/* 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: dimension.cc,v 1.31 2004/09/20 16:35:37 gawrilow Exp $"

#include <Rational.h>
#include <argv_options.h>
#include <Poly.h>
#include <linalg.h>

namespace polymake { namespace polytope {

template <typename Coord>
void dimension_primal(Poly& p, const char *out)
{
   const Matrix<Coord> V=p.give("VERTICES | POINTS");
   int dim=rank(V)-1;
   p.take(out) << dim;
}

template <typename Coord>
void dimension_dual (Poly& p, const char *out)
{
   const Matrix<Coord> F=p.give("FACETS | INEQUALITIES"),
      		       E=p.lookup("AFFINE_HULL | EQUATIONS");
   int dim= E.rows()
            ? rank((F / E).minor(All, range(1,F.cols()-1)))
            : rank(F.minor(All, range(1,F.cols()-1)));
   p.take(out) << dim;
}

template <typename Coord>
void dimension_rays (Poly& p, const char *out)
{
   const Matrix<Coord> V=p.give("VERTICES | POINTS");
   int dim=rank(V.minor(far_points(V), range(1,V.cols()-1)));
   p.take(out) << dim;
}

} }

using namespace polymake;

/** @file dimension
 *
 *  Compute the dimension of the vector space built of points or hyperplanes.
 *
 *  $mode$ may be -primal, -dual, or -rays.
 *  If option $-rays$ is specified, only far points (rays) are considered,
 *  that is, the dimension of the far face is computed.
 *
 *  @synopsis dimension <file> <mode> <output_section>
 *
 *  @reading  in primal or ray mode: VERTICES | POINTS
 *            in dual mode:          FACETS | INEQUALITIES, AFFINE_HULL | EQUATIONS
 *  @writing  output_section
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ { "-primal" }, { "-dual" }, { "-rays" } };
   enum { primal, dual, rays };
   if (argc != 4
       || !extract_options(argc,argv,1,options)
       || argc != 3) {
      cerr << "usage: " << argv[0] << " <file> { -primal | -dual | -rays } <output_section>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      if (p.isa("RationalPolytope")) {
	 if (options[primal])
	    polytope::dimension_primal<Rational>(p, argv[2]);
	 else if(options[dual])
	    polytope::dimension_dual<Rational>(p, argv[2]);
	 else
	    polytope::dimension_rays<Rational>(p, argv[2]);

      } else if (p.isa("FloatPolytope")) {
	 p.lookup("EPSILON") >> conv<double,bool>::global_epsilon;
	 if (options[primal])
	    polytope::dimension_primal<double>(p, argv[2]);
	 else if(options[dual])
	    polytope::dimension_dual<double>(p, argv[2]);
	 else
	    polytope::dimension_rays<double>(p, argv[2]);

      } else {
	 throw std::runtime_error("don't know how to proceed with " + p.type());
      }
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
