Spectra  1.0.1
Header-only C++ Library for Large Scale Eigenvalue Problems
SparseGenComplexShiftSolve.h
1 // Copyright (C) 2020-2022 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 SPECTRA_SPARSE_GEN_COMPLEX_SHIFT_SOLVE_H
8 #define SPECTRA_SPARSE_GEN_COMPLEX_SHIFT_SOLVE_H
9 
10 #include <Eigen/Core>
11 #include <Eigen/SparseCore>
12 #include <Eigen/SparseLU>
13 #include <stdexcept>
14 
15 namespace Spectra {
16 
31 template <typename Scalar_, int Flags = Eigen::ColMajor, typename StorageIndex = int>
33 {
34 public:
38  using Scalar = Scalar_;
39 
40 private:
41  using Index = Eigen::Index;
42  using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
43  using MapConstVec = Eigen::Map<const Vector>;
44  using MapVec = Eigen::Map<Vector>;
45  using SparseMatrix = Eigen::SparseMatrix<Scalar, Flags, StorageIndex>;
46  using ConstGenericSparseMatrix = const Eigen::Ref<const SparseMatrix>;
47 
48  using Complex = std::complex<Scalar>;
49  using ComplexVector = Eigen::Matrix<Complex, Eigen::Dynamic, 1>;
50  using SparseComplexMatrix = Eigen::SparseMatrix<Complex, Flags, StorageIndex>;
51 
52  using ComplexSolver = Eigen::SparseLU<SparseComplexMatrix>;
53 
54  ConstGenericSparseMatrix m_mat;
55  const Index m_n;
56  ComplexSolver m_solver;
57  mutable ComplexVector m_x_cache;
58 
59 public:
67  template <typename Derived>
68  SparseGenComplexShiftSolve(const Eigen::SparseMatrixBase<Derived>& mat) :
69  m_mat(mat), m_n(mat.rows())
70  {
71  static_assert(
72  static_cast<int>(Derived::PlainObject::IsRowMajor) == static_cast<int>(SparseMatrix::IsRowMajor),
73  "SparseGenComplexShiftSolve: the \"Flags\" template parameter does not match the input matrix (Eigen::ColMajor/Eigen::RowMajor)");
74 
75  if (mat.rows() != mat.cols())
76  throw std::invalid_argument("SparseGenComplexShiftSolve: matrix must be square");
77  }
78 
82  Index rows() const { return m_n; }
86  Index cols() const { return m_n; }
87 
94  void set_shift(const Scalar& sigmar, const Scalar& sigmai)
95  {
96  // Create a sparse idendity matrix (1 + 0i on diagonal)
97  SparseComplexMatrix I(m_n, m_n);
98  I.setIdentity();
99  // Sparse LU decomposition
100  m_solver.compute(m_mat.template cast<Complex>() - Complex(sigmar, sigmai) * I);
101  // Set cache to zero
102  m_x_cache.resize(m_n);
103  m_x_cache.setZero();
104  }
105 
113  // y_out = Re( inv(A - sigma * I) * x_in )
114  void perform_op(const Scalar* x_in, Scalar* y_out) const
115  {
116  m_x_cache.real() = MapConstVec(x_in, m_n);
117  MapVec y(y_out, m_n);
118  y.noalias() = m_solver.solve(m_x_cache).real();
119  }
120 };
121 
122 } // namespace Spectra
123 
124 #endif // SPECTRA_SPARSE_GEN_COMPLEX_SHIFT_SOLVE_H
SparseGenComplexShiftSolve(const Eigen::SparseMatrixBase< Derived > &mat)
void set_shift(const Scalar &sigmar, const Scalar &sigmai)
void perform_op(const Scalar *x_in, Scalar *y_out) const