RE: sum with lists
- To: mathgroup at smc.vnet.net
- Subject: [mg40641] RE: [mg40534] sum with lists
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 12 Apr 2003 03:08:01 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: Nathan Moore [mailto:nmoore at physics.umn.edu] To: mathgroup at smc.vnet.net >Sent: Thursday, April 10, 2003 2:26 AM >To: mathgroup at smc.vnet.net >Subject: [mg40641] [mg40534] sum with lists > > >Another list question. Suppose that I have the list > d={ > {x0,y0,z0}, > {x1,y1,z1}, > {x2,y2,z1}, > ... > {xn,yn,zn}} > >and now I'd like to calculate the sum, > Sum[x[i]*z[i],{i,0,n/2}] > >I tried > Sum[d[[i, 1]]*d[[i, 3]], {i, 0, 10}] >with little success > >thanks, > >Nathan Moore, >University of Minnesota Physics > Nathan, try In[1]:= d = Table[{Unique["a"], Unique["b"], Unique["c"]}, {9}] Out[1]= {{a1, b1, c1}, {a2, b2, c2}, {a3, b3, c3}, {a4, b4, c4}, {a5, b5, c5}, {a6, b6, c6}, {a7, b7, c7}, {a8, b8, c8}, {a9, b9, c9}} In[2]:= Dot @@ Transpose@Take[d, Floor[Length[d]/2]][[All, {1, 3}]] Out[2]= a1 c1 + a2 c2 + a3 c3 + a4 c4 In[3]:= Dot @@ (Transpose@Take[d, Floor[Length[d]/2]])[[{1, 3}]] Out[3]= a1 c1 + a2 c2 + a3 c3 + a4 c4 In[4]:= Dot @@ (Take[#, Floor[Length[d]/2]] &) /@ Transpose[d][[{1, 3}]] Out[4]= a1 c1 + a2 c2 + a3 c3 + a4 c4 Check which is most effective. -- Hartmut Wolf