#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
| NumPy | eLucid8 | |
|---|---|---|
a[3, 4] | a[t = 3, x = 4] | coordinates by name, in any order |
a.sum(axis=1) | sum a over (x : 6) end | an 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) end | a moving window, indexed by the step it ends at |
a for loop over time steps | initial fby.step (…) | each step defined from the one before |
np.nan | an 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) end | aggregates leave out missing readings, and say how many |
| every element computed | only what a question needs | a 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
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
- A running sum is
total:xat t = 0, then the total one step back plusx.np.cumsumcomputes every element; a query fortotal[t = 4]computestotalat t = 0 to 4 only. - A shift is
x[t = #t - 1],xone step back. Before the first step there is no data, sochange[t = 0]is eod.np.rollwraps the last element round to the front, and slicing (x[1:] - x[:-1]) gives a shorter array, whose element 0 belongs to step 1: in eLucid8 every value stays indexed by its step. - A moving window is a mean over a region of
t.np.convolve(…, "valid")gives one element for each full window, the first one ending at step 2;movingis defined at every step, and positions below 0 are left out of its region, somoving[t = 0]is the mean ofxat t = 0 alone.restrict … to (t : 6) endbounds it to the steps ofx.
#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
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:
| question | eLucid8, one thread | eLucid8, 18 threads |
|---|---|---|
| one probe cell at step 100 (5% of the field) | 0.042 s | 0.017 s |
| one probe cell at step 1,000 (83% of the plate) | 1.50 s | 0.29 s |
| the whole plate at step 1,000 | 1.78 s | 0.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
- The missing reading at t = 1 (an empty cell) is an error of kind
missingthat flows throughratioandscaled, as NaN does, but says where it started.known, asum, leaves it out, asnp.nansumleaves NaN out, and says so beside the answer: "aggregates left out 1 missing point of input x". - A division by zero is an error, not a number:
scaled[t = 3]is an error that says it came throughratioat t = 3, where NumPy'sinfsays nothing about where it came from. - Every answer also carries a timestamp, the time of the newest data it was computed from (Values and timestamps); NumPy has none.
#Where NumPy is stronger
- The ecosystem. pandas, SciPy, scikit-learn and much of scientific Python are built on NumPy arrays, with decades of use behind them.
- Linear algebra and transforms. NumPy solves linear systems and computes eigenvalues, inverses and Fourier transforms (
np.linalg,np.fft). eLucid8 has none of these: a matrix product is a sum over a region, as in the playground's Matrix multiplication example. - GPUs. Libraries with NumPy's interface run arrays on GPUs; eLucid8 has no GPU backend.
- Familiarity. Most people who compute with arrays in Python know NumPy already.
- Speed, measured (release 0.24.0, the Examples page):
- in the sensor field's stream, each tick in one process: NumPy 5.8 ms on 18 threads, against eLucid8's 10.5 ms, most of it sending the readings to
elucid8 serveand reading the answer back (the query itself takes 2.2 ms); - on one thread, the whole plate at step 1,000, with eLucid8's exact sums: NumPy 1.32 s, eLucid8 1.78 s.
- in the sensor field's stream, each tick in one process: NumPy 5.8 ms on 18 threads, against eLucid8's 10.5 ms, most of it sending the readings to
#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.