ogdoad 1.0.0

Clifford algebras (with nilpotents) over the field-like subclasses of combinatorial games: nimbers, surreals, surcomplex.
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
//! The checked integer-valued Clifford deformation: [`GameCliffordError`] and
//! [`GameClifford`].

use crate::clifford::{bits, CliffordAlgebra, Metric, Multivector};
use crate::games::partizan::Game;
use crate::linalg::integer::reduce_integer_vector;
use crate::scalar::Integer;
use std::collections::BTreeMap;
use std::fmt;

use super::lambda::{
    discover_relations, grade_masks, relation_multivector, DEFAULT_RELATION_BOUND,
};
use super::relations::{
    eval_relation, relation_search_certificate, GameRelation, RelationSearchCertificate,
};

/// Why a checked game-Clifford deformation was rejected.
///
/// The target here is an integer-valued Clifford deformation on the chosen game
/// subgroup. Relations in the game group are imposed as Clifford-ideal relations,
/// so each relation vector must be both null for `Q` and radical for the polar
/// pairing. Over the torsion-free target `Z`, this is what forces documented
/// vanishings such as `2* = 0` killing every pairing involving `*`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum GameCliffordError {
    QuadraticLength {
        expected: usize,
        got: usize,
    },
    BilinearKeyInvalid {
        i: usize,
        j: usize,
        dim: usize,
    },
    RelationLength {
        relation_index: usize,
        expected: usize,
        got: usize,
    },
    RelationNotZero {
        relation_index: usize,
        value_key: String,
    },
    RelationPolarNonzero {
        relation_index: usize,
        generator: usize,
        value: i128,
    },
    RelationQuadraticNonzero {
        relation_index: usize,
        value: i128,
    },
    ArithmeticOverflow {
        relation_index: usize,
        context: &'static str,
    },
}

impl fmt::Display for GameCliffordError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GameCliffordError::QuadraticLength { expected, got } => write!(
                f,
                "quadratic diagonal length must match generator count: expected {expected}, got {got}"
            ),
            GameCliffordError::BilinearKeyInvalid { i, j, dim } => write!(
                f,
                "bilinear key ({i},{j}) must satisfy i < j < {dim}"
            ),
            GameCliffordError::RelationLength {
                relation_index,
                expected,
                got,
            } => write!(
                f,
                "game relation #{relation_index} length must match generator count: expected {expected}, got {got}"
            ),
            GameCliffordError::RelationNotZero {
                relation_index,
                value_key,
            } => write!(
                f,
                "game relation #{relation_index} does not evaluate to zero (value {value_key})"
            ),
            GameCliffordError::RelationPolarNonzero {
                relation_index,
                generator,
                value,
            } => write!(
                f,
                "game relation #{relation_index} has nonzero polar pairing with generator {generator}: {value}"
            ),
            GameCliffordError::RelationQuadraticNonzero {
                relation_index,
                value,
            } => write!(
                f,
                "game relation #{relation_index} has nonzero quadratic value: {value}"
            ),
            GameCliffordError::ArithmeticOverflow {
                relation_index,
                context,
            } => write!(
                f,
                "integer overflow while checking game relation #{relation_index} ({context})"
            ),
        }
    }
}

impl std::error::Error for GameCliffordError {}

/// A checked integer-valued Clifford deformation of a game subgroup.
///
/// This is deliberately an engineering object, not a claim that arbitrary games
/// form Clifford scalars. The caller supplies integer quadratic data: diagonal
/// values `Q(e_i)` and off-diagonal polar values `{e_i,e_j}` for `i < j`. The
/// constructor verifies that every imposed game-group relation is null and polar
/// radical for that data before quotienting the ordinary integer Clifford algebra
/// by the generated relation ideal.
#[derive(Clone)]
pub struct GameClifford {
    alg: CliffordAlgebra<Integer>,
    gens: Vec<Game>,
    relations: Vec<GameRelation>,
    relation_search_complete: bool,
    relation_certificate: RelationSearchCertificate,
}

impl GameClifford {
    /// Build using bounded automatic relation discovery, matching
    /// [`GameExterior::new`](crate::games::GameExterior::new).
    pub fn new(
        gens: Vec<Game>,
        q: Vec<i128>,
        b: BTreeMap<(usize, usize), i128>,
    ) -> Result<GameClifford, GameCliffordError> {
        GameClifford::with_relation_search(gens, DEFAULT_RELATION_BOUND, q, b)
    }

    /// The free integer Clifford algebra on the chosen generators, with no
    /// game-group relations imposed. This is useful as an ambient object, but it
    /// does not check torsion or duplicate-generator constraints.
    pub fn free(
        gens: Vec<Game>,
        q: Vec<i128>,
        b: BTreeMap<(usize, usize), i128>,
    ) -> Result<GameClifford, GameCliffordError> {
        GameClifford::with_quadratic_data(gens, vec![], q, b)
    }

    /// Build from all bounded discovered relations `Σ c_i g_i = 0`, then verify
    /// those relations against the supplied quadratic data.
    pub fn with_relation_search(
        gens: Vec<Game>,
        bound: i128,
        q: Vec<i128>,
        b: BTreeMap<(usize, usize), i128>,
    ) -> Result<GameClifford, GameCliffordError> {
        let (relations, complete, candidate_count) = discover_relations(&gens, bound);
        let relation_certificate =
            relation_search_certificate(&gens, bound, complete, candidate_count, &relations, true);
        let mut out = GameClifford::with_quadratic_data(gens, relations, q, b)?;
        out.relation_search_complete = complete;
        out.relation_certificate = relation_certificate;
        Ok(out)
    }

    /// Build from explicit game-group relations and hand-supplied integer
    /// quadratic data. Every relation is checked both in the game group and in
    /// the Clifford data:
    ///
    /// * `Σ c_i g_i = 0` in the game group;
    /// * `Q(Σ c_i e_i) = 0`;
    /// * the polar pairing of `Σ c_i e_i` with every basis generator is zero.
    pub fn with_quadratic_data(
        gens: Vec<Game>,
        relations: Vec<GameRelation>,
        q: Vec<i128>,
        b: BTreeMap<(usize, usize), i128>,
    ) -> Result<GameClifford, GameCliffordError> {
        let n = gens.len();
        validate_quadratic_shape(n, &q, &b)?;
        for (relation_index, rel) in relations.iter().enumerate() {
            validate_game_relation(relation_index, &gens, rel)?;
            validate_quadratic_relation(relation_index, rel, &q, &b)?;
        }
        let relation_certificate =
            relation_search_certificate(&gens, 0, true, None, &relations, false);
        let metric = Metric::new(
            q.into_iter().map(Integer).collect(),
            b.into_iter().map(|(key, value)| (key, Integer(value))),
        );
        Ok(GameClifford {
            alg: CliffordAlgebra::new(n, metric),
            gens,
            relation_certificate,
            relations,
            relation_search_complete: true,
        })
    }

    /// The underlying free integer Clifford algebra before quotienting by
    /// game-group relations.
    pub fn algebra(&self) -> &CliffordAlgebra<Integer> {
        &self.alg
    }

    pub fn relations(&self) -> &[GameRelation] {
        &self.relations
    }

    pub fn relation_search_complete(&self) -> bool {
        self.relation_search_complete
    }

    pub fn relation_search_certificate(&self) -> &RelationSearchCertificate {
        &self.relation_certificate
    }

    /// The grade-1 generator `e_i` (corresponding to the game `g_i`), reduced in
    /// the checked Clifford quotient.
    pub fn generator(&self, i: usize) -> Multivector<Integer> {
        self.reduce(&self.alg.e(i))
    }

    /// The game `g_i` a generator stands for.
    pub fn game(&self, i: usize) -> &Game {
        &self.gens[i]
    }

    /// The module map from grade-1 elements to the game group. Panics if the
    /// reduced multivector is not purely grade 1.
    pub fn value_of_grade1(&self, mv: &Multivector<Integer>) -> Game {
        let mut acc = Game::zero();
        let mv = self.reduce(mv);
        for (&blade, coeff) in &mv.terms {
            assert_eq!(
                blade.count_ones(),
                1,
                "value_of_grade1 expects a grade-1 element"
            );
            let i = blade.trailing_zeros() as usize;
            acc = acc.add(&self.gens[i].times_int(coeff.0));
        }
        acc
    }

    pub fn add(&self, a: &Multivector<Integer>, b: &Multivector<Integer>) -> Multivector<Integer> {
        self.reduce(&self.alg.add(a, b))
    }

    pub fn scalar_mul(&self, s: i128, a: &Multivector<Integer>) -> Multivector<Integer> {
        self.reduce(&self.alg.scalar_mul(&Integer(s), a))
    }

    /// Quotient-aware Clifford product.
    pub fn mul(&self, a: &Multivector<Integer>, b: &Multivector<Integer>) -> Multivector<Integer> {
        self.reduce(&self.alg.mul(a, b))
    }

    /// Metric-independent exterior product, reduced in the same checked quotient.
    pub fn wedge(
        &self,
        a: &Multivector<Integer>,
        b: &Multivector<Integer>,
    ) -> Multivector<Integer> {
        self.reduce(&self.alg.wedge(a, b))
    }

    pub fn is_zero(&self, mv: &Multivector<Integer>) -> bool {
        self.reduce(mv).is_zero()
    }

    /// Reduce a free Clifford multivector by the two-sided ideal generated by the
    /// stored grade-1 game relations. Constructor compatibility checks ensure
    /// these relation vectors are null and polar-radical, so this quotient is the
    /// intended integer Clifford deformation of the game subgroup.
    pub fn reduce(&self, mv: &Multivector<Integer>) -> Multivector<Integer> {
        reduce_by_clifford_relation_ideal(&self.alg, self.gens.len(), &self.relations, mv)
    }
}

fn validate_quadratic_shape(
    n: usize,
    q: &[i128],
    b: &BTreeMap<(usize, usize), i128>,
) -> Result<(), GameCliffordError> {
    if q.len() != n {
        return Err(GameCliffordError::QuadraticLength {
            expected: n,
            got: q.len(),
        });
    }
    for &(i, j) in b.keys() {
        if i >= j || j >= n {
            return Err(GameCliffordError::BilinearKeyInvalid { i, j, dim: n });
        }
    }
    Ok(())
}

fn validate_game_relation(
    relation_index: usize,
    gens: &[Game],
    rel: &GameRelation,
) -> Result<(), GameCliffordError> {
    if rel.coeffs.len() != gens.len() {
        return Err(GameCliffordError::RelationLength {
            relation_index,
            expected: gens.len(),
            got: rel.coeffs.len(),
        });
    }
    let value = eval_relation(gens, &rel.coeffs);
    if !value.eq(&Game::zero()) {
        return Err(GameCliffordError::RelationNotZero {
            relation_index,
            value_key: value.canonical_string(),
        });
    }
    Ok(())
}

fn validate_quadratic_relation(
    relation_index: usize,
    rel: &GameRelation,
    q: &[i128],
    b: &BTreeMap<(usize, usize), i128>,
) -> Result<(), GameCliffordError> {
    for j in 0..q.len() {
        let value = relation_polar_value(relation_index, &rel.coeffs, q, b, j)?;
        if value != 0 {
            return Err(GameCliffordError::RelationPolarNonzero {
                relation_index,
                generator: j,
                value,
            });
        }
    }
    let value = relation_quadratic_value(relation_index, &rel.coeffs, q, b)?;
    if value != 0 {
        return Err(GameCliffordError::RelationQuadraticNonzero {
            relation_index,
            value,
        });
    }
    Ok(())
}

fn relation_polar_value(
    relation_index: usize,
    coeffs: &[i128],
    q: &[i128],
    b: &BTreeMap<(usize, usize), i128>,
    j: usize,
) -> Result<i128, GameCliffordError> {
    let mut acc = 0i128;
    for (i, &c) in coeffs.iter().enumerate() {
        if c == 0 {
            continue;
        }
        let polar_entry = if i == j {
            checked_mul_i128(relation_index, q[i], 2, "diagonal polar entry")?
        } else {
            let key = if i < j { (i, j) } else { (j, i) };
            *b.get(&key).unwrap_or(&0)
        };
        let term = checked_mul_i128(relation_index, c, polar_entry, "polar term")?;
        acc = checked_add_i128(relation_index, acc, term, "polar sum")?;
    }
    Ok(acc)
}

fn relation_quadratic_value(
    relation_index: usize,
    coeffs: &[i128],
    q: &[i128],
    b: &BTreeMap<(usize, usize), i128>,
) -> Result<i128, GameCliffordError> {
    let mut acc = 0i128;
    for (i, &c) in coeffs.iter().enumerate() {
        if c == 0 || q[i] == 0 {
            continue;
        }
        let square = checked_mul_i128(relation_index, c, c, "quadratic square")?;
        let term = checked_mul_i128(relation_index, square, q[i], "diagonal quadratic term")?;
        acc = checked_add_i128(relation_index, acc, term, "quadratic sum")?;
    }
    for i in 0..coeffs.len() {
        for j in i + 1..coeffs.len() {
            let bij = *b.get(&(i, j)).unwrap_or(&0);
            if coeffs[i] == 0 || coeffs[j] == 0 || bij == 0 {
                continue;
            }
            let coeff_product =
                checked_mul_i128(relation_index, coeffs[i], coeffs[j], "cross coefficient")?;
            let term =
                checked_mul_i128(relation_index, coeff_product, bij, "cross quadratic term")?;
            acc = checked_add_i128(relation_index, acc, term, "quadratic sum")?;
        }
    }
    Ok(acc)
}

fn checked_add_i128(
    relation_index: usize,
    a: i128,
    b: i128,
    context: &'static str,
) -> Result<i128, GameCliffordError> {
    a.checked_add(b)
        .ok_or(GameCliffordError::ArithmeticOverflow {
            relation_index,
            context,
        })
}

fn checked_mul_i128(
    relation_index: usize,
    a: i128,
    b: i128,
    context: &'static str,
) -> Result<i128, GameCliffordError> {
    a.checked_mul(b)
        .ok_or(GameCliffordError::ArithmeticOverflow {
            relation_index,
            context,
        })
}

fn reduce_by_clifford_relation_ideal(
    alg: &CliffordAlgebra<Integer>,
    dim: usize,
    relations: &[GameRelation],
    mv: &Multivector<Integer>,
) -> Multivector<Integer> {
    if relations.is_empty() || mv.is_zero() {
        return mv.clone();
    }
    let basis = all_blade_masks(dim);
    let index: BTreeMap<u128, usize> = basis.iter().enumerate().map(|(i, &m)| (m, i)).collect();
    let mut v = vec![0i128; basis.len()];
    for (&blade, coeff) in &mv.terms {
        if let Some(&i) = index.get(&blade) {
            v[i] += coeff.0;
        }
    }
    let rows = relation_rows_for_clifford_ideal(alg, relations, &basis, &index);
    reduce_integer_vector(&mut v, rows);
    let terms = basis
        .into_iter()
        .zip(v)
        .filter(|&(_, coeff)| coeff != 0)
        .map(|(blade, coeff)| (blade, Integer(coeff)))
        .collect();
    Multivector { terms }
}

fn relation_rows_for_clifford_ideal(
    alg: &CliffordAlgebra<Integer>,
    relations: &[GameRelation],
    basis: &[u128],
    index: &BTreeMap<u128, usize>,
) -> Vec<Vec<i128>> {
    let mut rows = Vec::new();
    for rel in relations {
        let rel_mv = relation_multivector(rel);
        for &mask in basis {
            let blade = alg.blade(&bits(mask));
            push_clifford_relation_row(alg.mul(&rel_mv, &blade), index, &mut rows);
            push_clifford_relation_row(alg.mul(&blade, &rel_mv), index, &mut rows);
        }
    }
    rows
}

fn push_clifford_relation_row(
    mv: Multivector<Integer>,
    index: &BTreeMap<u128, usize>,
    rows: &mut Vec<Vec<i128>>,
) {
    let mut row = vec![0i128; index.len()];
    for (blade, coeff) in mv.terms {
        if let Some(&i) = index.get(&blade) {
            row[i] += coeff.0;
        }
    }
    if row.iter().any(|&x| x != 0) {
        rows.push(row);
    }
}

fn all_blade_masks(n: usize) -> Vec<u128> {
    let mut out = Vec::new();
    for grade in 0..=n {
        out.extend(grade_masks(n, grade));
    }
    out
}