Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Radical symbols ($\sqrt{\,\,\,\,}$) are the devil. Is there any way to get mathematica to never use them, and instead express everything as an exponential?

i.e. I want

In[1]:= Sqrt[x]

to give $x^{1/2}$ instead of $\sqrt{x}$.

share|improve this question
    
Does $Post=(#/.Power[x_,Rational[a_,b_]]:>HoldForm[x]^HoldForm[(a/b)])& do what you want? –  rasher Jun 1 at 1:49
    
@rasher You should be using Defer rather than HoldForm so that output can be used as input. Also I don't think holding x is necessary. –  Mr.Wizard Jun 1 at 6:40
    
@Mr.Wizard: Yeah, just threw out an idea - this kind of thing is something I've never had any real need to do. –  rasher Jun 1 at 6:56

2 Answers 2

up vote 4 down vote accepted

I think I would choose to use MakeBoxes and Defer for this:

MakeBoxes[a_^Rational[1, x_], fmt_] := ToBoxes[a^Defer[1/x], fmt]

Now:

-Sqrt[a - bar]
-(a - bar)^(1/2)

This also catches cases that use RadicalBox:

x^(1/3) // TraditionalForm

x1/3

Defer is used to allow the output to be used as input. An alternative is Interpretation but that seems like overkill here.

Instead of MakeBoxes definition you could use $PrePrint, assuming it is not already in use or you will append a rule to an existing definition. It is clean but gives you less control over specific formatting.

$PrePrint = # /. a_^Rational[1, x_] :> a^Defer[1/x] &;

If these miss some cases or change things that should not be changed (undetermined), you could instead intercept all box conversions and replace SqrtBox and RadicalBox:

lhs : MakeBoxes[arg__] /; ! TrueQ[$sqrtReplace] :=
     Block[{$sqrtReplace = True},
  lhs /. {
    SqrtBox[a_] :> SuperscriptBox[a, RowBox[{"1", "/", "2"}]],
    RadicalBox[a_, x_] :> SuperscriptBox[a, RowBox[{"1", "/", x}]]
   }
 ]

This should be avoided if possible as it is a costly operation. (It will add overhead to all output generation.)

share|improve this answer
    
This looks great in a new notebook! How can I append this rule to my previous $PrePrint (i.e. the previous thread)? –  zhermes Jun 1 at 14:34
    
@zhermes I suggested MakeBoxes so that that would not be necessary, but if you wish you could use: $PrePrint = If[$note =!= Null, # &[Row[{Pane@#, Spacer[50], $note}], $note = Null], #] &[ ScientificForm@PowerExpand@# /. a_^Rational[1, x_] :> a^Defer[1/x]] &;. Incidentally I made a mistake in the prior answer which I will now correct. –  Mr.Wizard Jun 2 at 3:39
    
Also, it looks like this (the solution from your comment, not using the 'interception') doesn't work for radicals in the denominator. Is this an issue with catching a '-1/2' instead of '+1/2' ? –  zhermes Jun 2 at 14:56
    
@zhermes You're right, I'd need to allow for Rational[-1,2] there. That also raises the question of which format you prefer: foo/-Sqrt[a - bar] could render as either -(foo/(a - bar)^(1/2)) or -(a - bar)^(-(1/2)) foo. –  Mr.Wizard Jun 2 at 20:57

If you look at the FullForm, you will see that it already uses the exponential form:

Sqrt[x] // FullForm
(* -> Power[x, Rational[1, 2]] *)

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

Sqrt[x] === x^(1/2)
(* -> True *)
share|improve this answer
    
I would argue that it doesn't use exponential form, it uses verbose-descriptions-form :) –  zhermes Jun 1 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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