pairinteraction
A Rydberg Interaction Calculator
DiagonalizerEigen.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Pairinteraction Developers
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
5
10
11#include <Eigen/Dense>
12#include <Eigen/Eigenvalues>
13#include <cmath>
14
15namespace pairinteraction {
16
17template <typename Scalar>
19 : DiagonalizerInterface<Scalar>(float_type) {}
20
21template <typename Scalar>
22template <typename ScalarLim>
24DiagonalizerEigen<Scalar>::dispatch_eigh(const Eigen::SparseMatrix<Scalar, Eigen::RowMajor> &matrix,
25 double rtol) const {
27 int dim = matrix.rows();
28
29 // Subtract the mean of the diagonal elements from the diagonal
30 real_t shift{};
31 Eigen::MatrixX<ScalarLim> shifted_matrix =
32 this->template subtract_mean<ScalarLim>(matrix, shift, rtol);
33
34 // Diagonalize the shifted matrix
35 Eigen::SelfAdjointEigenSolver<Eigen::MatrixX<ScalarLim>> eigensolver;
36 eigensolver.compute(shifted_matrix);
37
38 return {eigensolver.eigenvectors()
39 .sparseView(1, 0.5 * rtol / std::sqrt(dim))
40 .template cast<Scalar>(),
41 this->add_mean(eigensolver.eigenvalues(), shift)};
42}
43
44template <typename Scalar>
45EigenSystemH<Scalar>
46DiagonalizerEigen<Scalar>::eigh(const Eigen::SparseMatrix<Scalar, Eigen::RowMajor> &matrix,
47 double rtol) const {
48 switch (this->float_type) {
50 return dispatch_eigh<traits::restricted_t<Scalar, FloatType::FLOAT32>>(matrix, rtol);
52 return dispatch_eigh<traits::restricted_t<Scalar, FloatType::FLOAT64>>(matrix, rtol);
53 default:
54 throw std::invalid_argument("Unsupported floating point precision.");
55 }
56}
57
58// Explicit instantiations
59template class DiagonalizerEigen<double>;
61} // namespace pairinteraction
DiagonalizerEigen(FloatType float_type=FloatType::FLOAT64)
EigenSystemH< Scalar > eigh(const Eigen::SparseMatrix< Scalar, Eigen::RowMajor > &matrix, double rtol) const override
Matrix< Type, Dynamic, Dynamic > MatrixX