# FullSimplify inconsistency

I'm pulling my hair out with this one. I have two complex functions f and g, ρ is real.

f = (-1)^(2/3) + 3/4 (1 - I Sqrt[3]) ρ -1/2 Sqrt[-(1 + (-1)^(1/3))^2 ρ (-4 + 3 ρ)];
g = (-1)^(2/3) + 3/4 (1 - I Sqrt[3]) ρ + 1/2 Sqrt[-(1 + (-1)^(1/3))^2 ρ (-4 + 3 ρ)];


I wish to simplify both, and specifically factor out the complex term out of the radical.

FullSimplify[ExpToTrig[f], 0 < ρ < 1]


1/4 (-2 + 2 I Sqrt[3] + (3 - 3 I Sqrt[3]) ρ + (-3 - I Sqrt[3]) Sqrt[(4 - 3 ρ) ρ])

FullSimplify[ExpToTrig[g], 0 <= ρ <= 1]


1/4 (-2 + 2 I Sqrt[3] + (3 - 3 I Sqrt[3]) ρ + Sqrt[(-6 - 6 I Sqrt[3]) ρ (-4 + 3 ρ)])

Mathematica does this with f but not with g! Then I try

f1 = -(1/2) Sqrt[-(1 + (-1)^(1/3))^2 ρ (-4 + 3 ρ)];
g1 = +(1/2) Sqrt[-(1 + (-1)^(1/3))^2 ρ (-4 + 3 ρ)];

FullSimplify[ExpToTrig[f1], 0 < ρ < 1]


-(1/4) I (-3 I + Sqrt[3]) Sqrt[(4 - 3 ρ) ρ]

FullSimplify[ExpToTrig[g1], 0 <= ρ <= 1]


1/4 (3 + I Sqrt[3]) Sqrt[(4 - 3 ρ) ρ]

How can I get Mathematica to consistently factor out the (3 + I Sqrt[3]) from the radical in both f and g?

Here's my ugly solution.

First I naively pull terms out of the radical when allowed, then simplify each seperately.

Q-function for when (x y)^n == x^n y^n:

ExpOverProdQ[x_?Positive, y_, n_] := True

ExpOverProdQ[x_, y_?Positive, n_] := True

ExpOverProdQ[x_, y_, n_] := IntegerQ[Refine[n Floor[(Pi-Arg[x]-Arg[y])/(2Pi)]]]


Code for pulling terms out of the radical:

simpRad[Power[t_Times, r_Rational]] :=
Module[{args, n, radSepList = {}, radKeepList = {}},

(* radSepList = terms we can pull out of the radical *)
(* radKeepList = terms we can't pull out of the radical *)

args = List @@ t;
n = Length[args];

Do[
With[{elm = args[[i]],
inrad = Times @@ Join[radKeepList, If[i != n, args[[i + 1 ;; n]], {}]]},

]

],
{i, 1, n}];

]


And finally

Assuming[0 <= ρ <= 1,
FullSimplify[ExpToTrig[g]] /. {p : Power[_Times, _Rational] :> simpRad[p]}
]

(* 1/4 (-2 + 2 I Sqrt[3] + (3 - 3 I Sqrt[3])ρ + (-3 I + Sqrt[3]) Sqrt[ρ]Sqrt[-4 + 3ρ]) *)


A side note: I tried injecting simpRad into FullSimplify using the option TransformationFunctions but had no luck.

Edit:

My solution based on the answer by Sjoerd C. de Vries:

comp[f_] := LeafCount[f]

comp[f_] /; ! FreeQ[f, Sqrt[g_] /; ! FreeQ[g, Power[_, 1/2]]] := 20 + LeafCount[f]


And now we get what we want.

FullSimplify[ExpToTrig[g], 0 <= ρ <= 1, ComplexityFunction -> comp]

(* 1/4 (-2 + 2 I Sqrt[3] + 3ρ - 3 I Sqrt[3]ρ + (3 + I Sqrt[3]) Sqrt[(4 - 3ρ)ρ]) *)


This has probably to do with the measure that FullSimplify uses to determine whether a structural change in an expression yields a simpler or a more complex expression. By default Mathematica uses LeafCount to do that.

You can check that the second result is actually simpler in this sense than the first:

LeafCount@FullSimplify[ExpToTrig[f], 0 < ρ < 1]


51

LeafCount@FullSimplify[ExpToTrig[g], 0 < ρ < 1]


50

By redefining g as:

g = (-1)^(2/3) + 3/4 (1 - I Sqrt[3]) ρ - a/2 Sqrt[-(1 + (-1)^(1/3))^2 ρ (-4 + 3 ρ)];


with the +1/2 renamed as -a/2, simplifying and then replacing a with -1 so that we get the old g again, we get:

FullSimplify[ExpToTrig[g], 0 < ρ < 1] /. a -> -1


1/4 (-2 + 2 I Sqrt[3] + (3 - 3 I Sqrt[3]) ρ - (-3 -I Sqrt[3]) Sqrt[(4 - 3 ρ) ρ])

i.e., the shape you wanted, but what is its complexity?

LeafCount@(FullSimplify[ExpToTrig[g], 0 < ρ < 1] /. a -> -1)


52

So that's even more complex. No wonder Mathematica doesn't automatically finish there.

• FullSimplify actually using SimplifyCount. reference.wolfram.com/mathematica/ref/ComplexityFunction.html Commented Dec 22, 2012 at 22:49
• @RiemannZeta You're right. Most often LeafCount gets you similar results but it indeed isn't the Automatic ComplexityFunction Commented Dec 22, 2012 at 23:04