Fractals/Iterations in the complex plane/demm

This algorithm has 2 versions :
 * exterior
 * interior

Compare it with version for dynamic plane and Julia set : DEM/J

=Exterior DEM/M= Distance Estimation Method for Mandelbrots set ( DEM/M ) estimates distance from point $$c\,$$ ( in the exterior of Mandelbrot set ) to nearest point in Mandelbrot set.

Names
 * the (directional) distance estimate formula

It can be used to create B&W images from BOF :
 * map 41 on page 84
 * map 43 on page 85
 * an unnumbered plot on page 188

types

 * analytic DE = DEM = true DE
 * Claude DE = fake DE ( FDE):  FDE=1/(g*log(2)) when g>0 and undefined when g=0 (i.e., in interior)

Math formule
Math formula :


 * $$distance(c_0,\sigma M) =\lim_{n \to \infty} 2 \frac{|z_n|}{|z'_n|}\ln|z_n|$$

where :


 * $$z_n = f_c^n(z=0.0)$$


 * $$z_n' = \frac{d}{dc} f_c^n(z_0).$$

is a first derivative of $$f_c^n(z_0)$$ with respect to c which can be found by iteration starting with


 * $$z_0' = \frac{d}{dc} f_c^0(z_0) = 1$$

and then replacing at every consecutive step


 * $$z_{n+1}' = \frac{d}{dc} f_c^{n+1}(z_0) = 2\cdot{}f_c^n(z)\cdot\frac{d}{dc} f_c^n(z_0) + 1 = 2 \cdot z_n \cdot z_n' +1.$$

Escape radius

 * "you need to change "If mag > 2" to increase the escape radius. Attached is a comparison, showing that the "straps" reduce when the radius is increased, no harm going huge like R = 1e6 or so because once it escapes 2 it grows very fast. The bigger the radius, the more accurate the distance estimate." Claude
 * Basically in testing magnitude squared against bailout
 * if your bailout is 10^4 then your DE estimates are accurate to 2 decimal figures i.e. if your DE is 0.01 then it's only accurate to +/-0.0001,
 * if your bailout is 10^6 then your DE estimates are accurate to 3 decimal figures (David Makin in fractalforums.com)

"For exterior distance estimation, you need a large escape radius, eg 100100. Iteration count limit is arbitrary, with a finite limit some pixels will always be classified as "unknown"." Claude Heiland-Allen

Comparison

 * The Beauty of Fractals
 * The Science of Fractal Images, page 198,
 * Distance Estimator  by Robert P. Munafo

Pseudocode by 	Claude Heiland-Allen foreach pixel c while not escaped and iteration limit not reached dc := 2 * z * dc + 1 z := z^2 + c de := 2 * |z| * log(|z|) / |dz| d := de / pixel_spacing plot_grey(tanh(d))

Code

Analysis of code from BOF
"The Beauty of Fractals" gives an almost correct computer program for the distance estimation shown in the right image. A possible reason that that method did not gain ground is that the procedure in this program is seriously flawed: The calculation of $$z_k$$ is performed (and completed) before the calculation of $$z'_k$$, and not after as it ought to be ($$z'_{k+1}$$ uses $$z_k$$, not $$z_{k+1}$$). For the successive calculation of $$z'_k$$, we must know $$f'(z_k)$$ (which in this case is 2$$z_k$$). In order to avoid the calculation of $$z_k$$ (k = 0, 1, 2, ...) again, this sequence is saved in an array. Using this array, $$z'_{k+1} = 2z_kz'_k + 1$$ is calculated up to the last iteration number, and it is stated that overflow can occur. If overflow occurs the point is regarded as belonging to the boundary (the bail-out condition). If overflow does not occur, the calculation of the distance can be performed. Apart from it being untrue that overflow can occur, the method makes use of an unnecessary storing and repetition of the iteration, making it unnecessarily slower and less attractive. The following remark in the book is nor inviting either: "It turns out that the images depend very sensitively on the various choices" (bail-out radius, maximum iteration number, overflow, thickness of the boundary and blow-up factor). Is it this nonsense that has got people to lose all desire for using and generalizing the method? " Gertbuschmann

So :
 * in the loop, derivative should be calculated before the new z

"I'm finally generating DEM (Distance Estimate Method) based images that I'm happy with. It turns out that my code had a couple of bugs in it. The new code runs reasonably fast, even with all the extra math to compute distance estimates.

The algorithm in S of F I ("The Science of Fractal Images" uses a sharp cut-off from white to black when pixels get close enough to the set, but I find this makes for jagged looking plots. I've implemented a non-linear color gradient that works pretty well.

For pixels where their DE value is threshold>=DE>=0, I scale the value of DE to 1>=DE>=0, then do the calculation: gradient_value = 1 - (1-DE)^n,     ("^n" means to the n power. I wish I could use superscripts!)

"n" is an adjustment factor that lets me change the shape of the gradient curve. The value "gradient_value" determines the color of the pixel. If it's 0, the pixel is colored at the starting color (white, for a B&W plot.) If "gradient_value" is 1, the pixel gets the end color (e.g.

black) For values of n>1, this gives a rapid change in color for pixels that are far from the set, and a slower change in value as DE approaches 0. For values of n<1, it gives a slow color change for pixels far from the set, and rapid change close to the set. For n=1, I get a linear

color gradient. The non-linear gradient lets me use the DE value to anti-alias my plots. By adjusting my threshold value and my adjustment value, I can get good looking results for a variety of images." Duncan C

"All our DE formulas above are only approximations – valid in the limit n→∞, and some also only for point close to the fractal boundary. This becomes very apparent when you start rendering these structures – you will often encounter noise and artifacts.

Multiplying the DE estimates by a number smaller than 1, may be used to reduce noise (this is the Fudge Factor in Fragmentarium).

Another common approach is to oversample – or render images at large sizes and downscale." Mikael Hvidtfeldt Christensen

Example code

 * ultrafractal

Two algorithm in two loops

 * Distance estimation to the Mandelbrot set by Inigo Quilez in shadertoy : Distance estimation to the Mandelbrot set by Inigo Quilez

Here is Java function. It computes in one loop both : iteration $$z_n\,$$ and derivative $$z'_n\,$$. If (on dynamic plane) critical point :
 * does not escapes to infinity ( bailouts), then it belongs to interior of Mandelbrot set and has colour maxColor,
 * escapes then it maybe in exterior or near boundary. Its colour is "proportional" to distance between the point c and the nearest point in the Mandelbrot set. It uses also colour cycling ( (int)R % maxColor ).

Here is cpp function. It gives integer index of color as an output.

Two algorithms in one loop

 * distance rendering for fractals by ińigo quilez in c++

Modifications / optimisations
Creating DEM images can be improved by use of :
 * the Koebe 1/4-theorem
 * gray scale based on distance
 * Fractal forum : Relationship between bailout and accuracy of analytical DE
 * weight in DEM/M
 * if you see double contur of main antenna :
 * increasing resolution does't solve the problem
 * add one pixel to height or change CyMax or CyMin ( add one pixel size).

equalisation
Distance Estimate Equalisation

Program exrdeeq
 * reads DEX and DEY channels
 * outputs Y channel
 * DE less than 0 becomes 0
 * DE greater than 1 becomes 1
 * otherwise Y is the index of DE in the sorted array, scaled to the range 0 to 1

exrdeeq in.exr out.exr

Examples
height=800 width=800 max_iterations=10000 center_r=-1.74920463346 center_i=-0.000286846601561 r_width=3.19E-10
 * B of F map 43 DEM by Duncan Champney
 * This plot is centered on -0.7436636773584340, 0.1318632143960234i at a width of about 6.25E-11
 * Smily Kaleidoscope by Duncan Champney

=Interior distance estimation= Estimates distance from limitly periodic point $$c\,$$ ( in the interior of Mandelbrot set ) to the boundary of Mandelbrot set.

Descriptions : ‎
 * Book : The Science of Fractal Images, Springer Verlag, Tokyo, Springer, New York, 1988 by Heinz-Otto Peitgen, D. Saupe, page 294
 * Interior and exterior distance bounds for the Mandelbrot by Albert Lobo Cusidó
 * interior distance estimate by R Munafo
 * mandelbrot-numerics library : m_d_interior by Claude

Math description
The estimate is given by :


 * $$distance(c,\sigma M) = d(c, z_0, p) = \frac{1-\mid{\frac{\partial}{\partial{z}}f_c^p(z_0)}\mid^2}

{\mid{\frac{\partial}{\partial{c}}\frac{\partial}{\partial{z}}f_c^p(z_0) + \frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^p(z_0) \frac{\frac{\partial}{\partial{c}}f_c^p(z_0)} {1-\frac{\partial}{\partial{z}}f_c^p(z_0)}}\mid}$$ where

$$p$$ is the period = length of the periodic orbit

$$c$$ is the point from parameter plane to be estimated

$$f_c(z)$$ is the complex quadratic polynomial $$f_c(z)=z^2 + c$$

$$f_c^p(z_0)$$ is the $$p$$-fold iteration of $$f_c(z) $$

$$z_0$$ is any of the $$p$$ points that make the periodic orbit ( limit cycle ) $$\{z_0, \dots, z_{p-1} \}$$

4 derivatives of $$f_c^p $$ evaluated at $$z_0, c, p$$ :

$$\frac{\partial}{\partial{c}}\frac{\partial}{\partial{z}}f_c^p(z_0)$$

$$\frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^p(z_0)$$

$$\frac{\partial}{\partial{c}}f_c^p(z_0)$$

$$\frac{\partial}{\partial{z}}f_c^p(z_0)$$

First partial derivative with respect to z
It must be computed recursively by applying the chain rule : Starting points : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^0(z_0) = 1 \\ f_c^0(z_0) = z_0 \end{cases} $$

First iteration : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^1(z_0) = 2*f_c^0(z_0)*\frac{\partial}{\partial{z}}f_c^0(z_0) = 2z_0 \\ f_c^1(z_0) = (f_c^0(z_0))^2 + c = z_0^2 + c \end{cases} $$

Second iteration : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^2(z_0) =2*f_c^1(z_0)*\frac{\partial}{\partial{z}}f_c^1(z_0)= 4z_0^3+4cz_0 \\ f_c^2(z_0) = (f_c^1(z_0))^2 + c = (z_0^2 + c)^2 + c = z_0^4 + 2*c*z_0^2 + c^2 + c \end{cases} $$

General rule for p iteration : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^p(z_0) = 2*f_c^{p-1}(z_0)*\frac{\partial}{\partial{z}}f_c^{p-1}(z_0) \\ f_c^p(z_0) = f_c^{p-1}(z_0)^2 + c \end{cases} $$

First partial derivative with respect to c
It must be computed recursively by applying the chain rule : Starting points : $$ \begin{cases} \frac{\partial}{\partial{c}}f_c^p(z_0) = 0 \\ f_c^0(z_0) = z_0 \end{cases} $$

First iteration : $$ \begin{cases} \frac{\partial}{\partial{c}}f_c^1(z_0) = 2*f_c^0(z_0)*\frac{\partial}{\partial{c}}f_c^0(z_0) + 1 = 1 \\ f_c^1(z_0) = (f_c^0(z_0))^2 + c = z_0^2 + c \end{cases} $$

Second iteration : $$ \begin{cases} \frac{\partial}{\partial{c}}f_c^2(z_0) =2*f_c^1(z_0)*\frac{\partial}{\partial{c}}f_c^1(z_0) + 1 = 2z_0^2 +2c + 1 \\ f_c^2(z_0) = (f_c^1(z_0))^2 + c = (z_0^2 + c)^2 + c = z_0^4 + 2*c*z_0^2 + c^2 + c \end{cases} $$

General rule for p iteration : $$ \begin{cases} \frac{\partial}{\partial{c}}f_c^p(z_0) = 2*f_c^{p-1}(z_0)*\frac{\partial}{\partial{c}}f_c^p(z_0) + 1 \\ f_c^p(z_0) = f_c^{p-1}(z_0)^2 + c \end{cases} $$

Second order partial derivative with respect to z
It must be computed : Starting points : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^0(z_0) = 1 \\ \frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^p(z_0) = 0 \\ f_c^0(z_0) = z_0 \end{cases} $$
 * together with :$$f_c^n $$ and $$\frac{\partial}{\partial{z}}f_c^p(z_0)$$
 * recursively by applying the chain rule

First iteration : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^1(z_0) = 2*f_c^0(z_0)*\frac{\partial}{\partial{z}}f_c^0(z_0) = 2z_0 \\ \frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^p(z_0) = 2\{ (\frac{\partial}{\partial{z}}f_c^0(z_0))^2 + f_c^0(z_0)*\frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^0(z_0)\} = 2\\ f_c^1(z_0) = (f_c^0(z_0))^2 + c = z_0^2 + c \end{cases} $$

Second iteration : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^2(z_0) =2*f_c^1(z_0)*\frac{\partial}{\partial{z}}f_c^1(z_0)= 4z_0^3+4cz_0 \\ \frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^p(z_0) = \\ f_c^2(z_0) = (f_c^1(z_0))^2 + c = (z_0^2 + c)^2 + c = z_0^4 + 2*c*z_0^2 + c^2 + c \end{cases} $$

General rule for p iteration : $$ \begin{cases} \frac{\partial}{\partial{z}}f_c^p(z_0) = 2*f_c^{p-1}(z_0)*\frac{\partial}{\partial{z}}f_c^{p-1}(z_0) \\ \frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^p(z_0) = 2\{ (\frac{\partial}{\partial{z}}f_c^{p-1}(z_0))^2 + f_c^{p-1}(z_0)*\frac{\partial}{\partial{z}}\frac{\partial}{\partial{z}}f_c^{p-1}(z_0)\}\\ f_c^p(z_0) = f_c^{p-1}(z_0)^2 + c \end{cases} $$

Second order mixed partial derivative
$$\frac{\partial}{\partial{c}}\frac{\partial}{\partial{z}}f_c^p(z_0) = 2(\frac{\partial}{\partial{c}} f_c^{p-1}(z_0)*\frac{\partial}{\partial{z}}f_c^{p-1}(z_0) + f_c^{p-1}(z_0)*\frac{\partial}{\partial{c}}\frac{\partial}{\partial{z}}f_c^{p-1}(z_0))$$

Steps

 * choose c
 * check if critical point for given c is periodic ( interior ) or not ( exterior or near boundary or to big period )
 * if not periodic do not use this algorithm ( use exterior version)
 * if periodic compute period and periodic point $$z_0$$
 * using $$ c, z_0, p$$ compute distance using p iteration

Code

 * Java code by Albert Lobo Cusidó
 * Haskell code by Claude and image
 * processing code and description by tit_toinou

Problems
There are two practical problems with the interior distance estimate:
 * first, we need to find $$z_0$$ precisely,
 * second, we need to find $$p$$ precisely.

The problem with $$z_0$$ is that the convergence to $$z_0$$ by iterating $$P_c(z)$$ requires, theoretically, an infinite number of operations.

The problem with period is that, sometimes, due to rounding errors, a period is falsely identified to be an integer multiple of the real period (e.g., a period of 86 is detected, while the real period is only 43=86/2). In such case, the distance is overestimated, i.e., the reported radius could contain points outside the Mandelbrot set.

For interior distance estimation, you need the period, then a number (maybe 10 or so should usually be enough) Newton steps to find the limit cycle.

the interior checking code absolutely requires the reference to be at the nucleus of the island (not any of its child discs, and certainly not some random point)

Optimisation

 * Analogous to the exterior case, once b is found, we know that all points within the distance of b/4 from c are inside the Mandelbrot set.
 * Adaptive super-sampling using distance estimate Adaptive super-sampling using distance estimate by Claude Heiland-Allen

=References=
 * Distance Estimation for Hybrid Escape Time Fractals by Claude Heiland-Allen 2020-04-23
 * Milnor "Self-similarity and hairiness in the Mandelbrot set"	in Computers in Geometry and Topology, 1989.