-
-
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 7 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,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); | ||
for _ in 0..rows*cols { data.push(0.0); } | ||
Matrix { rows, cols, data } | ||
} | ||
|
||
fn get(&self, row: usize, col: usize) -> 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. I would use the |
||
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 | ||
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 probably meant column 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. oops, got a bit to greedy with a find & replace |
||
let mut max_row = 0; | ||
let mut max_value = 0.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. 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); | ||
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. 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); | ||
} |
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.
This is just:
let data = vec![0.0; rows * cols];