Mandelbrot set
The Mandelbrot set (Dilão 2023 §5.5).
- Class:
MandelbrotSystem - Import:
from skcomplex.systems.mathematics.fractals import MandelbrotSystem - Field:
mathematics/fractals - Topics:
fractal,complex,map,lattice,escape time - References:
- Dilão 2023
Use this system
The block imports the class and simulates the system with its default parameters and its tuned run settings.
from skcomplex.systems.mathematics.fractals import MandelbrotSystem
trajectory = MandelbrotSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
shape |
(240, 300) |
window |
(-2.2, 0.8, -1.2, 1.2) |
radius |
2.0 |
solver |
'auto' |
backend |
'auto' |
Tags
The system with its default parameters has these capability tags, which solvers and analyses check.
| Tag | Values |
|---|---|
| Time | discrete |
| Space | lattice |
| Laws | update |
| Randomness | deterministic |
| Domains | complex, integer |
| Structure | fixed |
Class
The source of the class shows how it builds its components from its model parameters.
class MandelbrotSystem(BaseSystem):
"""The Mandelbrot set (Dilão 2023 §5.5).
The set holds the points `c` whose orbit of `z -> z^2 + c` from 0 stays bounded.
Every site holds one point `c` of the window and iterates the map, and `n` counts the steps the orbit stays within
the escape radius. The points of the set never escape, so their count grows with every step, while the count of an
escaped point stops at its escape time and colors the bands around the set. The system has the field `plane`, its
orbits `plane.z`, and its escape times `plane.n`.
Args:
shape:
The number of sites along the imaginary and the real axis.
window:
The smallest and largest real part, then the smallest and largest imaginary part, of the points.
radius:
The escape radius, 2 for the quadratic map.
label:
The label of the system, which names it when it is nested in another one.
solver:
The solver of the problem, or `'auto'` for the one the system chooses.
backend:
The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.
Examples:
>>> from skcomplex.systems.mathematics.fractals import MandelbrotSystem
>>> MandelbrotSystem((3, 3), window=(-1, 1, -1, 1)).prepare().system_.elements[0].c[3:6].tolist()
[(-1+0j), 0j, (1+0j)]
"""
field = 'mathematics/fractals'
topics = ('fractal', 'complex', 'map', 'lattice', 'escape time')
references = ('Dilão 2023',)
until = 100
dt = 1
@staticmethod
def _get_plane(shape: Sequence[int], window: Window) -> npt.NDArray[np.complex128]:
"""Return the complex points of a lattice over a window.
The real part runs along columns and the imaginary part up.
"""
rows, columns = (int(item) for item in shape)
left, right, bottom, top = window
real = np.linspace(left, right, columns)
imaginary = np.linspace(top, bottom, rows)
return real[np.newaxis, :] + 1j * imaginary[:, np.newaxis]
@staticmethod
def _get_escape_system(
label: str,
shape: Sequence[int],
z: npt.NDArray[np.complex128] | complex,
c: npt.NDArray[np.complex128] | complex,
radius: float,
) -> System:
"""Return the system of the quadratic map over a lattice.
The system comes from the initial points and the constants, given as arrays of the shape of the lattice or as
one shared value, and read row by row as one value per site.
"""
space = LatticeSpace(tuple(int(item) for item in shape))
z, c = (np.ravel(value) if isinstance(value, np.ndarray) else value for value in (z, c))
return System(
label,
elements=[QuadraticMapElement('plane', z=z, c=c, space=space)],
interactions=[QuadraticMapInteraction('escape', target='plane', radius=radius)],
)
def __init__(
self,
shape: Sequence[int] = (240, 300),
*,
window: Window = MANDELBROT_WINDOW,
radius: float = 2.0,
label: str = 'system',
solver: BaseSolver | str = 'auto',
backend: BaseBackend | str = 'auto',
) -> None:
super().__init__(label, solver=solver, backend=backend)
self.shape = shape
self.window = window
self.radius = radius
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the points of the window and their escape."""
super()._prepare(context)
points = MandelbrotSystem._get_plane(self.shape, self.window)
self.system_ = MandelbrotSystem._get_escape_system(
self.label,
self.shape,
np.zeros_like(points),
points,
self.radius,
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
QuadraticMapElement, fromElement
Interactions:
QuadraticMapInteraction, fromInteraction
class QuadraticMapElement(Element):
"""Points of the complex quadratic map `z' = z^2 + c`, each with a count `n` of its steps.
A `QuadraticMapInteraction` iterates the points while they stay within the escape radius.
Args:
label:
The label of the element.
z:
The initial point.
n:
The initial number of steps within the escape radius.
c:
The constant of the map.
space:
The space with one point per site, or `None` for one point.
Examples:
>>> from skcomplex.systems.mathematics.fractals import QuadraticMapElement
>>> QuadraticMapElement('point', c=0.25).c
0.25
"""
visual = VisualSpec('image', {'states': ('n',), 'low': 0.0})
def __init__(
self,
label: str,
*,
z: object = 0.0,
n: int = 0,
c: object = 0.0,
space: BaseSpace | None = None,
) -> None:
super().__init__(label, space=space)
self.z = z
self.n = n
self.c = c
class QuadraticMapInteraction(Interaction):
"""Iteration of `z' = z^2 + c` with a count of the steps within the escape radius.
The point stays once `|z|` exceeds the escape radius, and the count `n` stops at its escape time.
Args:
label:
The label of the interaction.
target:
The points whose values `z` and `n` the map sets.
radius:
The escape radius.
Examples:
>>> from skcomplex.systems.mathematics.fractals import QuadraticMapInteraction
>>> QuadraticMapInteraction('escape', radius=3.0).radius
3.0
"""
law_classes = (UpdateLaw,)
def __init__(self, label: str, *, target: Role = 'plane', radius: float = 2.0) -> None:
super().__init__(label)
self.target = target
self.radius = radius
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the next point and the next count as the laws.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
z, n, c = context.target.z, context.target.n, context.target.c
inside = abs(z) <= context.radius
self.laws_: list[BaseLaw] = [
UpdateLaw(z, where(inside, z**2 + c, z)),
UpdateLaw(n, n + where(inside, 1, 0)),
]