-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Add IFS in python #700
Conversation
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 |
For sure! |
Alright, I made a few tweaks. First, I opted to make Second, I also added a function to generate the points of a regular N-gon. |
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'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!
Removed the |
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.
Nice use of the generator. Just a few requests.
contents/IFS/code/python/IFS.py
Outdated
@@ -0,0 +1,25 @@ | |||
from random import random, choice | |||
from math import sqrt, sin, cos, pi |
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.
Can you remove your unused imports?
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.
Oh! yup, my bad
contents/IFS/code/python/IFS.py
Outdated
[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)) |
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.
For consistency with the Julia implementation, the plot output will be easier to read if it's tab-separated:
f.write("{0},{1}\n".format(*point)) | |
f.write("{0}\t{1}\n".format(*point)) |
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.
done
contents/IFS/code/python/IFS.py
Outdated
|
||
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))] |
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.
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.
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'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 |
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.
👍
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.
:)
@@ -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" %} |
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 would fix the range so that the imports in the source code file aren't included.
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.
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.
Congrats on your first AAA contribution!
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