Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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)]

Mathematica graphics

FullForm[(-1/3)^(2/z)]

Mathematica graphics

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]]

Mathematica graphics

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

Mathematica graphics

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

enter image description here

Here is the old package link http://reference.wolfram.com/legacy/v5_2/Add-onsLinks/StandardPackages/Miscellaneous/RealOnly.html (thanks for P.Fonseca for the link)

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).

share|improve this question
Thanks Mike. I tried it on the above example, but it does not seem to work for me: (-1/3)^(2/3) /. a_^(2/b_) :> (a^2)^b returned (-(1/3))^(2/3) please see: (on version 9) !Mathematica graphics – Nasser Dec 16 '12 at 7:28
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
Thanks Mike. corrected. these are typos ofcourse. I am tried from trying to find this pattern :) – Nasser Dec 16 '12 at 7:50
show 4 more comments

1 Answer

up vote 4 down vote accepted

I'm probably missing something, but maybe if you show me where this fails I can improve it:

rule = x_^(2/y_ | Rational[2, y_]) :> HoldForm[(x^2)^(1/y)];

{(-1/3)^(2/3), (-1/3)^(2/z)} /. rule

Mathematica graphics

share|improve this answer
+1 Mr Wizard. so far looking very good. Tried on symbols and numbers and it works. The reason I needed this is the following: Compare (-1/3)^(2/3) // N which gives -0.240375 + 0.416342 I but when the 2 is moved inside first, using your rule, the result becomes (-1/3)^(2/3) /. rule // N giving 0.48075 a big difference ! Squaring first, before talking the cubic root made the difference. Mathematica does not do this on it own. Thanks – Nasser Dec 16 '12 at 8:15
2  
@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
Yes ofcourse. But that is the point! Using this rule, now one can just use it regardless of the sign: (-1/3)^(2/3) /. rule // N will give the same answer now as (1/3)^(2/3) /. rule // N This is what one would do on paper. Square first. So the rule will be used regardless. No need to check. The student who asked this in the student forum, wanted the positive answer. So I was trying to help him. I figured how to do it for just numbers. But wanted to generalize for symbols also.... I see your point about doing it for the negative case, then do Abs[] at the end. Yes, that would work also. – Nasser Dec 16 '12 at 8:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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