Spectra
SparseGenMatProd.h
1 // Copyright (C) 2016-2018 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_GEN_MAT_PROD_H
8 #define SPARSE_GEN_MAT_PROD_H
9 
10 #include <Eigen/Core>
11 #include <Eigen/SparseCore>
12 
13 namespace Spectra {
14 
15 
24 template <typename Scalar, int Flags = 0, typename StorageIndex = int>
26 {
27 private:
28  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
29  typedef Eigen::Map<const Vector> MapConstVec;
30  typedef Eigen::Map<Vector> MapVec;
31  typedef Eigen::SparseMatrix<Scalar, Flags, StorageIndex> SparseMatrix;
32  typedef const Eigen::Ref<const SparseMatrix> ConstGenericSparseMatrix;
33 
34  ConstGenericSparseMatrix m_mat;
35 
36 public:
44  SparseGenMatProd(ConstGenericSparseMatrix& mat) :
45  m_mat(mat)
46  {}
47 
51  int rows() const { return m_mat.rows(); }
55  int cols() const { return m_mat.cols(); }
56 
63  // y_out = A * x_in
64  void perform_op(const Scalar* x_in, Scalar* y_out) const
65  {
66  MapConstVec x(x_in, m_mat.cols());
67  MapVec y(y_out, m_mat.rows());
68  y.noalias() = m_mat * x;
69  }
70 };
71 
72 
73 } // namespace Spectra
74 
75 #endif // SPARSE_GEN_MAT_PROD_H
void perform_op(const Scalar *x_in, Scalar *y_out) const
SparseGenMatProd(ConstGenericSparseMatrix &mat)