#  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: common.rules 7473 2006-11-22 02:55:05Z witte $

INCLUDE
   complex_properties.rules
   gap.rules
   morse_matching.rules

object SimplicialComplex;

FACETS, VERTEX_LABELS : INPUT_FACES
  client("faces_to_facets", $this, "INPUT_FACES");

FACETS : HASSE_DIAGRAM
  client("facets_from_hasse_diagram", $this);

PURE, DIM : FACETS
WEIGHT 1.10
   my @simplex = $this->FACETS->[0] =~ /\d+/g;
   my $dim = $#simplex;
   my $pure = 1;
   foreach my $facet (@{$this->FACETS}) {
      @simplex = $facet =~ /\d+/g;
      if ($dim != $#simplex) {
	 $pure = 0;
      }
      if ($dim < $#simplex) {
	 $dim = $#simplex;
      }
   }
   $this->DIM=$dim;
   $this->PURE=$pure;

N_FACETS : FACETS
WEIGHT 0.10
   $this->N_FACETS=@{$this->FACETS};

N_MINIMAL_NON_FACES : MINIMAL_NON_FACES
WEIGHT 0.10
   $this->N_MINIMAL_NON_FACES=@{$this->MINIMAL_NON_FACES};

DIM : FACETS
PRECONDITION: PURE
   $this->PURE
WEIGHT 0.10
   my @simplex = $this->FACETS->[0] =~ /\d+/g;
   $this->DIM=$#simplex;

PURE :
PRECONDITION: PSEUDO_MANIFOLD
   $this->PSEUDO_MANIFOLD
WEIGHT 0.10
   $this->PURE=1;

PURE :
PRECONDITION: MANIFOLD
   $this->MANIFOLD
WEIGHT 0.10
   $this->PURE=1;

GRAPH : FACETS
WEIGHT 2.10
   client("t_graph", $this);

DUAL_GRAPH : FACETS
   client("t_dual_graph", $this);

VERTEX_DEGREES : GRAPH
WEIGHT 1.10
   $this->VERTEX_DEGREES=join(" ", row_sizes($this->GRAPH));

CONNECTED : GRAPH
WEIGHT 2.10
   client("connected", $this, "GRAPH", "CONNECTED");

DUAL_CONNECTED : DUAL_GRAPH
WEIGHT 2.10
   client("connected", $this, "DUAL_GRAPH", "DUAL_CONNECTED");

CONNECTED_COMPONENTS : GRAPH
WEIGHT 2.10
   client("connected_comp", $this, "GRAPH", "CONNECTED_COMPONENTS");

N_CONNECTED_COMPONENTS : CONNECTED_COMPONENTS
WEIGHT 0.10
   $this->N_CONNECTED_COMPONENTS=@{$this->CONNECTED_COMPONENTS};

DUAL_CONNECTED_COMPONENTS : DUAL_GRAPH
WEIGHT 2.10
   client("connected_comp", $this, "DUAL_GRAPH", "DUAL_CONNECTED_COMPONENTS");

MAX_CLIQUES : GRAPH
WEIGHT 2.10
   client("max_cliques", $this, "GRAPH", "MAX_CLIQUES");

DUAL_MAX_CLIQUES : DUAL_GRAPH
WEIGHT 2.10
   client("max_cliques", $this, "DUAL_GRAPH", "DUAL_MAX_CLIQUES");

CONNECTIVITY : GRAPH
   client("connectivity", $this, "GRAPH", "CONNECTIVITY")

DUAL_CONNECTIVITY : DUAL_GRAPH
   client("connectivity", $this, "DUAL_GRAPH", "DUAL_CONNECTIVITY")

BIPARTITE : GRAPH
WEIGHT 2.10
   client("bipartite", $this, "GRAPH", "BIPARTITE");

DUAL_BIPARTITE : DUAL_GRAPH
WEIGHT 2.10
   client("bipartite", $this, "DUAL_GRAPH", "DUAL_BIPARTITE");

DUAL_BIPARTITE : DUAL_GRAPH_SIGNATURE
WEIGHT 0.10
  $this->DUAL_BIPARTITE = $this->DUAL_GRAPH_SIGNATURE >= 0;

DUAL_BIPARTITE : SIGNATURE
WEIGHT 0.10
  $this->DUAL_BIPARTITE = $this->SIGNATURE >= 0;

GRAPH_SIGNATURE, BIPARTITE : GRAPH
WEIGHT 2.10
  client("bipartite", $this, "GRAPH", "BIPARTITE", "GRAPH_SIGNATURE");

DUAL_GRAPH_SIGNATURE, DUAL_BIPARTITE : DUAL_GRAPH
WEIGHT 2.10
  client("bipartite", $this, "DUAL_GRAPH", "DUAL_BIPARTITE", "DUAL_GRAPH_SIGNATURE");

FOLDABLE : COLORING
PRECONDITION: COLORING, DIM
  my $dim = $this->DIM;
  my %colors;
  foreach my $c ($this->COLORING =~ /\d+/g) {
    $colors{$c} += 1;
  }
  keys %colors == $dim+1
WEIGHT 0.10
  $this->FOLDABLE = 1;

FOLDABLE : DUAL_BIPARTITE
PRECONDITION: BALL
   $this->BALL
WEIGHT 0.10
  $this->FOLDABLE = $this->DUAL_BIPARTITE;

FOLDABLE : DUAL_BIPARTITE
PRECONDITION: SPHERE
   $this->SPHERE
WEIGHT 0.10
  $this->FOLDABLE = $this->DUAL_BIPARTITE;

COLORING : GRAPH
WEIGHT 5.10
  client("greedy_coloring", $this, "GRAPH", "COLORING")

COLORING : PROJ_DICTIONARY
PRECONDITION: MANIFOLD
  $this->MANIFOLD;
PRECONDITION: PROJ_ORBITS, DIM
  my $dim = $this->DIM;
  my @proj_orbits = $this->PROJ_ORBITS->[0] =~ /\{[^{}]+\}/g;
  @proj_orbits == $dim+1;
WEIGHT 0.10
  $this->COLORING = $this->PROJ_DICTIONARY;

DUAL_COLORING : DUAL_GRAPH
WEIGHT 5.10
  client("greedy_coloring", $this, "DUAL_GRAPH", "DUAL_COLORING")

F_VECTOR : FACETS, DIM, PURE
WEIGHT 3.20
  client("t_f_vector", $this)

F_VECTOR, F2_VECTOR : HASSE_DIAGRAM
WEIGHT 3.10
   client("f2_vector", $this)

EULER_CHARACTERISTIC : F_VECTOR
WEIGHT 0.40
   my @f_vector = $this->F_VECTOR =~ /\d+/g;
   my $euler_char = -1;
   my $sign = 1;
   foreach my $f (@f_vector) {
      $euler_char += $sign*$f;
      $sign = -$sign
   }
   $this->EULER_CHARACTERISTIC=$euler_char;

N_VERTICES : FACETS
WEIGHT 1.10
   client("n_vertices", $this);

GEOMETRIC_REALIZATION, G_DIM : N_VERTICES
WEIGHT 1.10
   my $nv=$this->N_VERTICES;
   $this->GEOMETRIC_REALIZATION=[
      map {
	 my @coordinates = (0)x$nv;
	 $coordinates[$_]=1;
	 "@coordinates";
      } 0..$nv-1
   ];
   $this->G_DIM=$nv;

G_DIM : GEOMETRIC_REALIZATION
WEIGHT 0.10
   my @vertex0= $this->GEOMETRIC_REALIZATION->[0] =~ /\S+/g;
   $this->G_DIM=@vertex0;

default.homology: HOMOLOGY : FACETS, DIM
WEIGHT 4.10
   client("t_homology", $this, "FACETS", "HOMOLOGY");

COHOMOLOGY : FACETS, DIM
WEIGHT 4.10
   client("t_homology", $this, "FACETS", "-co", "COHOMOLOGY");

default.homology: HOMOLOGY, CYCLES : FACETS, DIM
WEIGHT 4.50
   client("t_homology", $this, "FACETS", "HOMOLOGY", "CYCLES");

COHOMOLOGY, COCYCLES : FACETS, DIM
WEIGHT 4.50
   client("t_homology", $this, "FACETS", "-co", "COHOMOLOGY", "COCYCLES");

INTERSECTION_FORM : CYCLES, COCYCLES
WEIGHT 2.10
   client("intersection_form", $this, "CYCLES", "COCYCLES", "INTERSECTION_FORM");

STIEFEL_WHITNEY : FACETS
PRECONDITION: CLOSED_PSEUDO_MANIFOLD
   $this->CLOSED_PSEUDO_MANIFOLD
WEIGHT 5.10
   client("stiefel_whitney", $this, "STIEFEL_WHITNEY");

ODD_SUBCOMPLEX : FACETS
PRECONDITION: PURE
   $this->PURE
WEIGHT 3.10
   client("odd_complex", $this);

ODD_SUBCOMPLEX : FACETS, BOUNDARY_OF_PSEUDO_MANIFOLD
PRECONDITION: MANIFOLD
   $this->MANIFOLD
WEIGHT 2.10
   client("odd_complex_of_manifold", $this);
 
SURFACE : DIM, CONNECTED, MANIFOLD
WEIGHT 0.10
   $this->SURFACE= $this->CONNECTED && $this->MANIFOLD && $this->DIM==2;

GENUS : ORIENTED_PSEUDO_MANIFOLD, EULER_CHARACTERISTIC
PRECONDITION: SURFACE, CLOSED_PSEUDO_MANIFOLD
  $this->SURFACE && $this->CLOSED_PSEUDO_MANIFOLD
WEIGHT 0.20
  my $euler_char = $this->EULER_CHARACTERISTIC;
  if ($this->ORIENTED_PSEUDO_MANIFOLD) {
    $this->GENUS = (1-$euler_char)/2;
  } else {
    $this->GENUS = 1-$euler_char;
  }

MANIFOLD :
PRECONDITION: BALL
  $this->BALL
WEIGHT 0.10
  $this->MANIFOLD=1;

MANIFOLD :
PRECONDITION: SPHERE
  $this->SPHERE
WEIGHT 0.10
  $this->MANIFOLD=1;

MANIFOLD : FACETS, DIM
PRECONDITION: DIM
   $this->DIM < 4
WEIGHT 4.10
   client("is_manifold", $this, "-quiet");

MANIFOLD : HASSE_DIAGRAM, PSEUDO_MANIFOLD, CLOSED_PSEUDO_MANIFOLD
WEIGHT 4.50
   client("is_manifold_h", $this, "-quiet");

PSEUDO_MANIFOLD :
PRECONDITION: MANIFOLD
   $this->MANIFOLD
WEIGHT 0.10
   $this->PSEUDO_MANIFOLD=1;

PSEUDO_MANIFOLD :
PRECONDITION: CLOSED_PSEUDO_MANIFOLD
   $this->CLOSED_PSEUDO_MANIFOLD
WEIGHT 0.10
   $this->PSEUDO_MANIFOLD=1;

PSEUDO_MANIFOLD :
PRECONDITION: ORIENTED_PSEUDO_MANIFOLD
   $this->ORIENTED_PSEUDO_MANIFOLD
WEIGHT 0.10
   $this->PSEUDO_MANIFOLD=1;

PSEUDO_MANIFOLD : HASSE_DIAGRAM
WEIGHT 1.50
   client("is_pseudo_manifold", $this);

CLOSED_PSEUDO_MANIFOLD : HASSE_DIAGRAM
WEIGHT 1.50
   client("is_closed_pseudo_manifold", $this);

ORIENTED_PSEUDO_MANIFOLD, ORIENTATION : FACETS, DUAL_GRAPH, PSEUDO_MANIFOLD
   client("orientation", $this);

BALL : HASSE_DIAGRAM, CLOSED_PSEUDO_MANIFOLD, PSEUDO_MANIFOLD
WEIGHT 4.10
  client("is_ball_h", $this, "-quiet");

BALL : FACETS, DIM, CLOSED_PSEUDO_MANIFOLD
PRECONDITION: DIM
   $this->DIM < 3
WEIGHT 2.10
   client("is_ball", $this);

SPHERE : HASSE_DIAGRAM, CLOSED_PSEUDO_MANIFOLD
WEIGHT 4.10
  client("is_sphere_h", $this, "-quiet");

SPHERE : FACETS, DIM, CLOSED_PSEUDO_MANIFOLD
PRECONDITION: DIM
   $this->DIM < 3
WEIGHT 2.10
   client("is_sphere", $this);

HASSE_DIAGRAM : FACETS, DIM
PRECONDITION: PURE
   $this->PURE
WEIGHT 4.10
   client("t_hasse_diagram", $this, "-pure");

HASSE_DIAGRAM : FACETS, DIM
PRECONDITION: PURE
   !$this->PURE
WEIGHT 4.10
   client("t_hasse_diagram", $this);

MINIMAL_NON_FACES : HASSE_DIAGRAM
WEIGHT 4.10 : DIM
   $this->DIM <= 10 ? [0,0] : [4,10]
BODY:
   client("minimal_non_faces", $this);

MINIMAL_NON_FACES : FACETS, DIM, N_VERTICES
WEIGHT 4.10 : DIM
   $this->DIM > 10 ? [0,0] : [4,10]
BODY:
   client("lawler", $this);

LOCALLY_STRONGLY_CONNECTED :
PRECONDITION: MANIFOLD
   $this->MANIFOLD
WEIGHT 0.10
   $this->LOCALLY_STRONGLY_CONNECTED=1;

LOCALLY_STRONGLY_CONNECTED : HASSE_DIAGRAM
WEIGHT 2.10
   client("is_locally_strongly_connected", $this);

BOUNDARY_OF_PSEUDO_MANIFOLD : HASSE_DIAGRAM
PRECONDITION: PSEUDO_MANIFOLD
   $this->PSEUDO_MANIFOLD
WEIGHT 1.10
   client("boundary_of_pseudo_manifold", $this, "BOUNDARY_OF_PSEUDO_MANIFOLD");

VERTEX_LABELS : N_VERTICES
WEIGHT 0.10
   my @labels = (0..$this->N_VERTICES-1);
   $this->VERTEX_LABELS="@labels";

PROJ_ORBITS, PROJ_DICTIONARY : N_VERTICES, DUAL_GRAPH, FACETS, DUAL_CONNECTED
PRECONDITION: DUAL_CONNECTED
   $this->DUAL_CONNECTED
WEIGHT 1.10
   client("projectivities", $this, "PROJ_ORBITS", "PROJ_DICTIONARY");

VOLUME : FACETS, GEOMETRIC_REALIZATION, DIM
  client("t_volume", $this);

SIGNATURE : DUAL_GRAPH, GEOMETRIC_REALIZATION, FACETS, DIM, G_DIM
  client("t_signature", $this);


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