| 1 | #!/bin/sh |
| 2 | # autopkgtest for arpack++ |
| 3 | # 2014/09/11 |
| 4 | # |
| 5 | # (C) 2014 Anton Gladky |
| 6 | |
| 7 | set -e |
| 8 | |
| 9 | WORKDIR=$(mktemp -d) |
| 10 | trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM |
| 11 | cd $WORKDIR |
| 12 | |
| 13 | cat <<EOF > demo.cc |
| 14 | /* |
| 15 | ARPACK++ v1.2 2/18/2000 |
| 16 | c++ interface to ARPACK code. |
| 17 | |
| 18 | MODULE SymSimp.cc. |
| 19 | Example program that illustrates how to solve a very simple |
| 20 | real symmetric standard eigenvalue problem in regular mode |
| 21 | using the ARSymStdEig class. |
| 22 | |
| 23 | 1) Problem description: |
| 24 | |
| 25 | In this example we try to solve A*x = x*lambda in regular mode |
| 26 | where A is the one dimensional discrete Laplacian on the interval |
| 27 | [0,1] with zero Dirichlet boundary conditions. |
| 28 | |
| 29 | 2) Data structure used to represent matrices A and B: |
| 30 | |
| 31 | When using ARSymGenEig, the user is required to provide a |
| 32 | class that contain as member function the matrix-vector |
| 33 | product w = Av. In this example, this class is called SymMatrix. |
| 34 | |
| 35 | 3) Included header files: |
| 36 | |
| 37 | File Contents |
| 38 | ----------- ------------------------------------------- |
| 39 | arssym.h The ARSymStdEig class definition. |
| 40 | blas1.h Some blas 1 routines. |
| 41 | |
| 42 | 4) ARPACK Authors: |
| 43 | |
| 44 | Richard Lehoucq |
| 45 | Kristyn Maschhoff |
| 46 | Danny Sorensen |
| 47 | Chao Yang |
| 48 | Dept. of Computational & Applied Mathematics |
| 49 | Rice University |
| 50 | Houston, Texas |
| 51 | */ |
| 52 | |
| 53 | #include "blas1c.h" |
| 54 | #include "arssym.h" |
| 55 | |
| 56 | template<class T> |
| 57 | class SymMatrix { |
| 58 | /* |
| 59 | |
| 60 | This simple class exemplifies how to create a matrix class |
| 61 | that can be used by ARPACK++. |
| 62 | Basically, SymMatrix is required to have a member function |
| 63 | that calculates the matrix-vector product SymMatrix*v, |
| 64 | where v is a vector with elements of type T. |
| 65 | |
| 66 | */ |
| 67 | |
| 68 | private: |
| 69 | |
| 70 | int n; |
| 71 | |
| 72 | public: |
| 73 | |
| 74 | int ncols() { return n; } |
| 75 | |
| 76 | void MultMv(T* v, T* w) |
| 77 | |
| 78 | /* |
| 79 | Matrix vector subroutine |
| 80 | where the matrix is the one dimensional discrete Laplacian on |
| 81 | the interval [0,1] with zero Dirichlet boundary conditions. |
| 82 | */ |
| 83 | |
| 84 | { |
| 85 | |
| 86 | int j; |
| 87 | T h2; |
| 88 | |
| 89 | const T two = 2.0; |
| 90 | |
| 91 | w[0] = two*v[0] - v[1]; |
| 92 | for (j=1; j<n-1; j++) { |
| 93 | w[j] = - v[j-1] + two*v[j] - v[j+1]; |
| 94 | } |
| 95 | w[n-1] = - v[n-2] + two*v[n-1]; |
| 96 | |
| 97 | // Scaling the vector w by (1 / h^2) using fortran blas routine scal. |
| 98 | |
| 99 | h2 = T((n+1)*(n+1)); |
| 100 | scal(n, h2, w, 1L); |
| 101 | |
| 102 | } // MultMv |
| 103 | |
| 104 | SymMatrix(int nval) { n = nval; } |
| 105 | |
| 106 | }; // SymMatrix. |
| 107 | |
| 108 | |
| 109 | template<class FLOAT, class EIGPROB> |
| 110 | void Solution(SymMatrix<FLOAT> &A, EIGPROB &Prob) |
| 111 | /* |
| 112 | This function prints eigenvalues and eigenvetors on standard "cout" |
| 113 | stream and exemplifies how to retrieve information from ARPACK++ classes. |
| 114 | */ |
| 115 | |
| 116 | { |
| 117 | |
| 118 | int i, n, nconv, mode; |
| 119 | FLOAT *Ax; |
| 120 | FLOAT *ResNorm; |
| 121 | |
| 122 | /* |
| 123 | ARPACK++ includes some functions that provide information |
| 124 | about the problem. For example, GetN furnishes the dimension |
| 125 | of the problem and ConvergedEigenvalues the number of |
| 126 | eigenvalues that attained the required accuracy. GetMode |
| 127 | indicates if the problem was solved in regular, |
| 128 | shift-and-invert or other mode. |
| 129 | */ |
| 130 | |
| 131 | n = Prob.GetN(); |
| 132 | nconv = Prob.ConvergedEigenvalues(); |
| 133 | mode = Prob.GetMode(); |
| 134 | |
| 135 | std::cout << std::endl << std::endl << "Testing ARPACK++ class ARSymEig \n"; |
| 136 | std::cout << "Real symmetric eigenvalue problem: A*x - lambda*x" << std::endl; |
| 137 | switch (mode) { |
| 138 | case 1: |
| 139 | std::cout << "Regular mode" << std::endl << std::endl; |
| 140 | break; |
| 141 | case 3: |
| 142 | std::cout << "Shift and invert mode" << std::endl << std::endl; |
| 143 | } |
| 144 | |
| 145 | std::cout << "Dimension of the system : " << n << std::endl; |
| 146 | std::cout << "Number of 'requested' eigenvalues : " << Prob.GetNev() << std::endl; |
| 147 | std::cout << "Number of 'converged' eigenvalues : " << nconv << std::endl; |
| 148 | std::cout << "Number of Arnoldi vectors generated: " << Prob.GetNcv() << std::endl; |
| 149 | std::cout << std::endl; |
| 150 | |
| 151 | /* |
| 152 | EigenvaluesFound is a boolean function that indicates |
| 153 | if the eigenvalues were found or not. Eigenvalue can be |
| 154 | used to obtain one of the "converged" eigenvalues. There |
| 155 | are other functions that return eigenvectors elements, |
| 156 | Schur vectors elements, residual vector elements, etc. |
| 157 | */ |
| 158 | |
| 159 | if (Prob.EigenvaluesFound()) { |
| 160 | std::cout << "Eigenvalues:" << std::endl; |
| 161 | for (i=0; i<nconv; i++) { |
| 162 | std::cout << " lambda[" << (i+1) << "]: " << Prob.Eigenvalue(i) << std::endl; |
| 163 | } |
| 164 | std::cout << std::endl; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | EigenvectorsFound indicates if the eigenvectors are |
| 169 | available. RawEigenvector is one of the functions that |
| 170 | provide raw access to ARPACK++ output data. Other functions |
| 171 | of this type include RawEigenvalues, RawEigenvectors, |
| 172 | RawSchurVector, RawResidualVector, etc. |
| 173 | */ |
| 174 | |
| 175 | if (Prob.EigenvectorsFound()) { |
| 176 | |
| 177 | // Printing the residual norm || A*x - lambda*x || |
| 178 | // for the nconv accurately computed eigenvectors. |
| 179 | |
| 180 | Ax = new FLOAT[n]; |
| 181 | ResNorm = new FLOAT[nconv]; |
| 182 | |
| 183 | for (i=0; i<nconv; i++) { |
| 184 | A.MultMv(Prob.RawEigenvector(i),Ax); |
| 185 | axpy(n, -Prob.Eigenvalue(i), Prob.RawEigenvector(i), 1, Ax, 1); |
| 186 | ResNorm[i] = nrm2(n, Ax, 1) / fabs(Prob.Eigenvalue(i)); |
| 187 | } |
| 188 | |
| 189 | for (i=0; i<nconv; i++) { |
| 190 | std::cout << "||A*x(" << (i+1) << ") - lambda(" << (i+1); |
| 191 | std::cout << ")*x(" << (i+1) << ")||: " << ResNorm[i] << "\n"; |
| 192 | } |
| 193 | std::cout << "\n"; |
| 194 | |
| 195 | delete[] Ax; |
| 196 | delete[] ResNorm; |
| 197 | |
| 198 | } |
| 199 | |
| 200 | } // Solution |
| 201 | |
| 202 | |
| 203 | void SimpleTest() |
| 204 | { |
| 205 | |
| 206 | int nconv; |
| 207 | |
| 208 | // Creating a double precision 100x100 matrix. |
| 209 | |
| 210 | SymMatrix<double> A(100); |
| 211 | |
| 212 | // Defining what we need: the four eigenvectors of A with smallest magnitude. |
| 213 | |
| 214 | ARSymStdEig<double, SymMatrix<double> > |
| 215 | dprob(A.ncols(), 4, &A, &SymMatrix<double>::MultMv, "SM"); |
| 216 | |
| 217 | /* |
| 218 | It is possible to pass other parameters directly to the constructor |
| 219 | of class ARSymStdEig in order to define a problem. The list |
| 220 | of parameters includes, among other values, the maximum number of |
| 221 | iterations allowed and the the relative accuracy used to define the |
| 222 | stopping criterion. Alternatively, it is also possible to use function |
| 223 | DefineParameters to set ARPACK++ variables after declaring dprob as an |
| 224 | object of ARSymStdEig class using the default constructor. |
| 225 | */ |
| 226 | |
| 227 | // Finding eigenvectors. |
| 228 | |
| 229 | nconv = dprob.FindEigenvectors(); |
| 230 | |
| 231 | /* |
| 232 | FindEigenvalues, FindArnoldiBasis and FindSchurVectors |
| 233 | are alternatives for FindEigenvectors. However, FindSchurVectors |
| 234 | can only be used with non-symmetric real and complex problems. |
| 235 | Some other functions like Eigenvectors, Eigenvalues and |
| 236 | EigenValVectors could also be used. These functions store the |
| 237 | desired data in user supplied vectors and matrices. |
| 238 | */ |
| 239 | |
| 240 | // Printing solution. |
| 241 | |
| 242 | Solution(A, dprob); |
| 243 | |
| 244 | } // SimpleTest |
| 245 | |
| 246 | |
| 247 | int main() |
| 248 | { |
| 249 | |
| 250 | // Solving a double precision problem with n = 100. |
| 251 | |
| 252 | SimpleTest(); |
| 253 | |
| 254 | } // main |
| 255 | |
| 256 | |
| 257 | EOF |
| 258 | |
| 259 | g++ -o demo demo.cc -I/usr/include/arpack++ -Wno-write-strings -lblas -larpack -larpack++ |
| 260 | echo "build: OK" |
| 261 | [ -x demo ] |
| 262 | ./demo |
| 263 | echo "run: OK" |