/* 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: nullspace.cc,v 1.16 2004/09/20 16:13:03 gawrilow Exp $"

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

namespace polymake { namespace polytope {

template <typename Coord>
void nullspace(Poly& p, const char* matrix_section, const char* nullspace_section)
{
   const Matrix<Coord> A=p.give(matrix_section), N=null_space(A);
   p.take(nullspace_section) << N;
}

} }

using namespace polymake;

/** @file nullspace
 *
 *  Compute the basis of the orthogonal complement to the row space of a given matrix.
 *
 *  @synopsis nullspace <file> <matrix_section> <nullspace_section>
 *
 *  @reading  <matrix_section>
 *  @writing  <nullspace_section>
 */

int main(int argc, const char *argv[])
{
   if (argc != 4) {
      cerr << "usage: " << argv[0] << " <file> <matrix_section> <nullspace_section>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      if (p.isa("RationalPolytope")) {
	 polytope::nullspace<Rational>(p, argv[2], argv[3]);
      } else if (p.isa("FloatPolytope")) {
	 p.lookup("EPSILON") >> conv<double,bool>::global_epsilon;
	 polytope::nullspace<double>(p, argv[2], argv[3]);
      } 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;
}
