4 #include <barvinok/evalue.h>
5 #include <barvinok/util.h>
6 #include <barvinok/barvinok.h>
10 #include "verif_ehrhart.h"
11 #include "remove_equalities.h"
12 #include "evalue_convert.h"
13 #include "conversion.h"
15 #undef CS /* for Solaris 10 */
20 /* The input of this example program is the same as that of testehrhart
21 * in the PolyLib distribution, i.e., a polytope in combined
22 * data and parameter space, a context polytope in parameter space
23 * and (optionally) the names of the parameters.
24 * Both polytopes are in PolyLib notation.
27 #define PRINT_STATS (BV_OPT_LAST+1)
29 struct argp_option argp_options
[] = {
31 { "series", 's', 0, 0, "compute rational generating function" },
32 { "explicit", 'e', 0, 0, "convert rgf to psp" },
34 { "print-stats", PRINT_STATS
, 0, 0 },
44 struct verify_options verify
;
45 struct convert_options convert
;
48 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
50 struct arguments
*options
= (struct arguments
*) state
->input
;
54 state
->child_inputs
[0] = options
->verify
.barvinok
;
55 state
->child_inputs
[1] = &options
->verify
;
56 state
->child_inputs
[2] = &options
->convert
;
59 options
->function
= 0;
61 options
->print_stats
= 0;
64 options
->print_stats
= 1;
70 options
->function
= 1;
79 return ARGP_ERR_UNKNOWN
;
84 struct skewed_gen_fun
{
86 /* maps original space to space in which gf is defined */
88 /* equalities in the original space that need to be satisfied for
92 /* divisibilities in the original space that need to be satisfied for
97 skewed_gen_fun(gen_fun
*gf
, Matrix
*T
, Matrix
*eq
, Matrix
*div
) :
98 gf(gf
), T(T
), eq(eq
), div(div
) {}
109 void print(std::ostream
& os
, unsigned int nparam
, char **param_name
) const;
110 operator evalue
*() const {
111 assert(T
== NULL
&& eq
== NULL
); /* other cases not supported for now */
114 void coefficient(Value
* params
, Value
* c
, barvinok_options
*options
) const;
117 void skewed_gen_fun::print(std::ostream
& os
, unsigned int nparam
,
118 char **param_name
) const
123 matrix2zz(T
, m
, T
->NbRows
, T
->NbColumns
);
128 matrix2zz(eq
, m
, eq
->NbRows
, eq
->NbColumns
);
132 os
<< "div:" << endl
;
133 matrix2zz(div
, m
, div
->NbRows
, div
->NbColumns
);
136 gf
->print(os
, nparam
, param_name
);
139 void skewed_gen_fun::coefficient(Value
* params
, Value
* c
,
140 barvinok_options
*options
) const
143 for (int i
= 0; i
< eq
->NbRows
; ++i
) {
144 Inner_Product(eq
->p
[i
]+1, params
, eq
->NbColumns
-2, eq
->p
[i
]);
145 if (value_notzero_p(eq
->p
[i
][0])) {
154 for (int i
= 0; i
< div
->NbRows
; ++i
) {
155 Inner_Product(div
->p
[i
], params
, div
->NbColumns
-1, &tmp
);
156 if (!mpz_divisible_p(tmp
, div
->p
[i
][div
->NbColumns
-1])) {
166 coeff
= gf
->coefficient(params
, options
);
168 Vector
*p2
= Vector_Alloc(T
->NbRows
);
169 Matrix_Vector_Product(T
, params
, p2
->p
);
170 if (value_notone_p(p2
->p
[T
->NbRows
-1]))
171 Vector_AntiScale(p2
->p
, p2
->p
, p2
->p
[T
->NbRows
-1], T
->NbRows
);
172 coeff
= gf
->coefficient(p2
->p
, options
);
179 static int check_series(Polyhedron
*S
, Polyhedron
*CS
, skewed_gen_fun
*gf
,
180 int nparam
, int pos
, Value
*z
, verify_options
*options
)
192 /* Computes the coefficient */
193 gf
->coefficient(&z
[S
->Dimension
-nparam
+1], &c
, options
->barvinok
);
195 /* if c=0 we may be out of context. */
196 /* scanning is useless in this case*/
198 if (options
->print_all
) {
200 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
201 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
203 value_print(stdout
,VALUE_FMT
,z
[k
]);
206 value_print(stdout
,VALUE_FMT
,c
);
210 /* Manually count the number of points */
211 count_points(1,S
,z
,&tmp
);
212 if (options
->print_all
) {
213 printf(", count = ");
214 value_print(stdout
, P_VALUE_FMT
, tmp
);
218 if (value_ne(tmp
,c
)) {
221 fprintf(stderr
,"Error !\n");
222 fprintf(stderr
,"EP( ");
223 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
224 for (k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
225 fprintf(stderr
,", ");
226 value_print(stderr
,VALUE_FMT
,z
[k
]);
228 fprintf(stderr
," ) should be ");
229 value_print(stderr
,VALUE_FMT
,tmp
);
230 fprintf(stderr
,", while EP eval gives ");
231 value_print(stderr
,VALUE_FMT
,c
);
232 fprintf(stderr
,".\n");
233 if (!options
->continue_on_error
) {
234 value_clear(c
); value_clear(tmp
);
237 } else if (options
->print_all
)
241 !(lower_upper_bounds(1+pos
, CS
, &z
[S
->Dimension
-nparam
], &LB
, &UB
));
243 for (value_assign(tmp
,LB
); value_le(tmp
,UB
); value_increment(tmp
,tmp
)) {
244 if (!options
->print_all
) {
245 k
= VALUE_TO_INT(tmp
);
246 if(!pos
&& !(k
% options
->st
)) {
251 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
252 if (!check_series(S
, CS
->next
, gf
, nparam
, pos
+1, z
, options
)) {
253 value_clear(c
); value_clear(tmp
);
259 value_set_si(z
[pos
+S
->Dimension
-nparam
+1],0);
269 static int verify(Polyhedron
*P
, Polyhedron
*C
, evalue
*EP
, skewed_gen_fun
*gf
,
276 CS
= check_poly_context_scan(P
, &C
, C
->Dimension
, &options
->verify
);
278 p
= Vector_Alloc(P
->Dimension
+2);
279 value_set_si(p
->p
[P
->Dimension
+1], 1);
281 /* S = scanning list of polyhedra */
282 S
= Polyhedron_Scan(P
, C
, options
->verify
.barvinok
->MaxRays
);
284 check_poly_init(C
, &options
->verify
);
286 /******* CHECK NOW *********/
288 if (!options
->series
|| options
->function
) {
289 if (!check_poly_EP(S
, CS
, EP
, 0, C
->Dimension
, 0, p
->p
,
293 if (!check_series(S
, CS
, gf
, C
->Dimension
, 0, p
->p
, &options
->verify
))
300 fprintf(stderr
,"Check failed !\n");
302 if (!options
->verify
.print_all
)
314 /* frees M and Minv */
315 static void apply_transformation(Polyhedron
**P
, Polyhedron
**C
,
316 bool free_P
, bool free_C
,
317 Matrix
*M
, Matrix
*Minv
, Matrix
**inv
,
318 barvinok_options
*options
)
323 M2
= align_matrix(M
, (*P
)->Dimension
+ 1);
325 *P
= Polyhedron_Preimage(*P
, M2
, options
->MaxRays
);
331 *C
= Polyhedron_Preimage(*C
, M
, options
->MaxRays
);
339 *inv
= Matrix_Alloc(Minv
->NbRows
, T
->NbColumns
);
340 Matrix_Product(Minv
, T
, *inv
);
347 /* Since we have "compressed" the parameters (in case there were
348 * any equalities), the result is independent of the coordinates in the
349 * coordinate subspace spanned by the lines. We can therefore assume
350 * these coordinates are zero and compute the inverse image of the map
351 * from a lower dimensional space that adds zeros in the appropriate
354 static void remove_lines(Polyhedron
*C
, Matrix
**M
, Matrix
**Minv
)
356 Matrix
*L
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
+1);
357 for (int r
= 0; r
< C
->NbBid
; ++r
)
358 Vector_Copy(C
->Ray
[r
]+1, L
->p
[r
], C
->Dimension
);
359 unimodular_complete(L
, C
->NbBid
);
360 assert(value_one_p(L
->p
[C
->Dimension
][C
->Dimension
]));
361 assert(First_Non_Zero(L
->p
[C
->Dimension
], C
->Dimension
) == -1);
362 Matrix_Transposition(L
);
363 assert(First_Non_Zero(L
->p
[C
->Dimension
], C
->Dimension
) == -1);
365 *M
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
-C
->NbBid
+1);
366 for (int i
= 0; i
< C
->Dimension
+1; ++i
)
367 Vector_Copy(L
->p
[i
]+C
->NbBid
, (*M
)->p
[i
], C
->Dimension
-C
->NbBid
+1);
369 Matrix
*Linv
= Matrix_Alloc(C
->Dimension
+1, C
->Dimension
+1);
370 int ok
= Matrix_Inverse(L
, Linv
);
374 *Minv
= Matrix_Alloc(C
->Dimension
-C
->NbBid
+1, C
->Dimension
+1);
375 for (int i
= C
->NbBid
; i
< C
->Dimension
+1; ++i
)
376 Vector_AntiScale(Linv
->p
[i
], (*Minv
)->p
[i
-C
->NbBid
],
377 Linv
->p
[C
->Dimension
][C
->Dimension
], C
->Dimension
+1);
381 static skewed_gen_fun
*series(Polyhedron
*P
, Polyhedron
* C
,
382 barvinok_options
*options
)
391 /* Compute true context */
392 C1
= Polyhedron_Project(P
, C
->Dimension
);
393 C2
= DomainIntersection(C
, C1
, options
->MaxRays
);
396 POL_ENSURE_VERTICES(C2
);
397 if (C2
->NbBid
!= 0) {
399 Matrix
*M
, *Minv
, *M2
;
401 if (C2
->NbEq
|| P
->NbEq
) {
402 /* We remove all equalities to be sure all lines are unit vectors */
404 remove_all_equalities(&PT
, &CT
, &CP
, NULL
, C2
->Dimension
,
411 inv
= left_inverse(CP
, &eq
);
417 div
= Matrix_Alloc(inv
->NbRows
-1, inv
->NbColumns
+1);
418 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
419 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &tmp
);
420 if (mpz_divisible_p(tmp
,
421 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
423 Vector_Copy(inv
->p
[i
], div
->p
[d
], inv
->NbColumns
);
424 value_assign(div
->p
[d
][inv
->NbColumns
],
425 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
437 POL_ENSURE_VERTICES(C2
);
441 remove_lines(C2
, &M
, &Minv
);
442 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Minv
, &inv
,
446 POL_ENSURE_VERTICES(C2
);
447 if (!Polyhedron_has_revlex_positive_rays(C2
, C2
->Dimension
)) {
451 Constraints
= Matrix_Alloc(C2
->NbConstraints
, C2
->Dimension
+1);
452 for (int i
= 0; i
< C2
->NbConstraints
; ++i
)
453 Vector_Copy(C2
->Constraint
[i
]+1, Constraints
->p
[i
], C2
->Dimension
);
454 left_hermite(Constraints
, &H
, &Q
, &U
);
455 Matrix_Free(Constraints
);
457 for (int i
= 0; i
< C2
->Dimension
/2; ++i
)
458 Vector_Exchange(Q
->p
[i
], Q
->p
[C2
->Dimension
-1-i
], C2
->Dimension
);
461 Matrix
*M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
+1);
463 int ok
= Matrix_Inverse(U
, M
);
467 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Q
, &inv
, options
);
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 skewed_gen_fun
*gf
= NULL
;
483 int print_solution
= 1;
485 struct arguments options
;
486 static struct argp_child argp_children
[] = {
487 { &barvinok_argp
, 0, 0, 0 },
488 { &verify_argp
, 0, "verification", BV_GRP_LAST
+1 },
489 { &convert_argp
, 0, "output conversion", BV_GRP_LAST
+2 },
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
.verify
.barvinok
= bv_options
;
496 set_program_name(argv
[0]);
497 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
501 A
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
505 C
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
507 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
509 if (options
.verify
.verify
) {
510 verify_options_set_range(&options
.verify
, A
->Dimension
);
511 if (!options
.verbose
)
515 if (print_solution
&& options
.verbose
) {
516 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
517 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
520 if (options
.series
) {
521 gf
= series(A
, C
, bv_options
);
522 if (print_solution
) {
523 gf
->print(cout
, C
->Dimension
, param_name
);
526 if (options
.function
) {
529 print_evalue(stdout
, EP
, param_name
);
532 EP
= barvinok_enumerate_with_options(A
, C
, bv_options
);
533 if (evalue_convert(EP
, &options
.convert
, options
.verbose
, C
->Dimension
,
537 printf("\nSize: %d\n", evalue_size(EP
));
539 print_evalue(stdout
, EP
, param_name
);
542 if (options
.verify
.verify
) {
543 options
.verify
.params
= param_name
;
544 result
= verify(A
, C
, EP
, gf
, &options
);
552 if (options
.print_stats
)
553 barvinok_stats_print(options
.verify
.barvinok
->stats
, stdout
);
555 Free_ParamNames(param_name
, C
->Dimension
);
558 barvinok_options_free(bv_options
);