Skip to content

Commit dc87fe0

Browse files
committed
Address code review comments
- remove asserts - don't swap if it is a same row
1 parent e843418 commit dc87fe0

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

contents/gaussian_elimination/code/c++/gaussian_elimination.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <iomanip>
77

88
void gaussian_elimination(std::vector<double>& a, int cols) {
9-
assert(a.size() % cols == 0);
109
int rows = a.size() / cols;
1110

1211
int row = 0;
@@ -28,8 +27,10 @@ void gaussian_elimination(std::vector<double>& a, int cols) {
2827
}
2928

3029
// 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+
}
3334

3435
// Loop for all remaining rows
3536
for (int i = row + 1; i < rows; ++i) {
@@ -52,7 +53,6 @@ void gaussian_elimination(std::vector<double>& a, int cols) {
5253
}
5354

5455
std::vector<double> back_substitution(const std::vector<double>& a, int cols) {
55-
assert(a.size() % cols == 0);
5656
int rows = a.size() / cols;
5757

5858
// Creating the solution Vector
@@ -71,7 +71,6 @@ std::vector<double> back_substitution(const std::vector<double>& a, int cols) {
7171
}
7272

7373
void gauss_jordan_elimination(std::vector<double>& a, int cols) {
74-
assert(a.size() % cols == 0);
7574
// After this, we know what row to start on (r-1)
7675
// to go back through the matrix
7776
int row = 0;
@@ -92,7 +91,6 @@ void gauss_jordan_elimination(std::vector<double>& a, int cols) {
9291
}
9392

9493
void print_matrix(const std::vector<double>& a, int cols) {
95-
assert(a.size() % cols == 0);
9694
int rows = a.size() / cols;
9795
for (int i = 0; i < rows; ++i) {
9896
std::cout << "[";

contents/gaussian_elimination/gaussian_elimination.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ In code, this looks like:
357357
{% sample lang="c" %}
358358
[import:13-44, lang:"c_cpp"](code/c/gaussian_elimination.c)
359359
{% sample lang="cpp" %}
360-
[import:6-54, lang:"c_cpp"](code/c++/gaussian_elimination.cpp)
360+
[import:8-53, lang:"c_cpp"](code/c++/gaussian_elimination.cpp)
361361
{% sample lang="rs" %}
362362
[import:41-78, lang:"rust"](code/rust/gaussian_elimination.rs)
363363
{% sample lang="hs" %}
@@ -392,7 +392,7 @@ Here it is in code:
392392
This code does not exist yet in C, so here's Julia code (sorry for the inconvenience)
393393
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
394394
{% sample lang="cpp" %}
395-
[import:79-98, lang:"c_cpp"](code/c++/gaussian_elimination.cpp)
395+
[import:73-91, lang:"c_cpp"](code/c++/gaussian_elimination.cpp)
396396
{% sample lang="rs" %}
397397
This code does not exist yet in rust, so here's Julia code (sorry for the inconvenience)
398398
[import:67-93, lang:"julia"](code/julia/gaussian_elimination.jl)
@@ -425,7 +425,7 @@ In code, this involves keeping a rolling sum of all the values we substitute in
425425
{% sample lang="c" %}
426426
[import:46-58, lang:"c_cpp"](code/c/gaussian_elimination.c)
427427
{% sample lang="cpp" %}
428-
[import:56-77, lang:"c_cpp"](code/c++/gaussian_elimination.cpp)
428+
[import:55-71, lang:"c_cpp"](code/c++/gaussian_elimination.cpp)
429429
{% sample lang="rs" %}
430430
[import:79-94, lang:"rust"](code/rust/gaussian_elimination.rs)
431431
{% sample lang="hs" %}

0 commit comments

Comments
 (0)