SparseSymShiftSolve.h
Go to the documentation of this file.
1 // Copyright (C) 2016-2019 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 SPARSE_SYM_SHIFT_SOLVE_H
8 #define SPARSE_SYM_SHIFT_SOLVE_H
9 
10 #include <Eigen/Core>
11 #include <Eigen/SparseCore>
12 #include <Eigen/SparseLU>
13 #include <stdexcept>
14 
15 namespace Spectra {
16 
24 template <typename Scalar, int Uplo = Eigen::Lower, int Flags = 0, typename StorageIndex = int>
26 {
27 private:
34 
36  const int m_n;
38 
39 public:
48  m_mat(mat), m_n(mat.rows())
49  {
50  if (mat.rows() != mat.cols())
51  throw std::invalid_argument("SparseSymShiftSolve: matrix must be square");
52  }
53 
57  Index rows() const { return m_n; }
61  Index cols() const { return m_n; }
62 
67  {
68  SparseMatrix mat = m_mat.template selfadjointView<Uplo>();
69  SparseMatrix identity(m_n, m_n);
70  identity.setIdentity();
71  mat = mat - sigma * identity;
72  m_solver.isSymmetric(true);
73  m_solver.compute(mat);
74  if (m_solver.info() != Eigen::Success)
75  throw std::invalid_argument("SparseSymShiftSolve: factorization failed with the given shift");
76  }
77 
84  // y_out = inv(A - sigma * I) * x_in
85  void perform_op(const Scalar* x_in, Scalar* y_out) const
86  {
87  MapConstVec x(x_in, m_n);
88  MapVec y(y_out, m_n);
89  y.noalias() = m_solver.solve(x);
90  }
91 };
92 
93 } // namespace Spectra
94 
95 #endif // SPARSE_SYM_SHIFT_SOLVE_H
Spectra::SparseSymShiftSolve::cols
Index cols() const
Definition: SparseSymShiftSolve.h:61
Spectra::SparseSymShiftSolve::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Definition: SparseSymShiftSolve.h:29
Spectra::SparseSymShiftSolve::set_shift
void set_shift(Scalar sigma)
Definition: SparseSymShiftSolve.h:66
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Spectra::SparseSymShiftSolve::perform_op
void perform_op(const Scalar *x_in, Scalar *y_out) const
Definition: SparseSymShiftSolve.h:85
Spectra::SparseSymShiftSolve::MapVec
Eigen::Map< Vector > MapVec
Definition: SparseSymShiftSolve.h:31
Spectra::SparseSymShiftSolve::MapConstVec
Eigen::Map< const Vector > MapConstVec
Definition: SparseSymShiftSolve.h:30
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
Spectra::SparseSymShiftSolve::m_n
const int m_n
Definition: SparseSymShiftSolve.h:36
Spectra::SparseSymShiftSolve::m_solver
Eigen::SparseLU< SparseMatrix > m_solver
Definition: SparseSymShiftSolve.h:37
Eigen::Success
@ Success
Definition: Constants.h:442
Spectra::SparseSymShiftSolve::Index
Eigen::Index Index
Definition: SparseSymShiftSolve.h:28
Spectra::SparseSymShiftSolve::SparseMatrix
Eigen::SparseMatrix< Scalar, Flags, StorageIndex > SparseMatrix
Definition: SparseSymShiftSolve.h:32
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
sampling::sigma
static const double sigma
Definition: testGaussianBayesNet.cpp:170
Spectra::SparseSymShiftSolve::rows
Index rows() const
Definition: SparseSymShiftSolve.h:57
Eigen::SparseMatrix::setIdentity
void setIdentity()
Definition: SparseMatrix.h:749
Spectra::SparseSymShiftSolve::SparseSymShiftSolve
SparseSymShiftSolve(ConstGenericSparseMatrix &mat)
Definition: SparseSymShiftSolve.h:47
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
Spectra::SparseSymShiftSolve::ConstGenericSparseMatrix
const typedef Eigen::Ref< const SparseMatrix > ConstGenericSparseMatrix
Definition: SparseSymShiftSolve.h:33
Eigen::Ref
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
Spectra
Definition: LOBPCGSolver.h:19
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 >
Eigen::SparseLU
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:17
Spectra::SparseSymShiftSolve::m_mat
ConstGenericSparseMatrix m_mat
Definition: SparseSymShiftSolve.h:35
Spectra::SparseSymShiftSolve
Definition: SparseSymShiftSolve.h:25
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
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 Sun Dec 22 2024 04:13:51