CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Permutation.hh
Go to the documentation of this file.
1 // Permutation.hh
2 
3 #ifndef PERMUTATION_HH
4 #define PERMUTATION_HH
5 
7 
8 namespace CASM {
9 
34 
35  class Permutation {
36  private:
41 
42  public:
43  Permutation(Index N): m_perm_array(Array<Index>::sequence(0, N - 1)) {};
44 
45  Permutation(const std::vector<Index> &init_perm) {
46  m_perm_array.reserve(init_perm.size());
47  for(auto it = init_perm.cbegin(); it != init_perm.cend(); ++it)
48  m_perm_array.push_back(*it);
49  };
50  Permutation(const Array<Index> &init_perm): m_perm_array(init_perm) {};
51  Permutation(ReturnArray<Index> &init_perm): m_perm_array(init_perm) {};
52 
53  Index size() const {
54  return m_perm_array.size();
55  };
56 
57  const Array<Index> &perm_array() const {
58  return m_perm_array;
59  };
60 
62  bool is_perm() const;
63 
65  bool has_fixed_points() const;
66 
68  void append_fixed_points(Index N_new);
69 
71  Permutation inverse() const;
72 
75  Permutation make_block_permutation(const Array<Index> &block_dims)const;
76 
79  Permutation transformed_by(const Permutation &trans_perm) const;
80 
82  const Index &operator[](Index i) const {
83  return m_perm_array[i];
84  };
85 
87  const Index &at(Index i) const {
88  return m_perm_array[i];
89  };
90 
92  template<typename T>
93  ReturnArray<T> permute(const Array<T> &before_array) const;
94 
96  template<typename T>
97  std::vector<T> permute(const std::vector<T> &before_array) const;
98 
100  template<typename T>
101  ReturnArray<T> ipermute(const Array<T> &before_array) const;
102 
103  template<typename T>
104  ReturnArray<T> operator*(const Array<T> &before_array) const {
105  return this->permute(before_array);
106  }
107 
108  Permutation operator*(const Permutation &RHS) const;
109 
110  jsonParser &to_json(jsonParser &json) const;
111  void from_json(const jsonParser &json);
112  };
113 
114  jsonParser &to_json(const Permutation &value, jsonParser &json);
115  void from_json(Permutation &value, const jsonParser &json);
116 
117  std::ostream &operator<<(std::ostream &, const Permutation &);
118 
119  //************************************************************************************************************************************//
120  //************************************************************************************************************************************//
121 
125  template<typename T>
126  ReturnArray<T> Permutation::permute(const Array<T> &before_array) const {
127  assert(before_array.size() == size() && "WARNING: You're trying to permute an Array with an incompatible permutation!");
128 
129  Array<T> after_array;
130  after_array.reserve(size());
131 
132  for(Index i = 0; i < size(); i++) {
133  after_array.push_back(before_array[m_perm_array[i]]);
134  }
135  return after_array;
136  }
137 
138 
139  //************************************************************************************************************************************//
140 
144  template<typename T>
145  std::vector<T> Permutation::permute(const std::vector<T> &before_vec) const {
146  assert(before_vec.size() == size() && "WARNING: You're trying to permute an Array with an incompatible permutation!");
147 
148  std::vector<T> after_vec;
149  after_vec.reserve(size());
150 
151  for(Index i = 0; i < size(); i++) {
152  after_vec.push_back(before_vec[m_perm_array[i]]);
153  }
154  return after_vec;
155  }
156 
157  //**************************************************************
158 
162  template<typename T>
163  ReturnArray<T> Permutation::ipermute(const Array<T> &before_array) const {
164  assert(before_array.size() == size() && "WARNING: You're trying to permute an Array with an incompatible permutation!");
165 
166  Array<T> after_array(before_array);
167  //after_array.reserve(size());
168 
169  for(Index i = 0; i < size(); i++) {
170  if(i != m_perm_array[i]) {
171  after_array[m_perm_array[i]] = before_array[i];
172  }
173  }
174  return after_array;
175 
176  }
177 
178 }
179 
180 
181 #endif
bool has_fixed_points() const
Checks whether any indices remain unchanged by permutation.
Definition: Permutation.cc:64
void from_json(ClexDescription &desc, const jsonParser &json)
Index size() const
Definition: Array.hh:145
void push_back(const T &toPush)
Definition: Array.hh:513
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
const Array< Index > & perm_array() const
Definition: Permutation.hh:57
Permutation(const std::vector< Index > &init_perm)
Definition: Permutation.hh:45
ReturnArray< T > operator*(const Array< T > &before_array) const
Definition: Permutation.hh:104
ReturnArray< T > permute(const Array< T > &before_array) const
Generate permuted copy of type-T Array.
Definition: Permutation.hh:126
Permutation(Index N)
Definition: Permutation.hh:43
void from_json(const jsonParser &json)
Definition: Permutation.cc:149
Main CASM namespace.
Definition: complete.cpp:8
Array< Index > m_perm_array
Definition: Permutation.hh:40
void append_fixed_points(Index N_new)
Add new indices that remain unchanged by permutation.
Definition: Permutation.cc:74
std::ostream & operator<<(std::ostream &_stream, const FormattedPrintable &_formatted)
const Index & operator[](Index i) const
const access of m_perm_array for doing low-level permutation algebra
Definition: Permutation.hh:82
EigenIndex Index
For long integer indexing:
Permutation inverse() const
Construct permutation that undoes the permutation performed by 'this'.
Definition: Permutation.cc:84
const Index & at(Index i) const
const access of m_perm_array for doing low-level permutation algebra
Definition: Permutation.hh:87
bool is_perm() const
Checks that m_perm_array contains values from 0 to m_perm_array.size()-1 and that no value is repeate...
Definition: Permutation.cc:52
jsonParser & to_json(jsonParser &json) const
Definition: Permutation.cc:138
Index size() const
Definition: Permutation.hh:53
ReturnArray< T > ipermute(const Array< T > &before_array) const
Generate inversely permuted copy of type-T Array.
Definition: Permutation.hh:163
void reserve(Index new_max)
Definition: Array.hh:491
Permutation transformed_by(const Permutation &trans_perm) const
Definition: Permutation.cc:118
Permutation(ReturnArray< Index > &init_perm)
Definition: Permutation.hh:51
Permutation make_block_permutation(const Array< Index > &block_dims) const
Definition: Permutation.cc:95
Permutation(const Array< Index > &init_perm)
Definition: Permutation.hh:50