/* Copyright (c) 1997-2009
   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: permutahedron.cc 9513 2010-03-01 21:10:37Z herr $
*/

#include <algorithm>
#include <Rational.h>
#include <Vector.h>
#include <Matrix.h>
#include <polymake.h>

namespace polymake { namespace polytope {

perl::Object permutahedron(int d)
{
   if (d < 2) {
      throw std::runtime_error("permutahedron: dimension >= 2 required\n");
   }
   perl::Object p("Polytope<Rational>");
   p.set_description() << "permutahedron of dimension " << d << endl;
   // 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("AMBIENT_DIM") << d+1;
   p.take("DIM") << d;
   p.take("VERTICES") << V;
   p.take("N_VERTICES") << V.rows();
   p.take("BOUNDED") << true;
   return p;
}

UserFunction4perl("# @category Producing from scratch"
		  "# Produce a //d//-dimensional permutahedron."
		  "# The vertices correspond to the elements of the symmetric group of degree //d//+1."
		  "# @param Int d the dimension"
		  "# @return Polytope",
		  &permutahedron, "permutahedron($)");
} }
