4
$\begingroup$

By defining:

rule = Sqrt[expr_^2] :> expr;

and writing:

Sqrt[x^2] /. rule

I get:

x

which is what is desired (regardless of formal correctness).

On the other hand, writing:

Sqrt[x^2 y] /. rule

I get:

Sqrt[x^2 y]

instead of:

x Sqrt[y]

How can I generalize the above rule so that it also applies in the second case?

$\endgroup$
  • $\begingroup$ I'm not sure how you would generalize, but Sqrt/@(x^2 y)/.rule gives the desired output. $\endgroup$ – user1066 Feb 17 at 13:35
  • $\begingroup$ @user1066: Thank you! Unfortunately, without $y$, you don't get what you want. $\endgroup$ – TeM Feb 17 at 13:46
8
$\begingroup$
rule = Sqrt[a_. * expr_^n_?EvenQ] :> expr^(n/2)*Sqrt[a];

expr = {Sqrt[x^2], Sqrt[x^2 * y], Sqrt[x^2*y^2], Sqrt[x^2*y*z], 
   Sqrt[x^4*y^2 *z]};

Using ReplaceAll (/.)

expr /. rule

{x, x Sqrt[y], x Sqrt[y^2], x Sqrt[y z], x^2 Sqrt[y^2 z]}

However, in general ReplaceRepeated (//.) is needed

expr //. rule

(* {x, x Sqrt[y], x y, x Sqrt[y z], x^2 y Sqrt[z]} *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ Perfect, that's exactly what I wanted! $\endgroup$ – TeM Feb 17 at 14:04
5
$\begingroup$

Why not use PowerExpand:

PowerExpand[Sqrt[x^2 y]]

x Sqrt[y]

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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