6

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?

David
  • 14,883
  • 4
  • 44
  • 117
  • 2
    you may want some {1, Sqrt[2]}*Sqrt[2] /. {x_, y_} :> Divide[HoldForm[x], y] – garej Jan 17 '16 at 20:06
  • 1
    may be Divide @@ HoldForm /@ {Sqrt[2], 2} – garej Jan 17 '16 at 20:15
  • 1
    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.) – murray Jan 17 '16 at 21:42
  • 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. – Jens Jan 17 '16 at 22:20
  • I'd like to thank all my colleagues for a wonderful collection of answers. – David Jan 18 '16 at 00:32
  • Murray, I don't think I recognize the phrase "leaf counts." Can you explain that to me? – David Jan 18 '16 at 00:33
  • 1
    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. – Oleksandr R. Jan 18 '16 at 01:39

4 Answers4

6

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.

Jens
  • 97,245
  • 7
  • 213
  • 499
6

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.

Oleksandr R.
  • 23,023
  • 4
  • 87
  • 125
4

A slight variation of garej's comment:

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

enter image description here

eldo
  • 67,911
  • 5
  • 60
  • 168
4

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

C. E.
  • 70,533
  • 6
  • 140
  • 264