4
$\begingroup$

I've noticed the following in the Mathematica documentation for ReplaceAll:

Apply the first matching rule:
In[1]:= x /. {x -> 1, x -> 3, x -> 7}
Out[1]= 1

Apply each rule separately:
In[2]:= x /. {{x -> 1}, {x -> 3}, {x -> 7}}
Out[2]= {1, 3, 7}

What is the purpose of the first use case? It's just ignoring some of the input. Is there a situation where this comes in handy? Why wasn't Mathematica made to simply output {1, 3 ,7} for that one, and {{1}, {3}, {7}} for the second?

A step by step explanation of what's happening here could also be helpful?

$\endgroup$
1
  • $\begingroup$ Roughly speaking, it is the difference between applying one set of rules vs. three separate sets of rules. The first of those should not act like the second. $\endgroup$ Feb 7, 2022 at 22:32

2 Answers 2

11
$\begingroup$

This example should be illuminating:

In[3]:= {x, y, z, t} /. {x -> 1, y -> 3, z -> 7}
Out[3]= {1, 3, 7, t}

In[4]:= {x, y, z, t} /. {{x -> 1}, {y -> 3}, {z -> 7}}
Out[4]= {{1, y, z, t}, {x, 3, z, t}, {x, y, 7, t}}
$\endgroup$
0
7
$\begingroup$

If you study Solve[] and its output, a single solution is a list of rules, one for each variable:

{x -> 1, y -> 3, ...}

Consequently a solution set is a list of solutions:

{    
 {x -> 1, y -> 4, ...}
 {x -> 2, y -> 3, ...},
 ...
}

Now if I substitute one solution into an expression, I would expect to get one answer:

2 x + y^2 /. {x -> 1, y -> 4}
(*  18  *)

If I substitute a solution set into an expression, you get the value of each expression at each solution, which I might or might not expect but seems reasonable in hindsight:

2 x + y^2 /. {{x -> 1, y -> 4}, {x -> 2, y -> 3}}
(*  {18, 13}    *)

In this view {x -> 1, x -> 2,...} is meaningless as a solution, because it is not possible to replace x by 1 and 2 simultaneously. One would expect one of the following to happen when it is applied: only the first rule would be applied, only the last rule, or you get an error without any replacement. I have forgotten if I read in the documentation somewhere about which of these happens or not -- I can't find it now -- but try it and you find only the first rule is applied when multiple rules match, even for something like

x /. {_ -> 4, x -> 1} (* _ matches everything *)
(*  4  *)
$\endgroup$
6
  • $\begingroup$ Thanks @Michael E2, I accepted Szabolcs's answer because it was posted 1st, and was more to the point. I do appreciate the extra information though. $\endgroup$
    – Cat Bisque
    Feb 8, 2022 at 2:31
  • $\begingroup$ My example was pulled straight from the Mathematica documentation, including the specification that the first matching rule is the one that's applied. $\endgroup$
    – Cat Bisque
    Feb 8, 2022 at 2:34
  • $\begingroup$ @AllisonB You're welcome. We used to recommend that folks wait a day before accepting, because it gives everyone around the world a chance to answer. After that, the OP can select the answer that best helps them. But people got tired of doing that, myself included. Also, if the question is marked "answered," then you might lose out if someone else who knows a really good solution skips the answered question. $\endgroup$
    – Michael E2
    Feb 8, 2022 at 2:34
  • $\begingroup$ @AllisonB Oh, which documentation page? $\endgroup$
    – Michael E2
    Feb 8, 2022 at 2:35
  • $\begingroup$ reference.wolfram.com/language/ref/ReplaceAll.html $\endgroup$
    – Cat Bisque
    Feb 8, 2022 at 2:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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