Line data Source code
1 : // SPDX-FileCopyrightText: 2026 PairInteraction Developers 2 : // SPDX-License-Identifier: LGPL-3.0-or-later 3 : 4 : #pragma once 5 : 6 : #include "pairinteraction/basis/Basis.hpp" 7 : 8 : #include <Eigen/SparseCore> 9 : #include <memory> 10 : 11 : namespace pairinteraction::utils { 12 : template <typename BasisType> 13 : Eigen::SparseMatrix<typename BasisType::scalar_t, Eigen::RowMajor> 14 1226 : get_energies_in_canonical_basis(const std::shared_ptr<const BasisType> &basis) { 15 : using scalar_t = typename BasisType::scalar_t; 16 : 17 1226 : const auto num_kets = static_cast<Eigen::Index>(basis->get_number_of_kets()); 18 : 19 1224 : Eigen::SparseMatrix<scalar_t, Eigen::RowMajor> matrix(num_kets, num_kets); 20 1222 : matrix.reserve(Eigen::VectorXi::Constant(num_kets, 1)); 21 144724 : for (Eigen::Index idx = 0; idx < num_kets; ++idx) { 22 143522 : matrix.insert(idx, idx) = basis->get_ket(static_cast<size_t>(idx))->get_energy(); 23 : } 24 1202 : matrix.makeCompressed(); 25 : 26 1226 : return matrix; 27 0 : } 28 : } // namespace pairinteraction::utils