#  Copyright (c) 1997-2016
#  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.
#-------------------------------------------------------------------------------

REQUIRE graph::compare.rules

CREDIT graph_compare

# @category Symmetry
# Compute the combinatorial symmetries (i.e., automorphisms of the face lattice) of
# a given polytope //p//. They are stored in terms of a GROUP.VERTICES_ACTION and a GROUP.FACETS_ACTION
# property in //p//, and the GROUP.VERTICES_ACTION is also returned.
# @param Polytope p
# @return group::PermutationAction the action of the combinatorial symmetry group on the vertices
# @example To get the vertex symmetry group of the square and print its generators, type the following:
# > print combinatorial_symmetries(cube(2))->GENERATORS;
# | 2 3 0 1
# | 1 0 2 3
# > $p = cube(2); combinatorial_symmetries($p);
# > print $p->GROUP->VERTICES_ACTION->GENERATORS;
# | 2 3 0 1
# | 1 0 2 3
# > print $p->GROUP->FACETS_ACTION->GENERATORS;
# | 0 2 1 3
# | 1 0 3 2

user_function combinatorial_symmetries(polytope::Polytope) {
    my ($p) = @_;
    my $pairs_of_gens = automorphisms($p->VERTICES_IN_FACETS);
    my @vertex_gens = map {$_->first}  @$pairs_of_gens;
    my @facet_gens  = map {$_->second} @$pairs_of_gens;
    my $vertex_action = new group::PermutationAction(GENERATORS=>new Array<Array<Int>>(\@vertex_gens));
    my $facet_action  = new group::PermutationAction(GENERATORS=>new Array<Array<Int>>(\@facet_gens));
    my $g = new group::Group("CombAut");
    $g->description="combinatorial symmetry group";
    if (!defined($p->give("GROUP", "CombAut"))) {
        $p->add("GROUP", $g, VERTICES_ACTION=>$vertex_action, FACETS_ACTION=>$facet_action);
    }
    return $vertex_action;
}

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

