/* 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: cubical_h_vector.cc,v 1.4 2006/03/09 16:21:35 gawrilow Exp $"

#include <Integer.h>
#include <Poly.h>
#include <Vector.h>

namespace polymake { namespace polytope {
  
   void cubical_h_vector(Poly& p, bool cubical)
   {
      Vector<Integer> f = p.give("F_VECTOR");
      const int d=f.size();
      Vector<Integer> h(d+1);
      Vector<Integer>::iterator h_k=h.begin();
    
      if (!cubical) std::reverse(f.begin(),f.end());
      *h_k = Integer::pow(2,d-1);
      ++h_k;
      for (int k=0, startsign=1;  k<=d-1;  ++k, ++h_k, startsign=-startsign) {
	 for (int i=0, sign=startsign;  i<=k;  ++i, sign=-sign)
	    *h_k += sign * Integer::binom(d-1-i,d-1-k) * (Integer::pow(2,i)) * f[i];
	 *h_k -= h_k[-1];
      }
    
      p.take("CUBICAL_H_VECTOR") << h;
   }
} }

using namespace polymake;

/** @file cubical_h_vector 
 *
 * Compute the cubical h-vector of a cubical or cocubical polytope
 * from the f-vector as defined in "A New Cubical $h$-Vector" by Ron
 * M. Adin in 1995.
 *
 *  @synopsis cubical_h_vector <file> { CUBICAL, COCUBICAL }
 *
 *  @reading  CUBICAL or COCUBICAL, F_VECTOR
 *  @writing  CUBICAL_H_VECTOR
 */

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