/usr/include/polymake/permutations.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 | /* Copyright (c) 1997-2018
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 POLYMAKE_PERMUTATIONS_H
#define POLYMAKE_PERMUTATIONS_H
#include "polymake/GenericMatrix.h"
#include "polymake/Integer.h"
#include "polymake/Map.h"
#include "polymake/Set.h"
#include "polymake/Array.h"
#include "polymake/Bitset.h"
#include "polymake/hash_map"
#include "polymake/vector"
#include "polymake/list"
#include <algorithm>
#include <sstream>
namespace pm {
template <typename Permutation>
int permutation_sign(const Permutation& v)
{
const int l=v.size();
if (l<=1) return 1;
std::vector<int> w(v.size());
copy_range(v.begin(), entire(w));
int sign=1;
for (int i=0; i<l; ) {
if (w[i]!=i) {
int k=w[i];
w[i]=w[k];
w[k]=k;
sign = -sign;
} else {
++i;
}
}
return sign;
}
template <typename Permutation>
int n_fixed_points(const Permutation& p)
{
int i(0), n(0);
for (auto pit = entire(p); !pit.at_end(); ++pit, ++i)
if (*pit == i)
++n;
return n;
}
template <typename Permutation, typename InvPermutation> inline
void inverse_permutation(const Permutation& perm, InvPermutation& inv_perm)
{
inv_perm.resize(perm.size());
int pos=0;
for (auto i=entire(perm); !i.at_end(); ++i, ++pos)
inv_perm[*i]=pos;
}
template <typename Permutation>
class permutation_cycles_iterator {
public:
typedef forward_iterator_tag iterator_category;
typedef std::list<int> value_type;
typedef const value_type& reference;
typedef const value_type* pointer;
typedef ptrdiff_t difference_type;
typedef permutation_cycles_iterator iterator;
typedef iterator const_iterator;
protected:
typedef typename Permutation::const_iterator perm_iterator;
int first, limit;
Bitset visited;
value_type value;
perm_iterator cur;
void valid_position()
{
for ( ;first<limit ;++first, ++cur)
if (first!=*cur && !visited.contains(first)) {
int v=first;
do {
visited+=v;
value.push_back(v);
const int prev=v;
v=*cur;
std::advance(cur, v-prev);
} while (v!=first);
break;
}
}
public:
permutation_cycles_iterator() {}
permutation_cycles_iterator(const Permutation& perm)
: first(0), limit(perm.size()), visited(limit), cur(perm.begin()) { valid_position(); }
permutation_cycles_iterator(const Permutation& perm, bool /* end_position */)
: first(perm.size()), limit(first), cur(perm.end()) {}
reference operator* () const { return value; }
pointer operator-> () const { return &value; }
iterator& operator++ ()
{
value.clear();
++first; ++cur;
valid_position();
return *this;
}
const iterator operator++ (int) { iterator copy=*this; operator++(); return copy; }
bool operator== (const permutation_cycles_iterator& it) const { return cur==it.cur; }
bool operator!= (const permutation_cycles_iterator& it) const { return !operator==(it); }
bool at_end() const { return first>=limit; }
};
template <typename Permutation>
class PermutationCycles {
protected:
PermutationCycles();
~PermutationCycles();
const Permutation& hidden() const { return reinterpret_cast<const Permutation&>(*this); }
public:
typedef permutation_cycles_iterator<Permutation> iterator;
typedef iterator const_iterator;
typedef typename iterator::value_type value_type;
typedef typename iterator::reference reference;
typedef reference const_reference;
iterator begin() const { return hidden(); }
iterator end() const { return iterator(hidden(), true); }
value_type front() const { return value_type(hidden().begin(), 0, hidden().size()); }
bool empty() const { return begin().at_end(); }
int size() const { return count_it(begin()); }
};
template <typename Permutation> inline
const PermutationCycles<Permutation>& permutation_cycles(const Permutation& p)
{
return reinterpret_cast<const PermutationCycles<Permutation>&>(p);
}
template <typename Permutation>
struct check_iterator_feature<permutation_cycles_iterator<Permutation>, end_sensitive> : std::true_type {};
template <typename Permutation>
struct spec_object_traits< PermutationCycles<Permutation> >
: spec_object_traits<is_container> {
static const bool is_lazy=true, is_always_const=true;
typedef Permutation masquerade_for;
static const IO_separator_kind IO_separator=IO_sep_inherit;
};
template <typename Value, typename Comparator, typename enabled=void>
struct permutation_map {
typedef Map<Value, int, Comparator> type;
};
template <typename Value>
struct permutation_map<Value, operations::cmp, typename std::enable_if<!std::numeric_limits<Value>::is_specialized && is_ordered<Value>::value>::type> {
typedef Map<Value, int, operations::cmp> type;
};
template <typename Value>
struct permutation_map<Value, operations::cmp, typename std::enable_if<std::numeric_limits<Value>::is_specialized>::type> {
typedef hash_map<Value, int> type;
};
template <typename Input1, typename Input2, typename Output, typename Comparator>
void find_permutation(Input1&& src1, Input2&& src2, Output&& dst, const Comparator& comparator)
{
typename permutation_map<typename iterator_traits<Input1>::value_type, Comparator>::type index_map;
for (int i=0; !src1.at_end(); ++src1, ++i)
index_map[*src1]=i;
for (; !src2.at_end(); ++src2, ++dst) {
const auto where=index_map.find(*src2);
if (where == index_map.end()) {
std::string reason;
if (index_map.empty()) {
reason="not a permutation: first sequence is shorter";
} else {
std::ostringstream os;
wrap(os) << "not a permutation: no match for <" << *src2 << ">";
reason=os.str();
}
throw no_match(reason);
}
*dst=where->second;
index_map.erase(where);
}
if (!index_map.empty())
throw no_match("not a permutation: second sequence is shorter");
}
template <typename Container1, typename Container2, typename Comparator> inline
Array<int> find_permutation(const Container1& c1, const Container2& c2, const Comparator& comparator)
{
Array<int> perm(c1.size());
find_permutation(entire(c1), entire(c2), perm.begin(), comparator);
return perm;
}
template <typename Container1, typename Container2> inline
Array<int> find_permutation(const Container1& c1, const Container2& c2)
{
return find_permutation(c1, c2, polymake::operations::cmp());
}
template <typename Container1, typename Container2, typename Comparator> inline
bool are_permuted(const Container1& c1, const Container2& c2, const Comparator& comparator)
{
Array<int> perm(c1.size());
try {
find_permutation(entire(c1), entire(c2), perm.begin(), comparator);
return true;
} catch (const no_match&) {
return false;
}
return false;
}
template <typename Container1, typename Container2> inline
bool are_permuted(const Container1& c1, const Container2& c2)
{
return are_permuted(c1,c2,polymake::operations::cmp());
}
template <typename Container, typename Permutation> inline
typename std::enable_if<std::is_same<typename object_traits<Container>::generic_tag, is_container>::value,
typename object_traits<Container>::persistent_type>::type
permuted(const Container& c, const Permutation& perm)
{
if (POLYMAKE_DEBUG) {
if (c.size() != perm.size())
throw std::runtime_error("permuted - dimension mismatch");
}
typename object_traits<Container>::persistent_type result(c.size());
copy_range(entire(select(c, perm)), result.begin());
return result;
}
template <typename Container, typename Permutation> inline
typename std::enable_if<std::is_same<typename object_traits<Container>::generic_tag, is_container>::value,
typename object_traits<Container>::persistent_type>::type
permuted_inv(const Container& c, const Permutation& perm)
{
if (POLYMAKE_DEBUG) {
if (c.size() != perm.size())
throw std::runtime_error("permuted_inv - dimension mismatch");
}
typename object_traits<Container>::persistent_type result(c.size());
copy_range(entire(c), select(result,perm).begin());
return result;
}
enum permutation_sequence {
permutations_heap,
permutations_adjacent,
permutations_lex
};
template <permutation_sequence kind=permutations_heap>
class AllPermutations;
template <permutation_sequence kind>
class permutation_iterator;
class permutation_iterator_base {
public:
typedef forward_iterator_tag iterator_category;
typedef std::vector<int> value_type;
typedef const value_type* pointer;
typedef const value_type& reference;
typedef ptrdiff_t difference_type;
protected:
value_type perm;
permutation_iterator_base() {}
permutation_iterator_base(int n) : perm(n) { reset(n); }
void reset(int n) { copy_range(entire(sequence(0, n)), perm.begin()); }
public:
reference operator* () const { return perm; }
pointer operator-> () const { return &perm; }
};
/// Implementation of the Heap's algorithm by R. Sedgewick
template <>
class permutation_iterator<permutations_heap>
: public permutation_iterator_base {
friend class AllPermutations<permutations_heap>;
public:
typedef permutation_iterator iterator;
typedef iterator const_iterator;
protected:
std::vector<int> cnt;
int n, pos;
permutation_iterator(int n_arg, bool)
: permutation_iterator_base(n_arg)
, cnt(n_arg, 0)
, n(n_arg)
, pos(n_arg) {}
public:
permutation_iterator()
: n(0)
, pos(0) {}
explicit permutation_iterator(int n_arg)
: permutation_iterator_base(n_arg)
, cnt(size_t(n_arg), 0)
, n(n_arg)
, pos(n_arg>1) {}
iterator& operator++ ()
{
do {
if (cnt[pos]<pos) {
std::swap(perm[pos], perm[pos%2 * cnt[pos]]);
++cnt[pos];
pos=1;
break;
}
cnt[pos]=0;
} while (++pos < n);
return *this;
}
const iterator operator++ (int) { iterator copy(*this); operator++(); return copy; }
bool at_end() const { return pos>=n; }
bool operator== (const iterator& it) const
{
return n==it.n && pos==it.pos && cnt==it.cnt;
}
bool operator!= (const iterator& it) const
{
return !operator==(it);
}
void rewind()
{
reset(n);
pos=1;
fill_range(entire(cnt), 0);
}
};
template <>
class permutation_iterator<permutations_adjacent>
: public permutation_iterator_base {
friend class AllPermutations<permutations_adjacent>;
public:
typedef permutation_iterator iterator;
typedef iterator const_iterator;
protected:
std::vector<int> move;
int n, next;
void incr()
{
if (next==0) {
next=-1; return;
}
int lim=n, offset=0;
std::vector<int>::iterator pos=move.begin();
for (;;) {
int m=*pos;
if (m>0) {
if (m<lim) {
next=m+offset;
++*pos;
break;
}
} else {
if (m<-1) {
next=offset-m-1;
++*pos;
break;
}
++offset;
}
if (--lim > 1) {
*pos=-m;
++pos;
} else {
next=0;
break;
}
}
}
public:
permutation_iterator() : n(0) {}
explicit permutation_iterator(int n_arg)
: permutation_iterator_base(n_arg)
, move(size_t(n_arg), 1)
, n(n_arg)
, next(n-1)
{
if (next>0) {
next=1; move[0]=2;
}
}
iterator& operator++ ()
{
std::swap(perm[next-1], perm[next]);
incr();
return *this;
}
const iterator operator++ (int) { iterator copy(*this); operator++(); return copy; }
int next_swap() const { return next; }
bool at_end() const { return next<0; }
bool operator== (const iterator& it) const
{
return next==it.next && n==it.n && (!next || move==it.move);
}
bool operator!= (const iterator& it) const
{
return !operator==(it);
}
void rewind()
{
reset(n);
fill_range(entire(move), 1);
next=n-1;
if (next>1) {
next=1; move[0]=2;
}
}
};
// permutations in lexicographic order via STL
template <>
class permutation_iterator<permutations_lex>
: public permutation_iterator_base {
friend class AllPermutations<permutations_lex>;
public:
typedef permutation_iterator iterator;
typedef iterator const_iterator;
protected:
bool _at_end;
public:
permutation_iterator() {}
explicit permutation_iterator(int n, bool end_arg=false)
: permutation_iterator_base(n), _at_end(end_arg || n==0) {}
iterator& operator++ ()
{
_at_end=!std::next_permutation(perm.begin(), perm.end());
return *this;
}
const iterator operator++ (int) { iterator copy(*this); operator++(); return copy; }
bool at_end() const { return _at_end; }
bool operator== (const iterator& it) const
{
return _at_end ? it._at_end : !it._at_end && perm==it.perm;
}
bool operator!= (const iterator& it) const
{
return !operator==(it);
}
void rewind()
{
reset(perm.size());
_at_end=perm.empty();
}
};
template <permutation_sequence kind>
struct check_iterator_feature<permutation_iterator<kind>, end_sensitive> : std::true_type {};
template <permutation_sequence kind>
struct check_iterator_feature<permutation_iterator<kind>, rewindable> : std::true_type {};
template <permutation_sequence kind>
class AllPermutations {
protected:
int n;
public:
explicit AllPermutations(int n_arg=0) : n(n_arg) {}
typedef permutation_iterator<kind> iterator;
typedef iterator const_iterator;
typedef typename iterator::value_type value_type;
typedef typename iterator::reference reference;
typedef reference const_reference;
iterator begin() const { return iterator(n); }
iterator end() const { return iterator(n,true); }
value_type front() const
{
return value_type(sequence(0,n).begin(), sequence(0,n).end());
}
//FIXME: ticket 727 changed from Integer to int to get perl acces
long size() const { return n ? long(Integer::fac(n)) : 0; }
bool empty() const { return n==0; }
};
template <permutation_sequence kind>
struct spec_object_traits< AllPermutations<kind> >
: spec_object_traits<is_container> {
static const bool is_always_const=true;
};
inline
AllPermutations<> all_permutations(int n)
{
return AllPermutations<>(n);
}
template <permutation_sequence kind>
AllPermutations<kind> all_permutations(int n)
{
return AllPermutations<kind>(n);
}
template <typename PermutationRef, typename Element=int>
class PermutationMatrix
: public GenericMatrix< PermutationMatrix<PermutationRef,Element>, Element > {
protected:
alias<PermutationRef> perm;
mutable std::vector<int> inv_perm;
public:
typedef Element value_type;
typedef const Element& reference;
typedef reference const_reference;
typedef typename alias<PermutationRef>::arg_type arg_type;
PermutationMatrix(arg_type perm_arg) : perm(perm_arg) {}
typename alias<PermutationRef>::const_reference get_perm() const { return *perm; }
const std::vector<int>& get_inv_perm() const
{
if (inv_perm.empty() && !get_perm().empty())
inverse_permutation(get_perm(), inv_perm);
return inv_perm;
}
};
template <typename PermutationRef, typename Element>
struct spec_object_traits< PermutationMatrix<PermutationRef,Element> >
: spec_object_traits<is_container> {
static const bool is_temporary=true, is_always_const=true;
};
template <typename PermutationRef, typename Element>
struct check_container_feature< PermutationMatrix<PermutationRef,Element>, pure_sparse> : std::true_type {};
template <typename PermutationRef, typename Element>
class matrix_random_access_methods< PermutationMatrix<PermutationRef,Element> > {
typedef PermutationMatrix<PermutationRef,Element> master;
public:
const Element& operator() (int i, int j) const
{
const master& me=*static_cast<const master*>(this);
return me.get_perm()[i]==j ? one_value<Element>() : zero_value<Element>();
}
};
template <typename PermutationRef, typename Element>
class Rows< PermutationMatrix<PermutationRef, Element> >
: public modified_container_pair_impl< Rows< PermutationMatrix<PermutationRef, Element> >,
mlist< Container1Tag< PermutationRef >,
Container2Tag< constant_value_container<const Element&> >,
OperationTag< SameElementSparseVector_factory<2> >,
MasqueradedTop > > {
typedef modified_container_pair_impl<Rows> base_t;
protected:
~Rows();
public:
const typename base_t::container1& get_container1() const
{
return this->hidden().get_perm();
}
typename base_t::container2 get_container2() const
{
return one_value<Element>();
}
typename base_t::operation get_operation() const
{
return this->size();
}
};
template <typename PermutationRef, typename Element>
class Cols< PermutationMatrix<PermutationRef, Element> >
: public modified_container_pair_impl< Cols< PermutationMatrix<PermutationRef, Element> >,
mlist< Container1Tag< std::vector<int> >,
Container2Tag< constant_value_container<const Element&> >,
OperationTag< SameElementSparseVector_factory<2> >,
MasqueradedTop > > {
typedef modified_container_pair_impl<Cols> base_t;
protected:
~Cols();
public:
const typename base_t::container1& get_container1() const
{
return this->hidden().get_inv_perm();
}
typename base_t::container2 get_container2() const
{
return one_value<Element>();
}
typename base_t::operation get_operation() const
{
return this->size();
}
};
template <typename Element, typename Permutation> inline
PermutationMatrix<const Permutation&, Element>
permutation_matrix(const Permutation& perm)
{
return perm;
}
template<typename Permutation> inline
bool is_permutation(const Permutation& perm){
// check that perm really is a permutation
Set<typename Permutation::value_type> values;
for (auto i=entire(perm); !i.at_end(); ++i){
if(*i >= perm.size() or *i < 0){
return 0;
}
values.insert(*i);
}
if(values.size() != perm.size() ){
return 0;
}
return 1;
}
template <typename Scalar>
Array<Array<int>> rows_induced_from_cols(const Matrix<Scalar>& M, const Array<Array<int>> G)
{
Map<Vector<Scalar>,int> RevPerm;
int index = 0;
for (auto v = entire(rows(M)); !v.at_end(); ++v, ++index){
RevPerm[*v] = index;
}
Array<Array<int>> RowPerm(G.size());
auto old_perm = entire(G);
for (auto new_perm = entire(RowPerm); !new_perm.at_end(); ++new_perm, ++old_perm){
new_perm->resize(M.rows());
auto new_perm_entry = entire(*new_perm);
for (auto v = entire(rows(M)); !v.at_end(); ++v, ++new_perm_entry){
*new_perm_entry = RevPerm[permuted(*v,*old_perm)];
}
}
return RowPerm;
}
template<typename Permutation, typename SubdomainType>
Array<Permutation>
permutation_subgroup_generators(const Array<Permutation>& gens,
const SubdomainType& subdomain)
{
Map<int, int> index_of;
int index(0);
for (const auto& s: subdomain)
index_of[s] = index++;
Set<Array<int>> subgens;
const Array<int> id(sequence(0, subdomain.size()));
for (const auto& g: gens) {
Array<int> candidate_gen(subdomain.size());
bool candidate_ok(true);
for (const auto& i: subdomain) {
if (!index_of.exists(g[i])) {
candidate_ok = false;
break;
}
candidate_gen[index_of[i]] = index_of[g[i]];
}
if (candidate_ok && candidate_gen != id)
subgens += candidate_gen;
}
return Array<Permutation>(subgens.size(), entire(subgens));
}
} // end namespace pm
namespace polymake {
using pm::permutation_sign;
using pm::inverse_permutation;
using pm::permutation_cycles;
using pm::find_permutation;
using pm::permuted;
using pm::permuted_inv;
using pm::permutation_sequence;
using pm::permutations_heap;
using pm::permutations_adjacent;
using pm::permutations_lex;
using pm::AllPermutations;
using pm::all_permutations;
using pm::PermutationMatrix;
using pm::permutation_matrix;
using pm::permutation_subgroup_generators;
}
#endif // POLYMAKE_PERMUTATIONS_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|