3 #include <barvinok/evalue.h>
4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
9 #include "verif_ehrhart.h"
10 #include "remove_equalities.h"
12 #undef CS /* for Solaris 10 */
14 /* The input of this example program is the same as that of testehrhart
15 * in the PolyLib distribution, i.e., a polytope in combined
16 * data and parameter space, a context polytope in parameter space
17 * and (optionally) the names of the parameters.
18 * Both polytopes are in PolyLib notation.
21 struct argp_option argp_options
[] = {
22 { "convert", 'c', 0, 0, "convert fractionals to periodics" },
23 { "floor", 'f', 0, 0, "convert fractionals to floorings" },
25 { "series", 's', 0, 0, "compute rational generating function" },
26 { "explicit", 'e', 0, 0, "convert rgf to psp" },
32 struct barvinok_options
*barvinok
;
39 struct verify_options verify
;
42 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
44 struct arguments
*options
= (struct arguments
*) state
->input
;
48 state
->child_inputs
[0] = options
->barvinok
;
49 state
->child_inputs
[1] = &options
->verify
;
54 options
->function
= 0;
67 options
->function
= 1;
76 return ARGP_ERR_UNKNOWN
;
81 struct skewed_gen_fun
{
83 /* maps original space to space in which gf is defined */
85 /* equalities in the original space that need to be satisfied for
89 /* divisibilities in the original space that need to be satisfied for
94 skewed_gen_fun(gen_fun
*gf
, Matrix
*T
, Matrix
*eq
, Matrix
*div
) :
95 gf(gf
), T(T
), eq(eq
), div(div
) {}
106 void print(FILE *out
, unsigned int nparam
, char **param_name
) const;
107 operator evalue
*() const {
108 assert(T
== NULL
&& eq
== NULL
); /* other cases not supported for now */
111 void coefficient(Value
* params
, Value
* c
, barvinok_options
*options
) const;
114 void skewed_gen_fun::print(FILE *out
, unsigned int nparam
,
115 char **param_name
) const
117 fdostream
os(dup(fileno(out
)));
119 fprintf(out
, "T:\n");
120 Matrix_Print(out
, P_VALUE_FMT
, T
);
123 fprintf(out
, "eq:\n");
124 Matrix_Print(out
, P_VALUE_FMT
, eq
);
127 fprintf(out
, "div:\n");
128 Matrix_Print(out
, P_VALUE_FMT
, div
);
130 gf
->print(os
, nparam
, param_name
);
133 void skewed_gen_fun::coefficient(Value
* params
, Value
* c
,
134 barvinok_options
*options
) const
137 for (int i
= 0; i
< eq
->NbRows
; ++i
) {
138 Inner_Product(eq
->p
[i
]+1, params
, eq
->NbColumns
-2, eq
->p
[i
]);
139 if (value_notzero_p(eq
->p
[i
][0])) {
148 for (int i
= 0; i
< div
->NbRows
; ++i
) {
149 Inner_Product(div
->p
[i
], params
, div
->NbColumns
-1, &tmp
);
150 if (!mpz_divisible_p(tmp
, div
->p
[i
][div
->NbColumns
-1])) {
160 coeff
= gf
->coefficient(params
, options
);
162 Vector
*p2
= Vector_Alloc(T
->NbRows
);
163 Matrix_Vector_Product(T
, params
, p2
->p
);
164 if (value_notone_p(p2
->p
[T
->NbRows
-1]))
165 Vector_AntiScale(p2
->p
, p2
->p
, p2
->p
[T
->NbRows
-1], T
->NbRows
);
166 coeff
= gf
->coefficient(p2
->p
, options
);
173 static int check_series(Polyhedron
*S
, Polyhedron
*CS
, skewed_gen_fun
*gf
,
174 int nparam
, int pos
, Value
*z
, int print_all
,
175 barvinok_options
*options
)
187 /* Computes the coefficient */
188 gf
->coefficient(&z
[S
->Dimension
-nparam
+1], &c
, options
);
190 /* if c=0 we may be out of context. */
191 /* scanning is useless in this case*/
195 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
196 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
198 value_print(stdout
,VALUE_FMT
,z
[k
]);
201 value_print(stdout
,VALUE_FMT
,c
);
205 /* Manually count the number of points */
206 count_points(1,S
,z
,&tmp
);
208 printf(", count = ");
209 value_print(stdout
, P_VALUE_FMT
, tmp
);
213 if (value_ne(tmp
,c
)) {
216 fprintf(stderr
,"Error !\n");
217 fprintf(stderr
,"EP( ");
218 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
219 for (k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
220 fprintf(stderr
,", ");
221 value_print(stderr
,VALUE_FMT
,z
[k
]);
223 fprintf(stderr
," ) should be ");
224 value_print(stderr
,VALUE_FMT
,tmp
);
225 fprintf(stderr
,", while EP eval gives ");
226 value_print(stderr
,VALUE_FMT
,c
);
227 fprintf(stderr
,".\n");
228 #ifndef DONT_BREAK_ON_ERROR
229 value_clear(c
); value_clear(tmp
);
232 } else if (print_all
)
236 !(lower_upper_bounds(1+pos
, CS
, &z
[S
->Dimension
-nparam
], &LB
, &UB
));
238 for (value_assign(tmp
,LB
); value_le(tmp
,UB
); value_increment(tmp
,tmp
)) {
240 k
= VALUE_TO_INT(tmp
);
241 if(!pos
&& !(k
%st
)) {
246 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
247 if (!check_series(S
, CS
->next
, gf
, nparam
, pos
+1, z
, print_all
,
249 value_clear(c
); value_clear(tmp
);
255 value_set_si(z
[pos
+S
->Dimension
-nparam
+1],0);
265 static int verify(Polyhedron
*P
, Polyhedron
**C
, Enumeration
*en
, skewed_gen_fun
*gf
,
268 Polyhedron
*CC
, *PP
, *CS
, *S
, *U
;
273 /******* Compute true context *******/
274 CC
= align_context(*C
, P
->Dimension
, options
->barvinok
->MaxRays
);
275 PP
= DomainIntersection(P
, CC
, options
->barvinok
->MaxRays
);
277 C1
= Matrix_Alloc((*C
)->Dimension
+1, P
->Dimension
+1);
279 for (int i
= 0; i
< C1
->NbRows
; i
++)
280 for (int j
= 0; j
< C1
->NbColumns
; j
++)
281 if (i
== j
-P
->Dimension
+(*C
)->Dimension
)
282 value_set_si(C1
->p
[i
][j
], 1);
284 value_set_si(C1
->p
[i
][j
], 0);
285 CC
= Polyhedron_Image(PP
, C1
, options
->barvinok
->MaxRays
);
291 /* Intersect context with range */
292 if ((*C
)->Dimension
> 0) {
293 MM
= Matrix_Alloc(2*(*C
)->Dimension
, (*C
)->Dimension
+2);
294 for (int i
= 0; i
< (*C
)->Dimension
; ++i
) {
295 value_set_si(MM
->p
[2*i
][0], 1);
296 value_set_si(MM
->p
[2*i
][1+i
], 1);
297 value_set_si(MM
->p
[2*i
][1+(*C
)->Dimension
], -options
->verify
.m
);
298 value_set_si(MM
->p
[2*i
+1][0], 1);
299 value_set_si(MM
->p
[2*i
+1][1+i
], -1);
300 value_set_si(MM
->p
[2*i
+1][1+(*C
)->Dimension
], options
->verify
.M
);
302 CC
= AddConstraints(MM
->p
[0], 2*(*C
)->Dimension
, *C
,
303 options
->barvinok
->MaxRays
);
304 U
= Universe_Polyhedron(0);
305 CS
= Polyhedron_Scan(CC
, U
, options
->barvinok
->MaxRays
);
312 p
= Vector_Alloc(P
->Dimension
+2);
313 value_set_si(p
->p
[P
->Dimension
+1], 1);
315 /* S = scanning list of polyhedra */
316 S
= Polyhedron_Scan(P
, *C
, options
->barvinok
->MaxRays
);
318 if (!options
->verify
.print_all
)
319 if ((*C
)->Dimension
> 0) {
320 int d
= options
->verify
.M
- options
->verify
.m
;
325 for (int i
= options
->verify
.m
; i
<= options
->verify
.M
; i
+= st
)
331 /******* CHECK NOW *********/
333 if (!options
->series
|| options
->function
) {
334 if (!check_poly(S
, CS
, en
, (*C
)->Dimension
, 0, p
->p
,
335 options
->verify
.print_all
))
338 if (!check_series(S
, CS
, gf
, (*C
)->Dimension
, 0, p
->p
,
339 options
->verify
.print_all
, options
->barvinok
))
346 fprintf(stderr
,"Check failed !\n");
348 if (!options
->verify
.print_all
)
358 static void unimodular_complete(Matrix
*M
, int row
)
361 left_hermite(M
, &H
, &Q
, &U
);
364 for (int r
= row
; r
< M
->NbRows
; ++r
)
365 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
369 static skewed_gen_fun
*series(Polyhedron
*P
, Polyhedron
* C
,
370 barvinok_options
*options
)
379 /* Compute true context */
380 C1
= Polyhedron_Project(P
, C
->Dimension
);
381 C2
= DomainIntersection(C
, C1
, options
->MaxRays
);
384 POL_ENSURE_VERTICES(C2
);
385 if (C2
->NbBid
!= 0) {
387 Matrix
*M
, *Minv
, *M2
;
389 if (C2
->NbEq
|| P
->NbEq
) {
390 /* We remove all equalities to be sure all lines are unit vectors */
392 remove_all_equalities(&PT
, &CT
, &CP
, NULL
, C2
->Dimension
,
399 inv
= left_inverse(CP
, &eq
);
405 div
= Matrix_Alloc(inv
->NbRows
-1, inv
->NbColumns
+1);
406 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
407 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &tmp
);
408 if (mpz_divisible_p(tmp
,
409 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
411 Vector_Copy(inv
->p
[i
], div
->p
[d
], inv
->NbColumns
);
412 value_assign(div
->p
[d
][inv
->NbColumns
],
413 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
425 POL_ENSURE_VERTICES(C2
);
427 /* Since we have "compressed" the parameters (in case there were
428 * any equalities), the result is independent of the coordinates in the
429 * coordinate subspace spanned by the lines. We can therefore assume
430 * these coordinates are zero and compute the inverse image of the map
431 * from a lower dimensional space that adds zeros in the appropriate
434 M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
-C2
->NbBid
+1);
436 for (int i
= 0; i
< C2
->NbBid
; ++i
) {
437 int j
= First_Non_Zero(C2
->Ray
[i
]+1, C2
->Dimension
);
438 assert(First_Non_Zero(C2
->Ray
[i
]+1+j
+1, C2
->Dimension
-j
-1) == -1);
440 value_set_si(M
->p
[k
+i
][k
], 1);
442 for ( ; k
< C2
->Dimension
-C2
->NbBid
+1; k
++)
443 value_set_si(M
->p
[k
+C2
->NbBid
][k
], 1);
445 M2
= align_matrix(M
, PT
->Dimension
+ 1);
448 PT
= Polyhedron_Preimage(PT
, M2
, options
->MaxRays
);
453 C2
= Polyhedron_Preimage(C2
, M
, options
->MaxRays
);
462 inv
= Matrix_Alloc(Minv
->NbRows
, T
->NbColumns
);
463 Matrix_Product(Minv
, T
, inv
);
469 gf
= barvinok_series_with_options(PT
, C2
, options
);
473 return new skewed_gen_fun(gf
, inv
, eq
, div
);
476 int main(int argc
, char **argv
)
481 Enumeration
*en
= NULL
;
482 skewed_gen_fun
*gf
= NULL
;
484 int print_solution
= 1;
486 struct arguments options
;
487 static struct argp_child argp_children
[] = {
488 { &barvinok_argp
, 0, 0, 0 },
489 { &verify_argp
, 0, "verification", 1 },
492 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
493 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
495 options
.barvinok
= bv_options
;
496 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
499 A
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
502 C
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
504 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
506 if (options
.verify
.verify
) {
507 verify_options_set_range(&options
.verify
, A
);
508 if (!options
.verbose
)
512 if (print_solution
) {
513 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
514 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
517 if (options
.series
) {
518 gf
= series(A
, C
, bv_options
);
519 if (print_solution
) {
520 gf
->print(stdout
, C
->Dimension
, param_name
);
523 if (options
.function
) {
526 print_evalue(stdout
, EP
, param_name
);
529 EP
= barvinok_enumerate_with_options(A
, C
, bv_options
);
531 print_evalue(stdout
, EP
, param_name
);
533 printf("\nSize: %d\n", evalue_size(EP
));
535 fprintf(stderr
, "WARNING: floor conversion not supported\n");
536 evalue_frac2floor2(EP
, 0);
537 print_evalue(stdout
, EP
, param_name
);
538 } else if (options
.convert
) {
539 evalue_mod2table(EP
, C
->Dimension
);
540 print_evalue(stdout
, EP
, param_name
);
542 printf("\nSize: %d\n", evalue_size(EP
));
546 if (options
.verify
.verify
) {
548 en
= partition2enumeration(EP
);
551 result
= verify(A
, &C
, en
, gf
, &options
);
555 Enumeration_Free(en
);
559 free_evalue_refs(EP
);
563 Free_ParamNames(param_name
, C
->Dimension
);