Spectra 1.1.0
Header-only C++ Library for Large Scale Eigenvalue Problems
Loading...
Searching...
No Matches
SparseHermMatProd.h
1// Copyright (C) 2024-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_HERM_MAT_PROD_H
8#define SPECTRA_SPARSE_HERM_MAT_PROD_H
9
10#include <Eigen/Core>
11#include <Eigen/SparseCore>
12
13namespace Spectra {
14
30template <typename Scalar_, int Uplo = Eigen::Lower, int Flags = Eigen::ColMajor, typename StorageIndex = int>
32{
33public:
37 using Scalar = Scalar_;
38
39private:
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
50public:
58 template <typename Derived>
59 SparseHermMatProd(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 "SparseHermMatProd: 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};
90} // namespace Spectra
91
92#endif // SPECTRA_SPARSE_HERM_MAT_PROD_H
SparseHermMatProd(const Eigen::SparseMatrixBase< Derived > &mat)
void perform_op(const Scalar *x_in, Scalar *y_out) const