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

Conversation

c252
Copy link
Member

@c252 c252 commented Sep 29, 2018

implemented thomas algorithm in nim

@Gathros Gathros added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Sep 30, 2018
@leios leios added the Hacktoberfest The label for all Hacktoberfest related things! label Oct 1, 2018
Copy link
Member

@jiegillet jiegillet left a comment

Choose a reason for hiding this comment

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

The algorithm looks fine, I just have a couple of small requests.


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 :)

@@ -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?

@jiegillet jiegillet self-assigned this Oct 3, 2018
Copy link
Member

@jiegillet jiegillet left a comment

Choose a reason for hiding this comment

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

Next round :)

@@ -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.

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.

@@ -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.

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

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.

@c252
Copy link
Member Author

c252 commented Oct 3, 2018

I might have messed something up with git but if this looks good, then I will change the import lines.

Copy link
Member

@jiegillet jiegillet left a comment

Choose a reason for hiding this comment

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

This is also not a great solution. I apologize for not knowing nim, I cannot really help you figure out the answer, but there must be a cleaner way.


const n: int = len(w)

proc thomas_algorithm(a,b,c_in,d_in: array[n,float]): array[n,float] =
Copy link
Member

Choose a reason for hiding this comment

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

@@ -0,0 +1,40 @@
const x: array[3,float] = [0.0,2.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.

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.

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 could pass the length of the arrays to the thomas_algorithm function. Would that be okay?

Copy link
Member

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

#const n: int = len(d1)

var c: array[n,float] = c_in
var d: array[n,float] = d_in
Copy link
Member

Choose a reason for hiding this comment

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

This is quite clean I think :)


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.

Don't forget to fix this :)

Copy link
Member

@berquist berquist left a comment

Choose a reason for hiding this comment

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

We talked in chat about the solution, but a few other things:

  1. Please always put spaces around binary operators (a[i-1] vs. a[i - 1]). Similarly, add a space after a comma ([1.0,3.0,6.0] vs. [1.0, 3.0, 6.0]).
  2. In the style guide, they say indentation should be two spaces. Can you add these lines to the .editorconfig file at the project root?
# Nim
[*.nim]
indent_style = space
indent_size = 2

@jiegillet
Copy link
Member

The precise use of seq is beyond my understanding, the rest looks fine to me now. @berquist the final word is all yours.

@c252
Copy link
Member Author

c252 commented Oct 5, 2018

Cool, I will change the import lines when I get the ok from berquist


const soln: seq[float] = 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?

c[0] /= b[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.

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)


echo "has the solution:"

const soln: seq[float] = thomas_algorithm(x,y,z,w)
Copy link
Member

Choose a reason for hiding this comment

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

(x,y,z,w) -> (x, y, z, w)

@c252
Copy link
Member Author

c252 commented Oct 5, 2018

I will fix these when I get home from school, I did fix the indentation previously but I must have messed something up with git.

@c252
Copy link
Member Author

c252 commented Oct 5, 2018

I made the formatting changes that Berquist requested and the array print loop is not hard coded any more.

@berquist
Copy link
Member

berquist commented Oct 5, 2018

I can't render it right now because I'm at work, but

  1. there is a conflict in the Markdown file, and
  2. you accidentally committed the generated *.c and *.o files.

@c252
Copy link
Member Author

c252 commented Oct 5, 2018

Should be good now.

@berquist berquist merged commit 93c1cd9 into algorithm-archivists:master Oct 5, 2018
@berquist
Copy link
Member

berquist commented Oct 5, 2018

I am glad this was figured out. Thank you!

kenpower pushed a commit to kenpower/algorithm-archive that referenced this pull request Oct 22, 2018
implemented thomas algorithm in nim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants