Skip to content

Add IFS in python #700

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

Conversation

jonathanvanschenck
Copy link
Contributor

@jonathanvanschenck jonathanvanschenck commented May 21, 2020

I tried to adapt the julia code as closely as possible, with just a few python-specific tweaks.
I opted to only use base python packages so that the code would be usable for more people (with numpy, I think this is a one-liner).
Additionally, I intentionally used some python specific syntax and structuring (like "[x for x in y]" list comprehension and the zip function, etc) because I felt it was truer to the language, than doing the usually thing of making a full for loop block that loops over an index. This whole thing would be much cleaner if base python had element-wise addition. . . sigh

@leios leios mentioned this pull request May 21, 2020
@leios
Copy link
Member

leios commented May 21, 2020

Not a rigorous review yet, but would you mind outputting the set of points and writing them to file afterwards? I will be modifying the Julia code to do this as well.

This was done in PR #701, and I think it's a good addition

@jonathanvanschenck
Copy link
Contributor Author

For sure!

@jonathanvanschenck
Copy link
Contributor Author

Alright, I made a few tweaks.

First, I opted to make chaos_game a python generator rather than to have it build up the list and return the whole thing. If you think it obscures the algorithm too much, I'll switch it to a regular function like you were mentioning above. I'm still pretty new to publishing code, so I'm still learning what code is "clear" and what code is not.

Second, I also added a function to generate the points of a regular N-gon.

Copy link
Member

@leios leios left a comment

Choose a reason for hiding this comment

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

I'm alright with this code, although I would ask you to remove the ngon_shape_points(...) function for consistency.

Feel free to argue this point. Thanks again for the submission!

@jonathanvanschenck
Copy link
Contributor Author

Removed the ngon_shape_points function.

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.

Nice use of the generator. Just a few requests.

@@ -0,0 +1,25 @@
from random import random, choice
from math import sqrt, sin, cos, pi
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 remove your unused imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! yup, my bad

[1.0, 0.0]]
with open("sierpinski.dat", "w") as f:
for point in chaos_game(10000, shape_points):
f.write("{0},{1}\n".format(*point))
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with the Julia implementation, the plot output will be easier to read if it's tab-separated:

Suggested change
f.write("{0},{1}\n".format(*point))
f.write("{0}\t{1}\n".format(*point))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


for _ in range(n):
# Update the point position and yield the result
point = [(_p + _s) / 2 for _p, _s in zip(point, choice(shape_points))]
Copy link
Member

Choose a reason for hiding this comment

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

You don't need the underscores here, since _p and _s can't escape the scope of the list comprehension. Up to you whether or not to keep them.

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'll change them

for _ in range(n):
# Update the point position and yield the result
point = [(_p + _s) / 2 for _p, _s in zip(point, choice(shape_points))]
yield point
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

:)

@@ -130,6 +130,8 @@ Here, instead of tracking children of children, we track a single individual tha
[import:4-17, lang:"julia"](code/julia/IFS.jl)
{% sample lang="cpp" %}
[import:39-52, lang:"cpp"](code/c++/IFS.cpp)
{% sample lang="py" %}
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 fix the range so that the imports in the source code file aren't included.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay!

@berquist berquist added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label May 24, 2020
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.

Congrats on your first AAA contribution!

@berquist berquist merged commit a7a8dfb into algorithm-archivists:master May 27, 2020
@jonathanvanschenck jonathanvanschenck deleted the iterated_function_systems_python branch May 27, 2020 16:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants