isl_map_simplify.c: eliminate_selected_unit_divs: use *_is_marked_unknown
[isl.git] / isl_polynomial.c
blobd47de587b36740a2efcb9ca918d61a8849670adc
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 EL_BASE
32 #define EL_BASE qpolynomial
34 #include <isl_list_templ.c>
36 #undef EL_BASE
37 #define EL_BASE pw_qpolynomial
39 #include <isl_list_templ.c>
41 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
43 switch (type) {
44 case isl_dim_param: return 0;
45 case isl_dim_in: return space->nparam;
46 case isl_dim_out: return space->nparam + space->n_in;
47 default: return 0;
51 isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
53 if (!poly)
54 return isl_bool_error;
56 return isl_bool_ok(poly->var < 0);
59 __isl_keep isl_poly_cst *isl_poly_as_cst(__isl_keep isl_poly *poly)
61 if (!poly)
62 return NULL;
64 isl_assert(poly->ctx, poly->var < 0, return NULL);
66 return (isl_poly_cst *) poly;
69 __isl_keep isl_poly_rec *isl_poly_as_rec(__isl_keep isl_poly *poly)
71 if (!poly)
72 return NULL;
74 isl_assert(poly->ctx, poly->var >= 0, return NULL);
76 return (isl_poly_rec *) poly;
79 /* Compare two polynomials.
81 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
82 * than "poly2" and 0 if they are equal.
84 static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1,
85 __isl_keep isl_poly *poly2)
87 int i;
88 isl_bool is_cst1;
89 isl_poly_rec *rec1, *rec2;
91 if (poly1 == poly2)
92 return 0;
93 is_cst1 = isl_poly_is_cst(poly1);
94 if (is_cst1 < 0)
95 return -1;
96 if (!poly2)
97 return 1;
98 if (poly1->var != poly2->var)
99 return poly1->var - poly2->var;
101 if (is_cst1) {
102 isl_poly_cst *cst1, *cst2;
103 int cmp;
105 cst1 = isl_poly_as_cst(poly1);
106 cst2 = isl_poly_as_cst(poly2);
107 if (!cst1 || !cst2)
108 return 0;
109 cmp = isl_int_cmp(cst1->n, cst2->n);
110 if (cmp != 0)
111 return cmp;
112 return isl_int_cmp(cst1->d, cst2->d);
115 rec1 = isl_poly_as_rec(poly1);
116 rec2 = isl_poly_as_rec(poly2);
117 if (!rec1 || !rec2)
118 return 0;
120 if (rec1->n != rec2->n)
121 return rec1->n - rec2->n;
123 for (i = 0; i < rec1->n; ++i) {
124 int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
125 if (cmp != 0)
126 return cmp;
129 return 0;
132 isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1,
133 __isl_keep isl_poly *poly2)
135 int i;
136 isl_bool is_cst1;
137 isl_poly_rec *rec1, *rec2;
139 is_cst1 = isl_poly_is_cst(poly1);
140 if (is_cst1 < 0 || !poly2)
141 return isl_bool_error;
142 if (poly1 == poly2)
143 return isl_bool_true;
144 if (poly1->var != poly2->var)
145 return isl_bool_false;
146 if (is_cst1) {
147 isl_poly_cst *cst1, *cst2;
148 int r;
149 cst1 = isl_poly_as_cst(poly1);
150 cst2 = isl_poly_as_cst(poly2);
151 if (!cst1 || !cst2)
152 return isl_bool_error;
153 r = isl_int_eq(cst1->n, cst2->n) &&
154 isl_int_eq(cst1->d, cst2->d);
155 return isl_bool_ok(r);
158 rec1 = isl_poly_as_rec(poly1);
159 rec2 = isl_poly_as_rec(poly2);
160 if (!rec1 || !rec2)
161 return isl_bool_error;
163 if (rec1->n != rec2->n)
164 return isl_bool_false;
166 for (i = 0; i < rec1->n; ++i) {
167 isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
168 if (eq < 0 || !eq)
169 return eq;
172 return isl_bool_true;
175 isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
177 isl_bool is_cst;
178 isl_poly_cst *cst;
180 is_cst = isl_poly_is_cst(poly);
181 if (is_cst < 0 || !is_cst)
182 return is_cst;
184 cst = isl_poly_as_cst(poly);
185 if (!cst)
186 return isl_bool_error;
188 return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d));
191 int isl_poly_sgn(__isl_keep isl_poly *poly)
193 isl_bool is_cst;
194 isl_poly_cst *cst;
196 is_cst = isl_poly_is_cst(poly);
197 if (is_cst < 0 || !is_cst)
198 return 0;
200 cst = isl_poly_as_cst(poly);
201 if (!cst)
202 return 0;
204 return isl_int_sgn(cst->n);
207 isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
209 isl_bool is_cst;
210 isl_poly_cst *cst;
212 is_cst = isl_poly_is_cst(poly);
213 if (is_cst < 0 || !is_cst)
214 return is_cst;
216 cst = isl_poly_as_cst(poly);
217 if (!cst)
218 return isl_bool_error;
220 return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d));
223 isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
225 isl_bool is_cst;
226 isl_poly_cst *cst;
228 is_cst = isl_poly_is_cst(poly);
229 if (is_cst < 0 || !is_cst)
230 return is_cst;
232 cst = isl_poly_as_cst(poly);
233 if (!cst)
234 return isl_bool_error;
236 return isl_bool_ok(isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d));
239 isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
241 isl_bool is_cst;
242 isl_poly_cst *cst;
244 is_cst = isl_poly_is_cst(poly);
245 if (is_cst < 0 || !is_cst)
246 return is_cst;
248 cst = isl_poly_as_cst(poly);
249 if (!cst)
250 return isl_bool_error;
252 return isl_bool_ok(isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d));
255 isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
257 isl_bool is_cst;
258 isl_poly_cst *cst;
259 int r;
261 is_cst = isl_poly_is_cst(poly);
262 if (is_cst < 0 || !is_cst)
263 return is_cst;
265 cst = isl_poly_as_cst(poly);
266 if (!cst)
267 return isl_bool_error;
269 r = isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
270 return isl_bool_ok(r);
273 isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
275 isl_bool is_cst;
276 isl_poly_cst *cst;
278 is_cst = isl_poly_is_cst(poly);
279 if (is_cst < 0 || !is_cst)
280 return is_cst;
282 cst = isl_poly_as_cst(poly);
283 if (!cst)
284 return isl_bool_error;
286 return isl_bool_ok(isl_int_is_negone(cst->n) && isl_int_is_one(cst->d));
289 __isl_give isl_poly_cst *isl_poly_cst_alloc(isl_ctx *ctx)
291 isl_poly_cst *cst;
293 cst = isl_alloc_type(ctx, struct isl_poly_cst);
294 if (!cst)
295 return NULL;
297 cst->poly.ref = 1;
298 cst->poly.ctx = ctx;
299 isl_ctx_ref(ctx);
300 cst->poly.var = -1;
302 isl_int_init(cst->n);
303 isl_int_init(cst->d);
305 return cst;
308 __isl_give isl_poly *isl_poly_zero(isl_ctx *ctx)
310 isl_poly_cst *cst;
312 cst = isl_poly_cst_alloc(ctx);
313 if (!cst)
314 return NULL;
316 isl_int_set_si(cst->n, 0);
317 isl_int_set_si(cst->d, 1);
319 return &cst->poly;
322 __isl_give isl_poly *isl_poly_one(isl_ctx *ctx)
324 isl_poly_cst *cst;
326 cst = isl_poly_cst_alloc(ctx);
327 if (!cst)
328 return NULL;
330 isl_int_set_si(cst->n, 1);
331 isl_int_set_si(cst->d, 1);
333 return &cst->poly;
336 __isl_give isl_poly *isl_poly_infty(isl_ctx *ctx)
338 isl_poly_cst *cst;
340 cst = isl_poly_cst_alloc(ctx);
341 if (!cst)
342 return NULL;
344 isl_int_set_si(cst->n, 1);
345 isl_int_set_si(cst->d, 0);
347 return &cst->poly;
350 __isl_give isl_poly *isl_poly_neginfty(isl_ctx *ctx)
352 isl_poly_cst *cst;
354 cst = isl_poly_cst_alloc(ctx);
355 if (!cst)
356 return NULL;
358 isl_int_set_si(cst->n, -1);
359 isl_int_set_si(cst->d, 0);
361 return &cst->poly;
364 __isl_give isl_poly *isl_poly_nan(isl_ctx *ctx)
366 isl_poly_cst *cst;
368 cst = isl_poly_cst_alloc(ctx);
369 if (!cst)
370 return NULL;
372 isl_int_set_si(cst->n, 0);
373 isl_int_set_si(cst->d, 0);
375 return &cst->poly;
378 __isl_give isl_poly *isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
380 isl_poly_cst *cst;
382 cst = isl_poly_cst_alloc(ctx);
383 if (!cst)
384 return NULL;
386 isl_int_set(cst->n, n);
387 isl_int_set(cst->d, d);
389 return &cst->poly;
392 __isl_give isl_poly_rec *isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
394 isl_poly_rec *rec;
396 isl_assert(ctx, var >= 0, return NULL);
397 isl_assert(ctx, size >= 0, return NULL);
398 rec = isl_calloc(ctx, struct isl_poly_rec,
399 sizeof(struct isl_poly_rec) +
400 size * sizeof(struct isl_poly *));
401 if (!rec)
402 return NULL;
404 rec->poly.ref = 1;
405 rec->poly.ctx = ctx;
406 isl_ctx_ref(ctx);
407 rec->poly.var = var;
409 rec->n = 0;
410 rec->size = size;
412 return rec;
415 /* Return the domain space of "qp".
416 * This may be either a copy or the space itself
417 * if there is only one reference to "qp".
418 * This allows the space to be modified inplace
419 * if both the quasi-polynomial and its domain space
420 * have only a single reference.
421 * The caller is not allowed to modify "qp" between this call and
422 * a subsequent call to isl_qpolynomial_restore_domain_space.
423 * The only exception is that isl_qpolynomial_free can be called instead.
425 static __isl_give isl_space *isl_qpolynomial_take_domain_space(
426 __isl_keep isl_qpolynomial *qp)
428 isl_space *space;
430 if (!qp)
431 return NULL;
432 if (qp->ref != 1)
433 return isl_qpolynomial_get_domain_space(qp);
434 space = qp->dim;
435 qp->dim = NULL;
436 return space;
439 /* Set the domain space of "qp" to "space",
440 * where the domain space of "qp" may be missing
441 * due to a preceding call to isl_qpolynomial_take_domain_space.
442 * However, in this case, "qp" only has a single reference and
443 * then the call to isl_qpolynomial_cow has no effect.
445 static __isl_give isl_qpolynomial *isl_qpolynomial_restore_domain_space(
446 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
448 if (!qp || !space)
449 goto error;
451 if (qp->dim == space) {
452 isl_space_free(space);
453 return qp;
456 qp = isl_qpolynomial_cow(qp);
457 if (!qp)
458 goto error;
459 isl_space_free(qp->dim);
460 qp->dim = space;
462 return qp;
463 error:
464 isl_qpolynomial_free(qp);
465 isl_space_free(space);
466 return NULL;
469 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
470 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
472 return isl_qpolynomial_restore_domain_space(qp, space);
475 /* Reset the space of "qp". This function is called from isl_pw_templ.c
476 * and doesn't know if the space of an element object is represented
477 * directly or through its domain. It therefore passes along both.
479 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
480 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
481 __isl_take isl_space *domain)
483 isl_space_free(space);
484 return isl_qpolynomial_reset_domain_space(qp, domain);
487 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
489 return qp ? qp->dim->ctx : NULL;
492 /* Return the domain space of "qp".
494 static __isl_keep isl_space *isl_qpolynomial_peek_domain_space(
495 __isl_keep isl_qpolynomial *qp)
497 return qp ? qp->dim : NULL;
500 /* Return a copy of the domain space of "qp".
502 __isl_give isl_space *isl_qpolynomial_get_domain_space(
503 __isl_keep isl_qpolynomial *qp)
505 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp));
508 #undef TYPE
509 #define TYPE isl_qpolynomial
510 #undef PEEK_SPACE
511 #define PEEK_SPACE peek_domain_space
513 static
514 #include "isl_type_has_equal_space_bin_templ.c"
515 static
516 #include "isl_type_check_equal_space_templ.c"
518 #undef PEEK_SPACE
520 /* Return a copy of the local variables of "qp".
522 __isl_keep isl_local *isl_qpolynomial_get_local(
523 __isl_keep isl_qpolynomial *qp)
525 return qp ? isl_local_copy(qp->div) : NULL;
528 /* Return the local variables of "qp".
529 * This may be either a copy or the local variables themselves
530 * if there is only one reference to "qp".
531 * This allows the local variables to be modified in-place
532 * if both the quasi-polynomial and its local variables
533 * have only a single reference.
534 * The caller is not allowed to modify "qp" between this call and
535 * the subsequent call to isl_qpolynomial_restore_local.
536 * The only exception is that isl_qpolynomial_free can be called instead.
538 static __isl_give isl_local *isl_qpolynomial_take_local(
539 __isl_keep isl_qpolynomial *qp)
541 isl_local *local;
543 if (!qp)
544 return NULL;
545 if (qp->ref != 1)
546 return isl_qpolynomial_get_local(qp);
547 local = qp->div;
548 qp->div = NULL;
549 return local;
552 /* Set the local variables of "qp" to "local",
553 * where the local variables of "qp" may be missing
554 * due to a preceding call to isl_qpolynomial_take_local.
555 * However, in this case, "qp" only has a single reference and
556 * then the call to isl_qpolynomial_cow has no effect.
558 static __isl_give isl_qpolynomial *isl_qpolynomial_restore_local(
559 __isl_keep isl_qpolynomial *qp, __isl_take isl_local *local)
561 if (!qp || !local)
562 goto error;
564 if (qp->div == local) {
565 isl_local_free(local);
566 return qp;
569 qp = isl_qpolynomial_cow(qp);
570 if (!qp)
571 goto error;
572 isl_local_free(qp->div);
573 qp->div = local;
575 return qp;
576 error:
577 isl_qpolynomial_free(qp);
578 isl_local_free(local);
579 return NULL;
582 /* Return a copy of the local space on which "qp" is defined.
584 static __isl_give isl_local_space *isl_qpolynomial_get_domain_local_space(
585 __isl_keep isl_qpolynomial *qp)
587 isl_space *space;
588 isl_local *local;
590 if (!qp)
591 return NULL;
593 space = isl_qpolynomial_get_domain_space(qp);
594 local = isl_qpolynomial_get_local(qp);
595 return isl_local_space_alloc_div(space, local);
598 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
600 isl_space *space;
601 if (!qp)
602 return NULL;
603 space = isl_space_copy(qp->dim);
604 space = isl_space_from_domain(space);
605 space = isl_space_add_dims(space, isl_dim_out, 1);
606 return space;
609 /* Return the number of variables of the given type in the domain of "qp".
611 isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp,
612 enum isl_dim_type type)
614 isl_space *space;
615 isl_size dim;
617 space = isl_qpolynomial_peek_domain_space(qp);
619 if (!space)
620 return isl_size_error;
621 if (type == isl_dim_div)
622 return qp->div->n_row;
623 dim = isl_space_dim(space, type);
624 if (dim < 0)
625 return isl_size_error;
626 if (type == isl_dim_all) {
627 isl_size n_div;
629 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
630 if (n_div < 0)
631 return isl_size_error;
632 dim += n_div;
634 return dim;
637 /* Given the type of a dimension of an isl_qpolynomial,
638 * return the type of the corresponding dimension in its domain.
639 * This function is only called for "type" equal to isl_dim_in or
640 * isl_dim_param.
642 static enum isl_dim_type domain_type(enum isl_dim_type type)
644 return type == isl_dim_in ? isl_dim_set : type;
647 /* Externally, an isl_qpolynomial has a map space, but internally, the
648 * ls field corresponds to the domain of that space.
650 isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
651 enum isl_dim_type type)
653 if (!qp)
654 return isl_size_error;
655 if (type == isl_dim_out)
656 return 1;
657 type = domain_type(type);
658 return isl_qpolynomial_domain_dim(qp, type);
661 /* Return the offset of the first variable of type "type" within
662 * the variables of the domain of "qp".
664 static isl_size isl_qpolynomial_domain_var_offset(
665 __isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
667 isl_space *space;
669 space = isl_qpolynomial_peek_domain_space(qp);
671 switch (type) {
672 case isl_dim_param:
673 case isl_dim_set: return isl_space_offset(space, type);
674 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
675 case isl_dim_cst:
676 default:
677 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
678 "invalid dimension type", return isl_size_error);
682 /* Return the offset of the first coefficient of type "type" in
683 * the domain of "qp".
685 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp,
686 enum isl_dim_type type)
688 switch (type) {
689 case isl_dim_cst:
690 return 0;
691 case isl_dim_param:
692 case isl_dim_set:
693 case isl_dim_div:
694 return 1 + isl_qpolynomial_domain_var_offset(qp, type);
695 default:
696 return 0;
700 /* Return the polynomial expression of "qp".
702 static __isl_keep isl_poly *isl_qpolynomial_peek_poly(
703 __isl_keep isl_qpolynomial *qp)
705 return qp ? qp->poly : NULL;
708 isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
710 return isl_poly_is_zero(isl_qpolynomial_peek_poly(qp));
713 isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
715 return isl_poly_is_one(isl_qpolynomial_peek_poly(qp));
718 isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
720 return isl_poly_is_nan(isl_qpolynomial_peek_poly(qp));
723 isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
725 return isl_poly_is_infty(isl_qpolynomial_peek_poly(qp));
728 isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
730 return isl_poly_is_neginfty(isl_qpolynomial_peek_poly(qp));
733 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
735 return isl_poly_sgn(isl_qpolynomial_peek_poly(qp));
738 static void poly_free_cst(__isl_take isl_poly_cst *cst)
740 isl_int_clear(cst->n);
741 isl_int_clear(cst->d);
744 static void poly_free_rec(__isl_take isl_poly_rec *rec)
746 int i;
748 for (i = 0; i < rec->n; ++i)
749 isl_poly_free(rec->p[i]);
752 __isl_give isl_poly *isl_poly_copy(__isl_keep isl_poly *poly)
754 if (!poly)
755 return NULL;
757 poly->ref++;
758 return poly;
761 __isl_give isl_poly *isl_poly_dup_cst(__isl_keep isl_poly *poly)
763 isl_poly_cst *cst;
764 isl_poly_cst *dup;
766 cst = isl_poly_as_cst(poly);
767 if (!cst)
768 return NULL;
770 dup = isl_poly_as_cst(isl_poly_zero(poly->ctx));
771 if (!dup)
772 return NULL;
773 isl_int_set(dup->n, cst->n);
774 isl_int_set(dup->d, cst->d);
776 return &dup->poly;
779 __isl_give isl_poly *isl_poly_dup_rec(__isl_keep isl_poly *poly)
781 int i;
782 isl_poly_rec *rec;
783 isl_poly_rec *dup;
785 rec = isl_poly_as_rec(poly);
786 if (!rec)
787 return NULL;
789 dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
790 if (!dup)
791 return NULL;
793 for (i = 0; i < rec->n; ++i) {
794 dup->p[i] = isl_poly_copy(rec->p[i]);
795 if (!dup->p[i])
796 goto error;
797 dup->n++;
800 return &dup->poly;
801 error:
802 isl_poly_free(&dup->poly);
803 return NULL;
806 __isl_give isl_poly *isl_poly_dup(__isl_keep isl_poly *poly)
808 isl_bool is_cst;
810 is_cst = isl_poly_is_cst(poly);
811 if (is_cst < 0)
812 return NULL;
813 if (is_cst)
814 return isl_poly_dup_cst(poly);
815 else
816 return isl_poly_dup_rec(poly);
819 __isl_give isl_poly *isl_poly_cow(__isl_take isl_poly *poly)
821 if (!poly)
822 return NULL;
824 if (poly->ref == 1)
825 return poly;
826 poly->ref--;
827 return isl_poly_dup(poly);
830 __isl_null isl_poly *isl_poly_free(__isl_take isl_poly *poly)
832 if (!poly)
833 return NULL;
835 if (--poly->ref > 0)
836 return NULL;
838 if (poly->var < 0)
839 poly_free_cst((isl_poly_cst *) poly);
840 else
841 poly_free_rec((isl_poly_rec *) poly);
843 isl_ctx_deref(poly->ctx);
844 free(poly);
845 return NULL;
848 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
850 isl_int gcd;
852 isl_int_init(gcd);
853 isl_int_gcd(gcd, cst->n, cst->d);
854 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
855 isl_int_divexact(cst->n, cst->n, gcd);
856 isl_int_divexact(cst->d, cst->d, gcd);
858 isl_int_clear(gcd);
861 __isl_give isl_poly *isl_poly_sum_cst(__isl_take isl_poly *poly1,
862 __isl_take isl_poly *poly2)
864 isl_poly_cst *cst1;
865 isl_poly_cst *cst2;
867 poly1 = isl_poly_cow(poly1);
868 if (!poly1 || !poly2)
869 goto error;
871 cst1 = isl_poly_as_cst(poly1);
872 cst2 = isl_poly_as_cst(poly2);
874 if (isl_int_eq(cst1->d, cst2->d))
875 isl_int_add(cst1->n, cst1->n, cst2->n);
876 else {
877 isl_int_mul(cst1->n, cst1->n, cst2->d);
878 isl_int_addmul(cst1->n, cst2->n, cst1->d);
879 isl_int_mul(cst1->d, cst1->d, cst2->d);
882 isl_poly_cst_reduce(cst1);
884 isl_poly_free(poly2);
885 return poly1;
886 error:
887 isl_poly_free(poly1);
888 isl_poly_free(poly2);
889 return NULL;
892 static __isl_give isl_poly *replace_by_zero(__isl_take isl_poly *poly)
894 struct isl_ctx *ctx;
896 if (!poly)
897 return NULL;
898 ctx = poly->ctx;
899 isl_poly_free(poly);
900 return isl_poly_zero(ctx);
903 static __isl_give isl_poly *replace_by_constant_term(__isl_take isl_poly *poly)
905 isl_poly_rec *rec;
906 isl_poly *cst;
908 if (!poly)
909 return NULL;
911 rec = isl_poly_as_rec(poly);
912 if (!rec)
913 goto error;
914 cst = isl_poly_copy(rec->p[0]);
915 isl_poly_free(poly);
916 return cst;
917 error:
918 isl_poly_free(poly);
919 return NULL;
922 __isl_give isl_poly *isl_poly_sum(__isl_take isl_poly *poly1,
923 __isl_take isl_poly *poly2)
925 int i;
926 isl_bool is_zero, is_nan, is_cst;
927 isl_poly_rec *rec1, *rec2;
929 if (!poly1 || !poly2)
930 goto error;
932 is_nan = isl_poly_is_nan(poly1);
933 if (is_nan < 0)
934 goto error;
935 if (is_nan) {
936 isl_poly_free(poly2);
937 return poly1;
940 is_nan = isl_poly_is_nan(poly2);
941 if (is_nan < 0)
942 goto error;
943 if (is_nan) {
944 isl_poly_free(poly1);
945 return poly2;
948 is_zero = isl_poly_is_zero(poly1);
949 if (is_zero < 0)
950 goto error;
951 if (is_zero) {
952 isl_poly_free(poly1);
953 return poly2;
956 is_zero = isl_poly_is_zero(poly2);
957 if (is_zero < 0)
958 goto error;
959 if (is_zero) {
960 isl_poly_free(poly2);
961 return poly1;
964 if (poly1->var < poly2->var)
965 return isl_poly_sum(poly2, poly1);
967 if (poly2->var < poly1->var) {
968 isl_poly_rec *rec;
969 isl_bool is_infty;
971 is_infty = isl_poly_is_infty(poly2);
972 if (is_infty >= 0 && !is_infty)
973 is_infty = isl_poly_is_neginfty(poly2);
974 if (is_infty < 0)
975 goto error;
976 if (is_infty) {
977 isl_poly_free(poly1);
978 return poly2;
980 poly1 = isl_poly_cow(poly1);
981 rec = isl_poly_as_rec(poly1);
982 if (!rec)
983 goto error;
984 rec->p[0] = isl_poly_sum(rec->p[0], poly2);
985 if (rec->n == 1)
986 poly1 = replace_by_constant_term(poly1);
987 return poly1;
990 is_cst = isl_poly_is_cst(poly1);
991 if (is_cst < 0)
992 goto error;
993 if (is_cst)
994 return isl_poly_sum_cst(poly1, poly2);
996 rec1 = isl_poly_as_rec(poly1);
997 rec2 = isl_poly_as_rec(poly2);
998 if (!rec1 || !rec2)
999 goto error;
1001 if (rec1->n < rec2->n)
1002 return isl_poly_sum(poly2, poly1);
1004 poly1 = isl_poly_cow(poly1);
1005 rec1 = isl_poly_as_rec(poly1);
1006 if (!rec1)
1007 goto error;
1009 for (i = rec2->n - 1; i >= 0; --i) {
1010 isl_bool is_zero;
1012 rec1->p[i] = isl_poly_sum(rec1->p[i],
1013 isl_poly_copy(rec2->p[i]));
1014 if (!rec1->p[i])
1015 goto error;
1016 if (i != rec1->n - 1)
1017 continue;
1018 is_zero = isl_poly_is_zero(rec1->p[i]);
1019 if (is_zero < 0)
1020 goto error;
1021 if (is_zero) {
1022 isl_poly_free(rec1->p[i]);
1023 rec1->n--;
1027 if (rec1->n == 0)
1028 poly1 = replace_by_zero(poly1);
1029 else if (rec1->n == 1)
1030 poly1 = replace_by_constant_term(poly1);
1032 isl_poly_free(poly2);
1034 return poly1;
1035 error:
1036 isl_poly_free(poly1);
1037 isl_poly_free(poly2);
1038 return NULL;
1041 __isl_give isl_poly *isl_poly_cst_add_isl_int(__isl_take isl_poly *poly,
1042 isl_int v)
1044 isl_poly_cst *cst;
1046 poly = isl_poly_cow(poly);
1047 if (!poly)
1048 return NULL;
1050 cst = isl_poly_as_cst(poly);
1052 isl_int_addmul(cst->n, cst->d, v);
1054 return poly;
1057 __isl_give isl_poly *isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
1059 isl_bool is_cst;
1060 isl_poly_rec *rec;
1062 is_cst = isl_poly_is_cst(poly);
1063 if (is_cst < 0)
1064 return isl_poly_free(poly);
1065 if (is_cst)
1066 return isl_poly_cst_add_isl_int(poly, v);
1068 poly = isl_poly_cow(poly);
1069 rec = isl_poly_as_rec(poly);
1070 if (!rec)
1071 goto error;
1073 rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
1074 if (!rec->p[0])
1075 goto error;
1077 return poly;
1078 error:
1079 isl_poly_free(poly);
1080 return NULL;
1083 __isl_give isl_poly *isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly,
1084 isl_int v)
1086 isl_bool is_zero;
1087 isl_poly_cst *cst;
1089 is_zero = isl_poly_is_zero(poly);
1090 if (is_zero < 0)
1091 return isl_poly_free(poly);
1092 if (is_zero)
1093 return poly;
1095 poly = isl_poly_cow(poly);
1096 if (!poly)
1097 return NULL;
1099 cst = isl_poly_as_cst(poly);
1101 isl_int_mul(cst->n, cst->n, v);
1103 return poly;
1106 __isl_give isl_poly *isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
1108 int i;
1109 isl_bool is_cst;
1110 isl_poly_rec *rec;
1112 is_cst = isl_poly_is_cst(poly);
1113 if (is_cst < 0)
1114 return isl_poly_free(poly);
1115 if (is_cst)
1116 return isl_poly_cst_mul_isl_int(poly, v);
1118 poly = isl_poly_cow(poly);
1119 rec = isl_poly_as_rec(poly);
1120 if (!rec)
1121 goto error;
1123 for (i = 0; i < rec->n; ++i) {
1124 rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
1125 if (!rec->p[i])
1126 goto error;
1129 return poly;
1130 error:
1131 isl_poly_free(poly);
1132 return NULL;
1135 /* Multiply the constant polynomial "poly" by "v".
1137 static __isl_give isl_poly *isl_poly_cst_scale_val(__isl_take isl_poly *poly,
1138 __isl_keep isl_val *v)
1140 isl_bool is_zero;
1141 isl_poly_cst *cst;
1143 is_zero = isl_poly_is_zero(poly);
1144 if (is_zero < 0)
1145 return isl_poly_free(poly);
1146 if (is_zero)
1147 return poly;
1149 poly = isl_poly_cow(poly);
1150 if (!poly)
1151 return NULL;
1153 cst = isl_poly_as_cst(poly);
1155 isl_int_mul(cst->n, cst->n, v->n);
1156 isl_int_mul(cst->d, cst->d, v->d);
1157 isl_poly_cst_reduce(cst);
1159 return poly;
1162 /* Multiply the polynomial "poly" by "v".
1164 static __isl_give isl_poly *isl_poly_scale_val(__isl_take isl_poly *poly,
1165 __isl_keep isl_val *v)
1167 int i;
1168 isl_bool is_cst;
1169 isl_poly_rec *rec;
1171 is_cst = isl_poly_is_cst(poly);
1172 if (is_cst < 0)
1173 return isl_poly_free(poly);
1174 if (is_cst)
1175 return isl_poly_cst_scale_val(poly, v);
1177 poly = isl_poly_cow(poly);
1178 rec = isl_poly_as_rec(poly);
1179 if (!rec)
1180 goto error;
1182 for (i = 0; i < rec->n; ++i) {
1183 rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1184 if (!rec->p[i])
1185 goto error;
1188 return poly;
1189 error:
1190 isl_poly_free(poly);
1191 return NULL;
1194 __isl_give isl_poly *isl_poly_mul_cst(__isl_take isl_poly *poly1,
1195 __isl_take isl_poly *poly2)
1197 isl_poly_cst *cst1;
1198 isl_poly_cst *cst2;
1200 poly1 = isl_poly_cow(poly1);
1201 if (!poly1 || !poly2)
1202 goto error;
1204 cst1 = isl_poly_as_cst(poly1);
1205 cst2 = isl_poly_as_cst(poly2);
1207 isl_int_mul(cst1->n, cst1->n, cst2->n);
1208 isl_int_mul(cst1->d, cst1->d, cst2->d);
1210 isl_poly_cst_reduce(cst1);
1212 isl_poly_free(poly2);
1213 return poly1;
1214 error:
1215 isl_poly_free(poly1);
1216 isl_poly_free(poly2);
1217 return NULL;
1220 __isl_give isl_poly *isl_poly_mul_rec(__isl_take isl_poly *poly1,
1221 __isl_take isl_poly *poly2)
1223 isl_poly_rec *rec1;
1224 isl_poly_rec *rec2;
1225 isl_poly_rec *res = NULL;
1226 int i, j;
1227 int size;
1229 rec1 = isl_poly_as_rec(poly1);
1230 rec2 = isl_poly_as_rec(poly2);
1231 if (!rec1 || !rec2)
1232 goto error;
1233 size = rec1->n + rec2->n - 1;
1234 res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1235 if (!res)
1236 goto error;
1238 for (i = 0; i < rec1->n; ++i) {
1239 res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1240 isl_poly_copy(rec1->p[i]));
1241 if (!res->p[i])
1242 goto error;
1243 res->n++;
1245 for (; i < size; ++i) {
1246 res->p[i] = isl_poly_zero(poly1->ctx);
1247 if (!res->p[i])
1248 goto error;
1249 res->n++;
1251 for (i = 0; i < rec1->n; ++i) {
1252 for (j = 1; j < rec2->n; ++j) {
1253 isl_poly *poly;
1254 poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1255 isl_poly_copy(rec1->p[i]));
1256 res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1257 if (!res->p[i + j])
1258 goto error;
1262 isl_poly_free(poly1);
1263 isl_poly_free(poly2);
1265 return &res->poly;
1266 error:
1267 isl_poly_free(poly1);
1268 isl_poly_free(poly2);
1269 isl_poly_free(&res->poly);
1270 return NULL;
1273 __isl_give isl_poly *isl_poly_mul(__isl_take isl_poly *poly1,
1274 __isl_take isl_poly *poly2)
1276 isl_bool is_zero, is_nan, is_one, is_cst;
1278 if (!poly1 || !poly2)
1279 goto error;
1281 is_nan = isl_poly_is_nan(poly1);
1282 if (is_nan < 0)
1283 goto error;
1284 if (is_nan) {
1285 isl_poly_free(poly2);
1286 return poly1;
1289 is_nan = isl_poly_is_nan(poly2);
1290 if (is_nan < 0)
1291 goto error;
1292 if (is_nan) {
1293 isl_poly_free(poly1);
1294 return poly2;
1297 is_zero = isl_poly_is_zero(poly1);
1298 if (is_zero < 0)
1299 goto error;
1300 if (is_zero) {
1301 isl_poly_free(poly2);
1302 return poly1;
1305 is_zero = isl_poly_is_zero(poly2);
1306 if (is_zero < 0)
1307 goto error;
1308 if (is_zero) {
1309 isl_poly_free(poly1);
1310 return poly2;
1313 is_one = isl_poly_is_one(poly1);
1314 if (is_one < 0)
1315 goto error;
1316 if (is_one) {
1317 isl_poly_free(poly1);
1318 return poly2;
1321 is_one = isl_poly_is_one(poly2);
1322 if (is_one < 0)
1323 goto error;
1324 if (is_one) {
1325 isl_poly_free(poly2);
1326 return poly1;
1329 if (poly1->var < poly2->var)
1330 return isl_poly_mul(poly2, poly1);
1332 if (poly2->var < poly1->var) {
1333 int i;
1334 isl_poly_rec *rec;
1335 isl_bool is_infty;
1337 is_infty = isl_poly_is_infty(poly2);
1338 if (is_infty >= 0 && !is_infty)
1339 is_infty = isl_poly_is_neginfty(poly2);
1340 if (is_infty < 0)
1341 goto error;
1342 if (is_infty) {
1343 isl_ctx *ctx = poly1->ctx;
1344 isl_poly_free(poly1);
1345 isl_poly_free(poly2);
1346 return isl_poly_nan(ctx);
1348 poly1 = isl_poly_cow(poly1);
1349 rec = isl_poly_as_rec(poly1);
1350 if (!rec)
1351 goto error;
1353 for (i = 0; i < rec->n; ++i) {
1354 rec->p[i] = isl_poly_mul(rec->p[i],
1355 isl_poly_copy(poly2));
1356 if (!rec->p[i])
1357 goto error;
1359 isl_poly_free(poly2);
1360 return poly1;
1363 is_cst = isl_poly_is_cst(poly1);
1364 if (is_cst < 0)
1365 goto error;
1366 if (is_cst)
1367 return isl_poly_mul_cst(poly1, poly2);
1369 return isl_poly_mul_rec(poly1, poly2);
1370 error:
1371 isl_poly_free(poly1);
1372 isl_poly_free(poly2);
1373 return NULL;
1376 __isl_give isl_poly *isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
1378 isl_poly *res;
1380 if (!poly)
1381 return NULL;
1382 if (power == 1)
1383 return poly;
1385 if (power % 2)
1386 res = isl_poly_copy(poly);
1387 else
1388 res = isl_poly_one(poly->ctx);
1390 while (power >>= 1) {
1391 poly = isl_poly_mul(poly, isl_poly_copy(poly));
1392 if (power % 2)
1393 res = isl_poly_mul(res, isl_poly_copy(poly));
1396 isl_poly_free(poly);
1397 return res;
1400 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *space,
1401 unsigned n_div, __isl_take isl_poly *poly)
1403 struct isl_qpolynomial *qp = NULL;
1404 isl_size total;
1406 total = isl_space_dim(space, isl_dim_all);
1407 if (total < 0 || !poly)
1408 goto error;
1410 if (!isl_space_is_set(space))
1411 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1412 "domain of polynomial should be a set", goto error);
1414 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1415 if (!qp)
1416 goto error;
1418 qp->ref = 1;
1419 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1420 if (!qp->div)
1421 goto error;
1423 qp->dim = space;
1424 qp->poly = poly;
1426 return qp;
1427 error:
1428 isl_space_free(space);
1429 isl_poly_free(poly);
1430 isl_qpolynomial_free(qp);
1431 return NULL;
1434 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1436 if (!qp)
1437 return NULL;
1439 qp->ref++;
1440 return qp;
1443 /* Return a copy of the polynomial expression of "qp".
1445 __isl_give isl_poly *isl_qpolynomial_get_poly(__isl_keep isl_qpolynomial *qp)
1447 return isl_poly_copy(isl_qpolynomial_peek_poly(qp));
1450 /* Return the polynomial expression of "qp".
1451 * This may be either a copy or the polynomial expression itself
1452 * if there is only one reference to "qp".
1453 * This allows the polynomial expression to be modified inplace
1454 * if both the quasi-polynomial and its polynomial expression
1455 * have only a single reference.
1456 * The caller is not allowed to modify "qp" between this call and
1457 * a subsequent call to isl_qpolynomial_restore_poly.
1458 * The only exception is that isl_qpolynomial_free can be called instead.
1460 static __isl_give isl_poly *isl_qpolynomial_take_poly(
1461 __isl_keep isl_qpolynomial *qp)
1463 isl_poly *poly;
1465 if (!qp)
1466 return NULL;
1467 if (qp->ref != 1)
1468 return isl_qpolynomial_get_poly(qp);
1469 poly = qp->poly;
1470 qp->poly = NULL;
1471 return poly;
1474 /* Set the polynomial expression of "qp" to "space",
1475 * where the polynomial expression of "qp" may be missing
1476 * due to a preceding call to isl_qpolynomial_take_poly.
1477 * However, in this case, "qp" only has a single reference and
1478 * then the call to isl_qpolynomial_cow has no effect.
1480 static __isl_give isl_qpolynomial *isl_qpolynomial_restore_poly(
1481 __isl_keep isl_qpolynomial *qp, __isl_take isl_poly *poly)
1483 if (!qp || !poly)
1484 goto error;
1486 if (qp->poly == poly) {
1487 isl_poly_free(poly);
1488 return qp;
1491 qp = isl_qpolynomial_cow(qp);
1492 if (!qp)
1493 goto error;
1494 isl_poly_free(qp->poly);
1495 qp->poly = poly;
1497 return qp;
1498 error:
1499 isl_qpolynomial_free(qp);
1500 isl_poly_free(poly);
1501 return NULL;
1504 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1506 isl_poly *poly;
1507 struct isl_qpolynomial *dup;
1509 if (!qp)
1510 return NULL;
1512 poly = isl_qpolynomial_get_poly(qp);
1513 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1514 poly);
1515 if (!dup)
1516 return NULL;
1517 isl_mat_free(dup->div);
1518 dup->div = isl_qpolynomial_get_local(qp);
1519 if (!dup->div)
1520 goto error;
1522 return dup;
1523 error:
1524 isl_qpolynomial_free(dup);
1525 return NULL;
1528 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1530 if (!qp)
1531 return NULL;
1533 if (qp->ref == 1)
1534 return qp;
1535 qp->ref--;
1536 return isl_qpolynomial_dup(qp);
1539 __isl_null isl_qpolynomial *isl_qpolynomial_free(
1540 __isl_take isl_qpolynomial *qp)
1542 if (!qp)
1543 return NULL;
1545 if (--qp->ref > 0)
1546 return NULL;
1548 isl_space_free(qp->dim);
1549 isl_mat_free(qp->div);
1550 isl_poly_free(qp->poly);
1552 free(qp);
1553 return NULL;
1556 __isl_give isl_poly *isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
1558 int i;
1559 isl_poly_rec *rec;
1560 isl_poly_cst *cst;
1562 rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1563 if (!rec)
1564 return NULL;
1565 for (i = 0; i < 1 + power; ++i) {
1566 rec->p[i] = isl_poly_zero(ctx);
1567 if (!rec->p[i])
1568 goto error;
1569 rec->n++;
1571 cst = isl_poly_as_cst(rec->p[power]);
1572 isl_int_set_si(cst->n, 1);
1574 return &rec->poly;
1575 error:
1576 isl_poly_free(&rec->poly);
1577 return NULL;
1580 /* r array maps original positions to new positions.
1582 static __isl_give isl_poly *reorder(__isl_take isl_poly *poly, int *r)
1584 int i;
1585 isl_bool is_cst;
1586 isl_poly_rec *rec;
1587 isl_poly *base;
1588 isl_poly *res;
1590 is_cst = isl_poly_is_cst(poly);
1591 if (is_cst < 0)
1592 return isl_poly_free(poly);
1593 if (is_cst)
1594 return poly;
1596 rec = isl_poly_as_rec(poly);
1597 if (!rec)
1598 goto error;
1600 isl_assert(poly->ctx, rec->n >= 1, goto error);
1602 base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1603 res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1605 for (i = rec->n - 2; i >= 0; --i) {
1606 res = isl_poly_mul(res, isl_poly_copy(base));
1607 res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1610 isl_poly_free(base);
1611 isl_poly_free(poly);
1613 return res;
1614 error:
1615 isl_poly_free(poly);
1616 return NULL;
1619 static isl_bool compatible_divs(__isl_keep isl_mat *div1,
1620 __isl_keep isl_mat *div2)
1622 int n_row, n_col;
1623 isl_bool equal;
1625 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1626 div1->n_col >= div2->n_col,
1627 return isl_bool_error);
1629 if (div1->n_row == div2->n_row)
1630 return isl_mat_is_equal(div1, div2);
1632 n_row = div1->n_row;
1633 n_col = div1->n_col;
1634 div1->n_row = div2->n_row;
1635 div1->n_col = div2->n_col;
1637 equal = isl_mat_is_equal(div1, div2);
1639 div1->n_row = n_row;
1640 div1->n_col = n_col;
1642 return equal;
1645 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1647 int li, lj;
1649 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1650 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1652 if (li != lj)
1653 return li - lj;
1655 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1658 struct isl_div_sort_info {
1659 isl_mat *div;
1660 int row;
1663 static int div_sort_cmp(const void *p1, const void *p2)
1665 const struct isl_div_sort_info *i1, *i2;
1666 i1 = (const struct isl_div_sort_info *) p1;
1667 i2 = (const struct isl_div_sort_info *) p2;
1669 return cmp_row(i1->div, i1->row, i2->row);
1672 /* Sort divs and remove duplicates.
1674 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1676 int i;
1677 int skip;
1678 int len;
1679 struct isl_div_sort_info *array = NULL;
1680 int *pos = NULL, *at = NULL;
1681 int *reordering = NULL;
1682 isl_size div_pos;
1684 if (!qp)
1685 return NULL;
1686 if (qp->div->n_row <= 1)
1687 return qp;
1689 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
1690 if (div_pos < 0)
1691 return isl_qpolynomial_free(qp);
1693 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1694 qp->div->n_row);
1695 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1696 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1697 len = qp->div->n_col - 2;
1698 reordering = isl_alloc_array(qp->div->ctx, int, len);
1699 if (!array || !pos || !at || !reordering)
1700 goto error;
1702 for (i = 0; i < qp->div->n_row; ++i) {
1703 array[i].div = qp->div;
1704 array[i].row = i;
1705 pos[i] = i;
1706 at[i] = i;
1709 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1710 div_sort_cmp);
1712 for (i = 0; i < div_pos; ++i)
1713 reordering[i] = i;
1715 for (i = 0; i < qp->div->n_row; ++i) {
1716 if (pos[array[i].row] == i)
1717 continue;
1718 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1719 pos[at[i]] = pos[array[i].row];
1720 at[pos[array[i].row]] = at[i];
1721 at[i] = array[i].row;
1722 pos[array[i].row] = i;
1725 skip = 0;
1726 for (i = 0; i < len - div_pos; ++i) {
1727 if (i > 0 &&
1728 isl_seq_eq(qp->div->row[i - skip - 1],
1729 qp->div->row[i - skip], qp->div->n_col)) {
1730 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1731 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1732 2 + div_pos + i - skip);
1733 qp->div = isl_mat_drop_cols(qp->div,
1734 2 + div_pos + i - skip, 1);
1735 skip++;
1737 reordering[div_pos + array[i].row] = div_pos + i - skip;
1740 qp->poly = reorder(qp->poly, reordering);
1742 if (!qp->poly || !qp->div)
1743 goto error;
1745 free(at);
1746 free(pos);
1747 free(array);
1748 free(reordering);
1750 return qp;
1751 error:
1752 free(at);
1753 free(pos);
1754 free(array);
1755 free(reordering);
1756 isl_qpolynomial_free(qp);
1757 return NULL;
1760 static __isl_give isl_poly *expand(__isl_take isl_poly *poly, int *exp,
1761 int first)
1763 int i;
1764 isl_bool is_cst;
1765 isl_poly_rec *rec;
1767 is_cst = isl_poly_is_cst(poly);
1768 if (is_cst < 0)
1769 return isl_poly_free(poly);
1770 if (is_cst)
1771 return poly;
1773 if (poly->var < first)
1774 return poly;
1776 if (exp[poly->var - first] == poly->var - first)
1777 return poly;
1779 poly = isl_poly_cow(poly);
1780 if (!poly)
1781 goto error;
1783 poly->var = exp[poly->var - first] + first;
1785 rec = isl_poly_as_rec(poly);
1786 if (!rec)
1787 goto error;
1789 for (i = 0; i < rec->n; ++i) {
1790 rec->p[i] = expand(rec->p[i], exp, first);
1791 if (!rec->p[i])
1792 goto error;
1795 return poly;
1796 error:
1797 isl_poly_free(poly);
1798 return NULL;
1801 static __isl_give isl_qpolynomial *with_merged_divs(
1802 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1803 __isl_take isl_qpolynomial *qp2),
1804 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1806 int *exp1 = NULL;
1807 int *exp2 = NULL;
1808 isl_mat *div = NULL;
1809 int n_div1, n_div2;
1811 qp1 = isl_qpolynomial_cow(qp1);
1812 qp2 = isl_qpolynomial_cow(qp2);
1814 if (!qp1 || !qp2)
1815 goto error;
1817 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1818 qp1->div->n_col >= qp2->div->n_col, goto error);
1820 n_div1 = qp1->div->n_row;
1821 n_div2 = qp2->div->n_row;
1822 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1823 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1824 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1825 goto error;
1827 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1828 if (!div)
1829 goto error;
1831 isl_mat_free(qp1->div);
1832 qp1->div = isl_mat_copy(div);
1833 isl_mat_free(qp2->div);
1834 qp2->div = isl_mat_copy(div);
1836 qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1837 qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1839 if (!qp1->poly || !qp2->poly)
1840 goto error;
1842 isl_mat_free(div);
1843 free(exp1);
1844 free(exp2);
1846 return fn(qp1, qp2);
1847 error:
1848 isl_mat_free(div);
1849 free(exp1);
1850 free(exp2);
1851 isl_qpolynomial_free(qp1);
1852 isl_qpolynomial_free(qp2);
1853 return NULL;
1856 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1857 __isl_take isl_qpolynomial *qp2)
1859 isl_bool compatible;
1860 isl_poly *poly;
1862 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1863 goto error;
1865 if (qp1->div->n_row < qp2->div->n_row)
1866 return isl_qpolynomial_add(qp2, qp1);
1868 compatible = compatible_divs(qp1->div, qp2->div);
1869 if (compatible < 0)
1870 goto error;
1871 if (!compatible)
1872 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1874 poly = isl_qpolynomial_take_poly(qp1);
1875 poly = isl_poly_sum(poly, isl_qpolynomial_get_poly(qp2));
1876 qp1 = isl_qpolynomial_restore_poly(qp1, poly);
1878 isl_qpolynomial_free(qp2);
1880 return qp1;
1881 error:
1882 isl_qpolynomial_free(qp1);
1883 isl_qpolynomial_free(qp2);
1884 return NULL;
1887 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1888 __isl_keep isl_set *dom,
1889 __isl_take isl_qpolynomial *qp1,
1890 __isl_take isl_qpolynomial *qp2)
1892 qp1 = isl_qpolynomial_add(qp1, qp2);
1893 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1894 return qp1;
1897 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1898 __isl_take isl_qpolynomial *qp2)
1900 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1903 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1904 __isl_take isl_qpolynomial *qp, isl_int v)
1906 isl_poly *poly;
1908 if (isl_int_is_zero(v))
1909 return qp;
1911 poly = isl_qpolynomial_take_poly(qp);
1912 poly = isl_poly_add_isl_int(poly, v);
1913 qp = isl_qpolynomial_restore_poly(qp, poly);
1915 return qp;
1918 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1920 if (!qp)
1921 return NULL;
1923 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1926 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1927 __isl_take isl_qpolynomial *qp, isl_int v)
1929 isl_poly *poly;
1931 if (isl_int_is_one(v))
1932 return qp;
1934 if (qp && isl_int_is_zero(v)) {
1935 isl_qpolynomial *zero;
1936 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1937 isl_qpolynomial_free(qp);
1938 return zero;
1941 poly = isl_qpolynomial_take_poly(qp);
1942 poly = isl_poly_mul_isl_int(poly, v);
1943 qp = isl_qpolynomial_restore_poly(qp, poly);
1945 return qp;
1948 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1949 __isl_take isl_qpolynomial *qp, isl_int v)
1951 return isl_qpolynomial_mul_isl_int(qp, v);
1954 /* Multiply "qp" by "v".
1956 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1957 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1959 isl_poly *poly;
1961 if (!qp || !v)
1962 goto error;
1964 if (!isl_val_is_rat(v))
1965 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1966 "expecting rational factor", goto error);
1968 if (isl_val_is_one(v)) {
1969 isl_val_free(v);
1970 return qp;
1973 if (isl_val_is_zero(v)) {
1974 isl_space *space;
1976 space = isl_qpolynomial_get_domain_space(qp);
1977 isl_qpolynomial_free(qp);
1978 isl_val_free(v);
1979 return isl_qpolynomial_zero_on_domain(space);
1982 poly = isl_qpolynomial_take_poly(qp);
1983 poly = isl_poly_scale_val(poly, v);
1984 qp = isl_qpolynomial_restore_poly(qp, poly);
1986 isl_val_free(v);
1987 return qp;
1988 error:
1989 isl_val_free(v);
1990 isl_qpolynomial_free(qp);
1991 return NULL;
1994 /* Divide "qp" by "v".
1996 __isl_give isl_qpolynomial *isl_qpolynomial_scale_down_val(
1997 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1999 if (!qp || !v)
2000 goto error;
2002 if (!isl_val_is_rat(v))
2003 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2004 "expecting rational factor", goto error);
2005 if (isl_val_is_zero(v))
2006 isl_die(isl_val_get_ctx(v), isl_error_invalid,
2007 "cannot scale down by zero", goto error);
2009 return isl_qpolynomial_scale_val(qp, isl_val_inv(v));
2010 error:
2011 isl_val_free(v);
2012 isl_qpolynomial_free(qp);
2013 return NULL;
2016 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
2017 __isl_take isl_qpolynomial *qp2)
2019 isl_bool compatible;
2020 isl_poly *poly;
2022 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
2023 goto error;
2025 if (qp1->div->n_row < qp2->div->n_row)
2026 return isl_qpolynomial_mul(qp2, qp1);
2028 compatible = compatible_divs(qp1->div, qp2->div);
2029 if (compatible < 0)
2030 goto error;
2031 if (!compatible)
2032 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
2034 poly = isl_qpolynomial_take_poly(qp1);
2035 poly = isl_poly_mul(poly, isl_qpolynomial_get_poly(qp2));
2036 qp1 = isl_qpolynomial_restore_poly(qp1, poly);
2038 isl_qpolynomial_free(qp2);
2040 return qp1;
2041 error:
2042 isl_qpolynomial_free(qp1);
2043 isl_qpolynomial_free(qp2);
2044 return NULL;
2047 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
2048 unsigned power)
2050 isl_poly *poly;
2052 poly = isl_qpolynomial_take_poly(qp);
2053 poly = isl_poly_pow(poly, power);
2054 qp = isl_qpolynomial_restore_poly(qp, poly);
2056 return qp;
2059 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
2060 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
2062 int i;
2064 if (power == 1)
2065 return pwqp;
2067 pwqp = isl_pw_qpolynomial_cow(pwqp);
2068 if (!pwqp)
2069 return NULL;
2071 for (i = 0; i < pwqp->n; ++i) {
2072 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
2073 if (!pwqp->p[i].qp)
2074 return isl_pw_qpolynomial_free(pwqp);
2077 return pwqp;
2080 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
2081 __isl_take isl_space *domain)
2083 if (!domain)
2084 return NULL;
2085 return isl_qpolynomial_alloc(domain, 0, isl_poly_zero(domain->ctx));
2088 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
2089 __isl_take isl_space *domain)
2091 if (!domain)
2092 return NULL;
2093 return isl_qpolynomial_alloc(domain, 0, isl_poly_one(domain->ctx));
2096 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
2097 __isl_take isl_space *domain)
2099 if (!domain)
2100 return NULL;
2101 return isl_qpolynomial_alloc(domain, 0, isl_poly_infty(domain->ctx));
2104 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
2105 __isl_take isl_space *domain)
2107 if (!domain)
2108 return NULL;
2109 return isl_qpolynomial_alloc(domain, 0, isl_poly_neginfty(domain->ctx));
2112 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
2113 __isl_take isl_space *domain)
2115 if (!domain)
2116 return NULL;
2117 return isl_qpolynomial_alloc(domain, 0, isl_poly_nan(domain->ctx));
2120 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
2121 __isl_take isl_space *domain,
2122 isl_int v)
2124 struct isl_qpolynomial *qp;
2125 isl_poly_cst *cst;
2127 qp = isl_qpolynomial_zero_on_domain(domain);
2128 if (!qp)
2129 return NULL;
2131 cst = isl_poly_as_cst(qp->poly);
2132 isl_int_set(cst->n, v);
2134 return qp;
2137 isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
2138 isl_int *n, isl_int *d)
2140 isl_bool is_cst;
2141 isl_poly *poly;
2142 isl_poly_cst *cst;
2144 poly = isl_qpolynomial_peek_poly(qp);
2145 is_cst = isl_poly_is_cst(poly);
2146 if (is_cst < 0 || !is_cst)
2147 return is_cst;
2149 cst = isl_poly_as_cst(poly);
2150 if (!cst)
2151 return isl_bool_error;
2153 if (n)
2154 isl_int_set(*n, cst->n);
2155 if (d)
2156 isl_int_set(*d, cst->d);
2158 return isl_bool_true;
2161 /* Return the constant term of "poly".
2163 static __isl_give isl_val *isl_poly_get_constant_val(__isl_keep isl_poly *poly)
2165 isl_bool is_cst;
2166 isl_poly_cst *cst;
2168 if (!poly)
2169 return NULL;
2171 while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
2172 isl_poly_rec *rec;
2174 rec = isl_poly_as_rec(poly);
2175 if (!rec)
2176 return NULL;
2177 poly = rec->p[0];
2179 if (is_cst < 0)
2180 return NULL;
2182 cst = isl_poly_as_cst(poly);
2183 if (!cst)
2184 return NULL;
2185 return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
2188 /* Return the constant term of "qp".
2190 __isl_give isl_val *isl_qpolynomial_get_constant_val(
2191 __isl_keep isl_qpolynomial *qp)
2193 return isl_poly_get_constant_val(isl_qpolynomial_peek_poly(qp));
2196 isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
2198 isl_bool is_cst;
2199 isl_poly_rec *rec;
2201 if (!poly)
2202 return isl_bool_error;
2204 if (poly->var < 0)
2205 return isl_bool_true;
2207 rec = isl_poly_as_rec(poly);
2208 if (!rec)
2209 return isl_bool_error;
2211 if (rec->n > 2)
2212 return isl_bool_false;
2214 isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
2216 is_cst = isl_poly_is_cst(rec->p[1]);
2217 if (is_cst < 0 || !is_cst)
2218 return is_cst;
2220 return isl_poly_is_affine(rec->p[0]);
2223 /* Can "qp" be converted to an isl_aff?
2224 * That is, does it represent a quasi-affine expression?
2226 isl_bool isl_qpolynomial_isa_aff(__isl_keep isl_qpolynomial *qp)
2228 return isl_poly_is_affine(isl_qpolynomial_peek_poly(qp));
2231 isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
2233 if (!qp)
2234 return isl_bool_error;
2236 if (qp->div->n_row > 0)
2237 return isl_bool_false;
2239 return isl_qpolynomial_isa_aff(qp);
2242 static void update_coeff(__isl_keep isl_vec *aff,
2243 __isl_keep isl_poly_cst *cst, int pos)
2245 isl_int gcd;
2246 isl_int f;
2248 if (isl_int_is_zero(cst->n))
2249 return;
2251 isl_int_init(gcd);
2252 isl_int_init(f);
2253 isl_int_gcd(gcd, cst->d, aff->el[0]);
2254 isl_int_divexact(f, cst->d, gcd);
2255 isl_int_divexact(gcd, aff->el[0], gcd);
2256 isl_seq_scale(aff->el, aff->el, f, aff->size);
2257 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
2258 isl_int_clear(gcd);
2259 isl_int_clear(f);
2262 int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
2264 isl_poly_cst *cst;
2265 isl_poly_rec *rec;
2267 if (!poly || !aff)
2268 return -1;
2270 if (poly->var < 0) {
2271 isl_poly_cst *cst;
2273 cst = isl_poly_as_cst(poly);
2274 if (!cst)
2275 return -1;
2276 update_coeff(aff, cst, 0);
2277 return 0;
2280 rec = isl_poly_as_rec(poly);
2281 if (!rec)
2282 return -1;
2283 isl_assert(poly->ctx, rec->n == 2, return -1);
2285 cst = isl_poly_as_cst(rec->p[1]);
2286 if (!cst)
2287 return -1;
2288 update_coeff(aff, cst, 1 + poly->var);
2290 return isl_poly_update_affine(rec->p[0], aff);
2293 __isl_give isl_vec *isl_qpolynomial_extract_affine(
2294 __isl_keep isl_qpolynomial *qp)
2296 isl_vec *aff;
2297 isl_size d;
2299 d = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2300 if (d < 0)
2301 return NULL;
2303 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
2304 if (!aff)
2305 return NULL;
2307 isl_seq_clr(aff->el + 1, 1 + d);
2308 isl_int_set_si(aff->el[0], 1);
2310 if (isl_poly_update_affine(qp->poly, aff) < 0)
2311 goto error;
2313 return aff;
2314 error:
2315 isl_vec_free(aff);
2316 return NULL;
2319 /* Compare two quasi-polynomials.
2321 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2322 * than "qp2" and 0 if they are equal.
2324 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1,
2325 __isl_keep isl_qpolynomial *qp2)
2327 int cmp;
2329 if (qp1 == qp2)
2330 return 0;
2331 if (!qp1)
2332 return -1;
2333 if (!qp2)
2334 return 1;
2336 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2337 if (cmp != 0)
2338 return cmp;
2340 cmp = isl_local_cmp(qp1->div, qp2->div);
2341 if (cmp != 0)
2342 return cmp;
2344 return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2347 /* Is "qp1" obviously equal to "qp2"?
2349 * NaN is not equal to anything, not even to another NaN.
2351 isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
2352 __isl_keep isl_qpolynomial *qp2)
2354 isl_bool equal;
2356 if (!qp1 || !qp2)
2357 return isl_bool_error;
2359 if (isl_qpolynomial_is_nan(qp1) || isl_qpolynomial_is_nan(qp2))
2360 return isl_bool_false;
2362 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2363 if (equal < 0 || !equal)
2364 return equal;
2366 equal = isl_mat_is_equal(qp1->div, qp2->div);
2367 if (equal < 0 || !equal)
2368 return equal;
2370 return isl_poly_is_equal(qp1->poly, qp2->poly);
2373 static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
2375 int i;
2376 isl_bool is_cst;
2377 isl_poly_rec *rec;
2379 is_cst = isl_poly_is_cst(poly);
2380 if (is_cst < 0)
2381 return isl_stat_error;
2382 if (is_cst) {
2383 isl_poly_cst *cst;
2384 cst = isl_poly_as_cst(poly);
2385 if (!cst)
2386 return isl_stat_error;
2387 isl_int_lcm(*d, *d, cst->d);
2388 return isl_stat_ok;
2391 rec = isl_poly_as_rec(poly);
2392 if (!rec)
2393 return isl_stat_error;
2395 for (i = 0; i < rec->n; ++i)
2396 poly_update_den(rec->p[i], d);
2398 return isl_stat_ok;
2401 __isl_give isl_val *isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
2403 isl_val *d;
2405 if (!qp)
2406 return NULL;
2407 d = isl_val_one(isl_qpolynomial_get_ctx(qp));
2408 if (!d)
2409 return NULL;
2410 if (poly_update_den(qp->poly, &d->n) < 0)
2411 return isl_val_free(d);
2412 return d;
2415 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
2416 __isl_take isl_space *domain, int pos, int power)
2418 struct isl_ctx *ctx;
2420 if (!domain)
2421 return NULL;
2423 ctx = domain->ctx;
2425 return isl_qpolynomial_alloc(domain, 0,
2426 isl_poly_var_pow(ctx, pos, power));
2429 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(
2430 __isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
2432 isl_size off;
2434 if (isl_space_check_is_set(domain ) < 0)
2435 goto error;
2436 if (isl_space_check_range(domain, type, pos, 1) < 0)
2437 goto error;
2439 off = isl_space_offset(domain, type);
2440 if (off < 0)
2441 goto error;
2443 return isl_qpolynomial_var_pow_on_domain(domain, off + pos, 1);
2444 error:
2445 isl_space_free(domain);
2446 return NULL;
2449 __isl_give isl_poly *isl_poly_subs(__isl_take isl_poly *poly,
2450 unsigned first, unsigned n, __isl_keep isl_poly **subs)
2452 int i;
2453 isl_bool is_cst;
2454 isl_poly_rec *rec;
2455 isl_poly *base, *res;
2457 is_cst = isl_poly_is_cst(poly);
2458 if (is_cst < 0)
2459 return isl_poly_free(poly);
2460 if (is_cst)
2461 return poly;
2463 if (poly->var < first)
2464 return poly;
2466 rec = isl_poly_as_rec(poly);
2467 if (!rec)
2468 goto error;
2470 isl_assert(poly->ctx, rec->n >= 1, goto error);
2472 if (poly->var >= first + n)
2473 base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2474 else
2475 base = isl_poly_copy(subs[poly->var - first]);
2477 res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2478 for (i = rec->n - 2; i >= 0; --i) {
2479 isl_poly *t;
2480 t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2481 res = isl_poly_mul(res, isl_poly_copy(base));
2482 res = isl_poly_sum(res, t);
2485 isl_poly_free(base);
2486 isl_poly_free(poly);
2488 return res;
2489 error:
2490 isl_poly_free(poly);
2491 return NULL;
2494 __isl_give isl_poly *isl_poly_from_affine(isl_ctx *ctx, isl_int *f,
2495 isl_int denom, unsigned len)
2497 int i;
2498 isl_poly *poly;
2500 isl_assert(ctx, len >= 1, return NULL);
2502 poly = isl_poly_rat_cst(ctx, f[0], denom);
2503 for (i = 0; i < len - 1; ++i) {
2504 isl_poly *t;
2505 isl_poly *c;
2507 if (isl_int_is_zero(f[1 + i]))
2508 continue;
2510 c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2511 t = isl_poly_var_pow(ctx, i, 1);
2512 t = isl_poly_mul(c, t);
2513 poly = isl_poly_sum(poly, t);
2516 return poly;
2519 /* Remove common factor of non-constant terms and denominator.
2521 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2523 isl_ctx *ctx = qp->div->ctx;
2524 unsigned total = qp->div->n_col - 2;
2526 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2527 isl_int_gcd(ctx->normalize_gcd,
2528 ctx->normalize_gcd, qp->div->row[div][0]);
2529 if (isl_int_is_one(ctx->normalize_gcd))
2530 return;
2532 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2533 ctx->normalize_gcd, total);
2534 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2535 ctx->normalize_gcd);
2536 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2537 ctx->normalize_gcd);
2540 /* Replace the integer division identified by "div" by the polynomial "s".
2541 * The integer division is assumed not to appear in the definition
2542 * of any other integer divisions.
2544 static __isl_give isl_qpolynomial *substitute_div(
2545 __isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
2547 int i;
2548 isl_size div_pos;
2549 int *reordering;
2550 isl_ctx *ctx;
2552 if (!qp || !s)
2553 goto error;
2555 qp = isl_qpolynomial_cow(qp);
2556 if (!qp)
2557 goto error;
2559 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2560 if (div_pos < 0)
2561 goto error;
2562 qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
2563 if (!qp->poly)
2564 goto error;
2566 ctx = isl_qpolynomial_get_ctx(qp);
2567 reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row);
2568 if (!reordering)
2569 goto error;
2570 for (i = 0; i < div_pos + div; ++i)
2571 reordering[i] = i;
2572 for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
2573 reordering[i] = i - 1;
2574 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2575 qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
2576 qp->poly = reorder(qp->poly, reordering);
2577 free(reordering);
2579 if (!qp->poly || !qp->div)
2580 goto error;
2582 isl_poly_free(s);
2583 return qp;
2584 error:
2585 isl_qpolynomial_free(qp);
2586 isl_poly_free(s);
2587 return NULL;
2590 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2591 * divisions because d is equal to 1 by their definition, i.e., e.
2593 static __isl_give isl_qpolynomial *substitute_non_divs(
2594 __isl_take isl_qpolynomial *qp)
2596 int i, j;
2597 isl_size div_pos;
2598 isl_poly *s;
2600 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2601 if (div_pos < 0)
2602 return isl_qpolynomial_free(qp);
2604 for (i = 0; qp && i < qp->div->n_row; ++i) {
2605 if (!isl_int_is_one(qp->div->row[i][0]))
2606 continue;
2607 for (j = i + 1; j < qp->div->n_row; ++j) {
2608 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
2609 continue;
2610 isl_seq_combine(qp->div->row[j] + 1,
2611 qp->div->ctx->one, qp->div->row[j] + 1,
2612 qp->div->row[j][2 + div_pos + i],
2613 qp->div->row[i] + 1, 1 + div_pos + i);
2614 isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0);
2615 normalize_div(qp, j);
2617 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2618 qp->div->row[i][0], qp->div->n_col - 1);
2619 qp = substitute_div(qp, i, s);
2620 --i;
2623 return qp;
2626 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2627 * with d the denominator. When replacing the coefficient e of x by
2628 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2629 * inside the division, so we need to add floor(e/d) * x outside.
2630 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2631 * to adjust the coefficient of x in each later div that depends on the
2632 * current div "div" and also in the affine expressions in the rows of "mat"
2633 * (if they too depend on "div").
2635 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2636 __isl_keep isl_mat **mat)
2638 int i, j;
2639 isl_int v;
2640 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2642 isl_int_init(v);
2643 for (i = 0; i < 1 + total + div; ++i) {
2644 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2645 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2646 continue;
2647 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2648 isl_int_fdiv_r(qp->div->row[div][1 + i],
2649 qp->div->row[div][1 + i], qp->div->row[div][0]);
2650 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2651 for (j = div + 1; j < qp->div->n_row; ++j) {
2652 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2653 continue;
2654 isl_int_addmul(qp->div->row[j][1 + i],
2655 v, qp->div->row[j][2 + total + div]);
2658 isl_int_clear(v);
2661 /* Check if the last non-zero coefficient is bigger that half of the
2662 * denominator. If so, we will invert the div to further reduce the number
2663 * of distinct divs that may appear.
2664 * If the last non-zero coefficient is exactly half the denominator,
2665 * then we continue looking for earlier coefficients that are bigger
2666 * than half the denominator.
2668 static int needs_invert(__isl_keep isl_mat *div, int row)
2670 int i;
2671 int cmp;
2673 for (i = div->n_col - 1; i >= 1; --i) {
2674 if (isl_int_is_zero(div->row[row][i]))
2675 continue;
2676 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2677 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2678 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2679 if (cmp)
2680 return cmp > 0;
2681 if (i == 1)
2682 return 1;
2685 return 0;
2688 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2689 * We only invert the coefficients of e (and the coefficient of q in
2690 * later divs and in the rows of "mat"). After calling this function, the
2691 * coefficients of e should be reduced again.
2693 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2694 __isl_keep isl_mat **mat)
2696 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2698 isl_seq_neg(qp->div->row[div] + 1,
2699 qp->div->row[div] + 1, qp->div->n_col - 1);
2700 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2701 isl_int_add(qp->div->row[div][1],
2702 qp->div->row[div][1], qp->div->row[div][0]);
2703 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2704 isl_mat_col_mul(qp->div, 2 + total + div,
2705 qp->div->ctx->negone, 2 + total + div);
2708 /* Reduce all divs of "qp" to have coefficients
2709 * in the interval [0, d-1], with d the denominator and such that the
2710 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2711 * The modifications to the integer divisions need to be reflected
2712 * in the factors of the polynomial that refer to the original
2713 * integer divisions. To this end, the modifications are collected
2714 * as a set of affine expressions and then plugged into the polynomial.
2716 * After the reduction, some divs may have become redundant or identical,
2717 * so we call substitute_non_divs and sort_divs. If these functions
2718 * eliminate divs or merge two or more divs into one, the coefficients
2719 * of the enclosing divs may have to be reduced again, so we call
2720 * ourselves recursively if the number of divs decreases.
2722 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2724 int i;
2725 isl_ctx *ctx;
2726 isl_mat *mat;
2727 isl_poly **s;
2728 unsigned o_div;
2729 isl_size n_div, total, new_n_div;
2731 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
2732 n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2733 o_div = isl_qpolynomial_domain_offset(qp, isl_dim_div);
2734 if (total < 0 || n_div < 0)
2735 return isl_qpolynomial_free(qp);
2736 ctx = isl_qpolynomial_get_ctx(qp);
2737 mat = isl_mat_zero(ctx, n_div, 1 + total);
2739 for (i = 0; i < n_div; ++i)
2740 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2742 for (i = 0; i < qp->div->n_row; ++i) {
2743 normalize_div(qp, i);
2744 reduce_div(qp, i, &mat);
2745 if (needs_invert(qp->div, i)) {
2746 invert_div(qp, i, &mat);
2747 reduce_div(qp, i, &mat);
2750 if (!mat)
2751 goto error;
2753 s = isl_alloc_array(ctx, struct isl_poly *, n_div);
2754 if (n_div && !s)
2755 goto error;
2756 for (i = 0; i < n_div; ++i)
2757 s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2758 1 + total);
2759 qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2760 for (i = 0; i < n_div; ++i)
2761 isl_poly_free(s[i]);
2762 free(s);
2763 if (!qp->poly)
2764 goto error;
2766 isl_mat_free(mat);
2768 qp = substitute_non_divs(qp);
2769 qp = sort_divs(qp);
2770 new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2771 if (new_n_div < 0)
2772 return isl_qpolynomial_free(qp);
2773 if (new_n_div < n_div)
2774 return reduce_divs(qp);
2776 return qp;
2777 error:
2778 isl_qpolynomial_free(qp);
2779 isl_mat_free(mat);
2780 return NULL;
2783 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2784 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2786 struct isl_qpolynomial *qp;
2787 isl_poly_cst *cst;
2789 qp = isl_qpolynomial_zero_on_domain(domain);
2790 if (!qp)
2791 return NULL;
2793 cst = isl_poly_as_cst(qp->poly);
2794 isl_int_set(cst->n, n);
2795 isl_int_set(cst->d, d);
2797 return qp;
2800 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2802 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2803 __isl_take isl_space *domain, __isl_take isl_val *val)
2805 isl_qpolynomial *qp;
2806 isl_poly_cst *cst;
2808 qp = isl_qpolynomial_zero_on_domain(domain);
2809 if (!qp || !val)
2810 goto error;
2812 cst = isl_poly_as_cst(qp->poly);
2813 isl_int_set(cst->n, val->n);
2814 isl_int_set(cst->d, val->d);
2816 isl_val_free(val);
2817 return qp;
2818 error:
2819 isl_val_free(val);
2820 isl_qpolynomial_free(qp);
2821 return NULL;
2824 static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
2826 isl_bool is_cst;
2827 isl_poly_rec *rec;
2828 int i;
2830 is_cst = isl_poly_is_cst(poly);
2831 if (is_cst < 0)
2832 return isl_stat_error;
2833 if (is_cst)
2834 return isl_stat_ok;
2836 if (poly->var < d)
2837 active[poly->var] = 1;
2839 rec = isl_poly_as_rec(poly);
2840 for (i = 0; i < rec->n; ++i)
2841 if (poly_set_active(rec->p[i], active, d) < 0)
2842 return isl_stat_error;
2844 return isl_stat_ok;
2847 static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
2849 int i, j;
2850 isl_size d;
2851 isl_space *space;
2853 space = isl_qpolynomial_peek_domain_space(qp);
2854 d = isl_space_dim(space, isl_dim_all);
2855 if (d < 0 || !active)
2856 return isl_stat_error;
2858 for (i = 0; i < d; ++i)
2859 for (j = 0; j < qp->div->n_row; ++j) {
2860 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2861 continue;
2862 active[i] = 1;
2863 break;
2866 return poly_set_active(isl_qpolynomial_peek_poly(qp), active, d);
2869 #undef TYPE
2870 #define TYPE isl_qpolynomial
2871 static
2872 #include "check_type_range_templ.c"
2874 isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2875 enum isl_dim_type type, unsigned first, unsigned n)
2877 int i;
2878 int *active = NULL;
2879 isl_bool involves = isl_bool_false;
2880 isl_size offset;
2881 isl_size d;
2882 isl_space *space;
2884 if (!qp)
2885 return isl_bool_error;
2886 if (n == 0)
2887 return isl_bool_false;
2889 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2890 return isl_bool_error;
2891 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2892 type == isl_dim_in, return isl_bool_error);
2894 space = isl_qpolynomial_peek_domain_space(qp);
2895 d = isl_space_dim(space, isl_dim_all);
2896 if (d < 0)
2897 return isl_bool_error;
2898 active = isl_calloc_array(qp->dim->ctx, int, d);
2899 if (set_active(qp, active) < 0)
2900 goto error;
2902 offset = isl_qpolynomial_domain_var_offset(qp, domain_type(type));
2903 if (offset < 0)
2904 goto error;
2905 first += offset;
2906 for (i = 0; i < n; ++i)
2907 if (active[first + i]) {
2908 involves = isl_bool_true;
2909 break;
2912 free(active);
2914 return involves;
2915 error:
2916 free(active);
2917 return isl_bool_error;
2920 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2921 * of the divs that do appear in the quasi-polynomial.
2923 static __isl_give isl_qpolynomial *remove_redundant_divs(
2924 __isl_take isl_qpolynomial *qp)
2926 int i, j;
2927 isl_size div_pos;
2928 int len;
2929 int skip;
2930 int *active = NULL;
2931 int *reordering = NULL;
2932 int redundant = 0;
2933 int n_div;
2934 isl_ctx *ctx;
2936 if (!qp)
2937 return NULL;
2938 if (qp->div->n_row == 0)
2939 return qp;
2941 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
2942 if (div_pos < 0)
2943 return isl_qpolynomial_free(qp);
2944 len = qp->div->n_col - 2;
2945 ctx = isl_qpolynomial_get_ctx(qp);
2946 active = isl_calloc_array(ctx, int, len);
2947 if (!active)
2948 goto error;
2950 if (poly_set_active(isl_qpolynomial_peek_poly(qp), active, len) < 0)
2951 goto error;
2953 for (i = qp->div->n_row - 1; i >= 0; --i) {
2954 if (!active[div_pos + i]) {
2955 redundant = 1;
2956 continue;
2958 for (j = 0; j < i; ++j) {
2959 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j]))
2960 continue;
2961 active[div_pos + j] = 1;
2962 break;
2966 if (!redundant) {
2967 free(active);
2968 return qp;
2971 reordering = isl_alloc_array(qp->div->ctx, int, len);
2972 if (!reordering)
2973 goto error;
2975 for (i = 0; i < div_pos; ++i)
2976 reordering[i] = i;
2978 skip = 0;
2979 n_div = qp->div->n_row;
2980 for (i = 0; i < n_div; ++i) {
2981 if (!active[div_pos + i]) {
2982 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2983 qp->div = isl_mat_drop_cols(qp->div,
2984 2 + div_pos + i - skip, 1);
2985 skip++;
2987 reordering[div_pos + i] = div_pos + i - skip;
2990 qp->poly = reorder(qp->poly, reordering);
2992 if (!qp->poly || !qp->div)
2993 goto error;
2995 free(active);
2996 free(reordering);
2998 return qp;
2999 error:
3000 free(active);
3001 free(reordering);
3002 isl_qpolynomial_free(qp);
3003 return NULL;
3006 __isl_give isl_poly *isl_poly_drop(__isl_take isl_poly *poly,
3007 unsigned first, unsigned n)
3009 int i;
3010 isl_poly_rec *rec;
3012 if (!poly)
3013 return NULL;
3014 if (n == 0 || poly->var < 0 || poly->var < first)
3015 return poly;
3016 if (poly->var < first + n) {
3017 poly = replace_by_constant_term(poly);
3018 return isl_poly_drop(poly, first, n);
3020 poly = isl_poly_cow(poly);
3021 if (!poly)
3022 return NULL;
3023 poly->var -= n;
3024 rec = isl_poly_as_rec(poly);
3025 if (!rec)
3026 goto error;
3028 for (i = 0; i < rec->n; ++i) {
3029 rec->p[i] = isl_poly_drop(rec->p[i], first, n);
3030 if (!rec->p[i])
3031 goto error;
3034 return poly;
3035 error:
3036 isl_poly_free(poly);
3037 return NULL;
3040 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
3041 __isl_take isl_qpolynomial *qp,
3042 enum isl_dim_type type, unsigned pos, const char *s)
3044 isl_space *space;
3046 if (!qp)
3047 return NULL;
3048 if (type == isl_dim_out)
3049 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
3050 "cannot set name of output/set dimension",
3051 return isl_qpolynomial_free(qp));
3052 type = domain_type(type);
3053 space = isl_qpolynomial_take_domain_space(qp);
3054 space = isl_space_set_dim_name(space, type, pos, s);
3055 qp = isl_qpolynomial_restore_domain_space(qp, space);
3056 return qp;
3059 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
3060 __isl_take isl_qpolynomial *qp,
3061 enum isl_dim_type type, unsigned first, unsigned n)
3063 isl_space *space;
3064 isl_size offset;
3066 if (!qp)
3067 return NULL;
3068 if (type == isl_dim_out)
3069 isl_die(qp->dim->ctx, isl_error_invalid,
3070 "cannot drop output/set dimension",
3071 goto error);
3072 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3073 return isl_qpolynomial_free(qp);
3074 type = domain_type(type);
3075 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3076 return qp;
3079 isl_assert(qp->dim->ctx, type == isl_dim_param ||
3080 type == isl_dim_set, goto error);
3082 space = isl_qpolynomial_take_domain_space(qp);
3083 space = isl_space_drop_dims(space, type, first, n);
3084 qp = isl_qpolynomial_restore_domain_space(qp, space);
3086 qp = isl_qpolynomial_cow(qp);
3087 if (!qp)
3088 return NULL;
3090 offset = isl_qpolynomial_domain_var_offset(qp, type);
3091 if (offset < 0)
3092 goto error;
3093 first += offset;
3095 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
3096 if (!qp->div)
3097 goto error;
3099 qp->poly = isl_poly_drop(qp->poly, first, n);
3100 if (!qp->poly)
3101 goto error;
3103 return qp;
3104 error:
3105 isl_qpolynomial_free(qp);
3106 return NULL;
3109 /* Project the domain of the quasi-polynomial onto its parameter space.
3110 * The quasi-polynomial may not involve any of the domain dimensions.
3112 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
3113 __isl_take isl_qpolynomial *qp)
3115 isl_space *space;
3116 isl_size n;
3117 isl_bool involves;
3119 n = isl_qpolynomial_dim(qp, isl_dim_in);
3120 if (n < 0)
3121 return isl_qpolynomial_free(qp);
3122 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
3123 if (involves < 0)
3124 return isl_qpolynomial_free(qp);
3125 if (involves)
3126 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
3127 "polynomial involves some of the domain dimensions",
3128 return isl_qpolynomial_free(qp));
3129 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
3130 space = isl_qpolynomial_get_domain_space(qp);
3131 space = isl_space_params(space);
3132 qp = isl_qpolynomial_reset_domain_space(qp, space);
3133 return qp;
3136 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
3137 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
3139 int i, j, k;
3140 isl_int denom;
3141 unsigned total;
3142 unsigned n_div;
3143 isl_poly *poly;
3145 if (!eq)
3146 goto error;
3147 if (eq->n_eq == 0) {
3148 isl_basic_set_free(eq);
3149 return qp;
3152 qp = isl_qpolynomial_cow(qp);
3153 if (!qp)
3154 goto error;
3155 qp->div = isl_mat_cow(qp->div);
3156 if (!qp->div)
3157 goto error;
3159 total = isl_basic_set_offset(eq, isl_dim_div);
3160 n_div = eq->n_div;
3161 isl_int_init(denom);
3162 for (i = 0; i < eq->n_eq; ++i) {
3163 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
3164 if (j < 0 || j == 0 || j >= total)
3165 continue;
3167 for (k = 0; k < qp->div->n_row; ++k) {
3168 if (isl_int_is_zero(qp->div->row[k][1 + j]))
3169 continue;
3170 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
3171 &qp->div->row[k][0]);
3172 normalize_div(qp, k);
3175 if (isl_int_is_pos(eq->eq[i][j]))
3176 isl_seq_neg(eq->eq[i], eq->eq[i], total);
3177 isl_int_abs(denom, eq->eq[i][j]);
3178 isl_int_set_si(eq->eq[i][j], 0);
3180 poly = isl_poly_from_affine(qp->dim->ctx,
3181 eq->eq[i], denom, total);
3182 qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
3183 isl_poly_free(poly);
3185 isl_int_clear(denom);
3187 if (!qp->poly)
3188 goto error;
3190 isl_basic_set_free(eq);
3192 qp = substitute_non_divs(qp);
3193 qp = sort_divs(qp);
3195 return qp;
3196 error:
3197 isl_basic_set_free(eq);
3198 isl_qpolynomial_free(qp);
3199 return NULL;
3202 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3204 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
3205 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
3207 if (!qp || !eq)
3208 goto error;
3209 if (qp->div->n_row > 0)
3210 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
3211 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
3212 error:
3213 isl_basic_set_free(eq);
3214 isl_qpolynomial_free(qp);
3215 return NULL;
3218 /* Look for equalities among the variables shared by context and qp
3219 * and the integer divisions of qp, if any.
3220 * The equalities are then used to eliminate variables and/or integer
3221 * divisions from qp.
3223 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
3224 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3226 isl_local_space *ls;
3227 isl_basic_set *aff;
3229 ls = isl_qpolynomial_get_domain_local_space(qp);
3230 context = isl_local_space_lift_set(ls, context);
3232 aff = isl_set_affine_hull(context);
3233 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
3236 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
3237 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
3239 isl_space *space = isl_qpolynomial_get_domain_space(qp);
3240 isl_set *dom_context = isl_set_universe(space);
3241 dom_context = isl_set_intersect_params(dom_context, context);
3242 return isl_qpolynomial_gist(qp, dom_context);
3245 /* Return a zero isl_qpolynomial in the given space.
3247 * This is a helper function for isl_pw_*_as_* that ensures a uniform
3248 * interface over all piecewise types.
3250 static __isl_give isl_qpolynomial *isl_qpolynomial_zero_in_space(
3251 __isl_take isl_space *space)
3253 return isl_qpolynomial_zero_on_domain(isl_space_domain(space));
3256 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3258 #undef PW
3259 #define PW isl_pw_qpolynomial
3260 #undef BASE
3261 #define BASE qpolynomial
3262 #undef EL_IS_ZERO
3263 #define EL_IS_ZERO is_zero
3264 #undef ZERO
3265 #define ZERO zero
3266 #undef IS_ZERO
3267 #define IS_ZERO is_zero
3268 #undef FIELD
3269 #define FIELD qp
3270 #undef DEFAULT_IS_ZERO
3271 #define DEFAULT_IS_ZERO 1
3273 #include <isl_pw_templ.c>
3274 #include <isl_pw_un_op_templ.c>
3275 #include <isl_pw_add_disjoint_templ.c>
3276 #include <isl_pw_domain_reverse_templ.c>
3277 #include <isl_pw_eval.c>
3278 #include <isl_pw_fix_templ.c>
3279 #include <isl_pw_from_range_templ.c>
3280 #include <isl_pw_insert_dims_templ.c>
3281 #include <isl_pw_lift_templ.c>
3282 #include <isl_pw_morph_templ.c>
3283 #include <isl_pw_move_dims_templ.c>
3284 #include <isl_pw_neg_templ.c>
3285 #include <isl_pw_opt_templ.c>
3286 #include <isl_pw_split_dims_templ.c>
3287 #include <isl_pw_sub_templ.c>
3289 #undef BASE
3290 #define BASE pw_qpolynomial
3292 #include <isl_union_single.c>
3293 #include <isl_union_domain_reverse_templ.c>
3294 #include <isl_union_eval.c>
3295 #include <isl_union_neg.c>
3296 #include <isl_union_sub_templ.c>
3298 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
3300 if (!pwqp)
3301 return -1;
3303 if (pwqp->n != -1)
3304 return 0;
3306 if (!isl_set_plain_is_universe(pwqp->p[0].set))
3307 return 0;
3309 return isl_qpolynomial_is_one(pwqp->p[0].qp);
3312 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
3313 __isl_take isl_pw_qpolynomial *pwqp1,
3314 __isl_take isl_pw_qpolynomial *pwqp2)
3316 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3319 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
3320 __isl_take isl_pw_qpolynomial *pwqp1,
3321 __isl_take isl_pw_qpolynomial *pwqp2)
3323 int i, j, n;
3324 struct isl_pw_qpolynomial *res;
3326 if (!pwqp1 || !pwqp2)
3327 goto error;
3329 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3330 goto error);
3332 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3333 isl_pw_qpolynomial_free(pwqp2);
3334 return pwqp1;
3337 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3338 isl_pw_qpolynomial_free(pwqp1);
3339 return pwqp2;
3342 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3343 isl_pw_qpolynomial_free(pwqp1);
3344 return pwqp2;
3347 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3348 isl_pw_qpolynomial_free(pwqp2);
3349 return pwqp1;
3352 n = pwqp1->n * pwqp2->n;
3353 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3355 for (i = 0; i < pwqp1->n; ++i) {
3356 for (j = 0; j < pwqp2->n; ++j) {
3357 struct isl_set *common;
3358 struct isl_qpolynomial *prod;
3359 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3360 isl_set_copy(pwqp2->p[j].set));
3361 if (isl_set_plain_is_empty(common)) {
3362 isl_set_free(common);
3363 continue;
3366 prod = isl_qpolynomial_mul(
3367 isl_qpolynomial_copy(pwqp1->p[i].qp),
3368 isl_qpolynomial_copy(pwqp2->p[j].qp));
3370 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3374 isl_pw_qpolynomial_free(pwqp1);
3375 isl_pw_qpolynomial_free(pwqp2);
3377 return res;
3378 error:
3379 isl_pw_qpolynomial_free(pwqp1);
3380 isl_pw_qpolynomial_free(pwqp2);
3381 return NULL;
3384 __isl_give isl_val *isl_poly_eval(__isl_take isl_poly *poly,
3385 __isl_take isl_vec *vec)
3387 int i;
3388 isl_bool is_cst;
3389 isl_poly_rec *rec;
3390 isl_val *res;
3391 isl_val *base;
3393 is_cst = isl_poly_is_cst(poly);
3394 if (is_cst < 0)
3395 goto error;
3396 if (is_cst) {
3397 isl_vec_free(vec);
3398 res = isl_poly_get_constant_val(poly);
3399 isl_poly_free(poly);
3400 return res;
3403 rec = isl_poly_as_rec(poly);
3404 if (!rec || !vec)
3405 goto error;
3407 isl_assert(poly->ctx, rec->n >= 1, goto error);
3409 base = isl_val_rat_from_isl_int(poly->ctx,
3410 vec->el[1 + poly->var], vec->el[0]);
3412 res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3413 isl_vec_copy(vec));
3415 for (i = rec->n - 2; i >= 0; --i) {
3416 res = isl_val_mul(res, isl_val_copy(base));
3417 res = isl_val_add(res, isl_poly_eval(isl_poly_copy(rec->p[i]),
3418 isl_vec_copy(vec)));
3421 isl_val_free(base);
3422 isl_poly_free(poly);
3423 isl_vec_free(vec);
3424 return res;
3425 error:
3426 isl_poly_free(poly);
3427 isl_vec_free(vec);
3428 return NULL;
3431 /* Evaluate "qp" in the void point "pnt".
3432 * In particular, return the value NaN.
3434 static __isl_give isl_val *eval_void(__isl_take isl_qpolynomial *qp,
3435 __isl_take isl_point *pnt)
3437 isl_ctx *ctx;
3439 ctx = isl_point_get_ctx(pnt);
3440 isl_qpolynomial_free(qp);
3441 isl_point_free(pnt);
3442 return isl_val_nan(ctx);
3445 __isl_give isl_val *isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp,
3446 __isl_take isl_point *pnt)
3448 isl_bool is_void;
3449 isl_vec *ext;
3450 isl_val *v;
3452 if (!qp || !pnt)
3453 goto error;
3454 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3455 is_void = isl_point_is_void(pnt);
3456 if (is_void < 0)
3457 goto error;
3458 if (is_void)
3459 return eval_void(qp, pnt);
3461 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3463 v = isl_poly_eval(isl_qpolynomial_get_poly(qp), ext);
3465 isl_qpolynomial_free(qp);
3466 isl_point_free(pnt);
3468 return v;
3469 error:
3470 isl_qpolynomial_free(qp);
3471 isl_point_free(pnt);
3472 return NULL;
3475 int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
3477 int cmp;
3478 isl_int t;
3479 isl_int_init(t);
3480 isl_int_mul(t, cst1->n, cst2->d);
3481 isl_int_submul(t, cst2->n, cst1->d);
3482 cmp = isl_int_sgn(t);
3483 isl_int_clear(t);
3484 return cmp;
3487 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3488 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3489 unsigned first, unsigned n)
3491 unsigned total;
3492 unsigned g_pos;
3493 int *exp;
3494 isl_space *space;
3496 if (!qp)
3497 return NULL;
3498 if (type == isl_dim_out)
3499 isl_die(qp->div->ctx, isl_error_invalid,
3500 "cannot insert output/set dimensions",
3501 goto error);
3502 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3503 return isl_qpolynomial_free(qp);
3504 type = domain_type(type);
3505 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3506 return qp;
3508 qp = isl_qpolynomial_cow(qp);
3509 if (!qp)
3510 return NULL;
3512 g_pos = pos(qp->dim, type) + first;
3514 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3515 if (!qp->div)
3516 goto error;
3518 total = qp->div->n_col - 2;
3519 if (total > g_pos) {
3520 int i;
3521 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3522 if (!exp)
3523 goto error;
3524 for (i = 0; i < total - g_pos; ++i)
3525 exp[i] = i + n;
3526 qp->poly = expand(qp->poly, exp, g_pos);
3527 free(exp);
3528 if (!qp->poly)
3529 goto error;
3532 space = isl_qpolynomial_take_domain_space(qp);
3533 space = isl_space_insert_dims(space, type, first, n);
3534 qp = isl_qpolynomial_restore_domain_space(qp, space);
3536 return qp;
3537 error:
3538 isl_qpolynomial_free(qp);
3539 return NULL;
3542 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3543 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3545 isl_size pos;
3547 pos = isl_qpolynomial_dim(qp, type);
3548 if (pos < 0)
3549 return isl_qpolynomial_free(qp);
3551 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3554 static int *reordering_move(isl_ctx *ctx,
3555 unsigned len, unsigned dst, unsigned src, unsigned n)
3557 int i;
3558 int *reordering;
3560 reordering = isl_alloc_array(ctx, int, len);
3561 if (!reordering)
3562 return NULL;
3564 if (dst <= src) {
3565 for (i = 0; i < dst; ++i)
3566 reordering[i] = i;
3567 for (i = 0; i < n; ++i)
3568 reordering[src + i] = dst + i;
3569 for (i = 0; i < src - dst; ++i)
3570 reordering[dst + i] = dst + n + i;
3571 for (i = 0; i < len - src - n; ++i)
3572 reordering[src + n + i] = src + n + i;
3573 } else {
3574 for (i = 0; i < src; ++i)
3575 reordering[i] = i;
3576 for (i = 0; i < n; ++i)
3577 reordering[src + i] = dst + i;
3578 for (i = 0; i < dst - src; ++i)
3579 reordering[src + n + i] = src + i;
3580 for (i = 0; i < len - dst - n; ++i)
3581 reordering[dst + n + i] = dst + n + i;
3584 return reordering;
3587 /* Move the "n" variables starting at "src_pos" of "qp" to "dst_pos".
3588 * Only modify the polynomial expression and the local variables of "qp".
3589 * The caller is responsible for modifying the space accordingly.
3591 static __isl_give isl_qpolynomial *local_poly_move_dims(
3592 __isl_take isl_qpolynomial *qp,
3593 unsigned dst_pos, unsigned src_pos, unsigned n)
3595 isl_ctx *ctx;
3596 isl_size total;
3597 int *reordering;
3598 isl_local *local;
3599 isl_poly *poly;
3601 local = isl_qpolynomial_take_local(qp);
3602 local = isl_local_move_vars(local, dst_pos, src_pos, n);
3603 qp = isl_qpolynomial_restore_local(qp, local);
3604 qp = sort_divs(qp);
3606 total = isl_qpolynomial_domain_dim(qp, isl_dim_all);
3607 if (total < 0)
3608 return isl_qpolynomial_free(qp);
3609 ctx = isl_qpolynomial_get_ctx(qp);
3610 reordering = reordering_move(ctx, total, dst_pos, src_pos, n);
3611 if (!reordering)
3612 return isl_qpolynomial_free(qp);
3614 poly = isl_qpolynomial_take_poly(qp);
3615 poly = reorder(poly, reordering);
3616 qp = isl_qpolynomial_restore_poly(qp, poly);
3617 free(reordering);
3619 return qp;
3622 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3623 __isl_take isl_qpolynomial *qp,
3624 enum isl_dim_type dst_type, unsigned dst_pos,
3625 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3627 isl_ctx *ctx;
3628 unsigned g_dst_pos;
3629 unsigned g_src_pos;
3630 isl_size src_off, dst_off;
3631 isl_space *space;
3633 if (!qp)
3634 return NULL;
3636 ctx = isl_qpolynomial_get_ctx(qp);
3637 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3638 isl_die(ctx, isl_error_invalid,
3639 "cannot move output/set dimension",
3640 return isl_qpolynomial_free(qp));
3641 if (src_type == isl_dim_div || dst_type == isl_dim_div)
3642 isl_die(ctx, isl_error_invalid, "cannot move local variables",
3643 return isl_qpolynomial_free(qp));
3644 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3645 return isl_qpolynomial_free(qp);
3646 if (dst_type == isl_dim_in)
3647 dst_type = isl_dim_set;
3648 if (src_type == isl_dim_in)
3649 src_type = isl_dim_set;
3651 if (n == 0 &&
3652 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3653 !isl_space_is_named_or_nested(qp->dim, dst_type))
3654 return qp;
3656 src_off = isl_qpolynomial_domain_var_offset(qp, src_type);
3657 dst_off = isl_qpolynomial_domain_var_offset(qp, dst_type);
3658 if (src_off < 0 || dst_off < 0)
3659 return isl_qpolynomial_free(qp);
3661 g_dst_pos = dst_off + dst_pos;
3662 g_src_pos = src_off + src_pos;
3663 if (dst_type > src_type)
3664 g_dst_pos -= n;
3666 qp = local_poly_move_dims(qp, g_dst_pos, g_src_pos, n);
3668 space = isl_qpolynomial_take_domain_space(qp);
3669 space = isl_space_move_dims(space, dst_type, dst_pos,
3670 src_type, src_pos, n);
3671 qp = isl_qpolynomial_restore_domain_space(qp, space);
3673 return qp;
3676 /* Given a quasi-polynomial on a domain (A -> B),
3677 * interchange A and B in the wrapped domain
3678 * to obtain a quasi-polynomial on the domain (B -> A).
3680 __isl_give isl_qpolynomial *isl_qpolynomial_domain_reverse(
3681 __isl_take isl_qpolynomial *qp)
3683 isl_space *space;
3684 isl_size n_in, n_out, offset;
3686 space = isl_qpolynomial_peek_domain_space(qp);
3687 offset = isl_space_offset(space, isl_dim_set);
3688 n_in = isl_space_wrapped_dim(space, isl_dim_set, isl_dim_in);
3689 n_out = isl_space_wrapped_dim(space, isl_dim_set, isl_dim_out);
3690 if (offset < 0 || n_in < 0 || n_out < 0)
3691 return isl_qpolynomial_free(qp);
3693 qp = local_poly_move_dims(qp, offset, offset + n_in, n_out);
3695 space = isl_qpolynomial_take_domain_space(qp);
3696 space = isl_space_wrapped_reverse(space);
3697 qp = isl_qpolynomial_restore_domain_space(qp, space);
3699 return qp;
3702 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(
3703 __isl_take isl_space *space, isl_int *f, isl_int denom)
3705 isl_size d;
3706 isl_poly *poly;
3708 space = isl_space_domain(space);
3709 if (!space)
3710 return NULL;
3712 d = isl_space_dim(space, isl_dim_all);
3713 poly = d < 0 ? NULL : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
3715 return isl_qpolynomial_alloc(space, 0, poly);
3718 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3720 isl_ctx *ctx;
3721 isl_poly *poly;
3722 isl_qpolynomial *qp;
3724 if (!aff)
3725 return NULL;
3727 ctx = isl_aff_get_ctx(aff);
3728 poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3729 aff->v->size - 1);
3731 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3732 aff->ls->div->n_row, poly);
3733 if (!qp)
3734 goto error;
3736 isl_mat_free(qp->div);
3737 qp->div = isl_mat_copy(aff->ls->div);
3738 qp->div = isl_mat_cow(qp->div);
3739 if (!qp->div)
3740 goto error;
3742 isl_aff_free(aff);
3743 qp = reduce_divs(qp);
3744 qp = remove_redundant_divs(qp);
3745 return qp;
3746 error:
3747 isl_aff_free(aff);
3748 return isl_qpolynomial_free(qp);
3751 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3752 __isl_take isl_pw_aff *pwaff)
3754 int i;
3755 isl_pw_qpolynomial *pwqp;
3757 if (!pwaff)
3758 return NULL;
3760 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3761 pwaff->n);
3763 for (i = 0; i < pwaff->n; ++i) {
3764 isl_set *dom;
3765 isl_qpolynomial *qp;
3767 dom = isl_set_copy(pwaff->p[i].set);
3768 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3769 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3772 isl_pw_aff_free(pwaff);
3773 return pwqp;
3776 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3777 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3779 isl_aff *aff;
3781 aff = isl_constraint_get_bound(c, type, pos);
3782 isl_constraint_free(c);
3783 return isl_qpolynomial_from_aff(aff);
3786 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3787 * in "qp" by subs[i].
3789 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3790 __isl_take isl_qpolynomial *qp,
3791 enum isl_dim_type type, unsigned first, unsigned n,
3792 __isl_keep isl_qpolynomial **subs)
3794 int i;
3795 isl_poly *poly;
3796 isl_poly **polys;
3798 if (n == 0)
3799 return qp;
3801 if (!qp)
3802 return NULL;
3804 if (type == isl_dim_out)
3805 isl_die(qp->dim->ctx, isl_error_invalid,
3806 "cannot substitute output/set dimension",
3807 goto error);
3808 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3809 return isl_qpolynomial_free(qp);
3810 type = domain_type(type);
3812 for (i = 0; i < n; ++i)
3813 if (!subs[i])
3814 goto error;
3816 for (i = 0; i < n; ++i)
3817 if (isl_qpolynomial_check_equal_space(qp, subs[i]) < 0)
3818 goto error;
3820 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3821 for (i = 0; i < n; ++i)
3822 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3824 first += pos(qp->dim, type);
3826 polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
3827 if (!polys)
3828 goto error;
3829 for (i = 0; i < n; ++i)
3830 polys[i] = subs[i]->poly;
3832 poly = isl_qpolynomial_take_poly(qp);
3833 poly = isl_poly_subs(poly, first, n, polys);
3834 qp = isl_qpolynomial_restore_poly(qp, poly);
3836 free(polys);
3838 return qp;
3839 error:
3840 isl_qpolynomial_free(qp);
3841 return NULL;
3844 /* Extend "bset" with extra set dimensions for each integer division
3845 * in "qp" and then call "fn" with the extended bset and the polynomial
3846 * that results from replacing each of the integer divisions by the
3847 * corresponding extra set dimension.
3849 isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3850 __isl_keep isl_basic_set *bset,
3851 isl_stat (*fn)(__isl_take isl_basic_set *bset,
3852 __isl_take isl_qpolynomial *poly, void *user), void *user)
3854 isl_space *space;
3855 isl_local_space *ls;
3856 isl_poly *poly;
3857 isl_qpolynomial *polynomial;
3859 if (!qp || !bset)
3860 return isl_stat_error;
3861 if (qp->div->n_row == 0)
3862 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3863 user);
3865 space = isl_space_copy(qp->dim);
3866 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3867 poly = isl_qpolynomial_get_poly(qp);
3868 polynomial = isl_qpolynomial_alloc(space, 0, poly);
3869 bset = isl_basic_set_copy(bset);
3870 ls = isl_qpolynomial_get_domain_local_space(qp);
3871 bset = isl_local_space_lift_basic_set(ls, bset);
3873 return fn(bset, polynomial, user);
3876 /* Return total degree in variables first (inclusive) up to last (exclusive).
3878 int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3880 int deg = -1;
3881 int i;
3882 isl_bool is_zero, is_cst;
3883 isl_poly_rec *rec;
3885 is_zero = isl_poly_is_zero(poly);
3886 if (is_zero < 0)
3887 return -2;
3888 if (is_zero)
3889 return -1;
3890 is_cst = isl_poly_is_cst(poly);
3891 if (is_cst < 0)
3892 return -2;
3893 if (is_cst || poly->var < first)
3894 return 0;
3896 rec = isl_poly_as_rec(poly);
3897 if (!rec)
3898 return -2;
3900 for (i = 0; i < rec->n; ++i) {
3901 int d;
3903 is_zero = isl_poly_is_zero(rec->p[i]);
3904 if (is_zero < 0)
3905 return -2;
3906 if (is_zero)
3907 continue;
3908 d = isl_poly_degree(rec->p[i], first, last);
3909 if (poly->var < last)
3910 d += i;
3911 if (d > deg)
3912 deg = d;
3915 return deg;
3918 /* Return total degree in set variables.
3920 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3922 isl_size ovar;
3923 isl_size nvar;
3925 if (!poly)
3926 return -2;
3928 ovar = isl_space_offset(poly->dim, isl_dim_set);
3929 nvar = isl_space_dim(poly->dim, isl_dim_set);
3930 if (ovar < 0 || nvar < 0)
3931 return -2;
3932 return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3935 __isl_give isl_poly *isl_poly_coeff(__isl_keep isl_poly *poly,
3936 unsigned pos, int deg)
3938 int i;
3939 isl_bool is_cst;
3940 isl_poly_rec *rec;
3942 is_cst = isl_poly_is_cst(poly);
3943 if (is_cst < 0)
3944 return NULL;
3945 if (is_cst || poly->var < pos) {
3946 if (deg == 0)
3947 return isl_poly_copy(poly);
3948 else
3949 return isl_poly_zero(poly->ctx);
3952 rec = isl_poly_as_rec(poly);
3953 if (!rec)
3954 return NULL;
3956 if (poly->var == pos) {
3957 if (deg < rec->n)
3958 return isl_poly_copy(rec->p[deg]);
3959 else
3960 return isl_poly_zero(poly->ctx);
3963 poly = isl_poly_copy(poly);
3964 poly = isl_poly_cow(poly);
3965 rec = isl_poly_as_rec(poly);
3966 if (!rec)
3967 goto error;
3969 for (i = 0; i < rec->n; ++i) {
3970 isl_poly *t;
3971 t = isl_poly_coeff(rec->p[i], pos, deg);
3972 if (!t)
3973 goto error;
3974 isl_poly_free(rec->p[i]);
3975 rec->p[i] = t;
3978 return poly;
3979 error:
3980 isl_poly_free(poly);
3981 return NULL;
3984 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3986 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3987 __isl_keep isl_qpolynomial *qp,
3988 enum isl_dim_type type, unsigned t_pos, int deg)
3990 unsigned g_pos;
3991 isl_poly *poly;
3992 isl_qpolynomial *c;
3994 if (!qp)
3995 return NULL;
3997 if (type == isl_dim_out)
3998 isl_die(qp->div->ctx, isl_error_invalid,
3999 "output/set dimension does not have a coefficient",
4000 return NULL);
4001 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
4002 return NULL;
4003 type = domain_type(type);
4005 g_pos = pos(qp->dim, type) + t_pos;
4006 poly = isl_poly_coeff(isl_qpolynomial_peek_poly(qp), g_pos, deg);
4008 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim),
4009 qp->div->n_row, poly);
4010 if (!c)
4011 return NULL;
4012 isl_mat_free(c->div);
4013 c->div = isl_qpolynomial_get_local(qp);
4014 if (!c->div)
4015 goto error;
4016 return c;
4017 error:
4018 isl_qpolynomial_free(c);
4019 return NULL;
4022 /* Homogenize the polynomial in the variables first (inclusive) up to
4023 * last (exclusive) by inserting powers of variable first.
4024 * Variable first is assumed not to appear in the input.
4026 __isl_give isl_poly *isl_poly_homogenize(__isl_take isl_poly *poly, int deg,
4027 int target, int first, int last)
4029 int i;
4030 isl_bool is_zero, is_cst;
4031 isl_poly_rec *rec;
4033 is_zero = isl_poly_is_zero(poly);
4034 if (is_zero < 0)
4035 return isl_poly_free(poly);
4036 if (is_zero)
4037 return poly;
4038 if (deg == target)
4039 return poly;
4040 is_cst = isl_poly_is_cst(poly);
4041 if (is_cst < 0)
4042 return isl_poly_free(poly);
4043 if (is_cst || poly->var < first) {
4044 isl_poly *hom;
4046 hom = isl_poly_var_pow(poly->ctx, first, target - deg);
4047 if (!hom)
4048 goto error;
4049 rec = isl_poly_as_rec(hom);
4050 rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
4052 return hom;
4055 poly = isl_poly_cow(poly);
4056 rec = isl_poly_as_rec(poly);
4057 if (!rec)
4058 goto error;
4060 for (i = 0; i < rec->n; ++i) {
4061 is_zero = isl_poly_is_zero(rec->p[i]);
4062 if (is_zero < 0)
4063 return isl_poly_free(poly);
4064 if (is_zero)
4065 continue;
4066 rec->p[i] = isl_poly_homogenize(rec->p[i],
4067 poly->var < last ? deg + i : i, target,
4068 first, last);
4069 if (!rec->p[i])
4070 goto error;
4073 return poly;
4074 error:
4075 isl_poly_free(poly);
4076 return NULL;
4079 /* Homogenize the polynomial in the set variables by introducing
4080 * powers of an extra set variable at position 0.
4082 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
4083 __isl_take isl_qpolynomial *poly)
4085 isl_size ovar;
4086 isl_size nvar;
4087 int deg = isl_qpolynomial_degree(poly);
4089 if (deg < -1)
4090 goto error;
4092 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
4093 poly = isl_qpolynomial_cow(poly);
4094 if (!poly)
4095 goto error;
4097 ovar = isl_space_offset(poly->dim, isl_dim_set);
4098 nvar = isl_space_dim(poly->dim, isl_dim_set);
4099 if (ovar < 0 || nvar < 0)
4100 return isl_qpolynomial_free(poly);
4101 poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
4102 if (!poly->poly)
4103 goto error;
4105 return poly;
4106 error:
4107 isl_qpolynomial_free(poly);
4108 return NULL;
4111 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *space,
4112 __isl_take isl_mat *div)
4114 isl_term *term;
4115 isl_size d;
4116 int n;
4118 d = isl_space_dim(space, isl_dim_all);
4119 if (d < 0 || !div)
4120 goto error;
4122 n = d + div->n_row;
4124 term = isl_calloc(space->ctx, struct isl_term,
4125 sizeof(struct isl_term) + (n - 1) * sizeof(int));
4126 if (!term)
4127 goto error;
4129 term->ref = 1;
4130 term->dim = space;
4131 term->div = div;
4132 isl_int_init(term->n);
4133 isl_int_init(term->d);
4135 return term;
4136 error:
4137 isl_space_free(space);
4138 isl_mat_free(div);
4139 return NULL;
4142 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
4144 if (!term)
4145 return NULL;
4147 term->ref++;
4148 return term;
4151 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
4153 int i;
4154 isl_term *dup;
4155 isl_size total;
4157 total = isl_term_dim(term, isl_dim_all);
4158 if (total < 0)
4159 return NULL;
4161 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
4162 if (!dup)
4163 return NULL;
4165 isl_int_set(dup->n, term->n);
4166 isl_int_set(dup->d, term->d);
4168 for (i = 0; i < total; ++i)
4169 dup->pow[i] = term->pow[i];
4171 return dup;
4174 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
4176 if (!term)
4177 return NULL;
4179 if (term->ref == 1)
4180 return term;
4181 term->ref--;
4182 return isl_term_dup(term);
4185 __isl_null isl_term *isl_term_free(__isl_take isl_term *term)
4187 if (!term)
4188 return NULL;
4190 if (--term->ref > 0)
4191 return NULL;
4193 isl_space_free(term->dim);
4194 isl_mat_free(term->div);
4195 isl_int_clear(term->n);
4196 isl_int_clear(term->d);
4197 free(term);
4199 return NULL;
4202 isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
4204 isl_size dim;
4206 if (!term)
4207 return isl_size_error;
4209 switch (type) {
4210 case isl_dim_param:
4211 case isl_dim_in:
4212 case isl_dim_out: return isl_space_dim(term->dim, type);
4213 case isl_dim_div: return term->div->n_row;
4214 case isl_dim_all: dim = isl_space_dim(term->dim, isl_dim_all);
4215 if (dim < 0)
4216 return isl_size_error;
4217 return dim + term->div->n_row;
4218 default: return isl_size_error;
4222 /* Return the space of "term".
4224 static __isl_keep isl_space *isl_term_peek_space(__isl_keep isl_term *term)
4226 return term ? term->dim : NULL;
4229 /* Return the offset of the first variable of type "type" within
4230 * the variables of "term".
4232 static isl_size isl_term_offset(__isl_keep isl_term *term,
4233 enum isl_dim_type type)
4235 isl_space *space;
4237 space = isl_term_peek_space(term);
4238 if (!space)
4239 return isl_size_error;
4241 switch (type) {
4242 case isl_dim_param:
4243 case isl_dim_set: return isl_space_offset(space, type);
4244 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
4245 default:
4246 isl_die(isl_term_get_ctx(term), isl_error_invalid,
4247 "invalid dimension type", return isl_size_error);
4251 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
4253 return term ? term->dim->ctx : NULL;
4256 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
4258 if (!term)
4259 return;
4260 isl_int_set(*n, term->n);
4263 /* Return the coefficient of the term "term".
4265 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
4267 if (!term)
4268 return NULL;
4270 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
4271 term->n, term->d);
4274 #undef TYPE
4275 #define TYPE isl_term
4276 static
4277 #include "check_type_range_templ.c"
4279 isl_size isl_term_get_exp(__isl_keep isl_term *term,
4280 enum isl_dim_type type, unsigned pos)
4282 isl_size offset;
4284 if (isl_term_check_range(term, type, pos, 1) < 0)
4285 return isl_size_error;
4286 offset = isl_term_offset(term, type);
4287 if (offset < 0)
4288 return isl_size_error;
4290 return term->pow[offset + pos];
4293 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
4295 isl_local_space *ls;
4296 isl_aff *aff;
4298 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
4299 return NULL;
4301 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
4302 isl_mat_copy(term->div));
4303 aff = isl_aff_alloc(ls);
4304 if (!aff)
4305 return NULL;
4307 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
4309 aff = isl_aff_normalize(aff);
4311 return aff;
4314 __isl_give isl_term *isl_poly_foreach_term(__isl_keep isl_poly *poly,
4315 isl_stat (*fn)(__isl_take isl_term *term, void *user),
4316 __isl_take isl_term *term, void *user)
4318 int i;
4319 isl_bool is_zero, is_bad, is_cst;
4320 isl_poly_rec *rec;
4322 is_zero = isl_poly_is_zero(poly);
4323 if (is_zero < 0 || !term)
4324 goto error;
4326 if (is_zero)
4327 return term;
4329 is_cst = isl_poly_is_cst(poly);
4330 is_bad = isl_poly_is_nan(poly);
4331 if (is_bad >= 0 && !is_bad)
4332 is_bad = isl_poly_is_infty(poly);
4333 if (is_bad >= 0 && !is_bad)
4334 is_bad = isl_poly_is_neginfty(poly);
4335 if (is_cst < 0 || is_bad < 0)
4336 return isl_term_free(term);
4337 if (is_bad)
4338 isl_die(isl_term_get_ctx(term), isl_error_invalid,
4339 "cannot handle NaN/infty polynomial",
4340 return isl_term_free(term));
4342 if (is_cst) {
4343 isl_poly_cst *cst;
4344 cst = isl_poly_as_cst(poly);
4345 if (!cst)
4346 goto error;
4347 term = isl_term_cow(term);
4348 if (!term)
4349 goto error;
4350 isl_int_set(term->n, cst->n);
4351 isl_int_set(term->d, cst->d);
4352 if (fn(isl_term_copy(term), user) < 0)
4353 goto error;
4354 return term;
4357 rec = isl_poly_as_rec(poly);
4358 if (!rec)
4359 goto error;
4361 for (i = 0; i < rec->n; ++i) {
4362 term = isl_term_cow(term);
4363 if (!term)
4364 goto error;
4365 term->pow[poly->var] = i;
4366 term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4367 if (!term)
4368 goto error;
4370 term = isl_term_cow(term);
4371 if (!term)
4372 return NULL;
4373 term->pow[poly->var] = 0;
4375 return term;
4376 error:
4377 isl_term_free(term);
4378 return NULL;
4381 isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
4382 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4384 isl_local *local;
4385 isl_term *term;
4387 if (!qp)
4388 return isl_stat_error;
4390 local = isl_qpolynomial_get_local(qp);
4391 term = isl_term_alloc(isl_space_copy(qp->dim), local);
4392 if (!term)
4393 return isl_stat_error;
4395 term = isl_poly_foreach_term(isl_qpolynomial_peek_poly(qp),
4396 fn, term, user);
4398 isl_term_free(term);
4400 return term ? isl_stat_ok : isl_stat_error;
4403 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
4405 isl_poly *poly;
4406 isl_qpolynomial *qp;
4407 int i;
4408 isl_size n;
4410 n = isl_term_dim(term, isl_dim_all);
4411 if (n < 0)
4412 term = isl_term_free(term);
4413 if (!term)
4414 return NULL;
4416 poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4417 for (i = 0; i < n; ++i) {
4418 if (!term->pow[i])
4419 continue;
4420 poly = isl_poly_mul(poly,
4421 isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4424 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4425 term->div->n_row, poly);
4426 if (!qp)
4427 goto error;
4428 isl_mat_free(qp->div);
4429 qp->div = isl_mat_copy(term->div);
4430 if (!qp->div)
4431 goto error;
4433 isl_term_free(term);
4434 return qp;
4435 error:
4436 isl_qpolynomial_free(qp);
4437 isl_term_free(term);
4438 return NULL;
4441 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
4442 __isl_take isl_space *space)
4444 int i;
4445 int extra;
4446 isl_size total, d_set, d_qp;
4448 if (!qp || !space)
4449 goto error;
4451 if (isl_space_is_equal(qp->dim, space)) {
4452 isl_space_free(space);
4453 return qp;
4456 qp = isl_qpolynomial_cow(qp);
4457 if (!qp)
4458 goto error;
4460 d_set = isl_space_dim(space, isl_dim_set);
4461 d_qp = isl_qpolynomial_domain_dim(qp, isl_dim_set);
4462 extra = d_set - d_qp;
4463 total = isl_space_dim(qp->dim, isl_dim_all);
4464 if (d_set < 0 || d_qp < 0 || total < 0)
4465 goto error;
4466 if (qp->div->n_row) {
4467 int *exp;
4469 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4470 if (!exp)
4471 goto error;
4472 for (i = 0; i < qp->div->n_row; ++i)
4473 exp[i] = extra + i;
4474 qp->poly = expand(qp->poly, exp, total);
4475 free(exp);
4476 if (!qp->poly)
4477 goto error;
4479 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4480 if (!qp->div)
4481 goto error;
4482 for (i = 0; i < qp->div->n_row; ++i)
4483 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4485 isl_space_free(isl_qpolynomial_take_domain_space(qp));
4486 qp = isl_qpolynomial_restore_domain_space(qp, space);
4488 return qp;
4489 error:
4490 isl_space_free(space);
4491 isl_qpolynomial_free(qp);
4492 return NULL;
4495 /* For each parameter or variable that does not appear in qp,
4496 * first eliminate the variable from all constraints and then set it to zero.
4498 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
4499 __isl_keep isl_qpolynomial *qp)
4501 int *active = NULL;
4502 int i;
4503 isl_size d;
4504 isl_size nparam;
4505 isl_size nvar;
4507 d = isl_set_dim(set, isl_dim_all);
4508 if (d < 0 || !qp)
4509 goto error;
4511 active = isl_calloc_array(set->ctx, int, d);
4512 if (set_active(qp, active) < 0)
4513 goto error;
4515 for (i = 0; i < d; ++i)
4516 if (!active[i])
4517 break;
4519 if (i == d) {
4520 free(active);
4521 return set;
4524 nparam = isl_set_dim(set, isl_dim_param);
4525 nvar = isl_set_dim(set, isl_dim_set);
4526 if (nparam < 0 || nvar < 0)
4527 goto error;
4528 for (i = 0; i < nparam; ++i) {
4529 if (active[i])
4530 continue;
4531 set = isl_set_eliminate(set, isl_dim_param, i, 1);
4532 set = isl_set_fix_si(set, isl_dim_param, i, 0);
4534 for (i = 0; i < nvar; ++i) {
4535 if (active[nparam + i])
4536 continue;
4537 set = isl_set_eliminate(set, isl_dim_set, i, 1);
4538 set = isl_set_fix_si(set, isl_dim_set, i, 0);
4541 free(active);
4543 return set;
4544 error:
4545 free(active);
4546 isl_set_free(set);
4547 return NULL;
4550 struct isl_opt_data {
4551 isl_qpolynomial *qp;
4552 int first;
4553 isl_val *opt;
4554 int max;
4557 static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
4559 struct isl_opt_data *data = (struct isl_opt_data *)user;
4560 isl_val *val;
4562 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4563 if (data->first) {
4564 data->first = 0;
4565 data->opt = val;
4566 } else if (data->max) {
4567 data->opt = isl_val_max(data->opt, val);
4568 } else {
4569 data->opt = isl_val_min(data->opt, val);
4572 return isl_stat_ok;
4575 __isl_give isl_val *isl_qpolynomial_opt_on_domain(
4576 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4578 struct isl_opt_data data = { NULL, 1, NULL, max };
4579 isl_bool is_cst;
4581 if (!set)
4582 goto error;
4584 is_cst = isl_poly_is_cst(isl_qpolynomial_peek_poly(qp));
4585 if (is_cst < 0)
4586 goto error;
4587 if (is_cst) {
4588 isl_set_free(set);
4589 data.opt = isl_qpolynomial_get_constant_val(qp);
4590 isl_qpolynomial_free(qp);
4591 return data.opt;
4594 set = fix_inactive(set, qp);
4596 data.qp = qp;
4597 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4598 goto error;
4600 if (data.first)
4601 data.opt = isl_val_zero(isl_set_get_ctx(set));
4603 isl_set_free(set);
4604 isl_qpolynomial_free(qp);
4605 return data.opt;
4606 error:
4607 isl_set_free(set);
4608 isl_qpolynomial_free(qp);
4609 isl_val_free(data.opt);
4610 return NULL;
4613 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4614 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4616 int i;
4617 int n_sub;
4618 isl_ctx *ctx;
4619 isl_space *space;
4620 isl_poly **subs;
4621 isl_mat *mat, *diag;
4623 qp = isl_qpolynomial_cow(qp);
4625 space = isl_qpolynomial_peek_domain_space(qp);
4626 if (isl_morph_check_applies(morph, space) < 0)
4627 goto error;
4629 ctx = isl_qpolynomial_get_ctx(qp);
4630 n_sub = morph->inv->n_row - 1;
4631 if (morph->inv->n_row != morph->inv->n_col)
4632 n_sub += qp->div->n_row;
4633 subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
4634 if (n_sub && !subs)
4635 goto error;
4637 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4638 subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4639 morph->inv->row[0][0], morph->inv->n_col);
4640 if (morph->inv->n_row != morph->inv->n_col)
4641 for (i = 0; i < qp->div->n_row; ++i)
4642 subs[morph->inv->n_row - 1 + i] =
4643 isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4645 qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4647 for (i = 0; i < n_sub; ++i)
4648 isl_poly_free(subs[i]);
4649 free(subs);
4651 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4652 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4653 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4654 mat = isl_mat_diagonal(mat, diag);
4655 qp->div = isl_mat_product(qp->div, mat);
4657 if (!qp->poly || !qp->div)
4658 goto error;
4660 isl_space_free(isl_qpolynomial_take_domain_space(qp));
4661 space = isl_space_copy(morph->ran->dim);
4662 qp = isl_qpolynomial_restore_domain_space(qp, space);
4664 isl_morph_free(morph);
4666 return qp;
4667 error:
4668 isl_qpolynomial_free(qp);
4669 isl_morph_free(morph);
4670 return NULL;
4673 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4674 __isl_take isl_union_pw_qpolynomial *upwqp1,
4675 __isl_take isl_union_pw_qpolynomial *upwqp2)
4677 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4678 &isl_pw_qpolynomial_mul);
4681 /* Reorder the dimension of "qp" according to the given reordering.
4683 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4684 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4686 isl_space *space;
4687 isl_poly *poly;
4688 isl_local *local;
4690 if (!qp)
4691 goto error;
4693 r = isl_reordering_extend(r, qp->div->n_row);
4694 if (!r)
4695 goto error;
4697 local = isl_qpolynomial_take_local(qp);
4698 local = isl_local_reorder(local, isl_reordering_copy(r));
4699 qp = isl_qpolynomial_restore_local(qp, local);
4701 poly = isl_qpolynomial_take_poly(qp);
4702 poly = reorder(poly, r->pos);
4703 qp = isl_qpolynomial_restore_poly(qp, poly);
4705 space = isl_reordering_get_space(r);
4706 qp = isl_qpolynomial_reset_domain_space(qp, space);
4708 isl_reordering_free(r);
4709 return qp;
4710 error:
4711 isl_qpolynomial_free(qp);
4712 isl_reordering_free(r);
4713 return NULL;
4716 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4717 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4719 isl_space *domain_space;
4720 isl_bool equal_params;
4722 domain_space = isl_qpolynomial_peek_domain_space(qp);
4723 equal_params = isl_space_has_equal_params(domain_space, model);
4724 if (equal_params < 0)
4725 goto error;
4726 if (!equal_params) {
4727 isl_reordering *exp;
4729 exp = isl_parameter_alignment_reordering(domain_space, model);
4730 qp = isl_qpolynomial_realign_domain(qp, exp);
4733 isl_space_free(model);
4734 return qp;
4735 error:
4736 isl_space_free(model);
4737 isl_qpolynomial_free(qp);
4738 return NULL;
4741 struct isl_split_periods_data {
4742 int max_periods;
4743 isl_pw_qpolynomial *res;
4746 /* Create a slice where the integer division "div" has the fixed value "v".
4747 * In particular, if "div" refers to floor(f/m), then create a slice
4749 * m v <= f <= m v + (m - 1)
4751 * or
4753 * f - m v >= 0
4754 * -f + m v + (m - 1) >= 0
4756 static __isl_give isl_set *set_div_slice(__isl_take isl_space *space,
4757 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4759 isl_size total;
4760 isl_basic_set *bset = NULL;
4761 int k;
4763 total = isl_space_dim(space, isl_dim_all);
4764 if (total < 0 || !qp)
4765 goto error;
4767 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4769 k = isl_basic_set_alloc_inequality(bset);
4770 if (k < 0)
4771 goto error;
4772 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4773 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4775 k = isl_basic_set_alloc_inequality(bset);
4776 if (k < 0)
4777 goto error;
4778 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4779 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4780 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4781 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4783 isl_space_free(space);
4784 return isl_set_from_basic_set(bset);
4785 error:
4786 isl_basic_set_free(bset);
4787 isl_space_free(space);
4788 return NULL;
4791 static isl_stat split_periods(__isl_take isl_set *set,
4792 __isl_take isl_qpolynomial *qp, void *user);
4794 /* Create a slice of the domain "set" such that integer division "div"
4795 * has the fixed value "v" and add the results to data->res,
4796 * replacing the integer division by "v" in "qp".
4798 static isl_stat set_div(__isl_take isl_set *set,
4799 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4800 struct isl_split_periods_data *data)
4802 int i;
4803 isl_size div_pos;
4804 isl_set *slice;
4805 isl_poly *cst;
4807 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4808 set = isl_set_intersect(set, slice);
4810 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4811 if (div_pos < 0)
4812 goto error;
4814 for (i = div + 1; i < qp->div->n_row; ++i) {
4815 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div]))
4816 continue;
4817 isl_int_addmul(qp->div->row[i][1],
4818 qp->div->row[i][2 + div_pos + div], v);
4819 isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0);
4822 cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4823 qp = substitute_div(qp, div, cst);
4825 return split_periods(set, qp, data);
4826 error:
4827 isl_set_free(set);
4828 isl_qpolynomial_free(qp);
4829 return isl_stat_error;
4832 /* Split the domain "set" such that integer division "div"
4833 * has a fixed value (ranging from "min" to "max") on each slice
4834 * and add the results to data->res.
4836 static isl_stat split_div(__isl_take isl_set *set,
4837 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4838 struct isl_split_periods_data *data)
4840 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4841 isl_set *set_i = isl_set_copy(set);
4842 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4844 if (set_div(set_i, qp_i, div, min, data) < 0)
4845 goto error;
4847 isl_set_free(set);
4848 isl_qpolynomial_free(qp);
4849 return isl_stat_ok;
4850 error:
4851 isl_set_free(set);
4852 isl_qpolynomial_free(qp);
4853 return isl_stat_error;
4856 /* If "qp" refers to any integer division
4857 * that can only attain "max_periods" distinct values on "set"
4858 * then split the domain along those distinct values.
4859 * Add the results (or the original if no splitting occurs)
4860 * to data->res.
4862 static isl_stat split_periods(__isl_take isl_set *set,
4863 __isl_take isl_qpolynomial *qp, void *user)
4865 int i;
4866 isl_pw_qpolynomial *pwqp;
4867 struct isl_split_periods_data *data;
4868 isl_int min, max;
4869 isl_size div_pos;
4870 isl_stat r = isl_stat_ok;
4872 data = (struct isl_split_periods_data *)user;
4874 if (!set || !qp)
4875 goto error;
4877 if (qp->div->n_row == 0) {
4878 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4879 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4880 return isl_stat_ok;
4883 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
4884 if (div_pos < 0)
4885 goto error;
4887 isl_int_init(min);
4888 isl_int_init(max);
4889 for (i = 0; i < qp->div->n_row; ++i) {
4890 enum isl_lp_result lp_res;
4892 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + div_pos,
4893 qp->div->n_row) != -1)
4894 continue;
4896 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4897 set->ctx->one, &min, NULL, NULL);
4898 if (lp_res == isl_lp_error)
4899 goto error2;
4900 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4901 continue;
4902 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4904 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4905 set->ctx->one, &max, NULL, NULL);
4906 if (lp_res == isl_lp_error)
4907 goto error2;
4908 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4909 continue;
4910 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4912 isl_int_sub(max, max, min);
4913 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4914 isl_int_add(max, max, min);
4915 break;
4919 if (i < qp->div->n_row) {
4920 r = split_div(set, qp, i, min, max, data);
4921 } else {
4922 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4923 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4926 isl_int_clear(max);
4927 isl_int_clear(min);
4929 return r;
4930 error2:
4931 isl_int_clear(max);
4932 isl_int_clear(min);
4933 error:
4934 isl_set_free(set);
4935 isl_qpolynomial_free(qp);
4936 return isl_stat_error;
4939 /* If any quasi-polynomial in pwqp refers to any integer division
4940 * that can only attain "max_periods" distinct values on its domain
4941 * then split the domain along those distinct values.
4943 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4944 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4946 struct isl_split_periods_data data;
4948 data.max_periods = max_periods;
4949 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4951 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4952 goto error;
4954 isl_pw_qpolynomial_free(pwqp);
4956 return data.res;
4957 error:
4958 isl_pw_qpolynomial_free(data.res);
4959 isl_pw_qpolynomial_free(pwqp);
4960 return NULL;
4963 /* Construct a piecewise quasipolynomial that is constant on the given
4964 * domain. In particular, it is
4965 * 0 if cst == 0
4966 * 1 if cst == 1
4967 * infinity if cst == -1
4969 * If cst == -1, then explicitly check whether the domain is empty and,
4970 * if so, return 0 instead.
4972 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4973 __isl_take isl_basic_set *bset, int cst)
4975 isl_space *space;
4976 isl_qpolynomial *qp;
4978 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4979 cst = 0;
4980 if (!bset)
4981 return NULL;
4983 bset = isl_basic_set_params(bset);
4984 space = isl_basic_set_get_space(bset);
4985 if (cst < 0)
4986 qp = isl_qpolynomial_infty_on_domain(space);
4987 else if (cst == 0)
4988 qp = isl_qpolynomial_zero_on_domain(space);
4989 else
4990 qp = isl_qpolynomial_one_on_domain(space);
4991 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4994 /* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4995 * "fn" is the function that is called on each factor.
4996 * "pwpq" collects the results.
4998 struct isl_multiplicative_call_data_pw_qpolynomial {
4999 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset);
5000 isl_pw_qpolynomial *pwqp;
5003 /* Call "fn" on "bset" and return the result,
5004 * but first check if "bset" has any redundant constraints or
5005 * implicit equality constraints.
5006 * If so, there may be further opportunities for detecting factors or
5007 * removing equality constraints, so recursively call
5008 * the top-level isl_basic_set_multiplicative_call.
5010 static __isl_give isl_pw_qpolynomial *multiplicative_call_base(
5011 __isl_take isl_basic_set *bset,
5012 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
5014 isl_size n1, n2, n_eq;
5016 n1 = isl_basic_set_n_constraint(bset);
5017 if (n1 < 0)
5018 bset = isl_basic_set_free(bset);
5019 bset = isl_basic_set_remove_redundancies(bset);
5020 bset = isl_basic_set_detect_equalities(bset);
5021 n2 = isl_basic_set_n_constraint(bset);
5022 n_eq = isl_basic_set_n_equality(bset);
5023 if (n2 < 0 || n_eq < 0)
5024 bset = isl_basic_set_free(bset);
5025 else if (n2 < n1 || n_eq > 0)
5026 return isl_basic_set_multiplicative_call(bset, fn);
5027 return fn(bset);
5030 /* isl_factorizer_every_factor_basic_set callback that applies
5031 * data->fn to the factor "bset" and multiplies in the result
5032 * in data->pwqp.
5034 static isl_bool multiplicative_call_factor_pw_qpolynomial(
5035 __isl_keep isl_basic_set *bset, void *user)
5037 struct isl_multiplicative_call_data_pw_qpolynomial *data = user;
5038 isl_pw_qpolynomial *res;
5040 bset = isl_basic_set_copy(bset);
5041 res = multiplicative_call_base(bset, data->fn);
5042 data->pwqp = isl_pw_qpolynomial_mul(data->pwqp, res);
5043 if (!data->pwqp)
5044 return isl_bool_error;
5046 return isl_bool_true;
5049 /* Factor bset, call fn on each of the factors and return the product.
5051 * If no factors can be found, simply call fn on the input.
5052 * Otherwise, construct the factors based on the factorizer,
5053 * call fn on each factor and compute the product.
5055 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
5056 __isl_take isl_basic_set *bset,
5057 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
5059 struct isl_multiplicative_call_data_pw_qpolynomial data = { fn };
5060 isl_space *space;
5061 isl_set *set;
5062 isl_factorizer *f;
5063 isl_qpolynomial *qp;
5064 isl_bool every;
5066 f = isl_basic_set_factorizer(bset);
5067 if (!f)
5068 goto error;
5069 if (f->n_group == 0) {
5070 isl_factorizer_free(f);
5071 return multiplicative_call_base(bset, fn);
5074 space = isl_basic_set_get_space(bset);
5075 space = isl_space_params(space);
5076 set = isl_set_universe(isl_space_copy(space));
5077 qp = isl_qpolynomial_one_on_domain(space);
5078 data.pwqp = isl_pw_qpolynomial_alloc(set, qp);
5080 every = isl_factorizer_every_factor_basic_set(f,
5081 &multiplicative_call_factor_pw_qpolynomial, &data);
5082 if (every < 0)
5083 data.pwqp = isl_pw_qpolynomial_free(data.pwqp);
5085 isl_basic_set_free(bset);
5086 isl_factorizer_free(f);
5088 return data.pwqp;
5089 error:
5090 isl_basic_set_free(bset);
5091 return NULL;
5094 /* Factor bset, call fn on each of the factors and return the product.
5095 * The function is assumed to evaluate to zero on empty domains,
5096 * to one on zero-dimensional domains and to infinity on unbounded domains
5097 * and will not be called explicitly on zero-dimensional or unbounded domains.
5099 * We first check for some special cases and remove all equalities.
5100 * Then we hand over control to compressed_multiplicative_call.
5102 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
5103 __isl_take isl_basic_set *bset,
5104 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
5106 isl_bool bounded;
5107 isl_size dim;
5108 isl_morph *morph;
5109 isl_pw_qpolynomial *pwqp;
5111 if (!bset)
5112 return NULL;
5114 if (isl_basic_set_plain_is_empty(bset))
5115 return constant_on_domain(bset, 0);
5117 dim = isl_basic_set_dim(bset, isl_dim_set);
5118 if (dim < 0)
5119 goto error;
5120 if (dim == 0)
5121 return constant_on_domain(bset, 1);
5123 bounded = isl_basic_set_is_bounded(bset);
5124 if (bounded < 0)
5125 goto error;
5126 if (!bounded)
5127 return constant_on_domain(bset, -1);
5129 if (bset->n_eq == 0)
5130 return compressed_multiplicative_call(bset, fn);
5132 morph = isl_basic_set_full_compression(bset);
5133 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
5135 pwqp = compressed_multiplicative_call(bset, fn);
5137 morph = isl_morph_dom_params(morph);
5138 morph = isl_morph_ran_params(morph);
5139 morph = isl_morph_inverse(morph);
5141 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
5143 return pwqp;
5144 error:
5145 isl_basic_set_free(bset);
5146 return NULL;
5149 /* Drop all floors in "qp", turning each integer division [a/m] into
5150 * a rational division a/m. If "down" is set, then the integer division
5151 * is replaced by (a-(m-1))/m instead.
5153 static __isl_give isl_qpolynomial *qp_drop_floors(
5154 __isl_take isl_qpolynomial *qp, int down)
5156 int i;
5157 isl_poly *s;
5159 if (!qp)
5160 return NULL;
5161 if (qp->div->n_row == 0)
5162 return qp;
5164 qp = isl_qpolynomial_cow(qp);
5165 if (!qp)
5166 return NULL;
5168 for (i = qp->div->n_row - 1; i >= 0; --i) {
5169 if (down) {
5170 isl_int_sub(qp->div->row[i][1],
5171 qp->div->row[i][1], qp->div->row[i][0]);
5172 isl_int_add_ui(qp->div->row[i][1],
5173 qp->div->row[i][1], 1);
5175 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
5176 qp->div->row[i][0], qp->div->n_col - 1);
5177 qp = substitute_div(qp, i, s);
5178 if (!qp)
5179 return NULL;
5182 return qp;
5185 /* Drop all floors in "pwqp", turning each integer division [a/m] into
5186 * a rational division a/m.
5188 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
5189 __isl_take isl_pw_qpolynomial *pwqp)
5191 int i;
5193 if (!pwqp)
5194 return NULL;
5196 if (isl_pw_qpolynomial_is_zero(pwqp))
5197 return pwqp;
5199 pwqp = isl_pw_qpolynomial_cow(pwqp);
5200 if (!pwqp)
5201 return NULL;
5203 for (i = 0; i < pwqp->n; ++i) {
5204 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
5205 if (!pwqp->p[i].qp)
5206 goto error;
5209 return pwqp;
5210 error:
5211 isl_pw_qpolynomial_free(pwqp);
5212 return NULL;
5215 /* Adjust all the integer divisions in "qp" such that they are at least
5216 * one over the given orthant (identified by "signs"). This ensures
5217 * that they will still be non-negative even after subtracting (m-1)/m.
5219 * In particular, f is replaced by f' + v, changing f = [a/m]
5220 * to f' = [(a - m v)/m].
5221 * If the constant term k in a is smaller than m,
5222 * the constant term of v is set to floor(k/m) - 1.
5223 * For any other term, if the coefficient c and the variable x have
5224 * the same sign, then no changes are needed.
5225 * Otherwise, if the variable is positive (and c is negative),
5226 * then the coefficient of x in v is set to floor(c/m).
5227 * If the variable is negative (and c is positive),
5228 * then the coefficient of x in v is set to ceil(c/m).
5230 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
5231 int *signs)
5233 int i, j;
5234 isl_size div_pos;
5235 isl_vec *v = NULL;
5236 isl_poly *s;
5238 qp = isl_qpolynomial_cow(qp);
5239 div_pos = isl_qpolynomial_domain_var_offset(qp, isl_dim_div);
5240 if (div_pos < 0)
5241 return isl_qpolynomial_free(qp);
5242 qp->div = isl_mat_cow(qp->div);
5243 if (!qp->div)
5244 goto error;
5246 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
5248 for (i = 0; i < qp->div->n_row; ++i) {
5249 isl_int *row = qp->div->row[i];
5250 v = isl_vec_clr(v);
5251 if (!v)
5252 goto error;
5253 if (isl_int_lt(row[1], row[0])) {
5254 isl_int_fdiv_q(v->el[0], row[1], row[0]);
5255 isl_int_sub_ui(v->el[0], v->el[0], 1);
5256 isl_int_submul(row[1], row[0], v->el[0]);
5258 for (j = 0; j < div_pos; ++j) {
5259 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
5260 continue;
5261 if (signs[j] < 0)
5262 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
5263 else
5264 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
5265 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
5267 for (j = 0; j < i; ++j) {
5268 if (isl_int_sgn(row[2 + div_pos + j]) >= 0)
5269 continue;
5270 isl_int_fdiv_q(v->el[1 + div_pos + j],
5271 row[2 + div_pos + j], row[0]);
5272 isl_int_submul(row[2 + div_pos + j],
5273 row[0], v->el[1 + div_pos + j]);
5275 for (j = i + 1; j < qp->div->n_row; ++j) {
5276 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
5277 continue;
5278 isl_seq_combine(qp->div->row[j] + 1,
5279 qp->div->ctx->one, qp->div->row[j] + 1,
5280 qp->div->row[j][2 + div_pos + i], v->el,
5281 v->size);
5283 isl_int_set_si(v->el[1 + div_pos + i], 1);
5284 s = isl_poly_from_affine(qp->dim->ctx, v->el,
5285 qp->div->ctx->one, v->size);
5286 qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
5287 isl_poly_free(s);
5288 if (!qp->poly)
5289 goto error;
5292 isl_vec_free(v);
5293 return qp;
5294 error:
5295 isl_vec_free(v);
5296 isl_qpolynomial_free(qp);
5297 return NULL;
5300 struct isl_to_poly_data {
5301 int sign;
5302 isl_pw_qpolynomial *res;
5303 isl_qpolynomial *qp;
5306 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5307 * We first make all integer divisions positive and then split the
5308 * quasipolynomials into terms with sign data->sign (the direction
5309 * of the requested approximation) and terms with the opposite sign.
5310 * In the first set of terms, each integer division [a/m] is
5311 * overapproximated by a/m, while in the second it is underapproximated
5312 * by (a-(m-1))/m.
5314 static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant,
5315 int *signs, void *user)
5317 struct isl_to_poly_data *data = user;
5318 isl_pw_qpolynomial *t;
5319 isl_qpolynomial *qp, *up, *down;
5321 qp = isl_qpolynomial_copy(data->qp);
5322 qp = make_divs_pos(qp, signs);
5324 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
5325 up = qp_drop_floors(up, 0);
5326 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
5327 down = qp_drop_floors(down, 1);
5329 isl_qpolynomial_free(qp);
5330 qp = isl_qpolynomial_add(up, down);
5332 t = isl_pw_qpolynomial_alloc(orthant, qp);
5333 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
5335 return isl_stat_ok;
5338 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5339 * the polynomial will be an overapproximation. If "sign" is negative,
5340 * it will be an underapproximation. If "sign" is zero, the approximation
5341 * will lie somewhere in between.
5343 * In particular, is sign == 0, we simply drop the floors, turning
5344 * the integer divisions into rational divisions.
5345 * Otherwise, we split the domains into orthants, make all integer divisions
5346 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5347 * depending on the requested sign and the sign of the term in which
5348 * the integer division appears.
5350 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
5351 __isl_take isl_pw_qpolynomial *pwqp, int sign)
5353 int i;
5354 struct isl_to_poly_data data;
5356 if (sign == 0)
5357 return pwqp_drop_floors(pwqp);
5359 if (!pwqp)
5360 return NULL;
5362 data.sign = sign;
5363 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
5365 for (i = 0; i < pwqp->n; ++i) {
5366 if (pwqp->p[i].qp->div->n_row == 0) {
5367 isl_pw_qpolynomial *t;
5368 t = isl_pw_qpolynomial_alloc(
5369 isl_set_copy(pwqp->p[i].set),
5370 isl_qpolynomial_copy(pwqp->p[i].qp));
5371 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
5372 continue;
5374 data.qp = pwqp->p[i].qp;
5375 if (isl_set_foreach_orthant(pwqp->p[i].set,
5376 &to_polynomial_on_orthant, &data) < 0)
5377 goto error;
5380 isl_pw_qpolynomial_free(pwqp);
5382 return data.res;
5383 error:
5384 isl_pw_qpolynomial_free(pwqp);
5385 isl_pw_qpolynomial_free(data.res);
5386 return NULL;
5389 static __isl_give isl_pw_qpolynomial *poly_entry(
5390 __isl_take isl_pw_qpolynomial *pwqp, void *user)
5392 int *sign = user;
5394 return isl_pw_qpolynomial_to_polynomial(pwqp, *sign);
5397 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
5398 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
5400 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5401 &poly_entry, &sign);
5404 /* Return an isl_aff that is equivalent to "qp".
5406 __isl_give isl_aff *isl_qpolynomial_as_aff(__isl_take isl_qpolynomial *qp)
5408 isl_local_space *ls;
5409 isl_vec *vec;
5410 isl_aff *aff;
5411 isl_bool is_affine;
5413 is_affine = isl_qpolynomial_isa_aff(qp);
5414 if (is_affine < 0)
5415 goto error;
5416 if (!is_affine)
5417 isl_die(qp->dim->ctx, isl_error_invalid,
5418 "input quasi-polynomial not affine", goto error);
5419 ls = isl_qpolynomial_get_domain_local_space(qp);
5420 vec = isl_qpolynomial_extract_affine(qp);
5421 aff = isl_aff_alloc_vec(ls, vec);
5422 isl_qpolynomial_free(qp);
5423 return aff;
5424 error:
5425 isl_qpolynomial_free(qp);
5426 return NULL;
5429 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
5430 __isl_take isl_qpolynomial *qp)
5432 return isl_basic_map_from_aff(isl_qpolynomial_as_aff(qp));