seq - Maple Programming Help

# Online Help

###### All Products    Maple    MapleSim

seq

create a sequence

 Calling Sequence seq(f, m..n) seq(f, i = m..n) seq(f, i = m..n, step) seq(f, i = 1..m, step) seq(f, i = x) seq(f, i in x) seq(m .. n, step) seq(f, m .. n, step) seq(x)

Parameters

 f - any expression i - name m, n - numerical values x - expression step - (optional) numerical value

Description

 • The seq command is used to construct a sequence of values. The most typical calling sequence is seq(f(i), i = 1..n)  which generates the sequence $f\left(1\right),f\left(2\right),...,f\left(n\right)$. More generally, seq(f(i), i = m..n) generates the sequence $f\left(m\right),f\left(m+1\right),...,f\left(n\right)$.
 • The seq(f(i), i = x) calling sequence 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 and rtables, the entries of x are scanned rather than the operands. This form of the seq command can also iterate over the characters within a string.
 • The seq(m..n) and seq(m..n, step) calling sequences generate numerical sequences starting with m, followed by m + step.  The last value does not exceed n. When step is omitted, the default increment is 1.
 • The seq(x) calling sequence is equivalent to seq(i,i in x).
 • The seq command is related to the for-loop construct. The precise semantics of the first two versions of the seq call 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.

 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 for-loop version because the for-loop 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 for-loop version is quadratic.
 Note: The limits m and n must evaluate to literal constants, that is integers, fractions, floating-point 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).
 Note: The index variable i is NOT private to the seq invocation.  It is recommended that you always explicitly declare the index variable to be local inside procedures.
 • When x is a sparse Matrix, Vector or rtable, only the nonzero entries are scanned.  Otherwise, regardless of the indexing function, or storage, the entire index-space of the object is scanned.
 • See also the add and mul commands which work similarly to seq except that instead of constructing a sequence, they construct a sum and product respectively.
 • The seq(f(i), i in x) calling sequence is equivalent to seq(f(i), i = x).
 • 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 command 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.
 • For more information on thread safety, see index/threadsafe.

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≔\left[\mathrm{seq}\left(i,i=0..6\right)\right]$
 ${X}{≔}\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≔\left[\mathrm{seq}\left({i}^{2},i=X\right)\right]$
 ${Y}{≔}\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]$
 $\left[\right]$ (10)
 > $L≔\left[\mathrm{seq}\left(i,i=1..10,2\right)\right]$
 ${L}{≔}\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≔\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}{≔}\left[\begin{array}{ccc}{1}& {2}& {3}\\ {4}& {5}& {6}\end{array}\right]$ (13)
 > $s≔\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}{≔}{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(2x-4+{x}^{3},x\right)$
 $\left[{-4}{,}{2}{,}{0}{,}{1}\right]$ (16)
 > $T≔\mathrm{table}\left(\left\{\mathrm{color}="red",\mathrm{size}="XL"\right\}\right)$
 ${T}{≔}{table}{}\left(\left[{\mathrm{size}}{=}{"XL"}{,}{\mathrm{color}}{=}{"red"}\right]\right)$ (17)
 > $\mathrm{seq}\left(e,e=\mathrm{eval}\left(T\right)\right)$
 ${"XL"}{,}{"red"}$ (18)

 See Also