3 #include <barvinok/evalue.h>
4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
10 #include "verif_ehrhart.h"
11 #include "remove_equalities.h"
12 #include "evalue_convert.h"
14 #undef CS /* for Solaris 10 */
16 /* The input of this example program is the same as that of testehrhart
17 * in the PolyLib distribution, i.e., a polytope in combined
18 * data and parameter space, a context polytope in parameter space
19 * and (optionally) the names of the parameters.
20 * Both polytopes are in PolyLib notation.
23 #define PRINT_STATS (BV_OPT_LAST+1)
25 struct argp_option argp_options
[] = {
27 { "series", 's', 0, 0, "compute rational generating function" },
28 { "explicit", 'e', 0, 0, "convert rgf to psp" },
30 { "print-stats", PRINT_STATS
, 0, 0 },
40 struct verify_options verify
;
41 struct convert_options convert
;
44 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
46 struct arguments
*options
= (struct arguments
*) state
->input
;
50 state
->child_inputs
[0] = options
->verify
.barvinok
;
51 state
->child_inputs
[1] = &options
->verify
;
52 state
->child_inputs
[2] = &options
->convert
;
55 options
->function
= 0;
57 options
->print_stats
= 0;
60 options
->print_stats
= 1;
66 options
->function
= 1;
75 return ARGP_ERR_UNKNOWN
;
80 struct skewed_gen_fun
{
82 /* maps original space to space in which gf is defined */
84 /* equalities in the original space that need to be satisfied for
88 /* divisibilities in the original space that need to be satisfied for
93 skewed_gen_fun(gen_fun
*gf
, Matrix
*T
, Matrix
*eq
, Matrix
*div
) :
94 gf(gf
), T(T
), eq(eq
), div(div
) {}
105 void print(FILE *out
, unsigned int nparam
, char **param_name
) const;
106 operator evalue
*() const {
107 assert(T
== NULL
&& eq
== NULL
); /* other cases not supported for now */
110 void coefficient(Value
* params
, Value
* c
, barvinok_options
*options
) const;
113 void skewed_gen_fun::print(FILE *out
, unsigned int nparam
,
114 char **param_name
) const
116 fdostream
os(dup(fileno(out
)));
118 fprintf(out
, "T:\n");
119 Matrix_Print(out
, P_VALUE_FMT
, T
);
122 fprintf(out
, "eq:\n");
123 Matrix_Print(out
, P_VALUE_FMT
, eq
);
126 fprintf(out
, "div:\n");
127 Matrix_Print(out
, P_VALUE_FMT
, div
);
129 gf
->print(os
, nparam
, param_name
);
132 void skewed_gen_fun::coefficient(Value
* params
, Value
* c
,
133 barvinok_options
*options
) const
136 for (int i
= 0; i
< eq
->NbRows
; ++i
) {
137 Inner_Product(eq
->p
[i
]+1, params
, eq
->NbColumns
-2, eq
->p
[i
]);
138 if (value_notzero_p(eq
->p
[i
][0])) {
147 for (int i
= 0; i
< div
->NbRows
; ++i
) {
148 Inner_Product(div
->p
[i
], params
, div
->NbColumns
-1, &tmp
);
149 if (!mpz_divisible_p(tmp
, div
->p
[i
][div
->NbColumns
-1])) {
159 coeff
= gf
->coefficient(params
, options
);
161 Vector
*p2
= Vector_Alloc(T
->NbRows
);
162 Matrix_Vector_Product(T
, params
, p2
->p
);
163 if (value_notone_p(p2
->p
[T
->NbRows
-1]))
164 Vector_AntiScale(p2
->p
, p2
->p
, p2
->p
[T
->NbRows
-1], T
->NbRows
);
165 coeff
= gf
->coefficient(p2
->p
, options
);
172 static int check_series(Polyhedron
*S
, Polyhedron
*CS
, skewed_gen_fun
*gf
,
173 int nparam
, int pos
, Value
*z
, verify_options
*options
)
185 /* Computes the coefficient */
186 gf
->coefficient(&z
[S
->Dimension
-nparam
+1], &c
, options
->barvinok
);
188 /* if c=0 we may be out of context. */
189 /* scanning is useless in this case*/
191 if (options
->print_all
) {
193 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
194 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
196 value_print(stdout
,VALUE_FMT
,z
[k
]);
199 value_print(stdout
,VALUE_FMT
,c
);
203 /* Manually count the number of points */
204 count_points(1,S
,z
,&tmp
);
205 if (options
->print_all
) {
206 printf(", count = ");
207 value_print(stdout
, P_VALUE_FMT
, tmp
);
211 if (value_ne(tmp
,c
)) {
214 fprintf(stderr
,"Error !\n");
215 fprintf(stderr
,"EP( ");
216 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
217 for (k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
218 fprintf(stderr
,", ");
219 value_print(stderr
,VALUE_FMT
,z
[k
]);
221 fprintf(stderr
," ) should be ");
222 value_print(stderr
,VALUE_FMT
,tmp
);
223 fprintf(stderr
,", while EP eval gives ");
224 value_print(stderr
,VALUE_FMT
,c
);
225 fprintf(stderr
,".\n");
226 if (!options
->continue_on_error
) {
227 value_clear(c
); value_clear(tmp
);
230 } else if (options
->print_all
)
234 !(lower_upper_bounds(1+pos
, CS
, &z
[S
->Dimension
-nparam
], &LB
, &UB
));
236 for (value_assign(tmp
,LB
); value_le(tmp
,UB
); value_increment(tmp
,tmp
)) {
237 if (!options
->print_all
) {
238 k
= VALUE_TO_INT(tmp
);
239 if(!pos
&& !(k
% options
->st
)) {
244 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
245 if (!check_series(S
, CS
->next
, gf
, nparam
, pos
+1, z
, options
)) {
246 value_clear(c
); value_clear(tmp
);
252 value_set_si(z
[pos
+S
->Dimension
-nparam
+1],0);
262 static int verify(Polyhedron
*P
, Polyhedron
*C
, evalue
*EP
, skewed_gen_fun
*gf
,
269 CS
= check_poly_context_scan(P
, &C
, C
->Dimension
, &options
->verify
);
271 p
= Vector_Alloc(P
->Dimension
+2);
272 value_set_si(p
->p
[P
->Dimension
+1], 1);
274 /* S = scanning list of polyhedra */
275 S
= Polyhedron_Scan(P
, C
, options
->verify
.barvinok
->MaxRays
);
277 check_poly_init(C
, &options
->verify
);
279 /******* CHECK NOW *********/
281 if (!options
->series
|| options
->function
) {
282 if (!check_poly_EP(S
, CS
, EP
, 0, C
->Dimension
, 0, p
->p
,
286 if (!check_series(S
, CS
, gf
, C
->Dimension
, 0, p
->p
, &options
->verify
))
293 fprintf(stderr
,"Check failed !\n");
295 if (!options
->verify
.print_all
)
307 static void unimodular_complete(Matrix
*M
, int row
)
310 left_hermite(M
, &H
, &Q
, &U
);
313 for (int r
= row
; r
< M
->NbRows
; ++r
)
314 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
318 /* frees M and Minv */
319 static void apply_transformation(Polyhedron
**P
, Polyhedron
**C
,
320 bool free_P
, bool free_C
,
321 Matrix
*M
, Matrix
*Minv
, Matrix
**inv
,
322 barvinok_options
*options
)
327 M2
= align_matrix(M
, (*P
)->Dimension
+ 1);
329 *P
= Polyhedron_Preimage(*P
, M2
, options
->MaxRays
);
335 *C
= Polyhedron_Preimage(*C
, M
, options
->MaxRays
);
343 *inv
= Matrix_Alloc(Minv
->NbRows
, T
->NbColumns
);
344 Matrix_Product(Minv
, T
, *inv
);
351 static skewed_gen_fun
*series(Polyhedron
*P
, Polyhedron
* C
,
352 barvinok_options
*options
)
361 /* Compute true context */
362 C1
= Polyhedron_Project(P
, C
->Dimension
);
363 C2
= DomainIntersection(C
, C1
, options
->MaxRays
);
366 POL_ENSURE_VERTICES(C2
);
367 if (C2
->NbBid
!= 0) {
369 Matrix
*M
, *Minv
, *M2
;
371 if (C2
->NbEq
|| P
->NbEq
) {
372 /* We remove all equalities to be sure all lines are unit vectors */
374 remove_all_equalities(&PT
, &CT
, &CP
, NULL
, C2
->Dimension
,
381 inv
= left_inverse(CP
, &eq
);
387 div
= Matrix_Alloc(inv
->NbRows
-1, inv
->NbColumns
+1);
388 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
389 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &tmp
);
390 if (mpz_divisible_p(tmp
,
391 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
393 Vector_Copy(inv
->p
[i
], div
->p
[d
], inv
->NbColumns
);
394 value_assign(div
->p
[d
][inv
->NbColumns
],
395 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
407 POL_ENSURE_VERTICES(C2
);
409 /* Since we have "compressed" the parameters (in case there were
410 * any equalities), the result is independent of the coordinates in the
411 * coordinate subspace spanned by the lines. We can therefore assume
412 * these coordinates are zero and compute the inverse image of the map
413 * from a lower dimensional space that adds zeros in the appropriate
416 M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
-C2
->NbBid
+1);
418 for (int i
= 0; i
< C2
->NbBid
; ++i
) {
419 int j
= First_Non_Zero(C2
->Ray
[i
]+1, C2
->Dimension
);
420 assert(First_Non_Zero(C2
->Ray
[i
]+1+j
+1, C2
->Dimension
-j
-1) == -1);
422 value_set_si(M
->p
[k
+i
][k
], 1);
424 for ( ; k
< C2
->Dimension
-C2
->NbBid
+1; k
++)
425 value_set_si(M
->p
[k
+C2
->NbBid
][k
], 1);
428 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Minv
, &inv
, options
);
430 POL_ENSURE_VERTICES(C2
);
431 if (!Polyhedron_has_revlex_positive_rays(C2
, C2
->Dimension
)) {
435 Constraints
= Matrix_Alloc(C2
->NbConstraints
, C2
->Dimension
+1);
436 for (int i
= 0; i
< C2
->NbConstraints
; ++i
)
437 Vector_Copy(C2
->Constraint
[i
]+1, Constraints
->p
[i
], C2
->Dimension
);
438 left_hermite(Constraints
, &H
, &Q
, &U
);
440 for (int i
= 0; i
< C2
->Dimension
/2; ++i
)
441 Vector_Exchange(Q
->p
[i
], Q
->p
[C2
->Dimension
-1-i
], C2
->Dimension
);
444 Matrix
*M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
+1);
446 int ok
= Matrix_Inverse(U
, M
);
450 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Q
, &inv
, options
);
452 gf
= barvinok_series_with_options(PT
, C2
, options
);
456 return new skewed_gen_fun(gf
, inv
, eq
, div
);
459 int main(int argc
, char **argv
)
464 skewed_gen_fun
*gf
= NULL
;
466 int print_solution
= 1;
468 struct arguments options
;
469 static struct argp_child argp_children
[] = {
470 { &barvinok_argp
, 0, 0, 0 },
471 { &verify_argp
, 0, "verification", BV_GRP_LAST
+1 },
472 { &convert_argp
, 0, "output conversion", BV_GRP_LAST
+2 },
475 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
476 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
478 options
.verify
.barvinok
= bv_options
;
479 set_program_name(argv
[0]);
480 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
483 A
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
486 C
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
488 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
490 if (options
.verify
.verify
) {
491 verify_options_set_range(&options
.verify
, A
->Dimension
);
492 if (!options
.verbose
)
496 if (print_solution
&& options
.verbose
) {
497 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
498 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
501 if (options
.series
) {
502 gf
= series(A
, C
, bv_options
);
503 if (print_solution
) {
504 gf
->print(stdout
, C
->Dimension
, param_name
);
507 if (options
.function
) {
510 print_evalue(stdout
, EP
, param_name
);
513 EP
= barvinok_enumerate_with_options(A
, C
, bv_options
);
514 if (evalue_convert(EP
, &options
.convert
, options
.verbose
, C
->Dimension
,
518 printf("\nSize: %d\n", evalue_size(EP
));
521 print_evalue(stdout
, EP
, param_name
);
523 if (options
.verify
.verify
) {
524 options
.verify
.params
= param_name
;
525 result
= verify(A
, C
, EP
, gf
, &options
);
531 free_evalue_refs(EP
);
535 if (options
.print_stats
)
536 barvinok_stats_print(options
.verify
.barvinok
->stats
, stdout
);
538 Free_ParamNames(param_name
, C
->Dimension
);
541 barvinok_options_free(bv_options
);