Re: Extracting a Function's Domain and Image
- To: mathgroup at smc.vnet.net
- Subject: [mg68003] Re: [mg67970] Extracting a Function's Domain and Image
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Thu, 20 Jul 2006 06:04:45 -0400 (EDT)
- References: <200607190921.FAA21378@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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}
The problem with Cases[h,f[x_]->x,Infinity] is that f[x] is being
evaluated before the rule is used. So, you are actually evaluating
Cases[h, 0->x, Infinity]
You need to prevent the f from evaluating. The reason things worked as
expected when f[_]=0 was not included is because f[x_] didn't turn into
0 in that case, so preventing evaluation of f was not necessary.
There are many possible ways of preventing f from evaluating. Here are two:
Cases[h, HoldPattern[f][x_]->x, Infinity]
Block[{f}, Cases[h, f[x_]->x, Infinity]
Carl Woll
Wolfram Research
- References:
- Extracting a Function's Domain and Image
- From: Bruce Colletti <vze269bv@verizon.net>
- Extracting a Function's Domain and Image