[ Arithmetic | Reference Manual | Alphabetic Index ]

sum(+Vector, -Result)

Computes the sum of the elements of Vector, or the scalar product of two vectors
Vector
A list, array or vector expression, or a product of those.
Result
Output: number.

Description

This predicate is used by the ECLiPSe compiler to expand evaluable arithmetic expressions. So the call to sum(Vector, Result) is equivalent to
    Result is sum(Vector)
which should be preferred.

A vector is a list, an array, or a collection expression (as accepted by eval_to_list/2), whose elements are numbers.

If Vector is a single vector, then Result is the sum of its elements.

If Vector is of the form V1*V2, where V1 and V2 are vectors, then Result is the sum of the pair-wise products of the vector elements (scalar product). If V1 and V2 are of different size, the missing elements are considered to be zero.

If the vector elements are of different number types, then contagion rules apply, i.e. the result is of the widest type that occurs among the vector elements (integer -> rational -> float -> breal).

In coroutining mode, if the list is only partly instantiated, the predicate delays until the list is complete.

Modes and Determinism

Modules

This predicate is sensitive to its module context (tool predicate, see @/2).

Exceptions

(4) instantiation fault
Vector is insufficiently instantiated (non-coroutining mode only).
(5) type error
Vector is not a vector or product of vectors.

Examples

Success:
      X is sum([]).                              % gives X = 0
      X is sum([1,2,3]).                         % gives X = 6
      X is sum([1,2.0,3]).                       % gives X = 6.0

      X is sum([](1,2,3)).                       % gives X = 6

      A=[](4,5,6,7), X is sum(A[2..3]).          % gives X = 11


      X is sum([1,2,3] * [4,5,6]).               % gives X = 32

      M = []([](1,2,3), [](4,5,6), [](7,8,9)),
      X is sum(M[2,*] * M[*,1]).                 % gives X = 66

See Also

is / 2, + / 3, eval_to_list / 2