> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rhymeq.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Cantor Set: Building a Fractal with Interference

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.

```text theme={null}
Level 0:

0------------------------1
```

We divide it into three equal pieces and keep the two outer pieces.

```text theme={null}
Level 1:

0--------        --------1
```

Then we do the same thing again to the two pieces that remain.

```text theme={null}
Level 2:

0---  ---        ---  ---1
```

We can keep repeating this process.

![Figure representing Cantor Set](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=5841a3d6099680d57c971789ac2206c1)

There is also a simple way to describe this pattern using **ternary numbers**.

Ternary numbers use three digits:

```text theme={null}
0   1   2
```

At every step:

* `0` represents the left third.
* `1` represents the middle third.
* `2` represents the right third.

![Figure representing Cantor Set Ternary numbers](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_ternary.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=74bcf7d843901c5e8ce1cf9d8ff206c8)

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:

```text theme={null}
00   02   20   22
```

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:

```python theme={null}
import math
from rhyme import quint

cantor = quint(0)
```

We want to process one ternary digit at a time.

To do that, we need to know the place value of each ternary digit.

```python theme={null}
def digit_place(n: int, i: int) -> int:
    return 3 ** (n - i)
```

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:

```
i = 1       i = 2
  ↓           ↓
  3           1
```

because:

```
3 ** (2 - 1) = 3
3 ** (2 - 2) = 1
```

So for two ternary digits, the first digit represents the `3`s place and the second digit represents the `1`s place.

For example, if we have three ternary digits, the digit places are `9`, `3`, and `1`.

![Figure representing Cantor Set Ternary 202 number](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_dust_202.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=962688ba6f92e3c45345360986310844)

Now we can pair the ternary digits.

For each position, we want to pair:

```text theme={null}
0 ↔ 2
```

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 `3`s place:

```
0  →  6
1  →  7
2  →  8
```

because:

```
6 = 0 + 2 * 3
7 = 1 + 2 * 3
8 = 2 + 2 * 3
```

In the other direction:

```
6  →  0
7  →  1
8  →  2
```

because subtracting `2 * 3` changes the `3`s digit from `2` back to `0`.

We can implement this using `make_digit_pair()`:

```python theme={null}
def make_digit_pair(n: int, i: int):
    place = digit_place(n, i)

    def pair(x: int) -> int:
        digit = (x // place) % 3

        if digit == 0:
            return x + 2 * place

        if digit == 2:
            return x - 2 * place

        return x

    return pair
```

So `pair()` does three things:

```text theme={null}
if the i-th digit of x is 0 → map x to a number y whose i-th digit is 2

if the i-th digit of x is 2 → map x to a number y whose i-th digit is 0

if the i-th digit of x is 1 → map x to itself
```

In the first two cases, `x` and `y` differ only in their `i`-th ternary digit.

For example, when working with the `3`s place:

```text theme={null}
x = 0  = 00
y = 6  = 20
```

So `pair(0)` returns `6`. The `3`s digit changes from `0` to `2`, while the other digit stays the same.

Similarly:

```text theme={null}
pair(6) = 0
```

because the `3`s 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.

```python theme={null}
def make_digit_lr(n: int, i: int):
    place = digit_place(n, i)

    def lr(x: int) -> bool:
        return (x // place) % 3 == 0

    return lr
```

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:

```
0 ↔ 2
```

with `0` identified as the left side.

Now we have everything we need.

## Building the Cantor Set

Let's put the pieces together.

```python theme={null}
def prepare_cantor_ternary(n: int) -> quint:
    cantor = quint(0)

    for i in range(1, n + 1):
        cantor.interfere(
            make_digit_pair(n, i),
            make_digit_lr(n, i),
        )

    return cantor
```

We start with:

```python theme={null}
cantor = quint(0)
```

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:

```
i = 1
 ↓
3s place
 ↓
interfere
 ↓
i = 2
 ↓
1s place
 ↓
interfere
```

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:

```python theme={null}
cantor1 = prepare_cantor_ternary(1)
```

![Debugger showing the two surviving values](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_cantor1.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=7e6583af7cccfe2eb24f30e7cbd1f62e)

At the next level, level 2:

```python theme={null}
cantor2 = prepare_cantor_ternary(2)
```

![Debugger showing the surviving values](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_cantor2.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=65e25c8420281252281b3086c350fd6b)

At level 3:

```python theme={null}
cantor3 = prepare_cantor_ternary(3)
```

![Debugger showing the surviving values](https://mintcdn.com/rhyme-q/JlURJlNdnA85HOaP/images/basic-tutorials/cantor_set_cantor3.png?fit=max\&auto=format\&n=JlURJlNdnA85HOaP\&q=85\&s=ad7d82cc87e56981f1b770d8d4dd236c)

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:

```text theme={null}
000
002
020
022
200
202
220
222
```

Notice something?

There is never a `1`.

Every ternary digit is either:

```text theme={null}
0 or 2
```

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:

```text theme={null}
0 → 0 or 2
2 → 0 or 2
```

So the number of values grows like:

$$
1\,2\,4\,8,\ldots
$$

while the number of possible ternary values at each level grows like:

$$
3\,9\,27\,81,\ldots
$$

This is what makes the construction interesting.

We use **ternary positions** to build a pattern whose values contain only `0`s and `2`s.

## The whole construction

The idea is simple:

```text theme={null}
Start with 0
     ↓
Choose a ternary digit
     ↓
Pair 0 with 2
     ↓
Interfere
     ↓
Move to the next digit
     ↓
Repeat
```

We start from `0` and grow the pattern one ternary digit at a time.

At every position, `0` and `2` form a pair:

```
0 ↔ 2
```

while a `1` maps to itself.

Repeating this for every ternary digit generates values whose ternary representation contains only `0`s and `2`s.

For example, after three levels we get:

```
000
002
020
022
200
202
220
222
```

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!**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.