/* 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: cyclic_caratheodory.cc,v 1.16 2005/07/28 18:03:50 gawrilow Exp $"

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

namespace polymake {
namespace polytope {

void cyclic_caratheodory(Poly& p, int d, int n)
{
   p.take("AMBIENT_DIM") << d;
   p.take("DIM") << d;
   p.take("N_VERTICES") << n;

   Matrix<double> Vertices(n,d+1);
   double* v=concat_rows(Vertices).begin();

   for (int x = 0; x < n; ++x) {
      *v++ = 1;
      for (int i = 1; i <= d/2; ++i) {
	 *v++ = cos(i*x);
	 *v++ = sin(i*x);
      }
   }
   p.take("VERTICES") << Vertices;

   p.take("BOUNDED") << true;
}

} }

using namespace polymake;

/** @file cyclic_caratheodory
 *
 *  Produce a $d$-dimensional cyclic polytope with $n$ points.
 *  Prototypical example of a neighborly polytope.  Combinatorics completely known
 *  due to Gale's evenness criterion.  Coordinates are chosen on the trigonometric
 *  moment curve.
 *
 *  $d$ must be even; $n$ > $d$
 *
 *  @synopsis cyclic_caratheodory <file> <d> <n>
 *
 *  @writing  AMBIENT_DIM, DIM, N_VERTICES, VERTICES, BOUNDED
 *
 *  @index clients Producing from scratch
 */

int main(int argc, const char *argv[]) {
   if (argc!=4) { 
      cerr << "usage: " << argv[0] << " <file> <dimension> <number of points> " << endl;
      return 1;
   }
   try {
      const int d = atoi(argv[2]), // projective dimension
                n = atoi(argv[3]); // number of points/inequalities

      // there is a certain restriction on the parameters
      if ((d < 2) || (d >= n)) {
	 cerr << argv[0] << ": (d >= 2) && (n > d)\n";
	 return 1;
      }
      if (d % 2) {
	 cerr << argv[0] << ": dimension should be even" << endl;
	 return 1;
      }

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