 add - Maple Programming Help

add up a sequence of values

mul

multiply a sequence of values

 Calling Sequence add(f, i = m..n) add(f, i = m..n, step) add(f, i = x) add(f, i in x) add(x) 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

Description

 • The add command is used to add up an explicit sequence of values. The mul command computes a product of an explicit sequence of values. A typical call to the add command is add(f(i), i=1..n). This computes $f\left(1\right)+f\left(2\right)+\mathrm{...}+f\left(n\right)$. More generally, add(f(i), i=m..n) computes $f\left(m\right)+f\left(m+1\right)+\mathrm{...}+f\left(n\right)$. Similarly, mul(f(i), i=m..n) computes $f\left(m\right)f\left(m+1\right)\mathrm{...}f\left(n\right)$.
 • If the optional step is specified, add and mul step through the range m..n using that step size, i.e. $f\left(m\right)+f\left(m+\mathrm{step}\right)+\mathrm{...}+f\left(\mathrm{ns}\right)$ where ns is the last value that would not pass n.
 • The call add(f(i), i=x) computes the sum of applying f to each operand or entry of x. Here, x would often 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. Likewise, the call mul(f(i), i=x) computes the product of f of the operands of x. For tables, Arrays, and rtables, the entries of x are scanned rather than the operands.
 • The add(x) calling sequence is equivalent to add(i,i in x). Likewise, mul(x) is equivalent to mul(i,i in x).
 • The add and mul commands are related to the seq command, which is related to the for-loop construct. The precise semantics of the two versions of the add and mul commands can best be understood by noting that they are equivalent to the following for-loops. 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 mul(f, i=x)    ==  P := 1; old := i; for i in x do P := P*f end do; i := old; P; # the result

 • 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 that the limits m and n must evaluate to numerical constants, that is, integers, fractions, or floating point numbers. If you want to compute a formula for a symbolic sum or product in terms of a symbolic limit, use the sum and product commands. As a special case, m may evaluate to $\mathrm{\infty }$, or n may evaluate to $-\mathrm{\infty }$. If m is greater than n, add returns 0 and mul returns 1.
 • Note also that any previously existing value assigned to the index variable i is restored after the add or mul invocation. Thus the value of any local or global variable of the same name visible at the time the add or mul is executed will not be affected.
 • When x is a sparse Matrix, Vector or rtable, 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 commands for computing symbolic sums and products.  These commands are designed to return a formula for an indefinite (or definite) sum or product, rather than an explicit sum or product.
 • The calling sequence add(f(i), i in x) is equivalent to add(f(i), i = x).  The "in" and "=" notations produce the same result for both add and mul.

 • 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.

Examples

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

 > $\mathrm{mul}\left(i,i=1..5\right)$
 ${120}$ (1)
 > $\mathrm{add}\left({i}^{2},i=1..5\right)$
 ${55}$ (2)
 > $\mathrm{add}\left(a\left[i\right]{x}^{i},i=0..5\right)$
 ${{a}}_{{5}}{}{{x}}^{{5}}{+}{{a}}_{{4}}{}{{x}}^{{4}}{+}{{a}}_{{3}}{}{{x}}^{{3}}{+}{{a}}_{{2}}{}{{x}}^{{2}}{+}{{a}}_{{1}}{}{x}{+}{{a}}_{{0}}$ (3)

The add and mul commands can be used on matrices.

 > $M≔\mathrm{Matrix}\left(\left[\left[1,2\right],\left[3,4\right]\right]\right)$
 ${M}{≔}\left[\begin{array}{cc}{1}& {2}\\ {3}& {4}\end{array}\right]$ (4)
 > $\mathrm{add}\left(M,k=1..2\right)$
 $\left[\begin{array}{cc}{2}& {4}\\ {6}& {8}\end{array}\right]$ (5)
 > $\mathrm{mul}\left(M,k=1..3\right)$
 $\left[\begin{array}{cc}{37}& {54}\\ {81}& {118}\end{array}\right]$ (6)

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

 > $L≔\left[\mathrm{seq}\left(i,i=1..5\right)\right]$
 ${L}{≔}\left[{1}{,}{2}{,}{3}{,}{4}{,}{5}\right]$ (7)
 > $\mathrm{add}\left(i,i=L\right)$
 ${15}$ (8)
 > $\mathrm{add}\left(i,i\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}L\right)$
 ${15}$ (9)
 > $\mathrm{mul}\left(x-i,i=L\right)$
 $\left({x}{-}{1}\right){}\left({x}{-}{2}\right){}\left({x}{-}{3}\right){}\left({x}{-}{4}\right){}\left({x}{-}{5}\right)$ (10)
 > $\mathrm{mul}\left(x-i,i\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}L\right)$
 $\left({x}{-}{1}\right){}\left({x}{-}{2}\right){}\left({x}{-}{3}\right){}\left({x}{-}{4}\right){}\left({x}{-}{5}\right)$ (11)

The add command works only for numeric ranges.

 > $\mathrm{add}\left({i}^{2},i=0..n\right)$
 > $\mathrm{sum}\left({i}^{2},i=0..n\right)$
 $\frac{{\left({n}{+}{1}\right)}^{{3}}}{{3}}{-}\frac{{\left({n}{+}{1}\right)}^{{2}}}{{2}}{+}\frac{{n}}{{6}}{+}\frac{{1}}{{6}}$ (12)
 > $\mathrm{add}\left(i,i=\mathrm{\infty }..0\right)$
 ${0}$ (13)
 > $\mathrm{mul}\left(i,i=0..-\mathrm{\infty }\right)$
 ${1}$ (14)
 > $\mathrm{product}\left(i,i=1..\mathrm{\infty }\right)$
 ${\mathrm{\infty }}$ (15)

Compatibility

 • The add and mul commands were updated in Maple 2015.
 • The step parameter was introduced in Maple 2015.