Skip to content

Accumulate ​

The running sums of a list.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageAccumulate✓
Accumulate(list){a1,a1+a2,a1+a2+a3,…}\{a_1, a_1+a_2, a_1+a_2+a_3, \ldots\}.
Typed signatures
TypePackageApplies
(collection<any>) -> collectioncombinatoricsalways

Domain: Collections

Details
  • The first element is kept as-is; each later element is the sum of everything up to and including it.
  • Equivalent to FoldList(Add,list)FoldList(Add, list) — see FoldList.

Examples

Basic #
live ↗
InAccumulate([1,2,3,4])\mathrm{Accumulate}(\bigl\lbrack1, 2, 3, 4\bigr\rbrack)Out[1,3,6,10]\bigl\lbrack1, 3, 6, 10\bigr\rbrack

Running sums with symbolic entries

live ↗
InAccumulate([a,b,c,d])\mathrm{Accumulate}(\bigl\lbrack a, b, c, d\bigr\rbrack)Out[a,a+b,a+b+c,a+b+c+d]\bigl\lbrack a, a+b, a+b+c, a+b+c+d\bigr\rbrack
live ↗
InAccumulate([[a,b],[c,d],[k,f]])\mathrm{Accumulate}(\bigl\lbrack\bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack c, d\bigr\rbrack, \bigl\lbrack k, f\bigr\rbrack\bigr\rbrack)Out[[a,b],[a+c,b+d],[a+c+k,b+d+f]]\bigl\lbrack\bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack a+c, b+d\bigr\rbrack, \bigl\lbrack a+c+k, b+d+f\bigr\rbrack\bigr\rbrack
Scope #

The running sum of the empty list is empty

live ↗
InAccumulate([])\mathrm{Accumulate}(\bigl\lbrack\bigr\rbrack)Out[]\bigl\lbrack\bigr\rbrack
live ↗
InAccumulate(data)\mathrm{Accumulate}(\mathrm{data})OutAccumulate(data)\mathrm{Accumulate}(\mathrm{data})
live ↗
InAccumulate(v)\mathrm{Accumulate}(v)OutAccumulate(v)\mathrm{Accumulate}(v)
Applications #

Running sums of 1..101..10 give the triangular numbers

live ↗
InAccumulate(1..10)\mathrm{Accumulate}(1..10)Out[1,3,6,10,15,21,28,36,45,55]\bigl\lbrack1, 3, 6, 10, 15, 21, 28, 36, 45, 55\bigr\rbrack
Properties #

The last element is the sum of the whole list

live ↗
InLast(Accumulate([1,2,3,4,5]))=1+2+3+4+5\mathrm{Last}(\mathrm{Accumulate}(\bigl\lbrack1, 2, 3, 4, 5\bigr\rbrack))=1+2+3+4+5OutTrue\mathrm{True}
Neat examples #

Repeated accumulation builds columns of binomial coefficients

live ↗
InNestList(Accumulate,1..5,5)T\mathrm{NestList}(\mathrm{Accumulate}, 1..5, 5)^TOut[[1,1,1,1,1,1],[2,3,4,5,6,7],[3,6,10,15,21,28],[4,10,20,35,56,84],[5,15,35,70,126,210]]\bigl\lbrack\bigl\lbrack1, 1, 1, 1, 1, 1\bigr\rbrack, \bigl\lbrack2, 3, 4, 5, 6, 7\bigr\rbrack, \bigl\lbrack3, 6, 10, 15, 21, 28\bigr\rbrack, \bigl\lbrack4, 10, 20, 35, 56, 84\bigr\rbrack, \bigl\lbrack5, 15, 35, 70, 126, 210\bigr\rbrack\bigr\rbrack
About
Examples9
Checked againstWolfram Language 9 agree
CrosswalkWolfram Language

See also: FoldList