/* 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: incidence.cc,v 1.21 2004/09/20 16:12:31 gawrilow Exp $"

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

namespace polymake { namespace polytope {

template <typename E, typename Matrix1, typename Matrix2> inline
IncidenceMatrix<>
incidence_matrix(const GenericMatrix<Matrix1,E>& R, const GenericMatrix<Matrix2,E>& C)
{
   return IncidenceMatrix<> (R.rows(), C.rows(),
			     attach_operation(product(rows(R), rows(C), operations::mul()),
					      operations::composed11<conv<E,bool>, operations::logical_not>()).begin());
}

template <typename Matrix1, typename Matrix2> inline
IncidenceMatrix<>
incidence_matrix(const GenericMatrix<Matrix1,double>& R, const GenericMatrix<Matrix2,double>& C)
{
   return incidence_matrix<double, Matrix1, Matrix2>(normalized(R), normalized(C));
}

template <typename Coord>
void incidence(Poly& p, const char* row_section, const char* col_section, const char* incidence_section)
{
   const Matrix<Coord> R=p.give(row_section), C=p.give(col_section);
   const IncidenceMatrix<> VIF=incidence_matrix(R,C);
   p.take(incidence_section) << VIF;
}

} }

using namespace polymake;

/** @file incidence
 *
 *  Compute the incidence matrix of two vector sets.
 *
 *  Rows of the incidence matrix correspond to the vectors of the first set, columns correspond to the second set.
 *  Vector sets are encoded as rows of a matrix.
 *
 *  Two vectors are considered incident if their scalar product is exactly zero.
 *
 *  @synopsis incidence <file> <row_section> <col_section> <incidence_section>
 *
 *  @reading  <row_section> <row_section>
 *  @writing  <incidence_section>
 */

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