ARPACK-Eigen
DenseSymShiftSolve.h
1 #ifndef DENSE_SYM_SHIFT_SOLVE_H
2 #define DENSE_SYM_SHIFT_SOLVE_H
3 
4 #include <Eigen/Core>
5 #include <Eigen/Cholesky>
6 #include <stdexcept>
7 
15 template <typename Scalar>
17 {
18 private:
19  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix;
20  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
21  typedef Eigen::Map<const Matrix> MapMat;
22  typedef Eigen::Map< Eigen::Matrix<Scalar, Eigen::Dynamic, 1> > MapVec;
23 
24  typedef const Eigen::Ref<const Matrix> ConstGenericMatrix;
25 
26  const MapMat mat;
27  const int dim_n;
28  Eigen::LDLT<Matrix> solver;
29 
30 public:
39  DenseSymShiftSolve(ConstGenericMatrix &mat_) :
40  mat(mat_.data(), mat_.rows(), mat_.cols()),
41  dim_n(mat_.rows())
42  {
43  if(mat_.rows() != mat_.cols())
44  throw std::invalid_argument("DenseSymShiftSolve: matrix must be square");
45  }
46 
50  int rows() { return dim_n; }
54  int cols() { return dim_n; }
55 
59  void set_shift(Scalar sigma)
60  {
61  solver.compute(mat - sigma * Matrix::Identity(dim_n, dim_n));
62  }
63 
70  // y_out = inv(A - sigma * I) * x_in
71  void perform_op(Scalar *x_in, Scalar *y_out)
72  {
73  MapVec x(x_in, dim_n);
74  MapVec y(y_out, dim_n);
75  y.noalias() = solver.solve(x);
76  }
77 };
78 
79 
80 #endif // DENSESYMSHIFTSOLVE_H
void set_shift(Scalar sigma)
DenseSymShiftSolve(ConstGenericMatrix &mat_)
void perform_op(Scalar *x_in, Scalar *y_out)