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

Online Help

All Products    Maple    MapleSim


seq

create a sequence

 

Calling Sequence

Parameters

Description

Thread Safety

Examples

Compatibility

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 where 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 n-m is an integer, then M equals n, otherwise n-1 < 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(n-m)+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 n-step < 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 form seq(f,n) is equivalent to seq(f,1..n).

• 

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 non-NULL 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 non-NULL 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 side-effects, 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 for-loop construct. The precise semantics of seq 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).

• 

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

• 

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

Examples

seqi2&comma;i=1..5

1,4,9,16,25

(1)

seqsinπi6&comma;i=0..6

0,12,32,1,32,12,0

(2)

seqxi&comma;i=1..5

x1,x2,x3,x4,x5

(3)

Xseqi&comma;i=0..6

X0&comma;1&comma;2&comma;3&comma;4&comma;5&comma;6

(4)

seqi2mod7&comma;i=X

0&comma;1&comma;2&comma;4

(5)

Yseqi2&comma;i=X

Y0&comma;1&comma;4&comma;9&comma;16&comma;25&comma;36

(6)

seqXi&comma;Yi&comma;i=1..nopsX

0&comma;0&comma;1&comma;1&comma;2&comma;4&comma;3&comma;9&comma;4&comma;16&comma;5&comma;25&comma;6&comma;36

(7)

seqi&comma;i=Hello

H,e,l,l,o

(8)

seqi&comma;i=a..f

a,b,c,d,e,f

(9)

seqi&comma;i=0..

(10)

Lseqi&comma;i=1..10&comma;2

L1&comma;3&comma;5&comma;7&comma;9

(11)

seqi2&comma;iinL

12,32,52,72,92

(12)

AMatrixseqi&comma;i=1..3&comma;seqi&comma;i=4..6

A123456

(13)

sseqi&comma;iinA

s1,4,2,5,3,6

(14)

seq0..100&comma;10

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:

C2x4+x3&comma;x

−4&comma;2&comma;0&comma;1

(16)

Ttablecolor=red&comma;size=XL

Ttablesize=XL&comma;color=red

(17)

seqe&comma;e=evalT

XL,red

(18)

seq`=`reduce&comma;x&comma;ycatx&comma;*&comma;yc&comma;cinMAPLE

M*A*P*L*E

(19)

logsumseqreduce=`+`,evalhflogi&comma;i=1..9

logsum12.8018274800815

(20)

explogsum

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.

• 

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

See Also

$

add

for

map

mul

op

sequence

The seq Modifier

Threads[Seq]