MathGroup Archive 2006

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

Search the Archive

Re: Is it possible to "pre-evaluate" parts of pure function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg70310] Re: [mg70286] Is it possible to "pre-evaluate" parts of pure function?
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Thu, 12 Oct 2006 05:37:47 -0400 (EDT)
  • References: <200610110554.BAA19488@smc.vnet.net>

Is there any reason you keep switching between Rule and RuleDelayed?
RuleDelayed Doesn't seem like it is necessary for your purposes.

You are running into problems with the HoldAll attribute of Function
(and sometimes, the HoldRest attribute of RuleDelayed).

If you don't like it, there are several methods you could use to get around it.

In this case, I recommend Block

const=5;
SetOptions[f,MultiplierFunction->Block[{Function},(const #&)]]


On 10/11/06, Philpp <piotr at bigpond.net.au> wrote:
> This is a problem that's been bugging me for a long time now. Consider
> the following function definition:
>
> In[1]:=  Clear[f];
>          f[x_, opts___] :=
>             Module[
>               {multiplierfun =
>                   MultiplierFunction /. {opts} /. Options[f]},
>               multiplierfun[x^2]
>              ];
>          Options[f] = {MultiplierFunction :> (2 # &)};
>
> In[4]:=  f[2]
> Out[4]=  8
>
> Changing the MultiplierFunction option changes the result
>
> In[5]:=  f[2, MultiplierFunction :> (3 # &)]
> Out[5]=  12
>
> Now, consider the following
>
> In[6]:=  const = 4;
>          f[2, MultiplierFunction :> (const # &)]
> Out[7]=  16
>
> Again, the result is as expected.
>
>
> >From now on, I would like to "fix" the pure function with the present
> value of const. The solution that immediately comes to mind is
>
> In[8]:=  SetOptions[f, MultiplierFunction :> (const # &)]
> Out[8]=  {MultiplierFunction :> (const #1 &)}
>
> this is not what I intended, I expected
>
> {MultiplierFunction :> (4 #1 &)}
>
> since the const value is 4.
>
> Trying any of the following doesn't work either
>
> In[9]:=  SetOptions[f, MultiplierFunction -> (const # &)]
>          SetOptions[f, MultiplierFunction -> Evaluate[(const # &)]]
>          SetOptions[f, MultiplierFunction -> ReleaseHold[(const # &)]]
>
> Out[9]=  {MultiplierFunction -> (const #1 &)}
> Out[10]= {MultiplierFunction -> (const #1 &)}
> Out[11]= {MultiplierFunction -> (const #1 &)}
>
> the symbol const remains unevaluated.
>
> The only way to even approach what I want is to define a named function
> g
>
> In[12]:= (Clear[r, g]; g[r_] = (const r))
>          SetOptions[f, MultiplierFunction -> g]
>
> Out[12]= 4 r
> Out[13]= {MultiplierFunction -> g}
>
> In[14]:= ?g
>          g[r_] = 4 r
>
> However, this is not a solution to my problem. It only shifts the focus
> from const onto g symbol, i.e., now g could be redefined before f is
> executed.
>
> So, is it possible to "pre-evaluate" parts of pure function?
>
> Can anybody offer any suggestions?
>
> Cheers,
>
> Philipp
>
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Re: Automate datafitting to a series of parameterized function
  • Next by Date: Re: Automate datafitting to a series of parameterized function
  • Previous by thread: Is it possible to "pre-evaluate" parts of pure function?
  • Next by thread: Re: Is it possible to "pre-evaluate" parts of pure function?