Algosim documentation: sum

sum

Computes the sum of all elements in a container or in a sequence. The elements may be numbers, vectors, or matrices.

Syntax

Description

Summing a container

If X is a container, then sum(X) is the sum of all elements in X.

The following container types are supported:

Typewise, the summands may be either real or complex, and the result is complex iff at least one of the summands is complex.

Summing a sequence

sum(expr, var, a, b) computes the sum of the expression expr in one variable var as var takes all integer values from a to b (inclusively). This precisely implements the sigma notation for n-ary sums.

expr must return a number, a vector, or a matrix.

Notes

These note apply to both modes of operation: summing a container and summing a sequence.

The empty sum is 0. Notice that the sum of an empty collection of vectors or matrices is also equal to the scalar 0, because the system cannot tell that the collection indeed is an empty collection of vectors or matrices. And even if it could, it wouldn’t know the dimension of the vectors or the size of the matrices.

When summing a collection of objects, the objects must be of the same kind (numbers, vectors, or matrices). If they are vectors or matrices, they must all be of the same dimension or size, respectively.

Examples

Summing a container

sum(❨5, 2, 3, 0, −1, 4❩)
13
sum(IdentityMatrix(100))
100
sum(SequenceList(100))
5050
sum('(❨6, 1, 0❩, ❨2, 1, 3❩, ❨0, −2, 1❩, ❨1, 1, −2❩, ❨0, −3, 1❩, ❨−2, 4, 5❩))
 ⎛7⎞
e⎜2⎟
 ⎝8⎠
sum('(❨❨5, 1❩, ❨3, −1❩❩, ❨❨2, −i❩, ❨i, 1❩❩, ❨❨5, 0❩, ❨−1, 2⋅i❩❩))
⎛ 12   1 − i⎞
⎝2 + i  2⋅i ⎠

Summing a sequence

sum(k, k, 1, 100)
5050
sum(k^2, k, 1, 100)
338350
sum(1/k!, k, 0, 100)
2.71828182846	(=e)
sum(2^k⋅k!^2/(2⋅k + 1)!, k, 0, 100)
1.57079632679	(=π/2)
sum(❨1, k, k^2, k^3, k^4❩, k, 1, 100)
 ⎛   100    ⎞
 ⎜   5050   ⎟
e⎜  338350  ⎟
 ⎜ 25502500 ⎟
 ⎝2050333330⎠
sum(1/(1 + Fibonacci(2⋅n + 1)), n, 0, 100)
1.11803398875	(=√5/2)
sum(1/prime(n)^2, n, 1, 1000000)
0.452247416352

See also