From: Szabolcs Horvát on 25 Jun 2010 07:29 [note: message sent to comp.soft-sys.math.mathematica as well] On 2010.06.24. 10:27, 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? > > 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. > > 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? > I get the impression from your message that you believed that when evaluating a /. {a -> x, b -> y}, 'a' gets replaced by 'x'. This is not the case. First the 'a' on the lhs of /. is evaluated to A c, then the 'a' on the rhs is evaluated to A c (and 'b' is evaluated too, of course). At this point the evaluator has the expression A c /. { A c -> x, B c -> y }, and replaces A c by x. This is all easy to see by using the Trace function: In[41]:= Trace[a/.a->x] Out[41]= {{a,A c},{{a,A c},A c->x,A c->x},A c/.A c->x,x} What happens with the last expression is this: In[42]:= Trace[a b/.{a->x,b->y}] Out[42]= {{{a,A c},{b,B c},(A c) (B c),A c B c,A B c c,A B c^2},{{{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 c^2/.{A c->x,B c->y},A B c^2} I.e. the ReplaceAll expression evaluates to A B c^2 /. {A c->x,B c->y}, and no replacement can be done, as A c or B c do not appear *formally* in the expression A B c^2. What you are looking for is achieved by Unevaluated[a b] /. {HoldPattern[a] -> x, HoldPattern[b] -> y} Hope this helps, Szabolcs
From: AES on 26 Jun 2010 03:12 In article <i023lj$r3c$1(a)smc.vnet.net>, Albert Retey <awnl(a)gmx-topmail.de> wrote: > > No, what actually happens is that a and b are evaluated to A*c and B*c > respectively, even before the ReplaceAll sees them. Then the ReplaceAll > sees > > A*c+B*c /. {A*c -> x, B*c -> y} > A*B*c^2 /. {A*c -> x, B*c -> y} > > where it will find matches in the first case but not in the second and > so does nothing. I'm guessing that in the initial evaluation stage(s) referred to above, any and all "superfluous" parens are also stripped out, so that even if the OP had started with a := (A c) b := (B c) (and maybe inserted protecive parens at various places in subsequent line, the results would have been exactly the same . . . ? Would there be a place in Mathematica for a "FindAndReplaceText[-]" command that would do the equivalent of a word processor's "Find and Replace" operation ***on the literal text string that was typed into a Mathematica cell*** (or that appeared on screen in an Output cell), without doing any evaluation at all, before or afterwards? Thinking that this is what ReplaceAll does (or should do) is obviously the source of a lot of misinterpretation and trouble for Mathematica users. But maybe defining such a command, and determining what it should and shouldn't do would be too difficult or too complicated to be realistically possible.
From: AES on 26 Jun 2010 03:12 In article <i023mj$r4u$1(a)smc.vnet.net>, Bill Rowe <readnews(a)sbcglobal.net> wrote: > >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. Just be to be argumentative here: The OP's thinking obviously doesn't agree with what Mathematica actually does. But what Mathematica does is not the only standard of correctness, or even reasonableness, in the world. His thinking is not unreasonable, and not even incorrect by some reasonable standards.
From: Bill Rowe on 27 Jun 2010 04:57 On 6/26/10 at 3:12 AM, siegman(a)stanford.edu (AES) wrote: >In article <i023mj$r4u$1(a)smc.vnet.net>, >Bill Rowe <readnews(a)sbcglobal.net> wrote: > >>>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. >Just be to be argumentative here: The OP's thinking obviously >doesn't agree with what Mathematica actually does. In terms of using Mathematica, this is the only aspect that matters. >But what Mathematica does is not the only standard of correctness, or >even reasonableness, in the world. His thinking is not unreasonable, >and not even incorrect by some reasonable standards. Sure, it might be reasonable to think the way the OP indicated. But to do so will clearly get in the way of using Mathematica efficiently. It is understanding what Mathematica actually does that is important here.
From: Bill Rowe on 27 Jun 2010 04:57 On 6/26/10 at 3:12 AM, siegman(a)stanford.edu (AES) wrote: >In article <i023lj$r3c$1(a)smc.vnet.net>, >Albert Retey <awnl(a)gmx-topmail.de> wrote: >>No, what actually happens is that a and b are evaluated to A*c and >>B*c respectively, even before the ReplaceAll sees them. Then the >>ReplaceAll sees >>A*c+B*c /. {A*c -> x, B*c -> y} A*B*c^2 /. {A*c -> x, B*c -> y} >>where it will find matches in the first case but not in the second >>and so does nothing. >I'm guessing that in the initial evaluation stage(s) referred to >above, any and all "superfluous" parens are also stripped out, so >that even if the OP had started with >a := (A c) >b := (B c) >(and maybe inserted protecive parens at various places in subsequent >line, the results would have been exactly the same . . . ? Yes, the result would be the same. Try it and see. If this were not the case, there would be a serious bug. >Would there be a place in Mathematica for a "FindAndReplaceText[-]" >command that would do the equivalent of a word processor's "Find and >Replace" operation ***on the literal text string that was typed into >a Mathematica cell*** (or that appeared on screen in an Output >cell), without doing any evaluation at all, before or afterwards? There is a FindAndReplaceText already in Mathematica. It is called StringRreplace and operates on string (text) data. But keep in mind, most things you input into Mathematica are not strings, not represented as literal text when you enter them. >Thinking that this is what ReplaceAll does (or should do) is >obviously the source of a lot of misinterpretation and trouble for >Mathematica users. But maybe defining such a command, and >determining what it should and shouldn't do would be too difficult >or too complicated to be realistically possible. ReplaceAll does do what you have described. That is replaces all instances of a target with something else as specified. The difficulty new users have with ReplaceAll is that it operates on the form of the expression Mathematica retains internally which is often not what a user expects based on what is displayed.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Export 3D graphics to Web page (a la LiveGraphics3D) Next: ContourStyle Question |