Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am a newbie in the field so please not angry. I am reading the tutorial below and having a problem. Link: How to Create and Use Rules

From the link:
If you give two rules for the same variable, the Wolfram Language will use only the first rule:

In[4]:= 2 x + y /. {x -> 3, x -> 4}
Out[4]= 6 + y

Could you explain how Mathematica do the substitution here? Is this the order of implementation:

  1. Substitute x-> 3 to the expression
  2. Substitute x-> 4 to the expression gotten from No. 1
share|improve this question
1  
As far as I understand it, once a subexpression gets replaced, it can't get replaced again. Try this: a*b /. {a -> a, a -> 1, b -> c}. The first replacement just replaces a with a, but since that expression has already been changed, it is ignored after that, so the Rule a - > 1 is only applied to other parts of the expression, and there are no more a's, but b gets replaced with c. So you're kind of right and kind of not. – march Jan 29 at 18:51
1  
Use a List of rule lists. In[265]:= 2 x + y /. {{x -> 3}, {x -> 4}} Out[265]= {6 + y, 8 + y} – Daniel Lichtblau Jan 29 at 18:56
    
@march is correct that ReplaceAll (/.) doesn't apply several rules to the same part of a given expression. For that case you want to use ReplaceRepeated (//.). In your case however this won't chance the output since x -> 3 changes x to be 3 so there is no more x for the second rule to match against – Sascha Jan 29 at 18:56
    
Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Louis Jan 29 at 18:58
1  
I can't help but feel that this is a dupe (although it's a great question). Does anyone have a link? I'll go check the Pitfalls thread. If it's not there, we might consider adding a post about how exactly ReplaceAll works. – march Jan 29 at 19:01
up vote 11 down vote accepted

As far as I understand it, once a subexpression gets replaced, it can't get replaced again. Try this:

a*b /. {a -> a, a -> 1, b -> c}

resulting in

a*c

The first replacement just replaces a with a, but since that expression has already been changed, it is ignored after that. So the Rule a -> 1 is only applied to other parts of the expression (in this case the b), and those other parts don't depend on a, so nothing changes. Finally, b gets replaced with c. (By the way, in somewhat-advanced Mathematica programming, this fact can be taken advantage of in many clever ways. Here is an example by Mr. Wizard, provided in the comments.)

So you're kind of right and kind of not. The Rules are applied sequentially, but the rules are not applied to the new expressions resulting from previous replacements.

Here's another interesting example:

a b c /. {a -> b, b -> c, c -> d}
% /. {a -> b, b -> c, c -> d}
% /. {a -> b, b -> c, c -> d}
(* b c d *)
(* c d^2 *)
(* d^3 *)

In the first case, the a instance is replaced by b, but it is not in turn replaced by c. However, the original b is replaced by c. And so on. To do all of them at once, use ReplaceRepeated:

a b c //. {a -> b, b -> c, c -> d}
(* d^3 *)

(Be careful of this one, because it can run into infinite recursions.) For completeness, note that ReplaceRepeated does not act the same as a sequence of ReplaceAlls if the list of Rules are different:

a b c //. {a -> b, c -> d, b -> c}
a b c /. {a -> b} /. {c -> d} /. {b -> c}
(* d^3 *)
(* c^2 d*)

Finally, as noted by Daniel Lichtblau in a comment, if you do want to apply the two rules to get different expressions, do this:

2 x + y /. {{x -> 3}, {x -> 4}}
(* {6 + y, 8 + y} *)

Alternatively, do

2 x + y /. # &/@ {x -> 3, x -> 4}

or

2 x + y /. List/@{x -> 3, x -> 4}
share|improve this answer
    
Well, thanks a lot for the thorough answer! I've got it now. However, I think I need to read and try these more. – BlueSky Jan 29 at 19:08
    
I often use the fact that a given expression will not be replaced twice, e.g. (25538) – Mr.Wizard Jan 29 at 22:54
    
Yeah, that is what I used to remember after reading the answer above! – BlueSky Jan 30 at 15:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.