FLINT3.7.0-dev
Algorithm

Optimal precision in p-adic linear algebra

Cost
Determinant and linear solving: the cost of a blocked LU decomposition with fast matrix multiplication, plus one rank-revealing LU decomposition over Fp\mathbf{F}_p per valuation level. Characteristic polynomial: O(n4)O(n^4) (Berkowitz) for matrices that are cyclic modulo pp, otherwise an O(n3)O(n^3) Hessenberg reduction at doubled precision and nn matrix products modulo pNp^N.

In the ball model (The ball model of padic_radix) every result comes with a guaranteed precision, but different algorithms for the same problem can guarantee very different precisions: the error terms propagated operation by operation may be much larger than the actual uncertainty of the result. Caruso, Roe and Vaccon [CRV2014; CRV2015; CRV2017] showed how to determine the actual uncertainty from the differential of the function computed, and studied which algorithms attain it; [Car2017] surveys these results. This page describes the methods padic_radix uses to attain the optimal precision for determinants, linear systems and characteristic polynomials.

Differential precision#

Lemma 1 (First- and second-order terms). Let f:Zpm→Zpf : \mathbf{Z}_p^m \to \mathbf{Z}_p be a polynomial with coefficients in Zp\mathbf{Z}_p, let x∈Zpmx \in \mathbf{Z}_p^m, and let ε∈Qpm\varepsilon \in \mathbf{Q}_p^m with ord⁡p(εi)≥Ni≥0\operatorname{ord}_p(\varepsilon_i) \ge N_i \ge 0. Then

f(x+ε)−f(x)=∑i∂f∂xi(x) εi+O(p2Nmin⁡),Nmin⁡=min⁡iNi.f(x + \varepsilon) - f(x) = \sum_i \frac{\partial f}{\partial x_i}(x)\, \varepsilon_i + O(p^{2 N_{\min}}), \qquad N_{\min} = \min_i N_i.

Hence f(x+ε)∈f(x)+O(pM)f(x + \varepsilon) \in f(x) + O(p^{M}) with M=min⁡(2Nmin⁡,min⁡i(Ni+ord⁡p∂if(x)))M = \min(2N_{\min}, \min_i (N_i + \operatorname{ord}_p \partial_i f(x))), and if Ni+ord⁡p∂if(x)<2NiN_i + \operatorname{ord}_p \partial_i f(x) < 2 N_i for the index attaining the minimum, no better precision holds for all such ε\varepsilon.

Proof. Expand f(x+ε)f(x + \varepsilon) by Taylor's formula: the coefficients of the terms of degree d≥2d \ge 2 in ε\varepsilon are values at xx of divided derivatives ∂αf/α!\partial^\alpha f / \alpha!, which are polynomials with coefficients in Zp\mathbf{Z}_p, so these terms have valuation at least dNmin⁡≥2Nmin⁡d N_{\min} \ge 2 N_{\min}. For the last claim take ε=pNiei\varepsilon = p^{N_i} e_i: the change is pNi∂if(x)p^{N_i}\partial_i f(x) plus terms of valuation at least 2Ni2N_i.

For a matrix function the partial derivatives are entries of a matrix: ddet⁡(A)=tr⁡(adj⁡(A) dA)d \det(A) = \operatorname{tr}(\operatorname{adj}(A)\, dA), and dχA=−tr⁡(adj⁡(xI−A) dA)d \chi_A = -\operatorname{tr}(\operatorname{adj}(xI - A)\, dA) for the characteristic polynomial χA(x)=det⁡(xI−A)\chi_A(x) = \det(xI - A). Non-integral inputs are reduced to integral ones by scaling with a power of pp. The quantity min⁡i(Ni+ord⁡p∂if)\min_i (N_i + \operatorname{ord}_p \partial_i f) is the optimal precision below.

Determinants and full pivoting#

Theorem 2 (Full pivoting attains the optimal precision). Let A∈Mn(Zp)A \in M_n(\mathbf{Z}_p) be known to O(pN)O(p^N) entrywise, with elementary divisors pa1,…,panp^{a_1}, \ldots, p^{a_n}, a1≤⋯≤an<Na_1 \le \cdots \le a_n < N. Gaussian elimination in ball arithmetic in which each pivot has minimal valuation in the remaining submatrix (full pivoting on valuations) produces pivots of valuations a1,…,ana_1, \ldots, a_n and returns det⁡A\det A to precision N+a1+⋯+an−1N + a_1 + \cdots + a_{n-1}, which is the optimal precision.

Proof. Let dd be the pivot of the current submatrix SS, of minimal valuation aa, and let yy and zz be entries of its row and column. An entry xx becomes x−yz/dx - yz/d, and all four inputs are known to absolute precision NN. By Proposition 1 of Precision and representation of p-adic numbers the error terms of the update come from xx (precision NN), from yy and zz (N+ord⁡p(z)−a≥NN + \operatorname{ord}_p(z) - a \ge N and likewise), and from dd (N+ord⁡p(y)+ord⁡p(z)−2a≥NN + \operatorname{ord}_p(y) + \operatorname{ord}_p(z) - 2a \ge N), so every Schur complement is again known to precision NN. Elimination is a sequence of row and column operations in GLn(Zp)\mathrm{GL}_n(\mathbf{Z}_p) followed by the removal of a pivot, so the elementary divisors of the Schur complement are pa2,…,panp^{a_2}, \ldots, p^{a_n}; since the minimal valuation of the entries of a matrix is its smallest elementary divisor, and an<Na_n < N makes the valuations visible at precision NN, the pivots have the valuations aka_k. The determinant is the product of the pivots up to sign; a product of balls of absolute precision NN and valuations aka_k has precision min⁡k(N+∑l≠kal)=N+a1+⋯+an−1\min_k (N + \sum_{l \ne k} a_l) = N + a_1 + \cdots + a_{n-1}. Finally, with A=PDQA = P D Q for P,Q∈GLn(Zp)P, Q \in \mathrm{GL}_n(\mathbf{Z}_p) and DD diagonal, adj⁡(A)=adj⁡(Q)adj⁡(D)adj⁡(P)\operatorname{adj}(A) = \operatorname{adj}(Q) \operatorname{adj}(D) \operatorname{adj}(P), whose minimal valuation is that of adj⁡(D)\operatorname{adj}(D), namely a1+⋯+an−1a_1 + \cdots + a_{n-1}. This is the optimal precision of Lemma 1, attained since an<Na_n < N.

Other algorithms lose precision. A division-free algorithm (Berkowitz's algorithm, cofactor expansion) computes only sums of products of entries, each known to O(pN)O(p^N), so it can guarantee little more than precision NN, losing up to ord⁡p(det⁡A)−an\operatorname{ord}_p(\det A) - a_n digits. Elimination that takes the first nonzero entry of a column as pivot loses digits whenever that entry is not of minimal valuation, which for small pp happens often even for random matrices. Choosing the entry of minimal valuation in the pivot column (partial pivoting) is optimal for matrices that are invertible modulo pp, where all pivots are units, and nearly optimal in general. Average digits lost against the optimal precision for p=7p = 7, entries known to O(720)O(7^{20}), and matrices Udiag⁡(7ai)VU \operatorname{diag}(7^{a_i}) V with random ai≤6a_i \le 6:

nnBerkowitzfirst nonzero pivotcolumn pivotingfull pivoting
12282.70.60
4011510.31.20
60173160.70

Blocked full pivoting#

Full pivoting needs the updated Schur complement before each pivot is chosen, which rules out the blocked algorithms that make LU decomposition fast. However, only valuations matter for the choice of pivots, and these can be read off modulo pp:

Algorithm 1: Elimination by valuation levels

Input: a square matrix SS over Qp\mathbf{Q}_p.

Output: det⁡S\det S.

  1. If SS has no nonzero entry, return the determinant of the block of zeros (bounded by the precisions). Otherwise let aa be the minimal valuation of the entries of SS.
  2. Compute a rank-revealing LU decomposition of T=(S/pa) mod pT = (S/p^a) \bmod p over Fp\mathbf{F}_p; it selects rr rows and columns such that the corresponding submatrix of TT is invertible. Permute them to the front: S=(S11S12S21S22)S = \begin{pmatrix} S_{11} & S_{12} \\ S_{21} & S_{22} \end{pmatrix} with S11=paUS_{11} = p^a U, U∈GLr(Zp)U \in \mathrm{GL}_r(\mathbf{Z}_p).
  3. Compute det⁡S11\det S_{11} from an LU decomposition of S11S_{11} (all of whose pivots have valuation aa), and the Schur complement S′=S22−S21(S11−1S12)S' = S_{22} - S_{21} (S_{11}^{-1} S_{12}) by triangular solves and one matrix product.
  4. Return ±det⁡(S11)det⁡(S′)\pm \det(S_{11}) \det(S'), with det⁡S′\det S' computed recursively.

The entries of S′S' have valuation at least a+1a + 1, because TT has rank rr and S′/pa≡T22−T21T11−1T12≡0(modp)S'/p^a \equiv T_{22} - T_{21} T_{11}^{-1} T_{12} \equiv 0 \pmod p. The rr pivots of a level have the same valuation aa, which is minimal in every intermediate Schur complement, so the computation is a full pivoting elimination carried out in blocks, with the same precision. A matrix AA such that A/paA/p^a is invertible modulo pp for the minimal valuation aa of its entries (in particular a matrix invertible modulo pp) needs a single level, that is, one blocked LU decomposition. The cost is that of the blocked LU decomposition, which uses the fast matrix products of Matrix products, plus O(n3)O(n^3) word operations modulo pp per level.

Linear systems#

For AX=BAX = B the differential is dX=A−1(dB−dA X)dX = A^{-1}(dB - dA\, X), so for entries of AA and BB known to O(pNA)O(p^{N_A}) and O(pNB)O(p^{N_B}) the optimal precision of XijX_{ij} is

min⁡l(ord⁡p(A−1)il+min⁡(NB,  NA+min⁡mord⁡pXmj)).\min_l \Big(\operatorname{ord}_p (A^{-1})_{il} + \min\big(N_B, \; N_A + \min_m \operatorname{ord}_p X_{mj}\big)\Big).

Gauss–Jordan elimination with full pivoting on valuations attains it in experiments, while LU decomposition with column pivoting followed by back substitution loses digits when AA is not invertible modulo pp (about 0.3 to 3.6 digits per entry of XX for p=2p = 2 and n=5n = 5 to 20). The difference lies in the order of operations: back substitution computes X1=S11−1(B1−S12X2)X_1 = S_{11}^{-1}(B_1 - S_{12} X_2) from the already rounded X2X_2, whose errors are correlated with those of S12S_{12} in a way the ball arithmetic cannot see, whereas Gauss–Jordan elimination eliminates the later pivots from the earlier pivot rows before any division by them.

padic_radix_mat_nonsingular_solve therefore runs Gauss–Jordan elimination by valuation levels on [A∣B][A \mid B]: at each level the pivot block of Algorithm 1 is applied to its rows (LU decomposition and triangular solves), and its columns are eliminated from all the other rows, including the pivot rows of the previous levels, by one matrix product. When A/paA/p^a is invertible modulo pp for the minimal valuation aa, all pivots have valuation aa and the LU solve with column pivoting is already optimal, so it is used directly. The cost is that of a blocked LU solve. gr_mat_nonsingular_solve and gr_mat_inv call these functions for padic_radix matrices.

Characteristic polynomials#

Write adj⁡(xI−A)=∑k=0n−1Bkxk\operatorname{adj}(xI - A) = \sum_{k=0}^{n-1} B_k x^k, so that Bn−1=IB_{n-1} = I and Bk−1=ABk+ckIB_{k-1} = A B_k + c_k I for χA=∑ckxk\chi_A = \sum c_k x^k. By Lemma 1, for an integral matrix with entries known to O(pNij)O(p^{N_{ij}}) the coefficient ckc_k is known to precision

Nk=min⁡(2Nmin⁡,  min⁡ij(Nij+ord⁡p(Bk)ji)),N_k = \min\big(2 N_{\min}, \; \min_{ij} (N_{ij} + \operatorname{ord}_p (B_k)_{ji})\big),

and to no better precision when the minimum is attained by the first-order term. The higher-order terms are bounded by 2Nmin⁡2N_{\min} because their coefficients are minors of xI−AxI - A, which are integral.

Proposition 3 (Cyclic matrices). If A∈Mn(Zp)A \in M_n(\mathbf{Z}_p) is cyclic modulo pp (its minimal polynomial modulo pp has degree nn) and all entries are known to O(pN)O(p^N), then the optimal precision of every coefficient of χA\chi_A is NN, and any division-free algorithm, such as Berkowitz's, attains it.

Proof. Bk=qk(A)B_k = q_k(A) for the monic polynomial qk=∑i>kcixi−k−1q_k = \sum_{i > k} c_i x^{i - k - 1} of degree n−k−1<nn - k - 1 < n. Since I,A,…,An−1I, A, \ldots, A^{n-1} are linearly independent modulo pp, qk(A)≢0(modp)q_k(A) \not\equiv 0 \pmod p, so some entry of BkB_k is a unit and Nk=NN_k = N. A division-free algorithm computes the coefficients from integral entries known to O(pN)O(p^N) by additions and multiplications, which preserve absolute precision NN (Proposition 1 of Precision and representation of p-adic numbers).

Matrices that are not cyclic modulo pp are where precision is lost: for Udiag⁡(pai)VU \operatorname{diag}(p^{a_i}) V with ai≤5a_i \le 5 and n=16n = 16, Berkowitz's algorithm loses 35 digits on the constant coefficient and 11 digits on average, and the division-based algorithms (Hessenberg reduction, Danilevsky's algorithm, see Characteristic and minimal polynomials) lose a few digits on every coefficient, even for generic matrices. padic_radix_mat_charpoly therefore uses a division-free algorithm only for matrices that are integral, of uniform precision NN and cyclic modulo pp (tested by the rank of a random Krylov basis over Fp\mathbf{F}_p). Even there it first runs the Hessenberg reduction, which costs O(n3)O(n^3) operations instead of O(n4)O(n^4) and loses nothing on matrices that are generic modulo pp: when all its coefficients come out with precision NN, they are optimal by Proposition 3, and only otherwise is Berkowitz's algorithm run (at 10410^4 digits and n=30n = 30 this is about 12 times faster for random matrices). Other matrices get the optimal precision by computing on an exact lift:

Algorithm 2: Characteristic polynomial with optimal precision

Input: AA with entries known to O(pNij)O(p^{N_{ij}}).

Output: χA\chi_A, each coefficient to precision NkN_k.

  1. Scale AA by a power of pp to make all entries and error terms integral, and let M∈Mn(Z)M \in M_n(\mathbb{Z}) be the exact lift of the entries.
  2. Compute χM mod p2Nmin⁡\chi_M \bmod p^{2N_{\min}}: run the Hessenberg reduction with pivots of minimal valuation (similarity transformations in GLn(Zp)\mathrm{GL}_n(\mathbf{Z}_p), O(n3)O(n^3) operations) in ball arithmetic on MM known to O(pW)O(p^{W}) for some W>2Nmin⁡W > 2N_{\min}; if a coefficient comes out with precision below 2Nmin⁡2N_{\min}, increase WW, and as a last resort use Berkowitz's algorithm, which loses nothing.
  3. Compute Bn−1,…,B0B_{n-1}, \ldots, B_0 by the recurrence modulo one word (the largest power of pp below 2642^{64}), which gives ord⁡p(Bk)ji\operatorname{ord}_p (B_k)_{ji} when it is smaller; repeat modulo pNmin⁡p^{N_{\min}} only if a valuation at that bound determines some NkN_k. These are nn matrix products over Z/pKZ\mathbb{Z}/p^K\mathbb{Z} (nmod_mat or mpn_mod_mat).
  4. Return the coefficients of χM\chi_M with the precisions NkN_k, scaled back.

Where the bound 2Nmin⁡2N_{\min} for the higher-order terms limits the result, Danilevsky's algorithm in ball arithmetic is also run, and the more precise of the two valid results is kept for each coefficient. In the experiments this gives the optimal precision up to a fraction of a digit on average for p=2,7p = 2, 7 and 10091009. An exact matrix gives the exact characteristic polynomial (computed by fmpz_mat_charpoly), rounded to the context precision. The cost is about that of Berkowitz's algorithm for cyclic matrices and 1.5 to 5 times more for structured ones.

The lift works with K=Nmin⁡+sK = N_{\min} + s digits, where psp^s is the scaling, and with entries of MM of any size. Entries of high valuation are harmless, since everything is computed modulo at most p4K+64p^{4K + 64} and MM can be reduced modulo it. A large KK is not: one entry of valuation −106-10^6 among entries known to 20 digits gives K≈106K \approx 10^6, and the lift then works at a million digits for results that carry a few dozen. The lift gives up when KK exceeds 64(R+n)+102464(R + n) + 1024 for the largest relative precision RR of an inexact entry (and for exact matrices whose entries are dominated by powers of pp or exceed what the context keeps), and Berkowitz's algorithm in ball arithmetic, whose cost follows the precisions of the entries, is used instead.

Use in FLINT#

Referenced from#

References#

Cite this page

The FLINT team, “Optimal precision in p-adic linear algebra,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/p-adic/linear-algebra.html (generated 2026-11-02).