Actual source code: test8.c
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2012, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7:
8: SLEPc is free software: you can redistribute it and/or modify it under the
9: terms of version 3 of the GNU Lesser General Public License as published by
10: the Free Software Foundation.
12: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
13: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15: more details.
17: You should have received a copy of the GNU Lesser General Public License
18: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
19: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: */
22: static char help[] = "Test DSSVD with compact storage.\n\n";
24: #include slepcds.h
28: int main( int argc, char **argv )
29: {
31: DS ds;
32: PetscReal *T,sigma;
33: PetscScalar *w;
34: PetscInt i,n=10,m,l=2,k=5,ld;
35: PetscViewer viewer;
36: PetscBool verbose;
38: SlepcInitialize(&argc,&argv,(char*)0,help);
39: PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);
40: m = n;
41: PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type SVD with compact storage - dimension %Dx%D.\n",n,m);
42: PetscOptionsGetInt(PETSC_NULL,"-l",&l,PETSC_NULL);
43: PetscOptionsGetInt(PETSC_NULL,"-k",&k,PETSC_NULL);
44: if (l>n || k>n || l>k) SETERRQ(PETSC_COMM_WORLD,1,"Wrong value of dimensions");
45: PetscOptionsHasName(PETSC_NULL,"-verbose",&verbose);
47: /* Create DS object */
48: DSCreate(PETSC_COMM_WORLD,&ds);
49: DSSetType(ds,DSSVD);
50: DSSetFromOptions(ds);
51: ld = n+2; /* test leading dimension larger than n */
52: DSAllocate(ds,ld);
53: DSSetDimensions(ds,n,m,l,k);
54: DSSetCompact(ds,PETSC_TRUE);
56: /* Set up viewer */
57: PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);
58: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);
59: DSView(ds,viewer);
60: PetscViewerPopFormat(viewer);
61: if (verbose) {
62: PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);
63: }
65: /* Fill upper arrow-tridiagonal matrix */
66: DSGetArrayReal(ds,DS_MAT_T,&T);
67: for (i=0;i<n;i++) T[i] = (PetscReal)(i+1);
68: for (i=l;i<n-1;i++) T[i+ld] = 1.0;
69: DSRestoreArrayReal(ds,DS_MAT_T,&T);
70: if (l==0 && k==0) {
71: DSSetState(ds,DS_STATE_INTERMEDIATE);
72: } else {
73: DSSetState(ds,DS_STATE_RAW);
74: }
75: if (verbose) {
76: PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");
77: DSView(ds,viewer);
78: }
80: /* Solve */
81: PetscMalloc(n*sizeof(PetscScalar),&w);
82: DSSetEigenvalueComparison(ds,SlepcCompareLargestReal,PETSC_NULL);
83: DSSolve(ds,w,PETSC_NULL);
84: DSSort(ds,w,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);
85: if (verbose) {
86: PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");
87: DSView(ds,viewer);
88: }
89:
90: /* Print singular values */
91: PetscPrintf(PETSC_COMM_WORLD,"Computed singular values =\n",n);
92: for (i=0;i<n;i++) {
93: sigma = PetscRealPart(w[i]);
94: PetscViewerASCIIPrintf(viewer," %.5F\n",sigma);
95: }
96: PetscFree(w);
97: DSDestroy(&ds);
98: SlepcFinalize();
99: return 0;
100: }