diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b32244..eb75cdb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,4 +13,6 @@ add_executable(${PROJECT_NAME} src/main.cpp src/main.h src/data/dateMoney.cpp src/data/dateMoney.h src/optHandlers/optHandler.cpp src/optHandlers/optHandler.h src/optHandlers/accountOptHandler.cpp src/optHandlers/accountOptHandler.h - src/optHandlers/mainOptHandler.cpp src/optHandlers/mainOptHandler.h) \ No newline at end of file + src/optHandlers/mainOptHandler.cpp src/optHandlers/mainOptHandler.h + utilities/math.cpp utilities/math.h + utilities/polynomialFunction.cpp utilities/polynomialFunction.h) diff --git a/src/data/accountData.cpp b/src/data/accountData.cpp index 1a58fcd..bc734a8 100644 --- a/src/data/accountData.cpp +++ b/src/data/accountData.cpp @@ -96,6 +96,4 @@ Account *AccountData::getAccount() { return &account; } -AccountData::AccountData() : Data("/dev/null") { - -} +AccountData::AccountData() : Data("/dev/null") {} diff --git a/src/main.cpp b/src/main.cpp index 86ad8eb..7dfcf82 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "data/accountData.h" #include "optHandlers/accountOptHandler.h" #include "optHandlers/mainOptHandler.h" +#include "../utilities/math.h" #include #include diff --git a/utilities/math.cpp b/utilities/math.cpp new file mode 100644 index 0000000..4d43269 --- /dev/null +++ b/utilities/math.cpp @@ -0,0 +1,97 @@ +// +// Created by quentin on 9/13/22. +// + +#include "math.h" + +//Polynomial Fit +#include +#include + +using namespace Budget::Utilities; + +PolynomialFunction Math::polynomialFit(int degree, int numberOfPoints, const double x[], const double y[]) { + //Array that will store the values of sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2numberOfPoints) + double X[2 * degree + 1]; + for (int i = 0; i < 2 * degree + 1; i++) { + X[i] = 0; + for (int j = 0; j < numberOfPoints; j++) { + //consecutive positions of the array will store numberOfPoints,sigma(xi),sigma(xi^2),sigma(xi^3)....sigma(xi^2n) + X[i] = X[i] + pow(x[j], i); + } + } + //B is the Normal matrix(augmented) that will store the equations, 'a' is for value of the final coefficients + double B[degree + 1][degree + 2], a[degree + 1]; + for (int i = 0; i <= degree; i++) { + for (int j = 0; j <= degree; j++) { + //Build the Normal matrix by storing the corresponding coefficients at the right positions except the last column of the matrix + B[i][j] = X[i + j]; + } + } + + //Array to store the values of sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^degree*yi) + double Y[degree + 1]; + for (int i = 0; i < degree + 1; i++) { + Y[i] = 0; + for (int j = 0; j < numberOfPoints; j++) { + //consecutive positions will store sigma(yi),sigma(xi*yi),sigma(xi^2*yi)...sigma(xi^degree*yi) + Y[i] = Y[i] + pow(x[j], i) * y[j]; + } + } + for (int i = 0; i <= degree; i++) { + //load the values of Y as the last column of B(Normal Matrix but augmented) + B[i][degree + 1] = Y[i]; + } + + //degree is made degree+1 because the Gaussian Elimination part below was for degree equations, but here degree is the degree of polynomial and for degree degree we get degree+1 equations + degree = degree + 1; + //From now Gaussian Elimination starts(can be ignored) to solve the set of linear equations (Pivotisation) + for (int i = 0; i < degree; i++) { + for (int k = i + 1; k < degree; k++) { + if (B[i][i] < B[k][i]) { + for (int j = 0; j <= degree; j++) { + double temp = B[i][j]; + B[i][j] = B[k][j]; + B[k][j] = temp; + } + } + } + } + + //loop to perform the gauss elimination + for (int i = 0; i < degree - 1; i++) { + for (int k = i + 1; k < degree; k++) { + double t = B[k][i] / B[i][i]; + for (int j = 0; j <= degree; j++) { + //make the elements below the pivot elements equal to zero or elimnate the variables + B[k][j] = B[k][j] - t * B[i][j]; + } + } + } + + //back-substitution + for (int i = degree - 1; i >= 0; i--) { + //x is an array whose values correspond to the values of x,y,z.. + //make the variable to be calculated equal to the rhs of the last equation + a[i] = B[i][degree]; + for (int j = 0; j < degree; j++) { + //then subtract all the lhs values except the coefficient of the variable whose value is being calculated + if (j != i) { + a[i] = a[i] - B[i][j] * a[j]; + } + } + //now finally divide the rhs by the coefficient of the variable to be calculated + a[i] = a[i] / B[i][i]; + } + std::cout << "\nThe values of the coefficients are as follows:\n"; + for (int i = 0; i < degree; i++) + std::cout << "x^" << i << "=" << a[i] << std::endl; // Print the values of x^0,x^1,x^2,x^3,.... + std::cout << "\nHence the fitted Polynomial is given by:\ny="; + for (int i = 0; i < degree; i++) + std::cout << " + (" << a[i] << ")" << "x^" << i; + std::cout << "\n"; + + std::vector va(a, a + degree); + return PolynomialFunction(va); +} + diff --git a/utilities/math.h b/utilities/math.h new file mode 100644 index 0000000..8a2d164 --- /dev/null +++ b/utilities/math.h @@ -0,0 +1,19 @@ +// +// Created by quentin on 9/13/22. +// + +#ifndef BUDGET_MATH_H +#define BUDGET_MATH_H + + +#include "polynomialFunction.h" + +namespace Budget::Utilities { + class Math { + public: + static PolynomialFunction polynomialFit(int degree, int numberOfPoints, const double x[], const double y[]); + }; +} + + +#endif //BUDGET_MATH_H diff --git a/utilities/polynomialFunction.cpp b/utilities/polynomialFunction.cpp new file mode 100644 index 0000000..5247f62 --- /dev/null +++ b/utilities/polynomialFunction.cpp @@ -0,0 +1,21 @@ +// +// Created by quentin on 9/13/22. +// + +#include "polynomialFunction.h" + +#include +#include + +using namespace Budget::Utilities; + +PolynomialFunction::PolynomialFunction(std::vector a) : a(std::move(a)) {} + +double PolynomialFunction::get(double x) { + double ret = 0; + for (int i = 0; i < a.size(); ++i) { + ret += a[i] * std::pow(x, i); + } + + return ret; +} diff --git a/utilities/polynomialFunction.h b/utilities/polynomialFunction.h new file mode 100644 index 0000000..116d046 --- /dev/null +++ b/utilities/polynomialFunction.h @@ -0,0 +1,25 @@ +// +// Created by quentin on 9/13/22. +// + +#ifndef BUDGET_POLYNOMIALFUNCTION_H +#define BUDGET_POLYNOMIALFUNCTION_H + + +#include + +namespace Budget::Utilities { + class PolynomialFunction { + private: + public: + explicit PolynomialFunction(std::vector a); + + double get(double x); + + private: + std::vector a; + }; +} + + +#endif //BUDGET_POLYNOMIALFUNCTION_H