/* 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: cross.cc,v 1.21 2004/06/22 16:23:10 gawrilow Exp $"

#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>
#include <IncidenceMatrix.h>
#include <CubeFacets.h>

namespace polymake {
namespace polytope {

void cross (Poly& p, int d, const Rational& s)
{
   const int n_vertices=2*d;

   Matrix<Rational> V(n_vertices,d+1);
   V.col(0).fill(1);
   V.minor(series(0,d,2),sequence(1,d)).diagonal().fill(s);
   V.minor(series(1,d,2),sequence(1,d)).diagonal().fill(-s);

   IncidenceMatrix<> VIF(1<<d, n_vertices);
   std::copy(CubeFacets<int>(d).begin(), cols(VIF).begin());

   p.take("AMBIENT_DIM") << d;
   p.take("DIM") << d;
   p.take("N_VERTICES")	<< n_vertices;
   p.take("VERTICES") << V;
   p.take("VERTICES_IN_FACETS") << VIF;
   p.take("BOUNDED") << true;
   p.take("CENTERED") << true;
}

} }

using namespace polymake;

/** @file cross
 *
 *  Produce a $d$-dimensional cross polytope.
 *  Regular polytope corresponding to the Coxeter group of type $B_{d-1} = C_{d-1}$.
 *
 *  $scale$ must be positive.  All coordinates are $+/- scale$ or 0.
 *
 *  @synopsis cross <file> <d> [<scale>]
 *
 *  @writing  AMBIENT_DIM, DIM, VERTICES, N_VERTICES, BOUNDED, CENTERED
 *
 *  @index clients Producing from scratch
 */

int main(int argc, char *argv[]) {
   if (argc<3 || argc>4) {
      cerr << "usage: " << argv[0] << " <file> <dimension> [<scale>]" << endl;
      return 1;
   }
   try {
      // dimension
      int d = atoi(argv[2]);

      const Rational s= argc==4 ? Rational(argv[3]) : Rational(1);

      // parameter check
      if (d<=0 || s<=0) {
	 cerr << argv[0] << ": dimension > 0 && scale > 0" << endl;
	 return 1;
      }

      Poly p(argv[1], ios::out | ios::trunc);
      polytope::cross(p, d, s);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}


