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