eigs.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "eigs.h"
  9. #include "cotmatrix.h"
  10. #include "sort.h"
  11. #include "slice.h"
  12. #include "massmatrix.h"
  13. #include <iostream>
  14. template <
  15. typename Atype,
  16. typename Btype,
  17. typename DerivedU,
  18. typename DerivedS>
  19. IGL_INLINE bool igl::eigs(
  20. const Eigen::SparseMatrix<Atype> & A,
  21. const Eigen::SparseMatrix<Btype> & iB,
  22. const size_t k,
  23. const EigsType type,
  24. Eigen::PlainObjectBase<DerivedU> & sU,
  25. Eigen::PlainObjectBase<DerivedS> & sS)
  26. {
  27. using namespace Eigen;
  28. using namespace std;
  29. const size_t n = A.rows();
  30. assert(A.cols() == n && "A should be square.");
  31. assert(iB.rows() == n && "B should be match A's dims.");
  32. assert(iB.cols() == n && "B should be square.");
  33. assert(type == EIGS_TYPE_SM && "Only low frequencies are supported");
  34. DerivedU U(n,k);
  35. DerivedS S(k,1);
  36. typedef Atype Scalar;
  37. typedef Eigen::Matrix<typename DerivedU::Scalar,DerivedU::RowsAtCompileTime,1> VectorXS;
  38. // Rescale B for better numerics
  39. const Scalar rescale = std::abs(iB.diagonal().maxCoeff());
  40. const Eigen::SparseMatrix<Btype> B = iB/rescale;
  41. Scalar tol = 1e-4;
  42. Scalar conv = 1e-14;
  43. int max_iter = 100;
  44. int i = 0;
  45. while(true)
  46. {
  47. // Random initial guess
  48. VectorXS y = VectorXS::Random(n,1);
  49. Scalar eff_sigma = 0;
  50. if(i>0)
  51. {
  52. eff_sigma = 1e-8+std::abs(S(i-1));
  53. }
  54. // whether to use rayleigh quotient method
  55. bool ray = false;
  56. Scalar err = std::numeric_limits<Scalar>::infinity();
  57. int iter;
  58. Scalar sigma = std::numeric_limits<Scalar>::infinity();
  59. VectorXS x;
  60. for(iter = 0;iter<max_iter;iter++)
  61. {
  62. if(i>0 && !ray)
  63. {
  64. // project-out existing modes
  65. for(int j = 0;j<i;j++)
  66. {
  67. const VectorXS u = U.col(j);
  68. y = (y - u*u.dot(B*y)/u.dot(B * u)).eval();
  69. }
  70. }
  71. // normalize
  72. x = y/sqrt(y.dot(B*y));
  73. // current guess at eigen value
  74. sigma = x.dot(A*x)/x.dot(B*x);
  75. //x *= sigma>0?1.:-1.;
  76. Scalar err_prev = err;
  77. err = (A*x-sigma*B*x).array().abs().maxCoeff();
  78. if(err<conv)
  79. {
  80. break;
  81. }
  82. if(ray || err<tol)
  83. {
  84. eff_sigma = sigma;
  85. ray = true;
  86. }
  87. Scalar tikhonov = std::abs(eff_sigma)<1e-12?1e-10:0;
  88. switch(type)
  89. {
  90. default:
  91. assert(false && "Not supported");
  92. break;
  93. case EIGS_TYPE_SM:
  94. {
  95. SimplicialLDLT<SparseMatrix<Scalar> > solver;
  96. const SparseMatrix<Scalar> C = A-eff_sigma*B+tikhonov*B;
  97. //mw.save(C,"C");
  98. //mw.save(eff_sigma,"eff_sigma");
  99. //mw.save(tikhonov,"tikhonov");
  100. solver.compute(C);
  101. switch(solver.info())
  102. {
  103. case Eigen::Success:
  104. break;
  105. case Eigen::NumericalIssue:
  106. cerr<<"Error: Numerical issue."<<endl;
  107. return false;
  108. default:
  109. cerr<<"Error: Other."<<endl;
  110. return false;
  111. }
  112. const VectorXS rhs = B*x;
  113. y = solver.solve(rhs);
  114. //mw.save(rhs,"rhs");
  115. //mw.save(y,"y");
  116. //mw.save(x,"x");
  117. //mw.write("eigs.mat");
  118. //if(i == 1)
  119. //return false;
  120. break;
  121. }
  122. }
  123. }
  124. if(iter == max_iter)
  125. {
  126. cerr<<"Failed to converge."<<endl;
  127. return false;
  128. }
  129. if(i==0 || (S.head(i).array()-sigma).abs().maxCoeff()>1e-14)
  130. {
  131. U.col(i) = x;
  132. S(i) = sigma;
  133. i++;
  134. if(i == k)
  135. {
  136. break;
  137. }
  138. }else
  139. {
  140. // restart with new random guess.
  141. cout<<"RESTART!"<<endl;
  142. }
  143. }
  144. // finally sort
  145. VectorXi I;
  146. igl::sort(S,1,false,sS,I);
  147. sU = igl::slice(U,I,2);
  148. sS /= rescale;
  149. sU /= sqrt(rescale);
  150. return true;
  151. }
  152. #ifdef IGL_STATIC_LIBRARY
  153. // Explicit template specialization
  154. template bool igl::eigs<double, double, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int> const&, unsigned long, igl::EigsType, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  155. #ifdef WIN32
  156. template bool igl::eigs<double, double, Eigen::Matrix<double,-1,-1,0,-1,-1>, Eigen::Matrix<double,-1,1,0,-1,1> >(Eigen::SparseMatrix<double,0,int> const &,Eigen::SparseMatrix<double,0,int> const &,unsigned long long, igl::EigsType, Eigen::PlainObjectBase< Eigen::Matrix<double,-1,-1,0,-1,-1> > &, Eigen::PlainObjectBase<Eigen::Matrix<double,-1,1,0,-1,1> > &);
  157. #endif
  158. #endif