isl_basic_map_add_div_constraints: use isl_basic_map_var_offset
[isl.git] / isl_polynomial.c
blob17c8be820a1b75e97daf11f81bde0950091fc16d
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 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
16 #include <isl_seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.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>
31 #undef BASE
32 #define BASE pw_qpolynomial
34 #include <isl_list_templ.c>
36 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
38 switch (type) {
39 case isl_dim_param: return 0;
40 case isl_dim_in: return dim->nparam;
41 case isl_dim_out: return dim->nparam + dim->n_in;
42 default: return 0;
46 isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
48 if (!poly)
49 return isl_bool_error;
51 return poly->var < 0;
54 __isl_keep isl_poly_cst *isl_poly_as_cst(__isl_keep isl_poly *poly)
56 if (!poly)
57 return NULL;
59 isl_assert(poly->ctx, poly->var < 0, return NULL);
61 return (isl_poly_cst *) poly;
64 __isl_keep isl_poly_rec *isl_poly_as_rec(__isl_keep isl_poly *poly)
66 if (!poly)
67 return NULL;
69 isl_assert(poly->ctx, poly->var >= 0, return NULL);
71 return (isl_poly_rec *) poly;
74 /* Compare two polynomials.
76 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
77 * than "poly2" and 0 if they are equal.
79 static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1,
80 __isl_keep isl_poly *poly2)
82 int i;
83 isl_bool is_cst1;
84 isl_poly_rec *rec1, *rec2;
86 if (poly1 == poly2)
87 return 0;
88 is_cst1 = isl_poly_is_cst(poly1);
89 if (is_cst1 < 0)
90 return -1;
91 if (!poly2)
92 return 1;
93 if (poly1->var != poly2->var)
94 return poly1->var - poly2->var;
96 if (is_cst1) {
97 isl_poly_cst *cst1, *cst2;
98 int cmp;
100 cst1 = isl_poly_as_cst(poly1);
101 cst2 = isl_poly_as_cst(poly2);
102 if (!cst1 || !cst2)
103 return 0;
104 cmp = isl_int_cmp(cst1->n, cst2->n);
105 if (cmp != 0)
106 return cmp;
107 return isl_int_cmp(cst1->d, cst2->d);
110 rec1 = isl_poly_as_rec(poly1);
111 rec2 = isl_poly_as_rec(poly2);
112 if (!rec1 || !rec2)
113 return 0;
115 if (rec1->n != rec2->n)
116 return rec1->n - rec2->n;
118 for (i = 0; i < rec1->n; ++i) {
119 int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
120 if (cmp != 0)
121 return cmp;
124 return 0;
127 isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1,
128 __isl_keep isl_poly *poly2)
130 int i;
131 isl_bool is_cst1;
132 isl_poly_rec *rec1, *rec2;
134 is_cst1 = isl_poly_is_cst(poly1);
135 if (is_cst1 < 0 || !poly2)
136 return isl_bool_error;
137 if (poly1 == poly2)
138 return isl_bool_true;
139 if (poly1->var != poly2->var)
140 return isl_bool_false;
141 if (is_cst1) {
142 isl_poly_cst *cst1, *cst2;
143 cst1 = isl_poly_as_cst(poly1);
144 cst2 = isl_poly_as_cst(poly2);
145 if (!cst1 || !cst2)
146 return isl_bool_error;
147 return isl_int_eq(cst1->n, cst2->n) &&
148 isl_int_eq(cst1->d, cst2->d);
151 rec1 = isl_poly_as_rec(poly1);
152 rec2 = isl_poly_as_rec(poly2);
153 if (!rec1 || !rec2)
154 return isl_bool_error;
156 if (rec1->n != rec2->n)
157 return isl_bool_false;
159 for (i = 0; i < rec1->n; ++i) {
160 isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
161 if (eq < 0 || !eq)
162 return eq;
165 return isl_bool_true;
168 isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
170 isl_bool is_cst;
171 isl_poly_cst *cst;
173 is_cst = isl_poly_is_cst(poly);
174 if (is_cst < 0 || !is_cst)
175 return is_cst;
177 cst = isl_poly_as_cst(poly);
178 if (!cst)
179 return isl_bool_error;
181 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
184 int isl_poly_sgn(__isl_keep isl_poly *poly)
186 isl_bool is_cst;
187 isl_poly_cst *cst;
189 is_cst = isl_poly_is_cst(poly);
190 if (is_cst < 0 || !is_cst)
191 return 0;
193 cst = isl_poly_as_cst(poly);
194 if (!cst)
195 return 0;
197 return isl_int_sgn(cst->n);
200 isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
202 isl_bool is_cst;
203 isl_poly_cst *cst;
205 is_cst = isl_poly_is_cst(poly);
206 if (is_cst < 0 || !is_cst)
207 return is_cst;
209 cst = isl_poly_as_cst(poly);
210 if (!cst)
211 return isl_bool_error;
213 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
216 isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
218 isl_bool is_cst;
219 isl_poly_cst *cst;
221 is_cst = isl_poly_is_cst(poly);
222 if (is_cst < 0 || !is_cst)
223 return is_cst;
225 cst = isl_poly_as_cst(poly);
226 if (!cst)
227 return isl_bool_error;
229 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
232 isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
234 isl_bool is_cst;
235 isl_poly_cst *cst;
237 is_cst = isl_poly_is_cst(poly);
238 if (is_cst < 0 || !is_cst)
239 return is_cst;
241 cst = isl_poly_as_cst(poly);
242 if (!cst)
243 return isl_bool_error;
245 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
248 isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
250 isl_bool is_cst;
251 isl_poly_cst *cst;
253 is_cst = isl_poly_is_cst(poly);
254 if (is_cst < 0 || !is_cst)
255 return is_cst;
257 cst = isl_poly_as_cst(poly);
258 if (!cst)
259 return isl_bool_error;
261 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
264 isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
266 isl_bool is_cst;
267 isl_poly_cst *cst;
269 is_cst = isl_poly_is_cst(poly);
270 if (is_cst < 0 || !is_cst)
271 return is_cst;
273 cst = isl_poly_as_cst(poly);
274 if (!cst)
275 return isl_bool_error;
277 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
280 __isl_give isl_poly_cst *isl_poly_cst_alloc(isl_ctx *ctx)
282 isl_poly_cst *cst;
284 cst = isl_alloc_type(ctx, struct isl_poly_cst);
285 if (!cst)
286 return NULL;
288 cst->poly.ref = 1;
289 cst->poly.ctx = ctx;
290 isl_ctx_ref(ctx);
291 cst->poly.var = -1;
293 isl_int_init(cst->n);
294 isl_int_init(cst->d);
296 return cst;
299 __isl_give isl_poly *isl_poly_zero(isl_ctx *ctx)
301 isl_poly_cst *cst;
303 cst = isl_poly_cst_alloc(ctx);
304 if (!cst)
305 return NULL;
307 isl_int_set_si(cst->n, 0);
308 isl_int_set_si(cst->d, 1);
310 return &cst->poly;
313 __isl_give isl_poly *isl_poly_one(isl_ctx *ctx)
315 isl_poly_cst *cst;
317 cst = isl_poly_cst_alloc(ctx);
318 if (!cst)
319 return NULL;
321 isl_int_set_si(cst->n, 1);
322 isl_int_set_si(cst->d, 1);
324 return &cst->poly;
327 __isl_give isl_poly *isl_poly_infty(isl_ctx *ctx)
329 isl_poly_cst *cst;
331 cst = isl_poly_cst_alloc(ctx);
332 if (!cst)
333 return NULL;
335 isl_int_set_si(cst->n, 1);
336 isl_int_set_si(cst->d, 0);
338 return &cst->poly;
341 __isl_give isl_poly *isl_poly_neginfty(isl_ctx *ctx)
343 isl_poly_cst *cst;
345 cst = isl_poly_cst_alloc(ctx);
346 if (!cst)
347 return NULL;
349 isl_int_set_si(cst->n, -1);
350 isl_int_set_si(cst->d, 0);
352 return &cst->poly;
355 __isl_give isl_poly *isl_poly_nan(isl_ctx *ctx)
357 isl_poly_cst *cst;
359 cst = isl_poly_cst_alloc(ctx);
360 if (!cst)
361 return NULL;
363 isl_int_set_si(cst->n, 0);
364 isl_int_set_si(cst->d, 0);
366 return &cst->poly;
369 __isl_give isl_poly *isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
371 isl_poly_cst *cst;
373 cst = isl_poly_cst_alloc(ctx);
374 if (!cst)
375 return NULL;
377 isl_int_set(cst->n, n);
378 isl_int_set(cst->d, d);
380 return &cst->poly;
383 __isl_give isl_poly_rec *isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
385 isl_poly_rec *rec;
387 isl_assert(ctx, var >= 0, return NULL);
388 isl_assert(ctx, size >= 0, return NULL);
389 rec = isl_calloc(ctx, struct isl_poly_rec,
390 sizeof(struct isl_poly_rec) +
391 size * sizeof(struct isl_poly *));
392 if (!rec)
393 return NULL;
395 rec->poly.ref = 1;
396 rec->poly.ctx = ctx;
397 isl_ctx_ref(ctx);
398 rec->poly.var = var;
400 rec->n = 0;
401 rec->size = size;
403 return rec;
406 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
407 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
409 qp = isl_qpolynomial_cow(qp);
410 if (!qp || !dim)
411 goto error;
413 isl_space_free(qp->dim);
414 qp->dim = dim;
416 return qp;
417 error:
418 isl_qpolynomial_free(qp);
419 isl_space_free(dim);
420 return NULL;
423 /* Reset the space of "qp". This function is called from isl_pw_templ.c
424 * and doesn't know if the space of an element object is represented
425 * directly or through its domain. It therefore passes along both.
427 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
428 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
429 __isl_take isl_space *domain)
431 isl_space_free(space);
432 return isl_qpolynomial_reset_domain_space(qp, domain);
435 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
437 return qp ? qp->dim->ctx : NULL;
440 __isl_give isl_space *isl_qpolynomial_get_domain_space(
441 __isl_keep isl_qpolynomial *qp)
443 return qp ? isl_space_copy(qp->dim) : NULL;
446 /* Return a copy of the local space on which "qp" is defined.
448 static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
449 __isl_keep isl_qpolynomial *qp)
451 isl_space *space;
453 if (!qp)
454 return NULL;
456 space = isl_qpolynomial_get_domain_space(qp);
457 return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
460 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
462 isl_space *space;
463 if (!qp)
464 return NULL;
465 space = isl_space_copy(qp->dim);
466 space = isl_space_from_domain(space);
467 space = isl_space_add_dims(space, isl_dim_out, 1);
468 return space;
471 /* Return the number of variables of the given type in the domain of "qp".
473 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
474 enum isl_dim_type type)
476 if (!qp)
477 return 0;
478 if (type == isl_dim_div)
479 return qp->div->n_row;
480 if (type == isl_dim_all)
481 return isl_space_dim(qp->dim, isl_dim_all) +
482 isl_qpolynomial_domain_dim(qp, isl_dim_div);
483 return isl_space_dim(qp->dim, type);
486 /* Given the type of a dimension of an isl_qpolynomial,
487 * return the type of the corresponding dimension in its domain.
488 * This function is only called for "type" equal to isl_dim_in or
489 * isl_dim_param.
491 static enum isl_dim_type domain_type(enum isl_dim_type type)
493 return type == isl_dim_in ? isl_dim_set : type;
496 /* Externally, an isl_qpolynomial has a map space, but internally, the
497 * ls field corresponds to the domain of that space.
499 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
500 enum isl_dim_type type)
502 if (!qp)
503 return 0;
504 if (type == isl_dim_out)
505 return 1;
506 type = domain_type(type);
507 return isl_qpolynomial_domain_dim(qp, type);
510 /* Return the offset of the first coefficient of type "type" in
511 * the domain of "qp".
513 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
514 enum isl_dim_type type)
516 if (!qp)
517 return 0;
518 switch (type) {
519 case isl_dim_cst:
520 return 0;
521 case isl_dim_param:
522 case isl_dim_set:
523 return 1 + isl_space_offset(qp->dim, type);
524 case isl_dim_div:
525 return 1 + isl_space_dim(qp->dim, isl_dim_all);
526 default:
527 return 0;
531 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
533 return qp ? isl_poly_is_zero(qp->poly) : isl_bool_error;
536 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
538 return qp ? isl_poly_is_one(qp->poly) : isl_bool_error;
541 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
543 return qp ? isl_poly_is_nan(qp->poly) : isl_bool_error;
546 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
548 return qp ? isl_poly_is_infty(qp->poly) : isl_bool_error;
551 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
553 return qp ? isl_poly_is_neginfty(qp->poly) : isl_bool_error;
556 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
558 return qp ? isl_poly_sgn(qp->poly) : 0;
561 static void poly_free_cst(__isl_take isl_poly_cst *cst)
563 isl_int_clear(cst->n);
564 isl_int_clear(cst->d);
567 static void poly_free_rec(__isl_take isl_poly_rec *rec)
569 int i;
571 for (i = 0; i < rec->n; ++i)
572 isl_poly_free(rec->p[i]);
575 __isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
577 if (!poly)
578 return NULL;
580 poly->ref++;
581 return poly;
584 __isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
586 isl_poly_cst *cst;
587 isl_poly_cst *dup;
589 cst = isl_poly_as_cst(poly);
590 if (!cst)
591 return NULL;
593 dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
594 if (!dup)
595 return NULL;
596 isl_int_set(dup->n, cst->n);
597 isl_int_set(dup->d, cst->d);
599 return &dup->poly;
602 __isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
604 int i;
605 isl_poly_rec *rec;
606 isl_poly_rec *dup;
608 rec = isl_poly_as_rec(poly);
609 if (!rec)
610 return NULL;
612 dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
613 if (!dup)
614 return NULL;
616 for (i = 0; i < rec->n; ++i) {
617 dup->p[i] = isl_poly_copy(rec->p[i]);
618 if (!dup->p[i])
619 goto error;
620 dup->n++;
623 return &dup->poly;
624 error:
625 isl_poly_free(&dup->poly);
626 return NULL;
629 __isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
631 isl_bool is_cst;
633 is_cst = isl_poly_is_cst(poly);
634 if (is_cst < 0)
635 return NULL;
636 if (is_cst)
637 return isl_poly_dup_cst(poly);
638 else
639 return isl_poly_dup_rec(poly);
642 __isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
644 if (!poly)
645 return NULL;
647 if (poly->ref == 1)
648 return poly;
649 poly->ref--;
650 return isl_poly_dup(poly);
653 __isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
655 if (!poly)
656 return NULL;
658 if (--poly->ref > 0)
659 return NULL;
661 if (poly->var < 0)
662 poly_free_cst((isl_poly_cst *) poly);
663 else
664 poly_free_rec((isl_poly_rec *) poly);
666 isl_ctx_deref(poly->ctx);
667 free(poly);
668 return NULL;
671 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
673 isl_int gcd;
675 isl_int_init(gcd);
676 isl_int_gcd(gcd, cst->n, cst->d);
677 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
678 isl_int_divexact(cst->n, cst->n, gcd);
679 isl_int_divexact(cst->d, cst->d, gcd);
681 isl_int_clear(gcd);
684 __isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
685 __isl_take isl_poly *poly2)
687 isl_poly_cst *cst1;
688 isl_poly_cst *cst2;
690 poly1 = isl_poly_cow(poly1);
691 if (!poly1 || !poly2)
692 goto error;
694 cst1 = isl_poly_as_cst(poly1);
695 cst2 = isl_poly_as_cst(poly2);
697 if (isl_int_eq(cst1->d, cst2->d))
698 isl_int_add(cst1->n, cst1->n, cst2->n);
699 else {
700 isl_int_mul(cst1->n, cst1->n, cst2->d);
701 isl_int_addmul(cst1->n, cst2->n, cst1->d);
702 isl_int_mul(cst1->d, cst1->d, cst2->d);
705 isl_poly_cst_reduce(cst1);
707 isl_poly_free(poly2);
708 return poly1;
709 error:
710 isl_poly_free(poly1);
711 isl_poly_free(poly2);
712 return NULL;
715 static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
717 struct isl_ctx *ctx;
719 if (!poly)
720 return NULL;
721 ctx = poly->ctx;
722 isl_poly_free(poly);
723 return isl_poly_zero(ctx);
726 static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
728 isl_poly_rec *rec;
729 isl_poly *cst;
731 if (!poly)
732 return NULL;
734 rec = isl_poly_as_rec(poly);
735 if (!rec)
736 goto error;
737 cst = isl_poly_copy(rec->p[0]);
738 isl_poly_free(poly);
739 return cst;
740 error:
741 isl_poly_free(poly);
742 return NULL;
745 __isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
746 __isl_take isl_poly *poly2)
748 int i;
749 isl_bool is_zero, is_nan, is_cst;
750 isl_poly_rec *rec1, *rec2;
752 if (!poly1 || !poly2)
753 goto error;
755 is_nan = isl_poly_is_nan(poly1);
756 if (is_nan < 0)
757 goto error;
758 if (is_nan) {
759 isl_poly_free(poly2);
760 return poly1;
763 is_nan = isl_poly_is_nan(poly2);
764 if (is_nan < 0)
765 goto error;
766 if (is_nan) {
767 isl_poly_free(poly1);
768 return poly2;
771 is_zero = isl_poly_is_zero(poly1);
772 if (is_zero < 0)
773 goto error;
774 if (is_zero) {
775 isl_poly_free(poly1);
776 return poly2;
779 is_zero = isl_poly_is_zero(poly2);
780 if (is_zero < 0)
781 goto error;
782 if (is_zero) {
783 isl_poly_free(poly2);
784 return poly1;
787 if (poly1->var < poly2->var)
788 return isl_poly_sum(poly2, poly1);
790 if (poly2->var < poly1->var) {
791 isl_poly_rec *rec;
792 isl_bool is_infty;
794 is_infty = isl_poly_is_infty(poly2);
795 if (is_infty >= 0 && !is_infty)
796 is_infty = isl_poly_is_neginfty(poly2);
797 if (is_infty < 0)
798 goto error;
799 if (is_infty) {
800 isl_poly_free(poly1);
801 return poly2;
803 poly1 = isl_poly_cow(poly1);
804 rec = isl_poly_as_rec(poly1);
805 if (!rec)
806 goto error;
807 rec->p[0] = isl_poly_sum(rec->p[0], poly2);
808 if (rec->n == 1)
809 poly1 = replace_by_constant_term(poly1);
810 return poly1;
813 is_cst = isl_poly_is_cst(poly1);
814 if (is_cst < 0)
815 goto error;
816 if (is_cst)
817 return isl_poly_sum_cst(poly1, poly2);
819 rec1 = isl_poly_as_rec(poly1);
820 rec2 = isl_poly_as_rec(poly2);
821 if (!rec1 || !rec2)
822 goto error;
824 if (rec1->n < rec2->n)
825 return isl_poly_sum(poly2, poly1);
827 poly1 = isl_poly_cow(poly1);
828 rec1 = isl_poly_as_rec(poly1);
829 if (!rec1)
830 goto error;
832 for (i = rec2->n - 1; i >= 0; --i) {
833 isl_bool is_zero;
835 rec1->p[i] = isl_poly_sum(rec1->p[i],
836 isl_poly_copy(rec2->p[i]));
837 if (!rec1->p[i])
838 goto error;
839 if (i != rec1->n - 1)
840 continue;
841 is_zero = isl_poly_is_zero(rec1->p[i]);
842 if (is_zero < 0)
843 goto error;
844 if (is_zero) {
845 isl_poly_free(rec1->p[i]);
846 rec1->n--;
850 if (rec1->n == 0)
851 poly1 = replace_by_zero(poly1);
852 else if (rec1->n == 1)
853 poly1 = replace_by_constant_term(poly1);
855 isl_poly_free(poly2);
857 return poly1;
858 error:
859 isl_poly_free(poly1);
860 isl_poly_free(poly2);
861 return NULL;
864 __isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
865 isl_int v)
867 isl_poly_cst *cst;
869 poly = isl_poly_cow(poly);
870 if (!poly)
871 return NULL;
873 cst = isl_poly_as_cst(poly);
875 isl_int_addmul(cst->n, cst->d, v);
877 return poly;
880 __isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
882 isl_bool is_cst;
883 isl_poly_rec *rec;
885 is_cst = isl_poly_is_cst(poly);
886 if (is_cst < 0)
887 return isl_poly_free(poly);
888 if (is_cst)
889 return isl_poly_cst_add_isl_int(poly, v);
891 poly = isl_poly_cow(poly);
892 rec = isl_poly_as_rec(poly);
893 if (!rec)
894 goto error;
896 rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
897 if (!rec->p[0])
898 goto error;
900 return poly;
901 error:
902 isl_poly_free(poly);
903 return NULL;
906 __isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
907 isl_int v)
909 isl_bool is_zero;
910 isl_poly_cst *cst;
912 is_zero = isl_poly_is_zero(poly);
913 if (is_zero < 0)
914 return isl_poly_free(poly);
915 if (is_zero)
916 return poly;
918 poly = isl_poly_cow(poly);
919 if (!poly)
920 return NULL;
922 cst = isl_poly_as_cst(poly);
924 isl_int_mul(cst->n, cst->n, v);
926 return poly;
929 __isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
931 int i;
932 isl_bool is_cst;
933 isl_poly_rec *rec;
935 is_cst = isl_poly_is_cst(poly);
936 if (is_cst < 0)
937 return isl_poly_free(poly);
938 if (is_cst)
939 return isl_poly_cst_mul_isl_int(poly, v);
941 poly = isl_poly_cow(poly);
942 rec = isl_poly_as_rec(poly);
943 if (!rec)
944 goto error;
946 for (i = 0; i < rec->n; ++i) {
947 rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
948 if (!rec->p[i])
949 goto error;
952 return poly;
953 error:
954 isl_poly_free(poly);
955 return NULL;
958 /* Multiply the constant polynomial "poly" by "v".
960 static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
961 __isl_keep isl_val *v)
963 isl_bool is_zero;
964 isl_poly_cst *cst;
966 is_zero = isl_poly_is_zero(poly);
967 if (is_zero < 0)
968 return isl_poly_free(poly);
969 if (is_zero)
970 return poly;
972 poly = isl_poly_cow(poly);
973 if (!poly)
974 return NULL;
976 cst = isl_poly_as_cst(poly);
978 isl_int_mul(cst->n, cst->n, v->n);
979 isl_int_mul(cst->d, cst->d, v->d);
980 isl_poly_cst_reduce(cst);
982 return poly;
985 /* Multiply the polynomial "poly" by "v".
987 static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
988 __isl_keep isl_val *v)
990 int i;
991 isl_bool is_cst;
992 isl_poly_rec *rec;
994 is_cst = isl_poly_is_cst(poly);
995 if (is_cst < 0)
996 return isl_poly_free(poly);
997 if (is_cst)
998 return isl_poly_cst_scale_val(poly, v);
1000 poly = isl_poly_cow(poly);
1001 rec = isl_poly_as_rec(poly);
1002 if (!rec)
1003 goto error;
1005 for (i = 0; i < rec->n; ++i) {
1006 rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1007 if (!rec->p[i])
1008 goto error;
1011 return poly;
1012 error:
1013 isl_poly_free(poly);
1014 return NULL;
1017 __isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
1018 __isl_take isl_poly *poly2)
1020 isl_poly_cst *cst1;
1021 isl_poly_cst *cst2;
1023 poly1 = isl_poly_cow(poly1);
1024 if (!poly1 || !poly2)
1025 goto error;
1027 cst1 = isl_poly_as_cst(poly1);
1028 cst2 = isl_poly_as_cst(poly2);
1030 isl_int_mul(cst1->n, cst1->n, cst2->n);
1031 isl_int_mul(cst1->d, cst1->d, cst2->d);
1033 isl_poly_cst_reduce(cst1);
1035 isl_poly_free(poly2);
1036 return poly1;
1037 error:
1038 isl_poly_free(poly1);
1039 isl_poly_free(poly2);
1040 return NULL;
1043 __isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
1044 __isl_take isl_poly *poly2)
1046 isl_poly_rec *rec1;
1047 isl_poly_rec *rec2;
1048 isl_poly_rec *res = NULL;
1049 int i, j;
1050 int size;
1052 rec1 = isl_poly_as_rec(poly1);
1053 rec2 = isl_poly_as_rec(poly2);
1054 if (!rec1 || !rec2)
1055 goto error;
1056 size = rec1->n + rec2->n - 1;
1057 res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1058 if (!res)
1059 goto error;
1061 for (i = 0; i < rec1->n; ++i) {
1062 res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1063 isl_poly_copy(rec1->p[i]));
1064 if (!res->p[i])
1065 goto error;
1066 res->n++;
1068 for (; i < size; ++i) {
1069 res->p[i] = isl_poly_zero(poly1->ctx);
1070 if (!res->p[i])
1071 goto error;
1072 res->n++;
1074 for (i = 0; i < rec1->n; ++i) {
1075 for (j = 1; j < rec2->n; ++j) {
1076 isl_poly *poly;
1077 poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1078 isl_poly_copy(rec1->p[i]));
1079 res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1080 if (!res->p[i + j])
1081 goto error;
1085 isl_poly_free(poly1);
1086 isl_poly_free(poly2);
1088 return &res->poly;
1089 error:
1090 isl_poly_free(poly1);
1091 isl_poly_free(poly2);
1092 isl_poly_free(&res->poly);
1093 return NULL;
1096 __isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
1097 __isl_take isl_poly *poly2)
1099 isl_bool is_zero, is_nan, is_one, is_cst;
1101 if (!poly1 || !poly2)
1102 goto error;
1104 is_nan = isl_poly_is_nan(poly1);
1105 if (is_nan < 0)
1106 goto error;
1107 if (is_nan) {
1108 isl_poly_free(poly2);
1109 return poly1;
1112 is_nan = isl_poly_is_nan(poly2);
1113 if (is_nan < 0)
1114 goto error;
1115 if (is_nan) {
1116 isl_poly_free(poly1);
1117 return poly2;
1120 is_zero = isl_poly_is_zero(poly1);
1121 if (is_zero < 0)
1122 goto error;
1123 if (is_zero) {
1124 isl_poly_free(poly2);
1125 return poly1;
1128 is_zero = isl_poly_is_zero(poly2);
1129 if (is_zero < 0)
1130 goto error;
1131 if (is_zero) {
1132 isl_poly_free(poly1);
1133 return poly2;
1136 is_one = isl_poly_is_one(poly1);
1137 if (is_one < 0)
1138 goto error;
1139 if (is_one) {
1140 isl_poly_free(poly1);
1141 return poly2;
1144 is_one = isl_poly_is_one(poly2);
1145 if (is_one < 0)
1146 goto error;
1147 if (is_one) {
1148 isl_poly_free(poly2);
1149 return poly1;
1152 if (poly1->var < poly2->var)
1153 return isl_poly_mul(poly2, poly1);
1155 if (poly2->var < poly1->var) {
1156 int i;
1157 isl_poly_rec *rec;
1158 isl_bool is_infty;
1160 is_infty = isl_poly_is_infty(poly2);
1161 if (is_infty >= 0 && !is_infty)
1162 is_infty = isl_poly_is_neginfty(poly2);
1163 if (is_infty < 0)
1164 goto error;
1165 if (is_infty) {
1166 isl_ctx *ctx = poly1->ctx;
1167 isl_poly_free(poly1);
1168 isl_poly_free(poly2);
1169 return isl_poly_nan(ctx);
1171 poly1 = isl_poly_cow(poly1);
1172 rec = isl_poly_as_rec(poly1);
1173 if (!rec)
1174 goto error;
1176 for (i = 0; i < rec->n; ++i) {
1177 rec->p[i] = isl_poly_mul(rec->p[i],
1178 isl_poly_copy(poly2));
1179 if (!rec->p[i])
1180 goto error;
1182 isl_poly_free(poly2);
1183 return poly1;
1186 is_cst = isl_poly_is_cst(poly1);
1187 if (is_cst < 0)
1188 goto error;
1189 if (is_cst)
1190 return isl_poly_mul_cst(poly1, poly2);
1192 return isl_poly_mul_rec(poly1, poly2);
1193 error:
1194 isl_poly_free(poly1);
1195 isl_poly_free(poly2);
1196 return NULL;
1199 __isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
1201 isl_poly *res;
1203 if (!poly)
1204 return NULL;
1205 if (power == 1)
1206 return poly;
1208 if (power % 2)
1209 res = isl_poly_copy(poly);
1210 else
1211 res = isl_poly_one(poly->ctx);
1213 while (power >>= 1) {
1214 poly = isl_poly_mul(poly, isl_poly_copy(poly));
1215 if (power % 2)
1216 res = isl_poly_mul(res, isl_poly_copy(poly));
1219 isl_poly_free(poly);
1220 return res;
1223 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1224 unsigned n_div, __isl_take isl_poly *poly)
1226 struct isl_qpolynomial *qp = NULL;
1227 unsigned total;
1229 if (!space || !poly)
1230 goto error;
1232 if (!isl_space_is_set(space))
1233 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1234 "domain of polynomial should be a set", goto error);
1236 total = isl_space_dim(space, isl_dim_all);
1238 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1239 if (!qp)
1240 goto error;
1242 qp->ref = 1;
1243 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1244 if (!qp->div)
1245 goto error;
1247 qp->dim = space;
1248 qp->poly = poly;
1250 return qp;
1251 error:
1252 isl_space_free(space);
1253 isl_poly_free(poly);
1254 isl_qpolynomial_free(qp);
1255 return NULL;
1258 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1260 if (!qp)
1261 return NULL;
1263 qp->ref++;
1264 return qp;
1267 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1269 struct isl_qpolynomial *dup;
1271 if (!qp)
1272 return NULL;
1274 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1275 isl_poly_copy(qp->poly));
1276 if (!dup)
1277 return NULL;
1278 isl_mat_free(dup->div);
1279 dup->div = isl_mat_copy(qp->div);
1280 if (!dup->div)
1281 goto error;
1283 return dup;
1284 error:
1285 isl_qpolynomial_free(dup);
1286 return NULL;
1289 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1291 if (!qp)
1292 return NULL;
1294 if (qp->ref == 1)
1295 return qp;
1296 qp->ref--;
1297 return isl_qpolynomial_dup(qp);
1300 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1301 __isl_take isl_qpolynomial *qp)
1303 if (!qp)
1304 return NULL;
1306 if (--qp->ref > 0)
1307 return NULL;
1309 isl_space_free(qp->dim);
1310 isl_mat_free(qp->div);
1311 isl_poly_free(qp->poly);
1313 free(qp);
1314 return NULL;
1317 __isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
1319 int i;
1320 isl_poly_rec *rec;
1321 isl_poly_cst *cst;
1323 rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1324 if (!rec)
1325 return NULL;
1326 for (i = 0; i < 1 + power; ++i) {
1327 rec->p[i] = isl_poly_zero(ctx);
1328 if (!rec->p[i])
1329 goto error;
1330 rec->n++;
1332 cst = isl_poly_as_cst(rec->p[power]);
1333 isl_int_set_si(cst->n, 1);
1335 return &rec->poly;
1336 error:
1337 isl_poly_free(&rec->poly);
1338 return NULL;
1341 /* r array maps original positions to new positions.
1343 static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
1345 int i;
1346 isl_bool is_cst;
1347 isl_poly_rec *rec;
1348 isl_poly *base;
1349 isl_poly *res;
1351 is_cst = isl_poly_is_cst(poly);
1352 if (is_cst < 0)
1353 return isl_poly_free(poly);
1354 if (is_cst)
1355 return poly;
1357 rec = isl_poly_as_rec(poly);
1358 if (!rec)
1359 goto error;
1361 isl_assert(poly->ctx, rec->n >= 1, goto error);
1363 base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1364 res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1366 for (i = rec->n - 2; i >= 0; --i) {
1367 res = isl_poly_mul(res, isl_poly_copy(base));
1368 res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1371 isl_poly_free(base);
1372 isl_poly_free(poly);
1374 return res;
1375 error:
1376 isl_poly_free(poly);
1377 return NULL;
1380 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1381 __isl_keep isl_mat *div2)
1383 int n_row, n_col;
1384 isl_bool equal;
1386 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1387 div1->n_col >= div2->n_col,
1388 return isl_bool_error);
1390 if (div1->n_row == div2->n_row)
1391 return isl_mat_is_equal(div1, div2);
1393 n_row = div1->n_row;
1394 n_col = div1->n_col;
1395 div1->n_row = div2->n_row;
1396 div1->n_col = div2->n_col;
1398 equal = isl_mat_is_equal(div1, div2);
1400 div1->n_row = n_row;
1401 div1->n_col = n_col;
1403 return equal;
1406 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1408 int li, lj;
1410 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1411 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1413 if (li != lj)
1414 return li - lj;
1416 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1419 struct isl_div_sort_info {
1420 isl_mat *div;
1421 int row;
1424 static int div_sort_cmp(const void *p1, const void *p2)
1426 const struct isl_div_sort_info *i1, *i2;
1427 i1 = (const struct isl_div_sort_info *) p1;
1428 i2 = (const struct isl_div_sort_info *) p2;
1430 return cmp_row(i1->div, i1->row, i2->row);
1433 /* Sort divs and remove duplicates.
1435 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1437 int i;
1438 int skip;
1439 int len;
1440 struct isl_div_sort_info *array = NULL;
1441 int *pos = NULL, *at = NULL;
1442 int *reordering = NULL;
1443 unsigned div_pos;
1445 if (!qp)
1446 return NULL;
1447 if (qp->div->n_row <= 1)
1448 return qp;
1450 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1452 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1453 qp->div->n_row);
1454 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1455 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1456 len = qp->div->n_col - 2;
1457 reordering = isl_alloc_array(qp->div->ctx, int, len);
1458 if (!array || !pos || !at || !reordering)
1459 goto error;
1461 for (i = 0; i < qp->div->n_row; ++i) {
1462 array[i].div = qp->div;
1463 array[i].row = i;
1464 pos[i] = i;
1465 at[i] = i;
1468 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1469 div_sort_cmp);
1471 for (i = 0; i < div_pos; ++i)
1472 reordering[i] = i;
1474 for (i = 0; i < qp->div->n_row; ++i) {
1475 if (pos[array[i].row] == i)
1476 continue;
1477 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1478 pos[at[i]] = pos[array[i].row];
1479 at[pos[array[i].row]] = at[i];
1480 at[i] = array[i].row;
1481 pos[array[i].row] = i;
1484 skip = 0;
1485 for (i = 0; i < len - div_pos; ++i) {
1486 if (i > 0 &&
1487 isl_seq_eq(qp->div->row[i - skip - 1],
1488 qp->div->row[i - skip], qp->div->n_col)) {
1489 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1490 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1491 2 + div_pos + i - skip);
1492 qp->div = isl_mat_drop_cols(qp->div,
1493 2 + div_pos + i - skip, 1);
1494 skip++;
1496 reordering[div_pos + array[i].row] = div_pos + i - skip;
1499 qp->poly = reorder(qp->poly, reordering);
1501 if (!qp->poly || !qp->div)
1502 goto error;
1504 free(at);
1505 free(pos);
1506 free(array);
1507 free(reordering);
1509 return qp;
1510 error:
1511 free(at);
1512 free(pos);
1513 free(array);
1514 free(reordering);
1515 isl_qpolynomial_free(qp);
1516 return NULL;
1519 static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
1520 int first)
1522 int i;
1523 isl_bool is_cst;
1524 isl_poly_rec *rec;
1526 is_cst = isl_poly_is_cst(poly);
1527 if (is_cst < 0)
1528 return isl_poly_free(poly);
1529 if (is_cst)
1530 return poly;
1532 if (poly->var < first)
1533 return poly;
1535 if (exp[poly->var - first] == poly->var - first)
1536 return poly;
1538 poly = isl_poly_cow(poly);
1539 if (!poly)
1540 goto error;
1542 poly->var = exp[poly->var - first] + first;
1544 rec = isl_poly_as_rec(poly);
1545 if (!rec)
1546 goto error;
1548 for (i = 0; i < rec->n; ++i) {
1549 rec->p[i] = expand(rec->p[i], exp, first);
1550 if (!rec->p[i])
1551 goto error;
1554 return poly;
1555 error:
1556 isl_poly_free(poly);
1557 return NULL;
1560 static __isl_give isl_qpolynomial *with_merged_divs(
1561 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1562 __isl_take isl_qpolynomial *qp2),
1563 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1565 int *exp1 = NULL;
1566 int *exp2 = NULL;
1567 isl_mat *div = NULL;
1568 int n_div1, n_div2;
1570 qp1 = isl_qpolynomial_cow(qp1);
1571 qp2 = isl_qpolynomial_cow(qp2);
1573 if (!qp1 || !qp2)
1574 goto error;
1576 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1577 qp1->div->n_col >= qp2->div->n_col, goto error);
1579 n_div1 = qp1->div->n_row;
1580 n_div2 = qp2->div->n_row;
1581 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1582 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1583 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1584 goto error;
1586 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1587 if (!div)
1588 goto error;
1590 isl_mat_free(qp1->div);
1591 qp1->div = isl_mat_copy(div);
1592 isl_mat_free(qp2->div);
1593 qp2->div = isl_mat_copy(div);
1595 qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1596 qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1598 if (!qp1->poly || !qp2->poly)
1599 goto error;
1601 isl_mat_free(div);
1602 free(exp1);
1603 free(exp2);
1605 return fn(qp1, qp2);
1606 error:
1607 isl_mat_free(div);
1608 free(exp1);
1609 free(exp2);
1610 isl_qpolynomial_free(qp1);
1611 isl_qpolynomial_free(qp2);
1612 return NULL;
1615 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1616 __isl_take isl_qpolynomial *qp2)
1618 isl_bool compatible;
1620 qp1 = isl_qpolynomial_cow(qp1);
1622 if (!qp1 || !qp2)
1623 goto error;
1625 if (qp1->div->n_row < qp2->div->n_row)
1626 return isl_qpolynomial_add(qp2, qp1);
1628 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1629 compatible = compatible_divs(qp1->div, qp2->div);
1630 if (compatible < 0)
1631 goto error;
1632 if (!compatible)
1633 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1635 qp1->poly = isl_poly_sum(qp1->poly, isl_poly_copy(qp2->poly));
1636 if (!qp1->poly)
1637 goto error;
1639 isl_qpolynomial_free(qp2);
1641 return qp1;
1642 error:
1643 isl_qpolynomial_free(qp1);
1644 isl_qpolynomial_free(qp2);
1645 return NULL;
1648 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1649 __isl_keep isl_set *dom,
1650 __isl_take isl_qpolynomial *qp1,
1651 __isl_take isl_qpolynomial *qp2)
1653 qp1 = isl_qpolynomial_add(qp1, qp2);
1654 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1655 return qp1;
1658 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1659 __isl_take isl_qpolynomial *qp2)
1661 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1664 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1665 __isl_take isl_qpolynomial *qp, isl_int v)
1667 if (isl_int_is_zero(v))
1668 return qp;
1670 qp = isl_qpolynomial_cow(qp);
1671 if (!qp)
1672 return NULL;
1674 qp->poly = isl_poly_add_isl_int(qp->poly, v);
1675 if (!qp->poly)
1676 goto error;
1678 return qp;
1679 error:
1680 isl_qpolynomial_free(qp);
1681 return NULL;
1685 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1687 if (!qp)
1688 return NULL;
1690 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1693 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1694 __isl_take isl_qpolynomial *qp, isl_int v)
1696 if (isl_int_is_one(v))
1697 return qp;
1699 if (qp && isl_int_is_zero(v)) {
1700 isl_qpolynomial *zero;
1701 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1702 isl_qpolynomial_free(qp);
1703 return zero;
1706 qp = isl_qpolynomial_cow(qp);
1707 if (!qp)
1708 return NULL;
1710 qp->poly = isl_poly_mul_isl_int(qp->poly, v);
1711 if (!qp->poly)
1712 goto error;
1714 return qp;
1715 error:
1716 isl_qpolynomial_free(qp);
1717 return NULL;
1720 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1721 __isl_take isl_qpolynomial *qp, isl_int v)
1723 return isl_qpolynomial_mul_isl_int(qp, v);
1726 /* Multiply "qp" by "v".
1728 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1729 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1731 if (!qp || !v)
1732 goto error;
1734 if (!isl_val_is_rat(v))
1735 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1736 "expecting rational factor", goto error);
1738 if (isl_val_is_one(v)) {
1739 isl_val_free(v);
1740 return qp;
1743 if (isl_val_is_zero(v)) {
1744 isl_space *space;
1746 space = isl_qpolynomial_get_domain_space(qp);
1747 isl_qpolynomial_free(qp);
1748 isl_val_free(v);
1749 return isl_qpolynomial_zero_on_domain(space);
1752 qp = isl_qpolynomial_cow(qp);
1753 if (!qp)
1754 goto error;
1756 qp->poly = isl_poly_scale_val(qp->poly, v);
1757 if (!qp->poly)
1758 qp = isl_qpolynomial_free(qp);
1760 isl_val_free(v);
1761 return qp;
1762 error:
1763 isl_val_free(v);
1764 isl_qpolynomial_free(qp);
1765 return NULL;
1768 /* Divide "qp" by "v".
1770 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1771 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1773 if (!qp || !v)
1774 goto error;
1776 if (!isl_val_is_rat(v))
1777 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1778 "expecting rational factor", goto error);
1779 if (isl_val_is_zero(v))
1780 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1781 "cannot scale down by zero", goto error);
1783 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1784 error:
1785 isl_val_free(v);
1786 isl_qpolynomial_free(qp);
1787 return NULL;
1790 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1791 __isl_take isl_qpolynomial *qp2)
1793 isl_bool compatible;
1795 qp1 = isl_qpolynomial_cow(qp1);
1797 if (!qp1 || !qp2)
1798 goto error;
1800 if (qp1->div->n_row < qp2->div->n_row)
1801 return isl_qpolynomial_mul(qp2, qp1);
1803 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1804 compatible = compatible_divs(qp1->div, qp2->div);
1805 if (compatible < 0)
1806 goto error;
1807 if (!compatible)
1808 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1810 qp1->poly = isl_poly_mul(qp1->poly, isl_poly_copy(qp2->poly));
1811 if (!qp1->poly)
1812 goto error;
1814 isl_qpolynomial_free(qp2);
1816 return qp1;
1817 error:
1818 isl_qpolynomial_free(qp1);
1819 isl_qpolynomial_free(qp2);
1820 return NULL;
1823 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1824 unsigned power)
1826 qp = isl_qpolynomial_cow(qp);
1828 if (!qp)
1829 return NULL;
1831 qp->poly = isl_poly_pow(qp->poly, power);
1832 if (!qp->poly)
1833 goto error;
1835 return qp;
1836 error:
1837 isl_qpolynomial_free(qp);
1838 return NULL;
1841 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1842 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1844 int i;
1846 if (power == 1)
1847 return pwqp;
1849 pwqp = isl_pw_qpolynomial_cow(pwqp);
1850 if (!pwqp)
1851 return NULL;
1853 for (i = 0; i < pwqp->n; ++i) {
1854 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1855 if (!pwqp->p[i].qp)
1856 return isl_pw_qpolynomial_free(pwqp);
1859 return pwqp;
1862 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1863 __isl_take isl_space *domain)
1865 if (!domain)
1866 return NULL;
1867 return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
1870 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1871 __isl_take isl_space *domain)
1873 if (!domain)
1874 return NULL;
1875 return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
1878 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1879 __isl_take isl_space *domain)
1881 if (!domain)
1882 return NULL;
1883 return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
1886 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1887 __isl_take isl_space *domain)
1889 if (!domain)
1890 return NULL;
1891 return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
1894 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1895 __isl_take isl_space *domain)
1897 if (!domain)
1898 return NULL;
1899 return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
1902 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1903 __isl_take isl_space *domain,
1904 isl_int v)
1906 struct isl_qpolynomial *qp;
1907 isl_poly_cst *cst;
1909 qp = isl_qpolynomial_zero_on_domain(domain);
1910 if (!qp)
1911 return NULL;
1913 cst = isl_poly_as_cst(qp->poly);
1914 isl_int_set(cst->n, v);
1916 return qp;
1919 isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1920 isl_int *n, isl_int *d)
1922 isl_bool is_cst;
1923 isl_poly_cst *cst;
1925 if (!qp)
1926 return isl_bool_error;
1928 is_cst = isl_poly_is_cst(qp->poly);
1929 if (is_cst < 0 || !is_cst)
1930 return is_cst;
1932 cst = isl_poly_as_cst(qp->poly);
1933 if (!cst)
1934 return isl_bool_error;
1936 if (n)
1937 isl_int_set(*n, cst->n);
1938 if (d)
1939 isl_int_set(*d, cst->d);
1941 return isl_bool_true;
1944 /* Return the constant term of "poly".
1946 static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
1948 isl_bool is_cst;
1949 isl_poly_cst *cst;
1951 if (!poly)
1952 return NULL;
1954 while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
1955 isl_poly_rec *rec;
1957 rec = isl_poly_as_rec(poly);
1958 if (!rec)
1959 return NULL;
1960 poly = rec->p[0];
1962 if (is_cst < 0)
1963 return NULL;
1965 cst = isl_poly_as_cst(poly);
1966 if (!cst)
1967 return NULL;
1968 return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
1971 /* Return the constant term of "qp".
1973 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1974 __isl_keep isl_qpolynomial *qp)
1976 if (!qp)
1977 return NULL;
1979 return isl_poly_get_constant_val(qp->poly);
1982 isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
1984 isl_bool is_cst;
1985 isl_poly_rec *rec;
1987 if (!poly)
1988 return isl_bool_error;
1990 if (poly->var < 0)
1991 return isl_bool_true;
1993 rec = isl_poly_as_rec(poly);
1994 if (!rec)
1995 return isl_bool_error;
1997 if (rec->n > 2)
1998 return isl_bool_false;
2000 isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
2002 is_cst = isl_poly_is_cst(rec->p[1]);
2003 if (is_cst < 0 || !is_cst)
2004 return is_cst;
2006 return isl_poly_is_affine(rec->p[0]);
2009 isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
2011 if (!qp)
2012 return isl_bool_error;
2014 if (qp->div->n_row > 0)
2015 return isl_bool_false;
2017 return isl_poly_is_affine(qp->poly);
2020 static void update_coeff(__isl_keep isl_vec *aff,
2021 __isl_keep isl_poly_cst *cst, int pos)
2023 isl_int gcd;
2024 isl_int f;
2026 if (isl_int_is_zero(cst->n))
2027 return;
2029 isl_int_init(gcd);
2030 isl_int_init(f);
2031 isl_int_gcd(gcd, cst->d, aff->el[0]);
2032 isl_int_divexact(f, cst->d, gcd);
2033 isl_int_divexact(gcd, aff->el[0], gcd);
2034 isl_seq_scale(aff->el, aff->el, f, aff->size);
2035 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
2036 isl_int_clear(gcd);
2037 isl_int_clear(f);
2040 int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
2042 isl_poly_cst *cst;
2043 isl_poly_rec *rec;
2045 if (!poly || !aff)
2046 return -1;
2048 if (poly->var < 0) {
2049 isl_poly_cst *cst;
2051 cst = isl_poly_as_cst(poly);
2052 if (!cst)
2053 return -1;
2054 update_coeff(aff, cst, 0);
2055 return 0;
2058 rec = isl_poly_as_rec(poly);
2059 if (!rec)
2060 return -1;
2061 isl_assert(poly->ctx, rec->n == 2, return -1);
2063 cst = isl_poly_as_cst(rec->p[1]);
2064 if (!cst)
2065 return -1;
2066 update_coeff(aff, cst, 1 + poly->var);
2068 return isl_poly_update_affine(rec->p[0], aff);
2071 __isl_give isl_vec *isl_qpolynomial_extract_affine(
2072 __isl_keep isl_qpolynomial *qp)
2074 isl_vec *aff;
2075 unsigned d;
2077 if (!qp)
2078 return NULL;
2080 d = isl_space_dim(qp->dim, isl_dim_all);
2081 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
2082 if (!aff)
2083 return NULL;
2085 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
2086 isl_int_set_si(aff->el[0], 1);
2088 if (isl_poly_update_affine(qp->poly, aff) < 0)
2089 goto error;
2091 return aff;
2092 error:
2093 isl_vec_free(aff);
2094 return NULL;
2097 /* Compare two quasi-polynomials.
2099 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2100 * than "qp2" and 0 if they are equal.
2102 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2103 __isl_keep isl_qpolynomial *qp2)
2105 int cmp;
2107 if (qp1 == qp2)
2108 return 0;
2109 if (!qp1)
2110 return -1;
2111 if (!qp2)
2112 return 1;
2114 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2115 if (cmp != 0)
2116 return cmp;
2118 cmp = isl_local_cmp(qp1->div, qp2->div);
2119 if (cmp != 0)
2120 return cmp;
2122 return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2125 /* Is "qp1" obviously equal to "qp2"?
2127 * NaN is not equal to anything, not even to another NaN.
2129 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2130 __isl_keep isl_qpolynomial *qp2)
2132 isl_bool equal;
2134 if (!qp1 || !qp2)
2135 return isl_bool_error;
2137 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2138 return isl_bool_false;
2140 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2141 if (equal < 0 || !equal)
2142 return equal;
2144 equal = isl_mat_is_equal(qp1->div, qp2->div);
2145 if (equal < 0 || !equal)
2146 return equal;
2148 return isl_poly_is_equal(qp1->poly, qp2->poly);
2151 static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
2153 int i;
2154 isl_bool is_cst;
2155 isl_poly_rec *rec;
2157 is_cst = isl_poly_is_cst(poly);
2158 if (is_cst < 0)
2159 return isl_stat_error;
2160 if (is_cst) {
2161 isl_poly_cst *cst;
2162 cst = isl_poly_as_cst(poly);
2163 if (!cst)
2164 return isl_stat_error;
2165 isl_int_lcm(*d, *d, cst->d);
2166 return isl_stat_ok;
2169 rec = isl_poly_as_rec(poly);
2170 if (!rec)
2171 return isl_stat_error;
2173 for (i = 0; i < rec->n; ++i)
2174 poly_update_den(rec->p[i], d);
2176 return isl_stat_ok;
2179 __isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
2181 isl_val *d;
2183 if (!qp)
2184 return NULL;
2185 d = isl_val_one(isl_qpolynomial_get_ctx(qp));
2186 if (!d)
2187 return NULL;
2188 if (poly_update_den(qp->poly, &d->n) < 0)
2189 return isl_val_free(d);
2190 return d;
2193 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2194 __isl_take isl_space *domain, int pos, int power)
2196 struct isl_ctx *ctx;
2198 if (!domain)
2199 return NULL;
2201 ctx = domain->ctx;
2203 return isl_qpolynomial_alloc(domain, 0,
2204 isl_poly_var_pow(ctx, pos, power));
2207 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2208 __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2210 if (isl_space_check_is_set(domain ) < 0)
2211 goto error;
2212 if (isl_space_check_range(domain, type, pos, 1) < 0)
2213 goto error;
2215 if (type == isl_dim_set)
2216 pos += isl_space_dim(domain, isl_dim_param);
2218 return isl_qpolynomial_var_pow_on_domain(domain, pos, 1);
2219 error:
2220 isl_space_free(domain);
2221 return NULL;
2224 __isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
2225 unsigned first, unsigned n, __isl_keep isl_poly **subs)
2227 int i;
2228 isl_bool is_cst;
2229 isl_poly_rec *rec;
2230 isl_poly *base, *res;
2232 is_cst = isl_poly_is_cst(poly);
2233 if (is_cst < 0)
2234 return isl_poly_free(poly);
2235 if (is_cst)
2236 return poly;
2238 if (poly->var < first)
2239 return poly;
2241 rec = isl_poly_as_rec(poly);
2242 if (!rec)
2243 goto error;
2245 isl_assert(poly->ctx, rec->n >= 1, goto error);
2247 if (poly->var >= first + n)
2248 base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2249 else
2250 base = isl_poly_copy(subs[poly->var - first]);
2252 res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2253 for (i = rec->n - 2; i >= 0; --i) {
2254 isl_poly *t;
2255 t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2256 res = isl_poly_mul(res, isl_poly_copy(base));
2257 res = isl_poly_sum(res, t);
2260 isl_poly_free(base);
2261 isl_poly_free(poly);
2263 return res;
2264 error:
2265 isl_poly_free(poly);
2266 return NULL;
2269 __isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
2270 isl_int denom, unsigned len)
2272 int i;
2273 isl_poly *poly;
2275 isl_assert(ctx, len >= 1, return NULL);
2277 poly = isl_poly_rat_cst(ctx, f[0], denom);
2278 for (i = 0; i < len - 1; ++i) {
2279 isl_poly *t;
2280 isl_poly *c;
2282 if (isl_int_is_zero(f[1 + i]))
2283 continue;
2285 c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2286 t = isl_poly_var_pow(ctx, i, 1);
2287 t = isl_poly_mul(c, t);
2288 poly = isl_poly_sum(poly, t);
2291 return poly;
2294 /* Remove common factor of non-constant terms and denominator.
2296 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2298 isl_ctx *ctx = qp->div->ctx;
2299 unsigned total = qp->div->n_col - 2;
2301 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2302 isl_int_gcd(ctx->normalize_gcd,
2303 ctx->normalize_gcd, qp->div->row[div][0]);
2304 if (isl_int_is_one(ctx->normalize_gcd))
2305 return;
2307 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2308 ctx->normalize_gcd, total);
2309 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2310 ctx->normalize_gcd);
2311 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2312 ctx->normalize_gcd);
2315 /* Replace the integer division identified by "div" by the polynomial "s".
2316 * The integer division is assumed not to appear in the definition
2317 * of any other integer divisions.
2319 static __isl_give isl_qpolynomial *substitute_div(
2320 __isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
2322 int i;
2323 int total;
2324 int *reordering;
2326 if (!qp || !s)
2327 goto error;
2329 qp = isl_qpolynomial_cow(qp);
2330 if (!qp)
2331 goto error;
2333 total = isl_space_dim(qp->dim, isl_dim_all);
2334 qp->poly = isl_poly_subs(qp->poly, total + div, 1, &s);
2335 if (!qp->poly)
2336 goto error;
2338 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2339 if (!reordering)
2340 goto error;
2341 for (i = 0; i < total + div; ++i)
2342 reordering[i] = i;
2343 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2344 reordering[i] = i - 1;
2345 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2346 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2347 qp->poly = reorder(qp->poly, reordering);
2348 free(reordering);
2350 if (!qp->poly || !qp->div)
2351 goto error;
2353 isl_poly_free(s);
2354 return qp;
2355 error:
2356 isl_qpolynomial_free(qp);
2357 isl_poly_free(s);
2358 return NULL;
2361 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2362 * divisions because d is equal to 1 by their definition, i.e., e.
2364 static __isl_give isl_qpolynomial *substitute_non_divs(
2365 __isl_take isl_qpolynomial *qp)
2367 int i, j;
2368 int total;
2369 isl_poly *s;
2371 if (!qp)
2372 return NULL;
2374 total = isl_space_dim(qp->dim, isl_dim_all);
2375 for (i = 0; qp && i < qp->div->n_row; ++i) {
2376 if (!isl_int_is_one(qp->div->row[i][0]))
2377 continue;
2378 for (j = i + 1; j < qp->div->n_row; ++j) {
2379 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2380 continue;
2381 isl_seq_combine(qp->div->row[j] + 1,
2382 qp->div->ctx->one, qp->div->row[j] + 1,
2383 qp->div->row[j][2 + total + i],
2384 qp->div->row[i] + 1, 1 + total + i);
2385 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2386 normalize_div(qp, j);
2388 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2389 qp->div->row[i][0], qp->div->n_col - 1);
2390 qp = substitute_div(qp, i, s);
2391 --i;
2394 return qp;
2397 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2398 * with d the denominator. When replacing the coefficient e of x by
2399 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2400 * inside the division, so we need to add floor(e/d) * x outside.
2401 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2402 * to adjust the coefficient of x in each later div that depends on the
2403 * current div "div" and also in the affine expressions in the rows of "mat"
2404 * (if they too depend on "div").
2406 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2407 __isl_keep isl_mat **mat)
2409 int i, j;
2410 isl_int v;
2411 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2413 isl_int_init(v);
2414 for (i = 0; i < 1 + total + div; ++i) {
2415 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2416 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2417 continue;
2418 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2419 isl_int_fdiv_r(qp->div->row[div][1 + i],
2420 qp->div->row[div][1 + i], qp->div->row[div][0]);
2421 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2422 for (j = div + 1; j < qp->div->n_row; ++j) {
2423 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2424 continue;
2425 isl_int_addmul(qp->div->row[j][1 + i],
2426 v, qp->div->row[j][2 + total + div]);
2429 isl_int_clear(v);
2432 /* Check if the last non-zero coefficient is bigger that half of the
2433 * denominator. If so, we will invert the div to further reduce the number
2434 * of distinct divs that may appear.
2435 * If the last non-zero coefficient is exactly half the denominator,
2436 * then we continue looking for earlier coefficients that are bigger
2437 * than half the denominator.
2439 static int needs_invert(__isl_keep isl_mat *div, int row)
2441 int i;
2442 int cmp;
2444 for (i = div->n_col - 1; i >= 1; --i) {
2445 if (isl_int_is_zero(div->row[row][i]))
2446 continue;
2447 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2448 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2449 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2450 if (cmp)
2451 return cmp > 0;
2452 if (i == 1)
2453 return 1;
2456 return 0;
2459 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2460 * We only invert the coefficients of e (and the coefficient of q in
2461 * later divs and in the rows of "mat"). After calling this function, the
2462 * coefficients of e should be reduced again.
2464 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2465 __isl_keep isl_mat **mat)
2467 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2469 isl_seq_neg(qp->div->row[div] + 1,
2470 qp->div->row[div] + 1, qp->div->n_col - 1);
2471 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2472 isl_int_add(qp->div->row[div][1],
2473 qp->div->row[div][1], qp->div->row[div][0]);
2474 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2475 isl_mat_col_mul(qp->div, 2 + total + div,
2476 qp->div->ctx->negone, 2 + total + div);
2479 /* Reduce all divs of "qp" to have coefficients
2480 * in the interval [0, d-1], with d the denominator and such that the
2481 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2482 * The modifications to the integer divisions need to be reflected
2483 * in the factors of the polynomial that refer to the original
2484 * integer divisions. To this end, the modifications are collected
2485 * as a set of affine expressions and then plugged into the polynomial.
2487 * After the reduction, some divs may have become redundant or identical,
2488 * so we call substitute_non_divs and sort_divs. If these functions
2489 * eliminate divs or merge two or more divs into one, the coefficients
2490 * of the enclosing divs may have to be reduced again, so we call
2491 * ourselves recursively if the number of divs decreases.
2493 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2495 int i;
2496 isl_ctx *ctx;
2497 isl_mat *mat;
2498 isl_poly **s;
2499 unsigned o_div, n_div, total;
2501 if (!qp)
2502 return NULL;
2504 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2505 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2506 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2507 ctx = isl_qpolynomial_get_ctx(qp);
2508 mat = isl_mat_zero(ctx, n_div, 1 + total);
2510 for (i = 0; i < n_div; ++i)
2511 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2513 for (i = 0; i < qp->div->n_row; ++i) {
2514 normalize_div(qp, i);
2515 reduce_div(qp, i, &mat);
2516 if (needs_invert(qp->div, i)) {
2517 invert_div(qp, i, &mat);
2518 reduce_div(qp, i, &mat);
2521 if (!mat)
2522 goto error;
2524 s = isl_alloc_array(ctx, struct isl_poly *, n_div);
2525 if (n_div && !s)
2526 goto error;
2527 for (i = 0; i < n_div; ++i)
2528 s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2529 1 + total);
2530 qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2531 for (i = 0; i < n_div; ++i)
2532 isl_poly_free(s[i]);
2533 free(s);
2534 if (!qp->poly)
2535 goto error;
2537 isl_mat_free(mat);
2539 qp = substitute_non_divs(qp);
2540 qp = sort_divs(qp);
2541 if (qp && isl_qpolynomial_domain_dim(qp, isl_dim_div) < n_div)
2542 return reduce_divs(qp);
2544 return qp;
2545 error:
2546 isl_qpolynomial_free(qp);
2547 isl_mat_free(mat);
2548 return NULL;
2551 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2552 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2554 struct isl_qpolynomial *qp;
2555 isl_poly_cst *cst;
2557 qp = isl_qpolynomial_zero_on_domain(domain);
2558 if (!qp)
2559 return NULL;
2561 cst = isl_poly_as_cst(qp->poly);
2562 isl_int_set(cst->n, n);
2563 isl_int_set(cst->d, d);
2565 return qp;
2568 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2570 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2571 __isl_take isl_space *domain, __isl_take isl_val *val)
2573 isl_qpolynomial *qp;
2574 isl_poly_cst *cst;
2576 qp = isl_qpolynomial_zero_on_domain(domain);
2577 if (!qp || !val)
2578 goto error;
2580 cst = isl_poly_as_cst(qp->poly);
2581 isl_int_set(cst->n, val->n);
2582 isl_int_set(cst->d, val->d);
2584 isl_val_free(val);
2585 return qp;
2586 error:
2587 isl_val_free(val);
2588 isl_qpolynomial_free(qp);
2589 return NULL;
2592 static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
2594 isl_bool is_cst;
2595 isl_poly_rec *rec;
2596 int i;
2598 is_cst = isl_poly_is_cst(poly);
2599 if (is_cst < 0)
2600 return isl_stat_error;
2601 if (is_cst)
2602 return isl_stat_ok;
2604 if (poly->var < d)
2605 active[poly->var] = 1;
2607 rec = isl_poly_as_rec(poly);
2608 for (i = 0; i < rec->n; ++i)
2609 if (poly_set_active(rec->p[i], active, d) < 0)
2610 return isl_stat_error;
2612 return isl_stat_ok;
2615 static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
2617 int i, j;
2618 int d = isl_space_dim(qp->dim, isl_dim_all);
2620 if (!qp || !active)
2621 return isl_stat_error;
2623 for (i = 0; i < d; ++i)
2624 for (j = 0; j < qp->div->n_row; ++j) {
2625 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2626 continue;
2627 active[i] = 1;
2628 break;
2631 return poly_set_active(qp->poly, active, d);
2634 #undef TYPE
2635 #define TYPE isl_qpolynomial
2636 static
2637 #include "check_type_range_templ.c"
2639 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2640 enum isl_dim_type type, unsigned first, unsigned n)
2642 int i;
2643 int *active = NULL;
2644 isl_bool involves = isl_bool_false;
2646 if (!qp)
2647 return isl_bool_error;
2648 if (n == 0)
2649 return isl_bool_false;
2651 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2652 return isl_bool_error;
2653 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2654 type == isl_dim_in, return isl_bool_error);
2656 active = isl_calloc_array(qp->dim->ctx, int,
2657 isl_space_dim(qp->dim, isl_dim_all));
2658 if (set_active(qp, active) < 0)
2659 goto error;
2661 if (type == isl_dim_in)
2662 first += isl_space_dim(qp->dim, isl_dim_param);
2663 for (i = 0; i < n; ++i)
2664 if (active[first + i]) {
2665 involves = isl_bool_true;
2666 break;
2669 free(active);
2671 return involves;
2672 error:
2673 free(active);
2674 return isl_bool_error;
2677 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2678 * of the divs that do appear in the quasi-polynomial.
2680 static __isl_give isl_qpolynomial *remove_redundant_divs(
2681 __isl_take isl_qpolynomial *qp)
2683 int i, j;
2684 int d;
2685 int len;
2686 int skip;
2687 int *active = NULL;
2688 int *reordering = NULL;
2689 int redundant = 0;
2690 int n_div;
2691 isl_ctx *ctx;
2693 if (!qp)
2694 return NULL;
2695 if (qp->div->n_row == 0)
2696 return qp;
2698 d = isl_space_dim(qp->dim, isl_dim_all);
2699 len = qp->div->n_col - 2;
2700 ctx = isl_qpolynomial_get_ctx(qp);
2701 active = isl_calloc_array(ctx, int, len);
2702 if (!active)
2703 goto error;
2705 if (poly_set_active(qp->poly, active, len) < 0)
2706 goto error;
2708 for (i = qp->div->n_row - 1; i >= 0; --i) {
2709 if (!active[d + i]) {
2710 redundant = 1;
2711 continue;
2713 for (j = 0; j < i; ++j) {
2714 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2715 continue;
2716 active[d + j] = 1;
2717 break;
2721 if (!redundant) {
2722 free(active);
2723 return qp;
2726 reordering = isl_alloc_array(qp->div->ctx, int, len);
2727 if (!reordering)
2728 goto error;
2730 for (i = 0; i < d; ++i)
2731 reordering[i] = i;
2733 skip = 0;
2734 n_div = qp->div->n_row;
2735 for (i = 0; i < n_div; ++i) {
2736 if (!active[d + i]) {
2737 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2738 qp->div = isl_mat_drop_cols(qp->div,
2739 2 + d + i - skip, 1);
2740 skip++;
2742 reordering[d + i] = d + i - skip;
2745 qp->poly = reorder(qp->poly, reordering);
2747 if (!qp->poly || !qp->div)
2748 goto error;
2750 free(active);
2751 free(reordering);
2753 return qp;
2754 error:
2755 free(active);
2756 free(reordering);
2757 isl_qpolynomial_free(qp);
2758 return NULL;
2761 __isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
2762 unsigned first, unsigned n)
2764 int i;
2765 isl_poly_rec *rec;
2767 if (!poly)
2768 return NULL;
2769 if (n == 0 || poly->var < 0 || poly->var < first)
2770 return poly;
2771 if (poly->var < first + n) {
2772 poly = replace_by_constant_term(poly);
2773 return isl_poly_drop(poly, first, n);
2775 poly = isl_poly_cow(poly);
2776 if (!poly)
2777 return NULL;
2778 poly->var -= n;
2779 rec = isl_poly_as_rec(poly);
2780 if (!rec)
2781 goto error;
2783 for (i = 0; i < rec->n; ++i) {
2784 rec->p[i] = isl_poly_drop(rec->p[i], first, n);
2785 if (!rec->p[i])
2786 goto error;
2789 return poly;
2790 error:
2791 isl_poly_free(poly);
2792 return NULL;
2795 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2796 __isl_take isl_qpolynomial *qp,
2797 enum isl_dim_type type, unsigned pos, const char *s)
2799 qp = isl_qpolynomial_cow(qp);
2800 if (!qp)
2801 return NULL;
2802 if (type == isl_dim_out)
2803 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2804 "cannot set name of output/set dimension",
2805 return isl_qpolynomial_free(qp));
2806 type = domain_type(type);
2807 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2808 if (!qp->dim)
2809 goto error;
2810 return qp;
2811 error:
2812 isl_qpolynomial_free(qp);
2813 return NULL;
2816 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2817 __isl_take isl_qpolynomial *qp,
2818 enum isl_dim_type type, unsigned first, unsigned n)
2820 if (!qp)
2821 return NULL;
2822 if (type == isl_dim_out)
2823 isl_die(qp->dim->ctx, isl_error_invalid,
2824 "cannot drop output/set dimension",
2825 goto error);
2826 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2827 return isl_qpolynomial_free(qp);
2828 type = domain_type(type);
2829 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2830 return qp;
2832 qp = isl_qpolynomial_cow(qp);
2833 if (!qp)
2834 return NULL;
2836 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2837 type == isl_dim_set, goto error);
2839 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2840 if (!qp->dim)
2841 goto error;
2843 if (type == isl_dim_set)
2844 first += isl_space_dim(qp->dim, isl_dim_param);
2846 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2847 if (!qp->div)
2848 goto error;
2850 qp->poly = isl_poly_drop(qp->poly, first, n);
2851 if (!qp->poly)
2852 goto error;
2854 return qp;
2855 error:
2856 isl_qpolynomial_free(qp);
2857 return NULL;
2860 /* Project the domain of the quasi-polynomial onto its parameter space.
2861 * The quasi-polynomial may not involve any of the domain dimensions.
2863 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2864 __isl_take isl_qpolynomial *qp)
2866 isl_space *space;
2867 unsigned n;
2868 isl_bool involves;
2870 n = isl_qpolynomial_dim(qp, isl_dim_in);
2871 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2872 if (involves < 0)
2873 return isl_qpolynomial_free(qp);
2874 if (involves)
2875 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2876 "polynomial involves some of the domain dimensions",
2877 return isl_qpolynomial_free(qp));
2878 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2879 space = isl_qpolynomial_get_domain_space(qp);
2880 space = isl_space_params(space);
2881 qp = isl_qpolynomial_reset_domain_space(qp, space);
2882 return qp;
2885 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2886 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2888 int i, j, k;
2889 isl_int denom;
2890 unsigned total;
2891 unsigned n_div;
2892 isl_poly *poly;
2894 if (!eq)
2895 goto error;
2896 if (eq->n_eq == 0) {
2897 isl_basic_set_free(eq);
2898 return qp;
2901 qp = isl_qpolynomial_cow(qp);
2902 if (!qp)
2903 goto error;
2904 qp->div = isl_mat_cow(qp->div);
2905 if (!qp->div)
2906 goto error;
2908 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2909 n_div = eq->n_div;
2910 isl_int_init(denom);
2911 for (i = 0; i < eq->n_eq; ++i) {
2912 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2913 if (j < 0 || j == 0 || j >= total)
2914 continue;
2916 for (k = 0; k < qp->div->n_row; ++k) {
2917 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2918 continue;
2919 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2920 &qp->div->row[k][0]);
2921 normalize_div(qp, k);
2924 if (isl_int_is_pos(eq->eq[i][j]))
2925 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2926 isl_int_abs(denom, eq->eq[i][j]);
2927 isl_int_set_si(eq->eq[i][j], 0);
2929 poly = isl_poly_from_affine(qp->dim->ctx,
2930 eq->eq[i], denom, total);
2931 qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
2932 isl_poly_free(poly);
2934 isl_int_clear(denom);
2936 if (!qp->poly)
2937 goto error;
2939 isl_basic_set_free(eq);
2941 qp = substitute_non_divs(qp);
2942 qp = sort_divs(qp);
2944 return qp;
2945 error:
2946 isl_basic_set_free(eq);
2947 isl_qpolynomial_free(qp);
2948 return NULL;
2951 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2953 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2954 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2956 if (!qp || !eq)
2957 goto error;
2958 if (qp->div->n_row > 0)
2959 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2960 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2961 error:
2962 isl_basic_set_free(eq);
2963 isl_qpolynomial_free(qp);
2964 return NULL;
2967 /* Look for equalities among the variables shared by context and qp
2968 * and the integer divisions of qp, if any.
2969 * The equalities are then used to eliminate variables and/or integer
2970 * divisions from qp.
2972 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2973 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2975 isl_local_space *ls;
2976 isl_basic_set *aff;
2978 ls = isl_qpolynomial_get_domain_local_space(qp);
2979 context = isl_local_space_lift_set(ls, context);
2981 aff = isl_set_affine_hull(context);
2982 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2985 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2986 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2988 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2989 isl_set *dom_context = isl_set_universe(space);
2990 dom_context = isl_set_intersect_params(dom_context, context);
2991 return isl_qpolynomial_gist(qp, dom_context);
2994 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2995 __isl_take isl_qpolynomial *qp)
2997 isl_set *dom;
2999 if (!qp)
3000 return NULL;
3001 if (isl_qpolynomial_is_zero(qp)) {
3002 isl_space *dim = isl_qpolynomial_get_space(qp);
3003 isl_qpolynomial_free(qp);
3004 return isl_pw_qpolynomial_zero(dim);
3007 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
3008 return isl_pw_qpolynomial_alloc(dom, qp);
3011 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3013 #undef PW
3014 #define PW isl_pw_qpolynomial
3015 #undef EL
3016 #define EL isl_qpolynomial
3017 #undef EL_IS_ZERO
3018 #define EL_IS_ZERO is_zero
3019 #undef ZERO
3020 #define ZERO zero
3021 #undef IS_ZERO
3022 #define IS_ZERO is_zero
3023 #undef FIELD
3024 #define FIELD qp
3025 #undef DEFAULT_IS_ZERO
3026 #define DEFAULT_IS_ZERO 1
3028 #define NO_PULLBACK
3030 #include <isl_pw_templ.c>
3031 #include <isl_pw_eval.c>
3033 #undef BASE
3034 #define BASE pw_qpolynomial
3036 #include <isl_union_single.c>
3037 #include <isl_union_eval.c>
3038 #include <isl_union_neg.c>
3040 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
3042 if (!pwqp)
3043 return -1;
3045 if (pwqp->n != -1)
3046 return 0;
3048 if (!isl_set_plain_is_universe(pwqp->p[0].set))
3049 return 0;
3051 return isl_qpolynomial_is_one(pwqp->p[0].qp);
3054 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
3055 __isl_take isl_pw_qpolynomial *pwqp1,
3056 __isl_take isl_pw_qpolynomial *pwqp2)
3058 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3061 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
3062 __isl_take isl_pw_qpolynomial *pwqp1,
3063 __isl_take isl_pw_qpolynomial *pwqp2)
3065 int i, j, n;
3066 struct isl_pw_qpolynomial *res;
3068 if (!pwqp1 || !pwqp2)
3069 goto error;
3071 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3072 goto error);
3074 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3075 isl_pw_qpolynomial_free(pwqp2);
3076 return pwqp1;
3079 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3080 isl_pw_qpolynomial_free(pwqp1);
3081 return pwqp2;
3084 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3085 isl_pw_qpolynomial_free(pwqp1);
3086 return pwqp2;
3089 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3090 isl_pw_qpolynomial_free(pwqp2);
3091 return pwqp1;
3094 n = pwqp1->n * pwqp2->n;
3095 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3097 for (i = 0; i < pwqp1->n; ++i) {
3098 for (j = 0; j < pwqp2->n; ++j) {
3099 struct isl_set *common;
3100 struct isl_qpolynomial *prod;
3101 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3102 isl_set_copy(pwqp2->p[j].set));
3103 if (isl_set_plain_is_empty(common)) {
3104 isl_set_free(common);
3105 continue;
3108 prod = isl_qpolynomial_mul(
3109 isl_qpolynomial_copy(pwqp1->p[i].qp),
3110 isl_qpolynomial_copy(pwqp2->p[j].qp));
3112 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3116 isl_pw_qpolynomial_free(pwqp1);
3117 isl_pw_qpolynomial_free(pwqp2);
3119 return res;
3120 error:
3121 isl_pw_qpolynomial_free(pwqp1);
3122 isl_pw_qpolynomial_free(pwqp2);
3123 return NULL;
3126 __isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
3127 __isl_take isl_vec *vec)
3129 int i;
3130 isl_bool is_cst;
3131 isl_poly_rec *rec;
3132 isl_val *res;
3133 isl_val *base;
3135 is_cst = isl_poly_is_cst(poly);
3136 if (is_cst < 0)
3137 goto error;
3138 if (is_cst) {
3139 isl_vec_free(vec);
3140 res = isl_poly_get_constant_val(poly);
3141 isl_poly_free(poly);
3142 return res;
3145 rec = isl_poly_as_rec(poly);
3146 if (!rec || !vec)
3147 goto error;
3149 isl_assert(poly->ctx, rec->n >= 1, goto error);
3151 base = isl_val_rat_from_isl_int(poly->ctx,
3152 vec->el[1 + poly->var], vec->el[0]);
3154 res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3155 isl_vec_copy(vec));
3157 for (i = rec->n - 2; i >= 0; --i) {
3158 res = isl_val_mul(res, isl_val_copy(base));
3159 res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
3160 isl_vec_copy(vec)));
3163 isl_val_free(base);
3164 isl_poly_free(poly);
3165 isl_vec_free(vec);
3166 return res;
3167 error:
3168 isl_poly_free(poly);
3169 isl_vec_free(vec);
3170 return NULL;
3173 /* Evaluate "qp" in the void point "pnt".
3174 * In particular, return the value NaN.
3176 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3177 __isl_take isl_point *pnt)
3179 isl_ctx *ctx;
3181 ctx = isl_point_get_ctx(pnt);
3182 isl_qpolynomial_free(qp);
3183 isl_point_free(pnt);
3184 return isl_val_nan(ctx);
3187 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3188 __isl_take isl_point *pnt)
3190 isl_bool is_void;
3191 isl_vec *ext;
3192 isl_val *v;
3194 if (!qp || !pnt)
3195 goto error;
3196 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3197 is_void = isl_point_is_void(pnt);
3198 if (is_void < 0)
3199 goto error;
3200 if (is_void)
3201 return eval_void(qp, pnt);
3203 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3205 v = isl_poly_eval(isl_poly_copy(qp->poly), ext);
3207 isl_qpolynomial_free(qp);
3208 isl_point_free(pnt);
3210 return v;
3211 error:
3212 isl_qpolynomial_free(qp);
3213 isl_point_free(pnt);
3214 return NULL;
3217 int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
3219 int cmp;
3220 isl_int t;
3221 isl_int_init(t);
3222 isl_int_mul(t, cst1->n, cst2->d);
3223 isl_int_submul(t, cst2->n, cst1->d);
3224 cmp = isl_int_sgn(t);
3225 isl_int_clear(t);
3226 return cmp;
3229 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3230 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3231 unsigned first, unsigned n)
3233 unsigned total;
3234 unsigned g_pos;
3235 int *exp;
3237 if (!qp)
3238 return NULL;
3239 if (type == isl_dim_out)
3240 isl_die(qp->div->ctx, isl_error_invalid,
3241 "cannot insert output/set dimensions",
3242 goto error);
3243 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3244 return isl_qpolynomial_free(qp);
3245 type = domain_type(type);
3246 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3247 return qp;
3249 qp = isl_qpolynomial_cow(qp);
3250 if (!qp)
3251 return NULL;
3253 g_pos = pos(qp->dim, type) + first;
3255 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3256 if (!qp->div)
3257 goto error;
3259 total = qp->div->n_col - 2;
3260 if (total > g_pos) {
3261 int i;
3262 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3263 if (!exp)
3264 goto error;
3265 for (i = 0; i < total - g_pos; ++i)
3266 exp[i] = i + n;
3267 qp->poly = expand(qp->poly, exp, g_pos);
3268 free(exp);
3269 if (!qp->poly)
3270 goto error;
3273 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3274 if (!qp->dim)
3275 goto error;
3277 return qp;
3278 error:
3279 isl_qpolynomial_free(qp);
3280 return NULL;
3283 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3284 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3286 unsigned pos;
3288 pos = isl_qpolynomial_dim(qp, type);
3290 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3293 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3294 __isl_take isl_pw_qpolynomial *pwqp,
3295 enum isl_dim_type type, unsigned n)
3297 unsigned pos;
3299 pos = isl_pw_qpolynomial_dim(pwqp, type);
3301 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3304 static int *reordering_move(isl_ctx *ctx,
3305 unsigned len, unsigned dst, unsigned src, unsigned n)
3307 int i;
3308 int *reordering;
3310 reordering = isl_alloc_array(ctx, int, len);
3311 if (!reordering)
3312 return NULL;
3314 if (dst <= src) {
3315 for (i = 0; i < dst; ++i)
3316 reordering[i] = i;
3317 for (i = 0; i < n; ++i)
3318 reordering[src + i] = dst + i;
3319 for (i = 0; i < src - dst; ++i)
3320 reordering[dst + i] = dst + n + i;
3321 for (i = 0; i < len - src - n; ++i)
3322 reordering[src + n + i] = src + n + i;
3323 } else {
3324 for (i = 0; i < src; ++i)
3325 reordering[i] = i;
3326 for (i = 0; i < n; ++i)
3327 reordering[src + i] = dst + i;
3328 for (i = 0; i < dst - src; ++i)
3329 reordering[src + n + i] = src + i;
3330 for (i = 0; i < len - dst - n; ++i)
3331 reordering[dst + n + i] = dst + n + i;
3334 return reordering;
3337 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3338 __isl_take isl_qpolynomial *qp,
3339 enum isl_dim_type dst_type, unsigned dst_pos,
3340 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3342 unsigned g_dst_pos;
3343 unsigned g_src_pos;
3344 int *reordering;
3346 if (!qp)
3347 return NULL;
3349 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3350 isl_die(qp->dim->ctx, isl_error_invalid,
3351 "cannot move output/set dimension",
3352 goto error);
3353 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3354 return isl_qpolynomial_free(qp);
3355 if (dst_type == isl_dim_in)
3356 dst_type = isl_dim_set;
3357 if (src_type == isl_dim_in)
3358 src_type = isl_dim_set;
3360 if (n == 0 &&
3361 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3362 !isl_space_is_named_or_nested(qp->dim, dst_type))
3363 return qp;
3365 qp = isl_qpolynomial_cow(qp);
3366 if (!qp)
3367 return NULL;
3369 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3370 g_src_pos = pos(qp->dim, src_type) + src_pos;
3371 if (dst_type > src_type)
3372 g_dst_pos -= n;
3374 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3375 if (!qp->div)
3376 goto error;
3377 qp = sort_divs(qp);
3378 if (!qp)
3379 goto error;
3381 reordering = reordering_move(qp->dim->ctx,
3382 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3383 if (!reordering)
3384 goto error;
3386 qp->poly = reorder(qp->poly, reordering);
3387 free(reordering);
3388 if (!qp->poly)
3389 goto error;
3391 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3392 if (!qp->dim)
3393 goto error;
3395 return qp;
3396 error:
3397 isl_qpolynomial_free(qp);
3398 return NULL;
3401 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3402 __isl_take isl_space *space, isl_int *f, isl_int denom)
3404 isl_poly *poly;
3406 space = isl_space_domain(space);
3407 if (!space)
3408 return NULL;
3410 poly = isl_poly_from_affine(space->ctx, f, denom,
3411 1 + isl_space_dim(space, isl_dim_all));
3413 return isl_qpolynomial_alloc(space, 0, poly);
3416 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3418 isl_ctx *ctx;
3419 isl_poly *poly;
3420 isl_qpolynomial *qp;
3422 if (!aff)
3423 return NULL;
3425 ctx = isl_aff_get_ctx(aff);
3426 poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3427 aff->v->size - 1);
3429 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3430 aff->ls->div->n_row, poly);
3431 if (!qp)
3432 goto error;
3434 isl_mat_free(qp->div);
3435 qp->div = isl_mat_copy(aff->ls->div);
3436 qp->div = isl_mat_cow(qp->div);
3437 if (!qp->div)
3438 goto error;
3440 isl_aff_free(aff);
3441 qp = reduce_divs(qp);
3442 qp = remove_redundant_divs(qp);
3443 return qp;
3444 error:
3445 isl_aff_free(aff);
3446 return isl_qpolynomial_free(qp);
3449 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3450 __isl_take isl_pw_aff *pwaff)
3452 int i;
3453 isl_pw_qpolynomial *pwqp;
3455 if (!pwaff)
3456 return NULL;
3458 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3459 pwaff->n);
3461 for (i = 0; i < pwaff->n; ++i) {
3462 isl_set *dom;
3463 isl_qpolynomial *qp;
3465 dom = isl_set_copy(pwaff->p[i].set);
3466 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3467 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3470 isl_pw_aff_free(pwaff);
3471 return pwqp;
3474 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3475 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3477 isl_aff *aff;
3479 aff = isl_constraint_get_bound(c, type, pos);
3480 isl_constraint_free(c);
3481 return isl_qpolynomial_from_aff(aff);
3484 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3485 * in "qp" by subs[i].
3487 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3488 __isl_take isl_qpolynomial *qp,
3489 enum isl_dim_type type, unsigned first, unsigned n,
3490 __isl_keep isl_qpolynomial **subs)
3492 int i;
3493 isl_poly **polys;
3495 if (n == 0)
3496 return qp;
3498 qp = isl_qpolynomial_cow(qp);
3499 if (!qp)
3500 return NULL;
3502 if (type == isl_dim_out)
3503 isl_die(qp->dim->ctx, isl_error_invalid,
3504 "cannot substitute output/set dimension",
3505 goto error);
3506 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3507 return isl_qpolynomial_free(qp);
3508 type = domain_type(type);
3510 for (i = 0; i < n; ++i)
3511 if (!subs[i])
3512 goto error;
3514 for (i = 0; i < n; ++i)
3515 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3516 goto error);
3518 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3519 for (i = 0; i < n; ++i)
3520 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3522 first += pos(qp->dim, type);
3524 polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
3525 if (!polys)
3526 goto error;
3527 for (i = 0; i < n; ++i)
3528 polys[i] = subs[i]->poly;
3530 qp->poly = isl_poly_subs(qp->poly, first, n, polys);
3532 free(polys);
3534 if (!qp->poly)
3535 goto error;
3537 return qp;
3538 error:
3539 isl_qpolynomial_free(qp);
3540 return NULL;
3543 /* Extend "bset" with extra set dimensions for each integer division
3544 * in "qp" and then call "fn" with the extended bset and the polynomial
3545 * that results from replacing each of the integer divisions by the
3546 * corresponding extra set dimension.
3548 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3549 __isl_keep isl_basic_set *bset,
3550 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3551 __isl_take isl_qpolynomial *poly, void *user), void *user)
3553 isl_space *space;
3554 isl_local_space *ls;
3555 isl_qpolynomial *poly;
3557 if (!qp || !bset)
3558 return isl_stat_error;
3559 if (qp->div->n_row == 0)
3560 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3561 user);
3563 space = isl_space_copy(qp->dim);
3564 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3565 poly = isl_qpolynomial_alloc(space, 0, isl_poly_copy(qp->poly));
3566 bset = isl_basic_set_copy(bset);
3567 ls = isl_qpolynomial_get_domain_local_space(qp);
3568 bset = isl_local_space_lift_basic_set(ls, bset);
3570 return fn(bset, poly, user);
3573 /* Return total degree in variables first (inclusive) up to last (exclusive).
3575 int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3577 int deg = -1;
3578 int i;
3579 isl_bool is_zero, is_cst;
3580 isl_poly_rec *rec;
3582 is_zero = isl_poly_is_zero(poly);
3583 if (is_zero < 0)
3584 return -2;
3585 if (is_zero)
3586 return -1;
3587 is_cst = isl_poly_is_cst(poly);
3588 if (is_cst < 0)
3589 return -2;
3590 if (is_cst || poly->var < first)
3591 return 0;
3593 rec = isl_poly_as_rec(poly);
3594 if (!rec)
3595 return -2;
3597 for (i = 0; i < rec->n; ++i) {
3598 int d;
3600 is_zero = isl_poly_is_zero(rec->p[i]);
3601 if (is_zero < 0)
3602 return -2;
3603 if (is_zero)
3604 continue;
3605 d = isl_poly_degree(rec->p[i], first, last);
3606 if (poly->var < last)
3607 d += i;
3608 if (d > deg)
3609 deg = d;
3612 return deg;
3615 /* Return total degree in set variables.
3617 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3619 unsigned ovar;
3620 unsigned nvar;
3622 if (!poly)
3623 return -2;
3625 ovar = isl_space_offset(poly->dim, isl_dim_set);
3626 nvar = isl_space_dim(poly->dim, isl_dim_set);
3627 return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3630 __isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
3631 unsigned pos, int deg)
3633 int i;
3634 isl_bool is_cst;
3635 isl_poly_rec *rec;
3637 is_cst = isl_poly_is_cst(poly);
3638 if (is_cst < 0)
3639 return NULL;
3640 if (is_cst || poly->var < pos) {
3641 if (deg == 0)
3642 return isl_poly_copy(poly);
3643 else
3644 return isl_poly_zero(poly->ctx);
3647 rec = isl_poly_as_rec(poly);
3648 if (!rec)
3649 return NULL;
3651 if (poly->var == pos) {
3652 if (deg < rec->n)
3653 return isl_poly_copy(rec->p[deg]);
3654 else
3655 return isl_poly_zero(poly->ctx);
3658 poly = isl_poly_copy(poly);
3659 poly = isl_poly_cow(poly);
3660 rec = isl_poly_as_rec(poly);
3661 if (!rec)
3662 goto error;
3664 for (i = 0; i < rec->n; ++i) {
3665 isl_poly *t;
3666 t = isl_poly_coeff(rec->p[i], pos, deg);
3667 if (!t)
3668 goto error;
3669 isl_poly_free(rec->p[i]);
3670 rec->p[i] = t;
3673 return poly;
3674 error:
3675 isl_poly_free(poly);
3676 return NULL;
3679 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3681 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3682 __isl_keep isl_qpolynomial *qp,
3683 enum isl_dim_type type, unsigned t_pos, int deg)
3685 unsigned g_pos;
3686 isl_poly *poly;
3687 isl_qpolynomial *c;
3689 if (!qp)
3690 return NULL;
3692 if (type == isl_dim_out)
3693 isl_die(qp->div->ctx, isl_error_invalid,
3694 "output/set dimension does not have a coefficient",
3695 return NULL);
3696 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
3697 return NULL;
3698 type = domain_type(type);
3700 g_pos = pos(qp->dim, type) + t_pos;
3701 poly = isl_poly_coeff(qp->poly, g_pos, deg);
3703 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
3704 qp->div->n_row, poly);
3705 if (!c)
3706 return NULL;
3707 isl_mat_free(c->div);
3708 c->div = isl_mat_copy(qp->div);
3709 if (!c->div)
3710 goto error;
3711 return c;
3712 error:
3713 isl_qpolynomial_free(c);
3714 return NULL;
3717 /* Homogenize the polynomial in the variables first (inclusive) up to
3718 * last (exclusive) by inserting powers of variable first.
3719 * Variable first is assumed not to appear in the input.
3721 __isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
3722 int target, int first, int last)
3724 int i;
3725 isl_bool is_zero, is_cst;
3726 isl_poly_rec *rec;
3728 is_zero = isl_poly_is_zero(poly);
3729 if (is_zero < 0)
3730 return isl_poly_free(poly);
3731 if (is_zero)
3732 return poly;
3733 if (deg == target)
3734 return poly;
3735 is_cst = isl_poly_is_cst(poly);
3736 if (is_cst < 0)
3737 return isl_poly_free(poly);
3738 if (is_cst || poly->var < first) {
3739 isl_poly *hom;
3741 hom = isl_poly_var_pow(poly->ctx, first, target - deg);
3742 if (!hom)
3743 goto error;
3744 rec = isl_poly_as_rec(hom);
3745 rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
3747 return hom;
3750 poly = isl_poly_cow(poly);
3751 rec = isl_poly_as_rec(poly);
3752 if (!rec)
3753 goto error;
3755 for (i = 0; i < rec->n; ++i) {
3756 is_zero = isl_poly_is_zero(rec->p[i]);
3757 if (is_zero < 0)
3758 return isl_poly_free(poly);
3759 if (is_zero)
3760 continue;
3761 rec->p[i] = isl_poly_homogenize(rec->p[i],
3762 poly->var < last ? deg + i : i, target,
3763 first, last);
3764 if (!rec->p[i])
3765 goto error;
3768 return poly;
3769 error:
3770 isl_poly_free(poly);
3771 return NULL;
3774 /* Homogenize the polynomial in the set variables by introducing
3775 * powers of an extra set variable at position 0.
3777 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3778 __isl_take isl_qpolynomial *poly)
3780 unsigned ovar;
3781 unsigned nvar;
3782 int deg = isl_qpolynomial_degree(poly);
3784 if (deg < -1)
3785 goto error;
3787 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3788 poly = isl_qpolynomial_cow(poly);
3789 if (!poly)
3790 goto error;
3792 ovar = isl_space_offset(poly->dim, isl_dim_set);
3793 nvar = isl_space_dim(poly->dim, isl_dim_set);
3794 poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
3795 if (!poly->poly)
3796 goto error;
3798 return poly;
3799 error:
3800 isl_qpolynomial_free(poly);
3801 return NULL;
3804 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
3805 __isl_take isl_mat *div)
3807 isl_term *term;
3808 int n;
3810 if (!space || !div)
3811 goto error;
3813 n = isl_space_dim(space, isl_dim_all) + div->n_row;
3815 term = isl_calloc(space->ctx, struct isl_term,
3816 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3817 if (!term)
3818 goto error;
3820 term->ref = 1;
3821 term->dim = space;
3822 term->div = div;
3823 isl_int_init(term->n);
3824 isl_int_init(term->d);
3826 return term;
3827 error:
3828 isl_space_free(space);
3829 isl_mat_free(div);
3830 return NULL;
3833 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3835 if (!term)
3836 return NULL;
3838 term->ref++;
3839 return term;
3842 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3844 int i;
3845 isl_term *dup;
3846 unsigned total;
3848 if (!term)
3849 return NULL;
3851 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3853 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3854 if (!dup)
3855 return NULL;
3857 isl_int_set(dup->n, term->n);
3858 isl_int_set(dup->d, term->d);
3860 for (i = 0; i < total; ++i)
3861 dup->pow[i] = term->pow[i];
3863 return dup;
3866 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3868 if (!term)
3869 return NULL;
3871 if (term->ref == 1)
3872 return term;
3873 term->ref--;
3874 return isl_term_dup(term);
3877 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3879 if (!term)
3880 return NULL;
3882 if (--term->ref > 0)
3883 return NULL;
3885 isl_space_free(term->dim);
3886 isl_mat_free(term->div);
3887 isl_int_clear(term->n);
3888 isl_int_clear(term->d);
3889 free(term);
3891 return NULL;
3894 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3896 if (!term)
3897 return 0;
3899 switch (type) {
3900 case isl_dim_param:
3901 case isl_dim_in:
3902 case isl_dim_out: return isl_space_dim(term->dim, type);
3903 case isl_dim_div: return term->div->n_row;
3904 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3905 term->div->n_row;
3906 default: return 0;
3910 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3912 return term ? term->dim->ctx : NULL;
3915 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3917 if (!term)
3918 return;
3919 isl_int_set(*n, term->n);
3922 /* Return the coefficient of the term "term".
3924 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3926 if (!term)
3927 return NULL;
3929 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3930 term->n, term->d);
3933 #undef TYPE
3934 #define TYPE isl_term
3935 static
3936 #include "check_type_range_templ.c"
3938 int isl_term_get_exp(__isl_keep isl_term *term,
3939 enum isl_dim_type type, unsigned pos)
3941 if (isl_term_check_range(term, type, pos, 1) < 0)
3942 return -1;
3944 if (type >= isl_dim_set)
3945 pos += isl_space_dim(term->dim, isl_dim_param);
3946 if (type >= isl_dim_div)
3947 pos += isl_space_dim(term->dim, isl_dim_set);
3949 return term->pow[pos];
3952 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3954 isl_local_space *ls;
3955 isl_aff *aff;
3957 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
3958 return NULL;
3960 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3961 isl_mat_copy(term->div));
3962 aff = isl_aff_alloc(ls);
3963 if (!aff)
3964 return NULL;
3966 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3968 aff = isl_aff_normalize(aff);
3970 return aff;
3973 __isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
3974 isl_stat (*fn)(__isl_take isl_term *term, void *user),
3975 __isl_take isl_term *term, void *user)
3977 int i;
3978 isl_bool is_zero, is_bad, is_cst;
3979 isl_poly_rec *rec;
3981 is_zero = isl_poly_is_zero(poly);
3982 if (is_zero < 0 || !term)
3983 goto error;
3985 if (is_zero)
3986 return term;
3988 is_cst = isl_poly_is_cst(poly);
3989 is_bad = isl_poly_is_nan(poly);
3990 if (is_bad >= 0 && !is_bad)
3991 is_bad = isl_poly_is_infty(poly);
3992 if (is_bad >= 0 && !is_bad)
3993 is_bad = isl_poly_is_neginfty(poly);
3994 if (is_cst < 0 || is_bad < 0)
3995 return isl_term_free(term);
3996 if (is_bad)
3997 isl_die(isl_term_get_ctx(term), isl_error_invalid,
3998 "cannot handle NaN/infty polynomial",
3999 return isl_term_free(term));
4001 if (is_cst) {
4002 isl_poly_cst *cst;
4003 cst = isl_poly_as_cst(poly);
4004 if (!cst)
4005 goto error;
4006 term = isl_term_cow(term);
4007 if (!term)
4008 goto error;
4009 isl_int_set(term->n, cst->n);
4010 isl_int_set(term->d, cst->d);
4011 if (fn(isl_term_copy(term), user) < 0)
4012 goto error;
4013 return term;
4016 rec = isl_poly_as_rec(poly);
4017 if (!rec)
4018 goto error;
4020 for (i = 0; i < rec->n; ++i) {
4021 term = isl_term_cow(term);
4022 if (!term)
4023 goto error;
4024 term->pow[poly->var] = i;
4025 term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4026 if (!term)
4027 goto error;
4029 term->pow[poly->var] = 0;
4031 return term;
4032 error:
4033 isl_term_free(term);
4034 return NULL;
4037 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
4038 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4040 isl_term *term;
4042 if (!qp)
4043 return isl_stat_error;
4045 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
4046 if (!term)
4047 return isl_stat_error;
4049 term = isl_poly_foreach_term(qp->poly, fn, term, user);
4051 isl_term_free(term);
4053 return term ? isl_stat_ok : isl_stat_error;
4056 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
4058 isl_poly *poly;
4059 isl_qpolynomial *qp;
4060 int i, n;
4062 if (!term)
4063 return NULL;
4065 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
4067 poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4068 for (i = 0; i < n; ++i) {
4069 if (!term->pow[i])
4070 continue;
4071 poly = isl_poly_mul(poly,
4072 isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4075 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4076 term->div->n_row, poly);
4077 if (!qp)
4078 goto error;
4079 isl_mat_free(qp->div);
4080 qp->div = isl_mat_copy(term->div);
4081 if (!qp->div)
4082 goto error;
4084 isl_term_free(term);
4085 return qp;
4086 error:
4087 isl_qpolynomial_free(qp);
4088 isl_term_free(term);
4089 return NULL;
4092 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4093 __isl_take isl_space *space)
4095 int i;
4096 int extra;
4097 unsigned total;
4099 if (!qp || !space)
4100 goto error;
4102 if (isl_space_is_equal(qp->dim, space)) {
4103 isl_space_free(space);
4104 return qp;
4107 qp = isl_qpolynomial_cow(qp);
4108 if (!qp)
4109 goto error;
4111 extra = isl_space_dim(space, isl_dim_set) -
4112 isl_space_dim(qp->dim, isl_dim_set);
4113 total = isl_space_dim(qp->dim, isl_dim_all);
4114 if (qp->div->n_row) {
4115 int *exp;
4117 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4118 if (!exp)
4119 goto error;
4120 for (i = 0; i < qp->div->n_row; ++i)
4121 exp[i] = extra + i;
4122 qp->poly = expand(qp->poly, exp, total);
4123 free(exp);
4124 if (!qp->poly)
4125 goto error;
4127 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4128 if (!qp->div)
4129 goto error;
4130 for (i = 0; i < qp->div->n_row; ++i)
4131 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4133 isl_space_free(qp->dim);
4134 qp->dim = space;
4136 return qp;
4137 error:
4138 isl_space_free(space);
4139 isl_qpolynomial_free(qp);
4140 return NULL;
4143 /* For each parameter or variable that does not appear in qp,
4144 * first eliminate the variable from all constraints and then set it to zero.
4146 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4147 __isl_keep isl_qpolynomial *qp)
4149 int *active = NULL;
4150 int i;
4151 int d;
4152 unsigned nparam;
4153 unsigned nvar;
4155 if (!set || !qp)
4156 goto error;
4158 d = isl_space_dim(set->dim, isl_dim_all);
4159 active = isl_calloc_array(set->ctx, int, d);
4160 if (set_active(qp, active) < 0)
4161 goto error;
4163 for (i = 0; i < d; ++i)
4164 if (!active[i])
4165 break;
4167 if (i == d) {
4168 free(active);
4169 return set;
4172 nparam = isl_space_dim(set->dim, isl_dim_param);
4173 nvar = isl_space_dim(set->dim, isl_dim_set);
4174 for (i = 0; i < nparam; ++i) {
4175 if (active[i])
4176 continue;
4177 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4178 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4180 for (i = 0; i < nvar; ++i) {
4181 if (active[nparam + i])
4182 continue;
4183 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4184 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4187 free(active);
4189 return set;
4190 error:
4191 free(active);
4192 isl_set_free(set);
4193 return NULL;
4196 struct isl_opt_data {
4197 isl_qpolynomial *qp;
4198 int first;
4199 isl_val *opt;
4200 int max;
4203 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4205 struct isl_opt_data *data = (struct isl_opt_data *)user;
4206 isl_val *val;
4208 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4209 if (data->first) {
4210 data->first = 0;
4211 data->opt = val;
4212 } else if (data->max) {
4213 data->opt = isl_val_max(data->opt, val);
4214 } else {
4215 data->opt = isl_val_min(data->opt, val);
4218 return isl_stat_ok;
4221 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4222 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4224 struct isl_opt_data data = { NULL, 1, NULL, max };
4225 isl_bool is_cst;
4227 if (!set || !qp)
4228 goto error;
4230 is_cst = isl_poly_is_cst(qp->poly);
4231 if (is_cst < 0)
4232 goto error;
4233 if (is_cst) {
4234 isl_set_free(set);
4235 data.opt = isl_qpolynomial_get_constant_val(qp);
4236 isl_qpolynomial_free(qp);
4237 return data.opt;
4240 set = fix_inactive(set, qp);
4242 data.qp = qp;
4243 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4244 goto error;
4246 if (data.first)
4247 data.opt = isl_val_zero(isl_set_get_ctx(set));
4249 isl_set_free(set);
4250 isl_qpolynomial_free(qp);
4251 return data.opt;
4252 error:
4253 isl_set_free(set);
4254 isl_qpolynomial_free(qp);
4255 isl_val_free(data.opt);
4256 return NULL;
4259 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4260 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4262 int i;
4263 int n_sub;
4264 isl_ctx *ctx;
4265 isl_poly **subs;
4266 isl_mat *mat, *diag;
4268 qp = isl_qpolynomial_cow(qp);
4269 if (!qp || !morph)
4270 goto error;
4272 ctx = qp->dim->ctx;
4273 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4275 n_sub = morph->inv->n_row - 1;
4276 if (morph->inv->n_row != morph->inv->n_col)
4277 n_sub += qp->div->n_row;
4278 subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
4279 if (n_sub && !subs)
4280 goto error;
4282 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4283 subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4284 morph->inv->row[0][0], morph->inv->n_col);
4285 if (morph->inv->n_row != morph->inv->n_col)
4286 for (i = 0; i < qp->div->n_row; ++i)
4287 subs[morph->inv->n_row - 1 + i] =
4288 isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4290 qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4292 for (i = 0; i < n_sub; ++i)
4293 isl_poly_free(subs[i]);
4294 free(subs);
4296 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4297 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4298 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4299 mat = isl_mat_diagonal(mat, diag);
4300 qp->div = isl_mat_product(qp->div, mat);
4301 isl_space_free(qp->dim);
4302 qp->dim = isl_space_copy(morph->ran->dim);
4304 if (!qp->poly || !qp->div || !qp->dim)
4305 goto error;
4307 isl_morph_free(morph);
4309 return qp;
4310 error:
4311 isl_qpolynomial_free(qp);
4312 isl_morph_free(morph);
4313 return NULL;
4316 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4317 __isl_take isl_union_pw_qpolynomial *upwqp1,
4318 __isl_take isl_union_pw_qpolynomial *upwqp2)
4320 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4321 &isl_pw_qpolynomial_mul);
4324 /* Reorder the dimension of "qp" according to the given reordering.
4326 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4327 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4329 isl_space *space;
4331 qp = isl_qpolynomial_cow(qp);
4332 if (!qp)
4333 goto error;
4335 r = isl_reordering_extend(r, qp->div->n_row);
4336 if (!r)
4337 goto error;
4339 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4340 if (!qp->div)
4341 goto error;
4343 qp->poly = reorder(qp->poly, r->pos);
4344 if (!qp->poly)
4345 goto error;
4347 space = isl_reordering_get_space(r);
4348 qp = isl_qpolynomial_reset_domain_space(qp, space);
4350 isl_reordering_free(r);
4351 return qp;
4352 error:
4353 isl_qpolynomial_free(qp);
4354 isl_reordering_free(r);
4355 return NULL;
4358 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4359 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4361 isl_bool equal_params;
4363 if (!qp || !model)
4364 goto error;
4366 equal_params = isl_space_has_equal_params(qp->dim, model);
4367 if (equal_params < 0)
4368 goto error;
4369 if (!equal_params) {
4370 isl_reordering *exp;
4372 exp = isl_parameter_alignment_reordering(qp->dim, model);
4373 exp = isl_reordering_extend_space(exp,
4374 isl_qpolynomial_get_domain_space(qp));
4375 qp = isl_qpolynomial_realign_domain(qp, exp);
4378 isl_space_free(model);
4379 return qp;
4380 error:
4381 isl_space_free(model);
4382 isl_qpolynomial_free(qp);
4383 return NULL;
4386 struct isl_split_periods_data {
4387 int max_periods;
4388 isl_pw_qpolynomial *res;
4391 /* Create a slice where the integer division "div" has the fixed value "v".
4392 * In particular, if "div" refers to floor(f/m), then create a slice
4394 * m v <= f <= m v + (m - 1)
4396 * or
4398 * f - m v >= 0
4399 * -f + m v + (m - 1) >= 0
4401 static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
4402 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4404 int total;
4405 isl_basic_set *bset = NULL;
4406 int k;
4408 if (!space || !qp)
4409 goto error;
4411 total = isl_space_dim(space, isl_dim_all);
4412 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4414 k = isl_basic_set_alloc_inequality(bset);
4415 if (k < 0)
4416 goto error;
4417 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4418 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4420 k = isl_basic_set_alloc_inequality(bset);
4421 if (k < 0)
4422 goto error;
4423 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4424 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4425 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4426 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4428 isl_space_free(space);
4429 return isl_set_from_basic_set(bset);
4430 error:
4431 isl_basic_set_free(bset);
4432 isl_space_free(space);
4433 return NULL;
4436 static isl_stat split_periods(__isl_take isl_set *set,
4437 __isl_take isl_qpolynomial *qp, void *user);
4439 /* Create a slice of the domain "set" such that integer division "div"
4440 * has the fixed value "v" and add the results to data->res,
4441 * replacing the integer division by "v" in "qp".
4443 static isl_stat set_div(__isl_take isl_set *set,
4444 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4445 struct isl_split_periods_data *data)
4447 int i;
4448 int total;
4449 isl_set *slice;
4450 isl_poly *cst;
4452 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4453 set = isl_set_intersect(set, slice);
4455 if (!qp)
4456 goto error;
4458 total = isl_space_dim(qp->dim, isl_dim_all);
4460 for (i = div + 1; i < qp->div->n_row; ++i) {
4461 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4462 continue;
4463 isl_int_addmul(qp->div->row[i][1],
4464 qp->div->row[i][2 + total + div], v);
4465 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4468 cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4469 qp = substitute_div(qp, div, cst);
4471 return split_periods(set, qp, data);
4472 error:
4473 isl_set_free(set);
4474 isl_qpolynomial_free(qp);
4475 return isl_stat_error;
4478 /* Split the domain "set" such that integer division "div"
4479 * has a fixed value (ranging from "min" to "max") on each slice
4480 * and add the results to data->res.
4482 static isl_stat split_div(__isl_take isl_set *set,
4483 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4484 struct isl_split_periods_data *data)
4486 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4487 isl_set *set_i = isl_set_copy(set);
4488 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4490 if (set_div(set_i, qp_i, div, min, data) < 0)
4491 goto error;
4493 isl_set_free(set);
4494 isl_qpolynomial_free(qp);
4495 return isl_stat_ok;
4496 error:
4497 isl_set_free(set);
4498 isl_qpolynomial_free(qp);
4499 return isl_stat_error;
4502 /* If "qp" refers to any integer division
4503 * that can only attain "max_periods" distinct values on "set"
4504 * then split the domain along those distinct values.
4505 * Add the results (or the original if no splitting occurs)
4506 * to data->res.
4508 static isl_stat split_periods(__isl_take isl_set *set,
4509 __isl_take isl_qpolynomial *qp, void *user)
4511 int i;
4512 isl_pw_qpolynomial *pwqp;
4513 struct isl_split_periods_data *data;
4514 isl_int min, max;
4515 int total;
4516 isl_stat r = isl_stat_ok;
4518 data = (struct isl_split_periods_data *)user;
4520 if (!set || !qp)
4521 goto error;
4523 if (qp->div->n_row == 0) {
4524 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4525 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4526 return isl_stat_ok;
4529 isl_int_init(min);
4530 isl_int_init(max);
4531 total = isl_space_dim(qp->dim, isl_dim_all);
4532 for (i = 0; i < qp->div->n_row; ++i) {
4533 enum isl_lp_result lp_res;
4535 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4536 qp->div->n_row) != -1)
4537 continue;
4539 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4540 set->ctx->one, &min, NULL, NULL);
4541 if (lp_res == isl_lp_error)
4542 goto error2;
4543 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4544 continue;
4545 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4547 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4548 set->ctx->one, &max, NULL, NULL);
4549 if (lp_res == isl_lp_error)
4550 goto error2;
4551 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4552 continue;
4553 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4555 isl_int_sub(max, max, min);
4556 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4557 isl_int_add(max, max, min);
4558 break;
4562 if (i < qp->div->n_row) {
4563 r = split_div(set, qp, i, min, max, data);
4564 } else {
4565 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4566 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4569 isl_int_clear(max);
4570 isl_int_clear(min);
4572 return r;
4573 error2:
4574 isl_int_clear(max);
4575 isl_int_clear(min);
4576 error:
4577 isl_set_free(set);
4578 isl_qpolynomial_free(qp);
4579 return isl_stat_error;
4582 /* If any quasi-polynomial in pwqp refers to any integer division
4583 * that can only attain "max_periods" distinct values on its domain
4584 * then split the domain along those distinct values.
4586 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4587 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4589 struct isl_split_periods_data data;
4591 data.max_periods = max_periods;
4592 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4594 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4595 goto error;
4597 isl_pw_qpolynomial_free(pwqp);
4599 return data.res;
4600 error:
4601 isl_pw_qpolynomial_free(data.res);
4602 isl_pw_qpolynomial_free(pwqp);
4603 return NULL;
4606 /* Construct a piecewise quasipolynomial that is constant on the given
4607 * domain. In particular, it is
4608 * 0 if cst == 0
4609 * 1 if cst == 1
4610 * infinity if cst == -1
4612 * If cst == -1, then explicitly check whether the domain is empty and,
4613 * if so, return 0 instead.
4615 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4616 __isl_take isl_basic_set *bset, int cst)
4618 isl_space *dim;
4619 isl_qpolynomial *qp;
4621 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4622 cst = 0;
4623 if (!bset)
4624 return NULL;
4626 bset = isl_basic_set_params(bset);
4627 dim = isl_basic_set_get_space(bset);
4628 if (cst < 0)
4629 qp = isl_qpolynomial_infty_on_domain(dim);
4630 else if (cst == 0)
4631 qp = isl_qpolynomial_zero_on_domain(dim);
4632 else
4633 qp = isl_qpolynomial_one_on_domain(dim);
4634 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4637 /* Factor bset, call fn on each of the factors and return the product.
4639 * If no factors can be found, simply call fn on the input.
4640 * Otherwise, construct the factors based on the factorizer,
4641 * call fn on each factor and compute the product.
4643 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4644 __isl_take isl_basic_set *bset,
4645 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4647 int i, n;
4648 isl_space *space;
4649 isl_set *set;
4650 isl_factorizer *f;
4651 isl_qpolynomial *qp;
4652 isl_pw_qpolynomial *pwqp;
4653 unsigned nparam;
4654 unsigned nvar;
4656 f = isl_basic_set_factorizer(bset);
4657 if (!f)
4658 goto error;
4659 if (f->n_group == 0) {
4660 isl_factorizer_free(f);
4661 return fn(bset);
4664 nparam = isl_basic_set_dim(bset, isl_dim_param);
4665 nvar = isl_basic_set_dim(bset, isl_dim_set);
4667 space = isl_basic_set_get_space(bset);
4668 space = isl_space_params(space);
4669 set = isl_set_universe(isl_space_copy(space));
4670 qp = isl_qpolynomial_one_on_domain(space);
4671 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4673 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4675 for (i = 0, n = 0; i < f->n_group; ++i) {
4676 isl_basic_set *bset_i;
4677 isl_pw_qpolynomial *pwqp_i;
4679 bset_i = isl_basic_set_copy(bset);
4680 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4681 nparam + n + f->len[i], nvar - n - f->len[i]);
4682 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4683 nparam, n);
4684 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4685 n + f->len[i], nvar - n - f->len[i]);
4686 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4688 pwqp_i = fn(bset_i);
4689 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4691 n += f->len[i];
4694 isl_basic_set_free(bset);
4695 isl_factorizer_free(f);
4697 return pwqp;
4698 error:
4699 isl_basic_set_free(bset);
4700 return NULL;
4703 /* Factor bset, call fn on each of the factors and return the product.
4704 * The function is assumed to evaluate to zero on empty domains,
4705 * to one on zero-dimensional domains and to infinity on unbounded domains
4706 * and will not be called explicitly on zero-dimensional or unbounded domains.
4708 * We first check for some special cases and remove all equalities.
4709 * Then we hand over control to compressed_multiplicative_call.
4711 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4712 __isl_take isl_basic_set *bset,
4713 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4715 isl_bool bounded;
4716 isl_morph *morph;
4717 isl_pw_qpolynomial *pwqp;
4719 if (!bset)
4720 return NULL;
4722 if (isl_basic_set_plain_is_empty(bset))
4723 return constant_on_domain(bset, 0);
4725 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4726 return constant_on_domain(bset, 1);
4728 bounded = isl_basic_set_is_bounded(bset);
4729 if (bounded < 0)
4730 goto error;
4731 if (!bounded)
4732 return constant_on_domain(bset, -1);
4734 if (bset->n_eq == 0)
4735 return compressed_multiplicative_call(bset, fn);
4737 morph = isl_basic_set_full_compression(bset);
4738 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4740 pwqp = compressed_multiplicative_call(bset, fn);
4742 morph = isl_morph_dom_params(morph);
4743 morph = isl_morph_ran_params(morph);
4744 morph = isl_morph_inverse(morph);
4746 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4748 return pwqp;
4749 error:
4750 isl_basic_set_free(bset);
4751 return NULL;
4754 /* Drop all floors in "qp", turning each integer division [a/m] into
4755 * a rational division a/m. If "down" is set, then the integer division
4756 * is replaced by (a-(m-1))/m instead.
4758 static __isl_give isl_qpolynomial *qp_drop_floors(
4759 __isl_take isl_qpolynomial *qp, int down)
4761 int i;
4762 isl_poly *s;
4764 if (!qp)
4765 return NULL;
4766 if (qp->div->n_row == 0)
4767 return qp;
4769 qp = isl_qpolynomial_cow(qp);
4770 if (!qp)
4771 return NULL;
4773 for (i = qp->div->n_row - 1; i >= 0; --i) {
4774 if (down) {
4775 isl_int_sub(qp->div->row[i][1],
4776 qp->div->row[i][1], qp->div->row[i][0]);
4777 isl_int_add_ui(qp->div->row[i][1],
4778 qp->div->row[i][1], 1);
4780 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4781 qp->div->row[i][0], qp->div->n_col - 1);
4782 qp = substitute_div(qp, i, s);
4783 if (!qp)
4784 return NULL;
4787 return qp;
4790 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4791 * a rational division a/m.
4793 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4794 __isl_take isl_pw_qpolynomial *pwqp)
4796 int i;
4798 if (!pwqp)
4799 return NULL;
4801 if (isl_pw_qpolynomial_is_zero(pwqp))
4802 return pwqp;
4804 pwqp = isl_pw_qpolynomial_cow(pwqp);
4805 if (!pwqp)
4806 return NULL;
4808 for (i = 0; i < pwqp->n; ++i) {
4809 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4810 if (!pwqp->p[i].qp)
4811 goto error;
4814 return pwqp;
4815 error:
4816 isl_pw_qpolynomial_free(pwqp);
4817 return NULL;
4820 /* Adjust all the integer divisions in "qp" such that they are at least
4821 * one over the given orthant (identified by "signs"). This ensures
4822 * that they will still be non-negative even after subtracting (m-1)/m.
4824 * In particular, f is replaced by f' + v, changing f = [a/m]
4825 * to f' = [(a - m v)/m].
4826 * If the constant term k in a is smaller than m,
4827 * the constant term of v is set to floor(k/m) - 1.
4828 * For any other term, if the coefficient c and the variable x have
4829 * the same sign, then no changes are needed.
4830 * Otherwise, if the variable is positive (and c is negative),
4831 * then the coefficient of x in v is set to floor(c/m).
4832 * If the variable is negative (and c is positive),
4833 * then the coefficient of x in v is set to ceil(c/m).
4835 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4836 int *signs)
4838 int i, j;
4839 int total;
4840 isl_vec *v = NULL;
4841 isl_poly *s;
4843 qp = isl_qpolynomial_cow(qp);
4844 if (!qp)
4845 return NULL;
4846 qp->div = isl_mat_cow(qp->div);
4847 if (!qp->div)
4848 goto error;
4850 total = isl_space_dim(qp->dim, isl_dim_all);
4851 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4853 for (i = 0; i < qp->div->n_row; ++i) {
4854 isl_int *row = qp->div->row[i];
4855 v = isl_vec_clr(v);
4856 if (!v)
4857 goto error;
4858 if (isl_int_lt(row[1], row[0])) {
4859 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4860 isl_int_sub_ui(v->el[0], v->el[0], 1);
4861 isl_int_submul(row[1], row[0], v->el[0]);
4863 for (j = 0; j < total; ++j) {
4864 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4865 continue;
4866 if (signs[j] < 0)
4867 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4868 else
4869 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4870 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4872 for (j = 0; j < i; ++j) {
4873 if (isl_int_sgn(row[2 + total + j]) >= 0)
4874 continue;
4875 isl_int_fdiv_q(v->el[1 + total + j],
4876 row[2 + total + j], row[0]);
4877 isl_int_submul(row[2 + total + j],
4878 row[0], v->el[1 + total + j]);
4880 for (j = i + 1; j < qp->div->n_row; ++j) {
4881 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4882 continue;
4883 isl_seq_combine(qp->div->row[j] + 1,
4884 qp->div->ctx->one, qp->div->row[j] + 1,
4885 qp->div->row[j][2 + total + i], v->el, v->size);
4887 isl_int_set_si(v->el[1 + total + i], 1);
4888 s = isl_poly_from_affine(qp->dim->ctx, v->el,
4889 qp->div->ctx->one, v->size);
4890 qp->poly = isl_poly_subs(qp->poly, total + i, 1, &s);
4891 isl_poly_free(s);
4892 if (!qp->poly)
4893 goto error;
4896 isl_vec_free(v);
4897 return qp;
4898 error:
4899 isl_vec_free(v);
4900 isl_qpolynomial_free(qp);
4901 return NULL;
4904 struct isl_to_poly_data {
4905 int sign;
4906 isl_pw_qpolynomial *res;
4907 isl_qpolynomial *qp;
4910 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4911 * We first make all integer divisions positive and then split the
4912 * quasipolynomials into terms with sign data->sign (the direction
4913 * of the requested approximation) and terms with the opposite sign.
4914 * In the first set of terms, each integer division [a/m] is
4915 * overapproximated by a/m, while in the second it is underapproximated
4916 * by (a-(m-1))/m.
4918 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
4919 int *signs, void *user)
4921 struct isl_to_poly_data *data = user;
4922 isl_pw_qpolynomial *t;
4923 isl_qpolynomial *qp, *up, *down;
4925 qp = isl_qpolynomial_copy(data->qp);
4926 qp = make_divs_pos(qp, signs);
4928 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4929 up = qp_drop_floors(up, 0);
4930 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4931 down = qp_drop_floors(down, 1);
4933 isl_qpolynomial_free(qp);
4934 qp = isl_qpolynomial_add(up, down);
4936 t = isl_pw_qpolynomial_alloc(orthant, qp);
4937 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4939 return isl_stat_ok;
4942 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4943 * the polynomial will be an overapproximation. If "sign" is negative,
4944 * it will be an underapproximation. If "sign" is zero, the approximation
4945 * will lie somewhere in between.
4947 * In particular, is sign == 0, we simply drop the floors, turning
4948 * the integer divisions into rational divisions.
4949 * Otherwise, we split the domains into orthants, make all integer divisions
4950 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4951 * depending on the requested sign and the sign of the term in which
4952 * the integer division appears.
4954 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4955 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4957 int i;
4958 struct isl_to_poly_data data;
4960 if (sign == 0)
4961 return pwqp_drop_floors(pwqp);
4963 if (!pwqp)
4964 return NULL;
4966 data.sign = sign;
4967 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4969 for (i = 0; i < pwqp->n; ++i) {
4970 if (pwqp->p[i].qp->div->n_row == 0) {
4971 isl_pw_qpolynomial *t;
4972 t = isl_pw_qpolynomial_alloc(
4973 isl_set_copy(pwqp->p[i].set),
4974 isl_qpolynomial_copy(pwqp->p[i].qp));
4975 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4976 continue;
4978 data.qp = pwqp->p[i].qp;
4979 if (isl_set_foreach_orthant(pwqp->p[i].set,
4980 &to_polynomial_on_orthant, &data) < 0)
4981 goto error;
4984 isl_pw_qpolynomial_free(pwqp);
4986 return data.res;
4987 error:
4988 isl_pw_qpolynomial_free(pwqp);
4989 isl_pw_qpolynomial_free(data.res);
4990 return NULL;
4993 static __isl_give isl_pw_qpolynomial *poly_entry(
4994 __isl_take isl_pw_qpolynomial *pwqp, void *user)
4996 int *sign = user;
4998 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
5001 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
5002 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
5004 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5005 &poly_entry, &sign);
5008 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
5009 __isl_take isl_qpolynomial *qp)
5011 int i, k;
5012 isl_space *dim;
5013 isl_vec *aff = NULL;
5014 isl_basic_map *bmap = NULL;
5015 isl_bool is_affine;
5016 unsigned pos;
5017 unsigned n_div;
5019 if (!qp)
5020 return NULL;
5021 is_affine = isl_poly_is_affine(qp->poly);
5022 if (is_affine < 0)
5023 goto error;
5024 if (!is_affine)
5025 isl_die(qp->dim->ctx, isl_error_invalid,
5026 "input quasi-polynomial not affine", goto error);
5027 aff = isl_qpolynomial_extract_affine(qp);
5028 if (!aff)
5029 goto error;
5030 dim = isl_qpolynomial_get_space(qp);
5031 pos = 1 + isl_space_offset(dim, isl_dim_out);
5032 n_div = qp->div->n_row;
5033 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
5035 for (i = 0; i < n_div; ++i) {
5036 k = isl_basic_map_alloc_div(bmap);
5037 if (k < 0)
5038 goto error;
5039 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
5040 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
5041 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
5042 goto error;
5044 k = isl_basic_map_alloc_equality(bmap);
5045 if (k < 0)
5046 goto error;
5047 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
5048 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
5049 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
5051 isl_vec_free(aff);
5052 isl_qpolynomial_free(qp);
5053 bmap = isl_basic_map_finalize(bmap);
5054 return bmap;
5055 error:
5056 isl_vec_free(aff);
5057 isl_qpolynomial_free(qp);
5058 isl_basic_map_free(bmap);
5059 return NULL;