isl_tab_pip.c: restore_lexmin: return int instead of isl_tab *
[isl.git] / isl_polynomial.c
blob4ac53abe5778c250c1929e104c805f4c7b46f649
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #include <stdlib.h>
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl/lp.h>
16 #include <isl/seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_polynomial_private.h>
19 #include <isl_point_private.h>
20 #include <isl_dim_private.h>
21 #include <isl_mat_private.h>
22 #include <isl_range.h>
24 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
26 switch (type) {
27 case isl_dim_param: return 0;
28 case isl_dim_in: return dim->nparam;
29 case isl_dim_out: return dim->nparam + dim->n_in;
30 default: return 0;
34 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
36 if (!up)
37 return -1;
39 return up->var < 0;
42 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
44 if (!up)
45 return NULL;
47 isl_assert(up->ctx, up->var < 0, return NULL);
49 return (struct isl_upoly_cst *)up;
52 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
54 if (!up)
55 return NULL;
57 isl_assert(up->ctx, up->var >= 0, return NULL);
59 return (struct isl_upoly_rec *)up;
62 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
63 __isl_keep struct isl_upoly *up2)
65 int i;
66 struct isl_upoly_rec *rec1, *rec2;
68 if (!up1 || !up2)
69 return -1;
70 if (up1 == up2)
71 return 1;
72 if (up1->var != up2->var)
73 return 0;
74 if (isl_upoly_is_cst(up1)) {
75 struct isl_upoly_cst *cst1, *cst2;
76 cst1 = isl_upoly_as_cst(up1);
77 cst2 = isl_upoly_as_cst(up2);
78 if (!cst1 || !cst2)
79 return -1;
80 return isl_int_eq(cst1->n, cst2->n) &&
81 isl_int_eq(cst1->d, cst2->d);
84 rec1 = isl_upoly_as_rec(up1);
85 rec2 = isl_upoly_as_rec(up2);
86 if (!rec1 || !rec2)
87 return -1;
89 if (rec1->n != rec2->n)
90 return 0;
92 for (i = 0; i < rec1->n; ++i) {
93 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
94 if (eq < 0 || !eq)
95 return eq;
98 return 1;
101 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
103 struct isl_upoly_cst *cst;
105 if (!up)
106 return -1;
107 if (!isl_upoly_is_cst(up))
108 return 0;
110 cst = isl_upoly_as_cst(up);
111 if (!cst)
112 return -1;
114 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
117 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
119 struct isl_upoly_cst *cst;
121 if (!up)
122 return 0;
123 if (!isl_upoly_is_cst(up))
124 return 0;
126 cst = isl_upoly_as_cst(up);
127 if (!cst)
128 return 0;
130 return isl_int_sgn(cst->n);
133 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
135 struct isl_upoly_cst *cst;
137 if (!up)
138 return -1;
139 if (!isl_upoly_is_cst(up))
140 return 0;
142 cst = isl_upoly_as_cst(up);
143 if (!cst)
144 return -1;
146 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
149 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
151 struct isl_upoly_cst *cst;
153 if (!up)
154 return -1;
155 if (!isl_upoly_is_cst(up))
156 return 0;
158 cst = isl_upoly_as_cst(up);
159 if (!cst)
160 return -1;
162 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
165 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
167 struct isl_upoly_cst *cst;
169 if (!up)
170 return -1;
171 if (!isl_upoly_is_cst(up))
172 return 0;
174 cst = isl_upoly_as_cst(up);
175 if (!cst)
176 return -1;
178 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
181 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
183 struct isl_upoly_cst *cst;
185 if (!up)
186 return -1;
187 if (!isl_upoly_is_cst(up))
188 return 0;
190 cst = isl_upoly_as_cst(up);
191 if (!cst)
192 return -1;
194 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
197 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
199 struct isl_upoly_cst *cst;
201 if (!up)
202 return -1;
203 if (!isl_upoly_is_cst(up))
204 return 0;
206 cst = isl_upoly_as_cst(up);
207 if (!cst)
208 return -1;
210 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
213 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
215 struct isl_upoly_cst *cst;
217 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
218 if (!cst)
219 return NULL;
221 cst->up.ref = 1;
222 cst->up.ctx = ctx;
223 isl_ctx_ref(ctx);
224 cst->up.var = -1;
226 isl_int_init(cst->n);
227 isl_int_init(cst->d);
229 return cst;
232 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
234 struct isl_upoly_cst *cst;
236 cst = isl_upoly_cst_alloc(ctx);
237 if (!cst)
238 return NULL;
240 isl_int_set_si(cst->n, 0);
241 isl_int_set_si(cst->d, 1);
243 return &cst->up;
246 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
248 struct isl_upoly_cst *cst;
250 cst = isl_upoly_cst_alloc(ctx);
251 if (!cst)
252 return NULL;
254 isl_int_set_si(cst->n, 1);
255 isl_int_set_si(cst->d, 1);
257 return &cst->up;
260 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
262 struct isl_upoly_cst *cst;
264 cst = isl_upoly_cst_alloc(ctx);
265 if (!cst)
266 return NULL;
268 isl_int_set_si(cst->n, 1);
269 isl_int_set_si(cst->d, 0);
271 return &cst->up;
274 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
276 struct isl_upoly_cst *cst;
278 cst = isl_upoly_cst_alloc(ctx);
279 if (!cst)
280 return NULL;
282 isl_int_set_si(cst->n, -1);
283 isl_int_set_si(cst->d, 0);
285 return &cst->up;
288 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
290 struct isl_upoly_cst *cst;
292 cst = isl_upoly_cst_alloc(ctx);
293 if (!cst)
294 return NULL;
296 isl_int_set_si(cst->n, 0);
297 isl_int_set_si(cst->d, 0);
299 return &cst->up;
302 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
303 isl_int n, isl_int d)
305 struct isl_upoly_cst *cst;
307 cst = isl_upoly_cst_alloc(ctx);
308 if (!cst)
309 return NULL;
311 isl_int_set(cst->n, n);
312 isl_int_set(cst->d, d);
314 return &cst->up;
317 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
318 int var, int size)
320 struct isl_upoly_rec *rec;
322 isl_assert(ctx, var >= 0, return NULL);
323 isl_assert(ctx, size >= 0, return NULL);
324 rec = isl_calloc(ctx, struct isl_upoly_rec,
325 sizeof(struct isl_upoly_rec) +
326 (size - 1) * sizeof(struct isl_upoly *));
327 if (!rec)
328 return NULL;
330 rec->up.ref = 1;
331 rec->up.ctx = ctx;
332 isl_ctx_ref(ctx);
333 rec->up.var = var;
335 rec->n = 0;
336 rec->size = size;
338 return rec;
341 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
342 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
344 qp = isl_qpolynomial_cow(qp);
345 if (!qp || !dim)
346 goto error;
348 isl_dim_free(qp->dim);
349 qp->dim = dim;
351 return qp;
352 error:
353 isl_qpolynomial_free(qp);
354 isl_dim_free(dim);
355 return NULL;
358 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
360 return qp ? qp->dim->ctx : NULL;
363 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
365 return qp ? isl_dim_copy(qp->dim) : NULL;
368 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
369 enum isl_dim_type type)
371 return qp ? isl_dim_size(qp->dim, type) : 0;
374 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
376 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
379 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
381 return qp ? isl_upoly_is_one(qp->upoly) : -1;
384 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
386 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
389 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
391 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
394 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
396 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
399 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
401 return qp ? isl_upoly_sgn(qp->upoly) : 0;
404 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
406 isl_int_clear(cst->n);
407 isl_int_clear(cst->d);
410 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
412 int i;
414 for (i = 0; i < rec->n; ++i)
415 isl_upoly_free(rec->p[i]);
418 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
420 if (!up)
421 return NULL;
423 up->ref++;
424 return up;
427 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
429 struct isl_upoly_cst *cst;
430 struct isl_upoly_cst *dup;
432 cst = isl_upoly_as_cst(up);
433 if (!cst)
434 return NULL;
436 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
437 if (!dup)
438 return NULL;
439 isl_int_set(dup->n, cst->n);
440 isl_int_set(dup->d, cst->d);
442 return &dup->up;
445 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
447 int i;
448 struct isl_upoly_rec *rec;
449 struct isl_upoly_rec *dup;
451 rec = isl_upoly_as_rec(up);
452 if (!rec)
453 return NULL;
455 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
456 if (!dup)
457 return NULL;
459 for (i = 0; i < rec->n; ++i) {
460 dup->p[i] = isl_upoly_copy(rec->p[i]);
461 if (!dup->p[i])
462 goto error;
463 dup->n++;
466 return &dup->up;
467 error:
468 isl_upoly_free(&dup->up);
469 return NULL;
472 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
474 struct isl_upoly *dup;
476 if (!up)
477 return NULL;
479 if (isl_upoly_is_cst(up))
480 return isl_upoly_dup_cst(up);
481 else
482 return isl_upoly_dup_rec(up);
485 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
487 if (!up)
488 return NULL;
490 if (up->ref == 1)
491 return up;
492 up->ref--;
493 return isl_upoly_dup(up);
496 void isl_upoly_free(__isl_take struct isl_upoly *up)
498 if (!up)
499 return;
501 if (--up->ref > 0)
502 return;
504 if (up->var < 0)
505 upoly_free_cst((struct isl_upoly_cst *)up);
506 else
507 upoly_free_rec((struct isl_upoly_rec *)up);
509 isl_ctx_deref(up->ctx);
510 free(up);
513 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
515 isl_int gcd;
517 isl_int_init(gcd);
518 isl_int_gcd(gcd, cst->n, cst->d);
519 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
520 isl_int_divexact(cst->n, cst->n, gcd);
521 isl_int_divexact(cst->d, cst->d, gcd);
523 isl_int_clear(gcd);
526 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
527 __isl_take struct isl_upoly *up2)
529 struct isl_upoly_cst *cst1;
530 struct isl_upoly_cst *cst2;
532 up1 = isl_upoly_cow(up1);
533 if (!up1 || !up2)
534 goto error;
536 cst1 = isl_upoly_as_cst(up1);
537 cst2 = isl_upoly_as_cst(up2);
539 if (isl_int_eq(cst1->d, cst2->d))
540 isl_int_add(cst1->n, cst1->n, cst2->n);
541 else {
542 isl_int_mul(cst1->n, cst1->n, cst2->d);
543 isl_int_addmul(cst1->n, cst2->n, cst1->d);
544 isl_int_mul(cst1->d, cst1->d, cst2->d);
547 isl_upoly_cst_reduce(cst1);
549 isl_upoly_free(up2);
550 return up1;
551 error:
552 isl_upoly_free(up1);
553 isl_upoly_free(up2);
554 return NULL;
557 static __isl_give struct isl_upoly *replace_by_zero(
558 __isl_take struct isl_upoly *up)
560 struct isl_ctx *ctx;
562 if (!up)
563 return NULL;
564 ctx = up->ctx;
565 isl_upoly_free(up);
566 return isl_upoly_zero(ctx);
569 static __isl_give struct isl_upoly *replace_by_constant_term(
570 __isl_take struct isl_upoly *up)
572 struct isl_upoly_rec *rec;
573 struct isl_upoly *cst;
575 if (!up)
576 return NULL;
578 rec = isl_upoly_as_rec(up);
579 if (!rec)
580 goto error;
581 cst = isl_upoly_copy(rec->p[0]);
582 isl_upoly_free(up);
583 return cst;
584 error:
585 isl_upoly_free(up);
586 return NULL;
589 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
590 __isl_take struct isl_upoly *up2)
592 int i;
593 struct isl_upoly_rec *rec1, *rec2;
595 if (!up1 || !up2)
596 goto error;
598 if (isl_upoly_is_nan(up1)) {
599 isl_upoly_free(up2);
600 return up1;
603 if (isl_upoly_is_nan(up2)) {
604 isl_upoly_free(up1);
605 return up2;
608 if (isl_upoly_is_zero(up1)) {
609 isl_upoly_free(up1);
610 return up2;
613 if (isl_upoly_is_zero(up2)) {
614 isl_upoly_free(up2);
615 return up1;
618 if (up1->var < up2->var)
619 return isl_upoly_sum(up2, up1);
621 if (up2->var < up1->var) {
622 struct isl_upoly_rec *rec;
623 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
624 isl_upoly_free(up1);
625 return up2;
627 up1 = isl_upoly_cow(up1);
628 rec = isl_upoly_as_rec(up1);
629 if (!rec)
630 goto error;
631 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
632 if (rec->n == 1)
633 up1 = replace_by_constant_term(up1);
634 return up1;
637 if (isl_upoly_is_cst(up1))
638 return isl_upoly_sum_cst(up1, up2);
640 rec1 = isl_upoly_as_rec(up1);
641 rec2 = isl_upoly_as_rec(up2);
642 if (!rec1 || !rec2)
643 goto error;
645 if (rec1->n < rec2->n)
646 return isl_upoly_sum(up2, up1);
648 up1 = isl_upoly_cow(up1);
649 rec1 = isl_upoly_as_rec(up1);
650 if (!rec1)
651 goto error;
653 for (i = rec2->n - 1; i >= 0; --i) {
654 rec1->p[i] = isl_upoly_sum(rec1->p[i],
655 isl_upoly_copy(rec2->p[i]));
656 if (!rec1->p[i])
657 goto error;
658 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
659 isl_upoly_free(rec1->p[i]);
660 rec1->n--;
664 if (rec1->n == 0)
665 up1 = replace_by_zero(up1);
666 else if (rec1->n == 1)
667 up1 = replace_by_constant_term(up1);
669 isl_upoly_free(up2);
671 return up1;
672 error:
673 isl_upoly_free(up1);
674 isl_upoly_free(up2);
675 return NULL;
678 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
679 __isl_take struct isl_upoly *up, isl_int v)
681 struct isl_upoly_cst *cst;
683 up = isl_upoly_cow(up);
684 if (!up)
685 return NULL;
687 cst = isl_upoly_as_cst(up);
689 isl_int_addmul(cst->n, cst->d, v);
691 return up;
694 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
695 __isl_take struct isl_upoly *up, isl_int v)
697 struct isl_upoly_rec *rec;
699 if (!up)
700 return NULL;
702 if (isl_upoly_is_cst(up))
703 return isl_upoly_cst_add_isl_int(up, v);
705 up = isl_upoly_cow(up);
706 rec = isl_upoly_as_rec(up);
707 if (!rec)
708 goto error;
710 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
711 if (!rec->p[0])
712 goto error;
714 return up;
715 error:
716 isl_upoly_free(up);
717 return NULL;
720 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
721 __isl_take struct isl_upoly *up, isl_int v)
723 struct isl_upoly_cst *cst;
725 if (isl_upoly_is_zero(up))
726 return up;
728 up = isl_upoly_cow(up);
729 if (!up)
730 return NULL;
732 cst = isl_upoly_as_cst(up);
734 isl_int_mul(cst->n, cst->n, v);
736 return up;
739 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
740 __isl_take struct isl_upoly *up, isl_int v)
742 int i;
743 struct isl_upoly_rec *rec;
745 if (!up)
746 return NULL;
748 if (isl_upoly_is_cst(up))
749 return isl_upoly_cst_mul_isl_int(up, v);
751 up = isl_upoly_cow(up);
752 rec = isl_upoly_as_rec(up);
753 if (!rec)
754 goto error;
756 for (i = 0; i < rec->n; ++i) {
757 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
758 if (!rec->p[i])
759 goto error;
762 return up;
763 error:
764 isl_upoly_free(up);
765 return NULL;
768 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
769 __isl_take struct isl_upoly *up2)
771 struct isl_upoly_cst *cst1;
772 struct isl_upoly_cst *cst2;
774 up1 = isl_upoly_cow(up1);
775 if (!up1 || !up2)
776 goto error;
778 cst1 = isl_upoly_as_cst(up1);
779 cst2 = isl_upoly_as_cst(up2);
781 isl_int_mul(cst1->n, cst1->n, cst2->n);
782 isl_int_mul(cst1->d, cst1->d, cst2->d);
784 isl_upoly_cst_reduce(cst1);
786 isl_upoly_free(up2);
787 return up1;
788 error:
789 isl_upoly_free(up1);
790 isl_upoly_free(up2);
791 return NULL;
794 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
795 __isl_take struct isl_upoly *up2)
797 struct isl_upoly_rec *rec1;
798 struct isl_upoly_rec *rec2;
799 struct isl_upoly_rec *res;
800 int i, j;
801 int size;
803 rec1 = isl_upoly_as_rec(up1);
804 rec2 = isl_upoly_as_rec(up2);
805 if (!rec1 || !rec2)
806 goto error;
807 size = rec1->n + rec2->n - 1;
808 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
809 if (!res)
810 goto error;
812 for (i = 0; i < rec1->n; ++i) {
813 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
814 isl_upoly_copy(rec1->p[i]));
815 if (!res->p[i])
816 goto error;
817 res->n++;
819 for (; i < size; ++i) {
820 res->p[i] = isl_upoly_zero(up1->ctx);
821 if (!res->p[i])
822 goto error;
823 res->n++;
825 for (i = 0; i < rec1->n; ++i) {
826 for (j = 1; j < rec2->n; ++j) {
827 struct isl_upoly *up;
828 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
829 isl_upoly_copy(rec1->p[i]));
830 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
831 if (!res->p[i + j])
832 goto error;
836 isl_upoly_free(up1);
837 isl_upoly_free(up2);
839 return &res->up;
840 error:
841 isl_upoly_free(up1);
842 isl_upoly_free(up2);
843 isl_upoly_free(&res->up);
844 return NULL;
847 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
848 __isl_take struct isl_upoly *up2)
850 if (!up1 || !up2)
851 goto error;
853 if (isl_upoly_is_nan(up1)) {
854 isl_upoly_free(up2);
855 return up1;
858 if (isl_upoly_is_nan(up2)) {
859 isl_upoly_free(up1);
860 return up2;
863 if (isl_upoly_is_zero(up1)) {
864 isl_upoly_free(up2);
865 return up1;
868 if (isl_upoly_is_zero(up2)) {
869 isl_upoly_free(up1);
870 return up2;
873 if (isl_upoly_is_one(up1)) {
874 isl_upoly_free(up1);
875 return up2;
878 if (isl_upoly_is_one(up2)) {
879 isl_upoly_free(up2);
880 return up1;
883 if (up1->var < up2->var)
884 return isl_upoly_mul(up2, up1);
886 if (up2->var < up1->var) {
887 int i;
888 struct isl_upoly_rec *rec;
889 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
890 isl_ctx *ctx = up1->ctx;
891 isl_upoly_free(up1);
892 isl_upoly_free(up2);
893 return isl_upoly_nan(ctx);
895 up1 = isl_upoly_cow(up1);
896 rec = isl_upoly_as_rec(up1);
897 if (!rec)
898 goto error;
900 for (i = 0; i < rec->n; ++i) {
901 rec->p[i] = isl_upoly_mul(rec->p[i],
902 isl_upoly_copy(up2));
903 if (!rec->p[i])
904 goto error;
906 isl_upoly_free(up2);
907 return up1;
910 if (isl_upoly_is_cst(up1))
911 return isl_upoly_mul_cst(up1, up2);
913 return isl_upoly_mul_rec(up1, up2);
914 error:
915 isl_upoly_free(up1);
916 isl_upoly_free(up2);
917 return NULL;
920 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
921 unsigned power)
923 struct isl_upoly *res;
925 if (!up)
926 return NULL;
927 if (power == 1)
928 return up;
930 if (power % 2)
931 res = isl_upoly_copy(up);
932 else
933 res = isl_upoly_one(up->ctx);
935 while (power >>= 1) {
936 up = isl_upoly_mul(up, isl_upoly_copy(up));
937 if (power % 2)
938 res = isl_upoly_mul(res, isl_upoly_copy(up));
941 isl_upoly_free(up);
942 return res;
945 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
946 unsigned n_div, __isl_take struct isl_upoly *up)
948 struct isl_qpolynomial *qp = NULL;
949 unsigned total;
951 if (!dim || !up)
952 goto error;
954 total = isl_dim_total(dim);
956 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
957 if (!qp)
958 goto error;
960 qp->ref = 1;
961 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
962 if (!qp->div)
963 goto error;
965 qp->dim = dim;
966 qp->upoly = up;
968 return qp;
969 error:
970 isl_dim_free(dim);
971 isl_upoly_free(up);
972 isl_qpolynomial_free(qp);
973 return NULL;
976 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
978 if (!qp)
979 return NULL;
981 qp->ref++;
982 return qp;
985 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
987 struct isl_qpolynomial *dup;
989 if (!qp)
990 return NULL;
992 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
993 isl_upoly_copy(qp->upoly));
994 if (!dup)
995 return NULL;
996 isl_mat_free(dup->div);
997 dup->div = isl_mat_copy(qp->div);
998 if (!dup->div)
999 goto error;
1001 return dup;
1002 error:
1003 isl_qpolynomial_free(dup);
1004 return NULL;
1007 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1009 if (!qp)
1010 return NULL;
1012 if (qp->ref == 1)
1013 return qp;
1014 qp->ref--;
1015 return isl_qpolynomial_dup(qp);
1018 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1020 if (!qp)
1021 return;
1023 if (--qp->ref > 0)
1024 return;
1026 isl_dim_free(qp->dim);
1027 isl_mat_free(qp->div);
1028 isl_upoly_free(qp->upoly);
1030 free(qp);
1033 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1035 int i;
1036 struct isl_upoly *up;
1037 struct isl_upoly_rec *rec;
1038 struct isl_upoly_cst *cst;
1040 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1041 if (!rec)
1042 return NULL;
1043 for (i = 0; i < 1 + power; ++i) {
1044 rec->p[i] = isl_upoly_zero(ctx);
1045 if (!rec->p[i])
1046 goto error;
1047 rec->n++;
1049 cst = isl_upoly_as_cst(rec->p[power]);
1050 isl_int_set_si(cst->n, 1);
1052 return &rec->up;
1053 error:
1054 isl_upoly_free(&rec->up);
1055 return NULL;
1058 /* r array maps original positions to new positions.
1060 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1061 int *r)
1063 int i;
1064 struct isl_upoly_rec *rec;
1065 struct isl_upoly *base;
1066 struct isl_upoly *res;
1068 if (isl_upoly_is_cst(up))
1069 return up;
1071 rec = isl_upoly_as_rec(up);
1072 if (!rec)
1073 goto error;
1075 isl_assert(up->ctx, rec->n >= 1, goto error);
1077 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1078 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1080 for (i = rec->n - 2; i >= 0; --i) {
1081 res = isl_upoly_mul(res, isl_upoly_copy(base));
1082 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1085 isl_upoly_free(base);
1086 isl_upoly_free(up);
1088 return res;
1089 error:
1090 isl_upoly_free(up);
1091 return NULL;
1094 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1096 int n_row, n_col;
1097 int equal;
1099 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1100 div1->n_col >= div2->n_col, return -1);
1102 if (div1->n_row == div2->n_row)
1103 return isl_mat_is_equal(div1, div2);
1105 n_row = div1->n_row;
1106 n_col = div1->n_col;
1107 div1->n_row = div2->n_row;
1108 div1->n_col = div2->n_col;
1110 equal = isl_mat_is_equal(div1, div2);
1112 div1->n_row = n_row;
1113 div1->n_col = n_col;
1115 return equal;
1118 static void expand_row(__isl_keep isl_mat *dst, int d,
1119 __isl_keep isl_mat *src, int s, int *exp)
1121 int i;
1122 unsigned c = src->n_col - src->n_row;
1124 isl_seq_cpy(dst->row[d], src->row[s], c);
1125 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
1127 for (i = 0; i < s; ++i)
1128 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
1131 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1133 int li, lj;
1135 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1136 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1138 if (li != lj)
1139 return li - lj;
1141 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1144 struct isl_div_sort_info {
1145 isl_mat *div;
1146 int row;
1149 static int div_sort_cmp(const void *p1, const void *p2)
1151 const struct isl_div_sort_info *i1, *i2;
1152 i1 = (const struct isl_div_sort_info *) p1;
1153 i2 = (const struct isl_div_sort_info *) p2;
1155 return cmp_row(i1->div, i1->row, i2->row);
1158 /* Sort divs and remove duplicates.
1160 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1162 int i;
1163 int skip;
1164 int len;
1165 struct isl_div_sort_info *array = NULL;
1166 int *pos = NULL, *at = NULL;
1167 int *reordering = NULL;
1168 unsigned div_pos;
1170 if (!qp)
1171 return NULL;
1172 if (qp->div->n_row <= 1)
1173 return qp;
1175 div_pos = isl_dim_total(qp->dim);
1177 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1178 qp->div->n_row);
1179 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1180 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1181 len = qp->div->n_col - 2;
1182 reordering = isl_alloc_array(qp->div->ctx, int, len);
1183 if (!array || !pos || !at || !reordering)
1184 goto error;
1186 for (i = 0; i < qp->div->n_row; ++i) {
1187 array[i].div = qp->div;
1188 array[i].row = i;
1189 pos[i] = i;
1190 at[i] = i;
1193 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1194 div_sort_cmp);
1196 for (i = 0; i < div_pos; ++i)
1197 reordering[i] = i;
1199 for (i = 0; i < qp->div->n_row; ++i) {
1200 if (pos[array[i].row] == i)
1201 continue;
1202 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1203 pos[at[i]] = pos[array[i].row];
1204 at[pos[array[i].row]] = at[i];
1205 at[i] = array[i].row;
1206 pos[array[i].row] = i;
1209 skip = 0;
1210 for (i = 0; i < len - div_pos; ++i) {
1211 if (i > 0 &&
1212 isl_seq_eq(qp->div->row[i - skip - 1],
1213 qp->div->row[i - skip], qp->div->n_col)) {
1214 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1215 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1216 2 + div_pos + i - skip);
1217 qp->div = isl_mat_drop_cols(qp->div,
1218 2 + div_pos + i - skip, 1);
1219 skip++;
1221 reordering[div_pos + array[i].row] = div_pos + i - skip;
1224 qp->upoly = reorder(qp->upoly, reordering);
1226 if (!qp->upoly || !qp->div)
1227 goto error;
1229 free(at);
1230 free(pos);
1231 free(array);
1232 free(reordering);
1234 return qp;
1235 error:
1236 free(at);
1237 free(pos);
1238 free(array);
1239 free(reordering);
1240 isl_qpolynomial_free(qp);
1241 return NULL;
1244 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1245 __isl_keep isl_mat *div2, int *exp1, int *exp2)
1247 int i, j, k;
1248 isl_mat *div = NULL;
1249 unsigned d = div1->n_col - div1->n_row;
1251 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1252 d + div1->n_row + div2->n_row);
1253 if (!div)
1254 return NULL;
1256 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1257 int cmp;
1259 expand_row(div, k, div1, i, exp1);
1260 expand_row(div, k + 1, div2, j, exp2);
1262 cmp = cmp_row(div, k, k + 1);
1263 if (cmp == 0) {
1264 exp1[i++] = k;
1265 exp2[j++] = k;
1266 } else if (cmp < 0) {
1267 exp1[i++] = k;
1268 } else {
1269 exp2[j++] = k;
1270 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1273 for (; i < div1->n_row; ++i, ++k) {
1274 expand_row(div, k, div1, i, exp1);
1275 exp1[i] = k;
1277 for (; j < div2->n_row; ++j, ++k) {
1278 expand_row(div, k, div2, j, exp2);
1279 exp2[j] = k;
1282 div->n_row = k;
1283 div->n_col = d + k;
1285 return div;
1288 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1289 int *exp, int first)
1291 int i;
1292 struct isl_upoly_rec *rec;
1294 if (isl_upoly_is_cst(up))
1295 return up;
1297 if (up->var < first)
1298 return up;
1300 if (exp[up->var - first] == up->var - first)
1301 return up;
1303 up = isl_upoly_cow(up);
1304 if (!up)
1305 goto error;
1307 up->var = exp[up->var - first] + first;
1309 rec = isl_upoly_as_rec(up);
1310 if (!rec)
1311 goto error;
1313 for (i = 0; i < rec->n; ++i) {
1314 rec->p[i] = expand(rec->p[i], exp, first);
1315 if (!rec->p[i])
1316 goto error;
1319 return up;
1320 error:
1321 isl_upoly_free(up);
1322 return NULL;
1325 static __isl_give isl_qpolynomial *with_merged_divs(
1326 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1327 __isl_take isl_qpolynomial *qp2),
1328 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1330 int *exp1 = NULL;
1331 int *exp2 = NULL;
1332 isl_mat *div = NULL;
1334 qp1 = isl_qpolynomial_cow(qp1);
1335 qp2 = isl_qpolynomial_cow(qp2);
1337 if (!qp1 || !qp2)
1338 goto error;
1340 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1341 qp1->div->n_col >= qp2->div->n_col, goto error);
1343 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1344 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1345 if (!exp1 || !exp2)
1346 goto error;
1348 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1349 if (!div)
1350 goto error;
1352 isl_mat_free(qp1->div);
1353 qp1->div = isl_mat_copy(div);
1354 isl_mat_free(qp2->div);
1355 qp2->div = isl_mat_copy(div);
1357 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1358 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1360 if (!qp1->upoly || !qp2->upoly)
1361 goto error;
1363 isl_mat_free(div);
1364 free(exp1);
1365 free(exp2);
1367 return fn(qp1, qp2);
1368 error:
1369 isl_mat_free(div);
1370 free(exp1);
1371 free(exp2);
1372 isl_qpolynomial_free(qp1);
1373 isl_qpolynomial_free(qp2);
1374 return NULL;
1377 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1378 __isl_take isl_qpolynomial *qp2)
1380 qp1 = isl_qpolynomial_cow(qp1);
1382 if (!qp1 || !qp2)
1383 goto error;
1385 if (qp1->div->n_row < qp2->div->n_row)
1386 return isl_qpolynomial_add(qp2, qp1);
1388 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1389 if (!compatible_divs(qp1->div, qp2->div))
1390 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1392 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1393 if (!qp1->upoly)
1394 goto error;
1396 isl_qpolynomial_free(qp2);
1398 return qp1;
1399 error:
1400 isl_qpolynomial_free(qp1);
1401 isl_qpolynomial_free(qp2);
1402 return NULL;
1405 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1406 __isl_keep isl_set *dom,
1407 __isl_take isl_qpolynomial *qp1,
1408 __isl_take isl_qpolynomial *qp2)
1410 qp1 = isl_qpolynomial_add(qp1, qp2);
1411 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1412 return qp1;
1415 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1416 __isl_take isl_qpolynomial *qp2)
1418 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1421 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1422 __isl_take isl_qpolynomial *qp, isl_int v)
1424 if (isl_int_is_zero(v))
1425 return qp;
1427 qp = isl_qpolynomial_cow(qp);
1428 if (!qp)
1429 return NULL;
1431 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1432 if (!qp->upoly)
1433 goto error;
1435 return qp;
1436 error:
1437 isl_qpolynomial_free(qp);
1438 return NULL;
1442 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1444 if (!qp)
1445 return NULL;
1447 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1450 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1451 __isl_take isl_qpolynomial *qp, isl_int v)
1453 if (isl_int_is_one(v))
1454 return qp;
1456 if (qp && isl_int_is_zero(v)) {
1457 isl_qpolynomial *zero;
1458 zero = isl_qpolynomial_zero(isl_dim_copy(qp->dim));
1459 isl_qpolynomial_free(qp);
1460 return zero;
1463 qp = isl_qpolynomial_cow(qp);
1464 if (!qp)
1465 return NULL;
1467 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1468 if (!qp->upoly)
1469 goto error;
1471 return qp;
1472 error:
1473 isl_qpolynomial_free(qp);
1474 return NULL;
1477 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1478 __isl_take isl_qpolynomial *qp2)
1480 qp1 = isl_qpolynomial_cow(qp1);
1482 if (!qp1 || !qp2)
1483 goto error;
1485 if (qp1->div->n_row < qp2->div->n_row)
1486 return isl_qpolynomial_mul(qp2, qp1);
1488 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1489 if (!compatible_divs(qp1->div, qp2->div))
1490 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1492 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1493 if (!qp1->upoly)
1494 goto error;
1496 isl_qpolynomial_free(qp2);
1498 return qp1;
1499 error:
1500 isl_qpolynomial_free(qp1);
1501 isl_qpolynomial_free(qp2);
1502 return NULL;
1505 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1506 unsigned power)
1508 qp = isl_qpolynomial_cow(qp);
1510 if (!qp)
1511 return NULL;
1513 qp->upoly = isl_upoly_pow(qp->upoly, power);
1514 if (!qp->upoly)
1515 goto error;
1517 return qp;
1518 error:
1519 isl_qpolynomial_free(qp);
1520 return NULL;
1523 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1525 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1528 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1530 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1533 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1535 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1538 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1540 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1543 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1545 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1548 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1549 isl_int v)
1551 struct isl_qpolynomial *qp;
1552 struct isl_upoly_cst *cst;
1554 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1555 if (!qp)
1556 return NULL;
1558 cst = isl_upoly_as_cst(qp->upoly);
1559 isl_int_set(cst->n, v);
1561 return qp;
1564 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1565 isl_int *n, isl_int *d)
1567 struct isl_upoly_cst *cst;
1569 if (!qp)
1570 return -1;
1572 if (!isl_upoly_is_cst(qp->upoly))
1573 return 0;
1575 cst = isl_upoly_as_cst(qp->upoly);
1576 if (!cst)
1577 return -1;
1579 if (n)
1580 isl_int_set(*n, cst->n);
1581 if (d)
1582 isl_int_set(*d, cst->d);
1584 return 1;
1587 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1589 int is_cst;
1590 struct isl_upoly_rec *rec;
1592 if (!up)
1593 return -1;
1595 if (up->var < 0)
1596 return 1;
1598 rec = isl_upoly_as_rec(up);
1599 if (!rec)
1600 return -1;
1602 if (rec->n > 2)
1603 return 0;
1605 isl_assert(up->ctx, rec->n > 1, return -1);
1607 is_cst = isl_upoly_is_cst(rec->p[1]);
1608 if (is_cst < 0)
1609 return -1;
1610 if (!is_cst)
1611 return 0;
1613 return isl_upoly_is_affine(rec->p[0]);
1616 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1618 if (!qp)
1619 return -1;
1621 if (qp->div->n_row > 0)
1622 return 0;
1624 return isl_upoly_is_affine(qp->upoly);
1627 static void update_coeff(__isl_keep isl_vec *aff,
1628 __isl_keep struct isl_upoly_cst *cst, int pos)
1630 isl_int gcd;
1631 isl_int f;
1633 if (isl_int_is_zero(cst->n))
1634 return;
1636 isl_int_init(gcd);
1637 isl_int_init(f);
1638 isl_int_gcd(gcd, cst->d, aff->el[0]);
1639 isl_int_divexact(f, cst->d, gcd);
1640 isl_int_divexact(gcd, aff->el[0], gcd);
1641 isl_seq_scale(aff->el, aff->el, f, aff->size);
1642 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1643 isl_int_clear(gcd);
1644 isl_int_clear(f);
1647 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1648 __isl_keep isl_vec *aff)
1650 struct isl_upoly_cst *cst;
1651 struct isl_upoly_rec *rec;
1653 if (!up || !aff)
1654 return -1;
1656 if (up->var < 0) {
1657 struct isl_upoly_cst *cst;
1659 cst = isl_upoly_as_cst(up);
1660 if (!cst)
1661 return -1;
1662 update_coeff(aff, cst, 0);
1663 return 0;
1666 rec = isl_upoly_as_rec(up);
1667 if (!rec)
1668 return -1;
1669 isl_assert(up->ctx, rec->n == 2, return -1);
1671 cst = isl_upoly_as_cst(rec->p[1]);
1672 if (!cst)
1673 return -1;
1674 update_coeff(aff, cst, 1 + up->var);
1676 return isl_upoly_update_affine(rec->p[0], aff);
1679 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1680 __isl_keep isl_qpolynomial *qp)
1682 isl_vec *aff;
1683 unsigned d;
1685 if (!qp)
1686 return NULL;
1688 d = isl_dim_total(qp->dim);
1689 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1690 if (!aff)
1691 return NULL;
1693 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1694 isl_int_set_si(aff->el[0], 1);
1696 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1697 goto error;
1699 return aff;
1700 error:
1701 isl_vec_free(aff);
1702 return NULL;
1705 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1706 __isl_keep isl_qpolynomial *qp2)
1708 if (!qp1 || !qp2)
1709 return -1;
1711 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1714 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1716 int i;
1717 struct isl_upoly_rec *rec;
1719 if (isl_upoly_is_cst(up)) {
1720 struct isl_upoly_cst *cst;
1721 cst = isl_upoly_as_cst(up);
1722 if (!cst)
1723 return;
1724 isl_int_lcm(*d, *d, cst->d);
1725 return;
1728 rec = isl_upoly_as_rec(up);
1729 if (!rec)
1730 return;
1732 for (i = 0; i < rec->n; ++i)
1733 upoly_update_den(rec->p[i], d);
1736 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1738 isl_int_set_si(*d, 1);
1739 if (!qp)
1740 return;
1741 upoly_update_den(qp->upoly, d);
1744 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1745 int pos, int power)
1747 struct isl_ctx *ctx;
1749 if (!dim)
1750 return NULL;
1752 ctx = dim->ctx;
1754 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1757 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1758 enum isl_dim_type type, unsigned pos)
1760 if (!dim)
1761 return NULL;
1763 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1764 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1766 if (type == isl_dim_set)
1767 pos += isl_dim_size(dim, isl_dim_param);
1769 return isl_qpolynomial_var_pow(dim, pos, 1);
1770 error:
1771 isl_dim_free(dim);
1772 return NULL;
1775 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1776 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1778 int i;
1779 struct isl_upoly_rec *rec;
1780 struct isl_upoly *base, *res;
1782 if (!up)
1783 return NULL;
1785 if (isl_upoly_is_cst(up))
1786 return up;
1788 if (up->var < first)
1789 return up;
1791 rec = isl_upoly_as_rec(up);
1792 if (!rec)
1793 goto error;
1795 isl_assert(up->ctx, rec->n >= 1, goto error);
1797 if (up->var >= first + n)
1798 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1799 else
1800 base = isl_upoly_copy(subs[up->var - first]);
1802 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1803 for (i = rec->n - 2; i >= 0; --i) {
1804 struct isl_upoly *t;
1805 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1806 res = isl_upoly_mul(res, isl_upoly_copy(base));
1807 res = isl_upoly_sum(res, t);
1810 isl_upoly_free(base);
1811 isl_upoly_free(up);
1813 return res;
1814 error:
1815 isl_upoly_free(up);
1816 return NULL;
1819 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1820 isl_int denom, unsigned len)
1822 int i;
1823 struct isl_upoly *up;
1825 isl_assert(ctx, len >= 1, return NULL);
1827 up = isl_upoly_rat_cst(ctx, f[0], denom);
1828 for (i = 0; i < len - 1; ++i) {
1829 struct isl_upoly *t;
1830 struct isl_upoly *c;
1832 if (isl_int_is_zero(f[1 + i]))
1833 continue;
1835 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1836 t = isl_upoly_var_pow(ctx, i, 1);
1837 t = isl_upoly_mul(c, t);
1838 up = isl_upoly_sum(up, t);
1841 return up;
1844 /* Remove common factor of non-constant terms and denominator.
1846 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1848 isl_ctx *ctx = qp->div->ctx;
1849 unsigned total = qp->div->n_col - 2;
1851 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1852 isl_int_gcd(ctx->normalize_gcd,
1853 ctx->normalize_gcd, qp->div->row[div][0]);
1854 if (isl_int_is_one(ctx->normalize_gcd))
1855 return;
1857 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1858 ctx->normalize_gcd, total);
1859 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1860 ctx->normalize_gcd);
1861 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1862 ctx->normalize_gcd);
1865 /* Replace the integer division identified by "div" by the polynomial "s".
1866 * The integer division is assumed not to appear in the definition
1867 * of any other integer divisions.
1869 static __isl_give isl_qpolynomial *substitute_div(
1870 __isl_take isl_qpolynomial *qp,
1871 int div, __isl_take struct isl_upoly *s)
1873 int i;
1874 int total;
1875 int *reordering;
1877 if (!qp || !s)
1878 goto error;
1880 qp = isl_qpolynomial_cow(qp);
1881 if (!qp)
1882 goto error;
1884 total = isl_dim_total(qp->dim);
1885 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1886 if (!qp->upoly)
1887 goto error;
1889 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1890 if (!reordering)
1891 goto error;
1892 for (i = 0; i < total + div; ++i)
1893 reordering[i] = i;
1894 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1895 reordering[i] = i - 1;
1896 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1897 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1898 qp->upoly = reorder(qp->upoly, reordering);
1899 free(reordering);
1901 if (!qp->upoly || !qp->div)
1902 goto error;
1904 isl_upoly_free(s);
1905 return qp;
1906 error:
1907 isl_qpolynomial_free(qp);
1908 isl_upoly_free(s);
1909 return NULL;
1912 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1913 * divisions because d is equal to 1 by their definition, i.e., e.
1915 static __isl_give isl_qpolynomial *substitute_non_divs(
1916 __isl_take isl_qpolynomial *qp)
1918 int i, j;
1919 int total;
1920 struct isl_upoly *s;
1922 if (!qp)
1923 return NULL;
1925 total = isl_dim_total(qp->dim);
1926 for (i = 0; qp && i < qp->div->n_row; ++i) {
1927 if (!isl_int_is_one(qp->div->row[i][0]))
1928 continue;
1929 for (j = i + 1; j < qp->div->n_row; ++j) {
1930 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1931 continue;
1932 isl_seq_combine(qp->div->row[j] + 1,
1933 qp->div->ctx->one, qp->div->row[j] + 1,
1934 qp->div->row[j][2 + total + i],
1935 qp->div->row[i] + 1, 1 + total + i);
1936 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1937 normalize_div(qp, j);
1939 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1940 qp->div->row[i][0], qp->div->n_col - 1);
1941 qp = substitute_div(qp, i, s);
1942 --i;
1945 return qp;
1948 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1949 * with d the denominator. When replacing the coefficient e of x by
1950 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1951 * inside the division, so we need to add floor(e/d) * x outside.
1952 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1953 * to adjust the coefficient of x in each later div that depends on the
1954 * current div "div" and also in the affine expression "aff"
1955 * (if it too depends on "div").
1957 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1958 __isl_keep isl_vec *aff)
1960 int i, j;
1961 isl_int v;
1962 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1964 isl_int_init(v);
1965 for (i = 0; i < 1 + total + div; ++i) {
1966 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1967 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1968 continue;
1969 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1970 isl_int_fdiv_r(qp->div->row[div][1 + i],
1971 qp->div->row[div][1 + i], qp->div->row[div][0]);
1972 if (!isl_int_is_zero(aff->el[1 + total + div]))
1973 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1974 for (j = div + 1; j < qp->div->n_row; ++j) {
1975 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1976 continue;
1977 isl_int_addmul(qp->div->row[j][1 + i],
1978 v, qp->div->row[j][2 + total + div]);
1981 isl_int_clear(v);
1984 /* Check if the last non-zero coefficient is bigger that half of the
1985 * denominator. If so, we will invert the div to further reduce the number
1986 * of distinct divs that may appear.
1987 * If the last non-zero coefficient is exactly half the denominator,
1988 * then we continue looking for earlier coefficients that are bigger
1989 * than half the denominator.
1991 static int needs_invert(__isl_keep isl_mat *div, int row)
1993 int i;
1994 int cmp;
1996 for (i = div->n_col - 1; i >= 1; --i) {
1997 if (isl_int_is_zero(div->row[row][i]))
1998 continue;
1999 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2000 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2001 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2002 if (cmp)
2003 return cmp > 0;
2004 if (i == 1)
2005 return 1;
2008 return 0;
2011 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2012 * We only invert the coefficients of e (and the coefficient of q in
2013 * later divs and in "aff"). After calling this function, the
2014 * coefficients of e should be reduced again.
2016 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2017 __isl_keep isl_vec *aff)
2019 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2021 isl_seq_neg(qp->div->row[div] + 1,
2022 qp->div->row[div] + 1, qp->div->n_col - 1);
2023 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2024 isl_int_add(qp->div->row[div][1],
2025 qp->div->row[div][1], qp->div->row[div][0]);
2026 if (!isl_int_is_zero(aff->el[1 + total + div]))
2027 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2028 isl_mat_col_mul(qp->div, 2 + total + div,
2029 qp->div->ctx->negone, 2 + total + div);
2032 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2033 * in the interval [0, d-1], with d the denominator and such that the
2034 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2036 * After the reduction, some divs may have become redundant or identical,
2037 * so we call substitute_non_divs and sort_divs. If these functions
2038 * eliminate divs of merge * two or more divs into one, the coefficients
2039 * of the enclosing divs may have to be reduced again, so we call
2040 * ourselves recursively if the number of divs decreases.
2042 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2044 int i, j;
2045 isl_vec *aff = NULL;
2046 struct isl_upoly *s;
2047 unsigned n_div;
2049 if (!qp)
2050 return NULL;
2052 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2053 aff = isl_vec_clr(aff);
2054 if (!aff)
2055 goto error;
2057 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2059 for (i = 0; i < qp->div->n_row; ++i) {
2060 normalize_div(qp, i);
2061 reduce_div(qp, i, aff);
2062 if (needs_invert(qp->div, i)) {
2063 invert_div(qp, i, aff);
2064 reduce_div(qp, i, aff);
2068 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2069 qp->div->ctx->one, aff->size);
2070 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2071 isl_upoly_free(s);
2072 if (!qp->upoly)
2073 goto error;
2075 isl_vec_free(aff);
2077 n_div = qp->div->n_row;
2078 qp = substitute_non_divs(qp);
2079 qp = sort_divs(qp);
2080 if (qp && qp->div->n_row < n_div)
2081 return reduce_divs(qp);
2083 return qp;
2084 error:
2085 isl_qpolynomial_free(qp);
2086 isl_vec_free(aff);
2087 return NULL;
2090 /* Assumes each div only depends on earlier divs.
2092 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2093 int power)
2095 struct isl_qpolynomial *qp = NULL;
2096 struct isl_upoly_rec *rec;
2097 struct isl_upoly_cst *cst;
2098 int i, d;
2099 int pos;
2101 if (!div)
2102 return NULL;
2104 d = div->line - div->bmap->div;
2106 pos = isl_dim_total(div->bmap->dim) + d;
2107 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2108 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2109 div->bmap->n_div, &rec->up);
2110 if (!qp)
2111 goto error;
2113 for (i = 0; i < div->bmap->n_div; ++i)
2114 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2116 for (i = 0; i < 1 + power; ++i) {
2117 rec->p[i] = isl_upoly_zero(div->ctx);
2118 if (!rec->p[i])
2119 goto error;
2120 rec->n++;
2122 cst = isl_upoly_as_cst(rec->p[power]);
2123 isl_int_set_si(cst->n, 1);
2125 isl_div_free(div);
2127 qp = reduce_divs(qp);
2129 return qp;
2130 error:
2131 isl_qpolynomial_free(qp);
2132 isl_div_free(div);
2133 return NULL;
2136 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2138 return isl_qpolynomial_div_pow(div, 1);
2141 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2142 const isl_int n, const isl_int d)
2144 struct isl_qpolynomial *qp;
2145 struct isl_upoly_cst *cst;
2147 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2148 if (!qp)
2149 return NULL;
2151 cst = isl_upoly_as_cst(qp->upoly);
2152 isl_int_set(cst->n, n);
2153 isl_int_set(cst->d, d);
2155 return qp;
2158 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2160 struct isl_upoly_rec *rec;
2161 int i;
2163 if (!up)
2164 return -1;
2166 if (isl_upoly_is_cst(up))
2167 return 0;
2169 if (up->var < d)
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)
2175 return -1;
2177 return 0;
2180 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2182 int i, j;
2183 int d = isl_dim_total(qp->dim);
2185 if (!qp || !active)
2186 return -1;
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]))
2191 continue;
2192 active[i] = 1;
2193 break;
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)
2202 int i;
2203 int *active = NULL;
2204 int involves = 0;
2206 if (!qp)
2207 return -1;
2208 if (n == 0)
2209 return 0;
2211 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2212 return -1);
2213 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2214 type == isl_dim_set, return -1);
2216 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
2217 if (set_active(qp, active) < 0)
2218 goto error;
2220 if (type == isl_dim_set)
2221 first += isl_dim_size(qp->dim, isl_dim_param);
2222 for (i = 0; i < n; ++i)
2223 if (active[first + i]) {
2224 involves = 1;
2225 break;
2228 free(active);
2230 return involves;
2231 error:
2232 free(active);
2233 return -1;
2236 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2237 unsigned first, unsigned n)
2239 int i;
2240 struct isl_upoly_rec *rec;
2242 if (!up)
2243 return NULL;
2244 if (n == 0 || up->var < 0 || up->var < first)
2245 return up;
2246 if (up->var < first + n) {
2247 up = replace_by_constant_term(up);
2248 return isl_upoly_drop(up, first, n);
2250 up = isl_upoly_cow(up);
2251 if (!up)
2252 return NULL;
2253 up->var -= n;
2254 rec = isl_upoly_as_rec(up);
2255 if (!rec)
2256 goto error;
2258 for (i = 0; i < rec->n; ++i) {
2259 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2260 if (!rec->p[i])
2261 goto error;
2264 return up;
2265 error:
2266 isl_upoly_free(up);
2267 return NULL;
2270 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2271 __isl_take isl_qpolynomial *qp,
2272 enum isl_dim_type type, unsigned pos, const char *s)
2274 qp = isl_qpolynomial_cow(qp);
2275 if (!qp)
2276 return NULL;
2277 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2278 if (!qp->dim)
2279 goto error;
2280 return qp;
2281 error:
2282 isl_qpolynomial_free(qp);
2283 return NULL;
2286 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2287 __isl_take isl_qpolynomial *qp,
2288 enum isl_dim_type type, unsigned first, unsigned n)
2290 if (!qp)
2291 return NULL;
2292 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
2293 return qp;
2295 qp = isl_qpolynomial_cow(qp);
2296 if (!qp)
2297 return NULL;
2299 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2300 goto error);
2301 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2302 type == isl_dim_set, goto error);
2304 qp->dim = isl_dim_drop(qp->dim, type, first, n);
2305 if (!qp->dim)
2306 goto error;
2308 if (type == isl_dim_set)
2309 first += isl_dim_size(qp->dim, isl_dim_param);
2311 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2312 if (!qp->div)
2313 goto error;
2315 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2316 if (!qp->upoly)
2317 goto error;
2319 return qp;
2320 error:
2321 isl_qpolynomial_free(qp);
2322 return NULL;
2325 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2326 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2328 int i, j, k;
2329 isl_int denom;
2330 unsigned total;
2331 unsigned n_div;
2332 struct isl_upoly *up;
2334 if (!eq)
2335 goto error;
2336 if (eq->n_eq == 0) {
2337 isl_basic_set_free(eq);
2338 return qp;
2341 qp = isl_qpolynomial_cow(qp);
2342 if (!qp)
2343 goto error;
2344 qp->div = isl_mat_cow(qp->div);
2345 if (!qp->div)
2346 goto error;
2348 total = 1 + isl_dim_total(eq->dim);
2349 n_div = eq->n_div;
2350 isl_int_init(denom);
2351 for (i = 0; i < eq->n_eq; ++i) {
2352 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2353 if (j < 0 || j == 0 || j >= total)
2354 continue;
2356 for (k = 0; k < qp->div->n_row; ++k) {
2357 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2358 continue;
2359 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2360 &qp->div->row[k][0]);
2361 normalize_div(qp, k);
2364 if (isl_int_is_pos(eq->eq[i][j]))
2365 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2366 isl_int_abs(denom, eq->eq[i][j]);
2367 isl_int_set_si(eq->eq[i][j], 0);
2369 up = isl_upoly_from_affine(qp->dim->ctx,
2370 eq->eq[i], denom, total);
2371 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2372 isl_upoly_free(up);
2374 isl_int_clear(denom);
2376 if (!qp->upoly)
2377 goto error;
2379 isl_basic_set_free(eq);
2381 qp = substitute_non_divs(qp);
2382 qp = sort_divs(qp);
2384 return qp;
2385 error:
2386 isl_basic_set_free(eq);
2387 isl_qpolynomial_free(qp);
2388 return NULL;
2391 static __isl_give isl_basic_set *add_div_constraints(
2392 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2394 int i;
2395 unsigned total;
2397 if (!bset || !div)
2398 goto error;
2400 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2401 if (!bset)
2402 goto error;
2403 total = isl_basic_set_total_dim(bset);
2404 for (i = 0; i < div->n_row; ++i)
2405 if (isl_basic_set_add_div_constraints_var(bset,
2406 total - div->n_row + i, div->row[i]) < 0)
2407 goto error;
2409 isl_mat_free(div);
2410 return bset;
2411 error:
2412 isl_mat_free(div);
2413 isl_basic_set_free(bset);
2414 return NULL;
2417 /* Look for equalities among the variables shared by context and qp
2418 * and the integer divisions of qp, if any.
2419 * The equalities are then used to eliminate variables and/or integer
2420 * divisions from qp.
2422 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2423 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2425 isl_basic_set *aff;
2427 if (!qp)
2428 goto error;
2429 if (qp->div->n_row > 0) {
2430 isl_basic_set *bset;
2431 context = isl_set_add_dims(context, isl_dim_set,
2432 qp->div->n_row);
2433 bset = isl_basic_set_universe(isl_set_get_dim(context));
2434 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2435 context = isl_set_intersect(context,
2436 isl_set_from_basic_set(bset));
2439 aff = isl_set_affine_hull(context);
2440 return isl_qpolynomial_substitute_equalities(qp, aff);
2441 error:
2442 isl_qpolynomial_free(qp);
2443 isl_set_free(context);
2444 return NULL;
2447 #undef PW
2448 #define PW isl_pw_qpolynomial
2449 #undef EL
2450 #define EL isl_qpolynomial
2451 #undef IS_ZERO
2452 #define IS_ZERO is_zero
2453 #undef FIELD
2454 #define FIELD qp
2456 #include <isl_pw_templ.c>
2458 #undef UNION
2459 #define UNION isl_union_pw_qpolynomial
2460 #undef PART
2461 #define PART isl_pw_qpolynomial
2462 #undef PARTS
2463 #define PARTS pw_qpolynomial
2465 #include <isl_union_templ.c>
2467 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2469 if (!pwqp)
2470 return -1;
2472 if (pwqp->n != -1)
2473 return 0;
2475 if (!isl_set_fast_is_universe(pwqp->p[0].set))
2476 return 0;
2478 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2481 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2482 __isl_take isl_pw_qpolynomial *pwqp1,
2483 __isl_take isl_pw_qpolynomial *pwqp2)
2485 int i, j, n;
2486 struct isl_pw_qpolynomial *res;
2487 isl_set *set;
2489 if (!pwqp1 || !pwqp2)
2490 goto error;
2492 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2493 goto error);
2495 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2496 isl_pw_qpolynomial_free(pwqp2);
2497 return pwqp1;
2500 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2501 isl_pw_qpolynomial_free(pwqp1);
2502 return pwqp2;
2505 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2506 isl_pw_qpolynomial_free(pwqp1);
2507 return pwqp2;
2510 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2511 isl_pw_qpolynomial_free(pwqp2);
2512 return pwqp1;
2515 n = pwqp1->n * pwqp2->n;
2516 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2518 for (i = 0; i < pwqp1->n; ++i) {
2519 for (j = 0; j < pwqp2->n; ++j) {
2520 struct isl_set *common;
2521 struct isl_qpolynomial *prod;
2522 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2523 isl_set_copy(pwqp2->p[j].set));
2524 if (isl_set_fast_is_empty(common)) {
2525 isl_set_free(common);
2526 continue;
2529 prod = isl_qpolynomial_mul(
2530 isl_qpolynomial_copy(pwqp1->p[i].qp),
2531 isl_qpolynomial_copy(pwqp2->p[j].qp));
2533 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2537 isl_pw_qpolynomial_free(pwqp1);
2538 isl_pw_qpolynomial_free(pwqp2);
2540 return res;
2541 error:
2542 isl_pw_qpolynomial_free(pwqp1);
2543 isl_pw_qpolynomial_free(pwqp2);
2544 return NULL;
2547 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2548 __isl_take isl_pw_qpolynomial *pwqp)
2550 int i;
2552 if (!pwqp)
2553 return NULL;
2555 if (isl_pw_qpolynomial_is_zero(pwqp))
2556 return pwqp;
2558 pwqp = isl_pw_qpolynomial_cow(pwqp);
2559 if (!pwqp)
2560 return NULL;
2562 for (i = 0; i < pwqp->n; ++i) {
2563 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2564 if (!pwqp->p[i].qp)
2565 goto error;
2568 return pwqp;
2569 error:
2570 isl_pw_qpolynomial_free(pwqp);
2571 return NULL;
2574 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2575 __isl_take isl_pw_qpolynomial *pwqp1,
2576 __isl_take isl_pw_qpolynomial *pwqp2)
2578 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2581 __isl_give struct isl_upoly *isl_upoly_eval(
2582 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2584 int i;
2585 struct isl_upoly_rec *rec;
2586 struct isl_upoly *res;
2587 struct isl_upoly *base;
2589 if (isl_upoly_is_cst(up)) {
2590 isl_vec_free(vec);
2591 return up;
2594 rec = isl_upoly_as_rec(up);
2595 if (!rec)
2596 goto error;
2598 isl_assert(up->ctx, rec->n >= 1, goto error);
2600 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2602 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2603 isl_vec_copy(vec));
2605 for (i = rec->n - 2; i >= 0; --i) {
2606 res = isl_upoly_mul(res, isl_upoly_copy(base));
2607 res = isl_upoly_sum(res,
2608 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2609 isl_vec_copy(vec)));
2612 isl_upoly_free(base);
2613 isl_upoly_free(up);
2614 isl_vec_free(vec);
2615 return res;
2616 error:
2617 isl_upoly_free(up);
2618 isl_vec_free(vec);
2619 return NULL;
2622 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2623 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2625 isl_vec *ext;
2626 struct isl_upoly *up;
2627 isl_dim *dim;
2629 if (!qp || !pnt)
2630 goto error;
2631 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2633 if (qp->div->n_row == 0)
2634 ext = isl_vec_copy(pnt->vec);
2635 else {
2636 int i;
2637 unsigned dim = isl_dim_total(qp->dim);
2638 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2639 if (!ext)
2640 goto error;
2642 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2643 for (i = 0; i < qp->div->n_row; ++i) {
2644 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2645 1 + dim + i, &ext->el[1+dim+i]);
2646 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2647 qp->div->row[i][0]);
2651 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2652 if (!up)
2653 goto error;
2655 dim = isl_dim_copy(qp->dim);
2656 isl_qpolynomial_free(qp);
2657 isl_point_free(pnt);
2659 return isl_qpolynomial_alloc(dim, 0, up);
2660 error:
2661 isl_qpolynomial_free(qp);
2662 isl_point_free(pnt);
2663 return NULL;
2666 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2667 __isl_keep struct isl_upoly_cst *cst2)
2669 int cmp;
2670 isl_int t;
2671 isl_int_init(t);
2672 isl_int_mul(t, cst1->n, cst2->d);
2673 isl_int_submul(t, cst2->n, cst1->d);
2674 cmp = isl_int_sgn(t);
2675 isl_int_clear(t);
2676 return cmp;
2679 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2680 __isl_keep isl_qpolynomial *qp2)
2682 struct isl_upoly_cst *cst1, *cst2;
2684 if (!qp1 || !qp2)
2685 return -1;
2686 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2687 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2688 if (isl_qpolynomial_is_nan(qp1))
2689 return -1;
2690 if (isl_qpolynomial_is_nan(qp2))
2691 return -1;
2692 cst1 = isl_upoly_as_cst(qp1->upoly);
2693 cst2 = isl_upoly_as_cst(qp2->upoly);
2695 return isl_upoly_cmp(cst1, cst2) <= 0;
2698 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2699 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2701 struct isl_upoly_cst *cst1, *cst2;
2702 int cmp;
2704 if (!qp1 || !qp2)
2705 goto error;
2706 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2707 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2708 cst1 = isl_upoly_as_cst(qp1->upoly);
2709 cst2 = isl_upoly_as_cst(qp2->upoly);
2710 cmp = isl_upoly_cmp(cst1, cst2);
2712 if (cmp <= 0) {
2713 isl_qpolynomial_free(qp2);
2714 } else {
2715 isl_qpolynomial_free(qp1);
2716 qp1 = qp2;
2718 return qp1;
2719 error:
2720 isl_qpolynomial_free(qp1);
2721 isl_qpolynomial_free(qp2);
2722 return NULL;
2725 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2726 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2728 struct isl_upoly_cst *cst1, *cst2;
2729 int cmp;
2731 if (!qp1 || !qp2)
2732 goto error;
2733 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2734 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2735 cst1 = isl_upoly_as_cst(qp1->upoly);
2736 cst2 = isl_upoly_as_cst(qp2->upoly);
2737 cmp = isl_upoly_cmp(cst1, cst2);
2739 if (cmp >= 0) {
2740 isl_qpolynomial_free(qp2);
2741 } else {
2742 isl_qpolynomial_free(qp1);
2743 qp1 = qp2;
2745 return qp1;
2746 error:
2747 isl_qpolynomial_free(qp1);
2748 isl_qpolynomial_free(qp2);
2749 return NULL;
2752 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2753 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2754 unsigned first, unsigned n)
2756 unsigned total;
2757 unsigned g_pos;
2758 int *exp;
2760 if (n == 0)
2761 return qp;
2763 qp = isl_qpolynomial_cow(qp);
2764 if (!qp)
2765 return NULL;
2767 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2768 goto error);
2770 g_pos = pos(qp->dim, type) + first;
2772 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2773 if (!qp->div)
2774 goto error;
2776 total = qp->div->n_col - 2;
2777 if (total > g_pos) {
2778 int i;
2779 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2780 if (!exp)
2781 goto error;
2782 for (i = 0; i < total - g_pos; ++i)
2783 exp[i] = i + n;
2784 qp->upoly = expand(qp->upoly, exp, g_pos);
2785 free(exp);
2786 if (!qp->upoly)
2787 goto error;
2790 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2791 if (!qp->dim)
2792 goto error;
2794 return qp;
2795 error:
2796 isl_qpolynomial_free(qp);
2797 return NULL;
2800 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2801 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2803 unsigned pos;
2805 pos = isl_qpolynomial_dim(qp, type);
2807 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2810 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2811 __isl_take isl_pw_qpolynomial *pwqp,
2812 enum isl_dim_type type, unsigned n)
2814 unsigned pos;
2816 pos = isl_pw_qpolynomial_dim(pwqp, type);
2818 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2821 static int *reordering_move(isl_ctx *ctx,
2822 unsigned len, unsigned dst, unsigned src, unsigned n)
2824 int i;
2825 int *reordering;
2827 reordering = isl_alloc_array(ctx, int, len);
2828 if (!reordering)
2829 return NULL;
2831 if (dst <= src) {
2832 for (i = 0; i < dst; ++i)
2833 reordering[i] = i;
2834 for (i = 0; i < n; ++i)
2835 reordering[src + i] = dst + i;
2836 for (i = 0; i < src - dst; ++i)
2837 reordering[dst + i] = dst + n + i;
2838 for (i = 0; i < len - src - n; ++i)
2839 reordering[src + n + i] = src + n + i;
2840 } else {
2841 for (i = 0; i < src; ++i)
2842 reordering[i] = i;
2843 for (i = 0; i < n; ++i)
2844 reordering[src + i] = dst + i;
2845 for (i = 0; i < dst - src; ++i)
2846 reordering[src + n + i] = src + i;
2847 for (i = 0; i < len - dst - n; ++i)
2848 reordering[dst + n + i] = dst + n + i;
2851 return reordering;
2854 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2855 __isl_take isl_qpolynomial *qp,
2856 enum isl_dim_type dst_type, unsigned dst_pos,
2857 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2859 unsigned g_dst_pos;
2860 unsigned g_src_pos;
2861 int *reordering;
2863 qp = isl_qpolynomial_cow(qp);
2864 if (!qp)
2865 return NULL;
2867 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2868 goto error);
2870 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2871 g_src_pos = pos(qp->dim, src_type) + src_pos;
2872 if (dst_type > src_type)
2873 g_dst_pos -= n;
2875 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2876 if (!qp->div)
2877 goto error;
2878 qp = sort_divs(qp);
2879 if (!qp)
2880 goto error;
2882 reordering = reordering_move(qp->dim->ctx,
2883 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2884 if (!reordering)
2885 goto error;
2887 qp->upoly = reorder(qp->upoly, reordering);
2888 free(reordering);
2889 if (!qp->upoly)
2890 goto error;
2892 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2893 if (!qp->dim)
2894 goto error;
2896 return qp;
2897 error:
2898 isl_qpolynomial_free(qp);
2899 return NULL;
2902 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2903 isl_int *f, isl_int denom)
2905 struct isl_upoly *up;
2907 if (!dim)
2908 return NULL;
2910 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2912 return isl_qpolynomial_alloc(dim, 0, up);
2915 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2916 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2918 isl_int denom;
2919 isl_dim *dim;
2920 struct isl_upoly *up;
2921 isl_qpolynomial *qp;
2922 int sgn;
2924 if (!c)
2925 return NULL;
2927 isl_int_init(denom);
2929 isl_constraint_get_coefficient(c, type, pos, &denom);
2930 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2931 sgn = isl_int_sgn(denom);
2932 isl_int_abs(denom, denom);
2933 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2934 1 + isl_constraint_dim(c, isl_dim_all));
2935 if (sgn < 0)
2936 isl_int_neg(denom, denom);
2937 isl_constraint_set_coefficient(c, type, pos, denom);
2939 dim = isl_dim_copy(c->bmap->dim);
2941 isl_int_clear(denom);
2942 isl_constraint_free(c);
2944 qp = isl_qpolynomial_alloc(dim, 0, up);
2945 if (sgn > 0)
2946 qp = isl_qpolynomial_neg(qp);
2947 return qp;
2950 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2951 * in "qp" by subs[i].
2953 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2954 __isl_take isl_qpolynomial *qp,
2955 enum isl_dim_type type, unsigned first, unsigned n,
2956 __isl_keep isl_qpolynomial **subs)
2958 int i;
2959 struct isl_upoly **ups;
2961 if (n == 0)
2962 return qp;
2964 qp = isl_qpolynomial_cow(qp);
2965 if (!qp)
2966 return NULL;
2967 for (i = 0; i < n; ++i)
2968 if (!subs[i])
2969 goto error;
2971 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2972 goto error);
2974 for (i = 0; i < n; ++i)
2975 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2976 goto error);
2978 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2979 for (i = 0; i < n; ++i)
2980 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2982 first += pos(qp->dim, type);
2984 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2985 if (!ups)
2986 goto error;
2987 for (i = 0; i < n; ++i)
2988 ups[i] = subs[i]->upoly;
2990 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2992 free(ups);
2994 if (!qp->upoly)
2995 goto error;
2997 return qp;
2998 error:
2999 isl_qpolynomial_free(qp);
3000 return NULL;
3003 /* Extend "bset" with extra set dimensions for each integer division
3004 * in "qp" and then call "fn" with the extended bset and the polynomial
3005 * that results from replacing each of the integer divisions by the
3006 * corresponding extra set dimension.
3008 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3009 __isl_keep isl_basic_set *bset,
3010 int (*fn)(__isl_take isl_basic_set *bset,
3011 __isl_take isl_qpolynomial *poly, void *user), void *user)
3013 isl_dim *dim;
3014 isl_mat *div;
3015 isl_qpolynomial *poly;
3017 if (!qp || !bset)
3018 goto error;
3019 if (qp->div->n_row == 0)
3020 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3021 user);
3023 div = isl_mat_copy(qp->div);
3024 dim = isl_dim_copy(qp->dim);
3025 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3026 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3027 bset = isl_basic_set_copy(bset);
3028 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3029 bset = add_div_constraints(bset, div);
3031 return fn(bset, poly, user);
3032 error:
3033 return -1;
3036 /* Return total degree in variables first (inclusive) up to last (exclusive).
3038 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3040 int deg = -1;
3041 int i;
3042 struct isl_upoly_rec *rec;
3044 if (!up)
3045 return -2;
3046 if (isl_upoly_is_zero(up))
3047 return -1;
3048 if (isl_upoly_is_cst(up) || up->var < first)
3049 return 0;
3051 rec = isl_upoly_as_rec(up);
3052 if (!rec)
3053 return -2;
3055 for (i = 0; i < rec->n; ++i) {
3056 int d;
3058 if (isl_upoly_is_zero(rec->p[i]))
3059 continue;
3060 d = isl_upoly_degree(rec->p[i], first, last);
3061 if (up->var < last)
3062 d += i;
3063 if (d > deg)
3064 deg = d;
3067 return deg;
3070 /* Return total degree in set variables.
3072 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3074 unsigned ovar;
3075 unsigned nvar;
3077 if (!poly)
3078 return -2;
3080 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3081 nvar = isl_dim_size(poly->dim, isl_dim_set);
3082 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3085 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3086 unsigned pos, int deg)
3088 int i;
3089 struct isl_upoly_rec *rec;
3091 if (!up)
3092 return NULL;
3094 if (isl_upoly_is_cst(up) || up->var < pos) {
3095 if (deg == 0)
3096 return isl_upoly_copy(up);
3097 else
3098 return isl_upoly_zero(up->ctx);
3101 rec = isl_upoly_as_rec(up);
3102 if (!rec)
3103 return NULL;
3105 if (up->var == pos) {
3106 if (deg < rec->n)
3107 return isl_upoly_copy(rec->p[deg]);
3108 else
3109 return isl_upoly_zero(up->ctx);
3112 up = isl_upoly_copy(up);
3113 up = isl_upoly_cow(up);
3114 rec = isl_upoly_as_rec(up);
3115 if (!rec)
3116 goto error;
3118 for (i = 0; i < rec->n; ++i) {
3119 struct isl_upoly *t;
3120 t = isl_upoly_coeff(rec->p[i], pos, deg);
3121 if (!t)
3122 goto error;
3123 isl_upoly_free(rec->p[i]);
3124 rec->p[i] = t;
3127 return up;
3128 error:
3129 isl_upoly_free(up);
3130 return NULL;
3133 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3135 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3136 __isl_keep isl_qpolynomial *qp,
3137 enum isl_dim_type type, unsigned t_pos, int deg)
3139 unsigned g_pos;
3140 struct isl_upoly *up;
3141 isl_qpolynomial *c;
3143 if (!qp)
3144 return NULL;
3146 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3147 return NULL);
3149 g_pos = pos(qp->dim, type) + t_pos;
3150 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3152 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3153 if (!c)
3154 return NULL;
3155 isl_mat_free(c->div);
3156 c->div = isl_mat_copy(qp->div);
3157 if (!c->div)
3158 goto error;
3159 return c;
3160 error:
3161 isl_qpolynomial_free(c);
3162 return NULL;
3165 /* Homogenize the polynomial in the variables first (inclusive) up to
3166 * last (exclusive) by inserting powers of variable first.
3167 * Variable first is assumed not to appear in the input.
3169 __isl_give struct isl_upoly *isl_upoly_homogenize(
3170 __isl_take struct isl_upoly *up, int deg, int target,
3171 int first, int last)
3173 int i;
3174 struct isl_upoly_rec *rec;
3176 if (!up)
3177 return NULL;
3178 if (isl_upoly_is_zero(up))
3179 return up;
3180 if (deg == target)
3181 return up;
3182 if (isl_upoly_is_cst(up) || up->var < first) {
3183 struct isl_upoly *hom;
3185 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3186 if (!hom)
3187 goto error;
3188 rec = isl_upoly_as_rec(hom);
3189 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3191 return hom;
3194 up = isl_upoly_cow(up);
3195 rec = isl_upoly_as_rec(up);
3196 if (!rec)
3197 goto error;
3199 for (i = 0; i < rec->n; ++i) {
3200 if (isl_upoly_is_zero(rec->p[i]))
3201 continue;
3202 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3203 up->var < last ? deg + i : i, target,
3204 first, last);
3205 if (!rec->p[i])
3206 goto error;
3209 return up;
3210 error:
3211 isl_upoly_free(up);
3212 return NULL;
3215 /* Homogenize the polynomial in the set variables by introducing
3216 * powers of an extra set variable at position 0.
3218 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3219 __isl_take isl_qpolynomial *poly)
3221 unsigned ovar;
3222 unsigned nvar;
3223 int deg = isl_qpolynomial_degree(poly);
3225 if (deg < -1)
3226 goto error;
3228 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3229 poly = isl_qpolynomial_cow(poly);
3230 if (!poly)
3231 goto error;
3233 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3234 nvar = isl_dim_size(poly->dim, isl_dim_set);
3235 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3236 ovar, ovar + nvar);
3237 if (!poly->upoly)
3238 goto error;
3240 return poly;
3241 error:
3242 isl_qpolynomial_free(poly);
3243 return NULL;
3246 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3247 __isl_take isl_mat *div)
3249 isl_term *term;
3250 int n;
3252 if (!dim || !div)
3253 goto error;
3255 n = isl_dim_total(dim) + div->n_row;
3257 term = isl_calloc(dim->ctx, struct isl_term,
3258 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3259 if (!term)
3260 goto error;
3262 term->ref = 1;
3263 term->dim = dim;
3264 term->div = div;
3265 isl_int_init(term->n);
3266 isl_int_init(term->d);
3268 return term;
3269 error:
3270 isl_dim_free(dim);
3271 isl_mat_free(div);
3272 return NULL;
3275 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3277 if (!term)
3278 return NULL;
3280 term->ref++;
3281 return term;
3284 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3286 int i;
3287 isl_term *dup;
3288 unsigned total;
3290 if (term)
3291 return NULL;
3293 total = isl_dim_total(term->dim) + term->div->n_row;
3295 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3296 if (!dup)
3297 return NULL;
3299 isl_int_set(dup->n, term->n);
3300 isl_int_set(dup->d, term->d);
3302 for (i = 0; i < total; ++i)
3303 dup->pow[i] = term->pow[i];
3305 return dup;
3308 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3310 if (!term)
3311 return NULL;
3313 if (term->ref == 1)
3314 return term;
3315 term->ref--;
3316 return isl_term_dup(term);
3319 void isl_term_free(__isl_take isl_term *term)
3321 if (!term)
3322 return;
3324 if (--term->ref > 0)
3325 return;
3327 isl_dim_free(term->dim);
3328 isl_mat_free(term->div);
3329 isl_int_clear(term->n);
3330 isl_int_clear(term->d);
3331 free(term);
3334 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3336 if (!term)
3337 return 0;
3339 switch (type) {
3340 case isl_dim_param:
3341 case isl_dim_in:
3342 case isl_dim_out: return isl_dim_size(term->dim, type);
3343 case isl_dim_div: return term->div->n_row;
3344 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3345 default: return 0;
3349 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3351 return term ? term->dim->ctx : NULL;
3354 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3356 if (!term)
3357 return;
3358 isl_int_set(*n, term->n);
3361 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3363 if (!term)
3364 return;
3365 isl_int_set(*d, term->d);
3368 int isl_term_get_exp(__isl_keep isl_term *term,
3369 enum isl_dim_type type, unsigned pos)
3371 if (!term)
3372 return -1;
3374 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3376 if (type >= isl_dim_set)
3377 pos += isl_dim_size(term->dim, isl_dim_param);
3378 if (type >= isl_dim_div)
3379 pos += isl_dim_size(term->dim, isl_dim_set);
3381 return term->pow[pos];
3384 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3386 isl_basic_map *bmap;
3387 unsigned total;
3388 int k;
3390 if (!term)
3391 return NULL;
3393 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3394 return NULL);
3396 total = term->div->n_col - term->div->n_row - 2;
3397 /* No nested divs for now */
3398 isl_assert(term->dim->ctx,
3399 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3400 term->div->n_row) == -1,
3401 return NULL);
3403 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3404 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3405 goto error;
3407 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3409 return isl_basic_map_div(bmap, k);
3410 error:
3411 isl_basic_map_free(bmap);
3412 return NULL;
3415 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3416 int (*fn)(__isl_take isl_term *term, void *user),
3417 __isl_take isl_term *term, void *user)
3419 int i;
3420 struct isl_upoly_rec *rec;
3422 if (!up || !term)
3423 goto error;
3425 if (isl_upoly_is_zero(up))
3426 return term;
3428 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3429 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3430 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3432 if (isl_upoly_is_cst(up)) {
3433 struct isl_upoly_cst *cst;
3434 cst = isl_upoly_as_cst(up);
3435 if (!cst)
3436 goto error;
3437 term = isl_term_cow(term);
3438 if (!term)
3439 goto error;
3440 isl_int_set(term->n, cst->n);
3441 isl_int_set(term->d, cst->d);
3442 if (fn(isl_term_copy(term), user) < 0)
3443 goto error;
3444 return term;
3447 rec = isl_upoly_as_rec(up);
3448 if (!rec)
3449 goto error;
3451 for (i = 0; i < rec->n; ++i) {
3452 term = isl_term_cow(term);
3453 if (!term)
3454 goto error;
3455 term->pow[up->var] = i;
3456 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3457 if (!term)
3458 goto error;
3460 term->pow[up->var] = 0;
3462 return term;
3463 error:
3464 isl_term_free(term);
3465 return NULL;
3468 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3469 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3471 isl_term *term;
3473 if (!qp)
3474 return -1;
3476 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3477 if (!term)
3478 return -1;
3480 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3482 isl_term_free(term);
3484 return term ? 0 : -1;
3487 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3489 struct isl_upoly *up;
3490 isl_qpolynomial *qp;
3491 int i, n;
3493 if (!term)
3494 return NULL;
3496 n = isl_dim_total(term->dim) + term->div->n_row;
3498 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3499 for (i = 0; i < n; ++i) {
3500 if (!term->pow[i])
3501 continue;
3502 up = isl_upoly_mul(up,
3503 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3506 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3507 if (!qp)
3508 goto error;
3509 isl_mat_free(qp->div);
3510 qp->div = isl_mat_copy(term->div);
3511 if (!qp->div)
3512 goto error;
3514 isl_term_free(term);
3515 return qp;
3516 error:
3517 isl_qpolynomial_free(qp);
3518 isl_term_free(term);
3519 return NULL;
3522 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3523 __isl_take isl_dim *dim)
3525 int i;
3526 int extra;
3527 unsigned total;
3529 if (!qp || !dim)
3530 goto error;
3532 if (isl_dim_equal(qp->dim, dim)) {
3533 isl_dim_free(dim);
3534 return qp;
3537 qp = isl_qpolynomial_cow(qp);
3538 if (!qp)
3539 goto error;
3541 extra = isl_dim_size(dim, isl_dim_set) -
3542 isl_dim_size(qp->dim, isl_dim_set);
3543 total = isl_dim_total(qp->dim);
3544 if (qp->div->n_row) {
3545 int *exp;
3547 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3548 if (!exp)
3549 goto error;
3550 for (i = 0; i < qp->div->n_row; ++i)
3551 exp[i] = extra + i;
3552 qp->upoly = expand(qp->upoly, exp, total);
3553 free(exp);
3554 if (!qp->upoly)
3555 goto error;
3557 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3558 if (!qp->div)
3559 goto error;
3560 for (i = 0; i < qp->div->n_row; ++i)
3561 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3563 isl_dim_free(qp->dim);
3564 qp->dim = dim;
3566 return qp;
3567 error:
3568 isl_dim_free(dim);
3569 isl_qpolynomial_free(qp);
3570 return NULL;
3573 /* For each parameter or variable that does not appear in qp,
3574 * first eliminate the variable from all constraints and then set it to zero.
3576 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3577 __isl_keep isl_qpolynomial *qp)
3579 int *active = NULL;
3580 int i;
3581 int d;
3582 unsigned nparam;
3583 unsigned nvar;
3585 if (!set || !qp)
3586 goto error;
3588 d = isl_dim_total(set->dim);
3589 active = isl_calloc_array(set->ctx, int, d);
3590 if (set_active(qp, active) < 0)
3591 goto error;
3593 for (i = 0; i < d; ++i)
3594 if (!active[i])
3595 break;
3597 if (i == d) {
3598 free(active);
3599 return set;
3602 nparam = isl_dim_size(set->dim, isl_dim_param);
3603 nvar = isl_dim_size(set->dim, isl_dim_set);
3604 for (i = 0; i < nparam; ++i) {
3605 if (active[i])
3606 continue;
3607 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3608 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3610 for (i = 0; i < nvar; ++i) {
3611 if (active[nparam + i])
3612 continue;
3613 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3614 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3617 free(active);
3619 return set;
3620 error:
3621 free(active);
3622 isl_set_free(set);
3623 return NULL;
3626 struct isl_opt_data {
3627 isl_qpolynomial *qp;
3628 int first;
3629 isl_qpolynomial *opt;
3630 int max;
3633 static int opt_fn(__isl_take isl_point *pnt, void *user)
3635 struct isl_opt_data *data = (struct isl_opt_data *)user;
3636 isl_qpolynomial *val;
3638 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3639 if (data->first) {
3640 data->first = 0;
3641 data->opt = val;
3642 } else if (data->max) {
3643 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3644 } else {
3645 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3648 return 0;
3651 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3652 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3654 struct isl_opt_data data = { NULL, 1, NULL, max };
3656 if (!set || !qp)
3657 goto error;
3659 if (isl_upoly_is_cst(qp->upoly)) {
3660 isl_set_free(set);
3661 return qp;
3664 set = fix_inactive(set, qp);
3666 data.qp = qp;
3667 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3668 goto error;
3670 if (data.first)
3671 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3673 isl_set_free(set);
3674 isl_qpolynomial_free(qp);
3675 return data.opt;
3676 error:
3677 isl_set_free(set);
3678 isl_qpolynomial_free(qp);
3679 isl_qpolynomial_free(data.opt);
3680 return NULL;
3683 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3684 __isl_take isl_morph *morph)
3686 int i;
3687 int n_sub;
3688 isl_ctx *ctx;
3689 struct isl_upoly *up;
3690 unsigned n_div;
3691 struct isl_upoly **subs;
3692 isl_mat *mat;
3694 qp = isl_qpolynomial_cow(qp);
3695 if (!qp || !morph)
3696 goto error;
3698 ctx = qp->dim->ctx;
3699 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3701 n_sub = morph->inv->n_row - 1;
3702 if (morph->inv->n_row != morph->inv->n_col)
3703 n_sub += qp->div->n_row;
3704 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3705 if (!subs)
3706 goto error;
3708 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3709 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3710 morph->inv->row[0][0], morph->inv->n_col);
3711 if (morph->inv->n_row != morph->inv->n_col)
3712 for (i = 0; i < qp->div->n_row; ++i)
3713 subs[morph->inv->n_row - 1 + i] =
3714 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3716 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3718 for (i = 0; i < n_sub; ++i)
3719 isl_upoly_free(subs[i]);
3720 free(subs);
3722 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3723 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3724 qp->div = isl_mat_product(qp->div, mat);
3725 isl_dim_free(qp->dim);
3726 qp->dim = isl_dim_copy(morph->ran->dim);
3728 if (!qp->upoly || !qp->div || !qp->dim)
3729 goto error;
3731 isl_morph_free(morph);
3733 return qp;
3734 error:
3735 isl_qpolynomial_free(qp);
3736 isl_morph_free(morph);
3737 return NULL;
3740 static int neg_entry(void **entry, void *user)
3742 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3744 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3746 return *pwqp ? 0 : -1;
3749 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3750 __isl_take isl_union_pw_qpolynomial *upwqp)
3752 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3753 if (!upwqp)
3754 return NULL;
3756 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3757 &neg_entry, NULL) < 0)
3758 goto error;
3760 return upwqp;
3761 error:
3762 isl_union_pw_qpolynomial_free(upwqp);
3763 return NULL;
3766 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3767 __isl_take isl_union_pw_qpolynomial *upwqp1,
3768 __isl_take isl_union_pw_qpolynomial *upwqp2)
3770 return isl_union_pw_qpolynomial_add(upwqp1,
3771 isl_union_pw_qpolynomial_neg(upwqp2));
3774 static int mul_entry(void **entry, void *user)
3776 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3777 uint32_t hash;
3778 struct isl_hash_table_entry *entry2;
3779 isl_pw_qpolynomial *pwpq = *entry;
3780 int empty;
3782 hash = isl_dim_get_hash(pwpq->dim);
3783 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3784 hash, &has_dim, pwpq->dim, 0);
3785 if (!entry2)
3786 return 0;
3788 pwpq = isl_pw_qpolynomial_copy(pwpq);
3789 pwpq = isl_pw_qpolynomial_mul(pwpq,
3790 isl_pw_qpolynomial_copy(entry2->data));
3792 empty = isl_pw_qpolynomial_is_zero(pwpq);
3793 if (empty < 0) {
3794 isl_pw_qpolynomial_free(pwpq);
3795 return -1;
3797 if (empty) {
3798 isl_pw_qpolynomial_free(pwpq);
3799 return 0;
3802 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3804 return 0;
3807 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3808 __isl_take isl_union_pw_qpolynomial *upwqp1,
3809 __isl_take isl_union_pw_qpolynomial *upwqp2)
3811 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3814 /* Reorder the columns of the given div definitions according to the
3815 * given reordering.
3817 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3818 __isl_take isl_reordering *r)
3820 int i, j;
3821 isl_mat *mat;
3822 int extra;
3824 if (!div || !r)
3825 goto error;
3827 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3828 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3829 if (!mat)
3830 goto error;
3832 for (i = 0; i < div->n_row; ++i) {
3833 isl_seq_cpy(mat->row[i], div->row[i], 2);
3834 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3835 for (j = 0; j < r->len; ++j)
3836 isl_int_set(mat->row[i][2 + r->pos[j]],
3837 div->row[i][2 + j]);
3840 isl_reordering_free(r);
3841 isl_mat_free(div);
3842 return mat;
3843 error:
3844 isl_reordering_free(r);
3845 isl_mat_free(div);
3846 return NULL;
3849 /* Reorder the dimension of "qp" according to the given reordering.
3851 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3852 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3854 qp = isl_qpolynomial_cow(qp);
3855 if (!qp)
3856 goto error;
3858 r = isl_reordering_extend(r, qp->div->n_row);
3859 if (!r)
3860 goto error;
3862 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3863 if (!qp->div)
3864 goto error;
3866 qp->upoly = reorder(qp->upoly, r->pos);
3867 if (!qp->upoly)
3868 goto error;
3870 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3872 isl_reordering_free(r);
3873 return qp;
3874 error:
3875 isl_qpolynomial_free(qp);
3876 isl_reordering_free(r);
3877 return NULL;
3880 struct isl_split_periods_data {
3881 int max_periods;
3882 isl_pw_qpolynomial *res;
3885 /* Create a slice where the integer division "div" has the fixed value "v".
3886 * In particular, if "div" refers to floor(f/m), then create a slice
3888 * m v <= f <= m v + (m - 1)
3890 * or
3892 * f - m v >= 0
3893 * -f + m v + (m - 1) >= 0
3895 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3896 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3898 int total;
3899 isl_basic_set *bset = NULL;
3900 int k;
3902 if (!dim || !qp)
3903 goto error;
3905 total = isl_dim_total(dim);
3906 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3908 k = isl_basic_set_alloc_inequality(bset);
3909 if (k < 0)
3910 goto error;
3911 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3912 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3914 k = isl_basic_set_alloc_inequality(bset);
3915 if (k < 0)
3916 goto error;
3917 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3918 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3919 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3920 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3922 isl_dim_free(dim);
3923 return isl_set_from_basic_set(bset);
3924 error:
3925 isl_basic_set_free(bset);
3926 isl_dim_free(dim);
3927 return NULL;
3930 static int split_periods(__isl_take isl_set *set,
3931 __isl_take isl_qpolynomial *qp, void *user);
3933 /* Create a slice of the domain "set" such that integer division "div"
3934 * has the fixed value "v" and add the results to data->res,
3935 * replacing the integer division by "v" in "qp".
3937 static int set_div(__isl_take isl_set *set,
3938 __isl_take isl_qpolynomial *qp, int div, isl_int v,
3939 struct isl_split_periods_data *data)
3941 int i;
3942 int total;
3943 isl_set *slice;
3944 struct isl_upoly *cst;
3946 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3947 set = isl_set_intersect(set, slice);
3949 if (!qp)
3950 goto error;
3952 total = isl_dim_total(qp->dim);
3954 for (i = div + 1; i < qp->div->n_row; ++i) {
3955 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
3956 continue;
3957 isl_int_addmul(qp->div->row[i][1],
3958 qp->div->row[i][2 + total + div], v);
3959 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
3962 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3963 qp = substitute_div(qp, div, cst);
3965 return split_periods(set, qp, data);
3966 error:
3967 isl_set_free(set);
3968 isl_qpolynomial_free(qp);
3969 return -1;
3972 /* Split the domain "set" such that integer division "div"
3973 * has a fixed value (ranging from "min" to "max") on each slice
3974 * and add the results to data->res.
3976 static int split_div(__isl_take isl_set *set,
3977 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3978 struct isl_split_periods_data *data)
3980 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3981 isl_set *set_i = isl_set_copy(set);
3982 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3984 if (set_div(set_i, qp_i, div, min, data) < 0)
3985 goto error;
3987 isl_set_free(set);
3988 isl_qpolynomial_free(qp);
3989 return 0;
3990 error:
3991 isl_set_free(set);
3992 isl_qpolynomial_free(qp);
3993 return -1;
3996 /* If "qp" refers to any integer division
3997 * that can only attain "max_periods" distinct values on "set"
3998 * then split the domain along those distinct values.
3999 * Add the results (or the original if no splitting occurs)
4000 * to data->res.
4002 static int split_periods(__isl_take isl_set *set,
4003 __isl_take isl_qpolynomial *qp, void *user)
4005 int i;
4006 isl_pw_qpolynomial *pwqp;
4007 struct isl_split_periods_data *data;
4008 isl_int min, max;
4009 int total;
4010 int r = 0;
4012 data = (struct isl_split_periods_data *)user;
4014 if (!set || !qp)
4015 goto error;
4017 if (qp->div->n_row == 0) {
4018 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4019 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4020 return 0;
4023 isl_int_init(min);
4024 isl_int_init(max);
4025 total = isl_dim_total(qp->dim);
4026 for (i = 0; i < qp->div->n_row; ++i) {
4027 enum isl_lp_result lp_res;
4029 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4030 qp->div->n_row) != -1)
4031 continue;
4033 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4034 set->ctx->one, &min, NULL, NULL);
4035 if (lp_res == isl_lp_error)
4036 goto error2;
4037 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4038 continue;
4039 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4041 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4042 set->ctx->one, &max, NULL, NULL);
4043 if (lp_res == isl_lp_error)
4044 goto error2;
4045 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4046 continue;
4047 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4049 isl_int_sub(max, max, min);
4050 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4051 isl_int_add(max, max, min);
4052 break;
4056 if (i < qp->div->n_row) {
4057 r = split_div(set, qp, i, min, max, data);
4058 } else {
4059 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4060 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4063 isl_int_clear(max);
4064 isl_int_clear(min);
4066 return r;
4067 error2:
4068 isl_int_clear(max);
4069 isl_int_clear(min);
4070 error:
4071 isl_set_free(set);
4072 isl_qpolynomial_free(qp);
4073 return -1;
4076 /* If any quasi-polynomial in pwqp refers to any integer division
4077 * that can only attain "max_periods" distinct values on its domain
4078 * then split the domain along those distinct values.
4080 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4081 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4083 struct isl_split_periods_data data;
4085 data.max_periods = max_periods;
4086 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4088 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4089 goto error;
4091 isl_pw_qpolynomial_free(pwqp);
4093 return data.res;
4094 error:
4095 isl_pw_qpolynomial_free(data.res);
4096 isl_pw_qpolynomial_free(pwqp);
4097 return NULL;
4100 /* Construct a piecewise quasipolynomial that is constant on the given
4101 * domain. In particular, it is
4102 * 0 if cst == 0
4103 * 1 if cst == 1
4104 * infinity if cst == -1
4106 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4107 __isl_take isl_basic_set *bset, int cst)
4109 isl_dim *dim;
4110 isl_qpolynomial *qp;
4112 if (!bset)
4113 return NULL;
4115 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4116 dim = isl_basic_set_get_dim(bset);
4117 if (cst < 0)
4118 qp = isl_qpolynomial_infty(dim);
4119 else if (cst == 0)
4120 qp = isl_qpolynomial_zero(dim);
4121 else
4122 qp = isl_qpolynomial_one(dim);
4123 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4126 /* Factor bset, call fn on each of the factors and return the product.
4128 * If no factors can be found, simply call fn on the input.
4129 * Otherwise, construct the factors based on the factorizer,
4130 * call fn on each factor and compute the product.
4132 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4133 __isl_take isl_basic_set *bset,
4134 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4136 int i, n;
4137 isl_dim *dim;
4138 isl_set *set;
4139 isl_factorizer *f;
4140 isl_qpolynomial *qp;
4141 isl_pw_qpolynomial *pwqp;
4142 unsigned nparam;
4143 unsigned nvar;
4145 f = isl_basic_set_factorizer(bset);
4146 if (!f)
4147 goto error;
4148 if (f->n_group == 0) {
4149 isl_factorizer_free(f);
4150 return fn(bset);
4153 nparam = isl_basic_set_dim(bset, isl_dim_param);
4154 nvar = isl_basic_set_dim(bset, isl_dim_set);
4156 dim = isl_basic_set_get_dim(bset);
4157 dim = isl_dim_domain(dim);
4158 set = isl_set_universe(isl_dim_copy(dim));
4159 qp = isl_qpolynomial_one(dim);
4160 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4162 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4164 for (i = 0, n = 0; i < f->n_group; ++i) {
4165 isl_basic_set *bset_i;
4166 isl_pw_qpolynomial *pwqp_i;
4168 bset_i = isl_basic_set_copy(bset);
4169 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4170 nparam + n + f->len[i], nvar - n - f->len[i]);
4171 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4172 nparam, n);
4173 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4174 n + f->len[i], nvar - n - f->len[i]);
4175 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4177 pwqp_i = fn(bset_i);
4178 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4180 n += f->len[i];
4183 isl_basic_set_free(bset);
4184 isl_factorizer_free(f);
4186 return pwqp;
4187 error:
4188 isl_basic_set_free(bset);
4189 return NULL;
4192 /* Factor bset, call fn on each of the factors and return the product.
4193 * The function is assumed to evaluate to zero on empty domains,
4194 * to one on zero-dimensional domains and to infinity on unbounded domains
4195 * and will not be called explicitly on zero-dimensional or unbounded domains.
4197 * We first check for some special cases and remove all equalities.
4198 * Then we hand over control to compressed_multiplicative_call.
4200 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4201 __isl_take isl_basic_set *bset,
4202 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4204 int bounded;
4205 isl_morph *morph;
4206 isl_pw_qpolynomial *pwqp;
4207 unsigned orig_nvar, final_nvar;
4209 if (!bset)
4210 return NULL;
4212 if (isl_basic_set_fast_is_empty(bset))
4213 return constant_on_domain(bset, 0);
4215 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4217 if (orig_nvar == 0)
4218 return constant_on_domain(bset, 1);
4220 bounded = isl_basic_set_is_bounded(bset);
4221 if (bounded < 0)
4222 goto error;
4223 if (!bounded)
4224 return constant_on_domain(bset, -1);
4226 if (bset->n_eq == 0)
4227 return compressed_multiplicative_call(bset, fn);
4229 morph = isl_basic_set_full_compression(bset);
4230 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4232 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4234 pwqp = compressed_multiplicative_call(bset, fn);
4236 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4237 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4238 morph = isl_morph_inverse(morph);
4240 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4242 return pwqp;
4243 error:
4244 isl_basic_set_free(bset);
4245 return NULL;
4248 /* Drop all floors in "qp", turning each integer division [a/m] into
4249 * a rational division a/m. If "down" is set, then the integer division
4250 * is replaces by (a-(m-1))/m instead.
4252 static __isl_give isl_qpolynomial *qp_drop_floors(
4253 __isl_take isl_qpolynomial *qp, int down)
4255 int i;
4256 struct isl_upoly *s;
4258 if (!qp)
4259 return NULL;
4260 if (qp->div->n_row == 0)
4261 return qp;
4263 qp = isl_qpolynomial_cow(qp);
4264 if (!qp)
4265 return NULL;
4267 for (i = qp->div->n_row - 1; i >= 0; --i) {
4268 if (down) {
4269 isl_int_sub(qp->div->row[i][1],
4270 qp->div->row[i][1], qp->div->row[i][0]);
4271 isl_int_add_ui(qp->div->row[i][1],
4272 qp->div->row[i][1], 1);
4274 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4275 qp->div->row[i][0], qp->div->n_col - 1);
4276 qp = substitute_div(qp, i, s);
4277 if (!qp)
4278 return NULL;
4281 return qp;
4284 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4285 * a rational division a/m.
4287 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4288 __isl_take isl_pw_qpolynomial *pwqp)
4290 int i;
4292 if (!pwqp)
4293 return NULL;
4295 if (isl_pw_qpolynomial_is_zero(pwqp))
4296 return pwqp;
4298 pwqp = isl_pw_qpolynomial_cow(pwqp);
4299 if (!pwqp)
4300 return NULL;
4302 for (i = 0; i < pwqp->n; ++i) {
4303 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4304 if (!pwqp->p[i].qp)
4305 goto error;
4308 return pwqp;
4309 error:
4310 isl_pw_qpolynomial_free(pwqp);
4311 return NULL;
4314 /* Adjust all the integer divisions in "qp" such that they are at least
4315 * one over the given orthant (identified by "signs"). This ensures
4316 * that they will still be non-negative even after subtracting (m-1)/m.
4318 * In particular, f is replaced by f' + v, changing f = [a/m]
4319 * to f' = [(a - m v)/m].
4320 * If the constant term k in a is smaller than m,
4321 * the constant term of v is set to floor(k/m) - 1.
4322 * For any other term, if the coefficient c and the variable x have
4323 * the same sign, then no changes are needed.
4324 * Otherwise, if the variable is positive (and c is negative),
4325 * then the coefficient of x in v is set to floor(c/m).
4326 * If the variable is negative (and c is positive),
4327 * then the coefficient of x in v is set to ceil(c/m).
4329 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4330 int *signs)
4332 int i, j;
4333 int total;
4334 isl_vec *v = NULL;
4335 struct isl_upoly *s;
4337 qp = isl_qpolynomial_cow(qp);
4338 if (!qp)
4339 return NULL;
4340 qp->div = isl_mat_cow(qp->div);
4341 if (!qp->div)
4342 goto error;
4344 total = isl_dim_total(qp->dim);
4345 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4347 for (i = 0; i < qp->div->n_row; ++i) {
4348 isl_int *row = qp->div->row[i];
4349 v = isl_vec_clr(v);
4350 if (!v)
4351 goto error;
4352 if (isl_int_lt(row[1], row[0])) {
4353 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4354 isl_int_sub_ui(v->el[0], v->el[0], 1);
4355 isl_int_submul(row[1], row[0], v->el[0]);
4357 for (j = 0; j < total; ++j) {
4358 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4359 continue;
4360 if (signs[j] < 0)
4361 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4362 else
4363 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4364 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4366 for (j = 0; j < i; ++j) {
4367 if (isl_int_sgn(row[2 + total + j]) >= 0)
4368 continue;
4369 isl_int_fdiv_q(v->el[1 + total + j],
4370 row[2 + total + j], row[0]);
4371 isl_int_submul(row[2 + total + j],
4372 row[0], v->el[1 + total + j]);
4374 for (j = i + 1; j < qp->div->n_row; ++j) {
4375 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4376 continue;
4377 isl_seq_combine(qp->div->row[j] + 1,
4378 qp->div->ctx->one, qp->div->row[j] + 1,
4379 qp->div->row[j][2 + total + i], v->el, v->size);
4381 isl_int_set_si(v->el[1 + total + i], 1);
4382 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4383 qp->div->ctx->one, v->size);
4384 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4385 isl_upoly_free(s);
4386 if (!qp->upoly)
4387 goto error;
4390 isl_vec_free(v);
4391 return qp;
4392 error:
4393 isl_vec_free(v);
4394 isl_qpolynomial_free(qp);
4395 return NULL;
4398 struct isl_to_poly_data {
4399 int sign;
4400 isl_pw_qpolynomial *res;
4401 isl_qpolynomial *qp;
4404 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4405 * We first make all integer divisions positive and then split the
4406 * quasipolynomials into terms with sign data->sign (the direction
4407 * of the requested approximation) and terms with the opposite sign.
4408 * In the first set of terms, each integer division [a/m] is
4409 * overapproximated by a/m, while in the second it is underapproximated
4410 * by (a-(m-1))/m.
4412 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4413 void *user)
4415 struct isl_to_poly_data *data = user;
4416 isl_pw_qpolynomial *t;
4417 isl_qpolynomial *qp, *up, *down;
4419 qp = isl_qpolynomial_copy(data->qp);
4420 qp = make_divs_pos(qp, signs);
4422 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4423 up = qp_drop_floors(up, 0);
4424 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4425 down = qp_drop_floors(down, 1);
4427 isl_qpolynomial_free(qp);
4428 qp = isl_qpolynomial_add(up, down);
4430 t = isl_pw_qpolynomial_alloc(orthant, qp);
4431 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4433 return 0;
4436 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4437 * the polynomial will be an overapproximation. If "sign" is negative,
4438 * it will be an underapproximation. If "sign" is zero, the approximation
4439 * will lie somewhere in between.
4441 * In particular, is sign == 0, we simply drop the floors, turning
4442 * the integer divisions into rational divisions.
4443 * Otherwise, we split the domains into orthants, make all integer divisions
4444 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4445 * depending on the requested sign and the sign of the term in which
4446 * the integer division appears.
4448 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4449 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4451 int i;
4452 struct isl_to_poly_data data;
4454 if (sign == 0)
4455 return pwqp_drop_floors(pwqp);
4457 if (!pwqp)
4458 return NULL;
4460 data.sign = sign;
4461 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4463 for (i = 0; i < pwqp->n; ++i) {
4464 if (pwqp->p[i].qp->div->n_row == 0) {
4465 isl_pw_qpolynomial *t;
4466 t = isl_pw_qpolynomial_alloc(
4467 isl_set_copy(pwqp->p[i].set),
4468 isl_qpolynomial_copy(pwqp->p[i].qp));
4469 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4470 continue;
4472 data.qp = pwqp->p[i].qp;
4473 if (isl_set_foreach_orthant(pwqp->p[i].set,
4474 &to_polynomial_on_orthant, &data) < 0)
4475 goto error;
4478 isl_pw_qpolynomial_free(pwqp);
4480 return data.res;
4481 error:
4482 isl_pw_qpolynomial_free(pwqp);
4483 isl_pw_qpolynomial_free(data.res);
4484 return NULL;
4487 static int poly_entry(void **entry, void *user)
4489 int *sign = user;
4490 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4492 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4494 return *pwqp ? 0 : -1;
4497 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4498 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4500 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4501 if (!upwqp)
4502 return NULL;
4504 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4505 &poly_entry, &sign) < 0)
4506 goto error;
4508 return upwqp;
4509 error:
4510 isl_union_pw_qpolynomial_free(upwqp);
4511 return NULL;
4514 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4515 __isl_take isl_qpolynomial *qp)
4517 int i, k;
4518 isl_dim *dim;
4519 isl_vec *aff = NULL;
4520 isl_basic_map *bmap = NULL;
4521 unsigned pos;
4522 unsigned n_div;
4524 if (!qp)
4525 return NULL;
4526 if (!isl_upoly_is_affine(qp->upoly))
4527 isl_die(qp->dim->ctx, isl_error_invalid,
4528 "input quasi-polynomial not affine", goto error);
4529 aff = isl_qpolynomial_extract_affine(qp);
4530 if (!aff)
4531 goto error;
4532 dim = isl_qpolynomial_get_dim(qp);
4533 dim = isl_dim_from_domain(dim);
4534 pos = 1 + isl_dim_offset(dim, isl_dim_out);
4535 dim = isl_dim_add(dim, isl_dim_out, 1);
4536 n_div = qp->div->n_row;
4537 bmap = isl_basic_map_alloc_dim(dim, n_div, 1, 2 * n_div);
4539 for (i = 0; i < n_div; ++i) {
4540 k = isl_basic_map_alloc_div(bmap);
4541 if (k < 0)
4542 goto error;
4543 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4544 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4545 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4546 goto error;
4548 k = isl_basic_map_alloc_equality(bmap);
4549 if (k < 0)
4550 goto error;
4551 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4552 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4553 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4555 isl_vec_free(aff);
4556 isl_qpolynomial_free(qp);
4557 bmap = isl_basic_map_finalize(bmap);
4558 return bmap;
4559 error:
4560 isl_vec_free(aff);
4561 isl_qpolynomial_free(qp);
4562 isl_basic_map_free(bmap);
4563 return NULL;