p-adic numbers

p-adic numbers

Measure a number by how often p divides it, and the integers acquire a new completion: numbers with infinitely many digits to the left. Here a p-adic number is a rule that produces as many digits as you ask for, exactly.

Valuation and the p-adic size

For a rational x≠0x \neq 0, vp(x)v_p(x) is the exponent of pp in xx and ∣x∣p=p−vp(x)|x|_p = p^{-v_p(x)}. So ∣50/27∣5=1/25|50/27|_5 = 1/25 and ∣50/27∣3=27|50/27|_3 = 27: being divisible by pp makes a number small. This size satisfies the strong triangle inequality ∣x+y∣p≤max⁡(∣x∣p,∣y∣p)|x + y|_p \le \max(|x|_p, |y|_p), and with the ordinary size it gives the product formula ∣x∣∏p∣x∣p=1|x| \prod_p |x|_p = 1.

import { padic, padicValuation, padicNorm, expansion, henselRoot, teichmuller } from "@ratmath/numtheory";

padicValuation("50/27", 5n);          // 2
String(padicNorm("50/27", 3n));       // "27"
padic("50/27", 3n).toString();        // "…00000000000000001.212"

Infinitely many digits to the left

Completing Q\mathbb Q with ∣⋅∣p|\cdot|_p gives Qp\mathbb Q_p, whose elements are ∑i≥vdipi\sum_{i \ge v} d_i p^i with digits 0≤di<p0 \le d_i < p, written right to left. In Z5\mathbb Z_5, …4444=−1\dots 4444 = -1: add 1 and every digit carries. A rational's expansion is eventually periodic, and conversely.

padic(-1, 5n).toString();      // "…44444444444444444444"
padic("1/3", 5n).toString();   // "…31313131313131313132": 3 times it is …0001
expansion("1/3", 5n);          // { valuation: 0, preperiod: [2n], period: [3n, 1n] }
padic("1/10", 5n).toString();  // "…2222222222222222222.3"

Lazy and exact

A Padic is a rule: given NN, it returns a rational rr with vp(x−r)≥Nv_p(x - r) \ge N. Rationals are exact. A sum asks its parts for precision NN; a product asks each for NN minus the other's valuation; a quotient first finds the divisor's valuation. Nothing is fixed in advance, so asking for more digits reruns the whole computation to that depth, and results are cached. What cannot be decided by digits is reported: the valuation of a number that may be zero is searched for, and the search gives up with the code "unknown-valuation" rather than claiming a zero.

Square roots and Hensel

For odd pp, a unit uu is a square in Zp\mathbb Z_p exactly when it is a square mod pp; for p=2p = 2, when u≡1(mod8)u \equiv 1 \pmod 8. Then Newton's iteration from a root mod pp converges pp-adically, doubling the correct digits each step. henselRoot does the same for any polynomial, given a start r0r_0 with v(f(r0))>2v(f′(r0))v(f(r_0)) > 2v(f'(r_0)).

const i = padic(-1, 5n).sqrt();
i.toString();                   // "…40423140223032431212": a square root of −1 in Z₅
i.mul(i).toString();            // "…44444444444444444444"
padic(-7, 2n).sqrt();           // exists: −7 ≡ 1 (mod 8)
padic(2, 5n).sqrt();            // throws "not-a-square": 2 is not a square mod 5
henselRoot([-2, 0, 0, 1], 3, 5n).toString();   // "…21134404003204132203", the cube root of 2 ≡ 3 (mod 5)

exp, log and roots of unity

The series exp⁡x=∑xn/n!\exp x = \sum x^n/n! converges when vp(x)≥1v_p(x) \ge 1 (≥2\ge 2 for p=2p = 2), since vp(n!)≤(n−1)/(p−1)v_p(n!) \le (n-1)/(p-1). The series log⁡(1+y)=y−y2/2+…\log(1 + y) = y - y^2/2 + \dots converges for vp(y)≥1v_p(y) \ge 1, and extends to every unit by log⁡u=log⁡(up−1)/(p−1)\log u = \log(u^{p-1})/(p-1); on the domain of exp the two are inverse. The Teichmüller representative ω(a)=lim⁡apn\omega(a) = \lim a^{p^n} is the (p−1)(p-1)-th root of unity congruent to aa: in Z5\mathbb Z_5, ω(2)\omega(2) is the square root of −1 above.

padic(5, 5n).exp().log().toString();    // "…000000000000000000010": back to 5
teichmuller(2n, 5n).toString();         // "…40423140223032431212"
teichmuller(2n, 5n).pow(4).toString();  // "…00000000000000000001"
Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs