/* Copyright (c) 1997-2010
   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: rand_sphere.cc 9831 2010-10-30 18:47:05Z gawrilow $
*/

#include <Matrix.h>
#include <Vector.h>
#include <random_generators.h>
#include <polymake.h>

namespace polymake { namespace polytope {

perl::Object rand_sphere(int d, int n, perl::OptionSet options)
{
   if (d<2 || n<=d) {
      throw std::runtime_error("rand_sphere: 2 <= dim < #vertices\n");
   }
   long seed;
   if(!(options["seed"] >> seed)) seed=random_seed();

   perl::Object p("Polytope<Rational>");
   p.set_description() << "Random sphere of dimension " << d << "; seed=" << seed << endl;

   UniformRNG< Vector<double> > random_source(d, seed);

   Matrix<double> Points(n, d+1);
   Points.col(0).fill(1);
   copy(random_source.begin(), entire(rows(Points.minor(All, range(1,d)).top())));
   p.take("POINTS") << Points;
   p.take("AMBIENT_DIM") << d;
   p.take("BOUNDED") << true;
   return p;
}

UserFunction4perl("# @category Producing from scratch"
		  "# Produce a //d//-dimensional polytope with //n// random vertices"
		  "# uniformly distributed on the unit sphere."
		  "# @param Int d the dimension"
		  "# @param Int n the number of random vertices"
		  "# @option Int seed controls the outcome of the random number generator;"
		  "#   fixing a seed number guarantees the same outcome. "
		  "# @return Polytope",
		  &rand_sphere, "rand_sphere($$ { seed => undef })");
} }
