extract out shared isl_space_drop_all_params
[isl.git] / isl_polynomial.c
blob4ce69972a57e606eb8829de27a7d0240fcbd96f2
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 /* Return a copy of the local space on which "qp" is defined.
446 static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
447 __isl_keep isl_qpolynomial *qp)
449 isl_space *space;
451 if (!qp)
452 return NULL;
454 space = isl_qpolynomial_get_domain_space(qp);
455 return isl_local_space_alloc_div(space, isl_mat_copy(qp->div));
458 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
460 isl_space *space;
461 if (!qp)
462 return NULL;
463 space = isl_space_copy(qp->dim);
464 space = isl_space_from_domain(space);
465 space = isl_space_add_dims(space, isl_dim_out, 1);
466 return space;
469 /* Return the number of variables of the given type in the domain of "qp".
471 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
472 enum isl_dim_type type)
474 if (!qp)
475 return 0;
476 if (type == isl_dim_div)
477 return qp->div->n_row;
478 if (type == isl_dim_all)
479 return isl_space_dim(qp->dim, isl_dim_all) +
480 isl_qpolynomial_domain_dim(qp, isl_dim_div);
481 return isl_space_dim(qp->dim, type);
484 /* Given the type of a dimension of an isl_qpolynomial,
485 * return the type of the corresponding dimension in its domain.
486 * This function is only called for "type" equal to isl_dim_in or
487 * isl_dim_param.
489 static enum isl_dim_type domain_type(enum isl_dim_type type)
491 return type == isl_dim_in ? isl_dim_set : type;
494 /* Externally, an isl_qpolynomial has a map space, but internally, the
495 * ls field corresponds to the domain of that space.
497 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
498 enum isl_dim_type type)
500 if (!qp)
501 return 0;
502 if (type == isl_dim_out)
503 return 1;
504 type = domain_type(type);
505 return isl_qpolynomial_domain_dim(qp, type);
508 /* Return the offset of the first coefficient of type "type" in
509 * the domain of "qp".
511 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
512 enum isl_dim_type type)
514 if (!qp)
515 return 0;
516 switch (type) {
517 case isl_dim_cst:
518 return 0;
519 case isl_dim_param:
520 case isl_dim_set:
521 return 1 + isl_space_offset(qp->dim, type);
522 case isl_dim_div:
523 return 1 + isl_space_dim(qp->dim, isl_dim_all);
524 default:
525 return 0;
529 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
531 return qp ? isl_upoly_is_zero(qp->upoly) : isl_bool_error;
534 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
536 return qp ? isl_upoly_is_one(qp->upoly) : isl_bool_error;
539 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
541 return qp ? isl_upoly_is_nan(qp->upoly) : isl_bool_error;
544 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
546 return qp ? isl_upoly_is_infty(qp->upoly) : isl_bool_error;
549 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
551 return qp ? isl_upoly_is_neginfty(qp->upoly) : isl_bool_error;
554 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
556 return qp ? isl_upoly_sgn(qp->upoly) : 0;
559 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
561 isl_int_clear(cst->n);
562 isl_int_clear(cst->d);
565 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
567 int i;
569 for (i = 0; i < rec->n; ++i)
570 isl_upoly_free(rec->p[i]);
573 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
575 if (!up)
576 return NULL;
578 up->ref++;
579 return up;
582 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
584 struct isl_upoly_cst *cst;
585 struct isl_upoly_cst *dup;
587 cst = isl_upoly_as_cst(up);
588 if (!cst)
589 return NULL;
591 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
592 if (!dup)
593 return NULL;
594 isl_int_set(dup->n, cst->n);
595 isl_int_set(dup->d, cst->d);
597 return &dup->up;
600 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
602 int i;
603 struct isl_upoly_rec *rec;
604 struct isl_upoly_rec *dup;
606 rec = isl_upoly_as_rec(up);
607 if (!rec)
608 return NULL;
610 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
611 if (!dup)
612 return NULL;
614 for (i = 0; i < rec->n; ++i) {
615 dup->p[i] = isl_upoly_copy(rec->p[i]);
616 if (!dup->p[i])
617 goto error;
618 dup->n++;
621 return &dup->up;
622 error:
623 isl_upoly_free(&dup->up);
624 return NULL;
627 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
629 if (!up)
630 return NULL;
632 if (isl_upoly_is_cst(up))
633 return isl_upoly_dup_cst(up);
634 else
635 return isl_upoly_dup_rec(up);
638 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
640 if (!up)
641 return NULL;
643 if (up->ref == 1)
644 return up;
645 up->ref--;
646 return isl_upoly_dup(up);
649 __isl_null struct isl_upoly *isl_upoly_free(__isl_take struct isl_upoly *up)
651 if (!up)
652 return NULL;
654 if (--up->ref > 0)
655 return NULL;
657 if (up->var < 0)
658 upoly_free_cst((struct isl_upoly_cst *)up);
659 else
660 upoly_free_rec((struct isl_upoly_rec *)up);
662 isl_ctx_deref(up->ctx);
663 free(up);
664 return NULL;
667 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
669 isl_int gcd;
671 isl_int_init(gcd);
672 isl_int_gcd(gcd, cst->n, cst->d);
673 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
674 isl_int_divexact(cst->n, cst->n, gcd);
675 isl_int_divexact(cst->d, cst->d, gcd);
677 isl_int_clear(gcd);
680 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
681 __isl_take struct isl_upoly *up2)
683 struct isl_upoly_cst *cst1;
684 struct isl_upoly_cst *cst2;
686 up1 = isl_upoly_cow(up1);
687 if (!up1 || !up2)
688 goto error;
690 cst1 = isl_upoly_as_cst(up1);
691 cst2 = isl_upoly_as_cst(up2);
693 if (isl_int_eq(cst1->d, cst2->d))
694 isl_int_add(cst1->n, cst1->n, cst2->n);
695 else {
696 isl_int_mul(cst1->n, cst1->n, cst2->d);
697 isl_int_addmul(cst1->n, cst2->n, cst1->d);
698 isl_int_mul(cst1->d, cst1->d, cst2->d);
701 isl_upoly_cst_reduce(cst1);
703 isl_upoly_free(up2);
704 return up1;
705 error:
706 isl_upoly_free(up1);
707 isl_upoly_free(up2);
708 return NULL;
711 static __isl_give struct isl_upoly *replace_by_zero(
712 __isl_take struct isl_upoly *up)
714 struct isl_ctx *ctx;
716 if (!up)
717 return NULL;
718 ctx = up->ctx;
719 isl_upoly_free(up);
720 return isl_upoly_zero(ctx);
723 static __isl_give struct isl_upoly *replace_by_constant_term(
724 __isl_take struct isl_upoly *up)
726 struct isl_upoly_rec *rec;
727 struct isl_upoly *cst;
729 if (!up)
730 return NULL;
732 rec = isl_upoly_as_rec(up);
733 if (!rec)
734 goto error;
735 cst = isl_upoly_copy(rec->p[0]);
736 isl_upoly_free(up);
737 return cst;
738 error:
739 isl_upoly_free(up);
740 return NULL;
743 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
744 __isl_take struct isl_upoly *up2)
746 int i;
747 struct isl_upoly_rec *rec1, *rec2;
749 if (!up1 || !up2)
750 goto error;
752 if (isl_upoly_is_nan(up1)) {
753 isl_upoly_free(up2);
754 return up1;
757 if (isl_upoly_is_nan(up2)) {
758 isl_upoly_free(up1);
759 return up2;
762 if (isl_upoly_is_zero(up1)) {
763 isl_upoly_free(up1);
764 return up2;
767 if (isl_upoly_is_zero(up2)) {
768 isl_upoly_free(up2);
769 return up1;
772 if (up1->var < up2->var)
773 return isl_upoly_sum(up2, up1);
775 if (up2->var < up1->var) {
776 struct isl_upoly_rec *rec;
777 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
778 isl_upoly_free(up1);
779 return up2;
781 up1 = isl_upoly_cow(up1);
782 rec = isl_upoly_as_rec(up1);
783 if (!rec)
784 goto error;
785 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
786 if (rec->n == 1)
787 up1 = replace_by_constant_term(up1);
788 return up1;
791 if (isl_upoly_is_cst(up1))
792 return isl_upoly_sum_cst(up1, up2);
794 rec1 = isl_upoly_as_rec(up1);
795 rec2 = isl_upoly_as_rec(up2);
796 if (!rec1 || !rec2)
797 goto error;
799 if (rec1->n < rec2->n)
800 return isl_upoly_sum(up2, up1);
802 up1 = isl_upoly_cow(up1);
803 rec1 = isl_upoly_as_rec(up1);
804 if (!rec1)
805 goto error;
807 for (i = rec2->n - 1; i >= 0; --i) {
808 rec1->p[i] = isl_upoly_sum(rec1->p[i],
809 isl_upoly_copy(rec2->p[i]));
810 if (!rec1->p[i])
811 goto error;
812 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
813 isl_upoly_free(rec1->p[i]);
814 rec1->n--;
818 if (rec1->n == 0)
819 up1 = replace_by_zero(up1);
820 else if (rec1->n == 1)
821 up1 = replace_by_constant_term(up1);
823 isl_upoly_free(up2);
825 return up1;
826 error:
827 isl_upoly_free(up1);
828 isl_upoly_free(up2);
829 return NULL;
832 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
833 __isl_take struct isl_upoly *up, isl_int v)
835 struct isl_upoly_cst *cst;
837 up = isl_upoly_cow(up);
838 if (!up)
839 return NULL;
841 cst = isl_upoly_as_cst(up);
843 isl_int_addmul(cst->n, cst->d, v);
845 return up;
848 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
849 __isl_take struct isl_upoly *up, isl_int v)
851 struct isl_upoly_rec *rec;
853 if (!up)
854 return NULL;
856 if (isl_upoly_is_cst(up))
857 return isl_upoly_cst_add_isl_int(up, v);
859 up = isl_upoly_cow(up);
860 rec = isl_upoly_as_rec(up);
861 if (!rec)
862 goto error;
864 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
865 if (!rec->p[0])
866 goto error;
868 return up;
869 error:
870 isl_upoly_free(up);
871 return NULL;
874 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
875 __isl_take struct isl_upoly *up, isl_int v)
877 struct isl_upoly_cst *cst;
879 if (isl_upoly_is_zero(up))
880 return up;
882 up = isl_upoly_cow(up);
883 if (!up)
884 return NULL;
886 cst = isl_upoly_as_cst(up);
888 isl_int_mul(cst->n, cst->n, v);
890 return up;
893 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
894 __isl_take struct isl_upoly *up, isl_int v)
896 int i;
897 struct isl_upoly_rec *rec;
899 if (!up)
900 return NULL;
902 if (isl_upoly_is_cst(up))
903 return isl_upoly_cst_mul_isl_int(up, v);
905 up = isl_upoly_cow(up);
906 rec = isl_upoly_as_rec(up);
907 if (!rec)
908 goto error;
910 for (i = 0; i < rec->n; ++i) {
911 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
912 if (!rec->p[i])
913 goto error;
916 return up;
917 error:
918 isl_upoly_free(up);
919 return NULL;
922 /* Multiply the constant polynomial "up" by "v".
924 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
925 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
927 struct isl_upoly_cst *cst;
929 if (isl_upoly_is_zero(up))
930 return up;
932 up = isl_upoly_cow(up);
933 if (!up)
934 return NULL;
936 cst = isl_upoly_as_cst(up);
938 isl_int_mul(cst->n, cst->n, v->n);
939 isl_int_mul(cst->d, cst->d, v->d);
940 isl_upoly_cst_reduce(cst);
942 return up;
945 /* Multiply the polynomial "up" by "v".
947 static __isl_give struct isl_upoly *isl_upoly_scale_val(
948 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
950 int i;
951 struct isl_upoly_rec *rec;
953 if (!up)
954 return NULL;
956 if (isl_upoly_is_cst(up))
957 return isl_upoly_cst_scale_val(up, v);
959 up = isl_upoly_cow(up);
960 rec = isl_upoly_as_rec(up);
961 if (!rec)
962 goto error;
964 for (i = 0; i < rec->n; ++i) {
965 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
966 if (!rec->p[i])
967 goto error;
970 return up;
971 error:
972 isl_upoly_free(up);
973 return NULL;
976 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
977 __isl_take struct isl_upoly *up2)
979 struct isl_upoly_cst *cst1;
980 struct isl_upoly_cst *cst2;
982 up1 = isl_upoly_cow(up1);
983 if (!up1 || !up2)
984 goto error;
986 cst1 = isl_upoly_as_cst(up1);
987 cst2 = isl_upoly_as_cst(up2);
989 isl_int_mul(cst1->n, cst1->n, cst2->n);
990 isl_int_mul(cst1->d, cst1->d, cst2->d);
992 isl_upoly_cst_reduce(cst1);
994 isl_upoly_free(up2);
995 return up1;
996 error:
997 isl_upoly_free(up1);
998 isl_upoly_free(up2);
999 return NULL;
1002 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
1003 __isl_take struct isl_upoly *up2)
1005 struct isl_upoly_rec *rec1;
1006 struct isl_upoly_rec *rec2;
1007 struct isl_upoly_rec *res = NULL;
1008 int i, j;
1009 int size;
1011 rec1 = isl_upoly_as_rec(up1);
1012 rec2 = isl_upoly_as_rec(up2);
1013 if (!rec1 || !rec2)
1014 goto error;
1015 size = rec1->n + rec2->n - 1;
1016 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
1017 if (!res)
1018 goto error;
1020 for (i = 0; i < rec1->n; ++i) {
1021 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
1022 isl_upoly_copy(rec1->p[i]));
1023 if (!res->p[i])
1024 goto error;
1025 res->n++;
1027 for (; i < size; ++i) {
1028 res->p[i] = isl_upoly_zero(up1->ctx);
1029 if (!res->p[i])
1030 goto error;
1031 res->n++;
1033 for (i = 0; i < rec1->n; ++i) {
1034 for (j = 1; j < rec2->n; ++j) {
1035 struct isl_upoly *up;
1036 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
1037 isl_upoly_copy(rec1->p[i]));
1038 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
1039 if (!res->p[i + j])
1040 goto error;
1044 isl_upoly_free(up1);
1045 isl_upoly_free(up2);
1047 return &res->up;
1048 error:
1049 isl_upoly_free(up1);
1050 isl_upoly_free(up2);
1051 isl_upoly_free(&res->up);
1052 return NULL;
1055 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
1056 __isl_take struct isl_upoly *up2)
1058 if (!up1 || !up2)
1059 goto error;
1061 if (isl_upoly_is_nan(up1)) {
1062 isl_upoly_free(up2);
1063 return up1;
1066 if (isl_upoly_is_nan(up2)) {
1067 isl_upoly_free(up1);
1068 return up2;
1071 if (isl_upoly_is_zero(up1)) {
1072 isl_upoly_free(up2);
1073 return up1;
1076 if (isl_upoly_is_zero(up2)) {
1077 isl_upoly_free(up1);
1078 return up2;
1081 if (isl_upoly_is_one(up1)) {
1082 isl_upoly_free(up1);
1083 return up2;
1086 if (isl_upoly_is_one(up2)) {
1087 isl_upoly_free(up2);
1088 return up1;
1091 if (up1->var < up2->var)
1092 return isl_upoly_mul(up2, up1);
1094 if (up2->var < up1->var) {
1095 int i;
1096 struct isl_upoly_rec *rec;
1097 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
1098 isl_ctx *ctx = up1->ctx;
1099 isl_upoly_free(up1);
1100 isl_upoly_free(up2);
1101 return isl_upoly_nan(ctx);
1103 up1 = isl_upoly_cow(up1);
1104 rec = isl_upoly_as_rec(up1);
1105 if (!rec)
1106 goto error;
1108 for (i = 0; i < rec->n; ++i) {
1109 rec->p[i] = isl_upoly_mul(rec->p[i],
1110 isl_upoly_copy(up2));
1111 if (!rec->p[i])
1112 goto error;
1114 isl_upoly_free(up2);
1115 return up1;
1118 if (isl_upoly_is_cst(up1))
1119 return isl_upoly_mul_cst(up1, up2);
1121 return isl_upoly_mul_rec(up1, up2);
1122 error:
1123 isl_upoly_free(up1);
1124 isl_upoly_free(up2);
1125 return NULL;
1128 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1129 unsigned power)
1131 struct isl_upoly *res;
1133 if (!up)
1134 return NULL;
1135 if (power == 1)
1136 return up;
1138 if (power % 2)
1139 res = isl_upoly_copy(up);
1140 else
1141 res = isl_upoly_one(up->ctx);
1143 while (power >>= 1) {
1144 up = isl_upoly_mul(up, isl_upoly_copy(up));
1145 if (power % 2)
1146 res = isl_upoly_mul(res, isl_upoly_copy(up));
1149 isl_upoly_free(up);
1150 return res;
1153 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1154 unsigned n_div, __isl_take struct isl_upoly *up)
1156 struct isl_qpolynomial *qp = NULL;
1157 unsigned total;
1159 if (!space || !up)
1160 goto error;
1162 if (!isl_space_is_set(space))
1163 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1164 "domain of polynomial should be a set", goto error);
1166 total = isl_space_dim(space, isl_dim_all);
1168 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1169 if (!qp)
1170 goto error;
1172 qp->ref = 1;
1173 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1174 if (!qp->div)
1175 goto error;
1177 qp->dim = space;
1178 qp->upoly = up;
1180 return qp;
1181 error:
1182 isl_space_free(space);
1183 isl_upoly_free(up);
1184 isl_qpolynomial_free(qp);
1185 return NULL;
1188 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1190 if (!qp)
1191 return NULL;
1193 qp->ref++;
1194 return qp;
1197 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1199 struct isl_qpolynomial *dup;
1201 if (!qp)
1202 return NULL;
1204 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1205 isl_upoly_copy(qp->upoly));
1206 if (!dup)
1207 return NULL;
1208 isl_mat_free(dup->div);
1209 dup->div = isl_mat_copy(qp->div);
1210 if (!dup->div)
1211 goto error;
1213 return dup;
1214 error:
1215 isl_qpolynomial_free(dup);
1216 return NULL;
1219 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1221 if (!qp)
1222 return NULL;
1224 if (qp->ref == 1)
1225 return qp;
1226 qp->ref--;
1227 return isl_qpolynomial_dup(qp);
1230 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1231 __isl_take isl_qpolynomial *qp)
1233 if (!qp)
1234 return NULL;
1236 if (--qp->ref > 0)
1237 return NULL;
1239 isl_space_free(qp->dim);
1240 isl_mat_free(qp->div);
1241 isl_upoly_free(qp->upoly);
1243 free(qp);
1244 return NULL;
1247 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1249 int i;
1250 struct isl_upoly_rec *rec;
1251 struct isl_upoly_cst *cst;
1253 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1254 if (!rec)
1255 return NULL;
1256 for (i = 0; i < 1 + power; ++i) {
1257 rec->p[i] = isl_upoly_zero(ctx);
1258 if (!rec->p[i])
1259 goto error;
1260 rec->n++;
1262 cst = isl_upoly_as_cst(rec->p[power]);
1263 isl_int_set_si(cst->n, 1);
1265 return &rec->up;
1266 error:
1267 isl_upoly_free(&rec->up);
1268 return NULL;
1271 /* r array maps original positions to new positions.
1273 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1274 int *r)
1276 int i;
1277 struct isl_upoly_rec *rec;
1278 struct isl_upoly *base;
1279 struct isl_upoly *res;
1281 if (isl_upoly_is_cst(up))
1282 return up;
1284 rec = isl_upoly_as_rec(up);
1285 if (!rec)
1286 goto error;
1288 isl_assert(up->ctx, rec->n >= 1, goto error);
1290 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1291 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1293 for (i = rec->n - 2; i >= 0; --i) {
1294 res = isl_upoly_mul(res, isl_upoly_copy(base));
1295 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1298 isl_upoly_free(base);
1299 isl_upoly_free(up);
1301 return res;
1302 error:
1303 isl_upoly_free(up);
1304 return NULL;
1307 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1308 __isl_keep isl_mat *div2)
1310 int n_row, n_col;
1311 isl_bool equal;
1313 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1314 div1->n_col >= div2->n_col,
1315 return isl_bool_error);
1317 if (div1->n_row == div2->n_row)
1318 return isl_mat_is_equal(div1, div2);
1320 n_row = div1->n_row;
1321 n_col = div1->n_col;
1322 div1->n_row = div2->n_row;
1323 div1->n_col = div2->n_col;
1325 equal = isl_mat_is_equal(div1, div2);
1327 div1->n_row = n_row;
1328 div1->n_col = n_col;
1330 return equal;
1333 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1335 int li, lj;
1337 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1338 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1340 if (li != lj)
1341 return li - lj;
1343 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1346 struct isl_div_sort_info {
1347 isl_mat *div;
1348 int row;
1351 static int div_sort_cmp(const void *p1, const void *p2)
1353 const struct isl_div_sort_info *i1, *i2;
1354 i1 = (const struct isl_div_sort_info *) p1;
1355 i2 = (const struct isl_div_sort_info *) p2;
1357 return cmp_row(i1->div, i1->row, i2->row);
1360 /* Sort divs and remove duplicates.
1362 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1364 int i;
1365 int skip;
1366 int len;
1367 struct isl_div_sort_info *array = NULL;
1368 int *pos = NULL, *at = NULL;
1369 int *reordering = NULL;
1370 unsigned div_pos;
1372 if (!qp)
1373 return NULL;
1374 if (qp->div->n_row <= 1)
1375 return qp;
1377 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1379 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1380 qp->div->n_row);
1381 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1382 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1383 len = qp->div->n_col - 2;
1384 reordering = isl_alloc_array(qp->div->ctx, int, len);
1385 if (!array || !pos || !at || !reordering)
1386 goto error;
1388 for (i = 0; i < qp->div->n_row; ++i) {
1389 array[i].div = qp->div;
1390 array[i].row = i;
1391 pos[i] = i;
1392 at[i] = i;
1395 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1396 div_sort_cmp);
1398 for (i = 0; i < div_pos; ++i)
1399 reordering[i] = i;
1401 for (i = 0; i < qp->div->n_row; ++i) {
1402 if (pos[array[i].row] == i)
1403 continue;
1404 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1405 pos[at[i]] = pos[array[i].row];
1406 at[pos[array[i].row]] = at[i];
1407 at[i] = array[i].row;
1408 pos[array[i].row] = i;
1411 skip = 0;
1412 for (i = 0; i < len - div_pos; ++i) {
1413 if (i > 0 &&
1414 isl_seq_eq(qp->div->row[i - skip - 1],
1415 qp->div->row[i - skip], qp->div->n_col)) {
1416 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1417 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1418 2 + div_pos + i - skip);
1419 qp->div = isl_mat_drop_cols(qp->div,
1420 2 + div_pos + i - skip, 1);
1421 skip++;
1423 reordering[div_pos + array[i].row] = div_pos + i - skip;
1426 qp->upoly = reorder(qp->upoly, reordering);
1428 if (!qp->upoly || !qp->div)
1429 goto error;
1431 free(at);
1432 free(pos);
1433 free(array);
1434 free(reordering);
1436 return qp;
1437 error:
1438 free(at);
1439 free(pos);
1440 free(array);
1441 free(reordering);
1442 isl_qpolynomial_free(qp);
1443 return NULL;
1446 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1447 int *exp, int first)
1449 int i;
1450 struct isl_upoly_rec *rec;
1452 if (isl_upoly_is_cst(up))
1453 return up;
1455 if (up->var < first)
1456 return up;
1458 if (exp[up->var - first] == up->var - first)
1459 return up;
1461 up = isl_upoly_cow(up);
1462 if (!up)
1463 goto error;
1465 up->var = exp[up->var - first] + first;
1467 rec = isl_upoly_as_rec(up);
1468 if (!rec)
1469 goto error;
1471 for (i = 0; i < rec->n; ++i) {
1472 rec->p[i] = expand(rec->p[i], exp, first);
1473 if (!rec->p[i])
1474 goto error;
1477 return up;
1478 error:
1479 isl_upoly_free(up);
1480 return NULL;
1483 static __isl_give isl_qpolynomial *with_merged_divs(
1484 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1485 __isl_take isl_qpolynomial *qp2),
1486 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1488 int *exp1 = NULL;
1489 int *exp2 = NULL;
1490 isl_mat *div = NULL;
1491 int n_div1, n_div2;
1493 qp1 = isl_qpolynomial_cow(qp1);
1494 qp2 = isl_qpolynomial_cow(qp2);
1496 if (!qp1 || !qp2)
1497 goto error;
1499 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1500 qp1->div->n_col >= qp2->div->n_col, goto error);
1502 n_div1 = qp1->div->n_row;
1503 n_div2 = qp2->div->n_row;
1504 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1505 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1506 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1507 goto error;
1509 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1510 if (!div)
1511 goto error;
1513 isl_mat_free(qp1->div);
1514 qp1->div = isl_mat_copy(div);
1515 isl_mat_free(qp2->div);
1516 qp2->div = isl_mat_copy(div);
1518 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1519 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1521 if (!qp1->upoly || !qp2->upoly)
1522 goto error;
1524 isl_mat_free(div);
1525 free(exp1);
1526 free(exp2);
1528 return fn(qp1, qp2);
1529 error:
1530 isl_mat_free(div);
1531 free(exp1);
1532 free(exp2);
1533 isl_qpolynomial_free(qp1);
1534 isl_qpolynomial_free(qp2);
1535 return NULL;
1538 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1539 __isl_take isl_qpolynomial *qp2)
1541 isl_bool compatible;
1543 qp1 = isl_qpolynomial_cow(qp1);
1545 if (!qp1 || !qp2)
1546 goto error;
1548 if (qp1->div->n_row < qp2->div->n_row)
1549 return isl_qpolynomial_add(qp2, qp1);
1551 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1552 compatible = compatible_divs(qp1->div, qp2->div);
1553 if (compatible < 0)
1554 goto error;
1555 if (!compatible)
1556 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1558 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1559 if (!qp1->upoly)
1560 goto error;
1562 isl_qpolynomial_free(qp2);
1564 return qp1;
1565 error:
1566 isl_qpolynomial_free(qp1);
1567 isl_qpolynomial_free(qp2);
1568 return NULL;
1571 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1572 __isl_keep isl_set *dom,
1573 __isl_take isl_qpolynomial *qp1,
1574 __isl_take isl_qpolynomial *qp2)
1576 qp1 = isl_qpolynomial_add(qp1, qp2);
1577 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1578 return qp1;
1581 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1582 __isl_take isl_qpolynomial *qp2)
1584 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1587 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1588 __isl_take isl_qpolynomial *qp, isl_int v)
1590 if (isl_int_is_zero(v))
1591 return qp;
1593 qp = isl_qpolynomial_cow(qp);
1594 if (!qp)
1595 return NULL;
1597 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1598 if (!qp->upoly)
1599 goto error;
1601 return qp;
1602 error:
1603 isl_qpolynomial_free(qp);
1604 return NULL;
1608 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1610 if (!qp)
1611 return NULL;
1613 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1616 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1617 __isl_take isl_qpolynomial *qp, isl_int v)
1619 if (isl_int_is_one(v))
1620 return qp;
1622 if (qp && isl_int_is_zero(v)) {
1623 isl_qpolynomial *zero;
1624 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1625 isl_qpolynomial_free(qp);
1626 return zero;
1629 qp = isl_qpolynomial_cow(qp);
1630 if (!qp)
1631 return NULL;
1633 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1634 if (!qp->upoly)
1635 goto error;
1637 return qp;
1638 error:
1639 isl_qpolynomial_free(qp);
1640 return NULL;
1643 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1644 __isl_take isl_qpolynomial *qp, isl_int v)
1646 return isl_qpolynomial_mul_isl_int(qp, v);
1649 /* Multiply "qp" by "v".
1651 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1652 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1654 if (!qp || !v)
1655 goto error;
1657 if (!isl_val_is_rat(v))
1658 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1659 "expecting rational factor", goto error);
1661 if (isl_val_is_one(v)) {
1662 isl_val_free(v);
1663 return qp;
1666 if (isl_val_is_zero(v)) {
1667 isl_space *space;
1669 space = isl_qpolynomial_get_domain_space(qp);
1670 isl_qpolynomial_free(qp);
1671 isl_val_free(v);
1672 return isl_qpolynomial_zero_on_domain(space);
1675 qp = isl_qpolynomial_cow(qp);
1676 if (!qp)
1677 goto error;
1679 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1680 if (!qp->upoly)
1681 qp = isl_qpolynomial_free(qp);
1683 isl_val_free(v);
1684 return qp;
1685 error:
1686 isl_val_free(v);
1687 isl_qpolynomial_free(qp);
1688 return NULL;
1691 /* Divide "qp" by "v".
1693 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1694 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1696 if (!qp || !v)
1697 goto error;
1699 if (!isl_val_is_rat(v))
1700 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1701 "expecting rational factor", goto error);
1702 if (isl_val_is_zero(v))
1703 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1704 "cannot scale down by zero", goto error);
1706 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
1707 error:
1708 isl_val_free(v);
1709 isl_qpolynomial_free(qp);
1710 return NULL;
1713 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1714 __isl_take isl_qpolynomial *qp2)
1716 isl_bool compatible;
1718 qp1 = isl_qpolynomial_cow(qp1);
1720 if (!qp1 || !qp2)
1721 goto error;
1723 if (qp1->div->n_row < qp2->div->n_row)
1724 return isl_qpolynomial_mul(qp2, qp1);
1726 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1727 compatible = compatible_divs(qp1->div, qp2->div);
1728 if (compatible < 0)
1729 goto error;
1730 if (!compatible)
1731 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1733 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1734 if (!qp1->upoly)
1735 goto error;
1737 isl_qpolynomial_free(qp2);
1739 return qp1;
1740 error:
1741 isl_qpolynomial_free(qp1);
1742 isl_qpolynomial_free(qp2);
1743 return NULL;
1746 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1747 unsigned power)
1749 qp = isl_qpolynomial_cow(qp);
1751 if (!qp)
1752 return NULL;
1754 qp->upoly = isl_upoly_pow(qp->upoly, power);
1755 if (!qp->upoly)
1756 goto error;
1758 return qp;
1759 error:
1760 isl_qpolynomial_free(qp);
1761 return NULL;
1764 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1765 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1767 int i;
1769 if (power == 1)
1770 return pwqp;
1772 pwqp = isl_pw_qpolynomial_cow(pwqp);
1773 if (!pwqp)
1774 return NULL;
1776 for (i = 0; i < pwqp->n; ++i) {
1777 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1778 if (!pwqp->p[i].qp)
1779 return isl_pw_qpolynomial_free(pwqp);
1782 return pwqp;
1785 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1786 __isl_take isl_space *domain)
1788 if (!domain)
1789 return NULL;
1790 return isl_qpolynomial_alloc(domain, 0, isl_upoly_zero(domain->ctx));
1793 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1794 __isl_take isl_space *domain)
1796 if (!domain)
1797 return NULL;
1798 return isl_qpolynomial_alloc(domain, 0, isl_upoly_one(domain->ctx));
1801 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1802 __isl_take isl_space *domain)
1804 if (!domain)
1805 return NULL;
1806 return isl_qpolynomial_alloc(domain, 0, isl_upoly_infty(domain->ctx));
1809 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1810 __isl_take isl_space *domain)
1812 if (!domain)
1813 return NULL;
1814 return isl_qpolynomial_alloc(domain, 0,
1815 isl_upoly_neginfty(domain->ctx));
1818 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1819 __isl_take isl_space *domain)
1821 if (!domain)
1822 return NULL;
1823 return isl_qpolynomial_alloc(domain, 0, isl_upoly_nan(domain->ctx));
1826 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1827 __isl_take isl_space *domain,
1828 isl_int v)
1830 struct isl_qpolynomial *qp;
1831 struct isl_upoly_cst *cst;
1833 qp = isl_qpolynomial_zero_on_domain(domain);
1834 if (!qp)
1835 return NULL;
1837 cst = isl_upoly_as_cst(qp->upoly);
1838 isl_int_set(cst->n, v);
1840 return qp;
1843 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1844 isl_int *n, isl_int *d)
1846 struct isl_upoly_cst *cst;
1848 if (!qp)
1849 return -1;
1851 if (!isl_upoly_is_cst(qp->upoly))
1852 return 0;
1854 cst = isl_upoly_as_cst(qp->upoly);
1855 if (!cst)
1856 return -1;
1858 if (n)
1859 isl_int_set(*n, cst->n);
1860 if (d)
1861 isl_int_set(*d, cst->d);
1863 return 1;
1866 /* Return the constant term of "up".
1868 static __isl_give isl_val *isl_upoly_get_constant_val(
1869 __isl_keep struct isl_upoly *up)
1871 struct isl_upoly_cst *cst;
1873 if (!up)
1874 return NULL;
1876 while (!isl_upoly_is_cst(up)) {
1877 struct isl_upoly_rec *rec;
1879 rec = isl_upoly_as_rec(up);
1880 if (!rec)
1881 return NULL;
1882 up = rec->p[0];
1885 cst = isl_upoly_as_cst(up);
1886 if (!cst)
1887 return NULL;
1888 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1891 /* Return the constant term of "qp".
1893 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1894 __isl_keep isl_qpolynomial *qp)
1896 if (!qp)
1897 return NULL;
1899 return isl_upoly_get_constant_val(qp->upoly);
1902 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1904 int is_cst;
1905 struct isl_upoly_rec *rec;
1907 if (!up)
1908 return -1;
1910 if (up->var < 0)
1911 return 1;
1913 rec = isl_upoly_as_rec(up);
1914 if (!rec)
1915 return -1;
1917 if (rec->n > 2)
1918 return 0;
1920 isl_assert(up->ctx, rec->n > 1, return -1);
1922 is_cst = isl_upoly_is_cst(rec->p[1]);
1923 if (is_cst < 0)
1924 return -1;
1925 if (!is_cst)
1926 return 0;
1928 return isl_upoly_is_affine(rec->p[0]);
1931 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1933 if (!qp)
1934 return -1;
1936 if (qp->div->n_row > 0)
1937 return 0;
1939 return isl_upoly_is_affine(qp->upoly);
1942 static void update_coeff(__isl_keep isl_vec *aff,
1943 __isl_keep struct isl_upoly_cst *cst, int pos)
1945 isl_int gcd;
1946 isl_int f;
1948 if (isl_int_is_zero(cst->n))
1949 return;
1951 isl_int_init(gcd);
1952 isl_int_init(f);
1953 isl_int_gcd(gcd, cst->d, aff->el[0]);
1954 isl_int_divexact(f, cst->d, gcd);
1955 isl_int_divexact(gcd, aff->el[0], gcd);
1956 isl_seq_scale(aff->el, aff->el, f, aff->size);
1957 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1958 isl_int_clear(gcd);
1959 isl_int_clear(f);
1962 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1963 __isl_keep isl_vec *aff)
1965 struct isl_upoly_cst *cst;
1966 struct isl_upoly_rec *rec;
1968 if (!up || !aff)
1969 return -1;
1971 if (up->var < 0) {
1972 struct isl_upoly_cst *cst;
1974 cst = isl_upoly_as_cst(up);
1975 if (!cst)
1976 return -1;
1977 update_coeff(aff, cst, 0);
1978 return 0;
1981 rec = isl_upoly_as_rec(up);
1982 if (!rec)
1983 return -1;
1984 isl_assert(up->ctx, rec->n == 2, return -1);
1986 cst = isl_upoly_as_cst(rec->p[1]);
1987 if (!cst)
1988 return -1;
1989 update_coeff(aff, cst, 1 + up->var);
1991 return isl_upoly_update_affine(rec->p[0], aff);
1994 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1995 __isl_keep isl_qpolynomial *qp)
1997 isl_vec *aff;
1998 unsigned d;
2000 if (!qp)
2001 return NULL;
2003 d = isl_space_dim(qp->dim, isl_dim_all);
2004 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
2005 if (!aff)
2006 return NULL;
2008 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
2009 isl_int_set_si(aff->el[0], 1);
2011 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
2012 goto error;
2014 return aff;
2015 error:
2016 isl_vec_free(aff);
2017 return NULL;
2020 /* Compare two quasi-polynomials.
2022 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2023 * than "qp2" and 0 if they are equal.
2025 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2026 __isl_keep isl_qpolynomial *qp2)
2028 int cmp;
2030 if (qp1 == qp2)
2031 return 0;
2032 if (!qp1)
2033 return -1;
2034 if (!qp2)
2035 return 1;
2037 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2038 if (cmp != 0)
2039 return cmp;
2041 cmp = isl_local_cmp(qp1->div, qp2->div);
2042 if (cmp != 0)
2043 return cmp;
2045 return isl_upoly_plain_cmp(qp1->upoly, qp2->upoly);
2048 /* Is "qp1" obviously equal to "qp2"?
2050 * NaN is not equal to anything, not even to another NaN.
2052 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2053 __isl_keep isl_qpolynomial *qp2)
2055 isl_bool equal;
2057 if (!qp1 || !qp2)
2058 return isl_bool_error;
2060 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2061 return isl_bool_false;
2063 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2064 if (equal < 0 || !equal)
2065 return equal;
2067 equal = isl_mat_is_equal(qp1->div, qp2->div);
2068 if (equal < 0 || !equal)
2069 return equal;
2071 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
2074 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
2076 int i;
2077 struct isl_upoly_rec *rec;
2079 if (isl_upoly_is_cst(up)) {
2080 struct isl_upoly_cst *cst;
2081 cst = isl_upoly_as_cst(up);
2082 if (!cst)
2083 return;
2084 isl_int_lcm(*d, *d, cst->d);
2085 return;
2088 rec = isl_upoly_as_rec(up);
2089 if (!rec)
2090 return;
2092 for (i = 0; i < rec->n; ++i)
2093 upoly_update_den(rec->p[i], d);
2096 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
2098 isl_int_set_si(*d, 1);
2099 if (!qp)
2100 return;
2101 upoly_update_den(qp->upoly, d);
2104 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2105 __isl_take isl_space *domain, int pos, int power)
2107 struct isl_ctx *ctx;
2109 if (!domain)
2110 return NULL;
2112 ctx = domain->ctx;
2114 return isl_qpolynomial_alloc(domain, 0,
2115 isl_upoly_var_pow(ctx, pos, power));
2118 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2119 __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2121 if (isl_space_check_is_set(domain ) < 0)
2122 goto error;
2123 if (isl_space_check_range(domain, type, pos, 1) < 0)
2124 goto error;
2126 if (type == isl_dim_set)
2127 pos += isl_space_dim(domain, isl_dim_param);
2129 return isl_qpolynomial_var_pow_on_domain(domain, pos, 1);
2130 error:
2131 isl_space_free(domain);
2132 return NULL;
2135 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
2136 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
2138 int i;
2139 struct isl_upoly_rec *rec;
2140 struct isl_upoly *base, *res;
2142 if (!up)
2143 return NULL;
2145 if (isl_upoly_is_cst(up))
2146 return up;
2148 if (up->var < first)
2149 return up;
2151 rec = isl_upoly_as_rec(up);
2152 if (!rec)
2153 goto error;
2155 isl_assert(up->ctx, rec->n >= 1, goto error);
2157 if (up->var >= first + n)
2158 base = isl_upoly_var_pow(up->ctx, up->var, 1);
2159 else
2160 base = isl_upoly_copy(subs[up->var - first]);
2162 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
2163 for (i = rec->n - 2; i >= 0; --i) {
2164 struct isl_upoly *t;
2165 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
2166 res = isl_upoly_mul(res, isl_upoly_copy(base));
2167 res = isl_upoly_sum(res, t);
2170 isl_upoly_free(base);
2171 isl_upoly_free(up);
2173 return res;
2174 error:
2175 isl_upoly_free(up);
2176 return NULL;
2179 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
2180 isl_int denom, unsigned len)
2182 int i;
2183 struct isl_upoly *up;
2185 isl_assert(ctx, len >= 1, return NULL);
2187 up = isl_upoly_rat_cst(ctx, f[0], denom);
2188 for (i = 0; i < len - 1; ++i) {
2189 struct isl_upoly *t;
2190 struct isl_upoly *c;
2192 if (isl_int_is_zero(f[1 + i]))
2193 continue;
2195 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2196 t = isl_upoly_var_pow(ctx, i, 1);
2197 t = isl_upoly_mul(c, t);
2198 up = isl_upoly_sum(up, t);
2201 return up;
2204 /* Remove common factor of non-constant terms and denominator.
2206 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2208 isl_ctx *ctx = qp->div->ctx;
2209 unsigned total = qp->div->n_col - 2;
2211 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2212 isl_int_gcd(ctx->normalize_gcd,
2213 ctx->normalize_gcd, qp->div->row[div][0]);
2214 if (isl_int_is_one(ctx->normalize_gcd))
2215 return;
2217 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2218 ctx->normalize_gcd, total);
2219 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2220 ctx->normalize_gcd);
2221 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2222 ctx->normalize_gcd);
2225 /* Replace the integer division identified by "div" by the polynomial "s".
2226 * The integer division is assumed not to appear in the definition
2227 * of any other integer divisions.
2229 static __isl_give isl_qpolynomial *substitute_div(
2230 __isl_take isl_qpolynomial *qp,
2231 int div, __isl_take struct isl_upoly *s)
2233 int i;
2234 int total;
2235 int *reordering;
2237 if (!qp || !s)
2238 goto error;
2240 qp = isl_qpolynomial_cow(qp);
2241 if (!qp)
2242 goto error;
2244 total = isl_space_dim(qp->dim, isl_dim_all);
2245 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2246 if (!qp->upoly)
2247 goto error;
2249 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2250 if (!reordering)
2251 goto error;
2252 for (i = 0; i < total + div; ++i)
2253 reordering[i] = i;
2254 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2255 reordering[i] = i - 1;
2256 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2257 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2258 qp->upoly = reorder(qp->upoly, reordering);
2259 free(reordering);
2261 if (!qp->upoly || !qp->div)
2262 goto error;
2264 isl_upoly_free(s);
2265 return qp;
2266 error:
2267 isl_qpolynomial_free(qp);
2268 isl_upoly_free(s);
2269 return NULL;
2272 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2273 * divisions because d is equal to 1 by their definition, i.e., e.
2275 static __isl_give isl_qpolynomial *substitute_non_divs(
2276 __isl_take isl_qpolynomial *qp)
2278 int i, j;
2279 int total;
2280 struct isl_upoly *s;
2282 if (!qp)
2283 return NULL;
2285 total = isl_space_dim(qp->dim, isl_dim_all);
2286 for (i = 0; qp && i < qp->div->n_row; ++i) {
2287 if (!isl_int_is_one(qp->div->row[i][0]))
2288 continue;
2289 for (j = i + 1; j < qp->div->n_row; ++j) {
2290 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2291 continue;
2292 isl_seq_combine(qp->div->row[j] + 1,
2293 qp->div->ctx->one, qp->div->row[j] + 1,
2294 qp->div->row[j][2 + total + i],
2295 qp->div->row[i] + 1, 1 + total + i);
2296 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2297 normalize_div(qp, j);
2299 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2300 qp->div->row[i][0], qp->div->n_col - 1);
2301 qp = substitute_div(qp, i, s);
2302 --i;
2305 return qp;
2308 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2309 * with d the denominator. When replacing the coefficient e of x by
2310 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2311 * inside the division, so we need to add floor(e/d) * x outside.
2312 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2313 * to adjust the coefficient of x in each later div that depends on the
2314 * current div "div" and also in the affine expressions in the rows of "mat"
2315 * (if they too depend on "div").
2317 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2318 __isl_keep isl_mat **mat)
2320 int i, j;
2321 isl_int v;
2322 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2324 isl_int_init(v);
2325 for (i = 0; i < 1 + total + div; ++i) {
2326 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2327 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2328 continue;
2329 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2330 isl_int_fdiv_r(qp->div->row[div][1 + i],
2331 qp->div->row[div][1 + i], qp->div->row[div][0]);
2332 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2333 for (j = div + 1; j < qp->div->n_row; ++j) {
2334 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2335 continue;
2336 isl_int_addmul(qp->div->row[j][1 + i],
2337 v, qp->div->row[j][2 + total + div]);
2340 isl_int_clear(v);
2343 /* Check if the last non-zero coefficient is bigger that half of the
2344 * denominator. If so, we will invert the div to further reduce the number
2345 * of distinct divs that may appear.
2346 * If the last non-zero coefficient is exactly half the denominator,
2347 * then we continue looking for earlier coefficients that are bigger
2348 * than half the denominator.
2350 static int needs_invert(__isl_keep isl_mat *div, int row)
2352 int i;
2353 int cmp;
2355 for (i = div->n_col - 1; i >= 1; --i) {
2356 if (isl_int_is_zero(div->row[row][i]))
2357 continue;
2358 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2359 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2360 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2361 if (cmp)
2362 return cmp > 0;
2363 if (i == 1)
2364 return 1;
2367 return 0;
2370 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2371 * We only invert the coefficients of e (and the coefficient of q in
2372 * later divs and in the rows of "mat"). After calling this function, the
2373 * coefficients of e should be reduced again.
2375 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2376 __isl_keep isl_mat **mat)
2378 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2380 isl_seq_neg(qp->div->row[div] + 1,
2381 qp->div->row[div] + 1, qp->div->n_col - 1);
2382 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2383 isl_int_add(qp->div->row[div][1],
2384 qp->div->row[div][1], qp->div->row[div][0]);
2385 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2386 isl_mat_col_mul(qp->div, 2 + total + div,
2387 qp->div->ctx->negone, 2 + total + div);
2390 /* Reduce all divs of "qp" to have coefficients
2391 * in the interval [0, d-1], with d the denominator and such that the
2392 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2393 * The modifications to the integer divisions need to be reflected
2394 * in the factors of the polynomial that refer to the original
2395 * integer divisions. To this end, the modifications are collected
2396 * as a set of affine expressions and then plugged into the polynomial.
2398 * After the reduction, some divs may have become redundant or identical,
2399 * so we call substitute_non_divs and sort_divs. If these functions
2400 * eliminate divs or merge two or more divs into one, the coefficients
2401 * of the enclosing divs may have to be reduced again, so we call
2402 * ourselves recursively if the number of divs decreases.
2404 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2406 int i;
2407 isl_ctx *ctx;
2408 isl_mat *mat;
2409 struct isl_upoly **s;
2410 unsigned o_div, n_div, total;
2412 if (!qp)
2413 return NULL;
2415 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2416 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2417 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2418 ctx = isl_qpolynomial_get_ctx(qp);
2419 mat = isl_mat_zero(ctx, n_div, 1 + total);
2421 for (i = 0; i < n_div; ++i)
2422 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2424 for (i = 0; i < qp->div->n_row; ++i) {
2425 normalize_div(qp, i);
2426 reduce_div(qp, i, &mat);
2427 if (needs_invert(qp->div, i)) {
2428 invert_div(qp, i, &mat);
2429 reduce_div(qp, i, &mat);
2432 if (!mat)
2433 goto error;
2435 s = isl_alloc_array(ctx, struct isl_upoly *, n_div);
2436 if (n_div && !s)
2437 goto error;
2438 for (i = 0; i < n_div; ++i)
2439 s[i] = isl_upoly_from_affine(ctx, mat->row[i], ctx->one,
2440 1 + total);
2441 qp->upoly = isl_upoly_subs(qp->upoly, o_div - 1, n_div, s);
2442 for (i = 0; i < n_div; ++i)
2443 isl_upoly_free(s[i]);
2444 free(s);
2445 if (!qp->upoly)
2446 goto error;
2448 isl_mat_free(mat);
2450 qp = substitute_non_divs(qp);
2451 qp = sort_divs(qp);
2452 if (qp && isl_qpolynomial_domain_dim(qp, isl_dim_div) < n_div)
2453 return reduce_divs(qp);
2455 return qp;
2456 error:
2457 isl_qpolynomial_free(qp);
2458 isl_mat_free(mat);
2459 return NULL;
2462 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2463 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2465 struct isl_qpolynomial *qp;
2466 struct isl_upoly_cst *cst;
2468 qp = isl_qpolynomial_zero_on_domain(domain);
2469 if (!qp)
2470 return NULL;
2472 cst = isl_upoly_as_cst(qp->upoly);
2473 isl_int_set(cst->n, n);
2474 isl_int_set(cst->d, d);
2476 return qp;
2479 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2481 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2482 __isl_take isl_space *domain, __isl_take isl_val *val)
2484 isl_qpolynomial *qp;
2485 struct isl_upoly_cst *cst;
2487 qp = isl_qpolynomial_zero_on_domain(domain);
2488 if (!qp || !val)
2489 goto error;
2491 cst = isl_upoly_as_cst(qp->upoly);
2492 isl_int_set(cst->n, val->n);
2493 isl_int_set(cst->d, val->d);
2495 isl_val_free(val);
2496 return qp;
2497 error:
2498 isl_val_free(val);
2499 isl_qpolynomial_free(qp);
2500 return NULL;
2503 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2505 struct isl_upoly_rec *rec;
2506 int i;
2508 if (!up)
2509 return -1;
2511 if (isl_upoly_is_cst(up))
2512 return 0;
2514 if (up->var < d)
2515 active[up->var] = 1;
2517 rec = isl_upoly_as_rec(up);
2518 for (i = 0; i < rec->n; ++i)
2519 if (up_set_active(rec->p[i], active, d) < 0)
2520 return -1;
2522 return 0;
2525 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2527 int i, j;
2528 int d = isl_space_dim(qp->dim, isl_dim_all);
2530 if (!qp || !active)
2531 return -1;
2533 for (i = 0; i < d; ++i)
2534 for (j = 0; j < qp->div->n_row; ++j) {
2535 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2536 continue;
2537 active[i] = 1;
2538 break;
2541 return up_set_active(qp->upoly, active, d);
2544 #undef TYPE
2545 #define TYPE isl_qpolynomial
2546 static
2547 #include "check_type_range_templ.c"
2549 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2550 enum isl_dim_type type, unsigned first, unsigned n)
2552 int i;
2553 int *active = NULL;
2554 isl_bool involves = isl_bool_false;
2556 if (!qp)
2557 return isl_bool_error;
2558 if (n == 0)
2559 return isl_bool_false;
2561 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2562 return isl_bool_error;
2563 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2564 type == isl_dim_in, return isl_bool_error);
2566 active = isl_calloc_array(qp->dim->ctx, int,
2567 isl_space_dim(qp->dim, isl_dim_all));
2568 if (set_active(qp, active) < 0)
2569 goto error;
2571 if (type == isl_dim_in)
2572 first += isl_space_dim(qp->dim, isl_dim_param);
2573 for (i = 0; i < n; ++i)
2574 if (active[first + i]) {
2575 involves = isl_bool_true;
2576 break;
2579 free(active);
2581 return involves;
2582 error:
2583 free(active);
2584 return isl_bool_error;
2587 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2588 * of the divs that do appear in the quasi-polynomial.
2590 static __isl_give isl_qpolynomial *remove_redundant_divs(
2591 __isl_take isl_qpolynomial *qp)
2593 int i, j;
2594 int d;
2595 int len;
2596 int skip;
2597 int *active = NULL;
2598 int *reordering = NULL;
2599 int redundant = 0;
2600 int n_div;
2601 isl_ctx *ctx;
2603 if (!qp)
2604 return NULL;
2605 if (qp->div->n_row == 0)
2606 return qp;
2608 d = isl_space_dim(qp->dim, isl_dim_all);
2609 len = qp->div->n_col - 2;
2610 ctx = isl_qpolynomial_get_ctx(qp);
2611 active = isl_calloc_array(ctx, int, len);
2612 if (!active)
2613 goto error;
2615 if (up_set_active(qp->upoly, active, len) < 0)
2616 goto error;
2618 for (i = qp->div->n_row - 1; i >= 0; --i) {
2619 if (!active[d + i]) {
2620 redundant = 1;
2621 continue;
2623 for (j = 0; j < i; ++j) {
2624 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2625 continue;
2626 active[d + j] = 1;
2627 break;
2631 if (!redundant) {
2632 free(active);
2633 return qp;
2636 reordering = isl_alloc_array(qp->div->ctx, int, len);
2637 if (!reordering)
2638 goto error;
2640 for (i = 0; i < d; ++i)
2641 reordering[i] = i;
2643 skip = 0;
2644 n_div = qp->div->n_row;
2645 for (i = 0; i < n_div; ++i) {
2646 if (!active[d + i]) {
2647 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2648 qp->div = isl_mat_drop_cols(qp->div,
2649 2 + d + i - skip, 1);
2650 skip++;
2652 reordering[d + i] = d + i - skip;
2655 qp->upoly = reorder(qp->upoly, reordering);
2657 if (!qp->upoly || !qp->div)
2658 goto error;
2660 free(active);
2661 free(reordering);
2663 return qp;
2664 error:
2665 free(active);
2666 free(reordering);
2667 isl_qpolynomial_free(qp);
2668 return NULL;
2671 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2672 unsigned first, unsigned n)
2674 int i;
2675 struct isl_upoly_rec *rec;
2677 if (!up)
2678 return NULL;
2679 if (n == 0 || up->var < 0 || up->var < first)
2680 return up;
2681 if (up->var < first + n) {
2682 up = replace_by_constant_term(up);
2683 return isl_upoly_drop(up, first, n);
2685 up = isl_upoly_cow(up);
2686 if (!up)
2687 return NULL;
2688 up->var -= n;
2689 rec = isl_upoly_as_rec(up);
2690 if (!rec)
2691 goto error;
2693 for (i = 0; i < rec->n; ++i) {
2694 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2695 if (!rec->p[i])
2696 goto error;
2699 return up;
2700 error:
2701 isl_upoly_free(up);
2702 return NULL;
2705 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2706 __isl_take isl_qpolynomial *qp,
2707 enum isl_dim_type type, unsigned pos, const char *s)
2709 qp = isl_qpolynomial_cow(qp);
2710 if (!qp)
2711 return NULL;
2712 if (type == isl_dim_out)
2713 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2714 "cannot set name of output/set dimension",
2715 return isl_qpolynomial_free(qp));
2716 type = domain_type(type);
2717 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2718 if (!qp->dim)
2719 goto error;
2720 return qp;
2721 error:
2722 isl_qpolynomial_free(qp);
2723 return NULL;
2726 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2727 __isl_take isl_qpolynomial *qp,
2728 enum isl_dim_type type, unsigned first, unsigned n)
2730 if (!qp)
2731 return NULL;
2732 if (type == isl_dim_out)
2733 isl_die(qp->dim->ctx, isl_error_invalid,
2734 "cannot drop output/set dimension",
2735 goto error);
2736 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2737 return isl_qpolynomial_free(qp);
2738 type = domain_type(type);
2739 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2740 return qp;
2742 qp = isl_qpolynomial_cow(qp);
2743 if (!qp)
2744 return NULL;
2746 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2747 type == isl_dim_set, goto error);
2749 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2750 if (!qp->dim)
2751 goto error;
2753 if (type == isl_dim_set)
2754 first += isl_space_dim(qp->dim, isl_dim_param);
2756 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2757 if (!qp->div)
2758 goto error;
2760 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2761 if (!qp->upoly)
2762 goto error;
2764 return qp;
2765 error:
2766 isl_qpolynomial_free(qp);
2767 return NULL;
2770 /* Project the domain of the quasi-polynomial onto its parameter space.
2771 * The quasi-polynomial may not involve any of the domain dimensions.
2773 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2774 __isl_take isl_qpolynomial *qp)
2776 isl_space *space;
2777 unsigned n;
2778 isl_bool involves;
2780 n = isl_qpolynomial_dim(qp, isl_dim_in);
2781 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2782 if (involves < 0)
2783 return isl_qpolynomial_free(qp);
2784 if (involves)
2785 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2786 "polynomial involves some of the domain dimensions",
2787 return isl_qpolynomial_free(qp));
2788 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2789 space = isl_qpolynomial_get_domain_space(qp);
2790 space = isl_space_params(space);
2791 qp = isl_qpolynomial_reset_domain_space(qp, space);
2792 return qp;
2795 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2796 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2798 int i, j, k;
2799 isl_int denom;
2800 unsigned total;
2801 unsigned n_div;
2802 struct isl_upoly *up;
2804 if (!eq)
2805 goto error;
2806 if (eq->n_eq == 0) {
2807 isl_basic_set_free(eq);
2808 return qp;
2811 qp = isl_qpolynomial_cow(qp);
2812 if (!qp)
2813 goto error;
2814 qp->div = isl_mat_cow(qp->div);
2815 if (!qp->div)
2816 goto error;
2818 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2819 n_div = eq->n_div;
2820 isl_int_init(denom);
2821 for (i = 0; i < eq->n_eq; ++i) {
2822 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2823 if (j < 0 || j == 0 || j >= total)
2824 continue;
2826 for (k = 0; k < qp->div->n_row; ++k) {
2827 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2828 continue;
2829 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2830 &qp->div->row[k][0]);
2831 normalize_div(qp, k);
2834 if (isl_int_is_pos(eq->eq[i][j]))
2835 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2836 isl_int_abs(denom, eq->eq[i][j]);
2837 isl_int_set_si(eq->eq[i][j], 0);
2839 up = isl_upoly_from_affine(qp->dim->ctx,
2840 eq->eq[i], denom, total);
2841 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2842 isl_upoly_free(up);
2844 isl_int_clear(denom);
2846 if (!qp->upoly)
2847 goto error;
2849 isl_basic_set_free(eq);
2851 qp = substitute_non_divs(qp);
2852 qp = sort_divs(qp);
2854 return qp;
2855 error:
2856 isl_basic_set_free(eq);
2857 isl_qpolynomial_free(qp);
2858 return NULL;
2861 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2863 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2864 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2866 if (!qp || !eq)
2867 goto error;
2868 if (qp->div->n_row > 0)
2869 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2870 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2871 error:
2872 isl_basic_set_free(eq);
2873 isl_qpolynomial_free(qp);
2874 return NULL;
2877 /* Look for equalities among the variables shared by context and qp
2878 * and the integer divisions of qp, if any.
2879 * The equalities are then used to eliminate variables and/or integer
2880 * divisions from qp.
2882 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2883 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2885 isl_local_space *ls;
2886 isl_basic_set *aff;
2888 ls = isl_qpolynomial_get_domain_local_space(qp);
2889 context = isl_local_space_lift_set(ls, context);
2891 aff = isl_set_affine_hull(context);
2892 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2895 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2896 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2898 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2899 isl_set *dom_context = isl_set_universe(space);
2900 dom_context = isl_set_intersect_params(dom_context, context);
2901 return isl_qpolynomial_gist(qp, dom_context);
2904 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2905 __isl_take isl_qpolynomial *qp)
2907 isl_set *dom;
2909 if (!qp)
2910 return NULL;
2911 if (isl_qpolynomial_is_zero(qp)) {
2912 isl_space *dim = isl_qpolynomial_get_space(qp);
2913 isl_qpolynomial_free(qp);
2914 return isl_pw_qpolynomial_zero(dim);
2917 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2918 return isl_pw_qpolynomial_alloc(dom, qp);
2921 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2923 #undef PW
2924 #define PW isl_pw_qpolynomial
2925 #undef EL
2926 #define EL isl_qpolynomial
2927 #undef EL_IS_ZERO
2928 #define EL_IS_ZERO is_zero
2929 #undef ZERO
2930 #define ZERO zero
2931 #undef IS_ZERO
2932 #define IS_ZERO is_zero
2933 #undef FIELD
2934 #define FIELD qp
2935 #undef DEFAULT_IS_ZERO
2936 #define DEFAULT_IS_ZERO 1
2938 #define NO_PULLBACK
2940 #include <isl_pw_templ.c>
2941 #include <isl_pw_eval.c>
2943 #undef BASE
2944 #define BASE pw_qpolynomial
2946 #include <isl_union_single.c>
2947 #include <isl_union_eval.c>
2948 #include <isl_union_neg.c>
2950 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2952 if (!pwqp)
2953 return -1;
2955 if (pwqp->n != -1)
2956 return 0;
2958 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2959 return 0;
2961 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2964 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2965 __isl_take isl_pw_qpolynomial *pwqp1,
2966 __isl_take isl_pw_qpolynomial *pwqp2)
2968 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2971 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2972 __isl_take isl_pw_qpolynomial *pwqp1,
2973 __isl_take isl_pw_qpolynomial *pwqp2)
2975 int i, j, n;
2976 struct isl_pw_qpolynomial *res;
2978 if (!pwqp1 || !pwqp2)
2979 goto error;
2981 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
2982 goto error);
2984 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2985 isl_pw_qpolynomial_free(pwqp2);
2986 return pwqp1;
2989 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2990 isl_pw_qpolynomial_free(pwqp1);
2991 return pwqp2;
2994 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2995 isl_pw_qpolynomial_free(pwqp1);
2996 return pwqp2;
2999 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3000 isl_pw_qpolynomial_free(pwqp2);
3001 return pwqp1;
3004 n = pwqp1->n * pwqp2->n;
3005 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3007 for (i = 0; i < pwqp1->n; ++i) {
3008 for (j = 0; j < pwqp2->n; ++j) {
3009 struct isl_set *common;
3010 struct isl_qpolynomial *prod;
3011 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3012 isl_set_copy(pwqp2->p[j].set));
3013 if (isl_set_plain_is_empty(common)) {
3014 isl_set_free(common);
3015 continue;
3018 prod = isl_qpolynomial_mul(
3019 isl_qpolynomial_copy(pwqp1->p[i].qp),
3020 isl_qpolynomial_copy(pwqp2->p[j].qp));
3022 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3026 isl_pw_qpolynomial_free(pwqp1);
3027 isl_pw_qpolynomial_free(pwqp2);
3029 return res;
3030 error:
3031 isl_pw_qpolynomial_free(pwqp1);
3032 isl_pw_qpolynomial_free(pwqp2);
3033 return NULL;
3036 __isl_give isl_val *isl_upoly_eval(__isl_take struct isl_upoly *up,
3037 __isl_take isl_vec *vec)
3039 int i;
3040 struct isl_upoly_rec *rec;
3041 isl_val *res;
3042 isl_val *base;
3044 if (isl_upoly_is_cst(up)) {
3045 isl_vec_free(vec);
3046 res = isl_upoly_get_constant_val(up);
3047 isl_upoly_free(up);
3048 return res;
3051 rec = isl_upoly_as_rec(up);
3052 if (!rec || !vec)
3053 goto error;
3055 isl_assert(up->ctx, rec->n >= 1, goto error);
3057 base = isl_val_rat_from_isl_int(up->ctx,
3058 vec->el[1 + up->var], vec->el[0]);
3060 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
3061 isl_vec_copy(vec));
3063 for (i = rec->n - 2; i >= 0; --i) {
3064 res = isl_val_mul(res, isl_val_copy(base));
3065 res = isl_val_add(res,
3066 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
3067 isl_vec_copy(vec)));
3070 isl_val_free(base);
3071 isl_upoly_free(up);
3072 isl_vec_free(vec);
3073 return res;
3074 error:
3075 isl_upoly_free(up);
3076 isl_vec_free(vec);
3077 return NULL;
3080 /* Evaluate "qp" in the void point "pnt".
3081 * In particular, return the value NaN.
3083 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3084 __isl_take isl_point *pnt)
3086 isl_ctx *ctx;
3088 ctx = isl_point_get_ctx(pnt);
3089 isl_qpolynomial_free(qp);
3090 isl_point_free(pnt);
3091 return isl_val_nan(ctx);
3094 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3095 __isl_take isl_point *pnt)
3097 isl_bool is_void;
3098 isl_vec *ext;
3099 isl_val *v;
3101 if (!qp || !pnt)
3102 goto error;
3103 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3104 is_void = isl_point_is_void(pnt);
3105 if (is_void < 0)
3106 goto error;
3107 if (is_void)
3108 return eval_void(qp, pnt);
3110 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3112 v = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
3114 isl_qpolynomial_free(qp);
3115 isl_point_free(pnt);
3117 return v;
3118 error:
3119 isl_qpolynomial_free(qp);
3120 isl_point_free(pnt);
3121 return NULL;
3124 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
3125 __isl_keep struct isl_upoly_cst *cst2)
3127 int cmp;
3128 isl_int t;
3129 isl_int_init(t);
3130 isl_int_mul(t, cst1->n, cst2->d);
3131 isl_int_submul(t, cst2->n, cst1->d);
3132 cmp = isl_int_sgn(t);
3133 isl_int_clear(t);
3134 return cmp;
3137 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3138 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3139 unsigned first, unsigned n)
3141 unsigned total;
3142 unsigned g_pos;
3143 int *exp;
3145 if (!qp)
3146 return NULL;
3147 if (type == isl_dim_out)
3148 isl_die(qp->div->ctx, isl_error_invalid,
3149 "cannot insert output/set dimensions",
3150 goto error);
3151 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3152 return isl_qpolynomial_free(qp);
3153 type = domain_type(type);
3154 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3155 return qp;
3157 qp = isl_qpolynomial_cow(qp);
3158 if (!qp)
3159 return NULL;
3161 g_pos = pos(qp->dim, type) + first;
3163 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3164 if (!qp->div)
3165 goto error;
3167 total = qp->div->n_col - 2;
3168 if (total > g_pos) {
3169 int i;
3170 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3171 if (!exp)
3172 goto error;
3173 for (i = 0; i < total - g_pos; ++i)
3174 exp[i] = i + n;
3175 qp->upoly = expand(qp->upoly, exp, g_pos);
3176 free(exp);
3177 if (!qp->upoly)
3178 goto error;
3181 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3182 if (!qp->dim)
3183 goto error;
3185 return qp;
3186 error:
3187 isl_qpolynomial_free(qp);
3188 return NULL;
3191 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3192 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3194 unsigned pos;
3196 pos = isl_qpolynomial_dim(qp, type);
3198 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3201 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3202 __isl_take isl_pw_qpolynomial *pwqp,
3203 enum isl_dim_type type, unsigned n)
3205 unsigned pos;
3207 pos = isl_pw_qpolynomial_dim(pwqp, type);
3209 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3212 static int *reordering_move(isl_ctx *ctx,
3213 unsigned len, unsigned dst, unsigned src, unsigned n)
3215 int i;
3216 int *reordering;
3218 reordering = isl_alloc_array(ctx, int, len);
3219 if (!reordering)
3220 return NULL;
3222 if (dst <= src) {
3223 for (i = 0; i < dst; ++i)
3224 reordering[i] = i;
3225 for (i = 0; i < n; ++i)
3226 reordering[src + i] = dst + i;
3227 for (i = 0; i < src - dst; ++i)
3228 reordering[dst + i] = dst + n + i;
3229 for (i = 0; i < len - src - n; ++i)
3230 reordering[src + n + i] = src + n + i;
3231 } else {
3232 for (i = 0; i < src; ++i)
3233 reordering[i] = i;
3234 for (i = 0; i < n; ++i)
3235 reordering[src + i] = dst + i;
3236 for (i = 0; i < dst - src; ++i)
3237 reordering[src + n + i] = src + i;
3238 for (i = 0; i < len - dst - n; ++i)
3239 reordering[dst + n + i] = dst + n + i;
3242 return reordering;
3245 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3246 __isl_take isl_qpolynomial *qp,
3247 enum isl_dim_type dst_type, unsigned dst_pos,
3248 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3250 unsigned g_dst_pos;
3251 unsigned g_src_pos;
3252 int *reordering;
3254 if (!qp)
3255 return NULL;
3257 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3258 isl_die(qp->dim->ctx, isl_error_invalid,
3259 "cannot move output/set dimension",
3260 goto error);
3261 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3262 return isl_qpolynomial_free(qp);
3263 if (dst_type == isl_dim_in)
3264 dst_type = isl_dim_set;
3265 if (src_type == isl_dim_in)
3266 src_type = isl_dim_set;
3268 if (n == 0 &&
3269 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3270 !isl_space_is_named_or_nested(qp->dim, dst_type))
3271 return qp;
3273 qp = isl_qpolynomial_cow(qp);
3274 if (!qp)
3275 return NULL;
3277 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3278 g_src_pos = pos(qp->dim, src_type) + src_pos;
3279 if (dst_type > src_type)
3280 g_dst_pos -= n;
3282 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3283 if (!qp->div)
3284 goto error;
3285 qp = sort_divs(qp);
3286 if (!qp)
3287 goto error;
3289 reordering = reordering_move(qp->dim->ctx,
3290 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3291 if (!reordering)
3292 goto error;
3294 qp->upoly = reorder(qp->upoly, reordering);
3295 free(reordering);
3296 if (!qp->upoly)
3297 goto error;
3299 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3300 if (!qp->dim)
3301 goto error;
3303 return qp;
3304 error:
3305 isl_qpolynomial_free(qp);
3306 return NULL;
3309 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3310 __isl_take isl_space *space, isl_int *f, isl_int denom)
3312 struct isl_upoly *up;
3314 space = isl_space_domain(space);
3315 if (!space)
3316 return NULL;
3318 up = isl_upoly_from_affine(space->ctx, f, denom,
3319 1 + isl_space_dim(space, isl_dim_all));
3321 return isl_qpolynomial_alloc(space, 0, up);
3324 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3326 isl_ctx *ctx;
3327 struct isl_upoly *up;
3328 isl_qpolynomial *qp;
3330 if (!aff)
3331 return NULL;
3333 ctx = isl_aff_get_ctx(aff);
3334 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3335 aff->v->size - 1);
3337 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3338 aff->ls->div->n_row, up);
3339 if (!qp)
3340 goto error;
3342 isl_mat_free(qp->div);
3343 qp->div = isl_mat_copy(aff->ls->div);
3344 qp->div = isl_mat_cow(qp->div);
3345 if (!qp->div)
3346 goto error;
3348 isl_aff_free(aff);
3349 qp = reduce_divs(qp);
3350 qp = remove_redundant_divs(qp);
3351 return qp;
3352 error:
3353 isl_aff_free(aff);
3354 return isl_qpolynomial_free(qp);
3357 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3358 __isl_take isl_pw_aff *pwaff)
3360 int i;
3361 isl_pw_qpolynomial *pwqp;
3363 if (!pwaff)
3364 return NULL;
3366 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3367 pwaff->n);
3369 for (i = 0; i < pwaff->n; ++i) {
3370 isl_set *dom;
3371 isl_qpolynomial *qp;
3373 dom = isl_set_copy(pwaff->p[i].set);
3374 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3375 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3378 isl_pw_aff_free(pwaff);
3379 return pwqp;
3382 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3383 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3385 isl_aff *aff;
3387 aff = isl_constraint_get_bound(c, type, pos);
3388 isl_constraint_free(c);
3389 return isl_qpolynomial_from_aff(aff);
3392 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3393 * in "qp" by subs[i].
3395 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3396 __isl_take isl_qpolynomial *qp,
3397 enum isl_dim_type type, unsigned first, unsigned n,
3398 __isl_keep isl_qpolynomial **subs)
3400 int i;
3401 struct isl_upoly **ups;
3403 if (n == 0)
3404 return qp;
3406 qp = isl_qpolynomial_cow(qp);
3407 if (!qp)
3408 return NULL;
3410 if (type == isl_dim_out)
3411 isl_die(qp->dim->ctx, isl_error_invalid,
3412 "cannot substitute output/set dimension",
3413 goto error);
3414 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3415 return isl_qpolynomial_free(qp);
3416 type = domain_type(type);
3418 for (i = 0; i < n; ++i)
3419 if (!subs[i])
3420 goto error;
3422 for (i = 0; i < n; ++i)
3423 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3424 goto error);
3426 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3427 for (i = 0; i < n; ++i)
3428 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3430 first += pos(qp->dim, type);
3432 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3433 if (!ups)
3434 goto error;
3435 for (i = 0; i < n; ++i)
3436 ups[i] = subs[i]->upoly;
3438 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3440 free(ups);
3442 if (!qp->upoly)
3443 goto error;
3445 return qp;
3446 error:
3447 isl_qpolynomial_free(qp);
3448 return NULL;
3451 /* Extend "bset" with extra set dimensions for each integer division
3452 * in "qp" and then call "fn" with the extended bset and the polynomial
3453 * that results from replacing each of the integer divisions by the
3454 * corresponding extra set dimension.
3456 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3457 __isl_keep isl_basic_set *bset,
3458 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3459 __isl_take isl_qpolynomial *poly, void *user), void *user)
3461 isl_space *space;
3462 isl_local_space *ls;
3463 isl_qpolynomial *poly;
3465 if (!qp || !bset)
3466 return isl_stat_error;
3467 if (qp->div->n_row == 0)
3468 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3469 user);
3471 space = isl_space_copy(qp->dim);
3472 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3473 poly = isl_qpolynomial_alloc(space, 0, isl_upoly_copy(qp->upoly));
3474 bset = isl_basic_set_copy(bset);
3475 ls = isl_qpolynomial_get_domain_local_space(qp);
3476 bset = isl_local_space_lift_basic_set(ls, bset);
3478 return fn(bset, poly, user);
3481 /* Return total degree in variables first (inclusive) up to last (exclusive).
3483 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3485 int deg = -1;
3486 int i;
3487 struct isl_upoly_rec *rec;
3489 if (!up)
3490 return -2;
3491 if (isl_upoly_is_zero(up))
3492 return -1;
3493 if (isl_upoly_is_cst(up) || up->var < first)
3494 return 0;
3496 rec = isl_upoly_as_rec(up);
3497 if (!rec)
3498 return -2;
3500 for (i = 0; i < rec->n; ++i) {
3501 int d;
3503 if (isl_upoly_is_zero(rec->p[i]))
3504 continue;
3505 d = isl_upoly_degree(rec->p[i], first, last);
3506 if (up->var < last)
3507 d += i;
3508 if (d > deg)
3509 deg = d;
3512 return deg;
3515 /* Return total degree in set variables.
3517 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3519 unsigned ovar;
3520 unsigned nvar;
3522 if (!poly)
3523 return -2;
3525 ovar = isl_space_offset(poly->dim, isl_dim_set);
3526 nvar = isl_space_dim(poly->dim, isl_dim_set);
3527 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3530 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3531 unsigned pos, int deg)
3533 int i;
3534 struct isl_upoly_rec *rec;
3536 if (!up)
3537 return NULL;
3539 if (isl_upoly_is_cst(up) || up->var < pos) {
3540 if (deg == 0)
3541 return isl_upoly_copy(up);
3542 else
3543 return isl_upoly_zero(up->ctx);
3546 rec = isl_upoly_as_rec(up);
3547 if (!rec)
3548 return NULL;
3550 if (up->var == pos) {
3551 if (deg < rec->n)
3552 return isl_upoly_copy(rec->p[deg]);
3553 else
3554 return isl_upoly_zero(up->ctx);
3557 up = isl_upoly_copy(up);
3558 up = isl_upoly_cow(up);
3559 rec = isl_upoly_as_rec(up);
3560 if (!rec)
3561 goto error;
3563 for (i = 0; i < rec->n; ++i) {
3564 struct isl_upoly *t;
3565 t = isl_upoly_coeff(rec->p[i], pos, deg);
3566 if (!t)
3567 goto error;
3568 isl_upoly_free(rec->p[i]);
3569 rec->p[i] = t;
3572 return up;
3573 error:
3574 isl_upoly_free(up);
3575 return NULL;
3578 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3580 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3581 __isl_keep isl_qpolynomial *qp,
3582 enum isl_dim_type type, unsigned t_pos, int deg)
3584 unsigned g_pos;
3585 struct isl_upoly *up;
3586 isl_qpolynomial *c;
3588 if (!qp)
3589 return NULL;
3591 if (type == isl_dim_out)
3592 isl_die(qp->div->ctx, isl_error_invalid,
3593 "output/set dimension does not have a coefficient",
3594 return NULL);
3595 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
3596 return NULL;
3597 type = domain_type(type);
3599 g_pos = pos(qp->dim, type) + t_pos;
3600 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3602 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3603 if (!c)
3604 return NULL;
3605 isl_mat_free(c->div);
3606 c->div = isl_mat_copy(qp->div);
3607 if (!c->div)
3608 goto error;
3609 return c;
3610 error:
3611 isl_qpolynomial_free(c);
3612 return NULL;
3615 /* Homogenize the polynomial in the variables first (inclusive) up to
3616 * last (exclusive) by inserting powers of variable first.
3617 * Variable first is assumed not to appear in the input.
3619 __isl_give struct isl_upoly *isl_upoly_homogenize(
3620 __isl_take struct isl_upoly *up, int deg, int target,
3621 int first, int last)
3623 int i;
3624 struct isl_upoly_rec *rec;
3626 if (!up)
3627 return NULL;
3628 if (isl_upoly_is_zero(up))
3629 return up;
3630 if (deg == target)
3631 return up;
3632 if (isl_upoly_is_cst(up) || up->var < first) {
3633 struct isl_upoly *hom;
3635 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3636 if (!hom)
3637 goto error;
3638 rec = isl_upoly_as_rec(hom);
3639 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3641 return hom;
3644 up = isl_upoly_cow(up);
3645 rec = isl_upoly_as_rec(up);
3646 if (!rec)
3647 goto error;
3649 for (i = 0; i < rec->n; ++i) {
3650 if (isl_upoly_is_zero(rec->p[i]))
3651 continue;
3652 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3653 up->var < last ? deg + i : i, target,
3654 first, last);
3655 if (!rec->p[i])
3656 goto error;
3659 return up;
3660 error:
3661 isl_upoly_free(up);
3662 return NULL;
3665 /* Homogenize the polynomial in the set variables by introducing
3666 * powers of an extra set variable at position 0.
3668 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3669 __isl_take isl_qpolynomial *poly)
3671 unsigned ovar;
3672 unsigned nvar;
3673 int deg = isl_qpolynomial_degree(poly);
3675 if (deg < -1)
3676 goto error;
3678 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3679 poly = isl_qpolynomial_cow(poly);
3680 if (!poly)
3681 goto error;
3683 ovar = isl_space_offset(poly->dim, isl_dim_set);
3684 nvar = isl_space_dim(poly->dim, isl_dim_set);
3685 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3686 ovar, ovar + nvar);
3687 if (!poly->upoly)
3688 goto error;
3690 return poly;
3691 error:
3692 isl_qpolynomial_free(poly);
3693 return NULL;
3696 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
3697 __isl_take isl_mat *div)
3699 isl_term *term;
3700 int n;
3702 if (!space || !div)
3703 goto error;
3705 n = isl_space_dim(space, isl_dim_all) + div->n_row;
3707 term = isl_calloc(space->ctx, struct isl_term,
3708 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3709 if (!term)
3710 goto error;
3712 term->ref = 1;
3713 term->dim = space;
3714 term->div = div;
3715 isl_int_init(term->n);
3716 isl_int_init(term->d);
3718 return term;
3719 error:
3720 isl_space_free(space);
3721 isl_mat_free(div);
3722 return NULL;
3725 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3727 if (!term)
3728 return NULL;
3730 term->ref++;
3731 return term;
3734 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3736 int i;
3737 isl_term *dup;
3738 unsigned total;
3740 if (!term)
3741 return NULL;
3743 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3745 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3746 if (!dup)
3747 return NULL;
3749 isl_int_set(dup->n, term->n);
3750 isl_int_set(dup->d, term->d);
3752 for (i = 0; i < total; ++i)
3753 dup->pow[i] = term->pow[i];
3755 return dup;
3758 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3760 if (!term)
3761 return NULL;
3763 if (term->ref == 1)
3764 return term;
3765 term->ref--;
3766 return isl_term_dup(term);
3769 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
3771 if (!term)
3772 return NULL;
3774 if (--term->ref > 0)
3775 return NULL;
3777 isl_space_free(term->dim);
3778 isl_mat_free(term->div);
3779 isl_int_clear(term->n);
3780 isl_int_clear(term->d);
3781 free(term);
3783 return NULL;
3786 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3788 if (!term)
3789 return 0;
3791 switch (type) {
3792 case isl_dim_param:
3793 case isl_dim_in:
3794 case isl_dim_out: return isl_space_dim(term->dim, type);
3795 case isl_dim_div: return term->div->n_row;
3796 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3797 term->div->n_row;
3798 default: return 0;
3802 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3804 return term ? term->dim->ctx : NULL;
3807 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3809 if (!term)
3810 return;
3811 isl_int_set(*n, term->n);
3814 /* Return the coefficient of the term "term".
3816 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3818 if (!term)
3819 return NULL;
3821 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3822 term->n, term->d);
3825 #undef TYPE
3826 #define TYPE isl_term
3827 static
3828 #include "check_type_range_templ.c"
3830 int isl_term_get_exp(__isl_keep isl_term *term,
3831 enum isl_dim_type type, unsigned pos)
3833 if (isl_term_check_range(term, type, pos, 1) < 0)
3834 return -1;
3836 if (type >= isl_dim_set)
3837 pos += isl_space_dim(term->dim, isl_dim_param);
3838 if (type >= isl_dim_div)
3839 pos += isl_space_dim(term->dim, isl_dim_set);
3841 return term->pow[pos];
3844 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3846 isl_local_space *ls;
3847 isl_aff *aff;
3849 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
3850 return NULL;
3852 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3853 isl_mat_copy(term->div));
3854 aff = isl_aff_alloc(ls);
3855 if (!aff)
3856 return NULL;
3858 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3860 aff = isl_aff_normalize(aff);
3862 return aff;
3865 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3866 isl_stat (*fn)(__isl_take isl_term *term, void *user),
3867 __isl_take isl_term *term, void *user)
3869 int i;
3870 struct isl_upoly_rec *rec;
3872 if (!up || !term)
3873 goto error;
3875 if (isl_upoly_is_zero(up))
3876 return term;
3878 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3879 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3880 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3882 if (isl_upoly_is_cst(up)) {
3883 struct isl_upoly_cst *cst;
3884 cst = isl_upoly_as_cst(up);
3885 if (!cst)
3886 goto error;
3887 term = isl_term_cow(term);
3888 if (!term)
3889 goto error;
3890 isl_int_set(term->n, cst->n);
3891 isl_int_set(term->d, cst->d);
3892 if (fn(isl_term_copy(term), user) < 0)
3893 goto error;
3894 return term;
3897 rec = isl_upoly_as_rec(up);
3898 if (!rec)
3899 goto error;
3901 for (i = 0; i < rec->n; ++i) {
3902 term = isl_term_cow(term);
3903 if (!term)
3904 goto error;
3905 term->pow[up->var] = i;
3906 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3907 if (!term)
3908 goto error;
3910 term->pow[up->var] = 0;
3912 return term;
3913 error:
3914 isl_term_free(term);
3915 return NULL;
3918 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3919 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
3921 isl_term *term;
3923 if (!qp)
3924 return isl_stat_error;
3926 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3927 if (!term)
3928 return isl_stat_error;
3930 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3932 isl_term_free(term);
3934 return term ? isl_stat_ok : isl_stat_error;
3937 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3939 struct isl_upoly *up;
3940 isl_qpolynomial *qp;
3941 int i, n;
3943 if (!term)
3944 return NULL;
3946 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3948 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3949 for (i = 0; i < n; ++i) {
3950 if (!term->pow[i])
3951 continue;
3952 up = isl_upoly_mul(up,
3953 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3956 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3957 if (!qp)
3958 goto error;
3959 isl_mat_free(qp->div);
3960 qp->div = isl_mat_copy(term->div);
3961 if (!qp->div)
3962 goto error;
3964 isl_term_free(term);
3965 return qp;
3966 error:
3967 isl_qpolynomial_free(qp);
3968 isl_term_free(term);
3969 return NULL;
3972 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3973 __isl_take isl_space *space)
3975 int i;
3976 int extra;
3977 unsigned total;
3979 if (!qp || !space)
3980 goto error;
3982 if (isl_space_is_equal(qp->dim, space)) {
3983 isl_space_free(space);
3984 return qp;
3987 qp = isl_qpolynomial_cow(qp);
3988 if (!qp)
3989 goto error;
3991 extra = isl_space_dim(space, isl_dim_set) -
3992 isl_space_dim(qp->dim, isl_dim_set);
3993 total = isl_space_dim(qp->dim, isl_dim_all);
3994 if (qp->div->n_row) {
3995 int *exp;
3997 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3998 if (!exp)
3999 goto error;
4000 for (i = 0; i < qp->div->n_row; ++i)
4001 exp[i] = extra + i;
4002 qp->upoly = expand(qp->upoly, exp, total);
4003 free(exp);
4004 if (!qp->upoly)
4005 goto error;
4007 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4008 if (!qp->div)
4009 goto error;
4010 for (i = 0; i < qp->div->n_row; ++i)
4011 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4013 isl_space_free(qp->dim);
4014 qp->dim = space;
4016 return qp;
4017 error:
4018 isl_space_free(space);
4019 isl_qpolynomial_free(qp);
4020 return NULL;
4023 /* For each parameter or variable that does not appear in qp,
4024 * first eliminate the variable from all constraints and then set it to zero.
4026 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4027 __isl_keep isl_qpolynomial *qp)
4029 int *active = NULL;
4030 int i;
4031 int d;
4032 unsigned nparam;
4033 unsigned nvar;
4035 if (!set || !qp)
4036 goto error;
4038 d = isl_space_dim(set->dim, isl_dim_all);
4039 active = isl_calloc_array(set->ctx, int, d);
4040 if (set_active(qp, active) < 0)
4041 goto error;
4043 for (i = 0; i < d; ++i)
4044 if (!active[i])
4045 break;
4047 if (i == d) {
4048 free(active);
4049 return set;
4052 nparam = isl_space_dim(set->dim, isl_dim_param);
4053 nvar = isl_space_dim(set->dim, isl_dim_set);
4054 for (i = 0; i < nparam; ++i) {
4055 if (active[i])
4056 continue;
4057 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4058 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4060 for (i = 0; i < nvar; ++i) {
4061 if (active[nparam + i])
4062 continue;
4063 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4064 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4067 free(active);
4069 return set;
4070 error:
4071 free(active);
4072 isl_set_free(set);
4073 return NULL;
4076 struct isl_opt_data {
4077 isl_qpolynomial *qp;
4078 int first;
4079 isl_val *opt;
4080 int max;
4083 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4085 struct isl_opt_data *data = (struct isl_opt_data *)user;
4086 isl_val *val;
4088 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4089 if (data->first) {
4090 data->first = 0;
4091 data->opt = val;
4092 } else if (data->max) {
4093 data->opt = isl_val_max(data->opt, val);
4094 } else {
4095 data->opt = isl_val_min(data->opt, val);
4098 return isl_stat_ok;
4101 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4102 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4104 struct isl_opt_data data = { NULL, 1, NULL, max };
4106 if (!set || !qp)
4107 goto error;
4109 if (isl_upoly_is_cst(qp->upoly)) {
4110 isl_set_free(set);
4111 data.opt = isl_qpolynomial_get_constant_val(qp);
4112 isl_qpolynomial_free(qp);
4113 return data.opt;
4116 set = fix_inactive(set, qp);
4118 data.qp = qp;
4119 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4120 goto error;
4122 if (data.first)
4123 data.opt = isl_val_zero(isl_set_get_ctx(set));
4125 isl_set_free(set);
4126 isl_qpolynomial_free(qp);
4127 return data.opt;
4128 error:
4129 isl_set_free(set);
4130 isl_qpolynomial_free(qp);
4131 isl_val_free(data.opt);
4132 return NULL;
4135 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4136 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4138 int i;
4139 int n_sub;
4140 isl_ctx *ctx;
4141 struct isl_upoly **subs;
4142 isl_mat *mat, *diag;
4144 qp = isl_qpolynomial_cow(qp);
4145 if (!qp || !morph)
4146 goto error;
4148 ctx = qp->dim->ctx;
4149 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4151 n_sub = morph->inv->n_row - 1;
4152 if (morph->inv->n_row != morph->inv->n_col)
4153 n_sub += qp->div->n_row;
4154 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4155 if (n_sub && !subs)
4156 goto error;
4158 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4159 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4160 morph->inv->row[0][0], morph->inv->n_col);
4161 if (morph->inv->n_row != morph->inv->n_col)
4162 for (i = 0; i < qp->div->n_row; ++i)
4163 subs[morph->inv->n_row - 1 + i] =
4164 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4166 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4168 for (i = 0; i < n_sub; ++i)
4169 isl_upoly_free(subs[i]);
4170 free(subs);
4172 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4173 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4174 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4175 mat = isl_mat_diagonal(mat, diag);
4176 qp->div = isl_mat_product(qp->div, mat);
4177 isl_space_free(qp->dim);
4178 qp->dim = isl_space_copy(morph->ran->dim);
4180 if (!qp->upoly || !qp->div || !qp->dim)
4181 goto error;
4183 isl_morph_free(morph);
4185 return qp;
4186 error:
4187 isl_qpolynomial_free(qp);
4188 isl_morph_free(morph);
4189 return NULL;
4192 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4193 __isl_take isl_union_pw_qpolynomial *upwqp1,
4194 __isl_take isl_union_pw_qpolynomial *upwqp2)
4196 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4197 &isl_pw_qpolynomial_mul);
4200 /* Reorder the dimension of "qp" according to the given reordering.
4202 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4203 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4205 isl_space *space;
4207 qp = isl_qpolynomial_cow(qp);
4208 if (!qp)
4209 goto error;
4211 r = isl_reordering_extend(r, qp->div->n_row);
4212 if (!r)
4213 goto error;
4215 qp->div = isl_local_reorder(qp->div, isl_reordering_copy(r));
4216 if (!qp->div)
4217 goto error;
4219 qp->upoly = reorder(qp->upoly, r->pos);
4220 if (!qp->upoly)
4221 goto error;
4223 space = isl_reordering_get_space(r);
4224 qp = isl_qpolynomial_reset_domain_space(qp, space);
4226 isl_reordering_free(r);
4227 return qp;
4228 error:
4229 isl_qpolynomial_free(qp);
4230 isl_reordering_free(r);
4231 return NULL;
4234 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4235 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4237 isl_bool equal_params;
4239 if (!qp || !model)
4240 goto error;
4242 equal_params = isl_space_has_equal_params(qp->dim, model);
4243 if (equal_params < 0)
4244 goto error;
4245 if (!equal_params) {
4246 isl_reordering *exp;
4248 exp = isl_parameter_alignment_reordering(qp->dim, model);
4249 exp = isl_reordering_extend_space(exp,
4250 isl_qpolynomial_get_domain_space(qp));
4251 qp = isl_qpolynomial_realign_domain(qp, exp);
4254 isl_space_free(model);
4255 return qp;
4256 error:
4257 isl_space_free(model);
4258 isl_qpolynomial_free(qp);
4259 return NULL;
4262 struct isl_split_periods_data {
4263 int max_periods;
4264 isl_pw_qpolynomial *res;
4267 /* Create a slice where the integer division "div" has the fixed value "v".
4268 * In particular, if "div" refers to floor(f/m), then create a slice
4270 * m v <= f <= m v + (m - 1)
4272 * or
4274 * f - m v >= 0
4275 * -f + m v + (m - 1) >= 0
4277 static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
4278 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4280 int total;
4281 isl_basic_set *bset = NULL;
4282 int k;
4284 if (!space || !qp)
4285 goto error;
4287 total = isl_space_dim(space, isl_dim_all);
4288 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4290 k = isl_basic_set_alloc_inequality(bset);
4291 if (k < 0)
4292 goto error;
4293 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4294 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4296 k = isl_basic_set_alloc_inequality(bset);
4297 if (k < 0)
4298 goto error;
4299 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4300 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4301 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4302 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4304 isl_space_free(space);
4305 return isl_set_from_basic_set(bset);
4306 error:
4307 isl_basic_set_free(bset);
4308 isl_space_free(space);
4309 return NULL;
4312 static isl_stat split_periods(__isl_take isl_set *set,
4313 __isl_take isl_qpolynomial *qp, void *user);
4315 /* Create a slice of the domain "set" such that integer division "div"
4316 * has the fixed value "v" and add the results to data->res,
4317 * replacing the integer division by "v" in "qp".
4319 static isl_stat set_div(__isl_take isl_set *set,
4320 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4321 struct isl_split_periods_data *data)
4323 int i;
4324 int total;
4325 isl_set *slice;
4326 struct isl_upoly *cst;
4328 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4329 set = isl_set_intersect(set, slice);
4331 if (!qp)
4332 goto error;
4334 total = isl_space_dim(qp->dim, isl_dim_all);
4336 for (i = div + 1; i < qp->div->n_row; ++i) {
4337 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4338 continue;
4339 isl_int_addmul(qp->div->row[i][1],
4340 qp->div->row[i][2 + total + div], v);
4341 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4344 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4345 qp = substitute_div(qp, div, cst);
4347 return split_periods(set, qp, data);
4348 error:
4349 isl_set_free(set);
4350 isl_qpolynomial_free(qp);
4351 return isl_stat_error;
4354 /* Split the domain "set" such that integer division "div"
4355 * has a fixed value (ranging from "min" to "max") on each slice
4356 * and add the results to data->res.
4358 static isl_stat split_div(__isl_take isl_set *set,
4359 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4360 struct isl_split_periods_data *data)
4362 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4363 isl_set *set_i = isl_set_copy(set);
4364 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4366 if (set_div(set_i, qp_i, div, min, data) < 0)
4367 goto error;
4369 isl_set_free(set);
4370 isl_qpolynomial_free(qp);
4371 return isl_stat_ok;
4372 error:
4373 isl_set_free(set);
4374 isl_qpolynomial_free(qp);
4375 return isl_stat_error;
4378 /* If "qp" refers to any integer division
4379 * that can only attain "max_periods" distinct values on "set"
4380 * then split the domain along those distinct values.
4381 * Add the results (or the original if no splitting occurs)
4382 * to data->res.
4384 static isl_stat split_periods(__isl_take isl_set *set,
4385 __isl_take isl_qpolynomial *qp, void *user)
4387 int i;
4388 isl_pw_qpolynomial *pwqp;
4389 struct isl_split_periods_data *data;
4390 isl_int min, max;
4391 int total;
4392 isl_stat r = isl_stat_ok;
4394 data = (struct isl_split_periods_data *)user;
4396 if (!set || !qp)
4397 goto error;
4399 if (qp->div->n_row == 0) {
4400 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4401 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4402 return isl_stat_ok;
4405 isl_int_init(min);
4406 isl_int_init(max);
4407 total = isl_space_dim(qp->dim, isl_dim_all);
4408 for (i = 0; i < qp->div->n_row; ++i) {
4409 enum isl_lp_result lp_res;
4411 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4412 qp->div->n_row) != -1)
4413 continue;
4415 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4416 set->ctx->one, &min, NULL, NULL);
4417 if (lp_res == isl_lp_error)
4418 goto error2;
4419 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4420 continue;
4421 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4423 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4424 set->ctx->one, &max, NULL, NULL);
4425 if (lp_res == isl_lp_error)
4426 goto error2;
4427 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4428 continue;
4429 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4431 isl_int_sub(max, max, min);
4432 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4433 isl_int_add(max, max, min);
4434 break;
4438 if (i < qp->div->n_row) {
4439 r = split_div(set, qp, i, min, max, data);
4440 } else {
4441 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4442 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4445 isl_int_clear(max);
4446 isl_int_clear(min);
4448 return r;
4449 error2:
4450 isl_int_clear(max);
4451 isl_int_clear(min);
4452 error:
4453 isl_set_free(set);
4454 isl_qpolynomial_free(qp);
4455 return isl_stat_error;
4458 /* If any quasi-polynomial in pwqp refers to any integer division
4459 * that can only attain "max_periods" distinct values on its domain
4460 * then split the domain along those distinct values.
4462 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4463 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4465 struct isl_split_periods_data data;
4467 data.max_periods = max_periods;
4468 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4470 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4471 goto error;
4473 isl_pw_qpolynomial_free(pwqp);
4475 return data.res;
4476 error:
4477 isl_pw_qpolynomial_free(data.res);
4478 isl_pw_qpolynomial_free(pwqp);
4479 return NULL;
4482 /* Construct a piecewise quasipolynomial that is constant on the given
4483 * domain. In particular, it is
4484 * 0 if cst == 0
4485 * 1 if cst == 1
4486 * infinity if cst == -1
4488 * If cst == -1, then explicitly check whether the domain is empty and,
4489 * if so, return 0 instead.
4491 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4492 __isl_take isl_basic_set *bset, int cst)
4494 isl_space *dim;
4495 isl_qpolynomial *qp;
4497 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4498 cst = 0;
4499 if (!bset)
4500 return NULL;
4502 bset = isl_basic_set_params(bset);
4503 dim = isl_basic_set_get_space(bset);
4504 if (cst < 0)
4505 qp = isl_qpolynomial_infty_on_domain(dim);
4506 else if (cst == 0)
4507 qp = isl_qpolynomial_zero_on_domain(dim);
4508 else
4509 qp = isl_qpolynomial_one_on_domain(dim);
4510 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4513 /* Factor bset, call fn on each of the factors and return the product.
4515 * If no factors can be found, simply call fn on the input.
4516 * Otherwise, construct the factors based on the factorizer,
4517 * call fn on each factor and compute the product.
4519 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4520 __isl_take isl_basic_set *bset,
4521 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4523 int i, n;
4524 isl_space *space;
4525 isl_set *set;
4526 isl_factorizer *f;
4527 isl_qpolynomial *qp;
4528 isl_pw_qpolynomial *pwqp;
4529 unsigned nparam;
4530 unsigned nvar;
4532 f = isl_basic_set_factorizer(bset);
4533 if (!f)
4534 goto error;
4535 if (f->n_group == 0) {
4536 isl_factorizer_free(f);
4537 return fn(bset);
4540 nparam = isl_basic_set_dim(bset, isl_dim_param);
4541 nvar = isl_basic_set_dim(bset, isl_dim_set);
4543 space = isl_basic_set_get_space(bset);
4544 space = isl_space_params(space);
4545 set = isl_set_universe(isl_space_copy(space));
4546 qp = isl_qpolynomial_one_on_domain(space);
4547 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4549 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4551 for (i = 0, n = 0; i < f->n_group; ++i) {
4552 isl_basic_set *bset_i;
4553 isl_pw_qpolynomial *pwqp_i;
4555 bset_i = isl_basic_set_copy(bset);
4556 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4557 nparam + n + f->len[i], nvar - n - f->len[i]);
4558 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4559 nparam, n);
4560 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4561 n + f->len[i], nvar - n - f->len[i]);
4562 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4564 pwqp_i = fn(bset_i);
4565 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4567 n += f->len[i];
4570 isl_basic_set_free(bset);
4571 isl_factorizer_free(f);
4573 return pwqp;
4574 error:
4575 isl_basic_set_free(bset);
4576 return NULL;
4579 /* Factor bset, call fn on each of the factors and return the product.
4580 * The function is assumed to evaluate to zero on empty domains,
4581 * to one on zero-dimensional domains and to infinity on unbounded domains
4582 * and will not be called explicitly on zero-dimensional or unbounded domains.
4584 * We first check for some special cases and remove all equalities.
4585 * Then we hand over control to compressed_multiplicative_call.
4587 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4588 __isl_take isl_basic_set *bset,
4589 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4591 isl_bool bounded;
4592 isl_morph *morph;
4593 isl_pw_qpolynomial *pwqp;
4595 if (!bset)
4596 return NULL;
4598 if (isl_basic_set_plain_is_empty(bset))
4599 return constant_on_domain(bset, 0);
4601 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4602 return constant_on_domain(bset, 1);
4604 bounded = isl_basic_set_is_bounded(bset);
4605 if (bounded < 0)
4606 goto error;
4607 if (!bounded)
4608 return constant_on_domain(bset, -1);
4610 if (bset->n_eq == 0)
4611 return compressed_multiplicative_call(bset, fn);
4613 morph = isl_basic_set_full_compression(bset);
4614 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4616 pwqp = compressed_multiplicative_call(bset, fn);
4618 morph = isl_morph_dom_params(morph);
4619 morph = isl_morph_ran_params(morph);
4620 morph = isl_morph_inverse(morph);
4622 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4624 return pwqp;
4625 error:
4626 isl_basic_set_free(bset);
4627 return NULL;
4630 /* Drop all floors in "qp", turning each integer division [a/m] into
4631 * a rational division a/m. If "down" is set, then the integer division
4632 * is replaced by (a-(m-1))/m instead.
4634 static __isl_give isl_qpolynomial *qp_drop_floors(
4635 __isl_take isl_qpolynomial *qp, int down)
4637 int i;
4638 struct isl_upoly *s;
4640 if (!qp)
4641 return NULL;
4642 if (qp->div->n_row == 0)
4643 return qp;
4645 qp = isl_qpolynomial_cow(qp);
4646 if (!qp)
4647 return NULL;
4649 for (i = qp->div->n_row - 1; i >= 0; --i) {
4650 if (down) {
4651 isl_int_sub(qp->div->row[i][1],
4652 qp->div->row[i][1], qp->div->row[i][0]);
4653 isl_int_add_ui(qp->div->row[i][1],
4654 qp->div->row[i][1], 1);
4656 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4657 qp->div->row[i][0], qp->div->n_col - 1);
4658 qp = substitute_div(qp, i, s);
4659 if (!qp)
4660 return NULL;
4663 return qp;
4666 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4667 * a rational division a/m.
4669 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4670 __isl_take isl_pw_qpolynomial *pwqp)
4672 int i;
4674 if (!pwqp)
4675 return NULL;
4677 if (isl_pw_qpolynomial_is_zero(pwqp))
4678 return pwqp;
4680 pwqp = isl_pw_qpolynomial_cow(pwqp);
4681 if (!pwqp)
4682 return NULL;
4684 for (i = 0; i < pwqp->n; ++i) {
4685 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4686 if (!pwqp->p[i].qp)
4687 goto error;
4690 return pwqp;
4691 error:
4692 isl_pw_qpolynomial_free(pwqp);
4693 return NULL;
4696 /* Adjust all the integer divisions in "qp" such that they are at least
4697 * one over the given orthant (identified by "signs"). This ensures
4698 * that they will still be non-negative even after subtracting (m-1)/m.
4700 * In particular, f is replaced by f' + v, changing f = [a/m]
4701 * to f' = [(a - m v)/m].
4702 * If the constant term k in a is smaller than m,
4703 * the constant term of v is set to floor(k/m) - 1.
4704 * For any other term, if the coefficient c and the variable x have
4705 * the same sign, then no changes are needed.
4706 * Otherwise, if the variable is positive (and c is negative),
4707 * then the coefficient of x in v is set to floor(c/m).
4708 * If the variable is negative (and c is positive),
4709 * then the coefficient of x in v is set to ceil(c/m).
4711 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4712 int *signs)
4714 int i, j;
4715 int total;
4716 isl_vec *v = NULL;
4717 struct isl_upoly *s;
4719 qp = isl_qpolynomial_cow(qp);
4720 if (!qp)
4721 return NULL;
4722 qp->div = isl_mat_cow(qp->div);
4723 if (!qp->div)
4724 goto error;
4726 total = isl_space_dim(qp->dim, isl_dim_all);
4727 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4729 for (i = 0; i < qp->div->n_row; ++i) {
4730 isl_int *row = qp->div->row[i];
4731 v = isl_vec_clr(v);
4732 if (!v)
4733 goto error;
4734 if (isl_int_lt(row[1], row[0])) {
4735 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4736 isl_int_sub_ui(v->el[0], v->el[0], 1);
4737 isl_int_submul(row[1], row[0], v->el[0]);
4739 for (j = 0; j < total; ++j) {
4740 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4741 continue;
4742 if (signs[j] < 0)
4743 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4744 else
4745 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4746 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4748 for (j = 0; j < i; ++j) {
4749 if (isl_int_sgn(row[2 + total + j]) >= 0)
4750 continue;
4751 isl_int_fdiv_q(v->el[1 + total + j],
4752 row[2 + total + j], row[0]);
4753 isl_int_submul(row[2 + total + j],
4754 row[0], v->el[1 + total + j]);
4756 for (j = i + 1; j < qp->div->n_row; ++j) {
4757 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4758 continue;
4759 isl_seq_combine(qp->div->row[j] + 1,
4760 qp->div->ctx->one, qp->div->row[j] + 1,
4761 qp->div->row[j][2 + total + i], v->el, v->size);
4763 isl_int_set_si(v->el[1 + total + i], 1);
4764 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4765 qp->div->ctx->one, v->size);
4766 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4767 isl_upoly_free(s);
4768 if (!qp->upoly)
4769 goto error;
4772 isl_vec_free(v);
4773 return qp;
4774 error:
4775 isl_vec_free(v);
4776 isl_qpolynomial_free(qp);
4777 return NULL;
4780 struct isl_to_poly_data {
4781 int sign;
4782 isl_pw_qpolynomial *res;
4783 isl_qpolynomial *qp;
4786 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4787 * We first make all integer divisions positive and then split the
4788 * quasipolynomials into terms with sign data->sign (the direction
4789 * of the requested approximation) and terms with the opposite sign.
4790 * In the first set of terms, each integer division [a/m] is
4791 * overapproximated by a/m, while in the second it is underapproximated
4792 * by (a-(m-1))/m.
4794 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
4795 int *signs, void *user)
4797 struct isl_to_poly_data *data = user;
4798 isl_pw_qpolynomial *t;
4799 isl_qpolynomial *qp, *up, *down;
4801 qp = isl_qpolynomial_copy(data->qp);
4802 qp = make_divs_pos(qp, signs);
4804 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4805 up = qp_drop_floors(up, 0);
4806 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4807 down = qp_drop_floors(down, 1);
4809 isl_qpolynomial_free(qp);
4810 qp = isl_qpolynomial_add(up, down);
4812 t = isl_pw_qpolynomial_alloc(orthant, qp);
4813 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4815 return isl_stat_ok;
4818 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4819 * the polynomial will be an overapproximation. If "sign" is negative,
4820 * it will be an underapproximation. If "sign" is zero, the approximation
4821 * will lie somewhere in between.
4823 * In particular, is sign == 0, we simply drop the floors, turning
4824 * the integer divisions into rational divisions.
4825 * Otherwise, we split the domains into orthants, make all integer divisions
4826 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4827 * depending on the requested sign and the sign of the term in which
4828 * the integer division appears.
4830 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4831 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4833 int i;
4834 struct isl_to_poly_data data;
4836 if (sign == 0)
4837 return pwqp_drop_floors(pwqp);
4839 if (!pwqp)
4840 return NULL;
4842 data.sign = sign;
4843 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4845 for (i = 0; i < pwqp->n; ++i) {
4846 if (pwqp->p[i].qp->div->n_row == 0) {
4847 isl_pw_qpolynomial *t;
4848 t = isl_pw_qpolynomial_alloc(
4849 isl_set_copy(pwqp->p[i].set),
4850 isl_qpolynomial_copy(pwqp->p[i].qp));
4851 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4852 continue;
4854 data.qp = pwqp->p[i].qp;
4855 if (isl_set_foreach_orthant(pwqp->p[i].set,
4856 &to_polynomial_on_orthant, &data) < 0)
4857 goto error;
4860 isl_pw_qpolynomial_free(pwqp);
4862 return data.res;
4863 error:
4864 isl_pw_qpolynomial_free(pwqp);
4865 isl_pw_qpolynomial_free(data.res);
4866 return NULL;
4869 static __isl_give isl_pw_qpolynomial *poly_entry(
4870 __isl_take isl_pw_qpolynomial *pwqp, void *user)
4872 int *sign = user;
4874 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
4877 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4878 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4880 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
4881 &poly_entry, &sign);
4884 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4885 __isl_take isl_qpolynomial *qp)
4887 int i, k;
4888 isl_space *dim;
4889 isl_vec *aff = NULL;
4890 isl_basic_map *bmap = NULL;
4891 unsigned pos;
4892 unsigned n_div;
4894 if (!qp)
4895 return NULL;
4896 if (!isl_upoly_is_affine(qp->upoly))
4897 isl_die(qp->dim->ctx, isl_error_invalid,
4898 "input quasi-polynomial not affine", goto error);
4899 aff = isl_qpolynomial_extract_affine(qp);
4900 if (!aff)
4901 goto error;
4902 dim = isl_qpolynomial_get_space(qp);
4903 pos = 1 + isl_space_offset(dim, isl_dim_out);
4904 n_div = qp->div->n_row;
4905 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4907 for (i = 0; i < n_div; ++i) {
4908 k = isl_basic_map_alloc_div(bmap);
4909 if (k < 0)
4910 goto error;
4911 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4912 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4913 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4914 goto error;
4916 k = isl_basic_map_alloc_equality(bmap);
4917 if (k < 0)
4918 goto error;
4919 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4920 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4921 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4923 isl_vec_free(aff);
4924 isl_qpolynomial_free(qp);
4925 bmap = isl_basic_map_finalize(bmap);
4926 return bmap;
4927 error:
4928 isl_vec_free(aff);
4929 isl_qpolynomial_free(qp);
4930 isl_basic_map_free(bmap);
4931 return NULL;