#Regions and aggregates
A region is a set of positions, written like a restriction: (t : 0 .. 9) is t from 0 to 9. An aggregate combines a value over a region: sum, mean, min, max and count, written sum e over R end.
-- Windows, totals and a 3 × 3 block.
reading = restrict 20 + mod(#t * 7, 5) to (t : 10) end
avg3 = mean reading over (t : #t - 2 .. #t) end
highest = max reading over (t : 0 .. 9) end
how_many_warm = count reading over (t : 0 .. 9) if reading > 22 end
grid = restrict 10 * #x + #y to (x : 10, y : 10) end
block = sum grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
cells = count grid over (x : #x - 1 .. #x + 1, y : #y - 1 .. #y + 1) end
-- ask avg3[t = 4]
-- expect avg3[t = 0] = 20
-- expect avg3[t = 2] = 22
-- expect avg3[t = 4] = 22.6667
-- expect highest = 24
-- expect how_many_warm = 4
-- expect block[x = 5, y = 5] = 495
-- expect cells[x = 0, y = 0] = 4
How to read it:
avg3is a moving window: its region's ends use#t, so at each point it averages that reading and the two before. Att = 4: (24 + 21 + 23) / 3 = 22.6667.- Positions with no data are left out: at
t = 0the window reachest = -2, where there is nothing, soavg3is the mean of the one reading there is, 20. highestandhow_many_warmrange over every reading, so they have no dimensions.if reading > 22after the region is a filter: only the positions where it is true are counted.blocksums the 3 × 3 block ofgridaround each point: atx = 5, y = 5it is 495.cellscounts the block's positions: 4 at a corner, since the five off the grid are left out.
Sums are exact. eLucid8 adds numbers as if exactly and rounds once at the end, so the order of a sum never changes its answer, and a mean shows 22.6667, never a long tail of rounding. product e over R end multiplies the same way: exactly, rounded once.
An aggregate stops at eod: if a reading in the region is eod, so is the result. Write skip eod after the region, mean reading over (t : #t - 2 .. #t) skip eod end, to leave such readings out instead.
#Try it in the playground
- The answer is there as the program opens, with no button to press:
avg3[t = 4] is 22.6667. - Press Why?: the trace shows the three readings the mean read.
- Query
cellsatx = 0, y = 0(4) and atx = 5, y = 5(9). - Change
meantomaxinavg3: the row redraws by itself, the highest of each three readings.
#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) with made-up daily prices for 30 days and their 5-day moving average. Compile it with the elucid8 tools before you show it, and give me its Open in playground link.