7 #ifndef SPECTRA_SPARSE_SYM_SHIFT_SOLVE_H
8 #define SPECTRA_SPARSE_SYM_SHIFT_SOLVE_H
11 #include <Eigen/SparseCore>
12 #include <Eigen/SparseLU>
32 template <
typename Scalar_,
int Uplo = Eigen::Lower,
int Flags = Eigen::ColMajor,
typename StorageIndex =
int>
42 using Index = Eigen::Index;
43 using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
44 using MapConstVec = Eigen::Map<const Vector>;
45 using MapVec = Eigen::Map<Vector>;
46 using SparseMatrix = Eigen::SparseMatrix<Scalar, Flags, StorageIndex>;
47 using ConstGenericSparseMatrix =
const Eigen::Ref<const SparseMatrix>;
49 ConstGenericSparseMatrix m_mat;
51 Eigen::SparseLU<SparseMatrix> m_solver;
61 template <
typename Derived>
63 m_mat(mat), m_n(mat.
rows())
66 static_cast<int>(Derived::PlainObject::IsRowMajor) ==
static_cast<int>(SparseMatrix::IsRowMajor),
67 "SparseSymShiftSolve: the \"Flags\" template parameter does not match the input matrix (Eigen::ColMajor/Eigen::RowMajor)");
69 if (mat.rows() != mat.cols())
70 throw std::invalid_argument(
"SparseSymShiftSolve: matrix must be square");
76 Index
rows()
const {
return m_n; }
80 Index
cols()
const {
return m_n; }
87 SparseMatrix mat = m_mat.template selfadjointView<Uplo>();
88 SparseMatrix identity(m_n, m_n);
89 identity.setIdentity();
90 mat = mat - sigma * identity;
91 m_solver.isSymmetric(
true);
92 m_solver.compute(mat);
93 if (m_solver.info() != Eigen::Success)
94 throw std::invalid_argument(
"SparseSymShiftSolve: factorization failed with the given shift");
106 MapConstVec x(x_in, m_n);
107 MapVec y(y_out, m_n);
108 y.noalias() = m_solver.solve(x);
SparseSymShiftSolve(const Eigen::SparseMatrixBase< Derived > &mat)
void perform_op(const Scalar *x_in, Scalar *y_out) const
void set_shift(const Scalar &sigma)