Re: Newbie question: Which is more versatile, Table[] or Array[]?
- To: mathgroup at smc.vnet.net
- Subject: [mg94149] Re: Newbie question: Which is more versatile, Table[] or Array[]?
- From: dimitris <dimmechan at yahoo.com>
- Date: Fri, 5 Dec 2008 07:05:39 -0500 (EST)
- References: <ghb034$oii$1@smc.vnet.net>
On 5 =C4=E5=EA, 12:34, stpatryck <stpatr... at gmail.com> wrote: > I'm reading the Schaum's Outline for Mathematica and am in the chapter > concerning list manipulation. So far, the only difference I can tell > between the Array[] and Table[] commands is that with Table, you use > an expression as the first parameter/argument and with Array, you use > a function. The other parameters, of course, are tailored differently > because they support the first parameter i.e. expression or function. > > Anyone know of any significant difference between these two commands > other than syntax? Please feel free to e-mail me at > stpatr... at gmail.com. > > Thanks, > Patrick Hello. At first, let me point out that you use a very good book, but as far as I remember it is based on version 4.Of course, this is not a major problem, for basic commands like Table or Array. Anyway...here is a couple of things I think you will find useful (version 5.2 is used). 1) The following commands do the same thing In[183]:= Table[Cos[i/4], {i, 1, 12}] Cos[Range[1/4, 3, 1/4]] Array[Cos[#1/4] & , 12] Out[183]= {Cos[1/4], Cos[1/2], Cos[3/4], Cos[1], Cos[5/4], Cos[3/2], Cos[7/4], Cos[2], Cos[9/4], Cos[5/2], Cos[11/4], Cos[3]} Out[184]= {Cos[1/4], Cos[1/2], Cos[3/4], Cos[1], Cos[5/4], Cos[3/2], Cos[7/4], Cos[2], Cos[9/4], Cos[5/2], Cos[11/4], Cos[3]} Out[185]= {Cos[1/4], Cos[1/2], Cos[3/4], Cos[1], Cos[5/4], Cos[3/2], Cos[7/4], Cos[2], Cos[9/4], Cos[5/2], Cos[11/4], Cos[3]} 2) Nevertheless, if you are concerned about speed, you should use Range whenever possible In[213]:= Timing[Table[Cos[i/4],{i,1,1200000}];] ClearCache[]; Timing[Cos[Range[1/4,400000,1/4]];] ClearCache[]; Timing[Array[Cos[#1/4]&,1200000];] Out[213]= {3.61 Second,Null} Out[215]= {2.703 Second,Null} Out[217]= {4.781 Second,Null} 3) Array can take also a second and a third argument as the following examples demonstrate In[230]:= Array[Cos[#1/4] & , 12, 4] Out[230]= {Cos[1], Cos[5/4], Cos[3/2], Cos[7/4], Cos[2], Cos[9/4], Cos[5/2], Cos [11/4], Cos[3], Cos[13/4], Cos[7/2], Cos[15/4]} In[231]:= Array[Cos[#1/4] & , 12, 4, Min] Out[231]= Cos[13/4] In[233]:= Array[Cos[#1/4] & , 6, 4, Plus] Out[233]= Cos[1] + Cos[5/4] + Cos[3/2] + Cos[7/4] + Cos[2] + Cos[9/4] 4) The starting value for Array doesn't have to be an integer. In[234]:= Array[Cos, 5, t] Out[235]= {Cos[t], Cos[1 + t], Cos[2 + t], Cos[3 + t], Cos[4 + t]} 5) For other nice examples of Array try MatrixForm[Array[f, {5, 3}]] MatrixForm[Array[f, {5, 3}, 10]] MatrixForm[Array[f, {5, 3}, {4, 10}]] and notice the outputs. HTH, Dimitris