-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
berquist
merged 3 commits into
algorithm-archivists:master
from
fanninpm:thomas_algorithm_in_rust
May 24, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
One question before I override @Liikt and approve. Why is this nested and not just a single
println!
?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.
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.
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.
Isn't this the same?
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.
Not quite. The result I was looking for satisfies these guidelines:
I guess we don't have to pretty-print the result if we don't have to.
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.
I understand better now. As long as the explanation is in the PR thread like this, it's ok.