Reductions
Cumulative Sum
Build a running-total list from a 6-element input by accumulating a counter
inside a loop. Each step appends the current total to running. The trace
shows both total and running growing together, making the running-total
pattern explicit before the single-call NumPy version.
By hand
Initialize total to 0 and running to an empty list. On each iteration add
v to total, then append the updated total to running.
naive.py
Replay: real traced execution (multi-file project)
values = [1, 2, 3, 4, 5, 6]
running = []
total = 0
for v in values:
total += v
running.append(total)
print('RESULT:', running)
values ← [1, 2, 3, 4, 5, 6]
1values = [1, 2, 3, 4, 5, 6]2running = []values this step[1, 2, 3, 4, 5, 6]valuesrunning ← []
1values = [1, 2, 3, 4, 5, 6]2running = []3total = 0values this step[]runningtotal ← 0
2running = []3total = 04for v in values:values this step0totalv ← 1
3total = 04for v in values:5 total += vvalues this step1vtotal ← 1
4for v in values:5 total += v6 running.append(total)values this step0 → 1totalrunning ← [1]
5 total += v6 running.append(total)7print('RESULT:', running)values this step[] → [1]runningv ← 2
3total = 04for v in values:5 total += vvalues this step1 → 2vtotal ← 3
4for v in values:5 total += v6 running.append(total)values this step1 → 3totalrunning ← [1, 3]
5 total += v6 running.append(total)7print('RESULT:', running)values this step[1] → [1, 3]runningv ← 3
3total = 04for v in values:5 total += vvalues this step2 → 3vtotal ← 6
4for v in values:5 total += v6 running.append(total)values this step3 → 6totalrunning ← [1, 3, 6]
5 total += v6 running.append(total)7print('RESULT:', running)values this step[1, 3] → [1, 3, 6]runningv ← 4
3total = 04for v in values:5 total += vvalues this step3 → 4vtotal ← 10
4for v in values:5 total += v6 running.append(total)values this step6 → 10totalrunning ← [1, 3, 6, 10]
5 total += v6 running.append(total)7print('RESULT:', running)values this step[1, 3, 6] → [1, 3, 6, 10]runningv ← 5
3total = 04for v in values:5 total += vvalues this step4 → 5vtotal ← 15
4for v in values:5 total += v6 running.append(total)values this step10 → 15totalrunning ← [1, 3, 6, 10, 15]
5 total += v6 running.append(total)7print('RESULT:', running)values this step[1, 3, 6, 10] → [1, 3, 6, 10, 15]runningv ← 6
3total = 04for v in values:5 total += vvalues this step5 → 6vtotal ← 21
4for v in values:5 total += v6 running.append(total)values this step15 → 21totalrunning ← [1, 3, 6, 10, 15, 21]
5 total += v6 running.append(total)7print('RESULT:', running)values this step[1, 3, 6, 10, 15] → [1, 3, 6, 10, 15, 21]runningfor v in values:
3total = 04for v in values:5 total += vstdout ← RESULT: [1, 3, 6, 10, 15, 21]
6 running.append(total)7print('RESULT:', running)values this stepRESULT: [1, 3, 6, 10, 15, 21]stdout
With NumPy
np.cumsum(a) computes the cumulative sum in one call, returning an array of
the same length as the input where position i holds the sum of all elements
up to and including index i.
library.py
import numpy as np
values = [1, 2, 3, 4, 5, 6]
a = np.array(values)
result = np.cumsum(a)
print('shape:', result.shape)
print('dtype:', result.dtype)
print('values:', result.tolist())
print('RESULT:', result.tolist())
shape: (6,)
dtype: int64
values: [1, 3, 6, 10, 15, 21]
RESULT: [1, 3, 6, 10, 15, 21]
Implementation notes
np.cumsumis a scan, not a reduction: the output has the same shape as the input. Every partial sum is preserved, unlikea.sum()which collapses the whole array to a single scalar.- Position
iof the output equalsa[:i+1].sum()— the sum of the firsti+1elements. np.cumsumalso accepts anaxisargument for 2-D arrays:axis=0produces running column sums,axis=1produces running row sums.- For the equivalent pure-Python pattern see
running-totalin the python-data-basics book. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.