Date: 2026-03-09T09:12:11+00:00

Actual source code: test22.c

      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;

     27:   PetscFunctionBeginUser;
     28:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     29:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     30:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GSVD with compact storage - dimension %" PetscInt_FMT "x%" PetscInt_FMT ".\n",n,n));
     31:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL));
     32:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL));
     33:   PetscCheck(l<=n && k<=n && l<=k,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Wrong value of dimensions");
     34:   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
     35:   PetscCall(PetscOptionsHasName(NULL,NULL,"-test_dsview",&test_dsview));
     36:   PetscCall(PetscOptionsHasName(NULL,NULL,"-extrarow",&extrarow));

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

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

     55:   if (test_dsview) {
     56:     /* Fill A and B with dummy values to test DSView */
     57:     PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
     58:     PetscCall(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:     PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
     62:     PetscCall(DSRestoreArrayReal(ds,DS_MAT_D,&D));
     63:     PetscCall(DSView(ds,viewer));
     64:   }

     66:   /* Fill A and B with upper arrow-bidiagonal matrices
     67:      verifying that [A;B] has orthonormal columns */
     68:   PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
     69:   PetscCall(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:   PetscCall(PetscMalloc1(n,&w));
     87:   for (i=0;i<l;i++) w[i] = T[i]/D[i];
     88:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
     89:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_D,&D));
     90:   if (l==0 && k==0) PetscCall(DSSetState(ds,DS_STATE_INTERMEDIATE));
     91:   else PetscCall(DSSetState(ds,DS_STATE_RAW));
     92:   if (verbose) {
     93:     PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
     94:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n"));
     95:     PetscCall(DSView(ds,viewer));
     96:   }

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

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

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

    124:   if (extrarow) {
    125:     /* Check that extra row is correct */
    126:     PetscCall(DSGetArrayReal(ds,DS_MAT_T,&T));
    127:     PetscCall(DSGetArray(ds,DS_MAT_U,&U));
    128:     PetscCall(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) PetscCall(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) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Warning: there is a mismatch in B's extra row of %g\n",(double)PetscAbsScalar(d)));
    135:     PetscCall(DSRestoreArrayReal(ds,DS_MAT_T,&T));
    136:     PetscCall(DSRestoreArray(ds,DS_MAT_U,&U));
    137:     PetscCall(DSRestoreArray(ds,DS_MAT_V,&V));
    138:   }

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

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

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

    166:   PetscCall(PetscFree(w));
    167:   PetscCall(DSDestroy(&ds));
    168:   PetscCall(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*/