CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
Permutation.cc
Go to the documentation of this file.
2 
3 namespace CASM {
4 
5  /* PERMUTATION CLASS DEFINITON:
6 
7  class Permutation {
8  private:
12  Array<Index> m_perm_array;
13 
14  public:
15  Permutation(const Array<Index> &init_perm): m_perm_array(init_perm){};
16 
17 
18  Index size() const { return m_perm_array.size();};
19 
21  bool is_perm() const;
22 
24  bool has_fixed_points() const;
25 
27  Permutation inverse() const;
28 
31  Permutation transformed_by(const Permutation& trans_perm) const;
32 
34  const Index& operator[](Index i) const { return m_perm_array[i];};
35 
37  template<typename T>
38  Array<T> permute(const Array<T> &before_array) const;
39 
41  template<typename T>
42  Array<T> ipermute(const Array<T> &before_array) const;
43 
44 
45  };
46  */
47 
48  //**************************************************************
49 
52  bool Permutation::is_perm() const {
53  for(Index i = 0; i < size(); i++) {
55  return false;
56  }
57  return true;
58  }
59 
60 
61  //**************************************************************
62 
65  for(Index i = 0; i < size(); i++) {
66  if(m_perm_array[i] == i)
67  return true;
68  }
69  return false;
70  }
71 
72  //**************************************************************
75  if(!N_new)
76  return;
78  }
79 
80  //**************************************************************
81 
85  Array<Index> im_perm_array(size(), 0);
86  for(Index i = 0; i < size(); i++) {
87  im_perm_array[m_perm_array[i]] = i;
88  }
89  return Permutation(ReturnArray<Index>(im_perm_array));
90  }
91 
92  //**************************************************************
96  assert(block_dims.size() == size());
97  Array<Index> block_perm;
98  block_perm.reserve(block_dims.sum()*size());
99  Array<Index> i_start(block_dims.size(), 0);
100  for(Index i = 0; i + 1 < block_dims.size(); i++) {
101  i_start[i + 1] = i_start[i] + block_dims[i];
102  }
103 
104  for(Index i = 0; i < size(); i++) {
105  block_perm.append(Array<Index>::sequence(i_start[m_perm_array[i]], i_start[m_perm_array[i]] + block_dims[m_perm_array[i]] - 1));
106  }
107  return Permutation(ReturnArray<Index>(block_perm));
108  }
109 
110  //**************************************************************
119 
120  // Equivalent to Permutation(trans_perm*(*this)*trans_perm.inverse());
121  // There's probably a faster element-wise implementation, but it would be confusing
122  return Permutation(ReturnArray<Index>(trans_perm.permute(permute(trans_perm.inverse().m_perm_array))));
123  }
124 
125  //**************************************************************
126 
127  //**************************************************************
128 
132  //not sure if this is obvious.
134  }
135 
136  //**************************************************************
137 
139  return CASM::to_json(m_perm_array, json);
140  /* json.put_array();
141  for(Index i = 0; i < size(); i++)
142  json.push_back(m_perm_array[i]);
143  */
144  //return json;
145  }
146 
147  //**************************************************************
148 
149  void Permutation::from_json(const jsonParser &json) {
150  try {
151 
153  /*m_perm_array.resize(json.size());
154  for(Index i = 0; i < json.size(); i++)
155  from_json(m_perm_array[i], json[i]);*/
156  }
157  catch(...) {
159  throw;
160  }
161  }
162 
163  //**************************************************************
164 
165  std::ostream &operator<<(std::ostream &out, const Permutation &perm) {
166  out << perm.perm_array();
167  return out;
168  }
169 
170  //**************************************************************
171 
172  jsonParser &to_json(const Permutation &perm, jsonParser &json) {
173  return perm.to_json(json);
174  }
175 
176  //**************************************************************
177 
178  void from_json(Permutation &perm, const jsonParser &json) {
179  try {
180  perm.from_json(json);
181  }
182  catch(...) {
184  throw;
185  }
186  }
187 }
188 
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
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
const Array< Index > & perm_array() const
Definition: Permutation.hh:57
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)
EigenIndex Index
For long integer indexing:
Permutation inverse() const
Construct permutation that undoes the permutation performed by 'this'.
Definition: Permutation.cc:84
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
T sum() const
Definition: Array.hh:641
Index size() const
Definition: Permutation.hh:53
Index find(const T &test_elem) const
Definition: Array.hh:707
Array & append(const Array &new_tail)
Definition: Array.hh:897
void reserve(Index new_max)
Definition: Array.hh:491
Permutation transformed_by(const Permutation &trans_perm) const
Definition: Permutation.cc:118
Permutation make_block_permutation(const Array< Index > &block_dims) const
Definition: Permutation.cc:95
Basic std::vector like container (deprecated)
bool valid_index(Index i)