7 #ifndef SPECTRA_SPARSE_GEN_COMPLEX_SHIFT_SOLVE_H
8 #define SPECTRA_SPARSE_GEN_COMPLEX_SHIFT_SOLVE_H
11 #include <Eigen/SparseCore>
12 #include <Eigen/SparseLU>
31 template <
typename Scalar_,
int Flags = Eigen::ColMajor,
typename StorageIndex =
int>
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>;
48 using Complex = std::complex<Scalar>;
49 using ComplexVector = Eigen::Matrix<Complex, Eigen::Dynamic, 1>;
50 using SparseComplexMatrix = Eigen::SparseMatrix<Complex, Flags, StorageIndex>;
52 using ComplexSolver = Eigen::SparseLU<SparseComplexMatrix>;
54 ConstGenericSparseMatrix m_mat;
56 ComplexSolver m_solver;
57 mutable ComplexVector m_x_cache;
67 template <
typename Derived>
69 m_mat(mat), m_n(mat.
rows())
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)");
75 if (mat.rows() != mat.cols())
76 throw std::invalid_argument(
"SparseGenComplexShiftSolve: matrix must be square");
82 Index
rows()
const {
return m_n; }
86 Index
cols()
const {
return m_n; }
97 SparseComplexMatrix I(m_n, m_n);
100 m_solver.compute(m_mat.template cast<Complex>() - Complex(sigmar, sigmai) * I);
102 m_x_cache.resize(m_n);
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();
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