########################################################################
#  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
#  of the License, or (at your option) any later version.
#  
#  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.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA  02110-1301, USA.
#  
#  ---
#  Copyright (C) 2011-2015, Simon Hampe <simon.hampe@googlemail.com>
#
#
#	This file contains functions for general intersection-theoretic computations	
#
########################################################################

# @category Intersection theory
# Computes the degree of a tropical variety as the total weight of the
# 0-dimensional intersection product obtained by intersecting with the
# complementary uniform linear space.
# @param Cycle A tropical cycle
# @return Integer The degree 
user_function degree<Addition>(Cycle<Addition>) {
	my $X = shift;

	if(is_empty($X)) { return 0; }

	my $dim = $X->PROJECTIVE_DIM;

	if($dim == 0) {
		return $X->WEIGHTS * ones_vector<Integer>($X->WEIGHTS->dim());
	}

	my $ambient_dim = $X->PROJECTIVE_AMBIENT_DIM;

   my $expnum = unit_matrix<Int>($ambient_dim+1); 
   my $coeffnum = ones_vector<TropicalNumber<Addition>>($ambient_dim+1); 
	my $num = new Polynomial<TropicalNumber<Addition>>($coeffnum, $expnum);
	my $expden = new Matrix<Int>(1,$ambient_dim+1);
   $expden->elem(0,0) = 1;
   my $coeffden = new Vector<TropicalNumber<Addition>>([0]);
   my $den = new Polynomial<TropicalNumber<Addition>>($coeffden, $expden);
   
   my $rat_fct = new RationalFunction<Addition>(NUMERATOR=>$num, DENOMINATOR=>$den);

	my $div = divisor( $X, $rat_fct^$dim);
	return $div->WEIGHTS * ones_vector<Integer>($div->WEIGHTS->dim());
}
