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
//! A permutation is a bijection of a set. Together with function composition
//! this forms a group.
//!
//! # Examples
//! A permutation is a `GroupElement` and can therefore be multiplied together.
//! From the following computation it is clear that we compose from left to
//! right.
//!
//! ```rust
//! use std::collections::HashMap;
//! use permutation_rs::group::GroupElement;
//! use permutation_rs::group::permutation::Permutation;
//!
//! let mut left_image = HashMap::new();
//! left_image.insert(0, 1);
//! left_image.insert(1, 0);
//! left_image.insert(2, 2);
//! let left = Permutation::new(left_image);
//!
//! let mut right_image = HashMap::new();
//! right_image.insert(0, 0);
//! right_image.insert(1, 2);
//! right_image.insert(2, 1);
//! let right = Permutation::new(right_image);
//!
//! let answer = left.times(&right);
//!
//! let mut expected_image = HashMap::new();
//! expected_image.insert(0, 2);
//! expected_image.insert(1, 0);
//! expected_image.insert(2, 1);
//! let expected = Permutation::new(expected_image);
//!
//! assert_eq!(answer, expected);
//! ```
//!
//! Writing the above permutations can get a bit unwieldy. You can use the
//! `permute!` macro to alleviate the tedium a bit. For example, the `left`
//! permutation could have been written like this
//!
//! ```rust
//! # #[macro_use] extern crate permutation_rs;
//! # use std::collections::HashMap;
//! # use permutation_rs::group::permutation::Permutation;
//! # fn main() {
//! let left = permute!(
//!     0, 1,
//!     1, 0,
//!     2, 2
//! );
//! # }
//! ```

use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::fmt::Display;
use super::{GroupElement, GroupAction};

#[macro_export]
macro_rules! permute {
    ( $($from: expr, $to: expr),* ) => {
        {
            let mut permutation_images = HashMap::new();
            $(
                permutation_images.insert($from, $to);
            )*
            Permutation::new(permutation_images)
        }
    }
}

/// A permutation of the set 0..n for a suitable choice of n.
#[derive(Debug, PartialEq, Clone)]
pub struct Permutation {
    n: usize,
    images: HashMap<u64, u64>,
}

impl Permutation {
    /// Create an permutation with a given image.
    pub fn new(images: HashMap<u64, u64>) -> Permutation {
        let n = images.len();
        Permutation { images: images, n: n }
    }
}

impl GroupElement for Permutation {
    fn is_identity(&self) -> bool {
        for i in 0..self.n {
            let original = i as u64;
            let image = self.images.get(&original).unwrap_or(&original).clone();
            if image != original {
                return false
            }
        }
        true
    }

    fn times(&self, multiplicant: &Permutation) -> Permutation {
        let max_n = if self.n > multiplicant.n { self.n } else { multiplicant.n };
        let mut images = HashMap::new();
        for i in 0..max_n {
            let original = i as u64;
            let mut image = self.images.get(&original).unwrap_or(&original).clone();
            image = multiplicant.images.get(&image).unwrap_or(&image).clone();
            images.insert(original, image);
        }
        Permutation::new(images)
    }

    fn inverse(&self) -> Permutation {
        let mut images = HashMap::new();
        for i in 0..self.n {
            let original = i as u64;
            let image = self.images.get(&original).unwrap_or(&original).clone();
            images.insert(image, original);
        }
        Permutation::new(images)
    }
}

impl GroupAction for Permutation {
    type Domain = u64;

    fn act_on(&self, original: &u64) -> u64 {
        self.images.get(&original).unwrap_or(&original).clone()
    }
}

impl Display for Permutation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let cycles: Vec<Vec<u64>> = cycles(self.n, &self.images);
        if cycles.len() > 0 {
            for cycle in cycles {
                let representations: Vec<String> =
                    cycle
                    .into_iter()
                    .map(|element| format!("{}", element))
                    .collect();
                let representation: String = representations.join(" ");
                write!(f, "(")?;
                write!(f, "{}", representation)?;
                write!(f, ")")?
            }
            write!(f, "")
        } else {
            write!(f, "Id")
        }
    }
}

fn cycles(n: usize, images: &HashMap<u64, u64>) -> Vec<Vec<u64>> {
    let mut cycles = vec!();
    let mut visited = HashSet::new();
    for i in 0..n {
        let original = i as u64;
        if !visited.contains(&original) {
            visited.insert(original.clone());
            let mut cycle = vec!(original.clone());
            let mut image = images.get(&original).unwrap_or(&original).clone();
            while !visited.contains(&image) {
                visited.insert(image.clone());
                cycle.push(image.clone());
                image = images.get(&image).unwrap_or(&image).clone();
            }
            if cycle.len() > 1 {
                cycles.push(cycle);
            }
        }
    }
    cycles
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use super::super::{GroupElement, GroupAction};
    use super::*;

    #[test]
    fn permutaion_should_know_when_it_is_the_identity() {
        let mut not_identity_images = HashMap::new();
        not_identity_images.insert(0u64, 1u64);
        not_identity_images.insert(1u64, 0u64);
        let not_identity = Permutation::new(not_identity_images);

        assert!(!not_identity.is_identity());

        let mut identity_images = HashMap::new();
        identity_images.insert(0u64, 0u64);
        identity_images.insert(1u64, 1u64);
        let identity = Permutation::new(identity_images);

        assert!(identity.is_identity());
    }

    #[test]
    fn multiplication_should_be_from_left_to_right() {
        let mut first_images = HashMap::new();
        first_images.insert(0u64, 1u64);
        first_images.insert(1u64, 0u64);
        first_images.insert(2u64, 2u64);
        let first = Permutation::new(first_images);

        let mut second_images = HashMap::new();
        second_images.insert(0u64, 0u64);
        second_images.insert(1u64, 2u64);
        second_images.insert(2u64, 1u64);
        let second = Permutation::new(second_images);

        let product = first.times(&second);

        let mut expected_images = HashMap::new();
        expected_images.insert(0u64, 2u64);
        expected_images.insert(1u64, 0u64);
        expected_images.insert(2u64, 1u64);
        let expected = Permutation::new(expected_images);

        assert_eq!(product, expected);
    }

    #[test]
    fn inverse_should_multiply_to_identity() {
        let mut first_images = HashMap::new();
        first_images.insert(0u64, 1u64);
        first_images.insert(1u64, 2u64);
        first_images.insert(2u64, 0u64);
        let first = Permutation::new(first_images);

        let second = first.inverse();

        let product = first.times(&second);

        assert!(product.is_identity());
    }

    #[test]
    fn permutation_should_act_upon_integers() {
        let mut permutation_images = HashMap::new();
        permutation_images.insert(0u64, 1u64);
        permutation_images.insert(1u64, 2u64);
        permutation_images.insert(2u64, 0u64);
        let permutation = Permutation::new(permutation_images);

        assert_eq!(permutation.act_on(&0u64), 1u64);
        assert_eq!(permutation.act_on(&1u64), 2u64);
        assert_eq!(permutation.act_on(&2u64), 0u64);
    }

    #[test]
    fn permutation_should_display_correctly() {
        let mut identity_images = HashMap::new();
        identity_images.insert(0u64, 0u64);
        identity_images.insert(1u64, 1u64);
        let identity = Permutation::new(identity_images);

        let mut permutation_images = HashMap::new();
        permutation_images.insert(0u64, 1u64);
        permutation_images.insert(1u64, 2u64);
        permutation_images.insert(2u64, 0u64);
        permutation_images.insert(3u64, 4u64);
        permutation_images.insert(4u64, 3u64);
        let permutation = Permutation::new(permutation_images);

        assert_eq!("Id", format!("{}", identity));
        assert_eq!("(0 1 2)(3 4)", format!("{}", permutation));
    }
}