isl_tab: invalidate undo stack if pushing record fails
[isl.git] / isl_polynomial.c
blob040900221d2972b999cdcd6951f46b6ee8036e02
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <stdlib.h>
12 #define ISL_DIM_H
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_factorization.h>
16 #include <isl_lp_private.h>
17 #include <isl_seq.h>
18 #include <isl_union_map_private.h>
19 #include <isl_constraint_private.h>
20 #include <isl_polynomial_private.h>
21 #include <isl_point_private.h>
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_range.h>
26 #include <isl_local.h>
27 #include <isl_local_space_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_val_private.h>
30 #include <isl_config.h>
31 #include <isl/deprecated/polynomial_int.h>
33 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
35 switch (type) {
36 case isl_dim_param: return 0;
37 case isl_dim_in: return dim->nparam;
38 case isl_dim_out: return dim->nparam + dim->n_in;
39 default: return 0;
43 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
45 if (!up)
46 return -1;
48 return up->var < 0;
51 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
53 if (!up)
54 return NULL;
56 isl_assert(up->ctx, up->var < 0, return NULL);
58 return (struct isl_upoly_cst *)up;
61 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
63 if (!up)
64 return NULL;
66 isl_assert(up->ctx, up->var >= 0, return NULL);
68 return (struct isl_upoly_rec *)up;
71 /* Compare two polynomials.
73 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
74 * than "up2" and 0 if they are equal.
76 static int isl_upoly_plain_cmp(__isl_keep struct isl_upoly *up1,
77 __isl_keep struct isl_upoly *up2)
79 int i;
80 struct isl_upoly_rec *rec1, *rec2;
82 if (up1 == up2)
83 return 0;
84 if (!up1)
85 return -1;
86 if (!up2)
87 return 1;
88 if (up1->var != up2->var)
89 return up1->var - up2->var;
91 if (isl_upoly_is_cst(up1)) {
92 struct isl_upoly_cst *cst1, *cst2;
93 int cmp;
95 cst1 = isl_upoly_as_cst(up1);
96 cst2 = isl_upoly_as_cst(up2);
97 if (!cst1 || !cst2)
98 return 0;
99 cmp = isl_int_cmp(cst1->n, cst2->n);
100 if (cmp != 0)
101 return cmp;
102 return isl_int_cmp(cst1->d, cst2->d);
105 rec1 = isl_upoly_as_rec(up1);
106 rec2 = isl_upoly_as_rec(up2);
107 if (!rec1 || !rec2)
108 return 0;
110 if (rec1->n != rec2->n)
111 return rec1->n - rec2->n;
113 for (i = 0; i < rec1->n; ++i) {
114 int cmp = isl_upoly_plain_cmp(rec1->p[i], rec2->p[i]);
115 if (cmp != 0)
116 return cmp;
119 return 0;
122 isl_bool isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
123 __isl_keep struct isl_upoly *up2)
125 int i;
126 struct isl_upoly_rec *rec1, *rec2;
128 if (!up1 || !up2)
129 return isl_bool_error;
130 if (up1 == up2)
131 return isl_bool_true;
132 if (up1->var != up2->var)
133 return isl_bool_false;
134 if (isl_upoly_is_cst(up1)) {
135 struct isl_upoly_cst *cst1, *cst2;
136 cst1 = isl_upoly_as_cst(up1);
137 cst2 = isl_upoly_as_cst(up2);
138 if (!cst1 || !cst2)
139 return isl_bool_error;
140 return isl_int_eq(cst1->n, cst2->n) &&
141 isl_int_eq(cst1->d, cst2->d);
144 rec1 = isl_upoly_as_rec(up1);
145 rec2 = isl_upoly_as_rec(up2);
146 if (!rec1 || !rec2)
147 return isl_bool_error;
149 if (rec1->n != rec2->n)
150 return isl_bool_false;
152 for (i = 0; i < rec1->n; ++i) {
153 isl_bool eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
154 if (eq < 0 || !eq)
155 return eq;
158 return isl_bool_true;
161 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
163 struct isl_upoly_cst *cst;
165 if (!up)
166 return -1;
167 if (!isl_upoly_is_cst(up))
168 return 0;
170 cst = isl_upoly_as_cst(up);
171 if (!cst)
172 return -1;
174 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
177 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
179 struct isl_upoly_cst *cst;
181 if (!up)
182 return 0;
183 if (!isl_upoly_is_cst(up))
184 return 0;
186 cst = isl_upoly_as_cst(up);
187 if (!cst)
188 return 0;
190 return isl_int_sgn(cst->n);
193 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
195 struct isl_upoly_cst *cst;
197 if (!up)
198 return -1;
199 if (!isl_upoly_is_cst(up))
200 return 0;
202 cst = isl_upoly_as_cst(up);
203 if (!cst)
204 return -1;
206 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
209 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
211 struct isl_upoly_cst *cst;
213 if (!up)
214 return -1;
215 if (!isl_upoly_is_cst(up))
216 return 0;
218 cst = isl_upoly_as_cst(up);
219 if (!cst)
220 return -1;
222 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
225 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
227 struct isl_upoly_cst *cst;
229 if (!up)
230 return -1;
231 if (!isl_upoly_is_cst(up))
232 return 0;
234 cst = isl_upoly_as_cst(up);
235 if (!cst)
236 return -1;
238 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
241 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
243 struct isl_upoly_cst *cst;
245 if (!up)
246 return -1;
247 if (!isl_upoly_is_cst(up))
248 return 0;
250 cst = isl_upoly_as_cst(up);
251 if (!cst)
252 return -1;
254 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
257 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
259 struct isl_upoly_cst *cst;
261 if (!up)
262 return -1;
263 if (!isl_upoly_is_cst(up))
264 return 0;
266 cst = isl_upoly_as_cst(up);
267 if (!cst)
268 return -1;
270 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
273 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
275 struct isl_upoly_cst *cst;
277 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
278 if (!cst)
279 return NULL;
281 cst->up.ref = 1;
282 cst->up.ctx = ctx;
283 isl_ctx_ref(ctx);
284 cst->up.var = -1;
286 isl_int_init(cst->n);
287 isl_int_init(cst->d);
289 return cst;
292 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
294 struct isl_upoly_cst *cst;
296 cst = isl_upoly_cst_alloc(ctx);
297 if (!cst)
298 return NULL;
300 isl_int_set_si(cst->n, 0);
301 isl_int_set_si(cst->d, 1);
303 return &cst->up;
306 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
308 struct isl_upoly_cst *cst;
310 cst = isl_upoly_cst_alloc(ctx);
311 if (!cst)
312 return NULL;
314 isl_int_set_si(cst->n, 1);
315 isl_int_set_si(cst->d, 1);
317 return &cst->up;
320 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
322 struct isl_upoly_cst *cst;
324 cst = isl_upoly_cst_alloc(ctx);
325 if (!cst)
326 return NULL;
328 isl_int_set_si(cst->n, 1);
329 isl_int_set_si(cst->d, 0);
331 return &cst->up;
334 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
336 struct isl_upoly_cst *cst;
338 cst = isl_upoly_cst_alloc(ctx);
339 if (!cst)
340 return NULL;
342 isl_int_set_si(cst->n, -1);
343 isl_int_set_si(cst->d, 0);
345 return &cst->up;
348 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
350 struct isl_upoly_cst *cst;
352 cst = isl_upoly_cst_alloc(ctx);
353 if (!cst)
354 return NULL;
356 isl_int_set_si(cst->n, 0);
357 isl_int_set_si(cst->d, 0);
359 return &cst->up;
362 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
363 isl_int n, isl_int d)
365 struct isl_upoly_cst *cst;
367 cst = isl_upoly_cst_alloc(ctx);
368 if (!cst)
369 return NULL;
371 isl_int_set(cst->n, n);
372 isl_int_set(cst->d, d);
374 return &cst->up;
377 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
378 int var, int size)
380 struct isl_upoly_rec *rec;
382 isl_assert(ctx, var >= 0, return NULL);
383 isl_assert(ctx, size >= 0, return NULL);
384 rec = isl_calloc(ctx, struct isl_upoly_rec,
385 sizeof(struct isl_upoly_rec) +
386 size * sizeof(struct isl_upoly *));
387 if (!rec)
388 return NULL;
390 rec->up.ref = 1;
391 rec->up.ctx = ctx;
392 isl_ctx_ref(ctx);
393 rec->up.var = var;
395 rec->n = 0;
396 rec->size = size;
398 return rec;
401 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
402 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
404 qp = isl_qpolynomial_cow(qp);
405 if (!qp || !dim)
406 goto error;
408 isl_space_free(qp->dim);
409 qp->dim = dim;
411 return qp;
412 error:
413 isl_qpolynomial_free(qp);
414 isl_space_free(dim);
415 return NULL;
418 /* Reset the space of "qp". This function is called from isl_pw_templ.c
419 * and doesn't know if the space of an element object is represented
420 * directly or through its domain. It therefore passes along both.
422 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
423 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
424 __isl_take isl_space *domain)
426 isl_space_free(space);
427 return isl_qpolynomial_reset_domain_space(qp, domain);
430 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
432 return qp ? qp->dim->ctx : NULL;
435 __isl_give isl_space *isl_qpolynomial_get_domain_space(
436 __isl_keep isl_qpolynomial *qp)
438 return qp ? isl_space_copy(qp->dim) : NULL;
441 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
443 isl_space *space;
444 if (!qp)
445 return NULL;
446 space = isl_space_copy(qp->dim);
447 space = isl_space_from_domain(space);
448 space = isl_space_add_dims(space, isl_dim_out, 1);
449 return space;
452 /* Return the number of variables of the given type in the domain of "qp".
454 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
455 enum isl_dim_type type)
457 if (!qp)
458 return 0;
459 if (type == isl_dim_div)
460 return qp->div->n_row;
461 if (type == isl_dim_all)
462 return isl_space_dim(qp->dim, isl_dim_all) +
463 isl_qpolynomial_domain_dim(qp, isl_dim_div);
464 return isl_space_dim(qp->dim, type);
467 /* Externally, an isl_qpolynomial has a map space, but internally, the
468 * ls field corresponds to the domain of that space.
470 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
471 enum isl_dim_type type)
473 if (!qp)
474 return 0;
475 if (type == isl_dim_out)
476 return 1;
477 if (type == isl_dim_in)
478 type = isl_dim_set;
479 return isl_qpolynomial_domain_dim(qp, type);
482 /* Return the offset of the first coefficient of type "type" in
483 * the domain of "qp".
485 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
486 enum isl_dim_type type)
488 if (!qp)
489 return 0;
490 switch (type) {
491 case isl_dim_cst:
492 return 0;
493 case isl_dim_param:
494 case isl_dim_set:
495 return 1 + isl_space_offset(qp->dim, type);
496 case isl_dim_div:
497 return 1 + isl_space_dim(qp->dim, isl_dim_all);
498 default:
499 return 0;
503 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
505 return qp ? isl_upoly_is_zero(qp->upoly) : isl_bool_error;
508 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
510 return qp ? isl_upoly_is_one(qp->upoly) : isl_bool_error;
513 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
515 return qp ? isl_upoly_is_nan(qp->upoly) : isl_bool_error;
518 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
520 return qp ? isl_upoly_is_infty(qp->upoly) : isl_bool_error;
523 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
525 return qp ? isl_upoly_is_neginfty(qp->upoly) : isl_bool_error;
528 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
530 return qp ? isl_upoly_sgn(qp->upoly) : 0;
533 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
535 isl_int_clear(cst->n);
536 isl_int_clear(cst->d);
539 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
541 int i;
543 for (i = 0; i < rec->n; ++i)
544 isl_upoly_free(rec->p[i]);
547 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
549 if (!up)
550 return NULL;
552 up->ref++;
553 return up;
556 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
558 struct isl_upoly_cst *cst;
559 struct isl_upoly_cst *dup;
561 cst = isl_upoly_as_cst(up);
562 if (!cst)
563 return NULL;
565 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
566 if (!dup)
567 return NULL;
568 isl_int_set(dup->n, cst->n);
569 isl_int_set(dup->d, cst->d);
571 return &dup->up;
574 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
576 int i;
577 struct isl_upoly_rec *rec;
578 struct isl_upoly_rec *dup;
580 rec = isl_upoly_as_rec(up);
581 if (!rec)
582 return NULL;
584 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
585 if (!dup)
586 return NULL;
588 for (i = 0; i < rec->n; ++i) {
589 dup->p[i] = isl_upoly_copy(rec->p[i]);
590 if (!dup->p[i])
591 goto error;
592 dup->n++;
595 return &dup->up;
596 error:
597 isl_upoly_free(&dup->up);
598 return NULL;
601 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
603 if (!up)
604 return NULL;
606 if (isl_upoly_is_cst(up))
607 return isl_upoly_dup_cst(up);
608 else
609 return isl_upoly_dup_rec(up);
612 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
614 if (!up)
615 return NULL;
617 if (up->ref == 1)
618 return up;
619 up->ref--;
620 return isl_upoly_dup(up);
623 void isl_upoly_free(__isl_take struct isl_upoly *up)
625 if (!up)
626 return;
628 if (--up->ref > 0)
629 return;
631 if (up->var < 0)
632 upoly_free_cst((struct isl_upoly_cst *)up);
633 else
634 upoly_free_rec((struct isl_upoly_rec *)up);
636 isl_ctx_deref(up->ctx);
637 free(up);
640 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
642 isl_int gcd;
644 isl_int_init(gcd);
645 isl_int_gcd(gcd, cst->n, cst->d);
646 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
647 isl_int_divexact(cst->n, cst->n, gcd);
648 isl_int_divexact(cst->d, cst->d, gcd);
650 isl_int_clear(gcd);
653 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
654 __isl_take struct isl_upoly *up2)
656 struct isl_upoly_cst *cst1;
657 struct isl_upoly_cst *cst2;
659 up1 = isl_upoly_cow(up1);
660 if (!up1 || !up2)
661 goto error;
663 cst1 = isl_upoly_as_cst(up1);
664 cst2 = isl_upoly_as_cst(up2);
666 if (isl_int_eq(cst1->d, cst2->d))
667 isl_int_add(cst1->n, cst1->n, cst2->n);
668 else {
669 isl_int_mul(cst1->n, cst1->n, cst2->d);
670 isl_int_addmul(cst1->n, cst2->n, cst1->d);
671 isl_int_mul(cst1->d, cst1->d, cst2->d);
674 isl_upoly_cst_reduce(cst1);
676 isl_upoly_free(up2);
677 return up1;
678 error:
679 isl_upoly_free(up1);
680 isl_upoly_free(up2);
681 return NULL;
684 static __isl_give struct isl_upoly *replace_by_zero(
685 __isl_take struct isl_upoly *up)
687 struct isl_ctx *ctx;
689 if (!up)
690 return NULL;
691 ctx = up->ctx;
692 isl_upoly_free(up);
693 return isl_upoly_zero(ctx);
696 static __isl_give struct isl_upoly *replace_by_constant_term(
697 __isl_take struct isl_upoly *up)
699 struct isl_upoly_rec *rec;
700 struct isl_upoly *cst;
702 if (!up)
703 return NULL;
705 rec = isl_upoly_as_rec(up);
706 if (!rec)
707 goto error;
708 cst = isl_upoly_copy(rec->p[0]);
709 isl_upoly_free(up);
710 return cst;
711 error:
712 isl_upoly_free(up);
713 return NULL;
716 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
717 __isl_take struct isl_upoly *up2)
719 int i;
720 struct isl_upoly_rec *rec1, *rec2;
722 if (!up1 || !up2)
723 goto error;
725 if (isl_upoly_is_nan(up1)) {
726 isl_upoly_free(up2);
727 return up1;
730 if (isl_upoly_is_nan(up2)) {
731 isl_upoly_free(up1);
732 return up2;
735 if (isl_upoly_is_zero(up1)) {
736 isl_upoly_free(up1);
737 return up2;
740 if (isl_upoly_is_zero(up2)) {
741 isl_upoly_free(up2);
742 return up1;
745 if (up1->var < up2->var)
746 return isl_upoly_sum(up2, up1);
748 if (up2->var < up1->var) {
749 struct isl_upoly_rec *rec;
750 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
751 isl_upoly_free(up1);
752 return up2;
754 up1 = isl_upoly_cow(up1);
755 rec = isl_upoly_as_rec(up1);
756 if (!rec)
757 goto error;
758 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
759 if (rec->n == 1)
760 up1 = replace_by_constant_term(up1);
761 return up1;
764 if (isl_upoly_is_cst(up1))
765 return isl_upoly_sum_cst(up1, up2);
767 rec1 = isl_upoly_as_rec(up1);
768 rec2 = isl_upoly_as_rec(up2);
769 if (!rec1 || !rec2)
770 goto error;
772 if (rec1->n < rec2->n)
773 return isl_upoly_sum(up2, up1);
775 up1 = isl_upoly_cow(up1);
776 rec1 = isl_upoly_as_rec(up1);
777 if (!rec1)
778 goto error;
780 for (i = rec2->n - 1; i >= 0; --i) {
781 rec1->p[i] = isl_upoly_sum(rec1->p[i],
782 isl_upoly_copy(rec2->p[i]));
783 if (!rec1->p[i])
784 goto error;
785 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
786 isl_upoly_free(rec1->p[i]);
787 rec1->n--;
791 if (rec1->n == 0)
792 up1 = replace_by_zero(up1);
793 else if (rec1->n == 1)
794 up1 = replace_by_constant_term(up1);
796 isl_upoly_free(up2);
798 return up1;
799 error:
800 isl_upoly_free(up1);
801 isl_upoly_free(up2);
802 return NULL;
805 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
806 __isl_take struct isl_upoly *up, isl_int v)
808 struct isl_upoly_cst *cst;
810 up = isl_upoly_cow(up);
811 if (!up)
812 return NULL;
814 cst = isl_upoly_as_cst(up);
816 isl_int_addmul(cst->n, cst->d, v);
818 return up;
821 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
822 __isl_take struct isl_upoly *up, isl_int v)
824 struct isl_upoly_rec *rec;
826 if (!up)
827 return NULL;
829 if (isl_upoly_is_cst(up))
830 return isl_upoly_cst_add_isl_int(up, v);
832 up = isl_upoly_cow(up);
833 rec = isl_upoly_as_rec(up);
834 if (!rec)
835 goto error;
837 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
838 if (!rec->p[0])
839 goto error;
841 return up;
842 error:
843 isl_upoly_free(up);
844 return NULL;
847 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
848 __isl_take struct isl_upoly *up, isl_int v)
850 struct isl_upoly_cst *cst;
852 if (isl_upoly_is_zero(up))
853 return up;
855 up = isl_upoly_cow(up);
856 if (!up)
857 return NULL;
859 cst = isl_upoly_as_cst(up);
861 isl_int_mul(cst->n, cst->n, v);
863 return up;
866 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
867 __isl_take struct isl_upoly *up, isl_int v)
869 int i;
870 struct isl_upoly_rec *rec;
872 if (!up)
873 return NULL;
875 if (isl_upoly_is_cst(up))
876 return isl_upoly_cst_mul_isl_int(up, v);
878 up = isl_upoly_cow(up);
879 rec = isl_upoly_as_rec(up);
880 if (!rec)
881 goto error;
883 for (i = 0; i < rec->n; ++i) {
884 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
885 if (!rec->p[i])
886 goto error;
889 return up;
890 error:
891 isl_upoly_free(up);
892 return NULL;
895 /* Multiply the constant polynomial "up" by "v".
897 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
898 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
900 struct isl_upoly_cst *cst;
902 if (isl_upoly_is_zero(up))
903 return up;
905 up = isl_upoly_cow(up);
906 if (!up)
907 return NULL;
909 cst = isl_upoly_as_cst(up);
911 isl_int_mul(cst->n, cst->n, v->n);
912 isl_int_mul(cst->d, cst->d, v->d);
913 isl_upoly_cst_reduce(cst);
915 return up;
918 /* Multiply the polynomial "up" by "v".
920 static __isl_give struct isl_upoly *isl_upoly_scale_val(
921 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
923 int i;
924 struct isl_upoly_rec *rec;
926 if (!up)
927 return NULL;
929 if (isl_upoly_is_cst(up))
930 return isl_upoly_cst_scale_val(up, v);
932 up = isl_upoly_cow(up);
933 rec = isl_upoly_as_rec(up);
934 if (!rec)
935 goto error;
937 for (i = 0; i < rec->n; ++i) {
938 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
939 if (!rec->p[i])
940 goto error;
943 return up;
944 error:
945 isl_upoly_free(up);
946 return NULL;
949 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
950 __isl_take struct isl_upoly *up2)
952 struct isl_upoly_cst *cst1;
953 struct isl_upoly_cst *cst2;
955 up1 = isl_upoly_cow(up1);
956 if (!up1 || !up2)
957 goto error;
959 cst1 = isl_upoly_as_cst(up1);
960 cst2 = isl_upoly_as_cst(up2);
962 isl_int_mul(cst1->n, cst1->n, cst2->n);
963 isl_int_mul(cst1->d, cst1->d, cst2->d);
965 isl_upoly_cst_reduce(cst1);
967 isl_upoly_free(up2);
968 return up1;
969 error:
970 isl_upoly_free(up1);
971 isl_upoly_free(up2);
972 return NULL;
975 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
976 __isl_take struct isl_upoly *up2)
978 struct isl_upoly_rec *rec1;
979 struct isl_upoly_rec *rec2;
980 struct isl_upoly_rec *res = NULL;
981 int i, j;
982 int size;
984 rec1 = isl_upoly_as_rec(up1);
985 rec2 = isl_upoly_as_rec(up2);
986 if (!rec1 || !rec2)
987 goto error;
988 size = rec1->n + rec2->n - 1;
989 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
990 if (!res)
991 goto error;
993 for (i = 0; i < rec1->n; ++i) {
994 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
995 isl_upoly_copy(rec1->p[i]));
996 if (!res->p[i])
997 goto error;
998 res->n++;
1000 for (; i < size; ++i) {
1001 res->p[i] = isl_upoly_zero(up1->ctx);
1002 if (!res->p[i])
1003 goto error;
1004 res->n++;
1006 for (i = 0; i < rec1->n; ++i) {
1007 for (j = 1; j < rec2->n; ++j) {
1008 struct isl_upoly *up;
1009 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
1010 isl_upoly_copy(rec1->p[i]));
1011 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
1012 if (!res->p[i + j])
1013 goto error;
1017 isl_upoly_free(up1);
1018 isl_upoly_free(up2);
1020 return &res->up;
1021 error:
1022 isl_upoly_free(up1);
1023 isl_upoly_free(up2);
1024 isl_upoly_free(&res->up);
1025 return NULL;
1028 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
1029 __isl_take struct isl_upoly *up2)
1031 if (!up1 || !up2)
1032 goto error;
1034 if (isl_upoly_is_nan(up1)) {
1035 isl_upoly_free(up2);
1036 return up1;
1039 if (isl_upoly_is_nan(up2)) {
1040 isl_upoly_free(up1);
1041 return up2;
1044 if (isl_upoly_is_zero(up1)) {
1045 isl_upoly_free(up2);
1046 return up1;
1049 if (isl_upoly_is_zero(up2)) {
1050 isl_upoly_free(up1);
1051 return up2;
1054 if (isl_upoly_is_one(up1)) {
1055 isl_upoly_free(up1);
1056 return up2;
1059 if (isl_upoly_is_one(up2)) {
1060 isl_upoly_free(up2);
1061 return up1;
1064 if (up1->var < up2->var)
1065 return isl_upoly_mul(up2, up1);
1067 if (up2->var < up1->var) {
1068 int i;
1069 struct isl_upoly_rec *rec;
1070 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
1071 isl_ctx *ctx = up1->ctx;
1072 isl_upoly_free(up1);
1073 isl_upoly_free(up2);
1074 return isl_upoly_nan(ctx);
1076 up1 = isl_upoly_cow(up1);
1077 rec = isl_upoly_as_rec(up1);
1078 if (!rec)
1079 goto error;
1081 for (i = 0; i < rec->n; ++i) {
1082 rec->p[i] = isl_upoly_mul(rec->p[i],
1083 isl_upoly_copy(up2));
1084 if (!rec->p[i])
1085 goto error;
1087 isl_upoly_free(up2);
1088 return up1;
1091 if (isl_upoly_is_cst(up1))
1092 return isl_upoly_mul_cst(up1, up2);
1094 return isl_upoly_mul_rec(up1, up2);
1095 error:
1096 isl_upoly_free(up1);
1097 isl_upoly_free(up2);
1098 return NULL;
1101 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1102 unsigned power)
1104 struct isl_upoly *res;
1106 if (!up)
1107 return NULL;
1108 if (power == 1)
1109 return up;
1111 if (power % 2)
1112 res = isl_upoly_copy(up);
1113 else
1114 res = isl_upoly_one(up->ctx);
1116 while (power >>= 1) {
1117 up = isl_upoly_mul(up, isl_upoly_copy(up));
1118 if (power % 2)
1119 res = isl_upoly_mul(res, isl_upoly_copy(up));
1122 isl_upoly_free(up);
1123 return res;
1126 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
1127 unsigned n_div, __isl_take struct isl_upoly *up)
1129 struct isl_qpolynomial *qp = NULL;
1130 unsigned total;
1132 if (!dim || !up)
1133 goto error;
1135 if (!isl_space_is_set(dim))
1136 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1137 "domain of polynomial should be a set", goto error);
1139 total = isl_space_dim(dim, isl_dim_all);
1141 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
1142 if (!qp)
1143 goto error;
1145 qp->ref = 1;
1146 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
1147 if (!qp->div)
1148 goto error;
1150 qp->dim = dim;
1151 qp->upoly = up;
1153 return qp;
1154 error:
1155 isl_space_free(dim);
1156 isl_upoly_free(up);
1157 isl_qpolynomial_free(qp);
1158 return NULL;
1161 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1163 if (!qp)
1164 return NULL;
1166 qp->ref++;
1167 return qp;
1170 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1172 struct isl_qpolynomial *dup;
1174 if (!qp)
1175 return NULL;
1177 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1178 isl_upoly_copy(qp->upoly));
1179 if (!dup)
1180 return NULL;
1181 isl_mat_free(dup->div);
1182 dup->div = isl_mat_copy(qp->div);
1183 if (!dup->div)
1184 goto error;
1186 return dup;
1187 error:
1188 isl_qpolynomial_free(dup);
1189 return NULL;
1192 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1194 if (!qp)
1195 return NULL;
1197 if (qp->ref == 1)
1198 return qp;
1199 qp->ref--;
1200 return isl_qpolynomial_dup(qp);
1203 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1204 __isl_take isl_qpolynomial *qp)
1206 if (!qp)
1207 return NULL;
1209 if (--qp->ref > 0)
1210 return NULL;
1212 isl_space_free(qp->dim);
1213 isl_mat_free(qp->div);
1214 isl_upoly_free(qp->upoly);
1216 free(qp);
1217 return NULL;
1220 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1222 int i;
1223 struct isl_upoly_rec *rec;
1224 struct isl_upoly_cst *cst;
1226 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1227 if (!rec)
1228 return NULL;
1229 for (i = 0; i < 1 + power; ++i) {
1230 rec->p[i] = isl_upoly_zero(ctx);
1231 if (!rec->p[i])
1232 goto error;
1233 rec->n++;
1235 cst = isl_upoly_as_cst(rec->p[power]);
1236 isl_int_set_si(cst->n, 1);
1238 return &rec->up;
1239 error:
1240 isl_upoly_free(&rec->up);
1241 return NULL;
1244 /* r array maps original positions to new positions.
1246 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1247 int *r)
1249 int i;
1250 struct isl_upoly_rec *rec;
1251 struct isl_upoly *base;
1252 struct isl_upoly *res;
1254 if (isl_upoly_is_cst(up))
1255 return up;
1257 rec = isl_upoly_as_rec(up);
1258 if (!rec)
1259 goto error;
1261 isl_assert(up->ctx, rec->n >= 1, goto error);
1263 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1264 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1266 for (i = rec->n - 2; i >= 0; --i) {
1267 res = isl_upoly_mul(res, isl_upoly_copy(base));
1268 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1271 isl_upoly_free(base);
1272 isl_upoly_free(up);
1274 return res;
1275 error:
1276 isl_upoly_free(up);
1277 return NULL;
1280 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1282 int n_row, n_col;
1283 int equal;
1285 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1286 div1->n_col >= div2->n_col, return -1);
1288 if (div1->n_row == div2->n_row)
1289 return isl_mat_is_equal(div1, div2);
1291 n_row = div1->n_row;
1292 n_col = div1->n_col;
1293 div1->n_row = div2->n_row;
1294 div1->n_col = div2->n_col;
1296 equal = isl_mat_is_equal(div1, div2);
1298 div1->n_row = n_row;
1299 div1->n_col = n_col;
1301 return equal;
1304 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1306 int li, lj;
1308 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1309 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1311 if (li != lj)
1312 return li - lj;
1314 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1317 struct isl_div_sort_info {
1318 isl_mat *div;
1319 int row;
1322 static int div_sort_cmp(const void *p1, const void *p2)
1324 const struct isl_div_sort_info *i1, *i2;
1325 i1 = (const struct isl_div_sort_info *) p1;
1326 i2 = (const struct isl_div_sort_info *) p2;
1328 return cmp_row(i1->div, i1->row, i2->row);
1331 /* Sort divs and remove duplicates.
1333 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1335 int i;
1336 int skip;
1337 int len;
1338 struct isl_div_sort_info *array = NULL;
1339 int *pos = NULL, *at = NULL;
1340 int *reordering = NULL;
1341 unsigned div_pos;
1343 if (!qp)
1344 return NULL;
1345 if (qp->div->n_row <= 1)
1346 return qp;
1348 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1350 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1351 qp->div->n_row);
1352 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1353 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1354 len = qp->div->n_col - 2;
1355 reordering = isl_alloc_array(qp->div->ctx, int, len);
1356 if (!array || !pos || !at || !reordering)
1357 goto error;
1359 for (i = 0; i < qp->div->n_row; ++i) {
1360 array[i].div = qp->div;
1361 array[i].row = i;
1362 pos[i] = i;
1363 at[i] = i;
1366 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1367 div_sort_cmp);
1369 for (i = 0; i < div_pos; ++i)
1370 reordering[i] = i;
1372 for (i = 0; i < qp->div->n_row; ++i) {
1373 if (pos[array[i].row] == i)
1374 continue;
1375 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1376 pos[at[i]] = pos[array[i].row];
1377 at[pos[array[i].row]] = at[i];
1378 at[i] = array[i].row;
1379 pos[array[i].row] = i;
1382 skip = 0;
1383 for (i = 0; i < len - div_pos; ++i) {
1384 if (i > 0 &&
1385 isl_seq_eq(qp->div->row[i - skip - 1],
1386 qp->div->row[i - skip], qp->div->n_col)) {
1387 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1388 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1389 2 + div_pos + i - skip);
1390 qp->div = isl_mat_drop_cols(qp->div,
1391 2 + div_pos + i - skip, 1);
1392 skip++;
1394 reordering[div_pos + array[i].row] = div_pos + i - skip;
1397 qp->upoly = reorder(qp->upoly, reordering);
1399 if (!qp->upoly || !qp->div)
1400 goto error;
1402 free(at);
1403 free(pos);
1404 free(array);
1405 free(reordering);
1407 return qp;
1408 error:
1409 free(at);
1410 free(pos);
1411 free(array);
1412 free(reordering);
1413 isl_qpolynomial_free(qp);
1414 return NULL;
1417 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1418 int *exp, int first)
1420 int i;
1421 struct isl_upoly_rec *rec;
1423 if (isl_upoly_is_cst(up))
1424 return up;
1426 if (up->var < first)
1427 return up;
1429 if (exp[up->var - first] == up->var - first)
1430 return up;
1432 up = isl_upoly_cow(up);
1433 if (!up)
1434 goto error;
1436 up->var = exp[up->var - first] + first;
1438 rec = isl_upoly_as_rec(up);
1439 if (!rec)
1440 goto error;
1442 for (i = 0; i < rec->n; ++i) {
1443 rec->p[i] = expand(rec->p[i], exp, first);
1444 if (!rec->p[i])
1445 goto error;
1448 return up;
1449 error:
1450 isl_upoly_free(up);
1451 return NULL;
1454 static __isl_give isl_qpolynomial *with_merged_divs(
1455 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1456 __isl_take isl_qpolynomial *qp2),
1457 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1459 int *exp1 = NULL;
1460 int *exp2 = NULL;
1461 isl_mat *div = NULL;
1462 int n_div1, n_div2;
1464 qp1 = isl_qpolynomial_cow(qp1);
1465 qp2 = isl_qpolynomial_cow(qp2);
1467 if (!qp1 || !qp2)
1468 goto error;
1470 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1471 qp1->div->n_col >= qp2->div->n_col, goto error);
1473 n_div1 = qp1->div->n_row;
1474 n_div2 = qp2->div->n_row;
1475 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1476 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1477 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1478 goto error;
1480 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1481 if (!div)
1482 goto error;
1484 isl_mat_free(qp1->div);
1485 qp1->div = isl_mat_copy(div);
1486 isl_mat_free(qp2->div);
1487 qp2->div = isl_mat_copy(div);
1489 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1490 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1492 if (!qp1->upoly || !qp2->upoly)
1493 goto error;
1495 isl_mat_free(div);
1496 free(exp1);
1497 free(exp2);
1499 return fn(qp1, qp2);
1500 error:
1501 isl_mat_free(div);
1502 free(exp1);
1503 free(exp2);
1504 isl_qpolynomial_free(qp1);
1505 isl_qpolynomial_free(qp2);
1506 return NULL;
1509 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1510 __isl_take isl_qpolynomial *qp2)
1512 qp1 = isl_qpolynomial_cow(qp1);
1514 if (!qp1 || !qp2)
1515 goto error;
1517 if (qp1->div->n_row < qp2->div->n_row)
1518 return isl_qpolynomial_add(qp2, qp1);
1520 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1521 if (!compatible_divs(qp1->div, qp2->div))
1522 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1524 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1525 if (!qp1->upoly)
1526 goto error;
1528 isl_qpolynomial_free(qp2);
1530 return qp1;
1531 error:
1532 isl_qpolynomial_free(qp1);
1533 isl_qpolynomial_free(qp2);
1534 return NULL;
1537 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1538 __isl_keep isl_set *dom,
1539 __isl_take isl_qpolynomial *qp1,
1540 __isl_take isl_qpolynomial *qp2)
1542 qp1 = isl_qpolynomial_add(qp1, qp2);
1543 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1544 return qp1;
1547 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1548 __isl_take isl_qpolynomial *qp2)
1550 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1553 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1554 __isl_take isl_qpolynomial *qp, isl_int v)
1556 if (isl_int_is_zero(v))
1557 return qp;
1559 qp = isl_qpolynomial_cow(qp);
1560 if (!qp)
1561 return NULL;
1563 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1564 if (!qp->upoly)
1565 goto error;
1567 return qp;
1568 error:
1569 isl_qpolynomial_free(qp);
1570 return NULL;
1574 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1576 if (!qp)
1577 return NULL;
1579 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1582 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1583 __isl_take isl_qpolynomial *qp, isl_int v)
1585 if (isl_int_is_one(v))
1586 return qp;
1588 if (qp && isl_int_is_zero(v)) {
1589 isl_qpolynomial *zero;
1590 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1591 isl_qpolynomial_free(qp);
1592 return zero;
1595 qp = isl_qpolynomial_cow(qp);
1596 if (!qp)
1597 return NULL;
1599 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1600 if (!qp->upoly)
1601 goto error;
1603 return qp;
1604 error:
1605 isl_qpolynomial_free(qp);
1606 return NULL;
1609 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1610 __isl_take isl_qpolynomial *qp, isl_int v)
1612 return isl_qpolynomial_mul_isl_int(qp, v);
1615 /* Multiply "qp" by "v".
1617 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1618 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1620 if (!qp || !v)
1621 goto error;
1623 if (!isl_val_is_rat(v))
1624 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1625 "expecting rational factor", goto error);
1627 if (isl_val_is_one(v)) {
1628 isl_val_free(v);
1629 return qp;
1632 if (isl_val_is_zero(v)) {
1633 isl_space *space;
1635 space = isl_qpolynomial_get_domain_space(qp);
1636 isl_qpolynomial_free(qp);
1637 isl_val_free(v);
1638 return isl_qpolynomial_zero_on_domain(space);
1641 qp = isl_qpolynomial_cow(qp);
1642 if (!qp)
1643 goto error;
1645 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1646 if (!qp->upoly)
1647 qp = isl_qpolynomial_free(qp);
1649 isl_val_free(v);
1650 return qp;
1651 error:
1652 isl_val_free(v);
1653 isl_qpolynomial_free(qp);
1654 return NULL;
1657 /* Divide "qp" by "v".
1659 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1660 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1662 if (!qp || !v)
1663 goto error;
1665 if (!isl_val_is_rat(v))
1666 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1667 "expecting rational factor", goto error);
1668 if (isl_val_is_zero(v))
1669 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1670 "cannot scale down by zero", goto error);
1672 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1673 error:
1674 isl_val_free(v);
1675 isl_qpolynomial_free(qp);
1676 return NULL;
1679 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1680 __isl_take isl_qpolynomial *qp2)
1682 qp1 = isl_qpolynomial_cow(qp1);
1684 if (!qp1 || !qp2)
1685 goto error;
1687 if (qp1->div->n_row < qp2->div->n_row)
1688 return isl_qpolynomial_mul(qp2, qp1);
1690 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1691 if (!compatible_divs(qp1->div, qp2->div))
1692 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1694 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1695 if (!qp1->upoly)
1696 goto error;
1698 isl_qpolynomial_free(qp2);
1700 return qp1;
1701 error:
1702 isl_qpolynomial_free(qp1);
1703 isl_qpolynomial_free(qp2);
1704 return NULL;
1707 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1708 unsigned power)
1710 qp = isl_qpolynomial_cow(qp);
1712 if (!qp)
1713 return NULL;
1715 qp->upoly = isl_upoly_pow(qp->upoly, power);
1716 if (!qp->upoly)
1717 goto error;
1719 return qp;
1720 error:
1721 isl_qpolynomial_free(qp);
1722 return NULL;
1725 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1726 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1728 int i;
1730 if (power == 1)
1731 return pwqp;
1733 pwqp = isl_pw_qpolynomial_cow(pwqp);
1734 if (!pwqp)
1735 return NULL;
1737 for (i = 0; i < pwqp->n; ++i) {
1738 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1739 if (!pwqp->p[i].qp)
1740 return isl_pw_qpolynomial_free(pwqp);
1743 return pwqp;
1746 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1747 __isl_take isl_space *dim)
1749 if (!dim)
1750 return NULL;
1751 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1754 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1755 __isl_take isl_space *dim)
1757 if (!dim)
1758 return NULL;
1759 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1762 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1763 __isl_take isl_space *dim)
1765 if (!dim)
1766 return NULL;
1767 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1770 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1771 __isl_take isl_space *dim)
1773 if (!dim)
1774 return NULL;
1775 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1778 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1779 __isl_take isl_space *dim)
1781 if (!dim)
1782 return NULL;
1783 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1786 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1787 __isl_take isl_space *dim,
1788 isl_int v)
1790 struct isl_qpolynomial *qp;
1791 struct isl_upoly_cst *cst;
1793 if (!dim)
1794 return NULL;
1796 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1797 if (!qp)
1798 return NULL;
1800 cst = isl_upoly_as_cst(qp->upoly);
1801 isl_int_set(cst->n, v);
1803 return qp;
1806 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1807 isl_int *n, isl_int *d)
1809 struct isl_upoly_cst *cst;
1811 if (!qp)
1812 return -1;
1814 if (!isl_upoly_is_cst(qp->upoly))
1815 return 0;
1817 cst = isl_upoly_as_cst(qp->upoly);
1818 if (!cst)
1819 return -1;
1821 if (n)
1822 isl_int_set(*n, cst->n);
1823 if (d)
1824 isl_int_set(*d, cst->d);
1826 return 1;
1829 /* Return the constant term of "up".
1831 static __isl_give isl_val *isl_upoly_get_constant_val(
1832 __isl_keep struct isl_upoly *up)
1834 struct isl_upoly_cst *cst;
1836 if (!up)
1837 return NULL;
1839 while (!isl_upoly_is_cst(up)) {
1840 struct isl_upoly_rec *rec;
1842 rec = isl_upoly_as_rec(up);
1843 if (!rec)
1844 return NULL;
1845 up = rec->p[0];
1848 cst = isl_upoly_as_cst(up);
1849 if (!cst)
1850 return NULL;
1851 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1854 /* Return the constant term of "qp".
1856 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1857 __isl_keep isl_qpolynomial *qp)
1859 if (!qp)
1860 return NULL;
1862 return isl_upoly_get_constant_val(qp->upoly);
1865 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1867 int is_cst;
1868 struct isl_upoly_rec *rec;
1870 if (!up)
1871 return -1;
1873 if (up->var < 0)
1874 return 1;
1876 rec = isl_upoly_as_rec(up);
1877 if (!rec)
1878 return -1;
1880 if (rec->n > 2)
1881 return 0;
1883 isl_assert(up->ctx, rec->n > 1, return -1);
1885 is_cst = isl_upoly_is_cst(rec->p[1]);
1886 if (is_cst < 0)
1887 return -1;
1888 if (!is_cst)
1889 return 0;
1891 return isl_upoly_is_affine(rec->p[0]);
1894 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1896 if (!qp)
1897 return -1;
1899 if (qp->div->n_row > 0)
1900 return 0;
1902 return isl_upoly_is_affine(qp->upoly);
1905 static void update_coeff(__isl_keep isl_vec *aff,
1906 __isl_keep struct isl_upoly_cst *cst, int pos)
1908 isl_int gcd;
1909 isl_int f;
1911 if (isl_int_is_zero(cst->n))
1912 return;
1914 isl_int_init(gcd);
1915 isl_int_init(f);
1916 isl_int_gcd(gcd, cst->d, aff->el[0]);
1917 isl_int_divexact(f, cst->d, gcd);
1918 isl_int_divexact(gcd, aff->el[0], gcd);
1919 isl_seq_scale(aff->el, aff->el, f, aff->size);
1920 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1921 isl_int_clear(gcd);
1922 isl_int_clear(f);
1925 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1926 __isl_keep isl_vec *aff)
1928 struct isl_upoly_cst *cst;
1929 struct isl_upoly_rec *rec;
1931 if (!up || !aff)
1932 return -1;
1934 if (up->var < 0) {
1935 struct isl_upoly_cst *cst;
1937 cst = isl_upoly_as_cst(up);
1938 if (!cst)
1939 return -1;
1940 update_coeff(aff, cst, 0);
1941 return 0;
1944 rec = isl_upoly_as_rec(up);
1945 if (!rec)
1946 return -1;
1947 isl_assert(up->ctx, rec->n == 2, return -1);
1949 cst = isl_upoly_as_cst(rec->p[1]);
1950 if (!cst)
1951 return -1;
1952 update_coeff(aff, cst, 1 + up->var);
1954 return isl_upoly_update_affine(rec->p[0], aff);
1957 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1958 __isl_keep isl_qpolynomial *qp)
1960 isl_vec *aff;
1961 unsigned d;
1963 if (!qp)
1964 return NULL;
1966 d = isl_space_dim(qp->dim, isl_dim_all);
1967 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1968 if (!aff)
1969 return NULL;
1971 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1972 isl_int_set_si(aff->el[0], 1);
1974 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1975 goto error;
1977 return aff;
1978 error:
1979 isl_vec_free(aff);
1980 return NULL;
1983 /* Compare two quasi-polynomials.
1985 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
1986 * than "qp2" and 0 if they are equal.
1988 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
1989 __isl_keep isl_qpolynomial *qp2)
1991 int cmp;
1993 if (qp1 == qp2)
1994 return 0;
1995 if (!qp1)
1996 return -1;
1997 if (!qp2)
1998 return 1;
2000 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2001 if (cmp != 0)
2002 return cmp;
2004 cmp = isl_local_cmp(qp1->div, qp2->div);
2005 if (cmp != 0)
2006 return cmp;
2008 return isl_upoly_plain_cmp(qp1->upoly, qp2->upoly);
2011 /* Is "qp1" obviously equal to "qp2"?
2013 * NaN is not equal to anything, not even to another NaN.
2015 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2016 __isl_keep isl_qpolynomial *qp2)
2018 isl_bool equal;
2020 if (!qp1 || !qp2)
2021 return isl_bool_error;
2023 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2024 return isl_bool_false;
2026 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2027 if (equal < 0 || !equal)
2028 return equal;
2030 equal = isl_mat_is_equal(qp1->div, qp2->div);
2031 if (equal < 0 || !equal)
2032 return equal;
2034 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
2037 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
2039 int i;
2040 struct isl_upoly_rec *rec;
2042 if (isl_upoly_is_cst(up)) {
2043 struct isl_upoly_cst *cst;
2044 cst = isl_upoly_as_cst(up);
2045 if (!cst)
2046 return;
2047 isl_int_lcm(*d, *d, cst->d);
2048 return;
2051 rec = isl_upoly_as_rec(up);
2052 if (!rec)
2053 return;
2055 for (i = 0; i < rec->n; ++i)
2056 upoly_update_den(rec->p[i], d);
2059 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
2061 isl_int_set_si(*d, 1);
2062 if (!qp)
2063 return;
2064 upoly_update_den(qp->upoly, d);
2067 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2068 __isl_take isl_space *dim, int pos, int power)
2070 struct isl_ctx *ctx;
2072 if (!dim)
2073 return NULL;
2075 ctx = dim->ctx;
2077 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
2080 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(__isl_take isl_space *dim,
2081 enum isl_dim_type type, unsigned pos)
2083 if (!dim)
2084 return NULL;
2086 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
2087 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
2089 if (type == isl_dim_set)
2090 pos += isl_space_dim(dim, isl_dim_param);
2092 return isl_qpolynomial_var_pow_on_domain(dim, pos, 1);
2093 error:
2094 isl_space_free(dim);
2095 return NULL;
2098 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
2099 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
2101 int i;
2102 struct isl_upoly_rec *rec;
2103 struct isl_upoly *base, *res;
2105 if (!up)
2106 return NULL;
2108 if (isl_upoly_is_cst(up))
2109 return up;
2111 if (up->var < first)
2112 return up;
2114 rec = isl_upoly_as_rec(up);
2115 if (!rec)
2116 goto error;
2118 isl_assert(up->ctx, rec->n >= 1, goto error);
2120 if (up->var >= first + n)
2121 base = isl_upoly_var_pow(up->ctx, up->var, 1);
2122 else
2123 base = isl_upoly_copy(subs[up->var - first]);
2125 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
2126 for (i = rec->n - 2; i >= 0; --i) {
2127 struct isl_upoly *t;
2128 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
2129 res = isl_upoly_mul(res, isl_upoly_copy(base));
2130 res = isl_upoly_sum(res, t);
2133 isl_upoly_free(base);
2134 isl_upoly_free(up);
2136 return res;
2137 error:
2138 isl_upoly_free(up);
2139 return NULL;
2142 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
2143 isl_int denom, unsigned len)
2145 int i;
2146 struct isl_upoly *up;
2148 isl_assert(ctx, len >= 1, return NULL);
2150 up = isl_upoly_rat_cst(ctx, f[0], denom);
2151 for (i = 0; i < len - 1; ++i) {
2152 struct isl_upoly *t;
2153 struct isl_upoly *c;
2155 if (isl_int_is_zero(f[1 + i]))
2156 continue;
2158 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2159 t = isl_upoly_var_pow(ctx, i, 1);
2160 t = isl_upoly_mul(c, t);
2161 up = isl_upoly_sum(up, t);
2164 return up;
2167 /* Remove common factor of non-constant terms and denominator.
2169 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2171 isl_ctx *ctx = qp->div->ctx;
2172 unsigned total = qp->div->n_col - 2;
2174 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2175 isl_int_gcd(ctx->normalize_gcd,
2176 ctx->normalize_gcd, qp->div->row[div][0]);
2177 if (isl_int_is_one(ctx->normalize_gcd))
2178 return;
2180 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2181 ctx->normalize_gcd, total);
2182 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2183 ctx->normalize_gcd);
2184 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2185 ctx->normalize_gcd);
2188 /* Replace the integer division identified by "div" by the polynomial "s".
2189 * The integer division is assumed not to appear in the definition
2190 * of any other integer divisions.
2192 static __isl_give isl_qpolynomial *substitute_div(
2193 __isl_take isl_qpolynomial *qp,
2194 int div, __isl_take struct isl_upoly *s)
2196 int i;
2197 int total;
2198 int *reordering;
2200 if (!qp || !s)
2201 goto error;
2203 qp = isl_qpolynomial_cow(qp);
2204 if (!qp)
2205 goto error;
2207 total = isl_space_dim(qp->dim, isl_dim_all);
2208 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2209 if (!qp->upoly)
2210 goto error;
2212 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2213 if (!reordering)
2214 goto error;
2215 for (i = 0; i < total + div; ++i)
2216 reordering[i] = i;
2217 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2218 reordering[i] = i - 1;
2219 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2220 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2221 qp->upoly = reorder(qp->upoly, reordering);
2222 free(reordering);
2224 if (!qp->upoly || !qp->div)
2225 goto error;
2227 isl_upoly_free(s);
2228 return qp;
2229 error:
2230 isl_qpolynomial_free(qp);
2231 isl_upoly_free(s);
2232 return NULL;
2235 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2236 * divisions because d is equal to 1 by their definition, i.e., e.
2238 static __isl_give isl_qpolynomial *substitute_non_divs(
2239 __isl_take isl_qpolynomial *qp)
2241 int i, j;
2242 int total;
2243 struct isl_upoly *s;
2245 if (!qp)
2246 return NULL;
2248 total = isl_space_dim(qp->dim, isl_dim_all);
2249 for (i = 0; qp && i < qp->div->n_row; ++i) {
2250 if (!isl_int_is_one(qp->div->row[i][0]))
2251 continue;
2252 for (j = i + 1; j < qp->div->n_row; ++j) {
2253 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2254 continue;
2255 isl_seq_combine(qp->div->row[j] + 1,
2256 qp->div->ctx->one, qp->div->row[j] + 1,
2257 qp->div->row[j][2 + total + i],
2258 qp->div->row[i] + 1, 1 + total + i);
2259 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2260 normalize_div(qp, j);
2262 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2263 qp->div->row[i][0], qp->div->n_col - 1);
2264 qp = substitute_div(qp, i, s);
2265 --i;
2268 return qp;
2271 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2272 * with d the denominator. When replacing the coefficient e of x by
2273 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2274 * inside the division, so we need to add floor(e/d) * x outside.
2275 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2276 * to adjust the coefficient of x in each later div that depends on the
2277 * current div "div" and also in the affine expressions in the rows of "mat"
2278 * (if they too depend on "div").
2280 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2281 __isl_keep isl_mat **mat)
2283 int i, j;
2284 isl_int v;
2285 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2287 isl_int_init(v);
2288 for (i = 0; i < 1 + total + div; ++i) {
2289 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2290 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2291 continue;
2292 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2293 isl_int_fdiv_r(qp->div->row[div][1 + i],
2294 qp->div->row[div][1 + i], qp->div->row[div][0]);
2295 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2296 for (j = div + 1; j < qp->div->n_row; ++j) {
2297 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2298 continue;
2299 isl_int_addmul(qp->div->row[j][1 + i],
2300 v, qp->div->row[j][2 + total + div]);
2303 isl_int_clear(v);
2306 /* Check if the last non-zero coefficient is bigger that half of the
2307 * denominator. If so, we will invert the div to further reduce the number
2308 * of distinct divs that may appear.
2309 * If the last non-zero coefficient is exactly half the denominator,
2310 * then we continue looking for earlier coefficients that are bigger
2311 * than half the denominator.
2313 static int needs_invert(__isl_keep isl_mat *div, int row)
2315 int i;
2316 int cmp;
2318 for (i = div->n_col - 1; i >= 1; --i) {
2319 if (isl_int_is_zero(div->row[row][i]))
2320 continue;
2321 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2322 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2323 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2324 if (cmp)
2325 return cmp > 0;
2326 if (i == 1)
2327 return 1;
2330 return 0;
2333 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2334 * We only invert the coefficients of e (and the coefficient of q in
2335 * later divs and in the rows of "mat"). After calling this function, the
2336 * coefficients of e should be reduced again.
2338 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2339 __isl_keep isl_mat **mat)
2341 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2343 isl_seq_neg(qp->div->row[div] + 1,
2344 qp->div->row[div] + 1, qp->div->n_col - 1);
2345 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2346 isl_int_add(qp->div->row[div][1],
2347 qp->div->row[div][1], qp->div->row[div][0]);
2348 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2349 isl_mat_col_mul(qp->div, 2 + total + div,
2350 qp->div->ctx->negone, 2 + total + div);
2353 /* Reduce all divs of "qp" to have coefficients
2354 * in the interval [0, d-1], with d the denominator and such that the
2355 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2356 * The modifications to the integer divisions need to be reflected
2357 * in the factors of the polynomial that refer to the original
2358 * integer divisions. To this end, the modifications are collected
2359 * as a set of affine expressions and then plugged into the polynomial.
2361 * After the reduction, some divs may have become redundant or identical,
2362 * so we call substitute_non_divs and sort_divs. If these functions
2363 * eliminate divs or merge two or more divs into one, the coefficients
2364 * of the enclosing divs may have to be reduced again, so we call
2365 * ourselves recursively if the number of divs decreases.
2367 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2369 int i;
2370 isl_ctx *ctx;
2371 isl_mat *mat;
2372 struct isl_upoly **s;
2373 unsigned o_div, n_div, total;
2375 if (!qp)
2376 return NULL;
2378 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2379 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2380 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2381 ctx = isl_qpolynomial_get_ctx(qp);
2382 mat = isl_mat_zero(ctx, n_div, 1 + total);
2384 for (i = 0; i < n_div; ++i)
2385 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2387 for (i = 0; i < qp->div->n_row; ++i) {
2388 normalize_div(qp, i);
2389 reduce_div(qp, i, &mat);
2390 if (needs_invert(qp->div, i)) {
2391 invert_div(qp, i, &mat);
2392 reduce_div(qp, i, &mat);
2395 if (!mat)
2396 goto error;
2398 s = isl_alloc_array(ctx, struct isl_upoly *, n_div);
2399 if (n_div && !s)
2400 goto error;
2401 for (i = 0; i < n_div; ++i)
2402 s[i] = isl_upoly_from_affine(ctx, mat->row[i], ctx->one,
2403 1 + total);
2404 qp->upoly = isl_upoly_subs(qp->upoly, o_div - 1, n_div, s);
2405 for (i = 0; i < n_div; ++i)
2406 isl_upoly_free(s[i]);
2407 free(s);
2408 if (!qp->upoly)
2409 goto error;
2411 isl_mat_free(mat);
2413 qp = substitute_non_divs(qp);
2414 qp = sort_divs(qp);
2415 if (qp && isl_qpolynomial_domain_dim(qp, isl_dim_div) < n_div)
2416 return reduce_divs(qp);
2418 return qp;
2419 error:
2420 isl_qpolynomial_free(qp);
2421 isl_mat_free(mat);
2422 return NULL;
2425 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2426 __isl_take isl_space *dim, const isl_int n, const isl_int d)
2428 struct isl_qpolynomial *qp;
2429 struct isl_upoly_cst *cst;
2431 if (!dim)
2432 return NULL;
2434 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2435 if (!qp)
2436 return NULL;
2438 cst = isl_upoly_as_cst(qp->upoly);
2439 isl_int_set(cst->n, n);
2440 isl_int_set(cst->d, d);
2442 return qp;
2445 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2447 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2448 __isl_take isl_space *domain, __isl_take isl_val *val)
2450 isl_qpolynomial *qp;
2451 struct isl_upoly_cst *cst;
2453 if (!domain || !val)
2454 goto error;
2456 qp = isl_qpolynomial_alloc(isl_space_copy(domain), 0,
2457 isl_upoly_zero(domain->ctx));
2458 if (!qp)
2459 goto error;
2461 cst = isl_upoly_as_cst(qp->upoly);
2462 isl_int_set(cst->n, val->n);
2463 isl_int_set(cst->d, val->d);
2465 isl_space_free(domain);
2466 isl_val_free(val);
2467 return qp;
2468 error:
2469 isl_space_free(domain);
2470 isl_val_free(val);
2471 return NULL;
2474 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2476 struct isl_upoly_rec *rec;
2477 int i;
2479 if (!up)
2480 return -1;
2482 if (isl_upoly_is_cst(up))
2483 return 0;
2485 if (up->var < d)
2486 active[up->var] = 1;
2488 rec = isl_upoly_as_rec(up);
2489 for (i = 0; i < rec->n; ++i)
2490 if (up_set_active(rec->p[i], active, d) < 0)
2491 return -1;
2493 return 0;
2496 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2498 int i, j;
2499 int d = isl_space_dim(qp->dim, isl_dim_all);
2501 if (!qp || !active)
2502 return -1;
2504 for (i = 0; i < d; ++i)
2505 for (j = 0; j < qp->div->n_row; ++j) {
2506 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2507 continue;
2508 active[i] = 1;
2509 break;
2512 return up_set_active(qp->upoly, active, d);
2515 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2516 enum isl_dim_type type, unsigned first, unsigned n)
2518 int i;
2519 int *active = NULL;
2520 isl_bool involves = isl_bool_false;
2522 if (!qp)
2523 return isl_bool_error;
2524 if (n == 0)
2525 return isl_bool_false;
2527 isl_assert(qp->dim->ctx,
2528 first + n <= isl_qpolynomial_dim(qp, type),
2529 return isl_bool_error);
2530 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2531 type == isl_dim_in, return isl_bool_error);
2533 active = isl_calloc_array(qp->dim->ctx, int,
2534 isl_space_dim(qp->dim, isl_dim_all));
2535 if (set_active(qp, active) < 0)
2536 goto error;
2538 if (type == isl_dim_in)
2539 first += isl_space_dim(qp->dim, isl_dim_param);
2540 for (i = 0; i < n; ++i)
2541 if (active[first + i]) {
2542 involves = isl_bool_true;
2543 break;
2546 free(active);
2548 return involves;
2549 error:
2550 free(active);
2551 return isl_bool_error;
2554 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2555 * of the divs that do appear in the quasi-polynomial.
2557 static __isl_give isl_qpolynomial *remove_redundant_divs(
2558 __isl_take isl_qpolynomial *qp)
2560 int i, j;
2561 int d;
2562 int len;
2563 int skip;
2564 int *active = NULL;
2565 int *reordering = NULL;
2566 int redundant = 0;
2567 int n_div;
2568 isl_ctx *ctx;
2570 if (!qp)
2571 return NULL;
2572 if (qp->div->n_row == 0)
2573 return qp;
2575 d = isl_space_dim(qp->dim, isl_dim_all);
2576 len = qp->div->n_col - 2;
2577 ctx = isl_qpolynomial_get_ctx(qp);
2578 active = isl_calloc_array(ctx, int, len);
2579 if (!active)
2580 goto error;
2582 if (up_set_active(qp->upoly, active, len) < 0)
2583 goto error;
2585 for (i = qp->div->n_row - 1; i >= 0; --i) {
2586 if (!active[d + i]) {
2587 redundant = 1;
2588 continue;
2590 for (j = 0; j < i; ++j) {
2591 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2592 continue;
2593 active[d + j] = 1;
2594 break;
2598 if (!redundant) {
2599 free(active);
2600 return qp;
2603 reordering = isl_alloc_array(qp->div->ctx, int, len);
2604 if (!reordering)
2605 goto error;
2607 for (i = 0; i < d; ++i)
2608 reordering[i] = i;
2610 skip = 0;
2611 n_div = qp->div->n_row;
2612 for (i = 0; i < n_div; ++i) {
2613 if (!active[d + i]) {
2614 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2615 qp->div = isl_mat_drop_cols(qp->div,
2616 2 + d + i - skip, 1);
2617 skip++;
2619 reordering[d + i] = d + i - skip;
2622 qp->upoly = reorder(qp->upoly, reordering);
2624 if (!qp->upoly || !qp->div)
2625 goto error;
2627 free(active);
2628 free(reordering);
2630 return qp;
2631 error:
2632 free(active);
2633 free(reordering);
2634 isl_qpolynomial_free(qp);
2635 return NULL;
2638 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2639 unsigned first, unsigned n)
2641 int i;
2642 struct isl_upoly_rec *rec;
2644 if (!up)
2645 return NULL;
2646 if (n == 0 || up->var < 0 || up->var < first)
2647 return up;
2648 if (up->var < first + n) {
2649 up = replace_by_constant_term(up);
2650 return isl_upoly_drop(up, first, n);
2652 up = isl_upoly_cow(up);
2653 if (!up)
2654 return NULL;
2655 up->var -= n;
2656 rec = isl_upoly_as_rec(up);
2657 if (!rec)
2658 goto error;
2660 for (i = 0; i < rec->n; ++i) {
2661 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2662 if (!rec->p[i])
2663 goto error;
2666 return up;
2667 error:
2668 isl_upoly_free(up);
2669 return NULL;
2672 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2673 __isl_take isl_qpolynomial *qp,
2674 enum isl_dim_type type, unsigned pos, const char *s)
2676 qp = isl_qpolynomial_cow(qp);
2677 if (!qp)
2678 return NULL;
2679 if (type == isl_dim_out)
2680 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2681 "cannot set name of output/set dimension",
2682 return isl_qpolynomial_free(qp));
2683 if (type == isl_dim_in)
2684 type = isl_dim_set;
2685 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2686 if (!qp->dim)
2687 goto error;
2688 return qp;
2689 error:
2690 isl_qpolynomial_free(qp);
2691 return NULL;
2694 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2695 __isl_take isl_qpolynomial *qp,
2696 enum isl_dim_type type, unsigned first, unsigned n)
2698 if (!qp)
2699 return NULL;
2700 if (type == isl_dim_out)
2701 isl_die(qp->dim->ctx, isl_error_invalid,
2702 "cannot drop output/set dimension",
2703 goto error);
2704 if (type == isl_dim_in)
2705 type = isl_dim_set;
2706 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2707 return qp;
2709 qp = isl_qpolynomial_cow(qp);
2710 if (!qp)
2711 return NULL;
2713 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2714 goto error);
2715 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2716 type == isl_dim_set, goto error);
2718 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2719 if (!qp->dim)
2720 goto error;
2722 if (type == isl_dim_set)
2723 first += isl_space_dim(qp->dim, isl_dim_param);
2725 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2726 if (!qp->div)
2727 goto error;
2729 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2730 if (!qp->upoly)
2731 goto error;
2733 return qp;
2734 error:
2735 isl_qpolynomial_free(qp);
2736 return NULL;
2739 /* Project the domain of the quasi-polynomial onto its parameter space.
2740 * The quasi-polynomial may not involve any of the domain dimensions.
2742 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2743 __isl_take isl_qpolynomial *qp)
2745 isl_space *space;
2746 unsigned n;
2747 int involves;
2749 n = isl_qpolynomial_dim(qp, isl_dim_in);
2750 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2751 if (involves < 0)
2752 return isl_qpolynomial_free(qp);
2753 if (involves)
2754 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2755 "polynomial involves some of the domain dimensions",
2756 return isl_qpolynomial_free(qp));
2757 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2758 space = isl_qpolynomial_get_domain_space(qp);
2759 space = isl_space_params(space);
2760 qp = isl_qpolynomial_reset_domain_space(qp, space);
2761 return qp;
2764 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2765 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2767 int i, j, k;
2768 isl_int denom;
2769 unsigned total;
2770 unsigned n_div;
2771 struct isl_upoly *up;
2773 if (!eq)
2774 goto error;
2775 if (eq->n_eq == 0) {
2776 isl_basic_set_free(eq);
2777 return qp;
2780 qp = isl_qpolynomial_cow(qp);
2781 if (!qp)
2782 goto error;
2783 qp->div = isl_mat_cow(qp->div);
2784 if (!qp->div)
2785 goto error;
2787 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2788 n_div = eq->n_div;
2789 isl_int_init(denom);
2790 for (i = 0; i < eq->n_eq; ++i) {
2791 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2792 if (j < 0 || j == 0 || j >= total)
2793 continue;
2795 for (k = 0; k < qp->div->n_row; ++k) {
2796 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2797 continue;
2798 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2799 &qp->div->row[k][0]);
2800 normalize_div(qp, k);
2803 if (isl_int_is_pos(eq->eq[i][j]))
2804 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2805 isl_int_abs(denom, eq->eq[i][j]);
2806 isl_int_set_si(eq->eq[i][j], 0);
2808 up = isl_upoly_from_affine(qp->dim->ctx,
2809 eq->eq[i], denom, total);
2810 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2811 isl_upoly_free(up);
2813 isl_int_clear(denom);
2815 if (!qp->upoly)
2816 goto error;
2818 isl_basic_set_free(eq);
2820 qp = substitute_non_divs(qp);
2821 qp = sort_divs(qp);
2823 return qp;
2824 error:
2825 isl_basic_set_free(eq);
2826 isl_qpolynomial_free(qp);
2827 return NULL;
2830 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2832 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2833 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2835 if (!qp || !eq)
2836 goto error;
2837 if (qp->div->n_row > 0)
2838 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2839 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2840 error:
2841 isl_basic_set_free(eq);
2842 isl_qpolynomial_free(qp);
2843 return NULL;
2846 static __isl_give isl_basic_set *add_div_constraints(
2847 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2849 int i;
2850 unsigned total;
2852 if (!bset || !div)
2853 goto error;
2855 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2856 if (!bset)
2857 goto error;
2858 total = isl_basic_set_total_dim(bset);
2859 for (i = 0; i < div->n_row; ++i)
2860 if (isl_basic_set_add_div_constraints_var(bset,
2861 total - div->n_row + i, div->row[i]) < 0)
2862 goto error;
2864 isl_mat_free(div);
2865 return bset;
2866 error:
2867 isl_mat_free(div);
2868 isl_basic_set_free(bset);
2869 return NULL;
2872 /* Look for equalities among the variables shared by context and qp
2873 * and the integer divisions of qp, if any.
2874 * The equalities are then used to eliminate variables and/or integer
2875 * divisions from qp.
2877 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2878 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2880 isl_basic_set *aff;
2882 if (!qp)
2883 goto error;
2884 if (qp->div->n_row > 0) {
2885 isl_basic_set *bset;
2886 context = isl_set_add_dims(context, isl_dim_set,
2887 qp->div->n_row);
2888 bset = isl_basic_set_universe(isl_set_get_space(context));
2889 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2890 context = isl_set_intersect(context,
2891 isl_set_from_basic_set(bset));
2894 aff = isl_set_affine_hull(context);
2895 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2896 error:
2897 isl_qpolynomial_free(qp);
2898 isl_set_free(context);
2899 return NULL;
2902 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2903 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2905 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2906 isl_set *dom_context = isl_set_universe(space);
2907 dom_context = isl_set_intersect_params(dom_context, context);
2908 return isl_qpolynomial_gist(qp, dom_context);
2911 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2912 __isl_take isl_qpolynomial *qp)
2914 isl_set *dom;
2916 if (!qp)
2917 return NULL;
2918 if (isl_qpolynomial_is_zero(qp)) {
2919 isl_space *dim = isl_qpolynomial_get_space(qp);
2920 isl_qpolynomial_free(qp);
2921 return isl_pw_qpolynomial_zero(dim);
2924 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2925 return isl_pw_qpolynomial_alloc(dom, qp);
2928 #undef PW
2929 #define PW isl_pw_qpolynomial
2930 #undef EL
2931 #define EL isl_qpolynomial
2932 #undef EL_IS_ZERO
2933 #define EL_IS_ZERO is_zero
2934 #undef ZERO
2935 #define ZERO zero
2936 #undef IS_ZERO
2937 #define IS_ZERO is_zero
2938 #undef FIELD
2939 #define FIELD qp
2940 #undef DEFAULT_IS_ZERO
2941 #define DEFAULT_IS_ZERO 1
2943 #define NO_PULLBACK
2945 #include <isl_pw_templ.c>
2947 #undef UNION
2948 #define UNION isl_union_pw_qpolynomial
2949 #undef PART
2950 #define PART isl_pw_qpolynomial
2951 #undef PARTS
2952 #define PARTS pw_qpolynomial
2954 #include <isl_union_single.c>
2955 #include <isl_union_eval.c>
2956 #include <isl_union_neg.c>
2958 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2960 if (!pwqp)
2961 return -1;
2963 if (pwqp->n != -1)
2964 return 0;
2966 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2967 return 0;
2969 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2972 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2973 __isl_take isl_pw_qpolynomial *pwqp1,
2974 __isl_take isl_pw_qpolynomial *pwqp2)
2976 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2979 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2980 __isl_take isl_pw_qpolynomial *pwqp1,
2981 __isl_take isl_pw_qpolynomial *pwqp2)
2983 int i, j, n;
2984 struct isl_pw_qpolynomial *res;
2986 if (!pwqp1 || !pwqp2)
2987 goto error;
2989 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
2990 goto error);
2992 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2993 isl_pw_qpolynomial_free(pwqp2);
2994 return pwqp1;
2997 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2998 isl_pw_qpolynomial_free(pwqp1);
2999 return pwqp2;
3002 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3003 isl_pw_qpolynomial_free(pwqp1);
3004 return pwqp2;
3007 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3008 isl_pw_qpolynomial_free(pwqp2);
3009 return pwqp1;
3012 n = pwqp1->n * pwqp2->n;
3013 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3015 for (i = 0; i < pwqp1->n; ++i) {
3016 for (j = 0; j < pwqp2->n; ++j) {
3017 struct isl_set *common;
3018 struct isl_qpolynomial *prod;
3019 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3020 isl_set_copy(pwqp2->p[j].set));
3021 if (isl_set_plain_is_empty(common)) {
3022 isl_set_free(common);
3023 continue;
3026 prod = isl_qpolynomial_mul(
3027 isl_qpolynomial_copy(pwqp1->p[i].qp),
3028 isl_qpolynomial_copy(pwqp2->p[j].qp));
3030 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3034 isl_pw_qpolynomial_free(pwqp1);
3035 isl_pw_qpolynomial_free(pwqp2);
3037 return res;
3038 error:
3039 isl_pw_qpolynomial_free(pwqp1);
3040 isl_pw_qpolynomial_free(pwqp2);
3041 return NULL;
3044 __isl_give isl_val *isl_upoly_eval(__isl_take struct isl_upoly *up,
3045 __isl_take isl_vec *vec)
3047 int i;
3048 struct isl_upoly_rec *rec;
3049 isl_val *res;
3050 isl_val *base;
3052 if (isl_upoly_is_cst(up)) {
3053 isl_vec_free(vec);
3054 res = isl_upoly_get_constant_val(up);
3055 isl_upoly_free(up);
3056 return res;
3059 rec = isl_upoly_as_rec(up);
3060 if (!rec)
3061 goto error;
3063 isl_assert(up->ctx, rec->n >= 1, goto error);
3065 base = isl_val_rat_from_isl_int(up->ctx,
3066 vec->el[1 + up->var], vec->el[0]);
3068 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
3069 isl_vec_copy(vec));
3071 for (i = rec->n - 2; i >= 0; --i) {
3072 res = isl_val_mul(res, isl_val_copy(base));
3073 res = isl_val_add(res,
3074 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
3075 isl_vec_copy(vec)));
3078 isl_val_free(base);
3079 isl_upoly_free(up);
3080 isl_vec_free(vec);
3081 return res;
3082 error:
3083 isl_upoly_free(up);
3084 isl_vec_free(vec);
3085 return NULL;
3088 /* Evaluate "qp" in the void point "pnt".
3089 * In particular, return the value NaN.
3091 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3092 __isl_take isl_point *pnt)
3094 isl_ctx *ctx;
3096 ctx = isl_point_get_ctx(pnt);
3097 isl_qpolynomial_free(qp);
3098 isl_point_free(pnt);
3099 return isl_val_nan(ctx);
3102 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3103 __isl_take isl_point *pnt)
3105 isl_bool is_void;
3106 isl_vec *ext;
3107 isl_val *v;
3109 if (!qp || !pnt)
3110 goto error;
3111 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3112 is_void = isl_point_is_void(pnt);
3113 if (is_void < 0)
3114 goto error;
3115 if (is_void)
3116 return eval_void(qp, pnt);
3118 if (qp->div->n_row == 0)
3119 ext = isl_vec_copy(pnt->vec);
3120 else {
3121 int i;
3122 unsigned dim = isl_space_dim(qp->dim, isl_dim_all);
3123 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
3124 if (!ext)
3125 goto error;
3127 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
3128 for (i = 0; i < qp->div->n_row; ++i) {
3129 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
3130 1 + dim + i, &ext->el[1+dim+i]);
3131 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
3132 qp->div->row[i][0]);
3136 v = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
3138 isl_qpolynomial_free(qp);
3139 isl_point_free(pnt);
3141 return v;
3142 error:
3143 isl_qpolynomial_free(qp);
3144 isl_point_free(pnt);
3145 return NULL;
3148 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
3149 __isl_keep struct isl_upoly_cst *cst2)
3151 int cmp;
3152 isl_int t;
3153 isl_int_init(t);
3154 isl_int_mul(t, cst1->n, cst2->d);
3155 isl_int_submul(t, cst2->n, cst1->d);
3156 cmp = isl_int_sgn(t);
3157 isl_int_clear(t);
3158 return cmp;
3161 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3162 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3163 unsigned first, unsigned n)
3165 unsigned total;
3166 unsigned g_pos;
3167 int *exp;
3169 if (!qp)
3170 return NULL;
3171 if (type == isl_dim_out)
3172 isl_die(qp->div->ctx, isl_error_invalid,
3173 "cannot insert output/set dimensions",
3174 goto error);
3175 if (type == isl_dim_in)
3176 type = isl_dim_set;
3177 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3178 return qp;
3180 qp = isl_qpolynomial_cow(qp);
3181 if (!qp)
3182 return NULL;
3184 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
3185 goto error);
3187 g_pos = pos(qp->dim, type) + first;
3189 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3190 if (!qp->div)
3191 goto error;
3193 total = qp->div->n_col - 2;
3194 if (total > g_pos) {
3195 int i;
3196 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3197 if (!exp)
3198 goto error;
3199 for (i = 0; i < total - g_pos; ++i)
3200 exp[i] = i + n;
3201 qp->upoly = expand(qp->upoly, exp, g_pos);
3202 free(exp);
3203 if (!qp->upoly)
3204 goto error;
3207 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3208 if (!qp->dim)
3209 goto error;
3211 return qp;
3212 error:
3213 isl_qpolynomial_free(qp);
3214 return NULL;
3217 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3218 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3220 unsigned pos;
3222 pos = isl_qpolynomial_dim(qp, type);
3224 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3227 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3228 __isl_take isl_pw_qpolynomial *pwqp,
3229 enum isl_dim_type type, unsigned n)
3231 unsigned pos;
3233 pos = isl_pw_qpolynomial_dim(pwqp, type);
3235 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3238 static int *reordering_move(isl_ctx *ctx,
3239 unsigned len, unsigned dst, unsigned src, unsigned n)
3241 int i;
3242 int *reordering;
3244 reordering = isl_alloc_array(ctx, int, len);
3245 if (!reordering)
3246 return NULL;
3248 if (dst <= src) {
3249 for (i = 0; i < dst; ++i)
3250 reordering[i] = i;
3251 for (i = 0; i < n; ++i)
3252 reordering[src + i] = dst + i;
3253 for (i = 0; i < src - dst; ++i)
3254 reordering[dst + i] = dst + n + i;
3255 for (i = 0; i < len - src - n; ++i)
3256 reordering[src + n + i] = src + n + i;
3257 } else {
3258 for (i = 0; i < src; ++i)
3259 reordering[i] = i;
3260 for (i = 0; i < n; ++i)
3261 reordering[src + i] = dst + i;
3262 for (i = 0; i < dst - src; ++i)
3263 reordering[src + n + i] = src + i;
3264 for (i = 0; i < len - dst - n; ++i)
3265 reordering[dst + n + i] = dst + n + i;
3268 return reordering;
3271 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3272 __isl_take isl_qpolynomial *qp,
3273 enum isl_dim_type dst_type, unsigned dst_pos,
3274 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3276 unsigned g_dst_pos;
3277 unsigned g_src_pos;
3278 int *reordering;
3280 if (!qp)
3281 return NULL;
3283 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3284 isl_die(qp->dim->ctx, isl_error_invalid,
3285 "cannot move output/set dimension",
3286 goto error);
3287 if (dst_type == isl_dim_in)
3288 dst_type = isl_dim_set;
3289 if (src_type == isl_dim_in)
3290 src_type = isl_dim_set;
3292 if (n == 0 &&
3293 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3294 !isl_space_is_named_or_nested(qp->dim, dst_type))
3295 return qp;
3297 qp = isl_qpolynomial_cow(qp);
3298 if (!qp)
3299 return NULL;
3301 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
3302 goto error);
3304 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3305 g_src_pos = pos(qp->dim, src_type) + src_pos;
3306 if (dst_type > src_type)
3307 g_dst_pos -= n;
3309 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3310 if (!qp->div)
3311 goto error;
3312 qp = sort_divs(qp);
3313 if (!qp)
3314 goto error;
3316 reordering = reordering_move(qp->dim->ctx,
3317 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3318 if (!reordering)
3319 goto error;
3321 qp->upoly = reorder(qp->upoly, reordering);
3322 free(reordering);
3323 if (!qp->upoly)
3324 goto error;
3326 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3327 if (!qp->dim)
3328 goto error;
3330 return qp;
3331 error:
3332 isl_qpolynomial_free(qp);
3333 return NULL;
3336 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3337 isl_int *f, isl_int denom)
3339 struct isl_upoly *up;
3341 dim = isl_space_domain(dim);
3342 if (!dim)
3343 return NULL;
3345 up = isl_upoly_from_affine(dim->ctx, f, denom,
3346 1 + isl_space_dim(dim, isl_dim_all));
3348 return isl_qpolynomial_alloc(dim, 0, up);
3351 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3353 isl_ctx *ctx;
3354 struct isl_upoly *up;
3355 isl_qpolynomial *qp;
3357 if (!aff)
3358 return NULL;
3360 ctx = isl_aff_get_ctx(aff);
3361 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3362 aff->v->size - 1);
3364 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3365 aff->ls->div->n_row, up);
3366 if (!qp)
3367 goto error;
3369 isl_mat_free(qp->div);
3370 qp->div = isl_mat_copy(aff->ls->div);
3371 qp->div = isl_mat_cow(qp->div);
3372 if (!qp->div)
3373 goto error;
3375 isl_aff_free(aff);
3376 qp = reduce_divs(qp);
3377 qp = remove_redundant_divs(qp);
3378 return qp;
3379 error:
3380 isl_aff_free(aff);
3381 return isl_qpolynomial_free(qp);
3384 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3385 __isl_take isl_pw_aff *pwaff)
3387 int i;
3388 isl_pw_qpolynomial *pwqp;
3390 if (!pwaff)
3391 return NULL;
3393 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3394 pwaff->n);
3396 for (i = 0; i < pwaff->n; ++i) {
3397 isl_set *dom;
3398 isl_qpolynomial *qp;
3400 dom = isl_set_copy(pwaff->p[i].set);
3401 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3402 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3405 isl_pw_aff_free(pwaff);
3406 return pwqp;
3409 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3410 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3412 isl_aff *aff;
3414 aff = isl_constraint_get_bound(c, type, pos);
3415 isl_constraint_free(c);
3416 return isl_qpolynomial_from_aff(aff);
3419 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3420 * in "qp" by subs[i].
3422 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3423 __isl_take isl_qpolynomial *qp,
3424 enum isl_dim_type type, unsigned first, unsigned n,
3425 __isl_keep isl_qpolynomial **subs)
3427 int i;
3428 struct isl_upoly **ups;
3430 if (n == 0)
3431 return qp;
3433 qp = isl_qpolynomial_cow(qp);
3434 if (!qp)
3435 return NULL;
3437 if (type == isl_dim_out)
3438 isl_die(qp->dim->ctx, isl_error_invalid,
3439 "cannot substitute output/set dimension",
3440 goto error);
3441 if (type == isl_dim_in)
3442 type = isl_dim_set;
3444 for (i = 0; i < n; ++i)
3445 if (!subs[i])
3446 goto error;
3448 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3449 goto error);
3451 for (i = 0; i < n; ++i)
3452 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3453 goto error);
3455 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3456 for (i = 0; i < n; ++i)
3457 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3459 first += pos(qp->dim, type);
3461 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3462 if (!ups)
3463 goto error;
3464 for (i = 0; i < n; ++i)
3465 ups[i] = subs[i]->upoly;
3467 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3469 free(ups);
3471 if (!qp->upoly)
3472 goto error;
3474 return qp;
3475 error:
3476 isl_qpolynomial_free(qp);
3477 return NULL;
3480 /* Extend "bset" with extra set dimensions for each integer division
3481 * in "qp" and then call "fn" with the extended bset and the polynomial
3482 * that results from replacing each of the integer divisions by the
3483 * corresponding extra set dimension.
3485 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3486 __isl_keep isl_basic_set *bset,
3487 int (*fn)(__isl_take isl_basic_set *bset,
3488 __isl_take isl_qpolynomial *poly, void *user), void *user)
3490 isl_space *dim;
3491 isl_mat *div;
3492 isl_qpolynomial *poly;
3494 if (!qp || !bset)
3495 goto error;
3496 if (qp->div->n_row == 0)
3497 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3498 user);
3500 div = isl_mat_copy(qp->div);
3501 dim = isl_space_copy(qp->dim);
3502 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3503 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3504 bset = isl_basic_set_copy(bset);
3505 bset = isl_basic_set_add_dims(bset, isl_dim_set, qp->div->n_row);
3506 bset = add_div_constraints(bset, div);
3508 return fn(bset, poly, user);
3509 error:
3510 return -1;
3513 /* Return total degree in variables first (inclusive) up to last (exclusive).
3515 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3517 int deg = -1;
3518 int i;
3519 struct isl_upoly_rec *rec;
3521 if (!up)
3522 return -2;
3523 if (isl_upoly_is_zero(up))
3524 return -1;
3525 if (isl_upoly_is_cst(up) || up->var < first)
3526 return 0;
3528 rec = isl_upoly_as_rec(up);
3529 if (!rec)
3530 return -2;
3532 for (i = 0; i < rec->n; ++i) {
3533 int d;
3535 if (isl_upoly_is_zero(rec->p[i]))
3536 continue;
3537 d = isl_upoly_degree(rec->p[i], first, last);
3538 if (up->var < last)
3539 d += i;
3540 if (d > deg)
3541 deg = d;
3544 return deg;
3547 /* Return total degree in set variables.
3549 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3551 unsigned ovar;
3552 unsigned nvar;
3554 if (!poly)
3555 return -2;
3557 ovar = isl_space_offset(poly->dim, isl_dim_set);
3558 nvar = isl_space_dim(poly->dim, isl_dim_set);
3559 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3562 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3563 unsigned pos, int deg)
3565 int i;
3566 struct isl_upoly_rec *rec;
3568 if (!up)
3569 return NULL;
3571 if (isl_upoly_is_cst(up) || up->var < pos) {
3572 if (deg == 0)
3573 return isl_upoly_copy(up);
3574 else
3575 return isl_upoly_zero(up->ctx);
3578 rec = isl_upoly_as_rec(up);
3579 if (!rec)
3580 return NULL;
3582 if (up->var == pos) {
3583 if (deg < rec->n)
3584 return isl_upoly_copy(rec->p[deg]);
3585 else
3586 return isl_upoly_zero(up->ctx);
3589 up = isl_upoly_copy(up);
3590 up = isl_upoly_cow(up);
3591 rec = isl_upoly_as_rec(up);
3592 if (!rec)
3593 goto error;
3595 for (i = 0; i < rec->n; ++i) {
3596 struct isl_upoly *t;
3597 t = isl_upoly_coeff(rec->p[i], pos, deg);
3598 if (!t)
3599 goto error;
3600 isl_upoly_free(rec->p[i]);
3601 rec->p[i] = t;
3604 return up;
3605 error:
3606 isl_upoly_free(up);
3607 return NULL;
3610 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3612 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3613 __isl_keep isl_qpolynomial *qp,
3614 enum isl_dim_type type, unsigned t_pos, int deg)
3616 unsigned g_pos;
3617 struct isl_upoly *up;
3618 isl_qpolynomial *c;
3620 if (!qp)
3621 return NULL;
3623 if (type == isl_dim_out)
3624 isl_die(qp->div->ctx, isl_error_invalid,
3625 "output/set dimension does not have a coefficient",
3626 return NULL);
3627 if (type == isl_dim_in)
3628 type = isl_dim_set;
3630 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3631 return NULL);
3633 g_pos = pos(qp->dim, type) + t_pos;
3634 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3636 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3637 if (!c)
3638 return NULL;
3639 isl_mat_free(c->div);
3640 c->div = isl_mat_copy(qp->div);
3641 if (!c->div)
3642 goto error;
3643 return c;
3644 error:
3645 isl_qpolynomial_free(c);
3646 return NULL;
3649 /* Homogenize the polynomial in the variables first (inclusive) up to
3650 * last (exclusive) by inserting powers of variable first.
3651 * Variable first is assumed not to appear in the input.
3653 __isl_give struct isl_upoly *isl_upoly_homogenize(
3654 __isl_take struct isl_upoly *up, int deg, int target,
3655 int first, int last)
3657 int i;
3658 struct isl_upoly_rec *rec;
3660 if (!up)
3661 return NULL;
3662 if (isl_upoly_is_zero(up))
3663 return up;
3664 if (deg == target)
3665 return up;
3666 if (isl_upoly_is_cst(up) || up->var < first) {
3667 struct isl_upoly *hom;
3669 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3670 if (!hom)
3671 goto error;
3672 rec = isl_upoly_as_rec(hom);
3673 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3675 return hom;
3678 up = isl_upoly_cow(up);
3679 rec = isl_upoly_as_rec(up);
3680 if (!rec)
3681 goto error;
3683 for (i = 0; i < rec->n; ++i) {
3684 if (isl_upoly_is_zero(rec->p[i]))
3685 continue;
3686 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3687 up->var < last ? deg + i : i, target,
3688 first, last);
3689 if (!rec->p[i])
3690 goto error;
3693 return up;
3694 error:
3695 isl_upoly_free(up);
3696 return NULL;
3699 /* Homogenize the polynomial in the set variables by introducing
3700 * powers of an extra set variable at position 0.
3702 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3703 __isl_take isl_qpolynomial *poly)
3705 unsigned ovar;
3706 unsigned nvar;
3707 int deg = isl_qpolynomial_degree(poly);
3709 if (deg < -1)
3710 goto error;
3712 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3713 poly = isl_qpolynomial_cow(poly);
3714 if (!poly)
3715 goto error;
3717 ovar = isl_space_offset(poly->dim, isl_dim_set);
3718 nvar = isl_space_dim(poly->dim, isl_dim_set);
3719 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3720 ovar, ovar + nvar);
3721 if (!poly->upoly)
3722 goto error;
3724 return poly;
3725 error:
3726 isl_qpolynomial_free(poly);
3727 return NULL;
3730 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3731 __isl_take isl_mat *div)
3733 isl_term *term;
3734 int n;
3736 if (!dim || !div)
3737 goto error;
3739 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3741 term = isl_calloc(dim->ctx, struct isl_term,
3742 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3743 if (!term)
3744 goto error;
3746 term->ref = 1;
3747 term->dim = dim;
3748 term->div = div;
3749 isl_int_init(term->n);
3750 isl_int_init(term->d);
3752 return term;
3753 error:
3754 isl_space_free(dim);
3755 isl_mat_free(div);
3756 return NULL;
3759 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3761 if (!term)
3762 return NULL;
3764 term->ref++;
3765 return term;
3768 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3770 int i;
3771 isl_term *dup;
3772 unsigned total;
3774 if (!term)
3775 return NULL;
3777 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3779 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3780 if (!dup)
3781 return NULL;
3783 isl_int_set(dup->n, term->n);
3784 isl_int_set(dup->d, term->d);
3786 for (i = 0; i < total; ++i)
3787 dup->pow[i] = term->pow[i];
3789 return dup;
3792 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3794 if (!term)
3795 return NULL;
3797 if (term->ref == 1)
3798 return term;
3799 term->ref--;
3800 return isl_term_dup(term);
3803 void isl_term_free(__isl_take isl_term *term)
3805 if (!term)
3806 return;
3808 if (--term->ref > 0)
3809 return;
3811 isl_space_free(term->dim);
3812 isl_mat_free(term->div);
3813 isl_int_clear(term->n);
3814 isl_int_clear(term->d);
3815 free(term);
3818 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3820 if (!term)
3821 return 0;
3823 switch (type) {
3824 case isl_dim_param:
3825 case isl_dim_in:
3826 case isl_dim_out: return isl_space_dim(term->dim, type);
3827 case isl_dim_div: return term->div->n_row;
3828 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3829 term->div->n_row;
3830 default: return 0;
3834 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3836 return term ? term->dim->ctx : NULL;
3839 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3841 if (!term)
3842 return;
3843 isl_int_set(*n, term->n);
3846 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3848 if (!term)
3849 return;
3850 isl_int_set(*d, term->d);
3853 /* Return the coefficient of the term "term".
3855 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3857 if (!term)
3858 return NULL;
3860 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3861 term->n, term->d);
3864 int isl_term_get_exp(__isl_keep isl_term *term,
3865 enum isl_dim_type type, unsigned pos)
3867 if (!term)
3868 return -1;
3870 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3872 if (type >= isl_dim_set)
3873 pos += isl_space_dim(term->dim, isl_dim_param);
3874 if (type >= isl_dim_div)
3875 pos += isl_space_dim(term->dim, isl_dim_set);
3877 return term->pow[pos];
3880 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3882 isl_local_space *ls;
3883 isl_aff *aff;
3885 if (!term)
3886 return NULL;
3888 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3889 return NULL);
3891 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3892 isl_mat_copy(term->div));
3893 aff = isl_aff_alloc(ls);
3894 if (!aff)
3895 return NULL;
3897 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3899 aff = isl_aff_normalize(aff);
3901 return aff;
3904 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3905 isl_stat (*fn)(__isl_take isl_term *term, void *user),
3906 __isl_take isl_term *term, void *user)
3908 int i;
3909 struct isl_upoly_rec *rec;
3911 if (!up || !term)
3912 goto error;
3914 if (isl_upoly_is_zero(up))
3915 return term;
3917 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3918 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3919 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3921 if (isl_upoly_is_cst(up)) {
3922 struct isl_upoly_cst *cst;
3923 cst = isl_upoly_as_cst(up);
3924 if (!cst)
3925 goto error;
3926 term = isl_term_cow(term);
3927 if (!term)
3928 goto error;
3929 isl_int_set(term->n, cst->n);
3930 isl_int_set(term->d, cst->d);
3931 if (fn(isl_term_copy(term), user) < 0)
3932 goto error;
3933 return term;
3936 rec = isl_upoly_as_rec(up);
3937 if (!rec)
3938 goto error;
3940 for (i = 0; i < rec->n; ++i) {
3941 term = isl_term_cow(term);
3942 if (!term)
3943 goto error;
3944 term->pow[up->var] = i;
3945 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3946 if (!term)
3947 goto error;
3949 term->pow[up->var] = 0;
3951 return term;
3952 error:
3953 isl_term_free(term);
3954 return NULL;
3957 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3958 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
3960 isl_term *term;
3962 if (!qp)
3963 return isl_stat_error;
3965 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3966 if (!term)
3967 return isl_stat_error;
3969 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3971 isl_term_free(term);
3973 return term ? isl_stat_ok : isl_stat_error;
3976 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3978 struct isl_upoly *up;
3979 isl_qpolynomial *qp;
3980 int i, n;
3982 if (!term)
3983 return NULL;
3985 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3987 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3988 for (i = 0; i < n; ++i) {
3989 if (!term->pow[i])
3990 continue;
3991 up = isl_upoly_mul(up,
3992 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3995 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3996 if (!qp)
3997 goto error;
3998 isl_mat_free(qp->div);
3999 qp->div = isl_mat_copy(term->div);
4000 if (!qp->div)
4001 goto error;
4003 isl_term_free(term);
4004 return qp;
4005 error:
4006 isl_qpolynomial_free(qp);
4007 isl_term_free(term);
4008 return NULL;
4011 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4012 __isl_take isl_space *dim)
4014 int i;
4015 int extra;
4016 unsigned total;
4018 if (!qp || !dim)
4019 goto error;
4021 if (isl_space_is_equal(qp->dim, dim)) {
4022 isl_space_free(dim);
4023 return qp;
4026 qp = isl_qpolynomial_cow(qp);
4027 if (!qp)
4028 goto error;
4030 extra = isl_space_dim(dim, isl_dim_set) -
4031 isl_space_dim(qp->dim, isl_dim_set);
4032 total = isl_space_dim(qp->dim, isl_dim_all);
4033 if (qp->div->n_row) {
4034 int *exp;
4036 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4037 if (!exp)
4038 goto error;
4039 for (i = 0; i < qp->div->n_row; ++i)
4040 exp[i] = extra + i;
4041 qp->upoly = expand(qp->upoly, exp, total);
4042 free(exp);
4043 if (!qp->upoly)
4044 goto error;
4046 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4047 if (!qp->div)
4048 goto error;
4049 for (i = 0; i < qp->div->n_row; ++i)
4050 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4052 isl_space_free(qp->dim);
4053 qp->dim = dim;
4055 return qp;
4056 error:
4057 isl_space_free(dim);
4058 isl_qpolynomial_free(qp);
4059 return NULL;
4062 /* For each parameter or variable that does not appear in qp,
4063 * first eliminate the variable from all constraints and then set it to zero.
4065 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4066 __isl_keep isl_qpolynomial *qp)
4068 int *active = NULL;
4069 int i;
4070 int d;
4071 unsigned nparam;
4072 unsigned nvar;
4074 if (!set || !qp)
4075 goto error;
4077 d = isl_space_dim(set->dim, isl_dim_all);
4078 active = isl_calloc_array(set->ctx, int, d);
4079 if (set_active(qp, active) < 0)
4080 goto error;
4082 for (i = 0; i < d; ++i)
4083 if (!active[i])
4084 break;
4086 if (i == d) {
4087 free(active);
4088 return set;
4091 nparam = isl_space_dim(set->dim, isl_dim_param);
4092 nvar = isl_space_dim(set->dim, isl_dim_set);
4093 for (i = 0; i < nparam; ++i) {
4094 if (active[i])
4095 continue;
4096 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4097 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4099 for (i = 0; i < nvar; ++i) {
4100 if (active[nparam + i])
4101 continue;
4102 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4103 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4106 free(active);
4108 return set;
4109 error:
4110 free(active);
4111 isl_set_free(set);
4112 return NULL;
4115 struct isl_opt_data {
4116 isl_qpolynomial *qp;
4117 int first;
4118 isl_val *opt;
4119 int max;
4122 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4124 struct isl_opt_data *data = (struct isl_opt_data *)user;
4125 isl_val *val;
4127 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4128 if (data->first) {
4129 data->first = 0;
4130 data->opt = val;
4131 } else if (data->max) {
4132 data->opt = isl_val_max(data->opt, val);
4133 } else {
4134 data->opt = isl_val_min(data->opt, val);
4137 return isl_stat_ok;
4140 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4141 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4143 struct isl_opt_data data = { NULL, 1, NULL, max };
4145 if (!set || !qp)
4146 goto error;
4148 if (isl_upoly_is_cst(qp->upoly)) {
4149 isl_set_free(set);
4150 data.opt = isl_qpolynomial_get_constant_val(qp);
4151 isl_qpolynomial_free(qp);
4152 return data.opt;
4155 set = fix_inactive(set, qp);
4157 data.qp = qp;
4158 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4159 goto error;
4161 if (data.first)
4162 data.opt = isl_val_zero(isl_set_get_ctx(set));
4164 isl_set_free(set);
4165 isl_qpolynomial_free(qp);
4166 return data.opt;
4167 error:
4168 isl_set_free(set);
4169 isl_qpolynomial_free(qp);
4170 isl_val_free(data.opt);
4171 return NULL;
4174 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4175 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4177 int i;
4178 int n_sub;
4179 isl_ctx *ctx;
4180 struct isl_upoly **subs;
4181 isl_mat *mat, *diag;
4183 qp = isl_qpolynomial_cow(qp);
4184 if (!qp || !morph)
4185 goto error;
4187 ctx = qp->dim->ctx;
4188 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4190 n_sub = morph->inv->n_row - 1;
4191 if (morph->inv->n_row != morph->inv->n_col)
4192 n_sub += qp->div->n_row;
4193 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4194 if (n_sub && !subs)
4195 goto error;
4197 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4198 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4199 morph->inv->row[0][0], morph->inv->n_col);
4200 if (morph->inv->n_row != morph->inv->n_col)
4201 for (i = 0; i < qp->div->n_row; ++i)
4202 subs[morph->inv->n_row - 1 + i] =
4203 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4205 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4207 for (i = 0; i < n_sub; ++i)
4208 isl_upoly_free(subs[i]);
4209 free(subs);
4211 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4212 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4213 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4214 mat = isl_mat_diagonal(mat, diag);
4215 qp->div = isl_mat_product(qp->div, mat);
4216 isl_space_free(qp->dim);
4217 qp->dim = isl_space_copy(morph->ran->dim);
4219 if (!qp->upoly || !qp->div || !qp->dim)
4220 goto error;
4222 isl_morph_free(morph);
4224 return qp;
4225 error:
4226 isl_qpolynomial_free(qp);
4227 isl_morph_free(morph);
4228 return NULL;
4231 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4232 __isl_take isl_union_pw_qpolynomial *upwqp1,
4233 __isl_take isl_union_pw_qpolynomial *upwqp2)
4235 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4236 &isl_pw_qpolynomial_mul);
4239 /* Reorder the columns of the given div definitions according to the
4240 * given reordering.
4242 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
4243 __isl_take isl_reordering *r)
4245 int i, j;
4246 isl_mat *mat;
4247 int extra;
4249 if (!div || !r)
4250 goto error;
4252 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
4253 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
4254 if (!mat)
4255 goto error;
4257 for (i = 0; i < div->n_row; ++i) {
4258 isl_seq_cpy(mat->row[i], div->row[i], 2);
4259 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
4260 for (j = 0; j < r->len; ++j)
4261 isl_int_set(mat->row[i][2 + r->pos[j]],
4262 div->row[i][2 + j]);
4265 isl_reordering_free(r);
4266 isl_mat_free(div);
4267 return mat;
4268 error:
4269 isl_reordering_free(r);
4270 isl_mat_free(div);
4271 return NULL;
4274 /* Reorder the dimension of "qp" according to the given reordering.
4276 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4277 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4279 qp = isl_qpolynomial_cow(qp);
4280 if (!qp)
4281 goto error;
4283 r = isl_reordering_extend(r, qp->div->n_row);
4284 if (!r)
4285 goto error;
4287 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
4288 if (!qp->div)
4289 goto error;
4291 qp->upoly = reorder(qp->upoly, r->pos);
4292 if (!qp->upoly)
4293 goto error;
4295 qp = isl_qpolynomial_reset_domain_space(qp, isl_space_copy(r->dim));
4297 isl_reordering_free(r);
4298 return qp;
4299 error:
4300 isl_qpolynomial_free(qp);
4301 isl_reordering_free(r);
4302 return NULL;
4305 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4306 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4308 if (!qp || !model)
4309 goto error;
4311 if (!isl_space_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
4312 isl_reordering *exp;
4314 model = isl_space_drop_dims(model, isl_dim_in,
4315 0, isl_space_dim(model, isl_dim_in));
4316 model = isl_space_drop_dims(model, isl_dim_out,
4317 0, isl_space_dim(model, isl_dim_out));
4318 exp = isl_parameter_alignment_reordering(qp->dim, model);
4319 exp = isl_reordering_extend_space(exp,
4320 isl_qpolynomial_get_domain_space(qp));
4321 qp = isl_qpolynomial_realign_domain(qp, exp);
4324 isl_space_free(model);
4325 return qp;
4326 error:
4327 isl_space_free(model);
4328 isl_qpolynomial_free(qp);
4329 return NULL;
4332 struct isl_split_periods_data {
4333 int max_periods;
4334 isl_pw_qpolynomial *res;
4337 /* Create a slice where the integer division "div" has the fixed value "v".
4338 * In particular, if "div" refers to floor(f/m), then create a slice
4340 * m v <= f <= m v + (m - 1)
4342 * or
4344 * f - m v >= 0
4345 * -f + m v + (m - 1) >= 0
4347 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4348 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4350 int total;
4351 isl_basic_set *bset = NULL;
4352 int k;
4354 if (!dim || !qp)
4355 goto error;
4357 total = isl_space_dim(dim, isl_dim_all);
4358 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4360 k = isl_basic_set_alloc_inequality(bset);
4361 if (k < 0)
4362 goto error;
4363 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4364 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4366 k = isl_basic_set_alloc_inequality(bset);
4367 if (k < 0)
4368 goto error;
4369 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4370 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4371 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4372 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4374 isl_space_free(dim);
4375 return isl_set_from_basic_set(bset);
4376 error:
4377 isl_basic_set_free(bset);
4378 isl_space_free(dim);
4379 return NULL;
4382 static isl_stat split_periods(__isl_take isl_set *set,
4383 __isl_take isl_qpolynomial *qp, void *user);
4385 /* Create a slice of the domain "set" such that integer division "div"
4386 * has the fixed value "v" and add the results to data->res,
4387 * replacing the integer division by "v" in "qp".
4389 static isl_stat set_div(__isl_take isl_set *set,
4390 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4391 struct isl_split_periods_data *data)
4393 int i;
4394 int total;
4395 isl_set *slice;
4396 struct isl_upoly *cst;
4398 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4399 set = isl_set_intersect(set, slice);
4401 if (!qp)
4402 goto error;
4404 total = isl_space_dim(qp->dim, isl_dim_all);
4406 for (i = div + 1; i < qp->div->n_row; ++i) {
4407 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4408 continue;
4409 isl_int_addmul(qp->div->row[i][1],
4410 qp->div->row[i][2 + total + div], v);
4411 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4414 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4415 qp = substitute_div(qp, div, cst);
4417 return split_periods(set, qp, data);
4418 error:
4419 isl_set_free(set);
4420 isl_qpolynomial_free(qp);
4421 return -1;
4424 /* Split the domain "set" such that integer division "div"
4425 * has a fixed value (ranging from "min" to "max") on each slice
4426 * and add the results to data->res.
4428 static isl_stat split_div(__isl_take isl_set *set,
4429 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4430 struct isl_split_periods_data *data)
4432 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4433 isl_set *set_i = isl_set_copy(set);
4434 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4436 if (set_div(set_i, qp_i, div, min, data) < 0)
4437 goto error;
4439 isl_set_free(set);
4440 isl_qpolynomial_free(qp);
4441 return isl_stat_ok;
4442 error:
4443 isl_set_free(set);
4444 isl_qpolynomial_free(qp);
4445 return isl_stat_error;
4448 /* If "qp" refers to any integer division
4449 * that can only attain "max_periods" distinct values on "set"
4450 * then split the domain along those distinct values.
4451 * Add the results (or the original if no splitting occurs)
4452 * to data->res.
4454 static isl_stat split_periods(__isl_take isl_set *set,
4455 __isl_take isl_qpolynomial *qp, void *user)
4457 int i;
4458 isl_pw_qpolynomial *pwqp;
4459 struct isl_split_periods_data *data;
4460 isl_int min, max;
4461 int total;
4462 isl_stat r = isl_stat_ok;
4464 data = (struct isl_split_periods_data *)user;
4466 if (!set || !qp)
4467 goto error;
4469 if (qp->div->n_row == 0) {
4470 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4471 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4472 return isl_stat_ok;
4475 isl_int_init(min);
4476 isl_int_init(max);
4477 total = isl_space_dim(qp->dim, isl_dim_all);
4478 for (i = 0; i < qp->div->n_row; ++i) {
4479 enum isl_lp_result lp_res;
4481 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4482 qp->div->n_row) != -1)
4483 continue;
4485 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4486 set->ctx->one, &min, NULL, NULL);
4487 if (lp_res == isl_lp_error)
4488 goto error2;
4489 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4490 continue;
4491 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4493 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4494 set->ctx->one, &max, NULL, NULL);
4495 if (lp_res == isl_lp_error)
4496 goto error2;
4497 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4498 continue;
4499 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4501 isl_int_sub(max, max, min);
4502 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4503 isl_int_add(max, max, min);
4504 break;
4508 if (i < qp->div->n_row) {
4509 r = split_div(set, qp, i, min, max, data);
4510 } else {
4511 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4512 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4515 isl_int_clear(max);
4516 isl_int_clear(min);
4518 return r;
4519 error2:
4520 isl_int_clear(max);
4521 isl_int_clear(min);
4522 error:
4523 isl_set_free(set);
4524 isl_qpolynomial_free(qp);
4525 return isl_stat_error;
4528 /* If any quasi-polynomial in pwqp refers to any integer division
4529 * that can only attain "max_periods" distinct values on its domain
4530 * then split the domain along those distinct values.
4532 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4533 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4535 struct isl_split_periods_data data;
4537 data.max_periods = max_periods;
4538 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4540 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4541 goto error;
4543 isl_pw_qpolynomial_free(pwqp);
4545 return data.res;
4546 error:
4547 isl_pw_qpolynomial_free(data.res);
4548 isl_pw_qpolynomial_free(pwqp);
4549 return NULL;
4552 /* Construct a piecewise quasipolynomial that is constant on the given
4553 * domain. In particular, it is
4554 * 0 if cst == 0
4555 * 1 if cst == 1
4556 * infinity if cst == -1
4558 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4559 __isl_take isl_basic_set *bset, int cst)
4561 isl_space *dim;
4562 isl_qpolynomial *qp;
4564 if (!bset)
4565 return NULL;
4567 bset = isl_basic_set_params(bset);
4568 dim = isl_basic_set_get_space(bset);
4569 if (cst < 0)
4570 qp = isl_qpolynomial_infty_on_domain(dim);
4571 else if (cst == 0)
4572 qp = isl_qpolynomial_zero_on_domain(dim);
4573 else
4574 qp = isl_qpolynomial_one_on_domain(dim);
4575 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4578 /* Factor bset, call fn on each of the factors and return the product.
4580 * If no factors can be found, simply call fn on the input.
4581 * Otherwise, construct the factors based on the factorizer,
4582 * call fn on each factor and compute the product.
4584 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4585 __isl_take isl_basic_set *bset,
4586 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4588 int i, n;
4589 isl_space *space;
4590 isl_set *set;
4591 isl_factorizer *f;
4592 isl_qpolynomial *qp;
4593 isl_pw_qpolynomial *pwqp;
4594 unsigned nparam;
4595 unsigned nvar;
4597 f = isl_basic_set_factorizer(bset);
4598 if (!f)
4599 goto error;
4600 if (f->n_group == 0) {
4601 isl_factorizer_free(f);
4602 return fn(bset);
4605 nparam = isl_basic_set_dim(bset, isl_dim_param);
4606 nvar = isl_basic_set_dim(bset, isl_dim_set);
4608 space = isl_basic_set_get_space(bset);
4609 space = isl_space_params(space);
4610 set = isl_set_universe(isl_space_copy(space));
4611 qp = isl_qpolynomial_one_on_domain(space);
4612 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4614 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4616 for (i = 0, n = 0; i < f->n_group; ++i) {
4617 isl_basic_set *bset_i;
4618 isl_pw_qpolynomial *pwqp_i;
4620 bset_i = isl_basic_set_copy(bset);
4621 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4622 nparam + n + f->len[i], nvar - n - f->len[i]);
4623 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4624 nparam, n);
4625 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4626 n + f->len[i], nvar - n - f->len[i]);
4627 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4629 pwqp_i = fn(bset_i);
4630 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4632 n += f->len[i];
4635 isl_basic_set_free(bset);
4636 isl_factorizer_free(f);
4638 return pwqp;
4639 error:
4640 isl_basic_set_free(bset);
4641 return NULL;
4644 /* Factor bset, call fn on each of the factors and return the product.
4645 * The function is assumed to evaluate to zero on empty domains,
4646 * to one on zero-dimensional domains and to infinity on unbounded domains
4647 * and will not be called explicitly on zero-dimensional or unbounded domains.
4649 * We first check for some special cases and remove all equalities.
4650 * Then we hand over control to compressed_multiplicative_call.
4652 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4653 __isl_take isl_basic_set *bset,
4654 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4656 int bounded;
4657 isl_morph *morph;
4658 isl_pw_qpolynomial *pwqp;
4660 if (!bset)
4661 return NULL;
4663 if (isl_basic_set_plain_is_empty(bset))
4664 return constant_on_domain(bset, 0);
4666 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4667 return constant_on_domain(bset, 1);
4669 bounded = isl_basic_set_is_bounded(bset);
4670 if (bounded < 0)
4671 goto error;
4672 if (!bounded)
4673 return constant_on_domain(bset, -1);
4675 if (bset->n_eq == 0)
4676 return compressed_multiplicative_call(bset, fn);
4678 morph = isl_basic_set_full_compression(bset);
4679 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4681 pwqp = compressed_multiplicative_call(bset, fn);
4683 morph = isl_morph_dom_params(morph);
4684 morph = isl_morph_ran_params(morph);
4685 morph = isl_morph_inverse(morph);
4687 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4689 return pwqp;
4690 error:
4691 isl_basic_set_free(bset);
4692 return NULL;
4695 /* Drop all floors in "qp", turning each integer division [a/m] into
4696 * a rational division a/m. If "down" is set, then the integer division
4697 * is replaced by (a-(m-1))/m instead.
4699 static __isl_give isl_qpolynomial *qp_drop_floors(
4700 __isl_take isl_qpolynomial *qp, int down)
4702 int i;
4703 struct isl_upoly *s;
4705 if (!qp)
4706 return NULL;
4707 if (qp->div->n_row == 0)
4708 return qp;
4710 qp = isl_qpolynomial_cow(qp);
4711 if (!qp)
4712 return NULL;
4714 for (i = qp->div->n_row - 1; i >= 0; --i) {
4715 if (down) {
4716 isl_int_sub(qp->div->row[i][1],
4717 qp->div->row[i][1], qp->div->row[i][0]);
4718 isl_int_add_ui(qp->div->row[i][1],
4719 qp->div->row[i][1], 1);
4721 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4722 qp->div->row[i][0], qp->div->n_col - 1);
4723 qp = substitute_div(qp, i, s);
4724 if (!qp)
4725 return NULL;
4728 return qp;
4731 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4732 * a rational division a/m.
4734 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4735 __isl_take isl_pw_qpolynomial *pwqp)
4737 int i;
4739 if (!pwqp)
4740 return NULL;
4742 if (isl_pw_qpolynomial_is_zero(pwqp))
4743 return pwqp;
4745 pwqp = isl_pw_qpolynomial_cow(pwqp);
4746 if (!pwqp)
4747 return NULL;
4749 for (i = 0; i < pwqp->n; ++i) {
4750 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4751 if (!pwqp->p[i].qp)
4752 goto error;
4755 return pwqp;
4756 error:
4757 isl_pw_qpolynomial_free(pwqp);
4758 return NULL;
4761 /* Adjust all the integer divisions in "qp" such that they are at least
4762 * one over the given orthant (identified by "signs"). This ensures
4763 * that they will still be non-negative even after subtracting (m-1)/m.
4765 * In particular, f is replaced by f' + v, changing f = [a/m]
4766 * to f' = [(a - m v)/m].
4767 * If the constant term k in a is smaller than m,
4768 * the constant term of v is set to floor(k/m) - 1.
4769 * For any other term, if the coefficient c and the variable x have
4770 * the same sign, then no changes are needed.
4771 * Otherwise, if the variable is positive (and c is negative),
4772 * then the coefficient of x in v is set to floor(c/m).
4773 * If the variable is negative (and c is positive),
4774 * then the coefficient of x in v is set to ceil(c/m).
4776 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4777 int *signs)
4779 int i, j;
4780 int total;
4781 isl_vec *v = NULL;
4782 struct isl_upoly *s;
4784 qp = isl_qpolynomial_cow(qp);
4785 if (!qp)
4786 return NULL;
4787 qp->div = isl_mat_cow(qp->div);
4788 if (!qp->div)
4789 goto error;
4791 total = isl_space_dim(qp->dim, isl_dim_all);
4792 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4794 for (i = 0; i < qp->div->n_row; ++i) {
4795 isl_int *row = qp->div->row[i];
4796 v = isl_vec_clr(v);
4797 if (!v)
4798 goto error;
4799 if (isl_int_lt(row[1], row[0])) {
4800 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4801 isl_int_sub_ui(v->el[0], v->el[0], 1);
4802 isl_int_submul(row[1], row[0], v->el[0]);
4804 for (j = 0; j < total; ++j) {
4805 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4806 continue;
4807 if (signs[j] < 0)
4808 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4809 else
4810 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4811 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4813 for (j = 0; j < i; ++j) {
4814 if (isl_int_sgn(row[2 + total + j]) >= 0)
4815 continue;
4816 isl_int_fdiv_q(v->el[1 + total + j],
4817 row[2 + total + j], row[0]);
4818 isl_int_submul(row[2 + total + j],
4819 row[0], v->el[1 + total + j]);
4821 for (j = i + 1; j < qp->div->n_row; ++j) {
4822 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4823 continue;
4824 isl_seq_combine(qp->div->row[j] + 1,
4825 qp->div->ctx->one, qp->div->row[j] + 1,
4826 qp->div->row[j][2 + total + i], v->el, v->size);
4828 isl_int_set_si(v->el[1 + total + i], 1);
4829 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4830 qp->div->ctx->one, v->size);
4831 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4832 isl_upoly_free(s);
4833 if (!qp->upoly)
4834 goto error;
4837 isl_vec_free(v);
4838 return qp;
4839 error:
4840 isl_vec_free(v);
4841 isl_qpolynomial_free(qp);
4842 return NULL;
4845 struct isl_to_poly_data {
4846 int sign;
4847 isl_pw_qpolynomial *res;
4848 isl_qpolynomial *qp;
4851 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4852 * We first make all integer divisions positive and then split the
4853 * quasipolynomials into terms with sign data->sign (the direction
4854 * of the requested approximation) and terms with the opposite sign.
4855 * In the first set of terms, each integer division [a/m] is
4856 * overapproximated by a/m, while in the second it is underapproximated
4857 * by (a-(m-1))/m.
4859 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4860 void *user)
4862 struct isl_to_poly_data *data = user;
4863 isl_pw_qpolynomial *t;
4864 isl_qpolynomial *qp, *up, *down;
4866 qp = isl_qpolynomial_copy(data->qp);
4867 qp = make_divs_pos(qp, signs);
4869 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4870 up = qp_drop_floors(up, 0);
4871 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4872 down = qp_drop_floors(down, 1);
4874 isl_qpolynomial_free(qp);
4875 qp = isl_qpolynomial_add(up, down);
4877 t = isl_pw_qpolynomial_alloc(orthant, qp);
4878 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4880 return 0;
4883 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4884 * the polynomial will be an overapproximation. If "sign" is negative,
4885 * it will be an underapproximation. If "sign" is zero, the approximation
4886 * will lie somewhere in between.
4888 * In particular, is sign == 0, we simply drop the floors, turning
4889 * the integer divisions into rational divisions.
4890 * Otherwise, we split the domains into orthants, make all integer divisions
4891 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4892 * depending on the requested sign and the sign of the term in which
4893 * the integer division appears.
4895 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4896 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4898 int i;
4899 struct isl_to_poly_data data;
4901 if (sign == 0)
4902 return pwqp_drop_floors(pwqp);
4904 if (!pwqp)
4905 return NULL;
4907 data.sign = sign;
4908 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4910 for (i = 0; i < pwqp->n; ++i) {
4911 if (pwqp->p[i].qp->div->n_row == 0) {
4912 isl_pw_qpolynomial *t;
4913 t = isl_pw_qpolynomial_alloc(
4914 isl_set_copy(pwqp->p[i].set),
4915 isl_qpolynomial_copy(pwqp->p[i].qp));
4916 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4917 continue;
4919 data.qp = pwqp->p[i].qp;
4920 if (isl_set_foreach_orthant(pwqp->p[i].set,
4921 &to_polynomial_on_orthant, &data) < 0)
4922 goto error;
4925 isl_pw_qpolynomial_free(pwqp);
4927 return data.res;
4928 error:
4929 isl_pw_qpolynomial_free(pwqp);
4930 isl_pw_qpolynomial_free(data.res);
4931 return NULL;
4934 static __isl_give isl_pw_qpolynomial *poly_entry(
4935 __isl_take isl_pw_qpolynomial *pwqp, void *user)
4937 int *sign = user;
4939 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
4942 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4943 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4945 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
4946 &poly_entry, &sign);
4949 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4950 __isl_take isl_qpolynomial *qp)
4952 int i, k;
4953 isl_space *dim;
4954 isl_vec *aff = NULL;
4955 isl_basic_map *bmap = NULL;
4956 unsigned pos;
4957 unsigned n_div;
4959 if (!qp)
4960 return NULL;
4961 if (!isl_upoly_is_affine(qp->upoly))
4962 isl_die(qp->dim->ctx, isl_error_invalid,
4963 "input quasi-polynomial not affine", goto error);
4964 aff = isl_qpolynomial_extract_affine(qp);
4965 if (!aff)
4966 goto error;
4967 dim = isl_qpolynomial_get_space(qp);
4968 pos = 1 + isl_space_offset(dim, isl_dim_out);
4969 n_div = qp->div->n_row;
4970 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4972 for (i = 0; i < n_div; ++i) {
4973 k = isl_basic_map_alloc_div(bmap);
4974 if (k < 0)
4975 goto error;
4976 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4977 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4978 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4979 goto error;
4981 k = isl_basic_map_alloc_equality(bmap);
4982 if (k < 0)
4983 goto error;
4984 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4985 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4986 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4988 isl_vec_free(aff);
4989 isl_qpolynomial_free(qp);
4990 bmap = isl_basic_map_finalize(bmap);
4991 return bmap;
4992 error:
4993 isl_vec_free(aff);
4994 isl_qpolynomial_free(qp);
4995 isl_basic_map_free(bmap);
4996 return NULL;