/* 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: permutahedron.cc,v 1.23 2006/03/09 16:21:35 gawrilow Exp $"

#include <cstdlib>
#include <algorithm>
#include <Poly.h>
#include <Rational.h>
#include <Vector.h>
#include <Matrix.h>

namespace polymake { namespace polytope {

void permutahedron(Poly& p, int d)
{
   p.take("AMBIENT_DIM") << d+1;
   p.take("DIM") << d;

   // enumerate the d! vertices of the d-permutahedron
   Matrix<Rational> V(Integer::fac(d+1), d+2);
   Vector<int> perm(d+1, sequence(1).begin());

   Rows< Matrix<Rational> >::iterator r=rows(V).begin();
   do {
      *r = 1 | perm;
      ++r;
   } while (std::next_permutation(perm.begin(),perm.end()));

   p.take("VERTICES") << V;
   p.take("N_VERTICES") << V.rows();
   p.take("BOUNDED") << true;
}

} }

using namespace polymake;

/** @file permutahedron
 *
 *  Produce the $d$-permutahedron.
 *  The vertices correspond to the elements of the symmetric group of degree $d+1$.
 *
 *  @synopsis permutahedron <file> <d>
 *
 *  @writing  AMBIENT_DIM, DIM, VERTICES, N_VERTICES, BOUNDED
 *
 *  @index clients Producing from scratch
 */

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