3

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}$.

DilithiumMatrix
  • 582
  • 4
  • 12
  • Does $Post=(#/.Power[x_,Rational[a_,b_]]:>HoldForm[x]^HoldForm[(a/b)])& do what you want? – ciao Jun 01 '14 at 01: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 01 '14 at 06:40
  • @Mr.Wizard: Yeah, just threw out an idea - this kind of thing is something I've never had any real need to do. – ciao Jun 01 '14 at 06:56

2 Answers2

5

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • This looks great in a new notebook! How can I append this rule to my previous $PrePrint (i.e. the previous thread)? – DilithiumMatrix Jun 01 '14 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 02 '14 at 03: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' ? – DilithiumMatrix Jun 02 '14 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 02 '14 at 20:57
-1

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 *)
Oleksandr R.
  • 23,023
  • 4
  • 87
  • 125
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198