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.
Answer
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}]
Comments
Post a Comment