Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9523

Re: Bug in pattern test, or I did something wrong?

From A Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Bug in pattern test, or I did something wrong?
Date 2012-04-09 09:34 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jluafr$rqd$1@smc.vnet.net> (permalink)
References <201204050951.FAA13156@smc.vnet.net> <jlmejj$m99$1@smc.vnet.net>

Show all headers | View raw


Hi,

> Thank you all (especially Bob) for the kind suggestions! Bob's last
> post completely solves the problem. Here is a brief summary for
> people's suggestions and what works, in case for later reference:
>
> (1) Problem:
>
> f[a_ /; MemberQ[{0, 1, 2}, a]] := 0;
> f[x] /. f[expr_] :>  f[-expr]
> (* gets f[x] instead of f[-x] *)
>
> (2) I posted a workaround, but that's wrong (thank Fred to point it
> out). I should instead write
>
> g[a_?(MemberQ[{0,1,2},#]&)]:=0;
> g[x]/.g[expr_]:>g[-expr]
> (* still gets g[x] instead of g[-x] *)
>
> (3) The elegant and complete solution by Bob (as explained in his post):
>
> f[x] /. HoldPattern[f[expr_]] :>  f[-expr]
>
> (4) A workaround by Christoph:
>
> Do[h[i] = 0, {i, 0, 2}]
>
> (5) I also found  workaround:
>
> hasQ[list_,var_]:=Or@@Map[(var===# || Head[var]===#)&,list];
> And use hasQ instead of MemberQ

You are missing the solution which I think is probably the most simple. 
Use Alternatives:

ClearAll[f];

f[0|1|2]=0;

f[x] /. f[expr_] :>  f[-expr]

I used a version without giving the pattern a name, since there is not 
much sense doing so if you don't use that name anywhere. In case that 
example is just a simplification you can of course use something like, e.g.:

f[a:(0|1|2)]:=a^2

hth,

albert

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: Bug in pattern test, or I did something wrong? Yi Wang <tririverwangyi@gmail.com> - 2012-04-06 09:56 +0000
  Re: Bug in pattern test, or I did something wrong? A Retey <awnl@gmx-topmail.de> - 2012-04-09 09:34 +0000

csiph-web