5
5
#include < vector>
6
6
7
7
8
- int gaussianElimination (std::vector< std::vector<double > > &eqns, int varc) {
8
+ void gaussianElimination (std::vector< std::vector<double > > &eqns, int varc) {
9
9
// 'eqns' is the matrix, 'varc' is no. of vars
10
10
int err = 0 ; // marker for if matrix is singular
11
+
11
12
for (int i = 0 ; i < varc - 1 ; i++) {
12
- double pivot = fabs (eqns[i][i]);
13
- int newRow = i;
13
+ int pivot = i;
14
14
15
- for (int j = i + 1 ; j < varc; j++) {
16
- if (fabs (eqns[j][i]) > pivot) {
17
- pivot = fabs (eqns[j][i]);
18
- newRow = j;
19
- }
20
- }
15
+ for (int j = i + 1 ; j < varc; j++)
16
+ if (fabs (eqns[j][i]) > fabs (eqns[pivot][i]))
17
+ pivot = j;
21
18
22
- if (pivot == 0.0 ) {
19
+ if (eqns[ pivot][i] == 0.0 ) {
23
20
err = 1 ; // Marking that matrix is singular
24
- continue ;
21
+ continue ; // But continuing to simplify the matrix as much as possible
25
22
}
26
23
27
- if (newRow != i ) // Swapping the rows if new row with higher pivot is found
28
- std::swap (eqns[newRow ], eqns[i]); // C++ swap function
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
29
26
30
27
for (int j = i + 1 ; j < varc; j++) {
31
- if (eqns[j][i]) {
32
- double mult = eqns[j][i] / eqns[i][i];
33
- eqns[j][i] = 0.0 ;
34
- for (int k = i + 1 ; k < (varc + 1 ); k++) // k doesn't start at 0, since
35
- eqns[j][k] -= mult * eqns[i][k]; // values before from 0 to i
36
- } // are already 0
37
- }
38
- }
28
+ double scale = eqns[j][i] / eqns[i][i];
39
29
40
- if (eqns[varc - 1 ][varc - 1 ] == 0 || err) {
41
- if (eqns[varc - 1 ][varc] == 0 || err)
42
- return 1 ; // Error code: Singular matrix
43
- else
44
- return 2 ; // Error code: No solutions
45
- // Cannot solve since final equation is '0*xn = c'
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
33
+ eqns[j][i] = 0.0 ;
34
+ }
46
35
}
47
-
48
- return 0 ; // Successful
49
36
}
50
37
51
38
52
39
void gaussJordan (std::vector< std::vector<double > > &eqns, int varc) {
53
40
// 'eqns' is the (Row-echelon) matrix, 'varc' is no. of vars
54
41
for (int i = varc - 1 ; i >= 0 ; i--) {
55
- eqns[i][varc] /= eqns[i][i];
56
- eqns[i][i] = 1 ; // We know that the only entry in this row is 1
57
-
58
- // subtracting rows from below
59
- for (int j = i - 1 ; j >= 0 ; j--) {
60
- eqns[j][varc] -= eqns[j][i] * eqns[i][varc];
61
- eqns[j][i] = 0 ; // We also set all the other values in row to 0 directly
42
+ 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
45
+
46
+ // subtracting rows from below
47
+ 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
+ }
62
51
}
63
52
}
64
53
}
@@ -69,30 +58,22 @@ std::vector<double> backSubs(std::vector<std::vector<double>> &eqns, int varc)
69
58
// 'eqns' is matrix, 'varc' is no. of variables
70
59
std::vector<double > ans (varc);
71
60
for (int i = varc - 1 ; i >= 0 ; i--) {
72
- double sum = 0 ;
61
+ double sum = 0.0 ;
62
+
73
63
for (int j = i + 1 ; j < varc; j++)
74
64
sum += eqns[i][j] * ans[j];
75
65
76
- ans[i] = (eqns[i][varc] - sum) / eqns[i][i];
66
+ if (eqns[i][i] != 0 )
67
+ ans[i] = (eqns[i][varc] - sum) / eqns[i][i];
68
+ else
69
+ return std::vector<double >(0 );
77
70
}
78
71
return ans;
79
72
}
80
73
81
74
82
75
std::vector<double > solveByGaussJordan (std::vector<std::vector<double >> eqns, int varc) {
83
- int status = gaussianElimination (eqns, varc);
84
- switch (status) {
85
- case 0 :
86
- break ;
87
-
88
- case 1 :
89
- std::cout << " Singular matrix" << std::endl;
90
- return std::vector<double >(0 ); // return empty vector in case of no solutions
91
-
92
- case 2 :
93
- std::cout << " No solutions" << std::endl;
94
- return std::vector<double >(0 ); // return empty vectors in case of no solutions
95
- }
76
+ gaussianElimination (eqns, varc);
96
77
97
78
gaussJordan (eqns, varc);
98
79
@@ -104,19 +85,7 @@ std::vector<double> solveByGaussJordan(std::vector<std::vector<double>> eqns, in
104
85
105
86
106
87
std::vector<double > solveByBacksubs (std::vector<std::vector<double >> eqns, int varc) {
107
- int status = gaussianElimination (eqns, varc);
108
- switch (status) {
109
- case 0 :
110
- break ;
111
-
112
- case 1 :
113
- std::cout << " Singular matrix" << std::endl;
114
- return std::vector<double >(0 ); // return empty vectors in case of no solutions
115
-
116
- case 2 :
117
- std::cout << " No solutions" << std::endl;
118
- return std::vector<double >(0 ); // return empty vectors in case of no solutions
119
- }
88
+ gaussianElimination (eqns, varc);
120
89
121
90
std::vector<double > ans = backSubs (eqns, varc);
122
91
return ans;
@@ -134,23 +103,15 @@ int main() {
134
103
135
104
ans = solveByGaussJordan (equations, varc);
136
105
137
- if (ans.empty ()) { // If ans is empty, then no unique solutions exist
138
- std::cout << " No solution" << std::endl;
139
- } else {
140
- std::cout << " The solution is (by Gauss Jordan)," << std::endl
141
- << " x == " << ans[0 ] << std::endl
142
- << " y == " << ans[1 ] << std::endl
143
- << " z == " << ans[2 ] << std::endl;
144
- }
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;
145
110
146
111
ans = solveByBacksubs (equations, varc);
147
112
148
- if (ans.empty ()) { // If ans is empty, then no unique solutions exist
149
- std::cout << " No solution" << std::endl;
150
- } else {
151
- std::cout << " The solution is (by Backsubstitution)," << std::endl
152
- << " x == " << ans[0 ] << std::endl
153
- << " y == " << ans[1 ] << std::endl
154
- << " z == " << ans[2 ] << std::endl;
155
- }
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;
156
117
}
0 commit comments