# Mathematica doesn't want simplify the square root of the square even under assumptions

Suppose a simple function

f[x_,y_] = Sqrt[(x-y)^2]+Sqrt[(y-x)^2]


Knowing that $x>y>0$, without any commands like Simplify one can obtain (x-y) by using the Sqrt[(x-y)^2]-> (x-y) and Sqrt[(x-y)^2]-> (x-y):

f[s2, m2] /. {Sqrt[(x-y)^2] -> x-y,
Sqrt[(y-x)^2] -> x-y}


This might be used for the case of complicated functions involving many terms and other, when the Simplify command takes so much time. I tried to use it for one complicated function defined dynamically, but there was no result.

What is the reason and how to construct formal rule replacing the expressions like $\sqrt{(x-y)^2}$?

P.S. PowerExpand cripples the expression for the function...

• Perhaps you rejected PowerExpand because you used it without asumptions? For your example, PowerExpand[Sqrt[(x-y)^2] + Sqrt[(y-x)^2], Assumptions-> x>y] works well. – Carl Woll Aug 8 '17 at 17:20
• @CarlWoll : My complicated function after using the PowerExpand acquires some strange summands involving imarinary units and Args (even under assumptions) and expanding log[a/b] to log[a] - log[b], so I would like to avoid it. – John Taylor Aug 8 '17 at 17:22
• An example where PowerExpand is lacking would be useful – Carl Woll Aug 8 '17 at 17:27

You can target the Sqrt[..] expressions for simplification like this:
Sqrt[(x - y)^2] + Sqrt[(y - x)^2] /.

I've often done such targeted simplification, when the size of the main expression leads to a combinatorial explosion of rabbit trails for Simplify to follow. You can also use ExcludedForms to prevent Simplify from touching a subexpression, in case you want a somewhat complementary process (thanks to Carl Woll for pointing out this option). You can combine these in a sequence of steps to slowly get an expression down to a manageable size.
• Simplify has the ExcludedForms option that you can use to exclude patterns that shouldn't be simplified. – Carl Woll Aug 8 '17 at 17:31