isl_stream: maintain private ungetc buffer
[isl.git] / isl_polynomial.c
blob27c2ed05734fca0437269f296f077103789d0637
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_map_private.h>
13 #include <isl_factorization.h>
14 #include <isl/lp.h>
15 #include <isl/seq.h>
16 #include <isl_union_map_private.h>
17 #include <isl_polynomial_private.h>
18 #include <isl_point_private.h>
19 #include <isl_dim_private.h>
20 #include <isl_mat_private.h>
21 #include <isl_range.h>
23 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
25 switch (type) {
26 case isl_dim_param: return 0;
27 case isl_dim_in: return dim->nparam;
28 case isl_dim_out: return dim->nparam + dim->n_in;
29 default: return 0;
33 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
35 if (!up)
36 return -1;
38 return up->var < 0;
41 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
43 if (!up)
44 return NULL;
46 isl_assert(up->ctx, up->var < 0, return NULL);
48 return (struct isl_upoly_cst *)up;
51 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
53 if (!up)
54 return NULL;
56 isl_assert(up->ctx, up->var >= 0, return NULL);
58 return (struct isl_upoly_rec *)up;
61 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
62 __isl_keep struct isl_upoly *up2)
64 int i;
65 struct isl_upoly_rec *rec1, *rec2;
67 if (!up1 || !up2)
68 return -1;
69 if (up1 == up2)
70 return 1;
71 if (up1->var != up2->var)
72 return 0;
73 if (isl_upoly_is_cst(up1)) {
74 struct isl_upoly_cst *cst1, *cst2;
75 cst1 = isl_upoly_as_cst(up1);
76 cst2 = isl_upoly_as_cst(up2);
77 if (!cst1 || !cst2)
78 return -1;
79 return isl_int_eq(cst1->n, cst2->n) &&
80 isl_int_eq(cst1->d, cst2->d);
83 rec1 = isl_upoly_as_rec(up1);
84 rec2 = isl_upoly_as_rec(up2);
85 if (!rec1 || !rec2)
86 return -1;
88 if (rec1->n != rec2->n)
89 return 0;
91 for (i = 0; i < rec1->n; ++i) {
92 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
93 if (eq < 0 || !eq)
94 return eq;
97 return 1;
100 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
102 struct isl_upoly_cst *cst;
104 if (!up)
105 return -1;
106 if (!isl_upoly_is_cst(up))
107 return 0;
109 cst = isl_upoly_as_cst(up);
110 if (!cst)
111 return -1;
113 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
116 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
118 struct isl_upoly_cst *cst;
120 if (!up)
121 return 0;
122 if (!isl_upoly_is_cst(up))
123 return 0;
125 cst = isl_upoly_as_cst(up);
126 if (!cst)
127 return 0;
129 return isl_int_sgn(cst->n);
132 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
134 struct isl_upoly_cst *cst;
136 if (!up)
137 return -1;
138 if (!isl_upoly_is_cst(up))
139 return 0;
141 cst = isl_upoly_as_cst(up);
142 if (!cst)
143 return -1;
145 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
148 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
150 struct isl_upoly_cst *cst;
152 if (!up)
153 return -1;
154 if (!isl_upoly_is_cst(up))
155 return 0;
157 cst = isl_upoly_as_cst(up);
158 if (!cst)
159 return -1;
161 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
164 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
166 struct isl_upoly_cst *cst;
168 if (!up)
169 return -1;
170 if (!isl_upoly_is_cst(up))
171 return 0;
173 cst = isl_upoly_as_cst(up);
174 if (!cst)
175 return -1;
177 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
180 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
182 struct isl_upoly_cst *cst;
184 if (!up)
185 return -1;
186 if (!isl_upoly_is_cst(up))
187 return 0;
189 cst = isl_upoly_as_cst(up);
190 if (!cst)
191 return -1;
193 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
196 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
198 struct isl_upoly_cst *cst;
200 if (!up)
201 return -1;
202 if (!isl_upoly_is_cst(up))
203 return 0;
205 cst = isl_upoly_as_cst(up);
206 if (!cst)
207 return -1;
209 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
212 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
214 struct isl_upoly_cst *cst;
216 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
217 if (!cst)
218 return NULL;
220 cst->up.ref = 1;
221 cst->up.ctx = ctx;
222 isl_ctx_ref(ctx);
223 cst->up.var = -1;
225 isl_int_init(cst->n);
226 isl_int_init(cst->d);
228 return cst;
231 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
233 struct isl_upoly_cst *cst;
235 cst = isl_upoly_cst_alloc(ctx);
236 if (!cst)
237 return NULL;
239 isl_int_set_si(cst->n, 0);
240 isl_int_set_si(cst->d, 1);
242 return &cst->up;
245 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
247 struct isl_upoly_cst *cst;
249 cst = isl_upoly_cst_alloc(ctx);
250 if (!cst)
251 return NULL;
253 isl_int_set_si(cst->n, 1);
254 isl_int_set_si(cst->d, 1);
256 return &cst->up;
259 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
261 struct isl_upoly_cst *cst;
263 cst = isl_upoly_cst_alloc(ctx);
264 if (!cst)
265 return NULL;
267 isl_int_set_si(cst->n, 1);
268 isl_int_set_si(cst->d, 0);
270 return &cst->up;
273 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
275 struct isl_upoly_cst *cst;
277 cst = isl_upoly_cst_alloc(ctx);
278 if (!cst)
279 return NULL;
281 isl_int_set_si(cst->n, -1);
282 isl_int_set_si(cst->d, 0);
284 return &cst->up;
287 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
289 struct isl_upoly_cst *cst;
291 cst = isl_upoly_cst_alloc(ctx);
292 if (!cst)
293 return NULL;
295 isl_int_set_si(cst->n, 0);
296 isl_int_set_si(cst->d, 0);
298 return &cst->up;
301 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
302 isl_int n, isl_int d)
304 struct isl_upoly_cst *cst;
306 cst = isl_upoly_cst_alloc(ctx);
307 if (!cst)
308 return NULL;
310 isl_int_set(cst->n, n);
311 isl_int_set(cst->d, d);
313 return &cst->up;
316 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
317 int var, int size)
319 struct isl_upoly_rec *rec;
321 isl_assert(ctx, var >= 0, return NULL);
322 isl_assert(ctx, size >= 0, return NULL);
323 rec = isl_calloc(ctx, struct isl_upoly_rec,
324 sizeof(struct isl_upoly_rec) +
325 (size - 1) * sizeof(struct isl_upoly *));
326 if (!rec)
327 return NULL;
329 rec->up.ref = 1;
330 rec->up.ctx = ctx;
331 isl_ctx_ref(ctx);
332 rec->up.var = var;
334 rec->n = 0;
335 rec->size = size;
337 return rec;
340 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
341 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
343 qp = isl_qpolynomial_cow(qp);
344 if (!qp || !dim)
345 goto error;
347 isl_dim_free(qp->dim);
348 qp->dim = dim;
350 return qp;
351 error:
352 isl_qpolynomial_free(qp);
353 isl_dim_free(dim);
354 return NULL;
357 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
359 return qp ? qp->dim->ctx : NULL;
362 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
364 return qp ? isl_dim_copy(qp->dim) : NULL;
367 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
368 enum isl_dim_type type)
370 return qp ? isl_dim_size(qp->dim, type) : 0;
373 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
375 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
378 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
380 return qp ? isl_upoly_is_one(qp->upoly) : -1;
383 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
385 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
388 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
390 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
393 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
395 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
398 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
400 return qp ? isl_upoly_sgn(qp->upoly) : 0;
403 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
405 isl_int_clear(cst->n);
406 isl_int_clear(cst->d);
409 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
411 int i;
413 for (i = 0; i < rec->n; ++i)
414 isl_upoly_free(rec->p[i]);
417 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
419 if (!up)
420 return NULL;
422 up->ref++;
423 return up;
426 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
428 struct isl_upoly_cst *cst;
429 struct isl_upoly_cst *dup;
431 cst = isl_upoly_as_cst(up);
432 if (!cst)
433 return NULL;
435 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
436 if (!dup)
437 return NULL;
438 isl_int_set(dup->n, cst->n);
439 isl_int_set(dup->d, cst->d);
441 return &dup->up;
444 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
446 int i;
447 struct isl_upoly_rec *rec;
448 struct isl_upoly_rec *dup;
450 rec = isl_upoly_as_rec(up);
451 if (!rec)
452 return NULL;
454 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
455 if (!dup)
456 return NULL;
458 for (i = 0; i < rec->n; ++i) {
459 dup->p[i] = isl_upoly_copy(rec->p[i]);
460 if (!dup->p[i])
461 goto error;
462 dup->n++;
465 return &dup->up;
466 error:
467 isl_upoly_free(&dup->up);
468 return NULL;
471 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
473 struct isl_upoly *dup;
475 if (!up)
476 return NULL;
478 if (isl_upoly_is_cst(up))
479 return isl_upoly_dup_cst(up);
480 else
481 return isl_upoly_dup_rec(up);
484 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
486 if (!up)
487 return NULL;
489 if (up->ref == 1)
490 return up;
491 up->ref--;
492 return isl_upoly_dup(up);
495 void isl_upoly_free(__isl_take struct isl_upoly *up)
497 if (!up)
498 return;
500 if (--up->ref > 0)
501 return;
503 if (up->var < 0)
504 upoly_free_cst((struct isl_upoly_cst *)up);
505 else
506 upoly_free_rec((struct isl_upoly_rec *)up);
508 isl_ctx_deref(up->ctx);
509 free(up);
512 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
514 isl_int gcd;
516 isl_int_init(gcd);
517 isl_int_gcd(gcd, cst->n, cst->d);
518 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
519 isl_int_divexact(cst->n, cst->n, gcd);
520 isl_int_divexact(cst->d, cst->d, gcd);
522 isl_int_clear(gcd);
525 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
526 __isl_take struct isl_upoly *up2)
528 struct isl_upoly_cst *cst1;
529 struct isl_upoly_cst *cst2;
531 up1 = isl_upoly_cow(up1);
532 if (!up1 || !up2)
533 goto error;
535 cst1 = isl_upoly_as_cst(up1);
536 cst2 = isl_upoly_as_cst(up2);
538 if (isl_int_eq(cst1->d, cst2->d))
539 isl_int_add(cst1->n, cst1->n, cst2->n);
540 else {
541 isl_int_mul(cst1->n, cst1->n, cst2->d);
542 isl_int_addmul(cst1->n, cst2->n, cst1->d);
543 isl_int_mul(cst1->d, cst1->d, cst2->d);
546 isl_upoly_cst_reduce(cst1);
548 isl_upoly_free(up2);
549 return up1;
550 error:
551 isl_upoly_free(up1);
552 isl_upoly_free(up2);
553 return NULL;
556 static __isl_give struct isl_upoly *replace_by_zero(
557 __isl_take struct isl_upoly *up)
559 struct isl_ctx *ctx;
561 if (!up)
562 return NULL;
563 ctx = up->ctx;
564 isl_upoly_free(up);
565 return isl_upoly_zero(ctx);
568 static __isl_give struct isl_upoly *replace_by_constant_term(
569 __isl_take struct isl_upoly *up)
571 struct isl_upoly_rec *rec;
572 struct isl_upoly *cst;
574 if (!up)
575 return NULL;
577 rec = isl_upoly_as_rec(up);
578 if (!rec)
579 goto error;
580 cst = isl_upoly_copy(rec->p[0]);
581 isl_upoly_free(up);
582 return cst;
583 error:
584 isl_upoly_free(up);
585 return NULL;
588 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
589 __isl_take struct isl_upoly *up2)
591 int i;
592 struct isl_upoly_rec *rec1, *rec2;
594 if (!up1 || !up2)
595 goto error;
597 if (isl_upoly_is_nan(up1)) {
598 isl_upoly_free(up2);
599 return up1;
602 if (isl_upoly_is_nan(up2)) {
603 isl_upoly_free(up1);
604 return up2;
607 if (isl_upoly_is_zero(up1)) {
608 isl_upoly_free(up1);
609 return up2;
612 if (isl_upoly_is_zero(up2)) {
613 isl_upoly_free(up2);
614 return up1;
617 if (up1->var < up2->var)
618 return isl_upoly_sum(up2, up1);
620 if (up2->var < up1->var) {
621 struct isl_upoly_rec *rec;
622 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
623 isl_upoly_free(up1);
624 return up2;
626 up1 = isl_upoly_cow(up1);
627 rec = isl_upoly_as_rec(up1);
628 if (!rec)
629 goto error;
630 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
631 if (rec->n == 1)
632 up1 = replace_by_constant_term(up1);
633 return up1;
636 if (isl_upoly_is_cst(up1))
637 return isl_upoly_sum_cst(up1, up2);
639 rec1 = isl_upoly_as_rec(up1);
640 rec2 = isl_upoly_as_rec(up2);
641 if (!rec1 || !rec2)
642 goto error;
644 if (rec1->n < rec2->n)
645 return isl_upoly_sum(up2, up1);
647 up1 = isl_upoly_cow(up1);
648 rec1 = isl_upoly_as_rec(up1);
649 if (!rec1)
650 goto error;
652 for (i = rec2->n - 1; i >= 0; --i) {
653 rec1->p[i] = isl_upoly_sum(rec1->p[i],
654 isl_upoly_copy(rec2->p[i]));
655 if (!rec1->p[i])
656 goto error;
657 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
658 isl_upoly_free(rec1->p[i]);
659 rec1->n--;
663 if (rec1->n == 0)
664 up1 = replace_by_zero(up1);
665 else if (rec1->n == 1)
666 up1 = replace_by_constant_term(up1);
668 isl_upoly_free(up2);
670 return up1;
671 error:
672 isl_upoly_free(up1);
673 isl_upoly_free(up2);
674 return NULL;
677 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
678 __isl_take struct isl_upoly *up, isl_int v)
680 struct isl_upoly_cst *cst;
682 up = isl_upoly_cow(up);
683 if (!up)
684 return NULL;
686 cst = isl_upoly_as_cst(up);
688 isl_int_addmul(cst->n, cst->d, v);
690 return up;
693 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
694 __isl_take struct isl_upoly *up, isl_int v)
696 struct isl_upoly_rec *rec;
698 if (!up)
699 return NULL;
701 if (isl_upoly_is_cst(up))
702 return isl_upoly_cst_add_isl_int(up, v);
704 up = isl_upoly_cow(up);
705 rec = isl_upoly_as_rec(up);
706 if (!rec)
707 goto error;
709 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
710 if (!rec->p[0])
711 goto error;
713 return up;
714 error:
715 isl_upoly_free(up);
716 return NULL;
719 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
720 __isl_take struct isl_upoly *up, isl_int v)
722 struct isl_upoly_cst *cst;
724 if (isl_upoly_is_zero(up))
725 return up;
727 up = isl_upoly_cow(up);
728 if (!up)
729 return NULL;
731 cst = isl_upoly_as_cst(up);
733 isl_int_mul(cst->n, cst->n, v);
735 return up;
738 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
739 __isl_take struct isl_upoly *up, isl_int v)
741 int i;
742 struct isl_upoly_rec *rec;
744 if (!up)
745 return NULL;
747 if (isl_upoly_is_cst(up))
748 return isl_upoly_cst_mul_isl_int(up, v);
750 up = isl_upoly_cow(up);
751 rec = isl_upoly_as_rec(up);
752 if (!rec)
753 goto error;
755 for (i = 0; i < rec->n; ++i) {
756 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
757 if (!rec->p[i])
758 goto error;
761 return up;
762 error:
763 isl_upoly_free(up);
764 return NULL;
767 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
768 __isl_take struct isl_upoly *up2)
770 struct isl_upoly_cst *cst1;
771 struct isl_upoly_cst *cst2;
773 up1 = isl_upoly_cow(up1);
774 if (!up1 || !up2)
775 goto error;
777 cst1 = isl_upoly_as_cst(up1);
778 cst2 = isl_upoly_as_cst(up2);
780 isl_int_mul(cst1->n, cst1->n, cst2->n);
781 isl_int_mul(cst1->d, cst1->d, cst2->d);
783 isl_upoly_cst_reduce(cst1);
785 isl_upoly_free(up2);
786 return up1;
787 error:
788 isl_upoly_free(up1);
789 isl_upoly_free(up2);
790 return NULL;
793 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
794 __isl_take struct isl_upoly *up2)
796 struct isl_upoly_rec *rec1;
797 struct isl_upoly_rec *rec2;
798 struct isl_upoly_rec *res;
799 int i, j;
800 int size;
802 rec1 = isl_upoly_as_rec(up1);
803 rec2 = isl_upoly_as_rec(up2);
804 if (!rec1 || !rec2)
805 goto error;
806 size = rec1->n + rec2->n - 1;
807 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
808 if (!res)
809 goto error;
811 for (i = 0; i < rec1->n; ++i) {
812 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
813 isl_upoly_copy(rec1->p[i]));
814 if (!res->p[i])
815 goto error;
816 res->n++;
818 for (; i < size; ++i) {
819 res->p[i] = isl_upoly_zero(up1->ctx);
820 if (!res->p[i])
821 goto error;
822 res->n++;
824 for (i = 0; i < rec1->n; ++i) {
825 for (j = 1; j < rec2->n; ++j) {
826 struct isl_upoly *up;
827 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
828 isl_upoly_copy(rec1->p[i]));
829 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
830 if (!res->p[i + j])
831 goto error;
835 isl_upoly_free(up1);
836 isl_upoly_free(up2);
838 return &res->up;
839 error:
840 isl_upoly_free(up1);
841 isl_upoly_free(up2);
842 isl_upoly_free(&res->up);
843 return NULL;
846 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
847 __isl_take struct isl_upoly *up2)
849 if (!up1 || !up2)
850 goto error;
852 if (isl_upoly_is_nan(up1)) {
853 isl_upoly_free(up2);
854 return up1;
857 if (isl_upoly_is_nan(up2)) {
858 isl_upoly_free(up1);
859 return up2;
862 if (isl_upoly_is_zero(up1)) {
863 isl_upoly_free(up2);
864 return up1;
867 if (isl_upoly_is_zero(up2)) {
868 isl_upoly_free(up1);
869 return up2;
872 if (isl_upoly_is_one(up1)) {
873 isl_upoly_free(up1);
874 return up2;
877 if (isl_upoly_is_one(up2)) {
878 isl_upoly_free(up2);
879 return up1;
882 if (up1->var < up2->var)
883 return isl_upoly_mul(up2, up1);
885 if (up2->var < up1->var) {
886 int i;
887 struct isl_upoly_rec *rec;
888 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
889 isl_ctx *ctx = up1->ctx;
890 isl_upoly_free(up1);
891 isl_upoly_free(up2);
892 return isl_upoly_nan(ctx);
894 up1 = isl_upoly_cow(up1);
895 rec = isl_upoly_as_rec(up1);
896 if (!rec)
897 goto error;
899 for (i = 0; i < rec->n; ++i) {
900 rec->p[i] = isl_upoly_mul(rec->p[i],
901 isl_upoly_copy(up2));
902 if (!rec->p[i])
903 goto error;
905 isl_upoly_free(up2);
906 return up1;
909 if (isl_upoly_is_cst(up1))
910 return isl_upoly_mul_cst(up1, up2);
912 return isl_upoly_mul_rec(up1, up2);
913 error:
914 isl_upoly_free(up1);
915 isl_upoly_free(up2);
916 return NULL;
919 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
920 unsigned power)
922 struct isl_upoly *res;
924 if (!up)
925 return NULL;
926 if (power == 1)
927 return up;
929 if (power % 2)
930 res = isl_upoly_copy(up);
931 else
932 res = isl_upoly_one(up->ctx);
934 while (power >>= 1) {
935 up = isl_upoly_mul(up, isl_upoly_copy(up));
936 if (power % 2)
937 res = isl_upoly_mul(res, isl_upoly_copy(up));
940 isl_upoly_free(up);
941 return res;
944 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
945 unsigned n_div, __isl_take struct isl_upoly *up)
947 struct isl_qpolynomial *qp = NULL;
948 unsigned total;
950 if (!dim || !up)
951 goto error;
953 total = isl_dim_total(dim);
955 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
956 if (!qp)
957 goto error;
959 qp->ref = 1;
960 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
961 if (!qp->div)
962 goto error;
964 qp->dim = dim;
965 qp->upoly = up;
967 return qp;
968 error:
969 isl_dim_free(dim);
970 isl_upoly_free(up);
971 isl_qpolynomial_free(qp);
972 return NULL;
975 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
977 if (!qp)
978 return NULL;
980 qp->ref++;
981 return qp;
984 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
986 struct isl_qpolynomial *dup;
988 if (!qp)
989 return NULL;
991 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
992 isl_upoly_copy(qp->upoly));
993 if (!dup)
994 return NULL;
995 isl_mat_free(dup->div);
996 dup->div = isl_mat_copy(qp->div);
997 if (!dup->div)
998 goto error;
1000 return dup;
1001 error:
1002 isl_qpolynomial_free(dup);
1003 return NULL;
1006 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1008 if (!qp)
1009 return NULL;
1011 if (qp->ref == 1)
1012 return qp;
1013 qp->ref--;
1014 return isl_qpolynomial_dup(qp);
1017 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1019 if (!qp)
1020 return;
1022 if (--qp->ref > 0)
1023 return;
1025 isl_dim_free(qp->dim);
1026 isl_mat_free(qp->div);
1027 isl_upoly_free(qp->upoly);
1029 free(qp);
1032 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1034 int i;
1035 struct isl_upoly *up;
1036 struct isl_upoly_rec *rec;
1037 struct isl_upoly_cst *cst;
1039 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1040 if (!rec)
1041 return NULL;
1042 for (i = 0; i < 1 + power; ++i) {
1043 rec->p[i] = isl_upoly_zero(ctx);
1044 if (!rec->p[i])
1045 goto error;
1046 rec->n++;
1048 cst = isl_upoly_as_cst(rec->p[power]);
1049 isl_int_set_si(cst->n, 1);
1051 return &rec->up;
1052 error:
1053 isl_upoly_free(&rec->up);
1054 return NULL;
1057 /* r array maps original positions to new positions.
1059 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1060 int *r)
1062 int i;
1063 struct isl_upoly_rec *rec;
1064 struct isl_upoly *base;
1065 struct isl_upoly *res;
1067 if (isl_upoly_is_cst(up))
1068 return up;
1070 rec = isl_upoly_as_rec(up);
1071 if (!rec)
1072 goto error;
1074 isl_assert(up->ctx, rec->n >= 1, goto error);
1076 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1077 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1079 for (i = rec->n - 2; i >= 0; --i) {
1080 res = isl_upoly_mul(res, isl_upoly_copy(base));
1081 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1084 isl_upoly_free(base);
1085 isl_upoly_free(up);
1087 return res;
1088 error:
1089 isl_upoly_free(up);
1090 return NULL;
1093 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1095 int n_row, n_col;
1096 int equal;
1098 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1099 div1->n_col >= div2->n_col, return -1);
1101 if (div1->n_row == div2->n_row)
1102 return isl_mat_is_equal(div1, div2);
1104 n_row = div1->n_row;
1105 n_col = div1->n_col;
1106 div1->n_row = div2->n_row;
1107 div1->n_col = div2->n_col;
1109 equal = isl_mat_is_equal(div1, div2);
1111 div1->n_row = n_row;
1112 div1->n_col = n_col;
1114 return equal;
1117 static void expand_row(__isl_keep isl_mat *dst, int d,
1118 __isl_keep isl_mat *src, int s, int *exp)
1120 int i;
1121 unsigned c = src->n_col - src->n_row;
1123 isl_seq_cpy(dst->row[d], src->row[s], c);
1124 isl_seq_clr(dst->row[d] + c, dst->n_col - c);
1126 for (i = 0; i < s; ++i)
1127 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
1130 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1132 int li, lj;
1134 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1135 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1137 if (li != lj)
1138 return li - lj;
1140 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1143 struct isl_div_sort_info {
1144 isl_mat *div;
1145 int row;
1148 static int div_sort_cmp(const void *p1, const void *p2)
1150 const struct isl_div_sort_info *i1, *i2;
1151 i1 = (const struct isl_div_sort_info *) p1;
1152 i2 = (const struct isl_div_sort_info *) p2;
1154 return cmp_row(i1->div, i1->row, i2->row);
1157 /* Sort divs and remove duplicates.
1159 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1161 int i;
1162 int skip;
1163 int len;
1164 struct isl_div_sort_info *array = NULL;
1165 int *pos = NULL, *at = NULL;
1166 int *reordering = NULL;
1167 unsigned div_pos;
1169 if (!qp)
1170 return NULL;
1171 if (qp->div->n_row <= 1)
1172 return qp;
1174 div_pos = isl_dim_total(qp->dim);
1176 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1177 qp->div->n_row);
1178 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1179 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1180 len = qp->div->n_col - 2;
1181 reordering = isl_alloc_array(qp->div->ctx, int, len);
1182 if (!array || !pos || !at || !reordering)
1183 goto error;
1185 for (i = 0; i < qp->div->n_row; ++i) {
1186 array[i].div = qp->div;
1187 array[i].row = i;
1188 pos[i] = i;
1189 at[i] = i;
1192 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1193 div_sort_cmp);
1195 for (i = 0; i < div_pos; ++i)
1196 reordering[i] = i;
1198 for (i = 0; i < qp->div->n_row; ++i) {
1199 if (pos[array[i].row] == i)
1200 continue;
1201 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1202 pos[at[i]] = pos[array[i].row];
1203 at[pos[array[i].row]] = at[i];
1204 at[i] = array[i].row;
1205 pos[array[i].row] = i;
1208 skip = 0;
1209 for (i = 0; i < len - div_pos; ++i) {
1210 if (i > 0 &&
1211 isl_seq_eq(qp->div->row[i - skip - 1],
1212 qp->div->row[i - skip], qp->div->n_col)) {
1213 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1214 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1215 2 + div_pos + i - skip);
1216 qp->div = isl_mat_drop_cols(qp->div,
1217 2 + div_pos + i - skip, 1);
1218 skip++;
1220 reordering[div_pos + array[i].row] = div_pos + i - skip;
1223 qp->upoly = reorder(qp->upoly, reordering);
1225 if (!qp->upoly || !qp->div)
1226 goto error;
1228 free(at);
1229 free(pos);
1230 free(array);
1231 free(reordering);
1233 return qp;
1234 error:
1235 free(at);
1236 free(pos);
1237 free(array);
1238 free(reordering);
1239 isl_qpolynomial_free(qp);
1240 return NULL;
1243 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1244 __isl_keep isl_mat *div2, int *exp1, int *exp2)
1246 int i, j, k;
1247 isl_mat *div = NULL;
1248 unsigned d = div1->n_col - div1->n_row;
1250 div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1251 d + div1->n_row + div2->n_row);
1252 if (!div)
1253 return NULL;
1255 for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1256 int cmp;
1258 expand_row(div, k, div1, i, exp1);
1259 expand_row(div, k + 1, div2, j, exp2);
1261 cmp = cmp_row(div, k, k + 1);
1262 if (cmp == 0) {
1263 exp1[i++] = k;
1264 exp2[j++] = k;
1265 } else if (cmp < 0) {
1266 exp1[i++] = k;
1267 } else {
1268 exp2[j++] = k;
1269 isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1272 for (; i < div1->n_row; ++i, ++k) {
1273 expand_row(div, k, div1, i, exp1);
1274 exp1[i] = k;
1276 for (; j < div2->n_row; ++j, ++k) {
1277 expand_row(div, k, div2, j, exp2);
1278 exp2[j] = k;
1281 div->n_row = k;
1282 div->n_col = d + k;
1284 return div;
1287 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1288 int *exp, int first)
1290 int i;
1291 struct isl_upoly_rec *rec;
1293 if (isl_upoly_is_cst(up))
1294 return up;
1296 if (up->var < first)
1297 return up;
1299 if (exp[up->var - first] == up->var - first)
1300 return up;
1302 up = isl_upoly_cow(up);
1303 if (!up)
1304 goto error;
1306 up->var = exp[up->var - first] + first;
1308 rec = isl_upoly_as_rec(up);
1309 if (!rec)
1310 goto error;
1312 for (i = 0; i < rec->n; ++i) {
1313 rec->p[i] = expand(rec->p[i], exp, first);
1314 if (!rec->p[i])
1315 goto error;
1318 return up;
1319 error:
1320 isl_upoly_free(up);
1321 return NULL;
1324 static __isl_give isl_qpolynomial *with_merged_divs(
1325 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1326 __isl_take isl_qpolynomial *qp2),
1327 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1329 int *exp1 = NULL;
1330 int *exp2 = NULL;
1331 isl_mat *div = NULL;
1333 qp1 = isl_qpolynomial_cow(qp1);
1334 qp2 = isl_qpolynomial_cow(qp2);
1336 if (!qp1 || !qp2)
1337 goto error;
1339 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1340 qp1->div->n_col >= qp2->div->n_col, goto error);
1342 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1343 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1344 if (!exp1 || !exp2)
1345 goto error;
1347 div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1348 if (!div)
1349 goto error;
1351 isl_mat_free(qp1->div);
1352 qp1->div = isl_mat_copy(div);
1353 isl_mat_free(qp2->div);
1354 qp2->div = isl_mat_copy(div);
1356 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1357 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1359 if (!qp1->upoly || !qp2->upoly)
1360 goto error;
1362 isl_mat_free(div);
1363 free(exp1);
1364 free(exp2);
1366 return fn(qp1, qp2);
1367 error:
1368 isl_mat_free(div);
1369 free(exp1);
1370 free(exp2);
1371 isl_qpolynomial_free(qp1);
1372 isl_qpolynomial_free(qp2);
1373 return NULL;
1376 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1377 __isl_take isl_qpolynomial *qp2)
1379 qp1 = isl_qpolynomial_cow(qp1);
1381 if (!qp1 || !qp2)
1382 goto error;
1384 if (qp1->div->n_row < qp2->div->n_row)
1385 return isl_qpolynomial_add(qp2, qp1);
1387 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1388 if (!compatible_divs(qp1->div, qp2->div))
1389 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1391 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1392 if (!qp1->upoly)
1393 goto error;
1395 isl_qpolynomial_free(qp2);
1397 return qp1;
1398 error:
1399 isl_qpolynomial_free(qp1);
1400 isl_qpolynomial_free(qp2);
1401 return NULL;
1404 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1405 __isl_keep isl_set *dom,
1406 __isl_take isl_qpolynomial *qp1,
1407 __isl_take isl_qpolynomial *qp2)
1409 qp1 = isl_qpolynomial_add(qp1, qp2);
1410 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1411 return qp1;
1414 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1415 __isl_take isl_qpolynomial *qp2)
1417 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1420 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1421 __isl_take isl_qpolynomial *qp, isl_int v)
1423 if (isl_int_is_zero(v))
1424 return qp;
1426 qp = isl_qpolynomial_cow(qp);
1427 if (!qp)
1428 return NULL;
1430 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1431 if (!qp->upoly)
1432 goto error;
1434 return qp;
1435 error:
1436 isl_qpolynomial_free(qp);
1437 return NULL;
1441 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1443 if (!qp)
1444 return NULL;
1446 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1449 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1450 __isl_take isl_qpolynomial *qp, isl_int v)
1452 if (isl_int_is_one(v))
1453 return qp;
1455 if (qp && isl_int_is_zero(v)) {
1456 isl_qpolynomial *zero;
1457 zero = isl_qpolynomial_zero(isl_dim_copy(qp->dim));
1458 isl_qpolynomial_free(qp);
1459 return zero;
1462 qp = isl_qpolynomial_cow(qp);
1463 if (!qp)
1464 return NULL;
1466 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1467 if (!qp->upoly)
1468 goto error;
1470 return qp;
1471 error:
1472 isl_qpolynomial_free(qp);
1473 return NULL;
1476 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1477 __isl_take isl_qpolynomial *qp2)
1479 qp1 = isl_qpolynomial_cow(qp1);
1481 if (!qp1 || !qp2)
1482 goto error;
1484 if (qp1->div->n_row < qp2->div->n_row)
1485 return isl_qpolynomial_mul(qp2, qp1);
1487 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1488 if (!compatible_divs(qp1->div, qp2->div))
1489 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1491 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1492 if (!qp1->upoly)
1493 goto error;
1495 isl_qpolynomial_free(qp2);
1497 return qp1;
1498 error:
1499 isl_qpolynomial_free(qp1);
1500 isl_qpolynomial_free(qp2);
1501 return NULL;
1504 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1505 unsigned power)
1507 qp = isl_qpolynomial_cow(qp);
1509 if (!qp)
1510 return NULL;
1512 qp->upoly = isl_upoly_pow(qp->upoly, power);
1513 if (!qp->upoly)
1514 goto error;
1516 return qp;
1517 error:
1518 isl_qpolynomial_free(qp);
1519 return NULL;
1522 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1524 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1527 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1529 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1532 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1534 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1537 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1539 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1542 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1544 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1547 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1548 isl_int v)
1550 struct isl_qpolynomial *qp;
1551 struct isl_upoly_cst *cst;
1553 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1554 if (!qp)
1555 return NULL;
1557 cst = isl_upoly_as_cst(qp->upoly);
1558 isl_int_set(cst->n, v);
1560 return qp;
1563 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1564 isl_int *n, isl_int *d)
1566 struct isl_upoly_cst *cst;
1568 if (!qp)
1569 return -1;
1571 if (!isl_upoly_is_cst(qp->upoly))
1572 return 0;
1574 cst = isl_upoly_as_cst(qp->upoly);
1575 if (!cst)
1576 return -1;
1578 if (n)
1579 isl_int_set(*n, cst->n);
1580 if (d)
1581 isl_int_set(*d, cst->d);
1583 return 1;
1586 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1588 int is_cst;
1589 struct isl_upoly_rec *rec;
1591 if (!up)
1592 return -1;
1594 if (up->var < 0)
1595 return 1;
1597 rec = isl_upoly_as_rec(up);
1598 if (!rec)
1599 return -1;
1601 if (rec->n > 2)
1602 return 0;
1604 isl_assert(up->ctx, rec->n > 1, return -1);
1606 is_cst = isl_upoly_is_cst(rec->p[1]);
1607 if (is_cst < 0)
1608 return -1;
1609 if (!is_cst)
1610 return 0;
1612 return isl_upoly_is_affine(rec->p[0]);
1615 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1617 if (!qp)
1618 return -1;
1620 if (qp->div->n_row > 0)
1621 return 0;
1623 return isl_upoly_is_affine(qp->upoly);
1626 static void update_coeff(__isl_keep isl_vec *aff,
1627 __isl_keep struct isl_upoly_cst *cst, int pos)
1629 isl_int gcd;
1630 isl_int f;
1632 if (isl_int_is_zero(cst->n))
1633 return;
1635 isl_int_init(gcd);
1636 isl_int_init(f);
1637 isl_int_gcd(gcd, cst->d, aff->el[0]);
1638 isl_int_divexact(f, cst->d, gcd);
1639 isl_int_divexact(gcd, aff->el[0], gcd);
1640 isl_seq_scale(aff->el, aff->el, f, aff->size);
1641 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1642 isl_int_clear(gcd);
1643 isl_int_clear(f);
1646 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1647 __isl_keep isl_vec *aff)
1649 struct isl_upoly_cst *cst;
1650 struct isl_upoly_rec *rec;
1652 if (!up || !aff)
1653 return -1;
1655 if (up->var < 0) {
1656 struct isl_upoly_cst *cst;
1658 cst = isl_upoly_as_cst(up);
1659 if (!cst)
1660 return -1;
1661 update_coeff(aff, cst, 0);
1662 return 0;
1665 rec = isl_upoly_as_rec(up);
1666 if (!rec)
1667 return -1;
1668 isl_assert(up->ctx, rec->n == 2, return -1);
1670 cst = isl_upoly_as_cst(rec->p[1]);
1671 if (!cst)
1672 return -1;
1673 update_coeff(aff, cst, 1 + up->var);
1675 return isl_upoly_update_affine(rec->p[0], aff);
1678 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1679 __isl_keep isl_qpolynomial *qp)
1681 isl_vec *aff;
1682 unsigned d;
1684 if (!qp)
1685 return NULL;
1687 d = isl_dim_total(qp->dim);
1688 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1689 if (!aff)
1690 return NULL;
1692 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1693 isl_int_set_si(aff->el[0], 1);
1695 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1696 goto error;
1698 return aff;
1699 error:
1700 isl_vec_free(aff);
1701 return NULL;
1704 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1705 __isl_keep isl_qpolynomial *qp2)
1707 if (!qp1 || !qp2)
1708 return -1;
1710 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1713 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1715 int i;
1716 struct isl_upoly_rec *rec;
1718 if (isl_upoly_is_cst(up)) {
1719 struct isl_upoly_cst *cst;
1720 cst = isl_upoly_as_cst(up);
1721 if (!cst)
1722 return;
1723 isl_int_lcm(*d, *d, cst->d);
1724 return;
1727 rec = isl_upoly_as_rec(up);
1728 if (!rec)
1729 return;
1731 for (i = 0; i < rec->n; ++i)
1732 upoly_update_den(rec->p[i], d);
1735 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1737 isl_int_set_si(*d, 1);
1738 if (!qp)
1739 return;
1740 upoly_update_den(qp->upoly, d);
1743 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1744 int pos, int power)
1746 struct isl_ctx *ctx;
1748 if (!dim)
1749 return NULL;
1751 ctx = dim->ctx;
1753 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1756 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1757 enum isl_dim_type type, unsigned pos)
1759 if (!dim)
1760 return NULL;
1762 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1763 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1765 if (type == isl_dim_set)
1766 pos += isl_dim_size(dim, isl_dim_param);
1768 return isl_qpolynomial_var_pow(dim, pos, 1);
1769 error:
1770 isl_dim_free(dim);
1771 return NULL;
1774 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1775 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1777 int i;
1778 struct isl_upoly_rec *rec;
1779 struct isl_upoly *base, *res;
1781 if (!up)
1782 return NULL;
1784 if (isl_upoly_is_cst(up))
1785 return up;
1787 if (up->var < first)
1788 return up;
1790 rec = isl_upoly_as_rec(up);
1791 if (!rec)
1792 goto error;
1794 isl_assert(up->ctx, rec->n >= 1, goto error);
1796 if (up->var >= first + n)
1797 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1798 else
1799 base = isl_upoly_copy(subs[up->var - first]);
1801 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1802 for (i = rec->n - 2; i >= 0; --i) {
1803 struct isl_upoly *t;
1804 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1805 res = isl_upoly_mul(res, isl_upoly_copy(base));
1806 res = isl_upoly_sum(res, t);
1809 isl_upoly_free(base);
1810 isl_upoly_free(up);
1812 return res;
1813 error:
1814 isl_upoly_free(up);
1815 return NULL;
1818 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1819 isl_int denom, unsigned len)
1821 int i;
1822 struct isl_upoly *up;
1824 isl_assert(ctx, len >= 1, return NULL);
1826 up = isl_upoly_rat_cst(ctx, f[0], denom);
1827 for (i = 0; i < len - 1; ++i) {
1828 struct isl_upoly *t;
1829 struct isl_upoly *c;
1831 if (isl_int_is_zero(f[1 + i]))
1832 continue;
1834 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1835 t = isl_upoly_var_pow(ctx, i, 1);
1836 t = isl_upoly_mul(c, t);
1837 up = isl_upoly_sum(up, t);
1840 return up;
1843 /* Remove common factor of non-constant terms and denominator.
1845 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1847 isl_ctx *ctx = qp->div->ctx;
1848 unsigned total = qp->div->n_col - 2;
1850 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1851 isl_int_gcd(ctx->normalize_gcd,
1852 ctx->normalize_gcd, qp->div->row[div][0]);
1853 if (isl_int_is_one(ctx->normalize_gcd))
1854 return;
1856 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1857 ctx->normalize_gcd, total);
1858 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1859 ctx->normalize_gcd);
1860 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1861 ctx->normalize_gcd);
1864 /* Replace the integer division identified by "div" by the polynomial "s".
1865 * The integer division is assumed not to appear in the definition
1866 * of any other integer divisions.
1868 static __isl_give isl_qpolynomial *substitute_div(
1869 __isl_take isl_qpolynomial *qp,
1870 int div, __isl_take struct isl_upoly *s)
1872 int i;
1873 int total;
1874 int *reordering;
1876 if (!qp || !s)
1877 goto error;
1879 qp = isl_qpolynomial_cow(qp);
1880 if (!qp)
1881 goto error;
1883 total = isl_dim_total(qp->dim);
1884 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1885 if (!qp->upoly)
1886 goto error;
1888 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1889 if (!reordering)
1890 goto error;
1891 for (i = 0; i < total + div; ++i)
1892 reordering[i] = i;
1893 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1894 reordering[i] = i - 1;
1895 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1896 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1897 qp->upoly = reorder(qp->upoly, reordering);
1898 free(reordering);
1900 if (!qp->upoly || !qp->div)
1901 goto error;
1903 isl_upoly_free(s);
1904 return qp;
1905 error:
1906 isl_qpolynomial_free(qp);
1907 isl_upoly_free(s);
1908 return NULL;
1911 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1912 * divisions because d is equal to 1 by their definition, i.e., e.
1914 static __isl_give isl_qpolynomial *substitute_non_divs(
1915 __isl_take isl_qpolynomial *qp)
1917 int i, j;
1918 int total;
1919 struct isl_upoly *s;
1921 if (!qp)
1922 return NULL;
1924 total = isl_dim_total(qp->dim);
1925 for (i = 0; qp && i < qp->div->n_row; ++i) {
1926 if (!isl_int_is_one(qp->div->row[i][0]))
1927 continue;
1928 for (j = i + 1; j < qp->div->n_row; ++j) {
1929 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1930 continue;
1931 isl_seq_combine(qp->div->row[j] + 1,
1932 qp->div->ctx->one, qp->div->row[j] + 1,
1933 qp->div->row[j][2 + total + i],
1934 qp->div->row[i] + 1, 1 + total + i);
1935 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1936 normalize_div(qp, j);
1938 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1939 qp->div->row[i][0], qp->div->n_col - 1);
1940 qp = substitute_div(qp, i, s);
1941 --i;
1944 return qp;
1947 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1948 * with d the denominator. When replacing the coefficient e of x by
1949 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1950 * inside the division, so we need to add floor(e/d) * x outside.
1951 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1952 * to adjust the coefficient of x in each later div that depends on the
1953 * current div "div" and also in the affine expression "aff"
1954 * (if it too depends on "div").
1956 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1957 __isl_keep isl_vec *aff)
1959 int i, j;
1960 isl_int v;
1961 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1963 isl_int_init(v);
1964 for (i = 0; i < 1 + total + div; ++i) {
1965 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1966 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1967 continue;
1968 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1969 isl_int_fdiv_r(qp->div->row[div][1 + i],
1970 qp->div->row[div][1 + i], qp->div->row[div][0]);
1971 if (!isl_int_is_zero(aff->el[1 + total + div]))
1972 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1973 for (j = div + 1; j < qp->div->n_row; ++j) {
1974 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1975 continue;
1976 isl_int_addmul(qp->div->row[j][1 + i],
1977 v, qp->div->row[j][2 + total + div]);
1980 isl_int_clear(v);
1983 /* Check if the last non-zero coefficient is bigger that half of the
1984 * denominator. If so, we will invert the div to further reduce the number
1985 * of distinct divs that may appear.
1986 * If the last non-zero coefficient is exactly half the denominator,
1987 * then we continue looking for earlier coefficients that are bigger
1988 * than half the denominator.
1990 static int needs_invert(__isl_keep isl_mat *div, int row)
1992 int i;
1993 int cmp;
1995 for (i = div->n_col - 1; i >= 1; --i) {
1996 if (isl_int_is_zero(div->row[row][i]))
1997 continue;
1998 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1999 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2000 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2001 if (cmp)
2002 return cmp > 0;
2003 if (i == 1)
2004 return 1;
2007 return 0;
2010 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2011 * We only invert the coefficients of e (and the coefficient of q in
2012 * later divs and in "aff"). After calling this function, the
2013 * coefficients of e should be reduced again.
2015 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2016 __isl_keep isl_vec *aff)
2018 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2020 isl_seq_neg(qp->div->row[div] + 1,
2021 qp->div->row[div] + 1, qp->div->n_col - 1);
2022 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2023 isl_int_add(qp->div->row[div][1],
2024 qp->div->row[div][1], qp->div->row[div][0]);
2025 if (!isl_int_is_zero(aff->el[1 + total + div]))
2026 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2027 isl_mat_col_mul(qp->div, 2 + total + div,
2028 qp->div->ctx->negone, 2 + total + div);
2031 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2032 * in the interval [0, d-1], with d the denominator and such that the
2033 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2035 * After the reduction, some divs may have become redundant or identical,
2036 * so we call substitute_non_divs and sort_divs. If these functions
2037 * eliminate divs of merge * two or more divs into one, the coefficients
2038 * of the enclosing divs may have to be reduced again, so we call
2039 * ourselves recursively if the number of divs decreases.
2041 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2043 int i, j;
2044 isl_vec *aff = NULL;
2045 struct isl_upoly *s;
2046 unsigned n_div;
2048 if (!qp)
2049 return NULL;
2051 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2052 aff = isl_vec_clr(aff);
2053 if (!aff)
2054 goto error;
2056 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2058 for (i = 0; i < qp->div->n_row; ++i) {
2059 normalize_div(qp, i);
2060 reduce_div(qp, i, aff);
2061 if (needs_invert(qp->div, i)) {
2062 invert_div(qp, i, aff);
2063 reduce_div(qp, i, aff);
2067 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2068 qp->div->ctx->one, aff->size);
2069 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2070 isl_upoly_free(s);
2071 if (!qp->upoly)
2072 goto error;
2074 isl_vec_free(aff);
2076 n_div = qp->div->n_row;
2077 qp = substitute_non_divs(qp);
2078 qp = sort_divs(qp);
2079 if (qp && qp->div->n_row < n_div)
2080 return reduce_divs(qp);
2082 return qp;
2083 error:
2084 isl_qpolynomial_free(qp);
2085 isl_vec_free(aff);
2086 return NULL;
2089 /* Assumes each div only depends on earlier divs.
2091 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2092 int power)
2094 struct isl_qpolynomial *qp = NULL;
2095 struct isl_upoly_rec *rec;
2096 struct isl_upoly_cst *cst;
2097 int i, d;
2098 int pos;
2100 if (!div)
2101 return NULL;
2103 d = div->line - div->bmap->div;
2105 pos = isl_dim_total(div->bmap->dim) + d;
2106 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2107 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2108 div->bmap->n_div, &rec->up);
2109 if (!qp)
2110 goto error;
2112 for (i = 0; i < div->bmap->n_div; ++i)
2113 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2115 for (i = 0; i < 1 + power; ++i) {
2116 rec->p[i] = isl_upoly_zero(div->ctx);
2117 if (!rec->p[i])
2118 goto error;
2119 rec->n++;
2121 cst = isl_upoly_as_cst(rec->p[power]);
2122 isl_int_set_si(cst->n, 1);
2124 isl_div_free(div);
2126 qp = reduce_divs(qp);
2128 return qp;
2129 error:
2130 isl_qpolynomial_free(qp);
2131 isl_div_free(div);
2132 return NULL;
2135 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2137 return isl_qpolynomial_div_pow(div, 1);
2140 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2141 const isl_int n, const isl_int d)
2143 struct isl_qpolynomial *qp;
2144 struct isl_upoly_cst *cst;
2146 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2147 if (!qp)
2148 return NULL;
2150 cst = isl_upoly_as_cst(qp->upoly);
2151 isl_int_set(cst->n, n);
2152 isl_int_set(cst->d, d);
2154 return qp;
2157 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2159 struct isl_upoly_rec *rec;
2160 int i;
2162 if (!up)
2163 return -1;
2165 if (isl_upoly_is_cst(up))
2166 return 0;
2168 if (up->var < d)
2169 active[up->var] = 1;
2171 rec = isl_upoly_as_rec(up);
2172 for (i = 0; i < rec->n; ++i)
2173 if (up_set_active(rec->p[i], active, d) < 0)
2174 return -1;
2176 return 0;
2179 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2181 int i, j;
2182 int d = isl_dim_total(qp->dim);
2184 if (!qp || !active)
2185 return -1;
2187 for (i = 0; i < d; ++i)
2188 for (j = 0; j < qp->div->n_row; ++j) {
2189 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2190 continue;
2191 active[i] = 1;
2192 break;
2195 return up_set_active(qp->upoly, active, d);
2198 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2199 enum isl_dim_type type, unsigned first, unsigned n)
2201 int i;
2202 int *active = NULL;
2203 int involves = 0;
2205 if (!qp)
2206 return -1;
2207 if (n == 0)
2208 return 0;
2210 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2211 return -1);
2212 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2213 type == isl_dim_set, return -1);
2215 active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
2216 if (set_active(qp, active) < 0)
2217 goto error;
2219 if (type == isl_dim_set)
2220 first += isl_dim_size(qp->dim, isl_dim_param);
2221 for (i = 0; i < n; ++i)
2222 if (active[first + i]) {
2223 involves = 1;
2224 break;
2227 free(active);
2229 return involves;
2230 error:
2231 free(active);
2232 return -1;
2235 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2236 unsigned first, unsigned n)
2238 int i;
2239 struct isl_upoly_rec *rec;
2241 if (!up)
2242 return NULL;
2243 if (n == 0 || up->var < 0 || up->var < first)
2244 return up;
2245 if (up->var < first + n) {
2246 up = replace_by_constant_term(up);
2247 return isl_upoly_drop(up, first, n);
2249 up = isl_upoly_cow(up);
2250 if (!up)
2251 return NULL;
2252 up->var -= n;
2253 rec = isl_upoly_as_rec(up);
2254 if (!rec)
2255 goto error;
2257 for (i = 0; i < rec->n; ++i) {
2258 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2259 if (!rec->p[i])
2260 goto error;
2263 return up;
2264 error:
2265 isl_upoly_free(up);
2266 return NULL;
2269 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2270 __isl_take isl_qpolynomial *qp,
2271 enum isl_dim_type type, unsigned pos, const char *s)
2273 qp = isl_qpolynomial_cow(qp);
2274 if (!qp)
2275 return NULL;
2276 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2277 if (!qp->dim)
2278 goto error;
2279 return qp;
2280 error:
2281 isl_qpolynomial_free(qp);
2282 return NULL;
2285 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2286 __isl_take isl_qpolynomial *qp,
2287 enum isl_dim_type type, unsigned first, unsigned n)
2289 if (!qp)
2290 return NULL;
2291 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
2292 return qp;
2294 qp = isl_qpolynomial_cow(qp);
2295 if (!qp)
2296 return NULL;
2298 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2299 goto error);
2300 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2301 type == isl_dim_set, goto error);
2303 qp->dim = isl_dim_drop(qp->dim, type, first, n);
2304 if (!qp->dim)
2305 goto error;
2307 if (type == isl_dim_set)
2308 first += isl_dim_size(qp->dim, isl_dim_param);
2310 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2311 if (!qp->div)
2312 goto error;
2314 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2315 if (!qp->upoly)
2316 goto error;
2318 return qp;
2319 error:
2320 isl_qpolynomial_free(qp);
2321 return NULL;
2324 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2325 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2327 int i, j, k;
2328 isl_int denom;
2329 unsigned total;
2330 unsigned n_div;
2331 struct isl_upoly *up;
2333 if (!eq)
2334 goto error;
2335 if (eq->n_eq == 0) {
2336 isl_basic_set_free(eq);
2337 return qp;
2340 qp = isl_qpolynomial_cow(qp);
2341 if (!qp)
2342 goto error;
2343 qp->div = isl_mat_cow(qp->div);
2344 if (!qp->div)
2345 goto error;
2347 total = 1 + isl_dim_total(eq->dim);
2348 n_div = eq->n_div;
2349 isl_int_init(denom);
2350 for (i = 0; i < eq->n_eq; ++i) {
2351 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2352 if (j < 0 || j == 0 || j >= total)
2353 continue;
2355 for (k = 0; k < qp->div->n_row; ++k) {
2356 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2357 continue;
2358 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2359 &qp->div->row[k][0]);
2360 normalize_div(qp, k);
2363 if (isl_int_is_pos(eq->eq[i][j]))
2364 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2365 isl_int_abs(denom, eq->eq[i][j]);
2366 isl_int_set_si(eq->eq[i][j], 0);
2368 up = isl_upoly_from_affine(qp->dim->ctx,
2369 eq->eq[i], denom, total);
2370 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2371 isl_upoly_free(up);
2373 isl_int_clear(denom);
2375 if (!qp->upoly)
2376 goto error;
2378 isl_basic_set_free(eq);
2380 qp = substitute_non_divs(qp);
2381 qp = sort_divs(qp);
2383 return qp;
2384 error:
2385 isl_basic_set_free(eq);
2386 isl_qpolynomial_free(qp);
2387 return NULL;
2390 static __isl_give isl_basic_set *add_div_constraints(
2391 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2393 int i;
2394 unsigned total;
2396 if (!bset || !div)
2397 goto error;
2399 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2400 if (!bset)
2401 goto error;
2402 total = isl_basic_set_total_dim(bset);
2403 for (i = 0; i < div->n_row; ++i)
2404 if (isl_basic_set_add_div_constraints_var(bset,
2405 total - div->n_row + i, div->row[i]) < 0)
2406 goto error;
2408 isl_mat_free(div);
2409 return bset;
2410 error:
2411 isl_mat_free(div);
2412 isl_basic_set_free(bset);
2413 return NULL;
2416 /* Look for equalities among the variables shared by context and qp
2417 * and the integer divisions of qp, if any.
2418 * The equalities are then used to eliminate variables and/or integer
2419 * divisions from qp.
2421 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2422 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2424 isl_basic_set *aff;
2426 if (!qp)
2427 goto error;
2428 if (qp->div->n_row > 0) {
2429 isl_basic_set *bset;
2430 context = isl_set_add_dims(context, isl_dim_set,
2431 qp->div->n_row);
2432 bset = isl_basic_set_universe(isl_set_get_dim(context));
2433 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2434 context = isl_set_intersect(context,
2435 isl_set_from_basic_set(bset));
2438 aff = isl_set_affine_hull(context);
2439 return isl_qpolynomial_substitute_equalities(qp, aff);
2440 error:
2441 isl_qpolynomial_free(qp);
2442 isl_set_free(context);
2443 return NULL;
2446 #undef PW
2447 #define PW isl_pw_qpolynomial
2448 #undef EL
2449 #define EL isl_qpolynomial
2450 #undef IS_ZERO
2451 #define IS_ZERO is_zero
2452 #undef FIELD
2453 #define FIELD qp
2455 #include <isl_pw_templ.c>
2457 #undef UNION
2458 #define UNION isl_union_pw_qpolynomial
2459 #undef PART
2460 #define PART isl_pw_qpolynomial
2461 #undef PARTS
2462 #define PARTS pw_qpolynomial
2464 #include <isl_union_templ.c>
2466 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2468 if (!pwqp)
2469 return -1;
2471 if (pwqp->n != -1)
2472 return 0;
2474 if (!isl_set_fast_is_universe(pwqp->p[0].set))
2475 return 0;
2477 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2480 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2481 __isl_take isl_pw_qpolynomial *pwqp1,
2482 __isl_take isl_pw_qpolynomial *pwqp2)
2484 int i, j, n;
2485 struct isl_pw_qpolynomial *res;
2486 isl_set *set;
2488 if (!pwqp1 || !pwqp2)
2489 goto error;
2491 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2492 goto error);
2494 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2495 isl_pw_qpolynomial_free(pwqp2);
2496 return pwqp1;
2499 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2500 isl_pw_qpolynomial_free(pwqp1);
2501 return pwqp2;
2504 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2505 isl_pw_qpolynomial_free(pwqp1);
2506 return pwqp2;
2509 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2510 isl_pw_qpolynomial_free(pwqp2);
2511 return pwqp1;
2514 n = pwqp1->n * pwqp2->n;
2515 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2517 for (i = 0; i < pwqp1->n; ++i) {
2518 for (j = 0; j < pwqp2->n; ++j) {
2519 struct isl_set *common;
2520 struct isl_qpolynomial *prod;
2521 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2522 isl_set_copy(pwqp2->p[j].set));
2523 if (isl_set_fast_is_empty(common)) {
2524 isl_set_free(common);
2525 continue;
2528 prod = isl_qpolynomial_mul(
2529 isl_qpolynomial_copy(pwqp1->p[i].qp),
2530 isl_qpolynomial_copy(pwqp2->p[j].qp));
2532 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2536 isl_pw_qpolynomial_free(pwqp1);
2537 isl_pw_qpolynomial_free(pwqp2);
2539 return res;
2540 error:
2541 isl_pw_qpolynomial_free(pwqp1);
2542 isl_pw_qpolynomial_free(pwqp2);
2543 return NULL;
2546 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2547 __isl_take isl_pw_qpolynomial *pwqp)
2549 int i;
2551 if (!pwqp)
2552 return NULL;
2554 if (isl_pw_qpolynomial_is_zero(pwqp))
2555 return pwqp;
2557 pwqp = isl_pw_qpolynomial_cow(pwqp);
2558 if (!pwqp)
2559 return NULL;
2561 for (i = 0; i < pwqp->n; ++i) {
2562 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2563 if (!pwqp->p[i].qp)
2564 goto error;
2567 return pwqp;
2568 error:
2569 isl_pw_qpolynomial_free(pwqp);
2570 return NULL;
2573 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2574 __isl_take isl_pw_qpolynomial *pwqp1,
2575 __isl_take isl_pw_qpolynomial *pwqp2)
2577 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2580 __isl_give struct isl_upoly *isl_upoly_eval(
2581 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2583 int i;
2584 struct isl_upoly_rec *rec;
2585 struct isl_upoly *res;
2586 struct isl_upoly *base;
2588 if (isl_upoly_is_cst(up)) {
2589 isl_vec_free(vec);
2590 return up;
2593 rec = isl_upoly_as_rec(up);
2594 if (!rec)
2595 goto error;
2597 isl_assert(up->ctx, rec->n >= 1, goto error);
2599 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2601 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2602 isl_vec_copy(vec));
2604 for (i = rec->n - 2; i >= 0; --i) {
2605 res = isl_upoly_mul(res, isl_upoly_copy(base));
2606 res = isl_upoly_sum(res,
2607 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2608 isl_vec_copy(vec)));
2611 isl_upoly_free(base);
2612 isl_upoly_free(up);
2613 isl_vec_free(vec);
2614 return res;
2615 error:
2616 isl_upoly_free(up);
2617 isl_vec_free(vec);
2618 return NULL;
2621 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2622 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2624 isl_vec *ext;
2625 struct isl_upoly *up;
2626 isl_dim *dim;
2628 if (!qp || !pnt)
2629 goto error;
2630 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2632 if (qp->div->n_row == 0)
2633 ext = isl_vec_copy(pnt->vec);
2634 else {
2635 int i;
2636 unsigned dim = isl_dim_total(qp->dim);
2637 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2638 if (!ext)
2639 goto error;
2641 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2642 for (i = 0; i < qp->div->n_row; ++i) {
2643 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2644 1 + dim + i, &ext->el[1+dim+i]);
2645 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2646 qp->div->row[i][0]);
2650 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2651 if (!up)
2652 goto error;
2654 dim = isl_dim_copy(qp->dim);
2655 isl_qpolynomial_free(qp);
2656 isl_point_free(pnt);
2658 return isl_qpolynomial_alloc(dim, 0, up);
2659 error:
2660 isl_qpolynomial_free(qp);
2661 isl_point_free(pnt);
2662 return NULL;
2665 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2666 __isl_keep struct isl_upoly_cst *cst2)
2668 int cmp;
2669 isl_int t;
2670 isl_int_init(t);
2671 isl_int_mul(t, cst1->n, cst2->d);
2672 isl_int_submul(t, cst2->n, cst1->d);
2673 cmp = isl_int_sgn(t);
2674 isl_int_clear(t);
2675 return cmp;
2678 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2679 __isl_keep isl_qpolynomial *qp2)
2681 struct isl_upoly_cst *cst1, *cst2;
2683 if (!qp1 || !qp2)
2684 return -1;
2685 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2686 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2687 if (isl_qpolynomial_is_nan(qp1))
2688 return -1;
2689 if (isl_qpolynomial_is_nan(qp2))
2690 return -1;
2691 cst1 = isl_upoly_as_cst(qp1->upoly);
2692 cst2 = isl_upoly_as_cst(qp2->upoly);
2694 return isl_upoly_cmp(cst1, cst2) <= 0;
2697 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2698 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2700 struct isl_upoly_cst *cst1, *cst2;
2701 int cmp;
2703 if (!qp1 || !qp2)
2704 goto error;
2705 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2706 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2707 cst1 = isl_upoly_as_cst(qp1->upoly);
2708 cst2 = isl_upoly_as_cst(qp2->upoly);
2709 cmp = isl_upoly_cmp(cst1, cst2);
2711 if (cmp <= 0) {
2712 isl_qpolynomial_free(qp2);
2713 } else {
2714 isl_qpolynomial_free(qp1);
2715 qp1 = qp2;
2717 return qp1;
2718 error:
2719 isl_qpolynomial_free(qp1);
2720 isl_qpolynomial_free(qp2);
2721 return NULL;
2724 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2725 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2727 struct isl_upoly_cst *cst1, *cst2;
2728 int cmp;
2730 if (!qp1 || !qp2)
2731 goto error;
2732 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2733 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2734 cst1 = isl_upoly_as_cst(qp1->upoly);
2735 cst2 = isl_upoly_as_cst(qp2->upoly);
2736 cmp = isl_upoly_cmp(cst1, cst2);
2738 if (cmp >= 0) {
2739 isl_qpolynomial_free(qp2);
2740 } else {
2741 isl_qpolynomial_free(qp1);
2742 qp1 = qp2;
2744 return qp1;
2745 error:
2746 isl_qpolynomial_free(qp1);
2747 isl_qpolynomial_free(qp2);
2748 return NULL;
2751 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2752 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2753 unsigned first, unsigned n)
2755 unsigned total;
2756 unsigned g_pos;
2757 int *exp;
2759 if (n == 0)
2760 return qp;
2762 qp = isl_qpolynomial_cow(qp);
2763 if (!qp)
2764 return NULL;
2766 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2767 goto error);
2769 g_pos = pos(qp->dim, type) + first;
2771 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2772 if (!qp->div)
2773 goto error;
2775 total = qp->div->n_col - 2;
2776 if (total > g_pos) {
2777 int i;
2778 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2779 if (!exp)
2780 goto error;
2781 for (i = 0; i < total - g_pos; ++i)
2782 exp[i] = i + n;
2783 qp->upoly = expand(qp->upoly, exp, g_pos);
2784 free(exp);
2785 if (!qp->upoly)
2786 goto error;
2789 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2790 if (!qp->dim)
2791 goto error;
2793 return qp;
2794 error:
2795 isl_qpolynomial_free(qp);
2796 return NULL;
2799 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2800 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2802 unsigned pos;
2804 pos = isl_qpolynomial_dim(qp, type);
2806 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2809 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2810 __isl_take isl_pw_qpolynomial *pwqp,
2811 enum isl_dim_type type, unsigned n)
2813 unsigned pos;
2815 pos = isl_pw_qpolynomial_dim(pwqp, type);
2817 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2820 static int *reordering_move(isl_ctx *ctx,
2821 unsigned len, unsigned dst, unsigned src, unsigned n)
2823 int i;
2824 int *reordering;
2826 reordering = isl_alloc_array(ctx, int, len);
2827 if (!reordering)
2828 return NULL;
2830 if (dst <= src) {
2831 for (i = 0; i < dst; ++i)
2832 reordering[i] = i;
2833 for (i = 0; i < n; ++i)
2834 reordering[src + i] = dst + i;
2835 for (i = 0; i < src - dst; ++i)
2836 reordering[dst + i] = dst + n + i;
2837 for (i = 0; i < len - src - n; ++i)
2838 reordering[src + n + i] = src + n + i;
2839 } else {
2840 for (i = 0; i < src; ++i)
2841 reordering[i] = i;
2842 for (i = 0; i < n; ++i)
2843 reordering[src + i] = dst + i;
2844 for (i = 0; i < dst - src; ++i)
2845 reordering[src + n + i] = src + i;
2846 for (i = 0; i < len - dst - n; ++i)
2847 reordering[dst + n + i] = dst + n + i;
2850 return reordering;
2853 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2854 __isl_take isl_qpolynomial *qp,
2855 enum isl_dim_type dst_type, unsigned dst_pos,
2856 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2858 unsigned g_dst_pos;
2859 unsigned g_src_pos;
2860 int *reordering;
2862 qp = isl_qpolynomial_cow(qp);
2863 if (!qp)
2864 return NULL;
2866 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2867 goto error);
2869 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2870 g_src_pos = pos(qp->dim, src_type) + src_pos;
2871 if (dst_type > src_type)
2872 g_dst_pos -= n;
2874 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2875 if (!qp->div)
2876 goto error;
2877 qp = sort_divs(qp);
2878 if (!qp)
2879 goto error;
2881 reordering = reordering_move(qp->dim->ctx,
2882 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2883 if (!reordering)
2884 goto error;
2886 qp->upoly = reorder(qp->upoly, reordering);
2887 free(reordering);
2888 if (!qp->upoly)
2889 goto error;
2891 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2892 if (!qp->dim)
2893 goto error;
2895 return qp;
2896 error:
2897 isl_qpolynomial_free(qp);
2898 return NULL;
2901 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2902 isl_int *f, isl_int denom)
2904 struct isl_upoly *up;
2906 if (!dim)
2907 return NULL;
2909 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2911 return isl_qpolynomial_alloc(dim, 0, up);
2914 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2915 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2917 isl_int denom;
2918 isl_dim *dim;
2919 struct isl_upoly *up;
2920 isl_qpolynomial *qp;
2921 int sgn;
2923 if (!c)
2924 return NULL;
2926 isl_int_init(denom);
2928 isl_constraint_get_coefficient(c, type, pos, &denom);
2929 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2930 sgn = isl_int_sgn(denom);
2931 isl_int_abs(denom, denom);
2932 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2933 1 + isl_constraint_dim(c, isl_dim_all));
2934 if (sgn < 0)
2935 isl_int_neg(denom, denom);
2936 isl_constraint_set_coefficient(c, type, pos, denom);
2938 dim = isl_dim_copy(c->bmap->dim);
2940 isl_int_clear(denom);
2941 isl_constraint_free(c);
2943 qp = isl_qpolynomial_alloc(dim, 0, up);
2944 if (sgn > 0)
2945 qp = isl_qpolynomial_neg(qp);
2946 return qp;
2949 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2950 * in "qp" by subs[i].
2952 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2953 __isl_take isl_qpolynomial *qp,
2954 enum isl_dim_type type, unsigned first, unsigned n,
2955 __isl_keep isl_qpolynomial **subs)
2957 int i;
2958 struct isl_upoly **ups;
2960 if (n == 0)
2961 return qp;
2963 qp = isl_qpolynomial_cow(qp);
2964 if (!qp)
2965 return NULL;
2966 for (i = 0; i < n; ++i)
2967 if (!subs[i])
2968 goto error;
2970 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2971 goto error);
2973 for (i = 0; i < n; ++i)
2974 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2975 goto error);
2977 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2978 for (i = 0; i < n; ++i)
2979 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2981 first += pos(qp->dim, type);
2983 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2984 if (!ups)
2985 goto error;
2986 for (i = 0; i < n; ++i)
2987 ups[i] = subs[i]->upoly;
2989 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2991 free(ups);
2993 if (!qp->upoly)
2994 goto error;
2996 return qp;
2997 error:
2998 isl_qpolynomial_free(qp);
2999 return NULL;
3002 /* Extend "bset" with extra set dimensions for each integer division
3003 * in "qp" and then call "fn" with the extended bset and the polynomial
3004 * that results from replacing each of the integer divisions by the
3005 * corresponding extra set dimension.
3007 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3008 __isl_keep isl_basic_set *bset,
3009 int (*fn)(__isl_take isl_basic_set *bset,
3010 __isl_take isl_qpolynomial *poly, void *user), void *user)
3012 isl_dim *dim;
3013 isl_mat *div;
3014 isl_qpolynomial *poly;
3016 if (!qp || !bset)
3017 goto error;
3018 if (qp->div->n_row == 0)
3019 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3020 user);
3022 div = isl_mat_copy(qp->div);
3023 dim = isl_dim_copy(qp->dim);
3024 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3025 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3026 bset = isl_basic_set_copy(bset);
3027 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3028 bset = add_div_constraints(bset, div);
3030 return fn(bset, poly, user);
3031 error:
3032 return -1;
3035 /* Return total degree in variables first (inclusive) up to last (exclusive).
3037 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3039 int deg = -1;
3040 int i;
3041 struct isl_upoly_rec *rec;
3043 if (!up)
3044 return -2;
3045 if (isl_upoly_is_zero(up))
3046 return -1;
3047 if (isl_upoly_is_cst(up) || up->var < first)
3048 return 0;
3050 rec = isl_upoly_as_rec(up);
3051 if (!rec)
3052 return -2;
3054 for (i = 0; i < rec->n; ++i) {
3055 int d;
3057 if (isl_upoly_is_zero(rec->p[i]))
3058 continue;
3059 d = isl_upoly_degree(rec->p[i], first, last);
3060 if (up->var < last)
3061 d += i;
3062 if (d > deg)
3063 deg = d;
3066 return deg;
3069 /* Return total degree in set variables.
3071 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3073 unsigned ovar;
3074 unsigned nvar;
3076 if (!poly)
3077 return -2;
3079 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3080 nvar = isl_dim_size(poly->dim, isl_dim_set);
3081 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3084 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3085 unsigned pos, int deg)
3087 int i;
3088 struct isl_upoly_rec *rec;
3090 if (!up)
3091 return NULL;
3093 if (isl_upoly_is_cst(up) || up->var < pos) {
3094 if (deg == 0)
3095 return isl_upoly_copy(up);
3096 else
3097 return isl_upoly_zero(up->ctx);
3100 rec = isl_upoly_as_rec(up);
3101 if (!rec)
3102 return NULL;
3104 if (up->var == pos) {
3105 if (deg < rec->n)
3106 return isl_upoly_copy(rec->p[deg]);
3107 else
3108 return isl_upoly_zero(up->ctx);
3111 up = isl_upoly_copy(up);
3112 up = isl_upoly_cow(up);
3113 rec = isl_upoly_as_rec(up);
3114 if (!rec)
3115 goto error;
3117 for (i = 0; i < rec->n; ++i) {
3118 struct isl_upoly *t;
3119 t = isl_upoly_coeff(rec->p[i], pos, deg);
3120 if (!t)
3121 goto error;
3122 isl_upoly_free(rec->p[i]);
3123 rec->p[i] = t;
3126 return up;
3127 error:
3128 isl_upoly_free(up);
3129 return NULL;
3132 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3134 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3135 __isl_keep isl_qpolynomial *qp,
3136 enum isl_dim_type type, unsigned t_pos, int deg)
3138 unsigned g_pos;
3139 struct isl_upoly *up;
3140 isl_qpolynomial *c;
3142 if (!qp)
3143 return NULL;
3145 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3146 return NULL);
3148 g_pos = pos(qp->dim, type) + t_pos;
3149 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3151 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3152 if (!c)
3153 return NULL;
3154 isl_mat_free(c->div);
3155 c->div = isl_mat_copy(qp->div);
3156 if (!c->div)
3157 goto error;
3158 return c;
3159 error:
3160 isl_qpolynomial_free(c);
3161 return NULL;
3164 /* Homogenize the polynomial in the variables first (inclusive) up to
3165 * last (exclusive) by inserting powers of variable first.
3166 * Variable first is assumed not to appear in the input.
3168 __isl_give struct isl_upoly *isl_upoly_homogenize(
3169 __isl_take struct isl_upoly *up, int deg, int target,
3170 int first, int last)
3172 int i;
3173 struct isl_upoly_rec *rec;
3175 if (!up)
3176 return NULL;
3177 if (isl_upoly_is_zero(up))
3178 return up;
3179 if (deg == target)
3180 return up;
3181 if (isl_upoly_is_cst(up) || up->var < first) {
3182 struct isl_upoly *hom;
3184 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3185 if (!hom)
3186 goto error;
3187 rec = isl_upoly_as_rec(hom);
3188 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3190 return hom;
3193 up = isl_upoly_cow(up);
3194 rec = isl_upoly_as_rec(up);
3195 if (!rec)
3196 goto error;
3198 for (i = 0; i < rec->n; ++i) {
3199 if (isl_upoly_is_zero(rec->p[i]))
3200 continue;
3201 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3202 up->var < last ? deg + i : i, target,
3203 first, last);
3204 if (!rec->p[i])
3205 goto error;
3208 return up;
3209 error:
3210 isl_upoly_free(up);
3211 return NULL;
3214 /* Homogenize the polynomial in the set variables by introducing
3215 * powers of an extra set variable at position 0.
3217 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3218 __isl_take isl_qpolynomial *poly)
3220 unsigned ovar;
3221 unsigned nvar;
3222 int deg = isl_qpolynomial_degree(poly);
3224 if (deg < -1)
3225 goto error;
3227 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3228 poly = isl_qpolynomial_cow(poly);
3229 if (!poly)
3230 goto error;
3232 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3233 nvar = isl_dim_size(poly->dim, isl_dim_set);
3234 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3235 ovar, ovar + nvar);
3236 if (!poly->upoly)
3237 goto error;
3239 return poly;
3240 error:
3241 isl_qpolynomial_free(poly);
3242 return NULL;
3245 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3246 __isl_take isl_mat *div)
3248 isl_term *term;
3249 int n;
3251 if (!dim || !div)
3252 goto error;
3254 n = isl_dim_total(dim) + div->n_row;
3256 term = isl_calloc(dim->ctx, struct isl_term,
3257 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3258 if (!term)
3259 goto error;
3261 term->ref = 1;
3262 term->dim = dim;
3263 term->div = div;
3264 isl_int_init(term->n);
3265 isl_int_init(term->d);
3267 return term;
3268 error:
3269 isl_dim_free(dim);
3270 isl_mat_free(div);
3271 return NULL;
3274 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3276 if (!term)
3277 return NULL;
3279 term->ref++;
3280 return term;
3283 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3285 int i;
3286 isl_term *dup;
3287 unsigned total;
3289 if (term)
3290 return NULL;
3292 total = isl_dim_total(term->dim) + term->div->n_row;
3294 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3295 if (!dup)
3296 return NULL;
3298 isl_int_set(dup->n, term->n);
3299 isl_int_set(dup->d, term->d);
3301 for (i = 0; i < total; ++i)
3302 dup->pow[i] = term->pow[i];
3304 return dup;
3307 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3309 if (!term)
3310 return NULL;
3312 if (term->ref == 1)
3313 return term;
3314 term->ref--;
3315 return isl_term_dup(term);
3318 void isl_term_free(__isl_take isl_term *term)
3320 if (!term)
3321 return;
3323 if (--term->ref > 0)
3324 return;
3326 isl_dim_free(term->dim);
3327 isl_mat_free(term->div);
3328 isl_int_clear(term->n);
3329 isl_int_clear(term->d);
3330 free(term);
3333 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3335 if (!term)
3336 return 0;
3338 switch (type) {
3339 case isl_dim_param:
3340 case isl_dim_in:
3341 case isl_dim_out: return isl_dim_size(term->dim, type);
3342 case isl_dim_div: return term->div->n_row;
3343 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3344 default: return 0;
3348 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3350 return term ? term->dim->ctx : NULL;
3353 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3355 if (!term)
3356 return;
3357 isl_int_set(*n, term->n);
3360 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3362 if (!term)
3363 return;
3364 isl_int_set(*d, term->d);
3367 int isl_term_get_exp(__isl_keep isl_term *term,
3368 enum isl_dim_type type, unsigned pos)
3370 if (!term)
3371 return -1;
3373 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3375 if (type >= isl_dim_set)
3376 pos += isl_dim_size(term->dim, isl_dim_param);
3377 if (type >= isl_dim_div)
3378 pos += isl_dim_size(term->dim, isl_dim_set);
3380 return term->pow[pos];
3383 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3385 isl_basic_map *bmap;
3386 unsigned total;
3387 int k;
3389 if (!term)
3390 return NULL;
3392 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3393 return NULL);
3395 total = term->div->n_col - term->div->n_row - 2;
3396 /* No nested divs for now */
3397 isl_assert(term->dim->ctx,
3398 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3399 term->div->n_row) == -1,
3400 return NULL);
3402 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3403 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3404 goto error;
3406 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3408 return isl_basic_map_div(bmap, k);
3409 error:
3410 isl_basic_map_free(bmap);
3411 return NULL;
3414 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3415 int (*fn)(__isl_take isl_term *term, void *user),
3416 __isl_take isl_term *term, void *user)
3418 int i;
3419 struct isl_upoly_rec *rec;
3421 if (!up || !term)
3422 goto error;
3424 if (isl_upoly_is_zero(up))
3425 return term;
3427 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3428 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3429 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3431 if (isl_upoly_is_cst(up)) {
3432 struct isl_upoly_cst *cst;
3433 cst = isl_upoly_as_cst(up);
3434 if (!cst)
3435 goto error;
3436 term = isl_term_cow(term);
3437 if (!term)
3438 goto error;
3439 isl_int_set(term->n, cst->n);
3440 isl_int_set(term->d, cst->d);
3441 if (fn(isl_term_copy(term), user) < 0)
3442 goto error;
3443 return term;
3446 rec = isl_upoly_as_rec(up);
3447 if (!rec)
3448 goto error;
3450 for (i = 0; i < rec->n; ++i) {
3451 term = isl_term_cow(term);
3452 if (!term)
3453 goto error;
3454 term->pow[up->var] = i;
3455 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3456 if (!term)
3457 goto error;
3459 term->pow[up->var] = 0;
3461 return term;
3462 error:
3463 isl_term_free(term);
3464 return NULL;
3467 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3468 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3470 isl_term *term;
3472 if (!qp)
3473 return -1;
3475 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3476 if (!term)
3477 return -1;
3479 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3481 isl_term_free(term);
3483 return term ? 0 : -1;
3486 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3488 struct isl_upoly *up;
3489 isl_qpolynomial *qp;
3490 int i, n;
3492 if (!term)
3493 return NULL;
3495 n = isl_dim_total(term->dim) + term->div->n_row;
3497 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3498 for (i = 0; i < n; ++i) {
3499 if (!term->pow[i])
3500 continue;
3501 up = isl_upoly_mul(up,
3502 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3505 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3506 if (!qp)
3507 goto error;
3508 isl_mat_free(qp->div);
3509 qp->div = isl_mat_copy(term->div);
3510 if (!qp->div)
3511 goto error;
3513 isl_term_free(term);
3514 return qp;
3515 error:
3516 isl_qpolynomial_free(qp);
3517 isl_term_free(term);
3518 return NULL;
3521 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3522 __isl_take isl_dim *dim)
3524 int i;
3525 int extra;
3526 unsigned total;
3528 if (!qp || !dim)
3529 goto error;
3531 if (isl_dim_equal(qp->dim, dim)) {
3532 isl_dim_free(dim);
3533 return qp;
3536 qp = isl_qpolynomial_cow(qp);
3537 if (!qp)
3538 goto error;
3540 extra = isl_dim_size(dim, isl_dim_set) -
3541 isl_dim_size(qp->dim, isl_dim_set);
3542 total = isl_dim_total(qp->dim);
3543 if (qp->div->n_row) {
3544 int *exp;
3546 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3547 if (!exp)
3548 goto error;
3549 for (i = 0; i < qp->div->n_row; ++i)
3550 exp[i] = extra + i;
3551 qp->upoly = expand(qp->upoly, exp, total);
3552 free(exp);
3553 if (!qp->upoly)
3554 goto error;
3556 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3557 if (!qp->div)
3558 goto error;
3559 for (i = 0; i < qp->div->n_row; ++i)
3560 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3562 isl_dim_free(qp->dim);
3563 qp->dim = dim;
3565 return qp;
3566 error:
3567 isl_dim_free(dim);
3568 isl_qpolynomial_free(qp);
3569 return NULL;
3572 /* For each parameter or variable that does not appear in qp,
3573 * first eliminate the variable from all constraints and then set it to zero.
3575 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3576 __isl_keep isl_qpolynomial *qp)
3578 int *active = NULL;
3579 int i;
3580 int d;
3581 unsigned nparam;
3582 unsigned nvar;
3584 if (!set || !qp)
3585 goto error;
3587 d = isl_dim_total(set->dim);
3588 active = isl_calloc_array(set->ctx, int, d);
3589 if (set_active(qp, active) < 0)
3590 goto error;
3592 for (i = 0; i < d; ++i)
3593 if (!active[i])
3594 break;
3596 if (i == d) {
3597 free(active);
3598 return set;
3601 nparam = isl_dim_size(set->dim, isl_dim_param);
3602 nvar = isl_dim_size(set->dim, isl_dim_set);
3603 for (i = 0; i < nparam; ++i) {
3604 if (active[i])
3605 continue;
3606 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3607 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3609 for (i = 0; i < nvar; ++i) {
3610 if (active[nparam + i])
3611 continue;
3612 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3613 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3616 free(active);
3618 return set;
3619 error:
3620 free(active);
3621 isl_set_free(set);
3622 return NULL;
3625 struct isl_opt_data {
3626 isl_qpolynomial *qp;
3627 int first;
3628 isl_qpolynomial *opt;
3629 int max;
3632 static int opt_fn(__isl_take isl_point *pnt, void *user)
3634 struct isl_opt_data *data = (struct isl_opt_data *)user;
3635 isl_qpolynomial *val;
3637 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3638 if (data->first) {
3639 data->first = 0;
3640 data->opt = val;
3641 } else if (data->max) {
3642 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3643 } else {
3644 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3647 return 0;
3650 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3651 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3653 struct isl_opt_data data = { NULL, 1, NULL, max };
3655 if (!set || !qp)
3656 goto error;
3658 if (isl_upoly_is_cst(qp->upoly)) {
3659 isl_set_free(set);
3660 return qp;
3663 set = fix_inactive(set, qp);
3665 data.qp = qp;
3666 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3667 goto error;
3669 if (data.first)
3670 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3672 isl_set_free(set);
3673 isl_qpolynomial_free(qp);
3674 return data.opt;
3675 error:
3676 isl_set_free(set);
3677 isl_qpolynomial_free(qp);
3678 isl_qpolynomial_free(data.opt);
3679 return NULL;
3682 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3683 __isl_take isl_morph *morph)
3685 int i;
3686 int n_sub;
3687 isl_ctx *ctx;
3688 struct isl_upoly *up;
3689 unsigned n_div;
3690 struct isl_upoly **subs;
3691 isl_mat *mat;
3693 qp = isl_qpolynomial_cow(qp);
3694 if (!qp || !morph)
3695 goto error;
3697 ctx = qp->dim->ctx;
3698 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3700 n_sub = morph->inv->n_row - 1;
3701 if (morph->inv->n_row != morph->inv->n_col)
3702 n_sub += qp->div->n_row;
3703 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3704 if (!subs)
3705 goto error;
3707 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3708 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3709 morph->inv->row[0][0], morph->inv->n_col);
3710 if (morph->inv->n_row != morph->inv->n_col)
3711 for (i = 0; i < qp->div->n_row; ++i)
3712 subs[morph->inv->n_row - 1 + i] =
3713 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3715 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3717 for (i = 0; i < n_sub; ++i)
3718 isl_upoly_free(subs[i]);
3719 free(subs);
3721 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3722 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3723 qp->div = isl_mat_product(qp->div, mat);
3724 isl_dim_free(qp->dim);
3725 qp->dim = isl_dim_copy(morph->ran->dim);
3727 if (!qp->upoly || !qp->div || !qp->dim)
3728 goto error;
3730 isl_morph_free(morph);
3732 return qp;
3733 error:
3734 isl_qpolynomial_free(qp);
3735 isl_morph_free(morph);
3736 return NULL;
3739 static int neg_entry(void **entry, void *user)
3741 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3743 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3745 return *pwqp ? 0 : -1;
3748 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3749 __isl_take isl_union_pw_qpolynomial *upwqp)
3751 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3752 if (!upwqp)
3753 return NULL;
3755 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3756 &neg_entry, NULL) < 0)
3757 goto error;
3759 return upwqp;
3760 error:
3761 isl_union_pw_qpolynomial_free(upwqp);
3762 return NULL;
3765 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3766 __isl_take isl_union_pw_qpolynomial *upwqp1,
3767 __isl_take isl_union_pw_qpolynomial *upwqp2)
3769 return isl_union_pw_qpolynomial_add(upwqp1,
3770 isl_union_pw_qpolynomial_neg(upwqp2));
3773 static int mul_entry(void **entry, void *user)
3775 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3776 uint32_t hash;
3777 struct isl_hash_table_entry *entry2;
3778 isl_pw_qpolynomial *pwpq = *entry;
3779 int empty;
3781 hash = isl_dim_get_hash(pwpq->dim);
3782 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3783 hash, &has_dim, pwpq->dim, 0);
3784 if (!entry2)
3785 return 0;
3787 pwpq = isl_pw_qpolynomial_copy(pwpq);
3788 pwpq = isl_pw_qpolynomial_mul(pwpq,
3789 isl_pw_qpolynomial_copy(entry2->data));
3791 empty = isl_pw_qpolynomial_is_zero(pwpq);
3792 if (empty < 0) {
3793 isl_pw_qpolynomial_free(pwpq);
3794 return -1;
3796 if (empty) {
3797 isl_pw_qpolynomial_free(pwpq);
3798 return 0;
3801 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3803 return 0;
3806 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3807 __isl_take isl_union_pw_qpolynomial *upwqp1,
3808 __isl_take isl_union_pw_qpolynomial *upwqp2)
3810 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3813 /* Reorder the columns of the given div definitions according to the
3814 * given reordering.
3816 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3817 __isl_take isl_reordering *r)
3819 int i, j;
3820 isl_mat *mat;
3821 int extra;
3823 if (!div || !r)
3824 goto error;
3826 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3827 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3828 if (!mat)
3829 goto error;
3831 for (i = 0; i < div->n_row; ++i) {
3832 isl_seq_cpy(mat->row[i], div->row[i], 2);
3833 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3834 for (j = 0; j < r->len; ++j)
3835 isl_int_set(mat->row[i][2 + r->pos[j]],
3836 div->row[i][2 + j]);
3839 isl_reordering_free(r);
3840 isl_mat_free(div);
3841 return mat;
3842 error:
3843 isl_reordering_free(r);
3844 isl_mat_free(div);
3845 return NULL;
3848 /* Reorder the dimension of "qp" according to the given reordering.
3850 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3851 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3853 qp = isl_qpolynomial_cow(qp);
3854 if (!qp)
3855 goto error;
3857 r = isl_reordering_extend(r, qp->div->n_row);
3858 if (!r)
3859 goto error;
3861 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3862 if (!qp->div)
3863 goto error;
3865 qp->upoly = reorder(qp->upoly, r->pos);
3866 if (!qp->upoly)
3867 goto error;
3869 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3871 isl_reordering_free(r);
3872 return qp;
3873 error:
3874 isl_qpolynomial_free(qp);
3875 isl_reordering_free(r);
3876 return NULL;
3879 struct isl_split_periods_data {
3880 int max_periods;
3881 isl_pw_qpolynomial *res;
3884 /* Create a slice where the integer division "div" has the fixed value "v".
3885 * In particular, if "div" refers to floor(f/m), then create a slice
3887 * m v <= f <= m v + (m - 1)
3889 * or
3891 * f - m v >= 0
3892 * -f + m v + (m - 1) >= 0
3894 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3895 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3897 int total;
3898 isl_basic_set *bset = NULL;
3899 int k;
3901 if (!dim || !qp)
3902 goto error;
3904 total = isl_dim_total(dim);
3905 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3907 k = isl_basic_set_alloc_inequality(bset);
3908 if (k < 0)
3909 goto error;
3910 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3911 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3913 k = isl_basic_set_alloc_inequality(bset);
3914 if (k < 0)
3915 goto error;
3916 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3917 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3918 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3919 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3921 isl_dim_free(dim);
3922 return isl_set_from_basic_set(bset);
3923 error:
3924 isl_basic_set_free(bset);
3925 isl_dim_free(dim);
3926 return NULL;
3929 static int split_periods(__isl_take isl_set *set,
3930 __isl_take isl_qpolynomial *qp, void *user);
3932 /* Create a slice of the domain "set" such that integer division "div"
3933 * has the fixed value "v" and add the results to data->res,
3934 * replacing the integer division by "v" in "qp".
3936 static int set_div(__isl_take isl_set *set,
3937 __isl_take isl_qpolynomial *qp, int div, isl_int v,
3938 struct isl_split_periods_data *data)
3940 int i;
3941 int total;
3942 isl_set *slice;
3943 struct isl_upoly *cst;
3945 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3946 set = isl_set_intersect(set, slice);
3948 if (!qp)
3949 goto error;
3951 total = isl_dim_total(qp->dim);
3953 for (i = div + 1; i < qp->div->n_row; ++i) {
3954 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
3955 continue;
3956 isl_int_addmul(qp->div->row[i][1],
3957 qp->div->row[i][2 + total + div], v);
3958 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
3961 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3962 qp = substitute_div(qp, div, cst);
3964 return split_periods(set, qp, data);
3965 error:
3966 isl_set_free(set);
3967 isl_qpolynomial_free(qp);
3968 return -1;
3971 /* Split the domain "set" such that integer division "div"
3972 * has a fixed value (ranging from "min" to "max") on each slice
3973 * and add the results to data->res.
3975 static int split_div(__isl_take isl_set *set,
3976 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3977 struct isl_split_periods_data *data)
3979 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3980 isl_set *set_i = isl_set_copy(set);
3981 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3983 if (set_div(set_i, qp_i, div, min, data) < 0)
3984 goto error;
3986 isl_set_free(set);
3987 isl_qpolynomial_free(qp);
3988 return 0;
3989 error:
3990 isl_set_free(set);
3991 isl_qpolynomial_free(qp);
3992 return -1;
3995 /* If "qp" refers to any integer division
3996 * that can only attain "max_periods" distinct values on "set"
3997 * then split the domain along those distinct values.
3998 * Add the results (or the original if no splitting occurs)
3999 * to data->res.
4001 static int split_periods(__isl_take isl_set *set,
4002 __isl_take isl_qpolynomial *qp, void *user)
4004 int i;
4005 isl_pw_qpolynomial *pwqp;
4006 struct isl_split_periods_data *data;
4007 isl_int min, max;
4008 int total;
4009 int r = 0;
4011 data = (struct isl_split_periods_data *)user;
4013 if (!set || !qp)
4014 goto error;
4016 if (qp->div->n_row == 0) {
4017 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4018 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4019 return 0;
4022 isl_int_init(min);
4023 isl_int_init(max);
4024 total = isl_dim_total(qp->dim);
4025 for (i = 0; i < qp->div->n_row; ++i) {
4026 enum isl_lp_result lp_res;
4028 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4029 qp->div->n_row) != -1)
4030 continue;
4032 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4033 set->ctx->one, &min, NULL, NULL);
4034 if (lp_res == isl_lp_error)
4035 goto error2;
4036 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4037 continue;
4038 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4040 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4041 set->ctx->one, &max, NULL, NULL);
4042 if (lp_res == isl_lp_error)
4043 goto error2;
4044 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4045 continue;
4046 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4048 isl_int_sub(max, max, min);
4049 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4050 isl_int_add(max, max, min);
4051 break;
4055 if (i < qp->div->n_row) {
4056 r = split_div(set, qp, i, min, max, data);
4057 } else {
4058 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4059 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4062 isl_int_clear(max);
4063 isl_int_clear(min);
4065 return r;
4066 error2:
4067 isl_int_clear(max);
4068 isl_int_clear(min);
4069 error:
4070 isl_set_free(set);
4071 isl_qpolynomial_free(qp);
4072 return -1;
4075 /* If any quasi-polynomial in pwqp refers to any integer division
4076 * that can only attain "max_periods" distinct values on its domain
4077 * then split the domain along those distinct values.
4079 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4080 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4082 struct isl_split_periods_data data;
4084 data.max_periods = max_periods;
4085 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4087 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4088 goto error;
4090 isl_pw_qpolynomial_free(pwqp);
4092 return data.res;
4093 error:
4094 isl_pw_qpolynomial_free(data.res);
4095 isl_pw_qpolynomial_free(pwqp);
4096 return NULL;
4099 /* Construct a piecewise quasipolynomial that is constant on the given
4100 * domain. In particular, it is
4101 * 0 if cst == 0
4102 * 1 if cst == 1
4103 * infinity if cst == -1
4105 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4106 __isl_take isl_basic_set *bset, int cst)
4108 isl_dim *dim;
4109 isl_qpolynomial *qp;
4111 if (!bset)
4112 return NULL;
4114 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4115 dim = isl_basic_set_get_dim(bset);
4116 if (cst < 0)
4117 qp = isl_qpolynomial_infty(dim);
4118 else if (cst == 0)
4119 qp = isl_qpolynomial_zero(dim);
4120 else
4121 qp = isl_qpolynomial_one(dim);
4122 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4125 /* Factor bset, call fn on each of the factors and return the product.
4127 * If no factors can be found, simply call fn on the input.
4128 * Otherwise, construct the factors based on the factorizer,
4129 * call fn on each factor and compute the product.
4131 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4132 __isl_take isl_basic_set *bset,
4133 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4135 int i, n;
4136 isl_dim *dim;
4137 isl_set *set;
4138 isl_factorizer *f;
4139 isl_qpolynomial *qp;
4140 isl_pw_qpolynomial *pwqp;
4141 unsigned nparam;
4142 unsigned nvar;
4144 f = isl_basic_set_factorizer(bset);
4145 if (!f)
4146 goto error;
4147 if (f->n_group == 0) {
4148 isl_factorizer_free(f);
4149 return fn(bset);
4152 nparam = isl_basic_set_dim(bset, isl_dim_param);
4153 nvar = isl_basic_set_dim(bset, isl_dim_set);
4155 dim = isl_basic_set_get_dim(bset);
4156 dim = isl_dim_domain(dim);
4157 set = isl_set_universe(isl_dim_copy(dim));
4158 qp = isl_qpolynomial_one(dim);
4159 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4161 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4163 for (i = 0, n = 0; i < f->n_group; ++i) {
4164 isl_basic_set *bset_i;
4165 isl_pw_qpolynomial *pwqp_i;
4167 bset_i = isl_basic_set_copy(bset);
4168 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4169 nparam + n + f->len[i], nvar - n - f->len[i]);
4170 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4171 nparam, n);
4172 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4173 n + f->len[i], nvar - n - f->len[i]);
4174 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4176 pwqp_i = fn(bset_i);
4177 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4179 n += f->len[i];
4182 isl_basic_set_free(bset);
4183 isl_factorizer_free(f);
4185 return pwqp;
4186 error:
4187 isl_basic_set_free(bset);
4188 return NULL;
4191 /* Factor bset, call fn on each of the factors and return the product.
4192 * The function is assumed to evaluate to zero on empty domains,
4193 * to one on zero-dimensional domains and to infinity on unbounded domains
4194 * and will not be called explicitly on zero-dimensional or unbounded domains.
4196 * We first check for some special cases and remove all equalities.
4197 * Then we hand over control to compressed_multiplicative_call.
4199 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4200 __isl_take isl_basic_set *bset,
4201 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4203 int bounded;
4204 isl_morph *morph;
4205 isl_pw_qpolynomial *pwqp;
4206 unsigned orig_nvar, final_nvar;
4208 if (!bset)
4209 return NULL;
4211 if (isl_basic_set_fast_is_empty(bset))
4212 return constant_on_domain(bset, 0);
4214 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4216 if (orig_nvar == 0)
4217 return constant_on_domain(bset, 1);
4219 bounded = isl_basic_set_is_bounded(bset);
4220 if (bounded < 0)
4221 goto error;
4222 if (!bounded)
4223 return constant_on_domain(bset, -1);
4225 if (bset->n_eq == 0)
4226 return compressed_multiplicative_call(bset, fn);
4228 morph = isl_basic_set_full_compression(bset);
4229 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4231 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4233 pwqp = compressed_multiplicative_call(bset, fn);
4235 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4236 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4237 morph = isl_morph_inverse(morph);
4239 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4241 return pwqp;
4242 error:
4243 isl_basic_set_free(bset);
4244 return NULL;
4247 /* Drop all floors in "qp", turning each integer division [a/m] into
4248 * a rational division a/m. If "down" is set, then the integer division
4249 * is replaces by (a-(m-1))/m instead.
4251 static __isl_give isl_qpolynomial *qp_drop_floors(
4252 __isl_take isl_qpolynomial *qp, int down)
4254 int i;
4255 struct isl_upoly *s;
4257 if (!qp)
4258 return NULL;
4259 if (qp->div->n_row == 0)
4260 return qp;
4262 qp = isl_qpolynomial_cow(qp);
4263 if (!qp)
4264 return NULL;
4266 for (i = qp->div->n_row - 1; i >= 0; --i) {
4267 if (down) {
4268 isl_int_sub(qp->div->row[i][1],
4269 qp->div->row[i][1], qp->div->row[i][0]);
4270 isl_int_add_ui(qp->div->row[i][1],
4271 qp->div->row[i][1], 1);
4273 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4274 qp->div->row[i][0], qp->div->n_col - 1);
4275 qp = substitute_div(qp, i, s);
4276 if (!qp)
4277 return NULL;
4280 return qp;
4283 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4284 * a rational division a/m.
4286 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4287 __isl_take isl_pw_qpolynomial *pwqp)
4289 int i;
4291 if (!pwqp)
4292 return NULL;
4294 if (isl_pw_qpolynomial_is_zero(pwqp))
4295 return pwqp;
4297 pwqp = isl_pw_qpolynomial_cow(pwqp);
4298 if (!pwqp)
4299 return NULL;
4301 for (i = 0; i < pwqp->n; ++i) {
4302 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4303 if (!pwqp->p[i].qp)
4304 goto error;
4307 return pwqp;
4308 error:
4309 isl_pw_qpolynomial_free(pwqp);
4310 return NULL;
4313 /* Adjust all the integer divisions in "qp" such that they are at least
4314 * one over the given orthant (identified by "signs"). This ensures
4315 * that they will still be non-negative even after subtracting (m-1)/m.
4317 * In particular, f is replaced by f' + v, changing f = [a/m]
4318 * to f' = [(a - m v)/m].
4319 * If the constant term k in a is smaller than m,
4320 * the constant term of v is set to floor(k/m) - 1.
4321 * For any other term, if the coefficient c and the variable x have
4322 * the same sign, then no changes are needed.
4323 * Otherwise, if the variable is positive (and c is negative),
4324 * then the coefficient of x in v is set to floor(c/m).
4325 * If the variable is negative (and c is positive),
4326 * then the coefficient of x in v is set to ceil(c/m).
4328 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4329 int *signs)
4331 int i, j;
4332 int total;
4333 isl_vec *v = NULL;
4334 struct isl_upoly *s;
4336 qp = isl_qpolynomial_cow(qp);
4337 if (!qp)
4338 return NULL;
4339 qp->div = isl_mat_cow(qp->div);
4340 if (!qp->div)
4341 goto error;
4343 total = isl_dim_total(qp->dim);
4344 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4346 for (i = 0; i < qp->div->n_row; ++i) {
4347 isl_int *row = qp->div->row[i];
4348 v = isl_vec_clr(v);
4349 if (!v)
4350 goto error;
4351 if (isl_int_lt(row[1], row[0])) {
4352 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4353 isl_int_sub_ui(v->el[0], v->el[0], 1);
4354 isl_int_submul(row[1], row[0], v->el[0]);
4356 for (j = 0; j < total; ++j) {
4357 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4358 continue;
4359 if (signs[j] < 0)
4360 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4361 else
4362 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4363 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4365 for (j = 0; j < i; ++j) {
4366 if (isl_int_sgn(row[2 + total + j]) >= 0)
4367 continue;
4368 isl_int_fdiv_q(v->el[1 + total + j],
4369 row[2 + total + j], row[0]);
4370 isl_int_submul(row[2 + total + j],
4371 row[0], v->el[1 + total + j]);
4373 for (j = i + 1; j < qp->div->n_row; ++j) {
4374 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4375 continue;
4376 isl_seq_combine(qp->div->row[j] + 1,
4377 qp->div->ctx->one, qp->div->row[j] + 1,
4378 qp->div->row[j][2 + total + i], v->el, v->size);
4380 isl_int_set_si(v->el[1 + total + i], 1);
4381 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4382 qp->div->ctx->one, v->size);
4383 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4384 isl_upoly_free(s);
4385 if (!qp->upoly)
4386 goto error;
4389 isl_vec_free(v);
4390 return qp;
4391 error:
4392 isl_vec_free(v);
4393 isl_qpolynomial_free(qp);
4394 return NULL;
4397 struct isl_to_poly_data {
4398 int sign;
4399 isl_pw_qpolynomial *res;
4400 isl_qpolynomial *qp;
4403 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4404 * We first make all integer divisions positive and then split the
4405 * quasipolynomials into terms with sign data->sign (the direction
4406 * of the requested approximation) and terms with the opposite sign.
4407 * In the first set of terms, each integer division [a/m] is
4408 * overapproximated by a/m, while in the second it is underapproximated
4409 * by (a-(m-1))/m.
4411 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4412 void *user)
4414 struct isl_to_poly_data *data = user;
4415 isl_pw_qpolynomial *t;
4416 isl_qpolynomial *qp, *up, *down;
4418 qp = isl_qpolynomial_copy(data->qp);
4419 qp = make_divs_pos(qp, signs);
4421 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4422 up = qp_drop_floors(up, 0);
4423 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4424 down = qp_drop_floors(down, 1);
4426 isl_qpolynomial_free(qp);
4427 qp = isl_qpolynomial_add(up, down);
4429 t = isl_pw_qpolynomial_alloc(orthant, qp);
4430 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4432 return 0;
4435 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4436 * the polynomial will be an overapproximation. If "sign" is negative,
4437 * it will be an underapproximation. If "sign" is zero, the approximation
4438 * will lie somewhere in between.
4440 * In particular, is sign == 0, we simply drop the floors, turning
4441 * the integer divisions into rational divisions.
4442 * Otherwise, we split the domains into orthants, make all integer divisions
4443 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4444 * depending on the requested sign and the sign of the term in which
4445 * the integer division appears.
4447 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4448 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4450 int i;
4451 struct isl_to_poly_data data;
4453 if (sign == 0)
4454 return pwqp_drop_floors(pwqp);
4456 if (!pwqp)
4457 return NULL;
4459 data.sign = sign;
4460 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4462 for (i = 0; i < pwqp->n; ++i) {
4463 if (pwqp->p[i].qp->div->n_row == 0) {
4464 isl_pw_qpolynomial *t;
4465 t = isl_pw_qpolynomial_alloc(
4466 isl_set_copy(pwqp->p[i].set),
4467 isl_qpolynomial_copy(pwqp->p[i].qp));
4468 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4469 continue;
4471 data.qp = pwqp->p[i].qp;
4472 if (isl_set_foreach_orthant(pwqp->p[i].set,
4473 &to_polynomial_on_orthant, &data) < 0)
4474 goto error;
4477 isl_pw_qpolynomial_free(pwqp);
4479 return data.res;
4480 error:
4481 isl_pw_qpolynomial_free(pwqp);
4482 isl_pw_qpolynomial_free(data.res);
4483 return NULL;
4486 static int poly_entry(void **entry, void *user)
4488 int *sign = user;
4489 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4491 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4493 return *pwqp ? 0 : -1;
4496 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4497 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4499 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4500 if (!upwqp)
4501 return NULL;
4503 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4504 &poly_entry, &sign) < 0)
4505 goto error;
4507 return upwqp;
4508 error:
4509 isl_union_pw_qpolynomial_free(upwqp);
4510 return NULL;
4513 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4514 __isl_take isl_qpolynomial *qp)
4516 int i, k;
4517 isl_dim *dim;
4518 isl_vec *aff = NULL;
4519 isl_basic_map *bmap = NULL;
4520 unsigned pos;
4521 unsigned n_div;
4523 if (!qp)
4524 return NULL;
4525 if (!isl_upoly_is_affine(qp->upoly))
4526 isl_die(qp->dim->ctx, isl_error_invalid,
4527 "input quasi-polynomial not affine", goto error);
4528 aff = isl_qpolynomial_extract_affine(qp);
4529 if (!aff)
4530 goto error;
4531 dim = isl_qpolynomial_get_dim(qp);
4532 dim = isl_dim_from_domain(dim);
4533 pos = 1 + isl_dim_offset(dim, isl_dim_out);
4534 dim = isl_dim_add(dim, isl_dim_out, 1);
4535 n_div = qp->div->n_row;
4536 bmap = isl_basic_map_alloc_dim(dim, n_div, 1, 2 * n_div);
4538 for (i = 0; i < n_div; ++i) {
4539 k = isl_basic_map_alloc_div(bmap);
4540 if (k < 0)
4541 goto error;
4542 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4543 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4544 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4545 goto error;
4547 k = isl_basic_map_alloc_equality(bmap);
4548 if (k < 0)
4549 goto error;
4550 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4551 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4552 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4554 isl_vec_free(aff);
4555 isl_qpolynomial_free(qp);
4556 bmap = isl_basic_map_finalize(bmap);
4557 return bmap;
4558 error:
4559 isl_vec_free(aff);
4560 isl_qpolynomial_free(qp);
4561 isl_basic_map_free(bmap);
4562 return NULL;