File: permutations

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (205 lines) | stat: -rw-r--r-- 6,526 bytes parent folder | download | duplicates (2)
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
#  Copyright (c) 1997-2024
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://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.
#-------------------------------------------------------------------------------

# @category Combinatorics
# Base class for permutations of `big' objects
declare object PermBase {

   # Mapping of features stored in this subobject onto their order in the parent (non-permuted) object.
   # The features to be permuted can be any discrete objects with identity imparted by their involvement
   # in a larger structure, like graph nodes, cone rays, facets, complex celles, etc.
   property PERMUTATION : Array<Int>;
}

#######################################
#
#  Utilities for permutation rules

# Create a new Array with elements permuted in the given order.
function permuted(Array, *) : c++ (include => "polymake/permutations.h");

function permuted(Vector, *) : c++;

# Create a new Set consisting of images of the original Set elements under the given permutation.
function permuted(Set, *) : c++;

# Like permuted(), treating the given permutation as an inverted one.
function permuted_inv(Array, *) : c++ (include => "polymake/permutations.h");

function permuted_inv(Vector, *) : c++;

function permuted_inv(Set, *) : c++;

# Apply the given permutation to every element of an Array
# (the elements are supposed to be permutable containers).
function permuted_elements(Array, *) : c++ (include => "polymake/permutations.h");

function permuted_elements(Set, *) : c++ (include => "polymake/permutations.h");

# Like permuted_elements(), treating the given permutation as an inverted one.
function permuted_elements_inv(Array, *) : c++ (include => "polymake/permutations.h");

function permuted_elements_inv(Set, *) : c++ (include => "polymake/permutations.h");

# Create a new matrix by permuting the rows of the given one.
function permuted_rows(Matrix, *) : c++;

function permuted_rows(IncidenceMatrix *) : c++;

function permuted_inv_rows(Matrix, *) : c++;

function permuted_inv_rows(IncidenceMatrix, *) : c++;

# Create a new matrix by permuting the columns of the given one.
function permuted_cols(Matrix, *) : c++;

function permuted_cols(IncidenceMatrix, *) : c++;

function permuted_inv_cols(Matrix, *) : c++;

function permuted_inv_cols(IncidenceMatrix, *) : c++;

# Create a new graph by permuting the nodes of the given one.
function permuted_nodes(GraphAdjacency, *) : c++;

function permuted_inv_nodes(GraphAdjacency, *) : c++;


# @category Combinatorics
# Returns the permutation that maps //a// to //b//.
# @param Array a
# @param Array b
# @return Array<Int> permutation of element indexes, or undef if there are unmatched elements
# @example
# > $p = find_permutation([1,8,3,4],[3,8,4,1]);
# > print $p;
# | 2 1 3 0

user_function find_permutation(*,*) : c++ (include => "polymake/permutations.h");


# @category Combinatorics
# Returns the __cycles__ of a permutation given by //p//.
# @param Array<Int> p
# @return ARRAY
# @example
# > print permutation_cycles([1,0,3,2]);
# | {0 1}{2 3}

user_function permutation_cycles(*) : c++ (include => "polymake/permutations.h") : returns(@);

# @category Combinatorics
# Returns the sorted cycle lengths of a permutation
# @param Array<Int> p
# @return Array<Int>
# @example
# > print permutation_cycle_lengths(new Array<Int>([1,2,0,4,3]));
# | 2 3
user_function permutation_cycle_lengths(Array<Int>) {
    my @c = permutation_cycles(shift);
    if (scalar @c == 1) {
       my $a = new Array<Int>(1);
       $a->[0] = $c[0]->size();
       return $a;
    }
    return new Array<Int>( sort map { $_->size() } @c );
}                                          

# @category Combinatorics
# Returns the order of a permutation
# @param Array<Int> p
# @return Int
# @example
# > print permutation_order(new Array<Int>([1,2,0,4,3]));
# | 6
user_function permutation_order(Array<Int>) {
    my $p = shift;
    my $o = 1;
    foreach (permutation_cycles($p)) {
        $o = lcm($o, $_->size());
    }
    return $o;
}                                          
                                          
# @category Combinatorics
# Returns the __sign__ of the permutation given by //p//.
# @param Array<Int> p
# @return Int +1 or -1
# @example
# > print permutation_sign([1,0,3,2]);
# | 1

user_function permutation_sign(*) : c++ (include => "polymake/permutations.h");

# @category Combinatorics
# Returns the number of fixed points of the permutation given by //p//.
# @param Array<Int> p
# @return Int 
# @example
# > print n_fixed_points([1,0,2,4,3]);
# | 1

user_function n_fixed_points(*) : c++ (include => "polymake/permutations.h");

# @category Combinatorics
# Determine whether two arrays //a// and //b// are permuted copies of each other.
# @param Array a
# @param Array b
# @return Bool
# @example
# > print are_permuted([1,8,3,4],[3,8,4,1]);
# | true

user_function are_permuted(*,*) : c++ (include => "polymake/permutations.h");


#FIXME: ticket 725 and 727 return type should be masqueraded Array<Array <Int> > instead of perl-Array

# @category Combinatorics
# Returns a list of all permutations of the set {0...n-1} as a perl-array
# @param Int n
# @return Array<Array<Int>>
# @example
# > print all_permutations(3);
# | 0 1 2
# | 1 0 2
# | 2 0 1
# | 0 2 1
# | 1 2 0
# | 2 1 0

user_function all_permutations($) : c++  (include => "polymake/permutations.h") : returns(Container<Container>);

# @category Combinatorics
# Returns the __permutation matrix__ of the permutation given by //p//.
# @tparam Scalar default: [[Int]]
# @param Array<Int> p
# @return Matrix<Scalar>
# @example The following prints the permutation matrix in sparse representation.
# > print permutation_matrix([1,0,3,2]);
# | (4) (1 1)
# | (4) (0 1)
# | (4) (3 1)
# | (4) (2 1)

user_function permutation_matrix<Scalar=Int>(*&& const) : c++ (include => [ "polymake/permutations.h", "polymake/SparseMatrix.h" ]);


# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: