Skip to content

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 to Apply[Plus, list].
  • Total[list, n] totals all elements down to level n.
  • Total[list, {n}] totals elements at level n only.
  • Supports negative levels to count from the bottom (-1 is 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

Notes & additional examples

Worked examples

In[1]:= Total[{1, 2, 3, 4}]
Out[1]= 10

On a matrix, the one-argument form sums the rows (a column total):

In[1]:= Total[{{1, 2}, {3, 4}, {5, 6}}]
Out[1]= {9, 12}

A level specification controls the depth of summation: {2} sums each column instead, giving the per-column totals.

In[1]:= Total[{{1, 2}, {3, 4}, {5, 6}}, {2}]
Out[1]= {3, 7}

The tenth row of Pascal's triangle sums to a power of two:

In[1]:= Total[Table[Binomial[10, k], {k, 0, 10}]]
Out[1]= 1024

Summing exact rationals stays exact — a partial sum of the Basel series as a single reduced fraction:

In[1]:= Total[Table[1/k^2, {k, 1, 100}]]
Out[1]= 1589508694133037873112297928517553859702383498543709859889432834803818131090369901/972186144434381030589657976672623144161975583995746241782720354705517986165248000