/* 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: multiplex.cc,v 1.4 2004/06/22 16:23:09 gawrilow Exp $"

#include <cstdlib>
#include <Poly.h>
#include <IncidenceMatrix.h>

namespace polymake { namespace polytope {

namespace {

inline int index (int i, int n)
{
   return i < 0 ? 0 : i > n ? n : i;
}

}

void multiplex(Poly& p, int d, int n)
{
   IncidenceMatrix<> VIF(n+1,n+1);
   for (int j=0; j < n+1; ++j) 
      for (int k=1; k < d; ++k) 
	 VIF(j,index(j+k,n)) = VIF(j,index(j-k,n)) = true;

   p.take("VERTICES_IN_FACETS") << VIF;
   p.take("DIM") << d;
   p.take("N_VERTICES") << n+1;
   p.take("N_FACETS") << n+1;
}

} }

using namespace polymake;

/** @file multiplex
 *
 *  Produce a combinatorial description of a multiplex with  parameters $(d,n)$.
 *  This yields a self-dual $d$-dimensional polytope with $n+1$ vertices.
 *  They are introduced by T. Bisztriczky 
 *     [On a class of generalized simplices, Mathematika 43:27-285, 1996],
 *  see also Bayer et al.
 *     [M.M. Bayer, A.M. Bruening, and J.D. Stewart:
 *      A combinatorial study of multiplexes and ordinary polytopes,
 *       Discrete Comput. Geom. 27(1):49--63, 2002].
 *
 *  @synopsis multiplex <file>  <d> <n>
 *
 *  @writing  AMBIENT_DIM, DIM, BOUNDED, N_VERTICES, N_FACETS, VERTICES_IN_FACETS
 *
 *  @index clients Producing from scratch
 *
 *  @author Alexander Schwartz
 */

int main(int argc, char *argv[]) {
   if (argc != 4) {
      cerr << "usage: " << argv[0] << " <file> <d> <n>" << endl;
      return 1;
   }
   try {
      int d = atoi(argv[2]),			// projective dimension
	  n = atoi(argv[3]);			// number of points/inequalities
 
      // there is a certain restriction on the parameters
      if ((d < 2) || (d > n)) {
	 cerr << argv[0] << ": (d >= 2) && (n >= d)\n";
	 return 1;
      }

      Poly p(argv[1], ios::out | ios::trunc);
      polytope::multiplex(p, d, n);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
