Actual source code: test22.c

slepc-3.18.2 2023-01-26
Report Typos and Errors
  1: /*
  2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
  4:    Copyright (c) 2002-, 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[] = "Test DSGSVD with compact storage.\n\n";

 13: #include <slepcds.h>

 15: int main(int argc,char **argv)
 16: {
 17:   DS             ds;
 18:   Mat            X;
 19:   Vec            x0;
 20:   SlepcSC        sc;
 21:   PetscReal      *T,*D,sigma,rnorm,aux,cond;
 22:   PetscScalar    *U,*V,*w,d;
 23:   PetscInt       i,n=10,l=0,k=0,ld;
 24:   PetscViewer    viewer;
 25:   PetscBool      verbose,test_dsview,extrarow;

 28:   SlepcInitialize(&argc,&argv,(char*)0,help);
 29:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 30:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GSVD with compact storage - dimension %" PetscInt_FMT "x%" PetscInt_FMT ".\n",n,n);
 31:   PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL);
 32:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
 34:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
 35:   PetscOptionsHasName(NULL,NULL,"-test_dsview",&test_dsview);
 36:   PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow);

 38:   /* Create DS object */
 39:   DSCreate(PETSC_COMM_WORLD,&ds);
 40:   DSSetType(ds,DSGSVD);
 41:   DSSetFromOptions(ds);
 42:   ld = n+2;  /* test leading dimension larger than n */
 43:   DSAllocate(ds,ld);
 44:   DSSetDimensions(ds,n,l,k);
 45:   DSGSVDSetDimensions(ds,n,PETSC_DECIDE);
 46:   DSSetCompact(ds,PETSC_TRUE);
 47:   DSSetExtraRow(ds,extrarow);

 49:   /* Set up viewer */
 50:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 51:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
 52:   DSView(ds,viewer);
 53:   PetscViewerPopFormat(viewer);

 55:   if (test_dsview) {
 56:     /* Fill A and B with dummy values to test DSView */
 57:     DSGetArrayReal(ds,DS_MAT_T,&T);
 58:     DSGetArrayReal(ds,DS_MAT_D,&D);
 59:     for (i=0;i<n;i++) { T[i] = i+1; D[i] = -i-1; }
 60:     for (i=0;i<n-1;i++) { T[i+ld] = -1.0; T[i+2*ld] = 1.0; }
 61:     DSRestoreArrayReal(ds,DS_MAT_T,&T);
 62:     DSRestoreArrayReal(ds,DS_MAT_D,&D);
 63:     DSView(ds,viewer);
 64:   }

 66:   /* Fill A and B with upper arrow-bidiagonal matrices
 67:      verifying that [A;B] has orthonormal columns */
 68:   DSGetArrayReal(ds,DS_MAT_T,&T);
 69:   DSGetArrayReal(ds,DS_MAT_D,&D);
 70:   for (i=0;i<n;i++) T[i] = (PetscReal)(i+1)/(n+1); /* diagonal of matrix A */
 71:   for (i=0;i<k;i++) D[i] = PetscSqrtReal(1.0-T[i]*T[i]);
 72:   for (i=l;i<k;i++) {
 73:     T[i+ld] = PetscSqrtReal((1.0-T[k]*T[k])/(1.0+T[i]*T[i]/(D[i]*D[i])))*0.5*(1.0/k); /* upper diagonal of matrix A */
 74:     T[i+2*ld] = -T[i+ld]*T[i]/D[i]; /* upper diagonal of matrix B */
 75:   }
 76:   aux = 1.0-T[k]*T[k];
 77:   for (i=l;i<k;i++) aux -= T[i+ld]*T[i+ld]+T[i+2*ld]*T[i+2*ld];
 78:   D[k] = PetscSqrtReal(aux);
 79:   for (i=k;i<n-1;i++) {
 80:     T[i+ld] = PetscSqrtReal((1.0-T[i+1]*T[i+1])/(1.0+T[i]*T[i]/(D[i]*D[i])))*0.5; /* upper diagonal of matrix A */
 81:     T[i+2*ld] = -T[i+ld]*T[i]/D[i]; /* upper diagonal of matrix B */
 82:     D[i+1] = PetscSqrtReal(1.0-T[i+1]*T[i+1]-T[ld+i]*T[ld+i]-T[2*ld+i]*T[2*ld+i]); /* diagonal of matrix B */
 83:   }
 84:   if (extrarow) { T[n-1+ld]=-1.0; T[n-1+2*ld]=1.0; }
 85:   /* Fill locked eigenvalues */
 86:   PetscMalloc1(n,&w);
 87:   for (i=0;i<l;i++) w[i] = T[i]/D[i];
 88:   DSRestoreArrayReal(ds,DS_MAT_T,&T);
 89:   DSRestoreArrayReal(ds,DS_MAT_D,&D);
 90:   if (l==0 && k==0) DSSetState(ds,DS_STATE_INTERMEDIATE);
 91:   else DSSetState(ds,DS_STATE_RAW);
 92:   if (verbose) {
 93:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
 94:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 95:     DSView(ds,viewer);
 96:   }

 98:   /* Condition number */
 99:   DSCond(ds,&cond);
100:   PetscPrintf(PETSC_COMM_WORLD,"Condition number = %.3f\n",(double)cond);

102:   /* Solve */
103:   DSGetSlepcSC(ds,&sc);
104:   sc->comparison    = SlepcCompareLargestReal;
105:   sc->comparisonctx = NULL;
106:   sc->map           = NULL;
107:   sc->mapobj        = NULL;
108:   DSSolve(ds,w,NULL);
109:   DSSort(ds,w,NULL,NULL,NULL,NULL);
110:   if (extrarow) DSUpdateExtraRow(ds);
111:   DSSynchronize(ds,w,NULL);
112:   if (verbose) {
113:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
114:     DSView(ds,viewer);
115:   }

117:   /* Print singular values */
118:   PetscPrintf(PETSC_COMM_WORLD,"Computed singular values =\n");
119:   for (i=0;i<n;i++) {
120:     sigma = PetscRealPart(w[i]);
121:     PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)sigma);
122:   }

124:   if (extrarow) {
125:     /* Check that extra row is correct */
126:     DSGetArrayReal(ds,DS_MAT_T,&T);
127:     DSGetArray(ds,DS_MAT_U,&U);
128:     DSGetArray(ds,DS_MAT_V,&V);
129:     d = 0.0;
130:     for (i=0;i<n;i++) d += T[i+ld]+U[n-1+i*ld];
131:     if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in A's extra row of %g\n",(double)PetscAbsScalar(d));
132:     d = 0.0;
133:     for (i=0;i<n;i++) d += T[i+2*ld]-V[n-1+i*ld];
134:     if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in B's extra row of %g\n",(double)PetscAbsScalar(d));
135:     DSRestoreArrayReal(ds,DS_MAT_T,&T);
136:     DSRestoreArray(ds,DS_MAT_U,&U);
137:     DSRestoreArray(ds,DS_MAT_V,&V);
138:   }

140:   /* Singular vectors */
141:   DSVectors(ds,DS_MAT_X,NULL,NULL);  /* all singular vectors */
142:   DSGetMat(ds,DS_MAT_X,&X);
143:   MatCreateVecs(X,NULL,&x0);
144:   MatGetColumnVector(X,x0,0);
145:   VecNorm(x0,NORM_2,&rnorm);
146:   DSRestoreMat(ds,DS_MAT_X,&X);
147:   VecDestroy(&x0);
148:   PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st X vector = %.3f\n",(double)rnorm);

150:   DSGetMat(ds,DS_MAT_U,&X);
151:   MatCreateVecs(X,NULL,&x0);
152:   MatGetColumnVector(X,x0,0);
153:   VecNorm(x0,NORM_2,&rnorm);
154:   DSRestoreMat(ds,DS_MAT_U,&X);
155:   VecDestroy(&x0);
156:   if (PetscAbs(rnorm-1.0)>10*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the 1st U vector has norm %g\n",(double)rnorm);

158:   DSGetMat(ds,DS_MAT_V,&X);
159:   MatCreateVecs(X,NULL,&x0);
160:   MatGetColumnVector(X,x0,0);
161:   VecNorm(x0,NORM_2,&rnorm);
162:   DSRestoreMat(ds,DS_MAT_V,&X);
163:   VecDestroy(&x0);
164:   if (PetscAbs(rnorm-1.0)>10*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the 1st V vector has norm %g\n",(double)rnorm);

166:   PetscFree(w);
167:   DSDestroy(&ds);
168:   SlepcFinalize();
169:   return 0;
170: }

172: /*TEST

174:    testset:
175:       requires: double
176:       test:
177:          suffix: 1
178:          args: -test_dsview
179:       test:
180:          suffix: 2
181:          args: -l 1 -k 4
182:       test:
183:          suffix: 2_extrarow
184:          filter: sed -e "s/extrarow//"
185:          args: -l 1 -k 4 -extrarow
186:          output_file: output/test22_2.out

188: TEST*/