Cadabra
Computer algebra system for field theory problems
Permutations.hh
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <stdexcept>
5 #include <vector>
6 
16 
17 class PermutationException : std::logic_error {
18  public:
19  PermutationException(const std::string& ex) : std::logic_error(ex) {};
20  };
21 
22 class Perm {
23  public:
24  std::vector<int> perm;
25 
28  template<class iterator>
29  void find(iterator start1, iterator end1, iterator start2, iterator end2);
30 
32  template<class iterator>
33  void apply(iterator start1, iterator end1);
34 
35  };
36 
37 class Images {
38  public:
39  std::vector<int> images;
40 
42  template<class iterator>
43  void find(iterator start1, iterator end1, iterator start2, iterator end2);
44  };
45 
46 
47 
48 
49 template<class iterator>
50 void Perm::find(iterator start1, iterator end1, iterator start2, iterator end2)
51  {
52  perm.clear();
53 
54  while(start2!=end2) {
55  auto it=start1;
56  int num=0;
57  while(it!=end1) {
58  if(*start2==*it) {
59  perm.push_back(num);
60  break;
61  }
62  ++num;
63  ++it;
64  }
65  if(it==end1)
66  throw PermutationException("Sets do not contain the same elements.");
67 
68  ++start2;
69  }
70  }
71 
72 template<class iterator>
73 void Perm::apply(iterator start, iterator end)
74  {
75  typedef typename std::remove_reference<decltype(*start)>::type value_type;
76 
77  std::vector<value_type> orig;
78  auto it=start;
79  while(it!=end) {
80  orig.push_back(*it);
81  ++it;
82  }
83 
84  // std::cerr << orig.size() << ", " << perm.size() << std::endl;
85  if(orig.size()!=perm.size()) {
86  std::cerr << "Perm::apply: orig.size()=" << orig.size() << ", "
87  << "perm.size()=" << perm.size() << std::endl;
88  assert(orig.size()==perm.size());
89  }
90 
91  for(unsigned int i=0; i<orig.size(); ++i) {
92  *start=orig[perm[i]];
93  ++start;
94  }
95  }
96 
97 
98 template<class iterator>
99 void Images::find(iterator start1, iterator end1, iterator start2, iterator end2)
100  {
101  images.clear();
102 
103  while(start1!=end1) {
104  auto it=start2;
105  int num=0;
106  while(it!=end2) {
107  if(*start1==*it) {
108  images.push_back(num);
109  break;
110  }
111  ++num;
112  ++it;
113  }
114  if(it==end2)
115  throw PermutationException("Sets do not contain the same elements.");
116 
117  ++start1;
118  }
119  }
Definition: Permutations.hh:37
std::vector< int > images
Definition: Permutations.hh:39
void find(iterator start1, iterator end1, iterator start2, iterator end2)
Find the permutation that takes [start1, end1> to [start2, end2>.
Definition: Permutations.hh:99
Definition: Permutations.hh:22
std::vector< int > perm
Definition: Permutations.hh:24
void find(iterator start1, iterator end1, iterator start2, iterator end2)
Find the permutation that takes [start1, end1> to [start2, end2>.
Definition: Permutations.hh:50
void apply(iterator start1, iterator end1)
Apply the permutation 'perm' on the given range.
Definition: Permutations.hh:73
Generic permutation group material.
Definition: Permutations.hh:17
PermutationException(const std::string &ex)
Definition: Permutations.hh:19
end
Definition: nevaluate.py:22
start
Definition: nevaluate.py:20