#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.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: singular.rules 9502 2010-02-24 00:26:29Z gawrilow $

# path to the Singular executable
custom $Singular;

CONFIGURE {
   find_via_path($Singular, "Singular") or return;

   # check the version
   open SINGULAR_OUTPUT, "echo 'quit' | $Singular |";
   local $/;
   my $version=<SINGULAR_OUTPUT>;
   close SINGULAR_OUTPUT;
   $version=~/version\s+([\d-]+)/ and $version=$1;
   $version=~s/-/./;
   eval("v$version") ge v3.0.0
      or die <<'.';
polymake requires Singular v3.0.0 or newer;
Please download the current version
and repeat the auto-configuration.
.
}

# @topic credits/singular
# SINGULAR is a Computer Algebra System for polynomial computations with special emphasis on the needs of commutative algebra, algebraic geometry, and singularity theory.
# Copyright by the SINGULAR team.
# http://www.singular.uni-kl.de/

object TropicalPolytope {

# @Category Tropical convex hulls
# Use the Block and Yu algorithm via Singular.
label singular

# the following rule is incomplete in the sense that it is possible
# to derive more information from the Singular output

# Block, Florian(D-MUTU-ZMG); Yu, Josephine(1-CA)
#   Tropical convexity via cellular resolutions.
#   J. Algebraic Combin. 24 (2006), no. 1, 103--114.

rule singular.pseudovertex_types : PSEUDOVERTEX_TYPES : POINTS, AMBIENT_DIM {
  my $d=$this->AMBIENT_DIM+1;
  my @points=@{$this->POINTS};
  my $n=@points;
  my $dim= (($n<=$d)? $n : $d)-1;

  my $tempname=new Tempfile;
  open SINGULAR_FILE, ">$tempname"
    or die "can't create temporary file $tempname: $!";

  print SINGULAR_FILE << ".";
ring R = 0, (
.

  my @var_list=();
  for (my $i=0; $i<$n; ++$i) {
    for (my $j=0; $j<$d; ++$j) {
      push @var_list, "x_".$i."_".$j;
    }
  }
  my $var_string = join ",", @var_list;
  print SINGULAR_FILE << ".";
$var_string),
(wp(
.

  my $i=0;
  foreach (@points) {
    chomp;
    s/\s+/,/g;
    print SINGULAR_FILE ",\n" unless $i==0;
    print SINGULAR_FILE "$_";
    ++$i;
  }

print SINGULAR_FILE << ".";
));
matrix M[$n][$d]=
$var_string;
ideal J=std(minor(M,2));
ideal I=lead(J);
ideal var_squares=
.

  print SINGULAR_FILE join ",", map { "$_^2" } @var_list;
  print SINGULAR_FILE << ".";
;
ideal std_var_squares=std(var_squares);
ideal Q=quotient(std_var_squares,I);
ideal QQ=reduce(Q,std_var_squares);
ideal dualI=prune(QQ);
resolution Res=minres(res(dualI,$dim));
print(Res);
quit;
.

  close SINGULAR_FILE;

  my @pseudovertex_types=();
  open SINGULAR_OUTPUT, "$Singular $tempname|";
  local $_;
  while (<SINGULAR_OUTPUT>) {
    last if m/^\[1\]/;
  }
  while (<SINGULAR_OUTPUT>) {
    last if (m /^\[/) or (m /^Auf Wiedersehen/);
    chomp;
    my $res= s/\s+_\[(\d+)\]=//; # $1 is the index
    last unless $res;
    my @vars= split /\*/,$_;
    my @types=();
    for (my $j=0; $j<$d; ++$j) {
      push @types, [];
    }
    my $k=0;
    for (my $i=0; $i<$n; ++$i) {
      for (my $j=0; $j<$d; ++$j) {
	if ($vars[$k] eq "x_".$i."_".$j) {
	  ++$k;
        } else {
	  push @{$types[$j]}, $i;
        }
      }
    }
    push @pseudovertex_types, \@types;
  }
  close SINGULAR_OUTPUT;

  $this->PSEUDOVERTEX_TYPES=\@pseudovertex_types;
}
weight 4.30;
permutation : PseudoVertexPerm;

}

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