isl_output.c: print_nested_tuple: rename "local_dim" argument to "local_space"
[isl.git] / isl_polynomial.c
blob46b6ffb856839bf6ba9f22352a886019b86637c8
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <stdlib.h>
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
16 #include <isl_seq.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
31 #undef BASE
32 #define BASE pw_qpolynomial
34 #include <isl_list_templ.c>
36 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
38 switch (type) {
39 case isl_dim_param: return 0;
40 case isl_dim_in: return dim->nparam;
41 case isl_dim_out: return dim->nparam + dim->n_in;
42 default: return 0;
46 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
48 if (!up)
49 return -1;
51 return up->var < 0;
54 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
56 if (!up)
57 return NULL;
59 isl_assert(up->ctx, up->var < 0, return NULL);
61 return (struct isl_upoly_cst *)up;
64 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
66 if (!up)
67 return NULL;
69 isl_assert(up->ctx, up->var >= 0, return NULL);
71 return (struct isl_upoly_rec *)up;
74 /* Compare two polynomials.
76 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
77 * than "up2" and 0 if they are equal.
79 static int isl_upoly_plain_cmp(__isl_keep struct isl_upoly *up1,
80 __isl_keep struct isl_upoly *up2)
82 int i;
83 struct isl_upoly_rec *rec1, *rec2;
85 if (up1 == up2)
86 return 0;
87 if (!up1)
88 return -1;
89 if (!up2)
90 return 1;
91 if (up1->var != up2->var)
92 return up1->var - up2->var;
94 if (isl_upoly_is_cst(up1)) {
95 struct isl_upoly_cst *cst1, *cst2;
96 int cmp;
98 cst1 = isl_upoly_as_cst(up1);
99 cst2 = isl_upoly_as_cst(up2);
100 if (!cst1 || !cst2)
101 return 0;
102 cmp = isl_int_cmp(cst1->n, cst2->n);
103 if (cmp != 0)
104 return cmp;
105 return isl_int_cmp(cst1->d, cst2->d);
108 rec1 = isl_upoly_as_rec(up1);
109 rec2 = isl_upoly_as_rec(up2);
110 if (!rec1 || !rec2)
111 return 0;
113 if (rec1->n != rec2->n)
114 return rec1->n - rec2->n;
116 for (i = 0; i < rec1->n; ++i) {
117 int cmp = isl_upoly_plain_cmp(rec1->p[i], rec2->p[i]);
118 if (cmp != 0)
119 return cmp;
122 return 0;
125 isl_bool isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
126 __isl_keep struct isl_upoly *up2)
128 int i;
129 struct isl_upoly_rec *rec1, *rec2;
131 if (!up1 || !up2)
132 return isl_bool_error;
133 if (up1 == up2)
134 return isl_bool_true;
135 if (up1->var != up2->var)
136 return isl_bool_false;
137 if (isl_upoly_is_cst(up1)) {
138 struct isl_upoly_cst *cst1, *cst2;
139 cst1 = isl_upoly_as_cst(up1);
140 cst2 = isl_upoly_as_cst(up2);
141 if (!cst1 || !cst2)
142 return isl_bool_error;
143 return isl_int_eq(cst1->n, cst2->n) &&
144 isl_int_eq(cst1->d, cst2->d);
147 rec1 = isl_upoly_as_rec(up1);
148 rec2 = isl_upoly_as_rec(up2);
149 if (!rec1 || !rec2)
150 return isl_bool_error;
152 if (rec1->n != rec2->n)
153 return isl_bool_false;
155 for (i = 0; i < rec1->n; ++i) {
156 isl_bool eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
157 if (eq < 0 || !eq)
158 return eq;
161 return isl_bool_true;
164 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
166 struct isl_upoly_cst *cst;
168 if (!up)
169 return -1;
170 if (!isl_upoly_is_cst(up))
171 return 0;
173 cst = isl_upoly_as_cst(up);
174 if (!cst)
175 return -1;
177 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
180 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
182 struct isl_upoly_cst *cst;
184 if (!up)
185 return 0;
186 if (!isl_upoly_is_cst(up))
187 return 0;
189 cst = isl_upoly_as_cst(up);
190 if (!cst)
191 return 0;
193 return isl_int_sgn(cst->n);
196 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
198 struct isl_upoly_cst *cst;
200 if (!up)
201 return -1;
202 if (!isl_upoly_is_cst(up))
203 return 0;
205 cst = isl_upoly_as_cst(up);
206 if (!cst)
207 return -1;
209 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
212 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
214 struct isl_upoly_cst *cst;
216 if (!up)
217 return -1;
218 if (!isl_upoly_is_cst(up))
219 return 0;
221 cst = isl_upoly_as_cst(up);
222 if (!cst)
223 return -1;
225 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
228 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
230 struct isl_upoly_cst *cst;
232 if (!up)
233 return -1;
234 if (!isl_upoly_is_cst(up))
235 return 0;
237 cst = isl_upoly_as_cst(up);
238 if (!cst)
239 return -1;
241 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
244 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
246 struct isl_upoly_cst *cst;
248 if (!up)
249 return -1;
250 if (!isl_upoly_is_cst(up))
251 return 0;
253 cst = isl_upoly_as_cst(up);
254 if (!cst)
255 return -1;
257 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
260 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
262 struct isl_upoly_cst *cst;
264 if (!up)
265 return -1;
266 if (!isl_upoly_is_cst(up))
267 return 0;
269 cst = isl_upoly_as_cst(up);
270 if (!cst)
271 return -1;
273 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
276 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
278 struct isl_upoly_cst *cst;
280 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
281 if (!cst)
282 return NULL;
284 cst->up.ref = 1;
285 cst->up.ctx = ctx;
286 isl_ctx_ref(ctx);
287 cst->up.var = -1;
289 isl_int_init(cst->n);
290 isl_int_init(cst->d);
292 return cst;
295 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
297 struct isl_upoly_cst *cst;
299 cst = isl_upoly_cst_alloc(ctx);
300 if (!cst)
301 return NULL;
303 isl_int_set_si(cst->n, 0);
304 isl_int_set_si(cst->d, 1);
306 return &cst->up;
309 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
311 struct isl_upoly_cst *cst;
313 cst = isl_upoly_cst_alloc(ctx);
314 if (!cst)
315 return NULL;
317 isl_int_set_si(cst->n, 1);
318 isl_int_set_si(cst->d, 1);
320 return &cst->up;
323 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
325 struct isl_upoly_cst *cst;
327 cst = isl_upoly_cst_alloc(ctx);
328 if (!cst)
329 return NULL;
331 isl_int_set_si(cst->n, 1);
332 isl_int_set_si(cst->d, 0);
334 return &cst->up;
337 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
339 struct isl_upoly_cst *cst;
341 cst = isl_upoly_cst_alloc(ctx);
342 if (!cst)
343 return NULL;
345 isl_int_set_si(cst->n, -1);
346 isl_int_set_si(cst->d, 0);
348 return &cst->up;
351 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
353 struct isl_upoly_cst *cst;
355 cst = isl_upoly_cst_alloc(ctx);
356 if (!cst)
357 return NULL;
359 isl_int_set_si(cst->n, 0);
360 isl_int_set_si(cst->d, 0);
362 return &cst->up;
365 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
366 isl_int n, isl_int d)
368 struct isl_upoly_cst *cst;
370 cst = isl_upoly_cst_alloc(ctx);
371 if (!cst)
372 return NULL;
374 isl_int_set(cst->n, n);
375 isl_int_set(cst->d, d);
377 return &cst->up;
380 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
381 int var, int size)
383 struct isl_upoly_rec *rec;
385 isl_assert(ctx, var >= 0, return NULL);
386 isl_assert(ctx, size >= 0, return NULL);
387 rec = isl_calloc(ctx, struct isl_upoly_rec,
388 sizeof(struct isl_upoly_rec) +
389 size * sizeof(struct isl_upoly *));
390 if (!rec)
391 return NULL;
393 rec->up.ref = 1;
394 rec->up.ctx = ctx;
395 isl_ctx_ref(ctx);
396 rec->up.var = var;
398 rec->n = 0;
399 rec->size = size;
401 return rec;
404 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
405 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
407 qp = isl_qpolynomial_cow(qp);
408 if (!qp || !dim)
409 goto error;
411 isl_space_free(qp->dim);
412 qp->dim = dim;
414 return qp;
415 error:
416 isl_qpolynomial_free(qp);
417 isl_space_free(dim);
418 return NULL;
421 /* Reset the space of "qp". This function is called from isl_pw_templ.c
422 * and doesn't know if the space of an element object is represented
423 * directly or through its domain. It therefore passes along both.
425 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
426 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
427 __isl_take isl_space *domain)
429 isl_space_free(space);
430 return isl_qpolynomial_reset_domain_space(qp, domain);
433 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
435 return qp ? qp->dim->ctx : NULL;
438 __isl_give isl_space *isl_qpolynomial_get_domain_space(
439 __isl_keep isl_qpolynomial *qp)
441 return qp ? isl_space_copy(qp->dim) : NULL;
444 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
446 isl_space *space;
447 if (!qp)
448 return NULL;
449 space = isl_space_copy(qp->dim);
450 space = isl_space_from_domain(space);
451 space = isl_space_add_dims(space, isl_dim_out, 1);
452 return space;
455 /* Return the number of variables of the given type in the domain of "qp".
457 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
458 enum isl_dim_type type)
460 if (!qp)
461 return 0;
462 if (type == isl_dim_div)
463 return qp->div->n_row;
464 if (type == isl_dim_all)
465 return isl_space_dim(qp->dim, isl_dim_all) +
466 isl_qpolynomial_domain_dim(qp, isl_dim_div);
467 return isl_space_dim(qp->dim, type);
470 /* Externally, an isl_qpolynomial has a map space, but internally, the
471 * ls field corresponds to the domain of that space.
473 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
474 enum isl_dim_type type)
476 if (!qp)
477 return 0;
478 if (type == isl_dim_out)
479 return 1;
480 if (type == isl_dim_in)
481 type = isl_dim_set;
482 return isl_qpolynomial_domain_dim(qp, type);
485 /* Return the offset of the first coefficient of type "type" in
486 * the domain of "qp".
488 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
489 enum isl_dim_type type)
491 if (!qp)
492 return 0;
493 switch (type) {
494 case isl_dim_cst:
495 return 0;
496 case isl_dim_param:
497 case isl_dim_set:
498 return 1 + isl_space_offset(qp->dim, type);
499 case isl_dim_div:
500 return 1 + isl_space_dim(qp->dim, isl_dim_all);
501 default:
502 return 0;
506 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
508 return qp ? isl_upoly_is_zero(qp->upoly) : isl_bool_error;
511 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
513 return qp ? isl_upoly_is_one(qp->upoly) : isl_bool_error;
516 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
518 return qp ? isl_upoly_is_nan(qp->upoly) : isl_bool_error;
521 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
523 return qp ? isl_upoly_is_infty(qp->upoly) : isl_bool_error;
526 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
528 return qp ? isl_upoly_is_neginfty(qp->upoly) : isl_bool_error;
531 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
533 return qp ? isl_upoly_sgn(qp->upoly) : 0;
536 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
538 isl_int_clear(cst->n);
539 isl_int_clear(cst->d);
542 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
544 int i;
546 for (i = 0; i < rec->n; ++i)
547 isl_upoly_free(rec->p[i]);
550 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
552 if (!up)
553 return NULL;
555 up->ref++;
556 return up;
559 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
561 struct isl_upoly_cst *cst;
562 struct isl_upoly_cst *dup;
564 cst = isl_upoly_as_cst(up);
565 if (!cst)
566 return NULL;
568 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
569 if (!dup)
570 return NULL;
571 isl_int_set(dup->n, cst->n);
572 isl_int_set(dup->d, cst->d);
574 return &dup->up;
577 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
579 int i;
580 struct isl_upoly_rec *rec;
581 struct isl_upoly_rec *dup;
583 rec = isl_upoly_as_rec(up);
584 if (!rec)
585 return NULL;
587 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
588 if (!dup)
589 return NULL;
591 for (i = 0; i < rec->n; ++i) {
592 dup->p[i] = isl_upoly_copy(rec->p[i]);
593 if (!dup->p[i])
594 goto error;
595 dup->n++;
598 return &dup->up;
599 error:
600 isl_upoly_free(&dup->up);
601 return NULL;
604 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
606 if (!up)
607 return NULL;
609 if (isl_upoly_is_cst(up))
610 return isl_upoly_dup_cst(up);
611 else
612 return isl_upoly_dup_rec(up);
615 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
617 if (!up)
618 return NULL;
620 if (up->ref == 1)
621 return up;
622 up->ref--;
623 return isl_upoly_dup(up);
626 __isl_null struct isl_upoly *isl_upoly_free(__isl_take struct isl_upoly *up)
628 if (!up)
629 return NULL;
631 if (--up->ref > 0)
632 return NULL;
634 if (up->var < 0)
635 upoly_free_cst((struct isl_upoly_cst *)up);
636 else
637 upoly_free_rec((struct isl_upoly_rec *)up);
639 isl_ctx_deref(up->ctx);
640 free(up);
641 return NULL;
644 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
646 isl_int gcd;
648 isl_int_init(gcd);
649 isl_int_gcd(gcd, cst->n, cst->d);
650 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
651 isl_int_divexact(cst->n, cst->n, gcd);
652 isl_int_divexact(cst->d, cst->d, gcd);
654 isl_int_clear(gcd);
657 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
658 __isl_take struct isl_upoly *up2)
660 struct isl_upoly_cst *cst1;
661 struct isl_upoly_cst *cst2;
663 up1 = isl_upoly_cow(up1);
664 if (!up1 || !up2)
665 goto error;
667 cst1 = isl_upoly_as_cst(up1);
668 cst2 = isl_upoly_as_cst(up2);
670 if (isl_int_eq(cst1->d, cst2->d))
671 isl_int_add(cst1->n, cst1->n, cst2->n);
672 else {
673 isl_int_mul(cst1->n, cst1->n, cst2->d);
674 isl_int_addmul(cst1->n, cst2->n, cst1->d);
675 isl_int_mul(cst1->d, cst1->d, cst2->d);
678 isl_upoly_cst_reduce(cst1);
680 isl_upoly_free(up2);
681 return up1;
682 error:
683 isl_upoly_free(up1);
684 isl_upoly_free(up2);
685 return NULL;
688 static __isl_give struct isl_upoly *replace_by_zero(
689 __isl_take struct isl_upoly *up)
691 struct isl_ctx *ctx;
693 if (!up)
694 return NULL;
695 ctx = up->ctx;
696 isl_upoly_free(up);
697 return isl_upoly_zero(ctx);
700 static __isl_give struct isl_upoly *replace_by_constant_term(
701 __isl_take struct isl_upoly *up)
703 struct isl_upoly_rec *rec;
704 struct isl_upoly *cst;
706 if (!up)
707 return NULL;
709 rec = isl_upoly_as_rec(up);
710 if (!rec)
711 goto error;
712 cst = isl_upoly_copy(rec->p[0]);
713 isl_upoly_free(up);
714 return cst;
715 error:
716 isl_upoly_free(up);
717 return NULL;
720 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
721 __isl_take struct isl_upoly *up2)
723 int i;
724 struct isl_upoly_rec *rec1, *rec2;
726 if (!up1 || !up2)
727 goto error;
729 if (isl_upoly_is_nan(up1)) {
730 isl_upoly_free(up2);
731 return up1;
734 if (isl_upoly_is_nan(up2)) {
735 isl_upoly_free(up1);
736 return up2;
739 if (isl_upoly_is_zero(up1)) {
740 isl_upoly_free(up1);
741 return up2;
744 if (isl_upoly_is_zero(up2)) {
745 isl_upoly_free(up2);
746 return up1;
749 if (up1->var < up2->var)
750 return isl_upoly_sum(up2, up1);
752 if (up2->var < up1->var) {
753 struct isl_upoly_rec *rec;
754 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
755 isl_upoly_free(up1);
756 return up2;
758 up1 = isl_upoly_cow(up1);
759 rec = isl_upoly_as_rec(up1);
760 if (!rec)
761 goto error;
762 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
763 if (rec->n == 1)
764 up1 = replace_by_constant_term(up1);
765 return up1;
768 if (isl_upoly_is_cst(up1))
769 return isl_upoly_sum_cst(up1, up2);
771 rec1 = isl_upoly_as_rec(up1);
772 rec2 = isl_upoly_as_rec(up2);
773 if (!rec1 || !rec2)
774 goto error;
776 if (rec1->n < rec2->n)
777 return isl_upoly_sum(up2, up1);
779 up1 = isl_upoly_cow(up1);
780 rec1 = isl_upoly_as_rec(up1);
781 if (!rec1)
782 goto error;
784 for (i = rec2->n - 1; i >= 0; --i) {
785 rec1->p[i] = isl_upoly_sum(rec1->p[i],
786 isl_upoly_copy(rec2->p[i]));
787 if (!rec1->p[i])
788 goto error;
789 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
790 isl_upoly_free(rec1->p[i]);
791 rec1->n--;
795 if (rec1->n == 0)
796 up1 = replace_by_zero(up1);
797 else if (rec1->n == 1)
798 up1 = replace_by_constant_term(up1);
800 isl_upoly_free(up2);
802 return up1;
803 error:
804 isl_upoly_free(up1);
805 isl_upoly_free(up2);
806 return NULL;
809 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
810 __isl_take struct isl_upoly *up, isl_int v)
812 struct isl_upoly_cst *cst;
814 up = isl_upoly_cow(up);
815 if (!up)
816 return NULL;
818 cst = isl_upoly_as_cst(up);
820 isl_int_addmul(cst->n, cst->d, v);
822 return up;
825 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
826 __isl_take struct isl_upoly *up, isl_int v)
828 struct isl_upoly_rec *rec;
830 if (!up)
831 return NULL;
833 if (isl_upoly_is_cst(up))
834 return isl_upoly_cst_add_isl_int(up, v);
836 up = isl_upoly_cow(up);
837 rec = isl_upoly_as_rec(up);
838 if (!rec)
839 goto error;
841 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
842 if (!rec->p[0])
843 goto error;
845 return up;
846 error:
847 isl_upoly_free(up);
848 return NULL;
851 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
852 __isl_take struct isl_upoly *up, isl_int v)
854 struct isl_upoly_cst *cst;
856 if (isl_upoly_is_zero(up))
857 return up;
859 up = isl_upoly_cow(up);
860 if (!up)
861 return NULL;
863 cst = isl_upoly_as_cst(up);
865 isl_int_mul(cst->n, cst->n, v);
867 return up;
870 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
871 __isl_take struct isl_upoly *up, isl_int v)
873 int i;
874 struct isl_upoly_rec *rec;
876 if (!up)
877 return NULL;
879 if (isl_upoly_is_cst(up))
880 return isl_upoly_cst_mul_isl_int(up, v);
882 up = isl_upoly_cow(up);
883 rec = isl_upoly_as_rec(up);
884 if (!rec)
885 goto error;
887 for (i = 0; i < rec->n; ++i) {
888 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
889 if (!rec->p[i])
890 goto error;
893 return up;
894 error:
895 isl_upoly_free(up);
896 return NULL;
899 /* Multiply the constant polynomial "up" by "v".
901 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
902 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
904 struct isl_upoly_cst *cst;
906 if (isl_upoly_is_zero(up))
907 return up;
909 up = isl_upoly_cow(up);
910 if (!up)
911 return NULL;
913 cst = isl_upoly_as_cst(up);
915 isl_int_mul(cst->n, cst->n, v->n);
916 isl_int_mul(cst->d, cst->d, v->d);
917 isl_upoly_cst_reduce(cst);
919 return up;
922 /* Multiply the polynomial "up" by "v".
924 static __isl_give struct isl_upoly *isl_upoly_scale_val(
925 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
927 int i;
928 struct isl_upoly_rec *rec;
930 if (!up)
931 return NULL;
933 if (isl_upoly_is_cst(up))
934 return isl_upoly_cst_scale_val(up, v);
936 up = isl_upoly_cow(up);
937 rec = isl_upoly_as_rec(up);
938 if (!rec)
939 goto error;
941 for (i = 0; i < rec->n; ++i) {
942 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
943 if (!rec->p[i])
944 goto error;
947 return up;
948 error:
949 isl_upoly_free(up);
950 return NULL;
953 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
954 __isl_take struct isl_upoly *up2)
956 struct isl_upoly_cst *cst1;
957 struct isl_upoly_cst *cst2;
959 up1 = isl_upoly_cow(up1);
960 if (!up1 || !up2)
961 goto error;
963 cst1 = isl_upoly_as_cst(up1);
964 cst2 = isl_upoly_as_cst(up2);
966 isl_int_mul(cst1->n, cst1->n, cst2->n);
967 isl_int_mul(cst1->d, cst1->d, cst2->d);
969 isl_upoly_cst_reduce(cst1);
971 isl_upoly_free(up2);
972 return up1;
973 error:
974 isl_upoly_free(up1);
975 isl_upoly_free(up2);
976 return NULL;
979 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
980 __isl_take struct isl_upoly *up2)
982 struct isl_upoly_rec *rec1;
983 struct isl_upoly_rec *rec2;
984 struct isl_upoly_rec *res = NULL;
985 int i, j;
986 int size;
988 rec1 = isl_upoly_as_rec(up1);
989 rec2 = isl_upoly_as_rec(up2);
990 if (!rec1 || !rec2)
991 goto error;
992 size = rec1->n + rec2->n - 1;
993 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
994 if (!res)
995 goto error;
997 for (i = 0; i < rec1->n; ++i) {
998 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
999 isl_upoly_copy(rec1->p[i]));
1000 if (!res->p[i])
1001 goto error;
1002 res->n++;
1004 for (; i < size; ++i) {
1005 res->p[i] = isl_upoly_zero(up1->ctx);
1006 if (!res->p[i])
1007 goto error;
1008 res->n++;
1010 for (i = 0; i < rec1->n; ++i) {
1011 for (j = 1; j < rec2->n; ++j) {
1012 struct isl_upoly *up;
1013 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
1014 isl_upoly_copy(rec1->p[i]));
1015 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
1016 if (!res->p[i + j])
1017 goto error;
1021 isl_upoly_free(up1);
1022 isl_upoly_free(up2);
1024 return &res->up;
1025 error:
1026 isl_upoly_free(up1);
1027 isl_upoly_free(up2);
1028 isl_upoly_free(&res->up);
1029 return NULL;
1032 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
1033 __isl_take struct isl_upoly *up2)
1035 if (!up1 || !up2)
1036 goto error;
1038 if (isl_upoly_is_nan(up1)) {
1039 isl_upoly_free(up2);
1040 return up1;
1043 if (isl_upoly_is_nan(up2)) {
1044 isl_upoly_free(up1);
1045 return up2;
1048 if (isl_upoly_is_zero(up1)) {
1049 isl_upoly_free(up2);
1050 return up1;
1053 if (isl_upoly_is_zero(up2)) {
1054 isl_upoly_free(up1);
1055 return up2;
1058 if (isl_upoly_is_one(up1)) {
1059 isl_upoly_free(up1);
1060 return up2;
1063 if (isl_upoly_is_one(up2)) {
1064 isl_upoly_free(up2);
1065 return up1;
1068 if (up1->var < up2->var)
1069 return isl_upoly_mul(up2, up1);
1071 if (up2->var < up1->var) {
1072 int i;
1073 struct isl_upoly_rec *rec;
1074 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
1075 isl_ctx *ctx = up1->ctx;
1076 isl_upoly_free(up1);
1077 isl_upoly_free(up2);
1078 return isl_upoly_nan(ctx);
1080 up1 = isl_upoly_cow(up1);
1081 rec = isl_upoly_as_rec(up1);
1082 if (!rec)
1083 goto error;
1085 for (i = 0; i < rec->n; ++i) {
1086 rec->p[i] = isl_upoly_mul(rec->p[i],
1087 isl_upoly_copy(up2));
1088 if (!rec->p[i])
1089 goto error;
1091 isl_upoly_free(up2);
1092 return up1;
1095 if (isl_upoly_is_cst(up1))
1096 return isl_upoly_mul_cst(up1, up2);
1098 return isl_upoly_mul_rec(up1, up2);
1099 error:
1100 isl_upoly_free(up1);
1101 isl_upoly_free(up2);
1102 return NULL;
1105 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1106 unsigned power)
1108 struct isl_upoly *res;
1110 if (!up)
1111 return NULL;
1112 if (power == 1)
1113 return up;
1115 if (power % 2)
1116 res = isl_upoly_copy(up);
1117 else
1118 res = isl_upoly_one(up->ctx);
1120 while (power >>= 1) {
1121 up = isl_upoly_mul(up, isl_upoly_copy(up));
1122 if (power % 2)
1123 res = isl_upoly_mul(res, isl_upoly_copy(up));
1126 isl_upoly_free(up);
1127 return res;
1130 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
1131 unsigned n_div, __isl_take struct isl_upoly *up)
1133 struct isl_qpolynomial *qp = NULL;
1134 unsigned total;
1136 if (!dim || !up)
1137 goto error;
1139 if (!isl_space_is_set(dim))
1140 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1141 "domain of polynomial should be a set", goto error);
1143 total = isl_space_dim(dim, isl_dim_all);
1145 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
1146 if (!qp)
1147 goto error;
1149 qp->ref = 1;
1150 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
1151 if (!qp->div)
1152 goto error;
1154 qp->dim = dim;
1155 qp->upoly = up;
1157 return qp;
1158 error:
1159 isl_space_free(dim);
1160 isl_upoly_free(up);
1161 isl_qpolynomial_free(qp);
1162 return NULL;
1165 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1167 if (!qp)
1168 return NULL;
1170 qp->ref++;
1171 return qp;
1174 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1176 struct isl_qpolynomial *dup;
1178 if (!qp)
1179 return NULL;
1181 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1182 isl_upoly_copy(qp->upoly));
1183 if (!dup)
1184 return NULL;
1185 isl_mat_free(dup->div);
1186 dup->div = isl_mat_copy(qp->div);
1187 if (!dup->div)
1188 goto error;
1190 return dup;
1191 error:
1192 isl_qpolynomial_free(dup);
1193 return NULL;
1196 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1198 if (!qp)
1199 return NULL;
1201 if (qp->ref == 1)
1202 return qp;
1203 qp->ref--;
1204 return isl_qpolynomial_dup(qp);
1207 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1208 __isl_take isl_qpolynomial *qp)
1210 if (!qp)
1211 return NULL;
1213 if (--qp->ref > 0)
1214 return NULL;
1216 isl_space_free(qp->dim);
1217 isl_mat_free(qp->div);
1218 isl_upoly_free(qp->upoly);
1220 free(qp);
1221 return NULL;
1224 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1226 int i;
1227 struct isl_upoly_rec *rec;
1228 struct isl_upoly_cst *cst;
1230 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1231 if (!rec)
1232 return NULL;
1233 for (i = 0; i < 1 + power; ++i) {
1234 rec->p[i] = isl_upoly_zero(ctx);
1235 if (!rec->p[i])
1236 goto error;
1237 rec->n++;
1239 cst = isl_upoly_as_cst(rec->p[power]);
1240 isl_int_set_si(cst->n, 1);
1242 return &rec->up;
1243 error:
1244 isl_upoly_free(&rec->up);
1245 return NULL;
1248 /* r array maps original positions to new positions.
1250 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1251 int *r)
1253 int i;
1254 struct isl_upoly_rec *rec;
1255 struct isl_upoly *base;
1256 struct isl_upoly *res;
1258 if (isl_upoly_is_cst(up))
1259 return up;
1261 rec = isl_upoly_as_rec(up);
1262 if (!rec)
1263 goto error;
1265 isl_assert(up->ctx, rec->n >= 1, goto error);
1267 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1268 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1270 for (i = rec->n - 2; i >= 0; --i) {
1271 res = isl_upoly_mul(res, isl_upoly_copy(base));
1272 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1275 isl_upoly_free(base);
1276 isl_upoly_free(up);
1278 return res;
1279 error:
1280 isl_upoly_free(up);
1281 return NULL;
1284 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1285 __isl_keep isl_mat *div2)
1287 int n_row, n_col;
1288 isl_bool equal;
1290 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1291 div1->n_col >= div2->n_col,
1292 return isl_bool_error);
1294 if (div1->n_row == div2->n_row)
1295 return isl_mat_is_equal(div1, div2);
1297 n_row = div1->n_row;
1298 n_col = div1->n_col;
1299 div1->n_row = div2->n_row;
1300 div1->n_col = div2->n_col;
1302 equal = isl_mat_is_equal(div1, div2);
1304 div1->n_row = n_row;
1305 div1->n_col = n_col;
1307 return equal;
1310 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1312 int li, lj;
1314 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1315 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1317 if (li != lj)
1318 return li - lj;
1320 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1323 struct isl_div_sort_info {
1324 isl_mat *div;
1325 int row;
1328 static int div_sort_cmp(const void *p1, const void *p2)
1330 const struct isl_div_sort_info *i1, *i2;
1331 i1 = (const struct isl_div_sort_info *) p1;
1332 i2 = (const struct isl_div_sort_info *) p2;
1334 return cmp_row(i1->div, i1->row, i2->row);
1337 /* Sort divs and remove duplicates.
1339 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1341 int i;
1342 int skip;
1343 int len;
1344 struct isl_div_sort_info *array = NULL;
1345 int *pos = NULL, *at = NULL;
1346 int *reordering = NULL;
1347 unsigned div_pos;
1349 if (!qp)
1350 return NULL;
1351 if (qp->div->n_row <= 1)
1352 return qp;
1354 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1356 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1357 qp->div->n_row);
1358 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1359 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1360 len = qp->div->n_col - 2;
1361 reordering = isl_alloc_array(qp->div->ctx, int, len);
1362 if (!array || !pos || !at || !reordering)
1363 goto error;
1365 for (i = 0; i < qp->div->n_row; ++i) {
1366 array[i].div = qp->div;
1367 array[i].row = i;
1368 pos[i] = i;
1369 at[i] = i;
1372 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1373 div_sort_cmp);
1375 for (i = 0; i < div_pos; ++i)
1376 reordering[i] = i;
1378 for (i = 0; i < qp->div->n_row; ++i) {
1379 if (pos[array[i].row] == i)
1380 continue;
1381 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1382 pos[at[i]] = pos[array[i].row];
1383 at[pos[array[i].row]] = at[i];
1384 at[i] = array[i].row;
1385 pos[array[i].row] = i;
1388 skip = 0;
1389 for (i = 0; i < len - div_pos; ++i) {
1390 if (i > 0 &&
1391 isl_seq_eq(qp->div->row[i - skip - 1],
1392 qp->div->row[i - skip], qp->div->n_col)) {
1393 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1394 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1395 2 + div_pos + i - skip);
1396 qp->div = isl_mat_drop_cols(qp->div,
1397 2 + div_pos + i - skip, 1);
1398 skip++;
1400 reordering[div_pos + array[i].row] = div_pos + i - skip;
1403 qp->upoly = reorder(qp->upoly, reordering);
1405 if (!qp->upoly || !qp->div)
1406 goto error;
1408 free(at);
1409 free(pos);
1410 free(array);
1411 free(reordering);
1413 return qp;
1414 error:
1415 free(at);
1416 free(pos);
1417 free(array);
1418 free(reordering);
1419 isl_qpolynomial_free(qp);
1420 return NULL;
1423 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1424 int *exp, int first)
1426 int i;
1427 struct isl_upoly_rec *rec;
1429 if (isl_upoly_is_cst(up))
1430 return up;
1432 if (up->var < first)
1433 return up;
1435 if (exp[up->var - first] == up->var - first)
1436 return up;
1438 up = isl_upoly_cow(up);
1439 if (!up)
1440 goto error;
1442 up->var = exp[up->var - first] + first;
1444 rec = isl_upoly_as_rec(up);
1445 if (!rec)
1446 goto error;
1448 for (i = 0; i < rec->n; ++i) {
1449 rec->p[i] = expand(rec->p[i], exp, first);
1450 if (!rec->p[i])
1451 goto error;
1454 return up;
1455 error:
1456 isl_upoly_free(up);
1457 return NULL;
1460 static __isl_give isl_qpolynomial *with_merged_divs(
1461 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1462 __isl_take isl_qpolynomial *qp2),
1463 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1465 int *exp1 = NULL;
1466 int *exp2 = NULL;
1467 isl_mat *div = NULL;
1468 int n_div1, n_div2;
1470 qp1 = isl_qpolynomial_cow(qp1);
1471 qp2 = isl_qpolynomial_cow(qp2);
1473 if (!qp1 || !qp2)
1474 goto error;
1476 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1477 qp1->div->n_col >= qp2->div->n_col, goto error);
1479 n_div1 = qp1->div->n_row;
1480 n_div2 = qp2->div->n_row;
1481 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1482 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1483 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1484 goto error;
1486 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1487 if (!div)
1488 goto error;
1490 isl_mat_free(qp1->div);
1491 qp1->div = isl_mat_copy(div);
1492 isl_mat_free(qp2->div);
1493 qp2->div = isl_mat_copy(div);
1495 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1496 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1498 if (!qp1->upoly || !qp2->upoly)
1499 goto error;
1501 isl_mat_free(div);
1502 free(exp1);
1503 free(exp2);
1505 return fn(qp1, qp2);
1506 error:
1507 isl_mat_free(div);
1508 free(exp1);
1509 free(exp2);
1510 isl_qpolynomial_free(qp1);
1511 isl_qpolynomial_free(qp2);
1512 return NULL;
1515 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1516 __isl_take isl_qpolynomial *qp2)
1518 isl_bool compatible;
1520 qp1 = isl_qpolynomial_cow(qp1);
1522 if (!qp1 || !qp2)
1523 goto error;
1525 if (qp1->div->n_row < qp2->div->n_row)
1526 return isl_qpolynomial_add(qp2, qp1);
1528 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1529 compatible = compatible_divs(qp1->div, qp2->div);
1530 if (compatible < 0)
1531 goto error;
1532 if (!compatible)
1533 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1535 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1536 if (!qp1->upoly)
1537 goto error;
1539 isl_qpolynomial_free(qp2);
1541 return qp1;
1542 error:
1543 isl_qpolynomial_free(qp1);
1544 isl_qpolynomial_free(qp2);
1545 return NULL;
1548 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1549 __isl_keep isl_set *dom,
1550 __isl_take isl_qpolynomial *qp1,
1551 __isl_take isl_qpolynomial *qp2)
1553 qp1 = isl_qpolynomial_add(qp1, qp2);
1554 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1555 return qp1;
1558 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1559 __isl_take isl_qpolynomial *qp2)
1561 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1564 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1565 __isl_take isl_qpolynomial *qp, isl_int v)
1567 if (isl_int_is_zero(v))
1568 return qp;
1570 qp = isl_qpolynomial_cow(qp);
1571 if (!qp)
1572 return NULL;
1574 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1575 if (!qp->upoly)
1576 goto error;
1578 return qp;
1579 error:
1580 isl_qpolynomial_free(qp);
1581 return NULL;
1585 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1587 if (!qp)
1588 return NULL;
1590 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1593 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1594 __isl_take isl_qpolynomial *qp, isl_int v)
1596 if (isl_int_is_one(v))
1597 return qp;
1599 if (qp && isl_int_is_zero(v)) {
1600 isl_qpolynomial *zero;
1601 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1602 isl_qpolynomial_free(qp);
1603 return zero;
1606 qp = isl_qpolynomial_cow(qp);
1607 if (!qp)
1608 return NULL;
1610 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1611 if (!qp->upoly)
1612 goto error;
1614 return qp;
1615 error:
1616 isl_qpolynomial_free(qp);
1617 return NULL;
1620 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1621 __isl_take isl_qpolynomial *qp, isl_int v)
1623 return isl_qpolynomial_mul_isl_int(qp, v);
1626 /* Multiply "qp" by "v".
1628 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1629 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1631 if (!qp || !v)
1632 goto error;
1634 if (!isl_val_is_rat(v))
1635 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1636 "expecting rational factor", goto error);
1638 if (isl_val_is_one(v)) {
1639 isl_val_free(v);
1640 return qp;
1643 if (isl_val_is_zero(v)) {
1644 isl_space *space;
1646 space = isl_qpolynomial_get_domain_space(qp);
1647 isl_qpolynomial_free(qp);
1648 isl_val_free(v);
1649 return isl_qpolynomial_zero_on_domain(space);
1652 qp = isl_qpolynomial_cow(qp);
1653 if (!qp)
1654 goto error;
1656 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1657 if (!qp->upoly)
1658 qp = isl_qpolynomial_free(qp);
1660 isl_val_free(v);
1661 return qp;
1662 error:
1663 isl_val_free(v);
1664 isl_qpolynomial_free(qp);
1665 return NULL;
1668 /* Divide "qp" by "v".
1670 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1671 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1673 if (!qp || !v)
1674 goto error;
1676 if (!isl_val_is_rat(v))
1677 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1678 "expecting rational factor", goto error);
1679 if (isl_val_is_zero(v))
1680 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1681 "cannot scale down by zero", goto error);
1683 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1684 error:
1685 isl_val_free(v);
1686 isl_qpolynomial_free(qp);
1687 return NULL;
1690 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1691 __isl_take isl_qpolynomial *qp2)
1693 isl_bool compatible;
1695 qp1 = isl_qpolynomial_cow(qp1);
1697 if (!qp1 || !qp2)
1698 goto error;
1700 if (qp1->div->n_row < qp2->div->n_row)
1701 return isl_qpolynomial_mul(qp2, qp1);
1703 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1704 compatible = compatible_divs(qp1->div, qp2->div);
1705 if (compatible < 0)
1706 goto error;
1707 if (!compatible)
1708 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1710 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1711 if (!qp1->upoly)
1712 goto error;
1714 isl_qpolynomial_free(qp2);
1716 return qp1;
1717 error:
1718 isl_qpolynomial_free(qp1);
1719 isl_qpolynomial_free(qp2);
1720 return NULL;
1723 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1724 unsigned power)
1726 qp = isl_qpolynomial_cow(qp);
1728 if (!qp)
1729 return NULL;
1731 qp->upoly = isl_upoly_pow(qp->upoly, power);
1732 if (!qp->upoly)
1733 goto error;
1735 return qp;
1736 error:
1737 isl_qpolynomial_free(qp);
1738 return NULL;
1741 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1742 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1744 int i;
1746 if (power == 1)
1747 return pwqp;
1749 pwqp = isl_pw_qpolynomial_cow(pwqp);
1750 if (!pwqp)
1751 return NULL;
1753 for (i = 0; i < pwqp->n; ++i) {
1754 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1755 if (!pwqp->p[i].qp)
1756 return isl_pw_qpolynomial_free(pwqp);
1759 return pwqp;
1762 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1763 __isl_take isl_space *dim)
1765 if (!dim)
1766 return NULL;
1767 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1770 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1771 __isl_take isl_space *dim)
1773 if (!dim)
1774 return NULL;
1775 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1778 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1779 __isl_take isl_space *dim)
1781 if (!dim)
1782 return NULL;
1783 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1786 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1787 __isl_take isl_space *dim)
1789 if (!dim)
1790 return NULL;
1791 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1794 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1795 __isl_take isl_space *dim)
1797 if (!dim)
1798 return NULL;
1799 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1802 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1803 __isl_take isl_space *dim,
1804 isl_int v)
1806 struct isl_qpolynomial *qp;
1807 struct isl_upoly_cst *cst;
1809 if (!dim)
1810 return NULL;
1812 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1813 if (!qp)
1814 return NULL;
1816 cst = isl_upoly_as_cst(qp->upoly);
1817 isl_int_set(cst->n, v);
1819 return qp;
1822 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1823 isl_int *n, isl_int *d)
1825 struct isl_upoly_cst *cst;
1827 if (!qp)
1828 return -1;
1830 if (!isl_upoly_is_cst(qp->upoly))
1831 return 0;
1833 cst = isl_upoly_as_cst(qp->upoly);
1834 if (!cst)
1835 return -1;
1837 if (n)
1838 isl_int_set(*n, cst->n);
1839 if (d)
1840 isl_int_set(*d, cst->d);
1842 return 1;
1845 /* Return the constant term of "up".
1847 static __isl_give isl_val *isl_upoly_get_constant_val(
1848 __isl_keep struct isl_upoly *up)
1850 struct isl_upoly_cst *cst;
1852 if (!up)
1853 return NULL;
1855 while (!isl_upoly_is_cst(up)) {
1856 struct isl_upoly_rec *rec;
1858 rec = isl_upoly_as_rec(up);
1859 if (!rec)
1860 return NULL;
1861 up = rec->p[0];
1864 cst = isl_upoly_as_cst(up);
1865 if (!cst)
1866 return NULL;
1867 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1870 /* Return the constant term of "qp".
1872 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1873 __isl_keep isl_qpolynomial *qp)
1875 if (!qp)
1876 return NULL;
1878 return isl_upoly_get_constant_val(qp->upoly);
1881 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1883 int is_cst;
1884 struct isl_upoly_rec *rec;
1886 if (!up)
1887 return -1;
1889 if (up->var < 0)
1890 return 1;
1892 rec = isl_upoly_as_rec(up);
1893 if (!rec)
1894 return -1;
1896 if (rec->n > 2)
1897 return 0;
1899 isl_assert(up->ctx, rec->n > 1, return -1);
1901 is_cst = isl_upoly_is_cst(rec->p[1]);
1902 if (is_cst < 0)
1903 return -1;
1904 if (!is_cst)
1905 return 0;
1907 return isl_upoly_is_affine(rec->p[0]);
1910 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1912 if (!qp)
1913 return -1;
1915 if (qp->div->n_row > 0)
1916 return 0;
1918 return isl_upoly_is_affine(qp->upoly);
1921 static void update_coeff(__isl_keep isl_vec *aff,
1922 __isl_keep struct isl_upoly_cst *cst, int pos)
1924 isl_int gcd;
1925 isl_int f;
1927 if (isl_int_is_zero(cst->n))
1928 return;
1930 isl_int_init(gcd);
1931 isl_int_init(f);
1932 isl_int_gcd(gcd, cst->d, aff->el[0]);
1933 isl_int_divexact(f, cst->d, gcd);
1934 isl_int_divexact(gcd, aff->el[0], gcd);
1935 isl_seq_scale(aff->el, aff->el, f, aff->size);
1936 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1937 isl_int_clear(gcd);
1938 isl_int_clear(f);
1941 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1942 __isl_keep isl_vec *aff)
1944 struct isl_upoly_cst *cst;
1945 struct isl_upoly_rec *rec;
1947 if (!up || !aff)
1948 return -1;
1950 if (up->var < 0) {
1951 struct isl_upoly_cst *cst;
1953 cst = isl_upoly_as_cst(up);
1954 if (!cst)
1955 return -1;
1956 update_coeff(aff, cst, 0);
1957 return 0;
1960 rec = isl_upoly_as_rec(up);
1961 if (!rec)
1962 return -1;
1963 isl_assert(up->ctx, rec->n == 2, return -1);
1965 cst = isl_upoly_as_cst(rec->p[1]);
1966 if (!cst)
1967 return -1;
1968 update_coeff(aff, cst, 1 + up->var);
1970 return isl_upoly_update_affine(rec->p[0], aff);
1973 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1974 __isl_keep isl_qpolynomial *qp)
1976 isl_vec *aff;
1977 unsigned d;
1979 if (!qp)
1980 return NULL;
1982 d = isl_space_dim(qp->dim, isl_dim_all);
1983 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1984 if (!aff)
1985 return NULL;
1987 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1988 isl_int_set_si(aff->el[0], 1);
1990 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1991 goto error;
1993 return aff;
1994 error:
1995 isl_vec_free(aff);
1996 return NULL;
1999 /* Compare two quasi-polynomials.
2001 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2002 * than "qp2" and 0 if they are equal.
2004 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2005 __isl_keep isl_qpolynomial *qp2)
2007 int cmp;
2009 if (qp1 == qp2)
2010 return 0;
2011 if (!qp1)
2012 return -1;
2013 if (!qp2)
2014 return 1;
2016 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2017 if (cmp != 0)
2018 return cmp;
2020 cmp = isl_local_cmp(qp1->div, qp2->div);
2021 if (cmp != 0)
2022 return cmp;
2024 return isl_upoly_plain_cmp(qp1->upoly, qp2->upoly);
2027 /* Is "qp1" obviously equal to "qp2"?
2029 * NaN is not equal to anything, not even to another NaN.
2031 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2032 __isl_keep isl_qpolynomial *qp2)
2034 isl_bool equal;
2036 if (!qp1 || !qp2)
2037 return isl_bool_error;
2039 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2040 return isl_bool_false;
2042 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2043 if (equal < 0 || !equal)
2044 return equal;
2046 equal = isl_mat_is_equal(qp1->div, qp2->div);
2047 if (equal < 0 || !equal)
2048 return equal;
2050 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
2053 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
2055 int i;
2056 struct isl_upoly_rec *rec;
2058 if (isl_upoly_is_cst(up)) {
2059 struct isl_upoly_cst *cst;
2060 cst = isl_upoly_as_cst(up);
2061 if (!cst)
2062 return;
2063 isl_int_lcm(*d, *d, cst->d);
2064 return;
2067 rec = isl_upoly_as_rec(up);
2068 if (!rec)
2069 return;
2071 for (i = 0; i < rec->n; ++i)
2072 upoly_update_den(rec->p[i], d);
2075 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
2077 isl_int_set_si(*d, 1);
2078 if (!qp)
2079 return;
2080 upoly_update_den(qp->upoly, d);
2083 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2084 __isl_take isl_space *dim, int pos, int power)
2086 struct isl_ctx *ctx;
2088 if (!dim)
2089 return NULL;
2091 ctx = dim->ctx;
2093 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
2096 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(__isl_take isl_space *dim,
2097 enum isl_dim_type type, unsigned pos)
2099 if (!dim)
2100 return NULL;
2102 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
2103 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
2105 if (type == isl_dim_set)
2106 pos += isl_space_dim(dim, isl_dim_param);
2108 return isl_qpolynomial_var_pow_on_domain(dim, pos, 1);
2109 error:
2110 isl_space_free(dim);
2111 return NULL;
2114 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
2115 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
2117 int i;
2118 struct isl_upoly_rec *rec;
2119 struct isl_upoly *base, *res;
2121 if (!up)
2122 return NULL;
2124 if (isl_upoly_is_cst(up))
2125 return up;
2127 if (up->var < first)
2128 return up;
2130 rec = isl_upoly_as_rec(up);
2131 if (!rec)
2132 goto error;
2134 isl_assert(up->ctx, rec->n >= 1, goto error);
2136 if (up->var >= first + n)
2137 base = isl_upoly_var_pow(up->ctx, up->var, 1);
2138 else
2139 base = isl_upoly_copy(subs[up->var - first]);
2141 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
2142 for (i = rec->n - 2; i >= 0; --i) {
2143 struct isl_upoly *t;
2144 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
2145 res = isl_upoly_mul(res, isl_upoly_copy(base));
2146 res = isl_upoly_sum(res, t);
2149 isl_upoly_free(base);
2150 isl_upoly_free(up);
2152 return res;
2153 error:
2154 isl_upoly_free(up);
2155 return NULL;
2158 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
2159 isl_int denom, unsigned len)
2161 int i;
2162 struct isl_upoly *up;
2164 isl_assert(ctx, len >= 1, return NULL);
2166 up = isl_upoly_rat_cst(ctx, f[0], denom);
2167 for (i = 0; i < len - 1; ++i) {
2168 struct isl_upoly *t;
2169 struct isl_upoly *c;
2171 if (isl_int_is_zero(f[1 + i]))
2172 continue;
2174 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2175 t = isl_upoly_var_pow(ctx, i, 1);
2176 t = isl_upoly_mul(c, t);
2177 up = isl_upoly_sum(up, t);
2180 return up;
2183 /* Remove common factor of non-constant terms and denominator.
2185 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2187 isl_ctx *ctx = qp->div->ctx;
2188 unsigned total = qp->div->n_col - 2;
2190 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2191 isl_int_gcd(ctx->normalize_gcd,
2192 ctx->normalize_gcd, qp->div->row[div][0]);
2193 if (isl_int_is_one(ctx->normalize_gcd))
2194 return;
2196 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2197 ctx->normalize_gcd, total);
2198 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2199 ctx->normalize_gcd);
2200 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2201 ctx->normalize_gcd);
2204 /* Replace the integer division identified by "div" by the polynomial "s".
2205 * The integer division is assumed not to appear in the definition
2206 * of any other integer divisions.
2208 static __isl_give isl_qpolynomial *substitute_div(
2209 __isl_take isl_qpolynomial *qp,
2210 int div, __isl_take struct isl_upoly *s)
2212 int i;
2213 int total;
2214 int *reordering;
2216 if (!qp || !s)
2217 goto error;
2219 qp = isl_qpolynomial_cow(qp);
2220 if (!qp)
2221 goto error;
2223 total = isl_space_dim(qp->dim, isl_dim_all);
2224 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2225 if (!qp->upoly)
2226 goto error;
2228 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2229 if (!reordering)
2230 goto error;
2231 for (i = 0; i < total + div; ++i)
2232 reordering[i] = i;
2233 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2234 reordering[i] = i - 1;
2235 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2236 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2237 qp->upoly = reorder(qp->upoly, reordering);
2238 free(reordering);
2240 if (!qp->upoly || !qp->div)
2241 goto error;
2243 isl_upoly_free(s);
2244 return qp;
2245 error:
2246 isl_qpolynomial_free(qp);
2247 isl_upoly_free(s);
2248 return NULL;
2251 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2252 * divisions because d is equal to 1 by their definition, i.e., e.
2254 static __isl_give isl_qpolynomial *substitute_non_divs(
2255 __isl_take isl_qpolynomial *qp)
2257 int i, j;
2258 int total;
2259 struct isl_upoly *s;
2261 if (!qp)
2262 return NULL;
2264 total = isl_space_dim(qp->dim, isl_dim_all);
2265 for (i = 0; qp && i < qp->div->n_row; ++i) {
2266 if (!isl_int_is_one(qp->div->row[i][0]))
2267 continue;
2268 for (j = i + 1; j < qp->div->n_row; ++j) {
2269 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2270 continue;
2271 isl_seq_combine(qp->div->row[j] + 1,
2272 qp->div->ctx->one, qp->div->row[j] + 1,
2273 qp->div->row[j][2 + total + i],
2274 qp->div->row[i] + 1, 1 + total + i);
2275 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2276 normalize_div(qp, j);
2278 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2279 qp->div->row[i][0], qp->div->n_col - 1);
2280 qp = substitute_div(qp, i, s);
2281 --i;
2284 return qp;
2287 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2288 * with d the denominator. When replacing the coefficient e of x by
2289 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2290 * inside the division, so we need to add floor(e/d) * x outside.
2291 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2292 * to adjust the coefficient of x in each later div that depends on the
2293 * current div "div" and also in the affine expressions in the rows of "mat"
2294 * (if they too depend on "div").
2296 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2297 __isl_keep isl_mat **mat)
2299 int i, j;
2300 isl_int v;
2301 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2303 isl_int_init(v);
2304 for (i = 0; i < 1 + total + div; ++i) {
2305 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2306 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2307 continue;
2308 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2309 isl_int_fdiv_r(qp->div->row[div][1 + i],
2310 qp->div->row[div][1 + i], qp->div->row[div][0]);
2311 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2312 for (j = div + 1; j < qp->div->n_row; ++j) {
2313 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2314 continue;
2315 isl_int_addmul(qp->div->row[j][1 + i],
2316 v, qp->div->row[j][2 + total + div]);
2319 isl_int_clear(v);
2322 /* Check if the last non-zero coefficient is bigger that half of the
2323 * denominator. If so, we will invert the div to further reduce the number
2324 * of distinct divs that may appear.
2325 * If the last non-zero coefficient is exactly half the denominator,
2326 * then we continue looking for earlier coefficients that are bigger
2327 * than half the denominator.
2329 static int needs_invert(__isl_keep isl_mat *div, int row)
2331 int i;
2332 int cmp;
2334 for (i = div->n_col - 1; i >= 1; --i) {
2335 if (isl_int_is_zero(div->row[row][i]))
2336 continue;
2337 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2338 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2339 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2340 if (cmp)
2341 return cmp > 0;
2342 if (i == 1)
2343 return 1;
2346 return 0;
2349 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2350 * We only invert the coefficients of e (and the coefficient of q in
2351 * later divs and in the rows of "mat"). After calling this function, the
2352 * coefficients of e should be reduced again.
2354 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2355 __isl_keep isl_mat **mat)
2357 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2359 isl_seq_neg(qp->div->row[div] + 1,
2360 qp->div->row[div] + 1, qp->div->n_col - 1);
2361 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2362 isl_int_add(qp->div->row[div][1],
2363 qp->div->row[div][1], qp->div->row[div][0]);
2364 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2365 isl_mat_col_mul(qp->div, 2 + total + div,
2366 qp->div->ctx->negone, 2 + total + div);
2369 /* Reduce all divs of "qp" to have coefficients
2370 * in the interval [0, d-1], with d the denominator and such that the
2371 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2372 * The modifications to the integer divisions need to be reflected
2373 * in the factors of the polynomial that refer to the original
2374 * integer divisions. To this end, the modifications are collected
2375 * as a set of affine expressions and then plugged into the polynomial.
2377 * After the reduction, some divs may have become redundant or identical,
2378 * so we call substitute_non_divs and sort_divs. If these functions
2379 * eliminate divs or merge two or more divs into one, the coefficients
2380 * of the enclosing divs may have to be reduced again, so we call
2381 * ourselves recursively if the number of divs decreases.
2383 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2385 int i;
2386 isl_ctx *ctx;
2387 isl_mat *mat;
2388 struct isl_upoly **s;
2389 unsigned o_div, n_div, total;
2391 if (!qp)
2392 return NULL;
2394 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2395 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2396 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2397 ctx = isl_qpolynomial_get_ctx(qp);
2398 mat = isl_mat_zero(ctx, n_div, 1 + total);
2400 for (i = 0; i < n_div; ++i)
2401 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2403 for (i = 0; i < qp->div->n_row; ++i) {
2404 normalize_div(qp, i);
2405 reduce_div(qp, i, &mat);
2406 if (needs_invert(qp->div, i)) {
2407 invert_div(qp, i, &mat);
2408 reduce_div(qp, i, &mat);
2411 if (!mat)
2412 goto error;
2414 s = isl_alloc_array(ctx, struct isl_upoly *, n_div);
2415 if (n_div && !s)
2416 goto error;
2417 for (i = 0; i < n_div; ++i)
2418 s[i] = isl_upoly_from_affine(ctx, mat->row[i], ctx->one,
2419 1 + total);
2420 qp->upoly = isl_upoly_subs(qp->upoly, o_div - 1, n_div, s);
2421 for (i = 0; i < n_div; ++i)
2422 isl_upoly_free(s[i]);
2423 free(s);
2424 if (!qp->upoly)
2425 goto error;
2427 isl_mat_free(mat);
2429 qp = substitute_non_divs(qp);
2430 qp = sort_divs(qp);
2431 if (qp && isl_qpolynomial_domain_dim(qp, isl_dim_div) < n_div)
2432 return reduce_divs(qp);
2434 return qp;
2435 error:
2436 isl_qpolynomial_free(qp);
2437 isl_mat_free(mat);
2438 return NULL;
2441 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2442 __isl_take isl_space *dim, const isl_int n, const isl_int d)
2444 struct isl_qpolynomial *qp;
2445 struct isl_upoly_cst *cst;
2447 if (!dim)
2448 return NULL;
2450 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2451 if (!qp)
2452 return NULL;
2454 cst = isl_upoly_as_cst(qp->upoly);
2455 isl_int_set(cst->n, n);
2456 isl_int_set(cst->d, d);
2458 return qp;
2461 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2463 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2464 __isl_take isl_space *domain, __isl_take isl_val *val)
2466 isl_qpolynomial *qp;
2467 struct isl_upoly_cst *cst;
2469 if (!domain || !val)
2470 goto error;
2472 qp = isl_qpolynomial_alloc(isl_space_copy(domain), 0,
2473 isl_upoly_zero(domain->ctx));
2474 if (!qp)
2475 goto error;
2477 cst = isl_upoly_as_cst(qp->upoly);
2478 isl_int_set(cst->n, val->n);
2479 isl_int_set(cst->d, val->d);
2481 isl_space_free(domain);
2482 isl_val_free(val);
2483 return qp;
2484 error:
2485 isl_space_free(domain);
2486 isl_val_free(val);
2487 return NULL;
2490 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2492 struct isl_upoly_rec *rec;
2493 int i;
2495 if (!up)
2496 return -1;
2498 if (isl_upoly_is_cst(up))
2499 return 0;
2501 if (up->var < d)
2502 active[up->var] = 1;
2504 rec = isl_upoly_as_rec(up);
2505 for (i = 0; i < rec->n; ++i)
2506 if (up_set_active(rec->p[i], active, d) < 0)
2507 return -1;
2509 return 0;
2512 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2514 int i, j;
2515 int d = isl_space_dim(qp->dim, isl_dim_all);
2517 if (!qp || !active)
2518 return -1;
2520 for (i = 0; i < d; ++i)
2521 for (j = 0; j < qp->div->n_row; ++j) {
2522 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2523 continue;
2524 active[i] = 1;
2525 break;
2528 return up_set_active(qp->upoly, active, d);
2531 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2532 enum isl_dim_type type, unsigned first, unsigned n)
2534 int i;
2535 int *active = NULL;
2536 isl_bool involves = isl_bool_false;
2538 if (!qp)
2539 return isl_bool_error;
2540 if (n == 0)
2541 return isl_bool_false;
2543 isl_assert(qp->dim->ctx,
2544 first + n <= isl_qpolynomial_dim(qp, type),
2545 return isl_bool_error);
2546 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2547 type == isl_dim_in, return isl_bool_error);
2549 active = isl_calloc_array(qp->dim->ctx, int,
2550 isl_space_dim(qp->dim, isl_dim_all));
2551 if (set_active(qp, active) < 0)
2552 goto error;
2554 if (type == isl_dim_in)
2555 first += isl_space_dim(qp->dim, isl_dim_param);
2556 for (i = 0; i < n; ++i)
2557 if (active[first + i]) {
2558 involves = isl_bool_true;
2559 break;
2562 free(active);
2564 return involves;
2565 error:
2566 free(active);
2567 return isl_bool_error;
2570 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2571 * of the divs that do appear in the quasi-polynomial.
2573 static __isl_give isl_qpolynomial *remove_redundant_divs(
2574 __isl_take isl_qpolynomial *qp)
2576 int i, j;
2577 int d;
2578 int len;
2579 int skip;
2580 int *active = NULL;
2581 int *reordering = NULL;
2582 int redundant = 0;
2583 int n_div;
2584 isl_ctx *ctx;
2586 if (!qp)
2587 return NULL;
2588 if (qp->div->n_row == 0)
2589 return qp;
2591 d = isl_space_dim(qp->dim, isl_dim_all);
2592 len = qp->div->n_col - 2;
2593 ctx = isl_qpolynomial_get_ctx(qp);
2594 active = isl_calloc_array(ctx, int, len);
2595 if (!active)
2596 goto error;
2598 if (up_set_active(qp->upoly, active, len) < 0)
2599 goto error;
2601 for (i = qp->div->n_row - 1; i >= 0; --i) {
2602 if (!active[d + i]) {
2603 redundant = 1;
2604 continue;
2606 for (j = 0; j < i; ++j) {
2607 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2608 continue;
2609 active[d + j] = 1;
2610 break;
2614 if (!redundant) {
2615 free(active);
2616 return qp;
2619 reordering = isl_alloc_array(qp->div->ctx, int, len);
2620 if (!reordering)
2621 goto error;
2623 for (i = 0; i < d; ++i)
2624 reordering[i] = i;
2626 skip = 0;
2627 n_div = qp->div->n_row;
2628 for (i = 0; i < n_div; ++i) {
2629 if (!active[d + i]) {
2630 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2631 qp->div = isl_mat_drop_cols(qp->div,
2632 2 + d + i - skip, 1);
2633 skip++;
2635 reordering[d + i] = d + i - skip;
2638 qp->upoly = reorder(qp->upoly, reordering);
2640 if (!qp->upoly || !qp->div)
2641 goto error;
2643 free(active);
2644 free(reordering);
2646 return qp;
2647 error:
2648 free(active);
2649 free(reordering);
2650 isl_qpolynomial_free(qp);
2651 return NULL;
2654 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2655 unsigned first, unsigned n)
2657 int i;
2658 struct isl_upoly_rec *rec;
2660 if (!up)
2661 return NULL;
2662 if (n == 0 || up->var < 0 || up->var < first)
2663 return up;
2664 if (up->var < first + n) {
2665 up = replace_by_constant_term(up);
2666 return isl_upoly_drop(up, first, n);
2668 up = isl_upoly_cow(up);
2669 if (!up)
2670 return NULL;
2671 up->var -= n;
2672 rec = isl_upoly_as_rec(up);
2673 if (!rec)
2674 goto error;
2676 for (i = 0; i < rec->n; ++i) {
2677 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2678 if (!rec->p[i])
2679 goto error;
2682 return up;
2683 error:
2684 isl_upoly_free(up);
2685 return NULL;
2688 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2689 __isl_take isl_qpolynomial *qp,
2690 enum isl_dim_type type, unsigned pos, const char *s)
2692 qp = isl_qpolynomial_cow(qp);
2693 if (!qp)
2694 return NULL;
2695 if (type == isl_dim_out)
2696 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2697 "cannot set name of output/set dimension",
2698 return isl_qpolynomial_free(qp));
2699 if (type == isl_dim_in)
2700 type = isl_dim_set;
2701 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2702 if (!qp->dim)
2703 goto error;
2704 return qp;
2705 error:
2706 isl_qpolynomial_free(qp);
2707 return NULL;
2710 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2711 __isl_take isl_qpolynomial *qp,
2712 enum isl_dim_type type, unsigned first, unsigned n)
2714 if (!qp)
2715 return NULL;
2716 if (type == isl_dim_out)
2717 isl_die(qp->dim->ctx, isl_error_invalid,
2718 "cannot drop output/set dimension",
2719 goto error);
2720 if (type == isl_dim_in)
2721 type = isl_dim_set;
2722 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2723 return qp;
2725 qp = isl_qpolynomial_cow(qp);
2726 if (!qp)
2727 return NULL;
2729 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2730 goto error);
2731 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2732 type == isl_dim_set, goto error);
2734 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2735 if (!qp->dim)
2736 goto error;
2738 if (type == isl_dim_set)
2739 first += isl_space_dim(qp->dim, isl_dim_param);
2741 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2742 if (!qp->div)
2743 goto error;
2745 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2746 if (!qp->upoly)
2747 goto error;
2749 return qp;
2750 error:
2751 isl_qpolynomial_free(qp);
2752 return NULL;
2755 /* Project the domain of the quasi-polynomial onto its parameter space.
2756 * The quasi-polynomial may not involve any of the domain dimensions.
2758 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2759 __isl_take isl_qpolynomial *qp)
2761 isl_space *space;
2762 unsigned n;
2763 int involves;
2765 n = isl_qpolynomial_dim(qp, isl_dim_in);
2766 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2767 if (involves < 0)
2768 return isl_qpolynomial_free(qp);
2769 if (involves)
2770 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2771 "polynomial involves some of the domain dimensions",
2772 return isl_qpolynomial_free(qp));
2773 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2774 space = isl_qpolynomial_get_domain_space(qp);
2775 space = isl_space_params(space);
2776 qp = isl_qpolynomial_reset_domain_space(qp, space);
2777 return qp;
2780 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2781 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2783 int i, j, k;
2784 isl_int denom;
2785 unsigned total;
2786 unsigned n_div;
2787 struct isl_upoly *up;
2789 if (!eq)
2790 goto error;
2791 if (eq->n_eq == 0) {
2792 isl_basic_set_free(eq);
2793 return qp;
2796 qp = isl_qpolynomial_cow(qp);
2797 if (!qp)
2798 goto error;
2799 qp->div = isl_mat_cow(qp->div);
2800 if (!qp->div)
2801 goto error;
2803 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2804 n_div = eq->n_div;
2805 isl_int_init(denom);
2806 for (i = 0; i < eq->n_eq; ++i) {
2807 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2808 if (j < 0 || j == 0 || j >= total)
2809 continue;
2811 for (k = 0; k < qp->div->n_row; ++k) {
2812 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2813 continue;
2814 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2815 &qp->div->row[k][0]);
2816 normalize_div(qp, k);
2819 if (isl_int_is_pos(eq->eq[i][j]))
2820 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2821 isl_int_abs(denom, eq->eq[i][j]);
2822 isl_int_set_si(eq->eq[i][j], 0);
2824 up = isl_upoly_from_affine(qp->dim->ctx,
2825 eq->eq[i], denom, total);
2826 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2827 isl_upoly_free(up);
2829 isl_int_clear(denom);
2831 if (!qp->upoly)
2832 goto error;
2834 isl_basic_set_free(eq);
2836 qp = substitute_non_divs(qp);
2837 qp = sort_divs(qp);
2839 return qp;
2840 error:
2841 isl_basic_set_free(eq);
2842 isl_qpolynomial_free(qp);
2843 return NULL;
2846 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2848 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2849 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2851 if (!qp || !eq)
2852 goto error;
2853 if (qp->div->n_row > 0)
2854 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2855 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2856 error:
2857 isl_basic_set_free(eq);
2858 isl_qpolynomial_free(qp);
2859 return NULL;
2862 static __isl_give isl_basic_set *add_div_constraints(
2863 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2865 int i;
2866 unsigned total;
2868 if (!bset || !div)
2869 goto error;
2871 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2872 if (!bset)
2873 goto error;
2874 total = isl_basic_set_total_dim(bset);
2875 for (i = 0; i < div->n_row; ++i)
2876 if (isl_basic_set_add_div_constraints_var(bset,
2877 total - div->n_row + i, div->row[i]) < 0)
2878 goto error;
2880 isl_mat_free(div);
2881 return bset;
2882 error:
2883 isl_mat_free(div);
2884 isl_basic_set_free(bset);
2885 return NULL;
2888 /* Look for equalities among the variables shared by context and qp
2889 * and the integer divisions of qp, if any.
2890 * The equalities are then used to eliminate variables and/or integer
2891 * divisions from qp.
2893 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2894 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2896 isl_basic_set *aff;
2898 if (!qp)
2899 goto error;
2900 if (qp->div->n_row > 0) {
2901 isl_basic_set *bset;
2902 context = isl_set_add_dims(context, isl_dim_set,
2903 qp->div->n_row);
2904 bset = isl_basic_set_universe(isl_set_get_space(context));
2905 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2906 context = isl_set_intersect(context,
2907 isl_set_from_basic_set(bset));
2910 aff = isl_set_affine_hull(context);
2911 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2912 error:
2913 isl_qpolynomial_free(qp);
2914 isl_set_free(context);
2915 return NULL;
2918 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2919 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2921 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2922 isl_set *dom_context = isl_set_universe(space);
2923 dom_context = isl_set_intersect_params(dom_context, context);
2924 return isl_qpolynomial_gist(qp, dom_context);
2927 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2928 __isl_take isl_qpolynomial *qp)
2930 isl_set *dom;
2932 if (!qp)
2933 return NULL;
2934 if (isl_qpolynomial_is_zero(qp)) {
2935 isl_space *dim = isl_qpolynomial_get_space(qp);
2936 isl_qpolynomial_free(qp);
2937 return isl_pw_qpolynomial_zero(dim);
2940 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2941 return isl_pw_qpolynomial_alloc(dom, qp);
2944 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2946 #undef PW
2947 #define PW isl_pw_qpolynomial
2948 #undef EL
2949 #define EL isl_qpolynomial
2950 #undef EL_IS_ZERO
2951 #define EL_IS_ZERO is_zero
2952 #undef ZERO
2953 #define ZERO zero
2954 #undef IS_ZERO
2955 #define IS_ZERO is_zero
2956 #undef FIELD
2957 #define FIELD qp
2958 #undef DEFAULT_IS_ZERO
2959 #define DEFAULT_IS_ZERO 1
2961 #define NO_PULLBACK
2963 #include <isl_pw_templ.c>
2964 #include <isl_pw_eval.c>
2966 #undef BASE
2967 #define BASE pw_qpolynomial
2969 #include <isl_union_single.c>
2970 #include <isl_union_eval.c>
2971 #include <isl_union_neg.c>
2973 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2975 if (!pwqp)
2976 return -1;
2978 if (pwqp->n != -1)
2979 return 0;
2981 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2982 return 0;
2984 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2987 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2988 __isl_take isl_pw_qpolynomial *pwqp1,
2989 __isl_take isl_pw_qpolynomial *pwqp2)
2991 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2994 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2995 __isl_take isl_pw_qpolynomial *pwqp1,
2996 __isl_take isl_pw_qpolynomial *pwqp2)
2998 int i, j, n;
2999 struct isl_pw_qpolynomial *res;
3001 if (!pwqp1 || !pwqp2)
3002 goto error;
3004 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3005 goto error);
3007 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3008 isl_pw_qpolynomial_free(pwqp2);
3009 return pwqp1;
3012 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3013 isl_pw_qpolynomial_free(pwqp1);
3014 return pwqp2;
3017 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3018 isl_pw_qpolynomial_free(pwqp1);
3019 return pwqp2;
3022 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3023 isl_pw_qpolynomial_free(pwqp2);
3024 return pwqp1;
3027 n = pwqp1->n * pwqp2->n;
3028 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3030 for (i = 0; i < pwqp1->n; ++i) {
3031 for (j = 0; j < pwqp2->n; ++j) {
3032 struct isl_set *common;
3033 struct isl_qpolynomial *prod;
3034 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3035 isl_set_copy(pwqp2->p[j].set));
3036 if (isl_set_plain_is_empty(common)) {
3037 isl_set_free(common);
3038 continue;
3041 prod = isl_qpolynomial_mul(
3042 isl_qpolynomial_copy(pwqp1->p[i].qp),
3043 isl_qpolynomial_copy(pwqp2->p[j].qp));
3045 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3049 isl_pw_qpolynomial_free(pwqp1);
3050 isl_pw_qpolynomial_free(pwqp2);
3052 return res;
3053 error:
3054 isl_pw_qpolynomial_free(pwqp1);
3055 isl_pw_qpolynomial_free(pwqp2);
3056 return NULL;
3059 __isl_give isl_val *isl_upoly_eval(__isl_take struct isl_upoly *up,
3060 __isl_take isl_vec *vec)
3062 int i;
3063 struct isl_upoly_rec *rec;
3064 isl_val *res;
3065 isl_val *base;
3067 if (isl_upoly_is_cst(up)) {
3068 isl_vec_free(vec);
3069 res = isl_upoly_get_constant_val(up);
3070 isl_upoly_free(up);
3071 return res;
3074 rec = isl_upoly_as_rec(up);
3075 if (!rec || !vec)
3076 goto error;
3078 isl_assert(up->ctx, rec->n >= 1, goto error);
3080 base = isl_val_rat_from_isl_int(up->ctx,
3081 vec->el[1 + up->var], vec->el[0]);
3083 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
3084 isl_vec_copy(vec));
3086 for (i = rec->n - 2; i >= 0; --i) {
3087 res = isl_val_mul(res, isl_val_copy(base));
3088 res = isl_val_add(res,
3089 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
3090 isl_vec_copy(vec)));
3093 isl_val_free(base);
3094 isl_upoly_free(up);
3095 isl_vec_free(vec);
3096 return res;
3097 error:
3098 isl_upoly_free(up);
3099 isl_vec_free(vec);
3100 return NULL;
3103 /* Evaluate "qp" in the void point "pnt".
3104 * In particular, return the value NaN.
3106 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3107 __isl_take isl_point *pnt)
3109 isl_ctx *ctx;
3111 ctx = isl_point_get_ctx(pnt);
3112 isl_qpolynomial_free(qp);
3113 isl_point_free(pnt);
3114 return isl_val_nan(ctx);
3117 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3118 __isl_take isl_point *pnt)
3120 isl_bool is_void;
3121 isl_vec *ext;
3122 isl_val *v;
3124 if (!qp || !pnt)
3125 goto error;
3126 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3127 is_void = isl_point_is_void(pnt);
3128 if (is_void < 0)
3129 goto error;
3130 if (is_void)
3131 return eval_void(qp, pnt);
3133 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3135 v = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
3137 isl_qpolynomial_free(qp);
3138 isl_point_free(pnt);
3140 return v;
3141 error:
3142 isl_qpolynomial_free(qp);
3143 isl_point_free(pnt);
3144 return NULL;
3147 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
3148 __isl_keep struct isl_upoly_cst *cst2)
3150 int cmp;
3151 isl_int t;
3152 isl_int_init(t);
3153 isl_int_mul(t, cst1->n, cst2->d);
3154 isl_int_submul(t, cst2->n, cst1->d);
3155 cmp = isl_int_sgn(t);
3156 isl_int_clear(t);
3157 return cmp;
3160 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3161 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3162 unsigned first, unsigned n)
3164 unsigned total;
3165 unsigned g_pos;
3166 int *exp;
3168 if (!qp)
3169 return NULL;
3170 if (type == isl_dim_out)
3171 isl_die(qp->div->ctx, isl_error_invalid,
3172 "cannot insert output/set dimensions",
3173 goto error);
3174 if (type == isl_dim_in)
3175 type = isl_dim_set;
3176 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3177 return qp;
3179 qp = isl_qpolynomial_cow(qp);
3180 if (!qp)
3181 return NULL;
3183 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
3184 goto error);
3186 g_pos = pos(qp->dim, type) + first;
3188 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3189 if (!qp->div)
3190 goto error;
3192 total = qp->div->n_col - 2;
3193 if (total > g_pos) {
3194 int i;
3195 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3196 if (!exp)
3197 goto error;
3198 for (i = 0; i < total - g_pos; ++i)
3199 exp[i] = i + n;
3200 qp->upoly = expand(qp->upoly, exp, g_pos);
3201 free(exp);
3202 if (!qp->upoly)
3203 goto error;
3206 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3207 if (!qp->dim)
3208 goto error;
3210 return qp;
3211 error:
3212 isl_qpolynomial_free(qp);
3213 return NULL;
3216 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3217 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3219 unsigned pos;
3221 pos = isl_qpolynomial_dim(qp, type);
3223 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3226 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3227 __isl_take isl_pw_qpolynomial *pwqp,
3228 enum isl_dim_type type, unsigned n)
3230 unsigned pos;
3232 pos = isl_pw_qpolynomial_dim(pwqp, type);
3234 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3237 static int *reordering_move(isl_ctx *ctx,
3238 unsigned len, unsigned dst, unsigned src, unsigned n)
3240 int i;
3241 int *reordering;
3243 reordering = isl_alloc_array(ctx, int, len);
3244 if (!reordering)
3245 return NULL;
3247 if (dst <= src) {
3248 for (i = 0; i < dst; ++i)
3249 reordering[i] = i;
3250 for (i = 0; i < n; ++i)
3251 reordering[src + i] = dst + i;
3252 for (i = 0; i < src - dst; ++i)
3253 reordering[dst + i] = dst + n + i;
3254 for (i = 0; i < len - src - n; ++i)
3255 reordering[src + n + i] = src + n + i;
3256 } else {
3257 for (i = 0; i < src; ++i)
3258 reordering[i] = i;
3259 for (i = 0; i < n; ++i)
3260 reordering[src + i] = dst + i;
3261 for (i = 0; i < dst - src; ++i)
3262 reordering[src + n + i] = src + i;
3263 for (i = 0; i < len - dst - n; ++i)
3264 reordering[dst + n + i] = dst + n + i;
3267 return reordering;
3270 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3271 __isl_take isl_qpolynomial *qp,
3272 enum isl_dim_type dst_type, unsigned dst_pos,
3273 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3275 unsigned g_dst_pos;
3276 unsigned g_src_pos;
3277 int *reordering;
3279 if (!qp)
3280 return NULL;
3282 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3283 isl_die(qp->dim->ctx, isl_error_invalid,
3284 "cannot move output/set dimension",
3285 goto error);
3286 if (dst_type == isl_dim_in)
3287 dst_type = isl_dim_set;
3288 if (src_type == isl_dim_in)
3289 src_type = isl_dim_set;
3291 if (n == 0 &&
3292 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3293 !isl_space_is_named_or_nested(qp->dim, dst_type))
3294 return qp;
3296 qp = isl_qpolynomial_cow(qp);
3297 if (!qp)
3298 return NULL;
3300 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
3301 goto error);
3303 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3304 g_src_pos = pos(qp->dim, src_type) + src_pos;
3305 if (dst_type > src_type)
3306 g_dst_pos -= n;
3308 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3309 if (!qp->div)
3310 goto error;
3311 qp = sort_divs(qp);
3312 if (!qp)
3313 goto error;
3315 reordering = reordering_move(qp->dim->ctx,
3316 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3317 if (!reordering)
3318 goto error;
3320 qp->upoly = reorder(qp->upoly, reordering);
3321 free(reordering);
3322 if (!qp->upoly)
3323 goto error;
3325 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3326 if (!qp->dim)
3327 goto error;
3329 return qp;
3330 error:
3331 isl_qpolynomial_free(qp);
3332 return NULL;
3335 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3336 isl_int *f, isl_int denom)
3338 struct isl_upoly *up;
3340 dim = isl_space_domain(dim);
3341 if (!dim)
3342 return NULL;
3344 up = isl_upoly_from_affine(dim->ctx, f, denom,
3345 1 + isl_space_dim(dim, isl_dim_all));
3347 return isl_qpolynomial_alloc(dim, 0, up);
3350 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3352 isl_ctx *ctx;
3353 struct isl_upoly *up;
3354 isl_qpolynomial *qp;
3356 if (!aff)
3357 return NULL;
3359 ctx = isl_aff_get_ctx(aff);
3360 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3361 aff->v->size - 1);
3363 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3364 aff->ls->div->n_row, up);
3365 if (!qp)
3366 goto error;
3368 isl_mat_free(qp->div);
3369 qp->div = isl_mat_copy(aff->ls->div);
3370 qp->div = isl_mat_cow(qp->div);
3371 if (!qp->div)
3372 goto error;
3374 isl_aff_free(aff);
3375 qp = reduce_divs(qp);
3376 qp = remove_redundant_divs(qp);
3377 return qp;
3378 error:
3379 isl_aff_free(aff);
3380 return isl_qpolynomial_free(qp);
3383 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3384 __isl_take isl_pw_aff *pwaff)
3386 int i;
3387 isl_pw_qpolynomial *pwqp;
3389 if (!pwaff)
3390 return NULL;
3392 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3393 pwaff->n);
3395 for (i = 0; i < pwaff->n; ++i) {
3396 isl_set *dom;
3397 isl_qpolynomial *qp;
3399 dom = isl_set_copy(pwaff->p[i].set);
3400 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3401 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3404 isl_pw_aff_free(pwaff);
3405 return pwqp;
3408 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3409 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3411 isl_aff *aff;
3413 aff = isl_constraint_get_bound(c, type, pos);
3414 isl_constraint_free(c);
3415 return isl_qpolynomial_from_aff(aff);
3418 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3419 * in "qp" by subs[i].
3421 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3422 __isl_take isl_qpolynomial *qp,
3423 enum isl_dim_type type, unsigned first, unsigned n,
3424 __isl_keep isl_qpolynomial **subs)
3426 int i;
3427 struct isl_upoly **ups;
3429 if (n == 0)
3430 return qp;
3432 qp = isl_qpolynomial_cow(qp);
3433 if (!qp)
3434 return NULL;
3436 if (type == isl_dim_out)
3437 isl_die(qp->dim->ctx, isl_error_invalid,
3438 "cannot substitute output/set dimension",
3439 goto error);
3440 if (type == isl_dim_in)
3441 type = isl_dim_set;
3443 for (i = 0; i < n; ++i)
3444 if (!subs[i])
3445 goto error;
3447 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3448 goto error);
3450 for (i = 0; i < n; ++i)
3451 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3452 goto error);
3454 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3455 for (i = 0; i < n; ++i)
3456 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3458 first += pos(qp->dim, type);
3460 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3461 if (!ups)
3462 goto error;
3463 for (i = 0; i < n; ++i)
3464 ups[i] = subs[i]->upoly;
3466 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3468 free(ups);
3470 if (!qp->upoly)
3471 goto error;
3473 return qp;
3474 error:
3475 isl_qpolynomial_free(qp);
3476 return NULL;
3479 /* Extend "bset" with extra set dimensions for each integer division
3480 * in "qp" and then call "fn" with the extended bset and the polynomial
3481 * that results from replacing each of the integer divisions by the
3482 * corresponding extra set dimension.
3484 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3485 __isl_keep isl_basic_set *bset,
3486 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3487 __isl_take isl_qpolynomial *poly, void *user), void *user)
3489 isl_space *dim;
3490 isl_mat *div;
3491 isl_qpolynomial *poly;
3493 if (!qp || !bset)
3494 return isl_stat_error;
3495 if (qp->div->n_row == 0)
3496 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3497 user);
3499 div = isl_mat_copy(qp->div);
3500 dim = isl_space_copy(qp->dim);
3501 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3502 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3503 bset = isl_basic_set_copy(bset);
3504 bset = isl_basic_set_add_dims(bset, isl_dim_set, qp->div->n_row);
3505 bset = add_div_constraints(bset, div);
3507 return fn(bset, poly, user);
3510 /* Return total degree in variables first (inclusive) up to last (exclusive).
3512 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3514 int deg = -1;
3515 int i;
3516 struct isl_upoly_rec *rec;
3518 if (!up)
3519 return -2;
3520 if (isl_upoly_is_zero(up))
3521 return -1;
3522 if (isl_upoly_is_cst(up) || up->var < first)
3523 return 0;
3525 rec = isl_upoly_as_rec(up);
3526 if (!rec)
3527 return -2;
3529 for (i = 0; i < rec->n; ++i) {
3530 int d;
3532 if (isl_upoly_is_zero(rec->p[i]))
3533 continue;
3534 d = isl_upoly_degree(rec->p[i], first, last);
3535 if (up->var < last)
3536 d += i;
3537 if (d > deg)
3538 deg = d;
3541 return deg;
3544 /* Return total degree in set variables.
3546 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3548 unsigned ovar;
3549 unsigned nvar;
3551 if (!poly)
3552 return -2;
3554 ovar = isl_space_offset(poly->dim, isl_dim_set);
3555 nvar = isl_space_dim(poly->dim, isl_dim_set);
3556 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3559 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3560 unsigned pos, int deg)
3562 int i;
3563 struct isl_upoly_rec *rec;
3565 if (!up)
3566 return NULL;
3568 if (isl_upoly_is_cst(up) || up->var < pos) {
3569 if (deg == 0)
3570 return isl_upoly_copy(up);
3571 else
3572 return isl_upoly_zero(up->ctx);
3575 rec = isl_upoly_as_rec(up);
3576 if (!rec)
3577 return NULL;
3579 if (up->var == pos) {
3580 if (deg < rec->n)
3581 return isl_upoly_copy(rec->p[deg]);
3582 else
3583 return isl_upoly_zero(up->ctx);
3586 up = isl_upoly_copy(up);
3587 up = isl_upoly_cow(up);
3588 rec = isl_upoly_as_rec(up);
3589 if (!rec)
3590 goto error;
3592 for (i = 0; i < rec->n; ++i) {
3593 struct isl_upoly *t;
3594 t = isl_upoly_coeff(rec->p[i], pos, deg);
3595 if (!t)
3596 goto error;
3597 isl_upoly_free(rec->p[i]);
3598 rec->p[i] = t;
3601 return up;
3602 error:
3603 isl_upoly_free(up);
3604 return NULL;
3607 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3609 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3610 __isl_keep isl_qpolynomial *qp,
3611 enum isl_dim_type type, unsigned t_pos, int deg)
3613 unsigned g_pos;
3614 struct isl_upoly *up;
3615 isl_qpolynomial *c;
3617 if (!qp)
3618 return NULL;
3620 if (type == isl_dim_out)
3621 isl_die(qp->div->ctx, isl_error_invalid,
3622 "output/set dimension does not have a coefficient",
3623 return NULL);
3624 if (type == isl_dim_in)
3625 type = isl_dim_set;
3627 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3628 return NULL);
3630 g_pos = pos(qp->dim, type) + t_pos;
3631 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3633 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3634 if (!c)
3635 return NULL;
3636 isl_mat_free(c->div);
3637 c->div = isl_mat_copy(qp->div);
3638 if (!c->div)
3639 goto error;
3640 return c;
3641 error:
3642 isl_qpolynomial_free(c);
3643 return NULL;
3646 /* Homogenize the polynomial in the variables first (inclusive) up to
3647 * last (exclusive) by inserting powers of variable first.
3648 * Variable first is assumed not to appear in the input.
3650 __isl_give struct isl_upoly *isl_upoly_homogenize(
3651 __isl_take struct isl_upoly *up, int deg, int target,
3652 int first, int last)
3654 int i;
3655 struct isl_upoly_rec *rec;
3657 if (!up)
3658 return NULL;
3659 if (isl_upoly_is_zero(up))
3660 return up;
3661 if (deg == target)
3662 return up;
3663 if (isl_upoly_is_cst(up) || up->var < first) {
3664 struct isl_upoly *hom;
3666 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3667 if (!hom)
3668 goto error;
3669 rec = isl_upoly_as_rec(hom);
3670 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3672 return hom;
3675 up = isl_upoly_cow(up);
3676 rec = isl_upoly_as_rec(up);
3677 if (!rec)
3678 goto error;
3680 for (i = 0; i < rec->n; ++i) {
3681 if (isl_upoly_is_zero(rec->p[i]))
3682 continue;
3683 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3684 up->var < last ? deg + i : i, target,
3685 first, last);
3686 if (!rec->p[i])
3687 goto error;
3690 return up;
3691 error:
3692 isl_upoly_free(up);
3693 return NULL;
3696 /* Homogenize the polynomial in the set variables by introducing
3697 * powers of an extra set variable at position 0.
3699 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3700 __isl_take isl_qpolynomial *poly)
3702 unsigned ovar;
3703 unsigned nvar;
3704 int deg = isl_qpolynomial_degree(poly);
3706 if (deg < -1)
3707 goto error;
3709 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3710 poly = isl_qpolynomial_cow(poly);
3711 if (!poly)
3712 goto error;
3714 ovar = isl_space_offset(poly->dim, isl_dim_set);
3715 nvar = isl_space_dim(poly->dim, isl_dim_set);
3716 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3717 ovar, ovar + nvar);
3718 if (!poly->upoly)
3719 goto error;
3721 return poly;
3722 error:
3723 isl_qpolynomial_free(poly);
3724 return NULL;
3727 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3728 __isl_take isl_mat *div)
3730 isl_term *term;
3731 int n;
3733 if (!dim || !div)
3734 goto error;
3736 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3738 term = isl_calloc(dim->ctx, struct isl_term,
3739 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3740 if (!term)
3741 goto error;
3743 term->ref = 1;
3744 term->dim = dim;
3745 term->div = div;
3746 isl_int_init(term->n);
3747 isl_int_init(term->d);
3749 return term;
3750 error:
3751 isl_space_free(dim);
3752 isl_mat_free(div);
3753 return NULL;
3756 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3758 if (!term)
3759 return NULL;
3761 term->ref++;
3762 return term;
3765 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3767 int i;
3768 isl_term *dup;
3769 unsigned total;
3771 if (!term)
3772 return NULL;
3774 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3776 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3777 if (!dup)
3778 return NULL;
3780 isl_int_set(dup->n, term->n);
3781 isl_int_set(dup->d, term->d);
3783 for (i = 0; i < total; ++i)
3784 dup->pow[i] = term->pow[i];
3786 return dup;
3789 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3791 if (!term)
3792 return NULL;
3794 if (term->ref == 1)
3795 return term;
3796 term->ref--;
3797 return isl_term_dup(term);
3800 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3802 if (!term)
3803 return NULL;
3805 if (--term->ref > 0)
3806 return NULL;
3808 isl_space_free(term->dim);
3809 isl_mat_free(term->div);
3810 isl_int_clear(term->n);
3811 isl_int_clear(term->d);
3812 free(term);
3814 return NULL;
3817 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3819 if (!term)
3820 return 0;
3822 switch (type) {
3823 case isl_dim_param:
3824 case isl_dim_in:
3825 case isl_dim_out: return isl_space_dim(term->dim, type);
3826 case isl_dim_div: return term->div->n_row;
3827 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3828 term->div->n_row;
3829 default: return 0;
3833 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3835 return term ? term->dim->ctx : NULL;
3838 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3840 if (!term)
3841 return;
3842 isl_int_set(*n, term->n);
3845 /* Return the coefficient of the term "term".
3847 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3849 if (!term)
3850 return NULL;
3852 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3853 term->n, term->d);
3856 int isl_term_get_exp(__isl_keep isl_term *term,
3857 enum isl_dim_type type, unsigned pos)
3859 if (!term)
3860 return -1;
3862 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3864 if (type >= isl_dim_set)
3865 pos += isl_space_dim(term->dim, isl_dim_param);
3866 if (type >= isl_dim_div)
3867 pos += isl_space_dim(term->dim, isl_dim_set);
3869 return term->pow[pos];
3872 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3874 isl_local_space *ls;
3875 isl_aff *aff;
3877 if (!term)
3878 return NULL;
3880 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3881 return NULL);
3883 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3884 isl_mat_copy(term->div));
3885 aff = isl_aff_alloc(ls);
3886 if (!aff)
3887 return NULL;
3889 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3891 aff = isl_aff_normalize(aff);
3893 return aff;
3896 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3897 isl_stat (*fn)(__isl_take isl_term *term, void *user),
3898 __isl_take isl_term *term, void *user)
3900 int i;
3901 struct isl_upoly_rec *rec;
3903 if (!up || !term)
3904 goto error;
3906 if (isl_upoly_is_zero(up))
3907 return term;
3909 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3910 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3911 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3913 if (isl_upoly_is_cst(up)) {
3914 struct isl_upoly_cst *cst;
3915 cst = isl_upoly_as_cst(up);
3916 if (!cst)
3917 goto error;
3918 term = isl_term_cow(term);
3919 if (!term)
3920 goto error;
3921 isl_int_set(term->n, cst->n);
3922 isl_int_set(term->d, cst->d);
3923 if (fn(isl_term_copy(term), user) < 0)
3924 goto error;
3925 return term;
3928 rec = isl_upoly_as_rec(up);
3929 if (!rec)
3930 goto error;
3932 for (i = 0; i < rec->n; ++i) {
3933 term = isl_term_cow(term);
3934 if (!term)
3935 goto error;
3936 term->pow[up->var] = i;
3937 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3938 if (!term)
3939 goto error;
3941 term->pow[up->var] = 0;
3943 return term;
3944 error:
3945 isl_term_free(term);
3946 return NULL;
3949 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3950 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
3952 isl_term *term;
3954 if (!qp)
3955 return isl_stat_error;
3957 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3958 if (!term)
3959 return isl_stat_error;
3961 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3963 isl_term_free(term);
3965 return term ? isl_stat_ok : isl_stat_error;
3968 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3970 struct isl_upoly *up;
3971 isl_qpolynomial *qp;
3972 int i, n;
3974 if (!term)
3975 return NULL;
3977 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3979 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3980 for (i = 0; i < n; ++i) {
3981 if (!term->pow[i])
3982 continue;
3983 up = isl_upoly_mul(up,
3984 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3987 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3988 if (!qp)
3989 goto error;
3990 isl_mat_free(qp->div);
3991 qp->div = isl_mat_copy(term->div);
3992 if (!qp->div)
3993 goto error;
3995 isl_term_free(term);
3996 return qp;
3997 error:
3998 isl_qpolynomial_free(qp);
3999 isl_term_free(term);
4000 return NULL;
4003 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4004 __isl_take isl_space *dim)
4006 int i;
4007 int extra;
4008 unsigned total;
4010 if (!qp || !dim)
4011 goto error;
4013 if (isl_space_is_equal(qp->dim, dim)) {
4014 isl_space_free(dim);
4015 return qp;
4018 qp = isl_qpolynomial_cow(qp);
4019 if (!qp)
4020 goto error;
4022 extra = isl_space_dim(dim, isl_dim_set) -
4023 isl_space_dim(qp->dim, isl_dim_set);
4024 total = isl_space_dim(qp->dim, isl_dim_all);
4025 if (qp->div->n_row) {
4026 int *exp;
4028 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4029 if (!exp)
4030 goto error;
4031 for (i = 0; i < qp->div->n_row; ++i)
4032 exp[i] = extra + i;
4033 qp->upoly = expand(qp->upoly, exp, total);
4034 free(exp);
4035 if (!qp->upoly)
4036 goto error;
4038 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4039 if (!qp->div)
4040 goto error;
4041 for (i = 0; i < qp->div->n_row; ++i)
4042 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4044 isl_space_free(qp->dim);
4045 qp->dim = dim;
4047 return qp;
4048 error:
4049 isl_space_free(dim);
4050 isl_qpolynomial_free(qp);
4051 return NULL;
4054 /* For each parameter or variable that does not appear in qp,
4055 * first eliminate the variable from all constraints and then set it to zero.
4057 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4058 __isl_keep isl_qpolynomial *qp)
4060 int *active = NULL;
4061 int i;
4062 int d;
4063 unsigned nparam;
4064 unsigned nvar;
4066 if (!set || !qp)
4067 goto error;
4069 d = isl_space_dim(set->dim, isl_dim_all);
4070 active = isl_calloc_array(set->ctx, int, d);
4071 if (set_active(qp, active) < 0)
4072 goto error;
4074 for (i = 0; i < d; ++i)
4075 if (!active[i])
4076 break;
4078 if (i == d) {
4079 free(active);
4080 return set;
4083 nparam = isl_space_dim(set->dim, isl_dim_param);
4084 nvar = isl_space_dim(set->dim, isl_dim_set);
4085 for (i = 0; i < nparam; ++i) {
4086 if (active[i])
4087 continue;
4088 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4089 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4091 for (i = 0; i < nvar; ++i) {
4092 if (active[nparam + i])
4093 continue;
4094 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4095 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4098 free(active);
4100 return set;
4101 error:
4102 free(active);
4103 isl_set_free(set);
4104 return NULL;
4107 struct isl_opt_data {
4108 isl_qpolynomial *qp;
4109 int first;
4110 isl_val *opt;
4111 int max;
4114 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4116 struct isl_opt_data *data = (struct isl_opt_data *)user;
4117 isl_val *val;
4119 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4120 if (data->first) {
4121 data->first = 0;
4122 data->opt = val;
4123 } else if (data->max) {
4124 data->opt = isl_val_max(data->opt, val);
4125 } else {
4126 data->opt = isl_val_min(data->opt, val);
4129 return isl_stat_ok;
4132 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4133 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4135 struct isl_opt_data data = { NULL, 1, NULL, max };
4137 if (!set || !qp)
4138 goto error;
4140 if (isl_upoly_is_cst(qp->upoly)) {
4141 isl_set_free(set);
4142 data.opt = isl_qpolynomial_get_constant_val(qp);
4143 isl_qpolynomial_free(qp);
4144 return data.opt;
4147 set = fix_inactive(set, qp);
4149 data.qp = qp;
4150 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4151 goto error;
4153 if (data.first)
4154 data.opt = isl_val_zero(isl_set_get_ctx(set));
4156 isl_set_free(set);
4157 isl_qpolynomial_free(qp);
4158 return data.opt;
4159 error:
4160 isl_set_free(set);
4161 isl_qpolynomial_free(qp);
4162 isl_val_free(data.opt);
4163 return NULL;
4166 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4167 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4169 int i;
4170 int n_sub;
4171 isl_ctx *ctx;
4172 struct isl_upoly **subs;
4173 isl_mat *mat, *diag;
4175 qp = isl_qpolynomial_cow(qp);
4176 if (!qp || !morph)
4177 goto error;
4179 ctx = qp->dim->ctx;
4180 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4182 n_sub = morph->inv->n_row - 1;
4183 if (morph->inv->n_row != morph->inv->n_col)
4184 n_sub += qp->div->n_row;
4185 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4186 if (n_sub && !subs)
4187 goto error;
4189 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4190 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4191 morph->inv->row[0][0], morph->inv->n_col);
4192 if (morph->inv->n_row != morph->inv->n_col)
4193 for (i = 0; i < qp->div->n_row; ++i)
4194 subs[morph->inv->n_row - 1 + i] =
4195 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4197 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4199 for (i = 0; i < n_sub; ++i)
4200 isl_upoly_free(subs[i]);
4201 free(subs);
4203 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4204 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4205 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4206 mat = isl_mat_diagonal(mat, diag);
4207 qp->div = isl_mat_product(qp->div, mat);
4208 isl_space_free(qp->dim);
4209 qp->dim = isl_space_copy(morph->ran->dim);
4211 if (!qp->upoly || !qp->div || !qp->dim)
4212 goto error;
4214 isl_morph_free(morph);
4216 return qp;
4217 error:
4218 isl_qpolynomial_free(qp);
4219 isl_morph_free(morph);
4220 return NULL;
4223 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4224 __isl_take isl_union_pw_qpolynomial *upwqp1,
4225 __isl_take isl_union_pw_qpolynomial *upwqp2)
4227 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4228 &isl_pw_qpolynomial_mul);
4231 /* Reorder the dimension of "qp" according to the given reordering.
4233 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4234 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4236 isl_space *space;
4238 qp = isl_qpolynomial_cow(qp);
4239 if (!qp)
4240 goto error;
4242 r = isl_reordering_extend(r, qp->div->n_row);
4243 if (!r)
4244 goto error;
4246 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4247 if (!qp->div)
4248 goto error;
4250 qp->upoly = reorder(qp->upoly, r->pos);
4251 if (!qp->upoly)
4252 goto error;
4254 space = isl_reordering_get_space(r);
4255 qp = isl_qpolynomial_reset_domain_space(qp, space);
4257 isl_reordering_free(r);
4258 return qp;
4259 error:
4260 isl_qpolynomial_free(qp);
4261 isl_reordering_free(r);
4262 return NULL;
4265 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4266 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4268 isl_bool equal_params;
4270 if (!qp || !model)
4271 goto error;
4273 equal_params = isl_space_has_equal_params(qp->dim, model);
4274 if (equal_params < 0)
4275 goto error;
4276 if (!equal_params) {
4277 isl_reordering *exp;
4279 exp = isl_parameter_alignment_reordering(qp->dim, model);
4280 exp = isl_reordering_extend_space(exp,
4281 isl_qpolynomial_get_domain_space(qp));
4282 qp = isl_qpolynomial_realign_domain(qp, exp);
4285 isl_space_free(model);
4286 return qp;
4287 error:
4288 isl_space_free(model);
4289 isl_qpolynomial_free(qp);
4290 return NULL;
4293 struct isl_split_periods_data {
4294 int max_periods;
4295 isl_pw_qpolynomial *res;
4298 /* Create a slice where the integer division "div" has the fixed value "v".
4299 * In particular, if "div" refers to floor(f/m), then create a slice
4301 * m v <= f <= m v + (m - 1)
4303 * or
4305 * f - m v >= 0
4306 * -f + m v + (m - 1) >= 0
4308 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4309 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4311 int total;
4312 isl_basic_set *bset = NULL;
4313 int k;
4315 if (!dim || !qp)
4316 goto error;
4318 total = isl_space_dim(dim, isl_dim_all);
4319 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4321 k = isl_basic_set_alloc_inequality(bset);
4322 if (k < 0)
4323 goto error;
4324 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4325 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4327 k = isl_basic_set_alloc_inequality(bset);
4328 if (k < 0)
4329 goto error;
4330 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4331 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4332 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4333 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4335 isl_space_free(dim);
4336 return isl_set_from_basic_set(bset);
4337 error:
4338 isl_basic_set_free(bset);
4339 isl_space_free(dim);
4340 return NULL;
4343 static isl_stat split_periods(__isl_take isl_set *set,
4344 __isl_take isl_qpolynomial *qp, void *user);
4346 /* Create a slice of the domain "set" such that integer division "div"
4347 * has the fixed value "v" and add the results to data->res,
4348 * replacing the integer division by "v" in "qp".
4350 static isl_stat set_div(__isl_take isl_set *set,
4351 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4352 struct isl_split_periods_data *data)
4354 int i;
4355 int total;
4356 isl_set *slice;
4357 struct isl_upoly *cst;
4359 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4360 set = isl_set_intersect(set, slice);
4362 if (!qp)
4363 goto error;
4365 total = isl_space_dim(qp->dim, isl_dim_all);
4367 for (i = div + 1; i < qp->div->n_row; ++i) {
4368 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4369 continue;
4370 isl_int_addmul(qp->div->row[i][1],
4371 qp->div->row[i][2 + total + div], v);
4372 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4375 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4376 qp = substitute_div(qp, div, cst);
4378 return split_periods(set, qp, data);
4379 error:
4380 isl_set_free(set);
4381 isl_qpolynomial_free(qp);
4382 return isl_stat_error;
4385 /* Split the domain "set" such that integer division "div"
4386 * has a fixed value (ranging from "min" to "max") on each slice
4387 * and add the results to data->res.
4389 static isl_stat split_div(__isl_take isl_set *set,
4390 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4391 struct isl_split_periods_data *data)
4393 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4394 isl_set *set_i = isl_set_copy(set);
4395 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4397 if (set_div(set_i, qp_i, div, min, data) < 0)
4398 goto error;
4400 isl_set_free(set);
4401 isl_qpolynomial_free(qp);
4402 return isl_stat_ok;
4403 error:
4404 isl_set_free(set);
4405 isl_qpolynomial_free(qp);
4406 return isl_stat_error;
4409 /* If "qp" refers to any integer division
4410 * that can only attain "max_periods" distinct values on "set"
4411 * then split the domain along those distinct values.
4412 * Add the results (or the original if no splitting occurs)
4413 * to data->res.
4415 static isl_stat split_periods(__isl_take isl_set *set,
4416 __isl_take isl_qpolynomial *qp, void *user)
4418 int i;
4419 isl_pw_qpolynomial *pwqp;
4420 struct isl_split_periods_data *data;
4421 isl_int min, max;
4422 int total;
4423 isl_stat r = isl_stat_ok;
4425 data = (struct isl_split_periods_data *)user;
4427 if (!set || !qp)
4428 goto error;
4430 if (qp->div->n_row == 0) {
4431 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4432 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4433 return isl_stat_ok;
4436 isl_int_init(min);
4437 isl_int_init(max);
4438 total = isl_space_dim(qp->dim, isl_dim_all);
4439 for (i = 0; i < qp->div->n_row; ++i) {
4440 enum isl_lp_result lp_res;
4442 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4443 qp->div->n_row) != -1)
4444 continue;
4446 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4447 set->ctx->one, &min, NULL, NULL);
4448 if (lp_res == isl_lp_error)
4449 goto error2;
4450 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4451 continue;
4452 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4454 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4455 set->ctx->one, &max, NULL, NULL);
4456 if (lp_res == isl_lp_error)
4457 goto error2;
4458 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4459 continue;
4460 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4462 isl_int_sub(max, max, min);
4463 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4464 isl_int_add(max, max, min);
4465 break;
4469 if (i < qp->div->n_row) {
4470 r = split_div(set, qp, i, min, max, data);
4471 } else {
4472 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4473 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4476 isl_int_clear(max);
4477 isl_int_clear(min);
4479 return r;
4480 error2:
4481 isl_int_clear(max);
4482 isl_int_clear(min);
4483 error:
4484 isl_set_free(set);
4485 isl_qpolynomial_free(qp);
4486 return isl_stat_error;
4489 /* If any quasi-polynomial in pwqp refers to any integer division
4490 * that can only attain "max_periods" distinct values on its domain
4491 * then split the domain along those distinct values.
4493 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4494 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4496 struct isl_split_periods_data data;
4498 data.max_periods = max_periods;
4499 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4501 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4502 goto error;
4504 isl_pw_qpolynomial_free(pwqp);
4506 return data.res;
4507 error:
4508 isl_pw_qpolynomial_free(data.res);
4509 isl_pw_qpolynomial_free(pwqp);
4510 return NULL;
4513 /* Construct a piecewise quasipolynomial that is constant on the given
4514 * domain. In particular, it is
4515 * 0 if cst == 0
4516 * 1 if cst == 1
4517 * infinity if cst == -1
4519 * If cst == -1, then explicitly check whether the domain is empty and,
4520 * if so, return 0 instead.
4522 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4523 __isl_take isl_basic_set *bset, int cst)
4525 isl_space *dim;
4526 isl_qpolynomial *qp;
4528 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4529 cst = 0;
4530 if (!bset)
4531 return NULL;
4533 bset = isl_basic_set_params(bset);
4534 dim = isl_basic_set_get_space(bset);
4535 if (cst < 0)
4536 qp = isl_qpolynomial_infty_on_domain(dim);
4537 else if (cst == 0)
4538 qp = isl_qpolynomial_zero_on_domain(dim);
4539 else
4540 qp = isl_qpolynomial_one_on_domain(dim);
4541 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4544 /* Factor bset, call fn on each of the factors and return the product.
4546 * If no factors can be found, simply call fn on the input.
4547 * Otherwise, construct the factors based on the factorizer,
4548 * call fn on each factor and compute the product.
4550 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4551 __isl_take isl_basic_set *bset,
4552 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4554 int i, n;
4555 isl_space *space;
4556 isl_set *set;
4557 isl_factorizer *f;
4558 isl_qpolynomial *qp;
4559 isl_pw_qpolynomial *pwqp;
4560 unsigned nparam;
4561 unsigned nvar;
4563 f = isl_basic_set_factorizer(bset);
4564 if (!f)
4565 goto error;
4566 if (f->n_group == 0) {
4567 isl_factorizer_free(f);
4568 return fn(bset);
4571 nparam = isl_basic_set_dim(bset, isl_dim_param);
4572 nvar = isl_basic_set_dim(bset, isl_dim_set);
4574 space = isl_basic_set_get_space(bset);
4575 space = isl_space_params(space);
4576 set = isl_set_universe(isl_space_copy(space));
4577 qp = isl_qpolynomial_one_on_domain(space);
4578 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4580 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4582 for (i = 0, n = 0; i < f->n_group; ++i) {
4583 isl_basic_set *bset_i;
4584 isl_pw_qpolynomial *pwqp_i;
4586 bset_i = isl_basic_set_copy(bset);
4587 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4588 nparam + n + f->len[i], nvar - n - f->len[i]);
4589 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4590 nparam, n);
4591 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4592 n + f->len[i], nvar - n - f->len[i]);
4593 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4595 pwqp_i = fn(bset_i);
4596 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4598 n += f->len[i];
4601 isl_basic_set_free(bset);
4602 isl_factorizer_free(f);
4604 return pwqp;
4605 error:
4606 isl_basic_set_free(bset);
4607 return NULL;
4610 /* Factor bset, call fn on each of the factors and return the product.
4611 * The function is assumed to evaluate to zero on empty domains,
4612 * to one on zero-dimensional domains and to infinity on unbounded domains
4613 * and will not be called explicitly on zero-dimensional or unbounded domains.
4615 * We first check for some special cases and remove all equalities.
4616 * Then we hand over control to compressed_multiplicative_call.
4618 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4619 __isl_take isl_basic_set *bset,
4620 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4622 isl_bool bounded;
4623 isl_morph *morph;
4624 isl_pw_qpolynomial *pwqp;
4626 if (!bset)
4627 return NULL;
4629 if (isl_basic_set_plain_is_empty(bset))
4630 return constant_on_domain(bset, 0);
4632 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4633 return constant_on_domain(bset, 1);
4635 bounded = isl_basic_set_is_bounded(bset);
4636 if (bounded < 0)
4637 goto error;
4638 if (!bounded)
4639 return constant_on_domain(bset, -1);
4641 if (bset->n_eq == 0)
4642 return compressed_multiplicative_call(bset, fn);
4644 morph = isl_basic_set_full_compression(bset);
4645 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4647 pwqp = compressed_multiplicative_call(bset, fn);
4649 morph = isl_morph_dom_params(morph);
4650 morph = isl_morph_ran_params(morph);
4651 morph = isl_morph_inverse(morph);
4653 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4655 return pwqp;
4656 error:
4657 isl_basic_set_free(bset);
4658 return NULL;
4661 /* Drop all floors in "qp", turning each integer division [a/m] into
4662 * a rational division a/m. If "down" is set, then the integer division
4663 * is replaced by (a-(m-1))/m instead.
4665 static __isl_give isl_qpolynomial *qp_drop_floors(
4666 __isl_take isl_qpolynomial *qp, int down)
4668 int i;
4669 struct isl_upoly *s;
4671 if (!qp)
4672 return NULL;
4673 if (qp->div->n_row == 0)
4674 return qp;
4676 qp = isl_qpolynomial_cow(qp);
4677 if (!qp)
4678 return NULL;
4680 for (i = qp->div->n_row - 1; i >= 0; --i) {
4681 if (down) {
4682 isl_int_sub(qp->div->row[i][1],
4683 qp->div->row[i][1], qp->div->row[i][0]);
4684 isl_int_add_ui(qp->div->row[i][1],
4685 qp->div->row[i][1], 1);
4687 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4688 qp->div->row[i][0], qp->div->n_col - 1);
4689 qp = substitute_div(qp, i, s);
4690 if (!qp)
4691 return NULL;
4694 return qp;
4697 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4698 * a rational division a/m.
4700 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4701 __isl_take isl_pw_qpolynomial *pwqp)
4703 int i;
4705 if (!pwqp)
4706 return NULL;
4708 if (isl_pw_qpolynomial_is_zero(pwqp))
4709 return pwqp;
4711 pwqp = isl_pw_qpolynomial_cow(pwqp);
4712 if (!pwqp)
4713 return NULL;
4715 for (i = 0; i < pwqp->n; ++i) {
4716 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4717 if (!pwqp->p[i].qp)
4718 goto error;
4721 return pwqp;
4722 error:
4723 isl_pw_qpolynomial_free(pwqp);
4724 return NULL;
4727 /* Adjust all the integer divisions in "qp" such that they are at least
4728 * one over the given orthant (identified by "signs"). This ensures
4729 * that they will still be non-negative even after subtracting (m-1)/m.
4731 * In particular, f is replaced by f' + v, changing f = [a/m]
4732 * to f' = [(a - m v)/m].
4733 * If the constant term k in a is smaller than m,
4734 * the constant term of v is set to floor(k/m) - 1.
4735 * For any other term, if the coefficient c and the variable x have
4736 * the same sign, then no changes are needed.
4737 * Otherwise, if the variable is positive (and c is negative),
4738 * then the coefficient of x in v is set to floor(c/m).
4739 * If the variable is negative (and c is positive),
4740 * then the coefficient of x in v is set to ceil(c/m).
4742 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4743 int *signs)
4745 int i, j;
4746 int total;
4747 isl_vec *v = NULL;
4748 struct isl_upoly *s;
4750 qp = isl_qpolynomial_cow(qp);
4751 if (!qp)
4752 return NULL;
4753 qp->div = isl_mat_cow(qp->div);
4754 if (!qp->div)
4755 goto error;
4757 total = isl_space_dim(qp->dim, isl_dim_all);
4758 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4760 for (i = 0; i < qp->div->n_row; ++i) {
4761 isl_int *row = qp->div->row[i];
4762 v = isl_vec_clr(v);
4763 if (!v)
4764 goto error;
4765 if (isl_int_lt(row[1], row[0])) {
4766 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4767 isl_int_sub_ui(v->el[0], v->el[0], 1);
4768 isl_int_submul(row[1], row[0], v->el[0]);
4770 for (j = 0; j < total; ++j) {
4771 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4772 continue;
4773 if (signs[j] < 0)
4774 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4775 else
4776 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4777 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4779 for (j = 0; j < i; ++j) {
4780 if (isl_int_sgn(row[2 + total + j]) >= 0)
4781 continue;
4782 isl_int_fdiv_q(v->el[1 + total + j],
4783 row[2 + total + j], row[0]);
4784 isl_int_submul(row[2 + total + j],
4785 row[0], v->el[1 + total + j]);
4787 for (j = i + 1; j < qp->div->n_row; ++j) {
4788 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4789 continue;
4790 isl_seq_combine(qp->div->row[j] + 1,
4791 qp->div->ctx->one, qp->div->row[j] + 1,
4792 qp->div->row[j][2 + total + i], v->el, v->size);
4794 isl_int_set_si(v->el[1 + total + i], 1);
4795 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4796 qp->div->ctx->one, v->size);
4797 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4798 isl_upoly_free(s);
4799 if (!qp->upoly)
4800 goto error;
4803 isl_vec_free(v);
4804 return qp;
4805 error:
4806 isl_vec_free(v);
4807 isl_qpolynomial_free(qp);
4808 return NULL;
4811 struct isl_to_poly_data {
4812 int sign;
4813 isl_pw_qpolynomial *res;
4814 isl_qpolynomial *qp;
4817 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4818 * We first make all integer divisions positive and then split the
4819 * quasipolynomials into terms with sign data->sign (the direction
4820 * of the requested approximation) and terms with the opposite sign.
4821 * In the first set of terms, each integer division [a/m] is
4822 * overapproximated by a/m, while in the second it is underapproximated
4823 * by (a-(m-1))/m.
4825 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
4826 int *signs, void *user)
4828 struct isl_to_poly_data *data = user;
4829 isl_pw_qpolynomial *t;
4830 isl_qpolynomial *qp, *up, *down;
4832 qp = isl_qpolynomial_copy(data->qp);
4833 qp = make_divs_pos(qp, signs);
4835 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4836 up = qp_drop_floors(up, 0);
4837 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4838 down = qp_drop_floors(down, 1);
4840 isl_qpolynomial_free(qp);
4841 qp = isl_qpolynomial_add(up, down);
4843 t = isl_pw_qpolynomial_alloc(orthant, qp);
4844 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4846 return isl_stat_ok;
4849 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4850 * the polynomial will be an overapproximation. If "sign" is negative,
4851 * it will be an underapproximation. If "sign" is zero, the approximation
4852 * will lie somewhere in between.
4854 * In particular, is sign == 0, we simply drop the floors, turning
4855 * the integer divisions into rational divisions.
4856 * Otherwise, we split the domains into orthants, make all integer divisions
4857 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4858 * depending on the requested sign and the sign of the term in which
4859 * the integer division appears.
4861 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4862 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4864 int i;
4865 struct isl_to_poly_data data;
4867 if (sign == 0)
4868 return pwqp_drop_floors(pwqp);
4870 if (!pwqp)
4871 return NULL;
4873 data.sign = sign;
4874 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4876 for (i = 0; i < pwqp->n; ++i) {
4877 if (pwqp->p[i].qp->div->n_row == 0) {
4878 isl_pw_qpolynomial *t;
4879 t = isl_pw_qpolynomial_alloc(
4880 isl_set_copy(pwqp->p[i].set),
4881 isl_qpolynomial_copy(pwqp->p[i].qp));
4882 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4883 continue;
4885 data.qp = pwqp->p[i].qp;
4886 if (isl_set_foreach_orthant(pwqp->p[i].set,
4887 &to_polynomial_on_orthant, &data) < 0)
4888 goto error;
4891 isl_pw_qpolynomial_free(pwqp);
4893 return data.res;
4894 error:
4895 isl_pw_qpolynomial_free(pwqp);
4896 isl_pw_qpolynomial_free(data.res);
4897 return NULL;
4900 static __isl_give isl_pw_qpolynomial *poly_entry(
4901 __isl_take isl_pw_qpolynomial *pwqp, void *user)
4903 int *sign = user;
4905 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
4908 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4909 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4911 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
4912 &poly_entry, &sign);
4915 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4916 __isl_take isl_qpolynomial *qp)
4918 int i, k;
4919 isl_space *dim;
4920 isl_vec *aff = NULL;
4921 isl_basic_map *bmap = NULL;
4922 unsigned pos;
4923 unsigned n_div;
4925 if (!qp)
4926 return NULL;
4927 if (!isl_upoly_is_affine(qp->upoly))
4928 isl_die(qp->dim->ctx, isl_error_invalid,
4929 "input quasi-polynomial not affine", goto error);
4930 aff = isl_qpolynomial_extract_affine(qp);
4931 if (!aff)
4932 goto error;
4933 dim = isl_qpolynomial_get_space(qp);
4934 pos = 1 + isl_space_offset(dim, isl_dim_out);
4935 n_div = qp->div->n_row;
4936 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4938 for (i = 0; i < n_div; ++i) {
4939 k = isl_basic_map_alloc_div(bmap);
4940 if (k < 0)
4941 goto error;
4942 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4943 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4944 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4945 goto error;
4947 k = isl_basic_map_alloc_equality(bmap);
4948 if (k < 0)
4949 goto error;
4950 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4951 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4952 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4954 isl_vec_free(aff);
4955 isl_qpolynomial_free(qp);
4956 bmap = isl_basic_map_finalize(bmap);
4957 return bmap;
4958 error:
4959 isl_vec_free(aff);
4960 isl_qpolynomial_free(qp);
4961 isl_basic_map_free(bmap);
4962 return NULL;