export isl_pw_aff_coalesce
[isl.git] / isl_polynomial.c
blobce9158138542e324a5135f17b37aef9e60ed1511
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <stdlib.h>
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl/lp.h>
16 #include <isl/seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_polynomial_private.h>
19 #include <isl_point_private.h>
20 #include <isl_dim_private.h>
21 #include <isl_div_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_range.h>
24 #include <isl_local_space_private.h>
25 #include <isl_aff_private.h>
26 #include <isl_config.h>
28 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
30 switch (type) {
31 case isl_dim_param: return 0;
32 case isl_dim_in: return dim->nparam;
33 case isl_dim_out: return dim->nparam + dim->n_in;
34 default: return 0;
38 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
40 if (!up)
41 return -1;
43 return up->var < 0;
46 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
48 if (!up)
49 return NULL;
51 isl_assert(up->ctx, up->var < 0, return NULL);
53 return (struct isl_upoly_cst *)up;
56 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
58 if (!up)
59 return NULL;
61 isl_assert(up->ctx, up->var >= 0, return NULL);
63 return (struct isl_upoly_rec *)up;
66 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
67 __isl_keep struct isl_upoly *up2)
69 int i;
70 struct isl_upoly_rec *rec1, *rec2;
72 if (!up1 || !up2)
73 return -1;
74 if (up1 == up2)
75 return 1;
76 if (up1->var != up2->var)
77 return 0;
78 if (isl_upoly_is_cst(up1)) {
79 struct isl_upoly_cst *cst1, *cst2;
80 cst1 = isl_upoly_as_cst(up1);
81 cst2 = isl_upoly_as_cst(up2);
82 if (!cst1 || !cst2)
83 return -1;
84 return isl_int_eq(cst1->n, cst2->n) &&
85 isl_int_eq(cst1->d, cst2->d);
88 rec1 = isl_upoly_as_rec(up1);
89 rec2 = isl_upoly_as_rec(up2);
90 if (!rec1 || !rec2)
91 return -1;
93 if (rec1->n != rec2->n)
94 return 0;
96 for (i = 0; i < rec1->n; ++i) {
97 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
98 if (eq < 0 || !eq)
99 return eq;
102 return 1;
105 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
107 struct isl_upoly_cst *cst;
109 if (!up)
110 return -1;
111 if (!isl_upoly_is_cst(up))
112 return 0;
114 cst = isl_upoly_as_cst(up);
115 if (!cst)
116 return -1;
118 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
121 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
123 struct isl_upoly_cst *cst;
125 if (!up)
126 return 0;
127 if (!isl_upoly_is_cst(up))
128 return 0;
130 cst = isl_upoly_as_cst(up);
131 if (!cst)
132 return 0;
134 return isl_int_sgn(cst->n);
137 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
139 struct isl_upoly_cst *cst;
141 if (!up)
142 return -1;
143 if (!isl_upoly_is_cst(up))
144 return 0;
146 cst = isl_upoly_as_cst(up);
147 if (!cst)
148 return -1;
150 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
153 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
155 struct isl_upoly_cst *cst;
157 if (!up)
158 return -1;
159 if (!isl_upoly_is_cst(up))
160 return 0;
162 cst = isl_upoly_as_cst(up);
163 if (!cst)
164 return -1;
166 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
169 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
171 struct isl_upoly_cst *cst;
173 if (!up)
174 return -1;
175 if (!isl_upoly_is_cst(up))
176 return 0;
178 cst = isl_upoly_as_cst(up);
179 if (!cst)
180 return -1;
182 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
185 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
187 struct isl_upoly_cst *cst;
189 if (!up)
190 return -1;
191 if (!isl_upoly_is_cst(up))
192 return 0;
194 cst = isl_upoly_as_cst(up);
195 if (!cst)
196 return -1;
198 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
201 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
203 struct isl_upoly_cst *cst;
205 if (!up)
206 return -1;
207 if (!isl_upoly_is_cst(up))
208 return 0;
210 cst = isl_upoly_as_cst(up);
211 if (!cst)
212 return -1;
214 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
217 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
219 struct isl_upoly_cst *cst;
221 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
222 if (!cst)
223 return NULL;
225 cst->up.ref = 1;
226 cst->up.ctx = ctx;
227 isl_ctx_ref(ctx);
228 cst->up.var = -1;
230 isl_int_init(cst->n);
231 isl_int_init(cst->d);
233 return cst;
236 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
238 struct isl_upoly_cst *cst;
240 cst = isl_upoly_cst_alloc(ctx);
241 if (!cst)
242 return NULL;
244 isl_int_set_si(cst->n, 0);
245 isl_int_set_si(cst->d, 1);
247 return &cst->up;
250 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
252 struct isl_upoly_cst *cst;
254 cst = isl_upoly_cst_alloc(ctx);
255 if (!cst)
256 return NULL;
258 isl_int_set_si(cst->n, 1);
259 isl_int_set_si(cst->d, 1);
261 return &cst->up;
264 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
266 struct isl_upoly_cst *cst;
268 cst = isl_upoly_cst_alloc(ctx);
269 if (!cst)
270 return NULL;
272 isl_int_set_si(cst->n, 1);
273 isl_int_set_si(cst->d, 0);
275 return &cst->up;
278 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
280 struct isl_upoly_cst *cst;
282 cst = isl_upoly_cst_alloc(ctx);
283 if (!cst)
284 return NULL;
286 isl_int_set_si(cst->n, -1);
287 isl_int_set_si(cst->d, 0);
289 return &cst->up;
292 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
294 struct isl_upoly_cst *cst;
296 cst = isl_upoly_cst_alloc(ctx);
297 if (!cst)
298 return NULL;
300 isl_int_set_si(cst->n, 0);
301 isl_int_set_si(cst->d, 0);
303 return &cst->up;
306 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
307 isl_int n, isl_int d)
309 struct isl_upoly_cst *cst;
311 cst = isl_upoly_cst_alloc(ctx);
312 if (!cst)
313 return NULL;
315 isl_int_set(cst->n, n);
316 isl_int_set(cst->d, d);
318 return &cst->up;
321 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
322 int var, int size)
324 struct isl_upoly_rec *rec;
326 isl_assert(ctx, var >= 0, return NULL);
327 isl_assert(ctx, size >= 0, return NULL);
328 rec = isl_calloc(ctx, struct isl_upoly_rec,
329 sizeof(struct isl_upoly_rec) +
330 size * sizeof(struct isl_upoly *));
331 if (!rec)
332 return NULL;
334 rec->up.ref = 1;
335 rec->up.ctx = ctx;
336 isl_ctx_ref(ctx);
337 rec->up.var = var;
339 rec->n = 0;
340 rec->size = size;
342 return rec;
345 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
346 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
348 qp = isl_qpolynomial_cow(qp);
349 if (!qp || !dim)
350 goto error;
352 isl_dim_free(qp->dim);
353 qp->dim = dim;
355 return qp;
356 error:
357 isl_qpolynomial_free(qp);
358 isl_dim_free(dim);
359 return NULL;
362 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
364 return qp ? qp->dim->ctx : NULL;
367 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
369 return qp ? isl_dim_copy(qp->dim) : NULL;
372 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
373 enum isl_dim_type type)
375 return qp ? isl_dim_size(qp->dim, type) : 0;
378 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
380 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
383 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
385 return qp ? isl_upoly_is_one(qp->upoly) : -1;
388 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
390 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
393 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
395 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
398 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
400 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
403 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
405 return qp ? isl_upoly_sgn(qp->upoly) : 0;
408 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
410 isl_int_clear(cst->n);
411 isl_int_clear(cst->d);
414 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
416 int i;
418 for (i = 0; i < rec->n; ++i)
419 isl_upoly_free(rec->p[i]);
422 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
424 if (!up)
425 return NULL;
427 up->ref++;
428 return up;
431 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
433 struct isl_upoly_cst *cst;
434 struct isl_upoly_cst *dup;
436 cst = isl_upoly_as_cst(up);
437 if (!cst)
438 return NULL;
440 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
441 if (!dup)
442 return NULL;
443 isl_int_set(dup->n, cst->n);
444 isl_int_set(dup->d, cst->d);
446 return &dup->up;
449 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
451 int i;
452 struct isl_upoly_rec *rec;
453 struct isl_upoly_rec *dup;
455 rec = isl_upoly_as_rec(up);
456 if (!rec)
457 return NULL;
459 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
460 if (!dup)
461 return NULL;
463 for (i = 0; i < rec->n; ++i) {
464 dup->p[i] = isl_upoly_copy(rec->p[i]);
465 if (!dup->p[i])
466 goto error;
467 dup->n++;
470 return &dup->up;
471 error:
472 isl_upoly_free(&dup->up);
473 return NULL;
476 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
478 if (!up)
479 return NULL;
481 if (isl_upoly_is_cst(up))
482 return isl_upoly_dup_cst(up);
483 else
484 return isl_upoly_dup_rec(up);
487 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
489 if (!up)
490 return NULL;
492 if (up->ref == 1)
493 return up;
494 up->ref--;
495 return isl_upoly_dup(up);
498 void isl_upoly_free(__isl_take struct isl_upoly *up)
500 if (!up)
501 return;
503 if (--up->ref > 0)
504 return;
506 if (up->var < 0)
507 upoly_free_cst((struct isl_upoly_cst *)up);
508 else
509 upoly_free_rec((struct isl_upoly_rec *)up);
511 isl_ctx_deref(up->ctx);
512 free(up);
515 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
517 isl_int gcd;
519 isl_int_init(gcd);
520 isl_int_gcd(gcd, cst->n, cst->d);
521 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
522 isl_int_divexact(cst->n, cst->n, gcd);
523 isl_int_divexact(cst->d, cst->d, gcd);
525 isl_int_clear(gcd);
528 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
529 __isl_take struct isl_upoly *up2)
531 struct isl_upoly_cst *cst1;
532 struct isl_upoly_cst *cst2;
534 up1 = isl_upoly_cow(up1);
535 if (!up1 || !up2)
536 goto error;
538 cst1 = isl_upoly_as_cst(up1);
539 cst2 = isl_upoly_as_cst(up2);
541 if (isl_int_eq(cst1->d, cst2->d))
542 isl_int_add(cst1->n, cst1->n, cst2->n);
543 else {
544 isl_int_mul(cst1->n, cst1->n, cst2->d);
545 isl_int_addmul(cst1->n, cst2->n, cst1->d);
546 isl_int_mul(cst1->d, cst1->d, cst2->d);
549 isl_upoly_cst_reduce(cst1);
551 isl_upoly_free(up2);
552 return up1;
553 error:
554 isl_upoly_free(up1);
555 isl_upoly_free(up2);
556 return NULL;
559 static __isl_give struct isl_upoly *replace_by_zero(
560 __isl_take struct isl_upoly *up)
562 struct isl_ctx *ctx;
564 if (!up)
565 return NULL;
566 ctx = up->ctx;
567 isl_upoly_free(up);
568 return isl_upoly_zero(ctx);
571 static __isl_give struct isl_upoly *replace_by_constant_term(
572 __isl_take struct isl_upoly *up)
574 struct isl_upoly_rec *rec;
575 struct isl_upoly *cst;
577 if (!up)
578 return NULL;
580 rec = isl_upoly_as_rec(up);
581 if (!rec)
582 goto error;
583 cst = isl_upoly_copy(rec->p[0]);
584 isl_upoly_free(up);
585 return cst;
586 error:
587 isl_upoly_free(up);
588 return NULL;
591 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
592 __isl_take struct isl_upoly *up2)
594 int i;
595 struct isl_upoly_rec *rec1, *rec2;
597 if (!up1 || !up2)
598 goto error;
600 if (isl_upoly_is_nan(up1)) {
601 isl_upoly_free(up2);
602 return up1;
605 if (isl_upoly_is_nan(up2)) {
606 isl_upoly_free(up1);
607 return up2;
610 if (isl_upoly_is_zero(up1)) {
611 isl_upoly_free(up1);
612 return up2;
615 if (isl_upoly_is_zero(up2)) {
616 isl_upoly_free(up2);
617 return up1;
620 if (up1->var < up2->var)
621 return isl_upoly_sum(up2, up1);
623 if (up2->var < up1->var) {
624 struct isl_upoly_rec *rec;
625 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
626 isl_upoly_free(up1);
627 return up2;
629 up1 = isl_upoly_cow(up1);
630 rec = isl_upoly_as_rec(up1);
631 if (!rec)
632 goto error;
633 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
634 if (rec->n == 1)
635 up1 = replace_by_constant_term(up1);
636 return up1;
639 if (isl_upoly_is_cst(up1))
640 return isl_upoly_sum_cst(up1, up2);
642 rec1 = isl_upoly_as_rec(up1);
643 rec2 = isl_upoly_as_rec(up2);
644 if (!rec1 || !rec2)
645 goto error;
647 if (rec1->n < rec2->n)
648 return isl_upoly_sum(up2, up1);
650 up1 = isl_upoly_cow(up1);
651 rec1 = isl_upoly_as_rec(up1);
652 if (!rec1)
653 goto error;
655 for (i = rec2->n - 1; i >= 0; --i) {
656 rec1->p[i] = isl_upoly_sum(rec1->p[i],
657 isl_upoly_copy(rec2->p[i]));
658 if (!rec1->p[i])
659 goto error;
660 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
661 isl_upoly_free(rec1->p[i]);
662 rec1->n--;
666 if (rec1->n == 0)
667 up1 = replace_by_zero(up1);
668 else if (rec1->n == 1)
669 up1 = replace_by_constant_term(up1);
671 isl_upoly_free(up2);
673 return up1;
674 error:
675 isl_upoly_free(up1);
676 isl_upoly_free(up2);
677 return NULL;
680 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
681 __isl_take struct isl_upoly *up, isl_int v)
683 struct isl_upoly_cst *cst;
685 up = isl_upoly_cow(up);
686 if (!up)
687 return NULL;
689 cst = isl_upoly_as_cst(up);
691 isl_int_addmul(cst->n, cst->d, v);
693 return up;
696 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
697 __isl_take struct isl_upoly *up, isl_int v)
699 struct isl_upoly_rec *rec;
701 if (!up)
702 return NULL;
704 if (isl_upoly_is_cst(up))
705 return isl_upoly_cst_add_isl_int(up, v);
707 up = isl_upoly_cow(up);
708 rec = isl_upoly_as_rec(up);
709 if (!rec)
710 goto error;
712 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
713 if (!rec->p[0])
714 goto error;
716 return up;
717 error:
718 isl_upoly_free(up);
719 return NULL;
722 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
723 __isl_take struct isl_upoly *up, isl_int v)
725 struct isl_upoly_cst *cst;
727 if (isl_upoly_is_zero(up))
728 return up;
730 up = isl_upoly_cow(up);
731 if (!up)
732 return NULL;
734 cst = isl_upoly_as_cst(up);
736 isl_int_mul(cst->n, cst->n, v);
738 return up;
741 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
742 __isl_take struct isl_upoly *up, isl_int v)
744 int i;
745 struct isl_upoly_rec *rec;
747 if (!up)
748 return NULL;
750 if (isl_upoly_is_cst(up))
751 return isl_upoly_cst_mul_isl_int(up, v);
753 up = isl_upoly_cow(up);
754 rec = isl_upoly_as_rec(up);
755 if (!rec)
756 goto error;
758 for (i = 0; i < rec->n; ++i) {
759 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
760 if (!rec->p[i])
761 goto error;
764 return up;
765 error:
766 isl_upoly_free(up);
767 return NULL;
770 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
771 __isl_take struct isl_upoly *up2)
773 struct isl_upoly_cst *cst1;
774 struct isl_upoly_cst *cst2;
776 up1 = isl_upoly_cow(up1);
777 if (!up1 || !up2)
778 goto error;
780 cst1 = isl_upoly_as_cst(up1);
781 cst2 = isl_upoly_as_cst(up2);
783 isl_int_mul(cst1->n, cst1->n, cst2->n);
784 isl_int_mul(cst1->d, cst1->d, cst2->d);
786 isl_upoly_cst_reduce(cst1);
788 isl_upoly_free(up2);
789 return up1;
790 error:
791 isl_upoly_free(up1);
792 isl_upoly_free(up2);
793 return NULL;
796 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
797 __isl_take struct isl_upoly *up2)
799 struct isl_upoly_rec *rec1;
800 struct isl_upoly_rec *rec2;
801 struct isl_upoly_rec *res = NULL;
802 int i, j;
803 int size;
805 rec1 = isl_upoly_as_rec(up1);
806 rec2 = isl_upoly_as_rec(up2);
807 if (!rec1 || !rec2)
808 goto error;
809 size = rec1->n + rec2->n - 1;
810 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
811 if (!res)
812 goto error;
814 for (i = 0; i < rec1->n; ++i) {
815 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
816 isl_upoly_copy(rec1->p[i]));
817 if (!res->p[i])
818 goto error;
819 res->n++;
821 for (; i < size; ++i) {
822 res->p[i] = isl_upoly_zero(up1->ctx);
823 if (!res->p[i])
824 goto error;
825 res->n++;
827 for (i = 0; i < rec1->n; ++i) {
828 for (j = 1; j < rec2->n; ++j) {
829 struct isl_upoly *up;
830 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
831 isl_upoly_copy(rec1->p[i]));
832 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
833 if (!res->p[i + j])
834 goto error;
838 isl_upoly_free(up1);
839 isl_upoly_free(up2);
841 return &res->up;
842 error:
843 isl_upoly_free(up1);
844 isl_upoly_free(up2);
845 isl_upoly_free(&res->up);
846 return NULL;
849 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
850 __isl_take struct isl_upoly *up2)
852 if (!up1 || !up2)
853 goto error;
855 if (isl_upoly_is_nan(up1)) {
856 isl_upoly_free(up2);
857 return up1;
860 if (isl_upoly_is_nan(up2)) {
861 isl_upoly_free(up1);
862 return up2;
865 if (isl_upoly_is_zero(up1)) {
866 isl_upoly_free(up2);
867 return up1;
870 if (isl_upoly_is_zero(up2)) {
871 isl_upoly_free(up1);
872 return up2;
875 if (isl_upoly_is_one(up1)) {
876 isl_upoly_free(up1);
877 return up2;
880 if (isl_upoly_is_one(up2)) {
881 isl_upoly_free(up2);
882 return up1;
885 if (up1->var < up2->var)
886 return isl_upoly_mul(up2, up1);
888 if (up2->var < up1->var) {
889 int i;
890 struct isl_upoly_rec *rec;
891 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
892 isl_ctx *ctx = up1->ctx;
893 isl_upoly_free(up1);
894 isl_upoly_free(up2);
895 return isl_upoly_nan(ctx);
897 up1 = isl_upoly_cow(up1);
898 rec = isl_upoly_as_rec(up1);
899 if (!rec)
900 goto error;
902 for (i = 0; i < rec->n; ++i) {
903 rec->p[i] = isl_upoly_mul(rec->p[i],
904 isl_upoly_copy(up2));
905 if (!rec->p[i])
906 goto error;
908 isl_upoly_free(up2);
909 return up1;
912 if (isl_upoly_is_cst(up1))
913 return isl_upoly_mul_cst(up1, up2);
915 return isl_upoly_mul_rec(up1, up2);
916 error:
917 isl_upoly_free(up1);
918 isl_upoly_free(up2);
919 return NULL;
922 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
923 unsigned power)
925 struct isl_upoly *res;
927 if (!up)
928 return NULL;
929 if (power == 1)
930 return up;
932 if (power % 2)
933 res = isl_upoly_copy(up);
934 else
935 res = isl_upoly_one(up->ctx);
937 while (power >>= 1) {
938 up = isl_upoly_mul(up, isl_upoly_copy(up));
939 if (power % 2)
940 res = isl_upoly_mul(res, isl_upoly_copy(up));
943 isl_upoly_free(up);
944 return res;
947 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
948 unsigned n_div, __isl_take struct isl_upoly *up)
950 struct isl_qpolynomial *qp = NULL;
951 unsigned total;
953 if (!dim || !up)
954 goto error;
956 total = isl_dim_total(dim);
958 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
959 if (!qp)
960 goto error;
962 qp->ref = 1;
963 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
964 if (!qp->div)
965 goto error;
967 qp->dim = dim;
968 qp->upoly = up;
970 return qp;
971 error:
972 isl_dim_free(dim);
973 isl_upoly_free(up);
974 isl_qpolynomial_free(qp);
975 return NULL;
978 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
980 if (!qp)
981 return NULL;
983 qp->ref++;
984 return qp;
987 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
989 struct isl_qpolynomial *dup;
991 if (!qp)
992 return NULL;
994 dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
995 isl_upoly_copy(qp->upoly));
996 if (!dup)
997 return NULL;
998 isl_mat_free(dup->div);
999 dup->div = isl_mat_copy(qp->div);
1000 if (!dup->div)
1001 goto error;
1003 return dup;
1004 error:
1005 isl_qpolynomial_free(dup);
1006 return NULL;
1009 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1011 if (!qp)
1012 return NULL;
1014 if (qp->ref == 1)
1015 return qp;
1016 qp->ref--;
1017 return isl_qpolynomial_dup(qp);
1020 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1022 if (!qp)
1023 return;
1025 if (--qp->ref > 0)
1026 return;
1028 isl_dim_free(qp->dim);
1029 isl_mat_free(qp->div);
1030 isl_upoly_free(qp->upoly);
1032 free(qp);
1035 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1037 int i;
1038 struct isl_upoly_rec *rec;
1039 struct isl_upoly_cst *cst;
1041 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1042 if (!rec)
1043 return NULL;
1044 for (i = 0; i < 1 + power; ++i) {
1045 rec->p[i] = isl_upoly_zero(ctx);
1046 if (!rec->p[i])
1047 goto error;
1048 rec->n++;
1050 cst = isl_upoly_as_cst(rec->p[power]);
1051 isl_int_set_si(cst->n, 1);
1053 return &rec->up;
1054 error:
1055 isl_upoly_free(&rec->up);
1056 return NULL;
1059 /* r array maps original positions to new positions.
1061 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1062 int *r)
1064 int i;
1065 struct isl_upoly_rec *rec;
1066 struct isl_upoly *base;
1067 struct isl_upoly *res;
1069 if (isl_upoly_is_cst(up))
1070 return up;
1072 rec = isl_upoly_as_rec(up);
1073 if (!rec)
1074 goto error;
1076 isl_assert(up->ctx, rec->n >= 1, goto error);
1078 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1079 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1081 for (i = rec->n - 2; i >= 0; --i) {
1082 res = isl_upoly_mul(res, isl_upoly_copy(base));
1083 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1086 isl_upoly_free(base);
1087 isl_upoly_free(up);
1089 return res;
1090 error:
1091 isl_upoly_free(up);
1092 return NULL;
1095 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1097 int n_row, n_col;
1098 int equal;
1100 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1101 div1->n_col >= div2->n_col, return -1);
1103 if (div1->n_row == div2->n_row)
1104 return isl_mat_is_equal(div1, div2);
1106 n_row = div1->n_row;
1107 n_col = div1->n_col;
1108 div1->n_row = div2->n_row;
1109 div1->n_col = div2->n_col;
1111 equal = isl_mat_is_equal(div1, div2);
1113 div1->n_row = n_row;
1114 div1->n_col = n_col;
1116 return equal;
1119 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1121 int li, lj;
1123 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1124 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1126 if (li != lj)
1127 return li - lj;
1129 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1132 struct isl_div_sort_info {
1133 isl_mat *div;
1134 int row;
1137 static int div_sort_cmp(const void *p1, const void *p2)
1139 const struct isl_div_sort_info *i1, *i2;
1140 i1 = (const struct isl_div_sort_info *) p1;
1141 i2 = (const struct isl_div_sort_info *) p2;
1143 return cmp_row(i1->div, i1->row, i2->row);
1146 /* Sort divs and remove duplicates.
1148 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1150 int i;
1151 int skip;
1152 int len;
1153 struct isl_div_sort_info *array = NULL;
1154 int *pos = NULL, *at = NULL;
1155 int *reordering = NULL;
1156 unsigned div_pos;
1158 if (!qp)
1159 return NULL;
1160 if (qp->div->n_row <= 1)
1161 return qp;
1163 div_pos = isl_dim_total(qp->dim);
1165 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1166 qp->div->n_row);
1167 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1168 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1169 len = qp->div->n_col - 2;
1170 reordering = isl_alloc_array(qp->div->ctx, int, len);
1171 if (!array || !pos || !at || !reordering)
1172 goto error;
1174 for (i = 0; i < qp->div->n_row; ++i) {
1175 array[i].div = qp->div;
1176 array[i].row = i;
1177 pos[i] = i;
1178 at[i] = i;
1181 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1182 div_sort_cmp);
1184 for (i = 0; i < div_pos; ++i)
1185 reordering[i] = i;
1187 for (i = 0; i < qp->div->n_row; ++i) {
1188 if (pos[array[i].row] == i)
1189 continue;
1190 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1191 pos[at[i]] = pos[array[i].row];
1192 at[pos[array[i].row]] = at[i];
1193 at[i] = array[i].row;
1194 pos[array[i].row] = i;
1197 skip = 0;
1198 for (i = 0; i < len - div_pos; ++i) {
1199 if (i > 0 &&
1200 isl_seq_eq(qp->div->row[i - skip - 1],
1201 qp->div->row[i - skip], qp->div->n_col)) {
1202 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1203 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1204 2 + div_pos + i - skip);
1205 qp->div = isl_mat_drop_cols(qp->div,
1206 2 + div_pos + i - skip, 1);
1207 skip++;
1209 reordering[div_pos + array[i].row] = div_pos + i - skip;
1212 qp->upoly = reorder(qp->upoly, reordering);
1214 if (!qp->upoly || !qp->div)
1215 goto error;
1217 free(at);
1218 free(pos);
1219 free(array);
1220 free(reordering);
1222 return qp;
1223 error:
1224 free(at);
1225 free(pos);
1226 free(array);
1227 free(reordering);
1228 isl_qpolynomial_free(qp);
1229 return NULL;
1232 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1233 int *exp, int first)
1235 int i;
1236 struct isl_upoly_rec *rec;
1238 if (isl_upoly_is_cst(up))
1239 return up;
1241 if (up->var < first)
1242 return up;
1244 if (exp[up->var - first] == up->var - first)
1245 return up;
1247 up = isl_upoly_cow(up);
1248 if (!up)
1249 goto error;
1251 up->var = exp[up->var - first] + first;
1253 rec = isl_upoly_as_rec(up);
1254 if (!rec)
1255 goto error;
1257 for (i = 0; i < rec->n; ++i) {
1258 rec->p[i] = expand(rec->p[i], exp, first);
1259 if (!rec->p[i])
1260 goto error;
1263 return up;
1264 error:
1265 isl_upoly_free(up);
1266 return NULL;
1269 static __isl_give isl_qpolynomial *with_merged_divs(
1270 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1271 __isl_take isl_qpolynomial *qp2),
1272 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1274 int *exp1 = NULL;
1275 int *exp2 = NULL;
1276 isl_mat *div = NULL;
1278 qp1 = isl_qpolynomial_cow(qp1);
1279 qp2 = isl_qpolynomial_cow(qp2);
1281 if (!qp1 || !qp2)
1282 goto error;
1284 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1285 qp1->div->n_col >= qp2->div->n_col, goto error);
1287 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1288 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1289 if (!exp1 || !exp2)
1290 goto error;
1292 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1293 if (!div)
1294 goto error;
1296 isl_mat_free(qp1->div);
1297 qp1->div = isl_mat_copy(div);
1298 isl_mat_free(qp2->div);
1299 qp2->div = isl_mat_copy(div);
1301 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1302 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1304 if (!qp1->upoly || !qp2->upoly)
1305 goto error;
1307 isl_mat_free(div);
1308 free(exp1);
1309 free(exp2);
1311 return fn(qp1, qp2);
1312 error:
1313 isl_mat_free(div);
1314 free(exp1);
1315 free(exp2);
1316 isl_qpolynomial_free(qp1);
1317 isl_qpolynomial_free(qp2);
1318 return NULL;
1321 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1322 __isl_take isl_qpolynomial *qp2)
1324 qp1 = isl_qpolynomial_cow(qp1);
1326 if (!qp1 || !qp2)
1327 goto error;
1329 if (qp1->div->n_row < qp2->div->n_row)
1330 return isl_qpolynomial_add(qp2, qp1);
1332 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1333 if (!compatible_divs(qp1->div, qp2->div))
1334 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1336 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1337 if (!qp1->upoly)
1338 goto error;
1340 isl_qpolynomial_free(qp2);
1342 return qp1;
1343 error:
1344 isl_qpolynomial_free(qp1);
1345 isl_qpolynomial_free(qp2);
1346 return NULL;
1349 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1350 __isl_keep isl_set *dom,
1351 __isl_take isl_qpolynomial *qp1,
1352 __isl_take isl_qpolynomial *qp2)
1354 qp1 = isl_qpolynomial_add(qp1, qp2);
1355 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1356 return qp1;
1359 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1360 __isl_take isl_qpolynomial *qp2)
1362 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1365 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1366 __isl_take isl_qpolynomial *qp, isl_int v)
1368 if (isl_int_is_zero(v))
1369 return qp;
1371 qp = isl_qpolynomial_cow(qp);
1372 if (!qp)
1373 return NULL;
1375 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1376 if (!qp->upoly)
1377 goto error;
1379 return qp;
1380 error:
1381 isl_qpolynomial_free(qp);
1382 return NULL;
1386 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1388 if (!qp)
1389 return NULL;
1391 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1394 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1395 __isl_take isl_qpolynomial *qp, isl_int v)
1397 if (isl_int_is_one(v))
1398 return qp;
1400 if (qp && isl_int_is_zero(v)) {
1401 isl_qpolynomial *zero;
1402 zero = isl_qpolynomial_zero(isl_dim_copy(qp->dim));
1403 isl_qpolynomial_free(qp);
1404 return zero;
1407 qp = isl_qpolynomial_cow(qp);
1408 if (!qp)
1409 return NULL;
1411 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1412 if (!qp->upoly)
1413 goto error;
1415 return qp;
1416 error:
1417 isl_qpolynomial_free(qp);
1418 return NULL;
1421 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1422 __isl_take isl_qpolynomial *qp, isl_int v)
1424 return isl_qpolynomial_mul_isl_int(qp, v);
1427 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1428 __isl_take isl_qpolynomial *qp2)
1430 qp1 = isl_qpolynomial_cow(qp1);
1432 if (!qp1 || !qp2)
1433 goto error;
1435 if (qp1->div->n_row < qp2->div->n_row)
1436 return isl_qpolynomial_mul(qp2, qp1);
1438 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1439 if (!compatible_divs(qp1->div, qp2->div))
1440 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1442 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1443 if (!qp1->upoly)
1444 goto error;
1446 isl_qpolynomial_free(qp2);
1448 return qp1;
1449 error:
1450 isl_qpolynomial_free(qp1);
1451 isl_qpolynomial_free(qp2);
1452 return NULL;
1455 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1456 unsigned power)
1458 qp = isl_qpolynomial_cow(qp);
1460 if (!qp)
1461 return NULL;
1463 qp->upoly = isl_upoly_pow(qp->upoly, power);
1464 if (!qp->upoly)
1465 goto error;
1467 return qp;
1468 error:
1469 isl_qpolynomial_free(qp);
1470 return NULL;
1473 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1475 if (!dim)
1476 return NULL;
1477 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1480 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1482 if (!dim)
1483 return NULL;
1484 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1487 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1489 if (!dim)
1490 return NULL;
1491 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1494 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1496 if (!dim)
1497 return NULL;
1498 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1501 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1503 if (!dim)
1504 return NULL;
1505 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1508 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1509 isl_int v)
1511 struct isl_qpolynomial *qp;
1512 struct isl_upoly_cst *cst;
1514 if (!dim)
1515 return NULL;
1517 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1518 if (!qp)
1519 return NULL;
1521 cst = isl_upoly_as_cst(qp->upoly);
1522 isl_int_set(cst->n, v);
1524 return qp;
1527 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1528 isl_int *n, isl_int *d)
1530 struct isl_upoly_cst *cst;
1532 if (!qp)
1533 return -1;
1535 if (!isl_upoly_is_cst(qp->upoly))
1536 return 0;
1538 cst = isl_upoly_as_cst(qp->upoly);
1539 if (!cst)
1540 return -1;
1542 if (n)
1543 isl_int_set(*n, cst->n);
1544 if (d)
1545 isl_int_set(*d, cst->d);
1547 return 1;
1550 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1552 int is_cst;
1553 struct isl_upoly_rec *rec;
1555 if (!up)
1556 return -1;
1558 if (up->var < 0)
1559 return 1;
1561 rec = isl_upoly_as_rec(up);
1562 if (!rec)
1563 return -1;
1565 if (rec->n > 2)
1566 return 0;
1568 isl_assert(up->ctx, rec->n > 1, return -1);
1570 is_cst = isl_upoly_is_cst(rec->p[1]);
1571 if (is_cst < 0)
1572 return -1;
1573 if (!is_cst)
1574 return 0;
1576 return isl_upoly_is_affine(rec->p[0]);
1579 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1581 if (!qp)
1582 return -1;
1584 if (qp->div->n_row > 0)
1585 return 0;
1587 return isl_upoly_is_affine(qp->upoly);
1590 static void update_coeff(__isl_keep isl_vec *aff,
1591 __isl_keep struct isl_upoly_cst *cst, int pos)
1593 isl_int gcd;
1594 isl_int f;
1596 if (isl_int_is_zero(cst->n))
1597 return;
1599 isl_int_init(gcd);
1600 isl_int_init(f);
1601 isl_int_gcd(gcd, cst->d, aff->el[0]);
1602 isl_int_divexact(f, cst->d, gcd);
1603 isl_int_divexact(gcd, aff->el[0], gcd);
1604 isl_seq_scale(aff->el, aff->el, f, aff->size);
1605 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1606 isl_int_clear(gcd);
1607 isl_int_clear(f);
1610 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1611 __isl_keep isl_vec *aff)
1613 struct isl_upoly_cst *cst;
1614 struct isl_upoly_rec *rec;
1616 if (!up || !aff)
1617 return -1;
1619 if (up->var < 0) {
1620 struct isl_upoly_cst *cst;
1622 cst = isl_upoly_as_cst(up);
1623 if (!cst)
1624 return -1;
1625 update_coeff(aff, cst, 0);
1626 return 0;
1629 rec = isl_upoly_as_rec(up);
1630 if (!rec)
1631 return -1;
1632 isl_assert(up->ctx, rec->n == 2, return -1);
1634 cst = isl_upoly_as_cst(rec->p[1]);
1635 if (!cst)
1636 return -1;
1637 update_coeff(aff, cst, 1 + up->var);
1639 return isl_upoly_update_affine(rec->p[0], aff);
1642 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1643 __isl_keep isl_qpolynomial *qp)
1645 isl_vec *aff;
1646 unsigned d;
1648 if (!qp)
1649 return NULL;
1651 d = isl_dim_total(qp->dim);
1652 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1653 if (!aff)
1654 return NULL;
1656 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1657 isl_int_set_si(aff->el[0], 1);
1659 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1660 goto error;
1662 return aff;
1663 error:
1664 isl_vec_free(aff);
1665 return NULL;
1668 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
1669 __isl_keep isl_qpolynomial *qp2)
1671 int equal;
1673 if (!qp1 || !qp2)
1674 return -1;
1676 equal = isl_dim_equal(qp1->dim, qp2->dim);
1677 if (equal < 0 || !equal)
1678 return equal;
1680 equal = isl_mat_is_equal(qp1->div, qp2->div);
1681 if (equal < 0 || !equal)
1682 return equal;
1684 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1687 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1689 int i;
1690 struct isl_upoly_rec *rec;
1692 if (isl_upoly_is_cst(up)) {
1693 struct isl_upoly_cst *cst;
1694 cst = isl_upoly_as_cst(up);
1695 if (!cst)
1696 return;
1697 isl_int_lcm(*d, *d, cst->d);
1698 return;
1701 rec = isl_upoly_as_rec(up);
1702 if (!rec)
1703 return;
1705 for (i = 0; i < rec->n; ++i)
1706 upoly_update_den(rec->p[i], d);
1709 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1711 isl_int_set_si(*d, 1);
1712 if (!qp)
1713 return;
1714 upoly_update_den(qp->upoly, d);
1717 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1718 int pos, int power)
1720 struct isl_ctx *ctx;
1722 if (!dim)
1723 return NULL;
1725 ctx = dim->ctx;
1727 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1730 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1731 enum isl_dim_type type, unsigned pos)
1733 if (!dim)
1734 return NULL;
1736 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1737 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1739 if (type == isl_dim_set)
1740 pos += isl_dim_size(dim, isl_dim_param);
1742 return isl_qpolynomial_var_pow(dim, pos, 1);
1743 error:
1744 isl_dim_free(dim);
1745 return NULL;
1748 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1749 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1751 int i;
1752 struct isl_upoly_rec *rec;
1753 struct isl_upoly *base, *res;
1755 if (!up)
1756 return NULL;
1758 if (isl_upoly_is_cst(up))
1759 return up;
1761 if (up->var < first)
1762 return up;
1764 rec = isl_upoly_as_rec(up);
1765 if (!rec)
1766 goto error;
1768 isl_assert(up->ctx, rec->n >= 1, goto error);
1770 if (up->var >= first + n)
1771 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1772 else
1773 base = isl_upoly_copy(subs[up->var - first]);
1775 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1776 for (i = rec->n - 2; i >= 0; --i) {
1777 struct isl_upoly *t;
1778 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1779 res = isl_upoly_mul(res, isl_upoly_copy(base));
1780 res = isl_upoly_sum(res, t);
1783 isl_upoly_free(base);
1784 isl_upoly_free(up);
1786 return res;
1787 error:
1788 isl_upoly_free(up);
1789 return NULL;
1792 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1793 isl_int denom, unsigned len)
1795 int i;
1796 struct isl_upoly *up;
1798 isl_assert(ctx, len >= 1, return NULL);
1800 up = isl_upoly_rat_cst(ctx, f[0], denom);
1801 for (i = 0; i < len - 1; ++i) {
1802 struct isl_upoly *t;
1803 struct isl_upoly *c;
1805 if (isl_int_is_zero(f[1 + i]))
1806 continue;
1808 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1809 t = isl_upoly_var_pow(ctx, i, 1);
1810 t = isl_upoly_mul(c, t);
1811 up = isl_upoly_sum(up, t);
1814 return up;
1817 /* Remove common factor of non-constant terms and denominator.
1819 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1821 isl_ctx *ctx = qp->div->ctx;
1822 unsigned total = qp->div->n_col - 2;
1824 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1825 isl_int_gcd(ctx->normalize_gcd,
1826 ctx->normalize_gcd, qp->div->row[div][0]);
1827 if (isl_int_is_one(ctx->normalize_gcd))
1828 return;
1830 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1831 ctx->normalize_gcd, total);
1832 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1833 ctx->normalize_gcd);
1834 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1835 ctx->normalize_gcd);
1838 /* Replace the integer division identified by "div" by the polynomial "s".
1839 * The integer division is assumed not to appear in the definition
1840 * of any other integer divisions.
1842 static __isl_give isl_qpolynomial *substitute_div(
1843 __isl_take isl_qpolynomial *qp,
1844 int div, __isl_take struct isl_upoly *s)
1846 int i;
1847 int total;
1848 int *reordering;
1850 if (!qp || !s)
1851 goto error;
1853 qp = isl_qpolynomial_cow(qp);
1854 if (!qp)
1855 goto error;
1857 total = isl_dim_total(qp->dim);
1858 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1859 if (!qp->upoly)
1860 goto error;
1862 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1863 if (!reordering)
1864 goto error;
1865 for (i = 0; i < total + div; ++i)
1866 reordering[i] = i;
1867 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1868 reordering[i] = i - 1;
1869 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1870 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1871 qp->upoly = reorder(qp->upoly, reordering);
1872 free(reordering);
1874 if (!qp->upoly || !qp->div)
1875 goto error;
1877 isl_upoly_free(s);
1878 return qp;
1879 error:
1880 isl_qpolynomial_free(qp);
1881 isl_upoly_free(s);
1882 return NULL;
1885 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1886 * divisions because d is equal to 1 by their definition, i.e., e.
1888 static __isl_give isl_qpolynomial *substitute_non_divs(
1889 __isl_take isl_qpolynomial *qp)
1891 int i, j;
1892 int total;
1893 struct isl_upoly *s;
1895 if (!qp)
1896 return NULL;
1898 total = isl_dim_total(qp->dim);
1899 for (i = 0; qp && i < qp->div->n_row; ++i) {
1900 if (!isl_int_is_one(qp->div->row[i][0]))
1901 continue;
1902 for (j = i + 1; j < qp->div->n_row; ++j) {
1903 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1904 continue;
1905 isl_seq_combine(qp->div->row[j] + 1,
1906 qp->div->ctx->one, qp->div->row[j] + 1,
1907 qp->div->row[j][2 + total + i],
1908 qp->div->row[i] + 1, 1 + total + i);
1909 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1910 normalize_div(qp, j);
1912 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1913 qp->div->row[i][0], qp->div->n_col - 1);
1914 qp = substitute_div(qp, i, s);
1915 --i;
1918 return qp;
1921 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1922 * with d the denominator. When replacing the coefficient e of x by
1923 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1924 * inside the division, so we need to add floor(e/d) * x outside.
1925 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1926 * to adjust the coefficient of x in each later div that depends on the
1927 * current div "div" and also in the affine expression "aff"
1928 * (if it too depends on "div").
1930 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1931 __isl_keep isl_vec *aff)
1933 int i, j;
1934 isl_int v;
1935 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1937 isl_int_init(v);
1938 for (i = 0; i < 1 + total + div; ++i) {
1939 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1940 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1941 continue;
1942 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1943 isl_int_fdiv_r(qp->div->row[div][1 + i],
1944 qp->div->row[div][1 + i], qp->div->row[div][0]);
1945 if (!isl_int_is_zero(aff->el[1 + total + div]))
1946 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1947 for (j = div + 1; j < qp->div->n_row; ++j) {
1948 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1949 continue;
1950 isl_int_addmul(qp->div->row[j][1 + i],
1951 v, qp->div->row[j][2 + total + div]);
1954 isl_int_clear(v);
1957 /* Check if the last non-zero coefficient is bigger that half of the
1958 * denominator. If so, we will invert the div to further reduce the number
1959 * of distinct divs that may appear.
1960 * If the last non-zero coefficient is exactly half the denominator,
1961 * then we continue looking for earlier coefficients that are bigger
1962 * than half the denominator.
1964 static int needs_invert(__isl_keep isl_mat *div, int row)
1966 int i;
1967 int cmp;
1969 for (i = div->n_col - 1; i >= 1; --i) {
1970 if (isl_int_is_zero(div->row[row][i]))
1971 continue;
1972 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1973 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
1974 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
1975 if (cmp)
1976 return cmp > 0;
1977 if (i == 1)
1978 return 1;
1981 return 0;
1984 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
1985 * We only invert the coefficients of e (and the coefficient of q in
1986 * later divs and in "aff"). After calling this function, the
1987 * coefficients of e should be reduced again.
1989 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
1990 __isl_keep isl_vec *aff)
1992 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1994 isl_seq_neg(qp->div->row[div] + 1,
1995 qp->div->row[div] + 1, qp->div->n_col - 1);
1996 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
1997 isl_int_add(qp->div->row[div][1],
1998 qp->div->row[div][1], qp->div->row[div][0]);
1999 if (!isl_int_is_zero(aff->el[1 + total + div]))
2000 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2001 isl_mat_col_mul(qp->div, 2 + total + div,
2002 qp->div->ctx->negone, 2 + total + div);
2005 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2006 * in the interval [0, d-1], with d the denominator and such that the
2007 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2009 * After the reduction, some divs may have become redundant or identical,
2010 * so we call substitute_non_divs and sort_divs. If these functions
2011 * eliminate divs or merge two or more divs into one, the coefficients
2012 * of the enclosing divs may have to be reduced again, so we call
2013 * ourselves recursively if the number of divs decreases.
2015 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2017 int i;
2018 isl_vec *aff = NULL;
2019 struct isl_upoly *s;
2020 unsigned n_div;
2022 if (!qp)
2023 return NULL;
2025 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2026 aff = isl_vec_clr(aff);
2027 if (!aff)
2028 goto error;
2030 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2032 for (i = 0; i < qp->div->n_row; ++i) {
2033 normalize_div(qp, i);
2034 reduce_div(qp, i, aff);
2035 if (needs_invert(qp->div, i)) {
2036 invert_div(qp, i, aff);
2037 reduce_div(qp, i, aff);
2041 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2042 qp->div->ctx->one, aff->size);
2043 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2044 isl_upoly_free(s);
2045 if (!qp->upoly)
2046 goto error;
2048 isl_vec_free(aff);
2050 n_div = qp->div->n_row;
2051 qp = substitute_non_divs(qp);
2052 qp = sort_divs(qp);
2053 if (qp && qp->div->n_row < n_div)
2054 return reduce_divs(qp);
2056 return qp;
2057 error:
2058 isl_qpolynomial_free(qp);
2059 isl_vec_free(aff);
2060 return NULL;
2063 /* Assumes each div only depends on earlier divs.
2065 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2066 int power)
2068 struct isl_qpolynomial *qp = NULL;
2069 struct isl_upoly_rec *rec;
2070 struct isl_upoly_cst *cst;
2071 int i, d;
2072 int pos;
2074 if (!div)
2075 return NULL;
2077 d = div->line - div->bmap->div;
2079 pos = isl_dim_total(div->bmap->dim) + d;
2080 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2081 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2082 div->bmap->n_div, &rec->up);
2083 if (!qp)
2084 goto error;
2086 for (i = 0; i < div->bmap->n_div; ++i)
2087 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2089 for (i = 0; i < 1 + power; ++i) {
2090 rec->p[i] = isl_upoly_zero(div->ctx);
2091 if (!rec->p[i])
2092 goto error;
2093 rec->n++;
2095 cst = isl_upoly_as_cst(rec->p[power]);
2096 isl_int_set_si(cst->n, 1);
2098 isl_div_free(div);
2100 qp = reduce_divs(qp);
2102 return qp;
2103 error:
2104 isl_qpolynomial_free(qp);
2105 isl_div_free(div);
2106 return NULL;
2109 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2111 return isl_qpolynomial_div_pow(div, 1);
2114 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2115 const isl_int n, const isl_int d)
2117 struct isl_qpolynomial *qp;
2118 struct isl_upoly_cst *cst;
2120 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2121 if (!qp)
2122 return NULL;
2124 cst = isl_upoly_as_cst(qp->upoly);
2125 isl_int_set(cst->n, n);
2126 isl_int_set(cst->d, d);
2128 return qp;
2131 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2133 struct isl_upoly_rec *rec;
2134 int i;
2136 if (!up)
2137 return -1;
2139 if (isl_upoly_is_cst(up))
2140 return 0;
2142 if (up->var < d)
2143 active[up->var] = 1;
2145 rec = isl_upoly_as_rec(up);
2146 for (i = 0; i < rec->n; ++i)
2147 if (up_set_active(rec->p[i], active, d) < 0)
2148 return -1;
2150 return 0;
2153 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2155 int i, j;
2156 int d = isl_dim_total(qp->dim);
2158 if (!qp || !active)
2159 return -1;
2161 for (i = 0; i < d; ++i)
2162 for (j = 0; j < qp->div->n_row; ++j) {
2163 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2164 continue;
2165 active[i] = 1;
2166 break;
2169 return up_set_active(qp->upoly, active, d);
2172 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2173 enum isl_dim_type type, unsigned first, unsigned n)
2175 int i;
2176 int *active = NULL;
2177 int involves = 0;
2179 if (!qp)
2180 return -1;
2181 if (n == 0)
2182 return 0;
2184 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2185 return -1);
2186 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2187 type == isl_dim_set, return -1);
2189 active = isl_calloc_array(qp->dim->ctx, int, isl_dim_total(qp->dim));
2190 if (set_active(qp, active) < 0)
2191 goto error;
2193 if (type == isl_dim_set)
2194 first += isl_dim_size(qp->dim, isl_dim_param);
2195 for (i = 0; i < n; ++i)
2196 if (active[first + i]) {
2197 involves = 1;
2198 break;
2201 free(active);
2203 return involves;
2204 error:
2205 free(active);
2206 return -1;
2209 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2210 * of the divs that do appear in the quasi-polynomial.
2212 static __isl_give isl_qpolynomial *remove_redundant_divs(
2213 __isl_take isl_qpolynomial *qp)
2215 int i, j;
2216 int d;
2217 int len;
2218 int skip;
2219 int *active = NULL;
2220 int *reordering = NULL;
2221 int redundant = 0;
2222 int n_div;
2223 isl_ctx *ctx;
2225 if (!qp)
2226 return NULL;
2227 if (qp->div->n_row == 0)
2228 return qp;
2230 d = isl_dim_total(qp->dim);
2231 len = qp->div->n_col - 2;
2232 ctx = isl_qpolynomial_get_ctx(qp);
2233 active = isl_calloc_array(ctx, int, len);
2234 if (!active)
2235 goto error;
2237 if (up_set_active(qp->upoly, active, len) < 0)
2238 goto error;
2240 for (i = qp->div->n_row - 1; i >= 0; --i) {
2241 if (!active[d + i]) {
2242 redundant = 1;
2243 continue;
2245 for (j = 0; j < i; ++j) {
2246 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2247 continue;
2248 active[d + j] = 1;
2249 break;
2253 if (!redundant) {
2254 free(active);
2255 return qp;
2258 reordering = isl_alloc_array(qp->div->ctx, int, len);
2259 if (!reordering)
2260 goto error;
2262 for (i = 0; i < d; ++i)
2263 reordering[i] = i;
2265 skip = 0;
2266 n_div = qp->div->n_row;
2267 for (i = 0; i < n_div; ++i) {
2268 if (!active[d + i]) {
2269 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2270 qp->div = isl_mat_drop_cols(qp->div,
2271 2 + d + i - skip, 1);
2272 skip++;
2274 reordering[d + i] = d + i - skip;
2277 qp->upoly = reorder(qp->upoly, reordering);
2279 if (!qp->upoly || !qp->div)
2280 goto error;
2282 free(active);
2283 free(reordering);
2285 return qp;
2286 error:
2287 free(active);
2288 free(reordering);
2289 isl_qpolynomial_free(qp);
2290 return NULL;
2293 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2294 unsigned first, unsigned n)
2296 int i;
2297 struct isl_upoly_rec *rec;
2299 if (!up)
2300 return NULL;
2301 if (n == 0 || up->var < 0 || up->var < first)
2302 return up;
2303 if (up->var < first + n) {
2304 up = replace_by_constant_term(up);
2305 return isl_upoly_drop(up, first, n);
2307 up = isl_upoly_cow(up);
2308 if (!up)
2309 return NULL;
2310 up->var -= n;
2311 rec = isl_upoly_as_rec(up);
2312 if (!rec)
2313 goto error;
2315 for (i = 0; i < rec->n; ++i) {
2316 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2317 if (!rec->p[i])
2318 goto error;
2321 return up;
2322 error:
2323 isl_upoly_free(up);
2324 return NULL;
2327 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2328 __isl_take isl_qpolynomial *qp,
2329 enum isl_dim_type type, unsigned pos, const char *s)
2331 qp = isl_qpolynomial_cow(qp);
2332 if (!qp)
2333 return NULL;
2334 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2335 if (!qp->dim)
2336 goto error;
2337 return qp;
2338 error:
2339 isl_qpolynomial_free(qp);
2340 return NULL;
2343 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2344 __isl_take isl_qpolynomial *qp,
2345 enum isl_dim_type type, unsigned first, unsigned n)
2347 if (!qp)
2348 return NULL;
2349 if (n == 0 && !isl_dim_is_named_or_nested(qp->dim, type))
2350 return qp;
2352 qp = isl_qpolynomial_cow(qp);
2353 if (!qp)
2354 return NULL;
2356 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2357 goto error);
2358 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2359 type == isl_dim_set, goto error);
2361 qp->dim = isl_dim_drop(qp->dim, type, first, n);
2362 if (!qp->dim)
2363 goto error;
2365 if (type == isl_dim_set)
2366 first += isl_dim_size(qp->dim, isl_dim_param);
2368 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2369 if (!qp->div)
2370 goto error;
2372 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2373 if (!qp->upoly)
2374 goto error;
2376 return qp;
2377 error:
2378 isl_qpolynomial_free(qp);
2379 return NULL;
2382 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2383 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2385 int i, j, k;
2386 isl_int denom;
2387 unsigned total;
2388 unsigned n_div;
2389 struct isl_upoly *up;
2391 if (!eq)
2392 goto error;
2393 if (eq->n_eq == 0) {
2394 isl_basic_set_free(eq);
2395 return qp;
2398 qp = isl_qpolynomial_cow(qp);
2399 if (!qp)
2400 goto error;
2401 qp->div = isl_mat_cow(qp->div);
2402 if (!qp->div)
2403 goto error;
2405 total = 1 + isl_dim_total(eq->dim);
2406 n_div = eq->n_div;
2407 isl_int_init(denom);
2408 for (i = 0; i < eq->n_eq; ++i) {
2409 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2410 if (j < 0 || j == 0 || j >= total)
2411 continue;
2413 for (k = 0; k < qp->div->n_row; ++k) {
2414 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2415 continue;
2416 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2417 &qp->div->row[k][0]);
2418 normalize_div(qp, k);
2421 if (isl_int_is_pos(eq->eq[i][j]))
2422 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2423 isl_int_abs(denom, eq->eq[i][j]);
2424 isl_int_set_si(eq->eq[i][j], 0);
2426 up = isl_upoly_from_affine(qp->dim->ctx,
2427 eq->eq[i], denom, total);
2428 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2429 isl_upoly_free(up);
2431 isl_int_clear(denom);
2433 if (!qp->upoly)
2434 goto error;
2436 isl_basic_set_free(eq);
2438 qp = substitute_non_divs(qp);
2439 qp = sort_divs(qp);
2441 return qp;
2442 error:
2443 isl_basic_set_free(eq);
2444 isl_qpolynomial_free(qp);
2445 return NULL;
2448 static __isl_give isl_basic_set *add_div_constraints(
2449 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2451 int i;
2452 unsigned total;
2454 if (!bset || !div)
2455 goto error;
2457 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2458 if (!bset)
2459 goto error;
2460 total = isl_basic_set_total_dim(bset);
2461 for (i = 0; i < div->n_row; ++i)
2462 if (isl_basic_set_add_div_constraints_var(bset,
2463 total - div->n_row + i, div->row[i]) < 0)
2464 goto error;
2466 isl_mat_free(div);
2467 return bset;
2468 error:
2469 isl_mat_free(div);
2470 isl_basic_set_free(bset);
2471 return NULL;
2474 /* Look for equalities among the variables shared by context and qp
2475 * and the integer divisions of qp, if any.
2476 * The equalities are then used to eliminate variables and/or integer
2477 * divisions from qp.
2479 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2480 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2482 isl_basic_set *aff;
2484 if (!qp)
2485 goto error;
2486 if (qp->div->n_row > 0) {
2487 isl_basic_set *bset;
2488 context = isl_set_add_dims(context, isl_dim_set,
2489 qp->div->n_row);
2490 bset = isl_basic_set_universe(isl_set_get_dim(context));
2491 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2492 context = isl_set_intersect(context,
2493 isl_set_from_basic_set(bset));
2496 aff = isl_set_affine_hull(context);
2497 return isl_qpolynomial_substitute_equalities(qp, aff);
2498 error:
2499 isl_qpolynomial_free(qp);
2500 isl_set_free(context);
2501 return NULL;
2504 #undef PW
2505 #define PW isl_pw_qpolynomial
2506 #undef EL
2507 #define EL isl_qpolynomial
2508 #undef EL_IS_ZERO
2509 #define EL_IS_ZERO is_zero
2510 #undef ZERO
2511 #define ZERO zero
2512 #undef IS_ZERO
2513 #define IS_ZERO is_zero
2514 #undef FIELD
2515 #define FIELD qp
2517 #include <isl_pw_templ.c>
2519 #undef UNION
2520 #define UNION isl_union_pw_qpolynomial
2521 #undef PART
2522 #define PART isl_pw_qpolynomial
2523 #undef PARTS
2524 #define PARTS pw_qpolynomial
2526 #include <isl_union_templ.c>
2528 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2530 if (!pwqp)
2531 return -1;
2533 if (pwqp->n != -1)
2534 return 0;
2536 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2537 return 0;
2539 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2542 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2543 __isl_take isl_pw_qpolynomial *pwqp1,
2544 __isl_take isl_pw_qpolynomial *pwqp2)
2546 int i, j, n;
2547 struct isl_pw_qpolynomial *res;
2549 if (!pwqp1 || !pwqp2)
2550 goto error;
2552 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2553 goto error);
2555 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2556 isl_pw_qpolynomial_free(pwqp2);
2557 return pwqp1;
2560 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2561 isl_pw_qpolynomial_free(pwqp1);
2562 return pwqp2;
2565 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2566 isl_pw_qpolynomial_free(pwqp1);
2567 return pwqp2;
2570 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2571 isl_pw_qpolynomial_free(pwqp2);
2572 return pwqp1;
2575 n = pwqp1->n * pwqp2->n;
2576 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2578 for (i = 0; i < pwqp1->n; ++i) {
2579 for (j = 0; j < pwqp2->n; ++j) {
2580 struct isl_set *common;
2581 struct isl_qpolynomial *prod;
2582 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2583 isl_set_copy(pwqp2->p[j].set));
2584 if (isl_set_plain_is_empty(common)) {
2585 isl_set_free(common);
2586 continue;
2589 prod = isl_qpolynomial_mul(
2590 isl_qpolynomial_copy(pwqp1->p[i].qp),
2591 isl_qpolynomial_copy(pwqp2->p[j].qp));
2593 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2597 isl_pw_qpolynomial_free(pwqp1);
2598 isl_pw_qpolynomial_free(pwqp2);
2600 return res;
2601 error:
2602 isl_pw_qpolynomial_free(pwqp1);
2603 isl_pw_qpolynomial_free(pwqp2);
2604 return NULL;
2607 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2608 __isl_take isl_pw_qpolynomial *pwqp)
2610 int i;
2612 if (!pwqp)
2613 return NULL;
2615 if (isl_pw_qpolynomial_is_zero(pwqp))
2616 return pwqp;
2618 pwqp = isl_pw_qpolynomial_cow(pwqp);
2619 if (!pwqp)
2620 return NULL;
2622 for (i = 0; i < pwqp->n; ++i) {
2623 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2624 if (!pwqp->p[i].qp)
2625 goto error;
2628 return pwqp;
2629 error:
2630 isl_pw_qpolynomial_free(pwqp);
2631 return NULL;
2634 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2635 __isl_take isl_pw_qpolynomial *pwqp1,
2636 __isl_take isl_pw_qpolynomial *pwqp2)
2638 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2641 __isl_give struct isl_upoly *isl_upoly_eval(
2642 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2644 int i;
2645 struct isl_upoly_rec *rec;
2646 struct isl_upoly *res;
2647 struct isl_upoly *base;
2649 if (isl_upoly_is_cst(up)) {
2650 isl_vec_free(vec);
2651 return up;
2654 rec = isl_upoly_as_rec(up);
2655 if (!rec)
2656 goto error;
2658 isl_assert(up->ctx, rec->n >= 1, goto error);
2660 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2662 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2663 isl_vec_copy(vec));
2665 for (i = rec->n - 2; i >= 0; --i) {
2666 res = isl_upoly_mul(res, isl_upoly_copy(base));
2667 res = isl_upoly_sum(res,
2668 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2669 isl_vec_copy(vec)));
2672 isl_upoly_free(base);
2673 isl_upoly_free(up);
2674 isl_vec_free(vec);
2675 return res;
2676 error:
2677 isl_upoly_free(up);
2678 isl_vec_free(vec);
2679 return NULL;
2682 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2683 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2685 isl_vec *ext;
2686 struct isl_upoly *up;
2687 isl_dim *dim;
2689 if (!qp || !pnt)
2690 goto error;
2691 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2693 if (qp->div->n_row == 0)
2694 ext = isl_vec_copy(pnt->vec);
2695 else {
2696 int i;
2697 unsigned dim = isl_dim_total(qp->dim);
2698 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2699 if (!ext)
2700 goto error;
2702 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2703 for (i = 0; i < qp->div->n_row; ++i) {
2704 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2705 1 + dim + i, &ext->el[1+dim+i]);
2706 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2707 qp->div->row[i][0]);
2711 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2712 if (!up)
2713 goto error;
2715 dim = isl_dim_copy(qp->dim);
2716 isl_qpolynomial_free(qp);
2717 isl_point_free(pnt);
2719 return isl_qpolynomial_alloc(dim, 0, up);
2720 error:
2721 isl_qpolynomial_free(qp);
2722 isl_point_free(pnt);
2723 return NULL;
2726 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2727 __isl_keep struct isl_upoly_cst *cst2)
2729 int cmp;
2730 isl_int t;
2731 isl_int_init(t);
2732 isl_int_mul(t, cst1->n, cst2->d);
2733 isl_int_submul(t, cst2->n, cst1->d);
2734 cmp = isl_int_sgn(t);
2735 isl_int_clear(t);
2736 return cmp;
2739 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2740 __isl_keep isl_qpolynomial *qp2)
2742 struct isl_upoly_cst *cst1, *cst2;
2744 if (!qp1 || !qp2)
2745 return -1;
2746 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2747 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2748 if (isl_qpolynomial_is_nan(qp1))
2749 return -1;
2750 if (isl_qpolynomial_is_nan(qp2))
2751 return -1;
2752 cst1 = isl_upoly_as_cst(qp1->upoly);
2753 cst2 = isl_upoly_as_cst(qp2->upoly);
2755 return isl_upoly_cmp(cst1, cst2) <= 0;
2758 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2759 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2761 struct isl_upoly_cst *cst1, *cst2;
2762 int cmp;
2764 if (!qp1 || !qp2)
2765 goto error;
2766 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2767 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2768 cst1 = isl_upoly_as_cst(qp1->upoly);
2769 cst2 = isl_upoly_as_cst(qp2->upoly);
2770 cmp = isl_upoly_cmp(cst1, cst2);
2772 if (cmp <= 0) {
2773 isl_qpolynomial_free(qp2);
2774 } else {
2775 isl_qpolynomial_free(qp1);
2776 qp1 = qp2;
2778 return qp1;
2779 error:
2780 isl_qpolynomial_free(qp1);
2781 isl_qpolynomial_free(qp2);
2782 return NULL;
2785 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2786 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2788 struct isl_upoly_cst *cst1, *cst2;
2789 int cmp;
2791 if (!qp1 || !qp2)
2792 goto error;
2793 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2794 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2795 cst1 = isl_upoly_as_cst(qp1->upoly);
2796 cst2 = isl_upoly_as_cst(qp2->upoly);
2797 cmp = isl_upoly_cmp(cst1, cst2);
2799 if (cmp >= 0) {
2800 isl_qpolynomial_free(qp2);
2801 } else {
2802 isl_qpolynomial_free(qp1);
2803 qp1 = qp2;
2805 return qp1;
2806 error:
2807 isl_qpolynomial_free(qp1);
2808 isl_qpolynomial_free(qp2);
2809 return NULL;
2812 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2813 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2814 unsigned first, unsigned n)
2816 unsigned total;
2817 unsigned g_pos;
2818 int *exp;
2820 if (n == 0 && !isl_dim_is_named_or_nested(qp->dim, type))
2821 return qp;
2823 qp = isl_qpolynomial_cow(qp);
2824 if (!qp)
2825 return NULL;
2827 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2828 goto error);
2830 g_pos = pos(qp->dim, type) + first;
2832 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
2833 if (!qp->div)
2834 goto error;
2836 total = qp->div->n_col - 2;
2837 if (total > g_pos) {
2838 int i;
2839 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2840 if (!exp)
2841 goto error;
2842 for (i = 0; i < total - g_pos; ++i)
2843 exp[i] = i + n;
2844 qp->upoly = expand(qp->upoly, exp, g_pos);
2845 free(exp);
2846 if (!qp->upoly)
2847 goto error;
2850 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2851 if (!qp->dim)
2852 goto error;
2854 return qp;
2855 error:
2856 isl_qpolynomial_free(qp);
2857 return NULL;
2860 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2861 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2863 unsigned pos;
2865 pos = isl_qpolynomial_dim(qp, type);
2867 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2870 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2871 __isl_take isl_pw_qpolynomial *pwqp,
2872 enum isl_dim_type type, unsigned n)
2874 unsigned pos;
2876 pos = isl_pw_qpolynomial_dim(pwqp, type);
2878 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2881 static int *reordering_move(isl_ctx *ctx,
2882 unsigned len, unsigned dst, unsigned src, unsigned n)
2884 int i;
2885 int *reordering;
2887 reordering = isl_alloc_array(ctx, int, len);
2888 if (!reordering)
2889 return NULL;
2891 if (dst <= src) {
2892 for (i = 0; i < dst; ++i)
2893 reordering[i] = i;
2894 for (i = 0; i < n; ++i)
2895 reordering[src + i] = dst + i;
2896 for (i = 0; i < src - dst; ++i)
2897 reordering[dst + i] = dst + n + i;
2898 for (i = 0; i < len - src - n; ++i)
2899 reordering[src + n + i] = src + n + i;
2900 } else {
2901 for (i = 0; i < src; ++i)
2902 reordering[i] = i;
2903 for (i = 0; i < n; ++i)
2904 reordering[src + i] = dst + i;
2905 for (i = 0; i < dst - src; ++i)
2906 reordering[src + n + i] = src + i;
2907 for (i = 0; i < len - dst - n; ++i)
2908 reordering[dst + n + i] = dst + n + i;
2911 return reordering;
2914 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2915 __isl_take isl_qpolynomial *qp,
2916 enum isl_dim_type dst_type, unsigned dst_pos,
2917 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2919 unsigned g_dst_pos;
2920 unsigned g_src_pos;
2921 int *reordering;
2923 qp = isl_qpolynomial_cow(qp);
2924 if (!qp)
2925 return NULL;
2927 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2928 goto error);
2930 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2931 g_src_pos = pos(qp->dim, src_type) + src_pos;
2932 if (dst_type > src_type)
2933 g_dst_pos -= n;
2935 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2936 if (!qp->div)
2937 goto error;
2938 qp = sort_divs(qp);
2939 if (!qp)
2940 goto error;
2942 reordering = reordering_move(qp->dim->ctx,
2943 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2944 if (!reordering)
2945 goto error;
2947 qp->upoly = reorder(qp->upoly, reordering);
2948 free(reordering);
2949 if (!qp->upoly)
2950 goto error;
2952 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2953 if (!qp->dim)
2954 goto error;
2956 return qp;
2957 error:
2958 isl_qpolynomial_free(qp);
2959 return NULL;
2962 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2963 isl_int *f, isl_int denom)
2965 struct isl_upoly *up;
2967 if (!dim)
2968 return NULL;
2970 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2972 return isl_qpolynomial_alloc(dim, 0, up);
2975 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
2977 isl_ctx *ctx;
2978 struct isl_upoly *up;
2979 isl_qpolynomial *qp;
2981 if (!aff)
2982 return NULL;
2984 ctx = isl_aff_get_ctx(aff);
2985 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
2986 aff->v->size - 1);
2988 qp = isl_qpolynomial_alloc(isl_aff_get_dim(aff),
2989 aff->ls->div->n_row, up);
2990 if (!qp)
2991 goto error;
2993 isl_mat_free(qp->div);
2994 qp->div = isl_mat_copy(aff->ls->div);
2995 qp->div = isl_mat_cow(qp->div);
2996 if (!qp->div)
2997 goto error;
2999 isl_aff_free(aff);
3000 qp = reduce_divs(qp);
3001 qp = remove_redundant_divs(qp);
3002 return qp;
3003 error:
3004 isl_aff_free(aff);
3005 return NULL;
3008 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3009 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3011 isl_int denom;
3012 isl_dim *dim;
3013 struct isl_upoly *up;
3014 isl_qpolynomial *qp;
3015 int sgn;
3017 if (!c)
3018 return NULL;
3020 isl_int_init(denom);
3022 isl_constraint_get_coefficient(c, type, pos, &denom);
3023 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
3024 sgn = isl_int_sgn(denom);
3025 isl_int_abs(denom, denom);
3026 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
3027 1 + isl_constraint_dim(c, isl_dim_all));
3028 if (sgn < 0)
3029 isl_int_neg(denom, denom);
3030 isl_constraint_set_coefficient(c, type, pos, denom);
3032 dim = isl_dim_copy(c->bmap->dim);
3034 isl_int_clear(denom);
3035 isl_constraint_free(c);
3037 qp = isl_qpolynomial_alloc(dim, 0, up);
3038 if (sgn > 0)
3039 qp = isl_qpolynomial_neg(qp);
3040 return qp;
3043 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3044 * in "qp" by subs[i].
3046 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3047 __isl_take isl_qpolynomial *qp,
3048 enum isl_dim_type type, unsigned first, unsigned n,
3049 __isl_keep isl_qpolynomial **subs)
3051 int i;
3052 struct isl_upoly **ups;
3054 if (n == 0)
3055 return qp;
3057 qp = isl_qpolynomial_cow(qp);
3058 if (!qp)
3059 return NULL;
3060 for (i = 0; i < n; ++i)
3061 if (!subs[i])
3062 goto error;
3064 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
3065 goto error);
3067 for (i = 0; i < n; ++i)
3068 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
3069 goto error);
3071 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3072 for (i = 0; i < n; ++i)
3073 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3075 first += pos(qp->dim, type);
3077 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3078 if (!ups)
3079 goto error;
3080 for (i = 0; i < n; ++i)
3081 ups[i] = subs[i]->upoly;
3083 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3085 free(ups);
3087 if (!qp->upoly)
3088 goto error;
3090 return qp;
3091 error:
3092 isl_qpolynomial_free(qp);
3093 return NULL;
3096 /* Extend "bset" with extra set dimensions for each integer division
3097 * in "qp" and then call "fn" with the extended bset and the polynomial
3098 * that results from replacing each of the integer divisions by the
3099 * corresponding extra set dimension.
3101 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3102 __isl_keep isl_basic_set *bset,
3103 int (*fn)(__isl_take isl_basic_set *bset,
3104 __isl_take isl_qpolynomial *poly, void *user), void *user)
3106 isl_dim *dim;
3107 isl_mat *div;
3108 isl_qpolynomial *poly;
3110 if (!qp || !bset)
3111 goto error;
3112 if (qp->div->n_row == 0)
3113 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3114 user);
3116 div = isl_mat_copy(qp->div);
3117 dim = isl_dim_copy(qp->dim);
3118 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3119 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3120 bset = isl_basic_set_copy(bset);
3121 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3122 bset = add_div_constraints(bset, div);
3124 return fn(bset, poly, user);
3125 error:
3126 return -1;
3129 /* Return total degree in variables first (inclusive) up to last (exclusive).
3131 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3133 int deg = -1;
3134 int i;
3135 struct isl_upoly_rec *rec;
3137 if (!up)
3138 return -2;
3139 if (isl_upoly_is_zero(up))
3140 return -1;
3141 if (isl_upoly_is_cst(up) || up->var < first)
3142 return 0;
3144 rec = isl_upoly_as_rec(up);
3145 if (!rec)
3146 return -2;
3148 for (i = 0; i < rec->n; ++i) {
3149 int d;
3151 if (isl_upoly_is_zero(rec->p[i]))
3152 continue;
3153 d = isl_upoly_degree(rec->p[i], first, last);
3154 if (up->var < last)
3155 d += i;
3156 if (d > deg)
3157 deg = d;
3160 return deg;
3163 /* Return total degree in set variables.
3165 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3167 unsigned ovar;
3168 unsigned nvar;
3170 if (!poly)
3171 return -2;
3173 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3174 nvar = isl_dim_size(poly->dim, isl_dim_set);
3175 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3178 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3179 unsigned pos, int deg)
3181 int i;
3182 struct isl_upoly_rec *rec;
3184 if (!up)
3185 return NULL;
3187 if (isl_upoly_is_cst(up) || up->var < pos) {
3188 if (deg == 0)
3189 return isl_upoly_copy(up);
3190 else
3191 return isl_upoly_zero(up->ctx);
3194 rec = isl_upoly_as_rec(up);
3195 if (!rec)
3196 return NULL;
3198 if (up->var == pos) {
3199 if (deg < rec->n)
3200 return isl_upoly_copy(rec->p[deg]);
3201 else
3202 return isl_upoly_zero(up->ctx);
3205 up = isl_upoly_copy(up);
3206 up = isl_upoly_cow(up);
3207 rec = isl_upoly_as_rec(up);
3208 if (!rec)
3209 goto error;
3211 for (i = 0; i < rec->n; ++i) {
3212 struct isl_upoly *t;
3213 t = isl_upoly_coeff(rec->p[i], pos, deg);
3214 if (!t)
3215 goto error;
3216 isl_upoly_free(rec->p[i]);
3217 rec->p[i] = t;
3220 return up;
3221 error:
3222 isl_upoly_free(up);
3223 return NULL;
3226 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3228 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3229 __isl_keep isl_qpolynomial *qp,
3230 enum isl_dim_type type, unsigned t_pos, int deg)
3232 unsigned g_pos;
3233 struct isl_upoly *up;
3234 isl_qpolynomial *c;
3236 if (!qp)
3237 return NULL;
3239 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3240 return NULL);
3242 g_pos = pos(qp->dim, type) + t_pos;
3243 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3245 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3246 if (!c)
3247 return NULL;
3248 isl_mat_free(c->div);
3249 c->div = isl_mat_copy(qp->div);
3250 if (!c->div)
3251 goto error;
3252 return c;
3253 error:
3254 isl_qpolynomial_free(c);
3255 return NULL;
3258 /* Homogenize the polynomial in the variables first (inclusive) up to
3259 * last (exclusive) by inserting powers of variable first.
3260 * Variable first is assumed not to appear in the input.
3262 __isl_give struct isl_upoly *isl_upoly_homogenize(
3263 __isl_take struct isl_upoly *up, int deg, int target,
3264 int first, int last)
3266 int i;
3267 struct isl_upoly_rec *rec;
3269 if (!up)
3270 return NULL;
3271 if (isl_upoly_is_zero(up))
3272 return up;
3273 if (deg == target)
3274 return up;
3275 if (isl_upoly_is_cst(up) || up->var < first) {
3276 struct isl_upoly *hom;
3278 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3279 if (!hom)
3280 goto error;
3281 rec = isl_upoly_as_rec(hom);
3282 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3284 return hom;
3287 up = isl_upoly_cow(up);
3288 rec = isl_upoly_as_rec(up);
3289 if (!rec)
3290 goto error;
3292 for (i = 0; i < rec->n; ++i) {
3293 if (isl_upoly_is_zero(rec->p[i]))
3294 continue;
3295 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3296 up->var < last ? deg + i : i, target,
3297 first, last);
3298 if (!rec->p[i])
3299 goto error;
3302 return up;
3303 error:
3304 isl_upoly_free(up);
3305 return NULL;
3308 /* Homogenize the polynomial in the set variables by introducing
3309 * powers of an extra set variable at position 0.
3311 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3312 __isl_take isl_qpolynomial *poly)
3314 unsigned ovar;
3315 unsigned nvar;
3316 int deg = isl_qpolynomial_degree(poly);
3318 if (deg < -1)
3319 goto error;
3321 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3322 poly = isl_qpolynomial_cow(poly);
3323 if (!poly)
3324 goto error;
3326 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3327 nvar = isl_dim_size(poly->dim, isl_dim_set);
3328 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3329 ovar, ovar + nvar);
3330 if (!poly->upoly)
3331 goto error;
3333 return poly;
3334 error:
3335 isl_qpolynomial_free(poly);
3336 return NULL;
3339 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3340 __isl_take isl_mat *div)
3342 isl_term *term;
3343 int n;
3345 if (!dim || !div)
3346 goto error;
3348 n = isl_dim_total(dim) + div->n_row;
3350 term = isl_calloc(dim->ctx, struct isl_term,
3351 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3352 if (!term)
3353 goto error;
3355 term->ref = 1;
3356 term->dim = dim;
3357 term->div = div;
3358 isl_int_init(term->n);
3359 isl_int_init(term->d);
3361 return term;
3362 error:
3363 isl_dim_free(dim);
3364 isl_mat_free(div);
3365 return NULL;
3368 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3370 if (!term)
3371 return NULL;
3373 term->ref++;
3374 return term;
3377 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3379 int i;
3380 isl_term *dup;
3381 unsigned total;
3383 if (term)
3384 return NULL;
3386 total = isl_dim_total(term->dim) + term->div->n_row;
3388 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3389 if (!dup)
3390 return NULL;
3392 isl_int_set(dup->n, term->n);
3393 isl_int_set(dup->d, term->d);
3395 for (i = 0; i < total; ++i)
3396 dup->pow[i] = term->pow[i];
3398 return dup;
3401 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3403 if (!term)
3404 return NULL;
3406 if (term->ref == 1)
3407 return term;
3408 term->ref--;
3409 return isl_term_dup(term);
3412 void isl_term_free(__isl_take isl_term *term)
3414 if (!term)
3415 return;
3417 if (--term->ref > 0)
3418 return;
3420 isl_dim_free(term->dim);
3421 isl_mat_free(term->div);
3422 isl_int_clear(term->n);
3423 isl_int_clear(term->d);
3424 free(term);
3427 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3429 if (!term)
3430 return 0;
3432 switch (type) {
3433 case isl_dim_param:
3434 case isl_dim_in:
3435 case isl_dim_out: return isl_dim_size(term->dim, type);
3436 case isl_dim_div: return term->div->n_row;
3437 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3438 default: return 0;
3442 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3444 return term ? term->dim->ctx : NULL;
3447 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3449 if (!term)
3450 return;
3451 isl_int_set(*n, term->n);
3454 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3456 if (!term)
3457 return;
3458 isl_int_set(*d, term->d);
3461 int isl_term_get_exp(__isl_keep isl_term *term,
3462 enum isl_dim_type type, unsigned pos)
3464 if (!term)
3465 return -1;
3467 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3469 if (type >= isl_dim_set)
3470 pos += isl_dim_size(term->dim, isl_dim_param);
3471 if (type >= isl_dim_div)
3472 pos += isl_dim_size(term->dim, isl_dim_set);
3474 return term->pow[pos];
3477 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3479 isl_basic_map *bmap;
3480 unsigned total;
3481 int k;
3483 if (!term)
3484 return NULL;
3486 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3487 return NULL);
3489 total = term->div->n_col - term->div->n_row - 2;
3490 /* No nested divs for now */
3491 isl_assert(term->dim->ctx,
3492 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3493 term->div->n_row) == -1,
3494 return NULL);
3496 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3497 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3498 goto error;
3500 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3502 return isl_basic_map_div(bmap, k);
3503 error:
3504 isl_basic_map_free(bmap);
3505 return NULL;
3508 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3509 int (*fn)(__isl_take isl_term *term, void *user),
3510 __isl_take isl_term *term, void *user)
3512 int i;
3513 struct isl_upoly_rec *rec;
3515 if (!up || !term)
3516 goto error;
3518 if (isl_upoly_is_zero(up))
3519 return term;
3521 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3522 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3523 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3525 if (isl_upoly_is_cst(up)) {
3526 struct isl_upoly_cst *cst;
3527 cst = isl_upoly_as_cst(up);
3528 if (!cst)
3529 goto error;
3530 term = isl_term_cow(term);
3531 if (!term)
3532 goto error;
3533 isl_int_set(term->n, cst->n);
3534 isl_int_set(term->d, cst->d);
3535 if (fn(isl_term_copy(term), user) < 0)
3536 goto error;
3537 return term;
3540 rec = isl_upoly_as_rec(up);
3541 if (!rec)
3542 goto error;
3544 for (i = 0; i < rec->n; ++i) {
3545 term = isl_term_cow(term);
3546 if (!term)
3547 goto error;
3548 term->pow[up->var] = i;
3549 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3550 if (!term)
3551 goto error;
3553 term->pow[up->var] = 0;
3555 return term;
3556 error:
3557 isl_term_free(term);
3558 return NULL;
3561 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3562 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3564 isl_term *term;
3566 if (!qp)
3567 return -1;
3569 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3570 if (!term)
3571 return -1;
3573 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3575 isl_term_free(term);
3577 return term ? 0 : -1;
3580 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3582 struct isl_upoly *up;
3583 isl_qpolynomial *qp;
3584 int i, n;
3586 if (!term)
3587 return NULL;
3589 n = isl_dim_total(term->dim) + term->div->n_row;
3591 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3592 for (i = 0; i < n; ++i) {
3593 if (!term->pow[i])
3594 continue;
3595 up = isl_upoly_mul(up,
3596 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3599 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3600 if (!qp)
3601 goto error;
3602 isl_mat_free(qp->div);
3603 qp->div = isl_mat_copy(term->div);
3604 if (!qp->div)
3605 goto error;
3607 isl_term_free(term);
3608 return qp;
3609 error:
3610 isl_qpolynomial_free(qp);
3611 isl_term_free(term);
3612 return NULL;
3615 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3616 __isl_take isl_dim *dim)
3618 int i;
3619 int extra;
3620 unsigned total;
3622 if (!qp || !dim)
3623 goto error;
3625 if (isl_dim_equal(qp->dim, dim)) {
3626 isl_dim_free(dim);
3627 return qp;
3630 qp = isl_qpolynomial_cow(qp);
3631 if (!qp)
3632 goto error;
3634 extra = isl_dim_size(dim, isl_dim_set) -
3635 isl_dim_size(qp->dim, isl_dim_set);
3636 total = isl_dim_total(qp->dim);
3637 if (qp->div->n_row) {
3638 int *exp;
3640 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3641 if (!exp)
3642 goto error;
3643 for (i = 0; i < qp->div->n_row; ++i)
3644 exp[i] = extra + i;
3645 qp->upoly = expand(qp->upoly, exp, total);
3646 free(exp);
3647 if (!qp->upoly)
3648 goto error;
3650 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3651 if (!qp->div)
3652 goto error;
3653 for (i = 0; i < qp->div->n_row; ++i)
3654 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3656 isl_dim_free(qp->dim);
3657 qp->dim = dim;
3659 return qp;
3660 error:
3661 isl_dim_free(dim);
3662 isl_qpolynomial_free(qp);
3663 return NULL;
3666 /* For each parameter or variable that does not appear in qp,
3667 * first eliminate the variable from all constraints and then set it to zero.
3669 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3670 __isl_keep isl_qpolynomial *qp)
3672 int *active = NULL;
3673 int i;
3674 int d;
3675 unsigned nparam;
3676 unsigned nvar;
3678 if (!set || !qp)
3679 goto error;
3681 d = isl_dim_total(set->dim);
3682 active = isl_calloc_array(set->ctx, int, d);
3683 if (set_active(qp, active) < 0)
3684 goto error;
3686 for (i = 0; i < d; ++i)
3687 if (!active[i])
3688 break;
3690 if (i == d) {
3691 free(active);
3692 return set;
3695 nparam = isl_dim_size(set->dim, isl_dim_param);
3696 nvar = isl_dim_size(set->dim, isl_dim_set);
3697 for (i = 0; i < nparam; ++i) {
3698 if (active[i])
3699 continue;
3700 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3701 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3703 for (i = 0; i < nvar; ++i) {
3704 if (active[nparam + i])
3705 continue;
3706 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3707 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3710 free(active);
3712 return set;
3713 error:
3714 free(active);
3715 isl_set_free(set);
3716 return NULL;
3719 struct isl_opt_data {
3720 isl_qpolynomial *qp;
3721 int first;
3722 isl_qpolynomial *opt;
3723 int max;
3726 static int opt_fn(__isl_take isl_point *pnt, void *user)
3728 struct isl_opt_data *data = (struct isl_opt_data *)user;
3729 isl_qpolynomial *val;
3731 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3732 if (data->first) {
3733 data->first = 0;
3734 data->opt = val;
3735 } else if (data->max) {
3736 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3737 } else {
3738 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3741 return 0;
3744 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3745 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3747 struct isl_opt_data data = { NULL, 1, NULL, max };
3749 if (!set || !qp)
3750 goto error;
3752 if (isl_upoly_is_cst(qp->upoly)) {
3753 isl_set_free(set);
3754 return qp;
3757 set = fix_inactive(set, qp);
3759 data.qp = qp;
3760 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3761 goto error;
3763 if (data.first)
3764 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3766 isl_set_free(set);
3767 isl_qpolynomial_free(qp);
3768 return data.opt;
3769 error:
3770 isl_set_free(set);
3771 isl_qpolynomial_free(qp);
3772 isl_qpolynomial_free(data.opt);
3773 return NULL;
3776 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3777 __isl_take isl_morph *morph)
3779 int i;
3780 int n_sub;
3781 isl_ctx *ctx;
3782 struct isl_upoly **subs;
3783 isl_mat *mat;
3785 qp = isl_qpolynomial_cow(qp);
3786 if (!qp || !morph)
3787 goto error;
3789 ctx = qp->dim->ctx;
3790 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3792 n_sub = morph->inv->n_row - 1;
3793 if (morph->inv->n_row != morph->inv->n_col)
3794 n_sub += qp->div->n_row;
3795 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3796 if (!subs)
3797 goto error;
3799 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3800 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3801 morph->inv->row[0][0], morph->inv->n_col);
3802 if (morph->inv->n_row != morph->inv->n_col)
3803 for (i = 0; i < qp->div->n_row; ++i)
3804 subs[morph->inv->n_row - 1 + i] =
3805 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3807 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3809 for (i = 0; i < n_sub; ++i)
3810 isl_upoly_free(subs[i]);
3811 free(subs);
3813 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3814 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3815 qp->div = isl_mat_product(qp->div, mat);
3816 isl_dim_free(qp->dim);
3817 qp->dim = isl_dim_copy(morph->ran->dim);
3819 if (!qp->upoly || !qp->div || !qp->dim)
3820 goto error;
3822 isl_morph_free(morph);
3824 return qp;
3825 error:
3826 isl_qpolynomial_free(qp);
3827 isl_morph_free(morph);
3828 return NULL;
3831 static int neg_entry(void **entry, void *user)
3833 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3835 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3837 return *pwqp ? 0 : -1;
3840 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3841 __isl_take isl_union_pw_qpolynomial *upwqp)
3843 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3844 if (!upwqp)
3845 return NULL;
3847 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3848 &neg_entry, NULL) < 0)
3849 goto error;
3851 return upwqp;
3852 error:
3853 isl_union_pw_qpolynomial_free(upwqp);
3854 return NULL;
3857 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3858 __isl_take isl_union_pw_qpolynomial *upwqp1,
3859 __isl_take isl_union_pw_qpolynomial *upwqp2)
3861 return isl_union_pw_qpolynomial_add(upwqp1,
3862 isl_union_pw_qpolynomial_neg(upwqp2));
3865 static int mul_entry(void **entry, void *user)
3867 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3868 uint32_t hash;
3869 struct isl_hash_table_entry *entry2;
3870 isl_pw_qpolynomial *pwpq = *entry;
3871 int empty;
3873 hash = isl_dim_get_hash(pwpq->dim);
3874 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3875 hash, &has_dim, pwpq->dim, 0);
3876 if (!entry2)
3877 return 0;
3879 pwpq = isl_pw_qpolynomial_copy(pwpq);
3880 pwpq = isl_pw_qpolynomial_mul(pwpq,
3881 isl_pw_qpolynomial_copy(entry2->data));
3883 empty = isl_pw_qpolynomial_is_zero(pwpq);
3884 if (empty < 0) {
3885 isl_pw_qpolynomial_free(pwpq);
3886 return -1;
3888 if (empty) {
3889 isl_pw_qpolynomial_free(pwpq);
3890 return 0;
3893 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3895 return 0;
3898 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3899 __isl_take isl_union_pw_qpolynomial *upwqp1,
3900 __isl_take isl_union_pw_qpolynomial *upwqp2)
3902 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3905 /* Reorder the columns of the given div definitions according to the
3906 * given reordering.
3908 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3909 __isl_take isl_reordering *r)
3911 int i, j;
3912 isl_mat *mat;
3913 int extra;
3915 if (!div || !r)
3916 goto error;
3918 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3919 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3920 if (!mat)
3921 goto error;
3923 for (i = 0; i < div->n_row; ++i) {
3924 isl_seq_cpy(mat->row[i], div->row[i], 2);
3925 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3926 for (j = 0; j < r->len; ++j)
3927 isl_int_set(mat->row[i][2 + r->pos[j]],
3928 div->row[i][2 + j]);
3931 isl_reordering_free(r);
3932 isl_mat_free(div);
3933 return mat;
3934 error:
3935 isl_reordering_free(r);
3936 isl_mat_free(div);
3937 return NULL;
3940 /* Reorder the dimension of "qp" according to the given reordering.
3942 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3943 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3945 qp = isl_qpolynomial_cow(qp);
3946 if (!qp)
3947 goto error;
3949 r = isl_reordering_extend(r, qp->div->n_row);
3950 if (!r)
3951 goto error;
3953 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3954 if (!qp->div)
3955 goto error;
3957 qp->upoly = reorder(qp->upoly, r->pos);
3958 if (!qp->upoly)
3959 goto error;
3961 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3963 isl_reordering_free(r);
3964 return qp;
3965 error:
3966 isl_qpolynomial_free(qp);
3967 isl_reordering_free(r);
3968 return NULL;
3971 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
3972 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *model)
3974 if (!qp || !model)
3975 goto error;
3977 if (!isl_dim_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
3978 isl_reordering *exp;
3980 model = isl_dim_drop(model, isl_dim_in,
3981 0, isl_dim_size(model, isl_dim_in));
3982 model = isl_dim_drop(model, isl_dim_out,
3983 0, isl_dim_size(model, isl_dim_out));
3984 exp = isl_parameter_alignment_reordering(qp->dim, model);
3985 exp = isl_reordering_extend_dim(exp,
3986 isl_qpolynomial_get_dim(qp));
3987 qp = isl_qpolynomial_realign(qp, exp);
3990 isl_dim_free(model);
3991 return qp;
3992 error:
3993 isl_dim_free(model);
3994 isl_qpolynomial_free(qp);
3995 return NULL;
3998 struct isl_split_periods_data {
3999 int max_periods;
4000 isl_pw_qpolynomial *res;
4003 /* Create a slice where the integer division "div" has the fixed value "v".
4004 * In particular, if "div" refers to floor(f/m), then create a slice
4006 * m v <= f <= m v + (m - 1)
4008 * or
4010 * f - m v >= 0
4011 * -f + m v + (m - 1) >= 0
4013 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
4014 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4016 int total;
4017 isl_basic_set *bset = NULL;
4018 int k;
4020 if (!dim || !qp)
4021 goto error;
4023 total = isl_dim_total(dim);
4024 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
4026 k = isl_basic_set_alloc_inequality(bset);
4027 if (k < 0)
4028 goto error;
4029 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4030 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4032 k = isl_basic_set_alloc_inequality(bset);
4033 if (k < 0)
4034 goto error;
4035 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4036 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4037 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4038 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4040 isl_dim_free(dim);
4041 return isl_set_from_basic_set(bset);
4042 error:
4043 isl_basic_set_free(bset);
4044 isl_dim_free(dim);
4045 return NULL;
4048 static int split_periods(__isl_take isl_set *set,
4049 __isl_take isl_qpolynomial *qp, void *user);
4051 /* Create a slice of the domain "set" such that integer division "div"
4052 * has the fixed value "v" and add the results to data->res,
4053 * replacing the integer division by "v" in "qp".
4055 static int set_div(__isl_take isl_set *set,
4056 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4057 struct isl_split_periods_data *data)
4059 int i;
4060 int total;
4061 isl_set *slice;
4062 struct isl_upoly *cst;
4064 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
4065 set = isl_set_intersect(set, slice);
4067 if (!qp)
4068 goto error;
4070 total = isl_dim_total(qp->dim);
4072 for (i = div + 1; i < qp->div->n_row; ++i) {
4073 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4074 continue;
4075 isl_int_addmul(qp->div->row[i][1],
4076 qp->div->row[i][2 + total + div], v);
4077 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4080 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4081 qp = substitute_div(qp, div, cst);
4083 return split_periods(set, qp, data);
4084 error:
4085 isl_set_free(set);
4086 isl_qpolynomial_free(qp);
4087 return -1;
4090 /* Split the domain "set" such that integer division "div"
4091 * has a fixed value (ranging from "min" to "max") on each slice
4092 * and add the results to data->res.
4094 static int split_div(__isl_take isl_set *set,
4095 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4096 struct isl_split_periods_data *data)
4098 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4099 isl_set *set_i = isl_set_copy(set);
4100 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4102 if (set_div(set_i, qp_i, div, min, data) < 0)
4103 goto error;
4105 isl_set_free(set);
4106 isl_qpolynomial_free(qp);
4107 return 0;
4108 error:
4109 isl_set_free(set);
4110 isl_qpolynomial_free(qp);
4111 return -1;
4114 /* If "qp" refers to any integer division
4115 * that can only attain "max_periods" distinct values on "set"
4116 * then split the domain along those distinct values.
4117 * Add the results (or the original if no splitting occurs)
4118 * to data->res.
4120 static int split_periods(__isl_take isl_set *set,
4121 __isl_take isl_qpolynomial *qp, void *user)
4123 int i;
4124 isl_pw_qpolynomial *pwqp;
4125 struct isl_split_periods_data *data;
4126 isl_int min, max;
4127 int total;
4128 int r = 0;
4130 data = (struct isl_split_periods_data *)user;
4132 if (!set || !qp)
4133 goto error;
4135 if (qp->div->n_row == 0) {
4136 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4137 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4138 return 0;
4141 isl_int_init(min);
4142 isl_int_init(max);
4143 total = isl_dim_total(qp->dim);
4144 for (i = 0; i < qp->div->n_row; ++i) {
4145 enum isl_lp_result lp_res;
4147 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4148 qp->div->n_row) != -1)
4149 continue;
4151 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4152 set->ctx->one, &min, NULL, NULL);
4153 if (lp_res == isl_lp_error)
4154 goto error2;
4155 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4156 continue;
4157 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4159 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4160 set->ctx->one, &max, NULL, NULL);
4161 if (lp_res == isl_lp_error)
4162 goto error2;
4163 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4164 continue;
4165 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4167 isl_int_sub(max, max, min);
4168 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4169 isl_int_add(max, max, min);
4170 break;
4174 if (i < qp->div->n_row) {
4175 r = split_div(set, qp, i, min, max, data);
4176 } else {
4177 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4178 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4181 isl_int_clear(max);
4182 isl_int_clear(min);
4184 return r;
4185 error2:
4186 isl_int_clear(max);
4187 isl_int_clear(min);
4188 error:
4189 isl_set_free(set);
4190 isl_qpolynomial_free(qp);
4191 return -1;
4194 /* If any quasi-polynomial in pwqp refers to any integer division
4195 * that can only attain "max_periods" distinct values on its domain
4196 * then split the domain along those distinct values.
4198 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4199 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4201 struct isl_split_periods_data data;
4203 data.max_periods = max_periods;
4204 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4206 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4207 goto error;
4209 isl_pw_qpolynomial_free(pwqp);
4211 return data.res;
4212 error:
4213 isl_pw_qpolynomial_free(data.res);
4214 isl_pw_qpolynomial_free(pwqp);
4215 return NULL;
4218 /* Construct a piecewise quasipolynomial that is constant on the given
4219 * domain. In particular, it is
4220 * 0 if cst == 0
4221 * 1 if cst == 1
4222 * infinity if cst == -1
4224 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4225 __isl_take isl_basic_set *bset, int cst)
4227 isl_dim *dim;
4228 isl_qpolynomial *qp;
4230 if (!bset)
4231 return NULL;
4233 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4234 dim = isl_basic_set_get_dim(bset);
4235 if (cst < 0)
4236 qp = isl_qpolynomial_infty(dim);
4237 else if (cst == 0)
4238 qp = isl_qpolynomial_zero(dim);
4239 else
4240 qp = isl_qpolynomial_one(dim);
4241 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4244 /* Factor bset, call fn on each of the factors and return the product.
4246 * If no factors can be found, simply call fn on the input.
4247 * Otherwise, construct the factors based on the factorizer,
4248 * call fn on each factor and compute the product.
4250 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4251 __isl_take isl_basic_set *bset,
4252 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4254 int i, n;
4255 isl_dim *dim;
4256 isl_set *set;
4257 isl_factorizer *f;
4258 isl_qpolynomial *qp;
4259 isl_pw_qpolynomial *pwqp;
4260 unsigned nparam;
4261 unsigned nvar;
4263 f = isl_basic_set_factorizer(bset);
4264 if (!f)
4265 goto error;
4266 if (f->n_group == 0) {
4267 isl_factorizer_free(f);
4268 return fn(bset);
4271 nparam = isl_basic_set_dim(bset, isl_dim_param);
4272 nvar = isl_basic_set_dim(bset, isl_dim_set);
4274 dim = isl_basic_set_get_dim(bset);
4275 dim = isl_dim_domain(dim);
4276 set = isl_set_universe(isl_dim_copy(dim));
4277 qp = isl_qpolynomial_one(dim);
4278 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4280 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4282 for (i = 0, n = 0; i < f->n_group; ++i) {
4283 isl_basic_set *bset_i;
4284 isl_pw_qpolynomial *pwqp_i;
4286 bset_i = isl_basic_set_copy(bset);
4287 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4288 nparam + n + f->len[i], nvar - n - f->len[i]);
4289 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4290 nparam, n);
4291 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4292 n + f->len[i], nvar - n - f->len[i]);
4293 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4295 pwqp_i = fn(bset_i);
4296 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4298 n += f->len[i];
4301 isl_basic_set_free(bset);
4302 isl_factorizer_free(f);
4304 return pwqp;
4305 error:
4306 isl_basic_set_free(bset);
4307 return NULL;
4310 /* Factor bset, call fn on each of the factors and return the product.
4311 * The function is assumed to evaluate to zero on empty domains,
4312 * to one on zero-dimensional domains and to infinity on unbounded domains
4313 * and will not be called explicitly on zero-dimensional or unbounded domains.
4315 * We first check for some special cases and remove all equalities.
4316 * Then we hand over control to compressed_multiplicative_call.
4318 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4319 __isl_take isl_basic_set *bset,
4320 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4322 int bounded;
4323 isl_morph *morph;
4324 isl_pw_qpolynomial *pwqp;
4325 unsigned orig_nvar, final_nvar;
4327 if (!bset)
4328 return NULL;
4330 if (isl_basic_set_plain_is_empty(bset))
4331 return constant_on_domain(bset, 0);
4333 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4335 if (orig_nvar == 0)
4336 return constant_on_domain(bset, 1);
4338 bounded = isl_basic_set_is_bounded(bset);
4339 if (bounded < 0)
4340 goto error;
4341 if (!bounded)
4342 return constant_on_domain(bset, -1);
4344 if (bset->n_eq == 0)
4345 return compressed_multiplicative_call(bset, fn);
4347 morph = isl_basic_set_full_compression(bset);
4348 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4350 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4352 pwqp = compressed_multiplicative_call(bset, fn);
4354 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4355 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4356 morph = isl_morph_inverse(morph);
4358 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4360 return pwqp;
4361 error:
4362 isl_basic_set_free(bset);
4363 return NULL;
4366 /* Drop all floors in "qp", turning each integer division [a/m] into
4367 * a rational division a/m. If "down" is set, then the integer division
4368 * is replaces by (a-(m-1))/m instead.
4370 static __isl_give isl_qpolynomial *qp_drop_floors(
4371 __isl_take isl_qpolynomial *qp, int down)
4373 int i;
4374 struct isl_upoly *s;
4376 if (!qp)
4377 return NULL;
4378 if (qp->div->n_row == 0)
4379 return qp;
4381 qp = isl_qpolynomial_cow(qp);
4382 if (!qp)
4383 return NULL;
4385 for (i = qp->div->n_row - 1; i >= 0; --i) {
4386 if (down) {
4387 isl_int_sub(qp->div->row[i][1],
4388 qp->div->row[i][1], qp->div->row[i][0]);
4389 isl_int_add_ui(qp->div->row[i][1],
4390 qp->div->row[i][1], 1);
4392 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4393 qp->div->row[i][0], qp->div->n_col - 1);
4394 qp = substitute_div(qp, i, s);
4395 if (!qp)
4396 return NULL;
4399 return qp;
4402 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4403 * a rational division a/m.
4405 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4406 __isl_take isl_pw_qpolynomial *pwqp)
4408 int i;
4410 if (!pwqp)
4411 return NULL;
4413 if (isl_pw_qpolynomial_is_zero(pwqp))
4414 return pwqp;
4416 pwqp = isl_pw_qpolynomial_cow(pwqp);
4417 if (!pwqp)
4418 return NULL;
4420 for (i = 0; i < pwqp->n; ++i) {
4421 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4422 if (!pwqp->p[i].qp)
4423 goto error;
4426 return pwqp;
4427 error:
4428 isl_pw_qpolynomial_free(pwqp);
4429 return NULL;
4432 /* Adjust all the integer divisions in "qp" such that they are at least
4433 * one over the given orthant (identified by "signs"). This ensures
4434 * that they will still be non-negative even after subtracting (m-1)/m.
4436 * In particular, f is replaced by f' + v, changing f = [a/m]
4437 * to f' = [(a - m v)/m].
4438 * If the constant term k in a is smaller than m,
4439 * the constant term of v is set to floor(k/m) - 1.
4440 * For any other term, if the coefficient c and the variable x have
4441 * the same sign, then no changes are needed.
4442 * Otherwise, if the variable is positive (and c is negative),
4443 * then the coefficient of x in v is set to floor(c/m).
4444 * If the variable is negative (and c is positive),
4445 * then the coefficient of x in v is set to ceil(c/m).
4447 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4448 int *signs)
4450 int i, j;
4451 int total;
4452 isl_vec *v = NULL;
4453 struct isl_upoly *s;
4455 qp = isl_qpolynomial_cow(qp);
4456 if (!qp)
4457 return NULL;
4458 qp->div = isl_mat_cow(qp->div);
4459 if (!qp->div)
4460 goto error;
4462 total = isl_dim_total(qp->dim);
4463 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4465 for (i = 0; i < qp->div->n_row; ++i) {
4466 isl_int *row = qp->div->row[i];
4467 v = isl_vec_clr(v);
4468 if (!v)
4469 goto error;
4470 if (isl_int_lt(row[1], row[0])) {
4471 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4472 isl_int_sub_ui(v->el[0], v->el[0], 1);
4473 isl_int_submul(row[1], row[0], v->el[0]);
4475 for (j = 0; j < total; ++j) {
4476 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4477 continue;
4478 if (signs[j] < 0)
4479 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4480 else
4481 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4482 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4484 for (j = 0; j < i; ++j) {
4485 if (isl_int_sgn(row[2 + total + j]) >= 0)
4486 continue;
4487 isl_int_fdiv_q(v->el[1 + total + j],
4488 row[2 + total + j], row[0]);
4489 isl_int_submul(row[2 + total + j],
4490 row[0], v->el[1 + total + j]);
4492 for (j = i + 1; j < qp->div->n_row; ++j) {
4493 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4494 continue;
4495 isl_seq_combine(qp->div->row[j] + 1,
4496 qp->div->ctx->one, qp->div->row[j] + 1,
4497 qp->div->row[j][2 + total + i], v->el, v->size);
4499 isl_int_set_si(v->el[1 + total + i], 1);
4500 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4501 qp->div->ctx->one, v->size);
4502 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4503 isl_upoly_free(s);
4504 if (!qp->upoly)
4505 goto error;
4508 isl_vec_free(v);
4509 return qp;
4510 error:
4511 isl_vec_free(v);
4512 isl_qpolynomial_free(qp);
4513 return NULL;
4516 struct isl_to_poly_data {
4517 int sign;
4518 isl_pw_qpolynomial *res;
4519 isl_qpolynomial *qp;
4522 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4523 * We first make all integer divisions positive and then split the
4524 * quasipolynomials into terms with sign data->sign (the direction
4525 * of the requested approximation) and terms with the opposite sign.
4526 * In the first set of terms, each integer division [a/m] is
4527 * overapproximated by a/m, while in the second it is underapproximated
4528 * by (a-(m-1))/m.
4530 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4531 void *user)
4533 struct isl_to_poly_data *data = user;
4534 isl_pw_qpolynomial *t;
4535 isl_qpolynomial *qp, *up, *down;
4537 qp = isl_qpolynomial_copy(data->qp);
4538 qp = make_divs_pos(qp, signs);
4540 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4541 up = qp_drop_floors(up, 0);
4542 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4543 down = qp_drop_floors(down, 1);
4545 isl_qpolynomial_free(qp);
4546 qp = isl_qpolynomial_add(up, down);
4548 t = isl_pw_qpolynomial_alloc(orthant, qp);
4549 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4551 return 0;
4554 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4555 * the polynomial will be an overapproximation. If "sign" is negative,
4556 * it will be an underapproximation. If "sign" is zero, the approximation
4557 * will lie somewhere in between.
4559 * In particular, is sign == 0, we simply drop the floors, turning
4560 * the integer divisions into rational divisions.
4561 * Otherwise, we split the domains into orthants, make all integer divisions
4562 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4563 * depending on the requested sign and the sign of the term in which
4564 * the integer division appears.
4566 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4567 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4569 int i;
4570 struct isl_to_poly_data data;
4572 if (sign == 0)
4573 return pwqp_drop_floors(pwqp);
4575 if (!pwqp)
4576 return NULL;
4578 data.sign = sign;
4579 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4581 for (i = 0; i < pwqp->n; ++i) {
4582 if (pwqp->p[i].qp->div->n_row == 0) {
4583 isl_pw_qpolynomial *t;
4584 t = isl_pw_qpolynomial_alloc(
4585 isl_set_copy(pwqp->p[i].set),
4586 isl_qpolynomial_copy(pwqp->p[i].qp));
4587 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4588 continue;
4590 data.qp = pwqp->p[i].qp;
4591 if (isl_set_foreach_orthant(pwqp->p[i].set,
4592 &to_polynomial_on_orthant, &data) < 0)
4593 goto error;
4596 isl_pw_qpolynomial_free(pwqp);
4598 return data.res;
4599 error:
4600 isl_pw_qpolynomial_free(pwqp);
4601 isl_pw_qpolynomial_free(data.res);
4602 return NULL;
4605 static int poly_entry(void **entry, void *user)
4607 int *sign = user;
4608 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4610 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4612 return *pwqp ? 0 : -1;
4615 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4616 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4618 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4619 if (!upwqp)
4620 return NULL;
4622 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4623 &poly_entry, &sign) < 0)
4624 goto error;
4626 return upwqp;
4627 error:
4628 isl_union_pw_qpolynomial_free(upwqp);
4629 return NULL;
4632 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4633 __isl_take isl_qpolynomial *qp)
4635 int i, k;
4636 isl_dim *dim;
4637 isl_vec *aff = NULL;
4638 isl_basic_map *bmap = NULL;
4639 unsigned pos;
4640 unsigned n_div;
4642 if (!qp)
4643 return NULL;
4644 if (!isl_upoly_is_affine(qp->upoly))
4645 isl_die(qp->dim->ctx, isl_error_invalid,
4646 "input quasi-polynomial not affine", goto error);
4647 aff = isl_qpolynomial_extract_affine(qp);
4648 if (!aff)
4649 goto error;
4650 dim = isl_qpolynomial_get_dim(qp);
4651 dim = isl_dim_from_domain(dim);
4652 pos = 1 + isl_dim_offset(dim, isl_dim_out);
4653 dim = isl_dim_add(dim, isl_dim_out, 1);
4654 n_div = qp->div->n_row;
4655 bmap = isl_basic_map_alloc_dim(dim, n_div, 1, 2 * n_div);
4657 for (i = 0; i < n_div; ++i) {
4658 k = isl_basic_map_alloc_div(bmap);
4659 if (k < 0)
4660 goto error;
4661 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4662 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4663 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4664 goto error;
4666 k = isl_basic_map_alloc_equality(bmap);
4667 if (k < 0)
4668 goto error;
4669 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4670 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4671 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4673 isl_vec_free(aff);
4674 isl_qpolynomial_free(qp);
4675 bmap = isl_basic_map_finalize(bmap);
4676 return bmap;
4677 error:
4678 isl_vec_free(aff);
4679 isl_qpolynomial_free(qp);
4680 isl_basic_map_free(bmap);
4681 return NULL;