MathGroup Archive 1998

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

Search the Archive

Re: Help please: Summing a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13491] Re: Help please: Summing a list
  • From: scottb (Scott Brown)
  • Date: Fri, 31 Jul 1998 04:33:12 -0400
  • Organization: Wolfram Research, Inc.
  • References: <6pebm8$i6j@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

robpetersonSPAMME at iname.com writes:

>I make a list of numbers, eg:

>P=Table[6,{x,0,110}]; (I have a more interesting list to use later if I
>get this working)

>Now I want to make another list PP in which each entry PP[[i]] is the
>sum of P's first i entries.  


  If Plus, instead of Sum, will do, you could try this. If you
  really want Sum, you can adapt this easily.

  In[1]:= foo = Range[10]
    
  Out[1]= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}       
     
  In[2]:= partsum[l_] := MapIndexed[ Apply[ Plus, Take[ l, First[#2]
]]&, l]
    
  In[3]:= partsum[foo]
    
  Out[3]= {1, 3, 6, 10, 15, 21, 28, 36, 45, 55}
               


  Some specific comments about the problems you encountered follow.


  Your approach was very C-like. Mathematica includes
  lots of functions (e.g., MapIndexed) for manipulating Lists.
  Using these functions is faster and more robust than writing
  your own index-referencing code (you can avoid things like
  off-by-one errors, for instance).
                                            
> I try
>PP[[i]]:=Sum[P[[n]],{n,0,i}];

  Note: Mathematica Lists start their indices at 1. Also,
  you would use i_, not i, on the left side of := to do
  this. However, you shouldn't do this :), see above.

>At the definition, I get the following error: Part::"pspec": 
>    "Part specification \!\(n\) is neither an integer nor a list of
>integers."

  The problem isn't due to Sum, it's because you're telling
  Mathematica to assign to PP[[i]], and i is not defined to
  have a numeric value yet. 

In[1]:= somesum := Sum[ P[[n]], {n, 0, i} ]

In[2]:= PP[[i]] := somesum

SetDelayed::pspec:
   Part specification i is neither an integer nor a list of integers.

Out[2]= $Failed

In[3]:= PP[[j]] := 2

SetDelayed::pspec:
   Part specification j is neither an integer nor a list of integers.

Out[3]= $Failed

In[4]:= PP[[i_]] := 2

SetDelayed::pspec:
   Part specification i_ is neither an integer nor a list of integers.

Out[4]= $Failed
                          


>I don't know how to make n an integer.  In the definition of Sum[], it
>seems n is an integer unless you add a forth parameter "di" in the
>specification list such as 
>Sum[f, {i, imin, imax, di}]

  

>Can anyone help me to generate this second list?

>Thanks, Rob


------------------------------------------------------------------------
| Scott Brown        | "I may be speaking from Wolfram Research, Inc.,
| | scottb at wolfram.com | but that doesn't mean I'm speaking for them." 
|
------------------------------------------------------------------------


  • Prev by Date: Question about the ViewCenter option
  • Next by Date: Cluster-Analysis with Mathematica?
  • Previous by thread: Re: Help please: Summing a list
  • Next by thread: Re: Help please: Summing a list