Arithmetic with algebraic numbers
- Cost
- The general method computes a composed polynomial of degree with operations on rational power series and factors it over ; the structured methods replace the factorisation by LLL searches of dimension about and linear algebra in a field of that degree.
For algebraic numbers and given by minimal polynomials and and isolating balls (see Algebraic numbers by minimal polynomial and isolating ball), the number for is again algebraic, and qqbar.h computes its minimal polynomial and an isolating ball. Operations with a rational operand are affine substitutions and cost little (qqbar_scalar_op). What follows is about two irrational operands, which are handled by qqbar_binary_op. Its default strategy, classical since Loos [Loo1983], computes a polynomial of degree that annihilates , factors it and selects the right factor; the other strategies avoid this factorisation, which is the bottleneck when the composed polynomial has many factors.
Composed polynomials#
Let have roots and roots . The composed sum and composed product
have degree and vanish at and . Differences and quotients reduce to these by replacing by (roots ) or by its reversal (roots ). Up to the factor they are resultants, , and therefore have integer coefficients after multiplication by . Instead of a resultant, FLINT computes them with power sums, following Bostan, Flajolet, Salvy and Schost [BFSS2006].
Lemma 1 (Newton sums). Let have degree , roots and power sums . With the reversed polynomial ,
Proof. Up to the constant , . Its logarithmic derivative is ; integrating and exponentiating gives the second formula. The first follows from , which is the reversal of .
The power sums of the composed polynomials are obtained from those of and :
The first is a termwise (Hadamard) product of the power sum series; the second says that the exponential generating functions multiply. Hence qqbar_fmpz_poly_composed_op computes the first power sums of and by a power series division, combines them by a termwise product or by a product of exponential generating functions (multiplying by factorials before and after, a Borel transform), and recovers the composed polynomial by a power series exponential, all with fmpq_poly arithmetic. With fast multiplication this costs arithmetic operations on rational numbers, although the coefficients grow to about times the size of those of and .
When and are distinct roots of the same irreducible of degree , for example complex conjugates, has the factor and every root with twice, so a polynomial of half the degree suffices. qqbar_fmpz_poly_symmetric_composed_op computes the polynomials of degree with roots , or for from the power sums
and the even part of the exponential generating function for , where . The difference is then a root of , where is the minimal polynomial of .
Selecting the factor#
The minimal polynomial of is an irreducible factor of the composed polynomial , which is factored over (see Factoring polynomials over the integers). _qqbar_binary_op_select_factor then computes a ball containing from the enclosures of and and evaluates every irreducible factor on . Distinct irreducible polynomials have no common root, so at most one vanishes at , and as the enclosures of and are refined (at doubling precision) the values of all other factors on eventually exclude zero. When exactly one factor has a value containing zero and Theorem 1 of Algebraic numbers by minimal polynomial and isolating ball certifies that isolates a root of it, that factor is the minimal polynomial and the certified ball is the enclosure of .
Quadratic operands#
If both operands are quadratic, and with rational and principal square roots, the result lies in , which is a quadratic field if is a square and a biquadratic field otherwise. Its minimal polynomial is written down in closed form, of degree at most 2 or 4; no factorisation is needed.
If only is quadratic and , then for a polynomial of degree 1 with coefficients in (for example for ). Write with . The norm has degree and rational coefficients and vanishes at .
Lemma 2 (Norms of irreducible polynomials). If , then is a power of an irreducible polynomial in ; in particular, is irreducible if it is squarefree.
Proof. Since and , the fields and are linearly disjoint, so and remains irreducible over . A linear substitution preserves irreducibility, so is irreducible over , with root . The minimal polynomial of over is divisible by in , hence also by , and every root of is a conjugate of over . Thus is a product of copies of (up to a constant).
The hypothesis is certified modularly. Choose a prime , not dividing , such that is squarefree and has an irreducible factor of odd degree , is a quadratic nonresidue modulo , and is squarefree of full degree. Since is squarefree, divides neither the discriminant of nor the index of in the ring of integers of , so by Dedekind's theorem [Neu1999, I.8.3] the factor of degree corresponds to a prime of with residue field . If were in , its reduction modulo would be a square root of in ; but a nonresidue modulo remains a nonsquare in an extension of odd degree. Hence , and , being squarefree modulo , is squarefree and by Lemma 2 irreducible: it is the minimal polynomial of (made primitive). If no such prime is found among a few candidates, is factored. Either way, the factorisation of a polynomial of degree or is replaced by a polynomial of degree .
Operands in a common field#
The composed polynomial of degree splits into many factors over , and modulo every prime, when the Galois group of the compositum is small, for example when or when and are elements of a cyclotomic field. Then the factorisation, with its combinatorial recombination of modular factors, is very expensive, while the result can be computed by linear algebra in a field of degree about . _qqbar_binary_op_structured detects and exploits three situations, once (or for conjugates) is at least 36.
One operand in the field of the other. Suppose and , say with , . Then with (using an extended GCD to invert modulo for division), and the minimal polynomial of is the minimal polynomial of the matrix of multiplication by on with the basis , since a polynomial annihilates the matrix if and only if it annihilates . This is what qqbar_evaluate_fmpq_poly computes, followed by a numerical selection of the root. The polynomial is found with LLL [LLL1982] as an integer relation between at increasing precision, and verified exactly with qqbar_equal_fmpq_poly_val, which checks . Since a failed LLL search is costly, a necessary condition is tested first: for a prime not dividing the leading coefficients and discriminants, the degrees of the irreducible factors of are the residue degrees of the primes of above (Dedekind's theorem), and if then must have a root in each of the corresponding residue fields, two distinct roots if . This is checked for a few dozen primes.
A common cyclotomic field. If , every prime splits completely in [Was1997], so and split into distinct linear factors modulo . Candidate conductors with and are enumerated in order of (up to ), and the first that passes this splitting test for primes (four of them) is taken. Then and are expressed with LLL as polynomials in , or for real operands in the basis of the real subfield, which gives small coefficients for cyclotomic numbers; the expressions are verified exactly and combined by arithmetic in .
Guessing the result. Alternatively, an integer relation between for () is sought with LLL from a numerical value of , and the identity is proved by evaluating at a precision justified by the following bound.
Theorem 3 (Proving a guessed annihilating polynomial). Let , let be nonzero of degree with , let , let (with replaced by its reversal for division) and let bound the absolute values of all numbers . If
then .
Proof. Suppose and let be the minimal polynomial of , of degree . Then for every conjugate of (the conjugates are the roots of , and is irreducible), so the resultant is a nonzero integer and has absolute value at least 1. The polynomial has integer coefficients (it is a resultant) and vanishes at , so the primitive polynomial divides it in by Gauss's lemma, and comparing leading coefficients, divides . Every conjugate is one of the , so , and this bound is at least 1. Hence
which contradicts the hypothesis.
Here is computed from root bounds for and (fmpz_poly_bound_roots): their sum for addition and subtraction, their product for multiplication and division (with the bound for the reversed polynomial). The test needs a precision of about bits; before paying for it, is checked at three times the LLL precision, which eliminates most spurious relations. If the test succeeds, is factored (it has degree , much smaller than ) and the factor is selected as above.
These LLL searches can fail, and their cost grows with the precision. The implementation keeps a list of streams of attempts, one per method and lattice dimension, each with a doubling sequence of precisions, and always runs the cheapest next attempt according to a cost model for LLL, until the total cost exceeds a budget proportional to the estimated cost of the fallback (computing and factoring the composed polynomial). Without detected cyclotomic structure, relations with small coefficients are less likely and the budget is halved.
The complete strategy#
qqbar_binary_op tries, in order:
- equal operands: , 0, or 1;
- a quadratic operand: closed forms or certified norms (Quadratic operands);
- for operands of equal degree at least 4: a rational result, guessed numerically with
qqbar_guessat 128 bits and verified by the exact inverse operation (for example for a sum ), which is cheap since one operand is then rational; - structured methods within a cost budget (Operands in a common field);
- distinct conjugates: the symmetric composed polynomial of half degree (Composed polynomials), factored;
- otherwise: the composed polynomial, factored, and the factor selected numerically (Selecting the factor).
Powers are computed by evaluating at in , after checking for roots of unity, for principal roots of rational numbers, and for minimal polynomials of the form , in which case is a root of . Roots are computed by factoring and selecting the principal root; Capelli's theorem [Sch2000] often certifies that is irreducible, so that no factorisation is needed.
Use in FLINT#
qqbar_add,qqbar_sub,qqbar_mulandqqbar_divhandle rational operands withqqbar_scalar_opand callqqbar_binary_opotherwise;qqbar_re,qqbar_imandqqbar_abs2combine with its conjugate and benefit from the symmetric composed polynomials.qqbar_fmpz_poly_composed_opandqqbar_fmpz_poly_symmetric_composed_opcompute composed polynomials from power sums.qqbar_evaluate_fmpq_polycomputes minimal polynomials of elements of through multiplication matrices (nf_elem_rep_matandfmpq_mat_minpoly).qqbar_express_in_fieldandqqbar_guessexpose the LLL searches of Operands in a common field (see Simplification and closed forms).qqbar_evaluate_fmpz_mpolyandca_get_qqbarevaluate larger expressions with these operations, under limits on degrees and sizes that bound the cost of the composed polynomials.
Referenced from#
- Choosing a representation for exact numbers (overview)
- Algebraic numbers by minimal polynomial and isolating ball (algorithm)
- Simplification and closed forms (algorithm)
- Zero testing (algorithm)
- qqbar.h (module)
_qqbar_evaluate_fmpq_poly,qqbar_evaluate_fmpq_poly,_qqbar_evaluate_fmpz_poly,qqbar_evaluate_fmpz_polyqqbar_fmpz_poly_composed_opqqbar_binary_op- Using qqbar
References#
- [Loo1983] R. Loos. Computing in algebraic extensions. In B. Buchberger, G. E. Collins and R. Loos (eds.), Computer Algebra: Symbolic and Algebraic Computation, second edition, Springer, 1983, 173–187. https://doi.org/10.1007/978-3-7091-7551-4_12
- [BFSS2006] A. Bostan, P. Flajolet, B. Salvy and É. Schost. "Fast computation of special resultants". Journal of Symbolic Computation, 41(1):1–29, January 2006. https://doi.org/10.1016/j.jsc.2005.07.001
- [Neu1999] J. Neukirch. Algebraic Number Theory. Grundlehren der mathematischen Wissenschaften 322, Springer, 1999. https://doi.org/10.1007/978-3-662-03983-0
- [LLL1982] A. K. Lenstra, H. W. Lenstra, Jr. and L. Lovász, "Factoring polynomials with rational coefficients", Mathematische Annalen 261 (1982), 515–534. https://doi.org/10.1007/BF01457454
- [Was1997] L. C. Washington. Introduction to Cyclotomic Fields, second edition. Graduate Texts in Mathematics 83, Springer, 1997. https://doi.org/10.1007/978-1-4612-1934-7
- [Sch2000] A. Schinzel. Polynomials with Special Regard to Reducibility. Cambridge University Press, 2000. https://doi.org/10.1017/CBO9780511542916
The FLINT team, “Arithmetic with algebraic numbers,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/exact/qqbar-arithmetic.html (generated 2026-11-02).
@misc{flint-doc-algorithms-exact-qqbar-arithmetic,
author = {{The FLINT team}},
title = {{Arithmetic with algebraic numbers}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/exact/qqbar-arithmetic.html},
note = {Generated 2026-11-02}
}