MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Behavior of ReplaceAll with Computed Results from a Conditional Test

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66771] Re: [mg66745] Behavior of ReplaceAll with Computed Results from a Conditional Test
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Mon, 29 May 2006 06:06:43 -0400 (EDT)
  • References: <200605281008.GAA10238@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Mr. Park,
That testing function is a rather clever construction. I imagine
they'll have to update the documentation now (provided someone emails
them or they notice it on the list). I suppose the workaround is to
refactor your code. I am going to guess that you wanted to increase
processing speed by combining pattern matching with some
preprocessing; if that's the case, a workaround will depend on the
type of preprocessing you want to do. Otherwise, you might be able to
get away with simply enumerating all the cases:

vars=Sequence[a,b,c,d]
varpats=Sequence@@Map[#_&,{vars}]
rules=MapIndexed[Times[f[varpats],#1]->ReplacePart[f[vars],#1^2,#2]&,{vars}]

Regards,

On 5/28/06, David Park <djmp at earthlink.net> wrote:
> Dear MathGroup,
>
> Here is a routine that mimics a more complicated routine I was working on. I think it has strange behavior. The routine uses a conditional test on the pattern variables that as a side effect computes a parameter used in the replacement. When used on a list this doesn't work because Mathematica does all of the tests first, leaving only the parameter from the last test, and then does all of the replacements. Not what I want.
>
> The sample routine looks at expressions of the form f[a,b]c. If c ===a it returns f[a^2,b]. If c === b it returns f[a,b^2] and otherwise no replacement is done. I've added two Print statements to clarify what is happening.
>
> transformf[expr_] :=
>   Module[{testQ, replacea},
>
>     testQ[a_, b_, c_] :=
>       Module[{},
>         Print["Testing ", f[a, b]c];
>         Which[
>           c === a, replacea = True; True,
>           c === b, replacea = False; True,
>           True, False]];
>
>     expr /. (f[a_, b_]c_) /; testQ[a, b, c] :>
>         Module[{},
>           Print["Processing " , f[a, b]c];
>           If[replacea, f[a^2, b], f[a, b^2]]]
>     ]
>
> If I Map this onto the following list it works as expected.
>
> transformf /@ {f[a, b]a, f[a, b]b}
> Testing a f[a, b]
> Processing a f[a, b]
> Testing b f[a, b]
> Processing b f[a, b]
> {f[a^2, b], f[a, b^2]}
>
> However, if I use the routine on the entire list I obtain:
>
> {f[a, b]a, f[a, b]b} // transformf
> Testing a f[a, b]
> Testing b f[a, b]
> Processing a f[a, b]
> Processing b f[a, b]
> {f[a, b^2], f[a, b^2]}
>
> which I regard as incorrect. The Help for ReplaceAll says...
>
> "ReplaceAll looks at each part of expr, tries all the rules on it, and then goes on to the next part of expr."
>
> That seems to me to be a vague and inaccurate statement. One might think that if the rule matched then the replacement would be done - or at least calculated. Then ReplaceAll would go on to look at the next part of the expression. Instead all the tests are done first, and all the matching replacements are done afterwards. So in the above routine the replacea parameter is set to the last test result and then used in all of the replacements.
>
> That hardly seems fair. Is there a way around this behavior.
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
> Everyone should study mathematics - just so they will know what it is to be wrong.
>
>
>
>
>
>
>
>
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Extending Plus very slow
  • Next by Date: RE: Behavior of ReplaceAll with Computed Results from a Conditional Test
  • Previous by thread: Behavior of ReplaceAll with Computed Results from a Conditional Test
  • Next by thread: Re: Behavior of ReplaceAll with Computed Results from a Conditional Test