CASM  1.1.0
A Clusters Approach to Statistical Mechanics
Permutation.cc
Go to the documentation of this file.
2 
3 #include "casm/misc/algorithm.hh"
4 
5 namespace CASM {
6 
7 /* PERMUTATION CLASS DEFINITON:
8 
9 class Permutation {
10 private:
12 before permutation
14 result of performing
16  std::vector<Index> m_perm_array;
17 
18 public:
19  Permutation(const std::vector<Index> &init_perm): m_perm_array(init_perm){};
20 
21 
22  Index size() const { return m_perm_array.size();};
23 
25 and that no value is repeated bool is_perm() const;
26 
28  bool has_fixed_points() const;
29 
31  Permutation inverse() const;
32 
35  Permutation transformed_by(const Permutation& trans_perm) const;
36 
38  const Index& operator[](Index i) const { return m_perm_array[i];};
39 
41  template<typename T>
42  std::vector<T> permute(const std::vector<T> &before_array) const;
43 
45  template<typename T>
46  std::vector<T> ipermute(const std::vector<T> &before_array) const;
47 
48 
49 };
50 */
51 
52 //**************************************************************
53 
57 bool Permutation::is_perm() const {
58  for (Index i = 0; i < size(); i++) {
59  if (!valid_index(m_perm_array[i]) || m_perm_array[i] >= size() ||
61  return false;
62  }
63  return true;
64 }
65 
66 //**************************************************************
67 
69  Index result = 0;
70  for (Index i = 0; i < size(); ++i) {
71  if (m_perm_array[i] == i) ++result;
72  }
73  return result;
74 }
75 
76 //**************************************************************
77 
80  for (Index i = 0; i < size(); i++) {
81  if (m_perm_array[i] == i) return true;
82  }
83  return false;
84 }
85 
86 //**************************************************************
87 
90  for (Index i = 0; i < size(); i++) {
91  if (m_perm_array[i] != i) return false;
92  }
93  return true;
94 }
95 
96 //**************************************************************
99  if (!N_new) return;
100  Index orig_size = m_perm_array.size();
101  for (Index i = 0; i < N_new; ++i) {
102  m_perm_array.push_back(i + orig_size);
103  }
104 }
105 
106 //**************************************************************
107 
112  std::vector<Index> im_perm_array(size(), 0);
113  for (Index i = 0; i < size(); i++) {
114  im_perm_array[m_perm_array[i]] = i;
115  }
116  return Permutation(std::move(im_perm_array));
117 }
118 
119 //**************************************************************
124  const std::vector<Index> &block_dims) const {
125  assert(block_dims.size() == size());
126  std::vector<Index> block_perm;
127  block_perm.reserve(sum(block_dims) * size());
128  std::vector<Index> i_start(block_dims.size(), 0);
129  for (Index i = 0; i + 1 < block_dims.size(); i++) {
130  i_start[i + 1] = i_start[i] + block_dims[i];
131  }
132 
133  for (Index i = 0; i < size(); i++) {
134  for (Index j = 0; j < block_dims[m_perm_array[i]]; ++j) {
135  block_perm.push_back(i_start[m_perm_array[i]] + j);
136  }
137  }
138  return Permutation(std::move(block_perm));
139 }
140 
141 //**************************************************************
150  // Equivalent to Permutation(trans_perm*(*this)*trans_perm.inverse());
151  // There's probably a faster element-wise implementation, but it would be
152  // confusing
153  return Permutation(
154  trans_perm.permute(permute(trans_perm.inverse().m_perm_array)));
155 }
156 
157 //**************************************************************
158 
159 //**************************************************************
160 
164  // not sure if this is obvious.
165  return Permutation(permute(RHS.m_perm_array));
166 }
167 
168 //**************************************************************
169 
170 } // namespace CASM
Permutation inverse() const
Construct permutation that undoes the permutation performed by 'this'.
Definition: Permutation.cc:111
bool is_perm() const
Definition: Permutation.cc:57
Container permute(const Container &_before) const
Generate permuted copy of indexed container.
Definition: Permutation.hh:124
bool has_fixed_points() const
Checks whether any indices remain unchanged by permutation.
Definition: Permutation.cc:79
Index character() const
Character of permutation is number of fixed points.
Definition: Permutation.cc:68
bool is_identity() const
Checks whether permutation is identity (i.e., m_perm_aray[i]==i for all i)
Definition: Permutation.cc:89
Permutation transformed_by(const Permutation &trans_perm) const
Definition: Permutation.cc:149
Permutation(Index N)
Definition: Permutation.hh:49
std::vector< Index > m_perm_array
Definition: Permutation.hh:46
Permutation make_block_permutation(const std::vector< Index > &block_dims) const
Definition: Permutation.cc:123
void append_fixed_points(Index N_new)
Add new indices that remain unchanged by permutation.
Definition: Permutation.cc:98
Index size() const
Definition: Permutation.hh:60
Container operator*(const Container &_before) const
Definition: Permutation.hh:108
Main CASM namespace.
Definition: APICommand.hh:8
Index find_index(Iterator begin, Iterator end, const T &value)
Equivalent to std::distance(begin, std::find(begin, end, value))
Definition: algorithm.hh:24
Container::value_type sum(const Container &container, typename Container::value_type init_val=0)
Definition: algorithm.hh:131
bool valid_index(Index i)
Definition: definitions.cc:5
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39