add isl_basic_map_flat_range_product
[isl.git] / isl_polynomial.c
blob7eae4f8829155991767e68c6d1938488e4e3a009
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_mul(__isl_take isl_qpolynomial *qp1,
1422 __isl_take isl_qpolynomial *qp2)
1424 qp1 = isl_qpolynomial_cow(qp1);
1426 if (!qp1 || !qp2)
1427 goto error;
1429 if (qp1->div->n_row < qp2->div->n_row)
1430 return isl_qpolynomial_mul(qp2, qp1);
1432 isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1433 if (!compatible_divs(qp1->div, qp2->div))
1434 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1436 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1437 if (!qp1->upoly)
1438 goto error;
1440 isl_qpolynomial_free(qp2);
1442 return qp1;
1443 error:
1444 isl_qpolynomial_free(qp1);
1445 isl_qpolynomial_free(qp2);
1446 return NULL;
1449 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1450 unsigned power)
1452 qp = isl_qpolynomial_cow(qp);
1454 if (!qp)
1455 return NULL;
1457 qp->upoly = isl_upoly_pow(qp->upoly, power);
1458 if (!qp->upoly)
1459 goto error;
1461 return qp;
1462 error:
1463 isl_qpolynomial_free(qp);
1464 return NULL;
1467 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1469 if (!dim)
1470 return NULL;
1471 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1474 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1476 if (!dim)
1477 return NULL;
1478 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1481 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1483 if (!dim)
1484 return NULL;
1485 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1488 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1490 if (!dim)
1491 return NULL;
1492 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1495 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1497 if (!dim)
1498 return NULL;
1499 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1502 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1503 isl_int v)
1505 struct isl_qpolynomial *qp;
1506 struct isl_upoly_cst *cst;
1508 if (!dim)
1509 return NULL;
1511 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1512 if (!qp)
1513 return NULL;
1515 cst = isl_upoly_as_cst(qp->upoly);
1516 isl_int_set(cst->n, v);
1518 return qp;
1521 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1522 isl_int *n, isl_int *d)
1524 struct isl_upoly_cst *cst;
1526 if (!qp)
1527 return -1;
1529 if (!isl_upoly_is_cst(qp->upoly))
1530 return 0;
1532 cst = isl_upoly_as_cst(qp->upoly);
1533 if (!cst)
1534 return -1;
1536 if (n)
1537 isl_int_set(*n, cst->n);
1538 if (d)
1539 isl_int_set(*d, cst->d);
1541 return 1;
1544 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1546 int is_cst;
1547 struct isl_upoly_rec *rec;
1549 if (!up)
1550 return -1;
1552 if (up->var < 0)
1553 return 1;
1555 rec = isl_upoly_as_rec(up);
1556 if (!rec)
1557 return -1;
1559 if (rec->n > 2)
1560 return 0;
1562 isl_assert(up->ctx, rec->n > 1, return -1);
1564 is_cst = isl_upoly_is_cst(rec->p[1]);
1565 if (is_cst < 0)
1566 return -1;
1567 if (!is_cst)
1568 return 0;
1570 return isl_upoly_is_affine(rec->p[0]);
1573 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1575 if (!qp)
1576 return -1;
1578 if (qp->div->n_row > 0)
1579 return 0;
1581 return isl_upoly_is_affine(qp->upoly);
1584 static void update_coeff(__isl_keep isl_vec *aff,
1585 __isl_keep struct isl_upoly_cst *cst, int pos)
1587 isl_int gcd;
1588 isl_int f;
1590 if (isl_int_is_zero(cst->n))
1591 return;
1593 isl_int_init(gcd);
1594 isl_int_init(f);
1595 isl_int_gcd(gcd, cst->d, aff->el[0]);
1596 isl_int_divexact(f, cst->d, gcd);
1597 isl_int_divexact(gcd, aff->el[0], gcd);
1598 isl_seq_scale(aff->el, aff->el, f, aff->size);
1599 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1600 isl_int_clear(gcd);
1601 isl_int_clear(f);
1604 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1605 __isl_keep isl_vec *aff)
1607 struct isl_upoly_cst *cst;
1608 struct isl_upoly_rec *rec;
1610 if (!up || !aff)
1611 return -1;
1613 if (up->var < 0) {
1614 struct isl_upoly_cst *cst;
1616 cst = isl_upoly_as_cst(up);
1617 if (!cst)
1618 return -1;
1619 update_coeff(aff, cst, 0);
1620 return 0;
1623 rec = isl_upoly_as_rec(up);
1624 if (!rec)
1625 return -1;
1626 isl_assert(up->ctx, rec->n == 2, return -1);
1628 cst = isl_upoly_as_cst(rec->p[1]);
1629 if (!cst)
1630 return -1;
1631 update_coeff(aff, cst, 1 + up->var);
1633 return isl_upoly_update_affine(rec->p[0], aff);
1636 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1637 __isl_keep isl_qpolynomial *qp)
1639 isl_vec *aff;
1640 unsigned d;
1642 if (!qp)
1643 return NULL;
1645 d = isl_dim_total(qp->dim);
1646 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1647 if (!aff)
1648 return NULL;
1650 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1651 isl_int_set_si(aff->el[0], 1);
1653 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1654 goto error;
1656 return aff;
1657 error:
1658 isl_vec_free(aff);
1659 return NULL;
1662 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1663 __isl_keep isl_qpolynomial *qp2)
1665 if (!qp1 || !qp2)
1666 return -1;
1668 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1671 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1673 int i;
1674 struct isl_upoly_rec *rec;
1676 if (isl_upoly_is_cst(up)) {
1677 struct isl_upoly_cst *cst;
1678 cst = isl_upoly_as_cst(up);
1679 if (!cst)
1680 return;
1681 isl_int_lcm(*d, *d, cst->d);
1682 return;
1685 rec = isl_upoly_as_rec(up);
1686 if (!rec)
1687 return;
1689 for (i = 0; i < rec->n; ++i)
1690 upoly_update_den(rec->p[i], d);
1693 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1695 isl_int_set_si(*d, 1);
1696 if (!qp)
1697 return;
1698 upoly_update_den(qp->upoly, d);
1701 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1702 int pos, int power)
1704 struct isl_ctx *ctx;
1706 if (!dim)
1707 return NULL;
1709 ctx = dim->ctx;
1711 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1714 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1715 enum isl_dim_type type, unsigned pos)
1717 if (!dim)
1718 return NULL;
1720 isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1721 isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1723 if (type == isl_dim_set)
1724 pos += isl_dim_size(dim, isl_dim_param);
1726 return isl_qpolynomial_var_pow(dim, pos, 1);
1727 error:
1728 isl_dim_free(dim);
1729 return NULL;
1732 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1733 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1735 int i;
1736 struct isl_upoly_rec *rec;
1737 struct isl_upoly *base, *res;
1739 if (!up)
1740 return NULL;
1742 if (isl_upoly_is_cst(up))
1743 return up;
1745 if (up->var < first)
1746 return up;
1748 rec = isl_upoly_as_rec(up);
1749 if (!rec)
1750 goto error;
1752 isl_assert(up->ctx, rec->n >= 1, goto error);
1754 if (up->var >= first + n)
1755 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1756 else
1757 base = isl_upoly_copy(subs[up->var - first]);
1759 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1760 for (i = rec->n - 2; i >= 0; --i) {
1761 struct isl_upoly *t;
1762 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1763 res = isl_upoly_mul(res, isl_upoly_copy(base));
1764 res = isl_upoly_sum(res, t);
1767 isl_upoly_free(base);
1768 isl_upoly_free(up);
1770 return res;
1771 error:
1772 isl_upoly_free(up);
1773 return NULL;
1776 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1777 isl_int denom, unsigned len)
1779 int i;
1780 struct isl_upoly *up;
1782 isl_assert(ctx, len >= 1, return NULL);
1784 up = isl_upoly_rat_cst(ctx, f[0], denom);
1785 for (i = 0; i < len - 1; ++i) {
1786 struct isl_upoly *t;
1787 struct isl_upoly *c;
1789 if (isl_int_is_zero(f[1 + i]))
1790 continue;
1792 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1793 t = isl_upoly_var_pow(ctx, i, 1);
1794 t = isl_upoly_mul(c, t);
1795 up = isl_upoly_sum(up, t);
1798 return up;
1801 /* Remove common factor of non-constant terms and denominator.
1803 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1805 isl_ctx *ctx = qp->div->ctx;
1806 unsigned total = qp->div->n_col - 2;
1808 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1809 isl_int_gcd(ctx->normalize_gcd,
1810 ctx->normalize_gcd, qp->div->row[div][0]);
1811 if (isl_int_is_one(ctx->normalize_gcd))
1812 return;
1814 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1815 ctx->normalize_gcd, total);
1816 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1817 ctx->normalize_gcd);
1818 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1819 ctx->normalize_gcd);
1822 /* Replace the integer division identified by "div" by the polynomial "s".
1823 * The integer division is assumed not to appear in the definition
1824 * of any other integer divisions.
1826 static __isl_give isl_qpolynomial *substitute_div(
1827 __isl_take isl_qpolynomial *qp,
1828 int div, __isl_take struct isl_upoly *s)
1830 int i;
1831 int total;
1832 int *reordering;
1834 if (!qp || !s)
1835 goto error;
1837 qp = isl_qpolynomial_cow(qp);
1838 if (!qp)
1839 goto error;
1841 total = isl_dim_total(qp->dim);
1842 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1843 if (!qp->upoly)
1844 goto error;
1846 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1847 if (!reordering)
1848 goto error;
1849 for (i = 0; i < total + div; ++i)
1850 reordering[i] = i;
1851 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1852 reordering[i] = i - 1;
1853 qp->div = isl_mat_drop_rows(qp->div, div, 1);
1854 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1855 qp->upoly = reorder(qp->upoly, reordering);
1856 free(reordering);
1858 if (!qp->upoly || !qp->div)
1859 goto error;
1861 isl_upoly_free(s);
1862 return qp;
1863 error:
1864 isl_qpolynomial_free(qp);
1865 isl_upoly_free(s);
1866 return NULL;
1869 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1870 * divisions because d is equal to 1 by their definition, i.e., e.
1872 static __isl_give isl_qpolynomial *substitute_non_divs(
1873 __isl_take isl_qpolynomial *qp)
1875 int i, j;
1876 int total;
1877 struct isl_upoly *s;
1879 if (!qp)
1880 return NULL;
1882 total = isl_dim_total(qp->dim);
1883 for (i = 0; qp && i < qp->div->n_row; ++i) {
1884 if (!isl_int_is_one(qp->div->row[i][0]))
1885 continue;
1886 for (j = i + 1; j < qp->div->n_row; ++j) {
1887 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1888 continue;
1889 isl_seq_combine(qp->div->row[j] + 1,
1890 qp->div->ctx->one, qp->div->row[j] + 1,
1891 qp->div->row[j][2 + total + i],
1892 qp->div->row[i] + 1, 1 + total + i);
1893 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1894 normalize_div(qp, j);
1896 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1897 qp->div->row[i][0], qp->div->n_col - 1);
1898 qp = substitute_div(qp, i, s);
1899 --i;
1902 return qp;
1905 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1906 * with d the denominator. When replacing the coefficient e of x by
1907 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1908 * inside the division, so we need to add floor(e/d) * x outside.
1909 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1910 * to adjust the coefficient of x in each later div that depends on the
1911 * current div "div" and also in the affine expression "aff"
1912 * (if it too depends on "div").
1914 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1915 __isl_keep isl_vec *aff)
1917 int i, j;
1918 isl_int v;
1919 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1921 isl_int_init(v);
1922 for (i = 0; i < 1 + total + div; ++i) {
1923 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1924 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1925 continue;
1926 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1927 isl_int_fdiv_r(qp->div->row[div][1 + i],
1928 qp->div->row[div][1 + i], qp->div->row[div][0]);
1929 if (!isl_int_is_zero(aff->el[1 + total + div]))
1930 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1931 for (j = div + 1; j < qp->div->n_row; ++j) {
1932 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1933 continue;
1934 isl_int_addmul(qp->div->row[j][1 + i],
1935 v, qp->div->row[j][2 + total + div]);
1938 isl_int_clear(v);
1941 /* Check if the last non-zero coefficient is bigger that half of the
1942 * denominator. If so, we will invert the div to further reduce the number
1943 * of distinct divs that may appear.
1944 * If the last non-zero coefficient is exactly half the denominator,
1945 * then we continue looking for earlier coefficients that are bigger
1946 * than half the denominator.
1948 static int needs_invert(__isl_keep isl_mat *div, int row)
1950 int i;
1951 int cmp;
1953 for (i = div->n_col - 1; i >= 1; --i) {
1954 if (isl_int_is_zero(div->row[row][i]))
1955 continue;
1956 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1957 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
1958 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
1959 if (cmp)
1960 return cmp > 0;
1961 if (i == 1)
1962 return 1;
1965 return 0;
1968 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
1969 * We only invert the coefficients of e (and the coefficient of q in
1970 * later divs and in "aff"). After calling this function, the
1971 * coefficients of e should be reduced again.
1973 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
1974 __isl_keep isl_vec *aff)
1976 unsigned total = qp->div->n_col - qp->div->n_row - 2;
1978 isl_seq_neg(qp->div->row[div] + 1,
1979 qp->div->row[div] + 1, qp->div->n_col - 1);
1980 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
1981 isl_int_add(qp->div->row[div][1],
1982 qp->div->row[div][1], qp->div->row[div][0]);
1983 if (!isl_int_is_zero(aff->el[1 + total + div]))
1984 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
1985 isl_mat_col_mul(qp->div, 2 + total + div,
1986 qp->div->ctx->negone, 2 + total + div);
1989 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
1990 * in the interval [0, d-1], with d the denominator and such that the
1991 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
1993 * After the reduction, some divs may have become redundant or identical,
1994 * so we call substitute_non_divs and sort_divs. If these functions
1995 * eliminate divs or merge two or more divs into one, the coefficients
1996 * of the enclosing divs may have to be reduced again, so we call
1997 * ourselves recursively if the number of divs decreases.
1999 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2001 int i;
2002 isl_vec *aff = NULL;
2003 struct isl_upoly *s;
2004 unsigned n_div;
2006 if (!qp)
2007 return NULL;
2009 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2010 aff = isl_vec_clr(aff);
2011 if (!aff)
2012 goto error;
2014 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2016 for (i = 0; i < qp->div->n_row; ++i) {
2017 normalize_div(qp, i);
2018 reduce_div(qp, i, aff);
2019 if (needs_invert(qp->div, i)) {
2020 invert_div(qp, i, aff);
2021 reduce_div(qp, i, aff);
2025 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2026 qp->div->ctx->one, aff->size);
2027 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2028 isl_upoly_free(s);
2029 if (!qp->upoly)
2030 goto error;
2032 isl_vec_free(aff);
2034 n_div = qp->div->n_row;
2035 qp = substitute_non_divs(qp);
2036 qp = sort_divs(qp);
2037 if (qp && qp->div->n_row < n_div)
2038 return reduce_divs(qp);
2040 return qp;
2041 error:
2042 isl_qpolynomial_free(qp);
2043 isl_vec_free(aff);
2044 return NULL;
2047 /* Assumes each div only depends on earlier divs.
2049 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2050 int power)
2052 struct isl_qpolynomial *qp = NULL;
2053 struct isl_upoly_rec *rec;
2054 struct isl_upoly_cst *cst;
2055 int i, d;
2056 int pos;
2058 if (!div)
2059 return NULL;
2061 d = div->line - div->bmap->div;
2063 pos = isl_dim_total(div->bmap->dim) + d;
2064 rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2065 qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2066 div->bmap->n_div, &rec->up);
2067 if (!qp)
2068 goto error;
2070 for (i = 0; i < div->bmap->n_div; ++i)
2071 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2073 for (i = 0; i < 1 + power; ++i) {
2074 rec->p[i] = isl_upoly_zero(div->ctx);
2075 if (!rec->p[i])
2076 goto error;
2077 rec->n++;
2079 cst = isl_upoly_as_cst(rec->p[power]);
2080 isl_int_set_si(cst->n, 1);
2082 isl_div_free(div);
2084 qp = reduce_divs(qp);
2086 return qp;
2087 error:
2088 isl_qpolynomial_free(qp);
2089 isl_div_free(div);
2090 return NULL;
2093 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2095 return isl_qpolynomial_div_pow(div, 1);
2098 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2099 const isl_int n, const isl_int d)
2101 struct isl_qpolynomial *qp;
2102 struct isl_upoly_cst *cst;
2104 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2105 if (!qp)
2106 return NULL;
2108 cst = isl_upoly_as_cst(qp->upoly);
2109 isl_int_set(cst->n, n);
2110 isl_int_set(cst->d, d);
2112 return qp;
2115 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2117 struct isl_upoly_rec *rec;
2118 int i;
2120 if (!up)
2121 return -1;
2123 if (isl_upoly_is_cst(up))
2124 return 0;
2126 if (up->var < d)
2127 active[up->var] = 1;
2129 rec = isl_upoly_as_rec(up);
2130 for (i = 0; i < rec->n; ++i)
2131 if (up_set_active(rec->p[i], active, d) < 0)
2132 return -1;
2134 return 0;
2137 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2139 int i, j;
2140 int d = isl_dim_total(qp->dim);
2142 if (!qp || !active)
2143 return -1;
2145 for (i = 0; i < d; ++i)
2146 for (j = 0; j < qp->div->n_row; ++j) {
2147 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2148 continue;
2149 active[i] = 1;
2150 break;
2153 return up_set_active(qp->upoly, active, d);
2156 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2157 enum isl_dim_type type, unsigned first, unsigned n)
2159 int i;
2160 int *active = NULL;
2161 int involves = 0;
2163 if (!qp)
2164 return -1;
2165 if (n == 0)
2166 return 0;
2168 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2169 return -1);
2170 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2171 type == isl_dim_set, return -1);
2173 active = isl_calloc_array(qp->dim->ctx, int, isl_dim_total(qp->dim));
2174 if (set_active(qp, active) < 0)
2175 goto error;
2177 if (type == isl_dim_set)
2178 first += isl_dim_size(qp->dim, isl_dim_param);
2179 for (i = 0; i < n; ++i)
2180 if (active[first + i]) {
2181 involves = 1;
2182 break;
2185 free(active);
2187 return involves;
2188 error:
2189 free(active);
2190 return -1;
2193 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2194 * of the divs that do appear in the quasi-polynomial.
2196 static __isl_give isl_qpolynomial *remove_redundant_divs(
2197 __isl_take isl_qpolynomial *qp)
2199 int i, j;
2200 int d;
2201 int len;
2202 int skip;
2203 int *active = NULL;
2204 int *reordering = NULL;
2205 int redundant = 0;
2206 int n_div;
2207 isl_ctx *ctx;
2209 if (!qp)
2210 return NULL;
2211 if (qp->div->n_row == 0)
2212 return qp;
2214 d = isl_dim_total(qp->dim);
2215 len = qp->div->n_col - 2;
2216 ctx = isl_qpolynomial_get_ctx(qp);
2217 active = isl_calloc_array(ctx, int, len);
2218 if (!active)
2219 goto error;
2221 if (up_set_active(qp->upoly, active, len) < 0)
2222 goto error;
2224 for (i = qp->div->n_row - 1; i >= 0; --i) {
2225 if (!active[d + i]) {
2226 redundant = 1;
2227 continue;
2229 for (j = 0; j < i; ++j) {
2230 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2231 continue;
2232 active[d + j] = 1;
2233 break;
2237 if (!redundant) {
2238 free(active);
2239 return qp;
2242 reordering = isl_alloc_array(qp->div->ctx, int, len);
2243 if (!reordering)
2244 goto error;
2246 for (i = 0; i < d; ++i)
2247 reordering[i] = i;
2249 skip = 0;
2250 n_div = qp->div->n_row;
2251 for (i = 0; i < n_div; ++i) {
2252 if (!active[d + i]) {
2253 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2254 qp->div = isl_mat_drop_cols(qp->div,
2255 2 + d + i - skip, 1);
2256 skip++;
2258 reordering[d + i] = d + i - skip;
2261 qp->upoly = reorder(qp->upoly, reordering);
2263 if (!qp->upoly || !qp->div)
2264 goto error;
2266 free(active);
2267 free(reordering);
2269 return qp;
2270 error:
2271 free(active);
2272 free(reordering);
2273 isl_qpolynomial_free(qp);
2274 return NULL;
2277 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2278 unsigned first, unsigned n)
2280 int i;
2281 struct isl_upoly_rec *rec;
2283 if (!up)
2284 return NULL;
2285 if (n == 0 || up->var < 0 || up->var < first)
2286 return up;
2287 if (up->var < first + n) {
2288 up = replace_by_constant_term(up);
2289 return isl_upoly_drop(up, first, n);
2291 up = isl_upoly_cow(up);
2292 if (!up)
2293 return NULL;
2294 up->var -= n;
2295 rec = isl_upoly_as_rec(up);
2296 if (!rec)
2297 goto error;
2299 for (i = 0; i < rec->n; ++i) {
2300 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2301 if (!rec->p[i])
2302 goto error;
2305 return up;
2306 error:
2307 isl_upoly_free(up);
2308 return NULL;
2311 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2312 __isl_take isl_qpolynomial *qp,
2313 enum isl_dim_type type, unsigned pos, const char *s)
2315 qp = isl_qpolynomial_cow(qp);
2316 if (!qp)
2317 return NULL;
2318 qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2319 if (!qp->dim)
2320 goto error;
2321 return qp;
2322 error:
2323 isl_qpolynomial_free(qp);
2324 return NULL;
2327 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2328 __isl_take isl_qpolynomial *qp,
2329 enum isl_dim_type type, unsigned first, unsigned n)
2331 if (!qp)
2332 return NULL;
2333 if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
2334 return qp;
2336 qp = isl_qpolynomial_cow(qp);
2337 if (!qp)
2338 return NULL;
2340 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2341 goto error);
2342 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2343 type == isl_dim_set, goto error);
2345 qp->dim = isl_dim_drop(qp->dim, type, first, n);
2346 if (!qp->dim)
2347 goto error;
2349 if (type == isl_dim_set)
2350 first += isl_dim_size(qp->dim, isl_dim_param);
2352 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2353 if (!qp->div)
2354 goto error;
2356 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2357 if (!qp->upoly)
2358 goto error;
2360 return qp;
2361 error:
2362 isl_qpolynomial_free(qp);
2363 return NULL;
2366 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2367 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2369 int i, j, k;
2370 isl_int denom;
2371 unsigned total;
2372 unsigned n_div;
2373 struct isl_upoly *up;
2375 if (!eq)
2376 goto error;
2377 if (eq->n_eq == 0) {
2378 isl_basic_set_free(eq);
2379 return qp;
2382 qp = isl_qpolynomial_cow(qp);
2383 if (!qp)
2384 goto error;
2385 qp->div = isl_mat_cow(qp->div);
2386 if (!qp->div)
2387 goto error;
2389 total = 1 + isl_dim_total(eq->dim);
2390 n_div = eq->n_div;
2391 isl_int_init(denom);
2392 for (i = 0; i < eq->n_eq; ++i) {
2393 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2394 if (j < 0 || j == 0 || j >= total)
2395 continue;
2397 for (k = 0; k < qp->div->n_row; ++k) {
2398 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2399 continue;
2400 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2401 &qp->div->row[k][0]);
2402 normalize_div(qp, k);
2405 if (isl_int_is_pos(eq->eq[i][j]))
2406 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2407 isl_int_abs(denom, eq->eq[i][j]);
2408 isl_int_set_si(eq->eq[i][j], 0);
2410 up = isl_upoly_from_affine(qp->dim->ctx,
2411 eq->eq[i], denom, total);
2412 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2413 isl_upoly_free(up);
2415 isl_int_clear(denom);
2417 if (!qp->upoly)
2418 goto error;
2420 isl_basic_set_free(eq);
2422 qp = substitute_non_divs(qp);
2423 qp = sort_divs(qp);
2425 return qp;
2426 error:
2427 isl_basic_set_free(eq);
2428 isl_qpolynomial_free(qp);
2429 return NULL;
2432 static __isl_give isl_basic_set *add_div_constraints(
2433 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2435 int i;
2436 unsigned total;
2438 if (!bset || !div)
2439 goto error;
2441 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2442 if (!bset)
2443 goto error;
2444 total = isl_basic_set_total_dim(bset);
2445 for (i = 0; i < div->n_row; ++i)
2446 if (isl_basic_set_add_div_constraints_var(bset,
2447 total - div->n_row + i, div->row[i]) < 0)
2448 goto error;
2450 isl_mat_free(div);
2451 return bset;
2452 error:
2453 isl_mat_free(div);
2454 isl_basic_set_free(bset);
2455 return NULL;
2458 /* Look for equalities among the variables shared by context and qp
2459 * and the integer divisions of qp, if any.
2460 * The equalities are then used to eliminate variables and/or integer
2461 * divisions from qp.
2463 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2464 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2466 isl_basic_set *aff;
2468 if (!qp)
2469 goto error;
2470 if (qp->div->n_row > 0) {
2471 isl_basic_set *bset;
2472 context = isl_set_add_dims(context, isl_dim_set,
2473 qp->div->n_row);
2474 bset = isl_basic_set_universe(isl_set_get_dim(context));
2475 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2476 context = isl_set_intersect(context,
2477 isl_set_from_basic_set(bset));
2480 aff = isl_set_affine_hull(context);
2481 return isl_qpolynomial_substitute_equalities(qp, aff);
2482 error:
2483 isl_qpolynomial_free(qp);
2484 isl_set_free(context);
2485 return NULL;
2488 #undef PW
2489 #define PW isl_pw_qpolynomial
2490 #undef EL
2491 #define EL isl_qpolynomial
2492 #undef IS_ZERO
2493 #define IS_ZERO is_zero
2494 #undef FIELD
2495 #define FIELD qp
2497 #include <isl_pw_templ.c>
2499 #undef UNION
2500 #define UNION isl_union_pw_qpolynomial
2501 #undef PART
2502 #define PART isl_pw_qpolynomial
2503 #undef PARTS
2504 #define PARTS pw_qpolynomial
2506 #include <isl_union_templ.c>
2508 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2510 if (!pwqp)
2511 return -1;
2513 if (pwqp->n != -1)
2514 return 0;
2516 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2517 return 0;
2519 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2522 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2523 __isl_take isl_pw_qpolynomial *pwqp1,
2524 __isl_take isl_pw_qpolynomial *pwqp2)
2526 int i, j, n;
2527 struct isl_pw_qpolynomial *res;
2529 if (!pwqp1 || !pwqp2)
2530 goto error;
2532 isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2533 goto error);
2535 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2536 isl_pw_qpolynomial_free(pwqp2);
2537 return pwqp1;
2540 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2541 isl_pw_qpolynomial_free(pwqp1);
2542 return pwqp2;
2545 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2546 isl_pw_qpolynomial_free(pwqp1);
2547 return pwqp2;
2550 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2551 isl_pw_qpolynomial_free(pwqp2);
2552 return pwqp1;
2555 n = pwqp1->n * pwqp2->n;
2556 res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2558 for (i = 0; i < pwqp1->n; ++i) {
2559 for (j = 0; j < pwqp2->n; ++j) {
2560 struct isl_set *common;
2561 struct isl_qpolynomial *prod;
2562 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2563 isl_set_copy(pwqp2->p[j].set));
2564 if (isl_set_plain_is_empty(common)) {
2565 isl_set_free(common);
2566 continue;
2569 prod = isl_qpolynomial_mul(
2570 isl_qpolynomial_copy(pwqp1->p[i].qp),
2571 isl_qpolynomial_copy(pwqp2->p[j].qp));
2573 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2577 isl_pw_qpolynomial_free(pwqp1);
2578 isl_pw_qpolynomial_free(pwqp2);
2580 return res;
2581 error:
2582 isl_pw_qpolynomial_free(pwqp1);
2583 isl_pw_qpolynomial_free(pwqp2);
2584 return NULL;
2587 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2588 __isl_take isl_pw_qpolynomial *pwqp)
2590 int i;
2592 if (!pwqp)
2593 return NULL;
2595 if (isl_pw_qpolynomial_is_zero(pwqp))
2596 return pwqp;
2598 pwqp = isl_pw_qpolynomial_cow(pwqp);
2599 if (!pwqp)
2600 return NULL;
2602 for (i = 0; i < pwqp->n; ++i) {
2603 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2604 if (!pwqp->p[i].qp)
2605 goto error;
2608 return pwqp;
2609 error:
2610 isl_pw_qpolynomial_free(pwqp);
2611 return NULL;
2614 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2615 __isl_take isl_pw_qpolynomial *pwqp1,
2616 __isl_take isl_pw_qpolynomial *pwqp2)
2618 return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2621 __isl_give struct isl_upoly *isl_upoly_eval(
2622 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2624 int i;
2625 struct isl_upoly_rec *rec;
2626 struct isl_upoly *res;
2627 struct isl_upoly *base;
2629 if (isl_upoly_is_cst(up)) {
2630 isl_vec_free(vec);
2631 return up;
2634 rec = isl_upoly_as_rec(up);
2635 if (!rec)
2636 goto error;
2638 isl_assert(up->ctx, rec->n >= 1, goto error);
2640 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2642 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2643 isl_vec_copy(vec));
2645 for (i = rec->n - 2; i >= 0; --i) {
2646 res = isl_upoly_mul(res, isl_upoly_copy(base));
2647 res = isl_upoly_sum(res,
2648 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2649 isl_vec_copy(vec)));
2652 isl_upoly_free(base);
2653 isl_upoly_free(up);
2654 isl_vec_free(vec);
2655 return res;
2656 error:
2657 isl_upoly_free(up);
2658 isl_vec_free(vec);
2659 return NULL;
2662 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2663 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2665 isl_vec *ext;
2666 struct isl_upoly *up;
2667 isl_dim *dim;
2669 if (!qp || !pnt)
2670 goto error;
2671 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2673 if (qp->div->n_row == 0)
2674 ext = isl_vec_copy(pnt->vec);
2675 else {
2676 int i;
2677 unsigned dim = isl_dim_total(qp->dim);
2678 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2679 if (!ext)
2680 goto error;
2682 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2683 for (i = 0; i < qp->div->n_row; ++i) {
2684 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2685 1 + dim + i, &ext->el[1+dim+i]);
2686 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2687 qp->div->row[i][0]);
2691 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2692 if (!up)
2693 goto error;
2695 dim = isl_dim_copy(qp->dim);
2696 isl_qpolynomial_free(qp);
2697 isl_point_free(pnt);
2699 return isl_qpolynomial_alloc(dim, 0, up);
2700 error:
2701 isl_qpolynomial_free(qp);
2702 isl_point_free(pnt);
2703 return NULL;
2706 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2707 __isl_keep struct isl_upoly_cst *cst2)
2709 int cmp;
2710 isl_int t;
2711 isl_int_init(t);
2712 isl_int_mul(t, cst1->n, cst2->d);
2713 isl_int_submul(t, cst2->n, cst1->d);
2714 cmp = isl_int_sgn(t);
2715 isl_int_clear(t);
2716 return cmp;
2719 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2720 __isl_keep isl_qpolynomial *qp2)
2722 struct isl_upoly_cst *cst1, *cst2;
2724 if (!qp1 || !qp2)
2725 return -1;
2726 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2727 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2728 if (isl_qpolynomial_is_nan(qp1))
2729 return -1;
2730 if (isl_qpolynomial_is_nan(qp2))
2731 return -1;
2732 cst1 = isl_upoly_as_cst(qp1->upoly);
2733 cst2 = isl_upoly_as_cst(qp2->upoly);
2735 return isl_upoly_cmp(cst1, cst2) <= 0;
2738 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2739 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2741 struct isl_upoly_cst *cst1, *cst2;
2742 int cmp;
2744 if (!qp1 || !qp2)
2745 goto error;
2746 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2747 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2748 cst1 = isl_upoly_as_cst(qp1->upoly);
2749 cst2 = isl_upoly_as_cst(qp2->upoly);
2750 cmp = isl_upoly_cmp(cst1, cst2);
2752 if (cmp <= 0) {
2753 isl_qpolynomial_free(qp2);
2754 } else {
2755 isl_qpolynomial_free(qp1);
2756 qp1 = qp2;
2758 return qp1;
2759 error:
2760 isl_qpolynomial_free(qp1);
2761 isl_qpolynomial_free(qp2);
2762 return NULL;
2765 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2766 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2768 struct isl_upoly_cst *cst1, *cst2;
2769 int cmp;
2771 if (!qp1 || !qp2)
2772 goto error;
2773 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2774 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2775 cst1 = isl_upoly_as_cst(qp1->upoly);
2776 cst2 = isl_upoly_as_cst(qp2->upoly);
2777 cmp = isl_upoly_cmp(cst1, cst2);
2779 if (cmp >= 0) {
2780 isl_qpolynomial_free(qp2);
2781 } else {
2782 isl_qpolynomial_free(qp1);
2783 qp1 = qp2;
2785 return qp1;
2786 error:
2787 isl_qpolynomial_free(qp1);
2788 isl_qpolynomial_free(qp2);
2789 return NULL;
2792 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2793 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2794 unsigned first, unsigned n)
2796 unsigned total;
2797 unsigned g_pos;
2798 int *exp;
2800 if (n == 0)
2801 return qp;
2803 qp = isl_qpolynomial_cow(qp);
2804 if (!qp)
2805 return NULL;
2807 isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2808 goto error);
2810 g_pos = pos(qp->dim, type) + first;
2812 qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2813 if (!qp->div)
2814 goto error;
2816 total = qp->div->n_col - 2;
2817 if (total > g_pos) {
2818 int i;
2819 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2820 if (!exp)
2821 goto error;
2822 for (i = 0; i < total - g_pos; ++i)
2823 exp[i] = i + n;
2824 qp->upoly = expand(qp->upoly, exp, g_pos);
2825 free(exp);
2826 if (!qp->upoly)
2827 goto error;
2830 qp->dim = isl_dim_insert(qp->dim, type, first, n);
2831 if (!qp->dim)
2832 goto error;
2834 return qp;
2835 error:
2836 isl_qpolynomial_free(qp);
2837 return NULL;
2840 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2841 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2843 unsigned pos;
2845 pos = isl_qpolynomial_dim(qp, type);
2847 return isl_qpolynomial_insert_dims(qp, type, pos, n);
2850 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2851 __isl_take isl_pw_qpolynomial *pwqp,
2852 enum isl_dim_type type, unsigned n)
2854 unsigned pos;
2856 pos = isl_pw_qpolynomial_dim(pwqp, type);
2858 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2861 static int *reordering_move(isl_ctx *ctx,
2862 unsigned len, unsigned dst, unsigned src, unsigned n)
2864 int i;
2865 int *reordering;
2867 reordering = isl_alloc_array(ctx, int, len);
2868 if (!reordering)
2869 return NULL;
2871 if (dst <= src) {
2872 for (i = 0; i < dst; ++i)
2873 reordering[i] = i;
2874 for (i = 0; i < n; ++i)
2875 reordering[src + i] = dst + i;
2876 for (i = 0; i < src - dst; ++i)
2877 reordering[dst + i] = dst + n + i;
2878 for (i = 0; i < len - src - n; ++i)
2879 reordering[src + n + i] = src + n + i;
2880 } else {
2881 for (i = 0; i < src; ++i)
2882 reordering[i] = i;
2883 for (i = 0; i < n; ++i)
2884 reordering[src + i] = dst + i;
2885 for (i = 0; i < dst - src; ++i)
2886 reordering[src + n + i] = src + i;
2887 for (i = 0; i < len - dst - n; ++i)
2888 reordering[dst + n + i] = dst + n + i;
2891 return reordering;
2894 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2895 __isl_take isl_qpolynomial *qp,
2896 enum isl_dim_type dst_type, unsigned dst_pos,
2897 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2899 unsigned g_dst_pos;
2900 unsigned g_src_pos;
2901 int *reordering;
2903 qp = isl_qpolynomial_cow(qp);
2904 if (!qp)
2905 return NULL;
2907 isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2908 goto error);
2910 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2911 g_src_pos = pos(qp->dim, src_type) + src_pos;
2912 if (dst_type > src_type)
2913 g_dst_pos -= n;
2915 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2916 if (!qp->div)
2917 goto error;
2918 qp = sort_divs(qp);
2919 if (!qp)
2920 goto error;
2922 reordering = reordering_move(qp->dim->ctx,
2923 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2924 if (!reordering)
2925 goto error;
2927 qp->upoly = reorder(qp->upoly, reordering);
2928 free(reordering);
2929 if (!qp->upoly)
2930 goto error;
2932 qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2933 if (!qp->dim)
2934 goto error;
2936 return qp;
2937 error:
2938 isl_qpolynomial_free(qp);
2939 return NULL;
2942 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2943 isl_int *f, isl_int denom)
2945 struct isl_upoly *up;
2947 if (!dim)
2948 return NULL;
2950 up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2952 return isl_qpolynomial_alloc(dim, 0, up);
2955 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
2957 isl_ctx *ctx;
2958 struct isl_upoly *up;
2959 isl_qpolynomial *qp;
2961 if (!aff)
2962 return NULL;
2964 ctx = isl_aff_get_ctx(aff);
2965 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
2966 aff->v->size - 1);
2968 qp = isl_qpolynomial_alloc(isl_aff_get_dim(aff),
2969 aff->ls->div->n_row, up);
2970 if (!qp)
2971 goto error;
2973 isl_mat_free(qp->div);
2974 qp->div = isl_mat_copy(aff->ls->div);
2975 qp->div = isl_mat_cow(qp->div);
2976 if (!qp->div)
2977 goto error;
2979 isl_aff_free(aff);
2980 qp = reduce_divs(qp);
2981 qp = remove_redundant_divs(qp);
2982 return qp;
2983 error:
2984 isl_aff_free(aff);
2985 return NULL;
2988 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2989 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2991 isl_int denom;
2992 isl_dim *dim;
2993 struct isl_upoly *up;
2994 isl_qpolynomial *qp;
2995 int sgn;
2997 if (!c)
2998 return NULL;
3000 isl_int_init(denom);
3002 isl_constraint_get_coefficient(c, type, pos, &denom);
3003 isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
3004 sgn = isl_int_sgn(denom);
3005 isl_int_abs(denom, denom);
3006 up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
3007 1 + isl_constraint_dim(c, isl_dim_all));
3008 if (sgn < 0)
3009 isl_int_neg(denom, denom);
3010 isl_constraint_set_coefficient(c, type, pos, denom);
3012 dim = isl_dim_copy(c->bmap->dim);
3014 isl_int_clear(denom);
3015 isl_constraint_free(c);
3017 qp = isl_qpolynomial_alloc(dim, 0, up);
3018 if (sgn > 0)
3019 qp = isl_qpolynomial_neg(qp);
3020 return qp;
3023 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3024 * in "qp" by subs[i].
3026 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3027 __isl_take isl_qpolynomial *qp,
3028 enum isl_dim_type type, unsigned first, unsigned n,
3029 __isl_keep isl_qpolynomial **subs)
3031 int i;
3032 struct isl_upoly **ups;
3034 if (n == 0)
3035 return qp;
3037 qp = isl_qpolynomial_cow(qp);
3038 if (!qp)
3039 return NULL;
3040 for (i = 0; i < n; ++i)
3041 if (!subs[i])
3042 goto error;
3044 isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
3045 goto error);
3047 for (i = 0; i < n; ++i)
3048 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
3049 goto error);
3051 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3052 for (i = 0; i < n; ++i)
3053 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3055 first += pos(qp->dim, type);
3057 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3058 if (!ups)
3059 goto error;
3060 for (i = 0; i < n; ++i)
3061 ups[i] = subs[i]->upoly;
3063 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3065 free(ups);
3067 if (!qp->upoly)
3068 goto error;
3070 return qp;
3071 error:
3072 isl_qpolynomial_free(qp);
3073 return NULL;
3076 /* Extend "bset" with extra set dimensions for each integer division
3077 * in "qp" and then call "fn" with the extended bset and the polynomial
3078 * that results from replacing each of the integer divisions by the
3079 * corresponding extra set dimension.
3081 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3082 __isl_keep isl_basic_set *bset,
3083 int (*fn)(__isl_take isl_basic_set *bset,
3084 __isl_take isl_qpolynomial *poly, void *user), void *user)
3086 isl_dim *dim;
3087 isl_mat *div;
3088 isl_qpolynomial *poly;
3090 if (!qp || !bset)
3091 goto error;
3092 if (qp->div->n_row == 0)
3093 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3094 user);
3096 div = isl_mat_copy(qp->div);
3097 dim = isl_dim_copy(qp->dim);
3098 dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3099 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3100 bset = isl_basic_set_copy(bset);
3101 bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3102 bset = add_div_constraints(bset, div);
3104 return fn(bset, poly, user);
3105 error:
3106 return -1;
3109 /* Return total degree in variables first (inclusive) up to last (exclusive).
3111 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3113 int deg = -1;
3114 int i;
3115 struct isl_upoly_rec *rec;
3117 if (!up)
3118 return -2;
3119 if (isl_upoly_is_zero(up))
3120 return -1;
3121 if (isl_upoly_is_cst(up) || up->var < first)
3122 return 0;
3124 rec = isl_upoly_as_rec(up);
3125 if (!rec)
3126 return -2;
3128 for (i = 0; i < rec->n; ++i) {
3129 int d;
3131 if (isl_upoly_is_zero(rec->p[i]))
3132 continue;
3133 d = isl_upoly_degree(rec->p[i], first, last);
3134 if (up->var < last)
3135 d += i;
3136 if (d > deg)
3137 deg = d;
3140 return deg;
3143 /* Return total degree in set variables.
3145 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3147 unsigned ovar;
3148 unsigned nvar;
3150 if (!poly)
3151 return -2;
3153 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3154 nvar = isl_dim_size(poly->dim, isl_dim_set);
3155 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3158 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3159 unsigned pos, int deg)
3161 int i;
3162 struct isl_upoly_rec *rec;
3164 if (!up)
3165 return NULL;
3167 if (isl_upoly_is_cst(up) || up->var < pos) {
3168 if (deg == 0)
3169 return isl_upoly_copy(up);
3170 else
3171 return isl_upoly_zero(up->ctx);
3174 rec = isl_upoly_as_rec(up);
3175 if (!rec)
3176 return NULL;
3178 if (up->var == pos) {
3179 if (deg < rec->n)
3180 return isl_upoly_copy(rec->p[deg]);
3181 else
3182 return isl_upoly_zero(up->ctx);
3185 up = isl_upoly_copy(up);
3186 up = isl_upoly_cow(up);
3187 rec = isl_upoly_as_rec(up);
3188 if (!rec)
3189 goto error;
3191 for (i = 0; i < rec->n; ++i) {
3192 struct isl_upoly *t;
3193 t = isl_upoly_coeff(rec->p[i], pos, deg);
3194 if (!t)
3195 goto error;
3196 isl_upoly_free(rec->p[i]);
3197 rec->p[i] = t;
3200 return up;
3201 error:
3202 isl_upoly_free(up);
3203 return NULL;
3206 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3208 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3209 __isl_keep isl_qpolynomial *qp,
3210 enum isl_dim_type type, unsigned t_pos, int deg)
3212 unsigned g_pos;
3213 struct isl_upoly *up;
3214 isl_qpolynomial *c;
3216 if (!qp)
3217 return NULL;
3219 isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3220 return NULL);
3222 g_pos = pos(qp->dim, type) + t_pos;
3223 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3225 c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3226 if (!c)
3227 return NULL;
3228 isl_mat_free(c->div);
3229 c->div = isl_mat_copy(qp->div);
3230 if (!c->div)
3231 goto error;
3232 return c;
3233 error:
3234 isl_qpolynomial_free(c);
3235 return NULL;
3238 /* Homogenize the polynomial in the variables first (inclusive) up to
3239 * last (exclusive) by inserting powers of variable first.
3240 * Variable first is assumed not to appear in the input.
3242 __isl_give struct isl_upoly *isl_upoly_homogenize(
3243 __isl_take struct isl_upoly *up, int deg, int target,
3244 int first, int last)
3246 int i;
3247 struct isl_upoly_rec *rec;
3249 if (!up)
3250 return NULL;
3251 if (isl_upoly_is_zero(up))
3252 return up;
3253 if (deg == target)
3254 return up;
3255 if (isl_upoly_is_cst(up) || up->var < first) {
3256 struct isl_upoly *hom;
3258 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3259 if (!hom)
3260 goto error;
3261 rec = isl_upoly_as_rec(hom);
3262 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3264 return hom;
3267 up = isl_upoly_cow(up);
3268 rec = isl_upoly_as_rec(up);
3269 if (!rec)
3270 goto error;
3272 for (i = 0; i < rec->n; ++i) {
3273 if (isl_upoly_is_zero(rec->p[i]))
3274 continue;
3275 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3276 up->var < last ? deg + i : i, target,
3277 first, last);
3278 if (!rec->p[i])
3279 goto error;
3282 return up;
3283 error:
3284 isl_upoly_free(up);
3285 return NULL;
3288 /* Homogenize the polynomial in the set variables by introducing
3289 * powers of an extra set variable at position 0.
3291 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3292 __isl_take isl_qpolynomial *poly)
3294 unsigned ovar;
3295 unsigned nvar;
3296 int deg = isl_qpolynomial_degree(poly);
3298 if (deg < -1)
3299 goto error;
3301 poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3302 poly = isl_qpolynomial_cow(poly);
3303 if (!poly)
3304 goto error;
3306 ovar = isl_dim_offset(poly->dim, isl_dim_set);
3307 nvar = isl_dim_size(poly->dim, isl_dim_set);
3308 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3309 ovar, ovar + nvar);
3310 if (!poly->upoly)
3311 goto error;
3313 return poly;
3314 error:
3315 isl_qpolynomial_free(poly);
3316 return NULL;
3319 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3320 __isl_take isl_mat *div)
3322 isl_term *term;
3323 int n;
3325 if (!dim || !div)
3326 goto error;
3328 n = isl_dim_total(dim) + div->n_row;
3330 term = isl_calloc(dim->ctx, struct isl_term,
3331 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3332 if (!term)
3333 goto error;
3335 term->ref = 1;
3336 term->dim = dim;
3337 term->div = div;
3338 isl_int_init(term->n);
3339 isl_int_init(term->d);
3341 return term;
3342 error:
3343 isl_dim_free(dim);
3344 isl_mat_free(div);
3345 return NULL;
3348 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3350 if (!term)
3351 return NULL;
3353 term->ref++;
3354 return term;
3357 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3359 int i;
3360 isl_term *dup;
3361 unsigned total;
3363 if (term)
3364 return NULL;
3366 total = isl_dim_total(term->dim) + term->div->n_row;
3368 dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3369 if (!dup)
3370 return NULL;
3372 isl_int_set(dup->n, term->n);
3373 isl_int_set(dup->d, term->d);
3375 for (i = 0; i < total; ++i)
3376 dup->pow[i] = term->pow[i];
3378 return dup;
3381 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3383 if (!term)
3384 return NULL;
3386 if (term->ref == 1)
3387 return term;
3388 term->ref--;
3389 return isl_term_dup(term);
3392 void isl_term_free(__isl_take isl_term *term)
3394 if (!term)
3395 return;
3397 if (--term->ref > 0)
3398 return;
3400 isl_dim_free(term->dim);
3401 isl_mat_free(term->div);
3402 isl_int_clear(term->n);
3403 isl_int_clear(term->d);
3404 free(term);
3407 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3409 if (!term)
3410 return 0;
3412 switch (type) {
3413 case isl_dim_param:
3414 case isl_dim_in:
3415 case isl_dim_out: return isl_dim_size(term->dim, type);
3416 case isl_dim_div: return term->div->n_row;
3417 case isl_dim_all: return isl_dim_total(term->dim) + term->div->n_row;
3418 default: return 0;
3422 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3424 return term ? term->dim->ctx : NULL;
3427 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3429 if (!term)
3430 return;
3431 isl_int_set(*n, term->n);
3434 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3436 if (!term)
3437 return;
3438 isl_int_set(*d, term->d);
3441 int isl_term_get_exp(__isl_keep isl_term *term,
3442 enum isl_dim_type type, unsigned pos)
3444 if (!term)
3445 return -1;
3447 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3449 if (type >= isl_dim_set)
3450 pos += isl_dim_size(term->dim, isl_dim_param);
3451 if (type >= isl_dim_div)
3452 pos += isl_dim_size(term->dim, isl_dim_set);
3454 return term->pow[pos];
3457 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3459 isl_basic_map *bmap;
3460 unsigned total;
3461 int k;
3463 if (!term)
3464 return NULL;
3466 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3467 return NULL);
3469 total = term->div->n_col - term->div->n_row - 2;
3470 /* No nested divs for now */
3471 isl_assert(term->dim->ctx,
3472 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3473 term->div->n_row) == -1,
3474 return NULL);
3476 bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3477 if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3478 goto error;
3480 isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3482 return isl_basic_map_div(bmap, k);
3483 error:
3484 isl_basic_map_free(bmap);
3485 return NULL;
3488 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3489 int (*fn)(__isl_take isl_term *term, void *user),
3490 __isl_take isl_term *term, void *user)
3492 int i;
3493 struct isl_upoly_rec *rec;
3495 if (!up || !term)
3496 goto error;
3498 if (isl_upoly_is_zero(up))
3499 return term;
3501 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3502 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3503 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3505 if (isl_upoly_is_cst(up)) {
3506 struct isl_upoly_cst *cst;
3507 cst = isl_upoly_as_cst(up);
3508 if (!cst)
3509 goto error;
3510 term = isl_term_cow(term);
3511 if (!term)
3512 goto error;
3513 isl_int_set(term->n, cst->n);
3514 isl_int_set(term->d, cst->d);
3515 if (fn(isl_term_copy(term), user) < 0)
3516 goto error;
3517 return term;
3520 rec = isl_upoly_as_rec(up);
3521 if (!rec)
3522 goto error;
3524 for (i = 0; i < rec->n; ++i) {
3525 term = isl_term_cow(term);
3526 if (!term)
3527 goto error;
3528 term->pow[up->var] = i;
3529 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3530 if (!term)
3531 goto error;
3533 term->pow[up->var] = 0;
3535 return term;
3536 error:
3537 isl_term_free(term);
3538 return NULL;
3541 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3542 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3544 isl_term *term;
3546 if (!qp)
3547 return -1;
3549 term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3550 if (!term)
3551 return -1;
3553 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3555 isl_term_free(term);
3557 return term ? 0 : -1;
3560 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3562 struct isl_upoly *up;
3563 isl_qpolynomial *qp;
3564 int i, n;
3566 if (!term)
3567 return NULL;
3569 n = isl_dim_total(term->dim) + term->div->n_row;
3571 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3572 for (i = 0; i < n; ++i) {
3573 if (!term->pow[i])
3574 continue;
3575 up = isl_upoly_mul(up,
3576 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3579 qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3580 if (!qp)
3581 goto error;
3582 isl_mat_free(qp->div);
3583 qp->div = isl_mat_copy(term->div);
3584 if (!qp->div)
3585 goto error;
3587 isl_term_free(term);
3588 return qp;
3589 error:
3590 isl_qpolynomial_free(qp);
3591 isl_term_free(term);
3592 return NULL;
3595 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3596 __isl_take isl_dim *dim)
3598 int i;
3599 int extra;
3600 unsigned total;
3602 if (!qp || !dim)
3603 goto error;
3605 if (isl_dim_equal(qp->dim, dim)) {
3606 isl_dim_free(dim);
3607 return qp;
3610 qp = isl_qpolynomial_cow(qp);
3611 if (!qp)
3612 goto error;
3614 extra = isl_dim_size(dim, isl_dim_set) -
3615 isl_dim_size(qp->dim, isl_dim_set);
3616 total = isl_dim_total(qp->dim);
3617 if (qp->div->n_row) {
3618 int *exp;
3620 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3621 if (!exp)
3622 goto error;
3623 for (i = 0; i < qp->div->n_row; ++i)
3624 exp[i] = extra + i;
3625 qp->upoly = expand(qp->upoly, exp, total);
3626 free(exp);
3627 if (!qp->upoly)
3628 goto error;
3630 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3631 if (!qp->div)
3632 goto error;
3633 for (i = 0; i < qp->div->n_row; ++i)
3634 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3636 isl_dim_free(qp->dim);
3637 qp->dim = dim;
3639 return qp;
3640 error:
3641 isl_dim_free(dim);
3642 isl_qpolynomial_free(qp);
3643 return NULL;
3646 /* For each parameter or variable that does not appear in qp,
3647 * first eliminate the variable from all constraints and then set it to zero.
3649 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3650 __isl_keep isl_qpolynomial *qp)
3652 int *active = NULL;
3653 int i;
3654 int d;
3655 unsigned nparam;
3656 unsigned nvar;
3658 if (!set || !qp)
3659 goto error;
3661 d = isl_dim_total(set->dim);
3662 active = isl_calloc_array(set->ctx, int, d);
3663 if (set_active(qp, active) < 0)
3664 goto error;
3666 for (i = 0; i < d; ++i)
3667 if (!active[i])
3668 break;
3670 if (i == d) {
3671 free(active);
3672 return set;
3675 nparam = isl_dim_size(set->dim, isl_dim_param);
3676 nvar = isl_dim_size(set->dim, isl_dim_set);
3677 for (i = 0; i < nparam; ++i) {
3678 if (active[i])
3679 continue;
3680 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3681 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3683 for (i = 0; i < nvar; ++i) {
3684 if (active[nparam + i])
3685 continue;
3686 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3687 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3690 free(active);
3692 return set;
3693 error:
3694 free(active);
3695 isl_set_free(set);
3696 return NULL;
3699 struct isl_opt_data {
3700 isl_qpolynomial *qp;
3701 int first;
3702 isl_qpolynomial *opt;
3703 int max;
3706 static int opt_fn(__isl_take isl_point *pnt, void *user)
3708 struct isl_opt_data *data = (struct isl_opt_data *)user;
3709 isl_qpolynomial *val;
3711 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3712 if (data->first) {
3713 data->first = 0;
3714 data->opt = val;
3715 } else if (data->max) {
3716 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3717 } else {
3718 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3721 return 0;
3724 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3725 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3727 struct isl_opt_data data = { NULL, 1, NULL, max };
3729 if (!set || !qp)
3730 goto error;
3732 if (isl_upoly_is_cst(qp->upoly)) {
3733 isl_set_free(set);
3734 return qp;
3737 set = fix_inactive(set, qp);
3739 data.qp = qp;
3740 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3741 goto error;
3743 if (data.first)
3744 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3746 isl_set_free(set);
3747 isl_qpolynomial_free(qp);
3748 return data.opt;
3749 error:
3750 isl_set_free(set);
3751 isl_qpolynomial_free(qp);
3752 isl_qpolynomial_free(data.opt);
3753 return NULL;
3756 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3757 __isl_take isl_morph *morph)
3759 int i;
3760 int n_sub;
3761 isl_ctx *ctx;
3762 struct isl_upoly **subs;
3763 isl_mat *mat;
3765 qp = isl_qpolynomial_cow(qp);
3766 if (!qp || !morph)
3767 goto error;
3769 ctx = qp->dim->ctx;
3770 isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3772 n_sub = morph->inv->n_row - 1;
3773 if (morph->inv->n_row != morph->inv->n_col)
3774 n_sub += qp->div->n_row;
3775 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3776 if (!subs)
3777 goto error;
3779 for (i = 0; 1 + i < morph->inv->n_row; ++i)
3780 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3781 morph->inv->row[0][0], morph->inv->n_col);
3782 if (morph->inv->n_row != morph->inv->n_col)
3783 for (i = 0; i < qp->div->n_row; ++i)
3784 subs[morph->inv->n_row - 1 + i] =
3785 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3787 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3789 for (i = 0; i < n_sub; ++i)
3790 isl_upoly_free(subs[i]);
3791 free(subs);
3793 mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3794 mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3795 qp->div = isl_mat_product(qp->div, mat);
3796 isl_dim_free(qp->dim);
3797 qp->dim = isl_dim_copy(morph->ran->dim);
3799 if (!qp->upoly || !qp->div || !qp->dim)
3800 goto error;
3802 isl_morph_free(morph);
3804 return qp;
3805 error:
3806 isl_qpolynomial_free(qp);
3807 isl_morph_free(morph);
3808 return NULL;
3811 static int neg_entry(void **entry, void *user)
3813 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3815 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3817 return *pwqp ? 0 : -1;
3820 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3821 __isl_take isl_union_pw_qpolynomial *upwqp)
3823 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3824 if (!upwqp)
3825 return NULL;
3827 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3828 &neg_entry, NULL) < 0)
3829 goto error;
3831 return upwqp;
3832 error:
3833 isl_union_pw_qpolynomial_free(upwqp);
3834 return NULL;
3837 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3838 __isl_take isl_union_pw_qpolynomial *upwqp1,
3839 __isl_take isl_union_pw_qpolynomial *upwqp2)
3841 return isl_union_pw_qpolynomial_add(upwqp1,
3842 isl_union_pw_qpolynomial_neg(upwqp2));
3845 static int mul_entry(void **entry, void *user)
3847 struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3848 uint32_t hash;
3849 struct isl_hash_table_entry *entry2;
3850 isl_pw_qpolynomial *pwpq = *entry;
3851 int empty;
3853 hash = isl_dim_get_hash(pwpq->dim);
3854 entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3855 hash, &has_dim, pwpq->dim, 0);
3856 if (!entry2)
3857 return 0;
3859 pwpq = isl_pw_qpolynomial_copy(pwpq);
3860 pwpq = isl_pw_qpolynomial_mul(pwpq,
3861 isl_pw_qpolynomial_copy(entry2->data));
3863 empty = isl_pw_qpolynomial_is_zero(pwpq);
3864 if (empty < 0) {
3865 isl_pw_qpolynomial_free(pwpq);
3866 return -1;
3868 if (empty) {
3869 isl_pw_qpolynomial_free(pwpq);
3870 return 0;
3873 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3875 return 0;
3878 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3879 __isl_take isl_union_pw_qpolynomial *upwqp1,
3880 __isl_take isl_union_pw_qpolynomial *upwqp2)
3882 return match_bin_op(upwqp1, upwqp2, &mul_entry);
3885 /* Reorder the columns of the given div definitions according to the
3886 * given reordering.
3888 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3889 __isl_take isl_reordering *r)
3891 int i, j;
3892 isl_mat *mat;
3893 int extra;
3895 if (!div || !r)
3896 goto error;
3898 extra = isl_dim_total(r->dim) + div->n_row - r->len;
3899 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3900 if (!mat)
3901 goto error;
3903 for (i = 0; i < div->n_row; ++i) {
3904 isl_seq_cpy(mat->row[i], div->row[i], 2);
3905 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3906 for (j = 0; j < r->len; ++j)
3907 isl_int_set(mat->row[i][2 + r->pos[j]],
3908 div->row[i][2 + j]);
3911 isl_reordering_free(r);
3912 isl_mat_free(div);
3913 return mat;
3914 error:
3915 isl_reordering_free(r);
3916 isl_mat_free(div);
3917 return NULL;
3920 /* Reorder the dimension of "qp" according to the given reordering.
3922 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3923 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3925 qp = isl_qpolynomial_cow(qp);
3926 if (!qp)
3927 goto error;
3929 r = isl_reordering_extend(r, qp->div->n_row);
3930 if (!r)
3931 goto error;
3933 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3934 if (!qp->div)
3935 goto error;
3937 qp->upoly = reorder(qp->upoly, r->pos);
3938 if (!qp->upoly)
3939 goto error;
3941 qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3943 isl_reordering_free(r);
3944 return qp;
3945 error:
3946 isl_qpolynomial_free(qp);
3947 isl_reordering_free(r);
3948 return NULL;
3951 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
3952 __isl_take isl_qpolynomial *qp, __isl_take isl_dim *model)
3954 if (!qp || !model)
3955 goto error;
3957 if (!isl_dim_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
3958 isl_reordering *exp;
3960 model = isl_dim_drop(model, isl_dim_in,
3961 0, isl_dim_size(model, isl_dim_in));
3962 model = isl_dim_drop(model, isl_dim_out,
3963 0, isl_dim_size(model, isl_dim_out));
3964 exp = isl_parameter_alignment_reordering(qp->dim, model);
3965 exp = isl_reordering_extend_dim(exp,
3966 isl_qpolynomial_get_dim(qp));
3967 qp = isl_qpolynomial_realign(qp, exp);
3970 isl_dim_free(model);
3971 return qp;
3972 error:
3973 isl_dim_free(model);
3974 isl_qpolynomial_free(qp);
3975 return NULL;
3978 struct isl_split_periods_data {
3979 int max_periods;
3980 isl_pw_qpolynomial *res;
3983 /* Create a slice where the integer division "div" has the fixed value "v".
3984 * In particular, if "div" refers to floor(f/m), then create a slice
3986 * m v <= f <= m v + (m - 1)
3988 * or
3990 * f - m v >= 0
3991 * -f + m v + (m - 1) >= 0
3993 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3994 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3996 int total;
3997 isl_basic_set *bset = NULL;
3998 int k;
4000 if (!dim || !qp)
4001 goto error;
4003 total = isl_dim_total(dim);
4004 bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
4006 k = isl_basic_set_alloc_inequality(bset);
4007 if (k < 0)
4008 goto error;
4009 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4010 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4012 k = isl_basic_set_alloc_inequality(bset);
4013 if (k < 0)
4014 goto error;
4015 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4016 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4017 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4018 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4020 isl_dim_free(dim);
4021 return isl_set_from_basic_set(bset);
4022 error:
4023 isl_basic_set_free(bset);
4024 isl_dim_free(dim);
4025 return NULL;
4028 static int split_periods(__isl_take isl_set *set,
4029 __isl_take isl_qpolynomial *qp, void *user);
4031 /* Create a slice of the domain "set" such that integer division "div"
4032 * has the fixed value "v" and add the results to data->res,
4033 * replacing the integer division by "v" in "qp".
4035 static int set_div(__isl_take isl_set *set,
4036 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4037 struct isl_split_periods_data *data)
4039 int i;
4040 int total;
4041 isl_set *slice;
4042 struct isl_upoly *cst;
4044 slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
4045 set = isl_set_intersect(set, slice);
4047 if (!qp)
4048 goto error;
4050 total = isl_dim_total(qp->dim);
4052 for (i = div + 1; i < qp->div->n_row; ++i) {
4053 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4054 continue;
4055 isl_int_addmul(qp->div->row[i][1],
4056 qp->div->row[i][2 + total + div], v);
4057 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4060 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4061 qp = substitute_div(qp, div, cst);
4063 return split_periods(set, qp, data);
4064 error:
4065 isl_set_free(set);
4066 isl_qpolynomial_free(qp);
4067 return -1;
4070 /* Split the domain "set" such that integer division "div"
4071 * has a fixed value (ranging from "min" to "max") on each slice
4072 * and add the results to data->res.
4074 static int split_div(__isl_take isl_set *set,
4075 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4076 struct isl_split_periods_data *data)
4078 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4079 isl_set *set_i = isl_set_copy(set);
4080 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4082 if (set_div(set_i, qp_i, div, min, data) < 0)
4083 goto error;
4085 isl_set_free(set);
4086 isl_qpolynomial_free(qp);
4087 return 0;
4088 error:
4089 isl_set_free(set);
4090 isl_qpolynomial_free(qp);
4091 return -1;
4094 /* If "qp" refers to any integer division
4095 * that can only attain "max_periods" distinct values on "set"
4096 * then split the domain along those distinct values.
4097 * Add the results (or the original if no splitting occurs)
4098 * to data->res.
4100 static int split_periods(__isl_take isl_set *set,
4101 __isl_take isl_qpolynomial *qp, void *user)
4103 int i;
4104 isl_pw_qpolynomial *pwqp;
4105 struct isl_split_periods_data *data;
4106 isl_int min, max;
4107 int total;
4108 int r = 0;
4110 data = (struct isl_split_periods_data *)user;
4112 if (!set || !qp)
4113 goto error;
4115 if (qp->div->n_row == 0) {
4116 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4117 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4118 return 0;
4121 isl_int_init(min);
4122 isl_int_init(max);
4123 total = isl_dim_total(qp->dim);
4124 for (i = 0; i < qp->div->n_row; ++i) {
4125 enum isl_lp_result lp_res;
4127 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4128 qp->div->n_row) != -1)
4129 continue;
4131 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4132 set->ctx->one, &min, NULL, NULL);
4133 if (lp_res == isl_lp_error)
4134 goto error2;
4135 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4136 continue;
4137 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4139 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4140 set->ctx->one, &max, NULL, NULL);
4141 if (lp_res == isl_lp_error)
4142 goto error2;
4143 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4144 continue;
4145 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4147 isl_int_sub(max, max, min);
4148 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4149 isl_int_add(max, max, min);
4150 break;
4154 if (i < qp->div->n_row) {
4155 r = split_div(set, qp, i, min, max, data);
4156 } else {
4157 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4158 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4161 isl_int_clear(max);
4162 isl_int_clear(min);
4164 return r;
4165 error2:
4166 isl_int_clear(max);
4167 isl_int_clear(min);
4168 error:
4169 isl_set_free(set);
4170 isl_qpolynomial_free(qp);
4171 return -1;
4174 /* If any quasi-polynomial in pwqp refers to any integer division
4175 * that can only attain "max_periods" distinct values on its domain
4176 * then split the domain along those distinct values.
4178 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4179 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4181 struct isl_split_periods_data data;
4183 data.max_periods = max_periods;
4184 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4186 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4187 goto error;
4189 isl_pw_qpolynomial_free(pwqp);
4191 return data.res;
4192 error:
4193 isl_pw_qpolynomial_free(data.res);
4194 isl_pw_qpolynomial_free(pwqp);
4195 return NULL;
4198 /* Construct a piecewise quasipolynomial that is constant on the given
4199 * domain. In particular, it is
4200 * 0 if cst == 0
4201 * 1 if cst == 1
4202 * infinity if cst == -1
4204 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4205 __isl_take isl_basic_set *bset, int cst)
4207 isl_dim *dim;
4208 isl_qpolynomial *qp;
4210 if (!bset)
4211 return NULL;
4213 bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4214 dim = isl_basic_set_get_dim(bset);
4215 if (cst < 0)
4216 qp = isl_qpolynomial_infty(dim);
4217 else if (cst == 0)
4218 qp = isl_qpolynomial_zero(dim);
4219 else
4220 qp = isl_qpolynomial_one(dim);
4221 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4224 /* Factor bset, call fn on each of the factors and return the product.
4226 * If no factors can be found, simply call fn on the input.
4227 * Otherwise, construct the factors based on the factorizer,
4228 * call fn on each factor and compute the product.
4230 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4231 __isl_take isl_basic_set *bset,
4232 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4234 int i, n;
4235 isl_dim *dim;
4236 isl_set *set;
4237 isl_factorizer *f;
4238 isl_qpolynomial *qp;
4239 isl_pw_qpolynomial *pwqp;
4240 unsigned nparam;
4241 unsigned nvar;
4243 f = isl_basic_set_factorizer(bset);
4244 if (!f)
4245 goto error;
4246 if (f->n_group == 0) {
4247 isl_factorizer_free(f);
4248 return fn(bset);
4251 nparam = isl_basic_set_dim(bset, isl_dim_param);
4252 nvar = isl_basic_set_dim(bset, isl_dim_set);
4254 dim = isl_basic_set_get_dim(bset);
4255 dim = isl_dim_domain(dim);
4256 set = isl_set_universe(isl_dim_copy(dim));
4257 qp = isl_qpolynomial_one(dim);
4258 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4260 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4262 for (i = 0, n = 0; i < f->n_group; ++i) {
4263 isl_basic_set *bset_i;
4264 isl_pw_qpolynomial *pwqp_i;
4266 bset_i = isl_basic_set_copy(bset);
4267 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4268 nparam + n + f->len[i], nvar - n - f->len[i]);
4269 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4270 nparam, n);
4271 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4272 n + f->len[i], nvar - n - f->len[i]);
4273 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4275 pwqp_i = fn(bset_i);
4276 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4278 n += f->len[i];
4281 isl_basic_set_free(bset);
4282 isl_factorizer_free(f);
4284 return pwqp;
4285 error:
4286 isl_basic_set_free(bset);
4287 return NULL;
4290 /* Factor bset, call fn on each of the factors and return the product.
4291 * The function is assumed to evaluate to zero on empty domains,
4292 * to one on zero-dimensional domains and to infinity on unbounded domains
4293 * and will not be called explicitly on zero-dimensional or unbounded domains.
4295 * We first check for some special cases and remove all equalities.
4296 * Then we hand over control to compressed_multiplicative_call.
4298 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4299 __isl_take isl_basic_set *bset,
4300 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4302 int bounded;
4303 isl_morph *morph;
4304 isl_pw_qpolynomial *pwqp;
4305 unsigned orig_nvar, final_nvar;
4307 if (!bset)
4308 return NULL;
4310 if (isl_basic_set_plain_is_empty(bset))
4311 return constant_on_domain(bset, 0);
4313 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4315 if (orig_nvar == 0)
4316 return constant_on_domain(bset, 1);
4318 bounded = isl_basic_set_is_bounded(bset);
4319 if (bounded < 0)
4320 goto error;
4321 if (!bounded)
4322 return constant_on_domain(bset, -1);
4324 if (bset->n_eq == 0)
4325 return compressed_multiplicative_call(bset, fn);
4327 morph = isl_basic_set_full_compression(bset);
4328 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4330 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4332 pwqp = compressed_multiplicative_call(bset, fn);
4334 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4335 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4336 morph = isl_morph_inverse(morph);
4338 pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4340 return pwqp;
4341 error:
4342 isl_basic_set_free(bset);
4343 return NULL;
4346 /* Drop all floors in "qp", turning each integer division [a/m] into
4347 * a rational division a/m. If "down" is set, then the integer division
4348 * is replaces by (a-(m-1))/m instead.
4350 static __isl_give isl_qpolynomial *qp_drop_floors(
4351 __isl_take isl_qpolynomial *qp, int down)
4353 int i;
4354 struct isl_upoly *s;
4356 if (!qp)
4357 return NULL;
4358 if (qp->div->n_row == 0)
4359 return qp;
4361 qp = isl_qpolynomial_cow(qp);
4362 if (!qp)
4363 return NULL;
4365 for (i = qp->div->n_row - 1; i >= 0; --i) {
4366 if (down) {
4367 isl_int_sub(qp->div->row[i][1],
4368 qp->div->row[i][1], qp->div->row[i][0]);
4369 isl_int_add_ui(qp->div->row[i][1],
4370 qp->div->row[i][1], 1);
4372 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4373 qp->div->row[i][0], qp->div->n_col - 1);
4374 qp = substitute_div(qp, i, s);
4375 if (!qp)
4376 return NULL;
4379 return qp;
4382 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4383 * a rational division a/m.
4385 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4386 __isl_take isl_pw_qpolynomial *pwqp)
4388 int i;
4390 if (!pwqp)
4391 return NULL;
4393 if (isl_pw_qpolynomial_is_zero(pwqp))
4394 return pwqp;
4396 pwqp = isl_pw_qpolynomial_cow(pwqp);
4397 if (!pwqp)
4398 return NULL;
4400 for (i = 0; i < pwqp->n; ++i) {
4401 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4402 if (!pwqp->p[i].qp)
4403 goto error;
4406 return pwqp;
4407 error:
4408 isl_pw_qpolynomial_free(pwqp);
4409 return NULL;
4412 /* Adjust all the integer divisions in "qp" such that they are at least
4413 * one over the given orthant (identified by "signs"). This ensures
4414 * that they will still be non-negative even after subtracting (m-1)/m.
4416 * In particular, f is replaced by f' + v, changing f = [a/m]
4417 * to f' = [(a - m v)/m].
4418 * If the constant term k in a is smaller than m,
4419 * the constant term of v is set to floor(k/m) - 1.
4420 * For any other term, if the coefficient c and the variable x have
4421 * the same sign, then no changes are needed.
4422 * Otherwise, if the variable is positive (and c is negative),
4423 * then the coefficient of x in v is set to floor(c/m).
4424 * If the variable is negative (and c is positive),
4425 * then the coefficient of x in v is set to ceil(c/m).
4427 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4428 int *signs)
4430 int i, j;
4431 int total;
4432 isl_vec *v = NULL;
4433 struct isl_upoly *s;
4435 qp = isl_qpolynomial_cow(qp);
4436 if (!qp)
4437 return NULL;
4438 qp->div = isl_mat_cow(qp->div);
4439 if (!qp->div)
4440 goto error;
4442 total = isl_dim_total(qp->dim);
4443 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4445 for (i = 0; i < qp->div->n_row; ++i) {
4446 isl_int *row = qp->div->row[i];
4447 v = isl_vec_clr(v);
4448 if (!v)
4449 goto error;
4450 if (isl_int_lt(row[1], row[0])) {
4451 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4452 isl_int_sub_ui(v->el[0], v->el[0], 1);
4453 isl_int_submul(row[1], row[0], v->el[0]);
4455 for (j = 0; j < total; ++j) {
4456 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4457 continue;
4458 if (signs[j] < 0)
4459 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4460 else
4461 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4462 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4464 for (j = 0; j < i; ++j) {
4465 if (isl_int_sgn(row[2 + total + j]) >= 0)
4466 continue;
4467 isl_int_fdiv_q(v->el[1 + total + j],
4468 row[2 + total + j], row[0]);
4469 isl_int_submul(row[2 + total + j],
4470 row[0], v->el[1 + total + j]);
4472 for (j = i + 1; j < qp->div->n_row; ++j) {
4473 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4474 continue;
4475 isl_seq_combine(qp->div->row[j] + 1,
4476 qp->div->ctx->one, qp->div->row[j] + 1,
4477 qp->div->row[j][2 + total + i], v->el, v->size);
4479 isl_int_set_si(v->el[1 + total + i], 1);
4480 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4481 qp->div->ctx->one, v->size);
4482 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4483 isl_upoly_free(s);
4484 if (!qp->upoly)
4485 goto error;
4488 isl_vec_free(v);
4489 return qp;
4490 error:
4491 isl_vec_free(v);
4492 isl_qpolynomial_free(qp);
4493 return NULL;
4496 struct isl_to_poly_data {
4497 int sign;
4498 isl_pw_qpolynomial *res;
4499 isl_qpolynomial *qp;
4502 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4503 * We first make all integer divisions positive and then split the
4504 * quasipolynomials into terms with sign data->sign (the direction
4505 * of the requested approximation) and terms with the opposite sign.
4506 * In the first set of terms, each integer division [a/m] is
4507 * overapproximated by a/m, while in the second it is underapproximated
4508 * by (a-(m-1))/m.
4510 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4511 void *user)
4513 struct isl_to_poly_data *data = user;
4514 isl_pw_qpolynomial *t;
4515 isl_qpolynomial *qp, *up, *down;
4517 qp = isl_qpolynomial_copy(data->qp);
4518 qp = make_divs_pos(qp, signs);
4520 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4521 up = qp_drop_floors(up, 0);
4522 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4523 down = qp_drop_floors(down, 1);
4525 isl_qpolynomial_free(qp);
4526 qp = isl_qpolynomial_add(up, down);
4528 t = isl_pw_qpolynomial_alloc(orthant, qp);
4529 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4531 return 0;
4534 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4535 * the polynomial will be an overapproximation. If "sign" is negative,
4536 * it will be an underapproximation. If "sign" is zero, the approximation
4537 * will lie somewhere in between.
4539 * In particular, is sign == 0, we simply drop the floors, turning
4540 * the integer divisions into rational divisions.
4541 * Otherwise, we split the domains into orthants, make all integer divisions
4542 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4543 * depending on the requested sign and the sign of the term in which
4544 * the integer division appears.
4546 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4547 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4549 int i;
4550 struct isl_to_poly_data data;
4552 if (sign == 0)
4553 return pwqp_drop_floors(pwqp);
4555 if (!pwqp)
4556 return NULL;
4558 data.sign = sign;
4559 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4561 for (i = 0; i < pwqp->n; ++i) {
4562 if (pwqp->p[i].qp->div->n_row == 0) {
4563 isl_pw_qpolynomial *t;
4564 t = isl_pw_qpolynomial_alloc(
4565 isl_set_copy(pwqp->p[i].set),
4566 isl_qpolynomial_copy(pwqp->p[i].qp));
4567 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4568 continue;
4570 data.qp = pwqp->p[i].qp;
4571 if (isl_set_foreach_orthant(pwqp->p[i].set,
4572 &to_polynomial_on_orthant, &data) < 0)
4573 goto error;
4576 isl_pw_qpolynomial_free(pwqp);
4578 return data.res;
4579 error:
4580 isl_pw_qpolynomial_free(pwqp);
4581 isl_pw_qpolynomial_free(data.res);
4582 return NULL;
4585 static int poly_entry(void **entry, void *user)
4587 int *sign = user;
4588 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4590 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4592 return *pwqp ? 0 : -1;
4595 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4596 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4598 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4599 if (!upwqp)
4600 return NULL;
4602 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4603 &poly_entry, &sign) < 0)
4604 goto error;
4606 return upwqp;
4607 error:
4608 isl_union_pw_qpolynomial_free(upwqp);
4609 return NULL;
4612 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4613 __isl_take isl_qpolynomial *qp)
4615 int i, k;
4616 isl_dim *dim;
4617 isl_vec *aff = NULL;
4618 isl_basic_map *bmap = NULL;
4619 unsigned pos;
4620 unsigned n_div;
4622 if (!qp)
4623 return NULL;
4624 if (!isl_upoly_is_affine(qp->upoly))
4625 isl_die(qp->dim->ctx, isl_error_invalid,
4626 "input quasi-polynomial not affine", goto error);
4627 aff = isl_qpolynomial_extract_affine(qp);
4628 if (!aff)
4629 goto error;
4630 dim = isl_qpolynomial_get_dim(qp);
4631 dim = isl_dim_from_domain(dim);
4632 pos = 1 + isl_dim_offset(dim, isl_dim_out);
4633 dim = isl_dim_add(dim, isl_dim_out, 1);
4634 n_div = qp->div->n_row;
4635 bmap = isl_basic_map_alloc_dim(dim, n_div, 1, 2 * n_div);
4637 for (i = 0; i < n_div; ++i) {
4638 k = isl_basic_map_alloc_div(bmap);
4639 if (k < 0)
4640 goto error;
4641 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4642 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4643 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4644 goto error;
4646 k = isl_basic_map_alloc_equality(bmap);
4647 if (k < 0)
4648 goto error;
4649 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4650 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4651 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4653 isl_vec_free(aff);
4654 isl_qpolynomial_free(qp);
4655 bmap = isl_basic_map_finalize(bmap);
4656 return bmap;
4657 error:
4658 isl_vec_free(aff);
4659 isl_qpolynomial_free(qp);
4660 isl_basic_map_free(bmap);
4661 return NULL;