/* 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: pluecker.cc 10095 2011-03-28 13:11:26Z sherrmann $
*/

#include "polymake/client.h"
#include "polymake/Rational.h"
#include "polymake/Vector.h"
#include "polymake/Matrix.h"
#include "polymake/PowerSet.h"
#include "polymake/list"
#include <string>
#include <algorithm>

namespace polymake { namespace tropical {

namespace {
inline Rational tdet1(Matrix<Rational>& M, Vector<int>& perm) {
   const int d=M.rows();
   Rational v=0;
   for (int i=0; i<d; ++i) v+=M(i,perm[i]);
   return v;
}

// FIXME: this should be computed via the Hungarian method!
Rational tdet(Matrix<Rational> M) {
   const int d=M.rows();
   Vector<int> perm(d, sequence(0).begin());
   Rational v=tdet1(M,perm);
   while (std::next_permutation(perm.begin(),perm.end())) {
      const Rational x=tdet1(M,perm);
      if (x<v) v=x;
   }
   return v;
}
}

Vector<Rational> pluecker(Matrix<Rational> V)
{
   const int n(V.rows()), d(V.cols());
   const int nd_d=Integer::binom(d+n,d);
   Vector<Rational> pi(nd_d);

   const sequence all_rows = sequence(0,d+n); // d extra rows plus n given ones
   int i=0;

   // first merging given and extra rows and then taking them apart again
   // yields the desired ordering of the coefficients of the tropical Pluecker vector
   for (Entire< Subsets_of_k<const sequence&> >::const_iterator rho = entire(all_subsets_of_k(all_rows,d));
        !rho.at_end(); ++rho) {
      const Set<int> ri(*rho);
      Set<int> sigma, tau;
      for (Entire< Set<int> >::const_iterator rii=entire(ri); !rii.at_end(); ++rii)
         // take given and extra rows apart
         if (*rii < d)
            tau.push_back(*rii);
         else
            sigma.push_back(*rii-d);
      pi[i]=tdet(V.minor(sigma,~tau));
      ++i;
   }
   return pi;
}

UserFunction4perl("# @category Other"
                  "# Compute tropical Pluecker vector from matrix representing points in tropical torus."
                  "# Can be used to lift regular subdivision of ordinary product of simplices to"
                  "# matroid decomposition of hypersimplices."
                  "# @param Matrix V",
                  &pluecker,"pluecker($)");
} }

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
