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

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

namespace polymake { namespace polytope {

template <typename Coord>
void basis_compare (Poly& p, const char *out, const char *b1, const char *b2)
{
   const Matrix<Coord> B1=p.give(b1), B2=p.give(b2);
   bool OK=B1.rows()==B2.rows() &&
           std::find_if(entire(concat_rows(null_space(B1) * T(B2))), conv<Coord,bool>()).at_end();
   p.take(out) << OK;
}

} }

/** @file basis_compare
 *
 *  Given two full rank matrices, checks whether their rows are two basis of the same linear subspace
 *
 *  @synopsis basis_compare <file> <output> <matrix1> <matrix2>
 *
 *  @reading  <matrix1>, <matrix2>
 *  @writing  <output>
 */

using namespace polymake;

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