add - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


add

add up a sequence of values

mul

multiply a sequence of values

 

Calling Sequence

Parameters

Description

Thread Safety

Examples

Compatibility

Calling Sequence

add(f, i = m..n)

add(f, i = m..n, step)

add(f, i = x)

add(f, i in x)

add(x)

add[pairwise]( .. )

add[pairwise=p]( .. )

mul(f, i = m..n)

mul(f, i = m..n, step)

mul(f, i = x)

mul(f, i in x)

mul(x)

Parameters

f

-

expression

i

-

name

m, n

-

numerical values

x

-

expression

step

-

(optional) numerical value

p

-

positive integer value or false

Description

• 

The add function is used to add up an explicit sequence of values, and the mul function multiply together an explicit sequence of values.

• 

The descriptions that follow will all refer to add, but everything also applies to mul, except that addition is replaced by multiplication.

• 

The simplest form is add(m..n) or mul(m..n), which operates on the sequence m, m+1, ..., M, producing the sum or product of those values respectively. If n-m is an integer, them M equals n, otherwise n-1 < M < n.

• 

A more typical invocation is add(f(i), i = m..n) which computes the sum or product of f(m), f(m+1), ..., f(M), where M is as described above. Note that add(i, i = m..n) is equivalent to the simpler form add(m..n).

• 

If the expression f does not contain the iteration variable i, then a sequence of identical values is operated on. In this case, the iteration variable can also be omitted from the range argument. That is, add(y, i=m..n) is equivalent to add(y, m..n), and computes y * (floor(n-m)+1) (or y ^ (floor(n-m)+1) for mul).

• 

All of the above invocations of add and mul can take an optional last argument, step, which can take one of two forms:

– 

If step is a numeric value, the sequence to be added or multiplied consists of the values m, m+step, ..., M where n-step < M <= n.

– 

If step is of the form numelems=N, where N is an integer, the sequence consists of the values m, m+q, ..., n, where q = (n - m) / (N - 1).

• 

The add(f(i), i = x) form, where x is not a range operates on a sequence by applying f to each operand or entry of x. Here, x would most commonly be a set or list, but it could be any other data structure to which op can be applied, such as a sum or product. For tables, Arrays, Vectors, and Matrices, the entries of x are scanned rather than the operands.

• 

The form, add(x), is equivalent to add(i,i = x).

• 

The form add(f(i), i in x) is equivalent to add(f(i), i = x).

• 

The add function, when called in the form add(f(i), i=m..n), performs pairwise summation to minimize the accumulated round-off error when adding floating-point values. This is done using a divide-and-conquer approach, where the sequence of values to be added is split in half, each half added separately (perhaps using pairwise summation), and then the two results added together.

– 

Likewise, the forms add(i, i = x), add(i, i in x), and add(x) will make use of pairwise summation in the same way.

– 

By default, sequences of 50 or fewer elements, whether the original input, or the result of splitting a larger input, are not split further. This threshold can be changed using the pairwise option.

– 

If specified in the form pairwise=N, where N is an integer greater than or equal to 4, splitting is not done for subsequences of fewer than N elements.

– 

Specifying the option as just pairwise is equivalent to pairwise=4.

– 

A smaller value of the pairwise option may result in less rounding error, but will increase time and space overhead due to recursive calls to add. For most applications, omitting the pairwise option (equivalent to pairwise=51) is preferred.

– 

The pairwise summation can only be applied to adding the members of lists, sets, Arrays, Vectors, and expression sequences.

– 

Pairwise summation can be disabled entirely by using the option pairwise=false.

• 

The add and mul functions are related to performing similar operations via a for-loop. The semantics can best be understood by defining them in terms of the for-loop as follows. In this description, the expression f is typically a function of i.

add(f, i=m..n) ==  S := 0;

                   old := i;

                   for i from m to n do S := S + f end do;

                   i := old;

                   S; # the result

add(f, i=x)    ==  S := 0;

                   old := i;

                   for i in x do S := S + f end do;

                   i := old;

                   S; # the result

mul(f, i=m..n) ==  P := 1;

                   old := i;

                   for i from m to n do P := P * f end do;

                   i := old;

                   P; # the result

mul(f, i=x)    ==  P := 1;

                   old := i;

                   for i in x do P := P * f end do;

                   i := old;

                   P; # the result

  

Note: In either form, the add and mul commands are generally more efficient than the for-loop versions because the for-loop versions construct many intermediate sums and products.

  

Note: The limits m and n must evaluate to numerical constants, that is, integers, fractions, or floating-point numbers. To compute a symbolic sum or product expression in terms of a symbolic limit, use the sum and product functions. As a special case, m may evaluate to infinity, or n may evaluate to -infinity. If m is greater than n, add returns 0 and mul returns 1.

• 

The index variable i refers to the instance of the variable that is in scope in the context where the call to add or mul occurs. At the top-level (outside of any procedure or module), the global variable is used. Within a procedure or module, the local variable of that name is used. If the procedure or module does not explicitly declare a local variable with that name, an implicit local declaration is quietly generated.

  

Regardless of whether the index variable is local or global, the value that the variable had before the invocation of add or mul is restored afterwards. Thus the value of that variable is not affected.

• 

When x is a sparse Matrix, Vector or Array, only the non-zero entries are scanned.  Otherwise, regardless of the indexing function, or storage, the entire index-space of the object is scanned.

• 

See also the sum and product functions for computing symbolic sums and products.  These functions return an expression for an indefinite (or definite) sum or product, rather than an explicit sum or product.

Thread Safety

• 

The add and mul commands are thread safe as of Maple 15, provided that evaluating the expression f is thread safe.  Further the index variable i must not be shared between threads.  Using a procedure local is advised.

• 

For more information on thread safety, see index/threadsafe.

Examples

The add and mul commands are used to add up or multiply an explicit sequence of values.

muli&comma;i=1..5

120

(1)

addi2&comma;i=1..5

55

(2)

addaixi&comma;i=0..5

a5x5+a4x4+a3x3+a2x2+a1x+a0

(3)

The add and mul commands can be used on matrices.

MMatrix1&comma;2&comma;3&comma;4

M1234

(4)

addM&comma;k=1..2

2468

(5)

mulM&comma;k=1..3

375481118

(6)

The "in" and "=" notations produce the same result for both add and mul.

Lseqi&comma;i=1..5

L1&comma;2&comma;3&comma;4&comma;5

(7)

addi&comma;i=L

15

(8)

addi&comma;iinL

15

(9)

mulxi&comma;i=L

x1x2x3x4x5

(10)

mulxi&comma;iinL

x1x2x3x4x5

(11)

The add command works only for numeric ranges.

addi2&comma;i=0..n

Error, range bounds in add must be numeric

sumi2&comma;i=0..n

n+133n+122+n6+16

(12)

addi&comma;i=..0

0

(13)

muli&comma;i=0..

1

(14)

producti&comma;i=1..

(15)

The pairwise option has the potential to reduce round-off error. In the following example, calculations are being done to 10 significant digits, for which the correct result is 21097.45589.

Digits10&colon;

addsqrti1.0&comma;i=1..1000

21097.45588

(16)

addpairwise=4sqrti1.0&comma;i=1..1000

21097.45589

(17)

Disabling pairwise summation introduces significant round-off error.

addpairwise=falsesqrti1.0&comma;i=1..1000

21097.45585

(18)

Compatibility

• 

The mul command was updated in Maple 2015.

• 

The step parameter was introduced in Maple 2015.

• 

For more information on Maple 2015 changes, see Updates in Maple 2015.

• 

The add command was updated in Maple 2021.

• 

The p parameter was introduced in Maple 2021.

• 

The pairwise option was introduced in Maple 2021.

• 

For more information on Maple 2021 changes, see Updates in Maple 2021.

See Also

for

op

product

seq

sum

Threads[Add]

Threads[Mul]