FLINT3.7.0-dev
Algorithm

Arithmetic with algebraic numbers

Cost
The general method computes a composed polynomial of degree N=deg⁡xdeg⁡yN = \deg x \deg y with O(M(N))O(M(N)) operations on rational power series and factors it over Z\mathbb{Z}; the structured methods replace the factorisation by LLL searches of dimension about max⁡(deg⁡x,deg⁡y)\max(\deg x, \deg y) and linear algebra in a field of that degree.

For algebraic numbers xx and yy given by minimal polynomials PP and QQ and isolating balls (see Algebraic numbers by minimal polynomial and isolating ball), the number w=x∘yw = x \circ y for ∘∈{+,−,×,/}\circ \in \{+, -, \times, /\} is again algebraic, and qqbar.h computes its minimal polynomial and an isolating ball. Operations with a rational operand are affine substitutions P((t−b)/a)P((t - b)/a) 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 deg⁡xdeg⁡y\deg x \deg y that annihilates ww, 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 PP have roots a1,…,ama_1, \ldots, a_m and QQ roots b1,…,bnb_1, \ldots, b_n. The composed sum and composed product

P⊕Q=∏i,j(t−ai−bj),P⊗Q=∏i,j(t−aibj)P \oplus Q = \prod_{i,j} (t - a_i - b_j), \qquad P \otimes Q = \prod_{i,j} (t - a_i b_j)

have degree mnmn and vanish at x+yx + y and xyxy. Differences and quotients reduce to these by replacing Q(t)Q(t) by Q(−t)Q(-t) (roots −bj-b_j) or by its reversal tnQ(1/t)t^n Q(1/t) (roots 1/bj1/b_j). Up to the factor L=lc⁡(P)nlc⁡(Q)mL = \operatorname{lc}(P)^n \operatorname{lc}(Q)^m they are resultants, L⋅(P⊕Q)(t)=Res⁡s(P(s),Q(t−s))L \cdot (P \oplus Q)(t) = \operatorname{Res}_s(P(s), Q(t - s)), and therefore have integer coefficients after multiplication by LL. Instead of a resultant, FLINT computes them with power sums, following Bostan, Flajolet, Salvy and Schost [BFSS2006].

Lemma 1 (Newton sums). Let F∈Q[t]F \in \mathbb{Q}[t] have degree dd, roots c1,…,cdc_1, \ldots, c_d and power sums pk=∑icikp_k = \sum_i c_i^k. With the reversed polynomial F~(t)=tdF(1/t)\tilde F(t) = t^d F(1/t),

∑k≥0pktk=td−1F′(1/t)F~(t),F~(t)=F~(0)exp⁡(−∑k≥1pkktk).\sum_{k \ge 0} p_k t^k = \frac{t^{d-1} F'(1/t)}{\tilde F(t)}, \qquad \tilde F(t) = \tilde F(0) \exp\Bigl(-\sum_{k \ge 1} \frac{p_k}{k} t^k\Bigr).

Proof. Up to the constant lc⁡(F)\operatorname{lc}(F), F~(t)=∏i(1−cit)\tilde F(t) = \prod_i (1 - c_i t). Its logarithmic derivative is F~′/F~=−∑ici/(1−cit)=−∑k≥1pktk−1\tilde F'/\tilde F = -\sum_i c_i / (1 - c_i t) = -\sum_{k \ge 1} p_k t^{k-1}; integrating and exponentiating gives the second formula. The first follows from td−1F′(1/t)/F~(t)=∑i1/(1−cit)t^{d-1} F'(1/t) / \tilde F(t) = \sum_i 1/(1 - c_i t), which is the reversal of F′/F=∑i1/(t−ci)F'/F = \sum_i 1/(t - c_i).

The power sums of the composed polynomials are obtained from those of PP and QQ:

∑i,j(aibj)k=pk(P) pk(Q),∑i,j(ai+bj)k=∑l=0k(kl)pl(P) pk−l(Q).\sum_{i,j} (a_i b_j)^k = p_k(P) \, p_k(Q), \qquad \sum_{i,j} (a_i + b_j)^k = \sum_{l=0}^{k} \binom{k}{l} p_l(P) \, p_{k-l}(Q).

The first is a termwise (Hadamard) product of the power sum series; the second says that the exponential generating functions ∑kpktk/k!\sum_k p_k t^k / k! multiply. Hence qqbar_fmpz_poly_composed_op computes the first mn+1mn + 1 power sums of PP and QQ 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 O(M(mn))O(M(mn)) arithmetic operations on rational numbers, although the coefficients grow to about mnmn times the size of those of PP and QQ.

When xx and yy are distinct roots of the same irreducible PP of degree dd, for example complex conjugates, P⊕PP \oplus P has the factor ∏i(t−2ai)\prod_i (t - 2a_i) and every root ai+aja_i + a_j with i≠ji \ne j twice, so a polynomial of half the degree suffices. qqbar_fmpz_poly_symmetric_composed_op computes the polynomials of degree d(d−1)/2d(d-1)/2 with roots ai+aja_i + a_j, aiaja_i a_j or (ai−aj)2(a_i - a_j)^2 for i<ji < j from the power sums

∑i<j(aiaj)k=pk2−p2k2,∑i<j(ai+aj)k=12(∑l(kl)plpk−l−2kpk),\sum_{i<j} (a_i a_j)^k = \frac{p_k^2 - p_{2k}}{2}, \qquad \sum_{i<j} (a_i + a_j)^k = \frac{1}{2} \Bigl(\sum_{l} \binom{k}{l} p_l p_{k-l} - 2^k p_k\Bigr),

and the even part of the exponential generating function E(t)E(−t)E(t) E(-t) for (ai−aj)2(a_i - a_j)^2, where E(t)=∑pktk/k!E(t) = \sum p_k t^k/k!. The difference x−yx - y is then a root of V(t2)V(t^2), where VV is the minimal polynomial of (x−y)2(x - y)^2.

Selecting the factor#

The minimal polynomial of ww is an irreducible factor of the composed polynomial HH, which is factored over Z\mathbb{Z} (see Factoring polynomials over the integers). _qqbar_binary_op_select_factor then computes a ball WW containing ww from the enclosures of xx and yy and evaluates every irreducible factor fkf_k on WW. Distinct irreducible polynomials have no common root, so at most one fkf_k vanishes at ww, and as the enclosures of xx and yy are refined (at doubling precision) the values of all other factors on WW 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 WW isolates a root of it, that factor is the minimal polynomial and the certified ball is the enclosure of ww.

Quadratic operands#

If both operands are quadratic, x=a1+b1D1x = a_1 + b_1 \sqrt{D_1} and y=a2+b2D2y = a_2 + b_2 \sqrt{D_2} with rational ak,bka_k, b_k and principal square roots, the result lies in Q(D1,D2)\mathbb{Q}(\sqrt{D_1}, \sqrt{D_2}), which is a quadratic field if D1D2D_1 D_2 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 y=a+bDy = a + b\sqrt{D} is quadratic and deg⁡x=d≥3\deg x = d \ge 3, then x=L(w)x = L(w) for a polynomial LL of degree 1 with coefficients in K=Q(D)K = \mathbb{Q}(\sqrt{D}) (for example L(z)=z−yL(z) = z - y for w=x+yw = x + y). Write G(z)=P(L(z))=U(z)+DV(z)G(z) = P(L(z)) = U(z) + \sqrt{D} V(z) with U,V∈Q[z]U, V \in \mathbb{Q}[z]. The norm H=U2−DV2=GG‾H = U^2 - D V^2 = G \overline{G} has degree 2d2d and rational coefficients and vanishes at ww.

Lemma 2 (Norms of irreducible polynomials). If D∉Q(x)\sqrt{D} \notin \mathbb{Q}(x), then HH is a power of an irreducible polynomial in Q[z]\mathbb{Q}[z]; in particular, HH is irreducible if it is squarefree.

Proof. Since [K:Q]=2[K : \mathbb{Q}] = 2 and K⊈Q(x)K \not\subseteq \mathbb{Q}(x), the fields KK and Q(x)\mathbb{Q}(x) are linearly disjoint, so [K(x):K]=d[K(x) : K] = d and PP remains irreducible over KK. A linear substitution preserves irreducibility, so GG is irreducible over KK, with root ww. The minimal polynomial ff of ww over Q\mathbb{Q} is divisible by GG in K[z]K[z], hence also by G‾\overline{G}, and every root of H=GG‾H = G \overline{G} is a conjugate of ww over Q\mathbb{Q}. Thus HH is a product of copies of ff (up to a constant).

The hypothesis is certified modularly. Choose a prime pp, not dividing 2Dlc⁡(P)lc⁡(H)2D \operatorname{lc}(P) \operatorname{lc}(H), such that P mod pP \bmod p is squarefree and has an irreducible factor of odd degree ff, DD is a quadratic nonresidue modulo pp, and H mod pH \bmod p is squarefree of full degree. Since P mod pP \bmod p is squarefree, pp divides neither the discriminant of PP nor the index of Z[x]\mathbb{Z}[x] in the ring of integers of Q(x)\mathbb{Q}(x), so by Dedekind's theorem [Neu1999, I.8.3] the factor of degree ff corresponds to a prime p\mathfrak{p} of Q(x)\mathbb{Q}(x) with residue field Fpf\mathbb{F}_{p^f}. If D\sqrt{D} were in Q(x)\mathbb{Q}(x), its reduction modulo p\mathfrak{p} would be a square root of DD in Fpf\mathbb{F}_{p^f}; but a nonresidue modulo pp remains a nonsquare in an extension of odd degree. Hence D∉Q(x)\sqrt{D} \notin \mathbb{Q}(x), and HH, being squarefree modulo pp, is squarefree and by Lemma 2 irreducible: it is the minimal polynomial of ww (made primitive). If no such prime is found among a few candidates, HH is factored. Either way, the factorisation of a polynomial of degree d2d^2 or 2d22d^2 is replaced by a polynomial of degree 2d2d.

Operands in a common field#

The composed polynomial of degree mnmn splits into many factors over Z\mathbb{Z}, and modulo every prime, when the Galois group of the compositum is small, for example when y∈Q(x)y \in \mathbb{Q}(x) or when xx and yy 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 max⁡(m,n)\max(m, n). _qqbar_binary_op_structured detects and exploits three situations, once mnmn (or d(d−1)/2d(d-1)/2 for conjugates) is at least 36.

One operand in the field of the other. Suppose n∣mn \mid m and y∈Q(x)y \in \mathbb{Q}(x), say y=R(x)y = R(x) with R∈Q[t]R \in \mathbb{Q}[t], deg⁡R<m\deg R < m. Then w=h(x)w = h(x) with h=t∘R(t) mod Ph = t \circ R(t) \bmod P (using an extended GCD to invert RR modulo PP for division), and the minimal polynomial of h(x)h(x) is the minimal polynomial of the matrix of multiplication by h(x)h(x) on Q(x)\mathbb{Q}(x) with the basis 1,x,…,xm−11, x, \ldots, x^{m-1}, since a polynomial annihilates the matrix if and only if it annihilates h(x)h(x). This is what qqbar_evaluate_fmpq_poly computes, followed by a numerical selection of the root. The polynomial RR is found with LLL [LLL1982] as an integer relation between 1,x,…,xm−1,y1, x, \ldots, x^{m-1}, y at increasing precision, and verified exactly with qqbar_equal_fmpq_poly_val, which checks Q(R(x))≡0 mod PQ(R(x)) \equiv 0 \bmod P. Since a failed LLL search is costly, a necessary condition is tested first: for a prime pp not dividing the leading coefficients and discriminants, the degrees of the irreducible factors of P mod pP \bmod p are the residue degrees of the primes of Q(x)\mathbb{Q}(x) above pp (Dedekind's theorem), and if y∈Q(x)y \in \mathbb{Q}(x) then QQ must have a root in each of the corresponding residue fields, two distinct roots if P=QP = Q. This is checked for a few dozen primes.

A common cyclotomic field. If x,y∈Q(ζℓ)x, y \in \mathbb{Q}(\zeta_\ell), every prime p≡1(modℓ)p \equiv 1 \pmod \ell splits completely in Q(ζℓ)\mathbb{Q}(\zeta_\ell) [Was1997], so PP and QQ split into distinct linear factors modulo pp. Candidate conductors ℓ≢2(mod4)\ell \not\equiv 2 \pmod 4 with m∣φ(ℓ)m \mid \varphi(\ell) and n∣φ(ℓ)n \mid \varphi(\ell) are enumerated in order of φ(ℓ)\varphi(\ell) (up to φ(ℓ)≤128\varphi(\ell) \le 128), and the first that passes this splitting test for primes p≡1(modℓ)p \equiv 1 \pmod \ell (four of them) is taken. Then xx and yy are expressed with LLL as polynomials in ζℓ\zeta_\ell, or for real operands in the basis 1,2cos⁡(2πk/ℓ)1, 2\cos(2\pi k/\ell) of the real subfield, which gives small coefficients for cyclotomic numbers; the expressions are verified exactly and combined by arithmetic in Q(ζℓ)\mathbb{Q}(\zeta_\ell).

Guessing the result. Alternatively, an integer relation gg between 1,w,…,wk1, w, \ldots, w^k for k=d,d/2,d/4k = d, d/2, d/4 (d=max⁡(m,n)d = \max(m, n)) is sought with LLL from a numerical value of ww, and the identity g(w)=0g(w) = 0 is proved by evaluating g(w)g(w) at a precision justified by the following bound.

Theorem 3 (Proving a guessed annihilating polynomial). Let w=x∘yw = x \circ y, let g∈Z[z]g \in \mathbb{Z}[z] be nonzero of degree kk with ∥g∥1=∑∣gi∣\|g\|_1 = \sum |g_i|, let N=mnN = mn, let L=lc⁡(P)nlc⁡(Q)mL = \operatorname{lc}(P)^n \operatorname{lc}(Q)^m (with QQ replaced by its reversal for division) and let R≥1R \ge 1 bound the absolute values of all numbers ai∘bja_i \circ b_j. If

∣g(w)∣<∣L∣−k(∥g∥1Rk)−(N−1),|g(w)| < |L|^{-k} \bigl(\|g\|_1 R^k\bigr)^{-(N-1)},

then g(w)=0g(w) = 0.

Proof. Suppose g(w)≠0g(w) \ne 0 and let f∈Z[z]f \in \mathbb{Z}[z] be the minimal polynomial of ww, of degree e≤Ne \le N. Then g(v)≠0g(v) \ne 0 for every conjugate vv of ww (the conjugates are the roots of ff, and ff is irreducible), so the resultant Res⁡(f,g)=lc⁡(f)k∏f(v)=0g(v)\operatorname{Res}(f, g) = \operatorname{lc}(f)^k \prod_{f(v) = 0} g(v) is a nonzero integer and has absolute value at least 1. The polynomial L∏i,j(z−ai∘bj)L \prod_{i,j} (z - a_i \circ b_j) has integer coefficients (it is a resultant) and vanishes at ww, so the primitive polynomial ff divides it in Z[z]\mathbb{Z}[z] by Gauss's lemma, and comparing leading coefficients, lc⁡(f)\operatorname{lc}(f) divides LL. Every conjugate vv is one of the ai∘bja_i \circ b_j, so ∣g(v)∣≤∥g∥1Rk|g(v)| \le \|g\|_1 R^k, and this bound is at least 1. Hence

1≤∣lc⁡(f)∣k ∣g(w)∣∏v≠w∣g(v)∣≤∣L∣k ∣g(w)∣ (∥g∥1Rk)N−1,1 \le |\operatorname{lc}(f)|^k \, |g(w)| \prod_{v \ne w} |g(v)| \le |L|^k \, |g(w)| \, \bigl(\|g\|_1 R^k\bigr)^{N-1},

which contradicts the hypothesis.

Here RR is computed from root bounds for PP and QQ (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 klog⁡2∣L∣+(N−1)(log⁡2∥g∥1+klog⁡2R)k \log_2 |L| + (N - 1)(\log_2 \|g\|_1 + k \log_2 R) bits; before paying for it, g(w)g(w) is checked at three times the LLL precision, which eliminates most spurious relations. If the test succeeds, gg is factored (it has degree kk, much smaller than NN) 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:

  1. equal operands: 2x2x, 0, x2x^2 or 1;
  2. a quadratic operand: closed forms or certified norms (Quadratic operands);
  3. for operands of equal degree at least 4: a rational result, guessed numerically with qqbar_guess at 128 bits and verified by the exact inverse operation (for example x=t−yx = t - y for a sum tt), which is cheap since one operand is then rational;
  4. structured methods within a cost budget (Operands in a common field);
  5. distinct conjugates: the symmetric composed polynomial of half degree (Composed polynomials), factored;
  6. otherwise: the composed polynomial, factored, and the factor selected numerically (Selecting the factor).

Powers xnx^n are computed by evaluating tnt^n at xx in Q(x)\mathbb{Q}(x), after checking for roots of unity, for principal roots of rational numbers, and for minimal polynomials of the form P(t)=H(tn)P(t) = H(t^n), in which case xnx^n is a root of HH. Roots x1/nx^{1/n} are computed by factoring P(tn)P(t^n) and selecting the principal root; Capelli's theorem [Sch2000] often certifies that P(tn)P(t^n) is irreducible, so that no factorisation is needed.

Use in FLINT#

Referenced from#

References#

Cite this page

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).