-
-
Notifications
You must be signed in to change notification settings - Fork 359
added thomas alg in nim #399
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 4 commits
818fd32
a06341d
b5bdce3
887c35a
755cb2c
d0a22bf
578ca13
1f80db8
c06e13f
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,40 @@ | ||
const x: array[3,float] = [0.0,2.0,3.0] | ||
const y: array[3,float] = [1.0,3.0,6.0] | ||
const z: array[3,float] = [4.0,5.0,0.0] | ||
const w: array[3,float] = [7.0,5.0,3.0] | ||
|
||
const n: int = len(w) | ||
|
||
proc thomas_algorithm(a,b,c_in,d_in: array[n,float]): array[n,float] = | ||
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. That solution is also not satisfying, you shouldn't rely on a global variable outside of the function, this is basically hardcoding in disguise. |
||
|
||
#const n: int = len(d1) | ||
|
||
var c: array[n,float] = c_in | ||
var d: array[n,float] = d_in | ||
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 quite clean I think :) |
||
|
||
c[0] = c[0] / b[0] | ||
d[0] = d[0] / b[0] | ||
|
||
for i in 1..n-1: | ||
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. There is accidental indentation from here to the end of the procedure. Also |
||
let scale: float = (1 / (b[i] - c[i-1] * a[i])) | ||
|
||
c[i] = c[i] * scale | ||
d[i] = (d[i] - a[i] * d[i - 1]) * scale | ||
|
||
for i in countdown(n-2,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.
|
||
d[i] -= c[i]*d[i+1] | ||
|
||
return d | ||
|
||
|
||
echo "The system," | ||
echo "[1.0 4.0 0.0][x] = [7.0]" | ||
echo "[2.0 3.0 5.0][x] = [5.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 write 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. yeah that was a copy paste fail on my part. 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. Don't forget to fix this :) |
||
echo "[0.0 3.0 6.0][x] = [3.0]" | ||
|
||
echo "has the solution:" | ||
|
||
const soln: array[3,float] = thomas_algorithm(x,y,z,w) | ||
|
||
for i in 0..2: | ||
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. Can you calculate this range based on the number of equations again, rather than have it be hard-coded? |
||
echo soln[i] |
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.
It's more readable if these are defined after the function. I suspect that you had to do this for the function to know
n
but that's not the way you should do it.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 could pass the length of the arrays to the thomas_algorithm function. Would that be okay?
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.
Yes, that would be fine