# ZpL: a p-adic precision package¶

### Legend¶

• ZpCR stands for Capped Relative precision: this is roughly the analogue of interval arithmetic (a $p$-adic number is represented by a subset of the shape $x + O(p^n)$ and precision is tracked by operating of these subsets)
• ZpFP stands for Floating Point arithmetic: this is the analogue of usual floating point arithmetic (just a certain subset of $p$-adic numbers are available and results are rounded so that they always lie in this subset)
• ZpLC stands for Lattice Precision with cap (provided by our package)
• ZpLF stands for Lattice Precision combined with Floating Point arithmetics (provided by our package)

### Creation of the rings¶

In :
Z2 = Zp(2, prec=30, print_mode="digits")
Q2 = Z2.fraction_field()

In :
Z3 = Zp(3, prec=20, print_mode="digits")


## First examples¶

In :
x = random_element(Z3,5)
x

ZpCR :
0.000 s
...02112
ZpFP :
0.000 s
...00000000000000002112
ZpLC :
0.000 s
...02112
ZpLF :
0.000 s
...02112

When we multiply by $p$, we gain one digit of absolute precision:

In :
3*x

ZpCR :
0.000 s
...021120
ZpFP :
0.000 s
...000000000000000021120
ZpLC :
0.004 s
...021120
ZpLF :
0.004 s
...021120

The ZpL package sees this gain of precision even if the computation is split into several steps:

In :
x + x + x

ZpCR :
0.004 s
...21120
ZpFP :
0.000 s
...000000000000000021120
ZpLC :
0.000 s
...021120
ZpLF :
0.000 s
...021120
In :
y = 2*x
y

ZpCR :
0.000 s
...12001
ZpFP :
0.000 s
...00000000000000012001
ZpLC :
0.000 s
...12001
ZpLF :
0.000 s
...12001
In :
x + y

ZpCR :
0.000 s
...21120
ZpFP :
0.000 s
...000000000000000021120
ZpLC :
0.000 s
...021120
ZpLF :
0.000 s
...021120

The same with multiplication:

In :
x^3

ZpCR :
0.000 s
...022122
ZpFP :
0.000 s
...00000000120222022122
ZpLC :
0.000 s
...022122
ZpLF :
0.000 s
...022122
In :
x * x * x

ZpCR :
0.000 s
...22122
ZpFP :
0.000 s
...00000000120222022122
ZpLC :
0.000 s
...022122
ZpLF :
0.000 s
...022122
In :
y = x^2
y

ZpCR :
0.000 s
...00021
ZpFP :
0.000 s
...00000000000020100021
ZpLC :
0.000 s
...00021
ZpLF :
0.000 s
...00021
In :
x * y

ZpCR :
0.000 s
...22122
ZpFP :
0.000 s
...00000000120222022122
ZpLC :
0.000 s
...022122
ZpLF :
0.000 s
...022122

## Computations with numbers at different precision¶

In :
x = random_element(Z2,10)
y = random_element(Z2,5)

In :
u = x+y
u

ZpCR :
0.000 s
...10101
ZpFP :
0.000 s
...000000000000000000000111010101
ZpLC :
0.000 s
...10101
ZpLF :
0.000 s
...10101
In :
v = x-y
v

ZpCR :
0.000 s
...00111
ZpFP :
0.000 s
...000000000000000000000111000111
ZpLC :
0.000 s
...00111
ZpLF :
0.000 s
...00111

Now we compute $u + v$, which is also $2x$. Here is what we get:

In :
u+v

ZpCR :
0.000 s
...11100
ZpFP :
0.000 s
...00000000000000000000001110011100
ZpLC :
0.000 s
...01110011100
ZpLF :
0.004 s
...01110011100
In :
2*x

ZpCR :
0.000 s
...01110011100
ZpFP :
0.000 s
...00000000000000000000001110011100
ZpLC :
0.000 s
...01110011100
ZpLF :
0.000 s
...01110011100

## The SOMOS 4 sequence¶

The SOMOS 4 sequence is the sequence defined by the recurrence $$u_{n+4} = \frac{u_{n+1}u_{n+3} + u_{n+2}^2}{u_n}.$$ It turns out that its computation is highly unstable.
For this reason, using the ZpL package can here be decisive.

In :
def somos(u0, u1, u2, u3, n):
a, b, c, d = u0, u1, u2, u3
for _ in range(4, n+1):
a, b, c, d = b, c, d, (b*d + c*c) / a
return d

In :
somos(Z2(1,15), Z2(1,15), Z2(1,15), Z2(3,15), 18)

ZpCR :
0.004 s
...11
ZpFP :
0.000 s
...110111001000100100000000000111
ZpLC :
0.052 s
...100000000000111
ZpLF :
0.032 s
...100000000000111
In :
somos(Z2(1,15), Z2(1,15), Z2(1,15), Z2(3,15), 100)

ZpCR :
0.000 s
PrecisionError: cannot divide by something indistinguishable from zero.
ZpFP :
0.000 s
...110000101000011001001001110001
ZpLC :
0.364 s
...001001001110001
ZpLF :
0.228 s
...001001001110001

## Computations with matrices¶

### Product of many matrices¶

In :
MS = MatrixSpace(Z2,2)

In :
M = MS(1)
for _ in range(60):
M *= random_element(MS, prec=8)

In :
M

ZpCR :
0.000 s
[ ...010100000000000000 ...1111000000000000000]
[   ...0011000000000000   ...10010000000000000]
ZpFP :
0.000 s
[ ...00101100101000111110111100010100000000000000 ...011010011000000010010100111111000000000000000]
[   ...100110110001111110010001100011000000000000   ...0111001111111001101010101110010000000000000]
ZpLC :
0.000 s
[ ...00010100000000000000 ...111111000000000000000]
[   ...100011000000000000   ...1110010000000000000]
ZpLF :
0.000 s
[ ...00010100000000000000 ...111111000000000000000]
[   ...100011000000000000   ...1110010000000000000]

### Determinants¶

In :
D = diagonal_matrix([ Z3(1,5), Z3(9,5), Z3(27,5), Z3(81,5) ])
D

ZpCR :
0.040 s
[...00001        0        0        0]
[       0 ...00100        0        0]
[       0        0 ...01000        0]
[       0        0        0 ...10000]
ZpFP :
0.000 s
[    ...00000000000000000001                           0                           0                           0]
[                          0   ...0000000000000000000100                           0                           0]
[                          0                           0  ...00000000000000000001000                           0]
[                          0                           0                           0 ...000000000000000000010000]
ZpLC :
0.004 s
[...00001        0        0        0]
[       0 ...00100        0        0]
[       0        0 ...01000        0]
[       0        0        0 ...10000]
ZpLF :
0.000 s
[...00001        0        0        0]
[       0 ...00100        0        0]
[       0        0 ...01000        0]
[       0        0        0 ...10000]

The determinant of $D$ is known at higher precision. This is due to the particular shape of $D$.

In :
D.determinant()

ZpCR :
0.012 s
...1000000000
ZpFP :
0.004 s
...00000000000000000001000000000
ZpLC :
0.152 s
...1000000000
ZpLF :
0.056 s
...1000000000

This phenomenon is preserved when $D$ is modified by (left and right) multiplication by some matrix. However the software may or may not see it, depending on the method it uses for tracking precision

In :
MS = D.parent()
P = random_element(MS, prec=5)
Q = random_element(MS, prec=5)
M = P*D*Q
M

ZpCR :
0.000 s
[ ...00120  ...20020  ...20110 ...200100]
[ ...12000  ...21200  ...20200  ...10100]
[ ...21000  ...11100  ...22100  ...22200]
[ ...20021  ...01101  ...21002  ...20220]
ZpFP :
0.004 s
[  ...000000011020110000120   ...000000002100101020020   ...000000011002210020110  ...0000000010121011200100]
[...00000000020011211112000  ...0000000011121221221200  ...0000000021100012120200  ...0000000012221210210100]
[...00000000100010222221000  ...0000000012111001011100  ...0000000100012210022100  ...0000000021210022222200]
[   ...00000011220000220021    ...00000010010221001101    ...00000012020102121002   ...000000011100122120220]
ZpLC :
0.408 s
[ ...00120  ...20020  ...20110 ...200100]
[ ...12000  ...21200  ...20200  ...10100]
[ ...21000  ...11100  ...22100  ...22200]
[ ...20021  ...01101  ...21002  ...20220]
ZpLF :
0.428 s
[ ...00120  ...20020  ...20110 ...200100]
[ ...12000  ...21200  ...20200  ...10100]
[ ...21000  ...11100  ...22100  ...22200]
[ ...20021  ...01101  ...21002  ...20220]
In :
M.determinant()

ZpCR :
0.004 s
0
ZpFP :
0.000 s
...000000010121201222210000000000
ZpLC :
0.560 s
...10000000000
ZpLF :
0.376 s
...10000000000
In :
P.determinant() * D.determinant() * Q.determinant()

ZpCR :
0.004 s
...10000000000
ZpFP :
0.000 s
...001101010121201222210000000000
ZpLC :
0.676 s
...10000000000
ZpLF :
0.568 s
...10000000000

Notice that ZpCR cannot decide whether the determinant $M$ vanishes. This may have important consequences if, at some point, we will test if $M$ is invertible or not and jump to one branch or another one depending on the result.

In :
M.charpoly()

ZpCR :
0.004 s
(...00000000000000000001)*x^4 + (...10120)*x^3 + (...02100)*x^2 + (0)*x + (0)
ZpFP :
0.000 s
...00000000000000000001*x^4 + ...222222011120011010120*x^3 + ...0022121020011212002100*x^2 + ...00012200222121121011000000*x + ...000000010121201222210000000000
ZpLC :
0.356 s
...00000000000000000001*x^4 + ...10120*x^3 + ...02100*x^2 + ...1000000*x + ...10000000000
ZpLF :
0.360 s
...00000000000000000001*x^4 + ...10120*x^3 + ...02100*x^2 + ...1000000*x + ...10000000000

### Dodgson condensation algorithm¶

Dodgson condensation is a recursive method for computing the determinant of a matrix. A description of the method can be found here.

In :
def dodgson(M):   # M must be a square matrix
n = M.nrows() - 1
A = M
B = matrix(n, n,
[ [ M[i,j]*M[i+1,j+1] - M[i+1,j]*M[i,j+1]
for j in range(n) ] for i in range(n) ])
for m in range(n-1, 0, -1):
A, B = B, matrix(m, m,
[ [ (B[i,j]*B[i+1,j+1] - B[i+1,j]*B[i,j+1]) / A[i+1,j+1]
for j in range(m) ] for i in range(m) ])
return B[0,0]

In :
MS = MatrixSpace(Z2, 6)
M = random_element(MS, prec=10)

In :
dodgson(M)

ZpCR :
0.012 s
...1110
ZpFP :
0.008 s
...0101110010011100100110100001110
ZpLC :
1.004 s
...0100001110
ZpLF :
0.528 s
...0100001110
In :
M.determinant()

ZpCR :
0.004 s
...0100001110
ZpFP :
0.000 s
...0001110010011100100110100001110
ZpLC :
1.980 s
...0100001110
ZpLF :
1.668 s
...0100001110

## Computations with polynomials¶

In :
S.<x> = PolynomialRing(Q2)
P = random_element(S, degree=10, prec=5)
Q = random_element(S, degree=10, prec=5)
D = x^5 + random_element(S, degree=4, prec=8)
D

ZpCR :
0.004 s
(...000000000000000000000000000001)*x^5 + (...00110010)*x^4 + (...00011111)*x^3 + (...00010010)*x^2 + (...00011111)*x + (...10101000)
ZpFP :
0.000 s
...000000000000000000000000000001*x^5 + ...0000000000000000000000000110010*x^4 + ...000000000000000000000000011111*x^3 + ...0000000000000000000000000010010*x^2 + ...000000000000000000000000011111*x + ...000000000000000000000000010101000
ZpLC :
0.076 s
...000000000000000000000000000001*x^5 + ...00110010*x^4 + ...00011111*x^3 + ...00010010*x^2 + ...00011111*x + ...10101000
ZpLF :
0.080 s
...000000000000000000000000000001*x^5 + ...00110010*x^4 + ...00011111*x^3 + ...00010010*x^2 + ...00011111*x + ...10101000

With high probability, $P$ and $Q$ are coprime, implying that the gcd of $DP$ is $DQ$ is $D$.
However, we observe that we do not always get this expected result:

In :
def euclidean(A,B):
while B != 0:
A, B = B, A % B
return A.monic()

In :
euclidean(D*P, D*Q)

ZpCR :
0.004 s
(0)*x^13 + (0)*x^12 + (0)*x^11 + (...01)*x^10 + (0)*x^9 + (0)*x^8 + (0)*x^7 + (0)*x^6 + (...10)*x^5 + (0)*x^4 + (0)*x^3 + (0)*x^2 + (0)*x + (0)
ZpFP :
0.004 s
...000000000000000000000000000001
ZpLC :
4.024 s
...000000000000000000000000000001*x^5 + ...0010*x^4 + ...0011111*x^3 + ...0010*x^2 + ...1111*x + ...0101000
ZpLF :
2.812 s
...000000000000000000000000000001*x^5 + ...0010*x^4 + ...0011111*x^3 + ...0010*x^2 + ...1111*x + ...0101000

More precisely, ZpCR often found a gcd of higher degree. Indeed the Euclidean algorithm stops prematurely because the software believes that some early remainder vanishes due to the lack of precision.
The situation with ZpFP is the exact opposite: since the precision is too high (always the maximal cap), the software does not notice that the remainder vanishes. Therefore the Euclidean algorithm does not stop until it obtains a remainder of degree $0$.

### Gröbner basis¶

In :
R.<x,y,z> = PolynomialRing(Q2, order = 'invlex')
F = [Q2(2,10)*x + Q2(1,10)*z,
Q2(1,10)*x^2 + Q2(1,10)*y^2 - Q2(2,10)*z^2,
Q2(4,10)*y^2 + Q2(1,10)*y*z + Q2(8,10)*z^2]

In :
from sage.rings.polynomial.toy_buchberger import buchberger_improved
g = buchberger_improved(ideal(F)); g.sort()
g

ZpCR :
0.016 s
[x^3, x*y + ...1100010*x^2, y^2 + ...11001*x^2, z + ...0000000010*x]
ZpFP :
0.016 s
[x^3, x*y + ...1111111111111111111111111100010*x^2, y^2 + ...011111111111111111111111111001*x^2, z + ...0000000000000000000000000000010*x]
ZpLC :
1.656 s
[x^3, x*y + ...111100010*x^2, y^2 + ...1111111001*x^2, z + ...0000000010*x]
ZpLF :
1.868 s
[x^3, x*y + ...111100010*x^2, y^2 + ...1111111001*x^2, z + ...0000000010*x]

## $p$-adic differential equations¶

In :
def edp_simple(g, h, N):
S = g.parent()
u = S.gen()
for i in range(N):
u -= h(u) * (u.derivative()/h(u) - g).integral()
u = u[:2^(i+1)]
return u

In :
N = 4
S.<t> = PowerSeriesRing(Q2, 2^N)

In :
h = 1 + t + t^3
y = t + t^2 * random_element(S,10)
g = y.derivative() / h(y)

In :
u = edp_simple(g, h, N)
u

ZpCR :
0.056 s
...10011
ZpFP :
0.008 s
...011000000000000000000011110011
ZpLC :
41.623 s
...0011110011
ZpLF :
25.746 s
...0011110011