File: orbit.h

package info (click to toggle)
polymake 3.0r2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,752 kB
  • ctags: 30,928
  • sloc: cpp: 151,785; perl: 32,510; ansic: 3,597; java: 2,654; python: 278; makefile: 181; xml: 103; sh: 79
file content (280 lines) | stat: -rw-r--r-- 8,648 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* Copyright (c) 1997-2015
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#ifndef ORBITS_H
#define ORBITS_H

#include <polymake/Array.h>
#include <polymake/Set.h>
#include <polymake/Bitset.h>
#include <polymake/Matrix.h>
#include <polymake/ListMatrix.h>
#include <polymake/Polynomial.h>
#include <polymake/permutations.h>
#include <queue>

namespace pm{
namespace operations{
namespace group{

struct on_container{};
struct on_elements{};
struct on_rows{};
struct on_cols{};

template <typename OpRef, typename action_type, typename PERM, 
typename op_tag=typename object_traits<typename deref<OpRef>::type>::generic_tag, 
typename perm_tag=typename object_traits<PERM>::generic_tag,
typename enabled=True
>
struct action;

// generic action on container with Array<int>
template <typename OpRef, typename PERM, typename op_tag>
struct action<OpRef, on_container, PERM, op_tag, is_container> {
  typedef OpRef argument_type;
  typedef typename deref<argument_type>::type result_type;

  const PERM& perm;
  
  action(const PERM& p)
   : perm(p) {}
  
  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return permuted(x,perm);
  }      
}; 

// action on integers (anchor of the on_elements recursion)
template <typename PERM>
struct action<int&, on_elements, PERM, is_scalar, is_container> {
  typedef int& argument_type;
  typedef typename deref<argument_type>::type result_type;
  
  const PERM& perm;
  
  action(const PERM& p)
    : perm(p) {}
  
  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return perm[x];
  }      
}; 

// generic action on elements in a container with Array<int>
template <typename OpRef, typename PERM, typename op_tag>
  struct action<OpRef, on_elements, PERM, op_tag, is_container,
  typename enable_if<True, (identical< typename object_traits<typename deref<OpRef>::type>::model, is_container>::value && !identical<op_tag, is_matrix>::value) >::type
  > {
  typedef OpRef argument_type;
  typedef typename object_traits<typename deref<argument_type>::type>::persistent_type result_type;


  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return result_type(attach_operation(x,action<typename deref<argument_type>::type::value_type&, on_elements, PERM>(perm)));
  }      
}; 

// generic action on elements in a matrix with Array<int>
template <typename OpRef, typename PERM>
  struct action<OpRef, on_elements, PERM, is_matrix, is_container> {
  typedef OpRef argument_type;
  typedef typename object_traits<typename deref<argument_type>::type>::persistent_type result_type;


  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return result_type(x.rows(),x.cols(),entire(attach_operation(concat_rows(x),action<typename deref<argument_type>::type::value_type&, on_elements, PERM>(perm))));
  }      
}; 

// action on rows of a matrix with Array<int>
template <typename OpRef, typename PERM>
struct action<OpRef, on_rows, PERM, is_matrix, is_container> {
  typedef OpRef argument_type;
  typedef typename deref<argument_type>::type result_type;

  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument< argument_type >::const_type x) const
  {
    return permuted_rows(x,perm);
  }
};

// action on cols of a matrix with Array<int>
template <typename OpRef, typename PERM>
struct action<OpRef, on_cols, PERM, is_matrix, is_container> {
  typedef OpRef argument_type;
  typedef typename deref<argument_type>::type result_type;
    
  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument< argument_type >::const_type x) const
  {
    return permuted_cols(x,perm);
  }      
}; 

// action on a vector under a matrix group
template <typename OpRef, typename PERM>
struct action<OpRef, on_elements, PERM, is_vector, is_matrix> {
  typedef OpRef argument_type;
  typedef typename deref<argument_type>::type result_type;
    
  const PERM& mat;
    
  action(const PERM& m)
   : mat(m.top()) {}
    
  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return mat*x;
  }
};

// action on the variables of a polyomial with Array<int>
template <typename PERM, typename Coefficient, typename Exponent>
struct action<Polynomial<Coefficient,Exponent>&, on_container, PERM, is_polynomial, is_container> {
  typedef Polynomial<Coefficient, Exponent>& argument_type;
  typedef typename deref<argument_type>::type result_type;

  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return Polynomial<Coefficient, Exponent>(action<Matrix<Exponent>, on_cols, PERM>(perm)(x.monomials_as_matrix()), x.coefficients_as_vector(), x.get_ring());
  }      
}; 

// action on the variables of a monomial with Array<int>
template <typename PERM, typename Coefficient, typename Exponent>
struct action<Monomial<Coefficient,Exponent>&, on_container, PERM, is_opaque, is_container> {
  typedef Monomial<Coefficient,Exponent>& argument_type;
  typedef typename deref<argument_type>::type result_type;

  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return Monomial<Coefficient,Exponent>(action<Vector<Exponent>, on_container, PERM>(perm)(x.get_value()), x.get_ring());
  }      
};

// generic action on both elements of a pair
template <typename E1, typename E2, typename action_type, typename PERM, typename perm_tag>
struct action<std::pair<E1,E2>&, action_type, PERM, is_composite, perm_tag> {
  typedef std::pair<E1,E2>& argument_type;
  typedef typename deref<argument_type>::type result_type;

  const PERM& perm;

  action(const PERM& p)
   : perm(p) {}

  result_type operator() (typename function_argument<argument_type>::const_type x) const
  {
    return std::make_pair<E1,E2>(action<E1&, action_type, PERM>(perm)(x.first),action<E2&, action_type, PERM>(perm)(x.second));
  }      
}; 

} //end namespace group
} //end namespace operations

} //end namespace pm

namespace polymake {
namespace group {

using pm::operations::group::on_container;
using pm::operations::group::on_elements;
using pm::operations::group::on_rows;
using pm::operations::group::on_cols;

/*
 * computes the action on something under one permutation element
 */
template <typename action_type, typename PERM, typename Element>
typename pm::object_traits<Element>::persistent_type
action(const PERM& perm, const Element& element) 
{
  return pm::operations::group::action<Element&,action_type,PERM>(perm)(element);
}

/*
 * computes the action on something under the inverse of a Array<int>
 */
template <typename action_type, typename Element>
typename pm::object_traits<Element>::persistent_type
action_inv(const Array<int>& perm, const Element& element) 
{
  Array<int> inv(perm.size());
  inverse_permutation(perm, inv);
  return pm::operations::group::action<Element&,action_type,Array<int> >(inv)(element);
}


/*
 * Comutes the orbit of element, where the group is spanned by generators
 */
template<typename action_type, typename PERM, typename Element>
Set< Element > orbit(const Array< PERM >& generators, const Element& element) {
  Set< Element > orbit;
  orbit += element;
  std::queue< Element > q;
  q.push(element);
  while(!q.empty()) {
    Element orbitElement = q.front();
    q.pop();
    for(typename Entire<Array< PERM > >::const_iterator generator = entire(generators); !generator.at_end(); ++generator) {
      Element next = action<action_type>(*generator,orbitElement);
      if(!orbit.collect(next)) {
        q.push(next);
      }
    }
  }
  return orbit;
}

}
}

#endif