6 #include <NTL/vec_ZZ.h>
7 #include <NTL/mat_ZZ.h>
8 #include <barvinok/barvinok.h>
9 #include <barvinok/evalue.h>
10 #include <barvinok/options.h>
11 #include <barvinok/util.h>
13 #include "conversion.h"
14 #include "decomposer.h"
15 #include "lattice_point.h"
16 #include "reduce_domain.h"
20 #include "evalue_util.h"
21 #include "remove_equalities.h"
26 #undef CS /* for Solaris 10 */
39 #define EMPTINESS_CHECK (BV_OPT_LAST+1)
40 #define NO_REDUCTION (BV_OPT_LAST+2)
41 #define POLYSIGN (BV_OPT_LAST+3)
43 struct argp_option argp_options
[] = {
44 { "emptiness-check", EMPTINESS_CHECK
, "[none|count]", 0 },
45 { "no-reduction", NO_REDUCTION
, 0, 0 },
46 { "polysign", POLYSIGN
, "[cdd|cddf]", 0 },
50 static error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
52 struct lexmin_options
*options
= (struct lexmin_options
*)(state
->input
);
53 struct barvinok_options
*bv_options
= options
->barvinok
;
57 state
->child_inputs
[0] = options
->barvinok
;
58 state
->child_inputs
[1] = &options
->verify
;
59 options
->emptiness_check
= BV_LEXMIN_EMPTINESS_CHECK_SAMPLE
;
61 options
->polysign
= BV_LEXMIN_POLYSIGN_POLYLIB
;
64 if (!strcmp(arg
, "none"))
65 options
->emptiness_check
= BV_LEXMIN_EMPTINESS_CHECK_NONE
;
66 else if (!strcmp(arg
, "count")) {
67 options
->emptiness_check
= BV_LEXMIN_EMPTINESS_CHECK_COUNT
;
68 bv_options
->count_sample_infinite
= 0;
75 if (!strcmp(arg
, "cddf"))
76 options
->polysign
= BV_LEXMIN_POLYSIGN_CDDF
;
77 else if (!strcmp(arg
, "cdd"))
78 options
->polysign
= BV_LEXMIN_POLYSIGN_CDD
;
81 return ARGP_ERR_UNKNOWN
;
86 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
88 static int type_offset(enode
*p
)
90 return p
->type
== fractional
? 1 :
91 p
->type
== flooring
? 1 : 0;
94 void compute_evalue(evalue
*e
, Value
*val
, Value
*res
)
96 double d
= compute_evalue(e
, val
);
101 value_set_double(*res
, d
);
104 struct indicator_term
{
106 int pos
; /* number of rational vertex */
107 int n
; /* number of cone associated to given rational vertex */
111 indicator_term(unsigned dim
, int pos
) {
113 vertex
= new evalue
* [dim
];
118 indicator_term(unsigned dim
, int pos
, int n
) {
119 den
.SetDims(dim
, dim
);
120 vertex
= new evalue
* [dim
];
124 indicator_term(const indicator_term
& src
) {
129 unsigned dim
= den
.NumCols();
130 vertex
= new evalue
* [dim
];
131 for (int i
= 0; i
< dim
; ++i
) {
132 vertex
[i
] = new evalue();
133 value_init(vertex
[i
]->d
);
134 evalue_copy(vertex
[i
], src
.vertex
[i
]);
137 void swap(indicator_term
*other
) {
139 tmp
= sign
; sign
= other
->sign
; other
->sign
= tmp
;
140 tmp
= pos
; pos
= other
->pos
; other
->pos
= tmp
;
141 tmp
= n
; n
= other
->n
; other
->n
= tmp
;
142 mat_ZZ tmp_den
= den
; den
= other
->den
; other
->den
= tmp_den
;
143 unsigned dim
= den
.NumCols();
144 for (int i
= 0; i
< dim
; ++i
) {
145 evalue
*tmp
= vertex
[i
];
146 vertex
[i
] = other
->vertex
[i
];
147 other
->vertex
[i
] = tmp
;
151 unsigned dim
= den
.NumCols();
152 for (int i
= 0; i
< dim
; ++i
) {
153 free_evalue_refs(vertex
[i
]);
158 void print(ostream
& os
, char **p
) const;
159 void substitute(Matrix
*T
);
161 void substitute(evalue
*fract
, evalue
*val
);
162 void substitute(int pos
, evalue
*val
);
163 void reduce_in_domain(Polyhedron
*D
);
164 bool is_opposite(const indicator_term
*neg
) const;
165 vec_ZZ
eval(Value
*val
) const {
167 unsigned dim
= den
.NumCols();
171 for (int i
= 0; i
< dim
; ++i
) {
172 compute_evalue(vertex
[i
], val
, &tmp
);
180 static int evalue_rational_cmp(const evalue
*e1
, const evalue
*e2
)
188 assert(value_notzero_p(e1
->d
));
189 assert(value_notzero_p(e2
->d
));
190 value_multiply(m
, e1
->x
.n
, e2
->d
);
191 value_multiply(m2
, e2
->x
.n
, e1
->d
);
194 else if (value_gt(m
, m2
))
204 static int evalue_cmp(const evalue
*e1
, const evalue
*e2
)
206 if (value_notzero_p(e1
->d
)) {
207 if (value_zero_p(e2
->d
))
209 return evalue_rational_cmp(e1
, e2
);
211 if (value_notzero_p(e2
->d
))
213 if (e1
->x
.p
->type
!= e2
->x
.p
->type
)
214 return e1
->x
.p
->type
- e2
->x
.p
->type
;
215 if (e1
->x
.p
->size
!= e2
->x
.p
->size
)
216 return e1
->x
.p
->size
- e2
->x
.p
->size
;
217 if (e1
->x
.p
->pos
!= e2
->x
.p
->pos
)
218 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
219 assert(e1
->x
.p
->type
== polynomial
||
220 e1
->x
.p
->type
== fractional
||
221 e1
->x
.p
->type
== flooring
);
222 for (int i
= 0; i
< e1
->x
.p
->size
; ++i
) {
223 int s
= evalue_cmp(&e1
->x
.p
->arr
[i
], &e2
->x
.p
->arr
[i
]);
230 void evalue_length(evalue
*e
, int len
[2])
235 while (value_zero_p(e
->d
)) {
236 assert(e
->x
.p
->type
== polynomial
||
237 e
->x
.p
->type
== fractional
||
238 e
->x
.p
->type
== flooring
);
239 if (e
->x
.p
->type
== polynomial
)
243 int offset
= type_offset(e
->x
.p
);
244 assert(e
->x
.p
->size
== offset
+2);
245 e
= &e
->x
.p
->arr
[offset
];
249 static bool it_smaller(const indicator_term
* it1
, const indicator_term
* it2
)
253 int len1
[2], len2
[2];
254 unsigned dim
= it1
->den
.NumCols();
255 for (int i
= 0; i
< dim
; ++i
) {
256 evalue_length(it1
->vertex
[i
], len1
);
257 evalue_length(it2
->vertex
[i
], len2
);
258 if (len1
[0] != len2
[0])
259 return len1
[0] < len2
[0];
260 if (len1
[1] != len2
[1])
261 return len1
[1] < len2
[1];
263 if (it1
->pos
!= it2
->pos
)
264 return it1
->pos
< it2
->pos
;
265 if (it1
->n
!= it2
->n
)
266 return it1
->n
< it2
->n
;
267 int s
= lex_cmp(it1
->den
, it2
->den
);
270 for (int i
= 0; i
< dim
; ++i
) {
271 s
= evalue_cmp(it1
->vertex
[i
], it2
->vertex
[i
]);
275 assert(it1
->sign
!= 0);
276 assert(it2
->sign
!= 0);
277 if (it1
->sign
!= it2
->sign
)
278 return it1
->sign
> 0;
283 static const int requires_resort
;
284 bool operator()(const indicator_term
* it1
, const indicator_term
* it2
) const {
285 return it_smaller(it1
, it2
);
288 const int smaller_it::requires_resort
= 1;
290 struct smaller_it_p
{
291 static const int requires_resort
;
292 bool operator()(const indicator_term
* it1
, const indicator_term
* it2
) const {
296 const int smaller_it_p::requires_resort
= 0;
298 /* Returns true if this and neg are opposite using the knowledge
299 * that they have the same numerator.
300 * In particular, we check that the signs are different and that
301 * the denominator is the same.
303 bool indicator_term::is_opposite(const indicator_term
*neg
) const
305 if (sign
+ neg
->sign
!= 0)
312 void indicator_term::reduce_in_domain(Polyhedron
*D
)
314 for (int k
= 0; k
< den
.NumCols(); ++k
) {
315 reduce_evalue_in_domain(vertex
[k
], D
);
316 if (evalue_range_reduction_in_domain(vertex
[k
], D
))
317 reduce_evalue(vertex
[k
]);
321 void indicator_term::print(ostream
& os
, char **p
) const
323 unsigned dim
= den
.NumCols();
324 unsigned factors
= den
.NumRows();
332 for (int i
= 0; i
< dim
; ++i
) {
335 evalue_print(os
, vertex
[i
], p
);
338 for (int i
= 0; i
< factors
; ++i
) {
339 os
<< " + t" << i
<< "*[";
340 for (int j
= 0; j
< dim
; ++j
) {
347 os
<< " ((" << pos
<< ", " << n
<< ", " << (void*)this << "))";
350 /* Perform the substitution specified by T on the variables.
351 * T has dimension (newdim+nparam+1) x (olddim + nparam + 1).
352 * The substitution is performed as in gen_fun::substitute
354 void indicator_term::substitute(Matrix
*T
)
356 unsigned dim
= den
.NumCols();
357 unsigned nparam
= T
->NbColumns
- dim
- 1;
358 unsigned newdim
= T
->NbRows
- nparam
- 1;
361 matrix2zz(T
, trans
, newdim
, dim
);
362 trans
= transpose(trans
);
364 newvertex
= new evalue
* [newdim
];
367 v
.SetLength(nparam
+1);
370 value_init(factor
.d
);
371 value_set_si(factor
.d
, 1);
372 value_init(factor
.x
.n
);
373 for (int i
= 0; i
< newdim
; ++i
) {
374 values2zz(T
->p
[i
]+dim
, v
, nparam
+1);
375 newvertex
[i
] = multi_monom(v
);
377 for (int j
= 0; j
< dim
; ++j
) {
378 if (value_zero_p(T
->p
[i
][j
]))
382 evalue_copy(&term
, vertex
[j
]);
383 value_assign(factor
.x
.n
, T
->p
[i
][j
]);
384 emul(&factor
, &term
);
385 eadd(&term
, newvertex
[i
]);
386 free_evalue_refs(&term
);
389 free_evalue_refs(&factor
);
390 for (int i
= 0; i
< dim
; ++i
) {
391 free_evalue_refs(vertex
[i
]);
398 static void evalue_add_constant(evalue
*e
, ZZ v
)
403 /* go down to constant term */
404 while (value_zero_p(e
->d
))
405 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)];
408 value_multiply(tmp
, tmp
, e
->d
);
409 value_addto(e
->x
.n
, e
->x
.n
, tmp
);
414 /* Make all powers in denominator lexico-positive */
415 void indicator_term::normalize()
418 extra_vertex
.SetLength(den
.NumCols());
419 for (int r
= 0; r
< den
.NumRows(); ++r
) {
420 for (int k
= 0; k
< den
.NumCols(); ++k
) {
427 extra_vertex
+= den
[r
];
431 for (int k
= 0; k
< extra_vertex
.length(); ++k
)
432 if (extra_vertex
[k
] != 0)
433 evalue_add_constant(vertex
[k
], extra_vertex
[k
]);
436 static void substitute(evalue
*e
, evalue
*fract
, evalue
*val
)
440 for (t
= e
; value_zero_p(t
->d
); t
= &t
->x
.p
->arr
[type_offset(t
->x
.p
)]) {
441 if (t
->x
.p
->type
== fractional
&& eequal(&t
->x
.p
->arr
[0], fract
))
444 if (value_notzero_p(t
->d
))
447 free_evalue_refs(&t
->x
.p
->arr
[0]);
448 evalue
*term
= &t
->x
.p
->arr
[2];
455 free_evalue_refs(term
);
461 void indicator_term::substitute(evalue
*fract
, evalue
*val
)
463 unsigned dim
= den
.NumCols();
464 for (int i
= 0; i
< dim
; ++i
) {
465 ::substitute(vertex
[i
], fract
, val
);
469 static void substitute(evalue
*e
, int pos
, evalue
*val
)
473 for (t
= e
; value_zero_p(t
->d
); t
= &t
->x
.p
->arr
[type_offset(t
->x
.p
)]) {
474 if (t
->x
.p
->type
== polynomial
&& t
->x
.p
->pos
== pos
)
477 if (value_notzero_p(t
->d
))
480 evalue
*term
= &t
->x
.p
->arr
[1];
487 free_evalue_refs(term
);
493 void indicator_term::substitute(int pos
, evalue
*val
)
495 unsigned dim
= den
.NumCols();
496 for (int i
= 0; i
< dim
; ++i
) {
497 ::substitute(vertex
[i
], pos
, val
);
501 struct indicator_constructor
: public signed_cone_consumer
,
502 public vertex_decomposer
{
504 vector
<indicator_term
*> *terms
;
505 Matrix
*T
; /* Transformation to original space */
506 Param_Polyhedron
*PP
;
510 indicator_constructor(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
,
512 vertex_decomposer(P
, PP
->nbV
, *this), T(T
), PP(PP
) {
513 vertex
.SetLength(dim
);
514 terms
= new vector
<indicator_term
*>[nbV
];
516 ~indicator_constructor() {
517 for (int i
= 0; i
< nbV
; ++i
)
518 for (int j
= 0; j
< terms
[i
].size(); ++j
)
522 void substitute(Matrix
*T
);
524 void print(ostream
& os
, char **p
);
526 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
527 void decompose_at_vertex(Param_Vertices
*V
, int _i
,
528 barvinok_options
*options
) {
531 vertex_decomposer::decompose_at_vertex(V
, _i
, options
);
535 void indicator_constructor::handle(const signed_cone
& sc
, barvinok_options
*options
)
538 unsigned dim
= vertex
.length();
540 assert(sc
.rays
.NumRows() == dim
);
542 indicator_term
*term
= new indicator_term(dim
, pos
, n
++);
543 term
->sign
= sc
.sign
;
544 terms
[vert
].push_back(term
);
546 lattice_point(V
, sc
.rays
, vertex
, term
->vertex
, options
);
549 for (int r
= 0; r
< dim
; ++r
) {
550 for (int j
= 0; j
< dim
; ++j
) {
551 if (term
->den
[r
][j
] == 0)
553 if (term
->den
[r
][j
] > 0)
555 term
->sign
= -term
->sign
;
556 term
->den
[r
] = -term
->den
[r
];
557 vertex
+= term
->den
[r
];
562 for (int i
= 0; i
< dim
; ++i
) {
563 if (!term
->vertex
[i
]) {
564 term
->vertex
[i
] = new evalue();
565 value_init(term
->vertex
[i
]->d
);
566 value_init(term
->vertex
[i
]->x
.n
);
567 zz2value(vertex
[i
], term
->vertex
[i
]->x
.n
);
568 value_set_si(term
->vertex
[i
]->d
, 1);
573 evalue_add_constant(term
->vertex
[i
], vertex
[i
]);
581 lex_order_rows(term
->den
);
584 void indicator_constructor::print(ostream
& os
, char **p
)
586 for (int i
= 0; i
< nbV
; ++i
)
587 for (int j
= 0; j
< terms
[i
].size(); ++j
) {
588 os
<< "i: " << i
<< ", j: " << j
<< endl
;
589 terms
[i
][j
]->print(os
, p
);
594 void indicator_constructor::normalize()
596 for (int i
= 0; i
< nbV
; ++i
)
597 for (int j
= 0; j
< terms
[i
].size(); ++j
) {
599 vertex
.SetLength(terms
[i
][j
]->den
.NumCols());
600 for (int r
= 0; r
< terms
[i
][j
]->den
.NumRows(); ++r
) {
601 for (int k
= 0; k
< terms
[i
][j
]->den
.NumCols(); ++k
) {
602 if (terms
[i
][j
]->den
[r
][k
] == 0)
604 if (terms
[i
][j
]->den
[r
][k
] > 0)
606 terms
[i
][j
]->sign
= -terms
[i
][j
]->sign
;
607 terms
[i
][j
]->den
[r
] = -terms
[i
][j
]->den
[r
];
608 vertex
+= terms
[i
][j
]->den
[r
];
612 lex_order_rows(terms
[i
][j
]->den
);
613 for (int k
= 0; k
< vertex
.length(); ++k
)
615 evalue_add_constant(terms
[i
][j
]->vertex
[k
], vertex
[k
]);
619 struct order_cache_el
{
621 order_cache_el
copy() const {
623 for (int i
= 0; i
< e
.size(); ++i
) {
624 evalue
*c
= new evalue
;
626 evalue_copy(c
, e
[i
]);
632 for (int i
= 0; i
< e
.size(); ++i
) {
633 free_evalue_refs(e
[i
]);
640 evalue_set_si(&mone
, -1, 1);
641 for (int i
= 0; i
< e
.size(); ++i
)
643 free_evalue_refs(&mone
);
645 void print(ostream
& os
, char **p
);
648 void order_cache_el::print(ostream
& os
, char **p
)
651 for (int i
= 0; i
< e
.size(); ++i
) {
654 evalue_print(os
, e
[i
], p
);
660 vector
<order_cache_el
> lt
;
661 vector
<order_cache_el
> le
;
662 vector
<order_cache_el
> unknown
;
664 void clear_transients() {
665 for (int i
= 0; i
< le
.size(); ++i
)
667 for (int i
= 0; i
< unknown
.size(); ++i
)
674 for (int i
= 0; i
< lt
.size(); ++i
)
678 void add(order_cache_el
& cache_el
, order_sign sign
);
679 order_sign
check_lt(vector
<order_cache_el
>* list
,
680 const indicator_term
*a
, const indicator_term
*b
,
681 order_cache_el
& cache_el
);
682 order_sign
check_lt(const indicator_term
*a
, const indicator_term
*b
,
683 order_cache_el
& cache_el
);
684 order_sign
check_direct(const indicator_term
*a
, const indicator_term
*b
,
685 order_cache_el
& cache_el
);
686 order_sign
check(const indicator_term
*a
, const indicator_term
*b
,
687 order_cache_el
& cache_el
);
688 void copy(const order_cache
& cache
);
689 void print(ostream
& os
, char **p
);
692 void order_cache::copy(const order_cache
& cache
)
694 for (int i
= 0; i
< cache
.lt
.size(); ++i
) {
695 order_cache_el n
= cache
.lt
[i
].copy();
700 void order_cache::add(order_cache_el
& cache_el
, order_sign sign
)
702 if (sign
== order_lt
) {
703 lt
.push_back(cache_el
);
704 } else if (sign
== order_gt
) {
706 lt
.push_back(cache_el
);
707 } else if (sign
== order_le
) {
708 le
.push_back(cache_el
);
709 } else if (sign
== order_ge
) {
711 le
.push_back(cache_el
);
712 } else if (sign
== order_unknown
) {
713 unknown
.push_back(cache_el
);
715 assert(sign
== order_eq
);
722 static evalue
*ediff(const evalue
*a
, const evalue
*b
)
726 evalue_set_si(&mone
, -1, 1);
727 evalue
*diff
= new evalue
;
729 evalue_copy(diff
, b
);
733 free_evalue_refs(&mone
);
737 static bool evalue_first_difference(const evalue
*e1
, const evalue
*e2
,
738 const evalue
**d1
, const evalue
**d2
)
743 if (value_ne(e1
->d
, e2
->d
))
746 if (value_notzero_p(e1
->d
)) {
747 if (value_eq(e1
->x
.n
, e2
->x
.n
))
751 if (e1
->x
.p
->type
!= e2
->x
.p
->type
)
753 if (e1
->x
.p
->size
!= e2
->x
.p
->size
)
755 if (e1
->x
.p
->pos
!= e2
->x
.p
->pos
)
758 assert(e1
->x
.p
->type
== polynomial
||
759 e1
->x
.p
->type
== fractional
||
760 e1
->x
.p
->type
== flooring
);
761 int offset
= type_offset(e1
->x
.p
);
762 assert(e1
->x
.p
->size
== offset
+2);
763 for (int i
= 0; i
< e1
->x
.p
->size
; ++i
)
764 if (i
!= type_offset(e1
->x
.p
) &&
765 !eequal(&e1
->x
.p
->arr
[i
], &e2
->x
.p
->arr
[i
]))
768 return evalue_first_difference(&e1
->x
.p
->arr
[offset
],
769 &e2
->x
.p
->arr
[offset
], d1
, d2
);
772 static order_sign
evalue_diff_constant_sign(const evalue
*e1
, const evalue
*e2
)
774 if (!evalue_first_difference(e1
, e2
, &e1
, &e2
))
776 if (value_zero_p(e1
->d
) || value_zero_p(e2
->d
))
777 return order_undefined
;
778 int s
= evalue_rational_cmp(e1
, e2
);
787 order_sign
order_cache::check_lt(vector
<order_cache_el
>* list
,
788 const indicator_term
*a
, const indicator_term
*b
,
789 order_cache_el
& cache_el
)
791 order_sign sign
= order_undefined
;
792 for (int i
= 0; i
< list
->size(); ++i
) {
794 for (j
= cache_el
.e
.size(); j
< (*list
)[i
].e
.size(); ++j
)
795 cache_el
.e
.push_back(ediff(a
->vertex
[j
], b
->vertex
[j
]));
796 for (j
= 0; j
< (*list
)[i
].e
.size(); ++j
) {
797 order_sign diff_sign
;
798 diff_sign
= evalue_diff_constant_sign((*list
)[i
].e
[j
], cache_el
.e
[j
]);
799 if (diff_sign
== order_gt
) {
802 } else if (diff_sign
== order_lt
)
804 else if (diff_sign
== order_undefined
)
807 assert(diff_sign
== order_eq
);
809 if (j
== (*list
)[i
].e
.size())
810 sign
= list
== <
? order_lt
: order_le
;
811 if (sign
!= order_undefined
)
817 order_sign
order_cache::check_direct(const indicator_term
*a
,
818 const indicator_term
*b
,
819 order_cache_el
& cache_el
)
821 order_sign sign
= check_lt(<
, a
, b
, cache_el
);
822 if (sign
!= order_undefined
)
824 sign
= check_lt(&le
, a
, b
, cache_el
);
825 if (sign
!= order_undefined
)
828 for (int i
= 0; i
< unknown
.size(); ++i
) {
830 for (j
= cache_el
.e
.size(); j
< unknown
[i
].e
.size(); ++j
)
831 cache_el
.e
.push_back(ediff(a
->vertex
[j
], b
->vertex
[j
]));
832 for (j
= 0; j
< unknown
[i
].e
.size(); ++j
) {
833 if (!eequal(unknown
[i
].e
[j
], cache_el
.e
[j
]))
836 if (j
== unknown
[i
].e
.size()) {
837 sign
= order_unknown
;
844 order_sign
order_cache::check(const indicator_term
*a
, const indicator_term
*b
,
845 order_cache_el
& cache_el
)
847 order_sign sign
= check_direct(a
, b
, cache_el
);
848 if (sign
!= order_undefined
)
850 int size
= cache_el
.e
.size();
852 sign
= check_direct(a
, b
, cache_el
);
854 assert(cache_el
.e
.size() == size
);
855 if (sign
== order_undefined
)
857 if (sign
== order_lt
)
859 else if (sign
== order_le
)
862 assert(sign
== order_unknown
);
868 struct partial_order
{
871 std::set
<const indicator_term
*, smaller_it
> head
;
872 map
<const indicator_term
*, int, smaller_it
> pred
;
873 map
<const indicator_term
*, vector
<const indicator_term
* >, smaller_it
> lt
;
874 map
<const indicator_term
*, vector
<const indicator_term
* >, smaller_it
> le
;
875 map
<const indicator_term
*, vector
<const indicator_term
* >, smaller_it
> eq
;
877 map
<const indicator_term
*, vector
<const indicator_term
* >, smaller_it
> pending
;
881 partial_order(indicator
*ind
) : ind(ind
) {}
882 void copy(const partial_order
& order
,
883 map
< const indicator_term
*, indicator_term
* > old2new
);
885 map
<const indicator_term
*, vector
<const indicator_term
* > >::iterator i
;
886 map
<const indicator_term
*, int >::iterator j
;
887 std::set
<const indicator_term
*>::iterator k
;
889 if (head
.key_comp().requires_resort
) {
890 typeof(head
) new_head
;
891 for (k
= head
.begin(); k
!= head
.end(); ++k
)
897 if (pred
.key_comp().requires_resort
) {
898 typeof(pred
) new_pred
;
899 for (j
= pred
.begin(); j
!= pred
.end(); ++j
)
900 new_pred
[(*j
).first
] = (*j
).second
;
905 if (lt
.key_comp().requires_resort
) {
907 for (i
= lt
.begin(); i
!= lt
.end(); ++i
)
908 m
[(*i
).first
] = (*i
).second
;
913 if (le
.key_comp().requires_resort
) {
915 for (i
= le
.begin(); i
!= le
.end(); ++i
)
916 m
[(*i
).first
] = (*i
).second
;
921 if (eq
.key_comp().requires_resort
) {
923 for (i
= eq
.begin(); i
!= eq
.end(); ++i
)
924 m
[(*i
).first
] = (*i
).second
;
929 if (pending
.key_comp().requires_resort
) {
931 for (i
= pending
.begin(); i
!= pending
.end(); ++i
)
932 m
[(*i
).first
] = (*i
).second
;
938 order_sign
compare(const indicator_term
*a
, const indicator_term
*b
);
939 void set_equal(const indicator_term
*a
, const indicator_term
*b
);
940 void unset_le(const indicator_term
*a
, const indicator_term
*b
);
941 void dec_pred(const indicator_term
*it
) {
942 if (--pred
[it
] == 0) {
947 void inc_pred(const indicator_term
*it
) {
948 if (head
.find(it
) != head
.end())
953 bool compared(const indicator_term
* a
, const indicator_term
* b
);
954 void add(const indicator_term
* it
, std::set
<const indicator_term
*> *filter
);
955 void remove(const indicator_term
* it
);
957 void print(ostream
& os
, char **p
);
959 /* replace references to orig to references to replacement */
960 void replace(const indicator_term
* orig
, indicator_term
* replacement
);
961 void sanity_check() const;
964 /* We actually replace the contents of orig by that of replacement,
965 * but we have to be careful since replacing the content changes
966 * the order of orig in the maps.
968 void partial_order::replace(const indicator_term
* orig
, indicator_term
* replacement
)
970 std::set
<const indicator_term
*>::iterator k
;
972 bool is_head
= k
!= head
.end();
977 orig_pred
= pred
[orig
];
980 vector
<const indicator_term
* > orig_lt
;
981 vector
<const indicator_term
* > orig_le
;
982 vector
<const indicator_term
* > orig_eq
;
983 vector
<const indicator_term
* > orig_pending
;
984 map
<const indicator_term
*, vector
<const indicator_term
* > >::iterator i
;
985 bool in_lt
= ((i
= lt
.find(orig
)) != lt
.end());
987 orig_lt
= (*i
).second
;
990 bool in_le
= ((i
= le
.find(orig
)) != le
.end());
992 orig_le
= (*i
).second
;
995 bool in_eq
= ((i
= eq
.find(orig
)) != eq
.end());
997 orig_eq
= (*i
).second
;
1000 bool in_pending
= ((i
= pending
.find(orig
)) != pending
.end());
1002 orig_pending
= (*i
).second
;
1003 pending
.erase(orig
);
1005 indicator_term
*old
= const_cast<indicator_term
*>(orig
);
1006 old
->swap(replacement
);
1010 pred
[old
] = orig_pred
;
1018 pending
[old
] = orig_pending
;
1021 void partial_order::unset_le(const indicator_term
*a
, const indicator_term
*b
)
1023 vector
<const indicator_term
*>::iterator i
;
1024 i
= find(le
[a
].begin(), le
[a
].end(), b
);
1026 if (le
[a
].size() == 0)
1029 i
= find(pending
[a
].begin(), pending
[a
].end(), b
);
1030 if (i
!= pending
[a
].end())
1031 pending
[a
].erase(i
);
1034 void partial_order::set_equal(const indicator_term
*a
, const indicator_term
*b
)
1036 if (eq
[a
].size() == 0)
1038 if (eq
[b
].size() == 0)
1043 if (pred
.key_comp()(b
, a
)) {
1044 const indicator_term
*c
= a
;
1049 const indicator_term
*base
= a
;
1051 map
<const indicator_term
*, vector
<const indicator_term
* > >::iterator i
;
1053 for (int j
= 0; j
< eq
[b
].size(); ++j
) {
1054 eq
[base
].push_back(eq
[b
][j
]);
1055 eq
[eq
[b
][j
]][0] = base
;
1060 if (i
!= lt
.end()) {
1061 for (int j
= 0; j
< lt
[b
].size(); ++j
) {
1062 if (find(eq
[base
].begin(), eq
[base
].end(), lt
[b
][j
]) != eq
[base
].end())
1064 else if (find(lt
[base
].begin(), lt
[base
].end(), lt
[b
][j
])
1068 lt
[base
].push_back(lt
[b
][j
]);
1074 if (i
!= le
.end()) {
1075 for (int j
= 0; j
< le
[b
].size(); ++j
) {
1076 if (find(eq
[base
].begin(), eq
[base
].end(), le
[b
][j
]) != eq
[base
].end())
1078 else if (find(le
[base
].begin(), le
[base
].end(), le
[b
][j
])
1082 le
[base
].push_back(le
[b
][j
]);
1087 i
= pending
.find(base
);
1088 if (i
!= pending
.end()) {
1089 vector
<const indicator_term
* > old
= pending
[base
];
1090 pending
[base
].clear();
1091 for (int j
= 0; j
< old
.size(); ++j
) {
1092 if (find(eq
[base
].begin(), eq
[base
].end(), old
[j
]) == eq
[base
].end())
1093 pending
[base
].push_back(old
[j
]);
1097 i
= pending
.find(b
);
1098 if (i
!= pending
.end()) {
1099 for (int j
= 0; j
< pending
[b
].size(); ++j
) {
1100 if (find(eq
[base
].begin(), eq
[base
].end(), pending
[b
][j
]) == eq
[base
].end())
1101 pending
[base
].push_back(pending
[b
][j
]);
1107 void partial_order::copy(const partial_order
& order
,
1108 map
< const indicator_term
*, indicator_term
* > old2new
)
1110 cache
.copy(order
.cache
);
1112 map
<const indicator_term
*, vector
<const indicator_term
* > >::const_iterator i
;
1113 map
<const indicator_term
*, int >::const_iterator j
;
1114 std::set
<const indicator_term
*>::const_iterator k
;
1116 for (k
= order
.head
.begin(); k
!= order
.head
.end(); ++k
)
1117 head
.insert(old2new
[*k
]);
1119 for (j
= order
.pred
.begin(); j
!= order
.pred
.end(); ++j
)
1120 pred
[old2new
[(*j
).first
]] = (*j
).second
;
1122 for (i
= order
.lt
.begin(); i
!= order
.lt
.end(); ++i
) {
1123 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1124 lt
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1126 for (i
= order
.le
.begin(); i
!= order
.le
.end(); ++i
) {
1127 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1128 le
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1130 for (i
= order
.eq
.begin(); i
!= order
.eq
.end(); ++i
) {
1131 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1132 eq
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1134 for (i
= order
.pending
.begin(); i
!= order
.pending
.end(); ++i
) {
1135 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1136 pending
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1142 vector
<evalue
*> max
;
1144 void print(ostream
& os
, char **p
, barvinok_options
*options
) const;
1145 void substitute(Matrix
*T
, barvinok_options
*options
);
1146 Vector
*eval(Value
*val
, unsigned MaxRays
) const;
1149 for (int i
= 0; i
< max
.size(); ++i
) {
1150 free_evalue_refs(max
[i
]);
1158 * Project on first dim dimensions
1160 Polyhedron
* Polyhedron_Project_Initial(Polyhedron
*P
, int dim
)
1166 if (P
->Dimension
== dim
)
1167 return Polyhedron_Copy(P
);
1169 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
1170 for (i
= 0; i
< dim
; ++i
)
1171 value_set_si(T
->p
[i
][i
], 1);
1172 value_set_si(T
->p
[dim
][P
->Dimension
], 1);
1173 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
1179 vector
<indicator_term
*> term
;
1180 indicator_constructor
& ic
;
1181 partial_order order
;
1185 lexmin_options
*options
;
1186 vector
<evalue
*> substitutions
;
1188 indicator(indicator_constructor
& ic
, Param_Domain
*PD
, EDomain
*D
,
1189 lexmin_options
*options
) :
1190 ic(ic
), PD(PD
), D(D
), order(this), options(options
), P(NULL
) {}
1191 indicator(const indicator
& ind
, EDomain
*D
) :
1192 ic(ind
.ic
), PD(ind
.PD
), D(NULL
), order(this), options(ind
.options
),
1193 P(Polyhedron_Copy(ind
.P
)) {
1194 map
< const indicator_term
*, indicator_term
* > old2new
;
1195 for (int i
= 0; i
< ind
.term
.size(); ++i
) {
1196 indicator_term
*it
= new indicator_term(*ind
.term
[i
]);
1197 old2new
[ind
.term
[i
]] = it
;
1200 order
.copy(ind
.order
, old2new
);
1204 for (int i
= 0; i
< term
.size(); ++i
)
1212 void set_domain(EDomain
*D
) {
1213 order
.cache
.clear_transients();
1217 int nparam
= ic
.P
->Dimension
- ic
.vertex
.length();
1218 if (options
->reduce
) {
1219 Polyhedron
*Q
= Polyhedron_Project_Initial(D
->D
, nparam
);
1220 Q
= DomainConstraintSimplify(Q
, options
->barvinok
->MaxRays
);
1221 if (!P
|| !PolyhedronIncludes(Q
, P
))
1222 reduce_in_domain(Q
);
1230 void add(const indicator_term
* it
);
1231 void remove(const indicator_term
* it
);
1232 void remove_initial_rational_vertices();
1233 void expand_rational_vertex(const indicator_term
*initial
);
1235 void print(ostream
& os
, char **p
);
1237 void peel(int i
, int j
);
1238 void combine(const indicator_term
*a
, const indicator_term
*b
);
1239 void add_substitution(evalue
*equation
);
1240 void perform_pending_substitutions();
1241 void reduce_in_domain(Polyhedron
*D
);
1242 bool handle_equal_numerators(const indicator_term
*base
);
1244 max_term
* create_max_term(const indicator_term
*it
);
1246 void substitute(evalue
*equation
);
1249 void partial_order::sanity_check() const
1251 map
<const indicator_term
*, vector
<const indicator_term
* > >::const_iterator i
;
1252 map
<const indicator_term
*, vector
<const indicator_term
* > >::const_iterator prev
;
1253 map
<const indicator_term
*, vector
<const indicator_term
* > >::const_iterator l
;
1254 map
<const indicator_term
*, int >::const_iterator k
, prev_k
;
1256 for (k
= pred
.begin(); k
!= pred
.end(); prev_k
= k
, ++k
)
1257 if (k
!= pred
.begin())
1258 assert(pred
.key_comp()((*prev_k
).first
, (*k
).first
));
1259 for (i
= lt
.begin(); i
!= lt
.end(); prev
= i
, ++i
) {
1262 i_v
= (*i
).first
->eval(ind
->D
->sample
->p
);
1263 if (i
!= lt
.begin())
1264 assert(lt
.key_comp()((*prev
).first
, (*i
).first
));
1265 l
= eq
.find((*i
).first
);
1267 assert((*l
).second
.size() > 1);
1268 assert(head
.find((*i
).first
) != head
.end() ||
1269 pred
.find((*i
).first
) != pred
.end());
1270 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1271 k
= pred
.find((*i
).second
[j
]);
1272 assert(k
!= pred
.end());
1273 assert((*k
).second
!= 0);
1274 if ((*i
).first
->sign
!= 0 &&
1275 (*i
).second
[j
]->sign
!= 0 && ind
->D
->sample
) {
1276 vec_ZZ j_v
= (*i
).second
[j
]->eval(ind
->D
->sample
->p
);
1277 assert(lex_cmp(i_v
, j_v
) < 0);
1281 for (i
= le
.begin(); i
!= le
.end(); ++i
) {
1282 assert((*i
).second
.size() > 0);
1283 assert(head
.find((*i
).first
) != head
.end() ||
1284 pred
.find((*i
).first
) != pred
.end());
1285 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1286 k
= pred
.find((*i
).second
[j
]);
1287 assert(k
!= pred
.end());
1288 assert((*k
).second
!= 0);
1291 for (i
= eq
.begin(); i
!= eq
.end(); ++i
) {
1292 assert(head
.find((*i
).first
) != head
.end() ||
1293 pred
.find((*i
).first
) != pred
.end());
1294 assert((*i
).second
.size() >= 1);
1296 for (i
= pending
.begin(); i
!= pending
.end(); ++i
) {
1297 assert(head
.find((*i
).first
) != head
.end() ||
1298 pred
.find((*i
).first
) != pred
.end());
1299 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1300 assert(head
.find((*i
).second
[j
]) != head
.end() ||
1301 pred
.find((*i
).second
[j
]) != pred
.end());
1305 max_term
* indicator::create_max_term(const indicator_term
*it
)
1307 int dim
= it
->den
.NumCols();
1308 int nparam
= ic
.P
->Dimension
- ic
.vertex
.length();
1309 max_term
*maximum
= new max_term
;
1310 maximum
->domain
= new EDomain(D
);
1311 for (int j
= 0; j
< dim
; ++j
) {
1312 evalue
*E
= new evalue
;
1314 evalue_copy(E
, it
->vertex
[j
]);
1315 if (evalue_frac2floor_in_domain3(E
, D
->D
, 0))
1317 maximum
->max
.push_back(E
);
1322 static order_sign
evalue_sign(evalue
*diff
, EDomain
*D
, lexmin_options
*options
)
1324 order_sign sign
= order_eq
;
1327 evalue_set_si(&mone
, -1, 1);
1328 int len
= 1 + D
->D
->Dimension
+ 1;
1329 Vector
*c
= Vector_Alloc(len
);
1330 Matrix
*T
= Matrix_Alloc(2, len
-1);
1332 int fract
= evalue2constraint(D
, diff
, c
->p
, len
);
1333 Vector_Copy(c
->p
+1, T
->p
[0], len
-1);
1334 value_assign(T
->p
[1][len
-2], c
->p
[0]);
1336 order_sign upper_sign
= polyhedron_affine_sign(D
->D
, T
, options
);
1337 if (upper_sign
== order_lt
|| !fract
)
1341 evalue2constraint(D
, diff
, c
->p
, len
);
1343 Vector_Copy(c
->p
+1, T
->p
[0], len
-1);
1344 value_assign(T
->p
[1][len
-2], c
->p
[0]);
1346 order_sign neg_lower_sign
= polyhedron_affine_sign(D
->D
, T
, options
);
1348 if (neg_lower_sign
== order_lt
)
1350 else if (neg_lower_sign
== order_eq
|| neg_lower_sign
== order_le
) {
1351 if (upper_sign
== order_eq
|| upper_sign
== order_le
)
1356 if (upper_sign
== order_lt
|| upper_sign
== order_le
||
1357 upper_sign
== order_eq
)
1360 sign
= order_unknown
;
1366 free_evalue_refs(&mone
);
1371 /* An auxiliary class that keeps a reference to an evalue
1372 * and frees it when it goes out of scope.
1374 struct temp_evalue
{
1376 temp_evalue() : E(NULL
) {}
1377 temp_evalue(evalue
*e
) : E(e
) {}
1378 operator evalue
* () const { return E
; }
1379 evalue
*operator=(evalue
*e
) {
1381 free_evalue_refs(E
);
1389 free_evalue_refs(E
);
1395 static void substitute(vector
<indicator_term
*>& term
, evalue
*equation
)
1397 evalue
*fract
= NULL
;
1398 evalue
*val
= new evalue
;
1400 evalue_copy(val
, equation
);
1403 value_init(factor
.d
);
1404 value_init(factor
.x
.n
);
1407 for (e
= val
; value_zero_p(e
->d
) && e
->x
.p
->type
!= fractional
;
1408 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)])
1411 if (value_zero_p(e
->d
) && e
->x
.p
->type
== fractional
)
1412 fract
= &e
->x
.p
->arr
[0];
1414 for (e
= val
; value_zero_p(e
->d
) && e
->x
.p
->type
!= polynomial
;
1415 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)])
1417 assert(value_zero_p(e
->d
) && e
->x
.p
->type
== polynomial
);
1420 int offset
= type_offset(e
->x
.p
);
1422 assert(value_notzero_p(e
->x
.p
->arr
[offset
+1].d
));
1423 assert(value_notzero_p(e
->x
.p
->arr
[offset
+1].x
.n
));
1424 if (value_neg_p(e
->x
.p
->arr
[offset
+1].x
.n
)) {
1425 value_oppose(factor
.d
, e
->x
.p
->arr
[offset
+1].x
.n
);
1426 value_assign(factor
.x
.n
, e
->x
.p
->arr
[offset
+1].d
);
1428 value_assign(factor
.d
, e
->x
.p
->arr
[offset
+1].x
.n
);
1429 value_oppose(factor
.x
.n
, e
->x
.p
->arr
[offset
+1].d
);
1432 free_evalue_refs(&e
->x
.p
->arr
[offset
+1]);
1435 *e
= e
->x
.p
->arr
[offset
];
1440 for (int i
= 0; i
< term
.size(); ++i
)
1441 term
[i
]->substitute(fract
, val
);
1443 free_evalue_refs(&p
->arr
[0]);
1445 for (int i
= 0; i
< term
.size(); ++i
)
1446 term
[i
]->substitute(p
->pos
, val
);
1449 free_evalue_refs(&factor
);
1450 free_evalue_refs(val
);
1456 order_sign
partial_order::compare(const indicator_term
*a
, const indicator_term
*b
)
1458 unsigned dim
= a
->den
.NumCols();
1459 order_sign sign
= order_eq
;
1460 EDomain
*D
= ind
->D
;
1461 unsigned MaxRays
= ind
->options
->barvinok
->MaxRays
;
1462 bool rational
= a
->sign
== 0 || b
->sign
== 0;
1464 order_sign cached_sign
= order_eq
;
1465 for (int k
= 0; k
< dim
; ++k
) {
1466 cached_sign
= evalue_diff_constant_sign(a
->vertex
[k
], b
->vertex
[k
]);
1467 if (cached_sign
!= order_eq
)
1470 if (cached_sign
!= order_undefined
)
1473 order_cache_el cache_el
;
1474 cached_sign
= order_undefined
;
1476 cached_sign
= cache
.check(a
, b
, cache_el
);
1477 if (cached_sign
!= order_undefined
) {
1482 if (rational
&& POL_ISSET(ind
->options
->barvinok
->MaxRays
, POL_INTEGER
)) {
1483 ind
->options
->barvinok
->MaxRays
&= ~POL_INTEGER
;
1484 if (ind
->options
->barvinok
->MaxRays
)
1485 ind
->options
->barvinok
->MaxRays
|= POL_HIGH_BIT
;
1490 vector
<indicator_term
*> term
;
1492 for (int k
= 0; k
< dim
; ++k
) {
1493 /* compute a->vertex[k] - b->vertex[k] */
1495 if (cache_el
.e
.size() <= k
) {
1496 diff
= ediff(a
->vertex
[k
], b
->vertex
[k
]);
1497 cache_el
.e
.push_back(diff
);
1499 diff
= cache_el
.e
[k
];
1502 tdiff
= diff
= ediff(term
[0]->vertex
[k
], term
[1]->vertex
[k
]);
1503 order_sign diff_sign
;
1505 diff_sign
= order_undefined
;
1506 else if (eequal(a
->vertex
[k
], b
->vertex
[k
]))
1507 diff_sign
= order_eq
;
1509 diff_sign
= evalue_sign(diff
, D
, ind
->options
);
1511 if (diff_sign
== order_undefined
) {
1512 assert(sign
== order_le
|| sign
== order_ge
);
1513 if (sign
== order_le
)
1519 if (diff_sign
== order_lt
) {
1520 if (sign
== order_eq
|| sign
== order_le
)
1523 sign
= order_unknown
;
1526 if (diff_sign
== order_gt
) {
1527 if (sign
== order_eq
|| sign
== order_ge
)
1530 sign
= order_unknown
;
1533 if (diff_sign
== order_eq
) {
1534 if (D
== ind
->D
&& term
.size() == 0 && !rational
&&
1535 !EVALUE_IS_ZERO(*diff
))
1536 ind
->add_substitution(diff
);
1539 if ((diff_sign
== order_unknown
) ||
1540 ((diff_sign
== order_lt
|| diff_sign
== order_le
) && sign
== order_ge
) ||
1541 ((diff_sign
== order_gt
|| diff_sign
== order_ge
) && sign
== order_le
)) {
1542 sign
= order_unknown
;
1549 term
.push_back(new indicator_term(*a
));
1550 term
.push_back(new indicator_term(*b
));
1552 substitute(term
, diff
);
1556 cache
.add(cache_el
, sign
);
1560 if (D
&& D
!= ind
->D
)
1568 ind
->options
->barvinok
->MaxRays
= MaxRays
;
1572 bool partial_order::compared(const indicator_term
* a
, const indicator_term
* b
)
1574 map
<const indicator_term
*, vector
<const indicator_term
* > >::iterator j
;
1577 if (j
!= lt
.end() && find(lt
[a
].begin(), lt
[a
].end(), b
) != lt
[a
].end())
1581 if (j
!= le
.end() && find(le
[a
].begin(), le
[a
].end(), b
) != le
[a
].end())
1587 void partial_order::add(const indicator_term
* it
,
1588 std::set
<const indicator_term
*> *filter
)
1590 if (eq
.find(it
) != eq
.end() && eq
[it
].size() == 1)
1593 typeof(head
) head_copy(head
);
1598 std::set
<const indicator_term
*>::iterator i
;
1599 for (i
= head_copy
.begin(); i
!= head_copy
.end(); ++i
) {
1602 if (eq
.find(*i
) != eq
.end() && eq
[*i
].size() == 1)
1605 if (filter
->find(*i
) == filter
->end())
1607 if (compared(*i
, it
))
1610 order_sign sign
= compare(it
, *i
);
1611 if (sign
== order_lt
) {
1612 lt
[it
].push_back(*i
);
1614 } else if (sign
== order_le
) {
1615 le
[it
].push_back(*i
);
1617 } else if (sign
== order_eq
) {
1620 } else if (sign
== order_gt
) {
1621 pending
[*i
].push_back(it
);
1622 lt
[*i
].push_back(it
);
1624 } else if (sign
== order_ge
) {
1625 pending
[*i
].push_back(it
);
1626 le
[*i
].push_back(it
);
1632 void partial_order::remove(const indicator_term
* it
)
1634 std::set
<const indicator_term
*> filter
;
1635 map
<const indicator_term
*, vector
<const indicator_term
* > >::iterator i
;
1637 assert(head
.find(it
) != head
.end());
1640 if (i
!= eq
.end()) {
1641 assert(eq
[it
].size() >= 1);
1642 const indicator_term
*base
;
1643 if (eq
[it
].size() == 1) {
1647 vector
<const indicator_term
* >::iterator j
;
1648 j
= find(eq
[base
].begin(), eq
[base
].end(), it
);
1649 assert(j
!= eq
[base
].end());
1652 /* "it" may no longer be the smallest, since the order
1653 * structure may have been copied from another one.
1655 sort(eq
[it
].begin()+1, eq
[it
].end(), pred
.key_comp());
1656 assert(eq
[it
][0] == it
);
1657 eq
[it
].erase(eq
[it
].begin());
1662 for (int j
= 1; j
< eq
[base
].size(); ++j
)
1663 eq
[eq
[base
][j
]][0] = base
;
1666 if (i
!= lt
.end()) {
1672 if (i
!= le
.end()) {
1677 i
= pending
.find(it
);
1678 if (i
!= pending
.end()) {
1679 pending
[base
] = pending
[it
];
1684 if (eq
[base
].size() == 1)
1693 if (i
!= lt
.end()) {
1694 for (int j
= 0; j
< lt
[it
].size(); ++j
) {
1695 filter
.insert(lt
[it
][j
]);
1696 dec_pred(lt
[it
][j
]);
1702 if (i
!= le
.end()) {
1703 for (int j
= 0; j
< le
[it
].size(); ++j
) {
1704 filter
.insert(le
[it
][j
]);
1705 dec_pred(le
[it
][j
]);
1712 i
= pending
.find(it
);
1713 if (i
!= pending
.end()) {
1714 vector
<const indicator_term
*> it_pending
= pending
[it
];
1716 for (int j
= 0; j
< it_pending
.size(); ++j
) {
1717 filter
.erase(it_pending
[j
]);
1718 add(it_pending
[j
], &filter
);
1723 void partial_order::print(ostream
& os
, char **p
)
1725 map
<const indicator_term
*, vector
<const indicator_term
* > >::iterator i
;
1726 map
<const indicator_term
*, int >::iterator j
;
1727 std::set
<const indicator_term
*>::iterator k
;
1728 for (k
= head
.begin(); k
!= head
.end(); ++k
) {
1732 for (j
= pred
.begin(); j
!= pred
.end(); ++j
) {
1733 (*j
).first
->print(os
, p
);
1734 os
<< ": " << (*j
).second
<< endl
;
1736 for (i
= lt
.begin(); i
!= lt
.end(); ++i
) {
1737 (*i
).first
->print(os
, p
);
1738 assert(head
.find((*i
).first
) != head
.end() ||
1739 pred
.find((*i
).first
) != pred
.end());
1740 if (pred
.find((*i
).first
) != pred
.end())
1741 os
<< "(" << pred
[(*i
).first
] << ")";
1743 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1746 (*i
).second
[j
]->print(os
, p
);
1747 assert(pred
.find((*i
).second
[j
]) != pred
.end());
1748 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1752 for (i
= le
.begin(); i
!= le
.end(); ++i
) {
1753 (*i
).first
->print(os
, p
);
1754 assert(head
.find((*i
).first
) != head
.end() ||
1755 pred
.find((*i
).first
) != pred
.end());
1756 if (pred
.find((*i
).first
) != pred
.end())
1757 os
<< "(" << pred
[(*i
).first
] << ")";
1759 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1762 (*i
).second
[j
]->print(os
, p
);
1763 assert(pred
.find((*i
).second
[j
]) != pred
.end());
1764 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1768 for (i
= eq
.begin(); i
!= eq
.end(); ++i
) {
1769 if ((*i
).second
.size() <= 1)
1771 (*i
).first
->print(os
, p
);
1772 assert(head
.find((*i
).first
) != head
.end() ||
1773 pred
.find((*i
).first
) != pred
.end());
1774 if (pred
.find((*i
).first
) != pred
.end())
1775 os
<< "(" << pred
[(*i
).first
] << ")";
1776 for (int j
= 1; j
< (*i
).second
.size(); ++j
) {
1779 (*i
).second
[j
]->print(os
, p
);
1780 assert(head
.find((*i
).second
[j
]) != head
.end() ||
1781 pred
.find((*i
).second
[j
]) != pred
.end());
1782 if (pred
.find((*i
).second
[j
]) != pred
.end())
1783 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1787 for (i
= pending
.begin(); i
!= pending
.end(); ++i
) {
1788 os
<< "pending on ";
1789 (*i
).first
->print(os
, p
);
1790 assert(head
.find((*i
).first
) != head
.end() ||
1791 pred
.find((*i
).first
) != pred
.end());
1792 if (pred
.find((*i
).first
) != pred
.end())
1793 os
<< "(" << pred
[(*i
).first
] << ")";
1795 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1798 (*i
).second
[j
]->print(os
, p
);
1799 assert(pred
.find((*i
).second
[j
]) != pred
.end());
1800 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1806 void indicator::add(const indicator_term
* it
)
1808 indicator_term
*nt
= new indicator_term(*it
);
1809 if (options
->reduce
)
1810 nt
->reduce_in_domain(P
? P
: D
->D
);
1812 order
.add(nt
, NULL
);
1813 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1816 void indicator::remove(const indicator_term
* it
)
1818 vector
<indicator_term
*>::iterator i
;
1819 i
= find(term
.begin(), term
.end(), it
);
1820 assert(i
!= term
.end());
1823 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1827 void indicator::expand_rational_vertex(const indicator_term
*initial
)
1829 int pos
= initial
->pos
;
1831 if (ic
.terms
[pos
].size() == 0) {
1833 FORALL_PVertex_in_ParamPolyhedron(V
, PD
, ic
.PP
) // _i is internal counter
1835 ic
.decompose_at_vertex(V
, pos
, options
->barvinok
);
1838 END_FORALL_PVertex_in_ParamPolyhedron
;
1840 for (int j
= 0; j
< ic
.terms
[pos
].size(); ++j
)
1841 add(ic
.terms
[pos
][j
]);
1844 void indicator::remove_initial_rational_vertices()
1847 const indicator_term
*initial
= NULL
;
1848 std::set
<const indicator_term
*>::iterator i
;
1849 for (i
= order
.head
.begin(); i
!= order
.head
.end(); ++i
) {
1850 if ((*i
)->sign
!= 0)
1852 if (order
.eq
.find(*i
) != order
.eq
.end() && order
.eq
[*i
].size() <= 1)
1859 expand_rational_vertex(initial
);
1863 void indicator::reduce_in_domain(Polyhedron
*D
)
1865 for (int i
= 0; i
< term
.size(); ++i
)
1866 term
[i
]->reduce_in_domain(D
);
1869 void indicator::print(ostream
& os
, char **p
)
1871 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1872 for (int i
= 0; i
< term
.size(); ++i
) {
1873 term
[i
]->print(os
, p
);
1875 os
<< ": " << term
[i
]->eval(D
->sample
->p
);
1882 /* Remove pairs of opposite terms */
1883 void indicator::simplify()
1885 for (int i
= 0; i
< term
.size(); ++i
) {
1886 for (int j
= i
+1; j
< term
.size(); ++j
) {
1887 if (term
[i
]->sign
+ term
[j
]->sign
!= 0)
1889 if (term
[i
]->den
!= term
[j
]->den
)
1892 for (k
= 0; k
< term
[i
]->den
.NumCols(); ++k
)
1893 if (!eequal(term
[i
]->vertex
[k
], term
[j
]->vertex
[k
]))
1895 if (k
< term
[i
]->den
.NumCols())
1899 term
.erase(term
.begin()+j
);
1900 term
.erase(term
.begin()+i
);
1907 void indicator::peel(int i
, int j
)
1915 int dim
= term
[i
]->den
.NumCols();
1920 int n_common
= 0, n_i
= 0, n_j
= 0;
1922 common
.SetDims(min(term
[i
]->den
.NumRows(), term
[j
]->den
.NumRows()), dim
);
1923 rest_i
.SetDims(term
[i
]->den
.NumRows(), dim
);
1924 rest_j
.SetDims(term
[j
]->den
.NumRows(), dim
);
1927 for (k
= 0, l
= 0; k
< term
[i
]->den
.NumRows() && l
< term
[j
]->den
.NumRows(); ) {
1928 int s
= lex_cmp(term
[i
]->den
[k
], term
[j
]->den
[l
]);
1930 common
[n_common
++] = term
[i
]->den
[k
];
1934 rest_i
[n_i
++] = term
[i
]->den
[k
++];
1936 rest_j
[n_j
++] = term
[j
]->den
[l
++];
1938 while (k
< term
[i
]->den
.NumRows())
1939 rest_i
[n_i
++] = term
[i
]->den
[k
++];
1940 while (l
< term
[j
]->den
.NumRows())
1941 rest_j
[n_j
++] = term
[j
]->den
[l
++];
1942 common
.SetDims(n_common
, dim
);
1943 rest_i
.SetDims(n_i
, dim
);
1944 rest_j
.SetDims(n_j
, dim
);
1946 for (k
= 0; k
<= n_i
; ++k
) {
1947 indicator_term
*it
= new indicator_term(*term
[i
]);
1948 it
->den
.SetDims(n_common
+ k
, dim
);
1949 for (l
= 0; l
< n_common
; ++l
)
1950 it
->den
[l
] = common
[l
];
1951 for (l
= 0; l
< k
; ++l
)
1952 it
->den
[n_common
+l
] = rest_i
[l
];
1953 lex_order_rows(it
->den
);
1955 for (l
= 0; l
< dim
; ++l
)
1956 evalue_add_constant(it
->vertex
[l
], rest_i
[k
-1][l
]);
1960 for (k
= 0; k
<= n_j
; ++k
) {
1961 indicator_term
*it
= new indicator_term(*term
[j
]);
1962 it
->den
.SetDims(n_common
+ k
, dim
);
1963 for (l
= 0; l
< n_common
; ++l
)
1964 it
->den
[l
] = common
[l
];
1965 for (l
= 0; l
< k
; ++l
)
1966 it
->den
[n_common
+l
] = rest_j
[l
];
1967 lex_order_rows(it
->den
);
1969 for (l
= 0; l
< dim
; ++l
)
1970 evalue_add_constant(it
->vertex
[l
], rest_j
[k
-1][l
]);
1973 term
.erase(term
.begin()+j
);
1974 term
.erase(term
.begin()+i
);
1977 void indicator::combine(const indicator_term
*a
, const indicator_term
*b
)
1979 int dim
= a
->den
.NumCols();
1982 mat_ZZ rest_i
; /* factors in a, but not in b */
1983 mat_ZZ rest_j
; /* factors in b, but not in a */
1984 int n_common
= 0, n_i
= 0, n_j
= 0;
1986 common
.SetDims(min(a
->den
.NumRows(), b
->den
.NumRows()), dim
);
1987 rest_i
.SetDims(a
->den
.NumRows(), dim
);
1988 rest_j
.SetDims(b
->den
.NumRows(), dim
);
1991 for (k
= 0, l
= 0; k
< a
->den
.NumRows() && l
< b
->den
.NumRows(); ) {
1992 int s
= lex_cmp(a
->den
[k
], b
->den
[l
]);
1994 common
[n_common
++] = a
->den
[k
];
1998 rest_i
[n_i
++] = a
->den
[k
++];
2000 rest_j
[n_j
++] = b
->den
[l
++];
2002 while (k
< a
->den
.NumRows())
2003 rest_i
[n_i
++] = a
->den
[k
++];
2004 while (l
< b
->den
.NumRows())
2005 rest_j
[n_j
++] = b
->den
[l
++];
2006 common
.SetDims(n_common
, dim
);
2007 rest_i
.SetDims(n_i
, dim
);
2008 rest_j
.SetDims(n_j
, dim
);
2010 assert(order
.eq
[a
].size() > 1);
2011 indicator_term
*prev
;
2014 for (int k
= n_i
-1; k
>= 0; --k
) {
2015 indicator_term
*it
= new indicator_term(*b
);
2016 it
->den
.SetDims(n_common
+ n_j
+ n_i
-k
, dim
);
2017 for (int l
= k
; l
< n_i
; ++l
)
2018 it
->den
[n_common
+n_j
+l
-k
] = rest_i
[l
];
2019 lex_order_rows(it
->den
);
2020 for (int m
= 0; m
< dim
; ++m
)
2021 evalue_add_constant(it
->vertex
[m
], rest_i
[k
][m
]);
2022 it
->sign
= -it
->sign
;
2024 order
.pending
[it
].push_back(prev
);
2025 order
.lt
[it
].push_back(prev
);
2026 order
.inc_pred(prev
);
2029 order
.head
.insert(it
);
2033 indicator_term
*it
= new indicator_term(*b
);
2034 it
->den
.SetDims(n_common
+ n_i
+ n_j
, dim
);
2035 for (l
= 0; l
< n_i
; ++l
)
2036 it
->den
[n_common
+n_j
+l
] = rest_i
[l
];
2037 lex_order_rows(it
->den
);
2039 order
.pending
[a
].push_back(prev
);
2040 order
.lt
[a
].push_back(prev
);
2041 order
.inc_pred(prev
);
2042 order
.replace(b
, it
);
2047 for (int k
= n_j
-1; k
>= 0; --k
) {
2048 indicator_term
*it
= new indicator_term(*a
);
2049 it
->den
.SetDims(n_common
+ n_i
+ n_j
-k
, dim
);
2050 for (int l
= k
; l
< n_j
; ++l
)
2051 it
->den
[n_common
+n_i
+l
-k
] = rest_j
[l
];
2052 lex_order_rows(it
->den
);
2053 for (int m
= 0; m
< dim
; ++m
)
2054 evalue_add_constant(it
->vertex
[m
], rest_j
[k
][m
]);
2055 it
->sign
= -it
->sign
;
2057 order
.pending
[it
].push_back(prev
);
2058 order
.lt
[it
].push_back(prev
);
2059 order
.inc_pred(prev
);
2062 order
.head
.insert(it
);
2066 indicator_term
*it
= new indicator_term(*a
);
2067 it
->den
.SetDims(n_common
+ n_i
+ n_j
, dim
);
2068 for (l
= 0; l
< n_j
; ++l
)
2069 it
->den
[n_common
+n_i
+l
] = rest_j
[l
];
2070 lex_order_rows(it
->den
);
2072 order
.pending
[a
].push_back(prev
);
2073 order
.lt
[a
].push_back(prev
);
2074 order
.inc_pred(prev
);
2075 order
.replace(a
, it
);
2079 assert(term
.size() == order
.head
.size() + order
.pred
.size());
2082 bool indicator::handle_equal_numerators(const indicator_term
*base
)
2084 for (int i
= 0; i
< order
.eq
[base
].size(); ++i
) {
2085 for (int j
= i
+1; j
< order
.eq
[base
].size(); ++j
) {
2086 if (order
.eq
[base
][i
]->is_opposite(order
.eq
[base
][j
])) {
2087 remove(order
.eq
[base
][j
]);
2088 remove(i
? order
.eq
[base
][i
] : base
);
2093 for (int j
= 1; j
< order
.eq
[base
].size(); ++j
)
2094 if (order
.eq
[base
][j
]->sign
!= base
->sign
) {
2095 combine(base
, order
.eq
[base
][j
]);
2101 void indicator::substitute(evalue
*equation
)
2103 ::substitute(term
, equation
);
2106 void indicator::add_substitution(evalue
*equation
)
2108 for (int i
= 0; i
< substitutions
.size(); ++i
)
2109 if (eequal(substitutions
[i
], equation
))
2111 evalue
*copy
= new evalue();
2112 value_init(copy
->d
);
2113 evalue_copy(copy
, equation
);
2114 substitutions
.push_back(copy
);
2117 void indicator::perform_pending_substitutions()
2119 if (substitutions
.size() == 0)
2122 for (int i
= 0; i
< substitutions
.size(); ++i
) {
2123 substitute(substitutions
[i
]);
2124 free_evalue_refs(substitutions
[i
]);
2125 delete substitutions
[i
];
2127 substitutions
.clear();
2131 static void print_varlist(ostream
& os
, int n
, char **names
)
2135 for (i
= 0; i
< n
; ++i
) {
2143 void max_term::print(ostream
& os
, char **p
, barvinok_options
*options
) const
2146 print_varlist(os
, domain
->dimension(), p
);
2149 for (int i
= 0; i
< max
.size(); ++i
) {
2152 evalue_print(os
, max
[i
], p
);
2156 domain
->print_constraints(os
, p
, options
);
2160 /* T maps the compressed parameters to the original parameters,
2161 * while this max_term is based on the compressed parameters
2162 * and we want get the original parameters back.
2164 void max_term::substitute(Matrix
*T
, barvinok_options
*options
)
2166 assert(domain
->dimension() == T
->NbColumns
-1);
2167 int nexist
= domain
->D
->Dimension
- (T
->NbColumns
-1);
2169 Matrix
*inv
= left_inverse(T
, &Eq
);
2172 value_init(denom
.d
);
2173 value_init(denom
.x
.n
);
2174 value_set_si(denom
.x
.n
, 1);
2175 value_assign(denom
.d
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
2178 v
.SetLength(inv
->NbColumns
);
2179 evalue
* subs
[inv
->NbRows
-1];
2180 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
2181 values2zz(inv
->p
[i
], v
, v
.length());
2182 subs
[i
] = multi_monom(v
);
2183 emul(&denom
, subs
[i
]);
2185 free_evalue_refs(&denom
);
2187 domain
->substitute(subs
, inv
, Eq
, options
->MaxRays
);
2190 for (int i
= 0; i
< max
.size(); ++i
) {
2191 evalue_substitute(max
[i
], subs
);
2192 reduce_evalue(max
[i
]);
2195 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
2196 free_evalue_refs(subs
[i
]);
2202 int Last_Non_Zero(Value
*p
, unsigned len
)
2204 for (int i
= len
-1; i
>= 0; --i
)
2205 if (value_notzero_p(p
[i
]))
2210 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2212 for (int r
= 0; r
< n
; ++r
)
2213 value_swap(V
[r
][i
], V
[r
][j
]);
2216 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2218 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2219 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2222 Vector
*max_term::eval(Value
*val
, unsigned MaxRays
) const
2224 if (!domain
->contains(val
, domain
->dimension()))
2226 Vector
*res
= Vector_Alloc(max
.size());
2227 for (int i
= 0; i
< max
.size(); ++i
) {
2228 compute_evalue(max
[i
], val
, &res
->p
[i
]);
2235 enum sign
{ le
, ge
, lge
} sign
;
2237 split (evalue
*c
, enum sign s
) : constraint(c
), sign(s
) {}
2240 static void split_on(const split
& sp
, EDomain
*D
,
2241 EDomain
**Dlt
, EDomain
**Deq
, EDomain
**Dgt
,
2242 lexmin_options
*options
)
2248 ge_constraint
*ge
= D
->compute_ge_constraint(sp
.constraint
);
2249 if (sp
.sign
== split::lge
|| sp
.sign
== split::ge
)
2250 ED
[2] = EDomain::new_from_ge_constraint(ge
, 1, options
->barvinok
);
2253 if (sp
.sign
== split::lge
|| sp
.sign
== split::le
)
2254 ED
[0] = EDomain::new_from_ge_constraint(ge
, -1, options
->barvinok
);
2258 assert(sp
.sign
== split::lge
|| sp
.sign
== split::ge
|| sp
.sign
== split::le
);
2259 ED
[1] = EDomain::new_from_ge_constraint(ge
, 0, options
->barvinok
);
2263 for (int i
= 0; i
< 3; ++i
) {
2266 if (D
->sample
&& ED
[i
]->contains(D
->sample
->p
, D
->sample
->Size
-1)) {
2267 ED
[i
]->sample
= Vector_Alloc(D
->sample
->Size
);
2268 Vector_Copy(D
->sample
->p
, ED
[i
]->sample
->p
, D
->sample
->Size
);
2269 } else if (emptyQ2(ED
[i
]->D
) ||
2270 (options
->emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_NONE
&&
2271 !(ED
[i
]->not_empty(options
)))) {
2281 ostream
& operator<< (ostream
& os
, const vector
<int> & v
)
2284 for (int i
= 0; i
< v
.size(); ++i
) {
2293 static bool isTranslation(Matrix
*M
)
2296 if (M
->NbRows
!= M
->NbColumns
)
2299 for (i
= 0;i
< M
->NbRows
; i
++)
2300 for (j
= 0; j
< M
->NbColumns
-1; j
++)
2302 if(value_notone_p(M
->p
[i
][j
]))
2305 if(value_notzero_p(M
->p
[i
][j
]))
2308 return value_one_p(M
->p
[M
->NbRows
-1][M
->NbColumns
-1]);
2311 static Matrix
*compress_parameters(Polyhedron
**P
, Polyhedron
**C
,
2312 unsigned nparam
, unsigned MaxRays
)
2316 /* compress_parms doesn't like equalities that only involve parameters */
2317 for (int i
= 0; i
< (*P
)->NbEq
; ++i
)
2318 assert(First_Non_Zero((*P
)->Constraint
[i
]+1, (*P
)->Dimension
-nparam
) != -1);
2320 M
= Matrix_Alloc((*P
)->NbEq
, (*P
)->Dimension
+2);
2321 Vector_Copy((*P
)->Constraint
[0], M
->p
[0], (*P
)->NbEq
* ((*P
)->Dimension
+2));
2322 CP
= compress_parms(M
, nparam
);
2325 if (isTranslation(CP
)) {
2330 T
= align_matrix(CP
, (*P
)->Dimension
+1);
2331 *P
= Polyhedron_Preimage(*P
, T
, MaxRays
);
2334 *C
= Polyhedron_Preimage(*C
, CP
, MaxRays
);
2339 void construct_rational_vertices(Param_Polyhedron
*PP
, Matrix
*T
, unsigned dim
,
2340 int nparam
, vector
<indicator_term
*>& vertices
)
2349 v
.SetLength(nparam
+1);
2352 value_init(factor
.d
);
2353 value_init(factor
.x
.n
);
2354 value_set_si(factor
.x
.n
, 1);
2355 value_set_si(factor
.d
, 1);
2357 for (i
= 0, PV
= PP
->V
; PV
; ++i
, PV
= PV
->next
) {
2358 indicator_term
*term
= new indicator_term(dim
, i
);
2359 vertices
.push_back(term
);
2360 Matrix
*M
= Matrix_Alloc(PV
->Vertex
->NbRows
+nparam
+1, nparam
+1);
2361 value_set_si(lcm
, 1);
2362 for (int j
= 0; j
< PV
->Vertex
->NbRows
; ++j
)
2363 value_lcm(lcm
, PV
->Vertex
->p
[j
][nparam
+1], &lcm
);
2364 value_assign(M
->p
[M
->NbRows
-1][M
->NbColumns
-1], lcm
);
2365 for (int j
= 0; j
< PV
->Vertex
->NbRows
; ++j
) {
2366 value_division(tmp
, lcm
, PV
->Vertex
->p
[j
][nparam
+1]);
2367 Vector_Scale(PV
->Vertex
->p
[j
], M
->p
[j
], tmp
, nparam
+1);
2369 for (int j
= 0; j
< nparam
; ++j
)
2370 value_assign(M
->p
[PV
->Vertex
->NbRows
+j
][j
], lcm
);
2372 Matrix
*M2
= Matrix_Alloc(T
->NbRows
, M
->NbColumns
);
2373 Matrix_Product(T
, M
, M2
);
2377 for (int j
= 0; j
< dim
; ++j
) {
2378 values2zz(M
->p
[j
], v
, nparam
+1);
2379 term
->vertex
[j
] = multi_monom(v
);
2380 value_assign(factor
.d
, lcm
);
2381 emul(&factor
, term
->vertex
[j
]);
2385 assert(i
== PP
->nbV
);
2386 free_evalue_refs(&factor
);
2391 static vector
<max_term
*> lexmin(indicator
& ind
, unsigned nparam
,
2394 vector
<max_term
*> maxima
;
2395 std::set
<const indicator_term
*>::iterator i
;
2396 vector
<int> best_score
;
2397 vector
<int> second_score
;
2398 vector
<int> neg_score
;
2401 ind
.perform_pending_substitutions();
2402 const indicator_term
*best
= NULL
, *second
= NULL
, *neg
= NULL
,
2403 *neg_eq
= NULL
, *neg_le
= NULL
;
2404 for (i
= ind
.order
.head
.begin(); i
!= ind
.order
.head
.end(); ++i
) {
2406 const indicator_term
*term
= *i
;
2407 if (term
->sign
== 0) {
2408 ind
.expand_rational_vertex(term
);
2412 if (ind
.order
.eq
.find(term
) != ind
.order
.eq
.end()) {
2414 if (ind
.order
.eq
[term
].size() <= 1)
2416 for (j
= 1; j
< ind
.order
.eq
[term
].size(); ++j
)
2417 if (ind
.order
.pred
.find(ind
.order
.eq
[term
][j
]) !=
2418 ind
.order
.pred
.end())
2420 if (j
< ind
.order
.eq
[term
].size())
2422 score
.push_back(ind
.order
.eq
[term
].size());
2425 if (ind
.order
.le
.find(term
) != ind
.order
.le
.end())
2426 score
.push_back(ind
.order
.le
[term
].size());
2429 if (ind
.order
.lt
.find(term
) != ind
.order
.lt
.end())
2430 score
.push_back(-ind
.order
.lt
[term
].size());
2434 if (term
->sign
> 0) {
2435 if (!best
|| score
< best_score
) {
2437 second_score
= best_score
;
2440 } else if (!second
|| score
< second_score
) {
2442 second_score
= score
;
2445 if (!neg_eq
&& ind
.order
.eq
.find(term
) != ind
.order
.eq
.end()) {
2446 for (int j
= 1; j
< ind
.order
.eq
[term
].size(); ++j
)
2447 if (ind
.order
.eq
[term
][j
]->sign
!= term
->sign
) {
2452 if (!neg_le
&& ind
.order
.le
.find(term
) != ind
.order
.le
.end())
2454 if (!neg
|| score
< neg_score
) {
2460 if (i
!= ind
.order
.head
.end())
2463 if (!best
&& neg_eq
) {
2464 assert(ind
.order
.eq
[neg_eq
].size() != 0);
2465 bool handled
= ind
.handle_equal_numerators(neg_eq
);
2470 if (!best
&& neg_le
) {
2471 /* The smallest term is negative and <= some positive term */
2477 /* apparently there can be negative initial term on empty domains */
2478 if (ind
.options
->emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_NONE
&&
2479 ind
.options
->polysign
== BV_LEXMIN_POLYSIGN_POLYLIB
)
2484 if (!second
&& !neg
) {
2485 const indicator_term
*rat
= NULL
;
2487 if (ind
.order
.le
.find(best
) == ind
.order
.le
.end()) {
2488 if (ind
.order
.eq
.find(best
) != ind
.order
.eq
.end()) {
2489 bool handled
= ind
.handle_equal_numerators(best
);
2490 if (ind
.options
->emptiness_check
!=
2491 BV_LEXMIN_EMPTINESS_CHECK_NONE
&&
2492 ind
.options
->polysign
== BV_LEXMIN_POLYSIGN_POLYLIB
)
2494 /* If !handled then the leading coefficient is bigger than one;
2495 * must be an empty domain
2502 maxima
.push_back(ind
.create_max_term(best
));
2505 for (int j
= 0; j
< ind
.order
.le
[best
].size(); ++j
) {
2506 if (ind
.order
.le
[best
][j
]->sign
== 0) {
2507 if (!rat
&& ind
.order
.pred
[ind
.order
.le
[best
][j
]] == 1)
2508 rat
= ind
.order
.le
[best
][j
];
2509 } else if (ind
.order
.le
[best
][j
]->sign
> 0) {
2510 second
= ind
.order
.le
[best
][j
];
2513 neg
= ind
.order
.le
[best
][j
];
2516 if (!second
&& !neg
) {
2518 ind
.order
.unset_le(best
, rat
);
2519 ind
.expand_rational_vertex(rat
);
2526 ind
.order
.unset_le(best
, second
);
2532 unsigned dim
= best
->den
.NumCols();
2535 for (int k
= 0; k
< dim
; ++k
) {
2536 diff
= ediff(best
->vertex
[k
], second
->vertex
[k
]);
2537 sign
= evalue_sign(diff
, ind
.D
, ind
.options
);
2539 /* neg can never be smaller than best, unless it may still cancel.
2540 * This can happen if positive terms have been determined to
2541 * be equal or less than or equal to some negative term.
2543 if (second
== neg
&& !neg_eq
&& !neg_le
) {
2544 if (sign
== order_ge
)
2546 if (sign
== order_unknown
)
2550 if (sign
!= order_eq
)
2552 if (!EVALUE_IS_ZERO(*diff
)) {
2553 ind
.add_substitution(diff
);
2554 ind
.perform_pending_substitutions();
2557 if (sign
== order_eq
) {
2558 ind
.order
.set_equal(best
, second
);
2561 if (sign
== order_lt
) {
2562 ind
.order
.lt
[best
].push_back(second
);
2563 ind
.order
.inc_pred(second
);
2566 if (sign
== order_gt
) {
2567 ind
.order
.lt
[second
].push_back(best
);
2568 ind
.order
.inc_pred(best
);
2572 split
sp(diff
, sign
== order_le
? split::le
:
2573 sign
== order_ge
? split::ge
: split::lge
);
2575 EDomain
*Dlt
, *Deq
, *Dgt
;
2576 split_on(sp
, ind
.D
, &Dlt
, &Deq
, &Dgt
, ind
.options
);
2577 if (ind
.options
->emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_NONE
)
2578 assert(Dlt
|| Deq
|| Dgt
);
2579 else if (!(Dlt
|| Deq
|| Dgt
))
2580 /* Must have been empty all along */
2583 if (Deq
&& (Dlt
|| Dgt
)) {
2584 int locsize
= loc
.size();
2586 indicator
indeq(ind
, Deq
);
2588 indeq
.add_substitution(diff
);
2589 indeq
.perform_pending_substitutions();
2590 vector
<max_term
*> maxeq
= lexmin(indeq
, nparam
, loc
);
2591 maxima
.insert(maxima
.end(), maxeq
.begin(), maxeq
.end());
2592 loc
.resize(locsize
);
2595 int locsize
= loc
.size();
2597 indicator
indgt(ind
, Dgt
);
2599 /* we don't know the new location of these terms in indgt */
2601 indgt.order.lt[second].push_back(best);
2602 indgt.order.inc_pred(best);
2604 vector
<max_term
*> maxgt
= lexmin(indgt
, nparam
, loc
);
2605 maxima
.insert(maxima
.end(), maxgt
.begin(), maxgt
.end());
2606 loc
.resize(locsize
);
2611 ind
.set_domain(Deq
);
2612 ind
.add_substitution(diff
);
2613 ind
.perform_pending_substitutions();
2617 ind
.set_domain(Dlt
);
2618 ind
.order
.lt
[best
].push_back(second
);
2619 ind
.order
.inc_pred(second
);
2623 ind
.set_domain(Dgt
);
2624 ind
.order
.lt
[second
].push_back(best
);
2625 ind
.order
.inc_pred(best
);
2632 static vector
<max_term
*> lexmin(Polyhedron
*P
, Polyhedron
*C
,
2633 lexmin_options
*options
)
2635 unsigned nparam
= C
->Dimension
;
2636 Param_Polyhedron
*PP
= NULL
;
2637 Polyhedron
*CEq
= NULL
, *pVD
;
2639 Matrix
*T
= NULL
, *CP
= NULL
;
2640 Param_Domain
*D
, *next
;
2642 Polyhedron
*Porig
= P
;
2643 Polyhedron
*Corig
= C
;
2644 vector
<max_term
*> all_max
;
2646 unsigned P2PSD_MaxRays
;
2651 POL_ENSURE_VERTICES(P
);
2656 assert(P
->NbBid
== 0);
2659 remove_all_equalities(&P
, &C
, &CP
, &T
, nparam
, options
->barvinok
->MaxRays
);
2661 nparam
= CP
->NbColumns
-1;
2669 if (options
->barvinok
->MaxRays
& POL_NO_DUAL
)
2672 P2PSD_MaxRays
= options
->barvinok
->MaxRays
;
2675 PP
= Polyhedron2Param_SimplifiedDomain(&P
, C
, P2PSD_MaxRays
, &CEq
, &CT
);
2676 if (P
!= Q
&& Q
!= Porig
)
2680 if (isIdentity(CT
)) {
2684 nparam
= CT
->NbRows
- 1;
2688 unsigned dim
= P
->Dimension
- nparam
;
2691 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
2692 Polyhedron
**fVD
= new Polyhedron
*[nd
];
2694 indicator_constructor
ic(P
, dim
, PP
, T
);
2696 vector
<indicator_term
*> all_vertices
;
2697 construct_rational_vertices(PP
, T
, T
? T
->NbRows
-nparam
-1 : dim
,
2698 nparam
, all_vertices
);
2700 for (nd
= 0, D
=PP
->D
; D
; D
=next
) {
2703 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2704 fVD
, nd
, options
->barvinok
->MaxRays
);
2708 pVD
= CT
? DomainImage(rVD
, CT
, options
->barvinok
->MaxRays
) : rVD
;
2710 EDomain
*epVD
= new EDomain(pVD
);
2711 indicator
ind(ic
, D
, epVD
, options
);
2713 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
2714 ind
.add(all_vertices
[_i
]);
2715 END_FORALL_PVertex_in_ParamPolyhedron
;
2717 ind
.remove_initial_rational_vertices();
2720 vector
<max_term
*> maxima
= lexmin(ind
, nparam
, loc
);
2722 for (int j
= 0; j
< maxima
.size(); ++j
)
2723 maxima
[j
]->substitute(CP
, options
->barvinok
);
2724 all_max
.insert(all_max
.end(), maxima
.begin(), maxima
.end());
2731 for (int i
= 0; i
< all_vertices
.size(); ++i
)
2732 delete all_vertices
[i
];
2737 Param_Polyhedron_Free(PP
);
2739 Polyhedron_Free(CEq
);
2740 for (--nd
; nd
>= 0; --nd
) {
2741 Domain_Free(fVD
[nd
]);
2752 static void verify_results(Polyhedron
*A
, Polyhedron
*C
,
2753 vector
<max_term
*>& maxima
, int m
, int M
,
2754 int print_all
, unsigned MaxRays
);
2756 int main(int argc
, char **argv
)
2761 char **iter_names
, **param_names
;
2762 int print_solution
= 1;
2763 struct lexmin_options options
;
2764 static struct argp_child argp_children
[] = {
2765 { &barvinok_argp
, 0, 0, 0 },
2766 { &verify_argp
, 0, "verification", 1 },
2769 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
2770 struct barvinok_options
*bv_options
;
2772 bv_options
= barvinok_options_new_with_defaults();
2773 bv_options
->lookup_table
= 0;
2775 options
.barvinok
= bv_options
;
2776 argp_parse(&argp
, argc
, argv
, 0, 0, &options
);
2779 C
= Constraints2Polyhedron(MA
, bv_options
->MaxRays
);
2781 fscanf(stdin
, " %d", &bignum
);
2782 assert(bignum
== -1);
2784 A
= Constraints2Polyhedron(MA
, bv_options
->MaxRays
);
2787 verify_options_set_range(&options
.verify
, A
);
2789 if (options
.verify
.verify
)
2792 iter_names
= util_generate_names(A
->Dimension
- C
->Dimension
, "i");
2793 param_names
= util_generate_names(C
->Dimension
, "p");
2794 if (print_solution
) {
2795 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
2796 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
2798 vector
<max_term
*> maxima
= lexmin(A
, C
, &options
);
2800 for (int i
= 0; i
< maxima
.size(); ++i
)
2801 maxima
[i
]->print(cout
, param_names
, options
.barvinok
);
2803 if (options
.verify
.verify
)
2804 verify_results(A
, C
, maxima
, options
.verify
.m
, options
.verify
.M
,
2805 options
.verify
.print_all
, bv_options
->MaxRays
);
2807 for (int i
= 0; i
< maxima
.size(); ++i
)
2810 util_free_names(A
->Dimension
- C
->Dimension
, iter_names
);
2811 util_free_names(C
->Dimension
, param_names
);
2820 static bool lexmin(int pos
, Polyhedron
*P
, Value
*context
)
2829 value_init(LB
); value_init(UB
); value_init(k
);
2832 lu_flags
= lower_upper_bounds(pos
,P
,context
,&LB
,&UB
);
2833 assert(!(lu_flags
& LB_INFINITY
));
2835 value_set_si(context
[pos
],0);
2836 if (!lu_flags
&& value_lt(UB
,LB
)) {
2837 value_clear(LB
); value_clear(UB
); value_clear(k
);
2841 value_assign(context
[pos
], LB
);
2842 value_clear(LB
); value_clear(UB
); value_clear(k
);
2845 for (value_assign(k
,LB
); lu_flags
|| value_le(k
,UB
); value_increment(k
,k
)) {
2846 value_assign(context
[pos
],k
);
2847 if ((found
= lexmin(pos
+1, P
->next
, context
)))
2851 value_set_si(context
[pos
],0);
2852 value_clear(LB
); value_clear(UB
); value_clear(k
);
2856 static void print_list(FILE *out
, Value
*z
, char* brackets
, int len
)
2858 fprintf(out
, "%c", brackets
[0]);
2859 value_print(out
, VALUE_FMT
,z
[0]);
2860 for (int k
= 1; k
< len
; ++k
) {
2862 value_print(out
, VALUE_FMT
,z
[k
]);
2864 fprintf(out
, "%c", brackets
[1]);
2867 static int check_poly(Polyhedron
*S
, Polyhedron
*CS
, vector
<max_term
*>& maxima
,
2868 int nparam
, int pos
, Value
*z
, int print_all
, int st
,
2871 if (pos
== nparam
) {
2873 bool found
= lexmin(1, S
, z
);
2877 print_list(stdout
, z
+S
->Dimension
-nparam
+1, "()", nparam
);
2880 print_list(stdout
, z
+1, "[]", S
->Dimension
-nparam
);
2885 for (int i
= 0; i
< maxima
.size(); ++i
)
2886 if ((min
= maxima
[i
]->eval(z
+S
->Dimension
-nparam
+1, MaxRays
)))
2889 int ok
= !(found
^ !!min
);
2891 for (int i
= 0; i
< S
->Dimension
-nparam
; ++i
)
2892 if (value_ne(z
[1+i
], min
->p
[i
])) {
2899 fprintf(stderr
, "Error !\n");
2900 fprintf(stderr
, "lexmin");
2901 print_list(stderr
, z
+S
->Dimension
-nparam
+1, "()", nparam
);
2902 fprintf(stderr
, " should be ");
2904 print_list(stderr
, z
+1, "[]", S
->Dimension
-nparam
);
2905 fprintf(stderr
, " while digging gives ");
2907 print_list(stderr
, min
->p
, "[]", S
->Dimension
-nparam
);
2908 fprintf(stderr
, ".\n");
2910 } else if (print_all
)
2915 for (k
= 1; k
<= S
->Dimension
-nparam
; ++k
)
2916 value_set_si(z
[k
], 0);
2924 !(lower_upper_bounds(1+pos
, CS
, &z
[S
->Dimension
-nparam
], &LB
, &UB
));
2925 for (value_assign(tmp
,LB
); value_le(tmp
,UB
); value_increment(tmp
,tmp
)) {
2927 int k
= VALUE_TO_INT(tmp
);
2928 if (!pos
&& !(k
%st
)) {
2933 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
2934 if (!check_poly(S
, CS
->next
, maxima
, nparam
, pos
+1, z
, print_all
, st
,
2942 value_set_si(z
[pos
+S
->Dimension
-nparam
+1],0);
2950 void verify_results(Polyhedron
*A
, Polyhedron
*C
, vector
<max_term
*>& maxima
,
2951 int m
, int M
, int print_all
, unsigned MaxRays
)
2953 Polyhedron
*CC
, *CC2
, *CS
, *S
;
2954 unsigned nparam
= C
->Dimension
;
2959 CC
= Polyhedron_Project(A
, nparam
);
2960 CC2
= DomainIntersection(C
, CC
, MaxRays
);
2964 /* Intersect context with range */
2969 MM
= Matrix_Alloc(2*C
->Dimension
, C
->Dimension
+2);
2970 for (int i
= 0; i
< C
->Dimension
; ++i
) {
2971 value_set_si(MM
->p
[2*i
][0], 1);
2972 value_set_si(MM
->p
[2*i
][1+i
], 1);
2973 value_set_si(MM
->p
[2*i
][1+C
->Dimension
], -m
);
2974 value_set_si(MM
->p
[2*i
+1][0], 1);
2975 value_set_si(MM
->p
[2*i
+1][1+i
], -1);
2976 value_set_si(MM
->p
[2*i
+1][1+C
->Dimension
], M
);
2978 CC2
= AddConstraints(MM
->p
[0], 2*CC
->Dimension
, CC
, MaxRays
);
2979 U
= Universe_Polyhedron(0);
2980 CS
= Polyhedron_Scan(CC2
, U
, MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
2982 Polyhedron_Free(CC2
);
2987 p
= ALLOCN(Value
, A
->Dimension
+2);
2988 for (i
=0; i
<= A
->Dimension
; i
++) {
2990 value_set_si(p
[i
],0);
2993 value_set_si(p
[i
], 1);
2995 S
= Polyhedron_Scan(A
, C
, MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
2997 if (!print_all
&& C
->Dimension
> 0) {
3002 for (int i
= m
; i
<= M
; i
+= st
)
3009 if (!(CS
&& emptyQ2(CS
)))
3010 check_poly(S
, CS
, maxima
, nparam
, 0, p
, print_all
, st
, MaxRays
);
3017 for (i
=0; i
<= (A
->Dimension
+1); i
++)
3022 Polyhedron_Free(CC
);