########################################################################
#  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 $ring = new Ring<TropicalNumber<Addition> >($ambient_dim+1);
	my $num = new Polynomial<TropicalNumber<Addition> >($ring);
	my @vars = $ring->variables;
	for my $v (@vars) { 
		$num = $num + $v;
	}
	my $rat_fct = new RationalFunction<Addition>(NUMERATOR=>$num, DENOMINATOR=>$vars[0]);

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