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
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).
a_^((2/b_) | Rational[2, b_]) :> (a^2)^b
?.:> (Defer[a]^2)^Defer[b]
to see the output $\endgroup$_^_
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. $\endgroup$