Date: 2024-10-28T09:14:43+00:00

Actual source code: test27.c
slepc-3.22.1 2024-10-28
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 DSHSVD with dense storage.\n\n";

     13: #include <slepcds.h>

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

     25:   PetscFunctionBeginUser;
     26:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     27:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     28:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
     29:   k = PetscMin(n,m);
     30:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type HSVD - dimension %" PetscInt_FMT "x%" PetscInt_FMT ".\n",n,m));
     31:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL));
     32:   PetscCheck(p>=0 && p<=m,PETSC_COMM_WORLD,PETSC_ERR_USER_INPUT,"Wrong value %" PetscInt_FMT ", must be 0<=p<=%" PetscInt_FMT,p,m);
     33:   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));

     35:   /* Create DS object */
     36:   PetscCall(DSCreate(PETSC_COMM_WORLD,&ds));
     37:   PetscCall(DSSetType(ds,DSHSVD));
     38:   PetscCall(DSSetFromOptions(ds));
     39:   ld = PetscMax(n,m)+2;  /* test leading dimension larger than n */
     40:   PetscCall(DSAllocate(ds,ld));
     41:   PetscCall(DSSetDimensions(ds,n,0,0));
     42:   PetscCall(DSHSVDSetDimensions(ds,m));
     43:   PetscCall(DSHSVDGetDimensions(ds,&m1));
     44:   PetscCheck(m1==m,PETSC_COMM_WORLD,PETSC_ERR_PLIB,"Inconsistent dimension value");

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

     52:   /* Fill with a rectangular Toeplitz matrix */
     53:   PetscCall(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<m-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:   PetscCall(DSRestoreArray(ds,DS_MAT_A,&A));
     62:   /* Fill signature matrix */
     63:   PetscCall(DSGetArrayReal(ds,DS_MAT_D,&D));
     64:   for (i=0;i<n;i++) D[i] = (i<p)? -1.0: 1.0;
     65:   PetscCall(DSRestoreArrayReal(ds,DS_MAT_D,&D));
     66:   PetscCall(DSSetState(ds,DS_STATE_RAW));
     67:   if (verbose) {
     68:     PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB));
     69:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n"));
     70:   }
     71:   PetscCall(DSView(ds,viewer));

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

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

     94:   /* Singular vectors */
     95:   PetscCall(DSVectors(ds,DS_MAT_U,NULL,NULL));  /* all singular vectors */
     96:   j = 0;
     97:   rnorm = 0.0;
     98:   PetscCall(DSGetArray(ds,DS_MAT_U,&U));
     99:   for (i=0;i<n;i++) {
    100:     aux = PetscAbsScalar(U[i+j*ld]);
    101:     rnorm += aux*aux;
    102:   }
    103:   PetscCall(DSRestoreArray(ds,DS_MAT_U,&U));
    104:   rnorm = PetscSqrtReal(rnorm);
    105:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Norm of 1st U vector = %.3f\n",(double)rnorm));

    107:   PetscCall(PetscFree(w));
    108:   PetscCall(DSDestroy(&ds));
    109:   PetscCall(SlepcFinalize());
    110:   return 0;
    111: }

    113: /*TEST

    115:    test:
    116:       suffix: 1
    117:       requires: !single

    119: TEST*/