Skip to main content
Updated to fix entries like `1 * Sqrt[3]`
Source Link
Carl Woll
  • 131.7k
  • 6
  • 246
  • 359

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]

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:

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 - 1/2 Sqrt[6]

1 - I Sqrt[2] + 2.2 Sqrt[3] - (2 - I) Sqrt[6]

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]

Source Link
Carl Woll
  • 131.7k
  • 6
  • 246
  • 359

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:

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 - 1/2 Sqrt[6]

1 - I Sqrt[2] + 2.2 Sqrt[3] - (2 - I) Sqrt[6]