f



how to tell Mathematica to display x^-1 as x^-1 and not as 1/x

Hello;


When I type

x^-1

Mathematica replies

1/x

Is there a way to tell Mathematica to *display* terms with negative 
powers as  x^-n  and not as 1/x^n ? (i.e keep the term just like it 
would appear on paper).

Another example, suppose I type

expr = a*x^-2 + b *x^-1 +

The reason I want to do this, is just for display purposes. When I print 
the expression on the screen, I'd like it to look like x^-1 and not like 
1/x (to better match how the expression look like in the textbook)

I am trying to avoid having to convert everything to a string, and force 
the form to the way I want.  I tried Hold functions, and 
TraditionalForm, but can't get it to print as I want.

I am hoping there is an easy trick to do this?

thanks
--Nasser

0
Nasser
7/3/2010 12:19:49 PM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

3 Replies
802 Views

Similar Articles

[PageSpeed] 57

Hi Nasser,

I think the construction below should work. I don't have much
experience with this, but I couldn't find any side effect.

Unprotect[Power];
Power /: MakeBoxes[Power[x_, y_], StandardForm] :=
 RowBox[{MakeBoxes[x, StandardForm], "^", MakeBoxes[y, StandardForm]}]
Protect[Power];

Cheers -- Sjoerd

On Jul 3, 2:19 pm, "Nasser M. Abbasi" <n...@12000.org> wrote:
> Hello;
>
> When I type
>
> x^-1
>
> Mathematica replies
>
> 1/x
>
> Is there a way to tell Mathematica to *display* terms with negative
> powers as  x^-n  and not as 1/x^n ? (i.e keep the term just like it
> would appear on paper).
>
> Another example, suppose I type
>
> expr = a*x^-2 + b *x^-1 +
>
> The reason I want to do this, is just for display purposes. When I print
> the expression on the screen, I'd like it to look like x^-1 and not like
> 1/x (to better match how the expression look like in the textbook)
>
> I am trying to avoid having to convert everything to a string, and force
> the form to the way I want.  I tried Hold functions, and
> TraditionalForm, but can't get it to print as I want.
>
> I am hoping there is an easy trick to do this?
>
> thanks
> --Nasser


0
Sjoerd
7/4/2010 7:08:50 AM
On Jul 3, 7:19 am, "Nasser M. Abbasi" <n...@12000.org> wrote:
> Hello;
>
> When I type
>
> x^-1
>
> Mathematica replies
>
> 1/x
>
> Is there a way to tell Mathematica to *display* terms with negative
> powers as  x^-n  and not as 1/x^n ? (i.e keep the term just like it
> would appear on paper).
>
> Another example, suppose I type
>
> expr = a*x^-2 + b *x^-1 +
>
> The reason I want to do this, is just for display purposes. When I print
> the expression on the screen, I'd like it to look like x^-1 and not like
> 1/x (to better match how the expression look like in the textbook)
>
> I am trying to avoid having to convert everything to a string, and force
> the form to the way I want.  I tried Hold functions, and
> TraditionalForm, but can't get it to print as I want.
>
> I am hoping there is an easy trick to do this?
>
> thanks
> --Nasser

I've always found this behavior perplexing as well. A great example of
something that can drive you nuts! (Mathematically trivial time-
waster). Here's a hacky workaround that you probably won't like...

(First, note that this doesn't work...)

In[1]:= x^a//HoldForm
          %/.a->-1
Out[1]= x^a
Out[2]= 1/x

(But this does...)

In[3]:= x^-a//HoldForm
          %/.a->1
Out[3]= x^-a
Out[4]= x^-1

There has to be an easier way---I do seem to recall reading about this
issue before, but couldn't find it in the documentation.

-RG

0
telefunkenvf14
7/4/2010 7:09:13 AM
Am 03.07.2010 14:19, schrieb Nasser M. Abbasi:
> Hello;
> 
> 
> When I type
> 
> x^-1
> 
> Mathematica replies
> 
> 1/x
> 
> Is there a way to tell Mathematica to *display* terms with negative 
> powers as  x^-n  and not as 1/x^n ? (i.e keep the term just like it 
> would appear on paper).
> 
> Another example, suppose I type
> 
> expr = a*x^-2 + b *x^-1 +
> 
> The reason I want to do this, is just for display purposes. When I print 
> the expression on the screen, I'd like it to look like x^-1 and not like 
> 1/x (to better match how the expression look like in the textbook)
> 
> I am trying to avoid having to convert everything to a string, and force 
> the form to the way I want.  I tried Hold functions, and 
> TraditionalForm, but can't get it to print as I want.
> 
> I am hoping there is an easy trick to do this?

for this special case you could use the following:

expr /. Power[x, n_?Negative] :> Row[{x, "^", n}]

it should be fairly easy to adopt to other or more general cases.

hth,

albert

0
Albert
7/4/2010 7:10:17 AM
Reply: