MINT2
Permutation.cpp
Go to the documentation of this file.
1 // author: Jonas Rademacker (Jonas.Rademacker@bristol.ac.uk)
2 // status: Mon 9 Feb 2009 19:18:13 GMT
3 #include "Mint/Permutation.h"
4 
5 using namespace std;
6 
7 int Permutation::dummy = -9999;
9 
11  if(0 == __unit){
12  __unit = new Permutation();
13  __unit->makeUnity();
14  }
15  return *__unit;
16 }
17 
18 
19 int& Permutation::y_of_x(int i){
20  if(i >= (int) _v.size() || i < 0){
21  cout << "Permutation::y_of_x for x = " << i
22  << " out of range [0," << (_v.size()-1) << "]."
23  << endl;
24  return dummy;
25  }
26  return _v[i];
27 }
28 const int& Permutation::y_of_x(int i) const{
29  if(i >= (int) _v.size() || i < 0){
30  cout << "Permutation::y_of_x for x = " << i
31  << " out of range [0," << (_v.size()-1) << "]."
32  << endl;
33  return dummy;
34  }
35  return _v[i];
36 }
37 
38 void Permutation::reset(int n){
39  _v.resize(n);
40  makeUnity();
41 }
43  for(unsigned int i=0; i < size(); i++){
44  _v[i] = i;
45  }
46 }
47 bool Permutation::isUnity() const{
48  for(unsigned int i=0; i < size(); i++){
49  if (_v[i] != (int) i) return false;
50  }
51  return true;
52 }
53 
54 void Permutation::swap(int x1, int x2, int sgn){
55  int tmp = y_of_x(x1);
56  y_of_x(x1) = y_of_x(x2);
57  y_of_x(x2) = tmp;
58  _sign *= sgn;
59 }
60 
61 void Permutation::set(const std::vector<int>& v, int sgn){
62  _v=v;
63  _sign =sgn;
64 }
65 
66 void Permutation::set(int a){
67  _v.resize(1); (*this)[0]=a;
68 }
69 void Permutation::set(int a, int b){
70  _v.resize(2); (*this)[0]=a; (*this)[1]=b;
71 }
72 void Permutation::set(int a, int b, int c){
73  _v.resize(3); (*this)[0]=a; (*this)[1]=b; (*this)[2]=c;
74 }
75 void Permutation::set(int a, int b, int c, int d){
76  _v.resize(4); (*this)[0]=a; (*this)[1]=b; (*this)[2]=c; (*this)[3]=d;
77 }
78 void Permutation::set(int a, int b, int c, int d, int e){
79  _v.resize(4); (*this)[0]=a; (*this)[1]=b; (*this)[2]=c; (*this)[3]=d; (*this)[4]=e;
80 }
82  _sign = s;
83 }
85  Permutation newP(*this);
86  for(unsigned int i=0; i< this->size(); i++){
87  int newIndex = (*this)[i];
88  if(newIndex < 0 || newIndex >= (int) this->size()){
89  cout << "ERROR in Permutation::getInverse(): "
90  << " found that I am an invalid permutation when trying to"
91  << " invert myself. This is me:\n" << *this << endl;
92  cout << " Will carry on as best as I can..." <<endl;
93  continue;
94  }
95  newP[newIndex] = i;
96  }
97  return newP;
98 }
99 
101  int n=0;
102  for(unsigned int i=0; i< size()-1; ++i){
103  for(unsigned int j=i+1; j< size(); j++){
104  if( y_of_x(i) > y_of_x(j) ) ++n;
105  }
106  }
107  return n;
108 }
109 
110 void Permutation::print(std::ostream& os) const{
111 
112  for(unsigned int i=0; i < size(); i++){
113  os << '\t' << i;
114  }
115  os << "\n ->";
116  for(unsigned int i=0; i < size(); i++){
117  os << '\t' << (*this)[i];
118  }
119 
120 }
121 
123  unsigned int newSize = this->size();
124  if(rhs.size() > newSize) newSize = rhs.size();
125  Permutation newP(newSize);
126 
127  for(unsigned int i=0; i < newSize; i++){
128  int rhsMapping = -1;
129  if(i >= rhs.size()){
130  rhsMapping = i;
131  }else{
132  rhsMapping = rhs[i];
133  }
134  if(rhsMapping > (int) this->size() || rhsMapping < 0){
135  newP[i] = rhsMapping;
136  }else{
137  newP[i] = (*this)[rhsMapping];
138  }
139  }
140  return newP;
141 }
142 
144  return (*this)* rhs.getInverse();
145 }
146 
147 bool Permutation::operator<(const Permutation& rhs) const{
148 
149  if(this->size() < rhs.size()) return true;
150  if(this->size() > rhs.size()) return false;
151 
152  for(unsigned int i=0; i< this->size(); i++){
153  if( (*this)[i] < rhs[i] ) return true;
154  if( (*this)[i] > rhs[i] ) return false;
155  }
156  return false;
157 }
158 
159 bool Permutation::operator==(const Permutation& rhs) const{
160 
161  if(this->size() != rhs.size()) return false;
162 
163  for(unsigned int i=0; i< this->size(); i++){
164  if( (*this)[i] != rhs[i] ) return false;
165  }
166  return true;
167 }
168 bool Permutation::operator<=(const Permutation& rhs) const{
169  return ( (*this) < rhs || (*this) == rhs);
170 }
171 bool Permutation::operator>(const Permutation& rhs) const{
172  return ! ((*this) <= rhs);
173 }
174 bool Permutation::operator>=(const Permutation& rhs) const{
175  return ! ((*this) < rhs);
176 }
177 
178 std::ostream& operator<<(std::ostream& os, const Permutation& p){
179  p.print(os);
180  return os;
181 }
182 
183 //
void reset(int n)
Definition: Permutation.cpp:38
int nPermutations() const
void setSign(int s)
Definition: Permutation.cpp:81
static const double s
void swap(int x1, int x2, int sgn=1)
Definition: Permutation.cpp:54
void print(std::ostream &os=std::cout) const
bool operator<=(const Permutation &rhs) const
Permutation operator *(const Permutation &rhs) const
int & y_of_x(int i)
Definition: Permutation.cpp:19
void makeUnity()
Definition: Permutation.cpp:42
bool isUnity() const
Definition: Permutation.cpp:47
bool operator>=(const Permutation &rhs) const
static int dummy
Definition: Permutation.h:30
static const Permutation & unity()
Definition: Permutation.cpp:10
unsigned int size() const
Definition: Permutation.h:53
std::ostream & operator<<(std::ostream &os, const Permutation &p)
bool operator==(const Permutation &rhs) const
bool operator<(const Permutation &rhs) const
void set(const std::vector< int > &v, int sgn=1)
Definition: Permutation.cpp:61
bool operator>(const Permutation &rhs) const
static Permutation * __unit
Definition: Permutation.h:27
Permutation operator/(const Permutation &rhs) const
Permutation getInverse() const
Definition: Permutation.cpp:84