isl_map_transitive_closure: Floyd-Warshall: properly keep track of current group
[isl.git] / isl_polynomial.c
blobd5f5972069b3bd9860b7240bc5e13986672fd05a
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_seq.h>
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
15 #include <isl_dim.h>
16 #include <isl_map_private.h>
18 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
20 switch (type) {
21 case isl_dim_param: return 0;
22 case isl_dim_in: return dim->nparam;
23 case isl_dim_out: return dim->nparam + dim->n_in;
27 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
29 if (!up)
30 return -1;
32 return up->var < 0;
35 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
37 if (!up)
38 return NULL;
40 isl_assert(up->ctx, up->var < 0, return NULL);
42 return (struct isl_upoly_cst *)up;
45 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
47 if (!up)
48 return NULL;
50 isl_assert(up->ctx, up->var >= 0, return NULL);
52 return (struct isl_upoly_rec *)up;
55 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
56 __isl_keep struct isl_upoly *up2)
58 int i;
59 struct isl_upoly_rec *rec1, *rec2;
61 if (!up1 || !up2)
62 return -1;
63 if (up1 == up2)
64 return 1;
65 if (up1->var != up2->var)
66 return 0;
67 if (isl_upoly_is_cst(up1)) {
68 struct isl_upoly_cst *cst1, *cst2;
69 cst1 = isl_upoly_as_cst(up1);
70 cst2 = isl_upoly_as_cst(up2);
71 if (!cst1 || !cst2)
72 return -1;
73 return isl_int_eq(cst1->n, cst2->n) &&
74 isl_int_eq(cst1->d, cst2->d);
77 rec1 = isl_upoly_as_rec(up1);
78 rec2 = isl_upoly_as_rec(up2);
79 if (!rec1 || !rec2)
80 return -1;
82 if (rec1->n != rec2->n)
83 return 0;
85 for (i = 0; i < rec1->n; ++i) {
86 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
87 if (eq < 0 || !eq)
88 return eq;
91 return 1;
94 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
96 struct isl_upoly_cst *cst;
98 if (!up)
99 return -1;
100 if (!isl_upoly_is_cst(up))
101 return 0;
103 cst = isl_upoly_as_cst(up);
104 if (!cst)
105 return -1;
107 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
110 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
112 struct isl_upoly_cst *cst;
114 if (!up)
115 return 0;
116 if (!isl_upoly_is_cst(up))
117 return 0;
119 cst = isl_upoly_as_cst(up);
120 if (!cst)
121 return 0;
123 return isl_int_sgn(cst->n);
126 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
128 struct isl_upoly_cst *cst;
130 if (!up)
131 return -1;
132 if (!isl_upoly_is_cst(up))
133 return 0;
135 cst = isl_upoly_as_cst(up);
136 if (!cst)
137 return -1;
139 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
142 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
144 struct isl_upoly_cst *cst;
146 if (!up)
147 return -1;
148 if (!isl_upoly_is_cst(up))
149 return 0;
151 cst = isl_upoly_as_cst(up);
152 if (!cst)
153 return -1;
155 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
158 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
160 struct isl_upoly_cst *cst;
162 if (!up)
163 return -1;
164 if (!isl_upoly_is_cst(up))
165 return 0;
167 cst = isl_upoly_as_cst(up);
168 if (!cst)
169 return -1;
171 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
174 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
176 struct isl_upoly_cst *cst;
178 if (!up)
179 return -1;
180 if (!isl_upoly_is_cst(up))
181 return 0;
183 cst = isl_upoly_as_cst(up);
184 if (!cst)
185 return -1;
187 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
190 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
192 struct isl_upoly_cst *cst;
194 if (!up)
195 return -1;
196 if (!isl_upoly_is_cst(up))
197 return 0;
199 cst = isl_upoly_as_cst(up);
200 if (!cst)
201 return -1;
203 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
206 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
208 struct isl_upoly_cst *cst;
210 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
211 if (!cst)
212 return NULL;
214 cst->up.ref = 1;
215 cst->up.ctx = ctx;
216 isl_ctx_ref(ctx);
217 cst->up.var = -1;
219 isl_int_init(cst->n);
220 isl_int_init(cst->d);
222 return cst;
225 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
227 struct isl_upoly_cst *cst;
229 cst = isl_upoly_cst_alloc(ctx);
230 if (!cst)
231 return NULL;
233 isl_int_set_si(cst->n, 0);
234 isl_int_set_si(cst->d, 1);
236 return &cst->up;
239 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
241 struct isl_upoly_cst *cst;
243 cst = isl_upoly_cst_alloc(ctx);
244 if (!cst)
245 return NULL;
247 isl_int_set_si(cst->n, 1);
248 isl_int_set_si(cst->d, 0);
250 return &cst->up;
253 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
255 struct isl_upoly_cst *cst;
257 cst = isl_upoly_cst_alloc(ctx);
258 if (!cst)
259 return NULL;
261 isl_int_set_si(cst->n, 0);
262 isl_int_set_si(cst->d, 0);
264 return &cst->up;
267 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
268 isl_int n, isl_int d)
270 struct isl_upoly_cst *cst;
272 cst = isl_upoly_cst_alloc(ctx);
273 if (!cst)
274 return NULL;
276 isl_int_set(cst->n, n);
277 isl_int_set(cst->d, d);
279 return &cst->up;
282 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
283 int var, int size)
285 struct isl_upoly_rec *rec;
287 isl_assert(ctx, var >= 0, return NULL);
288 isl_assert(ctx, size >= 0, return NULL);
289 rec = isl_calloc(dim->ctx, struct isl_upoly_rec,
290 sizeof(struct isl_upoly_rec) +
291 (size - 1) * sizeof(struct isl_upoly *));
292 if (!rec)
293 return NULL;
295 rec->up.ref = 1;
296 rec->up.ctx = ctx;
297 isl_ctx_ref(ctx);
298 rec->up.var = var;
300 rec->n = 0;
301 rec->size = size;
303 return rec;
304 error:
305 isl_upoly_free(&rec->up);
306 return NULL;
309 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
311 return qp ? qp->dim->ctx : NULL;
314 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
316 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
319 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
321 return qp ? isl_upoly_is_one(qp->upoly) : -1;
324 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
326 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
329 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
331 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
334 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
336 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
339 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
341 return qp ? isl_upoly_sgn(qp->upoly) : 0;
344 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
346 isl_int_clear(cst->n);
347 isl_int_clear(cst->d);
350 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
352 int i;
354 for (i = 0; i < rec->n; ++i)
355 isl_upoly_free(rec->p[i]);
358 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
360 if (!up)
361 return NULL;
363 up->ref++;
364 return up;
367 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
369 struct isl_upoly_cst *cst;
370 struct isl_upoly_cst *dup;
372 cst = isl_upoly_as_cst(up);
373 if (!cst)
374 return NULL;
376 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
377 if (!dup)
378 return NULL;
379 isl_int_set(dup->n, cst->n);
380 isl_int_set(dup->d, cst->d);
382 return &dup->up;
385 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
387 int i;
388 struct isl_upoly_rec *rec;
389 struct isl_upoly_rec *dup;
391 rec = isl_upoly_as_rec(up);
392 if (!rec)
393 return NULL;
395 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
396 if (!dup)
397 return NULL;
399 for (i = 0; i < rec->n; ++i) {
400 dup->p[i] = isl_upoly_copy(rec->p[i]);
401 if (!dup->p[i])
402 goto error;
403 dup->n++;
406 return &dup->up;
407 error:
408 isl_upoly_free(&dup->up);
409 return NULL;
412 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
414 struct isl_upoly *dup;
416 if (!up)
417 return NULL;
419 if (isl_upoly_is_cst(up))
420 return isl_upoly_dup_cst(up);
421 else
422 return isl_upoly_dup_rec(up);
425 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
427 if (!up)
428 return NULL;
430 if (up->ref == 1)
431 return up;
432 up->ref--;
433 return isl_upoly_dup(up);
436 void isl_upoly_free(__isl_take struct isl_upoly *up)
438 if (!up)
439 return;
441 if (--up->ref > 0)
442 return;
444 if (up->var < 0)
445 upoly_free_cst((struct isl_upoly_cst *)up);
446 else
447 upoly_free_rec((struct isl_upoly_rec *)up);
449 isl_ctx_deref(up->ctx);
450 free(up);
453 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
455 isl_int gcd;
457 isl_int_init(gcd);
458 isl_int_gcd(gcd, cst->n, cst->d);
459 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
460 isl_int_divexact(cst->n, cst->n, gcd);
461 isl_int_divexact(cst->d, cst->d, gcd);
463 isl_int_clear(gcd);
466 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
467 __isl_take struct isl_upoly *up2)
469 struct isl_upoly_cst *cst1;
470 struct isl_upoly_cst *cst2;
472 up1 = isl_upoly_cow(up1);
473 if (!up1 || !up2)
474 goto error;
476 cst1 = isl_upoly_as_cst(up1);
477 cst2 = isl_upoly_as_cst(up2);
479 if (isl_int_eq(cst1->d, cst2->d))
480 isl_int_add(cst1->n, cst1->n, cst2->n);
481 else {
482 isl_int_mul(cst1->n, cst1->n, cst2->d);
483 isl_int_addmul(cst1->n, cst2->n, cst1->d);
484 isl_int_mul(cst1->d, cst1->d, cst2->d);
487 isl_upoly_cst_reduce(cst1);
489 isl_upoly_free(up2);
490 return up1;
491 error:
492 isl_upoly_free(up1);
493 isl_upoly_free(up2);
494 return NULL;
497 static __isl_give struct isl_upoly *replace_by_zero(
498 __isl_take struct isl_upoly *up)
500 struct isl_ctx *ctx;
502 if (!up)
503 return NULL;
504 ctx = up->ctx;
505 isl_upoly_free(up);
506 return isl_upoly_zero(ctx);
509 static __isl_give struct isl_upoly *replace_by_constant_term(
510 __isl_take struct isl_upoly *up)
512 struct isl_upoly_rec *rec;
513 struct isl_upoly *cst;
515 if (!up)
516 return NULL;
518 rec = isl_upoly_as_rec(up);
519 if (!rec)
520 goto error;
521 cst = isl_upoly_copy(rec->p[0]);
522 isl_upoly_free(up);
523 return cst;
524 error:
525 isl_upoly_free(up);
526 return NULL;
529 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
530 __isl_take struct isl_upoly *up2)
532 int i;
533 struct isl_upoly_rec *rec1, *rec2;
535 if (!up1 || !up2)
536 goto error;
538 if (isl_upoly_is_nan(up1)) {
539 isl_upoly_free(up2);
540 return up1;
543 if (isl_upoly_is_nan(up2)) {
544 isl_upoly_free(up1);
545 return up2;
548 if (isl_upoly_is_zero(up1)) {
549 isl_upoly_free(up1);
550 return up2;
553 if (isl_upoly_is_zero(up2)) {
554 isl_upoly_free(up2);
555 return up1;
558 if (up1->var < up2->var)
559 return isl_upoly_sum(up2, up1);
561 if (up2->var < up1->var) {
562 struct isl_upoly_rec *rec;
563 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
564 isl_upoly_free(up1);
565 return up2;
567 up1 = isl_upoly_cow(up1);
568 rec = isl_upoly_as_rec(up1);
569 if (!rec)
570 goto error;
571 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
572 if (rec->n == 1)
573 up1 = replace_by_constant_term(up1);
574 return up1;
577 if (isl_upoly_is_cst(up1))
578 return isl_upoly_sum_cst(up1, up2);
580 rec1 = isl_upoly_as_rec(up1);
581 rec2 = isl_upoly_as_rec(up2);
582 if (!rec1 || !rec2)
583 goto error;
585 if (rec1->n < rec2->n)
586 return isl_upoly_sum(up2, up1);
588 up1 = isl_upoly_cow(up1);
589 rec1 = isl_upoly_as_rec(up1);
590 if (!rec1)
591 goto error;
593 for (i = rec2->n - 1; i >= 0; --i) {
594 rec1->p[i] = isl_upoly_sum(rec1->p[i],
595 isl_upoly_copy(rec2->p[i]));
596 if (!rec1->p[i])
597 goto error;
598 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
599 isl_upoly_free(rec1->p[i]);
600 rec1->n--;
604 if (rec1->n == 0)
605 up1 = replace_by_zero(up1);
606 else if (rec1->n == 1)
607 up1 = replace_by_constant_term(up1);
609 isl_upoly_free(up2);
611 return up1;
612 error:
613 isl_upoly_free(up1);
614 isl_upoly_free(up2);
615 return NULL;
618 __isl_give struct isl_upoly *isl_upoly_neg_cst(__isl_take struct isl_upoly *up)
620 struct isl_upoly_cst *cst;
622 if (isl_upoly_is_zero(up))
623 return up;
625 up = isl_upoly_cow(up);
626 if (!up)
627 return NULL;
629 cst = isl_upoly_as_cst(up);
631 isl_int_neg(cst->n, cst->n);
633 return up;
636 __isl_give struct isl_upoly *isl_upoly_neg(__isl_take struct isl_upoly *up)
638 int i;
639 struct isl_upoly_rec *rec;
641 if (!up)
642 return NULL;
644 if (isl_upoly_is_cst(up))
645 return isl_upoly_neg_cst(up);
647 up = isl_upoly_cow(up);
648 rec = isl_upoly_as_rec(up);
649 if (!rec)
650 goto error;
652 for (i = 0; i < rec->n; ++i) {
653 rec->p[i] = isl_upoly_neg(rec->p[i]);
654 if (!rec->p[i])
655 goto error;
658 return up;
659 error:
660 isl_upoly_free(up);
661 return NULL;
664 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
665 __isl_take struct isl_upoly *up2)
667 struct isl_upoly_cst *cst1;
668 struct isl_upoly_cst *cst2;
670 up1 = isl_upoly_cow(up1);
671 if (!up1 || !up2)
672 goto error;
674 cst1 = isl_upoly_as_cst(up1);
675 cst2 = isl_upoly_as_cst(up2);
677 isl_int_mul(cst1->n, cst1->n, cst2->n);
678 isl_int_mul(cst1->d, cst1->d, cst2->d);
680 isl_upoly_cst_reduce(cst1);
682 isl_upoly_free(up2);
683 return up1;
684 error:
685 isl_upoly_free(up1);
686 isl_upoly_free(up2);
687 return NULL;
690 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
691 __isl_take struct isl_upoly *up2)
693 struct isl_upoly_rec *rec1;
694 struct isl_upoly_rec *rec2;
695 struct isl_upoly_rec *res;
696 int i, j;
697 int size;
699 rec1 = isl_upoly_as_rec(up1);
700 rec2 = isl_upoly_as_rec(up2);
701 if (!rec1 || !rec2)
702 goto error;
703 size = rec1->n + rec2->n - 1;
704 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
705 if (!res)
706 goto error;
708 for (i = 0; i < rec1->n; ++i) {
709 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
710 isl_upoly_copy(rec1->p[i]));
711 if (!res->p[i])
712 goto error;
713 res->n++;
715 for (; i < size; ++i) {
716 res->p[i] = isl_upoly_zero(up1->ctx);
717 if (!res->p[i])
718 goto error;
719 res->n++;
721 for (i = 0; i < rec1->n; ++i) {
722 for (j = 1; j < rec2->n; ++j) {
723 struct isl_upoly *up;
724 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
725 isl_upoly_copy(rec1->p[i]));
726 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
727 if (!res->p[i + j])
728 goto error;
732 isl_upoly_free(up1);
733 isl_upoly_free(up2);
735 return &res->up;
736 error:
737 isl_upoly_free(up1);
738 isl_upoly_free(up2);
739 isl_upoly_free(&res->up);
740 return NULL;
743 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
744 __isl_take struct isl_upoly *up2)
746 if (!up1 || !up2)
747 goto error;
749 if (isl_upoly_is_nan(up1)) {
750 isl_upoly_free(up2);
751 return up1;
754 if (isl_upoly_is_nan(up2)) {
755 isl_upoly_free(up1);
756 return up2;
759 if (isl_upoly_is_zero(up1)) {
760 isl_upoly_free(up2);
761 return up1;
764 if (isl_upoly_is_zero(up2)) {
765 isl_upoly_free(up1);
766 return up2;
769 if (isl_upoly_is_one(up1)) {
770 isl_upoly_free(up1);
771 return up2;
774 if (isl_upoly_is_one(up2)) {
775 isl_upoly_free(up2);
776 return up1;
779 if (up1->var < up2->var)
780 return isl_upoly_mul(up2, up1);
782 if (up2->var < up1->var) {
783 int i;
784 struct isl_upoly_rec *rec;
785 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
786 isl_ctx *ctx = up1->ctx;
787 isl_upoly_free(up1);
788 isl_upoly_free(up2);
789 return isl_upoly_nan(ctx);
791 up1 = isl_upoly_cow(up1);
792 rec = isl_upoly_as_rec(up1);
793 if (!rec)
794 goto error;
796 for (i = 0; i < rec->n; ++i) {
797 rec->p[i] = isl_upoly_mul(rec->p[i],
798 isl_upoly_copy(up2));
799 if (!rec->p[i])
800 goto error;
802 isl_upoly_free(up2);
803 return up1;
806 if (isl_upoly_is_cst(up1))
807 return isl_upoly_mul_cst(up1, up2);
809 return isl_upoly_mul_rec(up1, up2);
810 error:
811 isl_upoly_free(up1);
812 isl_upoly_free(up2);
813 return NULL;
816 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
817 unsigned n_div, __isl_take struct isl_upoly *up)
819 struct isl_qpolynomial *qp = NULL;
820 unsigned total;
822 if (!dim || !up)
823 goto error;
825 total = isl_dim_total(dim);
827 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
828 if (!qp)
829 goto error;
831 qp->ref = 1;
832 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
833 if (!qp->div)
834 goto error;
836 qp->dim = dim;
837 qp->upoly = up;
839 return qp;
840 error:
841 isl_dim_free(dim);
842 isl_upoly_free(up);
843 isl_qpolynomial_free(qp);
844 return NULL;
847 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
849 if (!qp)
850 return NULL;
852 qp->ref++;
853 return qp;
856 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
858 struct isl_qpolynomial *dup;
860 if (!qp)
861 return NULL;
863 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
864 isl_upoly_copy(qp->upoly));
865 if (!dup)
866 return NULL;
867 isl_mat_free(dup->div);
868 dup->div = isl_mat_copy(qp->div);
869 if (!dup->div)
870 goto error;
872 return dup;
873 error:
874 isl_qpolynomial_free(dup);
875 return NULL;
878 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
880 if (!qp)
881 return NULL;
883 if (qp->ref == 1)
884 return qp;
885 qp->ref--;
886 return isl_qpolynomial_dup(qp);
889 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
891 if (!qp)
892 return;
894 if (--qp->ref > 0)
895 return;
897 isl_dim_free(qp->dim);
898 isl_mat_free(qp->div);
899 isl_upoly_free(qp->upoly);
901 free(qp);
904 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
906 int n_row, n_col;
907 int equal;
909 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
910 div1->n_col >= div2->n_col, return -1);
912 if (div1->n_row == div2->n_row)
913 return isl_mat_is_equal(div1, div2);
915 n_row = div1->n_row;
916 n_col = div1->n_col;
917 div1->n_row = div2->n_row;
918 div1->n_col = div2->n_col;
920 equal = isl_mat_is_equal(div1, div2);
922 div1->n_row = n_row;
923 div1->n_col = n_col;
925 return equal;
928 static void expand_row(__isl_keep isl_mat *dst, int d,
929 __isl_keep isl_mat *src, int s, int *exp)
931 int i;
932 unsigned c = src->n_col - src->n_row;
934 isl_seq_cpy(dst->row[d], src->row[s], c);
935 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
937 for (i = 0; i < s; ++i)
938 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
941 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
943 int li, lj;
945 li = isl_seq_last_non_zero(div->row[i], div->n_col);
946 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
948 if (li != lj)
949 return li - lj;
951 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
954 struct isl_div_sort_info {
955 isl_mat *div;
956 int row;
959 static int div_sort_cmp(const void *p1, const void *p2)
961 const struct isl_div_sort_info *i1, *i2;
962 i1 = (const struct isl_div_sort_info *) p1;
963 i2 = (const struct isl_div_sort_info *) p2;
965 return cmp_row(i1->div, i1->row, i2->row);
968 static __isl_give isl_mat *sort_divs(__isl_take isl_mat *div)
970 int i;
971 struct isl_div_sort_info *array = NULL;
972 int *pos = NULL;
974 if (!div)
975 return NULL;
976 if (div->n_row <= 1)
977 return div;
979 array = isl_alloc_array(div->ctx, struct isl_div_sort_info, div->n_row);
980 pos = isl_alloc_array(div->ctx, int, div->n_row);
981 if (!array || !pos)
982 goto error;
984 for (i = 0; i < div->n_row; ++i) {
985 array[i].div = div;
986 array[i].row = i;
987 pos[i] = i;
990 qsort(array, div->n_row, sizeof(struct isl_div_sort_info),
991 div_sort_cmp);
993 for (i = 0; i < div->n_row; ++i) {
994 int t;
995 if (pos[array[i].row] == i)
996 continue;
997 div = isl_mat_cow(div);
998 div = isl_mat_swap_rows(div, i, pos[array[i].row]);
999 t = pos[array[i].row];
1000 pos[array[i].row] = pos[i];
1001 pos[i] = t;
1004 free(array);
1006 return div;
1007 error:
1008 free(pos);
1009 free(array);
1010 isl_mat_free(div);
1011 return NULL;
1014 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1015 __isl_keep isl_mat *div2, int *exp1, int *exp2)
1017 int i, j, k;
1018 isl_mat *div = NULL;
1019 unsigned d = div1->n_col - div1->n_row;
1021 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1022 d + div1->n_row + div2->n_row);
1023 if (!div)
1024 return NULL;
1026 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1027 int cmp;
1029 expand_row(div, k, div1, i, exp1);
1030 expand_row(div, k + 1, div2, j, exp2);
1032 cmp = cmp_row(div, k, k + 1);
1033 if (cmp == 0) {
1034 exp1[i++] = k;
1035 exp2[j++] = k;
1036 } else if (cmp < 0) {
1037 exp1[i++] = k;
1038 } else {
1039 exp2[j++] = k;
1040 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1043 for (; i < div1->n_row; ++i, ++k) {
1044 expand_row(div, k, div1, i, exp1);
1045 exp1[i] = k;
1047 for (; j < div2->n_row; ++j, ++k) {
1048 expand_row(div, k, div2, j, exp2);
1049 exp2[j] = k;
1052 div->n_row = k;
1053 div->n_col = d + k;
1055 return div;
1058 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1059 int *exp, int first)
1061 int i;
1062 struct isl_upoly_rec *rec;
1064 if (isl_upoly_is_cst(up))
1065 return up;
1067 if (up->var < first)
1068 return up;
1070 if (exp[up->var - first] == up->var - first)
1071 return up;
1073 up = isl_upoly_cow(up);
1074 if (!up)
1075 goto error;
1077 up->var = exp[up->var - first] + first;
1079 rec = isl_upoly_as_rec(up);
1080 if (!rec)
1081 goto error;
1083 for (i = 0; i < rec->n; ++i) {
1084 rec->p[i] = expand(rec->p[i], exp, first);
1085 if (!rec->p[i])
1086 goto error;
1089 return up;
1090 error:
1091 isl_upoly_free(up);
1092 return NULL;
1095 static __isl_give isl_qpolynomial *with_merged_divs(
1096 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1097 __isl_take isl_qpolynomial *qp2),
1098 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1100 int *exp1 = NULL;
1101 int *exp2 = NULL;
1102 isl_mat *div = NULL;
1104 qp1 = isl_qpolynomial_cow(qp1);
1105 qp2 = isl_qpolynomial_cow(qp2);
1107 if (!qp1 || !qp2)
1108 goto error;
1110 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1111 qp1->div->n_col >= qp2->div->n_col, goto error);
1113 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1114 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1115 if (!exp1 || !exp2)
1116 goto error;
1118 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1119 if (!div)
1120 goto error;
1122 isl_mat_free(qp1->div);
1123 qp1->div = isl_mat_copy(div);
1124 isl_mat_free(qp2->div);
1125 qp2->div = isl_mat_copy(div);
1127 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1128 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1130 if (!qp1->upoly || !qp2->upoly)
1131 goto error;
1133 isl_mat_free(div);
1134 free(exp1);
1135 free(exp2);
1137 return fn(qp1, qp2);
1138 error:
1139 isl_mat_free(div);
1140 free(exp1);
1141 free(exp2);
1142 isl_qpolynomial_free(qp1);
1143 isl_qpolynomial_free(qp2);
1144 return NULL;
1147 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1148 __isl_take isl_qpolynomial *qp2)
1150 qp1 = isl_qpolynomial_cow(qp1);
1152 if (!qp1 || !qp2)
1153 goto error;
1155 if (qp1->div->n_row < qp2->div->n_row)
1156 return isl_qpolynomial_add(qp2, qp1);
1158 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1159 if (!compatible_divs(qp1->div, qp2->div))
1160 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1162 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1163 if (!qp1->upoly)
1164 goto error;
1166 isl_qpolynomial_free(qp2);
1168 return qp1;
1169 error:
1170 isl_qpolynomial_free(qp1);
1171 isl_qpolynomial_free(qp2);
1172 return NULL;
1175 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1176 __isl_take isl_qpolynomial *qp2)
1178 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1181 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1183 qp = isl_qpolynomial_cow(qp);
1185 if (!qp)
1186 return NULL;
1188 qp->upoly = isl_upoly_neg(qp->upoly);
1189 if (!qp->upoly)
1190 goto error;
1192 return qp;
1193 error:
1194 isl_qpolynomial_free(qp);
1195 return NULL;
1198 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1199 __isl_take isl_qpolynomial *qp2)
1201 qp1 = isl_qpolynomial_cow(qp1);
1203 if (!qp1 || !qp2)
1204 goto error;
1206 if (qp1->div->n_row < qp2->div->n_row)
1207 return isl_qpolynomial_mul(qp2, qp1);
1209 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1210 if (!compatible_divs(qp1->div, qp2->div))
1211 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1213 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1214 if (!qp1->upoly)
1215 goto error;
1217 isl_qpolynomial_free(qp2);
1219 return qp1;
1220 error:
1221 isl_qpolynomial_free(qp1);
1222 isl_qpolynomial_free(qp2);
1223 return NULL;
1226 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1228 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1231 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1233 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1236 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1238 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1241 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1242 isl_int v)
1244 struct isl_qpolynomial *qp;
1245 struct isl_upoly_cst *cst;
1247 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1248 if (!qp)
1249 return NULL;
1251 cst = isl_upoly_as_cst(qp->upoly);
1252 isl_int_set(cst->n, v);
1254 return qp;
1255 error:
1256 isl_qpolynomial_free(qp);
1257 return NULL;
1260 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1261 isl_int *n, isl_int *d)
1263 struct isl_upoly_cst *cst;
1265 if (!qp)
1266 return -1;
1268 if (!isl_upoly_is_cst(qp->upoly))
1269 return 0;
1271 cst = isl_upoly_as_cst(qp->upoly);
1272 if (!cst)
1273 return -1;
1275 if (n)
1276 isl_int_set(*n, cst->n);
1277 if (d)
1278 isl_int_set(*d, cst->d);
1280 return 1;
1283 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1285 int is_cst;
1286 struct isl_upoly_rec *rec;
1288 if (!up)
1289 return -1;
1291 if (up->var < 0)
1292 return 1;
1294 rec = isl_upoly_as_rec(up);
1295 if (!rec)
1296 return -1;
1298 if (rec->n > 2)
1299 return 0;
1301 isl_assert(up->ctx, rec->n > 1, return -1);
1303 is_cst = isl_upoly_is_cst(rec->p[1]);
1304 if (is_cst < 0)
1305 return -1;
1306 if (!is_cst)
1307 return 0;
1309 return isl_upoly_is_affine(rec->p[0]);
1312 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1314 if (!qp)
1315 return -1;
1317 if (qp->div->n_row > 0)
1318 return 0;
1320 return isl_upoly_is_affine(qp->upoly);
1323 static void update_coeff(__isl_keep isl_vec *aff,
1324 __isl_keep struct isl_upoly_cst *cst, int pos)
1326 isl_int gcd;
1327 isl_int f;
1329 if (isl_int_is_zero(cst->n))
1330 return;
1332 isl_int_init(gcd);
1333 isl_int_init(f);
1334 isl_int_gcd(gcd, cst->d, aff->el[0]);
1335 isl_int_divexact(f, cst->d, gcd);
1336 isl_int_divexact(gcd, aff->el[0], gcd);
1337 isl_seq_scale(aff->el, aff->el, f, aff->size);
1338 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1339 isl_int_clear(gcd);
1340 isl_int_clear(f);
1343 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1344 __isl_keep isl_vec *aff)
1346 struct isl_upoly_cst *cst;
1347 struct isl_upoly_rec *rec;
1349 if (!up || !aff)
1350 return -1;
1352 if (up->var < 0) {
1353 struct isl_upoly_cst *cst;
1355 cst = isl_upoly_as_cst(up);
1356 if (!cst)
1357 return -1;
1358 update_coeff(aff, cst, 0);
1359 return 0;
1362 rec = isl_upoly_as_rec(up);
1363 if (!rec)
1364 return -1;
1365 isl_assert(up->ctx, rec->n == 2, return -1);
1367 cst = isl_upoly_as_cst(rec->p[1]);
1368 if (!cst)
1369 return -1;
1370 update_coeff(aff, cst, 1 + up->var);
1372 return isl_upoly_update_affine(rec->p[0], aff);
1375 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1376 __isl_keep isl_qpolynomial *qp)
1378 isl_vec *aff;
1379 unsigned d;
1381 if (!qp)
1382 return NULL;
1384 isl_assert(qp->div->ctx, qp->div->n_row == 0, return NULL);
1385 d = isl_dim_total(qp->dim);
1386 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
1387 if (!aff)
1388 return NULL;
1390 isl_seq_clr(aff->el + 1, 1 + d);
1391 isl_int_set_si(aff->el[0], 1);
1393 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1394 goto error;
1396 return aff;
1397 error:
1398 isl_vec_free(aff);
1399 return NULL;
1402 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1403 __isl_keep isl_qpolynomial *qp2)
1405 if (!qp1 || !qp2)
1406 return -1;
1408 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1411 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1413 int i;
1414 struct isl_upoly_rec *rec;
1416 if (isl_upoly_is_cst(up)) {
1417 struct isl_upoly_cst *cst;
1418 cst = isl_upoly_as_cst(up);
1419 if (!cst)
1420 return;
1421 isl_int_lcm(*d, *d, cst->d);
1422 return;
1425 rec = isl_upoly_as_rec(up);
1426 if (!rec)
1427 return;
1429 for (i = 0; i < rec->n; ++i)
1430 upoly_update_den(rec->p[i], d);
1433 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1435 isl_int_set_si(*d, 1);
1436 if (!qp)
1437 return;
1438 upoly_update_den(qp->upoly, d);
1441 __isl_give struct isl_upoly *isl_upoly_pow(isl_ctx *ctx, int pos, int power)
1443 int i;
1444 struct isl_upoly *up;
1445 struct isl_upoly_rec *rec;
1446 struct isl_upoly_cst *cst;
1448 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1449 if (!rec)
1450 return NULL;
1451 for (i = 0; i < 1 + power; ++i) {
1452 rec->p[i] = isl_upoly_zero(ctx);
1453 if (!rec->p[i])
1454 goto error;
1455 rec->n++;
1457 cst = isl_upoly_as_cst(rec->p[power]);
1458 isl_int_set_si(cst->n, 1);
1460 return &rec->up;
1461 error:
1462 isl_upoly_free(&rec->up);
1463 return NULL;
1466 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_dim *dim,
1467 int pos, int power)
1469 struct isl_ctx *ctx;
1471 if (!dim)
1472 return NULL;
1474 ctx = dim->ctx;
1476 return isl_qpolynomial_alloc(dim, 0, isl_upoly_pow(ctx, pos, power));
1479 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1480 enum isl_dim_type type, unsigned pos)
1482 if (!dim)
1483 return NULL;
1485 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1486 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1488 if (type == isl_dim_set)
1489 pos += isl_dim_size(dim, isl_dim_param);
1491 return isl_qpolynomial_pow(dim, pos, 1);
1492 error:
1493 isl_dim_free(dim);
1494 return NULL;
1497 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
1498 int power)
1500 struct isl_qpolynomial *qp = NULL;
1501 struct isl_upoly_rec *rec;
1502 struct isl_upoly_cst *cst;
1503 int i;
1504 int pos;
1506 if (!div)
1507 return NULL;
1508 isl_assert(div->ctx, div->bmap->n_div == 1, goto error);
1510 pos = isl_dim_total(div->bmap->dim);
1511 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
1512 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap), 1,
1513 &rec->up);
1514 if (!qp)
1515 goto error;
1517 isl_seq_cpy(qp->div->row[0], div->line[0], qp->div->n_col - 1);
1518 isl_int_set_si(qp->div->row[0][qp->div->n_col - 1], 0);
1520 for (i = 0; i < 1 + power; ++i) {
1521 rec->p[i] = isl_upoly_zero(div->ctx);
1522 if (!rec->p[i])
1523 goto error;
1524 rec->n++;
1526 cst = isl_upoly_as_cst(rec->p[power]);
1527 isl_int_set_si(cst->n, 1);
1529 isl_div_free(div);
1531 return qp;
1532 error:
1533 isl_qpolynomial_free(qp);
1534 isl_div_free(div);
1535 return NULL;
1538 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
1540 return isl_qpolynomial_div_pow(div, 1);
1543 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
1544 const isl_int n, const isl_int d)
1546 struct isl_qpolynomial *qp;
1547 struct isl_upoly_cst *cst;
1549 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1550 if (!qp)
1551 return NULL;
1553 cst = isl_upoly_as_cst(qp->upoly);
1554 isl_int_set(cst->n, n);
1555 isl_int_set(cst->d, d);
1557 return qp;
1558 error:
1559 isl_qpolynomial_free(qp);
1560 return NULL;
1563 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
1565 struct isl_upoly_rec *rec;
1566 int i;
1568 if (!up)
1569 return -1;
1571 if (isl_upoly_is_cst(up))
1572 return 0;
1574 if (up->var < d)
1575 active[up->var] = 1;
1577 rec = isl_upoly_as_rec(up);
1578 for (i = 0; i < rec->n; ++i)
1579 if (up_set_active(rec->p[i], active, d) < 0)
1580 return -1;
1582 return 0;
1585 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
1587 int i, j;
1588 int d = isl_dim_total(qp->dim);
1590 if (!qp || !active)
1591 return -1;
1593 for (i = 0; i < d; ++i)
1594 for (j = 0; j < qp->div->n_row; ++j) {
1595 if (isl_int_is_zero(qp->div->row[j][2 + i]))
1596 continue;
1597 active[i] = 1;
1598 break;
1601 return up_set_active(qp->upoly, active, d);
1604 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
1605 enum isl_dim_type type, unsigned first, unsigned n)
1607 int i;
1608 int *active = NULL;
1609 int involves = 0;
1611 if (!qp)
1612 return -1;
1613 if (n == 0)
1614 return 0;
1616 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1617 return -1);
1618 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1619 type == isl_dim_set, return -1);
1621 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
1622 if (set_active(qp, active) < 0)
1623 goto error;
1625 if (type == isl_dim_set)
1626 first += isl_dim_size(qp->dim, isl_dim_param);
1627 for (i = 0; i < n; ++i)
1628 if (active[first + i]) {
1629 involves = 1;
1630 break;
1633 free(active);
1635 return involves;
1636 error:
1637 free(active);
1638 return -1;
1641 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
1642 unsigned first, unsigned n)
1644 int i;
1645 struct isl_upoly_rec *rec;
1647 if (!up)
1648 return NULL;
1649 if (n == 0 || up->var < 0 || up->var < first)
1650 return up;
1651 if (up->var < first + n) {
1652 up = replace_by_constant_term(up);
1653 return isl_upoly_drop(up, first, n);
1655 up = isl_upoly_cow(up);
1656 if (!up)
1657 return NULL;
1658 up->var -= n;
1659 rec = isl_upoly_as_rec(up);
1660 if (!rec)
1661 goto error;
1663 for (i = 0; i < rec->n; ++i) {
1664 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
1665 if (!rec->p[i])
1666 goto error;
1669 return up;
1670 error:
1671 isl_upoly_free(up);
1672 return NULL;
1675 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
1676 __isl_take isl_qpolynomial *qp,
1677 enum isl_dim_type type, unsigned first, unsigned n)
1679 if (!qp)
1680 return NULL;
1681 if (n == 0)
1682 return qp;
1684 qp = isl_qpolynomial_cow(qp);
1685 if (!qp)
1686 return NULL;
1688 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
1689 goto error);
1690 isl_assert(qp->dim->ctx, type == isl_dim_param ||
1691 type == isl_dim_set, goto error);
1693 qp->dim = isl_dim_drop(qp->dim, type, first, n);
1694 if (!qp->dim)
1695 goto error;
1697 if (type == isl_dim_set)
1698 first += isl_dim_size(qp->dim, isl_dim_param);
1700 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
1701 if (!qp->div)
1702 goto error;
1704 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
1705 if (!qp->upoly)
1706 goto error;
1708 return qp;
1709 error:
1710 isl_qpolynomial_free(qp);
1711 return NULL;
1714 #undef PW
1715 #define PW isl_pw_qpolynomial
1716 #undef EL
1717 #define EL isl_qpolynomial
1718 #undef IS_ZERO
1719 #define IS_ZERO is_zero
1720 #undef FIELD
1721 #define FIELD qp
1722 #undef ADD
1723 #define ADD(d,e1,e2) isl_qpolynomial_add(e1,e2)
1725 #include <isl_pw_templ.c>
1727 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
1729 if (!pwqp)
1730 return -1;
1732 if (pwqp->n != -1)
1733 return 0;
1735 if (!isl_set_fast_is_universe(pwqp->p[0].set))
1736 return 0;
1738 return isl_qpolynomial_is_one(pwqp->p[0].qp);
1741 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
1742 __isl_take isl_pw_qpolynomial *pwqp1,
1743 __isl_take isl_pw_qpolynomial *pwqp2)
1745 int i, j, n;
1746 struct isl_pw_qpolynomial *res;
1747 isl_set *set;
1749 if (!pwqp1 || !pwqp2)
1750 goto error;
1752 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
1753 goto error);
1755 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
1756 isl_pw_qpolynomial_free(pwqp2);
1757 return pwqp1;
1760 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
1761 isl_pw_qpolynomial_free(pwqp1);
1762 return pwqp2;
1765 if (isl_pw_qpolynomial_is_one(pwqp1)) {
1766 isl_pw_qpolynomial_free(pwqp1);
1767 return pwqp2;
1770 if (isl_pw_qpolynomial_is_one(pwqp2)) {
1771 isl_pw_qpolynomial_free(pwqp2);
1772 return pwqp1;
1775 n = pwqp1->n * pwqp2->n;
1776 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
1778 for (i = 0; i < pwqp1->n; ++i) {
1779 for (j = 0; j < pwqp2->n; ++j) {
1780 struct isl_set *common;
1781 struct isl_qpolynomial *prod;
1782 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
1783 isl_set_copy(pwqp2->p[j].set));
1784 if (isl_set_fast_is_empty(common)) {
1785 isl_set_free(common);
1786 continue;
1789 prod = isl_qpolynomial_mul(
1790 isl_qpolynomial_copy(pwqp1->p[i].qp),
1791 isl_qpolynomial_copy(pwqp2->p[j].qp));
1793 res = isl_pw_qpolynomial_add_piece(res, common, prod);
1797 isl_pw_qpolynomial_free(pwqp1);
1798 isl_pw_qpolynomial_free(pwqp2);
1800 return res;
1801 error:
1802 isl_pw_qpolynomial_free(pwqp1);
1803 isl_pw_qpolynomial_free(pwqp2);
1804 return NULL;
1807 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
1808 __isl_take isl_pw_qpolynomial *pwqp)
1810 int i, j, n;
1811 struct isl_pw_qpolynomial *res;
1812 isl_set *set;
1814 if (!pwqp)
1815 return NULL;
1817 if (isl_pw_qpolynomial_is_zero(pwqp))
1818 return pwqp;
1820 pwqp = isl_pw_qpolynomial_cow(pwqp);
1822 for (i = 0; i < pwqp->n; ++i) {
1823 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
1824 if (!pwqp->p[i].qp)
1825 goto error;
1828 return pwqp;
1829 error:
1830 isl_pw_qpolynomial_free(pwqp);
1831 return NULL;
1834 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
1835 __isl_take isl_pw_qpolynomial *pwqp1,
1836 __isl_take isl_pw_qpolynomial *pwqp2)
1838 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
1841 __isl_give struct isl_upoly *isl_upoly_eval(
1842 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
1844 int i;
1845 struct isl_upoly_rec *rec;
1846 struct isl_upoly *res;
1847 struct isl_upoly *base;
1849 if (isl_upoly_is_cst(up)) {
1850 isl_vec_free(vec);
1851 return up;
1854 rec = isl_upoly_as_rec(up);
1855 if (!rec)
1856 goto error;
1858 isl_assert(up->ctx, rec->n >= 1, goto error);
1860 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
1862 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
1863 isl_vec_copy(vec));
1865 for (i = rec->n - 2; i >= 0; --i) {
1866 res = isl_upoly_mul(res, isl_upoly_copy(base));
1867 res = isl_upoly_sum(res,
1868 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
1869 isl_vec_copy(vec)));
1872 isl_upoly_free(base);
1873 isl_upoly_free(up);
1874 isl_vec_free(vec);
1875 return res;
1876 error:
1877 isl_upoly_free(up);
1878 isl_vec_free(vec);
1879 return NULL;
1882 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
1883 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
1885 isl_vec *ext;
1886 struct isl_upoly *up;
1887 isl_dim *dim;
1889 if (!qp || !pnt)
1890 goto error;
1891 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
1893 if (qp->div->n_row == 0)
1894 ext = isl_vec_copy(pnt->vec);
1895 else {
1896 int i;
1897 unsigned dim = isl_dim_total(qp->dim);
1898 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
1899 if (!ext)
1900 goto error;
1902 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
1903 for (i = 0; i < qp->div->n_row; ++i) {
1904 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
1905 1 + dim + i, &ext->el[1+dim+i]);
1906 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
1907 qp->div->row[i][0]);
1911 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
1912 if (!up)
1913 goto error;
1915 dim = isl_dim_copy(qp->dim);
1916 isl_qpolynomial_free(qp);
1917 isl_point_free(pnt);
1919 return isl_qpolynomial_alloc(dim, 0, up);
1920 error:
1921 isl_qpolynomial_free(qp);
1922 isl_point_free(pnt);
1923 return NULL;
1926 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
1927 __isl_keep struct isl_upoly_cst *cst2)
1929 int cmp;
1930 isl_int t;
1931 isl_int_init(t);
1932 isl_int_mul(t, cst1->n, cst2->d);
1933 isl_int_submul(t, cst2->n, cst1->d);
1934 cmp = isl_int_sgn(t);
1935 isl_int_clear(t);
1936 return cmp;
1939 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
1940 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1942 struct isl_upoly_cst *cst1, *cst2;
1943 int cmp;
1945 if (!qp1 || !qp2)
1946 goto error;
1947 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
1948 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
1949 cst1 = isl_upoly_as_cst(qp1->upoly);
1950 cst2 = isl_upoly_as_cst(qp2->upoly);
1951 cmp = isl_upoly_cmp(cst1, cst2);
1953 if (cmp <= 0) {
1954 isl_qpolynomial_free(qp2);
1955 } else {
1956 isl_qpolynomial_free(qp1);
1957 qp1 = qp2;
1959 return qp1;
1960 error:
1961 isl_qpolynomial_free(qp1);
1962 isl_qpolynomial_free(qp2);
1963 return NULL;
1966 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
1967 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1969 struct isl_upoly_cst *cst1, *cst2;
1970 int cmp;
1972 if (!qp1 || !qp2)
1973 goto error;
1974 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
1975 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
1976 cst1 = isl_upoly_as_cst(qp1->upoly);
1977 cst2 = isl_upoly_as_cst(qp2->upoly);
1978 cmp = isl_upoly_cmp(cst1, cst2);
1980 if (cmp >= 0) {
1981 isl_qpolynomial_free(qp2);
1982 } else {
1983 isl_qpolynomial_free(qp1);
1984 qp1 = qp2;
1986 return qp1;
1987 error:
1988 isl_qpolynomial_free(qp1);
1989 isl_qpolynomial_free(qp2);
1990 return NULL;
1993 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
1994 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
1996 unsigned total;
1997 unsigned g_pos;
1998 int *exp;
2000 if (n == 0)
2001 return qp;
2003 qp = isl_qpolynomial_cow(qp);
2004 if (!qp)
2005 return NULL;
2007 g_pos = pos(qp->dim, type) + isl_dim_size(qp->dim, type);
2009 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2010 if (!qp->div)
2011 goto error;
2013 total = qp->div->n_col - 2;
2014 if (total > g_pos) {
2015 int i;
2016 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2017 if (!exp)
2018 goto error;
2019 for (i = 0; i < total - g_pos; ++i)
2020 exp[i] = i + n;
2021 qp->upoly = expand(qp->upoly, exp, g_pos);
2022 free(exp);
2023 if (!qp->upoly)
2024 goto error;
2027 qp->dim = isl_dim_add(qp->dim, type, n);
2028 if (!qp->dim)
2029 goto error;
2031 return qp;
2032 error:
2033 isl_qpolynomial_free(qp);
2034 return NULL;
2037 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2038 __isl_take isl_pw_qpolynomial *pwqp,
2039 enum isl_dim_type type, unsigned n)
2041 int i;
2043 if (n == 0)
2044 return pwqp;
2046 pwqp = isl_pw_qpolynomial_cow(pwqp);
2047 if (!pwqp)
2048 return NULL;
2050 pwqp->dim = isl_dim_add(pwqp->dim, type, n);
2051 if (!pwqp->dim)
2052 goto error;
2054 for (i = 0; i < pwqp->n; ++i) {
2055 pwqp->p[i].set = isl_set_add(pwqp->p[i].set, type, n);
2056 if (!pwqp->p[i].set)
2057 goto error;
2058 pwqp->p[i].qp = isl_qpolynomial_add_dims(pwqp->p[i].qp, type, n);
2059 if (!pwqp->p[i].qp)
2060 goto error;
2063 return pwqp;
2064 error:
2065 isl_pw_qpolynomial_free(pwqp);
2066 return NULL;
2069 static int *reordering_move(isl_ctx *ctx,
2070 unsigned len, unsigned dst, unsigned src, unsigned n)
2072 int i;
2073 int *reordering;
2075 reordering = isl_alloc_array(ctx, int, len);
2076 if (!reordering)
2077 return NULL;
2079 if (dst <= src) {
2080 for (i = 0; i < dst; ++i)
2081 reordering[i] = i;
2082 for (i = 0; i < n; ++i)
2083 reordering[src + i] = dst + i;
2084 for (i = 0; i < src - dst; ++i)
2085 reordering[dst + i] = dst + n + i;
2086 for (i = 0; i < len - src - n; ++i)
2087 reordering[src + n + i] = src + n + i;
2088 } else {
2089 for (i = 0; i < src; ++i)
2090 reordering[i] = i;
2091 for (i = 0; i < n; ++i)
2092 reordering[src + i] = dst + i;
2093 for (i = 0; i < dst - src; ++i)
2094 reordering[src + n + i] = src + i;
2095 for (i = 0; i < len - dst - n; ++i)
2096 reordering[dst + n + i] = dst + n + i;
2099 return reordering;
2102 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
2103 int *r)
2105 int i;
2106 struct isl_upoly_rec *rec;
2107 struct isl_upoly *base;
2108 struct isl_upoly *res;
2110 if (isl_upoly_is_cst(up))
2111 return up;
2113 rec = isl_upoly_as_rec(up);
2114 if (!rec)
2115 goto error;
2117 isl_assert(up->ctx, rec->n >= 1, goto error);
2119 base = isl_upoly_pow(up->ctx, r[up->var], 1);
2120 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
2122 for (i = rec->n - 2; i >= 0; --i) {
2123 res = isl_upoly_mul(res, isl_upoly_copy(base));
2124 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
2127 isl_upoly_free(base);
2128 isl_upoly_free(up);
2130 return res;
2131 error:
2132 isl_upoly_free(up);
2133 return NULL;
2136 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2137 __isl_take isl_qpolynomial *qp,
2138 enum isl_dim_type dst_type, unsigned dst_pos,
2139 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2141 unsigned g_dst_pos;
2142 unsigned g_src_pos;
2143 int *reordering;
2145 qp = isl_qpolynomial_cow(qp);
2146 if (!qp)
2147 return NULL;
2149 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2150 goto error);
2152 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2153 g_src_pos = pos(qp->dim, src_type) + src_pos;
2154 if (dst_type > src_type)
2155 g_dst_pos -= n;
2157 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2158 qp->div = sort_divs(qp->div);
2159 if (!qp->div)
2160 goto error;
2162 reordering = reordering_move(qp->dim->ctx,
2163 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2164 if (!reordering)
2165 goto error;
2167 qp->upoly = reorder(qp->upoly, reordering);
2168 free(reordering);
2169 if (!qp->upoly)
2170 goto error;
2172 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2173 if (!qp->dim)
2174 goto error;
2176 return qp;
2177 error:
2178 isl_qpolynomial_free(qp);
2179 return NULL;
2182 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_move(
2183 __isl_take isl_pw_qpolynomial *pwqp,
2184 enum isl_dim_type dst_type, unsigned dst_pos,
2185 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2187 int i;
2189 pwqp = isl_pw_qpolynomial_cow(pwqp);
2190 if (!pwqp)
2191 return NULL;
2193 pwqp->dim = isl_dim_move(pwqp->dim,
2194 dst_type, dst_pos, src_type, src_pos, n);
2195 if (!pwqp->dim)
2196 goto error;
2198 for (i = 0; i < pwqp->n; ++i) {
2199 pwqp->p[i].set = isl_set_move_dims(pwqp->p[i].set,
2200 dst_type, dst_pos,
2201 src_type, src_pos, n);
2202 if (!pwqp->p[i].set)
2203 goto error;
2204 pwqp->p[i].qp = isl_qpolynomial_move_dims(pwqp->p[i].qp,
2205 dst_type, dst_pos, src_type, src_pos, n);
2206 if (!pwqp->p[i].qp)
2207 goto error;
2210 return pwqp;
2211 error:
2212 isl_pw_qpolynomial_free(pwqp);
2213 return NULL;
2216 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
2217 __isl_take isl_mat *div)
2219 isl_term *term;
2220 int n;
2222 if (!dim || !div)
2223 goto error;
2225 n = isl_dim_total(dim) + div->n_row;
2227 term = isl_calloc(dim->ctx, struct isl_term,
2228 sizeof(struct isl_term) + (n - 1) * sizeof(int));
2229 if (!term)
2230 goto error;
2232 term->ref = 1;
2233 term->dim = dim;
2234 term->div = div;
2235 isl_int_init(term->n);
2236 isl_int_init(term->d);
2238 return term;
2239 error:
2240 isl_dim_free(dim);
2241 isl_mat_free(div);
2242 return NULL;
2245 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
2247 if (!term)
2248 return NULL;
2250 term->ref++;
2251 return term;
2254 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
2256 int i;
2257 isl_term *dup;
2258 unsigned total;
2260 if (term)
2261 return NULL;
2263 total = isl_dim_total(term->dim) + term->div->n_row;
2265 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
2266 if (!dup)
2267 return NULL;
2269 isl_int_set(dup->n, term->n);
2270 isl_int_set(dup->d, term->d);
2272 for (i = 0; i < total; ++i)
2273 dup->pow[i] = term->pow[i];
2275 return dup;
2278 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
2280 if (!term)
2281 return NULL;
2283 if (term->ref == 1)
2284 return term;
2285 term->ref--;
2286 return isl_term_dup(term);
2289 void isl_term_free(__isl_take isl_term *term)
2291 if (!term)
2292 return;
2294 if (--term->ref > 0)
2295 return;
2297 isl_dim_free(term->dim);
2298 isl_mat_free(term->div);
2299 isl_int_clear(term->n);
2300 isl_int_clear(term->d);
2301 free(term);
2304 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
2306 if (!term)
2307 return 0;
2309 switch (type) {
2310 case isl_dim_param:
2311 case isl_dim_in:
2312 case isl_dim_out: return isl_dim_size(term->dim, type);
2313 case isl_dim_div: return term->div->n_row;
2314 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
2318 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
2320 return term ? term->dim->ctx : NULL;
2323 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
2325 if (!term)
2326 return;
2327 isl_int_set(*n, term->n);
2330 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
2332 if (!term)
2333 return;
2334 isl_int_set(*d, term->d);
2337 int isl_term_get_exp(__isl_keep isl_term *term,
2338 enum isl_dim_type type, unsigned pos)
2340 if (!term)
2341 return -1;
2343 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
2345 if (type >= isl_dim_set)
2346 pos += isl_dim_size(term->dim, isl_dim_param);
2347 if (type >= isl_dim_div)
2348 pos += isl_dim_size(term->dim, isl_dim_set);
2350 return term->pow[pos];
2353 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
2355 isl_basic_map *bmap;
2356 unsigned total;
2357 int k;
2359 if (!term)
2360 return NULL;
2362 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
2363 return NULL);
2365 total = term->div->n_col - term->div->n_row - 2;
2366 /* No nested divs for now */
2367 isl_assert(term->dim->ctx,
2368 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
2369 term->div->n_row) == -1,
2370 return NULL);
2372 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
2373 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
2374 goto error;
2376 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
2378 return isl_basic_map_div(bmap, k);
2379 error:
2380 isl_basic_map_free(bmap);
2381 return NULL;
2384 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
2385 int (*fn)(__isl_take isl_term *term, void *user),
2386 __isl_take isl_term *term, void *user)
2388 int i;
2389 struct isl_upoly_rec *rec;
2391 if (!up || !term)
2392 goto error;
2394 if (isl_upoly_is_zero(up))
2395 return term;
2397 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
2398 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
2399 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
2401 if (isl_upoly_is_cst(up)) {
2402 struct isl_upoly_cst *cst;
2403 cst = isl_upoly_as_cst(up);
2404 if (!cst)
2405 goto error;
2406 term = isl_term_cow(term);
2407 if (!term)
2408 goto error;
2409 isl_int_set(term->n, cst->n);
2410 isl_int_set(term->d, cst->d);
2411 if (fn(isl_term_copy(term), user) < 0)
2412 goto error;
2413 return term;
2416 rec = isl_upoly_as_rec(up);
2417 if (!rec)
2418 goto error;
2420 for (i = 0; i < rec->n; ++i) {
2421 term = isl_term_cow(term);
2422 if (!term)
2423 goto error;
2424 term->pow[up->var] = i;
2425 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
2426 if (!term)
2427 goto error;
2429 term->pow[up->var] = 0;
2431 return term;
2432 error:
2433 isl_term_free(term);
2434 return NULL;
2437 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
2438 int (*fn)(__isl_take isl_term *term, void *user), void *user)
2440 isl_term *term;
2442 if (!qp)
2443 return -1;
2445 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
2446 if (!term)
2447 return -1;
2449 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
2451 isl_term_free(term);
2453 return term ? 0 : -1;
2456 int isl_pw_qpolynomial_foreach_piece(__isl_keep isl_pw_qpolynomial *pwqp,
2457 int (*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
2458 void *user), void *user)
2460 int i;
2462 if (!pwqp)
2463 return -1;
2465 for (i = 0; i < pwqp->n; ++i)
2466 if (fn(isl_set_copy(pwqp->p[i].set),
2467 isl_qpolynomial_copy(pwqp->p[i].qp), user) < 0)
2468 return -1;
2470 return 0;
2473 static int any_divs(__isl_keep isl_set *set)
2475 int i;
2477 if (!set)
2478 return -1;
2480 for (i = 0; i < set->n; ++i)
2481 if (set->p[i]->n_div > 0)
2482 return 1;
2484 return 0;
2487 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
2488 __isl_take isl_dim *dim)
2490 if (!qp || !dim)
2491 goto error;
2493 if (isl_dim_equal(qp->dim, dim)) {
2494 isl_dim_free(dim);
2495 return qp;
2498 qp = isl_qpolynomial_cow(qp);
2499 if (!qp)
2500 goto error;
2502 if (qp->div->n_row) {
2503 int i;
2504 int extra;
2505 unsigned total;
2506 int *exp;
2508 extra = isl_dim_size(dim, isl_dim_set) -
2509 isl_dim_size(qp->dim, isl_dim_set);
2510 total = isl_dim_total(qp->dim);
2511 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
2512 if (!exp)
2513 goto error;
2514 for (i = 0; i < qp->div->n_row; ++i)
2515 exp[i] = extra + i;
2516 qp->upoly = expand(qp->upoly, exp, total);
2517 free(exp);
2518 if (!qp->upoly)
2519 goto error;
2520 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
2521 if (!qp->div)
2522 goto error;
2523 for (i = 0; i < qp->div->n_row; ++i)
2524 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
2527 isl_dim_free(qp->dim);
2528 qp->dim = dim;
2530 return qp;
2531 error:
2532 isl_dim_free(dim);
2533 isl_qpolynomial_free(qp);
2534 return NULL;
2537 static int foreach_lifted_subset(__isl_take isl_set *set,
2538 __isl_take isl_qpolynomial *qp,
2539 int (*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
2540 void *user), void *user)
2542 int i;
2544 if (!set || !qp)
2545 goto error;
2547 for (i = 0; i < set->n; ++i) {
2548 isl_set *lift;
2549 isl_qpolynomial *copy;
2551 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
2552 lift = isl_set_lift(lift);
2554 copy = isl_qpolynomial_copy(qp);
2555 copy = isl_qpolynomial_lift(copy, isl_set_get_dim(lift));
2557 if (fn(lift, copy, user) < 0)
2558 goto error;
2561 isl_set_free(set);
2562 isl_qpolynomial_free(qp);
2564 return 0;
2565 error:
2566 isl_set_free(set);
2567 isl_qpolynomial_free(qp);
2568 return -1;
2571 int isl_pw_qpolynomial_foreach_lifted_piece(__isl_keep isl_pw_qpolynomial *pwqp,
2572 int (*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
2573 void *user), void *user)
2575 int i;
2577 if (!pwqp)
2578 return -1;
2580 for (i = 0; i < pwqp->n; ++i) {
2581 isl_set *set;
2582 isl_qpolynomial *qp;
2584 set = isl_set_copy(pwqp->p[i].set);
2585 qp = isl_qpolynomial_copy(pwqp->p[i].qp);
2586 if (!any_divs(set)) {
2587 if (fn(set, qp, user) < 0)
2588 return -1;
2589 continue;
2591 if (foreach_lifted_subset(set, qp, fn, user) < 0)
2592 return -1;
2595 return 0;
2598 /* For each parameter or variable that does not appear in qp,
2599 * first eliminate the variable from all constraints and then set it to zero.
2601 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
2602 __isl_keep isl_qpolynomial *qp)
2604 int *active = NULL;
2605 int i;
2606 int d;
2607 unsigned nparam;
2608 unsigned nvar;
2610 if (!set || !qp)
2611 goto error;
2613 d = isl_dim_total(set->dim);
2614 active = isl_calloc_array(set->ctx, int, d);
2615 if (set_active(qp, active) < 0)
2616 goto error;
2618 for (i = 0; i < d; ++i)
2619 if (!active[i])
2620 break;
2622 if (i == d) {
2623 free(active);
2624 return set;
2627 nparam = isl_dim_size(set->dim, isl_dim_param);
2628 nvar = isl_dim_size(set->dim, isl_dim_set);
2629 for (i = 0; i < nparam; ++i) {
2630 if (active[i])
2631 continue;
2632 set = isl_set_eliminate(set, isl_dim_param, i, 1);
2633 set = isl_set_fix_si(set, isl_dim_param, i, 0);
2635 for (i = 0; i < nvar; ++i) {
2636 if (active[nparam + i])
2637 continue;
2638 set = isl_set_eliminate(set, isl_dim_set, i, 1);
2639 set = isl_set_fix_si(set, isl_dim_set, i, 0);
2642 free(active);
2644 return set;
2645 error:
2646 free(active);
2647 isl_set_free(set);
2648 return NULL;
2651 struct isl_opt_data {
2652 isl_qpolynomial *qp;
2653 int first;
2654 isl_qpolynomial *opt;
2655 int max;
2658 static int opt_fn(__isl_take isl_point *pnt, void *user)
2660 struct isl_opt_data *data = (struct isl_opt_data *)user;
2661 isl_qpolynomial *val;
2663 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
2664 if (data->first) {
2665 data->first = 0;
2666 data->opt = val;
2667 } else if (data->max) {
2668 data->opt = isl_qpolynomial_max_cst(data->opt, val);
2669 } else {
2670 data->opt = isl_qpolynomial_min_cst(data->opt, val);
2673 return 0;
2676 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
2677 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
2679 struct isl_opt_data data = { NULL, 1, NULL, max };
2681 if (!set || !qp)
2682 goto error;
2684 if (isl_upoly_is_cst(qp->upoly)) {
2685 isl_set_free(set);
2686 return qp;
2689 set = fix_inactive(set, qp);
2691 data.qp = qp;
2692 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
2693 goto error;
2695 isl_set_free(set);
2696 isl_qpolynomial_free(qp);
2697 return data.opt;
2698 error:
2699 isl_set_free(set);
2700 isl_qpolynomial_free(qp);
2701 isl_qpolynomial_free(data.opt);
2702 return NULL;