racah 0.1.1

Racah-Wigner calculus for compact Lie groups: exact SU(2) recoupling, and runtime Clebsch-Gordan / F- / R-coefficient generation for SU(N), SO(N), and Sp(2N)
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
//! Prime-factorized integer arithmetic for the Racah/Wigner factorial engine.
//!
//! Ported from WignerSymbols.jl v2.0.0 `src/primefactorization.jl`
//! (`PrimeFactorization`, `primefactor`, `primefactorial`, `mul!`,
//! `divexact!`, `gcd!`, `lcm!`, `divgcd!`, `splitsquare`). A nonnegative
//! integer is stored as the exponent vector of its prime factorization plus a
//! sign; products and exact quotients are integer vector add/sub, so no big
//! integer is multiplied until a single final reconstruction. Factorials and
//! their ratios (the whole cost of a 3j/6j evaluation) stay in exponent space.
//!
//! ## Thread-safety and the memory contract
//!
//! Two process-global tables grow monotonically and are shared across threads:
//! the prime list and the factorial exponent-vector table. Both are guarded by
//! an [`RwLock`]: the common case takes a read lock and returns a cached row;
//! only extending the table past the largest label seen takes the write lock.
//! WignerSymbols.jl uses the same growing-global-table design (its
//! `GrowingList`); an `RwLock` is the direct thread-safe Rust equivalent.
//!
//! The tables never shrink. Their size is bounded by the largest factorial
//! argument `N` any call has requested: the factorial table holds `N + 1` rows
//! of `~pi(N)` `u32` exponents each, and the prime list holds `~pi(N)` values.
//! For doubled spins in the thousands `N` is a few thousand, so the tables are
//! a few megabytes and are amortized across every subsequent symbol.

use std::sync::RwLock;

use num_bigint::BigInt;
use num_traits::{One, Zero};

/// A signed integer stored as `sign * prod_i prime(i)^powers[i]`.
///
/// `powers[i]` is the exponent of the `i`-th prime (`powers[0]` -> 2). The
/// vector is normalized so its last entry is nonzero (an empty vector is the
/// integer 1, or 0 when `sign == 0`). `sign` is `-1`, `0`, or `+1`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Pf {
    powers: Vec<u32>,
    sign: i8,
}

impl Pf {
    /// The integer 1.
    pub(crate) fn one() -> Self {
        Pf {
            powers: Vec::new(),
            sign: 1,
        }
    }

    /// Wrap a normalized (trailing-zero-trimmed) positive exponent vector.
    fn from_powers(mut powers: Vec<u32>) -> Self {
        trim(&mut powers);
        Pf { powers, sign: 1 }
    }

    /// Negate in place (flip the sign).
    pub(crate) fn neg(mut self) -> Self {
        self.sign = -self.sign;
        self
    }

    fn is_zero(&self) -> bool {
        self.sign == 0
    }

    /// `self *= other` — exponent vectors add, signs multiply
    /// (`primefactorization.jl::mul!`).
    pub(crate) fn mul_assign(&mut self, other: &Pf) {
        if self.sign == 0 || other.sign == 0 {
            self.sign = 0;
            self.powers.clear();
            return;
        }
        self.sign *= other.sign;
        if other.powers.len() > self.powers.len() {
            self.powers.resize(other.powers.len(), 0);
        }
        for (a, b) in self.powers.iter_mut().zip(other.powers.iter()) {
            *a += *b;
        }
    }

    /// `self *= prod_i prime(i)^powers[i]`, folding a raw positive exponent
    /// slice (a factorial table row) straight into `self` without allocating a
    /// [`Pf`] for it. This is the clone-free multiplicand path: callers
    /// accumulate a product of factorials in one buffer instead of cloning each
    /// `O(pi(N))` row only to add and drop it. Sign is unchanged (the slice is a
    /// factorial, hence positive).
    fn mul_by_powers(&mut self, powers: &[u32]) {
        if self.sign == 0 {
            return;
        }
        if powers.len() > self.powers.len() {
            self.powers.resize(powers.len(), 0);
        }
        for (a, b) in self.powers.iter_mut().zip(powers.iter()) {
            *a += *b;
        }
    }

    /// `self /= other`, exact division — exponent vectors subtract
    /// (`primefactorization.jl::divexact!`). `other` must divide `self`, which
    /// is guaranteed at every call site (a factorial ratio, or a divisor pulled
    /// out by gcd/lcm); the debug assertion documents that invariant.
    pub(crate) fn divexact_assign(&mut self, other: &Pf) {
        debug_assert!(other.sign != 0, "exact division by zero");
        if self.sign == 0 {
            return;
        }
        self.sign *= other.sign;
        debug_assert!(
            other.powers.len() <= self.powers.len(),
            "divexact: divisor has a prime the dividend lacks"
        );
        for (a, b) in self.powers.iter_mut().zip(other.powers.iter()) {
            debug_assert!(*a >= *b, "divexact: non-divisible exponent");
            *a -= *b;
        }
        trim(&mut self.powers);
    }

    /// `self = lcm(self, other)` (positive) — exponent-wise max
    /// (`primefactorization.jl::lcm!`).
    fn lcm_assign(&mut self, other: &Pf) {
        self.sign = 1;
        if other.powers.len() > self.powers.len() {
            self.powers.resize(other.powers.len(), 0);
        }
        for (a, b) in self.powers.iter_mut().zip(other.powers.iter()) {
            *a = (*a).max(*b);
        }
    }

    /// `self = gcd(self, other)` (positive) — exponent-wise min
    /// (`primefactorization.jl::gcd!`).
    fn gcd_assign(&mut self, other: &Pf) {
        self.sign = 1;
        let l = self.powers.len().min(other.powers.len());
        self.powers.truncate(l);
        for (a, b) in self.powers.iter_mut().zip(other.powers.iter()) {
            *a = (*a).min(*b);
        }
        trim(&mut self.powers);
    }

    /// Split off the common gcd of `a` and `b` in place, so afterwards
    /// `gcd(a, b) == 1` (`primefactorization.jl::divgcd!`). Keeps the two
    /// reconstructed big integers as small as possible.
    pub(crate) fn divgcd(a: &mut Pf, b: &mut Pf) {
        let l = a.powers.len().min(b.powers.len());
        for k in 0..l {
            let g = a.powers[k].min(b.powers[k]);
            a.powers[k] -= g;
            b.powers[k] -= g;
        }
        trim(&mut a.powers);
        trim(&mut b.powers);
    }

    /// Split `a = s^2 * r` with `r` square-free (all exponents 0 or 1)
    /// (`primefactorization.jl::splitsquare`). `r` carries the sign.
    pub(crate) fn splitsquare(&self) -> (Pf, Pf) {
        let s = Pf::from_powers(self.powers.iter().map(|&e| e >> 1).collect());
        let mut r = Pf::from_powers(self.powers.iter().map(|&e| e & 1).collect());
        r.sign = self.sign;
        (s, r)
    }

    /// Reconstruct the big integer (`primefactorization.jl::_convert!`). This is
    /// the only place a prime is raised to a power and multiplied out.
    pub(crate) fn to_bigint(&self) -> BigInt {
        if self.sign == 0 {
            return BigInt::zero();
        }
        let mut acc = BigInt::one();
        for (i, &e) in self.powers.iter().enumerate() {
            if e != 0 {
                acc *= BigInt::from(nth_prime(i)).pow(e);
            }
        }
        if self.sign < 0 {
            -acc
        } else {
            acc
        }
    }
}

/// Drop trailing zero exponents so the representation is canonical.
fn trim(powers: &mut Vec<u32>) {
    while matches!(powers.last(), Some(0)) {
        powers.pop();
    }
}

// ---------------------------------------------------------------------------
// Growing global tables (prime list, factorial exponent vectors).
// ---------------------------------------------------------------------------

static PRIMES: RwLock<Vec<u64>> = RwLock::new(Vec::new());
static FACT: RwLock<Vec<Vec<u32>>> = RwLock::new(Vec::new());

#[cfg(all(test, feature = "cgc-gen"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct TableStats {
    pub(crate) factorial_rows: usize,
    pub(crate) primes: usize,
    pub(crate) retained_capacity_bytes: usize,
}

#[cfg(all(test, feature = "cgc-gen"))]
pub(crate) fn table_stats() -> TableStats {
    let table = FACT.read().unwrap();
    let factorial_bytes = table
        .capacity()
        .saturating_mul(std::mem::size_of::<Vec<u32>>())
        .saturating_add(
            table
                .iter()
                .map(|row| row.capacity().saturating_mul(std::mem::size_of::<u32>()))
                .sum::<usize>(),
        );
    let primes = PRIMES.read().unwrap();
    TableStats {
        factorial_rows: table.len(),
        primes: primes.len(),
        retained_capacity_bytes: factorial_bytes
            .saturating_add(primes.capacity().saturating_mul(std::mem::size_of::<u64>())),
    }
}

/// The `idx`-th prime (`idx == 0` -> 2), extending the shared list if needed
/// (`primefactorization.jl::prime`).
fn nth_prime(idx: usize) -> u64 {
    {
        let primes = PRIMES.read().unwrap();
        if idx < primes.len() {
            return primes[idx];
        }
    }
    let mut primes = PRIMES.write().unwrap();
    if primes.is_empty() {
        primes.push(2);
    }
    while primes.len() <= idx {
        // Trial-divide by the already-known primes: all primes up to
        // sqrt(candidate) are present because the candidate starts just above
        // the current largest prime.
        let mut cand = primes[primes.len() - 1] + 1;
        while !is_prime_by(cand, &primes) {
            cand += 1;
        }
        primes.push(cand);
    }
    primes[idx]
}

fn is_prime_by(n: u64, primes: &[u64]) -> bool {
    for &p in primes {
        if p * p > n {
            break;
        }
        if n.is_multiple_of(p) {
            return false;
        }
    }
    true
}

/// Exponent vector of the prime factorization of `n >= 1`
/// (`primefactorization.jl::primefactor`).
fn primefactor_powers(mut n: u64) -> Vec<u32> {
    let mut powers = Vec::new();
    let mut idx = 0;
    while n > 1 {
        let p = nth_prime(idx);
        let mut e = 0u32;
        while n.is_multiple_of(p) {
            n /= p;
            e += 1;
        }
        powers.push(e);
        idx += 1;
    }
    trim(&mut powers);
    powers
}

/// Extend the shared factorial table so row `n` exists
/// (`primefactorization.jl::primefactorial`, growth half). Built incrementally:
/// `factorial(m) = factorial(m-1) * primefactor(m)` in exponent space.
fn grow_factorial(n: usize) {
    let mut table = FACT.write().unwrap();
    if table.is_empty() {
        table.push(Vec::new()); // 0! = 1
    }
    while table.len() <= n {
        let m = table.len() as u64;
        let fm = primefactor_powers(m);
        let mut next = table[table.len() - 1].clone();
        if fm.len() > next.len() {
            next.resize(fm.len(), 0);
        }
        for (a, b) in next.iter_mut().zip(fm.iter()) {
            *a += *b;
        }
        table.push(next);
    }
}

/// Multiply `acc` by `n!` in place. The common path takes only a read lock and
/// folds the memoized exponent row into `acc` with no per-call allocation or
/// row clone -- the whole point of the prime-factorized engine is that a
/// product of factorials costs one buffer, not one clone per factor.
pub(crate) fn mul_factorial(acc: &mut Pf, n: u64) {
    let n = n as usize;
    {
        let table = FACT.read().unwrap();
        if n < table.len() {
            acc.mul_by_powers(&table[n]);
            return;
        }
    }
    grow_factorial(n);
    let table = FACT.read().unwrap();
    acc.mul_by_powers(&table[n]);
}

/// `n!` as a fresh [`Pf`]. A thin wrapper over [`mul_factorial`] for the base
/// factor of a product (and the tests); multiplicands should use
/// [`mul_factorial`] to stay clone-free.
pub(crate) fn factorial(n: u64) -> Pf {
    let mut p = Pf::one();
    mul_factorial(&mut p, n);
    p
}

// ---------------------------------------------------------------------------
// Series over a common denominator (compute3jseries / compute6jseries core).
// ---------------------------------------------------------------------------

/// Sum a list of exact fractions `nums[i] / dens[i]`, each already prime
/// factorized, into one reduced [`num_bigint`] rational
/// (`primefactorization.jl::commondenominator!` + `sumlist!`,
/// `WignerSymbols.jl::compute{3,6}jseries`).
///
/// Puts every term over the lcm of the denominators, factors the gcd out of the
/// numerators before reconstruction, sums the small remainders as big integers,
/// then multiplies the common factor back. Big-integer work is confined here.
pub(crate) fn sum_series(mut terms: Vec<(Pf, Pf)>) -> num_rational::Ratio<BigInt> {
    use num_rational::Ratio;
    if terms.is_empty() {
        return Ratio::zero();
    }

    // Common denominator = lcm of all denominators; rescale each numerator.
    let mut den = terms[0].1.clone();
    for (_, d) in &terms[1..] {
        den.lcm_assign(d);
    }
    for (num, d) in &mut terms {
        num.mul_assign(&den);
        num.divexact_assign(d);
    }

    // Pull the gcd of all numerators out before reconstructing big integers, so
    // the summed integers stay as small as the series allows (`sumlist!`).
    let mut g = terms[0].0.clone();
    for (num, _) in &terms[1..] {
        g.gcd_assign(num);
    }
    let mut total = BigInt::zero();
    for (num, _) in &mut terms {
        if !g.is_zero() {
            num.divexact_assign(&g);
        }
        total += num.to_bigint();
    }
    total *= g.to_bigint();

    Ratio::new(total, den.to_bigint())
}

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

    /// Direct big-integer factorial, independent of the prime-factorized path.
    fn direct_factorial(n: u64) -> BigInt {
        let mut f = BigInt::one();
        for k in 2..=n {
            f *= BigInt::from(k);
        }
        f
    }

    #[test]
    fn nth_prime_matches_known() {
        let known = [2u64, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47];
        for (i, &p) in known.iter().enumerate() {
            assert_eq!(nth_prime(i), p, "prime index {i}");
        }
    }

    #[test]
    fn primefactor_reconstructs() {
        for n in 1u64..=200 {
            let pf = Pf::from_powers(primefactor_powers(n));
            assert_eq!(pf.to_bigint(), BigInt::from(n), "factoring {n}");
        }
    }

    #[test]
    fn factorial_exponents_vs_direct() {
        // The core TDD gate: prime-factorized n! reconstructs to the directly
        // computed n! for every n we care about at small scale.
        for n in 0u64..=30 {
            assert_eq!(
                factorial(n).to_bigint(),
                direct_factorial(n),
                "factorial({n})"
            );
        }
        // Out-of-order / repeated queries hit the memoized table.
        assert_eq!(factorial(10).to_bigint(), direct_factorial(10));
        assert_eq!(factorial(0).to_bigint(), BigInt::one());
        assert_eq!(factorial(1).to_bigint(), BigInt::one());
    }

    #[test]
    fn factorial_large_still_exact() {
        assert_eq!(factorial(100).to_bigint(), direct_factorial(100));
        assert_eq!(factorial(257).to_bigint(), direct_factorial(257));
    }

    #[test]
    fn mul_factorial_accumulates_clone_free() {
        // Accumulate 3! * 5! * 7! into one buffer via mul_factorial, and check
        // it equals the same product built by materializing each factorial.
        let mut acc = Pf::one();
        mul_factorial(&mut acc, 3);
        mul_factorial(&mut acc, 5);
        mul_factorial(&mut acc, 7);
        assert_eq!(
            acc.to_bigint(),
            direct_factorial(3) * direct_factorial(5) * direct_factorial(7)
        );
        // factorial(n) is the same as mul_factorial into one().
        assert_eq!(factorial(12).to_bigint(), {
            let mut p = Pf::one();
            mul_factorial(&mut p, 12);
            p.to_bigint()
        });
    }

    #[test]
    fn mul_and_divexact_roundtrip() {
        let a = factorial(12);
        let b = factorial(7);
        let mut p = a.clone();
        p.mul_assign(&b);
        assert_eq!(
            p.to_bigint(),
            direct_factorial(12) * direct_factorial(7),
            "12! * 7!"
        );
        p.divexact_assign(&b);
        assert_eq!(p.to_bigint(), a.to_bigint(), "(12! * 7!) / 7! == 12!");
    }

    #[test]
    fn divgcd_reduces_to_coprime() {
        let mut a = factorial(9); // 9!
        let mut b = factorial(6); // 6!
        Pf::divgcd(&mut a, &mut b);
        // gcd(9!,6!) = 6!, so a = 9!/6! = 7*8*9 = 504, b = 1.
        assert_eq!(a.to_bigint(), BigInt::from(504));
        assert_eq!(b.to_bigint(), BigInt::one());
    }

    #[test]
    fn splitsquare_roundtrips() {
        // a = s^2 * r with r square-free; check for a range of integers.
        for n in 1u64..=120 {
            let a = Pf::from_powers(primefactor_powers(n));
            let (s, r) = a.splitsquare();
            let recon = s.to_bigint().pow(2) * r.to_bigint();
            assert_eq!(recon, BigInt::from(n), "splitsquare {n}");
        }
        // A factorial: (2n)! splits and reconstructs.
        let a = factorial(20);
        let (s, r) = a.splitsquare();
        assert_eq!(s.to_bigint().pow(2) * r.to_bigint(), direct_factorial(20));
    }

    #[test]
    fn sign_propagates() {
        let mut a = factorial(5);
        a = a.neg();
        assert_eq!(a.to_bigint(), -direct_factorial(5));
        let b = factorial(3).neg();
        a.mul_assign(&b); // (-5!) * (-3!) = +5!*3!
        assert_eq!(a.to_bigint(), direct_factorial(5) * direct_factorial(3));
    }

    #[test]
    fn sum_series_matches_direct_rational() {
        use num_rational::Ratio;
        // Sum 1/3! - 1/(2!*4!) + 3!/(5!) as prime-factorized terms.
        let terms = vec![
            (Pf::one(), factorial(3)),
            (
                {
                    let mut n = Pf::one();
                    n = n.neg();
                    n
                },
                {
                    let mut d = factorial(2);
                    d.mul_assign(&factorial(4));
                    d
                },
            ),
            (factorial(3), factorial(5)),
        ];
        let got = sum_series(terms);
        let want = Ratio::new(BigInt::one(), BigInt::from(6))
            - Ratio::new(BigInt::one(), BigInt::from(48))
            + Ratio::new(BigInt::from(6), BigInt::from(120));
        assert_eq!(got, want);
    }
}