10 #include <TDecompSVD.h>
20 const double& m2,
const double& b2,
21 const double& m3,
const double& b3,
28 std::vector<std::vector<double>> mat = {
35 const int rows = mat.size();
36 const int cols = mat.empty() ? 0 : mat[0].size();
37 TMatrixD rootMatrix(rows, cols);
38 for (
int i = 0; i < rows; ++i) {
39 for (
int j = 0; j < cols; ++j) {
40 rootMatrix(i, j) = mat[i][j];
44 TDecompSVD svd(rootMatrix);
45 TVectorD b(3); b(0) = b1; b(1) = b2; b(2) = b3;
46 Bool_t is_good_solution;
49 TVectorD solution(b.GetNrows());
52 solution = svd.Solve(b, is_good_solution);
54 if (!is_good_solution)
57 std::cerr<<
"\n\nWARNING: The system cannot be solved or is ill-conditioned.\n";
78 std::cout<<
"\nBefore Gaussian elimination\n";
85 for (
int i = 0; i < n; ++i)
89 for (
int row = i + 1; row < n; ++row)
91 if (std::fabs(mat[row][i]) > std::fabs(mat[pivotRow][i]))
94 std::swap(mat[pivotRow], mat[i]);
97 if (std::fabs(mat[i][i]) <
EPSILON)
continue;
100 for (
int col = i + 1; col <= n; ++col)
102 mat[i][col] /= mat[i][i];
107 for (
int row = i + 1; row < n; ++row)
109 double factor = mat[row][i];
110 for (
int col = i; col <= n; ++col) {
111 mat[row][col] -= factor * mat[i][col];
118 std::cout<<
"\nAfter Gaussian elimination\n";
130 for (
const auto& row : mat) {
131 for (
double val : row) {
132 if (std::fabs(val) >
EPSILON) {
143 int CheckSolutions(
const std::vector<std::vector<double>>& mat, std::vector<double>& solution)
151 std::vector<std::vector<double>> augmentedMat(mat);
152 for (
int i = 0; i < n; ++i) {
153 augmentedMat[i].pop_back();
158 if (rankA < rankAugmented)
164 solution.assign(n, 0);
165 for (
int i = n - 1; i >= 0; --i) {
166 solution[i] = mat[i][n];
168 for (
int j = i + 1; j < n; ++j) {
169 solution[i] -= mat[i][j] * solution[j];
172 if (std::fabs(mat[i][i]) >
EPSILON) {
173 solution[i] /= mat[i][i];
176 else if (std::fabs(solution[i]) >
EPSILON) {
186 const std::vector<double>& solution,
191 int n = origMat.size();
192 for (
int i = 0; i < n; ++i)
195 for (
int j = 0; j < n; ++j) {
196 sum += origMat[i][j] * solution[j];
203 std::cout<<
"For equation "<<i<<
", sum(0, n-1) = "<<sum<<
", origMat[i][n] = "<<origMat[i][n]<<std::endl;
204 if (std::fabs(sum - origMat[i][n]) >
EPSILON)
215 return std::abs(slope) > std::numeric_limits<double>::max();
221 const double& m2,
const double& b2,
222 const double& m3,
const double& b3,
231 std::vector<std::vector<double>> mat = {
238 std::vector<std::vector<double>> origMat = mat;
243 std::vector<double> solution;
250 std::cout<<
"\nThe system has a unique solution:"<<std::endl;
251 for (
double s : solution) {
252 std::cout << s <<
" ";
254 std::cout<<std::endl;
261 std::cout<<
"The solution is valid."<<std::endl;
268 std::cout<<
"\n\nERROR: The solution is invalid.\nThis should never happen! Please check logic!\n\n"<<std::endl;
273 else if (result == 0)
276 std::cout<<
"\nThe system has infinite solutions."<<std::endl;
283 std::cout<<
"\nThe system has no solution."<<std::endl;
const double EPSILON
Definition: LinearAlgebra.h:13
Definition: LinearAlgebra.h:17
int CheckSolutions(const std::vector< std::vector< double >> &mat, std::vector< double > &solution)
Definition: LinearAlgebra.h:143
int GetMatrixRank(const std::vector< std::vector< double >> &mat)
Definition: LinearAlgebra.h:126
void GaussianElimination(std::vector< std::vector< double >> &mat, bool debugMode=false)
Definition: LinearAlgebra.h:69
std::vector< double > SolveSystemOfThreeEquations(const double &m1, const double &b1, const double &m2, const double &b2, const double &m3, const double &b3, bool debugMode)
Definition: LinearAlgebra.h:220
std::vector< double > ROOTEquationSolver(const double &m1, const double &b1, const double &m2, const double &b2, const double &m3, const double &b3, bool debugMode)
Definition: LinearAlgebra.h:19
bool ValidateSolution(const std::vector< std::vector< double >> &origMat, const std::vector< double > &solution, bool debugMode)
Definition: LinearAlgebra.h:185
bool IsSlopeUndefined(double slope)
Definition: LinearAlgebra.h:212
std::vector< double > TVectorDToStdVector(const TVectorD &vec)
Definition: Utilities.h:164
void printVectorOfVectors(std::vector< std::vector< double >> const &mat)
Definition: Utilities.h:67