MINT2
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Permutation Class Reference

#include <Permutation.h>

Public Member Functions

void swap (int x1, int x2, int sgn=1)
 
void set (const std::vector< int > &v, int sgn=1)
 
void set (int a)
 
void set (int a, int b)
 
void set (int a, int b, int c)
 
void set (int a, int b, int c, int d)
 
void set (int a, int b, int c, int d, int e)
 
void setSign (int s)
 
void reset (int n)
 
void makeUnity ()
 
bool isUnity () const
 
Permutation getInverse () const
 
unsigned int size () const
 
 Permutation (int n=1)
 
 Permutation (const Permutation &other)
 
int sign () const
 
int operator[] (int i) const
 
int & operator[] (int i)
 
std::vector< int > & mapValues (const std::vector< int > &in, std::vector< int > &out) const
 
std::vector< int > mapValues (const std::vector< int > &in) const
 
template<typename T >
std::vector< T > & mapOrder (const std::vector< T > &in, std::vector< T > &out) const
 
template<typename T >
std::vector< T > & mapOrder (const MINT::PolymorphVector< T > &pv, std::vector< T > &out) const
 
template<typename T >
MINT::PolymorphVector< T > & mapOrder (const MINT::PolymorphVector< T > &pv, MINT::PolymorphVector< T > &out) const
 
template<typename T >
std::vector< T > mapOrder (const std::vector< T > &in) const
 
template<typename T >
MINT::PolymorphVector< T > mapOrder (const MINT::PolymorphVector< T > &in) const
 
int nPermutations () const
 
void print (std::ostream &os=std::cout) const
 
Permutation operator * (const Permutation &rhs) const
 
Permutation operator/ (const Permutation &rhs) const
 
bool operator< (const Permutation &rhs) const
 
bool operator== (const Permutation &rhs) const
 
bool operator<= (const Permutation &rhs) const
 
bool operator> (const Permutation &rhs) const
 
bool operator>= (const Permutation &rhs) const
 

Static Public Member Functions

static const Permutationunity ()
 

Protected Member Functions

int & y_of_x (int i)
 
const int & y_of_x (int i) const
 

Protected Attributes

std::vector< int > _v
 
int _sign
 

Static Protected Attributes

static Permutation__unit
 
static int dummy = -9999
 

Detailed Description

Definition at line 18 of file Permutation.h.

Constructor & Destructor Documentation

◆ Permutation() [1/2]

Permutation::Permutation ( int  n = 1)
inline

Definition at line 55 of file Permutation.h.

55 : _v(n), _sign(1){reset(n);};
void reset(int n)
Definition: Permutation.cpp:38
std::vector< int > _v
Definition: Permutation.h:22

◆ Permutation() [2/2]

Permutation::Permutation ( const Permutation other)
inline

Definition at line 56 of file Permutation.h.

57  : _v(other._v), _sign(other._sign)
58  {}
std::vector< int > _v
Definition: Permutation.h:22

Member Function Documentation

◆ getInverse()

Permutation Permutation::getInverse ( ) const

Definition at line 84 of file Permutation.cpp.

84  {
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 }
unsigned int size() const
Definition: Permutation.h:53

◆ isUnity()

bool Permutation::isUnity ( ) const

Definition at line 47 of file Permutation.cpp.

47  {
48  for(unsigned int i=0; i < size(); i++){
49  if (_v[i] != (int) i) return false;
50  }
51  return true;
52 }
std::vector< int > _v
Definition: Permutation.h:22
unsigned int size() const
Definition: Permutation.h:53

◆ makeUnity()

void Permutation::makeUnity ( )

Definition at line 42 of file Permutation.cpp.

42  {
43  for(unsigned int i=0; i < size(); i++){
44  _v[i] = i;
45  }
46 }
std::vector< int > _v
Definition: Permutation.h:22
unsigned int size() const
Definition: Permutation.h:53

◆ mapOrder() [1/5]

template<typename T >
std::vector<T>& Permutation::mapOrder ( const std::vector< T > &  in,
std::vector< T > &  out 
) const
inline

Definition at line 91 of file Permutation.h.

92  {
93  out.resize(in.size());
94  unsigned int to = in.size();
95  if(in.size() > this->size()){
96  to = this->size();
97  for(unsigned int i = this->size(); i < in.size(); i++) out[i] = in[i];
98  }
99 
100  for(unsigned int i=0; i < to; i++){
101  out[y_of_x(i)] = in[i];
102  }
103  return out;
104  }
int & y_of_x(int i)
Definition: Permutation.cpp:19
unsigned int size() const
Definition: Permutation.h:53

◆ mapOrder() [2/5]

template<typename T >
std::vector<T>& Permutation::mapOrder ( const MINT::PolymorphVector< T > &  pv,
std::vector< T > &  out 
) const
inline

Definition at line 107 of file Permutation.h.

108  {
109  return mapOrder(pv.theVector(), out);
110  }
std::vector< T > & theVector()
std::vector< T > & mapOrder(const std::vector< T > &in, std::vector< T > &out) const
Definition: Permutation.h:91

◆ mapOrder() [3/5]

template<typename T >
MINT::PolymorphVector<T>& Permutation::mapOrder ( const MINT::PolymorphVector< T > &  pv,
MINT::PolymorphVector< T > &  out 
) const
inline

Definition at line 112 of file Permutation.h.

113  {
114  mapOrder(pv.theVector(), out.theVector());
115  return out;
116  }
std::vector< T > & theVector()
std::vector< T > & mapOrder(const std::vector< T > &in, std::vector< T > &out) const
Definition: Permutation.h:91

◆ mapOrder() [4/5]

template<typename T >
std::vector<T> Permutation::mapOrder ( const std::vector< T > &  in) const
inline

Definition at line 119 of file Permutation.h.

119  {
120  std::vector<T> out(in.size());
121  return mapOrder(in, out);
122  }
std::vector< T > & mapOrder(const std::vector< T > &in, std::vector< T > &out) const
Definition: Permutation.h:91

◆ mapOrder() [5/5]

template<typename T >
MINT::PolymorphVector<T> Permutation::mapOrder ( const MINT::PolymorphVector< T > &  in) const
inline

Definition at line 124 of file Permutation.h.

124  {
125  MINT::PolymorphVector<T> out(in.size());
126  return mapOrder(in, out);
127  }
unsigned int size() const
std::vector< T > & mapOrder(const std::vector< T > &in, std::vector< T > &out) const
Definition: Permutation.h:91

◆ mapValues() [1/2]

std::vector<int>& Permutation::mapValues ( const std::vector< int > &  in,
std::vector< int > &  out 
) const
inline

Definition at line 70 of file Permutation.h.

71  {
72  // same as mapValues below, except no new vector needs allocating
73  // good when time is precious...
74  if(this->isUnity()){
75  out = in;
76  return out;
77  }
78 
79  out.resize(in.size());
80  for(unsigned int i=0; i < in.size(); i++){
81  out[i] = y_of_x(in[i]);
82  }
83  return out;
84  }
int & y_of_x(int i)
Definition: Permutation.cpp:19
bool isUnity() const
Definition: Permutation.cpp:47

◆ mapValues() [2/2]

std::vector<int> Permutation::mapValues ( const std::vector< int > &  in) const
inline

Definition at line 85 of file Permutation.h.

85  {
86  std::vector<int> out(in.size());
87  return mapValues(in, out);
88  }
std::vector< int > & mapValues(const std::vector< int > &in, std::vector< int > &out) const
Definition: Permutation.h:70

◆ nPermutations()

int Permutation::nPermutations ( ) const

Definition at line 100 of file Permutation.cpp.

100  {
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 }
int & y_of_x(int i)
Definition: Permutation.cpp:19
unsigned int size() const
Definition: Permutation.h:53

◆ operator *()

Permutation Permutation::operator * ( const Permutation rhs) const

Definition at line 122 of file Permutation.cpp.

122  {
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 }
unsigned int size() const
Definition: Permutation.h:53

◆ operator/()

Permutation Permutation::operator/ ( const Permutation rhs) const

Definition at line 143 of file Permutation.cpp.

143  {
144  return (*this)* rhs.getInverse();
145 }
Permutation getInverse() const
Definition: Permutation.cpp:84

◆ operator<()

bool Permutation::operator< ( const Permutation rhs) const

Definition at line 147 of file Permutation.cpp.

147  {
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 }
unsigned int size() const
Definition: Permutation.h:53

◆ operator<=()

bool Permutation::operator<= ( const Permutation rhs) const

Definition at line 168 of file Permutation.cpp.

168  {
169  return ( (*this) < rhs || (*this) == rhs);
170 }

◆ operator==()

bool Permutation::operator== ( const Permutation rhs) const

Definition at line 159 of file Permutation.cpp.

159  {
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 }
unsigned int size() const
Definition: Permutation.h:53

◆ operator>()

bool Permutation::operator> ( const Permutation rhs) const

Definition at line 171 of file Permutation.cpp.

171  {
172  return ! ((*this) <= rhs);
173 }

◆ operator>=()

bool Permutation::operator>= ( const Permutation rhs) const

Definition at line 174 of file Permutation.cpp.

174  {
175  return ! ((*this) < rhs);
176 }

◆ operator[]() [1/2]

int Permutation::operator[] ( int  i) const
inline

Definition at line 62 of file Permutation.h.

62  {
63  return y_of_x(i);
64  }
int & y_of_x(int i)
Definition: Permutation.cpp:19

◆ operator[]() [2/2]

int& Permutation::operator[] ( int  i)
inline

Definition at line 66 of file Permutation.h.

66  {
67  return y_of_x(i);
68  }
int & y_of_x(int i)
Definition: Permutation.cpp:19

◆ print()

void Permutation::print ( std::ostream &  os = std::cout) const

Definition at line 110 of file Permutation.cpp.

110  {
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 }
unsigned int size() const
Definition: Permutation.h:53

◆ reset()

void Permutation::reset ( int  n)

Definition at line 38 of file Permutation.cpp.

38  {
39  _v.resize(n);
40  makeUnity();
41 }
std::vector< int > _v
Definition: Permutation.h:22
void makeUnity()
Definition: Permutation.cpp:42

◆ set() [1/6]

void Permutation::set ( const std::vector< int > &  v,
int  sgn = 1 
)

Definition at line 61 of file Permutation.cpp.

61  {
62  _v=v;
63  _sign =sgn;
64 }
std::vector< int > _v
Definition: Permutation.h:22

◆ set() [2/6]

void Permutation::set ( int  a)

Definition at line 66 of file Permutation.cpp.

66  {
67  _v.resize(1); (*this)[0]=a;
68 }
std::vector< int > _v
Definition: Permutation.h:22

◆ set() [3/6]

void Permutation::set ( int  a,
int  b 
)

Definition at line 69 of file Permutation.cpp.

69  {
70  _v.resize(2); (*this)[0]=a; (*this)[1]=b;
71 }
std::vector< int > _v
Definition: Permutation.h:22

◆ set() [4/6]

void Permutation::set ( int  a,
int  b,
int  c 
)

Definition at line 72 of file Permutation.cpp.

72  {
73  _v.resize(3); (*this)[0]=a; (*this)[1]=b; (*this)[2]=c;
74 }
std::vector< int > _v
Definition: Permutation.h:22

◆ set() [5/6]

void Permutation::set ( int  a,
int  b,
int  c,
int  d 
)

Definition at line 75 of file Permutation.cpp.

75  {
76  _v.resize(4); (*this)[0]=a; (*this)[1]=b; (*this)[2]=c; (*this)[3]=d;
77 }
std::vector< int > _v
Definition: Permutation.h:22

◆ set() [6/6]

void Permutation::set ( int  a,
int  b,
int  c,
int  d,
int  e 
)

Definition at line 78 of file Permutation.cpp.

78  {
79  _v.resize(4); (*this)[0]=a; (*this)[1]=b; (*this)[2]=c; (*this)[3]=d; (*this)[4]=e;
80 }
std::vector< int > _v
Definition: Permutation.h:22

◆ setSign()

void Permutation::setSign ( int  s)

Definition at line 81 of file Permutation.cpp.

81  {
82  _sign = s;
83 }
static const double s

◆ sign()

int Permutation::sign ( ) const
inline

Definition at line 60 of file Permutation.h.

60 {return _sign;}

◆ size()

unsigned int Permutation::size ( ) const
inline

Definition at line 53 of file Permutation.h.

53 { return _v.size();}
std::vector< int > _v
Definition: Permutation.h:22

◆ swap()

void Permutation::swap ( int  x1,
int  x2,
int  sgn = 1 
)

Definition at line 54 of file Permutation.cpp.

54  {
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 }
int & y_of_x(int i)
Definition: Permutation.cpp:19

◆ unity()

const Permutation & Permutation::unity ( )
static

Definition at line 10 of file Permutation.cpp.

10  {
11  if(0 == __unit){
12  __unit = new Permutation();
13  __unit->makeUnity();
14  }
15  return *__unit;
16 }
Permutation(int n=1)
Definition: Permutation.h:55
void makeUnity()
Definition: Permutation.cpp:42
static Permutation * __unit
Definition: Permutation.h:27

◆ y_of_x() [1/2]

int & Permutation::y_of_x ( int  i)
protected

Definition at line 19 of file Permutation.cpp.

19  {
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 }
std::vector< int > _v
Definition: Permutation.h:22
static int dummy
Definition: Permutation.h:30

◆ y_of_x() [2/2]

const int & Permutation::y_of_x ( int  i) const
protected

Definition at line 28 of file Permutation.cpp.

28  {
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 }
std::vector< int > _v
Definition: Permutation.h:22
static int dummy
Definition: Permutation.h:30

Member Data Documentation

◆ __unit

Permutation * Permutation::__unit
staticprotected

Definition at line 27 of file Permutation.h.

◆ _sign

int Permutation::_sign
protected

Definition at line 23 of file Permutation.h.

◆ _v

std::vector<int> Permutation::_v
protected

Definition at line 22 of file Permutation.h.

◆ dummy

int Permutation::dummy = -9999
staticprotected

Definition at line 30 of file Permutation.h.


The documentation for this class was generated from the following files: