6
$\begingroup$

I am trying to force Mathematica to output $\frac{1}{\sqrt2}$ as $\frac{\sqrt2}{2}$.

I tried:

{1, Sqrt[2]}*Sqrt[2]

Which outputs:

(* {Sqrt[2], 2} *)

Now,

Divide @@ {Sqrt[2], 2}

returns:

(* 1/Sqrt[2] *)

So I tried:

HoldForm[Divide @@ {Sqrt[2], 2}]

But that didn't work. Any suggestions to get my preferred output for this particular example?

$\endgroup$
  • 2
    $\begingroup$ you may want some {1, Sqrt[2]}*Sqrt[2] /. {x_, y_} :> Divide[HoldForm[x], y] $\endgroup$ – garej Jan 17 '16 at 20:06
  • 1
    $\begingroup$ may be Divide @@ HoldForm /@ {Sqrt[2], 2} $\endgroup$ – garej Jan 17 '16 at 20:15
  • 1
    $\begingroup$ Is it reasonable to ask why you prefer $\sqrt{2}/2$ to $1/\sqrt{2}$? While they have the same leaf counts, the latter is simpler in that it uses 1 rather than 2. (Too many secondary school teachers incubate a fear of radicals in denominators, insisting that the student rationalize the denominators.) $\endgroup$ – murray Jan 17 '16 at 21:42
  • $\begingroup$ There's a closely related question: Mathematica Sec and Csc. The title makes it seem not to be a duplicate, but the methods in two of the answers are applicable here. $\endgroup$ – Jens Jan 17 '16 at 22:20
  • 1
    $\begingroup$ I should perhaps mention that your approach, and your accepted answer, are in no way making Mathematica represent 1/Sqrt[2] as Sqrt[2]/2 in output. Rather the question seems to be about printing Sqrt[2]/2. It may be worth updating the title and text accordingly. $\endgroup$ – Oleksandr R. Jan 18 '16 at 1:39
4
$\begingroup$

A slight variation of garej's comment:

Divide @@@ HoldForm[{Sqrt[2], 2}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ this looks like the same, more or less, as @garej comment $\endgroup$ – Mike Honeychurch Jan 17 '16 at 21:50
6
$\begingroup$

If you really insist on getting exactly this output within a standard evaluation, you can do the following:

Unprotect[Power];
Format[Power[2, Rational[-1, 2]]] := HoldForm[Sqrt[2]/2];
Protect[Power];

This leads to the desired output:

1/Sqrt[2]

$$\frac{\sqrt{2}}{2}$$

Sin[1/Sqrt[2]]

$$\sin\left(\frac{\sqrt{2}}{2}\right)$$

N[%]

0.649637

I would only do this if you're sure this is the one and only form in which you wish to display this number.

$\endgroup$
6
$\begingroup$

So long as it is only for output purposes, there is probably no real harm in doing this, and so it does not seem to be a matter of "force" in this case--defining an output form is a perfectly reasonable thing to do. The situation here is rather like in this recent question, and so I propose a similar solution.

The most sensible (and least intrusive) way to accomplish it, in my opinion, would be the following:

MakeBoxes[1/Sqrt[2] | Power[2, Rational[-1, 2]], form_] :=
 MakeBoxes[Sqrt[2]/2, form];

Please let me know if this fails to cover any use cases.

$\endgroup$
4
$\begingroup$

I would make my own SomethingForm function like this:

SpecialForm[expr_] := expr /. {
   1/Sqrt[2] -> HoldForm[Sqrt[2]/2]
   }

SpecialForm[1/Sqrt[2]]

Mathematica graphics

SpecialForm[5 10 + 3^Sqrt[3] + 1/Sqrt[2]]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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