6 #define partition STL_PARTITION
10 #include <NTL/vec_ZZ.h>
11 #include <NTL/mat_ZZ.h>
12 #include <isl_set_polylib.h>
13 #include <barvinok/barvinok.h>
14 #include <barvinok/evalue.h>
15 #include <barvinok/options.h>
16 #include <barvinok/util.h>
17 #include "conversion.h"
18 #include "decomposer.h"
19 #include "lattice_point.h"
20 #include "reduce_domain.h"
23 #include "evalue_util.h"
24 #include "remove_equalities.h"
28 #include "param_util.h"
30 #undef CS /* for Solaris 10 */
41 #define ALLOC(type) (type*)malloc(sizeof(type))
42 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
44 static int type_offset(enode
*p
)
46 return p
->type
== fractional
? 1 :
47 p
->type
== flooring
? 1 : 0;
50 void compute_evalue(evalue
*e
, Value
*val
, Value
*res
)
52 double d
= compute_evalue(e
, val
);
57 value_set_double(*res
, d
);
60 struct indicator_term
{
62 int pos
; /* number of rational vertex */
63 int n
; /* number of cone associated to given rational vertex */
67 indicator_term(unsigned dim
, int pos
) {
69 vertex
= new evalue
* [dim
];
74 indicator_term(unsigned dim
, int pos
, int n
) {
75 den
.SetDims(dim
, dim
);
76 vertex
= new evalue
* [dim
];
80 indicator_term(const indicator_term
& src
) {
85 unsigned dim
= den
.NumCols();
86 vertex
= new evalue
* [dim
];
87 for (int i
= 0; i
< dim
; ++i
) {
88 vertex
[i
] = ALLOC(evalue
);
89 value_init(vertex
[i
]->d
);
90 evalue_copy(vertex
[i
], src
.vertex
[i
]);
93 void swap(indicator_term
*other
) {
95 tmp
= sign
; sign
= other
->sign
; other
->sign
= tmp
;
96 tmp
= pos
; pos
= other
->pos
; other
->pos
= tmp
;
97 tmp
= n
; n
= other
->n
; other
->n
= tmp
;
98 mat_ZZ tmp_den
= den
; den
= other
->den
; other
->den
= tmp_den
;
99 unsigned dim
= den
.NumCols();
100 for (int i
= 0; i
< dim
; ++i
) {
101 evalue
*tmp
= vertex
[i
];
102 vertex
[i
] = other
->vertex
[i
];
103 other
->vertex
[i
] = tmp
;
107 unsigned dim
= den
.NumCols();
108 for (int i
= 0; i
< dim
; ++i
)
109 evalue_free(vertex
[i
]);
112 void print(ostream
& os
, char **p
) const;
113 void substitute(Matrix
*T
);
115 void substitute(evalue
*fract
, evalue
*val
);
116 void substitute(int pos
, evalue
*val
);
117 void reduce_in_domain(Polyhedron
*D
);
118 bool is_opposite(const indicator_term
*neg
) const;
119 vec_ZZ
eval(Value
*val
) const {
121 unsigned dim
= den
.NumCols();
125 for (int i
= 0; i
< dim
; ++i
) {
126 compute_evalue(vertex
[i
], val
, &tmp
);
134 static int evalue_rational_cmp(const evalue
*e1
, const evalue
*e2
)
142 assert(value_notzero_p(e1
->d
));
143 assert(value_notzero_p(e2
->d
));
144 value_multiply(m
, e1
->x
.n
, e2
->d
);
145 value_multiply(m2
, e2
->x
.n
, e1
->d
);
148 else if (value_gt(m
, m2
))
158 static int evalue_cmp(const evalue
*e1
, const evalue
*e2
)
160 if (value_notzero_p(e1
->d
)) {
161 if (value_zero_p(e2
->d
))
163 return evalue_rational_cmp(e1
, e2
);
165 if (value_notzero_p(e2
->d
))
167 if (e1
->x
.p
->type
!= e2
->x
.p
->type
)
168 return e1
->x
.p
->type
- e2
->x
.p
->type
;
169 if (e1
->x
.p
->size
!= e2
->x
.p
->size
)
170 return e1
->x
.p
->size
- e2
->x
.p
->size
;
171 if (e1
->x
.p
->pos
!= e2
->x
.p
->pos
)
172 return e1
->x
.p
->pos
- e2
->x
.p
->pos
;
173 assert(e1
->x
.p
->type
== polynomial
||
174 e1
->x
.p
->type
== fractional
||
175 e1
->x
.p
->type
== flooring
);
176 for (int i
= 0; i
< e1
->x
.p
->size
; ++i
) {
177 int s
= evalue_cmp(&e1
->x
.p
->arr
[i
], &e2
->x
.p
->arr
[i
]);
184 void evalue_length(evalue
*e
, int len
[2])
189 while (value_zero_p(e
->d
)) {
190 assert(e
->x
.p
->type
== polynomial
||
191 e
->x
.p
->type
== fractional
||
192 e
->x
.p
->type
== flooring
);
193 if (e
->x
.p
->type
== polynomial
)
197 int offset
= type_offset(e
->x
.p
);
198 assert(e
->x
.p
->size
== offset
+2);
199 e
= &e
->x
.p
->arr
[offset
];
203 static bool it_smaller(const indicator_term
* it1
, const indicator_term
* it2
)
207 int len1
[2], len2
[2];
208 unsigned dim
= it1
->den
.NumCols();
209 for (int i
= 0; i
< dim
; ++i
) {
210 evalue_length(it1
->vertex
[i
], len1
);
211 evalue_length(it2
->vertex
[i
], len2
);
212 if (len1
[0] != len2
[0])
213 return len1
[0] < len2
[0];
214 if (len1
[1] != len2
[1])
215 return len1
[1] < len2
[1];
217 if (it1
->pos
!= it2
->pos
)
218 return it1
->pos
< it2
->pos
;
219 if (it1
->n
!= it2
->n
)
220 return it1
->n
< it2
->n
;
221 int s
= lex_cmp(it1
->den
, it2
->den
);
224 for (int i
= 0; i
< dim
; ++i
) {
225 s
= evalue_cmp(it1
->vertex
[i
], it2
->vertex
[i
]);
229 assert(it1
->sign
!= 0);
230 assert(it2
->sign
!= 0);
231 if (it1
->sign
!= it2
->sign
)
232 return it1
->sign
> 0;
237 static const int requires_resort
;
238 bool operator()(const indicator_term
* it1
, const indicator_term
* it2
) const {
239 return it_smaller(it1
, it2
);
242 const int smaller_it::requires_resort
= 1;
244 struct smaller_it_p
{
245 static const int requires_resort
;
246 bool operator()(const indicator_term
* it1
, const indicator_term
* it2
) const {
250 const int smaller_it_p::requires_resort
= 0;
252 /* Returns true if this and neg are opposite using the knowledge
253 * that they have the same numerator.
254 * In particular, we check that the signs are different and that
255 * the denominator is the same.
257 bool indicator_term::is_opposite(const indicator_term
*neg
) const
259 if (sign
+ neg
->sign
!= 0)
266 void indicator_term::reduce_in_domain(Polyhedron
*D
)
268 for (int k
= 0; k
< den
.NumCols(); ++k
) {
269 reduce_evalue_in_domain(vertex
[k
], D
);
270 if (evalue_range_reduction_in_domain(vertex
[k
], D
))
271 reduce_evalue(vertex
[k
]);
275 void indicator_term::print(ostream
& os
, char **p
) const
277 unsigned dim
= den
.NumCols();
278 unsigned factors
= den
.NumRows();
286 for (int i
= 0; i
< dim
; ++i
) {
289 evalue_print(os
, vertex
[i
], p
);
292 for (int i
= 0; i
< factors
; ++i
) {
293 os
<< " + t" << i
<< "*[";
294 for (int j
= 0; j
< dim
; ++j
) {
301 os
<< " ((" << pos
<< ", " << n
<< ", " << (void*)this << "))";
304 /* Perform the substitution specified by T on the variables.
305 * T has dimension (newdim+nparam+1) x (olddim + nparam + 1).
306 * The substitution is performed as in gen_fun::substitute
308 void indicator_term::substitute(Matrix
*T
)
310 unsigned dim
= den
.NumCols();
311 unsigned nparam
= T
->NbColumns
- dim
- 1;
312 unsigned newdim
= T
->NbRows
- nparam
- 1;
315 matrix2zz(T
, trans
, newdim
, dim
);
316 trans
= transpose(trans
);
318 newvertex
= new evalue
* [newdim
];
321 v
.SetLength(nparam
+1);
324 value_init(factor
.d
);
325 value_set_si(factor
.d
, 1);
326 value_init(factor
.x
.n
);
327 for (int i
= 0; i
< newdim
; ++i
) {
328 values2zz(T
->p
[i
]+dim
, v
, nparam
+1);
329 newvertex
[i
] = multi_monom(v
);
331 for (int j
= 0; j
< dim
; ++j
) {
332 if (value_zero_p(T
->p
[i
][j
]))
336 evalue_copy(&term
, vertex
[j
]);
337 value_assign(factor
.x
.n
, T
->p
[i
][j
]);
338 emul(&factor
, &term
);
339 eadd(&term
, newvertex
[i
]);
340 free_evalue_refs(&term
);
343 free_evalue_refs(&factor
);
344 for (int i
= 0; i
< dim
; ++i
)
345 evalue_free(vertex
[i
]);
350 static void evalue_add_constant(evalue
*e
, ZZ v
)
355 /* go down to constant term */
356 while (value_zero_p(e
->d
))
357 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)];
360 value_multiply(tmp
, tmp
, e
->d
);
361 value_addto(e
->x
.n
, e
->x
.n
, tmp
);
366 /* Make all powers in denominator lexico-positive */
367 void indicator_term::normalize()
370 extra_vertex
.SetLength(den
.NumCols());
371 for (int r
= 0; r
< den
.NumRows(); ++r
) {
372 for (int k
= 0; k
< den
.NumCols(); ++k
) {
379 extra_vertex
+= den
[r
];
383 for (int k
= 0; k
< extra_vertex
.length(); ++k
)
384 if (extra_vertex
[k
] != 0)
385 evalue_add_constant(vertex
[k
], extra_vertex
[k
]);
388 static void substitute(evalue
*e
, evalue
*fract
, evalue
*val
)
392 for (t
= e
; value_zero_p(t
->d
); t
= &t
->x
.p
->arr
[type_offset(t
->x
.p
)]) {
393 if (t
->x
.p
->type
== fractional
&& eequal(&t
->x
.p
->arr
[0], fract
))
396 if (value_notzero_p(t
->d
))
399 free_evalue_refs(&t
->x
.p
->arr
[0]);
400 evalue
*term
= &t
->x
.p
->arr
[2];
407 free_evalue_refs(term
);
413 void indicator_term::substitute(evalue
*fract
, evalue
*val
)
415 unsigned dim
= den
.NumCols();
416 for (int i
= 0; i
< dim
; ++i
) {
417 ::substitute(vertex
[i
], fract
, val
);
421 static void substitute(evalue
*e
, int pos
, evalue
*val
)
425 for (t
= e
; value_zero_p(t
->d
); t
= &t
->x
.p
->arr
[type_offset(t
->x
.p
)]) {
426 if (t
->x
.p
->type
== polynomial
&& t
->x
.p
->pos
== pos
)
429 if (value_notzero_p(t
->d
))
432 evalue
*term
= &t
->x
.p
->arr
[1];
439 free_evalue_refs(term
);
445 void indicator_term::substitute(int pos
, evalue
*val
)
447 unsigned dim
= den
.NumCols();
448 for (int i
= 0; i
< dim
; ++i
) {
449 ::substitute(vertex
[i
], pos
, val
);
453 struct indicator_constructor
: public signed_cone_consumer
,
454 public vertex_decomposer
{
456 vector
<indicator_term
*> *terms
;
457 Matrix
*T
; /* Transformation to original space */
462 indicator_constructor(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
,
464 vertex_decomposer(PP
, *this), T(T
), nbV(PP
->nbV
) {
465 vertex
.SetLength(dim
);
466 terms
= new vector
<indicator_term
*>[PP
->nbV
];
468 ~indicator_constructor() {
469 for (int i
= 0; i
< nbV
; ++i
)
470 for (int j
= 0; j
< terms
[i
].size(); ++j
)
474 void print(ostream
& os
, char **p
);
476 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
477 void decompose_at_vertex(Param_Vertices
*V
, int _i
,
478 barvinok_options
*options
) {
481 vertex_decomposer::decompose_at_vertex(V
, _i
, options
);
485 void indicator_constructor::handle(const signed_cone
& sc
, barvinok_options
*options
)
488 unsigned dim
= vertex
.length();
490 assert(sc
.rays
.NumRows() == dim
);
492 indicator_term
*term
= new indicator_term(dim
, pos
, n
++);
493 term
->sign
= sc
.sign
;
494 terms
[vert
].push_back(term
);
496 lattice_point(V
, sc
.rays
, vertex
, term
->vertex
, options
);
499 for (int r
= 0; r
< dim
; ++r
) {
500 for (int j
= 0; j
< dim
; ++j
) {
501 if (term
->den
[r
][j
] == 0)
503 if (term
->den
[r
][j
] > 0)
505 term
->sign
= -term
->sign
;
506 term
->den
[r
] = -term
->den
[r
];
507 vertex
+= term
->den
[r
];
512 for (int i
= 0; i
< dim
; ++i
) {
513 if (!term
->vertex
[i
]) {
514 term
->vertex
[i
] = ALLOC(evalue
);
515 value_init(term
->vertex
[i
]->d
);
516 value_init(term
->vertex
[i
]->x
.n
);
517 zz2value(vertex
[i
], term
->vertex
[i
]->x
.n
);
518 value_set_si(term
->vertex
[i
]->d
, 1);
523 evalue_add_constant(term
->vertex
[i
], vertex
[i
]);
531 lex_order_rows(term
->den
);
534 void indicator_constructor::print(ostream
& os
, char **p
)
536 for (int i
= 0; i
< PP
->nbV
; ++i
)
537 for (int j
= 0; j
< terms
[i
].size(); ++j
) {
538 os
<< "i: " << i
<< ", j: " << j
<< endl
;
539 terms
[i
][j
]->print(os
, p
);
544 struct order_cache_el
{
546 order_cache_el
copy() const {
548 for (int i
= 0; i
< e
.size(); ++i
) {
549 evalue
*c
= new evalue
;
551 evalue_copy(c
, e
[i
]);
557 for (int i
= 0; i
< e
.size(); ++i
) {
558 free_evalue_refs(e
[i
]);
565 evalue_set_si(&mone
, -1, 1);
566 for (int i
= 0; i
< e
.size(); ++i
)
568 free_evalue_refs(&mone
);
570 void print(ostream
& os
, char **p
);
573 void order_cache_el::print(ostream
& os
, char **p
)
576 for (int i
= 0; i
< e
.size(); ++i
) {
579 evalue_print(os
, e
[i
], p
);
585 vector
<order_cache_el
> lt
;
586 vector
<order_cache_el
> le
;
587 vector
<order_cache_el
> unknown
;
589 void clear_transients() {
590 for (int i
= 0; i
< le
.size(); ++i
)
592 for (int i
= 0; i
< unknown
.size(); ++i
)
599 for (int i
= 0; i
< lt
.size(); ++i
)
603 void add(order_cache_el
& cache_el
, order_sign sign
);
604 order_sign
check_lt(vector
<order_cache_el
>* list
,
605 const indicator_term
*a
, const indicator_term
*b
,
606 order_cache_el
& cache_el
);
607 order_sign
check_lt(const indicator_term
*a
, const indicator_term
*b
,
608 order_cache_el
& cache_el
);
609 order_sign
check_direct(const indicator_term
*a
, const indicator_term
*b
,
610 order_cache_el
& cache_el
);
611 order_sign
check(const indicator_term
*a
, const indicator_term
*b
,
612 order_cache_el
& cache_el
);
613 void copy(const order_cache
& cache
);
614 void print(ostream
& os
, char **p
);
617 void order_cache::copy(const order_cache
& cache
)
619 for (int i
= 0; i
< cache
.lt
.size(); ++i
) {
620 order_cache_el n
= cache
.lt
[i
].copy();
625 void order_cache::add(order_cache_el
& cache_el
, order_sign sign
)
627 if (sign
== order_lt
) {
628 lt
.push_back(cache_el
);
629 } else if (sign
== order_gt
) {
631 lt
.push_back(cache_el
);
632 } else if (sign
== order_le
) {
633 le
.push_back(cache_el
);
634 } else if (sign
== order_ge
) {
636 le
.push_back(cache_el
);
637 } else if (sign
== order_unknown
) {
638 unknown
.push_back(cache_el
);
640 assert(sign
== order_eq
);
647 static evalue
*ediff(const evalue
*a
, const evalue
*b
)
651 evalue_set_si(&mone
, -1, 1);
652 evalue
*diff
= new evalue
;
654 evalue_copy(diff
, b
);
658 free_evalue_refs(&mone
);
662 static bool evalue_first_difference(const evalue
*e1
, const evalue
*e2
,
663 const evalue
**d1
, const evalue
**d2
)
668 if (value_ne(e1
->d
, e2
->d
))
671 if (value_notzero_p(e1
->d
)) {
672 if (value_eq(e1
->x
.n
, e2
->x
.n
))
676 if (e1
->x
.p
->type
!= e2
->x
.p
->type
)
678 if (e1
->x
.p
->size
!= e2
->x
.p
->size
)
680 if (e1
->x
.p
->pos
!= e2
->x
.p
->pos
)
683 assert(e1
->x
.p
->type
== polynomial
||
684 e1
->x
.p
->type
== fractional
||
685 e1
->x
.p
->type
== flooring
);
686 int offset
= type_offset(e1
->x
.p
);
687 assert(e1
->x
.p
->size
== offset
+2);
688 for (int i
= 0; i
< e1
->x
.p
->size
; ++i
)
689 if (i
!= type_offset(e1
->x
.p
) &&
690 !eequal(&e1
->x
.p
->arr
[i
], &e2
->x
.p
->arr
[i
]))
693 return evalue_first_difference(&e1
->x
.p
->arr
[offset
],
694 &e2
->x
.p
->arr
[offset
], d1
, d2
);
697 static order_sign
evalue_diff_constant_sign(const evalue
*e1
, const evalue
*e2
)
699 if (!evalue_first_difference(e1
, e2
, &e1
, &e2
))
701 if (value_zero_p(e1
->d
) || value_zero_p(e2
->d
))
702 return order_undefined
;
703 int s
= evalue_rational_cmp(e1
, e2
);
712 order_sign
order_cache::check_lt(vector
<order_cache_el
>* list
,
713 const indicator_term
*a
, const indicator_term
*b
,
714 order_cache_el
& cache_el
)
716 order_sign sign
= order_undefined
;
717 for (int i
= 0; i
< list
->size(); ++i
) {
719 for (j
= cache_el
.e
.size(); j
< (*list
)[i
].e
.size(); ++j
)
720 cache_el
.e
.push_back(ediff(a
->vertex
[j
], b
->vertex
[j
]));
721 for (j
= 0; j
< (*list
)[i
].e
.size(); ++j
) {
722 order_sign diff_sign
;
723 diff_sign
= evalue_diff_constant_sign((*list
)[i
].e
[j
], cache_el
.e
[j
]);
724 if (diff_sign
== order_gt
) {
727 } else if (diff_sign
== order_lt
)
729 else if (diff_sign
== order_undefined
)
732 assert(diff_sign
== order_eq
);
734 if (j
== (*list
)[i
].e
.size())
735 sign
= list
== <
? order_lt
: order_le
;
736 if (sign
!= order_undefined
)
742 order_sign
order_cache::check_direct(const indicator_term
*a
,
743 const indicator_term
*b
,
744 order_cache_el
& cache_el
)
746 order_sign sign
= check_lt(<
, a
, b
, cache_el
);
747 if (sign
!= order_undefined
)
749 sign
= check_lt(&le
, a
, b
, cache_el
);
750 if (sign
!= order_undefined
)
753 for (int i
= 0; i
< unknown
.size(); ++i
) {
755 for (j
= cache_el
.e
.size(); j
< unknown
[i
].e
.size(); ++j
)
756 cache_el
.e
.push_back(ediff(a
->vertex
[j
], b
->vertex
[j
]));
757 for (j
= 0; j
< unknown
[i
].e
.size(); ++j
) {
758 if (!eequal(unknown
[i
].e
[j
], cache_el
.e
[j
]))
761 if (j
== unknown
[i
].e
.size()) {
762 sign
= order_unknown
;
769 order_sign
order_cache::check(const indicator_term
*a
, const indicator_term
*b
,
770 order_cache_el
& cache_el
)
772 order_sign sign
= check_direct(a
, b
, cache_el
);
773 if (sign
!= order_undefined
)
775 int size
= cache_el
.e
.size();
777 sign
= check_direct(a
, b
, cache_el
);
779 assert(cache_el
.e
.size() == size
);
780 if (sign
== order_undefined
)
782 if (sign
== order_lt
)
784 else if (sign
== order_le
)
787 assert(sign
== order_unknown
);
793 struct partial_order
{
796 typedef std::set
<const indicator_term
*, smaller_it
> head_type
;
798 typedef map
<const indicator_term
*, int, smaller_it
> pred_type
;
800 typedef map
<const indicator_term
*, vector
<const indicator_term
* >, smaller_it
> order_type
;
809 partial_order(indicator
*ind
) : ind(ind
) {}
810 void copy(const partial_order
& order
,
811 map
< const indicator_term
*, indicator_term
* > old2new
);
813 order_type::iterator i
;
814 pred_type::iterator j
;
815 head_type::iterator k
;
817 if (head
.key_comp().requires_resort
) {
819 for (k
= head
.begin(); k
!= head
.end(); ++k
)
825 if (pred
.key_comp().requires_resort
) {
827 for (j
= pred
.begin(); j
!= pred
.end(); ++j
)
828 new_pred
[(*j
).first
] = (*j
).second
;
833 if (lt
.key_comp().requires_resort
) {
835 for (i
= lt
.begin(); i
!= lt
.end(); ++i
)
836 m
[(*i
).first
] = (*i
).second
;
841 if (le
.key_comp().requires_resort
) {
843 for (i
= le
.begin(); i
!= le
.end(); ++i
)
844 m
[(*i
).first
] = (*i
).second
;
849 if (eq
.key_comp().requires_resort
) {
851 for (i
= eq
.begin(); i
!= eq
.end(); ++i
)
852 m
[(*i
).first
] = (*i
).second
;
857 if (pending
.key_comp().requires_resort
) {
859 for (i
= pending
.begin(); i
!= pending
.end(); ++i
)
860 m
[(*i
).first
] = (*i
).second
;
866 order_sign
compare(const indicator_term
*a
, const indicator_term
*b
);
867 void set_equal(const indicator_term
*a
, const indicator_term
*b
);
868 void unset_le(const indicator_term
*a
, const indicator_term
*b
);
869 void dec_pred(const indicator_term
*it
) {
870 if (--pred
[it
] == 0) {
875 void inc_pred(const indicator_term
*it
) {
876 if (head
.find(it
) != head
.end())
881 bool compared(const indicator_term
* a
, const indicator_term
* b
);
882 void add(const indicator_term
* it
, std::set
<const indicator_term
*> *filter
);
883 void remove(const indicator_term
* it
);
885 void print(ostream
& os
, char **p
);
887 /* replace references to orig to references to replacement */
888 void replace(const indicator_term
* orig
, indicator_term
* replacement
);
889 void sanity_check() const;
892 /* We actually replace the contents of orig by that of replacement,
893 * but we have to be careful since replacing the content changes
894 * the order of orig in the maps.
896 void partial_order::replace(const indicator_term
* orig
, indicator_term
* replacement
)
898 head_type::iterator k
;
900 bool is_head
= k
!= head
.end();
905 orig_pred
= pred
[orig
];
908 vector
<const indicator_term
* > orig_lt
;
909 vector
<const indicator_term
* > orig_le
;
910 vector
<const indicator_term
* > orig_eq
;
911 vector
<const indicator_term
* > orig_pending
;
912 order_type::iterator i
;
913 bool in_lt
= ((i
= lt
.find(orig
)) != lt
.end());
915 orig_lt
= (*i
).second
;
918 bool in_le
= ((i
= le
.find(orig
)) != le
.end());
920 orig_le
= (*i
).second
;
923 bool in_eq
= ((i
= eq
.find(orig
)) != eq
.end());
925 orig_eq
= (*i
).second
;
928 bool in_pending
= ((i
= pending
.find(orig
)) != pending
.end());
930 orig_pending
= (*i
).second
;
933 indicator_term
*old
= const_cast<indicator_term
*>(orig
);
934 old
->swap(replacement
);
938 pred
[old
] = orig_pred
;
946 pending
[old
] = orig_pending
;
949 void partial_order::unset_le(const indicator_term
*a
, const indicator_term
*b
)
951 vector
<const indicator_term
*>::iterator i
;
952 i
= std::find(le
[a
].begin(), le
[a
].end(), b
);
954 if (le
[a
].size() == 0)
957 i
= std::find(pending
[a
].begin(), pending
[a
].end(), b
);
958 if (i
!= pending
[a
].end())
962 void partial_order::set_equal(const indicator_term
*a
, const indicator_term
*b
)
964 if (eq
[a
].size() == 0)
966 if (eq
[b
].size() == 0)
971 if (pred
.key_comp()(b
, a
)) {
972 const indicator_term
*c
= a
;
977 const indicator_term
*base
= a
;
979 order_type::iterator i
;
981 for (int j
= 0; j
< eq
[b
].size(); ++j
) {
982 eq
[base
].push_back(eq
[b
][j
]);
983 eq
[eq
[b
][j
]][0] = base
;
989 for (int j
= 0; j
< lt
[b
].size(); ++j
) {
990 if (std::find(eq
[base
].begin(), eq
[base
].end(), lt
[b
][j
]) != eq
[base
].end())
992 else if (std::find(lt
[base
].begin(), lt
[base
].end(), lt
[b
][j
])
996 lt
[base
].push_back(lt
[b
][j
]);
1002 if (i
!= le
.end()) {
1003 for (int j
= 0; j
< le
[b
].size(); ++j
) {
1004 if (std::find(eq
[base
].begin(), eq
[base
].end(), le
[b
][j
]) != eq
[base
].end())
1006 else if (std::find(le
[base
].begin(), le
[base
].end(), le
[b
][j
])
1010 le
[base
].push_back(le
[b
][j
]);
1015 i
= pending
.find(base
);
1016 if (i
!= pending
.end()) {
1017 vector
<const indicator_term
* > old
= pending
[base
];
1018 pending
[base
].clear();
1019 for (int j
= 0; j
< old
.size(); ++j
) {
1020 if (std::find(eq
[base
].begin(), eq
[base
].end(), old
[j
]) == eq
[base
].end())
1021 pending
[base
].push_back(old
[j
]);
1025 i
= pending
.find(b
);
1026 if (i
!= pending
.end()) {
1027 for (int j
= 0; j
< pending
[b
].size(); ++j
) {
1028 if (std::find(eq
[base
].begin(), eq
[base
].end(), pending
[b
][j
]) == eq
[base
].end())
1029 pending
[base
].push_back(pending
[b
][j
]);
1035 void partial_order::copy(const partial_order
& order
,
1036 map
< const indicator_term
*, indicator_term
* > old2new
)
1038 cache
.copy(order
.cache
);
1040 order_type::const_iterator i
;
1041 pred_type::const_iterator j
;
1042 head_type::const_iterator k
;
1044 for (k
= order
.head
.begin(); k
!= order
.head
.end(); ++k
)
1045 head
.insert(old2new
[*k
]);
1047 for (j
= order
.pred
.begin(); j
!= order
.pred
.end(); ++j
)
1048 pred
[old2new
[(*j
).first
]] = (*j
).second
;
1050 for (i
= order
.lt
.begin(); i
!= order
.lt
.end(); ++i
) {
1051 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1052 lt
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1054 for (i
= order
.le
.begin(); i
!= order
.le
.end(); ++i
) {
1055 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1056 le
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1058 for (i
= order
.eq
.begin(); i
!= order
.eq
.end(); ++i
) {
1059 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1060 eq
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1062 for (i
= order
.pending
.begin(); i
!= order
.pending
.end(); ++i
) {
1063 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1064 pending
[old2new
[(*i
).first
]].push_back(old2new
[(*i
).second
[j
]]);
1070 vector
<evalue
*> max
;
1072 void print(ostream
& os
, char **p
, barvinok_options
*options
) const;
1073 void substitute(Matrix
*T
, barvinok_options
*options
);
1074 Vector
*eval(Value
*val
, unsigned MaxRays
) const;
1077 for (int i
= 0; i
< max
.size(); ++i
) {
1078 free_evalue_refs(max
[i
]);
1086 * Project on first dim dimensions
1088 Polyhedron
* Polyhedron_Project_Initial(Polyhedron
*P
, int dim
)
1094 if (P
->Dimension
== dim
)
1095 return Polyhedron_Copy(P
);
1097 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
1098 for (i
= 0; i
< dim
; ++i
)
1099 value_set_si(T
->p
[i
][i
], 1);
1100 value_set_si(T
->p
[dim
][P
->Dimension
], 1);
1101 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
1107 vector
<indicator_term
*> term
;
1108 indicator_constructor
& ic
;
1109 partial_order order
;
1113 lexmin_options
*options
;
1114 vector
<evalue
*> substitutions
;
1116 indicator(indicator_constructor
& ic
, Param_Domain
*PD
, EDomain
*D
,
1117 lexmin_options
*options
) :
1118 ic(ic
), PD(PD
), D(D
), order(this), options(options
), P(NULL
) {}
1119 indicator(const indicator
& ind
, EDomain
*D
) :
1120 ic(ind
.ic
), PD(ind
.PD
), D(NULL
), order(this), options(ind
.options
),
1121 P(Polyhedron_Copy(ind
.P
)) {
1122 map
< const indicator_term
*, indicator_term
* > old2new
;
1123 for (int i
= 0; i
< ind
.term
.size(); ++i
) {
1124 indicator_term
*it
= new indicator_term(*ind
.term
[i
]);
1125 old2new
[ind
.term
[i
]] = it
;
1128 order
.copy(ind
.order
, old2new
);
1132 for (int i
= 0; i
< term
.size(); ++i
)
1140 void set_domain(EDomain
*D
) {
1141 order
.cache
.clear_transients();
1145 int nparam
= ic
.PP
->Constraints
->NbColumns
-2 - ic
.vertex
.length();
1146 if (options
->reduce
) {
1147 Polyhedron
*Q
= Polyhedron_Project_Initial(D
->D
, nparam
);
1148 Q
= DomainConstraintSimplify(Q
, options
->verify
->barvinok
->MaxRays
);
1149 if (!P
|| !PolyhedronIncludes(Q
, P
))
1150 reduce_in_domain(Q
);
1158 void add(const indicator_term
* it
);
1159 void remove(const indicator_term
* it
);
1160 void remove_initial_rational_vertices();
1161 void expand_rational_vertex(const indicator_term
*initial
);
1163 void print(ostream
& os
, char **p
);
1165 void peel(int i
, int j
);
1166 void combine(const indicator_term
*a
, const indicator_term
*b
);
1167 void add_substitution(evalue
*equation
);
1168 void perform_pending_substitutions();
1169 void reduce_in_domain(Polyhedron
*D
);
1170 bool handle_equal_numerators(const indicator_term
*base
);
1172 max_term
* create_max_term(const indicator_term
*it
);
1174 void substitute(evalue
*equation
);
1177 void partial_order::sanity_check() const
1179 order_type::const_iterator i
;
1180 order_type::const_iterator prev
;
1181 order_type::const_iterator l
;
1182 pred_type::const_iterator k
, prev_k
;
1184 for (k
= pred
.begin(); k
!= pred
.end(); prev_k
= k
, ++k
)
1185 if (k
!= pred
.begin())
1186 assert(pred
.key_comp()((*prev_k
).first
, (*k
).first
));
1187 for (i
= lt
.begin(); i
!= lt
.end(); prev
= i
, ++i
) {
1190 i_v
= (*i
).first
->eval(ind
->D
->sample
->p
);
1191 if (i
!= lt
.begin())
1192 assert(lt
.key_comp()((*prev
).first
, (*i
).first
));
1193 l
= eq
.find((*i
).first
);
1195 assert((*l
).second
.size() > 1);
1196 assert(head
.find((*i
).first
) != head
.end() ||
1197 pred
.find((*i
).first
) != pred
.end());
1198 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1199 k
= pred
.find((*i
).second
[j
]);
1200 assert(k
!= pred
.end());
1201 assert((*k
).second
!= 0);
1202 if ((*i
).first
->sign
!= 0 &&
1203 (*i
).second
[j
]->sign
!= 0 && ind
->D
->sample
) {
1204 vec_ZZ j_v
= (*i
).second
[j
]->eval(ind
->D
->sample
->p
);
1205 assert(lex_cmp(i_v
, j_v
) < 0);
1209 for (i
= le
.begin(); i
!= le
.end(); ++i
) {
1210 assert((*i
).second
.size() > 0);
1211 assert(head
.find((*i
).first
) != head
.end() ||
1212 pred
.find((*i
).first
) != pred
.end());
1213 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1214 k
= pred
.find((*i
).second
[j
]);
1215 assert(k
!= pred
.end());
1216 assert((*k
).second
!= 0);
1219 for (i
= eq
.begin(); i
!= eq
.end(); ++i
) {
1220 assert(head
.find((*i
).first
) != head
.end() ||
1221 pred
.find((*i
).first
) != pred
.end());
1222 assert((*i
).second
.size() >= 1);
1224 for (i
= pending
.begin(); i
!= pending
.end(); ++i
) {
1225 assert(head
.find((*i
).first
) != head
.end() ||
1226 pred
.find((*i
).first
) != pred
.end());
1227 for (int j
= 0; j
< (*i
).second
.size(); ++j
)
1228 assert(head
.find((*i
).second
[j
]) != head
.end() ||
1229 pred
.find((*i
).second
[j
]) != pred
.end());
1233 max_term
* indicator::create_max_term(const indicator_term
*it
)
1235 int dim
= it
->den
.NumCols();
1236 int nparam
= ic
.PP
->Constraints
->NbColumns
-2 - ic
.vertex
.length();
1237 max_term
*maximum
= new max_term
;
1238 maximum
->domain
= new EDomain(D
);
1239 for (int j
= 0; j
< dim
; ++j
) {
1240 evalue
*E
= new evalue
;
1242 evalue_copy(E
, it
->vertex
[j
]);
1243 if (evalue_frac2floor_in_domain(E
, D
->D
))
1245 maximum
->max
.push_back(E
);
1250 static order_sign
evalue_sign(evalue
*diff
, EDomain
*D
, barvinok_options
*options
)
1252 order_sign sign
= order_eq
;
1255 evalue_set_si(&mone
, -1, 1);
1256 int len
= 1 + D
->D
->Dimension
+ 1;
1257 Vector
*c
= Vector_Alloc(len
);
1258 Matrix
*T
= Matrix_Alloc(2, len
-1);
1260 int fract
= evalue2constraint(D
, diff
, c
->p
, len
);
1261 Vector_Copy(c
->p
+1, T
->p
[0], len
-1);
1262 value_assign(T
->p
[1][len
-2], c
->p
[0]);
1264 order_sign upper_sign
= polyhedron_affine_sign(D
->D
, T
, options
);
1265 if (upper_sign
== order_lt
|| !fract
)
1269 evalue2constraint(D
, diff
, c
->p
, len
);
1271 Vector_Copy(c
->p
+1, T
->p
[0], len
-1);
1272 value_assign(T
->p
[1][len
-2], c
->p
[0]);
1274 order_sign neg_lower_sign
= polyhedron_affine_sign(D
->D
, T
, options
);
1276 if (neg_lower_sign
== order_lt
)
1278 else if (neg_lower_sign
== order_eq
|| neg_lower_sign
== order_le
) {
1279 if (upper_sign
== order_eq
|| upper_sign
== order_le
)
1284 if (upper_sign
== order_lt
|| upper_sign
== order_le
||
1285 upper_sign
== order_eq
)
1288 sign
= order_unknown
;
1294 free_evalue_refs(&mone
);
1299 /* An auxiliary class that keeps a reference to an evalue
1300 * and frees it when it goes out of scope.
1302 struct temp_evalue
{
1304 temp_evalue() : E(NULL
) {}
1305 temp_evalue(evalue
*e
) : E(e
) {}
1306 operator evalue
* () const { return E
; }
1307 evalue
*operator=(evalue
*e
) {
1309 free_evalue_refs(E
);
1317 free_evalue_refs(E
);
1323 static void substitute(vector
<indicator_term
*>& term
, evalue
*equation
)
1325 evalue
*fract
= NULL
;
1326 evalue
*val
= new evalue
;
1328 evalue_copy(val
, equation
);
1331 value_init(factor
.d
);
1332 value_init(factor
.x
.n
);
1335 for (e
= val
; value_zero_p(e
->d
) && e
->x
.p
->type
!= fractional
;
1336 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)])
1339 if (value_zero_p(e
->d
) && e
->x
.p
->type
== fractional
)
1340 fract
= &e
->x
.p
->arr
[0];
1342 for (e
= val
; value_zero_p(e
->d
) && e
->x
.p
->type
!= polynomial
;
1343 e
= &e
->x
.p
->arr
[type_offset(e
->x
.p
)])
1345 assert(value_zero_p(e
->d
) && e
->x
.p
->type
== polynomial
);
1348 int offset
= type_offset(e
->x
.p
);
1350 assert(value_notzero_p(e
->x
.p
->arr
[offset
+1].d
));
1351 assert(value_notzero_p(e
->x
.p
->arr
[offset
+1].x
.n
));
1352 if (value_neg_p(e
->x
.p
->arr
[offset
+1].x
.n
)) {
1353 value_oppose(factor
.d
, e
->x
.p
->arr
[offset
+1].x
.n
);
1354 value_assign(factor
.x
.n
, e
->x
.p
->arr
[offset
+1].d
);
1356 value_assign(factor
.d
, e
->x
.p
->arr
[offset
+1].x
.n
);
1357 value_oppose(factor
.x
.n
, e
->x
.p
->arr
[offset
+1].d
);
1360 free_evalue_refs(&e
->x
.p
->arr
[offset
+1]);
1363 *e
= e
->x
.p
->arr
[offset
];
1368 for (int i
= 0; i
< term
.size(); ++i
)
1369 term
[i
]->substitute(fract
, val
);
1371 free_evalue_refs(&p
->arr
[0]);
1373 for (int i
= 0; i
< term
.size(); ++i
)
1374 term
[i
]->substitute(p
->pos
, val
);
1377 free_evalue_refs(&factor
);
1378 free_evalue_refs(val
);
1384 order_sign
partial_order::compare(const indicator_term
*a
, const indicator_term
*b
)
1386 unsigned dim
= a
->den
.NumCols();
1387 order_sign sign
= order_eq
;
1388 bool rational
= a
->sign
== 0 || b
->sign
== 0;
1390 order_sign cached_sign
= order_eq
;
1391 for (int k
= 0; k
< dim
; ++k
) {
1392 cached_sign
= evalue_diff_constant_sign(a
->vertex
[k
], b
->vertex
[k
]);
1393 if (cached_sign
!= order_eq
)
1396 if (cached_sign
!= order_undefined
)
1399 order_cache_el cache_el
;
1400 cached_sign
= order_undefined
;
1402 cached_sign
= cache
.check(a
, b
, cache_el
);
1403 if (cached_sign
!= order_undefined
) {
1410 vector
<indicator_term
*> term
;
1412 for (int k
= 0; k
< dim
; ++k
) {
1413 /* compute a->vertex[k] - b->vertex[k] */
1415 if (cache_el
.e
.size() <= k
) {
1416 diff
= ediff(a
->vertex
[k
], b
->vertex
[k
]);
1417 cache_el
.e
.push_back(diff
);
1419 diff
= cache_el
.e
[k
];
1422 tdiff
= diff
= ediff(term
[0]->vertex
[k
], term
[1]->vertex
[k
]);
1423 order_sign diff_sign
;
1424 if (eequal(a
->vertex
[k
], b
->vertex
[k
]))
1425 diff_sign
= order_eq
;
1427 diff_sign
= evalue_sign(diff
, ind
->D
,
1428 ind
->options
->verify
->barvinok
);
1430 if (diff_sign
== order_undefined
) {
1431 assert(sign
== order_le
|| sign
== order_ge
);
1432 if (sign
== order_le
)
1438 if (diff_sign
== order_lt
) {
1439 if (sign
== order_eq
|| sign
== order_le
)
1442 sign
= order_unknown
;
1445 if (diff_sign
== order_gt
) {
1446 if (sign
== order_eq
|| sign
== order_ge
)
1449 sign
= order_unknown
;
1452 if (diff_sign
== order_eq
) {
1453 if (term
.size() == 0 && !rational
&& !EVALUE_IS_ZERO(*diff
))
1454 ind
->add_substitution(diff
);
1457 if ((diff_sign
== order_unknown
) ||
1458 ((diff_sign
== order_lt
|| diff_sign
== order_le
) && sign
== order_ge
) ||
1459 ((diff_sign
== order_gt
|| diff_sign
== order_ge
) && sign
== order_le
)) {
1460 sign
= order_unknown
;
1467 term
.push_back(new indicator_term(*a
));
1468 term
.push_back(new indicator_term(*b
));
1470 substitute(term
, diff
);
1474 cache
.add(cache_el
, sign
);
1486 bool partial_order::compared(const indicator_term
* a
, const indicator_term
* b
)
1488 order_type::iterator j
;
1491 if (j
!= lt
.end() && std::find(lt
[a
].begin(), lt
[a
].end(), b
) != lt
[a
].end())
1495 if (j
!= le
.end() && std::find(le
[a
].begin(), le
[a
].end(), b
) != le
[a
].end())
1501 void partial_order::add(const indicator_term
* it
,
1502 std::set
<const indicator_term
*> *filter
)
1504 if (eq
.find(it
) != eq
.end() && eq
[it
].size() == 1)
1507 head_type
head_copy(head
);
1512 head_type::iterator i
;
1513 for (i
= head_copy
.begin(); i
!= head_copy
.end(); ++i
) {
1516 if (eq
.find(*i
) != eq
.end() && eq
[*i
].size() == 1)
1519 if (filter
->find(*i
) == filter
->end())
1521 if (compared(*i
, it
))
1524 order_sign sign
= compare(it
, *i
);
1525 if (sign
== order_lt
) {
1526 lt
[it
].push_back(*i
);
1528 } else if (sign
== order_le
) {
1529 le
[it
].push_back(*i
);
1531 } else if (sign
== order_eq
) {
1534 } else if (sign
== order_gt
) {
1535 pending
[*i
].push_back(it
);
1536 lt
[*i
].push_back(it
);
1538 } else if (sign
== order_ge
) {
1539 pending
[*i
].push_back(it
);
1540 le
[*i
].push_back(it
);
1546 void partial_order::remove(const indicator_term
* it
)
1548 std::set
<const indicator_term
*> filter
;
1549 order_type::iterator i
;
1551 assert(head
.find(it
) != head
.end());
1554 if (i
!= eq
.end()) {
1555 assert(eq
[it
].size() >= 1);
1556 const indicator_term
*base
;
1557 if (eq
[it
].size() == 1) {
1561 vector
<const indicator_term
* >::iterator j
;
1562 j
= std::find(eq
[base
].begin(), eq
[base
].end(), it
);
1563 assert(j
!= eq
[base
].end());
1566 /* "it" may no longer be the smallest, since the order
1567 * structure may have been copied from another one.
1569 std::sort(eq
[it
].begin()+1, eq
[it
].end(), pred
.key_comp());
1570 assert(eq
[it
][0] == it
);
1571 eq
[it
].erase(eq
[it
].begin());
1576 for (int j
= 1; j
< eq
[base
].size(); ++j
)
1577 eq
[eq
[base
][j
]][0] = base
;
1580 if (i
!= lt
.end()) {
1586 if (i
!= le
.end()) {
1591 i
= pending
.find(it
);
1592 if (i
!= pending
.end()) {
1593 pending
[base
] = pending
[it
];
1598 if (eq
[base
].size() == 1)
1607 if (i
!= lt
.end()) {
1608 for (int j
= 0; j
< lt
[it
].size(); ++j
) {
1609 filter
.insert(lt
[it
][j
]);
1610 dec_pred(lt
[it
][j
]);
1616 if (i
!= le
.end()) {
1617 for (int j
= 0; j
< le
[it
].size(); ++j
) {
1618 filter
.insert(le
[it
][j
]);
1619 dec_pred(le
[it
][j
]);
1626 i
= pending
.find(it
);
1627 if (i
!= pending
.end()) {
1628 vector
<const indicator_term
*> it_pending
= pending
[it
];
1630 for (int j
= 0; j
< it_pending
.size(); ++j
) {
1631 filter
.erase(it_pending
[j
]);
1632 add(it_pending
[j
], &filter
);
1637 void partial_order::print(ostream
& os
, char **p
)
1639 order_type::iterator i
;
1640 pred_type::iterator j
;
1641 head_type::iterator k
;
1642 for (k
= head
.begin(); k
!= head
.end(); ++k
) {
1646 for (j
= pred
.begin(); j
!= pred
.end(); ++j
) {
1647 (*j
).first
->print(os
, p
);
1648 os
<< ": " << (*j
).second
<< endl
;
1650 for (i
= lt
.begin(); i
!= lt
.end(); ++i
) {
1651 (*i
).first
->print(os
, p
);
1652 assert(head
.find((*i
).first
) != head
.end() ||
1653 pred
.find((*i
).first
) != pred
.end());
1654 if (pred
.find((*i
).first
) != pred
.end())
1655 os
<< "(" << pred
[(*i
).first
] << ")";
1657 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1660 (*i
).second
[j
]->print(os
, p
);
1661 assert(pred
.find((*i
).second
[j
]) != pred
.end());
1662 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1666 for (i
= le
.begin(); i
!= le
.end(); ++i
) {
1667 (*i
).first
->print(os
, p
);
1668 assert(head
.find((*i
).first
) != head
.end() ||
1669 pred
.find((*i
).first
) != pred
.end());
1670 if (pred
.find((*i
).first
) != pred
.end())
1671 os
<< "(" << pred
[(*i
).first
] << ")";
1673 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1676 (*i
).second
[j
]->print(os
, p
);
1677 assert(pred
.find((*i
).second
[j
]) != pred
.end());
1678 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1682 for (i
= eq
.begin(); i
!= eq
.end(); ++i
) {
1683 if ((*i
).second
.size() <= 1)
1685 (*i
).first
->print(os
, p
);
1686 assert(head
.find((*i
).first
) != head
.end() ||
1687 pred
.find((*i
).first
) != pred
.end());
1688 if (pred
.find((*i
).first
) != pred
.end())
1689 os
<< "(" << pred
[(*i
).first
] << ")";
1690 for (int j
= 1; j
< (*i
).second
.size(); ++j
) {
1693 (*i
).second
[j
]->print(os
, p
);
1694 assert(head
.find((*i
).second
[j
]) != head
.end() ||
1695 pred
.find((*i
).second
[j
]) != pred
.end());
1696 if (pred
.find((*i
).second
[j
]) != pred
.end())
1697 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1701 for (i
= pending
.begin(); i
!= pending
.end(); ++i
) {
1702 os
<< "pending on ";
1703 (*i
).first
->print(os
, p
);
1704 assert(head
.find((*i
).first
) != head
.end() ||
1705 pred
.find((*i
).first
) != pred
.end());
1706 if (pred
.find((*i
).first
) != pred
.end())
1707 os
<< "(" << pred
[(*i
).first
] << ")";
1709 for (int j
= 0; j
< (*i
).second
.size(); ++j
) {
1712 (*i
).second
[j
]->print(os
, p
);
1713 assert(pred
.find((*i
).second
[j
]) != pred
.end());
1714 os
<< "(" << pred
[(*i
).second
[j
]] << ")";
1720 void indicator::add(const indicator_term
* it
)
1722 indicator_term
*nt
= new indicator_term(*it
);
1723 if (options
->reduce
)
1724 nt
->reduce_in_domain(P
? P
: D
->D
);
1726 order
.add(nt
, NULL
);
1727 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1730 void indicator::remove(const indicator_term
* it
)
1732 vector
<indicator_term
*>::iterator i
;
1733 i
= std::find(term
.begin(), term
.end(), it
);
1734 assert(i
!= term
.end());
1737 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1741 void indicator::expand_rational_vertex(const indicator_term
*initial
)
1743 int pos
= initial
->pos
;
1745 if (ic
.terms
[pos
].size() == 0) {
1747 FORALL_PVertex_in_ParamPolyhedron(V
, PD
, ic
.PP
) // _i is internal counter
1749 ic
.decompose_at_vertex(V
, pos
, options
->verify
->barvinok
);
1752 END_FORALL_PVertex_in_ParamPolyhedron
;
1754 for (int j
= 0; j
< ic
.terms
[pos
].size(); ++j
)
1755 add(ic
.terms
[pos
][j
]);
1758 void indicator::remove_initial_rational_vertices()
1761 const indicator_term
*initial
= NULL
;
1762 partial_order::head_type::iterator i
;
1763 for (i
= order
.head
.begin(); i
!= order
.head
.end(); ++i
) {
1764 if ((*i
)->sign
!= 0)
1766 if (order
.eq
.find(*i
) != order
.eq
.end() && order
.eq
[*i
].size() <= 1)
1773 expand_rational_vertex(initial
);
1777 void indicator::reduce_in_domain(Polyhedron
*D
)
1779 for (int i
= 0; i
< term
.size(); ++i
)
1780 term
[i
]->reduce_in_domain(D
);
1783 void indicator::print(ostream
& os
, char **p
)
1785 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1786 for (int i
= 0; i
< term
.size(); ++i
) {
1787 term
[i
]->print(os
, p
);
1789 os
<< ": " << term
[i
]->eval(D
->sample
->p
);
1796 /* Remove pairs of opposite terms */
1797 void indicator::simplify()
1799 for (int i
= 0; i
< term
.size(); ++i
) {
1800 for (int j
= i
+1; j
< term
.size(); ++j
) {
1801 if (term
[i
]->sign
+ term
[j
]->sign
!= 0)
1803 if (term
[i
]->den
!= term
[j
]->den
)
1806 for (k
= 0; k
< term
[i
]->den
.NumCols(); ++k
)
1807 if (!eequal(term
[i
]->vertex
[k
], term
[j
]->vertex
[k
]))
1809 if (k
< term
[i
]->den
.NumCols())
1813 term
.erase(term
.begin()+j
);
1814 term
.erase(term
.begin()+i
);
1821 void indicator::peel(int i
, int j
)
1829 int dim
= term
[i
]->den
.NumCols();
1834 int n_common
= 0, n_i
= 0, n_j
= 0;
1836 common
.SetDims(min(term
[i
]->den
.NumRows(), term
[j
]->den
.NumRows()), dim
);
1837 rest_i
.SetDims(term
[i
]->den
.NumRows(), dim
);
1838 rest_j
.SetDims(term
[j
]->den
.NumRows(), dim
);
1841 for (k
= 0, l
= 0; k
< term
[i
]->den
.NumRows() && l
< term
[j
]->den
.NumRows(); ) {
1842 int s
= lex_cmp(term
[i
]->den
[k
], term
[j
]->den
[l
]);
1844 common
[n_common
++] = term
[i
]->den
[k
];
1848 rest_i
[n_i
++] = term
[i
]->den
[k
++];
1850 rest_j
[n_j
++] = term
[j
]->den
[l
++];
1852 while (k
< term
[i
]->den
.NumRows())
1853 rest_i
[n_i
++] = term
[i
]->den
[k
++];
1854 while (l
< term
[j
]->den
.NumRows())
1855 rest_j
[n_j
++] = term
[j
]->den
[l
++];
1856 common
.SetDims(n_common
, dim
);
1857 rest_i
.SetDims(n_i
, dim
);
1858 rest_j
.SetDims(n_j
, dim
);
1860 for (k
= 0; k
<= n_i
; ++k
) {
1861 indicator_term
*it
= new indicator_term(*term
[i
]);
1862 it
->den
.SetDims(n_common
+ k
, dim
);
1863 for (l
= 0; l
< n_common
; ++l
)
1864 it
->den
[l
] = common
[l
];
1865 for (l
= 0; l
< k
; ++l
)
1866 it
->den
[n_common
+l
] = rest_i
[l
];
1867 lex_order_rows(it
->den
);
1869 for (l
= 0; l
< dim
; ++l
)
1870 evalue_add_constant(it
->vertex
[l
], rest_i
[k
-1][l
]);
1874 for (k
= 0; k
<= n_j
; ++k
) {
1875 indicator_term
*it
= new indicator_term(*term
[j
]);
1876 it
->den
.SetDims(n_common
+ k
, dim
);
1877 for (l
= 0; l
< n_common
; ++l
)
1878 it
->den
[l
] = common
[l
];
1879 for (l
= 0; l
< k
; ++l
)
1880 it
->den
[n_common
+l
] = rest_j
[l
];
1881 lex_order_rows(it
->den
);
1883 for (l
= 0; l
< dim
; ++l
)
1884 evalue_add_constant(it
->vertex
[l
], rest_j
[k
-1][l
]);
1887 term
.erase(term
.begin()+j
);
1888 term
.erase(term
.begin()+i
);
1891 void indicator::combine(const indicator_term
*a
, const indicator_term
*b
)
1893 int dim
= a
->den
.NumCols();
1896 mat_ZZ rest_i
; /* factors in a, but not in b */
1897 mat_ZZ rest_j
; /* factors in b, but not in a */
1898 int n_common
= 0, n_i
= 0, n_j
= 0;
1900 common
.SetDims(min(a
->den
.NumRows(), b
->den
.NumRows()), dim
);
1901 rest_i
.SetDims(a
->den
.NumRows(), dim
);
1902 rest_j
.SetDims(b
->den
.NumRows(), dim
);
1905 for (k
= 0, l
= 0; k
< a
->den
.NumRows() && l
< b
->den
.NumRows(); ) {
1906 int s
= lex_cmp(a
->den
[k
], b
->den
[l
]);
1908 common
[n_common
++] = a
->den
[k
];
1912 rest_i
[n_i
++] = a
->den
[k
++];
1914 rest_j
[n_j
++] = b
->den
[l
++];
1916 while (k
< a
->den
.NumRows())
1917 rest_i
[n_i
++] = a
->den
[k
++];
1918 while (l
< b
->den
.NumRows())
1919 rest_j
[n_j
++] = b
->den
[l
++];
1920 common
.SetDims(n_common
, dim
);
1921 rest_i
.SetDims(n_i
, dim
);
1922 rest_j
.SetDims(n_j
, dim
);
1924 assert(order
.eq
[a
].size() > 1);
1925 indicator_term
*prev
;
1928 for (int k
= n_i
-1; k
>= 0; --k
) {
1929 indicator_term
*it
= new indicator_term(*b
);
1930 it
->den
.SetDims(n_common
+ n_j
+ n_i
-k
, dim
);
1931 for (int l
= k
; l
< n_i
; ++l
)
1932 it
->den
[n_common
+n_j
+l
-k
] = rest_i
[l
];
1933 lex_order_rows(it
->den
);
1934 for (int m
= 0; m
< dim
; ++m
)
1935 evalue_add_constant(it
->vertex
[m
], rest_i
[k
][m
]);
1936 it
->sign
= -it
->sign
;
1938 order
.pending
[it
].push_back(prev
);
1939 order
.lt
[it
].push_back(prev
);
1940 order
.inc_pred(prev
);
1943 order
.head
.insert(it
);
1947 indicator_term
*it
= new indicator_term(*b
);
1948 it
->den
.SetDims(n_common
+ n_i
+ n_j
, dim
);
1949 for (l
= 0; l
< n_i
; ++l
)
1950 it
->den
[n_common
+n_j
+l
] = rest_i
[l
];
1951 lex_order_rows(it
->den
);
1953 order
.pending
[a
].push_back(prev
);
1954 order
.lt
[a
].push_back(prev
);
1955 order
.inc_pred(prev
);
1956 order
.replace(b
, it
);
1961 for (int k
= n_j
-1; k
>= 0; --k
) {
1962 indicator_term
*it
= new indicator_term(*a
);
1963 it
->den
.SetDims(n_common
+ n_i
+ n_j
-k
, dim
);
1964 for (int l
= k
; l
< n_j
; ++l
)
1965 it
->den
[n_common
+n_i
+l
-k
] = rest_j
[l
];
1966 lex_order_rows(it
->den
);
1967 for (int m
= 0; m
< dim
; ++m
)
1968 evalue_add_constant(it
->vertex
[m
], rest_j
[k
][m
]);
1969 it
->sign
= -it
->sign
;
1971 order
.pending
[it
].push_back(prev
);
1972 order
.lt
[it
].push_back(prev
);
1973 order
.inc_pred(prev
);
1976 order
.head
.insert(it
);
1980 indicator_term
*it
= new indicator_term(*a
);
1981 it
->den
.SetDims(n_common
+ n_i
+ n_j
, dim
);
1982 for (l
= 0; l
< n_j
; ++l
)
1983 it
->den
[n_common
+n_i
+l
] = rest_j
[l
];
1984 lex_order_rows(it
->den
);
1986 order
.pending
[a
].push_back(prev
);
1987 order
.lt
[a
].push_back(prev
);
1988 order
.inc_pred(prev
);
1989 order
.replace(a
, it
);
1993 assert(term
.size() == order
.head
.size() + order
.pred
.size());
1996 bool indicator::handle_equal_numerators(const indicator_term
*base
)
1998 for (int i
= 0; i
< order
.eq
[base
].size(); ++i
) {
1999 for (int j
= i
+1; j
< order
.eq
[base
].size(); ++j
) {
2000 if (order
.eq
[base
][i
]->is_opposite(order
.eq
[base
][j
])) {
2001 remove(order
.eq
[base
][j
]);
2002 remove(i
? order
.eq
[base
][i
] : base
);
2007 for (int j
= 1; j
< order
.eq
[base
].size(); ++j
)
2008 if (order
.eq
[base
][j
]->sign
!= base
->sign
) {
2009 combine(base
, order
.eq
[base
][j
]);
2015 void indicator::substitute(evalue
*equation
)
2017 ::substitute(term
, equation
);
2020 void indicator::add_substitution(evalue
*equation
)
2022 for (int i
= 0; i
< substitutions
.size(); ++i
)
2023 if (eequal(substitutions
[i
], equation
))
2025 evalue
*copy
= new evalue();
2026 value_init(copy
->d
);
2027 evalue_copy(copy
, equation
);
2028 substitutions
.push_back(copy
);
2031 void indicator::perform_pending_substitutions()
2033 if (substitutions
.size() == 0)
2036 for (int i
= 0; i
< substitutions
.size(); ++i
) {
2037 substitute(substitutions
[i
]);
2038 free_evalue_refs(substitutions
[i
]);
2039 delete substitutions
[i
];
2041 substitutions
.clear();
2045 static void print_varlist(ostream
& os
, int n
, char **names
)
2049 for (i
= 0; i
< n
; ++i
) {
2057 void max_term::print(ostream
& os
, char **p
, barvinok_options
*options
) const
2060 print_varlist(os
, domain
->dimension(), p
);
2063 for (int i
= 0; i
< max
.size(); ++i
) {
2066 evalue_print(os
, max
[i
], p
);
2070 domain
->print_constraints(os
, p
, options
);
2074 /* T maps the compressed parameters to the original parameters,
2075 * while this max_term is based on the compressed parameters
2076 * and we want get the original parameters back.
2078 void max_term::substitute(Matrix
*T
, barvinok_options
*options
)
2080 assert(domain
->dimension() == T
->NbColumns
-1);
2081 int nexist
= domain
->D
->Dimension
- (T
->NbColumns
-1);
2083 Matrix
*inv
= left_inverse(T
, &Eq
);
2086 value_init(denom
.d
);
2087 value_init(denom
.x
.n
);
2088 value_set_si(denom
.x
.n
, 1);
2089 value_assign(denom
.d
, inv
->p
[inv
->NbRows
-1][inv
->NbColumns
-1]);
2092 v
.SetLength(inv
->NbColumns
);
2093 evalue
**subs
= new evalue
*[inv
->NbRows
-1];
2094 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
2095 values2zz(inv
->p
[i
], v
, v
.length());
2096 subs
[i
] = multi_monom(v
);
2097 emul(&denom
, subs
[i
]);
2099 free_evalue_refs(&denom
);
2101 domain
->substitute(subs
, inv
, Eq
, options
->MaxRays
);
2104 for (int i
= 0; i
< max
.size(); ++i
) {
2105 evalue_substitute(max
[i
], subs
);
2106 reduce_evalue(max
[i
]);
2109 for (int i
= 0; i
< inv
->NbRows
-1; ++i
) {
2110 free_evalue_refs(subs
[i
]);
2117 Vector
*max_term::eval(Value
*val
, unsigned MaxRays
) const
2119 if (!domain
->contains(val
, domain
->dimension()))
2121 Vector
*res
= Vector_Alloc(max
.size());
2122 for (int i
= 0; i
< max
.size(); ++i
) {
2123 compute_evalue(max
[i
], val
, &res
->p
[i
]);
2130 enum sign
{ le
, ge
, lge
} sign
;
2132 split (evalue
*c
, enum sign s
) : constraint(c
), sign(s
) {}
2135 static void split_on(const split
& sp
, EDomain
*D
,
2136 EDomain
**Dlt
, EDomain
**Deq
, EDomain
**Dgt
,
2137 lexmin_options
*options
)
2143 ge_constraint
*ge
= D
->compute_ge_constraint(sp
.constraint
);
2144 if (sp
.sign
== split::lge
|| sp
.sign
== split::ge
)
2145 ED
[2] = EDomain::new_from_ge_constraint(ge
, 1, options
->verify
->barvinok
);
2148 if (sp
.sign
== split::lge
|| sp
.sign
== split::le
)
2149 ED
[0] = EDomain::new_from_ge_constraint(ge
, -1, options
->verify
->barvinok
);
2153 assert(sp
.sign
== split::lge
|| sp
.sign
== split::ge
|| sp
.sign
== split::le
);
2154 ED
[1] = EDomain::new_from_ge_constraint(ge
, 0, options
->verify
->barvinok
);
2158 for (int i
= 0; i
< 3; ++i
) {
2161 if (D
->sample
&& ED
[i
]->contains(D
->sample
->p
, D
->sample
->Size
-1)) {
2162 ED
[i
]->sample
= Vector_Alloc(D
->sample
->Size
);
2163 Vector_Copy(D
->sample
->p
, ED
[i
]->sample
->p
, D
->sample
->Size
);
2164 } else if (emptyQ2(ED
[i
]->D
) ||
2165 (options
->emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_NONE
&&
2166 !(ED
[i
]->not_empty(options
)))) {
2176 ostream
& operator<< (ostream
& os
, const vector
<int> & v
)
2179 for (int i
= 0; i
< v
.size(); ++i
) {
2188 void construct_rational_vertices(Param_Polyhedron
*PP
, Matrix
*T
, unsigned dim
,
2189 int nparam
, vector
<indicator_term
*>& vertices
)
2198 v
.SetLength(nparam
+1);
2201 value_init(factor
.d
);
2202 value_init(factor
.x
.n
);
2203 value_set_si(factor
.x
.n
, 1);
2204 value_set_si(factor
.d
, 1);
2206 for (i
= 0, PV
= PP
->V
; PV
; ++i
, PV
= PV
->next
) {
2207 indicator_term
*term
= new indicator_term(dim
, i
);
2208 vertices
.push_back(term
);
2209 Matrix
*M
= Matrix_Alloc(PV
->Vertex
->NbRows
+nparam
+1, nparam
+1);
2210 value_set_si(lcm
, 1);
2211 for (int j
= 0; j
< PV
->Vertex
->NbRows
; ++j
)
2212 value_lcm(lcm
, lcm
, PV
->Vertex
->p
[j
][nparam
+1]);
2213 value_assign(M
->p
[M
->NbRows
-1][M
->NbColumns
-1], lcm
);
2214 for (int j
= 0; j
< PV
->Vertex
->NbRows
; ++j
) {
2215 value_division(tmp
, lcm
, PV
->Vertex
->p
[j
][nparam
+1]);
2216 Vector_Scale(PV
->Vertex
->p
[j
], M
->p
[j
], tmp
, nparam
+1);
2218 for (int j
= 0; j
< nparam
; ++j
)
2219 value_assign(M
->p
[PV
->Vertex
->NbRows
+j
][j
], lcm
);
2221 Matrix
*M2
= Matrix_Alloc(T
->NbRows
, M
->NbColumns
);
2222 Matrix_Product(T
, M
, M2
);
2226 for (int j
= 0; j
< dim
; ++j
) {
2227 values2zz(M
->p
[j
], v
, nparam
+1);
2228 term
->vertex
[j
] = multi_monom(v
);
2229 value_assign(factor
.d
, lcm
);
2230 emul(&factor
, term
->vertex
[j
]);
2234 assert(i
== PP
->nbV
);
2235 free_evalue_refs(&factor
);
2240 static vector
<max_term
*> lexmin(indicator
& ind
, unsigned nparam
,
2243 vector
<max_term
*> maxima
;
2244 partial_order::head_type::iterator i
;
2245 vector
<int> best_score
;
2246 vector
<int> second_score
;
2247 vector
<int> neg_score
;
2250 ind
.perform_pending_substitutions();
2251 const indicator_term
*best
= NULL
, *second
= NULL
, *neg
= NULL
,
2252 *neg_eq
= NULL
, *neg_le
= NULL
;
2253 for (i
= ind
.order
.head
.begin(); i
!= ind
.order
.head
.end(); ++i
) {
2255 const indicator_term
*term
= *i
;
2256 if (term
->sign
== 0) {
2257 ind
.expand_rational_vertex(term
);
2261 if (ind
.order
.eq
.find(term
) != ind
.order
.eq
.end()) {
2263 if (ind
.order
.eq
[term
].size() <= 1)
2265 for (j
= 1; j
< ind
.order
.eq
[term
].size(); ++j
)
2266 if (ind
.order
.pred
.find(ind
.order
.eq
[term
][j
]) !=
2267 ind
.order
.pred
.end())
2269 if (j
< ind
.order
.eq
[term
].size())
2271 score
.push_back(ind
.order
.eq
[term
].size());
2274 if (ind
.order
.le
.find(term
) != ind
.order
.le
.end())
2275 score
.push_back(ind
.order
.le
[term
].size());
2278 if (ind
.order
.lt
.find(term
) != ind
.order
.lt
.end())
2279 score
.push_back(-ind
.order
.lt
[term
].size());
2283 if (term
->sign
> 0) {
2284 if (!best
|| score
< best_score
) {
2286 second_score
= best_score
;
2289 } else if (!second
|| score
< second_score
) {
2291 second_score
= score
;
2294 if (!neg_eq
&& ind
.order
.eq
.find(term
) != ind
.order
.eq
.end()) {
2295 for (int j
= 1; j
< ind
.order
.eq
[term
].size(); ++j
)
2296 if (ind
.order
.eq
[term
][j
]->sign
!= term
->sign
) {
2301 if (!neg_le
&& ind
.order
.le
.find(term
) != ind
.order
.le
.end())
2303 if (!neg
|| score
< neg_score
) {
2309 if (i
!= ind
.order
.head
.end())
2312 if (!best
&& neg_eq
) {
2313 assert(ind
.order
.eq
[neg_eq
].size() != 0);
2314 bool handled
= ind
.handle_equal_numerators(neg_eq
);
2319 if (!best
&& neg_le
) {
2320 /* The smallest term is negative and <= some positive term */
2326 /* apparently there can be negative initial term on empty domains */
2327 if (ind
.options
->emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_NONE
&&
2328 ind
.options
->verify
->barvinok
->lp_solver
== BV_LP_POLYLIB
)
2333 if (!second
&& !neg
) {
2334 const indicator_term
*rat
= NULL
;
2336 if (ind
.order
.le
.find(best
) == ind
.order
.le
.end()) {
2337 if (ind
.order
.eq
.find(best
) != ind
.order
.eq
.end()) {
2338 bool handled
= ind
.handle_equal_numerators(best
);
2339 if (ind
.options
->emptiness_check
!=
2340 BV_LEXMIN_EMPTINESS_CHECK_NONE
&&
2341 ind
.options
->verify
->barvinok
->lp_solver
== BV_LP_POLYLIB
)
2343 /* If !handled then the leading coefficient is bigger than one;
2344 * must be an empty domain
2351 maxima
.push_back(ind
.create_max_term(best
));
2354 for (int j
= 0; j
< ind
.order
.le
[best
].size(); ++j
) {
2355 if (ind
.order
.le
[best
][j
]->sign
== 0) {
2356 if (!rat
&& ind
.order
.pred
[ind
.order
.le
[best
][j
]] == 1)
2357 rat
= ind
.order
.le
[best
][j
];
2358 } else if (ind
.order
.le
[best
][j
]->sign
> 0) {
2359 second
= ind
.order
.le
[best
][j
];
2362 neg
= ind
.order
.le
[best
][j
];
2365 if (!second
&& !neg
) {
2367 ind
.order
.unset_le(best
, rat
);
2368 ind
.expand_rational_vertex(rat
);
2375 ind
.order
.unset_le(best
, second
);
2381 unsigned dim
= best
->den
.NumCols();
2384 for (int k
= 0; k
< dim
; ++k
) {
2385 diff
= ediff(best
->vertex
[k
], second
->vertex
[k
]);
2386 sign
= evalue_sign(diff
, ind
.D
, ind
.options
->verify
->barvinok
);
2388 /* neg can never be smaller than best, unless it may still cancel.
2389 * This can happen if positive terms have been determined to
2390 * be equal or less than or equal to some negative term.
2392 if (second
== neg
&& !neg_eq
&& !neg_le
) {
2393 if (sign
== order_ge
)
2395 if (sign
== order_unknown
)
2399 if (sign
!= order_eq
)
2401 if (!EVALUE_IS_ZERO(*diff
)) {
2402 ind
.add_substitution(diff
);
2403 ind
.perform_pending_substitutions();
2406 if (sign
== order_eq
) {
2407 ind
.order
.set_equal(best
, second
);
2410 if (sign
== order_lt
) {
2411 ind
.order
.lt
[best
].push_back(second
);
2412 ind
.order
.inc_pred(second
);
2415 if (sign
== order_gt
) {
2416 ind
.order
.lt
[second
].push_back(best
);
2417 ind
.order
.inc_pred(best
);
2421 split
sp(diff
, sign
== order_le
? split::le
:
2422 sign
== order_ge
? split::ge
: split::lge
);
2424 EDomain
*Dlt
, *Deq
, *Dgt
;
2425 split_on(sp
, ind
.D
, &Dlt
, &Deq
, &Dgt
, ind
.options
);
2426 if (ind
.options
->emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_NONE
)
2427 assert(Dlt
|| Deq
|| Dgt
);
2428 else if (!(Dlt
|| Deq
|| Dgt
))
2429 /* Must have been empty all along */
2432 if (Deq
&& (Dlt
|| Dgt
)) {
2433 int locsize
= loc
.size();
2435 indicator
indeq(ind
, Deq
);
2437 indeq
.add_substitution(diff
);
2438 indeq
.perform_pending_substitutions();
2439 vector
<max_term
*> maxeq
= lexmin(indeq
, nparam
, loc
);
2440 maxima
.insert(maxima
.end(), maxeq
.begin(), maxeq
.end());
2441 loc
.resize(locsize
);
2444 int locsize
= loc
.size();
2446 indicator
indgt(ind
, Dgt
);
2448 /* we don't know the new location of these terms in indgt */
2450 indgt.order.lt[second].push_back(best);
2451 indgt.order.inc_pred(best);
2453 vector
<max_term
*> maxgt
= lexmin(indgt
, nparam
, loc
);
2454 maxima
.insert(maxima
.end(), maxgt
.begin(), maxgt
.end());
2455 loc
.resize(locsize
);
2460 ind
.set_domain(Deq
);
2461 ind
.add_substitution(diff
);
2462 ind
.perform_pending_substitutions();
2466 ind
.set_domain(Dlt
);
2467 ind
.order
.lt
[best
].push_back(second
);
2468 ind
.order
.inc_pred(second
);
2472 ind
.set_domain(Dgt
);
2473 ind
.order
.lt
[second
].push_back(best
);
2474 ind
.order
.inc_pred(best
);
2481 static void lexmin_base(Polyhedron
*P
, Polyhedron
*C
,
2482 Matrix
*CP
, Matrix
*T
,
2483 vector
<max_term
*>& all_max
,
2484 lexmin_options
*options
)
2486 unsigned nparam
= C
->Dimension
;
2487 Param_Polyhedron
*PP
= NULL
;
2489 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
->verify
->barvinok
);
2491 unsigned dim
= P
->Dimension
- nparam
;
2495 indicator_constructor
ic(P
, dim
, PP
, T
);
2497 vector
<indicator_term
*> all_vertices
;
2498 construct_rational_vertices(PP
, T
, T
? T
->NbRows
-nparam
-1 : dim
,
2499 nparam
, all_vertices
);
2501 Polyhedron
*TC
= true_context(P
, C
, options
->verify
->barvinok
->MaxRays
);
2502 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
->verify
->barvinok
, i
, D
, rVD
)
2505 EDomain
*epVD
= new EDomain(rVD
);
2506 indicator
ind(ic
, D
, epVD
, options
);
2508 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
2509 ind
.add(all_vertices
[_i
]);
2510 END_FORALL_PVertex_in_ParamPolyhedron
;
2512 ind
.remove_initial_rational_vertices();
2515 vector
<max_term
*> maxima
= lexmin(ind
, nparam
, loc
);
2517 for (int j
= 0; j
< maxima
.size(); ++j
)
2518 maxima
[j
]->substitute(CP
, options
->verify
->barvinok
);
2519 all_max
.insert(all_max
.end(), maxima
.begin(), maxima
.end());
2522 END_FORALL_REDUCED_DOMAIN
2523 Polyhedron_Free(TC
);
2524 for (int i
= 0; i
< all_vertices
.size(); ++i
)
2525 delete all_vertices
[i
];
2526 Param_Polyhedron_Free(PP
);
2529 static vector
<max_term
*> lexmin(Polyhedron
*P
, Polyhedron
*C
,
2530 lexmin_options
*options
)
2532 unsigned nparam
= C
->Dimension
;
2533 Matrix
*T
= NULL
, *CP
= NULL
;
2534 Polyhedron
*Porig
= P
;
2535 Polyhedron
*Corig
= C
;
2536 vector
<max_term
*> all_max
;
2541 POL_ENSURE_VERTICES(P
);
2546 assert(P
->NbBid
== 0);
2549 remove_all_equalities(&P
, &C
, &CP
, &T
, nparam
,
2550 options
->verify
->barvinok
->MaxRays
);
2552 lexmin_base(P
, C
, CP
, T
, all_max
, options
);
2565 static void verify_results(Polyhedron
*A
, Polyhedron
*C
,
2566 vector
<max_term
*>& maxima
,
2567 struct verify_options
*options
);
2569 /* Turn the set dimensions of "context" into parameters and return
2570 * the corresponding parameter domain.
2572 static struct isl_basic_set
*to_parameter_domain(struct isl_basic_set
*context
)
2574 context
= isl_basic_set_move_dims(context
, isl_dim_param
, 0,
2575 isl_dim_set
, 0, isl_basic_set_dim(context
, isl_dim_set
));
2576 context
= isl_basic_set_params(context
);
2580 int main(int argc
, char **argv
)
2583 isl_basic_set
*context
, *bset
;
2588 int urs_unknowns
= 0;
2589 int print_solution
= 1;
2590 struct lexmin_options
*options
= lexmin_options_new_with_defaults();
2592 options
->verify
->barvinok
->lookup_table
= 0;
2594 argc
= lexmin_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
2595 ctx
= isl_ctx_alloc_with_options(&lexmin_options_args
, options
);
2597 context
= isl_basic_set_read_from_file(ctx
, stdin
);
2599 n
= fscanf(stdin
, "%d", &neg_one
);
2601 assert(neg_one
== -1);
2602 bset
= isl_basic_set_read_from_file(ctx
, stdin
);
2604 while (fgets(s
, sizeof(s
), stdin
)) {
2605 if (strncasecmp(s
, "Maximize", 8) == 0) {
2606 fprintf(stderr
, "Maximize option not supported\n");
2609 if (strncasecmp(s
, "Rational", 8) == 0) {
2610 fprintf(stderr
, "Rational option not supported\n");
2613 if (strncasecmp(s
, "Urs_parms", 9) == 0)
2615 if (strncasecmp(s
, "Urs_unknowns", 12) == 0)
2619 context
= isl_basic_set_intersect(context
,
2620 isl_basic_set_positive_orthant(isl_basic_set_get_space(context
)));
2621 context
= to_parameter_domain(context
);
2622 nparam
= isl_basic_set_dim(context
, isl_dim_param
);
2623 if (nparam
!= isl_basic_set_dim(bset
, isl_dim_param
)) {
2624 int dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2625 bset
= isl_basic_set_move_dims(bset
, isl_dim_param
, 0,
2626 isl_dim_set
, dim
- nparam
, nparam
);
2629 bset
= isl_basic_set_intersect(bset
,
2630 isl_basic_set_positive_orthant(isl_basic_set_get_space(bset
)));
2632 if (options
->verify
->verify
)
2635 A
= isl_basic_set_to_polylib(bset
);
2636 verify_options_set_range(options
->verify
, A
->Dimension
);
2637 C
= isl_basic_set_to_polylib(context
);
2638 vector
<max_term
*> maxima
= lexmin(A
, C
, options
);
2639 if (print_solution
) {
2641 param_names
= util_generate_names(C
->Dimension
, "p");
2642 for (int i
= 0; i
< maxima
.size(); ++i
)
2643 maxima
[i
]->print(cout
, param_names
,
2644 options
->verify
->barvinok
);
2645 util_free_names(C
->Dimension
, param_names
);
2648 if (options
->verify
->verify
)
2649 verify_results(A
, C
, maxima
, options
->verify
);
2651 for (int i
= 0; i
< maxima
.size(); ++i
)
2657 isl_basic_set_free(bset
);
2658 isl_basic_set_free(context
);
2664 static bool lexmin(int pos
, Polyhedron
*P
, Value
*context
)
2673 value_init(LB
); value_init(UB
); value_init(k
);
2676 lu_flags
= lower_upper_bounds(pos
,P
,context
,&LB
,&UB
);
2677 assert(!(lu_flags
& LB_INFINITY
));
2679 value_set_si(context
[pos
],0);
2680 if (!lu_flags
&& value_lt(UB
,LB
)) {
2681 value_clear(LB
); value_clear(UB
); value_clear(k
);
2685 value_assign(context
[pos
], LB
);
2686 value_clear(LB
); value_clear(UB
); value_clear(k
);
2689 for (value_assign(k
,LB
); lu_flags
|| value_le(k
,UB
); value_increment(k
,k
)) {
2690 value_assign(context
[pos
],k
);
2691 if ((found
= lexmin(pos
+1, P
->next
, context
)))
2695 value_set_si(context
[pos
],0);
2696 value_clear(LB
); value_clear(UB
); value_clear(k
);
2700 static void print_list(FILE *out
, Value
*z
, const char* brackets
, int len
)
2702 fprintf(out
, "%c", brackets
[0]);
2703 value_print(out
, VALUE_FMT
,z
[0]);
2704 for (int k
= 1; k
< len
; ++k
) {
2706 value_print(out
, VALUE_FMT
,z
[k
]);
2708 fprintf(out
, "%c", brackets
[1]);
2711 static int check_poly_lexmin(const struct check_poly_data
*data
,
2712 int nparam
, Value
*z
,
2713 const struct verify_options
*options
);
2715 struct check_poly_lexmin_data
: public check_poly_data
{
2717 vector
<max_term
*>& maxima
;
2719 check_poly_lexmin_data(Polyhedron
*S
, Value
*z
,
2720 vector
<max_term
*>& maxima
) : S(S
), maxima(maxima
) {
2722 this->check
= &check_poly_lexmin
;
2726 static int check_poly_lexmin(const struct check_poly_data
*data
,
2727 int nparam
, Value
*z
,
2728 const struct verify_options
*options
)
2730 const check_poly_lexmin_data
*lexmin_data
;
2731 lexmin_data
= static_cast<const check_poly_lexmin_data
*>(data
);
2732 Polyhedron
*S
= lexmin_data
->S
;
2733 vector
<max_term
*>& maxima
= lexmin_data
->maxima
;
2735 bool found
= lexmin(1, S
, lexmin_data
->z
);
2737 if (options
->print_all
) {
2739 print_list(stdout
, z
, "()", nparam
);
2742 print_list(stdout
, lexmin_data
->z
+1, "[]", S
->Dimension
-nparam
);
2747 for (int i
= 0; i
< maxima
.size(); ++i
)
2748 if ((min
= maxima
[i
]->eval(z
, options
->barvinok
->MaxRays
)))
2751 int ok
= !(found
^ !!min
);
2753 for (int i
= 0; i
< S
->Dimension
-nparam
; ++i
)
2754 if (value_ne(lexmin_data
->z
[1+i
], min
->p
[i
])) {
2761 fprintf(stderr
, "Error !\n");
2762 fprintf(stderr
, "lexmin");
2763 print_list(stderr
, z
, "()", nparam
);
2764 fprintf(stderr
, " should be ");
2766 print_list(stderr
, lexmin_data
->z
+1, "[]", S
->Dimension
-nparam
);
2767 fprintf(stderr
, " while digging gives ");
2769 print_list(stderr
, min
->p
, "[]", S
->Dimension
-nparam
);
2770 fprintf(stderr
, ".\n");
2772 } else if (options
->print_all
)
2777 for (k
= 1; k
<= S
->Dimension
-nparam
; ++k
)
2778 value_set_si(lexmin_data
->z
[k
], 0);
2783 void verify_results(Polyhedron
*A
, Polyhedron
*C
, vector
<max_term
*>& maxima
,
2784 struct verify_options
*options
)
2787 unsigned nparam
= C
->Dimension
;
2788 unsigned MaxRays
= options
->barvinok
->MaxRays
;
2793 CS
= check_poly_context_scan(A
, &C
, nparam
, options
);
2795 p
= Vector_Alloc(A
->Dimension
+2);
2796 value_set_si(p
->p
[A
->Dimension
+1], 1);
2798 S
= Polyhedron_Scan(A
, C
, MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
2800 check_poly_init(C
, options
);
2803 if (!(CS
&& emptyQ2(CS
))) {
2804 check_poly_lexmin_data
data(S
, p
->p
, maxima
);
2805 check_poly(CS
, &data
, nparam
, 0, p
->p
+S
->Dimension
-nparam
+1, options
);
2810 if (!options
->print_all
)