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: [mg98970] Re: [mg98942] pure function with optional number of arguments
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 22 Apr 2009 06:36:17 -0400 (EDT)
  • References: <200904220912.FAA13452@smc.vnet.net>

Hi Ruth,

You can use the undocumented form of the pure function which can take any
number of arguments:

Function[Null, body[##]].

(note the SlotSequence (##)).You will have to re-name the variable of the
internal pure function though, from slot to a named var, to avoid name
collision with the slot variables ## of the external Function:

In[1] =
mypureint =
 Function[Null,  Function[limit, Integrate[z*Efun[##], {z, 0, limit}]] /@
{1, 2, 3}];

In[2] = mypureint[1]

Out[2] = {1/2, 2, 9/2}

In[3] = mypureint[1, 2]

Out[3] = {5/2, 10, 45/2}

Regards,
Leonid



On Wed, Apr 22, 2009 at 2:12 AM, Ruth Lazcoz Saez <wtplasar at lg.ehu.es>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,
>
> Ruth Lazkoz
>
>
>
>



  • Prev by Date: Re: Application GUI in Mathematica
  • Next by Date: Using a jpg picture for a 2D plot background
  • Previous by thread: pure function with optional number of arguments
  • Next by thread: Re: pure function with optional number of arguments