10
$\begingroup$

Hi folks I have a simple Simplify[] question

the following expression simplifies nice:

Sqrt[4 (a + b) - 4 c] // Simplify
2 Sqrt[a + b - c]

the next one refuses

Sqrt[4 (a + b) - 4 (c + d)] // Simplify
Sqrt[4 (a + b) - 4 (c + d)]

it is possible to force a simplification like this:

Sqrt[4 (a + b) - 4 (c + d)] /. Sqrt[4 a_ - 4 b_] -> Sqrt[4(a - b)]
2 Sqrt[a + b - c - d]

Is there no "built in" way to get the simplification ?

Ok the Question got an easy answer :)

Not surprisingly my need is more complicated, in reality I have the following expression

(-2 D Cos[\[Phi]] - 2 E Sin[\[Phi]] + Sqrt[ 4 (D Cos[\[Phi]] + E Sin[\[Phi]])^2 - 4 F (A Cos[\[Phi]]^2 + C Sin[\[Phi]]^2 + B Sin[2 \[Phi]])])/(2 (A Cos[\[Phi]]^2 + C Sin[\[Phi]]^2 + B Sin[2 \[Phi]]))

enter image description here

which can substantially be simplified by use of

# /. Sqrt[4 a_ - 4 b_] -> Sqrt[4 ( a - b)] & // FullSimplify

to:

-(F/(D Cos[\[Phi]] + E Sin[\[Phi]] + Sqrt[(D Cos[\[Phi]] + E Sin[\[Phi]])^2 -F (A Cos[\[Phi]]^2 + C Sin[\[Phi]]^2 + B Sin[2 \[Phi]])]))

enter image description here

but this was more like a lucky punch ... Any straight way to do this?

$\endgroup$
10
$\begingroup$

You can have a look at ComplexityFunction option of Simplify

Simplify[
    Sqrt[4 (a+b)-4 (c+d)],
    ComplexityFunction -> (StringLength[ToString[InputForm[#]]]&)
    ]

2 Sqrt[a + b - c - d]

$\endgroup$
9
$\begingroup$
Sqrt[4 (a + b) - 4 (c + d)] // Apart

enter image description here

$\endgroup$
  • $\begingroup$ ok that was easy. $\endgroup$ – Robert Nowak Jun 27 '17 at 12:43
7
$\begingroup$

As luck would have it, the following works:

expr //  FullSimplify[#, ComplexityFunction -> LeafCount]&

Mathematica graphics

Sqrt[4 (a + b) - 4 (c + d)] // FullSimplify[#, ComplexityFunction -> LeafCount]&

Mathematica graphics

$\endgroup$
3
$\begingroup$

Thanks to Alexei I figured out that the following does the job by pushing the Root from the Numerator in to the Denominator. But one still has to know the "pretty" result in advance to do the guess / lucky punch.

# /. (a_ + Sqrt[b_])/c_ ->
Simplify[(a + Sqrt[b]) (a - Sqrt[b])]/(c (a - Sqrt[b]))& // Simplify
$\endgroup$
  • 1
    $\begingroup$ FWIW, I'd probably code the rationalization like this: ratXF = Simplify[# /. (a__ + (c_: 1) Sqrt[b_]) :> ((+a)^2 - c^2 b)/(a - c*Sqrt[b])] &. In this form, it is a standard algebraic trick or "guess." You could also use it in Simplify[expr, TransformationFunctions -> {Automatic, ratXF}] in case you wanted other simplifications to be done at the same time. $\endgroup$ – Michael E2 Jun 27 '17 at 17:47
3
$\begingroup$

Let us take your expression:

 expr1 = (-2 D Cos[\[Phi]] - 2 e Sin[\[Phi]] + Sqrt[
  4 (D Cos[\[Phi]] + e Sin[\[Phi]])^2 - 
   4 F (A Cos[\[Phi]]^2 + c Sin[\[Phi]]^2 + B Sin[2 \[Phi]])])/(
 2 (A Cos[\[Phi]]^2 + c Sin[\[Phi]]^2 + B Sin[2 \[Phi]]))

Here I changed C->c and E->e, since these Latin letters are reserved in Mma. If I understand you right, we need to factorize the expression 4 (D Cos[\[Phi]] + e Sin[\[Phi]])^2 - 4 F (A Cos[\[Phi]]^2 + c Sin[\[Phi]]^2 + B Sin[2 \[Phi]]). Let us have a look at the tree form of your expression

TreeForm[expr1]

yielding

enter image description here

From here we see that the necessary term has the tree coordinate {3,3,1}. Let us now factor it:

expr2 = MapAt[Factor, expr1, {3, 3, 1}]//Cancel



 (*  (-D Cos[\[Phi]] - e Sin[\[Phi]] + Sqrt[
 D^2 Cos[\[Phi]]^2 - A F Cos[\[Phi]]^2 + 
  2 D e Cos[\[Phi]] Sin[\[Phi]] + e^2 Sin[\[Phi]]^2 - 
  c F Sin[\[Phi]]^2 - B F Sin[2 \[Phi]]])/(
A Cos[\[Phi]]^2 + c Sin[\[Phi]]^2 + B Sin[2 \[Phi]])  *)

Now we can additionally simplyfy the expression under the radical:

expr3=MapAt[Simplify, expr2, {2,3,1}]

    (* (-D Cos[\[Phi]] - 
 e Sin[\[Phi]] + Sqrt[(D^2 - A F) Cos[\[Phi]]^2 + (e^2 - 
     c F) Sin[\[Phi]]^2 + (D e - B F) Sin[2 \[Phi]]])/(
A Cos[\[Phi]]^2 + c Sin[\[Phi]]^2 + B Sin[2 \[Phi]]) *)

You may still further transform it by standardly multiplying the numerator and denominator by the term "conjugated" to the numerator:

 expr4 = (expr3[[2]]*(expr3[[2]] - 2 Last[expr3[[2]]]) // Simplify)/(
  expr3[[1, 1]] (expr3[[2]] - 2 Last[expr3[[2]]])) // Simplify

enter image description here

It should be noted that kglr obtains the same result in a shorter way.

Have fun!

$\endgroup$
  • $\begingroup$ Hi Alex, nice try, but If you look at my lucky punch solution you will notice that it is substantially simpler, the complete Numerator consisting only of one singel "F" $\endgroup$ – Robert Nowak Jun 27 '17 at 13:34
  • $\begingroup$ @Robert Nowak Look, there is a simple solution offered by kglr, which I like very much. However, I just give a version of the answer to your original question: how to move 4 out of the radical. The further operating is quite evident: multiplying by the conjugated term, that we know from the school time. Have a look at my edit. $\endgroup$ – Alexei Boulbitch Jun 27 '17 at 14:02
  • $\begingroup$ Hi Alex, good to be reminded on the old school tricks :), however your Numerator: 2F is wrong, it should be 1F $\endgroup$ – Robert Nowak Jun 27 '17 at 14:23
  • $\begingroup$ @Robert Nowak You are right. I fixed it $\endgroup$ – Alexei Boulbitch Jun 27 '17 at 14:44
2
$\begingroup$
Sqrt[4 (a + b) - 4 (c + d)] // Factor

2 Sqrt[a + b - c - d]

$\endgroup$
2
$\begingroup$

I guess I would look for a general approach that might factor out numeric common factors of square-roots wherever they occur in simplifying expressions and not just globally treat the OP's expressions:

e1 = Sqrt[4 (a + b) - 4 (c + d)];
e2 = (-2 d Cos[ϕ] - 2 e Sin[ϕ] + 
      Sqrt[4 (d Cos[ϕ] + e Sin[ϕ])^2 - 4 F (A Cos[ϕ]^2 + c Sin[ϕ]^2 + B Sin[2 ϕ])]) /
       (2 (A Cos[ϕ]^2 + c Sin[ϕ]^2 + B Sin[2 ϕ]));

The problem is that the desired intermediate form is not "simpler" by the automatic measure

# -> Simplify`SimplifyCount[#] &@ e1
# -> Simplify`SimplifyCount[#] &@ Factor[e1])
(*
  Sqrt[4 (a + b) - 4 (c + d)] -> 16
  2 Sqrt[a + b - c - d] -> 17 
*)

So it will be impossible to transform e1 with Simplify[] without modifying ComplexityFunction.

Another issue arises in e2, and it is important because it seems to imply that e2 cannot be simplified only using a ComplexityFunction. Many of the standard transformations such as Factor expand the factors. It turns out that the expression under the radical in e2 becomes more complicated when transformed by Factor, even after the factor is taken out of the radical and cancelled. Further, Simplify[] is unable to transform the expanded expression back to its simpler form.

So it appears we need to extend TransformationFunctions, too. In particular, the transformation has to remove a given common factor from each term in a sum without modifying the terms further.

A third issue is that if number is a numeric common factor of expr, then Sqrt[expr/number] evaluates to Sqrt[expr]/Sqrt[number], unless number has been factored out of expr. This means you can simply do something like the following, because the two Sqrt[2] will cancel:

Sqrt[2] Sqrt[(2 x + 4 y) / 2]
(*  Sqrt[2 x + 4 y]  *)

The workaround is to simplify expr/number inside the Sqrt[] using the transformation in the preceding point. In particular, dividing through by number prevents Sqrt[expr/number] automatically separating to Sqrt[expr]/Sqrt[number]:

Sqrt[2 x/2 + 4 y/2]
(*  Sqrt[x + 2 y]  *)

Finally, a general observation about simplifying Sqrt. When Sqrt[x] (or rather something like √x) appears in the typeset output, it represents the StandardForm of Power[x, 1/2] or Power[x, -1/2]. Sometimes you have to consider both forms (although not in the two examples in the OP).

Solution. We implement the transformation indicated above by factoring out the numeric common factor given by FactorList. The result is cached (memoized) for two reasons: Principally for speed, since Simplify might call the transformation several times on the same expression as it tries to minimize complexity; in particular, and secondly, we reuse the square root transformations in the complexity function.

clearSqrtCache[] := (
   ClearAll[sqrtCache];
   mem : sqrtCache[Power[e_Plus, 1/2]] := mem =       (* case Sqrt[A + B +...] *)
     With[{numfactor = Power @@ First@ FactorList[e]},
      Sqrt[numfactor] *
       Sqrt[#/numfactor & /@ e]                       (* divide each term *)
      ];
   mem : sqrtCache[Power[e_, 1/2]] := mem =           (* all other cases *)
     With[{numfactor = Power @@ First@ FactorList[e]},
      Sqrt[numfactor] *
       Replace[Expand[e],
        {ee_Plus :> Sqrt[#/numfactor & /@ ee],        (* if Expand[e] converts to case 1 *)
         ee_ :> Sqrt@ Simplify[e/numfactor]}]         (* default; use e or ee *)
      ];
   sqrtCache[Power[e_, -1/2]] := 1/sqrtCache[Power[e, 1/2]]; (* convert to Sqrt[] *)
   sqrtCache[e_] := e;                                (* non-Sqrt[]: no transformation *)
   );
clearSqrtCache[];
sqrtXF = Replace[#, e : Power[_, 1/2 | -1/2] :> sqrtCache[e]] &;
sqrtCF = 2 Count[#, r : Power[_, 1/2 | -1/2] /; sqrtCache[r] =!= r, {0, Infinity}] &;

Examples:

Simplify[e1, TransformationFunctions -> {Automatic, sqrtXF}, 
 ComplexityFunction -> (Simplify`SimplifyCount[#] + sqrtCF[#] &)]
Simplify[e2, TransformationFunctions -> {Automatic, sqrtXF}, 
 ComplexityFunction -> (Simplify`SimplifyCount[#] + sqrtCF[#] &)]
(*
  2 Sqrt[a + b - c - d]
  (-d Cos[ϕ] - e Sin[ϕ] +
     Sqrt[(d Cos[ϕ] + e Sin[ϕ])^2 - F (A Cos[ϕ]^2 + c Sin[ϕ]^2 + B Sin[2 ϕ])]) /
       (A Cos[ϕ]^2 + c Sin[ϕ]^2 + B Sin[2 ϕ])
*)
$\endgroup$

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.