RE: Domain of a function
- To: mathgroup at smc.vnet.net
- Subject: [mg8343] RE: [mg8225] Domain of a function
- From: Ersek_Ted%PAX1A at mr.nawcad.navy.mil
- Date: Tue, 26 Aug 1997 02:22:36 -0400
- 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.
|
|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.
|
Lets make an example (a little more complicated than John's example):
In[1]:= f[1]=Sin[1];
f[2]=Sin[2];
f[q_Rational] := Sin[Numerator[q]];
f[n_?Negative] := Sin[-n];
(* Lets look at the FullForm of the first DownValue. *)
In[2]:= FullForm[ Part[ DownValues[f], 1 ]]
Out[2]// FullForm=
RuleDelayed[ HoldPattern[ f[1] ], Sin[1] ]
(* It took a little tinkering, but I found that the code below works: *)
(* The FullForm above helps explain why it works. *)
In[3]:= Domain[funct_]:=
DownValues[funct] /. RuleDelayed[x_,_] :> First[First[x]]
(* Now we try it out. *)
In[4]:= Domain[f]
Out[4]= {1, 2, q_Rational, n_?Negative}
(* I wanted to get rid of the Blanks and stuff. *)
(* Unfortunately I couldn't find an concise implementation. *)
In[5]:= Domain[funct_]:=Module[{temp,patts, pattest},
temp=DownValues[funct]/. RuleDelayed[x_,_]:>First[First[x]];
t0=Select[temp,(Head[#]=!=Pattern&&Head[#]=!=PatternTest)&];
t1=Select[temp, Head[#]==Pattern&];
t1=Map[Part[#,2,1]&,t1];
t2=Select[temp, Head[#]==PatternTest&];
t2=Map[Part[#,2]&, t2];
Join[t0,t1,t2]
]
In[6]:= Domain[f]
Out[6]= {1, 2, Rational, Negative}
(* Now that looks pretty good. *)
(* Maybe some of the experts will find a better implementation. *)
(*** Ted Ersek ***)