isl_map_simplify.c: fix typos in comments
[isl.git] / isl_polynomial.c
blob2f4ba43d4f98592df824a4e2f7d94e6b4d1177c1
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 __isl_null struct isl_upoly *isl_upoly_free(__isl_take struct isl_upoly *up)
625 if (!up)
626 return NULL;
628 if (--up->ref > 0)
629 return NULL;
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);
638 return NULL;
641 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
643 isl_int gcd;
645 isl_int_init(gcd);
646 isl_int_gcd(gcd, cst->n, cst->d);
647 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
648 isl_int_divexact(cst->n, cst->n, gcd);
649 isl_int_divexact(cst->d, cst->d, gcd);
651 isl_int_clear(gcd);
654 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
655 __isl_take struct isl_upoly *up2)
657 struct isl_upoly_cst *cst1;
658 struct isl_upoly_cst *cst2;
660 up1 = isl_upoly_cow(up1);
661 if (!up1 || !up2)
662 goto error;
664 cst1 = isl_upoly_as_cst(up1);
665 cst2 = isl_upoly_as_cst(up2);
667 if (isl_int_eq(cst1->d, cst2->d))
668 isl_int_add(cst1->n, cst1->n, cst2->n);
669 else {
670 isl_int_mul(cst1->n, cst1->n, cst2->d);
671 isl_int_addmul(cst1->n, cst2->n, cst1->d);
672 isl_int_mul(cst1->d, cst1->d, cst2->d);
675 isl_upoly_cst_reduce(cst1);
677 isl_upoly_free(up2);
678 return up1;
679 error:
680 isl_upoly_free(up1);
681 isl_upoly_free(up2);
682 return NULL;
685 static __isl_give struct isl_upoly *replace_by_zero(
686 __isl_take struct isl_upoly *up)
688 struct isl_ctx *ctx;
690 if (!up)
691 return NULL;
692 ctx = up->ctx;
693 isl_upoly_free(up);
694 return isl_upoly_zero(ctx);
697 static __isl_give struct isl_upoly *replace_by_constant_term(
698 __isl_take struct isl_upoly *up)
700 struct isl_upoly_rec *rec;
701 struct isl_upoly *cst;
703 if (!up)
704 return NULL;
706 rec = isl_upoly_as_rec(up);
707 if (!rec)
708 goto error;
709 cst = isl_upoly_copy(rec->p[0]);
710 isl_upoly_free(up);
711 return cst;
712 error:
713 isl_upoly_free(up);
714 return NULL;
717 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
718 __isl_take struct isl_upoly *up2)
720 int i;
721 struct isl_upoly_rec *rec1, *rec2;
723 if (!up1 || !up2)
724 goto error;
726 if (isl_upoly_is_nan(up1)) {
727 isl_upoly_free(up2);
728 return up1;
731 if (isl_upoly_is_nan(up2)) {
732 isl_upoly_free(up1);
733 return up2;
736 if (isl_upoly_is_zero(up1)) {
737 isl_upoly_free(up1);
738 return up2;
741 if (isl_upoly_is_zero(up2)) {
742 isl_upoly_free(up2);
743 return up1;
746 if (up1->var < up2->var)
747 return isl_upoly_sum(up2, up1);
749 if (up2->var < up1->var) {
750 struct isl_upoly_rec *rec;
751 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
752 isl_upoly_free(up1);
753 return up2;
755 up1 = isl_upoly_cow(up1);
756 rec = isl_upoly_as_rec(up1);
757 if (!rec)
758 goto error;
759 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
760 if (rec->n == 1)
761 up1 = replace_by_constant_term(up1);
762 return up1;
765 if (isl_upoly_is_cst(up1))
766 return isl_upoly_sum_cst(up1, up2);
768 rec1 = isl_upoly_as_rec(up1);
769 rec2 = isl_upoly_as_rec(up2);
770 if (!rec1 || !rec2)
771 goto error;
773 if (rec1->n < rec2->n)
774 return isl_upoly_sum(up2, up1);
776 up1 = isl_upoly_cow(up1);
777 rec1 = isl_upoly_as_rec(up1);
778 if (!rec1)
779 goto error;
781 for (i = rec2->n - 1; i >= 0; --i) {
782 rec1->p[i] = isl_upoly_sum(rec1->p[i],
783 isl_upoly_copy(rec2->p[i]));
784 if (!rec1->p[i])
785 goto error;
786 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
787 isl_upoly_free(rec1->p[i]);
788 rec1->n--;
792 if (rec1->n == 0)
793 up1 = replace_by_zero(up1);
794 else if (rec1->n == 1)
795 up1 = replace_by_constant_term(up1);
797 isl_upoly_free(up2);
799 return up1;
800 error:
801 isl_upoly_free(up1);
802 isl_upoly_free(up2);
803 return NULL;
806 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
807 __isl_take struct isl_upoly *up, isl_int v)
809 struct isl_upoly_cst *cst;
811 up = isl_upoly_cow(up);
812 if (!up)
813 return NULL;
815 cst = isl_upoly_as_cst(up);
817 isl_int_addmul(cst->n, cst->d, v);
819 return up;
822 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
823 __isl_take struct isl_upoly *up, isl_int v)
825 struct isl_upoly_rec *rec;
827 if (!up)
828 return NULL;
830 if (isl_upoly_is_cst(up))
831 return isl_upoly_cst_add_isl_int(up, v);
833 up = isl_upoly_cow(up);
834 rec = isl_upoly_as_rec(up);
835 if (!rec)
836 goto error;
838 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
839 if (!rec->p[0])
840 goto error;
842 return up;
843 error:
844 isl_upoly_free(up);
845 return NULL;
848 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
849 __isl_take struct isl_upoly *up, isl_int v)
851 struct isl_upoly_cst *cst;
853 if (isl_upoly_is_zero(up))
854 return up;
856 up = isl_upoly_cow(up);
857 if (!up)
858 return NULL;
860 cst = isl_upoly_as_cst(up);
862 isl_int_mul(cst->n, cst->n, v);
864 return up;
867 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
868 __isl_take struct isl_upoly *up, isl_int v)
870 int i;
871 struct isl_upoly_rec *rec;
873 if (!up)
874 return NULL;
876 if (isl_upoly_is_cst(up))
877 return isl_upoly_cst_mul_isl_int(up, v);
879 up = isl_upoly_cow(up);
880 rec = isl_upoly_as_rec(up);
881 if (!rec)
882 goto error;
884 for (i = 0; i < rec->n; ++i) {
885 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
886 if (!rec->p[i])
887 goto error;
890 return up;
891 error:
892 isl_upoly_free(up);
893 return NULL;
896 /* Multiply the constant polynomial "up" by "v".
898 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
899 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
901 struct isl_upoly_cst *cst;
903 if (isl_upoly_is_zero(up))
904 return up;
906 up = isl_upoly_cow(up);
907 if (!up)
908 return NULL;
910 cst = isl_upoly_as_cst(up);
912 isl_int_mul(cst->n, cst->n, v->n);
913 isl_int_mul(cst->d, cst->d, v->d);
914 isl_upoly_cst_reduce(cst);
916 return up;
919 /* Multiply the polynomial "up" by "v".
921 static __isl_give struct isl_upoly *isl_upoly_scale_val(
922 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
924 int i;
925 struct isl_upoly_rec *rec;
927 if (!up)
928 return NULL;
930 if (isl_upoly_is_cst(up))
931 return isl_upoly_cst_scale_val(up, v);
933 up = isl_upoly_cow(up);
934 rec = isl_upoly_as_rec(up);
935 if (!rec)
936 goto error;
938 for (i = 0; i < rec->n; ++i) {
939 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
940 if (!rec->p[i])
941 goto error;
944 return up;
945 error:
946 isl_upoly_free(up);
947 return NULL;
950 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
951 __isl_take struct isl_upoly *up2)
953 struct isl_upoly_cst *cst1;
954 struct isl_upoly_cst *cst2;
956 up1 = isl_upoly_cow(up1);
957 if (!up1 || !up2)
958 goto error;
960 cst1 = isl_upoly_as_cst(up1);
961 cst2 = isl_upoly_as_cst(up2);
963 isl_int_mul(cst1->n, cst1->n, cst2->n);
964 isl_int_mul(cst1->d, cst1->d, cst2->d);
966 isl_upoly_cst_reduce(cst1);
968 isl_upoly_free(up2);
969 return up1;
970 error:
971 isl_upoly_free(up1);
972 isl_upoly_free(up2);
973 return NULL;
976 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
977 __isl_take struct isl_upoly *up2)
979 struct isl_upoly_rec *rec1;
980 struct isl_upoly_rec *rec2;
981 struct isl_upoly_rec *res = NULL;
982 int i, j;
983 int size;
985 rec1 = isl_upoly_as_rec(up1);
986 rec2 = isl_upoly_as_rec(up2);
987 if (!rec1 || !rec2)
988 goto error;
989 size = rec1->n + rec2->n - 1;
990 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
991 if (!res)
992 goto error;
994 for (i = 0; i < rec1->n; ++i) {
995 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
996 isl_upoly_copy(rec1->p[i]));
997 if (!res->p[i])
998 goto error;
999 res->n++;
1001 for (; i < size; ++i) {
1002 res->p[i] = isl_upoly_zero(up1->ctx);
1003 if (!res->p[i])
1004 goto error;
1005 res->n++;
1007 for (i = 0; i < rec1->n; ++i) {
1008 for (j = 1; j < rec2->n; ++j) {
1009 struct isl_upoly *up;
1010 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
1011 isl_upoly_copy(rec1->p[i]));
1012 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
1013 if (!res->p[i + j])
1014 goto error;
1018 isl_upoly_free(up1);
1019 isl_upoly_free(up2);
1021 return &res->up;
1022 error:
1023 isl_upoly_free(up1);
1024 isl_upoly_free(up2);
1025 isl_upoly_free(&res->up);
1026 return NULL;
1029 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
1030 __isl_take struct isl_upoly *up2)
1032 if (!up1 || !up2)
1033 goto error;
1035 if (isl_upoly_is_nan(up1)) {
1036 isl_upoly_free(up2);
1037 return up1;
1040 if (isl_upoly_is_nan(up2)) {
1041 isl_upoly_free(up1);
1042 return up2;
1045 if (isl_upoly_is_zero(up1)) {
1046 isl_upoly_free(up2);
1047 return up1;
1050 if (isl_upoly_is_zero(up2)) {
1051 isl_upoly_free(up1);
1052 return up2;
1055 if (isl_upoly_is_one(up1)) {
1056 isl_upoly_free(up1);
1057 return up2;
1060 if (isl_upoly_is_one(up2)) {
1061 isl_upoly_free(up2);
1062 return up1;
1065 if (up1->var < up2->var)
1066 return isl_upoly_mul(up2, up1);
1068 if (up2->var < up1->var) {
1069 int i;
1070 struct isl_upoly_rec *rec;
1071 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
1072 isl_ctx *ctx = up1->ctx;
1073 isl_upoly_free(up1);
1074 isl_upoly_free(up2);
1075 return isl_upoly_nan(ctx);
1077 up1 = isl_upoly_cow(up1);
1078 rec = isl_upoly_as_rec(up1);
1079 if (!rec)
1080 goto error;
1082 for (i = 0; i < rec->n; ++i) {
1083 rec->p[i] = isl_upoly_mul(rec->p[i],
1084 isl_upoly_copy(up2));
1085 if (!rec->p[i])
1086 goto error;
1088 isl_upoly_free(up2);
1089 return up1;
1092 if (isl_upoly_is_cst(up1))
1093 return isl_upoly_mul_cst(up1, up2);
1095 return isl_upoly_mul_rec(up1, up2);
1096 error:
1097 isl_upoly_free(up1);
1098 isl_upoly_free(up2);
1099 return NULL;
1102 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1103 unsigned power)
1105 struct isl_upoly *res;
1107 if (!up)
1108 return NULL;
1109 if (power == 1)
1110 return up;
1112 if (power % 2)
1113 res = isl_upoly_copy(up);
1114 else
1115 res = isl_upoly_one(up->ctx);
1117 while (power >>= 1) {
1118 up = isl_upoly_mul(up, isl_upoly_copy(up));
1119 if (power % 2)
1120 res = isl_upoly_mul(res, isl_upoly_copy(up));
1123 isl_upoly_free(up);
1124 return res;
1127 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
1128 unsigned n_div, __isl_take struct isl_upoly *up)
1130 struct isl_qpolynomial *qp = NULL;
1131 unsigned total;
1133 if (!dim || !up)
1134 goto error;
1136 if (!isl_space_is_set(dim))
1137 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1138 "domain of polynomial should be a set", goto error);
1140 total = isl_space_dim(dim, isl_dim_all);
1142 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
1143 if (!qp)
1144 goto error;
1146 qp->ref = 1;
1147 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
1148 if (!qp->div)
1149 goto error;
1151 qp->dim = dim;
1152 qp->upoly = up;
1154 return qp;
1155 error:
1156 isl_space_free(dim);
1157 isl_upoly_free(up);
1158 isl_qpolynomial_free(qp);
1159 return NULL;
1162 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1164 if (!qp)
1165 return NULL;
1167 qp->ref++;
1168 return qp;
1171 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1173 struct isl_qpolynomial *dup;
1175 if (!qp)
1176 return NULL;
1178 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1179 isl_upoly_copy(qp->upoly));
1180 if (!dup)
1181 return NULL;
1182 isl_mat_free(dup->div);
1183 dup->div = isl_mat_copy(qp->div);
1184 if (!dup->div)
1185 goto error;
1187 return dup;
1188 error:
1189 isl_qpolynomial_free(dup);
1190 return NULL;
1193 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1195 if (!qp)
1196 return NULL;
1198 if (qp->ref == 1)
1199 return qp;
1200 qp->ref--;
1201 return isl_qpolynomial_dup(qp);
1204 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1205 __isl_take isl_qpolynomial *qp)
1207 if (!qp)
1208 return NULL;
1210 if (--qp->ref > 0)
1211 return NULL;
1213 isl_space_free(qp->dim);
1214 isl_mat_free(qp->div);
1215 isl_upoly_free(qp->upoly);
1217 free(qp);
1218 return NULL;
1221 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1223 int i;
1224 struct isl_upoly_rec *rec;
1225 struct isl_upoly_cst *cst;
1227 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1228 if (!rec)
1229 return NULL;
1230 for (i = 0; i < 1 + power; ++i) {
1231 rec->p[i] = isl_upoly_zero(ctx);
1232 if (!rec->p[i])
1233 goto error;
1234 rec->n++;
1236 cst = isl_upoly_as_cst(rec->p[power]);
1237 isl_int_set_si(cst->n, 1);
1239 return &rec->up;
1240 error:
1241 isl_upoly_free(&rec->up);
1242 return NULL;
1245 /* r array maps original positions to new positions.
1247 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1248 int *r)
1250 int i;
1251 struct isl_upoly_rec *rec;
1252 struct isl_upoly *base;
1253 struct isl_upoly *res;
1255 if (isl_upoly_is_cst(up))
1256 return up;
1258 rec = isl_upoly_as_rec(up);
1259 if (!rec)
1260 goto error;
1262 isl_assert(up->ctx, rec->n >= 1, goto error);
1264 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1265 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1267 for (i = rec->n - 2; i >= 0; --i) {
1268 res = isl_upoly_mul(res, isl_upoly_copy(base));
1269 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1272 isl_upoly_free(base);
1273 isl_upoly_free(up);
1275 return res;
1276 error:
1277 isl_upoly_free(up);
1278 return NULL;
1281 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1282 __isl_keep isl_mat *div2)
1284 int n_row, n_col;
1285 isl_bool equal;
1287 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1288 div1->n_col >= div2->n_col,
1289 return isl_bool_error);
1291 if (div1->n_row == div2->n_row)
1292 return isl_mat_is_equal(div1, div2);
1294 n_row = div1->n_row;
1295 n_col = div1->n_col;
1296 div1->n_row = div2->n_row;
1297 div1->n_col = div2->n_col;
1299 equal = isl_mat_is_equal(div1, div2);
1301 div1->n_row = n_row;
1302 div1->n_col = n_col;
1304 return equal;
1307 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1309 int li, lj;
1311 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1312 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1314 if (li != lj)
1315 return li - lj;
1317 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1320 struct isl_div_sort_info {
1321 isl_mat *div;
1322 int row;
1325 static int div_sort_cmp(const void *p1, const void *p2)
1327 const struct isl_div_sort_info *i1, *i2;
1328 i1 = (const struct isl_div_sort_info *) p1;
1329 i2 = (const struct isl_div_sort_info *) p2;
1331 return cmp_row(i1->div, i1->row, i2->row);
1334 /* Sort divs and remove duplicates.
1336 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1338 int i;
1339 int skip;
1340 int len;
1341 struct isl_div_sort_info *array = NULL;
1342 int *pos = NULL, *at = NULL;
1343 int *reordering = NULL;
1344 unsigned div_pos;
1346 if (!qp)
1347 return NULL;
1348 if (qp->div->n_row <= 1)
1349 return qp;
1351 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1353 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1354 qp->div->n_row);
1355 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1356 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1357 len = qp->div->n_col - 2;
1358 reordering = isl_alloc_array(qp->div->ctx, int, len);
1359 if (!array || !pos || !at || !reordering)
1360 goto error;
1362 for (i = 0; i < qp->div->n_row; ++i) {
1363 array[i].div = qp->div;
1364 array[i].row = i;
1365 pos[i] = i;
1366 at[i] = i;
1369 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1370 div_sort_cmp);
1372 for (i = 0; i < div_pos; ++i)
1373 reordering[i] = i;
1375 for (i = 0; i < qp->div->n_row; ++i) {
1376 if (pos[array[i].row] == i)
1377 continue;
1378 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1379 pos[at[i]] = pos[array[i].row];
1380 at[pos[array[i].row]] = at[i];
1381 at[i] = array[i].row;
1382 pos[array[i].row] = i;
1385 skip = 0;
1386 for (i = 0; i < len - div_pos; ++i) {
1387 if (i > 0 &&
1388 isl_seq_eq(qp->div->row[i - skip - 1],
1389 qp->div->row[i - skip], qp->div->n_col)) {
1390 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1391 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1392 2 + div_pos + i - skip);
1393 qp->div = isl_mat_drop_cols(qp->div,
1394 2 + div_pos + i - skip, 1);
1395 skip++;
1397 reordering[div_pos + array[i].row] = div_pos + i - skip;
1400 qp->upoly = reorder(qp->upoly, reordering);
1402 if (!qp->upoly || !qp->div)
1403 goto error;
1405 free(at);
1406 free(pos);
1407 free(array);
1408 free(reordering);
1410 return qp;
1411 error:
1412 free(at);
1413 free(pos);
1414 free(array);
1415 free(reordering);
1416 isl_qpolynomial_free(qp);
1417 return NULL;
1420 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1421 int *exp, int first)
1423 int i;
1424 struct isl_upoly_rec *rec;
1426 if (isl_upoly_is_cst(up))
1427 return up;
1429 if (up->var < first)
1430 return up;
1432 if (exp[up->var - first] == up->var - first)
1433 return up;
1435 up = isl_upoly_cow(up);
1436 if (!up)
1437 goto error;
1439 up->var = exp[up->var - first] + first;
1441 rec = isl_upoly_as_rec(up);
1442 if (!rec)
1443 goto error;
1445 for (i = 0; i < rec->n; ++i) {
1446 rec->p[i] = expand(rec->p[i], exp, first);
1447 if (!rec->p[i])
1448 goto error;
1451 return up;
1452 error:
1453 isl_upoly_free(up);
1454 return NULL;
1457 static __isl_give isl_qpolynomial *with_merged_divs(
1458 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1459 __isl_take isl_qpolynomial *qp2),
1460 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1462 int *exp1 = NULL;
1463 int *exp2 = NULL;
1464 isl_mat *div = NULL;
1465 int n_div1, n_div2;
1467 qp1 = isl_qpolynomial_cow(qp1);
1468 qp2 = isl_qpolynomial_cow(qp2);
1470 if (!qp1 || !qp2)
1471 goto error;
1473 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1474 qp1->div->n_col >= qp2->div->n_col, goto error);
1476 n_div1 = qp1->div->n_row;
1477 n_div2 = qp2->div->n_row;
1478 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1479 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1480 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1481 goto error;
1483 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1484 if (!div)
1485 goto error;
1487 isl_mat_free(qp1->div);
1488 qp1->div = isl_mat_copy(div);
1489 isl_mat_free(qp2->div);
1490 qp2->div = isl_mat_copy(div);
1492 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1493 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1495 if (!qp1->upoly || !qp2->upoly)
1496 goto error;
1498 isl_mat_free(div);
1499 free(exp1);
1500 free(exp2);
1502 return fn(qp1, qp2);
1503 error:
1504 isl_mat_free(div);
1505 free(exp1);
1506 free(exp2);
1507 isl_qpolynomial_free(qp1);
1508 isl_qpolynomial_free(qp2);
1509 return NULL;
1512 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1513 __isl_take isl_qpolynomial *qp2)
1515 isl_bool compatible;
1517 qp1 = isl_qpolynomial_cow(qp1);
1519 if (!qp1 || !qp2)
1520 goto error;
1522 if (qp1->div->n_row < qp2->div->n_row)
1523 return isl_qpolynomial_add(qp2, qp1);
1525 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1526 compatible = compatible_divs(qp1->div, qp2->div);
1527 if (compatible < 0)
1528 goto error;
1529 if (!compatible)
1530 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1532 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1533 if (!qp1->upoly)
1534 goto error;
1536 isl_qpolynomial_free(qp2);
1538 return qp1;
1539 error:
1540 isl_qpolynomial_free(qp1);
1541 isl_qpolynomial_free(qp2);
1542 return NULL;
1545 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1546 __isl_keep isl_set *dom,
1547 __isl_take isl_qpolynomial *qp1,
1548 __isl_take isl_qpolynomial *qp2)
1550 qp1 = isl_qpolynomial_add(qp1, qp2);
1551 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1552 return qp1;
1555 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1556 __isl_take isl_qpolynomial *qp2)
1558 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1561 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1562 __isl_take isl_qpolynomial *qp, isl_int v)
1564 if (isl_int_is_zero(v))
1565 return qp;
1567 qp = isl_qpolynomial_cow(qp);
1568 if (!qp)
1569 return NULL;
1571 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1572 if (!qp->upoly)
1573 goto error;
1575 return qp;
1576 error:
1577 isl_qpolynomial_free(qp);
1578 return NULL;
1582 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1584 if (!qp)
1585 return NULL;
1587 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1590 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1591 __isl_take isl_qpolynomial *qp, isl_int v)
1593 if (isl_int_is_one(v))
1594 return qp;
1596 if (qp && isl_int_is_zero(v)) {
1597 isl_qpolynomial *zero;
1598 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1599 isl_qpolynomial_free(qp);
1600 return zero;
1603 qp = isl_qpolynomial_cow(qp);
1604 if (!qp)
1605 return NULL;
1607 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1608 if (!qp->upoly)
1609 goto error;
1611 return qp;
1612 error:
1613 isl_qpolynomial_free(qp);
1614 return NULL;
1617 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1618 __isl_take isl_qpolynomial *qp, isl_int v)
1620 return isl_qpolynomial_mul_isl_int(qp, v);
1623 /* Multiply "qp" by "v".
1625 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1626 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1628 if (!qp || !v)
1629 goto error;
1631 if (!isl_val_is_rat(v))
1632 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1633 "expecting rational factor", goto error);
1635 if (isl_val_is_one(v)) {
1636 isl_val_free(v);
1637 return qp;
1640 if (isl_val_is_zero(v)) {
1641 isl_space *space;
1643 space = isl_qpolynomial_get_domain_space(qp);
1644 isl_qpolynomial_free(qp);
1645 isl_val_free(v);
1646 return isl_qpolynomial_zero_on_domain(space);
1649 qp = isl_qpolynomial_cow(qp);
1650 if (!qp)
1651 goto error;
1653 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1654 if (!qp->upoly)
1655 qp = isl_qpolynomial_free(qp);
1657 isl_val_free(v);
1658 return qp;
1659 error:
1660 isl_val_free(v);
1661 isl_qpolynomial_free(qp);
1662 return NULL;
1665 /* Divide "qp" by "v".
1667 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1668 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1670 if (!qp || !v)
1671 goto error;
1673 if (!isl_val_is_rat(v))
1674 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1675 "expecting rational factor", goto error);
1676 if (isl_val_is_zero(v))
1677 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1678 "cannot scale down by zero", goto error);
1680 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1681 error:
1682 isl_val_free(v);
1683 isl_qpolynomial_free(qp);
1684 return NULL;
1687 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1688 __isl_take isl_qpolynomial *qp2)
1690 isl_bool compatible;
1692 qp1 = isl_qpolynomial_cow(qp1);
1694 if (!qp1 || !qp2)
1695 goto error;
1697 if (qp1->div->n_row < qp2->div->n_row)
1698 return isl_qpolynomial_mul(qp2, qp1);
1700 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1701 compatible = compatible_divs(qp1->div, qp2->div);
1702 if (compatible < 0)
1703 goto error;
1704 if (!compatible)
1705 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1707 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1708 if (!qp1->upoly)
1709 goto error;
1711 isl_qpolynomial_free(qp2);
1713 return qp1;
1714 error:
1715 isl_qpolynomial_free(qp1);
1716 isl_qpolynomial_free(qp2);
1717 return NULL;
1720 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1721 unsigned power)
1723 qp = isl_qpolynomial_cow(qp);
1725 if (!qp)
1726 return NULL;
1728 qp->upoly = isl_upoly_pow(qp->upoly, power);
1729 if (!qp->upoly)
1730 goto error;
1732 return qp;
1733 error:
1734 isl_qpolynomial_free(qp);
1735 return NULL;
1738 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1739 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1741 int i;
1743 if (power == 1)
1744 return pwqp;
1746 pwqp = isl_pw_qpolynomial_cow(pwqp);
1747 if (!pwqp)
1748 return NULL;
1750 for (i = 0; i < pwqp->n; ++i) {
1751 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1752 if (!pwqp->p[i].qp)
1753 return isl_pw_qpolynomial_free(pwqp);
1756 return pwqp;
1759 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1760 __isl_take isl_space *dim)
1762 if (!dim)
1763 return NULL;
1764 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1767 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1768 __isl_take isl_space *dim)
1770 if (!dim)
1771 return NULL;
1772 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1775 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1776 __isl_take isl_space *dim)
1778 if (!dim)
1779 return NULL;
1780 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1783 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1784 __isl_take isl_space *dim)
1786 if (!dim)
1787 return NULL;
1788 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1791 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1792 __isl_take isl_space *dim)
1794 if (!dim)
1795 return NULL;
1796 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1799 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1800 __isl_take isl_space *dim,
1801 isl_int v)
1803 struct isl_qpolynomial *qp;
1804 struct isl_upoly_cst *cst;
1806 if (!dim)
1807 return NULL;
1809 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1810 if (!qp)
1811 return NULL;
1813 cst = isl_upoly_as_cst(qp->upoly);
1814 isl_int_set(cst->n, v);
1816 return qp;
1819 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1820 isl_int *n, isl_int *d)
1822 struct isl_upoly_cst *cst;
1824 if (!qp)
1825 return -1;
1827 if (!isl_upoly_is_cst(qp->upoly))
1828 return 0;
1830 cst = isl_upoly_as_cst(qp->upoly);
1831 if (!cst)
1832 return -1;
1834 if (n)
1835 isl_int_set(*n, cst->n);
1836 if (d)
1837 isl_int_set(*d, cst->d);
1839 return 1;
1842 /* Return the constant term of "up".
1844 static __isl_give isl_val *isl_upoly_get_constant_val(
1845 __isl_keep struct isl_upoly *up)
1847 struct isl_upoly_cst *cst;
1849 if (!up)
1850 return NULL;
1852 while (!isl_upoly_is_cst(up)) {
1853 struct isl_upoly_rec *rec;
1855 rec = isl_upoly_as_rec(up);
1856 if (!rec)
1857 return NULL;
1858 up = rec->p[0];
1861 cst = isl_upoly_as_cst(up);
1862 if (!cst)
1863 return NULL;
1864 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1867 /* Return the constant term of "qp".
1869 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1870 __isl_keep isl_qpolynomial *qp)
1872 if (!qp)
1873 return NULL;
1875 return isl_upoly_get_constant_val(qp->upoly);
1878 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1880 int is_cst;
1881 struct isl_upoly_rec *rec;
1883 if (!up)
1884 return -1;
1886 if (up->var < 0)
1887 return 1;
1889 rec = isl_upoly_as_rec(up);
1890 if (!rec)
1891 return -1;
1893 if (rec->n > 2)
1894 return 0;
1896 isl_assert(up->ctx, rec->n > 1, return -1);
1898 is_cst = isl_upoly_is_cst(rec->p[1]);
1899 if (is_cst < 0)
1900 return -1;
1901 if (!is_cst)
1902 return 0;
1904 return isl_upoly_is_affine(rec->p[0]);
1907 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1909 if (!qp)
1910 return -1;
1912 if (qp->div->n_row > 0)
1913 return 0;
1915 return isl_upoly_is_affine(qp->upoly);
1918 static void update_coeff(__isl_keep isl_vec *aff,
1919 __isl_keep struct isl_upoly_cst *cst, int pos)
1921 isl_int gcd;
1922 isl_int f;
1924 if (isl_int_is_zero(cst->n))
1925 return;
1927 isl_int_init(gcd);
1928 isl_int_init(f);
1929 isl_int_gcd(gcd, cst->d, aff->el[0]);
1930 isl_int_divexact(f, cst->d, gcd);
1931 isl_int_divexact(gcd, aff->el[0], gcd);
1932 isl_seq_scale(aff->el, aff->el, f, aff->size);
1933 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1934 isl_int_clear(gcd);
1935 isl_int_clear(f);
1938 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1939 __isl_keep isl_vec *aff)
1941 struct isl_upoly_cst *cst;
1942 struct isl_upoly_rec *rec;
1944 if (!up || !aff)
1945 return -1;
1947 if (up->var < 0) {
1948 struct isl_upoly_cst *cst;
1950 cst = isl_upoly_as_cst(up);
1951 if (!cst)
1952 return -1;
1953 update_coeff(aff, cst, 0);
1954 return 0;
1957 rec = isl_upoly_as_rec(up);
1958 if (!rec)
1959 return -1;
1960 isl_assert(up->ctx, rec->n == 2, return -1);
1962 cst = isl_upoly_as_cst(rec->p[1]);
1963 if (!cst)
1964 return -1;
1965 update_coeff(aff, cst, 1 + up->var);
1967 return isl_upoly_update_affine(rec->p[0], aff);
1970 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1971 __isl_keep isl_qpolynomial *qp)
1973 isl_vec *aff;
1974 unsigned d;
1976 if (!qp)
1977 return NULL;
1979 d = isl_space_dim(qp->dim, isl_dim_all);
1980 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1981 if (!aff)
1982 return NULL;
1984 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1985 isl_int_set_si(aff->el[0], 1);
1987 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1988 goto error;
1990 return aff;
1991 error:
1992 isl_vec_free(aff);
1993 return NULL;
1996 /* Compare two quasi-polynomials.
1998 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
1999 * than "qp2" and 0 if they are equal.
2001 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2002 __isl_keep isl_qpolynomial *qp2)
2004 int cmp;
2006 if (qp1 == qp2)
2007 return 0;
2008 if (!qp1)
2009 return -1;
2010 if (!qp2)
2011 return 1;
2013 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2014 if (cmp != 0)
2015 return cmp;
2017 cmp = isl_local_cmp(qp1->div, qp2->div);
2018 if (cmp != 0)
2019 return cmp;
2021 return isl_upoly_plain_cmp(qp1->upoly, qp2->upoly);
2024 /* Is "qp1" obviously equal to "qp2"?
2026 * NaN is not equal to anything, not even to another NaN.
2028 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2029 __isl_keep isl_qpolynomial *qp2)
2031 isl_bool equal;
2033 if (!qp1 || !qp2)
2034 return isl_bool_error;
2036 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2037 return isl_bool_false;
2039 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2040 if (equal < 0 || !equal)
2041 return equal;
2043 equal = isl_mat_is_equal(qp1->div, qp2->div);
2044 if (equal < 0 || !equal)
2045 return equal;
2047 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
2050 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
2052 int i;
2053 struct isl_upoly_rec *rec;
2055 if (isl_upoly_is_cst(up)) {
2056 struct isl_upoly_cst *cst;
2057 cst = isl_upoly_as_cst(up);
2058 if (!cst)
2059 return;
2060 isl_int_lcm(*d, *d, cst->d);
2061 return;
2064 rec = isl_upoly_as_rec(up);
2065 if (!rec)
2066 return;
2068 for (i = 0; i < rec->n; ++i)
2069 upoly_update_den(rec->p[i], d);
2072 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
2074 isl_int_set_si(*d, 1);
2075 if (!qp)
2076 return;
2077 upoly_update_den(qp->upoly, d);
2080 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2081 __isl_take isl_space *dim, int pos, int power)
2083 struct isl_ctx *ctx;
2085 if (!dim)
2086 return NULL;
2088 ctx = dim->ctx;
2090 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
2093 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(__isl_take isl_space *dim,
2094 enum isl_dim_type type, unsigned pos)
2096 if (!dim)
2097 return NULL;
2099 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
2100 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
2102 if (type == isl_dim_set)
2103 pos += isl_space_dim(dim, isl_dim_param);
2105 return isl_qpolynomial_var_pow_on_domain(dim, pos, 1);
2106 error:
2107 isl_space_free(dim);
2108 return NULL;
2111 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
2112 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
2114 int i;
2115 struct isl_upoly_rec *rec;
2116 struct isl_upoly *base, *res;
2118 if (!up)
2119 return NULL;
2121 if (isl_upoly_is_cst(up))
2122 return up;
2124 if (up->var < first)
2125 return up;
2127 rec = isl_upoly_as_rec(up);
2128 if (!rec)
2129 goto error;
2131 isl_assert(up->ctx, rec->n >= 1, goto error);
2133 if (up->var >= first + n)
2134 base = isl_upoly_var_pow(up->ctx, up->var, 1);
2135 else
2136 base = isl_upoly_copy(subs[up->var - first]);
2138 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
2139 for (i = rec->n - 2; i >= 0; --i) {
2140 struct isl_upoly *t;
2141 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
2142 res = isl_upoly_mul(res, isl_upoly_copy(base));
2143 res = isl_upoly_sum(res, t);
2146 isl_upoly_free(base);
2147 isl_upoly_free(up);
2149 return res;
2150 error:
2151 isl_upoly_free(up);
2152 return NULL;
2155 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
2156 isl_int denom, unsigned len)
2158 int i;
2159 struct isl_upoly *up;
2161 isl_assert(ctx, len >= 1, return NULL);
2163 up = isl_upoly_rat_cst(ctx, f[0], denom);
2164 for (i = 0; i < len - 1; ++i) {
2165 struct isl_upoly *t;
2166 struct isl_upoly *c;
2168 if (isl_int_is_zero(f[1 + i]))
2169 continue;
2171 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2172 t = isl_upoly_var_pow(ctx, i, 1);
2173 t = isl_upoly_mul(c, t);
2174 up = isl_upoly_sum(up, t);
2177 return up;
2180 /* Remove common factor of non-constant terms and denominator.
2182 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2184 isl_ctx *ctx = qp->div->ctx;
2185 unsigned total = qp->div->n_col - 2;
2187 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2188 isl_int_gcd(ctx->normalize_gcd,
2189 ctx->normalize_gcd, qp->div->row[div][0]);
2190 if (isl_int_is_one(ctx->normalize_gcd))
2191 return;
2193 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2194 ctx->normalize_gcd, total);
2195 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2196 ctx->normalize_gcd);
2197 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2198 ctx->normalize_gcd);
2201 /* Replace the integer division identified by "div" by the polynomial "s".
2202 * The integer division is assumed not to appear in the definition
2203 * of any other integer divisions.
2205 static __isl_give isl_qpolynomial *substitute_div(
2206 __isl_take isl_qpolynomial *qp,
2207 int div, __isl_take struct isl_upoly *s)
2209 int i;
2210 int total;
2211 int *reordering;
2213 if (!qp || !s)
2214 goto error;
2216 qp = isl_qpolynomial_cow(qp);
2217 if (!qp)
2218 goto error;
2220 total = isl_space_dim(qp->dim, isl_dim_all);
2221 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2222 if (!qp->upoly)
2223 goto error;
2225 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2226 if (!reordering)
2227 goto error;
2228 for (i = 0; i < total + div; ++i)
2229 reordering[i] = i;
2230 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2231 reordering[i] = i - 1;
2232 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2233 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2234 qp->upoly = reorder(qp->upoly, reordering);
2235 free(reordering);
2237 if (!qp->upoly || !qp->div)
2238 goto error;
2240 isl_upoly_free(s);
2241 return qp;
2242 error:
2243 isl_qpolynomial_free(qp);
2244 isl_upoly_free(s);
2245 return NULL;
2248 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2249 * divisions because d is equal to 1 by their definition, i.e., e.
2251 static __isl_give isl_qpolynomial *substitute_non_divs(
2252 __isl_take isl_qpolynomial *qp)
2254 int i, j;
2255 int total;
2256 struct isl_upoly *s;
2258 if (!qp)
2259 return NULL;
2261 total = isl_space_dim(qp->dim, isl_dim_all);
2262 for (i = 0; qp && i < qp->div->n_row; ++i) {
2263 if (!isl_int_is_one(qp->div->row[i][0]))
2264 continue;
2265 for (j = i + 1; j < qp->div->n_row; ++j) {
2266 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2267 continue;
2268 isl_seq_combine(qp->div->row[j] + 1,
2269 qp->div->ctx->one, qp->div->row[j] + 1,
2270 qp->div->row[j][2 + total + i],
2271 qp->div->row[i] + 1, 1 + total + i);
2272 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2273 normalize_div(qp, j);
2275 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2276 qp->div->row[i][0], qp->div->n_col - 1);
2277 qp = substitute_div(qp, i, s);
2278 --i;
2281 return qp;
2284 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2285 * with d the denominator. When replacing the coefficient e of x by
2286 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2287 * inside the division, so we need to add floor(e/d) * x outside.
2288 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2289 * to adjust the coefficient of x in each later div that depends on the
2290 * current div "div" and also in the affine expressions in the rows of "mat"
2291 * (if they too depend on "div").
2293 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2294 __isl_keep isl_mat **mat)
2296 int i, j;
2297 isl_int v;
2298 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2300 isl_int_init(v);
2301 for (i = 0; i < 1 + total + div; ++i) {
2302 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2303 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2304 continue;
2305 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2306 isl_int_fdiv_r(qp->div->row[div][1 + i],
2307 qp->div->row[div][1 + i], qp->div->row[div][0]);
2308 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2309 for (j = div + 1; j < qp->div->n_row; ++j) {
2310 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2311 continue;
2312 isl_int_addmul(qp->div->row[j][1 + i],
2313 v, qp->div->row[j][2 + total + div]);
2316 isl_int_clear(v);
2319 /* Check if the last non-zero coefficient is bigger that half of the
2320 * denominator. If so, we will invert the div to further reduce the number
2321 * of distinct divs that may appear.
2322 * If the last non-zero coefficient is exactly half the denominator,
2323 * then we continue looking for earlier coefficients that are bigger
2324 * than half the denominator.
2326 static int needs_invert(__isl_keep isl_mat *div, int row)
2328 int i;
2329 int cmp;
2331 for (i = div->n_col - 1; i >= 1; --i) {
2332 if (isl_int_is_zero(div->row[row][i]))
2333 continue;
2334 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2335 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2336 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2337 if (cmp)
2338 return cmp > 0;
2339 if (i == 1)
2340 return 1;
2343 return 0;
2346 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2347 * We only invert the coefficients of e (and the coefficient of q in
2348 * later divs and in the rows of "mat"). After calling this function, the
2349 * coefficients of e should be reduced again.
2351 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2352 __isl_keep isl_mat **mat)
2354 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2356 isl_seq_neg(qp->div->row[div] + 1,
2357 qp->div->row[div] + 1, qp->div->n_col - 1);
2358 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2359 isl_int_add(qp->div->row[div][1],
2360 qp->div->row[div][1], qp->div->row[div][0]);
2361 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2362 isl_mat_col_mul(qp->div, 2 + total + div,
2363 qp->div->ctx->negone, 2 + total + div);
2366 /* Reduce all divs of "qp" to have coefficients
2367 * in the interval [0, d-1], with d the denominator and such that the
2368 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2369 * The modifications to the integer divisions need to be reflected
2370 * in the factors of the polynomial that refer to the original
2371 * integer divisions. To this end, the modifications are collected
2372 * as a set of affine expressions and then plugged into the polynomial.
2374 * After the reduction, some divs may have become redundant or identical,
2375 * so we call substitute_non_divs and sort_divs. If these functions
2376 * eliminate divs or merge two or more divs into one, the coefficients
2377 * of the enclosing divs may have to be reduced again, so we call
2378 * ourselves recursively if the number of divs decreases.
2380 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2382 int i;
2383 isl_ctx *ctx;
2384 isl_mat *mat;
2385 struct isl_upoly **s;
2386 unsigned o_div, n_div, total;
2388 if (!qp)
2389 return NULL;
2391 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2392 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2393 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2394 ctx = isl_qpolynomial_get_ctx(qp);
2395 mat = isl_mat_zero(ctx, n_div, 1 + total);
2397 for (i = 0; i < n_div; ++i)
2398 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2400 for (i = 0; i < qp->div->n_row; ++i) {
2401 normalize_div(qp, i);
2402 reduce_div(qp, i, &mat);
2403 if (needs_invert(qp->div, i)) {
2404 invert_div(qp, i, &mat);
2405 reduce_div(qp, i, &mat);
2408 if (!mat)
2409 goto error;
2411 s = isl_alloc_array(ctx, struct isl_upoly *, n_div);
2412 if (n_div && !s)
2413 goto error;
2414 for (i = 0; i < n_div; ++i)
2415 s[i] = isl_upoly_from_affine(ctx, mat->row[i], ctx->one,
2416 1 + total);
2417 qp->upoly = isl_upoly_subs(qp->upoly, o_div - 1, n_div, s);
2418 for (i = 0; i < n_div; ++i)
2419 isl_upoly_free(s[i]);
2420 free(s);
2421 if (!qp->upoly)
2422 goto error;
2424 isl_mat_free(mat);
2426 qp = substitute_non_divs(qp);
2427 qp = sort_divs(qp);
2428 if (qp && isl_qpolynomial_domain_dim(qp, isl_dim_div) < n_div)
2429 return reduce_divs(qp);
2431 return qp;
2432 error:
2433 isl_qpolynomial_free(qp);
2434 isl_mat_free(mat);
2435 return NULL;
2438 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2439 __isl_take isl_space *dim, const isl_int n, const isl_int d)
2441 struct isl_qpolynomial *qp;
2442 struct isl_upoly_cst *cst;
2444 if (!dim)
2445 return NULL;
2447 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2448 if (!qp)
2449 return NULL;
2451 cst = isl_upoly_as_cst(qp->upoly);
2452 isl_int_set(cst->n, n);
2453 isl_int_set(cst->d, d);
2455 return qp;
2458 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2460 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2461 __isl_take isl_space *domain, __isl_take isl_val *val)
2463 isl_qpolynomial *qp;
2464 struct isl_upoly_cst *cst;
2466 if (!domain || !val)
2467 goto error;
2469 qp = isl_qpolynomial_alloc(isl_space_copy(domain), 0,
2470 isl_upoly_zero(domain->ctx));
2471 if (!qp)
2472 goto error;
2474 cst = isl_upoly_as_cst(qp->upoly);
2475 isl_int_set(cst->n, val->n);
2476 isl_int_set(cst->d, val->d);
2478 isl_space_free(domain);
2479 isl_val_free(val);
2480 return qp;
2481 error:
2482 isl_space_free(domain);
2483 isl_val_free(val);
2484 return NULL;
2487 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2489 struct isl_upoly_rec *rec;
2490 int i;
2492 if (!up)
2493 return -1;
2495 if (isl_upoly_is_cst(up))
2496 return 0;
2498 if (up->var < d)
2499 active[up->var] = 1;
2501 rec = isl_upoly_as_rec(up);
2502 for (i = 0; i < rec->n; ++i)
2503 if (up_set_active(rec->p[i], active, d) < 0)
2504 return -1;
2506 return 0;
2509 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2511 int i, j;
2512 int d = isl_space_dim(qp->dim, isl_dim_all);
2514 if (!qp || !active)
2515 return -1;
2517 for (i = 0; i < d; ++i)
2518 for (j = 0; j < qp->div->n_row; ++j) {
2519 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2520 continue;
2521 active[i] = 1;
2522 break;
2525 return up_set_active(qp->upoly, active, d);
2528 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2529 enum isl_dim_type type, unsigned first, unsigned n)
2531 int i;
2532 int *active = NULL;
2533 isl_bool involves = isl_bool_false;
2535 if (!qp)
2536 return isl_bool_error;
2537 if (n == 0)
2538 return isl_bool_false;
2540 isl_assert(qp->dim->ctx,
2541 first + n <= isl_qpolynomial_dim(qp, type),
2542 return isl_bool_error);
2543 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2544 type == isl_dim_in, return isl_bool_error);
2546 active = isl_calloc_array(qp->dim->ctx, int,
2547 isl_space_dim(qp->dim, isl_dim_all));
2548 if (set_active(qp, active) < 0)
2549 goto error;
2551 if (type == isl_dim_in)
2552 first += isl_space_dim(qp->dim, isl_dim_param);
2553 for (i = 0; i < n; ++i)
2554 if (active[first + i]) {
2555 involves = isl_bool_true;
2556 break;
2559 free(active);
2561 return involves;
2562 error:
2563 free(active);
2564 return isl_bool_error;
2567 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2568 * of the divs that do appear in the quasi-polynomial.
2570 static __isl_give isl_qpolynomial *remove_redundant_divs(
2571 __isl_take isl_qpolynomial *qp)
2573 int i, j;
2574 int d;
2575 int len;
2576 int skip;
2577 int *active = NULL;
2578 int *reordering = NULL;
2579 int redundant = 0;
2580 int n_div;
2581 isl_ctx *ctx;
2583 if (!qp)
2584 return NULL;
2585 if (qp->div->n_row == 0)
2586 return qp;
2588 d = isl_space_dim(qp->dim, isl_dim_all);
2589 len = qp->div->n_col - 2;
2590 ctx = isl_qpolynomial_get_ctx(qp);
2591 active = isl_calloc_array(ctx, int, len);
2592 if (!active)
2593 goto error;
2595 if (up_set_active(qp->upoly, active, len) < 0)
2596 goto error;
2598 for (i = qp->div->n_row - 1; i >= 0; --i) {
2599 if (!active[d + i]) {
2600 redundant = 1;
2601 continue;
2603 for (j = 0; j < i; ++j) {
2604 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2605 continue;
2606 active[d + j] = 1;
2607 break;
2611 if (!redundant) {
2612 free(active);
2613 return qp;
2616 reordering = isl_alloc_array(qp->div->ctx, int, len);
2617 if (!reordering)
2618 goto error;
2620 for (i = 0; i < d; ++i)
2621 reordering[i] = i;
2623 skip = 0;
2624 n_div = qp->div->n_row;
2625 for (i = 0; i < n_div; ++i) {
2626 if (!active[d + i]) {
2627 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2628 qp->div = isl_mat_drop_cols(qp->div,
2629 2 + d + i - skip, 1);
2630 skip++;
2632 reordering[d + i] = d + i - skip;
2635 qp->upoly = reorder(qp->upoly, reordering);
2637 if (!qp->upoly || !qp->div)
2638 goto error;
2640 free(active);
2641 free(reordering);
2643 return qp;
2644 error:
2645 free(active);
2646 free(reordering);
2647 isl_qpolynomial_free(qp);
2648 return NULL;
2651 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2652 unsigned first, unsigned n)
2654 int i;
2655 struct isl_upoly_rec *rec;
2657 if (!up)
2658 return NULL;
2659 if (n == 0 || up->var < 0 || up->var < first)
2660 return up;
2661 if (up->var < first + n) {
2662 up = replace_by_constant_term(up);
2663 return isl_upoly_drop(up, first, n);
2665 up = isl_upoly_cow(up);
2666 if (!up)
2667 return NULL;
2668 up->var -= n;
2669 rec = isl_upoly_as_rec(up);
2670 if (!rec)
2671 goto error;
2673 for (i = 0; i < rec->n; ++i) {
2674 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2675 if (!rec->p[i])
2676 goto error;
2679 return up;
2680 error:
2681 isl_upoly_free(up);
2682 return NULL;
2685 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2686 __isl_take isl_qpolynomial *qp,
2687 enum isl_dim_type type, unsigned pos, const char *s)
2689 qp = isl_qpolynomial_cow(qp);
2690 if (!qp)
2691 return NULL;
2692 if (type == isl_dim_out)
2693 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2694 "cannot set name of output/set dimension",
2695 return isl_qpolynomial_free(qp));
2696 if (type == isl_dim_in)
2697 type = isl_dim_set;
2698 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2699 if (!qp->dim)
2700 goto error;
2701 return qp;
2702 error:
2703 isl_qpolynomial_free(qp);
2704 return NULL;
2707 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2708 __isl_take isl_qpolynomial *qp,
2709 enum isl_dim_type type, unsigned first, unsigned n)
2711 if (!qp)
2712 return NULL;
2713 if (type == isl_dim_out)
2714 isl_die(qp->dim->ctx, isl_error_invalid,
2715 "cannot drop output/set dimension",
2716 goto error);
2717 if (type == isl_dim_in)
2718 type = isl_dim_set;
2719 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2720 return qp;
2722 qp = isl_qpolynomial_cow(qp);
2723 if (!qp)
2724 return NULL;
2726 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2727 goto error);
2728 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2729 type == isl_dim_set, goto error);
2731 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2732 if (!qp->dim)
2733 goto error;
2735 if (type == isl_dim_set)
2736 first += isl_space_dim(qp->dim, isl_dim_param);
2738 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2739 if (!qp->div)
2740 goto error;
2742 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2743 if (!qp->upoly)
2744 goto error;
2746 return qp;
2747 error:
2748 isl_qpolynomial_free(qp);
2749 return NULL;
2752 /* Project the domain of the quasi-polynomial onto its parameter space.
2753 * The quasi-polynomial may not involve any of the domain dimensions.
2755 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2756 __isl_take isl_qpolynomial *qp)
2758 isl_space *space;
2759 unsigned n;
2760 int involves;
2762 n = isl_qpolynomial_dim(qp, isl_dim_in);
2763 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2764 if (involves < 0)
2765 return isl_qpolynomial_free(qp);
2766 if (involves)
2767 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2768 "polynomial involves some of the domain dimensions",
2769 return isl_qpolynomial_free(qp));
2770 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2771 space = isl_qpolynomial_get_domain_space(qp);
2772 space = isl_space_params(space);
2773 qp = isl_qpolynomial_reset_domain_space(qp, space);
2774 return qp;
2777 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2778 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2780 int i, j, k;
2781 isl_int denom;
2782 unsigned total;
2783 unsigned n_div;
2784 struct isl_upoly *up;
2786 if (!eq)
2787 goto error;
2788 if (eq->n_eq == 0) {
2789 isl_basic_set_free(eq);
2790 return qp;
2793 qp = isl_qpolynomial_cow(qp);
2794 if (!qp)
2795 goto error;
2796 qp->div = isl_mat_cow(qp->div);
2797 if (!qp->div)
2798 goto error;
2800 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2801 n_div = eq->n_div;
2802 isl_int_init(denom);
2803 for (i = 0; i < eq->n_eq; ++i) {
2804 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2805 if (j < 0 || j == 0 || j >= total)
2806 continue;
2808 for (k = 0; k < qp->div->n_row; ++k) {
2809 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2810 continue;
2811 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2812 &qp->div->row[k][0]);
2813 normalize_div(qp, k);
2816 if (isl_int_is_pos(eq->eq[i][j]))
2817 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2818 isl_int_abs(denom, eq->eq[i][j]);
2819 isl_int_set_si(eq->eq[i][j], 0);
2821 up = isl_upoly_from_affine(qp->dim->ctx,
2822 eq->eq[i], denom, total);
2823 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2824 isl_upoly_free(up);
2826 isl_int_clear(denom);
2828 if (!qp->upoly)
2829 goto error;
2831 isl_basic_set_free(eq);
2833 qp = substitute_non_divs(qp);
2834 qp = sort_divs(qp);
2836 return qp;
2837 error:
2838 isl_basic_set_free(eq);
2839 isl_qpolynomial_free(qp);
2840 return NULL;
2843 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2845 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2846 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2848 if (!qp || !eq)
2849 goto error;
2850 if (qp->div->n_row > 0)
2851 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2852 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2853 error:
2854 isl_basic_set_free(eq);
2855 isl_qpolynomial_free(qp);
2856 return NULL;
2859 static __isl_give isl_basic_set *add_div_constraints(
2860 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2862 int i;
2863 unsigned total;
2865 if (!bset || !div)
2866 goto error;
2868 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2869 if (!bset)
2870 goto error;
2871 total = isl_basic_set_total_dim(bset);
2872 for (i = 0; i < div->n_row; ++i)
2873 if (isl_basic_set_add_div_constraints_var(bset,
2874 total - div->n_row + i, div->row[i]) < 0)
2875 goto error;
2877 isl_mat_free(div);
2878 return bset;
2879 error:
2880 isl_mat_free(div);
2881 isl_basic_set_free(bset);
2882 return NULL;
2885 /* Look for equalities among the variables shared by context and qp
2886 * and the integer divisions of qp, if any.
2887 * The equalities are then used to eliminate variables and/or integer
2888 * divisions from qp.
2890 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2891 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2893 isl_basic_set *aff;
2895 if (!qp)
2896 goto error;
2897 if (qp->div->n_row > 0) {
2898 isl_basic_set *bset;
2899 context = isl_set_add_dims(context, isl_dim_set,
2900 qp->div->n_row);
2901 bset = isl_basic_set_universe(isl_set_get_space(context));
2902 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2903 context = isl_set_intersect(context,
2904 isl_set_from_basic_set(bset));
2907 aff = isl_set_affine_hull(context);
2908 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2909 error:
2910 isl_qpolynomial_free(qp);
2911 isl_set_free(context);
2912 return NULL;
2915 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2916 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2918 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2919 isl_set *dom_context = isl_set_universe(space);
2920 dom_context = isl_set_intersect_params(dom_context, context);
2921 return isl_qpolynomial_gist(qp, dom_context);
2924 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2925 __isl_take isl_qpolynomial *qp)
2927 isl_set *dom;
2929 if (!qp)
2930 return NULL;
2931 if (isl_qpolynomial_is_zero(qp)) {
2932 isl_space *dim = isl_qpolynomial_get_space(qp);
2933 isl_qpolynomial_free(qp);
2934 return isl_pw_qpolynomial_zero(dim);
2937 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2938 return isl_pw_qpolynomial_alloc(dom, qp);
2941 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2943 #undef PW
2944 #define PW isl_pw_qpolynomial
2945 #undef EL
2946 #define EL isl_qpolynomial
2947 #undef EL_IS_ZERO
2948 #define EL_IS_ZERO is_zero
2949 #undef ZERO
2950 #define ZERO zero
2951 #undef IS_ZERO
2952 #define IS_ZERO is_zero
2953 #undef FIELD
2954 #define FIELD qp
2955 #undef DEFAULT_IS_ZERO
2956 #define DEFAULT_IS_ZERO 1
2958 #define NO_PULLBACK
2960 #include <isl_pw_templ.c>
2962 #undef UNION
2963 #define UNION isl_union_pw_qpolynomial
2964 #undef PART
2965 #define PART isl_pw_qpolynomial
2966 #undef PARTS
2967 #define PARTS pw_qpolynomial
2969 #include <isl_union_single.c>
2970 #include <isl_union_eval.c>
2971 #include <isl_union_neg.c>
2973 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2975 if (!pwqp)
2976 return -1;
2978 if (pwqp->n != -1)
2979 return 0;
2981 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2982 return 0;
2984 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2987 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2988 __isl_take isl_pw_qpolynomial *pwqp1,
2989 __isl_take isl_pw_qpolynomial *pwqp2)
2991 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2994 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2995 __isl_take isl_pw_qpolynomial *pwqp1,
2996 __isl_take isl_pw_qpolynomial *pwqp2)
2998 int i, j, n;
2999 struct isl_pw_qpolynomial *res;
3001 if (!pwqp1 || !pwqp2)
3002 goto error;
3004 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3005 goto error);
3007 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3008 isl_pw_qpolynomial_free(pwqp2);
3009 return pwqp1;
3012 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3013 isl_pw_qpolynomial_free(pwqp1);
3014 return pwqp2;
3017 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3018 isl_pw_qpolynomial_free(pwqp1);
3019 return pwqp2;
3022 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3023 isl_pw_qpolynomial_free(pwqp2);
3024 return pwqp1;
3027 n = pwqp1->n * pwqp2->n;
3028 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3030 for (i = 0; i < pwqp1->n; ++i) {
3031 for (j = 0; j < pwqp2->n; ++j) {
3032 struct isl_set *common;
3033 struct isl_qpolynomial *prod;
3034 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3035 isl_set_copy(pwqp2->p[j].set));
3036 if (isl_set_plain_is_empty(common)) {
3037 isl_set_free(common);
3038 continue;
3041 prod = isl_qpolynomial_mul(
3042 isl_qpolynomial_copy(pwqp1->p[i].qp),
3043 isl_qpolynomial_copy(pwqp2->p[j].qp));
3045 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3049 isl_pw_qpolynomial_free(pwqp1);
3050 isl_pw_qpolynomial_free(pwqp2);
3052 return res;
3053 error:
3054 isl_pw_qpolynomial_free(pwqp1);
3055 isl_pw_qpolynomial_free(pwqp2);
3056 return NULL;
3059 __isl_give isl_val *isl_upoly_eval(__isl_take struct isl_upoly *up,
3060 __isl_take isl_vec *vec)
3062 int i;
3063 struct isl_upoly_rec *rec;
3064 isl_val *res;
3065 isl_val *base;
3067 if (isl_upoly_is_cst(up)) {
3068 isl_vec_free(vec);
3069 res = isl_upoly_get_constant_val(up);
3070 isl_upoly_free(up);
3071 return res;
3074 rec = isl_upoly_as_rec(up);
3075 if (!rec)
3076 goto error;
3078 isl_assert(up->ctx, rec->n >= 1, goto error);
3080 base = isl_val_rat_from_isl_int(up->ctx,
3081 vec->el[1 + up->var], vec->el[0]);
3083 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
3084 isl_vec_copy(vec));
3086 for (i = rec->n - 2; i >= 0; --i) {
3087 res = isl_val_mul(res, isl_val_copy(base));
3088 res = isl_val_add(res,
3089 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
3090 isl_vec_copy(vec)));
3093 isl_val_free(base);
3094 isl_upoly_free(up);
3095 isl_vec_free(vec);
3096 return res;
3097 error:
3098 isl_upoly_free(up);
3099 isl_vec_free(vec);
3100 return NULL;
3103 /* Evaluate "qp" in the void point "pnt".
3104 * In particular, return the value NaN.
3106 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3107 __isl_take isl_point *pnt)
3109 isl_ctx *ctx;
3111 ctx = isl_point_get_ctx(pnt);
3112 isl_qpolynomial_free(qp);
3113 isl_point_free(pnt);
3114 return isl_val_nan(ctx);
3117 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3118 __isl_take isl_point *pnt)
3120 isl_bool is_void;
3121 isl_vec *ext;
3122 isl_val *v;
3124 if (!qp || !pnt)
3125 goto error;
3126 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3127 is_void = isl_point_is_void(pnt);
3128 if (is_void < 0)
3129 goto error;
3130 if (is_void)
3131 return eval_void(qp, pnt);
3133 if (qp->div->n_row == 0)
3134 ext = isl_vec_copy(pnt->vec);
3135 else {
3136 int i;
3137 unsigned dim = isl_space_dim(qp->dim, isl_dim_all);
3138 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
3139 if (!ext)
3140 goto error;
3142 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
3143 for (i = 0; i < qp->div->n_row; ++i) {
3144 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
3145 1 + dim + i, &ext->el[1+dim+i]);
3146 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
3147 qp->div->row[i][0]);
3151 v = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
3153 isl_qpolynomial_free(qp);
3154 isl_point_free(pnt);
3156 return v;
3157 error:
3158 isl_qpolynomial_free(qp);
3159 isl_point_free(pnt);
3160 return NULL;
3163 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
3164 __isl_keep struct isl_upoly_cst *cst2)
3166 int cmp;
3167 isl_int t;
3168 isl_int_init(t);
3169 isl_int_mul(t, cst1->n, cst2->d);
3170 isl_int_submul(t, cst2->n, cst1->d);
3171 cmp = isl_int_sgn(t);
3172 isl_int_clear(t);
3173 return cmp;
3176 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3177 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3178 unsigned first, unsigned n)
3180 unsigned total;
3181 unsigned g_pos;
3182 int *exp;
3184 if (!qp)
3185 return NULL;
3186 if (type == isl_dim_out)
3187 isl_die(qp->div->ctx, isl_error_invalid,
3188 "cannot insert output/set dimensions",
3189 goto error);
3190 if (type == isl_dim_in)
3191 type = isl_dim_set;
3192 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3193 return qp;
3195 qp = isl_qpolynomial_cow(qp);
3196 if (!qp)
3197 return NULL;
3199 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
3200 goto error);
3202 g_pos = pos(qp->dim, type) + first;
3204 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3205 if (!qp->div)
3206 goto error;
3208 total = qp->div->n_col - 2;
3209 if (total > g_pos) {
3210 int i;
3211 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3212 if (!exp)
3213 goto error;
3214 for (i = 0; i < total - g_pos; ++i)
3215 exp[i] = i + n;
3216 qp->upoly = expand(qp->upoly, exp, g_pos);
3217 free(exp);
3218 if (!qp->upoly)
3219 goto error;
3222 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3223 if (!qp->dim)
3224 goto error;
3226 return qp;
3227 error:
3228 isl_qpolynomial_free(qp);
3229 return NULL;
3232 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3233 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3235 unsigned pos;
3237 pos = isl_qpolynomial_dim(qp, type);
3239 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3242 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3243 __isl_take isl_pw_qpolynomial *pwqp,
3244 enum isl_dim_type type, unsigned n)
3246 unsigned pos;
3248 pos = isl_pw_qpolynomial_dim(pwqp, type);
3250 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3253 static int *reordering_move(isl_ctx *ctx,
3254 unsigned len, unsigned dst, unsigned src, unsigned n)
3256 int i;
3257 int *reordering;
3259 reordering = isl_alloc_array(ctx, int, len);
3260 if (!reordering)
3261 return NULL;
3263 if (dst <= src) {
3264 for (i = 0; i < dst; ++i)
3265 reordering[i] = i;
3266 for (i = 0; i < n; ++i)
3267 reordering[src + i] = dst + i;
3268 for (i = 0; i < src - dst; ++i)
3269 reordering[dst + i] = dst + n + i;
3270 for (i = 0; i < len - src - n; ++i)
3271 reordering[src + n + i] = src + n + i;
3272 } else {
3273 for (i = 0; i < src; ++i)
3274 reordering[i] = i;
3275 for (i = 0; i < n; ++i)
3276 reordering[src + i] = dst + i;
3277 for (i = 0; i < dst - src; ++i)
3278 reordering[src + n + i] = src + i;
3279 for (i = 0; i < len - dst - n; ++i)
3280 reordering[dst + n + i] = dst + n + i;
3283 return reordering;
3286 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3287 __isl_take isl_qpolynomial *qp,
3288 enum isl_dim_type dst_type, unsigned dst_pos,
3289 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3291 unsigned g_dst_pos;
3292 unsigned g_src_pos;
3293 int *reordering;
3295 if (!qp)
3296 return NULL;
3298 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3299 isl_die(qp->dim->ctx, isl_error_invalid,
3300 "cannot move output/set dimension",
3301 goto error);
3302 if (dst_type == isl_dim_in)
3303 dst_type = isl_dim_set;
3304 if (src_type == isl_dim_in)
3305 src_type = isl_dim_set;
3307 if (n == 0 &&
3308 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3309 !isl_space_is_named_or_nested(qp->dim, dst_type))
3310 return qp;
3312 qp = isl_qpolynomial_cow(qp);
3313 if (!qp)
3314 return NULL;
3316 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
3317 goto error);
3319 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3320 g_src_pos = pos(qp->dim, src_type) + src_pos;
3321 if (dst_type > src_type)
3322 g_dst_pos -= n;
3324 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3325 if (!qp->div)
3326 goto error;
3327 qp = sort_divs(qp);
3328 if (!qp)
3329 goto error;
3331 reordering = reordering_move(qp->dim->ctx,
3332 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3333 if (!reordering)
3334 goto error;
3336 qp->upoly = reorder(qp->upoly, reordering);
3337 free(reordering);
3338 if (!qp->upoly)
3339 goto error;
3341 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3342 if (!qp->dim)
3343 goto error;
3345 return qp;
3346 error:
3347 isl_qpolynomial_free(qp);
3348 return NULL;
3351 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3352 isl_int *f, isl_int denom)
3354 struct isl_upoly *up;
3356 dim = isl_space_domain(dim);
3357 if (!dim)
3358 return NULL;
3360 up = isl_upoly_from_affine(dim->ctx, f, denom,
3361 1 + isl_space_dim(dim, isl_dim_all));
3363 return isl_qpolynomial_alloc(dim, 0, up);
3366 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3368 isl_ctx *ctx;
3369 struct isl_upoly *up;
3370 isl_qpolynomial *qp;
3372 if (!aff)
3373 return NULL;
3375 ctx = isl_aff_get_ctx(aff);
3376 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3377 aff->v->size - 1);
3379 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3380 aff->ls->div->n_row, up);
3381 if (!qp)
3382 goto error;
3384 isl_mat_free(qp->div);
3385 qp->div = isl_mat_copy(aff->ls->div);
3386 qp->div = isl_mat_cow(qp->div);
3387 if (!qp->div)
3388 goto error;
3390 isl_aff_free(aff);
3391 qp = reduce_divs(qp);
3392 qp = remove_redundant_divs(qp);
3393 return qp;
3394 error:
3395 isl_aff_free(aff);
3396 return isl_qpolynomial_free(qp);
3399 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3400 __isl_take isl_pw_aff *pwaff)
3402 int i;
3403 isl_pw_qpolynomial *pwqp;
3405 if (!pwaff)
3406 return NULL;
3408 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3409 pwaff->n);
3411 for (i = 0; i < pwaff->n; ++i) {
3412 isl_set *dom;
3413 isl_qpolynomial *qp;
3415 dom = isl_set_copy(pwaff->p[i].set);
3416 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3417 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3420 isl_pw_aff_free(pwaff);
3421 return pwqp;
3424 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3425 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3427 isl_aff *aff;
3429 aff = isl_constraint_get_bound(c, type, pos);
3430 isl_constraint_free(c);
3431 return isl_qpolynomial_from_aff(aff);
3434 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3435 * in "qp" by subs[i].
3437 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3438 __isl_take isl_qpolynomial *qp,
3439 enum isl_dim_type type, unsigned first, unsigned n,
3440 __isl_keep isl_qpolynomial **subs)
3442 int i;
3443 struct isl_upoly **ups;
3445 if (n == 0)
3446 return qp;
3448 qp = isl_qpolynomial_cow(qp);
3449 if (!qp)
3450 return NULL;
3452 if (type == isl_dim_out)
3453 isl_die(qp->dim->ctx, isl_error_invalid,
3454 "cannot substitute output/set dimension",
3455 goto error);
3456 if (type == isl_dim_in)
3457 type = isl_dim_set;
3459 for (i = 0; i < n; ++i)
3460 if (!subs[i])
3461 goto error;
3463 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3464 goto error);
3466 for (i = 0; i < n; ++i)
3467 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3468 goto error);
3470 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3471 for (i = 0; i < n; ++i)
3472 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3474 first += pos(qp->dim, type);
3476 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3477 if (!ups)
3478 goto error;
3479 for (i = 0; i < n; ++i)
3480 ups[i] = subs[i]->upoly;
3482 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3484 free(ups);
3486 if (!qp->upoly)
3487 goto error;
3489 return qp;
3490 error:
3491 isl_qpolynomial_free(qp);
3492 return NULL;
3495 /* Extend "bset" with extra set dimensions for each integer division
3496 * in "qp" and then call "fn" with the extended bset and the polynomial
3497 * that results from replacing each of the integer divisions by the
3498 * corresponding extra set dimension.
3500 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3501 __isl_keep isl_basic_set *bset,
3502 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3503 __isl_take isl_qpolynomial *poly, void *user), void *user)
3505 isl_space *dim;
3506 isl_mat *div;
3507 isl_qpolynomial *poly;
3509 if (!qp || !bset)
3510 return isl_stat_error;
3511 if (qp->div->n_row == 0)
3512 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3513 user);
3515 div = isl_mat_copy(qp->div);
3516 dim = isl_space_copy(qp->dim);
3517 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3518 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3519 bset = isl_basic_set_copy(bset);
3520 bset = isl_basic_set_add_dims(bset, isl_dim_set, qp->div->n_row);
3521 bset = add_div_constraints(bset, div);
3523 return fn(bset, poly, user);
3526 /* Return total degree in variables first (inclusive) up to last (exclusive).
3528 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3530 int deg = -1;
3531 int i;
3532 struct isl_upoly_rec *rec;
3534 if (!up)
3535 return -2;
3536 if (isl_upoly_is_zero(up))
3537 return -1;
3538 if (isl_upoly_is_cst(up) || up->var < first)
3539 return 0;
3541 rec = isl_upoly_as_rec(up);
3542 if (!rec)
3543 return -2;
3545 for (i = 0; i < rec->n; ++i) {
3546 int d;
3548 if (isl_upoly_is_zero(rec->p[i]))
3549 continue;
3550 d = isl_upoly_degree(rec->p[i], first, last);
3551 if (up->var < last)
3552 d += i;
3553 if (d > deg)
3554 deg = d;
3557 return deg;
3560 /* Return total degree in set variables.
3562 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3564 unsigned ovar;
3565 unsigned nvar;
3567 if (!poly)
3568 return -2;
3570 ovar = isl_space_offset(poly->dim, isl_dim_set);
3571 nvar = isl_space_dim(poly->dim, isl_dim_set);
3572 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3575 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3576 unsigned pos, int deg)
3578 int i;
3579 struct isl_upoly_rec *rec;
3581 if (!up)
3582 return NULL;
3584 if (isl_upoly_is_cst(up) || up->var < pos) {
3585 if (deg == 0)
3586 return isl_upoly_copy(up);
3587 else
3588 return isl_upoly_zero(up->ctx);
3591 rec = isl_upoly_as_rec(up);
3592 if (!rec)
3593 return NULL;
3595 if (up->var == pos) {
3596 if (deg < rec->n)
3597 return isl_upoly_copy(rec->p[deg]);
3598 else
3599 return isl_upoly_zero(up->ctx);
3602 up = isl_upoly_copy(up);
3603 up = isl_upoly_cow(up);
3604 rec = isl_upoly_as_rec(up);
3605 if (!rec)
3606 goto error;
3608 for (i = 0; i < rec->n; ++i) {
3609 struct isl_upoly *t;
3610 t = isl_upoly_coeff(rec->p[i], pos, deg);
3611 if (!t)
3612 goto error;
3613 isl_upoly_free(rec->p[i]);
3614 rec->p[i] = t;
3617 return up;
3618 error:
3619 isl_upoly_free(up);
3620 return NULL;
3623 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3625 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3626 __isl_keep isl_qpolynomial *qp,
3627 enum isl_dim_type type, unsigned t_pos, int deg)
3629 unsigned g_pos;
3630 struct isl_upoly *up;
3631 isl_qpolynomial *c;
3633 if (!qp)
3634 return NULL;
3636 if (type == isl_dim_out)
3637 isl_die(qp->div->ctx, isl_error_invalid,
3638 "output/set dimension does not have a coefficient",
3639 return NULL);
3640 if (type == isl_dim_in)
3641 type = isl_dim_set;
3643 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3644 return NULL);
3646 g_pos = pos(qp->dim, type) + t_pos;
3647 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3649 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3650 if (!c)
3651 return NULL;
3652 isl_mat_free(c->div);
3653 c->div = isl_mat_copy(qp->div);
3654 if (!c->div)
3655 goto error;
3656 return c;
3657 error:
3658 isl_qpolynomial_free(c);
3659 return NULL;
3662 /* Homogenize the polynomial in the variables first (inclusive) up to
3663 * last (exclusive) by inserting powers of variable first.
3664 * Variable first is assumed not to appear in the input.
3666 __isl_give struct isl_upoly *isl_upoly_homogenize(
3667 __isl_take struct isl_upoly *up, int deg, int target,
3668 int first, int last)
3670 int i;
3671 struct isl_upoly_rec *rec;
3673 if (!up)
3674 return NULL;
3675 if (isl_upoly_is_zero(up))
3676 return up;
3677 if (deg == target)
3678 return up;
3679 if (isl_upoly_is_cst(up) || up->var < first) {
3680 struct isl_upoly *hom;
3682 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3683 if (!hom)
3684 goto error;
3685 rec = isl_upoly_as_rec(hom);
3686 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3688 return hom;
3691 up = isl_upoly_cow(up);
3692 rec = isl_upoly_as_rec(up);
3693 if (!rec)
3694 goto error;
3696 for (i = 0; i < rec->n; ++i) {
3697 if (isl_upoly_is_zero(rec->p[i]))
3698 continue;
3699 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3700 up->var < last ? deg + i : i, target,
3701 first, last);
3702 if (!rec->p[i])
3703 goto error;
3706 return up;
3707 error:
3708 isl_upoly_free(up);
3709 return NULL;
3712 /* Homogenize the polynomial in the set variables by introducing
3713 * powers of an extra set variable at position 0.
3715 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3716 __isl_take isl_qpolynomial *poly)
3718 unsigned ovar;
3719 unsigned nvar;
3720 int deg = isl_qpolynomial_degree(poly);
3722 if (deg < -1)
3723 goto error;
3725 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3726 poly = isl_qpolynomial_cow(poly);
3727 if (!poly)
3728 goto error;
3730 ovar = isl_space_offset(poly->dim, isl_dim_set);
3731 nvar = isl_space_dim(poly->dim, isl_dim_set);
3732 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3733 ovar, ovar + nvar);
3734 if (!poly->upoly)
3735 goto error;
3737 return poly;
3738 error:
3739 isl_qpolynomial_free(poly);
3740 return NULL;
3743 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3744 __isl_take isl_mat *div)
3746 isl_term *term;
3747 int n;
3749 if (!dim || !div)
3750 goto error;
3752 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3754 term = isl_calloc(dim->ctx, struct isl_term,
3755 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3756 if (!term)
3757 goto error;
3759 term->ref = 1;
3760 term->dim = dim;
3761 term->div = div;
3762 isl_int_init(term->n);
3763 isl_int_init(term->d);
3765 return term;
3766 error:
3767 isl_space_free(dim);
3768 isl_mat_free(div);
3769 return NULL;
3772 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3774 if (!term)
3775 return NULL;
3777 term->ref++;
3778 return term;
3781 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3783 int i;
3784 isl_term *dup;
3785 unsigned total;
3787 if (!term)
3788 return NULL;
3790 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3792 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3793 if (!dup)
3794 return NULL;
3796 isl_int_set(dup->n, term->n);
3797 isl_int_set(dup->d, term->d);
3799 for (i = 0; i < total; ++i)
3800 dup->pow[i] = term->pow[i];
3802 return dup;
3805 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3807 if (!term)
3808 return NULL;
3810 if (term->ref == 1)
3811 return term;
3812 term->ref--;
3813 return isl_term_dup(term);
3816 void isl_term_free(__isl_take isl_term *term)
3818 if (!term)
3819 return;
3821 if (--term->ref > 0)
3822 return;
3824 isl_space_free(term->dim);
3825 isl_mat_free(term->div);
3826 isl_int_clear(term->n);
3827 isl_int_clear(term->d);
3828 free(term);
3831 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3833 if (!term)
3834 return 0;
3836 switch (type) {
3837 case isl_dim_param:
3838 case isl_dim_in:
3839 case isl_dim_out: return isl_space_dim(term->dim, type);
3840 case isl_dim_div: return term->div->n_row;
3841 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3842 term->div->n_row;
3843 default: return 0;
3847 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3849 return term ? term->dim->ctx : NULL;
3852 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3854 if (!term)
3855 return;
3856 isl_int_set(*n, term->n);
3859 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3861 if (!term)
3862 return;
3863 isl_int_set(*d, term->d);
3866 /* Return the coefficient of the term "term".
3868 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3870 if (!term)
3871 return NULL;
3873 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3874 term->n, term->d);
3877 int isl_term_get_exp(__isl_keep isl_term *term,
3878 enum isl_dim_type type, unsigned pos)
3880 if (!term)
3881 return -1;
3883 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3885 if (type >= isl_dim_set)
3886 pos += isl_space_dim(term->dim, isl_dim_param);
3887 if (type >= isl_dim_div)
3888 pos += isl_space_dim(term->dim, isl_dim_set);
3890 return term->pow[pos];
3893 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3895 isl_local_space *ls;
3896 isl_aff *aff;
3898 if (!term)
3899 return NULL;
3901 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3902 return NULL);
3904 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3905 isl_mat_copy(term->div));
3906 aff = isl_aff_alloc(ls);
3907 if (!aff)
3908 return NULL;
3910 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3912 aff = isl_aff_normalize(aff);
3914 return aff;
3917 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3918 isl_stat (*fn)(__isl_take isl_term *term, void *user),
3919 __isl_take isl_term *term, void *user)
3921 int i;
3922 struct isl_upoly_rec *rec;
3924 if (!up || !term)
3925 goto error;
3927 if (isl_upoly_is_zero(up))
3928 return term;
3930 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3931 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3932 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3934 if (isl_upoly_is_cst(up)) {
3935 struct isl_upoly_cst *cst;
3936 cst = isl_upoly_as_cst(up);
3937 if (!cst)
3938 goto error;
3939 term = isl_term_cow(term);
3940 if (!term)
3941 goto error;
3942 isl_int_set(term->n, cst->n);
3943 isl_int_set(term->d, cst->d);
3944 if (fn(isl_term_copy(term), user) < 0)
3945 goto error;
3946 return term;
3949 rec = isl_upoly_as_rec(up);
3950 if (!rec)
3951 goto error;
3953 for (i = 0; i < rec->n; ++i) {
3954 term = isl_term_cow(term);
3955 if (!term)
3956 goto error;
3957 term->pow[up->var] = i;
3958 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3959 if (!term)
3960 goto error;
3962 term->pow[up->var] = 0;
3964 return term;
3965 error:
3966 isl_term_free(term);
3967 return NULL;
3970 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3971 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
3973 isl_term *term;
3975 if (!qp)
3976 return isl_stat_error;
3978 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3979 if (!term)
3980 return isl_stat_error;
3982 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3984 isl_term_free(term);
3986 return term ? isl_stat_ok : isl_stat_error;
3989 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3991 struct isl_upoly *up;
3992 isl_qpolynomial *qp;
3993 int i, n;
3995 if (!term)
3996 return NULL;
3998 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
4000 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
4001 for (i = 0; i < n; ++i) {
4002 if (!term->pow[i])
4003 continue;
4004 up = isl_upoly_mul(up,
4005 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
4008 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
4009 if (!qp)
4010 goto error;
4011 isl_mat_free(qp->div);
4012 qp->div = isl_mat_copy(term->div);
4013 if (!qp->div)
4014 goto error;
4016 isl_term_free(term);
4017 return qp;
4018 error:
4019 isl_qpolynomial_free(qp);
4020 isl_term_free(term);
4021 return NULL;
4024 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4025 __isl_take isl_space *dim)
4027 int i;
4028 int extra;
4029 unsigned total;
4031 if (!qp || !dim)
4032 goto error;
4034 if (isl_space_is_equal(qp->dim, dim)) {
4035 isl_space_free(dim);
4036 return qp;
4039 qp = isl_qpolynomial_cow(qp);
4040 if (!qp)
4041 goto error;
4043 extra = isl_space_dim(dim, isl_dim_set) -
4044 isl_space_dim(qp->dim, isl_dim_set);
4045 total = isl_space_dim(qp->dim, isl_dim_all);
4046 if (qp->div->n_row) {
4047 int *exp;
4049 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4050 if (!exp)
4051 goto error;
4052 for (i = 0; i < qp->div->n_row; ++i)
4053 exp[i] = extra + i;
4054 qp->upoly = expand(qp->upoly, exp, total);
4055 free(exp);
4056 if (!qp->upoly)
4057 goto error;
4059 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4060 if (!qp->div)
4061 goto error;
4062 for (i = 0; i < qp->div->n_row; ++i)
4063 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4065 isl_space_free(qp->dim);
4066 qp->dim = dim;
4068 return qp;
4069 error:
4070 isl_space_free(dim);
4071 isl_qpolynomial_free(qp);
4072 return NULL;
4075 /* For each parameter or variable that does not appear in qp,
4076 * first eliminate the variable from all constraints and then set it to zero.
4078 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4079 __isl_keep isl_qpolynomial *qp)
4081 int *active = NULL;
4082 int i;
4083 int d;
4084 unsigned nparam;
4085 unsigned nvar;
4087 if (!set || !qp)
4088 goto error;
4090 d = isl_space_dim(set->dim, isl_dim_all);
4091 active = isl_calloc_array(set->ctx, int, d);
4092 if (set_active(qp, active) < 0)
4093 goto error;
4095 for (i = 0; i < d; ++i)
4096 if (!active[i])
4097 break;
4099 if (i == d) {
4100 free(active);
4101 return set;
4104 nparam = isl_space_dim(set->dim, isl_dim_param);
4105 nvar = isl_space_dim(set->dim, isl_dim_set);
4106 for (i = 0; i < nparam; ++i) {
4107 if (active[i])
4108 continue;
4109 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4110 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4112 for (i = 0; i < nvar; ++i) {
4113 if (active[nparam + i])
4114 continue;
4115 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4116 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4119 free(active);
4121 return set;
4122 error:
4123 free(active);
4124 isl_set_free(set);
4125 return NULL;
4128 struct isl_opt_data {
4129 isl_qpolynomial *qp;
4130 int first;
4131 isl_val *opt;
4132 int max;
4135 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4137 struct isl_opt_data *data = (struct isl_opt_data *)user;
4138 isl_val *val;
4140 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4141 if (data->first) {
4142 data->first = 0;
4143 data->opt = val;
4144 } else if (data->max) {
4145 data->opt = isl_val_max(data->opt, val);
4146 } else {
4147 data->opt = isl_val_min(data->opt, val);
4150 return isl_stat_ok;
4153 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4154 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4156 struct isl_opt_data data = { NULL, 1, NULL, max };
4158 if (!set || !qp)
4159 goto error;
4161 if (isl_upoly_is_cst(qp->upoly)) {
4162 isl_set_free(set);
4163 data.opt = isl_qpolynomial_get_constant_val(qp);
4164 isl_qpolynomial_free(qp);
4165 return data.opt;
4168 set = fix_inactive(set, qp);
4170 data.qp = qp;
4171 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4172 goto error;
4174 if (data.first)
4175 data.opt = isl_val_zero(isl_set_get_ctx(set));
4177 isl_set_free(set);
4178 isl_qpolynomial_free(qp);
4179 return data.opt;
4180 error:
4181 isl_set_free(set);
4182 isl_qpolynomial_free(qp);
4183 isl_val_free(data.opt);
4184 return NULL;
4187 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4188 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4190 int i;
4191 int n_sub;
4192 isl_ctx *ctx;
4193 struct isl_upoly **subs;
4194 isl_mat *mat, *diag;
4196 qp = isl_qpolynomial_cow(qp);
4197 if (!qp || !morph)
4198 goto error;
4200 ctx = qp->dim->ctx;
4201 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4203 n_sub = morph->inv->n_row - 1;
4204 if (morph->inv->n_row != morph->inv->n_col)
4205 n_sub += qp->div->n_row;
4206 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4207 if (n_sub && !subs)
4208 goto error;
4210 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4211 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4212 morph->inv->row[0][0], morph->inv->n_col);
4213 if (morph->inv->n_row != morph->inv->n_col)
4214 for (i = 0; i < qp->div->n_row; ++i)
4215 subs[morph->inv->n_row - 1 + i] =
4216 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4218 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4220 for (i = 0; i < n_sub; ++i)
4221 isl_upoly_free(subs[i]);
4222 free(subs);
4224 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4225 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4226 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4227 mat = isl_mat_diagonal(mat, diag);
4228 qp->div = isl_mat_product(qp->div, mat);
4229 isl_space_free(qp->dim);
4230 qp->dim = isl_space_copy(morph->ran->dim);
4232 if (!qp->upoly || !qp->div || !qp->dim)
4233 goto error;
4235 isl_morph_free(morph);
4237 return qp;
4238 error:
4239 isl_qpolynomial_free(qp);
4240 isl_morph_free(morph);
4241 return NULL;
4244 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4245 __isl_take isl_union_pw_qpolynomial *upwqp1,
4246 __isl_take isl_union_pw_qpolynomial *upwqp2)
4248 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4249 &isl_pw_qpolynomial_mul);
4252 /* Reorder the columns of the given div definitions according to the
4253 * given reordering.
4255 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
4256 __isl_take isl_reordering *r)
4258 int i, j;
4259 isl_mat *mat;
4260 int extra;
4262 if (!div || !r)
4263 goto error;
4265 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
4266 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
4267 if (!mat)
4268 goto error;
4270 for (i = 0; i < div->n_row; ++i) {
4271 isl_seq_cpy(mat->row[i], div->row[i], 2);
4272 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
4273 for (j = 0; j < r->len; ++j)
4274 isl_int_set(mat->row[i][2 + r->pos[j]],
4275 div->row[i][2 + j]);
4278 isl_reordering_free(r);
4279 isl_mat_free(div);
4280 return mat;
4281 error:
4282 isl_reordering_free(r);
4283 isl_mat_free(div);
4284 return NULL;
4287 /* Reorder the dimension of "qp" according to the given reordering.
4289 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4290 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4292 qp = isl_qpolynomial_cow(qp);
4293 if (!qp)
4294 goto error;
4296 r = isl_reordering_extend(r, qp->div->n_row);
4297 if (!r)
4298 goto error;
4300 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
4301 if (!qp->div)
4302 goto error;
4304 qp->upoly = reorder(qp->upoly, r->pos);
4305 if (!qp->upoly)
4306 goto error;
4308 qp = isl_qpolynomial_reset_domain_space(qp, isl_space_copy(r->dim));
4310 isl_reordering_free(r);
4311 return qp;
4312 error:
4313 isl_qpolynomial_free(qp);
4314 isl_reordering_free(r);
4315 return NULL;
4318 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4319 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4321 isl_bool equal_params;
4323 if (!qp || !model)
4324 goto error;
4326 equal_params = isl_space_has_equal_params(qp->dim, model);
4327 if (equal_params < 0)
4328 goto error;
4329 if (!equal_params) {
4330 isl_reordering *exp;
4332 model = isl_space_drop_dims(model, isl_dim_in,
4333 0, isl_space_dim(model, isl_dim_in));
4334 model = isl_space_drop_dims(model, isl_dim_out,
4335 0, isl_space_dim(model, isl_dim_out));
4336 exp = isl_parameter_alignment_reordering(qp->dim, model);
4337 exp = isl_reordering_extend_space(exp,
4338 isl_qpolynomial_get_domain_space(qp));
4339 qp = isl_qpolynomial_realign_domain(qp, exp);
4342 isl_space_free(model);
4343 return qp;
4344 error:
4345 isl_space_free(model);
4346 isl_qpolynomial_free(qp);
4347 return NULL;
4350 struct isl_split_periods_data {
4351 int max_periods;
4352 isl_pw_qpolynomial *res;
4355 /* Create a slice where the integer division "div" has the fixed value "v".
4356 * In particular, if "div" refers to floor(f/m), then create a slice
4358 * m v <= f <= m v + (m - 1)
4360 * or
4362 * f - m v >= 0
4363 * -f + m v + (m - 1) >= 0
4365 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4366 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4368 int total;
4369 isl_basic_set *bset = NULL;
4370 int k;
4372 if (!dim || !qp)
4373 goto error;
4375 total = isl_space_dim(dim, isl_dim_all);
4376 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4378 k = isl_basic_set_alloc_inequality(bset);
4379 if (k < 0)
4380 goto error;
4381 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4382 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4384 k = isl_basic_set_alloc_inequality(bset);
4385 if (k < 0)
4386 goto error;
4387 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4388 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4389 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4390 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4392 isl_space_free(dim);
4393 return isl_set_from_basic_set(bset);
4394 error:
4395 isl_basic_set_free(bset);
4396 isl_space_free(dim);
4397 return NULL;
4400 static isl_stat split_periods(__isl_take isl_set *set,
4401 __isl_take isl_qpolynomial *qp, void *user);
4403 /* Create a slice of the domain "set" such that integer division "div"
4404 * has the fixed value "v" and add the results to data->res,
4405 * replacing the integer division by "v" in "qp".
4407 static isl_stat set_div(__isl_take isl_set *set,
4408 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4409 struct isl_split_periods_data *data)
4411 int i;
4412 int total;
4413 isl_set *slice;
4414 struct isl_upoly *cst;
4416 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4417 set = isl_set_intersect(set, slice);
4419 if (!qp)
4420 goto error;
4422 total = isl_space_dim(qp->dim, isl_dim_all);
4424 for (i = div + 1; i < qp->div->n_row; ++i) {
4425 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4426 continue;
4427 isl_int_addmul(qp->div->row[i][1],
4428 qp->div->row[i][2 + total + div], v);
4429 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4432 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4433 qp = substitute_div(qp, div, cst);
4435 return split_periods(set, qp, data);
4436 error:
4437 isl_set_free(set);
4438 isl_qpolynomial_free(qp);
4439 return -1;
4442 /* Split the domain "set" such that integer division "div"
4443 * has a fixed value (ranging from "min" to "max") on each slice
4444 * and add the results to data->res.
4446 static isl_stat split_div(__isl_take isl_set *set,
4447 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4448 struct isl_split_periods_data *data)
4450 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4451 isl_set *set_i = isl_set_copy(set);
4452 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4454 if (set_div(set_i, qp_i, div, min, data) < 0)
4455 goto error;
4457 isl_set_free(set);
4458 isl_qpolynomial_free(qp);
4459 return isl_stat_ok;
4460 error:
4461 isl_set_free(set);
4462 isl_qpolynomial_free(qp);
4463 return isl_stat_error;
4466 /* If "qp" refers to any integer division
4467 * that can only attain "max_periods" distinct values on "set"
4468 * then split the domain along those distinct values.
4469 * Add the results (or the original if no splitting occurs)
4470 * to data->res.
4472 static isl_stat split_periods(__isl_take isl_set *set,
4473 __isl_take isl_qpolynomial *qp, void *user)
4475 int i;
4476 isl_pw_qpolynomial *pwqp;
4477 struct isl_split_periods_data *data;
4478 isl_int min, max;
4479 int total;
4480 isl_stat r = isl_stat_ok;
4482 data = (struct isl_split_periods_data *)user;
4484 if (!set || !qp)
4485 goto error;
4487 if (qp->div->n_row == 0) {
4488 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4489 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4490 return isl_stat_ok;
4493 isl_int_init(min);
4494 isl_int_init(max);
4495 total = isl_space_dim(qp->dim, isl_dim_all);
4496 for (i = 0; i < qp->div->n_row; ++i) {
4497 enum isl_lp_result lp_res;
4499 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4500 qp->div->n_row) != -1)
4501 continue;
4503 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4504 set->ctx->one, &min, NULL, NULL);
4505 if (lp_res == isl_lp_error)
4506 goto error2;
4507 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4508 continue;
4509 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4511 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4512 set->ctx->one, &max, NULL, NULL);
4513 if (lp_res == isl_lp_error)
4514 goto error2;
4515 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4516 continue;
4517 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4519 isl_int_sub(max, max, min);
4520 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4521 isl_int_add(max, max, min);
4522 break;
4526 if (i < qp->div->n_row) {
4527 r = split_div(set, qp, i, min, max, data);
4528 } else {
4529 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4530 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4533 isl_int_clear(max);
4534 isl_int_clear(min);
4536 return r;
4537 error2:
4538 isl_int_clear(max);
4539 isl_int_clear(min);
4540 error:
4541 isl_set_free(set);
4542 isl_qpolynomial_free(qp);
4543 return isl_stat_error;
4546 /* If any quasi-polynomial in pwqp refers to any integer division
4547 * that can only attain "max_periods" distinct values on its domain
4548 * then split the domain along those distinct values.
4550 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4551 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4553 struct isl_split_periods_data data;
4555 data.max_periods = max_periods;
4556 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4558 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4559 goto error;
4561 isl_pw_qpolynomial_free(pwqp);
4563 return data.res;
4564 error:
4565 isl_pw_qpolynomial_free(data.res);
4566 isl_pw_qpolynomial_free(pwqp);
4567 return NULL;
4570 /* Construct a piecewise quasipolynomial that is constant on the given
4571 * domain. In particular, it is
4572 * 0 if cst == 0
4573 * 1 if cst == 1
4574 * infinity if cst == -1
4576 * If cst == -1, then explicitly check whether the domain is empty and,
4577 * if so, return 0 instead.
4579 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4580 __isl_take isl_basic_set *bset, int cst)
4582 isl_space *dim;
4583 isl_qpolynomial *qp;
4585 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4586 cst = 0;
4587 if (!bset)
4588 return NULL;
4590 bset = isl_basic_set_params(bset);
4591 dim = isl_basic_set_get_space(bset);
4592 if (cst < 0)
4593 qp = isl_qpolynomial_infty_on_domain(dim);
4594 else if (cst == 0)
4595 qp = isl_qpolynomial_zero_on_domain(dim);
4596 else
4597 qp = isl_qpolynomial_one_on_domain(dim);
4598 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4601 /* Factor bset, call fn on each of the factors and return the product.
4603 * If no factors can be found, simply call fn on the input.
4604 * Otherwise, construct the factors based on the factorizer,
4605 * call fn on each factor and compute the product.
4607 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4608 __isl_take isl_basic_set *bset,
4609 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4611 int i, n;
4612 isl_space *space;
4613 isl_set *set;
4614 isl_factorizer *f;
4615 isl_qpolynomial *qp;
4616 isl_pw_qpolynomial *pwqp;
4617 unsigned nparam;
4618 unsigned nvar;
4620 f = isl_basic_set_factorizer(bset);
4621 if (!f)
4622 goto error;
4623 if (f->n_group == 0) {
4624 isl_factorizer_free(f);
4625 return fn(bset);
4628 nparam = isl_basic_set_dim(bset, isl_dim_param);
4629 nvar = isl_basic_set_dim(bset, isl_dim_set);
4631 space = isl_basic_set_get_space(bset);
4632 space = isl_space_params(space);
4633 set = isl_set_universe(isl_space_copy(space));
4634 qp = isl_qpolynomial_one_on_domain(space);
4635 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4637 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4639 for (i = 0, n = 0; i < f->n_group; ++i) {
4640 isl_basic_set *bset_i;
4641 isl_pw_qpolynomial *pwqp_i;
4643 bset_i = isl_basic_set_copy(bset);
4644 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4645 nparam + n + f->len[i], nvar - n - f->len[i]);
4646 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4647 nparam, n);
4648 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4649 n + f->len[i], nvar - n - f->len[i]);
4650 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4652 pwqp_i = fn(bset_i);
4653 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4655 n += f->len[i];
4658 isl_basic_set_free(bset);
4659 isl_factorizer_free(f);
4661 return pwqp;
4662 error:
4663 isl_basic_set_free(bset);
4664 return NULL;
4667 /* Factor bset, call fn on each of the factors and return the product.
4668 * The function is assumed to evaluate to zero on empty domains,
4669 * to one on zero-dimensional domains and to infinity on unbounded domains
4670 * and will not be called explicitly on zero-dimensional or unbounded domains.
4672 * We first check for some special cases and remove all equalities.
4673 * Then we hand over control to compressed_multiplicative_call.
4675 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4676 __isl_take isl_basic_set *bset,
4677 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4679 isl_bool bounded;
4680 isl_morph *morph;
4681 isl_pw_qpolynomial *pwqp;
4683 if (!bset)
4684 return NULL;
4686 if (isl_basic_set_plain_is_empty(bset))
4687 return constant_on_domain(bset, 0);
4689 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4690 return constant_on_domain(bset, 1);
4692 bounded = isl_basic_set_is_bounded(bset);
4693 if (bounded < 0)
4694 goto error;
4695 if (!bounded)
4696 return constant_on_domain(bset, -1);
4698 if (bset->n_eq == 0)
4699 return compressed_multiplicative_call(bset, fn);
4701 morph = isl_basic_set_full_compression(bset);
4702 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4704 pwqp = compressed_multiplicative_call(bset, fn);
4706 morph = isl_morph_dom_params(morph);
4707 morph = isl_morph_ran_params(morph);
4708 morph = isl_morph_inverse(morph);
4710 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4712 return pwqp;
4713 error:
4714 isl_basic_set_free(bset);
4715 return NULL;
4718 /* Drop all floors in "qp", turning each integer division [a/m] into
4719 * a rational division a/m. If "down" is set, then the integer division
4720 * is replaced by (a-(m-1))/m instead.
4722 static __isl_give isl_qpolynomial *qp_drop_floors(
4723 __isl_take isl_qpolynomial *qp, int down)
4725 int i;
4726 struct isl_upoly *s;
4728 if (!qp)
4729 return NULL;
4730 if (qp->div->n_row == 0)
4731 return qp;
4733 qp = isl_qpolynomial_cow(qp);
4734 if (!qp)
4735 return NULL;
4737 for (i = qp->div->n_row - 1; i >= 0; --i) {
4738 if (down) {
4739 isl_int_sub(qp->div->row[i][1],
4740 qp->div->row[i][1], qp->div->row[i][0]);
4741 isl_int_add_ui(qp->div->row[i][1],
4742 qp->div->row[i][1], 1);
4744 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4745 qp->div->row[i][0], qp->div->n_col - 1);
4746 qp = substitute_div(qp, i, s);
4747 if (!qp)
4748 return NULL;
4751 return qp;
4754 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4755 * a rational division a/m.
4757 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4758 __isl_take isl_pw_qpolynomial *pwqp)
4760 int i;
4762 if (!pwqp)
4763 return NULL;
4765 if (isl_pw_qpolynomial_is_zero(pwqp))
4766 return pwqp;
4768 pwqp = isl_pw_qpolynomial_cow(pwqp);
4769 if (!pwqp)
4770 return NULL;
4772 for (i = 0; i < pwqp->n; ++i) {
4773 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4774 if (!pwqp->p[i].qp)
4775 goto error;
4778 return pwqp;
4779 error:
4780 isl_pw_qpolynomial_free(pwqp);
4781 return NULL;
4784 /* Adjust all the integer divisions in "qp" such that they are at least
4785 * one over the given orthant (identified by "signs"). This ensures
4786 * that they will still be non-negative even after subtracting (m-1)/m.
4788 * In particular, f is replaced by f' + v, changing f = [a/m]
4789 * to f' = [(a - m v)/m].
4790 * If the constant term k in a is smaller than m,
4791 * the constant term of v is set to floor(k/m) - 1.
4792 * For any other term, if the coefficient c and the variable x have
4793 * the same sign, then no changes are needed.
4794 * Otherwise, if the variable is positive (and c is negative),
4795 * then the coefficient of x in v is set to floor(c/m).
4796 * If the variable is negative (and c is positive),
4797 * then the coefficient of x in v is set to ceil(c/m).
4799 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4800 int *signs)
4802 int i, j;
4803 int total;
4804 isl_vec *v = NULL;
4805 struct isl_upoly *s;
4807 qp = isl_qpolynomial_cow(qp);
4808 if (!qp)
4809 return NULL;
4810 qp->div = isl_mat_cow(qp->div);
4811 if (!qp->div)
4812 goto error;
4814 total = isl_space_dim(qp->dim, isl_dim_all);
4815 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4817 for (i = 0; i < qp->div->n_row; ++i) {
4818 isl_int *row = qp->div->row[i];
4819 v = isl_vec_clr(v);
4820 if (!v)
4821 goto error;
4822 if (isl_int_lt(row[1], row[0])) {
4823 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4824 isl_int_sub_ui(v->el[0], v->el[0], 1);
4825 isl_int_submul(row[1], row[0], v->el[0]);
4827 for (j = 0; j < total; ++j) {
4828 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4829 continue;
4830 if (signs[j] < 0)
4831 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4832 else
4833 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4834 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4836 for (j = 0; j < i; ++j) {
4837 if (isl_int_sgn(row[2 + total + j]) >= 0)
4838 continue;
4839 isl_int_fdiv_q(v->el[1 + total + j],
4840 row[2 + total + j], row[0]);
4841 isl_int_submul(row[2 + total + j],
4842 row[0], v->el[1 + total + j]);
4844 for (j = i + 1; j < qp->div->n_row; ++j) {
4845 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4846 continue;
4847 isl_seq_combine(qp->div->row[j] + 1,
4848 qp->div->ctx->one, qp->div->row[j] + 1,
4849 qp->div->row[j][2 + total + i], v->el, v->size);
4851 isl_int_set_si(v->el[1 + total + i], 1);
4852 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4853 qp->div->ctx->one, v->size);
4854 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4855 isl_upoly_free(s);
4856 if (!qp->upoly)
4857 goto error;
4860 isl_vec_free(v);
4861 return qp;
4862 error:
4863 isl_vec_free(v);
4864 isl_qpolynomial_free(qp);
4865 return NULL;
4868 struct isl_to_poly_data {
4869 int sign;
4870 isl_pw_qpolynomial *res;
4871 isl_qpolynomial *qp;
4874 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4875 * We first make all integer divisions positive and then split the
4876 * quasipolynomials into terms with sign data->sign (the direction
4877 * of the requested approximation) and terms with the opposite sign.
4878 * In the first set of terms, each integer division [a/m] is
4879 * overapproximated by a/m, while in the second it is underapproximated
4880 * by (a-(m-1))/m.
4882 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
4883 int *signs, void *user)
4885 struct isl_to_poly_data *data = user;
4886 isl_pw_qpolynomial *t;
4887 isl_qpolynomial *qp, *up, *down;
4889 qp = isl_qpolynomial_copy(data->qp);
4890 qp = make_divs_pos(qp, signs);
4892 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4893 up = qp_drop_floors(up, 0);
4894 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4895 down = qp_drop_floors(down, 1);
4897 isl_qpolynomial_free(qp);
4898 qp = isl_qpolynomial_add(up, down);
4900 t = isl_pw_qpolynomial_alloc(orthant, qp);
4901 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4903 return isl_stat_ok;
4906 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4907 * the polynomial will be an overapproximation. If "sign" is negative,
4908 * it will be an underapproximation. If "sign" is zero, the approximation
4909 * will lie somewhere in between.
4911 * In particular, is sign == 0, we simply drop the floors, turning
4912 * the integer divisions into rational divisions.
4913 * Otherwise, we split the domains into orthants, make all integer divisions
4914 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4915 * depending on the requested sign and the sign of the term in which
4916 * the integer division appears.
4918 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4919 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4921 int i;
4922 struct isl_to_poly_data data;
4924 if (sign == 0)
4925 return pwqp_drop_floors(pwqp);
4927 if (!pwqp)
4928 return NULL;
4930 data.sign = sign;
4931 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4933 for (i = 0; i < pwqp->n; ++i) {
4934 if (pwqp->p[i].qp->div->n_row == 0) {
4935 isl_pw_qpolynomial *t;
4936 t = isl_pw_qpolynomial_alloc(
4937 isl_set_copy(pwqp->p[i].set),
4938 isl_qpolynomial_copy(pwqp->p[i].qp));
4939 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4940 continue;
4942 data.qp = pwqp->p[i].qp;
4943 if (isl_set_foreach_orthant(pwqp->p[i].set,
4944 &to_polynomial_on_orthant, &data) < 0)
4945 goto error;
4948 isl_pw_qpolynomial_free(pwqp);
4950 return data.res;
4951 error:
4952 isl_pw_qpolynomial_free(pwqp);
4953 isl_pw_qpolynomial_free(data.res);
4954 return NULL;
4957 static __isl_give isl_pw_qpolynomial *poly_entry(
4958 __isl_take isl_pw_qpolynomial *pwqp, void *user)
4960 int *sign = user;
4962 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
4965 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4966 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4968 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
4969 &poly_entry, &sign);
4972 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4973 __isl_take isl_qpolynomial *qp)
4975 int i, k;
4976 isl_space *dim;
4977 isl_vec *aff = NULL;
4978 isl_basic_map *bmap = NULL;
4979 unsigned pos;
4980 unsigned n_div;
4982 if (!qp)
4983 return NULL;
4984 if (!isl_upoly_is_affine(qp->upoly))
4985 isl_die(qp->dim->ctx, isl_error_invalid,
4986 "input quasi-polynomial not affine", goto error);
4987 aff = isl_qpolynomial_extract_affine(qp);
4988 if (!aff)
4989 goto error;
4990 dim = isl_qpolynomial_get_space(qp);
4991 pos = 1 + isl_space_offset(dim, isl_dim_out);
4992 n_div = qp->div->n_row;
4993 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4995 for (i = 0; i < n_div; ++i) {
4996 k = isl_basic_map_alloc_div(bmap);
4997 if (k < 0)
4998 goto error;
4999 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
5000 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
5001 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
5002 goto error;
5004 k = isl_basic_map_alloc_equality(bmap);
5005 if (k < 0)
5006 goto error;
5007 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
5008 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
5009 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
5011 isl_vec_free(aff);
5012 isl_qpolynomial_free(qp);
5013 bmap = isl_basic_map_finalize(bmap);
5014 return bmap;
5015 error:
5016 isl_vec_free(aff);
5017 isl_qpolynomial_free(qp);
5018 isl_basic_map_free(bmap);
5019 return NULL;