# Simplify with Assumptions Sqrt[(expr)^2]

While trying to simplify expressions in the form Sqrt[(expr)^2] when expr>0 I noticed a peculiar behavior that was not resolved with code from this Q&A.

Simplify[Sqrt[(x - y + a b c)^2], x - y + a b c > 0]

Abs[a b c + x - y]


When I remove any of the a, b, c, x or y it returns the result without the Abs:

Simplify[Sqrt[(x - y + b c)^2], x - y + b c > 0]

b d + x - y


I loose faith when I meet such behavior with Mathematica :-(

Even though

Simplify[Sqrt[expr^2], expr > 0] /. expr -> (x - y + a b c)


does fix the first issue how could I simplify this:

Simplify[Sqrt[(x - y + a ^2 b^2 c^2)^2], x > y]


Anyway, can anybody explain why this happens and propose a way to fix it for general expressions?

• PowerExpand[Sqrt[(x - y + a b c)^2]] perhaps, with all of the caveats of PowerExpand. – chuy May 9 '14 at 13:56
• Simplify[Sqrt[expr^2], expr > 0] /. expr -> (x - y + a b c) is pretty cheap but works.. – Öskå May 9 '14 at 14:09
• Thanks @Öskå , what if I have something like : Simplify[Sqrt[expr^2], x > y] /. expr -> (x - y + a ^2 b^2 c^2) where the assumption is not the whole expression to be positive ? – tchronis May 9 '14 at 14:13
• @tchronis NP. Your example in the comments is quite different from the first one, hence my edit :) – Öskå May 9 '14 at 14:32

It would appear that you can increase the number of assumptions variables that Mathematica will handle by altering a system option:

SetSystemOptions["SimplificationOptions" -> {"AssumptionsMaxNonlinearVariables" -> 5}]

Simplify[Sqrt[(x - y + a b c)^2], x - y + a b c > 0]
(* a b c + x - y *)

Simplify[Sqrt[(x - y + a^2 b^2 c^2)^2], {x > y, {a, b, c} ∈ Reals}]
(* a^2 b^2 c^2 + x - y *)

• Very Nice! I will check if it applies in all of my cases. Thanks @SimonWoods – tchronis May 11 '14 at 19:38
• I'm sure I've seen this option while "exploring" but its use was not obvious. Can you give any other examples where it affects the result? – Mr.Wizard May 11 '14 at 19:38
• @Mr.Wizard, I have no experience with it - I just checked the system options to see if there was anything that might be responsible for the behaviour change between 4 and 5 variables. – Simon Woods May 11 '14 at 19:42
• I have experimented in much more complex expressions and when I increase AssumptionsMaxNonlinearVariables then memory consumption increases leading sometimes in kernel crashes. It would be interesting to write a routine to automatically determine the suitable number for each expression. That is adaptive Simplification ... – tchronis May 12 '14 at 21:22

It is since Mma does not know, what among x,y,a,b and c is positive and negative and, therefore, which their combination should be chosen, x-y+abcor y-x-abc. If you tell it, say, like this:

    Simplify[Sqrt[(x - y + a b c)^2], {x - y + a b c > 0, x > 0, a > 0,
b > 0, c > 0, y < 0}]


it will return the expected result:

(*   a b c + x - y  *)


Later edit. To address your question below. No, it is not right. Let us see: The inequality x - y + a b c > 0has a lot of solutions depending on the signs of all these parameters:

 Reduce[x - y + a b c > 0]

(*   (a | b | y) \[Element]
Reals && ((x <=
y && ((c <
0 && ((b < 0 && a > (-x + y)/(b c)) || (b > 0 &&
a < (-x + y)/(b c)))) || (c >
0 && ((b < 0 && a < (-x + y)/(b c)) || (b > 0 &&
a > (-x + y)/(b c)))))) || (x >
y && ((c <
0 && ((b < 0 && a > (-x + y)/(b c)) ||
b == 0 || (b > 0 && a < (-x + y)/(b c)))) ||
c == 0 || (c >
0 && ((b < 0 && a < (-x + y)/(b c)) ||
b == 0 || (b > 0 && a > (-x + y)/(b c)))))))   *)

• Although nobody said that x or any other variable was negative or positive. Only the overall expression is positive. – Öskå May 9 '14 at 14:02
• But one constraint is enough for that right? Normally I have much more complex expressions to simplify and I cannot afford having more constraints than required. So this is a fix only for this and not the general case... – tchronis May 9 '14 at 14:08
• @ Öskå then not only Mma but you and me are unable to decide, how to write this expression without Àbs. Anyway, I only explain why Mma behaves this way. – Alexei Boulbitch May 9 '14 at 14:09
• If (x - y + a b c) > 0 then you and me and Mathematica know that Sqrt[(x - y + a b c)^2] = x - y + a b c`. That's the OP's point I guess. – Öskå May 9 '14 at 14:11
• @tchronis Please find my answer in the edit to the first one. – Alexei Boulbitch May 9 '14 at 14:14

Mathematically speaking, you are trying to specify a branch of the square-root function by an irrelevant assumption. This can lead to totally bogus results. Somewhat simplifying: Just because 9>0 does not mean that -3 is not also a square-root of 9. Yeah, yeah some people think that Sqrt[explicit_positive_real] must mean the positive root. But it doesn't generalize, say to cube roots (etc). Choosing a branch cut is independent of the sign of the radicand, mathematically. Math works that way.

• In some optimization problems you may have constraints that set some quantities positive . So the variables that maximize the objective function may be expressed without Abs in a more presentable way. Also in that way simplifications reduce a lot the resulted formulas. – tchronis May 9 '14 at 18:28
• In that case you are talking about a different function which might be describable as a particular branch of the square root "function". Mathematica has the Root[] function which makes it almost possible to talk correctly about the issue. Certainly physical problems provide insight into which branch of a square-root is the useful one. A glance through a physics text should reveal that the "sign" of the radicand is not the key as to which branch has a correct "physical" properties. It is possible to write programs like PowerExpand. Sometimes produces nonsense. – Richard Fateman May 12 '14 at 4:47