Date: 2024-04-26T10:22:49+00:00

Actual source code: test18.c
slepc-3.21.1 2024-04-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[] = "Test GSVD with user-provided initial vectors.\n\n"
     12:   "The command line options are:\n"
     13:   "  -m <m>, where <m> = number of rows of A.\n"
     14:   "  -n <n>, where <n> = number of columns of A.\n"
     15:   "  -p <p>, where <p> = number of rows of B.\n\n";

     17: #include <slepcsvd.h>

     19: /*
     20:    This example solves a GSVD problem for the bidiagonal matrices

     22:               |  1  2                     |       |  2                        |
     23:               |     1  2                  |       | -1  2                     |
     24:               |        1  2               |       |    -1  2                  |
     25:           A = |          .  .             |   B = |       .  .                |
     26:               |             .  .          |       |          .  .             |
     27:               |                1  2       |       |            -1  2          |
     28:               |                   1  2    |       |               -1  2       |
     29:  */

     31: int main(int argc,char **argv)
     32: {
     33:   Mat            A,B;
     34:   SVD            svd;
     35:   Vec            v0,w0;           /* initial vectors */
     36:   VecType        vtype;
     37:   PetscInt       m=22,n=20,p=22,Istart,Iend,i,col[2];
     38:   PetscScalar    valsa[] = { 1, 2 }, valsb[] = { -1, 2 };

     40:   PetscFunctionBeginUser;
     41:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
     42:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
     43:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
     44:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL));
     45:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized singular value decomposition, (%" PetscInt_FMT "+%" PetscInt_FMT ")x%" PetscInt_FMT "\n\n",m,p,n));

     47:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     48:                      Generate the matrices
     49:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     51:   PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
     52:   PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n));
     53:   PetscCall(MatSetFromOptions(A));
     54:   PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
     55:   for (i=Istart;i<Iend;i++) {
     56:     col[0]=i; col[1]=i+1;
     57:     if (i<n-1) PetscCall(MatSetValues(A,1,&i,2,col,valsa,INSERT_VALUES));
     58:     else if (i==n-1) PetscCall(MatSetValue(A,i,col[0],valsa[0],INSERT_VALUES));
     59:   }
     60:   PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
     61:   PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));

     63:   PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
     64:   PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,m,n));
     65:   PetscCall(MatSetFromOptions(B));
     66:   PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
     67:   for (i=Istart;i<Iend;i++) {
     68:     col[0]=i-1; col[1]=i;
     69:     if (i==0) PetscCall(MatSetValue(B,i,col[1],valsb[1],INSERT_VALUES));
     70:     else if (i<n) PetscCall(MatSetValues(B,1,&i,2,col,valsb,INSERT_VALUES));
     71:     else if (i==n) PetscCall(MatSetValue(B,i,col[0],valsb[0],INSERT_VALUES));
     72:   }
     73:   PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
     74:   PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));

     76:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     77:          Create the singular value solver, set options and solve
     78:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     80:   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
     81:   PetscCall(SVDSetOperators(svd,A,B));
     82:   PetscCall(SVDSetFromOptions(svd));

     84:   /*
     85:      Set the initial vectors. This is optional, if not done the initial
     86:      vectors are set to random values
     87:   */
     88:   PetscCall(MatCreateVecs(A,&v0,NULL));        /* right initial vector, length n */
     89:   PetscCall(VecCreate(PETSC_COMM_WORLD,&w0));  /* left initial vector, length m+p */
     90:   PetscCall(VecSetSizes(w0,PETSC_DECIDE,m+p));
     91:   PetscCall(VecGetType(v0,&vtype));
     92:   PetscCall(VecSetType(w0,vtype));
     93:   PetscCall(VecSet(v0,1.0));
     94:   PetscCall(VecSet(w0,1.0));
     95:   PetscCall(SVDSetInitialSpaces(svd,1,&v0,1,&w0));

     97:   /*
     98:      Compute solution
     99:   */
    100:   PetscCall(SVDSolve(svd));
    101:   PetscCall(SVDErrorView(svd,SVD_ERROR_NORM,NULL));

    103:   /* Free work space */
    104:   PetscCall(VecDestroy(&v0));
    105:   PetscCall(VecDestroy(&w0));
    106:   PetscCall(SVDDestroy(&svd));
    107:   PetscCall(MatDestroy(&A));
    108:   PetscCall(MatDestroy(&B));
    109:   PetscCall(SlepcFinalize());
    110:   return 0;
    111: }

    113: /*TEST

    115:    testset:
    116:       args: -svd_nsv 3
    117:       requires: !single
    118:       output_file: output/test18_1.out
    119:       test:
    120:          suffix: 1
    121:          args: -svd_type {{lapack cross cyclic}}
    122:       test:
    123:          suffix: 1_trlanczos
    124:          args: -svd_type trlanczos -svd_trlanczos_gbidiag {{single upper lower}}
    125:          requires: !__float128

    127:    test:
    128:       suffix: 2
    129:       args: -svd_nsv 3 -svd_type trlanczos -svd_monitor_conditioning
    130:       requires: double

    132: TEST*/