Skip to content

Gaussian elimination revisions #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
10 changes: 5 additions & 5 deletions contents/gaussian_elimination/code/java/GaussianElimination.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static void gaussianElimination(double[][] a) {
for (int col = 0; col < cols - 1; col++) {
int pivot = row;

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

if (row != pivot) {
// Step 2: Swap the row with the highest valued element
// Swap the row with the highest valued element
// with the current row
swapRow(a, col, pivot);
}

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

// Step 4: Subtract rows
// Subtract rows
a[i][j] -= a[row][j] * scale;
}

// Step 5: Set lower elements to 0
// Set lower elements to 0
a[i][col] = 0;
}
row++;
Expand Down
10 changes: 5 additions & 5 deletions contents/gaussian_elimination/code/julia/gaussian_elimination.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function gaussian_elimination!(A::Array{Float64,2})
# Main loop going through all columns
for col = 1:(cols-1)

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

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

# Step 2: swap row with highest value for that column to the top
# swap row with highest value for that column to the top
temp_vector = A[max_index, :]
A[max_index, :] = A[row, :]
A[row, :] = temp_vector

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

# Step 3: finding fraction
# finding fraction
fraction = A[i,col]/A[row,col]

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

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

end

# Step 5: Set lower elements to 0
# Set lower elements to 0
A[i,col] = 0
end
row += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ def gaussian_elimination(A):

pivot_row = 0

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

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

temp = A[pivot_row, :].copy()
Expand All @@ -18,11 +18,11 @@ def gaussian_elimination(A):
if A[pivot_row, pivot_col] == 0:
continue

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

pivot_row += 1
Expand Down
10 changes: 5 additions & 5 deletions contents/gaussian_elimination/code/rust/gaussian_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl IndexMut<(usize, usize)> for Matrix {

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

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

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

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

// Step 5: set lower elements to 0
// set lower elements to 0
a[(i, k)] = 0.0;
}
}
Expand Down
Loading