[Date Index]
[Thread Index]
[Author Index]
Re: Holding arguments of a family of functions
*To*: mathgroup at smc.vnet.net
*Subject*: [mg112254] Re: Holding arguments of a family of functions
*From*: Leonid Shifrin <lshifr at gmail.com>
*Date*: Tue, 7 Sep 2010 02:00:12 -0400 (EDT)
Hi,
Indeed, rather hard to do. The short answer seems to be that no, you can't,
if you insist on SubValues-based definition. However, the following will
presumably do what you want (I just made up some example definitions),
in terms of the syntax of function calls at least:
In[91]:=
Clear[f];
Module[{ff},
SetAttributes[ff, HoldAll];
f[x_, y_] :=
Function[z, ff[x, y, z], HoldAll] /;
MemberQ[Stack[_], HoldForm[f[_, _][args___]]];
Evaluate[f[1, 2][y_]] := y^2;
Evaluate[f[1, 3][y_]] := y^3;
Evaluate[f[2, 1][y_]] := y^4;
Evaluate[f[1, 1][y_]] := Head[Unevaluated[y]];
]
In[93]:= f[1, 2]
Out[93]= f[1, 2]
In[88]:= f[1, 2][t]
Out[88]= t^2
In[94]:= f[1, 3][t]
Out[94]= t^3
In[95]:= f[2, 1][t]
Out[95]= t^4
In[96]:= f[1, 1][Print["*"]]
Out[96]= Print
The last input proves that the argument is held.
The reference to evaluation stack was needed to emulate the behavior when
the last argument
is not supplied (see the first input). Just keep in mind that this is still
cheating - it is an emulation.
This may look like SubValues in the way you call the function, but the real
definitions here are not
SubValues-based:
In[97]:= SubValues[f]
Out[97]= {}
As far as I know, SubValues do not allow to keep "second" and other sets of
arguments held,
just by the way the evaluation sequence is set up. But, heads are evaluated
before anything else, and this
is what I exploited. Perhaps, a little more typing than you would like, but
I don't know of a shorter
way to get it. One more thing: you may want to expose ff to the top level,
or just remove the Module,
to be able to easily clear it, if accumulation of definitions and associated
with this memory consumption
becomes an issue. Just clearing <f> will not be enough in this case - my
solution in its present form potentially leaks memory (although in lots of
cases this will not be an issue).
Hope this helps.
Regards,
Leonid
On Mon, Sep 6, 2010 at 12:14 PM, magma <maderri2 at gmail.com> wrote:
> I am building a family of functions, parametrized by 2 variables.
> These functions take one argument, so i have these definitions:
>
> f[1,3][y_]:=....
> f[1,2][y_]:=....
> f[1,3][y_]:=...
> f[2,1][y_]:=....
>
> etc. As you see it's a case of subvalues being defined.
>
> The problem is that I also require that the y argument should be held.
> That is the functions should have something like
>
> Attributes[f[m,n]]={HoldFirst};
>
> But this is not acceptable to Mathematica, since Attributes only takes
> symbols.
>
> Even using
>
> Attributes[f]={HoldAll};
>
> does not seem to work.
> So , how can i keep the argument y held?
>
Prev by Date:
**Re: How to delay action of ...[[i]] (Part[...,i]) until appropriate time?**
Next by Date:
**Re: Finite Groups...infinite disappoinment**
Previous by thread:
**Re: Holding arguments of a family of functions**
Next by thread:
**Re: Holding arguments of a family of functions**
| |