Skip to content

Commit

Permalink
Adding eigen linear solver to the corresponding class
Browse files Browse the repository at this point in the history
  • Loading branch information
Goul-tard committed Dec 22, 2024
1 parent 400841d commit b935f99
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 8 additions & 2 deletions examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/demeter/solve/linear_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

#include <cmath>
#include <iostream>
#include <Eigen/Dense>

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
9 changes: 7 additions & 2 deletions src/demeter/solve/linear_solver.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <Eigen/Core>
#include <Eigen/Dense>

#include "methods.hpp"
#include "demeter/common.hpp"
Expand All @@ -12,16 +13,20 @@ 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();
void solve_ldtl();
void solve_PartialPivLu();

private:
Eigen::Matrix2f A_;
Eigen::Matrix2f b_;

double tolerance = 1e-5;

};
Expand Down

0 comments on commit b935f99

Please sign in to comment.