Skip to content

Commit f69ee2a

Browse files
authored
Filtering Nan's and infinites
1 parent 0490d58 commit f69ee2a

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

fractals/julia_sets.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
"""Author Alexandre De Zotti
22
3-
Draws Julia sets of quadratic polynomials and exponential maps. More specifically, this iterates the function a fixed number of times then plot whether the absolute value of the last iterate is greater than a fixed threshold (named "escape radius"). For the exponential map this is not really an escape radius but rather a convenient way to approximate the Julia set with bounded orbits.
3+
Draws Julia sets of quadratic polynomials and exponential maps.
4+
More specifically, this iterates the function a fixed number of times
5+
then plots whether the absolute value of the last iterate is greater than
6+
a fixed threshold (named "escape radius"). For the exponential map this is not
7+
really an escape radius but rather a convenient way to approximate the Julia
8+
set with bounded orbits.
49
510
The examples presented here are:
6-
- The Cauliflower Julia set, see e.g. https://en.wikipedia.org/wiki/File:Julia_z2%2B0,25.png
11+
- The Cauliflower Julia set, see e.g.
12+
https://en.wikipedia.org/wiki/File:Julia_z2%2B0,25.png
713
- Other examples from https://en.wikipedia.org/wiki/Julia_set
8-
- An exponential map Julia set ambiantly homeomorphic to the examples from in http://www.math.univ-toulouse.fr/~cheritat/GalII/galery.html and https://ddd.uab.cat/pub/pubmat/02141493v43n1/02141493v43n1p27.pdf
14+
- An exponential map Julia set, ambiantly homeomorphic to the examples in
15+
http://www.math.univ-toulouse.fr/~cheritat/GalII/galery.html
16+
and
17+
https://ddd.uab.cat/pub/pubmat/02141493v43n1/02141493v43n1p27.pdf
918
"""
1019

11-
import warnings
12-
from typing import Any, Callable, NewType, Union
20+
from typing import Any, Callable
1321

1422
import numpy
1523
from matplotlib import pyplot
1624

1725
c_cauliflower = 0.25 + 0.0j
1826
c_polynomial_1 = -0.4 + 0.6j
19-
c_polynomial_2 = -0.7269 + 0.1889j
20-
c_polynomial_2 = -0.1+0.651j
27+
c_polynomial_2 = -0.1 + 0.651j
2128
c_exponential = -2.0
2229
nb_iterations = 56
2330
window_size = 2.0
@@ -62,12 +69,12 @@ def prepare_grid(window_size: float, nb_pixels: int) -> numpy.ndarray:
6269
return x + 1.0j * y
6370

6471

65-
# def iterate_function(eval_function, function_params, nb_iterations: int, z_0: numpy.array) -> numpy.array:
6672
def iterate_function(
6773
eval_function: Callable[[Any, numpy.ndarray], numpy.ndarray],
6874
function_params: Any,
6975
nb_iterations: int,
7076
z_0: numpy.ndarray,
77+
infinity=None,
7178
) -> numpy.ndarray:
7279
"""
7380
Iterate the function "eval_function" exactly nb_iterations times.
@@ -83,6 +90,9 @@ def iterate_function(
8390
z_n = z_0
8491
for i in range(nb_iterations):
8592
z_n = eval_function(function_params, z_n)
93+
if infinity is not None:
94+
numpy.nan_to_num(z_n, copy=False, nan=infinity)
95+
z_n[abs(z_n) == numpy.inf] = infinity
8696
return z_n
8797

8898

@@ -96,8 +106,8 @@ def show_results(
96106
Plots of whether the absolute value of z_final is greater than
97107
the value of escape_radius. Adds the function_label and function_params to
98108
the title.
99-
100-
>>> show_results('Test', 0, 1, numpy.array([[0,1,0.5],[0.4,2.,1.1],[.2,1.,1.3]]))
109+
110+
>>> show_results('80', 0, 1, numpy.array([[0,1,.5],[.4,2,1.1],[.2,1,1.3]]))
101111
"""
102112

103113
abs_z_final = (abs(z_final)).transpose()
@@ -109,33 +119,48 @@ def show_results(
109119

110120
if __name__ == "__main__":
111121

112-
warnings.filterwarnings("ignore", category=RuntimeWarning)
122+
z_0 = prepare_grid(window_size, nb_pixels)
113123

114124
nb_iterations = 24
115-
z_0 = prepare_grid(window_size, nb_pixels)
125+
escape_radius = 2 * abs(c_cauliflower) + 1
116126
z_final = iterate_function(
117-
eval_quadratic_polynomial, c_cauliflower, nb_iterations, z_0
127+
eval_quadratic_polynomial,
128+
c_cauliflower,
129+
nb_iterations,
130+
z_0,
131+
infinity=1.1 * escape_radius,
118132
)
119-
escape_radius = 2 * abs(c_cauliflower) + 1
120133
show_results("z²+c", c_cauliflower, escape_radius, z_final)
121134

122135
nb_iterations = 64
123-
z_0 = prepare_grid(window_size, nb_pixels)
136+
escape_radius = 2 * abs(c_polynomial_1) + 1
124137
z_final = iterate_function(
125-
eval_quadratic_polynomial, c_polynomial_1, nb_iterations, z_0
138+
eval_quadratic_polynomial,
139+
c_polynomial_1,
140+
nb_iterations,
141+
z_0,
142+
infinity=1.1 * escape_radius,
126143
)
127-
escape_radius = 2 * abs(c_polynomial_1) + 1
128144
show_results("z²+c", c_polynomial_1, escape_radius, z_final)
129145

130146
nb_iterations = 161
131-
z_0 = prepare_grid(window_size, nb_pixels)
147+
escape_radius = 2 * abs(c_polynomial_2) + 1
132148
z_final = iterate_function(
133-
eval_quadratic_polynomial, c_polynomial_2, nb_iterations, z_0
149+
eval_quadratic_polynomial,
150+
c_polynomial_2,
151+
nb_iterations,
152+
z_0,
153+
infinity=1.1 * escape_radius,
134154
)
135-
escape_radius = 2 * abs(c_polynomial_2) + 1
136155
show_results("z²+c", c_polynomial_2, escape_radius, z_final)
137156

138157
nb_iterations = 12
139-
z_final = iterate_function(eval_exponential, c_exponential, nb_iterations, z_0 + 2)
140158
escape_radius = 10000.0
159+
z_final = iterate_function(
160+
eval_exponential,
161+
c_exponential,
162+
nb_iterations,
163+
z_0 + 2,
164+
infinity=1.0e10,
165+
)
141166
show_results("exp(z)+c", c_exponential, escape_radius, z_final)

0 commit comments

Comments
 (0)