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,
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_factorization.h>
18 #include <isl_union_map_private.h>
19 #include <isl_constraint_private.h>
20 #include <isl_polynomial_private.h>
21 #include <isl_point_private.h>
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_range.h>
25 #include <isl_local_space_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_val_private.h>
28 #include <isl_config.h>
30 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
33 case isl_dim_param: return 0;
34 case isl_dim_in: return dim->nparam;
35 case isl_dim_out: return dim->nparam + dim->n_in;
40 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
48 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
53 isl_assert(up->ctx, up->var < 0, return NULL);
55 return (struct isl_upoly_cst *)up;
58 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
63 isl_assert(up->ctx, up->var >= 0, return NULL);
65 return (struct isl_upoly_rec *)up;
68 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
69 __isl_keep struct isl_upoly *up2)
72 struct isl_upoly_rec *rec1, *rec2;
78 if (up1->var != up2->var)
80 if (isl_upoly_is_cst(up1)) {
81 struct isl_upoly_cst *cst1, *cst2;
82 cst1 = isl_upoly_as_cst(up1);
83 cst2 = isl_upoly_as_cst(up2);
86 return isl_int_eq(cst1->n, cst2->n) &&
87 isl_int_eq(cst1->d, cst2->d);
90 rec1 = isl_upoly_as_rec(up1);
91 rec2 = isl_upoly_as_rec(up2);
95 if (rec1->n != rec2->n)
98 for (i = 0; i < rec1->n; ++i) {
99 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
107 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
109 struct isl_upoly_cst *cst;
113 if (!isl_upoly_is_cst(up))
116 cst = isl_upoly_as_cst(up);
120 return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
123 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
125 struct isl_upoly_cst *cst;
129 if (!isl_upoly_is_cst(up))
132 cst = isl_upoly_as_cst(up);
136 return isl_int_sgn(cst->n);
139 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
141 struct isl_upoly_cst *cst;
145 if (!isl_upoly_is_cst(up))
148 cst = isl_upoly_as_cst(up);
152 return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
155 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
157 struct isl_upoly_cst *cst;
161 if (!isl_upoly_is_cst(up))
164 cst = isl_upoly_as_cst(up);
168 return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
171 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
173 struct isl_upoly_cst *cst;
177 if (!isl_upoly_is_cst(up))
180 cst = isl_upoly_as_cst(up);
184 return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
187 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
189 struct isl_upoly_cst *cst;
193 if (!isl_upoly_is_cst(up))
196 cst = isl_upoly_as_cst(up);
200 return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
203 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
205 struct isl_upoly_cst *cst;
209 if (!isl_upoly_is_cst(up))
212 cst = isl_upoly_as_cst(up);
216 return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
219 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
221 struct isl_upoly_cst *cst;
223 cst = isl_alloc_type(ctx, struct isl_upoly_cst);
232 isl_int_init(cst->n);
233 isl_int_init(cst->d);
238 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
240 struct isl_upoly_cst *cst;
242 cst = isl_upoly_cst_alloc(ctx);
246 isl_int_set_si(cst->n, 0);
247 isl_int_set_si(cst->d, 1);
252 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
254 struct isl_upoly_cst *cst;
256 cst = isl_upoly_cst_alloc(ctx);
260 isl_int_set_si(cst->n, 1);
261 isl_int_set_si(cst->d, 1);
266 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
268 struct isl_upoly_cst *cst;
270 cst = isl_upoly_cst_alloc(ctx);
274 isl_int_set_si(cst->n, 1);
275 isl_int_set_si(cst->d, 0);
280 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
282 struct isl_upoly_cst *cst;
284 cst = isl_upoly_cst_alloc(ctx);
288 isl_int_set_si(cst->n, -1);
289 isl_int_set_si(cst->d, 0);
294 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
296 struct isl_upoly_cst *cst;
298 cst = isl_upoly_cst_alloc(ctx);
302 isl_int_set_si(cst->n, 0);
303 isl_int_set_si(cst->d, 0);
308 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
309 isl_int n, isl_int d)
311 struct isl_upoly_cst *cst;
313 cst = isl_upoly_cst_alloc(ctx);
317 isl_int_set(cst->n, n);
318 isl_int_set(cst->d, d);
323 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
326 struct isl_upoly_rec *rec;
328 isl_assert(ctx, var >= 0, return NULL);
329 isl_assert(ctx, size >= 0, return NULL);
330 rec = isl_calloc(ctx, struct isl_upoly_rec,
331 sizeof(struct isl_upoly_rec) +
332 size * sizeof(struct isl_upoly *));
347 __isl_give isl_qpolynomial *isl_qpolynomial_reset_domain_space(
348 __isl_take isl_qpolynomial *qp, __isl_take isl_space *dim)
350 qp = isl_qpolynomial_cow(qp);
354 isl_space_free(qp->dim);
359 isl_qpolynomial_free(qp);
364 /* Reset the space of "qp". This function is called from isl_pw_templ.c
365 * and doesn't know if the space of an element object is represented
366 * directly or through its domain. It therefore passes along both.
368 __isl_give isl_qpolynomial *isl_qpolynomial_reset_space_and_domain(
369 __isl_take isl_qpolynomial *qp, __isl_take isl_space *space,
370 __isl_take isl_space *domain)
372 isl_space_free(space);
373 return isl_qpolynomial_reset_domain_space(qp, domain);
376 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
378 return qp ? qp->dim->ctx : NULL;
381 __isl_give isl_space *isl_qpolynomial_get_domain_space(
382 __isl_keep isl_qpolynomial *qp)
384 return qp ? isl_space_copy(qp->dim) : NULL;
387 __isl_give isl_space *isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
392 space = isl_space_copy(qp->dim);
393 space = isl_space_from_domain(space);
394 space = isl_space_add_dims(space, isl_dim_out, 1);
398 /* Externally, an isl_qpolynomial has a map space, but internally, the
399 * ls field corresponds to the domain of that space.
401 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
402 enum isl_dim_type type)
406 if (type == isl_dim_out)
408 if (type == isl_dim_in)
410 return isl_space_dim(qp->dim, type);
413 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
415 return qp ? isl_upoly_is_zero(qp->upoly) : -1;
418 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
420 return qp ? isl_upoly_is_one(qp->upoly) : -1;
423 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
425 return qp ? isl_upoly_is_nan(qp->upoly) : -1;
428 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
430 return qp ? isl_upoly_is_infty(qp->upoly) : -1;
433 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
435 return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
438 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
440 return qp ? isl_upoly_sgn(qp->upoly) : 0;
443 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
445 isl_int_clear(cst->n);
446 isl_int_clear(cst->d);
449 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
453 for (i = 0; i < rec->n; ++i)
454 isl_upoly_free(rec->p[i]);
457 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
466 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
468 struct isl_upoly_cst *cst;
469 struct isl_upoly_cst *dup;
471 cst = isl_upoly_as_cst(up);
475 dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
478 isl_int_set(dup->n, cst->n);
479 isl_int_set(dup->d, cst->d);
484 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
487 struct isl_upoly_rec *rec;
488 struct isl_upoly_rec *dup;
490 rec = isl_upoly_as_rec(up);
494 dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
498 for (i = 0; i < rec->n; ++i) {
499 dup->p[i] = isl_upoly_copy(rec->p[i]);
507 isl_upoly_free(&dup->up);
511 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
516 if (isl_upoly_is_cst(up))
517 return isl_upoly_dup_cst(up);
519 return isl_upoly_dup_rec(up);
522 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
530 return isl_upoly_dup(up);
533 void isl_upoly_free(__isl_take struct isl_upoly *up)
542 upoly_free_cst((struct isl_upoly_cst *)up);
544 upoly_free_rec((struct isl_upoly_rec *)up);
546 isl_ctx_deref(up->ctx);
550 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
555 isl_int_gcd(gcd, cst->n, cst->d);
556 if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
557 isl_int_divexact(cst->n, cst->n, gcd);
558 isl_int_divexact(cst->d, cst->d, gcd);
563 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
564 __isl_take struct isl_upoly *up2)
566 struct isl_upoly_cst *cst1;
567 struct isl_upoly_cst *cst2;
569 up1 = isl_upoly_cow(up1);
573 cst1 = isl_upoly_as_cst(up1);
574 cst2 = isl_upoly_as_cst(up2);
576 if (isl_int_eq(cst1->d, cst2->d))
577 isl_int_add(cst1->n, cst1->n, cst2->n);
579 isl_int_mul(cst1->n, cst1->n, cst2->d);
580 isl_int_addmul(cst1->n, cst2->n, cst1->d);
581 isl_int_mul(cst1->d, cst1->d, cst2->d);
584 isl_upoly_cst_reduce(cst1);
594 static __isl_give struct isl_upoly *replace_by_zero(
595 __isl_take struct isl_upoly *up)
603 return isl_upoly_zero(ctx);
606 static __isl_give struct isl_upoly *replace_by_constant_term(
607 __isl_take struct isl_upoly *up)
609 struct isl_upoly_rec *rec;
610 struct isl_upoly *cst;
615 rec = isl_upoly_as_rec(up);
618 cst = isl_upoly_copy(rec->p[0]);
626 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
627 __isl_take struct isl_upoly *up2)
630 struct isl_upoly_rec *rec1, *rec2;
635 if (isl_upoly_is_nan(up1)) {
640 if (isl_upoly_is_nan(up2)) {
645 if (isl_upoly_is_zero(up1)) {
650 if (isl_upoly_is_zero(up2)) {
655 if (up1->var < up2->var)
656 return isl_upoly_sum(up2, up1);
658 if (up2->var < up1->var) {
659 struct isl_upoly_rec *rec;
660 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
664 up1 = isl_upoly_cow(up1);
665 rec = isl_upoly_as_rec(up1);
668 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
670 up1 = replace_by_constant_term(up1);
674 if (isl_upoly_is_cst(up1))
675 return isl_upoly_sum_cst(up1, up2);
677 rec1 = isl_upoly_as_rec(up1);
678 rec2 = isl_upoly_as_rec(up2);
682 if (rec1->n < rec2->n)
683 return isl_upoly_sum(up2, up1);
685 up1 = isl_upoly_cow(up1);
686 rec1 = isl_upoly_as_rec(up1);
690 for (i = rec2->n - 1; i >= 0; --i) {
691 rec1->p[i] = isl_upoly_sum(rec1->p[i],
692 isl_upoly_copy(rec2->p[i]));
695 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
696 isl_upoly_free(rec1->p[i]);
702 up1 = replace_by_zero(up1);
703 else if (rec1->n == 1)
704 up1 = replace_by_constant_term(up1);
715 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
716 __isl_take struct isl_upoly *up, isl_int v)
718 struct isl_upoly_cst *cst;
720 up = isl_upoly_cow(up);
724 cst = isl_upoly_as_cst(up);
726 isl_int_addmul(cst->n, cst->d, v);
731 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
732 __isl_take struct isl_upoly *up, isl_int v)
734 struct isl_upoly_rec *rec;
739 if (isl_upoly_is_cst(up))
740 return isl_upoly_cst_add_isl_int(up, v);
742 up = isl_upoly_cow(up);
743 rec = isl_upoly_as_rec(up);
747 rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
757 __isl_give struct isl_upoly *isl_upoly_cst_mul_isl_int(
758 __isl_take struct isl_upoly *up, isl_int v)
760 struct isl_upoly_cst *cst;
762 if (isl_upoly_is_zero(up))
765 up = isl_upoly_cow(up);
769 cst = isl_upoly_as_cst(up);
771 isl_int_mul(cst->n, cst->n, v);
776 __isl_give struct isl_upoly *isl_upoly_mul_isl_int(
777 __isl_take struct isl_upoly *up, isl_int v)
780 struct isl_upoly_rec *rec;
785 if (isl_upoly_is_cst(up))
786 return isl_upoly_cst_mul_isl_int(up, v);
788 up = isl_upoly_cow(up);
789 rec = isl_upoly_as_rec(up);
793 for (i = 0; i < rec->n; ++i) {
794 rec->p[i] = isl_upoly_mul_isl_int(rec->p[i], v);
805 /* Multiply the constant polynomial "up" by "v".
807 static __isl_give struct isl_upoly *isl_upoly_cst_scale_val(
808 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
810 struct isl_upoly_cst *cst;
812 if (isl_upoly_is_zero(up))
815 up = isl_upoly_cow(up);
819 cst = isl_upoly_as_cst(up);
821 isl_int_mul(cst->n, cst->n, v->n);
822 isl_int_mul(cst->d, cst->d, v->d);
823 isl_upoly_cst_reduce(cst);
828 /* Multiply the polynomial "up" by "v".
830 static __isl_give struct isl_upoly *isl_upoly_scale_val(
831 __isl_take struct isl_upoly *up, __isl_keep isl_val *v)
834 struct isl_upoly_rec *rec;
839 if (isl_upoly_is_cst(up))
840 return isl_upoly_cst_scale_val(up, v);
842 up = isl_upoly_cow(up);
843 rec = isl_upoly_as_rec(up);
847 for (i = 0; i < rec->n; ++i) {
848 rec->p[i] = isl_upoly_scale_val(rec->p[i], v);
859 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
860 __isl_take struct isl_upoly *up2)
862 struct isl_upoly_cst *cst1;
863 struct isl_upoly_cst *cst2;
865 up1 = isl_upoly_cow(up1);
869 cst1 = isl_upoly_as_cst(up1);
870 cst2 = isl_upoly_as_cst(up2);
872 isl_int_mul(cst1->n, cst1->n, cst2->n);
873 isl_int_mul(cst1->d, cst1->d, cst2->d);
875 isl_upoly_cst_reduce(cst1);
885 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
886 __isl_take struct isl_upoly *up2)
888 struct isl_upoly_rec *rec1;
889 struct isl_upoly_rec *rec2;
890 struct isl_upoly_rec *res = NULL;
894 rec1 = isl_upoly_as_rec(up1);
895 rec2 = isl_upoly_as_rec(up2);
898 size = rec1->n + rec2->n - 1;
899 res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
903 for (i = 0; i < rec1->n; ++i) {
904 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
905 isl_upoly_copy(rec1->p[i]));
910 for (; i < size; ++i) {
911 res->p[i] = isl_upoly_zero(up1->ctx);
916 for (i = 0; i < rec1->n; ++i) {
917 for (j = 1; j < rec2->n; ++j) {
918 struct isl_upoly *up;
919 up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
920 isl_upoly_copy(rec1->p[i]));
921 res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
934 isl_upoly_free(&res->up);
938 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
939 __isl_take struct isl_upoly *up2)
944 if (isl_upoly_is_nan(up1)) {
949 if (isl_upoly_is_nan(up2)) {
954 if (isl_upoly_is_zero(up1)) {
959 if (isl_upoly_is_zero(up2)) {
964 if (isl_upoly_is_one(up1)) {
969 if (isl_upoly_is_one(up2)) {
974 if (up1->var < up2->var)
975 return isl_upoly_mul(up2, up1);
977 if (up2->var < up1->var) {
979 struct isl_upoly_rec *rec;
980 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
981 isl_ctx *ctx = up1->ctx;
984 return isl_upoly_nan(ctx);
986 up1 = isl_upoly_cow(up1);
987 rec = isl_upoly_as_rec(up1);
991 for (i = 0; i < rec->n; ++i) {
992 rec->p[i] = isl_upoly_mul(rec->p[i],
993 isl_upoly_copy(up2));
1001 if (isl_upoly_is_cst(up1))
1002 return isl_upoly_mul_cst(up1, up2);
1004 return isl_upoly_mul_rec(up1, up2);
1006 isl_upoly_free(up1);
1007 isl_upoly_free(up2);
1011 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
1014 struct isl_upoly *res;
1022 res = isl_upoly_copy(up);
1024 res = isl_upoly_one(up->ctx);
1026 while (power >>= 1) {
1027 up = isl_upoly_mul(up, isl_upoly_copy(up));
1029 res = isl_upoly_mul(res, isl_upoly_copy(up));
1036 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_space *dim,
1037 unsigned n_div, __isl_take struct isl_upoly *up)
1039 struct isl_qpolynomial *qp = NULL;
1045 if (!isl_space_is_set(dim))
1046 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1047 "domain of polynomial should be a set", goto error);
1049 total = isl_space_dim(dim, isl_dim_all);
1051 qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
1056 qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
1065 isl_space_free(dim);
1067 isl_qpolynomial_free(qp);
1071 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
1080 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
1082 struct isl_qpolynomial *dup;
1087 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1088 isl_upoly_copy(qp->upoly));
1091 isl_mat_free(dup->div);
1092 dup->div = isl_mat_copy(qp->div);
1098 isl_qpolynomial_free(dup);
1102 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1110 return isl_qpolynomial_dup(qp);
1113 void *isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1121 isl_space_free(qp->dim);
1122 isl_mat_free(qp->div);
1123 isl_upoly_free(qp->upoly);
1129 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1132 struct isl_upoly_rec *rec;
1133 struct isl_upoly_cst *cst;
1135 rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1138 for (i = 0; i < 1 + power; ++i) {
1139 rec->p[i] = isl_upoly_zero(ctx);
1144 cst = isl_upoly_as_cst(rec->p[power]);
1145 isl_int_set_si(cst->n, 1);
1149 isl_upoly_free(&rec->up);
1153 /* r array maps original positions to new positions.
1155 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1159 struct isl_upoly_rec *rec;
1160 struct isl_upoly *base;
1161 struct isl_upoly *res;
1163 if (isl_upoly_is_cst(up))
1166 rec = isl_upoly_as_rec(up);
1170 isl_assert(up->ctx, rec->n >= 1, goto error);
1172 base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1173 res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1175 for (i = rec->n - 2; i >= 0; --i) {
1176 res = isl_upoly_mul(res, isl_upoly_copy(base));
1177 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1180 isl_upoly_free(base);
1189 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1194 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1195 div1->n_col >= div2->n_col, return -1);
1197 if (div1->n_row == div2->n_row)
1198 return isl_mat_is_equal(div1, div2);
1200 n_row = div1->n_row;
1201 n_col = div1->n_col;
1202 div1->n_row = div2->n_row;
1203 div1->n_col = div2->n_col;
1205 equal = isl_mat_is_equal(div1, div2);
1207 div1->n_row = n_row;
1208 div1->n_col = n_col;
1213 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1217 li = isl_seq_last_non_zero(div->row[i], div->n_col);
1218 lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1223 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1226 struct isl_div_sort_info {
1231 static int div_sort_cmp(const void *p1, const void *p2)
1233 const struct isl_div_sort_info *i1, *i2;
1234 i1 = (const struct isl_div_sort_info *) p1;
1235 i2 = (const struct isl_div_sort_info *) p2;
1237 return cmp_row(i1->div, i1->row, i2->row);
1240 /* Sort divs and remove duplicates.
1242 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1247 struct isl_div_sort_info *array = NULL;
1248 int *pos = NULL, *at = NULL;
1249 int *reordering = NULL;
1254 if (qp->div->n_row <= 1)
1257 div_pos = isl_space_dim(qp->dim, isl_dim_all);
1259 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1261 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1262 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1263 len = qp->div->n_col - 2;
1264 reordering = isl_alloc_array(qp->div->ctx, int, len);
1265 if (!array || !pos || !at || !reordering)
1268 for (i = 0; i < qp->div->n_row; ++i) {
1269 array[i].div = qp->div;
1275 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1278 for (i = 0; i < div_pos; ++i)
1281 for (i = 0; i < qp->div->n_row; ++i) {
1282 if (pos[array[i].row] == i)
1284 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1285 pos[at[i]] = pos[array[i].row];
1286 at[pos[array[i].row]] = at[i];
1287 at[i] = array[i].row;
1288 pos[array[i].row] = i;
1292 for (i = 0; i < len - div_pos; ++i) {
1294 isl_seq_eq(qp->div->row[i - skip - 1],
1295 qp->div->row[i - skip], qp->div->n_col)) {
1296 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1297 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1298 2 + div_pos + i - skip);
1299 qp->div = isl_mat_drop_cols(qp->div,
1300 2 + div_pos + i - skip, 1);
1303 reordering[div_pos + array[i].row] = div_pos + i - skip;
1306 qp->upoly = reorder(qp->upoly, reordering);
1308 if (!qp->upoly || !qp->div)
1322 isl_qpolynomial_free(qp);
1326 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1327 int *exp, int first)
1330 struct isl_upoly_rec *rec;
1332 if (isl_upoly_is_cst(up))
1335 if (up->var < first)
1338 if (exp[up->var - first] == up->var - first)
1341 up = isl_upoly_cow(up);
1345 up->var = exp[up->var - first] + first;
1347 rec = isl_upoly_as_rec(up);
1351 for (i = 0; i < rec->n; ++i) {
1352 rec->p[i] = expand(rec->p[i], exp, first);
1363 static __isl_give isl_qpolynomial *with_merged_divs(
1364 __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1365 __isl_take isl_qpolynomial *qp2),
1366 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1370 isl_mat *div = NULL;
1372 qp1 = isl_qpolynomial_cow(qp1);
1373 qp2 = isl_qpolynomial_cow(qp2);
1378 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1379 qp1->div->n_col >= qp2->div->n_col, goto error);
1381 exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1382 exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1386 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1390 isl_mat_free(qp1->div);
1391 qp1->div = isl_mat_copy(div);
1392 isl_mat_free(qp2->div);
1393 qp2->div = isl_mat_copy(div);
1395 qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1396 qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1398 if (!qp1->upoly || !qp2->upoly)
1405 return fn(qp1, qp2);
1410 isl_qpolynomial_free(qp1);
1411 isl_qpolynomial_free(qp2);
1415 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1416 __isl_take isl_qpolynomial *qp2)
1418 qp1 = isl_qpolynomial_cow(qp1);
1423 if (qp1->div->n_row < qp2->div->n_row)
1424 return isl_qpolynomial_add(qp2, qp1);
1426 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1427 if (!compatible_divs(qp1->div, qp2->div))
1428 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1430 qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1434 isl_qpolynomial_free(qp2);
1438 isl_qpolynomial_free(qp1);
1439 isl_qpolynomial_free(qp2);
1443 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1444 __isl_keep isl_set *dom,
1445 __isl_take isl_qpolynomial *qp1,
1446 __isl_take isl_qpolynomial *qp2)
1448 qp1 = isl_qpolynomial_add(qp1, qp2);
1449 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1453 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1454 __isl_take isl_qpolynomial *qp2)
1456 return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1459 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1460 __isl_take isl_qpolynomial *qp, isl_int v)
1462 if (isl_int_is_zero(v))
1465 qp = isl_qpolynomial_cow(qp);
1469 qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1475 isl_qpolynomial_free(qp);
1480 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1485 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1488 __isl_give isl_qpolynomial *isl_qpolynomial_mul_isl_int(
1489 __isl_take isl_qpolynomial *qp, isl_int v)
1491 if (isl_int_is_one(v))
1494 if (qp && isl_int_is_zero(v)) {
1495 isl_qpolynomial *zero;
1496 zero = isl_qpolynomial_zero_on_domain(isl_space_copy(qp->dim));
1497 isl_qpolynomial_free(qp);
1501 qp = isl_qpolynomial_cow(qp);
1505 qp->upoly = isl_upoly_mul_isl_int(qp->upoly, v);
1511 isl_qpolynomial_free(qp);
1515 __isl_give isl_qpolynomial *isl_qpolynomial_scale(
1516 __isl_take isl_qpolynomial *qp, isl_int v)
1518 return isl_qpolynomial_mul_isl_int(qp, v);
1521 /* Multiply "qp" by "v".
1523 __isl_give isl_qpolynomial *isl_qpolynomial_scale_val(
1524 __isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
1529 if (!isl_val_is_rat(v))
1530 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
1531 "expecting rational factor", goto error);
1533 if (isl_val_is_one(v)) {
1538 if (isl_val_is_zero(v)) {
1541 space = isl_qpolynomial_get_domain_space(qp);
1542 isl_qpolynomial_free(qp);
1544 return isl_qpolynomial_zero_on_domain(space);
1547 qp = isl_qpolynomial_cow(qp);
1551 qp->upoly = isl_upoly_scale_val(qp->upoly, v);
1553 qp = isl_qpolynomial_free(qp);
1559 isl_qpolynomial_free(qp);
1563 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1564 __isl_take isl_qpolynomial *qp2)
1566 qp1 = isl_qpolynomial_cow(qp1);
1571 if (qp1->div->n_row < qp2->div->n_row)
1572 return isl_qpolynomial_mul(qp2, qp1);
1574 isl_assert(qp1->dim->ctx, isl_space_is_equal(qp1->dim, qp2->dim), goto error);
1575 if (!compatible_divs(qp1->div, qp2->div))
1576 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1578 qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1582 isl_qpolynomial_free(qp2);
1586 isl_qpolynomial_free(qp1);
1587 isl_qpolynomial_free(qp2);
1591 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1594 qp = isl_qpolynomial_cow(qp);
1599 qp->upoly = isl_upoly_pow(qp->upoly, power);
1605 isl_qpolynomial_free(qp);
1609 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_pow(
1610 __isl_take isl_pw_qpolynomial *pwqp, unsigned power)
1617 pwqp = isl_pw_qpolynomial_cow(pwqp);
1621 for (i = 0; i < pwqp->n; ++i) {
1622 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
1624 return isl_pw_qpolynomial_free(pwqp);
1630 __isl_give isl_qpolynomial *isl_qpolynomial_zero_on_domain(
1631 __isl_take isl_space *dim)
1635 return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1638 __isl_give isl_qpolynomial *isl_qpolynomial_one_on_domain(
1639 __isl_take isl_space *dim)
1643 return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1646 __isl_give isl_qpolynomial *isl_qpolynomial_infty_on_domain(
1647 __isl_take isl_space *dim)
1651 return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1654 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty_on_domain(
1655 __isl_take isl_space *dim)
1659 return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1662 __isl_give isl_qpolynomial *isl_qpolynomial_nan_on_domain(
1663 __isl_take isl_space *dim)
1667 return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1670 __isl_give isl_qpolynomial *isl_qpolynomial_cst_on_domain(
1671 __isl_take isl_space *dim,
1674 struct isl_qpolynomial *qp;
1675 struct isl_upoly_cst *cst;
1680 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1684 cst = isl_upoly_as_cst(qp->upoly);
1685 isl_int_set(cst->n, v);
1690 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1691 isl_int *n, isl_int *d)
1693 struct isl_upoly_cst *cst;
1698 if (!isl_upoly_is_cst(qp->upoly))
1701 cst = isl_upoly_as_cst(qp->upoly);
1706 isl_int_set(*n, cst->n);
1708 isl_int_set(*d, cst->d);
1713 /* Return the constant term of "up".
1715 static __isl_give isl_val *isl_upoly_get_constant_val(
1716 __isl_keep struct isl_upoly *up)
1718 struct isl_upoly_cst *cst;
1723 while (!isl_upoly_is_cst(up)) {
1724 struct isl_upoly_rec *rec;
1726 rec = isl_upoly_as_rec(up);
1732 cst = isl_upoly_as_cst(up);
1735 return isl_val_rat_from_isl_int(cst->up.ctx, cst->n, cst->d);
1738 /* Return the constant term of "qp".
1740 __isl_give isl_val *isl_qpolynomial_get_constant_val(
1741 __isl_keep isl_qpolynomial *qp)
1746 return isl_upoly_get_constant_val(qp->upoly);
1749 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1752 struct isl_upoly_rec *rec;
1760 rec = isl_upoly_as_rec(up);
1767 isl_assert(up->ctx, rec->n > 1, return -1);
1769 is_cst = isl_upoly_is_cst(rec->p[1]);
1775 return isl_upoly_is_affine(rec->p[0]);
1778 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1783 if (qp->div->n_row > 0)
1786 return isl_upoly_is_affine(qp->upoly);
1789 static void update_coeff(__isl_keep isl_vec *aff,
1790 __isl_keep struct isl_upoly_cst *cst, int pos)
1795 if (isl_int_is_zero(cst->n))
1800 isl_int_gcd(gcd, cst->d, aff->el[0]);
1801 isl_int_divexact(f, cst->d, gcd);
1802 isl_int_divexact(gcd, aff->el[0], gcd);
1803 isl_seq_scale(aff->el, aff->el, f, aff->size);
1804 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1809 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1810 __isl_keep isl_vec *aff)
1812 struct isl_upoly_cst *cst;
1813 struct isl_upoly_rec *rec;
1819 struct isl_upoly_cst *cst;
1821 cst = isl_upoly_as_cst(up);
1824 update_coeff(aff, cst, 0);
1828 rec = isl_upoly_as_rec(up);
1831 isl_assert(up->ctx, rec->n == 2, return -1);
1833 cst = isl_upoly_as_cst(rec->p[1]);
1836 update_coeff(aff, cst, 1 + up->var);
1838 return isl_upoly_update_affine(rec->p[0], aff);
1841 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1842 __isl_keep isl_qpolynomial *qp)
1850 d = isl_space_dim(qp->dim, isl_dim_all);
1851 aff = isl_vec_alloc(qp->div->ctx, 2 + d + qp->div->n_row);
1855 isl_seq_clr(aff->el + 1, 1 + d + qp->div->n_row);
1856 isl_int_set_si(aff->el[0], 1);
1858 if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1867 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1,
1868 __isl_keep isl_qpolynomial *qp2)
1875 equal = isl_space_is_equal(qp1->dim, qp2->dim);
1876 if (equal < 0 || !equal)
1879 equal = isl_mat_is_equal(qp1->div, qp2->div);
1880 if (equal < 0 || !equal)
1883 return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1886 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1889 struct isl_upoly_rec *rec;
1891 if (isl_upoly_is_cst(up)) {
1892 struct isl_upoly_cst *cst;
1893 cst = isl_upoly_as_cst(up);
1896 isl_int_lcm(*d, *d, cst->d);
1900 rec = isl_upoly_as_rec(up);
1904 for (i = 0; i < rec->n; ++i)
1905 upoly_update_den(rec->p[i], d);
1908 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1910 isl_int_set_si(*d, 1);
1913 upoly_update_den(qp->upoly, d);
1916 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow_on_domain(
1917 __isl_take isl_space *dim, int pos, int power)
1919 struct isl_ctx *ctx;
1926 return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1929 __isl_give isl_qpolynomial *isl_qpolynomial_var_on_domain(__isl_take isl_space *dim,
1930 enum isl_dim_type type, unsigned pos)
1935 isl_assert(dim->ctx, isl_space_dim(dim, isl_dim_in) == 0, goto error);
1936 isl_assert(dim->ctx, pos < isl_space_dim(dim, type), goto error);
1938 if (type == isl_dim_set)
1939 pos += isl_space_dim(dim, isl_dim_param);
1941 return isl_qpolynomial_var_pow_on_domain(dim, pos, 1);
1943 isl_space_free(dim);
1947 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1948 unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1951 struct isl_upoly_rec *rec;
1952 struct isl_upoly *base, *res;
1957 if (isl_upoly_is_cst(up))
1960 if (up->var < first)
1963 rec = isl_upoly_as_rec(up);
1967 isl_assert(up->ctx, rec->n >= 1, goto error);
1969 if (up->var >= first + n)
1970 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1972 base = isl_upoly_copy(subs[up->var - first]);
1974 res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1975 for (i = rec->n - 2; i >= 0; --i) {
1976 struct isl_upoly *t;
1977 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1978 res = isl_upoly_mul(res, isl_upoly_copy(base));
1979 res = isl_upoly_sum(res, t);
1982 isl_upoly_free(base);
1991 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1992 isl_int denom, unsigned len)
1995 struct isl_upoly *up;
1997 isl_assert(ctx, len >= 1, return NULL);
1999 up = isl_upoly_rat_cst(ctx, f[0], denom);
2000 for (i = 0; i < len - 1; ++i) {
2001 struct isl_upoly *t;
2002 struct isl_upoly *c;
2004 if (isl_int_is_zero(f[1 + i]))
2007 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
2008 t = isl_upoly_var_pow(ctx, i, 1);
2009 t = isl_upoly_mul(c, t);
2010 up = isl_upoly_sum(up, t);
2016 /* Remove common factor of non-constant terms and denominator.
2018 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
2020 isl_ctx *ctx = qp->div->ctx;
2021 unsigned total = qp->div->n_col - 2;
2023 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2024 isl_int_gcd(ctx->normalize_gcd,
2025 ctx->normalize_gcd, qp->div->row[div][0]);
2026 if (isl_int_is_one(ctx->normalize_gcd))
2029 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2030 ctx->normalize_gcd, total);
2031 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2032 ctx->normalize_gcd);
2033 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2034 ctx->normalize_gcd);
2037 /* Replace the integer division identified by "div" by the polynomial "s".
2038 * The integer division is assumed not to appear in the definition
2039 * of any other integer divisions.
2041 static __isl_give isl_qpolynomial *substitute_div(
2042 __isl_take isl_qpolynomial *qp,
2043 int div, __isl_take struct isl_upoly *s)
2052 qp = isl_qpolynomial_cow(qp);
2056 total = isl_space_dim(qp->dim, isl_dim_all);
2057 qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
2061 reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
2064 for (i = 0; i < total + div; ++i)
2066 for (i = total + div + 1; i < total + qp->div->n_row; ++i)
2067 reordering[i] = i - 1;
2068 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2069 qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
2070 qp->upoly = reorder(qp->upoly, reordering);
2073 if (!qp->upoly || !qp->div)
2079 isl_qpolynomial_free(qp);
2084 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2085 * divisions because d is equal to 1 by their definition, i.e., e.
2087 static __isl_give isl_qpolynomial *substitute_non_divs(
2088 __isl_take isl_qpolynomial *qp)
2092 struct isl_upoly *s;
2097 total = isl_space_dim(qp->dim, isl_dim_all);
2098 for (i = 0; qp && i < qp->div->n_row; ++i) {
2099 if (!isl_int_is_one(qp->div->row[i][0]))
2101 for (j = i + 1; j < qp->div->n_row; ++j) {
2102 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
2104 isl_seq_combine(qp->div->row[j] + 1,
2105 qp->div->ctx->one, qp->div->row[j] + 1,
2106 qp->div->row[j][2 + total + i],
2107 qp->div->row[i] + 1, 1 + total + i);
2108 isl_int_set_si(qp->div->row[j][2 + total + i], 0);
2109 normalize_div(qp, j);
2111 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2112 qp->div->row[i][0], qp->div->n_col - 1);
2113 qp = substitute_div(qp, i, s);
2120 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2121 * with d the denominator. When replacing the coefficient e of x by
2122 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2123 * inside the division, so we need to add floor(e/d) * x outside.
2124 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2125 * to adjust the coefficient of x in each later div that depends on the
2126 * current div "div" and also in the affine expression "aff"
2127 * (if it too depends on "div").
2129 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2130 __isl_keep isl_vec *aff)
2134 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2137 for (i = 0; i < 1 + total + div; ++i) {
2138 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2139 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2141 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2142 isl_int_fdiv_r(qp->div->row[div][1 + i],
2143 qp->div->row[div][1 + i], qp->div->row[div][0]);
2144 if (!isl_int_is_zero(aff->el[1 + total + div]))
2145 isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
2146 for (j = div + 1; j < qp->div->n_row; ++j) {
2147 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2149 isl_int_addmul(qp->div->row[j][1 + i],
2150 v, qp->div->row[j][2 + total + div]);
2156 /* Check if the last non-zero coefficient is bigger that half of the
2157 * denominator. If so, we will invert the div to further reduce the number
2158 * of distinct divs that may appear.
2159 * If the last non-zero coefficient is exactly half the denominator,
2160 * then we continue looking for earlier coefficients that are bigger
2161 * than half the denominator.
2163 static int needs_invert(__isl_keep isl_mat *div, int row)
2168 for (i = div->n_col - 1; i >= 1; --i) {
2169 if (isl_int_is_zero(div->row[row][i]))
2171 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2172 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2173 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2183 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2184 * We only invert the coefficients of e (and the coefficient of q in
2185 * later divs and in "aff"). After calling this function, the
2186 * coefficients of e should be reduced again.
2188 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2189 __isl_keep isl_vec *aff)
2191 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2193 isl_seq_neg(qp->div->row[div] + 1,
2194 qp->div->row[div] + 1, qp->div->n_col - 1);
2195 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2196 isl_int_add(qp->div->row[div][1],
2197 qp->div->row[div][1], qp->div->row[div][0]);
2198 if (!isl_int_is_zero(aff->el[1 + total + div]))
2199 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2200 isl_mat_col_mul(qp->div, 2 + total + div,
2201 qp->div->ctx->negone, 2 + total + div);
2204 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2205 * in the interval [0, d-1], with d the denominator and such that the
2206 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2208 * After the reduction, some divs may have become redundant or identical,
2209 * so we call substitute_non_divs and sort_divs. If these functions
2210 * eliminate divs or merge two or more divs into one, the coefficients
2211 * of the enclosing divs may have to be reduced again, so we call
2212 * ourselves recursively if the number of divs decreases.
2214 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2217 isl_vec *aff = NULL;
2218 struct isl_upoly *s;
2224 aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2225 aff = isl_vec_clr(aff);
2229 isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2231 for (i = 0; i < qp->div->n_row; ++i) {
2232 normalize_div(qp, i);
2233 reduce_div(qp, i, aff);
2234 if (needs_invert(qp->div, i)) {
2235 invert_div(qp, i, aff);
2236 reduce_div(qp, i, aff);
2240 s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2241 qp->div->ctx->one, aff->size);
2242 qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2249 n_div = qp->div->n_row;
2250 qp = substitute_non_divs(qp);
2252 if (qp && qp->div->n_row < n_div)
2253 return reduce_divs(qp);
2257 isl_qpolynomial_free(qp);
2262 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst_on_domain(
2263 __isl_take isl_space *dim, const isl_int n, const isl_int d)
2265 struct isl_qpolynomial *qp;
2266 struct isl_upoly_cst *cst;
2271 qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2275 cst = isl_upoly_as_cst(qp->upoly);
2276 isl_int_set(cst->n, n);
2277 isl_int_set(cst->d, d);
2282 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2284 __isl_give isl_qpolynomial *isl_qpolynomial_val_on_domain(
2285 __isl_take isl_space *domain, __isl_take isl_val *val)
2287 isl_qpolynomial *qp;
2288 struct isl_upoly_cst *cst;
2290 if (!domain || !val)
2293 qp = isl_qpolynomial_alloc(domain, 0, isl_upoly_zero(domain->ctx));
2297 cst = isl_upoly_as_cst(qp->upoly);
2298 isl_int_set(cst->n, val->n);
2299 isl_int_set(cst->d, val->d);
2304 isl_space_free(domain);
2309 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2311 struct isl_upoly_rec *rec;
2317 if (isl_upoly_is_cst(up))
2321 active[up->var] = 1;
2323 rec = isl_upoly_as_rec(up);
2324 for (i = 0; i < rec->n; ++i)
2325 if (up_set_active(rec->p[i], active, d) < 0)
2331 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2334 int d = isl_space_dim(qp->dim, isl_dim_all);
2339 for (i = 0; i < d; ++i)
2340 for (j = 0; j < qp->div->n_row; ++j) {
2341 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2347 return up_set_active(qp->upoly, active, d);
2350 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2351 enum isl_dim_type type, unsigned first, unsigned n)
2362 isl_assert(qp->dim->ctx,
2363 first + n <= isl_qpolynomial_dim(qp, type), return -1);
2364 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2365 type == isl_dim_in, return -1);
2367 active = isl_calloc_array(qp->dim->ctx, int,
2368 isl_space_dim(qp->dim, isl_dim_all));
2369 if (set_active(qp, active) < 0)
2372 if (type == isl_dim_in)
2373 first += isl_space_dim(qp->dim, isl_dim_param);
2374 for (i = 0; i < n; ++i)
2375 if (active[first + i]) {
2388 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2389 * of the divs that do appear in the quasi-polynomial.
2391 static __isl_give isl_qpolynomial *remove_redundant_divs(
2392 __isl_take isl_qpolynomial *qp)
2399 int *reordering = NULL;
2406 if (qp->div->n_row == 0)
2409 d = isl_space_dim(qp->dim, isl_dim_all);
2410 len = qp->div->n_col - 2;
2411 ctx = isl_qpolynomial_get_ctx(qp);
2412 active = isl_calloc_array(ctx, int, len);
2416 if (up_set_active(qp->upoly, active, len) < 0)
2419 for (i = qp->div->n_row - 1; i >= 0; --i) {
2420 if (!active[d + i]) {
2424 for (j = 0; j < i; ++j) {
2425 if (isl_int_is_zero(qp->div->row[i][2 + d + j]))
2437 reordering = isl_alloc_array(qp->div->ctx, int, len);
2441 for (i = 0; i < d; ++i)
2445 n_div = qp->div->n_row;
2446 for (i = 0; i < n_div; ++i) {
2447 if (!active[d + i]) {
2448 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2449 qp->div = isl_mat_drop_cols(qp->div,
2450 2 + d + i - skip, 1);
2453 reordering[d + i] = d + i - skip;
2456 qp->upoly = reorder(qp->upoly, reordering);
2458 if (!qp->upoly || !qp->div)
2468 isl_qpolynomial_free(qp);
2472 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2473 unsigned first, unsigned n)
2476 struct isl_upoly_rec *rec;
2480 if (n == 0 || up->var < 0 || up->var < first)
2482 if (up->var < first + n) {
2483 up = replace_by_constant_term(up);
2484 return isl_upoly_drop(up, first, n);
2486 up = isl_upoly_cow(up);
2490 rec = isl_upoly_as_rec(up);
2494 for (i = 0; i < rec->n; ++i) {
2495 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2506 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2507 __isl_take isl_qpolynomial *qp,
2508 enum isl_dim_type type, unsigned pos, const char *s)
2510 qp = isl_qpolynomial_cow(qp);
2513 qp->dim = isl_space_set_dim_name(qp->dim, type, pos, s);
2518 isl_qpolynomial_free(qp);
2522 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2523 __isl_take isl_qpolynomial *qp,
2524 enum isl_dim_type type, unsigned first, unsigned n)
2528 if (type == isl_dim_out)
2529 isl_die(qp->dim->ctx, isl_error_invalid,
2530 "cannot drop output/set dimension",
2532 if (type == isl_dim_in)
2534 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
2537 qp = isl_qpolynomial_cow(qp);
2541 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
2543 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2544 type == isl_dim_set, goto error);
2546 qp->dim = isl_space_drop_dims(qp->dim, type, first, n);
2550 if (type == isl_dim_set)
2551 first += isl_space_dim(qp->dim, isl_dim_param);
2553 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2557 qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2563 isl_qpolynomial_free(qp);
2567 /* Project the domain of the quasi-polynomial onto its parameter space.
2568 * The quasi-polynomial may not involve any of the domain dimensions.
2570 __isl_give isl_qpolynomial *isl_qpolynomial_project_domain_on_params(
2571 __isl_take isl_qpolynomial *qp)
2577 n = isl_qpolynomial_dim(qp, isl_dim_in);
2578 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
2580 return isl_qpolynomial_free(qp);
2582 isl_die(isl_qpolynomial_get_ctx(qp), isl_error_invalid,
2583 "polynomial involves some of the domain dimensions",
2584 return isl_qpolynomial_free(qp));
2585 qp = isl_qpolynomial_drop_dims(qp, isl_dim_in, 0, n);
2586 space = isl_qpolynomial_get_domain_space(qp);
2587 space = isl_space_params(space);
2588 qp = isl_qpolynomial_reset_domain_space(qp, space);
2592 static __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities_lifted(
2593 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2599 struct isl_upoly *up;
2603 if (eq->n_eq == 0) {
2604 isl_basic_set_free(eq);
2608 qp = isl_qpolynomial_cow(qp);
2611 qp->div = isl_mat_cow(qp->div);
2615 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2617 isl_int_init(denom);
2618 for (i = 0; i < eq->n_eq; ++i) {
2619 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2620 if (j < 0 || j == 0 || j >= total)
2623 for (k = 0; k < qp->div->n_row; ++k) {
2624 if (isl_int_is_zero(qp->div->row[k][1 + j]))
2626 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2627 &qp->div->row[k][0]);
2628 normalize_div(qp, k);
2631 if (isl_int_is_pos(eq->eq[i][j]))
2632 isl_seq_neg(eq->eq[i], eq->eq[i], total);
2633 isl_int_abs(denom, eq->eq[i][j]);
2634 isl_int_set_si(eq->eq[i][j], 0);
2636 up = isl_upoly_from_affine(qp->dim->ctx,
2637 eq->eq[i], denom, total);
2638 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2641 isl_int_clear(denom);
2646 isl_basic_set_free(eq);
2648 qp = substitute_non_divs(qp);
2653 isl_basic_set_free(eq);
2654 isl_qpolynomial_free(qp);
2658 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2660 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2661 __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2665 if (qp->div->n_row > 0)
2666 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
2667 return isl_qpolynomial_substitute_equalities_lifted(qp, eq);
2669 isl_basic_set_free(eq);
2670 isl_qpolynomial_free(qp);
2674 static __isl_give isl_basic_set *add_div_constraints(
2675 __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2683 bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2686 total = isl_basic_set_total_dim(bset);
2687 for (i = 0; i < div->n_row; ++i)
2688 if (isl_basic_set_add_div_constraints_var(bset,
2689 total - div->n_row + i, div->row[i]) < 0)
2696 isl_basic_set_free(bset);
2700 /* Look for equalities among the variables shared by context and qp
2701 * and the integer divisions of qp, if any.
2702 * The equalities are then used to eliminate variables and/or integer
2703 * divisions from qp.
2705 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2706 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2712 if (qp->div->n_row > 0) {
2713 isl_basic_set *bset;
2714 context = isl_set_add_dims(context, isl_dim_set,
2716 bset = isl_basic_set_universe(isl_set_get_space(context));
2717 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2718 context = isl_set_intersect(context,
2719 isl_set_from_basic_set(bset));
2722 aff = isl_set_affine_hull(context);
2723 return isl_qpolynomial_substitute_equalities_lifted(qp, aff);
2725 isl_qpolynomial_free(qp);
2726 isl_set_free(context);
2730 __isl_give isl_qpolynomial *isl_qpolynomial_gist_params(
2731 __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2733 isl_space *space = isl_qpolynomial_get_domain_space(qp);
2734 isl_set *dom_context = isl_set_universe(space);
2735 dom_context = isl_set_intersect_params(dom_context, context);
2736 return isl_qpolynomial_gist(qp, dom_context);
2739 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_qpolynomial(
2740 __isl_take isl_qpolynomial *qp)
2746 if (isl_qpolynomial_is_zero(qp)) {
2747 isl_space *dim = isl_qpolynomial_get_space(qp);
2748 isl_qpolynomial_free(qp);
2749 return isl_pw_qpolynomial_zero(dim);
2752 dom = isl_set_universe(isl_qpolynomial_get_domain_space(qp));
2753 return isl_pw_qpolynomial_alloc(dom, qp);
2757 #define PW isl_pw_qpolynomial
2759 #define EL isl_qpolynomial
2761 #define EL_IS_ZERO is_zero
2765 #define IS_ZERO is_zero
2768 #undef DEFAULT_IS_ZERO
2769 #define DEFAULT_IS_ZERO 1
2773 #include <isl_pw_templ.c>
2776 #define UNION isl_union_pw_qpolynomial
2778 #define PART isl_pw_qpolynomial
2780 #define PARTS pw_qpolynomial
2781 #define ALIGN_DOMAIN
2783 #include <isl_union_templ.c>
2785 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2793 if (!isl_set_plain_is_universe(pwqp->p[0].set))
2796 return isl_qpolynomial_is_one(pwqp->p[0].qp);
2799 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add(
2800 __isl_take isl_pw_qpolynomial *pwqp1,
2801 __isl_take isl_pw_qpolynomial *pwqp2)
2803 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
2806 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2807 __isl_take isl_pw_qpolynomial *pwqp1,
2808 __isl_take isl_pw_qpolynomial *pwqp2)
2811 struct isl_pw_qpolynomial *res;
2813 if (!pwqp1 || !pwqp2)
2816 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
2819 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2820 isl_pw_qpolynomial_free(pwqp2);
2824 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2825 isl_pw_qpolynomial_free(pwqp1);
2829 if (isl_pw_qpolynomial_is_one(pwqp1)) {
2830 isl_pw_qpolynomial_free(pwqp1);
2834 if (isl_pw_qpolynomial_is_one(pwqp2)) {
2835 isl_pw_qpolynomial_free(pwqp2);
2839 n = pwqp1->n * pwqp2->n;
2840 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
2842 for (i = 0; i < pwqp1->n; ++i) {
2843 for (j = 0; j < pwqp2->n; ++j) {
2844 struct isl_set *common;
2845 struct isl_qpolynomial *prod;
2846 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2847 isl_set_copy(pwqp2->p[j].set));
2848 if (isl_set_plain_is_empty(common)) {
2849 isl_set_free(common);
2853 prod = isl_qpolynomial_mul(
2854 isl_qpolynomial_copy(pwqp1->p[i].qp),
2855 isl_qpolynomial_copy(pwqp2->p[j].qp));
2857 res = isl_pw_qpolynomial_add_piece(res, common, prod);
2861 isl_pw_qpolynomial_free(pwqp1);
2862 isl_pw_qpolynomial_free(pwqp2);
2866 isl_pw_qpolynomial_free(pwqp1);
2867 isl_pw_qpolynomial_free(pwqp2);
2871 __isl_give struct isl_upoly *isl_upoly_eval(
2872 __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2875 struct isl_upoly_rec *rec;
2876 struct isl_upoly *res;
2877 struct isl_upoly *base;
2879 if (isl_upoly_is_cst(up)) {
2884 rec = isl_upoly_as_rec(up);
2888 isl_assert(up->ctx, rec->n >= 1, goto error);
2890 base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2892 res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2895 for (i = rec->n - 2; i >= 0; --i) {
2896 res = isl_upoly_mul(res, isl_upoly_copy(base));
2897 res = isl_upoly_sum(res,
2898 isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2899 isl_vec_copy(vec)));
2902 isl_upoly_free(base);
2912 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2913 __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2916 struct isl_upoly *up;
2921 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
2923 if (qp->div->n_row == 0)
2924 ext = isl_vec_copy(pnt->vec);
2927 unsigned dim = isl_space_dim(qp->dim, isl_dim_all);
2928 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2932 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2933 for (i = 0; i < qp->div->n_row; ++i) {
2934 isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2935 1 + dim + i, &ext->el[1+dim+i]);
2936 isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2937 qp->div->row[i][0]);
2941 up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2945 dim = isl_space_copy(qp->dim);
2946 isl_qpolynomial_free(qp);
2947 isl_point_free(pnt);
2949 return isl_qpolynomial_alloc(dim, 0, up);
2951 isl_qpolynomial_free(qp);
2952 isl_point_free(pnt);
2956 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2957 __isl_keep struct isl_upoly_cst *cst2)
2962 isl_int_mul(t, cst1->n, cst2->d);
2963 isl_int_submul(t, cst2->n, cst1->d);
2964 cmp = isl_int_sgn(t);
2969 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2970 __isl_keep isl_qpolynomial *qp2)
2972 struct isl_upoly_cst *cst1, *cst2;
2976 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2977 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2978 if (isl_qpolynomial_is_nan(qp1))
2980 if (isl_qpolynomial_is_nan(qp2))
2982 cst1 = isl_upoly_as_cst(qp1->upoly);
2983 cst2 = isl_upoly_as_cst(qp2->upoly);
2985 return isl_upoly_cmp(cst1, cst2) <= 0;
2988 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2989 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2991 struct isl_upoly_cst *cst1, *cst2;
2996 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2997 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2998 cst1 = isl_upoly_as_cst(qp1->upoly);
2999 cst2 = isl_upoly_as_cst(qp2->upoly);
3000 cmp = isl_upoly_cmp(cst1, cst2);
3003 isl_qpolynomial_free(qp2);
3005 isl_qpolynomial_free(qp1);
3010 isl_qpolynomial_free(qp1);
3011 isl_qpolynomial_free(qp2);
3015 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
3016 __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
3018 struct isl_upoly_cst *cst1, *cst2;
3023 isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
3024 isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
3025 cst1 = isl_upoly_as_cst(qp1->upoly);
3026 cst2 = isl_upoly_as_cst(qp2->upoly);
3027 cmp = isl_upoly_cmp(cst1, cst2);
3030 isl_qpolynomial_free(qp2);
3032 isl_qpolynomial_free(qp1);
3037 isl_qpolynomial_free(qp1);
3038 isl_qpolynomial_free(qp2);
3042 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
3043 __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
3044 unsigned first, unsigned n)
3052 if (type == isl_dim_out)
3053 isl_die(qp->div->ctx, isl_error_invalid,
3054 "cannot insert output/set dimensions",
3056 if (type == isl_dim_in)
3058 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3061 qp = isl_qpolynomial_cow(qp);
3065 isl_assert(qp->div->ctx, first <= isl_space_dim(qp->dim, type),
3068 g_pos = pos(qp->dim, type) + first;
3070 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3074 total = qp->div->n_col - 2;
3075 if (total > g_pos) {
3077 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3080 for (i = 0; i < total - g_pos; ++i)
3082 qp->upoly = expand(qp->upoly, exp, g_pos);
3088 qp->dim = isl_space_insert_dims(qp->dim, type, first, n);
3094 isl_qpolynomial_free(qp);
3098 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
3099 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3103 pos = isl_qpolynomial_dim(qp, type);
3105 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3108 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
3109 __isl_take isl_pw_qpolynomial *pwqp,
3110 enum isl_dim_type type, unsigned n)
3114 pos = isl_pw_qpolynomial_dim(pwqp, type);
3116 return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
3119 static int *reordering_move(isl_ctx *ctx,
3120 unsigned len, unsigned dst, unsigned src, unsigned n)
3125 reordering = isl_alloc_array(ctx, int, len);
3130 for (i = 0; i < dst; ++i)
3132 for (i = 0; i < n; ++i)
3133 reordering[src + i] = dst + i;
3134 for (i = 0; i < src - dst; ++i)
3135 reordering[dst + i] = dst + n + i;
3136 for (i = 0; i < len - src - n; ++i)
3137 reordering[src + n + i] = src + n + i;
3139 for (i = 0; i < src; ++i)
3141 for (i = 0; i < n; ++i)
3142 reordering[src + i] = dst + i;
3143 for (i = 0; i < dst - src; ++i)
3144 reordering[src + n + i] = src + i;
3145 for (i = 0; i < len - dst - n; ++i)
3146 reordering[dst + n + i] = dst + n + i;
3152 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
3153 __isl_take isl_qpolynomial *qp,
3154 enum isl_dim_type dst_type, unsigned dst_pos,
3155 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3161 qp = isl_qpolynomial_cow(qp);
3165 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3166 isl_die(qp->dim->ctx, isl_error_invalid,
3167 "cannot move output/set dimension",
3169 if (dst_type == isl_dim_in)
3170 dst_type = isl_dim_set;
3171 if (src_type == isl_dim_in)
3172 src_type = isl_dim_set;
3174 isl_assert(qp->dim->ctx, src_pos + n <= isl_space_dim(qp->dim, src_type),
3177 g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
3178 g_src_pos = pos(qp->dim, src_type) + src_pos;
3179 if (dst_type > src_type)
3182 qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
3189 reordering = reordering_move(qp->dim->ctx,
3190 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
3194 qp->upoly = reorder(qp->upoly, reordering);
3199 qp->dim = isl_space_move_dims(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
3205 isl_qpolynomial_free(qp);
3209 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_space *dim,
3210 isl_int *f, isl_int denom)
3212 struct isl_upoly *up;
3214 dim = isl_space_domain(dim);
3218 up = isl_upoly_from_affine(dim->ctx, f, denom,
3219 1 + isl_space_dim(dim, isl_dim_all));
3221 return isl_qpolynomial_alloc(dim, 0, up);
3224 __isl_give isl_qpolynomial *isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
3227 struct isl_upoly *up;
3228 isl_qpolynomial *qp;
3233 ctx = isl_aff_get_ctx(aff);
3234 up = isl_upoly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3237 qp = isl_qpolynomial_alloc(isl_aff_get_domain_space(aff),
3238 aff->ls->div->n_row, up);
3242 isl_mat_free(qp->div);
3243 qp->div = isl_mat_copy(aff->ls->div);
3244 qp->div = isl_mat_cow(qp->div);
3249 qp = reduce_divs(qp);
3250 qp = remove_redundant_divs(qp);
3257 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_from_pw_aff(
3258 __isl_take isl_pw_aff *pwaff)
3261 isl_pw_qpolynomial *pwqp;
3266 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3269 for (i = 0; i < pwaff->n; ++i) {
3271 isl_qpolynomial *qp;
3273 dom = isl_set_copy(pwaff->p[i].set);
3274 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3275 pwqp = isl_pw_qpolynomial_add_piece(pwqp, dom, qp);
3278 isl_pw_aff_free(pwaff);
3282 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
3283 __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
3287 aff = isl_constraint_get_bound(c, type, pos);
3288 isl_constraint_free(c);
3289 return isl_qpolynomial_from_aff(aff);
3292 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3293 * in "qp" by subs[i].
3295 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
3296 __isl_take isl_qpolynomial *qp,
3297 enum isl_dim_type type, unsigned first, unsigned n,
3298 __isl_keep isl_qpolynomial **subs)
3301 struct isl_upoly **ups;
3306 qp = isl_qpolynomial_cow(qp);
3310 if (type == isl_dim_out)
3311 isl_die(qp->dim->ctx, isl_error_invalid,
3312 "cannot substitute output/set dimension",
3314 if (type == isl_dim_in)
3317 for (i = 0; i < n; ++i)
3321 isl_assert(qp->dim->ctx, first + n <= isl_space_dim(qp->dim, type),
3324 for (i = 0; i < n; ++i)
3325 isl_assert(qp->dim->ctx, isl_space_is_equal(qp->dim, subs[i]->dim),
3328 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3329 for (i = 0; i < n; ++i)
3330 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3332 first += pos(qp->dim, type);
3334 ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
3337 for (i = 0; i < n; ++i)
3338 ups[i] = subs[i]->upoly;
3340 qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
3349 isl_qpolynomial_free(qp);
3353 /* Extend "bset" with extra set dimensions for each integer division
3354 * in "qp" and then call "fn" with the extended bset and the polynomial
3355 * that results from replacing each of the integer divisions by the
3356 * corresponding extra set dimension.
3358 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
3359 __isl_keep isl_basic_set *bset,
3360 int (*fn)(__isl_take isl_basic_set *bset,
3361 __isl_take isl_qpolynomial *poly, void *user), void *user)
3365 isl_qpolynomial *poly;
3369 if (qp->div->n_row == 0)
3370 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3373 div = isl_mat_copy(qp->div);
3374 dim = isl_space_copy(qp->dim);
3375 dim = isl_space_add_dims(dim, isl_dim_set, qp->div->n_row);
3376 poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3377 bset = isl_basic_set_copy(bset);
3378 bset = isl_basic_set_add_dims(bset, isl_dim_set, qp->div->n_row);
3379 bset = add_div_constraints(bset, div);
3381 return fn(bset, poly, user);
3386 /* Return total degree in variables first (inclusive) up to last (exclusive).
3388 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3392 struct isl_upoly_rec *rec;
3396 if (isl_upoly_is_zero(up))
3398 if (isl_upoly_is_cst(up) || up->var < first)
3401 rec = isl_upoly_as_rec(up);
3405 for (i = 0; i < rec->n; ++i) {
3408 if (isl_upoly_is_zero(rec->p[i]))
3410 d = isl_upoly_degree(rec->p[i], first, last);
3420 /* Return total degree in set variables.
3422 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3430 ovar = isl_space_offset(poly->dim, isl_dim_set);
3431 nvar = isl_space_dim(poly->dim, isl_dim_set);
3432 return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3435 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3436 unsigned pos, int deg)
3439 struct isl_upoly_rec *rec;
3444 if (isl_upoly_is_cst(up) || up->var < pos) {
3446 return isl_upoly_copy(up);
3448 return isl_upoly_zero(up->ctx);
3451 rec = isl_upoly_as_rec(up);
3455 if (up->var == pos) {
3457 return isl_upoly_copy(rec->p[deg]);
3459 return isl_upoly_zero(up->ctx);
3462 up = isl_upoly_copy(up);
3463 up = isl_upoly_cow(up);
3464 rec = isl_upoly_as_rec(up);
3468 for (i = 0; i < rec->n; ++i) {
3469 struct isl_upoly *t;
3470 t = isl_upoly_coeff(rec->p[i], pos, deg);
3473 isl_upoly_free(rec->p[i]);
3483 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3485 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3486 __isl_keep isl_qpolynomial *qp,
3487 enum isl_dim_type type, unsigned t_pos, int deg)
3490 struct isl_upoly *up;
3496 if (type == isl_dim_out)
3497 isl_die(qp->div->ctx, isl_error_invalid,
3498 "output/set dimension does not have a coefficient",
3500 if (type == isl_dim_in)
3503 isl_assert(qp->div->ctx, t_pos < isl_space_dim(qp->dim, type),
3506 g_pos = pos(qp->dim, type) + t_pos;
3507 up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3509 c = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row, up);
3512 isl_mat_free(c->div);
3513 c->div = isl_mat_copy(qp->div);
3518 isl_qpolynomial_free(c);
3522 /* Homogenize the polynomial in the variables first (inclusive) up to
3523 * last (exclusive) by inserting powers of variable first.
3524 * Variable first is assumed not to appear in the input.
3526 __isl_give struct isl_upoly *isl_upoly_homogenize(
3527 __isl_take struct isl_upoly *up, int deg, int target,
3528 int first, int last)
3531 struct isl_upoly_rec *rec;
3535 if (isl_upoly_is_zero(up))
3539 if (isl_upoly_is_cst(up) || up->var < first) {
3540 struct isl_upoly *hom;
3542 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3545 rec = isl_upoly_as_rec(hom);
3546 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3551 up = isl_upoly_cow(up);
3552 rec = isl_upoly_as_rec(up);
3556 for (i = 0; i < rec->n; ++i) {
3557 if (isl_upoly_is_zero(rec->p[i]))
3559 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3560 up->var < last ? deg + i : i, target,
3572 /* Homogenize the polynomial in the set variables by introducing
3573 * powers of an extra set variable at position 0.
3575 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3576 __isl_take isl_qpolynomial *poly)
3580 int deg = isl_qpolynomial_degree(poly);
3585 poly = isl_qpolynomial_insert_dims(poly, isl_dim_in, 0, 1);
3586 poly = isl_qpolynomial_cow(poly);
3590 ovar = isl_space_offset(poly->dim, isl_dim_set);
3591 nvar = isl_space_dim(poly->dim, isl_dim_set);
3592 poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3599 isl_qpolynomial_free(poly);
3603 __isl_give isl_term *isl_term_alloc(__isl_take isl_space *dim,
3604 __isl_take isl_mat *div)
3612 n = isl_space_dim(dim, isl_dim_all) + div->n_row;
3614 term = isl_calloc(dim->ctx, struct isl_term,
3615 sizeof(struct isl_term) + (n - 1) * sizeof(int));
3622 isl_int_init(term->n);
3623 isl_int_init(term->d);
3627 isl_space_free(dim);
3632 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3641 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3650 total = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3652 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
3656 isl_int_set(dup->n, term->n);
3657 isl_int_set(dup->d, term->d);
3659 for (i = 0; i < total; ++i)
3660 dup->pow[i] = term->pow[i];
3665 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3673 return isl_term_dup(term);
3676 void isl_term_free(__isl_take isl_term *term)
3681 if (--term->ref > 0)
3684 isl_space_free(term->dim);
3685 isl_mat_free(term->div);
3686 isl_int_clear(term->n);
3687 isl_int_clear(term->d);
3691 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3699 case isl_dim_out: return isl_space_dim(term->dim, type);
3700 case isl_dim_div: return term->div->n_row;
3701 case isl_dim_all: return isl_space_dim(term->dim, isl_dim_all) +
3707 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3709 return term ? term->dim->ctx : NULL;
3712 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3716 isl_int_set(*n, term->n);
3719 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3723 isl_int_set(*d, term->d);
3726 /* Return the coefficient of the term "term".
3728 __isl_give isl_val *isl_term_get_coefficient_val(__isl_keep isl_term *term)
3733 return isl_val_rat_from_isl_int(isl_term_get_ctx(term),
3737 int isl_term_get_exp(__isl_keep isl_term *term,
3738 enum isl_dim_type type, unsigned pos)
3743 isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3745 if (type >= isl_dim_set)
3746 pos += isl_space_dim(term->dim, isl_dim_param);
3747 if (type >= isl_dim_div)
3748 pos += isl_space_dim(term->dim, isl_dim_set);
3750 return term->pow[pos];
3753 __isl_give isl_aff *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3755 isl_local_space *ls;
3761 isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3764 ls = isl_local_space_alloc_div(isl_space_copy(term->dim),
3765 isl_mat_copy(term->div));
3766 aff = isl_aff_alloc(ls);
3770 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
3772 aff = isl_aff_normalize(aff);
3777 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3778 int (*fn)(__isl_take isl_term *term, void *user),
3779 __isl_take isl_term *term, void *user)
3782 struct isl_upoly_rec *rec;
3787 if (isl_upoly_is_zero(up))
3790 isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3791 isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3792 isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3794 if (isl_upoly_is_cst(up)) {
3795 struct isl_upoly_cst *cst;
3796 cst = isl_upoly_as_cst(up);
3799 term = isl_term_cow(term);
3802 isl_int_set(term->n, cst->n);
3803 isl_int_set(term->d, cst->d);
3804 if (fn(isl_term_copy(term), user) < 0)
3809 rec = isl_upoly_as_rec(up);
3813 for (i = 0; i < rec->n; ++i) {
3814 term = isl_term_cow(term);
3817 term->pow[up->var] = i;
3818 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3822 term->pow[up->var] = 0;
3826 isl_term_free(term);
3830 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3831 int (*fn)(__isl_take isl_term *term, void *user), void *user)
3838 term = isl_term_alloc(isl_space_copy(qp->dim), isl_mat_copy(qp->div));
3842 term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3844 isl_term_free(term);
3846 return term ? 0 : -1;
3849 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3851 struct isl_upoly *up;
3852 isl_qpolynomial *qp;
3858 n = isl_space_dim(term->dim, isl_dim_all) + term->div->n_row;
3860 up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3861 for (i = 0; i < n; ++i) {
3864 up = isl_upoly_mul(up,
3865 isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3868 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim), term->div->n_row, up);
3871 isl_mat_free(qp->div);
3872 qp->div = isl_mat_copy(term->div);
3876 isl_term_free(term);
3879 isl_qpolynomial_free(qp);
3880 isl_term_free(term);
3884 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3885 __isl_take isl_space *dim)
3894 if (isl_space_is_equal(qp->dim, dim)) {
3895 isl_space_free(dim);
3899 qp = isl_qpolynomial_cow(qp);
3903 extra = isl_space_dim(dim, isl_dim_set) -
3904 isl_space_dim(qp->dim, isl_dim_set);
3905 total = isl_space_dim(qp->dim, isl_dim_all);
3906 if (qp->div->n_row) {
3909 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3912 for (i = 0; i < qp->div->n_row; ++i)
3914 qp->upoly = expand(qp->upoly, exp, total);
3919 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3922 for (i = 0; i < qp->div->n_row; ++i)
3923 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3925 isl_space_free(qp->dim);
3930 isl_space_free(dim);
3931 isl_qpolynomial_free(qp);
3935 /* For each parameter or variable that does not appear in qp,
3936 * first eliminate the variable from all constraints and then set it to zero.
3938 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3939 __isl_keep isl_qpolynomial *qp)
3950 d = isl_space_dim(set->dim, isl_dim_all);
3951 active = isl_calloc_array(set->ctx, int, d);
3952 if (set_active(qp, active) < 0)
3955 for (i = 0; i < d; ++i)
3964 nparam = isl_space_dim(set->dim, isl_dim_param);
3965 nvar = isl_space_dim(set->dim, isl_dim_set);
3966 for (i = 0; i < nparam; ++i) {
3969 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3970 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3972 for (i = 0; i < nvar; ++i) {
3973 if (active[nparam + i])
3975 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3976 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3988 struct isl_opt_data {
3989 isl_qpolynomial *qp;
3991 isl_qpolynomial *opt;
3995 static int opt_fn(__isl_take isl_point *pnt, void *user)
3997 struct isl_opt_data *data = (struct isl_opt_data *)user;
3998 isl_qpolynomial *val;
4000 val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
4004 } else if (data->max) {
4005 data->opt = isl_qpolynomial_max_cst(data->opt, val);
4007 data->opt = isl_qpolynomial_min_cst(data->opt, val);
4013 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
4014 __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
4016 struct isl_opt_data data = { NULL, 1, NULL, max };
4021 if (isl_upoly_is_cst(qp->upoly)) {
4026 set = fix_inactive(set, qp);
4029 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4033 isl_space *space = isl_qpolynomial_get_domain_space(qp);
4034 data.opt = isl_qpolynomial_zero_on_domain(space);
4038 isl_qpolynomial_free(qp);
4042 isl_qpolynomial_free(qp);
4043 isl_qpolynomial_free(data.opt);
4047 __isl_give isl_qpolynomial *isl_qpolynomial_morph_domain(
4048 __isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
4053 struct isl_upoly **subs;
4054 isl_mat *mat, *diag;
4056 qp = isl_qpolynomial_cow(qp);
4061 isl_assert(ctx, isl_space_is_equal(qp->dim, morph->dom->dim), goto error);
4063 n_sub = morph->inv->n_row - 1;
4064 if (morph->inv->n_row != morph->inv->n_col)
4065 n_sub += qp->div->n_row;
4066 subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
4070 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4071 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
4072 morph->inv->row[0][0], morph->inv->n_col);
4073 if (morph->inv->n_row != morph->inv->n_col)
4074 for (i = 0; i < qp->div->n_row; ++i)
4075 subs[morph->inv->n_row - 1 + i] =
4076 isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4078 qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
4080 for (i = 0; i < n_sub; ++i)
4081 isl_upoly_free(subs[i]);
4084 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4085 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4086 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4087 mat = isl_mat_diagonal(mat, diag);
4088 qp->div = isl_mat_product(qp->div, mat);
4089 isl_space_free(qp->dim);
4090 qp->dim = isl_space_copy(morph->ran->dim);
4092 if (!qp->upoly || !qp->div || !qp->dim)
4095 isl_morph_free(morph);
4099 isl_qpolynomial_free(qp);
4100 isl_morph_free(morph);
4104 static int neg_entry(void **entry, void *user)
4106 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4108 *pwqp = isl_pw_qpolynomial_neg(*pwqp);
4110 return *pwqp ? 0 : -1;
4113 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
4114 __isl_take isl_union_pw_qpolynomial *upwqp)
4116 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4120 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4121 &neg_entry, NULL) < 0)
4126 isl_union_pw_qpolynomial_free(upwqp);
4130 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
4131 __isl_take isl_union_pw_qpolynomial *upwqp1,
4132 __isl_take isl_union_pw_qpolynomial *upwqp2)
4134 return match_bin_op(upwqp1, upwqp2, &isl_pw_qpolynomial_mul);
4137 /* Reorder the columns of the given div definitions according to the
4140 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
4141 __isl_take isl_reordering *r)
4150 extra = isl_space_dim(r->dim, isl_dim_all) + div->n_row - r->len;
4151 mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
4155 for (i = 0; i < div->n_row; ++i) {
4156 isl_seq_cpy(mat->row[i], div->row[i], 2);
4157 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
4158 for (j = 0; j < r->len; ++j)
4159 isl_int_set(mat->row[i][2 + r->pos[j]],
4160 div->row[i][2 + j]);
4163 isl_reordering_free(r);
4167 isl_reordering_free(r);
4172 /* Reorder the dimension of "qp" according to the given reordering.
4174 __isl_give isl_qpolynomial *isl_qpolynomial_realign_domain(
4175 __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
4177 qp = isl_qpolynomial_cow(qp);
4181 r = isl_reordering_extend(r, qp->div->n_row);
4185 qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
4189 qp->upoly = reorder(qp->upoly, r->pos);
4193 qp = isl_qpolynomial_reset_domain_space(qp, isl_space_copy(r->dim));
4195 isl_reordering_free(r);
4198 isl_qpolynomial_free(qp);
4199 isl_reordering_free(r);
4203 __isl_give isl_qpolynomial *isl_qpolynomial_align_params(
4204 __isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
4209 if (!isl_space_match(qp->dim, isl_dim_param, model, isl_dim_param)) {
4210 isl_reordering *exp;
4212 model = isl_space_drop_dims(model, isl_dim_in,
4213 0, isl_space_dim(model, isl_dim_in));
4214 model = isl_space_drop_dims(model, isl_dim_out,
4215 0, isl_space_dim(model, isl_dim_out));
4216 exp = isl_parameter_alignment_reordering(qp->dim, model);
4217 exp = isl_reordering_extend_space(exp,
4218 isl_qpolynomial_get_domain_space(qp));
4219 qp = isl_qpolynomial_realign_domain(qp, exp);
4222 isl_space_free(model);
4225 isl_space_free(model);
4226 isl_qpolynomial_free(qp);
4230 struct isl_split_periods_data {
4232 isl_pw_qpolynomial *res;
4235 /* Create a slice where the integer division "div" has the fixed value "v".
4236 * In particular, if "div" refers to floor(f/m), then create a slice
4238 * m v <= f <= m v + (m - 1)
4243 * -f + m v + (m - 1) >= 0
4245 static __isl_give isl_set *set_div_slice(__isl_take isl_space *dim,
4246 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4249 isl_basic_set *bset = NULL;
4255 total = isl_space_dim(dim, isl_dim_all);
4256 bset = isl_basic_set_alloc_space(isl_space_copy(dim), 0, 0, 2);
4258 k = isl_basic_set_alloc_inequality(bset);
4261 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4262 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4264 k = isl_basic_set_alloc_inequality(bset);
4267 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4268 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4269 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4270 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4272 isl_space_free(dim);
4273 return isl_set_from_basic_set(bset);
4275 isl_basic_set_free(bset);
4276 isl_space_free(dim);
4280 static int split_periods(__isl_take isl_set *set,
4281 __isl_take isl_qpolynomial *qp, void *user);
4283 /* Create a slice of the domain "set" such that integer division "div"
4284 * has the fixed value "v" and add the results to data->res,
4285 * replacing the integer division by "v" in "qp".
4287 static int set_div(__isl_take isl_set *set,
4288 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4289 struct isl_split_periods_data *data)
4294 struct isl_upoly *cst;
4296 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4297 set = isl_set_intersect(set, slice);
4302 total = isl_space_dim(qp->dim, isl_dim_all);
4304 for (i = div + 1; i < qp->div->n_row; ++i) {
4305 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
4307 isl_int_addmul(qp->div->row[i][1],
4308 qp->div->row[i][2 + total + div], v);
4309 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
4312 cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4313 qp = substitute_div(qp, div, cst);
4315 return split_periods(set, qp, data);
4318 isl_qpolynomial_free(qp);
4322 /* Split the domain "set" such that integer division "div"
4323 * has a fixed value (ranging from "min" to "max") on each slice
4324 * and add the results to data->res.
4326 static int split_div(__isl_take isl_set *set,
4327 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4328 struct isl_split_periods_data *data)
4330 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4331 isl_set *set_i = isl_set_copy(set);
4332 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
4334 if (set_div(set_i, qp_i, div, min, data) < 0)
4338 isl_qpolynomial_free(qp);
4342 isl_qpolynomial_free(qp);
4346 /* If "qp" refers to any integer division
4347 * that can only attain "max_periods" distinct values on "set"
4348 * then split the domain along those distinct values.
4349 * Add the results (or the original if no splitting occurs)
4352 static int split_periods(__isl_take isl_set *set,
4353 __isl_take isl_qpolynomial *qp, void *user)
4356 isl_pw_qpolynomial *pwqp;
4357 struct isl_split_periods_data *data;
4362 data = (struct isl_split_periods_data *)user;
4367 if (qp->div->n_row == 0) {
4368 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4369 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4375 total = isl_space_dim(qp->dim, isl_dim_all);
4376 for (i = 0; i < qp->div->n_row; ++i) {
4377 enum isl_lp_result lp_res;
4379 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4380 qp->div->n_row) != -1)
4383 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4384 set->ctx->one, &min, NULL, NULL);
4385 if (lp_res == isl_lp_error)
4387 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4389 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4391 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4392 set->ctx->one, &max, NULL, NULL);
4393 if (lp_res == isl_lp_error)
4395 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4397 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4399 isl_int_sub(max, max, min);
4400 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4401 isl_int_add(max, max, min);
4406 if (i < qp->div->n_row) {
4407 r = split_div(set, qp, i, min, max, data);
4409 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4410 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4422 isl_qpolynomial_free(qp);
4426 /* If any quasi-polynomial in pwqp refers to any integer division
4427 * that can only attain "max_periods" distinct values on its domain
4428 * then split the domain along those distinct values.
4430 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4431 __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4433 struct isl_split_periods_data data;
4435 data.max_periods = max_periods;
4436 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4438 if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4441 isl_pw_qpolynomial_free(pwqp);
4445 isl_pw_qpolynomial_free(data.res);
4446 isl_pw_qpolynomial_free(pwqp);
4450 /* Construct a piecewise quasipolynomial that is constant on the given
4451 * domain. In particular, it is
4454 * infinity if cst == -1
4456 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4457 __isl_take isl_basic_set *bset, int cst)
4460 isl_qpolynomial *qp;
4465 bset = isl_basic_set_params(bset);
4466 dim = isl_basic_set_get_space(bset);
4468 qp = isl_qpolynomial_infty_on_domain(dim);
4470 qp = isl_qpolynomial_zero_on_domain(dim);
4472 qp = isl_qpolynomial_one_on_domain(dim);
4473 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4476 /* Factor bset, call fn on each of the factors and return the product.
4478 * If no factors can be found, simply call fn on the input.
4479 * Otherwise, construct the factors based on the factorizer,
4480 * call fn on each factor and compute the product.
4482 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4483 __isl_take isl_basic_set *bset,
4484 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4490 isl_qpolynomial *qp;
4491 isl_pw_qpolynomial *pwqp;
4495 f = isl_basic_set_factorizer(bset);
4498 if (f->n_group == 0) {
4499 isl_factorizer_free(f);
4503 nparam = isl_basic_set_dim(bset, isl_dim_param);
4504 nvar = isl_basic_set_dim(bset, isl_dim_set);
4506 dim = isl_basic_set_get_space(bset);
4507 dim = isl_space_domain(dim);
4508 set = isl_set_universe(isl_space_copy(dim));
4509 qp = isl_qpolynomial_one_on_domain(dim);
4510 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4512 bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4514 for (i = 0, n = 0; i < f->n_group; ++i) {
4515 isl_basic_set *bset_i;
4516 isl_pw_qpolynomial *pwqp_i;
4518 bset_i = isl_basic_set_copy(bset);
4519 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4520 nparam + n + f->len[i], nvar - n - f->len[i]);
4521 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4523 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4524 n + f->len[i], nvar - n - f->len[i]);
4525 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4527 pwqp_i = fn(bset_i);
4528 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4533 isl_basic_set_free(bset);
4534 isl_factorizer_free(f);
4538 isl_basic_set_free(bset);
4542 /* Factor bset, call fn on each of the factors and return the product.
4543 * The function is assumed to evaluate to zero on empty domains,
4544 * to one on zero-dimensional domains and to infinity on unbounded domains
4545 * and will not be called explicitly on zero-dimensional or unbounded domains.
4547 * We first check for some special cases and remove all equalities.
4548 * Then we hand over control to compressed_multiplicative_call.
4550 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4551 __isl_take isl_basic_set *bset,
4552 __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4556 isl_pw_qpolynomial *pwqp;
4561 if (isl_basic_set_plain_is_empty(bset))
4562 return constant_on_domain(bset, 0);
4564 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
4565 return constant_on_domain(bset, 1);
4567 bounded = isl_basic_set_is_bounded(bset);
4571 return constant_on_domain(bset, -1);
4573 if (bset->n_eq == 0)
4574 return compressed_multiplicative_call(bset, fn);
4576 morph = isl_basic_set_full_compression(bset);
4577 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4579 pwqp = compressed_multiplicative_call(bset, fn);
4581 morph = isl_morph_dom_params(morph);
4582 morph = isl_morph_ran_params(morph);
4583 morph = isl_morph_inverse(morph);
4585 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, morph);
4589 isl_basic_set_free(bset);
4593 /* Drop all floors in "qp", turning each integer division [a/m] into
4594 * a rational division a/m. If "down" is set, then the integer division
4595 * is replaces by (a-(m-1))/m instead.
4597 static __isl_give isl_qpolynomial *qp_drop_floors(
4598 __isl_take isl_qpolynomial *qp, int down)
4601 struct isl_upoly *s;
4605 if (qp->div->n_row == 0)
4608 qp = isl_qpolynomial_cow(qp);
4612 for (i = qp->div->n_row - 1; i >= 0; --i) {
4614 isl_int_sub(qp->div->row[i][1],
4615 qp->div->row[i][1], qp->div->row[i][0]);
4616 isl_int_add_ui(qp->div->row[i][1],
4617 qp->div->row[i][1], 1);
4619 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4620 qp->div->row[i][0], qp->div->n_col - 1);
4621 qp = substitute_div(qp, i, s);
4629 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4630 * a rational division a/m.
4632 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4633 __isl_take isl_pw_qpolynomial *pwqp)
4640 if (isl_pw_qpolynomial_is_zero(pwqp))
4643 pwqp = isl_pw_qpolynomial_cow(pwqp);
4647 for (i = 0; i < pwqp->n; ++i) {
4648 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4655 isl_pw_qpolynomial_free(pwqp);
4659 /* Adjust all the integer divisions in "qp" such that they are at least
4660 * one over the given orthant (identified by "signs"). This ensures
4661 * that they will still be non-negative even after subtracting (m-1)/m.
4663 * In particular, f is replaced by f' + v, changing f = [a/m]
4664 * to f' = [(a - m v)/m].
4665 * If the constant term k in a is smaller than m,
4666 * the constant term of v is set to floor(k/m) - 1.
4667 * For any other term, if the coefficient c and the variable x have
4668 * the same sign, then no changes are needed.
4669 * Otherwise, if the variable is positive (and c is negative),
4670 * then the coefficient of x in v is set to floor(c/m).
4671 * If the variable is negative (and c is positive),
4672 * then the coefficient of x in v is set to ceil(c/m).
4674 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4680 struct isl_upoly *s;
4682 qp = isl_qpolynomial_cow(qp);
4685 qp->div = isl_mat_cow(qp->div);
4689 total = isl_space_dim(qp->dim, isl_dim_all);
4690 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4692 for (i = 0; i < qp->div->n_row; ++i) {
4693 isl_int *row = qp->div->row[i];
4697 if (isl_int_lt(row[1], row[0])) {
4698 isl_int_fdiv_q(v->el[0], row[1], row[0]);
4699 isl_int_sub_ui(v->el[0], v->el[0], 1);
4700 isl_int_submul(row[1], row[0], v->el[0]);
4702 for (j = 0; j < total; ++j) {
4703 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4706 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4708 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4709 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4711 for (j = 0; j < i; ++j) {
4712 if (isl_int_sgn(row[2 + total + j]) >= 0)
4714 isl_int_fdiv_q(v->el[1 + total + j],
4715 row[2 + total + j], row[0]);
4716 isl_int_submul(row[2 + total + j],
4717 row[0], v->el[1 + total + j]);
4719 for (j = i + 1; j < qp->div->n_row; ++j) {
4720 if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4722 isl_seq_combine(qp->div->row[j] + 1,
4723 qp->div->ctx->one, qp->div->row[j] + 1,
4724 qp->div->row[j][2 + total + i], v->el, v->size);
4726 isl_int_set_si(v->el[1 + total + i], 1);
4727 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4728 qp->div->ctx->one, v->size);
4729 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4739 isl_qpolynomial_free(qp);
4743 struct isl_to_poly_data {
4745 isl_pw_qpolynomial *res;
4746 isl_qpolynomial *qp;
4749 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4750 * We first make all integer divisions positive and then split the
4751 * quasipolynomials into terms with sign data->sign (the direction
4752 * of the requested approximation) and terms with the opposite sign.
4753 * In the first set of terms, each integer division [a/m] is
4754 * overapproximated by a/m, while in the second it is underapproximated
4757 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4760 struct isl_to_poly_data *data = user;
4761 isl_pw_qpolynomial *t;
4762 isl_qpolynomial *qp, *up, *down;
4764 qp = isl_qpolynomial_copy(data->qp);
4765 qp = make_divs_pos(qp, signs);
4767 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4768 up = qp_drop_floors(up, 0);
4769 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4770 down = qp_drop_floors(down, 1);
4772 isl_qpolynomial_free(qp);
4773 qp = isl_qpolynomial_add(up, down);
4775 t = isl_pw_qpolynomial_alloc(orthant, qp);
4776 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4781 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4782 * the polynomial will be an overapproximation. If "sign" is negative,
4783 * it will be an underapproximation. If "sign" is zero, the approximation
4784 * will lie somewhere in between.
4786 * In particular, is sign == 0, we simply drop the floors, turning
4787 * the integer divisions into rational divisions.
4788 * Otherwise, we split the domains into orthants, make all integer divisions
4789 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4790 * depending on the requested sign and the sign of the term in which
4791 * the integer division appears.
4793 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4794 __isl_take isl_pw_qpolynomial *pwqp, int sign)
4797 struct isl_to_poly_data data;
4800 return pwqp_drop_floors(pwqp);
4806 data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp));
4808 for (i = 0; i < pwqp->n; ++i) {
4809 if (pwqp->p[i].qp->div->n_row == 0) {
4810 isl_pw_qpolynomial *t;
4811 t = isl_pw_qpolynomial_alloc(
4812 isl_set_copy(pwqp->p[i].set),
4813 isl_qpolynomial_copy(pwqp->p[i].qp));
4814 data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4817 data.qp = pwqp->p[i].qp;
4818 if (isl_set_foreach_orthant(pwqp->p[i].set,
4819 &to_polynomial_on_orthant, &data) < 0)
4823 isl_pw_qpolynomial_free(pwqp);
4827 isl_pw_qpolynomial_free(pwqp);
4828 isl_pw_qpolynomial_free(data.res);
4832 static int poly_entry(void **entry, void *user)
4835 isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4837 *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4839 return *pwqp ? 0 : -1;
4842 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4843 __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4845 upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4849 if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4850 &poly_entry, &sign) < 0)
4855 isl_union_pw_qpolynomial_free(upwqp);
4859 __isl_give isl_basic_map *isl_basic_map_from_qpolynomial(
4860 __isl_take isl_qpolynomial *qp)
4864 isl_vec *aff = NULL;
4865 isl_basic_map *bmap = NULL;
4871 if (!isl_upoly_is_affine(qp->upoly))
4872 isl_die(qp->dim->ctx, isl_error_invalid,
4873 "input quasi-polynomial not affine", goto error);
4874 aff = isl_qpolynomial_extract_affine(qp);
4877 dim = isl_qpolynomial_get_space(qp);
4878 pos = 1 + isl_space_offset(dim, isl_dim_out);
4879 n_div = qp->div->n_row;
4880 bmap = isl_basic_map_alloc_space(dim, n_div, 1, 2 * n_div);
4882 for (i = 0; i < n_div; ++i) {
4883 k = isl_basic_map_alloc_div(bmap);
4886 isl_seq_cpy(bmap->div[k], qp->div->row[i], qp->div->n_col);
4887 isl_int_set_si(bmap->div[k][qp->div->n_col], 0);
4888 if (isl_basic_map_add_div_constraints(bmap, k) < 0)
4891 k = isl_basic_map_alloc_equality(bmap);
4894 isl_int_neg(bmap->eq[k][pos], aff->el[0]);
4895 isl_seq_cpy(bmap->eq[k], aff->el + 1, pos);
4896 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->el + 1 + pos, n_div);
4899 isl_qpolynomial_free(qp);
4900 bmap = isl_basic_map_finalize(bmap);
4904 isl_qpolynomial_free(qp);
4905 isl_basic_map_free(bmap);