geometric_rep_theory 0.1.3

Algebraic and geometric structures arising in mathematical physics and mirror symmetry.
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
use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use super::a_infinity::AInfinityAlgebra;
use crate::arithmetic_utils::Ring;
use crate::infinity_algebra::graded_module::GradedModule;

/// A differential graded algebra wrapping an underlying algebra `A` with a
/// differential given by `F: Fn(A) -> A`. The `Arc<F>` is the `Ctx` for this
/// type, shared among all elements of the same algebra instance.
///
/// - `m_1`: the differential `F`
/// - `m_2`: the multiplication on `A`
/// - `m_n` for `n ≥ 3`: zero
#[derive(Clone, Copy)]
pub struct DGA<A>
where
    A: Mul<A, Output = A>,
{
    pub value: A,
    pub differential: fn(A) -> A,
}

impl<A> PartialEq for DGA<A>
where
    A: Mul<A, Output = A> + Clone + PartialEq,
{
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<A> Eq for DGA<A> where A: Mul<A, Output = A> + Clone + Eq {}

impl<A> Add for DGA<A>
where
    A: Mul<A, Output = A> + Clone + Add<Output = A>,
{
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        Self {
            value: self.value + rhs.value,
            differential: self.differential,
        }
    }
}

impl<A> AddAssign for DGA<A>
where
    A: Mul<A, Output = A> + Clone + AddAssign,
{
    fn add_assign(&mut self, rhs: Self) {
        self.value += rhs.value;
    }
}

impl<A> Sub for DGA<A>
where
    A: Mul<A, Output = A> + Clone + Sub<Output = A>,
{
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        Self {
            value: self.value - rhs.value,
            differential: self.differential,
        }
    }
}

impl<A> SubAssign for DGA<A>
where
    A: Mul<A, Output = A> + Clone + SubAssign,
{
    fn sub_assign(&mut self, rhs: Self) {
        self.value -= rhs.value;
    }
}

impl<A> Neg for DGA<A>
where
    A: Mul<A, Output = A> + Clone + Neg<Output = A>,
{
    type Output = Self;
    fn neg(self) -> Self {
        Self {
            value: -self.value,
            differential: self.differential,
        }
    }
}

impl<A, Coeffs: Ring> Mul<Coeffs> for DGA<A>
where
    A: Mul<A, Output = A> + Clone + Mul<Coeffs, Output = A>,
{
    type Output = Self;
    fn mul(self, coeff: Coeffs) -> Self {
        Self {
            value: self.value * coeff,
            differential: self.differential,
        }
    }
}

impl<A, Coeffs: Ring> MulAssign<Coeffs> for DGA<A>
where
    A: Mul<A, Output = A> + Clone + MulAssign<Coeffs>,
{
    fn mul_assign(&mut self, coeff: Coeffs) {
        self.value *= coeff;
    }
}

impl<A, Coeffs> GradedModule<Coeffs> for DGA<A>
where
    A: Mul<A, Output = A> + Clone + GradedModule<Coeffs, Ctx = ()>,
    Coeffs: Ring,
{
    type Ctx = fn(A) -> A;
    fn extract_homogeneous(self, n: i64) -> (Self, Option<Self>) {
        let ctx = self.differential;
        let (homo, rest) = self.value.extract_homogeneous(n);
        let homo_dga = Self {
            value: homo,
            differential: ctx,
        };
        let rest_dga = rest.map(|v| Self {
            value: v,
            differential: ctx,
        });
        (homo_dga, rest_dga)
    }

    fn zero(ctx: Self::Ctx) -> Self {
        Self {
            value: A::zero(()),
            differential: ctx,
        }
    }

    fn ctx(&self) -> Self::Ctx {
        self.differential
    }
}

impl<A> Mul<DGA<A>> for DGA<A>
where
    A: Mul<A, Output = A> + Clone,
{
    type Output = Self;

    fn mul(self, rhs: DGA<A>) -> Self::Output {
        Self {
            value: self.value * rhs.value,
            differential: self.differential,
        }
    }
}

impl<A> DGA<A>
where
    A: Mul<A, Output = A> + Clone + Eq,
{
    #[must_use = "returns the algebra element resulting from taking the differential; discarding it loses the computed value"]
    pub fn diff(self) -> Self {
        let value = (self.differential)(self.value);
        Self {
            value,
            differential: self.differential,
        }
    }
}

impl<A, Coeffs> AInfinityAlgebra<Coeffs> for DGA<A>
where
    A: Mul<A, Output = A> + Clone + GradedModule<Coeffs, Ctx = ()>,
    Coeffs: Ring,
{
    fn max_nonzero_arity() -> Option<usize> {
        Some(2)
    }

    fn m_n_one_term_owned<const N: usize>(inputs: [Self; N]) -> Self {
        let mut iter = inputs.into_iter();
        match N {
            0 => unreachable!("N=0 (curvature) is not supported"),
            1 => {
                let input = iter.next().unwrap();
                let ctx = input.differential;
                Self {
                    value: (ctx)(input.value),
                    differential: ctx,
                }
            }
            2 => {
                let lhs = iter.next().unwrap();
                let rhs = iter.next().unwrap();
                let ctx = lhs.differential;
                Self {
                    value: lhs.value * rhs.value,
                    differential: ctx,
                }
            }
            _ => {
                let first = iter.next().unwrap();
                let ctx = first.differential;
                Self {
                    value: A::zero(()),
                    differential: ctx,
                }
            }
        }
    }

    fn m_n_one_term<const N: usize>(inputs: [&Self; N]) -> Self {
        let slice: &[&Self] = &inputs;
        match slice {
            [] => unreachable!("N=0 (curvature) is not supported"),
            [input] => {
                let ctx = input.differential;
                Self {
                    value: (ctx)(input.value.clone()),
                    differential: ctx,
                }
            }
            [lhs, rhs] => {
                let ctx = lhs.differential;
                Self {
                    value: lhs.value.clone() * rhs.value.clone(),
                    differential: ctx,
                }
            }
            [first, ..] => {
                let ctx = first.differential;
                Self {
                    value: A::zero(()),
                    differential: ctx,
                }
            }
        }
    }
}

/// `Coeffs[x,y]/(x^3,y^2)`
/// |y| = 1, |x| = 2
/// d(y) = x d(x) = 0
#[cfg(test)]
mod test_6d_example {
    use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

    use crate::{
        arithmetic_utils::Ring,
        infinity_algebra::{DGA, graded_module::GradedModule},
    };

    #[derive(PartialEq, Eq, Clone, Copy)]
    pub struct SmallGA<Coeffs: Ring>([Coeffs; 6]);

    impl<Coeffs: Ring> AddAssign for SmallGA<Coeffs> {
        fn add_assign(&mut self, rhs: Self) {
            self.0.iter_mut().zip(rhs.0).for_each(|(z, w)| {
                *z += w;
            });
        }
    }

    impl<Coeffs: Ring> Add for SmallGA<Coeffs> {
        type Output = Self;

        fn add(mut self, rhs: Self) -> Self::Output {
            self += rhs;
            self
        }
    }

    impl<Coeffs: Ring> SubAssign for SmallGA<Coeffs> {
        fn sub_assign(&mut self, rhs: Self) {
            self.0.iter_mut().zip(rhs.0).for_each(|(z, w)| {
                *z -= w;
            });
        }
    }

    impl<Coeffs: Ring> Sub for SmallGA<Coeffs> {
        type Output = Self;

        fn sub(mut self, rhs: Self) -> Self::Output {
            self -= rhs;
            self
        }
    }

    impl<Coeffs: Ring> Neg for SmallGA<Coeffs> {
        type Output = Self;

        fn neg(mut self) -> Self::Output {
            self.0.iter_mut().for_each(|z| *z = -z.clone());
            self
        }
    }

    impl<Coeffs: Ring> Mul for SmallGA<Coeffs> {
        type Output = Self;

        fn mul(self, rhs: Self) -> Self::Output {
            let self_1 = self.0[0].clone();
            let self_y = self.0[1].clone();
            let self_x = self.0[2].clone();
            let self_xy = self.0[3].clone();
            let self_x2 = self.0[4].clone();
            let self_x2y = self.0[5].clone();

            let rhs_1 = rhs.0[0].clone();
            let rhs_y = rhs.0[1].clone();
            let rhs_x = rhs.0[2].clone();
            let rhs_xy = rhs.0[3].clone();
            let rhs_x2 = rhs.0[4].clone();
            let rhs_x2y = rhs.0[5].clone();

            let mut to_return = core::array::from_fn(|_| Coeffs::zero());
            to_return[0] = self_1.clone() * rhs_1.clone();
            to_return[1] = self_1.clone() * rhs_y.clone() + self_y.clone() * rhs_1.clone();
            to_return[2] = self_1.clone() * rhs_x.clone() + self_x.clone() * rhs_1.clone();
            to_return[3] = self_1.clone() * rhs_xy.clone()
                + self_xy.clone() * rhs_1.clone()
                + self_x.clone() * rhs_y.clone()
                + self_y.clone() * rhs_x.clone();
            to_return[4] = self_1.clone() * rhs_x2.clone()
                + self_x2.clone() * rhs_1.clone()
                + self_x.clone() * rhs_x.clone();
            to_return[5] = self_1 * rhs_x2y
                + self_x2y * rhs_1
                + self_x2 * rhs_y
                + self_y * rhs_x2
                + self_x * rhs_xy
                + self_xy * rhs_x;

            Self(to_return)
        }
    }

    impl<Coeffs: Ring> MulAssign<Coeffs> for SmallGA<Coeffs> {
        fn mul_assign(&mut self, rhs: Coeffs) {
            self.0.iter_mut().for_each(|z| *z *= rhs.clone());
        }
    }

    impl<Coeffs: Ring> Mul<Coeffs> for SmallGA<Coeffs> {
        type Output = Self;

        fn mul(mut self, rhs: Coeffs) -> Self::Output {
            self *= rhs;
            self
        }
    }

    impl<Coeffs: Ring> GradedModule<Coeffs> for SmallGA<Coeffs> {
        type Ctx = ();
        fn extract_homogeneous(mut self, n: i64) -> (Self, Option<Self>) {
            if n < 0 {
                (SmallGA::zero(()), Some(self))
            } else if n > 6 {
                (SmallGA::zero(()), Some(self))
            } else {
                let n = n as usize;
                let mut just_n = SmallGA::zero(());
                just_n.0[n] = self.0[n].clone();
                self.0[n] = Coeffs::zero();
                if self.0.iter().all(Coeffs::is_zero) {
                    (just_n, None)
                } else {
                    (just_n, Some(self))
                }
            }
        }

        fn zero((): ()) -> Self {
            Self(core::array::from_fn(|_| Coeffs::zero()))
        }

        fn ctx(&self) -> Self::Ctx {}
    }

    fn diff<Coeffs: Ring>(x: SmallGA<Coeffs>) -> SmallGA<Coeffs> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[2] = x.0[1].clone();
        to_return[4] = x.0[3].clone();
        SmallGA(to_return)
    }

    fn small_dga_one<Coeffs: Ring>() -> DGA<SmallGA<Coeffs>> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[0] = Coeffs::one();
        DGA {
            value: SmallGA(to_return),
            differential: diff,
        }
    }

    fn small_dga_y<Coeffs: Ring>() -> DGA<SmallGA<Coeffs>> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[1] = Coeffs::one();
        DGA {
            value: SmallGA(to_return),
            differential: diff,
        }
    }

    fn small_dga_x<Coeffs: Ring>() -> DGA<SmallGA<Coeffs>> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[2] = Coeffs::one();
        DGA {
            value: SmallGA(to_return),
            differential: diff,
        }
    }

    fn small_dga_xy<Coeffs: Ring>() -> DGA<SmallGA<Coeffs>> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[3] = Coeffs::one();
        DGA {
            value: SmallGA(to_return),
            differential: diff,
        }
    }

    fn small_dga_x2<Coeffs: Ring>() -> DGA<SmallGA<Coeffs>> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[4] = Coeffs::one();
        DGA {
            value: SmallGA(to_return),
            differential: diff,
        }
    }

    fn small_dga_x2y<Coeffs: Ring>() -> DGA<SmallGA<Coeffs>> {
        let mut to_return = core::array::from_fn(|_| Coeffs::zero());
        to_return[5] = Coeffs::one();
        DGA {
            value: SmallGA(to_return),
            differential: diff,
        }
    }

    #[test]
    fn zero_times() {
        let zero_dga = DGA::<SmallGA<i64>>::zero(diff);
        let one_dga = small_dga_one();
        assert!(one_dga.clone() * one_dga.clone() - one_dga.clone() == zero_dga.clone());
        assert!(one_dga.clone().diff() == zero_dga.clone());
        let y_dga = small_dga_y();
        let x_dga = small_dga_x();
        let xy_dga = small_dga_xy();
        let x2_dga = small_dga_x2();
        let x2y_dga = small_dga_x2y();
        assert!(y_dga * zero_dga == zero_dga);
        assert!(x_dga * zero_dga == zero_dga);
        assert!(xy_dga * zero_dga == zero_dga);
        assert!(x2_dga * zero_dga == zero_dga);
        assert!(x2y_dga * zero_dga == zero_dga);

        assert!(zero_dga * y_dga == zero_dga);
        assert!(zero_dga * x_dga == zero_dga);
        assert!(zero_dga * xy_dga == zero_dga);
        assert!(zero_dga * x2_dga == zero_dga);
        assert!(zero_dga * x2y_dga == zero_dga);

        assert!(x_dga * x2y_dga == zero_dga);
        assert!(x_dga * x2_dga == zero_dga);
        assert!(y_dga * y_dga == zero_dga);
        assert!(y_dga * xy_dga == zero_dga);

        assert!(y_dga.diff() * xy_dga == x2y_dga);
    }

    #[test]
    fn evaluate_two_level_tree() {
        use crate::infinity_algebra::tensor_power::TensorPower;
        use crate::infinity_algebra::{AInfinityAlgebra, OperationTree};

        // tree: m_2(leaf, m_2(leaf, leaf))
        // The inner m_2 node sits at absolute range 1..3.
        // A sub-slicing bug would pass inputs[1..3] to the inner node
        // and then try to index that sub-slice with the absolute ranges
        // 1..2 and 2..3, giving wrong values or panicking.
        let tree = OperationTree::node(vec![
            OperationTree::leaf(),
            OperationTree::node(vec![OperationTree::leaf(), OperationTree::leaf()]),
        ]);
        assert_eq!(tree.num_leaves(), 3);

        // m_2(x, m_2(y, 1)) = x * (y * 1) = x * y = xy
        let combination =
            TensorPower::from_pure_tensor([small_dga_x(), small_dga_y(), small_dga_one()], 1i64);
        let result = DGA::<SmallGA<i64>>::evaluate(diff, &tree, combination).unwrap();
        assert!(result == small_dga_xy());
    }
}