isl_qpolynomial_move_dims: use isl_qpolynomial_domain_dim
[isl.git] / isl_polynomial.c
blobd1a6130e20fc16d6e8959487d3942de6bac40379
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 EL_BASE
32 #define EL_BASE qpolynomial
34 #include <isl_list_templ.c>
36 #undef EL_BASE
37 #define EL_BASE pw_qpolynomial
39 #include <isl_list_templ.c>
41 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
43 switch (type) {
44 case isl_dim_param: return 0;
45 case isl_dim_in: return space->nparam;
46 case isl_dim_out: return space->nparam + space->n_in;
47 default: return 0;
51 isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
53 if (!poly)
54 return isl_bool_error;
56 return isl_bool_ok(poly->var < 0);
59 __isl_keep isl_poly_cst *isl_poly_as_cst(__isl_keep isl_poly *poly)
61 if (!poly)
62 return NULL;
64 isl_assert(poly->ctx, poly->var < 0, return NULL);
66 return (isl_poly_cst *) poly;
69 __isl_keep isl_poly_rec *isl_poly_as_rec(__isl_keep isl_poly *poly)
71 if (!poly)
72 return NULL;
74 isl_assert(poly->ctx, poly->var >= 0, return NULL);
76 return (isl_poly_rec *) poly;
79 /* Compare two polynomials.
81 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
82 * than "poly2" and 0 if they are equal.
84 static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1,
85 __isl_keep isl_poly *poly2)
87 int i;
88 isl_bool is_cst1;
89 isl_poly_rec *rec1, *rec2;
91 if (poly1 == poly2)
92 return 0;
93 is_cst1 = isl_poly_is_cst(poly1);
94 if (is_cst1 < 0)
95 return -1;
96 if (!poly2)
97 return 1;
98 if (poly1->var != poly2->var)
99 return poly1->var - poly2->var;
101 if (is_cst1) {
102 isl_poly_cst *cst1, *cst2;
103 int cmp;
105 cst1 = isl_poly_as_cst(poly1);
106 cst2 = isl_poly_as_cst(poly2);
107 if (!cst1 || !cst2)
108 return 0;
109 cmp = isl_int_cmp(cst1->n, cst2->n);
110 if (cmp != 0)
111 return cmp;
112 return isl_int_cmp(cst1->d, cst2->d);
115 rec1 = isl_poly_as_rec(poly1);
116 rec2 = isl_poly_as_rec(poly2);
117 if (!rec1 || !rec2)
118 return 0;
120 if (rec1->n != rec2->n)
121 return rec1->n - rec2->n;
123 for (i = 0; i < rec1->n; ++i) {
124 int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
125 if (cmp != 0)
126 return cmp;
129 return 0;
132 isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1,
133 __isl_keep isl_poly *poly2)
135 int i;
136 isl_bool is_cst1;
137 isl_poly_rec *rec1, *rec2;
139 is_cst1 = isl_poly_is_cst(poly1);
140 if (is_cst1 < 0 || !poly2)
141 return isl_bool_error;
142 if (poly1 == poly2)
143 return isl_bool_true;
144 if (poly1->var != poly2->var)
145 return isl_bool_false;
146 if (is_cst1) {
147 isl_poly_cst *cst1, *cst2;
148 int r;
149 cst1 = isl_poly_as_cst(poly1);
150 cst2 = isl_poly_as_cst(poly2);
151 if (!cst1 || !cst2)
152 return isl_bool_error;
153 r = isl_int_eq(cst1->n, cst2->n) &&
154 isl_int_eq(cst1->d, cst2->d);
155 return isl_bool_ok(r);
158 rec1 = isl_poly_as_rec(poly1);
159 rec2 = isl_poly_as_rec(poly2);
160 if (!rec1 || !rec2)
161 return isl_bool_error;
163 if (rec1->n != rec2->n)
164 return isl_bool_false;
166 for (i = 0; i < rec1->n; ++i) {
167 isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
168 if (eq < 0 || !eq)
169 return eq;
172 return isl_bool_true;
175 isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
177 isl_bool is_cst;
178 isl_poly_cst *cst;
180 is_cst = isl_poly_is_cst(poly);
181 if (is_cst < 0 || !is_cst)
182 return is_cst;
184 cst = isl_poly_as_cst(poly);
185 if (!cst)
186 return isl_bool_error;
188 return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d));
191 int isl_poly_sgn(__isl_keep isl_poly *poly)
193 isl_bool is_cst;
194 isl_poly_cst *cst;
196 is_cst = isl_poly_is_cst(poly);
197 if (is_cst < 0 || !is_cst)
198 return 0;
200 cst = isl_poly_as_cst(poly);
201 if (!cst)
202 return 0;
204 return isl_int_sgn(cst->n);
207 isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
209 isl_bool is_cst;
210 isl_poly_cst *cst;
212 is_cst = isl_poly_is_cst(poly);
213 if (is_cst < 0 || !is_cst)
214 return is_cst;
216 cst = isl_poly_as_cst(poly);
217 if (!cst)
218 return isl_bool_error;
220 return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d));
223 isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
225 isl_bool is_cst;
226 isl_poly_cst *cst;
228 is_cst = isl_poly_is_cst(poly);
229 if (is_cst < 0 || !is_cst)
230 return is_cst;
232 cst = isl_poly_as_cst(poly);
233 if (!cst)
234 return isl_bool_error;
236 return isl_bool_ok(isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d));
239 isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
241 isl_bool is_cst;
242 isl_poly_cst *cst;
244 is_cst = isl_poly_is_cst(poly);
245 if (is_cst < 0 || !is_cst)
246 return is_cst;
248 cst = isl_poly_as_cst(poly);
249 if (!cst)
250 return isl_bool_error;
252 return isl_bool_ok(isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d));
255 isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
257 isl_bool is_cst;
258 isl_poly_cst *cst;
259 int r;
261 is_cst = isl_poly_is_cst(poly);
262 if (is_cst < 0 || !is_cst)
263 return is_cst;
265 cst = isl_poly_as_cst(poly);
266 if (!cst)
267 return isl_bool_error;
269 r = isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
270 return isl_bool_ok(r);
273 isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
275 isl_bool is_cst;
276 isl_poly_cst *cst;
278 is_cst = isl_poly_is_cst(poly);
279 if (is_cst < 0 || !is_cst)
280 return is_cst;
282 cst = isl_poly_as_cst(poly);
283 if (!cst)
284 return isl_bool_error;
286 return isl_bool_ok(isl_int_is_negone(cst->n) && isl_int_is_one(cst->d));
289 __isl_give isl_poly_cst *isl_poly_cst_alloc(isl_ctx *ctx)
291 isl_poly_cst *cst;
293 cst = isl_alloc_type(ctx, struct isl_poly_cst);
294 if (!cst)
295 return NULL;
297 cst->poly.ref = 1;
298 cst->poly.ctx = ctx;
299 isl_ctx_ref(ctx);
300 cst->poly.var = -1;
302 isl_int_init(cst->n);
303 isl_int_init(cst->d);
305 return cst;
308 __isl_give isl_poly *isl_poly_zero(isl_ctx *ctx)
310 isl_poly_cst *cst;
312 cst = isl_poly_cst_alloc(ctx);
313 if (!cst)
314 return NULL;
316 isl_int_set_si(cst->n, 0);
317 isl_int_set_si(cst->d, 1);
319 return &cst->poly;
322 __isl_give isl_poly *isl_poly_one(isl_ctx *ctx)
324 isl_poly_cst *cst;
326 cst = isl_poly_cst_alloc(ctx);
327 if (!cst)
328 return NULL;
330 isl_int_set_si(cst->n, 1);
331 isl_int_set_si(cst->d, 1);
333 return &cst->poly;
336 __isl_give isl_poly *isl_poly_infty(isl_ctx *ctx)
338 isl_poly_cst *cst;
340 cst = isl_poly_cst_alloc(ctx);
341 if (!cst)
342 return NULL;
344 isl_int_set_si(cst->n, 1);
345 isl_int_set_si(cst->d, 0);
347 return &cst->poly;
350 __isl_give isl_poly *isl_poly_neginfty(isl_ctx *ctx)
352 isl_poly_cst *cst;
354 cst = isl_poly_cst_alloc(ctx);
355 if (!cst)
356 return NULL;
358 isl_int_set_si(cst->n, -1);
359 isl_int_set_si(cst->d, 0);
361 return &cst->poly;
364 __isl_give isl_poly *isl_poly_nan(isl_ctx *ctx)
366 isl_poly_cst *cst;
368 cst = isl_poly_cst_alloc(ctx);
369 if (!cst)
370 return NULL;
372 isl_int_set_si(cst->n, 0);
373 isl_int_set_si(cst->d, 0);
375 return &cst->poly;
378 __isl_give isl_poly *isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
380 isl_poly_cst *cst;
382 cst = isl_poly_cst_alloc(ctx);
383 if (!cst)
384 return NULL;
386 isl_int_set(cst->n, n);
387 isl_int_set(cst->d, d);
389 return &cst->poly;
392 __isl_give isl_poly_rec *isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
394 isl_poly_rec *rec;
396 isl_assert(ctx, var >= 0, return NULL);
397 isl_assert(ctx, size >= 0, return NULL);
398 rec = isl_calloc(ctx, struct isl_poly_rec,
399 sizeof(struct isl_poly_rec) +
400 size * sizeof(struct isl_poly *));
401 if (!rec)
402 return NULL;
404 rec->poly.ref = 1;
405 rec->poly.ctx = ctx;
406 isl_ctx_ref(ctx);
407 rec->poly.var = var;
409 rec->n = 0;
410 rec->size = size;
412 return rec;
415 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
416 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
418 qp = isl_qpolynomial_cow(qp);
419 if (!qp || !space)
420 goto error;
422 isl_space_free(qp->dim);
423 qp->dim = space;
425 return qp;
426 error:
427 isl_qpolynomial_free(qp);
428 isl_space_free(space);
429 return NULL;
432 /* Reset the space of "qp". This function is called from isl_pw_templ.c
433 * and doesn't know if the space of an element object is represented
434 * directly or through its domain. It therefore passes along both.
436 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
437 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
438 __isl_take isl_space *domain)
440 isl_space_free(space);
441 return isl_qpolynomial_reset_domain_space(qp, domain);
444 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
446 return qp ? qp->dim->ctx : NULL;
449 /* Return the domain space of "qp".
451 static __isl_keep isl_space *isl_qpolynomial_peek_domain_space(
452 __isl_keep isl_qpolynomial *qp)
454 return qp ? qp->dim : NULL;
457 /* Return a copy of the domain space of "qp".
459 __isl_give isl_space *isl_qpolynomial_get_domain_space(
460 __isl_keep isl_qpolynomial *qp)
462 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp));
465 #undef TYPE
466 #define TYPE isl_qpolynomial
467 #undef PEEK_SPACE
468 #define PEEK_SPACE peek_domain_space
470 static
471 #include "isl_type_has_equal_space_bin_templ.c"
472 static
473 #include "isl_type_check_equal_space_templ.c"
475 #undef PEEK_SPACE
477 /* Return a copy of the local space on which "qp" is defined.
479 static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
480 __isl_keep isl_qpolynomial *qp)
482 isl_space *space;
484 if (!qp)
485 return NULL;
487 space = isl_qpolynomial_get_domain_space(qp);
488 return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
491 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
493 isl_space *space;
494 if (!qp)
495 return NULL;
496 space = isl_space_copy(qp->dim);
497 space = isl_space_from_domain(space);
498 space = isl_space_add_dims(space, isl_dim_out, 1);
499 return space;
502 /* Return the number of variables of the given type in the domain of "qp".
504 isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
505 enum isl_dim_type type)
507 isl_space *space;
508 isl_size dim;
510 space = isl_qpolynomial_peek_domain_space(qp);
512 if (!space)
513 return isl_size_error;
514 if (type == isl_dim_div)
515 return qp->div->n_row;
516 dim = isl_space_dim(space, type);
517 if (dim < 0)
518 return isl_size_error;
519 if (type == isl_dim_all) {
520 isl_size n_div;
522 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
523 if (n_div < 0)
524 return isl_size_error;
525 dim += n_div;
527 return dim;
530 /* Given the type of a dimension of an isl_qpolynomial,
531 * return the type of the corresponding dimension in its domain.
532 * This function is only called for "type" equal to isl_dim_in or
533 * isl_dim_param.
535 static enum isl_dim_type domain_type(enum isl_dim_type type)
537 return type == isl_dim_in ? isl_dim_set : type;
540 /* Externally, an isl_qpolynomial has a map space, but internally, the
541 * ls field corresponds to the domain of that space.
543 isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
544 enum isl_dim_type type)
546 if (!qp)
547 return isl_size_error;
548 if (type == isl_dim_out)
549 return 1;
550 type = domain_type(type);
551 return isl_qpolynomial_domain_dim(qp, type);
554 /* Return the offset of the first variable of type "type" within
555 * the variables of the domain of "qp".
557 static isl_size isl_qpolynomial_domain_var_offset(
558 __isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
560 isl_space *space;
562 space = isl_qpolynomial_peek_domain_space(qp);
564 switch (type) {
565 case isl_dim_param:
566 case isl_dim_set: return isl_space_offset(space, type);
567 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
568 case isl_dim_cst:
569 default:
570 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
571 "invalid dimension type", return isl_size_error);
575 /* Return the offset of the first coefficient of type "type" in
576 * the domain of "qp".
578 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
579 enum isl_dim_type type)
581 switch (type) {
582 case isl_dim_cst:
583 return 0;
584 case isl_dim_param:
585 case isl_dim_set:
586 case isl_dim_div:
587 return 1 + isl_qpolynomial_domain_var_offset(qp, type);
588 default:
589 return 0;
593 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
595 return qp ? isl_poly_is_zero(qp->poly) : isl_bool_error;
598 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
600 return qp ? isl_poly_is_one(qp->poly) : isl_bool_error;
603 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
605 return qp ? isl_poly_is_nan(qp->poly) : isl_bool_error;
608 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
610 return qp ? isl_poly_is_infty(qp->poly) : isl_bool_error;
613 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
615 return qp ? isl_poly_is_neginfty(qp->poly) : isl_bool_error;
618 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
620 return qp ? isl_poly_sgn(qp->poly) : 0;
623 static void poly_free_cst(__isl_take isl_poly_cst *cst)
625 isl_int_clear(cst->n);
626 isl_int_clear(cst->d);
629 static void poly_free_rec(__isl_take isl_poly_rec *rec)
631 int i;
633 for (i = 0; i < rec->n; ++i)
634 isl_poly_free(rec->p[i]);
637 __isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
639 if (!poly)
640 return NULL;
642 poly->ref++;
643 return poly;
646 __isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
648 isl_poly_cst *cst;
649 isl_poly_cst *dup;
651 cst = isl_poly_as_cst(poly);
652 if (!cst)
653 return NULL;
655 dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
656 if (!dup)
657 return NULL;
658 isl_int_set(dup->n, cst->n);
659 isl_int_set(dup->d, cst->d);
661 return &dup->poly;
664 __isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
666 int i;
667 isl_poly_rec *rec;
668 isl_poly_rec *dup;
670 rec = isl_poly_as_rec(poly);
671 if (!rec)
672 return NULL;
674 dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
675 if (!dup)
676 return NULL;
678 for (i = 0; i < rec->n; ++i) {
679 dup->p[i] = isl_poly_copy(rec->p[i]);
680 if (!dup->p[i])
681 goto error;
682 dup->n++;
685 return &dup->poly;
686 error:
687 isl_poly_free(&dup->poly);
688 return NULL;
691 __isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
693 isl_bool is_cst;
695 is_cst = isl_poly_is_cst(poly);
696 if (is_cst < 0)
697 return NULL;
698 if (is_cst)
699 return isl_poly_dup_cst(poly);
700 else
701 return isl_poly_dup_rec(poly);
704 __isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
706 if (!poly)
707 return NULL;
709 if (poly->ref == 1)
710 return poly;
711 poly->ref--;
712 return isl_poly_dup(poly);
715 __isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
717 if (!poly)
718 return NULL;
720 if (--poly->ref > 0)
721 return NULL;
723 if (poly->var < 0)
724 poly_free_cst((isl_poly_cst *) poly);
725 else
726 poly_free_rec((isl_poly_rec *) poly);
728 isl_ctx_deref(poly->ctx);
729 free(poly);
730 return NULL;
733 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
735 isl_int gcd;
737 isl_int_init(gcd);
738 isl_int_gcd(gcd, cst->n, cst->d);
739 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
740 isl_int_divexact(cst->n, cst->n, gcd);
741 isl_int_divexact(cst->d, cst->d, gcd);
743 isl_int_clear(gcd);
746 __isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
747 __isl_take isl_poly *poly2)
749 isl_poly_cst *cst1;
750 isl_poly_cst *cst2;
752 poly1 = isl_poly_cow(poly1);
753 if (!poly1 || !poly2)
754 goto error;
756 cst1 = isl_poly_as_cst(poly1);
757 cst2 = isl_poly_as_cst(poly2);
759 if (isl_int_eq(cst1->d, cst2->d))
760 isl_int_add(cst1->n, cst1->n, cst2->n);
761 else {
762 isl_int_mul(cst1->n, cst1->n, cst2->d);
763 isl_int_addmul(cst1->n, cst2->n, cst1->d);
764 isl_int_mul(cst1->d, cst1->d, cst2->d);
767 isl_poly_cst_reduce(cst1);
769 isl_poly_free(poly2);
770 return poly1;
771 error:
772 isl_poly_free(poly1);
773 isl_poly_free(poly2);
774 return NULL;
777 static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
779 struct isl_ctx *ctx;
781 if (!poly)
782 return NULL;
783 ctx = poly->ctx;
784 isl_poly_free(poly);
785 return isl_poly_zero(ctx);
788 static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
790 isl_poly_rec *rec;
791 isl_poly *cst;
793 if (!poly)
794 return NULL;
796 rec = isl_poly_as_rec(poly);
797 if (!rec)
798 goto error;
799 cst = isl_poly_copy(rec->p[0]);
800 isl_poly_free(poly);
801 return cst;
802 error:
803 isl_poly_free(poly);
804 return NULL;
807 __isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
808 __isl_take isl_poly *poly2)
810 int i;
811 isl_bool is_zero, is_nan, is_cst;
812 isl_poly_rec *rec1, *rec2;
814 if (!poly1 || !poly2)
815 goto error;
817 is_nan = isl_poly_is_nan(poly1);
818 if (is_nan < 0)
819 goto error;
820 if (is_nan) {
821 isl_poly_free(poly2);
822 return poly1;
825 is_nan = isl_poly_is_nan(poly2);
826 if (is_nan < 0)
827 goto error;
828 if (is_nan) {
829 isl_poly_free(poly1);
830 return poly2;
833 is_zero = isl_poly_is_zero(poly1);
834 if (is_zero < 0)
835 goto error;
836 if (is_zero) {
837 isl_poly_free(poly1);
838 return poly2;
841 is_zero = isl_poly_is_zero(poly2);
842 if (is_zero < 0)
843 goto error;
844 if (is_zero) {
845 isl_poly_free(poly2);
846 return poly1;
849 if (poly1->var < poly2->var)
850 return isl_poly_sum(poly2, poly1);
852 if (poly2->var < poly1->var) {
853 isl_poly_rec *rec;
854 isl_bool is_infty;
856 is_infty = isl_poly_is_infty(poly2);
857 if (is_infty >= 0 && !is_infty)
858 is_infty = isl_poly_is_neginfty(poly2);
859 if (is_infty < 0)
860 goto error;
861 if (is_infty) {
862 isl_poly_free(poly1);
863 return poly2;
865 poly1 = isl_poly_cow(poly1);
866 rec = isl_poly_as_rec(poly1);
867 if (!rec)
868 goto error;
869 rec->p[0] = isl_poly_sum(rec->p[0], poly2);
870 if (rec->n == 1)
871 poly1 = replace_by_constant_term(poly1);
872 return poly1;
875 is_cst = isl_poly_is_cst(poly1);
876 if (is_cst < 0)
877 goto error;
878 if (is_cst)
879 return isl_poly_sum_cst(poly1, poly2);
881 rec1 = isl_poly_as_rec(poly1);
882 rec2 = isl_poly_as_rec(poly2);
883 if (!rec1 || !rec2)
884 goto error;
886 if (rec1->n < rec2->n)
887 return isl_poly_sum(poly2, poly1);
889 poly1 = isl_poly_cow(poly1);
890 rec1 = isl_poly_as_rec(poly1);
891 if (!rec1)
892 goto error;
894 for (i = rec2->n - 1; i >= 0; --i) {
895 isl_bool is_zero;
897 rec1->p[i] = isl_poly_sum(rec1->p[i],
898 isl_poly_copy(rec2->p[i]));
899 if (!rec1->p[i])
900 goto error;
901 if (i != rec1->n - 1)
902 continue;
903 is_zero = isl_poly_is_zero(rec1->p[i]);
904 if (is_zero < 0)
905 goto error;
906 if (is_zero) {
907 isl_poly_free(rec1->p[i]);
908 rec1->n--;
912 if (rec1->n == 0)
913 poly1 = replace_by_zero(poly1);
914 else if (rec1->n == 1)
915 poly1 = replace_by_constant_term(poly1);
917 isl_poly_free(poly2);
919 return poly1;
920 error:
921 isl_poly_free(poly1);
922 isl_poly_free(poly2);
923 return NULL;
926 __isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
927 isl_int v)
929 isl_poly_cst *cst;
931 poly = isl_poly_cow(poly);
932 if (!poly)
933 return NULL;
935 cst = isl_poly_as_cst(poly);
937 isl_int_addmul(cst->n, cst->d, v);
939 return poly;
942 __isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
944 isl_bool is_cst;
945 isl_poly_rec *rec;
947 is_cst = isl_poly_is_cst(poly);
948 if (is_cst < 0)
949 return isl_poly_free(poly);
950 if (is_cst)
951 return isl_poly_cst_add_isl_int(poly, v);
953 poly = isl_poly_cow(poly);
954 rec = isl_poly_as_rec(poly);
955 if (!rec)
956 goto error;
958 rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
959 if (!rec->p[0])
960 goto error;
962 return poly;
963 error:
964 isl_poly_free(poly);
965 return NULL;
968 __isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
969 isl_int v)
971 isl_bool is_zero;
972 isl_poly_cst *cst;
974 is_zero = isl_poly_is_zero(poly);
975 if (is_zero < 0)
976 return isl_poly_free(poly);
977 if (is_zero)
978 return poly;
980 poly = isl_poly_cow(poly);
981 if (!poly)
982 return NULL;
984 cst = isl_poly_as_cst(poly);
986 isl_int_mul(cst->n, cst->n, v);
988 return poly;
991 __isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
993 int i;
994 isl_bool is_cst;
995 isl_poly_rec *rec;
997 is_cst = isl_poly_is_cst(poly);
998 if (is_cst < 0)
999 return isl_poly_free(poly);
1000 if (is_cst)
1001 return isl_poly_cst_mul_isl_int(poly, v);
1003 poly = isl_poly_cow(poly);
1004 rec = isl_poly_as_rec(poly);
1005 if (!rec)
1006 goto error;
1008 for (i = 0; i < rec->n; ++i) {
1009 rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
1010 if (!rec->p[i])
1011 goto error;
1014 return poly;
1015 error:
1016 isl_poly_free(poly);
1017 return NULL;
1020 /* Multiply the constant polynomial "poly" by "v".
1022 static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
1023 __isl_keep isl_val *v)
1025 isl_bool is_zero;
1026 isl_poly_cst *cst;
1028 is_zero = isl_poly_is_zero(poly);
1029 if (is_zero < 0)
1030 return isl_poly_free(poly);
1031 if (is_zero)
1032 return poly;
1034 poly = isl_poly_cow(poly);
1035 if (!poly)
1036 return NULL;
1038 cst = isl_poly_as_cst(poly);
1040 isl_int_mul(cst->n, cst->n, v->n);
1041 isl_int_mul(cst->d, cst->d, v->d);
1042 isl_poly_cst_reduce(cst);
1044 return poly;
1047 /* Multiply the polynomial "poly" by "v".
1049 static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
1050 __isl_keep isl_val *v)
1052 int i;
1053 isl_bool is_cst;
1054 isl_poly_rec *rec;
1056 is_cst = isl_poly_is_cst(poly);
1057 if (is_cst < 0)
1058 return isl_poly_free(poly);
1059 if (is_cst)
1060 return isl_poly_cst_scale_val(poly, v);
1062 poly = isl_poly_cow(poly);
1063 rec = isl_poly_as_rec(poly);
1064 if (!rec)
1065 goto error;
1067 for (i = 0; i < rec->n; ++i) {
1068 rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1069 if (!rec->p[i])
1070 goto error;
1073 return poly;
1074 error:
1075 isl_poly_free(poly);
1076 return NULL;
1079 __isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
1080 __isl_take isl_poly *poly2)
1082 isl_poly_cst *cst1;
1083 isl_poly_cst *cst2;
1085 poly1 = isl_poly_cow(poly1);
1086 if (!poly1 || !poly2)
1087 goto error;
1089 cst1 = isl_poly_as_cst(poly1);
1090 cst2 = isl_poly_as_cst(poly2);
1092 isl_int_mul(cst1->n, cst1->n, cst2->n);
1093 isl_int_mul(cst1->d, cst1->d, cst2->d);
1095 isl_poly_cst_reduce(cst1);
1097 isl_poly_free(poly2);
1098 return poly1;
1099 error:
1100 isl_poly_free(poly1);
1101 isl_poly_free(poly2);
1102 return NULL;
1105 __isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
1106 __isl_take isl_poly *poly2)
1108 isl_poly_rec *rec1;
1109 isl_poly_rec *rec2;
1110 isl_poly_rec *res = NULL;
1111 int i, j;
1112 int size;
1114 rec1 = isl_poly_as_rec(poly1);
1115 rec2 = isl_poly_as_rec(poly2);
1116 if (!rec1 || !rec2)
1117 goto error;
1118 size = rec1->n + rec2->n - 1;
1119 res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1120 if (!res)
1121 goto error;
1123 for (i = 0; i < rec1->n; ++i) {
1124 res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1125 isl_poly_copy(rec1->p[i]));
1126 if (!res->p[i])
1127 goto error;
1128 res->n++;
1130 for (; i < size; ++i) {
1131 res->p[i] = isl_poly_zero(poly1->ctx);
1132 if (!res->p[i])
1133 goto error;
1134 res->n++;
1136 for (i = 0; i < rec1->n; ++i) {
1137 for (j = 1; j < rec2->n; ++j) {
1138 isl_poly *poly;
1139 poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1140 isl_poly_copy(rec1->p[i]));
1141 res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1142 if (!res->p[i + j])
1143 goto error;
1147 isl_poly_free(poly1);
1148 isl_poly_free(poly2);
1150 return &res->poly;
1151 error:
1152 isl_poly_free(poly1);
1153 isl_poly_free(poly2);
1154 isl_poly_free(&res->poly);
1155 return NULL;
1158 __isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
1159 __isl_take isl_poly *poly2)
1161 isl_bool is_zero, is_nan, is_one, is_cst;
1163 if (!poly1 || !poly2)
1164 goto error;
1166 is_nan = isl_poly_is_nan(poly1);
1167 if (is_nan < 0)
1168 goto error;
1169 if (is_nan) {
1170 isl_poly_free(poly2);
1171 return poly1;
1174 is_nan = isl_poly_is_nan(poly2);
1175 if (is_nan < 0)
1176 goto error;
1177 if (is_nan) {
1178 isl_poly_free(poly1);
1179 return poly2;
1182 is_zero = isl_poly_is_zero(poly1);
1183 if (is_zero < 0)
1184 goto error;
1185 if (is_zero) {
1186 isl_poly_free(poly2);
1187 return poly1;
1190 is_zero = isl_poly_is_zero(poly2);
1191 if (is_zero < 0)
1192 goto error;
1193 if (is_zero) {
1194 isl_poly_free(poly1);
1195 return poly2;
1198 is_one = isl_poly_is_one(poly1);
1199 if (is_one < 0)
1200 goto error;
1201 if (is_one) {
1202 isl_poly_free(poly1);
1203 return poly2;
1206 is_one = isl_poly_is_one(poly2);
1207 if (is_one < 0)
1208 goto error;
1209 if (is_one) {
1210 isl_poly_free(poly2);
1211 return poly1;
1214 if (poly1->var < poly2->var)
1215 return isl_poly_mul(poly2, poly1);
1217 if (poly2->var < poly1->var) {
1218 int i;
1219 isl_poly_rec *rec;
1220 isl_bool is_infty;
1222 is_infty = isl_poly_is_infty(poly2);
1223 if (is_infty >= 0 && !is_infty)
1224 is_infty = isl_poly_is_neginfty(poly2);
1225 if (is_infty < 0)
1226 goto error;
1227 if (is_infty) {
1228 isl_ctx *ctx = poly1->ctx;
1229 isl_poly_free(poly1);
1230 isl_poly_free(poly2);
1231 return isl_poly_nan(ctx);
1233 poly1 = isl_poly_cow(poly1);
1234 rec = isl_poly_as_rec(poly1);
1235 if (!rec)
1236 goto error;
1238 for (i = 0; i < rec->n; ++i) {
1239 rec->p[i] = isl_poly_mul(rec->p[i],
1240 isl_poly_copy(poly2));
1241 if (!rec->p[i])
1242 goto error;
1244 isl_poly_free(poly2);
1245 return poly1;
1248 is_cst = isl_poly_is_cst(poly1);
1249 if (is_cst < 0)
1250 goto error;
1251 if (is_cst)
1252 return isl_poly_mul_cst(poly1, poly2);
1254 return isl_poly_mul_rec(poly1, poly2);
1255 error:
1256 isl_poly_free(poly1);
1257 isl_poly_free(poly2);
1258 return NULL;
1261 __isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
1263 isl_poly *res;
1265 if (!poly)
1266 return NULL;
1267 if (power == 1)
1268 return poly;
1270 if (power % 2)
1271 res = isl_poly_copy(poly);
1272 else
1273 res = isl_poly_one(poly->ctx);
1275 while (power >>= 1) {
1276 poly = isl_poly_mul(poly, isl_poly_copy(poly));
1277 if (power % 2)
1278 res = isl_poly_mul(res, isl_poly_copy(poly));
1281 isl_poly_free(poly);
1282 return res;
1285 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1286 unsigned n_div, __isl_take isl_poly *poly)
1288 struct isl_qpolynomial *qp = NULL;
1289 isl_size total;
1291 total = isl_space_dim(space, isl_dim_all);
1292 if (total < 0 || !poly)
1293 goto error;
1295 if (!isl_space_is_set(space))
1296 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1297 "domain of polynomial should be a set", goto error);
1299 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1300 if (!qp)
1301 goto error;
1303 qp->ref = 1;
1304 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1305 if (!qp->div)
1306 goto error;
1308 qp->dim = space;
1309 qp->poly = poly;
1311 return qp;
1312 error:
1313 isl_space_free(space);
1314 isl_poly_free(poly);
1315 isl_qpolynomial_free(qp);
1316 return NULL;
1319 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1321 if (!qp)
1322 return NULL;
1324 qp->ref++;
1325 return qp;
1328 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1330 struct isl_qpolynomial *dup;
1332 if (!qp)
1333 return NULL;
1335 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1336 isl_poly_copy(qp->poly));
1337 if (!dup)
1338 return NULL;
1339 isl_mat_free(dup->div);
1340 dup->div = isl_mat_copy(qp->div);
1341 if (!dup->div)
1342 goto error;
1344 return dup;
1345 error:
1346 isl_qpolynomial_free(dup);
1347 return NULL;
1350 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1352 if (!qp)
1353 return NULL;
1355 if (qp->ref == 1)
1356 return qp;
1357 qp->ref--;
1358 return isl_qpolynomial_dup(qp);
1361 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1362 __isl_take isl_qpolynomial *qp)
1364 if (!qp)
1365 return NULL;
1367 if (--qp->ref > 0)
1368 return NULL;
1370 isl_space_free(qp->dim);
1371 isl_mat_free(qp->div);
1372 isl_poly_free(qp->poly);
1374 free(qp);
1375 return NULL;
1378 __isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
1380 int i;
1381 isl_poly_rec *rec;
1382 isl_poly_cst *cst;
1384 rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1385 if (!rec)
1386 return NULL;
1387 for (i = 0; i < 1 + power; ++i) {
1388 rec->p[i] = isl_poly_zero(ctx);
1389 if (!rec->p[i])
1390 goto error;
1391 rec->n++;
1393 cst = isl_poly_as_cst(rec->p[power]);
1394 isl_int_set_si(cst->n, 1);
1396 return &rec->poly;
1397 error:
1398 isl_poly_free(&rec->poly);
1399 return NULL;
1402 /* r array maps original positions to new positions.
1404 static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
1406 int i;
1407 isl_bool is_cst;
1408 isl_poly_rec *rec;
1409 isl_poly *base;
1410 isl_poly *res;
1412 is_cst = isl_poly_is_cst(poly);
1413 if (is_cst < 0)
1414 return isl_poly_free(poly);
1415 if (is_cst)
1416 return poly;
1418 rec = isl_poly_as_rec(poly);
1419 if (!rec)
1420 goto error;
1422 isl_assert(poly->ctx, rec->n >= 1, goto error);
1424 base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1425 res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1427 for (i = rec->n - 2; i >= 0; --i) {
1428 res = isl_poly_mul(res, isl_poly_copy(base));
1429 res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1432 isl_poly_free(base);
1433 isl_poly_free(poly);
1435 return res;
1436 error:
1437 isl_poly_free(poly);
1438 return NULL;
1441 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1442 __isl_keep isl_mat *div2)
1444 int n_row, n_col;
1445 isl_bool equal;
1447 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1448 div1->n_col >= div2->n_col,
1449 return isl_bool_error);
1451 if (div1->n_row == div2->n_row)
1452 return isl_mat_is_equal(div1, div2);
1454 n_row = div1->n_row;
1455 n_col = div1->n_col;
1456 div1->n_row = div2->n_row;
1457 div1->n_col = div2->n_col;
1459 equal = isl_mat_is_equal(div1, div2);
1461 div1->n_row = n_row;
1462 div1->n_col = n_col;
1464 return equal;
1467 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1469 int li, lj;
1471 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1472 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1474 if (li != lj)
1475 return li - lj;
1477 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1480 struct isl_div_sort_info {
1481 isl_mat *div;
1482 int row;
1485 static int div_sort_cmp(const void *p1, const void *p2)
1487 const struct isl_div_sort_info *i1, *i2;
1488 i1 = (const struct isl_div_sort_info *) p1;
1489 i2 = (const struct isl_div_sort_info *) p2;
1491 return cmp_row(i1->div, i1->row, i2->row);
1494 /* Sort divs and remove duplicates.
1496 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1498 int i;
1499 int skip;
1500 int len;
1501 struct isl_div_sort_info *array = NULL;
1502 int *pos = NULL, *at = NULL;
1503 int *reordering = NULL;
1504 isl_size div_pos;
1506 if (!qp)
1507 return NULL;
1508 if (qp->div->n_row <= 1)
1509 return qp;
1511 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
1512 if (div_pos < 0)
1513 return isl_qpolynomial_free(qp);
1515 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1516 qp->div->n_row);
1517 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1518 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1519 len = qp->div->n_col - 2;
1520 reordering = isl_alloc_array(qp->div->ctx, int, len);
1521 if (!array || !pos || !at || !reordering)
1522 goto error;
1524 for (i = 0; i < qp->div->n_row; ++i) {
1525 array[i].div = qp->div;
1526 array[i].row = i;
1527 pos[i] = i;
1528 at[i] = i;
1531 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1532 div_sort_cmp);
1534 for (i = 0; i < div_pos; ++i)
1535 reordering[i] = i;
1537 for (i = 0; i < qp->div->n_row; ++i) {
1538 if (pos[array[i].row] == i)
1539 continue;
1540 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1541 pos[at[i]] = pos[array[i].row];
1542 at[pos[array[i].row]] = at[i];
1543 at[i] = array[i].row;
1544 pos[array[i].row] = i;
1547 skip = 0;
1548 for (i = 0; i < len - div_pos; ++i) {
1549 if (i > 0 &&
1550 isl_seq_eq(qp->div->row[i - skip - 1],
1551 qp->div->row[i - skip], qp->div->n_col)) {
1552 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1553 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1554 2 + div_pos + i - skip);
1555 qp->div = isl_mat_drop_cols(qp->div,
1556 2 + div_pos + i - skip, 1);
1557 skip++;
1559 reordering[div_pos + array[i].row] = div_pos + i - skip;
1562 qp->poly = reorder(qp->poly, reordering);
1564 if (!qp->poly || !qp->div)
1565 goto error;
1567 free(at);
1568 free(pos);
1569 free(array);
1570 free(reordering);
1572 return qp;
1573 error:
1574 free(at);
1575 free(pos);
1576 free(array);
1577 free(reordering);
1578 isl_qpolynomial_free(qp);
1579 return NULL;
1582 static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
1583 int first)
1585 int i;
1586 isl_bool is_cst;
1587 isl_poly_rec *rec;
1589 is_cst = isl_poly_is_cst(poly);
1590 if (is_cst < 0)
1591 return isl_poly_free(poly);
1592 if (is_cst)
1593 return poly;
1595 if (poly->var < first)
1596 return poly;
1598 if (exp[poly->var - first] == poly->var - first)
1599 return poly;
1601 poly = isl_poly_cow(poly);
1602 if (!poly)
1603 goto error;
1605 poly->var = exp[poly->var - first] + first;
1607 rec = isl_poly_as_rec(poly);
1608 if (!rec)
1609 goto error;
1611 for (i = 0; i < rec->n; ++i) {
1612 rec->p[i] = expand(rec->p[i], exp, first);
1613 if (!rec->p[i])
1614 goto error;
1617 return poly;
1618 error:
1619 isl_poly_free(poly);
1620 return NULL;
1623 static __isl_give isl_qpolynomial *with_merged_divs(
1624 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1625 __isl_take isl_qpolynomial *qp2),
1626 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1628 int *exp1 = NULL;
1629 int *exp2 = NULL;
1630 isl_mat *div = NULL;
1631 int n_div1, n_div2;
1633 qp1 = isl_qpolynomial_cow(qp1);
1634 qp2 = isl_qpolynomial_cow(qp2);
1636 if (!qp1 || !qp2)
1637 goto error;
1639 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1640 qp1->div->n_col >= qp2->div->n_col, goto error);
1642 n_div1 = qp1->div->n_row;
1643 n_div2 = qp2->div->n_row;
1644 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1645 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1646 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1647 goto error;
1649 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1650 if (!div)
1651 goto error;
1653 isl_mat_free(qp1->div);
1654 qp1->div = isl_mat_copy(div);
1655 isl_mat_free(qp2->div);
1656 qp2->div = isl_mat_copy(div);
1658 qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1659 qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1661 if (!qp1->poly || !qp2->poly)
1662 goto error;
1664 isl_mat_free(div);
1665 free(exp1);
1666 free(exp2);
1668 return fn(qp1, qp2);
1669 error:
1670 isl_mat_free(div);
1671 free(exp1);
1672 free(exp2);
1673 isl_qpolynomial_free(qp1);
1674 isl_qpolynomial_free(qp2);
1675 return NULL;
1678 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1679 __isl_take isl_qpolynomial *qp2)
1681 isl_bool compatible;
1683 qp1 = isl_qpolynomial_cow(qp1);
1685 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1686 goto error;
1688 if (qp1->div->n_row < qp2->div->n_row)
1689 return isl_qpolynomial_add(qp2, qp1);
1691 compatible = compatible_divs(qp1->div, qp2->div);
1692 if (compatible < 0)
1693 goto error;
1694 if (!compatible)
1695 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1697 qp1->poly = isl_poly_sum(qp1->poly, isl_poly_copy(qp2->poly));
1698 if (!qp1->poly)
1699 goto error;
1701 isl_qpolynomial_free(qp2);
1703 return qp1;
1704 error:
1705 isl_qpolynomial_free(qp1);
1706 isl_qpolynomial_free(qp2);
1707 return NULL;
1710 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1711 __isl_keep isl_set *dom,
1712 __isl_take isl_qpolynomial *qp1,
1713 __isl_take isl_qpolynomial *qp2)
1715 qp1 = isl_qpolynomial_add(qp1, qp2);
1716 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1717 return qp1;
1720 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1721 __isl_take isl_qpolynomial *qp2)
1723 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1726 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1727 __isl_take isl_qpolynomial *qp, isl_int v)
1729 if (isl_int_is_zero(v))
1730 return qp;
1732 qp = isl_qpolynomial_cow(qp);
1733 if (!qp)
1734 return NULL;
1736 qp->poly = isl_poly_add_isl_int(qp->poly, v);
1737 if (!qp->poly)
1738 goto error;
1740 return qp;
1741 error:
1742 isl_qpolynomial_free(qp);
1743 return NULL;
1747 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1749 if (!qp)
1750 return NULL;
1752 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1755 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1756 __isl_take isl_qpolynomial *qp, isl_int v)
1758 if (isl_int_is_one(v))
1759 return qp;
1761 if (qp && isl_int_is_zero(v)) {
1762 isl_qpolynomial *zero;
1763 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1764 isl_qpolynomial_free(qp);
1765 return zero;
1768 qp = isl_qpolynomial_cow(qp);
1769 if (!qp)
1770 return NULL;
1772 qp->poly = isl_poly_mul_isl_int(qp->poly, v);
1773 if (!qp->poly)
1774 goto error;
1776 return qp;
1777 error:
1778 isl_qpolynomial_free(qp);
1779 return NULL;
1782 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1783 __isl_take isl_qpolynomial *qp, isl_int v)
1785 return isl_qpolynomial_mul_isl_int(qp, v);
1788 /* Multiply "qp" by "v".
1790 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1791 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1793 if (!qp || !v)
1794 goto error;
1796 if (!isl_val_is_rat(v))
1797 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1798 "expecting rational factor", goto error);
1800 if (isl_val_is_one(v)) {
1801 isl_val_free(v);
1802 return qp;
1805 if (isl_val_is_zero(v)) {
1806 isl_space *space;
1808 space = isl_qpolynomial_get_domain_space(qp);
1809 isl_qpolynomial_free(qp);
1810 isl_val_free(v);
1811 return isl_qpolynomial_zero_on_domain(space);
1814 qp = isl_qpolynomial_cow(qp);
1815 if (!qp)
1816 goto error;
1818 qp->poly = isl_poly_scale_val(qp->poly, v);
1819 if (!qp->poly)
1820 qp = isl_qpolynomial_free(qp);
1822 isl_val_free(v);
1823 return qp;
1824 error:
1825 isl_val_free(v);
1826 isl_qpolynomial_free(qp);
1827 return NULL;
1830 /* Divide "qp" by "v".
1832 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1833 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1835 if (!qp || !v)
1836 goto error;
1838 if (!isl_val_is_rat(v))
1839 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1840 "expecting rational factor", goto error);
1841 if (isl_val_is_zero(v))
1842 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1843 "cannot scale down by zero", goto error);
1845 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1846 error:
1847 isl_val_free(v);
1848 isl_qpolynomial_free(qp);
1849 return NULL;
1852 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1853 __isl_take isl_qpolynomial *qp2)
1855 isl_bool compatible;
1857 qp1 = isl_qpolynomial_cow(qp1);
1859 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1860 goto error;
1862 if (qp1->div->n_row < qp2->div->n_row)
1863 return isl_qpolynomial_mul(qp2, qp1);
1865 compatible = compatible_divs(qp1->div, qp2->div);
1866 if (compatible < 0)
1867 goto error;
1868 if (!compatible)
1869 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1871 qp1->poly = isl_poly_mul(qp1->poly, isl_poly_copy(qp2->poly));
1872 if (!qp1->poly)
1873 goto error;
1875 isl_qpolynomial_free(qp2);
1877 return qp1;
1878 error:
1879 isl_qpolynomial_free(qp1);
1880 isl_qpolynomial_free(qp2);
1881 return NULL;
1884 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1885 unsigned power)
1887 qp = isl_qpolynomial_cow(qp);
1889 if (!qp)
1890 return NULL;
1892 qp->poly = isl_poly_pow(qp->poly, power);
1893 if (!qp->poly)
1894 goto error;
1896 return qp;
1897 error:
1898 isl_qpolynomial_free(qp);
1899 return NULL;
1902 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1903 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1905 int i;
1907 if (power == 1)
1908 return pwqp;
1910 pwqp = isl_pw_qpolynomial_cow(pwqp);
1911 if (!pwqp)
1912 return NULL;
1914 for (i = 0; i < pwqp->n; ++i) {
1915 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1916 if (!pwqp->p[i].qp)
1917 return isl_pw_qpolynomial_free(pwqp);
1920 return pwqp;
1923 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1924 __isl_take isl_space *domain)
1926 if (!domain)
1927 return NULL;
1928 return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
1931 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1932 __isl_take isl_space *domain)
1934 if (!domain)
1935 return NULL;
1936 return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
1939 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1940 __isl_take isl_space *domain)
1942 if (!domain)
1943 return NULL;
1944 return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
1947 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1948 __isl_take isl_space *domain)
1950 if (!domain)
1951 return NULL;
1952 return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
1955 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1956 __isl_take isl_space *domain)
1958 if (!domain)
1959 return NULL;
1960 return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
1963 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1964 __isl_take isl_space *domain,
1965 isl_int v)
1967 struct isl_qpolynomial *qp;
1968 isl_poly_cst *cst;
1970 qp = isl_qpolynomial_zero_on_domain(domain);
1971 if (!qp)
1972 return NULL;
1974 cst = isl_poly_as_cst(qp->poly);
1975 isl_int_set(cst->n, v);
1977 return qp;
1980 isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1981 isl_int *n, isl_int *d)
1983 isl_bool is_cst;
1984 isl_poly_cst *cst;
1986 if (!qp)
1987 return isl_bool_error;
1989 is_cst = isl_poly_is_cst(qp->poly);
1990 if (is_cst < 0 || !is_cst)
1991 return is_cst;
1993 cst = isl_poly_as_cst(qp->poly);
1994 if (!cst)
1995 return isl_bool_error;
1997 if (n)
1998 isl_int_set(*n, cst->n);
1999 if (d)
2000 isl_int_set(*d, cst->d);
2002 return isl_bool_true;
2005 /* Return the constant term of "poly".
2007 static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
2009 isl_bool is_cst;
2010 isl_poly_cst *cst;
2012 if (!poly)
2013 return NULL;
2015 while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
2016 isl_poly_rec *rec;
2018 rec = isl_poly_as_rec(poly);
2019 if (!rec)
2020 return NULL;
2021 poly = rec->p[0];
2023 if (is_cst < 0)
2024 return NULL;
2026 cst = isl_poly_as_cst(poly);
2027 if (!cst)
2028 return NULL;
2029 return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
2032 /* Return the constant term of "qp".
2034 __isl_give isl_val *isl_qpolynomial_get_constant_val(
2035 __isl_keep isl_qpolynomial *qp)
2037 if (!qp)
2038 return NULL;
2040 return isl_poly_get_constant_val(qp->poly);
2043 isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
2045 isl_bool is_cst;
2046 isl_poly_rec *rec;
2048 if (!poly)
2049 return isl_bool_error;
2051 if (poly->var < 0)
2052 return isl_bool_true;
2054 rec = isl_poly_as_rec(poly);
2055 if (!rec)
2056 return isl_bool_error;
2058 if (rec->n > 2)
2059 return isl_bool_false;
2061 isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
2063 is_cst = isl_poly_is_cst(rec->p[1]);
2064 if (is_cst < 0 || !is_cst)
2065 return is_cst;
2067 return isl_poly_is_affine(rec->p[0]);
2070 isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
2072 if (!qp)
2073 return isl_bool_error;
2075 if (qp->div->n_row > 0)
2076 return isl_bool_false;
2078 return isl_poly_is_affine(qp->poly);
2081 static void update_coeff(__isl_keep isl_vec *aff,
2082 __isl_keep isl_poly_cst *cst, int pos)
2084 isl_int gcd;
2085 isl_int f;
2087 if (isl_int_is_zero(cst->n))
2088 return;
2090 isl_int_init(gcd);
2091 isl_int_init(f);
2092 isl_int_gcd(gcd, cst->d, aff->el[0]);
2093 isl_int_divexact(f, cst->d, gcd);
2094 isl_int_divexact(gcd, aff->el[0], gcd);
2095 isl_seq_scale(aff->el, aff->el, f, aff->size);
2096 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
2097 isl_int_clear(gcd);
2098 isl_int_clear(f);
2101 int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
2103 isl_poly_cst *cst;
2104 isl_poly_rec *rec;
2106 if (!poly || !aff)
2107 return -1;
2109 if (poly->var < 0) {
2110 isl_poly_cst *cst;
2112 cst = isl_poly_as_cst(poly);
2113 if (!cst)
2114 return -1;
2115 update_coeff(aff, cst, 0);
2116 return 0;
2119 rec = isl_poly_as_rec(poly);
2120 if (!rec)
2121 return -1;
2122 isl_assert(poly->ctx, rec->n == 2, return -1);
2124 cst = isl_poly_as_cst(rec->p[1]);
2125 if (!cst)
2126 return -1;
2127 update_coeff(aff, cst, 1 + poly->var);
2129 return isl_poly_update_affine(rec->p[0], aff);
2132 __isl_give isl_vec *isl_qpolynomial_extract_affine(
2133 __isl_keep isl_qpolynomial *qp)
2135 isl_vec *aff;
2136 isl_size d;
2138 d = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2139 if (d < 0)
2140 return NULL;
2142 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
2143 if (!aff)
2144 return NULL;
2146 isl_seq_clr(aff->el + 1, 1 + d);
2147 isl_int_set_si(aff->el[0], 1);
2149 if (isl_poly_update_affine(qp->poly, aff) < 0)
2150 goto error;
2152 return aff;
2153 error:
2154 isl_vec_free(aff);
2155 return NULL;
2158 /* Compare two quasi-polynomials.
2160 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2161 * than "qp2" and 0 if they are equal.
2163 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2164 __isl_keep isl_qpolynomial *qp2)
2166 int cmp;
2168 if (qp1 == qp2)
2169 return 0;
2170 if (!qp1)
2171 return -1;
2172 if (!qp2)
2173 return 1;
2175 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2176 if (cmp != 0)
2177 return cmp;
2179 cmp = isl_local_cmp(qp1->div, qp2->div);
2180 if (cmp != 0)
2181 return cmp;
2183 return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2186 /* Is "qp1" obviously equal to "qp2"?
2188 * NaN is not equal to anything, not even to another NaN.
2190 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2191 __isl_keep isl_qpolynomial *qp2)
2193 isl_bool equal;
2195 if (!qp1 || !qp2)
2196 return isl_bool_error;
2198 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2199 return isl_bool_false;
2201 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2202 if (equal < 0 || !equal)
2203 return equal;
2205 equal = isl_mat_is_equal(qp1->div, qp2->div);
2206 if (equal < 0 || !equal)
2207 return equal;
2209 return isl_poly_is_equal(qp1->poly, qp2->poly);
2212 static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
2214 int i;
2215 isl_bool is_cst;
2216 isl_poly_rec *rec;
2218 is_cst = isl_poly_is_cst(poly);
2219 if (is_cst < 0)
2220 return isl_stat_error;
2221 if (is_cst) {
2222 isl_poly_cst *cst;
2223 cst = isl_poly_as_cst(poly);
2224 if (!cst)
2225 return isl_stat_error;
2226 isl_int_lcm(*d, *d, cst->d);
2227 return isl_stat_ok;
2230 rec = isl_poly_as_rec(poly);
2231 if (!rec)
2232 return isl_stat_error;
2234 for (i = 0; i < rec->n; ++i)
2235 poly_update_den(rec->p[i], d);
2237 return isl_stat_ok;
2240 __isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
2242 isl_val *d;
2244 if (!qp)
2245 return NULL;
2246 d = isl_val_one(isl_qpolynomial_get_ctx(qp));
2247 if (!d)
2248 return NULL;
2249 if (poly_update_den(qp->poly, &d->n) < 0)
2250 return isl_val_free(d);
2251 return d;
2254 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2255 __isl_take isl_space *domain, int pos, int power)
2257 struct isl_ctx *ctx;
2259 if (!domain)
2260 return NULL;
2262 ctx = domain->ctx;
2264 return isl_qpolynomial_alloc(domain, 0,
2265 isl_poly_var_pow(ctx, pos, power));
2268 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2269 __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2271 isl_size off;
2273 if (isl_space_check_is_set(domain ) < 0)
2274 goto error;
2275 if (isl_space_check_range(domain, type, pos, 1) < 0)
2276 goto error;
2278 off = isl_space_offset(domain, type);
2279 if (off < 0)
2280 goto error;
2282 return isl_qpolynomial_var_pow_on_domain(domain, off + pos, 1);
2283 error:
2284 isl_space_free(domain);
2285 return NULL;
2288 __isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
2289 unsigned first, unsigned n, __isl_keep isl_poly **subs)
2291 int i;
2292 isl_bool is_cst;
2293 isl_poly_rec *rec;
2294 isl_poly *base, *res;
2296 is_cst = isl_poly_is_cst(poly);
2297 if (is_cst < 0)
2298 return isl_poly_free(poly);
2299 if (is_cst)
2300 return poly;
2302 if (poly->var < first)
2303 return poly;
2305 rec = isl_poly_as_rec(poly);
2306 if (!rec)
2307 goto error;
2309 isl_assert(poly->ctx, rec->n >= 1, goto error);
2311 if (poly->var >= first + n)
2312 base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2313 else
2314 base = isl_poly_copy(subs[poly->var - first]);
2316 res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2317 for (i = rec->n - 2; i >= 0; --i) {
2318 isl_poly *t;
2319 t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2320 res = isl_poly_mul(res, isl_poly_copy(base));
2321 res = isl_poly_sum(res, t);
2324 isl_poly_free(base);
2325 isl_poly_free(poly);
2327 return res;
2328 error:
2329 isl_poly_free(poly);
2330 return NULL;
2333 __isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
2334 isl_int denom, unsigned len)
2336 int i;
2337 isl_poly *poly;
2339 isl_assert(ctx, len >= 1, return NULL);
2341 poly = isl_poly_rat_cst(ctx, f[0], denom);
2342 for (i = 0; i < len - 1; ++i) {
2343 isl_poly *t;
2344 isl_poly *c;
2346 if (isl_int_is_zero(f[1 + i]))
2347 continue;
2349 c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2350 t = isl_poly_var_pow(ctx, i, 1);
2351 t = isl_poly_mul(c, t);
2352 poly = isl_poly_sum(poly, t);
2355 return poly;
2358 /* Remove common factor of non-constant terms and denominator.
2360 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2362 isl_ctx *ctx = qp->div->ctx;
2363 unsigned total = qp->div->n_col - 2;
2365 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2366 isl_int_gcd(ctx->normalize_gcd,
2367 ctx->normalize_gcd, qp->div->row[div][0]);
2368 if (isl_int_is_one(ctx->normalize_gcd))
2369 return;
2371 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2372 ctx->normalize_gcd, total);
2373 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2374 ctx->normalize_gcd);
2375 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2376 ctx->normalize_gcd);
2379 /* Replace the integer division identified by "div" by the polynomial "s".
2380 * The integer division is assumed not to appear in the definition
2381 * of any other integer divisions.
2383 static __isl_give isl_qpolynomial *substitute_div(
2384 __isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
2386 int i;
2387 isl_size div_pos;
2388 int *reordering;
2389 isl_ctx *ctx;
2391 if (!qp || !s)
2392 goto error;
2394 qp = isl_qpolynomial_cow(qp);
2395 if (!qp)
2396 goto error;
2398 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2399 if (div_pos < 0)
2400 goto error;
2401 qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
2402 if (!qp->poly)
2403 goto error;
2405 ctx = isl_qpolynomial_get_ctx(qp);
2406 reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row);
2407 if (!reordering)
2408 goto error;
2409 for (i = 0; i < div_pos + div; ++i)
2410 reordering[i] = i;
2411 for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
2412 reordering[i] = i - 1;
2413 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2414 qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
2415 qp->poly = reorder(qp->poly, reordering);
2416 free(reordering);
2418 if (!qp->poly || !qp->div)
2419 goto error;
2421 isl_poly_free(s);
2422 return qp;
2423 error:
2424 isl_qpolynomial_free(qp);
2425 isl_poly_free(s);
2426 return NULL;
2429 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2430 * divisions because d is equal to 1 by their definition, i.e., e.
2432 static __isl_give isl_qpolynomial *substitute_non_divs(
2433 __isl_take isl_qpolynomial *qp)
2435 int i, j;
2436 isl_size div_pos;
2437 isl_poly *s;
2439 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2440 if (div_pos < 0)
2441 return isl_qpolynomial_free(qp);
2443 for (i = 0; qp && i < qp->div->n_row; ++i) {
2444 if (!isl_int_is_one(qp->div->row[i][0]))
2445 continue;
2446 for (j = i + 1; j < qp->div->n_row; ++j) {
2447 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
2448 continue;
2449 isl_seq_combine(qp->div->row[j] + 1,
2450 qp->div->ctx->one, qp->div->row[j] + 1,
2451 qp->div->row[j][2 + div_pos + i],
2452 qp->div->row[i] + 1, 1 + div_pos + i);
2453 isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0);
2454 normalize_div(qp, j);
2456 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2457 qp->div->row[i][0], qp->div->n_col - 1);
2458 qp = substitute_div(qp, i, s);
2459 --i;
2462 return qp;
2465 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2466 * with d the denominator. When replacing the coefficient e of x by
2467 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2468 * inside the division, so we need to add floor(e/d) * x outside.
2469 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2470 * to adjust the coefficient of x in each later div that depends on the
2471 * current div "div" and also in the affine expressions in the rows of "mat"
2472 * (if they too depend on "div").
2474 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2475 __isl_keep isl_mat **mat)
2477 int i, j;
2478 isl_int v;
2479 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2481 isl_int_init(v);
2482 for (i = 0; i < 1 + total + div; ++i) {
2483 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2484 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2485 continue;
2486 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2487 isl_int_fdiv_r(qp->div->row[div][1 + i],
2488 qp->div->row[div][1 + i], qp->div->row[div][0]);
2489 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2490 for (j = div + 1; j < qp->div->n_row; ++j) {
2491 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2492 continue;
2493 isl_int_addmul(qp->div->row[j][1 + i],
2494 v, qp->div->row[j][2 + total + div]);
2497 isl_int_clear(v);
2500 /* Check if the last non-zero coefficient is bigger that half of the
2501 * denominator. If so, we will invert the div to further reduce the number
2502 * of distinct divs that may appear.
2503 * If the last non-zero coefficient is exactly half the denominator,
2504 * then we continue looking for earlier coefficients that are bigger
2505 * than half the denominator.
2507 static int needs_invert(__isl_keep isl_mat *div, int row)
2509 int i;
2510 int cmp;
2512 for (i = div->n_col - 1; i >= 1; --i) {
2513 if (isl_int_is_zero(div->row[row][i]))
2514 continue;
2515 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2516 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2517 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2518 if (cmp)
2519 return cmp > 0;
2520 if (i == 1)
2521 return 1;
2524 return 0;
2527 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2528 * We only invert the coefficients of e (and the coefficient of q in
2529 * later divs and in the rows of "mat"). After calling this function, the
2530 * coefficients of e should be reduced again.
2532 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2533 __isl_keep isl_mat **mat)
2535 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2537 isl_seq_neg(qp->div->row[div] + 1,
2538 qp->div->row[div] + 1, qp->div->n_col - 1);
2539 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2540 isl_int_add(qp->div->row[div][1],
2541 qp->div->row[div][1], qp->div->row[div][0]);
2542 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2543 isl_mat_col_mul(qp->div, 2 + total + div,
2544 qp->div->ctx->negone, 2 + total + div);
2547 /* Reduce all divs of "qp" to have coefficients
2548 * in the interval [0, d-1], with d the denominator and such that the
2549 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2550 * The modifications to the integer divisions need to be reflected
2551 * in the factors of the polynomial that refer to the original
2552 * integer divisions. To this end, the modifications are collected
2553 * as a set of affine expressions and then plugged into the polynomial.
2555 * After the reduction, some divs may have become redundant or identical,
2556 * so we call substitute_non_divs and sort_divs. If these functions
2557 * eliminate divs or merge two or more divs into one, the coefficients
2558 * of the enclosing divs may have to be reduced again, so we call
2559 * ourselves recursively if the number of divs decreases.
2561 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2563 int i;
2564 isl_ctx *ctx;
2565 isl_mat *mat;
2566 isl_poly **s;
2567 unsigned o_div;
2568 isl_size n_div, total, new_n_div;
2570 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2571 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2572 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2573 if (total < 0 || n_div < 0)
2574 return isl_qpolynomial_free(qp);
2575 ctx = isl_qpolynomial_get_ctx(qp);
2576 mat = isl_mat_zero(ctx, n_div, 1 + total);
2578 for (i = 0; i < n_div; ++i)
2579 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2581 for (i = 0; i < qp->div->n_row; ++i) {
2582 normalize_div(qp, i);
2583 reduce_div(qp, i, &mat);
2584 if (needs_invert(qp->div, i)) {
2585 invert_div(qp, i, &mat);
2586 reduce_div(qp, i, &mat);
2589 if (!mat)
2590 goto error;
2592 s = isl_alloc_array(ctx, struct isl_poly *, n_div);
2593 if (n_div && !s)
2594 goto error;
2595 for (i = 0; i < n_div; ++i)
2596 s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2597 1 + total);
2598 qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2599 for (i = 0; i < n_div; ++i)
2600 isl_poly_free(s[i]);
2601 free(s);
2602 if (!qp->poly)
2603 goto error;
2605 isl_mat_free(mat);
2607 qp = substitute_non_divs(qp);
2608 qp = sort_divs(qp);
2609 new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2610 if (new_n_div < 0)
2611 return isl_qpolynomial_free(qp);
2612 if (new_n_div < n_div)
2613 return reduce_divs(qp);
2615 return qp;
2616 error:
2617 isl_qpolynomial_free(qp);
2618 isl_mat_free(mat);
2619 return NULL;
2622 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2623 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2625 struct isl_qpolynomial *qp;
2626 isl_poly_cst *cst;
2628 qp = isl_qpolynomial_zero_on_domain(domain);
2629 if (!qp)
2630 return NULL;
2632 cst = isl_poly_as_cst(qp->poly);
2633 isl_int_set(cst->n, n);
2634 isl_int_set(cst->d, d);
2636 return qp;
2639 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2641 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2642 __isl_take isl_space *domain, __isl_take isl_val *val)
2644 isl_qpolynomial *qp;
2645 isl_poly_cst *cst;
2647 qp = isl_qpolynomial_zero_on_domain(domain);
2648 if (!qp || !val)
2649 goto error;
2651 cst = isl_poly_as_cst(qp->poly);
2652 isl_int_set(cst->n, val->n);
2653 isl_int_set(cst->d, val->d);
2655 isl_val_free(val);
2656 return qp;
2657 error:
2658 isl_val_free(val);
2659 isl_qpolynomial_free(qp);
2660 return NULL;
2663 static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
2665 isl_bool is_cst;
2666 isl_poly_rec *rec;
2667 int i;
2669 is_cst = isl_poly_is_cst(poly);
2670 if (is_cst < 0)
2671 return isl_stat_error;
2672 if (is_cst)
2673 return isl_stat_ok;
2675 if (poly->var < d)
2676 active[poly->var] = 1;
2678 rec = isl_poly_as_rec(poly);
2679 for (i = 0; i < rec->n; ++i)
2680 if (poly_set_active(rec->p[i], active, d) < 0)
2681 return isl_stat_error;
2683 return isl_stat_ok;
2686 static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
2688 int i, j;
2689 isl_size d;
2690 isl_space *space;
2692 space = isl_qpolynomial_peek_domain_space(qp);
2693 d = isl_space_dim(space, isl_dim_all);
2694 if (d < 0 || !active)
2695 return isl_stat_error;
2697 for (i = 0; i < d; ++i)
2698 for (j = 0; j < qp->div->n_row; ++j) {
2699 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2700 continue;
2701 active[i] = 1;
2702 break;
2705 return poly_set_active(qp->poly, active, d);
2708 #undef TYPE
2709 #define TYPE isl_qpolynomial
2710 static
2711 #include "check_type_range_templ.c"
2713 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2714 enum isl_dim_type type, unsigned first, unsigned n)
2716 int i;
2717 int *active = NULL;
2718 isl_bool involves = isl_bool_false;
2719 isl_size offset;
2720 isl_size d;
2721 isl_space *space;
2723 if (!qp)
2724 return isl_bool_error;
2725 if (n == 0)
2726 return isl_bool_false;
2728 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2729 return isl_bool_error;
2730 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2731 type == isl_dim_in, return isl_bool_error);
2733 space = isl_qpolynomial_peek_domain_space(qp);
2734 d = isl_space_dim(space, isl_dim_all);
2735 if (d < 0)
2736 return isl_bool_error;
2737 active = isl_calloc_array(qp->dim->ctx, int, d);
2738 if (set_active(qp, active) < 0)
2739 goto error;
2741 offset = isl_qpolynomial_domain_var_offset(qp, domain_type(type));
2742 if (offset < 0)
2743 goto error;
2744 first += offset;
2745 for (i = 0; i < n; ++i)
2746 if (active[first + i]) {
2747 involves = isl_bool_true;
2748 break;
2751 free(active);
2753 return involves;
2754 error:
2755 free(active);
2756 return isl_bool_error;
2759 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2760 * of the divs that do appear in the quasi-polynomial.
2762 static __isl_give isl_qpolynomial *remove_redundant_divs(
2763 __isl_take isl_qpolynomial *qp)
2765 int i, j;
2766 isl_size div_pos;
2767 int len;
2768 int skip;
2769 int *active = NULL;
2770 int *reordering = NULL;
2771 int redundant = 0;
2772 int n_div;
2773 isl_ctx *ctx;
2775 if (!qp)
2776 return NULL;
2777 if (qp->div->n_row == 0)
2778 return qp;
2780 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2781 if (div_pos < 0)
2782 return isl_qpolynomial_free(qp);
2783 len = qp->div->n_col - 2;
2784 ctx = isl_qpolynomial_get_ctx(qp);
2785 active = isl_calloc_array(ctx, int, len);
2786 if (!active)
2787 goto error;
2789 if (poly_set_active(qp->poly, active, len) < 0)
2790 goto error;
2792 for (i = qp->div->n_row - 1; i >= 0; --i) {
2793 if (!active[div_pos + i]) {
2794 redundant = 1;
2795 continue;
2797 for (j = 0; j < i; ++j) {
2798 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j]))
2799 continue;
2800 active[div_pos + j] = 1;
2801 break;
2805 if (!redundant) {
2806 free(active);
2807 return qp;
2810 reordering = isl_alloc_array(qp->div->ctx, int, len);
2811 if (!reordering)
2812 goto error;
2814 for (i = 0; i < div_pos; ++i)
2815 reordering[i] = i;
2817 skip = 0;
2818 n_div = qp->div->n_row;
2819 for (i = 0; i < n_div; ++i) {
2820 if (!active[div_pos + i]) {
2821 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2822 qp->div = isl_mat_drop_cols(qp->div,
2823 2 + div_pos + i - skip, 1);
2824 skip++;
2826 reordering[div_pos + i] = div_pos + i - skip;
2829 qp->poly = reorder(qp->poly, reordering);
2831 if (!qp->poly || !qp->div)
2832 goto error;
2834 free(active);
2835 free(reordering);
2837 return qp;
2838 error:
2839 free(active);
2840 free(reordering);
2841 isl_qpolynomial_free(qp);
2842 return NULL;
2845 __isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
2846 unsigned first, unsigned n)
2848 int i;
2849 isl_poly_rec *rec;
2851 if (!poly)
2852 return NULL;
2853 if (n == 0 || poly->var < 0 || poly->var < first)
2854 return poly;
2855 if (poly->var < first + n) {
2856 poly = replace_by_constant_term(poly);
2857 return isl_poly_drop(poly, first, n);
2859 poly = isl_poly_cow(poly);
2860 if (!poly)
2861 return NULL;
2862 poly->var -= n;
2863 rec = isl_poly_as_rec(poly);
2864 if (!rec)
2865 goto error;
2867 for (i = 0; i < rec->n; ++i) {
2868 rec->p[i] = isl_poly_drop(rec->p[i], first, n);
2869 if (!rec->p[i])
2870 goto error;
2873 return poly;
2874 error:
2875 isl_poly_free(poly);
2876 return NULL;
2879 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2880 __isl_take isl_qpolynomial *qp,
2881 enum isl_dim_type type, unsigned pos, const char *s)
2883 qp = isl_qpolynomial_cow(qp);
2884 if (!qp)
2885 return NULL;
2886 if (type == isl_dim_out)
2887 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2888 "cannot set name of output/set dimension",
2889 return isl_qpolynomial_free(qp));
2890 type = domain_type(type);
2891 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2892 if (!qp->dim)
2893 goto error;
2894 return qp;
2895 error:
2896 isl_qpolynomial_free(qp);
2897 return NULL;
2900 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2901 __isl_take isl_qpolynomial *qp,
2902 enum isl_dim_type type, unsigned first, unsigned n)
2904 isl_size offset;
2906 if (!qp)
2907 return NULL;
2908 if (type == isl_dim_out)
2909 isl_die(qp->dim->ctx, isl_error_invalid,
2910 "cannot drop output/set dimension",
2911 goto error);
2912 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2913 return isl_qpolynomial_free(qp);
2914 type = domain_type(type);
2915 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2916 return qp;
2918 qp = isl_qpolynomial_cow(qp);
2919 if (!qp)
2920 return NULL;
2922 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2923 type == isl_dim_set, goto error);
2925 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2926 if (!qp->dim)
2927 goto error;
2929 offset = isl_qpolynomial_domain_var_offset(qp, type);
2930 if (offset < 0)
2931 goto error;
2932 first += offset;
2934 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2935 if (!qp->div)
2936 goto error;
2938 qp->poly = isl_poly_drop(qp->poly, first, n);
2939 if (!qp->poly)
2940 goto error;
2942 return qp;
2943 error:
2944 isl_qpolynomial_free(qp);
2945 return NULL;
2948 /* Project the domain of the quasi-polynomial onto its parameter space.
2949 * The quasi-polynomial may not involve any of the domain dimensions.
2951 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2952 __isl_take isl_qpolynomial *qp)
2954 isl_space *space;
2955 isl_size n;
2956 isl_bool involves;
2958 n = isl_qpolynomial_dim(qp, isl_dim_in);
2959 if (n < 0)
2960 return isl_qpolynomial_free(qp);
2961 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2962 if (involves < 0)
2963 return isl_qpolynomial_free(qp);
2964 if (involves)
2965 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2966 "polynomial involves some of the domain dimensions",
2967 return isl_qpolynomial_free(qp));
2968 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2969 space = isl_qpolynomial_get_domain_space(qp);
2970 space = isl_space_params(space);
2971 qp = isl_qpolynomial_reset_domain_space(qp, space);
2972 return qp;
2975 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2976 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2978 int i, j, k;
2979 isl_int denom;
2980 unsigned total;
2981 unsigned n_div;
2982 isl_poly *poly;
2984 if (!eq)
2985 goto error;
2986 if (eq->n_eq == 0) {
2987 isl_basic_set_free(eq);
2988 return qp;
2991 qp = isl_qpolynomial_cow(qp);
2992 if (!qp)
2993 goto error;
2994 qp->div = isl_mat_cow(qp->div);
2995 if (!qp->div)
2996 goto error;
2998 total = isl_basic_set_offset(eq, isl_dim_div);
2999 n_div = eq->n_div;
3000 isl_int_init(denom);
3001 for (i = 0; i < eq->n_eq; ++i) {
3002 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
3003 if (j < 0 || j == 0 || j >= total)
3004 continue;
3006 for (k = 0; k < qp->div->n_row; ++k) {
3007 if (isl_int_is_zero(qp->div->row[k][1 + j]))
3008 continue;
3009 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
3010 &qp->div->row[k][0]);
3011 normalize_div(qp, k);
3014 if (isl_int_is_pos(eq->eq[i][j]))
3015 isl_seq_neg(eq->eq[i], eq->eq[i], total);
3016 isl_int_abs(denom, eq->eq[i][j]);
3017 isl_int_set_si(eq->eq[i][j], 0);
3019 poly = isl_poly_from_affine(qp->dim->ctx,
3020 eq->eq[i], denom, total);
3021 qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
3022 isl_poly_free(poly);
3024 isl_int_clear(denom);
3026 if (!qp->poly)
3027 goto error;
3029 isl_basic_set_free(eq);
3031 qp = substitute_non_divs(qp);
3032 qp = sort_divs(qp);
3034 return qp;
3035 error:
3036 isl_basic_set_free(eq);
3037 isl_qpolynomial_free(qp);
3038 return NULL;
3041 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3043 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
3044 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
3046 if (!qp || !eq)
3047 goto error;
3048 if (qp->div->n_row > 0)
3049 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
3050 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
3051 error:
3052 isl_basic_set_free(eq);
3053 isl_qpolynomial_free(qp);
3054 return NULL;
3057 /* Look for equalities among the variables shared by context and qp
3058 * and the integer divisions of qp, if any.
3059 * The equalities are then used to eliminate variables and/or integer
3060 * divisions from qp.
3062 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
3063 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3065 isl_local_space *ls;
3066 isl_basic_set *aff;
3068 ls = isl_qpolynomial_get_domain_local_space(qp);
3069 context = isl_local_space_lift_set(ls, context);
3071 aff = isl_set_affine_hull(context);
3072 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
3075 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
3076 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3078 isl_space *space = isl_qpolynomial_get_domain_space(qp);
3079 isl_set *dom_context = isl_set_universe(space);
3080 dom_context = isl_set_intersect_params(dom_context, context);
3081 return isl_qpolynomial_gist(qp, dom_context);
3084 /* Return a zero isl_qpolynomial in the given space.
3086 * This is a helper function for isl_pw_*_as_* that ensures a uniform
3087 * interface over all piecewise types.
3089 static __isl_give isl_qpolynomial *isl_qpolynomial_zero_in_space(
3090 __isl_take isl_space *space)
3092 return isl_qpolynomial_zero_on_domain(isl_space_domain(space));
3095 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3097 #undef PW
3098 #define PW isl_pw_qpolynomial
3099 #undef BASE
3100 #define BASE qpolynomial
3101 #undef EL_IS_ZERO
3102 #define EL_IS_ZERO is_zero
3103 #undef ZERO
3104 #define ZERO zero
3105 #undef IS_ZERO
3106 #define IS_ZERO is_zero
3107 #undef FIELD
3108 #define FIELD qp
3109 #undef DEFAULT_IS_ZERO
3110 #define DEFAULT_IS_ZERO 1
3112 #include <isl_pw_templ.c>
3113 #include <isl_pw_un_op_templ.c>
3114 #include <isl_pw_add_disjoint_templ.c>
3115 #include <isl_pw_eval.c>
3116 #include <isl_pw_fix_templ.c>
3117 #include <isl_pw_from_range_templ.c>
3118 #include <isl_pw_insert_dims_templ.c>
3119 #include <isl_pw_lift_templ.c>
3120 #include <isl_pw_morph_templ.c>
3121 #include <isl_pw_move_dims_templ.c>
3122 #include <isl_pw_neg_templ.c>
3123 #include <isl_pw_opt_templ.c>
3124 #include <isl_pw_split_dims_templ.c>
3125 #include <isl_pw_sub_templ.c>
3127 #undef BASE
3128 #define BASE pw_qpolynomial
3130 #include <isl_union_single.c>
3131 #include <isl_union_eval.c>
3132 #include <isl_union_neg.c>
3133 #include <isl_union_sub_templ.c>
3135 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
3137 if (!pwqp)
3138 return -1;
3140 if (pwqp->n != -1)
3141 return 0;
3143 if (!isl_set_plain_is_universe(pwqp->p[0].set))
3144 return 0;
3146 return isl_qpolynomial_is_one(pwqp->p[0].qp);
3149 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
3150 __isl_take isl_pw_qpolynomial *pwqp1,
3151 __isl_take isl_pw_qpolynomial *pwqp2)
3153 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3156 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
3157 __isl_take isl_pw_qpolynomial *pwqp1,
3158 __isl_take isl_pw_qpolynomial *pwqp2)
3160 int i, j, n;
3161 struct isl_pw_qpolynomial *res;
3163 if (!pwqp1 || !pwqp2)
3164 goto error;
3166 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3167 goto error);
3169 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3170 isl_pw_qpolynomial_free(pwqp2);
3171 return pwqp1;
3174 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3175 isl_pw_qpolynomial_free(pwqp1);
3176 return pwqp2;
3179 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3180 isl_pw_qpolynomial_free(pwqp1);
3181 return pwqp2;
3184 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3185 isl_pw_qpolynomial_free(pwqp2);
3186 return pwqp1;
3189 n = pwqp1->n * pwqp2->n;
3190 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3192 for (i = 0; i < pwqp1->n; ++i) {
3193 for (j = 0; j < pwqp2->n; ++j) {
3194 struct isl_set *common;
3195 struct isl_qpolynomial *prod;
3196 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3197 isl_set_copy(pwqp2->p[j].set));
3198 if (isl_set_plain_is_empty(common)) {
3199 isl_set_free(common);
3200 continue;
3203 prod = isl_qpolynomial_mul(
3204 isl_qpolynomial_copy(pwqp1->p[i].qp),
3205 isl_qpolynomial_copy(pwqp2->p[j].qp));
3207 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3211 isl_pw_qpolynomial_free(pwqp1);
3212 isl_pw_qpolynomial_free(pwqp2);
3214 return res;
3215 error:
3216 isl_pw_qpolynomial_free(pwqp1);
3217 isl_pw_qpolynomial_free(pwqp2);
3218 return NULL;
3221 __isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
3222 __isl_take isl_vec *vec)
3224 int i;
3225 isl_bool is_cst;
3226 isl_poly_rec *rec;
3227 isl_val *res;
3228 isl_val *base;
3230 is_cst = isl_poly_is_cst(poly);
3231 if (is_cst < 0)
3232 goto error;
3233 if (is_cst) {
3234 isl_vec_free(vec);
3235 res = isl_poly_get_constant_val(poly);
3236 isl_poly_free(poly);
3237 return res;
3240 rec = isl_poly_as_rec(poly);
3241 if (!rec || !vec)
3242 goto error;
3244 isl_assert(poly->ctx, rec->n >= 1, goto error);
3246 base = isl_val_rat_from_isl_int(poly->ctx,
3247 vec->el[1 + poly->var], vec->el[0]);
3249 res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3250 isl_vec_copy(vec));
3252 for (i = rec->n - 2; i >= 0; --i) {
3253 res = isl_val_mul(res, isl_val_copy(base));
3254 res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
3255 isl_vec_copy(vec)));
3258 isl_val_free(base);
3259 isl_poly_free(poly);
3260 isl_vec_free(vec);
3261 return res;
3262 error:
3263 isl_poly_free(poly);
3264 isl_vec_free(vec);
3265 return NULL;
3268 /* Evaluate "qp" in the void point "pnt".
3269 * In particular, return the value NaN.
3271 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3272 __isl_take isl_point *pnt)
3274 isl_ctx *ctx;
3276 ctx = isl_point_get_ctx(pnt);
3277 isl_qpolynomial_free(qp);
3278 isl_point_free(pnt);
3279 return isl_val_nan(ctx);
3282 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3283 __isl_take isl_point *pnt)
3285 isl_bool is_void;
3286 isl_vec *ext;
3287 isl_val *v;
3289 if (!qp || !pnt)
3290 goto error;
3291 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3292 is_void = isl_point_is_void(pnt);
3293 if (is_void < 0)
3294 goto error;
3295 if (is_void)
3296 return eval_void(qp, pnt);
3298 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3300 v = isl_poly_eval(isl_poly_copy(qp->poly), ext);
3302 isl_qpolynomial_free(qp);
3303 isl_point_free(pnt);
3305 return v;
3306 error:
3307 isl_qpolynomial_free(qp);
3308 isl_point_free(pnt);
3309 return NULL;
3312 int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
3314 int cmp;
3315 isl_int t;
3316 isl_int_init(t);
3317 isl_int_mul(t, cst1->n, cst2->d);
3318 isl_int_submul(t, cst2->n, cst1->d);
3319 cmp = isl_int_sgn(t);
3320 isl_int_clear(t);
3321 return cmp;
3324 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3325 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3326 unsigned first, unsigned n)
3328 unsigned total;
3329 unsigned g_pos;
3330 int *exp;
3332 if (!qp)
3333 return NULL;
3334 if (type == isl_dim_out)
3335 isl_die(qp->div->ctx, isl_error_invalid,
3336 "cannot insert output/set dimensions",
3337 goto error);
3338 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3339 return isl_qpolynomial_free(qp);
3340 type = domain_type(type);
3341 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3342 return qp;
3344 qp = isl_qpolynomial_cow(qp);
3345 if (!qp)
3346 return NULL;
3348 g_pos = pos(qp->dim, type) + first;
3350 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3351 if (!qp->div)
3352 goto error;
3354 total = qp->div->n_col - 2;
3355 if (total > g_pos) {
3356 int i;
3357 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3358 if (!exp)
3359 goto error;
3360 for (i = 0; i < total - g_pos; ++i)
3361 exp[i] = i + n;
3362 qp->poly = expand(qp->poly, exp, g_pos);
3363 free(exp);
3364 if (!qp->poly)
3365 goto error;
3368 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3369 if (!qp->dim)
3370 goto error;
3372 return qp;
3373 error:
3374 isl_qpolynomial_free(qp);
3375 return NULL;
3378 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3379 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3381 isl_size pos;
3383 pos = isl_qpolynomial_dim(qp, type);
3384 if (pos < 0)
3385 return isl_qpolynomial_free(qp);
3387 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3390 static int *reordering_move(isl_ctx *ctx,
3391 unsigned len, unsigned dst, unsigned src, unsigned n)
3393 int i;
3394 int *reordering;
3396 reordering = isl_alloc_array(ctx, int, len);
3397 if (!reordering)
3398 return NULL;
3400 if (dst <= src) {
3401 for (i = 0; i < dst; ++i)
3402 reordering[i] = i;
3403 for (i = 0; i < n; ++i)
3404 reordering[src + i] = dst + i;
3405 for (i = 0; i < src - dst; ++i)
3406 reordering[dst + i] = dst + n + i;
3407 for (i = 0; i < len - src - n; ++i)
3408 reordering[src + n + i] = src + n + i;
3409 } else {
3410 for (i = 0; i < src; ++i)
3411 reordering[i] = i;
3412 for (i = 0; i < n; ++i)
3413 reordering[src + i] = dst + i;
3414 for (i = 0; i < dst - src; ++i)
3415 reordering[src + n + i] = src + i;
3416 for (i = 0; i < len - dst - n; ++i)
3417 reordering[dst + n + i] = dst + n + i;
3420 return reordering;
3423 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3424 __isl_take isl_qpolynomial *qp,
3425 enum isl_dim_type dst_type, unsigned dst_pos,
3426 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3428 isl_ctx *ctx;
3429 unsigned g_dst_pos;
3430 unsigned g_src_pos;
3431 isl_size total;
3432 int *reordering;
3434 if (!qp)
3435 return NULL;
3437 ctx = isl_qpolynomial_get_ctx(qp);
3438 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3439 isl_die(ctx, isl_error_invalid,
3440 "cannot move output/set dimension",
3441 goto error);
3442 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3443 return isl_qpolynomial_free(qp);
3444 if (dst_type == isl_dim_in)
3445 dst_type = isl_dim_set;
3446 if (src_type == isl_dim_in)
3447 src_type = isl_dim_set;
3449 if (n == 0 &&
3450 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3451 !isl_space_is_named_or_nested(qp->dim, dst_type))
3452 return qp;
3454 qp = isl_qpolynomial_cow(qp);
3455 if (!qp)
3456 return NULL;
3458 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3459 g_src_pos = pos(qp->dim, src_type) + src_pos;
3460 if (dst_type > src_type)
3461 g_dst_pos -= n;
3463 qp->div = isl_local_move_vars(qp->div, g_dst_pos, g_src_pos, n);
3464 if (!qp->div)
3465 goto error;
3466 qp = sort_divs(qp);
3468 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
3469 if (total < 0)
3470 return isl_qpolynomial_free(qp);
3471 reordering = reordering_move(ctx, total, g_dst_pos, g_src_pos, n);
3472 if (!reordering)
3473 goto error;
3475 qp->poly = reorder(qp->poly, reordering);
3476 free(reordering);
3477 if (!qp->poly)
3478 goto error;
3480 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3481 if (!qp->dim)
3482 goto error;
3484 return qp;
3485 error:
3486 isl_qpolynomial_free(qp);
3487 return NULL;
3490 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3491 __isl_take isl_space *space, isl_int *f, isl_int denom)
3493 isl_size d;
3494 isl_poly *poly;
3496 space = isl_space_domain(space);
3497 if (!space)
3498 return NULL;
3500 d = isl_space_dim(space, isl_dim_all);
3501 poly = d < 0 ? NULL : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
3503 return isl_qpolynomial_alloc(space, 0, poly);
3506 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3508 isl_ctx *ctx;
3509 isl_poly *poly;
3510 isl_qpolynomial *qp;
3512 if (!aff)
3513 return NULL;
3515 ctx = isl_aff_get_ctx(aff);
3516 poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3517 aff->v->size - 1);
3519 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3520 aff->ls->div->n_row, poly);
3521 if (!qp)
3522 goto error;
3524 isl_mat_free(qp->div);
3525 qp->div = isl_mat_copy(aff->ls->div);
3526 qp->div = isl_mat_cow(qp->div);
3527 if (!qp->div)
3528 goto error;
3530 isl_aff_free(aff);
3531 qp = reduce_divs(qp);
3532 qp = remove_redundant_divs(qp);
3533 return qp;
3534 error:
3535 isl_aff_free(aff);
3536 return isl_qpolynomial_free(qp);
3539 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3540 __isl_take isl_pw_aff *pwaff)
3542 int i;
3543 isl_pw_qpolynomial *pwqp;
3545 if (!pwaff)
3546 return NULL;
3548 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3549 pwaff->n);
3551 for (i = 0; i < pwaff->n; ++i) {
3552 isl_set *dom;
3553 isl_qpolynomial *qp;
3555 dom = isl_set_copy(pwaff->p[i].set);
3556 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3557 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3560 isl_pw_aff_free(pwaff);
3561 return pwqp;
3564 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3565 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3567 isl_aff *aff;
3569 aff = isl_constraint_get_bound(c, type, pos);
3570 isl_constraint_free(c);
3571 return isl_qpolynomial_from_aff(aff);
3574 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3575 * in "qp" by subs[i].
3577 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3578 __isl_take isl_qpolynomial *qp,
3579 enum isl_dim_type type, unsigned first, unsigned n,
3580 __isl_keep isl_qpolynomial **subs)
3582 int i;
3583 isl_poly **polys;
3585 if (n == 0)
3586 return qp;
3588 qp = isl_qpolynomial_cow(qp);
3589 if (!qp)
3590 return NULL;
3592 if (type == isl_dim_out)
3593 isl_die(qp->dim->ctx, isl_error_invalid,
3594 "cannot substitute output/set dimension",
3595 goto error);
3596 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3597 return isl_qpolynomial_free(qp);
3598 type = domain_type(type);
3600 for (i = 0; i < n; ++i)
3601 if (!subs[i])
3602 goto error;
3604 for (i = 0; i < n; ++i)
3605 if (isl_qpolynomial_check_equal_space(qp, subs[i]) < 0)
3606 goto error;
3608 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3609 for (i = 0; i < n; ++i)
3610 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3612 first += pos(qp->dim, type);
3614 polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
3615 if (!polys)
3616 goto error;
3617 for (i = 0; i < n; ++i)
3618 polys[i] = subs[i]->poly;
3620 qp->poly = isl_poly_subs(qp->poly, first, n, polys);
3622 free(polys);
3624 if (!qp->poly)
3625 goto error;
3627 return qp;
3628 error:
3629 isl_qpolynomial_free(qp);
3630 return NULL;
3633 /* Extend "bset" with extra set dimensions for each integer division
3634 * in "qp" and then call "fn" with the extended bset and the polynomial
3635 * that results from replacing each of the integer divisions by the
3636 * corresponding extra set dimension.
3638 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3639 __isl_keep isl_basic_set *bset,
3640 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3641 __isl_take isl_qpolynomial *poly, void *user), void *user)
3643 isl_space *space;
3644 isl_local_space *ls;
3645 isl_qpolynomial *poly;
3647 if (!qp || !bset)
3648 return isl_stat_error;
3649 if (qp->div->n_row == 0)
3650 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3651 user);
3653 space = isl_space_copy(qp->dim);
3654 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3655 poly = isl_qpolynomial_alloc(space, 0, isl_poly_copy(qp->poly));
3656 bset = isl_basic_set_copy(bset);
3657 ls = isl_qpolynomial_get_domain_local_space(qp);
3658 bset = isl_local_space_lift_basic_set(ls, bset);
3660 return fn(bset, poly, user);
3663 /* Return total degree in variables first (inclusive) up to last (exclusive).
3665 int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3667 int deg = -1;
3668 int i;
3669 isl_bool is_zero, is_cst;
3670 isl_poly_rec *rec;
3672 is_zero = isl_poly_is_zero(poly);
3673 if (is_zero < 0)
3674 return -2;
3675 if (is_zero)
3676 return -1;
3677 is_cst = isl_poly_is_cst(poly);
3678 if (is_cst < 0)
3679 return -2;
3680 if (is_cst || poly->var < first)
3681 return 0;
3683 rec = isl_poly_as_rec(poly);
3684 if (!rec)
3685 return -2;
3687 for (i = 0; i < rec->n; ++i) {
3688 int d;
3690 is_zero = isl_poly_is_zero(rec->p[i]);
3691 if (is_zero < 0)
3692 return -2;
3693 if (is_zero)
3694 continue;
3695 d = isl_poly_degree(rec->p[i], first, last);
3696 if (poly->var < last)
3697 d += i;
3698 if (d > deg)
3699 deg = d;
3702 return deg;
3705 /* Return total degree in set variables.
3707 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3709 isl_size ovar;
3710 isl_size nvar;
3712 if (!poly)
3713 return -2;
3715 ovar = isl_space_offset(poly->dim, isl_dim_set);
3716 nvar = isl_space_dim(poly->dim, isl_dim_set);
3717 if (ovar < 0 || nvar < 0)
3718 return -2;
3719 return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3722 __isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
3723 unsigned pos, int deg)
3725 int i;
3726 isl_bool is_cst;
3727 isl_poly_rec *rec;
3729 is_cst = isl_poly_is_cst(poly);
3730 if (is_cst < 0)
3731 return NULL;
3732 if (is_cst || poly->var < pos) {
3733 if (deg == 0)
3734 return isl_poly_copy(poly);
3735 else
3736 return isl_poly_zero(poly->ctx);
3739 rec = isl_poly_as_rec(poly);
3740 if (!rec)
3741 return NULL;
3743 if (poly->var == pos) {
3744 if (deg < rec->n)
3745 return isl_poly_copy(rec->p[deg]);
3746 else
3747 return isl_poly_zero(poly->ctx);
3750 poly = isl_poly_copy(poly);
3751 poly = isl_poly_cow(poly);
3752 rec = isl_poly_as_rec(poly);
3753 if (!rec)
3754 goto error;
3756 for (i = 0; i < rec->n; ++i) {
3757 isl_poly *t;
3758 t = isl_poly_coeff(rec->p[i], pos, deg);
3759 if (!t)
3760 goto error;
3761 isl_poly_free(rec->p[i]);
3762 rec->p[i] = t;
3765 return poly;
3766 error:
3767 isl_poly_free(poly);
3768 return NULL;
3771 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3773 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3774 __isl_keep isl_qpolynomial *qp,
3775 enum isl_dim_type type, unsigned t_pos, int deg)
3777 unsigned g_pos;
3778 isl_poly *poly;
3779 isl_qpolynomial *c;
3781 if (!qp)
3782 return NULL;
3784 if (type == isl_dim_out)
3785 isl_die(qp->div->ctx, isl_error_invalid,
3786 "output/set dimension does not have a coefficient",
3787 return NULL);
3788 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
3789 return NULL;
3790 type = domain_type(type);
3792 g_pos = pos(qp->dim, type) + t_pos;
3793 poly = isl_poly_coeff(qp->poly, g_pos, deg);
3795 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
3796 qp->div->n_row, poly);
3797 if (!c)
3798 return NULL;
3799 isl_mat_free(c->div);
3800 c->div = isl_mat_copy(qp->div);
3801 if (!c->div)
3802 goto error;
3803 return c;
3804 error:
3805 isl_qpolynomial_free(c);
3806 return NULL;
3809 /* Homogenize the polynomial in the variables first (inclusive) up to
3810 * last (exclusive) by inserting powers of variable first.
3811 * Variable first is assumed not to appear in the input.
3813 __isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
3814 int target, int first, int last)
3816 int i;
3817 isl_bool is_zero, is_cst;
3818 isl_poly_rec *rec;
3820 is_zero = isl_poly_is_zero(poly);
3821 if (is_zero < 0)
3822 return isl_poly_free(poly);
3823 if (is_zero)
3824 return poly;
3825 if (deg == target)
3826 return poly;
3827 is_cst = isl_poly_is_cst(poly);
3828 if (is_cst < 0)
3829 return isl_poly_free(poly);
3830 if (is_cst || poly->var < first) {
3831 isl_poly *hom;
3833 hom = isl_poly_var_pow(poly->ctx, first, target - deg);
3834 if (!hom)
3835 goto error;
3836 rec = isl_poly_as_rec(hom);
3837 rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
3839 return hom;
3842 poly = isl_poly_cow(poly);
3843 rec = isl_poly_as_rec(poly);
3844 if (!rec)
3845 goto error;
3847 for (i = 0; i < rec->n; ++i) {
3848 is_zero = isl_poly_is_zero(rec->p[i]);
3849 if (is_zero < 0)
3850 return isl_poly_free(poly);
3851 if (is_zero)
3852 continue;
3853 rec->p[i] = isl_poly_homogenize(rec->p[i],
3854 poly->var < last ? deg + i : i, target,
3855 first, last);
3856 if (!rec->p[i])
3857 goto error;
3860 return poly;
3861 error:
3862 isl_poly_free(poly);
3863 return NULL;
3866 /* Homogenize the polynomial in the set variables by introducing
3867 * powers of an extra set variable at position 0.
3869 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3870 __isl_take isl_qpolynomial *poly)
3872 isl_size ovar;
3873 isl_size nvar;
3874 int deg = isl_qpolynomial_degree(poly);
3876 if (deg < -1)
3877 goto error;
3879 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3880 poly = isl_qpolynomial_cow(poly);
3881 if (!poly)
3882 goto error;
3884 ovar = isl_space_offset(poly->dim, isl_dim_set);
3885 nvar = isl_space_dim(poly->dim, isl_dim_set);
3886 if (ovar < 0 || nvar < 0)
3887 return isl_qpolynomial_free(poly);
3888 poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
3889 if (!poly->poly)
3890 goto error;
3892 return poly;
3893 error:
3894 isl_qpolynomial_free(poly);
3895 return NULL;
3898 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
3899 __isl_take isl_mat *div)
3901 isl_term *term;
3902 isl_size d;
3903 int n;
3905 d = isl_space_dim(space, isl_dim_all);
3906 if (d < 0 || !div)
3907 goto error;
3909 n = d + div->n_row;
3911 term = isl_calloc(space->ctx, struct isl_term,
3912 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3913 if (!term)
3914 goto error;
3916 term->ref = 1;
3917 term->dim = space;
3918 term->div = div;
3919 isl_int_init(term->n);
3920 isl_int_init(term->d);
3922 return term;
3923 error:
3924 isl_space_free(space);
3925 isl_mat_free(div);
3926 return NULL;
3929 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3931 if (!term)
3932 return NULL;
3934 term->ref++;
3935 return term;
3938 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3940 int i;
3941 isl_term *dup;
3942 isl_size total;
3944 total = isl_term_dim(term, isl_dim_all);
3945 if (total < 0)
3946 return NULL;
3948 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3949 if (!dup)
3950 return NULL;
3952 isl_int_set(dup->n, term->n);
3953 isl_int_set(dup->d, term->d);
3955 for (i = 0; i < total; ++i)
3956 dup->pow[i] = term->pow[i];
3958 return dup;
3961 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3963 if (!term)
3964 return NULL;
3966 if (term->ref == 1)
3967 return term;
3968 term->ref--;
3969 return isl_term_dup(term);
3972 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3974 if (!term)
3975 return NULL;
3977 if (--term->ref > 0)
3978 return NULL;
3980 isl_space_free(term->dim);
3981 isl_mat_free(term->div);
3982 isl_int_clear(term->n);
3983 isl_int_clear(term->d);
3984 free(term);
3986 return NULL;
3989 isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3991 isl_size dim;
3993 if (!term)
3994 return isl_size_error;
3996 switch (type) {
3997 case isl_dim_param:
3998 case isl_dim_in:
3999 case isl_dim_out: return isl_space_dim(term->dim, type);
4000 case isl_dim_div: return term->div->n_row;
4001 case isl_dim_all: dim = isl_space_dim(term->dim, isl_dim_all);
4002 if (dim < 0)
4003 return isl_size_error;
4004 return dim + term->div->n_row;
4005 default: return isl_size_error;
4009 /* Return the space of "term".
4011 static __isl_keep isl_space *isl_term_peek_space(__isl_keep isl_term *term)
4013 return term ? term->dim : NULL;
4016 /* Return the offset of the first variable of type "type" within
4017 * the variables of "term".
4019 static isl_size isl_term_offset(__isl_keep isl_term *term,
4020 enum isl_dim_type type)
4022 isl_space *space;
4024 space = isl_term_peek_space(term);
4025 if (!space)
4026 return isl_size_error;
4028 switch (type) {
4029 case isl_dim_param:
4030 case isl_dim_set: return isl_space_offset(space, type);
4031 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
4032 default:
4033 isl_die(isl_term_get_ctx(term), isl_error_invalid,
4034 "invalid dimension type", return isl_size_error);
4038 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
4040 return term ? term->dim->ctx : NULL;
4043 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
4045 if (!term)
4046 return;
4047 isl_int_set(*n, term->n);
4050 /* Return the coefficient of the term "term".
4052 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
4054 if (!term)
4055 return NULL;
4057 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
4058 term->n, term->d);
4061 #undef TYPE
4062 #define TYPE isl_term
4063 static
4064 #include "check_type_range_templ.c"
4066 isl_size isl_term_get_exp(__isl_keep isl_term *term,
4067 enum isl_dim_type type, unsigned pos)
4069 isl_size offset;
4071 if (isl_term_check_range(term, type, pos, 1) < 0)
4072 return isl_size_error;
4073 offset = isl_term_offset(term, type);
4074 if (offset < 0)
4075 return isl_size_error;
4077 return term->pow[offset + pos];
4080 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
4082 isl_local_space *ls;
4083 isl_aff *aff;
4085 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
4086 return NULL;
4088 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
4089 isl_mat_copy(term->div));
4090 aff = isl_aff_alloc(ls);
4091 if (!aff)
4092 return NULL;
4094 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
4096 aff = isl_aff_normalize(aff);
4098 return aff;
4101 __isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
4102 isl_stat (*fn)(__isl_take isl_term *term, void *user),
4103 __isl_take isl_term *term, void *user)
4105 int i;
4106 isl_bool is_zero, is_bad, is_cst;
4107 isl_poly_rec *rec;
4109 is_zero = isl_poly_is_zero(poly);
4110 if (is_zero < 0 || !term)
4111 goto error;
4113 if (is_zero)
4114 return term;
4116 is_cst = isl_poly_is_cst(poly);
4117 is_bad = isl_poly_is_nan(poly);
4118 if (is_bad >= 0 && !is_bad)
4119 is_bad = isl_poly_is_infty(poly);
4120 if (is_bad >= 0 && !is_bad)
4121 is_bad = isl_poly_is_neginfty(poly);
4122 if (is_cst < 0 || is_bad < 0)
4123 return isl_term_free(term);
4124 if (is_bad)
4125 isl_die(isl_term_get_ctx(term), isl_error_invalid,
4126 "cannot handle NaN/infty polynomial",
4127 return isl_term_free(term));
4129 if (is_cst) {
4130 isl_poly_cst *cst;
4131 cst = isl_poly_as_cst(poly);
4132 if (!cst)
4133 goto error;
4134 term = isl_term_cow(term);
4135 if (!term)
4136 goto error;
4137 isl_int_set(term->n, cst->n);
4138 isl_int_set(term->d, cst->d);
4139 if (fn(isl_term_copy(term), user) < 0)
4140 goto error;
4141 return term;
4144 rec = isl_poly_as_rec(poly);
4145 if (!rec)
4146 goto error;
4148 for (i = 0; i < rec->n; ++i) {
4149 term = isl_term_cow(term);
4150 if (!term)
4151 goto error;
4152 term->pow[poly->var] = i;
4153 term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4154 if (!term)
4155 goto error;
4157 term = isl_term_cow(term);
4158 if (!term)
4159 return NULL;
4160 term->pow[poly->var] = 0;
4162 return term;
4163 error:
4164 isl_term_free(term);
4165 return NULL;
4168 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
4169 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4171 isl_term *term;
4173 if (!qp)
4174 return isl_stat_error;
4176 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
4177 if (!term)
4178 return isl_stat_error;
4180 term = isl_poly_foreach_term(qp->poly, fn, term, user);
4182 isl_term_free(term);
4184 return term ? isl_stat_ok : isl_stat_error;
4187 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
4189 isl_poly *poly;
4190 isl_qpolynomial *qp;
4191 int i;
4192 isl_size n;
4194 n = isl_term_dim(term, isl_dim_all);
4195 if (n < 0)
4196 term = isl_term_free(term);
4197 if (!term)
4198 return NULL;
4200 poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4201 for (i = 0; i < n; ++i) {
4202 if (!term->pow[i])
4203 continue;
4204 poly = isl_poly_mul(poly,
4205 isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4208 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4209 term->div->n_row, poly);
4210 if (!qp)
4211 goto error;
4212 isl_mat_free(qp->div);
4213 qp->div = isl_mat_copy(term->div);
4214 if (!qp->div)
4215 goto error;
4217 isl_term_free(term);
4218 return qp;
4219 error:
4220 isl_qpolynomial_free(qp);
4221 isl_term_free(term);
4222 return NULL;
4225 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4226 __isl_take isl_space *space)
4228 int i;
4229 int extra;
4230 isl_size total, d_set, d_qp;
4232 if (!qp || !space)
4233 goto error;
4235 if (isl_space_is_equal(qp->dim, space)) {
4236 isl_space_free(space);
4237 return qp;
4240 qp = isl_qpolynomial_cow(qp);
4241 if (!qp)
4242 goto error;
4244 d_set = isl_space_dim(space, isl_dim_set);
4245 d_qp = isl_qpolynomial_domain_dim(qp, isl_dim_set);
4246 extra = d_set - d_qp;
4247 total = isl_space_dim(qp->dim, isl_dim_all);
4248 if (d_set < 0 || d_qp < 0 || total < 0)
4249 goto error;
4250 if (qp->div->n_row) {
4251 int *exp;
4253 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4254 if (!exp)
4255 goto error;
4256 for (i = 0; i < qp->div->n_row; ++i)
4257 exp[i] = extra + i;
4258 qp->poly = expand(qp->poly, exp, total);
4259 free(exp);
4260 if (!qp->poly)
4261 goto error;
4263 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4264 if (!qp->div)
4265 goto error;
4266 for (i = 0; i < qp->div->n_row; ++i)
4267 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4269 isl_space_free(qp->dim);
4270 qp->dim = space;
4272 return qp;
4273 error:
4274 isl_space_free(space);
4275 isl_qpolynomial_free(qp);
4276 return NULL;
4279 /* For each parameter or variable that does not appear in qp,
4280 * first eliminate the variable from all constraints and then set it to zero.
4282 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4283 __isl_keep isl_qpolynomial *qp)
4285 int *active = NULL;
4286 int i;
4287 isl_size d;
4288 isl_size nparam;
4289 isl_size nvar;
4291 d = isl_set_dim(set, isl_dim_all);
4292 if (d < 0 || !qp)
4293 goto error;
4295 active = isl_calloc_array(set->ctx, int, d);
4296 if (set_active(qp, active) < 0)
4297 goto error;
4299 for (i = 0; i < d; ++i)
4300 if (!active[i])
4301 break;
4303 if (i == d) {
4304 free(active);
4305 return set;
4308 nparam = isl_set_dim(set, isl_dim_param);
4309 nvar = isl_set_dim(set, isl_dim_set);
4310 if (nparam < 0 || nvar < 0)
4311 goto error;
4312 for (i = 0; i < nparam; ++i) {
4313 if (active[i])
4314 continue;
4315 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4316 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4318 for (i = 0; i < nvar; ++i) {
4319 if (active[nparam + i])
4320 continue;
4321 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4322 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4325 free(active);
4327 return set;
4328 error:
4329 free(active);
4330 isl_set_free(set);
4331 return NULL;
4334 struct isl_opt_data {
4335 isl_qpolynomial *qp;
4336 int first;
4337 isl_val *opt;
4338 int max;
4341 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4343 struct isl_opt_data *data = (struct isl_opt_data *)user;
4344 isl_val *val;
4346 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4347 if (data->first) {
4348 data->first = 0;
4349 data->opt = val;
4350 } else if (data->max) {
4351 data->opt = isl_val_max(data->opt, val);
4352 } else {
4353 data->opt = isl_val_min(data->opt, val);
4356 return isl_stat_ok;
4359 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4360 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4362 struct isl_opt_data data = { NULL, 1, NULL, max };
4363 isl_bool is_cst;
4365 if (!set || !qp)
4366 goto error;
4368 is_cst = isl_poly_is_cst(qp->poly);
4369 if (is_cst < 0)
4370 goto error;
4371 if (is_cst) {
4372 isl_set_free(set);
4373 data.opt = isl_qpolynomial_get_constant_val(qp);
4374 isl_qpolynomial_free(qp);
4375 return data.opt;
4378 set = fix_inactive(set, qp);
4380 data.qp = qp;
4381 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4382 goto error;
4384 if (data.first)
4385 data.opt = isl_val_zero(isl_set_get_ctx(set));
4387 isl_set_free(set);
4388 isl_qpolynomial_free(qp);
4389 return data.opt;
4390 error:
4391 isl_set_free(set);
4392 isl_qpolynomial_free(qp);
4393 isl_val_free(data.opt);
4394 return NULL;
4397 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4398 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4400 int i;
4401 int n_sub;
4402 isl_ctx *ctx;
4403 isl_space *space;
4404 isl_poly **subs;
4405 isl_mat *mat, *diag;
4407 qp = isl_qpolynomial_cow(qp);
4409 space = isl_qpolynomial_peek_domain_space(qp);
4410 if (isl_morph_check_applies(morph, space) < 0)
4411 goto error;
4413 ctx = isl_qpolynomial_get_ctx(qp);
4414 n_sub = morph->inv->n_row - 1;
4415 if (morph->inv->n_row != morph->inv->n_col)
4416 n_sub += qp->div->n_row;
4417 subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
4418 if (n_sub && !subs)
4419 goto error;
4421 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4422 subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4423 morph->inv->row[0][0], morph->inv->n_col);
4424 if (morph->inv->n_row != morph->inv->n_col)
4425 for (i = 0; i < qp->div->n_row; ++i)
4426 subs[morph->inv->n_row - 1 + i] =
4427 isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4429 qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4431 for (i = 0; i < n_sub; ++i)
4432 isl_poly_free(subs[i]);
4433 free(subs);
4435 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4436 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4437 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4438 mat = isl_mat_diagonal(mat, diag);
4439 qp->div = isl_mat_product(qp->div, mat);
4440 isl_space_free(qp->dim);
4441 qp->dim = isl_space_copy(morph->ran->dim);
4443 if (!qp->poly || !qp->div || !qp->dim)
4444 goto error;
4446 isl_morph_free(morph);
4448 return qp;
4449 error:
4450 isl_qpolynomial_free(qp);
4451 isl_morph_free(morph);
4452 return NULL;
4455 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4456 __isl_take isl_union_pw_qpolynomial *upwqp1,
4457 __isl_take isl_union_pw_qpolynomial *upwqp2)
4459 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4460 &isl_pw_qpolynomial_mul);
4463 /* Reorder the dimension of "qp" according to the given reordering.
4465 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4466 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4468 isl_space *space;
4470 qp = isl_qpolynomial_cow(qp);
4471 if (!qp)
4472 goto error;
4474 r = isl_reordering_extend(r, qp->div->n_row);
4475 if (!r)
4476 goto error;
4478 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4479 if (!qp->div)
4480 goto error;
4482 qp->poly = reorder(qp->poly, r->pos);
4483 if (!qp->poly)
4484 goto error;
4486 space = isl_reordering_get_space(r);
4487 qp = isl_qpolynomial_reset_domain_space(qp, space);
4489 isl_reordering_free(r);
4490 return qp;
4491 error:
4492 isl_qpolynomial_free(qp);
4493 isl_reordering_free(r);
4494 return NULL;
4497 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4498 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4500 isl_space *domain_space;
4501 isl_bool equal_params;
4503 domain_space = isl_qpolynomial_peek_domain_space(qp);
4504 equal_params = isl_space_has_equal_params(domain_space, model);
4505 if (equal_params < 0)
4506 goto error;
4507 if (!equal_params) {
4508 isl_reordering *exp;
4510 exp = isl_parameter_alignment_reordering(domain_space, model);
4511 qp = isl_qpolynomial_realign_domain(qp, exp);
4514 isl_space_free(model);
4515 return qp;
4516 error:
4517 isl_space_free(model);
4518 isl_qpolynomial_free(qp);
4519 return NULL;
4522 struct isl_split_periods_data {
4523 int max_periods;
4524 isl_pw_qpolynomial *res;
4527 /* Create a slice where the integer division "div" has the fixed value "v".
4528 * In particular, if "div" refers to floor(f/m), then create a slice
4530 * m v <= f <= m v + (m - 1)
4532 * or
4534 * f - m v >= 0
4535 * -f + m v + (m - 1) >= 0
4537 static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
4538 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4540 isl_size total;
4541 isl_basic_set *bset = NULL;
4542 int k;
4544 total = isl_space_dim(space, isl_dim_all);
4545 if (total < 0 || !qp)
4546 goto error;
4548 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4550 k = isl_basic_set_alloc_inequality(bset);
4551 if (k < 0)
4552 goto error;
4553 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4554 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4556 k = isl_basic_set_alloc_inequality(bset);
4557 if (k < 0)
4558 goto error;
4559 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4560 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4561 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4562 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4564 isl_space_free(space);
4565 return isl_set_from_basic_set(bset);
4566 error:
4567 isl_basic_set_free(bset);
4568 isl_space_free(space);
4569 return NULL;
4572 static isl_stat split_periods(__isl_take isl_set *set,
4573 __isl_take isl_qpolynomial *qp, void *user);
4575 /* Create a slice of the domain "set" such that integer division "div"
4576 * has the fixed value "v" and add the results to data->res,
4577 * replacing the integer division by "v" in "qp".
4579 static isl_stat set_div(__isl_take isl_set *set,
4580 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4581 struct isl_split_periods_data *data)
4583 int i;
4584 isl_size div_pos;
4585 isl_set *slice;
4586 isl_poly *cst;
4588 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4589 set = isl_set_intersect(set, slice);
4591 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4592 if (div_pos < 0)
4593 goto error;
4595 for (i = div + 1; i < qp->div->n_row; ++i) {
4596 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div]))
4597 continue;
4598 isl_int_addmul(qp->div->row[i][1],
4599 qp->div->row[i][2 + div_pos + div], v);
4600 isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0);
4603 cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4604 qp = substitute_div(qp, div, cst);
4606 return split_periods(set, qp, data);
4607 error:
4608 isl_set_free(set);
4609 isl_qpolynomial_free(qp);
4610 return isl_stat_error;
4613 /* Split the domain "set" such that integer division "div"
4614 * has a fixed value (ranging from "min" to "max") on each slice
4615 * and add the results to data->res.
4617 static isl_stat split_div(__isl_take isl_set *set,
4618 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4619 struct isl_split_periods_data *data)
4621 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4622 isl_set *set_i = isl_set_copy(set);
4623 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4625 if (set_div(set_i, qp_i, div, min, data) < 0)
4626 goto error;
4628 isl_set_free(set);
4629 isl_qpolynomial_free(qp);
4630 return isl_stat_ok;
4631 error:
4632 isl_set_free(set);
4633 isl_qpolynomial_free(qp);
4634 return isl_stat_error;
4637 /* If "qp" refers to any integer division
4638 * that can only attain "max_periods" distinct values on "set"
4639 * then split the domain along those distinct values.
4640 * Add the results (or the original if no splitting occurs)
4641 * to data->res.
4643 static isl_stat split_periods(__isl_take isl_set *set,
4644 __isl_take isl_qpolynomial *qp, void *user)
4646 int i;
4647 isl_pw_qpolynomial *pwqp;
4648 struct isl_split_periods_data *data;
4649 isl_int min, max;
4650 isl_size div_pos;
4651 isl_stat r = isl_stat_ok;
4653 data = (struct isl_split_periods_data *)user;
4655 if (!set || !qp)
4656 goto error;
4658 if (qp->div->n_row == 0) {
4659 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4660 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4661 return isl_stat_ok;
4664 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4665 if (div_pos < 0)
4666 goto error;
4668 isl_int_init(min);
4669 isl_int_init(max);
4670 for (i = 0; i < qp->div->n_row; ++i) {
4671 enum isl_lp_result lp_res;
4673 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + div_pos,
4674 qp->div->n_row) != -1)
4675 continue;
4677 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4678 set->ctx->one, &min, NULL, NULL);
4679 if (lp_res == isl_lp_error)
4680 goto error2;
4681 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4682 continue;
4683 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4685 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4686 set->ctx->one, &max, NULL, NULL);
4687 if (lp_res == isl_lp_error)
4688 goto error2;
4689 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4690 continue;
4691 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4693 isl_int_sub(max, max, min);
4694 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4695 isl_int_add(max, max, min);
4696 break;
4700 if (i < qp->div->n_row) {
4701 r = split_div(set, qp, i, min, max, data);
4702 } else {
4703 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4704 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4707 isl_int_clear(max);
4708 isl_int_clear(min);
4710 return r;
4711 error2:
4712 isl_int_clear(max);
4713 isl_int_clear(min);
4714 error:
4715 isl_set_free(set);
4716 isl_qpolynomial_free(qp);
4717 return isl_stat_error;
4720 /* If any quasi-polynomial in pwqp refers to any integer division
4721 * that can only attain "max_periods" distinct values on its domain
4722 * then split the domain along those distinct values.
4724 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4725 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4727 struct isl_split_periods_data data;
4729 data.max_periods = max_periods;
4730 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4732 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4733 goto error;
4735 isl_pw_qpolynomial_free(pwqp);
4737 return data.res;
4738 error:
4739 isl_pw_qpolynomial_free(data.res);
4740 isl_pw_qpolynomial_free(pwqp);
4741 return NULL;
4744 /* Construct a piecewise quasipolynomial that is constant on the given
4745 * domain. In particular, it is
4746 * 0 if cst == 0
4747 * 1 if cst == 1
4748 * infinity if cst == -1
4750 * If cst == -1, then explicitly check whether the domain is empty and,
4751 * if so, return 0 instead.
4753 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4754 __isl_take isl_basic_set *bset, int cst)
4756 isl_space *space;
4757 isl_qpolynomial *qp;
4759 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4760 cst = 0;
4761 if (!bset)
4762 return NULL;
4764 bset = isl_basic_set_params(bset);
4765 space = isl_basic_set_get_space(bset);
4766 if (cst < 0)
4767 qp = isl_qpolynomial_infty_on_domain(space);
4768 else if (cst == 0)
4769 qp = isl_qpolynomial_zero_on_domain(space);
4770 else
4771 qp = isl_qpolynomial_one_on_domain(space);
4772 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4775 /* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4776 * "fn" is the function that is called on each factor.
4777 * "pwpq" collects the results.
4779 struct isl_multiplicative_call_data_pw_qpolynomial {
4780 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset);
4781 isl_pw_qpolynomial *pwqp;
4784 /* Call "fn" on "bset" and return the result,
4785 * but first check if "bset" has any redundant constraints or
4786 * implicit equality constraints.
4787 * If so, there may be further opportunities for detecting factors or
4788 * removing equality constraints, so recursively call
4789 * the top-level isl_basic_set_multiplicative_call.
4791 static __isl_give isl_pw_qpolynomial *multiplicative_call_base(
4792 __isl_take isl_basic_set *bset,
4793 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4795 isl_size n1, n2, n_eq;
4797 n1 = isl_basic_set_n_constraint(bset);
4798 if (n1 < 0)
4799 bset = isl_basic_set_free(bset);
4800 bset = isl_basic_set_remove_redundancies(bset);
4801 bset = isl_basic_set_detect_equalities(bset);
4802 n2 = isl_basic_set_n_constraint(bset);
4803 n_eq = isl_basic_set_n_equality(bset);
4804 if (n2 < 0 || n_eq < 0)
4805 bset = isl_basic_set_free(bset);
4806 else if (n2 < n1 || n_eq > 0)
4807 return isl_basic_set_multiplicative_call(bset, fn);
4808 return fn(bset);
4811 /* isl_factorizer_every_factor_basic_set callback that applies
4812 * data->fn to the factor "bset" and multiplies in the result
4813 * in data->pwqp.
4815 static isl_bool multiplicative_call_factor_pw_qpolynomial(
4816 __isl_keep isl_basic_set *bset, void *user)
4818 struct isl_multiplicative_call_data_pw_qpolynomial *data = user;
4819 isl_pw_qpolynomial *res;
4821 bset = isl_basic_set_copy(bset);
4822 res = multiplicative_call_base(bset, data->fn);
4823 data->pwqp = isl_pw_qpolynomial_mul(data->pwqp, res);
4824 if (!data->pwqp)
4825 return isl_bool_error;
4827 return isl_bool_true;
4830 /* Factor bset, call fn on each of the factors and return the product.
4832 * If no factors can be found, simply call fn on the input.
4833 * Otherwise, construct the factors based on the factorizer,
4834 * call fn on each factor and compute the product.
4836 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4837 __isl_take isl_basic_set *bset,
4838 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4840 struct isl_multiplicative_call_data_pw_qpolynomial data = { fn };
4841 isl_space *space;
4842 isl_set *set;
4843 isl_factorizer *f;
4844 isl_qpolynomial *qp;
4845 isl_bool every;
4847 f = isl_basic_set_factorizer(bset);
4848 if (!f)
4849 goto error;
4850 if (f->n_group == 0) {
4851 isl_factorizer_free(f);
4852 return multiplicative_call_base(bset, fn);
4855 space = isl_basic_set_get_space(bset);
4856 space = isl_space_params(space);
4857 set = isl_set_universe(isl_space_copy(space));
4858 qp = isl_qpolynomial_one_on_domain(space);
4859 data.pwqp = isl_pw_qpolynomial_alloc(set, qp);
4861 every = isl_factorizer_every_factor_basic_set(f,
4862 &multiplicative_call_factor_pw_qpolynomial, &data);
4863 if (every < 0)
4864 data.pwqp = isl_pw_qpolynomial_free(data.pwqp);
4866 isl_basic_set_free(bset);
4867 isl_factorizer_free(f);
4869 return data.pwqp;
4870 error:
4871 isl_basic_set_free(bset);
4872 return NULL;
4875 /* Factor bset, call fn on each of the factors and return the product.
4876 * The function is assumed to evaluate to zero on empty domains,
4877 * to one on zero-dimensional domains and to infinity on unbounded domains
4878 * and will not be called explicitly on zero-dimensional or unbounded domains.
4880 * We first check for some special cases and remove all equalities.
4881 * Then we hand over control to compressed_multiplicative_call.
4883 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4884 __isl_take isl_basic_set *bset,
4885 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4887 isl_bool bounded;
4888 isl_size dim;
4889 isl_morph *morph;
4890 isl_pw_qpolynomial *pwqp;
4892 if (!bset)
4893 return NULL;
4895 if (isl_basic_set_plain_is_empty(bset))
4896 return constant_on_domain(bset, 0);
4898 dim = isl_basic_set_dim(bset, isl_dim_set);
4899 if (dim < 0)
4900 goto error;
4901 if (dim == 0)
4902 return constant_on_domain(bset, 1);
4904 bounded = isl_basic_set_is_bounded(bset);
4905 if (bounded < 0)
4906 goto error;
4907 if (!bounded)
4908 return constant_on_domain(bset, -1);
4910 if (bset->n_eq == 0)
4911 return compressed_multiplicative_call(bset, fn);
4913 morph = isl_basic_set_full_compression(bset);
4914 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4916 pwqp = compressed_multiplicative_call(bset, fn);
4918 morph = isl_morph_dom_params(morph);
4919 morph = isl_morph_ran_params(morph);
4920 morph = isl_morph_inverse(morph);
4922 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4924 return pwqp;
4925 error:
4926 isl_basic_set_free(bset);
4927 return NULL;
4930 /* Drop all floors in "qp", turning each integer division [a/m] into
4931 * a rational division a/m. If "down" is set, then the integer division
4932 * is replaced by (a-(m-1))/m instead.
4934 static __isl_give isl_qpolynomial *qp_drop_floors(
4935 __isl_take isl_qpolynomial *qp, int down)
4937 int i;
4938 isl_poly *s;
4940 if (!qp)
4941 return NULL;
4942 if (qp->div->n_row == 0)
4943 return qp;
4945 qp = isl_qpolynomial_cow(qp);
4946 if (!qp)
4947 return NULL;
4949 for (i = qp->div->n_row - 1; i >= 0; --i) {
4950 if (down) {
4951 isl_int_sub(qp->div->row[i][1],
4952 qp->div->row[i][1], qp->div->row[i][0]);
4953 isl_int_add_ui(qp->div->row[i][1],
4954 qp->div->row[i][1], 1);
4956 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4957 qp->div->row[i][0], qp->div->n_col - 1);
4958 qp = substitute_div(qp, i, s);
4959 if (!qp)
4960 return NULL;
4963 return qp;
4966 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4967 * a rational division a/m.
4969 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4970 __isl_take isl_pw_qpolynomial *pwqp)
4972 int i;
4974 if (!pwqp)
4975 return NULL;
4977 if (isl_pw_qpolynomial_is_zero(pwqp))
4978 return pwqp;
4980 pwqp = isl_pw_qpolynomial_cow(pwqp);
4981 if (!pwqp)
4982 return NULL;
4984 for (i = 0; i < pwqp->n; ++i) {
4985 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4986 if (!pwqp->p[i].qp)
4987 goto error;
4990 return pwqp;
4991 error:
4992 isl_pw_qpolynomial_free(pwqp);
4993 return NULL;
4996 /* Adjust all the integer divisions in "qp" such that they are at least
4997 * one over the given orthant (identified by "signs"). This ensures
4998 * that they will still be non-negative even after subtracting (m-1)/m.
5000 * In particular, f is replaced by f' + v, changing f = [a/m]
5001 * to f' = [(a - m v)/m].
5002 * If the constant term k in a is smaller than m,
5003 * the constant term of v is set to floor(k/m) - 1.
5004 * For any other term, if the coefficient c and the variable x have
5005 * the same sign, then no changes are needed.
5006 * Otherwise, if the variable is positive (and c is negative),
5007 * then the coefficient of x in v is set to floor(c/m).
5008 * If the variable is negative (and c is positive),
5009 * then the coefficient of x in v is set to ceil(c/m).
5011 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
5012 int *signs)
5014 int i, j;
5015 isl_size div_pos;
5016 isl_vec *v = NULL;
5017 isl_poly *s;
5019 qp = isl_qpolynomial_cow(qp);
5020 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
5021 if (div_pos < 0)
5022 return isl_qpolynomial_free(qp);
5023 qp->div = isl_mat_cow(qp->div);
5024 if (!qp->div)
5025 goto error;
5027 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
5029 for (i = 0; i < qp->div->n_row; ++i) {
5030 isl_int *row = qp->div->row[i];
5031 v = isl_vec_clr(v);
5032 if (!v)
5033 goto error;
5034 if (isl_int_lt(row[1], row[0])) {
5035 isl_int_fdiv_q(v->el[0], row[1], row[0]);
5036 isl_int_sub_ui(v->el[0], v->el[0], 1);
5037 isl_int_submul(row[1], row[0], v->el[0]);
5039 for (j = 0; j < div_pos; ++j) {
5040 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
5041 continue;
5042 if (signs[j] < 0)
5043 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
5044 else
5045 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
5046 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
5048 for (j = 0; j < i; ++j) {
5049 if (isl_int_sgn(row[2 + div_pos + j]) >= 0)
5050 continue;
5051 isl_int_fdiv_q(v->el[1 + div_pos + j],
5052 row[2 + div_pos + j], row[0]);
5053 isl_int_submul(row[2 + div_pos + j],
5054 row[0], v->el[1 + div_pos + j]);
5056 for (j = i + 1; j < qp->div->n_row; ++j) {
5057 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
5058 continue;
5059 isl_seq_combine(qp->div->row[j] + 1,
5060 qp->div->ctx->one, qp->div->row[j] + 1,
5061 qp->div->row[j][2 + div_pos + i], v->el,
5062 v->size);
5064 isl_int_set_si(v->el[1 + div_pos + i], 1);
5065 s = isl_poly_from_affine(qp->dim->ctx, v->el,
5066 qp->div->ctx->one, v->size);
5067 qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
5068 isl_poly_free(s);
5069 if (!qp->poly)
5070 goto error;
5073 isl_vec_free(v);
5074 return qp;
5075 error:
5076 isl_vec_free(v);
5077 isl_qpolynomial_free(qp);
5078 return NULL;
5081 struct isl_to_poly_data {
5082 int sign;
5083 isl_pw_qpolynomial *res;
5084 isl_qpolynomial *qp;
5087 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5088 * We first make all integer divisions positive and then split the
5089 * quasipolynomials into terms with sign data->sign (the direction
5090 * of the requested approximation) and terms with the opposite sign.
5091 * In the first set of terms, each integer division [a/m] is
5092 * overapproximated by a/m, while in the second it is underapproximated
5093 * by (a-(m-1))/m.
5095 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
5096 int *signs, void *user)
5098 struct isl_to_poly_data *data = user;
5099 isl_pw_qpolynomial *t;
5100 isl_qpolynomial *qp, *up, *down;
5102 qp = isl_qpolynomial_copy(data->qp);
5103 qp = make_divs_pos(qp, signs);
5105 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
5106 up = qp_drop_floors(up, 0);
5107 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
5108 down = qp_drop_floors(down, 1);
5110 isl_qpolynomial_free(qp);
5111 qp = isl_qpolynomial_add(up, down);
5113 t = isl_pw_qpolynomial_alloc(orthant, qp);
5114 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
5116 return isl_stat_ok;
5119 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5120 * the polynomial will be an overapproximation. If "sign" is negative,
5121 * it will be an underapproximation. If "sign" is zero, the approximation
5122 * will lie somewhere in between.
5124 * In particular, is sign == 0, we simply drop the floors, turning
5125 * the integer divisions into rational divisions.
5126 * Otherwise, we split the domains into orthants, make all integer divisions
5127 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5128 * depending on the requested sign and the sign of the term in which
5129 * the integer division appears.
5131 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
5132 __isl_take isl_pw_qpolynomial *pwqp, int sign)
5134 int i;
5135 struct isl_to_poly_data data;
5137 if (sign == 0)
5138 return pwqp_drop_floors(pwqp);
5140 if (!pwqp)
5141 return NULL;
5143 data.sign = sign;
5144 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
5146 for (i = 0; i < pwqp->n; ++i) {
5147 if (pwqp->p[i].qp->div->n_row == 0) {
5148 isl_pw_qpolynomial *t;
5149 t = isl_pw_qpolynomial_alloc(
5150 isl_set_copy(pwqp->p[i].set),
5151 isl_qpolynomial_copy(pwqp->p[i].qp));
5152 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
5153 continue;
5155 data.qp = pwqp->p[i].qp;
5156 if (isl_set_foreach_orthant(pwqp->p[i].set,
5157 &to_polynomial_on_orthant, &data) < 0)
5158 goto error;
5161 isl_pw_qpolynomial_free(pwqp);
5163 return data.res;
5164 error:
5165 isl_pw_qpolynomial_free(pwqp);
5166 isl_pw_qpolynomial_free(data.res);
5167 return NULL;
5170 static __isl_give isl_pw_qpolynomial *poly_entry(
5171 __isl_take isl_pw_qpolynomial *pwqp, void *user)
5173 int *sign = user;
5175 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
5178 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
5179 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
5181 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5182 &poly_entry, &sign);
5185 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
5186 __isl_take isl_qpolynomial *qp)
5188 isl_local_space *ls;
5189 isl_vec *vec;
5190 isl_aff *aff;
5191 isl_basic_map *bmap;
5192 isl_bool is_affine;
5194 if (!qp)
5195 return NULL;
5196 is_affine = isl_poly_is_affine(qp->poly);
5197 if (is_affine < 0)
5198 goto error;
5199 if (!is_affine)
5200 isl_die(qp->dim->ctx, isl_error_invalid,
5201 "input quasi-polynomial not affine", goto error);
5202 ls = isl_qpolynomial_get_domain_local_space(qp);
5203 vec = isl_qpolynomial_extract_affine(qp);
5204 aff = isl_aff_alloc_vec(ls, vec);
5205 bmap = isl_basic_map_from_aff(aff);
5206 isl_qpolynomial_free(qp);
5207 return bmap;
5208 error:
5209 isl_qpolynomial_free(qp);
5210 return NULL;