doc: explicitly state that the divisor of an ?div_? operation is positive
[isl.git] / isl_polynomial.c
blob521611a99fe4fd8237c268cafeb582721ce2dcd9
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <stdlib.h>
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
16 #include <isl_seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
31 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
33 switch (type) {
34 case isl_dim_param: return 0;
35 case isl_dim_in: return dim->nparam;
36 case isl_dim_out: return dim->nparam + dim->n_in;
37 default: return 0;
41 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
43 if (!up)
44 return -1;
46 return up->var < 0;
49 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
51 if (!up)
52 return NULL;
54 isl_assert(up->ctx, up->var < 0, return NULL);
56 return (struct isl_upoly_cst *)up;
59 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
61 if (!up)
62 return NULL;
64 isl_assert(up->ctx, up->var >= 0, return NULL);
66 return (struct isl_upoly_rec *)up;
69 /* Compare two polynomials.
71 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
72 * than "up2" and 0 if they are equal.
74 static int isl_upoly_plain_cmp(__isl_keep struct isl_upoly *up1,
75 __isl_keep struct isl_upoly *up2)
77 int i;
78 struct isl_upoly_rec *rec1, *rec2;
80 if (up1 == up2)
81 return 0;
82 if (!up1)
83 return -1;
84 if (!up2)
85 return 1;
86 if (up1->var != up2->var)
87 return up1->var - up2->var;
89 if (isl_upoly_is_cst(up1)) {
90 struct isl_upoly_cst *cst1, *cst2;
91 int cmp;
93 cst1 = isl_upoly_as_cst(up1);
94 cst2 = isl_upoly_as_cst(up2);
95 if (!cst1 || !cst2)
96 return 0;
97 cmp = isl_int_cmp(cst1->n, cst2->n);
98 if (cmp != 0)
99 return cmp;
100 return isl_int_cmp(cst1->d, cst2->d);
103 rec1 = isl_upoly_as_rec(up1);
104 rec2 = isl_upoly_as_rec(up2);
105 if (!rec1 || !rec2)
106 return 0;
108 if (rec1->n != rec2->n)
109 return rec1->n - rec2->n;
111 for (i = 0; i < rec1->n; ++i) {
112 int cmp = isl_upoly_plain_cmp(rec1->p[i], rec2->p[i]);
113 if (cmp != 0)
114 return cmp;
117 return 0;
120 isl_bool isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
121 __isl_keep struct isl_upoly *up2)
123 int i;
124 struct isl_upoly_rec *rec1, *rec2;
126 if (!up1 || !up2)
127 return isl_bool_error;
128 if (up1 == up2)
129 return isl_bool_true;
130 if (up1->var != up2->var)
131 return isl_bool_false;
132 if (isl_upoly_is_cst(up1)) {
133 struct isl_upoly_cst *cst1, *cst2;
134 cst1 = isl_upoly_as_cst(up1);
135 cst2 = isl_upoly_as_cst(up2);
136 if (!cst1 || !cst2)
137 return isl_bool_error;
138 return isl_int_eq(cst1->n, cst2->n) &&
139 isl_int_eq(cst1->d, cst2->d);
142 rec1 = isl_upoly_as_rec(up1);
143 rec2 = isl_upoly_as_rec(up2);
144 if (!rec1 || !rec2)
145 return isl_bool_error;
147 if (rec1->n != rec2->n)
148 return isl_bool_false;
150 for (i = 0; i < rec1->n; ++i) {
151 isl_bool eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
152 if (eq < 0 || !eq)
153 return eq;
156 return isl_bool_true;
159 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
161 struct isl_upoly_cst *cst;
163 if (!up)
164 return -1;
165 if (!isl_upoly_is_cst(up))
166 return 0;
168 cst = isl_upoly_as_cst(up);
169 if (!cst)
170 return -1;
172 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
175 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
177 struct isl_upoly_cst *cst;
179 if (!up)
180 return 0;
181 if (!isl_upoly_is_cst(up))
182 return 0;
184 cst = isl_upoly_as_cst(up);
185 if (!cst)
186 return 0;
188 return isl_int_sgn(cst->n);
191 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
193 struct isl_upoly_cst *cst;
195 if (!up)
196 return -1;
197 if (!isl_upoly_is_cst(up))
198 return 0;
200 cst = isl_upoly_as_cst(up);
201 if (!cst)
202 return -1;
204 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
207 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
209 struct isl_upoly_cst *cst;
211 if (!up)
212 return -1;
213 if (!isl_upoly_is_cst(up))
214 return 0;
216 cst = isl_upoly_as_cst(up);
217 if (!cst)
218 return -1;
220 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
223 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
225 struct isl_upoly_cst *cst;
227 if (!up)
228 return -1;
229 if (!isl_upoly_is_cst(up))
230 return 0;
232 cst = isl_upoly_as_cst(up);
233 if (!cst)
234 return -1;
236 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
239 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
241 struct isl_upoly_cst *cst;
243 if (!up)
244 return -1;
245 if (!isl_upoly_is_cst(up))
246 return 0;
248 cst = isl_upoly_as_cst(up);
249 if (!cst)
250 return -1;
252 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
255 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
257 struct isl_upoly_cst *cst;
259 if (!up)
260 return -1;
261 if (!isl_upoly_is_cst(up))
262 return 0;
264 cst = isl_upoly_as_cst(up);
265 if (!cst)
266 return -1;
268 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
271 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
273 struct isl_upoly_cst *cst;
275 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
276 if (!cst)
277 return NULL;
279 cst->up.ref = 1;
280 cst->up.ctx = ctx;
281 isl_ctx_ref(ctx);
282 cst->up.var = -1;
284 isl_int_init(cst->n);
285 isl_int_init(cst->d);
287 return cst;
290 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
292 struct isl_upoly_cst *cst;
294 cst = isl_upoly_cst_alloc(ctx);
295 if (!cst)
296 return NULL;
298 isl_int_set_si(cst->n, 0);
299 isl_int_set_si(cst->d, 1);
301 return &cst->up;
304 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
306 struct isl_upoly_cst *cst;
308 cst = isl_upoly_cst_alloc(ctx);
309 if (!cst)
310 return NULL;
312 isl_int_set_si(cst->n, 1);
313 isl_int_set_si(cst->d, 1);
315 return &cst->up;
318 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
320 struct isl_upoly_cst *cst;
322 cst = isl_upoly_cst_alloc(ctx);
323 if (!cst)
324 return NULL;
326 isl_int_set_si(cst->n, 1);
327 isl_int_set_si(cst->d, 0);
329 return &cst->up;
332 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
334 struct isl_upoly_cst *cst;
336 cst = isl_upoly_cst_alloc(ctx);
337 if (!cst)
338 return NULL;
340 isl_int_set_si(cst->n, -1);
341 isl_int_set_si(cst->d, 0);
343 return &cst->up;
346 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
348 struct isl_upoly_cst *cst;
350 cst = isl_upoly_cst_alloc(ctx);
351 if (!cst)
352 return NULL;
354 isl_int_set_si(cst->n, 0);
355 isl_int_set_si(cst->d, 0);
357 return &cst->up;
360 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
361 isl_int n, isl_int d)
363 struct isl_upoly_cst *cst;
365 cst = isl_upoly_cst_alloc(ctx);
366 if (!cst)
367 return NULL;
369 isl_int_set(cst->n, n);
370 isl_int_set(cst->d, d);
372 return &cst->up;
375 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
376 int var, int size)
378 struct isl_upoly_rec *rec;
380 isl_assert(ctx, var >= 0, return NULL);
381 isl_assert(ctx, size >= 0, return NULL);
382 rec = isl_calloc(ctx, struct isl_upoly_rec,
383 sizeof(struct isl_upoly_rec) +
384 size * sizeof(struct isl_upoly *));
385 if (!rec)
386 return NULL;
388 rec->up.ref = 1;
389 rec->up.ctx = ctx;
390 isl_ctx_ref(ctx);
391 rec->up.var = var;
393 rec->n = 0;
394 rec->size = size;
396 return rec;
399 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
400 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
402 qp = isl_qpolynomial_cow(qp);
403 if (!qp || !dim)
404 goto error;
406 isl_space_free(qp->dim);
407 qp->dim = dim;
409 return qp;
410 error:
411 isl_qpolynomial_free(qp);
412 isl_space_free(dim);
413 return NULL;
416 /* Reset the space of "qp". This function is called from isl_pw_templ.c
417 * and doesn't know if the space of an element object is represented
418 * directly or through its domain. It therefore passes along both.
420 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
421 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
422 __isl_take isl_space *domain)
424 isl_space_free(space);
425 return isl_qpolynomial_reset_domain_space(qp, domain);
428 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
430 return qp ? qp->dim->ctx : NULL;
433 __isl_give isl_space *isl_qpolynomial_get_domain_space(
434 __isl_keep isl_qpolynomial *qp)
436 return qp ? isl_space_copy(qp->dim) : NULL;
439 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
441 isl_space *space;
442 if (!qp)
443 return NULL;
444 space = isl_space_copy(qp->dim);
445 space = isl_space_from_domain(space);
446 space = isl_space_add_dims(space, isl_dim_out, 1);
447 return space;
450 /* Return the number of variables of the given type in the domain of "qp".
452 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
453 enum isl_dim_type type)
455 if (!qp)
456 return 0;
457 if (type == isl_dim_div)
458 return qp->div->n_row;
459 if (type == isl_dim_all)
460 return isl_space_dim(qp->dim, isl_dim_all) +
461 isl_qpolynomial_domain_dim(qp, isl_dim_div);
462 return isl_space_dim(qp->dim, type);
465 /* Externally, an isl_qpolynomial has a map space, but internally, the
466 * ls field corresponds to the domain of that space.
468 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
469 enum isl_dim_type type)
471 if (!qp)
472 return 0;
473 if (type == isl_dim_out)
474 return 1;
475 if (type == isl_dim_in)
476 type = isl_dim_set;
477 return isl_qpolynomial_domain_dim(qp, type);
480 /* Return the offset of the first coefficient of type "type" in
481 * the domain of "qp".
483 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
484 enum isl_dim_type type)
486 if (!qp)
487 return 0;
488 switch (type) {
489 case isl_dim_cst:
490 return 0;
491 case isl_dim_param:
492 case isl_dim_set:
493 return 1 + isl_space_offset(qp->dim, type);
494 case isl_dim_div:
495 return 1 + isl_space_dim(qp->dim, isl_dim_all);
496 default:
497 return 0;
501 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
503 return qp ? isl_upoly_is_zero(qp->upoly) : isl_bool_error;
506 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
508 return qp ? isl_upoly_is_one(qp->upoly) : isl_bool_error;
511 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
513 return qp ? isl_upoly_is_nan(qp->upoly) : isl_bool_error;
516 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
518 return qp ? isl_upoly_is_infty(qp->upoly) : isl_bool_error;
521 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
523 return qp ? isl_upoly_is_neginfty(qp->upoly) : isl_bool_error;
526 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
528 return qp ? isl_upoly_sgn(qp->upoly) : 0;
531 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
533 isl_int_clear(cst->n);
534 isl_int_clear(cst->d);
537 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
539 int i;
541 for (i = 0; i < rec->n; ++i)
542 isl_upoly_free(rec->p[i]);
545 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
547 if (!up)
548 return NULL;
550 up->ref++;
551 return up;
554 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
556 struct isl_upoly_cst *cst;
557 struct isl_upoly_cst *dup;
559 cst = isl_upoly_as_cst(up);
560 if (!cst)
561 return NULL;
563 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
564 if (!dup)
565 return NULL;
566 isl_int_set(dup->n, cst->n);
567 isl_int_set(dup->d, cst->d);
569 return &dup->up;
572 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
574 int i;
575 struct isl_upoly_rec *rec;
576 struct isl_upoly_rec *dup;
578 rec = isl_upoly_as_rec(up);
579 if (!rec)
580 return NULL;
582 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
583 if (!dup)
584 return NULL;
586 for (i = 0; i < rec->n; ++i) {
587 dup->p[i] = isl_upoly_copy(rec->p[i]);
588 if (!dup->p[i])
589 goto error;
590 dup->n++;
593 return &dup->up;
594 error:
595 isl_upoly_free(&dup->up);
596 return NULL;
599 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
601 if (!up)
602 return NULL;
604 if (isl_upoly_is_cst(up))
605 return isl_upoly_dup_cst(up);
606 else
607 return isl_upoly_dup_rec(up);
610 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
612 if (!up)
613 return NULL;
615 if (up->ref == 1)
616 return up;
617 up->ref--;
618 return isl_upoly_dup(up);
621 __isl_null struct isl_upoly *isl_upoly_free(__isl_take struct isl_upoly *up)
623 if (!up)
624 return NULL;
626 if (--up->ref > 0)
627 return NULL;
629 if (up->var < 0)
630 upoly_free_cst((struct isl_upoly_cst *)up);
631 else
632 upoly_free_rec((struct isl_upoly_rec *)up);
634 isl_ctx_deref(up->ctx);
635 free(up);
636 return NULL;
639 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
641 isl_int gcd;
643 isl_int_init(gcd);
644 isl_int_gcd(gcd, cst->n, cst->d);
645 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
646 isl_int_divexact(cst->n, cst->n, gcd);
647 isl_int_divexact(cst->d, cst->d, gcd);
649 isl_int_clear(gcd);
652 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
653 __isl_take struct isl_upoly *up2)
655 struct isl_upoly_cst *cst1;
656 struct isl_upoly_cst *cst2;
658 up1 = isl_upoly_cow(up1);
659 if (!up1 || !up2)
660 goto error;
662 cst1 = isl_upoly_as_cst(up1);
663 cst2 = isl_upoly_as_cst(up2);
665 if (isl_int_eq(cst1->d, cst2->d))
666 isl_int_add(cst1->n, cst1->n, cst2->n);
667 else {
668 isl_int_mul(cst1->n, cst1->n, cst2->d);
669 isl_int_addmul(cst1->n, cst2->n, cst1->d);
670 isl_int_mul(cst1->d, cst1->d, cst2->d);
673 isl_upoly_cst_reduce(cst1);
675 isl_upoly_free(up2);
676 return up1;
677 error:
678 isl_upoly_free(up1);
679 isl_upoly_free(up2);
680 return NULL;
683 static __isl_give struct isl_upoly *replace_by_zero(
684 __isl_take struct isl_upoly *up)
686 struct isl_ctx *ctx;
688 if (!up)
689 return NULL;
690 ctx = up->ctx;
691 isl_upoly_free(up);
692 return isl_upoly_zero(ctx);
695 static __isl_give struct isl_upoly *replace_by_constant_term(
696 __isl_take struct isl_upoly *up)
698 struct isl_upoly_rec *rec;
699 struct isl_upoly *cst;
701 if (!up)
702 return NULL;
704 rec = isl_upoly_as_rec(up);
705 if (!rec)
706 goto error;
707 cst = isl_upoly_copy(rec->p[0]);
708 isl_upoly_free(up);
709 return cst;
710 error:
711 isl_upoly_free(up);
712 return NULL;
715 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
716 __isl_take struct isl_upoly *up2)
718 int i;
719 struct isl_upoly_rec *rec1, *rec2;
721 if (!up1 || !up2)
722 goto error;
724 if (isl_upoly_is_nan(up1)) {
725 isl_upoly_free(up2);
726 return up1;
729 if (isl_upoly_is_nan(up2)) {
730 isl_upoly_free(up1);
731 return up2;
734 if (isl_upoly_is_zero(up1)) {
735 isl_upoly_free(up1);
736 return up2;
739 if (isl_upoly_is_zero(up2)) {
740 isl_upoly_free(up2);
741 return up1;
744 if (up1->var < up2->var)
745 return isl_upoly_sum(up2, up1);
747 if (up2->var < up1->var) {
748 struct isl_upoly_rec *rec;
749 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
750 isl_upoly_free(up1);
751 return up2;
753 up1 = isl_upoly_cow(up1);
754 rec = isl_upoly_as_rec(up1);
755 if (!rec)
756 goto error;
757 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
758 if (rec->n == 1)
759 up1 = replace_by_constant_term(up1);
760 return up1;
763 if (isl_upoly_is_cst(up1))
764 return isl_upoly_sum_cst(up1, up2);
766 rec1 = isl_upoly_as_rec(up1);
767 rec2 = isl_upoly_as_rec(up2);
768 if (!rec1 || !rec2)
769 goto error;
771 if (rec1->n < rec2->n)
772 return isl_upoly_sum(up2, up1);
774 up1 = isl_upoly_cow(up1);
775 rec1 = isl_upoly_as_rec(up1);
776 if (!rec1)
777 goto error;
779 for (i = rec2->n - 1; i >= 0; --i) {
780 rec1->p[i] = isl_upoly_sum(rec1->p[i],
781 isl_upoly_copy(rec2->p[i]));
782 if (!rec1->p[i])
783 goto error;
784 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
785 isl_upoly_free(rec1->p[i]);
786 rec1->n--;
790 if (rec1->n == 0)
791 up1 = replace_by_zero(up1);
792 else if (rec1->n == 1)
793 up1 = replace_by_constant_term(up1);
795 isl_upoly_free(up2);
797 return up1;
798 error:
799 isl_upoly_free(up1);
800 isl_upoly_free(up2);
801 return NULL;
804 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
805 __isl_take struct isl_upoly *up, isl_int v)
807 struct isl_upoly_cst *cst;
809 up = isl_upoly_cow(up);
810 if (!up)
811 return NULL;
813 cst = isl_upoly_as_cst(up);
815 isl_int_addmul(cst->n, cst->d, v);
817 return up;
820 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
821 __isl_take struct isl_upoly *up, isl_int v)
823 struct isl_upoly_rec *rec;
825 if (!up)
826 return NULL;
828 if (isl_upoly_is_cst(up))
829 return isl_upoly_cst_add_isl_int(up, v);
831 up = isl_upoly_cow(up);
832 rec = isl_upoly_as_rec(up);
833 if (!rec)
834 goto error;
836 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
837 if (!rec->p[0])
838 goto error;
840 return up;
841 error:
842 isl_upoly_free(up);
843 return NULL;
846 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
847 __isl_take struct isl_upoly *up, isl_int v)
849 struct isl_upoly_cst *cst;
851 if (isl_upoly_is_zero(up))
852 return up;
854 up = isl_upoly_cow(up);
855 if (!up)
856 return NULL;
858 cst = isl_upoly_as_cst(up);
860 isl_int_mul(cst->n, cst->n, v);
862 return up;
865 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
866 __isl_take struct isl_upoly *up, isl_int v)
868 int i;
869 struct isl_upoly_rec *rec;
871 if (!up)
872 return NULL;
874 if (isl_upoly_is_cst(up))
875 return isl_upoly_cst_mul_isl_int(up, v);
877 up = isl_upoly_cow(up);
878 rec = isl_upoly_as_rec(up);
879 if (!rec)
880 goto error;
882 for (i = 0; i < rec->n; ++i) {
883 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
884 if (!rec->p[i])
885 goto error;
888 return up;
889 error:
890 isl_upoly_free(up);
891 return NULL;
894 /* Multiply the constant polynomial "up" by "v".
896 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
897 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
899 struct isl_upoly_cst *cst;
901 if (isl_upoly_is_zero(up))
902 return up;
904 up = isl_upoly_cow(up);
905 if (!up)
906 return NULL;
908 cst = isl_upoly_as_cst(up);
910 isl_int_mul(cst->n, cst->n, v->n);
911 isl_int_mul(cst->d, cst->d, v->d);
912 isl_upoly_cst_reduce(cst);
914 return up;
917 /* Multiply the polynomial "up" by "v".
919 static __isl_give struct isl_upoly *isl_upoly_scale_val(
920 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
922 int i;
923 struct isl_upoly_rec *rec;
925 if (!up)
926 return NULL;
928 if (isl_upoly_is_cst(up))
929 return isl_upoly_cst_scale_val(up, v);
931 up = isl_upoly_cow(up);
932 rec = isl_upoly_as_rec(up);
933 if (!rec)
934 goto error;
936 for (i = 0; i < rec->n; ++i) {
937 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
938 if (!rec->p[i])
939 goto error;
942 return up;
943 error:
944 isl_upoly_free(up);
945 return NULL;
948 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
949 __isl_take struct isl_upoly *up2)
951 struct isl_upoly_cst *cst1;
952 struct isl_upoly_cst *cst2;
954 up1 = isl_upoly_cow(up1);
955 if (!up1 || !up2)
956 goto error;
958 cst1 = isl_upoly_as_cst(up1);
959 cst2 = isl_upoly_as_cst(up2);
961 isl_int_mul(cst1->n, cst1->n, cst2->n);
962 isl_int_mul(cst1->d, cst1->d, cst2->d);
964 isl_upoly_cst_reduce(cst1);
966 isl_upoly_free(up2);
967 return up1;
968 error:
969 isl_upoly_free(up1);
970 isl_upoly_free(up2);
971 return NULL;
974 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
975 __isl_take struct isl_upoly *up2)
977 struct isl_upoly_rec *rec1;
978 struct isl_upoly_rec *rec2;
979 struct isl_upoly_rec *res = NULL;
980 int i, j;
981 int size;
983 rec1 = isl_upoly_as_rec(up1);
984 rec2 = isl_upoly_as_rec(up2);
985 if (!rec1 || !rec2)
986 goto error;
987 size = rec1->n + rec2->n - 1;
988 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
989 if (!res)
990 goto error;
992 for (i = 0; i < rec1->n; ++i) {
993 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
994 isl_upoly_copy(rec1->p[i]));
995 if (!res->p[i])
996 goto error;
997 res->n++;
999 for (; i < size; ++i) {
1000 res->p[i] = isl_upoly_zero(up1->ctx);
1001 if (!res->p[i])
1002 goto error;
1003 res->n++;
1005 for (i = 0; i < rec1->n; ++i) {
1006 for (j = 1; j < rec2->n; ++j) {
1007 struct isl_upoly *up;
1008 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
1009 isl_upoly_copy(rec1->p[i]));
1010 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
1011 if (!res->p[i + j])
1012 goto error;
1016 isl_upoly_free(up1);
1017 isl_upoly_free(up2);
1019 return &res->up;
1020 error:
1021 isl_upoly_free(up1);
1022 isl_upoly_free(up2);
1023 isl_upoly_free(&res->up);
1024 return NULL;
1027 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
1028 __isl_take struct isl_upoly *up2)
1030 if (!up1 || !up2)
1031 goto error;
1033 if (isl_upoly_is_nan(up1)) {
1034 isl_upoly_free(up2);
1035 return up1;
1038 if (isl_upoly_is_nan(up2)) {
1039 isl_upoly_free(up1);
1040 return up2;
1043 if (isl_upoly_is_zero(up1)) {
1044 isl_upoly_free(up2);
1045 return up1;
1048 if (isl_upoly_is_zero(up2)) {
1049 isl_upoly_free(up1);
1050 return up2;
1053 if (isl_upoly_is_one(up1)) {
1054 isl_upoly_free(up1);
1055 return up2;
1058 if (isl_upoly_is_one(up2)) {
1059 isl_upoly_free(up2);
1060 return up1;
1063 if (up1->var < up2->var)
1064 return isl_upoly_mul(up2, up1);
1066 if (up2->var < up1->var) {
1067 int i;
1068 struct isl_upoly_rec *rec;
1069 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
1070 isl_ctx *ctx = up1->ctx;
1071 isl_upoly_free(up1);
1072 isl_upoly_free(up2);
1073 return isl_upoly_nan(ctx);
1075 up1 = isl_upoly_cow(up1);
1076 rec = isl_upoly_as_rec(up1);
1077 if (!rec)
1078 goto error;
1080 for (i = 0; i < rec->n; ++i) {
1081 rec->p[i] = isl_upoly_mul(rec->p[i],
1082 isl_upoly_copy(up2));
1083 if (!rec->p[i])
1084 goto error;
1086 isl_upoly_free(up2);
1087 return up1;
1090 if (isl_upoly_is_cst(up1))
1091 return isl_upoly_mul_cst(up1, up2);
1093 return isl_upoly_mul_rec(up1, up2);
1094 error:
1095 isl_upoly_free(up1);
1096 isl_upoly_free(up2);
1097 return NULL;
1100 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1101 unsigned power)
1103 struct isl_upoly *res;
1105 if (!up)
1106 return NULL;
1107 if (power == 1)
1108 return up;
1110 if (power % 2)
1111 res = isl_upoly_copy(up);
1112 else
1113 res = isl_upoly_one(up->ctx);
1115 while (power >>= 1) {
1116 up = isl_upoly_mul(up, isl_upoly_copy(up));
1117 if (power % 2)
1118 res = isl_upoly_mul(res, isl_upoly_copy(up));
1121 isl_upoly_free(up);
1122 return res;
1125 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
1126 unsigned n_div, __isl_take struct isl_upoly *up)
1128 struct isl_qpolynomial *qp = NULL;
1129 unsigned total;
1131 if (!dim || !up)
1132 goto error;
1134 if (!isl_space_is_set(dim))
1135 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1136 "domain of polynomial should be a set", goto error);
1138 total = isl_space_dim(dim, isl_dim_all);
1140 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
1141 if (!qp)
1142 goto error;
1144 qp->ref = 1;
1145 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
1146 if (!qp->div)
1147 goto error;
1149 qp->dim = dim;
1150 qp->upoly = up;
1152 return qp;
1153 error:
1154 isl_space_free(dim);
1155 isl_upoly_free(up);
1156 isl_qpolynomial_free(qp);
1157 return NULL;
1160 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1162 if (!qp)
1163 return NULL;
1165 qp->ref++;
1166 return qp;
1169 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1171 struct isl_qpolynomial *dup;
1173 if (!qp)
1174 return NULL;
1176 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1177 isl_upoly_copy(qp->upoly));
1178 if (!dup)
1179 return NULL;
1180 isl_mat_free(dup->div);
1181 dup->div = isl_mat_copy(qp->div);
1182 if (!dup->div)
1183 goto error;
1185 return dup;
1186 error:
1187 isl_qpolynomial_free(dup);
1188 return NULL;
1191 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1193 if (!qp)
1194 return NULL;
1196 if (qp->ref == 1)
1197 return qp;
1198 qp->ref--;
1199 return isl_qpolynomial_dup(qp);
1202 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1203 __isl_take isl_qpolynomial *qp)
1205 if (!qp)
1206 return NULL;
1208 if (--qp->ref > 0)
1209 return NULL;
1211 isl_space_free(qp->dim);
1212 isl_mat_free(qp->div);
1213 isl_upoly_free(qp->upoly);
1215 free(qp);
1216 return NULL;
1219 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1221 int i;
1222 struct isl_upoly_rec *rec;
1223 struct isl_upoly_cst *cst;
1225 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1226 if (!rec)
1227 return NULL;
1228 for (i = 0; i < 1 + power; ++i) {
1229 rec->p[i] = isl_upoly_zero(ctx);
1230 if (!rec->p[i])
1231 goto error;
1232 rec->n++;
1234 cst = isl_upoly_as_cst(rec->p[power]);
1235 isl_int_set_si(cst->n, 1);
1237 return &rec->up;
1238 error:
1239 isl_upoly_free(&rec->up);
1240 return NULL;
1243 /* r array maps original positions to new positions.
1245 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1246 int *r)
1248 int i;
1249 struct isl_upoly_rec *rec;
1250 struct isl_upoly *base;
1251 struct isl_upoly *res;
1253 if (isl_upoly_is_cst(up))
1254 return up;
1256 rec = isl_upoly_as_rec(up);
1257 if (!rec)
1258 goto error;
1260 isl_assert(up->ctx, rec->n >= 1, goto error);
1262 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1263 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1265 for (i = rec->n - 2; i >= 0; --i) {
1266 res = isl_upoly_mul(res, isl_upoly_copy(base));
1267 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1270 isl_upoly_free(base);
1271 isl_upoly_free(up);
1273 return res;
1274 error:
1275 isl_upoly_free(up);
1276 return NULL;
1279 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1280 __isl_keep isl_mat *div2)
1282 int n_row, n_col;
1283 isl_bool equal;
1285 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1286 div1->n_col >= div2->n_col,
1287 return isl_bool_error);
1289 if (div1->n_row == div2->n_row)
1290 return isl_mat_is_equal(div1, div2);
1292 n_row = div1->n_row;
1293 n_col = div1->n_col;
1294 div1->n_row = div2->n_row;
1295 div1->n_col = div2->n_col;
1297 equal = isl_mat_is_equal(div1, div2);
1299 div1->n_row = n_row;
1300 div1->n_col = n_col;
1302 return equal;
1305 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1307 int li, lj;
1309 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1310 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1312 if (li != lj)
1313 return li - lj;
1315 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1318 struct isl_div_sort_info {
1319 isl_mat *div;
1320 int row;
1323 static int div_sort_cmp(const void *p1, const void *p2)
1325 const struct isl_div_sort_info *i1, *i2;
1326 i1 = (const struct isl_div_sort_info *) p1;
1327 i2 = (const struct isl_div_sort_info *) p2;
1329 return cmp_row(i1->div, i1->row, i2->row);
1332 /* Sort divs and remove duplicates.
1334 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1336 int i;
1337 int skip;
1338 int len;
1339 struct isl_div_sort_info *array = NULL;
1340 int *pos = NULL, *at = NULL;
1341 int *reordering = NULL;
1342 unsigned div_pos;
1344 if (!qp)
1345 return NULL;
1346 if (qp->div->n_row <= 1)
1347 return qp;
1349 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1351 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1352 qp->div->n_row);
1353 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1354 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1355 len = qp->div->n_col - 2;
1356 reordering = isl_alloc_array(qp->div->ctx, int, len);
1357 if (!array || !pos || !at || !reordering)
1358 goto error;
1360 for (i = 0; i < qp->div->n_row; ++i) {
1361 array[i].div = qp->div;
1362 array[i].row = i;
1363 pos[i] = i;
1364 at[i] = i;
1367 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1368 div_sort_cmp);
1370 for (i = 0; i < div_pos; ++i)
1371 reordering[i] = i;
1373 for (i = 0; i < qp->div->n_row; ++i) {
1374 if (pos[array[i].row] == i)
1375 continue;
1376 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1377 pos[at[i]] = pos[array[i].row];
1378 at[pos[array[i].row]] = at[i];
1379 at[i] = array[i].row;
1380 pos[array[i].row] = i;
1383 skip = 0;
1384 for (i = 0; i < len - div_pos; ++i) {
1385 if (i > 0 &&
1386 isl_seq_eq(qp->div->row[i - skip - 1],
1387 qp->div->row[i - skip], qp->div->n_col)) {
1388 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1389 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1390 2 + div_pos + i - skip);
1391 qp->div = isl_mat_drop_cols(qp->div,
1392 2 + div_pos + i - skip, 1);
1393 skip++;
1395 reordering[div_pos + array[i].row] = div_pos + i - skip;
1398 qp->upoly = reorder(qp->upoly, reordering);
1400 if (!qp->upoly || !qp->div)
1401 goto error;
1403 free(at);
1404 free(pos);
1405 free(array);
1406 free(reordering);
1408 return qp;
1409 error:
1410 free(at);
1411 free(pos);
1412 free(array);
1413 free(reordering);
1414 isl_qpolynomial_free(qp);
1415 return NULL;
1418 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1419 int *exp, int first)
1421 int i;
1422 struct isl_upoly_rec *rec;
1424 if (isl_upoly_is_cst(up))
1425 return up;
1427 if (up->var < first)
1428 return up;
1430 if (exp[up->var - first] == up->var - first)
1431 return up;
1433 up = isl_upoly_cow(up);
1434 if (!up)
1435 goto error;
1437 up->var = exp[up->var - first] + first;
1439 rec = isl_upoly_as_rec(up);
1440 if (!rec)
1441 goto error;
1443 for (i = 0; i < rec->n; ++i) {
1444 rec->p[i] = expand(rec->p[i], exp, first);
1445 if (!rec->p[i])
1446 goto error;
1449 return up;
1450 error:
1451 isl_upoly_free(up);
1452 return NULL;
1455 static __isl_give isl_qpolynomial *with_merged_divs(
1456 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1457 __isl_take isl_qpolynomial *qp2),
1458 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1460 int *exp1 = NULL;
1461 int *exp2 = NULL;
1462 isl_mat *div = NULL;
1463 int n_div1, n_div2;
1465 qp1 = isl_qpolynomial_cow(qp1);
1466 qp2 = isl_qpolynomial_cow(qp2);
1468 if (!qp1 || !qp2)
1469 goto error;
1471 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1472 qp1->div->n_col >= qp2->div->n_col, goto error);
1474 n_div1 = qp1->div->n_row;
1475 n_div2 = qp2->div->n_row;
1476 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1477 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1478 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1479 goto error;
1481 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1482 if (!div)
1483 goto error;
1485 isl_mat_free(qp1->div);
1486 qp1->div = isl_mat_copy(div);
1487 isl_mat_free(qp2->div);
1488 qp2->div = isl_mat_copy(div);
1490 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1491 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1493 if (!qp1->upoly || !qp2->upoly)
1494 goto error;
1496 isl_mat_free(div);
1497 free(exp1);
1498 free(exp2);
1500 return fn(qp1, qp2);
1501 error:
1502 isl_mat_free(div);
1503 free(exp1);
1504 free(exp2);
1505 isl_qpolynomial_free(qp1);
1506 isl_qpolynomial_free(qp2);
1507 return NULL;
1510 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1511 __isl_take isl_qpolynomial *qp2)
1513 isl_bool compatible;
1515 qp1 = isl_qpolynomial_cow(qp1);
1517 if (!qp1 || !qp2)
1518 goto error;
1520 if (qp1->div->n_row < qp2->div->n_row)
1521 return isl_qpolynomial_add(qp2, qp1);
1523 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1524 compatible = compatible_divs(qp1->div, qp2->div);
1525 if (compatible < 0)
1526 goto error;
1527 if (!compatible)
1528 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1530 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1531 if (!qp1->upoly)
1532 goto error;
1534 isl_qpolynomial_free(qp2);
1536 return qp1;
1537 error:
1538 isl_qpolynomial_free(qp1);
1539 isl_qpolynomial_free(qp2);
1540 return NULL;
1543 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1544 __isl_keep isl_set *dom,
1545 __isl_take isl_qpolynomial *qp1,
1546 __isl_take isl_qpolynomial *qp2)
1548 qp1 = isl_qpolynomial_add(qp1, qp2);
1549 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1550 return qp1;
1553 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1554 __isl_take isl_qpolynomial *qp2)
1556 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1559 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1560 __isl_take isl_qpolynomial *qp, isl_int v)
1562 if (isl_int_is_zero(v))
1563 return qp;
1565 qp = isl_qpolynomial_cow(qp);
1566 if (!qp)
1567 return NULL;
1569 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1570 if (!qp->upoly)
1571 goto error;
1573 return qp;
1574 error:
1575 isl_qpolynomial_free(qp);
1576 return NULL;
1580 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1582 if (!qp)
1583 return NULL;
1585 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1588 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1589 __isl_take isl_qpolynomial *qp, isl_int v)
1591 if (isl_int_is_one(v))
1592 return qp;
1594 if (qp && isl_int_is_zero(v)) {
1595 isl_qpolynomial *zero;
1596 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1597 isl_qpolynomial_free(qp);
1598 return zero;
1601 qp = isl_qpolynomial_cow(qp);
1602 if (!qp)
1603 return NULL;
1605 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1606 if (!qp->upoly)
1607 goto error;
1609 return qp;
1610 error:
1611 isl_qpolynomial_free(qp);
1612 return NULL;
1615 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1616 __isl_take isl_qpolynomial *qp, isl_int v)
1618 return isl_qpolynomial_mul_isl_int(qp, v);
1621 /* Multiply "qp" by "v".
1623 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1624 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1626 if (!qp || !v)
1627 goto error;
1629 if (!isl_val_is_rat(v))
1630 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1631 "expecting rational factor", goto error);
1633 if (isl_val_is_one(v)) {
1634 isl_val_free(v);
1635 return qp;
1638 if (isl_val_is_zero(v)) {
1639 isl_space *space;
1641 space = isl_qpolynomial_get_domain_space(qp);
1642 isl_qpolynomial_free(qp);
1643 isl_val_free(v);
1644 return isl_qpolynomial_zero_on_domain(space);
1647 qp = isl_qpolynomial_cow(qp);
1648 if (!qp)
1649 goto error;
1651 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1652 if (!qp->upoly)
1653 qp = isl_qpolynomial_free(qp);
1655 isl_val_free(v);
1656 return qp;
1657 error:
1658 isl_val_free(v);
1659 isl_qpolynomial_free(qp);
1660 return NULL;
1663 /* Divide "qp" by "v".
1665 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1666 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1668 if (!qp || !v)
1669 goto error;
1671 if (!isl_val_is_rat(v))
1672 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1673 "expecting rational factor", goto error);
1674 if (isl_val_is_zero(v))
1675 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1676 "cannot scale down by zero", goto error);
1678 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1679 error:
1680 isl_val_free(v);
1681 isl_qpolynomial_free(qp);
1682 return NULL;
1685 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1686 __isl_take isl_qpolynomial *qp2)
1688 isl_bool compatible;
1690 qp1 = isl_qpolynomial_cow(qp1);
1692 if (!qp1 || !qp2)
1693 goto error;
1695 if (qp1->div->n_row < qp2->div->n_row)
1696 return isl_qpolynomial_mul(qp2, qp1);
1698 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1699 compatible = compatible_divs(qp1->div, qp2->div);
1700 if (compatible < 0)
1701 goto error;
1702 if (!compatible)
1703 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1705 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1706 if (!qp1->upoly)
1707 goto error;
1709 isl_qpolynomial_free(qp2);
1711 return qp1;
1712 error:
1713 isl_qpolynomial_free(qp1);
1714 isl_qpolynomial_free(qp2);
1715 return NULL;
1718 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1719 unsigned power)
1721 qp = isl_qpolynomial_cow(qp);
1723 if (!qp)
1724 return NULL;
1726 qp->upoly = isl_upoly_pow(qp->upoly, power);
1727 if (!qp->upoly)
1728 goto error;
1730 return qp;
1731 error:
1732 isl_qpolynomial_free(qp);
1733 return NULL;
1736 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1737 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1739 int i;
1741 if (power == 1)
1742 return pwqp;
1744 pwqp = isl_pw_qpolynomial_cow(pwqp);
1745 if (!pwqp)
1746 return NULL;
1748 for (i = 0; i < pwqp->n; ++i) {
1749 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1750 if (!pwqp->p[i].qp)
1751 return isl_pw_qpolynomial_free(pwqp);
1754 return pwqp;
1757 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1758 __isl_take isl_space *dim)
1760 if (!dim)
1761 return NULL;
1762 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1765 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1766 __isl_take isl_space *dim)
1768 if (!dim)
1769 return NULL;
1770 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1773 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1774 __isl_take isl_space *dim)
1776 if (!dim)
1777 return NULL;
1778 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1781 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1782 __isl_take isl_space *dim)
1784 if (!dim)
1785 return NULL;
1786 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1789 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1790 __isl_take isl_space *dim)
1792 if (!dim)
1793 return NULL;
1794 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1797 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1798 __isl_take isl_space *dim,
1799 isl_int v)
1801 struct isl_qpolynomial *qp;
1802 struct isl_upoly_cst *cst;
1804 if (!dim)
1805 return NULL;
1807 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1808 if (!qp)
1809 return NULL;
1811 cst = isl_upoly_as_cst(qp->upoly);
1812 isl_int_set(cst->n, v);
1814 return qp;
1817 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1818 isl_int *n, isl_int *d)
1820 struct isl_upoly_cst *cst;
1822 if (!qp)
1823 return -1;
1825 if (!isl_upoly_is_cst(qp->upoly))
1826 return 0;
1828 cst = isl_upoly_as_cst(qp->upoly);
1829 if (!cst)
1830 return -1;
1832 if (n)
1833 isl_int_set(*n, cst->n);
1834 if (d)
1835 isl_int_set(*d, cst->d);
1837 return 1;
1840 /* Return the constant term of "up".
1842 static __isl_give isl_val *isl_upoly_get_constant_val(
1843 __isl_keep struct isl_upoly *up)
1845 struct isl_upoly_cst *cst;
1847 if (!up)
1848 return NULL;
1850 while (!isl_upoly_is_cst(up)) {
1851 struct isl_upoly_rec *rec;
1853 rec = isl_upoly_as_rec(up);
1854 if (!rec)
1855 return NULL;
1856 up = rec->p[0];
1859 cst = isl_upoly_as_cst(up);
1860 if (!cst)
1861 return NULL;
1862 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1865 /* Return the constant term of "qp".
1867 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1868 __isl_keep isl_qpolynomial *qp)
1870 if (!qp)
1871 return NULL;
1873 return isl_upoly_get_constant_val(qp->upoly);
1876 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1878 int is_cst;
1879 struct isl_upoly_rec *rec;
1881 if (!up)
1882 return -1;
1884 if (up->var < 0)
1885 return 1;
1887 rec = isl_upoly_as_rec(up);
1888 if (!rec)
1889 return -1;
1891 if (rec->n > 2)
1892 return 0;
1894 isl_assert(up->ctx, rec->n > 1, return -1);
1896 is_cst = isl_upoly_is_cst(rec->p[1]);
1897 if (is_cst < 0)
1898 return -1;
1899 if (!is_cst)
1900 return 0;
1902 return isl_upoly_is_affine(rec->p[0]);
1905 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1907 if (!qp)
1908 return -1;
1910 if (qp->div->n_row > 0)
1911 return 0;
1913 return isl_upoly_is_affine(qp->upoly);
1916 static void update_coeff(__isl_keep isl_vec *aff,
1917 __isl_keep struct isl_upoly_cst *cst, int pos)
1919 isl_int gcd;
1920 isl_int f;
1922 if (isl_int_is_zero(cst->n))
1923 return;
1925 isl_int_init(gcd);
1926 isl_int_init(f);
1927 isl_int_gcd(gcd, cst->d, aff->el[0]);
1928 isl_int_divexact(f, cst->d, gcd);
1929 isl_int_divexact(gcd, aff->el[0], gcd);
1930 isl_seq_scale(aff->el, aff->el, f, aff->size);
1931 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1932 isl_int_clear(gcd);
1933 isl_int_clear(f);
1936 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1937 __isl_keep isl_vec *aff)
1939 struct isl_upoly_cst *cst;
1940 struct isl_upoly_rec *rec;
1942 if (!up || !aff)
1943 return -1;
1945 if (up->var < 0) {
1946 struct isl_upoly_cst *cst;
1948 cst = isl_upoly_as_cst(up);
1949 if (!cst)
1950 return -1;
1951 update_coeff(aff, cst, 0);
1952 return 0;
1955 rec = isl_upoly_as_rec(up);
1956 if (!rec)
1957 return -1;
1958 isl_assert(up->ctx, rec->n == 2, return -1);
1960 cst = isl_upoly_as_cst(rec->p[1]);
1961 if (!cst)
1962 return -1;
1963 update_coeff(aff, cst, 1 + up->var);
1965 return isl_upoly_update_affine(rec->p[0], aff);
1968 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1969 __isl_keep isl_qpolynomial *qp)
1971 isl_vec *aff;
1972 unsigned d;
1974 if (!qp)
1975 return NULL;
1977 d = isl_space_dim(qp->dim, isl_dim_all);
1978 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1979 if (!aff)
1980 return NULL;
1982 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1983 isl_int_set_si(aff->el[0], 1);
1985 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1986 goto error;
1988 return aff;
1989 error:
1990 isl_vec_free(aff);
1991 return NULL;
1994 /* Compare two quasi-polynomials.
1996 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
1997 * than "qp2" and 0 if they are equal.
1999 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2000 __isl_keep isl_qpolynomial *qp2)
2002 int cmp;
2004 if (qp1 == qp2)
2005 return 0;
2006 if (!qp1)
2007 return -1;
2008 if (!qp2)
2009 return 1;
2011 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2012 if (cmp != 0)
2013 return cmp;
2015 cmp = isl_local_cmp(qp1->div, qp2->div);
2016 if (cmp != 0)
2017 return cmp;
2019 return isl_upoly_plain_cmp(qp1->upoly, qp2->upoly);
2022 /* Is "qp1" obviously equal to "qp2"?
2024 * NaN is not equal to anything, not even to another NaN.
2026 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2027 __isl_keep isl_qpolynomial *qp2)
2029 isl_bool equal;
2031 if (!qp1 || !qp2)
2032 return isl_bool_error;
2034 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2035 return isl_bool_false;
2037 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2038 if (equal < 0 || !equal)
2039 return equal;
2041 equal = isl_mat_is_equal(qp1->div, qp2->div);
2042 if (equal < 0 || !equal)
2043 return equal;
2045 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
2048 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
2050 int i;
2051 struct isl_upoly_rec *rec;
2053 if (isl_upoly_is_cst(up)) {
2054 struct isl_upoly_cst *cst;
2055 cst = isl_upoly_as_cst(up);
2056 if (!cst)
2057 return;
2058 isl_int_lcm(*d, *d, cst->d);
2059 return;
2062 rec = isl_upoly_as_rec(up);
2063 if (!rec)
2064 return;
2066 for (i = 0; i < rec->n; ++i)
2067 upoly_update_den(rec->p[i], d);
2070 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
2072 isl_int_set_si(*d, 1);
2073 if (!qp)
2074 return;
2075 upoly_update_den(qp->upoly, d);
2078 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2079 __isl_take isl_space *dim, int pos, int power)
2081 struct isl_ctx *ctx;
2083 if (!dim)
2084 return NULL;
2086 ctx = dim->ctx;
2088 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
2091 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(__isl_take isl_space *dim,
2092 enum isl_dim_type type, unsigned pos)
2094 if (!dim)
2095 return NULL;
2097 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
2098 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
2100 if (type == isl_dim_set)
2101 pos += isl_space_dim(dim, isl_dim_param);
2103 return isl_qpolynomial_var_pow_on_domain(dim, pos, 1);
2104 error:
2105 isl_space_free(dim);
2106 return NULL;
2109 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
2110 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
2112 int i;
2113 struct isl_upoly_rec *rec;
2114 struct isl_upoly *base, *res;
2116 if (!up)
2117 return NULL;
2119 if (isl_upoly_is_cst(up))
2120 return up;
2122 if (up->var < first)
2123 return up;
2125 rec = isl_upoly_as_rec(up);
2126 if (!rec)
2127 goto error;
2129 isl_assert(up->ctx, rec->n >= 1, goto error);
2131 if (up->var >= first + n)
2132 base = isl_upoly_var_pow(up->ctx, up->var, 1);
2133 else
2134 base = isl_upoly_copy(subs[up->var - first]);
2136 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
2137 for (i = rec->n - 2; i >= 0; --i) {
2138 struct isl_upoly *t;
2139 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
2140 res = isl_upoly_mul(res, isl_upoly_copy(base));
2141 res = isl_upoly_sum(res, t);
2144 isl_upoly_free(base);
2145 isl_upoly_free(up);
2147 return res;
2148 error:
2149 isl_upoly_free(up);
2150 return NULL;
2153 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
2154 isl_int denom, unsigned len)
2156 int i;
2157 struct isl_upoly *up;
2159 isl_assert(ctx, len >= 1, return NULL);
2161 up = isl_upoly_rat_cst(ctx, f[0], denom);
2162 for (i = 0; i < len - 1; ++i) {
2163 struct isl_upoly *t;
2164 struct isl_upoly *c;
2166 if (isl_int_is_zero(f[1 + i]))
2167 continue;
2169 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2170 t = isl_upoly_var_pow(ctx, i, 1);
2171 t = isl_upoly_mul(c, t);
2172 up = isl_upoly_sum(up, t);
2175 return up;
2178 /* Remove common factor of non-constant terms and denominator.
2180 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2182 isl_ctx *ctx = qp->div->ctx;
2183 unsigned total = qp->div->n_col - 2;
2185 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2186 isl_int_gcd(ctx->normalize_gcd,
2187 ctx->normalize_gcd, qp->div->row[div][0]);
2188 if (isl_int_is_one(ctx->normalize_gcd))
2189 return;
2191 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2192 ctx->normalize_gcd, total);
2193 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2194 ctx->normalize_gcd);
2195 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2196 ctx->normalize_gcd);
2199 /* Replace the integer division identified by "div" by the polynomial "s".
2200 * The integer division is assumed not to appear in the definition
2201 * of any other integer divisions.
2203 static __isl_give isl_qpolynomial *substitute_div(
2204 __isl_take isl_qpolynomial *qp,
2205 int div, __isl_take struct isl_upoly *s)
2207 int i;
2208 int total;
2209 int *reordering;
2211 if (!qp || !s)
2212 goto error;
2214 qp = isl_qpolynomial_cow(qp);
2215 if (!qp)
2216 goto error;
2218 total = isl_space_dim(qp->dim, isl_dim_all);
2219 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2220 if (!qp->upoly)
2221 goto error;
2223 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2224 if (!reordering)
2225 goto error;
2226 for (i = 0; i < total + div; ++i)
2227 reordering[i] = i;
2228 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2229 reordering[i] = i - 1;
2230 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2231 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2232 qp->upoly = reorder(qp->upoly, reordering);
2233 free(reordering);
2235 if (!qp->upoly || !qp->div)
2236 goto error;
2238 isl_upoly_free(s);
2239 return qp;
2240 error:
2241 isl_qpolynomial_free(qp);
2242 isl_upoly_free(s);
2243 return NULL;
2246 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2247 * divisions because d is equal to 1 by their definition, i.e., e.
2249 static __isl_give isl_qpolynomial *substitute_non_divs(
2250 __isl_take isl_qpolynomial *qp)
2252 int i, j;
2253 int total;
2254 struct isl_upoly *s;
2256 if (!qp)
2257 return NULL;
2259 total = isl_space_dim(qp->dim, isl_dim_all);
2260 for (i = 0; qp && i < qp->div->n_row; ++i) {
2261 if (!isl_int_is_one(qp->div->row[i][0]))
2262 continue;
2263 for (j = i + 1; j < qp->div->n_row; ++j) {
2264 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2265 continue;
2266 isl_seq_combine(qp->div->row[j] + 1,
2267 qp->div->ctx->one, qp->div->row[j] + 1,
2268 qp->div->row[j][2 + total + i],
2269 qp->div->row[i] + 1, 1 + total + i);
2270 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2271 normalize_div(qp, j);
2273 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2274 qp->div->row[i][0], qp->div->n_col - 1);
2275 qp = substitute_div(qp, i, s);
2276 --i;
2279 return qp;
2282 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2283 * with d the denominator. When replacing the coefficient e of x by
2284 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2285 * inside the division, so we need to add floor(e/d) * x outside.
2286 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2287 * to adjust the coefficient of x in each later div that depends on the
2288 * current div "div" and also in the affine expressions in the rows of "mat"
2289 * (if they too depend on "div").
2291 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2292 __isl_keep isl_mat **mat)
2294 int i, j;
2295 isl_int v;
2296 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2298 isl_int_init(v);
2299 for (i = 0; i < 1 + total + div; ++i) {
2300 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2301 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2302 continue;
2303 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2304 isl_int_fdiv_r(qp->div->row[div][1 + i],
2305 qp->div->row[div][1 + i], qp->div->row[div][0]);
2306 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2307 for (j = div + 1; j < qp->div->n_row; ++j) {
2308 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2309 continue;
2310 isl_int_addmul(qp->div->row[j][1 + i],
2311 v, qp->div->row[j][2 + total + div]);
2314 isl_int_clear(v);
2317 /* Check if the last non-zero coefficient is bigger that half of the
2318 * denominator. If so, we will invert the div to further reduce the number
2319 * of distinct divs that may appear.
2320 * If the last non-zero coefficient is exactly half the denominator,
2321 * then we continue looking for earlier coefficients that are bigger
2322 * than half the denominator.
2324 static int needs_invert(__isl_keep isl_mat *div, int row)
2326 int i;
2327 int cmp;
2329 for (i = div->n_col - 1; i >= 1; --i) {
2330 if (isl_int_is_zero(div->row[row][i]))
2331 continue;
2332 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2333 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2334 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2335 if (cmp)
2336 return cmp > 0;
2337 if (i == 1)
2338 return 1;
2341 return 0;
2344 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2345 * We only invert the coefficients of e (and the coefficient of q in
2346 * later divs and in the rows of "mat"). After calling this function, the
2347 * coefficients of e should be reduced again.
2349 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2350 __isl_keep isl_mat **mat)
2352 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2354 isl_seq_neg(qp->div->row[div] + 1,
2355 qp->div->row[div] + 1, qp->div->n_col - 1);
2356 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2357 isl_int_add(qp->div->row[div][1],
2358 qp->div->row[div][1], qp->div->row[div][0]);
2359 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2360 isl_mat_col_mul(qp->div, 2 + total + div,
2361 qp->div->ctx->negone, 2 + total + div);
2364 /* Reduce all divs of "qp" to have coefficients
2365 * in the interval [0, d-1], with d the denominator and such that the
2366 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2367 * The modifications to the integer divisions need to be reflected
2368 * in the factors of the polynomial that refer to the original
2369 * integer divisions. To this end, the modifications are collected
2370 * as a set of affine expressions and then plugged into the polynomial.
2372 * After the reduction, some divs may have become redundant or identical,
2373 * so we call substitute_non_divs and sort_divs. If these functions
2374 * eliminate divs or merge two or more divs into one, the coefficients
2375 * of the enclosing divs may have to be reduced again, so we call
2376 * ourselves recursively if the number of divs decreases.
2378 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2380 int i;
2381 isl_ctx *ctx;
2382 isl_mat *mat;
2383 struct isl_upoly **s;
2384 unsigned o_div, n_div, total;
2386 if (!qp)
2387 return NULL;
2389 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2390 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2391 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2392 ctx = isl_qpolynomial_get_ctx(qp);
2393 mat = isl_mat_zero(ctx, n_div, 1 + total);
2395 for (i = 0; i < n_div; ++i)
2396 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2398 for (i = 0; i < qp->div->n_row; ++i) {
2399 normalize_div(qp, i);
2400 reduce_div(qp, i, &mat);
2401 if (needs_invert(qp->div, i)) {
2402 invert_div(qp, i, &mat);
2403 reduce_div(qp, i, &mat);
2406 if (!mat)
2407 goto error;
2409 s = isl_alloc_array(ctx, struct isl_upoly *, n_div);
2410 if (n_div && !s)
2411 goto error;
2412 for (i = 0; i < n_div; ++i)
2413 s[i] = isl_upoly_from_affine(ctx, mat->row[i], ctx->one,
2414 1 + total);
2415 qp->upoly = isl_upoly_subs(qp->upoly, o_div - 1, n_div, s);
2416 for (i = 0; i < n_div; ++i)
2417 isl_upoly_free(s[i]);
2418 free(s);
2419 if (!qp->upoly)
2420 goto error;
2422 isl_mat_free(mat);
2424 qp = substitute_non_divs(qp);
2425 qp = sort_divs(qp);
2426 if (qp && isl_qpolynomial_domain_dim(qp, isl_dim_div) < n_div)
2427 return reduce_divs(qp);
2429 return qp;
2430 error:
2431 isl_qpolynomial_free(qp);
2432 isl_mat_free(mat);
2433 return NULL;
2436 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2437 __isl_take isl_space *dim, const isl_int n, const isl_int d)
2439 struct isl_qpolynomial *qp;
2440 struct isl_upoly_cst *cst;
2442 if (!dim)
2443 return NULL;
2445 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2446 if (!qp)
2447 return NULL;
2449 cst = isl_upoly_as_cst(qp->upoly);
2450 isl_int_set(cst->n, n);
2451 isl_int_set(cst->d, d);
2453 return qp;
2456 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2458 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2459 __isl_take isl_space *domain, __isl_take isl_val *val)
2461 isl_qpolynomial *qp;
2462 struct isl_upoly_cst *cst;
2464 if (!domain || !val)
2465 goto error;
2467 qp = isl_qpolynomial_alloc(isl_space_copy(domain), 0,
2468 isl_upoly_zero(domain->ctx));
2469 if (!qp)
2470 goto error;
2472 cst = isl_upoly_as_cst(qp->upoly);
2473 isl_int_set(cst->n, val->n);
2474 isl_int_set(cst->d, val->d);
2476 isl_space_free(domain);
2477 isl_val_free(val);
2478 return qp;
2479 error:
2480 isl_space_free(domain);
2481 isl_val_free(val);
2482 return NULL;
2485 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2487 struct isl_upoly_rec *rec;
2488 int i;
2490 if (!up)
2491 return -1;
2493 if (isl_upoly_is_cst(up))
2494 return 0;
2496 if (up->var < d)
2497 active[up->var] = 1;
2499 rec = isl_upoly_as_rec(up);
2500 for (i = 0; i < rec->n; ++i)
2501 if (up_set_active(rec->p[i], active, d) < 0)
2502 return -1;
2504 return 0;
2507 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2509 int i, j;
2510 int d = isl_space_dim(qp->dim, isl_dim_all);
2512 if (!qp || !active)
2513 return -1;
2515 for (i = 0; i < d; ++i)
2516 for (j = 0; j < qp->div->n_row; ++j) {
2517 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2518 continue;
2519 active[i] = 1;
2520 break;
2523 return up_set_active(qp->upoly, active, d);
2526 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2527 enum isl_dim_type type, unsigned first, unsigned n)
2529 int i;
2530 int *active = NULL;
2531 isl_bool involves = isl_bool_false;
2533 if (!qp)
2534 return isl_bool_error;
2535 if (n == 0)
2536 return isl_bool_false;
2538 isl_assert(qp->dim->ctx,
2539 first + n <= isl_qpolynomial_dim(qp, type),
2540 return isl_bool_error);
2541 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2542 type == isl_dim_in, return isl_bool_error);
2544 active = isl_calloc_array(qp->dim->ctx, int,
2545 isl_space_dim(qp->dim, isl_dim_all));
2546 if (set_active(qp, active) < 0)
2547 goto error;
2549 if (type == isl_dim_in)
2550 first += isl_space_dim(qp->dim, isl_dim_param);
2551 for (i = 0; i < n; ++i)
2552 if (active[first + i]) {
2553 involves = isl_bool_true;
2554 break;
2557 free(active);
2559 return involves;
2560 error:
2561 free(active);
2562 return isl_bool_error;
2565 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2566 * of the divs that do appear in the quasi-polynomial.
2568 static __isl_give isl_qpolynomial *remove_redundant_divs(
2569 __isl_take isl_qpolynomial *qp)
2571 int i, j;
2572 int d;
2573 int len;
2574 int skip;
2575 int *active = NULL;
2576 int *reordering = NULL;
2577 int redundant = 0;
2578 int n_div;
2579 isl_ctx *ctx;
2581 if (!qp)
2582 return NULL;
2583 if (qp->div->n_row == 0)
2584 return qp;
2586 d = isl_space_dim(qp->dim, isl_dim_all);
2587 len = qp->div->n_col - 2;
2588 ctx = isl_qpolynomial_get_ctx(qp);
2589 active = isl_calloc_array(ctx, int, len);
2590 if (!active)
2591 goto error;
2593 if (up_set_active(qp->upoly, active, len) < 0)
2594 goto error;
2596 for (i = qp->div->n_row - 1; i >= 0; --i) {
2597 if (!active[d + i]) {
2598 redundant = 1;
2599 continue;
2601 for (j = 0; j < i; ++j) {
2602 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2603 continue;
2604 active[d + j] = 1;
2605 break;
2609 if (!redundant) {
2610 free(active);
2611 return qp;
2614 reordering = isl_alloc_array(qp->div->ctx, int, len);
2615 if (!reordering)
2616 goto error;
2618 for (i = 0; i < d; ++i)
2619 reordering[i] = i;
2621 skip = 0;
2622 n_div = qp->div->n_row;
2623 for (i = 0; i < n_div; ++i) {
2624 if (!active[d + i]) {
2625 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2626 qp->div = isl_mat_drop_cols(qp->div,
2627 2 + d + i - skip, 1);
2628 skip++;
2630 reordering[d + i] = d + i - skip;
2633 qp->upoly = reorder(qp->upoly, reordering);
2635 if (!qp->upoly || !qp->div)
2636 goto error;
2638 free(active);
2639 free(reordering);
2641 return qp;
2642 error:
2643 free(active);
2644 free(reordering);
2645 isl_qpolynomial_free(qp);
2646 return NULL;
2649 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2650 unsigned first, unsigned n)
2652 int i;
2653 struct isl_upoly_rec *rec;
2655 if (!up)
2656 return NULL;
2657 if (n == 0 || up->var < 0 || up->var < first)
2658 return up;
2659 if (up->var < first + n) {
2660 up = replace_by_constant_term(up);
2661 return isl_upoly_drop(up, first, n);
2663 up = isl_upoly_cow(up);
2664 if (!up)
2665 return NULL;
2666 up->var -= n;
2667 rec = isl_upoly_as_rec(up);
2668 if (!rec)
2669 goto error;
2671 for (i = 0; i < rec->n; ++i) {
2672 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2673 if (!rec->p[i])
2674 goto error;
2677 return up;
2678 error:
2679 isl_upoly_free(up);
2680 return NULL;
2683 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2684 __isl_take isl_qpolynomial *qp,
2685 enum isl_dim_type type, unsigned pos, const char *s)
2687 qp = isl_qpolynomial_cow(qp);
2688 if (!qp)
2689 return NULL;
2690 if (type == isl_dim_out)
2691 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2692 "cannot set name of output/set dimension",
2693 return isl_qpolynomial_free(qp));
2694 if (type == isl_dim_in)
2695 type = isl_dim_set;
2696 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2697 if (!qp->dim)
2698 goto error;
2699 return qp;
2700 error:
2701 isl_qpolynomial_free(qp);
2702 return NULL;
2705 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2706 __isl_take isl_qpolynomial *qp,
2707 enum isl_dim_type type, unsigned first, unsigned n)
2709 if (!qp)
2710 return NULL;
2711 if (type == isl_dim_out)
2712 isl_die(qp->dim->ctx, isl_error_invalid,
2713 "cannot drop output/set dimension",
2714 goto error);
2715 if (type == isl_dim_in)
2716 type = isl_dim_set;
2717 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2718 return qp;
2720 qp = isl_qpolynomial_cow(qp);
2721 if (!qp)
2722 return NULL;
2724 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2725 goto error);
2726 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2727 type == isl_dim_set, goto error);
2729 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2730 if (!qp->dim)
2731 goto error;
2733 if (type == isl_dim_set)
2734 first += isl_space_dim(qp->dim, isl_dim_param);
2736 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2737 if (!qp->div)
2738 goto error;
2740 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2741 if (!qp->upoly)
2742 goto error;
2744 return qp;
2745 error:
2746 isl_qpolynomial_free(qp);
2747 return NULL;
2750 /* Project the domain of the quasi-polynomial onto its parameter space.
2751 * The quasi-polynomial may not involve any of the domain dimensions.
2753 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2754 __isl_take isl_qpolynomial *qp)
2756 isl_space *space;
2757 unsigned n;
2758 int involves;
2760 n = isl_qpolynomial_dim(qp, isl_dim_in);
2761 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2762 if (involves < 0)
2763 return isl_qpolynomial_free(qp);
2764 if (involves)
2765 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2766 "polynomial involves some of the domain dimensions",
2767 return isl_qpolynomial_free(qp));
2768 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2769 space = isl_qpolynomial_get_domain_space(qp);
2770 space = isl_space_params(space);
2771 qp = isl_qpolynomial_reset_domain_space(qp, space);
2772 return qp;
2775 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2776 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2778 int i, j, k;
2779 isl_int denom;
2780 unsigned total;
2781 unsigned n_div;
2782 struct isl_upoly *up;
2784 if (!eq)
2785 goto error;
2786 if (eq->n_eq == 0) {
2787 isl_basic_set_free(eq);
2788 return qp;
2791 qp = isl_qpolynomial_cow(qp);
2792 if (!qp)
2793 goto error;
2794 qp->div = isl_mat_cow(qp->div);
2795 if (!qp->div)
2796 goto error;
2798 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2799 n_div = eq->n_div;
2800 isl_int_init(denom);
2801 for (i = 0; i < eq->n_eq; ++i) {
2802 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2803 if (j < 0 || j == 0 || j >= total)
2804 continue;
2806 for (k = 0; k < qp->div->n_row; ++k) {
2807 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2808 continue;
2809 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2810 &qp->div->row[k][0]);
2811 normalize_div(qp, k);
2814 if (isl_int_is_pos(eq->eq[i][j]))
2815 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2816 isl_int_abs(denom, eq->eq[i][j]);
2817 isl_int_set_si(eq->eq[i][j], 0);
2819 up = isl_upoly_from_affine(qp->dim->ctx,
2820 eq->eq[i], denom, total);
2821 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2822 isl_upoly_free(up);
2824 isl_int_clear(denom);
2826 if (!qp->upoly)
2827 goto error;
2829 isl_basic_set_free(eq);
2831 qp = substitute_non_divs(qp);
2832 qp = sort_divs(qp);
2834 return qp;
2835 error:
2836 isl_basic_set_free(eq);
2837 isl_qpolynomial_free(qp);
2838 return NULL;
2841 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2843 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2844 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2846 if (!qp || !eq)
2847 goto error;
2848 if (qp->div->n_row > 0)
2849 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2850 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2851 error:
2852 isl_basic_set_free(eq);
2853 isl_qpolynomial_free(qp);
2854 return NULL;
2857 static __isl_give isl_basic_set *add_div_constraints(
2858 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2860 int i;
2861 unsigned total;
2863 if (!bset || !div)
2864 goto error;
2866 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2867 if (!bset)
2868 goto error;
2869 total = isl_basic_set_total_dim(bset);
2870 for (i = 0; i < div->n_row; ++i)
2871 if (isl_basic_set_add_div_constraints_var(bset,
2872 total - div->n_row + i, div->row[i]) < 0)
2873 goto error;
2875 isl_mat_free(div);
2876 return bset;
2877 error:
2878 isl_mat_free(div);
2879 isl_basic_set_free(bset);
2880 return NULL;
2883 /* Look for equalities among the variables shared by context and qp
2884 * and the integer divisions of qp, if any.
2885 * The equalities are then used to eliminate variables and/or integer
2886 * divisions from qp.
2888 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2889 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2891 isl_basic_set *aff;
2893 if (!qp)
2894 goto error;
2895 if (qp->div->n_row > 0) {
2896 isl_basic_set *bset;
2897 context = isl_set_add_dims(context, isl_dim_set,
2898 qp->div->n_row);
2899 bset = isl_basic_set_universe(isl_set_get_space(context));
2900 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2901 context = isl_set_intersect(context,
2902 isl_set_from_basic_set(bset));
2905 aff = isl_set_affine_hull(context);
2906 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2907 error:
2908 isl_qpolynomial_free(qp);
2909 isl_set_free(context);
2910 return NULL;
2913 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2914 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2916 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2917 isl_set *dom_context = isl_set_universe(space);
2918 dom_context = isl_set_intersect_params(dom_context, context);
2919 return isl_qpolynomial_gist(qp, dom_context);
2922 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2923 __isl_take isl_qpolynomial *qp)
2925 isl_set *dom;
2927 if (!qp)
2928 return NULL;
2929 if (isl_qpolynomial_is_zero(qp)) {
2930 isl_space *dim = isl_qpolynomial_get_space(qp);
2931 isl_qpolynomial_free(qp);
2932 return isl_pw_qpolynomial_zero(dim);
2935 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2936 return isl_pw_qpolynomial_alloc(dom, qp);
2939 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2941 #undef PW
2942 #define PW isl_pw_qpolynomial
2943 #undef EL
2944 #define EL isl_qpolynomial
2945 #undef EL_IS_ZERO
2946 #define EL_IS_ZERO is_zero
2947 #undef ZERO
2948 #define ZERO zero
2949 #undef IS_ZERO
2950 #define IS_ZERO is_zero
2951 #undef FIELD
2952 #define FIELD qp
2953 #undef DEFAULT_IS_ZERO
2954 #define DEFAULT_IS_ZERO 1
2956 #define NO_PULLBACK
2958 #include <isl_pw_templ.c>
2959 #include <isl_pw_eval.c>
2961 #undef UNION
2962 #define UNION isl_union_pw_qpolynomial
2963 #undef PART
2964 #define PART isl_pw_qpolynomial
2965 #undef PARTS
2966 #define PARTS pw_qpolynomial
2968 #include <isl_union_single.c>
2969 #include <isl_union_eval.c>
2970 #include <isl_union_neg.c>
2972 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2974 if (!pwqp)
2975 return -1;
2977 if (pwqp->n != -1)
2978 return 0;
2980 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2981 return 0;
2983 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2986 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2987 __isl_take isl_pw_qpolynomial *pwqp1,
2988 __isl_take isl_pw_qpolynomial *pwqp2)
2990 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2993 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2994 __isl_take isl_pw_qpolynomial *pwqp1,
2995 __isl_take isl_pw_qpolynomial *pwqp2)
2997 int i, j, n;
2998 struct isl_pw_qpolynomial *res;
3000 if (!pwqp1 || !pwqp2)
3001 goto error;
3003 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3004 goto error);
3006 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3007 isl_pw_qpolynomial_free(pwqp2);
3008 return pwqp1;
3011 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3012 isl_pw_qpolynomial_free(pwqp1);
3013 return pwqp2;
3016 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3017 isl_pw_qpolynomial_free(pwqp1);
3018 return pwqp2;
3021 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3022 isl_pw_qpolynomial_free(pwqp2);
3023 return pwqp1;
3026 n = pwqp1->n * pwqp2->n;
3027 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3029 for (i = 0; i < pwqp1->n; ++i) {
3030 for (j = 0; j < pwqp2->n; ++j) {
3031 struct isl_set *common;
3032 struct isl_qpolynomial *prod;
3033 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3034 isl_set_copy(pwqp2->p[j].set));
3035 if (isl_set_plain_is_empty(common)) {
3036 isl_set_free(common);
3037 continue;
3040 prod = isl_qpolynomial_mul(
3041 isl_qpolynomial_copy(pwqp1->p[i].qp),
3042 isl_qpolynomial_copy(pwqp2->p[j].qp));
3044 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3048 isl_pw_qpolynomial_free(pwqp1);
3049 isl_pw_qpolynomial_free(pwqp2);
3051 return res;
3052 error:
3053 isl_pw_qpolynomial_free(pwqp1);
3054 isl_pw_qpolynomial_free(pwqp2);
3055 return NULL;
3058 __isl_give isl_val *isl_upoly_eval(__isl_take struct isl_upoly *up,
3059 __isl_take isl_vec *vec)
3061 int i;
3062 struct isl_upoly_rec *rec;
3063 isl_val *res;
3064 isl_val *base;
3066 if (isl_upoly_is_cst(up)) {
3067 isl_vec_free(vec);
3068 res = isl_upoly_get_constant_val(up);
3069 isl_upoly_free(up);
3070 return res;
3073 rec = isl_upoly_as_rec(up);
3074 if (!rec || !vec)
3075 goto error;
3077 isl_assert(up->ctx, rec->n >= 1, goto error);
3079 base = isl_val_rat_from_isl_int(up->ctx,
3080 vec->el[1 + up->var], vec->el[0]);
3082 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
3083 isl_vec_copy(vec));
3085 for (i = rec->n - 2; i >= 0; --i) {
3086 res = isl_val_mul(res, isl_val_copy(base));
3087 res = isl_val_add(res,
3088 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
3089 isl_vec_copy(vec)));
3092 isl_val_free(base);
3093 isl_upoly_free(up);
3094 isl_vec_free(vec);
3095 return res;
3096 error:
3097 isl_upoly_free(up);
3098 isl_vec_free(vec);
3099 return NULL;
3102 /* Evaluate "qp" in the void point "pnt".
3103 * In particular, return the value NaN.
3105 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3106 __isl_take isl_point *pnt)
3108 isl_ctx *ctx;
3110 ctx = isl_point_get_ctx(pnt);
3111 isl_qpolynomial_free(qp);
3112 isl_point_free(pnt);
3113 return isl_val_nan(ctx);
3116 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3117 __isl_take isl_point *pnt)
3119 isl_bool is_void;
3120 isl_vec *ext;
3121 isl_val *v;
3123 if (!qp || !pnt)
3124 goto error;
3125 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3126 is_void = isl_point_is_void(pnt);
3127 if (is_void < 0)
3128 goto error;
3129 if (is_void)
3130 return eval_void(qp, pnt);
3132 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3134 v = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
3136 isl_qpolynomial_free(qp);
3137 isl_point_free(pnt);
3139 return v;
3140 error:
3141 isl_qpolynomial_free(qp);
3142 isl_point_free(pnt);
3143 return NULL;
3146 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
3147 __isl_keep struct isl_upoly_cst *cst2)
3149 int cmp;
3150 isl_int t;
3151 isl_int_init(t);
3152 isl_int_mul(t, cst1->n, cst2->d);
3153 isl_int_submul(t, cst2->n, cst1->d);
3154 cmp = isl_int_sgn(t);
3155 isl_int_clear(t);
3156 return cmp;
3159 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3160 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3161 unsigned first, unsigned n)
3163 unsigned total;
3164 unsigned g_pos;
3165 int *exp;
3167 if (!qp)
3168 return NULL;
3169 if (type == isl_dim_out)
3170 isl_die(qp->div->ctx, isl_error_invalid,
3171 "cannot insert output/set dimensions",
3172 goto error);
3173 if (type == isl_dim_in)
3174 type = isl_dim_set;
3175 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3176 return qp;
3178 qp = isl_qpolynomial_cow(qp);
3179 if (!qp)
3180 return NULL;
3182 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
3183 goto error);
3185 g_pos = pos(qp->dim, type) + first;
3187 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3188 if (!qp->div)
3189 goto error;
3191 total = qp->div->n_col - 2;
3192 if (total > g_pos) {
3193 int i;
3194 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3195 if (!exp)
3196 goto error;
3197 for (i = 0; i < total - g_pos; ++i)
3198 exp[i] = i + n;
3199 qp->upoly = expand(qp->upoly, exp, g_pos);
3200 free(exp);
3201 if (!qp->upoly)
3202 goto error;
3205 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3206 if (!qp->dim)
3207 goto error;
3209 return qp;
3210 error:
3211 isl_qpolynomial_free(qp);
3212 return NULL;
3215 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3216 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3218 unsigned pos;
3220 pos = isl_qpolynomial_dim(qp, type);
3222 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3225 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3226 __isl_take isl_pw_qpolynomial *pwqp,
3227 enum isl_dim_type type, unsigned n)
3229 unsigned pos;
3231 pos = isl_pw_qpolynomial_dim(pwqp, type);
3233 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3236 static int *reordering_move(isl_ctx *ctx,
3237 unsigned len, unsigned dst, unsigned src, unsigned n)
3239 int i;
3240 int *reordering;
3242 reordering = isl_alloc_array(ctx, int, len);
3243 if (!reordering)
3244 return NULL;
3246 if (dst <= src) {
3247 for (i = 0; i < dst; ++i)
3248 reordering[i] = i;
3249 for (i = 0; i < n; ++i)
3250 reordering[src + i] = dst + i;
3251 for (i = 0; i < src - dst; ++i)
3252 reordering[dst + i] = dst + n + i;
3253 for (i = 0; i < len - src - n; ++i)
3254 reordering[src + n + i] = src + n + i;
3255 } else {
3256 for (i = 0; i < src; ++i)
3257 reordering[i] = i;
3258 for (i = 0; i < n; ++i)
3259 reordering[src + i] = dst + i;
3260 for (i = 0; i < dst - src; ++i)
3261 reordering[src + n + i] = src + i;
3262 for (i = 0; i < len - dst - n; ++i)
3263 reordering[dst + n + i] = dst + n + i;
3266 return reordering;
3269 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3270 __isl_take isl_qpolynomial *qp,
3271 enum isl_dim_type dst_type, unsigned dst_pos,
3272 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3274 unsigned g_dst_pos;
3275 unsigned g_src_pos;
3276 int *reordering;
3278 if (!qp)
3279 return NULL;
3281 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3282 isl_die(qp->dim->ctx, isl_error_invalid,
3283 "cannot move output/set dimension",
3284 goto error);
3285 if (dst_type == isl_dim_in)
3286 dst_type = isl_dim_set;
3287 if (src_type == isl_dim_in)
3288 src_type = isl_dim_set;
3290 if (n == 0 &&
3291 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3292 !isl_space_is_named_or_nested(qp->dim, dst_type))
3293 return qp;
3295 qp = isl_qpolynomial_cow(qp);
3296 if (!qp)
3297 return NULL;
3299 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
3300 goto error);
3302 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3303 g_src_pos = pos(qp->dim, src_type) + src_pos;
3304 if (dst_type > src_type)
3305 g_dst_pos -= n;
3307 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3308 if (!qp->div)
3309 goto error;
3310 qp = sort_divs(qp);
3311 if (!qp)
3312 goto error;
3314 reordering = reordering_move(qp->dim->ctx,
3315 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3316 if (!reordering)
3317 goto error;
3319 qp->upoly = reorder(qp->upoly, reordering);
3320 free(reordering);
3321 if (!qp->upoly)
3322 goto error;
3324 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3325 if (!qp->dim)
3326 goto error;
3328 return qp;
3329 error:
3330 isl_qpolynomial_free(qp);
3331 return NULL;
3334 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3335 isl_int *f, isl_int denom)
3337 struct isl_upoly *up;
3339 dim = isl_space_domain(dim);
3340 if (!dim)
3341 return NULL;
3343 up = isl_upoly_from_affine(dim->ctx, f, denom,
3344 1 + isl_space_dim(dim, isl_dim_all));
3346 return isl_qpolynomial_alloc(dim, 0, up);
3349 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3351 isl_ctx *ctx;
3352 struct isl_upoly *up;
3353 isl_qpolynomial *qp;
3355 if (!aff)
3356 return NULL;
3358 ctx = isl_aff_get_ctx(aff);
3359 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3360 aff->v->size - 1);
3362 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3363 aff->ls->div->n_row, up);
3364 if (!qp)
3365 goto error;
3367 isl_mat_free(qp->div);
3368 qp->div = isl_mat_copy(aff->ls->div);
3369 qp->div = isl_mat_cow(qp->div);
3370 if (!qp->div)
3371 goto error;
3373 isl_aff_free(aff);
3374 qp = reduce_divs(qp);
3375 qp = remove_redundant_divs(qp);
3376 return qp;
3377 error:
3378 isl_aff_free(aff);
3379 return isl_qpolynomial_free(qp);
3382 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3383 __isl_take isl_pw_aff *pwaff)
3385 int i;
3386 isl_pw_qpolynomial *pwqp;
3388 if (!pwaff)
3389 return NULL;
3391 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3392 pwaff->n);
3394 for (i = 0; i < pwaff->n; ++i) {
3395 isl_set *dom;
3396 isl_qpolynomial *qp;
3398 dom = isl_set_copy(pwaff->p[i].set);
3399 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3400 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3403 isl_pw_aff_free(pwaff);
3404 return pwqp;
3407 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3408 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3410 isl_aff *aff;
3412 aff = isl_constraint_get_bound(c, type, pos);
3413 isl_constraint_free(c);
3414 return isl_qpolynomial_from_aff(aff);
3417 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3418 * in "qp" by subs[i].
3420 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3421 __isl_take isl_qpolynomial *qp,
3422 enum isl_dim_type type, unsigned first, unsigned n,
3423 __isl_keep isl_qpolynomial **subs)
3425 int i;
3426 struct isl_upoly **ups;
3428 if (n == 0)
3429 return qp;
3431 qp = isl_qpolynomial_cow(qp);
3432 if (!qp)
3433 return NULL;
3435 if (type == isl_dim_out)
3436 isl_die(qp->dim->ctx, isl_error_invalid,
3437 "cannot substitute output/set dimension",
3438 goto error);
3439 if (type == isl_dim_in)
3440 type = isl_dim_set;
3442 for (i = 0; i < n; ++i)
3443 if (!subs[i])
3444 goto error;
3446 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3447 goto error);
3449 for (i = 0; i < n; ++i)
3450 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3451 goto error);
3453 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3454 for (i = 0; i < n; ++i)
3455 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3457 first += pos(qp->dim, type);
3459 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3460 if (!ups)
3461 goto error;
3462 for (i = 0; i < n; ++i)
3463 ups[i] = subs[i]->upoly;
3465 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3467 free(ups);
3469 if (!qp->upoly)
3470 goto error;
3472 return qp;
3473 error:
3474 isl_qpolynomial_free(qp);
3475 return NULL;
3478 /* Extend "bset" with extra set dimensions for each integer division
3479 * in "qp" and then call "fn" with the extended bset and the polynomial
3480 * that results from replacing each of the integer divisions by the
3481 * corresponding extra set dimension.
3483 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3484 __isl_keep isl_basic_set *bset,
3485 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3486 __isl_take isl_qpolynomial *poly, void *user), void *user)
3488 isl_space *dim;
3489 isl_mat *div;
3490 isl_qpolynomial *poly;
3492 if (!qp || !bset)
3493 return isl_stat_error;
3494 if (qp->div->n_row == 0)
3495 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3496 user);
3498 div = isl_mat_copy(qp->div);
3499 dim = isl_space_copy(qp->dim);
3500 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3501 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3502 bset = isl_basic_set_copy(bset);
3503 bset = isl_basic_set_add_dims(bset, isl_dim_set, qp->div->n_row);
3504 bset = add_div_constraints(bset, div);
3506 return fn(bset, poly, user);
3509 /* Return total degree in variables first (inclusive) up to last (exclusive).
3511 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3513 int deg = -1;
3514 int i;
3515 struct isl_upoly_rec *rec;
3517 if (!up)
3518 return -2;
3519 if (isl_upoly_is_zero(up))
3520 return -1;
3521 if (isl_upoly_is_cst(up) || up->var < first)
3522 return 0;
3524 rec = isl_upoly_as_rec(up);
3525 if (!rec)
3526 return -2;
3528 for (i = 0; i < rec->n; ++i) {
3529 int d;
3531 if (isl_upoly_is_zero(rec->p[i]))
3532 continue;
3533 d = isl_upoly_degree(rec->p[i], first, last);
3534 if (up->var < last)
3535 d += i;
3536 if (d > deg)
3537 deg = d;
3540 return deg;
3543 /* Return total degree in set variables.
3545 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3547 unsigned ovar;
3548 unsigned nvar;
3550 if (!poly)
3551 return -2;
3553 ovar = isl_space_offset(poly->dim, isl_dim_set);
3554 nvar = isl_space_dim(poly->dim, isl_dim_set);
3555 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3558 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3559 unsigned pos, int deg)
3561 int i;
3562 struct isl_upoly_rec *rec;
3564 if (!up)
3565 return NULL;
3567 if (isl_upoly_is_cst(up) || up->var < pos) {
3568 if (deg == 0)
3569 return isl_upoly_copy(up);
3570 else
3571 return isl_upoly_zero(up->ctx);
3574 rec = isl_upoly_as_rec(up);
3575 if (!rec)
3576 return NULL;
3578 if (up->var == pos) {
3579 if (deg < rec->n)
3580 return isl_upoly_copy(rec->p[deg]);
3581 else
3582 return isl_upoly_zero(up->ctx);
3585 up = isl_upoly_copy(up);
3586 up = isl_upoly_cow(up);
3587 rec = isl_upoly_as_rec(up);
3588 if (!rec)
3589 goto error;
3591 for (i = 0; i < rec->n; ++i) {
3592 struct isl_upoly *t;
3593 t = isl_upoly_coeff(rec->p[i], pos, deg);
3594 if (!t)
3595 goto error;
3596 isl_upoly_free(rec->p[i]);
3597 rec->p[i] = t;
3600 return up;
3601 error:
3602 isl_upoly_free(up);
3603 return NULL;
3606 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3608 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3609 __isl_keep isl_qpolynomial *qp,
3610 enum isl_dim_type type, unsigned t_pos, int deg)
3612 unsigned g_pos;
3613 struct isl_upoly *up;
3614 isl_qpolynomial *c;
3616 if (!qp)
3617 return NULL;
3619 if (type == isl_dim_out)
3620 isl_die(qp->div->ctx, isl_error_invalid,
3621 "output/set dimension does not have a coefficient",
3622 return NULL);
3623 if (type == isl_dim_in)
3624 type = isl_dim_set;
3626 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3627 return NULL);
3629 g_pos = pos(qp->dim, type) + t_pos;
3630 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3632 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3633 if (!c)
3634 return NULL;
3635 isl_mat_free(c->div);
3636 c->div = isl_mat_copy(qp->div);
3637 if (!c->div)
3638 goto error;
3639 return c;
3640 error:
3641 isl_qpolynomial_free(c);
3642 return NULL;
3645 /* Homogenize the polynomial in the variables first (inclusive) up to
3646 * last (exclusive) by inserting powers of variable first.
3647 * Variable first is assumed not to appear in the input.
3649 __isl_give struct isl_upoly *isl_upoly_homogenize(
3650 __isl_take struct isl_upoly *up, int deg, int target,
3651 int first, int last)
3653 int i;
3654 struct isl_upoly_rec *rec;
3656 if (!up)
3657 return NULL;
3658 if (isl_upoly_is_zero(up))
3659 return up;
3660 if (deg == target)
3661 return up;
3662 if (isl_upoly_is_cst(up) || up->var < first) {
3663 struct isl_upoly *hom;
3665 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3666 if (!hom)
3667 goto error;
3668 rec = isl_upoly_as_rec(hom);
3669 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3671 return hom;
3674 up = isl_upoly_cow(up);
3675 rec = isl_upoly_as_rec(up);
3676 if (!rec)
3677 goto error;
3679 for (i = 0; i < rec->n; ++i) {
3680 if (isl_upoly_is_zero(rec->p[i]))
3681 continue;
3682 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3683 up->var < last ? deg + i : i, target,
3684 first, last);
3685 if (!rec->p[i])
3686 goto error;
3689 return up;
3690 error:
3691 isl_upoly_free(up);
3692 return NULL;
3695 /* Homogenize the polynomial in the set variables by introducing
3696 * powers of an extra set variable at position 0.
3698 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3699 __isl_take isl_qpolynomial *poly)
3701 unsigned ovar;
3702 unsigned nvar;
3703 int deg = isl_qpolynomial_degree(poly);
3705 if (deg < -1)
3706 goto error;
3708 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3709 poly = isl_qpolynomial_cow(poly);
3710 if (!poly)
3711 goto error;
3713 ovar = isl_space_offset(poly->dim, isl_dim_set);
3714 nvar = isl_space_dim(poly->dim, isl_dim_set);
3715 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3716 ovar, ovar + nvar);
3717 if (!poly->upoly)
3718 goto error;
3720 return poly;
3721 error:
3722 isl_qpolynomial_free(poly);
3723 return NULL;
3726 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3727 __isl_take isl_mat *div)
3729 isl_term *term;
3730 int n;
3732 if (!dim || !div)
3733 goto error;
3735 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3737 term = isl_calloc(dim->ctx, struct isl_term,
3738 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3739 if (!term)
3740 goto error;
3742 term->ref = 1;
3743 term->dim = dim;
3744 term->div = div;
3745 isl_int_init(term->n);
3746 isl_int_init(term->d);
3748 return term;
3749 error:
3750 isl_space_free(dim);
3751 isl_mat_free(div);
3752 return NULL;
3755 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3757 if (!term)
3758 return NULL;
3760 term->ref++;
3761 return term;
3764 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3766 int i;
3767 isl_term *dup;
3768 unsigned total;
3770 if (!term)
3771 return NULL;
3773 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3775 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3776 if (!dup)
3777 return NULL;
3779 isl_int_set(dup->n, term->n);
3780 isl_int_set(dup->d, term->d);
3782 for (i = 0; i < total; ++i)
3783 dup->pow[i] = term->pow[i];
3785 return dup;
3788 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3790 if (!term)
3791 return NULL;
3793 if (term->ref == 1)
3794 return term;
3795 term->ref--;
3796 return isl_term_dup(term);
3799 void isl_term_free(__isl_take isl_term *term)
3801 if (!term)
3802 return;
3804 if (--term->ref > 0)
3805 return;
3807 isl_space_free(term->dim);
3808 isl_mat_free(term->div);
3809 isl_int_clear(term->n);
3810 isl_int_clear(term->d);
3811 free(term);
3814 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3816 if (!term)
3817 return 0;
3819 switch (type) {
3820 case isl_dim_param:
3821 case isl_dim_in:
3822 case isl_dim_out: return isl_space_dim(term->dim, type);
3823 case isl_dim_div: return term->div->n_row;
3824 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3825 term->div->n_row;
3826 default: return 0;
3830 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3832 return term ? term->dim->ctx : NULL;
3835 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3837 if (!term)
3838 return;
3839 isl_int_set(*n, term->n);
3842 /* Return the coefficient of the term "term".
3844 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3846 if (!term)
3847 return NULL;
3849 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3850 term->n, term->d);
3853 int isl_term_get_exp(__isl_keep isl_term *term,
3854 enum isl_dim_type type, unsigned pos)
3856 if (!term)
3857 return -1;
3859 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3861 if (type >= isl_dim_set)
3862 pos += isl_space_dim(term->dim, isl_dim_param);
3863 if (type >= isl_dim_div)
3864 pos += isl_space_dim(term->dim, isl_dim_set);
3866 return term->pow[pos];
3869 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3871 isl_local_space *ls;
3872 isl_aff *aff;
3874 if (!term)
3875 return NULL;
3877 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3878 return NULL);
3880 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3881 isl_mat_copy(term->div));
3882 aff = isl_aff_alloc(ls);
3883 if (!aff)
3884 return NULL;
3886 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3888 aff = isl_aff_normalize(aff);
3890 return aff;
3893 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3894 isl_stat (*fn)(__isl_take isl_term *term, void *user),
3895 __isl_take isl_term *term, void *user)
3897 int i;
3898 struct isl_upoly_rec *rec;
3900 if (!up || !term)
3901 goto error;
3903 if (isl_upoly_is_zero(up))
3904 return term;
3906 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3907 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3908 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3910 if (isl_upoly_is_cst(up)) {
3911 struct isl_upoly_cst *cst;
3912 cst = isl_upoly_as_cst(up);
3913 if (!cst)
3914 goto error;
3915 term = isl_term_cow(term);
3916 if (!term)
3917 goto error;
3918 isl_int_set(term->n, cst->n);
3919 isl_int_set(term->d, cst->d);
3920 if (fn(isl_term_copy(term), user) < 0)
3921 goto error;
3922 return term;
3925 rec = isl_upoly_as_rec(up);
3926 if (!rec)
3927 goto error;
3929 for (i = 0; i < rec->n; ++i) {
3930 term = isl_term_cow(term);
3931 if (!term)
3932 goto error;
3933 term->pow[up->var] = i;
3934 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3935 if (!term)
3936 goto error;
3938 term->pow[up->var] = 0;
3940 return term;
3941 error:
3942 isl_term_free(term);
3943 return NULL;
3946 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3947 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
3949 isl_term *term;
3951 if (!qp)
3952 return isl_stat_error;
3954 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3955 if (!term)
3956 return isl_stat_error;
3958 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3960 isl_term_free(term);
3962 return term ? isl_stat_ok : isl_stat_error;
3965 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3967 struct isl_upoly *up;
3968 isl_qpolynomial *qp;
3969 int i, n;
3971 if (!term)
3972 return NULL;
3974 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3976 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3977 for (i = 0; i < n; ++i) {
3978 if (!term->pow[i])
3979 continue;
3980 up = isl_upoly_mul(up,
3981 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3984 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3985 if (!qp)
3986 goto error;
3987 isl_mat_free(qp->div);
3988 qp->div = isl_mat_copy(term->div);
3989 if (!qp->div)
3990 goto error;
3992 isl_term_free(term);
3993 return qp;
3994 error:
3995 isl_qpolynomial_free(qp);
3996 isl_term_free(term);
3997 return NULL;
4000 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4001 __isl_take isl_space *dim)
4003 int i;
4004 int extra;
4005 unsigned total;
4007 if (!qp || !dim)
4008 goto error;
4010 if (isl_space_is_equal(qp->dim, dim)) {
4011 isl_space_free(dim);
4012 return qp;
4015 qp = isl_qpolynomial_cow(qp);
4016 if (!qp)
4017 goto error;
4019 extra = isl_space_dim(dim, isl_dim_set) -
4020 isl_space_dim(qp->dim, isl_dim_set);
4021 total = isl_space_dim(qp->dim, isl_dim_all);
4022 if (qp->div->n_row) {
4023 int *exp;
4025 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4026 if (!exp)
4027 goto error;
4028 for (i = 0; i < qp->div->n_row; ++i)
4029 exp[i] = extra + i;
4030 qp->upoly = expand(qp->upoly, exp, total);
4031 free(exp);
4032 if (!qp->upoly)
4033 goto error;
4035 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4036 if (!qp->div)
4037 goto error;
4038 for (i = 0; i < qp->div->n_row; ++i)
4039 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4041 isl_space_free(qp->dim);
4042 qp->dim = dim;
4044 return qp;
4045 error:
4046 isl_space_free(dim);
4047 isl_qpolynomial_free(qp);
4048 return NULL;
4051 /* For each parameter or variable that does not appear in qp,
4052 * first eliminate the variable from all constraints and then set it to zero.
4054 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4055 __isl_keep isl_qpolynomial *qp)
4057 int *active = NULL;
4058 int i;
4059 int d;
4060 unsigned nparam;
4061 unsigned nvar;
4063 if (!set || !qp)
4064 goto error;
4066 d = isl_space_dim(set->dim, isl_dim_all);
4067 active = isl_calloc_array(set->ctx, int, d);
4068 if (set_active(qp, active) < 0)
4069 goto error;
4071 for (i = 0; i < d; ++i)
4072 if (!active[i])
4073 break;
4075 if (i == d) {
4076 free(active);
4077 return set;
4080 nparam = isl_space_dim(set->dim, isl_dim_param);
4081 nvar = isl_space_dim(set->dim, isl_dim_set);
4082 for (i = 0; i < nparam; ++i) {
4083 if (active[i])
4084 continue;
4085 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4086 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4088 for (i = 0; i < nvar; ++i) {
4089 if (active[nparam + i])
4090 continue;
4091 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4092 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4095 free(active);
4097 return set;
4098 error:
4099 free(active);
4100 isl_set_free(set);
4101 return NULL;
4104 struct isl_opt_data {
4105 isl_qpolynomial *qp;
4106 int first;
4107 isl_val *opt;
4108 int max;
4111 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4113 struct isl_opt_data *data = (struct isl_opt_data *)user;
4114 isl_val *val;
4116 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4117 if (data->first) {
4118 data->first = 0;
4119 data->opt = val;
4120 } else if (data->max) {
4121 data->opt = isl_val_max(data->opt, val);
4122 } else {
4123 data->opt = isl_val_min(data->opt, val);
4126 return isl_stat_ok;
4129 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4130 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4132 struct isl_opt_data data = { NULL, 1, NULL, max };
4134 if (!set || !qp)
4135 goto error;
4137 if (isl_upoly_is_cst(qp->upoly)) {
4138 isl_set_free(set);
4139 data.opt = isl_qpolynomial_get_constant_val(qp);
4140 isl_qpolynomial_free(qp);
4141 return data.opt;
4144 set = fix_inactive(set, qp);
4146 data.qp = qp;
4147 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4148 goto error;
4150 if (data.first)
4151 data.opt = isl_val_zero(isl_set_get_ctx(set));
4153 isl_set_free(set);
4154 isl_qpolynomial_free(qp);
4155 return data.opt;
4156 error:
4157 isl_set_free(set);
4158 isl_qpolynomial_free(qp);
4159 isl_val_free(data.opt);
4160 return NULL;
4163 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4164 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4166 int i;
4167 int n_sub;
4168 isl_ctx *ctx;
4169 struct isl_upoly **subs;
4170 isl_mat *mat, *diag;
4172 qp = isl_qpolynomial_cow(qp);
4173 if (!qp || !morph)
4174 goto error;
4176 ctx = qp->dim->ctx;
4177 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4179 n_sub = morph->inv->n_row - 1;
4180 if (morph->inv->n_row != morph->inv->n_col)
4181 n_sub += qp->div->n_row;
4182 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4183 if (n_sub && !subs)
4184 goto error;
4186 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4187 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4188 morph->inv->row[0][0], morph->inv->n_col);
4189 if (morph->inv->n_row != morph->inv->n_col)
4190 for (i = 0; i < qp->div->n_row; ++i)
4191 subs[morph->inv->n_row - 1 + i] =
4192 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4194 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4196 for (i = 0; i < n_sub; ++i)
4197 isl_upoly_free(subs[i]);
4198 free(subs);
4200 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4201 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4202 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4203 mat = isl_mat_diagonal(mat, diag);
4204 qp->div = isl_mat_product(qp->div, mat);
4205 isl_space_free(qp->dim);
4206 qp->dim = isl_space_copy(morph->ran->dim);
4208 if (!qp->upoly || !qp->div || !qp->dim)
4209 goto error;
4211 isl_morph_free(morph);
4213 return qp;
4214 error:
4215 isl_qpolynomial_free(qp);
4216 isl_morph_free(morph);
4217 return NULL;
4220 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4221 __isl_take isl_union_pw_qpolynomial *upwqp1,
4222 __isl_take isl_union_pw_qpolynomial *upwqp2)
4224 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4225 &isl_pw_qpolynomial_mul);
4228 /* Reorder the dimension of "qp" according to the given reordering.
4230 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4231 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4233 isl_space *space;
4235 qp = isl_qpolynomial_cow(qp);
4236 if (!qp)
4237 goto error;
4239 r = isl_reordering_extend(r, qp->div->n_row);
4240 if (!r)
4241 goto error;
4243 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4244 if (!qp->div)
4245 goto error;
4247 qp->upoly = reorder(qp->upoly, r->pos);
4248 if (!qp->upoly)
4249 goto error;
4251 space = isl_reordering_get_space(r);
4252 qp = isl_qpolynomial_reset_domain_space(qp, space);
4254 isl_reordering_free(r);
4255 return qp;
4256 error:
4257 isl_qpolynomial_free(qp);
4258 isl_reordering_free(r);
4259 return NULL;
4262 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4263 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4265 isl_bool equal_params;
4267 if (!qp || !model)
4268 goto error;
4270 equal_params = isl_space_has_equal_params(qp->dim, model);
4271 if (equal_params < 0)
4272 goto error;
4273 if (!equal_params) {
4274 isl_reordering *exp;
4276 model = isl_space_drop_dims(model, isl_dim_in,
4277 0, isl_space_dim(model, isl_dim_in));
4278 model = isl_space_drop_dims(model, isl_dim_out,
4279 0, isl_space_dim(model, isl_dim_out));
4280 exp = isl_parameter_alignment_reordering(qp->dim, model);
4281 exp = isl_reordering_extend_space(exp,
4282 isl_qpolynomial_get_domain_space(qp));
4283 qp = isl_qpolynomial_realign_domain(qp, exp);
4286 isl_space_free(model);
4287 return qp;
4288 error:
4289 isl_space_free(model);
4290 isl_qpolynomial_free(qp);
4291 return NULL;
4294 struct isl_split_periods_data {
4295 int max_periods;
4296 isl_pw_qpolynomial *res;
4299 /* Create a slice where the integer division "div" has the fixed value "v".
4300 * In particular, if "div" refers to floor(f/m), then create a slice
4302 * m v <= f <= m v + (m - 1)
4304 * or
4306 * f - m v >= 0
4307 * -f + m v + (m - 1) >= 0
4309 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4310 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4312 int total;
4313 isl_basic_set *bset = NULL;
4314 int k;
4316 if (!dim || !qp)
4317 goto error;
4319 total = isl_space_dim(dim, isl_dim_all);
4320 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4322 k = isl_basic_set_alloc_inequality(bset);
4323 if (k < 0)
4324 goto error;
4325 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4326 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4328 k = isl_basic_set_alloc_inequality(bset);
4329 if (k < 0)
4330 goto error;
4331 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4332 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4333 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4334 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4336 isl_space_free(dim);
4337 return isl_set_from_basic_set(bset);
4338 error:
4339 isl_basic_set_free(bset);
4340 isl_space_free(dim);
4341 return NULL;
4344 static isl_stat split_periods(__isl_take isl_set *set,
4345 __isl_take isl_qpolynomial *qp, void *user);
4347 /* Create a slice of the domain "set" such that integer division "div"
4348 * has the fixed value "v" and add the results to data->res,
4349 * replacing the integer division by "v" in "qp".
4351 static isl_stat set_div(__isl_take isl_set *set,
4352 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4353 struct isl_split_periods_data *data)
4355 int i;
4356 int total;
4357 isl_set *slice;
4358 struct isl_upoly *cst;
4360 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4361 set = isl_set_intersect(set, slice);
4363 if (!qp)
4364 goto error;
4366 total = isl_space_dim(qp->dim, isl_dim_all);
4368 for (i = div + 1; i < qp->div->n_row; ++i) {
4369 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4370 continue;
4371 isl_int_addmul(qp->div->row[i][1],
4372 qp->div->row[i][2 + total + div], v);
4373 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4376 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4377 qp = substitute_div(qp, div, cst);
4379 return split_periods(set, qp, data);
4380 error:
4381 isl_set_free(set);
4382 isl_qpolynomial_free(qp);
4383 return -1;
4386 /* Split the domain "set" such that integer division "div"
4387 * has a fixed value (ranging from "min" to "max") on each slice
4388 * and add the results to data->res.
4390 static isl_stat split_div(__isl_take isl_set *set,
4391 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4392 struct isl_split_periods_data *data)
4394 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4395 isl_set *set_i = isl_set_copy(set);
4396 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4398 if (set_div(set_i, qp_i, div, min, data) < 0)
4399 goto error;
4401 isl_set_free(set);
4402 isl_qpolynomial_free(qp);
4403 return isl_stat_ok;
4404 error:
4405 isl_set_free(set);
4406 isl_qpolynomial_free(qp);
4407 return isl_stat_error;
4410 /* If "qp" refers to any integer division
4411 * that can only attain "max_periods" distinct values on "set"
4412 * then split the domain along those distinct values.
4413 * Add the results (or the original if no splitting occurs)
4414 * to data->res.
4416 static isl_stat split_periods(__isl_take isl_set *set,
4417 __isl_take isl_qpolynomial *qp, void *user)
4419 int i;
4420 isl_pw_qpolynomial *pwqp;
4421 struct isl_split_periods_data *data;
4422 isl_int min, max;
4423 int total;
4424 isl_stat r = isl_stat_ok;
4426 data = (struct isl_split_periods_data *)user;
4428 if (!set || !qp)
4429 goto error;
4431 if (qp->div->n_row == 0) {
4432 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4433 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4434 return isl_stat_ok;
4437 isl_int_init(min);
4438 isl_int_init(max);
4439 total = isl_space_dim(qp->dim, isl_dim_all);
4440 for (i = 0; i < qp->div->n_row; ++i) {
4441 enum isl_lp_result lp_res;
4443 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4444 qp->div->n_row) != -1)
4445 continue;
4447 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4448 set->ctx->one, &min, NULL, NULL);
4449 if (lp_res == isl_lp_error)
4450 goto error2;
4451 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4452 continue;
4453 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4455 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4456 set->ctx->one, &max, NULL, NULL);
4457 if (lp_res == isl_lp_error)
4458 goto error2;
4459 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4460 continue;
4461 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4463 isl_int_sub(max, max, min);
4464 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4465 isl_int_add(max, max, min);
4466 break;
4470 if (i < qp->div->n_row) {
4471 r = split_div(set, qp, i, min, max, data);
4472 } else {
4473 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4474 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4477 isl_int_clear(max);
4478 isl_int_clear(min);
4480 return r;
4481 error2:
4482 isl_int_clear(max);
4483 isl_int_clear(min);
4484 error:
4485 isl_set_free(set);
4486 isl_qpolynomial_free(qp);
4487 return isl_stat_error;
4490 /* If any quasi-polynomial in pwqp refers to any integer division
4491 * that can only attain "max_periods" distinct values on its domain
4492 * then split the domain along those distinct values.
4494 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4495 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4497 struct isl_split_periods_data data;
4499 data.max_periods = max_periods;
4500 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4502 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4503 goto error;
4505 isl_pw_qpolynomial_free(pwqp);
4507 return data.res;
4508 error:
4509 isl_pw_qpolynomial_free(data.res);
4510 isl_pw_qpolynomial_free(pwqp);
4511 return NULL;
4514 /* Construct a piecewise quasipolynomial that is constant on the given
4515 * domain. In particular, it is
4516 * 0 if cst == 0
4517 * 1 if cst == 1
4518 * infinity if cst == -1
4520 * If cst == -1, then explicitly check whether the domain is empty and,
4521 * if so, return 0 instead.
4523 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4524 __isl_take isl_basic_set *bset, int cst)
4526 isl_space *dim;
4527 isl_qpolynomial *qp;
4529 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4530 cst = 0;
4531 if (!bset)
4532 return NULL;
4534 bset = isl_basic_set_params(bset);
4535 dim = isl_basic_set_get_space(bset);
4536 if (cst < 0)
4537 qp = isl_qpolynomial_infty_on_domain(dim);
4538 else if (cst == 0)
4539 qp = isl_qpolynomial_zero_on_domain(dim);
4540 else
4541 qp = isl_qpolynomial_one_on_domain(dim);
4542 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4545 /* Factor bset, call fn on each of the factors and return the product.
4547 * If no factors can be found, simply call fn on the input.
4548 * Otherwise, construct the factors based on the factorizer,
4549 * call fn on each factor and compute the product.
4551 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4552 __isl_take isl_basic_set *bset,
4553 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4555 int i, n;
4556 isl_space *space;
4557 isl_set *set;
4558 isl_factorizer *f;
4559 isl_qpolynomial *qp;
4560 isl_pw_qpolynomial *pwqp;
4561 unsigned nparam;
4562 unsigned nvar;
4564 f = isl_basic_set_factorizer(bset);
4565 if (!f)
4566 goto error;
4567 if (f->n_group == 0) {
4568 isl_factorizer_free(f);
4569 return fn(bset);
4572 nparam = isl_basic_set_dim(bset, isl_dim_param);
4573 nvar = isl_basic_set_dim(bset, isl_dim_set);
4575 space = isl_basic_set_get_space(bset);
4576 space = isl_space_params(space);
4577 set = isl_set_universe(isl_space_copy(space));
4578 qp = isl_qpolynomial_one_on_domain(space);
4579 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4581 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4583 for (i = 0, n = 0; i < f->n_group; ++i) {
4584 isl_basic_set *bset_i;
4585 isl_pw_qpolynomial *pwqp_i;
4587 bset_i = isl_basic_set_copy(bset);
4588 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4589 nparam + n + f->len[i], nvar - n - f->len[i]);
4590 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4591 nparam, n);
4592 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4593 n + f->len[i], nvar - n - f->len[i]);
4594 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4596 pwqp_i = fn(bset_i);
4597 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4599 n += f->len[i];
4602 isl_basic_set_free(bset);
4603 isl_factorizer_free(f);
4605 return pwqp;
4606 error:
4607 isl_basic_set_free(bset);
4608 return NULL;
4611 /* Factor bset, call fn on each of the factors and return the product.
4612 * The function is assumed to evaluate to zero on empty domains,
4613 * to one on zero-dimensional domains and to infinity on unbounded domains
4614 * and will not be called explicitly on zero-dimensional or unbounded domains.
4616 * We first check for some special cases and remove all equalities.
4617 * Then we hand over control to compressed_multiplicative_call.
4619 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4620 __isl_take isl_basic_set *bset,
4621 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4623 isl_bool bounded;
4624 isl_morph *morph;
4625 isl_pw_qpolynomial *pwqp;
4627 if (!bset)
4628 return NULL;
4630 if (isl_basic_set_plain_is_empty(bset))
4631 return constant_on_domain(bset, 0);
4633 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4634 return constant_on_domain(bset, 1);
4636 bounded = isl_basic_set_is_bounded(bset);
4637 if (bounded < 0)
4638 goto error;
4639 if (!bounded)
4640 return constant_on_domain(bset, -1);
4642 if (bset->n_eq == 0)
4643 return compressed_multiplicative_call(bset, fn);
4645 morph = isl_basic_set_full_compression(bset);
4646 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4648 pwqp = compressed_multiplicative_call(bset, fn);
4650 morph = isl_morph_dom_params(morph);
4651 morph = isl_morph_ran_params(morph);
4652 morph = isl_morph_inverse(morph);
4654 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4656 return pwqp;
4657 error:
4658 isl_basic_set_free(bset);
4659 return NULL;
4662 /* Drop all floors in "qp", turning each integer division [a/m] into
4663 * a rational division a/m. If "down" is set, then the integer division
4664 * is replaced by (a-(m-1))/m instead.
4666 static __isl_give isl_qpolynomial *qp_drop_floors(
4667 __isl_take isl_qpolynomial *qp, int down)
4669 int i;
4670 struct isl_upoly *s;
4672 if (!qp)
4673 return NULL;
4674 if (qp->div->n_row == 0)
4675 return qp;
4677 qp = isl_qpolynomial_cow(qp);
4678 if (!qp)
4679 return NULL;
4681 for (i = qp->div->n_row - 1; i >= 0; --i) {
4682 if (down) {
4683 isl_int_sub(qp->div->row[i][1],
4684 qp->div->row[i][1], qp->div->row[i][0]);
4685 isl_int_add_ui(qp->div->row[i][1],
4686 qp->div->row[i][1], 1);
4688 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4689 qp->div->row[i][0], qp->div->n_col - 1);
4690 qp = substitute_div(qp, i, s);
4691 if (!qp)
4692 return NULL;
4695 return qp;
4698 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4699 * a rational division a/m.
4701 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4702 __isl_take isl_pw_qpolynomial *pwqp)
4704 int i;
4706 if (!pwqp)
4707 return NULL;
4709 if (isl_pw_qpolynomial_is_zero(pwqp))
4710 return pwqp;
4712 pwqp = isl_pw_qpolynomial_cow(pwqp);
4713 if (!pwqp)
4714 return NULL;
4716 for (i = 0; i < pwqp->n; ++i) {
4717 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4718 if (!pwqp->p[i].qp)
4719 goto error;
4722 return pwqp;
4723 error:
4724 isl_pw_qpolynomial_free(pwqp);
4725 return NULL;
4728 /* Adjust all the integer divisions in "qp" such that they are at least
4729 * one over the given orthant (identified by "signs"). This ensures
4730 * that they will still be non-negative even after subtracting (m-1)/m.
4732 * In particular, f is replaced by f' + v, changing f = [a/m]
4733 * to f' = [(a - m v)/m].
4734 * If the constant term k in a is smaller than m,
4735 * the constant term of v is set to floor(k/m) - 1.
4736 * For any other term, if the coefficient c and the variable x have
4737 * the same sign, then no changes are needed.
4738 * Otherwise, if the variable is positive (and c is negative),
4739 * then the coefficient of x in v is set to floor(c/m).
4740 * If the variable is negative (and c is positive),
4741 * then the coefficient of x in v is set to ceil(c/m).
4743 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4744 int *signs)
4746 int i, j;
4747 int total;
4748 isl_vec *v = NULL;
4749 struct isl_upoly *s;
4751 qp = isl_qpolynomial_cow(qp);
4752 if (!qp)
4753 return NULL;
4754 qp->div = isl_mat_cow(qp->div);
4755 if (!qp->div)
4756 goto error;
4758 total = isl_space_dim(qp->dim, isl_dim_all);
4759 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4761 for (i = 0; i < qp->div->n_row; ++i) {
4762 isl_int *row = qp->div->row[i];
4763 v = isl_vec_clr(v);
4764 if (!v)
4765 goto error;
4766 if (isl_int_lt(row[1], row[0])) {
4767 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4768 isl_int_sub_ui(v->el[0], v->el[0], 1);
4769 isl_int_submul(row[1], row[0], v->el[0]);
4771 for (j = 0; j < total; ++j) {
4772 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4773 continue;
4774 if (signs[j] < 0)
4775 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4776 else
4777 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4778 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4780 for (j = 0; j < i; ++j) {
4781 if (isl_int_sgn(row[2 + total + j]) >= 0)
4782 continue;
4783 isl_int_fdiv_q(v->el[1 + total + j],
4784 row[2 + total + j], row[0]);
4785 isl_int_submul(row[2 + total + j],
4786 row[0], v->el[1 + total + j]);
4788 for (j = i + 1; j < qp->div->n_row; ++j) {
4789 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4790 continue;
4791 isl_seq_combine(qp->div->row[j] + 1,
4792 qp->div->ctx->one, qp->div->row[j] + 1,
4793 qp->div->row[j][2 + total + i], v->el, v->size);
4795 isl_int_set_si(v->el[1 + total + i], 1);
4796 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4797 qp->div->ctx->one, v->size);
4798 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4799 isl_upoly_free(s);
4800 if (!qp->upoly)
4801 goto error;
4804 isl_vec_free(v);
4805 return qp;
4806 error:
4807 isl_vec_free(v);
4808 isl_qpolynomial_free(qp);
4809 return NULL;
4812 struct isl_to_poly_data {
4813 int sign;
4814 isl_pw_qpolynomial *res;
4815 isl_qpolynomial *qp;
4818 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4819 * We first make all integer divisions positive and then split the
4820 * quasipolynomials into terms with sign data->sign (the direction
4821 * of the requested approximation) and terms with the opposite sign.
4822 * In the first set of terms, each integer division [a/m] is
4823 * overapproximated by a/m, while in the second it is underapproximated
4824 * by (a-(m-1))/m.
4826 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
4827 int *signs, void *user)
4829 struct isl_to_poly_data *data = user;
4830 isl_pw_qpolynomial *t;
4831 isl_qpolynomial *qp, *up, *down;
4833 qp = isl_qpolynomial_copy(data->qp);
4834 qp = make_divs_pos(qp, signs);
4836 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4837 up = qp_drop_floors(up, 0);
4838 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4839 down = qp_drop_floors(down, 1);
4841 isl_qpolynomial_free(qp);
4842 qp = isl_qpolynomial_add(up, down);
4844 t = isl_pw_qpolynomial_alloc(orthant, qp);
4845 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4847 return isl_stat_ok;
4850 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4851 * the polynomial will be an overapproximation. If "sign" is negative,
4852 * it will be an underapproximation. If "sign" is zero, the approximation
4853 * will lie somewhere in between.
4855 * In particular, is sign == 0, we simply drop the floors, turning
4856 * the integer divisions into rational divisions.
4857 * Otherwise, we split the domains into orthants, make all integer divisions
4858 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4859 * depending on the requested sign and the sign of the term in which
4860 * the integer division appears.
4862 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4863 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4865 int i;
4866 struct isl_to_poly_data data;
4868 if (sign == 0)
4869 return pwqp_drop_floors(pwqp);
4871 if (!pwqp)
4872 return NULL;
4874 data.sign = sign;
4875 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4877 for (i = 0; i < pwqp->n; ++i) {
4878 if (pwqp->p[i].qp->div->n_row == 0) {
4879 isl_pw_qpolynomial *t;
4880 t = isl_pw_qpolynomial_alloc(
4881 isl_set_copy(pwqp->p[i].set),
4882 isl_qpolynomial_copy(pwqp->p[i].qp));
4883 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4884 continue;
4886 data.qp = pwqp->p[i].qp;
4887 if (isl_set_foreach_orthant(pwqp->p[i].set,
4888 &to_polynomial_on_orthant, &data) < 0)
4889 goto error;
4892 isl_pw_qpolynomial_free(pwqp);
4894 return data.res;
4895 error:
4896 isl_pw_qpolynomial_free(pwqp);
4897 isl_pw_qpolynomial_free(data.res);
4898 return NULL;
4901 static __isl_give isl_pw_qpolynomial *poly_entry(
4902 __isl_take isl_pw_qpolynomial *pwqp, void *user)
4904 int *sign = user;
4906 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
4909 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4910 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4912 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
4913 &poly_entry, &sign);
4916 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4917 __isl_take isl_qpolynomial *qp)
4919 int i, k;
4920 isl_space *dim;
4921 isl_vec *aff = NULL;
4922 isl_basic_map *bmap = NULL;
4923 unsigned pos;
4924 unsigned n_div;
4926 if (!qp)
4927 return NULL;
4928 if (!isl_upoly_is_affine(qp->upoly))
4929 isl_die(qp->dim->ctx, isl_error_invalid,
4930 "input quasi-polynomial not affine", goto error);
4931 aff = isl_qpolynomial_extract_affine(qp);
4932 if (!aff)
4933 goto error;
4934 dim = isl_qpolynomial_get_space(qp);
4935 pos = 1 + isl_space_offset(dim, isl_dim_out);
4936 n_div = qp->div->n_row;
4937 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4939 for (i = 0; i < n_div; ++i) {
4940 k = isl_basic_map_alloc_div(bmap);
4941 if (k < 0)
4942 goto error;
4943 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4944 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4945 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4946 goto error;
4948 k = isl_basic_map_alloc_equality(bmap);
4949 if (k < 0)
4950 goto error;
4951 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4952 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4953 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4955 isl_vec_free(aff);
4956 isl_qpolynomial_free(qp);
4957 bmap = isl_basic_map_finalize(bmap);
4958 return bmap;
4959 error:
4960 isl_vec_free(aff);
4961 isl_qpolynomial_free(qp);
4962 isl_basic_map_free(bmap);
4963 return NULL;