algebraeon_groups/composition_table/
subgroup.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
use std::collections::BTreeSet;
use std::collections::HashSet;

use super::group::*;
use super::normal_subgroup::*;
use super::partition::*;
use super::subset::*;
use std::hash::Hash;

#[derive(Clone)]
pub struct Subgroup<'a> {
    pub subset: Subset<'a>,
}

impl<'a> PartialEq for Subgroup<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.subset == other.subset
    }
}

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

impl<'a> Hash for Subgroup<'a> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.subset.hash(state);
    }
}

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

        if !self.subset.is_subgroup() {
            return Err("subgroup is not closed under composition");
        }

        Ok(())
    }

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

    pub fn to_group(&self) -> Group {
        let sg_elems: Vec<usize> = self.subset.elems().clone().into_iter().collect();
        let k = sg_elems.len();
        let mut group_to_subgroup: Vec<Option<usize>> = vec![None; self.subset.group().size()];
        for (i, x) in sg_elems.iter().enumerate() {
            group_to_subgroup[*x] = Some(i);
        }
        //TODO: add a test that this group has valid structure
        Group::new_unchecked(
            self.size(),
            group_to_subgroup[self.subset.group().ident()].unwrap(),
            (0..k)
                .map(|x| group_to_subgroup[self.subset.group().inv(sg_elems[x])].unwrap())
                .collect(),
            (0..k)
                .map(|x| {
                    (0..k)
                        .map(|y| {
                            group_to_subgroup[self.subset.group().mul(sg_elems[x], sg_elems[y])]
                                .unwrap()
                        })
                        .collect()
                })
                .collect(),
            None,
            None,
        )
    }

    pub fn left_cosets(&self) -> Partition {
        let mut cosets: Vec<BTreeSet<usize>> = vec![];
        let mut coset_lookup = vec![0; self.subset.group().size()];
        let mut missing: HashSet<usize> = self.subset.group().elems().collect();
        while missing.len() > 0 {
            let g = missing.iter().next().unwrap();
            let coset = self.subset.left_mul(*g).unwrap().elems().clone();
            for h in &coset {
                missing.remove(h);
                coset_lookup[*h] = cosets.len();
            }
            cosets.push(coset);
        }
        Partition {
            group: self.subset.group(),
            state: PartitionState::new_unchecked(cosets, coset_lookup),
        }
    }

    pub fn right_cosets(&self) -> Partition {
        let mut cosets: Vec<BTreeSet<usize>> = vec![];
        let mut coset_lookup = vec![0; self.subset.group().size()];
        let mut missing: HashSet<usize> = self.subset.group().elems().collect();
        while missing.len() > 0 {
            let g = missing.iter().next().unwrap();
            let coset = self.subset.right_mul(*g).unwrap().elems().clone();
            for h in &coset {
                missing.remove(h);
                coset_lookup[*h] = cosets.len();
            }
            cosets.push(coset);
        }
        Partition {
            group: self.subset.group(),
            state: PartitionState::new_unchecked(cosets, coset_lookup),
        }
    }

    pub fn is_normal_subgroup(&self) -> bool {
        for x in self.subset.elems() {
            for g in self.subset.group().elems() {
                if !self.subset.elems().contains(
                    &self
                        .subset
                        .group()
                        .mul(self.subset.group().mul(g, *x), self.subset.group().inv(g)),
                )
                //gxg^{-1}
                {
                    return false;
                }
            }
        }
        true
    }

    pub fn to_normal_subgroup(&self) -> Option<NormalSubgroup> {
        match self.is_normal_subgroup() {
            true => Some(NormalSubgroup::new_unchecked(self.clone())),
            false => None,
        }
    }
}

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

    #[test]
    fn subgroup_counts() {
        for (grp, num_sgs) in vec![
            (examples::cyclic_group_structure(12), 6),
            (examples::cyclic_group_structure(13), 2),
            (examples::dihedral_group_structure(1), 2),
            (examples::dihedral_group_structure(12), 34),
            (examples::dihedral_group_structure(13), 16),
            (examples::symmetric_group_structure(1), 1),
            (examples::symmetric_group_structure(2), 2),
            (examples::symmetric_group_structure(3), 6),
            (examples::symmetric_group_structure(4), 30),
            (examples::symmetric_group_structure(5), 156),
        ] {
            assert_eq!(grp.subgroups().len(), num_sgs);
        }
    }

    #[test]
    fn subgroup_state() {
        //are the states of subgroups, normal subgroups, and generating sets correct when produced by grp.subgroups() and grp.normal_subgroups()?
        for grp in vec![
            examples::symmetric_group_structure(4),
            examples::dihedral_group_structure(12),
            examples::cyclic_group_structure(8),
        ] {
            let sgs = grp.subgroups();
            for (sg, gens) in sgs {
                // sg.is_normal_subgroup(); //cache is to check if the coset partition has the right is_congruence value
                sg.check_state().unwrap();
                gens.check_state().unwrap();
                // sg.left_cosets().unwrap().check_state().unwrap();
                // sg.right_cosets().unwrap().check_state().unwrap();
            }
        }
    }

    #[test]
    fn subgroup_left_cosets() {
        use crate::examples::symmetric::Permutation;

        let (grp, _perms, elems) = Permutation::<3>::symmetric_composition_table();
        let sg = Subgroup {
            subset: Subset::new_unchecked(
                &grp,
                vec![
                    elems[&Permutation::new([0, 1, 2]).unwrap()],
                    elems[&Permutation::new([1, 0, 2]).unwrap()],
                ]
                .into_iter()
                .collect(),
            ),
        };
        sg.check_state().unwrap();
        let cosets = sg.left_cosets();
        cosets.check_state().unwrap();
        assert_eq!(cosets.size(), 3);
        assert!(
            cosets
                == Partition::from_subsets(
                    &grp,
                    vec![
                        vec![
                            elems[&Permutation::new([0, 1, 2]).unwrap()],
                            elems[&Permutation::new([1, 0, 2]).unwrap()],
                        ]
                        .into_iter()
                        .collect(),
                        vec![
                            elems[&Permutation::new([1, 2, 0]).unwrap()],
                            elems[&Permutation::new([2, 1, 0]).unwrap()],
                        ]
                        .into_iter()
                        .collect(),
                        vec![
                            elems[&Permutation::new([2, 0, 1]).unwrap()],
                            elems[&Permutation::new([0, 2, 1]).unwrap()],
                        ]
                        .into_iter()
                        .collect()
                    ]
                )
                .unwrap()
        );
    }

    #[test]
    fn subgroup_right_cosets() {
        use crate::examples::symmetric::Permutation;

        let (grp, _perms, elems) = Permutation::<3>::symmetric_composition_table();
        let sg = Subgroup {
            subset: Subset::new_unchecked(
                &grp,
                vec![
                    elems[&Permutation::new([0, 1, 2]).unwrap()],
                    elems[&Permutation::new([1, 0, 2]).unwrap()],
                ]
                .into_iter()
                .collect(),
            ),
        };
        sg.check_state().unwrap();
        let cosets = sg.right_cosets();
        cosets.check_state().unwrap();
        assert_eq!(cosets.size(), 3);
        assert!(
            cosets
                == Partition::from_subsets(
                    &grp,
                    vec![
                        vec![
                            elems[&Permutation::new([0, 1, 2]).unwrap()],
                            elems[&Permutation::new([1, 0, 2]).unwrap()],
                        ]
                        .into_iter()
                        .collect(),
                        vec![
                            elems[&Permutation::new([1, 2, 0]).unwrap()],
                            elems[&Permutation::new([0, 2, 1]).unwrap()],
                        ]
                        .into_iter()
                        .collect(),
                        vec![
                            elems[&Permutation::new([2, 0, 1]).unwrap()],
                            elems[&Permutation::new([2, 1, 0]).unwrap()],
                        ]
                        .into_iter()
                        .collect()
                    ]
                )
                .unwrap()
        );
    }
}