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