Date: 2026-03-31T13:23:07+00:00

Actual source code: test14.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[] = "Tests multiple calls to SVDSolve with equal matrix size.\n\n"
     12:   "The command line options are:\n"
     13:   "  -m <m>, where <m> = matrix rows.\n"
     14:   "  -n <n>, where <n> = matrix columns (defaults to m+2).\n\n";

     16: #include <slepcsvd.h>

     18: /*
     19:    This example computes the singular values of two rectangular bidiagonal matrices

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

     30: int main(int argc,char **argv)
     31: {
     32:   Mat            A,B;
     33:   SVD            svd;
     34:   PetscInt       m=20,n,Istart,Iend,i,col[2];
     35:   PetscScalar    valsa[] = { 1, 2 }, valsb[] = { 2, 1 };
     36:   PetscBool      flg;

     38:   PetscFunctionBeginUser;
     39:   PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
     40:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
     41:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,&flg));
     42:   if (!flg) n=m+2;
     43:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nRectangular bidiagonal matrix, m=%" PetscInt_FMT " n=%" PetscInt_FMT "\n\n",m,n));

     45:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     46:                      Generate the matrices
     47:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

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

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

     74:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     75:          Create the singular value solver, set options and solve
     76:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     78:   PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
     79:   PetscCall(SVDSetOperators(svd,A,NULL));
     80:   PetscCall(SVDSetTolerances(svd,PETSC_CURRENT,1000));
     81:   PetscCall(SVDSetFromOptions(svd));
     82:   PetscCall(SVDSolve(svd));
     83:   PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));

     85:   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     86:                        Solve with second matrix
     87:      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

     89:   PetscCall(SVDSetOperators(svd,B,NULL));
     90:   PetscCall(SVDSolve(svd));
     91:   PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));

     93:   /* Free work space */
     94:   PetscCall(SVDDestroy(&svd));
     95:   PetscCall(MatDestroy(&A));
     96:   PetscCall(MatDestroy(&B));
     97:   PetscCall(SlepcFinalize());
     98:   return 0;
     99: }

    101: /*TEST

    103:    testset:
    104:       args: -svd_nsv 3
    105:       requires: !single
    106:       output_file: output/test14_1.out
    107:       test:
    108:          suffix: 1
    109:          args: -svd_type {{lanczos trlanczos lapack}}
    110:       test:
    111:          suffix: 1_cross
    112:          args: -svd_type cross -svd_cross_explicitmatrix {{0 1}}
    113:       test:
    114:          suffix: 1_cyclic
    115:          args: -svd_type cyclic -svd_cyclic_explicitmatrix {{0 1}}

    117:    testset:
    118:       args: -n 18 -svd_nsv 3
    119:       requires: !single
    120:       output_file: output/test14_2.out
    121:       test:
    122:          suffix: 2
    123:          args: -svd_type {{lanczos trlanczos lapack}}
    124:       test:
    125:          suffix: 2_cross
    126:          args: -svd_type cross -svd_cross_explicitmatrix {{0 1}}
    127:       test:
    128:          suffix: 2_cyclic
    129:          args: -svd_type cyclic -svd_cyclic_explicitmatrix {{0 1}}

    131: TEST*/