# Mandelbrot: SETL & Lisp

SETL is a general-purpose programming language developed by Jack Schwartz back in the late 1960s. The language is multiparadigm – both Procedural (containing subroutines and control structures like if statements) and Set Theoretical. The latter is not often mentioned when talking about language paradigms – and is not to be confused with Logical Programming. The concept is outlined in the 1974 paper An Introduction to the Set Theoretical Language SETL. Importantly though data in SETL is of three main types: Atoms, Sets, and Sequences (also referred to as tuples or ordered sets).

A good overview of SETL can be found in the GNU SETL Om.

Pixels inside the Mandelbrot Set are marked in black, the code used to generate them (mandelbrot.setl) can be found here.

Generation of the Mandelbrot Set
On the Wikipedia page for the Mandelbrot Set you will find its formal definition, which looks like so: $M = \left\{c\in \mathbb C : \exists s\in \mathbb R, \forall n\in \mathbb N, |P_c^n(0)| \le s \right\}.$

The most fantastic thing about SETL is how powerful its Set Builder Notation is. Almost exactly as you would in the formal case (removing ‘there exists some real s’ for the practical application):

M := {c : c in image | forall num in {0..iterations} | cabs(pofc(c, num)) &lt;= 2};


For some reason SETL doesn’t support complex numbers, but they are easily handled by writing the necessary procedures we need like cabs, ctimes, and cplus dealing with tuples in the form [x, y]. The variable ‘image’ is a sequence of these tuples. Another procedure is written, pofc, which is a more practical version of $P_c^n(0)$.

Interaction with the Lisp graphical display
The goal of the SETL program is to produce a set of points that lie inside the Mandelbrot set. To actually display the image I used Common Lisp and displayed a widget with the image using CommonQt. In a very rudimentary way I had mandelbrot.setl take arguments about the image then print the set of pixel co-ordinates. All lisp had to do was swap and ‘{}’ or ‘[]’ for ‘()’ and read it as lisp code then update the image.

(setf in-set (read-from-string (parse-line (uiop:run-program "setl mandelbrot.setl -- -250 -250 250 8" :output :string))))
(draw-mandel instance in-set)


An after-thought was to make a Mandelbrot searcher where you could zoom and move using the mouse but the SETL code is such an inefficient way of doing it that it’s not worth it. As an attempt to mimic the formal definition it was highly successful and fun. Though much quicker SETL code could be written for generating the Mandelbrot Set.

Source file for mandelbrot.setl and the Lisp/CommonQt front end can be found here.

# Accuracy of Generated Fractals

Note: I refer to the Mandelbrot set in general as the M-set for short.

When I was writing the post on Rough Mandelbrot Sets I tried out some variations on the rough set. One variation was to measure the generated M-set against a previously calculated master M-set of high precision (100000 iterations of $z = z^2 + C$). In the image below the master M-set is in white and the generated M-sets are in green (increasing in accuracy):

Here instead of approximating with tiles I measured the accuracy of the generated sets against the master set by pixel count. Where $P = \{ \text{set of all pixels} \}$ the ratio of $P_{master} / P_{generated}$ produced something that threw me, the generated sets made sudden but periodic jumps in accuracy:

Looking at the data I saw the jumps were, very roughly, at multiples of 256. The size of the image being generated was 256 by 256 pixels so I changed it to N by N for N = {120, 360, 680} and the increment was still every ~256. So I’m not really sure why, it might be obvious, if you know tell me in the comments!

I am reminded of the images generated from Fractal Binary and other Complex Bases where large geometric entities can be represented on a plane by iteration through a number system. I’d really like to know what the Mandelbrot Number System is…

Below is a table of the jumps and their iteration index:

 Iterations Accuracy measure 255 256 0.241929 0.397073 510 511 0.395135 0.510806 765 766 0.510157 0.579283 1020 1021 0.578861 0.644919 1275 1276 0.644919 0.679819 1530 1531 0.679696 0.718911

# Fractal Binary

I have previously talked about Complex Bases but I wanted to look again at Base (-1+i). It’s a really hefty number system so the length of the bit-strings increase very quickly, I’d quite like to know if there is a way to assess Radix Economy for complex and negative bases, so if there are any mathematicians out there who know – Please tell me!

Visualising Numbers
Today I wrote a little C++ program to act on Base 2 arithmetic but convert to decimal as if it was Base (-1+i), this meant I could increment through the bits in an ‘ordered’ fashion. The image to the left is the text output of the program, it doesn’t have a very obvious pattern to it – infact the pattern-order we derive from it is somewhat an imposed one. This is because complex numbers do not have a linear order (or Total Order) and I’m trying to list them in a linear manner. They can, on the other hand, be Well-Ordered in correspondence with the natural numbers like we’re doing here.
If we take the real and imaginary values of each number and use them as the x and y co-ordinates (like I did for generating the Mandlebrot Set fractal) then the fractal “Twindragon” appears:

The program I wrote runs through binary numbers starting at 0 colouring the pixel (x=r, y=i) discretely depending on number length. The result shows all Gaussian integers representable by all possible 16,12 and 8 bit complex binary strings in base (-1+i). The colour mapping relates to the position of the Most Significant Bit (essentially the bitstring length). 0 and 1 are both of length one and are the dark blue in the center of the fractal. The 12-bit and 8-bit fractal maps have been zoomed in on to emphases  the self-similarity of the shape.

Colouring the fractals like this is a nice way of showing the distribution of numbers in the complex system but, going back to the math, a number system isn’t useful without arithmetic. Luckily the (-1+i) system is closed under addition, subtraction and multiplication. For addition and multiplication it is the same as normal binary with the difference being in the carry. Below is a table of all possible carry situations:

 1+1 = 1100 1+1+1 = 1101 1+1+1+1 = 111010000 1+1+1+1+1 = 111010001 1+1+1+1+1+1 = 111011100 1+1+1+1+1+1+1 = 111011101 1+1+1+1+1+1+1+1 = 111000000

Division in the systems is rather complex, an explination of that and examples of addition/subtraction/multiplication can be found in a short paper called “Arithmetic in Complex Basis” by William Gilbert. The paper also talks about an equivalent to decimal which is base (-3+i) using the digits [0,1,2,3,4,5,6,7,8,9].

# Complex Bases

Bellow is Donald Knuth, his most famous work is probably The Art of Computer Programming for which the content won him a Turing Award in 1974. He also came up with the Up Arrow notation used in my posts on Large Numbers and God. Those posts were inspired by one of Knuth’s books, this post was inspired by his number system.

The Quater-Imaginary System
As a student at High School he entered into a science talent search and his submission was the Quater-Imaginary system, or Base 2i. It’s interesting because it uses the digits {0,1,2,3} for representation so it would seem at first glance to be quaternary (Base 4) – but it’s not!

It is actually an imaginary base, as the complex version is (0r±2i). Like the decimal system, quater-imaginary can finitely reperesent all positive real integers -but it can do more- it can finitely represent all positive AND negative real AND imaginary integers without signs (i.e., 3i, -8). Which can be seen in this table:

 Base 10 Base 2i Base 10 Base 2i Base 10 Base 2i Base 10 Base 2i 1 1 -1 103 1i 10.2 -1i 0.2 2 2 -2 102 2i 10.0 -2i 1030.0 3 3 -3 101 3i 20.0 -3i 1030.2 4 10300 -4 100 4i 20.0 -4i 1020.0 5 10301 -5 203 5i 30.2 -5i 1020.2

The Dragon System

The Twindragon also known as the Davis-Knuth dragon!

It’s not offically called that, but the second most known complex system is Base (−1±i) which has an associated fractal shape (twindragon). It uses the numbers {0,1} for representation. It’s a very clean and eligant number system that was created by Walter F. Penney in 1965. As with quater-imaginary, this number system can be used to finitely represnt the Gaussian Integers.

Interestingly the radix starts with a negative number, but this isn’t a problem, negative bases work just aswell as positive ones. Infact in 1957 a Polish computer called BINEG was designed using negabinary!

 Base 10 Base (-1±i) Base -2 Base 2 1 1 1 1 2 1100 110 10 3 1101 111 11 4 111010000 100 100 5 111010001 101 101

Base (-1±i√7)/2 and Others

Here the length of the numbers don’t increase monotonically, for example 12(11001100) and 13(11001101) are shorter than their predecessor 11(11100110011) which is the same length as 14(11100010110). It isn’t the only base to hold this attribute but it’s one of the quickest to show it. Source and explination here.

As well as whole-number radix systems it is possible to use fractions and even irrational numbers, one example I go over is Phinary (Base 1.61803…) also known as the Golden Ratio Base. On my Research Page I am looking at a series of systems, starting with the golden ratio, which I call the Metallic Series that can all be used under the same rules.

I find number systems quite interesting and I have started messing around modelling them in different ways, in a previous post (Numeral Automata) I look at them using cellular automata.

# Randolph Diagrams

This is what a genius looks like.

There is something aesthetic and elegant about Randolph diagrams, unfortunately they aren’t commonly used. I found out about them when reading Embodiments of Mind by the glorious and bearded Warren McCulloch.

The Original Proposal:
In the book he refers to them as “Venn functions” and they are briefly explained as being derived from Venn’s diagrams for sets but in McCulloch’s case they were used to express logical statements. If you draw a Venn diagram of two circles intersecting you are left with four spaces ( a/b, a&b, b/a, U ), adding a jot into a space to denote truth or leaving it blank for false gives you the 16 possible logic combinations. They are great examples of the isomorphism between logic and set theory:

He used these as tools to help teach logic to neurologists, psychiatrists and psychologists. Later he developed them into a probablistic logic which he applied to John vonn Neumann‘s logical neuron nets. Which I will discuss in the next post.

Randolph’s Diagrams:

The truth values for three statements.

McCulloch does mention that they could be used to apply more than two statements but doesn’t show how, later John F. Randolph developes the system as an alternative visualisation of set relations neatly coping with more than two sets (something Venn diagrams begin to struggle with after five). For each additional statement/set a new line is introduced in each quadrant. Four statements would be a large cross with four smaller crosses, one in each quadrant.

Wikipedia has an example of the tautological proof for the logical argument, modus ponens, which can be found here, but I thought it would be good to show how three values are handled – so we’ll use syllogism, as in “Socrates is a man, all men are mortal, therefore Socrates is mortal” being reduced in it’s logical form to tautology:

((A implies B) and (B implies C)) implies (A implies C)

# Numeral System Automata

Cellular Automata
Cellular automata Is made up of a grid of cells which sit in a finite number of states (such as on/off or blue/green/red). Cellular automaton (singular) adhear to the rules of the system and evolve over periodic time intervals. One successful application of CA is Conway’s Game of Life, the universe is an infinite 2D grid and the rules are as follows:

A single Gosper's Glider Gun creating "gliders"

1. Any live cell with fewer than two live neighbours dies, as if by needs caused by underpopulation.
2. Any live cell with more than three live neighbours dies, as if by overcrowding.
3. Any live cell with two or three live neighbours lives, unchanged, to the next generation.
4. Any dead cell with exactly three live neighbours cells will come to life.

Positional Number Systems
We use these all the time, notably the decimal system in which when a symbol exceeds 9 it “carries” one to the left and negates ten. 9+1=A, then A goes to 10.

$a_n \dots a_2a_1a_0 = (a_n {\times} b^n) + \dots + (a_2 {\times} b^2) + (a_1 {\times} b^1) + (a_0 {\times} b^0)$
Example: $1250 = (1 \times 10^3) + (2 \times 10^2) + (5 \times 10^1) + (0 \times 10^0)$

The Idea
To take the properties of a number system and create rules to govern individual cells. For our numeral universe we are going to need something different to what Conway used, ours is going to have an intrinsic property, gravity – and thus a floor to gravitate to. The center of our universe will be the radix point (decimal point), you could say the numeral universe is radixocentric. The real-life universe is made up of multiple dimensions, the numeral one is made up of infinite column dimensions.

Although initially I will only being dealing with addition and thus carry, I have put on paper negation in phinary which was quite fun. I talk about the arithmetic of phinary in my research (section 4). The Numeral Universe rules and an example are below:

1. Positional System – Each dimensional element (cell) is the value of the previous dimension’s capacity.
2. Gravity – Any cell above an empty cell moves to it.
3. Carry – When a dimension is full, it moves to the next higher one.

Example in base 3 (ternary). The green and red blocks are the two numbers being added to the universe, the grey block is the carry.

But what does this all mean?
Well, I’m not really sure – it’s nice to show number systems in a different way, it could be useful for teaching. But as for me, I’m using the rules to govern an alternative tetris game, I’ll upload it when I’m finnished.

Also if anyone is wondering how I made those fractal looking blocks, I spammed the hell out of the filters on Pixlr the free online image editor.