# 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\cdotf_{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
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_{k}z'_{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

## Examples

* 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 :
* together with :$f_{c}^{n}$ and ${\frac {\partial }{\partial {z}}}f_{c}^{p}(z_{0})$
* recursively by applying the chain rule
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}}$
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}))$

## Algorithm
### 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

#### Computing distance
## 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.
* ↑ A Cheritat wiki-draw: Mandelbrot_set
* ↑ fractalforums : m-set-distance-estimation
* ↑ fractalforums discussion : How are B&W images from "Beauty of Fractals" created?
* ↑ fractalforums.org : m-brot-distance-estimation-versus-claudes-fake-de
* ↑ Milnor (in Dynamics in One Complex Variable, appendix G)
* ↑ Heinz-Otto Peitgen (Editor, Contributor), Dietmar Saupe (Editor, Contributor), Yuval Fisher (Contributor), Michael McGuire (Contributor), Richard F. Voss (Contributor), Michael F. Barnsley (Contributor), Robert L. Devaney (Contributor), Benoit B. Mandelbrot (Contributor) : The Science of Fractal Images. Springer; 1 edition (July 19, 1988), page 199
* ↑ distance rendering for fractals by ińigo quilez
* ↑ fractalforums : mandelbrot-distance-estimation-problem
* ↑ fractalforums.org : list-of-numbers-with-fixed-digit-length-in-the-mandelbrot-set
* ↑ Distance Estimator by Robert P. Munafo
* ↑ math.stackexchange question: how-to-draw-a-mandelbrot-set-with-the-connecting-filaments-visible
* ↑ Mandelbrot book
* ↑ Fractal forum : How are B&W images from "Beauty of Fractals" created?
* ↑ Distance Estimated 3D Fractals (V): The Mandelbulb & Different DE Approximations by Mikael Hvidtfeldt Christensen
* ↑ distance rendering for fractals by ińigo quilez
* ↑ distance estimation by Claude Heiland-Allen
* ↑ distance rendering for fractals by ińigo quilez
* ↑ fractalforums gallery by Pauldelbrot
* ↑ fractalforums.org : histogram-de-coloring
* ↑ Distance-Estimate-Equalisation by Claude Heiland-Allen
* ↑ B of F map 43 DEM by Duncan Champney
* ↑ Smily Kaleidoscope by Duncan Champney
* ↑ Interior and exterior distance bounds for the Mandelbrot by Albert Lobo Cusidó
* ↑ Interior and exterior distance bounds for the Mandelbrot by Albert Lobo Cusidó
* ↑ Interior coordinates in the Mandelbrot set by Claude
* ↑ Fractal forum : Coloring points inside the Mandelbrot set
* ↑ MandelbrotDE in processing by ttoinou
* ↑ fractalforums : classic-mandelbrot-with-distance-and-gradient-for-coloring