add isl_basic_set_max
[isl.git] / isl_polynomial.c
blobff3196f3f117c951f0abee0ca4d1052ba73d74ce
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_div_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_range.h>
24 #include <isl_local_space_private.h>
25 #include <isl_aff_private.h>
27 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
29 switch (type) {
30 case isl_dim_param: return 0;
31 case isl_dim_in: return dim->nparam;
32 case isl_dim_out: return dim->nparam + dim->n_in;
33 default: return 0;
37 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
39 if (!up)
40 return -1;
42 return up->var < 0;
45 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
47 if (!up)
48 return NULL;
50 isl_assert(up->ctx, up->var < 0, return NULL);
52 return (struct isl_upoly_cst *)up;
55 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
57 if (!up)
58 return NULL;
60 isl_assert(up->ctx, up->var >= 0, return NULL);
62 return (struct isl_upoly_rec *)up;
65 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
66 __isl_keep struct isl_upoly *up2)
68 int i;
69 struct isl_upoly_rec *rec1, *rec2;
71 if (!up1 || !up2)
72 return -1;
73 if (up1 == up2)
74 return 1;
75 if (up1->var != up2->var)
76 return 0;
77 if (isl_upoly_is_cst(up1)) {
78 struct isl_upoly_cst *cst1, *cst2;
79 cst1 = isl_upoly_as_cst(up1);
80 cst2 = isl_upoly_as_cst(up2);
81 if (!cst1 || !cst2)
82 return -1;
83 return isl_int_eq(cst1->n, cst2->n) &&
84 isl_int_eq(cst1->d, cst2->d);
87 rec1 = isl_upoly_as_rec(up1);
88 rec2 = isl_upoly_as_rec(up2);
89 if (!rec1 || !rec2)
90 return -1;
92 if (rec1->n != rec2->n)
93 return 0;
95 for (i = 0; i < rec1->n; ++i) {
96 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
97 if (eq < 0 || !eq)
98 return eq;
101 return 1;
104 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
106 struct isl_upoly_cst *cst;
108 if (!up)
109 return -1;
110 if (!isl_upoly_is_cst(up))
111 return 0;
113 cst = isl_upoly_as_cst(up);
114 if (!cst)
115 return -1;
117 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
120 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
122 struct isl_upoly_cst *cst;
124 if (!up)
125 return 0;
126 if (!isl_upoly_is_cst(up))
127 return 0;
129 cst = isl_upoly_as_cst(up);
130 if (!cst)
131 return 0;
133 return isl_int_sgn(cst->n);
136 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
138 struct isl_upoly_cst *cst;
140 if (!up)
141 return -1;
142 if (!isl_upoly_is_cst(up))
143 return 0;
145 cst = isl_upoly_as_cst(up);
146 if (!cst)
147 return -1;
149 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
152 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
154 struct isl_upoly_cst *cst;
156 if (!up)
157 return -1;
158 if (!isl_upoly_is_cst(up))
159 return 0;
161 cst = isl_upoly_as_cst(up);
162 if (!cst)
163 return -1;
165 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
168 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
170 struct isl_upoly_cst *cst;
172 if (!up)
173 return -1;
174 if (!isl_upoly_is_cst(up))
175 return 0;
177 cst = isl_upoly_as_cst(up);
178 if (!cst)
179 return -1;
181 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
184 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
186 struct isl_upoly_cst *cst;
188 if (!up)
189 return -1;
190 if (!isl_upoly_is_cst(up))
191 return 0;
193 cst = isl_upoly_as_cst(up);
194 if (!cst)
195 return -1;
197 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
200 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
202 struct isl_upoly_cst *cst;
204 if (!up)
205 return -1;
206 if (!isl_upoly_is_cst(up))
207 return 0;
209 cst = isl_upoly_as_cst(up);
210 if (!cst)
211 return -1;
213 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
216 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
218 struct isl_upoly_cst *cst;
220 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
221 if (!cst)
222 return NULL;
224 cst->up.ref = 1;
225 cst->up.ctx = ctx;
226 isl_ctx_ref(ctx);
227 cst->up.var = -1;
229 isl_int_init(cst->n);
230 isl_int_init(cst->d);
232 return cst;
235 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
237 struct isl_upoly_cst *cst;
239 cst = isl_upoly_cst_alloc(ctx);
240 if (!cst)
241 return NULL;
243 isl_int_set_si(cst->n, 0);
244 isl_int_set_si(cst->d, 1);
246 return &cst->up;
249 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
251 struct isl_upoly_cst *cst;
253 cst = isl_upoly_cst_alloc(ctx);
254 if (!cst)
255 return NULL;
257 isl_int_set_si(cst->n, 1);
258 isl_int_set_si(cst->d, 1);
260 return &cst->up;
263 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
265 struct isl_upoly_cst *cst;
267 cst = isl_upoly_cst_alloc(ctx);
268 if (!cst)
269 return NULL;
271 isl_int_set_si(cst->n, 1);
272 isl_int_set_si(cst->d, 0);
274 return &cst->up;
277 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
279 struct isl_upoly_cst *cst;
281 cst = isl_upoly_cst_alloc(ctx);
282 if (!cst)
283 return NULL;
285 isl_int_set_si(cst->n, -1);
286 isl_int_set_si(cst->d, 0);
288 return &cst->up;
291 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
293 struct isl_upoly_cst *cst;
295 cst = isl_upoly_cst_alloc(ctx);
296 if (!cst)
297 return NULL;
299 isl_int_set_si(cst->n, 0);
300 isl_int_set_si(cst->d, 0);
302 return &cst->up;
305 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
306 isl_int n, isl_int d)
308 struct isl_upoly_cst *cst;
310 cst = isl_upoly_cst_alloc(ctx);
311 if (!cst)
312 return NULL;
314 isl_int_set(cst->n, n);
315 isl_int_set(cst->d, d);
317 return &cst->up;
320 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
321 int var, int size)
323 struct isl_upoly_rec *rec;
325 isl_assert(ctx, var >= 0, return NULL);
326 isl_assert(ctx, size >= 0, return NULL);
327 rec = isl_calloc(ctx, struct isl_upoly_rec,
328 sizeof(struct isl_upoly_rec) +
329 (size - 1) * sizeof(struct isl_upoly *));
330 if (!rec)
331 return NULL;
333 rec->up.ref = 1;
334 rec->up.ctx = ctx;
335 isl_ctx_ref(ctx);
336 rec->up.var = var;
338 rec->n = 0;
339 rec->size = size;
341 return rec;
344 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
345 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
347 qp = isl_qpolynomial_cow(qp);
348 if (!qp || !dim)
349 goto error;
351 isl_dim_free(qp->dim);
352 qp->dim = dim;
354 return qp;
355 error:
356 isl_qpolynomial_free(qp);
357 isl_dim_free(dim);
358 return NULL;
361 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
363 return qp ? qp->dim->ctx : NULL;
366 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
368 return qp ? isl_dim_copy(qp->dim) : NULL;
371 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
372 enum isl_dim_type type)
374 return qp ? isl_dim_size(qp->dim, type) : 0;
377 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
379 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
382 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
384 return qp ? isl_upoly_is_one(qp->upoly) : -1;
387 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
389 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
392 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
394 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
397 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
399 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
402 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
404 return qp ? isl_upoly_sgn(qp->upoly) : 0;
407 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
409 isl_int_clear(cst->n);
410 isl_int_clear(cst->d);
413 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
415 int i;
417 for (i = 0; i < rec->n; ++i)
418 isl_upoly_free(rec->p[i]);
421 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
423 if (!up)
424 return NULL;
426 up->ref++;
427 return up;
430 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
432 struct isl_upoly_cst *cst;
433 struct isl_upoly_cst *dup;
435 cst = isl_upoly_as_cst(up);
436 if (!cst)
437 return NULL;
439 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
440 if (!dup)
441 return NULL;
442 isl_int_set(dup->n, cst->n);
443 isl_int_set(dup->d, cst->d);
445 return &dup->up;
448 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
450 int i;
451 struct isl_upoly_rec *rec;
452 struct isl_upoly_rec *dup;
454 rec = isl_upoly_as_rec(up);
455 if (!rec)
456 return NULL;
458 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
459 if (!dup)
460 return NULL;
462 for (i = 0; i < rec->n; ++i) {
463 dup->p[i] = isl_upoly_copy(rec->p[i]);
464 if (!dup->p[i])
465 goto error;
466 dup->n++;
469 return &dup->up;
470 error:
471 isl_upoly_free(&dup->up);
472 return NULL;
475 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
477 struct isl_upoly *dup;
479 if (!up)
480 return NULL;
482 if (isl_upoly_is_cst(up))
483 return isl_upoly_dup_cst(up);
484 else
485 return isl_upoly_dup_rec(up);
488 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
490 if (!up)
491 return NULL;
493 if (up->ref == 1)
494 return up;
495 up->ref--;
496 return isl_upoly_dup(up);
499 void isl_upoly_free(__isl_take struct isl_upoly *up)
501 if (!up)
502 return;
504 if (--up->ref > 0)
505 return;
507 if (up->var < 0)
508 upoly_free_cst((struct isl_upoly_cst *)up);
509 else
510 upoly_free_rec((struct isl_upoly_rec *)up);
512 isl_ctx_deref(up->ctx);
513 free(up);
516 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
518 isl_int gcd;
520 isl_int_init(gcd);
521 isl_int_gcd(gcd, cst->n, cst->d);
522 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
523 isl_int_divexact(cst->n, cst->n, gcd);
524 isl_int_divexact(cst->d, cst->d, gcd);
526 isl_int_clear(gcd);
529 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
530 __isl_take struct isl_upoly *up2)
532 struct isl_upoly_cst *cst1;
533 struct isl_upoly_cst *cst2;
535 up1 = isl_upoly_cow(up1);
536 if (!up1 || !up2)
537 goto error;
539 cst1 = isl_upoly_as_cst(up1);
540 cst2 = isl_upoly_as_cst(up2);
542 if (isl_int_eq(cst1->d, cst2->d))
543 isl_int_add(cst1->n, cst1->n, cst2->n);
544 else {
545 isl_int_mul(cst1->n, cst1->n, cst2->d);
546 isl_int_addmul(cst1->n, cst2->n, cst1->d);
547 isl_int_mul(cst1->d, cst1->d, cst2->d);
550 isl_upoly_cst_reduce(cst1);
552 isl_upoly_free(up2);
553 return up1;
554 error:
555 isl_upoly_free(up1);
556 isl_upoly_free(up2);
557 return NULL;
560 static __isl_give struct isl_upoly *replace_by_zero(
561 __isl_take struct isl_upoly *up)
563 struct isl_ctx *ctx;
565 if (!up)
566 return NULL;
567 ctx = up->ctx;
568 isl_upoly_free(up);
569 return isl_upoly_zero(ctx);
572 static __isl_give struct isl_upoly *replace_by_constant_term(
573 __isl_take struct isl_upoly *up)
575 struct isl_upoly_rec *rec;
576 struct isl_upoly *cst;
578 if (!up)
579 return NULL;
581 rec = isl_upoly_as_rec(up);
582 if (!rec)
583 goto error;
584 cst = isl_upoly_copy(rec->p[0]);
585 isl_upoly_free(up);
586 return cst;
587 error:
588 isl_upoly_free(up);
589 return NULL;
592 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
593 __isl_take struct isl_upoly *up2)
595 int i;
596 struct isl_upoly_rec *rec1, *rec2;
598 if (!up1 || !up2)
599 goto error;
601 if (isl_upoly_is_nan(up1)) {
602 isl_upoly_free(up2);
603 return up1;
606 if (isl_upoly_is_nan(up2)) {
607 isl_upoly_free(up1);
608 return up2;
611 if (isl_upoly_is_zero(up1)) {
612 isl_upoly_free(up1);
613 return up2;
616 if (isl_upoly_is_zero(up2)) {
617 isl_upoly_free(up2);
618 return up1;
621 if (up1->var < up2->var)
622 return isl_upoly_sum(up2, up1);
624 if (up2->var < up1->var) {
625 struct isl_upoly_rec *rec;
626 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
627 isl_upoly_free(up1);
628 return up2;
630 up1 = isl_upoly_cow(up1);
631 rec = isl_upoly_as_rec(up1);
632 if (!rec)
633 goto error;
634 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
635 if (rec->n == 1)
636 up1 = replace_by_constant_term(up1);
637 return up1;
640 if (isl_upoly_is_cst(up1))
641 return isl_upoly_sum_cst(up1, up2);
643 rec1 = isl_upoly_as_rec(up1);
644 rec2 = isl_upoly_as_rec(up2);
645 if (!rec1 || !rec2)
646 goto error;
648 if (rec1->n < rec2->n)
649 return isl_upoly_sum(up2, up1);
651 up1 = isl_upoly_cow(up1);
652 rec1 = isl_upoly_as_rec(up1);
653 if (!rec1)
654 goto error;
656 for (i = rec2->n - 1; i >= 0; --i) {
657 rec1->p[i] = isl_upoly_sum(rec1->p[i],
658 isl_upoly_copy(rec2->p[i]));
659 if (!rec1->p[i])
660 goto error;
661 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
662 isl_upoly_free(rec1->p[i]);
663 rec1->n--;
667 if (rec1->n == 0)
668 up1 = replace_by_zero(up1);
669 else if (rec1->n == 1)
670 up1 = replace_by_constant_term(up1);
672 isl_upoly_free(up2);
674 return up1;
675 error:
676 isl_upoly_free(up1);
677 isl_upoly_free(up2);
678 return NULL;
681 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
682 __isl_take struct isl_upoly *up, isl_int v)
684 struct isl_upoly_cst *cst;
686 up = isl_upoly_cow(up);
687 if (!up)
688 return NULL;
690 cst = isl_upoly_as_cst(up);
692 isl_int_addmul(cst->n, cst->d, v);
694 return up;
697 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
698 __isl_take struct isl_upoly *up, isl_int v)
700 struct isl_upoly_rec *rec;
702 if (!up)
703 return NULL;
705 if (isl_upoly_is_cst(up))
706 return isl_upoly_cst_add_isl_int(up, v);
708 up = isl_upoly_cow(up);
709 rec = isl_upoly_as_rec(up);
710 if (!rec)
711 goto error;
713 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
714 if (!rec->p[0])
715 goto error;
717 return up;
718 error:
719 isl_upoly_free(up);
720 return NULL;
723 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
724 __isl_take struct isl_upoly *up, isl_int v)
726 struct isl_upoly_cst *cst;
728 if (isl_upoly_is_zero(up))
729 return up;
731 up = isl_upoly_cow(up);
732 if (!up)
733 return NULL;
735 cst = isl_upoly_as_cst(up);
737 isl_int_mul(cst->n, cst->n, v);
739 return up;
742 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
743 __isl_take struct isl_upoly *up, isl_int v)
745 int i;
746 struct isl_upoly_rec *rec;
748 if (!up)
749 return NULL;
751 if (isl_upoly_is_cst(up))
752 return isl_upoly_cst_mul_isl_int(up, v);
754 up = isl_upoly_cow(up);
755 rec = isl_upoly_as_rec(up);
756 if (!rec)
757 goto error;
759 for (i = 0; i < rec->n; ++i) {
760 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
761 if (!rec->p[i])
762 goto error;
765 return up;
766 error:
767 isl_upoly_free(up);
768 return NULL;
771 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
772 __isl_take struct isl_upoly *up2)
774 struct isl_upoly_cst *cst1;
775 struct isl_upoly_cst *cst2;
777 up1 = isl_upoly_cow(up1);
778 if (!up1 || !up2)
779 goto error;
781 cst1 = isl_upoly_as_cst(up1);
782 cst2 = isl_upoly_as_cst(up2);
784 isl_int_mul(cst1->n, cst1->n, cst2->n);
785 isl_int_mul(cst1->d, cst1->d, cst2->d);
787 isl_upoly_cst_reduce(cst1);
789 isl_upoly_free(up2);
790 return up1;
791 error:
792 isl_upoly_free(up1);
793 isl_upoly_free(up2);
794 return NULL;
797 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
798 __isl_take struct isl_upoly *up2)
800 struct isl_upoly_rec *rec1;
801 struct isl_upoly_rec *rec2;
802 struct isl_upoly_rec *res;
803 int i, j;
804 int size;
806 rec1 = isl_upoly_as_rec(up1);
807 rec2 = isl_upoly_as_rec(up2);
808 if (!rec1 || !rec2)
809 goto error;
810 size = rec1->n + rec2->n - 1;
811 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
812 if (!res)
813 goto error;
815 for (i = 0; i < rec1->n; ++i) {
816 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
817 isl_upoly_copy(rec1->p[i]));
818 if (!res->p[i])
819 goto error;
820 res->n++;
822 for (; i < size; ++i) {
823 res->p[i] = isl_upoly_zero(up1->ctx);
824 if (!res->p[i])
825 goto error;
826 res->n++;
828 for (i = 0; i < rec1->n; ++i) {
829 for (j = 1; j < rec2->n; ++j) {
830 struct isl_upoly *up;
831 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
832 isl_upoly_copy(rec1->p[i]));
833 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
834 if (!res->p[i + j])
835 goto error;
839 isl_upoly_free(up1);
840 isl_upoly_free(up2);
842 return &res->up;
843 error:
844 isl_upoly_free(up1);
845 isl_upoly_free(up2);
846 isl_upoly_free(&res->up);
847 return NULL;
850 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
851 __isl_take struct isl_upoly *up2)
853 if (!up1 || !up2)
854 goto error;
856 if (isl_upoly_is_nan(up1)) {
857 isl_upoly_free(up2);
858 return up1;
861 if (isl_upoly_is_nan(up2)) {
862 isl_upoly_free(up1);
863 return up2;
866 if (isl_upoly_is_zero(up1)) {
867 isl_upoly_free(up2);
868 return up1;
871 if (isl_upoly_is_zero(up2)) {
872 isl_upoly_free(up1);
873 return up2;
876 if (isl_upoly_is_one(up1)) {
877 isl_upoly_free(up1);
878 return up2;
881 if (isl_upoly_is_one(up2)) {
882 isl_upoly_free(up2);
883 return up1;
886 if (up1->var < up2->var)
887 return isl_upoly_mul(up2, up1);
889 if (up2->var < up1->var) {
890 int i;
891 struct isl_upoly_rec *rec;
892 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
893 isl_ctx *ctx = up1->ctx;
894 isl_upoly_free(up1);
895 isl_upoly_free(up2);
896 return isl_upoly_nan(ctx);
898 up1 = isl_upoly_cow(up1);
899 rec = isl_upoly_as_rec(up1);
900 if (!rec)
901 goto error;
903 for (i = 0; i < rec->n; ++i) {
904 rec->p[i] = isl_upoly_mul(rec->p[i],
905 isl_upoly_copy(up2));
906 if (!rec->p[i])
907 goto error;
909 isl_upoly_free(up2);
910 return up1;
913 if (isl_upoly_is_cst(up1))
914 return isl_upoly_mul_cst(up1, up2);
916 return isl_upoly_mul_rec(up1, up2);
917 error:
918 isl_upoly_free(up1);
919 isl_upoly_free(up2);
920 return NULL;
923 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
924 unsigned power)
926 struct isl_upoly *res;
928 if (!up)
929 return NULL;
930 if (power == 1)
931 return up;
933 if (power % 2)
934 res = isl_upoly_copy(up);
935 else
936 res = isl_upoly_one(up->ctx);
938 while (power >>= 1) {
939 up = isl_upoly_mul(up, isl_upoly_copy(up));
940 if (power % 2)
941 res = isl_upoly_mul(res, isl_upoly_copy(up));
944 isl_upoly_free(up);
945 return res;
948 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
949 unsigned n_div, __isl_take struct isl_upoly *up)
951 struct isl_qpolynomial *qp = NULL;
952 unsigned total;
954 if (!dim || !up)
955 goto error;
957 total = isl_dim_total(dim);
959 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
960 if (!qp)
961 goto error;
963 qp->ref = 1;
964 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
965 if (!qp->div)
966 goto error;
968 qp->dim = dim;
969 qp->upoly = up;
971 return qp;
972 error:
973 isl_dim_free(dim);
974 isl_upoly_free(up);
975 isl_qpolynomial_free(qp);
976 return NULL;
979 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
981 if (!qp)
982 return NULL;
984 qp->ref++;
985 return qp;
988 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
990 struct isl_qpolynomial *dup;
992 if (!qp)
993 return NULL;
995 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
996 isl_upoly_copy(qp->upoly));
997 if (!dup)
998 return NULL;
999 isl_mat_free(dup->div);
1000 dup->div = isl_mat_copy(qp->div);
1001 if (!dup->div)
1002 goto error;
1004 return dup;
1005 error:
1006 isl_qpolynomial_free(dup);
1007 return NULL;
1010 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1012 if (!qp)
1013 return NULL;
1015 if (qp->ref == 1)
1016 return qp;
1017 qp->ref--;
1018 return isl_qpolynomial_dup(qp);
1021 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1023 if (!qp)
1024 return;
1026 if (--qp->ref > 0)
1027 return;
1029 isl_dim_free(qp->dim);
1030 isl_mat_free(qp->div);
1031 isl_upoly_free(qp->upoly);
1033 free(qp);
1036 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1038 int i;
1039 struct isl_upoly *up;
1040 struct isl_upoly_rec *rec;
1041 struct isl_upoly_cst *cst;
1043 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1044 if (!rec)
1045 return NULL;
1046 for (i = 0; i < 1 + power; ++i) {
1047 rec->p[i] = isl_upoly_zero(ctx);
1048 if (!rec->p[i])
1049 goto error;
1050 rec->n++;
1052 cst = isl_upoly_as_cst(rec->p[power]);
1053 isl_int_set_si(cst->n, 1);
1055 return &rec->up;
1056 error:
1057 isl_upoly_free(&rec->up);
1058 return NULL;
1061 /* r array maps original positions to new positions.
1063 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1064 int *r)
1066 int i;
1067 struct isl_upoly_rec *rec;
1068 struct isl_upoly *base;
1069 struct isl_upoly *res;
1071 if (isl_upoly_is_cst(up))
1072 return up;
1074 rec = isl_upoly_as_rec(up);
1075 if (!rec)
1076 goto error;
1078 isl_assert(up->ctx, rec->n >= 1, goto error);
1080 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1081 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1083 for (i = rec->n - 2; i >= 0; --i) {
1084 res = isl_upoly_mul(res, isl_upoly_copy(base));
1085 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1088 isl_upoly_free(base);
1089 isl_upoly_free(up);
1091 return res;
1092 error:
1093 isl_upoly_free(up);
1094 return NULL;
1097 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1099 int n_row, n_col;
1100 int equal;
1102 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1103 div1->n_col >= div2->n_col, return -1);
1105 if (div1->n_row == div2->n_row)
1106 return isl_mat_is_equal(div1, div2);
1108 n_row = div1->n_row;
1109 n_col = div1->n_col;
1110 div1->n_row = div2->n_row;
1111 div1->n_col = div2->n_col;
1113 equal = isl_mat_is_equal(div1, div2);
1115 div1->n_row = n_row;
1116 div1->n_col = n_col;
1118 return equal;
1121 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1123 int li, lj;
1125 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1126 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1128 if (li != lj)
1129 return li - lj;
1131 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1134 struct isl_div_sort_info {
1135 isl_mat *div;
1136 int row;
1139 static int div_sort_cmp(const void *p1, const void *p2)
1141 const struct isl_div_sort_info *i1, *i2;
1142 i1 = (const struct isl_div_sort_info *) p1;
1143 i2 = (const struct isl_div_sort_info *) p2;
1145 return cmp_row(i1->div, i1->row, i2->row);
1148 /* Sort divs and remove duplicates.
1150 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1152 int i;
1153 int skip;
1154 int len;
1155 struct isl_div_sort_info *array = NULL;
1156 int *pos = NULL, *at = NULL;
1157 int *reordering = NULL;
1158 unsigned div_pos;
1160 if (!qp)
1161 return NULL;
1162 if (qp->div->n_row <= 1)
1163 return qp;
1165 div_pos = isl_dim_total(qp->dim);
1167 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1168 qp->div->n_row);
1169 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1170 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1171 len = qp->div->n_col - 2;
1172 reordering = isl_alloc_array(qp->div->ctx, int, len);
1173 if (!array || !pos || !at || !reordering)
1174 goto error;
1176 for (i = 0; i < qp->div->n_row; ++i) {
1177 array[i].div = qp->div;
1178 array[i].row = i;
1179 pos[i] = i;
1180 at[i] = i;
1183 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1184 div_sort_cmp);
1186 for (i = 0; i < div_pos; ++i)
1187 reordering[i] = i;
1189 for (i = 0; i < qp->div->n_row; ++i) {
1190 if (pos[array[i].row] == i)
1191 continue;
1192 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1193 pos[at[i]] = pos[array[i].row];
1194 at[pos[array[i].row]] = at[i];
1195 at[i] = array[i].row;
1196 pos[array[i].row] = i;
1199 skip = 0;
1200 for (i = 0; i < len - div_pos; ++i) {
1201 if (i > 0 &&
1202 isl_seq_eq(qp->div->row[i - skip - 1],
1203 qp->div->row[i - skip], qp->div->n_col)) {
1204 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1205 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1206 2 + div_pos + i - skip);
1207 qp->div = isl_mat_drop_cols(qp->div,
1208 2 + div_pos + i - skip, 1);
1209 skip++;
1211 reordering[div_pos + array[i].row] = div_pos + i - skip;
1214 qp->upoly = reorder(qp->upoly, reordering);
1216 if (!qp->upoly || !qp->div)
1217 goto error;
1219 free(at);
1220 free(pos);
1221 free(array);
1222 free(reordering);
1224 return qp;
1225 error:
1226 free(at);
1227 free(pos);
1228 free(array);
1229 free(reordering);
1230 isl_qpolynomial_free(qp);
1231 return NULL;
1234 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1235 int *exp, int first)
1237 int i;
1238 struct isl_upoly_rec *rec;
1240 if (isl_upoly_is_cst(up))
1241 return up;
1243 if (up->var < first)
1244 return up;
1246 if (exp[up->var - first] == up->var - first)
1247 return up;
1249 up = isl_upoly_cow(up);
1250 if (!up)
1251 goto error;
1253 up->var = exp[up->var - first] + first;
1255 rec = isl_upoly_as_rec(up);
1256 if (!rec)
1257 goto error;
1259 for (i = 0; i < rec->n; ++i) {
1260 rec->p[i] = expand(rec->p[i], exp, first);
1261 if (!rec->p[i])
1262 goto error;
1265 return up;
1266 error:
1267 isl_upoly_free(up);
1268 return NULL;
1271 static __isl_give isl_qpolynomial *with_merged_divs(
1272 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1273 __isl_take isl_qpolynomial *qp2),
1274 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1276 int *exp1 = NULL;
1277 int *exp2 = NULL;
1278 isl_mat *div = NULL;
1280 qp1 = isl_qpolynomial_cow(qp1);
1281 qp2 = isl_qpolynomial_cow(qp2);
1283 if (!qp1 || !qp2)
1284 goto error;
1286 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1287 qp1->div->n_col >= qp2->div->n_col, goto error);
1289 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1290 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1291 if (!exp1 || !exp2)
1292 goto error;
1294 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1295 if (!div)
1296 goto error;
1298 isl_mat_free(qp1->div);
1299 qp1->div = isl_mat_copy(div);
1300 isl_mat_free(qp2->div);
1301 qp2->div = isl_mat_copy(div);
1303 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1304 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1306 if (!qp1->upoly || !qp2->upoly)
1307 goto error;
1309 isl_mat_free(div);
1310 free(exp1);
1311 free(exp2);
1313 return fn(qp1, qp2);
1314 error:
1315 isl_mat_free(div);
1316 free(exp1);
1317 free(exp2);
1318 isl_qpolynomial_free(qp1);
1319 isl_qpolynomial_free(qp2);
1320 return NULL;
1323 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1324 __isl_take isl_qpolynomial *qp2)
1326 qp1 = isl_qpolynomial_cow(qp1);
1328 if (!qp1 || !qp2)
1329 goto error;
1331 if (qp1->div->n_row < qp2->div->n_row)
1332 return isl_qpolynomial_add(qp2, qp1);
1334 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1335 if (!compatible_divs(qp1->div, qp2->div))
1336 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1338 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1339 if (!qp1->upoly)
1340 goto error;
1342 isl_qpolynomial_free(qp2);
1344 return qp1;
1345 error:
1346 isl_qpolynomial_free(qp1);
1347 isl_qpolynomial_free(qp2);
1348 return NULL;
1351 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1352 __isl_keep isl_set *dom,
1353 __isl_take isl_qpolynomial *qp1,
1354 __isl_take isl_qpolynomial *qp2)
1356 qp1 = isl_qpolynomial_add(qp1, qp2);
1357 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1358 return qp1;
1361 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1362 __isl_take isl_qpolynomial *qp2)
1364 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1367 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1368 __isl_take isl_qpolynomial *qp, isl_int v)
1370 if (isl_int_is_zero(v))
1371 return qp;
1373 qp = isl_qpolynomial_cow(qp);
1374 if (!qp)
1375 return NULL;
1377 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1378 if (!qp->upoly)
1379 goto error;
1381 return qp;
1382 error:
1383 isl_qpolynomial_free(qp);
1384 return NULL;
1388 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1390 if (!qp)
1391 return NULL;
1393 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1396 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1397 __isl_take isl_qpolynomial *qp, isl_int v)
1399 if (isl_int_is_one(v))
1400 return qp;
1402 if (qp && isl_int_is_zero(v)) {
1403 isl_qpolynomial *zero;
1404 zero = isl_qpolynomial_zero(isl_dim_copy(qp->dim));
1405 isl_qpolynomial_free(qp);
1406 return zero;
1409 qp = isl_qpolynomial_cow(qp);
1410 if (!qp)
1411 return NULL;
1413 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1414 if (!qp->upoly)
1415 goto error;
1417 return qp;
1418 error:
1419 isl_qpolynomial_free(qp);
1420 return NULL;
1423 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1424 __isl_take isl_qpolynomial *qp2)
1426 qp1 = isl_qpolynomial_cow(qp1);
1428 if (!qp1 || !qp2)
1429 goto error;
1431 if (qp1->div->n_row < qp2->div->n_row)
1432 return isl_qpolynomial_mul(qp2, qp1);
1434 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1435 if (!compatible_divs(qp1->div, qp2->div))
1436 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1438 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1439 if (!qp1->upoly)
1440 goto error;
1442 isl_qpolynomial_free(qp2);
1444 return qp1;
1445 error:
1446 isl_qpolynomial_free(qp1);
1447 isl_qpolynomial_free(qp2);
1448 return NULL;
1451 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1452 unsigned power)
1454 qp = isl_qpolynomial_cow(qp);
1456 if (!qp)
1457 return NULL;
1459 qp->upoly = isl_upoly_pow(qp->upoly, power);
1460 if (!qp->upoly)
1461 goto error;
1463 return qp;
1464 error:
1465 isl_qpolynomial_free(qp);
1466 return NULL;
1469 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1471 if (!dim)
1472 return NULL;
1473 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1476 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1478 if (!dim)
1479 return NULL;
1480 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1483 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1485 if (!dim)
1486 return NULL;
1487 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1490 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1492 if (!dim)
1493 return NULL;
1494 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1497 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1499 if (!dim)
1500 return NULL;
1501 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1504 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1505 isl_int v)
1507 struct isl_qpolynomial *qp;
1508 struct isl_upoly_cst *cst;
1510 if (!dim)
1511 return NULL;
1513 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1514 if (!qp)
1515 return NULL;
1517 cst = isl_upoly_as_cst(qp->upoly);
1518 isl_int_set(cst->n, v);
1520 return qp;
1523 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1524 isl_int *n, isl_int *d)
1526 struct isl_upoly_cst *cst;
1528 if (!qp)
1529 return -1;
1531 if (!isl_upoly_is_cst(qp->upoly))
1532 return 0;
1534 cst = isl_upoly_as_cst(qp->upoly);
1535 if (!cst)
1536 return -1;
1538 if (n)
1539 isl_int_set(*n, cst->n);
1540 if (d)
1541 isl_int_set(*d, cst->d);
1543 return 1;
1546 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1548 int is_cst;
1549 struct isl_upoly_rec *rec;
1551 if (!up)
1552 return -1;
1554 if (up->var < 0)
1555 return 1;
1557 rec = isl_upoly_as_rec(up);
1558 if (!rec)
1559 return -1;
1561 if (rec->n > 2)
1562 return 0;
1564 isl_assert(up->ctx, rec->n > 1, return -1);
1566 is_cst = isl_upoly_is_cst(rec->p[1]);
1567 if (is_cst < 0)
1568 return -1;
1569 if (!is_cst)
1570 return 0;
1572 return isl_upoly_is_affine(rec->p[0]);
1575 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1577 if (!qp)
1578 return -1;
1580 if (qp->div->n_row > 0)
1581 return 0;
1583 return isl_upoly_is_affine(qp->upoly);
1586 static void update_coeff(__isl_keep isl_vec *aff,
1587 __isl_keep struct isl_upoly_cst *cst, int pos)
1589 isl_int gcd;
1590 isl_int f;
1592 if (isl_int_is_zero(cst->n))
1593 return;
1595 isl_int_init(gcd);
1596 isl_int_init(f);
1597 isl_int_gcd(gcd, cst->d, aff->el[0]);
1598 isl_int_divexact(f, cst->d, gcd);
1599 isl_int_divexact(gcd, aff->el[0], gcd);
1600 isl_seq_scale(aff->el, aff->el, f, aff->size);
1601 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1602 isl_int_clear(gcd);
1603 isl_int_clear(f);
1606 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1607 __isl_keep isl_vec *aff)
1609 struct isl_upoly_cst *cst;
1610 struct isl_upoly_rec *rec;
1612 if (!up || !aff)
1613 return -1;
1615 if (up->var < 0) {
1616 struct isl_upoly_cst *cst;
1618 cst = isl_upoly_as_cst(up);
1619 if (!cst)
1620 return -1;
1621 update_coeff(aff, cst, 0);
1622 return 0;
1625 rec = isl_upoly_as_rec(up);
1626 if (!rec)
1627 return -1;
1628 isl_assert(up->ctx, rec->n == 2, return -1);
1630 cst = isl_upoly_as_cst(rec->p[1]);
1631 if (!cst)
1632 return -1;
1633 update_coeff(aff, cst, 1 + up->var);
1635 return isl_upoly_update_affine(rec->p[0], aff);
1638 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1639 __isl_keep isl_qpolynomial *qp)
1641 isl_vec *aff;
1642 unsigned d;
1644 if (!qp)
1645 return NULL;
1647 d = isl_dim_total(qp->dim);
1648 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1649 if (!aff)
1650 return NULL;
1652 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1653 isl_int_set_si(aff->el[0], 1);
1655 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1656 goto error;
1658 return aff;
1659 error:
1660 isl_vec_free(aff);
1661 return NULL;
1664 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1665 __isl_keep isl_qpolynomial *qp2)
1667 if (!qp1 || !qp2)
1668 return -1;
1670 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1673 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1675 int i;
1676 struct isl_upoly_rec *rec;
1678 if (isl_upoly_is_cst(up)) {
1679 struct isl_upoly_cst *cst;
1680 cst = isl_upoly_as_cst(up);
1681 if (!cst)
1682 return;
1683 isl_int_lcm(*d, *d, cst->d);
1684 return;
1687 rec = isl_upoly_as_rec(up);
1688 if (!rec)
1689 return;
1691 for (i = 0; i < rec->n; ++i)
1692 upoly_update_den(rec->p[i], d);
1695 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1697 isl_int_set_si(*d, 1);
1698 if (!qp)
1699 return;
1700 upoly_update_den(qp->upoly, d);
1703 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1704 int pos, int power)
1706 struct isl_ctx *ctx;
1708 if (!dim)
1709 return NULL;
1711 ctx = dim->ctx;
1713 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1716 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1717 enum isl_dim_type type, unsigned pos)
1719 if (!dim)
1720 return NULL;
1722 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1723 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1725 if (type == isl_dim_set)
1726 pos += isl_dim_size(dim, isl_dim_param);
1728 return isl_qpolynomial_var_pow(dim, pos, 1);
1729 error:
1730 isl_dim_free(dim);
1731 return NULL;
1734 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1735 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1737 int i;
1738 struct isl_upoly_rec *rec;
1739 struct isl_upoly *base, *res;
1741 if (!up)
1742 return NULL;
1744 if (isl_upoly_is_cst(up))
1745 return up;
1747 if (up->var < first)
1748 return up;
1750 rec = isl_upoly_as_rec(up);
1751 if (!rec)
1752 goto error;
1754 isl_assert(up->ctx, rec->n >= 1, goto error);
1756 if (up->var >= first + n)
1757 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1758 else
1759 base = isl_upoly_copy(subs[up->var - first]);
1761 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1762 for (i = rec->n - 2; i >= 0; --i) {
1763 struct isl_upoly *t;
1764 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1765 res = isl_upoly_mul(res, isl_upoly_copy(base));
1766 res = isl_upoly_sum(res, t);
1769 isl_upoly_free(base);
1770 isl_upoly_free(up);
1772 return res;
1773 error:
1774 isl_upoly_free(up);
1775 return NULL;
1778 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1779 isl_int denom, unsigned len)
1781 int i;
1782 struct isl_upoly *up;
1784 isl_assert(ctx, len >= 1, return NULL);
1786 up = isl_upoly_rat_cst(ctx, f[0], denom);
1787 for (i = 0; i < len - 1; ++i) {
1788 struct isl_upoly *t;
1789 struct isl_upoly *c;
1791 if (isl_int_is_zero(f[1 + i]))
1792 continue;
1794 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1795 t = isl_upoly_var_pow(ctx, i, 1);
1796 t = isl_upoly_mul(c, t);
1797 up = isl_upoly_sum(up, t);
1800 return up;
1803 /* Remove common factor of non-constant terms and denominator.
1805 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1807 isl_ctx *ctx = qp->div->ctx;
1808 unsigned total = qp->div->n_col - 2;
1810 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1811 isl_int_gcd(ctx->normalize_gcd,
1812 ctx->normalize_gcd, qp->div->row[div][0]);
1813 if (isl_int_is_one(ctx->normalize_gcd))
1814 return;
1816 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1817 ctx->normalize_gcd, total);
1818 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1819 ctx->normalize_gcd);
1820 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1821 ctx->normalize_gcd);
1824 /* Replace the integer division identified by "div" by the polynomial "s".
1825 * The integer division is assumed not to appear in the definition
1826 * of any other integer divisions.
1828 static __isl_give isl_qpolynomial *substitute_div(
1829 __isl_take isl_qpolynomial *qp,
1830 int div, __isl_take struct isl_upoly *s)
1832 int i;
1833 int total;
1834 int *reordering;
1836 if (!qp || !s)
1837 goto error;
1839 qp = isl_qpolynomial_cow(qp);
1840 if (!qp)
1841 goto error;
1843 total = isl_dim_total(qp->dim);
1844 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1845 if (!qp->upoly)
1846 goto error;
1848 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1849 if (!reordering)
1850 goto error;
1851 for (i = 0; i < total + div; ++i)
1852 reordering[i] = i;
1853 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1854 reordering[i] = i - 1;
1855 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1856 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1857 qp->upoly = reorder(qp->upoly, reordering);
1858 free(reordering);
1860 if (!qp->upoly || !qp->div)
1861 goto error;
1863 isl_upoly_free(s);
1864 return qp;
1865 error:
1866 isl_qpolynomial_free(qp);
1867 isl_upoly_free(s);
1868 return NULL;
1871 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1872 * divisions because d is equal to 1 by their definition, i.e., e.
1874 static __isl_give isl_qpolynomial *substitute_non_divs(
1875 __isl_take isl_qpolynomial *qp)
1877 int i, j;
1878 int total;
1879 struct isl_upoly *s;
1881 if (!qp)
1882 return NULL;
1884 total = isl_dim_total(qp->dim);
1885 for (i = 0; qp && i < qp->div->n_row; ++i) {
1886 if (!isl_int_is_one(qp->div->row[i][0]))
1887 continue;
1888 for (j = i + 1; j < qp->div->n_row; ++j) {
1889 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1890 continue;
1891 isl_seq_combine(qp->div->row[j] + 1,
1892 qp->div->ctx->one, qp->div->row[j] + 1,
1893 qp->div->row[j][2 + total + i],
1894 qp->div->row[i] + 1, 1 + total + i);
1895 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1896 normalize_div(qp, j);
1898 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1899 qp->div->row[i][0], qp->div->n_col - 1);
1900 qp = substitute_div(qp, i, s);
1901 --i;
1904 return qp;
1907 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1908 * with d the denominator. When replacing the coefficient e of x by
1909 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1910 * inside the division, so we need to add floor(e/d) * x outside.
1911 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1912 * to adjust the coefficient of x in each later div that depends on the
1913 * current div "div" and also in the affine expression "aff"
1914 * (if it too depends on "div").
1916 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1917 __isl_keep isl_vec *aff)
1919 int i, j;
1920 isl_int v;
1921 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1923 isl_int_init(v);
1924 for (i = 0; i < 1 + total + div; ++i) {
1925 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1926 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1927 continue;
1928 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1929 isl_int_fdiv_r(qp->div->row[div][1 + i],
1930 qp->div->row[div][1 + i], qp->div->row[div][0]);
1931 if (!isl_int_is_zero(aff->el[1 + total + div]))
1932 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1933 for (j = div + 1; j < qp->div->n_row; ++j) {
1934 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1935 continue;
1936 isl_int_addmul(qp->div->row[j][1 + i],
1937 v, qp->div->row[j][2 + total + div]);
1940 isl_int_clear(v);
1943 /* Check if the last non-zero coefficient is bigger that half of the
1944 * denominator. If so, we will invert the div to further reduce the number
1945 * of distinct divs that may appear.
1946 * If the last non-zero coefficient is exactly half the denominator,
1947 * then we continue looking for earlier coefficients that are bigger
1948 * than half the denominator.
1950 static int needs_invert(__isl_keep isl_mat *div, int row)
1952 int i;
1953 int cmp;
1955 for (i = div->n_col - 1; i >= 1; --i) {
1956 if (isl_int_is_zero(div->row[row][i]))
1957 continue;
1958 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1959 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
1960 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
1961 if (cmp)
1962 return cmp > 0;
1963 if (i == 1)
1964 return 1;
1967 return 0;
1970 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
1971 * We only invert the coefficients of e (and the coefficient of q in
1972 * later divs and in "aff"). After calling this function, the
1973 * coefficients of e should be reduced again.
1975 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
1976 __isl_keep isl_vec *aff)
1978 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1980 isl_seq_neg(qp->div->row[div] + 1,
1981 qp->div->row[div] + 1, qp->div->n_col - 1);
1982 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
1983 isl_int_add(qp->div->row[div][1],
1984 qp->div->row[div][1], qp->div->row[div][0]);
1985 if (!isl_int_is_zero(aff->el[1 + total + div]))
1986 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
1987 isl_mat_col_mul(qp->div, 2 + total + div,
1988 qp->div->ctx->negone, 2 + total + div);
1991 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
1992 * in the interval [0, d-1], with d the denominator and such that the
1993 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
1995 * After the reduction, some divs may have become redundant or identical,
1996 * so we call substitute_non_divs and sort_divs. If these functions
1997 * eliminate divs or merge two or more divs into one, the coefficients
1998 * of the enclosing divs may have to be reduced again, so we call
1999 * ourselves recursively if the number of divs decreases.
2001 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2003 int i, j;
2004 isl_vec *aff = NULL;
2005 struct isl_upoly *s;
2006 unsigned n_div;
2008 if (!qp)
2009 return NULL;
2011 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2012 aff = isl_vec_clr(aff);
2013 if (!aff)
2014 goto error;
2016 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2018 for (i = 0; i < qp->div->n_row; ++i) {
2019 normalize_div(qp, i);
2020 reduce_div(qp, i, aff);
2021 if (needs_invert(qp->div, i)) {
2022 invert_div(qp, i, aff);
2023 reduce_div(qp, i, aff);
2027 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2028 qp->div->ctx->one, aff->size);
2029 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2030 isl_upoly_free(s);
2031 if (!qp->upoly)
2032 goto error;
2034 isl_vec_free(aff);
2036 n_div = qp->div->n_row;
2037 qp = substitute_non_divs(qp);
2038 qp = sort_divs(qp);
2039 if (qp && qp->div->n_row < n_div)
2040 return reduce_divs(qp);
2042 return qp;
2043 error:
2044 isl_qpolynomial_free(qp);
2045 isl_vec_free(aff);
2046 return NULL;
2049 /* Assumes each div only depends on earlier divs.
2051 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2052 int power)
2054 struct isl_qpolynomial *qp = NULL;
2055 struct isl_upoly_rec *rec;
2056 struct isl_upoly_cst *cst;
2057 int i, d;
2058 int pos;
2060 if (!div)
2061 return NULL;
2063 d = div->line - div->bmap->div;
2065 pos = isl_dim_total(div->bmap->dim) + d;
2066 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2067 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2068 div->bmap->n_div, &rec->up);
2069 if (!qp)
2070 goto error;
2072 for (i = 0; i < div->bmap->n_div; ++i)
2073 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2075 for (i = 0; i < 1 + power; ++i) {
2076 rec->p[i] = isl_upoly_zero(div->ctx);
2077 if (!rec->p[i])
2078 goto error;
2079 rec->n++;
2081 cst = isl_upoly_as_cst(rec->p[power]);
2082 isl_int_set_si(cst->n, 1);
2084 isl_div_free(div);
2086 qp = reduce_divs(qp);
2088 return qp;
2089 error:
2090 isl_qpolynomial_free(qp);
2091 isl_div_free(div);
2092 return NULL;
2095 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2097 return isl_qpolynomial_div_pow(div, 1);
2100 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2101 const isl_int n, const isl_int d)
2103 struct isl_qpolynomial *qp;
2104 struct isl_upoly_cst *cst;
2106 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2107 if (!qp)
2108 return NULL;
2110 cst = isl_upoly_as_cst(qp->upoly);
2111 isl_int_set(cst->n, n);
2112 isl_int_set(cst->d, d);
2114 return qp;
2117 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2119 struct isl_upoly_rec *rec;
2120 int i;
2122 if (!up)
2123 return -1;
2125 if (isl_upoly_is_cst(up))
2126 return 0;
2128 if (up->var < d)
2129 active[up->var] = 1;
2131 rec = isl_upoly_as_rec(up);
2132 for (i = 0; i < rec->n; ++i)
2133 if (up_set_active(rec->p[i], active, d) < 0)
2134 return -1;
2136 return 0;
2139 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2141 int i, j;
2142 int d = isl_dim_total(qp->dim);
2144 if (!qp || !active)
2145 return -1;
2147 for (i = 0; i < d; ++i)
2148 for (j = 0; j < qp->div->n_row; ++j) {
2149 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2150 continue;
2151 active[i] = 1;
2152 break;
2155 return up_set_active(qp->upoly, active, d);
2158 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2159 enum isl_dim_type type, unsigned first, unsigned n)
2161 int i;
2162 int *active = NULL;
2163 int involves = 0;
2165 if (!qp)
2166 return -1;
2167 if (n == 0)
2168 return 0;
2170 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2171 return -1);
2172 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2173 type == isl_dim_set, return -1);
2175 active = isl_calloc_array(qp->dim->ctx, int, isl_dim_total(qp->dim));
2176 if (set_active(qp, active) < 0)
2177 goto error;
2179 if (type == isl_dim_set)
2180 first += isl_dim_size(qp->dim, isl_dim_param);
2181 for (i = 0; i < n; ++i)
2182 if (active[first + i]) {
2183 involves = 1;
2184 break;
2187 free(active);
2189 return involves;
2190 error:
2191 free(active);
2192 return -1;
2195 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2196 * of the divs that do appear in the quasi-polynomial.
2198 static __isl_give isl_qpolynomial *remove_redundant_divs(
2199 __isl_take isl_qpolynomial *qp)
2201 int i, j;
2202 int d;
2203 int len;
2204 int skip;
2205 int *active = NULL;
2206 int *reordering = NULL;
2207 int redundant = 0;
2208 int n_div;
2210 if (!qp)
2211 return NULL;
2212 if (qp->div->n_row == 0)
2213 return qp;
2215 d = isl_dim_total(qp->dim);
2216 len = qp->div->n_col - 2;
2217 active = isl_calloc_array(qp->ctx, int, len);
2218 if (!active)
2219 goto error;
2221 if (up_set_active(qp->upoly, active, len) < 0)
2222 goto error;
2224 for (i = qp->div->n_row - 1; i >= 0; --i) {
2225 if (!active[d + i]) {
2226 redundant = 1;
2227 continue;
2229 for (j = 0; j < i; ++j) {
2230 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2231 continue;
2232 active[d + j] = 1;
2233 break;
2237 if (!redundant) {
2238 free(active);
2239 return qp;
2242 reordering = isl_alloc_array(qp->div->ctx, int, len);
2243 if (!reordering)
2244 goto error;
2246 for (i = 0; i < d; ++i)
2247 reordering[i] = i;
2249 skip = 0;
2250 n_div = qp->div->n_row;
2251 for (i = 0; i < n_div; ++i) {
2252 if (!active[d + i]) {
2253 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2254 qp->div = isl_mat_drop_cols(qp->div,
2255 2 + d + i - skip, 1);
2256 skip++;
2258 reordering[d + i] = d + i - skip;
2261 qp->upoly = reorder(qp->upoly, reordering);
2263 if (!qp->upoly || !qp->div)
2264 goto error;
2266 free(active);
2267 free(reordering);
2269 return qp;
2270 error:
2271 free(active);
2272 free(reordering);
2273 isl_qpolynomial_free(qp);
2274 return NULL;
2277 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2278 unsigned first, unsigned n)
2280 int i;
2281 struct isl_upoly_rec *rec;
2283 if (!up)
2284 return NULL;
2285 if (n == 0 || up->var < 0 || up->var < first)
2286 return up;
2287 if (up->var < first + n) {
2288 up = replace_by_constant_term(up);
2289 return isl_upoly_drop(up, first, n);
2291 up = isl_upoly_cow(up);
2292 if (!up)
2293 return NULL;
2294 up->var -= n;
2295 rec = isl_upoly_as_rec(up);
2296 if (!rec)
2297 goto error;
2299 for (i = 0; i < rec->n; ++i) {
2300 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2301 if (!rec->p[i])
2302 goto error;
2305 return up;
2306 error:
2307 isl_upoly_free(up);
2308 return NULL;
2311 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2312 __isl_take isl_qpolynomial *qp,
2313 enum isl_dim_type type, unsigned pos, const char *s)
2315 qp = isl_qpolynomial_cow(qp);
2316 if (!qp)
2317 return NULL;
2318 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2319 if (!qp->dim)
2320 goto error;
2321 return qp;
2322 error:
2323 isl_qpolynomial_free(qp);
2324 return NULL;
2327 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2328 __isl_take isl_qpolynomial *qp,
2329 enum isl_dim_type type, unsigned first, unsigned n)
2331 if (!qp)
2332 return NULL;
2333 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
2334 return qp;
2336 qp = isl_qpolynomial_cow(qp);
2337 if (!qp)
2338 return NULL;
2340 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2341 goto error);
2342 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2343 type == isl_dim_set, goto error);
2345 qp->dim = isl_dim_drop(qp->dim, type, first, n);
2346 if (!qp->dim)
2347 goto error;
2349 if (type == isl_dim_set)
2350 first += isl_dim_size(qp->dim, isl_dim_param);
2352 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2353 if (!qp->div)
2354 goto error;
2356 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2357 if (!qp->upoly)
2358 goto error;
2360 return qp;
2361 error:
2362 isl_qpolynomial_free(qp);
2363 return NULL;
2366 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2367 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2369 int i, j, k;
2370 isl_int denom;
2371 unsigned total;
2372 unsigned n_div;
2373 struct isl_upoly *up;
2375 if (!eq)
2376 goto error;
2377 if (eq->n_eq == 0) {
2378 isl_basic_set_free(eq);
2379 return qp;
2382 qp = isl_qpolynomial_cow(qp);
2383 if (!qp)
2384 goto error;
2385 qp->div = isl_mat_cow(qp->div);
2386 if (!qp->div)
2387 goto error;
2389 total = 1 + isl_dim_total(eq->dim);
2390 n_div = eq->n_div;
2391 isl_int_init(denom);
2392 for (i = 0; i < eq->n_eq; ++i) {
2393 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2394 if (j < 0 || j == 0 || j >= total)
2395 continue;
2397 for (k = 0; k < qp->div->n_row; ++k) {
2398 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2399 continue;
2400 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2401 &qp->div->row[k][0]);
2402 normalize_div(qp, k);
2405 if (isl_int_is_pos(eq->eq[i][j]))
2406 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2407 isl_int_abs(denom, eq->eq[i][j]);
2408 isl_int_set_si(eq->eq[i][j], 0);
2410 up = isl_upoly_from_affine(qp->dim->ctx,
2411 eq->eq[i], denom, total);
2412 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2413 isl_upoly_free(up);
2415 isl_int_clear(denom);
2417 if (!qp->upoly)
2418 goto error;
2420 isl_basic_set_free(eq);
2422 qp = substitute_non_divs(qp);
2423 qp = sort_divs(qp);
2425 return qp;
2426 error:
2427 isl_basic_set_free(eq);
2428 isl_qpolynomial_free(qp);
2429 return NULL;
2432 static __isl_give isl_basic_set *add_div_constraints(
2433 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2435 int i;
2436 unsigned total;
2438 if (!bset || !div)
2439 goto error;
2441 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2442 if (!bset)
2443 goto error;
2444 total = isl_basic_set_total_dim(bset);
2445 for (i = 0; i < div->n_row; ++i)
2446 if (isl_basic_set_add_div_constraints_var(bset,
2447 total - div->n_row + i, div->row[i]) < 0)
2448 goto error;
2450 isl_mat_free(div);
2451 return bset;
2452 error:
2453 isl_mat_free(div);
2454 isl_basic_set_free(bset);
2455 return NULL;
2458 /* Look for equalities among the variables shared by context and qp
2459 * and the integer divisions of qp, if any.
2460 * The equalities are then used to eliminate variables and/or integer
2461 * divisions from qp.
2463 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2464 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2466 isl_basic_set *aff;
2468 if (!qp)
2469 goto error;
2470 if (qp->div->n_row > 0) {
2471 isl_basic_set *bset;
2472 context = isl_set_add_dims(context, isl_dim_set,
2473 qp->div->n_row);
2474 bset = isl_basic_set_universe(isl_set_get_dim(context));
2475 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2476 context = isl_set_intersect(context,
2477 isl_set_from_basic_set(bset));
2480 aff = isl_set_affine_hull(context);
2481 return isl_qpolynomial_substitute_equalities(qp, aff);
2482 error:
2483 isl_qpolynomial_free(qp);
2484 isl_set_free(context);
2485 return NULL;
2488 #undef PW
2489 #define PW isl_pw_qpolynomial
2490 #undef EL
2491 #define EL isl_qpolynomial
2492 #undef IS_ZERO
2493 #define IS_ZERO is_zero
2494 #undef FIELD
2495 #define FIELD qp
2497 #include <isl_pw_templ.c>
2499 #undef UNION
2500 #define UNION isl_union_pw_qpolynomial
2501 #undef PART
2502 #define PART isl_pw_qpolynomial
2503 #undef PARTS
2504 #define PARTS pw_qpolynomial
2506 #include <isl_union_templ.c>
2508 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2510 if (!pwqp)
2511 return -1;
2513 if (pwqp->n != -1)
2514 return 0;
2516 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2517 return 0;
2519 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2522 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2523 __isl_take isl_pw_qpolynomial *pwqp1,
2524 __isl_take isl_pw_qpolynomial *pwqp2)
2526 int i, j, n;
2527 struct isl_pw_qpolynomial *res;
2528 isl_set *set;
2530 if (!pwqp1 || !pwqp2)
2531 goto error;
2533 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2534 goto error);
2536 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2537 isl_pw_qpolynomial_free(pwqp2);
2538 return pwqp1;
2541 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2542 isl_pw_qpolynomial_free(pwqp1);
2543 return pwqp2;
2546 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2547 isl_pw_qpolynomial_free(pwqp1);
2548 return pwqp2;
2551 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2552 isl_pw_qpolynomial_free(pwqp2);
2553 return pwqp1;
2556 n = pwqp1->n * pwqp2->n;
2557 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2559 for (i = 0; i < pwqp1->n; ++i) {
2560 for (j = 0; j < pwqp2->n; ++j) {
2561 struct isl_set *common;
2562 struct isl_qpolynomial *prod;
2563 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2564 isl_set_copy(pwqp2->p[j].set));
2565 if (isl_set_plain_is_empty(common)) {
2566 isl_set_free(common);
2567 continue;
2570 prod = isl_qpolynomial_mul(
2571 isl_qpolynomial_copy(pwqp1->p[i].qp),
2572 isl_qpolynomial_copy(pwqp2->p[j].qp));
2574 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2578 isl_pw_qpolynomial_free(pwqp1);
2579 isl_pw_qpolynomial_free(pwqp2);
2581 return res;
2582 error:
2583 isl_pw_qpolynomial_free(pwqp1);
2584 isl_pw_qpolynomial_free(pwqp2);
2585 return NULL;
2588 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2589 __isl_take isl_pw_qpolynomial *pwqp)
2591 int i;
2593 if (!pwqp)
2594 return NULL;
2596 if (isl_pw_qpolynomial_is_zero(pwqp))
2597 return pwqp;
2599 pwqp = isl_pw_qpolynomial_cow(pwqp);
2600 if (!pwqp)
2601 return NULL;
2603 for (i = 0; i < pwqp->n; ++i) {
2604 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2605 if (!pwqp->p[i].qp)
2606 goto error;
2609 return pwqp;
2610 error:
2611 isl_pw_qpolynomial_free(pwqp);
2612 return NULL;
2615 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2616 __isl_take isl_pw_qpolynomial *pwqp1,
2617 __isl_take isl_pw_qpolynomial *pwqp2)
2619 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2622 __isl_give struct isl_upoly *isl_upoly_eval(
2623 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2625 int i;
2626 struct isl_upoly_rec *rec;
2627 struct isl_upoly *res;
2628 struct isl_upoly *base;
2630 if (isl_upoly_is_cst(up)) {
2631 isl_vec_free(vec);
2632 return up;
2635 rec = isl_upoly_as_rec(up);
2636 if (!rec)
2637 goto error;
2639 isl_assert(up->ctx, rec->n >= 1, goto error);
2641 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2643 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2644 isl_vec_copy(vec));
2646 for (i = rec->n - 2; i >= 0; --i) {
2647 res = isl_upoly_mul(res, isl_upoly_copy(base));
2648 res = isl_upoly_sum(res,
2649 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2650 isl_vec_copy(vec)));
2653 isl_upoly_free(base);
2654 isl_upoly_free(up);
2655 isl_vec_free(vec);
2656 return res;
2657 error:
2658 isl_upoly_free(up);
2659 isl_vec_free(vec);
2660 return NULL;
2663 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2664 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2666 isl_vec *ext;
2667 struct isl_upoly *up;
2668 isl_dim *dim;
2670 if (!qp || !pnt)
2671 goto error;
2672 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2674 if (qp->div->n_row == 0)
2675 ext = isl_vec_copy(pnt->vec);
2676 else {
2677 int i;
2678 unsigned dim = isl_dim_total(qp->dim);
2679 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2680 if (!ext)
2681 goto error;
2683 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2684 for (i = 0; i < qp->div->n_row; ++i) {
2685 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2686 1 + dim + i, &ext->el[1+dim+i]);
2687 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2688 qp->div->row[i][0]);
2692 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2693 if (!up)
2694 goto error;
2696 dim = isl_dim_copy(qp->dim);
2697 isl_qpolynomial_free(qp);
2698 isl_point_free(pnt);
2700 return isl_qpolynomial_alloc(dim, 0, up);
2701 error:
2702 isl_qpolynomial_free(qp);
2703 isl_point_free(pnt);
2704 return NULL;
2707 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2708 __isl_keep struct isl_upoly_cst *cst2)
2710 int cmp;
2711 isl_int t;
2712 isl_int_init(t);
2713 isl_int_mul(t, cst1->n, cst2->d);
2714 isl_int_submul(t, cst2->n, cst1->d);
2715 cmp = isl_int_sgn(t);
2716 isl_int_clear(t);
2717 return cmp;
2720 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2721 __isl_keep isl_qpolynomial *qp2)
2723 struct isl_upoly_cst *cst1, *cst2;
2725 if (!qp1 || !qp2)
2726 return -1;
2727 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2728 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2729 if (isl_qpolynomial_is_nan(qp1))
2730 return -1;
2731 if (isl_qpolynomial_is_nan(qp2))
2732 return -1;
2733 cst1 = isl_upoly_as_cst(qp1->upoly);
2734 cst2 = isl_upoly_as_cst(qp2->upoly);
2736 return isl_upoly_cmp(cst1, cst2) <= 0;
2739 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2740 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2742 struct isl_upoly_cst *cst1, *cst2;
2743 int cmp;
2745 if (!qp1 || !qp2)
2746 goto error;
2747 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2748 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2749 cst1 = isl_upoly_as_cst(qp1->upoly);
2750 cst2 = isl_upoly_as_cst(qp2->upoly);
2751 cmp = isl_upoly_cmp(cst1, cst2);
2753 if (cmp <= 0) {
2754 isl_qpolynomial_free(qp2);
2755 } else {
2756 isl_qpolynomial_free(qp1);
2757 qp1 = qp2;
2759 return qp1;
2760 error:
2761 isl_qpolynomial_free(qp1);
2762 isl_qpolynomial_free(qp2);
2763 return NULL;
2766 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2767 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2769 struct isl_upoly_cst *cst1, *cst2;
2770 int cmp;
2772 if (!qp1 || !qp2)
2773 goto error;
2774 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2775 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2776 cst1 = isl_upoly_as_cst(qp1->upoly);
2777 cst2 = isl_upoly_as_cst(qp2->upoly);
2778 cmp = isl_upoly_cmp(cst1, cst2);
2780 if (cmp >= 0) {
2781 isl_qpolynomial_free(qp2);
2782 } else {
2783 isl_qpolynomial_free(qp1);
2784 qp1 = qp2;
2786 return qp1;
2787 error:
2788 isl_qpolynomial_free(qp1);
2789 isl_qpolynomial_free(qp2);
2790 return NULL;
2793 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2794 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2795 unsigned first, unsigned n)
2797 unsigned total;
2798 unsigned g_pos;
2799 int *exp;
2801 if (n == 0)
2802 return qp;
2804 qp = isl_qpolynomial_cow(qp);
2805 if (!qp)
2806 return NULL;
2808 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2809 goto error);
2811 g_pos = pos(qp->dim, type) + first;
2813 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2814 if (!qp->div)
2815 goto error;
2817 total = qp->div->n_col - 2;
2818 if (total > g_pos) {
2819 int i;
2820 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2821 if (!exp)
2822 goto error;
2823 for (i = 0; i < total - g_pos; ++i)
2824 exp[i] = i + n;
2825 qp->upoly = expand(qp->upoly, exp, g_pos);
2826 free(exp);
2827 if (!qp->upoly)
2828 goto error;
2831 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2832 if (!qp->dim)
2833 goto error;
2835 return qp;
2836 error:
2837 isl_qpolynomial_free(qp);
2838 return NULL;
2841 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2842 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2844 unsigned pos;
2846 pos = isl_qpolynomial_dim(qp, type);
2848 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2851 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2852 __isl_take isl_pw_qpolynomial *pwqp,
2853 enum isl_dim_type type, unsigned n)
2855 unsigned pos;
2857 pos = isl_pw_qpolynomial_dim(pwqp, type);
2859 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2862 static int *reordering_move(isl_ctx *ctx,
2863 unsigned len, unsigned dst, unsigned src, unsigned n)
2865 int i;
2866 int *reordering;
2868 reordering = isl_alloc_array(ctx, int, len);
2869 if (!reordering)
2870 return NULL;
2872 if (dst <= src) {
2873 for (i = 0; i < dst; ++i)
2874 reordering[i] = i;
2875 for (i = 0; i < n; ++i)
2876 reordering[src + i] = dst + i;
2877 for (i = 0; i < src - dst; ++i)
2878 reordering[dst + i] = dst + n + i;
2879 for (i = 0; i < len - src - n; ++i)
2880 reordering[src + n + i] = src + n + i;
2881 } else {
2882 for (i = 0; i < src; ++i)
2883 reordering[i] = i;
2884 for (i = 0; i < n; ++i)
2885 reordering[src + i] = dst + i;
2886 for (i = 0; i < dst - src; ++i)
2887 reordering[src + n + i] = src + i;
2888 for (i = 0; i < len - dst - n; ++i)
2889 reordering[dst + n + i] = dst + n + i;
2892 return reordering;
2895 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2896 __isl_take isl_qpolynomial *qp,
2897 enum isl_dim_type dst_type, unsigned dst_pos,
2898 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2900 unsigned g_dst_pos;
2901 unsigned g_src_pos;
2902 int *reordering;
2904 qp = isl_qpolynomial_cow(qp);
2905 if (!qp)
2906 return NULL;
2908 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2909 goto error);
2911 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2912 g_src_pos = pos(qp->dim, src_type) + src_pos;
2913 if (dst_type > src_type)
2914 g_dst_pos -= n;
2916 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2917 if (!qp->div)
2918 goto error;
2919 qp = sort_divs(qp);
2920 if (!qp)
2921 goto error;
2923 reordering = reordering_move(qp->dim->ctx,
2924 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2925 if (!reordering)
2926 goto error;
2928 qp->upoly = reorder(qp->upoly, reordering);
2929 free(reordering);
2930 if (!qp->upoly)
2931 goto error;
2933 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2934 if (!qp->dim)
2935 goto error;
2937 return qp;
2938 error:
2939 isl_qpolynomial_free(qp);
2940 return NULL;
2943 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2944 isl_int *f, isl_int denom)
2946 struct isl_upoly *up;
2948 if (!dim)
2949 return NULL;
2951 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2953 return isl_qpolynomial_alloc(dim, 0, up);
2956 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
2958 isl_ctx *ctx;
2959 struct isl_upoly *up;
2960 isl_qpolynomial *qp;
2962 if (!aff)
2963 return NULL;
2965 ctx = isl_aff_get_ctx(aff);
2966 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
2967 aff->v->size - 1);
2969 qp = isl_qpolynomial_alloc(isl_aff_get_dim(aff),
2970 aff->ls->div->n_row, up);
2971 if (!qp)
2972 goto error;
2974 isl_mat_free(qp->div);
2975 qp->div = isl_mat_copy(aff->ls->div);
2976 qp->div = isl_mat_cow(qp->div);
2977 if (!qp->div)
2978 goto error;
2980 isl_aff_free(aff);
2981 qp = reduce_divs(qp);
2982 qp = remove_redundant_divs(qp);
2983 return qp;
2984 error:
2985 isl_aff_free(aff);
2986 return NULL;
2989 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2990 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2992 isl_int denom;
2993 isl_dim *dim;
2994 struct isl_upoly *up;
2995 isl_qpolynomial *qp;
2996 int sgn;
2998 if (!c)
2999 return NULL;
3001 isl_int_init(denom);
3003 isl_constraint_get_coefficient(c, type, pos, &denom);
3004 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
3005 sgn = isl_int_sgn(denom);
3006 isl_int_abs(denom, denom);
3007 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
3008 1 + isl_constraint_dim(c, isl_dim_all));
3009 if (sgn < 0)
3010 isl_int_neg(denom, denom);
3011 isl_constraint_set_coefficient(c, type, pos, denom);
3013 dim = isl_dim_copy(c->bmap->dim);
3015 isl_int_clear(denom);
3016 isl_constraint_free(c);
3018 qp = isl_qpolynomial_alloc(dim, 0, up);
3019 if (sgn > 0)
3020 qp = isl_qpolynomial_neg(qp);
3021 return qp;
3024 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3025 * in "qp" by subs[i].
3027 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3028 __isl_take isl_qpolynomial *qp,
3029 enum isl_dim_type type, unsigned first, unsigned n,
3030 __isl_keep isl_qpolynomial **subs)
3032 int i;
3033 struct isl_upoly **ups;
3035 if (n == 0)
3036 return qp;
3038 qp = isl_qpolynomial_cow(qp);
3039 if (!qp)
3040 return NULL;
3041 for (i = 0; i < n; ++i)
3042 if (!subs[i])
3043 goto error;
3045 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
3046 goto error);
3048 for (i = 0; i < n; ++i)
3049 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
3050 goto error);
3052 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3053 for (i = 0; i < n; ++i)
3054 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3056 first += pos(qp->dim, type);
3058 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3059 if (!ups)
3060 goto error;
3061 for (i = 0; i < n; ++i)
3062 ups[i] = subs[i]->upoly;
3064 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3066 free(ups);
3068 if (!qp->upoly)
3069 goto error;
3071 return qp;
3072 error:
3073 isl_qpolynomial_free(qp);
3074 return NULL;
3077 /* Extend "bset" with extra set dimensions for each integer division
3078 * in "qp" and then call "fn" with the extended bset and the polynomial
3079 * that results from replacing each of the integer divisions by the
3080 * corresponding extra set dimension.
3082 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3083 __isl_keep isl_basic_set *bset,
3084 int (*fn)(__isl_take isl_basic_set *bset,
3085 __isl_take isl_qpolynomial *poly, void *user), void *user)
3087 isl_dim *dim;
3088 isl_mat *div;
3089 isl_qpolynomial *poly;
3091 if (!qp || !bset)
3092 goto error;
3093 if (qp->div->n_row == 0)
3094 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3095 user);
3097 div = isl_mat_copy(qp->div);
3098 dim = isl_dim_copy(qp->dim);
3099 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3100 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3101 bset = isl_basic_set_copy(bset);
3102 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3103 bset = add_div_constraints(bset, div);
3105 return fn(bset, poly, user);
3106 error:
3107 return -1;
3110 /* Return total degree in variables first (inclusive) up to last (exclusive).
3112 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3114 int deg = -1;
3115 int i;
3116 struct isl_upoly_rec *rec;
3118 if (!up)
3119 return -2;
3120 if (isl_upoly_is_zero(up))
3121 return -1;
3122 if (isl_upoly_is_cst(up) || up->var < first)
3123 return 0;
3125 rec = isl_upoly_as_rec(up);
3126 if (!rec)
3127 return -2;
3129 for (i = 0; i < rec->n; ++i) {
3130 int d;
3132 if (isl_upoly_is_zero(rec->p[i]))
3133 continue;
3134 d = isl_upoly_degree(rec->p[i], first, last);
3135 if (up->var < last)
3136 d += i;
3137 if (d > deg)
3138 deg = d;
3141 return deg;
3144 /* Return total degree in set variables.
3146 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3148 unsigned ovar;
3149 unsigned nvar;
3151 if (!poly)
3152 return -2;
3154 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3155 nvar = isl_dim_size(poly->dim, isl_dim_set);
3156 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3159 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3160 unsigned pos, int deg)
3162 int i;
3163 struct isl_upoly_rec *rec;
3165 if (!up)
3166 return NULL;
3168 if (isl_upoly_is_cst(up) || up->var < pos) {
3169 if (deg == 0)
3170 return isl_upoly_copy(up);
3171 else
3172 return isl_upoly_zero(up->ctx);
3175 rec = isl_upoly_as_rec(up);
3176 if (!rec)
3177 return NULL;
3179 if (up->var == pos) {
3180 if (deg < rec->n)
3181 return isl_upoly_copy(rec->p[deg]);
3182 else
3183 return isl_upoly_zero(up->ctx);
3186 up = isl_upoly_copy(up);
3187 up = isl_upoly_cow(up);
3188 rec = isl_upoly_as_rec(up);
3189 if (!rec)
3190 goto error;
3192 for (i = 0; i < rec->n; ++i) {
3193 struct isl_upoly *t;
3194 t = isl_upoly_coeff(rec->p[i], pos, deg);
3195 if (!t)
3196 goto error;
3197 isl_upoly_free(rec->p[i]);
3198 rec->p[i] = t;
3201 return up;
3202 error:
3203 isl_upoly_free(up);
3204 return NULL;
3207 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3209 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3210 __isl_keep isl_qpolynomial *qp,
3211 enum isl_dim_type type, unsigned t_pos, int deg)
3213 unsigned g_pos;
3214 struct isl_upoly *up;
3215 isl_qpolynomial *c;
3217 if (!qp)
3218 return NULL;
3220 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3221 return NULL);
3223 g_pos = pos(qp->dim, type) + t_pos;
3224 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3226 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3227 if (!c)
3228 return NULL;
3229 isl_mat_free(c->div);
3230 c->div = isl_mat_copy(qp->div);
3231 if (!c->div)
3232 goto error;
3233 return c;
3234 error:
3235 isl_qpolynomial_free(c);
3236 return NULL;
3239 /* Homogenize the polynomial in the variables first (inclusive) up to
3240 * last (exclusive) by inserting powers of variable first.
3241 * Variable first is assumed not to appear in the input.
3243 __isl_give struct isl_upoly *isl_upoly_homogenize(
3244 __isl_take struct isl_upoly *up, int deg, int target,
3245 int first, int last)
3247 int i;
3248 struct isl_upoly_rec *rec;
3250 if (!up)
3251 return NULL;
3252 if (isl_upoly_is_zero(up))
3253 return up;
3254 if (deg == target)
3255 return up;
3256 if (isl_upoly_is_cst(up) || up->var < first) {
3257 struct isl_upoly *hom;
3259 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3260 if (!hom)
3261 goto error;
3262 rec = isl_upoly_as_rec(hom);
3263 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3265 return hom;
3268 up = isl_upoly_cow(up);
3269 rec = isl_upoly_as_rec(up);
3270 if (!rec)
3271 goto error;
3273 for (i = 0; i < rec->n; ++i) {
3274 if (isl_upoly_is_zero(rec->p[i]))
3275 continue;
3276 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3277 up->var < last ? deg + i : i, target,
3278 first, last);
3279 if (!rec->p[i])
3280 goto error;
3283 return up;
3284 error:
3285 isl_upoly_free(up);
3286 return NULL;
3289 /* Homogenize the polynomial in the set variables by introducing
3290 * powers of an extra set variable at position 0.
3292 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3293 __isl_take isl_qpolynomial *poly)
3295 unsigned ovar;
3296 unsigned nvar;
3297 int deg = isl_qpolynomial_degree(poly);
3299 if (deg < -1)
3300 goto error;
3302 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3303 poly = isl_qpolynomial_cow(poly);
3304 if (!poly)
3305 goto error;
3307 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3308 nvar = isl_dim_size(poly->dim, isl_dim_set);
3309 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3310 ovar, ovar + nvar);
3311 if (!poly->upoly)
3312 goto error;
3314 return poly;
3315 error:
3316 isl_qpolynomial_free(poly);
3317 return NULL;
3320 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3321 __isl_take isl_mat *div)
3323 isl_term *term;
3324 int n;
3326 if (!dim || !div)
3327 goto error;
3329 n = isl_dim_total(dim) + div->n_row;
3331 term = isl_calloc(dim->ctx, struct isl_term,
3332 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3333 if (!term)
3334 goto error;
3336 term->ref = 1;
3337 term->dim = dim;
3338 term->div = div;
3339 isl_int_init(term->n);
3340 isl_int_init(term->d);
3342 return term;
3343 error:
3344 isl_dim_free(dim);
3345 isl_mat_free(div);
3346 return NULL;
3349 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3351 if (!term)
3352 return NULL;
3354 term->ref++;
3355 return term;
3358 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3360 int i;
3361 isl_term *dup;
3362 unsigned total;
3364 if (term)
3365 return NULL;
3367 total = isl_dim_total(term->dim) + term->div->n_row;
3369 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3370 if (!dup)
3371 return NULL;
3373 isl_int_set(dup->n, term->n);
3374 isl_int_set(dup->d, term->d);
3376 for (i = 0; i < total; ++i)
3377 dup->pow[i] = term->pow[i];
3379 return dup;
3382 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3384 if (!term)
3385 return NULL;
3387 if (term->ref == 1)
3388 return term;
3389 term->ref--;
3390 return isl_term_dup(term);
3393 void isl_term_free(__isl_take isl_term *term)
3395 if (!term)
3396 return;
3398 if (--term->ref > 0)
3399 return;
3401 isl_dim_free(term->dim);
3402 isl_mat_free(term->div);
3403 isl_int_clear(term->n);
3404 isl_int_clear(term->d);
3405 free(term);
3408 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3410 if (!term)
3411 return 0;
3413 switch (type) {
3414 case isl_dim_param:
3415 case isl_dim_in:
3416 case isl_dim_out: return isl_dim_size(term->dim, type);
3417 case isl_dim_div: return term->div->n_row;
3418 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3419 default: return 0;
3423 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3425 return term ? term->dim->ctx : NULL;
3428 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3430 if (!term)
3431 return;
3432 isl_int_set(*n, term->n);
3435 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3437 if (!term)
3438 return;
3439 isl_int_set(*d, term->d);
3442 int isl_term_get_exp(__isl_keep isl_term *term,
3443 enum isl_dim_type type, unsigned pos)
3445 if (!term)
3446 return -1;
3448 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3450 if (type >= isl_dim_set)
3451 pos += isl_dim_size(term->dim, isl_dim_param);
3452 if (type >= isl_dim_div)
3453 pos += isl_dim_size(term->dim, isl_dim_set);
3455 return term->pow[pos];
3458 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3460 isl_basic_map *bmap;
3461 unsigned total;
3462 int k;
3464 if (!term)
3465 return NULL;
3467 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3468 return NULL);
3470 total = term->div->n_col - term->div->n_row - 2;
3471 /* No nested divs for now */
3472 isl_assert(term->dim->ctx,
3473 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3474 term->div->n_row) == -1,
3475 return NULL);
3477 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3478 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3479 goto error;
3481 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3483 return isl_basic_map_div(bmap, k);
3484 error:
3485 isl_basic_map_free(bmap);
3486 return NULL;
3489 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3490 int (*fn)(__isl_take isl_term *term, void *user),
3491 __isl_take isl_term *term, void *user)
3493 int i;
3494 struct isl_upoly_rec *rec;
3496 if (!up || !term)
3497 goto error;
3499 if (isl_upoly_is_zero(up))
3500 return term;
3502 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3503 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3504 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3506 if (isl_upoly_is_cst(up)) {
3507 struct isl_upoly_cst *cst;
3508 cst = isl_upoly_as_cst(up);
3509 if (!cst)
3510 goto error;
3511 term = isl_term_cow(term);
3512 if (!term)
3513 goto error;
3514 isl_int_set(term->n, cst->n);
3515 isl_int_set(term->d, cst->d);
3516 if (fn(isl_term_copy(term), user) < 0)
3517 goto error;
3518 return term;
3521 rec = isl_upoly_as_rec(up);
3522 if (!rec)
3523 goto error;
3525 for (i = 0; i < rec->n; ++i) {
3526 term = isl_term_cow(term);
3527 if (!term)
3528 goto error;
3529 term->pow[up->var] = i;
3530 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3531 if (!term)
3532 goto error;
3534 term->pow[up->var] = 0;
3536 return term;
3537 error:
3538 isl_term_free(term);
3539 return NULL;
3542 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3543 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3545 isl_term *term;
3547 if (!qp)
3548 return -1;
3550 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3551 if (!term)
3552 return -1;
3554 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3556 isl_term_free(term);
3558 return term ? 0 : -1;
3561 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3563 struct isl_upoly *up;
3564 isl_qpolynomial *qp;
3565 int i, n;
3567 if (!term)
3568 return NULL;
3570 n = isl_dim_total(term->dim) + term->div->n_row;
3572 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3573 for (i = 0; i < n; ++i) {
3574 if (!term->pow[i])
3575 continue;
3576 up = isl_upoly_mul(up,
3577 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3580 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3581 if (!qp)
3582 goto error;
3583 isl_mat_free(qp->div);
3584 qp->div = isl_mat_copy(term->div);
3585 if (!qp->div)
3586 goto error;
3588 isl_term_free(term);
3589 return qp;
3590 error:
3591 isl_qpolynomial_free(qp);
3592 isl_term_free(term);
3593 return NULL;
3596 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3597 __isl_take isl_dim *dim)
3599 int i;
3600 int extra;
3601 unsigned total;
3603 if (!qp || !dim)
3604 goto error;
3606 if (isl_dim_equal(qp->dim, dim)) {
3607 isl_dim_free(dim);
3608 return qp;
3611 qp = isl_qpolynomial_cow(qp);
3612 if (!qp)
3613 goto error;
3615 extra = isl_dim_size(dim, isl_dim_set) -
3616 isl_dim_size(qp->dim, isl_dim_set);
3617 total = isl_dim_total(qp->dim);
3618 if (qp->div->n_row) {
3619 int *exp;
3621 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3622 if (!exp)
3623 goto error;
3624 for (i = 0; i < qp->div->n_row; ++i)
3625 exp[i] = extra + i;
3626 qp->upoly = expand(qp->upoly, exp, total);
3627 free(exp);
3628 if (!qp->upoly)
3629 goto error;
3631 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3632 if (!qp->div)
3633 goto error;
3634 for (i = 0; i < qp->div->n_row; ++i)
3635 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3637 isl_dim_free(qp->dim);
3638 qp->dim = dim;
3640 return qp;
3641 error:
3642 isl_dim_free(dim);
3643 isl_qpolynomial_free(qp);
3644 return NULL;
3647 /* For each parameter or variable that does not appear in qp,
3648 * first eliminate the variable from all constraints and then set it to zero.
3650 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3651 __isl_keep isl_qpolynomial *qp)
3653 int *active = NULL;
3654 int i;
3655 int d;
3656 unsigned nparam;
3657 unsigned nvar;
3659 if (!set || !qp)
3660 goto error;
3662 d = isl_dim_total(set->dim);
3663 active = isl_calloc_array(set->ctx, int, d);
3664 if (set_active(qp, active) < 0)
3665 goto error;
3667 for (i = 0; i < d; ++i)
3668 if (!active[i])
3669 break;
3671 if (i == d) {
3672 free(active);
3673 return set;
3676 nparam = isl_dim_size(set->dim, isl_dim_param);
3677 nvar = isl_dim_size(set->dim, isl_dim_set);
3678 for (i = 0; i < nparam; ++i) {
3679 if (active[i])
3680 continue;
3681 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3682 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3684 for (i = 0; i < nvar; ++i) {
3685 if (active[nparam + i])
3686 continue;
3687 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3688 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3691 free(active);
3693 return set;
3694 error:
3695 free(active);
3696 isl_set_free(set);
3697 return NULL;
3700 struct isl_opt_data {
3701 isl_qpolynomial *qp;
3702 int first;
3703 isl_qpolynomial *opt;
3704 int max;
3707 static int opt_fn(__isl_take isl_point *pnt, void *user)
3709 struct isl_opt_data *data = (struct isl_opt_data *)user;
3710 isl_qpolynomial *val;
3712 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3713 if (data->first) {
3714 data->first = 0;
3715 data->opt = val;
3716 } else if (data->max) {
3717 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3718 } else {
3719 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3722 return 0;
3725 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3726 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3728 struct isl_opt_data data = { NULL, 1, NULL, max };
3730 if (!set || !qp)
3731 goto error;
3733 if (isl_upoly_is_cst(qp->upoly)) {
3734 isl_set_free(set);
3735 return qp;
3738 set = fix_inactive(set, qp);
3740 data.qp = qp;
3741 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3742 goto error;
3744 if (data.first)
3745 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3747 isl_set_free(set);
3748 isl_qpolynomial_free(qp);
3749 return data.opt;
3750 error:
3751 isl_set_free(set);
3752 isl_qpolynomial_free(qp);
3753 isl_qpolynomial_free(data.opt);
3754 return NULL;
3757 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3758 __isl_take isl_morph *morph)
3760 int i;
3761 int n_sub;
3762 isl_ctx *ctx;
3763 struct isl_upoly *up;
3764 unsigned n_div;
3765 struct isl_upoly **subs;
3766 isl_mat *mat;
3768 qp = isl_qpolynomial_cow(qp);
3769 if (!qp || !morph)
3770 goto error;
3772 ctx = qp->dim->ctx;
3773 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3775 n_sub = morph->inv->n_row - 1;
3776 if (morph->inv->n_row != morph->inv->n_col)
3777 n_sub += qp->div->n_row;
3778 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3779 if (!subs)
3780 goto error;
3782 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3783 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3784 morph->inv->row[0][0], morph->inv->n_col);
3785 if (morph->inv->n_row != morph->inv->n_col)
3786 for (i = 0; i < qp->div->n_row; ++i)
3787 subs[morph->inv->n_row - 1 + i] =
3788 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3790 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3792 for (i = 0; i < n_sub; ++i)
3793 isl_upoly_free(subs[i]);
3794 free(subs);
3796 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3797 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3798 qp->div = isl_mat_product(qp->div, mat);
3799 isl_dim_free(qp->dim);
3800 qp->dim = isl_dim_copy(morph->ran->dim);
3802 if (!qp->upoly || !qp->div || !qp->dim)
3803 goto error;
3805 isl_morph_free(morph);
3807 return qp;
3808 error:
3809 isl_qpolynomial_free(qp);
3810 isl_morph_free(morph);
3811 return NULL;
3814 static int neg_entry(void **entry, void *user)
3816 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3818 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3820 return *pwqp ? 0 : -1;
3823 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3824 __isl_take isl_union_pw_qpolynomial *upwqp)
3826 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3827 if (!upwqp)
3828 return NULL;
3830 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3831 &neg_entry, NULL) < 0)
3832 goto error;
3834 return upwqp;
3835 error:
3836 isl_union_pw_qpolynomial_free(upwqp);
3837 return NULL;
3840 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3841 __isl_take isl_union_pw_qpolynomial *upwqp1,
3842 __isl_take isl_union_pw_qpolynomial *upwqp2)
3844 return isl_union_pw_qpolynomial_add(upwqp1,
3845 isl_union_pw_qpolynomial_neg(upwqp2));
3848 static int mul_entry(void **entry, void *user)
3850 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3851 uint32_t hash;
3852 struct isl_hash_table_entry *entry2;
3853 isl_pw_qpolynomial *pwpq = *entry;
3854 int empty;
3856 hash = isl_dim_get_hash(pwpq->dim);
3857 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3858 hash, &has_dim, pwpq->dim, 0);
3859 if (!entry2)
3860 return 0;
3862 pwpq = isl_pw_qpolynomial_copy(pwpq);
3863 pwpq = isl_pw_qpolynomial_mul(pwpq,
3864 isl_pw_qpolynomial_copy(entry2->data));
3866 empty = isl_pw_qpolynomial_is_zero(pwpq);
3867 if (empty < 0) {
3868 isl_pw_qpolynomial_free(pwpq);
3869 return -1;
3871 if (empty) {
3872 isl_pw_qpolynomial_free(pwpq);
3873 return 0;
3876 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3878 return 0;
3881 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3882 __isl_take isl_union_pw_qpolynomial *upwqp1,
3883 __isl_take isl_union_pw_qpolynomial *upwqp2)
3885 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3888 /* Reorder the columns of the given div definitions according to the
3889 * given reordering.
3891 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3892 __isl_take isl_reordering *r)
3894 int i, j;
3895 isl_mat *mat;
3896 int extra;
3898 if (!div || !r)
3899 goto error;
3901 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3902 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3903 if (!mat)
3904 goto error;
3906 for (i = 0; i < div->n_row; ++i) {
3907 isl_seq_cpy(mat->row[i], div->row[i], 2);
3908 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3909 for (j = 0; j < r->len; ++j)
3910 isl_int_set(mat->row[i][2 + r->pos[j]],
3911 div->row[i][2 + j]);
3914 isl_reordering_free(r);
3915 isl_mat_free(div);
3916 return mat;
3917 error:
3918 isl_reordering_free(r);
3919 isl_mat_free(div);
3920 return NULL;
3923 /* Reorder the dimension of "qp" according to the given reordering.
3925 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3926 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3928 qp = isl_qpolynomial_cow(qp);
3929 if (!qp)
3930 goto error;
3932 r = isl_reordering_extend(r, qp->div->n_row);
3933 if (!r)
3934 goto error;
3936 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3937 if (!qp->div)
3938 goto error;
3940 qp->upoly = reorder(qp->upoly, r->pos);
3941 if (!qp->upoly)
3942 goto error;
3944 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3946 isl_reordering_free(r);
3947 return qp;
3948 error:
3949 isl_qpolynomial_free(qp);
3950 isl_reordering_free(r);
3951 return NULL;
3954 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
3955 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *model)
3957 if (!qp || !model)
3958 goto error;
3960 if (!isl_dim_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
3961 isl_reordering *exp;
3963 model = isl_dim_drop(model, isl_dim_in,
3964 0, isl_dim_size(model, isl_dim_in));
3965 model = isl_dim_drop(model, isl_dim_out,
3966 0, isl_dim_size(model, isl_dim_out));
3967 exp = isl_parameter_alignment_reordering(qp->dim, model);
3968 exp = isl_reordering_extend_dim(exp,
3969 isl_qpolynomial_get_dim(qp));
3970 qp = isl_qpolynomial_realign(qp, exp);
3973 isl_dim_free(model);
3974 return qp;
3975 error:
3976 isl_dim_free(model);
3977 isl_qpolynomial_free(qp);
3978 return NULL;
3981 struct isl_split_periods_data {
3982 int max_periods;
3983 isl_pw_qpolynomial *res;
3986 /* Create a slice where the integer division "div" has the fixed value "v".
3987 * In particular, if "div" refers to floor(f/m), then create a slice
3989 * m v <= f <= m v + (m - 1)
3991 * or
3993 * f - m v >= 0
3994 * -f + m v + (m - 1) >= 0
3996 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3997 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3999 int total;
4000 isl_basic_set *bset = NULL;
4001 int k;
4003 if (!dim || !qp)
4004 goto error;
4006 total = isl_dim_total(dim);
4007 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
4009 k = isl_basic_set_alloc_inequality(bset);
4010 if (k < 0)
4011 goto error;
4012 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4013 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4015 k = isl_basic_set_alloc_inequality(bset);
4016 if (k < 0)
4017 goto error;
4018 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4019 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4020 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4021 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4023 isl_dim_free(dim);
4024 return isl_set_from_basic_set(bset);
4025 error:
4026 isl_basic_set_free(bset);
4027 isl_dim_free(dim);
4028 return NULL;
4031 static int split_periods(__isl_take isl_set *set,
4032 __isl_take isl_qpolynomial *qp, void *user);
4034 /* Create a slice of the domain "set" such that integer division "div"
4035 * has the fixed value "v" and add the results to data->res,
4036 * replacing the integer division by "v" in "qp".
4038 static int set_div(__isl_take isl_set *set,
4039 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4040 struct isl_split_periods_data *data)
4042 int i;
4043 int total;
4044 isl_set *slice;
4045 struct isl_upoly *cst;
4047 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
4048 set = isl_set_intersect(set, slice);
4050 if (!qp)
4051 goto error;
4053 total = isl_dim_total(qp->dim);
4055 for (i = div + 1; i < qp->div->n_row; ++i) {
4056 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4057 continue;
4058 isl_int_addmul(qp->div->row[i][1],
4059 qp->div->row[i][2 + total + div], v);
4060 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4063 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4064 qp = substitute_div(qp, div, cst);
4066 return split_periods(set, qp, data);
4067 error:
4068 isl_set_free(set);
4069 isl_qpolynomial_free(qp);
4070 return -1;
4073 /* Split the domain "set" such that integer division "div"
4074 * has a fixed value (ranging from "min" to "max") on each slice
4075 * and add the results to data->res.
4077 static int split_div(__isl_take isl_set *set,
4078 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4079 struct isl_split_periods_data *data)
4081 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4082 isl_set *set_i = isl_set_copy(set);
4083 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4085 if (set_div(set_i, qp_i, div, min, data) < 0)
4086 goto error;
4088 isl_set_free(set);
4089 isl_qpolynomial_free(qp);
4090 return 0;
4091 error:
4092 isl_set_free(set);
4093 isl_qpolynomial_free(qp);
4094 return -1;
4097 /* If "qp" refers to any integer division
4098 * that can only attain "max_periods" distinct values on "set"
4099 * then split the domain along those distinct values.
4100 * Add the results (or the original if no splitting occurs)
4101 * to data->res.
4103 static int split_periods(__isl_take isl_set *set,
4104 __isl_take isl_qpolynomial *qp, void *user)
4106 int i;
4107 isl_pw_qpolynomial *pwqp;
4108 struct isl_split_periods_data *data;
4109 isl_int min, max;
4110 int total;
4111 int r = 0;
4113 data = (struct isl_split_periods_data *)user;
4115 if (!set || !qp)
4116 goto error;
4118 if (qp->div->n_row == 0) {
4119 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4120 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4121 return 0;
4124 isl_int_init(min);
4125 isl_int_init(max);
4126 total = isl_dim_total(qp->dim);
4127 for (i = 0; i < qp->div->n_row; ++i) {
4128 enum isl_lp_result lp_res;
4130 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4131 qp->div->n_row) != -1)
4132 continue;
4134 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4135 set->ctx->one, &min, NULL, NULL);
4136 if (lp_res == isl_lp_error)
4137 goto error2;
4138 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4139 continue;
4140 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4142 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4143 set->ctx->one, &max, NULL, NULL);
4144 if (lp_res == isl_lp_error)
4145 goto error2;
4146 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4147 continue;
4148 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4150 isl_int_sub(max, max, min);
4151 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4152 isl_int_add(max, max, min);
4153 break;
4157 if (i < qp->div->n_row) {
4158 r = split_div(set, qp, i, min, max, data);
4159 } else {
4160 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4161 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4164 isl_int_clear(max);
4165 isl_int_clear(min);
4167 return r;
4168 error2:
4169 isl_int_clear(max);
4170 isl_int_clear(min);
4171 error:
4172 isl_set_free(set);
4173 isl_qpolynomial_free(qp);
4174 return -1;
4177 /* If any quasi-polynomial in pwqp refers to any integer division
4178 * that can only attain "max_periods" distinct values on its domain
4179 * then split the domain along those distinct values.
4181 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4182 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4184 struct isl_split_periods_data data;
4186 data.max_periods = max_periods;
4187 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4189 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4190 goto error;
4192 isl_pw_qpolynomial_free(pwqp);
4194 return data.res;
4195 error:
4196 isl_pw_qpolynomial_free(data.res);
4197 isl_pw_qpolynomial_free(pwqp);
4198 return NULL;
4201 /* Construct a piecewise quasipolynomial that is constant on the given
4202 * domain. In particular, it is
4203 * 0 if cst == 0
4204 * 1 if cst == 1
4205 * infinity if cst == -1
4207 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4208 __isl_take isl_basic_set *bset, int cst)
4210 isl_dim *dim;
4211 isl_qpolynomial *qp;
4213 if (!bset)
4214 return NULL;
4216 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4217 dim = isl_basic_set_get_dim(bset);
4218 if (cst < 0)
4219 qp = isl_qpolynomial_infty(dim);
4220 else if (cst == 0)
4221 qp = isl_qpolynomial_zero(dim);
4222 else
4223 qp = isl_qpolynomial_one(dim);
4224 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4227 /* Factor bset, call fn on each of the factors and return the product.
4229 * If no factors can be found, simply call fn on the input.
4230 * Otherwise, construct the factors based on the factorizer,
4231 * call fn on each factor and compute the product.
4233 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4234 __isl_take isl_basic_set *bset,
4235 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4237 int i, n;
4238 isl_dim *dim;
4239 isl_set *set;
4240 isl_factorizer *f;
4241 isl_qpolynomial *qp;
4242 isl_pw_qpolynomial *pwqp;
4243 unsigned nparam;
4244 unsigned nvar;
4246 f = isl_basic_set_factorizer(bset);
4247 if (!f)
4248 goto error;
4249 if (f->n_group == 0) {
4250 isl_factorizer_free(f);
4251 return fn(bset);
4254 nparam = isl_basic_set_dim(bset, isl_dim_param);
4255 nvar = isl_basic_set_dim(bset, isl_dim_set);
4257 dim = isl_basic_set_get_dim(bset);
4258 dim = isl_dim_domain(dim);
4259 set = isl_set_universe(isl_dim_copy(dim));
4260 qp = isl_qpolynomial_one(dim);
4261 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4263 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4265 for (i = 0, n = 0; i < f->n_group; ++i) {
4266 isl_basic_set *bset_i;
4267 isl_pw_qpolynomial *pwqp_i;
4269 bset_i = isl_basic_set_copy(bset);
4270 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4271 nparam + n + f->len[i], nvar - n - f->len[i]);
4272 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4273 nparam, n);
4274 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4275 n + f->len[i], nvar - n - f->len[i]);
4276 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4278 pwqp_i = fn(bset_i);
4279 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4281 n += f->len[i];
4284 isl_basic_set_free(bset);
4285 isl_factorizer_free(f);
4287 return pwqp;
4288 error:
4289 isl_basic_set_free(bset);
4290 return NULL;
4293 /* Factor bset, call fn on each of the factors and return the product.
4294 * The function is assumed to evaluate to zero on empty domains,
4295 * to one on zero-dimensional domains and to infinity on unbounded domains
4296 * and will not be called explicitly on zero-dimensional or unbounded domains.
4298 * We first check for some special cases and remove all equalities.
4299 * Then we hand over control to compressed_multiplicative_call.
4301 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4302 __isl_take isl_basic_set *bset,
4303 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4305 int bounded;
4306 isl_morph *morph;
4307 isl_pw_qpolynomial *pwqp;
4308 unsigned orig_nvar, final_nvar;
4310 if (!bset)
4311 return NULL;
4313 if (isl_basic_set_plain_is_empty(bset))
4314 return constant_on_domain(bset, 0);
4316 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4318 if (orig_nvar == 0)
4319 return constant_on_domain(bset, 1);
4321 bounded = isl_basic_set_is_bounded(bset);
4322 if (bounded < 0)
4323 goto error;
4324 if (!bounded)
4325 return constant_on_domain(bset, -1);
4327 if (bset->n_eq == 0)
4328 return compressed_multiplicative_call(bset, fn);
4330 morph = isl_basic_set_full_compression(bset);
4331 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4333 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4335 pwqp = compressed_multiplicative_call(bset, fn);
4337 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4338 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4339 morph = isl_morph_inverse(morph);
4341 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4343 return pwqp;
4344 error:
4345 isl_basic_set_free(bset);
4346 return NULL;
4349 /* Drop all floors in "qp", turning each integer division [a/m] into
4350 * a rational division a/m. If "down" is set, then the integer division
4351 * is replaces by (a-(m-1))/m instead.
4353 static __isl_give isl_qpolynomial *qp_drop_floors(
4354 __isl_take isl_qpolynomial *qp, int down)
4356 int i;
4357 struct isl_upoly *s;
4359 if (!qp)
4360 return NULL;
4361 if (qp->div->n_row == 0)
4362 return qp;
4364 qp = isl_qpolynomial_cow(qp);
4365 if (!qp)
4366 return NULL;
4368 for (i = qp->div->n_row - 1; i >= 0; --i) {
4369 if (down) {
4370 isl_int_sub(qp->div->row[i][1],
4371 qp->div->row[i][1], qp->div->row[i][0]);
4372 isl_int_add_ui(qp->div->row[i][1],
4373 qp->div->row[i][1], 1);
4375 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4376 qp->div->row[i][0], qp->div->n_col - 1);
4377 qp = substitute_div(qp, i, s);
4378 if (!qp)
4379 return NULL;
4382 return qp;
4385 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4386 * a rational division a/m.
4388 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4389 __isl_take isl_pw_qpolynomial *pwqp)
4391 int i;
4393 if (!pwqp)
4394 return NULL;
4396 if (isl_pw_qpolynomial_is_zero(pwqp))
4397 return pwqp;
4399 pwqp = isl_pw_qpolynomial_cow(pwqp);
4400 if (!pwqp)
4401 return NULL;
4403 for (i = 0; i < pwqp->n; ++i) {
4404 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4405 if (!pwqp->p[i].qp)
4406 goto error;
4409 return pwqp;
4410 error:
4411 isl_pw_qpolynomial_free(pwqp);
4412 return NULL;
4415 /* Adjust all the integer divisions in "qp" such that they are at least
4416 * one over the given orthant (identified by "signs"). This ensures
4417 * that they will still be non-negative even after subtracting (m-1)/m.
4419 * In particular, f is replaced by f' + v, changing f = [a/m]
4420 * to f' = [(a - m v)/m].
4421 * If the constant term k in a is smaller than m,
4422 * the constant term of v is set to floor(k/m) - 1.
4423 * For any other term, if the coefficient c and the variable x have
4424 * the same sign, then no changes are needed.
4425 * Otherwise, if the variable is positive (and c is negative),
4426 * then the coefficient of x in v is set to floor(c/m).
4427 * If the variable is negative (and c is positive),
4428 * then the coefficient of x in v is set to ceil(c/m).
4430 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4431 int *signs)
4433 int i, j;
4434 int total;
4435 isl_vec *v = NULL;
4436 struct isl_upoly *s;
4438 qp = isl_qpolynomial_cow(qp);
4439 if (!qp)
4440 return NULL;
4441 qp->div = isl_mat_cow(qp->div);
4442 if (!qp->div)
4443 goto error;
4445 total = isl_dim_total(qp->dim);
4446 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4448 for (i = 0; i < qp->div->n_row; ++i) {
4449 isl_int *row = qp->div->row[i];
4450 v = isl_vec_clr(v);
4451 if (!v)
4452 goto error;
4453 if (isl_int_lt(row[1], row[0])) {
4454 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4455 isl_int_sub_ui(v->el[0], v->el[0], 1);
4456 isl_int_submul(row[1], row[0], v->el[0]);
4458 for (j = 0; j < total; ++j) {
4459 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4460 continue;
4461 if (signs[j] < 0)
4462 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4463 else
4464 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4465 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4467 for (j = 0; j < i; ++j) {
4468 if (isl_int_sgn(row[2 + total + j]) >= 0)
4469 continue;
4470 isl_int_fdiv_q(v->el[1 + total + j],
4471 row[2 + total + j], row[0]);
4472 isl_int_submul(row[2 + total + j],
4473 row[0], v->el[1 + total + j]);
4475 for (j = i + 1; j < qp->div->n_row; ++j) {
4476 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4477 continue;
4478 isl_seq_combine(qp->div->row[j] + 1,
4479 qp->div->ctx->one, qp->div->row[j] + 1,
4480 qp->div->row[j][2 + total + i], v->el, v->size);
4482 isl_int_set_si(v->el[1 + total + i], 1);
4483 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4484 qp->div->ctx->one, v->size);
4485 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4486 isl_upoly_free(s);
4487 if (!qp->upoly)
4488 goto error;
4491 isl_vec_free(v);
4492 return qp;
4493 error:
4494 isl_vec_free(v);
4495 isl_qpolynomial_free(qp);
4496 return NULL;
4499 struct isl_to_poly_data {
4500 int sign;
4501 isl_pw_qpolynomial *res;
4502 isl_qpolynomial *qp;
4505 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4506 * We first make all integer divisions positive and then split the
4507 * quasipolynomials into terms with sign data->sign (the direction
4508 * of the requested approximation) and terms with the opposite sign.
4509 * In the first set of terms, each integer division [a/m] is
4510 * overapproximated by a/m, while in the second it is underapproximated
4511 * by (a-(m-1))/m.
4513 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4514 void *user)
4516 struct isl_to_poly_data *data = user;
4517 isl_pw_qpolynomial *t;
4518 isl_qpolynomial *qp, *up, *down;
4520 qp = isl_qpolynomial_copy(data->qp);
4521 qp = make_divs_pos(qp, signs);
4523 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4524 up = qp_drop_floors(up, 0);
4525 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4526 down = qp_drop_floors(down, 1);
4528 isl_qpolynomial_free(qp);
4529 qp = isl_qpolynomial_add(up, down);
4531 t = isl_pw_qpolynomial_alloc(orthant, qp);
4532 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4534 return 0;
4537 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4538 * the polynomial will be an overapproximation. If "sign" is negative,
4539 * it will be an underapproximation. If "sign" is zero, the approximation
4540 * will lie somewhere in between.
4542 * In particular, is sign == 0, we simply drop the floors, turning
4543 * the integer divisions into rational divisions.
4544 * Otherwise, we split the domains into orthants, make all integer divisions
4545 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4546 * depending on the requested sign and the sign of the term in which
4547 * the integer division appears.
4549 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4550 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4552 int i;
4553 struct isl_to_poly_data data;
4555 if (sign == 0)
4556 return pwqp_drop_floors(pwqp);
4558 if (!pwqp)
4559 return NULL;
4561 data.sign = sign;
4562 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4564 for (i = 0; i < pwqp->n; ++i) {
4565 if (pwqp->p[i].qp->div->n_row == 0) {
4566 isl_pw_qpolynomial *t;
4567 t = isl_pw_qpolynomial_alloc(
4568 isl_set_copy(pwqp->p[i].set),
4569 isl_qpolynomial_copy(pwqp->p[i].qp));
4570 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4571 continue;
4573 data.qp = pwqp->p[i].qp;
4574 if (isl_set_foreach_orthant(pwqp->p[i].set,
4575 &to_polynomial_on_orthant, &data) < 0)
4576 goto error;
4579 isl_pw_qpolynomial_free(pwqp);
4581 return data.res;
4582 error:
4583 isl_pw_qpolynomial_free(pwqp);
4584 isl_pw_qpolynomial_free(data.res);
4585 return NULL;
4588 static int poly_entry(void **entry, void *user)
4590 int *sign = user;
4591 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4593 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4595 return *pwqp ? 0 : -1;
4598 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4599 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4601 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4602 if (!upwqp)
4603 return NULL;
4605 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4606 &poly_entry, &sign) < 0)
4607 goto error;
4609 return upwqp;
4610 error:
4611 isl_union_pw_qpolynomial_free(upwqp);
4612 return NULL;
4615 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4616 __isl_take isl_qpolynomial *qp)
4618 int i, k;
4619 isl_dim *dim;
4620 isl_vec *aff = NULL;
4621 isl_basic_map *bmap = NULL;
4622 unsigned pos;
4623 unsigned n_div;
4625 if (!qp)
4626 return NULL;
4627 if (!isl_upoly_is_affine(qp->upoly))
4628 isl_die(qp->dim->ctx, isl_error_invalid,
4629 "input quasi-polynomial not affine", goto error);
4630 aff = isl_qpolynomial_extract_affine(qp);
4631 if (!aff)
4632 goto error;
4633 dim = isl_qpolynomial_get_dim(qp);
4634 dim = isl_dim_from_domain(dim);
4635 pos = 1 + isl_dim_offset(dim, isl_dim_out);
4636 dim = isl_dim_add(dim, isl_dim_out, 1);
4637 n_div = qp->div->n_row;
4638 bmap = isl_basic_map_alloc_dim(dim, n_div, 1, 2 * n_div);
4640 for (i = 0; i < n_div; ++i) {
4641 k = isl_basic_map_alloc_div(bmap);
4642 if (k < 0)
4643 goto error;
4644 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4645 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4646 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4647 goto error;
4649 k = isl_basic_map_alloc_equality(bmap);
4650 if (k < 0)
4651 goto error;
4652 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4653 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4654 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4656 isl_vec_free(aff);
4657 isl_qpolynomial_free(qp);
4658 bmap = isl_basic_map_finalize(bmap);
4659 return bmap;
4660 error:
4661 isl_vec_free(aff);
4662 isl_qpolynomial_free(qp);
4663 isl_basic_map_free(bmap);
4664 return NULL;