Is it possible to "pre-evaluate" parts of pure function?
- To: mathgroup at smc.vnet.net
- Subject: [mg70286] Is it possible to "pre-evaluate" parts of pure function?
- From: "Philpp" <piotr at bigpond.net.au>
- Date: Wed, 11 Oct 2006 01:54:12 -0400 (EDT)
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
- Follow-Ups:
- Re: Is it possible to "pre-evaluate" parts of pure function?
- From: "Chris Chiasson" <chris@chiasson.name>
- Re: Is it possible to "pre-evaluate" parts of pure function?