Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
dvberkel committed Oct 27, 2019
1 parent acd6644 commit 39472b5
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 201 deletions.
22 changes: 11 additions & 11 deletions src/group/calculation.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! A module that provides various group related calculations.

use std::collections::VecDeque;
use super::GroupElement;
use super::permutation::Permutation;
use super::GroupElement;
use std::collections::VecDeque;

/// Calculates the permutations generated by a set of generators.
///
/// Note that it uses a naive implementation that stores every permutation it
/// visits.
pub fn elements_generated_by(generators: &Vec<Permutation>) -> Vec<Permutation> {
let mut elements: Vec<Permutation> = vec!();
let mut elements: Vec<Permutation> = vec![];
let mut to_visit: VecDeque<Permutation> = VecDeque::new();
to_visit.push_back(identity(generators));

Expand All @@ -30,28 +30,28 @@ pub fn elements_generated_by(generators: &Vec<Permutation>) -> Vec<Permutation>

/// Calculate an identity element for a set of generators. Assume that set is
/// non empty, panics otherwise.
pub fn identity<G>(generators: &Vec<G>) -> G where G: GroupElement {
pub fn identity<G>(generators: &Vec<G>) -> G
where
G: GroupElement,
{
let g = generators.get(0).expect("at least one generator");
let inverse = g.inverse();
g.times(&inverse)
}


/// Calculate the nth factorial number.
///
/// The n! is defined as n * (n-1) * ... * 1
pub fn fact(m: u64) -> u64 {
(1..m)
.map(|n| n + 1)
.fold(1u64, |acc, n| acc * n)
(1..m).map(|n| n + 1).fold(1u64, |acc, n| acc * n)
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::super::GroupElement;
use super::super::permutation::Permutation;
use super::super::GroupElement;
use super::*;
use std::collections::HashMap;

#[test]
fn elements_generated_by_should_calculate_group_elements() {
Expand All @@ -62,7 +62,7 @@ mod tests {
let rotation = Permutation::new(rotation_images);
let id = rotation.times(&rotation.inverse());

let elements = elements_generated_by(&vec!(rotation.clone()));
let elements = elements_generated_by(&vec![rotation.clone()]);

assert!(elements.contains(&id));
assert!(elements.contains(&rotation));
Expand Down
37 changes: 22 additions & 15 deletions src/group/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
//! let expected = Word::new(vec![('a', 1), ('b', 2), ('c', 1)]);
//! assert_eq!(answer, expected);
//! ```
use super::GroupElement;
use std::fmt;
use std::fmt::Display;
use super::GroupElement;

/// The element of a free group.
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
Expand All @@ -30,33 +30,40 @@ pub struct Word {
impl Word {
/// Create the identity element in a free group.
pub fn identity() -> Word {
Word::new(vec!())
Word::new(vec![])
}

/// Constructor which creates a single generator.
pub fn generator(symbol: char) -> Word {
Word::new(vec!((symbol, 1)))
Word::new(vec![(symbol, 1)])
}

/// Create a word with prescribed characters.
pub fn new(elements: Vec<(char, i64)>) -> Word {
Word { terms : normalize(&elements) }
Word {
terms: normalize(&elements),
}
}
}


fn normalize(elements: &Vec<(char, i64)>) -> Vec<(char, i64)> {
let mut not_normalized : Vec<(char, i64)> = vec!();
let mut not_normalized: Vec<(char, i64)> = vec![];
not_normalized.extend(elements);

if not_normalized.len() <= 1 {
not_normalized
} else {
let mut normalized : Vec<(char, i64)> = vec!();
let mut current: (char, i64) = not_normalized.get(0).expect("at least two elements").clone();
let mut normalized: Vec<(char, i64)> = vec![];
let mut current: (char, i64) = not_normalized
.get(0)
.expect("at least two elements")
.clone();
let mut index = 1;
while index < not_normalized.len() {
let primitive = not_normalized.get(index).expect("index within bound").clone();
let primitive = not_normalized
.get(index)
.expect("index within bound")
.clone();
if current.0 == primitive.0 {
current = (current.0.clone(), current.1 + primitive.1)
} else {
Expand Down Expand Up @@ -86,15 +93,15 @@ impl GroupElement for Word {
}

fn times(&self, multiplicant: &Word) -> Word {
let mut terms: Vec<(char, i64)>= vec!();
let mut terms: Vec<(char, i64)> = vec![];
terms.extend(&self.terms);
terms.extend(&multiplicant.terms);
let terms = normalize(&terms);
Word { terms: terms }
}

fn inverse(&self) -> Word {
let mut terms: Vec<(char, i64)> = vec!();
let mut terms: Vec<(char, i64)> = vec![];
terms.extend(&self.terms);
terms.reverse();
for element in terms.iter_mut() {
Expand Down Expand Up @@ -140,27 +147,27 @@ mod tests {

let product = first.times(&second);

let expected = Word::new(vec!(('g', 1), ('h',1)));
let expected = Word::new(vec![('g', 1), ('h', 1)]);

assert_eq!(product, expected);
}

#[test]
fn inverse_should_multiply_to_identity() {
let first = Word::new(vec!(('g', 1), ('h',1)));
let first = Word::new(vec![('g', 1), ('h', 1)]);

let second = first.inverse();

let product = first.times(&second);

assert!(product.is_identity());
}

#[test]
fn word_should_display_correctly() {
let identity = Word::identity();

let word = Word::new(vec!(('x', 2), ('y', -3), ('x', -2), ('y', 3)));
let word = Word::new(vec![('x', 2), ('y', -3), ('x', -2), ('y', 3)]);

assert_eq!("Id", format!("{}", identity));
assert_eq!("x^2y^-3x^-2y^3", format!("{}", word));
Expand Down

0 comments on commit 39472b5

Please sign in to comment.