Total¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Total[list]
gives the total of the elements in list.
Total[list, n]
totals all elements down to level n.
Total[list, {n}]
totals elements at level n.
Total[list, {n1, n2}]
totals elements at levels n1 through n2.
Examples¶
All examples below are verified against the current Mathilda build.
In[1]:= Total[{a, b, c, d}]
Out[1]= a + b + c + d
In[2]:= Total[{{1, 2}, {3, 4}}]
Out[2]= {4, 6}
In[3]:= Total[{{1, 2}, {3, 4}}, 2]
Out[3]= 10
In[4]:= Total[{{1, 2}, {3}}, 2]
Out[4]= 6
Implementation notes¶
builtin_total sums the elements of a list, optionally restricted to a level range. Total[list] sums the top level; Total[list, n] sums levels 1..n; Total[list, {n}] sums exactly level n; Total[list, {n1, n2}] sums a range; Total[list, Infinity] sums all levels. Negative level indices count from the bottom using the list's depth (get_depth_for_total). The chosen levels' elements are gathered and combined with Plus (so the usual numeric/symbolic Plus folding applies — Total is just structural element collection feeding Plus). Total carries ATTR_PROTECTED.
Protected.Total[list]is equivalent toApply[Plus, list].Total[list, n]totals all elements down to leveln.Total[list, {n}]totals elements at levelnonly.- Supports negative levels to count from the bottom (
-1is the last dimension). - Handles ragged arrays correctly by summing from the inside out when multiple levels are specified.
Total[list, Infinity]totals all atoms in the expression.
Attributes: Protected.
Implementation status¶
Stable — documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
References¶
- Source:
src/list.c - Specification:
docs/spec/builtins/arithmetic.md
Notes & additional examples¶
Worked examples¶
On a matrix, the one-argument form sums the rows (a column total):
A level specification controls the depth of summation: {2} sums each column
instead, giving the per-column totals.
The tenth row of Pascal's triangle sums to a power of two:
Summing exact rationals stays exact — a partial sum of the Basel series as a single reduced fraction: