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,
12 #include <isl_factorization.h>
15 #include <isl_union_map_private.h>
16 #include <isl_polynomial_private.h>
17 #include <isl_point_private.h>
18 #include <isl_dim_private.h>
19 #include <isl_map_private.h>
20 #include <isl_mat_private.h>
22 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
25 case isl_dim_param: return 0;
26 case isl_dim_in: return dim->nparam;
27 case isl_dim_out: return dim->nparam + dim->n_in;
32 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
40 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
45 isl_assert(up->ctx, up->var < 0, return NULL);
47 return (struct isl_upoly_cst *)up;
50 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
55 isl_assert(up->ctx, up->var >= 0, return NULL);
57 return (struct isl_upoly_rec *)up;
60 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
61 __isl_keep struct isl_upoly *up2)
64 struct isl_upoly_rec *rec1, *rec2;
70 if (up1->var != up2->var)
72 if (isl_upoly_is_cst(up1)) {
73 struct isl_upoly_cst *cst1, *cst2;
74 cst1 = isl_upoly_as_cst(up1);
75 cst2 = isl_upoly_as_cst(up2);
78 return isl_int_eq(cst1->n, cst2->n) &&
79 isl_int_eq(cst1->d, cst2->d);
82 rec1 = isl_upoly_as_rec(up1);
83 rec2 = isl_upoly_as_rec(up2);
87 if (rec1->n != rec2->n)
90 for (i = 0; i < rec1->n; ++i) {
91 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
99 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
101 struct isl_upoly_cst *cst;
105 if (!isl_upoly_is_cst(up))
108 cst = isl_upoly_as_cst(up);
112 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
115 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
117 struct isl_upoly_cst *cst;
121 if (!isl_upoly_is_cst(up))
124 cst = isl_upoly_as_cst(up);
128 return isl_int_sgn(cst->n);
131 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
133 struct isl_upoly_cst *cst;
137 if (!isl_upoly_is_cst(up))
140 cst = isl_upoly_as_cst(up);
144 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
147 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
149 struct isl_upoly_cst *cst;
153 if (!isl_upoly_is_cst(up))
156 cst = isl_upoly_as_cst(up);
160 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
163 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
165 struct isl_upoly_cst *cst;
169 if (!isl_upoly_is_cst(up))
172 cst = isl_upoly_as_cst(up);
176 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
179 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
181 struct isl_upoly_cst *cst;
185 if (!isl_upoly_is_cst(up))
188 cst = isl_upoly_as_cst(up);
192 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
195 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
197 struct isl_upoly_cst *cst;
201 if (!isl_upoly_is_cst(up))
204 cst = isl_upoly_as_cst(up);
208 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
211 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
213 struct isl_upoly_cst *cst;
215 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
224 isl_int_init(cst->n);
225 isl_int_init(cst->d);
230 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
232 struct isl_upoly_cst *cst;
234 cst = isl_upoly_cst_alloc(ctx);
238 isl_int_set_si(cst->n, 0);
239 isl_int_set_si(cst->d, 1);
244 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
246 struct isl_upoly_cst *cst;
248 cst = isl_upoly_cst_alloc(ctx);
252 isl_int_set_si(cst->n, 1);
253 isl_int_set_si(cst->d, 1);
258 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
260 struct isl_upoly_cst *cst;
262 cst = isl_upoly_cst_alloc(ctx);
266 isl_int_set_si(cst->n, 1);
267 isl_int_set_si(cst->d, 0);
272 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
274 struct isl_upoly_cst *cst;
276 cst = isl_upoly_cst_alloc(ctx);
280 isl_int_set_si(cst->n, -1);
281 isl_int_set_si(cst->d, 0);
286 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
288 struct isl_upoly_cst *cst;
290 cst = isl_upoly_cst_alloc(ctx);
294 isl_int_set_si(cst->n, 0);
295 isl_int_set_si(cst->d, 0);
300 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
301 isl_int n, isl_int d)
303 struct isl_upoly_cst *cst;
305 cst = isl_upoly_cst_alloc(ctx);
309 isl_int_set(cst->n, n);
310 isl_int_set(cst->d, d);
315 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
318 struct isl_upoly_rec *rec;
320 isl_assert(ctx, var >= 0, return NULL);
321 isl_assert(ctx, size >= 0, return NULL);
322 rec = isl_calloc(ctx, struct isl_upoly_rec,
323 sizeof(struct isl_upoly_rec) +
324 (size - 1) * sizeof(struct isl_upoly *));
339 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
340 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
342 qp = isl_qpolynomial_cow(qp);
346 isl_dim_free(qp->dim);
351 isl_qpolynomial_free(qp);
356 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
358 return qp ? qp->dim->ctx : NULL;
361 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
363 return qp ? isl_dim_copy(qp->dim) : NULL;
366 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
367 enum isl_dim_type type)
369 return qp ? isl_dim_size(qp->dim, type) : 0;
372 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
374 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
377 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
379 return qp ? isl_upoly_is_one(qp->upoly) : -1;
382 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
384 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
387 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
389 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
392 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
394 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
397 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
399 return qp ? isl_upoly_sgn(qp->upoly) : 0;
402 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
404 isl_int_clear(cst->n);
405 isl_int_clear(cst->d);
408 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
412 for (i = 0; i < rec->n; ++i)
413 isl_upoly_free(rec->p[i]);
416 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
425 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
427 struct isl_upoly_cst *cst;
428 struct isl_upoly_cst *dup;
430 cst = isl_upoly_as_cst(up);
434 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
437 isl_int_set(dup->n, cst->n);
438 isl_int_set(dup->d, cst->d);
443 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
446 struct isl_upoly_rec *rec;
447 struct isl_upoly_rec *dup;
449 rec = isl_upoly_as_rec(up);
453 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
457 for (i = 0; i < rec->n; ++i) {
458 dup->p[i] = isl_upoly_copy(rec->p[i]);
466 isl_upoly_free(&dup->up);
470 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
472 struct isl_upoly *dup;
477 if (isl_upoly_is_cst(up))
478 return isl_upoly_dup_cst(up);
480 return isl_upoly_dup_rec(up);
483 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
491 return isl_upoly_dup(up);
494 void isl_upoly_free(__isl_take struct isl_upoly *up)
503 upoly_free_cst((struct isl_upoly_cst *)up);
505 upoly_free_rec((struct isl_upoly_rec *)up);
507 isl_ctx_deref(up->ctx);
511 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
516 isl_int_gcd(gcd, cst->n, cst->d);
517 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
518 isl_int_divexact(cst->n, cst->n, gcd);
519 isl_int_divexact(cst->d, cst->d, gcd);
524 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
525 __isl_take struct isl_upoly *up2)
527 struct isl_upoly_cst *cst1;
528 struct isl_upoly_cst *cst2;
530 up1 = isl_upoly_cow(up1);
534 cst1 = isl_upoly_as_cst(up1);
535 cst2 = isl_upoly_as_cst(up2);
537 if (isl_int_eq(cst1->d, cst2->d))
538 isl_int_add(cst1->n, cst1->n, cst2->n);
540 isl_int_mul(cst1->n, cst1->n, cst2->d);
541 isl_int_addmul(cst1->n, cst2->n, cst1->d);
542 isl_int_mul(cst1->d, cst1->d, cst2->d);
545 isl_upoly_cst_reduce(cst1);
555 static __isl_give struct isl_upoly *replace_by_zero(
556 __isl_take struct isl_upoly *up)
564 return isl_upoly_zero(ctx);
567 static __isl_give struct isl_upoly *replace_by_constant_term(
568 __isl_take struct isl_upoly *up)
570 struct isl_upoly_rec *rec;
571 struct isl_upoly *cst;
576 rec = isl_upoly_as_rec(up);
579 cst = isl_upoly_copy(rec->p[0]);
587 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
588 __isl_take struct isl_upoly *up2)
591 struct isl_upoly_rec *rec1, *rec2;
596 if (isl_upoly_is_nan(up1)) {
601 if (isl_upoly_is_nan(up2)) {
606 if (isl_upoly_is_zero(up1)) {
611 if (isl_upoly_is_zero(up2)) {
616 if (up1->var < up2->var)
617 return isl_upoly_sum(up2, up1);
619 if (up2->var < up1->var) {
620 struct isl_upoly_rec *rec;
621 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
625 up1 = isl_upoly_cow(up1);
626 rec = isl_upoly_as_rec(up1);
629 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
631 up1 = replace_by_constant_term(up1);
635 if (isl_upoly_is_cst(up1))
636 return isl_upoly_sum_cst(up1, up2);
638 rec1 = isl_upoly_as_rec(up1);
639 rec2 = isl_upoly_as_rec(up2);
643 if (rec1->n < rec2->n)
644 return isl_upoly_sum(up2, up1);
646 up1 = isl_upoly_cow(up1);
647 rec1 = isl_upoly_as_rec(up1);
651 for (i = rec2->n - 1; i >= 0; --i) {
652 rec1->p[i] = isl_upoly_sum(rec1->p[i],
653 isl_upoly_copy(rec2->p[i]));
656 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
657 isl_upoly_free(rec1->p[i]);
663 up1 = replace_by_zero(up1);
664 else if (rec1->n == 1)
665 up1 = replace_by_constant_term(up1);
676 __isl_give struct isl_upoly *isl_upoly_neg_cst(__isl_take struct isl_upoly *up)
678 struct isl_upoly_cst *cst;
680 if (isl_upoly_is_zero(up))
683 up = isl_upoly_cow(up);
687 cst = isl_upoly_as_cst(up);
689 isl_int_neg(cst->n, cst->n);
694 __isl_give struct isl_upoly *isl_upoly_neg(__isl_take struct isl_upoly *up)
697 struct isl_upoly_rec *rec;
702 if (isl_upoly_is_cst(up))
703 return isl_upoly_neg_cst(up);
705 up = isl_upoly_cow(up);
706 rec = isl_upoly_as_rec(up);
710 for (i = 0; i < rec->n; ++i) {
711 rec->p[i] = isl_upoly_neg(rec->p[i]);
722 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
723 __isl_take struct isl_upoly *up2)
725 struct isl_upoly_cst *cst1;
726 struct isl_upoly_cst *cst2;
728 up1 = isl_upoly_cow(up1);
732 cst1 = isl_upoly_as_cst(up1);
733 cst2 = isl_upoly_as_cst(up2);
735 isl_int_mul(cst1->n, cst1->n, cst2->n);
736 isl_int_mul(cst1->d, cst1->d, cst2->d);
738 isl_upoly_cst_reduce(cst1);
748 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
749 __isl_take struct isl_upoly *up2)
751 struct isl_upoly_rec *rec1;
752 struct isl_upoly_rec *rec2;
753 struct isl_upoly_rec *res;
757 rec1 = isl_upoly_as_rec(up1);
758 rec2 = isl_upoly_as_rec(up2);
761 size = rec1->n + rec2->n - 1;
762 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
766 for (i = 0; i < rec1->n; ++i) {
767 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
768 isl_upoly_copy(rec1->p[i]));
773 for (; i < size; ++i) {
774 res->p[i] = isl_upoly_zero(up1->ctx);
779 for (i = 0; i < rec1->n; ++i) {
780 for (j = 1; j < rec2->n; ++j) {
781 struct isl_upoly *up;
782 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
783 isl_upoly_copy(rec1->p[i]));
784 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
797 isl_upoly_free(&res->up);
801 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
802 __isl_take struct isl_upoly *up2)
807 if (isl_upoly_is_nan(up1)) {
812 if (isl_upoly_is_nan(up2)) {
817 if (isl_upoly_is_zero(up1)) {
822 if (isl_upoly_is_zero(up2)) {
827 if (isl_upoly_is_one(up1)) {
832 if (isl_upoly_is_one(up2)) {
837 if (up1->var < up2->var)
838 return isl_upoly_mul(up2, up1);
840 if (up2->var < up1->var) {
842 struct isl_upoly_rec *rec;
843 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
844 isl_ctx *ctx = up1->ctx;
847 return isl_upoly_nan(ctx);
849 up1 = isl_upoly_cow(up1);
850 rec = isl_upoly_as_rec(up1);
854 for (i = 0; i < rec->n; ++i) {
855 rec->p[i] = isl_upoly_mul(rec->p[i],
856 isl_upoly_copy(up2));
864 if (isl_upoly_is_cst(up1))
865 return isl_upoly_mul_cst(up1, up2);
867 return isl_upoly_mul_rec(up1, up2);
874 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
875 unsigned n_div, __isl_take struct isl_upoly *up)
877 struct isl_qpolynomial *qp = NULL;
883 total = isl_dim_total(dim);
885 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
890 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
901 isl_qpolynomial_free(qp);
905 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
914 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
916 struct isl_qpolynomial *dup;
921 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
922 isl_upoly_copy(qp->upoly));
925 isl_mat_free(dup->div);
926 dup->div = isl_mat_copy(qp->div);
932 isl_qpolynomial_free(dup);
936 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
944 return isl_qpolynomial_dup(qp);
947 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
955 isl_dim_free(qp->dim);
956 isl_mat_free(qp->div);
957 isl_upoly_free(qp->upoly);
962 __isl_give struct isl_upoly *isl_upoly_pow(isl_ctx *ctx, int pos, int power)
965 struct isl_upoly *up;
966 struct isl_upoly_rec *rec;
967 struct isl_upoly_cst *cst;
969 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
972 for (i = 0; i < 1 + power; ++i) {
973 rec->p[i] = isl_upoly_zero(ctx);
978 cst = isl_upoly_as_cst(rec->p[power]);
979 isl_int_set_si(cst->n, 1);
983 isl_upoly_free(&rec->up);
987 /* r array maps original positions to new positions.
989 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
993 struct isl_upoly_rec *rec;
994 struct isl_upoly *base;
995 struct isl_upoly *res;
997 if (isl_upoly_is_cst(up))
1000 rec = isl_upoly_as_rec(up);
1004 isl_assert(up->ctx, rec->n >= 1, goto error);
1006 base = isl_upoly_pow(up->ctx, r[up->var], 1);
1007 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1009 for (i = rec->n - 2; i >= 0; --i) {
1010 res = isl_upoly_mul(res, isl_upoly_copy(base));
1011 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1014 isl_upoly_free(base);
1023 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1028 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1029 div1->n_col >= div2->n_col, return -1);
1031 if (div1->n_row == div2->n_row)
1032 return isl_mat_is_equal(div1, div2);
1034 n_row = div1->n_row;
1035 n_col = div1->n_col;
1036 div1->n_row = div2->n_row;
1037 div1->n_col = div2->n_col;
1039 equal = isl_mat_is_equal(div1, div2);
1041 div1->n_row = n_row;
1042 div1->n_col = n_col;
1047 static void expand_row(__isl_keep isl_mat *dst, int d,
1048 __isl_keep isl_mat *src, int s, int *exp)
1051 unsigned c = src->n_col - src->n_row;
1053 isl_seq_cpy(dst->row[d], src->row[s], c);
1054 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
1056 for (i = 0; i < s; ++i)
1057 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
1060 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1064 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1065 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1070 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1073 struct isl_div_sort_info {
1078 static int div_sort_cmp(const void *p1, const void *p2)
1080 const struct isl_div_sort_info *i1, *i2;
1081 i1 = (const struct isl_div_sort_info *) p1;
1082 i2 = (const struct isl_div_sort_info *) p2;
1084 return cmp_row(i1->div, i1->row, i2->row);
1087 /* Sort divs and remove duplicates.
1089 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1094 struct isl_div_sort_info *array = NULL;
1095 int *pos = NULL, *at = NULL;
1096 int *reordering = NULL;
1101 if (qp->div->n_row <= 1)
1104 div_pos = isl_dim_total(qp->dim);
1106 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1108 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1109 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1110 len = qp->div->n_col - 2;
1111 reordering = isl_alloc_array(qp->div->ctx, int, len);
1112 if (!array || !pos || !at || !reordering)
1115 for (i = 0; i < qp->div->n_row; ++i) {
1116 array[i].div = qp->div;
1122 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1125 for (i = 0; i < div_pos; ++i)
1128 for (i = 0; i < qp->div->n_row; ++i) {
1129 if (pos[array[i].row] == i)
1131 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1132 pos[at[i]] = pos[array[i].row];
1133 at[pos[array[i].row]] = at[i];
1134 at[i] = array[i].row;
1135 pos[array[i].row] = i;
1139 for (i = 0; i < len - div_pos; ++i) {
1141 isl_seq_eq(qp->div->row[i - skip - 1],
1142 qp->div->row[i - skip], qp->div->n_col)) {
1143 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1144 qp->div = isl_mat_drop_cols(qp->div,
1145 2 + div_pos + i - skip, 1);
1148 reordering[div_pos + array[i].row] = div_pos + i - skip;
1151 qp->upoly = reorder(qp->upoly, reordering);
1153 if (!qp->upoly || !qp->div)
1167 isl_qpolynomial_free(qp);
1171 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1172 __isl_keep isl_mat *div2, int *exp1, int *exp2)
1175 isl_mat *div = NULL;
1176 unsigned d = div1->n_col - div1->n_row;
1178 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1179 d + div1->n_row + div2->n_row);
1183 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1186 expand_row(div, k, div1, i, exp1);
1187 expand_row(div, k + 1, div2, j, exp2);
1189 cmp = cmp_row(div, k, k + 1);
1193 } else if (cmp < 0) {
1197 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1200 for (; i < div1->n_row; ++i, ++k) {
1201 expand_row(div, k, div1, i, exp1);
1204 for (; j < div2->n_row; ++j, ++k) {
1205 expand_row(div, k, div2, j, exp2);
1215 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1216 int *exp, int first)
1219 struct isl_upoly_rec *rec;
1221 if (isl_upoly_is_cst(up))
1224 if (up->var < first)
1227 if (exp[up->var - first] == up->var - first)
1230 up = isl_upoly_cow(up);
1234 up->var = exp[up->var - first] + first;
1236 rec = isl_upoly_as_rec(up);
1240 for (i = 0; i < rec->n; ++i) {
1241 rec->p[i] = expand(rec->p[i], exp, first);
1252 static __isl_give isl_qpolynomial *with_merged_divs(
1253 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1254 __isl_take isl_qpolynomial *qp2),
1255 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1259 isl_mat *div = NULL;
1261 qp1 = isl_qpolynomial_cow(qp1);
1262 qp2 = isl_qpolynomial_cow(qp2);
1267 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1268 qp1->div->n_col >= qp2->div->n_col, goto error);
1270 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1271 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1275 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1279 isl_mat_free(qp1->div);
1280 qp1->div = isl_mat_copy(div);
1281 isl_mat_free(qp2->div);
1282 qp2->div = isl_mat_copy(div);
1284 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1285 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1287 if (!qp1->upoly || !qp2->upoly)
1294 return fn(qp1, qp2);
1299 isl_qpolynomial_free(qp1);
1300 isl_qpolynomial_free(qp2);
1304 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1305 __isl_take isl_qpolynomial *qp2)
1307 qp1 = isl_qpolynomial_cow(qp1);
1312 if (qp1->div->n_row < qp2->div->n_row)
1313 return isl_qpolynomial_add(qp2, qp1);
1315 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1316 if (!compatible_divs(qp1->div, qp2->div))
1317 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1319 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1323 isl_qpolynomial_free(qp2);
1327 isl_qpolynomial_free(qp1);
1328 isl_qpolynomial_free(qp2);
1332 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1333 __isl_keep isl_set *dom,
1334 __isl_take isl_qpolynomial *qp1,
1335 __isl_take isl_qpolynomial *qp2)
1337 return isl_qpolynomial_add(qp1, qp2);
1340 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1341 __isl_take isl_qpolynomial *qp2)
1343 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1346 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1348 qp = isl_qpolynomial_cow(qp);
1353 qp->upoly = isl_upoly_neg(qp->upoly);
1359 isl_qpolynomial_free(qp);
1363 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1364 __isl_take isl_qpolynomial *qp2)
1366 qp1 = isl_qpolynomial_cow(qp1);
1371 if (qp1->div->n_row < qp2->div->n_row)
1372 return isl_qpolynomial_mul(qp2, qp1);
1374 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1375 if (!compatible_divs(qp1->div, qp2->div))
1376 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1378 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1382 isl_qpolynomial_free(qp2);
1386 isl_qpolynomial_free(qp1);
1387 isl_qpolynomial_free(qp2);
1391 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1393 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1396 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1398 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1401 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1403 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1406 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1408 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1411 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1413 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1416 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1419 struct isl_qpolynomial *qp;
1420 struct isl_upoly_cst *cst;
1422 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1426 cst = isl_upoly_as_cst(qp->upoly);
1427 isl_int_set(cst->n, v);
1432 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1433 isl_int *n, isl_int *d)
1435 struct isl_upoly_cst *cst;
1440 if (!isl_upoly_is_cst(qp->upoly))
1443 cst = isl_upoly_as_cst(qp->upoly);
1448 isl_int_set(*n, cst->n);
1450 isl_int_set(*d, cst->d);
1455 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1458 struct isl_upoly_rec *rec;
1466 rec = isl_upoly_as_rec(up);
1473 isl_assert(up->ctx, rec->n > 1, return -1);
1475 is_cst = isl_upoly_is_cst(rec->p[1]);
1481 return isl_upoly_is_affine(rec->p[0]);
1484 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1489 if (qp->div->n_row > 0)
1492 return isl_upoly_is_affine(qp->upoly);
1495 static void update_coeff(__isl_keep isl_vec *aff,
1496 __isl_keep struct isl_upoly_cst *cst, int pos)
1501 if (isl_int_is_zero(cst->n))
1506 isl_int_gcd(gcd, cst->d, aff->el[0]);
1507 isl_int_divexact(f, cst->d, gcd);
1508 isl_int_divexact(gcd, aff->el[0], gcd);
1509 isl_seq_scale(aff->el, aff->el, f, aff->size);
1510 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1515 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1516 __isl_keep isl_vec *aff)
1518 struct isl_upoly_cst *cst;
1519 struct isl_upoly_rec *rec;
1525 struct isl_upoly_cst *cst;
1527 cst = isl_upoly_as_cst(up);
1530 update_coeff(aff, cst, 0);
1534 rec = isl_upoly_as_rec(up);
1537 isl_assert(up->ctx, rec->n == 2, return -1);
1539 cst = isl_upoly_as_cst(rec->p[1]);
1542 update_coeff(aff, cst, 1 + up->var);
1544 return isl_upoly_update_affine(rec->p[0], aff);
1547 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1548 __isl_keep isl_qpolynomial *qp)
1556 isl_assert(qp->div->ctx, qp->div->n_row == 0, return NULL);
1557 d = isl_dim_total(qp->dim);
1558 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
1562 isl_seq_clr(aff->el + 1, 1 + d);
1563 isl_int_set_si(aff->el[0], 1);
1565 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1574 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1575 __isl_keep isl_qpolynomial *qp2)
1580 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1583 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1586 struct isl_upoly_rec *rec;
1588 if (isl_upoly_is_cst(up)) {
1589 struct isl_upoly_cst *cst;
1590 cst = isl_upoly_as_cst(up);
1593 isl_int_lcm(*d, *d, cst->d);
1597 rec = isl_upoly_as_rec(up);
1601 for (i = 0; i < rec->n; ++i)
1602 upoly_update_den(rec->p[i], d);
1605 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1607 isl_int_set_si(*d, 1);
1610 upoly_update_den(qp->upoly, d);
1613 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_dim *dim,
1616 struct isl_ctx *ctx;
1623 return isl_qpolynomial_alloc(dim, 0, isl_upoly_pow(ctx, pos, power));
1626 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1627 enum isl_dim_type type, unsigned pos)
1632 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1633 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1635 if (type == isl_dim_set)
1636 pos += isl_dim_size(dim, isl_dim_param);
1638 return isl_qpolynomial_pow(dim, pos, 1);
1644 /* Remove common factor of non-constant terms and denominator.
1646 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1648 isl_ctx *ctx = qp->div->ctx;
1649 unsigned total = qp->div->n_col - 2;
1651 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1652 isl_int_gcd(ctx->normalize_gcd,
1653 ctx->normalize_gcd, qp->div->row[div][0]);
1654 if (isl_int_is_one(ctx->normalize_gcd))
1657 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1658 ctx->normalize_gcd, total);
1659 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1660 ctx->normalize_gcd);
1661 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1662 ctx->normalize_gcd);
1665 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
1668 struct isl_qpolynomial *qp = NULL;
1669 struct isl_upoly_rec *rec;
1670 struct isl_upoly_cst *cst;
1677 d = div->line - div->bmap->div;
1679 pos = isl_dim_total(div->bmap->dim) + d;
1680 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
1681 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
1682 div->bmap->n_div, &rec->up);
1686 for (i = 0; i < div->bmap->n_div; ++i) {
1687 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
1688 normalize_div(qp, i);
1691 for (i = 0; i < 1 + power; ++i) {
1692 rec->p[i] = isl_upoly_zero(div->ctx);
1697 cst = isl_upoly_as_cst(rec->p[power]);
1698 isl_int_set_si(cst->n, 1);
1704 isl_qpolynomial_free(qp);
1709 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
1711 return isl_qpolynomial_div_pow(div, 1);
1714 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
1715 const isl_int n, const isl_int d)
1717 struct isl_qpolynomial *qp;
1718 struct isl_upoly_cst *cst;
1720 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1724 cst = isl_upoly_as_cst(qp->upoly);
1725 isl_int_set(cst->n, n);
1726 isl_int_set(cst->d, d);
1731 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
1733 struct isl_upoly_rec *rec;
1739 if (isl_upoly_is_cst(up))
1743 active[up->var] = 1;
1745 rec = isl_upoly_as_rec(up);
1746 for (i = 0; i < rec->n; ++i)
1747 if (up_set_active(rec->p[i], active, d) < 0)
1753 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
1756 int d = isl_dim_total(qp->dim);
1761 for (i = 0; i < d; ++i)
1762 for (j = 0; j < qp->div->n_row; ++j) {
1763 if (isl_int_is_zero(qp->div->row[j][2 + i]))
1769 return up_set_active(qp->upoly, active, d);
1772 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
1773 enum isl_dim_type type, unsigned first, unsigned n)
1784 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1786 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1787 type == isl_dim_set, return -1);
1789 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
1790 if (set_active(qp, active) < 0)
1793 if (type == isl_dim_set)
1794 first += isl_dim_size(qp->dim, isl_dim_param);
1795 for (i = 0; i < n; ++i)
1796 if (active[first + i]) {
1809 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
1810 unsigned first, unsigned n)
1813 struct isl_upoly_rec *rec;
1817 if (n == 0 || up->var < 0 || up->var < first)
1819 if (up->var < first + n) {
1820 up = replace_by_constant_term(up);
1821 return isl_upoly_drop(up, first, n);
1823 up = isl_upoly_cow(up);
1827 rec = isl_upoly_as_rec(up);
1831 for (i = 0; i < rec->n; ++i) {
1832 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
1843 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
1844 __isl_take isl_qpolynomial *qp,
1845 enum isl_dim_type type, unsigned pos, const char *s)
1847 qp = isl_qpolynomial_cow(qp);
1850 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
1855 isl_qpolynomial_free(qp);
1859 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
1860 __isl_take isl_qpolynomial *qp,
1861 enum isl_dim_type type, unsigned first, unsigned n)
1865 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
1868 qp = isl_qpolynomial_cow(qp);
1872 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1874 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1875 type == isl_dim_set, goto error);
1877 qp->dim = isl_dim_drop(qp->dim, type, first, n);
1881 if (type == isl_dim_set)
1882 first += isl_dim_size(qp->dim, isl_dim_param);
1884 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
1888 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
1894 isl_qpolynomial_free(qp);
1898 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1899 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1902 struct isl_upoly_rec *rec;
1903 struct isl_upoly *base, *res;
1908 if (isl_upoly_is_cst(up))
1911 if (up->var < first)
1914 rec = isl_upoly_as_rec(up);
1918 isl_assert(up->ctx, rec->n >= 1, goto error);
1920 if (up->var >= first + n)
1921 base = isl_upoly_pow(up->ctx, up->var, 1);
1923 base = isl_upoly_copy(subs[up->var - first]);
1925 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1926 for (i = rec->n - 2; i >= 0; --i) {
1927 struct isl_upoly *t;
1928 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1929 res = isl_upoly_mul(res, isl_upoly_copy(base));
1930 res = isl_upoly_sum(res, t);
1933 isl_upoly_free(base);
1942 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1943 isl_int denom, unsigned len)
1946 struct isl_upoly *up;
1948 isl_assert(ctx, len >= 1, return NULL);
1950 up = isl_upoly_rat_cst(ctx, f[0], denom);
1951 for (i = 0; i < len - 1; ++i) {
1952 struct isl_upoly *t;
1953 struct isl_upoly *c;
1955 if (isl_int_is_zero(f[1 + i]))
1958 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1959 t = isl_upoly_pow(ctx, i, 1);
1960 t = isl_upoly_mul(c, t);
1961 up = isl_upoly_sum(up, t);
1967 /* Replace the integer division identified by "div" by the polynomial "s".
1968 * The integer division is assumed not to appear in the definition
1969 * of any other integer divisions.
1971 static __isl_give isl_qpolynomial *substitute_div(
1972 __isl_take isl_qpolynomial *qp,
1973 int div, __isl_take struct isl_upoly *s)
1982 qp = isl_qpolynomial_cow(qp);
1986 total = isl_dim_total(qp->dim);
1987 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1991 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1994 for (i = 0; i < total + div; ++i)
1996 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1997 reordering[i] = i - 1;
1998 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1999 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2000 qp->upoly = reorder(qp->upoly, reordering);
2003 if (!qp->upoly || !qp->div)
2009 isl_qpolynomial_free(qp);
2014 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2015 * divisions because d is equal to 1 by their definition, i.e., e.
2017 static __isl_give isl_qpolynomial *substitute_non_divs(
2018 __isl_take isl_qpolynomial *qp)
2022 struct isl_upoly *s;
2027 total = isl_dim_total(qp->dim);
2028 for (i = 0; qp && i < qp->div->n_row; ++i) {
2029 if (!isl_int_is_one(qp->div->row[i][0]))
2031 for (j = i + 1; j < qp->div->n_row; ++j) {
2032 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2034 isl_seq_combine(qp->div->row[j] + 1,
2035 qp->div->ctx->one, qp->div->row[j] + 1,
2036 qp->div->row[j][2 + total + i],
2037 qp->div->row[i] + 1, 1 + total + i);
2038 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2039 normalize_div(qp, j);
2041 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2042 qp->div->row[i][0], qp->div->n_col - 1);
2043 qp = substitute_div(qp, i, s);
2050 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2051 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2057 struct isl_upoly *up;
2061 if (eq->n_eq == 0) {
2062 isl_basic_set_free(eq);
2066 qp = isl_qpolynomial_cow(qp);
2069 qp->div = isl_mat_cow(qp->div);
2073 total = 1 + isl_dim_total(eq->dim);
2075 isl_int_init(denom);
2076 for (i = 0; i < eq->n_eq; ++i) {
2077 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2078 if (j < 0 || j == 0 || j >= total)
2081 for (k = 0; k < qp->div->n_row; ++k) {
2082 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2084 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2085 &qp->div->row[k][0]);
2086 normalize_div(qp, k);
2089 if (isl_int_is_pos(eq->eq[i][j]))
2090 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2091 isl_int_abs(denom, eq->eq[i][j]);
2092 isl_int_set_si(eq->eq[i][j], 0);
2094 up = isl_upoly_from_affine(qp->dim->ctx,
2095 eq->eq[i], denom, total);
2096 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2099 isl_int_clear(denom);
2104 isl_basic_set_free(eq);
2106 qp = substitute_non_divs(qp);
2111 isl_basic_set_free(eq);
2112 isl_qpolynomial_free(qp);
2116 static __isl_give isl_basic_set *add_div_constraints(
2117 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2125 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2128 total = isl_basic_set_total_dim(bset);
2129 for (i = 0; i < div->n_row; ++i)
2130 if (isl_basic_set_add_div_constraints_var(bset,
2131 total - div->n_row + i, div->row[i]) < 0)
2138 isl_basic_set_free(bset);
2142 /* Look for equalities among the variables shared by context and qp
2143 * and the integer divisions of qp, if any.
2144 * The equalities are then used to eliminate variables and/or integer
2145 * divisions from qp.
2147 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2148 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2154 if (qp->div->n_row > 0) {
2155 isl_basic_set *bset;
2156 context = isl_set_add_dims(context, isl_dim_set,
2158 bset = isl_basic_set_universe(isl_set_get_dim(context));
2159 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2160 context = isl_set_intersect(context,
2161 isl_set_from_basic_set(bset));
2164 aff = isl_set_affine_hull(context);
2165 return isl_qpolynomial_substitute_equalities(qp, aff);
2167 isl_qpolynomial_free(qp);
2168 isl_set_free(context);
2173 #define PW isl_pw_qpolynomial
2175 #define EL isl_qpolynomial
2177 #define IS_ZERO is_zero
2181 #include <isl_pw_templ.c>
2184 #define UNION isl_union_pw_qpolynomial
2186 #define PART isl_pw_qpolynomial
2188 #define PARTS pw_qpolynomial
2190 #include <isl_union_templ.c>
2192 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2200 if (!isl_set_fast_is_universe(pwqp->p[0].set))
2203 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2206 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2207 __isl_take isl_pw_qpolynomial *pwqp1,
2208 __isl_take isl_pw_qpolynomial *pwqp2)
2211 struct isl_pw_qpolynomial *res;
2214 if (!pwqp1 || !pwqp2)
2217 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2220 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2221 isl_pw_qpolynomial_free(pwqp2);
2225 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2226 isl_pw_qpolynomial_free(pwqp1);
2230 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2231 isl_pw_qpolynomial_free(pwqp1);
2235 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2236 isl_pw_qpolynomial_free(pwqp2);
2240 n = pwqp1->n * pwqp2->n;
2241 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2243 for (i = 0; i < pwqp1->n; ++i) {
2244 for (j = 0; j < pwqp2->n; ++j) {
2245 struct isl_set *common;
2246 struct isl_qpolynomial *prod;
2247 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2248 isl_set_copy(pwqp2->p[j].set));
2249 if (isl_set_fast_is_empty(common)) {
2250 isl_set_free(common);
2254 prod = isl_qpolynomial_mul(
2255 isl_qpolynomial_copy(pwqp1->p[i].qp),
2256 isl_qpolynomial_copy(pwqp2->p[j].qp));
2258 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2262 isl_pw_qpolynomial_free(pwqp1);
2263 isl_pw_qpolynomial_free(pwqp2);
2267 isl_pw_qpolynomial_free(pwqp1);
2268 isl_pw_qpolynomial_free(pwqp2);
2272 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2273 __isl_take isl_pw_qpolynomial *pwqp)
2280 if (isl_pw_qpolynomial_is_zero(pwqp))
2283 pwqp = isl_pw_qpolynomial_cow(pwqp);
2287 for (i = 0; i < pwqp->n; ++i) {
2288 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2295 isl_pw_qpolynomial_free(pwqp);
2299 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2300 __isl_take isl_pw_qpolynomial *pwqp1,
2301 __isl_take isl_pw_qpolynomial *pwqp2)
2303 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2306 __isl_give struct isl_upoly *isl_upoly_eval(
2307 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2310 struct isl_upoly_rec *rec;
2311 struct isl_upoly *res;
2312 struct isl_upoly *base;
2314 if (isl_upoly_is_cst(up)) {
2319 rec = isl_upoly_as_rec(up);
2323 isl_assert(up->ctx, rec->n >= 1, goto error);
2325 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2327 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2330 for (i = rec->n - 2; i >= 0; --i) {
2331 res = isl_upoly_mul(res, isl_upoly_copy(base));
2332 res = isl_upoly_sum(res,
2333 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2334 isl_vec_copy(vec)));
2337 isl_upoly_free(base);
2347 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2348 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2351 struct isl_upoly *up;
2356 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2358 if (qp->div->n_row == 0)
2359 ext = isl_vec_copy(pnt->vec);
2362 unsigned dim = isl_dim_total(qp->dim);
2363 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2367 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2368 for (i = 0; i < qp->div->n_row; ++i) {
2369 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2370 1 + dim + i, &ext->el[1+dim+i]);
2371 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2372 qp->div->row[i][0]);
2376 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2380 dim = isl_dim_copy(qp->dim);
2381 isl_qpolynomial_free(qp);
2382 isl_point_free(pnt);
2384 return isl_qpolynomial_alloc(dim, 0, up);
2386 isl_qpolynomial_free(qp);
2387 isl_point_free(pnt);
2391 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2392 __isl_keep struct isl_upoly_cst *cst2)
2397 isl_int_mul(t, cst1->n, cst2->d);
2398 isl_int_submul(t, cst2->n, cst1->d);
2399 cmp = isl_int_sgn(t);
2404 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2405 __isl_keep isl_qpolynomial *qp2)
2407 struct isl_upoly_cst *cst1, *cst2;
2411 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2412 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2413 if (isl_qpolynomial_is_nan(qp1))
2415 if (isl_qpolynomial_is_nan(qp2))
2417 cst1 = isl_upoly_as_cst(qp1->upoly);
2418 cst2 = isl_upoly_as_cst(qp2->upoly);
2420 return isl_upoly_cmp(cst1, cst2) <= 0;
2423 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2424 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2426 struct isl_upoly_cst *cst1, *cst2;
2431 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2432 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2433 cst1 = isl_upoly_as_cst(qp1->upoly);
2434 cst2 = isl_upoly_as_cst(qp2->upoly);
2435 cmp = isl_upoly_cmp(cst1, cst2);
2438 isl_qpolynomial_free(qp2);
2440 isl_qpolynomial_free(qp1);
2445 isl_qpolynomial_free(qp1);
2446 isl_qpolynomial_free(qp2);
2450 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2451 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2453 struct isl_upoly_cst *cst1, *cst2;
2458 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2459 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2460 cst1 = isl_upoly_as_cst(qp1->upoly);
2461 cst2 = isl_upoly_as_cst(qp2->upoly);
2462 cmp = isl_upoly_cmp(cst1, cst2);
2465 isl_qpolynomial_free(qp2);
2467 isl_qpolynomial_free(qp1);
2472 isl_qpolynomial_free(qp1);
2473 isl_qpolynomial_free(qp2);
2477 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2478 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2479 unsigned first, unsigned n)
2488 qp = isl_qpolynomial_cow(qp);
2492 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2495 g_pos = pos(qp->dim, type) + first;
2497 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2501 total = qp->div->n_col - 2;
2502 if (total > g_pos) {
2504 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2507 for (i = 0; i < total - g_pos; ++i)
2509 qp->upoly = expand(qp->upoly, exp, g_pos);
2515 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2521 isl_qpolynomial_free(qp);
2525 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2526 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2530 pos = isl_qpolynomial_dim(qp, type);
2532 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2535 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2536 __isl_take isl_pw_qpolynomial *pwqp,
2537 enum isl_dim_type type, unsigned n)
2541 pos = isl_pw_qpolynomial_dim(pwqp, type);
2543 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2546 static int *reordering_move(isl_ctx *ctx,
2547 unsigned len, unsigned dst, unsigned src, unsigned n)
2552 reordering = isl_alloc_array(ctx, int, len);
2557 for (i = 0; i < dst; ++i)
2559 for (i = 0; i < n; ++i)
2560 reordering[src + i] = dst + i;
2561 for (i = 0; i < src - dst; ++i)
2562 reordering[dst + i] = dst + n + i;
2563 for (i = 0; i < len - src - n; ++i)
2564 reordering[src + n + i] = src + n + i;
2566 for (i = 0; i < src; ++i)
2568 for (i = 0; i < n; ++i)
2569 reordering[src + i] = dst + i;
2570 for (i = 0; i < dst - src; ++i)
2571 reordering[src + n + i] = src + i;
2572 for (i = 0; i < len - dst - n; ++i)
2573 reordering[dst + n + i] = dst + n + i;
2579 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2580 __isl_take isl_qpolynomial *qp,
2581 enum isl_dim_type dst_type, unsigned dst_pos,
2582 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2588 qp = isl_qpolynomial_cow(qp);
2592 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2595 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2596 g_src_pos = pos(qp->dim, src_type) + src_pos;
2597 if (dst_type > src_type)
2600 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2607 reordering = reordering_move(qp->dim->ctx,
2608 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2612 qp->upoly = reorder(qp->upoly, reordering);
2617 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2623 isl_qpolynomial_free(qp);
2627 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2628 isl_int *f, isl_int denom)
2630 struct isl_upoly *up;
2635 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2637 return isl_qpolynomial_alloc(dim, 0, up);
2640 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2641 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2645 struct isl_upoly *up;
2646 isl_qpolynomial *qp;
2652 isl_int_init(denom);
2654 isl_constraint_get_coefficient(c, type, pos, &denom);
2655 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2656 sgn = isl_int_sgn(denom);
2657 isl_int_abs(denom, denom);
2658 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2659 1 + isl_constraint_dim(c, isl_dim_all));
2661 isl_int_neg(denom, denom);
2662 isl_constraint_set_coefficient(c, type, pos, denom);
2664 dim = isl_dim_copy(c->bmap->dim);
2666 isl_int_clear(denom);
2667 isl_constraint_free(c);
2669 qp = isl_qpolynomial_alloc(dim, 0, up);
2671 qp = isl_qpolynomial_neg(qp);
2675 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2676 * in "qp" by subs[i].
2678 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2679 __isl_take isl_qpolynomial *qp,
2680 enum isl_dim_type type, unsigned first, unsigned n,
2681 __isl_keep isl_qpolynomial **subs)
2684 struct isl_upoly **ups;
2689 qp = isl_qpolynomial_cow(qp);
2692 for (i = 0; i < n; ++i)
2696 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2699 for (i = 0; i < n; ++i)
2700 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2703 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2704 for (i = 0; i < n; ++i)
2705 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2707 first += pos(qp->dim, type);
2709 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2712 for (i = 0; i < n; ++i)
2713 ups[i] = subs[i]->upoly;
2715 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2724 isl_qpolynomial_free(qp);
2728 /* Extend "bset" with extra set dimensions for each integer division
2729 * in "qp" and then call "fn" with the extended bset and the polynomial
2730 * that results from replacing each of the integer divisions by the
2731 * corresponding extra set dimension.
2733 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
2734 __isl_keep isl_basic_set *bset,
2735 int (*fn)(__isl_take isl_basic_set *bset,
2736 __isl_take isl_qpolynomial *poly, void *user), void *user)
2740 isl_qpolynomial *poly;
2744 if (qp->div->n_row == 0)
2745 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
2748 div = isl_mat_copy(qp->div);
2749 dim = isl_dim_copy(qp->dim);
2750 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
2751 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
2752 bset = isl_basic_set_copy(bset);
2753 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
2754 bset = add_div_constraints(bset, div);
2756 return fn(bset, poly, user);
2761 /* Return total degree in variables first (inclusive) up to last (exclusive).
2763 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
2767 struct isl_upoly_rec *rec;
2771 if (isl_upoly_is_zero(up))
2773 if (isl_upoly_is_cst(up) || up->var < first)
2776 rec = isl_upoly_as_rec(up);
2780 for (i = 0; i < rec->n; ++i) {
2783 if (isl_upoly_is_zero(rec->p[i]))
2785 d = isl_upoly_degree(rec->p[i], first, last);
2795 /* Return total degree in set variables.
2797 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
2805 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2806 nvar = isl_dim_size(poly->dim, isl_dim_set);
2807 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
2810 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
2811 unsigned pos, int deg)
2814 struct isl_upoly_rec *rec;
2819 if (isl_upoly_is_cst(up) || up->var < pos) {
2821 return isl_upoly_copy(up);
2823 return isl_upoly_zero(up->ctx);
2826 rec = isl_upoly_as_rec(up);
2830 if (up->var == pos) {
2832 return isl_upoly_copy(rec->p[deg]);
2834 return isl_upoly_zero(up->ctx);
2837 up = isl_upoly_copy(up);
2838 up = isl_upoly_cow(up);
2839 rec = isl_upoly_as_rec(up);
2843 for (i = 0; i < rec->n; ++i) {
2844 struct isl_upoly *t;
2845 t = isl_upoly_coeff(rec->p[i], pos, deg);
2848 isl_upoly_free(rec->p[i]);
2858 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2860 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
2861 __isl_keep isl_qpolynomial *qp,
2862 enum isl_dim_type type, unsigned t_pos, int deg)
2865 struct isl_upoly *up;
2871 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
2874 g_pos = pos(qp->dim, type) + t_pos;
2875 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
2877 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
2880 isl_mat_free(c->div);
2881 c->div = isl_mat_copy(qp->div);
2886 isl_qpolynomial_free(c);
2890 /* Homogenize the polynomial in the variables first (inclusive) up to
2891 * last (exclusive) by inserting powers of variable first.
2892 * Variable first is assumed not to appear in the input.
2894 __isl_give struct isl_upoly *isl_upoly_homogenize(
2895 __isl_take struct isl_upoly *up, int deg, int target,
2896 int first, int last)
2899 struct isl_upoly_rec *rec;
2903 if (isl_upoly_is_zero(up))
2907 if (isl_upoly_is_cst(up) || up->var < first) {
2908 struct isl_upoly *hom;
2910 hom = isl_upoly_pow(up->ctx, first, target - deg);
2913 rec = isl_upoly_as_rec(hom);
2914 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
2919 up = isl_upoly_cow(up);
2920 rec = isl_upoly_as_rec(up);
2924 for (i = 0; i < rec->n; ++i) {
2925 if (isl_upoly_is_zero(rec->p[i]))
2927 rec->p[i] = isl_upoly_homogenize(rec->p[i],
2928 up->var < last ? deg + i : i, target,
2940 /* Homogenize the polynomial in the set variables by introducing
2941 * powers of an extra set variable at position 0.
2943 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
2944 __isl_take isl_qpolynomial *poly)
2948 int deg = isl_qpolynomial_degree(poly);
2953 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
2954 poly = isl_qpolynomial_cow(poly);
2958 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2959 nvar = isl_dim_size(poly->dim, isl_dim_set);
2960 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
2967 isl_qpolynomial_free(poly);
2971 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
2972 __isl_take isl_mat *div)
2980 n = isl_dim_total(dim) + div->n_row;
2982 term = isl_calloc(dim->ctx, struct isl_term,
2983 sizeof(struct isl_term) + (n - 1) * sizeof(int));
2990 isl_int_init(term->n);
2991 isl_int_init(term->d);
3000 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3009 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3018 total = isl_dim_total(term->dim) + term->div->n_row;
3020 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3024 isl_int_set(dup->n, term->n);
3025 isl_int_set(dup->d, term->d);
3027 for (i = 0; i < total; ++i)
3028 dup->pow[i] = term->pow[i];
3033 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3041 return isl_term_dup(term);
3044 void isl_term_free(__isl_take isl_term *term)
3049 if (--term->ref > 0)
3052 isl_dim_free(term->dim);
3053 isl_mat_free(term->div);
3054 isl_int_clear(term->n);
3055 isl_int_clear(term->d);
3059 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3067 case isl_dim_out: return isl_dim_size(term->dim, type);
3068 case isl_dim_div: return term->div->n_row;
3069 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3074 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3076 return term ? term->dim->ctx : NULL;
3079 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3083 isl_int_set(*n, term->n);
3086 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3090 isl_int_set(*d, term->d);
3093 int isl_term_get_exp(__isl_keep isl_term *term,
3094 enum isl_dim_type type, unsigned pos)
3099 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3101 if (type >= isl_dim_set)
3102 pos += isl_dim_size(term->dim, isl_dim_param);
3103 if (type >= isl_dim_div)
3104 pos += isl_dim_size(term->dim, isl_dim_set);
3106 return term->pow[pos];
3109 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3111 isl_basic_map *bmap;
3118 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3121 total = term->div->n_col - term->div->n_row - 2;
3122 /* No nested divs for now */
3123 isl_assert(term->dim->ctx,
3124 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3125 term->div->n_row) == -1,
3128 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3129 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3132 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3134 return isl_basic_map_div(bmap, k);
3136 isl_basic_map_free(bmap);
3140 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3141 int (*fn)(__isl_take isl_term *term, void *user),
3142 __isl_take isl_term *term, void *user)
3145 struct isl_upoly_rec *rec;
3150 if (isl_upoly_is_zero(up))
3153 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3154 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3155 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3157 if (isl_upoly_is_cst(up)) {
3158 struct isl_upoly_cst *cst;
3159 cst = isl_upoly_as_cst(up);
3162 term = isl_term_cow(term);
3165 isl_int_set(term->n, cst->n);
3166 isl_int_set(term->d, cst->d);
3167 if (fn(isl_term_copy(term), user) < 0)
3172 rec = isl_upoly_as_rec(up);
3176 for (i = 0; i < rec->n; ++i) {
3177 term = isl_term_cow(term);
3180 term->pow[up->var] = i;
3181 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3185 term->pow[up->var] = 0;
3189 isl_term_free(term);
3193 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3194 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3201 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3205 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3207 isl_term_free(term);
3209 return term ? 0 : -1;
3212 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3214 struct isl_upoly *up;
3215 isl_qpolynomial *qp;
3221 n = isl_dim_total(term->dim) + term->div->n_row;
3223 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3224 for (i = 0; i < n; ++i) {
3227 up = isl_upoly_mul(up,
3228 isl_upoly_pow(term->dim->ctx, i, term->pow[i]));
3231 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3234 isl_mat_free(qp->div);
3235 qp->div = isl_mat_copy(term->div);
3239 isl_term_free(term);
3242 isl_qpolynomial_free(qp);
3243 isl_term_free(term);
3247 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3248 __isl_take isl_dim *dim)
3257 if (isl_dim_equal(qp->dim, dim)) {
3262 qp = isl_qpolynomial_cow(qp);
3266 extra = isl_dim_size(dim, isl_dim_set) -
3267 isl_dim_size(qp->dim, isl_dim_set);
3268 total = isl_dim_total(qp->dim);
3269 if (qp->div->n_row) {
3272 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3275 for (i = 0; i < qp->div->n_row; ++i)
3277 qp->upoly = expand(qp->upoly, exp, total);
3282 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3285 for (i = 0; i < qp->div->n_row; ++i)
3286 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3288 isl_dim_free(qp->dim);
3294 isl_qpolynomial_free(qp);
3298 /* For each parameter or variable that does not appear in qp,
3299 * first eliminate the variable from all constraints and then set it to zero.
3301 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3302 __isl_keep isl_qpolynomial *qp)
3313 d = isl_dim_total(set->dim);
3314 active = isl_calloc_array(set->ctx, int, d);
3315 if (set_active(qp, active) < 0)
3318 for (i = 0; i < d; ++i)
3327 nparam = isl_dim_size(set->dim, isl_dim_param);
3328 nvar = isl_dim_size(set->dim, isl_dim_set);
3329 for (i = 0; i < nparam; ++i) {
3332 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3333 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3335 for (i = 0; i < nvar; ++i) {
3336 if (active[nparam + i])
3338 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3339 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3351 struct isl_opt_data {
3352 isl_qpolynomial *qp;
3354 isl_qpolynomial *opt;
3358 static int opt_fn(__isl_take isl_point *pnt, void *user)
3360 struct isl_opt_data *data = (struct isl_opt_data *)user;
3361 isl_qpolynomial *val;
3363 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3367 } else if (data->max) {
3368 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3370 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3376 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3377 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3379 struct isl_opt_data data = { NULL, 1, NULL, max };
3384 if (isl_upoly_is_cst(qp->upoly)) {
3389 set = fix_inactive(set, qp);
3392 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3396 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3399 isl_qpolynomial_free(qp);
3403 isl_qpolynomial_free(qp);
3404 isl_qpolynomial_free(data.opt);
3408 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3409 __isl_take isl_morph *morph)
3414 struct isl_upoly *up;
3416 struct isl_upoly **subs;
3419 qp = isl_qpolynomial_cow(qp);
3424 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3426 n_sub = morph->inv->n_row - 1;
3427 if (morph->inv->n_row != morph->inv->n_col)
3428 n_sub += qp->div->n_row;
3429 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3433 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3434 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3435 morph->inv->row[0][0], morph->inv->n_col);
3436 if (morph->inv->n_row != morph->inv->n_col)
3437 for (i = 0; i < qp->div->n_row; ++i)
3438 subs[morph->inv->n_row - 1 + i] =
3439 isl_upoly_pow(ctx, morph->inv->n_col - 1 + i, 1);
3441 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3443 for (i = 0; i < n_sub; ++i)
3444 isl_upoly_free(subs[i]);
3447 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3448 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3449 qp->div = isl_mat_product(qp->div, mat);
3450 isl_dim_free(qp->dim);
3451 qp->dim = isl_dim_copy(morph->ran->dim);
3453 if (!qp->upoly || !qp->div || !qp->dim)
3456 isl_morph_free(morph);
3460 isl_qpolynomial_free(qp);
3461 isl_morph_free(morph);
3465 static int neg_entry(void **entry, void *user)
3467 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3469 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3471 return *pwqp ? 0 : -1;
3474 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3475 __isl_take isl_union_pw_qpolynomial *upwqp)
3477 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3481 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3482 &neg_entry, NULL) < 0)
3487 isl_union_pw_qpolynomial_free(upwqp);
3491 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3492 __isl_take isl_union_pw_qpolynomial *upwqp1,
3493 __isl_take isl_union_pw_qpolynomial *upwqp2)
3495 return isl_union_pw_qpolynomial_add(upwqp1,
3496 isl_union_pw_qpolynomial_neg(upwqp2));
3499 static int mul_entry(void **entry, void *user)
3501 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3503 struct isl_hash_table_entry *entry2;
3504 isl_pw_qpolynomial *pwpq = *entry;
3507 hash = isl_dim_get_hash(pwpq->dim);
3508 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3509 hash, &has_dim, pwpq->dim, 0);
3513 pwpq = isl_pw_qpolynomial_copy(pwpq);
3514 pwpq = isl_pw_qpolynomial_mul(pwpq,
3515 isl_pw_qpolynomial_copy(entry2->data));
3517 empty = isl_pw_qpolynomial_is_zero(pwpq);
3519 isl_pw_qpolynomial_free(pwpq);
3523 isl_pw_qpolynomial_free(pwpq);
3527 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3532 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3533 __isl_take isl_union_pw_qpolynomial *upwqp1,
3534 __isl_take isl_union_pw_qpolynomial *upwqp2)
3536 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3539 /* Reorder the columns of the given div definitions according to the
3542 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3543 __isl_take isl_reordering *r)
3552 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3553 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3557 for (i = 0; i < div->n_row; ++i) {
3558 isl_seq_cpy(mat->row[i], div->row[i], 2);
3559 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3560 for (j = 0; j < r->len; ++j)
3561 isl_int_set(mat->row[i][2 + r->pos[j]],
3562 div->row[i][2 + j]);
3565 isl_reordering_free(r);
3569 isl_reordering_free(r);
3574 /* Reorder the dimension of "qp" according to the given reordering.
3576 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3577 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3579 qp = isl_qpolynomial_cow(qp);
3583 r = isl_reordering_extend(r, qp->div->n_row);
3587 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3591 qp->upoly = reorder(qp->upoly, r->pos);
3595 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3597 isl_reordering_free(r);
3600 isl_qpolynomial_free(qp);
3601 isl_reordering_free(r);
3605 struct isl_split_periods_data {
3607 isl_pw_qpolynomial *res;
3610 /* Create a slice where the integer division "div" has the fixed value "v".
3611 * In particular, if "div" refers to floor(f/m), then create a slice
3613 * m v <= f <= m v + (m - 1)
3618 * -f + m v + (m - 1) >= 0
3620 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3621 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3624 isl_basic_set *bset = NULL;
3630 total = isl_dim_total(dim);
3631 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3633 k = isl_basic_set_alloc_inequality(bset);
3636 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3637 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3639 k = isl_basic_set_alloc_inequality(bset);
3642 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3643 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3644 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3645 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3648 return isl_set_from_basic_set(bset);
3650 isl_basic_set_free(bset);
3655 static int split_periods(__isl_take isl_set *set,
3656 __isl_take isl_qpolynomial *qp, void *user);
3658 /* Create a slice of the domain "set" such that integer division "div"
3659 * has the fixed value "v" and add the results to data->res,
3660 * replacing the integer division by "v" in "qp".
3662 static int set_div(__isl_take isl_set *set,
3663 __isl_take isl_qpolynomial *qp, int div, isl_int v,
3664 struct isl_split_periods_data *data)
3669 struct isl_upoly *cst;
3671 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3672 set = isl_set_intersect(set, slice);
3677 total = isl_dim_total(qp->dim);
3679 for (i = div + 1; i < qp->div->n_row; ++i) {
3680 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
3682 isl_int_addmul(qp->div->row[i][1],
3683 qp->div->row[i][2 + total + div], v);
3684 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
3687 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3688 qp = substitute_div(qp, div, cst);
3690 return split_periods(set, qp, data);
3693 isl_qpolynomial_free(qp);
3697 /* Split the domain "set" such that integer division "div"
3698 * has a fixed value (ranging from "min" to "max") on each slice
3699 * and add the results to data->res.
3701 static int split_div(__isl_take isl_set *set,
3702 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3703 struct isl_split_periods_data *data)
3705 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3706 isl_set *set_i = isl_set_copy(set);
3707 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3709 if (set_div(set_i, qp_i, div, min, data) < 0)
3713 isl_qpolynomial_free(qp);
3717 isl_qpolynomial_free(qp);
3721 /* If "qp" refers to any integer division
3722 * that can only attain "max_periods" distinct values on "set"
3723 * then split the domain along those distinct values.
3724 * Add the results (or the original if no splitting occurs)
3727 static int split_periods(__isl_take isl_set *set,
3728 __isl_take isl_qpolynomial *qp, void *user)
3731 isl_pw_qpolynomial *pwqp;
3732 struct isl_split_periods_data *data;
3737 data = (struct isl_split_periods_data *)user;
3742 if (qp->div->n_row == 0) {
3743 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3744 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3750 total = isl_dim_total(qp->dim);
3751 for (i = 0; i < qp->div->n_row; ++i) {
3752 enum isl_lp_result lp_res;
3754 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
3755 qp->div->n_row) != -1)
3758 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
3759 set->ctx->one, &min, NULL, NULL);
3760 if (lp_res == isl_lp_error)
3762 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3764 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
3766 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
3767 set->ctx->one, &max, NULL, NULL);
3768 if (lp_res == isl_lp_error)
3770 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3772 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
3774 isl_int_sub(max, max, min);
3775 if (isl_int_cmp_si(max, data->max_periods) < 0) {
3776 isl_int_add(max, max, min);
3781 if (i < qp->div->n_row) {
3782 r = split_div(set, qp, i, min, max, data);
3784 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3785 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3797 isl_qpolynomial_free(qp);
3801 /* If any quasi-polynomial in pwqp refers to any integer division
3802 * that can only attain "max_periods" distinct values on its domain
3803 * then split the domain along those distinct values.
3805 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
3806 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
3808 struct isl_split_periods_data data;
3810 data.max_periods = max_periods;
3811 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
3813 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
3816 isl_pw_qpolynomial_free(pwqp);
3820 isl_pw_qpolynomial_free(data.res);
3821 isl_pw_qpolynomial_free(pwqp);
3825 /* Construct a piecewise quasipolynomial that is constant on the given
3826 * domain. In particular, it is
3829 * infinity if cst == -1
3831 static __isl_give isl_pw_qpolynomial *constant_on_domain(
3832 __isl_take isl_basic_set *bset, int cst)
3835 isl_qpolynomial *qp;
3840 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
3841 dim = isl_basic_set_get_dim(bset);
3843 qp = isl_qpolynomial_infty(dim);
3845 qp = isl_qpolynomial_zero(dim);
3847 qp = isl_qpolynomial_one(dim);
3848 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
3851 /* Factor bset, call fn on each of the factors and return the product.
3853 * If no factors can be found, simply call fn on the input.
3854 * Otherwise, construct the factors based on the factorizer,
3855 * call fn on each factor and compute the product.
3857 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
3858 __isl_take isl_basic_set *bset,
3859 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
3865 isl_qpolynomial *qp;
3866 isl_pw_qpolynomial *pwqp;
3870 f = isl_basic_set_factorizer(bset);
3873 if (f->n_group == 0) {
3874 isl_factorizer_free(f);
3878 nparam = isl_basic_set_dim(bset, isl_dim_param);
3879 nvar = isl_basic_set_dim(bset, isl_dim_set);
3881 dim = isl_basic_set_get_dim(bset);
3882 dim = isl_dim_domain(dim);
3883 set = isl_set_universe(isl_dim_copy(dim));
3884 qp = isl_qpolynomial_one(dim);
3885 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3887 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
3889 for (i = 0, n = 0; i < f->n_group; ++i) {
3890 isl_basic_set *bset_i;
3891 isl_pw_qpolynomial *pwqp_i;
3893 bset_i = isl_basic_set_copy(bset);
3894 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
3895 nparam + n + f->len[i], nvar - n - f->len[i]);
3896 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
3898 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
3899 n + f->len[i], nvar - n - f->len[i]);
3900 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
3902 pwqp_i = fn(bset_i);
3903 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
3908 isl_basic_set_free(bset);
3909 isl_factorizer_free(f);
3913 isl_basic_set_free(bset);
3917 /* Factor bset, call fn on each of the factors and return the product.
3918 * The function is assumed to evaluate to zero on empty domains,
3919 * to one on zero-dimensional domains and to infinity on unbounded domains
3920 * and will not be called explicitly on zero-dimensional or unbounded domains.
3922 * We first check for some special cases and remove all equalities.
3923 * Then we hand over control to compressed_multiplicative_call.
3925 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
3926 __isl_take isl_basic_set *bset,
3927 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
3931 isl_pw_qpolynomial *pwqp;
3932 unsigned orig_nvar, final_nvar;
3937 if (isl_basic_set_fast_is_empty(bset))
3938 return constant_on_domain(bset, 0);
3940 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
3943 return constant_on_domain(bset, 1);
3945 bounded = isl_basic_set_is_bounded(bset);
3949 return constant_on_domain(bset, -1);
3951 if (bset->n_eq == 0)
3952 return compressed_multiplicative_call(bset, fn);
3954 morph = isl_basic_set_full_compression(bset);
3955 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
3957 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
3959 pwqp = compressed_multiplicative_call(bset, fn);
3961 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
3962 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
3963 morph = isl_morph_inverse(morph);
3965 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
3969 isl_basic_set_free(bset);