/* 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: is_lattice.cc,v 1.4 2005/12/08 09:35:20 joswig Exp $"

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

namespace polymake { namespace polytope {

void is_lattice(Poly& p, const char* vertices_section, const char* is_lattice_section)
{
   const Matrix<Rational> M=p.give(vertices_section);
   for (Entire< ConcatRows< Matrix<Rational> > >::const_iterator m=entire(concat_rows(M)); !m.at_end(); ++m)
      if (denominator(*m)!=1) {
	 p.take(is_lattice_section) << false;
	 return;
      }
   p.take(is_lattice_section) << true;
}

} } 

using namespace polymake;

/** @file is_lattice
 *  Determine if all points in a coordinate matrix are integral.
 *
 *  @synopsis is_lattice_box <file> <vertices_section> <is_lattice_section>
 *
 *  @reading <vertices_section>
 *  @writing <is_lattice_section> 
 */

int main(int argc, char *argv[])
{
   if (argc!=4) {
      cerr << "usage: " << argv[0] << " <file> <vertices_section> <is_lattice_section>" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      polytope::is_lattice(p,argv[2],argv[3]);
   } catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
