algebraeon_groups/composition_table/
partition.rs

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
use std::collections::{BTreeSet, HashSet};

use super::group::*;
use super::subset::*;

#[derive(Clone, Debug)]
pub struct PartitionState {
    classes: Vec<BTreeSet<usize>>, //vector of conjugacy classes
    lookup: Vec<usize>,            //for each element, the index of its conjugacy class
}

impl PartitionState {
    pub fn check_state(&self, group: &Group) -> Result<(), &'static str> {
        //is a partition
        let mut accounted_elems: HashSet<usize> = HashSet::new();
        for class in &self.classes {
            if class.len() == 0 {
                return Err("partition contains an empty set");
            }
            for x in class {
                if accounted_elems.contains(x) {
                    return Err("partition contains duplicate elements");
                }
                accounted_elems.insert(*x);
            }
        }
        if accounted_elems.len() != group.size() {
            return Err("partition is missing some elements");
        }

        //lookup is correct
        if !(self.lookup.len() == group.size()) {
            return Err("partition lookup has the wrong length");
        }
        for (elem, class_idx) in self.lookup.iter().enumerate() {
            if !(*class_idx < self.classes.len()) {
                return Err("partition lookup index is bigger partition size");
            }
            if !self.classes[*class_idx].contains(&elem) {
                return Err("partition lookup points to wrong partition set");
            }
        }
        Ok(())
    }

    pub fn new_unchecked(classes: Vec<BTreeSet<usize>>, lookup: Vec<usize>) -> Self {
        Self { classes, lookup }
    }

    pub fn project(&self, x: usize) -> &BTreeSet<usize> {
        &self.classes[self.lookup[x]]
    }
}

pub struct Partition<'a> {
    pub group: &'a Group,
    pub state: PartitionState,
    // is_left_cosets: Option<bool>,
    // is_right_cosets: Option<bool>,
}

impl<'a> PartialEq for Partition<'a> {
    fn eq(&self, other: &Self) -> bool {
        let grp = self.group;
        if !std::ptr::eq(grp, other.group) {
            return false;
        }
        let n = self.size();
        if n != other.size() {
            return false;
        }
        //check equality by comparing lookup lists
        //equal iff lookups are equal up to permutation
        //build up a permutation f from indicies of self.lookup to indicies of other.lookup
        let mut f: Vec<Option<usize>> = vec![None; n];
        for i in 0..grp.size() {
            match f[self.state.lookup[i]] {
                Some(expected_other_lookup_i) => {
                    if expected_other_lookup_i != other.state.lookup[i] {
                        return false;
                    }
                }
                None => {
                    f[self.state.lookup[i]] = Some(other.state.lookup[i]);
                }
            }
        }
        true
    }
}

impl<'a> Eq for Partition<'a> {}

impl<'a> Partition<'a> {
    pub fn check_state(&self) -> Result<(), &'static str> {
        match self.state.check_state(self.group) {
            Err(msg) => {
                return Err(msg);
            }
            Ok(()) => {}
        };

        Ok(())
    }

    pub fn size(&self) -> usize {
        self.state.classes.len()
    }

    pub fn is_left_cosets(&self) -> bool {
        let ident_class = Subset::new_unchecked(
            self.group,
            self.state.classes[self.state.lookup[self.group.ident()]].clone(),
        );
        match ident_class.to_subgroup() {
            Some(ident_subgroup) => &ident_subgroup.left_cosets() == self,
            None => false,
        }
    }

    pub fn is_right_cosets(&self) -> bool {
        let ident_class = Subset::new_unchecked(
            self.group,
            self.state.classes[self.state.lookup[self.group.ident()]].clone(),
        );
        match ident_class.to_subgroup() {
            Some(ident_subgroup) => &ident_subgroup.right_cosets() == self,
            None => false,
        }
    }

    pub fn is_congruence(&self) -> bool {
        self.is_left_cosets() && self.is_right_cosets()
    }

    pub fn from_subsets(group: &'a Group, subsets: Vec<BTreeSet<usize>>) -> Result<Self, ()> {
        let classes = subsets;
        let mut lookup = vec![0; group.size()];
        for (class_idx, class) in classes.iter().enumerate() {
            for x in class {
                if !(*x < group.size()) {
                    return Err(());
                }
                lookup[*x] = class_idx;
            }
        }
        let partition = Self {
            group,
            state: PartitionState { classes, lookup },
        };
        match partition.check_state() {
            Ok(_) => {}
            Err(_) => {
                return Err(());
            }
        }
        Ok(partition)
    }
}

pub struct Congruence<'a> {
    pub partition: Partition<'a>,
}

impl<'a> Congruence<'a> {
    pub fn check_state(&self) -> Result<(), &'static str> {
        match self.partition.check_state() {
            Ok(()) => {}
            Err(msg) => {
                return Err(msg);
            }
        }

        match self.partition.is_congruence() {
            false => return Err("congruence is not a congruence"),
            true => {}
        }

        Ok(())
    }

    pub fn size(&self) -> usize {
        self.partition.size()
    }

    pub fn quotient_group(&self) -> Group {
        let n = self.size();

        Group::new_unchecked(
            n,
            self.partition.state.lookup[self.partition.group.ident()],
            {
                let mut inv = vec![0; n];
                for i in 0..n {
                    inv[i] = self.partition.state.lookup[self
                        .partition
                        .group
                        .inv(*self.partition.state.classes[i].iter().next().unwrap())];
                }
                inv
            },
            {
                let mut mul = vec![vec![0; n]; n];
                for i in 0..n {
                    for j in 0..n {
                        mul[i][j] = self.partition.state.lookup[self.partition.group.mul(
                            *self.partition.state.classes[i].iter().next().unwrap(),
                            *self.partition.state.classes[j].iter().next().unwrap(),
                        )];
                    }
                }
                mul
            },
            None,
            None,
        )
    }
}

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

    #[test]
    fn partition_check_bad_state() {
        let grp = examples::cyclic_group_structure(6);

        //elements too big
        let p = PartitionState {
            classes: vec![
                vec![0, 1, 2, 3].into_iter().collect(),
                vec![4, 5, 6].into_iter().collect(),
            ],
            lookup: vec![0, 0, 0, 0, 1, 1, 1],
        };
        match p.check_state(&grp) {
            Ok(()) => assert!(false),
            Err(_) => {}
        }

        //not a covering set
        let p = PartitionState {
            classes: vec![
                vec![0, 2].into_iter().collect(),
                vec![3, 5].into_iter().collect(),
            ],
            lookup: vec![0, 0, 0, 1, 1, 1],
        };
        match p.check_state(&grp) {
            Ok(()) => assert!(false),
            Err(_) => {}
        }

        //not disjoint
        let p = PartitionState {
            classes: vec![
                vec![0, 1, 2, 3].into_iter().collect(),
                vec![2, 3, 4, 5].into_iter().collect(),
            ],
            lookup: vec![0, 0, 0, 0, 1, 1],
        };
        match p.check_state(&grp) {
            Ok(()) => assert!(false),
            Err(_) => {}
        }

        //lookup values too big
        let p = PartitionState {
            classes: vec![
                vec![0, 1, 2].into_iter().collect(),
                vec![3, 4, 5].into_iter().collect(),
            ],
            lookup: vec![0, 0, 0, 1, 1, 2],
        };
        match p.check_state(&grp) {
            Ok(()) => assert!(false),
            Err(_) => {}
        }

        //incorrect lookup values
        let p = PartitionState {
            classes: vec![
                vec![0, 1, 2].into_iter().collect(),
                vec![3, 4, 5].into_iter().collect(),
            ],
            lookup: vec![0, 0, 1, 1, 1, 1],
        };
        match p.check_state(&grp) {
            Ok(()) => assert!(false),
            Err(_) => {}
        }
    }

    #[test]
    fn congruence_check_state() {
        let grp = examples::symmetric_group_structure(4);
        for (sg, _gens) in grp.subgroups() {
            match sg.to_normal_subgroup() {
                None => {
                    let cong: Congruence<'_> = Congruence {
                        partition: sg.left_cosets(),
                    };
                    match cong.check_state() {
                        Ok(_) => panic!(),
                        Err(_) => {}
                    }
                    let cong: Congruence<'_> = Congruence {
                        partition: sg.right_cosets(),
                    };
                    match cong.check_state() {
                        Ok(_) => panic!(),
                        Err(_) => {}
                    }
                }
                Some(nsg) => {
                    let cong = nsg.cosets();
                    match cong.check_state() {
                        Ok(_) => {}
                        Err(_) => panic!(),
                    }
                }
            }
        }
    }
}