1#ifndef XPED_PERMUTATIONS_HPP_
2#define XPED_PERMUTATIONS_HPP_
14#include <boost/algorithm/string.hpp>
41 typedef std::vector<std::size_t>
Cycle;
45 template <
typename Container>
49 std::copy(in.begin(), in.end(),
pi.begin());
55 std::ifstream stream(filename, std::ios::in);
57 if(stream.is_open()) {
58 while(std::getline(stream, line)) {
59 std::vector<std::string> results;
61 boost::split(results, line, [](
char c) {
return c ==
'\t'; });
62 if(results[0].find(
"#") != std::string::npos) {
continue; }
63 int source = stoi(results[0]);
64 int target = stoi(results[1]);
65 assert(source >= 0 and
"Invalid permutation data in file.");
66 assert(target >= 0 and
"Invalid permutation data in file.");
67 if(source >=
pi.size()) {
pi.resize(source + 1); }
75 std::set<std::size_t> bijectivePermutation;
76 for(std::size_t i = 0; i <
N; i++) {
77 assert(
pi[i] <
N and
"Invalid permutation data in file.");
78 auto it = bijectivePermutation.find(
pi[i]);
79 if(it == bijectivePermutation.end()) {
80 bijectivePermutation.insert(
pi[i]);
82 assert(
false and
"Invalid permutation data in file.");
92 for(std::size_t i = 0; i <
N; i++) {
pi_inv[
pi[i]] = i; }
94 std::vector<bool> visited(
N,
false);
95 while(!std::all_of(visited.cbegin(), visited.cend(), [](
bool b) { return b; })) {
96 auto start_it = std::find(visited.cbegin(), visited.cend(),
false);
97 std::size_t start = std::distance(visited.cbegin(), start_it);
99 visited[
pi_inv[start]] =
true;
102 cycle.push_back(start);
104 for(std::size_t i = 0; i <
N; i++) {
105 auto next_power =
pi[tmp];
106 if(next_power == start) {
109 cycle.push_back(next_power);
110 visited[
pi_inv[next_power]] =
true;
120 std::size_t seed = 0;
121 boost::hash_combine(seed, p.
pi);
129 std::vector<std::size_t> id(
N);
130 std::iota(
id.begin(),
id.end(), 0ul);
134 static std::vector<Permutation>
all(std::size_t
N)
136 std::vector<std::size_t> data(
N);
137 std::iota(data.begin(), data.end(), 0ul);
138 std::vector<Permutation> out;
142 }
while(std::next_permutation(data.begin(), data.end()));
148 std::stringstream ss;
149 for(
const auto& c :
cycles) {
151 for(std::size_t i = 0; i < c.size(); i++) {
152 if((i < c.size() - 1)) {
162 for(
const auto& elem :
pi) {
163 ss << count <<
" ==> " << elem << std::endl;
171 std::vector<Transposition> out;
172 for(
const auto& c :
cycles) {
173 for(std::size_t i = 0; i < c.size() - 1; i++) {
183 std::vector<std::vector<Transposition>> out(
cycles.size());
184 std::size_t count = 0;
185 for(
const auto& c :
cycles) {
186 for(std::size_t i = 0; i < c.size() - 1; i++) {
188 out[count].push_back(t);
197 std::vector<bool> visited(
N,
false);
198 std::vector<std::size_t> a(
N);
199 std::iota(a.begin(), a.end(), 0ul);
201 std::vector<std::size_t> out;
202 if(a ==
pi) {
return out; }
204 for(std::size_t i = 0; i <
N; i++) {
205 auto index = std::distance(a.begin(), std::find(a.begin(), a.end(),
pi[i]));
208 if(index == 0) {
continue; }
210 for(std::size_t j = index - 1; j < index; --j) {
212 std::swap(a[j], a[j + 1]);
216 for(std::size_t j = index - 1; j >= i; --j) {
218 std::swap(a[j], a[j + 1]);
223 assert(a ==
pi and
"Error when determining the swaps of a permutation.");
230 for(
const auto& c :
cycles) { out += (c.size() - 1) % 2; }
234 template <
typename Container>
237 if(c.size() == 1) {
return; }
239 for(std::size_t i = 0; i < c.size(); i++) { tmp[i] = c[
pi[i]]; }
249 template <
typename IndexType>
252 std::vector<IndexType> out(
N);
253 std::copy(
pi.begin(),
pi.end(), out.begin());
258 std::vector<std::size_t>
pi;
Definition: FusionTree.hpp:15
Definition: Permutations.hpp:40
std::size_t N
Definition: Permutations.hpp:257
Permutation(const Container &in)
Definition: Permutations.hpp:46
Permutation()
Definition: Permutations.hpp:43
std::vector< std::size_t > decompose() const
Definition: Permutations.hpp:195
std::vector< std::vector< Transposition > > independentTranspositions() const
Definition: Permutations.hpp:181
static std::vector< Permutation > all(std::size_t N)
Definition: Permutations.hpp:134
std::vector< IndexType > pi_as_index() const
Definition: Permutations.hpp:250
std::size_t parity()
Definition: Permutations.hpp:227
std::vector< std::size_t > pi_inv
Definition: Permutations.hpp:259
std::vector< Transposition > transpositions() const
Definition: Permutations.hpp:169
static Permutation Identity(std::size_t N)
Definition: Permutations.hpp:127
std::string print() const
Definition: Permutations.hpp:146
void initialize()
Definition: Permutations.hpp:88
Permutation inverse() const
Definition: Permutations.hpp:243
bool operator==(const Permutation &other) const
Definition: Permutations.hpp:125
std::vector< std::size_t > Cycle
Definition: Permutations.hpp:41
Permutation(const std::string filename)
Definition: Permutations.hpp:53
void apply(Container &c) const
Definition: Permutations.hpp:235
friend std::size_t hash_value(const Permutation &p)
Definition: Permutations.hpp:118
std::vector< std::size_t > pi
Definition: Permutations.hpp:258
std::vector< Cycle > cycles
Definition: Permutations.hpp:260
Definition: Permutations.hpp:21
Transposition(const std::size_t source_in, const std::size_t target_in)
Definition: Permutations.hpp:23
std::size_t target
Definition: Permutations.hpp:36
Transposition()
Definition: Permutations.hpp:22
Transposition(const std::array< std::size_t, 2 > data)
Definition: Permutations.hpp:26
std::string print() const
Definition: Permutations.hpp:29
std::size_t source
Definition: Permutations.hpp:35