# How to get variables out of square roots and collect in order to simplify

f = 2 Sqrt[b^3 (1 + b) (1 - b + 2 a^2 b) c] + 8 Sqrt[b^5 (1 + b) (1 - b + 2 a^2 b) c] +
10 Sqrt[b^7 (1 + b) (1 - b + 2 a^2 b) c] - 10 Sqrt[b^11 (1 + b) (1 - b + 2 a^2 b) c] -
8 Sqrt[b^13 (1 + b) (1 - b + 2 a^2 b) c] - 2 Sqrt[b^15 (1 + b) (1 - b + 2 a^2 b) c]


FullSimplify[f, Assumptions -> {a > 1, b > 1, c > 0,
Element[a, Reals], Element[b, Reals], Element[c, Reals]},
ComplexityFunction -> LeafCount]


We obtain (just each coefficient is reduced by a factor 2):

(Sqrt[b^3 (1 + b) (1 - b + 2 a^2 b) c] + 4 Sqrt[b^5 (1 + b) (1 - b + 2 a^2 b) c] +
5 Sqrt[b^7 (1 + b) (1 - b + 2 a^2 b) c] - 5 Sqrt[b^11 (1 + b) (1 - b + 2 a^2 b) c] -
4 Sqrt[b^13 (1 + b) (1 - b + 2 a^2 b) c] - Sqrt[b^15 (1 + b) (1 - b + 2 a^2 b) c])


We can do this (we see the common part Sqrt[b (1 + b) (1 + (-1 + 2 a^2) b) c]):

FullSimplify[(2 b + 8 b^2 + 10 b^3 - 10 b^5 - 8 b^6 - 2 b^7) Sqrt[b (1 + b)
(1 - b + 2 a^2 b) c], {b > 1}]


and Mathematica out is:

-2 (-1 + b) b (1 + b)^5 Sqrt[b (1 + b) (1 + (-1 + 2 a^2) b) c]


But the problem is that this is a little part of a huge expression and doing this manualy may take weeks or more.

The optimal expresion would be:

-2 b (b - 1) (b + 1)^5 Sqrt[b c (b + 1) (1 + b (2 a^2 - 1))]


But I will be hilarious with the previous.

## Suplemental question:

As prescribed in the second response by bbgodfrey, we obtain our objective! Thanks you very much!

But permit me to go far away, if we put bbgodfrey's solution in a more complex expression like:

ff = (a (2 a (-2 + a^2) b^(3/2) - 2 a^3 b^(5/2) - 2 a (-2 + a^2) b^(7/2) + 2 a^3 b^(9/2) +
Sqrt[2] (-1 + a^2) b Sqrt[(-1 + b) (1 + b) (1 - b + 2 a^2 b) c] - 6 Sqrt[2](-1 + a^2) b^2
Sqrt[(-1 + b) (1 + b) (1 - b + 2 a^2 b) c] + Sqrt[2] (-1 + a^2) b^3 Sqrt[(-1 + b) (1 + b)
(1 - b + 2 a^2 b) c] + (4 a (Sqrt[b^3 (1 + b) (1 - b + 2 a^2 b) c] + 3 Sqrt[b^5
(1 + b) (1 - b + 2 a^2 b) c] + 2 Sqrt[b^7 (1 + b) (1 - b + 2 a^2 b) c] - 2 Sqrt[b^9
(1 + b) (1 - b + 2 a^2 b) c] - 3 Sqrt[b^11 (1 + b) (1 - b + 2 a^2 b) c] - Sqrt[b^13
(1 + b) (1 - b + 2 a^2 b) c]))/(1 + b)^3 + b (-2 a Sqrt[b] + 2 a (-4 + a^2) b^(3/2) -
2 a^3 b^(5/2) - 2 a (-4 + a^2) b^(7/2) + 2 (a + a^3) b^(9/2) + Sqrt[2] (-1 + a^2) b
Sqrt[(-1 + b) (1 + b) (1 - b + 2 a^2 b) c] - 6 Sqrt[2] (-1 + a^2) b^2 Sqrt[(-1 + b) (1 + b)
(1 - b + 2 a^2 b) c] + Sqrt[2] (-1 + a^2) b^3 Sqrt[(-1 + b) (1 + b) (1 - b + 2 a^2 b) c] +
4 a (Sqrt[b^3 (1 + b) (1 - b + 2 a^2 b) c] - Sqrt[b^7 (1 + b) (1 - b + 2 a^2 b) c]))))
/(4 (-1 + b) b^2 (1 + b) (2 a^3 Sqrt[b] + 2 a^3 b^(3/2) + Sqrt[2] (1 + a^2) Sqrt[(-1 + b)
(1 + b) (1 + (-1 + 2 a^2) b) c]))

FullSimplify[ff /. Sqrt[z1___ z3_^n_ z2___] -> z3^(n/2) Sqrt[z1 z2]]


Mathematica out is:

(a (2 a^3 (-1 + b)^2 Sqrt[b] (1 + b) - Sqrt[2] (1 + (-6 + b) b) Sqrt[(1 + (-1 + 2 a^2) b)
(-1 + b^2) c] + Sqrt[2] a^2 (1 + (-6 + b) b) Sqrt[(1 + (-1 + 2 a^2) b) (-1 + b^2) c] +
2 a Sqrt[b] (-1 + b^2) (3 + b - 2 Sqrt[(1 + b) (1 - b + 2 a^2 b) c])))/(4 (-1 + b) b
(2 a^3 Sqrt[b] (1 + b) + Sqrt[2] Sqrt[(1 + (-1 + 2 a^2) b) (-1 + b^2) c] + Sqrt[2]
a^2 Sqrt[(1 + (-1 + 2 a^2) b) (-1 + b^2) c]))


When we expect:

(a (2 a Sqrt[b] (3 + a^2 (-1 + b) + b) (-1 + b^2) + Sqrt[2] (-1 + a^2) (1 + (-6 + b) b)
Sqrt[(1 + (-1 + 2 a^2) b) (-1 + b^2) c] - 2 a (-1 + b^2) 2 Sqrt[b (1 + b) (1 + (-1 + 2 a^2)
b) c]))/(4 (-1 + b) b (2 a^3 Sqrt[b] (1 + b) + Sqrt[2] (1 + a^2) Sqrt[(1 + (-1 + 2 a^2) b)
(-1 + b^2) c]))


Why can we automatize Mathematica to collect and reduce the expression length?

We are very interested in adding rules to accomplish a full simplification. What is the estructure of this sentences? We would be very grateful if anybody could explain or give information (other questions, tutorials...) about this.

• Giving an assumption a>1 should mean you don't then need Element[a,Reals]. If you are sure your assumptions are valid then you might try f /. Sqrt[p_^n_* q_] /; OddQ[n] :> p^((n-1)/2)* Sqrt[p* q] to pull your odd powers outside your square roots. – Bill Dec 15 '15 at 20:22
• After you have extracted even powers of b and stored the result in say ff then you might try Simplify[ff //. p_* Sqrt[q_]+r_* Sqrt[q_]->(p+r)* Sqrt[q]] to collect together all your identical square roots into one. Use this cautiously. Check results carefully. – Bill Dec 15 '15 at 20:50
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Dec 16 '15 at 12:23
• @Nicolas Jumilla, do not spoil you question and nickname. You've made in intractable. Make a second question separate from the first, starting with the reference to the code. And do not forget to accept the bbgofreys answer if you like it. – garej Dec 16 '15 at 21:28
• Nicolas - please see: mathematica.stackexchange.com/help/merging-accounts – Verbeia Dec 16 '15 at 22:23

The following works for expressions of the form in the question but, of course, not for all expressions.

Simplify[f /. Sqrt[Times[z1___, b^n_, z2___]] -> b^((n - 1)/2) Sqrt[Times[z1, b, z2]]]
(* -2 (-1 + b) b (1 + b)^5 Sqrt[b (1 + b) (1 + (-1 + 2 a^2) b) c] *)


If the variety of expressions to the simplified is modest, more rules can be developed along these lines to accomplish a full simplification.

A simpler and more general solution is

Simplify[f /. Sqrt[z1___ z3_^n_ z2___] -> z3^(n/2) Sqrt[z1 z2]]
{* -2 (-1 + b) b^(3/2) (1 + b)^5 Sqrt[(1 + b) (1 + (-1 + 2 a^2) b) c] *}


The following largely but not entirely satisfies the goal of the addition to the question.

FullSimplify[ff /. b -> d^2 /. Sqrt[z1___ z3_^n_ z2___] -> z3^(n/2) Sqrt[z1 z2], d > 0];
FullSimplify[
Collect[Numerator[%136], Sqrt[c (1 + (-1 + 2 a^2) d^2) (-1 + d^4)], Simplify]/
Collect[Denominator[%136], Sqrt[c (1 + (-1 + 2 a^2) d^2) (-1 + d^4)], Simplify] /.
d -> Sqrt[b], b > 0]
(* (a (Sqrt[2] (-1 + a^2) (1 + (-6 + b) b) Sqrt[(1 + (-1 + 2 a^2) b) (-1 +  b^2) c] +
2 a Sqrt[b] (-1 + b^2) (3 + a^2 (-1 + b) + b -
2 Sqrt[(1 + b) (1 - b + 2 a^2 b) c])))/
(8 a^3 b^(3/2) (-1 + b^2) + 4 Sqrt[2] (1 + a^2) (-1 + b) b
Sqrt[(1 + (-1 + 2 a^2) b) (-1 + b^2) c]) *)


The denominator could be further simplified by using a ComplexityFunction that penalizes terms that the OP wishes to eliminate. However, at some point the approach becomes so specific to the problem that it is equivalent to performing the simplification by hand.

• @bbgofrey, +1 for general solution and virtual accept =)) – garej Dec 16 '15 at 21:30
• @bbgodfrey, why does Mathematica not collect in a correct way in Simplify? I always though that Mathematica does this because it considers that its output expression is simpler than our expectative. Is there any command or sentence that change Mathematica behavior? Is there a general solution that reduces the length of expressions significantly? – Nicolas Jumilla Dec 19 '15 at 1:16
• @NicolasJumilla Simplify attempts to reduce the value of a ComplexityFunction by applying TransformationFunctions. This fails, if that desired result does not correspond to the lowest value of the ComplexityFunction being used or if none of the available TransformationFunctions is able to transform the expression to the desired result. Also, sometimes the needed sequence of transformations first must increase the complexity before reducing it, and Simplify may not explore that sequence in sufficient depth to determine that eventually it leads to a less complex result. – bbgodfrey Dec 19 '15 at 11:29

Let us take

z = Sqrt[b (1 + b) (1 + (-1 + 2 a^2) b) c]; k = {a > 0, b > 0, c > 0};


So

z * FullSimplify[f/z, Assumptions -> k]


-2 (-1 + b) b (1 + b)^5 Sqrt[b (1 + b) (1 + (-1 + 2 a^2) b) c]

• Your solution is elegant in its simplicity. The assumption b > 0 is sufficient, however. – bbgodfrey Dec 15 '15 at 22:20
• @bbgodfrey, you are right. The only thing c = 0 Should be ruled out. Otherwise expression f/z will return Indeterminate`. I kept all of them because of OP's assumption. – garej Dec 15 '15 at 22:57
• @garej, it is a good idea. We can use in some punctual cases. – Nicolas Jumilla Dec 18 '15 at 11:56