Skip to main content
In the previous tutorials, we learned how amplitudes can combine using interfere(). Now let’s use interfere() to build a famous mathematical pattern: the Cantor Set. (Note: in 1-dimension, this fractal is called the Cantor Set. When we build the same kind of fractal in more dimensions, we call it Cantor Dust. We’re building the 1-dimensional version here, so we’ll call it the Cantor Set from now on.)

What is the Cantor Set?

Let’s start with a line.
We divide it into three equal pieces and keep the two outer pieces.
Then we do the same thing again to the two pieces that remain.
We can keep repeating this process.
Figure representing Cantor Set
There is also a simple way to describe this pattern using ternary numbers. Ternary numbers use three digits:
At every step:
  • 0 represents the left third.
  • 1 represents the middle third.
  • 2 represents the right third.
Figure representing Cantor Set Ternary numbers
Since the Cantor Set keeps the left and right thirds, the values in the construction have ternary representations containing only 0 and 2. For example, at level 2:
Every digit is either 0 or 2. Now let’s see how we can create this pattern in Rhyme.

Building one ternary digit

We start with a single value:
We want to process one ternary digit at a time. To do that, we need to know the place value of each ternary digit.
Here:
  • n is the total number of ternary digits.
  • i is the position of the digit we are currently processing.
  • We start counting i from 1.
For example, if we have two ternary digits, then n is 2. The two digit positions are:
because:
So for two ternary digits, the first digit represents the 3s place and the second digit represents the 1s place. For example, if we have three ternary digits, the digit places are 9, 3, and 1.
Figure representing Cantor Set Ternary 202 number
Now we can pair the ternary digits. For each position, we want to pair:
while a number whose i-th ternary digit is 1 maps to itself. We can see this more clearly by looking at the values from 0 to 8. For the 3s place:
because:
In the other direction:
because subtracting 2 * 3 changes the 3s digit from 2 back to 0. We can implement this using make_digit_pair():
So pair() does three things:
In the first two cases, x and y differ only in their i-th ternary digit. For example, when working with the 3s place:
So pair(0) returns 6. The 3s digit changes from 0 to 2, while the other digit stays the same. Similarly:
because the 3s digit changes from 2 back to 0. If the i-th digit of x is 1, then pair(x) returns x itself, so its amplitude does not change during this step. We also need to tell interfere() which value comes first in the pair.
Here, lr() tells interfere() that the value with 0 in the current digit position is the left value of the pair. So for each ternary digit, we have:
with 0 identified as the left side. Now we have everything we need.

Building the Cantor Set

Let’s put the pieces together.
We start with:
So we begin with just the value 0. The for loop then processes each ternary digit, starting with i = 1. For every digit position, we:
  1. find its ternary place,
  2. pair 0 with 2,
  3. identify 0 as the left value,
  4. let the paired values interfere.
For example, with two ternary digits:
The first interference produces the two possibilities for the first ternary digit. The second interference extends each of them with two possibilities for the second digit. So the pattern grows one digit at a time. Let’s start with the first level:
Debugger showing the two surviving values
At the next level, level 2:
Debugger showing the surviving values
At level 3:
Debugger showing the surviving values
We can continue for more levels. As we can see in the debugger, the amplitudes are equally divided among the values generated by the construction.

Looking at the pattern

At level 3, the generated values correspond to these ternary numbers:
Notice something? There is never a 1. Every ternary digit is either:
This comes directly from the way we build the pattern: at every digit position, 0 and 2 are paired, while a digit 1 does not create a new branch. There is also a nice pattern in the number of values. At each new level, every generated value can be extended in two ways:
So the number of values grows like: 1 2 4 8,…1\,2\,4\,8,\ldots while the number of possible ternary values at each level grows like: 3 9 27 81,…3\,9\,27\,81,\ldots This is what makes the construction interesting. We use ternary positions to build a pattern whose values contain only 0s and 2s.

The whole construction

The idea is simple:
We start from 0 and grow the pattern one ternary digit at a time. At every position, 0 and 2 form a pair:
while a 1 maps to itself. Repeating this for every ternary digit generates values whose ternary representation contains only 0s and 2s. For example, after three levels we get:
These are the finite levels of the Cantor Set construction. In the next tutorial, we’ll try a different approach: instead of growing the pattern by pairing values, we’ll use phase to build the Cantor Set. That’s the Cantor Set in Rhyme!