Re: Is it possible to "pre-evaluate" parts of pure function?
- To: mathgroup at smc.vnet.net
- Subject: [mg70300] Re: Is it possible to "pre-evaluate" parts of pure function?
- From: "Jon McLoone" <jonm at wolfram.co.uk>
- Date: Thu, 12 Oct 2006 05:37:19 -0400 (EDT)
- References: <egi22b$jd9$1@smc.vnet.net>
Function has the attribute HoldAll, so you need to use Evaluate to
override that.
You were close with
SetOptions[f, MultiplierFunction -> Evaluate[(const # &)]]
But the Evaluate needs to be on the argument of Function not around the
outside of it.
SetOptions[f, MultiplierFunction -> (Evaluate[const #] &)]
seems to do what you want.
Philpp 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