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

Commit

Permalink
feat: using template for IterationMethodOutput
Browse files Browse the repository at this point in the history
adding JacobiMethod/ files
  • Loading branch information
tiankaima committed Dec 18, 2023
1 parent cffc78b commit ee5cebe
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 40 deletions.
2 changes: 2 additions & 0 deletions CustomMath_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ set(SOURCE_FILES
Matrix/MatrixGenerations.cpp
Matrix/MatrixSubOperations.cpp
Matrix/MatrixOperations.cpp
JacobiMethod/JacobiMethod.cpp
JacobiMethod/JacobiMethod.h
)

add_library(CustomMath_lib STATIC ${SOURCE_FILES} ${HEADER_FILES})
8 changes: 4 additions & 4 deletions CustomMath_lib/IterationMethod/IterationMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define ITERATION_METHOD_RETURN_DURATION std::chrono::microseconds(0)
#endif

IterationMethodOutput JacobiIteration(const IterationMethodInput &input) {
VIterationMethodOutput JacobiIteration(const IterationMethodInput &input) {
auto A = input.A;
auto x = input.x_default;
auto D = A.sub_diagonal();
Expand Down Expand Up @@ -48,7 +48,7 @@ IterationMethodOutput JacobiIteration(const IterationMethodInput &input) {
throw std::invalid_argument("JacobiIteration: iteration count exceeds ITERATION_METHOD_MAX_ITERATION");
}

IterationMethodOutput GaussSeidelIteration(const IterationMethodInput &input) {
VIterationMethodOutput GaussSeidelIteration(const IterationMethodInput &input) {
auto A = input.A;
auto x = input.x_default;
auto D = A.sub_diagonal();
Expand Down Expand Up @@ -77,7 +77,7 @@ IterationMethodOutput GaussSeidelIteration(const IterationMethodInput &input) {
throw std::invalid_argument("GaussSeidelIteration: iteration count exceeds ITERATION_METHOD_MAX_ITERATION");
}

IterationMethodOutput SORIteration(const IterationMethodInput &input, lld omega) {
VIterationMethodOutput SORIteration(const IterationMethodInput &input, lld omega) {
auto A = input.A;
auto x = input.x_default;
auto D = A.sub_diagonal();
Expand Down Expand Up @@ -107,7 +107,7 @@ IterationMethodOutput SORIteration(const IterationMethodInput &input, lld omega)
throw std::invalid_argument("SORIteration: iteration count exceeds ITERATION_METHOD_MAX_ITERATION");
}

IterationMethodOutput ConjugateGradientMethod(const IterationMethodInput &input) {
VIterationMethodOutput ConjugateGradientMethod(const IterationMethodInput &input) {
auto A = input.A;
auto x = input.x_default;
auto r = input.b - A * x;
Expand Down
20 changes: 8 additions & 12 deletions CustomMath_lib/IterationMethod/IterationMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,24 @@
#define ITERATION_METHOD_MAX_ITERATION 100000

typedef struct {
Matrix A;
Vector b;
Vector x_default;
const Matrix &A;
const Vector &b;
const Vector &x_default;
lld precision_requirement;
} IterationMethodInput;

typedef struct {
Vector x;
int iteration_count;
std::chrono::microseconds time_cost;
} IterationMethodOutput;
using VIterationMethodOutput = IterationMethodOutput<Vector>;

/// Solve Ax = b using Jacobi iteration method
IterationMethodOutput JacobiIteration(const IterationMethodInput &input);
VIterationMethodOutput JacobiIteration(const IterationMethodInput &input);

/// Solve Ax = b using Gauss-Seidel iteration method
IterationMethodOutput GaussSeidelIteration(const IterationMethodInput &input);
VIterationMethodOutput GaussSeidelIteration(const IterationMethodInput &input);

/// Solve Ax = b using SOR iteration method
IterationMethodOutput SORIteration(const IterationMethodInput &input, lld omega);
VIterationMethodOutput SORIteration(const IterationMethodInput &input, lld omega);

/// Solve Ax = b using Conjugate Gradient iteration method
IterationMethodOutput ConjugateGradientMethod(const IterationMethodInput &input);
VIterationMethodOutput ConjugateGradientMethod(const IterationMethodInput &input);

#endif //NUMERICAL_ALGEBRA_ITERATIONMETHOD_H
5 changes: 5 additions & 0 deletions CustomMath_lib/JacobiMethod/JacobiMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Created by TianKai Ma on 2023/12/18.
//

#include "JacobiMethod.h"
8 changes: 8 additions & 0 deletions CustomMath_lib/JacobiMethod/JacobiMethod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Created by TianKai Ma on 2023/12/18.
//

#ifndef NUMERICAL_ALGEBRA_JACOBIMETHOD_H
#define NUMERICAL_ALGEBRA_JACOBIMETHOD_H

#endif //NUMERICAL_ALGEBRA_JACOBIMETHOD_H
6 changes: 1 addition & 5 deletions CustomMath_lib/PowerIteration/PowerIteration.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ typedef struct {
int iteration_times;
} PowerIterationInput;

typedef struct {
lld x;
int iteration_times;
std::chrono::microseconds time_cost;
} PowerIterationOutput;
using PowerIterationOutput = IterationMethodOutput<lld>;

/// \brief: Find the max eigenvalue for a matrix using power iteration method
PowerIterationOutput PowerIteration(const PowerIterationInput &input);
Expand Down
6 changes: 3 additions & 3 deletions CustomMath_lib/QRMethod/QRMethod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define ITERATION_METHOD_RETURN_DURATION std::chrono::microseconds(0)
#endif

QRMethodOutput<Matrix> QRMethod(const Matrix &matrix) {
MIterationMethodOutput QRMethod(const Matrix &matrix) {
Matrix H = matrix;
Matrix Q;
auto P = Matrix::identity(H.rows);
Expand Down Expand Up @@ -98,7 +98,7 @@ QRMethodOutput<Matrix> QRMethod(const Matrix &matrix) {
throw std::runtime_error("QRMethod: iteration times exceed maximum");
}

QRMethodOutput<Vector> AllRootsForPolynomial(const Vector &coefficients) {
VIterationMethodOutput AllRootsForPolynomial(const Vector &coefficients) {
auto n = coefficients.size;
auto A = Matrix(n, n);

Expand All @@ -119,7 +119,7 @@ QRMethodOutput<Vector> AllRootsForPolynomial(const Vector &coefficients) {

return { //
result, //
r.iteration_times, //
r.iteration_count, //
r.time_cost //
};
}
Expand Down
13 changes: 4 additions & 9 deletions CustomMath_lib/QRMethod/QRMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@ typedef struct {
lld complex;
} llc;

template<typename T>
struct QRMethodOutput {
T result;
int iteration_times;
std::chrono::microseconds time_cost;
};
using MIterationMethodOutput = IterationMethodOutput<Matrix>;
using VIterationMethodOutput = IterationMethodOutput<Vector>;

MIterationMethodOutput QRMethod(const Matrix &matrix);

QRMethodOutput<Matrix> QRMethod(const Matrix &matrix);

QRMethodOutput<Vector> AllRootsForPolynomial(const Vector &coefficients);
VIterationMethodOutput AllRootsForPolynomial(const Vector &coefficients);

std::vector<llc> AllEigenValues(const Matrix &R);

Expand Down
7 changes: 7 additions & 0 deletions CustomMath_lib/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@

bool cmp(lld a, lld b);

template<typename T>
struct IterationMethodOutput {
T result;
int iteration_count;
std::chrono::microseconds time_cost;
};

#endif //NUMERICAL_ALGEBRA_BASE_H
14 changes: 7 additions & 7 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ void par_1_each(Vector &a) {

std::cout << "Polynomial is ";
a.print();
std::cout << "Max root for polynomial is " << std::setprecision(10) << max_root.x << std::endl;
std::cout << "Iteration times is " << max_root.iteration_times << std::endl;
std::cout << "Max root for polynomial is " << std::setprecision(10) << max_root.result << std::endl;
std::cout << "Iteration times is " << max_root.iteration_count << std::endl;
std::cout << "Time cost is " << max_root.time_cost.count() << " microseconds" << std::endl;
}

Expand Down Expand Up @@ -49,11 +49,11 @@ void par_2_2() {
coefficients.array[41 - 3 + 1] = 1;
coefficients.array[41 - 1] = 1;

auto result = AllRootsForPolynomial(coefficients);
auto r = AllRootsForPolynomial(coefficients);
std::cout << "Roots for polynomial are:" << std::endl;
result.result.print();
std::cout << "Iteration times is " << result.iteration_times << std::endl;
std::cout << "Time cost is " << result.time_cost.count() << " microseconds" << std::endl;
r.result.print();
std::cout << "Iteration times is " << r.iteration_count << std::endl;
std::cout << "Time cost is " << r.time_cost.count() << " microseconds" << std::endl;
}

void par_2_3() {
Expand Down Expand Up @@ -84,7 +84,7 @@ void par_2_3() {
auto k = AllEigenValues(h.result);
std::cout << "Eigen values are:" << std::endl;
print_llc(k);
std::cout << "Iteration times is " << h.iteration_times << std::endl;
std::cout << "Iteration times is " << h.iteration_count << std::endl;
std::cout << "Time cost is " << h.time_cost.count() << " microseconds" << std::endl;

std::cout << std::endl;
Expand Down

0 comments on commit ee5cebe

Please sign in to comment.