Learn › Beginner · Lesson 2 of 11

Two dimensions, and pictures

A value can have several dimensions. Over two dimensions, x and y, it has a result at every point of a grid, and the playground draws it as a picture: one cell per point.

-- A value over two dimensions.
grid = restrict 10 * #x + #y to (x : 10, y : 10) end
on_diagonal = restrict #x == #y to (x : 10, y : 10) end
-- ask grid[x = 3, y = 4]
-- expect grid[x = 3, y = 4] = 34
-- expect on_diagonal[x = 2, y = 2] = true
-- expect on_diagonal[x = 2, y = 5] = false

Open in playground

How to read it:

Try it in the playground

  1. The answer is there as the program opens, with no button to press: grid[x = 3, y = 4] is 34.
  2. In Visualize, grid is a picture: each cell's colour shows its value. Point at a cell to read its value; click it to query it.
  3. Open View ▸ under the picture and, beside Picture over, choose which dimension goes across and which goes up (or press ⇄ Swap), and watch the picture turn.
  4. Query on_diagonal at x = 2, y = 2: true. Its picture is a diagonal line, with true and false in two colours.
  5. Change on_diagonal to #x + #y == 9: the picture redraws by itself, the other diagonal.

Pictures you can zoom into

The Mandelbrot set on the home page is one program for every zoom, not a program per picture. The zoom has a dimension of its own, level, and the distance between points halves at each level, so the point (level L, x, y) is the same place as (level L + 1, 2x, 2y). Zooming in is the same program asked at the next level.

The Mandelbrot set at every level

-- The Mandelbrot set: each level twice as close as the one before.
step = 0.005 / pow(2, #level)
steps = min(2000, round(200 * (1 + log10(pow(4, floor(#level / 2))))))
cx = -4 + #x * step
cy = -4 + #y * step
escaped = zr * zr + zi * zi > 1048576
zr = restrict 0 fby.k (if escaped then zr else zr * zr - zi * zi + cx) to (k : 2001) end
zi = restrict 0 fby.k (if escaped then zi else 2 * zr * zi + cy) to (k : 2001) end
n_steps = restrict 0 fby.k (if escaped then n_steps else n_steps + 1) to (k : 2001) end
shade = if escaped then n_steps + 1 - log(log(zr * zr + zi * zi) / log(4)) / log(2) else steps
-- ask n_steps[level = 1, k = 200, x = 1302, y = 1648]
-- expect n_steps[level = 1, k = 200, x = 1302, y = 1648] = 61
-- expect n_steps[level = 2, k = 320, x = 2604, y = 3296] = 61
-- expect n_steps[level = 0, k = 200, x = 800, y = 800] = 200
-- expect steps[level = 1] = 200
-- expect steps[level = 2] = 320

Open in playground

How to read it:

In the playground:

  1. Open it: the picture and the answer, 61, are there by themselves.
  2. Press + Zoom in: the playground asks the same program at the next level, with the addresses doubled. − Zoom out asks the level before, and a double-click on the picture zooms in there. Drag the picture to see more of the plane at the same level. Each is a new question; only the points in the picture are computed.
  3. One edit makes a relative of the set. For the Tricorn, in zi, change 2 * zr * zi to -2 * zr * zi. For the Celtic fractal, in zr, change zr * zr - zi * zi + cx to abs(zr * zr - zi * zi) + cx. The picture redraws by itself; press − Zoom out to see the whole shape, then double-click any edge to zoom into it. The Examples menu has both, and the cubic Multibrot.
  4. Keep pressing + Zoom in and it stops, at about level 33, over four billion times closer than level 1, with "As close as this picture goes". The program's numbers are 64-bit floating-point numbers, about 16 significant digits. Deeper than that, an address passes 10^13, and the coordinates cx and cy of neighbouring points differ only in their 13th or 14th digit: the numbers can no longer tell them apart well enough to draw them.

Ask your own AI

Using eLucid8's reference at https://elucid8-lang.org/llms.txt, write a Beginner eLucid8 program (equations only: no input or output lines) whose picture is a checkerboard on an 8 × 8 grid. Compile it with the elucid8 tools before you show it, and give me its Open in playground link.