Skip to content

Preserve mateigen precision rounding and test numerical precision boundaries - #234

Draft
dimpase wants to merge 4 commits into
sagemath:mainfrom
dimpase:fix/pari-219-lfun-stack
Draft

dimpase wants to merge 4 commits into
sagemath:mainfrom
dimpase:fix/pari-219-lfun-stack

Conversation

@dimpase

@dimpase dimpase commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Follow up to #233: retain word-aligned working precision for mateigen and add broader tests for numerical precision arguments.

Passing exact bit counts to PARI 2.19 fixes the L-function stack overflow in #232, but exposes an eigenvector failure at 53-bit working precision:

from cypari2 import Pari
pari = Pari()
M = pari.matrix(2, 2, [0, 1, -2, 0])
M.mateigen(flag=1, precision=53)

Before this change, the call raises PariError: impossible inverse in divrr: 0.E-32. Rounding the working precision to 64 bits restores the previous behavior. Apply this rounding to both Gen.mateigen and Pari.mateigen; other wrappers retain #233's exact bit counts with PARI 2.19. This also fixes Sage's matrix(RealField(53), [[0, 1], [-2., 0]]).eigenvalues(algorithm="pari") without a Sage change.

For this matrix on a 64-bit host, raw PARI calls also fail at lower precisions (including 15, 32, and 48 bits) and at 63 bits. Calls at 64, 65, 66, and higher tested precisions succeed. This is a low-precision failure, not evidence that the algorithm requires word-aligned working precision. Restoring the historical rounding raises these low requests to 64 bits. The regression tests include 15, 32, and 48 bits as well as the boundary cases.

Add regression doctests and a numerical test suite at 53, 63, 64, 65, 127, 128, and 129 bits. The suite checks eigenvector and matrix-decomposition residuals, polynomial roots, 63 transcendental functions against 256-bit references, handwritten vector-returning calls, number-field embeddings/regulators, and elliptic-curve calculations. Run it through both make check and cibuildwheel testing.

Validation with Python 3.14.7 and cysignals 1.13.0:

  • PARI 2.19.0 and 2.17.4: all 2,510 doctests and 15 unit tests pass on each version.
  • Sage's eigenvalue example passes with RealField(53), RealField(65), and ComplexField(53), as do the corresponding empty-matrix cases, using PARI 2.19.
  • The low-precision L-function regression from stack overflow in L function computation with pari 2.19 #232 still passes with an unchanged 8 MB stack.
  • Additional local sweeps found no new exceptions when existing doctest calls were given non-word-aligned precisions. All 225 QR/Jacobi/Cholesky residual checks passed for generated matrices of dimensions 2 through 6.

Further investigation traced the failure to PARI's shared Gaussian pivot code, rather than a word-alignment requirement in mateigen. A direct GP reproducer is:

f(b) = { localbitprec(b); mateigen([0,1;-2,0],1) };
f(53)

It also reproduces on PARI development master d1430f50b248e8a06a5fb67d9a197ae5a6aa2133. Gaussian elimination selects the complex residual 5.421010862427522170 E-20 + 0.E-16*I as a pivot: cx_approx0(p, 1) is false, although its squared norm is 0.E-32. The nonzero real component has 64-bit stored precision; the zero imaginary component has only the requested accuracy. Inverting the pivot therefore raises the error before mateigen can reach its existing higher-precision recovery path.

The GP reproducer passes on PARI 2.17.1 (verified with a fresh source build) and 2.17.4. A git bisect identifies 567715f74ed1a4397fdbe1770254c1b4f08abe80 ("neutralize nbits2prec", committed 2026-09-22) as the first bad commit; its immediate parent 110e2bfa61999dcf17322e8ac7cb34c25efa8110 passes. The commit changes nbits2prec(53) from 64 to 53. At that point, get_localprec() still calls nbits2prec(get_localbitprec()), so the GP call starts passing exactly 53 bits to mateigen, exposing the latent numerical pivot defect.

This also affects matker, matrank, matimage, and matindexrank on [-r,1;-2,-r], where r=polroots(x^2+2)[1] computed at localbitprec(53). A candidate upstream change rejects complex pivots whose squared norm is zero at the available precision. It fixes these reproductions and the eigenvector example at every precision from 15 through 193; PARI's linear, mat, real, and rootsreal tests pass for both static and dynamic GP builds. Reported upstream as PARI bug #2708, with the reproducer, trace, bisect result, and candidate patch.

A separate local test reproduces the failure with unpatched PARI 2.17.4: see the standalone GP reproducer and recorded result. This is a reproduction performed during this investigation, not a reference to a previously published bug report. The equivalent cypari2 call, pari.matrix(2, 2, [0., 1., -2., 0.]).mateigen(flag=1, precision=64), raises impossible inverse in divrr with both PARI 2.17.4 and 2.19.0; precision 128 succeeds. Applying the candidate pivot fix directly to the PARI 2.19.0 release tag makes this exact cypari2 call succeed, returning eigenvalues approximately +/-1.4142135623730950488*I and corresponding eigenvectors. The loaded library path was checked to confirm that the patched 2.19.0 build was used. All nine cypari2 precision unit tests and PARI's four test suites above also pass with patched 2.19.0.


The cypari2 rounding workaround (the current branch of this PR) remains temporary and does not address all manifestations of the shared pivot defect.

@dimpase
dimpase requested a review from antonio-rojas October 2, 2026 01:19
@dimpase

dimpase commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@antonio-rojas - thanks to codex 🤖 it was easy to fix. Please review

@dimpase
dimpase marked this pull request as draft October 2, 2026 01:42
@dimpase

dimpase commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

Actually, it's a pure Pari/gp bug. Inverting matrices is hard :-)
More later

@dimpase

dimpase commented Oct 2, 2026

Copy link
Copy Markdown
Member Author

@orlitzky - see the description of the PR above (if only for fun)

@dimpase

dimpase commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@antonio-rojas Karim fixed the bug that was holding it all up, at least that's what he says. We can drop patches and switch to Pari master - although I would rather wait for a release

@antonio-rojas

antonio-rojas commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Yes, all tests in sage are fine now with cypari2 2.2.6 (and a patched pari 2.19)

@dimpase

dimpase commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

by patched pari, do you mean the current pari master?

@antonio-rojas

Copy link
Copy Markdown
Member

by patched pari, do you mean the current pari master?

No, I mean with backports for the fixed bugs that affect sage. I assume git master is likely to cause more breakage due to new stuff.

@dimpase

dimpase commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

it looks like cypari2 is a good way to find pari bugs.

I'd try using their master and see what news we get this way. Are there any changes in 2.2.6 needed for such a switch?

@AurelPage

Copy link
Copy Markdown

I second trying to update to master regularly and reporting things that break! If you have time to do it, that is. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants