Line data Source code
1 : /*
2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3 : SLEPc - Scalable Library for Eigenvalue Problem Computations
4 : Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
5 :
6 : This file is part of SLEPc.
7 : SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9 : */
10 :
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";
16 :
17 : #include <slepcsvd.h>
18 :
19 : /*
20 : This example solves a GSVD problem for the bidiagonal matrices
21 :
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 : */
30 :
31 7 : int main(int argc,char **argv)
32 : {
33 7 : Mat A,B;
34 7 : SVD svd;
35 7 : Vec v0,w0; /* initial vectors */
36 7 : VecType vtype;
37 7 : PetscInt m=22,n=20,p=22,Istart,Iend,i,col[2];
38 7 : PetscScalar valsa[] = { 1, 2 }, valsb[] = { -1, 2 };
39 :
40 7 : PetscFunctionBeginUser;
41 7 : PetscCall(SlepcInitialize(&argc,&argv,NULL,help));
42 7 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL));
43 7 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
44 7 : PetscCall(PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL));
45 7 : PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\nGeneralized singular value decomposition, (%" PetscInt_FMT "+%" PetscInt_FMT ")x%" PetscInt_FMT "\n\n",m,p,n));
46 :
47 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48 : Generate the matrices
49 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
50 :
51 7 : PetscCall(MatCreate(PETSC_COMM_WORLD,&A));
52 7 : PetscCall(MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m,n));
53 7 : PetscCall(MatSetFromOptions(A));
54 7 : PetscCall(MatGetOwnershipRange(A,&Istart,&Iend));
55 161 : for (i=Istart;i<Iend;i++) {
56 154 : col[0]=i; col[1]=i+1;
57 154 : if (i<n-1) PetscCall(MatSetValues(A,1,&i,2,col,valsa,INSERT_VALUES));
58 154 : else if (i==n-1) PetscCall(MatSetValue(A,i,col[0],valsa[0],INSERT_VALUES));
59 : }
60 7 : PetscCall(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY));
61 7 : PetscCall(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY));
62 :
63 7 : PetscCall(MatCreate(PETSC_COMM_WORLD,&B));
64 7 : PetscCall(MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,m,n));
65 7 : PetscCall(MatSetFromOptions(B));
66 7 : PetscCall(MatGetOwnershipRange(B,&Istart,&Iend));
67 161 : for (i=Istart;i<Iend;i++) {
68 154 : col[0]=i-1; col[1]=i;
69 154 : if (i==0) PetscCall(MatSetValue(B,i,col[1],valsb[1],INSERT_VALUES));
70 147 : else if (i<n) PetscCall(MatSetValues(B,1,&i,2,col,valsb,INSERT_VALUES));
71 154 : else if (i==n) PetscCall(MatSetValue(B,i,col[0],valsb[0],INSERT_VALUES));
72 : }
73 7 : PetscCall(MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY));
74 7 : PetscCall(MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY));
75 :
76 : /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77 : Create the singular value solver, set options and solve
78 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
79 :
80 7 : PetscCall(SVDCreate(PETSC_COMM_WORLD,&svd));
81 7 : PetscCall(SVDSetOperators(svd,A,B));
82 7 : PetscCall(SVDSetFromOptions(svd));
83 :
84 : /*
85 : Set the initial vectors. This is optional, if not done the initial
86 : vectors are set to random values
87 : */
88 7 : PetscCall(MatCreateVecs(A,&v0,NULL)); /* right initial vector, length n */
89 7 : PetscCall(VecCreate(PETSC_COMM_WORLD,&w0)); /* left initial vector, length m+p */
90 7 : PetscCall(VecSetSizes(w0,PETSC_DECIDE,m+p));
91 7 : PetscCall(VecGetType(v0,&vtype));
92 7 : PetscCall(VecSetType(w0,vtype));
93 7 : PetscCall(VecSet(v0,1.0));
94 7 : PetscCall(VecSet(w0,1.0));
95 7 : PetscCall(SVDSetInitialSpaces(svd,1,&v0,1,&w0));
96 :
97 : /*
98 : Compute solution
99 : */
100 7 : PetscCall(SVDSolve(svd));
101 7 : PetscCall(SVDErrorView(svd,SVD_ERROR_NORM,NULL));
102 :
103 : /* Free work space */
104 7 : PetscCall(VecDestroy(&v0));
105 7 : PetscCall(VecDestroy(&w0));
106 7 : PetscCall(SVDDestroy(&svd));
107 7 : PetscCall(MatDestroy(&A));
108 7 : PetscCall(MatDestroy(&B));
109 7 : PetscCall(SlepcFinalize());
110 : return 0;
111 : }
112 :
113 : /*TEST
114 :
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
126 :
127 : test:
128 : suffix: 2
129 : args: -svd_nsv 3 -svd_type trlanczos -svd_monitor_conditioning
130 : requires: double
131 :
132 : TEST*/
|