# Remove square roots in an expression

I have an expression of the following form (yet way longer)

(u^2 - Sqrt[z1])^2 (u^3 - Sqrt[z1])^3 (u^4 - Sqrt[z1])^2
(u^2 + Sqrt[z1])^2 (u^3 + Sqrt[z1])^3 (u^4 + Sqrt[z1])^2
(-1 + u^18 z1^(5/2)) (1 + u^18 z1^(5/2))


As you can see, terms combine by pairs to produce

(u^4 - z1)^2 (u^6 - z1)^2 (u^8-z1)^2 (-1 + u^36 z1^5)


Does there exist a command in Mathematica to do this simplification?

The full expression is this one:

(1 + u^4)^3 (1 - u^3 + u^6)^2 (1 + u^3 + u^6)^2 (1 - u^4 + u^8)^3
(-1 + u^12)^7 (1 - u^6 + u^12)^2 (u^2 - Sqrt[z1])^2 (u^3 - Sqrt[z1])^3
(u^4 - Sqrt[z1])^2 (u^9 - Sqrt[z1])^2 (u^12 - Sqrt[z1])^3
(u^18 - Sqrt[z1])^2 (u^2 + Sqrt[z1])^2 (u^3 + Sqrt[z1])^3
(u^4 + Sqrt[z1])^2 (u^9 + Sqrt[z1])^2 (u^12 + Sqrt[z1])^3
(u^18 + Sqrt[z1])^2 (-1 + u^2 Sqrt[z1])^2 (1 + u^2 Sqrt[z1])^2
(-1 + u^3 Sqrt[z1])^3 (1 + u^3 Sqrt[z1])^3 (-1 + u^4 Sqrt[z1])^2
(1 + u^4 Sqrt[z1])^2 (-1 + u^9 Sqrt[z1])^2 (1 + u^9 Sqrt[z1])^2
(-1 + u^12 Sqrt[z1])^3 (1 + u^12 Sqrt[z1])^3 (-1 + u^18 Sqrt[z1])^2
(1 + u^18 Sqrt[z1])^2 (u^3 - z1)^2 (u^9 - z1)^2 (-1 + z1)^5 (1 + z1)^2
(u^3 + z1)^2 (u^6 + z1)^3 (u^9 + z1)^2 (u^12 + z1)^3 (u^18 + z1)^2
(1 - Sqrt[z1] + z1)^2 (1 + Sqrt[z1] + z1)^2 (u^4 - u^2 Sqrt[z1] + z1)^2
(u^4 + u^2 Sqrt[z1] + z1)^2 (u^8 - u^4 Sqrt[z1] + z1)^2
(u^8 + u^4 Sqrt[z1] + z1)^2 (u^12 - u^6 Sqrt[z1] + z1)^2
(u^12 + u^6 Sqrt[z1] + z1)^2 (-1 + u^3 z1)^2 (1 + u^3 z1)^2
(1 - u^2 Sqrt[z1] + u^4 z1)^2 (1 + u^2 Sqrt[z1] + u^4 z1)^2
(1 + u^6 z1)^3 (1 - u^4 Sqrt[z1] + u^8 z1)^2
(1 + u^4 Sqrt[z1] + u^8 z1)^2 (-1 + u^9 z1)^2 (1 + u^9 z1)^2
(1 + u^12 z1)^3 (1 - u^6 Sqrt[z1] + u^12 z1)^2 (1 + u^6 Sqrt[z1] + u^12 z1)^2
(1 + u^18 z1)^2 (u^6 + z1^2)^2 (u^12 + z1^2) (u^18 + z1^2)^2
(1 + u^6 z1^2)^2 (1 + u^12 z1^2) (1 + u^18 z1^2)^2 (u^18 - z1^(5/2))
(u^18 + z1^(5/2)) (-1 + u^18 z1^(5/2)) (1 + u^18 z1^(5/2))
(z1 + u^24 z1 - u^12 (1 + z1^2))^6


and FullSimplify does not modify it.

The simplest way is to expand the expression. If it's rational, the square roots will cancel. For an excessively large expression, a cleverer approach may be faster:

expr1 // Expand // Factor   (* OP's first expression *)
(*  (u^4 - z1)^2 (u^6 - z1)^3 (u^8 - z1)^2 (-1 + u^36 z1^5)  *)

expr2 // Expand // Factor   (* OP's second expression *)
(* ((-1+u)^7 (1+u)^7 (1+u^2)^7 (1-u+u^2)^7 (1+u+u^2)^7 (1+u^4)^3 (1-u^2+u^4)^7 (1-u^3+u^6)^2 (1+u^3+u^6)^2 (1-u^4+u^8)^3 (1-u^6+u^12)^2 (u^3-z1)^2 (u^4-z1)^2 (u^6-z1)^3 (u^8-z1)^2 (u^9-z1)^2 (u^12-z1)^6 (u^18-z1)^2 (u^24-z1)^3 (u^36-z1)^2 (-1+z1)^5 (1+z1)^2 (u^3+z1)^2 (u^6+z1)^3 (u^9+z1)^2 (u^12+z1)^3 (u^18+z1)^2 (-1+u^3 z1)^2 (1+u^3 z1)^2 (-1+u^4 z1)^2 (-1+u^6 z1)^3 (1+u^6 z1)^3 (-1+u^8 z1)^2 (-1+u^9 z1)^2 (1+u^9 z1)^2 (-1+u^12 z1)^6 (1+u^12 z1)^3 (-1+u^18 z1)^2 (1+u^18 z1)^2 (-1+u^24 z1)^3 (-1+u^36 z1)^2 (u^6+z1^2)^2 (u^12+z1^2) (u^18+z1^2)^2 (1+z1+z1^2)^2 (u^8+u^4 z1+z1^2)^2 (u^16+u^8 z1+z1^2)^2 (u^24+u^12 z1+z1^2)^2 (1+u^6 z1^2)^2 (1+u^4 z1+u^8 z1^2)^2 (1+u^12 z1^2) (1+u^8 z1+u^16 z1^2)^2 (1+u^18 z1^2)^2 (1+u^12 z1+u^24 z1^2)^2 (u^36-z1^5) (-1+u^36 z1^5)  *)


Update: Cleverer approach, perhaps (0.25 sec. vs. 25.6 sec.):

conjugateQ[e1_, e2_, v_] := NumberQ@Simplify[e1/(e2 /. v^p_ /; OddQ[2 p] :> -v^p)];
With[{conj = Gather[expr // FactorList,
#1[[2]] == #2[[2]] && conjugateQ[#1[[1]], #2[[1]], z1] &]
Times @@ Map[Expand[Times @@ #[[All, 1]]]^#[[1, 2]] &, conj]
]

• Indeed, I did not think about this simple way of doing things. The problem is that I will have to do similar calculations with much longer expressions (a few hundreds terms)... – Romain Gicquaud Apr 26 '17 at 12:19
• @RomainGicquaud See if gathering the conjugates together works. One could use the underlying approach to check rationality, too. FactorList might be slow on large, unfactored expressions, and the use of Gather here probably has quadratic complexity. But it seems likely to beat Expand. – Michael E2 Apr 26 '17 at 12:39
• Wow! Thank you very much!!! I just had to correct it slightly: With[{conj = Gather[Denominator[E0] // FactorList, #1[[2]] == #2[[2]] && conjugateQ[#1[[1]], #2[[1]], z1] &] }, Times @@ Map[Expand[Times @@ #[[All, 1]]]^#[[1, 2]] &, conj]] – Romain Gicquaud Apr 26 '17 at 13:31