# Speed improvements - optimisation

## Symmetry

The Mandelbrot set is symmetric with respect to the x-axis in the plane :

colour(x,y) = colour(x,-y)


its intersection with the x-axis ( real slice of Mandelbrot set ) is an interval :

<-2 ; 1/4>


It can be used to speed up computations ( up to 2-times )[1]

## Bailout test

Instead of checking if magnitude ( radius = abs(z) ) is greater then escape radius ( ER):

${\displaystyle {\sqrt {Z_{x}^{2}+Z_{y}^{2}}}


compute square of ER:

 ER2 = ER*ER


once and check :

${\displaystyle Z_{x}^{2}+Z_{y}^{2}


It gives the same result and is faster.

## interior detection

Mandelbrot set with Interior detection method

Interior detection method[2]

• time with detection versus without detection is : 0.383s versus 8.692s so it is 23 times faster !!!!
// gives last iterate = escape time
// output 0< i < iMax
int iterate(double complex C , int iMax)
{
int i=0;
double complex Z= C; // initial value for iteration Z0
complex double D = 1.0; // derivative with respect to z

for(i=0;i<iMax;i++)
if(cabs(D)<eps) break; // interior
D = 2.0*D*Z;

}
return i;
}


## Period detection

"When rendering a Mandelbrot or Julia set, the most time-consuming parts of the image are the “black areas”. In these areas, the iterations never escape to infinity, so every pixel must be iterated to the maximum limit. Therefore, much time can be saved by using an algorithm to detect these areas in advance, so that they can be immediately coloured black, rather than rendering them in the normal way, pixel by pixel. FractalNet uses a recursive algorithm to split the image up into blocks, and tests each block to see whether it lies inside a “black area”. In this way, large areas of the image can be quickly coloured black, often saving a lot of rendering time. ... (some) blocks were detected as “black areas” and coloured black immediately, without having to be rendered. (Other) blocks were rendered in the normal way, pixel by pixel." Michael Hogg [3]

 // cpp code by Geek3
// http://commons.wikimedia.org/wiki/File:Mandelbrot_set_rainbow_colors.png
bool outcircle(double center_x, double center_y, double r, double x, double y)
{ // checks if (x,y) is outside the circle around (center_x,center_y) with radius r
x -= center_x;
y -= center_y;
if (x * x + y * y > r * r)
return(true);
return(false);

// skip values we know they are inside
if ((outcircle(-0.11, 0.0, 0.63, x0, y0) || x0 > 0.1)
&& outcircle(-1.0, 0.0, 0.25, x0, y0)
&& outcircle(-0.125, 0.744, 0.092, x0, y0)
&& outcircle(-1.308, 0.0, 0.058, x0, y0)
&& outcircle(0.0, 0.25, 0.35, x0, y0))
{
// code for iteration
}


### Cardioid and period-2 checking

One way to improve calculations is to find out beforehand whether the given point lies within the cardioid or in the period-2 bulb. Before passing the complex value through the escape time algorithm, first check if:

${\displaystyle (x+1)^{2}+y^{2}<{\frac {1}{16}}}$

to determine if the point lies within the period-2 bulb and

${\displaystyle q=\left(x-{\frac {1}{4}}\right)^{2}+y^{2}}$
${\displaystyle q\left(q+\left(x-{\frac {1}{4}}\right)\right)<{\frac {1}{4}}y^{2}.}$

to determine if the point lies inside the main cardioid.

another description [4]

// http://www.fractalforums.com/new-theories-and-research/quick-(non-iterative)-rejection-filter-for-mandelbrotbuddhabrot-with-benchmark/
public static void quickRejectionFilter(BlockingCollection<Complex> input, BlockingCollection<Complex> output)
{
foreach(var item in input.GetConsumingEnumerable())
{
if ((Complex.Abs(1.0 - Complex.Sqrt(Complex.One - (4 * item))) < 1.0)) continue;
if (((Complex.Abs(item - new Complex(-1, 0))) < 0.25)) continue;
if ((((item.Real + 1.309) * (item.Real + 1.309)) + item.Imaginary * item.Imaginary) < 0.00345) continue;
if ((((item.Real + 0.125) * (item.Real + 0.125)) + (item.Imaginary - 0.744) * (item.Imaginary - 0.744)) < 0.0088) continue;
if ((((item.Real + 0.125) * (item.Real + 0.125)) + (item.Imaginary + 0.744) * (item.Imaginary + 0.744)) < 0.0088) continue;

//We tried every known quick filter and didn't reject the item, adding it to next queue.
}
}


### Periodicity checking

Most points inside the Mandelbrot set oscillate within a fixed orbit. There could be anything from ten to tens of thousands of points in between, but if an orbit ever reaches a point where it has been before then it will continually follow this path, will never tend towards infinity and hence is in the Mandelbrot set. This Mandelbrot processor includes periodicity checking (and p-2 bulb/cardioid checking) for a great speed up during deep zoom animations with a high maximum iteration value.

## Perturbation theory

"The thing we call perturbation consist of 2 things: Calculate one pixel with high precision and use it as a reference for all other pixels.

This method will fail though, however thanks to Pauldelbrot we have a trustable method of detecting the pixels where the reference fails to compute the pixel with hardware precision. These pixels can be rendered with a reference closer to these pixels, so a typical perturbation render use several references. This method gives a speedup at about 10 times on depths of 10^100 Use a truncated Taylor series to approximate a starting value for a specific iteration, which make you able to skip all previous iterations. This method gives a speedup of typically Another 10 times on depths of 10^100, and together the speed up is typically 100 times. This, which we call Series Approximation, is where we have issues since we do not have any solid theoretically way of finding when too many iterations are skipped - for all pixels in the view. The more terms you include in the Taylor series, the more iterations you are able to skip. So if you stay below say 50 terms, it is not likely that you ever encounter any issues. Because some views can be correctly rendered 1000 or 100,000 times faster than full precision for each pixel with many terms - can you imagine a month turned into seconds! shocked K.I.Martin originally used only 3 terms " - Kalles Fraktaler [5]

### Description

Very highly magnified images require more than the standard 64-128 or so bits of precision most hardware floating-point units provide, requiring renderers use slow "bignum" or "arbitrary precision"[6] math libraries to calculate. However, this can be sped up by the exploitation of perturbation theory.[7] Given

${\displaystyle z_{n+1}=z_{n}^{2}+c}$

as the iteration, and a small epsilon, it is the case that

${\displaystyle (z_{n}+\epsilon )^{2}+c=z_{n}^{2}+2z_{n}\epsilon +\epsilon ^{2}+c}$

or

${\displaystyle z_{n+1}+2z_{n}\epsilon +\epsilon ^{2}}$

so if one defines

${\displaystyle \epsilon _{n+1}=2z_{n}\epsilon _{n}+\epsilon _{n}^{2}}$

one can calculate a single point (e.g. the center of an image) using normal, high-precision arithmetic (z), giving a reference orbit, and then compute many points around it in terms of various initial offsets epsilon-zero plus the above iteration for epsilon. For most iterations, epsilon does not need more than 16 significant figures, and consequently hardware floating-point may be used to get a mostly accurate image.[8] There will often be some areas where the orbits of points diverge enough from the reference orbit that extra precision is needed on those points, or else additional local high-precision-calculated reference orbits are needed. This rendering method, and particularly the automated detection of the need for additional reference orbits and automated optimal selection of same, is an area of ongoing, active research. Renderers implementing the technique are publicly available and offer speedups for highly magnified images in the multiple orders of magnitude range.[9][10][11]

#### Newton-Raphson zooming

One can "use newton's method to find and progressively refine the precision of the location of the minibrot at the center of a pattern. This allows them to arbitrarily select a magnification between the location they started at and the final minibrot they calculate to be at the center of that location."[12][13]

### history

• 2 independent descriptions :
• Fast Mandelbrot set with infinite resolution by Sergey Khashin (July 9, 2011)[16][17]
• perturbation method by K.I. Martin (2013-05-18)[18] ( only use 3 Series Approximation terms )
• Pauldelbrot's glitch detection method [19]
• extending Martin's description to handle interior distance estimation too by Claude Heiland-Allen[20]