Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm learning to use the ReplaceAll function and I found the behavior of which is quite confusing.


For

Sqrt[f[x, y]] /. f[___] -> u

Mathematica returns

Sqrt[u]

However, if I replace f with Plus, i.e.

Sqrt[Plus[x, y]] /. Plus[___] -> u

instead of returning Sqrt[u], Mathematica gives

u

For

a*Sqrt[x] /. Sqrt[_] -> u

Mathematica gives

a u

However, if the input is

a/Sqrt[x] /. Sqrt[_] -> u

Instead of returing a/u, Mathematica returns

a/Sqrt[x]

I'm really confused. Can anybody shed some light on the behavior of this function?

share|improve this question
add comment

1 Answer 1

up vote 14 down vote accepted

Case #1

Observe:

"anything" /. Plus[___] -> "match"
"match"

This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:

Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]

Case #2

You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the StandardForm output you normally see. Let's look at your second problematic expression:

a/Sqrt[x] // FullForm
Times[a,Power[x,Rational[-1,2]]]

As you can see, Sqrt is nowhere to be found.

You could prevent the evaluation of Sqrt on both the left-hand and right-hand sides:

Unevaluated[a/Sqrt[x]] /. HoldPattern[Sqrt[_]] -> u
a/u

If you need to match for things that appear as Radicals in StandardForm you can use the methods described here. An application of that might look like this:

ToBoxes[a/Sqrt[x]] /. _SqrtBox -> u // ToExpression
a/u
share|improve this answer
    
Great answer. As an mma beginner myself, I learnt quite a bit from it. It also made me realise that using Trace[] can be helpful in seeing how mma evaluates rules and matches patterns. I suggest OP might give it a whirl too. –  Aky Apr 9 '13 at 1:09
    
Thanks for the excellent answer. I find the 2nd solution on Case#2 very useful. –  user2256793 Apr 9 '13 at 2:59
    
@Aky Thanks. Indeed Trace is helpful, and I just recommended it in an answer a few hours before this one, which is probably the only reason I did not mention it here. –  Mr.Wizard Apr 9 '13 at 11:17
    
@sy0116 I'm glad the answer is helpful to you. Be aware that the ToBoxes method should only be used when necessary; if it is possible to accomplish your transformations using mathematical tools you should do so. Operating on the box forms is a "last resort" as it were to manipulate what Mathematica shows you in StandardForm. What Mathematica displays as a radical is somewhat arbitrary (or at least not readily apparent) and this is the only way to know to match exactly those cases. –  Mr.Wizard Apr 9 '13 at 11:22
add comment

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.