5

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
BoZenKhaa
  • 479
  • 3
  • 9

2 Answers2

5

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}]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
2

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
bill s
  • 68,936
  • 4
  • 101
  • 191
  • 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

    – BoZenKhaa Dec 11 '13 at 18:17
  • 1
    @BoZenKhaa I guess you shouldn't play with Replace since it doesn't work when AlgebraicNumber yields the Root objects. – Artes Dec 11 '13 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 :-) – BoZenKhaa Dec 11 '13 at 19:11