isl_{set,map}_{,unshifted_}simple_hull: finalize result
[isl.git] / isl_polynomial.c
blobede6c2046737a49b87b53e046ca93238088263dd
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT 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 #define ISL_DIM_H
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_factorization.h>
16 #include <isl_lp_private.h>
17 #include <isl_seq.h>
18 #include <isl_union_map_private.h>
19 #include <isl_constraint_private.h>
20 #include <isl_polynomial_private.h>
21 #include <isl_point_private.h>
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_range.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
30 #include <isl/deprecated/polynomial_int.h>
32 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
34 switch (type) {
35 case isl_dim_param: return 0;
36 case isl_dim_in: return dim->nparam;
37 case isl_dim_out: return dim->nparam + dim->n_in;
38 default: return 0;
42 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
44 if (!up)
45 return -1;
47 return up->var < 0;
50 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
52 if (!up)
53 return NULL;
55 isl_assert(up->ctx, up->var < 0, return NULL);
57 return (struct isl_upoly_cst *)up;
60 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
62 if (!up)
63 return NULL;
65 isl_assert(up->ctx, up->var >= 0, return NULL);
67 return (struct isl_upoly_rec *)up;
70 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
71 __isl_keep struct isl_upoly *up2)
73 int i;
74 struct isl_upoly_rec *rec1, *rec2;
76 if (!up1 || !up2)
77 return -1;
78 if (up1 == up2)
79 return 1;
80 if (up1->var != up2->var)
81 return 0;
82 if (isl_upoly_is_cst(up1)) {
83 struct isl_upoly_cst *cst1, *cst2;
84 cst1 = isl_upoly_as_cst(up1);
85 cst2 = isl_upoly_as_cst(up2);
86 if (!cst1 || !cst2)
87 return -1;
88 return isl_int_eq(cst1->n, cst2->n) &&
89 isl_int_eq(cst1->d, cst2->d);
92 rec1 = isl_upoly_as_rec(up1);
93 rec2 = isl_upoly_as_rec(up2);
94 if (!rec1 || !rec2)
95 return -1;
97 if (rec1->n != rec2->n)
98 return 0;
100 for (i = 0; i < rec1->n; ++i) {
101 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
102 if (eq < 0 || !eq)
103 return eq;
106 return 1;
109 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
111 struct isl_upoly_cst *cst;
113 if (!up)
114 return -1;
115 if (!isl_upoly_is_cst(up))
116 return 0;
118 cst = isl_upoly_as_cst(up);
119 if (!cst)
120 return -1;
122 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
125 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
127 struct isl_upoly_cst *cst;
129 if (!up)
130 return 0;
131 if (!isl_upoly_is_cst(up))
132 return 0;
134 cst = isl_upoly_as_cst(up);
135 if (!cst)
136 return 0;
138 return isl_int_sgn(cst->n);
141 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
143 struct isl_upoly_cst *cst;
145 if (!up)
146 return -1;
147 if (!isl_upoly_is_cst(up))
148 return 0;
150 cst = isl_upoly_as_cst(up);
151 if (!cst)
152 return -1;
154 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
157 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
159 struct isl_upoly_cst *cst;
161 if (!up)
162 return -1;
163 if (!isl_upoly_is_cst(up))
164 return 0;
166 cst = isl_upoly_as_cst(up);
167 if (!cst)
168 return -1;
170 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
173 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
175 struct isl_upoly_cst *cst;
177 if (!up)
178 return -1;
179 if (!isl_upoly_is_cst(up))
180 return 0;
182 cst = isl_upoly_as_cst(up);
183 if (!cst)
184 return -1;
186 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
189 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
191 struct isl_upoly_cst *cst;
193 if (!up)
194 return -1;
195 if (!isl_upoly_is_cst(up))
196 return 0;
198 cst = isl_upoly_as_cst(up);
199 if (!cst)
200 return -1;
202 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
205 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
207 struct isl_upoly_cst *cst;
209 if (!up)
210 return -1;
211 if (!isl_upoly_is_cst(up))
212 return 0;
214 cst = isl_upoly_as_cst(up);
215 if (!cst)
216 return -1;
218 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
221 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
223 struct isl_upoly_cst *cst;
225 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
226 if (!cst)
227 return NULL;
229 cst->up.ref = 1;
230 cst->up.ctx = ctx;
231 isl_ctx_ref(ctx);
232 cst->up.var = -1;
234 isl_int_init(cst->n);
235 isl_int_init(cst->d);
237 return cst;
240 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
242 struct isl_upoly_cst *cst;
244 cst = isl_upoly_cst_alloc(ctx);
245 if (!cst)
246 return NULL;
248 isl_int_set_si(cst->n, 0);
249 isl_int_set_si(cst->d, 1);
251 return &cst->up;
254 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
256 struct isl_upoly_cst *cst;
258 cst = isl_upoly_cst_alloc(ctx);
259 if (!cst)
260 return NULL;
262 isl_int_set_si(cst->n, 1);
263 isl_int_set_si(cst->d, 1);
265 return &cst->up;
268 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
270 struct isl_upoly_cst *cst;
272 cst = isl_upoly_cst_alloc(ctx);
273 if (!cst)
274 return NULL;
276 isl_int_set_si(cst->n, 1);
277 isl_int_set_si(cst->d, 0);
279 return &cst->up;
282 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
284 struct isl_upoly_cst *cst;
286 cst = isl_upoly_cst_alloc(ctx);
287 if (!cst)
288 return NULL;
290 isl_int_set_si(cst->n, -1);
291 isl_int_set_si(cst->d, 0);
293 return &cst->up;
296 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
298 struct isl_upoly_cst *cst;
300 cst = isl_upoly_cst_alloc(ctx);
301 if (!cst)
302 return NULL;
304 isl_int_set_si(cst->n, 0);
305 isl_int_set_si(cst->d, 0);
307 return &cst->up;
310 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
311 isl_int n, isl_int d)
313 struct isl_upoly_cst *cst;
315 cst = isl_upoly_cst_alloc(ctx);
316 if (!cst)
317 return NULL;
319 isl_int_set(cst->n, n);
320 isl_int_set(cst->d, d);
322 return &cst->up;
325 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
326 int var, int size)
328 struct isl_upoly_rec *rec;
330 isl_assert(ctx, var >= 0, return NULL);
331 isl_assert(ctx, size >= 0, return NULL);
332 rec = isl_calloc(ctx, struct isl_upoly_rec,
333 sizeof(struct isl_upoly_rec) +
334 size * sizeof(struct isl_upoly *));
335 if (!rec)
336 return NULL;
338 rec->up.ref = 1;
339 rec->up.ctx = ctx;
340 isl_ctx_ref(ctx);
341 rec->up.var = var;
343 rec->n = 0;
344 rec->size = size;
346 return rec;
349 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
350 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
352 qp = isl_qpolynomial_cow(qp);
353 if (!qp || !dim)
354 goto error;
356 isl_space_free(qp->dim);
357 qp->dim = dim;
359 return qp;
360 error:
361 isl_qpolynomial_free(qp);
362 isl_space_free(dim);
363 return NULL;
366 /* Reset the space of "qp". This function is called from isl_pw_templ.c
367 * and doesn't know if the space of an element object is represented
368 * directly or through its domain. It therefore passes along both.
370 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
371 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
372 __isl_take isl_space *domain)
374 isl_space_free(space);
375 return isl_qpolynomial_reset_domain_space(qp, domain);
378 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
380 return qp ? qp->dim->ctx : NULL;
383 __isl_give isl_space *isl_qpolynomial_get_domain_space(
384 __isl_keep isl_qpolynomial *qp)
386 return qp ? isl_space_copy(qp->dim) : NULL;
389 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
391 isl_space *space;
392 if (!qp)
393 return NULL;
394 space = isl_space_copy(qp->dim);
395 space = isl_space_from_domain(space);
396 space = isl_space_add_dims(space, isl_dim_out, 1);
397 return space;
400 /* Externally, an isl_qpolynomial has a map space, but internally, the
401 * ls field corresponds to the domain of that space.
403 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
404 enum isl_dim_type type)
406 if (!qp)
407 return 0;
408 if (type == isl_dim_out)
409 return 1;
410 if (type == isl_dim_in)
411 type = isl_dim_set;
412 return isl_space_dim(qp->dim, type);
415 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
417 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
420 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
422 return qp ? isl_upoly_is_one(qp->upoly) : -1;
425 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
427 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
430 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
432 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
435 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
437 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
440 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
442 return qp ? isl_upoly_sgn(qp->upoly) : 0;
445 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
447 isl_int_clear(cst->n);
448 isl_int_clear(cst->d);
451 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
453 int i;
455 for (i = 0; i < rec->n; ++i)
456 isl_upoly_free(rec->p[i]);
459 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
461 if (!up)
462 return NULL;
464 up->ref++;
465 return up;
468 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
470 struct isl_upoly_cst *cst;
471 struct isl_upoly_cst *dup;
473 cst = isl_upoly_as_cst(up);
474 if (!cst)
475 return NULL;
477 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
478 if (!dup)
479 return NULL;
480 isl_int_set(dup->n, cst->n);
481 isl_int_set(dup->d, cst->d);
483 return &dup->up;
486 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
488 int i;
489 struct isl_upoly_rec *rec;
490 struct isl_upoly_rec *dup;
492 rec = isl_upoly_as_rec(up);
493 if (!rec)
494 return NULL;
496 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
497 if (!dup)
498 return NULL;
500 for (i = 0; i < rec->n; ++i) {
501 dup->p[i] = isl_upoly_copy(rec->p[i]);
502 if (!dup->p[i])
503 goto error;
504 dup->n++;
507 return &dup->up;
508 error:
509 isl_upoly_free(&dup->up);
510 return NULL;
513 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
515 if (!up)
516 return NULL;
518 if (isl_upoly_is_cst(up))
519 return isl_upoly_dup_cst(up);
520 else
521 return isl_upoly_dup_rec(up);
524 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
526 if (!up)
527 return NULL;
529 if (up->ref == 1)
530 return up;
531 up->ref--;
532 return isl_upoly_dup(up);
535 void isl_upoly_free(__isl_take struct isl_upoly *up)
537 if (!up)
538 return;
540 if (--up->ref > 0)
541 return;
543 if (up->var < 0)
544 upoly_free_cst((struct isl_upoly_cst *)up);
545 else
546 upoly_free_rec((struct isl_upoly_rec *)up);
548 isl_ctx_deref(up->ctx);
549 free(up);
552 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
554 isl_int gcd;
556 isl_int_init(gcd);
557 isl_int_gcd(gcd, cst->n, cst->d);
558 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
559 isl_int_divexact(cst->n, cst->n, gcd);
560 isl_int_divexact(cst->d, cst->d, gcd);
562 isl_int_clear(gcd);
565 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
566 __isl_take struct isl_upoly *up2)
568 struct isl_upoly_cst *cst1;
569 struct isl_upoly_cst *cst2;
571 up1 = isl_upoly_cow(up1);
572 if (!up1 || !up2)
573 goto error;
575 cst1 = isl_upoly_as_cst(up1);
576 cst2 = isl_upoly_as_cst(up2);
578 if (isl_int_eq(cst1->d, cst2->d))
579 isl_int_add(cst1->n, cst1->n, cst2->n);
580 else {
581 isl_int_mul(cst1->n, cst1->n, cst2->d);
582 isl_int_addmul(cst1->n, cst2->n, cst1->d);
583 isl_int_mul(cst1->d, cst1->d, cst2->d);
586 isl_upoly_cst_reduce(cst1);
588 isl_upoly_free(up2);
589 return up1;
590 error:
591 isl_upoly_free(up1);
592 isl_upoly_free(up2);
593 return NULL;
596 static __isl_give struct isl_upoly *replace_by_zero(
597 __isl_take struct isl_upoly *up)
599 struct isl_ctx *ctx;
601 if (!up)
602 return NULL;
603 ctx = up->ctx;
604 isl_upoly_free(up);
605 return isl_upoly_zero(ctx);
608 static __isl_give struct isl_upoly *replace_by_constant_term(
609 __isl_take struct isl_upoly *up)
611 struct isl_upoly_rec *rec;
612 struct isl_upoly *cst;
614 if (!up)
615 return NULL;
617 rec = isl_upoly_as_rec(up);
618 if (!rec)
619 goto error;
620 cst = isl_upoly_copy(rec->p[0]);
621 isl_upoly_free(up);
622 return cst;
623 error:
624 isl_upoly_free(up);
625 return NULL;
628 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
629 __isl_take struct isl_upoly *up2)
631 int i;
632 struct isl_upoly_rec *rec1, *rec2;
634 if (!up1 || !up2)
635 goto error;
637 if (isl_upoly_is_nan(up1)) {
638 isl_upoly_free(up2);
639 return up1;
642 if (isl_upoly_is_nan(up2)) {
643 isl_upoly_free(up1);
644 return up2;
647 if (isl_upoly_is_zero(up1)) {
648 isl_upoly_free(up1);
649 return up2;
652 if (isl_upoly_is_zero(up2)) {
653 isl_upoly_free(up2);
654 return up1;
657 if (up1->var < up2->var)
658 return isl_upoly_sum(up2, up1);
660 if (up2->var < up1->var) {
661 struct isl_upoly_rec *rec;
662 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
663 isl_upoly_free(up1);
664 return up2;
666 up1 = isl_upoly_cow(up1);
667 rec = isl_upoly_as_rec(up1);
668 if (!rec)
669 goto error;
670 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
671 if (rec->n == 1)
672 up1 = replace_by_constant_term(up1);
673 return up1;
676 if (isl_upoly_is_cst(up1))
677 return isl_upoly_sum_cst(up1, up2);
679 rec1 = isl_upoly_as_rec(up1);
680 rec2 = isl_upoly_as_rec(up2);
681 if (!rec1 || !rec2)
682 goto error;
684 if (rec1->n < rec2->n)
685 return isl_upoly_sum(up2, up1);
687 up1 = isl_upoly_cow(up1);
688 rec1 = isl_upoly_as_rec(up1);
689 if (!rec1)
690 goto error;
692 for (i = rec2->n - 1; i >= 0; --i) {
693 rec1->p[i] = isl_upoly_sum(rec1->p[i],
694 isl_upoly_copy(rec2->p[i]));
695 if (!rec1->p[i])
696 goto error;
697 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
698 isl_upoly_free(rec1->p[i]);
699 rec1->n--;
703 if (rec1->n == 0)
704 up1 = replace_by_zero(up1);
705 else if (rec1->n == 1)
706 up1 = replace_by_constant_term(up1);
708 isl_upoly_free(up2);
710 return up1;
711 error:
712 isl_upoly_free(up1);
713 isl_upoly_free(up2);
714 return NULL;
717 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
718 __isl_take struct isl_upoly *up, isl_int v)
720 struct isl_upoly_cst *cst;
722 up = isl_upoly_cow(up);
723 if (!up)
724 return NULL;
726 cst = isl_upoly_as_cst(up);
728 isl_int_addmul(cst->n, cst->d, v);
730 return up;
733 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
734 __isl_take struct isl_upoly *up, isl_int v)
736 struct isl_upoly_rec *rec;
738 if (!up)
739 return NULL;
741 if (isl_upoly_is_cst(up))
742 return isl_upoly_cst_add_isl_int(up, v);
744 up = isl_upoly_cow(up);
745 rec = isl_upoly_as_rec(up);
746 if (!rec)
747 goto error;
749 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
750 if (!rec->p[0])
751 goto error;
753 return up;
754 error:
755 isl_upoly_free(up);
756 return NULL;
759 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
760 __isl_take struct isl_upoly *up, isl_int v)
762 struct isl_upoly_cst *cst;
764 if (isl_upoly_is_zero(up))
765 return up;
767 up = isl_upoly_cow(up);
768 if (!up)
769 return NULL;
771 cst = isl_upoly_as_cst(up);
773 isl_int_mul(cst->n, cst->n, v);
775 return up;
778 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
779 __isl_take struct isl_upoly *up, isl_int v)
781 int i;
782 struct isl_upoly_rec *rec;
784 if (!up)
785 return NULL;
787 if (isl_upoly_is_cst(up))
788 return isl_upoly_cst_mul_isl_int(up, v);
790 up = isl_upoly_cow(up);
791 rec = isl_upoly_as_rec(up);
792 if (!rec)
793 goto error;
795 for (i = 0; i < rec->n; ++i) {
796 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
797 if (!rec->p[i])
798 goto error;
801 return up;
802 error:
803 isl_upoly_free(up);
804 return NULL;
807 /* Multiply the constant polynomial "up" by "v".
809 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
810 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
812 struct isl_upoly_cst *cst;
814 if (isl_upoly_is_zero(up))
815 return up;
817 up = isl_upoly_cow(up);
818 if (!up)
819 return NULL;
821 cst = isl_upoly_as_cst(up);
823 isl_int_mul(cst->n, cst->n, v->n);
824 isl_int_mul(cst->d, cst->d, v->d);
825 isl_upoly_cst_reduce(cst);
827 return up;
830 /* Multiply the polynomial "up" by "v".
832 static __isl_give struct isl_upoly *isl_upoly_scale_val(
833 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
835 int i;
836 struct isl_upoly_rec *rec;
838 if (!up)
839 return NULL;
841 if (isl_upoly_is_cst(up))
842 return isl_upoly_cst_scale_val(up, v);
844 up = isl_upoly_cow(up);
845 rec = isl_upoly_as_rec(up);
846 if (!rec)
847 goto error;
849 for (i = 0; i < rec->n; ++i) {
850 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
851 if (!rec->p[i])
852 goto error;
855 return up;
856 error:
857 isl_upoly_free(up);
858 return NULL;
861 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
862 __isl_take struct isl_upoly *up2)
864 struct isl_upoly_cst *cst1;
865 struct isl_upoly_cst *cst2;
867 up1 = isl_upoly_cow(up1);
868 if (!up1 || !up2)
869 goto error;
871 cst1 = isl_upoly_as_cst(up1);
872 cst2 = isl_upoly_as_cst(up2);
874 isl_int_mul(cst1->n, cst1->n, cst2->n);
875 isl_int_mul(cst1->d, cst1->d, cst2->d);
877 isl_upoly_cst_reduce(cst1);
879 isl_upoly_free(up2);
880 return up1;
881 error:
882 isl_upoly_free(up1);
883 isl_upoly_free(up2);
884 return NULL;
887 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
888 __isl_take struct isl_upoly *up2)
890 struct isl_upoly_rec *rec1;
891 struct isl_upoly_rec *rec2;
892 struct isl_upoly_rec *res = NULL;
893 int i, j;
894 int size;
896 rec1 = isl_upoly_as_rec(up1);
897 rec2 = isl_upoly_as_rec(up2);
898 if (!rec1 || !rec2)
899 goto error;
900 size = rec1->n + rec2->n - 1;
901 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
902 if (!res)
903 goto error;
905 for (i = 0; i < rec1->n; ++i) {
906 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
907 isl_upoly_copy(rec1->p[i]));
908 if (!res->p[i])
909 goto error;
910 res->n++;
912 for (; i < size; ++i) {
913 res->p[i] = isl_upoly_zero(up1->ctx);
914 if (!res->p[i])
915 goto error;
916 res->n++;
918 for (i = 0; i < rec1->n; ++i) {
919 for (j = 1; j < rec2->n; ++j) {
920 struct isl_upoly *up;
921 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
922 isl_upoly_copy(rec1->p[i]));
923 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
924 if (!res->p[i + j])
925 goto error;
929 isl_upoly_free(up1);
930 isl_upoly_free(up2);
932 return &res->up;
933 error:
934 isl_upoly_free(up1);
935 isl_upoly_free(up2);
936 isl_upoly_free(&res->up);
937 return NULL;
940 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
941 __isl_take struct isl_upoly *up2)
943 if (!up1 || !up2)
944 goto error;
946 if (isl_upoly_is_nan(up1)) {
947 isl_upoly_free(up2);
948 return up1;
951 if (isl_upoly_is_nan(up2)) {
952 isl_upoly_free(up1);
953 return up2;
956 if (isl_upoly_is_zero(up1)) {
957 isl_upoly_free(up2);
958 return up1;
961 if (isl_upoly_is_zero(up2)) {
962 isl_upoly_free(up1);
963 return up2;
966 if (isl_upoly_is_one(up1)) {
967 isl_upoly_free(up1);
968 return up2;
971 if (isl_upoly_is_one(up2)) {
972 isl_upoly_free(up2);
973 return up1;
976 if (up1->var < up2->var)
977 return isl_upoly_mul(up2, up1);
979 if (up2->var < up1->var) {
980 int i;
981 struct isl_upoly_rec *rec;
982 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
983 isl_ctx *ctx = up1->ctx;
984 isl_upoly_free(up1);
985 isl_upoly_free(up2);
986 return isl_upoly_nan(ctx);
988 up1 = isl_upoly_cow(up1);
989 rec = isl_upoly_as_rec(up1);
990 if (!rec)
991 goto error;
993 for (i = 0; i < rec->n; ++i) {
994 rec->p[i] = isl_upoly_mul(rec->p[i],
995 isl_upoly_copy(up2));
996 if (!rec->p[i])
997 goto error;
999 isl_upoly_free(up2);
1000 return up1;
1003 if (isl_upoly_is_cst(up1))
1004 return isl_upoly_mul_cst(up1, up2);
1006 return isl_upoly_mul_rec(up1, up2);
1007 error:
1008 isl_upoly_free(up1);
1009 isl_upoly_free(up2);
1010 return NULL;
1013 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1014 unsigned power)
1016 struct isl_upoly *res;
1018 if (!up)
1019 return NULL;
1020 if (power == 1)
1021 return up;
1023 if (power % 2)
1024 res = isl_upoly_copy(up);
1025 else
1026 res = isl_upoly_one(up->ctx);
1028 while (power >>= 1) {
1029 up = isl_upoly_mul(up, isl_upoly_copy(up));
1030 if (power % 2)
1031 res = isl_upoly_mul(res, isl_upoly_copy(up));
1034 isl_upoly_free(up);
1035 return res;
1038 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
1039 unsigned n_div, __isl_take struct isl_upoly *up)
1041 struct isl_qpolynomial *qp = NULL;
1042 unsigned total;
1044 if (!dim || !up)
1045 goto error;
1047 if (!isl_space_is_set(dim))
1048 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1049 "domain of polynomial should be a set", goto error);
1051 total = isl_space_dim(dim, isl_dim_all);
1053 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
1054 if (!qp)
1055 goto error;
1057 qp->ref = 1;
1058 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
1059 if (!qp->div)
1060 goto error;
1062 qp->dim = dim;
1063 qp->upoly = up;
1065 return qp;
1066 error:
1067 isl_space_free(dim);
1068 isl_upoly_free(up);
1069 isl_qpolynomial_free(qp);
1070 return NULL;
1073 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1075 if (!qp)
1076 return NULL;
1078 qp->ref++;
1079 return qp;
1082 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1084 struct isl_qpolynomial *dup;
1086 if (!qp)
1087 return NULL;
1089 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1090 isl_upoly_copy(qp->upoly));
1091 if (!dup)
1092 return NULL;
1093 isl_mat_free(dup->div);
1094 dup->div = isl_mat_copy(qp->div);
1095 if (!dup->div)
1096 goto error;
1098 return dup;
1099 error:
1100 isl_qpolynomial_free(dup);
1101 return NULL;
1104 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1106 if (!qp)
1107 return NULL;
1109 if (qp->ref == 1)
1110 return qp;
1111 qp->ref--;
1112 return isl_qpolynomial_dup(qp);
1115 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1116 __isl_take isl_qpolynomial *qp)
1118 if (!qp)
1119 return NULL;
1121 if (--qp->ref > 0)
1122 return NULL;
1124 isl_space_free(qp->dim);
1125 isl_mat_free(qp->div);
1126 isl_upoly_free(qp->upoly);
1128 free(qp);
1129 return NULL;
1132 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1134 int i;
1135 struct isl_upoly_rec *rec;
1136 struct isl_upoly_cst *cst;
1138 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1139 if (!rec)
1140 return NULL;
1141 for (i = 0; i < 1 + power; ++i) {
1142 rec->p[i] = isl_upoly_zero(ctx);
1143 if (!rec->p[i])
1144 goto error;
1145 rec->n++;
1147 cst = isl_upoly_as_cst(rec->p[power]);
1148 isl_int_set_si(cst->n, 1);
1150 return &rec->up;
1151 error:
1152 isl_upoly_free(&rec->up);
1153 return NULL;
1156 /* r array maps original positions to new positions.
1158 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1159 int *r)
1161 int i;
1162 struct isl_upoly_rec *rec;
1163 struct isl_upoly *base;
1164 struct isl_upoly *res;
1166 if (isl_upoly_is_cst(up))
1167 return up;
1169 rec = isl_upoly_as_rec(up);
1170 if (!rec)
1171 goto error;
1173 isl_assert(up->ctx, rec->n >= 1, goto error);
1175 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1176 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1178 for (i = rec->n - 2; i >= 0; --i) {
1179 res = isl_upoly_mul(res, isl_upoly_copy(base));
1180 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1183 isl_upoly_free(base);
1184 isl_upoly_free(up);
1186 return res;
1187 error:
1188 isl_upoly_free(up);
1189 return NULL;
1192 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1194 int n_row, n_col;
1195 int equal;
1197 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1198 div1->n_col >= div2->n_col, return -1);
1200 if (div1->n_row == div2->n_row)
1201 return isl_mat_is_equal(div1, div2);
1203 n_row = div1->n_row;
1204 n_col = div1->n_col;
1205 div1->n_row = div2->n_row;
1206 div1->n_col = div2->n_col;
1208 equal = isl_mat_is_equal(div1, div2);
1210 div1->n_row = n_row;
1211 div1->n_col = n_col;
1213 return equal;
1216 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1218 int li, lj;
1220 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1221 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1223 if (li != lj)
1224 return li - lj;
1226 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1229 struct isl_div_sort_info {
1230 isl_mat *div;
1231 int row;
1234 static int div_sort_cmp(const void *p1, const void *p2)
1236 const struct isl_div_sort_info *i1, *i2;
1237 i1 = (const struct isl_div_sort_info *) p1;
1238 i2 = (const struct isl_div_sort_info *) p2;
1240 return cmp_row(i1->div, i1->row, i2->row);
1243 /* Sort divs and remove duplicates.
1245 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1247 int i;
1248 int skip;
1249 int len;
1250 struct isl_div_sort_info *array = NULL;
1251 int *pos = NULL, *at = NULL;
1252 int *reordering = NULL;
1253 unsigned div_pos;
1255 if (!qp)
1256 return NULL;
1257 if (qp->div->n_row <= 1)
1258 return qp;
1260 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1262 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1263 qp->div->n_row);
1264 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1265 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1266 len = qp->div->n_col - 2;
1267 reordering = isl_alloc_array(qp->div->ctx, int, len);
1268 if (!array || !pos || !at || !reordering)
1269 goto error;
1271 for (i = 0; i < qp->div->n_row; ++i) {
1272 array[i].div = qp->div;
1273 array[i].row = i;
1274 pos[i] = i;
1275 at[i] = i;
1278 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1279 div_sort_cmp);
1281 for (i = 0; i < div_pos; ++i)
1282 reordering[i] = i;
1284 for (i = 0; i < qp->div->n_row; ++i) {
1285 if (pos[array[i].row] == i)
1286 continue;
1287 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1288 pos[at[i]] = pos[array[i].row];
1289 at[pos[array[i].row]] = at[i];
1290 at[i] = array[i].row;
1291 pos[array[i].row] = i;
1294 skip = 0;
1295 for (i = 0; i < len - div_pos; ++i) {
1296 if (i > 0 &&
1297 isl_seq_eq(qp->div->row[i - skip - 1],
1298 qp->div->row[i - skip], qp->div->n_col)) {
1299 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1300 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1301 2 + div_pos + i - skip);
1302 qp->div = isl_mat_drop_cols(qp->div,
1303 2 + div_pos + i - skip, 1);
1304 skip++;
1306 reordering[div_pos + array[i].row] = div_pos + i - skip;
1309 qp->upoly = reorder(qp->upoly, reordering);
1311 if (!qp->upoly || !qp->div)
1312 goto error;
1314 free(at);
1315 free(pos);
1316 free(array);
1317 free(reordering);
1319 return qp;
1320 error:
1321 free(at);
1322 free(pos);
1323 free(array);
1324 free(reordering);
1325 isl_qpolynomial_free(qp);
1326 return NULL;
1329 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1330 int *exp, int first)
1332 int i;
1333 struct isl_upoly_rec *rec;
1335 if (isl_upoly_is_cst(up))
1336 return up;
1338 if (up->var < first)
1339 return up;
1341 if (exp[up->var - first] == up->var - first)
1342 return up;
1344 up = isl_upoly_cow(up);
1345 if (!up)
1346 goto error;
1348 up->var = exp[up->var - first] + first;
1350 rec = isl_upoly_as_rec(up);
1351 if (!rec)
1352 goto error;
1354 for (i = 0; i < rec->n; ++i) {
1355 rec->p[i] = expand(rec->p[i], exp, first);
1356 if (!rec->p[i])
1357 goto error;
1360 return up;
1361 error:
1362 isl_upoly_free(up);
1363 return NULL;
1366 static __isl_give isl_qpolynomial *with_merged_divs(
1367 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1368 __isl_take isl_qpolynomial *qp2),
1369 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1371 int *exp1 = NULL;
1372 int *exp2 = NULL;
1373 isl_mat *div = NULL;
1374 int n_div1, n_div2;
1376 qp1 = isl_qpolynomial_cow(qp1);
1377 qp2 = isl_qpolynomial_cow(qp2);
1379 if (!qp1 || !qp2)
1380 goto error;
1382 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1383 qp1->div->n_col >= qp2->div->n_col, goto error);
1385 n_div1 = qp1->div->n_row;
1386 n_div2 = qp2->div->n_row;
1387 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1388 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1389 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1390 goto error;
1392 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1393 if (!div)
1394 goto error;
1396 isl_mat_free(qp1->div);
1397 qp1->div = isl_mat_copy(div);
1398 isl_mat_free(qp2->div);
1399 qp2->div = isl_mat_copy(div);
1401 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1402 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1404 if (!qp1->upoly || !qp2->upoly)
1405 goto error;
1407 isl_mat_free(div);
1408 free(exp1);
1409 free(exp2);
1411 return fn(qp1, qp2);
1412 error:
1413 isl_mat_free(div);
1414 free(exp1);
1415 free(exp2);
1416 isl_qpolynomial_free(qp1);
1417 isl_qpolynomial_free(qp2);
1418 return NULL;
1421 __isl_give isl_qpolynomial *isl_qpolynomial_add(__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_add(qp2, qp1);
1432 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1433 if (!compatible_divs(qp1->div, qp2->div))
1434 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1436 qp1->upoly = isl_upoly_sum(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_add_on_domain(
1450 __isl_keep isl_set *dom,
1451 __isl_take isl_qpolynomial *qp1,
1452 __isl_take isl_qpolynomial *qp2)
1454 qp1 = isl_qpolynomial_add(qp1, qp2);
1455 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1456 return qp1;
1459 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1460 __isl_take isl_qpolynomial *qp2)
1462 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1465 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1466 __isl_take isl_qpolynomial *qp, isl_int v)
1468 if (isl_int_is_zero(v))
1469 return qp;
1471 qp = isl_qpolynomial_cow(qp);
1472 if (!qp)
1473 return NULL;
1475 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1476 if (!qp->upoly)
1477 goto error;
1479 return qp;
1480 error:
1481 isl_qpolynomial_free(qp);
1482 return NULL;
1486 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1488 if (!qp)
1489 return NULL;
1491 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1494 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1495 __isl_take isl_qpolynomial *qp, isl_int v)
1497 if (isl_int_is_one(v))
1498 return qp;
1500 if (qp && isl_int_is_zero(v)) {
1501 isl_qpolynomial *zero;
1502 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1503 isl_qpolynomial_free(qp);
1504 return zero;
1507 qp = isl_qpolynomial_cow(qp);
1508 if (!qp)
1509 return NULL;
1511 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1512 if (!qp->upoly)
1513 goto error;
1515 return qp;
1516 error:
1517 isl_qpolynomial_free(qp);
1518 return NULL;
1521 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1522 __isl_take isl_qpolynomial *qp, isl_int v)
1524 return isl_qpolynomial_mul_isl_int(qp, v);
1527 /* Multiply "qp" by "v".
1529 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1530 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1532 if (!qp || !v)
1533 goto error;
1535 if (!isl_val_is_rat(v))
1536 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1537 "expecting rational factor", goto error);
1539 if (isl_val_is_one(v)) {
1540 isl_val_free(v);
1541 return qp;
1544 if (isl_val_is_zero(v)) {
1545 isl_space *space;
1547 space = isl_qpolynomial_get_domain_space(qp);
1548 isl_qpolynomial_free(qp);
1549 isl_val_free(v);
1550 return isl_qpolynomial_zero_on_domain(space);
1553 qp = isl_qpolynomial_cow(qp);
1554 if (!qp)
1555 goto error;
1557 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1558 if (!qp->upoly)
1559 qp = isl_qpolynomial_free(qp);
1561 isl_val_free(v);
1562 return qp;
1563 error:
1564 isl_val_free(v);
1565 isl_qpolynomial_free(qp);
1566 return NULL;
1569 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1570 __isl_take isl_qpolynomial *qp2)
1572 qp1 = isl_qpolynomial_cow(qp1);
1574 if (!qp1 || !qp2)
1575 goto error;
1577 if (qp1->div->n_row < qp2->div->n_row)
1578 return isl_qpolynomial_mul(qp2, qp1);
1580 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1581 if (!compatible_divs(qp1->div, qp2->div))
1582 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1584 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1585 if (!qp1->upoly)
1586 goto error;
1588 isl_qpolynomial_free(qp2);
1590 return qp1;
1591 error:
1592 isl_qpolynomial_free(qp1);
1593 isl_qpolynomial_free(qp2);
1594 return NULL;
1597 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1598 unsigned power)
1600 qp = isl_qpolynomial_cow(qp);
1602 if (!qp)
1603 return NULL;
1605 qp->upoly = isl_upoly_pow(qp->upoly, power);
1606 if (!qp->upoly)
1607 goto error;
1609 return qp;
1610 error:
1611 isl_qpolynomial_free(qp);
1612 return NULL;
1615 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1616 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1618 int i;
1620 if (power == 1)
1621 return pwqp;
1623 pwqp = isl_pw_qpolynomial_cow(pwqp);
1624 if (!pwqp)
1625 return NULL;
1627 for (i = 0; i < pwqp->n; ++i) {
1628 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1629 if (!pwqp->p[i].qp)
1630 return isl_pw_qpolynomial_free(pwqp);
1633 return pwqp;
1636 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1637 __isl_take isl_space *dim)
1639 if (!dim)
1640 return NULL;
1641 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1644 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1645 __isl_take isl_space *dim)
1647 if (!dim)
1648 return NULL;
1649 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1652 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1653 __isl_take isl_space *dim)
1655 if (!dim)
1656 return NULL;
1657 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1660 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1661 __isl_take isl_space *dim)
1663 if (!dim)
1664 return NULL;
1665 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1668 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1669 __isl_take isl_space *dim)
1671 if (!dim)
1672 return NULL;
1673 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1676 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1677 __isl_take isl_space *dim,
1678 isl_int v)
1680 struct isl_qpolynomial *qp;
1681 struct isl_upoly_cst *cst;
1683 if (!dim)
1684 return NULL;
1686 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1687 if (!qp)
1688 return NULL;
1690 cst = isl_upoly_as_cst(qp->upoly);
1691 isl_int_set(cst->n, v);
1693 return qp;
1696 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1697 isl_int *n, isl_int *d)
1699 struct isl_upoly_cst *cst;
1701 if (!qp)
1702 return -1;
1704 if (!isl_upoly_is_cst(qp->upoly))
1705 return 0;
1707 cst = isl_upoly_as_cst(qp->upoly);
1708 if (!cst)
1709 return -1;
1711 if (n)
1712 isl_int_set(*n, cst->n);
1713 if (d)
1714 isl_int_set(*d, cst->d);
1716 return 1;
1719 /* Return the constant term of "up".
1721 static __isl_give isl_val *isl_upoly_get_constant_val(
1722 __isl_keep struct isl_upoly *up)
1724 struct isl_upoly_cst *cst;
1726 if (!up)
1727 return NULL;
1729 while (!isl_upoly_is_cst(up)) {
1730 struct isl_upoly_rec *rec;
1732 rec = isl_upoly_as_rec(up);
1733 if (!rec)
1734 return NULL;
1735 up = rec->p[0];
1738 cst = isl_upoly_as_cst(up);
1739 if (!cst)
1740 return NULL;
1741 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1744 /* Return the constant term of "qp".
1746 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1747 __isl_keep isl_qpolynomial *qp)
1749 if (!qp)
1750 return NULL;
1752 return isl_upoly_get_constant_val(qp->upoly);
1755 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1757 int is_cst;
1758 struct isl_upoly_rec *rec;
1760 if (!up)
1761 return -1;
1763 if (up->var < 0)
1764 return 1;
1766 rec = isl_upoly_as_rec(up);
1767 if (!rec)
1768 return -1;
1770 if (rec->n > 2)
1771 return 0;
1773 isl_assert(up->ctx, rec->n > 1, return -1);
1775 is_cst = isl_upoly_is_cst(rec->p[1]);
1776 if (is_cst < 0)
1777 return -1;
1778 if (!is_cst)
1779 return 0;
1781 return isl_upoly_is_affine(rec->p[0]);
1784 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1786 if (!qp)
1787 return -1;
1789 if (qp->div->n_row > 0)
1790 return 0;
1792 return isl_upoly_is_affine(qp->upoly);
1795 static void update_coeff(__isl_keep isl_vec *aff,
1796 __isl_keep struct isl_upoly_cst *cst, int pos)
1798 isl_int gcd;
1799 isl_int f;
1801 if (isl_int_is_zero(cst->n))
1802 return;
1804 isl_int_init(gcd);
1805 isl_int_init(f);
1806 isl_int_gcd(gcd, cst->d, aff->el[0]);
1807 isl_int_divexact(f, cst->d, gcd);
1808 isl_int_divexact(gcd, aff->el[0], gcd);
1809 isl_seq_scale(aff->el, aff->el, f, aff->size);
1810 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1811 isl_int_clear(gcd);
1812 isl_int_clear(f);
1815 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1816 __isl_keep isl_vec *aff)
1818 struct isl_upoly_cst *cst;
1819 struct isl_upoly_rec *rec;
1821 if (!up || !aff)
1822 return -1;
1824 if (up->var < 0) {
1825 struct isl_upoly_cst *cst;
1827 cst = isl_upoly_as_cst(up);
1828 if (!cst)
1829 return -1;
1830 update_coeff(aff, cst, 0);
1831 return 0;
1834 rec = isl_upoly_as_rec(up);
1835 if (!rec)
1836 return -1;
1837 isl_assert(up->ctx, rec->n == 2, return -1);
1839 cst = isl_upoly_as_cst(rec->p[1]);
1840 if (!cst)
1841 return -1;
1842 update_coeff(aff, cst, 1 + up->var);
1844 return isl_upoly_update_affine(rec->p[0], aff);
1847 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1848 __isl_keep isl_qpolynomial *qp)
1850 isl_vec *aff;
1851 unsigned d;
1853 if (!qp)
1854 return NULL;
1856 d = isl_space_dim(qp->dim, isl_dim_all);
1857 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1858 if (!aff)
1859 return NULL;
1861 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1862 isl_int_set_si(aff->el[0], 1);
1864 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1865 goto error;
1867 return aff;
1868 error:
1869 isl_vec_free(aff);
1870 return NULL;
1873 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
1874 __isl_keep isl_qpolynomial *qp2)
1876 int equal;
1878 if (!qp1 || !qp2)
1879 return -1;
1881 equal = isl_space_is_equal(qp1->dim, qp2->dim);
1882 if (equal < 0 || !equal)
1883 return equal;
1885 equal = isl_mat_is_equal(qp1->div, qp2->div);
1886 if (equal < 0 || !equal)
1887 return equal;
1889 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1892 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1894 int i;
1895 struct isl_upoly_rec *rec;
1897 if (isl_upoly_is_cst(up)) {
1898 struct isl_upoly_cst *cst;
1899 cst = isl_upoly_as_cst(up);
1900 if (!cst)
1901 return;
1902 isl_int_lcm(*d, *d, cst->d);
1903 return;
1906 rec = isl_upoly_as_rec(up);
1907 if (!rec)
1908 return;
1910 for (i = 0; i < rec->n; ++i)
1911 upoly_update_den(rec->p[i], d);
1914 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1916 isl_int_set_si(*d, 1);
1917 if (!qp)
1918 return;
1919 upoly_update_den(qp->upoly, d);
1922 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
1923 __isl_take isl_space *dim, int pos, int power)
1925 struct isl_ctx *ctx;
1927 if (!dim)
1928 return NULL;
1930 ctx = dim->ctx;
1932 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1935 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(__isl_take isl_space *dim,
1936 enum isl_dim_type type, unsigned pos)
1938 if (!dim)
1939 return NULL;
1941 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
1942 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
1944 if (type == isl_dim_set)
1945 pos += isl_space_dim(dim, isl_dim_param);
1947 return isl_qpolynomial_var_pow_on_domain(dim, pos, 1);
1948 error:
1949 isl_space_free(dim);
1950 return NULL;
1953 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1954 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1956 int i;
1957 struct isl_upoly_rec *rec;
1958 struct isl_upoly *base, *res;
1960 if (!up)
1961 return NULL;
1963 if (isl_upoly_is_cst(up))
1964 return up;
1966 if (up->var < first)
1967 return up;
1969 rec = isl_upoly_as_rec(up);
1970 if (!rec)
1971 goto error;
1973 isl_assert(up->ctx, rec->n >= 1, goto error);
1975 if (up->var >= first + n)
1976 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1977 else
1978 base = isl_upoly_copy(subs[up->var - first]);
1980 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1981 for (i = rec->n - 2; i >= 0; --i) {
1982 struct isl_upoly *t;
1983 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1984 res = isl_upoly_mul(res, isl_upoly_copy(base));
1985 res = isl_upoly_sum(res, t);
1988 isl_upoly_free(base);
1989 isl_upoly_free(up);
1991 return res;
1992 error:
1993 isl_upoly_free(up);
1994 return NULL;
1997 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1998 isl_int denom, unsigned len)
2000 int i;
2001 struct isl_upoly *up;
2003 isl_assert(ctx, len >= 1, return NULL);
2005 up = isl_upoly_rat_cst(ctx, f[0], denom);
2006 for (i = 0; i < len - 1; ++i) {
2007 struct isl_upoly *t;
2008 struct isl_upoly *c;
2010 if (isl_int_is_zero(f[1 + i]))
2011 continue;
2013 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2014 t = isl_upoly_var_pow(ctx, i, 1);
2015 t = isl_upoly_mul(c, t);
2016 up = isl_upoly_sum(up, t);
2019 return up;
2022 /* Remove common factor of non-constant terms and denominator.
2024 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2026 isl_ctx *ctx = qp->div->ctx;
2027 unsigned total = qp->div->n_col - 2;
2029 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2030 isl_int_gcd(ctx->normalize_gcd,
2031 ctx->normalize_gcd, qp->div->row[div][0]);
2032 if (isl_int_is_one(ctx->normalize_gcd))
2033 return;
2035 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2036 ctx->normalize_gcd, total);
2037 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2038 ctx->normalize_gcd);
2039 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2040 ctx->normalize_gcd);
2043 /* Replace the integer division identified by "div" by the polynomial "s".
2044 * The integer division is assumed not to appear in the definition
2045 * of any other integer divisions.
2047 static __isl_give isl_qpolynomial *substitute_div(
2048 __isl_take isl_qpolynomial *qp,
2049 int div, __isl_take struct isl_upoly *s)
2051 int i;
2052 int total;
2053 int *reordering;
2055 if (!qp || !s)
2056 goto error;
2058 qp = isl_qpolynomial_cow(qp);
2059 if (!qp)
2060 goto error;
2062 total = isl_space_dim(qp->dim, isl_dim_all);
2063 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2064 if (!qp->upoly)
2065 goto error;
2067 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2068 if (!reordering)
2069 goto error;
2070 for (i = 0; i < total + div; ++i)
2071 reordering[i] = i;
2072 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2073 reordering[i] = i - 1;
2074 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2075 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2076 qp->upoly = reorder(qp->upoly, reordering);
2077 free(reordering);
2079 if (!qp->upoly || !qp->div)
2080 goto error;
2082 isl_upoly_free(s);
2083 return qp;
2084 error:
2085 isl_qpolynomial_free(qp);
2086 isl_upoly_free(s);
2087 return NULL;
2090 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2091 * divisions because d is equal to 1 by their definition, i.e., e.
2093 static __isl_give isl_qpolynomial *substitute_non_divs(
2094 __isl_take isl_qpolynomial *qp)
2096 int i, j;
2097 int total;
2098 struct isl_upoly *s;
2100 if (!qp)
2101 return NULL;
2103 total = isl_space_dim(qp->dim, isl_dim_all);
2104 for (i = 0; qp && i < qp->div->n_row; ++i) {
2105 if (!isl_int_is_one(qp->div->row[i][0]))
2106 continue;
2107 for (j = i + 1; j < qp->div->n_row; ++j) {
2108 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2109 continue;
2110 isl_seq_combine(qp->div->row[j] + 1,
2111 qp->div->ctx->one, qp->div->row[j] + 1,
2112 qp->div->row[j][2 + total + i],
2113 qp->div->row[i] + 1, 1 + total + i);
2114 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2115 normalize_div(qp, j);
2117 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2118 qp->div->row[i][0], qp->div->n_col - 1);
2119 qp = substitute_div(qp, i, s);
2120 --i;
2123 return qp;
2126 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2127 * with d the denominator. When replacing the coefficient e of x by
2128 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2129 * inside the division, so we need to add floor(e/d) * x outside.
2130 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2131 * to adjust the coefficient of x in each later div that depends on the
2132 * current div "div" and also in the affine expression "aff"
2133 * (if it too depends on "div").
2135 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2136 __isl_keep isl_vec *aff)
2138 int i, j;
2139 isl_int v;
2140 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2142 isl_int_init(v);
2143 for (i = 0; i < 1 + total + div; ++i) {
2144 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2145 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2146 continue;
2147 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2148 isl_int_fdiv_r(qp->div->row[div][1 + i],
2149 qp->div->row[div][1 + i], qp->div->row[div][0]);
2150 if (!isl_int_is_zero(aff->el[1 + total + div]))
2151 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
2152 for (j = div + 1; j < qp->div->n_row; ++j) {
2153 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2154 continue;
2155 isl_int_addmul(qp->div->row[j][1 + i],
2156 v, qp->div->row[j][2 + total + div]);
2159 isl_int_clear(v);
2162 /* Check if the last non-zero coefficient is bigger that half of the
2163 * denominator. If so, we will invert the div to further reduce the number
2164 * of distinct divs that may appear.
2165 * If the last non-zero coefficient is exactly half the denominator,
2166 * then we continue looking for earlier coefficients that are bigger
2167 * than half the denominator.
2169 static int needs_invert(__isl_keep isl_mat *div, int row)
2171 int i;
2172 int cmp;
2174 for (i = div->n_col - 1; i >= 1; --i) {
2175 if (isl_int_is_zero(div->row[row][i]))
2176 continue;
2177 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2178 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2179 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2180 if (cmp)
2181 return cmp > 0;
2182 if (i == 1)
2183 return 1;
2186 return 0;
2189 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2190 * We only invert the coefficients of e (and the coefficient of q in
2191 * later divs and in "aff"). After calling this function, the
2192 * coefficients of e should be reduced again.
2194 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2195 __isl_keep isl_vec *aff)
2197 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2199 isl_seq_neg(qp->div->row[div] + 1,
2200 qp->div->row[div] + 1, qp->div->n_col - 1);
2201 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2202 isl_int_add(qp->div->row[div][1],
2203 qp->div->row[div][1], qp->div->row[div][0]);
2204 if (!isl_int_is_zero(aff->el[1 + total + div]))
2205 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2206 isl_mat_col_mul(qp->div, 2 + total + div,
2207 qp->div->ctx->negone, 2 + total + div);
2210 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2211 * in the interval [0, d-1], with d the denominator and such that the
2212 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2214 * After the reduction, some divs may have become redundant or identical,
2215 * so we call substitute_non_divs and sort_divs. If these functions
2216 * eliminate divs or merge two or more divs into one, the coefficients
2217 * of the enclosing divs may have to be reduced again, so we call
2218 * ourselves recursively if the number of divs decreases.
2220 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2222 int i;
2223 isl_vec *aff = NULL;
2224 struct isl_upoly *s;
2225 unsigned n_div;
2227 if (!qp)
2228 return NULL;
2230 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2231 aff = isl_vec_clr(aff);
2232 if (!aff)
2233 goto error;
2235 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2237 for (i = 0; i < qp->div->n_row; ++i) {
2238 normalize_div(qp, i);
2239 reduce_div(qp, i, aff);
2240 if (needs_invert(qp->div, i)) {
2241 invert_div(qp, i, aff);
2242 reduce_div(qp, i, aff);
2246 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2247 qp->div->ctx->one, aff->size);
2248 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2249 isl_upoly_free(s);
2250 if (!qp->upoly)
2251 goto error;
2253 isl_vec_free(aff);
2255 n_div = qp->div->n_row;
2256 qp = substitute_non_divs(qp);
2257 qp = sort_divs(qp);
2258 if (qp && qp->div->n_row < n_div)
2259 return reduce_divs(qp);
2261 return qp;
2262 error:
2263 isl_qpolynomial_free(qp);
2264 isl_vec_free(aff);
2265 return NULL;
2268 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2269 __isl_take isl_space *dim, const isl_int n, const isl_int d)
2271 struct isl_qpolynomial *qp;
2272 struct isl_upoly_cst *cst;
2274 if (!dim)
2275 return NULL;
2277 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2278 if (!qp)
2279 return NULL;
2281 cst = isl_upoly_as_cst(qp->upoly);
2282 isl_int_set(cst->n, n);
2283 isl_int_set(cst->d, d);
2285 return qp;
2288 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2290 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2291 __isl_take isl_space *domain, __isl_take isl_val *val)
2293 isl_qpolynomial *qp;
2294 struct isl_upoly_cst *cst;
2296 if (!domain || !val)
2297 goto error;
2299 qp = isl_qpolynomial_alloc(isl_space_copy(domain), 0,
2300 isl_upoly_zero(domain->ctx));
2301 if (!qp)
2302 goto error;
2304 cst = isl_upoly_as_cst(qp->upoly);
2305 isl_int_set(cst->n, val->n);
2306 isl_int_set(cst->d, val->d);
2308 isl_space_free(domain);
2309 isl_val_free(val);
2310 return qp;
2311 error:
2312 isl_space_free(domain);
2313 isl_val_free(val);
2314 return NULL;
2317 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2319 struct isl_upoly_rec *rec;
2320 int i;
2322 if (!up)
2323 return -1;
2325 if (isl_upoly_is_cst(up))
2326 return 0;
2328 if (up->var < d)
2329 active[up->var] = 1;
2331 rec = isl_upoly_as_rec(up);
2332 for (i = 0; i < rec->n; ++i)
2333 if (up_set_active(rec->p[i], active, d) < 0)
2334 return -1;
2336 return 0;
2339 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2341 int i, j;
2342 int d = isl_space_dim(qp->dim, isl_dim_all);
2344 if (!qp || !active)
2345 return -1;
2347 for (i = 0; i < d; ++i)
2348 for (j = 0; j < qp->div->n_row; ++j) {
2349 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2350 continue;
2351 active[i] = 1;
2352 break;
2355 return up_set_active(qp->upoly, active, d);
2358 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2359 enum isl_dim_type type, unsigned first, unsigned n)
2361 int i;
2362 int *active = NULL;
2363 int involves = 0;
2365 if (!qp)
2366 return -1;
2367 if (n == 0)
2368 return 0;
2370 isl_assert(qp->dim->ctx,
2371 first + n <= isl_qpolynomial_dim(qp, type), return -1);
2372 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2373 type == isl_dim_in, return -1);
2375 active = isl_calloc_array(qp->dim->ctx, int,
2376 isl_space_dim(qp->dim, isl_dim_all));
2377 if (set_active(qp, active) < 0)
2378 goto error;
2380 if (type == isl_dim_in)
2381 first += isl_space_dim(qp->dim, isl_dim_param);
2382 for (i = 0; i < n; ++i)
2383 if (active[first + i]) {
2384 involves = 1;
2385 break;
2388 free(active);
2390 return involves;
2391 error:
2392 free(active);
2393 return -1;
2396 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2397 * of the divs that do appear in the quasi-polynomial.
2399 static __isl_give isl_qpolynomial *remove_redundant_divs(
2400 __isl_take isl_qpolynomial *qp)
2402 int i, j;
2403 int d;
2404 int len;
2405 int skip;
2406 int *active = NULL;
2407 int *reordering = NULL;
2408 int redundant = 0;
2409 int n_div;
2410 isl_ctx *ctx;
2412 if (!qp)
2413 return NULL;
2414 if (qp->div->n_row == 0)
2415 return qp;
2417 d = isl_space_dim(qp->dim, isl_dim_all);
2418 len = qp->div->n_col - 2;
2419 ctx = isl_qpolynomial_get_ctx(qp);
2420 active = isl_calloc_array(ctx, int, len);
2421 if (!active)
2422 goto error;
2424 if (up_set_active(qp->upoly, active, len) < 0)
2425 goto error;
2427 for (i = qp->div->n_row - 1; i >= 0; --i) {
2428 if (!active[d + i]) {
2429 redundant = 1;
2430 continue;
2432 for (j = 0; j < i; ++j) {
2433 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2434 continue;
2435 active[d + j] = 1;
2436 break;
2440 if (!redundant) {
2441 free(active);
2442 return qp;
2445 reordering = isl_alloc_array(qp->div->ctx, int, len);
2446 if (!reordering)
2447 goto error;
2449 for (i = 0; i < d; ++i)
2450 reordering[i] = i;
2452 skip = 0;
2453 n_div = qp->div->n_row;
2454 for (i = 0; i < n_div; ++i) {
2455 if (!active[d + i]) {
2456 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2457 qp->div = isl_mat_drop_cols(qp->div,
2458 2 + d + i - skip, 1);
2459 skip++;
2461 reordering[d + i] = d + i - skip;
2464 qp->upoly = reorder(qp->upoly, reordering);
2466 if (!qp->upoly || !qp->div)
2467 goto error;
2469 free(active);
2470 free(reordering);
2472 return qp;
2473 error:
2474 free(active);
2475 free(reordering);
2476 isl_qpolynomial_free(qp);
2477 return NULL;
2480 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2481 unsigned first, unsigned n)
2483 int i;
2484 struct isl_upoly_rec *rec;
2486 if (!up)
2487 return NULL;
2488 if (n == 0 || up->var < 0 || up->var < first)
2489 return up;
2490 if (up->var < first + n) {
2491 up = replace_by_constant_term(up);
2492 return isl_upoly_drop(up, first, n);
2494 up = isl_upoly_cow(up);
2495 if (!up)
2496 return NULL;
2497 up->var -= n;
2498 rec = isl_upoly_as_rec(up);
2499 if (!rec)
2500 goto error;
2502 for (i = 0; i < rec->n; ++i) {
2503 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2504 if (!rec->p[i])
2505 goto error;
2508 return up;
2509 error:
2510 isl_upoly_free(up);
2511 return NULL;
2514 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2515 __isl_take isl_qpolynomial *qp,
2516 enum isl_dim_type type, unsigned pos, const char *s)
2518 qp = isl_qpolynomial_cow(qp);
2519 if (!qp)
2520 return NULL;
2521 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2522 if (!qp->dim)
2523 goto error;
2524 return qp;
2525 error:
2526 isl_qpolynomial_free(qp);
2527 return NULL;
2530 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2531 __isl_take isl_qpolynomial *qp,
2532 enum isl_dim_type type, unsigned first, unsigned n)
2534 if (!qp)
2535 return NULL;
2536 if (type == isl_dim_out)
2537 isl_die(qp->dim->ctx, isl_error_invalid,
2538 "cannot drop output/set dimension",
2539 goto error);
2540 if (type == isl_dim_in)
2541 type = isl_dim_set;
2542 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2543 return qp;
2545 qp = isl_qpolynomial_cow(qp);
2546 if (!qp)
2547 return NULL;
2549 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2550 goto error);
2551 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2552 type == isl_dim_set, goto error);
2554 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2555 if (!qp->dim)
2556 goto error;
2558 if (type == isl_dim_set)
2559 first += isl_space_dim(qp->dim, isl_dim_param);
2561 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2562 if (!qp->div)
2563 goto error;
2565 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2566 if (!qp->upoly)
2567 goto error;
2569 return qp;
2570 error:
2571 isl_qpolynomial_free(qp);
2572 return NULL;
2575 /* Project the domain of the quasi-polynomial onto its parameter space.
2576 * The quasi-polynomial may not involve any of the domain dimensions.
2578 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2579 __isl_take isl_qpolynomial *qp)
2581 isl_space *space;
2582 unsigned n;
2583 int involves;
2585 n = isl_qpolynomial_dim(qp, isl_dim_in);
2586 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2587 if (involves < 0)
2588 return isl_qpolynomial_free(qp);
2589 if (involves)
2590 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2591 "polynomial involves some of the domain dimensions",
2592 return isl_qpolynomial_free(qp));
2593 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2594 space = isl_qpolynomial_get_domain_space(qp);
2595 space = isl_space_params(space);
2596 qp = isl_qpolynomial_reset_domain_space(qp, space);
2597 return qp;
2600 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2601 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2603 int i, j, k;
2604 isl_int denom;
2605 unsigned total;
2606 unsigned n_div;
2607 struct isl_upoly *up;
2609 if (!eq)
2610 goto error;
2611 if (eq->n_eq == 0) {
2612 isl_basic_set_free(eq);
2613 return qp;
2616 qp = isl_qpolynomial_cow(qp);
2617 if (!qp)
2618 goto error;
2619 qp->div = isl_mat_cow(qp->div);
2620 if (!qp->div)
2621 goto error;
2623 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2624 n_div = eq->n_div;
2625 isl_int_init(denom);
2626 for (i = 0; i < eq->n_eq; ++i) {
2627 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2628 if (j < 0 || j == 0 || j >= total)
2629 continue;
2631 for (k = 0; k < qp->div->n_row; ++k) {
2632 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2633 continue;
2634 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2635 &qp->div->row[k][0]);
2636 normalize_div(qp, k);
2639 if (isl_int_is_pos(eq->eq[i][j]))
2640 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2641 isl_int_abs(denom, eq->eq[i][j]);
2642 isl_int_set_si(eq->eq[i][j], 0);
2644 up = isl_upoly_from_affine(qp->dim->ctx,
2645 eq->eq[i], denom, total);
2646 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2647 isl_upoly_free(up);
2649 isl_int_clear(denom);
2651 if (!qp->upoly)
2652 goto error;
2654 isl_basic_set_free(eq);
2656 qp = substitute_non_divs(qp);
2657 qp = sort_divs(qp);
2659 return qp;
2660 error:
2661 isl_basic_set_free(eq);
2662 isl_qpolynomial_free(qp);
2663 return NULL;
2666 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2668 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2669 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2671 if (!qp || !eq)
2672 goto error;
2673 if (qp->div->n_row > 0)
2674 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2675 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2676 error:
2677 isl_basic_set_free(eq);
2678 isl_qpolynomial_free(qp);
2679 return NULL;
2682 static __isl_give isl_basic_set *add_div_constraints(
2683 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2685 int i;
2686 unsigned total;
2688 if (!bset || !div)
2689 goto error;
2691 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2692 if (!bset)
2693 goto error;
2694 total = isl_basic_set_total_dim(bset);
2695 for (i = 0; i < div->n_row; ++i)
2696 if (isl_basic_set_add_div_constraints_var(bset,
2697 total - div->n_row + i, div->row[i]) < 0)
2698 goto error;
2700 isl_mat_free(div);
2701 return bset;
2702 error:
2703 isl_mat_free(div);
2704 isl_basic_set_free(bset);
2705 return NULL;
2708 /* Look for equalities among the variables shared by context and qp
2709 * and the integer divisions of qp, if any.
2710 * The equalities are then used to eliminate variables and/or integer
2711 * divisions from qp.
2713 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2714 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2716 isl_basic_set *aff;
2718 if (!qp)
2719 goto error;
2720 if (qp->div->n_row > 0) {
2721 isl_basic_set *bset;
2722 context = isl_set_add_dims(context, isl_dim_set,
2723 qp->div->n_row);
2724 bset = isl_basic_set_universe(isl_set_get_space(context));
2725 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2726 context = isl_set_intersect(context,
2727 isl_set_from_basic_set(bset));
2730 aff = isl_set_affine_hull(context);
2731 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2732 error:
2733 isl_qpolynomial_free(qp);
2734 isl_set_free(context);
2735 return NULL;
2738 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2739 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2741 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2742 isl_set *dom_context = isl_set_universe(space);
2743 dom_context = isl_set_intersect_params(dom_context, context);
2744 return isl_qpolynomial_gist(qp, dom_context);
2747 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2748 __isl_take isl_qpolynomial *qp)
2750 isl_set *dom;
2752 if (!qp)
2753 return NULL;
2754 if (isl_qpolynomial_is_zero(qp)) {
2755 isl_space *dim = isl_qpolynomial_get_space(qp);
2756 isl_qpolynomial_free(qp);
2757 return isl_pw_qpolynomial_zero(dim);
2760 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2761 return isl_pw_qpolynomial_alloc(dom, qp);
2764 #undef PW
2765 #define PW isl_pw_qpolynomial
2766 #undef EL
2767 #define EL isl_qpolynomial
2768 #undef EL_IS_ZERO
2769 #define EL_IS_ZERO is_zero
2770 #undef ZERO
2771 #define ZERO zero
2772 #undef IS_ZERO
2773 #define IS_ZERO is_zero
2774 #undef FIELD
2775 #define FIELD qp
2776 #undef DEFAULT_IS_ZERO
2777 #define DEFAULT_IS_ZERO 1
2779 #define NO_PULLBACK
2781 #include <isl_pw_templ.c>
2783 #undef UNION
2784 #define UNION isl_union_pw_qpolynomial
2785 #undef PART
2786 #define PART isl_pw_qpolynomial
2787 #undef PARTS
2788 #define PARTS pw_qpolynomial
2789 #define ALIGN_DOMAIN
2791 #include <isl_union_templ.c>
2793 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2795 if (!pwqp)
2796 return -1;
2798 if (pwqp->n != -1)
2799 return 0;
2801 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2802 return 0;
2804 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2807 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2808 __isl_take isl_pw_qpolynomial *pwqp1,
2809 __isl_take isl_pw_qpolynomial *pwqp2)
2811 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2814 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2815 __isl_take isl_pw_qpolynomial *pwqp1,
2816 __isl_take isl_pw_qpolynomial *pwqp2)
2818 int i, j, n;
2819 struct isl_pw_qpolynomial *res;
2821 if (!pwqp1 || !pwqp2)
2822 goto error;
2824 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
2825 goto error);
2827 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2828 isl_pw_qpolynomial_free(pwqp2);
2829 return pwqp1;
2832 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2833 isl_pw_qpolynomial_free(pwqp1);
2834 return pwqp2;
2837 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2838 isl_pw_qpolynomial_free(pwqp1);
2839 return pwqp2;
2842 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2843 isl_pw_qpolynomial_free(pwqp2);
2844 return pwqp1;
2847 n = pwqp1->n * pwqp2->n;
2848 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
2850 for (i = 0; i < pwqp1->n; ++i) {
2851 for (j = 0; j < pwqp2->n; ++j) {
2852 struct isl_set *common;
2853 struct isl_qpolynomial *prod;
2854 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2855 isl_set_copy(pwqp2->p[j].set));
2856 if (isl_set_plain_is_empty(common)) {
2857 isl_set_free(common);
2858 continue;
2861 prod = isl_qpolynomial_mul(
2862 isl_qpolynomial_copy(pwqp1->p[i].qp),
2863 isl_qpolynomial_copy(pwqp2->p[j].qp));
2865 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2869 isl_pw_qpolynomial_free(pwqp1);
2870 isl_pw_qpolynomial_free(pwqp2);
2872 return res;
2873 error:
2874 isl_pw_qpolynomial_free(pwqp1);
2875 isl_pw_qpolynomial_free(pwqp2);
2876 return NULL;
2879 __isl_give isl_val *isl_upoly_eval(__isl_take struct isl_upoly *up,
2880 __isl_take isl_vec *vec)
2882 int i;
2883 struct isl_upoly_rec *rec;
2884 isl_val *res;
2885 isl_val *base;
2887 if (isl_upoly_is_cst(up)) {
2888 isl_vec_free(vec);
2889 res = isl_upoly_get_constant_val(up);
2890 isl_upoly_free(up);
2891 return res;
2894 rec = isl_upoly_as_rec(up);
2895 if (!rec)
2896 goto error;
2898 isl_assert(up->ctx, rec->n >= 1, goto error);
2900 base = isl_val_rat_from_isl_int(up->ctx,
2901 vec->el[1 + up->var], vec->el[0]);
2903 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2904 isl_vec_copy(vec));
2906 for (i = rec->n - 2; i >= 0; --i) {
2907 res = isl_val_mul(res, isl_val_copy(base));
2908 res = isl_val_add(res,
2909 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2910 isl_vec_copy(vec)));
2913 isl_val_free(base);
2914 isl_upoly_free(up);
2915 isl_vec_free(vec);
2916 return res;
2917 error:
2918 isl_upoly_free(up);
2919 isl_vec_free(vec);
2920 return NULL;
2923 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
2924 __isl_take isl_point *pnt)
2926 isl_vec *ext;
2927 isl_val *v;
2929 if (!qp || !pnt)
2930 goto error;
2931 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
2933 if (qp->div->n_row == 0)
2934 ext = isl_vec_copy(pnt->vec);
2935 else {
2936 int i;
2937 unsigned dim = isl_space_dim(qp->dim, isl_dim_all);
2938 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2939 if (!ext)
2940 goto error;
2942 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2943 for (i = 0; i < qp->div->n_row; ++i) {
2944 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2945 1 + dim + i, &ext->el[1+dim+i]);
2946 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2947 qp->div->row[i][0]);
2951 v = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2953 isl_qpolynomial_free(qp);
2954 isl_point_free(pnt);
2956 return v;
2957 error:
2958 isl_qpolynomial_free(qp);
2959 isl_point_free(pnt);
2960 return NULL;
2963 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2964 __isl_keep struct isl_upoly_cst *cst2)
2966 int cmp;
2967 isl_int t;
2968 isl_int_init(t);
2969 isl_int_mul(t, cst1->n, cst2->d);
2970 isl_int_submul(t, cst2->n, cst1->d);
2971 cmp = isl_int_sgn(t);
2972 isl_int_clear(t);
2973 return cmp;
2976 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2977 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2978 unsigned first, unsigned n)
2980 unsigned total;
2981 unsigned g_pos;
2982 int *exp;
2984 if (!qp)
2985 return NULL;
2986 if (type == isl_dim_out)
2987 isl_die(qp->div->ctx, isl_error_invalid,
2988 "cannot insert output/set dimensions",
2989 goto error);
2990 if (type == isl_dim_in)
2991 type = isl_dim_set;
2992 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2993 return qp;
2995 qp = isl_qpolynomial_cow(qp);
2996 if (!qp)
2997 return NULL;
2999 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
3000 goto error);
3002 g_pos = pos(qp->dim, type) + first;
3004 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3005 if (!qp->div)
3006 goto error;
3008 total = qp->div->n_col - 2;
3009 if (total > g_pos) {
3010 int i;
3011 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3012 if (!exp)
3013 goto error;
3014 for (i = 0; i < total - g_pos; ++i)
3015 exp[i] = i + n;
3016 qp->upoly = expand(qp->upoly, exp, g_pos);
3017 free(exp);
3018 if (!qp->upoly)
3019 goto error;
3022 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3023 if (!qp->dim)
3024 goto error;
3026 return qp;
3027 error:
3028 isl_qpolynomial_free(qp);
3029 return NULL;
3032 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3033 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3035 unsigned pos;
3037 pos = isl_qpolynomial_dim(qp, type);
3039 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3042 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3043 __isl_take isl_pw_qpolynomial *pwqp,
3044 enum isl_dim_type type, unsigned n)
3046 unsigned pos;
3048 pos = isl_pw_qpolynomial_dim(pwqp, type);
3050 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3053 static int *reordering_move(isl_ctx *ctx,
3054 unsigned len, unsigned dst, unsigned src, unsigned n)
3056 int i;
3057 int *reordering;
3059 reordering = isl_alloc_array(ctx, int, len);
3060 if (!reordering)
3061 return NULL;
3063 if (dst <= src) {
3064 for (i = 0; i < dst; ++i)
3065 reordering[i] = i;
3066 for (i = 0; i < n; ++i)
3067 reordering[src + i] = dst + i;
3068 for (i = 0; i < src - dst; ++i)
3069 reordering[dst + i] = dst + n + i;
3070 for (i = 0; i < len - src - n; ++i)
3071 reordering[src + n + i] = src + n + i;
3072 } else {
3073 for (i = 0; i < src; ++i)
3074 reordering[i] = i;
3075 for (i = 0; i < n; ++i)
3076 reordering[src + i] = dst + i;
3077 for (i = 0; i < dst - src; ++i)
3078 reordering[src + n + i] = src + i;
3079 for (i = 0; i < len - dst - n; ++i)
3080 reordering[dst + n + i] = dst + n + i;
3083 return reordering;
3086 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3087 __isl_take isl_qpolynomial *qp,
3088 enum isl_dim_type dst_type, unsigned dst_pos,
3089 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3091 unsigned g_dst_pos;
3092 unsigned g_src_pos;
3093 int *reordering;
3095 if (n == 0)
3096 return qp;
3098 qp = isl_qpolynomial_cow(qp);
3099 if (!qp)
3100 return NULL;
3102 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3103 isl_die(qp->dim->ctx, isl_error_invalid,
3104 "cannot move output/set dimension",
3105 goto error);
3106 if (dst_type == isl_dim_in)
3107 dst_type = isl_dim_set;
3108 if (src_type == isl_dim_in)
3109 src_type = isl_dim_set;
3111 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
3112 goto error);
3114 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3115 g_src_pos = pos(qp->dim, src_type) + src_pos;
3116 if (dst_type > src_type)
3117 g_dst_pos -= n;
3119 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3120 if (!qp->div)
3121 goto error;
3122 qp = sort_divs(qp);
3123 if (!qp)
3124 goto error;
3126 reordering = reordering_move(qp->dim->ctx,
3127 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3128 if (!reordering)
3129 goto error;
3131 qp->upoly = reorder(qp->upoly, reordering);
3132 free(reordering);
3133 if (!qp->upoly)
3134 goto error;
3136 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3137 if (!qp->dim)
3138 goto error;
3140 return qp;
3141 error:
3142 isl_qpolynomial_free(qp);
3143 return NULL;
3146 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3147 isl_int *f, isl_int denom)
3149 struct isl_upoly *up;
3151 dim = isl_space_domain(dim);
3152 if (!dim)
3153 return NULL;
3155 up = isl_upoly_from_affine(dim->ctx, f, denom,
3156 1 + isl_space_dim(dim, isl_dim_all));
3158 return isl_qpolynomial_alloc(dim, 0, up);
3161 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3163 isl_ctx *ctx;
3164 struct isl_upoly *up;
3165 isl_qpolynomial *qp;
3167 if (!aff)
3168 return NULL;
3170 ctx = isl_aff_get_ctx(aff);
3171 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3172 aff->v->size - 1);
3174 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3175 aff->ls->div->n_row, up);
3176 if (!qp)
3177 goto error;
3179 isl_mat_free(qp->div);
3180 qp->div = isl_mat_copy(aff->ls->div);
3181 qp->div = isl_mat_cow(qp->div);
3182 if (!qp->div)
3183 goto error;
3185 isl_aff_free(aff);
3186 qp = reduce_divs(qp);
3187 qp = remove_redundant_divs(qp);
3188 return qp;
3189 error:
3190 isl_aff_free(aff);
3191 return isl_qpolynomial_free(qp);
3194 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3195 __isl_take isl_pw_aff *pwaff)
3197 int i;
3198 isl_pw_qpolynomial *pwqp;
3200 if (!pwaff)
3201 return NULL;
3203 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3204 pwaff->n);
3206 for (i = 0; i < pwaff->n; ++i) {
3207 isl_set *dom;
3208 isl_qpolynomial *qp;
3210 dom = isl_set_copy(pwaff->p[i].set);
3211 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3212 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3215 isl_pw_aff_free(pwaff);
3216 return pwqp;
3219 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3220 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3222 isl_aff *aff;
3224 aff = isl_constraint_get_bound(c, type, pos);
3225 isl_constraint_free(c);
3226 return isl_qpolynomial_from_aff(aff);
3229 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3230 * in "qp" by subs[i].
3232 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3233 __isl_take isl_qpolynomial *qp,
3234 enum isl_dim_type type, unsigned first, unsigned n,
3235 __isl_keep isl_qpolynomial **subs)
3237 int i;
3238 struct isl_upoly **ups;
3240 if (n == 0)
3241 return qp;
3243 qp = isl_qpolynomial_cow(qp);
3244 if (!qp)
3245 return NULL;
3247 if (type == isl_dim_out)
3248 isl_die(qp->dim->ctx, isl_error_invalid,
3249 "cannot substitute output/set dimension",
3250 goto error);
3251 if (type == isl_dim_in)
3252 type = isl_dim_set;
3254 for (i = 0; i < n; ++i)
3255 if (!subs[i])
3256 goto error;
3258 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3259 goto error);
3261 for (i = 0; i < n; ++i)
3262 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3263 goto error);
3265 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3266 for (i = 0; i < n; ++i)
3267 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3269 first += pos(qp->dim, type);
3271 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3272 if (!ups)
3273 goto error;
3274 for (i = 0; i < n; ++i)
3275 ups[i] = subs[i]->upoly;
3277 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3279 free(ups);
3281 if (!qp->upoly)
3282 goto error;
3284 return qp;
3285 error:
3286 isl_qpolynomial_free(qp);
3287 return NULL;
3290 /* Extend "bset" with extra set dimensions for each integer division
3291 * in "qp" and then call "fn" with the extended bset and the polynomial
3292 * that results from replacing each of the integer divisions by the
3293 * corresponding extra set dimension.
3295 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3296 __isl_keep isl_basic_set *bset,
3297 int (*fn)(__isl_take isl_basic_set *bset,
3298 __isl_take isl_qpolynomial *poly, void *user), void *user)
3300 isl_space *dim;
3301 isl_mat *div;
3302 isl_qpolynomial *poly;
3304 if (!qp || !bset)
3305 goto error;
3306 if (qp->div->n_row == 0)
3307 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3308 user);
3310 div = isl_mat_copy(qp->div);
3311 dim = isl_space_copy(qp->dim);
3312 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3313 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3314 bset = isl_basic_set_copy(bset);
3315 bset = isl_basic_set_add_dims(bset, isl_dim_set, qp->div->n_row);
3316 bset = add_div_constraints(bset, div);
3318 return fn(bset, poly, user);
3319 error:
3320 return -1;
3323 /* Return total degree in variables first (inclusive) up to last (exclusive).
3325 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3327 int deg = -1;
3328 int i;
3329 struct isl_upoly_rec *rec;
3331 if (!up)
3332 return -2;
3333 if (isl_upoly_is_zero(up))
3334 return -1;
3335 if (isl_upoly_is_cst(up) || up->var < first)
3336 return 0;
3338 rec = isl_upoly_as_rec(up);
3339 if (!rec)
3340 return -2;
3342 for (i = 0; i < rec->n; ++i) {
3343 int d;
3345 if (isl_upoly_is_zero(rec->p[i]))
3346 continue;
3347 d = isl_upoly_degree(rec->p[i], first, last);
3348 if (up->var < last)
3349 d += i;
3350 if (d > deg)
3351 deg = d;
3354 return deg;
3357 /* Return total degree in set variables.
3359 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3361 unsigned ovar;
3362 unsigned nvar;
3364 if (!poly)
3365 return -2;
3367 ovar = isl_space_offset(poly->dim, isl_dim_set);
3368 nvar = isl_space_dim(poly->dim, isl_dim_set);
3369 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3372 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3373 unsigned pos, int deg)
3375 int i;
3376 struct isl_upoly_rec *rec;
3378 if (!up)
3379 return NULL;
3381 if (isl_upoly_is_cst(up) || up->var < pos) {
3382 if (deg == 0)
3383 return isl_upoly_copy(up);
3384 else
3385 return isl_upoly_zero(up->ctx);
3388 rec = isl_upoly_as_rec(up);
3389 if (!rec)
3390 return NULL;
3392 if (up->var == pos) {
3393 if (deg < rec->n)
3394 return isl_upoly_copy(rec->p[deg]);
3395 else
3396 return isl_upoly_zero(up->ctx);
3399 up = isl_upoly_copy(up);
3400 up = isl_upoly_cow(up);
3401 rec = isl_upoly_as_rec(up);
3402 if (!rec)
3403 goto error;
3405 for (i = 0; i < rec->n; ++i) {
3406 struct isl_upoly *t;
3407 t = isl_upoly_coeff(rec->p[i], pos, deg);
3408 if (!t)
3409 goto error;
3410 isl_upoly_free(rec->p[i]);
3411 rec->p[i] = t;
3414 return up;
3415 error:
3416 isl_upoly_free(up);
3417 return NULL;
3420 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3422 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3423 __isl_keep isl_qpolynomial *qp,
3424 enum isl_dim_type type, unsigned t_pos, int deg)
3426 unsigned g_pos;
3427 struct isl_upoly *up;
3428 isl_qpolynomial *c;
3430 if (!qp)
3431 return NULL;
3433 if (type == isl_dim_out)
3434 isl_die(qp->div->ctx, isl_error_invalid,
3435 "output/set dimension does not have a coefficient",
3436 return NULL);
3437 if (type == isl_dim_in)
3438 type = isl_dim_set;
3440 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3441 return NULL);
3443 g_pos = pos(qp->dim, type) + t_pos;
3444 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3446 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3447 if (!c)
3448 return NULL;
3449 isl_mat_free(c->div);
3450 c->div = isl_mat_copy(qp->div);
3451 if (!c->div)
3452 goto error;
3453 return c;
3454 error:
3455 isl_qpolynomial_free(c);
3456 return NULL;
3459 /* Homogenize the polynomial in the variables first (inclusive) up to
3460 * last (exclusive) by inserting powers of variable first.
3461 * Variable first is assumed not to appear in the input.
3463 __isl_give struct isl_upoly *isl_upoly_homogenize(
3464 __isl_take struct isl_upoly *up, int deg, int target,
3465 int first, int last)
3467 int i;
3468 struct isl_upoly_rec *rec;
3470 if (!up)
3471 return NULL;
3472 if (isl_upoly_is_zero(up))
3473 return up;
3474 if (deg == target)
3475 return up;
3476 if (isl_upoly_is_cst(up) || up->var < first) {
3477 struct isl_upoly *hom;
3479 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3480 if (!hom)
3481 goto error;
3482 rec = isl_upoly_as_rec(hom);
3483 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3485 return hom;
3488 up = isl_upoly_cow(up);
3489 rec = isl_upoly_as_rec(up);
3490 if (!rec)
3491 goto error;
3493 for (i = 0; i < rec->n; ++i) {
3494 if (isl_upoly_is_zero(rec->p[i]))
3495 continue;
3496 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3497 up->var < last ? deg + i : i, target,
3498 first, last);
3499 if (!rec->p[i])
3500 goto error;
3503 return up;
3504 error:
3505 isl_upoly_free(up);
3506 return NULL;
3509 /* Homogenize the polynomial in the set variables by introducing
3510 * powers of an extra set variable at position 0.
3512 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3513 __isl_take isl_qpolynomial *poly)
3515 unsigned ovar;
3516 unsigned nvar;
3517 int deg = isl_qpolynomial_degree(poly);
3519 if (deg < -1)
3520 goto error;
3522 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3523 poly = isl_qpolynomial_cow(poly);
3524 if (!poly)
3525 goto error;
3527 ovar = isl_space_offset(poly->dim, isl_dim_set);
3528 nvar = isl_space_dim(poly->dim, isl_dim_set);
3529 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3530 ovar, ovar + nvar);
3531 if (!poly->upoly)
3532 goto error;
3534 return poly;
3535 error:
3536 isl_qpolynomial_free(poly);
3537 return NULL;
3540 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3541 __isl_take isl_mat *div)
3543 isl_term *term;
3544 int n;
3546 if (!dim || !div)
3547 goto error;
3549 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3551 term = isl_calloc(dim->ctx, struct isl_term,
3552 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3553 if (!term)
3554 goto error;
3556 term->ref = 1;
3557 term->dim = dim;
3558 term->div = div;
3559 isl_int_init(term->n);
3560 isl_int_init(term->d);
3562 return term;
3563 error:
3564 isl_space_free(dim);
3565 isl_mat_free(div);
3566 return NULL;
3569 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3571 if (!term)
3572 return NULL;
3574 term->ref++;
3575 return term;
3578 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3580 int i;
3581 isl_term *dup;
3582 unsigned total;
3584 if (!term)
3585 return NULL;
3587 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3589 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3590 if (!dup)
3591 return NULL;
3593 isl_int_set(dup->n, term->n);
3594 isl_int_set(dup->d, term->d);
3596 for (i = 0; i < total; ++i)
3597 dup->pow[i] = term->pow[i];
3599 return dup;
3602 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3604 if (!term)
3605 return NULL;
3607 if (term->ref == 1)
3608 return term;
3609 term->ref--;
3610 return isl_term_dup(term);
3613 void isl_term_free(__isl_take isl_term *term)
3615 if (!term)
3616 return;
3618 if (--term->ref > 0)
3619 return;
3621 isl_space_free(term->dim);
3622 isl_mat_free(term->div);
3623 isl_int_clear(term->n);
3624 isl_int_clear(term->d);
3625 free(term);
3628 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3630 if (!term)
3631 return 0;
3633 switch (type) {
3634 case isl_dim_param:
3635 case isl_dim_in:
3636 case isl_dim_out: return isl_space_dim(term->dim, type);
3637 case isl_dim_div: return term->div->n_row;
3638 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3639 term->div->n_row;
3640 default: return 0;
3644 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3646 return term ? term->dim->ctx : NULL;
3649 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3651 if (!term)
3652 return;
3653 isl_int_set(*n, term->n);
3656 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3658 if (!term)
3659 return;
3660 isl_int_set(*d, term->d);
3663 /* Return the coefficient of the term "term".
3665 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3667 if (!term)
3668 return NULL;
3670 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3671 term->n, term->d);
3674 int isl_term_get_exp(__isl_keep isl_term *term,
3675 enum isl_dim_type type, unsigned pos)
3677 if (!term)
3678 return -1;
3680 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3682 if (type >= isl_dim_set)
3683 pos += isl_space_dim(term->dim, isl_dim_param);
3684 if (type >= isl_dim_div)
3685 pos += isl_space_dim(term->dim, isl_dim_set);
3687 return term->pow[pos];
3690 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3692 isl_local_space *ls;
3693 isl_aff *aff;
3695 if (!term)
3696 return NULL;
3698 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3699 return NULL);
3701 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3702 isl_mat_copy(term->div));
3703 aff = isl_aff_alloc(ls);
3704 if (!aff)
3705 return NULL;
3707 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3709 aff = isl_aff_normalize(aff);
3711 return aff;
3714 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3715 int (*fn)(__isl_take isl_term *term, void *user),
3716 __isl_take isl_term *term, void *user)
3718 int i;
3719 struct isl_upoly_rec *rec;
3721 if (!up || !term)
3722 goto error;
3724 if (isl_upoly_is_zero(up))
3725 return term;
3727 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3728 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3729 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3731 if (isl_upoly_is_cst(up)) {
3732 struct isl_upoly_cst *cst;
3733 cst = isl_upoly_as_cst(up);
3734 if (!cst)
3735 goto error;
3736 term = isl_term_cow(term);
3737 if (!term)
3738 goto error;
3739 isl_int_set(term->n, cst->n);
3740 isl_int_set(term->d, cst->d);
3741 if (fn(isl_term_copy(term), user) < 0)
3742 goto error;
3743 return term;
3746 rec = isl_upoly_as_rec(up);
3747 if (!rec)
3748 goto error;
3750 for (i = 0; i < rec->n; ++i) {
3751 term = isl_term_cow(term);
3752 if (!term)
3753 goto error;
3754 term->pow[up->var] = i;
3755 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3756 if (!term)
3757 goto error;
3759 term->pow[up->var] = 0;
3761 return term;
3762 error:
3763 isl_term_free(term);
3764 return NULL;
3767 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3768 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3770 isl_term *term;
3772 if (!qp)
3773 return -1;
3775 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3776 if (!term)
3777 return -1;
3779 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3781 isl_term_free(term);
3783 return term ? 0 : -1;
3786 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3788 struct isl_upoly *up;
3789 isl_qpolynomial *qp;
3790 int i, n;
3792 if (!term)
3793 return NULL;
3795 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3797 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3798 for (i = 0; i < n; ++i) {
3799 if (!term->pow[i])
3800 continue;
3801 up = isl_upoly_mul(up,
3802 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3805 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3806 if (!qp)
3807 goto error;
3808 isl_mat_free(qp->div);
3809 qp->div = isl_mat_copy(term->div);
3810 if (!qp->div)
3811 goto error;
3813 isl_term_free(term);
3814 return qp;
3815 error:
3816 isl_qpolynomial_free(qp);
3817 isl_term_free(term);
3818 return NULL;
3821 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3822 __isl_take isl_space *dim)
3824 int i;
3825 int extra;
3826 unsigned total;
3828 if (!qp || !dim)
3829 goto error;
3831 if (isl_space_is_equal(qp->dim, dim)) {
3832 isl_space_free(dim);
3833 return qp;
3836 qp = isl_qpolynomial_cow(qp);
3837 if (!qp)
3838 goto error;
3840 extra = isl_space_dim(dim, isl_dim_set) -
3841 isl_space_dim(qp->dim, isl_dim_set);
3842 total = isl_space_dim(qp->dim, isl_dim_all);
3843 if (qp->div->n_row) {
3844 int *exp;
3846 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3847 if (!exp)
3848 goto error;
3849 for (i = 0; i < qp->div->n_row; ++i)
3850 exp[i] = extra + i;
3851 qp->upoly = expand(qp->upoly, exp, total);
3852 free(exp);
3853 if (!qp->upoly)
3854 goto error;
3856 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3857 if (!qp->div)
3858 goto error;
3859 for (i = 0; i < qp->div->n_row; ++i)
3860 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3862 isl_space_free(qp->dim);
3863 qp->dim = dim;
3865 return qp;
3866 error:
3867 isl_space_free(dim);
3868 isl_qpolynomial_free(qp);
3869 return NULL;
3872 /* For each parameter or variable that does not appear in qp,
3873 * first eliminate the variable from all constraints and then set it to zero.
3875 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3876 __isl_keep isl_qpolynomial *qp)
3878 int *active = NULL;
3879 int i;
3880 int d;
3881 unsigned nparam;
3882 unsigned nvar;
3884 if (!set || !qp)
3885 goto error;
3887 d = isl_space_dim(set->dim, isl_dim_all);
3888 active = isl_calloc_array(set->ctx, int, d);
3889 if (set_active(qp, active) < 0)
3890 goto error;
3892 for (i = 0; i < d; ++i)
3893 if (!active[i])
3894 break;
3896 if (i == d) {
3897 free(active);
3898 return set;
3901 nparam = isl_space_dim(set->dim, isl_dim_param);
3902 nvar = isl_space_dim(set->dim, isl_dim_set);
3903 for (i = 0; i < nparam; ++i) {
3904 if (active[i])
3905 continue;
3906 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3907 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3909 for (i = 0; i < nvar; ++i) {
3910 if (active[nparam + i])
3911 continue;
3912 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3913 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3916 free(active);
3918 return set;
3919 error:
3920 free(active);
3921 isl_set_free(set);
3922 return NULL;
3925 struct isl_opt_data {
3926 isl_qpolynomial *qp;
3927 int first;
3928 isl_val *opt;
3929 int max;
3932 static int opt_fn(__isl_take isl_point *pnt, void *user)
3934 struct isl_opt_data *data = (struct isl_opt_data *)user;
3935 isl_val *val;
3937 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3938 if (data->first) {
3939 data->first = 0;
3940 data->opt = val;
3941 } else if (data->max) {
3942 data->opt = isl_val_max(data->opt, val);
3943 } else {
3944 data->opt = isl_val_min(data->opt, val);
3947 return 0;
3950 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
3951 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3953 struct isl_opt_data data = { NULL, 1, NULL, max };
3955 if (!set || !qp)
3956 goto error;
3958 if (isl_upoly_is_cst(qp->upoly)) {
3959 isl_set_free(set);
3960 data.opt = isl_qpolynomial_get_constant_val(qp);
3961 isl_qpolynomial_free(qp);
3962 return data.opt;
3965 set = fix_inactive(set, qp);
3967 data.qp = qp;
3968 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3969 goto error;
3971 if (data.first)
3972 data.opt = isl_val_zero(isl_set_get_ctx(set));
3974 isl_set_free(set);
3975 isl_qpolynomial_free(qp);
3976 return data.opt;
3977 error:
3978 isl_set_free(set);
3979 isl_qpolynomial_free(qp);
3980 isl_val_free(data.opt);
3981 return NULL;
3984 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
3985 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
3987 int i;
3988 int n_sub;
3989 isl_ctx *ctx;
3990 struct isl_upoly **subs;
3991 isl_mat *mat, *diag;
3993 qp = isl_qpolynomial_cow(qp);
3994 if (!qp || !morph)
3995 goto error;
3997 ctx = qp->dim->ctx;
3998 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4000 n_sub = morph->inv->n_row - 1;
4001 if (morph->inv->n_row != morph->inv->n_col)
4002 n_sub += qp->div->n_row;
4003 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4004 if (n_sub && !subs)
4005 goto error;
4007 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4008 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4009 morph->inv->row[0][0], morph->inv->n_col);
4010 if (morph->inv->n_row != morph->inv->n_col)
4011 for (i = 0; i < qp->div->n_row; ++i)
4012 subs[morph->inv->n_row - 1 + i] =
4013 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4015 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4017 for (i = 0; i < n_sub; ++i)
4018 isl_upoly_free(subs[i]);
4019 free(subs);
4021 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4022 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4023 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4024 mat = isl_mat_diagonal(mat, diag);
4025 qp->div = isl_mat_product(qp->div, mat);
4026 isl_space_free(qp->dim);
4027 qp->dim = isl_space_copy(morph->ran->dim);
4029 if (!qp->upoly || !qp->div || !qp->dim)
4030 goto error;
4032 isl_morph_free(morph);
4034 return qp;
4035 error:
4036 isl_qpolynomial_free(qp);
4037 isl_morph_free(morph);
4038 return NULL;
4041 static int neg_entry(void **entry, void *user)
4043 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4045 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
4047 return *pwqp ? 0 : -1;
4050 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
4051 __isl_take isl_union_pw_qpolynomial *upwqp)
4053 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4054 if (!upwqp)
4055 return NULL;
4057 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4058 &neg_entry, NULL) < 0)
4059 goto error;
4061 return upwqp;
4062 error:
4063 isl_union_pw_qpolynomial_free(upwqp);
4064 return NULL;
4067 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4068 __isl_take isl_union_pw_qpolynomial *upwqp1,
4069 __isl_take isl_union_pw_qpolynomial *upwqp2)
4071 return match_bin_op(upwqp1, upwqp2, &isl_pw_qpolynomial_mul);
4074 /* Reorder the columns of the given div definitions according to the
4075 * given reordering.
4077 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
4078 __isl_take isl_reordering *r)
4080 int i, j;
4081 isl_mat *mat;
4082 int extra;
4084 if (!div || !r)
4085 goto error;
4087 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
4088 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
4089 if (!mat)
4090 goto error;
4092 for (i = 0; i < div->n_row; ++i) {
4093 isl_seq_cpy(mat->row[i], div->row[i], 2);
4094 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
4095 for (j = 0; j < r->len; ++j)
4096 isl_int_set(mat->row[i][2 + r->pos[j]],
4097 div->row[i][2 + j]);
4100 isl_reordering_free(r);
4101 isl_mat_free(div);
4102 return mat;
4103 error:
4104 isl_reordering_free(r);
4105 isl_mat_free(div);
4106 return NULL;
4109 /* Reorder the dimension of "qp" according to the given reordering.
4111 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4112 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4114 qp = isl_qpolynomial_cow(qp);
4115 if (!qp)
4116 goto error;
4118 r = isl_reordering_extend(r, qp->div->n_row);
4119 if (!r)
4120 goto error;
4122 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
4123 if (!qp->div)
4124 goto error;
4126 qp->upoly = reorder(qp->upoly, r->pos);
4127 if (!qp->upoly)
4128 goto error;
4130 qp = isl_qpolynomial_reset_domain_space(qp, isl_space_copy(r->dim));
4132 isl_reordering_free(r);
4133 return qp;
4134 error:
4135 isl_qpolynomial_free(qp);
4136 isl_reordering_free(r);
4137 return NULL;
4140 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4141 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4143 if (!qp || !model)
4144 goto error;
4146 if (!isl_space_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
4147 isl_reordering *exp;
4149 model = isl_space_drop_dims(model, isl_dim_in,
4150 0, isl_space_dim(model, isl_dim_in));
4151 model = isl_space_drop_dims(model, isl_dim_out,
4152 0, isl_space_dim(model, isl_dim_out));
4153 exp = isl_parameter_alignment_reordering(qp->dim, model);
4154 exp = isl_reordering_extend_space(exp,
4155 isl_qpolynomial_get_domain_space(qp));
4156 qp = isl_qpolynomial_realign_domain(qp, exp);
4159 isl_space_free(model);
4160 return qp;
4161 error:
4162 isl_space_free(model);
4163 isl_qpolynomial_free(qp);
4164 return NULL;
4167 struct isl_split_periods_data {
4168 int max_periods;
4169 isl_pw_qpolynomial *res;
4172 /* Create a slice where the integer division "div" has the fixed value "v".
4173 * In particular, if "div" refers to floor(f/m), then create a slice
4175 * m v <= f <= m v + (m - 1)
4177 * or
4179 * f - m v >= 0
4180 * -f + m v + (m - 1) >= 0
4182 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4183 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4185 int total;
4186 isl_basic_set *bset = NULL;
4187 int k;
4189 if (!dim || !qp)
4190 goto error;
4192 total = isl_space_dim(dim, isl_dim_all);
4193 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4195 k = isl_basic_set_alloc_inequality(bset);
4196 if (k < 0)
4197 goto error;
4198 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4199 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4201 k = isl_basic_set_alloc_inequality(bset);
4202 if (k < 0)
4203 goto error;
4204 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4205 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4206 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4207 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4209 isl_space_free(dim);
4210 return isl_set_from_basic_set(bset);
4211 error:
4212 isl_basic_set_free(bset);
4213 isl_space_free(dim);
4214 return NULL;
4217 static int split_periods(__isl_take isl_set *set,
4218 __isl_take isl_qpolynomial *qp, void *user);
4220 /* Create a slice of the domain "set" such that integer division "div"
4221 * has the fixed value "v" and add the results to data->res,
4222 * replacing the integer division by "v" in "qp".
4224 static int set_div(__isl_take isl_set *set,
4225 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4226 struct isl_split_periods_data *data)
4228 int i;
4229 int total;
4230 isl_set *slice;
4231 struct isl_upoly *cst;
4233 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4234 set = isl_set_intersect(set, slice);
4236 if (!qp)
4237 goto error;
4239 total = isl_space_dim(qp->dim, isl_dim_all);
4241 for (i = div + 1; i < qp->div->n_row; ++i) {
4242 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4243 continue;
4244 isl_int_addmul(qp->div->row[i][1],
4245 qp->div->row[i][2 + total + div], v);
4246 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4249 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4250 qp = substitute_div(qp, div, cst);
4252 return split_periods(set, qp, data);
4253 error:
4254 isl_set_free(set);
4255 isl_qpolynomial_free(qp);
4256 return -1;
4259 /* Split the domain "set" such that integer division "div"
4260 * has a fixed value (ranging from "min" to "max") on each slice
4261 * and add the results to data->res.
4263 static int split_div(__isl_take isl_set *set,
4264 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4265 struct isl_split_periods_data *data)
4267 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4268 isl_set *set_i = isl_set_copy(set);
4269 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4271 if (set_div(set_i, qp_i, div, min, data) < 0)
4272 goto error;
4274 isl_set_free(set);
4275 isl_qpolynomial_free(qp);
4276 return 0;
4277 error:
4278 isl_set_free(set);
4279 isl_qpolynomial_free(qp);
4280 return -1;
4283 /* If "qp" refers to any integer division
4284 * that can only attain "max_periods" distinct values on "set"
4285 * then split the domain along those distinct values.
4286 * Add the results (or the original if no splitting occurs)
4287 * to data->res.
4289 static int split_periods(__isl_take isl_set *set,
4290 __isl_take isl_qpolynomial *qp, void *user)
4292 int i;
4293 isl_pw_qpolynomial *pwqp;
4294 struct isl_split_periods_data *data;
4295 isl_int min, max;
4296 int total;
4297 int r = 0;
4299 data = (struct isl_split_periods_data *)user;
4301 if (!set || !qp)
4302 goto error;
4304 if (qp->div->n_row == 0) {
4305 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4306 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4307 return 0;
4310 isl_int_init(min);
4311 isl_int_init(max);
4312 total = isl_space_dim(qp->dim, isl_dim_all);
4313 for (i = 0; i < qp->div->n_row; ++i) {
4314 enum isl_lp_result lp_res;
4316 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4317 qp->div->n_row) != -1)
4318 continue;
4320 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4321 set->ctx->one, &min, NULL, NULL);
4322 if (lp_res == isl_lp_error)
4323 goto error2;
4324 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4325 continue;
4326 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4328 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4329 set->ctx->one, &max, NULL, NULL);
4330 if (lp_res == isl_lp_error)
4331 goto error2;
4332 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4333 continue;
4334 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4336 isl_int_sub(max, max, min);
4337 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4338 isl_int_add(max, max, min);
4339 break;
4343 if (i < qp->div->n_row) {
4344 r = split_div(set, qp, i, min, max, data);
4345 } else {
4346 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4347 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4350 isl_int_clear(max);
4351 isl_int_clear(min);
4353 return r;
4354 error2:
4355 isl_int_clear(max);
4356 isl_int_clear(min);
4357 error:
4358 isl_set_free(set);
4359 isl_qpolynomial_free(qp);
4360 return -1;
4363 /* If any quasi-polynomial in pwqp refers to any integer division
4364 * that can only attain "max_periods" distinct values on its domain
4365 * then split the domain along those distinct values.
4367 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4368 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4370 struct isl_split_periods_data data;
4372 data.max_periods = max_periods;
4373 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4375 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4376 goto error;
4378 isl_pw_qpolynomial_free(pwqp);
4380 return data.res;
4381 error:
4382 isl_pw_qpolynomial_free(data.res);
4383 isl_pw_qpolynomial_free(pwqp);
4384 return NULL;
4387 /* Construct a piecewise quasipolynomial that is constant on the given
4388 * domain. In particular, it is
4389 * 0 if cst == 0
4390 * 1 if cst == 1
4391 * infinity if cst == -1
4393 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4394 __isl_take isl_basic_set *bset, int cst)
4396 isl_space *dim;
4397 isl_qpolynomial *qp;
4399 if (!bset)
4400 return NULL;
4402 bset = isl_basic_set_params(bset);
4403 dim = isl_basic_set_get_space(bset);
4404 if (cst < 0)
4405 qp = isl_qpolynomial_infty_on_domain(dim);
4406 else if (cst == 0)
4407 qp = isl_qpolynomial_zero_on_domain(dim);
4408 else
4409 qp = isl_qpolynomial_one_on_domain(dim);
4410 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4413 /* Factor bset, call fn on each of the factors and return the product.
4415 * If no factors can be found, simply call fn on the input.
4416 * Otherwise, construct the factors based on the factorizer,
4417 * call fn on each factor and compute the product.
4419 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4420 __isl_take isl_basic_set *bset,
4421 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4423 int i, n;
4424 isl_space *dim;
4425 isl_set *set;
4426 isl_factorizer *f;
4427 isl_qpolynomial *qp;
4428 isl_pw_qpolynomial *pwqp;
4429 unsigned nparam;
4430 unsigned nvar;
4432 f = isl_basic_set_factorizer(bset);
4433 if (!f)
4434 goto error;
4435 if (f->n_group == 0) {
4436 isl_factorizer_free(f);
4437 return fn(bset);
4440 nparam = isl_basic_set_dim(bset, isl_dim_param);
4441 nvar = isl_basic_set_dim(bset, isl_dim_set);
4443 dim = isl_basic_set_get_space(bset);
4444 dim = isl_space_domain(dim);
4445 set = isl_set_universe(isl_space_copy(dim));
4446 qp = isl_qpolynomial_one_on_domain(dim);
4447 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4449 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4451 for (i = 0, n = 0; i < f->n_group; ++i) {
4452 isl_basic_set *bset_i;
4453 isl_pw_qpolynomial *pwqp_i;
4455 bset_i = isl_basic_set_copy(bset);
4456 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4457 nparam + n + f->len[i], nvar - n - f->len[i]);
4458 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4459 nparam, n);
4460 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4461 n + f->len[i], nvar - n - f->len[i]);
4462 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4464 pwqp_i = fn(bset_i);
4465 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4467 n += f->len[i];
4470 isl_basic_set_free(bset);
4471 isl_factorizer_free(f);
4473 return pwqp;
4474 error:
4475 isl_basic_set_free(bset);
4476 return NULL;
4479 /* Factor bset, call fn on each of the factors and return the product.
4480 * The function is assumed to evaluate to zero on empty domains,
4481 * to one on zero-dimensional domains and to infinity on unbounded domains
4482 * and will not be called explicitly on zero-dimensional or unbounded domains.
4484 * We first check for some special cases and remove all equalities.
4485 * Then we hand over control to compressed_multiplicative_call.
4487 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4488 __isl_take isl_basic_set *bset,
4489 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4491 int bounded;
4492 isl_morph *morph;
4493 isl_pw_qpolynomial *pwqp;
4495 if (!bset)
4496 return NULL;
4498 if (isl_basic_set_plain_is_empty(bset))
4499 return constant_on_domain(bset, 0);
4501 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4502 return constant_on_domain(bset, 1);
4504 bounded = isl_basic_set_is_bounded(bset);
4505 if (bounded < 0)
4506 goto error;
4507 if (!bounded)
4508 return constant_on_domain(bset, -1);
4510 if (bset->n_eq == 0)
4511 return compressed_multiplicative_call(bset, fn);
4513 morph = isl_basic_set_full_compression(bset);
4514 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4516 pwqp = compressed_multiplicative_call(bset, fn);
4518 morph = isl_morph_dom_params(morph);
4519 morph = isl_morph_ran_params(morph);
4520 morph = isl_morph_inverse(morph);
4522 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4524 return pwqp;
4525 error:
4526 isl_basic_set_free(bset);
4527 return NULL;
4530 /* Drop all floors in "qp", turning each integer division [a/m] into
4531 * a rational division a/m. If "down" is set, then the integer division
4532 * is replaced by (a-(m-1))/m instead.
4534 static __isl_give isl_qpolynomial *qp_drop_floors(
4535 __isl_take isl_qpolynomial *qp, int down)
4537 int i;
4538 struct isl_upoly *s;
4540 if (!qp)
4541 return NULL;
4542 if (qp->div->n_row == 0)
4543 return qp;
4545 qp = isl_qpolynomial_cow(qp);
4546 if (!qp)
4547 return NULL;
4549 for (i = qp->div->n_row - 1; i >= 0; --i) {
4550 if (down) {
4551 isl_int_sub(qp->div->row[i][1],
4552 qp->div->row[i][1], qp->div->row[i][0]);
4553 isl_int_add_ui(qp->div->row[i][1],
4554 qp->div->row[i][1], 1);
4556 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4557 qp->div->row[i][0], qp->div->n_col - 1);
4558 qp = substitute_div(qp, i, s);
4559 if (!qp)
4560 return NULL;
4563 return qp;
4566 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4567 * a rational division a/m.
4569 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4570 __isl_take isl_pw_qpolynomial *pwqp)
4572 int i;
4574 if (!pwqp)
4575 return NULL;
4577 if (isl_pw_qpolynomial_is_zero(pwqp))
4578 return pwqp;
4580 pwqp = isl_pw_qpolynomial_cow(pwqp);
4581 if (!pwqp)
4582 return NULL;
4584 for (i = 0; i < pwqp->n; ++i) {
4585 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4586 if (!pwqp->p[i].qp)
4587 goto error;
4590 return pwqp;
4591 error:
4592 isl_pw_qpolynomial_free(pwqp);
4593 return NULL;
4596 /* Adjust all the integer divisions in "qp" such that they are at least
4597 * one over the given orthant (identified by "signs"). This ensures
4598 * that they will still be non-negative even after subtracting (m-1)/m.
4600 * In particular, f is replaced by f' + v, changing f = [a/m]
4601 * to f' = [(a - m v)/m].
4602 * If the constant term k in a is smaller than m,
4603 * the constant term of v is set to floor(k/m) - 1.
4604 * For any other term, if the coefficient c and the variable x have
4605 * the same sign, then no changes are needed.
4606 * Otherwise, if the variable is positive (and c is negative),
4607 * then the coefficient of x in v is set to floor(c/m).
4608 * If the variable is negative (and c is positive),
4609 * then the coefficient of x in v is set to ceil(c/m).
4611 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4612 int *signs)
4614 int i, j;
4615 int total;
4616 isl_vec *v = NULL;
4617 struct isl_upoly *s;
4619 qp = isl_qpolynomial_cow(qp);
4620 if (!qp)
4621 return NULL;
4622 qp->div = isl_mat_cow(qp->div);
4623 if (!qp->div)
4624 goto error;
4626 total = isl_space_dim(qp->dim, isl_dim_all);
4627 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4629 for (i = 0; i < qp->div->n_row; ++i) {
4630 isl_int *row = qp->div->row[i];
4631 v = isl_vec_clr(v);
4632 if (!v)
4633 goto error;
4634 if (isl_int_lt(row[1], row[0])) {
4635 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4636 isl_int_sub_ui(v->el[0], v->el[0], 1);
4637 isl_int_submul(row[1], row[0], v->el[0]);
4639 for (j = 0; j < total; ++j) {
4640 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4641 continue;
4642 if (signs[j] < 0)
4643 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4644 else
4645 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4646 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4648 for (j = 0; j < i; ++j) {
4649 if (isl_int_sgn(row[2 + total + j]) >= 0)
4650 continue;
4651 isl_int_fdiv_q(v->el[1 + total + j],
4652 row[2 + total + j], row[0]);
4653 isl_int_submul(row[2 + total + j],
4654 row[0], v->el[1 + total + j]);
4656 for (j = i + 1; j < qp->div->n_row; ++j) {
4657 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4658 continue;
4659 isl_seq_combine(qp->div->row[j] + 1,
4660 qp->div->ctx->one, qp->div->row[j] + 1,
4661 qp->div->row[j][2 + total + i], v->el, v->size);
4663 isl_int_set_si(v->el[1 + total + i], 1);
4664 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4665 qp->div->ctx->one, v->size);
4666 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4667 isl_upoly_free(s);
4668 if (!qp->upoly)
4669 goto error;
4672 isl_vec_free(v);
4673 return qp;
4674 error:
4675 isl_vec_free(v);
4676 isl_qpolynomial_free(qp);
4677 return NULL;
4680 struct isl_to_poly_data {
4681 int sign;
4682 isl_pw_qpolynomial *res;
4683 isl_qpolynomial *qp;
4686 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4687 * We first make all integer divisions positive and then split the
4688 * quasipolynomials into terms with sign data->sign (the direction
4689 * of the requested approximation) and terms with the opposite sign.
4690 * In the first set of terms, each integer division [a/m] is
4691 * overapproximated by a/m, while in the second it is underapproximated
4692 * by (a-(m-1))/m.
4694 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4695 void *user)
4697 struct isl_to_poly_data *data = user;
4698 isl_pw_qpolynomial *t;
4699 isl_qpolynomial *qp, *up, *down;
4701 qp = isl_qpolynomial_copy(data->qp);
4702 qp = make_divs_pos(qp, signs);
4704 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4705 up = qp_drop_floors(up, 0);
4706 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4707 down = qp_drop_floors(down, 1);
4709 isl_qpolynomial_free(qp);
4710 qp = isl_qpolynomial_add(up, down);
4712 t = isl_pw_qpolynomial_alloc(orthant, qp);
4713 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4715 return 0;
4718 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4719 * the polynomial will be an overapproximation. If "sign" is negative,
4720 * it will be an underapproximation. If "sign" is zero, the approximation
4721 * will lie somewhere in between.
4723 * In particular, is sign == 0, we simply drop the floors, turning
4724 * the integer divisions into rational divisions.
4725 * Otherwise, we split the domains into orthants, make all integer divisions
4726 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4727 * depending on the requested sign and the sign of the term in which
4728 * the integer division appears.
4730 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4731 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4733 int i;
4734 struct isl_to_poly_data data;
4736 if (sign == 0)
4737 return pwqp_drop_floors(pwqp);
4739 if (!pwqp)
4740 return NULL;
4742 data.sign = sign;
4743 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4745 for (i = 0; i < pwqp->n; ++i) {
4746 if (pwqp->p[i].qp->div->n_row == 0) {
4747 isl_pw_qpolynomial *t;
4748 t = isl_pw_qpolynomial_alloc(
4749 isl_set_copy(pwqp->p[i].set),
4750 isl_qpolynomial_copy(pwqp->p[i].qp));
4751 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4752 continue;
4754 data.qp = pwqp->p[i].qp;
4755 if (isl_set_foreach_orthant(pwqp->p[i].set,
4756 &to_polynomial_on_orthant, &data) < 0)
4757 goto error;
4760 isl_pw_qpolynomial_free(pwqp);
4762 return data.res;
4763 error:
4764 isl_pw_qpolynomial_free(pwqp);
4765 isl_pw_qpolynomial_free(data.res);
4766 return NULL;
4769 static int poly_entry(void **entry, void *user)
4771 int *sign = user;
4772 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4774 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4776 return *pwqp ? 0 : -1;
4779 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4780 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4782 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4783 if (!upwqp)
4784 return NULL;
4786 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4787 &poly_entry, &sign) < 0)
4788 goto error;
4790 return upwqp;
4791 error:
4792 isl_union_pw_qpolynomial_free(upwqp);
4793 return NULL;
4796 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4797 __isl_take isl_qpolynomial *qp)
4799 int i, k;
4800 isl_space *dim;
4801 isl_vec *aff = NULL;
4802 isl_basic_map *bmap = NULL;
4803 unsigned pos;
4804 unsigned n_div;
4806 if (!qp)
4807 return NULL;
4808 if (!isl_upoly_is_affine(qp->upoly))
4809 isl_die(qp->dim->ctx, isl_error_invalid,
4810 "input quasi-polynomial not affine", goto error);
4811 aff = isl_qpolynomial_extract_affine(qp);
4812 if (!aff)
4813 goto error;
4814 dim = isl_qpolynomial_get_space(qp);
4815 pos = 1 + isl_space_offset(dim, isl_dim_out);
4816 n_div = qp->div->n_row;
4817 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4819 for (i = 0; i < n_div; ++i) {
4820 k = isl_basic_map_alloc_div(bmap);
4821 if (k < 0)
4822 goto error;
4823 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4824 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4825 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4826 goto error;
4828 k = isl_basic_map_alloc_equality(bmap);
4829 if (k < 0)
4830 goto error;
4831 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4832 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4833 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4835 isl_vec_free(aff);
4836 isl_qpolynomial_free(qp);
4837 bmap = isl_basic_map_finalize(bmap);
4838 return bmap;
4839 error:
4840 isl_vec_free(aff);
4841 isl_qpolynomial_free(qp);
4842 isl_basic_map_free(bmap);
4843 return NULL;