isl_schedule_node_get_schedule_depth: return isl_size
[isl.git] / isl_polynomial.c
blob8e69f52c67629500fe142dfe60c9bd4b4002fb48
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 /* Return the domain space of "qp".
442 static __isl_keep isl_space *isl_qpolynomial_peek_domain_space(
443 __isl_keep isl_qpolynomial *qp)
445 return qp ? qp->dim : NULL;
448 /* Return a copy of the domain space of "qp".
450 __isl_give isl_space *isl_qpolynomial_get_domain_space(
451 __isl_keep isl_qpolynomial *qp)
453 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp));
456 /* Return a copy of the local space on which "qp" is defined.
458 static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
459 __isl_keep isl_qpolynomial *qp)
461 isl_space *space;
463 if (!qp)
464 return NULL;
466 space = isl_qpolynomial_get_domain_space(qp);
467 return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
470 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
472 isl_space *space;
473 if (!qp)
474 return NULL;
475 space = isl_space_copy(qp->dim);
476 space = isl_space_from_domain(space);
477 space = isl_space_add_dims(space, isl_dim_out, 1);
478 return space;
481 /* Return the number of variables of the given type in the domain of "qp".
483 isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
484 enum isl_dim_type type)
486 isl_space *space;
487 isl_size dim;
489 space = isl_qpolynomial_peek_domain_space(qp);
491 if (!space)
492 return isl_size_error;
493 if (type == isl_dim_div)
494 return qp->div->n_row;
495 dim = isl_space_dim(space, type);
496 if (dim < 0)
497 return isl_size_error;
498 if (type == isl_dim_all) {
499 isl_size n_div;
501 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
502 if (n_div < 0)
503 return isl_size_error;
504 dim += n_div;
506 return dim;
509 /* Given the type of a dimension of an isl_qpolynomial,
510 * return the type of the corresponding dimension in its domain.
511 * This function is only called for "type" equal to isl_dim_in or
512 * isl_dim_param.
514 static enum isl_dim_type domain_type(enum isl_dim_type type)
516 return type == isl_dim_in ? isl_dim_set : type;
519 /* Externally, an isl_qpolynomial has a map space, but internally, the
520 * ls field corresponds to the domain of that space.
522 isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
523 enum isl_dim_type type)
525 if (!qp)
526 return isl_size_error;
527 if (type == isl_dim_out)
528 return 1;
529 type = domain_type(type);
530 return isl_qpolynomial_domain_dim(qp, type);
533 /* Return the offset of the first variable of type "type" within
534 * the variables of the domain of "qp".
536 static int isl_qpolynomial_domain_var_offset(__isl_keep isl_qpolynomial *qp,
537 enum isl_dim_type type)
539 isl_space *space;
541 space = isl_qpolynomial_peek_domain_space(qp);
542 if (!space)
543 return -1;
545 switch (type) {
546 case isl_dim_param:
547 case isl_dim_set: return isl_space_offset(space, type);
548 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
549 case isl_dim_cst:
550 default:
551 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
552 "invalid dimension type", return -1);
556 /* Return the offset of the first coefficient of type "type" in
557 * the domain of "qp".
559 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
560 enum isl_dim_type type)
562 switch (type) {
563 case isl_dim_cst:
564 return 0;
565 case isl_dim_param:
566 case isl_dim_set:
567 case isl_dim_div:
568 return 1 + isl_qpolynomial_domain_var_offset(qp, type);
569 default:
570 return 0;
574 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
576 return qp ? isl_poly_is_zero(qp->poly) : isl_bool_error;
579 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
581 return qp ? isl_poly_is_one(qp->poly) : isl_bool_error;
584 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
586 return qp ? isl_poly_is_nan(qp->poly) : isl_bool_error;
589 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
591 return qp ? isl_poly_is_infty(qp->poly) : isl_bool_error;
594 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
596 return qp ? isl_poly_is_neginfty(qp->poly) : isl_bool_error;
599 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
601 return qp ? isl_poly_sgn(qp->poly) : 0;
604 static void poly_free_cst(__isl_take isl_poly_cst *cst)
606 isl_int_clear(cst->n);
607 isl_int_clear(cst->d);
610 static void poly_free_rec(__isl_take isl_poly_rec *rec)
612 int i;
614 for (i = 0; i < rec->n; ++i)
615 isl_poly_free(rec->p[i]);
618 __isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
620 if (!poly)
621 return NULL;
623 poly->ref++;
624 return poly;
627 __isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
629 isl_poly_cst *cst;
630 isl_poly_cst *dup;
632 cst = isl_poly_as_cst(poly);
633 if (!cst)
634 return NULL;
636 dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
637 if (!dup)
638 return NULL;
639 isl_int_set(dup->n, cst->n);
640 isl_int_set(dup->d, cst->d);
642 return &dup->poly;
645 __isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
647 int i;
648 isl_poly_rec *rec;
649 isl_poly_rec *dup;
651 rec = isl_poly_as_rec(poly);
652 if (!rec)
653 return NULL;
655 dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
656 if (!dup)
657 return NULL;
659 for (i = 0; i < rec->n; ++i) {
660 dup->p[i] = isl_poly_copy(rec->p[i]);
661 if (!dup->p[i])
662 goto error;
663 dup->n++;
666 return &dup->poly;
667 error:
668 isl_poly_free(&dup->poly);
669 return NULL;
672 __isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
674 isl_bool is_cst;
676 is_cst = isl_poly_is_cst(poly);
677 if (is_cst < 0)
678 return NULL;
679 if (is_cst)
680 return isl_poly_dup_cst(poly);
681 else
682 return isl_poly_dup_rec(poly);
685 __isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
687 if (!poly)
688 return NULL;
690 if (poly->ref == 1)
691 return poly;
692 poly->ref--;
693 return isl_poly_dup(poly);
696 __isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
698 if (!poly)
699 return NULL;
701 if (--poly->ref > 0)
702 return NULL;
704 if (poly->var < 0)
705 poly_free_cst((isl_poly_cst *) poly);
706 else
707 poly_free_rec((isl_poly_rec *) poly);
709 isl_ctx_deref(poly->ctx);
710 free(poly);
711 return NULL;
714 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
716 isl_int gcd;
718 isl_int_init(gcd);
719 isl_int_gcd(gcd, cst->n, cst->d);
720 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
721 isl_int_divexact(cst->n, cst->n, gcd);
722 isl_int_divexact(cst->d, cst->d, gcd);
724 isl_int_clear(gcd);
727 __isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
728 __isl_take isl_poly *poly2)
730 isl_poly_cst *cst1;
731 isl_poly_cst *cst2;
733 poly1 = isl_poly_cow(poly1);
734 if (!poly1 || !poly2)
735 goto error;
737 cst1 = isl_poly_as_cst(poly1);
738 cst2 = isl_poly_as_cst(poly2);
740 if (isl_int_eq(cst1->d, cst2->d))
741 isl_int_add(cst1->n, cst1->n, cst2->n);
742 else {
743 isl_int_mul(cst1->n, cst1->n, cst2->d);
744 isl_int_addmul(cst1->n, cst2->n, cst1->d);
745 isl_int_mul(cst1->d, cst1->d, cst2->d);
748 isl_poly_cst_reduce(cst1);
750 isl_poly_free(poly2);
751 return poly1;
752 error:
753 isl_poly_free(poly1);
754 isl_poly_free(poly2);
755 return NULL;
758 static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
760 struct isl_ctx *ctx;
762 if (!poly)
763 return NULL;
764 ctx = poly->ctx;
765 isl_poly_free(poly);
766 return isl_poly_zero(ctx);
769 static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
771 isl_poly_rec *rec;
772 isl_poly *cst;
774 if (!poly)
775 return NULL;
777 rec = isl_poly_as_rec(poly);
778 if (!rec)
779 goto error;
780 cst = isl_poly_copy(rec->p[0]);
781 isl_poly_free(poly);
782 return cst;
783 error:
784 isl_poly_free(poly);
785 return NULL;
788 __isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
789 __isl_take isl_poly *poly2)
791 int i;
792 isl_bool is_zero, is_nan, is_cst;
793 isl_poly_rec *rec1, *rec2;
795 if (!poly1 || !poly2)
796 goto error;
798 is_nan = isl_poly_is_nan(poly1);
799 if (is_nan < 0)
800 goto error;
801 if (is_nan) {
802 isl_poly_free(poly2);
803 return poly1;
806 is_nan = isl_poly_is_nan(poly2);
807 if (is_nan < 0)
808 goto error;
809 if (is_nan) {
810 isl_poly_free(poly1);
811 return poly2;
814 is_zero = isl_poly_is_zero(poly1);
815 if (is_zero < 0)
816 goto error;
817 if (is_zero) {
818 isl_poly_free(poly1);
819 return poly2;
822 is_zero = isl_poly_is_zero(poly2);
823 if (is_zero < 0)
824 goto error;
825 if (is_zero) {
826 isl_poly_free(poly2);
827 return poly1;
830 if (poly1->var < poly2->var)
831 return isl_poly_sum(poly2, poly1);
833 if (poly2->var < poly1->var) {
834 isl_poly_rec *rec;
835 isl_bool is_infty;
837 is_infty = isl_poly_is_infty(poly2);
838 if (is_infty >= 0 && !is_infty)
839 is_infty = isl_poly_is_neginfty(poly2);
840 if (is_infty < 0)
841 goto error;
842 if (is_infty) {
843 isl_poly_free(poly1);
844 return poly2;
846 poly1 = isl_poly_cow(poly1);
847 rec = isl_poly_as_rec(poly1);
848 if (!rec)
849 goto error;
850 rec->p[0] = isl_poly_sum(rec->p[0], poly2);
851 if (rec->n == 1)
852 poly1 = replace_by_constant_term(poly1);
853 return poly1;
856 is_cst = isl_poly_is_cst(poly1);
857 if (is_cst < 0)
858 goto error;
859 if (is_cst)
860 return isl_poly_sum_cst(poly1, poly2);
862 rec1 = isl_poly_as_rec(poly1);
863 rec2 = isl_poly_as_rec(poly2);
864 if (!rec1 || !rec2)
865 goto error;
867 if (rec1->n < rec2->n)
868 return isl_poly_sum(poly2, poly1);
870 poly1 = isl_poly_cow(poly1);
871 rec1 = isl_poly_as_rec(poly1);
872 if (!rec1)
873 goto error;
875 for (i = rec2->n - 1; i >= 0; --i) {
876 isl_bool is_zero;
878 rec1->p[i] = isl_poly_sum(rec1->p[i],
879 isl_poly_copy(rec2->p[i]));
880 if (!rec1->p[i])
881 goto error;
882 if (i != rec1->n - 1)
883 continue;
884 is_zero = isl_poly_is_zero(rec1->p[i]);
885 if (is_zero < 0)
886 goto error;
887 if (is_zero) {
888 isl_poly_free(rec1->p[i]);
889 rec1->n--;
893 if (rec1->n == 0)
894 poly1 = replace_by_zero(poly1);
895 else if (rec1->n == 1)
896 poly1 = replace_by_constant_term(poly1);
898 isl_poly_free(poly2);
900 return poly1;
901 error:
902 isl_poly_free(poly1);
903 isl_poly_free(poly2);
904 return NULL;
907 __isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
908 isl_int v)
910 isl_poly_cst *cst;
912 poly = isl_poly_cow(poly);
913 if (!poly)
914 return NULL;
916 cst = isl_poly_as_cst(poly);
918 isl_int_addmul(cst->n, cst->d, v);
920 return poly;
923 __isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
925 isl_bool is_cst;
926 isl_poly_rec *rec;
928 is_cst = isl_poly_is_cst(poly);
929 if (is_cst < 0)
930 return isl_poly_free(poly);
931 if (is_cst)
932 return isl_poly_cst_add_isl_int(poly, v);
934 poly = isl_poly_cow(poly);
935 rec = isl_poly_as_rec(poly);
936 if (!rec)
937 goto error;
939 rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
940 if (!rec->p[0])
941 goto error;
943 return poly;
944 error:
945 isl_poly_free(poly);
946 return NULL;
949 __isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
950 isl_int v)
952 isl_bool is_zero;
953 isl_poly_cst *cst;
955 is_zero = isl_poly_is_zero(poly);
956 if (is_zero < 0)
957 return isl_poly_free(poly);
958 if (is_zero)
959 return poly;
961 poly = isl_poly_cow(poly);
962 if (!poly)
963 return NULL;
965 cst = isl_poly_as_cst(poly);
967 isl_int_mul(cst->n, cst->n, v);
969 return poly;
972 __isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
974 int i;
975 isl_bool is_cst;
976 isl_poly_rec *rec;
978 is_cst = isl_poly_is_cst(poly);
979 if (is_cst < 0)
980 return isl_poly_free(poly);
981 if (is_cst)
982 return isl_poly_cst_mul_isl_int(poly, v);
984 poly = isl_poly_cow(poly);
985 rec = isl_poly_as_rec(poly);
986 if (!rec)
987 goto error;
989 for (i = 0; i < rec->n; ++i) {
990 rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
991 if (!rec->p[i])
992 goto error;
995 return poly;
996 error:
997 isl_poly_free(poly);
998 return NULL;
1001 /* Multiply the constant polynomial "poly" by "v".
1003 static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
1004 __isl_keep isl_val *v)
1006 isl_bool is_zero;
1007 isl_poly_cst *cst;
1009 is_zero = isl_poly_is_zero(poly);
1010 if (is_zero < 0)
1011 return isl_poly_free(poly);
1012 if (is_zero)
1013 return poly;
1015 poly = isl_poly_cow(poly);
1016 if (!poly)
1017 return NULL;
1019 cst = isl_poly_as_cst(poly);
1021 isl_int_mul(cst->n, cst->n, v->n);
1022 isl_int_mul(cst->d, cst->d, v->d);
1023 isl_poly_cst_reduce(cst);
1025 return poly;
1028 /* Multiply the polynomial "poly" by "v".
1030 static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
1031 __isl_keep isl_val *v)
1033 int i;
1034 isl_bool is_cst;
1035 isl_poly_rec *rec;
1037 is_cst = isl_poly_is_cst(poly);
1038 if (is_cst < 0)
1039 return isl_poly_free(poly);
1040 if (is_cst)
1041 return isl_poly_cst_scale_val(poly, v);
1043 poly = isl_poly_cow(poly);
1044 rec = isl_poly_as_rec(poly);
1045 if (!rec)
1046 goto error;
1048 for (i = 0; i < rec->n; ++i) {
1049 rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1050 if (!rec->p[i])
1051 goto error;
1054 return poly;
1055 error:
1056 isl_poly_free(poly);
1057 return NULL;
1060 __isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
1061 __isl_take isl_poly *poly2)
1063 isl_poly_cst *cst1;
1064 isl_poly_cst *cst2;
1066 poly1 = isl_poly_cow(poly1);
1067 if (!poly1 || !poly2)
1068 goto error;
1070 cst1 = isl_poly_as_cst(poly1);
1071 cst2 = isl_poly_as_cst(poly2);
1073 isl_int_mul(cst1->n, cst1->n, cst2->n);
1074 isl_int_mul(cst1->d, cst1->d, cst2->d);
1076 isl_poly_cst_reduce(cst1);
1078 isl_poly_free(poly2);
1079 return poly1;
1080 error:
1081 isl_poly_free(poly1);
1082 isl_poly_free(poly2);
1083 return NULL;
1086 __isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
1087 __isl_take isl_poly *poly2)
1089 isl_poly_rec *rec1;
1090 isl_poly_rec *rec2;
1091 isl_poly_rec *res = NULL;
1092 int i, j;
1093 int size;
1095 rec1 = isl_poly_as_rec(poly1);
1096 rec2 = isl_poly_as_rec(poly2);
1097 if (!rec1 || !rec2)
1098 goto error;
1099 size = rec1->n + rec2->n - 1;
1100 res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1101 if (!res)
1102 goto error;
1104 for (i = 0; i < rec1->n; ++i) {
1105 res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1106 isl_poly_copy(rec1->p[i]));
1107 if (!res->p[i])
1108 goto error;
1109 res->n++;
1111 for (; i < size; ++i) {
1112 res->p[i] = isl_poly_zero(poly1->ctx);
1113 if (!res->p[i])
1114 goto error;
1115 res->n++;
1117 for (i = 0; i < rec1->n; ++i) {
1118 for (j = 1; j < rec2->n; ++j) {
1119 isl_poly *poly;
1120 poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1121 isl_poly_copy(rec1->p[i]));
1122 res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1123 if (!res->p[i + j])
1124 goto error;
1128 isl_poly_free(poly1);
1129 isl_poly_free(poly2);
1131 return &res->poly;
1132 error:
1133 isl_poly_free(poly1);
1134 isl_poly_free(poly2);
1135 isl_poly_free(&res->poly);
1136 return NULL;
1139 __isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
1140 __isl_take isl_poly *poly2)
1142 isl_bool is_zero, is_nan, is_one, is_cst;
1144 if (!poly1 || !poly2)
1145 goto error;
1147 is_nan = isl_poly_is_nan(poly1);
1148 if (is_nan < 0)
1149 goto error;
1150 if (is_nan) {
1151 isl_poly_free(poly2);
1152 return poly1;
1155 is_nan = isl_poly_is_nan(poly2);
1156 if (is_nan < 0)
1157 goto error;
1158 if (is_nan) {
1159 isl_poly_free(poly1);
1160 return poly2;
1163 is_zero = isl_poly_is_zero(poly1);
1164 if (is_zero < 0)
1165 goto error;
1166 if (is_zero) {
1167 isl_poly_free(poly2);
1168 return poly1;
1171 is_zero = isl_poly_is_zero(poly2);
1172 if (is_zero < 0)
1173 goto error;
1174 if (is_zero) {
1175 isl_poly_free(poly1);
1176 return poly2;
1179 is_one = isl_poly_is_one(poly1);
1180 if (is_one < 0)
1181 goto error;
1182 if (is_one) {
1183 isl_poly_free(poly1);
1184 return poly2;
1187 is_one = isl_poly_is_one(poly2);
1188 if (is_one < 0)
1189 goto error;
1190 if (is_one) {
1191 isl_poly_free(poly2);
1192 return poly1;
1195 if (poly1->var < poly2->var)
1196 return isl_poly_mul(poly2, poly1);
1198 if (poly2->var < poly1->var) {
1199 int i;
1200 isl_poly_rec *rec;
1201 isl_bool is_infty;
1203 is_infty = isl_poly_is_infty(poly2);
1204 if (is_infty >= 0 && !is_infty)
1205 is_infty = isl_poly_is_neginfty(poly2);
1206 if (is_infty < 0)
1207 goto error;
1208 if (is_infty) {
1209 isl_ctx *ctx = poly1->ctx;
1210 isl_poly_free(poly1);
1211 isl_poly_free(poly2);
1212 return isl_poly_nan(ctx);
1214 poly1 = isl_poly_cow(poly1);
1215 rec = isl_poly_as_rec(poly1);
1216 if (!rec)
1217 goto error;
1219 for (i = 0; i < rec->n; ++i) {
1220 rec->p[i] = isl_poly_mul(rec->p[i],
1221 isl_poly_copy(poly2));
1222 if (!rec->p[i])
1223 goto error;
1225 isl_poly_free(poly2);
1226 return poly1;
1229 is_cst = isl_poly_is_cst(poly1);
1230 if (is_cst < 0)
1231 goto error;
1232 if (is_cst)
1233 return isl_poly_mul_cst(poly1, poly2);
1235 return isl_poly_mul_rec(poly1, poly2);
1236 error:
1237 isl_poly_free(poly1);
1238 isl_poly_free(poly2);
1239 return NULL;
1242 __isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
1244 isl_poly *res;
1246 if (!poly)
1247 return NULL;
1248 if (power == 1)
1249 return poly;
1251 if (power % 2)
1252 res = isl_poly_copy(poly);
1253 else
1254 res = isl_poly_one(poly->ctx);
1256 while (power >>= 1) {
1257 poly = isl_poly_mul(poly, isl_poly_copy(poly));
1258 if (power % 2)
1259 res = isl_poly_mul(res, isl_poly_copy(poly));
1262 isl_poly_free(poly);
1263 return res;
1266 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1267 unsigned n_div, __isl_take isl_poly *poly)
1269 struct isl_qpolynomial *qp = NULL;
1270 isl_size total;
1272 total = isl_space_dim(space, isl_dim_all);
1273 if (total < 0 || !poly)
1274 goto error;
1276 if (!isl_space_is_set(space))
1277 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1278 "domain of polynomial should be a set", goto error);
1280 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1281 if (!qp)
1282 goto error;
1284 qp->ref = 1;
1285 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1286 if (!qp->div)
1287 goto error;
1289 qp->dim = space;
1290 qp->poly = poly;
1292 return qp;
1293 error:
1294 isl_space_free(space);
1295 isl_poly_free(poly);
1296 isl_qpolynomial_free(qp);
1297 return NULL;
1300 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1302 if (!qp)
1303 return NULL;
1305 qp->ref++;
1306 return qp;
1309 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1311 struct isl_qpolynomial *dup;
1313 if (!qp)
1314 return NULL;
1316 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1317 isl_poly_copy(qp->poly));
1318 if (!dup)
1319 return NULL;
1320 isl_mat_free(dup->div);
1321 dup->div = isl_mat_copy(qp->div);
1322 if (!dup->div)
1323 goto error;
1325 return dup;
1326 error:
1327 isl_qpolynomial_free(dup);
1328 return NULL;
1331 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1333 if (!qp)
1334 return NULL;
1336 if (qp->ref == 1)
1337 return qp;
1338 qp->ref--;
1339 return isl_qpolynomial_dup(qp);
1342 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1343 __isl_take isl_qpolynomial *qp)
1345 if (!qp)
1346 return NULL;
1348 if (--qp->ref > 0)
1349 return NULL;
1351 isl_space_free(qp->dim);
1352 isl_mat_free(qp->div);
1353 isl_poly_free(qp->poly);
1355 free(qp);
1356 return NULL;
1359 __isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
1361 int i;
1362 isl_poly_rec *rec;
1363 isl_poly_cst *cst;
1365 rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1366 if (!rec)
1367 return NULL;
1368 for (i = 0; i < 1 + power; ++i) {
1369 rec->p[i] = isl_poly_zero(ctx);
1370 if (!rec->p[i])
1371 goto error;
1372 rec->n++;
1374 cst = isl_poly_as_cst(rec->p[power]);
1375 isl_int_set_si(cst->n, 1);
1377 return &rec->poly;
1378 error:
1379 isl_poly_free(&rec->poly);
1380 return NULL;
1383 /* r array maps original positions to new positions.
1385 static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
1387 int i;
1388 isl_bool is_cst;
1389 isl_poly_rec *rec;
1390 isl_poly *base;
1391 isl_poly *res;
1393 is_cst = isl_poly_is_cst(poly);
1394 if (is_cst < 0)
1395 return isl_poly_free(poly);
1396 if (is_cst)
1397 return poly;
1399 rec = isl_poly_as_rec(poly);
1400 if (!rec)
1401 goto error;
1403 isl_assert(poly->ctx, rec->n >= 1, goto error);
1405 base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1406 res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1408 for (i = rec->n - 2; i >= 0; --i) {
1409 res = isl_poly_mul(res, isl_poly_copy(base));
1410 res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1413 isl_poly_free(base);
1414 isl_poly_free(poly);
1416 return res;
1417 error:
1418 isl_poly_free(poly);
1419 return NULL;
1422 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1423 __isl_keep isl_mat *div2)
1425 int n_row, n_col;
1426 isl_bool equal;
1428 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1429 div1->n_col >= div2->n_col,
1430 return isl_bool_error);
1432 if (div1->n_row == div2->n_row)
1433 return isl_mat_is_equal(div1, div2);
1435 n_row = div1->n_row;
1436 n_col = div1->n_col;
1437 div1->n_row = div2->n_row;
1438 div1->n_col = div2->n_col;
1440 equal = isl_mat_is_equal(div1, div2);
1442 div1->n_row = n_row;
1443 div1->n_col = n_col;
1445 return equal;
1448 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1450 int li, lj;
1452 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1453 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1455 if (li != lj)
1456 return li - lj;
1458 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1461 struct isl_div_sort_info {
1462 isl_mat *div;
1463 int row;
1466 static int div_sort_cmp(const void *p1, const void *p2)
1468 const struct isl_div_sort_info *i1, *i2;
1469 i1 = (const struct isl_div_sort_info *) p1;
1470 i2 = (const struct isl_div_sort_info *) p2;
1472 return cmp_row(i1->div, i1->row, i2->row);
1475 /* Sort divs and remove duplicates.
1477 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1479 int i;
1480 int skip;
1481 int len;
1482 struct isl_div_sort_info *array = NULL;
1483 int *pos = NULL, *at = NULL;
1484 int *reordering = NULL;
1485 int div_pos;
1487 if (!qp)
1488 return NULL;
1489 if (qp->div->n_row <= 1)
1490 return qp;
1492 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
1493 if (div_pos < 0)
1494 return isl_qpolynomial_free(qp);
1496 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1497 qp->div->n_row);
1498 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1499 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1500 len = qp->div->n_col - 2;
1501 reordering = isl_alloc_array(qp->div->ctx, int, len);
1502 if (!array || !pos || !at || !reordering)
1503 goto error;
1505 for (i = 0; i < qp->div->n_row; ++i) {
1506 array[i].div = qp->div;
1507 array[i].row = i;
1508 pos[i] = i;
1509 at[i] = i;
1512 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1513 div_sort_cmp);
1515 for (i = 0; i < div_pos; ++i)
1516 reordering[i] = i;
1518 for (i = 0; i < qp->div->n_row; ++i) {
1519 if (pos[array[i].row] == i)
1520 continue;
1521 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1522 pos[at[i]] = pos[array[i].row];
1523 at[pos[array[i].row]] = at[i];
1524 at[i] = array[i].row;
1525 pos[array[i].row] = i;
1528 skip = 0;
1529 for (i = 0; i < len - div_pos; ++i) {
1530 if (i > 0 &&
1531 isl_seq_eq(qp->div->row[i - skip - 1],
1532 qp->div->row[i - skip], qp->div->n_col)) {
1533 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1534 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1535 2 + div_pos + i - skip);
1536 qp->div = isl_mat_drop_cols(qp->div,
1537 2 + div_pos + i - skip, 1);
1538 skip++;
1540 reordering[div_pos + array[i].row] = div_pos + i - skip;
1543 qp->poly = reorder(qp->poly, reordering);
1545 if (!qp->poly || !qp->div)
1546 goto error;
1548 free(at);
1549 free(pos);
1550 free(array);
1551 free(reordering);
1553 return qp;
1554 error:
1555 free(at);
1556 free(pos);
1557 free(array);
1558 free(reordering);
1559 isl_qpolynomial_free(qp);
1560 return NULL;
1563 static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
1564 int first)
1566 int i;
1567 isl_bool is_cst;
1568 isl_poly_rec *rec;
1570 is_cst = isl_poly_is_cst(poly);
1571 if (is_cst < 0)
1572 return isl_poly_free(poly);
1573 if (is_cst)
1574 return poly;
1576 if (poly->var < first)
1577 return poly;
1579 if (exp[poly->var - first] == poly->var - first)
1580 return poly;
1582 poly = isl_poly_cow(poly);
1583 if (!poly)
1584 goto error;
1586 poly->var = exp[poly->var - first] + first;
1588 rec = isl_poly_as_rec(poly);
1589 if (!rec)
1590 goto error;
1592 for (i = 0; i < rec->n; ++i) {
1593 rec->p[i] = expand(rec->p[i], exp, first);
1594 if (!rec->p[i])
1595 goto error;
1598 return poly;
1599 error:
1600 isl_poly_free(poly);
1601 return NULL;
1604 static __isl_give isl_qpolynomial *with_merged_divs(
1605 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1606 __isl_take isl_qpolynomial *qp2),
1607 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1609 int *exp1 = NULL;
1610 int *exp2 = NULL;
1611 isl_mat *div = NULL;
1612 int n_div1, n_div2;
1614 qp1 = isl_qpolynomial_cow(qp1);
1615 qp2 = isl_qpolynomial_cow(qp2);
1617 if (!qp1 || !qp2)
1618 goto error;
1620 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1621 qp1->div->n_col >= qp2->div->n_col, goto error);
1623 n_div1 = qp1->div->n_row;
1624 n_div2 = qp2->div->n_row;
1625 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1626 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1627 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1628 goto error;
1630 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1631 if (!div)
1632 goto error;
1634 isl_mat_free(qp1->div);
1635 qp1->div = isl_mat_copy(div);
1636 isl_mat_free(qp2->div);
1637 qp2->div = isl_mat_copy(div);
1639 qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1640 qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1642 if (!qp1->poly || !qp2->poly)
1643 goto error;
1645 isl_mat_free(div);
1646 free(exp1);
1647 free(exp2);
1649 return fn(qp1, qp2);
1650 error:
1651 isl_mat_free(div);
1652 free(exp1);
1653 free(exp2);
1654 isl_qpolynomial_free(qp1);
1655 isl_qpolynomial_free(qp2);
1656 return NULL;
1659 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1660 __isl_take isl_qpolynomial *qp2)
1662 isl_bool compatible;
1664 qp1 = isl_qpolynomial_cow(qp1);
1666 if (!qp1 || !qp2)
1667 goto error;
1669 if (qp1->div->n_row < qp2->div->n_row)
1670 return isl_qpolynomial_add(qp2, qp1);
1672 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1673 compatible = compatible_divs(qp1->div, qp2->div);
1674 if (compatible < 0)
1675 goto error;
1676 if (!compatible)
1677 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1679 qp1->poly = isl_poly_sum(qp1->poly, isl_poly_copy(qp2->poly));
1680 if (!qp1->poly)
1681 goto error;
1683 isl_qpolynomial_free(qp2);
1685 return qp1;
1686 error:
1687 isl_qpolynomial_free(qp1);
1688 isl_qpolynomial_free(qp2);
1689 return NULL;
1692 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1693 __isl_keep isl_set *dom,
1694 __isl_take isl_qpolynomial *qp1,
1695 __isl_take isl_qpolynomial *qp2)
1697 qp1 = isl_qpolynomial_add(qp1, qp2);
1698 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1699 return qp1;
1702 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1703 __isl_take isl_qpolynomial *qp2)
1705 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1708 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1709 __isl_take isl_qpolynomial *qp, isl_int v)
1711 if (isl_int_is_zero(v))
1712 return qp;
1714 qp = isl_qpolynomial_cow(qp);
1715 if (!qp)
1716 return NULL;
1718 qp->poly = isl_poly_add_isl_int(qp->poly, v);
1719 if (!qp->poly)
1720 goto error;
1722 return qp;
1723 error:
1724 isl_qpolynomial_free(qp);
1725 return NULL;
1729 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1731 if (!qp)
1732 return NULL;
1734 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1737 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1738 __isl_take isl_qpolynomial *qp, isl_int v)
1740 if (isl_int_is_one(v))
1741 return qp;
1743 if (qp && isl_int_is_zero(v)) {
1744 isl_qpolynomial *zero;
1745 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1746 isl_qpolynomial_free(qp);
1747 return zero;
1750 qp = isl_qpolynomial_cow(qp);
1751 if (!qp)
1752 return NULL;
1754 qp->poly = isl_poly_mul_isl_int(qp->poly, v);
1755 if (!qp->poly)
1756 goto error;
1758 return qp;
1759 error:
1760 isl_qpolynomial_free(qp);
1761 return NULL;
1764 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1765 __isl_take isl_qpolynomial *qp, isl_int v)
1767 return isl_qpolynomial_mul_isl_int(qp, v);
1770 /* Multiply "qp" by "v".
1772 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1773 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1775 if (!qp || !v)
1776 goto error;
1778 if (!isl_val_is_rat(v))
1779 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1780 "expecting rational factor", goto error);
1782 if (isl_val_is_one(v)) {
1783 isl_val_free(v);
1784 return qp;
1787 if (isl_val_is_zero(v)) {
1788 isl_space *space;
1790 space = isl_qpolynomial_get_domain_space(qp);
1791 isl_qpolynomial_free(qp);
1792 isl_val_free(v);
1793 return isl_qpolynomial_zero_on_domain(space);
1796 qp = isl_qpolynomial_cow(qp);
1797 if (!qp)
1798 goto error;
1800 qp->poly = isl_poly_scale_val(qp->poly, v);
1801 if (!qp->poly)
1802 qp = isl_qpolynomial_free(qp);
1804 isl_val_free(v);
1805 return qp;
1806 error:
1807 isl_val_free(v);
1808 isl_qpolynomial_free(qp);
1809 return NULL;
1812 /* Divide "qp" by "v".
1814 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1815 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1817 if (!qp || !v)
1818 goto error;
1820 if (!isl_val_is_rat(v))
1821 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1822 "expecting rational factor", goto error);
1823 if (isl_val_is_zero(v))
1824 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1825 "cannot scale down by zero", goto error);
1827 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1828 error:
1829 isl_val_free(v);
1830 isl_qpolynomial_free(qp);
1831 return NULL;
1834 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1835 __isl_take isl_qpolynomial *qp2)
1837 isl_bool compatible;
1839 qp1 = isl_qpolynomial_cow(qp1);
1841 if (!qp1 || !qp2)
1842 goto error;
1844 if (qp1->div->n_row < qp2->div->n_row)
1845 return isl_qpolynomial_mul(qp2, qp1);
1847 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1848 compatible = compatible_divs(qp1->div, qp2->div);
1849 if (compatible < 0)
1850 goto error;
1851 if (!compatible)
1852 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1854 qp1->poly = isl_poly_mul(qp1->poly, isl_poly_copy(qp2->poly));
1855 if (!qp1->poly)
1856 goto error;
1858 isl_qpolynomial_free(qp2);
1860 return qp1;
1861 error:
1862 isl_qpolynomial_free(qp1);
1863 isl_qpolynomial_free(qp2);
1864 return NULL;
1867 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1868 unsigned power)
1870 qp = isl_qpolynomial_cow(qp);
1872 if (!qp)
1873 return NULL;
1875 qp->poly = isl_poly_pow(qp->poly, power);
1876 if (!qp->poly)
1877 goto error;
1879 return qp;
1880 error:
1881 isl_qpolynomial_free(qp);
1882 return NULL;
1885 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1886 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1888 int i;
1890 if (power == 1)
1891 return pwqp;
1893 pwqp = isl_pw_qpolynomial_cow(pwqp);
1894 if (!pwqp)
1895 return NULL;
1897 for (i = 0; i < pwqp->n; ++i) {
1898 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1899 if (!pwqp->p[i].qp)
1900 return isl_pw_qpolynomial_free(pwqp);
1903 return pwqp;
1906 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1907 __isl_take isl_space *domain)
1909 if (!domain)
1910 return NULL;
1911 return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
1914 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1915 __isl_take isl_space *domain)
1917 if (!domain)
1918 return NULL;
1919 return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
1922 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1923 __isl_take isl_space *domain)
1925 if (!domain)
1926 return NULL;
1927 return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
1930 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1931 __isl_take isl_space *domain)
1933 if (!domain)
1934 return NULL;
1935 return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
1938 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1939 __isl_take isl_space *domain)
1941 if (!domain)
1942 return NULL;
1943 return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
1946 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1947 __isl_take isl_space *domain,
1948 isl_int v)
1950 struct isl_qpolynomial *qp;
1951 isl_poly_cst *cst;
1953 qp = isl_qpolynomial_zero_on_domain(domain);
1954 if (!qp)
1955 return NULL;
1957 cst = isl_poly_as_cst(qp->poly);
1958 isl_int_set(cst->n, v);
1960 return qp;
1963 isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1964 isl_int *n, isl_int *d)
1966 isl_bool is_cst;
1967 isl_poly_cst *cst;
1969 if (!qp)
1970 return isl_bool_error;
1972 is_cst = isl_poly_is_cst(qp->poly);
1973 if (is_cst < 0 || !is_cst)
1974 return is_cst;
1976 cst = isl_poly_as_cst(qp->poly);
1977 if (!cst)
1978 return isl_bool_error;
1980 if (n)
1981 isl_int_set(*n, cst->n);
1982 if (d)
1983 isl_int_set(*d, cst->d);
1985 return isl_bool_true;
1988 /* Return the constant term of "poly".
1990 static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
1992 isl_bool is_cst;
1993 isl_poly_cst *cst;
1995 if (!poly)
1996 return NULL;
1998 while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
1999 isl_poly_rec *rec;
2001 rec = isl_poly_as_rec(poly);
2002 if (!rec)
2003 return NULL;
2004 poly = rec->p[0];
2006 if (is_cst < 0)
2007 return NULL;
2009 cst = isl_poly_as_cst(poly);
2010 if (!cst)
2011 return NULL;
2012 return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
2015 /* Return the constant term of "qp".
2017 __isl_give isl_val *isl_qpolynomial_get_constant_val(
2018 __isl_keep isl_qpolynomial *qp)
2020 if (!qp)
2021 return NULL;
2023 return isl_poly_get_constant_val(qp->poly);
2026 isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
2028 isl_bool is_cst;
2029 isl_poly_rec *rec;
2031 if (!poly)
2032 return isl_bool_error;
2034 if (poly->var < 0)
2035 return isl_bool_true;
2037 rec = isl_poly_as_rec(poly);
2038 if (!rec)
2039 return isl_bool_error;
2041 if (rec->n > 2)
2042 return isl_bool_false;
2044 isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
2046 is_cst = isl_poly_is_cst(rec->p[1]);
2047 if (is_cst < 0 || !is_cst)
2048 return is_cst;
2050 return isl_poly_is_affine(rec->p[0]);
2053 isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
2055 if (!qp)
2056 return isl_bool_error;
2058 if (qp->div->n_row > 0)
2059 return isl_bool_false;
2061 return isl_poly_is_affine(qp->poly);
2064 static void update_coeff(__isl_keep isl_vec *aff,
2065 __isl_keep isl_poly_cst *cst, int pos)
2067 isl_int gcd;
2068 isl_int f;
2070 if (isl_int_is_zero(cst->n))
2071 return;
2073 isl_int_init(gcd);
2074 isl_int_init(f);
2075 isl_int_gcd(gcd, cst->d, aff->el[0]);
2076 isl_int_divexact(f, cst->d, gcd);
2077 isl_int_divexact(gcd, aff->el[0], gcd);
2078 isl_seq_scale(aff->el, aff->el, f, aff->size);
2079 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
2080 isl_int_clear(gcd);
2081 isl_int_clear(f);
2084 int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
2086 isl_poly_cst *cst;
2087 isl_poly_rec *rec;
2089 if (!poly || !aff)
2090 return -1;
2092 if (poly->var < 0) {
2093 isl_poly_cst *cst;
2095 cst = isl_poly_as_cst(poly);
2096 if (!cst)
2097 return -1;
2098 update_coeff(aff, cst, 0);
2099 return 0;
2102 rec = isl_poly_as_rec(poly);
2103 if (!rec)
2104 return -1;
2105 isl_assert(poly->ctx, rec->n == 2, return -1);
2107 cst = isl_poly_as_cst(rec->p[1]);
2108 if (!cst)
2109 return -1;
2110 update_coeff(aff, cst, 1 + poly->var);
2112 return isl_poly_update_affine(rec->p[0], aff);
2115 __isl_give isl_vec *isl_qpolynomial_extract_affine(
2116 __isl_keep isl_qpolynomial *qp)
2118 isl_vec *aff;
2119 isl_size d;
2121 d = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2122 if (d < 0)
2123 return NULL;
2125 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
2126 if (!aff)
2127 return NULL;
2129 isl_seq_clr(aff->el + 1, 1 + d);
2130 isl_int_set_si(aff->el[0], 1);
2132 if (isl_poly_update_affine(qp->poly, aff) < 0)
2133 goto error;
2135 return aff;
2136 error:
2137 isl_vec_free(aff);
2138 return NULL;
2141 /* Compare two quasi-polynomials.
2143 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2144 * than "qp2" and 0 if they are equal.
2146 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2147 __isl_keep isl_qpolynomial *qp2)
2149 int cmp;
2151 if (qp1 == qp2)
2152 return 0;
2153 if (!qp1)
2154 return -1;
2155 if (!qp2)
2156 return 1;
2158 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2159 if (cmp != 0)
2160 return cmp;
2162 cmp = isl_local_cmp(qp1->div, qp2->div);
2163 if (cmp != 0)
2164 return cmp;
2166 return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2169 /* Is "qp1" obviously equal to "qp2"?
2171 * NaN is not equal to anything, not even to another NaN.
2173 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2174 __isl_keep isl_qpolynomial *qp2)
2176 isl_bool equal;
2178 if (!qp1 || !qp2)
2179 return isl_bool_error;
2181 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2182 return isl_bool_false;
2184 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2185 if (equal < 0 || !equal)
2186 return equal;
2188 equal = isl_mat_is_equal(qp1->div, qp2->div);
2189 if (equal < 0 || !equal)
2190 return equal;
2192 return isl_poly_is_equal(qp1->poly, qp2->poly);
2195 static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
2197 int i;
2198 isl_bool is_cst;
2199 isl_poly_rec *rec;
2201 is_cst = isl_poly_is_cst(poly);
2202 if (is_cst < 0)
2203 return isl_stat_error;
2204 if (is_cst) {
2205 isl_poly_cst *cst;
2206 cst = isl_poly_as_cst(poly);
2207 if (!cst)
2208 return isl_stat_error;
2209 isl_int_lcm(*d, *d, cst->d);
2210 return isl_stat_ok;
2213 rec = isl_poly_as_rec(poly);
2214 if (!rec)
2215 return isl_stat_error;
2217 for (i = 0; i < rec->n; ++i)
2218 poly_update_den(rec->p[i], d);
2220 return isl_stat_ok;
2223 __isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
2225 isl_val *d;
2227 if (!qp)
2228 return NULL;
2229 d = isl_val_one(isl_qpolynomial_get_ctx(qp));
2230 if (!d)
2231 return NULL;
2232 if (poly_update_den(qp->poly, &d->n) < 0)
2233 return isl_val_free(d);
2234 return d;
2237 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2238 __isl_take isl_space *domain, int pos, int power)
2240 struct isl_ctx *ctx;
2242 if (!domain)
2243 return NULL;
2245 ctx = domain->ctx;
2247 return isl_qpolynomial_alloc(domain, 0,
2248 isl_poly_var_pow(ctx, pos, power));
2251 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2252 __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2254 if (isl_space_check_is_set(domain ) < 0)
2255 goto error;
2256 if (isl_space_check_range(domain, type, pos, 1) < 0)
2257 goto error;
2259 pos += isl_space_offset(domain, type);
2261 return isl_qpolynomial_var_pow_on_domain(domain, pos, 1);
2262 error:
2263 isl_space_free(domain);
2264 return NULL;
2267 __isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
2268 unsigned first, unsigned n, __isl_keep isl_poly **subs)
2270 int i;
2271 isl_bool is_cst;
2272 isl_poly_rec *rec;
2273 isl_poly *base, *res;
2275 is_cst = isl_poly_is_cst(poly);
2276 if (is_cst < 0)
2277 return isl_poly_free(poly);
2278 if (is_cst)
2279 return poly;
2281 if (poly->var < first)
2282 return poly;
2284 rec = isl_poly_as_rec(poly);
2285 if (!rec)
2286 goto error;
2288 isl_assert(poly->ctx, rec->n >= 1, goto error);
2290 if (poly->var >= first + n)
2291 base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2292 else
2293 base = isl_poly_copy(subs[poly->var - first]);
2295 res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2296 for (i = rec->n - 2; i >= 0; --i) {
2297 isl_poly *t;
2298 t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2299 res = isl_poly_mul(res, isl_poly_copy(base));
2300 res = isl_poly_sum(res, t);
2303 isl_poly_free(base);
2304 isl_poly_free(poly);
2306 return res;
2307 error:
2308 isl_poly_free(poly);
2309 return NULL;
2312 __isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
2313 isl_int denom, unsigned len)
2315 int i;
2316 isl_poly *poly;
2318 isl_assert(ctx, len >= 1, return NULL);
2320 poly = isl_poly_rat_cst(ctx, f[0], denom);
2321 for (i = 0; i < len - 1; ++i) {
2322 isl_poly *t;
2323 isl_poly *c;
2325 if (isl_int_is_zero(f[1 + i]))
2326 continue;
2328 c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2329 t = isl_poly_var_pow(ctx, i, 1);
2330 t = isl_poly_mul(c, t);
2331 poly = isl_poly_sum(poly, t);
2334 return poly;
2337 /* Remove common factor of non-constant terms and denominator.
2339 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2341 isl_ctx *ctx = qp->div->ctx;
2342 unsigned total = qp->div->n_col - 2;
2344 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2345 isl_int_gcd(ctx->normalize_gcd,
2346 ctx->normalize_gcd, qp->div->row[div][0]);
2347 if (isl_int_is_one(ctx->normalize_gcd))
2348 return;
2350 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2351 ctx->normalize_gcd, total);
2352 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2353 ctx->normalize_gcd);
2354 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2355 ctx->normalize_gcd);
2358 /* Replace the integer division identified by "div" by the polynomial "s".
2359 * The integer division is assumed not to appear in the definition
2360 * of any other integer divisions.
2362 static __isl_give isl_qpolynomial *substitute_div(
2363 __isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
2365 int i;
2366 int div_pos;
2367 int *reordering;
2368 isl_ctx *ctx;
2370 if (!qp || !s)
2371 goto error;
2373 qp = isl_qpolynomial_cow(qp);
2374 if (!qp)
2375 goto error;
2377 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2378 if (div_pos < 0)
2379 goto error;
2380 qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
2381 if (!qp->poly)
2382 goto error;
2384 ctx = isl_qpolynomial_get_ctx(qp);
2385 reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row);
2386 if (!reordering)
2387 goto error;
2388 for (i = 0; i < div_pos + div; ++i)
2389 reordering[i] = i;
2390 for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
2391 reordering[i] = i - 1;
2392 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2393 qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
2394 qp->poly = reorder(qp->poly, reordering);
2395 free(reordering);
2397 if (!qp->poly || !qp->div)
2398 goto error;
2400 isl_poly_free(s);
2401 return qp;
2402 error:
2403 isl_qpolynomial_free(qp);
2404 isl_poly_free(s);
2405 return NULL;
2408 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2409 * divisions because d is equal to 1 by their definition, i.e., e.
2411 static __isl_give isl_qpolynomial *substitute_non_divs(
2412 __isl_take isl_qpolynomial *qp)
2414 int i, j;
2415 int div_pos;
2416 isl_poly *s;
2418 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2419 if (div_pos < 0)
2420 return isl_qpolynomial_free(qp);
2422 for (i = 0; qp && i < qp->div->n_row; ++i) {
2423 if (!isl_int_is_one(qp->div->row[i][0]))
2424 continue;
2425 for (j = i + 1; j < qp->div->n_row; ++j) {
2426 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
2427 continue;
2428 isl_seq_combine(qp->div->row[j] + 1,
2429 qp->div->ctx->one, qp->div->row[j] + 1,
2430 qp->div->row[j][2 + div_pos + i],
2431 qp->div->row[i] + 1, 1 + div_pos + i);
2432 isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0);
2433 normalize_div(qp, j);
2435 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2436 qp->div->row[i][0], qp->div->n_col - 1);
2437 qp = substitute_div(qp, i, s);
2438 --i;
2441 return qp;
2444 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2445 * with d the denominator. When replacing the coefficient e of x by
2446 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2447 * inside the division, so we need to add floor(e/d) * x outside.
2448 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2449 * to adjust the coefficient of x in each later div that depends on the
2450 * current div "div" and also in the affine expressions in the rows of "mat"
2451 * (if they too depend on "div").
2453 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2454 __isl_keep isl_mat **mat)
2456 int i, j;
2457 isl_int v;
2458 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2460 isl_int_init(v);
2461 for (i = 0; i < 1 + total + div; ++i) {
2462 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2463 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2464 continue;
2465 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2466 isl_int_fdiv_r(qp->div->row[div][1 + i],
2467 qp->div->row[div][1 + i], qp->div->row[div][0]);
2468 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2469 for (j = div + 1; j < qp->div->n_row; ++j) {
2470 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2471 continue;
2472 isl_int_addmul(qp->div->row[j][1 + i],
2473 v, qp->div->row[j][2 + total + div]);
2476 isl_int_clear(v);
2479 /* Check if the last non-zero coefficient is bigger that half of the
2480 * denominator. If so, we will invert the div to further reduce the number
2481 * of distinct divs that may appear.
2482 * If the last non-zero coefficient is exactly half the denominator,
2483 * then we continue looking for earlier coefficients that are bigger
2484 * than half the denominator.
2486 static int needs_invert(__isl_keep isl_mat *div, int row)
2488 int i;
2489 int cmp;
2491 for (i = div->n_col - 1; i >= 1; --i) {
2492 if (isl_int_is_zero(div->row[row][i]))
2493 continue;
2494 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2495 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2496 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2497 if (cmp)
2498 return cmp > 0;
2499 if (i == 1)
2500 return 1;
2503 return 0;
2506 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2507 * We only invert the coefficients of e (and the coefficient of q in
2508 * later divs and in the rows of "mat"). After calling this function, the
2509 * coefficients of e should be reduced again.
2511 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2512 __isl_keep isl_mat **mat)
2514 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2516 isl_seq_neg(qp->div->row[div] + 1,
2517 qp->div->row[div] + 1, qp->div->n_col - 1);
2518 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2519 isl_int_add(qp->div->row[div][1],
2520 qp->div->row[div][1], qp->div->row[div][0]);
2521 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2522 isl_mat_col_mul(qp->div, 2 + total + div,
2523 qp->div->ctx->negone, 2 + total + div);
2526 /* Reduce all divs of "qp" to have coefficients
2527 * in the interval [0, d-1], with d the denominator and such that the
2528 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2529 * The modifications to the integer divisions need to be reflected
2530 * in the factors of the polynomial that refer to the original
2531 * integer divisions. To this end, the modifications are collected
2532 * as a set of affine expressions and then plugged into the polynomial.
2534 * After the reduction, some divs may have become redundant or identical,
2535 * so we call substitute_non_divs and sort_divs. If these functions
2536 * eliminate divs or merge two or more divs into one, the coefficients
2537 * of the enclosing divs may have to be reduced again, so we call
2538 * ourselves recursively if the number of divs decreases.
2540 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2542 int i;
2543 isl_ctx *ctx;
2544 isl_mat *mat;
2545 isl_poly **s;
2546 unsigned o_div;
2547 isl_size n_div, total, new_n_div;
2549 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2550 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2551 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2552 if (total < 0 || n_div < 0)
2553 return isl_qpolynomial_free(qp);
2554 ctx = isl_qpolynomial_get_ctx(qp);
2555 mat = isl_mat_zero(ctx, n_div, 1 + total);
2557 for (i = 0; i < n_div; ++i)
2558 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2560 for (i = 0; i < qp->div->n_row; ++i) {
2561 normalize_div(qp, i);
2562 reduce_div(qp, i, &mat);
2563 if (needs_invert(qp->div, i)) {
2564 invert_div(qp, i, &mat);
2565 reduce_div(qp, i, &mat);
2568 if (!mat)
2569 goto error;
2571 s = isl_alloc_array(ctx, struct isl_poly *, n_div);
2572 if (n_div && !s)
2573 goto error;
2574 for (i = 0; i < n_div; ++i)
2575 s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2576 1 + total);
2577 qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2578 for (i = 0; i < n_div; ++i)
2579 isl_poly_free(s[i]);
2580 free(s);
2581 if (!qp->poly)
2582 goto error;
2584 isl_mat_free(mat);
2586 qp = substitute_non_divs(qp);
2587 qp = sort_divs(qp);
2588 new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2589 if (new_n_div < 0)
2590 return isl_qpolynomial_free(qp);
2591 if (new_n_div < n_div)
2592 return reduce_divs(qp);
2594 return qp;
2595 error:
2596 isl_qpolynomial_free(qp);
2597 isl_mat_free(mat);
2598 return NULL;
2601 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2602 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2604 struct isl_qpolynomial *qp;
2605 isl_poly_cst *cst;
2607 qp = isl_qpolynomial_zero_on_domain(domain);
2608 if (!qp)
2609 return NULL;
2611 cst = isl_poly_as_cst(qp->poly);
2612 isl_int_set(cst->n, n);
2613 isl_int_set(cst->d, d);
2615 return qp;
2618 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2620 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2621 __isl_take isl_space *domain, __isl_take isl_val *val)
2623 isl_qpolynomial *qp;
2624 isl_poly_cst *cst;
2626 qp = isl_qpolynomial_zero_on_domain(domain);
2627 if (!qp || !val)
2628 goto error;
2630 cst = isl_poly_as_cst(qp->poly);
2631 isl_int_set(cst->n, val->n);
2632 isl_int_set(cst->d, val->d);
2634 isl_val_free(val);
2635 return qp;
2636 error:
2637 isl_val_free(val);
2638 isl_qpolynomial_free(qp);
2639 return NULL;
2642 static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
2644 isl_bool is_cst;
2645 isl_poly_rec *rec;
2646 int i;
2648 is_cst = isl_poly_is_cst(poly);
2649 if (is_cst < 0)
2650 return isl_stat_error;
2651 if (is_cst)
2652 return isl_stat_ok;
2654 if (poly->var < d)
2655 active[poly->var] = 1;
2657 rec = isl_poly_as_rec(poly);
2658 for (i = 0; i < rec->n; ++i)
2659 if (poly_set_active(rec->p[i], active, d) < 0)
2660 return isl_stat_error;
2662 return isl_stat_ok;
2665 static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
2667 int i, j;
2668 isl_size d;
2669 isl_space *space;
2671 space = isl_qpolynomial_peek_domain_space(qp);
2672 d = isl_space_dim(space, isl_dim_all);
2673 if (d < 0 || !active)
2674 return isl_stat_error;
2676 for (i = 0; i < d; ++i)
2677 for (j = 0; j < qp->div->n_row; ++j) {
2678 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2679 continue;
2680 active[i] = 1;
2681 break;
2684 return poly_set_active(qp->poly, active, d);
2687 #undef TYPE
2688 #define TYPE isl_qpolynomial
2689 static
2690 #include "check_type_range_templ.c"
2692 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2693 enum isl_dim_type type, unsigned first, unsigned n)
2695 int i;
2696 int *active = NULL;
2697 isl_bool involves = isl_bool_false;
2698 int offset;
2699 isl_size d;
2700 isl_space *space;
2702 if (!qp)
2703 return isl_bool_error;
2704 if (n == 0)
2705 return isl_bool_false;
2707 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2708 return isl_bool_error;
2709 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2710 type == isl_dim_in, return isl_bool_error);
2712 space = isl_qpolynomial_peek_domain_space(qp);
2713 d = isl_space_dim(space, isl_dim_all);
2714 if (d < 0)
2715 return isl_bool_error;
2716 active = isl_calloc_array(qp->dim->ctx, int, d);
2717 if (set_active(qp, active) < 0)
2718 goto error;
2720 offset = isl_qpolynomial_domain_var_offset(qp, domain_type(type));
2721 if (offset < 0)
2722 goto error;
2723 first += offset;
2724 for (i = 0; i < n; ++i)
2725 if (active[first + i]) {
2726 involves = isl_bool_true;
2727 break;
2730 free(active);
2732 return involves;
2733 error:
2734 free(active);
2735 return isl_bool_error;
2738 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2739 * of the divs that do appear in the quasi-polynomial.
2741 static __isl_give isl_qpolynomial *remove_redundant_divs(
2742 __isl_take isl_qpolynomial *qp)
2744 int i, j;
2745 int div_pos;
2746 int len;
2747 int skip;
2748 int *active = NULL;
2749 int *reordering = NULL;
2750 int redundant = 0;
2751 int n_div;
2752 isl_ctx *ctx;
2754 if (!qp)
2755 return NULL;
2756 if (qp->div->n_row == 0)
2757 return qp;
2759 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2760 if (div_pos < 0)
2761 return isl_qpolynomial_free(qp);
2762 len = qp->div->n_col - 2;
2763 ctx = isl_qpolynomial_get_ctx(qp);
2764 active = isl_calloc_array(ctx, int, len);
2765 if (!active)
2766 goto error;
2768 if (poly_set_active(qp->poly, active, len) < 0)
2769 goto error;
2771 for (i = qp->div->n_row - 1; i >= 0; --i) {
2772 if (!active[div_pos + i]) {
2773 redundant = 1;
2774 continue;
2776 for (j = 0; j < i; ++j) {
2777 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j]))
2778 continue;
2779 active[div_pos + j] = 1;
2780 break;
2784 if (!redundant) {
2785 free(active);
2786 return qp;
2789 reordering = isl_alloc_array(qp->div->ctx, int, len);
2790 if (!reordering)
2791 goto error;
2793 for (i = 0; i < div_pos; ++i)
2794 reordering[i] = i;
2796 skip = 0;
2797 n_div = qp->div->n_row;
2798 for (i = 0; i < n_div; ++i) {
2799 if (!active[div_pos + i]) {
2800 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2801 qp->div = isl_mat_drop_cols(qp->div,
2802 2 + div_pos + i - skip, 1);
2803 skip++;
2805 reordering[div_pos + i] = div_pos + i - skip;
2808 qp->poly = reorder(qp->poly, reordering);
2810 if (!qp->poly || !qp->div)
2811 goto error;
2813 free(active);
2814 free(reordering);
2816 return qp;
2817 error:
2818 free(active);
2819 free(reordering);
2820 isl_qpolynomial_free(qp);
2821 return NULL;
2824 __isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
2825 unsigned first, unsigned n)
2827 int i;
2828 isl_poly_rec *rec;
2830 if (!poly)
2831 return NULL;
2832 if (n == 0 || poly->var < 0 || poly->var < first)
2833 return poly;
2834 if (poly->var < first + n) {
2835 poly = replace_by_constant_term(poly);
2836 return isl_poly_drop(poly, first, n);
2838 poly = isl_poly_cow(poly);
2839 if (!poly)
2840 return NULL;
2841 poly->var -= n;
2842 rec = isl_poly_as_rec(poly);
2843 if (!rec)
2844 goto error;
2846 for (i = 0; i < rec->n; ++i) {
2847 rec->p[i] = isl_poly_drop(rec->p[i], first, n);
2848 if (!rec->p[i])
2849 goto error;
2852 return poly;
2853 error:
2854 isl_poly_free(poly);
2855 return NULL;
2858 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2859 __isl_take isl_qpolynomial *qp,
2860 enum isl_dim_type type, unsigned pos, const char *s)
2862 qp = isl_qpolynomial_cow(qp);
2863 if (!qp)
2864 return NULL;
2865 if (type == isl_dim_out)
2866 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2867 "cannot set name of output/set dimension",
2868 return isl_qpolynomial_free(qp));
2869 type = domain_type(type);
2870 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2871 if (!qp->dim)
2872 goto error;
2873 return qp;
2874 error:
2875 isl_qpolynomial_free(qp);
2876 return NULL;
2879 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2880 __isl_take isl_qpolynomial *qp,
2881 enum isl_dim_type type, unsigned first, unsigned n)
2883 int offset;
2885 if (!qp)
2886 return NULL;
2887 if (type == isl_dim_out)
2888 isl_die(qp->dim->ctx, isl_error_invalid,
2889 "cannot drop output/set dimension",
2890 goto error);
2891 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2892 return isl_qpolynomial_free(qp);
2893 type = domain_type(type);
2894 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2895 return qp;
2897 qp = isl_qpolynomial_cow(qp);
2898 if (!qp)
2899 return NULL;
2901 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2902 type == isl_dim_set, goto error);
2904 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2905 if (!qp->dim)
2906 goto error;
2908 offset = isl_qpolynomial_domain_var_offset(qp, type);
2909 if (offset < 0)
2910 goto error;
2911 first += offset;
2913 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2914 if (!qp->div)
2915 goto error;
2917 qp->poly = isl_poly_drop(qp->poly, first, n);
2918 if (!qp->poly)
2919 goto error;
2921 return qp;
2922 error:
2923 isl_qpolynomial_free(qp);
2924 return NULL;
2927 /* Project the domain of the quasi-polynomial onto its parameter space.
2928 * The quasi-polynomial may not involve any of the domain dimensions.
2930 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2931 __isl_take isl_qpolynomial *qp)
2933 isl_space *space;
2934 isl_size n;
2935 isl_bool involves;
2937 n = isl_qpolynomial_dim(qp, isl_dim_in);
2938 if (n < 0)
2939 return isl_qpolynomial_free(qp);
2940 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2941 if (involves < 0)
2942 return isl_qpolynomial_free(qp);
2943 if (involves)
2944 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2945 "polynomial involves some of the domain dimensions",
2946 return isl_qpolynomial_free(qp));
2947 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2948 space = isl_qpolynomial_get_domain_space(qp);
2949 space = isl_space_params(space);
2950 qp = isl_qpolynomial_reset_domain_space(qp, space);
2951 return qp;
2954 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2955 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2957 int i, j, k;
2958 isl_int denom;
2959 unsigned total;
2960 unsigned n_div;
2961 isl_poly *poly;
2963 if (!eq)
2964 goto error;
2965 if (eq->n_eq == 0) {
2966 isl_basic_set_free(eq);
2967 return qp;
2970 qp = isl_qpolynomial_cow(qp);
2971 if (!qp)
2972 goto error;
2973 qp->div = isl_mat_cow(qp->div);
2974 if (!qp->div)
2975 goto error;
2977 total = isl_basic_set_offset(eq, isl_dim_div);
2978 n_div = eq->n_div;
2979 isl_int_init(denom);
2980 for (i = 0; i < eq->n_eq; ++i) {
2981 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2982 if (j < 0 || j == 0 || j >= total)
2983 continue;
2985 for (k = 0; k < qp->div->n_row; ++k) {
2986 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2987 continue;
2988 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2989 &qp->div->row[k][0]);
2990 normalize_div(qp, k);
2993 if (isl_int_is_pos(eq->eq[i][j]))
2994 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2995 isl_int_abs(denom, eq->eq[i][j]);
2996 isl_int_set_si(eq->eq[i][j], 0);
2998 poly = isl_poly_from_affine(qp->dim->ctx,
2999 eq->eq[i], denom, total);
3000 qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
3001 isl_poly_free(poly);
3003 isl_int_clear(denom);
3005 if (!qp->poly)
3006 goto error;
3008 isl_basic_set_free(eq);
3010 qp = substitute_non_divs(qp);
3011 qp = sort_divs(qp);
3013 return qp;
3014 error:
3015 isl_basic_set_free(eq);
3016 isl_qpolynomial_free(qp);
3017 return NULL;
3020 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3022 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
3023 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
3025 if (!qp || !eq)
3026 goto error;
3027 if (qp->div->n_row > 0)
3028 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
3029 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
3030 error:
3031 isl_basic_set_free(eq);
3032 isl_qpolynomial_free(qp);
3033 return NULL;
3036 /* Look for equalities among the variables shared by context and qp
3037 * and the integer divisions of qp, if any.
3038 * The equalities are then used to eliminate variables and/or integer
3039 * divisions from qp.
3041 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
3042 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3044 isl_local_space *ls;
3045 isl_basic_set *aff;
3047 ls = isl_qpolynomial_get_domain_local_space(qp);
3048 context = isl_local_space_lift_set(ls, context);
3050 aff = isl_set_affine_hull(context);
3051 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
3054 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
3055 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3057 isl_space *space = isl_qpolynomial_get_domain_space(qp);
3058 isl_set *dom_context = isl_set_universe(space);
3059 dom_context = isl_set_intersect_params(dom_context, context);
3060 return isl_qpolynomial_gist(qp, dom_context);
3063 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
3064 __isl_take isl_qpolynomial *qp)
3066 isl_set *dom;
3068 if (!qp)
3069 return NULL;
3070 if (isl_qpolynomial_is_zero(qp)) {
3071 isl_space *dim = isl_qpolynomial_get_space(qp);
3072 isl_qpolynomial_free(qp);
3073 return isl_pw_qpolynomial_zero(dim);
3076 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
3077 return isl_pw_qpolynomial_alloc(dom, qp);
3080 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3082 #undef PW
3083 #define PW isl_pw_qpolynomial
3084 #undef EL
3085 #define EL isl_qpolynomial
3086 #undef EL_IS_ZERO
3087 #define EL_IS_ZERO is_zero
3088 #undef ZERO
3089 #define ZERO zero
3090 #undef IS_ZERO
3091 #define IS_ZERO is_zero
3092 #undef FIELD
3093 #define FIELD qp
3094 #undef DEFAULT_IS_ZERO
3095 #define DEFAULT_IS_ZERO 1
3097 #define NO_PULLBACK
3099 #include <isl_pw_templ.c>
3100 #include <isl_pw_eval.c>
3102 #undef BASE
3103 #define BASE pw_qpolynomial
3105 #include <isl_union_single.c>
3106 #include <isl_union_eval.c>
3107 #include <isl_union_neg.c>
3109 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
3111 if (!pwqp)
3112 return -1;
3114 if (pwqp->n != -1)
3115 return 0;
3117 if (!isl_set_plain_is_universe(pwqp->p[0].set))
3118 return 0;
3120 return isl_qpolynomial_is_one(pwqp->p[0].qp);
3123 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
3124 __isl_take isl_pw_qpolynomial *pwqp1,
3125 __isl_take isl_pw_qpolynomial *pwqp2)
3127 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3130 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
3131 __isl_take isl_pw_qpolynomial *pwqp1,
3132 __isl_take isl_pw_qpolynomial *pwqp2)
3134 int i, j, n;
3135 struct isl_pw_qpolynomial *res;
3137 if (!pwqp1 || !pwqp2)
3138 goto error;
3140 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3141 goto error);
3143 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3144 isl_pw_qpolynomial_free(pwqp2);
3145 return pwqp1;
3148 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3149 isl_pw_qpolynomial_free(pwqp1);
3150 return pwqp2;
3153 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3154 isl_pw_qpolynomial_free(pwqp1);
3155 return pwqp2;
3158 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3159 isl_pw_qpolynomial_free(pwqp2);
3160 return pwqp1;
3163 n = pwqp1->n * pwqp2->n;
3164 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3166 for (i = 0; i < pwqp1->n; ++i) {
3167 for (j = 0; j < pwqp2->n; ++j) {
3168 struct isl_set *common;
3169 struct isl_qpolynomial *prod;
3170 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3171 isl_set_copy(pwqp2->p[j].set));
3172 if (isl_set_plain_is_empty(common)) {
3173 isl_set_free(common);
3174 continue;
3177 prod = isl_qpolynomial_mul(
3178 isl_qpolynomial_copy(pwqp1->p[i].qp),
3179 isl_qpolynomial_copy(pwqp2->p[j].qp));
3181 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3185 isl_pw_qpolynomial_free(pwqp1);
3186 isl_pw_qpolynomial_free(pwqp2);
3188 return res;
3189 error:
3190 isl_pw_qpolynomial_free(pwqp1);
3191 isl_pw_qpolynomial_free(pwqp2);
3192 return NULL;
3195 __isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
3196 __isl_take isl_vec *vec)
3198 int i;
3199 isl_bool is_cst;
3200 isl_poly_rec *rec;
3201 isl_val *res;
3202 isl_val *base;
3204 is_cst = isl_poly_is_cst(poly);
3205 if (is_cst < 0)
3206 goto error;
3207 if (is_cst) {
3208 isl_vec_free(vec);
3209 res = isl_poly_get_constant_val(poly);
3210 isl_poly_free(poly);
3211 return res;
3214 rec = isl_poly_as_rec(poly);
3215 if (!rec || !vec)
3216 goto error;
3218 isl_assert(poly->ctx, rec->n >= 1, goto error);
3220 base = isl_val_rat_from_isl_int(poly->ctx,
3221 vec->el[1 + poly->var], vec->el[0]);
3223 res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3224 isl_vec_copy(vec));
3226 for (i = rec->n - 2; i >= 0; --i) {
3227 res = isl_val_mul(res, isl_val_copy(base));
3228 res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
3229 isl_vec_copy(vec)));
3232 isl_val_free(base);
3233 isl_poly_free(poly);
3234 isl_vec_free(vec);
3235 return res;
3236 error:
3237 isl_poly_free(poly);
3238 isl_vec_free(vec);
3239 return NULL;
3242 /* Evaluate "qp" in the void point "pnt".
3243 * In particular, return the value NaN.
3245 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3246 __isl_take isl_point *pnt)
3248 isl_ctx *ctx;
3250 ctx = isl_point_get_ctx(pnt);
3251 isl_qpolynomial_free(qp);
3252 isl_point_free(pnt);
3253 return isl_val_nan(ctx);
3256 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3257 __isl_take isl_point *pnt)
3259 isl_bool is_void;
3260 isl_vec *ext;
3261 isl_val *v;
3263 if (!qp || !pnt)
3264 goto error;
3265 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3266 is_void = isl_point_is_void(pnt);
3267 if (is_void < 0)
3268 goto error;
3269 if (is_void)
3270 return eval_void(qp, pnt);
3272 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3274 v = isl_poly_eval(isl_poly_copy(qp->poly), ext);
3276 isl_qpolynomial_free(qp);
3277 isl_point_free(pnt);
3279 return v;
3280 error:
3281 isl_qpolynomial_free(qp);
3282 isl_point_free(pnt);
3283 return NULL;
3286 int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
3288 int cmp;
3289 isl_int t;
3290 isl_int_init(t);
3291 isl_int_mul(t, cst1->n, cst2->d);
3292 isl_int_submul(t, cst2->n, cst1->d);
3293 cmp = isl_int_sgn(t);
3294 isl_int_clear(t);
3295 return cmp;
3298 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3299 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3300 unsigned first, unsigned n)
3302 unsigned total;
3303 unsigned g_pos;
3304 int *exp;
3306 if (!qp)
3307 return NULL;
3308 if (type == isl_dim_out)
3309 isl_die(qp->div->ctx, isl_error_invalid,
3310 "cannot insert output/set dimensions",
3311 goto error);
3312 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3313 return isl_qpolynomial_free(qp);
3314 type = domain_type(type);
3315 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3316 return qp;
3318 qp = isl_qpolynomial_cow(qp);
3319 if (!qp)
3320 return NULL;
3322 g_pos = pos(qp->dim, type) + first;
3324 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3325 if (!qp->div)
3326 goto error;
3328 total = qp->div->n_col - 2;
3329 if (total > g_pos) {
3330 int i;
3331 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3332 if (!exp)
3333 goto error;
3334 for (i = 0; i < total - g_pos; ++i)
3335 exp[i] = i + n;
3336 qp->poly = expand(qp->poly, exp, g_pos);
3337 free(exp);
3338 if (!qp->poly)
3339 goto error;
3342 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3343 if (!qp->dim)
3344 goto error;
3346 return qp;
3347 error:
3348 isl_qpolynomial_free(qp);
3349 return NULL;
3352 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3353 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3355 isl_size pos;
3357 pos = isl_qpolynomial_dim(qp, type);
3358 if (pos < 0)
3359 return isl_qpolynomial_free(qp);
3361 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3364 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3365 __isl_take isl_pw_qpolynomial *pwqp,
3366 enum isl_dim_type type, unsigned n)
3368 isl_size pos;
3370 pos = isl_pw_qpolynomial_dim(pwqp, type);
3371 if (pos < 0)
3372 return isl_pw_qpolynomial_free(pwqp);
3374 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3377 static int *reordering_move(isl_ctx *ctx,
3378 unsigned len, unsigned dst, unsigned src, unsigned n)
3380 int i;
3381 int *reordering;
3383 reordering = isl_alloc_array(ctx, int, len);
3384 if (!reordering)
3385 return NULL;
3387 if (dst <= src) {
3388 for (i = 0; i < dst; ++i)
3389 reordering[i] = i;
3390 for (i = 0; i < n; ++i)
3391 reordering[src + i] = dst + i;
3392 for (i = 0; i < src - dst; ++i)
3393 reordering[dst + i] = dst + n + i;
3394 for (i = 0; i < len - src - n; ++i)
3395 reordering[src + n + i] = src + n + i;
3396 } else {
3397 for (i = 0; i < src; ++i)
3398 reordering[i] = i;
3399 for (i = 0; i < n; ++i)
3400 reordering[src + i] = dst + i;
3401 for (i = 0; i < dst - src; ++i)
3402 reordering[src + n + i] = src + i;
3403 for (i = 0; i < len - dst - n; ++i)
3404 reordering[dst + n + i] = dst + n + i;
3407 return reordering;
3410 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3411 __isl_take isl_qpolynomial *qp,
3412 enum isl_dim_type dst_type, unsigned dst_pos,
3413 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3415 unsigned g_dst_pos;
3416 unsigned g_src_pos;
3417 int *reordering;
3419 if (!qp)
3420 return NULL;
3422 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3423 isl_die(qp->dim->ctx, isl_error_invalid,
3424 "cannot move output/set dimension",
3425 goto error);
3426 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3427 return isl_qpolynomial_free(qp);
3428 if (dst_type == isl_dim_in)
3429 dst_type = isl_dim_set;
3430 if (src_type == isl_dim_in)
3431 src_type = isl_dim_set;
3433 if (n == 0 &&
3434 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3435 !isl_space_is_named_or_nested(qp->dim, dst_type))
3436 return qp;
3438 qp = isl_qpolynomial_cow(qp);
3439 if (!qp)
3440 return NULL;
3442 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3443 g_src_pos = pos(qp->dim, src_type) + src_pos;
3444 if (dst_type > src_type)
3445 g_dst_pos -= n;
3447 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3448 if (!qp->div)
3449 goto error;
3450 qp = sort_divs(qp);
3451 if (!qp)
3452 goto error;
3454 reordering = reordering_move(qp->dim->ctx,
3455 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3456 if (!reordering)
3457 goto error;
3459 qp->poly = reorder(qp->poly, reordering);
3460 free(reordering);
3461 if (!qp->poly)
3462 goto error;
3464 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3465 if (!qp->dim)
3466 goto error;
3468 return qp;
3469 error:
3470 isl_qpolynomial_free(qp);
3471 return NULL;
3474 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3475 __isl_take isl_space *space, isl_int *f, isl_int denom)
3477 isl_size d;
3478 isl_poly *poly;
3480 space = isl_space_domain(space);
3481 if (!space)
3482 return NULL;
3484 d = isl_space_dim(space, isl_dim_all);
3485 poly = d < 0 ? NULL : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
3487 return isl_qpolynomial_alloc(space, 0, poly);
3490 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3492 isl_ctx *ctx;
3493 isl_poly *poly;
3494 isl_qpolynomial *qp;
3496 if (!aff)
3497 return NULL;
3499 ctx = isl_aff_get_ctx(aff);
3500 poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3501 aff->v->size - 1);
3503 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3504 aff->ls->div->n_row, poly);
3505 if (!qp)
3506 goto error;
3508 isl_mat_free(qp->div);
3509 qp->div = isl_mat_copy(aff->ls->div);
3510 qp->div = isl_mat_cow(qp->div);
3511 if (!qp->div)
3512 goto error;
3514 isl_aff_free(aff);
3515 qp = reduce_divs(qp);
3516 qp = remove_redundant_divs(qp);
3517 return qp;
3518 error:
3519 isl_aff_free(aff);
3520 return isl_qpolynomial_free(qp);
3523 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3524 __isl_take isl_pw_aff *pwaff)
3526 int i;
3527 isl_pw_qpolynomial *pwqp;
3529 if (!pwaff)
3530 return NULL;
3532 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3533 pwaff->n);
3535 for (i = 0; i < pwaff->n; ++i) {
3536 isl_set *dom;
3537 isl_qpolynomial *qp;
3539 dom = isl_set_copy(pwaff->p[i].set);
3540 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3541 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3544 isl_pw_aff_free(pwaff);
3545 return pwqp;
3548 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3549 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3551 isl_aff *aff;
3553 aff = isl_constraint_get_bound(c, type, pos);
3554 isl_constraint_free(c);
3555 return isl_qpolynomial_from_aff(aff);
3558 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3559 * in "qp" by subs[i].
3561 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3562 __isl_take isl_qpolynomial *qp,
3563 enum isl_dim_type type, unsigned first, unsigned n,
3564 __isl_keep isl_qpolynomial **subs)
3566 int i;
3567 isl_poly **polys;
3569 if (n == 0)
3570 return qp;
3572 qp = isl_qpolynomial_cow(qp);
3573 if (!qp)
3574 return NULL;
3576 if (type == isl_dim_out)
3577 isl_die(qp->dim->ctx, isl_error_invalid,
3578 "cannot substitute output/set dimension",
3579 goto error);
3580 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3581 return isl_qpolynomial_free(qp);
3582 type = domain_type(type);
3584 for (i = 0; i < n; ++i)
3585 if (!subs[i])
3586 goto error;
3588 for (i = 0; i < n; ++i)
3589 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3590 goto error);
3592 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3593 for (i = 0; i < n; ++i)
3594 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3596 first += pos(qp->dim, type);
3598 polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
3599 if (!polys)
3600 goto error;
3601 for (i = 0; i < n; ++i)
3602 polys[i] = subs[i]->poly;
3604 qp->poly = isl_poly_subs(qp->poly, first, n, polys);
3606 free(polys);
3608 if (!qp->poly)
3609 goto error;
3611 return qp;
3612 error:
3613 isl_qpolynomial_free(qp);
3614 return NULL;
3617 /* Extend "bset" with extra set dimensions for each integer division
3618 * in "qp" and then call "fn" with the extended bset and the polynomial
3619 * that results from replacing each of the integer divisions by the
3620 * corresponding extra set dimension.
3622 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3623 __isl_keep isl_basic_set *bset,
3624 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3625 __isl_take isl_qpolynomial *poly, void *user), void *user)
3627 isl_space *space;
3628 isl_local_space *ls;
3629 isl_qpolynomial *poly;
3631 if (!qp || !bset)
3632 return isl_stat_error;
3633 if (qp->div->n_row == 0)
3634 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3635 user);
3637 space = isl_space_copy(qp->dim);
3638 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3639 poly = isl_qpolynomial_alloc(space, 0, isl_poly_copy(qp->poly));
3640 bset = isl_basic_set_copy(bset);
3641 ls = isl_qpolynomial_get_domain_local_space(qp);
3642 bset = isl_local_space_lift_basic_set(ls, bset);
3644 return fn(bset, poly, user);
3647 /* Return total degree in variables first (inclusive) up to last (exclusive).
3649 int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3651 int deg = -1;
3652 int i;
3653 isl_bool is_zero, is_cst;
3654 isl_poly_rec *rec;
3656 is_zero = isl_poly_is_zero(poly);
3657 if (is_zero < 0)
3658 return -2;
3659 if (is_zero)
3660 return -1;
3661 is_cst = isl_poly_is_cst(poly);
3662 if (is_cst < 0)
3663 return -2;
3664 if (is_cst || poly->var < first)
3665 return 0;
3667 rec = isl_poly_as_rec(poly);
3668 if (!rec)
3669 return -2;
3671 for (i = 0; i < rec->n; ++i) {
3672 int d;
3674 is_zero = isl_poly_is_zero(rec->p[i]);
3675 if (is_zero < 0)
3676 return -2;
3677 if (is_zero)
3678 continue;
3679 d = isl_poly_degree(rec->p[i], first, last);
3680 if (poly->var < last)
3681 d += i;
3682 if (d > deg)
3683 deg = d;
3686 return deg;
3689 /* Return total degree in set variables.
3691 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3693 unsigned ovar;
3694 isl_size nvar;
3696 if (!poly)
3697 return -2;
3699 ovar = isl_space_offset(poly->dim, isl_dim_set);
3700 nvar = isl_space_dim(poly->dim, isl_dim_set);
3701 if (nvar < 0)
3702 return -2;
3703 return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3706 __isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
3707 unsigned pos, int deg)
3709 int i;
3710 isl_bool is_cst;
3711 isl_poly_rec *rec;
3713 is_cst = isl_poly_is_cst(poly);
3714 if (is_cst < 0)
3715 return NULL;
3716 if (is_cst || poly->var < pos) {
3717 if (deg == 0)
3718 return isl_poly_copy(poly);
3719 else
3720 return isl_poly_zero(poly->ctx);
3723 rec = isl_poly_as_rec(poly);
3724 if (!rec)
3725 return NULL;
3727 if (poly->var == pos) {
3728 if (deg < rec->n)
3729 return isl_poly_copy(rec->p[deg]);
3730 else
3731 return isl_poly_zero(poly->ctx);
3734 poly = isl_poly_copy(poly);
3735 poly = isl_poly_cow(poly);
3736 rec = isl_poly_as_rec(poly);
3737 if (!rec)
3738 goto error;
3740 for (i = 0; i < rec->n; ++i) {
3741 isl_poly *t;
3742 t = isl_poly_coeff(rec->p[i], pos, deg);
3743 if (!t)
3744 goto error;
3745 isl_poly_free(rec->p[i]);
3746 rec->p[i] = t;
3749 return poly;
3750 error:
3751 isl_poly_free(poly);
3752 return NULL;
3755 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3757 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3758 __isl_keep isl_qpolynomial *qp,
3759 enum isl_dim_type type, unsigned t_pos, int deg)
3761 unsigned g_pos;
3762 isl_poly *poly;
3763 isl_qpolynomial *c;
3765 if (!qp)
3766 return NULL;
3768 if (type == isl_dim_out)
3769 isl_die(qp->div->ctx, isl_error_invalid,
3770 "output/set dimension does not have a coefficient",
3771 return NULL);
3772 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
3773 return NULL;
3774 type = domain_type(type);
3776 g_pos = pos(qp->dim, type) + t_pos;
3777 poly = isl_poly_coeff(qp->poly, g_pos, deg);
3779 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
3780 qp->div->n_row, poly);
3781 if (!c)
3782 return NULL;
3783 isl_mat_free(c->div);
3784 c->div = isl_mat_copy(qp->div);
3785 if (!c->div)
3786 goto error;
3787 return c;
3788 error:
3789 isl_qpolynomial_free(c);
3790 return NULL;
3793 /* Homogenize the polynomial in the variables first (inclusive) up to
3794 * last (exclusive) by inserting powers of variable first.
3795 * Variable first is assumed not to appear in the input.
3797 __isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
3798 int target, int first, int last)
3800 int i;
3801 isl_bool is_zero, is_cst;
3802 isl_poly_rec *rec;
3804 is_zero = isl_poly_is_zero(poly);
3805 if (is_zero < 0)
3806 return isl_poly_free(poly);
3807 if (is_zero)
3808 return poly;
3809 if (deg == target)
3810 return poly;
3811 is_cst = isl_poly_is_cst(poly);
3812 if (is_cst < 0)
3813 return isl_poly_free(poly);
3814 if (is_cst || poly->var < first) {
3815 isl_poly *hom;
3817 hom = isl_poly_var_pow(poly->ctx, first, target - deg);
3818 if (!hom)
3819 goto error;
3820 rec = isl_poly_as_rec(hom);
3821 rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
3823 return hom;
3826 poly = isl_poly_cow(poly);
3827 rec = isl_poly_as_rec(poly);
3828 if (!rec)
3829 goto error;
3831 for (i = 0; i < rec->n; ++i) {
3832 is_zero = isl_poly_is_zero(rec->p[i]);
3833 if (is_zero < 0)
3834 return isl_poly_free(poly);
3835 if (is_zero)
3836 continue;
3837 rec->p[i] = isl_poly_homogenize(rec->p[i],
3838 poly->var < last ? deg + i : i, target,
3839 first, last);
3840 if (!rec->p[i])
3841 goto error;
3844 return poly;
3845 error:
3846 isl_poly_free(poly);
3847 return NULL;
3850 /* Homogenize the polynomial in the set variables by introducing
3851 * powers of an extra set variable at position 0.
3853 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3854 __isl_take isl_qpolynomial *poly)
3856 unsigned ovar;
3857 isl_size nvar;
3858 int deg = isl_qpolynomial_degree(poly);
3860 if (deg < -1)
3861 goto error;
3863 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3864 poly = isl_qpolynomial_cow(poly);
3865 if (!poly)
3866 goto error;
3868 ovar = isl_space_offset(poly->dim, isl_dim_set);
3869 nvar = isl_space_dim(poly->dim, isl_dim_set);
3870 if (nvar < 0)
3871 return isl_qpolynomial_free(poly);
3872 poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
3873 if (!poly->poly)
3874 goto error;
3876 return poly;
3877 error:
3878 isl_qpolynomial_free(poly);
3879 return NULL;
3882 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
3883 __isl_take isl_mat *div)
3885 isl_term *term;
3886 isl_size d;
3887 int n;
3889 d = isl_space_dim(space, isl_dim_all);
3890 if (d < 0 || !div)
3891 goto error;
3893 n = d + div->n_row;
3895 term = isl_calloc(space->ctx, struct isl_term,
3896 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3897 if (!term)
3898 goto error;
3900 term->ref = 1;
3901 term->dim = space;
3902 term->div = div;
3903 isl_int_init(term->n);
3904 isl_int_init(term->d);
3906 return term;
3907 error:
3908 isl_space_free(space);
3909 isl_mat_free(div);
3910 return NULL;
3913 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3915 if (!term)
3916 return NULL;
3918 term->ref++;
3919 return term;
3922 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3924 int i;
3925 isl_term *dup;
3926 isl_size total;
3928 total = isl_term_dim(term, isl_dim_all);
3929 if (total < 0)
3930 return NULL;
3932 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3933 if (!dup)
3934 return NULL;
3936 isl_int_set(dup->n, term->n);
3937 isl_int_set(dup->d, term->d);
3939 for (i = 0; i < total; ++i)
3940 dup->pow[i] = term->pow[i];
3942 return dup;
3945 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3947 if (!term)
3948 return NULL;
3950 if (term->ref == 1)
3951 return term;
3952 term->ref--;
3953 return isl_term_dup(term);
3956 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3958 if (!term)
3959 return NULL;
3961 if (--term->ref > 0)
3962 return NULL;
3964 isl_space_free(term->dim);
3965 isl_mat_free(term->div);
3966 isl_int_clear(term->n);
3967 isl_int_clear(term->d);
3968 free(term);
3970 return NULL;
3973 isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3975 isl_size dim;
3977 if (!term)
3978 return isl_size_error;
3980 switch (type) {
3981 case isl_dim_param:
3982 case isl_dim_in:
3983 case isl_dim_out: return isl_space_dim(term->dim, type);
3984 case isl_dim_div: return term->div->n_row;
3985 case isl_dim_all: dim = isl_space_dim(term->dim, isl_dim_all);
3986 if (dim < 0)
3987 return isl_size_error;
3988 return dim + term->div->n_row;
3989 default: return isl_size_error;
3993 /* Return the space of "term".
3995 static __isl_keep isl_space *isl_term_peek_space(__isl_keep isl_term *term)
3997 return term ? term->dim : NULL;
4000 /* Return the offset of the first variable of type "type" within
4001 * the variables of "term".
4003 static int isl_term_offset(__isl_keep isl_term *term, enum isl_dim_type type)
4005 isl_space *space;
4007 space = isl_term_peek_space(term);
4008 if (!space)
4009 return -1;
4011 switch (type) {
4012 case isl_dim_param:
4013 case isl_dim_set: return isl_space_offset(space, type);
4014 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
4015 default:
4016 isl_die(isl_term_get_ctx(term), isl_error_invalid,
4017 "invalid dimension type", return -1);
4021 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
4023 return term ? term->dim->ctx : NULL;
4026 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
4028 if (!term)
4029 return;
4030 isl_int_set(*n, term->n);
4033 /* Return the coefficient of the term "term".
4035 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
4037 if (!term)
4038 return NULL;
4040 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
4041 term->n, term->d);
4044 #undef TYPE
4045 #define TYPE isl_term
4046 static
4047 #include "check_type_range_templ.c"
4049 isl_size isl_term_get_exp(__isl_keep isl_term *term,
4050 enum isl_dim_type type, unsigned pos)
4052 int offset;
4054 if (isl_term_check_range(term, type, pos, 1) < 0)
4055 return isl_size_error;
4056 offset = isl_term_offset(term, type);
4057 if (offset < 0)
4058 return isl_size_error;
4060 return term->pow[offset + pos];
4063 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
4065 isl_local_space *ls;
4066 isl_aff *aff;
4068 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
4069 return NULL;
4071 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
4072 isl_mat_copy(term->div));
4073 aff = isl_aff_alloc(ls);
4074 if (!aff)
4075 return NULL;
4077 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
4079 aff = isl_aff_normalize(aff);
4081 return aff;
4084 __isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
4085 isl_stat (*fn)(__isl_take isl_term *term, void *user),
4086 __isl_take isl_term *term, void *user)
4088 int i;
4089 isl_bool is_zero, is_bad, is_cst;
4090 isl_poly_rec *rec;
4092 is_zero = isl_poly_is_zero(poly);
4093 if (is_zero < 0 || !term)
4094 goto error;
4096 if (is_zero)
4097 return term;
4099 is_cst = isl_poly_is_cst(poly);
4100 is_bad = isl_poly_is_nan(poly);
4101 if (is_bad >= 0 && !is_bad)
4102 is_bad = isl_poly_is_infty(poly);
4103 if (is_bad >= 0 && !is_bad)
4104 is_bad = isl_poly_is_neginfty(poly);
4105 if (is_cst < 0 || is_bad < 0)
4106 return isl_term_free(term);
4107 if (is_bad)
4108 isl_die(isl_term_get_ctx(term), isl_error_invalid,
4109 "cannot handle NaN/infty polynomial",
4110 return isl_term_free(term));
4112 if (is_cst) {
4113 isl_poly_cst *cst;
4114 cst = isl_poly_as_cst(poly);
4115 if (!cst)
4116 goto error;
4117 term = isl_term_cow(term);
4118 if (!term)
4119 goto error;
4120 isl_int_set(term->n, cst->n);
4121 isl_int_set(term->d, cst->d);
4122 if (fn(isl_term_copy(term), user) < 0)
4123 goto error;
4124 return term;
4127 rec = isl_poly_as_rec(poly);
4128 if (!rec)
4129 goto error;
4131 for (i = 0; i < rec->n; ++i) {
4132 term = isl_term_cow(term);
4133 if (!term)
4134 goto error;
4135 term->pow[poly->var] = i;
4136 term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4137 if (!term)
4138 goto error;
4140 term->pow[poly->var] = 0;
4142 return term;
4143 error:
4144 isl_term_free(term);
4145 return NULL;
4148 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
4149 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4151 isl_term *term;
4153 if (!qp)
4154 return isl_stat_error;
4156 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
4157 if (!term)
4158 return isl_stat_error;
4160 term = isl_poly_foreach_term(qp->poly, fn, term, user);
4162 isl_term_free(term);
4164 return term ? isl_stat_ok : isl_stat_error;
4167 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
4169 isl_poly *poly;
4170 isl_qpolynomial *qp;
4171 int i;
4172 isl_size n;
4174 n = isl_term_dim(term, isl_dim_all);
4175 if (n < 0)
4176 term = isl_term_free(term);
4177 if (!term)
4178 return NULL;
4180 poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4181 for (i = 0; i < n; ++i) {
4182 if (!term->pow[i])
4183 continue;
4184 poly = isl_poly_mul(poly,
4185 isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4188 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4189 term->div->n_row, poly);
4190 if (!qp)
4191 goto error;
4192 isl_mat_free(qp->div);
4193 qp->div = isl_mat_copy(term->div);
4194 if (!qp->div)
4195 goto error;
4197 isl_term_free(term);
4198 return qp;
4199 error:
4200 isl_qpolynomial_free(qp);
4201 isl_term_free(term);
4202 return NULL;
4205 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4206 __isl_take isl_space *space)
4208 int i;
4209 int extra;
4210 isl_size total, d_set, d_qp;
4212 if (!qp || !space)
4213 goto error;
4215 if (isl_space_is_equal(qp->dim, space)) {
4216 isl_space_free(space);
4217 return qp;
4220 qp = isl_qpolynomial_cow(qp);
4221 if (!qp)
4222 goto error;
4224 d_set = isl_space_dim(space, isl_dim_set);
4225 d_qp = isl_qpolynomial_domain_dim(qp, isl_dim_set);
4226 extra = d_set - d_qp;
4227 total = isl_space_dim(qp->dim, isl_dim_all);
4228 if (d_set < 0 || d_qp < 0 || total < 0)
4229 goto error;
4230 if (qp->div->n_row) {
4231 int *exp;
4233 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4234 if (!exp)
4235 goto error;
4236 for (i = 0; i < qp->div->n_row; ++i)
4237 exp[i] = extra + i;
4238 qp->poly = expand(qp->poly, exp, total);
4239 free(exp);
4240 if (!qp->poly)
4241 goto error;
4243 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4244 if (!qp->div)
4245 goto error;
4246 for (i = 0; i < qp->div->n_row; ++i)
4247 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4249 isl_space_free(qp->dim);
4250 qp->dim = space;
4252 return qp;
4253 error:
4254 isl_space_free(space);
4255 isl_qpolynomial_free(qp);
4256 return NULL;
4259 /* For each parameter or variable that does not appear in qp,
4260 * first eliminate the variable from all constraints and then set it to zero.
4262 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4263 __isl_keep isl_qpolynomial *qp)
4265 int *active = NULL;
4266 int i;
4267 isl_size d;
4268 isl_size nparam;
4269 isl_size nvar;
4271 d = isl_set_dim(set, isl_dim_all);
4272 if (d < 0 || !qp)
4273 goto error;
4275 active = isl_calloc_array(set->ctx, int, d);
4276 if (set_active(qp, active) < 0)
4277 goto error;
4279 for (i = 0; i < d; ++i)
4280 if (!active[i])
4281 break;
4283 if (i == d) {
4284 free(active);
4285 return set;
4288 nparam = isl_set_dim(set, isl_dim_param);
4289 nvar = isl_set_dim(set, isl_dim_set);
4290 if (nparam < 0 || nvar < 0)
4291 goto error;
4292 for (i = 0; i < nparam; ++i) {
4293 if (active[i])
4294 continue;
4295 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4296 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4298 for (i = 0; i < nvar; ++i) {
4299 if (active[nparam + i])
4300 continue;
4301 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4302 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4305 free(active);
4307 return set;
4308 error:
4309 free(active);
4310 isl_set_free(set);
4311 return NULL;
4314 struct isl_opt_data {
4315 isl_qpolynomial *qp;
4316 int first;
4317 isl_val *opt;
4318 int max;
4321 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4323 struct isl_opt_data *data = (struct isl_opt_data *)user;
4324 isl_val *val;
4326 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4327 if (data->first) {
4328 data->first = 0;
4329 data->opt = val;
4330 } else if (data->max) {
4331 data->opt = isl_val_max(data->opt, val);
4332 } else {
4333 data->opt = isl_val_min(data->opt, val);
4336 return isl_stat_ok;
4339 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4340 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4342 struct isl_opt_data data = { NULL, 1, NULL, max };
4343 isl_bool is_cst;
4345 if (!set || !qp)
4346 goto error;
4348 is_cst = isl_poly_is_cst(qp->poly);
4349 if (is_cst < 0)
4350 goto error;
4351 if (is_cst) {
4352 isl_set_free(set);
4353 data.opt = isl_qpolynomial_get_constant_val(qp);
4354 isl_qpolynomial_free(qp);
4355 return data.opt;
4358 set = fix_inactive(set, qp);
4360 data.qp = qp;
4361 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4362 goto error;
4364 if (data.first)
4365 data.opt = isl_val_zero(isl_set_get_ctx(set));
4367 isl_set_free(set);
4368 isl_qpolynomial_free(qp);
4369 return data.opt;
4370 error:
4371 isl_set_free(set);
4372 isl_qpolynomial_free(qp);
4373 isl_val_free(data.opt);
4374 return NULL;
4377 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4378 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4380 int i;
4381 int n_sub;
4382 isl_ctx *ctx;
4383 isl_poly **subs;
4384 isl_mat *mat, *diag;
4386 qp = isl_qpolynomial_cow(qp);
4387 if (!qp || !morph)
4388 goto error;
4390 ctx = qp->dim->ctx;
4391 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4393 n_sub = morph->inv->n_row - 1;
4394 if (morph->inv->n_row != morph->inv->n_col)
4395 n_sub += qp->div->n_row;
4396 subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
4397 if (n_sub && !subs)
4398 goto error;
4400 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4401 subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4402 morph->inv->row[0][0], morph->inv->n_col);
4403 if (morph->inv->n_row != morph->inv->n_col)
4404 for (i = 0; i < qp->div->n_row; ++i)
4405 subs[morph->inv->n_row - 1 + i] =
4406 isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4408 qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4410 for (i = 0; i < n_sub; ++i)
4411 isl_poly_free(subs[i]);
4412 free(subs);
4414 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4415 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4416 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4417 mat = isl_mat_diagonal(mat, diag);
4418 qp->div = isl_mat_product(qp->div, mat);
4419 isl_space_free(qp->dim);
4420 qp->dim = isl_space_copy(morph->ran->dim);
4422 if (!qp->poly || !qp->div || !qp->dim)
4423 goto error;
4425 isl_morph_free(morph);
4427 return qp;
4428 error:
4429 isl_qpolynomial_free(qp);
4430 isl_morph_free(morph);
4431 return NULL;
4434 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4435 __isl_take isl_union_pw_qpolynomial *upwqp1,
4436 __isl_take isl_union_pw_qpolynomial *upwqp2)
4438 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4439 &isl_pw_qpolynomial_mul);
4442 /* Reorder the dimension of "qp" according to the given reordering.
4444 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4445 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4447 isl_space *space;
4449 qp = isl_qpolynomial_cow(qp);
4450 if (!qp)
4451 goto error;
4453 r = isl_reordering_extend(r, qp->div->n_row);
4454 if (!r)
4455 goto error;
4457 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4458 if (!qp->div)
4459 goto error;
4461 qp->poly = reorder(qp->poly, r->pos);
4462 if (!qp->poly)
4463 goto error;
4465 space = isl_reordering_get_space(r);
4466 qp = isl_qpolynomial_reset_domain_space(qp, space);
4468 isl_reordering_free(r);
4469 return qp;
4470 error:
4471 isl_qpolynomial_free(qp);
4472 isl_reordering_free(r);
4473 return NULL;
4476 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4477 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4479 isl_bool equal_params;
4481 if (!qp || !model)
4482 goto error;
4484 equal_params = isl_space_has_equal_params(qp->dim, model);
4485 if (equal_params < 0)
4486 goto error;
4487 if (!equal_params) {
4488 isl_reordering *exp;
4490 exp = isl_parameter_alignment_reordering(qp->dim, model);
4491 exp = isl_reordering_extend_space(exp,
4492 isl_qpolynomial_get_domain_space(qp));
4493 qp = isl_qpolynomial_realign_domain(qp, exp);
4496 isl_space_free(model);
4497 return qp;
4498 error:
4499 isl_space_free(model);
4500 isl_qpolynomial_free(qp);
4501 return NULL;
4504 struct isl_split_periods_data {
4505 int max_periods;
4506 isl_pw_qpolynomial *res;
4509 /* Create a slice where the integer division "div" has the fixed value "v".
4510 * In particular, if "div" refers to floor(f/m), then create a slice
4512 * m v <= f <= m v + (m - 1)
4514 * or
4516 * f - m v >= 0
4517 * -f + m v + (m - 1) >= 0
4519 static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
4520 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4522 isl_size total;
4523 isl_basic_set *bset = NULL;
4524 int k;
4526 total = isl_space_dim(space, isl_dim_all);
4527 if (total < 0 || !qp)
4528 goto error;
4530 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4532 k = isl_basic_set_alloc_inequality(bset);
4533 if (k < 0)
4534 goto error;
4535 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4536 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4538 k = isl_basic_set_alloc_inequality(bset);
4539 if (k < 0)
4540 goto error;
4541 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4542 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4543 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4544 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4546 isl_space_free(space);
4547 return isl_set_from_basic_set(bset);
4548 error:
4549 isl_basic_set_free(bset);
4550 isl_space_free(space);
4551 return NULL;
4554 static isl_stat split_periods(__isl_take isl_set *set,
4555 __isl_take isl_qpolynomial *qp, void *user);
4557 /* Create a slice of the domain "set" such that integer division "div"
4558 * has the fixed value "v" and add the results to data->res,
4559 * replacing the integer division by "v" in "qp".
4561 static isl_stat set_div(__isl_take isl_set *set,
4562 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4563 struct isl_split_periods_data *data)
4565 int i;
4566 int div_pos;
4567 isl_set *slice;
4568 isl_poly *cst;
4570 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4571 set = isl_set_intersect(set, slice);
4573 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4574 if (div_pos < 0)
4575 goto error;
4577 for (i = div + 1; i < qp->div->n_row; ++i) {
4578 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div]))
4579 continue;
4580 isl_int_addmul(qp->div->row[i][1],
4581 qp->div->row[i][2 + div_pos + div], v);
4582 isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0);
4585 cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4586 qp = substitute_div(qp, div, cst);
4588 return split_periods(set, qp, data);
4589 error:
4590 isl_set_free(set);
4591 isl_qpolynomial_free(qp);
4592 return isl_stat_error;
4595 /* Split the domain "set" such that integer division "div"
4596 * has a fixed value (ranging from "min" to "max") on each slice
4597 * and add the results to data->res.
4599 static isl_stat split_div(__isl_take isl_set *set,
4600 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4601 struct isl_split_periods_data *data)
4603 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4604 isl_set *set_i = isl_set_copy(set);
4605 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4607 if (set_div(set_i, qp_i, div, min, data) < 0)
4608 goto error;
4610 isl_set_free(set);
4611 isl_qpolynomial_free(qp);
4612 return isl_stat_ok;
4613 error:
4614 isl_set_free(set);
4615 isl_qpolynomial_free(qp);
4616 return isl_stat_error;
4619 /* If "qp" refers to any integer division
4620 * that can only attain "max_periods" distinct values on "set"
4621 * then split the domain along those distinct values.
4622 * Add the results (or the original if no splitting occurs)
4623 * to data->res.
4625 static isl_stat split_periods(__isl_take isl_set *set,
4626 __isl_take isl_qpolynomial *qp, void *user)
4628 int i;
4629 isl_pw_qpolynomial *pwqp;
4630 struct isl_split_periods_data *data;
4631 isl_int min, max;
4632 int div_pos;
4633 isl_stat r = isl_stat_ok;
4635 data = (struct isl_split_periods_data *)user;
4637 if (!set || !qp)
4638 goto error;
4640 if (qp->div->n_row == 0) {
4641 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4642 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4643 return isl_stat_ok;
4646 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4647 if (div_pos < 0)
4648 goto error;
4650 isl_int_init(min);
4651 isl_int_init(max);
4652 for (i = 0; i < qp->div->n_row; ++i) {
4653 enum isl_lp_result lp_res;
4655 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + div_pos,
4656 qp->div->n_row) != -1)
4657 continue;
4659 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4660 set->ctx->one, &min, NULL, NULL);
4661 if (lp_res == isl_lp_error)
4662 goto error2;
4663 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4664 continue;
4665 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4667 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4668 set->ctx->one, &max, NULL, NULL);
4669 if (lp_res == isl_lp_error)
4670 goto error2;
4671 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4672 continue;
4673 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4675 isl_int_sub(max, max, min);
4676 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4677 isl_int_add(max, max, min);
4678 break;
4682 if (i < qp->div->n_row) {
4683 r = split_div(set, qp, i, min, max, data);
4684 } else {
4685 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4686 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4689 isl_int_clear(max);
4690 isl_int_clear(min);
4692 return r;
4693 error2:
4694 isl_int_clear(max);
4695 isl_int_clear(min);
4696 error:
4697 isl_set_free(set);
4698 isl_qpolynomial_free(qp);
4699 return isl_stat_error;
4702 /* If any quasi-polynomial in pwqp refers to any integer division
4703 * that can only attain "max_periods" distinct values on its domain
4704 * then split the domain along those distinct values.
4706 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4707 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4709 struct isl_split_periods_data data;
4711 data.max_periods = max_periods;
4712 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4714 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4715 goto error;
4717 isl_pw_qpolynomial_free(pwqp);
4719 return data.res;
4720 error:
4721 isl_pw_qpolynomial_free(data.res);
4722 isl_pw_qpolynomial_free(pwqp);
4723 return NULL;
4726 /* Construct a piecewise quasipolynomial that is constant on the given
4727 * domain. In particular, it is
4728 * 0 if cst == 0
4729 * 1 if cst == 1
4730 * infinity if cst == -1
4732 * If cst == -1, then explicitly check whether the domain is empty and,
4733 * if so, return 0 instead.
4735 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4736 __isl_take isl_basic_set *bset, int cst)
4738 isl_space *dim;
4739 isl_qpolynomial *qp;
4741 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4742 cst = 0;
4743 if (!bset)
4744 return NULL;
4746 bset = isl_basic_set_params(bset);
4747 dim = isl_basic_set_get_space(bset);
4748 if (cst < 0)
4749 qp = isl_qpolynomial_infty_on_domain(dim);
4750 else if (cst == 0)
4751 qp = isl_qpolynomial_zero_on_domain(dim);
4752 else
4753 qp = isl_qpolynomial_one_on_domain(dim);
4754 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4757 /* Factor bset, call fn on each of the factors and return the product.
4759 * If no factors can be found, simply call fn on the input.
4760 * Otherwise, construct the factors based on the factorizer,
4761 * call fn on each factor and compute the product.
4763 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4764 __isl_take isl_basic_set *bset,
4765 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4767 int i, n;
4768 isl_space *space;
4769 isl_set *set;
4770 isl_factorizer *f;
4771 isl_qpolynomial *qp;
4772 isl_pw_qpolynomial *pwqp;
4773 isl_size nparam;
4774 isl_size nvar;
4776 f = isl_basic_set_factorizer(bset);
4777 if (!f)
4778 goto error;
4779 if (f->n_group == 0) {
4780 isl_factorizer_free(f);
4781 return fn(bset);
4784 nparam = isl_basic_set_dim(bset, isl_dim_param);
4785 nvar = isl_basic_set_dim(bset, isl_dim_set);
4786 if (nparam < 0 || nvar < 0)
4787 bset = isl_basic_set_free(bset);
4789 space = isl_basic_set_get_space(bset);
4790 space = isl_space_params(space);
4791 set = isl_set_universe(isl_space_copy(space));
4792 qp = isl_qpolynomial_one_on_domain(space);
4793 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4795 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4797 for (i = 0, n = 0; i < f->n_group; ++i) {
4798 isl_basic_set *bset_i;
4799 isl_pw_qpolynomial *pwqp_i;
4801 bset_i = isl_basic_set_copy(bset);
4802 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4803 nparam + n + f->len[i], nvar - n - f->len[i]);
4804 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4805 nparam, n);
4806 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4807 n + f->len[i], nvar - n - f->len[i]);
4808 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4810 pwqp_i = fn(bset_i);
4811 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4813 n += f->len[i];
4816 isl_basic_set_free(bset);
4817 isl_factorizer_free(f);
4819 return pwqp;
4820 error:
4821 isl_basic_set_free(bset);
4822 return NULL;
4825 /* Factor bset, call fn on each of the factors and return the product.
4826 * The function is assumed to evaluate to zero on empty domains,
4827 * to one on zero-dimensional domains and to infinity on unbounded domains
4828 * and will not be called explicitly on zero-dimensional or unbounded domains.
4830 * We first check for some special cases and remove all equalities.
4831 * Then we hand over control to compressed_multiplicative_call.
4833 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4834 __isl_take isl_basic_set *bset,
4835 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4837 isl_bool bounded;
4838 isl_size dim;
4839 isl_morph *morph;
4840 isl_pw_qpolynomial *pwqp;
4842 if (!bset)
4843 return NULL;
4845 if (isl_basic_set_plain_is_empty(bset))
4846 return constant_on_domain(bset, 0);
4848 dim = isl_basic_set_dim(bset, isl_dim_set);
4849 if (dim < 0)
4850 goto error;
4851 if (dim == 0)
4852 return constant_on_domain(bset, 1);
4854 bounded = isl_basic_set_is_bounded(bset);
4855 if (bounded < 0)
4856 goto error;
4857 if (!bounded)
4858 return constant_on_domain(bset, -1);
4860 if (bset->n_eq == 0)
4861 return compressed_multiplicative_call(bset, fn);
4863 morph = isl_basic_set_full_compression(bset);
4864 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4866 pwqp = compressed_multiplicative_call(bset, fn);
4868 morph = isl_morph_dom_params(morph);
4869 morph = isl_morph_ran_params(morph);
4870 morph = isl_morph_inverse(morph);
4872 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4874 return pwqp;
4875 error:
4876 isl_basic_set_free(bset);
4877 return NULL;
4880 /* Drop all floors in "qp", turning each integer division [a/m] into
4881 * a rational division a/m. If "down" is set, then the integer division
4882 * is replaced by (a-(m-1))/m instead.
4884 static __isl_give isl_qpolynomial *qp_drop_floors(
4885 __isl_take isl_qpolynomial *qp, int down)
4887 int i;
4888 isl_poly *s;
4890 if (!qp)
4891 return NULL;
4892 if (qp->div->n_row == 0)
4893 return qp;
4895 qp = isl_qpolynomial_cow(qp);
4896 if (!qp)
4897 return NULL;
4899 for (i = qp->div->n_row - 1; i >= 0; --i) {
4900 if (down) {
4901 isl_int_sub(qp->div->row[i][1],
4902 qp->div->row[i][1], qp->div->row[i][0]);
4903 isl_int_add_ui(qp->div->row[i][1],
4904 qp->div->row[i][1], 1);
4906 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4907 qp->div->row[i][0], qp->div->n_col - 1);
4908 qp = substitute_div(qp, i, s);
4909 if (!qp)
4910 return NULL;
4913 return qp;
4916 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4917 * a rational division a/m.
4919 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4920 __isl_take isl_pw_qpolynomial *pwqp)
4922 int i;
4924 if (!pwqp)
4925 return NULL;
4927 if (isl_pw_qpolynomial_is_zero(pwqp))
4928 return pwqp;
4930 pwqp = isl_pw_qpolynomial_cow(pwqp);
4931 if (!pwqp)
4932 return NULL;
4934 for (i = 0; i < pwqp->n; ++i) {
4935 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4936 if (!pwqp->p[i].qp)
4937 goto error;
4940 return pwqp;
4941 error:
4942 isl_pw_qpolynomial_free(pwqp);
4943 return NULL;
4946 /* Adjust all the integer divisions in "qp" such that they are at least
4947 * one over the given orthant (identified by "signs"). This ensures
4948 * that they will still be non-negative even after subtracting (m-1)/m.
4950 * In particular, f is replaced by f' + v, changing f = [a/m]
4951 * to f' = [(a - m v)/m].
4952 * If the constant term k in a is smaller than m,
4953 * the constant term of v is set to floor(k/m) - 1.
4954 * For any other term, if the coefficient c and the variable x have
4955 * the same sign, then no changes are needed.
4956 * Otherwise, if the variable is positive (and c is negative),
4957 * then the coefficient of x in v is set to floor(c/m).
4958 * If the variable is negative (and c is positive),
4959 * then the coefficient of x in v is set to ceil(c/m).
4961 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4962 int *signs)
4964 int i, j;
4965 int div_pos;
4966 isl_vec *v = NULL;
4967 isl_poly *s;
4969 qp = isl_qpolynomial_cow(qp);
4970 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4971 if (div_pos < 0)
4972 return isl_qpolynomial_free(qp);
4973 qp->div = isl_mat_cow(qp->div);
4974 if (!qp->div)
4975 goto error;
4977 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4979 for (i = 0; i < qp->div->n_row; ++i) {
4980 isl_int *row = qp->div->row[i];
4981 v = isl_vec_clr(v);
4982 if (!v)
4983 goto error;
4984 if (isl_int_lt(row[1], row[0])) {
4985 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4986 isl_int_sub_ui(v->el[0], v->el[0], 1);
4987 isl_int_submul(row[1], row[0], v->el[0]);
4989 for (j = 0; j < div_pos; ++j) {
4990 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4991 continue;
4992 if (signs[j] < 0)
4993 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4994 else
4995 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4996 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4998 for (j = 0; j < i; ++j) {
4999 if (isl_int_sgn(row[2 + div_pos + j]) >= 0)
5000 continue;
5001 isl_int_fdiv_q(v->el[1 + div_pos + j],
5002 row[2 + div_pos + j], row[0]);
5003 isl_int_submul(row[2 + div_pos + j],
5004 row[0], v->el[1 + div_pos + j]);
5006 for (j = i + 1; j < qp->div->n_row; ++j) {
5007 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
5008 continue;
5009 isl_seq_combine(qp->div->row[j] + 1,
5010 qp->div->ctx->one, qp->div->row[j] + 1,
5011 qp->div->row[j][2 + div_pos + i], v->el,
5012 v->size);
5014 isl_int_set_si(v->el[1 + div_pos + i], 1);
5015 s = isl_poly_from_affine(qp->dim->ctx, v->el,
5016 qp->div->ctx->one, v->size);
5017 qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
5018 isl_poly_free(s);
5019 if (!qp->poly)
5020 goto error;
5023 isl_vec_free(v);
5024 return qp;
5025 error:
5026 isl_vec_free(v);
5027 isl_qpolynomial_free(qp);
5028 return NULL;
5031 struct isl_to_poly_data {
5032 int sign;
5033 isl_pw_qpolynomial *res;
5034 isl_qpolynomial *qp;
5037 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5038 * We first make all integer divisions positive and then split the
5039 * quasipolynomials into terms with sign data->sign (the direction
5040 * of the requested approximation) and terms with the opposite sign.
5041 * In the first set of terms, each integer division [a/m] is
5042 * overapproximated by a/m, while in the second it is underapproximated
5043 * by (a-(m-1))/m.
5045 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
5046 int *signs, void *user)
5048 struct isl_to_poly_data *data = user;
5049 isl_pw_qpolynomial *t;
5050 isl_qpolynomial *qp, *up, *down;
5052 qp = isl_qpolynomial_copy(data->qp);
5053 qp = make_divs_pos(qp, signs);
5055 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
5056 up = qp_drop_floors(up, 0);
5057 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
5058 down = qp_drop_floors(down, 1);
5060 isl_qpolynomial_free(qp);
5061 qp = isl_qpolynomial_add(up, down);
5063 t = isl_pw_qpolynomial_alloc(orthant, qp);
5064 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
5066 return isl_stat_ok;
5069 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5070 * the polynomial will be an overapproximation. If "sign" is negative,
5071 * it will be an underapproximation. If "sign" is zero, the approximation
5072 * will lie somewhere in between.
5074 * In particular, is sign == 0, we simply drop the floors, turning
5075 * the integer divisions into rational divisions.
5076 * Otherwise, we split the domains into orthants, make all integer divisions
5077 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5078 * depending on the requested sign and the sign of the term in which
5079 * the integer division appears.
5081 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
5082 __isl_take isl_pw_qpolynomial *pwqp, int sign)
5084 int i;
5085 struct isl_to_poly_data data;
5087 if (sign == 0)
5088 return pwqp_drop_floors(pwqp);
5090 if (!pwqp)
5091 return NULL;
5093 data.sign = sign;
5094 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
5096 for (i = 0; i < pwqp->n; ++i) {
5097 if (pwqp->p[i].qp->div->n_row == 0) {
5098 isl_pw_qpolynomial *t;
5099 t = isl_pw_qpolynomial_alloc(
5100 isl_set_copy(pwqp->p[i].set),
5101 isl_qpolynomial_copy(pwqp->p[i].qp));
5102 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
5103 continue;
5105 data.qp = pwqp->p[i].qp;
5106 if (isl_set_foreach_orthant(pwqp->p[i].set,
5107 &to_polynomial_on_orthant, &data) < 0)
5108 goto error;
5111 isl_pw_qpolynomial_free(pwqp);
5113 return data.res;
5114 error:
5115 isl_pw_qpolynomial_free(pwqp);
5116 isl_pw_qpolynomial_free(data.res);
5117 return NULL;
5120 static __isl_give isl_pw_qpolynomial *poly_entry(
5121 __isl_take isl_pw_qpolynomial *pwqp, void *user)
5123 int *sign = user;
5125 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
5128 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
5129 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
5131 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5132 &poly_entry, &sign);
5135 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
5136 __isl_take isl_qpolynomial *qp)
5138 int i, k;
5139 isl_space *dim;
5140 isl_vec *aff = NULL;
5141 isl_basic_map *bmap = NULL;
5142 isl_bool is_affine;
5143 unsigned pos;
5144 unsigned n_div;
5146 if (!qp)
5147 return NULL;
5148 is_affine = isl_poly_is_affine(qp->poly);
5149 if (is_affine < 0)
5150 goto error;
5151 if (!is_affine)
5152 isl_die(qp->dim->ctx, isl_error_invalid,
5153 "input quasi-polynomial not affine", goto error);
5154 aff = isl_qpolynomial_extract_affine(qp);
5155 if (!aff)
5156 goto error;
5157 dim = isl_qpolynomial_get_space(qp);
5158 pos = 1 + isl_space_offset(dim, isl_dim_out);
5159 n_div = qp->div->n_row;
5160 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
5162 for (i = 0; i < n_div; ++i) {
5163 k = isl_basic_map_alloc_div(bmap);
5164 if (k < 0)
5165 goto error;
5166 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
5167 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
5168 bmap = isl_basic_map_add_div_constraints(bmap, k);
5170 k = isl_basic_map_alloc_equality(bmap);
5171 if (k < 0)
5172 goto error;
5173 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
5174 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
5175 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
5177 isl_vec_free(aff);
5178 isl_qpolynomial_free(qp);
5179 bmap = isl_basic_map_finalize(bmap);
5180 return bmap;
5181 error:
5182 isl_vec_free(aff);
5183 isl_qpolynomial_free(qp);
5184 isl_basic_map_free(bmap);
5185 return NULL;