Skip to content

Added rust implementation of gaussian elimination #183

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
merged 13 commits into from
Jul 2, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// submitted by jess 3jane

use std::cmp::min;

pub struct Matrix {
rows: usize,
cols: usize,
data: Vec<f64>,
}

impl Matrix {
fn new(rows: usize, cols: usize) -> Matrix {
let mut data = Vec::with_capacity(rows*cols);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just: let data = vec![0.0; rows * cols];

for _ in 0..rows*cols { data.push(0.0); }
Matrix { rows, cols, data }
}

fn get(&self, row: usize, col: usize) -> f64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use the Index traits for this, it would be a lot more elegant

self.data[row * self.cols + col]
}

fn set(&mut self, row: usize, col: usize, value: f64) {
self.data[row * self.cols + col] = value;
}

fn swap_rows(&mut self, a: usize, b: usize) {
for col in 0..self.cols {
self.data.swap(a * self.cols + col, b * self.cols + col);
}
}
}

fn gaussian_elimination(a: &mut Matrix) {
for k in 0..min(a.cols, a.rows) {
// Step 1: find the maximum element for this kumn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably meant column

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, got a bit to greedy with a find & replace

let mut max_row = 0;
let mut max_value = 0.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the cardinal sins of maximum finding: you should always start out with the maximum being the 0th element of the list, not an arbitrary low value.

for row in k..a.rows {
if max_value < a.get(row, k).abs() {
max_value = a.get(row, k).abs();
max_row = row;
}
}

// Check to make sure the matrix is good
if a.get(max_row, k) == 0.0 {
println!("Matrix is singular, aborting");
return;
}

// Step 2: 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
let fraction = a.get(i, k)/a.get(k, k);

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

// Step 5: set lower elements to 0
a.set(i, k, 0.0);
}
}
}

fn back_substitution(a: &Matrix) -> Vec<f64> {
let mut soln = Vec::with_capacity(a.rows);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to another comment, this is just vec![0.0; a.rows]

for _ in 0..a.rows { soln.push(0.0); }

soln[a.rows - 1] = a.get(a.rows - 1, a.cols - 1) / a.get(a.rows - 1, a.cols - 2);

for i in (0..a.rows - 1).rev() {
let mut sum = 0.0;
for j in (i..a.rows).rev() {
sum += soln[j] * a.get(i, j);
}
soln[i] = (a.get(i, a.cols - 1) - sum) / a.get(i,i);
}

soln
}

fn main() {
// The example matrix from the text
let mut a = Matrix::new(3,4);
a.data = vec![2.0, 3.0, 4.0, 6.0,
1.0, 2.0, 3.0, 4.0,
3.0, -4.0, 0.0, 10.0,];

gaussian_elimination(&mut a);
let soln = back_substitution(&a);
println!("Solution: {:?}", soln);
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ The full code can be seen here:
{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/gaussian_elimination.jl)
{% sample lang="rs" %}
[import, lang:"rust"](code/rust/gaussian_elimination.rs)
{% endmethod %}


Expand Down