util.c: move duplicate Polyhedron_Read
[barvinok.git] / barvinok_ehrhart.cc
blob9899d8caafbf9aebc9775cd54f85e2a1b12d206e
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <polylib/polylibgmp.h>
5 #include <barvinok/util.h>
6 #include <barvinok/barvinok.h>
7 #include "config.h"
9 /* The input of this example program is a polytope in PolyLib notation,
10 * i.e., an n by d+2 matrix of the n constraints A x + b >= 0 defining
11 * the polytope * sitting in a d-dimensional space. The first column
12 * is 1 for an inequality and 0 for an equality. b is placed in the
13 * final column.
14 * Alternatively, if the matrix is preceded by the word "vertices"
15 * on a line by itself, it will be interpreted as a list of vertices
16 * in PolyLib notation, i.e., an n by (d+2) matrix, where n is
17 * the number of vertices/rays and d the dimension. The first column is
18 * 0 for lines and 1 for vertices/rays. The final column is the denominator
19 * or 0 for rays. Note that for barvinok_ehrhart, the first column
20 * should always be 1.
23 #ifdef HAVE_GROWING_CHERNIKOVA
24 #define MAXRAYS POL_NO_DUAL
25 #else
26 #define MAXRAYS 600
27 #endif
29 #ifndef HAVE_GETOPT_H
30 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
31 #else
32 #include <getopt.h>
33 struct option options[] = {
34 { "convert", no_argument, 0, 'c' },
35 { "floor", no_argument, 0, 'f' },
36 { "series", no_argument, 0, 's' },
37 { "version", no_argument, 0, 'V' },
38 { 0, 0, 0, 0 }
40 #endif
42 int main(int argc, char **argv)
44 Polyhedron *A, *C, *U;
45 char **param_name;
46 int c, ind = 0;
47 int convert = 0;
48 int floor = 0;
49 int series = 0;
51 while ((c = getopt_long(argc, argv, "sfcV", options, &ind)) != -1) {
52 switch (c) {
53 case 's':
54 series = 1;
55 break;
56 case 'c':
57 convert = 1;
58 break;
59 case 'f':
60 floor = 1;
61 break;
62 case 'V':
63 printf(barvinok_version());
64 exit(0);
65 break;
69 A = Polyhedron_Read(MAXRAYS);
70 param_name = Read_ParamNames(stdin, 1);
71 Polyhedron_Print(stdout, P_VALUE_FMT, A);
72 C = Cone_over_Polyhedron(A);
73 U = Universe_Polyhedron(1);
74 if (series) {
75 gen_fun *gf;
76 gf = barvinok_series(C, U, MAXRAYS);
77 gf->print(std::cout, U->Dimension, param_name);
78 puts("");
79 delete gf;
80 } else {
81 evalue *EP;
82 /* A (conceptually) obvious optimization would be to pass in
83 * the parametric vertices, which are just n times the original
84 * vertices, rather than letting barvinok_enumerate_ev (re)compute
85 * them through Polyhedron2Param_SimplifiedDomain.
87 EP = barvinok_enumerate_ev(C, U, MAXRAYS);
88 print_evalue(stdout, EP, param_name);
89 if (floor) {
90 fprintf(stderr, "WARNING: floor conversion not supported\n");
91 evalue_frac2floor(EP);
92 print_evalue(stdout, EP, param_name);
93 } else if (convert) {
94 evalue_mod2table(EP, C->Dimension);
95 print_evalue(stdout, EP, param_name);
97 free_evalue_refs(EP);
98 free(EP);
100 Free_ParamNames(param_name, 1);
101 Polyhedron_Free(A);
102 Polyhedron_Free(C);
103 Polyhedron_Free(U);
104 return 0;