ZpL: a p-adic Precision Package

Xavier Caruso, David Roe, Tristan Vaccon

Abstract

We present a new package, ZpL, for the SageMath mathematical software system. It implements a "sharp" tracking of precision on $p$-adic numbers based on the theory of ultrametric precision. Unlike traditional interval arithmetic, ZpL uses automatic differentiation to track the joint precision of multiple variables as a lattice. This approach prevents the catastrophic precision loss common in numerically unstable algorithms. We discuss the underlying algorithms, their complexity, and design choices, illustrating the package's benefits through benchmarks in linear algebra, commutative algebra, and differential equations.

The Precision Problem in $p$-adic Arithmetic

When working with $p$-adic numbers—essential tools in number theory and algebraic geometry—exact representation is impossible due to their infinite expansions. Consequently, software must truncate these numbers, leading to precision errors.

Traditionally, software handles this using interval arithmetic, where each number carries its own error radius. However, this approach is often overly pessimistic because it assumes errors across different variables are independent. In reality, mathematical operations create correlations. For instance, subtracting two numbers with a shared "noisy" history might cancel out their errors. Standard interval arithmetic misses these correlations, leading to a "precision explosion" where results quickly lose all significant digits, even when the true result is well-defined.

Methods: Ultrametric Precision and Automatic Differentiation

The ZpL package implements ultrametric precision. Instead of tracking precision coordinate-wise (like a box around a point), it tracks the joint precision of all active variables as a lattice (the $p$-adic equivalent of an ellipsoid).

Key Technical Components

  • Automatic Differentiation (AD): When a new variable is created via a function $f(x, y)$, ZpL calculates the Jacobian (the matrix of partial derivatives). This Jacobian is used to transform the current precision lattice into a new one for the updated variable set.
  • Matrix Management: The precision state is stored as a matrix in row-echelon form. This representation allows the system to identify "diffused digits"—cases where a specific linear combination of variables is known more accurately than the individual variables themselves.
The precision lattice is maintained as a matrix in row-echelon form, where pivots (black squares) define the precision of individual variables or linear combinations.
  • Temporal Locality: While updating a global precision matrix might seem computationally expensive, the authors observed that most variables are deleted shortly after creation (e.g., temporary variables in a loop). ZpL optimizes for this by performing efficient row operations to maintain the matrix form when columns are removed.
Updating the precision matrix when a variable is deleted involves removing a column and restoring row-echelon form to maintain computational efficiency.

Implementation Flavors

  1. ZpLC (Cap): A version that introduces an absolute precision bound ("cap") to ensure the lattice remains mathematically sound and manageable.
  2. ZpLF (Float): A faster version using floating-point $p$-adic numbers to represent the lattice, allowing for arbitrary codimension (where some variables are known perfectly while others are fuzzy).

Key Results and Benchmarks

ZpL was tested against standard algorithms known for numerical instability:

  • SOMOS-4 Sequences: In a test of 100 iterations, the standard SageMath tracker (ZpCR) failed with a PrecisionError. ZpL successfully returned a result with significant digits.
  • Matrix Multiplication: After 25 consecutive matrix multiplications, standard methods could not determine if the result was zero. ZpL maintained enough precision to certify a non-zero result.
  • Characteristic Polynomials: For a $3 \times 3$ matrix, ZpL recovered four more digits of precision on the $x$ coefficient than standard methods and correctly identified the constant term (the determinant).
  • GCD Stability: In polynomial GCD computations, ZpL prevents premature termination by providing sharper bounds, allowing the algorithm to correctly identify when a remainder is truly zero.

Limitations and Future Work

The primary trade-off for this precision is computational overhead. Tracking a global lattice is significantly slower than tracking independent intervals, as every basic arithmetic operation requires matrix-vector products or row updates.

Consequently, ZpL is intended as a development and diagnostic tool rather than a high-performance library. It helps researchers identify numerical instability in their algorithms and design more stable alternatives. Future work will focus on integrating these lattice techniques more deeply into the SageMath core to reduce the overhead of the current "wrapper" implementation.

Summary updated

BibTeX
@inproceedings{caruso2018zpl,
  author = {Xavier Caruso and David Roe and Tristan Vaccon},
  title = {{ZpL: a p-adic Precision Package}},
  booktitle = {Issac},
  year = {2018},
  doi = {10.1145/3208976.3208995},
}
Figure