short_rat::print: correctly print out terms with a zero coefficient
[barvinok.git] / polytope_scan.c
blob8f585e7b50ee28a48956bf8be67429bf5af7f6a0
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <barvinok/util.h>
5 #include <barvinok/options.h>
6 #include <barvinok/basis_reduction.h>
7 #include "argp.h"
9 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
11 struct argp_option argp_options[] = {
12 { "direct", 'd', 0, 0,
13 "don't apply generalized basis reduction first" },
14 { 0 }
17 struct options {
18 struct barvinok_options *barvinok;
19 int direct;
22 error_t parse_opt(int key, char *arg, struct argp_state *state)
24 struct options *options = state->input;
26 switch (key) {
27 case ARGP_KEY_INIT:
28 state->child_inputs[0] = options->barvinok;
29 options->direct = 0;
30 break;
31 case 'd':
32 options->direct = 1;
33 break;
34 case 'V':
35 printf(barvinok_version());
36 exit(0);
37 default:
38 return ARGP_ERR_UNKNOWN;
40 return 0;
43 static void scan_poly(Polyhedron *S, int pos, Value *z, Matrix *T)
45 if (!S) {
46 int k;
47 Vector *v;
49 v = Vector_Alloc(T->NbRows);
50 Matrix_Vector_Product(T, z+1, v->p);
51 value_print(stdout, VALUE_FMT, v->p[0]);
52 for (k=1; k < pos; ++k) {
53 printf(", ");
54 value_print(stdout,VALUE_FMT, v->p[k]);
56 Vector_Free(v);
57 printf("\n");
58 } else {
59 int ok;
60 Value LB, UB, tmp;
61 value_init(LB);
62 value_init(UB);
63 value_init(tmp);
64 ok = !(lower_upper_bounds(1+pos, S, z, &LB, &UB));
65 assert(ok);
66 for (value_assign(tmp,LB); value_le(tmp,UB); value_increment(tmp,tmp)) {
67 value_assign(z[pos+1], tmp);
68 scan_poly(S->next, pos+1, z, T);
70 value_set_si(z[pos+1], 0);
71 value_clear(LB);
72 value_clear(UB);
73 value_clear(tmp);
77 int main(int argc, char **argv)
79 Polyhedron *A, *P, *U, *S;
80 Value *p;
81 int i, j, ok;
82 Matrix *basis, *T, *inv;
83 int c, ind = 0;
84 struct barvinok_options *bv_options = barvinok_options_new_with_defaults();
85 static struct argp_child argp_children[] = {
86 { &barvinok_argp, 0, 0, 0 },
87 { 0 }
89 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
90 struct options options;
92 options.barvinok = bv_options;
94 set_program_name(argv[0]);
95 argp_parse(&argp, argc, argv, 0, 0, &options);
97 A = Polyhedron_Read(bv_options->MaxRays);
99 if (options.direct) {
100 inv = Identity(A->Dimension+1);
101 P = A;
102 } else {
103 basis = Polyhedron_Reduced_Basis(A, bv_options);
105 T = Matrix_Alloc(A->Dimension+1, A->Dimension+1);
106 inv = Matrix_Alloc(A->Dimension+1, A->Dimension+1);
107 for (i = 0; i < A->Dimension; ++i)
108 for (j = 0; j < A->Dimension; ++j)
109 value_assign(T->p[i][j], basis->p[i][j]);
110 value_set_si(T->p[A->Dimension][A->Dimension], 1);
111 Matrix_Free(basis);
113 ok = Matrix_Inverse(T, inv);
114 assert(ok);
115 Matrix_Free(T);
117 P = Polyhedron_Preimage(A, inv, bv_options->MaxRays);
118 Polyhedron_Free(A);
121 U = Universe_Polyhedron(0);
122 S = Polyhedron_Scan(P, U, bv_options->MaxRays);
124 p = ALLOCN(Value, P->Dimension+2);
125 for(i=0;i<=P->Dimension;i++) {
126 value_init(p[i]);
127 value_set_si(p[i],0);
129 value_init(p[i]);
130 value_set_si(p[i],1);
132 scan_poly(S, 0, p, inv);
134 Matrix_Free(inv);
135 for(i=0;i<=(P->Dimension+1);i++)
136 value_clear(p[i]);
137 free(p);
138 Domain_Free(S);
139 Polyhedron_Free(P);
140 Polyhedron_Free(U);
141 barvinok_options_free(bv_options);
142 return 0;