3
$\begingroup$

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

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

2 Answers 2

5
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ This looks great in a new notebook! How can I append this rule to my previous $PrePrint (i.e. the previous thread)? $\endgroup$ Jun 1, 2014 at 14:34
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Jun 2, 2014 at 3:39
  • $\begingroup$ 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' ? $\endgroup$ Jun 2, 2014 at 14:56
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Jun 2, 2014 at 20:57
-1
$\begingroup$

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 *)
$\endgroup$
1
  • $\begingroup$ I would argue that it doesn't use exponential form, it uses verbose-descriptions-form :) $\endgroup$ Jun 1, 2014 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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