// Copyright (C) 2016-2019 Yixuan Qiu // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at https://mozilla.org/MPL/2.0/. #ifndef SPARSE_SYM_MAT_PROD_H #define SPARSE_SYM_MAT_PROD_H #include #include namespace Spectra { /// /// \ingroup MatOp /// /// This class defines the matrix-vector multiplication operation on a /// sparse real symmetric matrix \f$A\f$, i.e., calculating \f$y=Ax\f$ for any vector /// \f$x\f$. It is mainly used in the SymEigsSolver eigen solver. /// template class SparseSymMatProd { private: typedef Eigen::Index Index; typedef Eigen::Matrix Vector; typedef Eigen::Map MapConstVec; typedef Eigen::Map MapVec; typedef Eigen::SparseMatrix SparseMatrix; typedef const Eigen::Ref ConstGenericSparseMatrix; ConstGenericSparseMatrix m_mat; public: /// /// Constructor to create the matrix operation object. /// /// \param mat An **Eigen** sparse matrix object, whose type can be /// `Eigen::SparseMatrix` or its mapped version /// `Eigen::Map >`. /// SparseSymMatProd(ConstGenericSparseMatrix& mat) : m_mat(mat) {} /// /// Return the number of rows of the underlying matrix. /// Index rows() const { return m_mat.rows(); } /// /// Return the number of columns of the underlying matrix. /// Index cols() const { return m_mat.cols(); } /// /// Perform the matrix-vector multiplication operation \f$y=Ax\f$. /// /// \param x_in Pointer to the \f$x\f$ vector. /// \param y_out Pointer to the \f$y\f$ vector. /// // y_out = A * x_in void perform_op(const Scalar* x_in, Scalar* y_out) const { MapConstVec x(x_in, m_mat.cols()); MapVec y(y_out, m_mat.rows()); y.noalias() = m_mat.template selfadjointView() * x; } }; } // namespace Spectra #endif // SPARSE_SYM_MAT_PROD_H