MathGroup Archive 2004

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49720] Re: Using "Sum" (i = 1 ... N) in a function definition
  • From: "Peltio" <peltio at twilight.zone>
  • Date: Thu, 29 Jul 2004 07:44:30 -0400 (EDT)
  • References: <ce5d8p$b4k$1@smc.vnet.net>
  • Reply-to: "Peltio" <peltioNOSP at Mdespammed.com.invalid>
  • Sender: owner-wri-mathgroup at wolfram.com

"Rainer" wrote

>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}];

I suggest that you use a delayed assignment in the definition of your
function MyFunct, here.

    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

You can obtain the same result via

    MyFunc[i_] := Plus @@ Take[MyData, i]

(in version 5 Total@ can be used instead of Plus@@)

cheers,
Peltio
Invalid address in reply-to. Crafty demunging required to mail me.


  • Prev by Date: Re: Any ways to recover a broken file?
  • Next by Date: Re: Using "Sum" (i = 1 ... N) in a function definition
  • 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