4
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ Perhaps a_^((2/b_) | Rational[2, b_]) :> (a^2)^b?. :> (Defer[a]^2)^Defer[b] to see the output $\endgroup$
    – Rojo
    Dec 16, 2012 at 7:36
  • $\begingroup$ Do you just want an unevaluated expression? $\endgroup$ Dec 16, 2012 at 7:41
  • $\begingroup$ 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. $\endgroup$ Dec 16, 2012 at 7:46
  • $\begingroup$ 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. $\endgroup$ Dec 16, 2012 at 8:15

1 Answer 1

4
$\begingroup$

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

$\endgroup$
1
  • 2
    $\begingroup$ @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? $\endgroup$
    – Mr.Wizard
    Dec 16, 2012 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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