Re: Help please: Summing a list
- To: mathgroup at smc.vnet.net
- Subject: [mg13498] Re: [mg13480] Help please: Summing a list
- From: BobHanlon at aol.com
- Date: Fri, 31 Jul 1998 04:33:16 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Rob,
m = 110; P = Table[6, {x, 0, m}]; P[[0]]
List
Note that your definition has m+1 (i.e., 111) entries in P and that the
first entry is P[[1]], not P[[0]]. P[[0]] is the Head of P, i.e.,
List.
To define PP as a function without memory:
Clear[PP];
PP[(i_Integer)?Positive /; i <= Length[P]] :=
Sum[P[[n]], {n, i}];
{PP[0], PP[1], PP[10], PP[m + 1], PP[m + 2]} ?PP
{PP[0], 6, 60, 666, PP[112]}
"Global`PP"
PP[(i_Integer)?Positive /; i <= Length[P]] := Sum[P[[n]], {n, i}]
You could also define PP as
Clear[PP];
PP[(i_Integer)?Positive /; i <= Length[P]] :=
Plus @@ Take[P, i];
{PP[0], PP[1], PP[10], PP[m + 1], PP[m + 2]} ?PP
{PP[0], 6, 60, 666, PP[112]}
"Global`PP"
PP[(i_Integer)?Positive /; i <= Length[P]] := Plus @@ Take[P, i]
To define PP as a function with memory:
Clear[PP];
PP[(i_Integer)?Positive /; i <= Length[P]] :=
PP[i] = Sum[P[[n]], {n, i}];
{PP[0], PP[1], PP[10], PP[m + 1], PP[m + 2]} ?PP
{PP[0], 6, 60, 666, PP[112]}
"Global`PP"
PP[1] = 6
PP[10] = 60
PP[111] = 666
PP[(i_Integer)?Positive /; i <= Length[P]] := PP[i] = Sum[P[[n]], {n,
i}]
To define PP as an array and assign values only when called:
Clear[PP, elemPP];
PP = Array[elemPP, Length[P]];
elemPP[(i_Integer)?Positive /; i <= Length[P]] :=
elemPP[i] = Sum[P[[n]], {n, i}];
{PP[[0]], PP[[1]], PP[[10]], PP[[m + 1]]} ?PP
{List, 6, 60, 666}
"Global`PP"
PP = {elemPP[1], elemPP[2], elemPP[3], ..., elemPP[110], elemPP[111]}
(* middle entries deleted *)
To define PP as an array and assign all values:
Clear[PP, elemPP];
PP = Array[elemPP, Length[P]];
Do[PP[[i]] = Sum[P[[n]], {n, i}], {i, Length[P]}]; {PP[[0]], PP[[1]],
PP[[10]], PP[[m + 1]]} ?PP
{List, 6, 60, 666}
"Global`PP"
PP = {6, 12, 18, ..., 660, 666}
(* middle entries deleted *)
Bob Hanlon
In a message dated 7/26/98 5:51:08 AM, robpetersonSPAMME at iname.com
wrote:
>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. I try
>
>PP[[i]]:=Sum[P[[n]],{n,0,i}];
>
>At the definition, I get the following error: Part::"pspec":
> "Part specification \!\(n\) is neither an integer nor a list of
>integers."
>
>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?