Re: Avoid iteration in pattern test?

*To*: mathgroup at smc.vnet.net*Subject*: [mg132173] Re: Avoid iteration in pattern test?*From*: Carl K Woll <carlw at wolfram.com>*Date*: Thu, 9 Jan 2014 03:51:25 -0500 (EST)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-outx@smc.vnet.net*Delivered-to*: mathgroup-newsendx@smc.vnet.net*References*: <20140106073034.2A31D69D3@smc.vnet.net>

You could try: x /: HoldPattern[Times][f__, x] /; ! FreeQ[Hold[f], d] := "matched" Carl Woll On 1/6/2014 1:30 AM, Yi Wang wrote: > Dear all, > > I wonder if one can stop pattern test from unnecessary iteration. For example > > ClearAll[x, a, b, c, d, f] > x /: f_ x := "matched" /; !FreeQ[f, d] > > Trace[a b c x] > > One can find that Mathematica first tests if a*b*c is free of d. Then it tests if a*b is free of d, if a*c is free of d, ... > > However, we know that if a*b*c is free of d, all the sub-sequences are free of d. Thus is it possible to rewrite the above rule, to stop Mathematica from wasting the efforts? > > Thanks a lot! > Yi > > PS: currently, I have a workaround -- to hold evolution of x when no match. However, it makes things complicated and less automatic. > > x /: f_ x := If[!FreeQ[f, d], "matched", f Hold[x]] >

**References**:**Avoid iteration in pattern test?***From:*Yi Wang <tririverwangyi@gmail.com>