Skip to content

Add Thomas Algorithm in Rust #693

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
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions contents/thomas_algorithm/code/rust/thomas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
fn thomas(a: &[f64], b: &[f64], c: &[f64], x: &[f64]) -> Vec<f64> {
let size = a.len();
let mut y = vec![0.0; size];
let mut z = Vec::from(x);

y[0] = c[0] / b[0];
z[0] = x[0] / b[0];

for i in 1..size {
let scale = 1.0 / (b[i] - a[i] * y[i - 1]);
y[i] = c[i] * scale;
z[i] = (z[i] - a[i] * z[i - 1]) * scale;
}

for i in (0..(size - 1)).rev() {
z[i] -= y[i] * z[i + 1];
}

z
}

fn main() {
let a = vec![0.0, 2.0, 3.0];
let b = vec![1.0, 3.0, 6.0];
let c = vec![4.0, 5.0, 0.0];
let x = vec![7.0, 5.0, 3.0];

println!("The system");
println!("[{:?} {:?} {:?}][x] = [{:?}]", a[0], b[0], c[0], &x[0]);
println!("[{:?} {:?} {:?}][x] = [{:?}]", a[1], b[1], c[1], &x[1]);
println!("[{:?} {:?} {:?}][x] = [{:?}]", a[2], b[2], c[2], &x[2]);
println!("has the solution");

let y = thomas(&a, &b, &c, &x);

y.iter()
.for_each(|i| println!("[{:>19}]", format!("{:18}", format!("{:?}", i))));
Copy link
Member

Choose a reason for hiding this comment

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

One question before I override @Liikt and approve. Why is this nested and not just a single println!?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I essentially wanted to pretty-print the result in a certain way. (I want positive values to lead with a space.) If you want, I can get rid of the pretty-printing.

Copy link
Member

Choose a reason for hiding this comment

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

Isn't this the same?

y.iter().for_each(|i| println!("[{:>19}]", i));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not quite. The result I was looking for satisfies these guidelines:

  1. The sign should either be a negative or a space.
  2. The decimal point should (ideally) be aligned.

I guess we don't have to pretty-print the result if we don't have to.

Copy link
Member

Choose a reason for hiding this comment

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

I understand better now. As long as the explanation is in the PR thread like this, it's ok.

}
2 changes: 2 additions & 0 deletions contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang="ruby"](code/ruby/thomas.rb)
{% sample lang="js" %}
[import, lang:"javascript"](code/javascript/thomas.js)
{% sample lang="rs" %}
[import, lang:"rust"](code/rust/thomas.rs)
{% endmethod %}

<script>
Expand Down