adele-ring 0.1.0

Exact multi-base arithmetic engine via the Residue Number System (RNS), with a number tower from integers to symbolic expressions and first-class CPU (rayon) + GPU (wgpu) backends.
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
//! The number tower router. Every value carries a [`TowerLevel`]; operations try
//! to *stay at the lowest (cheapest) level* they can, dropping down when a result
//! simplifies (e.g. √2·√2 = 2) and rising only when forced.
//!
//! When a caller asks for digits, the request flows *down* the tower: a symbolic
//! expression is simplified, then (if still not exact) elevated to a computable
//! real that produces the requested precision.

use num_bigint::BigInt;

use crate::algebraic::AlgebraicNumber;
use crate::computable::ComputableReal;
use crate::primes::factorize;
use crate::rational::RnsRational;
use crate::rns::{Channels, RnsInt};
use crate::symbolic::{IdentityGraph, SymbolicExpr};

/// The levels of the number tower, ordered cheapest-first.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum TowerLevel {
    Integer = 0,
    Rational = 1,
    Algebraic = 2,
    Computable = 3,
    Symbolic = 4,
}

/// A value somewhere in the number tower.
#[derive(Clone)]
pub enum TowerValue {
    Integer(RnsInt),
    Rational(RnsRational),
    Algebraic(AlgebraicNumber),
    Computable(ComputableReal),
    Symbolic(SymbolicExpr),
}

impl TowerValue {
    /// The level this value currently sits at.
    pub fn level(&self) -> TowerLevel {
        match self {
            TowerValue::Integer(_) => TowerLevel::Integer,
            TowerValue::Rational(_) => TowerLevel::Rational,
            TowerValue::Algebraic(_) => TowerLevel::Algebraic,
            TowerValue::Computable(_) => TowerLevel::Computable,
            TowerValue::Symbolic(_) => TowerLevel::Symbolic,
        }
    }

    /// Best-effort channels for this value (symbolic falls back to the standard set).
    pub fn channels(&self) -> Channels {
        match self {
            TowerValue::Integer(i) => i.channels.clone(),
            TowerValue::Rational(r) => r.channels.clone(),
            TowerValue::Algebraic(a) => a.channels.clone(),
            TowerValue::Computable(c) => c.channels(),
            TowerValue::Symbolic(_) => Channels::standard(32),
        }
    }

    /// Reduce to the lowest valid level (applied to a fixed point).
    pub fn reduce(&self) -> TowerValue {
        let mut current = self.clone();
        loop {
            let next = current.reduce_once();
            if next.level() == current.level() {
                return next;
            }
            current = next;
        }
    }

    fn reduce_once(&self) -> TowerValue {
        match self {
            TowerValue::Algebraic(a) if a.degree() == 1 => {
                TowerValue::Rational(a.to_rational().unwrap())
            }
            TowerValue::Rational(r) if r.is_integer() => {
                let p = r.to_pair().0;
                TowerValue::Integer(RnsInt::from_bigint(&p, r.channels.clone()))
            }
            TowerValue::Symbolic(e) => {
                let simplified = IdentityGraph::standard().simplify(e.clone());
                match symbolic_to_value(&simplified, &self.channels()) {
                    Some(v) => v,
                    None => TowerValue::Symbolic(simplified),
                }
            }
            other => other.clone(),
        }
    }

    /// Elevate to at least `target` (for mixed-level operations). Symbolic is the
    /// top; numeric levels rise one step at a time.
    pub fn elevate_to(&self, target: TowerLevel) -> TowerValue {
        let mut current = self.clone();
        while current.level() < target {
            current = current.elevate_once();
        }
        current
    }

    fn elevate_once(&self) -> TowerValue {
        match self {
            TowerValue::Integer(i) => {
                TowerValue::Rational(RnsRational::new(i.to_bigint(), BigInt::from(1), i.channels.clone()))
            }
            TowerValue::Rational(r) => TowerValue::Algebraic(AlgebraicNumber::from_rational(r.clone())),
            TowerValue::Algebraic(a) => TowerValue::Computable(a.to_computable()),
            TowerValue::Computable(_) => self.clone(), // cannot rise to symbolic
            TowerValue::Symbolic(_) => self.clone(),
        }
    }

    /// Decimal approximation (levels 0–3; symbolic is simplified/evaluated first).
    pub fn to_f64(&self) -> Option<f64> {
        match self {
            TowerValue::Integer(i) => {
                Some(RnsRational::new(i.to_bigint(), BigInt::from(1), i.channels.clone()).to_f64())
            }
            TowerValue::Rational(r) => Some(r.to_f64()),
            TowerValue::Algebraic(a) => Some(a.to_f64()),
            TowerValue::Computable(c) => Some(c.evaluate_f64()),
            TowerValue::Symbolic(_) => self.reduce().non_symbolic_to_f64(),
        }
    }

    fn non_symbolic_to_f64(&self) -> Option<f64> {
        match self {
            TowerValue::Symbolic(e) => {
                // Try to evaluate a transcendental constant numerically.
                let ch = self.channels();
                symbolic_to_computable(e, &ch).map(|c| c.evaluate_f64())
            }
            other => other.to_f64(),
        }
    }

    /// Produce exact digits to `precision` decimal places, as a rational.
    pub fn digits(&self, precision: u64) -> RnsRational {
        match self {
            TowerValue::Integer(i) => {
                RnsRational::new(i.to_bigint(), BigInt::from(1), i.channels.clone())
            }
            TowerValue::Rational(r) => r.clone(),
            TowerValue::Algebraic(a) => {
                let mut clone = a.clone();
                let target = RnsRational::new(
                    BigInt::from(1),
                    BigInt::from(10u8).pow((precision + 1) as u32),
                    a.channels.clone(),
                );
                clone.refine_interval(&target);
                clone.interval.0.midpoint(&clone.interval.1)
            }
            TowerValue::Computable(c) => c.evaluate(precision),
            TowerValue::Symbolic(e) => {
                let ch = self.channels();
                let simplified = IdentityGraph::standard().simplify(e.clone());
                if let Some(v) = symbolic_to_value(&simplified, &ch) {
                    return v.digits(precision);
                }
                match symbolic_to_computable(&simplified, &ch) {
                    Some(c) => c.evaluate(precision),
                    None => panic!("cannot produce digits for irreducible symbolic expression"),
                }
            }
        }
    }

    // ── Arithmetic ──────────────────────────────────────────────────────────

    /// Addition, dropping the result to its lowest valid level.
    pub fn add(&self, other: &TowerValue) -> TowerValue {
        self.binop(other, Op::Add).reduce()
    }

    /// Multiplication, dropping the result to its lowest valid level.
    pub fn mul(&self, other: &TowerValue) -> TowerValue {
        self.binop(other, Op::Mul).reduce()
    }

    fn binop(&self, other: &TowerValue, op: Op) -> TowerValue {
        let lvl = self.level().max(other.level());
        if lvl == TowerLevel::Symbolic {
            let a = self.to_symbolic();
            let b = other.to_symbolic();
            let expr = match op {
                Op::Add => SymbolicExpr::Add(vec![a, b]),
                Op::Mul => SymbolicExpr::Mul(vec![a, b]),
            };
            return TowerValue::Symbolic(IdentityGraph::standard().simplify(expr));
        }
        let a = self.elevate_to(lvl);
        let b = other.elevate_to(lvl);
        match (a, b) {
            (TowerValue::Integer(x), TowerValue::Integer(y)) => TowerValue::Integer(match op {
                Op::Add => x.add(&y),
                Op::Mul => x.mul(&y),
            }),
            (TowerValue::Rational(x), TowerValue::Rational(y)) => TowerValue::Rational(match op {
                Op::Add => x.add(&y),
                Op::Mul => x.mul(&y),
            }),
            (TowerValue::Algebraic(x), TowerValue::Algebraic(y)) => TowerValue::Algebraic(match op {
                Op::Add => x.add(&y),
                Op::Mul => x.mul(&y),
            }),
            (TowerValue::Computable(x), TowerValue::Computable(y)) => TowerValue::Computable(match op {
                Op::Add => x.add(&y),
                Op::Mul => x.mul(&y),
            }),
            _ => unreachable!("levels were equalized before the operation"),
        }
    }

    /// Square root: a perfect square drops to `Integer`, otherwise rises to
    /// `Algebraic`.
    pub fn sqrt(&self) -> TowerValue {
        let ch = self.channels();
        if let TowerValue::Integer(i) = self {
            let n = i.to_bigint();
            if let Some(nu) = bigint_to_u64(&n) {
                let root = (nu as f64).sqrt().round() as u64;
                if root * root == nu {
                    return TowerValue::Integer(RnsInt::from_bigint(&BigInt::from(root), ch));
                }
                return TowerValue::Algebraic(AlgebraicNumber::sqrt(nu, ch)).reduce();
            }
        }
        // General: take the f64 value and build an algebraic square root if integral.
        let v = self.to_f64().unwrap_or(f64::NAN);
        if v >= 0.0 && v.fract() == 0.0 {
            return TowerValue::Algebraic(AlgebraicNumber::sqrt(v as u64, ch)).reduce();
        }
        panic!("sqrt of non-integer values is not supported at the tower level yet");
    }

    /// sin: checks the symbolic identity graph; otherwise stays symbolic.
    pub fn sin(&self) -> TowerValue {
        let expr = SymbolicExpr::Sin(Box::new(self.to_symbolic()));
        TowerValue::Symbolic(IdentityGraph::standard().simplify(expr)).reduce()
    }

    fn to_symbolic(&self) -> SymbolicExpr {
        match self {
            TowerValue::Integer(i) => SymbolicExpr::Integer(i.to_bigint()),
            TowerValue::Rational(r) => {
                let (p, q) = r.to_pair();
                SymbolicExpr::Rational(p, q)
            }
            TowerValue::Symbolic(e) => e.clone(),
            // Algebraic/Computable have no faithful finite symbolic form here.
            TowerValue::Algebraic(a) => {
                if let Some(r) = a.to_rational() {
                    let (p, q) = r.to_pair();
                    SymbolicExpr::Rational(p, q)
                } else {
                    panic!("cannot lift this algebraic number to symbolic form")
                }
            }
            TowerValue::Computable(_) => panic!("cannot lift a computable real to symbolic form"),
        }
    }
}

#[derive(Clone, Copy)]
enum Op {
    Add,
    Mul,
}

fn bigint_to_u64(n: &BigInt) -> Option<u64> {
    use num_traits::ToPrimitive;
    n.to_u64()
}

/// Map a fully simplified symbolic expression onto a concrete tower value.
fn symbolic_to_value(e: &SymbolicExpr, ch: &Channels) -> Option<TowerValue> {
    match e {
        SymbolicExpr::Integer(n) => Some(TowerValue::Integer(RnsInt::from_bigint(n, ch.clone()))),
        SymbolicExpr::Rational(p, q) => {
            Some(TowerValue::Rational(RnsRational::new(p.clone(), q.clone(), ch.clone())))
        }
        SymbolicExpr::Sqrt { radicand } => {
            let nu = bigint_to_u64(radicand)?;
            Some(TowerValue::Algebraic(AlgebraicNumber::sqrt(nu, ch.clone())))
        }
        SymbolicExpr::ScaledSqrt { coeff: (a, b), rad } => {
            let nu = bigint_to_u64(rad)?;
            let s = AlgebraicNumber::sqrt(nu, ch.clone());
            let coeff = RnsRational::new(a.clone(), b.clone(), ch.clone());
            Some(TowerValue::Algebraic(s).mul(&TowerValue::Rational(coeff)))
        }
        _ => None,
    }
}

/// Build a computable real from a symbolic expression (used for digit requests
/// on transcendentals like π+1).
fn symbolic_to_computable(e: &SymbolicExpr, ch: &Channels) -> Option<ComputableReal> {
    match e {
        SymbolicExpr::Integer(n) => Some(ComputableReal::from_rational(RnsRational::new(
            n.clone(),
            BigInt::from(1),
            ch.clone(),
        ))),
        SymbolicExpr::Rational(p, q) => Some(ComputableReal::from_rational(RnsRational::new(
            p.clone(),
            q.clone(),
            ch.clone(),
        ))),
        SymbolicExpr::Pi => Some(ComputableReal::pi(ch.clone())),
        SymbolicExpr::E => Some(ComputableReal::e(ch.clone())),
        SymbolicExpr::Sqrt { radicand } => {
            let r = RnsRational::new(radicand.clone(), BigInt::from(1), ch.clone());
            Some(ComputableReal::sqrt(r))
        }
        SymbolicExpr::Add(terms) => {
            let mut acc: Option<ComputableReal> = None;
            for t in terms {
                let c = symbolic_to_computable(t, ch)?;
                acc = Some(match acc {
                    Some(a) => a.add(&c),
                    None => c,
                });
            }
            acc
        }
        SymbolicExpr::Mul(factors) => {
            let mut acc: Option<ComputableReal> = None;
            for f in factors {
                let c = symbolic_to_computable(f, ch)?;
                acc = Some(match acc {
                    Some(a) => a.mul(&c),
                    None => c,
                });
            }
            acc
        }
        _ => None,
    }
}

/// Square-free check helper exposed for completeness (used by examples/tests).
pub fn is_perfect_square(n: u64) -> bool {
    let r = (n as f64).sqrt().round() as u64;
    r * r == n
}

#[allow(dead_code)]
fn _uses_factorize() {
    let _ = factorize(12);
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ch() -> Channels {
        Channels::standard(32)
    }

    #[test]
    fn rational_level() {
        let v = TowerValue::Rational(RnsRational::from_fraction(2, 3, ch()));
        assert_eq!(v.level(), TowerLevel::Rational);
    }

    #[test]
    fn rational_with_denom_one_reduces_to_integer() {
        let v = TowerValue::Rational(RnsRational::from_fraction(6, 2, ch()));
        assert_eq!(v.reduce().level(), TowerLevel::Integer);
    }

    #[test]
    fn sqrt2_times_sqrt2_drops_to_integer() {
        let s = TowerValue::Algebraic(AlgebraicNumber::sqrt(2, ch()));
        let prod = s.mul(&s);
        assert_eq!(prod.level(), TowerLevel::Integer);
        assert_eq!(prod.to_f64().unwrap().round(), 2.0);
    }

    #[test]
    fn mixed_level_add() {
        // Integer 1 + Rational 1/2 = 3/2.
        let a = TowerValue::Integer(RnsInt::from_i64(1, ch()));
        let b = TowerValue::Rational(RnsRational::from_fraction(1, 2, ch()));
        let sum = a.add(&b);
        assert_eq!(sum.level(), TowerLevel::Rational);
        assert!((sum.to_f64().unwrap() - 1.5).abs() < 1e-12);
    }

    #[test]
    fn pi_plus_one_digits() {
        // π + 1 stays symbolic, then digits() elevates to computable.
        let pi = TowerValue::Symbolic(SymbolicExpr::Pi);
        let one = TowerValue::Integer(RnsInt::from_i64(1, ch()));
        let sum = pi.add(&one);
        let d = sum.digits(20);
        assert!((d.to_f64() - (std::f64::consts::PI + 1.0)).abs() < 1e-12);
    }

    #[test]
    fn sin_pi_is_zero() {
        let pi = TowerValue::Symbolic(SymbolicExpr::Pi);
        let s = pi.sin();
        assert_eq!(s.to_f64().unwrap(), 0.0);
    }
}