|
FILTLAN 1.0a
|
00001 #ifndef LANEIG_H 00002 #define LANEIG_H 00003 00007 #include <math.h> // for sqrt, pow, etc. 00008 #include <iostream> // for ostream, cout, cerr, endl, etc. (under namespace std) 00009 00010 #ifdef USE_NAMESPACE 00011 using namespace MATKIT; 00012 namespace MATKIT { 00013 #endif 00014 00015 class Vector; 00016 class Matrix; 00017 class SymmetricMatrix; 00018 class SparseMatrix; 00019 00020 #ifdef USE_NAMESPACE 00021 } // end of namespace MATKIT 00022 #endif 00023 00024 00025 00031 00032 // an instance of this class, taken by LanczosEigenSolver(), is a collection of options for the Lanczos procedure to solve symmetric eigenvalue problems 00033 struct LanczosOptions { 00035 00038 // this parameter tells whether eigenvectors are wanted or not (default true) 00039 // note that the standard Lanczos procedure computes the eigenvalues first 00040 // the computation of eigenvectors is unnecessary if only the eigenvalues are required 00041 bool wantEigVec; 00043 00047 // minimum number of Lanczos iterations 00048 // this is the number of iterations performed before checking convergence 00049 // the default value is 0, which means that it will be determined by LanczosOptions::defaultMinIterFactor 00050 mkIndex minIter; 00052 00056 // maximum number of Lanczos iterations 00057 // this is the maximum number of iterations performed even if some desired eigenvalues do not yet converge 00058 // the default value is 0, which means that it will be determined by LanczosOptions::defaultMaxIterFactor 00059 mkIndex maxIter; 00061 00076 // extraIter is the extra number of Lanczos iterations (default 100) 00077 // this is the number of Lanczos iterations performed after the Ritz values are `deemed' converged to the desired eigenvalues 00078 // there are two reasons to perform more Lanczos iterations 00079 // first, to improve the accuracy of the computed eigenvalues / eigenvectors 00080 // second, it is possible that there are missing eigenvalues 00081 // for instance, when the 10 largest Ritz values are converged to 10 eigenvalues, 00082 // it is possible that the 10 eigenvalues are not the largest, 00083 // because one or more of the 10 largest eigenvalues do not yet show up as Ritz values 00084 // nevertheless, these missing eigenvalues, if any, may come out by applying more iterations 00085 // this issue can be serious in a filtered Lanczos procedure with a high degree polynomial 00086 // finally, the phase of such extra iterations may be repeated for a few times until all desired eigenvalues are found 00087 mkIndex extraIter; 00089 // the convergence is checked every `stride' iterations (default 10) 00090 mkIndex stride; 00092 00094 // The default LanczosOptions::minIter, if not set, is LanczosOptions::defaultMinIterFactor times the number of (largest or smallest) eigenvalues requested 00095 // the default value of LanczosOptions::defaultMinIterFactor is 5 00096 mkIndex defaultMinIterFactor; 00098 00100 // The default LanczosOptions::maxIter, if not set, is 500 plus LanczosOptions::defaultMaxIterFactor times the number of (largest or smallest) eigenvalues requested 00101 // the default value of LanczosOptions::defaultMaxIterFactor is 50 00102 mkIndex defaultMaxIterFactor; 00104 // tolerance of eigenvalues, defined in the relative and average sense, for convergence check (default eps^{0.8}, with eps the machine epsilon) 00105 Real tol; 00107 00110 // the initial Lanczos vector 00111 // if v0.Length() is not the number of rows/columns of the matrix, then 00112 // a random vector is generated for the initial Lanczos vector as the default case 00113 Vector v0; 00114 00115 // variables for reorthogonalization 00117 00123 // a flag to tell which reorthogonalization scheme is used (default 1): 00124 // 0 for no reorthogonalization (reserved option); 00125 // 1 for partial reorthogonalization (default and recommended); 00126 // 2 for full reorthogonalization 00127 int reorth; 00129 00137 // reorthogonalization is doubled in the iterations with nrm < doubleReorthGamma*nrm_old 00138 // the default value of doubleReorthGamma is sqrt(0.5) 00139 // nrm_old and nrm are the norms of the latest Lanczos vector before and after reorthogonalization 00140 // doubleReorthGamma==0.0 (or smaller) means that reorthogonalization is never doubled 00141 // doubleReorthGamma==1.0 (or larger) means that reorthogonalization, whenever it is performed, is always doubled 00142 // this parameter is effective only if LanczosOptions::reorh==1 or LanczosOptions::reorth==2 (i.e. partial or full reorthogonalization) 00143 Real doubleReorthGamma; 00145 00150 // local reorthogonalization is performed if beta[j-1] < localReorthGamma*beta[j] or localReorthGamma >= 1.0, 00151 // the default value of localReorthGamma is sqrt(0.5) 00152 // where beta[j] is the latest beta and beta[j-1] is the previous beta at j-th Lanczos iteration 00153 // localReorthGamma=0.0 (or smaller) means that local reorthogonalization is never performed 00154 // this parameter is effective only if LanczosOptions::reorth==1 (i.e. partial reorthogonalization) as input of LanczosEigenSolver(). 00155 Real localReorthGamma; 00157 00173 // a flag to tell which partial reorthogonalization strategy is used: 00174 // 0 for reorthogonalization against previous Lanczos vectors in groups; each group consists of 00175 // v(i),v(i+1),...,v(j) with omega(i),omega(i+1),...,omega(j) all greater than eta, and 00176 // there is v(k) with omega(k) > delta, i<=k<=j 00177 // 1 for reorthogonalization against previous Lanczos vectors v(i) with omega(i) > eta 00178 // 2 for reorthogonalization against previous Lanczos vectors in one group v(i),v(i+1),...,v(j) such that 00179 // i is the smallest index for omega(i) > eta, and j is the largest index for omega(j) > eta 00180 // 3 for reorthogonalization against all previous Lanczos vectors 00181 // omega(i) is the estimated orthogonal error of v(i) against the previous Lanczos vectors v(1),...,v(i-1) 00182 // by default, the value of eta is eps^(0.75) and the value of delta is eps^(0.5), 00183 // where eps is the machine epsilon 00184 // this parameter is effective only if reorth==1 (i.e. partial reorthogonalization) 00185 int partialReorthStrategy; 00187 00194 // set checkReorth=true for checking whether semi-orthogonality is preserved with partial reorthogonalization 00195 // this is for verification purposes, and is expensive, comparable to full reorthogonalization 00196 // therefore, checkReorth=true is not recommended for large problems 00197 // by default, checkReorth==false 00198 // this parameter is effective only with partial reorthogonalization (LanczosOptions::reorth==1 as input of LanczosEigenSolver()) 00199 bool checkReorth; 00201 // memoryExpansionFactor determines how much the memory should be expanded 00202 // when the allocated memory is not sufficient to store the Lanczos vectors 00203 // and the elements alpha's and beta's which form the symmetric tridiagonal matrix 00204 Real memoryExpansionFactor; 00205 00206 // cut points of eigenvalues 00208 00210 // eigenvalues larger than eigLowCut in the lower end will not be computed 00211 // this parameter is effective when eigPart is "SA" for smallest algebraic or when eigPart is "BE" for both ends 00212 Real eigLowCut; 00214 00216 // eigenvalues smaller than eigHighCut in the upper end will not be computed 00217 // this parameter is effective when eigPart is "LA" for largest algebraic or when eigPart is "BE" for both ends 00218 Real eigHighCut; 00220 00241 // a flag which indicates how to sort eigenvalues (default -1): 00242 // -2 for `don't sort' 00243 // -1 for the default setting (2 for decreasing if eigPart[] is "LA"; 0 for increasing if eigPart[] is "SA"; 0 for increasing if eigPart[] is "BE") 00244 // otherwise eigSort should be >= 0, in which case 00245 // the first digit is 0 (or 1) for to sort the eigenvalues in the lower end in increasing (or decreasing) order 00246 // the second digit is 0 (or 1) for to sort the eigenvalues in the higher end in increasing (or decreasing) order 00247 // the third digit is 0 (or 1) for to list the eigenvalues in the lower end before (or after) those in the higher end 00248 // Here eigPart[] is the input string of LanczosEigenSolver() 00249 // note: when eigPart is "LA", the first and the third digits do not matter 00250 // when eigPart is "SA", the second and the third digits do not matter 00251 // when eigPart is "BE", set eigSort=0 (or 7) for eigenvalues sorted in increasing (or decreasing) order 00252 int eigSort; 00253 00255 00261 // diagnostic information display level 0, 1 or 2 (default 1) 00262 // 0 means that no information will be displayed 00263 // 1 means that basic information will be displayed 00264 // 2 means that more information will be displayed 00265 int disp; 00267 // a pointer to an output stream for diagnostic information; by default, it points to std::cout 00268 std::ostream *out; 00270 // a pointer to an output stream for error messages; by default, it points to std::cerr 00271 std::ostream *err; 00272 00274 // a default constructor to set default options 00275 LanczosOptions() { 00276 // default options 00277 wantEigVec = true; 00278 minIter = 0; // 0 means that the default value will be set and used in LanczosEigenSolver 00279 maxIter = 0; // 0 means that the default value will be set and used in LanczosEigenSolver 00280 extraIter = 100; // extra number of iterations after convergence, to make sure no more eigenvalue to show up 00281 stride = 10; // check convergence every `stride' iterations; if stride==0, it will be set as 10 in the routine LanczosEigenSolver 00282 00283 defaultMinIterFactor = 5; 00284 defaultMaxIterFactor = 50; 00285 00286 // reorthogonalization parameters 00287 reorth = 1; // 1 for partial reorthogonalization; 2 for full reorthogonalization 00288 doubleReorthGamma = 1.0 / sqrt(2.0); 00289 localReorthGamma = 1.0 / sqrt(2.0); 00290 partialReorthStrategy = 2; 00291 checkReorth = false; 00292 00293 // memory expansion factor 00294 memoryExpansionFactor = 1.2; 00295 00296 // tolerance for convergence check 00297 tol = pow(Basics::machineEpsilon, 4.0/5.0); 00298 00299 // flag indicating how to sort the eigenvalues; see the description above 00300 eigSort = -1; 00301 00302 // diagnostic information display parameters 00303 disp = 1; 00304 #ifdef USE_MEX 00305 out = &mexcout; 00306 err = &mexcout; 00307 #else 00308 out = &std::cout; 00309 err = &std::cerr; 00310 #endif 00311 00312 // cut points of eigenvalues 00313 eigLowCut = Basics::infinity; // the default means no cut 00314 eigHighCut = -Basics::infinity; // the default means no cut 00315 } 00316 }; 00317 00319 // LanczosEigenSolver() returns an instance of this class which gives the information of the Lanczos procedure 00320 struct LanczosInfo { 00322 // number of iterations performed in the Lanczos procedure 00323 mkIndex numIter; 00325 // CPU time (in total) for obtaining the next Krylov vector in each iteration, e.g. matrix-vector products 00326 Real forNextKrylovVectorCpuTime; 00328 // CPU time (in total) for partial or full reorthogonalization 00329 Real reorthogonalizationCpuTime; 00331 // CPU time (in total) for convergence check 00332 Real convergenceCheckCpuTime; 00334 // CPU time for obtaining eigenvectors, after the eigenvalues are obtained 00335 Real forEigenVectorsCpuTime; 00337 00340 // CPU time for the whole Lanczos procedure, 00341 // including convergence check and getting eigenvectors (if requested); 00342 // this is the total CPU time for computing the eigenvalues / eigenvectors, 00343 // excluding reading the data matrix and reporting the result 00344 Real LanczosCpuTime; 00345 00347 00349 // maximum eigenvalue error 00350 // the computation requires the eigenvectors, so this parameter is computed only if LanczosOptions::wantEigVec==true as the input of LanczosEigenSolver() 00351 Real maxEigenvalueError; 00353 00355 // maximum relative eigenvalue error 00356 // the computation requires the eigenvectors, so this parameter is computed only if LanczosOptions::wantEigVec==true as the input of LanczosEigenSolver() 00357 Real maxRelativeEigenvalueError; 00358 00360 // memory (in bytes) required for the Lanczos iterations 00361 mkIndex memoryForLanczosInBytes; 00362 00364 00366 // allEigenvaluesCheckedConverged is true if all eigenvalues are checked converged; 00367 // otherwise, allEigenvaluesCheckedConverged is false, which means that the maximum number of Lanczos iterations is reached 00368 bool allEigenvaluesCheckedConverged; 00369 00370 // reorthogonalization cost information 00372 // number of iterations where reorthogonalization is performed 00373 mkIndex reorthIterCount; 00375 // total number of vectors against which the reorthogonalization is performed 00376 mkIndex reorthVectorCount; 00378 00384 // the value is reorthVectorCount divided by the number of vectors reorthogonalized by full reorthogonalization without double reorthogonalization 00385 // if no reorthogonalization (i.e. LanczosOptions::reorth==0 as input of LanczosEigenSolver()) is performed, then reorthVectorRate = 0.0 00386 // if full reorthogonalization (i.e. LanczosOptions::reorth==2 as input of LanczosEigenSolver()) is performed without double reorthogonalization is performed, then reorthVectorRate = 1.0 00387 // if partial reorthogonalization (i.e. LanczosOptions::reorth==1 as input of LanczosEigenSolver()) is performed, usually 0.0 < reorthVectorRate < 1.0 00388 Real reorthVectorRate; 00390 // number of iterations where reorthogonalization is doubled 00391 mkIndex localReorthIterCount; 00393 // number of iterations where local reorthogonalization is performed 00394 mkIndex doubleReorthIterCount; 00395 00397 00403 // maximum ratio of orthogonality level 00404 Real maxOrthLevelRatio; 00406 00412 // minimum ratio of orthogonality level 00413 Real minOrthLevelRatio; 00414 // the above two parameters are computed only if the partial reorthogonalization is applied (i.e. LanczosOptions::reorth==1 as input of LanczosEigenSolver()) 00415 // let w0(i,j) be the inner product of the i-th and j-th Lanczos vectors and w0(j) = max{w0(i,j)|i<j} 00416 // also let w(j) be the estimated upper bound of w0(j) 00417 // then minOrthLevelRatio = min{ w0(j)/w(j) | 1<=j<=numIter }, and 00418 // maxOrthLevelRatio = max{ w0(j)/w(j) | 1<=j<=numIter } 00419 }; 00420 00423 00429 // the definition of a function /operator with input a vector and output a vector 00430 // the operator should be self-adjoint in the Lanczos methods 00431 // in a standard Lanczos or conjugate gradient procedure, the input is v and output is A*v; 00432 // in a filtered Lanczos or conjugate gradient procedure, the input is v and output is p(A)*v, with p(z) a polynomial, 00433 // where A is a symmetric matrix; it can be dense or sparse 00434 typedef Vector (*NEXT_VECTOR)(const Vector&); 00435 00437 00452 // Lanczos eigensolver, the most general form 00453 // input: 00454 // NextKrylovVector is the function which defines a self-adjoint operator, e.g. A*v with A a symmetric matrix 00455 // n is the length of the input/output vectors of NextKrylovVector() 00456 // neigWanted is the number of eigenvalues sought 00457 // eigPart is a string "SA", "LA", or "BE" 00458 // "LA" - largest algebraic, for largest eigenvalues 00459 // "SA" - smallest algebraic, for smallest eigenvalues 00460 // "BE" - both ends, one more from high end if nev is odd 00461 // opts is a collection of Lanczos options 00462 // output: 00463 // eigVal is a vector of length neigWanted containing the computed eigenvalues 00464 // eigVec is a matrix whose columns are eigenvectors, if opts.wantEigVec==true 00465 // return: 00466 // this routine returns an instance of LanczosInfo which gives the information of the Lanczos procedure 00467 LanczosInfo LanczosEigenSolver(Vector &eigVal, Matrix &eigVec, NEXT_VECTOR NextKrylovVector, mkIndex n, mkIndex neigWanted, const char eigPart[], LanczosOptions &opts); 00469 // the same as LanczosEigenSolver(), but with the default LanczosOptions 00470 LanczosInfo LanczosEigenSolver(Vector &eigVal, Matrix &eigVec, NEXT_VECTOR NextKrylovVector, mkIndex n, mkIndex neigWanted, const char eigPart[]); 00472 // the same as LanczosEigenSolver(), but with the NextKrylovVector() defined as A*v 00473 LanczosInfo LanczosEigenSolver(Vector &eigVal, Matrix &eigVec, const SymmetricMatrix &A, mkIndex neigWanted, const char eigPart[], LanczosOptions &opts); 00475 // the same as LanczosEigenSolver(), but with the NextKrylovVector() defined as A*v and with the default LanczosOptions 00476 LanczosInfo LanczosEigenSolver(Vector &eigVal, Matrix &eigVec, const SymmetricMatrix &A, mkIndex neigWanted, const char eigPart[]); 00478 // the same as LanczosEigenSolver(), but with the NextKrylovVector() defined as A*v 00479 LanczosInfo LanczosEigenSolver(Vector &eigVal, Matrix &eigVec, const SparseMatrix &A, mkIndex neigWanted, const char eigPart[], LanczosOptions &opts); 00481 // the same as LanczosEigenSolver(), but with the NextKrylovVector() defined as A*v and with the default LanczosOptions 00482 LanczosInfo LanczosEigenSolver(Vector &eigVal, Matrix &eigVec, const SparseMatrix &A, mkIndex neigWanted, const char eigPart[]); 00483 // end of group_laneig 00485 00486 00487 #endif // end of #ifdef LANEIG_H
1.7.3