ex_convert.cc: extract ex2evalue from range.cc
[barvinok/uuh.git] / polytope_scan.c
blob1991fc8fe979bfe99578d5416c520baf054076db
1 #include <assert.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <strings.h>
5 #include <barvinok/util.h>
6 #include <barvinok/options.h>
7 #include <barvinok/basis_reduction.h>
8 #include "argp.h"
10 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
12 struct argp_option argp_options[] = {
13 { "direct", 'd', 0, 0,
14 "don't apply generalized basis reduction first" },
15 { 0 }
18 struct options {
19 struct barvinok_options *barvinok;
20 int direct;
23 error_t parse_opt(int key, char *arg, struct argp_state *state)
25 struct options *options = state->input;
27 switch (key) {
28 case ARGP_KEY_INIT:
29 state->child_inputs[0] = options->barvinok;
30 options->direct = 0;
31 break;
32 case 'd':
33 options->direct = 1;
34 break;
35 case 'V':
36 printf(barvinok_version());
37 exit(0);
38 default:
39 return ARGP_ERR_UNKNOWN;
41 return 0;
44 static void scan_poly(Polyhedron *S, int pos, Value *z, Matrix *T)
46 if (!S) {
47 int k;
48 Vector *v;
50 v = Vector_Alloc(T->NbRows);
51 Matrix_Vector_Product(T, z+1, v->p);
52 value_print(stdout, VALUE_FMT, v->p[0]);
53 for (k=1; k < pos; ++k) {
54 printf(", ");
55 value_print(stdout,VALUE_FMT, v->p[k]);
57 Vector_Free(v);
58 printf("\n");
59 } else {
60 int ok;
61 Value LB, UB, tmp;
62 value_init(LB);
63 value_init(UB);
64 value_init(tmp);
65 ok = !(lower_upper_bounds(1+pos, S, z, &LB, &UB));
66 assert(ok);
67 for (value_assign(tmp,LB); value_le(tmp,UB); value_increment(tmp,tmp)) {
68 value_assign(z[pos+1], tmp);
69 scan_poly(S->next, pos+1, z, T);
71 value_set_si(z[pos+1], 0);
72 value_clear(LB);
73 value_clear(UB);
74 value_clear(tmp);
78 int main(int argc, char **argv)
80 Polyhedron *A, *P, *U, *S;
81 Value *p;
82 int i, j, ok;
83 Matrix *basis, *T, *inv;
84 int c, ind = 0;
85 struct barvinok_options *bv_options = barvinok_options_new_with_defaults();
86 static struct argp_child argp_children[] = {
87 { &barvinok_argp, 0, 0, 0 },
88 { 0 }
90 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
91 struct options options;
93 options.barvinok = bv_options;
95 set_program_name(argv[0]);
96 argp_parse(&argp, argc, argv, 0, 0, &options);
98 A = Polyhedron_Read(bv_options->MaxRays);
100 if (options.direct) {
101 inv = Identity(A->Dimension+1);
102 P = A;
103 } else {
104 basis = Polyhedron_Reduced_Basis(A, bv_options);
106 T = Matrix_Alloc(A->Dimension+1, A->Dimension+1);
107 inv = Matrix_Alloc(A->Dimension+1, A->Dimension+1);
108 for (i = 0; i < A->Dimension; ++i)
109 for (j = 0; j < A->Dimension; ++j)
110 value_assign(T->p[i][j], basis->p[i][j]);
111 value_set_si(T->p[A->Dimension][A->Dimension], 1);
112 Matrix_Free(basis);
114 ok = Matrix_Inverse(T, inv);
115 assert(ok);
116 Matrix_Free(T);
118 P = Polyhedron_Preimage(A, inv, bv_options->MaxRays);
119 Polyhedron_Free(A);
122 U = Universe_Polyhedron(0);
123 S = Polyhedron_Scan(P, U, bv_options->MaxRays);
125 p = ALLOCN(Value, P->Dimension+2);
126 for(i=0;i<=P->Dimension;i++) {
127 value_init(p[i]);
128 value_set_si(p[i],0);
130 value_init(p[i]);
131 value_set_si(p[i],1);
133 scan_poly(S, 0, p, inv);
135 Matrix_Free(inv);
136 for(i=0;i<=(P->Dimension+1);i++)
137 value_clear(p[i]);
138 free(p);
139 Domain_Free(S);
140 Polyhedron_Free(P);
141 Polyhedron_Free(U);
142 barvinok_options_free(bv_options);
143 return 0;