Spectra  1.0.1
Header-only C++ Library for Large Scale Eigenvalue Problems
SparseSymMatProd.h
1 // Copyright (C) 2016-2022 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_SYM_MAT_PROD_H
8 #define SPECTRA_SPARSE_SYM_MAT_PROD_H
9 
10 #include <Eigen/Core>
11 #include <Eigen/SparseCore>
12 
13 namespace Spectra {
14 
30 template <typename Scalar_, int Uplo = Eigen::Lower, int Flags = Eigen::ColMajor, typename StorageIndex = int>
32 {
33 public:
37  using Scalar = Scalar_;
38 
39 private:
40  using Index = Eigen::Index;
41  using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
42  using MapConstVec = Eigen::Map<const Vector>;
43  using MapVec = Eigen::Map<Vector>;
44  using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
45  using SparseMatrix = Eigen::SparseMatrix<Scalar, Flags, StorageIndex>;
46  using ConstGenericSparseMatrix = const Eigen::Ref<const SparseMatrix>;
47 
48  ConstGenericSparseMatrix m_mat;
49 
50 public:
58  template <typename Derived>
59  SparseSymMatProd(const Eigen::SparseMatrixBase<Derived>& mat) :
60  m_mat(mat)
61  {
62  static_assert(
63  static_cast<int>(Derived::PlainObject::IsRowMajor) == static_cast<int>(SparseMatrix::IsRowMajor),
64  "SparseSymMatProd: the \"Flags\" template parameter does not match the input matrix (Eigen::ColMajor/Eigen::RowMajor)");
65  }
66 
70  Index rows() const { return m_mat.rows(); }
74  Index cols() const { return m_mat.cols(); }
75 
82  // y_out = A * x_in
83  void perform_op(const Scalar* x_in, Scalar* y_out) const
84  {
85  MapConstVec x(x_in, m_mat.cols());
86  MapVec y(y_out, m_mat.rows());
87  y.noalias() = m_mat.template selfadjointView<Uplo>() * x;
88  }
89 
93  Matrix operator*(const Eigen::Ref<const Matrix>& mat_in) const
94  {
95  return m_mat.template selfadjointView<Uplo>() * mat_in;
96  }
97 
101  Scalar operator()(Index i, Index j) const
102  {
103  return m_mat.coeff(i, j);
104  }
105 };
106 } // namespace Spectra
107 
108 #endif // SPECTRA_SPARSE_SYM_MAT_PROD_H
SparseSymMatProd(const Eigen::SparseMatrixBase< Derived > &mat)
void perform_op(const Scalar *x_in, Scalar *y_out) const
Matrix operator*(const Eigen::Ref< const Matrix > &mat_in) const
Scalar operator()(Index i, Index j) const