Skip to content

Commit dd2d3fb

Browse files
committed
Revert "Gaussian elimination revisions (#544)"
This reverts commit d740d52.
1 parent f5bba77 commit dd2d3fb

File tree

5 files changed

+101
-178
lines changed

5 files changed

+101
-178
lines changed

contents/gaussian_elimination/code/java/GaussianElimination.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ static void gaussianElimination(double[][] a) {
1111
for (int col = 0; col < cols - 1; col++) {
1212
int pivot = row;
1313

14-
// finding the maximum element
14+
// Step 1: finding the maximum element
1515
for (int i = row + 1; i < row; i++) {
1616
if (Math.abs(a[i][col]) > Math.abs(a[pivot][col])) {
1717
pivot = i;
@@ -24,22 +24,22 @@ static void gaussianElimination(double[][] a) {
2424
}
2525

2626
if (row != pivot) {
27-
// Swap the row with the highest valued element
27+
// Step 2: Swap the row with the highest valued element
2828
// with the current row
2929
swapRow(a, col, pivot);
3030
}
3131

3232
for (int i = row + 1; i < rows; i++) {
33-
// finding the inverse
33+
// Step 3: finding the inverse
3434
double scale = a[i][col] / a[row][col];
3535
// loop through all columns in current row
3636
for (int j = col + 1; j < cols; j++) {
3737

38-
// Subtract rows
38+
// Step 4: Subtract rows
3939
a[i][j] -= a[row][j] * scale;
4040
}
4141

42-
// Set lower elements to 0
42+
// Step 5: Set lower elements to 0
4343
a[i][col] = 0;
4444
}
4545
row++;

contents/gaussian_elimination/code/julia/gaussian_elimination.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function gaussian_elimination!(A::Array{Float64,2})
99
# Main loop going through all columns
1010
for col = 1:(cols-1)
1111

12-
# finding the maximum element for each column
12+
# Step 1: finding the maximum element for each column
1313
max_index = argmax(abs.(A[row:end,col])) + row-1
1414

1515
# Check to make sure matrix is good!
@@ -18,26 +18,26 @@ function gaussian_elimination!(A::Array{Float64,2})
1818
continue
1919
end
2020

21-
# swap row with highest value for that column to the top
21+
# Step 2: swap row with highest value for that column to the top
2222
temp_vector = A[max_index, :]
2323
A[max_index, :] = A[row, :]
2424
A[row, :] = temp_vector
2525

2626
# Loop for all remaining rows
2727
for i = (row+1):rows
2828

29-
# finding fraction
29+
# Step 3: finding fraction
3030
fraction = A[i,col]/A[row,col]
3131

3232
# loop through all columns for that row
3333
for j = (col+1):cols
3434

35-
# re-evaluate each element
35+
# Step 4: re-evaluate each element
3636
A[i,j] -= A[row,j]*fraction
3737

3838
end
3939

40-
# Set lower elements to 0
40+
# Step 5: Set lower elements to 0
4141
A[i,col] = 0
4242
end
4343
row += 1

contents/gaussian_elimination/code/python/gaussian_elimination.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ def gaussian_elimination(A):
44

55
pivot_row = 0
66

7-
# Go by column
7+
# Step 1: Go by column
88
for pivot_col in range(min(A.shape[0], A.shape[1])):
99

10-
# Swap row with highest element in col
10+
# Step 2: Swap row with highest element in col
1111
max_i = np.argmax(abs(A[pivot_row:, pivot_col])) + pivot_row
1212

1313
temp = A[pivot_row, :].copy()
@@ -18,11 +18,11 @@ def gaussian_elimination(A):
1818
if A[pivot_row, pivot_col] == 0:
1919
continue
2020

21-
# Zero out elements below pivot
21+
# Steps 3 & 4: Zero out elements below pivot
2222
for r in range(pivot_row + 1, A.shape[0]):
23-
# Get fraction
23+
# Step 3: Get fraction
2424
frac = -A[r, pivot_col] / A[pivot_row, pivot_col]
25-
# Add rows
25+
# Step 4: Add rows
2626
A[r, :] += frac * A[pivot_row, :]
2727

2828
pivot_row += 1

contents/gaussian_elimination/code/rust/gaussian_elimination.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl IndexMut<(usize, usize)> for Matrix {
4040

4141
fn gaussian_elimination(a: &mut Matrix) {
4242
for k in 0..min(a.cols, a.rows) {
43-
// find the maximum element for this column
43+
// Step 1: find the maximum element for this column
4444
let mut max_row = k;
4545
let mut max_value = a[(k, k)].abs();
4646
for row in (k + 1)..a.rows {
@@ -56,21 +56,21 @@ fn gaussian_elimination(a: &mut Matrix) {
5656
return;
5757
}
5858

59-
// swap the row with the highest value for this kumn to the top
59+
// Step 2: swap the row with the highest value for this kumn to the top
6060
a.swap_rows(k, max_row);
6161

6262
// Loop over all remaining rows
6363
for i in k + 1..a.rows {
64-
// find the fraction
64+
// Step 3: find the fraction
6565
let fraction = a[(i, k)] / a[(k, k)];
6666

6767
// Loop through all columns for that row
6868
for j in (k + 1)..a.cols {
69-
// re-evaluate each element
69+
// Step 4: re-evaluate each element
7070
a[(i, j)] -= a[(k, j)] * fraction;
7171
}
7272

73-
// set lower elements to 0
73+
// Step 5: set lower elements to 0
7474
a[(i, k)] = 0.0;
7575
}
7676
}

0 commit comments

Comments
 (0)