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,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.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>
32 #define BASE pw_qpolynomial
34 #include <isl_list_templ.c>
36 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
39 case isl_dim_param
: return 0;
40 case isl_dim_in
: return dim
->nparam
;
41 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
46 isl_bool
isl_poly_is_cst(__isl_keep isl_poly
*poly
)
49 return isl_bool_error
;
54 __isl_keep isl_poly_cst
*isl_poly_as_cst(__isl_keep isl_poly
*poly
)
59 isl_assert(poly
->ctx
, poly
->var
< 0, return NULL
);
61 return (isl_poly_cst
*) poly
;
64 __isl_keep isl_poly_rec
*isl_poly_as_rec(__isl_keep isl_poly
*poly
)
69 isl_assert(poly
->ctx
, poly
->var
>= 0, return NULL
);
71 return (isl_poly_rec
*) poly
;
74 /* Compare two polynomials.
76 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
77 * than "poly2" and 0 if they are equal.
79 static int isl_poly_plain_cmp(__isl_keep isl_poly
*poly1
,
80 __isl_keep isl_poly
*poly2
)
84 isl_poly_rec
*rec1
, *rec2
;
88 is_cst1
= isl_poly_is_cst(poly1
);
93 if (poly1
->var
!= poly2
->var
)
94 return poly1
->var
- poly2
->var
;
97 isl_poly_cst
*cst1
, *cst2
;
100 cst1
= isl_poly_as_cst(poly1
);
101 cst2
= isl_poly_as_cst(poly2
);
104 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
107 return isl_int_cmp(cst1
->d
, cst2
->d
);
110 rec1
= isl_poly_as_rec(poly1
);
111 rec2
= isl_poly_as_rec(poly2
);
115 if (rec1
->n
!= rec2
->n
)
116 return rec1
->n
- rec2
->n
;
118 for (i
= 0; i
< rec1
->n
; ++i
) {
119 int cmp
= isl_poly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
127 isl_bool
isl_poly_is_equal(__isl_keep isl_poly
*poly1
,
128 __isl_keep isl_poly
*poly2
)
132 isl_poly_rec
*rec1
, *rec2
;
134 is_cst1
= isl_poly_is_cst(poly1
);
135 if (is_cst1
< 0 || !poly2
)
136 return isl_bool_error
;
138 return isl_bool_true
;
139 if (poly1
->var
!= poly2
->var
)
140 return isl_bool_false
;
142 isl_poly_cst
*cst1
, *cst2
;
143 cst1
= isl_poly_as_cst(poly1
);
144 cst2
= isl_poly_as_cst(poly2
);
146 return isl_bool_error
;
147 return isl_int_eq(cst1
->n
, cst2
->n
) &&
148 isl_int_eq(cst1
->d
, cst2
->d
);
151 rec1
= isl_poly_as_rec(poly1
);
152 rec2
= isl_poly_as_rec(poly2
);
154 return isl_bool_error
;
156 if (rec1
->n
!= rec2
->n
)
157 return isl_bool_false
;
159 for (i
= 0; i
< rec1
->n
; ++i
) {
160 isl_bool eq
= isl_poly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
165 return isl_bool_true
;
168 isl_bool
isl_poly_is_zero(__isl_keep isl_poly
*poly
)
173 is_cst
= isl_poly_is_cst(poly
);
174 if (is_cst
< 0 || !is_cst
)
177 cst
= isl_poly_as_cst(poly
);
179 return isl_bool_error
;
181 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
184 int isl_poly_sgn(__isl_keep isl_poly
*poly
)
189 is_cst
= isl_poly_is_cst(poly
);
190 if (is_cst
< 0 || !is_cst
)
193 cst
= isl_poly_as_cst(poly
);
197 return isl_int_sgn(cst
->n
);
200 isl_bool
isl_poly_is_nan(__isl_keep isl_poly
*poly
)
205 is_cst
= isl_poly_is_cst(poly
);
206 if (is_cst
< 0 || !is_cst
)
209 cst
= isl_poly_as_cst(poly
);
211 return isl_bool_error
;
213 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
216 isl_bool
isl_poly_is_infty(__isl_keep isl_poly
*poly
)
221 is_cst
= isl_poly_is_cst(poly
);
222 if (is_cst
< 0 || !is_cst
)
225 cst
= isl_poly_as_cst(poly
);
227 return isl_bool_error
;
229 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
232 isl_bool
isl_poly_is_neginfty(__isl_keep isl_poly
*poly
)
237 is_cst
= isl_poly_is_cst(poly
);
238 if (is_cst
< 0 || !is_cst
)
241 cst
= isl_poly_as_cst(poly
);
243 return isl_bool_error
;
245 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
248 isl_bool
isl_poly_is_one(__isl_keep isl_poly
*poly
)
253 is_cst
= isl_poly_is_cst(poly
);
254 if (is_cst
< 0 || !is_cst
)
257 cst
= isl_poly_as_cst(poly
);
259 return isl_bool_error
;
261 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
264 isl_bool
isl_poly_is_negone(__isl_keep isl_poly
*poly
)
269 is_cst
= isl_poly_is_cst(poly
);
270 if (is_cst
< 0 || !is_cst
)
273 cst
= isl_poly_as_cst(poly
);
275 return isl_bool_error
;
277 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
280 __isl_give isl_poly_cst
*isl_poly_cst_alloc(isl_ctx
*ctx
)
284 cst
= isl_alloc_type(ctx
, struct isl_poly_cst
);
293 isl_int_init(cst
->n
);
294 isl_int_init(cst
->d
);
299 __isl_give isl_poly
*isl_poly_zero(isl_ctx
*ctx
)
303 cst
= isl_poly_cst_alloc(ctx
);
307 isl_int_set_si(cst
->n
, 0);
308 isl_int_set_si(cst
->d
, 1);
313 __isl_give isl_poly
*isl_poly_one(isl_ctx
*ctx
)
317 cst
= isl_poly_cst_alloc(ctx
);
321 isl_int_set_si(cst
->n
, 1);
322 isl_int_set_si(cst
->d
, 1);
327 __isl_give isl_poly
*isl_poly_infty(isl_ctx
*ctx
)
331 cst
= isl_poly_cst_alloc(ctx
);
335 isl_int_set_si(cst
->n
, 1);
336 isl_int_set_si(cst
->d
, 0);
341 __isl_give isl_poly
*isl_poly_neginfty(isl_ctx
*ctx
)
345 cst
= isl_poly_cst_alloc(ctx
);
349 isl_int_set_si(cst
->n
, -1);
350 isl_int_set_si(cst
->d
, 0);
355 __isl_give isl_poly
*isl_poly_nan(isl_ctx
*ctx
)
359 cst
= isl_poly_cst_alloc(ctx
);
363 isl_int_set_si(cst
->n
, 0);
364 isl_int_set_si(cst
->d
, 0);
369 __isl_give isl_poly
*isl_poly_rat_cst(isl_ctx
*ctx
, isl_int n
, isl_int d
)
373 cst
= isl_poly_cst_alloc(ctx
);
377 isl_int_set(cst
->n
, n
);
378 isl_int_set(cst
->d
, d
);
383 __isl_give isl_poly_rec
*isl_poly_alloc_rec(isl_ctx
*ctx
, int var
, int size
)
387 isl_assert(ctx
, var
>= 0, return NULL
);
388 isl_assert(ctx
, size
>= 0, return NULL
);
389 rec
= isl_calloc(ctx
, struct isl_poly_rec
,
390 sizeof(struct isl_poly_rec
) +
391 size
* sizeof(struct isl_poly
*));
406 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
407 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
409 qp
= isl_qpolynomial_cow(qp
);
413 isl_space_free(qp
->dim
);
418 isl_qpolynomial_free(qp
);
423 /* Reset the space of "qp". This function is called from isl_pw_templ.c
424 * and doesn't know if the space of an element object is represented
425 * directly or through its domain. It therefore passes along both.
427 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
428 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
429 __isl_take isl_space
*domain
)
431 isl_space_free(space
);
432 return isl_qpolynomial_reset_domain_space(qp
, domain
);
435 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
437 return qp
? qp
->dim
->ctx
: NULL
;
440 /* Return the domain space of "qp".
442 static __isl_keep isl_space
*isl_qpolynomial_peek_domain_space(
443 __isl_keep isl_qpolynomial
*qp
)
445 return qp
? qp
->dim
: NULL
;
448 /* Return a copy of the domain space of "qp".
450 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
451 __isl_keep isl_qpolynomial
*qp
)
453 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp
));
456 /* Return a copy of the local space on which "qp" is defined.
458 static __isl_give isl_local_space
*isl_qpolynomial_get_domain_local_space(
459 __isl_keep isl_qpolynomial
*qp
)
466 space
= isl_qpolynomial_get_domain_space(qp
);
467 return isl_local_space_alloc_div(space
, isl_mat_copy(qp
->div
));
470 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
475 space
= isl_space_copy(qp
->dim
);
476 space
= isl_space_from_domain(space
);
477 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
481 /* Return the number of variables of the given type in the domain of "qp".
483 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
484 enum isl_dim_type type
)
488 space
= isl_qpolynomial_peek_domain_space(qp
);
492 if (type
== isl_dim_div
)
493 return qp
->div
->n_row
;
494 if (type
== isl_dim_all
)
495 return isl_space_dim(space
, isl_dim_all
) +
496 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
497 return isl_space_dim(space
, type
);
500 /* Given the type of a dimension of an isl_qpolynomial,
501 * return the type of the corresponding dimension in its domain.
502 * This function is only called for "type" equal to isl_dim_in or
505 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
507 return type
== isl_dim_in
? isl_dim_set
: type
;
510 /* Externally, an isl_qpolynomial has a map space, but internally, the
511 * ls field corresponds to the domain of that space.
513 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
514 enum isl_dim_type type
)
518 if (type
== isl_dim_out
)
520 type
= domain_type(type
);
521 return isl_qpolynomial_domain_dim(qp
, type
);
524 /* Return the offset of the first variable of type "type" within
525 * the variables of the domain of "qp".
527 static int isl_qpolynomial_domain_var_offset(__isl_keep isl_qpolynomial
*qp
,
528 enum isl_dim_type type
)
532 space
= isl_qpolynomial_peek_domain_space(qp
);
538 case isl_dim_set
: return isl_space_offset(space
, type
);
539 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
542 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
543 "invalid dimension type", return -1);
547 /* Return the offset of the first coefficient of type "type" in
548 * the domain of "qp".
550 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
551 enum isl_dim_type type
)
559 return 1 + isl_qpolynomial_domain_var_offset(qp
, type
);
565 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
567 return qp
? isl_poly_is_zero(qp
->poly
) : isl_bool_error
;
570 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
572 return qp
? isl_poly_is_one(qp
->poly
) : isl_bool_error
;
575 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
577 return qp
? isl_poly_is_nan(qp
->poly
) : isl_bool_error
;
580 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
582 return qp
? isl_poly_is_infty(qp
->poly
) : isl_bool_error
;
585 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
587 return qp
? isl_poly_is_neginfty(qp
->poly
) : isl_bool_error
;
590 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
592 return qp
? isl_poly_sgn(qp
->poly
) : 0;
595 static void poly_free_cst(__isl_take isl_poly_cst
*cst
)
597 isl_int_clear(cst
->n
);
598 isl_int_clear(cst
->d
);
601 static void poly_free_rec(__isl_take isl_poly_rec
*rec
)
605 for (i
= 0; i
< rec
->n
; ++i
)
606 isl_poly_free(rec
->p
[i
]);
609 __isl_give isl_poly
*isl_poly_copy(__isl_keep isl_poly
*poly
)
618 __isl_give isl_poly
*isl_poly_dup_cst(__isl_keep isl_poly
*poly
)
623 cst
= isl_poly_as_cst(poly
);
627 dup
= isl_poly_as_cst(isl_poly_zero(poly
->ctx
));
630 isl_int_set(dup
->n
, cst
->n
);
631 isl_int_set(dup
->d
, cst
->d
);
636 __isl_give isl_poly
*isl_poly_dup_rec(__isl_keep isl_poly
*poly
)
642 rec
= isl_poly_as_rec(poly
);
646 dup
= isl_poly_alloc_rec(poly
->ctx
, poly
->var
, rec
->n
);
650 for (i
= 0; i
< rec
->n
; ++i
) {
651 dup
->p
[i
] = isl_poly_copy(rec
->p
[i
]);
659 isl_poly_free(&dup
->poly
);
663 __isl_give isl_poly
*isl_poly_dup(__isl_keep isl_poly
*poly
)
667 is_cst
= isl_poly_is_cst(poly
);
671 return isl_poly_dup_cst(poly
);
673 return isl_poly_dup_rec(poly
);
676 __isl_give isl_poly
*isl_poly_cow(__isl_take isl_poly
*poly
)
684 return isl_poly_dup(poly
);
687 __isl_null isl_poly
*isl_poly_free(__isl_take isl_poly
*poly
)
696 poly_free_cst((isl_poly_cst
*) poly
);
698 poly_free_rec((isl_poly_rec
*) poly
);
700 isl_ctx_deref(poly
->ctx
);
705 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst
*cst
)
710 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
711 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
712 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
713 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
718 __isl_give isl_poly
*isl_poly_sum_cst(__isl_take isl_poly
*poly1
,
719 __isl_take isl_poly
*poly2
)
724 poly1
= isl_poly_cow(poly1
);
725 if (!poly1
|| !poly2
)
728 cst1
= isl_poly_as_cst(poly1
);
729 cst2
= isl_poly_as_cst(poly2
);
731 if (isl_int_eq(cst1
->d
, cst2
->d
))
732 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
734 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
735 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
736 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
739 isl_poly_cst_reduce(cst1
);
741 isl_poly_free(poly2
);
744 isl_poly_free(poly1
);
745 isl_poly_free(poly2
);
749 static __isl_give isl_poly
*replace_by_zero(__isl_take isl_poly
*poly
)
757 return isl_poly_zero(ctx
);
760 static __isl_give isl_poly
*replace_by_constant_term(__isl_take isl_poly
*poly
)
768 rec
= isl_poly_as_rec(poly
);
771 cst
= isl_poly_copy(rec
->p
[0]);
779 __isl_give isl_poly
*isl_poly_sum(__isl_take isl_poly
*poly1
,
780 __isl_take isl_poly
*poly2
)
783 isl_bool is_zero
, is_nan
, is_cst
;
784 isl_poly_rec
*rec1
, *rec2
;
786 if (!poly1
|| !poly2
)
789 is_nan
= isl_poly_is_nan(poly1
);
793 isl_poly_free(poly2
);
797 is_nan
= isl_poly_is_nan(poly2
);
801 isl_poly_free(poly1
);
805 is_zero
= isl_poly_is_zero(poly1
);
809 isl_poly_free(poly1
);
813 is_zero
= isl_poly_is_zero(poly2
);
817 isl_poly_free(poly2
);
821 if (poly1
->var
< poly2
->var
)
822 return isl_poly_sum(poly2
, poly1
);
824 if (poly2
->var
< poly1
->var
) {
828 is_infty
= isl_poly_is_infty(poly2
);
829 if (is_infty
>= 0 && !is_infty
)
830 is_infty
= isl_poly_is_neginfty(poly2
);
834 isl_poly_free(poly1
);
837 poly1
= isl_poly_cow(poly1
);
838 rec
= isl_poly_as_rec(poly1
);
841 rec
->p
[0] = isl_poly_sum(rec
->p
[0], poly2
);
843 poly1
= replace_by_constant_term(poly1
);
847 is_cst
= isl_poly_is_cst(poly1
);
851 return isl_poly_sum_cst(poly1
, poly2
);
853 rec1
= isl_poly_as_rec(poly1
);
854 rec2
= isl_poly_as_rec(poly2
);
858 if (rec1
->n
< rec2
->n
)
859 return isl_poly_sum(poly2
, poly1
);
861 poly1
= isl_poly_cow(poly1
);
862 rec1
= isl_poly_as_rec(poly1
);
866 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
869 rec1
->p
[i
] = isl_poly_sum(rec1
->p
[i
],
870 isl_poly_copy(rec2
->p
[i
]));
873 if (i
!= rec1
->n
- 1)
875 is_zero
= isl_poly_is_zero(rec1
->p
[i
]);
879 isl_poly_free(rec1
->p
[i
]);
885 poly1
= replace_by_zero(poly1
);
886 else if (rec1
->n
== 1)
887 poly1
= replace_by_constant_term(poly1
);
889 isl_poly_free(poly2
);
893 isl_poly_free(poly1
);
894 isl_poly_free(poly2
);
898 __isl_give isl_poly
*isl_poly_cst_add_isl_int(__isl_take isl_poly
*poly
,
903 poly
= isl_poly_cow(poly
);
907 cst
= isl_poly_as_cst(poly
);
909 isl_int_addmul(cst
->n
, cst
->d
, v
);
914 __isl_give isl_poly
*isl_poly_add_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
919 is_cst
= isl_poly_is_cst(poly
);
921 return isl_poly_free(poly
);
923 return isl_poly_cst_add_isl_int(poly
, v
);
925 poly
= isl_poly_cow(poly
);
926 rec
= isl_poly_as_rec(poly
);
930 rec
->p
[0] = isl_poly_add_isl_int(rec
->p
[0], v
);
940 __isl_give isl_poly
*isl_poly_cst_mul_isl_int(__isl_take isl_poly
*poly
,
946 is_zero
= isl_poly_is_zero(poly
);
948 return isl_poly_free(poly
);
952 poly
= isl_poly_cow(poly
);
956 cst
= isl_poly_as_cst(poly
);
958 isl_int_mul(cst
->n
, cst
->n
, v
);
963 __isl_give isl_poly
*isl_poly_mul_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
969 is_cst
= isl_poly_is_cst(poly
);
971 return isl_poly_free(poly
);
973 return isl_poly_cst_mul_isl_int(poly
, v
);
975 poly
= isl_poly_cow(poly
);
976 rec
= isl_poly_as_rec(poly
);
980 for (i
= 0; i
< rec
->n
; ++i
) {
981 rec
->p
[i
] = isl_poly_mul_isl_int(rec
->p
[i
], v
);
992 /* Multiply the constant polynomial "poly" by "v".
994 static __isl_give isl_poly
*isl_poly_cst_scale_val(__isl_take isl_poly
*poly
,
995 __isl_keep isl_val
*v
)
1000 is_zero
= isl_poly_is_zero(poly
);
1002 return isl_poly_free(poly
);
1006 poly
= isl_poly_cow(poly
);
1010 cst
= isl_poly_as_cst(poly
);
1012 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
1013 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
1014 isl_poly_cst_reduce(cst
);
1019 /* Multiply the polynomial "poly" by "v".
1021 static __isl_give isl_poly
*isl_poly_scale_val(__isl_take isl_poly
*poly
,
1022 __isl_keep isl_val
*v
)
1028 is_cst
= isl_poly_is_cst(poly
);
1030 return isl_poly_free(poly
);
1032 return isl_poly_cst_scale_val(poly
, v
);
1034 poly
= isl_poly_cow(poly
);
1035 rec
= isl_poly_as_rec(poly
);
1039 for (i
= 0; i
< rec
->n
; ++i
) {
1040 rec
->p
[i
] = isl_poly_scale_val(rec
->p
[i
], v
);
1047 isl_poly_free(poly
);
1051 __isl_give isl_poly
*isl_poly_mul_cst(__isl_take isl_poly
*poly1
,
1052 __isl_take isl_poly
*poly2
)
1057 poly1
= isl_poly_cow(poly1
);
1058 if (!poly1
|| !poly2
)
1061 cst1
= isl_poly_as_cst(poly1
);
1062 cst2
= isl_poly_as_cst(poly2
);
1064 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
1065 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
1067 isl_poly_cst_reduce(cst1
);
1069 isl_poly_free(poly2
);
1072 isl_poly_free(poly1
);
1073 isl_poly_free(poly2
);
1077 __isl_give isl_poly
*isl_poly_mul_rec(__isl_take isl_poly
*poly1
,
1078 __isl_take isl_poly
*poly2
)
1082 isl_poly_rec
*res
= NULL
;
1086 rec1
= isl_poly_as_rec(poly1
);
1087 rec2
= isl_poly_as_rec(poly2
);
1090 size
= rec1
->n
+ rec2
->n
- 1;
1091 res
= isl_poly_alloc_rec(poly1
->ctx
, poly1
->var
, size
);
1095 for (i
= 0; i
< rec1
->n
; ++i
) {
1096 res
->p
[i
] = isl_poly_mul(isl_poly_copy(rec2
->p
[0]),
1097 isl_poly_copy(rec1
->p
[i
]));
1102 for (; i
< size
; ++i
) {
1103 res
->p
[i
] = isl_poly_zero(poly1
->ctx
);
1108 for (i
= 0; i
< rec1
->n
; ++i
) {
1109 for (j
= 1; j
< rec2
->n
; ++j
) {
1111 poly
= isl_poly_mul(isl_poly_copy(rec2
->p
[j
]),
1112 isl_poly_copy(rec1
->p
[i
]));
1113 res
->p
[i
+ j
] = isl_poly_sum(res
->p
[i
+ j
], poly
);
1119 isl_poly_free(poly1
);
1120 isl_poly_free(poly2
);
1124 isl_poly_free(poly1
);
1125 isl_poly_free(poly2
);
1126 isl_poly_free(&res
->poly
);
1130 __isl_give isl_poly
*isl_poly_mul(__isl_take isl_poly
*poly1
,
1131 __isl_take isl_poly
*poly2
)
1133 isl_bool is_zero
, is_nan
, is_one
, is_cst
;
1135 if (!poly1
|| !poly2
)
1138 is_nan
= isl_poly_is_nan(poly1
);
1142 isl_poly_free(poly2
);
1146 is_nan
= isl_poly_is_nan(poly2
);
1150 isl_poly_free(poly1
);
1154 is_zero
= isl_poly_is_zero(poly1
);
1158 isl_poly_free(poly2
);
1162 is_zero
= isl_poly_is_zero(poly2
);
1166 isl_poly_free(poly1
);
1170 is_one
= isl_poly_is_one(poly1
);
1174 isl_poly_free(poly1
);
1178 is_one
= isl_poly_is_one(poly2
);
1182 isl_poly_free(poly2
);
1186 if (poly1
->var
< poly2
->var
)
1187 return isl_poly_mul(poly2
, poly1
);
1189 if (poly2
->var
< poly1
->var
) {
1194 is_infty
= isl_poly_is_infty(poly2
);
1195 if (is_infty
>= 0 && !is_infty
)
1196 is_infty
= isl_poly_is_neginfty(poly2
);
1200 isl_ctx
*ctx
= poly1
->ctx
;
1201 isl_poly_free(poly1
);
1202 isl_poly_free(poly2
);
1203 return isl_poly_nan(ctx
);
1205 poly1
= isl_poly_cow(poly1
);
1206 rec
= isl_poly_as_rec(poly1
);
1210 for (i
= 0; i
< rec
->n
; ++i
) {
1211 rec
->p
[i
] = isl_poly_mul(rec
->p
[i
],
1212 isl_poly_copy(poly2
));
1216 isl_poly_free(poly2
);
1220 is_cst
= isl_poly_is_cst(poly1
);
1224 return isl_poly_mul_cst(poly1
, poly2
);
1226 return isl_poly_mul_rec(poly1
, poly2
);
1228 isl_poly_free(poly1
);
1229 isl_poly_free(poly2
);
1233 __isl_give isl_poly
*isl_poly_pow(__isl_take isl_poly
*poly
, unsigned power
)
1243 res
= isl_poly_copy(poly
);
1245 res
= isl_poly_one(poly
->ctx
);
1247 while (power
>>= 1) {
1248 poly
= isl_poly_mul(poly
, isl_poly_copy(poly
));
1250 res
= isl_poly_mul(res
, isl_poly_copy(poly
));
1253 isl_poly_free(poly
);
1257 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1258 unsigned n_div
, __isl_take isl_poly
*poly
)
1260 struct isl_qpolynomial
*qp
= NULL
;
1263 if (!space
|| !poly
)
1266 if (!isl_space_is_set(space
))
1267 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1268 "domain of polynomial should be a set", goto error
);
1270 total
= isl_space_dim(space
, isl_dim_all
);
1272 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1277 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1286 isl_space_free(space
);
1287 isl_poly_free(poly
);
1288 isl_qpolynomial_free(qp
);
1292 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1301 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1303 struct isl_qpolynomial
*dup
;
1308 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1309 isl_poly_copy(qp
->poly
));
1312 isl_mat_free(dup
->div
);
1313 dup
->div
= isl_mat_copy(qp
->div
);
1319 isl_qpolynomial_free(dup
);
1323 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1331 return isl_qpolynomial_dup(qp
);
1334 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1335 __isl_take isl_qpolynomial
*qp
)
1343 isl_space_free(qp
->dim
);
1344 isl_mat_free(qp
->div
);
1345 isl_poly_free(qp
->poly
);
1351 __isl_give isl_poly
*isl_poly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1357 rec
= isl_poly_alloc_rec(ctx
, pos
, 1 + power
);
1360 for (i
= 0; i
< 1 + power
; ++i
) {
1361 rec
->p
[i
] = isl_poly_zero(ctx
);
1366 cst
= isl_poly_as_cst(rec
->p
[power
]);
1367 isl_int_set_si(cst
->n
, 1);
1371 isl_poly_free(&rec
->poly
);
1375 /* r array maps original positions to new positions.
1377 static __isl_give isl_poly
*reorder(__isl_take isl_poly
*poly
, int *r
)
1385 is_cst
= isl_poly_is_cst(poly
);
1387 return isl_poly_free(poly
);
1391 rec
= isl_poly_as_rec(poly
);
1395 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
1397 base
= isl_poly_var_pow(poly
->ctx
, r
[poly
->var
], 1);
1398 res
= reorder(isl_poly_copy(rec
->p
[rec
->n
- 1]), r
);
1400 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1401 res
= isl_poly_mul(res
, isl_poly_copy(base
));
1402 res
= isl_poly_sum(res
, reorder(isl_poly_copy(rec
->p
[i
]), r
));
1405 isl_poly_free(base
);
1406 isl_poly_free(poly
);
1410 isl_poly_free(poly
);
1414 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1415 __isl_keep isl_mat
*div2
)
1420 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1421 div1
->n_col
>= div2
->n_col
,
1422 return isl_bool_error
);
1424 if (div1
->n_row
== div2
->n_row
)
1425 return isl_mat_is_equal(div1
, div2
);
1427 n_row
= div1
->n_row
;
1428 n_col
= div1
->n_col
;
1429 div1
->n_row
= div2
->n_row
;
1430 div1
->n_col
= div2
->n_col
;
1432 equal
= isl_mat_is_equal(div1
, div2
);
1434 div1
->n_row
= n_row
;
1435 div1
->n_col
= n_col
;
1440 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1444 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1445 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1450 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1453 struct isl_div_sort_info
{
1458 static int div_sort_cmp(const void *p1
, const void *p2
)
1460 const struct isl_div_sort_info
*i1
, *i2
;
1461 i1
= (const struct isl_div_sort_info
*) p1
;
1462 i2
= (const struct isl_div_sort_info
*) p2
;
1464 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1467 /* Sort divs and remove duplicates.
1469 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1474 struct isl_div_sort_info
*array
= NULL
;
1475 int *pos
= NULL
, *at
= NULL
;
1476 int *reordering
= NULL
;
1481 if (qp
->div
->n_row
<= 1)
1484 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
1486 return isl_qpolynomial_free(qp
);
1488 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1490 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1491 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1492 len
= qp
->div
->n_col
- 2;
1493 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1494 if (!array
|| !pos
|| !at
|| !reordering
)
1497 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1498 array
[i
].div
= qp
->div
;
1504 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1507 for (i
= 0; i
< div_pos
; ++i
)
1510 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1511 if (pos
[array
[i
].row
] == i
)
1513 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1514 pos
[at
[i
]] = pos
[array
[i
].row
];
1515 at
[pos
[array
[i
].row
]] = at
[i
];
1516 at
[i
] = array
[i
].row
;
1517 pos
[array
[i
].row
] = i
;
1521 for (i
= 0; i
< len
- div_pos
; ++i
) {
1523 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1524 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1525 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1526 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1527 2 + div_pos
+ i
- skip
);
1528 qp
->div
= isl_mat_drop_cols(qp
->div
,
1529 2 + div_pos
+ i
- skip
, 1);
1532 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1535 qp
->poly
= reorder(qp
->poly
, reordering
);
1537 if (!qp
->poly
|| !qp
->div
)
1551 isl_qpolynomial_free(qp
);
1555 static __isl_give isl_poly
*expand(__isl_take isl_poly
*poly
, int *exp
,
1562 is_cst
= isl_poly_is_cst(poly
);
1564 return isl_poly_free(poly
);
1568 if (poly
->var
< first
)
1571 if (exp
[poly
->var
- first
] == poly
->var
- first
)
1574 poly
= isl_poly_cow(poly
);
1578 poly
->var
= exp
[poly
->var
- first
] + first
;
1580 rec
= isl_poly_as_rec(poly
);
1584 for (i
= 0; i
< rec
->n
; ++i
) {
1585 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1592 isl_poly_free(poly
);
1596 static __isl_give isl_qpolynomial
*with_merged_divs(
1597 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1598 __isl_take isl_qpolynomial
*qp2
),
1599 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1603 isl_mat
*div
= NULL
;
1606 qp1
= isl_qpolynomial_cow(qp1
);
1607 qp2
= isl_qpolynomial_cow(qp2
);
1612 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1613 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1615 n_div1
= qp1
->div
->n_row
;
1616 n_div2
= qp2
->div
->n_row
;
1617 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1618 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1619 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1622 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1626 isl_mat_free(qp1
->div
);
1627 qp1
->div
= isl_mat_copy(div
);
1628 isl_mat_free(qp2
->div
);
1629 qp2
->div
= isl_mat_copy(div
);
1631 qp1
->poly
= expand(qp1
->poly
, exp1
, div
->n_col
- div
->n_row
- 2);
1632 qp2
->poly
= expand(qp2
->poly
, exp2
, div
->n_col
- div
->n_row
- 2);
1634 if (!qp1
->poly
|| !qp2
->poly
)
1641 return fn(qp1
, qp2
);
1646 isl_qpolynomial_free(qp1
);
1647 isl_qpolynomial_free(qp2
);
1651 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1652 __isl_take isl_qpolynomial
*qp2
)
1654 isl_bool compatible
;
1656 qp1
= isl_qpolynomial_cow(qp1
);
1661 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1662 return isl_qpolynomial_add(qp2
, qp1
);
1664 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1665 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1669 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1671 qp1
->poly
= isl_poly_sum(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1675 isl_qpolynomial_free(qp2
);
1679 isl_qpolynomial_free(qp1
);
1680 isl_qpolynomial_free(qp2
);
1684 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1685 __isl_keep isl_set
*dom
,
1686 __isl_take isl_qpolynomial
*qp1
,
1687 __isl_take isl_qpolynomial
*qp2
)
1689 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1690 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1694 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1695 __isl_take isl_qpolynomial
*qp2
)
1697 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1700 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1701 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1703 if (isl_int_is_zero(v
))
1706 qp
= isl_qpolynomial_cow(qp
);
1710 qp
->poly
= isl_poly_add_isl_int(qp
->poly
, v
);
1716 isl_qpolynomial_free(qp
);
1721 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1726 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1729 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1730 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1732 if (isl_int_is_one(v
))
1735 if (qp
&& isl_int_is_zero(v
)) {
1736 isl_qpolynomial
*zero
;
1737 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1738 isl_qpolynomial_free(qp
);
1742 qp
= isl_qpolynomial_cow(qp
);
1746 qp
->poly
= isl_poly_mul_isl_int(qp
->poly
, v
);
1752 isl_qpolynomial_free(qp
);
1756 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1757 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1759 return isl_qpolynomial_mul_isl_int(qp
, v
);
1762 /* Multiply "qp" by "v".
1764 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1765 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1770 if (!isl_val_is_rat(v
))
1771 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1772 "expecting rational factor", goto error
);
1774 if (isl_val_is_one(v
)) {
1779 if (isl_val_is_zero(v
)) {
1782 space
= isl_qpolynomial_get_domain_space(qp
);
1783 isl_qpolynomial_free(qp
);
1785 return isl_qpolynomial_zero_on_domain(space
);
1788 qp
= isl_qpolynomial_cow(qp
);
1792 qp
->poly
= isl_poly_scale_val(qp
->poly
, v
);
1794 qp
= isl_qpolynomial_free(qp
);
1800 isl_qpolynomial_free(qp
);
1804 /* Divide "qp" by "v".
1806 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1807 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1812 if (!isl_val_is_rat(v
))
1813 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1814 "expecting rational factor", goto error
);
1815 if (isl_val_is_zero(v
))
1816 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1817 "cannot scale down by zero", goto error
);
1819 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1822 isl_qpolynomial_free(qp
);
1826 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1827 __isl_take isl_qpolynomial
*qp2
)
1829 isl_bool compatible
;
1831 qp1
= isl_qpolynomial_cow(qp1
);
1836 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1837 return isl_qpolynomial_mul(qp2
, qp1
);
1839 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1840 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1844 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1846 qp1
->poly
= isl_poly_mul(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1850 isl_qpolynomial_free(qp2
);
1854 isl_qpolynomial_free(qp1
);
1855 isl_qpolynomial_free(qp2
);
1859 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1862 qp
= isl_qpolynomial_cow(qp
);
1867 qp
->poly
= isl_poly_pow(qp
->poly
, power
);
1873 isl_qpolynomial_free(qp
);
1877 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1878 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1885 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1889 for (i
= 0; i
< pwqp
->n
; ++i
) {
1890 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1892 return isl_pw_qpolynomial_free(pwqp
);
1898 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1899 __isl_take isl_space
*domain
)
1903 return isl_qpolynomial_alloc(domain
, 0, isl_poly_zero(domain
->ctx
));
1906 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1907 __isl_take isl_space
*domain
)
1911 return isl_qpolynomial_alloc(domain
, 0, isl_poly_one(domain
->ctx
));
1914 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1915 __isl_take isl_space
*domain
)
1919 return isl_qpolynomial_alloc(domain
, 0, isl_poly_infty(domain
->ctx
));
1922 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1923 __isl_take isl_space
*domain
)
1927 return isl_qpolynomial_alloc(domain
, 0, isl_poly_neginfty(domain
->ctx
));
1930 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1931 __isl_take isl_space
*domain
)
1935 return isl_qpolynomial_alloc(domain
, 0, isl_poly_nan(domain
->ctx
));
1938 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1939 __isl_take isl_space
*domain
,
1942 struct isl_qpolynomial
*qp
;
1945 qp
= isl_qpolynomial_zero_on_domain(domain
);
1949 cst
= isl_poly_as_cst(qp
->poly
);
1950 isl_int_set(cst
->n
, v
);
1955 isl_bool
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1956 isl_int
*n
, isl_int
*d
)
1962 return isl_bool_error
;
1964 is_cst
= isl_poly_is_cst(qp
->poly
);
1965 if (is_cst
< 0 || !is_cst
)
1968 cst
= isl_poly_as_cst(qp
->poly
);
1970 return isl_bool_error
;
1973 isl_int_set(*n
, cst
->n
);
1975 isl_int_set(*d
, cst
->d
);
1977 return isl_bool_true
;
1980 /* Return the constant term of "poly".
1982 static __isl_give isl_val
*isl_poly_get_constant_val(__isl_keep isl_poly
*poly
)
1990 while ((is_cst
= isl_poly_is_cst(poly
)) == isl_bool_false
) {
1993 rec
= isl_poly_as_rec(poly
);
2001 cst
= isl_poly_as_cst(poly
);
2004 return isl_val_rat_from_isl_int(cst
->poly
.ctx
, cst
->n
, cst
->d
);
2007 /* Return the constant term of "qp".
2009 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
2010 __isl_keep isl_qpolynomial
*qp
)
2015 return isl_poly_get_constant_val(qp
->poly
);
2018 isl_bool
isl_poly_is_affine(__isl_keep isl_poly
*poly
)
2024 return isl_bool_error
;
2027 return isl_bool_true
;
2029 rec
= isl_poly_as_rec(poly
);
2031 return isl_bool_error
;
2034 return isl_bool_false
;
2036 isl_assert(poly
->ctx
, rec
->n
> 1, return isl_bool_error
);
2038 is_cst
= isl_poly_is_cst(rec
->p
[1]);
2039 if (is_cst
< 0 || !is_cst
)
2042 return isl_poly_is_affine(rec
->p
[0]);
2045 isl_bool
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
2048 return isl_bool_error
;
2050 if (qp
->div
->n_row
> 0)
2051 return isl_bool_false
;
2053 return isl_poly_is_affine(qp
->poly
);
2056 static void update_coeff(__isl_keep isl_vec
*aff
,
2057 __isl_keep isl_poly_cst
*cst
, int pos
)
2062 if (isl_int_is_zero(cst
->n
))
2067 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
2068 isl_int_divexact(f
, cst
->d
, gcd
);
2069 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
2070 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
2071 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
2076 int isl_poly_update_affine(__isl_keep isl_poly
*poly
, __isl_keep isl_vec
*aff
)
2084 if (poly
->var
< 0) {
2087 cst
= isl_poly_as_cst(poly
);
2090 update_coeff(aff
, cst
, 0);
2094 rec
= isl_poly_as_rec(poly
);
2097 isl_assert(poly
->ctx
, rec
->n
== 2, return -1);
2099 cst
= isl_poly_as_cst(rec
->p
[1]);
2102 update_coeff(aff
, cst
, 1 + poly
->var
);
2104 return isl_poly_update_affine(rec
->p
[0], aff
);
2107 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
2108 __isl_keep isl_qpolynomial
*qp
)
2116 d
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2117 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
2121 isl_seq_clr(aff
->el
+ 1, 1 + d
);
2122 isl_int_set_si(aff
->el
[0], 1);
2124 if (isl_poly_update_affine(qp
->poly
, aff
) < 0)
2133 /* Compare two quasi-polynomials.
2135 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2136 * than "qp2" and 0 if they are equal.
2138 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2139 __isl_keep isl_qpolynomial
*qp2
)
2150 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2154 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2158 return isl_poly_plain_cmp(qp1
->poly
, qp2
->poly
);
2161 /* Is "qp1" obviously equal to "qp2"?
2163 * NaN is not equal to anything, not even to another NaN.
2165 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2166 __isl_keep isl_qpolynomial
*qp2
)
2171 return isl_bool_error
;
2173 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2174 return isl_bool_false
;
2176 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2177 if (equal
< 0 || !equal
)
2180 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2181 if (equal
< 0 || !equal
)
2184 return isl_poly_is_equal(qp1
->poly
, qp2
->poly
);
2187 static isl_stat
poly_update_den(__isl_keep isl_poly
*poly
, isl_int
*d
)
2193 is_cst
= isl_poly_is_cst(poly
);
2195 return isl_stat_error
;
2198 cst
= isl_poly_as_cst(poly
);
2200 return isl_stat_error
;
2201 isl_int_lcm(*d
, *d
, cst
->d
);
2205 rec
= isl_poly_as_rec(poly
);
2207 return isl_stat_error
;
2209 for (i
= 0; i
< rec
->n
; ++i
)
2210 poly_update_den(rec
->p
[i
], d
);
2215 __isl_give isl_val
*isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
)
2221 d
= isl_val_one(isl_qpolynomial_get_ctx(qp
));
2224 if (poly_update_den(qp
->poly
, &d
->n
) < 0)
2225 return isl_val_free(d
);
2229 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2230 __isl_take isl_space
*domain
, int pos
, int power
)
2232 struct isl_ctx
*ctx
;
2239 return isl_qpolynomial_alloc(domain
, 0,
2240 isl_poly_var_pow(ctx
, pos
, power
));
2243 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2244 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2246 if (isl_space_check_is_set(domain
) < 0)
2248 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2251 pos
+= isl_space_offset(domain
, type
);
2253 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2255 isl_space_free(domain
);
2259 __isl_give isl_poly
*isl_poly_subs(__isl_take isl_poly
*poly
,
2260 unsigned first
, unsigned n
, __isl_keep isl_poly
**subs
)
2265 isl_poly
*base
, *res
;
2267 is_cst
= isl_poly_is_cst(poly
);
2269 return isl_poly_free(poly
);
2273 if (poly
->var
< first
)
2276 rec
= isl_poly_as_rec(poly
);
2280 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
2282 if (poly
->var
>= first
+ n
)
2283 base
= isl_poly_var_pow(poly
->ctx
, poly
->var
, 1);
2285 base
= isl_poly_copy(subs
[poly
->var
- first
]);
2287 res
= isl_poly_subs(isl_poly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2288 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2290 t
= isl_poly_subs(isl_poly_copy(rec
->p
[i
]), first
, n
, subs
);
2291 res
= isl_poly_mul(res
, isl_poly_copy(base
));
2292 res
= isl_poly_sum(res
, t
);
2295 isl_poly_free(base
);
2296 isl_poly_free(poly
);
2300 isl_poly_free(poly
);
2304 __isl_give isl_poly
*isl_poly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2305 isl_int denom
, unsigned len
)
2310 isl_assert(ctx
, len
>= 1, return NULL
);
2312 poly
= isl_poly_rat_cst(ctx
, f
[0], denom
);
2313 for (i
= 0; i
< len
- 1; ++i
) {
2317 if (isl_int_is_zero(f
[1 + i
]))
2320 c
= isl_poly_rat_cst(ctx
, f
[1 + i
], denom
);
2321 t
= isl_poly_var_pow(ctx
, i
, 1);
2322 t
= isl_poly_mul(c
, t
);
2323 poly
= isl_poly_sum(poly
, t
);
2329 /* Remove common factor of non-constant terms and denominator.
2331 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2333 isl_ctx
*ctx
= qp
->div
->ctx
;
2334 unsigned total
= qp
->div
->n_col
- 2;
2336 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2337 isl_int_gcd(ctx
->normalize_gcd
,
2338 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2339 if (isl_int_is_one(ctx
->normalize_gcd
))
2342 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2343 ctx
->normalize_gcd
, total
);
2344 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2345 ctx
->normalize_gcd
);
2346 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2347 ctx
->normalize_gcd
);
2350 /* Replace the integer division identified by "div" by the polynomial "s".
2351 * The integer division is assumed not to appear in the definition
2352 * of any other integer divisions.
2354 static __isl_give isl_qpolynomial
*substitute_div(
2355 __isl_take isl_qpolynomial
*qp
, int div
, __isl_take isl_poly
*s
)
2365 qp
= isl_qpolynomial_cow(qp
);
2369 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2372 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ div
, 1, &s
);
2376 ctx
= isl_qpolynomial_get_ctx(qp
);
2377 reordering
= isl_alloc_array(ctx
, int, div_pos
+ qp
->div
->n_row
);
2380 for (i
= 0; i
< div_pos
+ div
; ++i
)
2382 for (i
= div_pos
+ div
+ 1; i
< div_pos
+ qp
->div
->n_row
; ++i
)
2383 reordering
[i
] = i
- 1;
2384 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2385 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + div_pos
+ div
, 1);
2386 qp
->poly
= reorder(qp
->poly
, reordering
);
2389 if (!qp
->poly
|| !qp
->div
)
2395 isl_qpolynomial_free(qp
);
2400 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2401 * divisions because d is equal to 1 by their definition, i.e., e.
2403 static __isl_give isl_qpolynomial
*substitute_non_divs(
2404 __isl_take isl_qpolynomial
*qp
)
2410 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2412 return isl_qpolynomial_free(qp
);
2414 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2415 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2417 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2418 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
2420 isl_seq_combine(qp
->div
->row
[j
] + 1,
2421 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2422 qp
->div
->row
[j
][2 + div_pos
+ i
],
2423 qp
->div
->row
[i
] + 1, 1 + div_pos
+ i
);
2424 isl_int_set_si(qp
->div
->row
[j
][2 + div_pos
+ i
], 0);
2425 normalize_div(qp
, j
);
2427 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2428 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2429 qp
= substitute_div(qp
, i
, s
);
2436 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2437 * with d the denominator. When replacing the coefficient e of x by
2438 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2439 * inside the division, so we need to add floor(e/d) * x outside.
2440 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2441 * to adjust the coefficient of x in each later div that depends on the
2442 * current div "div" and also in the affine expressions in the rows of "mat"
2443 * (if they too depend on "div").
2445 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2446 __isl_keep isl_mat
**mat
)
2450 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2453 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2454 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2455 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2457 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2458 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2459 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2460 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2461 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2462 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2464 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2465 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2471 /* Check if the last non-zero coefficient is bigger that half of the
2472 * denominator. If so, we will invert the div to further reduce the number
2473 * of distinct divs that may appear.
2474 * If the last non-zero coefficient is exactly half the denominator,
2475 * then we continue looking for earlier coefficients that are bigger
2476 * than half the denominator.
2478 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2483 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2484 if (isl_int_is_zero(div
->row
[row
][i
]))
2486 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2487 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2488 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2498 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2499 * We only invert the coefficients of e (and the coefficient of q in
2500 * later divs and in the rows of "mat"). After calling this function, the
2501 * coefficients of e should be reduced again.
2503 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2504 __isl_keep isl_mat
**mat
)
2506 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2508 isl_seq_neg(qp
->div
->row
[div
] + 1,
2509 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2510 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2511 isl_int_add(qp
->div
->row
[div
][1],
2512 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2513 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2514 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2515 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2518 /* Reduce all divs of "qp" to have coefficients
2519 * in the interval [0, d-1], with d the denominator and such that the
2520 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2521 * The modifications to the integer divisions need to be reflected
2522 * in the factors of the polynomial that refer to the original
2523 * integer divisions. To this end, the modifications are collected
2524 * as a set of affine expressions and then plugged into the polynomial.
2526 * After the reduction, some divs may have become redundant or identical,
2527 * so we call substitute_non_divs and sort_divs. If these functions
2528 * eliminate divs or merge two or more divs into one, the coefficients
2529 * of the enclosing divs may have to be reduced again, so we call
2530 * ourselves recursively if the number of divs decreases.
2532 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2538 unsigned o_div
, n_div
, total
;
2543 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2544 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2545 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2546 ctx
= isl_qpolynomial_get_ctx(qp
);
2547 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2549 for (i
= 0; i
< n_div
; ++i
)
2550 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2552 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2553 normalize_div(qp
, i
);
2554 reduce_div(qp
, i
, &mat
);
2555 if (needs_invert(qp
->div
, i
)) {
2556 invert_div(qp
, i
, &mat
);
2557 reduce_div(qp
, i
, &mat
);
2563 s
= isl_alloc_array(ctx
, struct isl_poly
*, n_div
);
2566 for (i
= 0; i
< n_div
; ++i
)
2567 s
[i
] = isl_poly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2569 qp
->poly
= isl_poly_subs(qp
->poly
, o_div
- 1, n_div
, s
);
2570 for (i
= 0; i
< n_div
; ++i
)
2571 isl_poly_free(s
[i
]);
2578 qp
= substitute_non_divs(qp
);
2580 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2581 return reduce_divs(qp
);
2585 isl_qpolynomial_free(qp
);
2590 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2591 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2593 struct isl_qpolynomial
*qp
;
2596 qp
= isl_qpolynomial_zero_on_domain(domain
);
2600 cst
= isl_poly_as_cst(qp
->poly
);
2601 isl_int_set(cst
->n
, n
);
2602 isl_int_set(cst
->d
, d
);
2607 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2609 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2610 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2612 isl_qpolynomial
*qp
;
2615 qp
= isl_qpolynomial_zero_on_domain(domain
);
2619 cst
= isl_poly_as_cst(qp
->poly
);
2620 isl_int_set(cst
->n
, val
->n
);
2621 isl_int_set(cst
->d
, val
->d
);
2627 isl_qpolynomial_free(qp
);
2631 static isl_stat
poly_set_active(__isl_keep isl_poly
*poly
, int *active
, int d
)
2637 is_cst
= isl_poly_is_cst(poly
);
2639 return isl_stat_error
;
2644 active
[poly
->var
] = 1;
2646 rec
= isl_poly_as_rec(poly
);
2647 for (i
= 0; i
< rec
->n
; ++i
)
2648 if (poly_set_active(rec
->p
[i
], active
, d
) < 0)
2649 return isl_stat_error
;
2654 static isl_stat
set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2660 space
= isl_qpolynomial_peek_domain_space(qp
);
2661 if (!space
|| !active
)
2662 return isl_stat_error
;
2664 d
= isl_space_dim(space
, isl_dim_all
);
2665 for (i
= 0; i
< d
; ++i
)
2666 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2667 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2673 return poly_set_active(qp
->poly
, active
, d
);
2677 #define TYPE isl_qpolynomial
2679 #include "check_type_range_templ.c"
2681 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2682 enum isl_dim_type type
, unsigned first
, unsigned n
)
2686 isl_bool involves
= isl_bool_false
;
2691 return isl_bool_error
;
2693 return isl_bool_false
;
2695 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2696 return isl_bool_error
;
2697 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2698 type
== isl_dim_in
, return isl_bool_error
);
2700 space
= isl_qpolynomial_peek_domain_space(qp
);
2701 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2702 isl_space_dim(space
, isl_dim_all
));
2703 if (set_active(qp
, active
) < 0)
2706 offset
= isl_qpolynomial_domain_var_offset(qp
, domain_type(type
));
2710 for (i
= 0; i
< n
; ++i
)
2711 if (active
[first
+ i
]) {
2712 involves
= isl_bool_true
;
2721 return isl_bool_error
;
2724 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2725 * of the divs that do appear in the quasi-polynomial.
2727 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2728 __isl_take isl_qpolynomial
*qp
)
2735 int *reordering
= NULL
;
2742 if (qp
->div
->n_row
== 0)
2745 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2747 return isl_qpolynomial_free(qp
);
2748 len
= qp
->div
->n_col
- 2;
2749 ctx
= isl_qpolynomial_get_ctx(qp
);
2750 active
= isl_calloc_array(ctx
, int, len
);
2754 if (poly_set_active(qp
->poly
, active
, len
) < 0)
2757 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2758 if (!active
[div_pos
+ i
]) {
2762 for (j
= 0; j
< i
; ++j
) {
2763 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ j
]))
2765 active
[div_pos
+ j
] = 1;
2775 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2779 for (i
= 0; i
< div_pos
; ++i
)
2783 n_div
= qp
->div
->n_row
;
2784 for (i
= 0; i
< n_div
; ++i
) {
2785 if (!active
[div_pos
+ i
]) {
2786 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2787 qp
->div
= isl_mat_drop_cols(qp
->div
,
2788 2 + div_pos
+ i
- skip
, 1);
2791 reordering
[div_pos
+ i
] = div_pos
+ i
- skip
;
2794 qp
->poly
= reorder(qp
->poly
, reordering
);
2796 if (!qp
->poly
|| !qp
->div
)
2806 isl_qpolynomial_free(qp
);
2810 __isl_give isl_poly
*isl_poly_drop(__isl_take isl_poly
*poly
,
2811 unsigned first
, unsigned n
)
2818 if (n
== 0 || poly
->var
< 0 || poly
->var
< first
)
2820 if (poly
->var
< first
+ n
) {
2821 poly
= replace_by_constant_term(poly
);
2822 return isl_poly_drop(poly
, first
, n
);
2824 poly
= isl_poly_cow(poly
);
2828 rec
= isl_poly_as_rec(poly
);
2832 for (i
= 0; i
< rec
->n
; ++i
) {
2833 rec
->p
[i
] = isl_poly_drop(rec
->p
[i
], first
, n
);
2840 isl_poly_free(poly
);
2844 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2845 __isl_take isl_qpolynomial
*qp
,
2846 enum isl_dim_type type
, unsigned pos
, const char *s
)
2848 qp
= isl_qpolynomial_cow(qp
);
2851 if (type
== isl_dim_out
)
2852 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2853 "cannot set name of output/set dimension",
2854 return isl_qpolynomial_free(qp
));
2855 type
= domain_type(type
);
2856 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2861 isl_qpolynomial_free(qp
);
2865 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2866 __isl_take isl_qpolynomial
*qp
,
2867 enum isl_dim_type type
, unsigned first
, unsigned n
)
2873 if (type
== isl_dim_out
)
2874 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2875 "cannot drop output/set dimension",
2877 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2878 return isl_qpolynomial_free(qp
);
2879 type
= domain_type(type
);
2880 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2883 qp
= isl_qpolynomial_cow(qp
);
2887 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2888 type
== isl_dim_set
, goto error
);
2890 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2894 offset
= isl_qpolynomial_domain_var_offset(qp
, type
);
2899 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2903 qp
->poly
= isl_poly_drop(qp
->poly
, first
, n
);
2909 isl_qpolynomial_free(qp
);
2913 /* Project the domain of the quasi-polynomial onto its parameter space.
2914 * The quasi-polynomial may not involve any of the domain dimensions.
2916 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2917 __isl_take isl_qpolynomial
*qp
)
2923 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2924 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2926 return isl_qpolynomial_free(qp
);
2928 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2929 "polynomial involves some of the domain dimensions",
2930 return isl_qpolynomial_free(qp
));
2931 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2932 space
= isl_qpolynomial_get_domain_space(qp
);
2933 space
= isl_space_params(space
);
2934 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2938 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2939 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2949 if (eq
->n_eq
== 0) {
2950 isl_basic_set_free(eq
);
2954 qp
= isl_qpolynomial_cow(qp
);
2957 qp
->div
= isl_mat_cow(qp
->div
);
2961 total
= isl_basic_set_offset(eq
, isl_dim_div
);
2963 isl_int_init(denom
);
2964 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2965 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2966 if (j
< 0 || j
== 0 || j
>= total
)
2969 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2970 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2972 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2973 &qp
->div
->row
[k
][0]);
2974 normalize_div(qp
, k
);
2977 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2978 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2979 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2980 isl_int_set_si(eq
->eq
[i
][j
], 0);
2982 poly
= isl_poly_from_affine(qp
->dim
->ctx
,
2983 eq
->eq
[i
], denom
, total
);
2984 qp
->poly
= isl_poly_subs(qp
->poly
, j
- 1, 1, &poly
);
2985 isl_poly_free(poly
);
2987 isl_int_clear(denom
);
2992 isl_basic_set_free(eq
);
2994 qp
= substitute_non_divs(qp
);
2999 isl_basic_set_free(eq
);
3000 isl_qpolynomial_free(qp
);
3004 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3006 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
3007 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
3011 if (qp
->div
->n_row
> 0)
3012 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
3013 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
3015 isl_basic_set_free(eq
);
3016 isl_qpolynomial_free(qp
);
3020 /* Look for equalities among the variables shared by context and qp
3021 * and the integer divisions of qp, if any.
3022 * The equalities are then used to eliminate variables and/or integer
3023 * divisions from qp.
3025 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
3026 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3028 isl_local_space
*ls
;
3031 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3032 context
= isl_local_space_lift_set(ls
, context
);
3034 aff
= isl_set_affine_hull(context
);
3035 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
3038 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
3039 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3041 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
3042 isl_set
*dom_context
= isl_set_universe(space
);
3043 dom_context
= isl_set_intersect_params(dom_context
, context
);
3044 return isl_qpolynomial_gist(qp
, dom_context
);
3047 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
3048 __isl_take isl_qpolynomial
*qp
)
3054 if (isl_qpolynomial_is_zero(qp
)) {
3055 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
3056 isl_qpolynomial_free(qp
);
3057 return isl_pw_qpolynomial_zero(dim
);
3060 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
3061 return isl_pw_qpolynomial_alloc(dom
, qp
);
3064 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3067 #define PW isl_pw_qpolynomial
3069 #define EL isl_qpolynomial
3071 #define EL_IS_ZERO is_zero
3075 #define IS_ZERO is_zero
3078 #undef DEFAULT_IS_ZERO
3079 #define DEFAULT_IS_ZERO 1
3083 #include <isl_pw_templ.c>
3084 #include <isl_pw_eval.c>
3087 #define BASE pw_qpolynomial
3089 #include <isl_union_single.c>
3090 #include <isl_union_eval.c>
3091 #include <isl_union_neg.c>
3093 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
3101 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
3104 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
3107 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
3108 __isl_take isl_pw_qpolynomial
*pwqp1
,
3109 __isl_take isl_pw_qpolynomial
*pwqp2
)
3111 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
3114 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
3115 __isl_take isl_pw_qpolynomial
*pwqp1
,
3116 __isl_take isl_pw_qpolynomial
*pwqp2
)
3119 struct isl_pw_qpolynomial
*res
;
3121 if (!pwqp1
|| !pwqp2
)
3124 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3127 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3128 isl_pw_qpolynomial_free(pwqp2
);
3132 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3133 isl_pw_qpolynomial_free(pwqp1
);
3137 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3138 isl_pw_qpolynomial_free(pwqp1
);
3142 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3143 isl_pw_qpolynomial_free(pwqp2
);
3147 n
= pwqp1
->n
* pwqp2
->n
;
3148 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3150 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3151 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3152 struct isl_set
*common
;
3153 struct isl_qpolynomial
*prod
;
3154 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3155 isl_set_copy(pwqp2
->p
[j
].set
));
3156 if (isl_set_plain_is_empty(common
)) {
3157 isl_set_free(common
);
3161 prod
= isl_qpolynomial_mul(
3162 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3163 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3165 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3169 isl_pw_qpolynomial_free(pwqp1
);
3170 isl_pw_qpolynomial_free(pwqp2
);
3174 isl_pw_qpolynomial_free(pwqp1
);
3175 isl_pw_qpolynomial_free(pwqp2
);
3179 __isl_give isl_val
*isl_poly_eval(__isl_take isl_poly
*poly
,
3180 __isl_take isl_vec
*vec
)
3188 is_cst
= isl_poly_is_cst(poly
);
3193 res
= isl_poly_get_constant_val(poly
);
3194 isl_poly_free(poly
);
3198 rec
= isl_poly_as_rec(poly
);
3202 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
3204 base
= isl_val_rat_from_isl_int(poly
->ctx
,
3205 vec
->el
[1 + poly
->var
], vec
->el
[0]);
3207 res
= isl_poly_eval(isl_poly_copy(rec
->p
[rec
->n
- 1]),
3210 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3211 res
= isl_val_mul(res
, isl_val_copy(base
));
3212 res
= isl_val_add(res
, isl_poly_eval(isl_poly_copy(rec
->p
[i
]),
3213 isl_vec_copy(vec
)));
3217 isl_poly_free(poly
);
3221 isl_poly_free(poly
);
3226 /* Evaluate "qp" in the void point "pnt".
3227 * In particular, return the value NaN.
3229 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3230 __isl_take isl_point
*pnt
)
3234 ctx
= isl_point_get_ctx(pnt
);
3235 isl_qpolynomial_free(qp
);
3236 isl_point_free(pnt
);
3237 return isl_val_nan(ctx
);
3240 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3241 __isl_take isl_point
*pnt
)
3249 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3250 is_void
= isl_point_is_void(pnt
);
3254 return eval_void(qp
, pnt
);
3256 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3258 v
= isl_poly_eval(isl_poly_copy(qp
->poly
), ext
);
3260 isl_qpolynomial_free(qp
);
3261 isl_point_free(pnt
);
3265 isl_qpolynomial_free(qp
);
3266 isl_point_free(pnt
);
3270 int isl_poly_cmp(__isl_keep isl_poly_cst
*cst1
, __isl_keep isl_poly_cst
*cst2
)
3275 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3276 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3277 cmp
= isl_int_sgn(t
);
3282 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3283 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3284 unsigned first
, unsigned n
)
3292 if (type
== isl_dim_out
)
3293 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3294 "cannot insert output/set dimensions",
3296 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3297 return isl_qpolynomial_free(qp
);
3298 type
= domain_type(type
);
3299 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3302 qp
= isl_qpolynomial_cow(qp
);
3306 g_pos
= pos(qp
->dim
, type
) + first
;
3308 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3312 total
= qp
->div
->n_col
- 2;
3313 if (total
> g_pos
) {
3315 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3318 for (i
= 0; i
< total
- g_pos
; ++i
)
3320 qp
->poly
= expand(qp
->poly
, exp
, g_pos
);
3326 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3332 isl_qpolynomial_free(qp
);
3336 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3337 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3341 pos
= isl_qpolynomial_dim(qp
, type
);
3343 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3346 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3347 __isl_take isl_pw_qpolynomial
*pwqp
,
3348 enum isl_dim_type type
, unsigned n
)
3352 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3354 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3357 static int *reordering_move(isl_ctx
*ctx
,
3358 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3363 reordering
= isl_alloc_array(ctx
, int, len
);
3368 for (i
= 0; i
< dst
; ++i
)
3370 for (i
= 0; i
< n
; ++i
)
3371 reordering
[src
+ i
] = dst
+ i
;
3372 for (i
= 0; i
< src
- dst
; ++i
)
3373 reordering
[dst
+ i
] = dst
+ n
+ i
;
3374 for (i
= 0; i
< len
- src
- n
; ++i
)
3375 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3377 for (i
= 0; i
< src
; ++i
)
3379 for (i
= 0; i
< n
; ++i
)
3380 reordering
[src
+ i
] = dst
+ i
;
3381 for (i
= 0; i
< dst
- src
; ++i
)
3382 reordering
[src
+ n
+ i
] = src
+ i
;
3383 for (i
= 0; i
< len
- dst
- n
; ++i
)
3384 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3390 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3391 __isl_take isl_qpolynomial
*qp
,
3392 enum isl_dim_type dst_type
, unsigned dst_pos
,
3393 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3402 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3403 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3404 "cannot move output/set dimension",
3406 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3407 return isl_qpolynomial_free(qp
);
3408 if (dst_type
== isl_dim_in
)
3409 dst_type
= isl_dim_set
;
3410 if (src_type
== isl_dim_in
)
3411 src_type
= isl_dim_set
;
3414 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3415 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3418 qp
= isl_qpolynomial_cow(qp
);
3422 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3423 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3424 if (dst_type
> src_type
)
3427 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3434 reordering
= reordering_move(qp
->dim
->ctx
,
3435 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3439 qp
->poly
= reorder(qp
->poly
, reordering
);
3444 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3450 isl_qpolynomial_free(qp
);
3454 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3455 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3459 space
= isl_space_domain(space
);
3463 poly
= isl_poly_from_affine(space
->ctx
, f
, denom
,
3464 1 + isl_space_dim(space
, isl_dim_all
));
3466 return isl_qpolynomial_alloc(space
, 0, poly
);
3469 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3473 isl_qpolynomial
*qp
;
3478 ctx
= isl_aff_get_ctx(aff
);
3479 poly
= isl_poly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3482 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3483 aff
->ls
->div
->n_row
, poly
);
3487 isl_mat_free(qp
->div
);
3488 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3489 qp
->div
= isl_mat_cow(qp
->div
);
3494 qp
= reduce_divs(qp
);
3495 qp
= remove_redundant_divs(qp
);
3499 return isl_qpolynomial_free(qp
);
3502 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3503 __isl_take isl_pw_aff
*pwaff
)
3506 isl_pw_qpolynomial
*pwqp
;
3511 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3514 for (i
= 0; i
< pwaff
->n
; ++i
) {
3516 isl_qpolynomial
*qp
;
3518 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3519 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3520 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3523 isl_pw_aff_free(pwaff
);
3527 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3528 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3532 aff
= isl_constraint_get_bound(c
, type
, pos
);
3533 isl_constraint_free(c
);
3534 return isl_qpolynomial_from_aff(aff
);
3537 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3538 * in "qp" by subs[i].
3540 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3541 __isl_take isl_qpolynomial
*qp
,
3542 enum isl_dim_type type
, unsigned first
, unsigned n
,
3543 __isl_keep isl_qpolynomial
**subs
)
3551 qp
= isl_qpolynomial_cow(qp
);
3555 if (type
== isl_dim_out
)
3556 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3557 "cannot substitute output/set dimension",
3559 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3560 return isl_qpolynomial_free(qp
);
3561 type
= domain_type(type
);
3563 for (i
= 0; i
< n
; ++i
)
3567 for (i
= 0; i
< n
; ++i
)
3568 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3571 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3572 for (i
= 0; i
< n
; ++i
)
3573 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3575 first
+= pos(qp
->dim
, type
);
3577 polys
= isl_alloc_array(qp
->dim
->ctx
, struct isl_poly
*, n
);
3580 for (i
= 0; i
< n
; ++i
)
3581 polys
[i
] = subs
[i
]->poly
;
3583 qp
->poly
= isl_poly_subs(qp
->poly
, first
, n
, polys
);
3592 isl_qpolynomial_free(qp
);
3596 /* Extend "bset" with extra set dimensions for each integer division
3597 * in "qp" and then call "fn" with the extended bset and the polynomial
3598 * that results from replacing each of the integer divisions by the
3599 * corresponding extra set dimension.
3601 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3602 __isl_keep isl_basic_set
*bset
,
3603 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3604 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3607 isl_local_space
*ls
;
3608 isl_qpolynomial
*poly
;
3611 return isl_stat_error
;
3612 if (qp
->div
->n_row
== 0)
3613 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3616 space
= isl_space_copy(qp
->dim
);
3617 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3618 poly
= isl_qpolynomial_alloc(space
, 0, isl_poly_copy(qp
->poly
));
3619 bset
= isl_basic_set_copy(bset
);
3620 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3621 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3623 return fn(bset
, poly
, user
);
3626 /* Return total degree in variables first (inclusive) up to last (exclusive).
3628 int isl_poly_degree(__isl_keep isl_poly
*poly
, int first
, int last
)
3632 isl_bool is_zero
, is_cst
;
3635 is_zero
= isl_poly_is_zero(poly
);
3640 is_cst
= isl_poly_is_cst(poly
);
3643 if (is_cst
|| poly
->var
< first
)
3646 rec
= isl_poly_as_rec(poly
);
3650 for (i
= 0; i
< rec
->n
; ++i
) {
3653 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3658 d
= isl_poly_degree(rec
->p
[i
], first
, last
);
3659 if (poly
->var
< last
)
3668 /* Return total degree in set variables.
3670 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3678 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3679 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3680 return isl_poly_degree(poly
->poly
, ovar
, ovar
+ nvar
);
3683 __isl_give isl_poly
*isl_poly_coeff(__isl_keep isl_poly
*poly
,
3684 unsigned pos
, int deg
)
3690 is_cst
= isl_poly_is_cst(poly
);
3693 if (is_cst
|| poly
->var
< pos
) {
3695 return isl_poly_copy(poly
);
3697 return isl_poly_zero(poly
->ctx
);
3700 rec
= isl_poly_as_rec(poly
);
3704 if (poly
->var
== pos
) {
3706 return isl_poly_copy(rec
->p
[deg
]);
3708 return isl_poly_zero(poly
->ctx
);
3711 poly
= isl_poly_copy(poly
);
3712 poly
= isl_poly_cow(poly
);
3713 rec
= isl_poly_as_rec(poly
);
3717 for (i
= 0; i
< rec
->n
; ++i
) {
3719 t
= isl_poly_coeff(rec
->p
[i
], pos
, deg
);
3722 isl_poly_free(rec
->p
[i
]);
3728 isl_poly_free(poly
);
3732 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3734 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3735 __isl_keep isl_qpolynomial
*qp
,
3736 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3745 if (type
== isl_dim_out
)
3746 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3747 "output/set dimension does not have a coefficient",
3749 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
3751 type
= domain_type(type
);
3753 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3754 poly
= isl_poly_coeff(qp
->poly
, g_pos
, deg
);
3756 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
),
3757 qp
->div
->n_row
, poly
);
3760 isl_mat_free(c
->div
);
3761 c
->div
= isl_mat_copy(qp
->div
);
3766 isl_qpolynomial_free(c
);
3770 /* Homogenize the polynomial in the variables first (inclusive) up to
3771 * last (exclusive) by inserting powers of variable first.
3772 * Variable first is assumed not to appear in the input.
3774 __isl_give isl_poly
*isl_poly_homogenize(__isl_take isl_poly
*poly
, int deg
,
3775 int target
, int first
, int last
)
3778 isl_bool is_zero
, is_cst
;
3781 is_zero
= isl_poly_is_zero(poly
);
3783 return isl_poly_free(poly
);
3788 is_cst
= isl_poly_is_cst(poly
);
3790 return isl_poly_free(poly
);
3791 if (is_cst
|| poly
->var
< first
) {
3794 hom
= isl_poly_var_pow(poly
->ctx
, first
, target
- deg
);
3797 rec
= isl_poly_as_rec(hom
);
3798 rec
->p
[target
- deg
] = isl_poly_mul(rec
->p
[target
- deg
], poly
);
3803 poly
= isl_poly_cow(poly
);
3804 rec
= isl_poly_as_rec(poly
);
3808 for (i
= 0; i
< rec
->n
; ++i
) {
3809 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3811 return isl_poly_free(poly
);
3814 rec
->p
[i
] = isl_poly_homogenize(rec
->p
[i
],
3815 poly
->var
< last
? deg
+ i
: i
, target
,
3823 isl_poly_free(poly
);
3827 /* Homogenize the polynomial in the set variables by introducing
3828 * powers of an extra set variable at position 0.
3830 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3831 __isl_take isl_qpolynomial
*poly
)
3835 int deg
= isl_qpolynomial_degree(poly
);
3840 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3841 poly
= isl_qpolynomial_cow(poly
);
3845 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3846 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3847 poly
->poly
= isl_poly_homogenize(poly
->poly
, 0, deg
, ovar
, ovar
+ nvar
);
3853 isl_qpolynomial_free(poly
);
3857 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3858 __isl_take isl_mat
*div
)
3866 n
= isl_space_dim(space
, isl_dim_all
) + div
->n_row
;
3868 term
= isl_calloc(space
->ctx
, struct isl_term
,
3869 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3876 isl_int_init(term
->n
);
3877 isl_int_init(term
->d
);
3881 isl_space_free(space
);
3886 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3895 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3904 total
= isl_term_dim(term
, isl_dim_all
);
3906 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3910 isl_int_set(dup
->n
, term
->n
);
3911 isl_int_set(dup
->d
, term
->d
);
3913 for (i
= 0; i
< total
; ++i
)
3914 dup
->pow
[i
] = term
->pow
[i
];
3919 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3927 return isl_term_dup(term
);
3930 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3935 if (--term
->ref
> 0)
3938 isl_space_free(term
->dim
);
3939 isl_mat_free(term
->div
);
3940 isl_int_clear(term
->n
);
3941 isl_int_clear(term
->d
);
3947 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3955 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3956 case isl_dim_div
: return term
->div
->n_row
;
3957 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3963 /* Return the space of "term".
3965 static __isl_keep isl_space
*isl_term_peek_space(__isl_keep isl_term
*term
)
3967 return term
? term
->dim
: NULL
;
3970 /* Return the offset of the first variable of type "type" within
3971 * the variables of "term".
3973 static int isl_term_offset(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3977 space
= isl_term_peek_space(term
);
3983 case isl_dim_set
: return isl_space_offset(space
, type
);
3984 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
3986 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
3987 "invalid dimension type", return -1);
3991 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3993 return term
? term
->dim
->ctx
: NULL
;
3996 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
4000 isl_int_set(*n
, term
->n
);
4003 /* Return the coefficient of the term "term".
4005 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
4010 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
4015 #define TYPE isl_term
4017 #include "check_type_range_templ.c"
4019 int isl_term_get_exp(__isl_keep isl_term
*term
,
4020 enum isl_dim_type type
, unsigned pos
)
4024 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
4026 offset
= isl_term_offset(term
, type
);
4030 return term
->pow
[offset
+ pos
];
4033 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
4035 isl_local_space
*ls
;
4038 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
4041 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
4042 isl_mat_copy(term
->div
));
4043 aff
= isl_aff_alloc(ls
);
4047 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
4049 aff
= isl_aff_normalize(aff
);
4054 __isl_give isl_term
*isl_poly_foreach_term(__isl_keep isl_poly
*poly
,
4055 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
4056 __isl_take isl_term
*term
, void *user
)
4059 isl_bool is_zero
, is_bad
, is_cst
;
4062 is_zero
= isl_poly_is_zero(poly
);
4063 if (is_zero
< 0 || !term
)
4069 is_cst
= isl_poly_is_cst(poly
);
4070 is_bad
= isl_poly_is_nan(poly
);
4071 if (is_bad
>= 0 && !is_bad
)
4072 is_bad
= isl_poly_is_infty(poly
);
4073 if (is_bad
>= 0 && !is_bad
)
4074 is_bad
= isl_poly_is_neginfty(poly
);
4075 if (is_cst
< 0 || is_bad
< 0)
4076 return isl_term_free(term
);
4078 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4079 "cannot handle NaN/infty polynomial",
4080 return isl_term_free(term
));
4084 cst
= isl_poly_as_cst(poly
);
4087 term
= isl_term_cow(term
);
4090 isl_int_set(term
->n
, cst
->n
);
4091 isl_int_set(term
->d
, cst
->d
);
4092 if (fn(isl_term_copy(term
), user
) < 0)
4097 rec
= isl_poly_as_rec(poly
);
4101 for (i
= 0; i
< rec
->n
; ++i
) {
4102 term
= isl_term_cow(term
);
4105 term
->pow
[poly
->var
] = i
;
4106 term
= isl_poly_foreach_term(rec
->p
[i
], fn
, term
, user
);
4110 term
->pow
[poly
->var
] = 0;
4114 isl_term_free(term
);
4118 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
4119 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
4124 return isl_stat_error
;
4126 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
4128 return isl_stat_error
;
4130 term
= isl_poly_foreach_term(qp
->poly
, fn
, term
, user
);
4132 isl_term_free(term
);
4134 return term
? isl_stat_ok
: isl_stat_error
;
4137 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
4140 isl_qpolynomial
*qp
;
4146 n
= isl_term_dim(term
, isl_dim_all
);
4148 poly
= isl_poly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4149 for (i
= 0; i
< n
; ++i
) {
4152 poly
= isl_poly_mul(poly
,
4153 isl_poly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4156 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
),
4157 term
->div
->n_row
, poly
);
4160 isl_mat_free(qp
->div
);
4161 qp
->div
= isl_mat_copy(term
->div
);
4165 isl_term_free(term
);
4168 isl_qpolynomial_free(qp
);
4169 isl_term_free(term
);
4173 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4174 __isl_take isl_space
*space
)
4183 if (isl_space_is_equal(qp
->dim
, space
)) {
4184 isl_space_free(space
);
4188 qp
= isl_qpolynomial_cow(qp
);
4192 extra
= isl_space_dim(space
, isl_dim_set
) -
4193 isl_qpolynomial_domain_dim(qp
, isl_dim_set
);
4194 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4195 if (qp
->div
->n_row
) {
4198 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4201 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4203 qp
->poly
= expand(qp
->poly
, exp
, total
);
4208 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4211 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4212 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4214 isl_space_free(qp
->dim
);
4219 isl_space_free(space
);
4220 isl_qpolynomial_free(qp
);
4224 /* For each parameter or variable that does not appear in qp,
4225 * first eliminate the variable from all constraints and then set it to zero.
4227 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4228 __isl_keep isl_qpolynomial
*qp
)
4239 d
= isl_set_dim(set
, isl_dim_all
);
4240 active
= isl_calloc_array(set
->ctx
, int, d
);
4241 if (set_active(qp
, active
) < 0)
4244 for (i
= 0; i
< d
; ++i
)
4253 nparam
= isl_set_dim(set
, isl_dim_param
);
4254 nvar
= isl_set_dim(set
, isl_dim_set
);
4255 for (i
= 0; i
< nparam
; ++i
) {
4258 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4259 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4261 for (i
= 0; i
< nvar
; ++i
) {
4262 if (active
[nparam
+ i
])
4264 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4265 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4277 struct isl_opt_data
{
4278 isl_qpolynomial
*qp
;
4284 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4286 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4289 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4293 } else if (data
->max
) {
4294 data
->opt
= isl_val_max(data
->opt
, val
);
4296 data
->opt
= isl_val_min(data
->opt
, val
);
4302 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4303 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4305 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4311 is_cst
= isl_poly_is_cst(qp
->poly
);
4316 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4317 isl_qpolynomial_free(qp
);
4321 set
= fix_inactive(set
, qp
);
4324 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4328 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4331 isl_qpolynomial_free(qp
);
4335 isl_qpolynomial_free(qp
);
4336 isl_val_free(data
.opt
);
4340 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4341 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4347 isl_mat
*mat
, *diag
;
4349 qp
= isl_qpolynomial_cow(qp
);
4354 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4356 n_sub
= morph
->inv
->n_row
- 1;
4357 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4358 n_sub
+= qp
->div
->n_row
;
4359 subs
= isl_calloc_array(ctx
, struct isl_poly
*, n_sub
);
4363 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4364 subs
[i
] = isl_poly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4365 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4366 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4367 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4368 subs
[morph
->inv
->n_row
- 1 + i
] =
4369 isl_poly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4371 qp
->poly
= isl_poly_subs(qp
->poly
, 0, n_sub
, subs
);
4373 for (i
= 0; i
< n_sub
; ++i
)
4374 isl_poly_free(subs
[i
]);
4377 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4378 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4379 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4380 mat
= isl_mat_diagonal(mat
, diag
);
4381 qp
->div
= isl_mat_product(qp
->div
, mat
);
4382 isl_space_free(qp
->dim
);
4383 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4385 if (!qp
->poly
|| !qp
->div
|| !qp
->dim
)
4388 isl_morph_free(morph
);
4392 isl_qpolynomial_free(qp
);
4393 isl_morph_free(morph
);
4397 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4398 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4399 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4401 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4402 &isl_pw_qpolynomial_mul
);
4405 /* Reorder the dimension of "qp" according to the given reordering.
4407 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4408 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4412 qp
= isl_qpolynomial_cow(qp
);
4416 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4420 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4424 qp
->poly
= reorder(qp
->poly
, r
->pos
);
4428 space
= isl_reordering_get_space(r
);
4429 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4431 isl_reordering_free(r
);
4434 isl_qpolynomial_free(qp
);
4435 isl_reordering_free(r
);
4439 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4440 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4442 isl_bool equal_params
;
4447 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4448 if (equal_params
< 0)
4450 if (!equal_params
) {
4451 isl_reordering
*exp
;
4453 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4454 exp
= isl_reordering_extend_space(exp
,
4455 isl_qpolynomial_get_domain_space(qp
));
4456 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4459 isl_space_free(model
);
4462 isl_space_free(model
);
4463 isl_qpolynomial_free(qp
);
4467 struct isl_split_periods_data
{
4469 isl_pw_qpolynomial
*res
;
4472 /* Create a slice where the integer division "div" has the fixed value "v".
4473 * In particular, if "div" refers to floor(f/m), then create a slice
4475 * m v <= f <= m v + (m - 1)
4480 * -f + m v + (m - 1) >= 0
4482 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4483 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4486 isl_basic_set
*bset
= NULL
;
4492 total
= isl_space_dim(space
, isl_dim_all
);
4493 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4495 k
= isl_basic_set_alloc_inequality(bset
);
4498 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4499 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4501 k
= isl_basic_set_alloc_inequality(bset
);
4504 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4505 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4506 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4507 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4509 isl_space_free(space
);
4510 return isl_set_from_basic_set(bset
);
4512 isl_basic_set_free(bset
);
4513 isl_space_free(space
);
4517 static isl_stat
split_periods(__isl_take isl_set
*set
,
4518 __isl_take isl_qpolynomial
*qp
, void *user
);
4520 /* Create a slice of the domain "set" such that integer division "div"
4521 * has the fixed value "v" and add the results to data->res,
4522 * replacing the integer division by "v" in "qp".
4524 static isl_stat
set_div(__isl_take isl_set
*set
,
4525 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4526 struct isl_split_periods_data
*data
)
4533 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4534 set
= isl_set_intersect(set
, slice
);
4536 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4540 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4541 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ div
]))
4543 isl_int_addmul(qp
->div
->row
[i
][1],
4544 qp
->div
->row
[i
][2 + div_pos
+ div
], v
);
4545 isl_int_set_si(qp
->div
->row
[i
][2 + div_pos
+ div
], 0);
4548 cst
= isl_poly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4549 qp
= substitute_div(qp
, div
, cst
);
4551 return split_periods(set
, qp
, data
);
4554 isl_qpolynomial_free(qp
);
4555 return isl_stat_error
;
4558 /* Split the domain "set" such that integer division "div"
4559 * has a fixed value (ranging from "min" to "max") on each slice
4560 * and add the results to data->res.
4562 static isl_stat
split_div(__isl_take isl_set
*set
,
4563 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4564 struct isl_split_periods_data
*data
)
4566 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4567 isl_set
*set_i
= isl_set_copy(set
);
4568 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4570 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4574 isl_qpolynomial_free(qp
);
4578 isl_qpolynomial_free(qp
);
4579 return isl_stat_error
;
4582 /* If "qp" refers to any integer division
4583 * that can only attain "max_periods" distinct values on "set"
4584 * then split the domain along those distinct values.
4585 * Add the results (or the original if no splitting occurs)
4588 static isl_stat
split_periods(__isl_take isl_set
*set
,
4589 __isl_take isl_qpolynomial
*qp
, void *user
)
4592 isl_pw_qpolynomial
*pwqp
;
4593 struct isl_split_periods_data
*data
;
4596 isl_stat r
= isl_stat_ok
;
4598 data
= (struct isl_split_periods_data
*)user
;
4603 if (qp
->div
->n_row
== 0) {
4604 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4605 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4609 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4615 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4616 enum isl_lp_result lp_res
;
4618 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + div_pos
,
4619 qp
->div
->n_row
) != -1)
4622 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4623 set
->ctx
->one
, &min
, NULL
, NULL
);
4624 if (lp_res
== isl_lp_error
)
4626 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4628 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4630 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4631 set
->ctx
->one
, &max
, NULL
, NULL
);
4632 if (lp_res
== isl_lp_error
)
4634 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4636 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4638 isl_int_sub(max
, max
, min
);
4639 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4640 isl_int_add(max
, max
, min
);
4645 if (i
< qp
->div
->n_row
) {
4646 r
= split_div(set
, qp
, i
, min
, max
, data
);
4648 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4649 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4661 isl_qpolynomial_free(qp
);
4662 return isl_stat_error
;
4665 /* If any quasi-polynomial in pwqp refers to any integer division
4666 * that can only attain "max_periods" distinct values on its domain
4667 * then split the domain along those distinct values.
4669 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4670 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4672 struct isl_split_periods_data data
;
4674 data
.max_periods
= max_periods
;
4675 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4677 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4680 isl_pw_qpolynomial_free(pwqp
);
4684 isl_pw_qpolynomial_free(data
.res
);
4685 isl_pw_qpolynomial_free(pwqp
);
4689 /* Construct a piecewise quasipolynomial that is constant on the given
4690 * domain. In particular, it is
4693 * infinity if cst == -1
4695 * If cst == -1, then explicitly check whether the domain is empty and,
4696 * if so, return 0 instead.
4698 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4699 __isl_take isl_basic_set
*bset
, int cst
)
4702 isl_qpolynomial
*qp
;
4704 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4709 bset
= isl_basic_set_params(bset
);
4710 dim
= isl_basic_set_get_space(bset
);
4712 qp
= isl_qpolynomial_infty_on_domain(dim
);
4714 qp
= isl_qpolynomial_zero_on_domain(dim
);
4716 qp
= isl_qpolynomial_one_on_domain(dim
);
4717 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4720 /* Factor bset, call fn on each of the factors and return the product.
4722 * If no factors can be found, simply call fn on the input.
4723 * Otherwise, construct the factors based on the factorizer,
4724 * call fn on each factor and compute the product.
4726 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4727 __isl_take isl_basic_set
*bset
,
4728 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4734 isl_qpolynomial
*qp
;
4735 isl_pw_qpolynomial
*pwqp
;
4739 f
= isl_basic_set_factorizer(bset
);
4742 if (f
->n_group
== 0) {
4743 isl_factorizer_free(f
);
4747 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4748 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4750 space
= isl_basic_set_get_space(bset
);
4751 space
= isl_space_params(space
);
4752 set
= isl_set_universe(isl_space_copy(space
));
4753 qp
= isl_qpolynomial_one_on_domain(space
);
4754 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4756 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4758 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4759 isl_basic_set
*bset_i
;
4760 isl_pw_qpolynomial
*pwqp_i
;
4762 bset_i
= isl_basic_set_copy(bset
);
4763 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4764 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4765 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4767 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4768 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4769 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4771 pwqp_i
= fn(bset_i
);
4772 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4777 isl_basic_set_free(bset
);
4778 isl_factorizer_free(f
);
4782 isl_basic_set_free(bset
);
4786 /* Factor bset, call fn on each of the factors and return the product.
4787 * The function is assumed to evaluate to zero on empty domains,
4788 * to one on zero-dimensional domains and to infinity on unbounded domains
4789 * and will not be called explicitly on zero-dimensional or unbounded domains.
4791 * We first check for some special cases and remove all equalities.
4792 * Then we hand over control to compressed_multiplicative_call.
4794 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4795 __isl_take isl_basic_set
*bset
,
4796 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4800 isl_pw_qpolynomial
*pwqp
;
4805 if (isl_basic_set_plain_is_empty(bset
))
4806 return constant_on_domain(bset
, 0);
4808 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4809 return constant_on_domain(bset
, 1);
4811 bounded
= isl_basic_set_is_bounded(bset
);
4815 return constant_on_domain(bset
, -1);
4817 if (bset
->n_eq
== 0)
4818 return compressed_multiplicative_call(bset
, fn
);
4820 morph
= isl_basic_set_full_compression(bset
);
4821 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4823 pwqp
= compressed_multiplicative_call(bset
, fn
);
4825 morph
= isl_morph_dom_params(morph
);
4826 morph
= isl_morph_ran_params(morph
);
4827 morph
= isl_morph_inverse(morph
);
4829 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4833 isl_basic_set_free(bset
);
4837 /* Drop all floors in "qp", turning each integer division [a/m] into
4838 * a rational division a/m. If "down" is set, then the integer division
4839 * is replaced by (a-(m-1))/m instead.
4841 static __isl_give isl_qpolynomial
*qp_drop_floors(
4842 __isl_take isl_qpolynomial
*qp
, int down
)
4849 if (qp
->div
->n_row
== 0)
4852 qp
= isl_qpolynomial_cow(qp
);
4856 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4858 isl_int_sub(qp
->div
->row
[i
][1],
4859 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4860 isl_int_add_ui(qp
->div
->row
[i
][1],
4861 qp
->div
->row
[i
][1], 1);
4863 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4864 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4865 qp
= substitute_div(qp
, i
, s
);
4873 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4874 * a rational division a/m.
4876 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4877 __isl_take isl_pw_qpolynomial
*pwqp
)
4884 if (isl_pw_qpolynomial_is_zero(pwqp
))
4887 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4891 for (i
= 0; i
< pwqp
->n
; ++i
) {
4892 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4899 isl_pw_qpolynomial_free(pwqp
);
4903 /* Adjust all the integer divisions in "qp" such that they are at least
4904 * one over the given orthant (identified by "signs"). This ensures
4905 * that they will still be non-negative even after subtracting (m-1)/m.
4907 * In particular, f is replaced by f' + v, changing f = [a/m]
4908 * to f' = [(a - m v)/m].
4909 * If the constant term k in a is smaller than m,
4910 * the constant term of v is set to floor(k/m) - 1.
4911 * For any other term, if the coefficient c and the variable x have
4912 * the same sign, then no changes are needed.
4913 * Otherwise, if the variable is positive (and c is negative),
4914 * then the coefficient of x in v is set to floor(c/m).
4915 * If the variable is negative (and c is positive),
4916 * then the coefficient of x in v is set to ceil(c/m).
4918 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4926 qp
= isl_qpolynomial_cow(qp
);
4927 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4929 return isl_qpolynomial_free(qp
);
4930 qp
->div
= isl_mat_cow(qp
->div
);
4934 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4936 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4937 isl_int
*row
= qp
->div
->row
[i
];
4941 if (isl_int_lt(row
[1], row
[0])) {
4942 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4943 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4944 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4946 for (j
= 0; j
< div_pos
; ++j
) {
4947 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4950 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4952 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4953 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4955 for (j
= 0; j
< i
; ++j
) {
4956 if (isl_int_sgn(row
[2 + div_pos
+ j
]) >= 0)
4958 isl_int_fdiv_q(v
->el
[1 + div_pos
+ j
],
4959 row
[2 + div_pos
+ j
], row
[0]);
4960 isl_int_submul(row
[2 + div_pos
+ j
],
4961 row
[0], v
->el
[1 + div_pos
+ j
]);
4963 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4964 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
4966 isl_seq_combine(qp
->div
->row
[j
] + 1,
4967 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4968 qp
->div
->row
[j
][2 + div_pos
+ i
], v
->el
,
4971 isl_int_set_si(v
->el
[1 + div_pos
+ i
], 1);
4972 s
= isl_poly_from_affine(qp
->dim
->ctx
, v
->el
,
4973 qp
->div
->ctx
->one
, v
->size
);
4974 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ i
, 1, &s
);
4984 isl_qpolynomial_free(qp
);
4988 struct isl_to_poly_data
{
4990 isl_pw_qpolynomial
*res
;
4991 isl_qpolynomial
*qp
;
4994 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4995 * We first make all integer divisions positive and then split the
4996 * quasipolynomials into terms with sign data->sign (the direction
4997 * of the requested approximation) and terms with the opposite sign.
4998 * In the first set of terms, each integer division [a/m] is
4999 * overapproximated by a/m, while in the second it is underapproximated
5002 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
5003 int *signs
, void *user
)
5005 struct isl_to_poly_data
*data
= user
;
5006 isl_pw_qpolynomial
*t
;
5007 isl_qpolynomial
*qp
, *up
, *down
;
5009 qp
= isl_qpolynomial_copy(data
->qp
);
5010 qp
= make_divs_pos(qp
, signs
);
5012 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
5013 up
= qp_drop_floors(up
, 0);
5014 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
5015 down
= qp_drop_floors(down
, 1);
5017 isl_qpolynomial_free(qp
);
5018 qp
= isl_qpolynomial_add(up
, down
);
5020 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
5021 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
5026 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5027 * the polynomial will be an overapproximation. If "sign" is negative,
5028 * it will be an underapproximation. If "sign" is zero, the approximation
5029 * will lie somewhere in between.
5031 * In particular, is sign == 0, we simply drop the floors, turning
5032 * the integer divisions into rational divisions.
5033 * Otherwise, we split the domains into orthants, make all integer divisions
5034 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5035 * depending on the requested sign and the sign of the term in which
5036 * the integer division appears.
5038 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
5039 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
5042 struct isl_to_poly_data data
;
5045 return pwqp_drop_floors(pwqp
);
5051 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
5053 for (i
= 0; i
< pwqp
->n
; ++i
) {
5054 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
5055 isl_pw_qpolynomial
*t
;
5056 t
= isl_pw_qpolynomial_alloc(
5057 isl_set_copy(pwqp
->p
[i
].set
),
5058 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
5059 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
5062 data
.qp
= pwqp
->p
[i
].qp
;
5063 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
5064 &to_polynomial_on_orthant
, &data
) < 0)
5068 isl_pw_qpolynomial_free(pwqp
);
5072 isl_pw_qpolynomial_free(pwqp
);
5073 isl_pw_qpolynomial_free(data
.res
);
5077 static __isl_give isl_pw_qpolynomial
*poly_entry(
5078 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
5082 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
5085 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
5086 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
5088 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
5089 &poly_entry
, &sign
);
5092 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
5093 __isl_take isl_qpolynomial
*qp
)
5097 isl_vec
*aff
= NULL
;
5098 isl_basic_map
*bmap
= NULL
;
5105 is_affine
= isl_poly_is_affine(qp
->poly
);
5109 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
5110 "input quasi-polynomial not affine", goto error
);
5111 aff
= isl_qpolynomial_extract_affine(qp
);
5114 dim
= isl_qpolynomial_get_space(qp
);
5115 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
5116 n_div
= qp
->div
->n_row
;
5117 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
5119 for (i
= 0; i
< n_div
; ++i
) {
5120 k
= isl_basic_map_alloc_div(bmap
);
5123 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
5124 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
5125 bmap
= isl_basic_map_add_div_constraints(bmap
, k
);
5127 k
= isl_basic_map_alloc_equality(bmap
);
5130 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5131 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5132 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5135 isl_qpolynomial_free(qp
);
5136 bmap
= isl_basic_map_finalize(bmap
);
5140 isl_qpolynomial_free(qp
);
5141 isl_basic_map_free(bmap
);