Re: Extracting a Function's Domain and Image
- To: mathgroup at smc.vnet.net
- Subject: [mg67991] Re: [mg67970] Extracting a Function's Domain and Image
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Thu, 20 Jul 2006 06:04:35 -0400 (EDT)
- References: <200607190921.FAA21378@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 19 Jul 2006, at 11:21, Bruce Colletti wrote:
> Re Mathematica 5.2.2.
>
> Why does the last line of code return {x} and not {a,b,_}, while if
> the f[_]=0 is struck, the answer is {a,b}?
>
> I want to extract the domain and image of a user-defined function,
> and tech support recommended using DownValues (pretty handy). If
> you have an easier approach, please share it. Thanks.
>
> Bruce
>
> Remove@f;
> f[_]=0;
> f["a"]=1;
> f["b"]=2;
> Clear@x;
> h=DownValues@f
> Cases[h,f[x_]->x,Infinity]
>
> Out[52]=
> {HoldPattern[f[a]]:>1,HoldPattern[f[
> b]]:>2,HoldPattern[f[_]]:>0}
>
> Out[53]=
> {x}
>
Because what happens when you evaluate
Cases[h,f[x_]->x,Infinity]
is completely different from what you obviously intended. First of
all, because of your definition of f, f[x_] is evaluated to 0 and then
Cases[h,0->x,Infinity]
finds one 0 in the DownValues of f and performs the requested
replacement by x.
You can avoid this simply by using
Cases[h,HoldPattern[f[x_]]->x,Infinity]
{a,b,_}
Andrzej Kozlowski
Karakida,Tokyo,Japan
- References:
- Extracting a Function's Domain and Image
- From: Bruce Colletti <vze269bv@verizon.net>
- Extracting a Function's Domain and Image