SparseGenRealShiftSolve.h
Go to the documentation of this file.
1 // Copyright (C) 2016-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_REAL_SHIFT_SOLVE_H
8 #define SPECTRA_SPARSE_GEN_REAL_SHIFT_SOLVE_H
9 
10 #include <Eigen/Core>
11 #include <Eigen/SparseCore>
12 #include <Eigen/SparseLU>
13 #include <stdexcept>
14 
15 namespace Spectra {
16 
30 template <typename Scalar_, int Flags = Eigen::ColMajor, typename StorageIndex = int>
32 {
33 public:
37  using Scalar = Scalar_;
38 
39 private:
46 
48  const Index m_n;
50 
51 public:
59  template <typename Derived>
61  m_mat(mat), m_n(mat.rows())
62  {
63  static_assert(
64  static_cast<int>(Derived::PlainObject::IsRowMajor) == static_cast<int>(SparseMatrix::IsRowMajor),
65  "SparseGenRealShiftSolve: the \"Flags\" template parameter does not match the input matrix (Eigen::ColMajor/Eigen::RowMajor)");
66 
67  if (mat.rows() != mat.cols())
68  throw std::invalid_argument("SparseGenRealShiftSolve: matrix must be square");
69  }
70 
74  Index rows() const { return m_n; }
78  Index cols() const { return m_n; }
79 
83  void set_shift(const Scalar& sigma)
84  {
86  I.setIdentity();
87 
88  m_solver.compute(m_mat - sigma * I);
89  if (m_solver.info() != Eigen::Success)
90  throw std::invalid_argument("SparseGenRealShiftSolve: factorization failed with the given shift");
91  }
92 
99  // y_out = inv(A - sigma * I) * x_in
100  void perform_op(const Scalar* x_in, Scalar* y_out) const
101  {
102  MapConstVec x(x_in, m_n);
103  MapVec y(y_out, m_n);
104  y.noalias() = m_solver.solve(x);
105  }
106 };
107 
108 } // namespace Spectra
109 
110 #endif // SPECTRA_SPARSE_GEN_REAL_SHIFT_SOLVE_H
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Spectra::SparseGenRealShiftSolve::m_solver
Eigen::SparseLU< SparseMatrix > m_solver
Definition: SparseGenRealShiftSolve.h:49
Spectra::SparseGenRealShiftSolve::m_n
const Index m_n
Definition: SparseGenRealShiftSolve.h:48
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
Eigen::Success
@ Success
Definition: Constants.h:442
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
Spectra::SparseGenRealShiftSolve::perform_op
void perform_op(const Scalar *x_in, Scalar *y_out) const
Definition: SparseGenRealShiftSolve.h:100
Spectra::SparseGenRealShiftSolve
Definition: SparseGenRealShiftSolve.h:31
sampling::sigma
static const double sigma
Definition: testGaussianBayesNet.cpp:170
Spectra::SparseGenRealShiftSolve::Index
Eigen::Index Index
Definition: SparseGenRealShiftSolve.h:40
Spectra::SparseGenRealShiftSolve::SparseGenRealShiftSolve
SparseGenRealShiftSolve(const Eigen::SparseMatrixBase< Derived > &mat)
Definition: SparseGenRealShiftSolve.h:60
I
#define I
Definition: main.h:112
Spectra::SparseGenRealShiftSolve::m_mat
ConstGenericSparseMatrix m_mat
Definition: SparseGenRealShiftSolve.h:47
Spectra::SparseGenRealShiftSolve::cols
Index cols() const
Definition: SparseGenRealShiftSolve.h:78
Spectra::SparseGenRealShiftSolve::rows
Index rows() const
Definition: SparseGenRealShiftSolve.h:74
Eigen::Map
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:94
y
Scalar * y
Definition: level1_cplx_impl.h:124
Eigen::Ref< const SparseMatrix >
Spectra
Definition: LOBPCGSolver.h:19
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:301
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 >
Eigen::SparseLU
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:17
Eigen::SparseCompressedBase< SparseMatrix< _Scalar, _Options, _StorageIndex > >::IsRowMajor
@ IsRowMajor
Definition: SparseMatrixBase.h:100
Spectra::SparseGenRealShiftSolve::Scalar
Scalar_ Scalar
Definition: SparseGenRealShiftSolve.h:37
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Spectra::SparseGenRealShiftSolve::set_shift
void set_shift(const Scalar &sigma)
Definition: SparseGenRealShiftSolve.h:83


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