#  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.
#-------------------------------------------------------------------------------

object Hypersurface { 

# Homogeneous tropical polynomial defining the hypersurface.

property POLYNOMIAL : Polynomial<TropicalNumber<Addition>>;

# Each row corresponds to one of the monomials in [[POLYNOMIAL]], each column to a variable.

property MONOMIALS : Matrix<Int>;

# Each row corresponds to one of the monomials in [[POLYNOMIAL]], each column to a variable.

property COEFFICIENTS : Vector<TropicalNumber<Addition>>;

# Indices of [[MONOMIALS]] which do not define facets of the [[DOME]].

property REDUNDANT_MONOMIALS : Set;

# The __dome__ of a tropical polynomial \(F:\mathbb R^d\to\mathbb R\) (and the corresponding tropical hypersurface) is the set 
# \[D(F)=\left\{(p,s)\in\mathbb R^{d+1}\mid p\in\mathbb R^d, s\in\mathbb R, s \oplus F(p) = s\right\}.\]
# It is an unbounded convex polyhedron, c.f.
# 
# 	Michael Joswig, [[http://page.math.tu-berlin.de/~joswig/etc/ETC-130412.pdf|Essentials of Tropical Combinatorics]], Chapter 1.

property DOME : polytope::Polytope<Rational>;


# Connected components of the complement.
# Rows correspond to facets of the [[DOME]], i.e. non-redundant [[MONOMIALS]] , columns correspond to [[VERTICES]].

property REGIONS : IncidenceMatrix;


rule MONOMIALS, COEFFICIENTS : POLYNOMIAL {
   my $p=$this->POLYNOMIAL;
   $this->MONOMIALS=$p->monomials_as_matrix();
   $this->COEFFICIENTS=$p->coefficients_as_vector();
}
weight 0.10;

rule POLYNOMIAL : MONOMIALS, COEFFICIENTS {
   my $ring = new Ring<TropicalNumber<Addition> >($this->MONOMIALS->cols());
   $this->POLYNOMIAL = new Polynomial<TropicalNumber<Addition> >($this->MONOMIALS, $this->COEFFICIENTS,$ring);
}
weight 0.10;

rule DOME, PROJECTIVE_VERTICES, LINEALITY_SPACE, FAR_VERTICES, MAXIMAL_POLYTOPES, REDUNDANT_MONOMIALS, REGIONS, WEIGHTS : MONOMIALS, COEFFICIENTS {
   hypersurface($this);
}



# Subdivision of the Newton polytope dual to the tropical hypersurface.
# The vertices of this PolyhedralComplex are the non-redundant [[MONOMIALS]].
# @return fan::PolyhedralComplex<Rational>
user_method dual_subdivision : MONOMIALS, REDUNDANT_MONOMIALS, REGIONS, FAR_VERTICES {
   my $this=shift;
   my $red=$this->REDUNDANT_MONOMIALS;
   my $n=$this->MONOMIALS->rows()-$red->size();
   my $homogenized=new Matrix<Rational>((new Vector<Int>(ones_vector($n)))|$this->MONOMIALS->minor(~$red,All));
   my $d=$homogenized->cols();
   my $dual_cells=transpose(new IncidenceMatrix($this->REGIONS));
   new fan::PolyhedralComplex<Rational>(VERTICES=>$homogenized,
                                        MAXIMAL_POLYTOPES=>$dual_cells->minor(~$this->FAR_VERTICES,All),
                                        LINEALITY_SPACE=>[],
                                        FAN_AMBIENT_DIM=>$d);
}



} # object Hypersurface

# @category Producing a tropical hypersurface
# Create a tropical hyperplane as object of type Hypersurface.
# @tparam Addition [[Min]] or [[Max]]
# @param Vector<TropicalNumber<Addition> > coeffs coefficients of the tropical linear form 
# (can also be specified as anonymous array).
# @return Hypersurface<Addition>

user_function hyperplane<Addition>($)  {
    my $pre_coefs = shift;
	 my $coefs = new Vector<TropicalNumber<Addition> >($pre_coefs);
    my $d=$coefs->dim();
    return new Hypersurface<Addition>(MONOMIALS=>unit_matrix($d),COEFFICIENTS=>$coefs);
}




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