2011-10-11 19:38:36 +02:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2011 Gael Guennebaud <g.gael@free.fr>
|
|
|
|
|
//
|
2012-07-13 14:42:47 -04:00
|
|
|
// 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/.
|
2011-10-11 19:38:36 +02:00
|
|
|
|
|
|
|
|
#include "sparse_solver.h"
|
2011-11-12 14:11:27 +01:00
|
|
|
#include <Eigen/IterativeLinearSolvers>
|
2011-10-11 19:38:36 +02:00
|
|
|
|
2019-01-25 14:54:39 +01:00
|
|
|
template <typename T, typename I_>
|
|
|
|
|
void test_bicgstab_T() {
|
|
|
|
|
BiCGSTAB<SparseMatrix<T, 0, I_>, DiagonalPreconditioner<T> > bicgstab_colmajor_diag;
|
|
|
|
|
BiCGSTAB<SparseMatrix<T, 0, I_>, IdentityPreconditioner> bicgstab_colmajor_I;
|
|
|
|
|
BiCGSTAB<SparseMatrix<T, 0, I_>, IncompleteLUT<T, I_> > bicgstab_colmajor_ilut;
|
2011-11-12 14:11:27 +01:00
|
|
|
// BiCGSTAB<SparseMatrix<T>, SSORPreconditioner<T> > bicgstab_colmajor_ssor;
|
2011-10-11 19:38:36 +02:00
|
|
|
|
2015-06-05 14:32:26 +02:00
|
|
|
bicgstab_colmajor_diag.setTolerance(NumTraits<T>::epsilon() * 4);
|
|
|
|
|
bicgstab_colmajor_ilut.setTolerance(NumTraits<T>::epsilon() * 4);
|
2023-12-05 21:22:55 +00:00
|
|
|
|
2011-10-11 19:38:36 +02:00
|
|
|
CALL_SUBTEST(check_sparse_square_solving(bicgstab_colmajor_diag));
|
2012-02-10 18:57:38 +01:00
|
|
|
// CALL_SUBTEST( check_sparse_square_solving(bicgstab_colmajor_I) );
|
|
|
|
|
CALL_SUBTEST(check_sparse_square_solving(bicgstab_colmajor_ilut));
|
2011-11-12 14:11:27 +01:00
|
|
|
// CALL_SUBTEST( check_sparse_square_solving(bicgstab_colmajor_ssor) );
|
2011-10-11 19:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-11 19:41:59 +00:00
|
|
|
// https://gitlab.com/libeigen/eigen/-/issues/2856
|
|
|
|
|
void test_2856() {
|
|
|
|
|
Eigen::MatrixXd D = Eigen::MatrixXd::Identity(14, 14);
|
|
|
|
|
D(6, 13) = 1;
|
|
|
|
|
D(13, 12) = 1;
|
|
|
|
|
using CSRMatrix = Eigen::SparseMatrix<double, Eigen::RowMajor>;
|
|
|
|
|
CSRMatrix A = D.sparseView();
|
|
|
|
|
|
|
|
|
|
Eigen::VectorXd b = Eigen::VectorXd::Zero(14);
|
|
|
|
|
b(12) = -1001;
|
|
|
|
|
|
|
|
|
|
Eigen::BiCGSTAB<CSRMatrix> solver;
|
|
|
|
|
solver.compute(A);
|
|
|
|
|
Eigen::VectorXd x = solver.solve(b);
|
|
|
|
|
Eigen::VectorXd expected = Eigen::VectorXd::Zero(14);
|
|
|
|
|
expected(6) = -1001;
|
|
|
|
|
expected(12) = -1001;
|
|
|
|
|
expected(13) = 1001;
|
|
|
|
|
VERIFY_IS_EQUAL(x, expected);
|
|
|
|
|
|
|
|
|
|
Eigen::VectorXd residual = b - A * x;
|
|
|
|
|
VERIFY(residual.isZero());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://gitlab.com/libeigen/eigen/-/issues/2899
|
|
|
|
|
void test_2899() {
|
2025-02-21 10:27:29 -08:00
|
|
|
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(4, 4);
|
2025-02-11 19:41:59 +00:00
|
|
|
A(0, 0) = 1;
|
|
|
|
|
A(1, 0) = -1.0 / 6;
|
|
|
|
|
A(1, 1) = 2.0 / 3;
|
|
|
|
|
A(1, 2) = -1.0 / 6;
|
|
|
|
|
A(1, 3) = -1.0 / 3;
|
|
|
|
|
A(2, 1) = -1.0 / 3;
|
|
|
|
|
A(2, 2) = 1;
|
|
|
|
|
A(2, 3) = -2.0 / 3;
|
|
|
|
|
A(3, 1) = -1.0 / 3;
|
|
|
|
|
A(3, 2) = -1.0 / 3;
|
|
|
|
|
A(3, 3) = 2.0 / 3;
|
2025-02-21 10:27:29 -08:00
|
|
|
Eigen::VectorXd b = Eigen::VectorXd::Zero(4);
|
2025-02-11 19:41:59 +00:00
|
|
|
b(0) = 0;
|
|
|
|
|
b(1) = 1;
|
|
|
|
|
b(2) = 1;
|
|
|
|
|
b(3) = 1;
|
|
|
|
|
Eigen::BiCGSTAB<Eigen::MatrixXd> solver;
|
|
|
|
|
solver.compute(A);
|
|
|
|
|
Eigen::VectorXd x = solver.solve(b);
|
|
|
|
|
Eigen::VectorXd expected(4);
|
|
|
|
|
expected << 0, 15, 18, 18;
|
|
|
|
|
VERIFY_IS_APPROX(x, expected);
|
|
|
|
|
Eigen::VectorXd residual = b - A * x;
|
|
|
|
|
VERIFY(residual.isZero());
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 14:46:15 +02:00
|
|
|
EIGEN_DECLARE_TEST(bicgstab) {
|
2015-03-09 13:55:20 +01:00
|
|
|
CALL_SUBTEST_1((test_bicgstab_T<double, int>()));
|
|
|
|
|
CALL_SUBTEST_2((test_bicgstab_T<std::complex<double>, int>()));
|
2015-03-09 14:33:15 +01:00
|
|
|
CALL_SUBTEST_3((test_bicgstab_T<double, long int>()));
|
2025-02-11 19:41:59 +00:00
|
|
|
CALL_SUBTEST_4(test_2856());
|
|
|
|
|
CALL_SUBTEST_5(test_2899());
|
2011-10-11 19:38:36 +02:00
|
|
|
}
|