/* 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: simplex.cc,v 1.27 2005/05/09 13:03:45 thilosch Exp $"

#include <cstdlib>
#include <Rational.h>
#include <Poly.h>
#include <Matrix.h>

namespace polymake { namespace polytope {

void simplex(Poly& p, int d, const Rational& s)
{
   if (d < 1)
      throw std::runtime_error("dimension must be at least 2");
   if (!s)
      throw std::runtime_error("scale must be non-zero");

   p.take("AMBIENT_DIM") << d;
   p.take("DIM") << d;

   Matrix<Rational> V(d+1,d+1);
   V.col(0).fill(1);
   V.minor(range(1,d), range(1,d)).diagonal().fill(s);
   p.take("VERTICES") << V;

   p.take("N_VERTICES") << d+1;
   p.take("SIMPLICIALITY") << d;
   p.take("BOUNDED") << true;
   p.take("CENTERED") << false;
}

} }

using namespace polymake;

/** @file simplex
 *
 *  Produce the standard $d$-simplex.
 *  Combinatorially equivalent to regular polytope corresponding to the Coxeter group of type $A_{d-1}$.
 *
 *  @synopsis simplex <file> <d> [<scale>]
 *
 *  @writing  AMBIENT_DIM, DIM, VERTICES, N_VERTICES, BOUNDED, CENTERED, SIMPLICIAL
 *
 *  @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 {
      const int d = atoi(argv[2]); // dimension
      const Rational s= argc==4 ? Rational(argv[3]) : Rational(1);
      Poly p(argv[1], ios::out | ios::trunc);
      polytope::simplex(p,d,s);
   }
   catch (const std::exception&e ) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
