Previous Up Next

6.1.2  Making a sequence or a list

The seq command or $ operator can create a sequence or a list.

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.

Remarks.

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 following commands are equivalent:

seq(j^2,j=-1..3)

or

(j^2)$(j=-1..3)
     
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:

seq(j^3,j,1..4)

or:

seq(j^3,j=1..4)

or:

(j^3)$(j=1..4)
     
1,8,27,64           

To create a list:

seq(j^3,j,1,4)
     

1,8,27,64
          

To create a sequence:

seq(j^3,j=-1..4,2)
     
−1,1,27           

To create a list:

seq(j^3,j,-1,4,2)
     

−1,1,27
          
seq(j^3,j,0,5,2)
     

0,8,64
          
seq(j^3,j,5,0,-2)

or:

seq(j^3,j,5,0,2)
     

125,27,1
          
seq(j^3,j,1,3,0.5)
     

1,3.375,8.0,15.625,27.0
          
seq(j^3,j,1,3,1/2)
     



1,
27
8
,8,
125
8
,27


          

To create a list with several copies of the same element:

seq(t,4)
     

t,t,t,t
          

To create a sequence with several copies of the same element:

seq(t,k=1..4)

or:

t$4
     
t,t,t,t           

Examples of sequences being used

Find the third derivative of ln(t) (see Section 13.2.1):

diff(log(t),t$3)
     
2
t3
          
l:=[[2,3],[5,1],[7,2]] seq((l[k][0])$(l[k][1]),k=0..size(l)-1)
     

2,2,2
,
5
,
7,7
          

then:

eval(ans())
     
2,2,2,5,7,7           

Transform a string into a list of its characters:

chn:="abracadbra":; seq(chn[j],j,0,size(chn)-1)
     

“a”,“b”,“r”,“a”,“c”, “a”,“d”,“a”,“b”,“r”,“a”
          

Previous Up Next