#  Copyright (c) 1997-2021
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://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 Ideal {

   rule initial : GENERATORS {
      my $gens = $this->GENERATORS;
      my $nvars = $gens->[0]->n_vars();
      foreach my $p (@$gens){
         if($p->n_vars() != $nvars){
            croak("\ninvalid input: Number of variables needs to be the same for all GENERATORS");
            return;
         }
      }
   }
   precondition : !exists(N_VARIABLES);
   precondition : exists(GENERATORS);
	precondition : GENERATORS {
		$this->GENERATORS->size() > 0;
	}

   rule initial : GENERATORS, N_VARIABLES {
      my $gens = $this->GENERATORS;
      my $nvars = $this->N_VARIABLES;
      foreach my $p (@$gens){
         if($p->n_vars() != $nvars){
            croak("\ninvalid input: N_VARIABLES does not agree with the number of variables of the GENERATORS");
            return;
         }
      }
   }
   precondition : exists(N_VARIABLES);
   precondition : exists(GENERATORS);

   rule PRIMARY : {
      $this->PRIMARY = 1;
   }
   precondition : PRIME;
   weight 0.10;

   rule PRIMARY : RADICAL.PRIME {
      $this->PRIMARY = $this->RADICAL->PRIME;
   }
   weight 0.10;

   rule ZERO : GENERATORS {
      foreach my $p (@{$this->GENERATORS}){
         if(!$p->trivial){
            $this->ZERO = 0;
            return;
         }
      }
      $this->ZERO = 1;
   }
   weight 1.10;

	rule N_VARIABLES : GENERATORS {
		$this->N_VARIABLES = $this->GENERATORS->[0]->n_vars();
	}
	precondition : GENERATORS {
		$this->GENERATORS->size() > 0;
	}
	weight 0.10;

    rule GENERATORS = GROEBNER.BASIS;

}

object Groebner {

   rule initial :  {
      if (my $order = $this->lookup("ORDER_NAME")) {
         $order =~ /^(dp|Dp|lp|Lp|rp|ls|rs|ds|Ds)$/ or croak ("unknown term order");
      } elsif ( ! defined($this->lookup("ORDER_VECTOR | ORDER_MATRIX"))) {
         croak ("no term order specified");
      }
   }

   rule INITIAL_FORMS : BASIS, ORDER_VECTOR {
      $this->INITIAL_FORMS = [ map { $_->initial_form($this->ORDER_VECTOR) } @{$this->BASIS} ];
   }
   weight 1.10;

   rule INITIAL_FORMS : BASIS, ORDER_MATRIX {
      $this->INITIAL_FORMS = [ map { $_->initial_form($this->ORDER_MATRIX->row(0)) } @{$this->BASIS} ];
   }
   weight 1.10;

}

object_specialization Ideal::Binomial {

   rule N_VARIABLES : BINOMIAL_GENERATORS {
      $this->N_VARIABLES = $this->BINOMIAL_GENERATORS->cols;
   }

   rule GENERATORS : BINOMIAL_GENERATORS {
      my $binomials = $this->BINOMIAL_GENERATORS;
      my @gens;
      my $coef = new Vector<Rational>(1, -1);
      foreach my $binomial (@$binomials){
         my $positivePart = new Vector<Int>(map($_>0 ? $_ : 0, @$binomial));
         my $negativePart = new Vector<Int>(map($_<0 ? -$_ : 0, @$binomial));
         my $expmat = new Matrix<Int>([$positivePart, $negativePart]);
         push @gens, new Polynomial($coef, $expmat);
      }
      $this->GENERATORS(temporary) = \@gens;
   }

   property GROEBNER {

        rule BASIS : BINOMIAL_BASIS {
             my $binomials = $this->BINOMIAL_BASIS;
             my @gens;
             my $coef = new Vector<Rational>(1, -1);
             foreach my $binomial (@$binomials){
                my $positivePart = new Vector<Int>(map($_>0 ? $_ : 0, @$binomial));
                my $negativePart = new Vector<Int>(map($_<0 ? -$_ : 0, @$binomial));
                my $expmat = new Matrix<Int>([$positivePart, $negativePart]);
                push @gens, new Polynomial($coef, $expmat);
             }
             $this->BASIS(temporary) = \@gens;
        }

   }

   rule BINOMIAL_GENERATORS = GROEBNER.BINOMIAL_BASIS;
}

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