# How to make a pattern to change x^(2/y) to (x^2)^(1/y) for any x and y?

This might have been asked before. Please let me know if it is.

I was looking at an interesting Mathematica question in students forum and trying to solve it, and a chance to learn more about patterns.

Having little hard time writing a general pattern to convert an expression anything^(2/anything) to (anything^2)^(1/anything)

For example, given (-1/3)^(2/3) , convert this to ((-1/3)^2)^(1/3) and when given (-1/3)^(2/z) convert it to ((-1/3)^2)^(1/z) which then simplifies to (1/9)^(1/z)

The problem is that the FullForm changes depending if it is a symbol or a number in the exponent:

FullForm[(-1/3)^(2/6)] FullForm[(-1/3)^(2/z)] Will there be other general FullForm(s) to check for other than these two?

For Rational (numbers) this is how I do it now:

 (-1/3)^(2/3)/.
Power[Rational[-1,x_],Rational[2,y_]]:>Power[Power[Rational[-1,x],2],Rational[1,y]] You might have wondered why I did not write the more direct way:

(-3^(-1))^(2/3) /. (x_)^(2/y_) :> (x^2)^(1/y)


Well, because the above does not work. So, I cheated and looked at the FullForm and used that above in the pattern.

But the above fails when the denominator is a symbol, for example 2/z instead of numbers 2/3 I need little help figuring how how to check for each case. If I know it is a symbol, then Times[2, Power[z_, -1]] can be used. If it is not numeric, then Rational[2, z_] can be used.

question is: How to write a general pattern to convert x^(2/y) to (x^2)^(1/y) for any x and any y (excluding edge cases like Infinity, Indeterminate, {}, zero, and such. Just for numerics and symbols. This is practical problem.

Update

I was pointed out in the chat room to an old Mathematica package which was designed to handle such cases. But this package is no longer available. On the right below, is a screen shot of that old package from WRI page, the left is current V9 result The original question from a student at the WRI forum as asking for the behavior given by this old package (i.e. not the complex root, but show the real root).

• Perhaps a_^((2/b_) | Rational[2, b_]) :> (a^2)^b?. :> (Defer[a]^2)^Defer[b] to see the output – Rojo Dec 16 '12 at 7:36
• Do you just want an unevaluated expression? – Mike Honeychurch Dec 16 '12 at 7:41
• Note also that your title and 3rd sentence asks for x^(2/y) to (x^2)^y whereas the 4th sentence and final paragraph asks for x^(2/y) to (x^2)^(1/y). etc. etc. Can you clean this up by removing these contradictions. – Mike Honeychurch Dec 16 '12 at 7:46
• I mean unevaluated in the generic sense because you clearly want to halt a full evaluation and ensure that _^_ is the output pattern. In other words you don't want (1/9)^(1/3) to be evaluated any further do you? ... so some "holding" of the expression is required. – Mike Honeychurch Dec 16 '12 at 8:15

rule = x_^(2/y_ | Rational[2, y_]) :> HoldForm[(x^2)^(1/y)]; • @Nasser I'm glad if this answer helps, but I can't say I understand what you're trying to do. This transformation is only valid if the base (x) is non-negative, right, so of course there is a difference? Isn't your result the same as Abs? – Mr.Wizard Dec 16 '12 at 8:22