From b935f99320404ae3dc3ce10d721759debbe63f41 Mon Sep 17 00:00:00 2001 From: Arthur Date: Sun, 22 Dec 2024 14:20:53 +0100 Subject: [PATCH] Adding eigen linear solver to the corresponding class --- examples/main.cpp | 10 ++++++++-- src/demeter/solve/linear_solver.cpp | 14 ++++++++++---- src/demeter/solve/linear_solver.hpp | 9 +++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/examples/main.cpp b/examples/main.cpp index a64c2b6..e8e6880 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -12,9 +12,8 @@ int main() { CheckOpenMP(); - Eigen::Matrix2f A, b; - A << 2, -1, -1, 3; // sorted line by line + A << 2, -1, -1, 3; b << 1, 2, 3, 1; std::cout << "Here is the matrix A:\n" << A << std::endl; std::cout << "Here is the right hand side b:\n" << b << std::endl; @@ -23,6 +22,13 @@ int main() { Eigen::Matrix2f x2 = A.llt().solve(b); std::cout << "The solution is:\n" << x2 << std::endl; + std::cout << '\n'; + + LinearSolver LinSolver(A,b); + LinSolver.solve_llt(); + LinSolver.solve_ldtl(); + LinSolver.solve_PartialPivLu(); + std::cout << '\n'; // Define variables diff --git a/src/demeter/solve/linear_solver.cpp b/src/demeter/solve/linear_solver.cpp index 9d0dfb0..734af35 100644 --- a/src/demeter/solve/linear_solver.cpp +++ b/src/demeter/solve/linear_solver.cpp @@ -2,19 +2,25 @@ #include #include +#include namespace Demeter { void LinearSolver::solve_llt() { - std::cout << "Standard Cholesky decomposition" << std::endl; + std::cout << "Method: Standard Cholesky decomposition" << std::endl; + Eigen::Matrix2f x1 = A_.llt().solve(b_); + std::cout << "The solution is:\n" << x1 << std::endl; } void LinearSolver::solve_ldtl() { - std::cout << "Robust Cholesky decomposition with pivoting" << std::endl; + std::cout << "Method: Robust Cholesky decomposition with pivoting" << std::endl; + Eigen::Matrix2f x1 = A_.ldlt().solve(b_); + std::cout << "The solution is:\n" << x1 << std::endl; } void LinearSolver::solve_PartialPivLu() { - std::cout << "Partial LU pivoting" << std::endl; + std::cout << "Method: Partial LU pivoting" << std::endl; + Eigen::Matrix2f x1 = A_.partialPivLu().solve(b_); + std::cout << "The solution is:\n" << x1 << std::endl; } - } // namespace Demeterr diff --git a/src/demeter/solve/linear_solver.hpp b/src/demeter/solve/linear_solver.hpp index b6b6bb3..b086484 100644 --- a/src/demeter/solve/linear_solver.hpp +++ b/src/demeter/solve/linear_solver.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include "methods.hpp" #include "demeter/common.hpp" @@ -12,9 +13,10 @@ class LinearSolver { public: // Constructeur vide - LinearSolver() { + LinearSolver(const Eigen::Matrix2f& A, const Eigen::Matrix2f& b) + : A_(A), b_(b) { // Initialisation par défaut - std::cout << "Constructeur vide" << std::endl; + std::cout << "Initialisation constructeur" << std::endl; } void solve_llt(); @@ -22,6 +24,9 @@ class LinearSolver { void solve_PartialPivLu(); private: + Eigen::Matrix2f A_; + Eigen::Matrix2f b_; + double tolerance = 1e-5; };