add isl_basic_map_get_tuple_name
[isl.git] / isl_polynomial.c
blob444d8ebfe954ef5648d1fa89288bf7104bbf7983
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_factorization.h>
13 #include <isl_lp.h>
14 #include <isl_seq.h>
15 #include <isl_union_map_private.h>
16 #include <isl_polynomial_private.h>
17 #include <isl_point_private.h>
18 #include <isl_dim_private.h>
19 #include <isl_map_private.h>
20 #include <isl_mat_private.h>
22 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
24 switch (type) {
25 case isl_dim_param: return 0;
26 case isl_dim_in: return dim->nparam;
27 case isl_dim_out: return dim->nparam + dim->n_in;
28 default: return 0;
32 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
34 if (!up)
35 return -1;
37 return up->var < 0;
40 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
42 if (!up)
43 return NULL;
45 isl_assert(up->ctx, up->var < 0, return NULL);
47 return (struct isl_upoly_cst *)up;
50 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
52 if (!up)
53 return NULL;
55 isl_assert(up->ctx, up->var >= 0, return NULL);
57 return (struct isl_upoly_rec *)up;
60 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
61 __isl_keep struct isl_upoly *up2)
63 int i;
64 struct isl_upoly_rec *rec1, *rec2;
66 if (!up1 || !up2)
67 return -1;
68 if (up1 == up2)
69 return 1;
70 if (up1->var != up2->var)
71 return 0;
72 if (isl_upoly_is_cst(up1)) {
73 struct isl_upoly_cst *cst1, *cst2;
74 cst1 = isl_upoly_as_cst(up1);
75 cst2 = isl_upoly_as_cst(up2);
76 if (!cst1 || !cst2)
77 return -1;
78 return isl_int_eq(cst1->n, cst2->n) &&
79 isl_int_eq(cst1->d, cst2->d);
82 rec1 = isl_upoly_as_rec(up1);
83 rec2 = isl_upoly_as_rec(up2);
84 if (!rec1 || !rec2)
85 return -1;
87 if (rec1->n != rec2->n)
88 return 0;
90 for (i = 0; i < rec1->n; ++i) {
91 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
92 if (eq < 0 || !eq)
93 return eq;
96 return 1;
99 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
101 struct isl_upoly_cst *cst;
103 if (!up)
104 return -1;
105 if (!isl_upoly_is_cst(up))
106 return 0;
108 cst = isl_upoly_as_cst(up);
109 if (!cst)
110 return -1;
112 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
115 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
117 struct isl_upoly_cst *cst;
119 if (!up)
120 return 0;
121 if (!isl_upoly_is_cst(up))
122 return 0;
124 cst = isl_upoly_as_cst(up);
125 if (!cst)
126 return 0;
128 return isl_int_sgn(cst->n);
131 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
133 struct isl_upoly_cst *cst;
135 if (!up)
136 return -1;
137 if (!isl_upoly_is_cst(up))
138 return 0;
140 cst = isl_upoly_as_cst(up);
141 if (!cst)
142 return -1;
144 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
147 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
149 struct isl_upoly_cst *cst;
151 if (!up)
152 return -1;
153 if (!isl_upoly_is_cst(up))
154 return 0;
156 cst = isl_upoly_as_cst(up);
157 if (!cst)
158 return -1;
160 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
163 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
165 struct isl_upoly_cst *cst;
167 if (!up)
168 return -1;
169 if (!isl_upoly_is_cst(up))
170 return 0;
172 cst = isl_upoly_as_cst(up);
173 if (!cst)
174 return -1;
176 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
179 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
181 struct isl_upoly_cst *cst;
183 if (!up)
184 return -1;
185 if (!isl_upoly_is_cst(up))
186 return 0;
188 cst = isl_upoly_as_cst(up);
189 if (!cst)
190 return -1;
192 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
195 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
197 struct isl_upoly_cst *cst;
199 if (!up)
200 return -1;
201 if (!isl_upoly_is_cst(up))
202 return 0;
204 cst = isl_upoly_as_cst(up);
205 if (!cst)
206 return -1;
208 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
211 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
213 struct isl_upoly_cst *cst;
215 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
216 if (!cst)
217 return NULL;
219 cst->up.ref = 1;
220 cst->up.ctx = ctx;
221 isl_ctx_ref(ctx);
222 cst->up.var = -1;
224 isl_int_init(cst->n);
225 isl_int_init(cst->d);
227 return cst;
230 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
232 struct isl_upoly_cst *cst;
234 cst = isl_upoly_cst_alloc(ctx);
235 if (!cst)
236 return NULL;
238 isl_int_set_si(cst->n, 0);
239 isl_int_set_si(cst->d, 1);
241 return &cst->up;
244 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
246 struct isl_upoly_cst *cst;
248 cst = isl_upoly_cst_alloc(ctx);
249 if (!cst)
250 return NULL;
252 isl_int_set_si(cst->n, 1);
253 isl_int_set_si(cst->d, 1);
255 return &cst->up;
258 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
260 struct isl_upoly_cst *cst;
262 cst = isl_upoly_cst_alloc(ctx);
263 if (!cst)
264 return NULL;
266 isl_int_set_si(cst->n, 1);
267 isl_int_set_si(cst->d, 0);
269 return &cst->up;
272 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
274 struct isl_upoly_cst *cst;
276 cst = isl_upoly_cst_alloc(ctx);
277 if (!cst)
278 return NULL;
280 isl_int_set_si(cst->n, -1);
281 isl_int_set_si(cst->d, 0);
283 return &cst->up;
286 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
288 struct isl_upoly_cst *cst;
290 cst = isl_upoly_cst_alloc(ctx);
291 if (!cst)
292 return NULL;
294 isl_int_set_si(cst->n, 0);
295 isl_int_set_si(cst->d, 0);
297 return &cst->up;
300 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
301 isl_int n, isl_int d)
303 struct isl_upoly_cst *cst;
305 cst = isl_upoly_cst_alloc(ctx);
306 if (!cst)
307 return NULL;
309 isl_int_set(cst->n, n);
310 isl_int_set(cst->d, d);
312 return &cst->up;
315 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
316 int var, int size)
318 struct isl_upoly_rec *rec;
320 isl_assert(ctx, var >= 0, return NULL);
321 isl_assert(ctx, size >= 0, return NULL);
322 rec = isl_calloc(ctx, struct isl_upoly_rec,
323 sizeof(struct isl_upoly_rec) +
324 (size - 1) * sizeof(struct isl_upoly *));
325 if (!rec)
326 return NULL;
328 rec->up.ref = 1;
329 rec->up.ctx = ctx;
330 isl_ctx_ref(ctx);
331 rec->up.var = var;
333 rec->n = 0;
334 rec->size = size;
336 return rec;
339 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
340 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
342 qp = isl_qpolynomial_cow(qp);
343 if (!qp || !dim)
344 goto error;
346 isl_dim_free(qp->dim);
347 qp->dim = dim;
349 return qp;
350 error:
351 isl_qpolynomial_free(qp);
352 isl_dim_free(dim);
353 return NULL;
356 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
358 return qp ? qp->dim->ctx : NULL;
361 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
363 return qp ? isl_dim_copy(qp->dim) : NULL;
366 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
367 enum isl_dim_type type)
369 return qp ? isl_dim_size(qp->dim, type) : 0;
372 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
374 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
377 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
379 return qp ? isl_upoly_is_one(qp->upoly) : -1;
382 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
384 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
387 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
389 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
392 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
394 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
397 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
399 return qp ? isl_upoly_sgn(qp->upoly) : 0;
402 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
404 isl_int_clear(cst->n);
405 isl_int_clear(cst->d);
408 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
410 int i;
412 for (i = 0; i < rec->n; ++i)
413 isl_upoly_free(rec->p[i]);
416 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
418 if (!up)
419 return NULL;
421 up->ref++;
422 return up;
425 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
427 struct isl_upoly_cst *cst;
428 struct isl_upoly_cst *dup;
430 cst = isl_upoly_as_cst(up);
431 if (!cst)
432 return NULL;
434 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
435 if (!dup)
436 return NULL;
437 isl_int_set(dup->n, cst->n);
438 isl_int_set(dup->d, cst->d);
440 return &dup->up;
443 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
445 int i;
446 struct isl_upoly_rec *rec;
447 struct isl_upoly_rec *dup;
449 rec = isl_upoly_as_rec(up);
450 if (!rec)
451 return NULL;
453 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
454 if (!dup)
455 return NULL;
457 for (i = 0; i < rec->n; ++i) {
458 dup->p[i] = isl_upoly_copy(rec->p[i]);
459 if (!dup->p[i])
460 goto error;
461 dup->n++;
464 return &dup->up;
465 error:
466 isl_upoly_free(&dup->up);
467 return NULL;
470 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
472 struct isl_upoly *dup;
474 if (!up)
475 return NULL;
477 if (isl_upoly_is_cst(up))
478 return isl_upoly_dup_cst(up);
479 else
480 return isl_upoly_dup_rec(up);
483 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
485 if (!up)
486 return NULL;
488 if (up->ref == 1)
489 return up;
490 up->ref--;
491 return isl_upoly_dup(up);
494 void isl_upoly_free(__isl_take struct isl_upoly *up)
496 if (!up)
497 return;
499 if (--up->ref > 0)
500 return;
502 if (up->var < 0)
503 upoly_free_cst((struct isl_upoly_cst *)up);
504 else
505 upoly_free_rec((struct isl_upoly_rec *)up);
507 isl_ctx_deref(up->ctx);
508 free(up);
511 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
513 isl_int gcd;
515 isl_int_init(gcd);
516 isl_int_gcd(gcd, cst->n, cst->d);
517 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
518 isl_int_divexact(cst->n, cst->n, gcd);
519 isl_int_divexact(cst->d, cst->d, gcd);
521 isl_int_clear(gcd);
524 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
525 __isl_take struct isl_upoly *up2)
527 struct isl_upoly_cst *cst1;
528 struct isl_upoly_cst *cst2;
530 up1 = isl_upoly_cow(up1);
531 if (!up1 || !up2)
532 goto error;
534 cst1 = isl_upoly_as_cst(up1);
535 cst2 = isl_upoly_as_cst(up2);
537 if (isl_int_eq(cst1->d, cst2->d))
538 isl_int_add(cst1->n, cst1->n, cst2->n);
539 else {
540 isl_int_mul(cst1->n, cst1->n, cst2->d);
541 isl_int_addmul(cst1->n, cst2->n, cst1->d);
542 isl_int_mul(cst1->d, cst1->d, cst2->d);
545 isl_upoly_cst_reduce(cst1);
547 isl_upoly_free(up2);
548 return up1;
549 error:
550 isl_upoly_free(up1);
551 isl_upoly_free(up2);
552 return NULL;
555 static __isl_give struct isl_upoly *replace_by_zero(
556 __isl_take struct isl_upoly *up)
558 struct isl_ctx *ctx;
560 if (!up)
561 return NULL;
562 ctx = up->ctx;
563 isl_upoly_free(up);
564 return isl_upoly_zero(ctx);
567 static __isl_give struct isl_upoly *replace_by_constant_term(
568 __isl_take struct isl_upoly *up)
570 struct isl_upoly_rec *rec;
571 struct isl_upoly *cst;
573 if (!up)
574 return NULL;
576 rec = isl_upoly_as_rec(up);
577 if (!rec)
578 goto error;
579 cst = isl_upoly_copy(rec->p[0]);
580 isl_upoly_free(up);
581 return cst;
582 error:
583 isl_upoly_free(up);
584 return NULL;
587 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
588 __isl_take struct isl_upoly *up2)
590 int i;
591 struct isl_upoly_rec *rec1, *rec2;
593 if (!up1 || !up2)
594 goto error;
596 if (isl_upoly_is_nan(up1)) {
597 isl_upoly_free(up2);
598 return up1;
601 if (isl_upoly_is_nan(up2)) {
602 isl_upoly_free(up1);
603 return up2;
606 if (isl_upoly_is_zero(up1)) {
607 isl_upoly_free(up1);
608 return up2;
611 if (isl_upoly_is_zero(up2)) {
612 isl_upoly_free(up2);
613 return up1;
616 if (up1->var < up2->var)
617 return isl_upoly_sum(up2, up1);
619 if (up2->var < up1->var) {
620 struct isl_upoly_rec *rec;
621 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
622 isl_upoly_free(up1);
623 return up2;
625 up1 = isl_upoly_cow(up1);
626 rec = isl_upoly_as_rec(up1);
627 if (!rec)
628 goto error;
629 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
630 if (rec->n == 1)
631 up1 = replace_by_constant_term(up1);
632 return up1;
635 if (isl_upoly_is_cst(up1))
636 return isl_upoly_sum_cst(up1, up2);
638 rec1 = isl_upoly_as_rec(up1);
639 rec2 = isl_upoly_as_rec(up2);
640 if (!rec1 || !rec2)
641 goto error;
643 if (rec1->n < rec2->n)
644 return isl_upoly_sum(up2, up1);
646 up1 = isl_upoly_cow(up1);
647 rec1 = isl_upoly_as_rec(up1);
648 if (!rec1)
649 goto error;
651 for (i = rec2->n - 1; i >= 0; --i) {
652 rec1->p[i] = isl_upoly_sum(rec1->p[i],
653 isl_upoly_copy(rec2->p[i]));
654 if (!rec1->p[i])
655 goto error;
656 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
657 isl_upoly_free(rec1->p[i]);
658 rec1->n--;
662 if (rec1->n == 0)
663 up1 = replace_by_zero(up1);
664 else if (rec1->n == 1)
665 up1 = replace_by_constant_term(up1);
667 isl_upoly_free(up2);
669 return up1;
670 error:
671 isl_upoly_free(up1);
672 isl_upoly_free(up2);
673 return NULL;
676 __isl_give struct isl_upoly *isl_upoly_neg_cst(__isl_take struct isl_upoly *up)
678 struct isl_upoly_cst *cst;
680 if (isl_upoly_is_zero(up))
681 return up;
683 up = isl_upoly_cow(up);
684 if (!up)
685 return NULL;
687 cst = isl_upoly_as_cst(up);
689 isl_int_neg(cst->n, cst->n);
691 return up;
694 __isl_give struct isl_upoly *isl_upoly_neg(__isl_take struct isl_upoly *up)
696 int i;
697 struct isl_upoly_rec *rec;
699 if (!up)
700 return NULL;
702 if (isl_upoly_is_cst(up))
703 return isl_upoly_neg_cst(up);
705 up = isl_upoly_cow(up);
706 rec = isl_upoly_as_rec(up);
707 if (!rec)
708 goto error;
710 for (i = 0; i < rec->n; ++i) {
711 rec->p[i] = isl_upoly_neg(rec->p[i]);
712 if (!rec->p[i])
713 goto error;
716 return up;
717 error:
718 isl_upoly_free(up);
719 return NULL;
722 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
723 __isl_take struct isl_upoly *up2)
725 struct isl_upoly_cst *cst1;
726 struct isl_upoly_cst *cst2;
728 up1 = isl_upoly_cow(up1);
729 if (!up1 || !up2)
730 goto error;
732 cst1 = isl_upoly_as_cst(up1);
733 cst2 = isl_upoly_as_cst(up2);
735 isl_int_mul(cst1->n, cst1->n, cst2->n);
736 isl_int_mul(cst1->d, cst1->d, cst2->d);
738 isl_upoly_cst_reduce(cst1);
740 isl_upoly_free(up2);
741 return up1;
742 error:
743 isl_upoly_free(up1);
744 isl_upoly_free(up2);
745 return NULL;
748 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
749 __isl_take struct isl_upoly *up2)
751 struct isl_upoly_rec *rec1;
752 struct isl_upoly_rec *rec2;
753 struct isl_upoly_rec *res;
754 int i, j;
755 int size;
757 rec1 = isl_upoly_as_rec(up1);
758 rec2 = isl_upoly_as_rec(up2);
759 if (!rec1 || !rec2)
760 goto error;
761 size = rec1->n + rec2->n - 1;
762 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
763 if (!res)
764 goto error;
766 for (i = 0; i < rec1->n; ++i) {
767 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
768 isl_upoly_copy(rec1->p[i]));
769 if (!res->p[i])
770 goto error;
771 res->n++;
773 for (; i < size; ++i) {
774 res->p[i] = isl_upoly_zero(up1->ctx);
775 if (!res->p[i])
776 goto error;
777 res->n++;
779 for (i = 0; i < rec1->n; ++i) {
780 for (j = 1; j < rec2->n; ++j) {
781 struct isl_upoly *up;
782 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
783 isl_upoly_copy(rec1->p[i]));
784 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
785 if (!res->p[i + j])
786 goto error;
790 isl_upoly_free(up1);
791 isl_upoly_free(up2);
793 return &res->up;
794 error:
795 isl_upoly_free(up1);
796 isl_upoly_free(up2);
797 isl_upoly_free(&res->up);
798 return NULL;
801 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
802 __isl_take struct isl_upoly *up2)
804 if (!up1 || !up2)
805 goto error;
807 if (isl_upoly_is_nan(up1)) {
808 isl_upoly_free(up2);
809 return up1;
812 if (isl_upoly_is_nan(up2)) {
813 isl_upoly_free(up1);
814 return up2;
817 if (isl_upoly_is_zero(up1)) {
818 isl_upoly_free(up2);
819 return up1;
822 if (isl_upoly_is_zero(up2)) {
823 isl_upoly_free(up1);
824 return up2;
827 if (isl_upoly_is_one(up1)) {
828 isl_upoly_free(up1);
829 return up2;
832 if (isl_upoly_is_one(up2)) {
833 isl_upoly_free(up2);
834 return up1;
837 if (up1->var < up2->var)
838 return isl_upoly_mul(up2, up1);
840 if (up2->var < up1->var) {
841 int i;
842 struct isl_upoly_rec *rec;
843 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
844 isl_ctx *ctx = up1->ctx;
845 isl_upoly_free(up1);
846 isl_upoly_free(up2);
847 return isl_upoly_nan(ctx);
849 up1 = isl_upoly_cow(up1);
850 rec = isl_upoly_as_rec(up1);
851 if (!rec)
852 goto error;
854 for (i = 0; i < rec->n; ++i) {
855 rec->p[i] = isl_upoly_mul(rec->p[i],
856 isl_upoly_copy(up2));
857 if (!rec->p[i])
858 goto error;
860 isl_upoly_free(up2);
861 return up1;
864 if (isl_upoly_is_cst(up1))
865 return isl_upoly_mul_cst(up1, up2);
867 return isl_upoly_mul_rec(up1, up2);
868 error:
869 isl_upoly_free(up1);
870 isl_upoly_free(up2);
871 return NULL;
874 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
875 unsigned n_div, __isl_take struct isl_upoly *up)
877 struct isl_qpolynomial *qp = NULL;
878 unsigned total;
880 if (!dim || !up)
881 goto error;
883 total = isl_dim_total(dim);
885 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
886 if (!qp)
887 goto error;
889 qp->ref = 1;
890 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
891 if (!qp->div)
892 goto error;
894 qp->dim = dim;
895 qp->upoly = up;
897 return qp;
898 error:
899 isl_dim_free(dim);
900 isl_upoly_free(up);
901 isl_qpolynomial_free(qp);
902 return NULL;
905 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
907 if (!qp)
908 return NULL;
910 qp->ref++;
911 return qp;
914 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
916 struct isl_qpolynomial *dup;
918 if (!qp)
919 return NULL;
921 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
922 isl_upoly_copy(qp->upoly));
923 if (!dup)
924 return NULL;
925 isl_mat_free(dup->div);
926 dup->div = isl_mat_copy(qp->div);
927 if (!dup->div)
928 goto error;
930 return dup;
931 error:
932 isl_qpolynomial_free(dup);
933 return NULL;
936 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
938 if (!qp)
939 return NULL;
941 if (qp->ref == 1)
942 return qp;
943 qp->ref--;
944 return isl_qpolynomial_dup(qp);
947 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
949 if (!qp)
950 return;
952 if (--qp->ref > 0)
953 return;
955 isl_dim_free(qp->dim);
956 isl_mat_free(qp->div);
957 isl_upoly_free(qp->upoly);
959 free(qp);
962 __isl_give struct isl_upoly *isl_upoly_pow(isl_ctx *ctx, int pos, int power)
964 int i;
965 struct isl_upoly *up;
966 struct isl_upoly_rec *rec;
967 struct isl_upoly_cst *cst;
969 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
970 if (!rec)
971 return NULL;
972 for (i = 0; i < 1 + power; ++i) {
973 rec->p[i] = isl_upoly_zero(ctx);
974 if (!rec->p[i])
975 goto error;
976 rec->n++;
978 cst = isl_upoly_as_cst(rec->p[power]);
979 isl_int_set_si(cst->n, 1);
981 return &rec->up;
982 error:
983 isl_upoly_free(&rec->up);
984 return NULL;
987 /* r array maps original positions to new positions.
989 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
990 int *r)
992 int i;
993 struct isl_upoly_rec *rec;
994 struct isl_upoly *base;
995 struct isl_upoly *res;
997 if (isl_upoly_is_cst(up))
998 return up;
1000 rec = isl_upoly_as_rec(up);
1001 if (!rec)
1002 goto error;
1004 isl_assert(up->ctx, rec->n >= 1, goto error);
1006 base = isl_upoly_pow(up->ctx, r[up->var], 1);
1007 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1009 for (i = rec->n - 2; i >= 0; --i) {
1010 res = isl_upoly_mul(res, isl_upoly_copy(base));
1011 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1014 isl_upoly_free(base);
1015 isl_upoly_free(up);
1017 return res;
1018 error:
1019 isl_upoly_free(up);
1020 return NULL;
1023 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1025 int n_row, n_col;
1026 int equal;
1028 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1029 div1->n_col >= div2->n_col, return -1);
1031 if (div1->n_row == div2->n_row)
1032 return isl_mat_is_equal(div1, div2);
1034 n_row = div1->n_row;
1035 n_col = div1->n_col;
1036 div1->n_row = div2->n_row;
1037 div1->n_col = div2->n_col;
1039 equal = isl_mat_is_equal(div1, div2);
1041 div1->n_row = n_row;
1042 div1->n_col = n_col;
1044 return equal;
1047 static void expand_row(__isl_keep isl_mat *dst, int d,
1048 __isl_keep isl_mat *src, int s, int *exp)
1050 int i;
1051 unsigned c = src->n_col - src->n_row;
1053 isl_seq_cpy(dst->row[d], src->row[s], c);
1054 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
1056 for (i = 0; i < s; ++i)
1057 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
1060 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1062 int li, lj;
1064 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1065 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1067 if (li != lj)
1068 return li - lj;
1070 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1073 struct isl_div_sort_info {
1074 isl_mat *div;
1075 int row;
1078 static int div_sort_cmp(const void *p1, const void *p2)
1080 const struct isl_div_sort_info *i1, *i2;
1081 i1 = (const struct isl_div_sort_info *) p1;
1082 i2 = (const struct isl_div_sort_info *) p2;
1084 return cmp_row(i1->div, i1->row, i2->row);
1087 /* Sort divs and remove duplicates.
1089 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1091 int i;
1092 int skip;
1093 int len;
1094 struct isl_div_sort_info *array = NULL;
1095 int *pos = NULL, *at = NULL;
1096 int *reordering = NULL;
1097 unsigned div_pos;
1099 if (!qp)
1100 return NULL;
1101 if (qp->div->n_row <= 1)
1102 return qp;
1104 div_pos = isl_dim_total(qp->dim);
1106 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1107 qp->div->n_row);
1108 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1109 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1110 len = qp->div->n_col - 2;
1111 reordering = isl_alloc_array(qp->div->ctx, int, len);
1112 if (!array || !pos || !at || !reordering)
1113 goto error;
1115 for (i = 0; i < qp->div->n_row; ++i) {
1116 array[i].div = qp->div;
1117 array[i].row = i;
1118 pos[i] = i;
1119 at[i] = i;
1122 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1123 div_sort_cmp);
1125 for (i = 0; i < div_pos; ++i)
1126 reordering[i] = i;
1128 for (i = 0; i < qp->div->n_row; ++i) {
1129 if (pos[array[i].row] == i)
1130 continue;
1131 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1132 pos[at[i]] = pos[array[i].row];
1133 at[pos[array[i].row]] = at[i];
1134 at[i] = array[i].row;
1135 pos[array[i].row] = i;
1138 skip = 0;
1139 for (i = 0; i < len - div_pos; ++i) {
1140 if (i > 0 &&
1141 isl_seq_eq(qp->div->row[i - skip - 1],
1142 qp->div->row[i - skip], qp->div->n_col)) {
1143 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1144 qp->div = isl_mat_drop_cols(qp->div,
1145 2 + div_pos + i - skip, 1);
1146 skip++;
1148 reordering[div_pos + array[i].row] = div_pos + i - skip;
1151 qp->upoly = reorder(qp->upoly, reordering);
1153 if (!qp->upoly || !qp->div)
1154 goto error;
1156 free(at);
1157 free(pos);
1158 free(array);
1159 free(reordering);
1161 return qp;
1162 error:
1163 free(at);
1164 free(pos);
1165 free(array);
1166 free(reordering);
1167 isl_qpolynomial_free(qp);
1168 return NULL;
1171 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1172 __isl_keep isl_mat *div2, int *exp1, int *exp2)
1174 int i, j, k;
1175 isl_mat *div = NULL;
1176 unsigned d = div1->n_col - div1->n_row;
1178 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1179 d + div1->n_row + div2->n_row);
1180 if (!div)
1181 return NULL;
1183 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1184 int cmp;
1186 expand_row(div, k, div1, i, exp1);
1187 expand_row(div, k + 1, div2, j, exp2);
1189 cmp = cmp_row(div, k, k + 1);
1190 if (cmp == 0) {
1191 exp1[i++] = k;
1192 exp2[j++] = k;
1193 } else if (cmp < 0) {
1194 exp1[i++] = k;
1195 } else {
1196 exp2[j++] = k;
1197 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1200 for (; i < div1->n_row; ++i, ++k) {
1201 expand_row(div, k, div1, i, exp1);
1202 exp1[i] = k;
1204 for (; j < div2->n_row; ++j, ++k) {
1205 expand_row(div, k, div2, j, exp2);
1206 exp2[j] = k;
1209 div->n_row = k;
1210 div->n_col = d + k;
1212 return div;
1215 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1216 int *exp, int first)
1218 int i;
1219 struct isl_upoly_rec *rec;
1221 if (isl_upoly_is_cst(up))
1222 return up;
1224 if (up->var < first)
1225 return up;
1227 if (exp[up->var - first] == up->var - first)
1228 return up;
1230 up = isl_upoly_cow(up);
1231 if (!up)
1232 goto error;
1234 up->var = exp[up->var - first] + first;
1236 rec = isl_upoly_as_rec(up);
1237 if (!rec)
1238 goto error;
1240 for (i = 0; i < rec->n; ++i) {
1241 rec->p[i] = expand(rec->p[i], exp, first);
1242 if (!rec->p[i])
1243 goto error;
1246 return up;
1247 error:
1248 isl_upoly_free(up);
1249 return NULL;
1252 static __isl_give isl_qpolynomial *with_merged_divs(
1253 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1254 __isl_take isl_qpolynomial *qp2),
1255 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1257 int *exp1 = NULL;
1258 int *exp2 = NULL;
1259 isl_mat *div = NULL;
1261 qp1 = isl_qpolynomial_cow(qp1);
1262 qp2 = isl_qpolynomial_cow(qp2);
1264 if (!qp1 || !qp2)
1265 goto error;
1267 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1268 qp1->div->n_col >= qp2->div->n_col, goto error);
1270 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1271 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1272 if (!exp1 || !exp2)
1273 goto error;
1275 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1276 if (!div)
1277 goto error;
1279 isl_mat_free(qp1->div);
1280 qp1->div = isl_mat_copy(div);
1281 isl_mat_free(qp2->div);
1282 qp2->div = isl_mat_copy(div);
1284 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1285 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1287 if (!qp1->upoly || !qp2->upoly)
1288 goto error;
1290 isl_mat_free(div);
1291 free(exp1);
1292 free(exp2);
1294 return fn(qp1, qp2);
1295 error:
1296 isl_mat_free(div);
1297 free(exp1);
1298 free(exp2);
1299 isl_qpolynomial_free(qp1);
1300 isl_qpolynomial_free(qp2);
1301 return NULL;
1304 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1305 __isl_take isl_qpolynomial *qp2)
1307 qp1 = isl_qpolynomial_cow(qp1);
1309 if (!qp1 || !qp2)
1310 goto error;
1312 if (qp1->div->n_row < qp2->div->n_row)
1313 return isl_qpolynomial_add(qp2, qp1);
1315 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1316 if (!compatible_divs(qp1->div, qp2->div))
1317 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1319 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1320 if (!qp1->upoly)
1321 goto error;
1323 isl_qpolynomial_free(qp2);
1325 return qp1;
1326 error:
1327 isl_qpolynomial_free(qp1);
1328 isl_qpolynomial_free(qp2);
1329 return NULL;
1332 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1333 __isl_keep isl_set *dom,
1334 __isl_take isl_qpolynomial *qp1,
1335 __isl_take isl_qpolynomial *qp2)
1337 return isl_qpolynomial_add(qp1, qp2);
1340 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1341 __isl_take isl_qpolynomial *qp2)
1343 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1346 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1348 qp = isl_qpolynomial_cow(qp);
1350 if (!qp)
1351 return NULL;
1353 qp->upoly = isl_upoly_neg(qp->upoly);
1354 if (!qp->upoly)
1355 goto error;
1357 return qp;
1358 error:
1359 isl_qpolynomial_free(qp);
1360 return NULL;
1363 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1364 __isl_take isl_qpolynomial *qp2)
1366 qp1 = isl_qpolynomial_cow(qp1);
1368 if (!qp1 || !qp2)
1369 goto error;
1371 if (qp1->div->n_row < qp2->div->n_row)
1372 return isl_qpolynomial_mul(qp2, qp1);
1374 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1375 if (!compatible_divs(qp1->div, qp2->div))
1376 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1378 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1379 if (!qp1->upoly)
1380 goto error;
1382 isl_qpolynomial_free(qp2);
1384 return qp1;
1385 error:
1386 isl_qpolynomial_free(qp1);
1387 isl_qpolynomial_free(qp2);
1388 return NULL;
1391 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1393 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1396 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1398 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1401 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1403 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1406 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1408 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1411 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1413 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1416 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1417 isl_int v)
1419 struct isl_qpolynomial *qp;
1420 struct isl_upoly_cst *cst;
1422 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1423 if (!qp)
1424 return NULL;
1426 cst = isl_upoly_as_cst(qp->upoly);
1427 isl_int_set(cst->n, v);
1429 return qp;
1432 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1433 isl_int *n, isl_int *d)
1435 struct isl_upoly_cst *cst;
1437 if (!qp)
1438 return -1;
1440 if (!isl_upoly_is_cst(qp->upoly))
1441 return 0;
1443 cst = isl_upoly_as_cst(qp->upoly);
1444 if (!cst)
1445 return -1;
1447 if (n)
1448 isl_int_set(*n, cst->n);
1449 if (d)
1450 isl_int_set(*d, cst->d);
1452 return 1;
1455 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1457 int is_cst;
1458 struct isl_upoly_rec *rec;
1460 if (!up)
1461 return -1;
1463 if (up->var < 0)
1464 return 1;
1466 rec = isl_upoly_as_rec(up);
1467 if (!rec)
1468 return -1;
1470 if (rec->n > 2)
1471 return 0;
1473 isl_assert(up->ctx, rec->n > 1, return -1);
1475 is_cst = isl_upoly_is_cst(rec->p[1]);
1476 if (is_cst < 0)
1477 return -1;
1478 if (!is_cst)
1479 return 0;
1481 return isl_upoly_is_affine(rec->p[0]);
1484 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1486 if (!qp)
1487 return -1;
1489 if (qp->div->n_row > 0)
1490 return 0;
1492 return isl_upoly_is_affine(qp->upoly);
1495 static void update_coeff(__isl_keep isl_vec *aff,
1496 __isl_keep struct isl_upoly_cst *cst, int pos)
1498 isl_int gcd;
1499 isl_int f;
1501 if (isl_int_is_zero(cst->n))
1502 return;
1504 isl_int_init(gcd);
1505 isl_int_init(f);
1506 isl_int_gcd(gcd, cst->d, aff->el[0]);
1507 isl_int_divexact(f, cst->d, gcd);
1508 isl_int_divexact(gcd, aff->el[0], gcd);
1509 isl_seq_scale(aff->el, aff->el, f, aff->size);
1510 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1511 isl_int_clear(gcd);
1512 isl_int_clear(f);
1515 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1516 __isl_keep isl_vec *aff)
1518 struct isl_upoly_cst *cst;
1519 struct isl_upoly_rec *rec;
1521 if (!up || !aff)
1522 return -1;
1524 if (up->var < 0) {
1525 struct isl_upoly_cst *cst;
1527 cst = isl_upoly_as_cst(up);
1528 if (!cst)
1529 return -1;
1530 update_coeff(aff, cst, 0);
1531 return 0;
1534 rec = isl_upoly_as_rec(up);
1535 if (!rec)
1536 return -1;
1537 isl_assert(up->ctx, rec->n == 2, return -1);
1539 cst = isl_upoly_as_cst(rec->p[1]);
1540 if (!cst)
1541 return -1;
1542 update_coeff(aff, cst, 1 + up->var);
1544 return isl_upoly_update_affine(rec->p[0], aff);
1547 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1548 __isl_keep isl_qpolynomial *qp)
1550 isl_vec *aff;
1551 unsigned d;
1553 if (!qp)
1554 return NULL;
1556 isl_assert(qp->div->ctx, qp->div->n_row == 0, return NULL);
1557 d = isl_dim_total(qp->dim);
1558 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
1559 if (!aff)
1560 return NULL;
1562 isl_seq_clr(aff->el + 1, 1 + d);
1563 isl_int_set_si(aff->el[0], 1);
1565 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1566 goto error;
1568 return aff;
1569 error:
1570 isl_vec_free(aff);
1571 return NULL;
1574 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1575 __isl_keep isl_qpolynomial *qp2)
1577 if (!qp1 || !qp2)
1578 return -1;
1580 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1583 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1585 int i;
1586 struct isl_upoly_rec *rec;
1588 if (isl_upoly_is_cst(up)) {
1589 struct isl_upoly_cst *cst;
1590 cst = isl_upoly_as_cst(up);
1591 if (!cst)
1592 return;
1593 isl_int_lcm(*d, *d, cst->d);
1594 return;
1597 rec = isl_upoly_as_rec(up);
1598 if (!rec)
1599 return;
1601 for (i = 0; i < rec->n; ++i)
1602 upoly_update_den(rec->p[i], d);
1605 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1607 isl_int_set_si(*d, 1);
1608 if (!qp)
1609 return;
1610 upoly_update_den(qp->upoly, d);
1613 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_dim *dim,
1614 int pos, int power)
1616 struct isl_ctx *ctx;
1618 if (!dim)
1619 return NULL;
1621 ctx = dim->ctx;
1623 return isl_qpolynomial_alloc(dim, 0, isl_upoly_pow(ctx, pos, power));
1626 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1627 enum isl_dim_type type, unsigned pos)
1629 if (!dim)
1630 return NULL;
1632 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1633 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1635 if (type == isl_dim_set)
1636 pos += isl_dim_size(dim, isl_dim_param);
1638 return isl_qpolynomial_pow(dim, pos, 1);
1639 error:
1640 isl_dim_free(dim);
1641 return NULL;
1644 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
1645 int power)
1647 struct isl_qpolynomial *qp = NULL;
1648 struct isl_upoly_rec *rec;
1649 struct isl_upoly_cst *cst;
1650 int i;
1651 int pos;
1653 if (!div)
1654 return NULL;
1655 isl_assert(div->ctx, div->bmap->n_div == 1, goto error);
1657 pos = isl_dim_total(div->bmap->dim);
1658 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
1659 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap), 1,
1660 &rec->up);
1661 if (!qp)
1662 goto error;
1664 isl_seq_cpy(qp->div->row[0], div->line[0], qp->div->n_col - 1);
1665 isl_int_set_si(qp->div->row[0][qp->div->n_col - 1], 0);
1667 for (i = 0; i < 1 + power; ++i) {
1668 rec->p[i] = isl_upoly_zero(div->ctx);
1669 if (!rec->p[i])
1670 goto error;
1671 rec->n++;
1673 cst = isl_upoly_as_cst(rec->p[power]);
1674 isl_int_set_si(cst->n, 1);
1676 isl_div_free(div);
1678 return qp;
1679 error:
1680 isl_qpolynomial_free(qp);
1681 isl_div_free(div);
1682 return NULL;
1685 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
1687 return isl_qpolynomial_div_pow(div, 1);
1690 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
1691 const isl_int n, const isl_int d)
1693 struct isl_qpolynomial *qp;
1694 struct isl_upoly_cst *cst;
1696 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1697 if (!qp)
1698 return NULL;
1700 cst = isl_upoly_as_cst(qp->upoly);
1701 isl_int_set(cst->n, n);
1702 isl_int_set(cst->d, d);
1704 return qp;
1707 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
1709 struct isl_upoly_rec *rec;
1710 int i;
1712 if (!up)
1713 return -1;
1715 if (isl_upoly_is_cst(up))
1716 return 0;
1718 if (up->var < d)
1719 active[up->var] = 1;
1721 rec = isl_upoly_as_rec(up);
1722 for (i = 0; i < rec->n; ++i)
1723 if (up_set_active(rec->p[i], active, d) < 0)
1724 return -1;
1726 return 0;
1729 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
1731 int i, j;
1732 int d = isl_dim_total(qp->dim);
1734 if (!qp || !active)
1735 return -1;
1737 for (i = 0; i < d; ++i)
1738 for (j = 0; j < qp->div->n_row; ++j) {
1739 if (isl_int_is_zero(qp->div->row[j][2 + i]))
1740 continue;
1741 active[i] = 1;
1742 break;
1745 return up_set_active(qp->upoly, active, d);
1748 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
1749 enum isl_dim_type type, unsigned first, unsigned n)
1751 int i;
1752 int *active = NULL;
1753 int involves = 0;
1755 if (!qp)
1756 return -1;
1757 if (n == 0)
1758 return 0;
1760 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1761 return -1);
1762 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1763 type == isl_dim_set, return -1);
1765 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
1766 if (set_active(qp, active) < 0)
1767 goto error;
1769 if (type == isl_dim_set)
1770 first += isl_dim_size(qp->dim, isl_dim_param);
1771 for (i = 0; i < n; ++i)
1772 if (active[first + i]) {
1773 involves = 1;
1774 break;
1777 free(active);
1779 return involves;
1780 error:
1781 free(active);
1782 return -1;
1785 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
1786 unsigned first, unsigned n)
1788 int i;
1789 struct isl_upoly_rec *rec;
1791 if (!up)
1792 return NULL;
1793 if (n == 0 || up->var < 0 || up->var < first)
1794 return up;
1795 if (up->var < first + n) {
1796 up = replace_by_constant_term(up);
1797 return isl_upoly_drop(up, first, n);
1799 up = isl_upoly_cow(up);
1800 if (!up)
1801 return NULL;
1802 up->var -= n;
1803 rec = isl_upoly_as_rec(up);
1804 if (!rec)
1805 goto error;
1807 for (i = 0; i < rec->n; ++i) {
1808 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
1809 if (!rec->p[i])
1810 goto error;
1813 return up;
1814 error:
1815 isl_upoly_free(up);
1816 return NULL;
1819 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
1820 __isl_take isl_qpolynomial *qp,
1821 enum isl_dim_type type, unsigned first, unsigned n)
1823 if (!qp)
1824 return NULL;
1825 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
1826 return qp;
1828 qp = isl_qpolynomial_cow(qp);
1829 if (!qp)
1830 return NULL;
1832 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1833 goto error);
1834 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1835 type == isl_dim_set, goto error);
1837 qp->dim = isl_dim_drop(qp->dim, type, first, n);
1838 if (!qp->dim)
1839 goto error;
1841 if (type == isl_dim_set)
1842 first += isl_dim_size(qp->dim, isl_dim_param);
1844 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
1845 if (!qp->div)
1846 goto error;
1848 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
1849 if (!qp->upoly)
1850 goto error;
1852 return qp;
1853 error:
1854 isl_qpolynomial_free(qp);
1855 return NULL;
1858 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1859 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1861 int i;
1862 struct isl_upoly_rec *rec;
1863 struct isl_upoly *base, *res;
1865 if (!up)
1866 return NULL;
1868 if (isl_upoly_is_cst(up))
1869 return up;
1871 if (up->var < first)
1872 return up;
1874 rec = isl_upoly_as_rec(up);
1875 if (!rec)
1876 goto error;
1878 isl_assert(up->ctx, rec->n >= 1, goto error);
1880 if (up->var >= first + n)
1881 base = isl_upoly_pow(up->ctx, up->var, 1);
1882 else
1883 base = isl_upoly_copy(subs[up->var - first]);
1885 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1886 for (i = rec->n - 2; i >= 0; --i) {
1887 struct isl_upoly *t;
1888 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1889 res = isl_upoly_mul(res, isl_upoly_copy(base));
1890 res = isl_upoly_sum(res, t);
1893 isl_upoly_free(base);
1894 isl_upoly_free(up);
1896 return res;
1897 error:
1898 isl_upoly_free(up);
1899 return NULL;
1902 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1903 isl_int denom, unsigned len)
1905 int i;
1906 struct isl_upoly *up;
1908 isl_assert(ctx, len >= 1, return NULL);
1910 up = isl_upoly_rat_cst(ctx, f[0], denom);
1911 for (i = 0; i < len - 1; ++i) {
1912 struct isl_upoly *t;
1913 struct isl_upoly *c;
1915 if (isl_int_is_zero(f[1 + i]))
1916 continue;
1918 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1919 t = isl_upoly_pow(ctx, i, 1);
1920 t = isl_upoly_mul(c, t);
1921 up = isl_upoly_sum(up, t);
1924 return up;
1927 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
1928 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
1930 int i, j, k;
1931 isl_int denom;
1932 unsigned total;
1933 struct isl_upoly *up;
1935 if (!eq)
1936 goto error;
1937 if (eq->n_eq == 0) {
1938 isl_basic_set_free(eq);
1939 return qp;
1942 qp = isl_qpolynomial_cow(qp);
1943 if (!qp)
1944 goto error;
1945 qp->div = isl_mat_cow(qp->div);
1946 if (!qp->div)
1947 goto error;
1949 total = 1 + isl_dim_total(eq->dim);
1950 isl_int_init(denom);
1951 for (i = 0; i < eq->n_eq; ++i) {
1952 j = isl_seq_last_non_zero(eq->eq[i], total);
1953 if (j < 0 || j == 0)
1954 continue;
1956 for (k = 0; k < qp->div->n_row; ++k) {
1957 if (isl_int_is_zero(qp->div->row[k][1 + j]))
1958 continue;
1959 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
1960 &qp->div->row[k][0]);
1961 isl_seq_normalize(qp->div->ctx,
1962 qp->div->row[k], 1 + total);
1965 if (isl_int_is_pos(eq->eq[i][j]))
1966 isl_seq_neg(eq->eq[i], eq->eq[i], total);
1967 isl_int_abs(denom, eq->eq[i][j]);
1968 isl_int_set_si(eq->eq[i][j], 0);
1970 up = isl_upoly_from_affine(qp->dim->ctx,
1971 eq->eq[i], denom, total);
1972 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
1973 isl_upoly_free(up);
1975 isl_int_clear(denom);
1977 if (!qp->upoly)
1978 goto error;
1980 isl_basic_set_free(eq);
1982 qp = sort_divs(qp);
1984 return qp;
1985 error:
1986 isl_basic_set_free(eq);
1987 isl_qpolynomial_free(qp);
1988 return NULL;
1991 #undef PW
1992 #define PW isl_pw_qpolynomial
1993 #undef EL
1994 #define EL isl_qpolynomial
1995 #undef IS_ZERO
1996 #define IS_ZERO is_zero
1997 #undef FIELD
1998 #define FIELD qp
2000 #include <isl_pw_templ.c>
2002 #undef UNION
2003 #define UNION isl_union_pw_qpolynomial
2004 #undef PART
2005 #define PART isl_pw_qpolynomial
2006 #undef PARTS
2007 #define PARTS pw_qpolynomial
2009 #include <isl_union_templ.c>
2011 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2013 if (!pwqp)
2014 return -1;
2016 if (pwqp->n != -1)
2017 return 0;
2019 if (!isl_set_fast_is_universe(pwqp->p[0].set))
2020 return 0;
2022 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2025 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2026 __isl_take isl_pw_qpolynomial *pwqp1,
2027 __isl_take isl_pw_qpolynomial *pwqp2)
2029 int i, j, n;
2030 struct isl_pw_qpolynomial *res;
2031 isl_set *set;
2033 if (!pwqp1 || !pwqp2)
2034 goto error;
2036 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2037 goto error);
2039 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2040 isl_pw_qpolynomial_free(pwqp2);
2041 return pwqp1;
2044 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2045 isl_pw_qpolynomial_free(pwqp1);
2046 return pwqp2;
2049 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2050 isl_pw_qpolynomial_free(pwqp1);
2051 return pwqp2;
2054 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2055 isl_pw_qpolynomial_free(pwqp2);
2056 return pwqp1;
2059 n = pwqp1->n * pwqp2->n;
2060 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2062 for (i = 0; i < pwqp1->n; ++i) {
2063 for (j = 0; j < pwqp2->n; ++j) {
2064 struct isl_set *common;
2065 struct isl_qpolynomial *prod;
2066 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2067 isl_set_copy(pwqp2->p[j].set));
2068 if (isl_set_fast_is_empty(common)) {
2069 isl_set_free(common);
2070 continue;
2073 prod = isl_qpolynomial_mul(
2074 isl_qpolynomial_copy(pwqp1->p[i].qp),
2075 isl_qpolynomial_copy(pwqp2->p[j].qp));
2077 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2081 isl_pw_qpolynomial_free(pwqp1);
2082 isl_pw_qpolynomial_free(pwqp2);
2084 return res;
2085 error:
2086 isl_pw_qpolynomial_free(pwqp1);
2087 isl_pw_qpolynomial_free(pwqp2);
2088 return NULL;
2091 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2092 __isl_take isl_pw_qpolynomial *pwqp)
2094 int i;
2096 if (!pwqp)
2097 return NULL;
2099 if (isl_pw_qpolynomial_is_zero(pwqp))
2100 return pwqp;
2102 pwqp = isl_pw_qpolynomial_cow(pwqp);
2103 if (!pwqp)
2104 return NULL;
2106 for (i = 0; i < pwqp->n; ++i) {
2107 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2108 if (!pwqp->p[i].qp)
2109 goto error;
2112 return pwqp;
2113 error:
2114 isl_pw_qpolynomial_free(pwqp);
2115 return NULL;
2118 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2119 __isl_take isl_pw_qpolynomial *pwqp1,
2120 __isl_take isl_pw_qpolynomial *pwqp2)
2122 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2125 __isl_give struct isl_upoly *isl_upoly_eval(
2126 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2128 int i;
2129 struct isl_upoly_rec *rec;
2130 struct isl_upoly *res;
2131 struct isl_upoly *base;
2133 if (isl_upoly_is_cst(up)) {
2134 isl_vec_free(vec);
2135 return up;
2138 rec = isl_upoly_as_rec(up);
2139 if (!rec)
2140 goto error;
2142 isl_assert(up->ctx, rec->n >= 1, goto error);
2144 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2146 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2147 isl_vec_copy(vec));
2149 for (i = rec->n - 2; i >= 0; --i) {
2150 res = isl_upoly_mul(res, isl_upoly_copy(base));
2151 res = isl_upoly_sum(res,
2152 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2153 isl_vec_copy(vec)));
2156 isl_upoly_free(base);
2157 isl_upoly_free(up);
2158 isl_vec_free(vec);
2159 return res;
2160 error:
2161 isl_upoly_free(up);
2162 isl_vec_free(vec);
2163 return NULL;
2166 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2167 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2169 isl_vec *ext;
2170 struct isl_upoly *up;
2171 isl_dim *dim;
2173 if (!qp || !pnt)
2174 goto error;
2175 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2177 if (qp->div->n_row == 0)
2178 ext = isl_vec_copy(pnt->vec);
2179 else {
2180 int i;
2181 unsigned dim = isl_dim_total(qp->dim);
2182 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2183 if (!ext)
2184 goto error;
2186 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2187 for (i = 0; i < qp->div->n_row; ++i) {
2188 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2189 1 + dim + i, &ext->el[1+dim+i]);
2190 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2191 qp->div->row[i][0]);
2195 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2196 if (!up)
2197 goto error;
2199 dim = isl_dim_copy(qp->dim);
2200 isl_qpolynomial_free(qp);
2201 isl_point_free(pnt);
2203 return isl_qpolynomial_alloc(dim, 0, up);
2204 error:
2205 isl_qpolynomial_free(qp);
2206 isl_point_free(pnt);
2207 return NULL;
2210 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2211 __isl_keep struct isl_upoly_cst *cst2)
2213 int cmp;
2214 isl_int t;
2215 isl_int_init(t);
2216 isl_int_mul(t, cst1->n, cst2->d);
2217 isl_int_submul(t, cst2->n, cst1->d);
2218 cmp = isl_int_sgn(t);
2219 isl_int_clear(t);
2220 return cmp;
2223 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2224 __isl_keep isl_qpolynomial *qp2)
2226 struct isl_upoly_cst *cst1, *cst2;
2228 if (!qp1 || !qp2)
2229 return -1;
2230 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2231 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2232 if (isl_qpolynomial_is_nan(qp1))
2233 return -1;
2234 if (isl_qpolynomial_is_nan(qp2))
2235 return -1;
2236 cst1 = isl_upoly_as_cst(qp1->upoly);
2237 cst2 = isl_upoly_as_cst(qp2->upoly);
2239 return isl_upoly_cmp(cst1, cst2) <= 0;
2242 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2243 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2245 struct isl_upoly_cst *cst1, *cst2;
2246 int cmp;
2248 if (!qp1 || !qp2)
2249 goto error;
2250 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2251 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2252 cst1 = isl_upoly_as_cst(qp1->upoly);
2253 cst2 = isl_upoly_as_cst(qp2->upoly);
2254 cmp = isl_upoly_cmp(cst1, cst2);
2256 if (cmp <= 0) {
2257 isl_qpolynomial_free(qp2);
2258 } else {
2259 isl_qpolynomial_free(qp1);
2260 qp1 = qp2;
2262 return qp1;
2263 error:
2264 isl_qpolynomial_free(qp1);
2265 isl_qpolynomial_free(qp2);
2266 return NULL;
2269 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2270 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2272 struct isl_upoly_cst *cst1, *cst2;
2273 int cmp;
2275 if (!qp1 || !qp2)
2276 goto error;
2277 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2278 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2279 cst1 = isl_upoly_as_cst(qp1->upoly);
2280 cst2 = isl_upoly_as_cst(qp2->upoly);
2281 cmp = isl_upoly_cmp(cst1, cst2);
2283 if (cmp >= 0) {
2284 isl_qpolynomial_free(qp2);
2285 } else {
2286 isl_qpolynomial_free(qp1);
2287 qp1 = qp2;
2289 return qp1;
2290 error:
2291 isl_qpolynomial_free(qp1);
2292 isl_qpolynomial_free(qp2);
2293 return NULL;
2296 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2297 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2298 unsigned first, unsigned n)
2300 unsigned total;
2301 unsigned g_pos;
2302 int *exp;
2304 if (n == 0)
2305 return qp;
2307 qp = isl_qpolynomial_cow(qp);
2308 if (!qp)
2309 return NULL;
2311 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2312 goto error);
2314 g_pos = pos(qp->dim, type) + first;
2316 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2317 if (!qp->div)
2318 goto error;
2320 total = qp->div->n_col - 2;
2321 if (total > g_pos) {
2322 int i;
2323 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2324 if (!exp)
2325 goto error;
2326 for (i = 0; i < total - g_pos; ++i)
2327 exp[i] = i + n;
2328 qp->upoly = expand(qp->upoly, exp, g_pos);
2329 free(exp);
2330 if (!qp->upoly)
2331 goto error;
2334 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2335 if (!qp->dim)
2336 goto error;
2338 return qp;
2339 error:
2340 isl_qpolynomial_free(qp);
2341 return NULL;
2344 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2345 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2347 unsigned pos;
2349 pos = isl_qpolynomial_dim(qp, type);
2351 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2354 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2355 __isl_take isl_pw_qpolynomial *pwqp,
2356 enum isl_dim_type type, unsigned n)
2358 unsigned pos;
2360 pos = isl_pw_qpolynomial_dim(pwqp, type);
2362 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2365 static int *reordering_move(isl_ctx *ctx,
2366 unsigned len, unsigned dst, unsigned src, unsigned n)
2368 int i;
2369 int *reordering;
2371 reordering = isl_alloc_array(ctx, int, len);
2372 if (!reordering)
2373 return NULL;
2375 if (dst <= src) {
2376 for (i = 0; i < dst; ++i)
2377 reordering[i] = i;
2378 for (i = 0; i < n; ++i)
2379 reordering[src + i] = dst + i;
2380 for (i = 0; i < src - dst; ++i)
2381 reordering[dst + i] = dst + n + i;
2382 for (i = 0; i < len - src - n; ++i)
2383 reordering[src + n + i] = src + n + i;
2384 } else {
2385 for (i = 0; i < src; ++i)
2386 reordering[i] = i;
2387 for (i = 0; i < n; ++i)
2388 reordering[src + i] = dst + i;
2389 for (i = 0; i < dst - src; ++i)
2390 reordering[src + n + i] = src + i;
2391 for (i = 0; i < len - dst - n; ++i)
2392 reordering[dst + n + i] = dst + n + i;
2395 return reordering;
2398 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2399 __isl_take isl_qpolynomial *qp,
2400 enum isl_dim_type dst_type, unsigned dst_pos,
2401 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2403 unsigned g_dst_pos;
2404 unsigned g_src_pos;
2405 int *reordering;
2407 qp = isl_qpolynomial_cow(qp);
2408 if (!qp)
2409 return NULL;
2411 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2412 goto error);
2414 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2415 g_src_pos = pos(qp->dim, src_type) + src_pos;
2416 if (dst_type > src_type)
2417 g_dst_pos -= n;
2419 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2420 if (!qp->div)
2421 goto error;
2422 qp = sort_divs(qp);
2423 if (!qp)
2424 goto error;
2426 reordering = reordering_move(qp->dim->ctx,
2427 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2428 if (!reordering)
2429 goto error;
2431 qp->upoly = reorder(qp->upoly, reordering);
2432 free(reordering);
2433 if (!qp->upoly)
2434 goto error;
2436 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2437 if (!qp->dim)
2438 goto error;
2440 return qp;
2441 error:
2442 isl_qpolynomial_free(qp);
2443 return NULL;
2446 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2447 isl_int *f, isl_int denom)
2449 struct isl_upoly *up;
2451 if (!dim)
2452 return NULL;
2454 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2456 return isl_qpolynomial_alloc(dim, 0, up);
2459 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2460 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2462 isl_int denom;
2463 isl_dim *dim;
2464 struct isl_upoly *up;
2465 isl_qpolynomial *qp;
2466 int sgn;
2468 if (!c)
2469 return NULL;
2471 isl_int_init(denom);
2473 isl_constraint_get_coefficient(c, type, pos, &denom);
2474 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2475 sgn = isl_int_sgn(denom);
2476 isl_int_abs(denom, denom);
2477 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2478 1 + isl_constraint_dim(c, isl_dim_all));
2479 if (sgn < 0)
2480 isl_int_neg(denom, denom);
2481 isl_constraint_set_coefficient(c, type, pos, denom);
2483 dim = isl_dim_copy(c->bmap->dim);
2485 isl_int_clear(denom);
2486 isl_constraint_free(c);
2488 qp = isl_qpolynomial_alloc(dim, 0, up);
2489 if (sgn > 0)
2490 qp = isl_qpolynomial_neg(qp);
2491 return qp;
2494 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2495 * in "qp" by subs[i].
2497 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2498 __isl_take isl_qpolynomial *qp,
2499 enum isl_dim_type type, unsigned first, unsigned n,
2500 __isl_keep isl_qpolynomial **subs)
2502 int i;
2503 struct isl_upoly **ups;
2505 if (n == 0)
2506 return qp;
2508 qp = isl_qpolynomial_cow(qp);
2509 if (!qp)
2510 return NULL;
2511 for (i = 0; i < n; ++i)
2512 if (!subs[i])
2513 goto error;
2515 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2516 goto error);
2518 for (i = 0; i < n; ++i)
2519 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2520 goto error);
2522 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2523 for (i = 0; i < n; ++i)
2524 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2526 first += pos(qp->dim, type);
2528 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2529 if (!ups)
2530 goto error;
2531 for (i = 0; i < n; ++i)
2532 ups[i] = subs[i]->upoly;
2534 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2536 free(ups);
2538 if (!qp->upoly)
2539 goto error;
2541 return qp;
2542 error:
2543 isl_qpolynomial_free(qp);
2544 return NULL;
2547 __isl_give isl_basic_set *add_div_constraints(__isl_take isl_basic_set *bset,
2548 __isl_take isl_mat *div)
2550 int i;
2551 unsigned total;
2553 if (!bset || !div)
2554 goto error;
2556 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2557 if (!bset)
2558 goto error;
2559 total = isl_basic_set_total_dim(bset);
2560 for (i = 0; i < div->n_row; ++i)
2561 if (isl_basic_set_add_div_constraints_var(bset,
2562 total - div->n_row + i, div->row[i]) < 0)
2563 goto error;
2565 isl_mat_free(div);
2566 return bset;
2567 error:
2568 isl_mat_free(div);
2569 isl_basic_set_free(bset);
2570 return NULL;
2573 /* Extend "bset" with extra set dimensions for each integer division
2574 * in "qp" and then call "fn" with the extended bset and the polynomial
2575 * that results from replacing each of the integer divisions by the
2576 * corresponding extra set dimension.
2578 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
2579 __isl_keep isl_basic_set *bset,
2580 int (*fn)(__isl_take isl_basic_set *bset,
2581 __isl_take isl_qpolynomial *poly, void *user), void *user)
2583 isl_dim *dim;
2584 isl_mat *div;
2585 isl_qpolynomial *poly;
2587 if (!qp || !bset)
2588 goto error;
2589 if (qp->div->n_row == 0)
2590 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
2591 user);
2593 div = isl_mat_copy(qp->div);
2594 dim = isl_dim_copy(qp->dim);
2595 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
2596 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
2597 bset = isl_basic_set_copy(bset);
2598 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
2599 bset = add_div_constraints(bset, div);
2601 return fn(bset, poly, user);
2602 error:
2603 return -1;
2606 /* Return total degree in variables first (inclusive) up to last (exclusive).
2608 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
2610 int deg = -1;
2611 int i;
2612 struct isl_upoly_rec *rec;
2614 if (!up)
2615 return -2;
2616 if (isl_upoly_is_zero(up))
2617 return -1;
2618 if (isl_upoly_is_cst(up) || up->var < first)
2619 return 0;
2621 rec = isl_upoly_as_rec(up);
2622 if (!rec)
2623 return -2;
2625 for (i = 0; i < rec->n; ++i) {
2626 int d;
2628 if (isl_upoly_is_zero(rec->p[i]))
2629 continue;
2630 d = isl_upoly_degree(rec->p[i], first, last);
2631 if (up->var < last)
2632 d += i;
2633 if (d > deg)
2634 deg = d;
2637 return deg;
2640 /* Return total degree in set variables.
2642 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
2644 unsigned ovar;
2645 unsigned nvar;
2647 if (!poly)
2648 return -2;
2650 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2651 nvar = isl_dim_size(poly->dim, isl_dim_set);
2652 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
2655 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
2656 unsigned pos, int deg)
2658 int i;
2659 struct isl_upoly_rec *rec;
2661 if (!up)
2662 return NULL;
2664 if (isl_upoly_is_cst(up) || up->var < pos) {
2665 if (deg == 0)
2666 return isl_upoly_copy(up);
2667 else
2668 return isl_upoly_zero(up->ctx);
2671 rec = isl_upoly_as_rec(up);
2672 if (!rec)
2673 return NULL;
2675 if (up->var == pos) {
2676 if (deg < rec->n)
2677 return isl_upoly_copy(rec->p[deg]);
2678 else
2679 return isl_upoly_zero(up->ctx);
2682 up = isl_upoly_copy(up);
2683 up = isl_upoly_cow(up);
2684 rec = isl_upoly_as_rec(up);
2685 if (!rec)
2686 goto error;
2688 for (i = 0; i < rec->n; ++i) {
2689 struct isl_upoly *t;
2690 t = isl_upoly_coeff(rec->p[i], pos, deg);
2691 if (!t)
2692 goto error;
2693 isl_upoly_free(rec->p[i]);
2694 rec->p[i] = t;
2697 return up;
2698 error:
2699 isl_upoly_free(up);
2700 return NULL;
2703 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2705 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
2706 __isl_keep isl_qpolynomial *qp,
2707 enum isl_dim_type type, unsigned t_pos, int deg)
2709 unsigned g_pos;
2710 struct isl_upoly *up;
2711 isl_qpolynomial *c;
2713 if (!qp)
2714 return NULL;
2716 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
2717 return NULL);
2719 g_pos = pos(qp->dim, type) + t_pos;
2720 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
2722 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
2723 if (!c)
2724 return NULL;
2725 isl_mat_free(c->div);
2726 c->div = isl_mat_copy(qp->div);
2727 if (!c->div)
2728 goto error;
2729 return c;
2730 error:
2731 isl_qpolynomial_free(c);
2732 return NULL;
2735 /* Homogenize the polynomial in the variables first (inclusive) up to
2736 * last (exclusive) by inserting powers of variable first.
2737 * Variable first is assumed not to appear in the input.
2739 __isl_give struct isl_upoly *isl_upoly_homogenize(
2740 __isl_take struct isl_upoly *up, int deg, int target,
2741 int first, int last)
2743 int i;
2744 struct isl_upoly_rec *rec;
2746 if (!up)
2747 return NULL;
2748 if (isl_upoly_is_zero(up))
2749 return up;
2750 if (deg == target)
2751 return up;
2752 if (isl_upoly_is_cst(up) || up->var < first) {
2753 struct isl_upoly *hom;
2755 hom = isl_upoly_pow(up->ctx, first, target - deg);
2756 if (!hom)
2757 goto error;
2758 rec = isl_upoly_as_rec(hom);
2759 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
2761 return hom;
2764 up = isl_upoly_cow(up);
2765 rec = isl_upoly_as_rec(up);
2766 if (!rec)
2767 goto error;
2769 for (i = 0; i < rec->n; ++i) {
2770 if (isl_upoly_is_zero(rec->p[i]))
2771 continue;
2772 rec->p[i] = isl_upoly_homogenize(rec->p[i],
2773 up->var < last ? deg + i : i, target,
2774 first, last);
2775 if (!rec->p[i])
2776 goto error;
2779 return up;
2780 error:
2781 isl_upoly_free(up);
2782 return NULL;
2785 /* Homogenize the polynomial in the set variables by introducing
2786 * powers of an extra set variable at position 0.
2788 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
2789 __isl_take isl_qpolynomial *poly)
2791 unsigned ovar;
2792 unsigned nvar;
2793 int deg = isl_qpolynomial_degree(poly);
2795 if (deg < -1)
2796 goto error;
2798 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
2799 poly = isl_qpolynomial_cow(poly);
2800 if (!poly)
2801 goto error;
2803 ovar = isl_dim_offset(poly->dim, isl_dim_set);
2804 nvar = isl_dim_size(poly->dim, isl_dim_set);
2805 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
2806 ovar, ovar + nvar);
2807 if (!poly->upoly)
2808 goto error;
2810 return poly;
2811 error:
2812 isl_qpolynomial_free(poly);
2813 return NULL;
2816 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
2817 __isl_take isl_mat *div)
2819 isl_term *term;
2820 int n;
2822 if (!dim || !div)
2823 goto error;
2825 n = isl_dim_total(dim) + div->n_row;
2827 term = isl_calloc(dim->ctx, struct isl_term,
2828 sizeof(struct isl_term) + (n - 1) * sizeof(int));
2829 if (!term)
2830 goto error;
2832 term->ref = 1;
2833 term->dim = dim;
2834 term->div = div;
2835 isl_int_init(term->n);
2836 isl_int_init(term->d);
2838 return term;
2839 error:
2840 isl_dim_free(dim);
2841 isl_mat_free(div);
2842 return NULL;
2845 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
2847 if (!term)
2848 return NULL;
2850 term->ref++;
2851 return term;
2854 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
2856 int i;
2857 isl_term *dup;
2858 unsigned total;
2860 if (term)
2861 return NULL;
2863 total = isl_dim_total(term->dim) + term->div->n_row;
2865 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
2866 if (!dup)
2867 return NULL;
2869 isl_int_set(dup->n, term->n);
2870 isl_int_set(dup->d, term->d);
2872 for (i = 0; i < total; ++i)
2873 dup->pow[i] = term->pow[i];
2875 return dup;
2878 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
2880 if (!term)
2881 return NULL;
2883 if (term->ref == 1)
2884 return term;
2885 term->ref--;
2886 return isl_term_dup(term);
2889 void isl_term_free(__isl_take isl_term *term)
2891 if (!term)
2892 return;
2894 if (--term->ref > 0)
2895 return;
2897 isl_dim_free(term->dim);
2898 isl_mat_free(term->div);
2899 isl_int_clear(term->n);
2900 isl_int_clear(term->d);
2901 free(term);
2904 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
2906 if (!term)
2907 return 0;
2909 switch (type) {
2910 case isl_dim_param:
2911 case isl_dim_in:
2912 case isl_dim_out: return isl_dim_size(term->dim, type);
2913 case isl_dim_div: return term->div->n_row;
2914 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
2915 default: return 0;
2919 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
2921 return term ? term->dim->ctx : NULL;
2924 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
2926 if (!term)
2927 return;
2928 isl_int_set(*n, term->n);
2931 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
2933 if (!term)
2934 return;
2935 isl_int_set(*d, term->d);
2938 int isl_term_get_exp(__isl_keep isl_term *term,
2939 enum isl_dim_type type, unsigned pos)
2941 if (!term)
2942 return -1;
2944 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
2946 if (type >= isl_dim_set)
2947 pos += isl_dim_size(term->dim, isl_dim_param);
2948 if (type >= isl_dim_div)
2949 pos += isl_dim_size(term->dim, isl_dim_set);
2951 return term->pow[pos];
2954 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
2956 isl_basic_map *bmap;
2957 unsigned total;
2958 int k;
2960 if (!term)
2961 return NULL;
2963 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
2964 return NULL);
2966 total = term->div->n_col - term->div->n_row - 2;
2967 /* No nested divs for now */
2968 isl_assert(term->dim->ctx,
2969 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
2970 term->div->n_row) == -1,
2971 return NULL);
2973 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
2974 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
2975 goto error;
2977 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
2979 return isl_basic_map_div(bmap, k);
2980 error:
2981 isl_basic_map_free(bmap);
2982 return NULL;
2985 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
2986 int (*fn)(__isl_take isl_term *term, void *user),
2987 __isl_take isl_term *term, void *user)
2989 int i;
2990 struct isl_upoly_rec *rec;
2992 if (!up || !term)
2993 goto error;
2995 if (isl_upoly_is_zero(up))
2996 return term;
2998 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
2999 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3000 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3002 if (isl_upoly_is_cst(up)) {
3003 struct isl_upoly_cst *cst;
3004 cst = isl_upoly_as_cst(up);
3005 if (!cst)
3006 goto error;
3007 term = isl_term_cow(term);
3008 if (!term)
3009 goto error;
3010 isl_int_set(term->n, cst->n);
3011 isl_int_set(term->d, cst->d);
3012 if (fn(isl_term_copy(term), user) < 0)
3013 goto error;
3014 return term;
3017 rec = isl_upoly_as_rec(up);
3018 if (!rec)
3019 goto error;
3021 for (i = 0; i < rec->n; ++i) {
3022 term = isl_term_cow(term);
3023 if (!term)
3024 goto error;
3025 term->pow[up->var] = i;
3026 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3027 if (!term)
3028 goto error;
3030 term->pow[up->var] = 0;
3032 return term;
3033 error:
3034 isl_term_free(term);
3035 return NULL;
3038 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3039 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3041 isl_term *term;
3043 if (!qp)
3044 return -1;
3046 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3047 if (!term)
3048 return -1;
3050 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3052 isl_term_free(term);
3054 return term ? 0 : -1;
3057 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3059 struct isl_upoly *up;
3060 isl_qpolynomial *qp;
3061 int i, n;
3063 if (!term)
3064 return NULL;
3066 n = isl_dim_total(term->dim) + term->div->n_row;
3068 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3069 for (i = 0; i < n; ++i) {
3070 if (!term->pow[i])
3071 continue;
3072 up = isl_upoly_mul(up,
3073 isl_upoly_pow(term->dim->ctx, i, term->pow[i]));
3076 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3077 if (!qp)
3078 goto error;
3079 isl_mat_free(qp->div);
3080 qp->div = isl_mat_copy(term->div);
3081 if (!qp->div)
3082 goto error;
3084 isl_term_free(term);
3085 return qp;
3086 error:
3087 isl_qpolynomial_free(qp);
3088 isl_term_free(term);
3089 return NULL;
3092 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3093 __isl_take isl_dim *dim)
3095 int i;
3096 int extra;
3097 unsigned total;
3099 if (!qp || !dim)
3100 goto error;
3102 if (isl_dim_equal(qp->dim, dim)) {
3103 isl_dim_free(dim);
3104 return qp;
3107 qp = isl_qpolynomial_cow(qp);
3108 if (!qp)
3109 goto error;
3111 extra = isl_dim_size(dim, isl_dim_set) -
3112 isl_dim_size(qp->dim, isl_dim_set);
3113 total = isl_dim_total(qp->dim);
3114 if (qp->div->n_row) {
3115 int *exp;
3117 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3118 if (!exp)
3119 goto error;
3120 for (i = 0; i < qp->div->n_row; ++i)
3121 exp[i] = extra + i;
3122 qp->upoly = expand(qp->upoly, exp, total);
3123 free(exp);
3124 if (!qp->upoly)
3125 goto error;
3127 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3128 if (!qp->div)
3129 goto error;
3130 for (i = 0; i < qp->div->n_row; ++i)
3131 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3133 isl_dim_free(qp->dim);
3134 qp->dim = dim;
3136 return qp;
3137 error:
3138 isl_dim_free(dim);
3139 isl_qpolynomial_free(qp);
3140 return NULL;
3143 /* For each parameter or variable that does not appear in qp,
3144 * first eliminate the variable from all constraints and then set it to zero.
3146 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3147 __isl_keep isl_qpolynomial *qp)
3149 int *active = NULL;
3150 int i;
3151 int d;
3152 unsigned nparam;
3153 unsigned nvar;
3155 if (!set || !qp)
3156 goto error;
3158 d = isl_dim_total(set->dim);
3159 active = isl_calloc_array(set->ctx, int, d);
3160 if (set_active(qp, active) < 0)
3161 goto error;
3163 for (i = 0; i < d; ++i)
3164 if (!active[i])
3165 break;
3167 if (i == d) {
3168 free(active);
3169 return set;
3172 nparam = isl_dim_size(set->dim, isl_dim_param);
3173 nvar = isl_dim_size(set->dim, isl_dim_set);
3174 for (i = 0; i < nparam; ++i) {
3175 if (active[i])
3176 continue;
3177 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3178 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3180 for (i = 0; i < nvar; ++i) {
3181 if (active[nparam + i])
3182 continue;
3183 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3184 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3187 free(active);
3189 return set;
3190 error:
3191 free(active);
3192 isl_set_free(set);
3193 return NULL;
3196 struct isl_opt_data {
3197 isl_qpolynomial *qp;
3198 int first;
3199 isl_qpolynomial *opt;
3200 int max;
3203 static int opt_fn(__isl_take isl_point *pnt, void *user)
3205 struct isl_opt_data *data = (struct isl_opt_data *)user;
3206 isl_qpolynomial *val;
3208 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3209 if (data->first) {
3210 data->first = 0;
3211 data->opt = val;
3212 } else if (data->max) {
3213 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3214 } else {
3215 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3218 return 0;
3221 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3222 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3224 struct isl_opt_data data = { NULL, 1, NULL, max };
3226 if (!set || !qp)
3227 goto error;
3229 if (isl_upoly_is_cst(qp->upoly)) {
3230 isl_set_free(set);
3231 return qp;
3234 set = fix_inactive(set, qp);
3236 data.qp = qp;
3237 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3238 goto error;
3240 if (data.first)
3241 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3243 isl_set_free(set);
3244 isl_qpolynomial_free(qp);
3245 return data.opt;
3246 error:
3247 isl_set_free(set);
3248 isl_qpolynomial_free(qp);
3249 isl_qpolynomial_free(data.opt);
3250 return NULL;
3253 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3254 __isl_take isl_morph *morph)
3256 int i;
3257 isl_ctx *ctx;
3258 struct isl_upoly *up;
3259 unsigned n_div;
3260 struct isl_upoly **subs;
3261 isl_mat *mat;
3263 qp = isl_qpolynomial_cow(qp);
3264 if (!qp || !morph)
3265 goto error;
3267 ctx = qp->dim->ctx;
3268 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3270 subs = isl_calloc_array(ctx, struct isl_upoly *, morph->inv->n_row - 1);
3271 if (!subs)
3272 goto error;
3274 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3275 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3276 morph->inv->row[0][0], morph->inv->n_col);
3278 qp->upoly = isl_upoly_subs(qp->upoly, 0, morph->inv->n_row - 1, subs);
3280 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3281 isl_upoly_free(subs[i]);
3282 free(subs);
3284 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3285 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3286 qp->div = isl_mat_product(qp->div, mat);
3287 isl_dim_free(qp->dim);
3288 qp->dim = isl_dim_copy(morph->ran->dim);
3290 if (!qp->upoly || !qp->div || !qp->dim)
3291 goto error;
3293 isl_morph_free(morph);
3295 return qp;
3296 error:
3297 isl_qpolynomial_free(qp);
3298 isl_morph_free(morph);
3299 return NULL;
3302 static int neg_entry(void **entry, void *user)
3304 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3306 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3308 return *pwqp ? 0 : -1;
3311 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3312 __isl_take isl_union_pw_qpolynomial *upwqp)
3314 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3315 if (!upwqp)
3316 return NULL;
3318 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3319 &neg_entry, NULL) < 0)
3320 goto error;
3322 return upwqp;
3323 error:
3324 isl_union_pw_qpolynomial_free(upwqp);
3325 return NULL;
3328 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3329 __isl_take isl_union_pw_qpolynomial *upwqp1,
3330 __isl_take isl_union_pw_qpolynomial *upwqp2)
3332 return isl_union_pw_qpolynomial_add(upwqp1,
3333 isl_union_pw_qpolynomial_neg(upwqp2));
3336 static int mul_entry(void **entry, void *user)
3338 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3339 uint32_t hash;
3340 struct isl_hash_table_entry *entry2;
3341 isl_pw_qpolynomial *pwpq = *entry;
3342 int empty;
3344 hash = isl_dim_get_hash(pwpq->dim);
3345 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3346 hash, &has_dim, pwpq->dim, 0);
3347 if (!entry2)
3348 return 0;
3350 pwpq = isl_pw_qpolynomial_copy(pwpq);
3351 pwpq = isl_pw_qpolynomial_mul(pwpq,
3352 isl_pw_qpolynomial_copy(entry2->data));
3354 empty = isl_pw_qpolynomial_is_zero(pwpq);
3355 if (empty < 0) {
3356 isl_pw_qpolynomial_free(pwpq);
3357 return -1;
3359 if (empty) {
3360 isl_pw_qpolynomial_free(pwpq);
3361 return 0;
3364 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3366 return 0;
3369 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3370 __isl_take isl_union_pw_qpolynomial *upwqp1,
3371 __isl_take isl_union_pw_qpolynomial *upwqp2)
3373 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3376 /* Reorder the columns of the given div definitions according to the
3377 * given reordering.
3379 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3380 __isl_take isl_reordering *r)
3382 int i, j;
3383 isl_mat *mat;
3384 int extra;
3386 if (!div || !r)
3387 goto error;
3389 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3390 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3391 if (!mat)
3392 goto error;
3394 for (i = 0; i < div->n_row; ++i) {
3395 isl_seq_cpy(mat->row[i], div->row[i], 2);
3396 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3397 for (j = 0; j < r->len; ++j)
3398 isl_int_set(mat->row[i][2 + r->pos[j]],
3399 div->row[i][2 + j]);
3402 isl_reordering_free(r);
3403 isl_mat_free(div);
3404 return mat;
3405 error:
3406 isl_reordering_free(r);
3407 isl_mat_free(div);
3408 return NULL;
3411 /* Reorder the dimension of "qp" according to the given reordering.
3413 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3414 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3416 qp = isl_qpolynomial_cow(qp);
3417 if (!qp)
3418 goto error;
3420 r = isl_reordering_extend(r, qp->div->n_row);
3421 if (!r)
3422 goto error;
3424 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3425 if (!qp->div)
3426 goto error;
3428 qp->upoly = reorder(qp->upoly, r->pos);
3429 if (!qp->upoly)
3430 goto error;
3432 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3434 isl_reordering_free(r);
3435 return qp;
3436 error:
3437 isl_qpolynomial_free(qp);
3438 isl_reordering_free(r);
3439 return NULL;
3442 struct isl_split_periods_data {
3443 int max_periods;
3444 isl_pw_qpolynomial *res;
3447 /* Create a slice where the integer division "div" has the fixed value "v".
3448 * In particular, if "div" refers to floor(f/m), then create a slice
3450 * m v <= f <= m v + (m - 1)
3452 * or
3454 * f - m v >= 0
3455 * -f + m v + (m - 1) >= 0
3457 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3458 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3460 int total;
3461 isl_basic_set *bset = NULL;
3462 int k;
3464 if (!dim || !qp)
3465 goto error;
3467 total = isl_dim_total(dim);
3468 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3470 k = isl_basic_set_alloc_inequality(bset);
3471 if (k < 0)
3472 goto error;
3473 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3474 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3476 k = isl_basic_set_alloc_inequality(bset);
3477 if (k < 0)
3478 goto error;
3479 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3480 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3481 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3482 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3484 isl_dim_free(dim);
3485 return isl_set_from_basic_set(bset);
3486 error:
3487 isl_basic_set_free(bset);
3488 isl_dim_free(dim);
3489 return NULL;
3492 static int split_periods(__isl_take isl_set *set,
3493 __isl_take isl_qpolynomial *qp, void *user);
3495 /* Create a slice of the domain "set" such that integer division "div"
3496 * has the fixed value "v" and add the results to data->res,
3497 * replacing the integer division by "v" in "qp".
3499 static int set_div(__isl_take isl_set *set,
3500 __isl_take isl_qpolynomial *qp, int div, isl_int v,
3501 struct isl_split_periods_data *data)
3503 int i;
3504 int *reordering;
3505 isl_set *slice;
3506 struct isl_upoly *cst;
3507 int total;
3509 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3510 set = isl_set_intersect(set, slice);
3512 qp = isl_qpolynomial_cow(qp);
3513 if (!qp)
3514 goto error;
3516 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3517 if (!cst)
3518 goto error;
3519 total = isl_dim_total(qp->dim);
3520 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &cst);
3521 isl_upoly_free(cst);
3522 if (!qp->upoly)
3523 goto error;
3525 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
3526 if (!reordering)
3527 goto error;
3528 for (i = 0; i < total + div; ++i)
3529 reordering[i] = i;
3530 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
3531 reordering[i] = i - 1;
3532 qp->div = isl_mat_drop_rows(qp->div, div, 1);
3533 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
3534 qp->upoly = reorder(qp->upoly, reordering);
3535 free(reordering);
3537 if (!qp->upoly || !qp->div)
3538 goto error;
3540 return split_periods(set, qp, data);
3541 error:
3542 isl_set_free(set);
3543 isl_qpolynomial_free(qp);
3544 return -1;
3547 /* Split the domain "set" such that integer division "div"
3548 * has a fixed value (ranging from "min" to "max") on each slice
3549 * and add the results to data->res.
3551 static int split_div(__isl_take isl_set *set,
3552 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3553 struct isl_split_periods_data *data)
3555 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3556 isl_set *set_i = isl_set_copy(set);
3557 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3559 if (set_div(set_i, qp_i, div, min, data) < 0)
3560 goto error;
3562 isl_set_free(set);
3563 isl_qpolynomial_free(qp);
3564 return 0;
3565 error:
3566 isl_set_free(set);
3567 isl_qpolynomial_free(qp);
3568 return -1;
3571 /* If "qp" refers to any integer division
3572 * that can only attain "max_periods" distinct values on "set"
3573 * then split the domain along those distinct values.
3574 * Add the results (or the original if no splitting occurs)
3575 * to data->res.
3577 static int split_periods(__isl_take isl_set *set,
3578 __isl_take isl_qpolynomial *qp, void *user)
3580 int i;
3581 isl_pw_qpolynomial *pwqp;
3582 struct isl_split_periods_data *data;
3583 isl_int min, max;
3584 int total;
3585 int r = 0;
3587 data = (struct isl_split_periods_data *)user;
3589 if (!set || !qp)
3590 goto error;
3592 if (qp->div->n_row == 0) {
3593 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3594 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3595 return 0;
3598 isl_int_init(min);
3599 isl_int_init(max);
3600 total = isl_dim_total(qp->dim);
3601 for (i = 0; i < qp->div->n_row; ++i) {
3602 enum isl_lp_result lp_res;
3604 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
3605 qp->div->n_row) != -1)
3606 continue;
3608 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
3609 set->ctx->one, &min, NULL, NULL);
3610 if (lp_res == isl_lp_error)
3611 goto error2;
3612 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3613 continue;
3614 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
3616 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
3617 set->ctx->one, &max, NULL, NULL);
3618 if (lp_res == isl_lp_error)
3619 goto error2;
3620 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
3621 continue;
3622 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
3624 isl_int_sub(max, max, min);
3625 if (isl_int_cmp_si(max, data->max_periods) < 0) {
3626 isl_int_add(max, max, min);
3627 break;
3631 if (i < qp->div->n_row) {
3632 r = split_div(set, qp, i, min, max, data);
3633 } else {
3634 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3635 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
3638 isl_int_clear(max);
3639 isl_int_clear(min);
3641 return r;
3642 error2:
3643 isl_int_clear(max);
3644 isl_int_clear(min);
3645 error:
3646 isl_set_free(set);
3647 isl_qpolynomial_free(qp);
3648 return -1;
3651 /* If any quasi-polynomial in pwqp refers to any integer division
3652 * that can only attain "max_periods" distinct values on its domain
3653 * then split the domain along those distinct values.
3655 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
3656 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
3658 struct isl_split_periods_data data;
3660 data.max_periods = max_periods;
3661 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
3663 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
3664 goto error;
3666 isl_pw_qpolynomial_free(pwqp);
3668 return data.res;
3669 error:
3670 isl_pw_qpolynomial_free(data.res);
3671 isl_pw_qpolynomial_free(pwqp);
3672 return NULL;
3675 /* Construct a piecewise quasipolynomial that is constant on the given
3676 * domain. In particular, it is
3677 * 0 if cst == 0
3678 * 1 if cst == 1
3679 * infinity if cst == -1
3681 static __isl_give isl_pw_qpolynomial *constant_on_domain(
3682 __isl_take isl_basic_set *bset, int cst)
3684 isl_dim *dim;
3685 isl_qpolynomial *qp;
3687 if (!bset)
3688 return NULL;
3690 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
3691 dim = isl_basic_set_get_dim(bset);
3692 if (cst < 0)
3693 qp = isl_qpolynomial_infty(dim);
3694 else if (cst == 0)
3695 qp = isl_qpolynomial_zero(dim);
3696 else
3697 qp = isl_qpolynomial_one(dim);
3698 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
3701 /* Factor bset, call fn on each of the factors and return the product.
3703 * If no factors can be found, simply call fn on the input.
3704 * Otherwise, construct the factors based on the factorizer,
3705 * call fn on each factor and compute the product.
3707 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
3708 __isl_take isl_basic_set *bset,
3709 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
3711 int i, n;
3712 isl_dim *dim;
3713 isl_set *set;
3714 isl_factorizer *f;
3715 isl_qpolynomial *qp;
3716 isl_pw_qpolynomial *pwqp;
3717 unsigned nparam;
3718 unsigned nvar;
3720 f = isl_basic_set_factorizer(bset);
3721 if (!f)
3722 goto error;
3723 if (f->n_group == 0) {
3724 isl_factorizer_free(f);
3725 return fn(bset);
3728 nparam = isl_basic_set_dim(bset, isl_dim_param);
3729 nvar = isl_basic_set_dim(bset, isl_dim_set);
3731 dim = isl_basic_set_get_dim(bset);
3732 dim = isl_dim_domain(dim);
3733 set = isl_set_universe(isl_dim_copy(dim));
3734 qp = isl_qpolynomial_one(dim);
3735 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3737 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
3739 for (i = 0, n = 0; i < f->n_group; ++i) {
3740 isl_basic_set *bset_i;
3741 isl_pw_qpolynomial *pwqp_i;
3743 bset_i = isl_basic_set_copy(bset);
3744 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
3745 nparam + n + f->len[i], nvar - n - f->len[i]);
3746 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
3747 nparam, n);
3748 bset_i = isl_basic_set_drop_dims(bset_i,
3749 nparam + n + f->len[i], nvar - n - f->len[i]);
3750 bset_i = isl_basic_set_drop_dims(bset_i, nparam, n);
3752 pwqp_i = fn(bset_i);
3753 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
3755 n += f->len[i];
3758 isl_basic_set_free(bset);
3759 isl_factorizer_free(f);
3761 return pwqp;
3762 error:
3763 isl_basic_set_free(bset);
3764 return NULL;
3767 /* Factor bset, call fn on each of the factors and return the product.
3768 * The function is assumed to evaluate to zero on empty domains,
3769 * to one on zero-dimensional domains and to infinity on unbounded domains
3770 * and will not be called explicitly on zero-dimensional or unbounded domains.
3772 * We first check for some special cases and remove all equalities.
3773 * Then we hand over control to compressed_multiplicative_call.
3775 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
3776 __isl_take isl_basic_set *bset,
3777 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
3779 int bounded;
3780 isl_morph *morph;
3781 isl_pw_qpolynomial *pwqp;
3782 unsigned orig_nvar, final_nvar;
3784 if (!bset)
3785 return NULL;
3787 if (isl_basic_set_fast_is_empty(bset))
3788 return constant_on_domain(bset, 0);
3790 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
3792 if (orig_nvar == 0)
3793 return constant_on_domain(bset, 1);
3795 bounded = isl_basic_set_is_bounded(bset);
3796 if (bounded < 0)
3797 goto error;
3798 if (!bounded)
3799 return constant_on_domain(bset, -1);
3801 if (bset->n_eq == 0)
3802 return compressed_multiplicative_call(bset, fn);
3804 morph = isl_basic_set_full_compression(bset);
3805 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
3807 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
3809 pwqp = compressed_multiplicative_call(bset, fn);
3811 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
3812 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
3813 morph = isl_morph_inverse(morph);
3815 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
3817 return pwqp;
3818 error:
3819 isl_basic_set_free(bset);
3820 return NULL;