add isl_pw_qpolynomial_from_pw_aff
[isl.git] / isl_polynomial.c
blob14a7b52da38977d3a7cf98a2077bee1750b5bfd7
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <stdlib.h>
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl/lp.h>
16 #include <isl/seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_dim_private.h>
22 #include <isl_div_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_range.h>
25 #include <isl_local_space_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_config.h>
29 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
31 switch (type) {
32 case isl_dim_param: return 0;
33 case isl_dim_in: return dim->nparam;
34 case isl_dim_out: return dim->nparam + dim->n_in;
35 default: return 0;
39 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
41 if (!up)
42 return -1;
44 return up->var < 0;
47 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
49 if (!up)
50 return NULL;
52 isl_assert(up->ctx, up->var < 0, return NULL);
54 return (struct isl_upoly_cst *)up;
57 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
59 if (!up)
60 return NULL;
62 isl_assert(up->ctx, up->var >= 0, return NULL);
64 return (struct isl_upoly_rec *)up;
67 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
68 __isl_keep struct isl_upoly *up2)
70 int i;
71 struct isl_upoly_rec *rec1, *rec2;
73 if (!up1 || !up2)
74 return -1;
75 if (up1 == up2)
76 return 1;
77 if (up1->var != up2->var)
78 return 0;
79 if (isl_upoly_is_cst(up1)) {
80 struct isl_upoly_cst *cst1, *cst2;
81 cst1 = isl_upoly_as_cst(up1);
82 cst2 = isl_upoly_as_cst(up2);
83 if (!cst1 || !cst2)
84 return -1;
85 return isl_int_eq(cst1->n, cst2->n) &&
86 isl_int_eq(cst1->d, cst2->d);
89 rec1 = isl_upoly_as_rec(up1);
90 rec2 = isl_upoly_as_rec(up2);
91 if (!rec1 || !rec2)
92 return -1;
94 if (rec1->n != rec2->n)
95 return 0;
97 for (i = 0; i < rec1->n; ++i) {
98 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
99 if (eq < 0 || !eq)
100 return eq;
103 return 1;
106 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
108 struct isl_upoly_cst *cst;
110 if (!up)
111 return -1;
112 if (!isl_upoly_is_cst(up))
113 return 0;
115 cst = isl_upoly_as_cst(up);
116 if (!cst)
117 return -1;
119 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
122 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
124 struct isl_upoly_cst *cst;
126 if (!up)
127 return 0;
128 if (!isl_upoly_is_cst(up))
129 return 0;
131 cst = isl_upoly_as_cst(up);
132 if (!cst)
133 return 0;
135 return isl_int_sgn(cst->n);
138 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
140 struct isl_upoly_cst *cst;
142 if (!up)
143 return -1;
144 if (!isl_upoly_is_cst(up))
145 return 0;
147 cst = isl_upoly_as_cst(up);
148 if (!cst)
149 return -1;
151 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
154 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
156 struct isl_upoly_cst *cst;
158 if (!up)
159 return -1;
160 if (!isl_upoly_is_cst(up))
161 return 0;
163 cst = isl_upoly_as_cst(up);
164 if (!cst)
165 return -1;
167 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
170 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
172 struct isl_upoly_cst *cst;
174 if (!up)
175 return -1;
176 if (!isl_upoly_is_cst(up))
177 return 0;
179 cst = isl_upoly_as_cst(up);
180 if (!cst)
181 return -1;
183 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
186 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
188 struct isl_upoly_cst *cst;
190 if (!up)
191 return -1;
192 if (!isl_upoly_is_cst(up))
193 return 0;
195 cst = isl_upoly_as_cst(up);
196 if (!cst)
197 return -1;
199 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
202 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
204 struct isl_upoly_cst *cst;
206 if (!up)
207 return -1;
208 if (!isl_upoly_is_cst(up))
209 return 0;
211 cst = isl_upoly_as_cst(up);
212 if (!cst)
213 return -1;
215 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
218 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
220 struct isl_upoly_cst *cst;
222 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
223 if (!cst)
224 return NULL;
226 cst->up.ref = 1;
227 cst->up.ctx = ctx;
228 isl_ctx_ref(ctx);
229 cst->up.var = -1;
231 isl_int_init(cst->n);
232 isl_int_init(cst->d);
234 return cst;
237 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
239 struct isl_upoly_cst *cst;
241 cst = isl_upoly_cst_alloc(ctx);
242 if (!cst)
243 return NULL;
245 isl_int_set_si(cst->n, 0);
246 isl_int_set_si(cst->d, 1);
248 return &cst->up;
251 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
253 struct isl_upoly_cst *cst;
255 cst = isl_upoly_cst_alloc(ctx);
256 if (!cst)
257 return NULL;
259 isl_int_set_si(cst->n, 1);
260 isl_int_set_si(cst->d, 1);
262 return &cst->up;
265 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
267 struct isl_upoly_cst *cst;
269 cst = isl_upoly_cst_alloc(ctx);
270 if (!cst)
271 return NULL;
273 isl_int_set_si(cst->n, 1);
274 isl_int_set_si(cst->d, 0);
276 return &cst->up;
279 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
281 struct isl_upoly_cst *cst;
283 cst = isl_upoly_cst_alloc(ctx);
284 if (!cst)
285 return NULL;
287 isl_int_set_si(cst->n, -1);
288 isl_int_set_si(cst->d, 0);
290 return &cst->up;
293 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
295 struct isl_upoly_cst *cst;
297 cst = isl_upoly_cst_alloc(ctx);
298 if (!cst)
299 return NULL;
301 isl_int_set_si(cst->n, 0);
302 isl_int_set_si(cst->d, 0);
304 return &cst->up;
307 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
308 isl_int n, isl_int d)
310 struct isl_upoly_cst *cst;
312 cst = isl_upoly_cst_alloc(ctx);
313 if (!cst)
314 return NULL;
316 isl_int_set(cst->n, n);
317 isl_int_set(cst->d, d);
319 return &cst->up;
322 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
323 int var, int size)
325 struct isl_upoly_rec *rec;
327 isl_assert(ctx, var >= 0, return NULL);
328 isl_assert(ctx, size >= 0, return NULL);
329 rec = isl_calloc(ctx, struct isl_upoly_rec,
330 sizeof(struct isl_upoly_rec) +
331 size * sizeof(struct isl_upoly *));
332 if (!rec)
333 return NULL;
335 rec->up.ref = 1;
336 rec->up.ctx = ctx;
337 isl_ctx_ref(ctx);
338 rec->up.var = var;
340 rec->n = 0;
341 rec->size = size;
343 return rec;
346 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
347 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
349 qp = isl_qpolynomial_cow(qp);
350 if (!qp || !dim)
351 goto error;
353 isl_dim_free(qp->dim);
354 qp->dim = dim;
356 return qp;
357 error:
358 isl_qpolynomial_free(qp);
359 isl_dim_free(dim);
360 return NULL;
363 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
365 return qp ? qp->dim->ctx : NULL;
368 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
370 return qp ? isl_dim_copy(qp->dim) : NULL;
373 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
374 enum isl_dim_type type)
376 return qp ? isl_dim_size(qp->dim, type) : 0;
379 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
381 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
384 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
386 return qp ? isl_upoly_is_one(qp->upoly) : -1;
389 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
391 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
394 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
396 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
399 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
401 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
404 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
406 return qp ? isl_upoly_sgn(qp->upoly) : 0;
409 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
411 isl_int_clear(cst->n);
412 isl_int_clear(cst->d);
415 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
417 int i;
419 for (i = 0; i < rec->n; ++i)
420 isl_upoly_free(rec->p[i]);
423 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
425 if (!up)
426 return NULL;
428 up->ref++;
429 return up;
432 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
434 struct isl_upoly_cst *cst;
435 struct isl_upoly_cst *dup;
437 cst = isl_upoly_as_cst(up);
438 if (!cst)
439 return NULL;
441 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
442 if (!dup)
443 return NULL;
444 isl_int_set(dup->n, cst->n);
445 isl_int_set(dup->d, cst->d);
447 return &dup->up;
450 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
452 int i;
453 struct isl_upoly_rec *rec;
454 struct isl_upoly_rec *dup;
456 rec = isl_upoly_as_rec(up);
457 if (!rec)
458 return NULL;
460 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
461 if (!dup)
462 return NULL;
464 for (i = 0; i < rec->n; ++i) {
465 dup->p[i] = isl_upoly_copy(rec->p[i]);
466 if (!dup->p[i])
467 goto error;
468 dup->n++;
471 return &dup->up;
472 error:
473 isl_upoly_free(&dup->up);
474 return NULL;
477 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
479 if (!up)
480 return NULL;
482 if (isl_upoly_is_cst(up))
483 return isl_upoly_dup_cst(up);
484 else
485 return isl_upoly_dup_rec(up);
488 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
490 if (!up)
491 return NULL;
493 if (up->ref == 1)
494 return up;
495 up->ref--;
496 return isl_upoly_dup(up);
499 void isl_upoly_free(__isl_take struct isl_upoly *up)
501 if (!up)
502 return;
504 if (--up->ref > 0)
505 return;
507 if (up->var < 0)
508 upoly_free_cst((struct isl_upoly_cst *)up);
509 else
510 upoly_free_rec((struct isl_upoly_rec *)up);
512 isl_ctx_deref(up->ctx);
513 free(up);
516 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
518 isl_int gcd;
520 isl_int_init(gcd);
521 isl_int_gcd(gcd, cst->n, cst->d);
522 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
523 isl_int_divexact(cst->n, cst->n, gcd);
524 isl_int_divexact(cst->d, cst->d, gcd);
526 isl_int_clear(gcd);
529 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
530 __isl_take struct isl_upoly *up2)
532 struct isl_upoly_cst *cst1;
533 struct isl_upoly_cst *cst2;
535 up1 = isl_upoly_cow(up1);
536 if (!up1 || !up2)
537 goto error;
539 cst1 = isl_upoly_as_cst(up1);
540 cst2 = isl_upoly_as_cst(up2);
542 if (isl_int_eq(cst1->d, cst2->d))
543 isl_int_add(cst1->n, cst1->n, cst2->n);
544 else {
545 isl_int_mul(cst1->n, cst1->n, cst2->d);
546 isl_int_addmul(cst1->n, cst2->n, cst1->d);
547 isl_int_mul(cst1->d, cst1->d, cst2->d);
550 isl_upoly_cst_reduce(cst1);
552 isl_upoly_free(up2);
553 return up1;
554 error:
555 isl_upoly_free(up1);
556 isl_upoly_free(up2);
557 return NULL;
560 static __isl_give struct isl_upoly *replace_by_zero(
561 __isl_take struct isl_upoly *up)
563 struct isl_ctx *ctx;
565 if (!up)
566 return NULL;
567 ctx = up->ctx;
568 isl_upoly_free(up);
569 return isl_upoly_zero(ctx);
572 static __isl_give struct isl_upoly *replace_by_constant_term(
573 __isl_take struct isl_upoly *up)
575 struct isl_upoly_rec *rec;
576 struct isl_upoly *cst;
578 if (!up)
579 return NULL;
581 rec = isl_upoly_as_rec(up);
582 if (!rec)
583 goto error;
584 cst = isl_upoly_copy(rec->p[0]);
585 isl_upoly_free(up);
586 return cst;
587 error:
588 isl_upoly_free(up);
589 return NULL;
592 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
593 __isl_take struct isl_upoly *up2)
595 int i;
596 struct isl_upoly_rec *rec1, *rec2;
598 if (!up1 || !up2)
599 goto error;
601 if (isl_upoly_is_nan(up1)) {
602 isl_upoly_free(up2);
603 return up1;
606 if (isl_upoly_is_nan(up2)) {
607 isl_upoly_free(up1);
608 return up2;
611 if (isl_upoly_is_zero(up1)) {
612 isl_upoly_free(up1);
613 return up2;
616 if (isl_upoly_is_zero(up2)) {
617 isl_upoly_free(up2);
618 return up1;
621 if (up1->var < up2->var)
622 return isl_upoly_sum(up2, up1);
624 if (up2->var < up1->var) {
625 struct isl_upoly_rec *rec;
626 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
627 isl_upoly_free(up1);
628 return up2;
630 up1 = isl_upoly_cow(up1);
631 rec = isl_upoly_as_rec(up1);
632 if (!rec)
633 goto error;
634 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
635 if (rec->n == 1)
636 up1 = replace_by_constant_term(up1);
637 return up1;
640 if (isl_upoly_is_cst(up1))
641 return isl_upoly_sum_cst(up1, up2);
643 rec1 = isl_upoly_as_rec(up1);
644 rec2 = isl_upoly_as_rec(up2);
645 if (!rec1 || !rec2)
646 goto error;
648 if (rec1->n < rec2->n)
649 return isl_upoly_sum(up2, up1);
651 up1 = isl_upoly_cow(up1);
652 rec1 = isl_upoly_as_rec(up1);
653 if (!rec1)
654 goto error;
656 for (i = rec2->n - 1; i >= 0; --i) {
657 rec1->p[i] = isl_upoly_sum(rec1->p[i],
658 isl_upoly_copy(rec2->p[i]));
659 if (!rec1->p[i])
660 goto error;
661 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
662 isl_upoly_free(rec1->p[i]);
663 rec1->n--;
667 if (rec1->n == 0)
668 up1 = replace_by_zero(up1);
669 else if (rec1->n == 1)
670 up1 = replace_by_constant_term(up1);
672 isl_upoly_free(up2);
674 return up1;
675 error:
676 isl_upoly_free(up1);
677 isl_upoly_free(up2);
678 return NULL;
681 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
682 __isl_take struct isl_upoly *up, isl_int v)
684 struct isl_upoly_cst *cst;
686 up = isl_upoly_cow(up);
687 if (!up)
688 return NULL;
690 cst = isl_upoly_as_cst(up);
692 isl_int_addmul(cst->n, cst->d, v);
694 return up;
697 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
698 __isl_take struct isl_upoly *up, isl_int v)
700 struct isl_upoly_rec *rec;
702 if (!up)
703 return NULL;
705 if (isl_upoly_is_cst(up))
706 return isl_upoly_cst_add_isl_int(up, v);
708 up = isl_upoly_cow(up);
709 rec = isl_upoly_as_rec(up);
710 if (!rec)
711 goto error;
713 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
714 if (!rec->p[0])
715 goto error;
717 return up;
718 error:
719 isl_upoly_free(up);
720 return NULL;
723 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
724 __isl_take struct isl_upoly *up, isl_int v)
726 struct isl_upoly_cst *cst;
728 if (isl_upoly_is_zero(up))
729 return up;
731 up = isl_upoly_cow(up);
732 if (!up)
733 return NULL;
735 cst = isl_upoly_as_cst(up);
737 isl_int_mul(cst->n, cst->n, v);
739 return up;
742 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
743 __isl_take struct isl_upoly *up, isl_int v)
745 int i;
746 struct isl_upoly_rec *rec;
748 if (!up)
749 return NULL;
751 if (isl_upoly_is_cst(up))
752 return isl_upoly_cst_mul_isl_int(up, v);
754 up = isl_upoly_cow(up);
755 rec = isl_upoly_as_rec(up);
756 if (!rec)
757 goto error;
759 for (i = 0; i < rec->n; ++i) {
760 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
761 if (!rec->p[i])
762 goto error;
765 return up;
766 error:
767 isl_upoly_free(up);
768 return NULL;
771 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
772 __isl_take struct isl_upoly *up2)
774 struct isl_upoly_cst *cst1;
775 struct isl_upoly_cst *cst2;
777 up1 = isl_upoly_cow(up1);
778 if (!up1 || !up2)
779 goto error;
781 cst1 = isl_upoly_as_cst(up1);
782 cst2 = isl_upoly_as_cst(up2);
784 isl_int_mul(cst1->n, cst1->n, cst2->n);
785 isl_int_mul(cst1->d, cst1->d, cst2->d);
787 isl_upoly_cst_reduce(cst1);
789 isl_upoly_free(up2);
790 return up1;
791 error:
792 isl_upoly_free(up1);
793 isl_upoly_free(up2);
794 return NULL;
797 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
798 __isl_take struct isl_upoly *up2)
800 struct isl_upoly_rec *rec1;
801 struct isl_upoly_rec *rec2;
802 struct isl_upoly_rec *res = NULL;
803 int i, j;
804 int size;
806 rec1 = isl_upoly_as_rec(up1);
807 rec2 = isl_upoly_as_rec(up2);
808 if (!rec1 || !rec2)
809 goto error;
810 size = rec1->n + rec2->n - 1;
811 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
812 if (!res)
813 goto error;
815 for (i = 0; i < rec1->n; ++i) {
816 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
817 isl_upoly_copy(rec1->p[i]));
818 if (!res->p[i])
819 goto error;
820 res->n++;
822 for (; i < size; ++i) {
823 res->p[i] = isl_upoly_zero(up1->ctx);
824 if (!res->p[i])
825 goto error;
826 res->n++;
828 for (i = 0; i < rec1->n; ++i) {
829 for (j = 1; j < rec2->n; ++j) {
830 struct isl_upoly *up;
831 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
832 isl_upoly_copy(rec1->p[i]));
833 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
834 if (!res->p[i + j])
835 goto error;
839 isl_upoly_free(up1);
840 isl_upoly_free(up2);
842 return &res->up;
843 error:
844 isl_upoly_free(up1);
845 isl_upoly_free(up2);
846 isl_upoly_free(&res->up);
847 return NULL;
850 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
851 __isl_take struct isl_upoly *up2)
853 if (!up1 || !up2)
854 goto error;
856 if (isl_upoly_is_nan(up1)) {
857 isl_upoly_free(up2);
858 return up1;
861 if (isl_upoly_is_nan(up2)) {
862 isl_upoly_free(up1);
863 return up2;
866 if (isl_upoly_is_zero(up1)) {
867 isl_upoly_free(up2);
868 return up1;
871 if (isl_upoly_is_zero(up2)) {
872 isl_upoly_free(up1);
873 return up2;
876 if (isl_upoly_is_one(up1)) {
877 isl_upoly_free(up1);
878 return up2;
881 if (isl_upoly_is_one(up2)) {
882 isl_upoly_free(up2);
883 return up1;
886 if (up1->var < up2->var)
887 return isl_upoly_mul(up2, up1);
889 if (up2->var < up1->var) {
890 int i;
891 struct isl_upoly_rec *rec;
892 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
893 isl_ctx *ctx = up1->ctx;
894 isl_upoly_free(up1);
895 isl_upoly_free(up2);
896 return isl_upoly_nan(ctx);
898 up1 = isl_upoly_cow(up1);
899 rec = isl_upoly_as_rec(up1);
900 if (!rec)
901 goto error;
903 for (i = 0; i < rec->n; ++i) {
904 rec->p[i] = isl_upoly_mul(rec->p[i],
905 isl_upoly_copy(up2));
906 if (!rec->p[i])
907 goto error;
909 isl_upoly_free(up2);
910 return up1;
913 if (isl_upoly_is_cst(up1))
914 return isl_upoly_mul_cst(up1, up2);
916 return isl_upoly_mul_rec(up1, up2);
917 error:
918 isl_upoly_free(up1);
919 isl_upoly_free(up2);
920 return NULL;
923 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
924 unsigned power)
926 struct isl_upoly *res;
928 if (!up)
929 return NULL;
930 if (power == 1)
931 return up;
933 if (power % 2)
934 res = isl_upoly_copy(up);
935 else
936 res = isl_upoly_one(up->ctx);
938 while (power >>= 1) {
939 up = isl_upoly_mul(up, isl_upoly_copy(up));
940 if (power % 2)
941 res = isl_upoly_mul(res, isl_upoly_copy(up));
944 isl_upoly_free(up);
945 return res;
948 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
949 unsigned n_div, __isl_take struct isl_upoly *up)
951 struct isl_qpolynomial *qp = NULL;
952 unsigned total;
954 if (!dim || !up)
955 goto error;
957 total = isl_dim_total(dim);
959 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
960 if (!qp)
961 goto error;
963 qp->ref = 1;
964 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
965 if (!qp->div)
966 goto error;
968 qp->dim = dim;
969 qp->upoly = up;
971 return qp;
972 error:
973 isl_dim_free(dim);
974 isl_upoly_free(up);
975 isl_qpolynomial_free(qp);
976 return NULL;
979 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
981 if (!qp)
982 return NULL;
984 qp->ref++;
985 return qp;
988 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
990 struct isl_qpolynomial *dup;
992 if (!qp)
993 return NULL;
995 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
996 isl_upoly_copy(qp->upoly));
997 if (!dup)
998 return NULL;
999 isl_mat_free(dup->div);
1000 dup->div = isl_mat_copy(qp->div);
1001 if (!dup->div)
1002 goto error;
1004 return dup;
1005 error:
1006 isl_qpolynomial_free(dup);
1007 return NULL;
1010 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1012 if (!qp)
1013 return NULL;
1015 if (qp->ref == 1)
1016 return qp;
1017 qp->ref--;
1018 return isl_qpolynomial_dup(qp);
1021 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1023 if (!qp)
1024 return;
1026 if (--qp->ref > 0)
1027 return;
1029 isl_dim_free(qp->dim);
1030 isl_mat_free(qp->div);
1031 isl_upoly_free(qp->upoly);
1033 free(qp);
1036 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1038 int i;
1039 struct isl_upoly_rec *rec;
1040 struct isl_upoly_cst *cst;
1042 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1043 if (!rec)
1044 return NULL;
1045 for (i = 0; i < 1 + power; ++i) {
1046 rec->p[i] = isl_upoly_zero(ctx);
1047 if (!rec->p[i])
1048 goto error;
1049 rec->n++;
1051 cst = isl_upoly_as_cst(rec->p[power]);
1052 isl_int_set_si(cst->n, 1);
1054 return &rec->up;
1055 error:
1056 isl_upoly_free(&rec->up);
1057 return NULL;
1060 /* r array maps original positions to new positions.
1062 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1063 int *r)
1065 int i;
1066 struct isl_upoly_rec *rec;
1067 struct isl_upoly *base;
1068 struct isl_upoly *res;
1070 if (isl_upoly_is_cst(up))
1071 return up;
1073 rec = isl_upoly_as_rec(up);
1074 if (!rec)
1075 goto error;
1077 isl_assert(up->ctx, rec->n >= 1, goto error);
1079 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1080 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1082 for (i = rec->n - 2; i >= 0; --i) {
1083 res = isl_upoly_mul(res, isl_upoly_copy(base));
1084 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1087 isl_upoly_free(base);
1088 isl_upoly_free(up);
1090 return res;
1091 error:
1092 isl_upoly_free(up);
1093 return NULL;
1096 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1098 int n_row, n_col;
1099 int equal;
1101 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1102 div1->n_col >= div2->n_col, return -1);
1104 if (div1->n_row == div2->n_row)
1105 return isl_mat_is_equal(div1, div2);
1107 n_row = div1->n_row;
1108 n_col = div1->n_col;
1109 div1->n_row = div2->n_row;
1110 div1->n_col = div2->n_col;
1112 equal = isl_mat_is_equal(div1, div2);
1114 div1->n_row = n_row;
1115 div1->n_col = n_col;
1117 return equal;
1120 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1122 int li, lj;
1124 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1125 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1127 if (li != lj)
1128 return li - lj;
1130 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1133 struct isl_div_sort_info {
1134 isl_mat *div;
1135 int row;
1138 static int div_sort_cmp(const void *p1, const void *p2)
1140 const struct isl_div_sort_info *i1, *i2;
1141 i1 = (const struct isl_div_sort_info *) p1;
1142 i2 = (const struct isl_div_sort_info *) p2;
1144 return cmp_row(i1->div, i1->row, i2->row);
1147 /* Sort divs and remove duplicates.
1149 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1151 int i;
1152 int skip;
1153 int len;
1154 struct isl_div_sort_info *array = NULL;
1155 int *pos = NULL, *at = NULL;
1156 int *reordering = NULL;
1157 unsigned div_pos;
1159 if (!qp)
1160 return NULL;
1161 if (qp->div->n_row <= 1)
1162 return qp;
1164 div_pos = isl_dim_total(qp->dim);
1166 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1167 qp->div->n_row);
1168 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1169 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1170 len = qp->div->n_col - 2;
1171 reordering = isl_alloc_array(qp->div->ctx, int, len);
1172 if (!array || !pos || !at || !reordering)
1173 goto error;
1175 for (i = 0; i < qp->div->n_row; ++i) {
1176 array[i].div = qp->div;
1177 array[i].row = i;
1178 pos[i] = i;
1179 at[i] = i;
1182 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1183 div_sort_cmp);
1185 for (i = 0; i < div_pos; ++i)
1186 reordering[i] = i;
1188 for (i = 0; i < qp->div->n_row; ++i) {
1189 if (pos[array[i].row] == i)
1190 continue;
1191 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1192 pos[at[i]] = pos[array[i].row];
1193 at[pos[array[i].row]] = at[i];
1194 at[i] = array[i].row;
1195 pos[array[i].row] = i;
1198 skip = 0;
1199 for (i = 0; i < len - div_pos; ++i) {
1200 if (i > 0 &&
1201 isl_seq_eq(qp->div->row[i - skip - 1],
1202 qp->div->row[i - skip], qp->div->n_col)) {
1203 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1204 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1205 2 + div_pos + i - skip);
1206 qp->div = isl_mat_drop_cols(qp->div,
1207 2 + div_pos + i - skip, 1);
1208 skip++;
1210 reordering[div_pos + array[i].row] = div_pos + i - skip;
1213 qp->upoly = reorder(qp->upoly, reordering);
1215 if (!qp->upoly || !qp->div)
1216 goto error;
1218 free(at);
1219 free(pos);
1220 free(array);
1221 free(reordering);
1223 return qp;
1224 error:
1225 free(at);
1226 free(pos);
1227 free(array);
1228 free(reordering);
1229 isl_qpolynomial_free(qp);
1230 return NULL;
1233 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1234 int *exp, int first)
1236 int i;
1237 struct isl_upoly_rec *rec;
1239 if (isl_upoly_is_cst(up))
1240 return up;
1242 if (up->var < first)
1243 return up;
1245 if (exp[up->var - first] == up->var - first)
1246 return up;
1248 up = isl_upoly_cow(up);
1249 if (!up)
1250 goto error;
1252 up->var = exp[up->var - first] + first;
1254 rec = isl_upoly_as_rec(up);
1255 if (!rec)
1256 goto error;
1258 for (i = 0; i < rec->n; ++i) {
1259 rec->p[i] = expand(rec->p[i], exp, first);
1260 if (!rec->p[i])
1261 goto error;
1264 return up;
1265 error:
1266 isl_upoly_free(up);
1267 return NULL;
1270 static __isl_give isl_qpolynomial *with_merged_divs(
1271 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1272 __isl_take isl_qpolynomial *qp2),
1273 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1275 int *exp1 = NULL;
1276 int *exp2 = NULL;
1277 isl_mat *div = NULL;
1279 qp1 = isl_qpolynomial_cow(qp1);
1280 qp2 = isl_qpolynomial_cow(qp2);
1282 if (!qp1 || !qp2)
1283 goto error;
1285 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1286 qp1->div->n_col >= qp2->div->n_col, goto error);
1288 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1289 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1290 if (!exp1 || !exp2)
1291 goto error;
1293 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1294 if (!div)
1295 goto error;
1297 isl_mat_free(qp1->div);
1298 qp1->div = isl_mat_copy(div);
1299 isl_mat_free(qp2->div);
1300 qp2->div = isl_mat_copy(div);
1302 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1303 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1305 if (!qp1->upoly || !qp2->upoly)
1306 goto error;
1308 isl_mat_free(div);
1309 free(exp1);
1310 free(exp2);
1312 return fn(qp1, qp2);
1313 error:
1314 isl_mat_free(div);
1315 free(exp1);
1316 free(exp2);
1317 isl_qpolynomial_free(qp1);
1318 isl_qpolynomial_free(qp2);
1319 return NULL;
1322 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1323 __isl_take isl_qpolynomial *qp2)
1325 qp1 = isl_qpolynomial_cow(qp1);
1327 if (!qp1 || !qp2)
1328 goto error;
1330 if (qp1->div->n_row < qp2->div->n_row)
1331 return isl_qpolynomial_add(qp2, qp1);
1333 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1334 if (!compatible_divs(qp1->div, qp2->div))
1335 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1337 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1338 if (!qp1->upoly)
1339 goto error;
1341 isl_qpolynomial_free(qp2);
1343 return qp1;
1344 error:
1345 isl_qpolynomial_free(qp1);
1346 isl_qpolynomial_free(qp2);
1347 return NULL;
1350 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1351 __isl_keep isl_set *dom,
1352 __isl_take isl_qpolynomial *qp1,
1353 __isl_take isl_qpolynomial *qp2)
1355 qp1 = isl_qpolynomial_add(qp1, qp2);
1356 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1357 return qp1;
1360 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1361 __isl_take isl_qpolynomial *qp2)
1363 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1366 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1367 __isl_take isl_qpolynomial *qp, isl_int v)
1369 if (isl_int_is_zero(v))
1370 return qp;
1372 qp = isl_qpolynomial_cow(qp);
1373 if (!qp)
1374 return NULL;
1376 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1377 if (!qp->upoly)
1378 goto error;
1380 return qp;
1381 error:
1382 isl_qpolynomial_free(qp);
1383 return NULL;
1387 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1389 if (!qp)
1390 return NULL;
1392 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1395 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1396 __isl_take isl_qpolynomial *qp, isl_int v)
1398 if (isl_int_is_one(v))
1399 return qp;
1401 if (qp && isl_int_is_zero(v)) {
1402 isl_qpolynomial *zero;
1403 zero = isl_qpolynomial_zero(isl_dim_copy(qp->dim));
1404 isl_qpolynomial_free(qp);
1405 return zero;
1408 qp = isl_qpolynomial_cow(qp);
1409 if (!qp)
1410 return NULL;
1412 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1413 if (!qp->upoly)
1414 goto error;
1416 return qp;
1417 error:
1418 isl_qpolynomial_free(qp);
1419 return NULL;
1422 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1423 __isl_take isl_qpolynomial *qp, isl_int v)
1425 return isl_qpolynomial_mul_isl_int(qp, v);
1428 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1429 __isl_take isl_qpolynomial *qp2)
1431 qp1 = isl_qpolynomial_cow(qp1);
1433 if (!qp1 || !qp2)
1434 goto error;
1436 if (qp1->div->n_row < qp2->div->n_row)
1437 return isl_qpolynomial_mul(qp2, qp1);
1439 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1440 if (!compatible_divs(qp1->div, qp2->div))
1441 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1443 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1444 if (!qp1->upoly)
1445 goto error;
1447 isl_qpolynomial_free(qp2);
1449 return qp1;
1450 error:
1451 isl_qpolynomial_free(qp1);
1452 isl_qpolynomial_free(qp2);
1453 return NULL;
1456 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1457 unsigned power)
1459 qp = isl_qpolynomial_cow(qp);
1461 if (!qp)
1462 return NULL;
1464 qp->upoly = isl_upoly_pow(qp->upoly, power);
1465 if (!qp->upoly)
1466 goto error;
1468 return qp;
1469 error:
1470 isl_qpolynomial_free(qp);
1471 return NULL;
1474 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1475 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1477 int i;
1479 if (power == 1)
1480 return pwqp;
1482 pwqp = isl_pw_qpolynomial_cow(pwqp);
1483 if (!pwqp)
1484 return NULL;
1486 for (i = 0; i < pwqp->n; ++i) {
1487 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1488 if (!pwqp->p[i].qp)
1489 return isl_pw_qpolynomial_free(pwqp);
1492 return pwqp;
1495 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1497 if (!dim)
1498 return NULL;
1499 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1502 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1504 if (!dim)
1505 return NULL;
1506 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1509 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1511 if (!dim)
1512 return NULL;
1513 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1516 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1518 if (!dim)
1519 return NULL;
1520 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1523 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1525 if (!dim)
1526 return NULL;
1527 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1530 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1531 isl_int v)
1533 struct isl_qpolynomial *qp;
1534 struct isl_upoly_cst *cst;
1536 if (!dim)
1537 return NULL;
1539 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1540 if (!qp)
1541 return NULL;
1543 cst = isl_upoly_as_cst(qp->upoly);
1544 isl_int_set(cst->n, v);
1546 return qp;
1549 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1550 isl_int *n, isl_int *d)
1552 struct isl_upoly_cst *cst;
1554 if (!qp)
1555 return -1;
1557 if (!isl_upoly_is_cst(qp->upoly))
1558 return 0;
1560 cst = isl_upoly_as_cst(qp->upoly);
1561 if (!cst)
1562 return -1;
1564 if (n)
1565 isl_int_set(*n, cst->n);
1566 if (d)
1567 isl_int_set(*d, cst->d);
1569 return 1;
1572 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1574 int is_cst;
1575 struct isl_upoly_rec *rec;
1577 if (!up)
1578 return -1;
1580 if (up->var < 0)
1581 return 1;
1583 rec = isl_upoly_as_rec(up);
1584 if (!rec)
1585 return -1;
1587 if (rec->n > 2)
1588 return 0;
1590 isl_assert(up->ctx, rec->n > 1, return -1);
1592 is_cst = isl_upoly_is_cst(rec->p[1]);
1593 if (is_cst < 0)
1594 return -1;
1595 if (!is_cst)
1596 return 0;
1598 return isl_upoly_is_affine(rec->p[0]);
1601 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1603 if (!qp)
1604 return -1;
1606 if (qp->div->n_row > 0)
1607 return 0;
1609 return isl_upoly_is_affine(qp->upoly);
1612 static void update_coeff(__isl_keep isl_vec *aff,
1613 __isl_keep struct isl_upoly_cst *cst, int pos)
1615 isl_int gcd;
1616 isl_int f;
1618 if (isl_int_is_zero(cst->n))
1619 return;
1621 isl_int_init(gcd);
1622 isl_int_init(f);
1623 isl_int_gcd(gcd, cst->d, aff->el[0]);
1624 isl_int_divexact(f, cst->d, gcd);
1625 isl_int_divexact(gcd, aff->el[0], gcd);
1626 isl_seq_scale(aff->el, aff->el, f, aff->size);
1627 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1628 isl_int_clear(gcd);
1629 isl_int_clear(f);
1632 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1633 __isl_keep isl_vec *aff)
1635 struct isl_upoly_cst *cst;
1636 struct isl_upoly_rec *rec;
1638 if (!up || !aff)
1639 return -1;
1641 if (up->var < 0) {
1642 struct isl_upoly_cst *cst;
1644 cst = isl_upoly_as_cst(up);
1645 if (!cst)
1646 return -1;
1647 update_coeff(aff, cst, 0);
1648 return 0;
1651 rec = isl_upoly_as_rec(up);
1652 if (!rec)
1653 return -1;
1654 isl_assert(up->ctx, rec->n == 2, return -1);
1656 cst = isl_upoly_as_cst(rec->p[1]);
1657 if (!cst)
1658 return -1;
1659 update_coeff(aff, cst, 1 + up->var);
1661 return isl_upoly_update_affine(rec->p[0], aff);
1664 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1665 __isl_keep isl_qpolynomial *qp)
1667 isl_vec *aff;
1668 unsigned d;
1670 if (!qp)
1671 return NULL;
1673 d = isl_dim_total(qp->dim);
1674 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1675 if (!aff)
1676 return NULL;
1678 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1679 isl_int_set_si(aff->el[0], 1);
1681 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1682 goto error;
1684 return aff;
1685 error:
1686 isl_vec_free(aff);
1687 return NULL;
1690 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
1691 __isl_keep isl_qpolynomial *qp2)
1693 int equal;
1695 if (!qp1 || !qp2)
1696 return -1;
1698 equal = isl_dim_equal(qp1->dim, qp2->dim);
1699 if (equal < 0 || !equal)
1700 return equal;
1702 equal = isl_mat_is_equal(qp1->div, qp2->div);
1703 if (equal < 0 || !equal)
1704 return equal;
1706 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1709 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1711 int i;
1712 struct isl_upoly_rec *rec;
1714 if (isl_upoly_is_cst(up)) {
1715 struct isl_upoly_cst *cst;
1716 cst = isl_upoly_as_cst(up);
1717 if (!cst)
1718 return;
1719 isl_int_lcm(*d, *d, cst->d);
1720 return;
1723 rec = isl_upoly_as_rec(up);
1724 if (!rec)
1725 return;
1727 for (i = 0; i < rec->n; ++i)
1728 upoly_update_den(rec->p[i], d);
1731 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1733 isl_int_set_si(*d, 1);
1734 if (!qp)
1735 return;
1736 upoly_update_den(qp->upoly, d);
1739 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1740 int pos, int power)
1742 struct isl_ctx *ctx;
1744 if (!dim)
1745 return NULL;
1747 ctx = dim->ctx;
1749 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1752 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1753 enum isl_dim_type type, unsigned pos)
1755 if (!dim)
1756 return NULL;
1758 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1759 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1761 if (type == isl_dim_set)
1762 pos += isl_dim_size(dim, isl_dim_param);
1764 return isl_qpolynomial_var_pow(dim, pos, 1);
1765 error:
1766 isl_dim_free(dim);
1767 return NULL;
1770 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1771 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1773 int i;
1774 struct isl_upoly_rec *rec;
1775 struct isl_upoly *base, *res;
1777 if (!up)
1778 return NULL;
1780 if (isl_upoly_is_cst(up))
1781 return up;
1783 if (up->var < first)
1784 return up;
1786 rec = isl_upoly_as_rec(up);
1787 if (!rec)
1788 goto error;
1790 isl_assert(up->ctx, rec->n >= 1, goto error);
1792 if (up->var >= first + n)
1793 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1794 else
1795 base = isl_upoly_copy(subs[up->var - first]);
1797 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1798 for (i = rec->n - 2; i >= 0; --i) {
1799 struct isl_upoly *t;
1800 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1801 res = isl_upoly_mul(res, isl_upoly_copy(base));
1802 res = isl_upoly_sum(res, t);
1805 isl_upoly_free(base);
1806 isl_upoly_free(up);
1808 return res;
1809 error:
1810 isl_upoly_free(up);
1811 return NULL;
1814 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1815 isl_int denom, unsigned len)
1817 int i;
1818 struct isl_upoly *up;
1820 isl_assert(ctx, len >= 1, return NULL);
1822 up = isl_upoly_rat_cst(ctx, f[0], denom);
1823 for (i = 0; i < len - 1; ++i) {
1824 struct isl_upoly *t;
1825 struct isl_upoly *c;
1827 if (isl_int_is_zero(f[1 + i]))
1828 continue;
1830 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1831 t = isl_upoly_var_pow(ctx, i, 1);
1832 t = isl_upoly_mul(c, t);
1833 up = isl_upoly_sum(up, t);
1836 return up;
1839 /* Remove common factor of non-constant terms and denominator.
1841 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1843 isl_ctx *ctx = qp->div->ctx;
1844 unsigned total = qp->div->n_col - 2;
1846 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1847 isl_int_gcd(ctx->normalize_gcd,
1848 ctx->normalize_gcd, qp->div->row[div][0]);
1849 if (isl_int_is_one(ctx->normalize_gcd))
1850 return;
1852 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1853 ctx->normalize_gcd, total);
1854 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1855 ctx->normalize_gcd);
1856 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1857 ctx->normalize_gcd);
1860 /* Replace the integer division identified by "div" by the polynomial "s".
1861 * The integer division is assumed not to appear in the definition
1862 * of any other integer divisions.
1864 static __isl_give isl_qpolynomial *substitute_div(
1865 __isl_take isl_qpolynomial *qp,
1866 int div, __isl_take struct isl_upoly *s)
1868 int i;
1869 int total;
1870 int *reordering;
1872 if (!qp || !s)
1873 goto error;
1875 qp = isl_qpolynomial_cow(qp);
1876 if (!qp)
1877 goto error;
1879 total = isl_dim_total(qp->dim);
1880 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1881 if (!qp->upoly)
1882 goto error;
1884 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1885 if (!reordering)
1886 goto error;
1887 for (i = 0; i < total + div; ++i)
1888 reordering[i] = i;
1889 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1890 reordering[i] = i - 1;
1891 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1892 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1893 qp->upoly = reorder(qp->upoly, reordering);
1894 free(reordering);
1896 if (!qp->upoly || !qp->div)
1897 goto error;
1899 isl_upoly_free(s);
1900 return qp;
1901 error:
1902 isl_qpolynomial_free(qp);
1903 isl_upoly_free(s);
1904 return NULL;
1907 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1908 * divisions because d is equal to 1 by their definition, i.e., e.
1910 static __isl_give isl_qpolynomial *substitute_non_divs(
1911 __isl_take isl_qpolynomial *qp)
1913 int i, j;
1914 int total;
1915 struct isl_upoly *s;
1917 if (!qp)
1918 return NULL;
1920 total = isl_dim_total(qp->dim);
1921 for (i = 0; qp && i < qp->div->n_row; ++i) {
1922 if (!isl_int_is_one(qp->div->row[i][0]))
1923 continue;
1924 for (j = i + 1; j < qp->div->n_row; ++j) {
1925 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1926 continue;
1927 isl_seq_combine(qp->div->row[j] + 1,
1928 qp->div->ctx->one, qp->div->row[j] + 1,
1929 qp->div->row[j][2 + total + i],
1930 qp->div->row[i] + 1, 1 + total + i);
1931 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1932 normalize_div(qp, j);
1934 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1935 qp->div->row[i][0], qp->div->n_col - 1);
1936 qp = substitute_div(qp, i, s);
1937 --i;
1940 return qp;
1943 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1944 * with d the denominator. When replacing the coefficient e of x by
1945 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1946 * inside the division, so we need to add floor(e/d) * x outside.
1947 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1948 * to adjust the coefficient of x in each later div that depends on the
1949 * current div "div" and also in the affine expression "aff"
1950 * (if it too depends on "div").
1952 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1953 __isl_keep isl_vec *aff)
1955 int i, j;
1956 isl_int v;
1957 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1959 isl_int_init(v);
1960 for (i = 0; i < 1 + total + div; ++i) {
1961 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1962 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1963 continue;
1964 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1965 isl_int_fdiv_r(qp->div->row[div][1 + i],
1966 qp->div->row[div][1 + i], qp->div->row[div][0]);
1967 if (!isl_int_is_zero(aff->el[1 + total + div]))
1968 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1969 for (j = div + 1; j < qp->div->n_row; ++j) {
1970 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1971 continue;
1972 isl_int_addmul(qp->div->row[j][1 + i],
1973 v, qp->div->row[j][2 + total + div]);
1976 isl_int_clear(v);
1979 /* Check if the last non-zero coefficient is bigger that half of the
1980 * denominator. If so, we will invert the div to further reduce the number
1981 * of distinct divs that may appear.
1982 * If the last non-zero coefficient is exactly half the denominator,
1983 * then we continue looking for earlier coefficients that are bigger
1984 * than half the denominator.
1986 static int needs_invert(__isl_keep isl_mat *div, int row)
1988 int i;
1989 int cmp;
1991 for (i = div->n_col - 1; i >= 1; --i) {
1992 if (isl_int_is_zero(div->row[row][i]))
1993 continue;
1994 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1995 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
1996 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
1997 if (cmp)
1998 return cmp > 0;
1999 if (i == 1)
2000 return 1;
2003 return 0;
2006 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2007 * We only invert the coefficients of e (and the coefficient of q in
2008 * later divs and in "aff"). After calling this function, the
2009 * coefficients of e should be reduced again.
2011 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2012 __isl_keep isl_vec *aff)
2014 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2016 isl_seq_neg(qp->div->row[div] + 1,
2017 qp->div->row[div] + 1, qp->div->n_col - 1);
2018 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2019 isl_int_add(qp->div->row[div][1],
2020 qp->div->row[div][1], qp->div->row[div][0]);
2021 if (!isl_int_is_zero(aff->el[1 + total + div]))
2022 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2023 isl_mat_col_mul(qp->div, 2 + total + div,
2024 qp->div->ctx->negone, 2 + total + div);
2027 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2028 * in the interval [0, d-1], with d the denominator and such that the
2029 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2031 * After the reduction, some divs may have become redundant or identical,
2032 * so we call substitute_non_divs and sort_divs. If these functions
2033 * eliminate divs or merge two or more divs into one, the coefficients
2034 * of the enclosing divs may have to be reduced again, so we call
2035 * ourselves recursively if the number of divs decreases.
2037 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2039 int i;
2040 isl_vec *aff = NULL;
2041 struct isl_upoly *s;
2042 unsigned n_div;
2044 if (!qp)
2045 return NULL;
2047 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2048 aff = isl_vec_clr(aff);
2049 if (!aff)
2050 goto error;
2052 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2054 for (i = 0; i < qp->div->n_row; ++i) {
2055 normalize_div(qp, i);
2056 reduce_div(qp, i, aff);
2057 if (needs_invert(qp->div, i)) {
2058 invert_div(qp, i, aff);
2059 reduce_div(qp, i, aff);
2063 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2064 qp->div->ctx->one, aff->size);
2065 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2066 isl_upoly_free(s);
2067 if (!qp->upoly)
2068 goto error;
2070 isl_vec_free(aff);
2072 n_div = qp->div->n_row;
2073 qp = substitute_non_divs(qp);
2074 qp = sort_divs(qp);
2075 if (qp && qp->div->n_row < n_div)
2076 return reduce_divs(qp);
2078 return qp;
2079 error:
2080 isl_qpolynomial_free(qp);
2081 isl_vec_free(aff);
2082 return NULL;
2085 /* Assumes each div only depends on earlier divs.
2087 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2088 int power)
2090 struct isl_qpolynomial *qp = NULL;
2091 struct isl_upoly_rec *rec;
2092 struct isl_upoly_cst *cst;
2093 int i, d;
2094 int pos;
2096 if (!div)
2097 return NULL;
2099 d = div->line - div->bmap->div;
2101 pos = isl_dim_total(div->bmap->dim) + d;
2102 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2103 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2104 div->bmap->n_div, &rec->up);
2105 if (!qp)
2106 goto error;
2108 for (i = 0; i < div->bmap->n_div; ++i)
2109 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2111 for (i = 0; i < 1 + power; ++i) {
2112 rec->p[i] = isl_upoly_zero(div->ctx);
2113 if (!rec->p[i])
2114 goto error;
2115 rec->n++;
2117 cst = isl_upoly_as_cst(rec->p[power]);
2118 isl_int_set_si(cst->n, 1);
2120 isl_div_free(div);
2122 qp = reduce_divs(qp);
2124 return qp;
2125 error:
2126 isl_qpolynomial_free(qp);
2127 isl_div_free(div);
2128 return NULL;
2131 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2133 return isl_qpolynomial_div_pow(div, 1);
2136 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2137 const isl_int n, const isl_int d)
2139 struct isl_qpolynomial *qp;
2140 struct isl_upoly_cst *cst;
2142 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2143 if (!qp)
2144 return NULL;
2146 cst = isl_upoly_as_cst(qp->upoly);
2147 isl_int_set(cst->n, n);
2148 isl_int_set(cst->d, d);
2150 return qp;
2153 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2155 struct isl_upoly_rec *rec;
2156 int i;
2158 if (!up)
2159 return -1;
2161 if (isl_upoly_is_cst(up))
2162 return 0;
2164 if (up->var < d)
2165 active[up->var] = 1;
2167 rec = isl_upoly_as_rec(up);
2168 for (i = 0; i < rec->n; ++i)
2169 if (up_set_active(rec->p[i], active, d) < 0)
2170 return -1;
2172 return 0;
2175 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2177 int i, j;
2178 int d = isl_dim_total(qp->dim);
2180 if (!qp || !active)
2181 return -1;
2183 for (i = 0; i < d; ++i)
2184 for (j = 0; j < qp->div->n_row; ++j) {
2185 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2186 continue;
2187 active[i] = 1;
2188 break;
2191 return up_set_active(qp->upoly, active, d);
2194 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2195 enum isl_dim_type type, unsigned first, unsigned n)
2197 int i;
2198 int *active = NULL;
2199 int involves = 0;
2201 if (!qp)
2202 return -1;
2203 if (n == 0)
2204 return 0;
2206 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2207 return -1);
2208 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2209 type == isl_dim_set, return -1);
2211 active = isl_calloc_array(qp->dim->ctx, int, isl_dim_total(qp->dim));
2212 if (set_active(qp, active) < 0)
2213 goto error;
2215 if (type == isl_dim_set)
2216 first += isl_dim_size(qp->dim, isl_dim_param);
2217 for (i = 0; i < n; ++i)
2218 if (active[first + i]) {
2219 involves = 1;
2220 break;
2223 free(active);
2225 return involves;
2226 error:
2227 free(active);
2228 return -1;
2231 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2232 * of the divs that do appear in the quasi-polynomial.
2234 static __isl_give isl_qpolynomial *remove_redundant_divs(
2235 __isl_take isl_qpolynomial *qp)
2237 int i, j;
2238 int d;
2239 int len;
2240 int skip;
2241 int *active = NULL;
2242 int *reordering = NULL;
2243 int redundant = 0;
2244 int n_div;
2245 isl_ctx *ctx;
2247 if (!qp)
2248 return NULL;
2249 if (qp->div->n_row == 0)
2250 return qp;
2252 d = isl_dim_total(qp->dim);
2253 len = qp->div->n_col - 2;
2254 ctx = isl_qpolynomial_get_ctx(qp);
2255 active = isl_calloc_array(ctx, int, len);
2256 if (!active)
2257 goto error;
2259 if (up_set_active(qp->upoly, active, len) < 0)
2260 goto error;
2262 for (i = qp->div->n_row - 1; i >= 0; --i) {
2263 if (!active[d + i]) {
2264 redundant = 1;
2265 continue;
2267 for (j = 0; j < i; ++j) {
2268 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2269 continue;
2270 active[d + j] = 1;
2271 break;
2275 if (!redundant) {
2276 free(active);
2277 return qp;
2280 reordering = isl_alloc_array(qp->div->ctx, int, len);
2281 if (!reordering)
2282 goto error;
2284 for (i = 0; i < d; ++i)
2285 reordering[i] = i;
2287 skip = 0;
2288 n_div = qp->div->n_row;
2289 for (i = 0; i < n_div; ++i) {
2290 if (!active[d + i]) {
2291 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2292 qp->div = isl_mat_drop_cols(qp->div,
2293 2 + d + i - skip, 1);
2294 skip++;
2296 reordering[d + i] = d + i - skip;
2299 qp->upoly = reorder(qp->upoly, reordering);
2301 if (!qp->upoly || !qp->div)
2302 goto error;
2304 free(active);
2305 free(reordering);
2307 return qp;
2308 error:
2309 free(active);
2310 free(reordering);
2311 isl_qpolynomial_free(qp);
2312 return NULL;
2315 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2316 unsigned first, unsigned n)
2318 int i;
2319 struct isl_upoly_rec *rec;
2321 if (!up)
2322 return NULL;
2323 if (n == 0 || up->var < 0 || up->var < first)
2324 return up;
2325 if (up->var < first + n) {
2326 up = replace_by_constant_term(up);
2327 return isl_upoly_drop(up, first, n);
2329 up = isl_upoly_cow(up);
2330 if (!up)
2331 return NULL;
2332 up->var -= n;
2333 rec = isl_upoly_as_rec(up);
2334 if (!rec)
2335 goto error;
2337 for (i = 0; i < rec->n; ++i) {
2338 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2339 if (!rec->p[i])
2340 goto error;
2343 return up;
2344 error:
2345 isl_upoly_free(up);
2346 return NULL;
2349 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2350 __isl_take isl_qpolynomial *qp,
2351 enum isl_dim_type type, unsigned pos, const char *s)
2353 qp = isl_qpolynomial_cow(qp);
2354 if (!qp)
2355 return NULL;
2356 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2357 if (!qp->dim)
2358 goto error;
2359 return qp;
2360 error:
2361 isl_qpolynomial_free(qp);
2362 return NULL;
2365 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2366 __isl_take isl_qpolynomial *qp,
2367 enum isl_dim_type type, unsigned first, unsigned n)
2369 if (!qp)
2370 return NULL;
2371 if (n == 0 && !isl_dim_is_named_or_nested(qp->dim, type))
2372 return qp;
2374 qp = isl_qpolynomial_cow(qp);
2375 if (!qp)
2376 return NULL;
2378 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2379 goto error);
2380 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2381 type == isl_dim_set, goto error);
2383 qp->dim = isl_dim_drop(qp->dim, type, first, n);
2384 if (!qp->dim)
2385 goto error;
2387 if (type == isl_dim_set)
2388 first += isl_dim_size(qp->dim, isl_dim_param);
2390 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2391 if (!qp->div)
2392 goto error;
2394 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2395 if (!qp->upoly)
2396 goto error;
2398 return qp;
2399 error:
2400 isl_qpolynomial_free(qp);
2401 return NULL;
2404 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2405 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2407 int i, j, k;
2408 isl_int denom;
2409 unsigned total;
2410 unsigned n_div;
2411 struct isl_upoly *up;
2413 if (!eq)
2414 goto error;
2415 if (eq->n_eq == 0) {
2416 isl_basic_set_free(eq);
2417 return qp;
2420 qp = isl_qpolynomial_cow(qp);
2421 if (!qp)
2422 goto error;
2423 qp->div = isl_mat_cow(qp->div);
2424 if (!qp->div)
2425 goto error;
2427 total = 1 + isl_dim_total(eq->dim);
2428 n_div = eq->n_div;
2429 isl_int_init(denom);
2430 for (i = 0; i < eq->n_eq; ++i) {
2431 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2432 if (j < 0 || j == 0 || j >= total)
2433 continue;
2435 for (k = 0; k < qp->div->n_row; ++k) {
2436 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2437 continue;
2438 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2439 &qp->div->row[k][0]);
2440 normalize_div(qp, k);
2443 if (isl_int_is_pos(eq->eq[i][j]))
2444 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2445 isl_int_abs(denom, eq->eq[i][j]);
2446 isl_int_set_si(eq->eq[i][j], 0);
2448 up = isl_upoly_from_affine(qp->dim->ctx,
2449 eq->eq[i], denom, total);
2450 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2451 isl_upoly_free(up);
2453 isl_int_clear(denom);
2455 if (!qp->upoly)
2456 goto error;
2458 isl_basic_set_free(eq);
2460 qp = substitute_non_divs(qp);
2461 qp = sort_divs(qp);
2463 return qp;
2464 error:
2465 isl_basic_set_free(eq);
2466 isl_qpolynomial_free(qp);
2467 return NULL;
2470 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2472 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2473 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2475 if (!qp || !eq)
2476 goto error;
2477 if (qp->div->n_row > 0)
2478 eq = isl_basic_set_add(eq, isl_dim_set, qp->div->n_row);
2479 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2480 error:
2481 isl_basic_set_free(eq);
2482 isl_qpolynomial_free(qp);
2483 return NULL;
2486 static __isl_give isl_basic_set *add_div_constraints(
2487 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2489 int i;
2490 unsigned total;
2492 if (!bset || !div)
2493 goto error;
2495 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2496 if (!bset)
2497 goto error;
2498 total = isl_basic_set_total_dim(bset);
2499 for (i = 0; i < div->n_row; ++i)
2500 if (isl_basic_set_add_div_constraints_var(bset,
2501 total - div->n_row + i, div->row[i]) < 0)
2502 goto error;
2504 isl_mat_free(div);
2505 return bset;
2506 error:
2507 isl_mat_free(div);
2508 isl_basic_set_free(bset);
2509 return NULL;
2512 /* Look for equalities among the variables shared by context and qp
2513 * and the integer divisions of qp, if any.
2514 * The equalities are then used to eliminate variables and/or integer
2515 * divisions from qp.
2517 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2518 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2520 isl_basic_set *aff;
2522 if (!qp)
2523 goto error;
2524 if (qp->div->n_row > 0) {
2525 isl_basic_set *bset;
2526 context = isl_set_add_dims(context, isl_dim_set,
2527 qp->div->n_row);
2528 bset = isl_basic_set_universe(isl_set_get_dim(context));
2529 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2530 context = isl_set_intersect(context,
2531 isl_set_from_basic_set(bset));
2534 aff = isl_set_affine_hull(context);
2535 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2536 error:
2537 isl_qpolynomial_free(qp);
2538 isl_set_free(context);
2539 return NULL;
2542 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2543 __isl_take isl_qpolynomial *qp)
2545 isl_set *dom;
2547 if (!qp)
2548 return NULL;
2549 if (isl_qpolynomial_is_zero(qp)) {
2550 isl_dim *dim = isl_qpolynomial_get_dim(qp);
2551 isl_qpolynomial_free(qp);
2552 return isl_pw_qpolynomial_zero(dim);
2555 dom = isl_set_universe(isl_qpolynomial_get_dim(qp));
2556 return isl_pw_qpolynomial_alloc(dom, qp);
2559 #undef PW
2560 #define PW isl_pw_qpolynomial
2561 #undef EL
2562 #define EL isl_qpolynomial
2563 #undef EL_IS_ZERO
2564 #define EL_IS_ZERO is_zero
2565 #undef ZERO
2566 #define ZERO zero
2567 #undef IS_ZERO
2568 #define IS_ZERO is_zero
2569 #undef FIELD
2570 #define FIELD qp
2572 #include <isl_pw_templ.c>
2574 #undef UNION
2575 #define UNION isl_union_pw_qpolynomial
2576 #undef PART
2577 #define PART isl_pw_qpolynomial
2578 #undef PARTS
2579 #define PARTS pw_qpolynomial
2581 #include <isl_union_templ.c>
2583 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2585 if (!pwqp)
2586 return -1;
2588 if (pwqp->n != -1)
2589 return 0;
2591 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2592 return 0;
2594 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2597 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2598 __isl_take isl_pw_qpolynomial *pwqp1,
2599 __isl_take isl_pw_qpolynomial *pwqp2)
2601 int i, j, n;
2602 struct isl_pw_qpolynomial *res;
2604 if (!pwqp1 || !pwqp2)
2605 goto error;
2607 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2608 goto error);
2610 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2611 isl_pw_qpolynomial_free(pwqp2);
2612 return pwqp1;
2615 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2616 isl_pw_qpolynomial_free(pwqp1);
2617 return pwqp2;
2620 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2621 isl_pw_qpolynomial_free(pwqp1);
2622 return pwqp2;
2625 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2626 isl_pw_qpolynomial_free(pwqp2);
2627 return pwqp1;
2630 n = pwqp1->n * pwqp2->n;
2631 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2633 for (i = 0; i < pwqp1->n; ++i) {
2634 for (j = 0; j < pwqp2->n; ++j) {
2635 struct isl_set *common;
2636 struct isl_qpolynomial *prod;
2637 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2638 isl_set_copy(pwqp2->p[j].set));
2639 if (isl_set_plain_is_empty(common)) {
2640 isl_set_free(common);
2641 continue;
2644 prod = isl_qpolynomial_mul(
2645 isl_qpolynomial_copy(pwqp1->p[i].qp),
2646 isl_qpolynomial_copy(pwqp2->p[j].qp));
2648 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2652 isl_pw_qpolynomial_free(pwqp1);
2653 isl_pw_qpolynomial_free(pwqp2);
2655 return res;
2656 error:
2657 isl_pw_qpolynomial_free(pwqp1);
2658 isl_pw_qpolynomial_free(pwqp2);
2659 return NULL;
2662 __isl_give struct isl_upoly *isl_upoly_eval(
2663 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2665 int i;
2666 struct isl_upoly_rec *rec;
2667 struct isl_upoly *res;
2668 struct isl_upoly *base;
2670 if (isl_upoly_is_cst(up)) {
2671 isl_vec_free(vec);
2672 return up;
2675 rec = isl_upoly_as_rec(up);
2676 if (!rec)
2677 goto error;
2679 isl_assert(up->ctx, rec->n >= 1, goto error);
2681 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2683 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2684 isl_vec_copy(vec));
2686 for (i = rec->n - 2; i >= 0; --i) {
2687 res = isl_upoly_mul(res, isl_upoly_copy(base));
2688 res = isl_upoly_sum(res,
2689 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2690 isl_vec_copy(vec)));
2693 isl_upoly_free(base);
2694 isl_upoly_free(up);
2695 isl_vec_free(vec);
2696 return res;
2697 error:
2698 isl_upoly_free(up);
2699 isl_vec_free(vec);
2700 return NULL;
2703 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2704 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2706 isl_vec *ext;
2707 struct isl_upoly *up;
2708 isl_dim *dim;
2710 if (!qp || !pnt)
2711 goto error;
2712 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2714 if (qp->div->n_row == 0)
2715 ext = isl_vec_copy(pnt->vec);
2716 else {
2717 int i;
2718 unsigned dim = isl_dim_total(qp->dim);
2719 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2720 if (!ext)
2721 goto error;
2723 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2724 for (i = 0; i < qp->div->n_row; ++i) {
2725 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2726 1 + dim + i, &ext->el[1+dim+i]);
2727 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2728 qp->div->row[i][0]);
2732 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2733 if (!up)
2734 goto error;
2736 dim = isl_dim_copy(qp->dim);
2737 isl_qpolynomial_free(qp);
2738 isl_point_free(pnt);
2740 return isl_qpolynomial_alloc(dim, 0, up);
2741 error:
2742 isl_qpolynomial_free(qp);
2743 isl_point_free(pnt);
2744 return NULL;
2747 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2748 __isl_keep struct isl_upoly_cst *cst2)
2750 int cmp;
2751 isl_int t;
2752 isl_int_init(t);
2753 isl_int_mul(t, cst1->n, cst2->d);
2754 isl_int_submul(t, cst2->n, cst1->d);
2755 cmp = isl_int_sgn(t);
2756 isl_int_clear(t);
2757 return cmp;
2760 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2761 __isl_keep isl_qpolynomial *qp2)
2763 struct isl_upoly_cst *cst1, *cst2;
2765 if (!qp1 || !qp2)
2766 return -1;
2767 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2768 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2769 if (isl_qpolynomial_is_nan(qp1))
2770 return -1;
2771 if (isl_qpolynomial_is_nan(qp2))
2772 return -1;
2773 cst1 = isl_upoly_as_cst(qp1->upoly);
2774 cst2 = isl_upoly_as_cst(qp2->upoly);
2776 return isl_upoly_cmp(cst1, cst2) <= 0;
2779 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2780 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2782 struct isl_upoly_cst *cst1, *cst2;
2783 int cmp;
2785 if (!qp1 || !qp2)
2786 goto error;
2787 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2788 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2789 cst1 = isl_upoly_as_cst(qp1->upoly);
2790 cst2 = isl_upoly_as_cst(qp2->upoly);
2791 cmp = isl_upoly_cmp(cst1, cst2);
2793 if (cmp <= 0) {
2794 isl_qpolynomial_free(qp2);
2795 } else {
2796 isl_qpolynomial_free(qp1);
2797 qp1 = qp2;
2799 return qp1;
2800 error:
2801 isl_qpolynomial_free(qp1);
2802 isl_qpolynomial_free(qp2);
2803 return NULL;
2806 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2807 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2809 struct isl_upoly_cst *cst1, *cst2;
2810 int cmp;
2812 if (!qp1 || !qp2)
2813 goto error;
2814 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2815 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2816 cst1 = isl_upoly_as_cst(qp1->upoly);
2817 cst2 = isl_upoly_as_cst(qp2->upoly);
2818 cmp = isl_upoly_cmp(cst1, cst2);
2820 if (cmp >= 0) {
2821 isl_qpolynomial_free(qp2);
2822 } else {
2823 isl_qpolynomial_free(qp1);
2824 qp1 = qp2;
2826 return qp1;
2827 error:
2828 isl_qpolynomial_free(qp1);
2829 isl_qpolynomial_free(qp2);
2830 return NULL;
2833 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2834 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2835 unsigned first, unsigned n)
2837 unsigned total;
2838 unsigned g_pos;
2839 int *exp;
2841 if (n == 0 && !isl_dim_is_named_or_nested(qp->dim, type))
2842 return qp;
2844 qp = isl_qpolynomial_cow(qp);
2845 if (!qp)
2846 return NULL;
2848 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2849 goto error);
2851 g_pos = pos(qp->dim, type) + first;
2853 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
2854 if (!qp->div)
2855 goto error;
2857 total = qp->div->n_col - 2;
2858 if (total > g_pos) {
2859 int i;
2860 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2861 if (!exp)
2862 goto error;
2863 for (i = 0; i < total - g_pos; ++i)
2864 exp[i] = i + n;
2865 qp->upoly = expand(qp->upoly, exp, g_pos);
2866 free(exp);
2867 if (!qp->upoly)
2868 goto error;
2871 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2872 if (!qp->dim)
2873 goto error;
2875 return qp;
2876 error:
2877 isl_qpolynomial_free(qp);
2878 return NULL;
2881 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2882 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2884 unsigned pos;
2886 pos = isl_qpolynomial_dim(qp, type);
2888 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2891 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2892 __isl_take isl_pw_qpolynomial *pwqp,
2893 enum isl_dim_type type, unsigned n)
2895 unsigned pos;
2897 pos = isl_pw_qpolynomial_dim(pwqp, type);
2899 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2902 static int *reordering_move(isl_ctx *ctx,
2903 unsigned len, unsigned dst, unsigned src, unsigned n)
2905 int i;
2906 int *reordering;
2908 reordering = isl_alloc_array(ctx, int, len);
2909 if (!reordering)
2910 return NULL;
2912 if (dst <= src) {
2913 for (i = 0; i < dst; ++i)
2914 reordering[i] = i;
2915 for (i = 0; i < n; ++i)
2916 reordering[src + i] = dst + i;
2917 for (i = 0; i < src - dst; ++i)
2918 reordering[dst + i] = dst + n + i;
2919 for (i = 0; i < len - src - n; ++i)
2920 reordering[src + n + i] = src + n + i;
2921 } else {
2922 for (i = 0; i < src; ++i)
2923 reordering[i] = i;
2924 for (i = 0; i < n; ++i)
2925 reordering[src + i] = dst + i;
2926 for (i = 0; i < dst - src; ++i)
2927 reordering[src + n + i] = src + i;
2928 for (i = 0; i < len - dst - n; ++i)
2929 reordering[dst + n + i] = dst + n + i;
2932 return reordering;
2935 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2936 __isl_take isl_qpolynomial *qp,
2937 enum isl_dim_type dst_type, unsigned dst_pos,
2938 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2940 unsigned g_dst_pos;
2941 unsigned g_src_pos;
2942 int *reordering;
2944 qp = isl_qpolynomial_cow(qp);
2945 if (!qp)
2946 return NULL;
2948 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2949 goto error);
2951 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2952 g_src_pos = pos(qp->dim, src_type) + src_pos;
2953 if (dst_type > src_type)
2954 g_dst_pos -= n;
2956 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2957 if (!qp->div)
2958 goto error;
2959 qp = sort_divs(qp);
2960 if (!qp)
2961 goto error;
2963 reordering = reordering_move(qp->dim->ctx,
2964 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2965 if (!reordering)
2966 goto error;
2968 qp->upoly = reorder(qp->upoly, reordering);
2969 free(reordering);
2970 if (!qp->upoly)
2971 goto error;
2973 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2974 if (!qp->dim)
2975 goto error;
2977 return qp;
2978 error:
2979 isl_qpolynomial_free(qp);
2980 return NULL;
2983 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2984 isl_int *f, isl_int denom)
2986 struct isl_upoly *up;
2988 if (!dim)
2989 return NULL;
2991 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2993 return isl_qpolynomial_alloc(dim, 0, up);
2996 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
2998 isl_ctx *ctx;
2999 struct isl_upoly *up;
3000 isl_qpolynomial *qp;
3002 if (!aff)
3003 return NULL;
3005 ctx = isl_aff_get_ctx(aff);
3006 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3007 aff->v->size - 1);
3009 qp = isl_qpolynomial_alloc(isl_aff_get_dim(aff),
3010 aff->ls->div->n_row, up);
3011 if (!qp)
3012 goto error;
3014 isl_mat_free(qp->div);
3015 qp->div = isl_mat_copy(aff->ls->div);
3016 qp->div = isl_mat_cow(qp->div);
3017 if (!qp->div)
3018 goto error;
3020 isl_aff_free(aff);
3021 qp = reduce_divs(qp);
3022 qp = remove_redundant_divs(qp);
3023 return qp;
3024 error:
3025 isl_aff_free(aff);
3026 return NULL;
3029 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3030 __isl_take isl_pw_aff *pwaff)
3032 int i;
3033 isl_pw_qpolynomial *pwqp;
3035 if (!pwaff)
3036 return NULL;
3038 pwqp = isl_pw_qpolynomial_alloc_(isl_pw_aff_get_dim(pwaff), pwaff->n);
3040 for (i = 0; i < pwaff->n; ++i) {
3041 isl_set *dom;
3042 isl_qpolynomial *qp;
3044 dom = isl_set_copy(pwaff->p[i].set);
3045 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3046 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3049 isl_pw_aff_free(pwaff);
3050 return pwqp;
3053 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3054 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3056 isl_aff *aff;
3058 aff = isl_constraint_get_bound(c, type, pos);
3059 isl_constraint_free(c);
3060 return isl_qpolynomial_from_aff(aff);
3063 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3064 * in "qp" by subs[i].
3066 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3067 __isl_take isl_qpolynomial *qp,
3068 enum isl_dim_type type, unsigned first, unsigned n,
3069 __isl_keep isl_qpolynomial **subs)
3071 int i;
3072 struct isl_upoly **ups;
3074 if (n == 0)
3075 return qp;
3077 qp = isl_qpolynomial_cow(qp);
3078 if (!qp)
3079 return NULL;
3080 for (i = 0; i < n; ++i)
3081 if (!subs[i])
3082 goto error;
3084 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
3085 goto error);
3087 for (i = 0; i < n; ++i)
3088 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
3089 goto error);
3091 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3092 for (i = 0; i < n; ++i)
3093 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3095 first += pos(qp->dim, type);
3097 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3098 if (!ups)
3099 goto error;
3100 for (i = 0; i < n; ++i)
3101 ups[i] = subs[i]->upoly;
3103 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3105 free(ups);
3107 if (!qp->upoly)
3108 goto error;
3110 return qp;
3111 error:
3112 isl_qpolynomial_free(qp);
3113 return NULL;
3116 /* Extend "bset" with extra set dimensions for each integer division
3117 * in "qp" and then call "fn" with the extended bset and the polynomial
3118 * that results from replacing each of the integer divisions by the
3119 * corresponding extra set dimension.
3121 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3122 __isl_keep isl_basic_set *bset,
3123 int (*fn)(__isl_take isl_basic_set *bset,
3124 __isl_take isl_qpolynomial *poly, void *user), void *user)
3126 isl_dim *dim;
3127 isl_mat *div;
3128 isl_qpolynomial *poly;
3130 if (!qp || !bset)
3131 goto error;
3132 if (qp->div->n_row == 0)
3133 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3134 user);
3136 div = isl_mat_copy(qp->div);
3137 dim = isl_dim_copy(qp->dim);
3138 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3139 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3140 bset = isl_basic_set_copy(bset);
3141 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3142 bset = add_div_constraints(bset, div);
3144 return fn(bset, poly, user);
3145 error:
3146 return -1;
3149 /* Return total degree in variables first (inclusive) up to last (exclusive).
3151 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3153 int deg = -1;
3154 int i;
3155 struct isl_upoly_rec *rec;
3157 if (!up)
3158 return -2;
3159 if (isl_upoly_is_zero(up))
3160 return -1;
3161 if (isl_upoly_is_cst(up) || up->var < first)
3162 return 0;
3164 rec = isl_upoly_as_rec(up);
3165 if (!rec)
3166 return -2;
3168 for (i = 0; i < rec->n; ++i) {
3169 int d;
3171 if (isl_upoly_is_zero(rec->p[i]))
3172 continue;
3173 d = isl_upoly_degree(rec->p[i], first, last);
3174 if (up->var < last)
3175 d += i;
3176 if (d > deg)
3177 deg = d;
3180 return deg;
3183 /* Return total degree in set variables.
3185 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3187 unsigned ovar;
3188 unsigned nvar;
3190 if (!poly)
3191 return -2;
3193 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3194 nvar = isl_dim_size(poly->dim, isl_dim_set);
3195 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3198 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3199 unsigned pos, int deg)
3201 int i;
3202 struct isl_upoly_rec *rec;
3204 if (!up)
3205 return NULL;
3207 if (isl_upoly_is_cst(up) || up->var < pos) {
3208 if (deg == 0)
3209 return isl_upoly_copy(up);
3210 else
3211 return isl_upoly_zero(up->ctx);
3214 rec = isl_upoly_as_rec(up);
3215 if (!rec)
3216 return NULL;
3218 if (up->var == pos) {
3219 if (deg < rec->n)
3220 return isl_upoly_copy(rec->p[deg]);
3221 else
3222 return isl_upoly_zero(up->ctx);
3225 up = isl_upoly_copy(up);
3226 up = isl_upoly_cow(up);
3227 rec = isl_upoly_as_rec(up);
3228 if (!rec)
3229 goto error;
3231 for (i = 0; i < rec->n; ++i) {
3232 struct isl_upoly *t;
3233 t = isl_upoly_coeff(rec->p[i], pos, deg);
3234 if (!t)
3235 goto error;
3236 isl_upoly_free(rec->p[i]);
3237 rec->p[i] = t;
3240 return up;
3241 error:
3242 isl_upoly_free(up);
3243 return NULL;
3246 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3248 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3249 __isl_keep isl_qpolynomial *qp,
3250 enum isl_dim_type type, unsigned t_pos, int deg)
3252 unsigned g_pos;
3253 struct isl_upoly *up;
3254 isl_qpolynomial *c;
3256 if (!qp)
3257 return NULL;
3259 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3260 return NULL);
3262 g_pos = pos(qp->dim, type) + t_pos;
3263 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3265 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3266 if (!c)
3267 return NULL;
3268 isl_mat_free(c->div);
3269 c->div = isl_mat_copy(qp->div);
3270 if (!c->div)
3271 goto error;
3272 return c;
3273 error:
3274 isl_qpolynomial_free(c);
3275 return NULL;
3278 /* Homogenize the polynomial in the variables first (inclusive) up to
3279 * last (exclusive) by inserting powers of variable first.
3280 * Variable first is assumed not to appear in the input.
3282 __isl_give struct isl_upoly *isl_upoly_homogenize(
3283 __isl_take struct isl_upoly *up, int deg, int target,
3284 int first, int last)
3286 int i;
3287 struct isl_upoly_rec *rec;
3289 if (!up)
3290 return NULL;
3291 if (isl_upoly_is_zero(up))
3292 return up;
3293 if (deg == target)
3294 return up;
3295 if (isl_upoly_is_cst(up) || up->var < first) {
3296 struct isl_upoly *hom;
3298 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3299 if (!hom)
3300 goto error;
3301 rec = isl_upoly_as_rec(hom);
3302 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3304 return hom;
3307 up = isl_upoly_cow(up);
3308 rec = isl_upoly_as_rec(up);
3309 if (!rec)
3310 goto error;
3312 for (i = 0; i < rec->n; ++i) {
3313 if (isl_upoly_is_zero(rec->p[i]))
3314 continue;
3315 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3316 up->var < last ? deg + i : i, target,
3317 first, last);
3318 if (!rec->p[i])
3319 goto error;
3322 return up;
3323 error:
3324 isl_upoly_free(up);
3325 return NULL;
3328 /* Homogenize the polynomial in the set variables by introducing
3329 * powers of an extra set variable at position 0.
3331 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3332 __isl_take isl_qpolynomial *poly)
3334 unsigned ovar;
3335 unsigned nvar;
3336 int deg = isl_qpolynomial_degree(poly);
3338 if (deg < -1)
3339 goto error;
3341 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3342 poly = isl_qpolynomial_cow(poly);
3343 if (!poly)
3344 goto error;
3346 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3347 nvar = isl_dim_size(poly->dim, isl_dim_set);
3348 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3349 ovar, ovar + nvar);
3350 if (!poly->upoly)
3351 goto error;
3353 return poly;
3354 error:
3355 isl_qpolynomial_free(poly);
3356 return NULL;
3359 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3360 __isl_take isl_mat *div)
3362 isl_term *term;
3363 int n;
3365 if (!dim || !div)
3366 goto error;
3368 n = isl_dim_total(dim) + div->n_row;
3370 term = isl_calloc(dim->ctx, struct isl_term,
3371 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3372 if (!term)
3373 goto error;
3375 term->ref = 1;
3376 term->dim = dim;
3377 term->div = div;
3378 isl_int_init(term->n);
3379 isl_int_init(term->d);
3381 return term;
3382 error:
3383 isl_dim_free(dim);
3384 isl_mat_free(div);
3385 return NULL;
3388 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3390 if (!term)
3391 return NULL;
3393 term->ref++;
3394 return term;
3397 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3399 int i;
3400 isl_term *dup;
3401 unsigned total;
3403 if (term)
3404 return NULL;
3406 total = isl_dim_total(term->dim) + term->div->n_row;
3408 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3409 if (!dup)
3410 return NULL;
3412 isl_int_set(dup->n, term->n);
3413 isl_int_set(dup->d, term->d);
3415 for (i = 0; i < total; ++i)
3416 dup->pow[i] = term->pow[i];
3418 return dup;
3421 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3423 if (!term)
3424 return NULL;
3426 if (term->ref == 1)
3427 return term;
3428 term->ref--;
3429 return isl_term_dup(term);
3432 void isl_term_free(__isl_take isl_term *term)
3434 if (!term)
3435 return;
3437 if (--term->ref > 0)
3438 return;
3440 isl_dim_free(term->dim);
3441 isl_mat_free(term->div);
3442 isl_int_clear(term->n);
3443 isl_int_clear(term->d);
3444 free(term);
3447 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3449 if (!term)
3450 return 0;
3452 switch (type) {
3453 case isl_dim_param:
3454 case isl_dim_in:
3455 case isl_dim_out: return isl_dim_size(term->dim, type);
3456 case isl_dim_div: return term->div->n_row;
3457 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3458 default: return 0;
3462 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3464 return term ? term->dim->ctx : NULL;
3467 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3469 if (!term)
3470 return;
3471 isl_int_set(*n, term->n);
3474 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3476 if (!term)
3477 return;
3478 isl_int_set(*d, term->d);
3481 int isl_term_get_exp(__isl_keep isl_term *term,
3482 enum isl_dim_type type, unsigned pos)
3484 if (!term)
3485 return -1;
3487 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3489 if (type >= isl_dim_set)
3490 pos += isl_dim_size(term->dim, isl_dim_param);
3491 if (type >= isl_dim_div)
3492 pos += isl_dim_size(term->dim, isl_dim_set);
3494 return term->pow[pos];
3497 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3499 isl_basic_map *bmap;
3500 unsigned total;
3501 int k;
3503 if (!term)
3504 return NULL;
3506 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3507 return NULL);
3509 total = term->div->n_col - term->div->n_row - 2;
3510 /* No nested divs for now */
3511 isl_assert(term->dim->ctx,
3512 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3513 term->div->n_row) == -1,
3514 return NULL);
3516 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3517 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3518 goto error;
3520 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3522 return isl_basic_map_div(bmap, k);
3523 error:
3524 isl_basic_map_free(bmap);
3525 return NULL;
3528 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3529 int (*fn)(__isl_take isl_term *term, void *user),
3530 __isl_take isl_term *term, void *user)
3532 int i;
3533 struct isl_upoly_rec *rec;
3535 if (!up || !term)
3536 goto error;
3538 if (isl_upoly_is_zero(up))
3539 return term;
3541 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3542 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3543 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3545 if (isl_upoly_is_cst(up)) {
3546 struct isl_upoly_cst *cst;
3547 cst = isl_upoly_as_cst(up);
3548 if (!cst)
3549 goto error;
3550 term = isl_term_cow(term);
3551 if (!term)
3552 goto error;
3553 isl_int_set(term->n, cst->n);
3554 isl_int_set(term->d, cst->d);
3555 if (fn(isl_term_copy(term), user) < 0)
3556 goto error;
3557 return term;
3560 rec = isl_upoly_as_rec(up);
3561 if (!rec)
3562 goto error;
3564 for (i = 0; i < rec->n; ++i) {
3565 term = isl_term_cow(term);
3566 if (!term)
3567 goto error;
3568 term->pow[up->var] = i;
3569 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3570 if (!term)
3571 goto error;
3573 term->pow[up->var] = 0;
3575 return term;
3576 error:
3577 isl_term_free(term);
3578 return NULL;
3581 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3582 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3584 isl_term *term;
3586 if (!qp)
3587 return -1;
3589 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3590 if (!term)
3591 return -1;
3593 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3595 isl_term_free(term);
3597 return term ? 0 : -1;
3600 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3602 struct isl_upoly *up;
3603 isl_qpolynomial *qp;
3604 int i, n;
3606 if (!term)
3607 return NULL;
3609 n = isl_dim_total(term->dim) + term->div->n_row;
3611 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3612 for (i = 0; i < n; ++i) {
3613 if (!term->pow[i])
3614 continue;
3615 up = isl_upoly_mul(up,
3616 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3619 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3620 if (!qp)
3621 goto error;
3622 isl_mat_free(qp->div);
3623 qp->div = isl_mat_copy(term->div);
3624 if (!qp->div)
3625 goto error;
3627 isl_term_free(term);
3628 return qp;
3629 error:
3630 isl_qpolynomial_free(qp);
3631 isl_term_free(term);
3632 return NULL;
3635 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3636 __isl_take isl_dim *dim)
3638 int i;
3639 int extra;
3640 unsigned total;
3642 if (!qp || !dim)
3643 goto error;
3645 if (isl_dim_equal(qp->dim, dim)) {
3646 isl_dim_free(dim);
3647 return qp;
3650 qp = isl_qpolynomial_cow(qp);
3651 if (!qp)
3652 goto error;
3654 extra = isl_dim_size(dim, isl_dim_set) -
3655 isl_dim_size(qp->dim, isl_dim_set);
3656 total = isl_dim_total(qp->dim);
3657 if (qp->div->n_row) {
3658 int *exp;
3660 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3661 if (!exp)
3662 goto error;
3663 for (i = 0; i < qp->div->n_row; ++i)
3664 exp[i] = extra + i;
3665 qp->upoly = expand(qp->upoly, exp, total);
3666 free(exp);
3667 if (!qp->upoly)
3668 goto error;
3670 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3671 if (!qp->div)
3672 goto error;
3673 for (i = 0; i < qp->div->n_row; ++i)
3674 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3676 isl_dim_free(qp->dim);
3677 qp->dim = dim;
3679 return qp;
3680 error:
3681 isl_dim_free(dim);
3682 isl_qpolynomial_free(qp);
3683 return NULL;
3686 /* For each parameter or variable that does not appear in qp,
3687 * first eliminate the variable from all constraints and then set it to zero.
3689 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3690 __isl_keep isl_qpolynomial *qp)
3692 int *active = NULL;
3693 int i;
3694 int d;
3695 unsigned nparam;
3696 unsigned nvar;
3698 if (!set || !qp)
3699 goto error;
3701 d = isl_dim_total(set->dim);
3702 active = isl_calloc_array(set->ctx, int, d);
3703 if (set_active(qp, active) < 0)
3704 goto error;
3706 for (i = 0; i < d; ++i)
3707 if (!active[i])
3708 break;
3710 if (i == d) {
3711 free(active);
3712 return set;
3715 nparam = isl_dim_size(set->dim, isl_dim_param);
3716 nvar = isl_dim_size(set->dim, isl_dim_set);
3717 for (i = 0; i < nparam; ++i) {
3718 if (active[i])
3719 continue;
3720 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3721 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3723 for (i = 0; i < nvar; ++i) {
3724 if (active[nparam + i])
3725 continue;
3726 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3727 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3730 free(active);
3732 return set;
3733 error:
3734 free(active);
3735 isl_set_free(set);
3736 return NULL;
3739 struct isl_opt_data {
3740 isl_qpolynomial *qp;
3741 int first;
3742 isl_qpolynomial *opt;
3743 int max;
3746 static int opt_fn(__isl_take isl_point *pnt, void *user)
3748 struct isl_opt_data *data = (struct isl_opt_data *)user;
3749 isl_qpolynomial *val;
3751 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3752 if (data->first) {
3753 data->first = 0;
3754 data->opt = val;
3755 } else if (data->max) {
3756 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3757 } else {
3758 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3761 return 0;
3764 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3765 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3767 struct isl_opt_data data = { NULL, 1, NULL, max };
3769 if (!set || !qp)
3770 goto error;
3772 if (isl_upoly_is_cst(qp->upoly)) {
3773 isl_set_free(set);
3774 return qp;
3777 set = fix_inactive(set, qp);
3779 data.qp = qp;
3780 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3781 goto error;
3783 if (data.first)
3784 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3786 isl_set_free(set);
3787 isl_qpolynomial_free(qp);
3788 return data.opt;
3789 error:
3790 isl_set_free(set);
3791 isl_qpolynomial_free(qp);
3792 isl_qpolynomial_free(data.opt);
3793 return NULL;
3796 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3797 __isl_take isl_morph *morph)
3799 int i;
3800 int n_sub;
3801 isl_ctx *ctx;
3802 struct isl_upoly **subs;
3803 isl_mat *mat;
3805 qp = isl_qpolynomial_cow(qp);
3806 if (!qp || !morph)
3807 goto error;
3809 ctx = qp->dim->ctx;
3810 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3812 n_sub = morph->inv->n_row - 1;
3813 if (morph->inv->n_row != morph->inv->n_col)
3814 n_sub += qp->div->n_row;
3815 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3816 if (!subs)
3817 goto error;
3819 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3820 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3821 morph->inv->row[0][0], morph->inv->n_col);
3822 if (morph->inv->n_row != morph->inv->n_col)
3823 for (i = 0; i < qp->div->n_row; ++i)
3824 subs[morph->inv->n_row - 1 + i] =
3825 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3827 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3829 for (i = 0; i < n_sub; ++i)
3830 isl_upoly_free(subs[i]);
3831 free(subs);
3833 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3834 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3835 qp->div = isl_mat_product(qp->div, mat);
3836 isl_dim_free(qp->dim);
3837 qp->dim = isl_dim_copy(morph->ran->dim);
3839 if (!qp->upoly || !qp->div || !qp->dim)
3840 goto error;
3842 isl_morph_free(morph);
3844 return qp;
3845 error:
3846 isl_qpolynomial_free(qp);
3847 isl_morph_free(morph);
3848 return NULL;
3851 static int neg_entry(void **entry, void *user)
3853 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3855 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3857 return *pwqp ? 0 : -1;
3860 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3861 __isl_take isl_union_pw_qpolynomial *upwqp)
3863 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3864 if (!upwqp)
3865 return NULL;
3867 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3868 &neg_entry, NULL) < 0)
3869 goto error;
3871 return upwqp;
3872 error:
3873 isl_union_pw_qpolynomial_free(upwqp);
3874 return NULL;
3877 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3878 __isl_take isl_union_pw_qpolynomial *upwqp1,
3879 __isl_take isl_union_pw_qpolynomial *upwqp2)
3881 return isl_union_pw_qpolynomial_add(upwqp1,
3882 isl_union_pw_qpolynomial_neg(upwqp2));
3885 static int mul_entry(void **entry, void *user)
3887 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3888 uint32_t hash;
3889 struct isl_hash_table_entry *entry2;
3890 isl_pw_qpolynomial *pwpq = *entry;
3891 int empty;
3893 hash = isl_dim_get_hash(pwpq->dim);
3894 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3895 hash, &has_dim, pwpq->dim, 0);
3896 if (!entry2)
3897 return 0;
3899 pwpq = isl_pw_qpolynomial_copy(pwpq);
3900 pwpq = isl_pw_qpolynomial_mul(pwpq,
3901 isl_pw_qpolynomial_copy(entry2->data));
3903 empty = isl_pw_qpolynomial_is_zero(pwpq);
3904 if (empty < 0) {
3905 isl_pw_qpolynomial_free(pwpq);
3906 return -1;
3908 if (empty) {
3909 isl_pw_qpolynomial_free(pwpq);
3910 return 0;
3913 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3915 return 0;
3918 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3919 __isl_take isl_union_pw_qpolynomial *upwqp1,
3920 __isl_take isl_union_pw_qpolynomial *upwqp2)
3922 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3925 /* Reorder the columns of the given div definitions according to the
3926 * given reordering.
3928 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3929 __isl_take isl_reordering *r)
3931 int i, j;
3932 isl_mat *mat;
3933 int extra;
3935 if (!div || !r)
3936 goto error;
3938 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3939 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3940 if (!mat)
3941 goto error;
3943 for (i = 0; i < div->n_row; ++i) {
3944 isl_seq_cpy(mat->row[i], div->row[i], 2);
3945 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3946 for (j = 0; j < r->len; ++j)
3947 isl_int_set(mat->row[i][2 + r->pos[j]],
3948 div->row[i][2 + j]);
3951 isl_reordering_free(r);
3952 isl_mat_free(div);
3953 return mat;
3954 error:
3955 isl_reordering_free(r);
3956 isl_mat_free(div);
3957 return NULL;
3960 /* Reorder the dimension of "qp" according to the given reordering.
3962 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3963 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3965 qp = isl_qpolynomial_cow(qp);
3966 if (!qp)
3967 goto error;
3969 r = isl_reordering_extend(r, qp->div->n_row);
3970 if (!r)
3971 goto error;
3973 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3974 if (!qp->div)
3975 goto error;
3977 qp->upoly = reorder(qp->upoly, r->pos);
3978 if (!qp->upoly)
3979 goto error;
3981 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3983 isl_reordering_free(r);
3984 return qp;
3985 error:
3986 isl_qpolynomial_free(qp);
3987 isl_reordering_free(r);
3988 return NULL;
3991 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
3992 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *model)
3994 if (!qp || !model)
3995 goto error;
3997 if (!isl_dim_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
3998 isl_reordering *exp;
4000 model = isl_dim_drop(model, isl_dim_in,
4001 0, isl_dim_size(model, isl_dim_in));
4002 model = isl_dim_drop(model, isl_dim_out,
4003 0, isl_dim_size(model, isl_dim_out));
4004 exp = isl_parameter_alignment_reordering(qp->dim, model);
4005 exp = isl_reordering_extend_dim(exp,
4006 isl_qpolynomial_get_dim(qp));
4007 qp = isl_qpolynomial_realign(qp, exp);
4010 isl_dim_free(model);
4011 return qp;
4012 error:
4013 isl_dim_free(model);
4014 isl_qpolynomial_free(qp);
4015 return NULL;
4018 struct isl_split_periods_data {
4019 int max_periods;
4020 isl_pw_qpolynomial *res;
4023 /* Create a slice where the integer division "div" has the fixed value "v".
4024 * In particular, if "div" refers to floor(f/m), then create a slice
4026 * m v <= f <= m v + (m - 1)
4028 * or
4030 * f - m v >= 0
4031 * -f + m v + (m - 1) >= 0
4033 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
4034 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4036 int total;
4037 isl_basic_set *bset = NULL;
4038 int k;
4040 if (!dim || !qp)
4041 goto error;
4043 total = isl_dim_total(dim);
4044 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
4046 k = isl_basic_set_alloc_inequality(bset);
4047 if (k < 0)
4048 goto error;
4049 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4050 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4052 k = isl_basic_set_alloc_inequality(bset);
4053 if (k < 0)
4054 goto error;
4055 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4056 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4057 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4058 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4060 isl_dim_free(dim);
4061 return isl_set_from_basic_set(bset);
4062 error:
4063 isl_basic_set_free(bset);
4064 isl_dim_free(dim);
4065 return NULL;
4068 static int split_periods(__isl_take isl_set *set,
4069 __isl_take isl_qpolynomial *qp, void *user);
4071 /* Create a slice of the domain "set" such that integer division "div"
4072 * has the fixed value "v" and add the results to data->res,
4073 * replacing the integer division by "v" in "qp".
4075 static int set_div(__isl_take isl_set *set,
4076 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4077 struct isl_split_periods_data *data)
4079 int i;
4080 int total;
4081 isl_set *slice;
4082 struct isl_upoly *cst;
4084 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
4085 set = isl_set_intersect(set, slice);
4087 if (!qp)
4088 goto error;
4090 total = isl_dim_total(qp->dim);
4092 for (i = div + 1; i < qp->div->n_row; ++i) {
4093 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4094 continue;
4095 isl_int_addmul(qp->div->row[i][1],
4096 qp->div->row[i][2 + total + div], v);
4097 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4100 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4101 qp = substitute_div(qp, div, cst);
4103 return split_periods(set, qp, data);
4104 error:
4105 isl_set_free(set);
4106 isl_qpolynomial_free(qp);
4107 return -1;
4110 /* Split the domain "set" such that integer division "div"
4111 * has a fixed value (ranging from "min" to "max") on each slice
4112 * and add the results to data->res.
4114 static int split_div(__isl_take isl_set *set,
4115 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4116 struct isl_split_periods_data *data)
4118 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4119 isl_set *set_i = isl_set_copy(set);
4120 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4122 if (set_div(set_i, qp_i, div, min, data) < 0)
4123 goto error;
4125 isl_set_free(set);
4126 isl_qpolynomial_free(qp);
4127 return 0;
4128 error:
4129 isl_set_free(set);
4130 isl_qpolynomial_free(qp);
4131 return -1;
4134 /* If "qp" refers to any integer division
4135 * that can only attain "max_periods" distinct values on "set"
4136 * then split the domain along those distinct values.
4137 * Add the results (or the original if no splitting occurs)
4138 * to data->res.
4140 static int split_periods(__isl_take isl_set *set,
4141 __isl_take isl_qpolynomial *qp, void *user)
4143 int i;
4144 isl_pw_qpolynomial *pwqp;
4145 struct isl_split_periods_data *data;
4146 isl_int min, max;
4147 int total;
4148 int r = 0;
4150 data = (struct isl_split_periods_data *)user;
4152 if (!set || !qp)
4153 goto error;
4155 if (qp->div->n_row == 0) {
4156 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4157 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4158 return 0;
4161 isl_int_init(min);
4162 isl_int_init(max);
4163 total = isl_dim_total(qp->dim);
4164 for (i = 0; i < qp->div->n_row; ++i) {
4165 enum isl_lp_result lp_res;
4167 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4168 qp->div->n_row) != -1)
4169 continue;
4171 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4172 set->ctx->one, &min, NULL, NULL);
4173 if (lp_res == isl_lp_error)
4174 goto error2;
4175 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4176 continue;
4177 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4179 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4180 set->ctx->one, &max, NULL, NULL);
4181 if (lp_res == isl_lp_error)
4182 goto error2;
4183 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4184 continue;
4185 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4187 isl_int_sub(max, max, min);
4188 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4189 isl_int_add(max, max, min);
4190 break;
4194 if (i < qp->div->n_row) {
4195 r = split_div(set, qp, i, min, max, data);
4196 } else {
4197 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4198 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4201 isl_int_clear(max);
4202 isl_int_clear(min);
4204 return r;
4205 error2:
4206 isl_int_clear(max);
4207 isl_int_clear(min);
4208 error:
4209 isl_set_free(set);
4210 isl_qpolynomial_free(qp);
4211 return -1;
4214 /* If any quasi-polynomial in pwqp refers to any integer division
4215 * that can only attain "max_periods" distinct values on its domain
4216 * then split the domain along those distinct values.
4218 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4219 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4221 struct isl_split_periods_data data;
4223 data.max_periods = max_periods;
4224 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4226 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4227 goto error;
4229 isl_pw_qpolynomial_free(pwqp);
4231 return data.res;
4232 error:
4233 isl_pw_qpolynomial_free(data.res);
4234 isl_pw_qpolynomial_free(pwqp);
4235 return NULL;
4238 /* Construct a piecewise quasipolynomial that is constant on the given
4239 * domain. In particular, it is
4240 * 0 if cst == 0
4241 * 1 if cst == 1
4242 * infinity if cst == -1
4244 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4245 __isl_take isl_basic_set *bset, int cst)
4247 isl_dim *dim;
4248 isl_qpolynomial *qp;
4250 if (!bset)
4251 return NULL;
4253 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4254 dim = isl_basic_set_get_dim(bset);
4255 if (cst < 0)
4256 qp = isl_qpolynomial_infty(dim);
4257 else if (cst == 0)
4258 qp = isl_qpolynomial_zero(dim);
4259 else
4260 qp = isl_qpolynomial_one(dim);
4261 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4264 /* Factor bset, call fn on each of the factors and return the product.
4266 * If no factors can be found, simply call fn on the input.
4267 * Otherwise, construct the factors based on the factorizer,
4268 * call fn on each factor and compute the product.
4270 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4271 __isl_take isl_basic_set *bset,
4272 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4274 int i, n;
4275 isl_dim *dim;
4276 isl_set *set;
4277 isl_factorizer *f;
4278 isl_qpolynomial *qp;
4279 isl_pw_qpolynomial *pwqp;
4280 unsigned nparam;
4281 unsigned nvar;
4283 f = isl_basic_set_factorizer(bset);
4284 if (!f)
4285 goto error;
4286 if (f->n_group == 0) {
4287 isl_factorizer_free(f);
4288 return fn(bset);
4291 nparam = isl_basic_set_dim(bset, isl_dim_param);
4292 nvar = isl_basic_set_dim(bset, isl_dim_set);
4294 dim = isl_basic_set_get_dim(bset);
4295 dim = isl_dim_domain(dim);
4296 set = isl_set_universe(isl_dim_copy(dim));
4297 qp = isl_qpolynomial_one(dim);
4298 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4300 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4302 for (i = 0, n = 0; i < f->n_group; ++i) {
4303 isl_basic_set *bset_i;
4304 isl_pw_qpolynomial *pwqp_i;
4306 bset_i = isl_basic_set_copy(bset);
4307 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4308 nparam + n + f->len[i], nvar - n - f->len[i]);
4309 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4310 nparam, n);
4311 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4312 n + f->len[i], nvar - n - f->len[i]);
4313 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4315 pwqp_i = fn(bset_i);
4316 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4318 n += f->len[i];
4321 isl_basic_set_free(bset);
4322 isl_factorizer_free(f);
4324 return pwqp;
4325 error:
4326 isl_basic_set_free(bset);
4327 return NULL;
4330 /* Factor bset, call fn on each of the factors and return the product.
4331 * The function is assumed to evaluate to zero on empty domains,
4332 * to one on zero-dimensional domains and to infinity on unbounded domains
4333 * and will not be called explicitly on zero-dimensional or unbounded domains.
4335 * We first check for some special cases and remove all equalities.
4336 * Then we hand over control to compressed_multiplicative_call.
4338 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4339 __isl_take isl_basic_set *bset,
4340 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4342 int bounded;
4343 isl_morph *morph;
4344 isl_pw_qpolynomial *pwqp;
4345 unsigned orig_nvar, final_nvar;
4347 if (!bset)
4348 return NULL;
4350 if (isl_basic_set_plain_is_empty(bset))
4351 return constant_on_domain(bset, 0);
4353 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4355 if (orig_nvar == 0)
4356 return constant_on_domain(bset, 1);
4358 bounded = isl_basic_set_is_bounded(bset);
4359 if (bounded < 0)
4360 goto error;
4361 if (!bounded)
4362 return constant_on_domain(bset, -1);
4364 if (bset->n_eq == 0)
4365 return compressed_multiplicative_call(bset, fn);
4367 morph = isl_basic_set_full_compression(bset);
4368 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4370 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4372 pwqp = compressed_multiplicative_call(bset, fn);
4374 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4375 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4376 morph = isl_morph_inverse(morph);
4378 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4380 return pwqp;
4381 error:
4382 isl_basic_set_free(bset);
4383 return NULL;
4386 /* Drop all floors in "qp", turning each integer division [a/m] into
4387 * a rational division a/m. If "down" is set, then the integer division
4388 * is replaces by (a-(m-1))/m instead.
4390 static __isl_give isl_qpolynomial *qp_drop_floors(
4391 __isl_take isl_qpolynomial *qp, int down)
4393 int i;
4394 struct isl_upoly *s;
4396 if (!qp)
4397 return NULL;
4398 if (qp->div->n_row == 0)
4399 return qp;
4401 qp = isl_qpolynomial_cow(qp);
4402 if (!qp)
4403 return NULL;
4405 for (i = qp->div->n_row - 1; i >= 0; --i) {
4406 if (down) {
4407 isl_int_sub(qp->div->row[i][1],
4408 qp->div->row[i][1], qp->div->row[i][0]);
4409 isl_int_add_ui(qp->div->row[i][1],
4410 qp->div->row[i][1], 1);
4412 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4413 qp->div->row[i][0], qp->div->n_col - 1);
4414 qp = substitute_div(qp, i, s);
4415 if (!qp)
4416 return NULL;
4419 return qp;
4422 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4423 * a rational division a/m.
4425 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4426 __isl_take isl_pw_qpolynomial *pwqp)
4428 int i;
4430 if (!pwqp)
4431 return NULL;
4433 if (isl_pw_qpolynomial_is_zero(pwqp))
4434 return pwqp;
4436 pwqp = isl_pw_qpolynomial_cow(pwqp);
4437 if (!pwqp)
4438 return NULL;
4440 for (i = 0; i < pwqp->n; ++i) {
4441 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4442 if (!pwqp->p[i].qp)
4443 goto error;
4446 return pwqp;
4447 error:
4448 isl_pw_qpolynomial_free(pwqp);
4449 return NULL;
4452 /* Adjust all the integer divisions in "qp" such that they are at least
4453 * one over the given orthant (identified by "signs"). This ensures
4454 * that they will still be non-negative even after subtracting (m-1)/m.
4456 * In particular, f is replaced by f' + v, changing f = [a/m]
4457 * to f' = [(a - m v)/m].
4458 * If the constant term k in a is smaller than m,
4459 * the constant term of v is set to floor(k/m) - 1.
4460 * For any other term, if the coefficient c and the variable x have
4461 * the same sign, then no changes are needed.
4462 * Otherwise, if the variable is positive (and c is negative),
4463 * then the coefficient of x in v is set to floor(c/m).
4464 * If the variable is negative (and c is positive),
4465 * then the coefficient of x in v is set to ceil(c/m).
4467 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4468 int *signs)
4470 int i, j;
4471 int total;
4472 isl_vec *v = NULL;
4473 struct isl_upoly *s;
4475 qp = isl_qpolynomial_cow(qp);
4476 if (!qp)
4477 return NULL;
4478 qp->div = isl_mat_cow(qp->div);
4479 if (!qp->div)
4480 goto error;
4482 total = isl_dim_total(qp->dim);
4483 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4485 for (i = 0; i < qp->div->n_row; ++i) {
4486 isl_int *row = qp->div->row[i];
4487 v = isl_vec_clr(v);
4488 if (!v)
4489 goto error;
4490 if (isl_int_lt(row[1], row[0])) {
4491 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4492 isl_int_sub_ui(v->el[0], v->el[0], 1);
4493 isl_int_submul(row[1], row[0], v->el[0]);
4495 for (j = 0; j < total; ++j) {
4496 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4497 continue;
4498 if (signs[j] < 0)
4499 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4500 else
4501 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4502 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4504 for (j = 0; j < i; ++j) {
4505 if (isl_int_sgn(row[2 + total + j]) >= 0)
4506 continue;
4507 isl_int_fdiv_q(v->el[1 + total + j],
4508 row[2 + total + j], row[0]);
4509 isl_int_submul(row[2 + total + j],
4510 row[0], v->el[1 + total + j]);
4512 for (j = i + 1; j < qp->div->n_row; ++j) {
4513 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4514 continue;
4515 isl_seq_combine(qp->div->row[j] + 1,
4516 qp->div->ctx->one, qp->div->row[j] + 1,
4517 qp->div->row[j][2 + total + i], v->el, v->size);
4519 isl_int_set_si(v->el[1 + total + i], 1);
4520 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4521 qp->div->ctx->one, v->size);
4522 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4523 isl_upoly_free(s);
4524 if (!qp->upoly)
4525 goto error;
4528 isl_vec_free(v);
4529 return qp;
4530 error:
4531 isl_vec_free(v);
4532 isl_qpolynomial_free(qp);
4533 return NULL;
4536 struct isl_to_poly_data {
4537 int sign;
4538 isl_pw_qpolynomial *res;
4539 isl_qpolynomial *qp;
4542 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4543 * We first make all integer divisions positive and then split the
4544 * quasipolynomials into terms with sign data->sign (the direction
4545 * of the requested approximation) and terms with the opposite sign.
4546 * In the first set of terms, each integer division [a/m] is
4547 * overapproximated by a/m, while in the second it is underapproximated
4548 * by (a-(m-1))/m.
4550 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4551 void *user)
4553 struct isl_to_poly_data *data = user;
4554 isl_pw_qpolynomial *t;
4555 isl_qpolynomial *qp, *up, *down;
4557 qp = isl_qpolynomial_copy(data->qp);
4558 qp = make_divs_pos(qp, signs);
4560 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4561 up = qp_drop_floors(up, 0);
4562 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4563 down = qp_drop_floors(down, 1);
4565 isl_qpolynomial_free(qp);
4566 qp = isl_qpolynomial_add(up, down);
4568 t = isl_pw_qpolynomial_alloc(orthant, qp);
4569 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4571 return 0;
4574 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4575 * the polynomial will be an overapproximation. If "sign" is negative,
4576 * it will be an underapproximation. If "sign" is zero, the approximation
4577 * will lie somewhere in between.
4579 * In particular, is sign == 0, we simply drop the floors, turning
4580 * the integer divisions into rational divisions.
4581 * Otherwise, we split the domains into orthants, make all integer divisions
4582 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4583 * depending on the requested sign and the sign of the term in which
4584 * the integer division appears.
4586 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4587 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4589 int i;
4590 struct isl_to_poly_data data;
4592 if (sign == 0)
4593 return pwqp_drop_floors(pwqp);
4595 if (!pwqp)
4596 return NULL;
4598 data.sign = sign;
4599 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4601 for (i = 0; i < pwqp->n; ++i) {
4602 if (pwqp->p[i].qp->div->n_row == 0) {
4603 isl_pw_qpolynomial *t;
4604 t = isl_pw_qpolynomial_alloc(
4605 isl_set_copy(pwqp->p[i].set),
4606 isl_qpolynomial_copy(pwqp->p[i].qp));
4607 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4608 continue;
4610 data.qp = pwqp->p[i].qp;
4611 if (isl_set_foreach_orthant(pwqp->p[i].set,
4612 &to_polynomial_on_orthant, &data) < 0)
4613 goto error;
4616 isl_pw_qpolynomial_free(pwqp);
4618 return data.res;
4619 error:
4620 isl_pw_qpolynomial_free(pwqp);
4621 isl_pw_qpolynomial_free(data.res);
4622 return NULL;
4625 static int poly_entry(void **entry, void *user)
4627 int *sign = user;
4628 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4630 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4632 return *pwqp ? 0 : -1;
4635 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4636 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4638 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4639 if (!upwqp)
4640 return NULL;
4642 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4643 &poly_entry, &sign) < 0)
4644 goto error;
4646 return upwqp;
4647 error:
4648 isl_union_pw_qpolynomial_free(upwqp);
4649 return NULL;
4652 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4653 __isl_take isl_qpolynomial *qp)
4655 int i, k;
4656 isl_dim *dim;
4657 isl_vec *aff = NULL;
4658 isl_basic_map *bmap = NULL;
4659 unsigned pos;
4660 unsigned n_div;
4662 if (!qp)
4663 return NULL;
4664 if (!isl_upoly_is_affine(qp->upoly))
4665 isl_die(qp->dim->ctx, isl_error_invalid,
4666 "input quasi-polynomial not affine", goto error);
4667 aff = isl_qpolynomial_extract_affine(qp);
4668 if (!aff)
4669 goto error;
4670 dim = isl_qpolynomial_get_dim(qp);
4671 dim = isl_dim_from_domain(dim);
4672 pos = 1 + isl_dim_offset(dim, isl_dim_out);
4673 dim = isl_dim_add(dim, isl_dim_out, 1);
4674 n_div = qp->div->n_row;
4675 bmap = isl_basic_map_alloc_dim(dim, n_div, 1, 2 * n_div);
4677 for (i = 0; i < n_div; ++i) {
4678 k = isl_basic_map_alloc_div(bmap);
4679 if (k < 0)
4680 goto error;
4681 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4682 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4683 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4684 goto error;
4686 k = isl_basic_map_alloc_equality(bmap);
4687 if (k < 0)
4688 goto error;
4689 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4690 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4691 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4693 isl_vec_free(aff);
4694 isl_qpolynomial_free(qp);
4695 bmap = isl_basic_map_finalize(bmap);
4696 return bmap;
4697 error:
4698 isl_vec_free(aff);
4699 isl_qpolynomial_free(qp);
4700 isl_basic_map_free(bmap);
4701 return NULL;