6.39.2 Making a sequence or a list: seq $
The seq command or $ operator can create a sequence or a list.
To create a sequence:
-
seq takes three mandatory arguments and one optional
argument:
-
expr, an expression depending on a parameter.
- k, the parameter.
- a..b, a range of values.
The range can be combined with the parameter into one argument of
k=a..b.
- Optionally p, a step size (by default 1 or -1, depending
on whether b>a or b<a). This is only allowed
if the previous two arguments are combined into one, k=a..b
This is Maple-like syntax.
- seq(expr,k,a..b) (or
seq(expr,k=a..b ⟨ ,p⟩))
returns the sequence formed by the values of expr, as k
changes from a to b in steps of p.
Alternatively, a sequence can be created with the infixed $ operator.
Namely, expr$ k=a..b
returns the sequence formed by the values of expr as k
changes from a to b. As a special case, expr$
n creates a sequence consisting of n copies of expr.
There are two ways to create a list with seq.
First:
-
seq takes four mandatory arguments and one optional
argument:
-
expr, an expression depending on a parameter.
- k, the parameter.
- a, the beginning value of the parameter.
- b, the ending value of the parameter.
- Optionally p, a step size (by default 1 or -1, depending
on whether b>a or b<a).
This is TI-like syntax.
- seq(expr,k,a,b ⟨,p⟩)
returns the list consisting of the values of expr, as k
changes from a to b in steps of p.
- As a special case, seq(expr,n)
creates a list consisting of n copies of expr.
Second:
-
seq takes two arguments:
argument:
-
expr, an expression.
- n, a positive integer.
- seq(expr,n)
returns the list consisting of n copies of expr.
Remark.
-
In Xcas mode, the precedence of $ is not the
same as it is, for example, in Maple. In case of doubt,
put the arguments of $ in parenthesis.
For example, the equivalent of seq(j^2,j=-1..3) is
(j^2)$(j=-1..3) and returns (1,0,1,4,9).
- With Maple syntax, j,a..b,p is not valid.
To specify a step p for the variation of
j from a to b, use j=a..b,p or use the TI syntax
j,a,b,p and get the sequence from the list with
op(…).
Examples.
-
To create a sequence:
Input:
seq(j^3,j,1..4)
or:
seq(j^3,j=1..4)
or:
(j^3)$(j=1..4)
Output:
- To create a list:
Input:
seq(j^3,j,1,4)
Output:
- To create a sequence:
Input:
seq(j^3,j=-1..4,2)
Output:
To create a list:
Input:
seq(j^3,j,-1,4,2)
Output:
- Input:
seq(j^3,j,0,5,2)
Output:
- Input:
seq(j^3,j,5,0,-2)
or:
seq(j^3,j,5,0,2)
Output:
- Input:
seq(j^3,j,1,3,0.5)
Output:
⎡
⎣ | 1,3.375,8.0,15.625,27.0 | ⎤
⎦ |
- Input:
seq(j^3,j,1,3,1/2)
Output:
- To create a list with several copies of the same element:
Input:
seq(t,4)
Output:
- To create a sequence with several copies of the same element:
Input:
seq(t,k=1..4)
or:
t$4
Output:
Examples of sequences being used.
-
Find the third derivative of ln(t):
(See Section 6.19.4).)
Input:
diff(log(t),t$3)
Output:
- Input:
l:=[[2,3],[5,1],[7,2]] |
seq((l[k][0])$(l[k][1]),k=0 .. size(l)-1)
|
Output:
⎛
⎝ | 2,2,2 | ⎞
⎠ | , | ⎛
⎝ | 5 | ⎞
⎠ | , | ⎛
⎝ | 7,7 | ⎞
⎠ |
then:
Input:
eval(ans())
Output:
- Transform a string into a list of its characters:
Input:
chn := "abracadbra" |
seq(chn[j],j,0,size(chn)-1)
|
Output:
["a","b","r","a","c","a","d","a","b","r","a"]