Skip to content

Commit fbe7bbc

Browse files
committed
Made changes as requested
1 parent 64426a1 commit fbe7bbc

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed
Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,115 @@
11
#include <algorithm>
22
#include <cmath>
3+
#include <iomanip>
34
#include <iostream>
4-
#include <stdexcept>
55
#include <vector>
66

77

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();
1111

12-
for (int i = 0; i < varc - 1; i++) {
12+
for (int i = 0; i < rows - 1; i++) {
1313
int pivot = i;
1414

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;
2217
}
2318

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
2625

27-
for (int j = i + 1; j < varc; j++) {
26+
for (int j = i + 1; j < rows; j++) {
2827
double scale = eqns[j][i] / eqns[i][i];
2928

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
3332
eqns[j][i] = 0.0;
3433
}
3534
}
3635
}
3736

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--) {
3842

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--) {
4243
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
4547

4648
// subtracting rows from below
4749
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
5052
}
5153
}
5254
}
5355
}
5456

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();
5560

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--) {
6163
double sum = 0.0;
6264

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];
6566

6667
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];
6869
else
6970
return std::vector<double>(0);
7071
}
7172
return ans;
7273
}
7374

7475

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 << "[";
8579

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];
8683

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+
}
9287
}
9388

9489

9590
int main() {
96-
int varc = 3;
97-
std::vector< std::vector<double> > equations{
91+
std::vector<std::vector<double> > equations{
9892
{2, 3, 4, 6},
9993
{1, 2, 3, 4},
10094
{3, -4, 0, 10}};
10195

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;
117115
}

0 commit comments

Comments
 (0)