/* Copyright (c) 1997-2010
   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: cross.cc 10061 2011-03-16 12:46:53Z herr $
*/

#include "polymake/client.h"
#include "polymake/Rational.h"
#include "polymake/SparseMatrix.h"
#include "polymake/IncidenceMatrix.h"
#include "polymake/polytope/CubeFacets.h"

namespace polymake { namespace polytope {

perl::Object cross(int d, const Rational& s)
{
   if (d < 1)
      throw std::runtime_error("cross : dimension d >= 1 required");
   if (s <= 0)
      throw std::runtime_error("cross : x >= 0 required");

   const int n_vertices=2*d;

   perl::Object p("Polytope<Rational>");
   p.set_description() << "cross-polytope of dimension " << d << endl;

   SparseMatrix<Rational> V(n_vertices,d+1);
   V.col(0).fill(1);
   int c=1;
   for (Entire< Rows< SparseMatrix<Rational> > >::iterator v=entire(rows(V)); !v.at_end(); ++v, ++c) {
      (*v)[c]=s;
      ++v;
      (*v)[c]=-s;
   }
   IncidenceMatrix<> VIF(1<<d, n_vertices);
   copy(CubeFacets<int>(d).begin(), cols(VIF).begin());

   p.take("CONE_AMBIENT_DIM") << d+1;
   p.take("CONE_DIM") << d+1;
   p.take("N_VERTICES") << n_vertices;
   p.take("VERTICES") << V;
   p.take("VERTICES_IN_FACETS") << VIF;
   p.take("BOUNDED") << true;
   p.take("CENTERED") << true;

   return p;
}

UserFunction4perl("# @category Producing from scratch"
                  "# Produce a //d//-dimensional cross polytope."
                  "# Regular polytope corresponding to the Coxeter group of type B<sub>//d//-1</sub> = C<sub>//d//-1</sub>."
                  "# "
                  "# All coordinates are +/- //scale// or 0."
                  "# @param Int d the dimension"
                  "# @param Rational scale Needs to be positive. The default value is 1."
                  "# @return Polytope",
                  &cross, "cross($;$=1)");
} }

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