Skip to content

Drawing Julia sets #4382

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 24 commits into from
Sep 29, 2021
Merged

Drawing Julia sets #4382

merged 24 commits into from
Sep 29, 2021

Conversation

alexn11
Copy link
Contributor

@alexn11 alexn11 commented Apr 30, 2021

Describe your change:

Drawing Julia sets

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@ghost ghost added awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Apr 30, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algobytewise
Copy link
Contributor

Hi alexn11, I couldn't find the images produced by your program neither on the Wikipedia article nor through a short google search. Do you know of such a website to have a visual confirmation that the program does what it is supposed to do?

@alexn11
Copy link
Contributor Author

alexn11 commented May 4, 2021

Hi algobytewise,
The first two images are of the Cauliflower Julia set: it is visible on this wikipedia article https://en.wikipedia.org/wiki/List_of_fractals_by_Hausdorff_dimension (9th row of 1st table), specifically this image:https://en.wikipedia.org/wiki/File:Julia_z2%2B0,25.png)
For the two other ones, there are similar images: http://www.math.univ-toulouse.fr/~cheritat/GalII/galery.html, or p.4 of this article:https://ddd.uab.cat/pub/pubmat/02141493v43n1/02141493v43n1p27.pdf. All these sets are proved to be ambiently homeomorphic. If you think it's better I could change the parameter c to match one of these examples exactly.
Thanks for reviewing my PR.

@ghost ghost added the tests are failing Do not merge until tests pass label May 4, 2021
@algobytewise
Copy link
Contributor

Thanks for the prompt response. Visually it looks fine. Correct me if I'm wrong, but I think for the examples you picked, it's not important to have two plots per example (one for the absolute value & one whether it escapes) because there is not much of a color gradient for the absolute value. Since it's in the directory fractals, you could add one example that looks a little more "fractal-like", for example, something from https://en.wikipedia.org/wiki/Julia_set#Quadratic_polynomials. For them, the two plots should make sense, I guess.

You still need to add the type-hints for mypy. https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md has some basic information on that.

By the way, I get the following warnings:
julia_sets.py:39: RuntimeWarning: overflow encountered in multiply return z * z + c
julia_sets.py:39: RuntimeWarning: invalid value encountered in multiply return z * z + c
julia_sets.py:27: RuntimeWarning: overflow encountered in exp return numpy.exp(z) + c
julia_sets.py:27: RuntimeWarning: invalid value encountered in exp return numpy.exp(z) + c

@alexn11
Copy link
Contributor Author

alexn11 commented May 4, 2021

Thanks a lot, your remarks are very helpful.

@algobytewise
Copy link
Contributor

algobytewise commented May 4, 2021

I think the type is not "numpy.array" but "numpy.ndarray".

Added online sources for comparison.
Added more examples of fractal Julia sets.
Added all type hints.
Only show one picture
Silented RuntuleWarning's (there's no way of avoiding them and they're not an issue per se)
@ghost ghost removed the require type hints https://docs.python.org/3/library/typing.html label May 4, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@ghost ghost removed the require tests Tests [doctest/unittest/pytest] are required label May 4, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Copy link
Contributor

@algobytewise algobytewise left a comment

Choose a reason for hiding this comment

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

Thanks for implementing all the changes. I'm not sure that it's a good idea to just hide the RuntimeWarnings. I haven't looked at the code in detail, but I imagine that having an overflow might make the difference whether a pixel is categorized as above or below the escape_radius, resulting in a bug. Maybe that's the reason why the last example is dotted while the example on the website has solid colored sections.

An easy solution might be to change the array-type to 64 bit, see https://stackoverflow.com/questions/7559595/python-runtimewarning-overflow-encountered-in-long-scalars. Maybe reducing the number of iterations might also do the trick. Otherwise you may have to implement a check in the code whether the numbers are getting too big and maybe stop the iteration-process for the concerned values.

I'm also getting the following errors from flake-8 test. I guess some of the comment-lines are too long & need to be separated into several lines.
julia_sets.py:3:89: E501 line too long (388 > 88 characters)
julia_sets.py:6:89: E501 line too long (92 > 88 characters)
julia_sets.py:8:89: E501 line too long (208 > 88 characters)
julia_sets.py:12:1: F401 'typing.NewType' imported but unused
julia_sets.py:12:1: F401 'typing.Union' imported but unused
julia_sets.py:65:89: E501 line too long (108 > 88 characters)

@alexn11
Copy link
Contributor Author

alexn11 commented May 6, 2021

Yes you're right for the dotted graph. I've made a new version that replace nans and infinites with a large value. Reducing the number of iterations would decrease the quality of the output. I was looking for a way to have numpy replace overflows etc with a specific value without raising the warning (while continuing array computations) but couldn't find any. Given the way the loop is implemented, the runtime warnings are unavoidable. I think this is fine as long as we are aware of why these warnings occur.

@ghost ghost added the require type hints https://docs.python.org/3/library/typing.html label May 6, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@ghost ghost removed the require type hints https://docs.python.org/3/library/typing.html label May 6, 2021
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@algobytewise
Copy link
Contributor

algobytewise commented May 6, 2021

Your solution is definitely a progress but I'm not sure that it fully solves the problem. Have a look at the following example:

z_0 = numpy.array([[2, 3]])
z_final = iterate_function(
	eval_quadratic_polynomial,
	0,
	10,
	z_0,
)

The final value is [[ 0 -1876701183]], which does not make much sense: both values should be really high or infinite, not 0 or negative.
(my mistake, see below)

@algobytewise
Copy link
Contributor

One solution that comes to my mind is to apply the eval_function not to the array as a whole but to individual values. While iterating you could check whether the value crosses the escape radius and then break the loop for this value. This way you should never get close to overflow.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Type of expected output value for iterate function int array -> complex array (throws an error on test)
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@poyea poyea self-assigned this Sep 23, 2021
@poyea
Copy link
Member

poyea commented Sep 23, 2021

@alexn11 Please take a look:

FAILED fractals/julia_sets.py::julia_sets.eval_quadratic_polynomial
FAILED fractals/julia_sets.py::julia_sets.iterate_function

- More accurate type for tests cases in eval_quadratic_polynomial and iterate_function
- added more characters for variables c & z in eval_quadratic_polynomial and eval_exponential to silent bot warnings
@ghost ghost removed the require descriptive names This PR needs descriptive function and/or variable names label Sep 23, 2021
@alexn11
Copy link
Contributor Author

alexn11 commented Sep 23, 2021

@alexn11 Please take a look:

FAILED fractals/julia_sets.py::julia_sets.eval_quadratic_polynomial
FAILED fractals/julia_sets.py::julia_sets.iterate_function

Thanks for pointing these out. There was some typing issue. Should be resolved now.

@poyea
Copy link
Member

poyea commented Sep 28, 2021

@algorithms-keeper review-all

@ghost ghost removed the tests are failing Do not merge until tests pass label Sep 28, 2021
Copy link
Member

@poyea poyea left a comment

Choose a reason for hiding this comment

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

Thank you for your pull request!🤩Looks good to me. While we're waiting for the final checks, feel free to improve anything you want.

@ghost ghost removed the awaiting reviews This PR is ready to be reviewed label Sep 28, 2021
@alexn11
Copy link
Contributor Author

alexn11 commented Sep 28, 2021

Thank you for your pull request!star_struckLooks good to me. While we're waiting for the final checks, feel free to improve anything you want.

Great! Thank you very much for the edits!

@poyea poyea merged commit 02bc4bf into TheAlgorithms:master Sep 29, 2021
shermanhui pushed a commit to shermanhui/Python that referenced this pull request Oct 22, 2021
* Added Julia sets drawing

* Forgot the .py extension

* Update julia_sets.py

Added online sources for comparison.
Added more examples of fractal Julia sets.
Added all type hints.
Only show one picture
Silented RuntuleWarning's (there's no way of avoiding them and they're not an issue per se)

* Added doctest example for "show_results"

* Filtering Nan's and infinites

* added 1 missing type hint

* in iterate_function, convert to dtype=complex64

* RuntimeWarning (fine) filtering

* Type hint, test for ignore_warnings function, typo in header

* Update julia_sets.py

Type of expected output value for iterate function int array -> complex array (throws an error on test)

* Update julia_sets.py

- More accurate type for tests cases in eval_quadratic_polynomial and iterate_function
- added more characters for variables c & z in eval_quadratic_polynomial and eval_exponential to silent bot warnings

* Function def formatting

Blocked by black

* Update julia_sets.py

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

* added more doctests for eval_exponential

* Update fractals/julia_sets.py

Co-authored-by: John Law <[email protected]>

Co-authored-by: John Law <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants