2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 #include <isl_union_map_private.h>
15 #include <isl_polynomial_private.h>
16 #include <isl_point_private.h>
17 #include <isl_dim_private.h>
18 #include <isl_map_private.h>
19 #include <isl_mat_private.h>
21 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
24 case isl_dim_param: return 0;
25 case isl_dim_in: return dim->nparam;
26 case isl_dim_out: return dim->nparam + dim->n_in;
31 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
39 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
44 isl_assert(up->ctx, up->var < 0, return NULL);
46 return (struct isl_upoly_cst *)up;
49 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
54 isl_assert(up->ctx, up->var >= 0, return NULL);
56 return (struct isl_upoly_rec *)up;
59 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
60 __isl_keep struct isl_upoly *up2)
63 struct isl_upoly_rec *rec1, *rec2;
69 if (up1->var != up2->var)
71 if (isl_upoly_is_cst(up1)) {
72 struct isl_upoly_cst *cst1, *cst2;
73 cst1 = isl_upoly_as_cst(up1);
74 cst2 = isl_upoly_as_cst(up2);
77 return isl_int_eq(cst1->n, cst2->n) &&
78 isl_int_eq(cst1->d, cst2->d);
81 rec1 = isl_upoly_as_rec(up1);
82 rec2 = isl_upoly_as_rec(up2);
86 if (rec1->n != rec2->n)
89 for (i = 0; i < rec1->n; ++i) {
90 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
98 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
100 struct isl_upoly_cst *cst;
104 if (!isl_upoly_is_cst(up))
107 cst = isl_upoly_as_cst(up);
111 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
114 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
116 struct isl_upoly_cst *cst;
120 if (!isl_upoly_is_cst(up))
123 cst = isl_upoly_as_cst(up);
127 return isl_int_sgn(cst->n);
130 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
132 struct isl_upoly_cst *cst;
136 if (!isl_upoly_is_cst(up))
139 cst = isl_upoly_as_cst(up);
143 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
146 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
148 struct isl_upoly_cst *cst;
152 if (!isl_upoly_is_cst(up))
155 cst = isl_upoly_as_cst(up);
159 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
162 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
164 struct isl_upoly_cst *cst;
168 if (!isl_upoly_is_cst(up))
171 cst = isl_upoly_as_cst(up);
175 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
178 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
180 struct isl_upoly_cst *cst;
184 if (!isl_upoly_is_cst(up))
187 cst = isl_upoly_as_cst(up);
191 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
194 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
196 struct isl_upoly_cst *cst;
200 if (!isl_upoly_is_cst(up))
203 cst = isl_upoly_as_cst(up);
207 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
210 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
212 struct isl_upoly_cst *cst;
214 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
223 isl_int_init(cst->n);
224 isl_int_init(cst->d);
229 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
231 struct isl_upoly_cst *cst;
233 cst = isl_upoly_cst_alloc(ctx);
237 isl_int_set_si(cst->n, 0);
238 isl_int_set_si(cst->d, 1);
243 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
245 struct isl_upoly_cst *cst;
247 cst = isl_upoly_cst_alloc(ctx);
251 isl_int_set_si(cst->n, 1);
252 isl_int_set_si(cst->d, 0);
257 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
259 struct isl_upoly_cst *cst;
261 cst = isl_upoly_cst_alloc(ctx);
265 isl_int_set_si(cst->n, -1);
266 isl_int_set_si(cst->d, 0);
271 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
273 struct isl_upoly_cst *cst;
275 cst = isl_upoly_cst_alloc(ctx);
279 isl_int_set_si(cst->n, 0);
280 isl_int_set_si(cst->d, 0);
285 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
286 isl_int n, isl_int d)
288 struct isl_upoly_cst *cst;
290 cst = isl_upoly_cst_alloc(ctx);
294 isl_int_set(cst->n, n);
295 isl_int_set(cst->d, d);
300 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
303 struct isl_upoly_rec *rec;
305 isl_assert(ctx, var >= 0, return NULL);
306 isl_assert(ctx, size >= 0, return NULL);
307 rec = isl_calloc(ctx, struct isl_upoly_rec,
308 sizeof(struct isl_upoly_rec) +
309 (size - 1) * sizeof(struct isl_upoly *));
324 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
325 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
327 qp = isl_qpolynomial_cow(qp);
331 isl_dim_free(qp->dim);
336 isl_qpolynomial_free(qp);
341 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
343 return qp ? qp->dim->ctx : NULL;
346 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
348 return qp ? isl_dim_copy(qp->dim) : NULL;
351 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
352 enum isl_dim_type type)
354 return qp ? isl_dim_size(qp->dim, type) : 0;
357 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
359 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
362 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
364 return qp ? isl_upoly_is_one(qp->upoly) : -1;
367 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
369 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
372 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
374 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
377 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
379 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
382 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
384 return qp ? isl_upoly_sgn(qp->upoly) : 0;
387 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
389 isl_int_clear(cst->n);
390 isl_int_clear(cst->d);
393 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
397 for (i = 0; i < rec->n; ++i)
398 isl_upoly_free(rec->p[i]);
401 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
410 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
412 struct isl_upoly_cst *cst;
413 struct isl_upoly_cst *dup;
415 cst = isl_upoly_as_cst(up);
419 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
422 isl_int_set(dup->n, cst->n);
423 isl_int_set(dup->d, cst->d);
428 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
431 struct isl_upoly_rec *rec;
432 struct isl_upoly_rec *dup;
434 rec = isl_upoly_as_rec(up);
438 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
442 for (i = 0; i < rec->n; ++i) {
443 dup->p[i] = isl_upoly_copy(rec->p[i]);
451 isl_upoly_free(&dup->up);
455 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
457 struct isl_upoly *dup;
462 if (isl_upoly_is_cst(up))
463 return isl_upoly_dup_cst(up);
465 return isl_upoly_dup_rec(up);
468 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
476 return isl_upoly_dup(up);
479 void isl_upoly_free(__isl_take struct isl_upoly *up)
488 upoly_free_cst((struct isl_upoly_cst *)up);
490 upoly_free_rec((struct isl_upoly_rec *)up);
492 isl_ctx_deref(up->ctx);
496 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
501 isl_int_gcd(gcd, cst->n, cst->d);
502 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
503 isl_int_divexact(cst->n, cst->n, gcd);
504 isl_int_divexact(cst->d, cst->d, gcd);
509 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
510 __isl_take struct isl_upoly *up2)
512 struct isl_upoly_cst *cst1;
513 struct isl_upoly_cst *cst2;
515 up1 = isl_upoly_cow(up1);
519 cst1 = isl_upoly_as_cst(up1);
520 cst2 = isl_upoly_as_cst(up2);
522 if (isl_int_eq(cst1->d, cst2->d))
523 isl_int_add(cst1->n, cst1->n, cst2->n);
525 isl_int_mul(cst1->n, cst1->n, cst2->d);
526 isl_int_addmul(cst1->n, cst2->n, cst1->d);
527 isl_int_mul(cst1->d, cst1->d, cst2->d);
530 isl_upoly_cst_reduce(cst1);
540 static __isl_give struct isl_upoly *replace_by_zero(
541 __isl_take struct isl_upoly *up)
549 return isl_upoly_zero(ctx);
552 static __isl_give struct isl_upoly *replace_by_constant_term(
553 __isl_take struct isl_upoly *up)
555 struct isl_upoly_rec *rec;
556 struct isl_upoly *cst;
561 rec = isl_upoly_as_rec(up);
564 cst = isl_upoly_copy(rec->p[0]);
572 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
573 __isl_take struct isl_upoly *up2)
576 struct isl_upoly_rec *rec1, *rec2;
581 if (isl_upoly_is_nan(up1)) {
586 if (isl_upoly_is_nan(up2)) {
591 if (isl_upoly_is_zero(up1)) {
596 if (isl_upoly_is_zero(up2)) {
601 if (up1->var < up2->var)
602 return isl_upoly_sum(up2, up1);
604 if (up2->var < up1->var) {
605 struct isl_upoly_rec *rec;
606 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
610 up1 = isl_upoly_cow(up1);
611 rec = isl_upoly_as_rec(up1);
614 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
616 up1 = replace_by_constant_term(up1);
620 if (isl_upoly_is_cst(up1))
621 return isl_upoly_sum_cst(up1, up2);
623 rec1 = isl_upoly_as_rec(up1);
624 rec2 = isl_upoly_as_rec(up2);
628 if (rec1->n < rec2->n)
629 return isl_upoly_sum(up2, up1);
631 up1 = isl_upoly_cow(up1);
632 rec1 = isl_upoly_as_rec(up1);
636 for (i = rec2->n - 1; i >= 0; --i) {
637 rec1->p[i] = isl_upoly_sum(rec1->p[i],
638 isl_upoly_copy(rec2->p[i]));
641 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
642 isl_upoly_free(rec1->p[i]);
648 up1 = replace_by_zero(up1);
649 else if (rec1->n == 1)
650 up1 = replace_by_constant_term(up1);
661 __isl_give struct isl_upoly *isl_upoly_neg_cst(__isl_take struct isl_upoly *up)
663 struct isl_upoly_cst *cst;
665 if (isl_upoly_is_zero(up))
668 up = isl_upoly_cow(up);
672 cst = isl_upoly_as_cst(up);
674 isl_int_neg(cst->n, cst->n);
679 __isl_give struct isl_upoly *isl_upoly_neg(__isl_take struct isl_upoly *up)
682 struct isl_upoly_rec *rec;
687 if (isl_upoly_is_cst(up))
688 return isl_upoly_neg_cst(up);
690 up = isl_upoly_cow(up);
691 rec = isl_upoly_as_rec(up);
695 for (i = 0; i < rec->n; ++i) {
696 rec->p[i] = isl_upoly_neg(rec->p[i]);
707 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
708 __isl_take struct isl_upoly *up2)
710 struct isl_upoly_cst *cst1;
711 struct isl_upoly_cst *cst2;
713 up1 = isl_upoly_cow(up1);
717 cst1 = isl_upoly_as_cst(up1);
718 cst2 = isl_upoly_as_cst(up2);
720 isl_int_mul(cst1->n, cst1->n, cst2->n);
721 isl_int_mul(cst1->d, cst1->d, cst2->d);
723 isl_upoly_cst_reduce(cst1);
733 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
734 __isl_take struct isl_upoly *up2)
736 struct isl_upoly_rec *rec1;
737 struct isl_upoly_rec *rec2;
738 struct isl_upoly_rec *res;
742 rec1 = isl_upoly_as_rec(up1);
743 rec2 = isl_upoly_as_rec(up2);
746 size = rec1->n + rec2->n - 1;
747 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
751 for (i = 0; i < rec1->n; ++i) {
752 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
753 isl_upoly_copy(rec1->p[i]));
758 for (; i < size; ++i) {
759 res->p[i] = isl_upoly_zero(up1->ctx);
764 for (i = 0; i < rec1->n; ++i) {
765 for (j = 1; j < rec2->n; ++j) {
766 struct isl_upoly *up;
767 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
768 isl_upoly_copy(rec1->p[i]));
769 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
782 isl_upoly_free(&res->up);
786 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
787 __isl_take struct isl_upoly *up2)
792 if (isl_upoly_is_nan(up1)) {
797 if (isl_upoly_is_nan(up2)) {
802 if (isl_upoly_is_zero(up1)) {
807 if (isl_upoly_is_zero(up2)) {
812 if (isl_upoly_is_one(up1)) {
817 if (isl_upoly_is_one(up2)) {
822 if (up1->var < up2->var)
823 return isl_upoly_mul(up2, up1);
825 if (up2->var < up1->var) {
827 struct isl_upoly_rec *rec;
828 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
829 isl_ctx *ctx = up1->ctx;
832 return isl_upoly_nan(ctx);
834 up1 = isl_upoly_cow(up1);
835 rec = isl_upoly_as_rec(up1);
839 for (i = 0; i < rec->n; ++i) {
840 rec->p[i] = isl_upoly_mul(rec->p[i],
841 isl_upoly_copy(up2));
849 if (isl_upoly_is_cst(up1))
850 return isl_upoly_mul_cst(up1, up2);
852 return isl_upoly_mul_rec(up1, up2);
859 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
860 unsigned n_div, __isl_take struct isl_upoly *up)
862 struct isl_qpolynomial *qp = NULL;
868 total = isl_dim_total(dim);
870 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
875 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
886 isl_qpolynomial_free(qp);
890 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
899 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
901 struct isl_qpolynomial *dup;
906 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
907 isl_upoly_copy(qp->upoly));
910 isl_mat_free(dup->div);
911 dup->div = isl_mat_copy(qp->div);
917 isl_qpolynomial_free(dup);
921 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
929 return isl_qpolynomial_dup(qp);
932 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
940 isl_dim_free(qp->dim);
941 isl_mat_free(qp->div);
942 isl_upoly_free(qp->upoly);
947 __isl_give struct isl_upoly *isl_upoly_pow(isl_ctx *ctx, int pos, int power)
950 struct isl_upoly *up;
951 struct isl_upoly_rec *rec;
952 struct isl_upoly_cst *cst;
954 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
957 for (i = 0; i < 1 + power; ++i) {
958 rec->p[i] = isl_upoly_zero(ctx);
963 cst = isl_upoly_as_cst(rec->p[power]);
964 isl_int_set_si(cst->n, 1);
968 isl_upoly_free(&rec->up);
972 /* r array maps original positions to new positions.
974 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
978 struct isl_upoly_rec *rec;
979 struct isl_upoly *base;
980 struct isl_upoly *res;
982 if (isl_upoly_is_cst(up))
985 rec = isl_upoly_as_rec(up);
989 isl_assert(up->ctx, rec->n >= 1, goto error);
991 base = isl_upoly_pow(up->ctx, r[up->var], 1);
992 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
994 for (i = rec->n - 2; i >= 0; --i) {
995 res = isl_upoly_mul(res, isl_upoly_copy(base));
996 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
999 isl_upoly_free(base);
1008 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1013 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1014 div1->n_col >= div2->n_col, return -1);
1016 if (div1->n_row == div2->n_row)
1017 return isl_mat_is_equal(div1, div2);
1019 n_row = div1->n_row;
1020 n_col = div1->n_col;
1021 div1->n_row = div2->n_row;
1022 div1->n_col = div2->n_col;
1024 equal = isl_mat_is_equal(div1, div2);
1026 div1->n_row = n_row;
1027 div1->n_col = n_col;
1032 static void expand_row(__isl_keep isl_mat *dst, int d,
1033 __isl_keep isl_mat *src, int s, int *exp)
1036 unsigned c = src->n_col - src->n_row;
1038 isl_seq_cpy(dst->row[d], src->row[s], c);
1039 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
1041 for (i = 0; i < s; ++i)
1042 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
1045 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1049 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1050 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1055 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1058 struct isl_div_sort_info {
1063 static int div_sort_cmp(const void *p1, const void *p2)
1065 const struct isl_div_sort_info *i1, *i2;
1066 i1 = (const struct isl_div_sort_info *) p1;
1067 i2 = (const struct isl_div_sort_info *) p2;
1069 return cmp_row(i1->div, i1->row, i2->row);
1072 /* Sort divs and remove duplicates.
1074 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1079 struct isl_div_sort_info *array = NULL;
1080 int *pos = NULL, *at = NULL;
1081 int *reordering = NULL;
1086 if (qp->div->n_row <= 1)
1089 div_pos = isl_dim_total(qp->dim);
1091 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1093 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1094 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1095 len = qp->div->n_col - 2;
1096 reordering = isl_alloc_array(qp->div->ctx, int, len);
1097 if (!array || !pos || !at || !reordering)
1100 for (i = 0; i < qp->div->n_row; ++i) {
1101 array[i].div = qp->div;
1107 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1110 for (i = 0; i < div_pos; ++i)
1113 for (i = 0; i < qp->div->n_row; ++i) {
1114 if (pos[array[i].row] == i)
1116 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1117 pos[at[i]] = pos[array[i].row];
1118 at[pos[array[i].row]] = at[i];
1119 at[i] = array[i].row;
1120 pos[array[i].row] = i;
1124 for (i = 0; i < len - div_pos; ++i) {
1126 isl_seq_eq(qp->div->row[i - skip - 1],
1127 qp->div->row[i - skip], qp->div->n_col)) {
1128 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1129 qp->div = isl_mat_drop_cols(qp->div,
1130 2 + div_pos + i - skip, 1);
1133 reordering[div_pos + array[i].row] = div_pos + i - skip;
1136 qp->upoly = reorder(qp->upoly, reordering);
1138 if (!qp->upoly || !qp->div)
1152 isl_qpolynomial_free(qp);
1156 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1157 __isl_keep isl_mat *div2, int *exp1, int *exp2)
1160 isl_mat *div = NULL;
1161 unsigned d = div1->n_col - div1->n_row;
1163 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1164 d + div1->n_row + div2->n_row);
1168 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1171 expand_row(div, k, div1, i, exp1);
1172 expand_row(div, k + 1, div2, j, exp2);
1174 cmp = cmp_row(div, k, k + 1);
1178 } else if (cmp < 0) {
1182 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1185 for (; i < div1->n_row; ++i, ++k) {
1186 expand_row(div, k, div1, i, exp1);
1189 for (; j < div2->n_row; ++j, ++k) {
1190 expand_row(div, k, div2, j, exp2);
1200 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1201 int *exp, int first)
1204 struct isl_upoly_rec *rec;
1206 if (isl_upoly_is_cst(up))
1209 if (up->var < first)
1212 if (exp[up->var - first] == up->var - first)
1215 up = isl_upoly_cow(up);
1219 up->var = exp[up->var - first] + first;
1221 rec = isl_upoly_as_rec(up);
1225 for (i = 0; i < rec->n; ++i) {
1226 rec->p[i] = expand(rec->p[i], exp, first);
1237 static __isl_give isl_qpolynomial *with_merged_divs(
1238 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1239 __isl_take isl_qpolynomial *qp2),
1240 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1244 isl_mat *div = NULL;
1246 qp1 = isl_qpolynomial_cow(qp1);
1247 qp2 = isl_qpolynomial_cow(qp2);
1252 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1253 qp1->div->n_col >= qp2->div->n_col, goto error);
1255 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1256 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1260 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1264 isl_mat_free(qp1->div);
1265 qp1->div = isl_mat_copy(div);
1266 isl_mat_free(qp2->div);
1267 qp2->div = isl_mat_copy(div);
1269 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1270 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1272 if (!qp1->upoly || !qp2->upoly)
1279 return fn(qp1, qp2);
1284 isl_qpolynomial_free(qp1);
1285 isl_qpolynomial_free(qp2);
1289 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1290 __isl_take isl_qpolynomial *qp2)
1292 qp1 = isl_qpolynomial_cow(qp1);
1297 if (qp1->div->n_row < qp2->div->n_row)
1298 return isl_qpolynomial_add(qp2, qp1);
1300 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1301 if (!compatible_divs(qp1->div, qp2->div))
1302 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1304 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1308 isl_qpolynomial_free(qp2);
1312 isl_qpolynomial_free(qp1);
1313 isl_qpolynomial_free(qp2);
1317 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1318 __isl_keep isl_set *dom,
1319 __isl_take isl_qpolynomial *qp1,
1320 __isl_take isl_qpolynomial *qp2)
1322 return isl_qpolynomial_add(qp1, qp2);
1325 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1326 __isl_take isl_qpolynomial *qp2)
1328 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1331 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1333 qp = isl_qpolynomial_cow(qp);
1338 qp->upoly = isl_upoly_neg(qp->upoly);
1344 isl_qpolynomial_free(qp);
1348 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1349 __isl_take isl_qpolynomial *qp2)
1351 qp1 = isl_qpolynomial_cow(qp1);
1356 if (qp1->div->n_row < qp2->div->n_row)
1357 return isl_qpolynomial_mul(qp2, qp1);
1359 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1360 if (!compatible_divs(qp1->div, qp2->div))
1361 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1363 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1367 isl_qpolynomial_free(qp2);
1371 isl_qpolynomial_free(qp1);
1372 isl_qpolynomial_free(qp2);
1376 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1378 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1381 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1383 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1386 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1388 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1391 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1393 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1396 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1399 struct isl_qpolynomial *qp;
1400 struct isl_upoly_cst *cst;
1402 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1406 cst = isl_upoly_as_cst(qp->upoly);
1407 isl_int_set(cst->n, v);
1412 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1413 isl_int *n, isl_int *d)
1415 struct isl_upoly_cst *cst;
1420 if (!isl_upoly_is_cst(qp->upoly))
1423 cst = isl_upoly_as_cst(qp->upoly);
1428 isl_int_set(*n, cst->n);
1430 isl_int_set(*d, cst->d);
1435 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1438 struct isl_upoly_rec *rec;
1446 rec = isl_upoly_as_rec(up);
1453 isl_assert(up->ctx, rec->n > 1, return -1);
1455 is_cst = isl_upoly_is_cst(rec->p[1]);
1461 return isl_upoly_is_affine(rec->p[0]);
1464 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1469 if (qp->div->n_row > 0)
1472 return isl_upoly_is_affine(qp->upoly);
1475 static void update_coeff(__isl_keep isl_vec *aff,
1476 __isl_keep struct isl_upoly_cst *cst, int pos)
1481 if (isl_int_is_zero(cst->n))
1486 isl_int_gcd(gcd, cst->d, aff->el[0]);
1487 isl_int_divexact(f, cst->d, gcd);
1488 isl_int_divexact(gcd, aff->el[0], gcd);
1489 isl_seq_scale(aff->el, aff->el, f, aff->size);
1490 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1495 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1496 __isl_keep isl_vec *aff)
1498 struct isl_upoly_cst *cst;
1499 struct isl_upoly_rec *rec;
1505 struct isl_upoly_cst *cst;
1507 cst = isl_upoly_as_cst(up);
1510 update_coeff(aff, cst, 0);
1514 rec = isl_upoly_as_rec(up);
1517 isl_assert(up->ctx, rec->n == 2, return -1);
1519 cst = isl_upoly_as_cst(rec->p[1]);
1522 update_coeff(aff, cst, 1 + up->var);
1524 return isl_upoly_update_affine(rec->p[0], aff);
1527 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1528 __isl_keep isl_qpolynomial *qp)
1536 isl_assert(qp->div->ctx, qp->div->n_row == 0, return NULL);
1537 d = isl_dim_total(qp->dim);
1538 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
1542 isl_seq_clr(aff->el + 1, 1 + d);
1543 isl_int_set_si(aff->el[0], 1);
1545 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1554 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1555 __isl_keep isl_qpolynomial *qp2)
1560 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1563 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1566 struct isl_upoly_rec *rec;
1568 if (isl_upoly_is_cst(up)) {
1569 struct isl_upoly_cst *cst;
1570 cst = isl_upoly_as_cst(up);
1573 isl_int_lcm(*d, *d, cst->d);
1577 rec = isl_upoly_as_rec(up);
1581 for (i = 0; i < rec->n; ++i)
1582 upoly_update_den(rec->p[i], d);
1585 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1587 isl_int_set_si(*d, 1);
1590 upoly_update_den(qp->upoly, d);
1593 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_dim *dim,
1596 struct isl_ctx *ctx;
1603 return isl_qpolynomial_alloc(dim, 0, isl_upoly_pow(ctx, pos, power));
1606 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1607 enum isl_dim_type type, unsigned pos)
1612 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1613 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1615 if (type == isl_dim_set)
1616 pos += isl_dim_size(dim, isl_dim_param);
1618 return isl_qpolynomial_pow(dim, pos, 1);
1624 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
1627 struct isl_qpolynomial *qp = NULL;
1628 struct isl_upoly_rec *rec;
1629 struct isl_upoly_cst *cst;
1635 isl_assert(div->ctx, div->bmap->n_div == 1, goto error);
1637 pos = isl_dim_total(div->bmap->dim);
1638 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
1639 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap), 1,
1644 isl_seq_cpy(qp->div->row[0], div->line[0], qp->div->n_col - 1);
1645 isl_int_set_si(qp->div->row[0][qp->div->n_col - 1], 0);
1647 for (i = 0; i < 1 + power; ++i) {
1648 rec->p[i] = isl_upoly_zero(div->ctx);
1653 cst = isl_upoly_as_cst(rec->p[power]);
1654 isl_int_set_si(cst->n, 1);
1660 isl_qpolynomial_free(qp);
1665 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
1667 return isl_qpolynomial_div_pow(div, 1);
1670 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
1671 const isl_int n, const isl_int d)
1673 struct isl_qpolynomial *qp;
1674 struct isl_upoly_cst *cst;
1676 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1680 cst = isl_upoly_as_cst(qp->upoly);
1681 isl_int_set(cst->n, n);
1682 isl_int_set(cst->d, d);
1687 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
1689 struct isl_upoly_rec *rec;
1695 if (isl_upoly_is_cst(up))
1699 active[up->var] = 1;
1701 rec = isl_upoly_as_rec(up);
1702 for (i = 0; i < rec->n; ++i)
1703 if (up_set_active(rec->p[i], active, d) < 0)
1709 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
1712 int d = isl_dim_total(qp->dim);
1717 for (i = 0; i < d; ++i)
1718 for (j = 0; j < qp->div->n_row; ++j) {
1719 if (isl_int_is_zero(qp->div->row[j][2 + i]))
1725 return up_set_active(qp->upoly, active, d);
1728 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
1729 enum isl_dim_type type, unsigned first, unsigned n)
1740 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1742 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1743 type == isl_dim_set, return -1);
1745 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
1746 if (set_active(qp, active) < 0)
1749 if (type == isl_dim_set)
1750 first += isl_dim_size(qp->dim, isl_dim_param);
1751 for (i = 0; i < n; ++i)
1752 if (active[first + i]) {
1765 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
1766 unsigned first, unsigned n)
1769 struct isl_upoly_rec *rec;
1773 if (n == 0 || up->var < 0 || up->var < first)
1775 if (up->var < first + n) {
1776 up = replace_by_constant_term(up);
1777 return isl_upoly_drop(up, first, n);
1779 up = isl_upoly_cow(up);
1783 rec = isl_upoly_as_rec(up);
1787 for (i = 0; i < rec->n; ++i) {
1788 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
1799 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
1800 __isl_take isl_qpolynomial *qp,
1801 enum isl_dim_type type, unsigned first, unsigned n)
1805 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
1808 qp = isl_qpolynomial_cow(qp);
1812 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1814 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1815 type == isl_dim_set, goto error);
1817 qp->dim = isl_dim_drop(qp->dim, type, first, n);
1821 if (type == isl_dim_set)
1822 first += isl_dim_size(qp->dim, isl_dim_param);
1824 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
1828 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
1834 isl_qpolynomial_free(qp);
1838 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1839 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1842 struct isl_upoly_rec *rec;
1843 struct isl_upoly *base, *res;
1848 if (isl_upoly_is_cst(up))
1851 if (up->var < first)
1854 rec = isl_upoly_as_rec(up);
1858 isl_assert(up->ctx, rec->n >= 1, goto error);
1860 if (up->var >= first + n)
1861 base = isl_upoly_pow(up->ctx, up->var, 1);
1863 base = isl_upoly_copy(subs[up->var - first]);
1865 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1866 for (i = rec->n - 2; i >= 0; --i) {
1867 struct isl_upoly *t;
1868 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1869 res = isl_upoly_mul(res, isl_upoly_copy(base));
1870 res = isl_upoly_sum(res, t);
1873 isl_upoly_free(base);
1882 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1883 isl_int denom, unsigned len)
1886 struct isl_upoly *up;
1888 isl_assert(ctx, len >= 1, return NULL);
1890 up = isl_upoly_rat_cst(ctx, f[0], denom);
1891 for (i = 0; i < len - 1; ++i) {
1892 struct isl_upoly *t;
1893 struct isl_upoly *c;
1895 if (isl_int_is_zero(f[1 + i]))
1898 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1899 t = isl_upoly_pow(ctx, i, 1);
1900 t = isl_upoly_mul(c, t);
1901 up = isl_upoly_sum(up, t);
1907 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
1908 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
1913 struct isl_upoly *up;
1917 if (eq->n_eq == 0) {
1918 isl_basic_set_free(eq);
1922 qp = isl_qpolynomial_cow(qp);
1925 qp->div = isl_mat_cow(qp->div);
1929 total = 1 + isl_dim_total(eq->dim);
1930 isl_int_init(denom);
1931 for (i = 0; i < eq->n_eq; ++i) {
1932 j = isl_seq_last_non_zero(eq->eq[i], total);
1933 if (j < 0 || j == 0)
1936 for (k = 0; k < qp->div->n_row; ++k) {
1937 if (isl_int_is_zero(qp->div->row[k][1 + j]))
1939 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
1940 &qp->div->row[k][0]);
1941 isl_seq_normalize(qp->div->ctx,
1942 qp->div->row[k], 1 + total);
1945 if (isl_int_is_pos(eq->eq[i][j]))
1946 isl_seq_neg(eq->eq[i], eq->eq[i], total);
1947 isl_int_abs(denom, eq->eq[i][j]);
1948 isl_int_set_si(eq->eq[i][j], 0);
1950 up = isl_upoly_from_affine(qp->dim->ctx,
1951 eq->eq[i], denom, total);
1952 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
1955 isl_int_clear(denom);
1960 isl_basic_set_free(eq);
1966 isl_basic_set_free(eq);
1967 isl_qpolynomial_free(qp);
1972 #define PW isl_pw_qpolynomial
1974 #define EL isl_qpolynomial
1976 #define IS_ZERO is_zero
1980 #include <isl_pw_templ.c>
1983 #define UNION isl_union_pw_qpolynomial
1985 #define PART isl_pw_qpolynomial
1987 #define PARTS pw_qpolynomial
1989 #include <isl_union_templ.c>
1991 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
1999 if (!isl_set_fast_is_universe(pwqp->p[0].set))
2002 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2005 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2006 __isl_take isl_pw_qpolynomial *pwqp1,
2007 __isl_take isl_pw_qpolynomial *pwqp2)
2010 struct isl_pw_qpolynomial *res;
2013 if (!pwqp1 || !pwqp2)
2016 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2019 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2020 isl_pw_qpolynomial_free(pwqp2);
2024 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2025 isl_pw_qpolynomial_free(pwqp1);
2029 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2030 isl_pw_qpolynomial_free(pwqp1);
2034 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2035 isl_pw_qpolynomial_free(pwqp2);
2039 n = pwqp1->n * pwqp2->n;
2040 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2042 for (i = 0; i < pwqp1->n; ++i) {
2043 for (j = 0; j < pwqp2->n; ++j) {
2044 struct isl_set *common;
2045 struct isl_qpolynomial *prod;
2046 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2047 isl_set_copy(pwqp2->p[j].set));
2048 if (isl_set_fast_is_empty(common)) {
2049 isl_set_free(common);
2053 prod = isl_qpolynomial_mul(
2054 isl_qpolynomial_copy(pwqp1->p[i].qp),
2055 isl_qpolynomial_copy(pwqp2->p[j].qp));
2057 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2061 isl_pw_qpolynomial_free(pwqp1);
2062 isl_pw_qpolynomial_free(pwqp2);
2066 isl_pw_qpolynomial_free(pwqp1);
2067 isl_pw_qpolynomial_free(pwqp2);
2071 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2072 __isl_take isl_pw_qpolynomial *pwqp)
2079 if (isl_pw_qpolynomial_is_zero(pwqp))
2082 pwqp = isl_pw_qpolynomial_cow(pwqp);
2086 for (i = 0; i < pwqp->n; ++i) {
2087 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2094 isl_pw_qpolynomial_free(pwqp);
2098 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2099 __isl_take isl_pw_qpolynomial *pwqp1,
2100 __isl_take isl_pw_qpolynomial *pwqp2)
2102 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2105 __isl_give struct isl_upoly *isl_upoly_eval(
2106 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2109 struct isl_upoly_rec *rec;
2110 struct isl_upoly *res;
2111 struct isl_upoly *base;
2113 if (isl_upoly_is_cst(up)) {
2118 rec = isl_upoly_as_rec(up);
2122 isl_assert(up->ctx, rec->n >= 1, goto error);
2124 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2126 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2129 for (i = rec->n - 2; i >= 0; --i) {
2130 res = isl_upoly_mul(res, isl_upoly_copy(base));
2131 res = isl_upoly_sum(res,
2132 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2133 isl_vec_copy(vec)));
2136 isl_upoly_free(base);
2146 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2147 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2150 struct isl_upoly *up;
2155 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2157 if (qp->div->n_row == 0)
2158 ext = isl_vec_copy(pnt->vec);
2161 unsigned dim = isl_dim_total(qp->dim);
2162 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2166 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2167 for (i = 0; i < qp->div->n_row; ++i) {
2168 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2169 1 + dim + i, &ext->el[1+dim+i]);
2170 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2171 qp->div->row[i][0]);
2175 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2179 dim = isl_dim_copy(qp->dim);
2180 isl_qpolynomial_free(qp);
2181 isl_point_free(pnt);
2183 return isl_qpolynomial_alloc(dim, 0, up);
2185 isl_qpolynomial_free(qp);
2186 isl_point_free(pnt);
2190 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2191 __isl_keep struct isl_upoly_cst *cst2)
2196 isl_int_mul(t, cst1->n, cst2->d);
2197 isl_int_submul(t, cst2->n, cst1->d);
2198 cmp = isl_int_sgn(t);
2203 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2204 __isl_keep isl_qpolynomial *qp2)
2206 struct isl_upoly_cst *cst1, *cst2;
2210 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2211 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2212 if (isl_qpolynomial_is_nan(qp1))
2214 if (isl_qpolynomial_is_nan(qp2))
2216 cst1 = isl_upoly_as_cst(qp1->upoly);
2217 cst2 = isl_upoly_as_cst(qp2->upoly);
2219 return isl_upoly_cmp(cst1, cst2) <= 0;
2222 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2223 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2225 struct isl_upoly_cst *cst1, *cst2;
2230 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2231 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2232 cst1 = isl_upoly_as_cst(qp1->upoly);
2233 cst2 = isl_upoly_as_cst(qp2->upoly);
2234 cmp = isl_upoly_cmp(cst1, cst2);
2237 isl_qpolynomial_free(qp2);
2239 isl_qpolynomial_free(qp1);
2244 isl_qpolynomial_free(qp1);
2245 isl_qpolynomial_free(qp2);
2249 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2250 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2252 struct isl_upoly_cst *cst1, *cst2;
2257 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2258 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2259 cst1 = isl_upoly_as_cst(qp1->upoly);
2260 cst2 = isl_upoly_as_cst(qp2->upoly);
2261 cmp = isl_upoly_cmp(cst1, cst2);
2264 isl_qpolynomial_free(qp2);
2266 isl_qpolynomial_free(qp1);
2271 isl_qpolynomial_free(qp1);
2272 isl_qpolynomial_free(qp2);
2276 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2277 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2278 unsigned first, unsigned n)
2287 qp = isl_qpolynomial_cow(qp);
2291 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2294 g_pos = pos(qp->dim, type) + first;
2296 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2300 total = qp->div->n_col - 2;
2301 if (total > g_pos) {
2303 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2306 for (i = 0; i < total - g_pos; ++i)
2308 qp->upoly = expand(qp->upoly, exp, g_pos);
2314 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2320 isl_qpolynomial_free(qp);
2324 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2325 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2329 pos = isl_qpolynomial_dim(qp, type);
2331 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2334 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2335 __isl_take isl_pw_qpolynomial *pwqp,
2336 enum isl_dim_type type, unsigned n)
2340 pos = isl_pw_qpolynomial_dim(pwqp, type);
2342 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2345 static int *reordering_move(isl_ctx *ctx,
2346 unsigned len, unsigned dst, unsigned src, unsigned n)
2351 reordering = isl_alloc_array(ctx, int, len);
2356 for (i = 0; i < dst; ++i)
2358 for (i = 0; i < n; ++i)
2359 reordering[src + i] = dst + i;
2360 for (i = 0; i < src - dst; ++i)
2361 reordering[dst + i] = dst + n + i;
2362 for (i = 0; i < len - src - n; ++i)
2363 reordering[src + n + i] = src + n + i;
2365 for (i = 0; i < src; ++i)
2367 for (i = 0; i < n; ++i)
2368 reordering[src + i] = dst + i;
2369 for (i = 0; i < dst - src; ++i)
2370 reordering[src + n + i] = src + i;
2371 for (i = 0; i < len - dst - n; ++i)
2372 reordering[dst + n + i] = dst + n + i;
2378 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2379 __isl_take isl_qpolynomial *qp,
2380 enum isl_dim_type dst_type, unsigned dst_pos,
2381 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2387 qp = isl_qpolynomial_cow(qp);
2391 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2394 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2395 g_src_pos = pos(qp->dim, src_type) + src_pos;
2396 if (dst_type > src_type)
2399 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2406 reordering = reordering_move(qp->dim->ctx,
2407 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2411 qp->upoly = reorder(qp->upoly, reordering);
2416 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2422 isl_qpolynomial_free(qp);
2426 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2427 isl_int *f, isl_int denom)
2429 struct isl_upoly *up;
2434 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2436 return isl_qpolynomial_alloc(dim, 0, up);
2439 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2440 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2444 struct isl_upoly *up;
2445 isl_qpolynomial *qp;
2451 isl_int_init(denom);
2453 isl_constraint_get_coefficient(c, type, pos, &denom);
2454 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2455 sgn = isl_int_sgn(denom);
2456 isl_int_abs(denom, denom);
2457 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2458 1 + isl_constraint_dim(c, isl_dim_all));
2460 isl_int_neg(denom, denom);
2461 isl_constraint_set_coefficient(c, type, pos, denom);
2463 dim = isl_dim_copy(c->bmap->dim);
2465 isl_int_clear(denom);
2466 isl_constraint_free(c);
2468 qp = isl_qpolynomial_alloc(dim, 0, up);
2470 qp = isl_qpolynomial_neg(qp);
2474 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2475 * in "qp" by subs[i].
2477 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2478 __isl_take isl_qpolynomial *qp,
2479 enum isl_dim_type type, unsigned first, unsigned n,
2480 __isl_keep isl_qpolynomial **subs)
2483 struct isl_upoly **ups;
2488 qp = isl_qpolynomial_cow(qp);
2491 for (i = 0; i < n; ++i)
2495 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2498 for (i = 0; i < n; ++i)
2499 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2502 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2503 for (i = 0; i < n; ++i)
2504 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2506 first += pos(qp->dim, type);
2508 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2511 for (i = 0; i < n; ++i)
2512 ups[i] = subs[i]->upoly;
2514 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2523 isl_qpolynomial_free(qp);
2527 __isl_give isl_basic_set *add_div_constraints(__isl_take isl_basic_set *bset,
2528 __isl_take isl_mat *div)
2536 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2539 total = isl_basic_set_total_dim(bset);
2540 for (i = 0; i < div->n_row; ++i)
2541 if (isl_basic_set_add_div_constraints_var(bset,
2542 total - div->n_row + i, div->row[i]) < 0)
2549 isl_basic_set_free(bset);
2553 /* Extend "bset" with extra set dimensions for each integer division
2554 * in "qp" and then call "fn" with the extended bset and the polynomial
2555 * that results from replacing each of the integer divisions by the
2556 * corresponding extra set dimension.
2558 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
2559 __isl_keep isl_basic_set *bset,
2560 int (*fn)(__isl_take isl_basic_set *bset,
2561 __isl_take isl_qpolynomial *poly, void *user), void *user)
2565 isl_qpolynomial *poly;
2569 if (qp->div->n_row == 0)
2570 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
2573 div = isl_mat_copy(qp->div);
2574 dim = isl_dim_copy(qp->dim);
2575 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
2576 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
2577 bset = isl_basic_set_copy(bset);
2578 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
2579 bset = add_div_constraints(bset, div);
2581 return fn(bset, poly, user);
2586 /* Return total degree in variables first (inclusive) up to last (exclusive).
2588 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
2592 struct isl_upoly_rec *rec;
2596 if (isl_upoly_is_zero(up))
2598 if (isl_upoly_is_cst(up) || up->var < first)
2601 rec = isl_upoly_as_rec(up);
2605 for (i = 0; i < rec->n; ++i) {
2608 if (isl_upoly_is_zero(rec->p[i]))
2610 d = isl_upoly_degree(rec->p[i], first, last);
2620 /* Return total degree in set variables.
2622 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
2630 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2631 nvar = isl_dim_size(poly->dim, isl_dim_set);
2632 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
2635 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
2636 unsigned pos, int deg)
2639 struct isl_upoly_rec *rec;
2644 if (isl_upoly_is_cst(up) || up->var < pos) {
2646 return isl_upoly_copy(up);
2648 return isl_upoly_zero(up->ctx);
2651 rec = isl_upoly_as_rec(up);
2655 if (up->var == pos) {
2657 return isl_upoly_copy(rec->p[deg]);
2659 return isl_upoly_zero(up->ctx);
2662 up = isl_upoly_copy(up);
2663 up = isl_upoly_cow(up);
2664 rec = isl_upoly_as_rec(up);
2668 for (i = 0; i < rec->n; ++i) {
2669 struct isl_upoly *t;
2670 t = isl_upoly_coeff(rec->p[i], pos, deg);
2673 isl_upoly_free(rec->p[i]);
2683 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2685 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
2686 __isl_keep isl_qpolynomial *qp,
2687 enum isl_dim_type type, unsigned t_pos, int deg)
2690 struct isl_upoly *up;
2696 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
2699 g_pos = pos(qp->dim, type) + t_pos;
2700 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
2702 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
2705 isl_mat_free(c->div);
2706 c->div = isl_mat_copy(qp->div);
2711 isl_qpolynomial_free(c);
2715 /* Homogenize the polynomial in the variables first (inclusive) up to
2716 * last (exclusive) by inserting powers of variable first.
2717 * Variable first is assumed not to appear in the input.
2719 __isl_give struct isl_upoly *isl_upoly_homogenize(
2720 __isl_take struct isl_upoly *up, int deg, int target,
2721 int first, int last)
2724 struct isl_upoly_rec *rec;
2728 if (isl_upoly_is_zero(up))
2732 if (isl_upoly_is_cst(up) || up->var < first) {
2733 struct isl_upoly *hom;
2735 hom = isl_upoly_pow(up->ctx, first, target - deg);
2738 rec = isl_upoly_as_rec(hom);
2739 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
2744 up = isl_upoly_cow(up);
2745 rec = isl_upoly_as_rec(up);
2749 for (i = 0; i < rec->n; ++i) {
2750 if (isl_upoly_is_zero(rec->p[i]))
2752 rec->p[i] = isl_upoly_homogenize(rec->p[i],
2753 up->var < last ? deg + i : i, target,
2765 /* Homogenize the polynomial in the set variables by introducing
2766 * powers of an extra set variable at position 0.
2768 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
2769 __isl_take isl_qpolynomial *poly)
2773 int deg = isl_qpolynomial_degree(poly);
2778 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
2779 poly = isl_qpolynomial_cow(poly);
2783 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2784 nvar = isl_dim_size(poly->dim, isl_dim_set);
2785 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
2792 isl_qpolynomial_free(poly);
2796 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
2797 __isl_take isl_mat *div)
2805 n = isl_dim_total(dim) + div->n_row;
2807 term = isl_calloc(dim->ctx, struct isl_term,
2808 sizeof(struct isl_term) + (n - 1) * sizeof(int));
2815 isl_int_init(term->n);
2816 isl_int_init(term->d);
2825 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
2834 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
2843 total = isl_dim_total(term->dim) + term->div->n_row;
2845 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
2849 isl_int_set(dup->n, term->n);
2850 isl_int_set(dup->d, term->d);
2852 for (i = 0; i < total; ++i)
2853 dup->pow[i] = term->pow[i];
2858 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
2866 return isl_term_dup(term);
2869 void isl_term_free(__isl_take isl_term *term)
2874 if (--term->ref > 0)
2877 isl_dim_free(term->dim);
2878 isl_mat_free(term->div);
2879 isl_int_clear(term->n);
2880 isl_int_clear(term->d);
2884 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
2892 case isl_dim_out: return isl_dim_size(term->dim, type);
2893 case isl_dim_div: return term->div->n_row;
2894 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
2899 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
2901 return term ? term->dim->ctx : NULL;
2904 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
2908 isl_int_set(*n, term->n);
2911 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
2915 isl_int_set(*d, term->d);
2918 int isl_term_get_exp(__isl_keep isl_term *term,
2919 enum isl_dim_type type, unsigned pos)
2924 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
2926 if (type >= isl_dim_set)
2927 pos += isl_dim_size(term->dim, isl_dim_param);
2928 if (type >= isl_dim_div)
2929 pos += isl_dim_size(term->dim, isl_dim_set);
2931 return term->pow[pos];
2934 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
2936 isl_basic_map *bmap;
2943 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
2946 total = term->div->n_col - term->div->n_row - 2;
2947 /* No nested divs for now */
2948 isl_assert(term->dim->ctx,
2949 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
2950 term->div->n_row) == -1,
2953 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
2954 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
2957 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
2959 return isl_basic_map_div(bmap, k);
2961 isl_basic_map_free(bmap);
2965 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
2966 int (*fn)(__isl_take isl_term *term, void *user),
2967 __isl_take isl_term *term, void *user)
2970 struct isl_upoly_rec *rec;
2975 if (isl_upoly_is_zero(up))
2978 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
2979 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
2980 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
2982 if (isl_upoly_is_cst(up)) {
2983 struct isl_upoly_cst *cst;
2984 cst = isl_upoly_as_cst(up);
2987 term = isl_term_cow(term);
2990 isl_int_set(term->n, cst->n);
2991 isl_int_set(term->d, cst->d);
2992 if (fn(isl_term_copy(term), user) < 0)
2997 rec = isl_upoly_as_rec(up);
3001 for (i = 0; i < rec->n; ++i) {
3002 term = isl_term_cow(term);
3005 term->pow[up->var] = i;
3006 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3010 term->pow[up->var] = 0;
3014 isl_term_free(term);
3018 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3019 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3026 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3030 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3032 isl_term_free(term);
3034 return term ? 0 : -1;
3037 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3039 struct isl_upoly *up;
3040 isl_qpolynomial *qp;
3046 n = isl_dim_total(term->dim) + term->div->n_row;
3048 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3049 for (i = 0; i < n; ++i) {
3052 up = isl_upoly_mul(up,
3053 isl_upoly_pow(term->dim->ctx, i, term->pow[i]));
3056 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3059 isl_mat_free(qp->div);
3060 qp->div = isl_mat_copy(term->div);
3064 isl_term_free(term);
3067 isl_qpolynomial_free(qp);
3068 isl_term_free(term);
3072 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3073 __isl_take isl_dim *dim)
3082 if (isl_dim_equal(qp->dim, dim)) {
3087 qp = isl_qpolynomial_cow(qp);
3091 extra = isl_dim_size(dim, isl_dim_set) -
3092 isl_dim_size(qp->dim, isl_dim_set);
3093 total = isl_dim_total(qp->dim);
3094 if (qp->div->n_row) {
3097 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3100 for (i = 0; i < qp->div->n_row; ++i)
3102 qp->upoly = expand(qp->upoly, exp, total);
3107 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3110 for (i = 0; i < qp->div->n_row; ++i)
3111 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3113 isl_dim_free(qp->dim);
3119 isl_qpolynomial_free(qp);
3123 /* For each parameter or variable that does not appear in qp,
3124 * first eliminate the variable from all constraints and then set it to zero.
3126 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3127 __isl_keep isl_qpolynomial *qp)
3138 d = isl_dim_total(set->dim);
3139 active = isl_calloc_array(set->ctx, int, d);
3140 if (set_active(qp, active) < 0)
3143 for (i = 0; i < d; ++i)
3152 nparam = isl_dim_size(set->dim, isl_dim_param);
3153 nvar = isl_dim_size(set->dim, isl_dim_set);
3154 for (i = 0; i < nparam; ++i) {
3157 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3158 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3160 for (i = 0; i < nvar; ++i) {
3161 if (active[nparam + i])
3163 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3164 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3176 struct isl_opt_data {
3177 isl_qpolynomial *qp;
3179 isl_qpolynomial *opt;
3183 static int opt_fn(__isl_take isl_point *pnt, void *user)
3185 struct isl_opt_data *data = (struct isl_opt_data *)user;
3186 isl_qpolynomial *val;
3188 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3192 } else if (data->max) {
3193 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3195 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3201 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3202 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3204 struct isl_opt_data data = { NULL, 1, NULL, max };
3209 if (isl_upoly_is_cst(qp->upoly)) {
3214 set = fix_inactive(set, qp);
3217 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3221 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3224 isl_qpolynomial_free(qp);
3228 isl_qpolynomial_free(qp);
3229 isl_qpolynomial_free(data.opt);
3233 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3234 __isl_take isl_morph *morph)
3238 struct isl_upoly *up;
3240 struct isl_upoly **subs;
3243 qp = isl_qpolynomial_cow(qp);
3248 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3250 subs = isl_calloc_array(ctx, struct isl_upoly *, morph->inv->n_row - 1);
3254 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3255 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3256 morph->inv->row[0][0], morph->inv->n_col);
3258 qp->upoly = isl_upoly_subs(qp->upoly, 0, morph->inv->n_row - 1, subs);
3260 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3261 isl_upoly_free(subs[i]);
3264 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3265 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3266 qp->div = isl_mat_product(qp->div, mat);
3267 isl_dim_free(qp->dim);
3268 qp->dim = isl_dim_copy(morph->ran->dim);
3270 if (!qp->upoly || !qp->div || !qp->dim)
3273 isl_morph_free(morph);
3277 isl_qpolynomial_free(qp);
3278 isl_morph_free(morph);
3282 static int neg_entry(void **entry, void *user)
3284 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3286 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3288 return *pwqp ? 0 : -1;
3291 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3292 __isl_take isl_union_pw_qpolynomial *upwqp)
3294 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3298 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3299 &neg_entry, NULL) < 0)
3304 isl_union_pw_qpolynomial_free(upwqp);
3308 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3309 __isl_take isl_union_pw_qpolynomial *upwqp1,
3310 __isl_take isl_union_pw_qpolynomial *upwqp2)
3312 return isl_union_pw_qpolynomial_add(upwqp1,
3313 isl_union_pw_qpolynomial_neg(upwqp2));
3316 static int mul_entry(void **entry, void *user)
3318 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3320 struct isl_hash_table_entry *entry2;
3321 isl_pw_qpolynomial *pwpq = *entry;
3324 hash = isl_dim_get_hash(pwpq->dim);
3325 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3326 hash, &has_dim, pwpq->dim, 0);
3330 pwpq = isl_pw_qpolynomial_copy(pwpq);
3331 pwpq = isl_pw_qpolynomial_mul(pwpq,
3332 isl_pw_qpolynomial_copy(entry2->data));
3334 empty = isl_pw_qpolynomial_is_zero(pwpq);
3336 isl_pw_qpolynomial_free(pwpq);
3340 isl_pw_qpolynomial_free(pwpq);
3344 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3349 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3350 __isl_take isl_union_pw_qpolynomial *upwqp1,
3351 __isl_take isl_union_pw_qpolynomial *upwqp2)
3353 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3356 /* Reorder the columns of the given div definitions according to the
3359 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3360 __isl_take isl_reordering *r)
3369 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3370 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3374 for (i = 0; i < div->n_row; ++i) {
3375 isl_seq_cpy(mat->row[i], div->row[i], 2);
3376 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3377 for (j = 0; j < r->len; ++j)
3378 isl_int_set(mat->row[i][2 + r->pos[j]],
3379 div->row[i][2 + j]);
3382 isl_reordering_free(r);
3386 isl_reordering_free(r);
3391 /* Reorder the dimension of "qp" according to the given reordering.
3393 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3394 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3396 qp = isl_qpolynomial_cow(qp);
3400 r = isl_reordering_extend(r, qp->div->n_row);
3404 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3408 qp->upoly = reorder(qp->upoly, r->pos);
3412 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3414 isl_reordering_free(r);
3417 isl_qpolynomial_free(qp);
3418 isl_reordering_free(r);
3422 struct isl_split_periods_data {
3424 isl_pw_qpolynomial *res;
3427 /* Create a slice where the integer division "div" has the fixed value "v".
3428 * In particular, if "div" refers to floor(f/m), then create a slice
3430 * m v <= f <= m v + (m - 1)
3435 * -f + m v + (m - 1) >= 0
3437 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3438 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3441 isl_basic_set *bset = NULL;
3447 total = isl_dim_total(dim);
3448 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3450 k = isl_basic_set_alloc_inequality(bset);
3453 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3454 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3456 k = isl_basic_set_alloc_inequality(bset);
3459 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3460 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3461 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3462 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3465 return isl_set_from_basic_set(bset);
3467 isl_basic_set_free(bset);
3472 static int split_periods(__isl_take isl_set *set,
3473 __isl_take isl_qpolynomial *qp, void *user);
3475 /* Create a slice of the domain "set" such that integer division "div"
3476 * has the fixed value "v" and add the results to data->res,
3477 * replacing the integer division by "v" in "qp".
3479 static int set_div(__isl_take isl_set *set,
3480 __isl_take isl_qpolynomial *qp, int div, isl_int v,
3481 struct isl_split_periods_data *data)
3486 struct isl_upoly *cst;
3489 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3490 set = isl_set_intersect(set, slice);
3492 qp = isl_qpolynomial_cow(qp);
3496 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3499 total = isl_dim_total(qp->dim);
3500 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &cst);
3501 isl_upoly_free(cst);
3505 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
3508 for (i = 0; i < total + div; ++i)
3510 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
3511 reordering[i] = i - 1;
3512 qp->div = isl_mat_drop_rows(qp->div, div, 1);
3513 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
3514 qp->upoly = reorder(qp->upoly, reordering);
3517 if (!qp->upoly || !qp->div)
3520 return split_periods(set, qp, data);
3523 isl_qpolynomial_free(qp);
3527 /* Split the domain "set" such that integer division "div"
3528 * has a fixed value (ranging from "min" to "max") on each slice
3529 * and add the results to data->res.
3531 static int split_div(__isl_take isl_set *set,
3532 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3533 struct isl_split_periods_data *data)
3535 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3536 isl_set *set_i = isl_set_copy(set);
3537 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3539 if (set_div(set_i, qp_i, div, min, data) < 0)
3543 isl_qpolynomial_free(qp);
3547 isl_qpolynomial_free(qp);
3551 /* If "qp" refers to any integer division
3552 * that can only attain "max_periods" distinct values on "set"
3553 * then split the domain along those distinct values.
3554 * Add the results (or the original if no splitting occurs)
3557 static int split_periods(__isl_take isl_set *set,
3558 __isl_take isl_qpolynomial *qp, void *user)
3561 isl_pw_qpolynomial *pwqp;
3562 struct isl_split_periods_data *data;
3567 data = (struct isl_split_periods_data *)user;
3572 if (qp->div->n_row == 0) {
3573 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3574 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3580 total = isl_dim_total(qp->dim);
3581 for (i = 0; i < qp->div->n_row; ++i) {
3582 enum isl_lp_result lp_res;
3584 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
3585 qp->div->n_row) != -1)
3588 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
3589 set->ctx->one, &min, NULL, NULL);
3590 if (lp_res == isl_lp_error)
3592 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3594 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
3596 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
3597 set->ctx->one, &max, NULL, NULL);
3598 if (lp_res == isl_lp_error)
3600 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3602 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
3604 isl_int_sub(max, max, min);
3605 if (isl_int_cmp_si(max, data->max_periods) < 0) {
3606 isl_int_add(max, max, min);
3611 if (i < qp->div->n_row) {
3612 r = split_div(set, qp, i, min, max, data);
3614 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3615 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3627 isl_qpolynomial_free(qp);
3631 /* If any quasi-polynomial in pwqp refers to any integer division
3632 * that can only attain "max_periods" distinct values on its domain
3633 * then split the domain along those distinct values.
3635 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
3636 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
3638 struct isl_split_periods_data data;
3640 data.max_periods = max_periods;
3641 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
3643 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
3646 isl_pw_qpolynomial_free(pwqp);
3650 isl_pw_qpolynomial_free(data.res);
3651 isl_pw_qpolynomial_free(pwqp);