Re: Extracting the domain of a function, dynamic programming
- To: mathgroup at smc.vnet.net
- Subject: [mg8272] Re: Extracting the domain of a function, dynamic programming
- From: John M Lee <lee at math.washington.edu>
- Date: Sat, 23 Aug 1997 01:59:13 -0400
- Organization: University of Washington Department of Mathematics
- Sender: owner-wri-mathgroup at wolfram.com
John JOWETT wrote:
>
> Hello, I would like to extract the set of values on which a function is
> defined. Consider the following toy example (to illustrate a
> situation I have while using dynamic programming):
>
> In[182]:=
> Clear[f]
> In[185]:=
> f[1]:=f1;
> f[2]:=f2;
>
> To find out about f, I can always do
>
> In[186]:=
> ?f
> Global`f
> f[1] := f1
>
> f[2] := f2
>
> However in my application, the values f1 and f2 are enormous expressions
> that I have to avoid appearing in my notebook (even if I allow them to
> print, it is quite hard to sift through the output cells looking for
> what I want). In any case I would like to get hold of this domain
> list and do other things with it.
>
> I would like something
> that returns just the list {1,2}, ie, the domain or set of values for
> which the function has been explicitly defined.
>
> I have tried
>
> In[188]:=
> DownValues[f]
> Out[188]=
> {HoldPattern[f[1]]\[RuleDelayed]f1,HoldPattern[f[2]]\[RuleDelayed]f2}
>
> and then
>
> In[189]:=
> First /@ DownValues[f]
> Out[189]=
> {HoldPattern[f[1]],HoldPattern[f[2]]}
>
> Then I can use Length[] to find out at least for how many cases f
> is defined but I have trouble extracting my domain (basically because
> attempts to dig deeper into the DownValues list always evaluate f[1]
> and f[2] at some point.
>
> I am sure that someone who really understands held expressions
> can help me .....
>
> (I realise that I could simply accumulate an auxiliary list at
> each point where a new value is defined for f but that's not really
> satisfactory.)
>
> Thanks for any efforts,
> John Jowett
A slight modification of your idea works.
In[1]:= f[1] := f1;
In[2]:= f[2] := f2;
In[3]:= domain[function_] :=
Function[ x, x[[1,1,1]] ] /@ DownValues[function];
In[4]:= domain[f]
Out[4]= {1, 2}
Jack Lee
+----------------------------------------------------------------------+
| John M. Lee, Professor of Mathematics University of Washington |
| lee at math.washington.edu Mathematics Department |
| http://www.math.washington.edu/~lee Box 354350 |
| Phone 206-543-1735, Fax 206-543-0397 Seattle, WA 98195-4350 |
+----------------------------------------------------------------------+