#  Copyright (c) 1997-2004
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de
#
#  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.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: gap.rules,v 1.9 2004/11/10 11:01:28 gawrilow Exp $

object SimplicialComplex;

# category: Topology
# A finite representation of the fundamental group.
# The fundamental group is represented as a pair of an integer,
# the number of generators, and a list of relations. The generators are numbered
# consecutively starting with zero. A relation is encoded as a list of pairs,
# each pair consisting of a generator and a boolean value which determines if
# the generator itself or it's inverse is used in the relation.
#
# You may use the @see fundamental2gap method to produce a @c GAP file.

property FUNDAMENTAL_GROUP
$type="tuple<cardinal, list< tuple<cardinal,boolean> > >";


# category: Topology
# Labels of the generators of the @see FUNDAMENTAL_GROUP.
# The labels can be chosen freely. If the @see FUNDAMENTAL_GROUP is computed
# by the @c polymake system, the generators correspond to the edges of the
# complex. Hence they are labeled @c g followed by the vertices of the edge, e.g.
# @c g3_6 corresponds to the edge @c {3 @c 6}.

property FUNDAMENTAL_GROUP_GEN_LABELS
$type="array<label>";


FUNDAMENTAL_GROUP, FUNDAMENTAL_GROUP_GEN_LABELS : FACETS, GRAPH, CONNECTED
   client("fundamental_group", $this);


# category: Topology
# Writes the @see FUNDAMENTAL_GROUP using @see FUNDAMENTAL_GROUP_GEN_LABELS to
# the given file in @see external.GAP input format.

user_method fundamental2gap(;$) {
   my ($this, $gap_file)=@_;
   $gap_file ||= $this->name . ".gap";
   open my $out, ">", "$gap_file" or die "fundamental2gap: can't open $gap_file for writing";
   my @gen_labels = split /\s+/, $this->FUNDAMENTAL_GROUP_GEN_LABELS;
   my @relations = $this->FUNDAMENTAL_GROUP->[0] =~ /\{[^{}]+\}/g;

   foreach (@relations) {
      my @terms;
      while (m/\( (\d+) \s+ (\d+) \)/gx) {
	 push @terms, "$gen_labels[$1]" . ( $2 ? "^-1" : "");
      }
      $_ = join("*", @terms), "\n";
   }
   foreach (@gen_labels) {
      $_ = "\"$_\"";
   }
   print $out "F := FreeGroup(", join(", ",@gen_labels), ");\n";
   print $out "AssignGeneratorVariables(F);\n";
   print $out "G := F / [",join(", ",@relations),"];\n";
   print $out <<'.';
Print("FreeGroup is called F, after division by relators it's called G.\n");
Print("Use H:=SimplifiedFpGroup(G) to get a simplified representation H of the group G.\n");
.
   $gap_file;	# as return value
}


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
