MathGroup Archive 2009

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

Search the Archive

Re: pure function with optional number of arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98971] Re: pure function with optional number of arguments
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Wed, 22 Apr 2009 06:36:28 -0400 (EDT)
  • References: <gsmmvk$d24$1@smc.vnet.net>

Ruth Lazcoz Saez wrote:
> Hi,
> 
> I have two definitions for a function, one in the case it has one  
> argument and another one if it has two.
> 
> Efun[x_]:=x^2
> Efun[x_,y_]:=x^2+y^2
> 
> Then I want to construct a pure function that does the same thing as  
> this non-pure function
> 
> myint[params__] := Integrate[z*Efun[params], {z, 0, #}] & /@ {1, 2, 3}
> 
> I tried to accomplish it with
> 
> myintpure= Function[params, Integrate[z*Efun[params], {z, 0, #}] & /@  
> {1, 2, 3}],
> 
> but myintpure[x,y] gives not the same as myint[x,y], so this pure  
> function I have constructed seems to be not right.
> 
> Help will be much appreciated. Thanks,

The trick is to use ## as argument, like this:

myintpure = Function[
  Function[{xmax}, Integrate[z*Efun[##], {z, 0, xmax}]] /@ {1, 2, 3}
  ]

where I have changed the inner pure function to use a named argument to
avoid confusion. Of course you are in trouble when needing deeply nested
constructs of this sort, but that is something you probably want to
avoid (for readability) anyway.

hth,

albert





  • Prev by Date: Re: pure function with optional number of arguments
  • Next by Date: Re: pure function with optional number of arguments
  • Previous by thread: Re: pure function with optional number of arguments
  • Next by thread: Re: pure function with optional number of arguments