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

#include "polymake/client.h"
#include "polymake/Rational.h"
#include "polymake/Matrix.h"
#include "polymake/ListMatrix.h"
#include "polymake/Vector.h"
#include "polymake/Set.h"
#include "polymake/Array.h"

namespace polymake { namespace polytope {

perl::Object create_24_cell()
{
   // simple roots; Bourbaki, Lie IV-VI, Planche VIII, p.272f
   const int rank=4;
   Matrix<Rational> alpha(rank,rank);
   const Matrix<Rational> e=unit_matrix<Rational>(rank);

   alpha[0]=e[1]-e[2];
   alpha[1]=e[2]-e[3];
   alpha[2]=e[3];
   alpha[3]=(e[0]-e[1]-e[2]-e[3])/2;

   // reflections corresponding to the simple roots
   Array< Matrix<Rational> > s(rank,e);
   Rows< Matrix<Rational> >::iterator alpha_i=rows(alpha).begin();
   for (Entire< Array< Matrix<Rational> > >::iterator s_i=entire(s); !s_i.at_end(); ++s_i, ++alpha_i)
      for (int k=0; k<rank; ++k)
         (*s_i)[k]-=(2*(*alpha_i)[k])/sqr(*alpha_i)*(*alpha_i);

   // choose any vertex of the polytope;
   // this one is contained in the hyperplanes perpendicular to alpha[0], alpha[1], alpha[2]
   // the vertices are the orbit of x under the group generated by the reflections
   Set< Vector<Rational> > orbit;
   ListMatrix< Vector<Rational> > Vertices(0,rank+1);
   orbit += e[0];
   Vertices /= 1 | e[0];

   for (Rows< ListMatrix< Vector<Rational> > >::iterator v=rows(Vertices).begin();
        v != rows(Vertices).end();  ++v) {
      for (int i=0; i<rank; ++i) {
         Vector<Rational> w=v->slice(1) * s[i];
         if (!orbit.contains(w)) {
            orbit += w;
            Vertices /= 1 | w;
         }
      }
   }

   perl::Object p("Polytope<Rational>");
   p.set_description() << "the 24-cell polytope" << endl;

   p.take("CONE_AMBIENT_DIM") << rank+1;
   p.take("CONE_DIM") << rank+1;
   p.take("VERTICES") << Vertices;
   p.take("BOUNDED") << true;
   p.take("CENTERED") << true;
   return p;
}

UserFunction4perl("# @category Producing from scratch"
                  "# Create the 24-cell polytope."
                  "# @return Polytope",
                  &create_24_cell, "create_24_cell()");
} }

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