4 #include <isl_set_polylib.h>
5 #include <barvinok/evalue.h>
6 #include <barvinok/util.h>
7 #include <barvinok/barvinok.h>
11 #include "verif_ehrhart.h"
12 #include "verify_series.h"
13 #include "remove_equalities.h"
14 #include "evalue_convert.h"
15 #include "conversion.h"
16 #include "skewed_genfun.h"
18 #undef CS /* for Solaris 10 */
23 /* The input of this example program is the same as that of testehrhart
24 * in the PolyLib distribution, i.e., a polytope in combined
25 * data and parameter space, a context polytope in parameter space
26 * and (optionally) the names of the parameters.
27 * Both polytopes are in PolyLib notation.
30 struct argp_option argp_options
[] = {
32 { "series", 's', 0, 0, "compute rational generating function" },
33 { "explicit", 'e', 0, 0, "convert rgf to psp" },
41 struct verify_options verify
;
42 struct convert_options convert
;
45 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
47 struct arguments
*options
= (struct arguments
*) state
->input
;
51 state
->child_inputs
[0] = options
->verify
.barvinok
;
52 state
->child_inputs
[1] = &options
->verify
;
53 state
->child_inputs
[2] = &options
->convert
;
56 options
->function
= 0;
62 options
->function
= 1;
68 return ARGP_ERR_UNKNOWN
;
73 static __isl_give isl_set
*set_bounds(__isl_take isl_set
*set
,
74 const struct verify_options
*options
)
81 nparam
= isl_set_dim(set
, isl_dim_param
);
84 pt
= isl_set_sample_point(isl_set_copy(set
));
85 pt2
= isl_point_copy(pt
);
87 for (i
= 0; i
< nparam
; ++i
) {
88 pt
= isl_point_add_ui(pt
, isl_dim_param
, i
, options
->r
);
89 pt2
= isl_point_sub_ui(pt2
, isl_dim_param
, i
, options
->r
);
95 pt
= isl_point_zero(isl_set_get_dim(set
));
96 isl_int_set_si(v
, options
->m
);
97 for (i
= 0; i
< nparam
; ++i
)
98 pt
= isl_point_set_coordinate(pt
, isl_dim_param
, i
, v
);
100 pt2
= isl_point_zero(isl_set_get_dim(set
));
101 isl_int_set_si(v
, options
->M
);
102 for (i
= 0; i
< nparam
; ++i
)
103 pt2
= isl_point_set_coordinate(pt2
, isl_dim_param
, i
, v
);
108 box
= isl_set_box_from_points(pt
, pt2
);
110 return isl_set_intersect(set
, box
);
113 struct verify_point_data
{
114 const struct verify_options
*options
;
116 isl_pw_qpolynomial
*pwqp
;
122 static int verify_point(__isl_take isl_point
*pnt
, void *user
)
124 struct verify_point_data
*vpd
= (struct verify_point_data
*) user
;
129 isl_qpolynomial
*cnt
= NULL
;
130 int pa
= vpd
->options
->barvinok
->polynomial_approximation
;
133 FILE *out
= vpd
->options
->print_all
? stdout
: stderr
;
140 set
= isl_set_copy(vpd
->set
);
141 nparam
= isl_set_dim(set
, isl_dim_param
);
142 for (i
= 0; i
< nparam
; ++i
) {
143 isl_point_get_coordinate(pnt
, isl_dim_param
, i
, &v
);
144 set
= isl_set_fix(set
, isl_dim_param
, i
, v
);
147 if (isl_set_count(set
, &v
) < 0)
150 cnt
= isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vpd
->pwqp
),
151 isl_point_copy(pnt
));
153 cst
= isl_qpolynomial_is_cst(cnt
, &n
, &d
);
157 if (pa
== BV_APPROX_SIGN_LOWER
)
158 isl_int_cdiv_q(n
, n
, d
);
159 else if (pa
== BV_APPROX_SIGN_UPPER
)
160 isl_int_fdiv_q(n
, n
, d
);
162 isl_int_tdiv_q(n
, n
, d
);
164 if (pa
== BV_APPROX_SIGN_APPROX
)
165 /* just accept everything */
167 else if (pa
== BV_APPROX_SIGN_LOWER
)
168 ok
= isl_int_le(n
, v
);
169 else if (pa
== BV_APPROX_SIGN_UPPER
)
170 ok
= isl_int_ge(n
, v
);
172 ok
= isl_int_eq(n
, v
);
174 if (vpd
->options
->print_all
|| !ok
) {
176 for (i
= 0; i
< nparam
; ++i
) {
179 isl_point_get_coordinate(pnt
, isl_dim_param
, i
, &d
);
180 isl_int_print(out
, d
, 0);
182 fprintf(out
, ") = ");
183 isl_int_print(out
, n
, 0);
184 fprintf(out
, ", count = ");
185 isl_int_print(out
, v
, 0);
187 fprintf(out
, ". OK\n");
189 fprintf(out
, ". NOT OK\n");
190 } else if ((vpd
->n
% vpd
->s
) == 0) {
200 isl_qpolynomial_free(cnt
);
209 if (vpd
->options
->continue_on_error
)
212 return (vpd
->n
>= 1 && ok
) ? 0 : -1;
215 static int verify_isl(Polyhedron
*P
, Polyhedron
*C
,
216 evalue
*EP
, const struct verify_options
*options
)
218 struct verify_point_data vpd
= { options
};
220 isl_ctx
*ctx
= isl_ctx_alloc();
227 dim
= isl_dim_set_alloc(ctx
, C
->Dimension
, P
->Dimension
- C
->Dimension
);
228 for (i
= 0; i
< C
->Dimension
; ++i
)
229 dim
= isl_dim_set_name(dim
, isl_dim_param
, i
, options
->params
[i
]);
230 set
= isl_set_new_from_polylib(P
, isl_dim_copy(dim
));
231 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, P
->Dimension
- C
->Dimension
);
232 set_C
= isl_set_new_from_polylib(C
, dim
);
233 set_C
= isl_set_intersect(isl_set_copy(set
), set_C
);
234 set_C
= isl_set_remove(set_C
, isl_dim_set
, 0, P
->Dimension
- C
->Dimension
);
236 set_C
= set_bounds(set_C
, options
);
239 r
= isl_set_count(set_C
, &v
);
240 vpd
.n
= isl_int_cmp_si(v
, 200) < 0 ? isl_int_get_si(v
) : 200;
243 if (!options
->print_all
) {
244 vpd
.s
= vpd
.n
< 80 ? 1 : 1 + vpd
.n
/80;
245 for (i
= 0; i
< vpd
.n
; i
+= vpd
.s
)
252 vpd
.pwqp
= isl_pw_qpolynomial_from_evalue(isl_set_get_dim(set_C
), EP
);
255 isl_set_foreach_point(set_C
, verify_point
, &vpd
);
259 isl_pw_qpolynomial_free(vpd
.pwqp
);
265 if (!options
->print_all
)
269 fprintf(stderr
, "Check failed !\n");
274 static int verify(Polyhedron
*P
, Polyhedron
*C
, evalue
*EP
, skewed_gen_fun
*gf
,
281 if (!options
->series
|| options
->function
)
282 return verify_isl(P
, C
, EP
, &options
->verify
);
284 CS
= check_poly_context_scan(P
, &C
, C
->Dimension
, &options
->verify
);
286 p
= Vector_Alloc(P
->Dimension
+2);
287 value_set_si(p
->p
[P
->Dimension
+1], 1);
289 /* S = scanning list of polyhedra */
290 S
= Polyhedron_Scan(P
, C
, options
->verify
.barvinok
->MaxRays
);
292 check_poly_init(C
, &options
->verify
);
294 /******* CHECK NOW *********/
296 if (!options
->series
|| options
->function
) {
297 if (!check_poly_EP(S
, CS
, EP
, 0, C
->Dimension
, 0, p
->p
,
301 if (!check_poly_gf(S
, CS
, gf
, 0, C
->Dimension
, 0, p
->p
,
309 fprintf(stderr
,"Check failed !\n");
311 if (!options
->verify
.print_all
)
323 /* frees M and Minv */
324 static void apply_transformation(Polyhedron
**P
, Polyhedron
**C
,
325 bool free_P
, bool free_C
,
326 Matrix
*M
, Matrix
*Minv
, Matrix
**inv
,
327 barvinok_options
*options
)
332 M2
= align_matrix(M
, (*P
)->Dimension
+ 1);
334 *P
= Polyhedron_Preimage(*P
, M2
, options
->MaxRays
);
340 *C
= Polyhedron_Preimage(*C
, M
, options
->MaxRays
);
348 *inv
= Matrix_Alloc(Minv
->NbRows
, T
->NbColumns
);
349 Matrix_Product(Minv
, T
, *inv
);
356 /* Since we have "compressed" the parameters (in case there were
357 * any equalities), the result is independent of the coordinates in the
358 * coordinate subspace spanned by the lines. We can therefore assume
359 * these coordinates are zero and compute the inverse image of the map
360 * from a lower dimensional space that adds zeros in the appropriate
363 static void remove_lines(Polyhedron
*C
, Matrix
**M
, Matrix
**Minv
)
365 Matrix
*L
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
+1);
366 for (int r
= 0; r
< C
->NbBid
; ++r
)
367 Vector_Copy(C
->Ray
[r
]+1, L
->p
[r
], C
->Dimension
);
368 unimodular_complete(L
, C
->NbBid
);
369 assert(value_one_p(L
->p
[C
->Dimension
][C
->Dimension
]));
370 assert(First_Non_Zero(L
->p
[C
->Dimension
], C
->Dimension
) == -1);
371 Matrix_Transposition(L
);
372 assert(First_Non_Zero(L
->p
[C
->Dimension
], C
->Dimension
) == -1);
374 *M
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
-C
->NbBid
+1);
375 for (int i
= 0; i
< C
->Dimension
+1; ++i
)
376 Vector_Copy(L
->p
[i
]+C
->NbBid
, (*M
)->p
[i
], C
->Dimension
-C
->NbBid
+1);
378 Matrix
*Linv
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
+1);
379 int ok
= Matrix_Inverse(L
, Linv
);
383 *Minv
= Matrix_Alloc(C
->Dimension
-C
->NbBid
+1, C
->Dimension
+1);
384 for (int i
= C
->NbBid
; i
< C
->Dimension
+1; ++i
)
385 Vector_AntiScale(Linv
->p
[i
], (*Minv
)->p
[i
-C
->NbBid
],
386 Linv
->p
[C
->Dimension
][C
->Dimension
], C
->Dimension
+1);
390 static skewed_gen_fun
*series(Polyhedron
*P
, Polyhedron
* C
,
391 barvinok_options
*options
)
400 /* Compute true context */
401 C1
= Polyhedron_Project(P
, C
->Dimension
);
402 C2
= DomainIntersection(C
, C1
, options
->MaxRays
);
405 POL_ENSURE_VERTICES(C2
);
406 if (C2
->NbBid
!= 0) {
408 Matrix
*M
, *Minv
, *M2
;
410 if (C2
->NbEq
|| P
->NbEq
) {
411 /* We remove all equalities to be sure all lines are unit vectors */
413 remove_all_equalities(&PT
, &CT
, &CP
, NULL
, C2
->Dimension
,
420 inv
= left_inverse(CP
, &eq
);
426 div
= Matrix_Alloc(inv
->NbRows
-1, inv
->NbColumns
+1);
427 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
428 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &tmp
);
429 if (mpz_divisible_p(tmp
,
430 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
432 Vector_Copy(inv
->p
[i
], div
->p
[d
], inv
->NbColumns
);
433 value_assign(div
->p
[d
][inv
->NbColumns
],
434 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
446 POL_ENSURE_VERTICES(C2
);
450 remove_lines(C2
, &M
, &Minv
);
451 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Minv
, &inv
,
455 POL_ENSURE_VERTICES(C2
);
456 if (!Polyhedron_has_revlex_positive_rays(C2
, C2
->Dimension
)) {
460 Constraints
= Matrix_Alloc(C2
->NbConstraints
, C2
->Dimension
+1);
461 for (int i
= 0; i
< C2
->NbConstraints
; ++i
)
462 Vector_Copy(C2
->Constraint
[i
]+1, Constraints
->p
[i
], C2
->Dimension
);
463 left_hermite(Constraints
, &H
, &Q
, &U
);
464 Matrix_Free(Constraints
);
466 for (int i
= 0; i
< C2
->Dimension
/2; ++i
)
467 Vector_Exchange(Q
->p
[i
], Q
->p
[C2
->Dimension
-1-i
], C2
->Dimension
);
470 Matrix
*M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
+1);
472 int ok
= Matrix_Inverse(U
, M
);
476 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Q
, &inv
, options
);
478 gf
= barvinok_series_with_options(PT
, C2
, options
);
482 return new skewed_gen_fun(gf
, inv
, eq
, div
);
485 int main(int argc
, char **argv
)
490 skewed_gen_fun
*gf
= NULL
;
491 const char **param_name
;
492 int print_solution
= 1;
494 struct arguments options
;
495 static struct argp_child argp_children
[] = {
496 { &barvinok_argp
, 0, 0, 0 },
497 { &verify_argp
, 0, "verification", BV_GRP_LAST
+1 },
498 { &convert_argp
, 0, "output conversion", BV_GRP_LAST
+2 },
501 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
502 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
504 options
.verify
.barvinok
= bv_options
;
505 set_program_name(argv
[0]);
506 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
510 A
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
514 C
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
516 assert(A
->Dimension
>= C
->Dimension
);
517 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
519 if (options
.verify
.verify
) {
520 verify_options_set_range(&options
.verify
, A
->Dimension
);
521 if (!bv_options
->verbose
)
525 if (print_solution
&& bv_options
->verbose
) {
526 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
527 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
530 if (options
.series
) {
531 gf
= series(A
, C
, bv_options
);
532 if (print_solution
) {
533 gf
->print(cout
, C
->Dimension
, param_name
);
536 if (options
.function
) {
539 print_evalue(stdout
, EP
, param_name
);
542 EP
= barvinok_enumerate_with_options(A
, C
, bv_options
);
544 if (evalue_convert(EP
, &options
.convert
, bv_options
->verbose
,
545 C
->Dimension
, param_name
))
548 printf("\nSize: %d\n", evalue_size(EP
));
550 print_evalue(stdout
, EP
, param_name
);
553 if (options
.verify
.verify
) {
554 options
.verify
.params
= param_name
;
555 result
= verify(A
, C
, EP
, gf
, &options
);
563 if (options
.verify
.barvinok
->print_stats
)
564 barvinok_stats_print(options
.verify
.barvinok
->stats
, stdout
);
566 Free_ParamNames(param_name
, C
->Dimension
);
569 barvinok_options_free(bv_options
);