1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Chapter 4. Introduction to Lists and Tables

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (25.81 MB, 585 trang )


230



Chapter 4 Introduction to Lists and Tables



1. Table[f[i],{i,n}] creates the list {f[1],...,f[n]};

2. Table[f[i],{i,0,n}] creates the list {f[0],...,f[n]};

3. Table[f[i],{i,n,m}] creates the list

{f[n],f[n+1],...,f[m-1],f[m]};

4. Table[f[i],{i,imin,imax,istep}] creates the list

{f[imin],f[imin+istep],f[imin+2*step],...,f[imax]};

and

5. Array[f,n] creates the list {f[1],...,f[n]}.

In particular,

Table[f[x],{x,a,b,(b-a)/(n-1)}]

returns a list of f x values for n equally spaced values of x between a and b;

Table[{x,f[x]},{x,a,b,(b-a)/(n-1)}]

returns a list of points x, f x for n equally spaced values of x between a and b.



In addition to using Table, lists of numbers can be calculated using Range:

1. Range[n] generates the list {1,2, ... , n};



4.1 Lists and List Operations



231



2. Range[n1,n2] generates the list {n1, n1+1, ... , n2-1, n2}; and

3. Range[n1,n2,nstep] generates the list

{n1, n1+nstep,n1+2*nstep, ... , n2-nstep,n2}.



EXAMPLE 4.1.1: Use Mathematica to generate the list

{1,2,3,4,5,6,7,8,9,10}.



SOLUTION: Generally, a given list can be constructed in several

ways. In fact, each of the following five commands generates the list

{1,2,3,4,5,6,7,8,9,10}.

In[603]:=



1, 2, 3, 4, 5, 6, 7, 8, 9, 10



Out[603]=



1, 2, 3, 4, 5, 6, 7, 8, 9, 10



In[604]:= Table i, i, 10

Out[604]=



1, 2, 3, 4, 5, 6, 7, 8, 9, 10



In[605]:= Table i, i, 1, 10

Out[605]=



1, 2, 3, 4, 5, 6, 7, 8, 9, 10

i

, i, 2, 20, 2

2

1, 2, 3, 4, 5, 6, 7, 8, 9, 10



In[606]:= Table

Out[606]=



In[607]:= Range 10

Out[607]=



1, 2, 3, 4, 5, 6, 7, 8, 9, 10



EXAMPLE 4.1.2: Use Mathematica to define listone to be the list of

numbers 1, 3/ 2, 2, 5/ 2, 3, 7/ 2, 4 .



SOLUTION: In this case, we generate a list and name the result

listone. As in Example 4.1.1, we illustrate that listone can be created in several ways.

3

5

7

, 2, , 3, , 4

2

2

2

3

5

7

1, , 2, , 3, , 4

2

2

2



In[608]:= listone

Out[608]=



1,



232



Chapter 4 Introduction to Lists and Tables



In[609]:= listone

Out[609]=



1,



1

2



and use Array to create the table listone.



n

2



In[610]:= i n



listone



Out[610]=



1

2



3

5

7

, 2, , 3, , 4

2

2

2

1

2n



Last, we define i n



Table i, i, 1, 4,



1

2

Array i, 7



3

5

7

1, , 2, , 3, , 4

2

2

2



EXAMPLE 4.1.3: Create a list of the first 25 prime numbers. What is the

fifteenth prime number?



SOLUTION: The command Prime[n] yields the nth prime number.

We use Table to generate a list of the ordered pairs {n,Prime[n]}

for n 1, 2, 3, . . . , 25.

In[611]:= list

Out[611]=



Table



n, Prime n



, n, 1, 25



1, 2 , 2, 3 , 3, 5 , 4, 7 , 5, 11 , 6, 13 ,

7, 17 , 8, 19 , 9, 23 , 10, 29 , 11, 31 ,

12, 37 , 13, 41 , 14, 43 , 15, 47 , 16, 53 ,

17, 59 , 18, 61 , 19, 67 , 20, 71 , 21, 73 ,

22, 79 , 23, 83 , 24, 89 , 25, 97



The ith element of a list list is extracted from list with list[[i]].

From the resulting output, we see that the fifteenth prime number is 47.

In[612]:= list

Out[612]=



15



15, 47



In addition, we can use Table to generate lists consisting of the same or similar

objects.



4.1 Lists and List Operations



233



EXAMPLE 4.1.4: (a) Generate a list consisting of five copies of the

letter a. (b) Generate a list consisting of ten random integers between

10 and 10.



SOLUTION: Entering

In[613]:= Table a, 5

Out[613]=



a, a, a, a, a



generates a list consisting of five copies of the letter a. For (b), we use

the command Random to generate the desired list. Because we are using

Random, your results will certainly differ from those obtained here.

In[614]:= Table Random Integer,

Out[614]=



10, 10



, 10



4, 2, 10, 2, 10, 0, 8, 7, 3, 0



4.1.2 Plotting Lists of Points

Lists are plotted with ListPlot.

1. ListPlot[{{x1,y1},{x2,y2},...,{xn,yn}}] plots the list of

points x1 , y1 , x2 , y2 , . . . , xn , yn . The size of the points in the resulting

plot is controlled with the option PlotStyle->PointSize[w], where

w is the fraction of the total width of the graphic. For two-dimensional

graphics, the default value is 0.008.

2. ListPlot[{y1,y2,..,yn}] plots the list of points 1, y1 , 2, y2 , . . . ,

n, yn .



EXAMPLE 4.1.5: Entering

In[615]:= t1



Table Sin n , n, 1, 1000



ListPlot t1



creates a list consisting of sin n for n 1, 2, . . . , 1000 and then graphs

the list of points n, sin n for n 1, 2, . . . , 1000. See Figure 4-1.



When a semi-colon is

included at the end of a

command, the resulting

output is suppressed.



234



Chapter 4 Introduction to Lists and Tables



1

0.5



200



400



600



800



1000



-0.5

-1

Figure 4-1 Plot of n, sin n for n



1, 2, . . . , 1000



EXAMPLE 4.1.6 (The Prime Difference Function and the Prime Number Theorem): In t1, we use Prime and Table to compute a list of the

first 25, 000 prime numbers.

In[616]:= t1



Table Prime n , n, 1, 25000



We use Length to verify that t1 has 25, 000 elements and Short to see

an abbreviated portion of t1.

In[617]:= Length t1

Out[617]= 25000

In[618]:= Short t1

Out[618]=



First[list] returns the

first element of list;

Last[list] returns the

last element of list.



2, 3, 5, 7, 11, 13, 24988 , 287059, 287087,

287093, 287099, 287107, 287117



You can also use Take to extract elements of lists.

1. Take[list,n] returns the first n elements of list;

2. Take[list,-n] returns the last n elements of list; and

3. Take[list,{n,m}] returns the nth through mth elements of list.

In[619]:= Take t1, 5

Out[619]=



2, 3, 5, 7, 11



In[620]:= Take t1, 5

Out[620]=



287087, 287093, 287099, 287107, 287117



4.1 Lists and List Operations



235



80



60



40



20



5000



Figure 4-2



10000



15000



20000



25000



A plot of the difference, dn , between successive prime numbers



In[621]:= Take t1, 12501, 12505

Out[621]=



134059, 134077, 134081, 134087, 134089



However, you can use Table together with Part ([[...]]) to obtain

the same results as those obtained with Take.

In[622]:= Table t1



i



, i, 1, 5



Table t1



i



, i, 24996, 25000



Out[622]=



Table t1 i , i, 12501, 12505

2, 3, 5, 7, 11



Out[622]=



287087, 287093, 287099, 287107, 287117



Out[622]=



134059, 134077, 134081, 134087, 134089



In t2, we compute the difference, dn , between the successive prime

numbers in t1. The result is plotted with ListPlot in Figure 4-2.

In[623]:= t2



Table t1 i 1

i, 1, Length t1



t1

1



i



,



In[624]:= Short t2

Out[624]=



1, 2, 2, 4, 2, 4, 2, 4, 6, 2, 6, 4, 2, 4, 24972

46, 8, 6, 12, 4, 44, 10, 2, 28, 6, 6, 8, 10



In[625]:= ListPlot t2, PlotRange



,



All



Let Π n denote the number of primes less than n and Li x denote the

logarithmic integral:

x



LogIntegral x



Li x

0



1

dt.

ln t



list[[i]] returns the ith

element of list so

list i 1

list i

computes the difference

between the i 1 st and ith

elements of list.



236



Chapter 4 Introduction to Lists and Tables



We use Plot to graph Li x for 1

In[626]:= p1



x



25, 000 in p1.



Plot LogIntegral x , x, 1, 25000



The Prime Number Theorem states that

Π n ∼ Li n .

(See [20].) In the following, we use Select and Length to define Π n .

Select[list,criteria] returns the elements of list for which

criteria is true. Note that #
argument #, #
the end of a pure function. Thus, given n, Select[t1,#
a list of the elements of t1 less than n; Select[t1,#
returns the number of elements in the list.

In[627]:= smallpi n



Select t1, # < n& //Length



For example,

In[628]:= smallpi 100

Out[628]= 25



shows us that Π 100

25. Note that because t1 contains the first 25, 000

primes, smallpi[n] is valid for 1 n N where Π N

25, 000. In t3,

we compute Π n for n 1, 2, . . . , 25, 000

In[629]:= t3



Table smallpi n , n, 1, 25000



In[630]:= Short t3

Out[630]=



0, 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 24978 , 2762,

2762, 2762, 2762, 2762, 2762, 2762, 2762,

2762, 2762, 2762



and plot the resulting list with ListPlot.

In[631]:= p2



ListPlot t3, PlotStyle



GrayLevel 0.4



p1 and p2 are displayed together with Show in Figure 4-3.

In[632]:= Show p1, p2



4.1 Lists and List Operations



237



2500

2000

1500

1000

500

5000



10000 15000 20000 25000



Figure 4-3 Graphs of Li x (in black) and Π n (in gray)



You can iterate recursively with Table. Both

In[633]:= t1 Table a i, j , j, 2, 10, 2 , i, 1, 5

Out[633]=

a 1, 2 , a 2, 2 , a 3, 2 , a 4, 2 , a 5, 2 ,

a 1, 4 , a 2, 4 , a 3, 4 , a 4, 4 , a 5, 4 ,

a 1, 6 , a 2, 6 , a 3, 6 , a 4, 6 , a 5, 6 ,

a 1, 8 , a 2, 8 , a 3, 8 , a 4, 8 , a 5, 8 ,

a 1, 10 , a 2, 10 , a 3, 10 , a 4, 10 , a 5, 10

In[634]:= Length t1

Out[634]= 5



and

In[635]:= t2 Table Table a i, j , i, 1, 5 , j, 2, 10, 2

Out[635]=

a 1, 2 , a 2, 2 , a 3, 2 , a 4, 2 , a 5, 2 ,

a 1, 4 , a 2, 4 , a 3, 4 , a 4, 4 , a 5, 4 ,

a 1, 6 , a 2, 6 , a 3, 6 , a 4, 6 , a 5, 6 ,

a 1, 8 , a 2, 8 , a 3, 8 , a 4, 8 , a 5, 8 ,

a 1, 10 , a 2, 10 , a 3, 10 , a 4, 10 , a 5, 10



compute tables of ai j . The outermost iterator is evaluated first: in this case, i is

followed by j as in t1 and the result is a list of lists. To eliminate the inner lists

(that is, the braces), use Flatten. Generally, Flatten[list,n] flattens list

(removes braces) to level n.

In[636]:= Flatten t1

Out[636]= a 1, 2 , a 2, 2 , a 3, 2 , a 4, 2 , a 5, 2 , a 1, 4 ,

a 2, 4 , a 3, 4 , a 4, 4 , a 5, 4 , a 1, 6 , a 2, 6 ,

a 3, 6 , a 4, 6 , a 5, 6 , a 1, 8 , a 2, 8 ,

a 3, 8 , a 4, 8 , a 5, 8 , a 1, 10 , a 2, 10 ,

a 3, 10 , a 4, 10 , a 5, 10



238



Length[list] returns the

number of elements in list.



Chapter 4 Introduction to Lists and Tables



The observation is especially important when graphing lists of points obtained by

iterating Table. For example,

In[637]:= t1

Out[637]=



Table



Sin x



y , Cos x



y



, x, 1, 5 , y, 1, 5



Sin 2 , 1 , Sin 3 , Cos 1 , Sin 4 , Cos 2 ,

Sin 5 , Cos 3 , Sin 6 , Cos 4

, Sin 3 , Cos 1 ,

Sin 4 , 1 , Sin 5 , Cos 1 , Sin 6 , Cos 2 ,

Sin 7 , Cos 3

, Sin 4 , Cos 2 , Sin 5 , Cos 1 ,

Sin 6 , 1 , Sin 7 , Cos 1 , Sin 8 , Cos 2

,

Sin 5 , Cos 3 , Sin 6 , Cos 2 , Sin 7 , Cos 1 ,

Sin 8 , 1 , Sin 9 , Cos 1

, Sin 6 , Cos 4 ,

Sin 7 , Cos 3 , Sin 8 , Cos 2 , Sin 9 , Cos 1 ,

Sin 10 , 1



In[638]:= Length t1

Out[638]= 5



is not a list of 25 points: t1 is a list of 5 lists each consisting of 5 points. t1 has two

levels. For example, the 3rd element of the second level is

In[639]:= t1

Out[639]=



3

Sin 4 , Cos 2 , Sin 5 , Cos 1 , Sin 6 , 1 ,

Sin 7 , Cos 1 , Sin 8 , Cos 2



and the 2nd element of the third level is

In[640]:= t1

Out[640]=



3, 2



Sin 5 , Cos 1



To flatten t2 to level 1, we enter

In[641]:= t2

Out[641]=



Flatten t1, 1

Sin 2 , 1 ,

Sin 5 , Cos

Sin 4 , 1 ,

Sin 7 , Cos

Sin 6 , 1 ,

Sin 5 , Cos

Sin 8 , 1 ,

Sin 7 , Cos

Sin 9 , Cos



Sin 3 , Cos 1 ,

3 , Sin 6 , Cos

Sin 5 , Cos 1 ,

3 , Sin 4 , Cos

Sin 7 , Cos 1 ,

3 , Sin 6 , Cos

Sin 9 , Cos 1 ,

3 , Sin 8 , Cos

1 , Sin 10 , 1



Sin 4 , Cos 2 ,

4 , Sin 3 , Cos 1

Sin 6 , Cos 2 ,

2 , Sin 5 , Cos 1

Sin 8 , Cos 2 ,

2 , Sin 7 , Cos 1

Sin 6 , Cos 4 ,

2 ,



,

,

,



and see the result is a list of points. These are plotted with ListPlot in Figure 4-4 (a). We also illustrate the use of the PlotStyle, PlotRange, and

AspectRatio options in the ListPlot command.



4.1 Lists and List Operations



In[642]:= lp1



239



ListPlot t2, PlotStyle

PointSize 0.05 ,

GrayLevel 0.5 , PlotRange

3/ 2, 3/ 2 ,

3/2, 3/2 , AspectRatio Automatic



Increasing the number of points further illustrates the use of Flatten. Entering

In[643]:= t1



Table



Sin x y , Cos x y



, x, 1, 125 , y, 1, 125



In[644]:= Length t1

Out[644]= 125



results in a very long nested list. t1 has 125 elements each of which has

125 elements.

An abbreviated version is viewed with Short.

In[645]:= Short t1

Out[645]=



Sin 2 , 1 , Sin 3 , Cos 1 , 121 ,

Sin 125 , Cos 123 , Sin 126 , Cos 124

123 ,

1



,



After using Flatten, we see with Length and Short that t2 contains 15, 625

points,

In[646]:= t2



Flatten t1, 1



In[647]:= Length t2

Out[647]= 15625

In[648]:= Short t2

Out[648]=



Sin 2 , 1 , Sin 3 , Cos 1 , 15621

Sin 249 , Cos 1 , Sin 250 , 1



,l



which are plotted with ListPlot in Figure 4-4 (b).

In[649]:= lp2



ListPlot t2, AspectRatio



In[650]:= Show GraphicsArray



Automatic



lp1, lp2



Remark. Mathematica is very flexible and most calculations can be carried out in

more than one way. Depending on how you think, some sequences of calculations may make more sense to you than others, even if they are less efficient than

the most efficient way to perform the desired calculations. Often, the difference in

time required for Mathematica to perform equivalent – but different – calculations

is quite small. For the beginner, we think it is wisest to work with familiar calculations first and then efficiency.



Short[list] yields an

abbreviated version of list.



240



Chapter 4 Introduction to Lists and Tables



1



1.5

1



0.5

0.5



-1.5



-1



-0.5



0.5



1



1.5



-1



-0.5



0.5



-0.5

-0.5

-1

-1.5



-1



(a)



(b)

Figure 4-4 (a) and (b)



Observe that xn 1 f xn

can also be computed with

xn 1 f n x0 .



EXAMPLE 4.1.7 (Dynamical Systems): A sequence of the form xn 1

f xn is called a dynamical system.

Sometimes, unusual behavior can be observed when working with

dynamical systems. For example, consider the dynamical system with

1.2. Note that we define xn using the

f x

x 2.5x 1 x and x0

form x[n_]:=x[n]=... so that Mathematica remembers the functional values it computes and thus avoids recomputing functional values previously computed. This is particularly advantageous when we

compute the value of xn for large values of n.

In[651]:= Clear f, x

2. 5 x 1



f x



x



x n



x n



x 0



x



f x n



1



1 .2



In Figure 4-5, we see that the sequence oscillates between 0.6 and 1.2.

We say that the dynamical system has a 2-cycle because the values of

the sequence oscillate between two numbers.

In[652]:= tb



Table x n , n, 1, 200



In[653]:= Short tb, 20



1



Xem Thêm
Tải bản đầy đủ (.pdf) (585 trang)

×