Skip to main content
added 1373 characters in body
Source Link
RandomBits
  • 627
  • 4
  • 12

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.

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.

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.

Tweeted twitter.com/#!/StackMma/status/308344074451181568
Added some additional information.
Source Link
RandomBits
  • 627
  • 4
  • 12

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.

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

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.

Source Link
RandomBits
  • 627
  • 4
  • 12

How do I display an expression with negative powers?

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