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,
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
16 #include <isl_map_private.h>
18 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
21 case isl_dim_param: return 0;
22 case isl_dim_in: return dim->nparam;
23 case isl_dim_out: return dim->nparam + dim->n_in;
27 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
35 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
40 isl_assert(up->ctx, up->var < 0, return NULL);
42 return (struct isl_upoly_cst *)up;
45 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
50 isl_assert(up->ctx, up->var >= 0, return NULL);
52 return (struct isl_upoly_rec *)up;
55 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
56 __isl_keep struct isl_upoly *up2)
59 struct isl_upoly_rec *rec1, *rec2;
65 if (up1->var != up2->var)
67 if (isl_upoly_is_cst(up1)) {
68 struct isl_upoly_cst *cst1, *cst2;
69 cst1 = isl_upoly_as_cst(up1);
70 cst2 = isl_upoly_as_cst(up2);
73 return isl_int_eq(cst1->n, cst2->n) &&
74 isl_int_eq(cst1->d, cst2->d);
77 rec1 = isl_upoly_as_rec(up1);
78 rec2 = isl_upoly_as_rec(up2);
82 if (rec1->n != rec2->n)
85 for (i = 0; i < rec1->n; ++i) {
86 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
94 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
96 struct isl_upoly_cst *cst;
100 if (!isl_upoly_is_cst(up))
103 cst = isl_upoly_as_cst(up);
107 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
110 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
112 struct isl_upoly_cst *cst;
116 if (!isl_upoly_is_cst(up))
119 cst = isl_upoly_as_cst(up);
123 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
126 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
128 struct isl_upoly_cst *cst;
132 if (!isl_upoly_is_cst(up))
135 cst = isl_upoly_as_cst(up);
139 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
142 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
144 struct isl_upoly_cst *cst;
148 if (!isl_upoly_is_cst(up))
151 cst = isl_upoly_as_cst(up);
155 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
158 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
160 struct isl_upoly_cst *cst;
164 if (!isl_upoly_is_cst(up))
167 cst = isl_upoly_as_cst(up);
171 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
174 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
176 struct isl_upoly_cst *cst;
180 if (!isl_upoly_is_cst(up))
183 cst = isl_upoly_as_cst(up);
187 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
190 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
192 struct isl_upoly_cst *cst;
194 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
203 isl_int_init(cst->n);
204 isl_int_init(cst->d);
209 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
211 struct isl_upoly_cst *cst;
213 cst = isl_upoly_cst_alloc(ctx);
217 isl_int_set_si(cst->n, 0);
218 isl_int_set_si(cst->d, 1);
223 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
225 struct isl_upoly_cst *cst;
227 cst = isl_upoly_cst_alloc(ctx);
231 isl_int_set_si(cst->n, 1);
232 isl_int_set_si(cst->d, 0);
237 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
239 struct isl_upoly_cst *cst;
241 cst = isl_upoly_cst_alloc(ctx);
245 isl_int_set_si(cst->n, 0);
246 isl_int_set_si(cst->d, 0);
251 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
252 isl_int n, isl_int d)
254 struct isl_upoly_cst *cst;
256 cst = isl_upoly_cst_alloc(ctx);
260 isl_int_set(cst->n, n);
261 isl_int_set(cst->d, d);
266 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
269 struct isl_upoly_rec *rec;
271 isl_assert(ctx, var >= 0, return NULL);
272 isl_assert(ctx, size >= 0, return NULL);
273 rec = isl_calloc(dim->ctx, struct isl_upoly_rec,
274 sizeof(struct isl_upoly_rec) +
275 (size - 1) * sizeof(struct isl_upoly *));
289 isl_upoly_free(&rec->up);
293 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
295 struct isl_upoly_cst *cst;
300 return isl_upoly_is_zero(qp->upoly);
303 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
305 struct isl_upoly_cst *cst;
310 return isl_upoly_is_one(qp->upoly);
313 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
315 isl_int_clear(cst->n);
316 isl_int_clear(cst->d);
319 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
323 for (i = 0; i < rec->n; ++i)
324 isl_upoly_free(rec->p[i]);
327 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
336 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
338 struct isl_upoly_cst *cst;
339 struct isl_upoly_cst *dup;
341 cst = isl_upoly_as_cst(up);
345 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
348 isl_int_set(dup->n, cst->n);
349 isl_int_set(dup->d, cst->d);
354 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
357 struct isl_upoly_rec *rec;
358 struct isl_upoly_rec *dup;
360 rec = isl_upoly_as_rec(up);
364 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
368 for (i = 0; i < rec->n; ++i) {
369 dup->p[i] = isl_upoly_copy(rec->p[i]);
377 isl_upoly_free(&dup->up);
381 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
383 struct isl_upoly *dup;
388 if (isl_upoly_is_cst(up))
389 return isl_upoly_dup_cst(up);
391 return isl_upoly_dup_rec(up);
394 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
402 return isl_upoly_dup(up);
405 void isl_upoly_free(__isl_take struct isl_upoly *up)
414 upoly_free_cst((struct isl_upoly_cst *)up);
416 upoly_free_rec((struct isl_upoly_rec *)up);
418 isl_ctx_deref(up->ctx);
422 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
427 isl_int_gcd(gcd, cst->n, cst->d);
428 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
429 isl_int_divexact(cst->n, cst->n, gcd);
430 isl_int_divexact(cst->d, cst->d, gcd);
435 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
436 __isl_take struct isl_upoly *up2)
438 struct isl_upoly_cst *cst1;
439 struct isl_upoly_cst *cst2;
441 up1 = isl_upoly_cow(up1);
445 cst1 = isl_upoly_as_cst(up1);
446 cst2 = isl_upoly_as_cst(up2);
448 if (isl_int_eq(cst1->d, cst2->d))
449 isl_int_add(cst1->n, cst1->n, cst2->n);
451 isl_int_mul(cst1->n, cst1->n, cst2->d);
452 isl_int_addmul(cst1->n, cst2->n, cst1->d);
453 isl_int_mul(cst1->d, cst1->d, cst2->d);
456 isl_upoly_cst_reduce(cst1);
466 static __isl_give struct isl_upoly *replace_by_zero(
467 __isl_take struct isl_upoly *up)
475 return isl_upoly_zero(ctx);
478 static __isl_give struct isl_upoly *replace_by_constant_term(
479 __isl_take struct isl_upoly *up)
481 struct isl_upoly_rec *rec;
482 struct isl_upoly *cst;
487 rec = isl_upoly_as_rec(up);
490 cst = isl_upoly_copy(rec->p[0]);
498 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
499 __isl_take struct isl_upoly *up2)
502 struct isl_upoly_rec *rec1, *rec2;
507 if (isl_upoly_is_nan(up1)) {
512 if (isl_upoly_is_nan(up2)) {
517 if (isl_upoly_is_zero(up1)) {
522 if (isl_upoly_is_zero(up2)) {
527 if (up1->var < up2->var)
528 return isl_upoly_sum(up2, up1);
530 if (up2->var < up1->var) {
531 struct isl_upoly_rec *rec;
532 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
536 up1 = isl_upoly_cow(up1);
537 rec = isl_upoly_as_rec(up1);
540 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
542 up1 = replace_by_constant_term(up1);
546 if (isl_upoly_is_cst(up1))
547 return isl_upoly_sum_cst(up1, up2);
549 rec1 = isl_upoly_as_rec(up1);
550 rec2 = isl_upoly_as_rec(up2);
554 if (rec1->n < rec2->n)
555 return isl_upoly_sum(up2, up1);
557 up1 = isl_upoly_cow(up1);
558 rec1 = isl_upoly_as_rec(up1);
562 for (i = rec2->n - 1; i >= 0; --i) {
563 rec1->p[i] = isl_upoly_sum(rec1->p[i],
564 isl_upoly_copy(rec2->p[i]));
567 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
568 isl_upoly_free(rec1->p[i]);
574 up1 = replace_by_zero(up1);
575 else if (rec1->n == 1)
576 up1 = replace_by_constant_term(up1);
587 __isl_give struct isl_upoly *isl_upoly_neg_cst(__isl_take struct isl_upoly *up)
589 struct isl_upoly_cst *cst;
591 if (isl_upoly_is_zero(up))
594 up = isl_upoly_cow(up);
598 cst = isl_upoly_as_cst(up);
600 isl_int_neg(cst->n, cst->n);
605 __isl_give struct isl_upoly *isl_upoly_neg(__isl_take struct isl_upoly *up)
608 struct isl_upoly_rec *rec;
613 if (isl_upoly_is_cst(up))
614 return isl_upoly_neg_cst(up);
616 up = isl_upoly_cow(up);
617 rec = isl_upoly_as_rec(up);
621 for (i = 0; i < rec->n; ++i) {
622 rec->p[i] = isl_upoly_neg(rec->p[i]);
633 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
634 __isl_take struct isl_upoly *up2)
636 struct isl_upoly_cst *cst1;
637 struct isl_upoly_cst *cst2;
639 up1 = isl_upoly_cow(up1);
643 cst1 = isl_upoly_as_cst(up1);
644 cst2 = isl_upoly_as_cst(up2);
646 isl_int_mul(cst1->n, cst1->n, cst2->n);
647 isl_int_mul(cst1->d, cst1->d, cst2->d);
649 isl_upoly_cst_reduce(cst1);
659 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
660 __isl_take struct isl_upoly *up2)
662 struct isl_upoly_rec *rec1;
663 struct isl_upoly_rec *rec2;
664 struct isl_upoly_rec *res;
668 rec1 = isl_upoly_as_rec(up1);
669 rec2 = isl_upoly_as_rec(up2);
672 size = rec1->n + rec2->n - 1;
673 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
677 for (i = 0; i < rec1->n; ++i) {
678 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
679 isl_upoly_copy(rec1->p[i]));
684 for (; i < size; ++i) {
685 res->p[i] = isl_upoly_zero(up1->ctx);
690 for (i = 0; i < rec1->n; ++i) {
691 for (j = 1; j < rec2->n; ++j) {
692 struct isl_upoly *up;
693 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
694 isl_upoly_copy(rec1->p[i]));
695 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
708 isl_upoly_free(&res->up);
712 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
713 __isl_take struct isl_upoly *up2)
718 if (isl_upoly_is_nan(up1)) {
723 if (isl_upoly_is_nan(up2)) {
728 if (isl_upoly_is_zero(up1)) {
733 if (isl_upoly_is_zero(up2)) {
738 if (isl_upoly_is_one(up1)) {
743 if (isl_upoly_is_one(up2)) {
748 if (up1->var < up2->var)
749 return isl_upoly_mul(up2, up1);
751 if (up2->var < up1->var) {
753 struct isl_upoly_rec *rec;
754 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
755 isl_ctx *ctx = up1->ctx;
758 return isl_upoly_nan(ctx);
760 up1 = isl_upoly_cow(up1);
761 rec = isl_upoly_as_rec(up1);
765 for (i = 0; i < rec->n; ++i) {
766 rec->p[i] = isl_upoly_mul(rec->p[i],
767 isl_upoly_copy(up2));
775 if (isl_upoly_is_cst(up1))
776 return isl_upoly_mul_cst(up1, up2);
778 return isl_upoly_mul_rec(up1, up2);
785 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
786 unsigned n_div, __isl_take struct isl_upoly *up)
788 struct isl_qpolynomial *qp = NULL;
794 total = isl_dim_total(dim);
796 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
801 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
812 isl_qpolynomial_free(qp);
816 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
825 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
827 struct isl_qpolynomial *dup;
832 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
833 isl_upoly_copy(qp->upoly));
836 isl_mat_free(dup->div);
837 dup->div = isl_mat_copy(qp->div);
843 isl_qpolynomial_free(dup);
847 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
855 return isl_qpolynomial_dup(qp);
858 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
866 isl_dim_free(qp->dim);
867 isl_mat_free(qp->div);
868 isl_upoly_free(qp->upoly);
873 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
878 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
879 div1->n_col >= div2->n_col, return -1);
881 if (div1->n_row == div2->n_row)
882 return isl_mat_is_equal(div1, div2);
886 div1->n_row = div2->n_row;
887 div1->n_col = div2->n_col;
889 equal = isl_mat_is_equal(div1, div2);
897 static void expand_row(__isl_keep isl_mat *dst, int d,
898 __isl_keep isl_mat *src, int s, int *exp)
901 unsigned c = src->n_col - src->n_row;
903 isl_seq_cpy(dst->row[d], src->row[s], c);
904 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
906 for (i = 0; i < s; ++i)
907 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
910 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
914 li = isl_seq_last_non_zero(div->row[i], div->n_col);
915 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
920 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
923 struct isl_div_sort_info {
928 static int div_sort_cmp(const void *p1, const void *p2)
930 const struct isl_div_sort_info *i1, *i2;
931 i1 = (const struct isl_div_sort_info *) p1;
932 i2 = (const struct isl_div_sort_info *) p2;
934 return cmp_row(i1->div, i1->row, i2->row);
937 static __isl_give isl_mat *sort_divs(__isl_take isl_mat *div)
940 struct isl_div_sort_info *array = NULL;
948 array = isl_alloc_array(div->ctx, struct isl_div_sort_info, div->n_row);
949 pos = isl_alloc_array(div->ctx, int, div->n_row);
953 for (i = 0; i < div->n_row; ++i) {
959 qsort(array, div->n_row, sizeof(struct isl_div_sort_info),
962 for (i = 0; i < div->n_row; ++i) {
964 if (pos[array[i].row] == i)
966 div = isl_mat_cow(div);
967 div = isl_mat_swap_rows(div, i, pos[array[i].row]);
968 t = pos[array[i].row];
969 pos[array[i].row] = pos[i];
983 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
984 __isl_keep isl_mat *div2, int *exp1, int *exp2)
988 unsigned d = div1->n_col - div1->n_row;
990 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
991 d + div1->n_row + div2->n_row);
995 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
998 expand_row(div, k, div1, i, exp1);
999 expand_row(div, k + 1, div2, j, exp2);
1001 cmp = cmp_row(div, k, k + 1);
1005 } else if (cmp < 0) {
1009 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1012 for (; i < div1->n_row; ++i, ++k) {
1013 expand_row(div, k, div1, i, exp1);
1016 for (; j < div2->n_row; ++j, ++k) {
1017 expand_row(div, k, div2, j, exp2);
1027 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1028 int *exp, int first)
1031 struct isl_upoly_rec *rec;
1033 if (isl_upoly_is_cst(up))
1036 if (up->var < first)
1039 if (exp[up->var - first] == up->var - first)
1042 up = isl_upoly_cow(up);
1046 up->var = exp[up->var - first] + first;
1048 rec = isl_upoly_as_rec(up);
1052 for (i = 0; i < rec->n; ++i) {
1053 rec->p[i] = expand(rec->p[i], exp, first);
1064 static __isl_give isl_qpolynomial *with_merged_divs(
1065 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1066 __isl_take isl_qpolynomial *qp2),
1067 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1071 isl_mat *div = NULL;
1073 qp1 = isl_qpolynomial_cow(qp1);
1074 qp2 = isl_qpolynomial_cow(qp2);
1079 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1080 qp1->div->n_col >= qp2->div->n_col, goto error);
1082 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1083 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1087 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1091 isl_mat_free(qp1->div);
1092 qp1->div = isl_mat_copy(div);
1093 isl_mat_free(qp2->div);
1094 qp2->div = isl_mat_copy(div);
1096 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1097 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1099 if (!qp1->upoly || !qp2->upoly)
1106 return fn(qp1, qp2);
1111 isl_qpolynomial_free(qp1);
1112 isl_qpolynomial_free(qp2);
1116 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1117 __isl_take isl_qpolynomial *qp2)
1119 qp1 = isl_qpolynomial_cow(qp1);
1124 if (qp1->div->n_row < qp2->div->n_row)
1125 return isl_qpolynomial_add(qp2, qp1);
1127 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1128 if (!compatible_divs(qp1->div, qp2->div))
1129 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1131 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1135 isl_qpolynomial_free(qp2);
1139 isl_qpolynomial_free(qp1);
1140 isl_qpolynomial_free(qp2);
1144 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1145 __isl_take isl_qpolynomial *qp2)
1147 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1150 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1152 qp = isl_qpolynomial_cow(qp);
1157 qp->upoly = isl_upoly_neg(qp->upoly);
1163 isl_qpolynomial_free(qp);
1167 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1168 __isl_take isl_qpolynomial *qp2)
1170 qp1 = isl_qpolynomial_cow(qp1);
1175 if (qp1->div->n_row < qp2->div->n_row)
1176 return isl_qpolynomial_mul(qp2, qp1);
1178 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1179 if (!compatible_divs(qp1->div, qp2->div))
1180 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1182 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1186 isl_qpolynomial_free(qp2);
1190 isl_qpolynomial_free(qp1);
1191 isl_qpolynomial_free(qp2);
1195 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1197 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1200 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1202 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1205 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1207 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1210 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1213 struct isl_qpolynomial *qp;
1214 struct isl_upoly_cst *cst;
1216 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1220 cst = isl_upoly_as_cst(qp->upoly);
1221 isl_int_set(cst->n, v);
1225 isl_qpolynomial_free(qp);
1229 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1230 isl_int *n, isl_int *d)
1232 struct isl_upoly_cst *cst;
1237 if (!isl_upoly_is_cst(qp->upoly))
1240 cst = isl_upoly_as_cst(qp->upoly);
1245 isl_int_set(*n, cst->n);
1247 isl_int_set(*d, cst->d);
1252 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1255 struct isl_upoly_rec *rec;
1263 rec = isl_upoly_as_rec(up);
1270 isl_assert(up->ctx, rec->n > 1, return -1);
1272 is_cst = isl_upoly_is_cst(rec->p[1]);
1278 return isl_upoly_is_affine(rec->p[0]);
1281 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1286 if (qp->div->n_row > 0)
1289 return isl_upoly_is_affine(qp->upoly);
1292 static void update_coeff(__isl_keep isl_vec *aff,
1293 __isl_keep struct isl_upoly_cst *cst, int pos)
1298 if (isl_int_is_zero(cst->n))
1303 isl_int_gcd(gcd, cst->d, aff->el[0]);
1304 isl_int_divexact(f, cst->d, gcd);
1305 isl_int_divexact(gcd, aff->el[0], gcd);
1306 isl_seq_scale(aff->el, aff->el, f, aff->size);
1307 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1312 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1313 __isl_keep isl_vec *aff)
1315 struct isl_upoly_cst *cst;
1316 struct isl_upoly_rec *rec;
1322 struct isl_upoly_cst *cst;
1324 cst = isl_upoly_as_cst(up);
1327 update_coeff(aff, cst, 0);
1331 rec = isl_upoly_as_rec(up);
1334 isl_assert(up->ctx, rec->n == 2, return -1);
1336 cst = isl_upoly_as_cst(rec->p[1]);
1339 update_coeff(aff, cst, 1 + up->var);
1341 return isl_upoly_update_affine(rec->p[0], aff);
1344 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1345 __isl_keep isl_qpolynomial *qp)
1353 isl_assert(qp->div->ctx, qp->div->n_row == 0, return NULL);
1354 d = isl_dim_total(qp->dim);
1355 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
1359 isl_seq_clr(aff->el + 1, 1 + d);
1360 isl_int_set_si(aff->el[0], 1);
1362 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1371 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1372 __isl_keep isl_qpolynomial *qp2)
1377 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1380 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1383 struct isl_upoly_rec *rec;
1385 if (isl_upoly_is_cst(up)) {
1386 struct isl_upoly_cst *cst;
1387 cst = isl_upoly_as_cst(up);
1390 isl_int_lcm(*d, *d, cst->d);
1394 rec = isl_upoly_as_rec(up);
1398 for (i = 0; i < rec->n; ++i)
1399 upoly_update_den(rec->p[i], d);
1402 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1404 isl_int_set_si(*d, 1);
1407 upoly_update_den(qp->upoly, d);
1410 __isl_give struct isl_upoly *isl_upoly_pow(isl_ctx *ctx, int pos, int power)
1413 struct isl_upoly *up;
1414 struct isl_upoly_rec *rec;
1415 struct isl_upoly_cst *cst;
1417 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1420 for (i = 0; i < 1 + power; ++i) {
1421 rec->p[i] = isl_upoly_zero(ctx);
1426 cst = isl_upoly_as_cst(rec->p[power]);
1427 isl_int_set_si(cst->n, 1);
1431 isl_upoly_free(&rec->up);
1435 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_dim *dim,
1438 struct isl_ctx *ctx;
1445 return isl_qpolynomial_alloc(dim, 0, isl_upoly_pow(ctx, pos, power));
1448 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1449 enum isl_dim_type type, unsigned pos)
1454 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1455 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1457 if (type == isl_dim_set)
1458 pos += isl_dim_size(dim, isl_dim_param);
1460 return isl_qpolynomial_pow(dim, pos, 1);
1466 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
1469 struct isl_qpolynomial *qp = NULL;
1470 struct isl_upoly_rec *rec;
1471 struct isl_upoly_cst *cst;
1477 isl_assert(div->ctx, div->bmap->n_div == 1, goto error);
1479 pos = isl_dim_total(div->bmap->dim);
1480 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
1481 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap), 1,
1486 isl_seq_cpy(qp->div->row[0], div->line[0], qp->div->n_col - 1);
1487 isl_int_set_si(qp->div->row[0][qp->div->n_col - 1], 0);
1489 for (i = 0; i < 1 + power; ++i) {
1490 rec->p[i] = isl_upoly_zero(div->ctx);
1495 cst = isl_upoly_as_cst(rec->p[power]);
1496 isl_int_set_si(cst->n, 1);
1502 isl_qpolynomial_free(qp);
1507 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
1509 return isl_qpolynomial_div_pow(div, 1);
1512 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
1513 const isl_int n, const isl_int d)
1515 struct isl_qpolynomial *qp;
1516 struct isl_upoly_cst *cst;
1518 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1522 cst = isl_upoly_as_cst(qp->upoly);
1523 isl_int_set(cst->n, n);
1524 isl_int_set(cst->d, d);
1528 isl_qpolynomial_free(qp);
1532 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
1534 struct isl_upoly_rec *rec;
1540 if (isl_upoly_is_cst(up))
1544 active[up->var] = 1;
1546 rec = isl_upoly_as_rec(up);
1547 for (i = 0; i < rec->n; ++i)
1548 if (up_set_active(rec->p[i], active, d) < 0)
1554 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
1557 int d = isl_dim_total(qp->dim);
1562 for (i = 0; i < d; ++i)
1563 for (j = 0; j < qp->div->n_row; ++j) {
1564 if (isl_int_is_zero(qp->div->row[j][2 + i]))
1570 return up_set_active(qp->upoly, active, d);
1573 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
1574 enum isl_dim_type type, unsigned first, unsigned n)
1585 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1587 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1588 type == isl_dim_set, return -1);
1590 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
1591 if (set_active(qp, active) < 0)
1594 if (type == isl_dim_set)
1595 first += isl_dim_size(qp->dim, isl_dim_param);
1596 for (i = 0; i < n; ++i)
1597 if (active[first + i]) {
1610 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
1611 unsigned first, unsigned n)
1614 struct isl_upoly_rec *rec;
1618 if (n == 0 || up->var < 0 || up->var < first)
1620 if (up->var < first + n) {
1621 up = replace_by_constant_term(up);
1622 return isl_upoly_drop(up, first, n);
1624 up = isl_upoly_cow(up);
1628 rec = isl_upoly_as_rec(up);
1632 for (i = 0; i < rec->n; ++i) {
1633 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
1644 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
1645 __isl_take isl_qpolynomial *qp,
1646 enum isl_dim_type type, unsigned first, unsigned n)
1653 qp = isl_qpolynomial_cow(qp);
1657 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1659 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1660 type == isl_dim_set, goto error);
1662 qp->dim = isl_dim_drop(qp->dim, type, first, n);
1666 if (type == isl_dim_set)
1667 first += isl_dim_size(qp->dim, isl_dim_param);
1669 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
1673 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
1679 isl_qpolynomial_free(qp);
1684 #define PW isl_pw_qpolynomial
1686 #define EL isl_qpolynomial
1688 #define IS_ZERO is_zero
1692 #define ADD(d,e1,e2) isl_qpolynomial_add(e1,e2)
1694 #include <isl_pw_templ.c>
1696 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
1704 if (!isl_set_fast_is_universe(pwqp->p[0].set))
1707 return isl_qpolynomial_is_one(pwqp->p[0].qp);
1710 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
1711 __isl_take isl_pw_qpolynomial *pwqp1,
1712 __isl_take isl_pw_qpolynomial *pwqp2)
1715 struct isl_pw_qpolynomial *res;
1718 if (!pwqp1 || !pwqp2)
1721 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
1724 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
1725 isl_pw_qpolynomial_free(pwqp2);
1729 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
1730 isl_pw_qpolynomial_free(pwqp1);
1734 if (isl_pw_qpolynomial_is_one(pwqp1)) {
1735 isl_pw_qpolynomial_free(pwqp1);
1739 if (isl_pw_qpolynomial_is_one(pwqp2)) {
1740 isl_pw_qpolynomial_free(pwqp2);
1744 n = pwqp1->n * pwqp2->n;
1745 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
1747 for (i = 0; i < pwqp1->n; ++i) {
1748 for (j = 0; j < pwqp2->n; ++j) {
1749 struct isl_set *common;
1750 struct isl_qpolynomial *prod;
1751 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
1752 isl_set_copy(pwqp2->p[j].set));
1753 if (isl_set_fast_is_empty(common)) {
1754 isl_set_free(common);
1758 prod = isl_qpolynomial_mul(
1759 isl_qpolynomial_copy(pwqp1->p[i].qp),
1760 isl_qpolynomial_copy(pwqp2->p[j].qp));
1762 res = isl_pw_qpolynomial_add_piece(res, common, prod);
1766 isl_pw_qpolynomial_free(pwqp1);
1767 isl_pw_qpolynomial_free(pwqp2);
1771 isl_pw_qpolynomial_free(pwqp1);
1772 isl_pw_qpolynomial_free(pwqp2);
1776 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
1777 __isl_take isl_pw_qpolynomial *pwqp)
1780 struct isl_pw_qpolynomial *res;
1786 if (isl_pw_qpolynomial_is_zero(pwqp))
1789 pwqp = isl_pw_qpolynomial_cow(pwqp);
1791 for (i = 0; i < pwqp->n; ++i) {
1792 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
1799 isl_pw_qpolynomial_free(pwqp);
1803 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
1804 __isl_take isl_pw_qpolynomial *pwqp1,
1805 __isl_take isl_pw_qpolynomial *pwqp2)
1807 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
1810 __isl_give struct isl_upoly *isl_upoly_eval(
1811 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
1814 struct isl_upoly_rec *rec;
1815 struct isl_upoly *res;
1816 struct isl_upoly *base;
1818 if (isl_upoly_is_cst(up)) {
1823 rec = isl_upoly_as_rec(up);
1827 isl_assert(up->ctx, rec->n >= 1, goto error);
1829 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
1831 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
1834 for (i = rec->n - 2; i >= 0; --i) {
1835 res = isl_upoly_mul(res, isl_upoly_copy(base));
1836 res = isl_upoly_sum(res,
1837 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
1838 isl_vec_copy(vec)));
1841 isl_upoly_free(base);
1851 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
1852 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
1855 struct isl_upoly *up;
1860 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
1862 if (qp->div->n_row == 0)
1863 ext = isl_vec_copy(pnt->vec);
1866 unsigned dim = isl_dim_total(qp->dim);
1867 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
1871 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
1872 for (i = 0; i < qp->div->n_row; ++i) {
1873 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
1874 1 + dim + i, &ext->el[1+dim+i]);
1875 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
1876 qp->div->row[i][0]);
1880 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
1884 dim = isl_dim_copy(qp->dim);
1885 isl_qpolynomial_free(qp);
1886 isl_point_free(pnt);
1888 return isl_qpolynomial_alloc(dim, 0, up);
1890 isl_qpolynomial_free(qp);
1891 isl_point_free(pnt);
1895 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
1896 __isl_keep struct isl_upoly_cst *cst2)
1901 isl_int_mul(t, cst1->n, cst2->d);
1902 isl_int_submul(t, cst2->n, cst1->d);
1903 cmp = isl_int_sgn(t);
1908 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
1909 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1911 struct isl_upoly_cst *cst1, *cst2;
1916 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
1917 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
1918 cst1 = isl_upoly_as_cst(qp1->upoly);
1919 cst2 = isl_upoly_as_cst(qp2->upoly);
1920 cmp = isl_upoly_cmp(cst1, cst2);
1923 isl_qpolynomial_free(qp2);
1925 isl_qpolynomial_free(qp1);
1930 isl_qpolynomial_free(qp1);
1931 isl_qpolynomial_free(qp2);
1935 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
1936 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1938 struct isl_upoly_cst *cst1, *cst2;
1943 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
1944 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
1945 cst1 = isl_upoly_as_cst(qp1->upoly);
1946 cst2 = isl_upoly_as_cst(qp2->upoly);
1947 cmp = isl_upoly_cmp(cst1, cst2);
1950 isl_qpolynomial_free(qp2);
1952 isl_qpolynomial_free(qp1);
1957 isl_qpolynomial_free(qp1);
1958 isl_qpolynomial_free(qp2);
1962 static int *reordering_move(isl_ctx *ctx,
1963 unsigned len, unsigned dst, unsigned src, unsigned n)
1968 reordering = isl_alloc_array(ctx, int, len);
1973 for (i = 0; i < dst; ++i)
1975 for (i = 0; i < n; ++i)
1976 reordering[src + i] = dst + i;
1977 for (i = 0; i < src - dst; ++i)
1978 reordering[dst + i] = dst + n + i;
1979 for (i = 0; i < len - src - n; ++i)
1980 reordering[src + n + i] = src + n + i;
1982 for (i = 0; i < src; ++i)
1984 for (i = 0; i < n; ++i)
1985 reordering[src + i] = dst + i;
1986 for (i = 0; i < dst - src; ++i)
1987 reordering[src + n + i] = src + i;
1988 for (i = 0; i < len - dst - n; ++i)
1989 reordering[dst + n + i] = dst + n + i;
1995 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1999 struct isl_upoly_rec *rec;
2000 struct isl_upoly *base;
2001 struct isl_upoly *res;
2003 if (isl_upoly_is_cst(up))
2006 rec = isl_upoly_as_rec(up);
2010 isl_assert(up->ctx, rec->n >= 1, goto error);
2012 base = isl_upoly_pow(up->ctx, r[up->var], 1);
2013 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
2015 for (i = rec->n - 2; i >= 0; --i) {
2016 res = isl_upoly_mul(res, isl_upoly_copy(base));
2017 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
2020 isl_upoly_free(base);
2029 __isl_give isl_qpolynomial *isl_qpolynomial_move(__isl_take isl_qpolynomial *qp,
2030 enum isl_dim_type dst_type, unsigned dst_pos,
2031 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2037 qp = isl_qpolynomial_cow(qp);
2041 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2044 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2045 g_src_pos = pos(qp->dim, src_type) + src_pos;
2046 if (dst_type > src_type)
2049 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2050 qp->div = sort_divs(qp->div);
2054 reordering = reordering_move(qp->dim->ctx,
2055 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2059 qp->upoly = reorder(qp->upoly, reordering);
2064 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2070 isl_qpolynomial_free(qp);
2074 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_move(
2075 __isl_take isl_pw_qpolynomial *pwqp,
2076 enum isl_dim_type dst_type, unsigned dst_pos,
2077 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2081 pwqp = isl_pw_qpolynomial_cow(pwqp);
2085 pwqp->dim = isl_dim_move(pwqp->dim,
2086 dst_type, dst_pos, src_type, src_pos, n);
2090 for (i = 0; i < pwqp->n; ++i) {
2091 pwqp->p[i].set = isl_set_move(pwqp->p[i].set, dst_type, dst_pos,
2092 src_type, src_pos, n);
2093 if (!pwqp->p[i].set)
2095 pwqp->p[i].qp = isl_qpolynomial_move(pwqp->p[i].qp,
2096 dst_type, dst_pos, src_type, src_pos, n);
2103 isl_pw_qpolynomial_free(pwqp);
2107 __isl_give isl_dim *isl_pw_qpolynomial_get_dim(
2108 __isl_keep isl_pw_qpolynomial *pwqp)
2113 return isl_dim_copy(pwqp->dim);
2116 unsigned isl_pw_qpolynomial_dim(__isl_keep isl_pw_qpolynomial *pwqp,
2117 enum isl_dim_type type)
2119 return pwqp ? isl_dim_size(pwqp->dim, type) : 0;
2122 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
2123 __isl_take isl_mat *div)
2131 n = isl_dim_total(dim) + div->n_row;
2133 term = isl_calloc(dim->ctx, struct isl_term,
2134 sizeof(struct isl_term) + (n - 1) * sizeof(int));
2141 isl_int_init(term->n);
2142 isl_int_init(term->d);
2151 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
2160 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
2169 total = isl_dim_total(term->dim) + term->div->n_row;
2171 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
2175 isl_int_set(dup->n, term->n);
2176 isl_int_set(dup->d, term->d);
2178 for (i = 0; i < total; ++i)
2179 dup->pow[i] = term->pow[i];
2184 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
2192 return isl_term_dup(term);
2195 void isl_term_free(__isl_take isl_term *term)
2200 if (--term->ref > 0)
2203 isl_dim_free(term->dim);
2204 isl_mat_free(term->div);
2205 isl_int_clear(term->n);
2206 isl_int_clear(term->d);
2210 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
2218 case isl_dim_out: return isl_dim_size(term->dim, type);
2219 case isl_dim_div: return term->div->n_row;
2220 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
2224 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
2226 return term ? term->dim->ctx : NULL;
2229 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
2233 isl_int_set(*n, term->n);
2236 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
2240 isl_int_set(*d, term->d);
2243 int isl_term_get_exp(__isl_keep isl_term *term,
2244 enum isl_dim_type type, unsigned pos)
2249 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
2251 if (type >= isl_dim_set)
2252 pos += isl_dim_size(term->dim, isl_dim_param);
2253 if (type >= isl_dim_div)
2254 pos += isl_dim_size(term->dim, isl_dim_set);
2256 return term->pow[pos];
2259 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
2261 isl_basic_map *bmap;
2268 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
2271 total = term->div->n_col - term->div->n_row - 2;
2272 /* No nested divs for now */
2273 isl_assert(term->dim->ctx,
2274 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
2275 term->div->n_row) == -1,
2278 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
2279 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
2282 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
2284 return isl_basic_map_div(bmap, k);
2286 isl_basic_map_free(bmap);
2290 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
2291 int (*fn)(__isl_take isl_term *term, void *user),
2292 __isl_take isl_term *term, void *user)
2295 struct isl_upoly_rec *rec;
2300 if (isl_upoly_is_zero(up))
2303 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
2304 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
2305 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
2307 if (isl_upoly_is_cst(up)) {
2308 struct isl_upoly_cst *cst;
2309 cst = isl_upoly_as_cst(up);
2312 term = isl_term_cow(term);
2315 isl_int_set(term->n, cst->n);
2316 isl_int_set(term->d, cst->d);
2317 if (fn(isl_term_copy(term), user) < 0)
2322 rec = isl_upoly_as_rec(up);
2326 for (i = 0; i < rec->n; ++i) {
2327 term = isl_term_cow(term);
2330 term->pow[up->var] = i;
2331 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
2335 term->pow[up->var] = 0;
2339 isl_term_free(term);
2343 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
2344 int (*fn)(__isl_take isl_term *term, void *user), void *user)
2351 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
2355 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
2357 isl_term_free(term);
2359 return term ? 0 : -1;
2362 int isl_pw_qpolynomial_foreach_piece(__isl_keep isl_pw_qpolynomial *pwqp,
2363 int (*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
2364 void *user), void *user)
2371 for (i = 0; i < pwqp->n; ++i)
2372 if (fn(isl_set_copy(pwqp->p[i].set),
2373 isl_qpolynomial_copy(pwqp->p[i].qp), user) < 0)
2379 static int any_divs(__isl_keep isl_set *set)
2386 for (i = 0; i < set->n; ++i)
2387 if (set->p[i]->n_div > 0)
2393 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
2394 __isl_take isl_dim *dim)
2399 if (isl_dim_equal(qp->dim, dim)) {
2404 qp = isl_qpolynomial_cow(qp);
2408 if (qp->div->n_row) {
2414 extra = isl_dim_size(dim, isl_dim_set) -
2415 isl_dim_size(qp->dim, isl_dim_set);
2416 total = isl_dim_total(qp->dim);
2417 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
2420 for (i = 0; i < qp->div->n_row; ++i)
2422 qp->upoly = expand(qp->upoly, exp, total);
2426 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
2429 for (i = 0; i < qp->div->n_row; ++i)
2430 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
2433 isl_dim_free(qp->dim);
2439 isl_qpolynomial_free(qp);
2443 static int foreach_lifted_subset(__isl_take isl_set *set,
2444 __isl_take isl_qpolynomial *qp,
2445 int (*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
2446 void *user), void *user)
2453 for (i = 0; i < set->n; ++i) {
2455 isl_qpolynomial *copy;
2457 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
2458 lift = isl_set_lift(lift);
2460 copy = isl_qpolynomial_copy(qp);
2461 copy = isl_qpolynomial_lift(copy, isl_set_get_dim(lift));
2463 if (fn(lift, copy, user) < 0)
2468 isl_qpolynomial_free(qp);
2473 isl_qpolynomial_free(qp);
2477 int isl_pw_qpolynomial_foreach_lifted_piece(__isl_keep isl_pw_qpolynomial *pwqp,
2478 int (*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
2479 void *user), void *user)
2486 for (i = 0; i < pwqp->n; ++i) {
2488 isl_qpolynomial *qp;
2490 set = isl_set_copy(pwqp->p[i].set);
2491 qp = isl_qpolynomial_copy(pwqp->p[i].qp);
2492 if (!any_divs(set)) {
2493 if (fn(set, qp, user) < 0)
2497 if (foreach_lifted_subset(set, qp, fn, user) < 0)
2504 /* For each parameter or variable that does not appear in qp,
2505 * first eliminate the variable from all constraints and then set it to zero.
2507 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
2508 __isl_keep isl_qpolynomial *qp)
2519 d = isl_dim_total(set->dim);
2520 active = isl_calloc_array(set->ctx, int, d);
2521 if (set_active(qp, active) < 0)
2524 for (i = 0; i < d; ++i)
2533 nparam = isl_dim_size(set->dim, isl_dim_param);
2534 nvar = isl_dim_size(set->dim, isl_dim_set);
2535 for (i = 0; i < nparam; ++i) {
2538 set = isl_set_eliminate(set, isl_dim_param, i, 1);
2539 set = isl_set_fix_si(set, isl_dim_param, i, 0);
2541 for (i = 0; i < nvar; ++i) {
2544 set = isl_set_eliminate(set, isl_dim_set, i, 1);
2545 set = isl_set_fix_si(set, isl_dim_set, i, 0);
2557 struct isl_max_data {
2558 isl_qpolynomial *qp;
2560 isl_qpolynomial *max;
2563 static int max_fn(__isl_take isl_point *pnt, void *user)
2565 struct isl_max_data *data = (struct isl_max_data *)user;
2566 isl_qpolynomial *val;
2568 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
2573 data->max = isl_qpolynomial_max_cst(data->max, val);
2579 static __isl_give isl_qpolynomial *guarded_qpolynomial_max(
2580 __isl_take isl_set *set, __isl_take isl_qpolynomial *qp)
2582 struct isl_max_data data = { NULL, 1, NULL };
2587 if (isl_upoly_is_cst(qp->upoly)) {
2592 set = fix_inactive(set, qp);
2595 if (isl_set_foreach_point(set, max_fn, &data) < 0)
2599 isl_qpolynomial_free(qp);
2603 isl_qpolynomial_free(qp);
2604 isl_qpolynomial_free(data.max);
2608 /* Compute the maximal value attained by the piecewise quasipolynomial
2609 * on its domain or zero if the domain is empty.
2610 * In the worst case, the domain is scanned completely,
2611 * so the domain is assumed to be bounded.
2613 __isl_give isl_qpolynomial *isl_pw_qpolynomial_max(
2614 __isl_take isl_pw_qpolynomial *pwqp)
2617 isl_qpolynomial *max;
2623 isl_dim *dim = isl_dim_copy(pwqp->dim);
2624 isl_pw_qpolynomial_free(pwqp);
2625 return isl_qpolynomial_zero(dim);
2628 max = guarded_qpolynomial_max(isl_set_copy(pwqp->p[0].set),
2629 isl_qpolynomial_copy(pwqp->p[0].qp));
2630 for (i = 1; i < pwqp->n; ++i) {
2631 isl_qpolynomial *max_i;
2632 max_i = guarded_qpolynomial_max(isl_set_copy(pwqp->p[i].set),
2633 isl_qpolynomial_copy(pwqp->p[i].qp));
2634 max = isl_qpolynomial_max_cst(max, max_i);
2637 isl_pw_qpolynomial_free(pwqp);