Learn

Contents

From NumPy to eLucid8

NumPy and eLucid8 both compute with values over many coordinates at once. In NumPy a value is an array: its axes are numbered, every element is computed and stored, and a computation over time is a Python loop that steps the array forward. In eLucid8 a value is defined by an equation over named dimensions: a[t = 3, x = 4] names each coordinate, a step in time is fby or a shift, and only the elements a question needs are computed. This page puts the two side by side, with the same data and the same answers.

The NumPy fragments on this page follow the NumPy baselines of the published examples (Examples at full size): examples/simulation/baseline/heat_numpy.py steps a plate of heat with array operations, and examples/sensor-field/baseline/field_numpy.py computes a field of alerts. Each NumPy fragment is shown with what it prints; each eLucid8 example with its data (a -- table or -- data line, as the playground's table or formula) and its stated answers (-- expect). A test runs every fragment and checks every answer on this page, the eLucid8 ones in both of eLucid8's engines.

NumPy and eLucid8, side by side

NumPyeLucid8
a[3, 4]a[t = 3, x = 4]coordinates by name, in any order
a.sum(axis=1)sum a over (x : 6) endan aggregate names the dimension it runs over
np.cumsum(x)x fby.t (total + next.t x)a running sum, defined step by step
np.roll(x, 1), x[:-1]x[t = #t - 1]one step back; eod before the first step, never a wrap-around
np.convolve(x, np.ones(3) / 3, "valid")mean x over (t : #t - 2 .. #t) enda moving window, indexed by the step it ends at
a for loop over time stepsinitial fby.step (…)each step defined from the one before
np.nanan error of kind missing (a missing reading), another error (a failure), or eod (past the end of the data)each says which it is, and where it started
np.nansum(x)sum x over (t : 4) endaggregates leave out missing readings, and say how many
every element computedonly what a question needsa query for one cell computes the cells it depends on

Axes by number, dimensions by name

In NumPy, which axis is time and which is space is the order of the axes, which the program must remember; a.T swaps them. In eLucid8 each coordinate is named, so there is no order to remember, and an aggregate names the dimension it runs over.

import numpy as np

a = np.arange(5)[:, None] * 10 + np.arange(6)[None, :]
print(a[3, 4])
print(a.sum(axis=1))
print(a.T[4, 3])
34
[ 15  75 135 195 255]
34
-- data a = #t * 10 + #x
a = input("a", t : 5, x : 6)

row_total = sum a over (x : 6) end

-- expect a[t = 3, x = 4] = 34
-- expect a[x = 4, t = 3] = 34
-- expect row_total[t = 1] = 75
-- expect row_total[t = 4] = 255

Open in playground

a[x = 4, t = 3] is the same point as a[t = 3, x = 4]. row_total has the dimension t only: the aggregate runs over x.

Running sums, shifts and moving windows

import numpy as np

x = np.array([3.0, 1.0, 4.0, 1.0, 5.0, 9.0])
print(np.cumsum(x))
print(np.roll(x, 1))
print(x[1:] - x[:-1])
print(np.convolve(x, np.ones(3) / 3, mode="valid"))
[ 3.  4.  8.  9. 14. 23.]
[9. 3. 1. 4. 1. 5.]
[-2.  3. -3.  4.  4.]
[2.66666667 2.         3.33333333 5.        ]
-- table x = t,value; 0,3; 1,1; 2,4; 3,1; 4,5; 5,9
x = input("x", t : 6)

total = x fby.t (total + next.t x)
change = x - x[t = #t - 1]
moving = restrict mean x over (t : #t - 2 .. #t) end to (t : 6) end

-- expect total[t = 4] = 14
-- expect total[t = 5] = 23
-- expect total[t = 6] = eod
-- expect change[t = 0] = eod
-- expect change[t = 1] = -2
-- expect change[t = 5] = 4
-- expect moving[t = 0] = 3
-- expect moving[t = 2] = 2.66667
-- expect moving[t = 5] = 5

Open in playground

A time-step loop, and fby

The heat baseline steps a plate forward in a Python loop: each cell that is not held takes the mean of its 3 × 3 neighbourhood at the step before, leaving out the cells beyond the plate. Here on a 4 × 4 plate whose row x = 0 is held at 100, three steps:

import numpy as np

def mean3x3(a):
    padded = np.pad(a, 1, constant_values=np.nan)
    windows = np.lib.stride_tricks.sliding_window_view(padded, (3, 3))
    return np.nanmean(windows, axis=(2, 3))

n = 4
initial = np.zeros((n, n))
initial[0, :] = 100.0
held = initial == 100.0
heat = initial.copy()
for step in range(3):
    heat = np.where(held, initial, mean3x3(heat))
print(round(heat[1, 1], 4), round(heat[3, 3], 4))
51.8519 5.5556

In eLucid8 the loop is an equation: heat at step 0 is initial, and at every later step it is defined from heat at the step before (fby.step). It is the program of the simulation example (examples/simulation/heat.el8) on a smaller plate:

-- data initial = if #x == 0 then 100 else 0
-- data held = #x == 0
initial = input("initial", x : 4, y : 4)
held = input("held", x : 4, y : 4)

heat = restrict
  initial fby.step
    (if held then initial
     else mean heat over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end)
  to (step : 10, x : 4, y : 4) end

-- expect heat[step = 3, x = 1, y = 1] = 51.8519
-- expect heat[step = 3, x = 3, y = 3] = 5.55556
-- expect heat[step = 0, x = 1, y = 1] = 0
-- expect heat[step = 3, x = 0, y = 2] = 100
-- expect heat[step = 3, x = 4, y = 0] = eod

Open in playground

The region (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) leaves out the cells beyond the plate, as NaN padding and np.nanmean do in NumPy. The NumPy loop says in which order to compute the steps; the equation says only how each step follows from the one before, and eLucid8 decides the order.

Computing everything, or what is asked

NumPy computes every cell of every step it is given: there is no way to ask for one cell without computing the whole plate up to that step. A query for one cell in eLucid8 computes only the values that cell depends on, its light cone: at the step before, its 3 × 3 neighbourhood; two steps back, a 5 × 5 square; and so on. A query for the whole plate is computed a step at a time, in compiled loops.

On the simulation example (a 512 × 512 plate; measured with release 0.22.0 on an Apple M5 Max, the whole plate with release 0.24.0, as the Examples page gives them), against NumPy's 1.32 s for the whole plate at step 1,000 and 0.27 s at step 100:

questioneLucid8, one threadeLucid8, 18 threads
one probe cell at step 100 (5% of the field)0.042 s0.017 s
one probe cell at step 1,000 (83% of the plate)1.50 s0.29 s
the whole plate at step 1,0001.78 s0.35 s

The further back a probe looks, the more of the plate its light cone covers: at step 1,000 it is most of this plate, and on one thread NumPy's whole plate is faster.

NaN, eod and errors

In NumPy a missing reading is usually np.nan, and a division by zero gives inf (with a warning). Both flow on through every operation, and a sum with one NaN is NaN. eLucid8 keeps them apart: a missing reading is an error of kind missing, a failure is an error of its own kind, and each names the point where it started wherever it is read. eod (end of data) is something else: the point is outside the data, past its end or outside a bound.

import numpy as np

x = np.array([3.0, np.nan, 4.0, 0.0])
with np.errstate(divide="ignore"):
    ratio = 12 / x
print(ratio)
print(ratio * 2)
print(x.sum(), np.nansum(x))
[ 4. nan  3. inf]
[ 8. nan  6. inf]
nan 7.0
-- table x = t,value; 0,3; 1,; 2,4; 3,0
x = input("x", t : 4)

ratio = 12 / x
scaled = ratio * 2
known = sum x over (t : 4) end

-- expect ratio[t = 0] = 4
-- expect ratio[t = 1] = error missing from x[t = 1]: input x has no data at this point
-- expect ratio[t = 3] = error division: division by zero
-- expect scaled[t = 1] = error missing from x[t = 1]: input x has no data at this point
-- expect scaled[t = 3] = error division from ratio[t = 3]: division by zero
-- expect known = 7

Open in playground

Where NumPy is stronger

Using both together

The Python package runs eLucid8 programs with your data from Python, and gives the answers back as a table that turns into NumPy arrays (eLucid8 from Python):

import numpy as np

import elucid8

program = """
x = input("x", t : 6)
moving = restrict mean x over (t : #t - 2 .. #t) end to (t : 6) end
"""
readings = {"t": [0, 1, 2, 3, 4, 5], "value": [3.0, 1.0, 4.0, 1.0, 5.0, 9.0]}
table = elucid8.run(program, {"x": readings}, query="moving[t = 0..5]", as_="arrow")
moving = table.column("moving_value").to_numpy()
print(np.round(moving, 4))
[3.     2.     2.6667 2.     3.3333 5.    ]

The elucid8 command also writes answers as NumPy .npz files, with a frame for each query (Using eLucid8 from your code).

More: the Tutorial, and the Quick reference for every construct.