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_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_factorization.h>
18 #include <isl_union_map_private.h>
19 #include <isl_constraint_private.h>
20 #include <isl_polynomial_private.h>
21 #include <isl_point_private.h>
22 #include <isl_space_private.h>
23 #include <isl_div_private.h>
24 #include <isl_mat_private.h>
25 #include <isl_range.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_config.h>
30 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
33 case isl_dim_param: return 0;
34 case isl_dim_in: return dim->nparam;
35 case isl_dim_out: return dim->nparam + dim->n_in;
40 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
48 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
53 isl_assert(up->ctx, up->var < 0, return NULL);
55 return (struct isl_upoly_cst *)up;
58 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
63 isl_assert(up->ctx, up->var >= 0, return NULL);
65 return (struct isl_upoly_rec *)up;
68 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
69 __isl_keep struct isl_upoly *up2)
72 struct isl_upoly_rec *rec1, *rec2;
78 if (up1->var != up2->var)
80 if (isl_upoly_is_cst(up1)) {
81 struct isl_upoly_cst *cst1, *cst2;
82 cst1 = isl_upoly_as_cst(up1);
83 cst2 = isl_upoly_as_cst(up2);
86 return isl_int_eq(cst1->n, cst2->n) &&
87 isl_int_eq(cst1->d, cst2->d);
90 rec1 = isl_upoly_as_rec(up1);
91 rec2 = isl_upoly_as_rec(up2);
95 if (rec1->n != rec2->n)
98 for (i = 0; i < rec1->n; ++i) {
99 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
107 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
109 struct isl_upoly_cst *cst;
113 if (!isl_upoly_is_cst(up))
116 cst = isl_upoly_as_cst(up);
120 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
123 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
125 struct isl_upoly_cst *cst;
129 if (!isl_upoly_is_cst(up))
132 cst = isl_upoly_as_cst(up);
136 return isl_int_sgn(cst->n);
139 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
141 struct isl_upoly_cst *cst;
145 if (!isl_upoly_is_cst(up))
148 cst = isl_upoly_as_cst(up);
152 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
155 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
157 struct isl_upoly_cst *cst;
161 if (!isl_upoly_is_cst(up))
164 cst = isl_upoly_as_cst(up);
168 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
171 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
173 struct isl_upoly_cst *cst;
177 if (!isl_upoly_is_cst(up))
180 cst = isl_upoly_as_cst(up);
184 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
187 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
189 struct isl_upoly_cst *cst;
193 if (!isl_upoly_is_cst(up))
196 cst = isl_upoly_as_cst(up);
200 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
203 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
205 struct isl_upoly_cst *cst;
209 if (!isl_upoly_is_cst(up))
212 cst = isl_upoly_as_cst(up);
216 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
219 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
221 struct isl_upoly_cst *cst;
223 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
232 isl_int_init(cst->n);
233 isl_int_init(cst->d);
238 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
240 struct isl_upoly_cst *cst;
242 cst = isl_upoly_cst_alloc(ctx);
246 isl_int_set_si(cst->n, 0);
247 isl_int_set_si(cst->d, 1);
252 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
254 struct isl_upoly_cst *cst;
256 cst = isl_upoly_cst_alloc(ctx);
260 isl_int_set_si(cst->n, 1);
261 isl_int_set_si(cst->d, 1);
266 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
268 struct isl_upoly_cst *cst;
270 cst = isl_upoly_cst_alloc(ctx);
274 isl_int_set_si(cst->n, 1);
275 isl_int_set_si(cst->d, 0);
280 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
282 struct isl_upoly_cst *cst;
284 cst = isl_upoly_cst_alloc(ctx);
288 isl_int_set_si(cst->n, -1);
289 isl_int_set_si(cst->d, 0);
294 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
296 struct isl_upoly_cst *cst;
298 cst = isl_upoly_cst_alloc(ctx);
302 isl_int_set_si(cst->n, 0);
303 isl_int_set_si(cst->d, 0);
308 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
309 isl_int n, isl_int d)
311 struct isl_upoly_cst *cst;
313 cst = isl_upoly_cst_alloc(ctx);
317 isl_int_set(cst->n, n);
318 isl_int_set(cst->d, d);
323 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
326 struct isl_upoly_rec *rec;
328 isl_assert(ctx, var >= 0, return NULL);
329 isl_assert(ctx, size >= 0, return NULL);
330 rec = isl_calloc(ctx, struct isl_upoly_rec,
331 sizeof(struct isl_upoly_rec) +
332 size * sizeof(struct isl_upoly *));
347 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space(
348 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
350 qp = isl_qpolynomial_cow(qp);
354 isl_space_free(qp->dim);
359 isl_qpolynomial_free(qp);
364 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
366 return qp ? qp->dim->ctx : NULL;
369 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
371 return qp ? isl_space_copy(qp->dim) : NULL;
374 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
375 enum isl_dim_type type)
377 return qp ? isl_space_dim(qp->dim, type) : 0;
380 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
382 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
385 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
387 return qp ? isl_upoly_is_one(qp->upoly) : -1;
390 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
392 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
395 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
397 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
400 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
402 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
405 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
407 return qp ? isl_upoly_sgn(qp->upoly) : 0;
410 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
412 isl_int_clear(cst->n);
413 isl_int_clear(cst->d);
416 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
420 for (i = 0; i < rec->n; ++i)
421 isl_upoly_free(rec->p[i]);
424 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
433 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
435 struct isl_upoly_cst *cst;
436 struct isl_upoly_cst *dup;
438 cst = isl_upoly_as_cst(up);
442 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
445 isl_int_set(dup->n, cst->n);
446 isl_int_set(dup->d, cst->d);
451 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
454 struct isl_upoly_rec *rec;
455 struct isl_upoly_rec *dup;
457 rec = isl_upoly_as_rec(up);
461 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
465 for (i = 0; i < rec->n; ++i) {
466 dup->p[i] = isl_upoly_copy(rec->p[i]);
474 isl_upoly_free(&dup->up);
478 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
483 if (isl_upoly_is_cst(up))
484 return isl_upoly_dup_cst(up);
486 return isl_upoly_dup_rec(up);
489 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
497 return isl_upoly_dup(up);
500 void isl_upoly_free(__isl_take struct isl_upoly *up)
509 upoly_free_cst((struct isl_upoly_cst *)up);
511 upoly_free_rec((struct isl_upoly_rec *)up);
513 isl_ctx_deref(up->ctx);
517 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
522 isl_int_gcd(gcd, cst->n, cst->d);
523 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
524 isl_int_divexact(cst->n, cst->n, gcd);
525 isl_int_divexact(cst->d, cst->d, gcd);
530 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
531 __isl_take struct isl_upoly *up2)
533 struct isl_upoly_cst *cst1;
534 struct isl_upoly_cst *cst2;
536 up1 = isl_upoly_cow(up1);
540 cst1 = isl_upoly_as_cst(up1);
541 cst2 = isl_upoly_as_cst(up2);
543 if (isl_int_eq(cst1->d, cst2->d))
544 isl_int_add(cst1->n, cst1->n, cst2->n);
546 isl_int_mul(cst1->n, cst1->n, cst2->d);
547 isl_int_addmul(cst1->n, cst2->n, cst1->d);
548 isl_int_mul(cst1->d, cst1->d, cst2->d);
551 isl_upoly_cst_reduce(cst1);
561 static __isl_give struct isl_upoly *replace_by_zero(
562 __isl_take struct isl_upoly *up)
570 return isl_upoly_zero(ctx);
573 static __isl_give struct isl_upoly *replace_by_constant_term(
574 __isl_take struct isl_upoly *up)
576 struct isl_upoly_rec *rec;
577 struct isl_upoly *cst;
582 rec = isl_upoly_as_rec(up);
585 cst = isl_upoly_copy(rec->p[0]);
593 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
594 __isl_take struct isl_upoly *up2)
597 struct isl_upoly_rec *rec1, *rec2;
602 if (isl_upoly_is_nan(up1)) {
607 if (isl_upoly_is_nan(up2)) {
612 if (isl_upoly_is_zero(up1)) {
617 if (isl_upoly_is_zero(up2)) {
622 if (up1->var < up2->var)
623 return isl_upoly_sum(up2, up1);
625 if (up2->var < up1->var) {
626 struct isl_upoly_rec *rec;
627 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
631 up1 = isl_upoly_cow(up1);
632 rec = isl_upoly_as_rec(up1);
635 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
637 up1 = replace_by_constant_term(up1);
641 if (isl_upoly_is_cst(up1))
642 return isl_upoly_sum_cst(up1, up2);
644 rec1 = isl_upoly_as_rec(up1);
645 rec2 = isl_upoly_as_rec(up2);
649 if (rec1->n < rec2->n)
650 return isl_upoly_sum(up2, up1);
652 up1 = isl_upoly_cow(up1);
653 rec1 = isl_upoly_as_rec(up1);
657 for (i = rec2->n - 1; i >= 0; --i) {
658 rec1->p[i] = isl_upoly_sum(rec1->p[i],
659 isl_upoly_copy(rec2->p[i]));
662 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
663 isl_upoly_free(rec1->p[i]);
669 up1 = replace_by_zero(up1);
670 else if (rec1->n == 1)
671 up1 = replace_by_constant_term(up1);
682 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
683 __isl_take struct isl_upoly *up, isl_int v)
685 struct isl_upoly_cst *cst;
687 up = isl_upoly_cow(up);
691 cst = isl_upoly_as_cst(up);
693 isl_int_addmul(cst->n, cst->d, v);
698 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
699 __isl_take struct isl_upoly *up, isl_int v)
701 struct isl_upoly_rec *rec;
706 if (isl_upoly_is_cst(up))
707 return isl_upoly_cst_add_isl_int(up, v);
709 up = isl_upoly_cow(up);
710 rec = isl_upoly_as_rec(up);
714 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
724 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
725 __isl_take struct isl_upoly *up, isl_int v)
727 struct isl_upoly_cst *cst;
729 if (isl_upoly_is_zero(up))
732 up = isl_upoly_cow(up);
736 cst = isl_upoly_as_cst(up);
738 isl_int_mul(cst->n, cst->n, v);
743 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
744 __isl_take struct isl_upoly *up, isl_int v)
747 struct isl_upoly_rec *rec;
752 if (isl_upoly_is_cst(up))
753 return isl_upoly_cst_mul_isl_int(up, v);
755 up = isl_upoly_cow(up);
756 rec = isl_upoly_as_rec(up);
760 for (i = 0; i < rec->n; ++i) {
761 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
772 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
773 __isl_take struct isl_upoly *up2)
775 struct isl_upoly_cst *cst1;
776 struct isl_upoly_cst *cst2;
778 up1 = isl_upoly_cow(up1);
782 cst1 = isl_upoly_as_cst(up1);
783 cst2 = isl_upoly_as_cst(up2);
785 isl_int_mul(cst1->n, cst1->n, cst2->n);
786 isl_int_mul(cst1->d, cst1->d, cst2->d);
788 isl_upoly_cst_reduce(cst1);
798 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
799 __isl_take struct isl_upoly *up2)
801 struct isl_upoly_rec *rec1;
802 struct isl_upoly_rec *rec2;
803 struct isl_upoly_rec *res = NULL;
807 rec1 = isl_upoly_as_rec(up1);
808 rec2 = isl_upoly_as_rec(up2);
811 size = rec1->n + rec2->n - 1;
812 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
816 for (i = 0; i < rec1->n; ++i) {
817 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
818 isl_upoly_copy(rec1->p[i]));
823 for (; i < size; ++i) {
824 res->p[i] = isl_upoly_zero(up1->ctx);
829 for (i = 0; i < rec1->n; ++i) {
830 for (j = 1; j < rec2->n; ++j) {
831 struct isl_upoly *up;
832 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
833 isl_upoly_copy(rec1->p[i]));
834 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
847 isl_upoly_free(&res->up);
851 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
852 __isl_take struct isl_upoly *up2)
857 if (isl_upoly_is_nan(up1)) {
862 if (isl_upoly_is_nan(up2)) {
867 if (isl_upoly_is_zero(up1)) {
872 if (isl_upoly_is_zero(up2)) {
877 if (isl_upoly_is_one(up1)) {
882 if (isl_upoly_is_one(up2)) {
887 if (up1->var < up2->var)
888 return isl_upoly_mul(up2, up1);
890 if (up2->var < up1->var) {
892 struct isl_upoly_rec *rec;
893 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
894 isl_ctx *ctx = up1->ctx;
897 return isl_upoly_nan(ctx);
899 up1 = isl_upoly_cow(up1);
900 rec = isl_upoly_as_rec(up1);
904 for (i = 0; i < rec->n; ++i) {
905 rec->p[i] = isl_upoly_mul(rec->p[i],
906 isl_upoly_copy(up2));
914 if (isl_upoly_is_cst(up1))
915 return isl_upoly_mul_cst(up1, up2);
917 return isl_upoly_mul_rec(up1, up2);
924 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
927 struct isl_upoly *res;
935 res = isl_upoly_copy(up);
937 res = isl_upoly_one(up->ctx);
939 while (power >>= 1) {
940 up = isl_upoly_mul(up, isl_upoly_copy(up));
942 res = isl_upoly_mul(res, isl_upoly_copy(up));
949 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
950 unsigned n_div, __isl_take struct isl_upoly *up)
952 struct isl_qpolynomial *qp = NULL;
958 total = isl_space_dim(dim, isl_dim_all);
960 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
965 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
976 isl_qpolynomial_free(qp);
980 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
989 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
991 struct isl_qpolynomial *dup;
996 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
997 isl_upoly_copy(qp->upoly));
1000 isl_mat_free(dup->div);
1001 dup->div = isl_mat_copy(qp->div);
1007 isl_qpolynomial_free(dup);
1011 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1019 return isl_qpolynomial_dup(qp);
1022 void *isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1030 isl_space_free(qp->dim);
1031 isl_mat_free(qp->div);
1032 isl_upoly_free(qp->upoly);
1038 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1041 struct isl_upoly_rec *rec;
1042 struct isl_upoly_cst *cst;
1044 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1047 for (i = 0; i < 1 + power; ++i) {
1048 rec->p[i] = isl_upoly_zero(ctx);
1053 cst = isl_upoly_as_cst(rec->p[power]);
1054 isl_int_set_si(cst->n, 1);
1058 isl_upoly_free(&rec->up);
1062 /* r array maps original positions to new positions.
1064 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1068 struct isl_upoly_rec *rec;
1069 struct isl_upoly *base;
1070 struct isl_upoly *res;
1072 if (isl_upoly_is_cst(up))
1075 rec = isl_upoly_as_rec(up);
1079 isl_assert(up->ctx, rec->n >= 1, goto error);
1081 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1082 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1084 for (i = rec->n - 2; i >= 0; --i) {
1085 res = isl_upoly_mul(res, isl_upoly_copy(base));
1086 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1089 isl_upoly_free(base);
1098 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1103 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1104 div1->n_col >= div2->n_col, return -1);
1106 if (div1->n_row == div2->n_row)
1107 return isl_mat_is_equal(div1, div2);
1109 n_row = div1->n_row;
1110 n_col = div1->n_col;
1111 div1->n_row = div2->n_row;
1112 div1->n_col = div2->n_col;
1114 equal = isl_mat_is_equal(div1, div2);
1116 div1->n_row = n_row;
1117 div1->n_col = n_col;
1122 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1126 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1127 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1132 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1135 struct isl_div_sort_info {
1140 static int div_sort_cmp(const void *p1, const void *p2)
1142 const struct isl_div_sort_info *i1, *i2;
1143 i1 = (const struct isl_div_sort_info *) p1;
1144 i2 = (const struct isl_div_sort_info *) p2;
1146 return cmp_row(i1->div, i1->row, i2->row);
1149 /* Sort divs and remove duplicates.
1151 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1156 struct isl_div_sort_info *array = NULL;
1157 int *pos = NULL, *at = NULL;
1158 int *reordering = NULL;
1163 if (qp->div->n_row <= 1)
1166 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1168 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1170 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1171 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1172 len = qp->div->n_col - 2;
1173 reordering = isl_alloc_array(qp->div->ctx, int, len);
1174 if (!array || !pos || !at || !reordering)
1177 for (i = 0; i < qp->div->n_row; ++i) {
1178 array[i].div = qp->div;
1184 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1187 for (i = 0; i < div_pos; ++i)
1190 for (i = 0; i < qp->div->n_row; ++i) {
1191 if (pos[array[i].row] == i)
1193 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1194 pos[at[i]] = pos[array[i].row];
1195 at[pos[array[i].row]] = at[i];
1196 at[i] = array[i].row;
1197 pos[array[i].row] = i;
1201 for (i = 0; i < len - div_pos; ++i) {
1203 isl_seq_eq(qp->div->row[i - skip - 1],
1204 qp->div->row[i - skip], qp->div->n_col)) {
1205 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1206 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1207 2 + div_pos + i - skip);
1208 qp->div = isl_mat_drop_cols(qp->div,
1209 2 + div_pos + i - skip, 1);
1212 reordering[div_pos + array[i].row] = div_pos + i - skip;
1215 qp->upoly = reorder(qp->upoly, reordering);
1217 if (!qp->upoly || !qp->div)
1231 isl_qpolynomial_free(qp);
1235 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1236 int *exp, int first)
1239 struct isl_upoly_rec *rec;
1241 if (isl_upoly_is_cst(up))
1244 if (up->var < first)
1247 if (exp[up->var - first] == up->var - first)
1250 up = isl_upoly_cow(up);
1254 up->var = exp[up->var - first] + first;
1256 rec = isl_upoly_as_rec(up);
1260 for (i = 0; i < rec->n; ++i) {
1261 rec->p[i] = expand(rec->p[i], exp, first);
1272 static __isl_give isl_qpolynomial *with_merged_divs(
1273 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1274 __isl_take isl_qpolynomial *qp2),
1275 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1279 isl_mat *div = NULL;
1281 qp1 = isl_qpolynomial_cow(qp1);
1282 qp2 = isl_qpolynomial_cow(qp2);
1287 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1288 qp1->div->n_col >= qp2->div->n_col, goto error);
1290 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1291 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1295 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1299 isl_mat_free(qp1->div);
1300 qp1->div = isl_mat_copy(div);
1301 isl_mat_free(qp2->div);
1302 qp2->div = isl_mat_copy(div);
1304 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1305 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1307 if (!qp1->upoly || !qp2->upoly)
1314 return fn(qp1, qp2);
1319 isl_qpolynomial_free(qp1);
1320 isl_qpolynomial_free(qp2);
1324 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1325 __isl_take isl_qpolynomial *qp2)
1327 qp1 = isl_qpolynomial_cow(qp1);
1332 if (qp1->div->n_row < qp2->div->n_row)
1333 return isl_qpolynomial_add(qp2, qp1);
1335 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1336 if (!compatible_divs(qp1->div, qp2->div))
1337 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1339 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1343 isl_qpolynomial_free(qp2);
1347 isl_qpolynomial_free(qp1);
1348 isl_qpolynomial_free(qp2);
1352 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1353 __isl_keep isl_set *dom,
1354 __isl_take isl_qpolynomial *qp1,
1355 __isl_take isl_qpolynomial *qp2)
1357 qp1 = isl_qpolynomial_add(qp1, qp2);
1358 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1362 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1363 __isl_take isl_qpolynomial *qp2)
1365 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1368 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1369 __isl_take isl_qpolynomial *qp, isl_int v)
1371 if (isl_int_is_zero(v))
1374 qp = isl_qpolynomial_cow(qp);
1378 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1384 isl_qpolynomial_free(qp);
1389 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1394 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1397 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1398 __isl_take isl_qpolynomial *qp, isl_int v)
1400 if (isl_int_is_one(v))
1403 if (qp && isl_int_is_zero(v)) {
1404 isl_qpolynomial *zero;
1405 zero = isl_qpolynomial_zero(isl_space_copy(qp->dim));
1406 isl_qpolynomial_free(qp);
1410 qp = isl_qpolynomial_cow(qp);
1414 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1420 isl_qpolynomial_free(qp);
1424 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1425 __isl_take isl_qpolynomial *qp, isl_int v)
1427 return isl_qpolynomial_mul_isl_int(qp, v);
1430 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1431 __isl_take isl_qpolynomial *qp2)
1433 qp1 = isl_qpolynomial_cow(qp1);
1438 if (qp1->div->n_row < qp2->div->n_row)
1439 return isl_qpolynomial_mul(qp2, qp1);
1441 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1442 if (!compatible_divs(qp1->div, qp2->div))
1443 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1445 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1449 isl_qpolynomial_free(qp2);
1453 isl_qpolynomial_free(qp1);
1454 isl_qpolynomial_free(qp2);
1458 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1461 qp = isl_qpolynomial_cow(qp);
1466 qp->upoly = isl_upoly_pow(qp->upoly, power);
1472 isl_qpolynomial_free(qp);
1476 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1477 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1484 pwqp = isl_pw_qpolynomial_cow(pwqp);
1488 for (i = 0; i < pwqp->n; ++i) {
1489 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1491 return isl_pw_qpolynomial_free(pwqp);
1497 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_space *dim)
1501 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1504 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_space *dim)
1508 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1511 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_space *dim)
1515 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1518 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_space *dim)
1522 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1525 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_space *dim)
1529 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1532 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_space *dim,
1535 struct isl_qpolynomial *qp;
1536 struct isl_upoly_cst *cst;
1541 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1545 cst = isl_upoly_as_cst(qp->upoly);
1546 isl_int_set(cst->n, v);
1551 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1552 isl_int *n, isl_int *d)
1554 struct isl_upoly_cst *cst;
1559 if (!isl_upoly_is_cst(qp->upoly))
1562 cst = isl_upoly_as_cst(qp->upoly);
1567 isl_int_set(*n, cst->n);
1569 isl_int_set(*d, cst->d);
1574 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1577 struct isl_upoly_rec *rec;
1585 rec = isl_upoly_as_rec(up);
1592 isl_assert(up->ctx, rec->n > 1, return -1);
1594 is_cst = isl_upoly_is_cst(rec->p[1]);
1600 return isl_upoly_is_affine(rec->p[0]);
1603 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1608 if (qp->div->n_row > 0)
1611 return isl_upoly_is_affine(qp->upoly);
1614 static void update_coeff(__isl_keep isl_vec *aff,
1615 __isl_keep struct isl_upoly_cst *cst, int pos)
1620 if (isl_int_is_zero(cst->n))
1625 isl_int_gcd(gcd, cst->d, aff->el[0]);
1626 isl_int_divexact(f, cst->d, gcd);
1627 isl_int_divexact(gcd, aff->el[0], gcd);
1628 isl_seq_scale(aff->el, aff->el, f, aff->size);
1629 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1634 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1635 __isl_keep isl_vec *aff)
1637 struct isl_upoly_cst *cst;
1638 struct isl_upoly_rec *rec;
1644 struct isl_upoly_cst *cst;
1646 cst = isl_upoly_as_cst(up);
1649 update_coeff(aff, cst, 0);
1653 rec = isl_upoly_as_rec(up);
1656 isl_assert(up->ctx, rec->n == 2, return -1);
1658 cst = isl_upoly_as_cst(rec->p[1]);
1661 update_coeff(aff, cst, 1 + up->var);
1663 return isl_upoly_update_affine(rec->p[0], aff);
1666 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1667 __isl_keep isl_qpolynomial *qp)
1675 d = isl_space_dim(qp->dim, isl_dim_all);
1676 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1680 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1681 isl_int_set_si(aff->el[0], 1);
1683 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1692 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
1693 __isl_keep isl_qpolynomial *qp2)
1700 equal = isl_space_is_equal(qp1->dim, qp2->dim);
1701 if (equal < 0 || !equal)
1704 equal = isl_mat_is_equal(qp1->div, qp2->div);
1705 if (equal < 0 || !equal)
1708 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1711 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1714 struct isl_upoly_rec *rec;
1716 if (isl_upoly_is_cst(up)) {
1717 struct isl_upoly_cst *cst;
1718 cst = isl_upoly_as_cst(up);
1721 isl_int_lcm(*d, *d, cst->d);
1725 rec = isl_upoly_as_rec(up);
1729 for (i = 0; i < rec->n; ++i)
1730 upoly_update_den(rec->p[i], d);
1733 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1735 isl_int_set_si(*d, 1);
1738 upoly_update_den(qp->upoly, d);
1741 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_space *dim,
1744 struct isl_ctx *ctx;
1751 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1754 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_space *dim,
1755 enum isl_dim_type type, unsigned pos)
1760 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
1761 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
1763 if (type == isl_dim_set)
1764 pos += isl_space_dim(dim, isl_dim_param);
1766 return isl_qpolynomial_var_pow(dim, pos, 1);
1768 isl_space_free(dim);
1772 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1773 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1776 struct isl_upoly_rec *rec;
1777 struct isl_upoly *base, *res;
1782 if (isl_upoly_is_cst(up))
1785 if (up->var < first)
1788 rec = isl_upoly_as_rec(up);
1792 isl_assert(up->ctx, rec->n >= 1, goto error);
1794 if (up->var >= first + n)
1795 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1797 base = isl_upoly_copy(subs[up->var - first]);
1799 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1800 for (i = rec->n - 2; i >= 0; --i) {
1801 struct isl_upoly *t;
1802 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1803 res = isl_upoly_mul(res, isl_upoly_copy(base));
1804 res = isl_upoly_sum(res, t);
1807 isl_upoly_free(base);
1816 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1817 isl_int denom, unsigned len)
1820 struct isl_upoly *up;
1822 isl_assert(ctx, len >= 1, return NULL);
1824 up = isl_upoly_rat_cst(ctx, f[0], denom);
1825 for (i = 0; i < len - 1; ++i) {
1826 struct isl_upoly *t;
1827 struct isl_upoly *c;
1829 if (isl_int_is_zero(f[1 + i]))
1832 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1833 t = isl_upoly_var_pow(ctx, i, 1);
1834 t = isl_upoly_mul(c, t);
1835 up = isl_upoly_sum(up, t);
1841 /* Remove common factor of non-constant terms and denominator.
1843 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1845 isl_ctx *ctx = qp->div->ctx;
1846 unsigned total = qp->div->n_col - 2;
1848 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1849 isl_int_gcd(ctx->normalize_gcd,
1850 ctx->normalize_gcd, qp->div->row[div][0]);
1851 if (isl_int_is_one(ctx->normalize_gcd))
1854 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1855 ctx->normalize_gcd, total);
1856 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1857 ctx->normalize_gcd);
1858 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1859 ctx->normalize_gcd);
1862 /* Replace the integer division identified by "div" by the polynomial "s".
1863 * The integer division is assumed not to appear in the definition
1864 * of any other integer divisions.
1866 static __isl_give isl_qpolynomial *substitute_div(
1867 __isl_take isl_qpolynomial *qp,
1868 int div, __isl_take struct isl_upoly *s)
1877 qp = isl_qpolynomial_cow(qp);
1881 total = isl_space_dim(qp->dim, isl_dim_all);
1882 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1886 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1889 for (i = 0; i < total + div; ++i)
1891 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1892 reordering[i] = i - 1;
1893 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1894 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1895 qp->upoly = reorder(qp->upoly, reordering);
1898 if (!qp->upoly || !qp->div)
1904 isl_qpolynomial_free(qp);
1909 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1910 * divisions because d is equal to 1 by their definition, i.e., e.
1912 static __isl_give isl_qpolynomial *substitute_non_divs(
1913 __isl_take isl_qpolynomial *qp)
1917 struct isl_upoly *s;
1922 total = isl_space_dim(qp->dim, isl_dim_all);
1923 for (i = 0; qp && i < qp->div->n_row; ++i) {
1924 if (!isl_int_is_one(qp->div->row[i][0]))
1926 for (j = i + 1; j < qp->div->n_row; ++j) {
1927 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1929 isl_seq_combine(qp->div->row[j] + 1,
1930 qp->div->ctx->one, qp->div->row[j] + 1,
1931 qp->div->row[j][2 + total + i],
1932 qp->div->row[i] + 1, 1 + total + i);
1933 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1934 normalize_div(qp, j);
1936 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1937 qp->div->row[i][0], qp->div->n_col - 1);
1938 qp = substitute_div(qp, i, s);
1945 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1946 * with d the denominator. When replacing the coefficient e of x by
1947 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1948 * inside the division, so we need to add floor(e/d) * x outside.
1949 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1950 * to adjust the coefficient of x in each later div that depends on the
1951 * current div "div" and also in the affine expression "aff"
1952 * (if it too depends on "div").
1954 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1955 __isl_keep isl_vec *aff)
1959 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1962 for (i = 0; i < 1 + total + div; ++i) {
1963 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1964 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1966 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1967 isl_int_fdiv_r(qp->div->row[div][1 + i],
1968 qp->div->row[div][1 + i], qp->div->row[div][0]);
1969 if (!isl_int_is_zero(aff->el[1 + total + div]))
1970 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1971 for (j = div + 1; j < qp->div->n_row; ++j) {
1972 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1974 isl_int_addmul(qp->div->row[j][1 + i],
1975 v, qp->div->row[j][2 + total + div]);
1981 /* Check if the last non-zero coefficient is bigger that half of the
1982 * denominator. If so, we will invert the div to further reduce the number
1983 * of distinct divs that may appear.
1984 * If the last non-zero coefficient is exactly half the denominator,
1985 * then we continue looking for earlier coefficients that are bigger
1986 * than half the denominator.
1988 static int needs_invert(__isl_keep isl_mat *div, int row)
1993 for (i = div->n_col - 1; i >= 1; --i) {
1994 if (isl_int_is_zero(div->row[row][i]))
1996 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1997 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
1998 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2008 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2009 * We only invert the coefficients of e (and the coefficient of q in
2010 * later divs and in "aff"). After calling this function, the
2011 * coefficients of e should be reduced again.
2013 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2014 __isl_keep isl_vec *aff)
2016 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2018 isl_seq_neg(qp->div->row[div] + 1,
2019 qp->div->row[div] + 1, qp->div->n_col - 1);
2020 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2021 isl_int_add(qp->div->row[div][1],
2022 qp->div->row[div][1], qp->div->row[div][0]);
2023 if (!isl_int_is_zero(aff->el[1 + total + div]))
2024 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2025 isl_mat_col_mul(qp->div, 2 + total + div,
2026 qp->div->ctx->negone, 2 + total + div);
2029 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2030 * in the interval [0, d-1], with d the denominator and such that the
2031 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2033 * After the reduction, some divs may have become redundant or identical,
2034 * so we call substitute_non_divs and sort_divs. If these functions
2035 * eliminate divs or merge two or more divs into one, the coefficients
2036 * of the enclosing divs may have to be reduced again, so we call
2037 * ourselves recursively if the number of divs decreases.
2039 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2042 isl_vec *aff = NULL;
2043 struct isl_upoly *s;
2049 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2050 aff = isl_vec_clr(aff);
2054 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2056 for (i = 0; i < qp->div->n_row; ++i) {
2057 normalize_div(qp, i);
2058 reduce_div(qp, i, aff);
2059 if (needs_invert(qp->div, i)) {
2060 invert_div(qp, i, aff);
2061 reduce_div(qp, i, aff);
2065 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2066 qp->div->ctx->one, aff->size);
2067 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2074 n_div = qp->div->n_row;
2075 qp = substitute_non_divs(qp);
2077 if (qp && qp->div->n_row < n_div)
2078 return reduce_divs(qp);
2082 isl_qpolynomial_free(qp);
2087 /* Assumes each div only depends on earlier divs.
2089 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2092 struct isl_qpolynomial *qp = NULL;
2093 struct isl_upoly_rec *rec;
2094 struct isl_upoly_cst *cst;
2101 d = div->line - div->bmap->div;
2103 pos = isl_space_dim(div->bmap->dim, isl_dim_all) + d;
2104 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2105 qp = isl_qpolynomial_alloc(isl_basic_map_get_space(div->bmap),
2106 div->bmap->n_div, &rec->up);
2110 for (i = 0; i < div->bmap->n_div; ++i)
2111 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2113 for (i = 0; i < 1 + power; ++i) {
2114 rec->p[i] = isl_upoly_zero(div->ctx);
2119 cst = isl_upoly_as_cst(rec->p[power]);
2120 isl_int_set_si(cst->n, 1);
2124 qp = reduce_divs(qp);
2128 isl_qpolynomial_free(qp);
2133 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2135 return isl_qpolynomial_div_pow(div, 1);
2138 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_space *dim,
2139 const isl_int n, const isl_int d)
2141 struct isl_qpolynomial *qp;
2142 struct isl_upoly_cst *cst;
2147 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2151 cst = isl_upoly_as_cst(qp->upoly);
2152 isl_int_set(cst->n, n);
2153 isl_int_set(cst->d, d);
2158 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2160 struct isl_upoly_rec *rec;
2166 if (isl_upoly_is_cst(up))
2170 active[up->var] = 1;
2172 rec = isl_upoly_as_rec(up);
2173 for (i = 0; i < rec->n; ++i)
2174 if (up_set_active(rec->p[i], active, d) < 0)
2180 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2183 int d = isl_space_dim(qp->dim, isl_dim_all);
2188 for (i = 0; i < d; ++i)
2189 for (j = 0; j < qp->div->n_row; ++j) {
2190 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2196 return up_set_active(qp->upoly, active, d);
2199 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2200 enum isl_dim_type type, unsigned first, unsigned n)
2211 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2213 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2214 type == isl_dim_set, return -1);
2216 active = isl_calloc_array(qp->dim->ctx, int,
2217 isl_space_dim(qp->dim, isl_dim_all));
2218 if (set_active(qp, active) < 0)
2221 if (type == isl_dim_set)
2222 first += isl_space_dim(qp->dim, isl_dim_param);
2223 for (i = 0; i < n; ++i)
2224 if (active[first + i]) {
2237 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2238 * of the divs that do appear in the quasi-polynomial.
2240 static __isl_give isl_qpolynomial *remove_redundant_divs(
2241 __isl_take isl_qpolynomial *qp)
2248 int *reordering = NULL;
2255 if (qp->div->n_row == 0)
2258 d = isl_space_dim(qp->dim, isl_dim_all);
2259 len = qp->div->n_col - 2;
2260 ctx = isl_qpolynomial_get_ctx(qp);
2261 active = isl_calloc_array(ctx, int, len);
2265 if (up_set_active(qp->upoly, active, len) < 0)
2268 for (i = qp->div->n_row - 1; i >= 0; --i) {
2269 if (!active[d + i]) {
2273 for (j = 0; j < i; ++j) {
2274 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2286 reordering = isl_alloc_array(qp->div->ctx, int, len);
2290 for (i = 0; i < d; ++i)
2294 n_div = qp->div->n_row;
2295 for (i = 0; i < n_div; ++i) {
2296 if (!active[d + i]) {
2297 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2298 qp->div = isl_mat_drop_cols(qp->div,
2299 2 + d + i - skip, 1);
2302 reordering[d + i] = d + i - skip;
2305 qp->upoly = reorder(qp->upoly, reordering);
2307 if (!qp->upoly || !qp->div)
2317 isl_qpolynomial_free(qp);
2321 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2322 unsigned first, unsigned n)
2325 struct isl_upoly_rec *rec;
2329 if (n == 0 || up->var < 0 || up->var < first)
2331 if (up->var < first + n) {
2332 up = replace_by_constant_term(up);
2333 return isl_upoly_drop(up, first, n);
2335 up = isl_upoly_cow(up);
2339 rec = isl_upoly_as_rec(up);
2343 for (i = 0; i < rec->n; ++i) {
2344 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2355 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2356 __isl_take isl_qpolynomial *qp,
2357 enum isl_dim_type type, unsigned pos, const char *s)
2359 qp = isl_qpolynomial_cow(qp);
2362 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2367 isl_qpolynomial_free(qp);
2371 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2372 __isl_take isl_qpolynomial *qp,
2373 enum isl_dim_type type, unsigned first, unsigned n)
2377 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2380 qp = isl_qpolynomial_cow(qp);
2384 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2386 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2387 type == isl_dim_set, goto error);
2389 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2393 if (type == isl_dim_set)
2394 first += isl_space_dim(qp->dim, isl_dim_param);
2396 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2400 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2406 isl_qpolynomial_free(qp);
2410 /* Project the domain of the quasi-polynomial onto its parameter space.
2411 * The quasi-polynomial may not involve any of the domain dimensions.
2413 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2414 __isl_take isl_qpolynomial *qp)
2420 n = isl_qpolynomial_dim(qp, isl_dim_set);
2421 involves = isl_qpolynomial_involves_dims(qp, isl_dim_set, 0, n);
2423 return isl_qpolynomial_free(qp);
2425 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2426 "polynomial involves some of the domain dimensions",
2427 return isl_qpolynomial_free(qp));
2428 qp = isl_qpolynomial_drop_dims(qp, isl_dim_set, 0, n);
2429 space = isl_qpolynomial_get_space(qp);
2430 space = isl_space_params(space);
2431 qp = isl_qpolynomial_reset_space(qp, space);
2435 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2436 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2442 struct isl_upoly *up;
2446 if (eq->n_eq == 0) {
2447 isl_basic_set_free(eq);
2451 qp = isl_qpolynomial_cow(qp);
2454 qp->div = isl_mat_cow(qp->div);
2458 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2460 isl_int_init(denom);
2461 for (i = 0; i < eq->n_eq; ++i) {
2462 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2463 if (j < 0 || j == 0 || j >= total)
2466 for (k = 0; k < qp->div->n_row; ++k) {
2467 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2469 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2470 &qp->div->row[k][0]);
2471 normalize_div(qp, k);
2474 if (isl_int_is_pos(eq->eq[i][j]))
2475 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2476 isl_int_abs(denom, eq->eq[i][j]);
2477 isl_int_set_si(eq->eq[i][j], 0);
2479 up = isl_upoly_from_affine(qp->dim->ctx,
2480 eq->eq[i], denom, total);
2481 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2484 isl_int_clear(denom);
2489 isl_basic_set_free(eq);
2491 qp = substitute_non_divs(qp);
2496 isl_basic_set_free(eq);
2497 isl_qpolynomial_free(qp);
2501 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2503 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2504 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2508 if (qp->div->n_row > 0)
2509 eq = isl_basic_set_add(eq, isl_dim_set, qp->div->n_row);
2510 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2512 isl_basic_set_free(eq);
2513 isl_qpolynomial_free(qp);
2517 static __isl_give isl_basic_set *add_div_constraints(
2518 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2526 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2529 total = isl_basic_set_total_dim(bset);
2530 for (i = 0; i < div->n_row; ++i)
2531 if (isl_basic_set_add_div_constraints_var(bset,
2532 total - div->n_row + i, div->row[i]) < 0)
2539 isl_basic_set_free(bset);
2543 /* Look for equalities among the variables shared by context and qp
2544 * and the integer divisions of qp, if any.
2545 * The equalities are then used to eliminate variables and/or integer
2546 * divisions from qp.
2548 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2549 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2555 if (qp->div->n_row > 0) {
2556 isl_basic_set *bset;
2557 context = isl_set_add_dims(context, isl_dim_set,
2559 bset = isl_basic_set_universe(isl_set_get_space(context));
2560 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2561 context = isl_set_intersect(context,
2562 isl_set_from_basic_set(bset));
2565 aff = isl_set_affine_hull(context);
2566 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2568 isl_qpolynomial_free(qp);
2569 isl_set_free(context);
2573 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2574 __isl_take isl_qpolynomial *qp)
2580 if (isl_qpolynomial_is_zero(qp)) {
2581 isl_space *dim = isl_qpolynomial_get_space(qp);
2582 isl_qpolynomial_free(qp);
2583 return isl_pw_qpolynomial_zero(dim);
2586 dom = isl_set_universe(isl_qpolynomial_get_space(qp));
2587 return isl_pw_qpolynomial_alloc(dom, qp);
2591 #define PW isl_pw_qpolynomial
2593 #define EL isl_qpolynomial
2595 #define EL_IS_ZERO is_zero
2599 #define IS_ZERO is_zero
2603 #include <isl_pw_templ.c>
2606 #define UNION isl_union_pw_qpolynomial
2608 #define PART isl_pw_qpolynomial
2610 #define PARTS pw_qpolynomial
2612 #include <isl_union_templ.c>
2614 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2622 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2625 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2628 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2629 __isl_take isl_pw_qpolynomial *pwqp1,
2630 __isl_take isl_pw_qpolynomial *pwqp2)
2633 struct isl_pw_qpolynomial *res;
2635 if (!pwqp1 || !pwqp2)
2638 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
2641 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2642 isl_pw_qpolynomial_free(pwqp2);
2646 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2647 isl_pw_qpolynomial_free(pwqp1);
2651 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2652 isl_pw_qpolynomial_free(pwqp1);
2656 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2657 isl_pw_qpolynomial_free(pwqp2);
2661 n = pwqp1->n * pwqp2->n;
2662 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
2664 for (i = 0; i < pwqp1->n; ++i) {
2665 for (j = 0; j < pwqp2->n; ++j) {
2666 struct isl_set *common;
2667 struct isl_qpolynomial *prod;
2668 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2669 isl_set_copy(pwqp2->p[j].set));
2670 if (isl_set_plain_is_empty(common)) {
2671 isl_set_free(common);
2675 prod = isl_qpolynomial_mul(
2676 isl_qpolynomial_copy(pwqp1->p[i].qp),
2677 isl_qpolynomial_copy(pwqp2->p[j].qp));
2679 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2683 isl_pw_qpolynomial_free(pwqp1);
2684 isl_pw_qpolynomial_free(pwqp2);
2688 isl_pw_qpolynomial_free(pwqp1);
2689 isl_pw_qpolynomial_free(pwqp2);
2693 __isl_give struct isl_upoly *isl_upoly_eval(
2694 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2697 struct isl_upoly_rec *rec;
2698 struct isl_upoly *res;
2699 struct isl_upoly *base;
2701 if (isl_upoly_is_cst(up)) {
2706 rec = isl_upoly_as_rec(up);
2710 isl_assert(up->ctx, rec->n >= 1, goto error);
2712 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2714 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2717 for (i = rec->n - 2; i >= 0; --i) {
2718 res = isl_upoly_mul(res, isl_upoly_copy(base));
2719 res = isl_upoly_sum(res,
2720 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2721 isl_vec_copy(vec)));
2724 isl_upoly_free(base);
2734 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2735 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2738 struct isl_upoly *up;
2743 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
2745 if (qp->div->n_row == 0)
2746 ext = isl_vec_copy(pnt->vec);
2749 unsigned dim = isl_space_dim(qp->dim, isl_dim_all);
2750 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2754 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2755 for (i = 0; i < qp->div->n_row; ++i) {
2756 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2757 1 + dim + i, &ext->el[1+dim+i]);
2758 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2759 qp->div->row[i][0]);
2763 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2767 dim = isl_space_copy(qp->dim);
2768 isl_qpolynomial_free(qp);
2769 isl_point_free(pnt);
2771 return isl_qpolynomial_alloc(dim, 0, up);
2773 isl_qpolynomial_free(qp);
2774 isl_point_free(pnt);
2778 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2779 __isl_keep struct isl_upoly_cst *cst2)
2784 isl_int_mul(t, cst1->n, cst2->d);
2785 isl_int_submul(t, cst2->n, cst1->d);
2786 cmp = isl_int_sgn(t);
2791 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2792 __isl_keep isl_qpolynomial *qp2)
2794 struct isl_upoly_cst *cst1, *cst2;
2798 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2799 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2800 if (isl_qpolynomial_is_nan(qp1))
2802 if (isl_qpolynomial_is_nan(qp2))
2804 cst1 = isl_upoly_as_cst(qp1->upoly);
2805 cst2 = isl_upoly_as_cst(qp2->upoly);
2807 return isl_upoly_cmp(cst1, cst2) <= 0;
2810 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2811 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2813 struct isl_upoly_cst *cst1, *cst2;
2818 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2819 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2820 cst1 = isl_upoly_as_cst(qp1->upoly);
2821 cst2 = isl_upoly_as_cst(qp2->upoly);
2822 cmp = isl_upoly_cmp(cst1, cst2);
2825 isl_qpolynomial_free(qp2);
2827 isl_qpolynomial_free(qp1);
2832 isl_qpolynomial_free(qp1);
2833 isl_qpolynomial_free(qp2);
2837 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2838 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2840 struct isl_upoly_cst *cst1, *cst2;
2845 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2846 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2847 cst1 = isl_upoly_as_cst(qp1->upoly);
2848 cst2 = isl_upoly_as_cst(qp2->upoly);
2849 cmp = isl_upoly_cmp(cst1, cst2);
2852 isl_qpolynomial_free(qp2);
2854 isl_qpolynomial_free(qp1);
2859 isl_qpolynomial_free(qp1);
2860 isl_qpolynomial_free(qp2);
2864 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2865 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2866 unsigned first, unsigned n)
2872 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2875 qp = isl_qpolynomial_cow(qp);
2879 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
2882 g_pos = pos(qp->dim, type) + first;
2884 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
2888 total = qp->div->n_col - 2;
2889 if (total > g_pos) {
2891 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2894 for (i = 0; i < total - g_pos; ++i)
2896 qp->upoly = expand(qp->upoly, exp, g_pos);
2902 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
2908 isl_qpolynomial_free(qp);
2912 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2913 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2917 pos = isl_qpolynomial_dim(qp, type);
2919 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2922 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2923 __isl_take isl_pw_qpolynomial *pwqp,
2924 enum isl_dim_type type, unsigned n)
2928 pos = isl_pw_qpolynomial_dim(pwqp, type);
2930 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2933 static int *reordering_move(isl_ctx *ctx,
2934 unsigned len, unsigned dst, unsigned src, unsigned n)
2939 reordering = isl_alloc_array(ctx, int, len);
2944 for (i = 0; i < dst; ++i)
2946 for (i = 0; i < n; ++i)
2947 reordering[src + i] = dst + i;
2948 for (i = 0; i < src - dst; ++i)
2949 reordering[dst + i] = dst + n + i;
2950 for (i = 0; i < len - src - n; ++i)
2951 reordering[src + n + i] = src + n + i;
2953 for (i = 0; i < src; ++i)
2955 for (i = 0; i < n; ++i)
2956 reordering[src + i] = dst + i;
2957 for (i = 0; i < dst - src; ++i)
2958 reordering[src + n + i] = src + i;
2959 for (i = 0; i < len - dst - n; ++i)
2960 reordering[dst + n + i] = dst + n + i;
2966 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2967 __isl_take isl_qpolynomial *qp,
2968 enum isl_dim_type dst_type, unsigned dst_pos,
2969 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2975 qp = isl_qpolynomial_cow(qp);
2979 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
2982 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2983 g_src_pos = pos(qp->dim, src_type) + src_pos;
2984 if (dst_type > src_type)
2987 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2994 reordering = reordering_move(qp->dim->ctx,
2995 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2999 qp->upoly = reorder(qp->upoly, reordering);
3004 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3010 isl_qpolynomial_free(qp);
3014 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3015 isl_int *f, isl_int denom)
3017 struct isl_upoly *up;
3022 up = isl_upoly_from_affine(dim->ctx, f, denom,
3023 1 + isl_space_dim(dim, isl_dim_all));
3025 return isl_qpolynomial_alloc(dim, 0, up);
3028 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3031 struct isl_upoly *up;
3032 isl_qpolynomial *qp;
3037 ctx = isl_aff_get_ctx(aff);
3038 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3041 qp = isl_qpolynomial_alloc(isl_aff_get_space(aff),
3042 aff->ls->div->n_row, up);
3046 isl_mat_free(qp->div);
3047 qp->div = isl_mat_copy(aff->ls->div);
3048 qp->div = isl_mat_cow(qp->div);
3053 qp = reduce_divs(qp);
3054 qp = remove_redundant_divs(qp);
3061 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3062 __isl_take isl_pw_aff *pwaff)
3065 isl_pw_qpolynomial *pwqp;
3070 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3073 for (i = 0; i < pwaff->n; ++i) {
3075 isl_qpolynomial *qp;
3077 dom = isl_set_copy(pwaff->p[i].set);
3078 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3079 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3082 isl_pw_aff_free(pwaff);
3086 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3087 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3091 aff = isl_constraint_get_bound(c, type, pos);
3092 isl_constraint_free(c);
3093 return isl_qpolynomial_from_aff(aff);
3096 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3097 * in "qp" by subs[i].
3099 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3100 __isl_take isl_qpolynomial *qp,
3101 enum isl_dim_type type, unsigned first, unsigned n,
3102 __isl_keep isl_qpolynomial **subs)
3105 struct isl_upoly **ups;
3110 qp = isl_qpolynomial_cow(qp);
3113 for (i = 0; i < n; ++i)
3117 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3120 for (i = 0; i < n; ++i)
3121 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3124 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3125 for (i = 0; i < n; ++i)
3126 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3128 first += pos(qp->dim, type);
3130 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3133 for (i = 0; i < n; ++i)
3134 ups[i] = subs[i]->upoly;
3136 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3145 isl_qpolynomial_free(qp);
3149 /* Extend "bset" with extra set dimensions for each integer division
3150 * in "qp" and then call "fn" with the extended bset and the polynomial
3151 * that results from replacing each of the integer divisions by the
3152 * corresponding extra set dimension.
3154 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3155 __isl_keep isl_basic_set *bset,
3156 int (*fn)(__isl_take isl_basic_set *bset,
3157 __isl_take isl_qpolynomial *poly, void *user), void *user)
3161 isl_qpolynomial *poly;
3165 if (qp->div->n_row == 0)
3166 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3169 div = isl_mat_copy(qp->div);
3170 dim = isl_space_copy(qp->dim);
3171 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3172 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3173 bset = isl_basic_set_copy(bset);
3174 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3175 bset = add_div_constraints(bset, div);
3177 return fn(bset, poly, user);
3182 /* Return total degree in variables first (inclusive) up to last (exclusive).
3184 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3188 struct isl_upoly_rec *rec;
3192 if (isl_upoly_is_zero(up))
3194 if (isl_upoly_is_cst(up) || up->var < first)
3197 rec = isl_upoly_as_rec(up);
3201 for (i = 0; i < rec->n; ++i) {
3204 if (isl_upoly_is_zero(rec->p[i]))
3206 d = isl_upoly_degree(rec->p[i], first, last);
3216 /* Return total degree in set variables.
3218 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3226 ovar = isl_space_offset(poly->dim, isl_dim_set);
3227 nvar = isl_space_dim(poly->dim, isl_dim_set);
3228 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3231 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3232 unsigned pos, int deg)
3235 struct isl_upoly_rec *rec;
3240 if (isl_upoly_is_cst(up) || up->var < pos) {
3242 return isl_upoly_copy(up);
3244 return isl_upoly_zero(up->ctx);
3247 rec = isl_upoly_as_rec(up);
3251 if (up->var == pos) {
3253 return isl_upoly_copy(rec->p[deg]);
3255 return isl_upoly_zero(up->ctx);
3258 up = isl_upoly_copy(up);
3259 up = isl_upoly_cow(up);
3260 rec = isl_upoly_as_rec(up);
3264 for (i = 0; i < rec->n; ++i) {
3265 struct isl_upoly *t;
3266 t = isl_upoly_coeff(rec->p[i], pos, deg);
3269 isl_upoly_free(rec->p[i]);
3279 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3281 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3282 __isl_keep isl_qpolynomial *qp,
3283 enum isl_dim_type type, unsigned t_pos, int deg)
3286 struct isl_upoly *up;
3292 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3295 g_pos = pos(qp->dim, type) + t_pos;
3296 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3298 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3301 isl_mat_free(c->div);
3302 c->div = isl_mat_copy(qp->div);
3307 isl_qpolynomial_free(c);
3311 /* Homogenize the polynomial in the variables first (inclusive) up to
3312 * last (exclusive) by inserting powers of variable first.
3313 * Variable first is assumed not to appear in the input.
3315 __isl_give struct isl_upoly *isl_upoly_homogenize(
3316 __isl_take struct isl_upoly *up, int deg, int target,
3317 int first, int last)
3320 struct isl_upoly_rec *rec;
3324 if (isl_upoly_is_zero(up))
3328 if (isl_upoly_is_cst(up) || up->var < first) {
3329 struct isl_upoly *hom;
3331 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3334 rec = isl_upoly_as_rec(hom);
3335 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3340 up = isl_upoly_cow(up);
3341 rec = isl_upoly_as_rec(up);
3345 for (i = 0; i < rec->n; ++i) {
3346 if (isl_upoly_is_zero(rec->p[i]))
3348 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3349 up->var < last ? deg + i : i, target,
3361 /* Homogenize the polynomial in the set variables by introducing
3362 * powers of an extra set variable at position 0.
3364 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3365 __isl_take isl_qpolynomial *poly)
3369 int deg = isl_qpolynomial_degree(poly);
3374 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3375 poly = isl_qpolynomial_cow(poly);
3379 ovar = isl_space_offset(poly->dim, isl_dim_set);
3380 nvar = isl_space_dim(poly->dim, isl_dim_set);
3381 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3388 isl_qpolynomial_free(poly);
3392 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3393 __isl_take isl_mat *div)
3401 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3403 term = isl_calloc(dim->ctx, struct isl_term,
3404 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3411 isl_int_init(term->n);
3412 isl_int_init(term->d);
3416 isl_space_free(dim);
3421 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3430 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3439 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3441 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3445 isl_int_set(dup->n, term->n);
3446 isl_int_set(dup->d, term->d);
3448 for (i = 0; i < total; ++i)
3449 dup->pow[i] = term->pow[i];
3454 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3462 return isl_term_dup(term);
3465 void isl_term_free(__isl_take isl_term *term)
3470 if (--term->ref > 0)
3473 isl_space_free(term->dim);
3474 isl_mat_free(term->div);
3475 isl_int_clear(term->n);
3476 isl_int_clear(term->d);
3480 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3488 case isl_dim_out: return isl_space_dim(term->dim, type);
3489 case isl_dim_div: return term->div->n_row;
3490 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3496 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3498 return term ? term->dim->ctx : NULL;
3501 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3505 isl_int_set(*n, term->n);
3508 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3512 isl_int_set(*d, term->d);
3515 int isl_term_get_exp(__isl_keep isl_term *term,
3516 enum isl_dim_type type, unsigned pos)
3521 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3523 if (type >= isl_dim_set)
3524 pos += isl_space_dim(term->dim, isl_dim_param);
3525 if (type >= isl_dim_div)
3526 pos += isl_space_dim(term->dim, isl_dim_set);
3528 return term->pow[pos];
3531 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3533 isl_basic_map *bmap;
3540 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3543 total = term->div->n_col - term->div->n_row - 2;
3544 /* No nested divs for now */
3545 isl_assert(term->dim->ctx,
3546 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3547 term->div->n_row) == -1,
3550 bmap = isl_basic_map_alloc_space(isl_space_copy(term->dim), 1, 0, 0);
3551 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3554 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3556 return isl_basic_map_div(bmap, k);
3558 isl_basic_map_free(bmap);
3562 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3563 int (*fn)(__isl_take isl_term *term, void *user),
3564 __isl_take isl_term *term, void *user)
3567 struct isl_upoly_rec *rec;
3572 if (isl_upoly_is_zero(up))
3575 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3576 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3577 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3579 if (isl_upoly_is_cst(up)) {
3580 struct isl_upoly_cst *cst;
3581 cst = isl_upoly_as_cst(up);
3584 term = isl_term_cow(term);
3587 isl_int_set(term->n, cst->n);
3588 isl_int_set(term->d, cst->d);
3589 if (fn(isl_term_copy(term), user) < 0)
3594 rec = isl_upoly_as_rec(up);
3598 for (i = 0; i < rec->n; ++i) {
3599 term = isl_term_cow(term);
3602 term->pow[up->var] = i;
3603 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3607 term->pow[up->var] = 0;
3611 isl_term_free(term);
3615 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3616 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3623 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3627 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3629 isl_term_free(term);
3631 return term ? 0 : -1;
3634 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3636 struct isl_upoly *up;
3637 isl_qpolynomial *qp;
3643 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3645 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3646 for (i = 0; i < n; ++i) {
3649 up = isl_upoly_mul(up,
3650 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3653 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3656 isl_mat_free(qp->div);
3657 qp->div = isl_mat_copy(term->div);
3661 isl_term_free(term);
3664 isl_qpolynomial_free(qp);
3665 isl_term_free(term);
3669 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3670 __isl_take isl_space *dim)
3679 if (isl_space_is_equal(qp->dim, dim)) {
3680 isl_space_free(dim);
3684 qp = isl_qpolynomial_cow(qp);
3688 extra = isl_space_dim(dim, isl_dim_set) -
3689 isl_space_dim(qp->dim, isl_dim_set);
3690 total = isl_space_dim(qp->dim, isl_dim_all);
3691 if (qp->div->n_row) {
3694 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3697 for (i = 0; i < qp->div->n_row; ++i)
3699 qp->upoly = expand(qp->upoly, exp, total);
3704 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3707 for (i = 0; i < qp->div->n_row; ++i)
3708 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3710 isl_space_free(qp->dim);
3715 isl_space_free(dim);
3716 isl_qpolynomial_free(qp);
3720 /* For each parameter or variable that does not appear in qp,
3721 * first eliminate the variable from all constraints and then set it to zero.
3723 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3724 __isl_keep isl_qpolynomial *qp)
3735 d = isl_space_dim(set->dim, isl_dim_all);
3736 active = isl_calloc_array(set->ctx, int, d);
3737 if (set_active(qp, active) < 0)
3740 for (i = 0; i < d; ++i)
3749 nparam = isl_space_dim(set->dim, isl_dim_param);
3750 nvar = isl_space_dim(set->dim, isl_dim_set);
3751 for (i = 0; i < nparam; ++i) {
3754 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3755 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3757 for (i = 0; i < nvar; ++i) {
3758 if (active[nparam + i])
3760 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3761 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3773 struct isl_opt_data {
3774 isl_qpolynomial *qp;
3776 isl_qpolynomial *opt;
3780 static int opt_fn(__isl_take isl_point *pnt, void *user)
3782 struct isl_opt_data *data = (struct isl_opt_data *)user;
3783 isl_qpolynomial *val;
3785 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3789 } else if (data->max) {
3790 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3792 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3798 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3799 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3801 struct isl_opt_data data = { NULL, 1, NULL, max };
3806 if (isl_upoly_is_cst(qp->upoly)) {
3811 set = fix_inactive(set, qp);
3814 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3818 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_space(qp));
3821 isl_qpolynomial_free(qp);
3825 isl_qpolynomial_free(qp);
3826 isl_qpolynomial_free(data.opt);
3830 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3831 __isl_take isl_morph *morph)
3836 struct isl_upoly **subs;
3839 qp = isl_qpolynomial_cow(qp);
3844 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
3846 n_sub = morph->inv->n_row - 1;
3847 if (morph->inv->n_row != morph->inv->n_col)
3848 n_sub += qp->div->n_row;
3849 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3853 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3854 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3855 morph->inv->row[0][0], morph->inv->n_col);
3856 if (morph->inv->n_row != morph->inv->n_col)
3857 for (i = 0; i < qp->div->n_row; ++i)
3858 subs[morph->inv->n_row - 1 + i] =
3859 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3861 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3863 for (i = 0; i < n_sub; ++i)
3864 isl_upoly_free(subs[i]);
3867 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3868 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3869 qp->div = isl_mat_product(qp->div, mat);
3870 isl_space_free(qp->dim);
3871 qp->dim = isl_space_copy(morph->ran->dim);
3873 if (!qp->upoly || !qp->div || !qp->dim)
3876 isl_morph_free(morph);
3880 isl_qpolynomial_free(qp);
3881 isl_morph_free(morph);
3885 static int neg_entry(void **entry, void *user)
3887 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3889 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3891 return *pwqp ? 0 : -1;
3894 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3895 __isl_take isl_union_pw_qpolynomial *upwqp)
3897 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3901 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3902 &neg_entry, NULL) < 0)
3907 isl_union_pw_qpolynomial_free(upwqp);
3911 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3912 __isl_take isl_union_pw_qpolynomial *upwqp1,
3913 __isl_take isl_union_pw_qpolynomial *upwqp2)
3915 return isl_union_pw_qpolynomial_add(upwqp1,
3916 isl_union_pw_qpolynomial_neg(upwqp2));
3919 static int mul_entry(void **entry, void *user)
3921 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3923 struct isl_hash_table_entry *entry2;
3924 isl_pw_qpolynomial *pwpq = *entry;
3927 hash = isl_space_get_hash(pwpq->dim);
3928 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3929 hash, &has_dim, pwpq->dim, 0);
3933 pwpq = isl_pw_qpolynomial_copy(pwpq);
3934 pwpq = isl_pw_qpolynomial_mul(pwpq,
3935 isl_pw_qpolynomial_copy(entry2->data));
3937 empty = isl_pw_qpolynomial_is_zero(pwpq);
3939 isl_pw_qpolynomial_free(pwpq);
3943 isl_pw_qpolynomial_free(pwpq);
3947 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3952 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3953 __isl_take isl_union_pw_qpolynomial *upwqp1,
3954 __isl_take isl_union_pw_qpolynomial *upwqp2)
3956 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3959 /* Reorder the columns of the given div definitions according to the
3962 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3963 __isl_take isl_reordering *r)
3972 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
3973 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3977 for (i = 0; i < div->n_row; ++i) {
3978 isl_seq_cpy(mat->row[i], div->row[i], 2);
3979 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3980 for (j = 0; j < r->len; ++j)
3981 isl_int_set(mat->row[i][2 + r->pos[j]],
3982 div->row[i][2 + j]);
3985 isl_reordering_free(r);
3989 isl_reordering_free(r);
3994 /* Reorder the dimension of "qp" according to the given reordering.
3996 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3997 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3999 qp = isl_qpolynomial_cow(qp);
4003 r = isl_reordering_extend(r, qp->div->n_row);
4007 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
4011 qp->upoly = reorder(qp->upoly, r->pos);
4015 qp = isl_qpolynomial_reset_space(qp, isl_space_copy(r->dim));
4017 isl_reordering_free(r);
4020 isl_qpolynomial_free(qp);
4021 isl_reordering_free(r);
4025 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4026 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4031 if (!isl_space_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
4032 isl_reordering *exp;
4034 model = isl_space_drop_dims(model, isl_dim_in,
4035 0, isl_space_dim(model, isl_dim_in));
4036 model = isl_space_drop_dims(model, isl_dim_out,
4037 0, isl_space_dim(model, isl_dim_out));
4038 exp = isl_parameter_alignment_reordering(qp->dim, model);
4039 exp = isl_reordering_extend_space(exp,
4040 isl_qpolynomial_get_space(qp));
4041 qp = isl_qpolynomial_realign(qp, exp);
4044 isl_space_free(model);
4047 isl_space_free(model);
4048 isl_qpolynomial_free(qp);
4052 struct isl_split_periods_data {
4054 isl_pw_qpolynomial *res;
4057 /* Create a slice where the integer division "div" has the fixed value "v".
4058 * In particular, if "div" refers to floor(f/m), then create a slice
4060 * m v <= f <= m v + (m - 1)
4065 * -f + m v + (m - 1) >= 0
4067 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4068 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4071 isl_basic_set *bset = NULL;
4077 total = isl_space_dim(dim, isl_dim_all);
4078 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4080 k = isl_basic_set_alloc_inequality(bset);
4083 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4084 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4086 k = isl_basic_set_alloc_inequality(bset);
4089 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4090 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4091 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4092 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4094 isl_space_free(dim);
4095 return isl_set_from_basic_set(bset);
4097 isl_basic_set_free(bset);
4098 isl_space_free(dim);
4102 static int split_periods(__isl_take isl_set *set,
4103 __isl_take isl_qpolynomial *qp, void *user);
4105 /* Create a slice of the domain "set" such that integer division "div"
4106 * has the fixed value "v" and add the results to data->res,
4107 * replacing the integer division by "v" in "qp".
4109 static int set_div(__isl_take isl_set *set,
4110 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4111 struct isl_split_periods_data *data)
4116 struct isl_upoly *cst;
4118 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4119 set = isl_set_intersect(set, slice);
4124 total = isl_space_dim(qp->dim, isl_dim_all);
4126 for (i = div + 1; i < qp->div->n_row; ++i) {
4127 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4129 isl_int_addmul(qp->div->row[i][1],
4130 qp->div->row[i][2 + total + div], v);
4131 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4134 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4135 qp = substitute_div(qp, div, cst);
4137 return split_periods(set, qp, data);
4140 isl_qpolynomial_free(qp);
4144 /* Split the domain "set" such that integer division "div"
4145 * has a fixed value (ranging from "min" to "max") on each slice
4146 * and add the results to data->res.
4148 static int split_div(__isl_take isl_set *set,
4149 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4150 struct isl_split_periods_data *data)
4152 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4153 isl_set *set_i = isl_set_copy(set);
4154 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4156 if (set_div(set_i, qp_i, div, min, data) < 0)
4160 isl_qpolynomial_free(qp);
4164 isl_qpolynomial_free(qp);
4168 /* If "qp" refers to any integer division
4169 * that can only attain "max_periods" distinct values on "set"
4170 * then split the domain along those distinct values.
4171 * Add the results (or the original if no splitting occurs)
4174 static int split_periods(__isl_take isl_set *set,
4175 __isl_take isl_qpolynomial *qp, void *user)
4178 isl_pw_qpolynomial *pwqp;
4179 struct isl_split_periods_data *data;
4184 data = (struct isl_split_periods_data *)user;
4189 if (qp->div->n_row == 0) {
4190 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4191 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4197 total = isl_space_dim(qp->dim, isl_dim_all);
4198 for (i = 0; i < qp->div->n_row; ++i) {
4199 enum isl_lp_result lp_res;
4201 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4202 qp->div->n_row) != -1)
4205 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4206 set->ctx->one, &min, NULL, NULL);
4207 if (lp_res == isl_lp_error)
4209 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4211 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4213 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4214 set->ctx->one, &max, NULL, NULL);
4215 if (lp_res == isl_lp_error)
4217 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4219 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4221 isl_int_sub(max, max, min);
4222 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4223 isl_int_add(max, max, min);
4228 if (i < qp->div->n_row) {
4229 r = split_div(set, qp, i, min, max, data);
4231 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4232 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4244 isl_qpolynomial_free(qp);
4248 /* If any quasi-polynomial in pwqp refers to any integer division
4249 * that can only attain "max_periods" distinct values on its domain
4250 * then split the domain along those distinct values.
4252 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4253 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4255 struct isl_split_periods_data data;
4257 data.max_periods = max_periods;
4258 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4260 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4263 isl_pw_qpolynomial_free(pwqp);
4267 isl_pw_qpolynomial_free(data.res);
4268 isl_pw_qpolynomial_free(pwqp);
4272 /* Construct a piecewise quasipolynomial that is constant on the given
4273 * domain. In particular, it is
4276 * infinity if cst == -1
4278 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4279 __isl_take isl_basic_set *bset, int cst)
4282 isl_qpolynomial *qp;
4287 bset = isl_basic_set_params(bset);
4288 dim = isl_basic_set_get_space(bset);
4290 qp = isl_qpolynomial_infty(dim);
4292 qp = isl_qpolynomial_zero(dim);
4294 qp = isl_qpolynomial_one(dim);
4295 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4298 /* Factor bset, call fn on each of the factors and return the product.
4300 * If no factors can be found, simply call fn on the input.
4301 * Otherwise, construct the factors based on the factorizer,
4302 * call fn on each factor and compute the product.
4304 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4305 __isl_take isl_basic_set *bset,
4306 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4312 isl_qpolynomial *qp;
4313 isl_pw_qpolynomial *pwqp;
4317 f = isl_basic_set_factorizer(bset);
4320 if (f->n_group == 0) {
4321 isl_factorizer_free(f);
4325 nparam = isl_basic_set_dim(bset, isl_dim_param);
4326 nvar = isl_basic_set_dim(bset, isl_dim_set);
4328 dim = isl_basic_set_get_space(bset);
4329 dim = isl_space_domain(dim);
4330 set = isl_set_universe(isl_space_copy(dim));
4331 qp = isl_qpolynomial_one(dim);
4332 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4334 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4336 for (i = 0, n = 0; i < f->n_group; ++i) {
4337 isl_basic_set *bset_i;
4338 isl_pw_qpolynomial *pwqp_i;
4340 bset_i = isl_basic_set_copy(bset);
4341 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4342 nparam + n + f->len[i], nvar - n - f->len[i]);
4343 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4345 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4346 n + f->len[i], nvar - n - f->len[i]);
4347 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4349 pwqp_i = fn(bset_i);
4350 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4355 isl_basic_set_free(bset);
4356 isl_factorizer_free(f);
4360 isl_basic_set_free(bset);
4364 /* Factor bset, call fn on each of the factors and return the product.
4365 * The function is assumed to evaluate to zero on empty domains,
4366 * to one on zero-dimensional domains and to infinity on unbounded domains
4367 * and will not be called explicitly on zero-dimensional or unbounded domains.
4369 * We first check for some special cases and remove all equalities.
4370 * Then we hand over control to compressed_multiplicative_call.
4372 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4373 __isl_take isl_basic_set *bset,
4374 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4378 isl_pw_qpolynomial *pwqp;
4383 if (isl_basic_set_plain_is_empty(bset))
4384 return constant_on_domain(bset, 0);
4386 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4387 return constant_on_domain(bset, 1);
4389 bounded = isl_basic_set_is_bounded(bset);
4393 return constant_on_domain(bset, -1);
4395 if (bset->n_eq == 0)
4396 return compressed_multiplicative_call(bset, fn);
4398 morph = isl_basic_set_full_compression(bset);
4399 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4401 pwqp = compressed_multiplicative_call(bset, fn);
4403 morph = isl_morph_dom_params(morph);
4404 morph = isl_morph_ran_params(morph);
4405 morph = isl_morph_inverse(morph);
4407 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4411 isl_basic_set_free(bset);
4415 /* Drop all floors in "qp", turning each integer division [a/m] into
4416 * a rational division a/m. If "down" is set, then the integer division
4417 * is replaces by (a-(m-1))/m instead.
4419 static __isl_give isl_qpolynomial *qp_drop_floors(
4420 __isl_take isl_qpolynomial *qp, int down)
4423 struct isl_upoly *s;
4427 if (qp->div->n_row == 0)
4430 qp = isl_qpolynomial_cow(qp);
4434 for (i = qp->div->n_row - 1; i >= 0; --i) {
4436 isl_int_sub(qp->div->row[i][1],
4437 qp->div->row[i][1], qp->div->row[i][0]);
4438 isl_int_add_ui(qp->div->row[i][1],
4439 qp->div->row[i][1], 1);
4441 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4442 qp->div->row[i][0], qp->div->n_col - 1);
4443 qp = substitute_div(qp, i, s);
4451 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4452 * a rational division a/m.
4454 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4455 __isl_take isl_pw_qpolynomial *pwqp)
4462 if (isl_pw_qpolynomial_is_zero(pwqp))
4465 pwqp = isl_pw_qpolynomial_cow(pwqp);
4469 for (i = 0; i < pwqp->n; ++i) {
4470 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4477 isl_pw_qpolynomial_free(pwqp);
4481 /* Adjust all the integer divisions in "qp" such that they are at least
4482 * one over the given orthant (identified by "signs"). This ensures
4483 * that they will still be non-negative even after subtracting (m-1)/m.
4485 * In particular, f is replaced by f' + v, changing f = [a/m]
4486 * to f' = [(a - m v)/m].
4487 * If the constant term k in a is smaller than m,
4488 * the constant term of v is set to floor(k/m) - 1.
4489 * For any other term, if the coefficient c and the variable x have
4490 * the same sign, then no changes are needed.
4491 * Otherwise, if the variable is positive (and c is negative),
4492 * then the coefficient of x in v is set to floor(c/m).
4493 * If the variable is negative (and c is positive),
4494 * then the coefficient of x in v is set to ceil(c/m).
4496 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4502 struct isl_upoly *s;
4504 qp = isl_qpolynomial_cow(qp);
4507 qp->div = isl_mat_cow(qp->div);
4511 total = isl_space_dim(qp->dim, isl_dim_all);
4512 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4514 for (i = 0; i < qp->div->n_row; ++i) {
4515 isl_int *row = qp->div->row[i];
4519 if (isl_int_lt(row[1], row[0])) {
4520 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4521 isl_int_sub_ui(v->el[0], v->el[0], 1);
4522 isl_int_submul(row[1], row[0], v->el[0]);
4524 for (j = 0; j < total; ++j) {
4525 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4528 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4530 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4531 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4533 for (j = 0; j < i; ++j) {
4534 if (isl_int_sgn(row[2 + total + j]) >= 0)
4536 isl_int_fdiv_q(v->el[1 + total + j],
4537 row[2 + total + j], row[0]);
4538 isl_int_submul(row[2 + total + j],
4539 row[0], v->el[1 + total + j]);
4541 for (j = i + 1; j < qp->div->n_row; ++j) {
4542 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4544 isl_seq_combine(qp->div->row[j] + 1,
4545 qp->div->ctx->one, qp->div->row[j] + 1,
4546 qp->div->row[j][2 + total + i], v->el, v->size);
4548 isl_int_set_si(v->el[1 + total + i], 1);
4549 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4550 qp->div->ctx->one, v->size);
4551 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4561 isl_qpolynomial_free(qp);
4565 struct isl_to_poly_data {
4567 isl_pw_qpolynomial *res;
4568 isl_qpolynomial *qp;
4571 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4572 * We first make all integer divisions positive and then split the
4573 * quasipolynomials into terms with sign data->sign (the direction
4574 * of the requested approximation) and terms with the opposite sign.
4575 * In the first set of terms, each integer division [a/m] is
4576 * overapproximated by a/m, while in the second it is underapproximated
4579 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4582 struct isl_to_poly_data *data = user;
4583 isl_pw_qpolynomial *t;
4584 isl_qpolynomial *qp, *up, *down;
4586 qp = isl_qpolynomial_copy(data->qp);
4587 qp = make_divs_pos(qp, signs);
4589 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4590 up = qp_drop_floors(up, 0);
4591 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4592 down = qp_drop_floors(down, 1);
4594 isl_qpolynomial_free(qp);
4595 qp = isl_qpolynomial_add(up, down);
4597 t = isl_pw_qpolynomial_alloc(orthant, qp);
4598 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4603 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4604 * the polynomial will be an overapproximation. If "sign" is negative,
4605 * it will be an underapproximation. If "sign" is zero, the approximation
4606 * will lie somewhere in between.
4608 * In particular, is sign == 0, we simply drop the floors, turning
4609 * the integer divisions into rational divisions.
4610 * Otherwise, we split the domains into orthants, make all integer divisions
4611 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4612 * depending on the requested sign and the sign of the term in which
4613 * the integer division appears.
4615 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4616 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4619 struct isl_to_poly_data data;
4622 return pwqp_drop_floors(pwqp);
4628 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4630 for (i = 0; i < pwqp->n; ++i) {
4631 if (pwqp->p[i].qp->div->n_row == 0) {
4632 isl_pw_qpolynomial *t;
4633 t = isl_pw_qpolynomial_alloc(
4634 isl_set_copy(pwqp->p[i].set),
4635 isl_qpolynomial_copy(pwqp->p[i].qp));
4636 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4639 data.qp = pwqp->p[i].qp;
4640 if (isl_set_foreach_orthant(pwqp->p[i].set,
4641 &to_polynomial_on_orthant, &data) < 0)
4645 isl_pw_qpolynomial_free(pwqp);
4649 isl_pw_qpolynomial_free(pwqp);
4650 isl_pw_qpolynomial_free(data.res);
4654 static int poly_entry(void **entry, void *user)
4657 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4659 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4661 return *pwqp ? 0 : -1;
4664 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4665 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4667 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4671 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4672 &poly_entry, &sign) < 0)
4677 isl_union_pw_qpolynomial_free(upwqp);
4681 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4682 __isl_take isl_qpolynomial *qp)
4686 isl_vec *aff = NULL;
4687 isl_basic_map *bmap = NULL;
4693 if (!isl_upoly_is_affine(qp->upoly))
4694 isl_die(qp->dim->ctx, isl_error_invalid,
4695 "input quasi-polynomial not affine", goto error);
4696 aff = isl_qpolynomial_extract_affine(qp);
4699 dim = isl_qpolynomial_get_space(qp);
4700 dim = isl_space_from_domain(dim);
4701 pos = 1 + isl_space_offset(dim, isl_dim_out);
4702 dim = isl_space_add_dims(dim, isl_dim_out, 1);
4703 n_div = qp->div->n_row;
4704 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4706 for (i = 0; i < n_div; ++i) {
4707 k = isl_basic_map_alloc_div(bmap);
4710 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4711 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4712 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4715 k = isl_basic_map_alloc_equality(bmap);
4718 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4719 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4720 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4723 isl_qpolynomial_free(qp);
4724 bmap = isl_basic_map_finalize(bmap);
4728 isl_qpolynomial_free(qp);
4729 isl_basic_map_free(bmap);