| // This file is part of Eigen, a lightweight C++ template library | |
| // for linear algebra. | |
| // | |
| // Copyright (C) 2026 The Eigen Authors | |
| // | |
| // 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 http://mozilla.org/MPL/2.0/. | |
| // SPDX-License-Identifier: MPL-2.0 | |
| // Tests the state handling of ArpackGeneralizedSelfAdjointEigenSolver, especially | |
| // the values supplied as shift when calling the compute() method. | |
| #include "sparse.h" | |
| #include <Eigen/ArpackSupport> | |
| template <class Solver> | |
| struct handles_indefinite : std::false_type {}; | |
| template <class M, int UpLo, class Ord> | |
| struct handles_indefinite<SimplicialLDLT<M, UpLo, Ord>> : std::true_type {}; | |
| template <class M, class Ord> | |
| struct handles_indefinite<SparseLU<M, Ord>> : std::true_type {}; | |
| template <class T> | |
| constexpr bool handles_indefinite_v = handles_indefinite<T>::value; | |
| // builds the 'stiffness' matrix for the 1D Laplacian equation with size n and grid spacing h | |
| static SparseMatrix<double> build_laplacian_1d_A_matrix(Index n, double h) { | |
| SparseMatrix<double> A(n, n); | |
| for (int i = 0; i < n; ++i) { | |
| A.insert(i, i) = 2.0 / h; | |
| if (i + 1 < n) { | |
| A.insert(i, i + 1) = -1.0 / h; | |
| A.insert(i + 1, i) = -1.0 / h; | |
| } | |
| } | |
| return A; | |
| } | |
| // builds the 'mass' matrix for the 1D Laplacian equation with size n and grid spacing h | |
| static SparseMatrix<double> build_laplacian_1d_B_matrix(Index n, double h) { | |
| SparseMatrix<double> B(n, n); | |
| for (int i = 0; i < n; ++i) { | |
| B.insert(i, i) = 4.0 * h / 6.0; | |
| if (i + 1 < n) { | |
| B.insert(i, i + 1) = h / 6.0; | |
| B.insert(i + 1, i) = h / 6.0; | |
| } | |
| } | |
| return B; | |
| } | |
| // returns the exact k-th eigenvalue for a given grid spacing h | |
| double lambda_exact(int k, double h) { | |
| const double c = std::cos(k * EIGEN_PI * h); | |
| return 6.0 * (1.0 - c) / (h * h * (2.0 + c)); | |
| } | |
| // returns all eigenvalues for a given sigma and grid spacing h | |
| std::vector<double> reference(Index n, Index nev, double sigma, double h) { | |
| std::vector<double> all(n); | |
| for (int k = 1; k <= n; ++k) all[k - 1] = lambda_exact(k, h); | |
| std::sort(all.begin(), all.end(), [&](double a, double b) { return std::abs(a - sigma) < std::abs(b - sigma); }); | |
| all.resize(nev); | |
| std::sort(all.begin(), all.end()); | |
| return all; | |
| } | |
| // extraction of largest magnitude eigenvalues | |
| // - LLT or LU matrix solvers work | |
| // - checks whether the solver returns success | |
| // - checks whether the extracted eigenvalues match w.r.t. theory | |
| template <class T> | |
| void test_arpack_sigma_LM() { | |
| const Index n = 200; | |
| static const double h = 1.0 / (n + 1); | |
| const Index nev = 6; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, h); | |
| const SparseMatrix<double> B = build_laplacian_1d_B_matrix(n, h); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| solver.compute(A, B, nev, "LM"); | |
| VERIFY(solver.info() == Success); | |
| for (int i = 0; i < nev; ++i) { | |
| VERIFY_IS_APPROX(solver.eigenvalues()[i], lambda_exact(n - nev + i + 1, h)); | |
| } | |
| } | |
| // extraction of smallest magnitude eigenvalues | |
| // - LLT or LU matrix solvers work | |
| // - checks whether the solver returns success | |
| // - checks whether the extracted eigenvalues match w.r.t. theory | |
| template <class T> | |
| void test_arpack_sigma_SM() { | |
| const Index n = 200; | |
| static const double h = 1.0 / (n + 1); | |
| const Index nev = 6; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, h); | |
| const SparseMatrix<double> B = build_laplacian_1d_B_matrix(n, h); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| solver.compute(A, B, nev, "SM"); | |
| VERIFY(solver.info() == Success); | |
| for (int i = 0; i < nev; ++i) { | |
| VERIFY_IS_APPROX(solver.eigenvalues()[i], lambda_exact(i + 1, h)); | |
| } | |
| } | |
| // extraction of eigenvalues close to sigma, which is below the spectrum | |
| // - LLT or LU matrix solvers work (A-sig*M is positive definite) | |
| // - checks whether the solver returns success | |
| // - checks whether the extracted eigenvalues match w.r.t. theory | |
| template <class T> | |
| void test_arpack_sigma_below() { | |
| const Index n = 200; | |
| static const double h = 1.0 / (n + 1); | |
| const Index nev = 6; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, h); | |
| const SparseMatrix<double> B = build_laplacian_1d_B_matrix(n, h); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| std::stringstream sig; | |
| sig << std::setprecision(8) << lambda_exact(1, h) / 2.0; // arbitrary sigma, as long as it is below the spectrum | |
| solver.compute(A, B, nev, sig.str()); | |
| VERIFY(solver.info() == Success); | |
| for (int i = 0; i < nev; ++i) { | |
| VERIFY_IS_APPROX(solver.eigenvalues()[i], lambda_exact(i + 1, h)); | |
| } | |
| } | |
| // extraction of eigenvalues close to sigma, which is inside the spectrum | |
| // - LLT matrix solvers fails because the A-sig*M is indefinite | |
| // - LU matrix solvers works | |
| // - checks whether the solver returns success | |
| // - checks whether the extracted eigenvalues match w.r.t. theory | |
| template <class T> | |
| void test_arpack_sigma_inside() { | |
| const Index n = 200; | |
| static const double h = 1.0 / (n + 1); | |
| const Index nev = 6; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, h); | |
| const SparseMatrix<double> B = build_laplacian_1d_B_matrix(n, h); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| double sig_val = | |
| (lambda_exact(1, h) + lambda_exact(n + 1, h)) / 2.0; // arbitrary sigma, as long as it is inside the spectrum | |
| std::stringstream sig; | |
| sig << std::setprecision(8) << sig_val; // string version | |
| solver.compute(A, B, nev, sig.str()); | |
| EIGEN_IF_CONSTEXPR (handles_indefinite_v<T>) { | |
| // works | |
| VERIFY(solver.info() == Success); | |
| std::vector<double> ref = reference(n, nev, sig_val, h); | |
| for (int i = 0; i < nev; ++i) { | |
| VERIFY_IS_APPROX(solver.eigenvalues()[i], ref[i]); | |
| } | |
| } else { | |
| // A-sig*M is indefinite, the test should fail | |
| VERIFY(solver.info() == NumericalIssue); | |
| } | |
| } | |
| // extraction of eigenvalues close to sigma, which is above the spectrum | |
| // - LLT matrix solvers fails because the A-sig*M is negative definite | |
| // - LU matrix solvers works | |
| // - checks whether the solver returns success | |
| // - checks whether the extracted eigenvalues match w.r.t. theory | |
| template <class T> | |
| void test_arpack_sigma_above() { | |
| const Index n = 200; | |
| static const double h = 1.0 / (n + 1); | |
| const Index nev = 6; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, h); | |
| const SparseMatrix<double> B = build_laplacian_1d_B_matrix(n, h); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| double sig_val = lambda_exact(n + 1, h) * 1.5; // arbitrary sigma, as long as it is above the spectrum | |
| std::stringstream sig; | |
| sig << std::setprecision(8) << sig_val; // string version | |
| solver.compute(A, B, nev, sig.str()); | |
| EIGEN_IF_CONSTEXPR (handles_indefinite_v<T>) { | |
| // works | |
| VERIFY(solver.info() == Success); | |
| std::vector<double> ref = reference(n, nev, sig_val, h); | |
| for (int i = 0; i < nev; ++i) { | |
| VERIFY_IS_APPROX(solver.eigenvalues()[i], ref[i]); | |
| } | |
| } else { | |
| // A-sig*M is indefinite, the test should fail | |
| VERIFY(solver.info() == NumericalIssue); | |
| } | |
| } | |
| // invalid values of sigma | |
| // - a string that is neither a mode nor a finite number gives InvalidInput | |
| // - a valid shift or mode afterwards succeeds | |
| template <class T> | |
| void test_arpack_bad_sigma() { | |
| const Index n = 200; | |
| static const double h = 1.0 / (n + 1); | |
| const Index nev = 6; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, h); | |
| const SparseMatrix<double> B = build_laplacian_1d_B_matrix(n, h); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| // test for empty space in sigma (should fail) | |
| solver.compute(A, B, nev, ""); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for single char in sigma (should fail) | |
| solver.compute(A, B, nev, "x"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for non-supported string sigma (should fail) | |
| solver.compute(A, B, nev, "AB"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for leading space in sigma (should fail) | |
| solver.compute(A, B, nev, " LM"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for trailing space in sigma (should fail) | |
| solver.compute(A, B, nev, "LM "); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for comma as decimal separator in sigma (should fail) | |
| solver.compute(A, B, nev, "1,5"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for bad value in sigma (should fail) | |
| solver.compute(A, B, nev, "nan"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for bad value in sigma (should fail) | |
| solver.compute(A, B, nev, "inf"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for bad value in sigma (should fail) | |
| solver.compute(A, B, nev, "1e999"); | |
| VERIFY(solver.info() == InvalidInput); | |
| // test for good value in sigma (should work) | |
| solver.compute(A, B, nev, "1.5"); | |
| VERIFY(solver.info() == Success); | |
| // test for good value in sigma (should work) | |
| solver.compute(A, B, nev, "lm"); // checks toupper | |
| VERIFY(solver.info() == Success); | |
| } | |
| // a shift for the standard problem A x = lambda x, which factorizes A - sigma * I | |
| template <class T> | |
| void test_arpack_sigma_standard() { | |
| const Index n = 200; | |
| const Index nev = 6; | |
| const double sig_val = 1.05; | |
| const SparseMatrix<double> A = build_laplacian_1d_A_matrix(n, 1.0); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<double>, T> solver; | |
| solver.compute(A, nev, "1.05"); | |
| VERIFY(solver.info() == Success); | |
| // the eigenvalues of tridiag(-1, 2, -1) are 2 - 2 cos(k pi / (n + 1)) | |
| std::vector<double> ref(n); | |
| for (int k = 1; k <= n; ++k) ref[k - 1] = 2.0 - 2.0 * std::cos(k * EIGEN_PI / (n + 1)); | |
| std::sort(ref.begin(), ref.end(), [&](double a, double b) { return std::abs(a - sig_val) < std::abs(b - sig_val); }); | |
| ref.resize(nev); | |
| std::sort(ref.begin(), ref.end()); | |
| for (int i = 0; i < nev; ++i) { | |
| VERIFY_IS_APPROX(solver.eigenvalues()[i], ref[i]); | |
| } | |
| } | |
| // a shift that is finite as a double but overflows the solver's float is rejected | |
| void test_arpack_sigma_overflows_float() { | |
| const SparseMatrix<float> A = build_laplacian_1d_A_matrix(200, 1.0).cast<float>(); | |
| ArpackGeneralizedSelfAdjointEigenSolver<SparseMatrix<float>> solver; | |
| solver.compute(A, 6, "1e39"); | |
| VERIFY(solver.info() == InvalidInput); | |
| } | |
| EIGEN_DECLARE_TEST(arpack) { | |
| // largest magnitude, with SimplicialLLT matrix solver | |
| CALL_SUBTEST_1(test_arpack_sigma_LM<SimplicialLLT<SparseMatrix<double>>>()); | |
| // smallest magnitude, with SimplicialLLT matrix solver | |
| CALL_SUBTEST_1(test_arpack_sigma_SM<SimplicialLLT<SparseMatrix<double>>>()); | |
| // sigma below the spectrum, with SimplicialLLT matrix solver | |
| CALL_SUBTEST_1(test_arpack_sigma_below<SimplicialLLT<SparseMatrix<double>>>()); | |
| // sigma inside the spectrum, with SimplicialLLT and SparseLU matrix solvers | |
| CALL_SUBTEST_1(test_arpack_sigma_inside<SimplicialLLT<SparseMatrix<double>>>()); | |
| CALL_SUBTEST_1(test_arpack_sigma_inside<SparseLU<SparseMatrix<double>>>()); | |
| // sigma above the spectrum, with SimplicialLLT and SparseLU matrix solvers | |
| CALL_SUBTEST_1(test_arpack_sigma_above<SimplicialLLT<SparseMatrix<double>>>()); | |
| CALL_SUBTEST_1(test_arpack_sigma_above<SparseLU<SparseMatrix<double>>>()); | |
| // testing for bad values | |
| CALL_SUBTEST_1(test_arpack_bad_sigma<SimplicialLLT<SparseMatrix<double>>>()); | |
| // testing shift for standard problem A x = lambda x, with SimplicialLLT and SparseLU matrix solvers | |
| CALL_SUBTEST_1(test_arpack_sigma_standard<SimplicialLDLT<SparseMatrix<double>>>()); | |
| CALL_SUBTEST_1(test_arpack_sigma_standard<SparseLU<SparseMatrix<double>>>()); | |
| // testing for sigma overflow with float | |
| CALL_SUBTEST_1(test_arpack_sigma_overflows_float()); | |
| } |