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

CREDIT azove
  azove is a tool designed for counting (without explicit enumeration) and enumeration of 0/1 vertices.
  Copyright by Markus Behle
  http://www.mpi-inf.mpg.de/~behle/azove.html

# path to the "azove" executable
custom $azove;

CONFIGURE {
   find_program($azove, qw(azove2 azove));
}

object Polytope<Rational> {

# @category Geometry
# Use the [[wiki:external_software#azove|azove]] program for counting 0/1-points in a polytope.
label azove

rule azove.points01: N_01POINTS : CONE_AMBIENT_DIM, FACETS | INEQUALITIES, AFFINE_HULL | EQUATIONS {
   my $dim = $this->CONE_AMBIENT_DIM-1;
   my $ineqs = eliminate_denominators_in_rows($this->FACETS|INEQUALITIES);
   if (defined (my $AFFINE_HULL=$this->lookup("AFFINE_HULL | EQUATIONS"))) {
     my $eqs = eliminate_denominators_in_rows($AFFINE_HULL);
     $ineqs /= $eqs / (-$eqs);
   }

# write the ine-file
   my $tempname=new Tempfile;
   open(P, ">$tempname.ine")
     or die "can't create temporary file $tempname.ine: $!";
   print P "* Automatically generated by polymake.\n",
           "H-representation\n",
           "begin\n", $ineqs->rows, " ", $dim+1, " integer\n",
           dense($ineqs),	# polymake and azove have the same format, but azove does not know how to handle sparse matrices
           "end\n";
   close P;

# call azove
   open P, "$azove -c $tempname.ine 2>&1 |"
   or die "couldn't run $azove: $!";
   local $_;
   while (<P>) {
      if (my ($n)=/^Number of 0\/1 vertices =\s+(\d+)/) {
	$this->N_01POINTS=$n;
	close P;
	return;
      }
   }
   die "can't parse output from $azove";
}

}


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