
Calling Sequence


seq(m .. n, step)
seq(f, i = m .. n, step)
seq(f, m .. n, step)
seq(f, i = x)
seq(f, i in x)
seq(x)
seq[fold=(rfcn,zero)]( .. )
seq[reduce=rfcn]( .. )
seq[scan=sfcn]( .. )


Parameters


f



any expression

i



name

m, n



numerical or character values

x



expression

step



(optional) numerical value or numelems = N were N is a positive integer value

rfcn



a function to apply to the elements of the sequence to reduce them to a single value

sfcn



function to apply to the elements of prefixes of the sequence to compute a reduction of the prefix

zero



the starting value when computing the reduction





Description


•

The seq function is used to construct a sequence of values. The simplest form is seq(m..n), which generates the sequence of numerical values m, m+1, ..., M. If nm is an integer, then M equals n, otherwise n1 < M < n.

•

A more typical invocation is seq(f(i), i = m..n) which generates the sequence f(m), f(m+1), ..., f(M), where M is as described above. Note that seq(i, i = m..n) is equivalent to the simpler form seq(m..n).


If the expression f does not contain the iteration variable i, then a sequence of identical values is produced. In this case, the iteration variable can also be omitted from the range argument. That is, seq(y, i=m..n) is equivalent to seq(y, m..n), and produces a sequence of floor(nm)+1 repetitions of y.

•

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

–

If step is a numeric value, the sequence is generated using the values m, m+step, ..., M where nstep < M <= n.

–

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

•

The seq(f(i), i = x) form, where x is not a range generates 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 value of x may also be a string, in which case seq iterates over the individual characters within it.

•

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

•

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

•

The fold option specifies a function, rfcn, and an initial value, zero, used to reduce the elements of the sequence to be generated to a single value, instead of producing the sequence. The value is initialized to that specified by zero. Then, for each nonNULL element generated, rfcn is applied to the value and that element, and the value is updated.

•

The reduce option is similar to fold, except that the value is initialized to the first nonNULL element generated. Application of rfcn then begins with the value and the next element.

•

Both fold and reduce compute the single result without actually creating the sequence. Instead, the result value is updated as each individual element is computed. This prevents the allocation of a potentially large amount of memory for a sequence that would just be discarded.

•

An additonal option, evalhf, can be used with fold or reduce, in which case the arguments to each call to rfcn, as well as the result returned, are evaluated using evalhf. The final result returned by seq will be a hardware float.


Note that fold and reduce are equivalent when the generated sequence would not be empty, and the specified zero is the natural zero (or identity) of the reduction function. For example, the natural zero of addition is 0, and of multiplication is 1.


If the generated sequence would be empty (that is, f evaluates to NULL for all elements of the sequence), reduce will return undefined, whereas fold will return zero.

•

Sometimes it is useful to evaluate a sequence of expressions only for their sideeffects, without actually constructing a sequence or returning a value. The option reduce=NULL provides this functionality, discarding each element of the sequence after it is computed.

•

The scan option is related to reduce, except that instead reducing the result otherwise produced by seq to a single value, each prefix of that result is reduced to a single result. It is equivalent to mapping map[reduce] over each prefix of the result produced by the call to seq, without having to generate each prefix sequence separately.

•

The fold, reduce, and scan options are mutually exclusive.

•

The seq function is related to the forloop construct. The precise semantics of seq can best be understood by defining them in terms of the forloop as follows. In this description, the expression f is typically a function of i.

seq(f, i=m..n) == S := NULL;

old := i;

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

i := old;

S; # the result

seq(f, i=x) == S := NULL;

old := i;

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

i := old;

S; # the result




Note: As written above, the seq version is more efficient than the forloop version because the forloop version constructs many intermediate sequences in the process of building the final result. Specifically, here the cost of the seq version is linear in the length of the sequence, but the forloop version is quadratic.


Note: The limits m and n must evaluate to literal constants, that is integers, fractions, floatingpoint numbers, or characters. As a special case, m may evaluate to infinity, or n may evaluate to infinity. If m is greater than n, seq returns the empty sequence (NULL).

•

The index variable i refers to the instance of the variable that is in scope in the context where the call to seq occurs. At the toplevel (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 seq is restored afterwards. Thus the value of that variable is not affected.

•

See also the add, mul, andseq, orseq, and xorseq functions, which work similarly to seq except that instead of constructing a sequence, they construct a sum, product, Boolean conjunction, disjunction, or exclusive disjunction respectively.

•

There is also a procedure parameter modifier, seq, which declares that multiple arguments of a specific type within a procedure invocation will be assigned to a single parameter (as a sequence).



Thread Safety


•

The seq function is thread safe as of Maple 15 provided that evaluating f is itself thread safe. Further the index variable i must not be shared between threads. Using a procedure local is advised.



Examples


>

$\mathrm{seq}\left({i}^{2}\,i=1..5\right)$

${1}{,}{4}{,}{9}{,}{16}{,}{25}$
 (1) 
>

$\mathrm{seq}\left(\mathrm{sin}\left(\frac{\mathrm{\pi}i}{6}\right)\,i=0..6\right)$

${0}{,}\frac{{1}}{{2}}{,}\frac{\sqrt{{3}}}{{2}}{,}{1}{,}\frac{\sqrt{{3}}}{{2}}{,}\frac{{1}}{{2}}{,}{0}$
 (2) 
>

$\mathrm{seq}\left(x\left[i\right]\,i=1..5\right)$

${{x}}_{{1}}{,}{{x}}_{{2}}{,}{{x}}_{{3}}{,}{{x}}_{{4}}{,}{{x}}_{{5}}$
 (3) 
>

$X\u2254\left[\mathrm{seq}\left(i\,i=0..6\right)\right]$

${X}{\u2254}\left[{0}{\,}{1}{\,}{2}{\,}{3}{\,}{4}{\,}{5}{\,}{6}\right]$
 (4) 
>

$\left\{\mathrm{seq}\left({i}^{2}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{mod}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}7\,i=X\right)\right\}$

$\left\{{0}{\,}{1}{\,}{2}{\,}{4}\right\}$
 (5) 
>

$Y\u2254\left[\mathrm{seq}\left({i}^{2}\,i=X\right)\right]$

${Y}{\u2254}\left[{0}{\,}{1}{\,}{4}{\,}{9}{\,}{16}{\,}{25}{\,}{36}\right]$
 (6) 
>

$\left[\mathrm{seq}\left(\left[X\left[i\right]\,Y\left[i\right]\right]\,i=1..\mathrm{nops}\left(X\right)\right)\right]$

$\left[\left[{0}{\,}{0}\right]{\,}\left[{1}{\,}{1}\right]{\,}\left[{2}{\,}{4}\right]{\,}\left[{3}{\,}{9}\right]{\,}\left[{4}{\,}{16}\right]{\,}\left[{5}{\,}{25}\right]{\,}\left[{6}{\,}{36}\right]\right]$
 (7) 
>

$\mathrm{seq}\left(i\,i=''Hello''\right)$

${''H''}{,}{''e''}{,}{''l''}{,}{''l''}{,}{''o''}$
 (8) 
>

$\mathrm{seq}\left(i\,i=''a''..''f''\right)$

${''a''}{,}{''b''}{,}{''c''}{,}{''d''}{,}{''e''}{,}{''f''}$
 (9) 
>

$\left[\mathrm{seq}\left(i\,i=0..\mathrm{\infty}\right)\right]$

>

$L\u2254\left[\mathrm{seq}\left(i\,i=1..10\,2\right)\right]$

${L}{\u2254}\left[{1}{\,}{3}{\,}{5}{\,}{7}{\,}{9}\right]$
 (11) 
>

$\mathrm{seq}\left(\frac{i}{2}\,i\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}L\right)$

$\frac{{1}}{{2}}{,}\frac{{3}}{{2}}{,}\frac{{5}}{{2}}{,}\frac{{7}}{{2}}{,}\frac{{9}}{{2}}$
 (12) 
>

$A\u2254\mathrm{Matrix}\left(\left[\left[\mathrm{seq}\left(i\,i=1..3\right)\right]\,\left[\mathrm{seq}\left(i\,i=4..6\right)\right]\right]\right)$

${A}{\u2254}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$
 (13) 
>

$s\u2254\mathrm{seq}\left(i\,i\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}A\right)$

${s}{\u2254}{1}{,}{4}{,}{2}{,}{5}{,}{3}{,}{6}$
 (14) 
>

$\mathrm{seq}\left(0..100\,10\right)$

${0}{,}{10}{,}{20}{,}{30}{,}{40}{,}{50}{,}{60}{,}{70}{,}{80}{,}{90}{,}{100}$
 (15) 
>

C := proc(f,x)
local i; # declare index variable to be local
[seq( coeff(f,x,i), i=0..degree(f,x) )];
end proc:

>

$C\left(2x4+{x}^{3}\,x\right)$

$\left[{\mathrm{4}}{\,}{2}{\,}{0}{\,}{1}\right]$
 (16) 
>

$T\u2254\mathrm{table}\left(\left\{\mathrm{color}=''red''\,\mathrm{size}=''XL''\right\}\right)$

${T}{\u2254}{table}{}\left(\left[{\mathrm{color}}{=}{''red''}{\,}{\mathrm{size}}{=}{''XL''}\right]\right)$
 (17) 
>

$\mathrm{seq}\left(e\,e=\mathrm{eval}\left(T\right)\right)$

${''red''}{,}{''XL''}$
 (18) 
>

$\mathrm{seq}\left[\mathrm{`=`}\left(\mathrm{reduce}\,\left(x\,y\right)\mapsto \mathrm{cat}\left(x\,''*''\,y\right)\right)\right]\left(c\,c\phantom{\rule[0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[0.0ex]{0.3em}{0.0ex}}''MAPLE''\right)$

>

$\mathrm{logsum}\u2254\mathrm{seq}\left[\mathrm{reduce}=\mathrm{`+`},\mathrm{evalhf}\right]\left(\mathrm{log}\left(i\right)\,i=1..9\right)$

${\mathrm{logsum}}{\u2254}{12.8018274800815}$
 (20) 
>

$\mathrm{exp}\left(\mathrm{logsum}\right)$

${362880.000000000}$
 (21) 


Compatibility


•

The seq command was updated in Maple 2021.

•

The seq[fold], seq[reduce], seq[scan] and numelems options were introduced in Maple 2021.



