Actual source code: test14.c
slepc-3.20.2 2024-03-15
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,(char*)0,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(MatSetUp(A));
53: PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
54: for (i=Istart;i<Iend;i++) {
55: col[0]=i; col[1]=i+1;
56: if (i<n-1) PetscCall(MatSetValues(A,1,&i,2,col,valsa,INSERT_VALUES));
57: else if (i==n-1) PetscCall(MatSetValue(A,i,col[0],valsa[0],INSERT_VALUES));
58: }
59: PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
60: PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
62: PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
63: PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,m,n));
64: PetscCall(MatSetFromOptions(B));
65: PetscCall(MatSetUp(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,NULL));
82: PetscCall(SVDSetTolerances(svd,PETSC_DEFAULT,1000));
83: PetscCall(SVDSetFromOptions(svd));
84: PetscCall(SVDSolve(svd));
85: PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
87: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88: Solve with second matrix
89: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
91: PetscCall(SVDSetOperators(svd,B,NULL));
92: PetscCall(SVDSolve(svd));
93: PetscCall(SVDErrorView(svd,SVD_ERROR_RELATIVE,NULL));
95: /* Free work space */
96: PetscCall(SVDDestroy(&svd));
97: PetscCall(MatDestroy(&A));
98: PetscCall(MatDestroy(&B));
99: PetscCall(SlepcFinalize());
100: return 0;
101: }
103: /*TEST
105: testset:
106: args: -svd_nsv 3
107: requires: !single
108: output_file: output/test14_1.out
109: test:
110: suffix: 1
111: args: -svd_type {{lanczos trlanczos lapack}}
112: test:
113: suffix: 1_cross
114: args: -svd_type cross -svd_cross_explicitmatrix {{0 1}}
115: test:
116: suffix: 1_cyclic
117: args: -svd_type cyclic -svd_cyclic_explicitmatrix {{0 1}}
119: testset:
120: args: -n 18 -svd_nsv 3
121: requires: !single
122: output_file: output/test14_2.out
123: test:
124: suffix: 2
125: args: -svd_type {{lanczos trlanczos lapack}}
126: test:
127: suffix: 2_cross
128: args: -svd_type cross -svd_cross_explicitmatrix {{0 1}}
129: test:
130: suffix: 2_cyclic
131: args: -svd_type cyclic -svd_cyclic_explicitmatrix {{0 1}}
133: TEST*/