
/* 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: infinitesimal_motions.cc,v 1.2 2006/02/27 00:11:25 gawrilow Exp $"

#include <Poly.h>
#include <Vector.h>
#include <Matrix.h>
#include <SparseMatrix.h>
#include <SparseVector.h>
#include <Rational.h>
#include <Graph.h>
#include <linalg.h>
#include <PowerSet.h>

namespace polymake { namespace polytope {
   namespace {
      typedef Graph< Vector< Rational > > Framework;
   }

   void infinitesimal_motions(Poly& p) {

     const Framework F = p.give("FRAMEWORK");
     const SparseMatrix<Rational> R = p.give("RIGIDITY_MATRIX");
     const int n_points = F.nodes();
     const int dim = R.cols()/n_points;

     //generate rigidity matrix of complete graph
     SparseMatrix<Rational> K(((n_points-1)*n_points)/2,dim*n_points);
     Rows< SparseMatrix<Rational> >::iterator ri = rows(K).begin();
     const sequence points = sequence(0,n_points);
     const Vector<Rational> zero_vector_dim(dim,Rational(0));
     for(Entire< Subsets_of_k< sequence > >::const_iterator ei = entire(all_subsets_of_k(points,2)); 
	 !ei.at_end(); ++ei, ++ri) {
	int v = (*ei).front();
	int w = (*ei).back();
	const Vector<Rational> diff = F.node(v)-F.node(w);
	if(diff != zero_vector_dim) {
	   (*ri).slice(v*dim,dim) = diff;
	   (*ri).slice(w*dim,dim) = -diff;
	}
     }

     //calculate INFINITESIMAL_MOTIONS
     const SparseMatrix<Rational> M(null_space(R));
     p.take("INFINITESIMAL_MOTIONS") << M;

     //calculate coordinates of translated points of INFINITESIMAL_MOTIONS
     {
	int i = 0;
	SparseMatrix<Rational> MplusP(M);     
	for(Entire< Nodes< Framework > >::const_iterator ni = entire(nodes(F)); !ni.at_end(); ++ni, ++i) {
	   for(Entire< Rows< SparseMatrix<Rational> > >::iterator mi = entire(rows(MplusP)); !mi.at_end(); ++mi) {
	      (*mi).slice(i*dim,dim) += *ni;
	   }
	}
	p.take("INFINITESIMAL_MOTION_COORDINATES") << MplusP;
     }
     const Matrix< Rational > MKt(M*T(K));
     p.take("INFINITESIMAL_PATTERNS") << MKt;

     p.take("N_DEGREES_OF_FREEDOM") << rank(MKt);
  }
} }

using namespace polymake;

/** @file infinitesimal_motions
 *
 *  Compute the @see Framework.INFINITESIMAL_MOTIONS of a bar and joint framework from its @see RIGIDITY_MATRIX. 
 *  Further this client computes the expansive patterns of the motions and the number of degrees of 
 *  freedom.
 *
 *  @synopsis infinitesimal_motions <file>
 *
 *  @reading  RIGIDITY_MATRIX, FRAMEWORK
 *  @writing  INFINITESIMAL_MOTIONS, INFINITESIMAL_MOTION_COORDINATES,
 *            INFINITESIMAL_PATTERNS, N_DEGREES_OF_FREEDOM
 */

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