/* 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: zonotope.cc,v 1.10 2004/06/22 16:23:08 gawrilow Exp $"

#include <Poly.h>
#include <Rational.h>
#include <Matrix.h>

namespace polymake { namespace polytope {
  
void zonotope(Poly& p)
{
   const Matrix<Rational> Zones=p.give("ZONOTOPE_INPUT_VECTORS");

   const int dim=Zones.cols();
   Matrix<Rational> Zonotope(1,dim);	// one zero-filled row
    
   for (Entire< Rows< Matrix<Rational> > >::const_iterator z=entire(rows(Zones)); !z.at_end(); ++z)
      Zonotope = ( Zonotope + repeat_row(*z, Zonotope.rows()) ) /
	 ( Zonotope - repeat_row(*z, Zonotope.rows()) );
   Zonotope.col(0).fill(1);
   p.take("POINTS") << Zonotope;
}

} }

using namespace polymake;

/** @file zonotope
 *
 *  Produce a zonotope from given vectors.
 *  The zonotope is obtained as the iterated Minkowski sum of all intervals $[-x,x]$,
 *  where $x$ ranges over the rows of a given matrix.
 *
 *  @synopsis zonotope <file>
 *
 *  @reading  ZONOTOPE_INPUT_VECTORS
 *  @writing  POINTS
 *
 *  @index clients Producing from given data
 */

int main(int argc, char *argv[])
{
   if (argc!=2) {
      cerr << "usage: " << argv[0] << " <file>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      polytope::zonotope(p);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
