Actual source code: test24.c

slepc-3.18.0 2022-10-01
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 and rectangular matrix A.\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,m,l=0,k=0,ld;
 24:   PetscViewer    viewer;
 25:   PetscBool      verbose,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+1,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,"-extrarow",&extrarow);
 36:   m = n+1;

 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,m,l,k);
 45:   DSGSVDSetDimensions(ds,n,n);
 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:   /* Fill A and B with lower/upper arrow-bidiagonal matrices
 56:      verifying that [A;B] has orthonormal columns */
 57:   DSGetArrayReal(ds,DS_MAT_T,&T);
 58:   DSGetArrayReal(ds,DS_MAT_D,&D);
 59:   for (i=0;i<n;i++) T[i] = (PetscReal)(i+1)/(n+1); /* diagonal of matrix A */
 60:   for (i=0;i<k;i++) D[i] = PetscSqrtReal(1.0-T[i]*T[i]);
 61:   for (i=l;i<k;i++) {
 62:     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 */
 63:     T[i+2*ld] = -T[i+ld]*T[i]/D[i]; /* upper diagonal of matrix B */
 64:   }
 65:   aux = 1.0-T[k]*T[k];
 66:   for (i=l;i<k;i++) aux -= T[i+ld]*T[i+ld]+T[i+2*ld]*T[i+2*ld];
 67:   T[k+ld] = PetscSqrtReal((1.0-aux)*.1);
 68:   aux -= T[k+ld]*T[k+ld];
 69:   D[k] = PetscSqrtReal(aux);
 70:   for (i=k+1;i<n;i++) {
 71:     T[i-1+2*ld] = -T[i-1+ld]*T[i]/D[i-1]; /* upper diagonal of matrix B */
 72:     aux = 1.0-T[i]*T[i]-T[2*ld+i-1]*T[2*ld+i-1];
 73:     T[i+ld] = PetscSqrtReal(aux)*.1; /* upper diagonal of matrix A */
 74:     D[i] = PetscSqrtReal(aux-T[i+ld]*T[i+ld]);
 75:   }
 76:   if (extrarow) { T[n]=-1.0; T[n-1+2*ld]=1.0; }
 77:   /* Fill locked eigenvalues */
 78:   PetscMalloc1(n,&w);
 79:   for (i=0;i<l;i++) w[i] = T[i]/D[i];
 80:   DSRestoreArrayReal(ds,DS_MAT_T,&T);
 81:   DSRestoreArrayReal(ds,DS_MAT_D,&D);
 82:   if (l==0 && k==0) DSSetState(ds,DS_STATE_INTERMEDIATE);
 83:   else DSSetState(ds,DS_STATE_RAW);
 84:   if (verbose) {
 85:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
 86:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 87:     DSView(ds,viewer);
 88:   }

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

 94:   /* Solve */
 95:   DSGetSlepcSC(ds,&sc);
 96:   sc->comparison    = SlepcCompareLargestReal;
 97:   sc->comparisonctx = NULL;
 98:   sc->map           = NULL;
 99:   sc->mapobj        = NULL;
100:   DSSolve(ds,w,NULL);
101:   DSSort(ds,w,NULL,NULL,NULL,NULL);
102:   if (extrarow) DSUpdateExtraRow(ds);
103:   DSSynchronize(ds,w,NULL);
104:   if (verbose) {
105:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
106:     DSView(ds,viewer);
107:   }

109:   /* Print singular values */
110:   PetscPrintf(PETSC_COMM_WORLD,"Computed singular values =\n");
111:   for (i=0;i<n;i++) {
112:     sigma = PetscRealPart(w[i]);
113:     PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)sigma);
114:   }

116:   if (extrarow) {
117:     /* Check that extra row is correct */
118:     DSGetArrayReal(ds,DS_MAT_T,&T);
119:     DSGetArray(ds,DS_MAT_U,&U);
120:     DSGetArray(ds,DS_MAT_V,&V);
121:     d = 0.0;
122:     for (i=0;i<n;i++) d += T[i+ld]+U[n+i*ld];
123:     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));
124:     d = 0.0;
125:     for (i=0;i<n;i++) d += T[i+2*ld]-V[n-1+i*ld];
126:     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));
127:     DSRestoreArrayReal(ds,DS_MAT_T,&T);
128:     DSRestoreArray(ds,DS_MAT_U,&U);
129:     DSRestoreArray(ds,DS_MAT_V,&V);
130:   }

132:   /* Singular vectors */
133:   DSVectors(ds,DS_MAT_X,NULL,NULL);  /* all singular vectors */
134:   DSGetMat(ds,DS_MAT_X,&X);
135:   MatCreateVecs(X,NULL,&x0);
136:   MatGetColumnVector(X,x0,0);
137:   VecNorm(x0,NORM_2,&rnorm);
138:   DSRestoreMat(ds,DS_MAT_X,&X);
139:   VecDestroy(&x0);
140:   PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st X vector = %.3f\n",(double)rnorm);

142:   DSGetMat(ds,DS_MAT_U,&X);
143:   MatCreateVecs(X,NULL,&x0);
144:   MatGetColumnVector(X,x0,0);
145:   VecNorm(x0,NORM_2,&rnorm);
146:   DSRestoreMat(ds,DS_MAT_U,&X);
147:   VecDestroy(&x0);
148:   if (PetscAbs(rnorm-1.0)>10*PETSC_MACHINE_EPSILON) PetscPrintf(PETSC_COMM_WORLD,"Warning: the 1st U vector has norm %g\n",(double)rnorm);

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

158:   PetscFree(w);
159:   DSDestroy(&ds);
160:   SlepcFinalize();
161:   return 0;
162: }

164: /*TEST

166:    testset:
167:       requires: double
168:       output_file: output/test24_1.out
169:       test:
170:          suffix: 1
171:          args: -l 1 -k 4
172:       test:
173:          suffix: 1_extrarow
174:          filter: sed -e "s/extrarow//"
175:          args: -l 1 -k 4 -extrarow

177: TEST*/