|
| 1 | +#include <algorithm> |
| 2 | +#include <cmath> |
| 3 | +#include <iomanip> |
| 4 | +#include <iostream> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | + |
| 8 | +void gaussianElimination(std::vector<std::vector<double> > &eqns) { |
| 9 | + // 'eqns' is the matrix, 'rows' is no. of vars |
| 10 | + int rows = eqns.size(), cols = eqns[0].size(); |
| 11 | + |
| 12 | + for (int i = 0; i < rows - 1; i++) { |
| 13 | + int pivot = i; |
| 14 | + |
| 15 | + for (int j = i + 1; j < rows; j++) { |
| 16 | + if (fabs(eqns[j][i]) > fabs(eqns[pivot][i])) pivot = j; |
| 17 | + } |
| 18 | + |
| 19 | + if (eqns[pivot][i] == 0.0) |
| 20 | + continue; // But continuing to simplify the matrix as much as possible |
| 21 | + |
| 22 | + if (i != pivot) // Swapping the rows if new row with higher maxVals is found |
| 23 | + std::swap(eqns[pivot], eqns[i]); // C++ swap function |
| 24 | + |
| 25 | + for (int j = i + 1; j < rows; j++) { |
| 26 | + double scale = eqns[j][i] / eqns[i][i]; |
| 27 | + |
| 28 | + for (int k = i + 1; k < cols; k++) // k doesn't start at 0, since |
| 29 | + eqns[j][k] -= scale * eqns[i][k]; // values before from 0 to i |
| 30 | + // are already 0 |
| 31 | + eqns[j][i] = 0.0; |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void gaussJordan(std::vector<std::vector<double> > &eqns) { |
| 37 | + // 'eqns' is the (Row-echelon) matrix, 'rows' is no. of vars |
| 38 | + int rows = eqns.size(); |
| 39 | + |
| 40 | + for (int i = rows - 1; i >= 0; i--) { |
| 41 | + |
| 42 | + if (eqns[i][i] != 0) { |
| 43 | + |
| 44 | + eqns[i][rows] /= eqns[i][i]; |
| 45 | + eqns[i][i] = 1; // We know that the only entry in this row is 1 |
| 46 | + |
| 47 | + // subtracting rows from below |
| 48 | + for (int j = i - 1; j >= 0; j--) { |
| 49 | + eqns[j][rows] -= eqns[j][i] * eqns[i][rows]; |
| 50 | + eqns[j][i] = 0; // We also set all the other values in row to 0 directly |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +std::vector<double> backSubs(const std::vector<std::vector<double> > &eqns) { |
| 57 | + // 'eqns' is matrix, 'rows' is no. of variables |
| 58 | + int rows = eqns.size(); |
| 59 | + |
| 60 | + std::vector<double> ans(rows); |
| 61 | + for (int i = rows - 1; i >= 0; i--) { |
| 62 | + double sum = 0.0; |
| 63 | + |
| 64 | + for (int j = i + 1; j < rows; j++) sum += eqns[i][j] * ans[j]; |
| 65 | + |
| 66 | + if (eqns[i][i] != 0) |
| 67 | + ans[i] = (eqns[i][rows] - sum) / eqns[i][i]; |
| 68 | + else |
| 69 | + return std::vector<double>(0); |
| 70 | + } |
| 71 | + return ans; |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +void printMatrix(const std::vector<std::vector<double> > &matrix) { |
| 76 | + for (int row = 0; row < matrix.size(); row++) { |
| 77 | + std::cout << "["; |
| 78 | + |
| 79 | + for (int col = 0; col < matrix[row].size() - 1; col++) |
| 80 | + std::cout << std::setw(8) << std::fixed << std::setprecision(3) |
| 81 | + << matrix[row][col]; |
| 82 | + |
| 83 | + std::cout << " |" << std::setw(8) << std::fixed << std::setprecision(3) |
| 84 | + << matrix[row].back() << " ]" << std::endl; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +int main() { |
| 90 | + std::vector<std::vector<double> > equations{ |
| 91 | + {2, 3, 4, 6}, |
| 92 | + {1, 2, 3, 4}, |
| 93 | + {3, -4, 0, 10}}; |
| 94 | + |
| 95 | + std::cout << "Initial matrix:" << std::endl; |
| 96 | + printMatrix(equations); |
| 97 | + std::cout << std::endl; |
| 98 | + |
| 99 | + gaussianElimination(equations); |
| 100 | + std::cout << "Matrix after gaussian elimination:" << std::endl; |
| 101 | + printMatrix(equations); |
| 102 | + std::cout << std::endl; |
| 103 | + |
| 104 | + std::vector<double> ans = backSubs(equations); |
| 105 | + std::cout << "Solution from backsubstitution" << std::endl; |
| 106 | + std::cout << "x = " << ans[0] << ", y = " << ans[1] << ", z = " << ans[2] |
| 107 | + << std::endl |
| 108 | + << std::endl; |
| 109 | + |
| 110 | + gaussJordan(equations); |
| 111 | + std::cout << "Matrix after Gauss Jordan:" << std::endl; |
| 112 | + printMatrix(equations); |
| 113 | + std::cout << std::endl; |
| 114 | +} |
0 commit comments