# Showing AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}] in human-readable way

Is there a function to show algebraic numbers in Mathematica more conveniently? I am looking for something similar to MatrixForm for matrices.

For example, I would like to view AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}] as -3 + 2*q, where q is the generator of the number field my algebraic number comes from, i. e. 1/2 (1 + Sqrt[5]) in this case.

I searched the documentation concerning algebraic numbers with no success. Something along the lines of Extract[AlgebraicNumber[...], {2}].{1, x} works, but it requires tweaking for different fields.

• Have you heard about ToRadicals ? Dec 11, 2013 at 17:29
• I have not, thank you! It looks promising, but it also reduces the root the number field is based on. Dec 11, 2013 at 17:38
• Is this useful? AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}] //. 1/2 (1 + Sqrt[5]) -> q Dec 11, 2013 at 17:59
• Yes, thanks, that does the job exactly! I take it there is probably no built in function for this? I guess not as this is very simple and neat solution already.. Dec 11, 2013 at 18:05

Are you perhaps looking for AlgebraicNumberPolynomial?

AlgebraicNumberPolynomial[AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}], HoldForm[q]]

-3 + 2 q


The question states: "... show algebraic numbers" and "I would like to view ..." If you would like to display the AlgebraicNumber expression this way but retain its full syntax you can use a formatting function. You typically have several choices including Format, MakeBoxes, and \$PrePrint. MakeBoxes is usually preferred for robustness and performance. For example:

MakeBoxes[p : AlgebraicNumber[_, {__}], fmt_] :=
ToBoxes[Interpretation[AlgebraicNumberPolynomial[p, HoldForm @ q], p], fmt]


Now:

AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}]

-3 + 2 q

% // InputForm

AlgebraicNumber[(1 + Sqrt[5])/2, {-3, 2}]

• Seems like you've found the way to go (+1) but why do you need to play with HoldForm in AlgebraicNumberPolynomial, it seems to be superfluous. Dec 11, 2013 at 19:12
• Oh yes, this is awesome. Along the lines of A. C. Clarke, it looks like magic to me. Thank you a lot. Dec 11, 2013 at 19:18
• @Artes In the case that q has a value assigned? One could also use \[FormalQ]. Dec 11, 2013 at 19:20
• @BoZenKhaa Glad I could help. :-) Dec 11, 2013 at 19:20

This is a reasonable application of Replace:

AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}] /. {1/2 (1 + Sqrt[5]) -> q}
-3 + 2 q


The same replacement rule works for sums and products of AlgebraicNumbers.

As BoZenKhaa points out, if you don't know the exact form of the base, you can extract it and then do the replacement.

b = AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}];
b /. Extract[b, {1}] -> q
-3 + 2 q

• I am using algebraic number b as the base of the number field, and this slight change works for me: AlgebraicNumber[..] //. Extract[b, {1}] -> x Dec 11, 2013 at 18:17
• @BoZenKhaa I guess you shouldn't play with Replace since it doesn't work when AlgebraicNumber yields the Root objects. Dec 11, 2013 at 18:58
• That is a good point, however it works with the Extract. I will not be using the results for any computations, just presentation, so I feel safe :-) Dec 11, 2013 at 19:11