/* Copyright (c) 1997-2010
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   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.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: types.cc 9570 2010-03-15 23:21:23Z gawrilow $
*/

#include <polymake.h>
#include <Set.h>
#include <Array.h>
#include <Matrix.h>

namespace polymake { namespace tropical {

template <typename Coord>
Array< Array < Set <int> > >  types(const Matrix<Coord>& points, const Matrix<Coord>& generators)
{
   const int m(points.rows()), n(generators.rows()), d(generators.cols());
   Array< Array < Set<int> > > types(m);

   for (int j=0; j<m; ++j) { // determine the type of each point
      Array< Set<int> > this_type(d);

      for (int i=0; i<n; ++i) { // iterate over all generators
         Vector<Coord> diff = generators[i]-points[j];
         Coord m = accumulate(diff, operations::min());
         for (int k=0; k<d; ++k)
            if (diff[k]==m) this_type[k] += i;
      }

      types[j] = this_type;
   }

   return Array< Array< Set <int> > >(types);
}

template <typename Coord>
Array< Array<int> >  coarse_types(const Matrix<Coord>& points, const Matrix<Coord>& generators)
{
   const int m(points.rows()), n(generators.rows()), d(generators.cols());
   Array< Array<int> > coarse_types(m);

   for (int j=0; j<m; ++j) { // determine the coarse type of each point
      Array<int> this_coarse_type(d);

      for (int i=0; i<n; ++i) { // iterate over all generators
         Vector<Coord> diff = generators[i]-points[j];
         Coord m = accumulate(diff, operations::min());
         for (int k=0; k<d; ++k)
            if (diff[k]==m) ++this_coarse_type[k];
      }

      coarse_types[j] = this_coarse_type;
   }

   return Array< Array<int> >(coarse_types);
}

UserFunctionTemplate4perl("# @category Other"
			  "# Compute the fine types of the //points// set relative to a set of //generators//."
			  "#  The following are two typical cases:"
			  "#  	(1) //points// = [[TropicalPolytope::VERTICES|VERTICES]] and //generators// = [[TropicalPolytope::VERTICES|VERTICES]]"
			  "#  	(2) //points// = [[TropicalPolytope::POINTS|POINTS]]  and //generators// = [[TropicalPolytope::PSEUDOVERTICES|PSEUDOVERTICES]]"
			  "# @param Matrix points"
			  "# @param Matrix generators"
			  "# @return Array<Array<Set>>",
			  "types<Coord>(Matrix<Coord> Matrix<Coord>)");

UserFunctionTemplate4perl("# @category Other"
			  "# Compute the coarse types of the //points// set relative to a set of //generators//."
			  "#  The following are two typical cases:"
			  "#  	(1) //points// = [[TropicalPolytope::VERTICES|VERTICES]] and //generators// = [[TropicalPolytope::VERTICES|VERTICES]]"
			  "#  	(2) //points// = [[TropicalPolytope::POINTS|POINTS]]  and //generators// = [[TropicalPolytope::PSEUDOVERTICES|PSEUDOVERTICES]]"
			  "# @param Matrix points"
			  "# @param Matrix generators"
			  "# @return Array< Array<int>>",
			  "coarse_types<Coord>(Matrix<Coord> Matrix<Coord>)");
} }
