5
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ Have you heard about ToRadicals ? $\endgroup$
    – Artes
    Commented Dec 11, 2013 at 17:29
  • $\begingroup$ I have not, thank you! It looks promising, but it also reduces the root the number field is based on. $\endgroup$
    – BoZenKhaa
    Commented Dec 11, 2013 at 17:38
  • 1
    $\begingroup$ Is this useful? AlgebraicNumber[1/2 (1 + Sqrt[5]), {-3, 2}] //. 1/2 (1 + Sqrt[5]) -> q $\endgroup$
    – bill s
    Commented Dec 11, 2013 at 17:59
  • $\begingroup$ 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.. $\endgroup$
    – BoZenKhaa
    Commented Dec 11, 2013 at 18:05

2 Answers 2

5
$\begingroup$

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}]
$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Artes
    Commented Dec 11, 2013 at 19:12
  • $\begingroup$ Oh yes, this is awesome. Along the lines of A. C. Clarke, it looks like magic to me. Thank you a lot. $\endgroup$
    – BoZenKhaa
    Commented Dec 11, 2013 at 19:18
  • 1
    $\begingroup$ @Artes In the case that q has a value assigned? One could also use \[FormalQ]. $\endgroup$
    – Mr.Wizard
    Commented Dec 11, 2013 at 19:20
  • $\begingroup$ @BoZenKhaa Glad I could help. :-) $\endgroup$
    – Mr.Wizard
    Commented Dec 11, 2013 at 19:20
2
$\begingroup$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.