/* Copyright (c) 1997-2011
   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: balanced_prism.cc 10225 2011-05-24 12:17:04Z joswig $
*/

#include "polymake/client.h"
#include "polymake/Matrix.h"
#include "polymake/Vector.h"
#include "polymake/Rational.h"
#include "polymake/topaz/complex_tools.h"
#include "polymake/IncidenceMatrix.h"

#include <sstream>

namespace polymake { namespace topaz {
  
perl::Object balanced_prism (perl::Object p_in, perl::OptionSet options)
{
   const IncidenceMatrix<> C_in = p_in.give("FACETS");
   const int n_vert = C_in.cols();
   const bool is_balanced = p_in.give("FOLDABLE");
   if (!is_balanced)
      throw std::runtime_error("balanced_prism: Complex not balanced.");

   const Array<int> Coloring = p_in.give("COLORING");
   std::list< Set<int> > C_out;

   int v=0;
   for (Entire< Cols< IncidenceMatrix<> > >::const_iterator star=entire( cols(C_in) );
        !star.at_end(); ++star, ++v)
      for (Entire< IncidenceMatrix<>::col_type >::const_iterator f=entire(*star); !f.at_end(); ++f) {
         Set<int> new_f;
         for (Entire< IncidenceMatrix<>::row_type >::const_iterator w=entire(C_in[f.index()]); !w.at_end(); ++w) {
            if (Coloring[v] <= Coloring[*w])
               new_f += *w + n_vert;
            if (Coloring[v] >= Coloring[*w])
               new_f += *w;
         }
         C_out.push_back(new_f);
      }
   
   perl::Object p_out("SimplicialComplex");
   p_out.set_description()  << "balanced prism of " <<p_in.name() << "."<<endl;
   p_out.take("FACETS") << as_array(C_out);
   
   if (options["geom_real"]) {
      Matrix<Rational> GR=p_in.give("GEOMETRIC_REALIZATION");
      Matrix<Rational> GR2 = Vector<Rational>(n_vert,Rational(1)) | GR;
      GR = Vector<Rational>(n_vert) | GR;

      p_out.take("GEOMETRIC_REALIZATION") << GR/GR2;
   }
   return p_out;
}

UserFunction4perl("# @category Producing a new simplicial complex from others\n"
                  "# Produce a __prism__ over a given [[SimplicialComplex]].\n"
                  "# @param SimplicialComplex complex"
                  "# @option Bool geom_real"
                  "# @return SimplicialComplex",
                  &balanced_prism,"balanced_prism(SimplicialComplex {geom_real => 0})");


} }

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