seq
create a sequence
Calling Sequence
Parameters
Description
Thread Safety
Examples
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)
f
-
any expression
i
name
m, n
numerical values
x
expression
step
(optional) numerical value
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⁡1,f⁡2,...,f⁡n. More generally, seq(f(i), i = m..n) generates the sequence f⁡m,f⁡m+1,...,f⁡n.
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;
for i in x do S := S,f end do;
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).
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.
seq⁡i2,i=1..5
1,4,9,16,25
seq⁡sin⁡π⁢i6,i=0..6
0,12,32,1,32,12,0
seq⁡xi,i=1..5
x1,x2,x3,x4,x5
X≔seq⁡i,i=0..6
X≔0,1,2,3,4,5,6
seq⁡i2mod7,i=X
0,1,2,4
Y≔seq⁡i2,i=X
Y≔0,1,4,9,16,25,36
seq⁡Xi,Yi,i=1..nops⁡X
0,0,1,1,2,4,3,9,4,16,5,25,6,36
seq⁡i,i=Hello
H,e,l,l,o
seq⁡i,i=a..f
a,b,c,d,e,f
seq⁡i,i=0..−∞
L≔seq⁡i,i=1..10,2
L≔1,3,5,7,9
seq⁡i2,iinL
12,32,52,72,92
A≔Matrix⁡seq⁡i,i=1..3,seq⁡i,i=4..6
A≔123456
s≔seq⁡i,iinA
s≔1,4,2,5,3,6
seq⁡0..100,10
0,10,20,30,40,50,60,70,80,90,100
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⁡2⁢x−4+x3,x
−4,2,0,1
T≔table⁡color=red,size=XL
T≔table⁡size=XL,color=red
seq⁡e,e=eval⁡T
XL,red
See Also
$
add
for
map
mul
op
sequence
The seq Modifier
Threads[Seq]
Download Help Document
What kind of issue would you like to report? (Optional)