/* 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: 24-cell.cc,v 1.19 2004/06/22 16:23:11 gawrilow Exp $"

#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <ListMatrix.h>
#include <Vector.h>
#include <Set.h>
#include <Array.h>

namespace polymake {
namespace polytope {

void create_24_cell(Poly& p)
{
   // 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;
	 }
      }
   }

   p.take("AMBIENT_DIM") << rank;
   p.take("DIM") << rank;
   p.take("VERTICES") << Vertices;
   p.take("BOUNDED") << true;
   p.take("CENTERED") << true;
}

} }

using namespace polymake;

/** @file 24-cell
 *
 *  Produce a polymake description of the 24-cell.
 *  
 *  @synopsis 24-cell <file>
 *
 *  @writing  AMBIENT_DIM, DIM, VERTICES, BOUNDED, CENTERED
 *
 *  @index clients Producing from scratch
 */
int main(int argc, const char *argv[]) {
   if (argc!=2) {
      cerr << "usage: " << argv[0] << " <file>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::out | ios::trunc);
      polytope::create_24_cell(p);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
