Subversion Repositories slepc-dev

Rev

Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1376 slepc 1
/*
2
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1672 slepc 3
   SLEPc - Scalable Library for Eigenvalue Problem Computations
2116 eromero 4
   Copyright (c) 2002-2010, Universidad Politecnica de Valencia, Spain
6 dsic.upv.es!jroman 5
 
1672 slepc 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.
11
 
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.
16
 
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/>.
1376 slepc 19
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20
*/
21
 
1030 slepc 22
static char help[] = "Estimates the 2-norm condition number of a matrix A, that is, the ratio of the largest to the smallest singular values of A. "
161 dsic.upv.es!antodo 23
  "The matrix is a Grcar matrix.\n\n"
1156 slepc 24
  "The command line options are:\n"
6 dsic.upv.es!jroman 25
  "  -n <n>, where <n> = matrix dimension.\n\n";
26
 
1253 slepc 27
#include "slepcsvd.h"
6 dsic.upv.es!jroman 28
 
29
/*
1253 slepc 30
   This example computes the singular values of an nxn Grcar matrix,
31
   which is a nonsymmetric Toeplitz matrix:
6 dsic.upv.es!jroman 32
 
33
              |  1  1  1  1               |
34
              | -1  1  1  1  1            |
35
              |    -1  1  1  1  1         |
36
              |       .  .  .  .  .       |
37
          A = |          .  .  .  .  .    |
38
              |            -1  1  1  1  1 |
39
              |               -1  1  1  1 |
40
              |                  -1  1  1 |
41
              |                     -1  1 |
42
 
43
 */
44
 
45
#undef __FUNCT__
46
#define __FUNCT__ "main"
47
int main( int argc, char **argv )
48
{
1090 slepc 49
  PetscErrorCode ierr;
50
  Mat            A;               /* Grcar matrix */
1253 slepc 51
  SVD            svd;             /* singular value solver context */
1505 slepc 52
  PetscInt       N=30, Istart, Iend, i, col[5], nconv1, nconv2;
1253 slepc 53
  PetscScalar    value[] = { -1, 1, 1, 1, 1 };
1090 slepc 54
  PetscReal      sigma_1, sigma_n;
6 dsic.upv.es!jroman 55
 
56
  SlepcInitialize(&argc,&argv,(char*)0,help);
57
 
58
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&N,PETSC_NULL);CHKERRQ(ierr);
99 dsic.upv.es!antodo 59
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\nEstimate the condition number of a Grcar matrix, n=%d\n\n",N);CHKERRQ(ierr);
6 dsic.upv.es!jroman 60
 
61
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62
        Generate the matrix
63
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
64
 
828 dsic.upv.es!antodo 65
  ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
66
  ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr);
6 dsic.upv.es!jroman 67
  ierr = MatSetFromOptions(A);CHKERRQ(ierr);
68
 
69
  ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
70
  for( i=Istart; i<Iend; i++ ) {
71
    col[0]=i-1; col[1]=i; col[2]=i+1; col[3]=i+2; col[4]=i+3;
72
    if (i==0) {
73
      ierr = MatSetValues(A,1,&i,4,col+1,value+1,INSERT_VALUES);CHKERRQ(ierr);
74
    }
75
    else {
76
      ierr = MatSetValues(A,1,&i,PetscMin(5,N-i+1),col,value,INSERT_VALUES);CHKERRQ(ierr);
77
    }
78
  }
79
 
80
  ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
81
  ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
82
 
83
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1253 slepc 84
             Create the singular value solver and set the solution method
6 dsic.upv.es!jroman 85
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
86
 
87
  /*
1253 slepc 88
     Create singular value context
6 dsic.upv.es!jroman 89
  */
1253 slepc 90
  ierr = SVDCreate(PETSC_COMM_WORLD,&svd);CHKERRQ(ierr);
6 dsic.upv.es!jroman 91
 
92
  /*
1253 slepc 93
     Set operator
6 dsic.upv.es!jroman 94
  */
1253 slepc 95
  ierr = SVDSetOperator(svd,A);CHKERRQ(ierr);
6 dsic.upv.es!jroman 96
 
97
  /*
98
     Set solver parameters at runtime
99
  */
1253 slepc 100
  ierr = SVDSetFromOptions(svd);CHKERRQ(ierr);
1575 slepc 101
  ierr = SVDSetDimensions(svd,1,PETSC_IGNORE,PETSC_IGNORE);CHKERRQ(ierr);
6 dsic.upv.es!jroman 102
 
103
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
104
                      Solve the eigensystem
105
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
106
 
132 dsic.upv.es!antodo 107
  /*
108
     First request an eigenvalue from one end of the spectrum
109
  */
1272 slepc 110
  ierr = SVDSetWhichSingularTriplets(svd,SVD_LARGEST);CHKERRQ(ierr);
1253 slepc 111
  ierr = SVDSolve(svd);CHKERRQ(ierr);
99 dsic.upv.es!antodo 112
  /*
1253 slepc 113
     Get number of converged singular values
99 dsic.upv.es!antodo 114
  */
1253 slepc 115
  ierr = SVDGetConverged(svd,&nconv1);CHKERRQ(ierr);
116
  /*
117
     Get converged singular values: largest singular value is stored in sigma_1.
118
     In this example, we are not interested in the singular vectors
119
  */
132 dsic.upv.es!antodo 120
  if (nconv1 > 0) {
1253 slepc 121
    ierr = SVDGetSingularTriplet(svd,0,&sigma_1,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1286 slepc 122
  } else {
123
    ierr = PetscPrintf(PETSC_COMM_WORLD," Unable to compute large singular value!\n\n");CHKERRQ(ierr);
124
  }
99 dsic.upv.es!antodo 125
 
132 dsic.upv.es!antodo 126
  /*
127
     Request an eigenvalue from the other end of the spectrum
99 dsic.upv.es!antodo 128
  */
1272 slepc 129
  ierr = SVDSetWhichSingularTriplets(svd,SVD_SMALLEST);CHKERRQ(ierr);
1253 slepc 130
  ierr = SVDSolve(svd);CHKERRQ(ierr);
6 dsic.upv.es!jroman 131
  /*
132
     Get number of converged eigenpairs
133
  */
1253 slepc 134
  ierr = SVDGetConverged(svd,&nconv2);CHKERRQ(ierr);
99 dsic.upv.es!antodo 135
  /*
1253 slepc 136
     Get converged singular values: smallest singular value is stored in sigma_n.
137
     As before, we are not interested in the singular vectors
99 dsic.upv.es!antodo 138
  */
132 dsic.upv.es!antodo 139
  if (nconv2 > 0) {
1253 slepc 140
    ierr = SVDGetSingularTriplet(svd,0,&sigma_n,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
1286 slepc 141
  } else {
142
    ierr = PetscPrintf(PETSC_COMM_WORLD," Unable to compute small singular value!\n\n");CHKERRQ(ierr);
143
  }
6 dsic.upv.es!jroman 144
 
99 dsic.upv.es!antodo 145
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146
                    Display solution and clean up
147
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
132 dsic.upv.es!antodo 148
  if (nconv1 > 0 && nconv2 > 0) {
149
    ierr = PetscPrintf(PETSC_COMM_WORLD," Computed singular values: sigma_1=%6f, sigma_n=%6f\n",sigma_1,sigma_n);CHKERRQ(ierr);
150
    ierr = PetscPrintf(PETSC_COMM_WORLD," Estimated condition number: sigma_1/sigma_n=%6f\n\n",sigma_1/sigma_n);CHKERRQ(ierr);
1286 slepc 151
  }  
99 dsic.upv.es!antodo 152
 
6 dsic.upv.es!jroman 153
  /*
154
     Free work space
155
  */
1253 slepc 156
  ierr = SVDDestroy(svd);CHKERRQ(ierr);
6 dsic.upv.es!jroman 157
  ierr = MatDestroy(A);CHKERRQ(ierr);
158
  ierr = SlepcFinalize();CHKERRQ(ierr);
159
  return 0;
160
}
161