/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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.
*/

#ident "$Project: polymake $$Id: t_balanced_prism.cc,v 1.3 2005/04/19 12:07:38 witte Exp $"

#include <Poly.h>
#include <Matrix.h>
#include <Vector.h>
#include <Rational.h>
#include <complex_tools.h>
#include <IncidenceMatrix.h>

#include <sstream>


namespace polymake {
namespace topaz {
  
void t_balanced_prism (Poly& p_out, Poly& p_in, const bool geom_real, const std::string& comment)
{
   const IncidenceMatrix<> C_in = p_in.give("FACETS");
   const int n_vert = C_in.cols();
   const bool is_balanced = p_in.give("BALANCED");
   if (!is_balanced)
      throw std::runtime_error("t_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);
      }
   
   p_out.take("DESCRIPTION") << comment;
   p_out.take("FACETS") << as_array(C_out);

   if (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;
   }
}
   
} }

using namespace polymake;

/** @file t_balanced_prism
 *
 *  Produce a prism over a given simplicial complex.
 *
 *  @synopsis t_balanced_prism <out_file> <in_file> [ -geom_real ]
 *
 *  @reading  FACETS, COLORING, BALANCED, may read GEOMETRIC_REALIZATION
 *  @writing  DESCRIPTION, FACETS, may write GEOMETRIC_REALIZATION
 *
 *  @index clients Producing a new simplicial complex from others
 */


int main(int argc, const char* argv[])
{
   if ( argc<3 || argc>4 ) {
      cerr << "usage: " << argv[0] << " <out_file> <in_file> [ -geom_real ]" << endl;
      return 1;
   }
   try {
      Poly p_out(argv[1], ios::out | ios::trunc, "topaz::default");
      Poly p_in(argv[2], ios::in);

      std::ostringstream comment;
      comment << "balanced prism of " << argv[2] << ".";

      topaz::t_balanced_prism(p_out, p_in, argc>3, comment.str());      
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


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