/* 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: cube.cc 10061 2011-03-16 12:46:53Z herr $
*/

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

namespace polymake { namespace polytope {

perl::Object cube(int d, Rational x_up, Rational x_low)
{
   if (d < 1)
      throw std::runtime_error("cube: dimension d >= 1 required");
   if (!x_up && !x_low) {
      x_up=1; x_low=0;
   } else {
      if (x_up <= x_low)
         throw std::runtime_error("cube: x_up > x_low required");
      x_low.negate();
   }

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

   SparseMatrix<Rational> F(2*d,d+1);
   Rows< SparseMatrix<Rational> >::iterator f=rows(F).begin();
   for (int i=1; i<=d; ++i) {
      (*f)[0]=x_low;
      (*f)[i]=1;
      ++f;
      (*f)[0]=x_up;
      (*f)[i]=-1;
      ++f;
   }

   IncidenceMatrix<> VIF(2*d, 1<<d, CubeFacets<int>(d).begin());

   p.take("CONE_AMBIENT_DIM") << d+1;
   p.take("CONE_DIM") << d+1;
   p.take("FACETS") << F;
   p.take("AFFINE_HULL") << Matrix<Rational>();
   p.take("VERTICES_IN_FACETS") << VIF;
   p.take("BOUNDED") << true;

   return p;
}

UserFunction4perl("# @category Producing from scratch"
                  "# Produce a //d//-dimensional cube."
                  "# Regular polytope corresponding to the Coxeter group of type B<sub>//d//-1</sub> = C<sub>//d//-1</sub>."
                  "# "
                  "# The bounding hyperplanes are x<sub>i</sub> <= //x_up// and x<sub>i</sub> >= //x_low//."
                  "# Default values: //x_up//=1, //x_low//=-//x_up// or 1 if //x_up//==0."
                  "# @param Int d the dimension"
                  "# @param Rational x_up"
                  "# @param Rational x_low"
                  "# @return Polytope",
                  &cube, "cube($; $=1, $=-$_[1] )");
} }

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