# Join all square root expressions?

Sometimes I observe Mathematica produce expressions exactly like the following after a simplification step:

$$\frac{a+\sqrt{-(1-b)}\sqrt{\frac{1}{b-1}}}{\sqrt{c}\sqrt{\frac{1}{c}}}$$

Now, any further generic Simplify or FullSimplify would not do any good any more. One is tempted to use PowerExpand, but this would just introduce an annoying i term in the numerator, instead of simplifying correctly. Therefore, what I have been doing is typing in simplifications like:

/.Sqrt[x_]Sqrt[y_]/(Sqrt[z_]Sqrt[m_])->Sqrt[x y]/Sqrt[z m]//Simplify


by hand. This is very annoying. Maybe there is a Mathematica function which joins all the square roots in all possible places of an expression together?

EDIT Example code to be simplified:

Sqrt[-(1 - b)] Sqrt[1/(b - 1)]


Generally, any simplification involving explicitly specifying properties of terms involved does not help really, since the amount of actions is the same as manually correcting the expression. What I am looking for is a function which merges all possible square roots together, regardless of content.

EDIT2

Please note the following triviality: a search for a function performing the merging of all square roots in an expression only makes sense if an appropriate branch for the square root functions is chosen so that $\sqrt{x}\sqrt{y}=\sqrt{x y}$ is actually true for the expressions in question. Thank you.

-
Could you post Mathematica syntax ? –  b.gatessucks May 2 '13 at 12:19
FullSimplify[ Sqrt[a] Sqrt[b]/(Sqrt[c] Sqrt[d]), {a > 0, b > 0, c > 0, d > 0}] –  chuy May 2 '13 at 13:14
chuy, the aim is to have such a function for arbitrary expressions under the square roots. –  Kagaratsch May 2 '13 at 13:20
Consider Sqrt[-2] Sqrt[-2] != Sqrt[4] and Sqrt[-3] Sqrt[-1/3] != 1. –  chuy May 2 '13 at 13:21
Holw about expr //. Sqrt[x_] Sqrt[y_] -> Sqrt[x y]. –  chyaong May 9 '13 at 8:13

You have to let Mathematica know that the arguments inside the square roots are positive and real. So you can add the assumptions that a and c are positive and that b>1 (so that b-1 is positive).

expr = (a + 3 w Sqrt[-(1 - b)] Sqrt[1/(b - 1)])/(r Sqrt[c] Sqrt[1/c]);
FullSimplify[expr, {a > 0, c > 0, b > 1}]


(a + 3 w)/r

-
The same simplification is correct also for b<1. –  Kagaratsch May 2 '13 at 13:30
No it doesn't. As wikipedia says: "Because of the discontinuous nature of the square root function in the complex plane, the law √zw = √z√w is in general not true. (Equivalently, the problem occurs because of the freedom in the choice of branch. The chosen branch may or may not yield the equality; in fact, the choice of branch for the square root need not contain the value of √z√w at all, leading to the equality's failure." en.wikipedia.org/wiki/Square_root –  bill s May 2 '13 at 14:52
Good point. In further discussion, please, choose the appropriate branch for the equality to indeed hold. Thank you. –  Kagaratsch May 9 '13 at 7:20
As far as I know, there is not a "canonical" way to do this that holds in all cases. You can see what can happen in J.M.'s answer: when you always allow the simplification, you can get contradictions. In other words, your goal of " a function which merges all possible square roots together, regardless of content" may lead to contradictions, depending on the content. –  bill s May 9 '13 at 8:35
Ok, fair enough. Then like this: consider the choice of branch to be dynamic and adaptive to any situation such that the equality holds. Exclude cases in which such a branch cannot be found. Btw: the only problem with J.M.'s version, is that it lets spectator factors vanish which do not belong to the square roots being simplified. –  Kagaratsch May 9 '13 at 8:53

My attempt, using a combination of replacement rules, Factor and PowerExpand

simple = PowerExpand[Factor //@ #] //.
{1/Sqrt[x_] :> Sqrt[1/x], Sqrt[x_] Sqrt[y_] :> Sqrt[x y]} &;


Testing on various expressions found among the comments:

exprs = {Sqrt[a]/Sqrt[b],
Sqrt[-(1 - b)] Sqrt[1/(b - 1)],
(a + 3 w Sqrt[-(1 - b)] Sqrt[1/(b - 1)])/(r Sqrt[c] Sqrt[1/c]),
Sqrt[1/(1 - s^2)] Sqrt[s^2 - s^4],
Sqrt[-1 + (1 + 1/b^2) Cosh[s]^2] Sqrt[1/(b^2 - (1 + b^2) Cosh[s]^2)],
Sqrt[-1 + (1 + 1/b^2) Cosh[s]^2] Sqrt[1/(b^2 - (1 + b^2) Cosh[s]^2)] Sech[s]^2};

simple /@ exprs

{Sqrt[a/b], 1, (a + 3 w)/r, -s, I/b, (I Sech[s]^2)/b}

-

expr = (a + 3 w Sqrt[-(1 - b)] Sqrt[1/(b - 1)])/(r Sqrt[c] Sqrt[1/c]);

Simplify[expr, Positive[Cases[expr, Power[stuff_, 1/2 | -1/2] :> stuff, ∞]]]
(a + 3 w)/r

-
Good idea, but now this function gives for expr=Sqrt[1/(1 - s^2)] Sqrt[s^2 - s^4] the unhandy result Abs[s]. Is there a way to merge square roots but to retain their structure? –  Kagaratsch May 2 '13 at 12:29
That'd take a bit more work... –  Guess who it is. May 2 '13 at 12:31
Just added /. Abs[xx_] -> Sqrt[xx^2] to your expression. :] Thank you very much! –  Kagaratsch May 2 '13 at 12:37
Oops! Something going wrong here: Sqrt[-1 + (1 + 1/b^2) Cosh[s]^2] Sqrt[1/(b^2 - (1 + b^2) Cosh[s]^2)] complains about contradictory assumtions and gives I/b. But more importantly Sqrt[-1 + (1 + 1/b^2) Cosh[s]^2] Sqrt[1/(b^2 - (1 + b^2) Cosh[s]^2)] Sech[s]^2 also complains but gives the same I/b! So Sech[s]^2 vanishes from the expression... –  Kagaratsch May 2 '13 at 12:44

I know this thread is old, but the missing of the PowerContract function as a build-in is a bit annoying, because such simplification tasks occur quite often.

From an old book (Quantum Methods with Matematica / Springer 1994 / James M. Feagin) I have the following function:

PowerContract[expr_] := expr //.
{ m_^q_ n_^q_ :> (m n)^q /; !IntegerQ[m] && !IntegerQ[n],
m_^q_ n_^p_ :> (m/n)^q /; q >= 0 && p == -q &&
!IntegerQ[m] && !IntegerQ[n]}


For example gives

Sqrt[-(1 - b)] Sqrt[1/(b - 1)] // PowerContract


as result simply 1.

-
Simplify[Sqrt[-(1 - b)] Sqrt[1/(b - 1)], Element[a | b, Integers] && a > 0 && b > 0] == 1 –  eldo Jun 18 '14 at 16:35