Group Abstract Group Abstract

Message Boards Message Boards

How to output negative exponents always as fractions

GROUPS:
I try to output expressions with negative exponents as fractions. I already tried
Notation[ParsedBoxWrapper[
FractionBox["1",
SuperscriptBox["a_", "b_"]]] \[DoubleLongLeftArrow] ParsedBoxWrapper[
RowBox[{"Power", "[",
RowBox[{"a_", ",",
RowBox[{"-", "b_"}]}], "]"}]]]
This only seems to work for simple negative exponents, but not for negative exponents which include Products or Sums.
So, is there a way to do this ?
POSTED BY: Mark Butler
Answer
2 months ago
Mark,
I found your question from a search because the title of your question is, I think, exactly what I want to ask. So, I don't know the answer and rather than asking another question with a similar title I'll just pile on to yours. I hope that you don't mind.

Here is my question: I have a function that generates output with a negative exponent. I'd like to see it as a fraction instead. Here is a simple example:
In[29]:= TimeValue[pv, rate, -nper]

Out[29]= pv (1 + rate)^-nper
I can force it to display as a fraction:
 In[22]:= FractionBox[Numerator[TimeValue[pv, rate, -nper]],
   Denominator[TimeValue[pv, rate, -nper]]] // DisplayForm
 
 Out[22]//DisplayForm= \!\(
 TagBox[
 FractionBox["pv",
 SuperscriptBox[
 RowBox[{"(",
 RowBox[{"1", "+", "rate"}], ")"}], "nper"]],
DisplayForm]\)
but that is a lot of work and doesn't really work for what I want to do. It seems like there should be an option somewhere to automatically display expressions with negative exponents as fractions in output without having to massage the input. Does such a setting exist somewhere?
POSTED BY: Timothy Mayes
Answer
2 months ago
Hello Timothy ! 
I found a solution to this problem again using the Notation package and some PatternWrappers and it goes like this:
 Needs["Notation`"]
 ClearNotations[]
 Notation[ParsedBoxWrapper[
 FractionBox["1",
 SuperscriptBox["",
 TemplateBox[{
 RowBox[{"Simplify", "[",
 SuperscriptBox["a_",
 RowBox[{
RowBox[{"-", "b_"}], " ", "c_"}]], "]"}]},
      "NotationPatternTag"]]]] \[DoubleLongLeftArrow] ParsedBoxWrapper[
SuperscriptBox["a_",
RowBox[{
TemplateBox[{
RowBox[{"b_", "?", "Negative"}]}, "NotationPatternTag"], " ", "c_"}]]]]
Notation[ParsedBoxWrapper[
FractionBox[
SuperscriptBox["a_", "d_"],
TemplateBox[{
RowBox[{"Simplify", "[",
SuperscriptBox["a_",
RowBox[{
RowBox[{"-", "b_"}], " ", "c_"}]], "]"}]},
     "NotationPatternTag"]]] \[DoubleLongLeftArrow] ParsedBoxWrapper[
SuperscriptBox["a_",
RowBox[{
RowBox[{
TemplateBox[{
RowBox[{"b_", "?", "Negative"}]}, "NotationPatternTag"], " ", "c_"}],
      "+", "d_"}]]]]
With this expressions with negative exponents are always output as fractions. I am still not sure, if this works in every case, but for me it is doing the trick !
POSTED BY: Mark Butler
Answer
2 months ago