Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LUD methods #90

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions core/include/marsvin/lu_decomposition/partial_pivoting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
#include <cstdlib>

#include "marsvin/containers/matrix.hpp"
#include "marsvin/lu_decomposition/result_types.hpp"
#include "marsvin/tools/logger.hpp"

namespace marsvin {
namespace lud {

template<typename T>
std::vector<::marsvin::Matrix<T>> partial_pivoting(::marsvin::Matrix<T>& A) {
result<T> partial_pivoting(::marsvin::Matrix<T>& A) {
marsvin::Logger logger_;
// Initial conditions
std::size_t n = A.rows();
Expand All @@ -24,6 +25,7 @@ std::vector<::marsvin::Matrix<T>> partial_pivoting(::marsvin::Matrix<T>& A) {
::marsvin::Matrix<T> U{A};
::marsvin::Matrix<T> P(n);
P.set_diagonal(1);
::marsvin::Matrix<T> Q;

std::size_t i;
for (std::size_t k = 0; k <= (n - 2); ++k) {
Expand Down Expand Up @@ -57,12 +59,9 @@ std::vector<::marsvin::Matrix<T>> partial_pivoting(::marsvin::Matrix<T>& A) {
}
}
// Results
std::vector<::marsvin::Matrix<T>> result_matrices;
result_matrices.reserve(3);
result_matrices.push_back(std::move(L));
result_matrices.push_back(std::move(U));
result_matrices.push_back(std::move(P));
return result_matrices;
::marsvin::lud::result<T> result_(
std::move(L), std::move(U), std::move(P), std::move(Q));
return result_;
}

} // namespace lud
Expand Down
45 changes: 45 additions & 0 deletions core/include/marsvin/lu_decomposition/result_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @file result_types.hpp
*
*/

#ifndef MARSVIN_LU_DECOMPOSITION_RESULT_TYPES_HPP_
#define MARSVIN_LU_DECOMPOSITION_RESULT_TYPES_HPP_

namespace marsvin {
namespace lud {

template<typename T>
class result {
public:
result(::marsvin::Matrix<T> L,
::marsvin::Matrix<T> U,
::marsvin::Matrix<T> P,
::marsvin::Matrix<T> Q) {
result_.reserve(4);
result_.push_back(std::move(L));
result_.push_back(std::move(U));
result_.push_back(std::move(P));
result_.push_back(std::move(Q));
}
const ::marsvin::Matrix<T>& L() {
return result_.at(0);
};
::marsvin::Matrix<T> U() {
return result_.at(1);
}
::marsvin::Matrix<T> P() {
return result_.at(2);
}
::marsvin::Matrix<T> Q() {
return result_.at(3);
}

private:
std::vector<::marsvin::Matrix<T>> result_;
};

} // namespace lud
} // namespace marsvin

#endif // MARSVIN_LU_DECOMPOSITION_RESULT_TYPES_HPP_
22 changes: 11 additions & 11 deletions test/test_marsvin_lud_partial_pivoting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ TEST(PartialPivoting, Algorithm_2x2) {
marsvin::Matrix<float> A = {{1,2},
{3,4}};
auto lud_matrices = marsvin::lud::partial_pivoting(A);
auto L = lud_matrices.at(0);
auto U = lud_matrices.at(1);
auto P = lud_matrices.at(2);
auto L = lud_matrices.L();
auto U = lud_matrices.U();
auto P = lud_matrices.P();
std::cout << "P*A :" << "\n";
logger_ << P*A;
std::cout << "L*U :" << "\n";
logger_ << L*U;
float tolerance = 0.1;
EXPECT_TRUE(marsvin::tools::compare(P*A,L*U,tolerance));
}

/*
TEST(PartialPivoting, Algorithm_3x3) {
marsvin::Logger logger_;
marsvin::Matrix<float> A = {{1.0,2.0,3.0},
{4.0,5.0,6.0},
{7.0,8.0,9.0}};
auto lud_matrices = marsvin::lud::partial_pivoting(A);
auto L = lud_matrices.at(0);
auto U = lud_matrices.at(1);
auto P = lud_matrices.at(2);
auto& L = lud_matrices.L;
auto& U = lud_matrices.U;
auto& P = lud_matrices.P;
std::cout << "P*A :" << "\n";
logger_ << P*A;
std::cout << "L*U :" << "\n";
Expand All @@ -44,14 +44,14 @@ TEST(PartialPivoting, Algorithm_4x4) {
{2.0,19.0,10.0,23.0},
{4.0,10.0,11.0,31.0}};
auto lud_matrices = marsvin::lud::partial_pivoting(A);
auto L = lud_matrices.at(0);
auto U = lud_matrices.at(1);
auto P = lud_matrices.at(2);
auto& L = lud_matrices.L;
auto& U = lud_matrices.U;
auto& P = lud_matrices.P;
std::cout << "P*A :" << "\n";
logger_ << P*A;
std::cout << "L*U :" << "\n";
logger_ << L*U;
float tolerance = 0.1;
EXPECT_TRUE(marsvin::tools::compare(P*A,L*U,tolerance));
}

*/
Loading