puremp 0.2.2

A pure-Rust arbitrary-precision arithmetic library — integers, rationals and MPFR-class floats — with a dependency-free clean-room core (optional serde/rand bridges), plus a C ABI and a CLI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Fixed-precision `p`-adic numbers — elements of the field `ℚ_p` (and its ring
//! of integers `ℤ_p`), carried to a bounded number of significant `p`-adic
//! digits.
//!
//! # Representation
//!
//! A nonzero value is written in the canonical form
//!
//! ```text
//! x = p^v · u
//! ```
//!
//! where `v = v_p(x)` is the `p`-adic **valuation** (an integer, possibly
//! negative for elements of `ℚ_p ∖ ℤ_p`) and `u` is a **unit** — an integer
//! coprime to `p`, stored reduced modulo `p^N`. `N` is the *relative* precision:
//! the number of significant `p`-adic digits we keep for the unit. Equivalently
//! the value is known modulo `p^{v+N}`; that exponent `v + N` is the *absolute*
//! precision and is what [`Padic`] stores internally (as `abs_prec`), so that
//! cancellation is tracked honestly.
//!
//! The **zero** value carries no valuation (its valuation is `+∞`); it is stored
//! with `unit = 0` and an absolute precision `a`, meaning only that the value is
//! `≡ 0 (mod p^a)`. [`Padic::is_zero`] reports whether all known digits vanish.
//!
//! # Digit order and [`Display`](core::fmt::Display)
//!
//! [`Display`](core::fmt::Display) prints the expansion **low-order digit
//! first** as a sum `Σ dᵢ·p^i` (with `0 ≤ dᵢ < p`, `i` running from the
//! valuation up), terminated by a big-`O` term recording the absolute precision,
//! e.g. `-1` in `ℤ₂` to five digits prints as
//! `1 + 1*2 + 1*2^2 + 1*2^3 + 1*2^4 + O(2^5)`. The zero value prints as
//! `O(p^a)`.
//!
//! # Clean-room provenance
//!
//! Definitions and algorithms are drawn from the open literature — Gouvêa,
//! *p-adic Numbers: An Introduction*; Koblitz, *p-adic Numbers, p-adic Analysis,
//! and Zeta-Functions*; Knuth, *TAOCP* Vol. 2 (Hensel lifting / Newton
//! iteration). No third-party source was consulted.

use core::cmp::Ordering;
use core::fmt;

use alloc::vec::Vec;

use crate::int::Int;
use crate::rational::Rational;

/// A `p`-adic number held to a fixed number of significant digits.
///
/// See the [module documentation](self) for the representation and the
/// [`Display`](core::fmt::Display) format.
#[derive(Clone)]
pub struct Padic {
    /// The prime `p`. Must be prime (checked with a `debug_assert!`).
    p: Int,
    /// Valuation `v`; meaningful only when `unit != 0`.
    val: i64,
    /// The unit `u`, reduced modulo `p^{abs_prec - val}` and coprime to `p`;
    /// `0` for the zero value.
    unit: Int,
    /// Absolute precision `a`: the value is known modulo `p^a`.
    abs_prec: i64,
}

/// Splits `p` out of `m`, returning `(v, cofactor)` with `m = p^v · cofactor`
/// and `cofactor` coprime to `p` (the cofactor keeps the sign of `m`). For
/// `m = 0` returns `(0, 0)`.
fn split_val(mut m: Int, p: &Int) -> (i64, Int) {
    if m.is_zero() {
        return (0, m);
    }
    let mut v = 0i64;
    while m.rem_euclid(p).is_zero() {
        m = m.div_exact(p);
        v += 1;
    }
    (v, m)
}

impl Padic {
    /// Builds the zero value of `ℚ_p` with the given `p` and relative precision
    /// `N` (its absolute precision is `N`, i.e. it is only known to be `≡ 0
    /// (mod p^N)`).
    ///
    /// Panics if `precision < 1`. `p` must be prime (checked in debug builds).
    pub fn new(p: Int, precision: i64) -> Padic {
        assert!(precision >= 1, "p-adic precision must be >= 1");
        debug_assert!(p.is_prime_bpsw(), "p-adic modulus must be prime");
        Padic {
            p,
            val: 0,
            unit: Int::ZERO,
            abs_prec: precision,
        }
    }

    /// The multiplicative identity `1` in `ℚ_p` at relative precision `N`.
    pub fn one(p: Int, precision: i64) -> Padic {
        Padic::from_int(p, precision, Int::ONE)
    }

    /// Builds a raw zero with a given absolute precision.
    fn zero_with(p: Int, abs_prec: i64) -> Padic {
        Padic {
            p,
            val: 0,
            unit: Int::ZERO,
            abs_prec,
        }
    }

    /// The `p`-adic value of an integer, to relative precision `N`.
    ///
    /// Panics if `precision < 1`.
    pub fn from_int(p: Int, precision: i64, n: Int) -> Padic {
        assert!(precision >= 1, "p-adic precision must be >= 1");
        debug_assert!(p.is_prime_bpsw(), "p-adic modulus must be prime");
        if n.is_zero() {
            return Padic::zero_with(p, precision);
        }
        let (v, cof) = split_val(n, &p);
        let modulus = p.pow(precision as u32);
        let unit = cof.rem_euclid(&modulus);
        Padic {
            p,
            val: v,
            unit,
            abs_prec: v + precision,
        }
    }

    /// The `p`-adic value of a rational `a/b`, to relative precision `N`.
    ///
    /// This is defined for **every** rational: a factor of `p` in the
    /// denominator simply contributes a negative valuation (so `1/p` becomes a
    /// genuine element of `ℚ_p` with valuation `-1`).
    ///
    /// Panics if `precision < 1`.
    pub fn from_rational(p: Int, precision: i64, r: &Rational) -> Padic {
        assert!(precision >= 1, "p-adic precision must be >= 1");
        debug_assert!(p.is_prime_bpsw(), "p-adic modulus must be prime");
        if r.is_zero() {
            return Padic::zero_with(p, precision);
        }
        let (va, ac) = split_val(r.numerator().clone(), &p);
        let (vb, bc) = split_val(r.denominator().clone(), &p);
        let v = va - vb;
        let modulus = p.pow(precision as u32);
        let binv = bc
            .rem_euclid(&modulus)
            .modinv(&modulus)
            .expect("denominator cofactor is coprime to p");
        let unit = ac.mul(&binv).rem_euclid(&modulus);
        Padic {
            p,
            val: v,
            unit,
            abs_prec: v + precision,
        }
    }

    /// The prime `p`.
    #[inline]
    pub fn prime(&self) -> &Int {
        &self.p
    }

    /// Returns `true` if every known digit is zero (the value is zero to the
    /// working precision).
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.unit.is_zero()
    }

    /// The `p`-adic valuation `v_p(x)`, or `None` for zero (whose valuation is
    /// `+∞`).
    #[inline]
    pub fn valuation(&self) -> Option<i64> {
        if self.is_zero() { None } else { Some(self.val) }
    }

    /// The relative precision — the number of significant unit digits kept. For
    /// a zero value this is its absolute precision (the exponent to which it is
    /// known to vanish).
    #[inline]
    pub fn precision(&self) -> i64 {
        self.abs_prec - self.val
    }

    /// The absolute precision `a`: the value is known modulo `p^a`.
    #[inline]
    pub fn absolute_precision(&self) -> i64 {
        self.abs_prec
    }

    /// The `p`-adic absolute value `|x|_p = p^{-v}` as an exact
    /// [`Rational`]; `0` for the zero value.
    pub fn abs_value(&self) -> Rational {
        if self.is_zero() {
            return Rational::ZERO;
        }
        if self.val >= 0 {
            Rational::new(Int::ONE, self.p.pow(self.val as u32))
        } else {
            Rational::from_integer(self.p.pow((-self.val) as u32))
        }
    }

    /// The known base-`p` digits `dᵢ ∈ [0, p)`, **low-order first**, starting at
    /// the [`valuation`](Padic::valuation). Empty for the zero value.
    ///
    /// For example `-1` in `ℤ₂` yields all-ones.
    pub fn digits(&self) -> Vec<Int> {
        let mut out = Vec::new();
        if self.is_zero() {
            return out;
        }
        let rel = self.abs_prec - self.val;
        let mut u = self.unit.clone();
        for _ in 0..rel {
            let (q, r) = u.div_rem(&self.p).expect("p != 0");
            out.push(r);
            u = q;
        }
        out
    }

    /// Returns a copy known only to absolute precision `a` (never *raises*
    /// precision: if `a >= self.abs_prec` the value is returned unchanged).
    fn reduce_to_abs(&self, a: i64) -> Padic {
        if a >= self.abs_prec {
            return self.clone();
        }
        if self.is_zero() {
            return Padic::zero_with(self.p.clone(), a);
        }
        let rel = a - self.val;
        if rel <= 0 {
            return Padic::zero_with(self.p.clone(), a);
        }
        let modulus = self.p.pow(rel as u32);
        Padic {
            p: self.p.clone(),
            val: self.val,
            unit: self.unit.rem_euclid(&modulus),
            abs_prec: a,
        }
    }

    /// Rebuilds a canonical value from `x = p^{v0}·c`, known modulo `p^{aprec}`,
    /// factoring any further powers of `p` out of `c`.
    fn normalize(p: Int, v0: i64, c: Int, aprec: i64) -> Padic {
        let rel = aprec - v0;
        if rel <= 0 {
            return Padic::zero_with(p, aprec);
        }
        let modulus = p.pow(rel as u32);
        let cr = c.rem_euclid(&modulus);
        if cr.is_zero() {
            return Padic::zero_with(p, aprec);
        }
        let (t, unit_full) = split_val(cr, &p);
        // unit_full is coprime to p and already in [0, p^rel); reduce to the
        // now-shorter unit window for good measure.
        let unit_mod = p.pow((rel - t) as u32);
        let unit = unit_full.rem_euclid(&unit_mod);
        Padic {
            p,
            val: v0 + t,
            unit,
            abs_prec: aprec,
        }
    }

    /// Asserts the two operands live in the same `ℚ_p`.
    fn check_same(&self, other: &Padic) {
        assert!(self.p == other.p, "p-adic operands have different primes");
    }

    /// Returns `-self`.
    pub fn neg(&self) -> Padic {
        if self.is_zero() {
            return self.clone();
        }
        let rel = self.abs_prec - self.val;
        let modulus = self.p.pow(rel as u32);
        Padic {
            p: self.p.clone(),
            val: self.val,
            unit: modulus.sub(&self.unit),
            abs_prec: self.abs_prec,
        }
    }

    /// Returns `self + rhs`.
    pub fn add(&self, rhs: &Padic) -> Padic {
        self.check_same(rhs);
        let a = self.abs_prec.min(rhs.abs_prec);
        if self.is_zero() {
            return rhs.reduce_to_abs(a);
        }
        if rhs.is_zero() {
            return self.reduce_to_abs(a);
        }
        let vmin = self.val.min(rhs.val);
        let c1 = self.unit.mul(&self.p.pow((self.val - vmin) as u32));
        let c2 = rhs.unit.mul(&rhs.p.pow((rhs.val - vmin) as u32));
        Padic::normalize(self.p.clone(), vmin, c1.add(&c2), a)
    }

    /// Returns `self - rhs`.
    pub fn sub(&self, rhs: &Padic) -> Padic {
        self.add(&rhs.neg())
    }

    /// Returns `self · rhs`.
    pub fn mul(&self, rhs: &Padic) -> Padic {
        self.check_same(rhs);
        // For precision bookkeeping, a zero of absolute precision `a` behaves
        // like a value of valuation `a`.
        let veff = |x: &Padic| if x.is_zero() { x.abs_prec } else { x.val };
        if self.is_zero() || rhs.is_zero() {
            return Padic::zero_with(self.p.clone(), veff(self) + veff(rhs));
        }
        let v = self.val + rhs.val;
        let rel = (self.abs_prec - self.val).min(rhs.abs_prec - rhs.val);
        let modulus = self.p.pow(rel as u32);
        let unit = self.unit.mul(&rhs.unit).rem_euclid(&modulus);
        Padic {
            p: self.p.clone(),
            val: v,
            unit,
            abs_prec: v + rel,
        }
    }

    /// Returns `self / rhs`. Division is defined for every nonzero `rhs` in
    /// `ℚ_p`.
    ///
    /// Panics if `rhs` is zero.
    pub fn div(&self, rhs: &Padic) -> Padic {
        self.check_same(rhs);
        assert!(!rhs.is_zero(), "p-adic division by zero");
        if self.is_zero() {
            return Padic::zero_with(self.p.clone(), self.abs_prec - rhs.val);
        }
        let v = self.val - rhs.val;
        let rel = (self.abs_prec - self.val).min(rhs.abs_prec - rhs.val);
        let modulus = self.p.pow(rel as u32);
        let inv = rhs
            .unit
            .modinv(&modulus)
            .expect("unit is coprime to p, hence invertible");
        let unit = self.unit.mul(&inv).rem_euclid(&modulus);
        Padic {
            p: self.p.clone(),
            val: v,
            unit,
            abs_prec: v + rel,
        }
    }

    /// Returns `1 / self`. Panics if `self` is zero.
    pub fn inv(&self) -> Padic {
        Padic::one(self.p.clone(), self.abs_prec - self.val).div(self)
    }

    /// A representative rational congruent to this value modulo its absolute
    /// precision: the exact number `u · p^v` (an integer when `v ≥ 0`, otherwise
    /// `u / p^{-v}`). Returns `0` for the zero value.
    ///
    /// Note this is one representative of the residue class, not a canonical
    /// small fraction; use it for exact re-injection, not for pretty-printing.
    pub fn to_rational(&self) -> Rational {
        if self.is_zero() {
            return Rational::ZERO;
        }
        if self.val >= 0 {
            Rational::from_integer(self.unit.mul(&self.p.pow(self.val as u32)))
        } else {
            Rational::new(self.unit.clone(), self.p.pow((-self.val) as u32))
        }
    }

    /// A square root of `self` via Hensel lifting, or `None` if `self` is not a
    /// square in `ℚ_p`.
    ///
    /// A nonzero `x = p^v·u` is a square iff `v` is even **and** the unit `u` is
    /// a square unit — a quadratic residue mod `p` for odd `p`, or `≡ 1 (mod 8)`
    /// for `p = 2`. Exactly one of the two roots is returned. The result has the
    /// same relative precision as `self`.
    pub fn sqrt(&self) -> Option<Padic> {
        if self.is_zero() {
            // x ≡ 0 (mod p^a) ⇒ √x ≡ 0 (mod p^⌈a/2⌉).
            return Some(Padic::zero_with(self.p.clone(), (self.abs_prec + 1) / 2));
        }
        if self.val % 2 != 0 {
            return None;
        }
        let rel = self.abs_prec - self.val;
        let root_unit = if self.p == Int::from_i64(2) {
            sqrt_unit_2adic(&self.unit, rel)?
        } else {
            sqrt_unit_odd(&self.p, &self.unit, rel)?
        };
        let v = self.val / 2;
        Some(Padic {
            p: self.p.clone(),
            val: v,
            unit: root_unit,
            abs_prec: v + rel,
        })
    }
}

/// Hensel/Newton square root of a unit `u` modulo `p^rel` for **odd** `p`, or
/// `None` if `u` is a non-residue.
fn sqrt_unit_odd(p: &Int, u: &Int, rel: i64) -> Option<Int> {
    // Seed: a square root mod p.
    let mut r = u.sqrt_mod(p)?;
    let mut k = 1i64;
    while k < rel {
        let k2 = (2 * k).min(rel);
        let modulus = p.pow(k2 as u32);
        // Newton step: r ← r - (r² - u)·(2r)⁻¹  (mod p^{k2}).
        let two_r = r.add(&r).rem_euclid(&modulus);
        let inv = two_r
            .modinv(&modulus)
            .expect("2r is a unit for odd p and unit r");
        let f = r.mul(&r).sub(u).rem_euclid(&modulus);
        r = r.sub(&f.mul(&inv)).rem_euclid(&modulus);
        k = k2;
    }
    let modulus = p.pow(rel as u32);
    Some(r.rem_euclid(&modulus))
}

/// Square root of a 2-adic unit `u` modulo `2^rel`, lifting one bit at a time,
/// or `None` if `u` is not a 2-adic square.
fn sqrt_unit_2adic(u: &Int, rel: i64) -> Option<Int> {
    let two = Int::from_i64(2);
    // Seed at k = min(rel, 3): every square unit is ≡ 1 (mod 8), so r = 1 works
    // provided u already matches to that depth.
    let m0 = rel.min(3);
    let seed_mod = two.pow(m0 as u32);
    if !u.sub(&Int::ONE).rem_euclid(&seed_mod).is_zero() {
        return None; // u ≢ 1 (mod 2^m0): not a square this far.
    }
    let mut r = Int::ONE;
    let mut k = m0;
    while k < rel {
        let mod_next = two.pow((k + 1) as u32);
        let bit = two.pow((k - 1) as u32);
        // Try r and r + 2^{k-1}; keep whichever squares to u mod 2^{k+1}.
        let cand2 = r.add(&bit);
        if r.mul(&r).sub(u).rem_euclid(&mod_next).is_zero() {
            // r already correct at this depth.
        } else if cand2.mul(&cand2).sub(u).rem_euclid(&mod_next).is_zero() {
            r = cand2;
        } else {
            return None;
        }
        k += 1;
    }
    let modulus = two.pow(rel as u32);
    Some(r.rem_euclid(&modulus))
}

impl PartialEq for Padic {
    /// Values are equal when they share a prime and agree on every digit up to
    /// their common absolute precision.
    fn eq(&self, other: &Padic) -> bool {
        if self.p != other.p {
            return false;
        }
        let a = self.abs_prec.min(other.abs_prec);
        let x = self.reduce_to_abs(a);
        let y = other.reduce_to_abs(a);
        match (x.is_zero(), y.is_zero()) {
            (true, true) => true,
            (false, false) => x.val == y.val && x.unit == y.unit,
            _ => false,
        }
    }
}

impl Eq for Padic {}

impl fmt::Display for Padic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_zero() {
            return write!(f, "O({}^{})", self.p, self.abs_prec);
        }
        let digits = self.digits();
        let mut first = true;
        for (i, d) in digits.iter().enumerate() {
            if d.is_zero() {
                continue;
            }
            if !first {
                f.write_str(" + ")?;
            }
            first = false;
            let exp = self.val + i as i64;
            match exp {
                0 => write!(f, "{d}")?,
                1 => write!(f, "{d}*{}", self.p)?,
                _ => write!(f, "{d}*{}^{exp}", self.p)?,
            }
        }
        if !first {
            f.write_str(" + ")?;
        }
        write!(f, "O({}^{})", self.p, self.abs_prec)
    }
}

impl fmt::Debug for Padic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_zero() {
            write!(f, "Padic(0 + O({}^{}))", self.p, self.abs_prec)
        } else {
            write!(
                f,
                "Padic({}^{} · {} + O({}^{}))",
                self.p, self.val, self.unit, self.p, self.abs_prec
            )
        }
    }
}

impl PartialOrd for Padic {
    /// `ℚ_p` has no compatible order; this only distinguishes equal values
    /// (returning `Some(Equal)`) from unequal ones (`None`).
    fn partial_cmp(&self, other: &Padic) -> Option<Ordering> {
        if self == other {
            Some(Ordering::Equal)
        } else {
            None
        }
    }
}

macro_rules! padic_binop {
    ($tr:ident, $m:ident, $atr:ident, $am:ident) => {
        impl core::ops::$tr for Padic {
            type Output = Padic;
            #[inline]
            fn $m(self, rhs: Padic) -> Padic {
                Padic::$m(&self, &rhs)
            }
        }
        impl core::ops::$tr<&Padic> for &Padic {
            type Output = Padic;
            #[inline]
            fn $m(self, rhs: &Padic) -> Padic {
                Padic::$m(self, rhs)
            }
        }
        impl core::ops::$atr for Padic {
            #[inline]
            fn $am(&mut self, rhs: Padic) {
                *self = Padic::$m(self, &rhs);
            }
        }
    };
}

padic_binop!(Add, add, AddAssign, add_assign);
padic_binop!(Sub, sub, SubAssign, sub_assign);
padic_binop!(Mul, mul, MulAssign, mul_assign);
padic_binop!(Div, div, DivAssign, div_assign);

impl core::ops::Neg for Padic {
    type Output = Padic;
    #[inline]
    fn neg(self) -> Padic {
        Padic::neg(&self)
    }
}

impl core::ops::Neg for &Padic {
    type Output = Padic;
    #[inline]
    fn neg(self) -> Padic {
        Padic::neg(self)
    }
}