diff --git a/CustomMath_lib/CMakeLists.txt b/CustomMath_lib/CMakeLists.txt index f9fa3b5..38df055 100644 --- a/CustomMath_lib/CMakeLists.txt +++ b/CustomMath_lib/CMakeLists.txt @@ -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}) \ No newline at end of file diff --git a/CustomMath_lib/IterationMethod/IterationMethod.cpp b/CustomMath_lib/IterationMethod/IterationMethod.cpp index 00dff8b..b8fcd4e 100644 --- a/CustomMath_lib/IterationMethod/IterationMethod.cpp +++ b/CustomMath_lib/IterationMethod/IterationMethod.cpp @@ -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(); @@ -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(); @@ -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(); @@ -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; diff --git a/CustomMath_lib/IterationMethod/IterationMethod.h b/CustomMath_lib/IterationMethod/IterationMethod.h index ed416a4..42b25c2 100644 --- a/CustomMath_lib/IterationMethod/IterationMethod.h +++ b/CustomMath_lib/IterationMethod/IterationMethod.h @@ -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; /// 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 diff --git a/CustomMath_lib/JacobiMethod/JacobiMethod.cpp b/CustomMath_lib/JacobiMethod/JacobiMethod.cpp new file mode 100644 index 0000000..24c1c08 --- /dev/null +++ b/CustomMath_lib/JacobiMethod/JacobiMethod.cpp @@ -0,0 +1,5 @@ +// +// Created by TianKai Ma on 2023/12/18. +// + +#include "JacobiMethod.h" diff --git a/CustomMath_lib/JacobiMethod/JacobiMethod.h b/CustomMath_lib/JacobiMethod/JacobiMethod.h new file mode 100644 index 0000000..c158586 --- /dev/null +++ b/CustomMath_lib/JacobiMethod/JacobiMethod.h @@ -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 diff --git a/CustomMath_lib/PowerIteration/PowerIteration.h b/CustomMath_lib/PowerIteration/PowerIteration.h index c70600f..7c42093 100644 --- a/CustomMath_lib/PowerIteration/PowerIteration.h +++ b/CustomMath_lib/PowerIteration/PowerIteration.h @@ -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; /// \brief: Find the max eigenvalue for a matrix using power iteration method PowerIterationOutput PowerIteration(const PowerIterationInput &input); diff --git a/CustomMath_lib/QRMethod/QRMethod.cpp b/CustomMath_lib/QRMethod/QRMethod.cpp index 68048d3..a604f9a 100644 --- a/CustomMath_lib/QRMethod/QRMethod.cpp +++ b/CustomMath_lib/QRMethod/QRMethod.cpp @@ -18,7 +18,7 @@ #define ITERATION_METHOD_RETURN_DURATION std::chrono::microseconds(0) #endif -QRMethodOutput QRMethod(const Matrix &matrix) { +MIterationMethodOutput QRMethod(const Matrix &matrix) { Matrix H = matrix; Matrix Q; auto P = Matrix::identity(H.rows); @@ -98,7 +98,7 @@ QRMethodOutput QRMethod(const Matrix &matrix) { throw std::runtime_error("QRMethod: iteration times exceed maximum"); } -QRMethodOutput AllRootsForPolynomial(const Vector &coefficients) { +VIterationMethodOutput AllRootsForPolynomial(const Vector &coefficients) { auto n = coefficients.size; auto A = Matrix(n, n); @@ -119,7 +119,7 @@ QRMethodOutput AllRootsForPolynomial(const Vector &coefficients) { return { // result, // - r.iteration_times, // + r.iteration_count, // r.time_cost // }; } diff --git a/CustomMath_lib/QRMethod/QRMethod.h b/CustomMath_lib/QRMethod/QRMethod.h index b7634a4..f7e6c0e 100644 --- a/CustomMath_lib/QRMethod/QRMethod.h +++ b/CustomMath_lib/QRMethod/QRMethod.h @@ -13,17 +13,12 @@ typedef struct { lld complex; } llc; -template -struct QRMethodOutput { - T result; - int iteration_times; - std::chrono::microseconds time_cost; -}; +using MIterationMethodOutput = IterationMethodOutput; +using VIterationMethodOutput = IterationMethodOutput; +MIterationMethodOutput QRMethod(const Matrix &matrix); -QRMethodOutput QRMethod(const Matrix &matrix); - -QRMethodOutput AllRootsForPolynomial(const Vector &coefficients); +VIterationMethodOutput AllRootsForPolynomial(const Vector &coefficients); std::vector AllEigenValues(const Matrix &R); diff --git a/CustomMath_lib/base.h b/CustomMath_lib/base.h index f3cefbf..5933f39 100644 --- a/CustomMath_lib/base.h +++ b/CustomMath_lib/base.h @@ -21,4 +21,11 @@ bool cmp(lld a, lld b); +template +struct IterationMethodOutput { + T result; + int iteration_count; + std::chrono::microseconds time_cost; +}; + #endif //NUMERICAL_ALGEBRA_BASE_H diff --git a/main.cpp b/main.cpp index 30c054e..4b542ca 100644 --- a/main.cpp +++ b/main.cpp @@ -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; } @@ -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() { @@ -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;