Re: Bug in pattern test, or I did something wrong?
- To: mathgroup at smc.vnet.net
- Subject: [mg125911] Re: Bug in pattern test, or I did something wrong?
- From: Christoph Lhotka <christoph.lhotka at fundp.ac.be>
- Date: Fri, 6 Apr 2012 06:01:13 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201204050951.FAA13156@smc.vnet.net>
hi,
this is an interesting problem.
The reason why your first replacement rule does not work is hidden in
the evaluation process.
Define
In[1]:= f[a_ /; MemberQ[{0, 1, 2}, a]] := 0;
As you mention you get:
In[2]:= f[x] /. f[expr_] :> f[-expr]
Out[2]= f[x]
Use Trace to see the reason:
In[3]:= Trace[f[x] /. f[expr_] :> f[-expr]]
Out[3]:=
{{f[x],{MemberQ[{0,1,2},x],False},f[x]},{{f[expr_],{MemberQ[{0,1,2},expr_],True},0},0:>f[-expr],0:>f[-expr]},f[x]/.
0:>f[-expr],f[x]}
f[x] evaluates to f[x] while f[expr_] evaluates to 0 thus f[x] /. 0 :>
f[-expr] does not apply
and you get f[x].
So the problem is the following:
In[4]:= f[expr_]
Out[4]= 0
Which is due to MemberQ
In[5]:= MemberQ[{0, 1, 2}, expr_]
Out[5]= True
The evaluation chain is ok:
In[6]:= 0 /. f[expr_] :> f[-expr]
Out[6]= f[-expr]
By the way, in your definition of g
In[7]:= g[a_?MemberQ[{0, 1, 2}, #] &] := 0
the replacement works
In[8]:= g[x] /. g[expr_] :> g[-expr]
Out[8]= g[-x]
but not the definition:
In[9]:= g[0]
Out[9]= g[0]
If I understand your needs well a simple solution to your problem would be:
In[10]:= Do[h[i] = 0, {i, 0, 2}]
which gives:
In[11]:= {h[0], h[1], h[2], h[x]} /. h[expr_] :> h[-expr]
Out[11]:= {0, 0, 0, h[-x]}
- References:
- Bug in pattern test, or I did something wrong?
- From: Yi Wang <tririverwangyi@gmail.com>
- Bug in pattern test, or I did something wrong?