/* Copyright (c) 1997-2006
   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: altshuler_det.cc,v 1.16 2006/01/17 19:34:37 gawrilow Exp $"

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

namespace polymake { namespace polytope {

void altshuler_det(Poly& p)
{
   const IncidenceMatrix<> Inc=p.give("VERTICES_IN_FACETS");
   const Rational ad= Inc.rows() <= Inc.cols()
	              ? det( same_element_sparse_matrix<Rational>(Inc) * T(same_element_sparse_matrix<Rational>(Inc)) )
		      : det( T(same_element_sparse_matrix<Rational>(Inc)) * same_element_sparse_matrix<Rational>(Inc) );
   p.take("ALTSHULER_DET") << ad;
}

} }

using namespace polymake;

/** @file altshuler_det
 *
 *  Calculate the @see ALTSHULER_DET{Altshuler Determinant} of a polytope.
 *
 *  @synopsis altshuler_det <file>
 *
 *  @reading  VERTICES_IN_FACETS
 *  @writing  ALTSHULER_DET
 */

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