Skip to content

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

Merged
merged 9 commits into from
Oct 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions contents/thomas_algorithm/code/nim/thomas_algorithm.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
proc thomas_algorithm(a,b,c,d: var array[3,float]) =
Copy link
Member

Choose a reason for hiding this comment

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

It's a personal bias of mine, but I prefer functions that don't modify the input. Is there an easy way to return the modified d without changing the original?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can do that, it might look less neat though. Because I would have to create new variables in the thomas alg proc and assign them to the inputs.

Copy link
Member

Choose a reason for hiding this comment

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

You only need to copy c and d though, it's only a couple more lines... There should be some kind of copy function that does it.

Copy link
Member

Choose a reason for hiding this comment

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

On that very same line: array[3,float].
Having a 3 there is a big no no!

Copy link
Member Author

Choose a reason for hiding this comment

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

is that because the size shouldn't be hard coded?


let n: int = len(d)

c[0] = c[0] / b[0]
d[0] = d[0] / b[0]

for i in 1..n-1:
Copy link
Member

Choose a reason for hiding this comment

The 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 n-1 -> n - 1.

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):
Copy link
Member

Choose a reason for hiding this comment

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

(n-2,0) -> (n - 2, 0)

d[i] -= c[i]*d[i+1]



var x: array[3,float] = [0.0,2.0,3.0]
var y: array[3,float] = [1.0,3.0,6.0]
var z: array[3,float] = [4.0,5.0,0.0]
var w: array[3,float] = [7.0,5.0,3.0]
Copy link
Member

Choose a reason for hiding this comment

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

If you make your thomas_algorithm pure, then you can change the var to const.


echo "The system,"
echo "[1.0 4.0 0.0][x] = [7.0]"
echo "[2.0 3.0 5.0][x] = [5.0]"
Copy link
Member

Choose a reason for hiding this comment

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

I would write [y] and [z] instead fo [x] everywhere

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah that was a copy paste fail on my part.

Copy link
Member

Choose a reason for hiding this comment

The 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:"

thomas_algorithm(x,y,z,w)

for i in 0..2:
Copy link
Member

Choose a reason for hiding this comment

The 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 w[i]
4 changes: 3 additions & 1 deletion contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang:"haskell"](code/haskell/thomas.hs)
{% sample lang="swift" %}
[import, lang:"swift"](code/swift/thomas.swift)
{%sample lang="nim" %}
[import, lang:"nim"](code/nim/thomas_algorithm.nim)
{% endmethod %}

<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>
</script>