MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: sum with lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40597] Re: [mg40534] sum with lists
  • From: Dr Bob <majort at cox-internet.com>
  • Date: Fri, 11 Apr 2003 01:59:03 -0400 (EDT)
  • References: <200304100026.UAA21540@smc.vnet.net>
  • Reply-to: majort at cox-internet.com
  • Sender: owner-wri-mathgroup at wolfram.com

If you wanted to sum over the entire list, this works:

d = {{x0, y0, z0}, {x1, y1, z1}, {x2, y2, z1}, {xn, yn, zn}};
foo[a_List] := First[a]Last[a]
Plus @@ foo /@ d

x0 z0 + x1 z1 + x2 z1 + xn zn

You wanted to sum only to n/2, so that might be:

bar[d : {__List}] := Plus @@ foo /@ Take[d, Floor[Length@d/2]]
bar@d

or, back to the method you wanted to use:

Sum[d[[i, 1]]d[[i, 3]], {i, 1, Floor[Length@d/2]}]

You started at index 0, but evaluate d[[0]] and you get

d[[0]]

List

That doesn't have a first part, so d[[0,1]] fails.

On large lists something like foo and bar will be MUCH faster.

Bobby

On Wed, 9 Apr 2003 20:26:04 -0400 (EDT), Nathan Moore 
<nmoore at physics.umn.edu> wrote:

> 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
>
>



-- 
majort at cox-internet.com
Bobby R. Treat



  • References:
  • Prev by Date: Re: Re: sum with lists
  • Next by Date: Re: ODE systems
  • Previous by thread: sum with lists
  • Next by thread: Re: sum with lists