create a 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[fold=(rfcn,zero)]( .. )
seq[reduce=rfcn]( .. )
seq[scan=sfcn]( .. )
numerical or character values
(optional) numerical value or numelems = N were N is a positive integer value
a function to apply to the elements of the sequence to reduce them to a single value
function to apply to the elements of prefixes of the sequence to compute a reduction of the prefix
the starting value when computing the reduction
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 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;
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).
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).
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.
C := proc(f,x)
local i; # declare index variable to be local
[seq( coeff(f,x,i), i=0..degree(f,x) )];
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.
The seq Modifier
Download Help Document
What kind of issue would you like to report? (Optional)