6
6
#include < iomanip>
7
7
8
8
void gaussian_elimination (std::vector<double >& a, int cols) {
9
- assert (a.size () % cols == 0 );
10
9
int rows = a.size () / cols;
11
10
12
11
int row = 0 ;
@@ -28,8 +27,10 @@ void gaussian_elimination(std::vector<double>& a, int cols) {
28
27
}
29
28
30
29
// Step 2: swap row with highest value for that column to the top
31
- for (int c = 0 ; c < cols; ++c)
32
- std::swap (a[c + row * cols], a[c + max_index * cols]);
30
+ if (row != max_index) {
31
+ for (int c = 0 ; c < cols; ++c)
32
+ std::swap (a[c + row * cols], a[c + max_index * cols]);
33
+ }
33
34
34
35
// Loop for all remaining rows
35
36
for (int i = row + 1 ; i < rows; ++i) {
@@ -52,7 +53,6 @@ void gaussian_elimination(std::vector<double>& a, int cols) {
52
53
}
53
54
54
55
std::vector<double > back_substitution (const std::vector<double >& a, int cols) {
55
- assert (a.size () % cols == 0 );
56
56
int rows = a.size () / cols;
57
57
58
58
// Creating the solution Vector
@@ -71,7 +71,6 @@ std::vector<double> back_substitution(const std::vector<double>& a, int cols) {
71
71
}
72
72
73
73
void gauss_jordan_elimination (std::vector<double >& a, int cols) {
74
- assert (a.size () % cols == 0 );
75
74
// After this, we know what row to start on (r-1)
76
75
// to go back through the matrix
77
76
int row = 0 ;
@@ -92,7 +91,6 @@ void gauss_jordan_elimination(std::vector<double>& a, int cols) {
92
91
}
93
92
94
93
void print_matrix (const std::vector<double >& a, int cols) {
95
- assert (a.size () % cols == 0 );
96
94
int rows = a.size () / cols;
97
95
for (int i = 0 ; i < rows; ++i) {
98
96
std::cout << " [" ;
0 commit comments