10 #include <isl/polynomial.h>
11 #include <isl/printer.h>
12 #include <isl_set_polylib.h>
13 #include <barvinok/evalue.h>
14 #include <barvinok/util.h>
15 #include <barvinok/barvinok.h>
16 #include "barvinok_enumerate_options.h"
18 #include "verify_series.h"
19 #include "remove_equalities.h"
20 #include "evalue_convert.h"
21 #include "conversion.h"
22 #include "skewed_genfun.h"
24 #undef CS /* for Solaris 10 */
29 /* The input of this example program is the same as that of testehrhart
30 * in the PolyLib distribution, i.e., a polytope in combined
31 * data and parameter space, a context polytope in parameter space
32 * and (optionally) the names of the parameters.
33 * Both polytopes are in PolyLib notation.
36 struct verify_point_enum
{
37 struct verify_point_data vpd
;
39 isl_pw_qpolynomial
*pwqp
;
42 static isl_stat
verify_point(__isl_take isl_point
*pnt
, void *user
)
44 struct verify_point_enum
*vpe
= (struct verify_point_enum
*) user
;
49 int pa
= vpe
->vpd
.options
->barvinok
->approx
->approximation
;
51 FILE *out
= vpe
->vpd
.options
->print_all
? stdout
: stderr
;
55 set
= isl_set_copy(vpe
->set
);
56 nparam
= isl_set_dim(set
, isl_dim_param
);
57 for (i
= 0; i
< nparam
; ++i
) {
58 v
= isl_point_get_coordinate_val(pnt
, isl_dim_param
, i
);
59 set
= isl_set_fix_val(set
, isl_dim_param
, i
, v
);
62 v
= isl_set_count_val(set
);
64 n
= isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vpe
->pwqp
),
67 if (pa
== BV_APPROX_SIGN_LOWER
)
69 else if (pa
== BV_APPROX_SIGN_UPPER
)
74 if (pa
== BV_APPROX_SIGN_APPROX
)
75 /* just accept everything */
77 else if (pa
== BV_APPROX_SIGN_LOWER
)
78 ok
= isl_val_le(n
, v
);
79 else if (pa
== BV_APPROX_SIGN_UPPER
)
80 ok
= isl_val_ge(n
, v
);
82 ok
= isl_val_eq(n
, v
);
84 if (vpe
->vpd
.options
->print_all
|| !ok
) {
85 isl_ctx
*ctx
= isl_point_get_ctx(pnt
);
87 p
= isl_printer_to_file(ctx
, out
);
88 p
= isl_printer_print_str(p
, "EP(");
89 for (i
= 0; i
< nparam
; ++i
) {
91 p
= isl_printer_print_str(p
, ", ");
92 t
= isl_point_get_coordinate_val(pnt
, isl_dim_param
, i
);
93 p
= isl_printer_print_val(p
, t
);
96 p
= isl_printer_print_str(p
, ") = ");
97 p
= isl_printer_print_val(p
, n
);
98 p
= isl_printer_print_str(p
, ", count = ");
99 p
= isl_printer_print_val(p
, v
);
101 p
= isl_printer_print_str(p
, ". OK");
103 p
= isl_printer_print_str(p
, ". NOT OK");
104 p
= isl_printer_end_line(p
);
106 } else if ((vpe
->vpd
.n
% vpe
->vpd
.s
) == 0) {
119 if (vpe
->vpd
.options
->continue_on_error
)
122 return (vpe
->vpd
.n
>= 1 && ok
) ? isl_stat_ok
: isl_stat_error
;
125 static int verify_isl(Polyhedron
*P
, Polyhedron
*C
,
126 evalue
*EP
, const struct verify_options
*options
)
128 struct verify_point_enum vpe
= { { options
} };
130 isl_ctx
*ctx
= isl_ctx_alloc();
136 space
= isl_space_set_alloc(ctx
, C
->Dimension
,
137 P
->Dimension
- C
->Dimension
);
138 for (i
= 0; i
< C
->Dimension
; ++i
)
139 space
= isl_space_set_dim_name(space
, isl_dim_param
, i
,
141 set
= isl_set_new_from_polylib(P
, isl_space_copy(space
));
142 space
= isl_space_params(space
);
143 set_C
= isl_set_new_from_polylib(C
, space
);
144 set_C
= isl_set_intersect_params(isl_set_copy(set
), set_C
);
145 set_C
= isl_set_params(set_C
);
147 set_C
= verify_context_set_bounds(set_C
, options
);
149 r
= verify_point_data_init(&vpe
.vpd
, set_C
);
152 vpe
.pwqp
= isl_pw_qpolynomial_from_evalue(isl_set_get_space(set_C
), EP
);
154 isl_set_foreach_point(set_C
, verify_point
, &vpe
);
158 isl_pw_qpolynomial_free(vpe
.pwqp
);
164 verify_point_data_fini(&vpe
.vpd
);
169 static int verify(Polyhedron
*P
, Polyhedron
*C
, evalue
*EP
, skewed_gen_fun
*gf
,
170 struct enumerate_options
*options
)
176 if (!options
->series
|| options
->function
)
177 return verify_isl(P
, C
, EP
, options
->verify
);
179 CS
= check_poly_context_scan(P
, &C
, C
->Dimension
, options
->verify
);
181 p
= Vector_Alloc(P
->Dimension
+2);
182 value_set_si(p
->p
[P
->Dimension
+1], 1);
184 /* S = scanning list of polyhedra */
185 S
= Polyhedron_Scan(P
, C
, options
->verify
->barvinok
->MaxRays
);
187 check_poly_init(C
, options
->verify
);
189 /******* CHECK NOW *********/
191 if (!check_poly_gf(S
, CS
, gf
, 0, C
->Dimension
, 0, p
->p
,
198 fprintf(stderr
,"Check failed !\n");
200 if (!options
->verify
->print_all
)
212 /* frees M and Minv */
213 static void apply_transformation(Polyhedron
**P
, Polyhedron
**C
,
214 bool free_P
, bool free_C
,
215 Matrix
*M
, Matrix
*Minv
, Matrix
**inv
,
216 barvinok_options
*options
)
221 M2
= align_matrix(M
, (*P
)->Dimension
+ 1);
223 *P
= Polyhedron_Preimage(*P
, M2
, options
->MaxRays
);
229 *C
= Polyhedron_Preimage(*C
, M
, options
->MaxRays
);
237 *inv
= Matrix_Alloc(Minv
->NbRows
, T
->NbColumns
);
238 Matrix_Product(Minv
, T
, *inv
);
245 /* Since we have "compressed" the parameters (in case there were
246 * any equalities), the result is independent of the coordinates in the
247 * coordinate subspace spanned by the lines. We can therefore assume
248 * these coordinates are zero and compute the inverse image of the map
249 * from a lower dimensional space that adds zeros in the appropriate
252 static void remove_lines(Polyhedron
*C
, Matrix
**M
, Matrix
**Minv
)
254 Matrix
*L
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
+1);
255 for (int r
= 0; r
< C
->NbBid
; ++r
)
256 Vector_Copy(C
->Ray
[r
]+1, L
->p
[r
], C
->Dimension
);
257 unimodular_complete(L
, C
->NbBid
);
258 assert(value_one_p(L
->p
[C
->Dimension
][C
->Dimension
]));
259 assert(First_Non_Zero(L
->p
[C
->Dimension
], C
->Dimension
) == -1);
260 Matrix_Transposition(L
);
261 assert(First_Non_Zero(L
->p
[C
->Dimension
], C
->Dimension
) == -1);
263 *M
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
-C
->NbBid
+1);
264 for (int i
= 0; i
< C
->Dimension
+1; ++i
)
265 Vector_Copy(L
->p
[i
]+C
->NbBid
, (*M
)->p
[i
], C
->Dimension
-C
->NbBid
+1);
267 Matrix
*Linv
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
+1);
268 int ok
= Matrix_Inverse(L
, Linv
);
272 *Minv
= Matrix_Alloc(C
->Dimension
-C
->NbBid
+1, C
->Dimension
+1);
273 for (int i
= C
->NbBid
; i
< C
->Dimension
+1; ++i
)
274 Vector_AntiScale(Linv
->p
[i
], (*Minv
)->p
[i
-C
->NbBid
],
275 Linv
->p
[C
->Dimension
][C
->Dimension
], C
->Dimension
+1);
279 static skewed_gen_fun
*series(Polyhedron
*P
, Polyhedron
* C
,
280 barvinok_options
*options
)
289 /* Compute true context */
290 C1
= Polyhedron_Project(P
, C
->Dimension
);
291 C2
= DomainIntersection(C
, C1
, options
->MaxRays
);
294 POL_ENSURE_VERTICES(C2
);
295 if (C2
->NbBid
!= 0) {
297 if (C2
->NbEq
|| P
->NbEq
) {
298 /* We remove all equalities to be sure all lines are unit vectors */
300 remove_all_equalities(&PT
, &CT
, &CP
, NULL
, C2
->Dimension
,
307 inv
= left_inverse(CP
, &eq
);
313 div
= Matrix_Alloc(inv
->NbRows
-1, inv
->NbColumns
+1);
314 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
315 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &tmp
);
316 if (mpz_divisible_p(tmp
,
317 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
319 Vector_Copy(inv
->p
[i
], div
->p
[d
], inv
->NbColumns
);
320 value_assign(div
->p
[d
][inv
->NbColumns
],
321 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
333 POL_ENSURE_VERTICES(C2
);
337 remove_lines(C2
, &M
, &Minv
);
338 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Minv
, &inv
,
342 POL_ENSURE_VERTICES(C2
);
343 if (!Polyhedron_has_revlex_positive_rays(C2
, C2
->Dimension
)) {
346 Constraints
= Matrix_Alloc(C2
->NbConstraints
, C2
->Dimension
+1);
347 for (int i
= 0; i
< C2
->NbConstraints
; ++i
)
348 Vector_Copy(C2
->Constraint
[i
]+1, Constraints
->p
[i
], C2
->Dimension
);
349 left_hermite(Constraints
, &H
, &Q
, &U
);
350 Matrix_Free(Constraints
);
352 for (int i
= 0; i
< C2
->Dimension
/2; ++i
)
353 Vector_Exchange(Q
->p
[i
], Q
->p
[C2
->Dimension
-1-i
], C2
->Dimension
);
356 Matrix
*M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
+1);
358 int ok
= Matrix_Inverse(U
, M
);
362 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Q
, &inv
, options
);
364 gf
= barvinok_series_with_options(PT
, C2
, options
);
368 return new skewed_gen_fun(gf
, inv
, eq
, div
);
371 int main(int argc
, char **argv
)
376 skewed_gen_fun
*gf
= NULL
;
377 const char **param_name
;
378 int print_solution
= 1;
380 struct enumerate_options
*options
= enumerate_options_new_with_defaults();
382 argc
= enumerate_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
386 A
= Constraints2Polyhedron(M
, options
->verify
->barvinok
->MaxRays
);
390 C
= Constraints2Polyhedron(M
, options
->verify
->barvinok
->MaxRays
);
392 assert(A
->Dimension
>= C
->Dimension
);
393 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
395 if (options
->verify
->verify
) {
396 verify_options_set_range(options
->verify
, A
->Dimension
);
397 if (!options
->verify
->barvinok
->verbose
)
401 if (print_solution
&& options
->verify
->barvinok
->verbose
) {
402 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
403 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
406 if (options
->series
) {
407 gf
= series(A
, C
, options
->verify
->barvinok
);
408 if (print_solution
) {
409 gf
->print(cout
, C
->Dimension
, param_name
);
412 if (options
->function
) {
415 print_evalue(stdout
, EP
, param_name
);
418 EP
= barvinok_enumerate_with_options(A
, C
, options
->verify
->barvinok
);
420 if (evalue_convert(EP
, options
->convert
, options
->verify
->barvinok
->verbose
,
421 C
->Dimension
, param_name
))
424 printf("\nSize: %zd\n", evalue_size(EP
));
426 print_evalue(stdout
, EP
, param_name
);
429 if (options
->verify
->verify
) {
430 options
->verify
->params
= param_name
;
431 result
= verify(A
, C
, EP
, gf
, options
);
439 if (options
->verify
->barvinok
->print_stats
)
440 barvinok_stats_print(options
->verify
->barvinok
->stats
, stdout
);
442 Free_ParamNames(param_name
, C
->Dimension
);
445 enumerate_options_free(options
);