finitediff
Mpermutation.h
Go to the documentation of this file.
1 /*
2 Copyright 2017 Laurent Claessens
3 contact : laurent@claessens-donadello.eu
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef __MPERMUTATION_H_121119__
20 #define __MPERMUTATION_H_121119__
21 
22 #include <array>
23 
24 #include "MgenericPermutation.h"
25 #include "MelementaryPermutation.h"
26 
27 // THE CLASS HEADER -----------------------------------------
28 
42 template <unsigned int tp_size>
43 class Mpermutation : public MgenericPermutation<tp_size>
44 {
45 
46  template <unsigned int s>
47  friend std::ostream& operator<<(std::ostream&, Mpermutation<s>);
48  template <unsigned int s>
49  friend bool operator==(const Mpermutation<s>&,const Mpermutation<s>&);
50 
51  private:
52  std::array<unsigned int,tp_size> data;
53  public :
54  Mpermutation(const std::array<unsigned int,tp_size>& d);
56 
58  Mpermutation();
59 
61  unsigned int image(const unsigned int k) const override;
62 
68  unsigned int& at(const unsigned int k);
69 
72 };
73 
74 
75 // GETTER METHODS ----------------------------
76 
77 template <unsigned int tp_size>
78 unsigned int& Mpermutation<tp_size>::at(const unsigned int k)
79 {
80  return data.at(k);
81 }
82 
83 // CONSTRUCTOR ----------------------------
84 
85 template <unsigned int tp_size>
86 Mpermutation<tp_size>::Mpermutation(const std::array<unsigned int,tp_size>& d) :
87  data(d)
88 {
89  for (unsigned int k=0;k<tp_size;++k)
90  {
91  if (d.at(k)>tp_size-1) // Mpermutation<4> permutes the set {0,1,2,3}.
92  {
93  throw PermutationIdexoutOfRangeException(k,tp_size);
94  }
95  }
96 }
97 
98 template <unsigned int tp_size>
100 {
101  this->at( p.getA() )=p.getB();
102  this->at( p.getB() )=p.getA();
103 }
104 
105 template <unsigned int tp_size>
107 {
108  for (unsigned int k=0;k<tp_size;++k)
109  {
110  at(k)=k;
111  }
112 }
113 
114 // OPERATORS -------------------------------
115 template <unsigned int s>
116 std::ostream& operator<<(std::ostream& stream, Mpermutation<s> perm)
117 {
118  for (unsigned int l=0;l<s;l++)
119  {
120  stream<<l<<"->"<<perm.data.at(l)<<std::endl;
121  }
122  return stream;
123 }
124 
125 
126 // MATHEMATICS -------------------------------
127 
128 template <unsigned int tp_size>
129 unsigned int Mpermutation<tp_size>::image(const unsigned int k) const
130 {
131  if (k>tp_size)
132  {
133  throw PermutationIdexoutOfRangeException(k,tp_size);
134  }
135  return data.at(k);
136 }
137 
138 
139 template <unsigned int tp_size>
141 {
143  for (unsigned int k=0;k<tp_size;++k)
144  {
145  inv.at( this->image(k) )=k;
146  }
147  return inv;
148 }
149 
150 #endif
Mpermutation()
Definition: Mpermutation.h:106
A permutation is a bijection of a finite subset of N.
Definition: MgenericPermutation.h:48
unsigned int getA() const
Return the first of the two elements that are permuted.
Definition: MelementaryPermutation.h:73
friend bool operator==(const Mpermutation< s > &, const Mpermutation< s > &)
std::array< unsigned int, tp_size > data
Definition: Mpermutation.h:52
unsigned int & at(const unsigned int k)
return by reference the image of &#39;k&#39; by the permutation
Definition: Mpermutation.h:78
unsigned int image(const unsigned int k) const override
Definition: Mpermutation.h:129
Definition: MelementaryPermutation.h:34
unsigned int getB() const
Return the second of the two elements that are permuted.
Definition: MelementaryPermutation.h:79
Mpermutation< tp_size > inverse() const
Definition: Mpermutation.h:140
Definition: SNexceptions.cpp:177
This class represents a permutation (not a matrix).
Definition: MelementaryPermutation.h:26