2
$\begingroup$

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...

$\endgroup$
  • 2
    $\begingroup$ 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. $\endgroup$ – Carl Woll Aug 8 '17 at 17:20
  • $\begingroup$ @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. $\endgroup$ – John Taylor Aug 8 '17 at 17:22
  • $\begingroup$ An example where PowerExpand is lacking would be useful $\endgroup$ – Carl Woll Aug 8 '17 at 17:27
4
$\begingroup$

You can target the Sqrt[..] expressions for simplification like this:

Sqrt[(x - y)^2] + Sqrt[(y - x)^2] /. 
 rt : (Sqrt[_] | 1/Sqrt[_]) :> Simplify[rt, x > y]
(*  2 x - 2 y  *)

Perhaps that is what you're after.

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.

$\endgroup$
  • 2
    $\begingroup$ Simplify has the ExcludedForms option that you can use to exclude patterns that shouldn't be simplified. $\endgroup$ – Carl Woll Aug 8 '17 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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