How to display numbers as multiples of a square root
Maybe you can use a wrapper + MakeBoxes
. Here is the basis using the wrapper q
:
QBasis = {1, q[Sqrt[2]], q[Sqrt[3]], q[Sqrt[6]]}
Now, we tell Mathematica how to format the q
:
q /: MakeBoxes[q[x_], StandardForm] := MakeBoxes[x, StandardForm]
MakeBoxes[n_ q[x_], StandardForm] := RowBox[{
Parenthesize[n, StandardForm, Times, Left],
MakeBoxes[x]}
]
A couple examples:
{1, 0, 0, -1/2} . QBasis
{1, -I, 2.2, -2+I} . QBasis
{1, 0, 1, 1} . QBasis
1 - 1/2 Sqrt[6]
1 - I Sqrt[2] + 2.2 Sqrt[3] - (2 - I) Sqrt[6]
1 + Sqrt[3] + 1/2 Sqrt[6]
A = {1, 2, 3, 1/2} /. {Rational[x_, y_] :> If[x > 0, HoldForm[x/y],
-HoldForm@Evaluate[-x/y]]};
B = MapAt[HoldForm, {1, Sqrt[2], Sqrt[3], Sqrt[6]}, 2 ;;]
A.B
1+2 Sqrt[2]+3 Sqrt[3]+1/2 Sqrt[6]