1 #include "isl_sample.h"
2 #include "isl_sample_piplib.h"
6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
9 #include "isl_basis_reduction.h"
11 static struct isl_vec
*empty_sample(struct isl_basic_set
*bset
)
15 vec
= isl_vec_alloc(bset
->ctx
, 0);
16 isl_basic_set_free(bset
);
20 /* Construct a zero sample of the same dimension as bset.
21 * As a special case, if bset is zero-dimensional, this
22 * function creates a zero-dimensional sample point.
24 static struct isl_vec
*zero_sample(struct isl_basic_set
*bset
)
27 struct isl_vec
*sample
;
29 dim
= isl_basic_set_total_dim(bset
);
30 sample
= isl_vec_alloc(bset
->ctx
, 1 + dim
);
32 isl_int_set_si(sample
->el
[0], 1);
33 isl_seq_clr(sample
->el
+ 1, dim
);
35 isl_basic_set_free(bset
);
39 static struct isl_vec
*interval_sample(struct isl_basic_set
*bset
)
43 struct isl_vec
*sample
;
45 bset
= isl_basic_set_simplify(bset
);
48 if (isl_basic_set_fast_is_empty(bset
))
49 return empty_sample(bset
);
50 if (bset
->n_eq
== 0 && bset
->n_ineq
== 0)
51 return zero_sample(bset
);
53 sample
= isl_vec_alloc(bset
->ctx
, 2);
54 isl_int_set_si(sample
->block
.data
[0], 1);
57 isl_assert(bset
->ctx
, bset
->n_eq
== 1, goto error
);
58 isl_assert(bset
->ctx
, bset
->n_ineq
== 0, goto error
);
59 if (isl_int_is_one(bset
->eq
[0][1]))
60 isl_int_neg(sample
->el
[1], bset
->eq
[0][0]);
62 isl_assert(bset
->ctx
, isl_int_is_negone(bset
->eq
[0][1]),
64 isl_int_set(sample
->el
[1], bset
->eq
[0][0]);
66 isl_basic_set_free(bset
);
71 if (isl_int_is_one(bset
->ineq
[0][1]))
72 isl_int_neg(sample
->block
.data
[1], bset
->ineq
[0][0]);
74 isl_int_set(sample
->block
.data
[1], bset
->ineq
[0][0]);
75 for (i
= 1; i
< bset
->n_ineq
; ++i
) {
76 isl_seq_inner_product(sample
->block
.data
,
77 bset
->ineq
[i
], 2, &t
);
78 if (isl_int_is_neg(t
))
82 if (i
< bset
->n_ineq
) {
84 return empty_sample(bset
);
87 isl_basic_set_free(bset
);
90 isl_basic_set_free(bset
);
95 static struct isl_mat
*independent_bounds(struct isl_ctx
*ctx
,
96 struct isl_basic_set
*bset
)
99 struct isl_mat
*dirs
= NULL
;
105 dim
= isl_basic_set_n_dim(bset
);
106 if (bset
->n_ineq
== 0)
107 return isl_mat_alloc(ctx
, 0, dim
);
109 dirs
= isl_mat_alloc(ctx
, dim
, dim
);
112 isl_seq_cpy(dirs
->row
[0], bset
->ineq
[0]+1, dirs
->n_col
);
113 for (j
= 1, n
= 1; n
< dim
&& j
< bset
->n_ineq
; ++j
) {
116 isl_seq_cpy(dirs
->row
[n
], bset
->ineq
[j
]+1, dirs
->n_col
);
118 pos
= isl_seq_first_non_zero(dirs
->row
[n
], dirs
->n_col
);
121 for (i
= 0; i
< n
; ++i
) {
123 pos_i
= isl_seq_first_non_zero(dirs
->row
[i
], dirs
->n_col
);
128 isl_seq_elim(dirs
->row
[n
], dirs
->row
[i
], pos
,
130 pos
= isl_seq_first_non_zero(dirs
->row
[n
], dirs
->n_col
);
138 isl_int
*t
= dirs
->row
[n
];
139 for (k
= n
; k
> i
; --k
)
140 dirs
->row
[k
] = dirs
->row
[k
-1];
149 /* Find a sample integer point, if any, in bset, which is known
150 * to have equalities. If bset contains no integer points, then
151 * return a zero-length vector.
152 * We simply remove the known equalities, compute a sample
153 * in the resulting bset, using the specified recurse function,
154 * and then transform the sample back to the original space.
156 static struct isl_vec
*sample_eq(struct isl_basic_set
*bset
,
157 struct isl_vec
*(*recurse
)(struct isl_basic_set
*))
160 struct isl_vec
*sample
;
167 bset
= isl_basic_set_remove_equalities(bset
, &T
, NULL
);
168 sample
= recurse(bset
);
169 if (!sample
|| sample
->size
== 0)
170 isl_mat_free(ctx
, T
);
172 sample
= isl_mat_vec_product(ctx
, T
, sample
);
176 /* Given a basic set "bset" and an affine function "f"/"denom",
177 * check if bset is bounded and non-empty and if so, return the minimal
178 * and maximal value attained by the affine function in "min" and "max".
179 * The minimal value is rounded up to the nearest integer, while the
180 * maximal value is rounded down.
181 * The return value indicates whether the set was empty or unbounded.
183 * If we happen to find an integer point while looking for the minimal
184 * or maximal value, then we record that value in "bset" and return early.
186 static enum isl_lp_result
basic_set_range(struct isl_basic_set
*bset
,
187 isl_int
*f
, isl_int denom
, isl_int
*min
, isl_int
*max
)
191 enum isl_lp_result res
;
195 if (isl_basic_set_fast_is_empty(bset
))
198 tab
= isl_tab_from_basic_set(bset
);
199 res
= isl_tab_min(bset
->ctx
, tab
, f
, denom
, min
, NULL
, 0);
200 if (res
!= isl_lp_ok
)
203 if (isl_tab_sample_is_integer(bset
->ctx
, tab
)) {
204 isl_vec_free(bset
->sample
);
205 bset
->sample
= isl_tab_get_sample_value(bset
->ctx
, tab
);
208 isl_int_set(*max
, *min
);
212 dim
= isl_basic_set_total_dim(bset
);
213 isl_seq_neg(f
, f
, 1 + dim
);
214 res
= isl_tab_min(bset
->ctx
, tab
, f
, denom
, max
, NULL
, 0);
215 isl_seq_neg(f
, f
, 1 + dim
);
216 isl_int_neg(*max
, *max
);
218 if (isl_tab_sample_is_integer(bset
->ctx
, tab
)) {
219 isl_vec_free(bset
->sample
);
220 bset
->sample
= isl_tab_get_sample_value(bset
->ctx
, tab
);
226 isl_tab_free(bset
->ctx
, tab
);
229 isl_tab_free(bset
->ctx
, tab
);
233 /* Perform a basis reduction on "bset" and return the inverse of
234 * the new basis, i.e., an affine mapping from the new coordinates to the old,
237 static struct isl_basic_set
*basic_set_reduced(struct isl_basic_set
*bset
,
241 unsigned gbr_only_first
;
249 gbr_only_first
= ctx
->gbr_only_first
;
250 ctx
->gbr_only_first
= 1;
251 *T
= isl_basic_set_reduced_basis(bset
);
252 ctx
->gbr_only_first
= gbr_only_first
;
254 *T
= isl_mat_lin_to_aff(bset
->ctx
, *T
);
255 *T
= isl_mat_right_inverse(bset
->ctx
, *T
);
257 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(bset
->ctx
, *T
));
263 isl_mat_free(ctx
, *T
);
268 static struct isl_vec
*sample_bounded(struct isl_basic_set
*bset
);
270 /* Given a basic set "bset" whose first coordinate ranges between
271 * "min" and "max", step through all values from min to max, until
272 * the slice of bset with the first coordinate fixed to one of these
273 * values contains an integer point. If such a point is found, return it.
274 * If none of the slices contains any integer point, then bset itself
275 * doesn't contain any integer point and an empty sample is returned.
277 static struct isl_vec
*sample_scan(struct isl_basic_set
*bset
,
278 isl_int min
, isl_int max
)
281 struct isl_basic_set
*slice
= NULL
;
282 struct isl_vec
*sample
= NULL
;
285 total
= isl_basic_set_total_dim(bset
);
288 for (isl_int_set(tmp
, min
); isl_int_le(tmp
, max
);
289 isl_int_add_ui(tmp
, tmp
, 1)) {
292 slice
= isl_basic_set_copy(bset
);
293 slice
= isl_basic_set_cow(slice
);
294 slice
= isl_basic_set_extend_constraints(slice
, 1, 0);
295 k
= isl_basic_set_alloc_equality(slice
);
298 isl_int_set(slice
->eq
[k
][0], tmp
);
299 isl_int_set_si(slice
->eq
[k
][1], -1);
300 isl_seq_clr(slice
->eq
[k
] + 2, total
- 1);
301 slice
= isl_basic_set_simplify(slice
);
302 sample
= sample_bounded(slice
);
306 if (sample
->size
> 0)
308 isl_vec_free(sample
);
312 sample
= empty_sample(bset
);
314 isl_basic_set_free(bset
);
318 isl_basic_set_free(bset
);
319 isl_basic_set_free(slice
);
324 /* Given a basic set that is known to be bounded, find and return
325 * an integer point in the basic set, if there is any.
327 * After handling some trivial cases, we check the range of the
328 * first coordinate. If this coordinate can only attain one integer
329 * value, we are happy. Otherwise, we perform basis reduction and
330 * determine the new range.
332 * Then we step through all possible values in the range in sample_scan.
334 * If any basis reduction was performed, the sample value found, if any,
335 * is transformed back to the original space.
337 static struct isl_vec
*sample_bounded(struct isl_basic_set
*bset
)
341 struct isl_vec
*sample
;
342 struct isl_vec
*obj
= NULL
;
343 struct isl_mat
*T
= NULL
;
345 enum isl_lp_result res
;
350 if (isl_basic_set_fast_is_empty(bset
))
351 return empty_sample(bset
);
354 dim
= isl_basic_set_total_dim(bset
);
356 return zero_sample(bset
);
358 return interval_sample(bset
);
360 return sample_eq(bset
, sample_bounded
);
364 obj
= isl_vec_alloc(bset
->ctx
, 1 + dim
);
367 isl_seq_clr(obj
->el
, 1+ dim
);
368 isl_int_set_si(obj
->el
[1], 1);
370 res
= basic_set_range(bset
, obj
->el
, bset
->ctx
->one
, &min
, &max
);
371 if (res
== isl_lp_error
)
373 isl_assert(bset
->ctx
, res
!= isl_lp_unbounded
, goto error
);
375 sample
= isl_vec_copy(bset
->sample
);
376 isl_basic_set_free(bset
);
379 if (res
== isl_lp_empty
|| isl_int_lt(max
, min
)) {
380 sample
= empty_sample(bset
);
384 if (isl_int_ne(min
, max
)) {
385 bset
= basic_set_reduced(bset
, &T
);
389 res
= basic_set_range(bset
, obj
->el
, bset
->ctx
->one
, &min
, &max
);
390 if (res
== isl_lp_error
)
392 isl_assert(bset
->ctx
, res
!= isl_lp_unbounded
, goto error
);
394 sample
= isl_vec_copy(bset
->sample
);
395 isl_basic_set_free(bset
);
398 if (res
== isl_lp_empty
|| isl_int_lt(max
, min
)) {
399 sample
= empty_sample(bset
);
404 sample
= sample_scan(bset
, min
, max
);
407 if (!sample
|| sample
->size
== 0)
408 isl_mat_free(ctx
, T
);
410 sample
= isl_mat_vec_product(ctx
, T
, sample
);
417 isl_mat_free(ctx
, T
);
418 isl_basic_set_free(bset
);
425 /* Given a basic set "bset" and a value "sample" for the first coordinates
426 * of bset, plug in these values and drop the corresponding coordinates.
428 * We do this by computing the preimage of the transformation
434 * where [1 s] is the sample value and I is the identity matrix of the
435 * appropriate dimension.
437 static struct isl_basic_set
*plug_in(struct isl_basic_set
*bset
,
438 struct isl_vec
*sample
)
444 if (!bset
|| !sample
)
447 total
= isl_basic_set_total_dim(bset
);
448 T
= isl_mat_alloc(bset
->ctx
, 1 + total
, 1 + total
- (sample
->size
- 1));
452 for (i
= 0; i
< sample
->size
; ++i
) {
453 isl_int_set(T
->row
[i
][0], sample
->el
[i
]);
454 isl_seq_clr(T
->row
[i
] + 1, T
->n_col
- 1);
456 for (i
= 0; i
< T
->n_col
- 1; ++i
) {
457 isl_seq_clr(T
->row
[sample
->size
+ i
], T
->n_col
);
458 isl_int_set_si(T
->row
[sample
->size
+ i
][1 + i
], 1);
460 isl_vec_free(sample
);
462 bset
= isl_basic_set_preimage(bset
, T
);
465 isl_basic_set_free(bset
);
466 isl_vec_free(sample
);
470 /* Given a basic set "bset", return any (possibly non-integer) point
473 static struct isl_vec
*rational_sample(struct isl_basic_set
*bset
)
476 struct isl_vec
*sample
;
481 tab
= isl_tab_from_basic_set(bset
);
482 sample
= isl_tab_get_sample_value(bset
->ctx
, tab
);
483 isl_tab_free(bset
->ctx
, tab
);
485 isl_basic_set_free(bset
);
490 /* Given a rational vector, with the denominator in the first element
491 * of the vector, round up all coordinates.
493 struct isl_vec
*isl_vec_ceil(struct isl_vec
*vec
)
497 vec
= isl_vec_cow(vec
);
501 isl_seq_cdiv_q(vec
->el
+ 1, vec
->el
+ 1, vec
->el
[0], vec
->size
- 1);
503 isl_int_set_si(vec
->el
[0], 1);
508 /* Given a linear cone "cone" and a rational point "vec",
509 * construct a polyhedron with shifted copies of the constraints in "cone",
510 * i.e., a polyhedron with "cone" as its recession cone, such that each
511 * point x in this polyhedron is such that the unit box positioned at x
512 * lies entirely inside the affine cone 'vec + cone'.
513 * Any rational point in this polyhedron may therefore be rounded up
514 * to yield an integer point that lies inside said affine cone.
516 * Denote the constraints of cone by "<a_i, x> >= 0" and the rational
517 * point "vec" by v/d.
518 * Let b_i = <a_i, v>. Then the affine cone 'vec + cone' is given
519 * by <a_i, x> - b/d >= 0.
520 * The polyhedron <a_i, x> - ceil{b/d} >= 0 is a subset of this affine cone.
521 * We prefer this polyhedron over the actual affine cone because it doesn't
522 * require a scaling of the constraints.
523 * If each of the vertices of the unit cube positioned at x lies inside
524 * this polyhedron, then the whole unit cube at x lies inside the affine cone.
525 * We therefore impose that x' = x + \sum e_i, for any selection of unit
526 * vectors lies inside the polyhedron, i.e.,
528 * <a_i, x'> - ceil{b/d} = <a_i, x> + sum a_i - ceil{b/d} >= 0
530 * The most stringent of these constraints is the one that selects
531 * all negative a_i, so the polyhedron we are looking for has constraints
533 * <a_i, x> + sum_{a_i < 0} a_i - ceil{b/d} >= 0
535 * Note that if cone were known to have only non-negative rays
536 * (which can be accomplished by a unimodular transformation),
537 * then we would only have to check the points x' = x + e_i
538 * and we only have to add the smallest negative a_i (if any)
539 * instead of the sum of all negative a_i.
541 static struct isl_basic_set
*shift_cone(struct isl_basic_set
*cone
,
547 struct isl_basic_set
*shift
= NULL
;
552 isl_assert(cone
->ctx
, cone
->n_eq
== 0, goto error
);
554 total
= isl_basic_set_total_dim(cone
);
556 shift
= isl_basic_set_alloc_dim(isl_basic_set_get_dim(cone
),
559 for (i
= 0; i
< cone
->n_ineq
; ++i
) {
560 k
= isl_basic_set_alloc_inequality(shift
);
563 isl_seq_cpy(shift
->ineq
[k
] + 1, cone
->ineq
[i
] + 1, total
);
564 isl_seq_inner_product(shift
->ineq
[k
] + 1, vec
->el
+ 1, total
,
566 isl_int_cdiv_q(shift
->ineq
[k
][0],
567 shift
->ineq
[k
][0], vec
->el
[0]);
568 isl_int_neg(shift
->ineq
[k
][0], shift
->ineq
[k
][0]);
569 for (j
= 0; j
< total
; ++j
) {
570 if (isl_int_is_nonneg(shift
->ineq
[k
][1 + j
]))
572 isl_int_add(shift
->ineq
[k
][0],
573 shift
->ineq
[k
][0], shift
->ineq
[k
][1 + j
]);
577 isl_basic_set_free(cone
);
580 return isl_basic_set_finalize(shift
);
582 isl_basic_set_free(shift
);
583 isl_basic_set_free(cone
);
588 /* Given a rational point vec in a (transformed) basic set,
589 * such that cone is the recession cone of the original basic set,
590 * "round up" the rational point to an integer point.
592 * We first check if the rational point just happens to be integer.
593 * If not, we transform the cone in the same way as the basic set,
594 * pick a point x in this cone shifted to the rational point such that
595 * the whole unit cube at x is also inside this affine cone.
596 * Then we simply round up the coordinates of x and return the
597 * resulting integer point.
599 static struct isl_vec
*round_up_in_cone(struct isl_vec
*vec
,
600 struct isl_basic_set
*cone
, struct isl_mat
*U
)
604 if (!vec
|| !cone
|| !U
)
607 isl_assert(vec
->ctx
, vec
->size
!= 0, goto error
);
608 if (isl_int_is_one(vec
->el
[0])) {
609 isl_mat_free(vec
->ctx
, U
);
610 isl_basic_set_free(cone
);
614 total
= isl_basic_set_total_dim(cone
);
615 cone
= isl_basic_set_preimage(cone
, U
);
616 cone
= isl_basic_set_remove_dims(cone
, 0, total
- (vec
->size
- 1));
618 cone
= shift_cone(cone
, vec
);
620 vec
= rational_sample(cone
);
621 vec
= isl_vec_ceil(vec
);
624 isl_mat_free(vec
? vec
->ctx
: cone
? cone
->ctx
: NULL
, U
);
626 isl_basic_set_free(cone
);
630 /* Concatenate two integer vectors, i.e., two vectors with denominator
631 * (stored in element 0) equal to 1.
633 static struct isl_vec
*vec_concat(struct isl_vec
*vec1
, struct isl_vec
*vec2
)
639 isl_assert(vec1
->ctx
, vec1
->size
> 0, goto error
);
640 isl_assert(vec2
->ctx
, vec2
->size
> 0, goto error
);
641 isl_assert(vec1
->ctx
, isl_int_is_one(vec1
->el
[0]), goto error
);
642 isl_assert(vec2
->ctx
, isl_int_is_one(vec2
->el
[0]), goto error
);
644 vec
= isl_vec_alloc(vec1
->ctx
, vec1
->size
+ vec2
->size
- 1);
648 isl_seq_cpy(vec
->el
, vec1
->el
, vec1
->size
);
649 isl_seq_cpy(vec
->el
+ vec1
->size
, vec2
->el
+ 1, vec2
->size
- 1);
661 /* Drop all constraints in bset that involve any of the dimensions
662 * first to first+n-1.
664 static struct isl_basic_set
*drop_constraints_involving
665 (struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
672 bset
= isl_basic_set_cow(bset
);
674 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
675 if (isl_seq_first_non_zero(bset
->ineq
[i
] + 1 + first
, n
) == -1)
677 isl_basic_set_drop_inequality(bset
, i
);
683 /* Give a basic set "bset" with recession cone "cone", compute and
684 * return an integer point in bset, if any.
686 * If the recession cone is full-dimensional, then we know that
687 * bset contains an infinite number of integer points and it is
688 * fairly easy to pick one of them.
689 * If the recession cone is not full-dimensional, then we first
690 * transform bset such that the bounded directions appear as
691 * the first dimensions of the transformed basic set.
692 * We do this by using a unimodular transformation that transforms
693 * the equalities in the recession cone to equalities on the first
696 * The transformed set is then projected onto its bounded dimensions.
697 * Note that to compute this projection, we can simply drop all constraints
698 * involving any of the unbounded dimensions since these constraints
699 * cannot be combined to produce a constraint on the bounded dimensions.
700 * To see this, assume that there is such a combination of constraints
701 * that produces a constraint on the bounded dimensions. This means
702 * that some combination of the unbounded dimensions has both an upper
703 * bound and a lower bound in terms of the bounded dimensions, but then
704 * this combination would be a bounded direction too and would have been
705 * transformed into a bounded dimensions.
707 * We then compute a sample value in the bounded dimensions.
708 * If no such value can be found, then the original set did not contain
709 * any integer points and we are done.
710 * Otherwise, we plug in the value we found in the bounded dimensions,
711 * project out these bounded dimensions and end up with a set with
712 * a full-dimensional recession cone.
713 * A sample point in this set is computed by "rounding up" any
714 * rational point in the set.
716 * The sample points in the bounded and unbounded dimensions are
717 * then combined into a single sample point and transformed back
718 * to the original space.
720 static struct isl_vec
*sample_with_cone(struct isl_basic_set
*bset
,
721 struct isl_basic_set
*cone
)
725 struct isl_mat
*M
, *U
;
726 struct isl_vec
*sample
;
727 struct isl_vec
*cone_sample
;
729 struct isl_basic_set
*bounded
;
735 total
= isl_basic_set_total_dim(cone
);
736 cone_dim
= total
- cone
->n_eq
;
738 M
= isl_mat_sub_alloc(bset
->ctx
, cone
->eq
, 0, cone
->n_eq
, 1, total
);
739 M
= isl_mat_left_hermite(bset
->ctx
, M
, 0, &U
, NULL
);
742 isl_mat_free(bset
->ctx
, M
);
744 U
= isl_mat_lin_to_aff(bset
->ctx
, U
);
745 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(bset
->ctx
, U
));
747 bounded
= isl_basic_set_copy(bset
);
748 bounded
= drop_constraints_involving(bounded
, total
- cone_dim
, cone_dim
);
749 bounded
= isl_basic_set_drop_dims(bounded
, total
- cone_dim
, cone_dim
);
750 sample
= sample_bounded(bounded
);
751 if (!sample
|| sample
->size
== 0) {
752 isl_basic_set_free(bset
);
753 isl_basic_set_free(cone
);
754 isl_mat_free(ctx
, U
);
757 bset
= plug_in(bset
, isl_vec_copy(sample
));
758 cone_sample
= rational_sample(bset
);
759 cone_sample
= round_up_in_cone(cone_sample
, cone
, isl_mat_copy(ctx
, U
));
760 sample
= vec_concat(sample
, cone_sample
);
761 sample
= isl_mat_vec_product(ctx
, U
, sample
);
764 isl_basic_set_free(cone
);
765 isl_basic_set_free(bset
);
769 /* Compute and return a sample point in bset using generalized basis
770 * reduction. We first check if the input set has a non-trivial
771 * recession cone. If so, we perform some extra preprocessing in
772 * sample_with_cone. Otherwise, we directly perform generalized basis
775 static struct isl_vec
*gbr_sample_no_lineality(struct isl_basic_set
*bset
)
778 struct isl_basic_set
*cone
;
780 dim
= isl_basic_set_total_dim(bset
);
782 cone
= isl_basic_set_recession_cone(isl_basic_set_copy(bset
));
784 if (cone
->n_eq
< dim
)
785 return sample_with_cone(bset
, cone
);
787 isl_basic_set_free(cone
);
788 return sample_bounded(bset
);
791 static struct isl_vec
*sample_no_lineality(struct isl_basic_set
*bset
)
795 if (isl_basic_set_fast_is_empty(bset
))
796 return empty_sample(bset
);
798 return sample_eq(bset
, sample_no_lineality
);
799 dim
= isl_basic_set_total_dim(bset
);
801 return zero_sample(bset
);
803 return interval_sample(bset
);
805 switch (bset
->ctx
->ilp_solver
) {
807 return isl_pip_basic_set_sample(bset
);
809 return gbr_sample_no_lineality(bset
);
811 isl_assert(bset
->ctx
, 0, );
812 isl_basic_set_free(bset
);
816 /* Compute an integer point in "bset" with a lineality space that
817 * is orthogonal to the constraints in "bounds".
819 * We first perform a unimodular transformation on bset that
820 * make the constraints in bounds (and therefore all constraints in bset)
821 * only involve the first dimensions. The remaining dimensions
822 * then do not appear in any constraints and we can select any value
823 * for them, say zero. We therefore project out this final dimensions
824 * and plug in the value zero later. This is accomplished by simply
825 * dropping the final columns of the unimodular transformation.
827 static struct isl_vec
*sample_lineality(struct isl_basic_set
*bset
,
828 struct isl_mat
*bounds
)
830 struct isl_mat
*U
= NULL
;
831 unsigned old_dim
, new_dim
;
832 struct isl_vec
*sample
;
835 if (!bset
|| !bounds
)
839 old_dim
= isl_basic_set_n_dim(bset
);
840 new_dim
= bounds
->n_row
;
841 bounds
= isl_mat_left_hermite(ctx
, bounds
, 0, &U
, NULL
);
844 U
= isl_mat_lin_to_aff(ctx
, U
);
845 U
= isl_mat_drop_cols(ctx
, U
, 1 + new_dim
, old_dim
- new_dim
);
846 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(ctx
, U
));
849 isl_mat_free(ctx
, bounds
);
851 sample
= sample_no_lineality(bset
);
852 if (sample
&& sample
->size
!= 0)
853 sample
= isl_mat_vec_product(ctx
, U
, sample
);
855 isl_mat_free(ctx
, U
);
858 isl_mat_free(ctx
, bounds
);
859 isl_mat_free(ctx
, U
);
860 isl_basic_set_free(bset
);
864 struct isl_vec
*isl_basic_set_sample(struct isl_basic_set
*bset
)
867 struct isl_mat
*bounds
;
873 if (isl_basic_set_fast_is_empty(bset
))
874 return empty_sample(bset
);
876 dim
= isl_basic_set_n_dim(bset
);
877 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
878 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
880 if (bset
->sample
&& bset
->sample
->size
== 1 + dim
) {
881 int contains
= isl_basic_set_contains(bset
, bset
->sample
);
885 struct isl_vec
*sample
= isl_vec_copy(bset
->sample
);
886 isl_basic_set_free(bset
);
890 isl_vec_free(bset
->sample
);
894 return sample_eq(bset
, isl_basic_set_sample
);
896 return zero_sample(bset
);
898 return interval_sample(bset
);
899 bounds
= independent_bounds(ctx
, bset
);
903 if (bounds
->n_row
== 0) {
904 isl_mat_free(ctx
, bounds
);
905 return zero_sample(bset
);
907 if (bounds
->n_row
< dim
)
908 return sample_lineality(bset
, bounds
);
910 isl_mat_free(ctx
, bounds
);
911 return sample_no_lineality(bset
);
913 isl_basic_set_free(bset
);