Chapter 19 Visualization of the Mandelbrot Set

19.1 The Mandelbrot Set

IterationsToLeave[zr_, zi_, cr_, ci_] := Module[
	{cnt, nzr, temp, nzi, ncr, nci}, 
	nzr = N[zr]; nzi = N[zi]; ncr = N[cr]; nci = N[ci];
	For[cnt = 0, 
		(nzr^2 + nzi^2 < 10000) && (cnt < 100), 
		cnt++,
		temp = nzr^2  - nzi^2 + ncr; 
		nzi = 2*nzr*nzi + nci;
		nzr = temp
		];
	cnt
];


IterationsToLeave[0, 0, 1, 0.5]

fractaldata = Table[IterationsToLeave[0, 0, x, y],
{x, -3.0, 1.0, 0.2}, {y, 0.0, 1.0, 0.2}];
ListDensityPlot[fractaldata, ColorFunction -> (Hue[#]&)];

FractalM[z0r_, z0i_, {{ReMin_, ReMax_}, {ImMin_, ImMax_}}, 
			steps_] :=
	{{{ReMin, ReMax}, {ImMin, ImMax}},
	Table[
	IterationsToLeave[z0r, z0i, x, y],
	{y, ImMin, ImMax, (ImMax - ImMin)/steps},
	{x, ReMin, ReMax, (ReMax - ReMin)/steps}
		]}

Colourit[x_] := If[x == 1, Hue[1, 1, 0], Hue[5*x/6]]

fractalPlot[{{{ReMin_, ReMax_}, {ImMin_, ImMax_}}, 
	matrix_}] :=
		ListDensityPlot[matrix, 
		MeshRange -> {{ReMin, ReMax},{ImMin, ImMax}},
		Mesh -> False,
		Frame -> False,
		PlotRange -> {1, 100},
		AspectRatio -> Automatic,
		ColorFunction -> (Colourit[#]&)]

fractaldata = 
	FractalM[0.0, 0.0, {{-3.0, 1.0}, {0.0, 1.0}}, 8]

fractalPlot[fractaldata];

19.2 The Mathematica Compiler

NITL = Compile[{zr, zi, cr, ci},
Module[{cnt, nzr, nzi, temp}, nzr = zr; nzi = zi;
	For[cnt = 0, 
		(nzr^2 + nzi^2 < 10000) && (cnt < 100),
		cnt++, 
		temp = nzr^2  - nzi^2 + cr; 
		nzi = 2*nzr*nzi + ci;
		nzr = temp
		]; cnt]]

FractalMC[z0r_, z0i_, {{ReMin_, ReMax_}, {ImMin_, ImMax_}}, 
	steps_] :=
	{{{ReMin, ReMax}, {ImMin, ImMax}},
	Table[
	NITL[z0r, z0i, x, y],
	{y, ImMin, ImMax, (ImMax - ImMin)/steps},
	{x, ReMin, ReMax, (ReMax - ReMin)/steps}]}

Timing[fractalPlot[FractalM[0, 0, {{-3, 1}, {0, 2}}, 4]];]

Timing[fractalPlot[FractalM[0, 0, {{-3, 1}, {0, 2}}, 8]];]

Timing[fractalPlot[FractalMC[0, 0, {{-3, 1}, {0, 2}}, 8]];]

19.3 Use of MathLink

Install["dfractal", LinkMode -> Connect]

?FractalML

FractalML[0.0,0.0,{{-3.0,1.0},{0.0,2.0}},5]

FractalMLfull[z0r_, z0i_, 
	{{ReMin_, ReMax_}, {ImMin_, ImMax_}}, 
	steps_] :=
	{{{ReMin, ReMax}, {ImMin, ImMax}},
	FractalML[z0r, z0i, {{ReMin, ReMax}, {ImMin, ImMax}}, 
	steps]}

FractalMLfull[0.0,0.0,{{-3.0,1.0},{0.0,2.0}},5]

Timing[fractalPlot[
FractalMLfull[0.0, 0.0, {{-3.0, 1.0}, {0.0, 2.0}}, 8]]]

Timing[FractalMC[0, 0, {{-3, 1}, {0, 2}}, 50]; ]

Timing[
FractalMLfull[0.0, 0.0, {{-3.0, 1.0}, {0.0, 2.0}}, 50]; ]

Timing[fractalPlot[
FractalMC[0.0, 0.0, {{-3.0, 1.0}, {0.0, 2.0}}, 50]]]

Timing[fractalPlot[
FractalMLfull[0.0, 0.0, {{-3.0, 1.0}, {0.0, 2.0}}, 50]]]

fractalPlot[
FractalMLfull[0.0, 0.0, {{-3.0, 1.0}, {0.0, 2.0}}, 200]];

19.4 Further Optimization with MathLink

Install["rast",LinkMode -> Connect]

?FractalML

FractalML[0.0,0.0,{{-2.0,1.0},{-1.5,1.5}},3]

We show these color directives as a RasterArray:

Show[Graphics[RasterArray[%], AspectRatio -> 1]];

19.5 A Visit to MathMovies

f[n_] := 0.8^n

g[n_] := {{-1.40835915-f[n],-1.40835915+f[n]},
			{0.13627737-f[n],0.13627737+f[n]}}

g[0]

plot[n_]:=Show[Graphics[RasterArray[
		FractalML[0.0, 0.0, g[n], 216]], 
		AspectRatio -> 1]]

Do[plot[n],{n,-3, 30}]

plot[-3];

plot[8];

plot[19];

plot[30];

19.5.1 C Code and MathLink Template Files