Spectra
DenseSymShiftSolve.h
1 // Copyright (C) 2016-2018 Yixuan Qiu <yixuan.qiu@cos.name>
2 //
3 // This Source Code Form is subject to the terms of the Mozilla
4 // Public License v. 2.0. If a copy of the MPL was not distributed
5 // with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
6 
7 #ifndef DENSE_SYM_SHIFT_SOLVE_H
8 #define DENSE_SYM_SHIFT_SOLVE_H
9 
10 #include <Eigen/Core>
11 #include <Eigen/Cholesky>
12 #include <stdexcept>
13 
14 namespace Spectra {
15 
16 
24 template <typename Scalar, int Uplo = Eigen::Lower>
26 {
27 private:
28  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix;
29  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
30  typedef Eigen::Map<const Vector> MapConstVec;
31  typedef Eigen::Map<Vector> MapVec;
32  typedef const Eigen::Ref<const Matrix> ConstGenericMatrix;
33 
34  ConstGenericMatrix m_mat;
35  const int m_n;
36  Eigen::LDLT<Matrix, Uplo> m_solver;
37 
38 public:
47  DenseSymShiftSolve(ConstGenericMatrix& mat) :
48  m_mat(mat), m_n(mat.rows())
49  {
50  if(mat.rows() != mat.cols())
51  throw std::invalid_argument("DenseSymShiftSolve: matrix must be square");
52  }
53 
57  int rows() const { return m_n; }
61  int cols() const { return m_n; }
62 
66  void set_shift(Scalar sigma)
67  {
68  m_solver.compute(m_mat - sigma * Matrix::Identity(m_n, m_n));
69  }
70 
77  // y_out = inv(A - sigma * I) * x_in
78  void perform_op(const Scalar* x_in, Scalar* y_out) const
79  {
80  MapConstVec x(x_in, m_n);
81  MapVec y(y_out, m_n);
82  y.noalias() = m_solver.solve(x);
83  }
84 };
85 
86 
87 } // namespace Spectra
88 
89 #endif // DENSE_SYM_SHIFT_SOLVE_H
void perform_op(const Scalar *x_in, Scalar *y_out) const
DenseSymShiftSolve(ConstGenericMatrix &mat)