Actual source code: test9.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[] = "Tests multiple calls to SVDSolve with different matrix size.\n\n"
 12:   "The command line options are:\n"
 13:   "  -n <n>, where <n> = matrix dimension.\n\n";

 15: #include <slepcsvd.h>

 17: /*
 18:    This example computes the singular values of an nxn Grcar matrix,
 19:    which is a nonsymmetric Toeplitz matrix:

 21:               |  1  1  1  1               |
 22:               | -1  1  1  1  1            |
 23:               |    -1  1  1  1  1         |
 24:               |       .  .  .  .  .       |
 25:           A = |          .  .  .  .  .    |
 26:               |            -1  1  1  1  1 |
 27:               |               -1  1  1  1 |
 28:               |                  -1  1  1 |
 29:               |                     -1  1 |

 31:  */

 33: int main(int argc,char **argv)
 34: {
 35:   Mat            A,B;
 36:   SVD            svd;
 37:   PetscInt       N=30,Istart,Iend,i,col[5];
 38:   PetscScalar    value[] = { -1, 1, 1, 1, 1 };

 41:   SlepcInitialize(&argc,&argv,(char*)0,help);
 42:   PetscOptionsGetInt(NULL,NULL,"-n",&N,NULL);
 43:   PetscPrintf(PETSC_COMM_WORLD,"\nSingular values of a Grcar matrix, n=%" PetscInt_FMT "\n\n",N);

 45:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 46:         Generate the matrix of size N
 47:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 49:   MatCreate(PETSC_COMM_WORLD,&A);
 50:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);
 51:   MatSetFromOptions(A);
 52:   MatSetUp(A);
 53:   MatGetOwnershipRange(A,&Istart,&Iend);
 54:   for (i=Istart;i<Iend;i++) {
 55:     col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
 56:     if (i==0) MatSetValues(A,1,&i,4,col+1,value+1,INSERT_VALUES);
 57:     else MatSetValues(A,1,&i,PetscMin(5,N-i+1),col,value,INSERT_VALUES);
 58:   }
 59:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 60:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 62:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 63:          Create the singular value solver, set options and solve
 64:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 66:   SVDCreate(PETSC_COMM_WORLD,&svd);
 67:   SVDSetOperators(svd,A,NULL);
 68:   SVDSetTolerances(svd,1e-6,1000);
 69:   SVDSetFromOptions(svd);
 70:   SVDSolve(svd);
 71:   SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);

 73:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 74:         Generate the matrix of size 2*N
 75:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 77:   N *= 2;
 78:   PetscPrintf(PETSC_COMM_WORLD,"\nSingular values of a Grcar matrix, n=%" PetscInt_FMT "\n\n",N);

 80:   MatCreate(PETSC_COMM_WORLD,&B);
 81:   MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,N,N);
 82:   MatSetFromOptions(B);
 83:   MatSetUp(B);
 84:   MatGetOwnershipRange(B,&Istart,&Iend);
 85:   for (i=Istart;i<Iend;i++) {
 86:     col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
 87:     if (i==0) MatSetValues(B,1,&i,4,col+1,value+1,INSERT_VALUES);
 88:     else MatSetValues(B,1,&i,PetscMin(5,N-i+1),col,value,INSERT_VALUES);
 89:   }
 90:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
 91:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

 93:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 94:        Solve again, calling SVDReset() since matrix size has changed
 95:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

 97:   SVDReset(svd);  /* if this is omitted, it will be called in SVDSetOperators() */
 98:   SVDSetOperators(svd,B,NULL);
 99:   SVDSolve(svd);
100:   SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL);

102:   /* Free work space */
103:   SVDDestroy(&svd);
104:   MatDestroy(&A);
105:   MatDestroy(&B);
106:   SlepcFinalize();
107:   return 0;
108: }

110: /*TEST

112:    test:
113:       suffix: 1
114:       args: -svd_type {{lanczos trlanczos cross cyclic lapack randomized}} -svd_nsv 3
115:       requires: double

117: TEST*/