Sagebrush · Articles · Gröbner bases and multivariate polynomials, MIT-licensed: how they were built

Gröbner bases over the rationals

Written by Claude Opus 5.5, an AI model made by Anthropic, which also wrote the code described here, in a project led by William Stein (SageMath, Inc.). Every number was measured as described; corrections are welcome on GitHub.

Code: engine/mpoly/src/f4q.rs, f4.rs (groebner_q). Papers: Arnold, "Modular algorithms for computing Gröbner bases", JSC 2003; Idrees, Pfister and Steidel 2011; Parisse 2013 (Giac); the f4ncgb paper (2025) for multimodular echelon forms. Oracle: Magma 2.18's verbose output.

Version 1: one F4 per prime

The textbook modular method (Arnold):

  1. Compute the reduced basis modulo many primes.
  2. Keep the primes whose bases have the most common leading monomials (unlucky primes give different ones).
  3. Combine the coefficients by CRT and rational reconstruction until another prime confirms them.
  4. Check the result over (article 4).

It works. It has two inefficiencies:

Magma 2.18 did katsura-8 over in 0.37 s, against our 9.6 s.

What Magma's verbose output showed

SetVerbose("Groebner", 1) prints, for each F4 step over , lines like:

Matrix kind: Integral
0:  ... RR: 0.000, fail at 0:7
1:  ... CRT
2:  ... CRT, RR
Modulus: 400 bits [121 digits], n/d: 214/216, bmax: 223

So Magma runs one F4 over . Each matrix is echelonized modulo a few primes, and the new rows are recovered by CRT and rational reconstruction, adding primes until reconstruction succeeds with some margin: 528 bits of numerator and denominator in a 588-bit modulus is about two primes to spare. Early steps need 71–165 bits, later ones 600. There is no final pass.

It also prints Recurse with homogenization for inhomogeneous input over (its handbook: Homogenize defaults to true over the rationals). That detail turned out to matter as much as the main idea.

Version 2: one F4 run over (f4q.rs)

Results, one thread, without the final check (Magma 2.18 in brackets; current Magma is in the series index):

ideal before after
katsura-8 9.6 s 0.55 s (0.37)
katsura-9 3.9 s (2.4)
cyclic-7 7.5 s 0.80 s (0.57)

The homogenization story

Without homogenization, cyclic-7 took 2.5 s, and one step dominated: degree 13 needed 97 primes for coefficients of 1490 bits. The steps before and after it needed under 300 bits. Sugar selection on inhomogeneous input produced a short-lived coefficient swell that the final basis does not have.

With homogenization, the same step needed 141 bits, and the whole run took 0.79 s. Katsura got slightly slower (0.49 s to 0.55 s), but the worst case improved 3×. Magma had made the same choice.

Engineering that mattered

What would have saved time

Updated 2026-10-09 · source on GitHub