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

# A normal toric variety that is affine, i.e., given by a cone.

declare object_specialization AffineNormalToricVariety = NormalToricVariety {
   
   precondition : AFFINE;

   # @category Combinatorics
   # The weight cone of the algebra yielding the affine normal toric variety.
   # I.e., intersect this cone with the lattice, take the semigroup algebra over
   # it and take the spectrum.
   property WEIGHT_CONE : Cone;

   property DIVISOR {

      # @category Combinatorics
      # Exponents of the fractional ideal of global sections. This property only
      # makes sense for affine toric varieties.
      # @depends 4ti2 or libnormaliz
      property MODULE_GENERATORS : Matrix<Integer> {

         sub equal { &equal_up_to_row_permutation; }

      }

   }

   rule WEIGHT_CONE.INEQUALITIES : RAYS {
      $this->WEIGHT_CONE->INEQUALITIES = $this->RAYS;
   }

   rule DIVISOR.MODULE_GENERATORS : DIVISOR.SECTION_POLYTOPE {
      my $section_polytope = $this->DIVISOR->SECTION_POLYTOPE;
      my $result = lower_lattice_points($section_polytope);
      $this->DIVISOR->MODULE_GENERATORS = $result->minor(All, ~[0]);
   }

}


# @category Commutative Algebra
# Find all lattice points of a polytope P that are not reachable
# from some other lattice point via the tail cone.
user_function lower_lattice_points(Polytope){
   my ($P) = @_;
   my $C = new Cone($P);
   my $H = $C->HILBERT_BASIS_GENERATORS->[0];
   return new Matrix(grep($_->[0]==1,@$H));
}


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