Actual source code: test10.c

slepc-3.15.2 2021-09-20
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain

  6:    This file is part of SLEPc.
  7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9: */

 11: static char help[] = "Lanczos SVD. Also illustrates the use of SVDSetBV().\n\n"
 12:   "The command line options are:\n"
 13:   "  -m <m>, where <m> = matrix rows.\n"
 14:   "  -n <n>, where <n> = matrix columns (defaults to m+2).\n\n";

 16: #include <slepcsvd.h>

 18: /*
 19:    This example computes the singular values of a rectangular bidiagonal matrix

 21:               |  1  2                     |
 22:               |     1  2                  |
 23:               |        1  2               |
 24:           A = |          .  .             |
 25:               |             .  .          |
 26:               |                1  2       |
 27:               |                   1  2    |
 28:  */

 30: int main(int argc,char **argv)
 31: {
 32:   Mat            A;
 33:   SVD            svd;
 34:   PetscInt       m=20,n,Istart,Iend,i,k=6,col[2];
 35:   PetscScalar    value[] = { 1, 2 };
 36:   PetscBool      flg,oneside=PETSC_FALSE;
 37:   const char     *prefix;
 38:   BV             U,V;
 39:   Vec            u,v;

 42:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 43:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 44:   PetscOptionsGetInt(NULL,NULL,"-n",&n,&flg);
 45:   if (!flg) n=m+2;
 46:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
 47:   PetscPrintf(PETSC_COMM_WORLD,"\nRectangular bidiagonal matrix, m=%D n=%D\n\n",m,n);

 49:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 50:         Generate the matrix
 51:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 53:   MatCreate(PETSC_COMM_WORLD,&A);
 54:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n);
 55:   MatSetFromOptions(A);
 56:   MatSetUp(A);
 57:   MatGetOwnershipRange(A,&Istart,&Iend);
 58:   for (i=Istart;i<Iend;i++) {
 59:     col[0]=i; col[1]=i+1;
 60:     if (i<n-1) {
 61:       MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);
 62:     } else if (i==n-1) {
 63:       MatSetValue(A,i,col[0],value[0],INSERT_VALUES);
 64:     }
 65:   }
 66:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 67:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 68:   MatCreateVecs(A,&v,&u);

 70:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 71:          Create standalone BV objects to illustrate use of SVDSetBV()
 72:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 74:   BVCreate(PETSC_COMM_WORLD,&U);
 75:   PetscObjectSetName((PetscObject)U,"U");
 76:   BVSetSizesFromVec(U,u,k);
 77:   BVSetFromOptions(U);
 78:   BVCreate(PETSC_COMM_WORLD,&V);
 79:   PetscObjectSetName((PetscObject)V,"V");
 80:   BVSetSizesFromVec(V,v,k);
 81:   BVSetFromOptions(V);

 83:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 84:         Compute singular values
 85:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 87:   SVDCreate(PETSC_COMM_WORLD,&svd);
 88:   SVDSetBV(svd,V,U);
 89:   SVDSetOptionsPrefix(svd,"check_");
 90:   SVDAppendOptionsPrefix(svd,"myprefix_");
 91:   SVDGetOptionsPrefix(svd,&prefix);
 92:   PetscPrintf(PETSC_COMM_WORLD,"SVD prefix is currently: %s\n\n",prefix);
 93:   PetscObjectSetName((PetscObject)svd,"SVD_solver");

 95:   SVDSetOperators(svd,A,NULL);
 96:   SVDSetType(svd,SVDLANCZOS);
 97:   SVDSetFromOptions(svd);

 99:   PetscObjectTypeCompare((PetscObject)svd,SVDLANCZOS,&flg);
100:   if (flg) {
101:     SVDLanczosGetOneSide(svd,&oneside);
102:     PetscPrintf(PETSC_COMM_WORLD,"Running Lanczos %s\n\n",oneside?"(onesided)":"");
103:   }
104:   PetscObjectTypeCompare((PetscObject)svd,SVDTRLANCZOS,&flg);
105:   if (flg) {
106:     SVDTRLanczosGetOneSide(svd,&oneside);
107:     PetscPrintf(PETSC_COMM_WORLD,"Running thick-restart Lanczos %s\n\n",oneside?"(onesided)":"");
108:   }

110:   SVDSolve(svd);

112:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
113:                     Display solution and clean up
114:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
115:   SVDErrorView(svd,SVD_ERROR_RELATIVE,PETSC_VIEWER_STDOUT_WORLD);
116:   BVDestroy(&U);
117:   BVDestroy(&V);
118:   VecDestroy(&u);
119:   VecDestroy(&v);
120:   SVDDestroy(&svd);
121:   MatDestroy(&A);
122:   SlepcFinalize();
123:   return ierr;
124: }

126: /*TEST

128:    testset:
129:       args: -check_myprefix_svd_nsv 3
130:       requires: double
131:       test:
132:          suffix: 1
133:          args: -check_myprefix_svd_view_vectors ::ascii_info
134:       test:
135:          suffix: 2
136:          args: -check_myprefix_svd_type trlanczos -check_myprefix_svd_monitor -check_myprefix_svd_view_values ::ascii_matlab
137:          filter: sed -e "s/[0-9]\.[0-9]*e[+-]\([0-9]*\)/removed/g"
138:       test:
139:          suffix: 3
140:          args: -m 22 -n 20

142: TEST*/