RE: Using "Sum" (i = 1 ... N) in a function definition

• To: mathgroup at smc.vnet.net
• Subject: [mg49707] RE: [mg49666] Using "Sum" (i = 1 ... N) in a function definition
• From: "David Park" <djmp at earthlink.net>
• Date: Thu, 29 Jul 2004 07:43:23 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Rainer,

MyData = {3, 2, 4, 5, 6};
MyFunc[i_] := Sum[MyData[[j]], {j, 1, i}];

MyFunc[1]
MyFunc[2]
MyFunc[3]
3
5
9

You need the DelayedSet, :=, instead of Set, =. Otherwise Mathematica tries
to evaluate the Sum statement immediately, which it can't do because it
doesn't yet have a value for i.

David Park

From: Rainer [mailto:wilhelm.rainer at gmx.net]
To: mathgroup at smc.vnet.net

I was trying to define a function using the Sum command in the
following way (simplified example):

MyData = {3,2,4,5,6};
MyFunc[i_] = Sum[MyData[[j]],{j,1,i}];

As you can see, the idea is to use the length of the list (i)
as a variable in the function definition, such that

MyFunc[1] = 3
MyFunc[2] = 3 + 2 = 5
MyFunc[3] = 3 + 2 + 4 = 9

etc. Mathematica does not like the abobe statements, and prints the
following error message:

"Part specification K\$394 is neither an integer nor a list of
integers."

Does anybody know how to define (correctly) such a function. Should be
possible ...?
Thanks
Rainer

```

• Prev by Date: Re: Combinations
• Next by Date: Re: Combinations
• Previous by thread: Re: Using "Sum" (i = 1 ... N) in a function definition
• Next by thread: Re: Using "Sum" (i = 1 ... N) in a function definition