-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Changes from 12 commits
ea8bb8e
57ca40e
e4b5b53
bf264c2
dc4fe0a
faad419
c83b1a8
165cbe5
931e18b
d3439b1
f23060a
2db7f97
7ea86eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// submitted by jess 3jane | ||
|
||
use std::cmp::min; | ||
use std::ops::{Index, IndexMut}; | ||
|
||
pub struct Matrix { | ||
rows: usize, | ||
cols: usize, | ||
data: Vec<f64>, | ||
} | ||
|
||
impl Matrix { | ||
fn new(rows: usize, cols: usize) -> Matrix { | ||
Matrix { | ||
rows, | ||
cols, | ||
data: vec![0.0; rows * cols], | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} | ||
|
||
impl Index<(usize, usize)> for Matrix { | ||
type Output = f64; | ||
fn index(&self, index: (usize, usize)) -> &f64 { | ||
let (row, col) = index; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You left the separate destructuring here |
||
&self.data[row * self.cols + col] | ||
} | ||
} | ||
|
||
impl IndexMut<(usize, usize)> for Matrix { | ||
fn index_mut<'a>(&'a mut self, (row, col): (usize, usize)) -> &'a mut f64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And the explicit lifetime here :) |
||
&mut self.data[row * 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 column | ||
let mut max_row = k; | ||
let mut max_value = a[(k, k)].abs(); | ||
for row in (k + 1)..a.rows { | ||
if max_value < a[(row, k)].abs() { | ||
max_value = a[(row, k)].abs(); | ||
max_row = row; | ||
} | ||
} | ||
|
||
// Check to make sure the matrix is good | ||
if a[(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[(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 | ||
a[(i, j)] -= a[(k, j)] * fraction; | ||
} | ||
|
||
// Step 5: set lower elements to 0 | ||
a[(i, k)] = 0.0; | ||
} | ||
} | ||
} | ||
|
||
fn back_substitution(a: &Matrix) -> Vec<f64> { | ||
let mut soln = vec![0.0; a.rows]; | ||
|
||
soln[a.rows - 1] = a[(a.rows - 1, a.cols - 1)] / a[(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[(i, j)]; | ||
} | ||
soln[i] = (a[(i, a.cols - 1)] - sum) / a[(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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer this to be bassed as the 3rd argument to |
||
|
||
gaussian_elimination(&mut a); | ||
let soln = back_substitution(&a); | ||
println!("Solution: {:?}", soln); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just destructure these in the argument list:
(row, col): (usize, usize)