• Aucun résultat trouvé

% Length nums

3.2 Creating and measuring lists

List construction

In addition to using theList function to collect data objects, you can also generate lists from scratch by creating the objects and then placing them in a list.

Range[imin,imax, di] generates a list of ordered numbers starting from imin and going up to, but not exceeding,imax in increments of di.

In[1]:= Range 4, 7, 3

Out[1]= 4, 1, 2, 5

If di is not specified, a value of one is used.

In[2]:= Range 4, 8

Out[2]= 4, 5, 6, 7, 8

If neitherimin nordi is specified, then both are given the value of 1.

In[3]:= Range 4

Out[3]= 1, 2, 3, 4

It is not necessary forimin, imax, ordi to be integers.

In[4]:= Range 1.5, 6.3, .75

Out[4]= 1.5, 2.25, 3., 3.75, 4.5, 5.25, 6.

Table[expr,{i,imin,imax,di}] generates a list by evaluating expr a number of times.

In[5]:= Table 3 k, k, 1, 10, 2

Out[5]= 3, 9, 15, 21, 27

The first argument,3k in the above example, is the expression that is evaluated to produce the elements in the list. The second argument to theTable function,{i,imin,imax,di}, is called theiterator. It is a list that specifies the number of times the expression is evaluated and hence the number of elements in the list. The iterator variable may or may not appear in the expression being evaluated. The valueimin is the value ofi used in the expression to create the first list element. The value di is the incremental increase in the value ofi used in the expression to create additional list elements. The valueimax is the maximum value ofi used in the expression to create the last list element (if incrementingi bydi gives a value greater than imax, that value is not used).

In[6]:= Table i, i, 1.5, 6.3, .75

Out[6]= 1.5, 2.25, 3., 3.75, 4.5, 5.25, 6.

Table[i,{i,imin,imax,di}] is equivalent to Range[imin,imax,di]. As with the Range function, the arguments toTable can be simplified when the iterator increment is one.

In[7]:= Table 3 i, i, 2, 5

Out[7]= 6, 9, 12, 15

Similarly, bothimin and di can be omitted and are then assumed to be 1.

In[8]:= Table i2, i, 4

Out[8]= 1, 4, 9, 16

If the iterator variable does not appear in the expression being evaluated, it may be omitted as well. The expression will then simply be evaluated that many times.

In[9]:= Table Random , 3

Out[9]= 0.155408, 0.0408563, 0.62081

The expression that the Table function evaluates can be completely arbitrary. In the following computation, it is used to create tables of formulas.

In[10]:= Table Expand 1 i , i, 1, 3

Out[10]= 1 , 1 2 2, 1 3 3 2 3

Table can be used to create a nested list; that is, a list containing other lists as ele-ments. This can be done by using additional iterators.

In[11]:= Table i j, j, 1, 4 , i, 1, 3

Out[11]= 2, 3, 4 , 3, 4, 5 , 4, 5, 6 , 5, 6, 7

When there is more than one iterator, their order of appearance is important, because the value of the outer iterator is varied for each value of the inner iterator In the above exam-ple, for each value ofj (the inner iterator), i was varied from 1 to 3, producing a three-ele-ment list for each of the four values ofj. If you reverse the iterator order, you will get an entirely different list.

In[12]:= Table i j, i, 1, 3 , j, 1, 4

Out[12]= 2, 3, 4, 5 , 3, 4, 5, 6 , 4, 5, 6, 7

You will often find it useful to display nested lists in a matrix or tabular form.

In[13]:= Table i j, i, 1, 4 , j, 1, 3 TableForm

The value of the outer iterator may depend on the value of the inner iterator, which can

However, the inner iterator may not depend on the outer iterator because, as we have seen, the inner iterator is fixed as the outer one varies.

In[17]:= Table i j, i, 1, j , j, 1, 3

Table::iterb :

Iterator i, 1, j doesnot have appropriate bounds. More…

Out[17]= Table i j, i, 1, j , j, 1, 3

Measuring lists

Recall from Chapter 2 thatLength[expr] is used to give the number of elements inexpr.

For a simple unnested (linear) list, theLength function tells us how many elements are in the list.

In[18]:= Length a, b, c, d, e, f

Out[18]= 6

In a nested list, each inner list is an element of the outer list. Therefore, theLength of a nested list indicates the number of inner lists, and not their sizes.

In[19]:= Length 1, 2 , 3, 4 , 5, 6 , a, b , c, d , e, f

Out[19]= 2

To find out more about the inner lists, use theDimensions function.

In[20]:= Dimensions 1, 2 , 3, 4 , 5, 6 , a, b , c, d , e, f

Out[20]= 2, 3, 2

This indicates that there are two inner lists, that each inner list contains three lists, and that the innermost lists each have two elements.MatrixForm may help to see the struc-ture better.

In[21]:= MatrixForm 1, 2 , 3, 4 , 5, 6 , a, b , c, d , e, f

The number of dimensions of a (possibly nested) list, is given by ArrayDepth.

In[22]:= ArrayDepth 1, 2 , 3, 4 , 5, 6 , a, b , c, d , e, f

Out[22]= 3

This is identical to the number of levels in that expression, as displayed byTreeForm.

In[23]:= TreeForm a, b, c

1. Generate the list {{0},{0,2},{0,2,4},{0,2,4,6},{0,2,4,6,8}} in two different ways using theTable function.

2. A table containing ten random 1s and 0s can easily be created using Table[

Random[Integer],{10}]. Create a ten-element list of random 1s, 0s and 1s.

3. Create a ten-element list of random 1s and 1s. This table can be viewed as a list of the steps taken in a random walk along thex-axis, where a step can be taken in either the positive x direction (corresponding to 1) or the negativex direction

(corresponding to 1) with equal likelihood.

The random walk in one, two, three (and even higher) dimensions is used in science and engineering to represent phenomena that are probabilistic in nature. We will use a variety of random walk models throughout this book to illustrate specific program-ming points.

4. From a mathematical point of view, a list can be viewed as a vector and a nested list containing inner lists of equal length can be viewed as a matrix (or an array). Mathe-matica has another built-in function Array which creates lists. We can use an undefined function f to see howArray works.

In[1]:= Array f, 5

Out[1]= f 1 , f 2 , f 3 , f 4 , f 5

In[2]:= Array f, 3, 4

Out[2]= f 1, 1 , f 1, 2 , f 1, 3 , f 1, 4 ,

f 2, 1 , f 2, 2 , f 2, 3 , f 2, 4 , f 3, 1 , f 3, 2 , f 3, 3 , f 3, 4 Generate both of these lists using theTable function.

5. Predict the dimensions of the list{{{1,a},{4,d}},{{2,b},{3,c}}}. Use the Dimensions function to check your answer.