use super::{GroupError, Permutation, PermutationGroup};
pub fn trivial(degree: usize) -> PermutationGroup {
PermutationGroup::new(degree, Vec::new()).expect("no generators, so no degree to mismatch")
}
pub fn symmetric(n: usize) -> Result<PermutationGroup, GroupError> {
let mut generators = Vec::new();
if n >= 2 {
generators.push(Permutation::from_cycles(n, &[vec![0, 1]])?);
}
if n >= 3 {
generators.push(Permutation::from_cycles(n, &[(0..n).collect()])?);
}
PermutationGroup::new(n, generators)
}
pub fn alternating(n: usize) -> Result<PermutationGroup, GroupError> {
let mut generators = Vec::new();
if n >= 3 {
generators.push(Permutation::from_cycles(n, &[vec![0, 1, 2]])?);
}
if n >= 4 {
let long: Vec<usize> = if n % 2 == 1 {
(0..n).collect()
} else {
(1..n).collect()
};
generators.push(Permutation::from_cycles(n, &[long])?);
}
PermutationGroup::new(n, generators)
}
pub fn cyclic(n: usize) -> Result<PermutationGroup, GroupError> {
if n == 0 {
return Err(GroupError::UnsupportedDegree {
family: "cyclic",
requested: n,
reason: "n must be at least 1; there is no group of order 0",
});
}
let generators = if n == 1 {
Vec::new()
} else {
vec![Permutation::from_cycles(n, &[(0..n).collect()])?]
};
PermutationGroup::new(n, generators)
}
pub fn dihedral(n: usize) -> Result<PermutationGroup, GroupError> {
if n < 3 {
return Err(GroupError::UnsupportedDegree {
family: "dihedral",
requested: n,
reason: "the action of D_n on n vertices is faithful only for n >= 3; for n = 1 \
or 2 it would return a group of order less than 2n",
});
}
let rotation = Permutation::from_cycles(n, &[(0..n).collect()])?;
let reflection = Permutation::from_images((0..n).map(|i| (n - i) % n).collect())?;
PermutationGroup::new(n, vec![rotation, reflection])
}