Int64 overflow in `hnfc` gives wrong Hermite forms, sometimes with no error
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 32/100
Direzione di ricerca
Start by reproducing the failing case in the issue: run hnfc on the 3×3 matrix and compare H against hnfc(big.(A)).H, then read hnf_ma! (referenced as the source of the overflow) in the package source and the existing isunimodular check that usually raises AssertionError. The thread asks the maintainer to choose between replacing the square nonsingular path inside hnfc/hnfr and adding a separate function, so no patch should be written until that is answered; done looks like random-matrix tests against big.(A) with no wrong answers.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
While testing the speedups in #26 for my package Enumlib.jl, I compared hnfc on random integer matrices with the same call on BigInt copies of them. The comparison turned up the problem below. It is in the current release (0.1.10) and #26 does not change it.
The problem
Intermediate values in hnf_ma! overflow Int64 even when the answer is small. Usually the result is AssertionError: U is not unimodular. For 3×3 matrices with large entries the wrong H comes back with no error.
A = [-2418 -2096 -2389; -1433 2107 -1800; -2007 1321 756]
hnfc(A).H # [1 0 0; 0 1 0; -17078536264 -16215364689 25023961348]
hnfc(big.(A)).H # [1 0 0; 0 1 0; -17959973296 -16215364689 25023961348]
H[3,1] is wrong. The returned U passes isunimodular, but A*U != H.
How often it happens (hnfc on random square matrices, compared with big.(A)):
| Size | Entries | Wrong H |
Error raised |
|---|---|---|---|
| 6×6 | −9 to 9 | 9% | yes |
| 7×7 | −9 to 9 | 68% | yes |
| 8×8 | −9 to 9 | 99.5% | yes |
| 3×3 | −1000 to 1000 | 0 of 20,000 | |
| 3×3 | −3000 to 3000 | 22 of 20,000 | no |
| 3×3 | −10⁴ to 10⁴ | 15% | no |
| 3×3 | −10⁵ to 10⁵ | 97% | no |
The answers themselves are small. At 10×10 with entries −9 to 9 the largest entry of the correct H or U has 12 digits. The largest intermediate entry in hnf_ma! has 286 digits. This growth comes from the elimination order. Clearing one row at a time across all remaining columns is known to give exponential growth in the size of the intermediate entries (Storjohann's survey talk, slide 20, citing Fang and Havas, ISSAC 1997). The Kannan–Bachem order keeps the growth polynomial, but in my tests it still passes 18 digits from about 7×7.
This is related to #2 and to your comment on #20. There you preferred not to change the output type, and you noted that overflow is rare in your own work. I agree on both points. What worries me is the silent case, because a user has no signal to switch to BigInt.
A possible fix
For square nonsingular input there is a standard method that removes the problem without changing the output type. It works modulo the determinant d = |det A| (Domich, Kannan and Trotter 1987). FLINT implements it as fmpz_mat_hnf_modular, citing that paper and Cohen's Algorithm 2.4.8. Every intermediate entry stays below d, so the sizes are known before any arithmetic starts. No prediction of intermediate growth is needed.
I wrote a prototype in this form:
- The Hadamard bound (the product of the column lengths) bounds d and every minor of A. If it is below 2^30 all arithmetic is
Int64. If it is below 2^62 the products are formed inInt128. Otherwise the function declines, so it never returns a wrong answer. Uis computed afterwards asA^-1 H, which is unique for nonsingular A, using a fraction-free inverse.- Output is
Matrix{Int}, the same type as now, in both rounding conventions.
Checks so far:
- 46,746 comparisons with
hnfconBigIntcopies (sizes 1 to 12, 3×3 entries up to 10⁶, matrices with many nontrivial diagonal entries, both rounding modes), run with every product and sum checked for overflow. There were no wrong answers and no overflows. - 18,941 matrices compared with FLINT's
hnfthrough Nemo.jl, with no disagreements. - With the Hadamard-bound guarantee, 3×3 matrices with entries up to about 960,000 are covered, 6×6 up to 526, and 10×10 up to 23. In practice random matrices with entries −9 to 9 fit up to 14×14.
Timings (minimum, ns, Julia 1.12, Intel Xeon W-3245):
| Matrix | hnfc now |
prototype, H only | prototype, H and U |
|---|---|---|---|
| 3×3, small determinant | 341 | 244 | 409 |
| 3×3, entries −9 to 9 | 391 | 349 | 532 |
| 5×5, entries −9 to 9 | 1140 | 1063 | 1762 |
| 8×8, entries −9 to 9 | fails | 5240 | 9097 |
So H alone is faster than now, and H with U is 1.2 to 1.5 times slower.
The prototype is about 280 lines with comments, and non-square or singular input would still go through hnf_ma!. Before preparing a pull request I wanted to ask whether you would like this in NormalForms.jl, and if so in what form. It could replace the square nonsingular case inside hnfc/hnfr, or be a separate function.
I made these measurements and the prototype with assistance from Claude Code. I checked all the results.
- Lingua principale
- Julia
- Stelle
- 6
- Fork
- 3
- Merge medio
- 2g 2h
- PR unite (30g)
- 4
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di brainandforce/NormalForms.jl
-
tagbot-manual
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 22/100
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 12/100
brainandforce/NormalForms.jl#17 · 1 commento ·
-
good first issue help wanted
Difficoltà 4/5 3-5 giorni Idoneità per principianti 22/100
brainandforce/NormalForms.jl#16 · 1 commento ·
Tutte le issue di brainandforce/NormalForms.jl
Issue simili
-
The entrypoint deletes its manifest runfiles tree at exit even under TEST_TMPDIRForse già presa @TrapsterDK l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
periareon/rules_julia#64 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
JuliaAI/MLJTransforms.jl#108 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
CliMA/Oceananigans.jl#6603 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
JuliaGPU/GemmKernels.jl#225 ·