Skip to content

Radix p-adic numbers - #2719

Merged
fredrik-johansson merged 14 commits into
flintlib:mainfrom
fredrik-johansson:padic
Jun 17, 2026
Merged

fredrik-johansson merged 14 commits into
flintlib:mainfrom
fredrik-johansson:padic

Conversation

@fredrik-johansson

Copy link
Copy Markdown
Collaborator

This PR (co-written with Claude Opus) implements $p$-adic numbers and fields for word-size $p$ in the radix representation. Unlike the existing padic module, radix_padic elements track error bounds, i.e. we implement $p$-adic ball arithmetic, and this works out of the box with GR generics:

>>> from flint2 import *
>>> Q7 = Qp_radix_padic(7, rel_prec=100)
>>> M = Mat(Q7, 100, 100)().hilbert()
>>> M.det()
(45682083872693503927050627998564591825763040376697649165447021164130553995076081) * 7^-196 + O(7^-100)
>>> (M * M.inv()).det()
(1) + O(7^98)

Other advantages of radix_padic:

  • The user can specify both absolute and relative precision.
  • Allows an exact-only mode, i.e. exact arithmetic in $\mathbb{Z}[1/p]$.
  • We support signed units. This is optional and controlled by the context object. Currently only units of exact elements can be signed; inexact balls always convert to unsigned residues (this may be changed in the future, or both modes may be supported).
  • Faster high-precision multiplication and division than padic (see benchmarks below), and there is no need to precompute huge powers of $p$ in the context object.
  • Faster digit-level manipulation.

Other differences:

  • In padic the output precision is controlled by the variable (MPFR-style); in radix_padic the default precision is set in the context object. TODO: add arithmetic functions that take precision parameters as input (Arb-style).

Known issues:

  • Not yet optimized for single-limb precision. Actually @r-mb is working on a specialized single-limb $p$-adic implementation which will be preferable for low precision, but we can optimize radix_padic a bit for this use case too.
  • Various other things are not yet fully optimized.
  • This will currently be slower than padic_poly and padic_mat for most things as there is no fast polynomial multiplication and matrix multiplication yet.
  • Lacks many important methods, e.g. for conversions.
  • In degenerate situations, some operations currently take time proportional to the absolute valuation or absolute error even when the relative precision is small.
  • Precision semantics (other than the enclosure property) are inadequately tested.
  • Code has some AI-isms that need to be cleaned up.

The PR mostly consists of wrapper code around existing radix algorithms, but also implements block-basecase and Karp-Markstein $p$-adic division.

Output of radix/profile/p-padic below comparing padic and radix_padic operations; x and y are full-precision 7-adic numbers (with big-O error bounds attached in the radix_padic case).

precision 7^n, limb radix = 7^22

x + y  (same valuation)
       n      padic     radix_padic    speedup
      10     1.48e-08      2.30e-08      0.64x
      20     1.67e-08      2.90e-08      0.58x
      40     4.50e-08      2.36e-08      1.91x
      80     6.96e-08      2.50e-08      2.78x
     160     8.23e-08      2.79e-08      2.95x
     320     6.66e-08      3.29e-08      2.02x
     640     1.24e-07      4.72e-08      2.63x
    1280     1.68e-07      7.51e-08      2.24x
    2560     1.64e-07      1.32e-07      1.24x
    5120     8.35e-07      4.41e-07      1.89x
   10240     7.93e-07      4.83e-07      1.64x
   20480     1.60e-06      9.25e-07      1.73x
   40960     3.21e-06      1.85e-06      1.74x
   81920     4.12e-06      3.67e-06      1.12x
  163840     8.21e-06      7.31e-06      1.12x
  327680     2.59e-05      1.46e-05      1.77x
  655360     5.23e-05      2.96e-05      1.77x
 1310720     6.87e-05      5.95e-05      1.15x
 2621440     2.06e-04      1.19e-04      1.73x
 5242880     9.74e-04      4.30e-04      2.27x
10485760     2.26e-03      8.67e-04      2.61x

x + y  (delta val = 3)
       n      padic     radix_padic    speedup
      10     1.40e-08      4.44e-08      0.32x
      20     1.40e-08      4.60e-08      0.30x
      40     5.06e-08      4.62e-08      1.10x
      80     7.49e-08      4.92e-08      1.52x
     160     7.96e-08      5.68e-08      1.40x
     320     5.85e-08      6.97e-08      0.84x
     640     1.02e-07      9.67e-08      1.05x
    1280     1.40e-07      1.51e-07      0.93x
    2560     2.03e-07      2.65e-07      0.77x
    5120     1.92e-07      4.94e-07      0.39x
   10240     6.39e-07      9.29e-07      0.69x
   20480     1.25e-06      1.85e-06      0.68x
   40960     1.43e-06      3.69e-06      0.39x
   81920     5.06e-06      7.33e-06      0.69x
  163840     5.64e-06      1.45e-05      0.39x
  327680     2.08e-05      2.99e-05      0.70x
  655360     2.34e-05      5.98e-05      0.39x
 1310720     8.36e-05      1.36e-04      0.61x
 2621440     1.65e-04      2.38e-04      0.69x
 5242880     3.36e-04      4.72e-04      0.71x
10485760     4.43e-04      9.83e-04      0.45x

x + y  (delta val = n / 2)
       n      padic     radix_padic    speedup
      10     1.41e-08      4.37e-08      0.32x
      20     1.43e-08      4.49e-08      0.32x
      40     5.03e-08      4.68e-08      1.07x
      80     9.56e-08      5.21e-08      1.83x
     160     1.43e-07      5.86e-08      2.44x
     320     1.20e-07      7.38e-08      1.63x
     640     2.83e-07      9.74e-08      2.91x
    1280     5.15e-07      1.50e-07      3.43x
    2560     1.36e-06      2.87e-07      4.74x
    5120     4.08e-06      5.05e-07      8.08x
   10240     1.16e-05      9.44e-07     12.29x
   20480     3.13e-05      1.89e-06     16.56x
   40960     6.94e-05      3.79e-06     18.31x
   81920     1.57e-04      7.39e-06     21.24x
  163840     3.65e-04      1.48e-05     24.66x
  327680     8.58e-04      3.08e-05     27.86x
  655360     1.91e-03      5.97e-05     31.99x
 1310720     4.14e-03      1.19e-04     34.79x
 2621440     8.77e-03      2.40e-04     36.54x
 5242880     1.96e-02      4.91e-04     39.92x
10485760     4.27e-02      1.11e-03     38.47x

x * y
       n      padic     radix_padic    speedup
      10     8.16e-09      2.52e-08      0.32x
      20     2.98e-08      2.52e-08      1.18x
      40     4.57e-08      3.13e-08      1.46x
      80     8.25e-08      5.06e-08      1.63x
     160     1.49e-07      8.95e-08      1.66x
     320     3.11e-07      2.43e-07      1.28x
     640     9.22e-07      6.08e-07      1.52x
    1280     2.80e-06      1.71e-06      1.64x
    2560     8.63e-06      5.75e-06      1.50x
    5120     2.61e-05      1.14e-05      2.29x
   10240     7.54e-05      2.16e-05      3.49x
   20480     1.99e-04      4.36e-05      4.56x
   40960     4.69e-04      8.61e-05      5.45x
   81920     1.02e-03      1.83e-04      5.57x
  163840     2.07e-03      3.87e-04      5.35x
  327680     4.27e-03      8.01e-04      5.33x
  655360     8.78e-03      1.66e-03      5.29x
 1310720     1.82e-02      3.47e-03      5.24x
 2621440     3.84e-02      7.33e-03      5.24x
 5242880     8.39e-02      1.61e-02      5.21x
10485760     2.03e-01      3.61e-02      5.62x

x * 100
       n      padic     radix_padic    speedup
      10     8.20e-09      2.57e-08      0.32x
      20     8.20e-09      2.54e-08      0.32x
      40     4.15e-08      2.99e-08      1.39x
      80     6.25e-08      3.67e-08      1.70x
     160     6.43e-08      5.34e-08      1.20x
     320     7.07e-08      8.19e-08      0.86x
     640     8.50e-08      1.47e-07      0.58x
    1280     1.14e-07      2.72e-07      0.42x
    2560     1.77e-07      5.23e-07      0.34x
    5120     3.06e-07      1.02e-06      0.30x
   10240     5.62e-07      2.03e-06      0.28x
   20480     1.11e-06      4.10e-06      0.27x
   40960     2.29e-06      8.05e-06      0.28x
   81920     4.52e-06      1.61e-05      0.28x
  163840     9.32e-06      3.21e-05      0.29x
  327680     1.84e-05      6.43e-05      0.29x
  655360     3.72e-05      1.29e-04      0.29x
 1310720     7.38e-05      2.59e-04      0.28x
 2621440     1.46e-04      5.16e-04      0.28x
 5242880     2.97e-04      1.03e-03      0.29x
10485760     8.08e-04      2.06e-03      0.39x

inv(y)
       n      padic     radix_padic    speedup
      10     1.26e-07      4.68e-08      2.69x
      20     1.92e-07      4.68e-08      4.10x
      40     3.84e-07      6.83e-08      5.62x
      80     6.20e-07      1.40e-07      4.43x
     160     1.00e-06      2.34e-07      4.27x
     320     1.50e-06      4.00e-07      3.75x
     640     3.07e-06      1.20e-06      2.56x
    1280     7.70e-06      2.46e-06      3.13x
    2560     2.06e-05      6.90e-06      2.99x
    5120     5.85e-05      2.23e-05      2.62x
   10240     1.68e-04      4.99e-05      3.37x
   20480     4.65e-04      1.02e-04      4.56x
   40960     1.26e-03      2.07e-04      6.09x
   81920     2.96e-03      4.26e-04      6.95x
  163840     6.34e-03      8.91e-04      7.12x
  327680     1.34e-02      1.84e-03      7.28x
  655360     2.74e-02      3.80e-03      7.21x
 1310720     5.65e-02      7.90e-03      7.15x
 2621440     1.18e-01      1.65e-02      7.15x
 5242880     2.49e-01      3.57e-02      6.97x
10485760     5.44e-01      7.82e-02      6.96x

inv(100)
       n      padic     radix_padic    speedup
      10     1.27e-07      4.72e-08      2.69x
      20     2.00e-07      4.69e-08      4.26x
      40     3.11e-07      6.54e-08      4.76x
      80     4.84e-07      1.01e-07      4.79x
     160     6.35e-07      1.39e-07      4.57x
     320     8.37e-07      1.99e-07      4.21x
     640     1.20e-06      2.98e-07      4.03x
    1280     1.94e-06      4.29e-07      4.52x
    2560     4.55e-06      7.16e-07      6.35x
    5120     1.03e-05      1.24e-06      8.31x
   10240     2.53e-05      2.30e-06     11.00x
   20480     6.78e-05      4.36e-06     15.55x
   40960     1.60e-04      8.50e-06     18.82x
   81920     3.58e-04      1.67e-05     21.44x
  163840     8.03e-04      3.32e-05     24.19x
  327680     1.68e-03      6.62e-05     25.38x
  655360     3.45e-03      1.32e-04     26.14x
 1310720     7.08e-03      2.64e-04     26.82x
 2621440     1.46e-02      5.28e-04     27.65x
 5242880     3.04e-02      1.06e-03     28.68x
10485760     6.53e-02      2.12e-03     30.80x

x / y
       n      padic     radix_padic    speedup
      10     1.44e-07      7.27e-08      1.98x
      20     2.33e-07      7.25e-08      3.21x
      40     4.39e-07      1.09e-07      4.03x
      80     7.01e-07      1.69e-07      4.15x
     160     1.10e-06      3.04e-07      3.62x
     320     1.85e-06      5.08e-07      3.64x
     640     4.01e-06      1.27e-06      3.16x
    1280     1.07e-05      3.14e-06      3.41x
    2560     2.97e-05      8.74e-06      3.40x
    5120     8.52e-05      2.84e-05      3.00x
   10240     2.44e-04      6.20e-05      3.94x
   20480     6.67e-04      1.25e-04      5.34x
   40960     1.80e-03      2.58e-04      6.98x
   81920     4.02e-03      5.22e-04      7.70x
  163840     8.49e-03      1.09e-03      7.79x
  327680     1.76e-02      2.25e-03      7.82x
  655360     3.63e-02      4.65e-03      7.81x
 1310720     7.55e-02      9.66e-03      7.82x
 2621440     1.57e-01      2.03e-02      7.73x
 5242880     3.35e-01      4.36e-02      7.68x
10485760     7.14e-01      9.56e-02      7.47x

x / 100
       n      padic     radix_padic    speedup
      10     1.42e-07      7.30e-08      1.95x
      20     1.88e-07      7.29e-08      2.58x
      40     3.85e-07      8.31e-08      4.63x
      80     5.99e-07      1.02e-07      5.87x
     160     8.12e-07      1.41e-07      5.76x
     320     1.23e-06      2.11e-07      5.83x
     640     2.26e-06      3.63e-07      6.23x
    1280     4.90e-06      6.54e-07      7.49x
    2560     1.34e-05      1.23e-06     10.89x
    5120     3.68e-05      2.43e-06     15.14x
   10240     1.02e-04      4.81e-06     21.21x
   20480     2.69e-04      9.43e-06     28.53x
   40960     6.71e-04      1.88e-05     35.69x
   81920     1.42e-03      3.75e-05     37.87x
  163840     2.93e-03      7.50e-05     39.07x
  327680     5.99e-03      1.51e-04     39.67x
  655360     1.23e-02      3.00e-04     41.00x
 1310720     2.53e-02      6.00e-04     42.17x
 2621440     5.35e-02      1.20e-03     44.58x
 5242880     1.14e-01      2.40e-03     47.50x
10485760     2.44e-01      4.79e-03     50.94x

The only operations that are slower are:

  • Single-limb addition and multiplication (probably improvable by special-casing these; see comments above)
  • Addition with different valuations (should be fixable with combined shift-and-add low-level routines)
  • Multiplication of a high-precision number by a small (e.g. single-word) scalar. This can probably be improved too, but may be much harder than the others to optimize.

PR marked as draft as this needs review, code cleanup, and work on the interfaces.

@thofma @fieker @r-mb

@fredrik-johansson

Copy link
Copy Markdown
Collaborator Author

Added square root functions. Lots of things to tighten up, but the asymptotic speed is good.

p = 7

sqrt(y)
       n      padic     radix_padic    speedup
      10     1.61e-07      1.46e-07      1.10x
      20     3.21e-07      1.44e-07      2.23x
      40     5.14e-07      1.74e-07      2.95x
      80     7.80e-07      2.56e-07      3.05x
     160     1.18e-06      4.36e-07      2.71x
     320     2.05e-06      7.37e-07      2.78x
     640     4.47e-06      1.62e-06      2.76x
    1280     1.18e-05      3.67e-06      3.22x
    2560     3.20e-05      9.37e-06      3.42x
    5120     8.97e-05      2.94e-05      3.05x
   10240     2.61e-04      6.89e-05      3.79x
   20480     6.99e-04      1.43e-04      4.89x
   40960     1.81e-03      2.96e-04      6.11x
   81920     4.17e-03      6.06e-04      6.88x
  163840     8.90e-03      1.27e-03      7.01x
  327680     1.87e-02      2.62e-03      7.14x
  655360     3.87e-02      5.41e-03      7.15x
 1310720     8.04e-02      1.12e-02      7.18x
 2621440     1.69e-01      2.32e-02      7.28x
 5242880     3.57e-01      4.90e-02      7.29x
10485760     7.85e-01      1.05e-01      7.48x

sqrt(100)
       n      padic     radix_padic    speedup
      10     1.69e-07      1.51e-07      1.12x
      20     2.71e-07      1.49e-07      1.82x
      40     4.59e-07      1.79e-07      2.56x
      80     6.78e-07      2.54e-07      2.67x
     160     9.44e-07      3.96e-07      2.38x
     320     1.37e-06      6.38e-07      2.15x
     640     2.29e-06      1.26e-06      1.82x
    1280     5.17e-06      2.63e-06      1.97x
    2560     1.28e-05      6.19e-06      2.07x
    5120     3.42e-05      1.75e-05      1.95x
   10240     9.42e-05      4.00e-05      2.35x
   20480     2.63e-04      8.48e-05      3.10x
   40960     6.66e-04      1.75e-04      3.81x
   81920     1.59e-03      3.57e-04      4.45x
  163840     3.36e-03      7.38e-04      4.55x
  327680     6.93e-03      1.52e-03      4.56x
  655360     1.42e-02      3.13e-03      4.54x
 1310720     2.95e-02      6.69e-03      4.41x
 2621440     6.27e-02      1.35e-02      4.64x
 5242880     1.30e-01      2.79e-02      4.66x
10485760     2.75e-01      5.97e-02      4.61x

@fredrik-johansson

Copy link
Copy Markdown
Collaborator Author

Now moved to its own module.

I'm still undecided about whether this should actually be named radix_padic, to go along with a future nmod_padic, or whether it should be padic_radix / padic_nmod.

The latter would match the fq_* naming convention.

Or maybe we should use the shorter qp_* for a new generation of p-adic modules.

@fredrik-johansson

fredrik-johansson commented Jun 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

There is now decent dot product code with delayed limb reduction.

Some timings for FLINT (radix p-adics) vs p-adics in Sage and Nemo. p = 7, precision of prec digits (the precision models are not identical across the systems, but should be roughly comparable for this benchmark).

Matrix multiplication: M * M2 where M = nxn Hilbert matrix, M2 = M+1.

 prec |   n |        Sage |        Nemo |       FLINT | Sage/FLINT | Nemo/FLINT
-------------------------------------------------------------------------------
   10 |  10 |    0.000292 |    0.000078 |    0.000124 |      2.35x |      0.63x
   10 |  30 |    0.007773 |    0.001456 |    0.001651 |      4.71x |      0.88x
   10 | 100 |    0.297975 |    0.056114 |    0.013823 |     21.56x |      4.06x
   10 | 300 |    8.341158 |    1.583230 |    0.314119 |     26.55x |      5.04x
  100 |  10 |    0.000420 |    0.000325 |    0.000085 |      4.94x |      3.82x
  100 |  30 |    0.012003 |    0.008699 |    0.001297 |      9.25x |      6.71x
  100 | 100 |    0.480267 |    0.325987 |    0.034900 |     13.76x |      9.34x
  100 | 300 |   13.594377 |   11.700377 |    0.852103 |     15.95x |     13.73x
 1000 |  10 |    0.002754 |    0.002847 |    0.000784 |      3.51x |      3.63x
 1000 |  30 |    0.081803 |    0.082550 |    0.020073 |      4.08x |      4.11x
 1000 | 100 |    3.143398 |    3.108869 |    0.717659 |      4.38x |      4.33x
 1000 | 300 |   88.522816 |   87.782684 |   19.675000 |      4.50x |      4.46x
10000 |  10 |    0.084507 |    0.104399 |    0.019801 |      4.27x |      5.27x
10000 |  30 |    2.504386 |    3.115179 |    0.592198 |      4.23x |      5.26x
10000 | 100 |   97.347908 |  117.251128 |   23.352778 |      4.17x |      5.02x

Determinant: det(M) where M = nxn Hilbert matrix

 prec |   n |        Sage |        Nemo |       FLINT | Sage/FLINT | Nemo/FLINT
-------------------------------------------------------------------------------
   10 |  10 |    0.000318 |    0.000133 |    0.000095 |      3.35x |      1.40x
   10 |  30 |    0.004541 |    0.001043 |    0.000579 |      7.84x |      1.80x
   10 | 100 |    0.125386 |    0.076648 |    0.013297 |      9.43x |      5.76x
   10 | 300 |    3.479442 |    1.071934 |    0.225207 |     15.45x |      4.76x
  100 |  10 |    0.000402 |    0.000846 |    0.000057 |      7.05x |     14.84x
  100 |  30 |    0.006124 |    0.006071 |    0.001004 |      6.10x |      6.05x
  100 | 100 |    0.186187 |    0.217974 |    0.025577 |      7.28x |      8.52x
  100 | 300 |    4.750252 |    6.090669 |    0.506611 |      9.38x |     12.02x
 1000 |  10 |    0.001291 |    0.019146 |    0.000363 |      3.56x |     52.74x
 1000 |  30 |    0.029726 |    0.069803 |    0.009134 |      3.25x |      7.64x
 1000 | 100 |    1.024461 |    2.689935 |    0.290966 |      3.52x |      9.24x
 1000 | 300 |   28.976581 |   78.727166 |    7.239035 |      4.00x |     10.88x
10000 |  10 |    0.033156 |    0.088356 |    0.006241 |      5.31x |     14.16x
10000 |  30 |    0.884207 |    2.773256 |    0.190726 |      4.64x |     14.54x
10000 | 100 |   31.650675 |  102.790328 |    7.773456 |      4.07x |     13.22x

This should be even better once there is actual fast matrix multiplication code.

@fredrik-johansson
fredrik-johansson marked this pull request as ready for review June 16, 2026 09:48
@fredrik-johansson

Copy link
Copy Markdown
Collaborator Author

Renamed to padic_radix which seems to be the most popular option.

@fredrik-johansson
fredrik-johansson merged commit d2b758a into flintlib:main Jun 17, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant