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"
11 #include "evalue_convert.h"
13 #undef CS /* for Solaris 10 */
15 /* The input of this example program is the same as that of testehrhart
16 * in the PolyLib distribution, i.e., a polytope in combined
17 * data and parameter space, a context polytope in parameter space
18 * and (optionally) the names of the parameters.
19 * Both polytopes are in PolyLib notation.
22 #define PRINT_STATS (BV_OPT_LAST+1)
24 struct argp_option argp_options
[] = {
26 { "series", 's', 0, 0, "compute rational generating function" },
27 { "explicit", 'e', 0, 0, "convert rgf to psp" },
29 { "print-stats", PRINT_STATS
, 0, 0 },
39 struct verify_options verify
;
40 struct convert_options convert
;
43 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
45 struct arguments
*options
= (struct arguments
*) state
->input
;
49 state
->child_inputs
[0] = options
->verify
.barvinok
;
50 state
->child_inputs
[1] = &options
->verify
;
51 state
->child_inputs
[2] = &options
->convert
;
54 options
->function
= 0;
56 options
->print_stats
= 0;
59 options
->print_stats
= 1;
65 options
->function
= 1;
74 return ARGP_ERR_UNKNOWN
;
79 struct skewed_gen_fun
{
81 /* maps original space to space in which gf is defined */
83 /* equalities in the original space that need to be satisfied for
87 /* divisibilities in the original space that need to be satisfied for
92 skewed_gen_fun(gen_fun
*gf
, Matrix
*T
, Matrix
*eq
, Matrix
*div
) :
93 gf(gf
), T(T
), eq(eq
), div(div
) {}
104 void print(FILE *out
, unsigned int nparam
, char **param_name
) const;
105 operator evalue
*() const {
106 assert(T
== NULL
&& eq
== NULL
); /* other cases not supported for now */
109 void coefficient(Value
* params
, Value
* c
, barvinok_options
*options
) const;
112 void skewed_gen_fun::print(FILE *out
, unsigned int nparam
,
113 char **param_name
) const
115 fdostream
os(dup(fileno(out
)));
117 fprintf(out
, "T:\n");
118 Matrix_Print(out
, P_VALUE_FMT
, T
);
121 fprintf(out
, "eq:\n");
122 Matrix_Print(out
, P_VALUE_FMT
, eq
);
125 fprintf(out
, "div:\n");
126 Matrix_Print(out
, P_VALUE_FMT
, div
);
128 gf
->print(os
, nparam
, param_name
);
131 void skewed_gen_fun::coefficient(Value
* params
, Value
* c
,
132 barvinok_options
*options
) const
135 for (int i
= 0; i
< eq
->NbRows
; ++i
) {
136 Inner_Product(eq
->p
[i
]+1, params
, eq
->NbColumns
-2, eq
->p
[i
]);
137 if (value_notzero_p(eq
->p
[i
][0])) {
146 for (int i
= 0; i
< div
->NbRows
; ++i
) {
147 Inner_Product(div
->p
[i
], params
, div
->NbColumns
-1, &tmp
);
148 if (!mpz_divisible_p(tmp
, div
->p
[i
][div
->NbColumns
-1])) {
158 coeff
= gf
->coefficient(params
, options
);
160 Vector
*p2
= Vector_Alloc(T
->NbRows
);
161 Matrix_Vector_Product(T
, params
, p2
->p
);
162 if (value_notone_p(p2
->p
[T
->NbRows
-1]))
163 Vector_AntiScale(p2
->p
, p2
->p
, p2
->p
[T
->NbRows
-1], T
->NbRows
);
164 coeff
= gf
->coefficient(p2
->p
, options
);
171 static int check_series(Polyhedron
*S
, Polyhedron
*CS
, skewed_gen_fun
*gf
,
172 int nparam
, int pos
, Value
*z
, verify_options
*options
)
184 /* Computes the coefficient */
185 gf
->coefficient(&z
[S
->Dimension
-nparam
+1], &c
, options
->barvinok
);
187 /* if c=0 we may be out of context. */
188 /* scanning is useless in this case*/
190 if (options
->print_all
) {
192 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
193 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
195 value_print(stdout
,VALUE_FMT
,z
[k
]);
198 value_print(stdout
,VALUE_FMT
,c
);
202 /* Manually count the number of points */
203 count_points(1,S
,z
,&tmp
);
204 if (options
->print_all
) {
205 printf(", count = ");
206 value_print(stdout
, P_VALUE_FMT
, tmp
);
210 if (value_ne(tmp
,c
)) {
213 fprintf(stderr
,"Error !\n");
214 fprintf(stderr
,"EP( ");
215 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
216 for (k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
217 fprintf(stderr
,", ");
218 value_print(stderr
,VALUE_FMT
,z
[k
]);
220 fprintf(stderr
," ) should be ");
221 value_print(stderr
,VALUE_FMT
,tmp
);
222 fprintf(stderr
,", while EP eval gives ");
223 value_print(stderr
,VALUE_FMT
,c
);
224 fprintf(stderr
,".\n");
225 if (!options
->continue_on_error
) {
226 value_clear(c
); value_clear(tmp
);
229 } else if (options
->print_all
)
233 !(lower_upper_bounds(1+pos
, CS
, &z
[S
->Dimension
-nparam
], &LB
, &UB
));
235 for (value_assign(tmp
,LB
); value_le(tmp
,UB
); value_increment(tmp
,tmp
)) {
236 if (!options
->print_all
) {
237 k
= VALUE_TO_INT(tmp
);
238 if(!pos
&& !(k
% options
->st
)) {
243 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
244 if (!check_series(S
, CS
->next
, gf
, nparam
, pos
+1, z
, options
)) {
245 value_clear(c
); value_clear(tmp
);
251 value_set_si(z
[pos
+S
->Dimension
-nparam
+1],0);
261 static int verify(Polyhedron
*P
, Polyhedron
**C
, evalue
*EP
, skewed_gen_fun
*gf
,
264 Polyhedron
*CC
, *PP
, *CS
, *S
;
269 /******* Compute true context *******/
270 CC
= align_context(*C
, P
->Dimension
, options
->verify
.barvinok
->MaxRays
);
271 PP
= DomainIntersection(P
, CC
, options
->verify
.barvinok
->MaxRays
);
273 C1
= Matrix_Alloc((*C
)->Dimension
+1, P
->Dimension
+1);
275 for (int i
= 0; i
< C1
->NbRows
; i
++)
276 for (int j
= 0; j
< C1
->NbColumns
; j
++)
277 if (i
== j
-P
->Dimension
+(*C
)->Dimension
)
278 value_set_si(C1
->p
[i
][j
], 1);
280 value_set_si(C1
->p
[i
][j
], 0);
281 CC
= Polyhedron_Image(PP
, C1
, options
->verify
.barvinok
->MaxRays
);
287 CS
= check_poly_context_scan(*C
, &options
->verify
);
289 p
= Vector_Alloc(P
->Dimension
+2);
290 value_set_si(p
->p
[P
->Dimension
+1], 1);
292 /* S = scanning list of polyhedra */
293 S
= Polyhedron_Scan(P
, *C
, options
->verify
.barvinok
->MaxRays
);
295 check_poly_init(*C
, &options
->verify
);
297 /******* CHECK NOW *********/
299 if (!options
->series
|| options
->function
) {
300 if (!check_poly_EP(S
, CS
, EP
, 0, (*C
)->Dimension
, 0, p
->p
,
304 if (!check_series(S
, CS
, gf
, (*C
)->Dimension
, 0, p
->p
, &options
->verify
))
311 fprintf(stderr
,"Check failed !\n");
313 if (!options
->verify
.print_all
)
323 static void unimodular_complete(Matrix
*M
, int row
)
326 left_hermite(M
, &H
, &Q
, &U
);
329 for (int r
= row
; r
< M
->NbRows
; ++r
)
330 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
334 /* frees M and Minv */
335 static void apply_transformation(Polyhedron
**P
, Polyhedron
**C
,
336 bool free_P
, bool free_C
,
337 Matrix
*M
, Matrix
*Minv
, Matrix
**inv
,
338 barvinok_options
*options
)
343 M2
= align_matrix(M
, (*P
)->Dimension
+ 1);
345 *P
= Polyhedron_Preimage(*P
, M2
, options
->MaxRays
);
351 *C
= Polyhedron_Preimage(*C
, M
, options
->MaxRays
);
359 *inv
= Matrix_Alloc(Minv
->NbRows
, T
->NbColumns
);
360 Matrix_Product(Minv
, T
, *inv
);
367 static skewed_gen_fun
*series(Polyhedron
*P
, Polyhedron
* C
,
368 barvinok_options
*options
)
377 /* Compute true context */
378 C1
= Polyhedron_Project(P
, C
->Dimension
);
379 C2
= DomainIntersection(C
, C1
, options
->MaxRays
);
382 POL_ENSURE_VERTICES(C2
);
383 if (C2
->NbBid
!= 0) {
385 Matrix
*M
, *Minv
, *M2
;
387 if (C2
->NbEq
|| P
->NbEq
) {
388 /* We remove all equalities to be sure all lines are unit vectors */
390 remove_all_equalities(&PT
, &CT
, &CP
, NULL
, C2
->Dimension
,
397 inv
= left_inverse(CP
, &eq
);
403 div
= Matrix_Alloc(inv
->NbRows
-1, inv
->NbColumns
+1);
404 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
405 Vector_Gcd(inv
->p
[i
], inv
->NbColumns
, &tmp
);
406 if (mpz_divisible_p(tmp
,
407 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]))
409 Vector_Copy(inv
->p
[i
], div
->p
[d
], inv
->NbColumns
);
410 value_assign(div
->p
[d
][inv
->NbColumns
],
411 inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
423 POL_ENSURE_VERTICES(C2
);
425 /* Since we have "compressed" the parameters (in case there were
426 * any equalities), the result is independent of the coordinates in the
427 * coordinate subspace spanned by the lines. We can therefore assume
428 * these coordinates are zero and compute the inverse image of the map
429 * from a lower dimensional space that adds zeros in the appropriate
432 M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
-C2
->NbBid
+1);
434 for (int i
= 0; i
< C2
->NbBid
; ++i
) {
435 int j
= First_Non_Zero(C2
->Ray
[i
]+1, C2
->Dimension
);
436 assert(First_Non_Zero(C2
->Ray
[i
]+1+j
+1, C2
->Dimension
-j
-1) == -1);
438 value_set_si(M
->p
[k
+i
][k
], 1);
440 for ( ; k
< C2
->Dimension
-C2
->NbBid
+1; k
++)
441 value_set_si(M
->p
[k
+C2
->NbBid
][k
], 1);
444 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Minv
, &inv
, options
);
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
);
456 for (int i
= 0; i
< C2
->Dimension
/2; ++i
)
457 Vector_Exchange(Q
->p
[i
], Q
->p
[C2
->Dimension
-1-i
], C2
->Dimension
);
460 Matrix
*M
= Matrix_Alloc(C2
->Dimension
+1, C2
->Dimension
+1);
462 int ok
= Matrix_Inverse(U
, M
);
466 apply_transformation(&PT
, &C2
, PT
!= P
, C2
!= C
, M
, Q
, &inv
, options
);
468 gf
= barvinok_series_with_options(PT
, C2
, options
);
472 return new skewed_gen_fun(gf
, inv
, eq
, div
);
475 int main(int argc
, char **argv
)
480 skewed_gen_fun
*gf
= NULL
;
482 int print_solution
= 1;
484 struct arguments options
;
485 static struct argp_child argp_children
[] = {
486 { &barvinok_argp
, 0, 0, 0 },
487 { &verify_argp
, 0, "verification", 1 },
488 { &convert_argp
, 0, "output conversion", 2 },
491 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
492 struct barvinok_options
*bv_options
= barvinok_options_new_with_defaults();
494 options
.verify
.barvinok
= bv_options
;
495 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
498 A
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
501 C
= Constraints2Polyhedron(M
, bv_options
->MaxRays
);
503 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
505 if (options
.verify
.verify
) {
506 verify_options_set_range(&options
.verify
, A
->Dimension
);
507 if (!options
.verbose
)
511 if (print_solution
&& options
.verbose
) {
512 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
513 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
516 if (options
.series
) {
517 gf
= series(A
, C
, bv_options
);
518 if (print_solution
) {
519 gf
->print(stdout
, C
->Dimension
, param_name
);
522 if (options
.function
) {
525 print_evalue(stdout
, EP
, param_name
);
528 EP
= barvinok_enumerate_with_options(A
, C
, bv_options
);
529 evalue_convert(EP
, &options
.convert
, C
->Dimension
,
530 options
.verbose
? param_name
: NULL
);
532 printf("\nSize: %d\n", evalue_size(EP
));
534 if (print_solution
&& !options
.verbose
)
535 print_evalue(stdout
, EP
, param_name
);
537 if (options
.verify
.verify
) {
538 options
.verify
.params
= param_name
;
539 result
= verify(A
, &C
, EP
, gf
, &options
);
545 free_evalue_refs(EP
);
549 if (options
.print_stats
)
550 barvinok_stats_print(options
.verify
.barvinok
->stats
, stdout
);
552 Free_ParamNames(param_name
, C
->Dimension
);
555 barvinok_options_free(bv_options
);