Actual source code: test7.c

slepc-3.16.1 2021-11-17
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[] = "Test DSSVD.\n\n";

 13: #include <slepcds.h>

 15: int main(int argc,char **argv)
 16: {
 18:   DS             ds;
 19:   SlepcSC        sc;
 20:   PetscReal      sigma,rnorm,aux;
 21:   PetscScalar    *A,*U,*w,d;
 22:   PetscInt       i,j,k,n=15,m=10,m1,ld;
 23:   PetscViewer    viewer;
 24:   PetscBool      verbose,extrarow;

 26:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 27:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);
 28:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 29:   k = PetscMin(n,m);
 30:   PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type SVD - dimension %Dx%D.\n",n,m);
 31:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
 32:   PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow);

 34:   /* Create DS object */
 35:   DSCreate(PETSC_COMM_WORLD,&ds);
 36:   DSSetType(ds,DSSVD);
 37:   DSSetFromOptions(ds);
 38:   ld = n+2;  /* test leading dimension larger than n */
 39:   DSAllocate(ds,ld);
 40:   DSSetDimensions(ds,n,0,0);
 41:   DSSVDSetDimensions(ds,m);
 42:   DSSVDGetDimensions(ds,&m1);
 43:   if (m1!=m) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Inconsistent dimension value");
 44:   DSSetExtraRow(ds,extrarow);

 46:   /* Set up viewer */
 47:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
 48:   PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
 49:   DSView(ds,viewer);
 50:   PetscViewerPopFormat(viewer);

 52:   /* Fill with a rectangular Toeplitz matrix */
 53:   DSGetArray(ds,DS_MAT_A,&A);
 54:   for (i=0;i<k;i++) A[i+i*ld]=1.0;
 55:   for (j=1;j<3;j++) {
 56:     for (i=0;i<n-j;i++) { if ((i+j)<m) A[i+(i+j)*ld]=(PetscScalar)(j+1); }
 57:   }
 58:   for (j=1;j<n/2;j++) {
 59:     for (i=0;i<n-j;i++) { if ((i+j)<n && i<m) A[(i+j)+i*ld]=-1.0; }
 60:   }
 61:   if (extrarow) { A[n-2+m*ld]=1.0; A[n-1+m*ld]=1.0; }  /* really an extra column */
 62:   DSRestoreArray(ds,DS_MAT_A,&A);
 63:   DSSetState(ds,DS_STATE_RAW);
 64:   if (verbose) {
 65:     PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
 66:     PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
 67:   }
 68:   DSView(ds,viewer);

 70:   /* Solve */
 71:   PetscMalloc1(k,&w);
 72:   DSGetSlepcSC(ds,&sc);
 73:   sc->comparison    = SlepcCompareLargestReal;
 74:   sc->comparisonctx = NULL;
 75:   sc->map           = NULL;
 76:   sc->mapobj        = NULL;
 77:   DSSolve(ds,w,NULL);
 78:   DSSort(ds,w,NULL,NULL,NULL,NULL);
 79:   if (extrarow) { DSUpdateExtraRow(ds); }
 80:   if (verbose) {
 81:     PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
 82:     DSView(ds,viewer);
 83:   }

 85:   /* Print singular values */
 86:   PetscPrintf(PETSC_COMM_WORLD,"Computed singular values =\n");
 87:   for (i=0;i<k;i++) {
 88:     sigma = PetscRealPart(w[i]);
 89:     PetscViewerASCIIPrintf(viewer,"  %.5f\n",(double)sigma);
 90:   }

 92:   if (extrarow) {
 93:     /* Check that extra column is correct */
 94:     DSGetArray(ds,DS_MAT_A,&A);
 95:     DSGetArray(ds,DS_MAT_U,&U);
 96:     d = 0.0;
 97:     for (i=0;i<n;i++) d += A[i+m*ld]-U[n-2+i*ld]-U[n-1+i*ld];
 98:     if (PetscAbsScalar(d)>10*PETSC_MACHINE_EPSILON) {
 99:       PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in the extra row of %g\n",(double)PetscAbsScalar(d));
100:     }
101:     DSRestoreArray(ds,DS_MAT_A,&A);
102:     DSRestoreArray(ds,DS_MAT_U,&U);
103:   }

105:   /* Singular vectors */
106:   DSVectors(ds,DS_MAT_U,NULL,NULL);  /* all singular vectors */
107:   j = 0;
108:   rnorm = 0.0;
109:   DSGetArray(ds,DS_MAT_U,&U);
110:   for (i=0;i<n;i++) {
111:     aux = PetscAbsScalar(U[i+j*ld]);
112:     rnorm += aux*aux;
113:   }
114:   DSRestoreArray(ds,DS_MAT_U,&U);
115:   rnorm = PetscSqrtReal(rnorm);
116:   PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st U vector = %.3f\n",(double)rnorm);

118:   PetscFree(w);
119:   DSDestroy(&ds);
120:   SlepcFinalize();
121:   return ierr;
122: }

124: /*TEST

126:    test:
127:       suffix: 1
128:       requires: !single

130:    test:
131:       suffix: 2
132:       args: -extrarow
133:       requires: !single

135: TEST*/