/* Copyright (c) 1997-2009
   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: cyclic_caratheodory.cc 9510 2010-02-27 23:52:10Z herr $
*/

#include <cmath>
#include <Matrix.h>
#include <polymake.h>

namespace polymake { namespace polytope {

perl::Object cyclic_caratheodory(int d, int n)
{
   if ((d < 2) || (d >= n)) {
      throw std::runtime_error("cyclic_caratheodory: (d >= 2) && (n > d)\n");
   }
   if (d % 2) {
      throw std::runtime_error("cyclic_caratheodory: even dimension required.\n");
   }
   
   perl::Object p("Polytope<Rational>");
   p.set_description() << "Cyclic " << d << "-polytope on " << n << " vertices on the trigonometric moment curve" << endl;

   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("AMBIENT_DIM") << d;
   p.take("DIM") << d;
   p.take("N_VERTICES") << n;
   p.take("VERTICES") << Vertices;
   p.take("BOUNDED") << true;
   return p;
}

UserFunction4perl("# @category Producing from scratch"
		  "# 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."
		  "# @param Int d the dimension"
		  "# @param Int n the number of points"
		  "# @return Polytope",
		  &cyclic_caratheodory,"cyclic_caratheodory($$)");

} }


