Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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]], {}]]},

    If[ExpOverProdQ[elm, inrad, r], 
     AppendTo[radSepList, elm], AppendTo[radKeepList, elm]
    ]

   ],
   {i, 1, n}];

  Times @@ FullSimplify[Append[radSepList, Times @@ radKeepList]^r]
 ]

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ρ)ρ]) *)
share|improve this answer
add comment

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.

share|improve this answer
1  
FullSimplify actually using SimplifyCount. reference.wolfram.com/mathematica/ref/ComplexityFunction.html –  RiemannZeta Dec 22 '12 at 22:49
    
@RiemannZeta You're right. Most often LeafCount gets you similar results but it indeed isn't the Automatic ComplexityFunction –  Sjoerd C. de Vries Dec 22 '12 at 23:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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