SparseGenComplexShiftSolve.h
Go to the documentation of this file.
1 // Copyright (C) 2020-2025 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:
47 
48  using Complex = std::complex<Scalar>;
51 
53 
55  const Index m_n;
58 
59 public:
67  template <typename Derived>
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)
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
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
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Spectra::SparseGenComplexShiftSolve::SparseGenComplexShiftSolve
SparseGenComplexShiftSolve(const Eigen::SparseMatrixBase< Derived > &mat)
Definition: SparseGenComplexShiftSolve.h:68
Spectra::SparseGenComplexShiftSolve::Scalar
Scalar_ Scalar
Definition: SparseGenComplexShiftSolve.h:38
Spectra::SparseGenComplexShiftSolve::Complex
std::complex< Scalar > Complex
Definition: SparseGenComplexShiftSolve.h:48
Eigen::SparseLU::compute
void compute(const MatrixType &matrix)
Definition: SparseLU.h:182
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
Eigen::PlainObjectBase::resize
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:271
Spectra::SparseGenComplexShiftSolve::m_x_cache
ComplexVector m_x_cache
Definition: SparseGenComplexShiftSolve.h:57
Spectra::SparseGenComplexShiftSolve::rows
Index rows() const
Definition: SparseGenComplexShiftSolve.h:82
I
#define I
Definition: main.h:112
Spectra::SparseGenComplexShiftSolve::m_n
const Index m_n
Definition: SparseGenComplexShiftSolve.h:55
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
Spectra::SparseGenComplexShiftSolve::set_shift
void set_shift(const Scalar &sigmar, const Scalar &sigmai)
Definition: SparseGenComplexShiftSolve.h:94
y
Scalar * y
Definition: level1_cplx_impl.h:124
Spectra::SparseGenComplexShiftSolve::m_solver
ComplexSolver m_solver
Definition: SparseGenComplexShiftSolve.h:56
Spectra::SparseGenComplexShiftSolve::cols
Index cols() const
Definition: SparseGenComplexShiftSolve.h:86
Eigen::Ref< const SparseMatrix >
Spectra::SparseGenComplexShiftSolve::perform_op
void perform_op(const Scalar *x_in, Scalar *y_out) const
Definition: SparseGenComplexShiftSolve.h:114
Spectra
Definition: LOBPCGSolver.h:19
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:301
Spectra::SparseGenComplexShiftSolve::MapConstVec
Eigen::Map< const Vector > MapConstVec
Definition: SparseGenComplexShiftSolve.h:43
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 >
Spectra::SparseGenComplexShiftSolve::Index
Eigen::Index Index
Definition: SparseGenComplexShiftSolve.h:41
Eigen::SparseLU< SparseComplexMatrix >
Eigen::PlainObjectBase::setZero
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:562
Spectra::SparseGenComplexShiftSolve::m_mat
ConstGenericSparseMatrix m_mat
Definition: SparseGenComplexShiftSolve.h:54
Eigen::SparseCompressedBase< SparseMatrix< _Scalar, _Options, _StorageIndex > >::IsRowMajor
@ IsRowMajor
Definition: SparseMatrixBase.h:100
Spectra::SparseGenComplexShiftSolve
Definition: SparseGenComplexShiftSolve.h:32
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Thu Apr 10 2025 03:03:35