Computing the Mandelbrot set with the Wolfram Language
Author
Arnoud Buzing
Title
Computing the Mandelbrot set with the Wolfram Language
Description
Different ways to efficiently compute the Mandelbrot set in the Wolfram Language
Category
Educational Materials
Keywords
mandelbrot set, julia set, fractals
URL
http://www.notebookarchive.org/2021-08-ebi6zry/
DOI
https://notebookarchive.org/2021-08-ebi6zry
Date Added
2021-08-31
Date Last Modified
2021-08-31
File Size
334.19 kilobytes
Supplements
Rights
Redistribution rights reserved



Computing the Mandelbrot set with the Wolfram Language
Computing the Mandelbrot set with the Wolfram Language
A Modern Approach
by Arnoud Buzing
What is the Mandelbrot set
What is the Mandelbrot set
The Mandelbrot set is the set of complex points c for which the following sequence does not diverge:
z
0
z
n+1
z
n
It is known that this sequence diverges whenever . Computer algorithms that implement this sequence use this fact as a stopping condition. Because many starting points do not diverge, computer algorithms also stop after a limited number of iterations. The number of iterations for each starting point is often used to make colorful visualizations. The color indicates the number of iterations a starting point remains inside the complex disk .
>2
z
n
<2
z
n
The Wolfram Language has a built-in visualization function for the Mandelbrot set. In this plot only the black points are considered to be part of the set. All other points (non-black points) simply represent how slow or how fast the sequence escapes to infinity:
In[]:=
MandelbrotSetPlot[]
Out[]=
In this notebook we look at different ways to create the above visualization from scratch in the Wolfram Language.
Basic implementation in the Wolfram Language
Basic implementation in the Wolfram Language
The implementation below uses two stopping conditions to exit the While loop. The first stopping condition (n<100) is used when the sequence stays close to the origin (Abs[z]<2) for many iterations. The second stopping condition is used when the sequence diverges (Abs[z] becomes >2). The implementation returns the number of iterations that was spent inside the While loop. This number is used as an indicator about how slow or fast a point diverged
In[]:=
f[c_]:=Module[{n=1,z=0.0+0.0*I},While[n<=100&&Abs[z]<2,n=n+1;z=z^2+c];n]
The point 0.1+0.1 is in the Mandelbrot set and so the algorithm exits when it hits the maximum number of iterations:
In[]:=
f[0.1+0.1*I]
Out[]=
101
The point 0.9+0.9 is not in the Mandelbrot set and so the algorithm exits when it reaches stopping condition 1:
In[]:=
f[0.9+0.9*I]
Out[]=
3
We can now make a basic Mandelbrot set plot:
In[]:=
ArrayPlot[Table[f[x+y*I],{y,-2,2,.03},{x,-2,2,.03}]]//AbsoluteTiming
Out[]=
0.351807,
Calculating a million points is not very fast:
In[]:=
Do[f[0.0+0.0*I],10^6]//AbsoluteTiming
Out[]=
{106.157,Null}
Compiled Wolfram Language
Compiled Wolfram Language
This is the same algorithm as above, but uses the Wolfram Compiler to generate faster code:
In[]:=
cf=FunctionCompile[Function[{Typed[c,"ComplexReal64"]},Module[{n=1,z=0.0+0.0*I},While[n<=100&&Abs[z]<2,n=n+1;z=z^2+c];n]]]
Out[]=
CompiledCodeFunction
|
In[]:=
cf[0.1+0.1*I]
Out[]=
101
In[]:=
cf[0.9+0.9*I]
Out[]=
3
The compiled function is two orders of magnitude faster than the first method:
In[]:=
Do[cf[0.0+0.0*I],10^6]//AbsoluteTiming
Out[]=
{0.831221,Null}
This lets you create more detailed renderings in less time:
In[]:=
ArrayPlot[Table[cf[x+y*I],{y,-2,2,.03},{x,-2,2,.03}]]
Out[]=
Refinements and tweaks
Refinements and tweaks
This refinement lets you specify two boundary points (e.g. bottom-left and top-right) and a small step size to generate a table of values with the compiled version:
In[]:=
cf=FunctionCompile[Function[{Typed[c1,"ComplexReal64"],Typed[c2,"ComplexReal64"],Typed[h,"Real64"]},Table[Module[{c=re+I*im,n=1,z=0.0+0.0*I},While[n<=100&&Abs[z]<2,n=n+1;z=z^2+c];n],{im,Im[c1],Im[c2],h},{re,Re[c1],Re[c2],h}]]]
Out[]=
CompiledCodeFunction
|
This speeds up computation even more, because all points are now computed with a compiled Table command.
In[]:=
cf[-2.0-2.0*I,2.0+2.0*I,0.03];//AbsoluteTiming
Out[]=
{0.003783,Null}
In[]:=
Image[1-Rescale[cf[-2.0-2.0*I,2.0+2.0*I,0.01]]]
Out[]=
Creating full-color images
Creating full-color images
To create full color images, we return color-triplets. In this case, the color-triplet is a HSB value (Hue-Saturation-Value color space).
In[]:=
cf=FunctionCompile[Function[{Typed[c1,"ComplexReal64"],Typed[c2,"ComplexReal64"],Typed[h,"Real64"],Typed[m,"Integer64"]},Table[Module[{c=re+I*im,n=1,z=0.0+0.0*I},While[n<=m&&Abs[z]<2,n=n+1;z=z^2+c];{n/N[m],1.0-n/N[m],1.0}],{im,Im[c2],Im[c1],-h},{re,Re[c1],Re[c2],h}]]]
Out[]=
CompiledCodeFunction
|
In[]:=
data=cf[-2.0-2.0*I,2.0+2.0*I,0.01,100];
In[]:=
Image[data,ColorSpace"HSB"]
Out[]=


Cite this as: Arnoud Buzing, "Computing the Mandelbrot set with the Wolfram Language" from the Notebook Archive (2021), https://notebookarchive.org/2021-08-ebi6zry

Download

