eigs.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Alec Jacobson <[email protected]>
  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 "massmatrix.h"
  12. #include <iostream>
  13. template <
  14. typename Atype,
  15. typename Btype,
  16. typename DerivedU,
  17. typename DerivedS>
  18. IGL_INLINE bool igl::eigs(
  19. const Eigen::SparseMatrix<Atype> & A,
  20. const Eigen::SparseMatrix<Btype> & iB,
  21. const size_t k,
  22. const EigsType type,
  23. Eigen::PlainObjectBase<DerivedU> & sU,
  24. Eigen::PlainObjectBase<DerivedS> & sS)
  25. {
  26. using namespace Eigen;
  27. using namespace std;
  28. const size_t n = A.rows();
  29. assert(A.cols() == n && "A should be square.");
  30. assert(iB.rows() == n && "B should be match A's dims.");
  31. assert(iB.cols() == n && "B should be square.");
  32. assert(type == EIGS_TYPE_SM && "Only low frequencies are supported");
  33. DerivedU U(n,k);
  34. DerivedS S(k,1);
  35. typedef Atype Scalar;
  36. typedef Eigen::Matrix<typename DerivedU::Scalar,DerivedU::RowsAtCompileTime,1> VectorXS;
  37. // Rescale B for better numerics
  38. const Scalar rescale = std::abs(iB.diagonal().maxCoeff());
  39. const Eigen::SparseMatrix<Btype> B = iB/rescale;
  40. Scalar tol = 1e-4;
  41. Scalar conv = 1e-14;
  42. int max_iter = 100;
  43. int i = 0;
  44. //std::cout<<"start"<<std::endl;
  45. while(true)
  46. {
  47. //std::cout<<i<<std::endl;
  48. // Random initial guess
  49. VectorXS y = VectorXS::Random(n,1);
  50. Scalar eff_sigma = 0;
  51. if(i>0)
  52. {
  53. eff_sigma = 1e-8+std::abs(S(i-1));
  54. }
  55. // whether to use rayleigh quotient method
  56. bool ray = false;
  57. Scalar err = std::numeric_limits<Scalar>::infinity();
  58. int iter;
  59. Scalar sigma = std::numeric_limits<Scalar>::infinity();
  60. VectorXS x;
  61. for(iter = 0;iter<max_iter;iter++)
  62. {
  63. if(i>0 && !ray)
  64. {
  65. // project-out existing modes
  66. for(int j = 0;j<i;j++)
  67. {
  68. const VectorXS u = U.col(j);
  69. y = (y - u*u.dot(B*y)/u.dot(B * u)).eval();
  70. }
  71. }
  72. // normalize
  73. x = y/sqrt(y.dot(B*y));
  74. // current guess at eigen value
  75. sigma = x.dot(A*x)/x.dot(B*x);
  76. //x *= sigma>0?1.:-1.;
  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. #ifdef IGL_EIGS_DEBUG
  107. cerr<<"Error: Numerical issue."<<endl;
  108. #endif
  109. return false;
  110. default:
  111. #ifdef IGL_EIGS_DEBUG
  112. cerr<<"Error: Other."<<endl;
  113. #endif
  114. return false;
  115. }
  116. const VectorXS rhs = B*x;
  117. y = solver.solve(rhs);
  118. //mw.save(rhs,"rhs");
  119. //mw.save(y,"y");
  120. //mw.save(x,"x");
  121. //mw.write("eigs.mat");
  122. //if(i == 1)
  123. //return false;
  124. break;
  125. }
  126. }
  127. }
  128. if(iter == max_iter)
  129. {
  130. cerr<<"Failed to converge."<<endl;
  131. return false;
  132. }
  133. if(
  134. i==0 ||
  135. (S.head(i).array()-sigma).abs().maxCoeff()>1e-14 ||
  136. ((U.leftCols(i).transpose()*B*x).array().abs()<=1e-7).all()
  137. )
  138. {
  139. //cout<<"Found "<<i<<"th mode"<<endl;
  140. U.col(i) = x;
  141. S(i) = sigma;
  142. i++;
  143. if(i == k)
  144. {
  145. break;
  146. }
  147. }else
  148. {
  149. //std::cout<<"i: "<<i<<std::endl;
  150. //std::cout<<" "<<S.head(i).transpose()<<" << "<<sigma<<std::endl;
  151. //std::cout<<" "<<(S.head(i).array()-sigma).abs().maxCoeff()<<std::endl;
  152. //std::cout<<" "<<(U.leftCols(i).transpose()*B*x).array().abs().transpose()<<std::endl;
  153. // restart with new random guess.
  154. cout<<"igl::eigs RESTART"<<endl;
  155. }
  156. }
  157. // finally sort
  158. VectorXi I;
  159. igl::sort(S,1,false,sS,I);
  160. sU = U(Eigen::placeholders::all,I);
  161. sS /= rescale;
  162. sU /= sqrt(rescale);
  163. return true;
  164. }
  165. #ifdef IGL_STATIC_LIBRARY
  166. // Explicit template instantiation
  167. 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&, const size_t, igl::EigsType, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  168. #endif