## Mandelbrot Set ASCII Viewer

The Mandelbrot Set is the set of all complex points which, when one iteratively and infinitely applies the function $f_c(z)=z^2+c$, converge to a value. This simple rule results in stunning complexity and beauty.
Many Mandelbrot Set animations use regularly colored pixels to represent the number of iterations needed at the fractal’s edges to escape converging. Yet this mathematical object can also be represented as ASCII characters — similar to what I did in my Curses Cam post. The characters are chosen according to their opaqueness. A full stop (‘.’) looks lighter than a dollar sign (‘$’), so they represent a smaller or larger number of iterations needed. The order of characters used is taken from this post by Paul Borke. As there are only 70 characters used, each frame is being rendered twice to determine the minimum number of iterations needed by every point in that frame. Thereby the full visual character range is used. The characters shown below represent a Mandelbrot Set still. To see the zoom in action, either run the program (listed below) or take a look at this Mandelbrot Set ASCII journey.  ..................''''''''"">>II''''...... ..................''''''''^^,,ii::^^''''...... ..................''''''''^^::ww$$++,,''''...... ................''''''''^^^^""::$$::""^^''''...... ..............''''''""{{;;XXuuUU,,,,""''...... ............''''^^,,rr<<$$--........ ........''^^""LL$$$$__""''...... ..''''''^^!!"""",,""""::__$$ll""''........ ''''^^::__IIYYii::llpp^^''........ ''"";;[[$$++__$$^^''''...... ^^^^,,;;>>ww''''...... "",,,,II$$nn$$$$""''''...... "",,,,II$$nn""''''...... ^^^^,,;;>>ww''''...... ''"";;[[$$++__$$^^''''...... ''''^^::__IIYYii::llpp^^''........ ..''''''^^!!"""",,""""::__$$ll""''........ ........''^^""LL$$$$__""''...... ............''''^^,,rr$$$$<<$$--........ ..............''''''""{{;;XXuuUU,,,,""''...... ................''''''''^^^^""::$$::""^^''''...... ..................''''''''^^::ww$$++,,''''...... ..................''''''''^^,,ii::^^''''...... The fractal viewer is written in Python 2.7 and works by determining the terminal’s size and then printing a string of according size. This creates the illusion of a moving image, as the terminal will hopefully always perfectly scroll so that only one frame is visible at a time. In the code’s first non-comment line one can change the complex point at the image’s center, (really, its conjugate, which is partially irrelevant as the set is symmetric along the real axis) the initial zoom value (complex distance above the image’s center), the zoom factor (the factor by which the zoom value gets multiplied after a frame), the total number of frames (-1 means there is no upper limit), the delay between frames (in seconds, can be floating-point) and the color characters used. The program’s source code may not be particularly easy to read, yet it does its job and only requires seven non-comment lines! The code is shown below, though the .py file can also be downloaded. To achieve the JavaScript animation linked to above, I wrote a simple Python converter which takes in the fractal renderer’s output and it spits out an HTML page. This converter’s code is not listed, though the .py file can be downloaded. Instructions on how to use the converter can be seen in its source code. # Python 2.7 Code; Jonathan Frech, 15th and 16th of June 2017 P,Z,F,N,D,K=-.707+.353j,3,.9,-1,.1," .'^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"
def C(c):
global m;z,i=0j,-1
while abs(z)<=2 and i<len(K)-1+M:z,i=z*z+c,i+1
m=min(m,i);return K[i-M]*2
while n<N or N==-1:h=Z*2.;w=h*W/H;R=lambda:"\n\n"*(n!=0)+"\n".join("".join(C(P-complex(w/2-w*x/W,h/2-h*y/H))for x in range(W))for y in range(H));M,m=0,len(K);R();M=max(M,m);S.write(R());S.flush();Z,n=Z*F,n+1;time.sleep(D)

## Multibrot Set

The Mandelbrot Set is typically defined as the set of all numbers $c \in \mathbb{C}$ for which — with $z_0 = 0$, $z_{n+1} = f_c(z_n)$ and $f_c(z) = z^2 + c$ — the limit $\lim\limits_{n \to \infty} z_n$ converges. Visualizations of this standard Mandelbrot Set can be seen in three of my posts (Mandelbrot Set, Mandelbrot Set Miscalculations and Mandelbrot Set II).

However, one can extend the fractal’s definition beyond only having the exponent $2$ in the function to be $f_c(z)=z^\text{exp}+c$ with $\text{exp} \in \mathbb{R}$. The third post I mentioned actually has some generalization as it allows for $\text{exp} \in \{2,3,4,5\}$, although the approach used cannot be extended to real or even rational numbers.

The method I used in the aforementioned post consists of manually expanding $(a+b\cdot i)^n$ for each $n$. The polynomial $(a+b\cdot i)^3$, for example, would be expanded to $(a^3 - 3 \cdot a \cdot b^2) + (3 \cdot a^2 \cdot b - b^3) \cdot i$.
This method is not only tedious, error-prone and has to be done for every exponent (of which there are many), it also only works for whole-number exponents. To visualize real Multibrots, I had to come up with an algorithm for complex number exponentiation.

Luckily enough, there are two main ways to represent a complex number, Cartesian form $z = a+b\cdot i$ and polar form $z = k\cdot e^{\alpha\cdot i}$. Converting from Cartesian to polar form is simply done by finding the number’s vector’s magnitude $k = \sqrt{a^2+b^2}$ and its angle to the x-axis $\alpha = \mbox{atan2}(\frac{a}{b})$. (The function $\mbox{atan2}$ is used in favor of $\arctan$ to avoid having to divide by zero. View this Wikipedia article for more on the function and its definition.)
Once having converted the number to polar form, exponentiation becomes easy as $z^\text{exp} = (k \cdot e^{\alpha\cdot i})^\text{exp} = k^\text{exp} \cdot e^{\alpha \cdot \text{exp} \cdot i}$. With the exponentiated $z^\text{exp}$ in polar form, it can be converted back in Cartesian form with $z^\text{exp} = k^\text{exp} \cdot (\cos{(\alpha \cdot \text{exp})} + \sin{(\alpha \cdot \text{exp})} \cdot i \big)$.

Using this method, converting the complex number to perform exponentiation, I wrote a Java program which visualizes the Multibrot for a given range of exponents and a number of frames.
Additionally, I added a new strategy for coloring the Multibrot Set, which consists of choosing a few anchor colors and then linearly interpolating the red, green and blue values. The resulting images have a reproducible (in contrast to randomly choosing colors) and more interesting (in contrast to only varying brightness) look.

The family of Multibrot Sets can also be visualized as an animation, showing the fractal with an increasing exponent. The animated gif shown below was created using ImageMagick’s convert -delay <ms> *.png multibrot.gif command to stitch together the various .png files the Java application creates. To speed up the rendering, a separate thread is created for each frame, often resulting in 100% CPU-usage. (Be aware of this should you render your own Multibrot Sets!)

To use the program on your own, either copy the source code listed below or download the .java file. The sections to change parameters or the color palette are clearly highlighted using block comments (simply search for ‘/*’).
To compile and execute the Java application, run (on Linux or MacOS) the command javac multibrot.java; java -Xmx4096m multibrot in the source code’s directory (-Xmx4096m tag optional, though for many frames at high quality it may be necessary as it allows Java to use more memory).
If you are a sole Windows user, I recommend installing the Windows 10 Bash Shell.

// Java 1.8 Code
// Jonathan Frech, 11th of September 2016
//          edited 17th of April     2017
//          edited 18th of April     2017
//          edited 20th of April     2017
//          edited 21st of April     2017
//          edited 22nd of April     2017

## Bifurcation Diagram

Generating the famous fractal, which can be used to model populations with various cycles, generate pseudo-random numbers and determine one of nature’s fundamental constants, the Feigenbaum constant $\delta$.
The fractal nature comes from iteratively applying a simple function, $f(x) = \lambda \cdot x \cdot (1-x)$ with $0 \leq \lambda \leq 4$, and looking at its poles.
The resulting image looks mundane at first, when looking at $0 \leq \lambda \leq 3$, though the last quarter section is where the interesting things are happening (hence the image below only shows the diagram for $2 \leq \lambda \leq 4$).
From $\lambda = 3$ on, the diagram bifurcates, always doubling its number of poles, until it enters the beautiful realm of chaos and fractals.

For more on bifurcation, fractals and $\delta$, I refer to this Wikipedia entry and WolframMathworld.

# Python 2.7.7 Code
# Jonathan Frech, 24th of March 2017`