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 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
1647 struct isl_qpolynomial *qp = NULL;
1648 struct isl_upoly_rec *rec;
1649 struct isl_upoly_cst *cst;
1655 isl_assert(div->ctx, div->bmap->n_div == 1, goto error);
1657 pos = isl_dim_total(div->bmap->dim);
1658 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
1659 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap), 1,
1664 isl_seq_cpy(qp->div->row[0], div->line[0], qp->div->n_col - 1);
1665 isl_int_set_si(qp->div->row[0][qp->div->n_col - 1], 0);
1667 for (i = 0; i < 1 + power; ++i) {
1668 rec->p[i] = isl_upoly_zero(div->ctx);
1673 cst = isl_upoly_as_cst(rec->p[power]);
1674 isl_int_set_si(cst->n, 1);
1680 isl_qpolynomial_free(qp);
1685 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
1687 return isl_qpolynomial_div_pow(div, 1);
1690 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
1691 const isl_int n, const isl_int d)
1693 struct isl_qpolynomial *qp;
1694 struct isl_upoly_cst *cst;
1696 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1700 cst = isl_upoly_as_cst(qp->upoly);
1701 isl_int_set(cst->n, n);
1702 isl_int_set(cst->d, d);
1707 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
1709 struct isl_upoly_rec *rec;
1715 if (isl_upoly_is_cst(up))
1719 active[up->var] = 1;
1721 rec = isl_upoly_as_rec(up);
1722 for (i = 0; i < rec->n; ++i)
1723 if (up_set_active(rec->p[i], active, d) < 0)
1729 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
1732 int d = isl_dim_total(qp->dim);
1737 for (i = 0; i < d; ++i)
1738 for (j = 0; j < qp->div->n_row; ++j) {
1739 if (isl_int_is_zero(qp->div->row[j][2 + i]))
1745 return up_set_active(qp->upoly, active, d);
1748 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
1749 enum isl_dim_type type, unsigned first, unsigned n)
1760 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1762 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1763 type == isl_dim_set, return -1);
1765 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
1766 if (set_active(qp, active) < 0)
1769 if (type == isl_dim_set)
1770 first += isl_dim_size(qp->dim, isl_dim_param);
1771 for (i = 0; i < n; ++i)
1772 if (active[first + i]) {
1785 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
1786 unsigned first, unsigned n)
1789 struct isl_upoly_rec *rec;
1793 if (n == 0 || up->var < 0 || up->var < first)
1795 if (up->var < first + n) {
1796 up = replace_by_constant_term(up);
1797 return isl_upoly_drop(up, first, n);
1799 up = isl_upoly_cow(up);
1803 rec = isl_upoly_as_rec(up);
1807 for (i = 0; i < rec->n; ++i) {
1808 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
1819 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
1820 __isl_take isl_qpolynomial *qp,
1821 enum isl_dim_type type, unsigned pos, const char *s)
1823 qp = isl_qpolynomial_cow(qp);
1826 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
1831 isl_qpolynomial_free(qp);
1835 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
1836 __isl_take isl_qpolynomial *qp,
1837 enum isl_dim_type type, unsigned first, unsigned n)
1841 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
1844 qp = isl_qpolynomial_cow(qp);
1848 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1850 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1851 type == isl_dim_set, goto error);
1853 qp->dim = isl_dim_drop(qp->dim, type, first, n);
1857 if (type == isl_dim_set)
1858 first += isl_dim_size(qp->dim, isl_dim_param);
1860 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
1864 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
1870 isl_qpolynomial_free(qp);
1874 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1875 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1878 struct isl_upoly_rec *rec;
1879 struct isl_upoly *base, *res;
1884 if (isl_upoly_is_cst(up))
1887 if (up->var < first)
1890 rec = isl_upoly_as_rec(up);
1894 isl_assert(up->ctx, rec->n >= 1, goto error);
1896 if (up->var >= first + n)
1897 base = isl_upoly_pow(up->ctx, up->var, 1);
1899 base = isl_upoly_copy(subs[up->var - first]);
1901 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1902 for (i = rec->n - 2; i >= 0; --i) {
1903 struct isl_upoly *t;
1904 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1905 res = isl_upoly_mul(res, isl_upoly_copy(base));
1906 res = isl_upoly_sum(res, t);
1909 isl_upoly_free(base);
1918 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1919 isl_int denom, unsigned len)
1922 struct isl_upoly *up;
1924 isl_assert(ctx, len >= 1, return NULL);
1926 up = isl_upoly_rat_cst(ctx, f[0], denom);
1927 for (i = 0; i < len - 1; ++i) {
1928 struct isl_upoly *t;
1929 struct isl_upoly *c;
1931 if (isl_int_is_zero(f[1 + i]))
1934 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1935 t = isl_upoly_pow(ctx, i, 1);
1936 t = isl_upoly_mul(c, t);
1937 up = isl_upoly_sum(up, t);
1943 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
1944 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
1949 struct isl_upoly *up;
1953 if (eq->n_eq == 0) {
1954 isl_basic_set_free(eq);
1958 qp = isl_qpolynomial_cow(qp);
1961 qp->div = isl_mat_cow(qp->div);
1965 total = 1 + isl_dim_total(eq->dim);
1966 isl_int_init(denom);
1967 for (i = 0; i < eq->n_eq; ++i) {
1968 j = isl_seq_last_non_zero(eq->eq[i], total);
1969 if (j < 0 || j == 0)
1972 for (k = 0; k < qp->div->n_row; ++k) {
1973 if (isl_int_is_zero(qp->div->row[k][1 + j]))
1975 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
1976 &qp->div->row[k][0]);
1977 isl_seq_normalize(qp->div->ctx,
1978 qp->div->row[k], 1 + total);
1981 if (isl_int_is_pos(eq->eq[i][j]))
1982 isl_seq_neg(eq->eq[i], eq->eq[i], total);
1983 isl_int_abs(denom, eq->eq[i][j]);
1984 isl_int_set_si(eq->eq[i][j], 0);
1986 up = isl_upoly_from_affine(qp->dim->ctx,
1987 eq->eq[i], denom, total);
1988 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
1991 isl_int_clear(denom);
1996 isl_basic_set_free(eq);
2002 isl_basic_set_free(eq);
2003 isl_qpolynomial_free(qp);
2008 #define PW isl_pw_qpolynomial
2010 #define EL isl_qpolynomial
2012 #define IS_ZERO is_zero
2016 #include <isl_pw_templ.c>
2019 #define UNION isl_union_pw_qpolynomial
2021 #define PART isl_pw_qpolynomial
2023 #define PARTS pw_qpolynomial
2025 #include <isl_union_templ.c>
2027 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2035 if (!isl_set_fast_is_universe(pwqp->p[0].set))
2038 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2041 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2042 __isl_take isl_pw_qpolynomial *pwqp1,
2043 __isl_take isl_pw_qpolynomial *pwqp2)
2046 struct isl_pw_qpolynomial *res;
2049 if (!pwqp1 || !pwqp2)
2052 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2055 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2056 isl_pw_qpolynomial_free(pwqp2);
2060 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2061 isl_pw_qpolynomial_free(pwqp1);
2065 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2066 isl_pw_qpolynomial_free(pwqp1);
2070 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2071 isl_pw_qpolynomial_free(pwqp2);
2075 n = pwqp1->n * pwqp2->n;
2076 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2078 for (i = 0; i < pwqp1->n; ++i) {
2079 for (j = 0; j < pwqp2->n; ++j) {
2080 struct isl_set *common;
2081 struct isl_qpolynomial *prod;
2082 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2083 isl_set_copy(pwqp2->p[j].set));
2084 if (isl_set_fast_is_empty(common)) {
2085 isl_set_free(common);
2089 prod = isl_qpolynomial_mul(
2090 isl_qpolynomial_copy(pwqp1->p[i].qp),
2091 isl_qpolynomial_copy(pwqp2->p[j].qp));
2093 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2097 isl_pw_qpolynomial_free(pwqp1);
2098 isl_pw_qpolynomial_free(pwqp2);
2102 isl_pw_qpolynomial_free(pwqp1);
2103 isl_pw_qpolynomial_free(pwqp2);
2107 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2108 __isl_take isl_pw_qpolynomial *pwqp)
2115 if (isl_pw_qpolynomial_is_zero(pwqp))
2118 pwqp = isl_pw_qpolynomial_cow(pwqp);
2122 for (i = 0; i < pwqp->n; ++i) {
2123 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2130 isl_pw_qpolynomial_free(pwqp);
2134 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2135 __isl_take isl_pw_qpolynomial *pwqp1,
2136 __isl_take isl_pw_qpolynomial *pwqp2)
2138 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2141 __isl_give struct isl_upoly *isl_upoly_eval(
2142 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2145 struct isl_upoly_rec *rec;
2146 struct isl_upoly *res;
2147 struct isl_upoly *base;
2149 if (isl_upoly_is_cst(up)) {
2154 rec = isl_upoly_as_rec(up);
2158 isl_assert(up->ctx, rec->n >= 1, goto error);
2160 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2162 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2165 for (i = rec->n - 2; i >= 0; --i) {
2166 res = isl_upoly_mul(res, isl_upoly_copy(base));
2167 res = isl_upoly_sum(res,
2168 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2169 isl_vec_copy(vec)));
2172 isl_upoly_free(base);
2182 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2183 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2186 struct isl_upoly *up;
2191 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2193 if (qp->div->n_row == 0)
2194 ext = isl_vec_copy(pnt->vec);
2197 unsigned dim = isl_dim_total(qp->dim);
2198 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2202 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2203 for (i = 0; i < qp->div->n_row; ++i) {
2204 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2205 1 + dim + i, &ext->el[1+dim+i]);
2206 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2207 qp->div->row[i][0]);
2211 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2215 dim = isl_dim_copy(qp->dim);
2216 isl_qpolynomial_free(qp);
2217 isl_point_free(pnt);
2219 return isl_qpolynomial_alloc(dim, 0, up);
2221 isl_qpolynomial_free(qp);
2222 isl_point_free(pnt);
2226 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2227 __isl_keep struct isl_upoly_cst *cst2)
2232 isl_int_mul(t, cst1->n, cst2->d);
2233 isl_int_submul(t, cst2->n, cst1->d);
2234 cmp = isl_int_sgn(t);
2239 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2240 __isl_keep isl_qpolynomial *qp2)
2242 struct isl_upoly_cst *cst1, *cst2;
2246 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2247 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2248 if (isl_qpolynomial_is_nan(qp1))
2250 if (isl_qpolynomial_is_nan(qp2))
2252 cst1 = isl_upoly_as_cst(qp1->upoly);
2253 cst2 = isl_upoly_as_cst(qp2->upoly);
2255 return isl_upoly_cmp(cst1, cst2) <= 0;
2258 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2259 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2261 struct isl_upoly_cst *cst1, *cst2;
2266 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2267 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2268 cst1 = isl_upoly_as_cst(qp1->upoly);
2269 cst2 = isl_upoly_as_cst(qp2->upoly);
2270 cmp = isl_upoly_cmp(cst1, cst2);
2273 isl_qpolynomial_free(qp2);
2275 isl_qpolynomial_free(qp1);
2280 isl_qpolynomial_free(qp1);
2281 isl_qpolynomial_free(qp2);
2285 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2286 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2288 struct isl_upoly_cst *cst1, *cst2;
2293 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2294 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2295 cst1 = isl_upoly_as_cst(qp1->upoly);
2296 cst2 = isl_upoly_as_cst(qp2->upoly);
2297 cmp = isl_upoly_cmp(cst1, cst2);
2300 isl_qpolynomial_free(qp2);
2302 isl_qpolynomial_free(qp1);
2307 isl_qpolynomial_free(qp1);
2308 isl_qpolynomial_free(qp2);
2312 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2313 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2314 unsigned first, unsigned n)
2323 qp = isl_qpolynomial_cow(qp);
2327 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2330 g_pos = pos(qp->dim, type) + first;
2332 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2336 total = qp->div->n_col - 2;
2337 if (total > g_pos) {
2339 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2342 for (i = 0; i < total - g_pos; ++i)
2344 qp->upoly = expand(qp->upoly, exp, g_pos);
2350 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2356 isl_qpolynomial_free(qp);
2360 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2361 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2365 pos = isl_qpolynomial_dim(qp, type);
2367 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2370 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2371 __isl_take isl_pw_qpolynomial *pwqp,
2372 enum isl_dim_type type, unsigned n)
2376 pos = isl_pw_qpolynomial_dim(pwqp, type);
2378 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2381 static int *reordering_move(isl_ctx *ctx,
2382 unsigned len, unsigned dst, unsigned src, unsigned n)
2387 reordering = isl_alloc_array(ctx, int, len);
2392 for (i = 0; i < dst; ++i)
2394 for (i = 0; i < n; ++i)
2395 reordering[src + i] = dst + i;
2396 for (i = 0; i < src - dst; ++i)
2397 reordering[dst + i] = dst + n + i;
2398 for (i = 0; i < len - src - n; ++i)
2399 reordering[src + n + i] = src + n + i;
2401 for (i = 0; i < src; ++i)
2403 for (i = 0; i < n; ++i)
2404 reordering[src + i] = dst + i;
2405 for (i = 0; i < dst - src; ++i)
2406 reordering[src + n + i] = src + i;
2407 for (i = 0; i < len - dst - n; ++i)
2408 reordering[dst + n + i] = dst + n + i;
2414 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2415 __isl_take isl_qpolynomial *qp,
2416 enum isl_dim_type dst_type, unsigned dst_pos,
2417 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2423 qp = isl_qpolynomial_cow(qp);
2427 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2430 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2431 g_src_pos = pos(qp->dim, src_type) + src_pos;
2432 if (dst_type > src_type)
2435 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2442 reordering = reordering_move(qp->dim->ctx,
2443 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2447 qp->upoly = reorder(qp->upoly, reordering);
2452 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2458 isl_qpolynomial_free(qp);
2462 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2463 isl_int *f, isl_int denom)
2465 struct isl_upoly *up;
2470 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2472 return isl_qpolynomial_alloc(dim, 0, up);
2475 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2476 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2480 struct isl_upoly *up;
2481 isl_qpolynomial *qp;
2487 isl_int_init(denom);
2489 isl_constraint_get_coefficient(c, type, pos, &denom);
2490 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2491 sgn = isl_int_sgn(denom);
2492 isl_int_abs(denom, denom);
2493 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2494 1 + isl_constraint_dim(c, isl_dim_all));
2496 isl_int_neg(denom, denom);
2497 isl_constraint_set_coefficient(c, type, pos, denom);
2499 dim = isl_dim_copy(c->bmap->dim);
2501 isl_int_clear(denom);
2502 isl_constraint_free(c);
2504 qp = isl_qpolynomial_alloc(dim, 0, up);
2506 qp = isl_qpolynomial_neg(qp);
2510 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2511 * in "qp" by subs[i].
2513 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2514 __isl_take isl_qpolynomial *qp,
2515 enum isl_dim_type type, unsigned first, unsigned n,
2516 __isl_keep isl_qpolynomial **subs)
2519 struct isl_upoly **ups;
2524 qp = isl_qpolynomial_cow(qp);
2527 for (i = 0; i < n; ++i)
2531 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2534 for (i = 0; i < n; ++i)
2535 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2538 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2539 for (i = 0; i < n; ++i)
2540 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2542 first += pos(qp->dim, type);
2544 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2547 for (i = 0; i < n; ++i)
2548 ups[i] = subs[i]->upoly;
2550 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2559 isl_qpolynomial_free(qp);
2563 __isl_give isl_basic_set *add_div_constraints(__isl_take isl_basic_set *bset,
2564 __isl_take isl_mat *div)
2572 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2575 total = isl_basic_set_total_dim(bset);
2576 for (i = 0; i < div->n_row; ++i)
2577 if (isl_basic_set_add_div_constraints_var(bset,
2578 total - div->n_row + i, div->row[i]) < 0)
2585 isl_basic_set_free(bset);
2589 /* Extend "bset" with extra set dimensions for each integer division
2590 * in "qp" and then call "fn" with the extended bset and the polynomial
2591 * that results from replacing each of the integer divisions by the
2592 * corresponding extra set dimension.
2594 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
2595 __isl_keep isl_basic_set *bset,
2596 int (*fn)(__isl_take isl_basic_set *bset,
2597 __isl_take isl_qpolynomial *poly, void *user), void *user)
2601 isl_qpolynomial *poly;
2605 if (qp->div->n_row == 0)
2606 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
2609 div = isl_mat_copy(qp->div);
2610 dim = isl_dim_copy(qp->dim);
2611 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
2612 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
2613 bset = isl_basic_set_copy(bset);
2614 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
2615 bset = add_div_constraints(bset, div);
2617 return fn(bset, poly, user);
2622 /* Return total degree in variables first (inclusive) up to last (exclusive).
2624 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
2628 struct isl_upoly_rec *rec;
2632 if (isl_upoly_is_zero(up))
2634 if (isl_upoly_is_cst(up) || up->var < first)
2637 rec = isl_upoly_as_rec(up);
2641 for (i = 0; i < rec->n; ++i) {
2644 if (isl_upoly_is_zero(rec->p[i]))
2646 d = isl_upoly_degree(rec->p[i], first, last);
2656 /* Return total degree in set variables.
2658 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
2666 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2667 nvar = isl_dim_size(poly->dim, isl_dim_set);
2668 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
2671 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
2672 unsigned pos, int deg)
2675 struct isl_upoly_rec *rec;
2680 if (isl_upoly_is_cst(up) || up->var < pos) {
2682 return isl_upoly_copy(up);
2684 return isl_upoly_zero(up->ctx);
2687 rec = isl_upoly_as_rec(up);
2691 if (up->var == pos) {
2693 return isl_upoly_copy(rec->p[deg]);
2695 return isl_upoly_zero(up->ctx);
2698 up = isl_upoly_copy(up);
2699 up = isl_upoly_cow(up);
2700 rec = isl_upoly_as_rec(up);
2704 for (i = 0; i < rec->n; ++i) {
2705 struct isl_upoly *t;
2706 t = isl_upoly_coeff(rec->p[i], pos, deg);
2709 isl_upoly_free(rec->p[i]);
2719 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2721 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
2722 __isl_keep isl_qpolynomial *qp,
2723 enum isl_dim_type type, unsigned t_pos, int deg)
2726 struct isl_upoly *up;
2732 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
2735 g_pos = pos(qp->dim, type) + t_pos;
2736 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
2738 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
2741 isl_mat_free(c->div);
2742 c->div = isl_mat_copy(qp->div);
2747 isl_qpolynomial_free(c);
2751 /* Homogenize the polynomial in the variables first (inclusive) up to
2752 * last (exclusive) by inserting powers of variable first.
2753 * Variable first is assumed not to appear in the input.
2755 __isl_give struct isl_upoly *isl_upoly_homogenize(
2756 __isl_take struct isl_upoly *up, int deg, int target,
2757 int first, int last)
2760 struct isl_upoly_rec *rec;
2764 if (isl_upoly_is_zero(up))
2768 if (isl_upoly_is_cst(up) || up->var < first) {
2769 struct isl_upoly *hom;
2771 hom = isl_upoly_pow(up->ctx, first, target - deg);
2774 rec = isl_upoly_as_rec(hom);
2775 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
2780 up = isl_upoly_cow(up);
2781 rec = isl_upoly_as_rec(up);
2785 for (i = 0; i < rec->n; ++i) {
2786 if (isl_upoly_is_zero(rec->p[i]))
2788 rec->p[i] = isl_upoly_homogenize(rec->p[i],
2789 up->var < last ? deg + i : i, target,
2801 /* Homogenize the polynomial in the set variables by introducing
2802 * powers of an extra set variable at position 0.
2804 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
2805 __isl_take isl_qpolynomial *poly)
2809 int deg = isl_qpolynomial_degree(poly);
2814 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
2815 poly = isl_qpolynomial_cow(poly);
2819 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2820 nvar = isl_dim_size(poly->dim, isl_dim_set);
2821 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
2828 isl_qpolynomial_free(poly);
2832 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
2833 __isl_take isl_mat *div)
2841 n = isl_dim_total(dim) + div->n_row;
2843 term = isl_calloc(dim->ctx, struct isl_term,
2844 sizeof(struct isl_term) + (n - 1) * sizeof(int));
2851 isl_int_init(term->n);
2852 isl_int_init(term->d);
2861 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
2870 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
2879 total = isl_dim_total(term->dim) + term->div->n_row;
2881 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
2885 isl_int_set(dup->n, term->n);
2886 isl_int_set(dup->d, term->d);
2888 for (i = 0; i < total; ++i)
2889 dup->pow[i] = term->pow[i];
2894 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
2902 return isl_term_dup(term);
2905 void isl_term_free(__isl_take isl_term *term)
2910 if (--term->ref > 0)
2913 isl_dim_free(term->dim);
2914 isl_mat_free(term->div);
2915 isl_int_clear(term->n);
2916 isl_int_clear(term->d);
2920 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
2928 case isl_dim_out: return isl_dim_size(term->dim, type);
2929 case isl_dim_div: return term->div->n_row;
2930 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
2935 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
2937 return term ? term->dim->ctx : NULL;
2940 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
2944 isl_int_set(*n, term->n);
2947 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
2951 isl_int_set(*d, term->d);
2954 int isl_term_get_exp(__isl_keep isl_term *term,
2955 enum isl_dim_type type, unsigned pos)
2960 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
2962 if (type >= isl_dim_set)
2963 pos += isl_dim_size(term->dim, isl_dim_param);
2964 if (type >= isl_dim_div)
2965 pos += isl_dim_size(term->dim, isl_dim_set);
2967 return term->pow[pos];
2970 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
2972 isl_basic_map *bmap;
2979 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
2982 total = term->div->n_col - term->div->n_row - 2;
2983 /* No nested divs for now */
2984 isl_assert(term->dim->ctx,
2985 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
2986 term->div->n_row) == -1,
2989 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
2990 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
2993 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
2995 return isl_basic_map_div(bmap, k);
2997 isl_basic_map_free(bmap);
3001 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3002 int (*fn)(__isl_take isl_term *term, void *user),
3003 __isl_take isl_term *term, void *user)
3006 struct isl_upoly_rec *rec;
3011 if (isl_upoly_is_zero(up))
3014 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3015 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3016 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3018 if (isl_upoly_is_cst(up)) {
3019 struct isl_upoly_cst *cst;
3020 cst = isl_upoly_as_cst(up);
3023 term = isl_term_cow(term);
3026 isl_int_set(term->n, cst->n);
3027 isl_int_set(term->d, cst->d);
3028 if (fn(isl_term_copy(term), user) < 0)
3033 rec = isl_upoly_as_rec(up);
3037 for (i = 0; i < rec->n; ++i) {
3038 term = isl_term_cow(term);
3041 term->pow[up->var] = i;
3042 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3046 term->pow[up->var] = 0;
3050 isl_term_free(term);
3054 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3055 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3062 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3066 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3068 isl_term_free(term);
3070 return term ? 0 : -1;
3073 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3075 struct isl_upoly *up;
3076 isl_qpolynomial *qp;
3082 n = isl_dim_total(term->dim) + term->div->n_row;
3084 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3085 for (i = 0; i < n; ++i) {
3088 up = isl_upoly_mul(up,
3089 isl_upoly_pow(term->dim->ctx, i, term->pow[i]));
3092 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3095 isl_mat_free(qp->div);
3096 qp->div = isl_mat_copy(term->div);
3100 isl_term_free(term);
3103 isl_qpolynomial_free(qp);
3104 isl_term_free(term);
3108 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3109 __isl_take isl_dim *dim)
3118 if (isl_dim_equal(qp->dim, dim)) {
3123 qp = isl_qpolynomial_cow(qp);
3127 extra = isl_dim_size(dim, isl_dim_set) -
3128 isl_dim_size(qp->dim, isl_dim_set);
3129 total = isl_dim_total(qp->dim);
3130 if (qp->div->n_row) {
3133 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3136 for (i = 0; i < qp->div->n_row; ++i)
3138 qp->upoly = expand(qp->upoly, exp, total);
3143 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3146 for (i = 0; i < qp->div->n_row; ++i)
3147 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3149 isl_dim_free(qp->dim);
3155 isl_qpolynomial_free(qp);
3159 /* For each parameter or variable that does not appear in qp,
3160 * first eliminate the variable from all constraints and then set it to zero.
3162 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3163 __isl_keep isl_qpolynomial *qp)
3174 d = isl_dim_total(set->dim);
3175 active = isl_calloc_array(set->ctx, int, d);
3176 if (set_active(qp, active) < 0)
3179 for (i = 0; i < d; ++i)
3188 nparam = isl_dim_size(set->dim, isl_dim_param);
3189 nvar = isl_dim_size(set->dim, isl_dim_set);
3190 for (i = 0; i < nparam; ++i) {
3193 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3194 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3196 for (i = 0; i < nvar; ++i) {
3197 if (active[nparam + i])
3199 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3200 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3212 struct isl_opt_data {
3213 isl_qpolynomial *qp;
3215 isl_qpolynomial *opt;
3219 static int opt_fn(__isl_take isl_point *pnt, void *user)
3221 struct isl_opt_data *data = (struct isl_opt_data *)user;
3222 isl_qpolynomial *val;
3224 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3228 } else if (data->max) {
3229 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3231 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3237 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3238 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3240 struct isl_opt_data data = { NULL, 1, NULL, max };
3245 if (isl_upoly_is_cst(qp->upoly)) {
3250 set = fix_inactive(set, qp);
3253 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3257 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3260 isl_qpolynomial_free(qp);
3264 isl_qpolynomial_free(qp);
3265 isl_qpolynomial_free(data.opt);
3269 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3270 __isl_take isl_morph *morph)
3274 struct isl_upoly *up;
3276 struct isl_upoly **subs;
3279 qp = isl_qpolynomial_cow(qp);
3284 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3286 subs = isl_calloc_array(ctx, struct isl_upoly *, morph->inv->n_row - 1);
3290 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3291 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3292 morph->inv->row[0][0], morph->inv->n_col);
3294 qp->upoly = isl_upoly_subs(qp->upoly, 0, morph->inv->n_row - 1, subs);
3296 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3297 isl_upoly_free(subs[i]);
3300 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3301 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3302 qp->div = isl_mat_product(qp->div, mat);
3303 isl_dim_free(qp->dim);
3304 qp->dim = isl_dim_copy(morph->ran->dim);
3306 if (!qp->upoly || !qp->div || !qp->dim)
3309 isl_morph_free(morph);
3313 isl_qpolynomial_free(qp);
3314 isl_morph_free(morph);
3318 static int neg_entry(void **entry, void *user)
3320 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3322 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3324 return *pwqp ? 0 : -1;
3327 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3328 __isl_take isl_union_pw_qpolynomial *upwqp)
3330 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3334 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3335 &neg_entry, NULL) < 0)
3340 isl_union_pw_qpolynomial_free(upwqp);
3344 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3345 __isl_take isl_union_pw_qpolynomial *upwqp1,
3346 __isl_take isl_union_pw_qpolynomial *upwqp2)
3348 return isl_union_pw_qpolynomial_add(upwqp1,
3349 isl_union_pw_qpolynomial_neg(upwqp2));
3352 static int mul_entry(void **entry, void *user)
3354 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3356 struct isl_hash_table_entry *entry2;
3357 isl_pw_qpolynomial *pwpq = *entry;
3360 hash = isl_dim_get_hash(pwpq->dim);
3361 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3362 hash, &has_dim, pwpq->dim, 0);
3366 pwpq = isl_pw_qpolynomial_copy(pwpq);
3367 pwpq = isl_pw_qpolynomial_mul(pwpq,
3368 isl_pw_qpolynomial_copy(entry2->data));
3370 empty = isl_pw_qpolynomial_is_zero(pwpq);
3372 isl_pw_qpolynomial_free(pwpq);
3376 isl_pw_qpolynomial_free(pwpq);
3380 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3385 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3386 __isl_take isl_union_pw_qpolynomial *upwqp1,
3387 __isl_take isl_union_pw_qpolynomial *upwqp2)
3389 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3392 /* Reorder the columns of the given div definitions according to the
3395 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3396 __isl_take isl_reordering *r)
3405 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3406 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3410 for (i = 0; i < div->n_row; ++i) {
3411 isl_seq_cpy(mat->row[i], div->row[i], 2);
3412 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3413 for (j = 0; j < r->len; ++j)
3414 isl_int_set(mat->row[i][2 + r->pos[j]],
3415 div->row[i][2 + j]);
3418 isl_reordering_free(r);
3422 isl_reordering_free(r);
3427 /* Reorder the dimension of "qp" according to the given reordering.
3429 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3430 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3432 qp = isl_qpolynomial_cow(qp);
3436 r = isl_reordering_extend(r, qp->div->n_row);
3440 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3444 qp->upoly = reorder(qp->upoly, r->pos);
3448 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3450 isl_reordering_free(r);
3453 isl_qpolynomial_free(qp);
3454 isl_reordering_free(r);
3458 struct isl_split_periods_data {
3460 isl_pw_qpolynomial *res;
3463 /* Create a slice where the integer division "div" has the fixed value "v".
3464 * In particular, if "div" refers to floor(f/m), then create a slice
3466 * m v <= f <= m v + (m - 1)
3471 * -f + m v + (m - 1) >= 0
3473 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3474 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3477 isl_basic_set *bset = NULL;
3483 total = isl_dim_total(dim);
3484 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3486 k = isl_basic_set_alloc_inequality(bset);
3489 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3490 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3492 k = isl_basic_set_alloc_inequality(bset);
3495 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3496 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3497 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3498 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3501 return isl_set_from_basic_set(bset);
3503 isl_basic_set_free(bset);
3508 static int split_periods(__isl_take isl_set *set,
3509 __isl_take isl_qpolynomial *qp, void *user);
3511 /* Create a slice of the domain "set" such that integer division "div"
3512 * has the fixed value "v" and add the results to data->res,
3513 * replacing the integer division by "v" in "qp".
3515 static int set_div(__isl_take isl_set *set,
3516 __isl_take isl_qpolynomial *qp, int div, isl_int v,
3517 struct isl_split_periods_data *data)
3522 struct isl_upoly *cst;
3525 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3526 set = isl_set_intersect(set, slice);
3528 qp = isl_qpolynomial_cow(qp);
3532 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3535 total = isl_dim_total(qp->dim);
3536 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &cst);
3537 isl_upoly_free(cst);
3541 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
3544 for (i = 0; i < total + div; ++i)
3546 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
3547 reordering[i] = i - 1;
3548 qp->div = isl_mat_drop_rows(qp->div, div, 1);
3549 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
3550 qp->upoly = reorder(qp->upoly, reordering);
3553 if (!qp->upoly || !qp->div)
3556 return split_periods(set, qp, data);
3559 isl_qpolynomial_free(qp);
3563 /* Split the domain "set" such that integer division "div"
3564 * has a fixed value (ranging from "min" to "max") on each slice
3565 * and add the results to data->res.
3567 static int split_div(__isl_take isl_set *set,
3568 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3569 struct isl_split_periods_data *data)
3571 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3572 isl_set *set_i = isl_set_copy(set);
3573 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3575 if (set_div(set_i, qp_i, div, min, data) < 0)
3579 isl_qpolynomial_free(qp);
3583 isl_qpolynomial_free(qp);
3587 /* If "qp" refers to any integer division
3588 * that can only attain "max_periods" distinct values on "set"
3589 * then split the domain along those distinct values.
3590 * Add the results (or the original if no splitting occurs)
3593 static int split_periods(__isl_take isl_set *set,
3594 __isl_take isl_qpolynomial *qp, void *user)
3597 isl_pw_qpolynomial *pwqp;
3598 struct isl_split_periods_data *data;
3603 data = (struct isl_split_periods_data *)user;
3608 if (qp->div->n_row == 0) {
3609 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3610 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3616 total = isl_dim_total(qp->dim);
3617 for (i = 0; i < qp->div->n_row; ++i) {
3618 enum isl_lp_result lp_res;
3620 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
3621 qp->div->n_row) != -1)
3624 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
3625 set->ctx->one, &min, NULL, NULL);
3626 if (lp_res == isl_lp_error)
3628 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3630 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
3632 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
3633 set->ctx->one, &max, NULL, NULL);
3634 if (lp_res == isl_lp_error)
3636 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3638 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
3640 isl_int_sub(max, max, min);
3641 if (isl_int_cmp_si(max, data->max_periods) < 0) {
3642 isl_int_add(max, max, min);
3647 if (i < qp->div->n_row) {
3648 r = split_div(set, qp, i, min, max, data);
3650 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3651 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3663 isl_qpolynomial_free(qp);
3667 /* If any quasi-polynomial in pwqp refers to any integer division
3668 * that can only attain "max_periods" distinct values on its domain
3669 * then split the domain along those distinct values.
3671 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
3672 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
3674 struct isl_split_periods_data data;
3676 data.max_periods = max_periods;
3677 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
3679 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
3682 isl_pw_qpolynomial_free(pwqp);
3686 isl_pw_qpolynomial_free(data.res);
3687 isl_pw_qpolynomial_free(pwqp);
3691 /* Construct a piecewise quasipolynomial that is constant on the given
3692 * domain. In particular, it is
3695 * infinity if cst == -1
3697 static __isl_give isl_pw_qpolynomial *constant_on_domain(
3698 __isl_take isl_basic_set *bset, int cst)
3701 isl_qpolynomial *qp;
3706 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
3707 dim = isl_basic_set_get_dim(bset);
3709 qp = isl_qpolynomial_infty(dim);
3711 qp = isl_qpolynomial_zero(dim);
3713 qp = isl_qpolynomial_one(dim);
3714 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
3717 /* Factor bset, call fn on each of the factors and return the product.
3719 * If no factors can be found, simply call fn on the input.
3720 * Otherwise, construct the factors based on the factorizer,
3721 * call fn on each factor and compute the product.
3723 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
3724 __isl_take isl_basic_set *bset,
3725 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
3731 isl_qpolynomial *qp;
3732 isl_pw_qpolynomial *pwqp;
3736 f = isl_basic_set_factorizer(bset);
3739 if (f->n_group == 0) {
3740 isl_factorizer_free(f);
3744 nparam = isl_basic_set_dim(bset, isl_dim_param);
3745 nvar = isl_basic_set_dim(bset, isl_dim_set);
3747 dim = isl_basic_set_get_dim(bset);
3748 dim = isl_dim_domain(dim);
3749 set = isl_set_universe(isl_dim_copy(dim));
3750 qp = isl_qpolynomial_one(dim);
3751 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3753 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
3755 for (i = 0, n = 0; i < f->n_group; ++i) {
3756 isl_basic_set *bset_i;
3757 isl_pw_qpolynomial *pwqp_i;
3759 bset_i = isl_basic_set_copy(bset);
3760 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
3761 nparam + n + f->len[i], nvar - n - f->len[i]);
3762 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
3764 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
3765 n + f->len[i], nvar - n - f->len[i]);
3766 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
3768 pwqp_i = fn(bset_i);
3769 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
3774 isl_basic_set_free(bset);
3775 isl_factorizer_free(f);
3779 isl_basic_set_free(bset);
3783 /* Factor bset, call fn on each of the factors and return the product.
3784 * The function is assumed to evaluate to zero on empty domains,
3785 * to one on zero-dimensional domains and to infinity on unbounded domains
3786 * and will not be called explicitly on zero-dimensional or unbounded domains.
3788 * We first check for some special cases and remove all equalities.
3789 * Then we hand over control to compressed_multiplicative_call.
3791 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
3792 __isl_take isl_basic_set *bset,
3793 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
3797 isl_pw_qpolynomial *pwqp;
3798 unsigned orig_nvar, final_nvar;
3803 if (isl_basic_set_fast_is_empty(bset))
3804 return constant_on_domain(bset, 0);
3806 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
3809 return constant_on_domain(bset, 1);
3811 bounded = isl_basic_set_is_bounded(bset);
3815 return constant_on_domain(bset, -1);
3817 if (bset->n_eq == 0)
3818 return compressed_multiplicative_call(bset, fn);
3820 morph = isl_basic_set_full_compression(bset);
3821 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
3823 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
3825 pwqp = compressed_multiplicative_call(bset, fn);
3827 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
3828 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
3829 morph = isl_morph_inverse(morph);
3831 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
3835 isl_basic_set_free(bset);