2.2.6 Building Lists from Functions
| Array[f, n] | generate a length n list of the form {f[1], f[2], ... } | Array[f, { , , ... }] | generate an nested list, each of whose entries consists of f applied to its indices | | NestList[f, x, n] | generate a list of the form {x, f[x], f[f[x]], ... }, where f is nested up to n deep | | FoldList[f, x, {a, b, ... }] | generate a list of the form {x, f[x, a], f[f[x, a], b], ... } | ComposeList[{ , , ... }, x] | generate a list of the form {x, [x], [ [x]], ... } |
Making lists from functions. | This makes a list of 5 elements, each of the form p[i]. | |
Out[1]=
|
|
| Here is another way to produce the same list. | |
In[2]:=
Table[p[i], {i, 5}]
|
Out[2]=
|
|
This produces a list whose elements are . | |
In[3]:=
Array[ # + #^2 &, 5]
|
Out[3]=
|
|
This generates a matrix whose entries are m[i, j]. | |
Out[4]=
|
|
This generates a matrix whose elements are the squares of the sums of their indices. | |
In[5]:=
Array[Plus[##]^2 &, {3, 3}]
|
Out[5]=
|
|
NestList and FoldList were discussed in Section 2.2.2. Particularly by using them with pure functions, you can construct some very elegant and efficient Mathematica programs. This gives a list of results obtained by successively differentiating with respect to . | |
In[6]:=
NestList[ D[#, x]&, x^n, 3 ]
|
Out[6]=
|
|
|