Re: precedence for ReplaceAll?
- To: mathgroup at smc.vnet.net
- Subject: [mg110546] Re: precedence for ReplaceAll?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 25 Jun 2010 07:26:01 -0400 (EDT)
On 6/24/10 at 4:27 AM, mfripp at gmail.com (Matthias Fripp) wrote: >I am having trouble using ReplaceAll to replace symbols that already >have a delayed assignment. >e.g., this input: >In[287]:= >a := A c >b := B c >a /. {a -> x, b -> y} >b /. {a -> x, b -> y} >a + b /. {a -> x, b -> y} >a * b /. {a -> x, b -> y} >gives this output: >Out[289]= x >Out[290]= y >Out[291]= x + y >Out[292]= A B c^2 >All of this works as expected except for the final term. I would >have expected to get the result "x y". Is there any way to force >Mathematica to produce that result? In[10]:= a := A c b := B c In[12]:= a*b /. {A -> x/c, B -> y/c} Out[12]= x y The reason this needs to be different than the others is due to the way Mathematica's evaluator works. In everyone of your examples a,b are replaced by A c and B c when Mathematica encounters them as expected by using SetDelayed. After the replacement occurs, then evaluation of the specified operations on the left hand side of /. occurs. In each case except for the last, this leaves an expression with in terms of A c and B c. So, in those cases when a,b on the right hand side get evaluated, they match something on the left hand side and the replacement occurs as you want. But in the last case, the multiplication operation yields the result (in FullForm) of Times[A, B, Power[c,2]] Since nothing in this matches either Times[A, c] or Times[B, c], the replacement you are looking for does not occur. My work around does what you want since there is a match for A and B. >If on the other hand the original assignment is a := A + c and b := >B + c, I get an unexpected output for the sum, but the expected >output (x y) for the product. If I insert d instead of one of the >c's, I get various other (unpredictable) kinds of result. This fails for the same reason a * b /. {a -> x, b -> y} failed. That is, with these assignments, a + b will evaluate to (in FullForm) Plus[A, B, Times[2, c]] Again, there will be nothing to match either A+c or B+c and the replacement fails. >My first guess is that Mathematica is doing a sort of "double >ReplaceAll", where it first tries the pattern given in the delayed >assignment, and any symbols matched by that are not tested against >the explicit ReplaceAll. But that doesn't explain why the sum works >and not the product. Am I thinking about this the wrong way? Yes, your thinking here is incorrect. Note you can verify what occurs by using Trace. For example, In[18]:= a + b /. {a -> x, b -> y} // Trace Out[18]= {{{a,A+c},{b,B+c},(A+c)+(B+c),A+c+B+c,A+B+c+c,A+B+2 c},{{{a,A+c},A+c->x,A+c->x},{{b,B+c},B+c->y,B+c->y},{A+c->x,B+c->y}},A+B+2= c/.{A+c->x,B+c->y},A+B+2 c} Here you can see the evaluation of a+b to A+B+2c occurs before the replacement rule evaluates as I described above. The consequence is, there is no match for either A+c or B+c. A key point about replacement rules is these are implemented as literal replacements. There is no mathematic operation being by the replacement rules. Any mathematics is done on the left hand side before the replacement. Further mathematics may be done after the replacement depending on the nature of the replacement. But the replacement itself does not do mathematic operations.