Select Git revision
PugsFunctionAdapter.hpp
test_LeastSquareSolver.cpp 1.47 KiB
#include <catch2/catch.hpp>
#include <algebra/LeastSquareSolver.hpp>
#include <algebra/LocalRectangularMatrix.hpp>
#include <algebra/Vector.hpp>
// clazy:excludeall=non-pod-global-static
TEST_CASE("LeastSquareSolver", "[algebra]")
{
SECTION("Least Squares [under-determined]")
{
Vector<double> b{3};
b[0] = 1;
b[1] = 0;
b[2] = 0;
LocalRectangularMatrix<double> A{3, 4};
A(0, 0) = 1;
A(0, 1) = 1;
A(0, 2) = 1;
A(0, 3) = 1;
A(1, 0) = 1;
A(1, 1) = -1;
A(1, 2) = 0;
A(1, 3) = 0;
A(2, 0) = 0;
A(2, 1) = 0;
A(2, 2) = 1;
A(2, 3) = -1;
Vector<double> x_exact{4};
x_exact[0] = 0.25;
x_exact[1] = 0.25;
x_exact[2] = 0.25;
x_exact[3] = 0.25;
Vector<double> x{4};
x = 0;
LeastSquareSolver ls_solver;
ls_solver.solveLocalSystem(A, x, b);
Vector error = x - x_exact;
REQUIRE(std::sqrt((error, error)) < 1E-10 * std::sqrt((x, x)));
}
SECTION("Least Squares [over-determined]")
{
LocalRectangularMatrix<double> A{3, 2};
A(0, 0) = 0;
A(0, 1) = 1;
A(1, 0) = 1;
A(1, 1) = 1;
A(2, 0) = 2;
A(2, 1) = 1;
Vector<double> x_exact{2};
x_exact[0] = -3;
x_exact[1] = 5;
Vector b{3};
b[0] = 6;
b[1] = 0;
b[2] = 0;
Vector<double> x{2};
x = 0;
LeastSquareSolver ls_solver;
ls_solver.solveLocalSystem(A, x, b);
Vector error = x - x_exact;
REQUIRE(std::sqrt((error, error)) < 1E-10 * std::sqrt((x_exact, x_exact)));
}
}