Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
refactor: avoid passing ref and init, always return with struct
Browse files Browse the repository at this point in the history
  • Loading branch information
tiankaima committed Dec 19, 2023
1 parent c4361a3 commit 01f1e3e
Show file tree
Hide file tree
Showing 25 changed files with 1,414 additions and 1,378 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: |
run: |
cmake -DDEFINE_DEBUG=1 ${{ steps.strings.outputs.build-output-dir }}
cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}
Expand Down
5 changes: 5 additions & 0 deletions CustomMath_lib/BisectMethod/BisectMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ ull CalculateSignChange(const Vector &x, const Vector &y, lld mu) {
return s;
}

#pragma clang diagnostic push
#pragma ide diagnostic ignored "misc-no-recursion"

std::vector<lld> BisectMethodCall(const Vector &x, const Vector &y, lld start, lld end, lld precision) {
#ifdef DEBUG
if (start > end) {
Expand Down Expand Up @@ -72,6 +75,8 @@ std::vector<lld> BisectMethodCall(const Vector &x, const Vector &y, lld start, l
return result;
}

#pragma clang diagnostic pop

Vector BisectMethod(const Vector &x, const Vector &y, lld precision) {
#ifdef DEBUG
if (x.size != y.size + 1) {
Expand Down
10 changes: 5 additions & 5 deletions CustomMath_lib/CholeskyMethod/Cholesky_Decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void Cholesky_Decomposition_InPlace(Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
auto n = A.rows;

for (ull k = 0; k < n; k++) {
// if (A.matrix[k][k] <= 0) {
Expand All @@ -30,7 +30,7 @@ void Cholesky_Decomposition_InPlace(Matrix &A) {
Matrix Cholesky_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
auto n = A.rows;
auto L = Matrix(A);
Cholesky_Decomposition_InPlace(L);

Expand All @@ -44,9 +44,9 @@ Matrix Cholesky_Decomposition(const Matrix &A) {
}

Vector Cholesky_Solve(const Matrix &A, const Vector &b) {
Matrix L = Cholesky_Decomposition(A);
Vector y = LowerTriangleMatrix_Solve(L, b);
Vector x = UpperTriangleMatrix_Solve(L.transpose(), y);
auto L = Cholesky_Decomposition(A);
auto y = LowerTriangleMatrix_Solve(L, b);
auto x = UpperTriangleMatrix_Solve(L.transpose(), y);
return x;
}

Expand Down
8 changes: 4 additions & 4 deletions CustomMath_lib/CholeskyMethod/Cholesky_LDLT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void Cholesky_LDLT_Decomposition_InPlace(Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
auto n = A.rows;
Vector tmp = Vector(n);

for (ull j = 0; j < n; j++) {
Expand All @@ -30,7 +30,7 @@ void Cholesky_LDLT_Decomposition_InPlace(Matrix &A) {
Cholesky_LDLT_Result Cholesky_LDLT_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
auto n = A.rows;
auto L = Matrix(A);
Cholesky_LDLT_Decomposition_InPlace(L);

Expand All @@ -56,11 +56,11 @@ Vector Cholesky_LDLT_Solve(const Matrix &A, const Vector &b) {
auto L = tmp.L;
auto D = tmp.D;

Vector y = LowerTriangleMatrix_Solve(L, b);
auto y = LowerTriangleMatrix_Solve(L, b);
for (ull i = 0; i < A.rows; i++) {
y.array[i] /= D.matrix[i][i];
}
Vector x = UpperTriangleMatrix_Solve(L.transpose(), y);
auto x = UpperTriangleMatrix_Solve(L.transpose(), y);
return x;
}

Expand Down
20 changes: 7 additions & 13 deletions CustomMath_lib/GaussMethod/LU_Decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void LU_Decomposition_InPlace(Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
auto n = A.rows;

for (ull i = 0; i < n - 1; i++) {
for (ull j = i + 1; j < n; j++) {
Expand All @@ -20,12 +20,12 @@ void LU_Decomposition_InPlace(Matrix &A) {
}
}

void LU_Decomposition(const Matrix &A, Matrix &L, Matrix &U) {
LU_Decomposition_Result LU_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
L = Matrix(n, n);
U = Matrix(A);
auto n = A.rows;
auto L = Matrix(n, n);
auto U = Matrix(A);
LU_Decomposition_InPlace(U);

for (ull i = 0; i < n; i++) {
Expand All @@ -38,13 +38,7 @@ void LU_Decomposition(const Matrix &A, Matrix &L, Matrix &U) {
U.matrix[i][j] = 0;
}
}
}

LU_Decomposition_Result LU_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

Matrix L, U;
LU_Decomposition(A, L, U);
return {L, U};
}

Expand All @@ -61,8 +55,8 @@ Vector LU_Solve(const Matrix &A, const Vector &b) {
CHECK_SQUARE_MATRIX(A)
CHECK_EQUAL_SIZE(A, b)

Matrix A_copy = Matrix(A);
Vector b_copy = Vector(b);
auto A_copy = Matrix(A);
auto b_copy = Vector(b);
LU_Solve_InPlace(A_copy, b_copy);
return b_copy;
}
3 changes: 0 additions & 3 deletions CustomMath_lib/GaussMethod/LU_Decomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ typedef struct {
Matrix U;
} LU_Decomposition_Result;

/// Decomposition A = LU, det(L) == 1
void LU_Decomposition(const Matrix &A, Matrix &L, Matrix &U);

/// Decomposition A = LU, det(L) == 1
LU_Decomposition_Result LU_Decomposition(const Matrix &A);

Expand Down
39 changes: 17 additions & 22 deletions CustomMath_lib/GaussMethod/LU_FP_Decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include "LU_FP_Decomposition.h"

void LU_FP_Decomposition_InPlace(Matrix &A, Matrix &P, Matrix &Q) {
LU_FP_Decomposition_Inplace_Result LU_FP_Decomposition_InPlace(Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
P = Matrix::identity(n);
Q = Matrix::identity(n);
auto n = A.rows;
auto P = Matrix::identity(n);
auto Q = Matrix::identity(n);

for (ull i = 0; i < n; i++) {
// find the pivot
Expand Down Expand Up @@ -53,15 +53,17 @@ void LU_FP_Decomposition_InPlace(Matrix &A, Matrix &P, Matrix &Q) {
}
}
}

return {P, Q};
}

void LU_FP_Decomposition(const Matrix &A, Matrix &L, Matrix &U, Matrix &P, Matrix &Q) {
LU_FP_Decomposition_Result LU_FP_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
L = Matrix(n, n);
U = Matrix(A);
LU_FP_Decomposition_InPlace(U, P, Q);
auto n = A.rows;
auto L = Matrix(n, n);
auto U = Matrix(A);
auto [P, Q] = LU_FP_Decomposition_InPlace(U);

for (ull i = 0; i < n; i++) {
L.matrix[i][i] = 1;
Expand All @@ -72,34 +74,27 @@ void LU_FP_Decomposition(const Matrix &A, Matrix &L, Matrix &U, Matrix &P, Matri
U.matrix[i][j] = 0;
}
}
}

LU_FP_Decomposition_Result LU_FP_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

Matrix L, U, P, Q;
LU_FP_Decomposition(A, L, U, P, Q);
return {L, U, P, Q};
}

void LU_FP_Solve_InPlace(Matrix &A, Vector &b) {
CHECK_SQUARE_MATRIX(A)
CHECK_EQUAL_SIZE(A, b)

Matrix P, Q;
LU_FP_Decomposition_InPlace(A, P, Q);
Vector Pb = P * b;
Vector UQix = LowerTriangleMatrix_Solve(A, Pb, true);
Vector Qix = UpperTriangleMatrix_Solve(A, UQix);
auto [P, Q] = LU_FP_Decomposition_InPlace(A);
auto Pb = P * b;
auto UQix = LowerTriangleMatrix_Solve(A, Pb, true);
auto Qix = UpperTriangleMatrix_Solve(A, UQix);
b = Q * Qix;
}

Vector LU_FP_Solve(const Matrix &A, const Vector &b) {
CHECK_SQUARE_MATRIX(A)
CHECK_EQUAL_SIZE(A, b)

Matrix A_copy = Matrix(A);
Vector b_copy = Vector(b);
auto A_copy = Matrix(A);
auto b_copy = Vector(b);
LU_FP_Solve_InPlace(A_copy, b_copy);
return b_copy;
}
10 changes: 6 additions & 4 deletions CustomMath_lib/GaussMethod/LU_FP_Decomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@

#include "CustomMath_lib.h"

typedef struct {
Matrix P;
Matrix Q;
} LU_FP_Decomposition_Inplace_Result;

typedef struct {
Matrix L;
Matrix U;
Matrix P;
Matrix Q;
} LU_FP_Decomposition_Result;

// LU Decomposition with Full Pivoting
void LU_FP_Decomposition(const Matrix &A, Matrix &L, Matrix &U, Matrix &P, Matrix &Q);

// LU Decomposition with Full Pivoting
LU_FP_Decomposition_Result LU_FP_Decomposition(const Matrix &A);

// Solve Ax = b using LU decomposition with Full Pivoting
Vector LU_FP_Solve(const Matrix &A, const Vector &b);

// LU Decomposition with Full Pivoting, and A is stored as L + U (ignoring L's all-one diagonal)
void LU_FP_Decomposition_InPlace(Matrix &A, Matrix &P, Matrix &Q);
LU_FP_Decomposition_Inplace_Result LU_FP_Decomposition_InPlace(Matrix &A);

// Solve Ax = b using LU decomposition with Full Pivoting, b is then replaced by x
void LU_FP_Solve_InPlace(Matrix &A, Vector &b);
Expand Down
39 changes: 17 additions & 22 deletions CustomMath_lib/GaussMethod/LU_PP_Decomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

#include "LU_PP_Decomposition.h"

void LU_PP_Decomposition_InPlace(Matrix &A, Matrix &P) {
Matrix LU_PP_Decomposition_InPlace(Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
P = Matrix::identity(n);
auto n = A.rows;
auto P = Matrix::identity(n);

for (ull i = 0; i < n; i++) {
// find the pivot
ull pivot_i = i;
lld pivot = std::abs(A.matrix[i][i]);
auto pivot_i = i;
auto pivot = std::abs(A.matrix[i][i]);
for (ull j = i; j < n; j++) {
if (std::abs(A.matrix[j][i]) > std::abs(pivot)) {
pivot = A.matrix[j][i];
Expand Down Expand Up @@ -42,15 +42,17 @@ void LU_PP_Decomposition_InPlace(Matrix &A, Matrix &P) {
}
}
}

return P;
}

void LU_PP_Decomposition(const Matrix &A, Matrix &L, Matrix &U, Matrix &P) {
LU_PP_Decomposition_Result LU_PP_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

ull n = A.rows;
L = Matrix(n, n);
U = Matrix(A);
LU_PP_Decomposition_InPlace(U, P);
auto n = A.rows;
auto L = Matrix(n, n);
auto U = Matrix(A);
auto P = LU_PP_Decomposition_InPlace(U);

for (ull i = 0; i < n; i++) {
L.matrix[i][i] = 1;
Expand All @@ -61,33 +63,26 @@ void LU_PP_Decomposition(const Matrix &A, Matrix &L, Matrix &U, Matrix &P) {
U.matrix[i][j] = 0;
}
}
}

LU_PP_Decomposition_Result LU_PP_Decomposition(const Matrix &A) {
CHECK_SQUARE_MATRIX(A)

Matrix L, U, P;
LU_PP_Decomposition(A, L, U, P);
return {L, U, P};
}

void LU_PP_Solve_InPlace(Matrix &A, Vector &b) {
CHECK_SQUARE_MATRIX(A)
CHECK_EQUAL_SIZE(A, b)

Matrix P;
LU_PP_Decomposition_InPlace(A, P);
Vector Pb = P * b; // fixme: this can be optimized since P only contains pivots
Vector y = LowerTriangleMatrix_Solve(A, Pb, true);
auto P = LU_PP_Decomposition_InPlace(A);
auto Pb = P * b; // fixme: this can be optimized since P only contains pivots
auto y = LowerTriangleMatrix_Solve(A, Pb, true);
b = UpperTriangleMatrix_Solve(A, y);
}

Vector LU_PP_Solve(const Matrix &A, const Vector &b) {
CHECK_SQUARE_MATRIX(A)
CHECK_EQUAL_SIZE(A, b)

Matrix A_copy = Matrix(A);
Vector b_copy = Vector(b);
auto A_copy = Matrix(A);
auto b_copy = Vector(b);
LU_PP_Solve_InPlace(A_copy, b_copy);
return b_copy;
}
5 changes: 1 addition & 4 deletions CustomMath_lib/GaussMethod/LU_PP_Decomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ typedef struct {
Matrix P;
} LU_PP_Decomposition_Result;

// LU Decomposition with Partial Pivoting
void LU_PP_Decomposition(const Matrix &A, Matrix &L, Matrix &U, Matrix &P);

// LU Decomposition with Partial Pivoting
LU_PP_Decomposition_Result LU_PP_Decomposition(const Matrix &A);

// Solve Ax = b using LU decomposition with Partial Pivoting
Vector LU_PP_Solve(const Matrix &A, const Vector &b);

// LU Decomposition with Partial Pivoting, and A is stored as L + U (ignoring L's all-one diagonal)
void LU_PP_Decomposition_InPlace(Matrix &A, Matrix &P);
Matrix LU_PP_Decomposition_InPlace(Matrix &A);

// Solve Ax = b using LU decomposition with Partial Pivoting, b is then replaced by x
void LU_PP_Solve_InPlace(Matrix &A, Vector &b);
Expand Down
Loading

0 comments on commit 01f1e3e

Please sign in to comment.