11
$\begingroup$

How do I display an expression with negative powers? Mathematica seems to always invert a term with a negative rational power. None of the following work:

Power[4,-3]
HoldForm[Power[4,-3]]
Unevaluated[Power[4,-3]]

The problem I am trying to solve is pretty simple. I have a function that takes an expression as an argument and returns two TextCells as a result. One text cell restates the expression and the other shows the simplified version of the expression.

I currently call the function like this:

f[HoldForm[ x^2 + 3x + 5 == 0 ]]

Ideally, it will display the original expression with minimal formatting (it is nice for Abs[x] to be replaced with |x| for example, but I'd like to leave negative powers intact).

Edit:

I just wanted to add, that I have a list of expressions to which I apply the expression to produce an "exam" document and an "answer key" document. That is why I need some control over the formatting of the expression. To the extent that a technique can be embedded in the function, that is probably preferable to having to change all of the expressions.

Edit 2:

I'd like to thank everyone for their suggestions. As Xerxes points out in his comments, Mathematica does not differentiate between the following two inputs:

x^-1 // FullForm
(* Power[x,-1] *)

1/x // FullForm
(* Power[x,-1] *)

Given this, I think the only way to achieve my formatting goal is to differentiate the input forms. Here is what I came up with:

xPower /: MakeBoxes[xPower[x_, e_ /; e < 0], form_] := 
    SuperscriptBox[MakeBoxes[x, form], MakeBoxes[e, form]] 
xPower /: MakeBoxes[xPower[x_, e_ /; e < 0], form_] := 
    SuperscriptBox[MakeBoxes[x, form], MakeBoxes[e, form]]
xTimes := Times
xPower := Power

Now, for my test case of HoldForm[4^-3 \[Times] 1/2^-4], which Mathematica will by default rearrange to 1/4^3/2^4, I can write:

HoldForm[xTimes[xPower[4,-3],xPower[2,-4]]]
(* 4^-3 \[CenterDot] 1/2^-4 *)

and,

xTimes[xPower[4,-3],xPower[2,-4]]
(* 1/4 *)

Using this paradigm, I need to use the alternate functions xTimes and xPower whenever I need to maintain the strict expression formatting. This seems like the minimum deviation necessary on the input side to achieve the desired result.

Does this make sense? Or, I am in for some unexpected behavior down the road? Did I miss an easier approach? Or, something more Mathematica-ish.

$\endgroup$
2
  • $\begingroup$ I think you forgot your definition xTimes /: MakeBoxes[xTimes[x_, y_], form_] := RowBox[{MakeBoxes[x, form], "\[CenterDot]", MakeBoxes[y, form]}] in the above $\endgroup$ Commented Apr 7, 2013 at 12:30
  • $\begingroup$ In response to your second edit, I like the way you have done this. Possibly you can still use a function like doubleShow (below) to do default formatting and use functions like xTimes and xPower when you want to make an exception. For example you might make it so that doubleShow[xDivide[c,d]]-> c/d (with the c above the d, like a FractionBox), doubleShow[c/d]-> c/d (like how it looks here, i.e. with a RowBox) and doubleShow[c xPower[d,-1]]-> c [CenterDot] d^-1 (with a RowBox and a SuperscriptBox). But really it depends on your preferences. $\endgroup$ Commented Apr 7, 2013 at 13:04

4 Answers 4

13
$\begingroup$

Use a combination of Hold and special Forms to do this:

MakeBoxes[MyForm[expr_], form_] := MakeBoxes[expr, form]
MakeBoxes[MyForm[Power[x_, p_ /; p < 0]], form_] := 
 SuperscriptBox[MakeBoxes[x, form], MakeBoxes[p, form]]

Attributes[doubleShow] = {HoldFirst};
doubleShow[expr_] := 
 Module[{}, Print[MyForm //@ HoldForm[expr]]; Simplify[expr]]

Resulting in:

doubleShow[4^-1 + 2^-2 + 2^1 + x^-2]
(* 4^-1+2^-2+2^1+x^-2 *)
(* 5/2 + 1/x^2 *)
$\endgroup$
7
  • $\begingroup$ I see you already implemented what I alluded to in my answer. +1 $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2013 at 21:50
  • $\begingroup$ This looks appealing, but it will take me a little time to understand it and implement it. $\endgroup$
    – RandomBits
    Commented Mar 3, 2013 at 22:07
  • $\begingroup$ I got it to work for the given example, but now it seems to convert everything that it can with a negative power. 1/2 comes out as 2^-1. $\endgroup$
    – RandomBits
    Commented Mar 4, 2013 at 0:30
  • $\begingroup$ I see what you mean, but this is a bigger problem with the way Mathematica treats input. When you type 1/2, Mathematica sees Times[1,Power[2,-1]]. There's nothing like a Divide produced, as you might think, so there's no way to discriminate between the two inputs you gave. $\endgroup$
    – Xerxes
    Commented Mar 4, 2013 at 0:56
  • 1
    $\begingroup$ @RolfMertig I think that conversion happens later. Consider FullForm[{1/2,Hold[1/2]}]: (* List[Rational[1, 2], Hold[Times[1, Power[2, -1]]]] *) $\endgroup$
    – Xerxes
    Commented Mar 4, 2013 at 16:47
8
$\begingroup$

The simplest is probably just using Superscript:

Superscript[4, -3]

Mathematica graphics

You can use Format or MakeBoxes, etc., for automatic formatting.

Another example:

myFormat =
  TraditionalForm[# /. Power[expr_, r_?Negative] :> Superscript[expr, r]] &;

Abs[x]^-3 // myFormat

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ Is there a way to get the Superscript[4,-3] to evaluate as Power[4,-3]? For one use, I need to the unadulterated expression, but for the other I need to evaluate it. Edit: Sorry, I see what you mean now. Replace any elements that I don't want to change with an inert to evaluation element that displays in the appropriate manner. $\endgroup$
    – RandomBits
    Commented Mar 3, 2013 at 22:02
  • $\begingroup$ This works for simple expressions, but when I apply this rule to 1/2^-4, Mathematica produces (1/2^4)^-1. $\endgroup$
    – RandomBits
    Commented Mar 3, 2013 at 22:12
  • $\begingroup$ @RandomBits Point #1: Yes, you can use Interpretation for that, or MakeBoxes. #2: I meant my "example" to be just that, not a complete solution. I'll take another look at this later, but I think Xerxes may have already solved this. (I didn't test it extensively.) $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2013 at 22:30
  • $\begingroup$ @Mr.Wizard, probably you want to wrap within a bracket? A case where this is weird, 1/(1-x (1-y)) //myFormat $\endgroup$
    – BabaYaga
    Commented Sep 2, 2020 at 17:03
4
$\begingroup$
Power[4, HoldForm[-3]]

displays as Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Any reason to prefer this over Superscript? $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2013 at 21:51
  • $\begingroup$ Simple and effective -- this is probably useful in many contexts. The drawback for my application is that I have to very, very carefully write my expressions for this to work. One of my examples is 4^(-3) x 1/(2^(-4)). Putting the hold on the -3 keeps the first term from changing, but putting the hold on the -4 doesn't stop Mathematica from producing 2^(-(-4)). Adding a hold to the fraction fixes that, but I end up with four HoldForms for the simple expression. $\endgroup$
    – RandomBits
    Commented Mar 3, 2013 at 22:01
2
$\begingroup$

Building on Rolf Mertig's answer, it would seem useful to embody his idea in a function:

Attributes[pwrForm] = {HoldAll};
pwrForm[expr_] := Unevaluated[expr] /. Power[x_, y_] :> Power[x, HoldForm[y]]

Then it is possible to write expressions like these:

4^-1 + 2^-2 + 2^1 + x^-2 // pwrForm

enter image description here

and

Abs[x]^-3 // pwrForm // TraditionalForm

enter image description here

$\endgroup$
1
  • $\begingroup$ Applying this to my enhanced test case of 4^-3 1/2^-4 yields $4^{-3-2 (-1)}$. I am not sure how that result is produced, but it is not what I expected. The default result from Mathematica for HoldForm[4^-3 1/2^-4] is $\frac{1}{\frac{4^3}{2^4}}$. The desired result is $4^{-3} \cdot \frac{1}{2^{-4}}$. $\endgroup$
    – RandomBits
    Commented Mar 4, 2013 at 16:28

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.