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 isl_size
isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
484 enum isl_dim_type type
)
489 space
= isl_qpolynomial_peek_domain_space(qp
);
492 return isl_size_error
;
493 if (type
== isl_dim_div
)
494 return qp
->div
->n_row
;
495 dim
= isl_space_dim(space
, type
);
497 return isl_size_error
;
498 if (type
== isl_dim_all
) {
501 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
503 return isl_size_error
;
509 /* Given the type of a dimension of an isl_qpolynomial,
510 * return the type of the corresponding dimension in its domain.
511 * This function is only called for "type" equal to isl_dim_in or
514 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
516 return type
== isl_dim_in
? isl_dim_set
: type
;
519 /* Externally, an isl_qpolynomial has a map space, but internally, the
520 * ls field corresponds to the domain of that space.
522 isl_size
isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
523 enum isl_dim_type type
)
526 return isl_size_error
;
527 if (type
== isl_dim_out
)
529 type
= domain_type(type
);
530 return isl_qpolynomial_domain_dim(qp
, type
);
533 /* Return the offset of the first variable of type "type" within
534 * the variables of the domain of "qp".
536 static int isl_qpolynomial_domain_var_offset(__isl_keep isl_qpolynomial
*qp
,
537 enum isl_dim_type type
)
541 space
= isl_qpolynomial_peek_domain_space(qp
);
547 case isl_dim_set
: return isl_space_offset(space
, type
);
548 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
551 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
552 "invalid dimension type", return -1);
556 /* Return the offset of the first coefficient of type "type" in
557 * the domain of "qp".
559 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
560 enum isl_dim_type type
)
568 return 1 + isl_qpolynomial_domain_var_offset(qp
, type
);
574 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
576 return qp
? isl_poly_is_zero(qp
->poly
) : isl_bool_error
;
579 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
581 return qp
? isl_poly_is_one(qp
->poly
) : isl_bool_error
;
584 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
586 return qp
? isl_poly_is_nan(qp
->poly
) : isl_bool_error
;
589 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
591 return qp
? isl_poly_is_infty(qp
->poly
) : isl_bool_error
;
594 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
596 return qp
? isl_poly_is_neginfty(qp
->poly
) : isl_bool_error
;
599 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
601 return qp
? isl_poly_sgn(qp
->poly
) : 0;
604 static void poly_free_cst(__isl_take isl_poly_cst
*cst
)
606 isl_int_clear(cst
->n
);
607 isl_int_clear(cst
->d
);
610 static void poly_free_rec(__isl_take isl_poly_rec
*rec
)
614 for (i
= 0; i
< rec
->n
; ++i
)
615 isl_poly_free(rec
->p
[i
]);
618 __isl_give isl_poly
*isl_poly_copy(__isl_keep isl_poly
*poly
)
627 __isl_give isl_poly
*isl_poly_dup_cst(__isl_keep isl_poly
*poly
)
632 cst
= isl_poly_as_cst(poly
);
636 dup
= isl_poly_as_cst(isl_poly_zero(poly
->ctx
));
639 isl_int_set(dup
->n
, cst
->n
);
640 isl_int_set(dup
->d
, cst
->d
);
645 __isl_give isl_poly
*isl_poly_dup_rec(__isl_keep isl_poly
*poly
)
651 rec
= isl_poly_as_rec(poly
);
655 dup
= isl_poly_alloc_rec(poly
->ctx
, poly
->var
, rec
->n
);
659 for (i
= 0; i
< rec
->n
; ++i
) {
660 dup
->p
[i
] = isl_poly_copy(rec
->p
[i
]);
668 isl_poly_free(&dup
->poly
);
672 __isl_give isl_poly
*isl_poly_dup(__isl_keep isl_poly
*poly
)
676 is_cst
= isl_poly_is_cst(poly
);
680 return isl_poly_dup_cst(poly
);
682 return isl_poly_dup_rec(poly
);
685 __isl_give isl_poly
*isl_poly_cow(__isl_take isl_poly
*poly
)
693 return isl_poly_dup(poly
);
696 __isl_null isl_poly
*isl_poly_free(__isl_take isl_poly
*poly
)
705 poly_free_cst((isl_poly_cst
*) poly
);
707 poly_free_rec((isl_poly_rec
*) poly
);
709 isl_ctx_deref(poly
->ctx
);
714 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst
*cst
)
719 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
720 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
721 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
722 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
727 __isl_give isl_poly
*isl_poly_sum_cst(__isl_take isl_poly
*poly1
,
728 __isl_take isl_poly
*poly2
)
733 poly1
= isl_poly_cow(poly1
);
734 if (!poly1
|| !poly2
)
737 cst1
= isl_poly_as_cst(poly1
);
738 cst2
= isl_poly_as_cst(poly2
);
740 if (isl_int_eq(cst1
->d
, cst2
->d
))
741 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
743 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
744 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
745 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
748 isl_poly_cst_reduce(cst1
);
750 isl_poly_free(poly2
);
753 isl_poly_free(poly1
);
754 isl_poly_free(poly2
);
758 static __isl_give isl_poly
*replace_by_zero(__isl_take isl_poly
*poly
)
766 return isl_poly_zero(ctx
);
769 static __isl_give isl_poly
*replace_by_constant_term(__isl_take isl_poly
*poly
)
777 rec
= isl_poly_as_rec(poly
);
780 cst
= isl_poly_copy(rec
->p
[0]);
788 __isl_give isl_poly
*isl_poly_sum(__isl_take isl_poly
*poly1
,
789 __isl_take isl_poly
*poly2
)
792 isl_bool is_zero
, is_nan
, is_cst
;
793 isl_poly_rec
*rec1
, *rec2
;
795 if (!poly1
|| !poly2
)
798 is_nan
= isl_poly_is_nan(poly1
);
802 isl_poly_free(poly2
);
806 is_nan
= isl_poly_is_nan(poly2
);
810 isl_poly_free(poly1
);
814 is_zero
= isl_poly_is_zero(poly1
);
818 isl_poly_free(poly1
);
822 is_zero
= isl_poly_is_zero(poly2
);
826 isl_poly_free(poly2
);
830 if (poly1
->var
< poly2
->var
)
831 return isl_poly_sum(poly2
, poly1
);
833 if (poly2
->var
< poly1
->var
) {
837 is_infty
= isl_poly_is_infty(poly2
);
838 if (is_infty
>= 0 && !is_infty
)
839 is_infty
= isl_poly_is_neginfty(poly2
);
843 isl_poly_free(poly1
);
846 poly1
= isl_poly_cow(poly1
);
847 rec
= isl_poly_as_rec(poly1
);
850 rec
->p
[0] = isl_poly_sum(rec
->p
[0], poly2
);
852 poly1
= replace_by_constant_term(poly1
);
856 is_cst
= isl_poly_is_cst(poly1
);
860 return isl_poly_sum_cst(poly1
, poly2
);
862 rec1
= isl_poly_as_rec(poly1
);
863 rec2
= isl_poly_as_rec(poly2
);
867 if (rec1
->n
< rec2
->n
)
868 return isl_poly_sum(poly2
, poly1
);
870 poly1
= isl_poly_cow(poly1
);
871 rec1
= isl_poly_as_rec(poly1
);
875 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
878 rec1
->p
[i
] = isl_poly_sum(rec1
->p
[i
],
879 isl_poly_copy(rec2
->p
[i
]));
882 if (i
!= rec1
->n
- 1)
884 is_zero
= isl_poly_is_zero(rec1
->p
[i
]);
888 isl_poly_free(rec1
->p
[i
]);
894 poly1
= replace_by_zero(poly1
);
895 else if (rec1
->n
== 1)
896 poly1
= replace_by_constant_term(poly1
);
898 isl_poly_free(poly2
);
902 isl_poly_free(poly1
);
903 isl_poly_free(poly2
);
907 __isl_give isl_poly
*isl_poly_cst_add_isl_int(__isl_take isl_poly
*poly
,
912 poly
= isl_poly_cow(poly
);
916 cst
= isl_poly_as_cst(poly
);
918 isl_int_addmul(cst
->n
, cst
->d
, v
);
923 __isl_give isl_poly
*isl_poly_add_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
928 is_cst
= isl_poly_is_cst(poly
);
930 return isl_poly_free(poly
);
932 return isl_poly_cst_add_isl_int(poly
, v
);
934 poly
= isl_poly_cow(poly
);
935 rec
= isl_poly_as_rec(poly
);
939 rec
->p
[0] = isl_poly_add_isl_int(rec
->p
[0], v
);
949 __isl_give isl_poly
*isl_poly_cst_mul_isl_int(__isl_take isl_poly
*poly
,
955 is_zero
= isl_poly_is_zero(poly
);
957 return isl_poly_free(poly
);
961 poly
= isl_poly_cow(poly
);
965 cst
= isl_poly_as_cst(poly
);
967 isl_int_mul(cst
->n
, cst
->n
, v
);
972 __isl_give isl_poly
*isl_poly_mul_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
978 is_cst
= isl_poly_is_cst(poly
);
980 return isl_poly_free(poly
);
982 return isl_poly_cst_mul_isl_int(poly
, v
);
984 poly
= isl_poly_cow(poly
);
985 rec
= isl_poly_as_rec(poly
);
989 for (i
= 0; i
< rec
->n
; ++i
) {
990 rec
->p
[i
] = isl_poly_mul_isl_int(rec
->p
[i
], v
);
1001 /* Multiply the constant polynomial "poly" by "v".
1003 static __isl_give isl_poly
*isl_poly_cst_scale_val(__isl_take isl_poly
*poly
,
1004 __isl_keep isl_val
*v
)
1009 is_zero
= isl_poly_is_zero(poly
);
1011 return isl_poly_free(poly
);
1015 poly
= isl_poly_cow(poly
);
1019 cst
= isl_poly_as_cst(poly
);
1021 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
1022 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
1023 isl_poly_cst_reduce(cst
);
1028 /* Multiply the polynomial "poly" by "v".
1030 static __isl_give isl_poly
*isl_poly_scale_val(__isl_take isl_poly
*poly
,
1031 __isl_keep isl_val
*v
)
1037 is_cst
= isl_poly_is_cst(poly
);
1039 return isl_poly_free(poly
);
1041 return isl_poly_cst_scale_val(poly
, v
);
1043 poly
= isl_poly_cow(poly
);
1044 rec
= isl_poly_as_rec(poly
);
1048 for (i
= 0; i
< rec
->n
; ++i
) {
1049 rec
->p
[i
] = isl_poly_scale_val(rec
->p
[i
], v
);
1056 isl_poly_free(poly
);
1060 __isl_give isl_poly
*isl_poly_mul_cst(__isl_take isl_poly
*poly1
,
1061 __isl_take isl_poly
*poly2
)
1066 poly1
= isl_poly_cow(poly1
);
1067 if (!poly1
|| !poly2
)
1070 cst1
= isl_poly_as_cst(poly1
);
1071 cst2
= isl_poly_as_cst(poly2
);
1073 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
1074 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
1076 isl_poly_cst_reduce(cst1
);
1078 isl_poly_free(poly2
);
1081 isl_poly_free(poly1
);
1082 isl_poly_free(poly2
);
1086 __isl_give isl_poly
*isl_poly_mul_rec(__isl_take isl_poly
*poly1
,
1087 __isl_take isl_poly
*poly2
)
1091 isl_poly_rec
*res
= NULL
;
1095 rec1
= isl_poly_as_rec(poly1
);
1096 rec2
= isl_poly_as_rec(poly2
);
1099 size
= rec1
->n
+ rec2
->n
- 1;
1100 res
= isl_poly_alloc_rec(poly1
->ctx
, poly1
->var
, size
);
1104 for (i
= 0; i
< rec1
->n
; ++i
) {
1105 res
->p
[i
] = isl_poly_mul(isl_poly_copy(rec2
->p
[0]),
1106 isl_poly_copy(rec1
->p
[i
]));
1111 for (; i
< size
; ++i
) {
1112 res
->p
[i
] = isl_poly_zero(poly1
->ctx
);
1117 for (i
= 0; i
< rec1
->n
; ++i
) {
1118 for (j
= 1; j
< rec2
->n
; ++j
) {
1120 poly
= isl_poly_mul(isl_poly_copy(rec2
->p
[j
]),
1121 isl_poly_copy(rec1
->p
[i
]));
1122 res
->p
[i
+ j
] = isl_poly_sum(res
->p
[i
+ j
], poly
);
1128 isl_poly_free(poly1
);
1129 isl_poly_free(poly2
);
1133 isl_poly_free(poly1
);
1134 isl_poly_free(poly2
);
1135 isl_poly_free(&res
->poly
);
1139 __isl_give isl_poly
*isl_poly_mul(__isl_take isl_poly
*poly1
,
1140 __isl_take isl_poly
*poly2
)
1142 isl_bool is_zero
, is_nan
, is_one
, is_cst
;
1144 if (!poly1
|| !poly2
)
1147 is_nan
= isl_poly_is_nan(poly1
);
1151 isl_poly_free(poly2
);
1155 is_nan
= isl_poly_is_nan(poly2
);
1159 isl_poly_free(poly1
);
1163 is_zero
= isl_poly_is_zero(poly1
);
1167 isl_poly_free(poly2
);
1171 is_zero
= isl_poly_is_zero(poly2
);
1175 isl_poly_free(poly1
);
1179 is_one
= isl_poly_is_one(poly1
);
1183 isl_poly_free(poly1
);
1187 is_one
= isl_poly_is_one(poly2
);
1191 isl_poly_free(poly2
);
1195 if (poly1
->var
< poly2
->var
)
1196 return isl_poly_mul(poly2
, poly1
);
1198 if (poly2
->var
< poly1
->var
) {
1203 is_infty
= isl_poly_is_infty(poly2
);
1204 if (is_infty
>= 0 && !is_infty
)
1205 is_infty
= isl_poly_is_neginfty(poly2
);
1209 isl_ctx
*ctx
= poly1
->ctx
;
1210 isl_poly_free(poly1
);
1211 isl_poly_free(poly2
);
1212 return isl_poly_nan(ctx
);
1214 poly1
= isl_poly_cow(poly1
);
1215 rec
= isl_poly_as_rec(poly1
);
1219 for (i
= 0; i
< rec
->n
; ++i
) {
1220 rec
->p
[i
] = isl_poly_mul(rec
->p
[i
],
1221 isl_poly_copy(poly2
));
1225 isl_poly_free(poly2
);
1229 is_cst
= isl_poly_is_cst(poly1
);
1233 return isl_poly_mul_cst(poly1
, poly2
);
1235 return isl_poly_mul_rec(poly1
, poly2
);
1237 isl_poly_free(poly1
);
1238 isl_poly_free(poly2
);
1242 __isl_give isl_poly
*isl_poly_pow(__isl_take isl_poly
*poly
, unsigned power
)
1252 res
= isl_poly_copy(poly
);
1254 res
= isl_poly_one(poly
->ctx
);
1256 while (power
>>= 1) {
1257 poly
= isl_poly_mul(poly
, isl_poly_copy(poly
));
1259 res
= isl_poly_mul(res
, isl_poly_copy(poly
));
1262 isl_poly_free(poly
);
1266 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1267 unsigned n_div
, __isl_take isl_poly
*poly
)
1269 struct isl_qpolynomial
*qp
= NULL
;
1272 total
= isl_space_dim(space
, isl_dim_all
);
1273 if (total
< 0 || !poly
)
1276 if (!isl_space_is_set(space
))
1277 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1278 "domain of polynomial should be a set", goto error
);
1280 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1285 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1294 isl_space_free(space
);
1295 isl_poly_free(poly
);
1296 isl_qpolynomial_free(qp
);
1300 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1309 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1311 struct isl_qpolynomial
*dup
;
1316 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1317 isl_poly_copy(qp
->poly
));
1320 isl_mat_free(dup
->div
);
1321 dup
->div
= isl_mat_copy(qp
->div
);
1327 isl_qpolynomial_free(dup
);
1331 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1339 return isl_qpolynomial_dup(qp
);
1342 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1343 __isl_take isl_qpolynomial
*qp
)
1351 isl_space_free(qp
->dim
);
1352 isl_mat_free(qp
->div
);
1353 isl_poly_free(qp
->poly
);
1359 __isl_give isl_poly
*isl_poly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1365 rec
= isl_poly_alloc_rec(ctx
, pos
, 1 + power
);
1368 for (i
= 0; i
< 1 + power
; ++i
) {
1369 rec
->p
[i
] = isl_poly_zero(ctx
);
1374 cst
= isl_poly_as_cst(rec
->p
[power
]);
1375 isl_int_set_si(cst
->n
, 1);
1379 isl_poly_free(&rec
->poly
);
1383 /* r array maps original positions to new positions.
1385 static __isl_give isl_poly
*reorder(__isl_take isl_poly
*poly
, int *r
)
1393 is_cst
= isl_poly_is_cst(poly
);
1395 return isl_poly_free(poly
);
1399 rec
= isl_poly_as_rec(poly
);
1403 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
1405 base
= isl_poly_var_pow(poly
->ctx
, r
[poly
->var
], 1);
1406 res
= reorder(isl_poly_copy(rec
->p
[rec
->n
- 1]), r
);
1408 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1409 res
= isl_poly_mul(res
, isl_poly_copy(base
));
1410 res
= isl_poly_sum(res
, reorder(isl_poly_copy(rec
->p
[i
]), r
));
1413 isl_poly_free(base
);
1414 isl_poly_free(poly
);
1418 isl_poly_free(poly
);
1422 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1423 __isl_keep isl_mat
*div2
)
1428 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1429 div1
->n_col
>= div2
->n_col
,
1430 return isl_bool_error
);
1432 if (div1
->n_row
== div2
->n_row
)
1433 return isl_mat_is_equal(div1
, div2
);
1435 n_row
= div1
->n_row
;
1436 n_col
= div1
->n_col
;
1437 div1
->n_row
= div2
->n_row
;
1438 div1
->n_col
= div2
->n_col
;
1440 equal
= isl_mat_is_equal(div1
, div2
);
1442 div1
->n_row
= n_row
;
1443 div1
->n_col
= n_col
;
1448 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1452 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1453 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1458 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1461 struct isl_div_sort_info
{
1466 static int div_sort_cmp(const void *p1
, const void *p2
)
1468 const struct isl_div_sort_info
*i1
, *i2
;
1469 i1
= (const struct isl_div_sort_info
*) p1
;
1470 i2
= (const struct isl_div_sort_info
*) p2
;
1472 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1475 /* Sort divs and remove duplicates.
1477 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1482 struct isl_div_sort_info
*array
= NULL
;
1483 int *pos
= NULL
, *at
= NULL
;
1484 int *reordering
= NULL
;
1489 if (qp
->div
->n_row
<= 1)
1492 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
1494 return isl_qpolynomial_free(qp
);
1496 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1498 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1499 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1500 len
= qp
->div
->n_col
- 2;
1501 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1502 if (!array
|| !pos
|| !at
|| !reordering
)
1505 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1506 array
[i
].div
= qp
->div
;
1512 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1515 for (i
= 0; i
< div_pos
; ++i
)
1518 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1519 if (pos
[array
[i
].row
] == i
)
1521 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1522 pos
[at
[i
]] = pos
[array
[i
].row
];
1523 at
[pos
[array
[i
].row
]] = at
[i
];
1524 at
[i
] = array
[i
].row
;
1525 pos
[array
[i
].row
] = i
;
1529 for (i
= 0; i
< len
- div_pos
; ++i
) {
1531 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1532 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1533 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1534 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1535 2 + div_pos
+ i
- skip
);
1536 qp
->div
= isl_mat_drop_cols(qp
->div
,
1537 2 + div_pos
+ i
- skip
, 1);
1540 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1543 qp
->poly
= reorder(qp
->poly
, reordering
);
1545 if (!qp
->poly
|| !qp
->div
)
1559 isl_qpolynomial_free(qp
);
1563 static __isl_give isl_poly
*expand(__isl_take isl_poly
*poly
, int *exp
,
1570 is_cst
= isl_poly_is_cst(poly
);
1572 return isl_poly_free(poly
);
1576 if (poly
->var
< first
)
1579 if (exp
[poly
->var
- first
] == poly
->var
- first
)
1582 poly
= isl_poly_cow(poly
);
1586 poly
->var
= exp
[poly
->var
- first
] + first
;
1588 rec
= isl_poly_as_rec(poly
);
1592 for (i
= 0; i
< rec
->n
; ++i
) {
1593 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1600 isl_poly_free(poly
);
1604 static __isl_give isl_qpolynomial
*with_merged_divs(
1605 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1606 __isl_take isl_qpolynomial
*qp2
),
1607 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1611 isl_mat
*div
= NULL
;
1614 qp1
= isl_qpolynomial_cow(qp1
);
1615 qp2
= isl_qpolynomial_cow(qp2
);
1620 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1621 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1623 n_div1
= qp1
->div
->n_row
;
1624 n_div2
= qp2
->div
->n_row
;
1625 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1626 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1627 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1630 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1634 isl_mat_free(qp1
->div
);
1635 qp1
->div
= isl_mat_copy(div
);
1636 isl_mat_free(qp2
->div
);
1637 qp2
->div
= isl_mat_copy(div
);
1639 qp1
->poly
= expand(qp1
->poly
, exp1
, div
->n_col
- div
->n_row
- 2);
1640 qp2
->poly
= expand(qp2
->poly
, exp2
, div
->n_col
- div
->n_row
- 2);
1642 if (!qp1
->poly
|| !qp2
->poly
)
1649 return fn(qp1
, qp2
);
1654 isl_qpolynomial_free(qp1
);
1655 isl_qpolynomial_free(qp2
);
1659 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1660 __isl_take isl_qpolynomial
*qp2
)
1662 isl_bool compatible
;
1664 qp1
= isl_qpolynomial_cow(qp1
);
1669 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1670 return isl_qpolynomial_add(qp2
, qp1
);
1672 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1673 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1677 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1679 qp1
->poly
= isl_poly_sum(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1683 isl_qpolynomial_free(qp2
);
1687 isl_qpolynomial_free(qp1
);
1688 isl_qpolynomial_free(qp2
);
1692 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1693 __isl_keep isl_set
*dom
,
1694 __isl_take isl_qpolynomial
*qp1
,
1695 __isl_take isl_qpolynomial
*qp2
)
1697 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1698 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1702 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1703 __isl_take isl_qpolynomial
*qp2
)
1705 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1708 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1709 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1711 if (isl_int_is_zero(v
))
1714 qp
= isl_qpolynomial_cow(qp
);
1718 qp
->poly
= isl_poly_add_isl_int(qp
->poly
, v
);
1724 isl_qpolynomial_free(qp
);
1729 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1734 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1737 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1738 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1740 if (isl_int_is_one(v
))
1743 if (qp
&& isl_int_is_zero(v
)) {
1744 isl_qpolynomial
*zero
;
1745 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1746 isl_qpolynomial_free(qp
);
1750 qp
= isl_qpolynomial_cow(qp
);
1754 qp
->poly
= isl_poly_mul_isl_int(qp
->poly
, v
);
1760 isl_qpolynomial_free(qp
);
1764 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1765 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1767 return isl_qpolynomial_mul_isl_int(qp
, v
);
1770 /* Multiply "qp" by "v".
1772 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1773 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1778 if (!isl_val_is_rat(v
))
1779 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1780 "expecting rational factor", goto error
);
1782 if (isl_val_is_one(v
)) {
1787 if (isl_val_is_zero(v
)) {
1790 space
= isl_qpolynomial_get_domain_space(qp
);
1791 isl_qpolynomial_free(qp
);
1793 return isl_qpolynomial_zero_on_domain(space
);
1796 qp
= isl_qpolynomial_cow(qp
);
1800 qp
->poly
= isl_poly_scale_val(qp
->poly
, v
);
1802 qp
= isl_qpolynomial_free(qp
);
1808 isl_qpolynomial_free(qp
);
1812 /* Divide "qp" by "v".
1814 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1815 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1820 if (!isl_val_is_rat(v
))
1821 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1822 "expecting rational factor", goto error
);
1823 if (isl_val_is_zero(v
))
1824 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1825 "cannot scale down by zero", goto error
);
1827 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1830 isl_qpolynomial_free(qp
);
1834 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1835 __isl_take isl_qpolynomial
*qp2
)
1837 isl_bool compatible
;
1839 qp1
= isl_qpolynomial_cow(qp1
);
1844 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1845 return isl_qpolynomial_mul(qp2
, qp1
);
1847 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1848 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1852 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1854 qp1
->poly
= isl_poly_mul(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1858 isl_qpolynomial_free(qp2
);
1862 isl_qpolynomial_free(qp1
);
1863 isl_qpolynomial_free(qp2
);
1867 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1870 qp
= isl_qpolynomial_cow(qp
);
1875 qp
->poly
= isl_poly_pow(qp
->poly
, power
);
1881 isl_qpolynomial_free(qp
);
1885 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1886 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1893 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1897 for (i
= 0; i
< pwqp
->n
; ++i
) {
1898 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1900 return isl_pw_qpolynomial_free(pwqp
);
1906 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1907 __isl_take isl_space
*domain
)
1911 return isl_qpolynomial_alloc(domain
, 0, isl_poly_zero(domain
->ctx
));
1914 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1915 __isl_take isl_space
*domain
)
1919 return isl_qpolynomial_alloc(domain
, 0, isl_poly_one(domain
->ctx
));
1922 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1923 __isl_take isl_space
*domain
)
1927 return isl_qpolynomial_alloc(domain
, 0, isl_poly_infty(domain
->ctx
));
1930 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1931 __isl_take isl_space
*domain
)
1935 return isl_qpolynomial_alloc(domain
, 0, isl_poly_neginfty(domain
->ctx
));
1938 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1939 __isl_take isl_space
*domain
)
1943 return isl_qpolynomial_alloc(domain
, 0, isl_poly_nan(domain
->ctx
));
1946 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1947 __isl_take isl_space
*domain
,
1950 struct isl_qpolynomial
*qp
;
1953 qp
= isl_qpolynomial_zero_on_domain(domain
);
1957 cst
= isl_poly_as_cst(qp
->poly
);
1958 isl_int_set(cst
->n
, v
);
1963 isl_bool
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1964 isl_int
*n
, isl_int
*d
)
1970 return isl_bool_error
;
1972 is_cst
= isl_poly_is_cst(qp
->poly
);
1973 if (is_cst
< 0 || !is_cst
)
1976 cst
= isl_poly_as_cst(qp
->poly
);
1978 return isl_bool_error
;
1981 isl_int_set(*n
, cst
->n
);
1983 isl_int_set(*d
, cst
->d
);
1985 return isl_bool_true
;
1988 /* Return the constant term of "poly".
1990 static __isl_give isl_val
*isl_poly_get_constant_val(__isl_keep isl_poly
*poly
)
1998 while ((is_cst
= isl_poly_is_cst(poly
)) == isl_bool_false
) {
2001 rec
= isl_poly_as_rec(poly
);
2009 cst
= isl_poly_as_cst(poly
);
2012 return isl_val_rat_from_isl_int(cst
->poly
.ctx
, cst
->n
, cst
->d
);
2015 /* Return the constant term of "qp".
2017 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
2018 __isl_keep isl_qpolynomial
*qp
)
2023 return isl_poly_get_constant_val(qp
->poly
);
2026 isl_bool
isl_poly_is_affine(__isl_keep isl_poly
*poly
)
2032 return isl_bool_error
;
2035 return isl_bool_true
;
2037 rec
= isl_poly_as_rec(poly
);
2039 return isl_bool_error
;
2042 return isl_bool_false
;
2044 isl_assert(poly
->ctx
, rec
->n
> 1, return isl_bool_error
);
2046 is_cst
= isl_poly_is_cst(rec
->p
[1]);
2047 if (is_cst
< 0 || !is_cst
)
2050 return isl_poly_is_affine(rec
->p
[0]);
2053 isl_bool
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
2056 return isl_bool_error
;
2058 if (qp
->div
->n_row
> 0)
2059 return isl_bool_false
;
2061 return isl_poly_is_affine(qp
->poly
);
2064 static void update_coeff(__isl_keep isl_vec
*aff
,
2065 __isl_keep isl_poly_cst
*cst
, int pos
)
2070 if (isl_int_is_zero(cst
->n
))
2075 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
2076 isl_int_divexact(f
, cst
->d
, gcd
);
2077 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
2078 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
2079 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
2084 int isl_poly_update_affine(__isl_keep isl_poly
*poly
, __isl_keep isl_vec
*aff
)
2092 if (poly
->var
< 0) {
2095 cst
= isl_poly_as_cst(poly
);
2098 update_coeff(aff
, cst
, 0);
2102 rec
= isl_poly_as_rec(poly
);
2105 isl_assert(poly
->ctx
, rec
->n
== 2, return -1);
2107 cst
= isl_poly_as_cst(rec
->p
[1]);
2110 update_coeff(aff
, cst
, 1 + poly
->var
);
2112 return isl_poly_update_affine(rec
->p
[0], aff
);
2115 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
2116 __isl_keep isl_qpolynomial
*qp
)
2121 d
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2125 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
2129 isl_seq_clr(aff
->el
+ 1, 1 + d
);
2130 isl_int_set_si(aff
->el
[0], 1);
2132 if (isl_poly_update_affine(qp
->poly
, aff
) < 0)
2141 /* Compare two quasi-polynomials.
2143 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2144 * than "qp2" and 0 if they are equal.
2146 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2147 __isl_keep isl_qpolynomial
*qp2
)
2158 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2162 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2166 return isl_poly_plain_cmp(qp1
->poly
, qp2
->poly
);
2169 /* Is "qp1" obviously equal to "qp2"?
2171 * NaN is not equal to anything, not even to another NaN.
2173 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2174 __isl_keep isl_qpolynomial
*qp2
)
2179 return isl_bool_error
;
2181 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2182 return isl_bool_false
;
2184 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2185 if (equal
< 0 || !equal
)
2188 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2189 if (equal
< 0 || !equal
)
2192 return isl_poly_is_equal(qp1
->poly
, qp2
->poly
);
2195 static isl_stat
poly_update_den(__isl_keep isl_poly
*poly
, isl_int
*d
)
2201 is_cst
= isl_poly_is_cst(poly
);
2203 return isl_stat_error
;
2206 cst
= isl_poly_as_cst(poly
);
2208 return isl_stat_error
;
2209 isl_int_lcm(*d
, *d
, cst
->d
);
2213 rec
= isl_poly_as_rec(poly
);
2215 return isl_stat_error
;
2217 for (i
= 0; i
< rec
->n
; ++i
)
2218 poly_update_den(rec
->p
[i
], d
);
2223 __isl_give isl_val
*isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
)
2229 d
= isl_val_one(isl_qpolynomial_get_ctx(qp
));
2232 if (poly_update_den(qp
->poly
, &d
->n
) < 0)
2233 return isl_val_free(d
);
2237 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2238 __isl_take isl_space
*domain
, int pos
, int power
)
2240 struct isl_ctx
*ctx
;
2247 return isl_qpolynomial_alloc(domain
, 0,
2248 isl_poly_var_pow(ctx
, pos
, power
));
2251 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2252 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2254 if (isl_space_check_is_set(domain
) < 0)
2256 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2259 pos
+= isl_space_offset(domain
, type
);
2261 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2263 isl_space_free(domain
);
2267 __isl_give isl_poly
*isl_poly_subs(__isl_take isl_poly
*poly
,
2268 unsigned first
, unsigned n
, __isl_keep isl_poly
**subs
)
2273 isl_poly
*base
, *res
;
2275 is_cst
= isl_poly_is_cst(poly
);
2277 return isl_poly_free(poly
);
2281 if (poly
->var
< first
)
2284 rec
= isl_poly_as_rec(poly
);
2288 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
2290 if (poly
->var
>= first
+ n
)
2291 base
= isl_poly_var_pow(poly
->ctx
, poly
->var
, 1);
2293 base
= isl_poly_copy(subs
[poly
->var
- first
]);
2295 res
= isl_poly_subs(isl_poly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2296 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2298 t
= isl_poly_subs(isl_poly_copy(rec
->p
[i
]), first
, n
, subs
);
2299 res
= isl_poly_mul(res
, isl_poly_copy(base
));
2300 res
= isl_poly_sum(res
, t
);
2303 isl_poly_free(base
);
2304 isl_poly_free(poly
);
2308 isl_poly_free(poly
);
2312 __isl_give isl_poly
*isl_poly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2313 isl_int denom
, unsigned len
)
2318 isl_assert(ctx
, len
>= 1, return NULL
);
2320 poly
= isl_poly_rat_cst(ctx
, f
[0], denom
);
2321 for (i
= 0; i
< len
- 1; ++i
) {
2325 if (isl_int_is_zero(f
[1 + i
]))
2328 c
= isl_poly_rat_cst(ctx
, f
[1 + i
], denom
);
2329 t
= isl_poly_var_pow(ctx
, i
, 1);
2330 t
= isl_poly_mul(c
, t
);
2331 poly
= isl_poly_sum(poly
, t
);
2337 /* Remove common factor of non-constant terms and denominator.
2339 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2341 isl_ctx
*ctx
= qp
->div
->ctx
;
2342 unsigned total
= qp
->div
->n_col
- 2;
2344 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2345 isl_int_gcd(ctx
->normalize_gcd
,
2346 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2347 if (isl_int_is_one(ctx
->normalize_gcd
))
2350 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2351 ctx
->normalize_gcd
, total
);
2352 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2353 ctx
->normalize_gcd
);
2354 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2355 ctx
->normalize_gcd
);
2358 /* Replace the integer division identified by "div" by the polynomial "s".
2359 * The integer division is assumed not to appear in the definition
2360 * of any other integer divisions.
2362 static __isl_give isl_qpolynomial
*substitute_div(
2363 __isl_take isl_qpolynomial
*qp
, int div
, __isl_take isl_poly
*s
)
2373 qp
= isl_qpolynomial_cow(qp
);
2377 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2380 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ div
, 1, &s
);
2384 ctx
= isl_qpolynomial_get_ctx(qp
);
2385 reordering
= isl_alloc_array(ctx
, int, div_pos
+ qp
->div
->n_row
);
2388 for (i
= 0; i
< div_pos
+ div
; ++i
)
2390 for (i
= div_pos
+ div
+ 1; i
< div_pos
+ qp
->div
->n_row
; ++i
)
2391 reordering
[i
] = i
- 1;
2392 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2393 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + div_pos
+ div
, 1);
2394 qp
->poly
= reorder(qp
->poly
, reordering
);
2397 if (!qp
->poly
|| !qp
->div
)
2403 isl_qpolynomial_free(qp
);
2408 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2409 * divisions because d is equal to 1 by their definition, i.e., e.
2411 static __isl_give isl_qpolynomial
*substitute_non_divs(
2412 __isl_take isl_qpolynomial
*qp
)
2418 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2420 return isl_qpolynomial_free(qp
);
2422 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2423 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2425 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2426 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
2428 isl_seq_combine(qp
->div
->row
[j
] + 1,
2429 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2430 qp
->div
->row
[j
][2 + div_pos
+ i
],
2431 qp
->div
->row
[i
] + 1, 1 + div_pos
+ i
);
2432 isl_int_set_si(qp
->div
->row
[j
][2 + div_pos
+ i
], 0);
2433 normalize_div(qp
, j
);
2435 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2436 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2437 qp
= substitute_div(qp
, i
, s
);
2444 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2445 * with d the denominator. When replacing the coefficient e of x by
2446 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2447 * inside the division, so we need to add floor(e/d) * x outside.
2448 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2449 * to adjust the coefficient of x in each later div that depends on the
2450 * current div "div" and also in the affine expressions in the rows of "mat"
2451 * (if they too depend on "div").
2453 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2454 __isl_keep isl_mat
**mat
)
2458 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2461 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2462 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2463 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2465 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2466 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2467 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2468 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2469 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2470 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2472 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2473 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2479 /* Check if the last non-zero coefficient is bigger that half of the
2480 * denominator. If so, we will invert the div to further reduce the number
2481 * of distinct divs that may appear.
2482 * If the last non-zero coefficient is exactly half the denominator,
2483 * then we continue looking for earlier coefficients that are bigger
2484 * than half the denominator.
2486 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2491 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2492 if (isl_int_is_zero(div
->row
[row
][i
]))
2494 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2495 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2496 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2506 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2507 * We only invert the coefficients of e (and the coefficient of q in
2508 * later divs and in the rows of "mat"). After calling this function, the
2509 * coefficients of e should be reduced again.
2511 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2512 __isl_keep isl_mat
**mat
)
2514 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2516 isl_seq_neg(qp
->div
->row
[div
] + 1,
2517 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2518 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2519 isl_int_add(qp
->div
->row
[div
][1],
2520 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2521 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2522 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2523 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2526 /* Reduce all divs of "qp" to have coefficients
2527 * in the interval [0, d-1], with d the denominator and such that the
2528 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2529 * The modifications to the integer divisions need to be reflected
2530 * in the factors of the polynomial that refer to the original
2531 * integer divisions. To this end, the modifications are collected
2532 * as a set of affine expressions and then plugged into the polynomial.
2534 * After the reduction, some divs may have become redundant or identical,
2535 * so we call substitute_non_divs and sort_divs. If these functions
2536 * eliminate divs or merge two or more divs into one, the coefficients
2537 * of the enclosing divs may have to be reduced again, so we call
2538 * ourselves recursively if the number of divs decreases.
2540 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2547 isl_size n_div
, total
, new_n_div
;
2549 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2550 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2551 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2552 if (total
< 0 || n_div
< 0)
2553 return isl_qpolynomial_free(qp
);
2554 ctx
= isl_qpolynomial_get_ctx(qp
);
2555 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2557 for (i
= 0; i
< n_div
; ++i
)
2558 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2560 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2561 normalize_div(qp
, i
);
2562 reduce_div(qp
, i
, &mat
);
2563 if (needs_invert(qp
->div
, i
)) {
2564 invert_div(qp
, i
, &mat
);
2565 reduce_div(qp
, i
, &mat
);
2571 s
= isl_alloc_array(ctx
, struct isl_poly
*, n_div
);
2574 for (i
= 0; i
< n_div
; ++i
)
2575 s
[i
] = isl_poly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2577 qp
->poly
= isl_poly_subs(qp
->poly
, o_div
- 1, n_div
, s
);
2578 for (i
= 0; i
< n_div
; ++i
)
2579 isl_poly_free(s
[i
]);
2586 qp
= substitute_non_divs(qp
);
2588 new_n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2590 return isl_qpolynomial_free(qp
);
2591 if (new_n_div
< n_div
)
2592 return reduce_divs(qp
);
2596 isl_qpolynomial_free(qp
);
2601 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2602 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2604 struct isl_qpolynomial
*qp
;
2607 qp
= isl_qpolynomial_zero_on_domain(domain
);
2611 cst
= isl_poly_as_cst(qp
->poly
);
2612 isl_int_set(cst
->n
, n
);
2613 isl_int_set(cst
->d
, d
);
2618 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2620 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2621 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2623 isl_qpolynomial
*qp
;
2626 qp
= isl_qpolynomial_zero_on_domain(domain
);
2630 cst
= isl_poly_as_cst(qp
->poly
);
2631 isl_int_set(cst
->n
, val
->n
);
2632 isl_int_set(cst
->d
, val
->d
);
2638 isl_qpolynomial_free(qp
);
2642 static isl_stat
poly_set_active(__isl_keep isl_poly
*poly
, int *active
, int d
)
2648 is_cst
= isl_poly_is_cst(poly
);
2650 return isl_stat_error
;
2655 active
[poly
->var
] = 1;
2657 rec
= isl_poly_as_rec(poly
);
2658 for (i
= 0; i
< rec
->n
; ++i
)
2659 if (poly_set_active(rec
->p
[i
], active
, d
) < 0)
2660 return isl_stat_error
;
2665 static isl_stat
set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2671 space
= isl_qpolynomial_peek_domain_space(qp
);
2672 d
= isl_space_dim(space
, isl_dim_all
);
2673 if (d
< 0 || !active
)
2674 return isl_stat_error
;
2676 for (i
= 0; i
< d
; ++i
)
2677 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2678 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2684 return poly_set_active(qp
->poly
, active
, d
);
2688 #define TYPE isl_qpolynomial
2690 #include "check_type_range_templ.c"
2692 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2693 enum isl_dim_type type
, unsigned first
, unsigned n
)
2697 isl_bool involves
= isl_bool_false
;
2703 return isl_bool_error
;
2705 return isl_bool_false
;
2707 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2708 return isl_bool_error
;
2709 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2710 type
== isl_dim_in
, return isl_bool_error
);
2712 space
= isl_qpolynomial_peek_domain_space(qp
);
2713 d
= isl_space_dim(space
, isl_dim_all
);
2715 return isl_bool_error
;
2716 active
= isl_calloc_array(qp
->dim
->ctx
, int, d
);
2717 if (set_active(qp
, active
) < 0)
2720 offset
= isl_qpolynomial_domain_var_offset(qp
, domain_type(type
));
2724 for (i
= 0; i
< n
; ++i
)
2725 if (active
[first
+ i
]) {
2726 involves
= isl_bool_true
;
2735 return isl_bool_error
;
2738 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2739 * of the divs that do appear in the quasi-polynomial.
2741 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2742 __isl_take isl_qpolynomial
*qp
)
2749 int *reordering
= NULL
;
2756 if (qp
->div
->n_row
== 0)
2759 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2761 return isl_qpolynomial_free(qp
);
2762 len
= qp
->div
->n_col
- 2;
2763 ctx
= isl_qpolynomial_get_ctx(qp
);
2764 active
= isl_calloc_array(ctx
, int, len
);
2768 if (poly_set_active(qp
->poly
, active
, len
) < 0)
2771 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2772 if (!active
[div_pos
+ i
]) {
2776 for (j
= 0; j
< i
; ++j
) {
2777 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ j
]))
2779 active
[div_pos
+ j
] = 1;
2789 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2793 for (i
= 0; i
< div_pos
; ++i
)
2797 n_div
= qp
->div
->n_row
;
2798 for (i
= 0; i
< n_div
; ++i
) {
2799 if (!active
[div_pos
+ i
]) {
2800 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2801 qp
->div
= isl_mat_drop_cols(qp
->div
,
2802 2 + div_pos
+ i
- skip
, 1);
2805 reordering
[div_pos
+ i
] = div_pos
+ i
- skip
;
2808 qp
->poly
= reorder(qp
->poly
, reordering
);
2810 if (!qp
->poly
|| !qp
->div
)
2820 isl_qpolynomial_free(qp
);
2824 __isl_give isl_poly
*isl_poly_drop(__isl_take isl_poly
*poly
,
2825 unsigned first
, unsigned n
)
2832 if (n
== 0 || poly
->var
< 0 || poly
->var
< first
)
2834 if (poly
->var
< first
+ n
) {
2835 poly
= replace_by_constant_term(poly
);
2836 return isl_poly_drop(poly
, first
, n
);
2838 poly
= isl_poly_cow(poly
);
2842 rec
= isl_poly_as_rec(poly
);
2846 for (i
= 0; i
< rec
->n
; ++i
) {
2847 rec
->p
[i
] = isl_poly_drop(rec
->p
[i
], first
, n
);
2854 isl_poly_free(poly
);
2858 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2859 __isl_take isl_qpolynomial
*qp
,
2860 enum isl_dim_type type
, unsigned pos
, const char *s
)
2862 qp
= isl_qpolynomial_cow(qp
);
2865 if (type
== isl_dim_out
)
2866 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2867 "cannot set name of output/set dimension",
2868 return isl_qpolynomial_free(qp
));
2869 type
= domain_type(type
);
2870 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2875 isl_qpolynomial_free(qp
);
2879 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2880 __isl_take isl_qpolynomial
*qp
,
2881 enum isl_dim_type type
, unsigned first
, unsigned n
)
2887 if (type
== isl_dim_out
)
2888 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2889 "cannot drop output/set dimension",
2891 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2892 return isl_qpolynomial_free(qp
);
2893 type
= domain_type(type
);
2894 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2897 qp
= isl_qpolynomial_cow(qp
);
2901 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2902 type
== isl_dim_set
, goto error
);
2904 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2908 offset
= isl_qpolynomial_domain_var_offset(qp
, type
);
2913 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2917 qp
->poly
= isl_poly_drop(qp
->poly
, first
, n
);
2923 isl_qpolynomial_free(qp
);
2927 /* Project the domain of the quasi-polynomial onto its parameter space.
2928 * The quasi-polynomial may not involve any of the domain dimensions.
2930 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2931 __isl_take isl_qpolynomial
*qp
)
2937 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2939 return isl_qpolynomial_free(qp
);
2940 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2942 return isl_qpolynomial_free(qp
);
2944 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2945 "polynomial involves some of the domain dimensions",
2946 return isl_qpolynomial_free(qp
));
2947 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2948 space
= isl_qpolynomial_get_domain_space(qp
);
2949 space
= isl_space_params(space
);
2950 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2954 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2955 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2965 if (eq
->n_eq
== 0) {
2966 isl_basic_set_free(eq
);
2970 qp
= isl_qpolynomial_cow(qp
);
2973 qp
->div
= isl_mat_cow(qp
->div
);
2977 total
= isl_basic_set_offset(eq
, isl_dim_div
);
2979 isl_int_init(denom
);
2980 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2981 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2982 if (j
< 0 || j
== 0 || j
>= total
)
2985 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2986 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2988 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2989 &qp
->div
->row
[k
][0]);
2990 normalize_div(qp
, k
);
2993 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2994 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2995 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2996 isl_int_set_si(eq
->eq
[i
][j
], 0);
2998 poly
= isl_poly_from_affine(qp
->dim
->ctx
,
2999 eq
->eq
[i
], denom
, total
);
3000 qp
->poly
= isl_poly_subs(qp
->poly
, j
- 1, 1, &poly
);
3001 isl_poly_free(poly
);
3003 isl_int_clear(denom
);
3008 isl_basic_set_free(eq
);
3010 qp
= substitute_non_divs(qp
);
3015 isl_basic_set_free(eq
);
3016 isl_qpolynomial_free(qp
);
3020 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3022 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
3023 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
3027 if (qp
->div
->n_row
> 0)
3028 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
3029 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
3031 isl_basic_set_free(eq
);
3032 isl_qpolynomial_free(qp
);
3036 /* Look for equalities among the variables shared by context and qp
3037 * and the integer divisions of qp, if any.
3038 * The equalities are then used to eliminate variables and/or integer
3039 * divisions from qp.
3041 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
3042 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3044 isl_local_space
*ls
;
3047 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3048 context
= isl_local_space_lift_set(ls
, context
);
3050 aff
= isl_set_affine_hull(context
);
3051 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
3054 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
3055 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3057 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
3058 isl_set
*dom_context
= isl_set_universe(space
);
3059 dom_context
= isl_set_intersect_params(dom_context
, context
);
3060 return isl_qpolynomial_gist(qp
, dom_context
);
3063 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
3064 __isl_take isl_qpolynomial
*qp
)
3070 if (isl_qpolynomial_is_zero(qp
)) {
3071 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
3072 isl_qpolynomial_free(qp
);
3073 return isl_pw_qpolynomial_zero(dim
);
3076 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
3077 return isl_pw_qpolynomial_alloc(dom
, qp
);
3080 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3083 #define PW isl_pw_qpolynomial
3085 #define EL isl_qpolynomial
3087 #define EL_IS_ZERO is_zero
3091 #define IS_ZERO is_zero
3094 #undef DEFAULT_IS_ZERO
3095 #define DEFAULT_IS_ZERO 1
3099 #include <isl_pw_templ.c>
3100 #include <isl_pw_eval.c>
3103 #define BASE pw_qpolynomial
3105 #include <isl_union_single.c>
3106 #include <isl_union_eval.c>
3107 #include <isl_union_neg.c>
3109 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
3117 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
3120 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
3123 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
3124 __isl_take isl_pw_qpolynomial
*pwqp1
,
3125 __isl_take isl_pw_qpolynomial
*pwqp2
)
3127 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
3130 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
3131 __isl_take isl_pw_qpolynomial
*pwqp1
,
3132 __isl_take isl_pw_qpolynomial
*pwqp2
)
3135 struct isl_pw_qpolynomial
*res
;
3137 if (!pwqp1
|| !pwqp2
)
3140 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3143 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3144 isl_pw_qpolynomial_free(pwqp2
);
3148 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3149 isl_pw_qpolynomial_free(pwqp1
);
3153 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3154 isl_pw_qpolynomial_free(pwqp1
);
3158 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3159 isl_pw_qpolynomial_free(pwqp2
);
3163 n
= pwqp1
->n
* pwqp2
->n
;
3164 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3166 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3167 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3168 struct isl_set
*common
;
3169 struct isl_qpolynomial
*prod
;
3170 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3171 isl_set_copy(pwqp2
->p
[j
].set
));
3172 if (isl_set_plain_is_empty(common
)) {
3173 isl_set_free(common
);
3177 prod
= isl_qpolynomial_mul(
3178 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3179 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3181 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3185 isl_pw_qpolynomial_free(pwqp1
);
3186 isl_pw_qpolynomial_free(pwqp2
);
3190 isl_pw_qpolynomial_free(pwqp1
);
3191 isl_pw_qpolynomial_free(pwqp2
);
3195 __isl_give isl_val
*isl_poly_eval(__isl_take isl_poly
*poly
,
3196 __isl_take isl_vec
*vec
)
3204 is_cst
= isl_poly_is_cst(poly
);
3209 res
= isl_poly_get_constant_val(poly
);
3210 isl_poly_free(poly
);
3214 rec
= isl_poly_as_rec(poly
);
3218 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
3220 base
= isl_val_rat_from_isl_int(poly
->ctx
,
3221 vec
->el
[1 + poly
->var
], vec
->el
[0]);
3223 res
= isl_poly_eval(isl_poly_copy(rec
->p
[rec
->n
- 1]),
3226 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3227 res
= isl_val_mul(res
, isl_val_copy(base
));
3228 res
= isl_val_add(res
, isl_poly_eval(isl_poly_copy(rec
->p
[i
]),
3229 isl_vec_copy(vec
)));
3233 isl_poly_free(poly
);
3237 isl_poly_free(poly
);
3242 /* Evaluate "qp" in the void point "pnt".
3243 * In particular, return the value NaN.
3245 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3246 __isl_take isl_point
*pnt
)
3250 ctx
= isl_point_get_ctx(pnt
);
3251 isl_qpolynomial_free(qp
);
3252 isl_point_free(pnt
);
3253 return isl_val_nan(ctx
);
3256 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3257 __isl_take isl_point
*pnt
)
3265 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3266 is_void
= isl_point_is_void(pnt
);
3270 return eval_void(qp
, pnt
);
3272 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3274 v
= isl_poly_eval(isl_poly_copy(qp
->poly
), ext
);
3276 isl_qpolynomial_free(qp
);
3277 isl_point_free(pnt
);
3281 isl_qpolynomial_free(qp
);
3282 isl_point_free(pnt
);
3286 int isl_poly_cmp(__isl_keep isl_poly_cst
*cst1
, __isl_keep isl_poly_cst
*cst2
)
3291 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3292 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3293 cmp
= isl_int_sgn(t
);
3298 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3299 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3300 unsigned first
, unsigned n
)
3308 if (type
== isl_dim_out
)
3309 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3310 "cannot insert output/set dimensions",
3312 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3313 return isl_qpolynomial_free(qp
);
3314 type
= domain_type(type
);
3315 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3318 qp
= isl_qpolynomial_cow(qp
);
3322 g_pos
= pos(qp
->dim
, type
) + first
;
3324 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3328 total
= qp
->div
->n_col
- 2;
3329 if (total
> g_pos
) {
3331 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3334 for (i
= 0; i
< total
- g_pos
; ++i
)
3336 qp
->poly
= expand(qp
->poly
, exp
, g_pos
);
3342 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3348 isl_qpolynomial_free(qp
);
3352 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3353 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3357 pos
= isl_qpolynomial_dim(qp
, type
);
3359 return isl_qpolynomial_free(qp
);
3361 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3364 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3365 __isl_take isl_pw_qpolynomial
*pwqp
,
3366 enum isl_dim_type type
, unsigned n
)
3370 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3372 return isl_pw_qpolynomial_free(pwqp
);
3374 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3377 static int *reordering_move(isl_ctx
*ctx
,
3378 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3383 reordering
= isl_alloc_array(ctx
, int, len
);
3388 for (i
= 0; i
< dst
; ++i
)
3390 for (i
= 0; i
< n
; ++i
)
3391 reordering
[src
+ i
] = dst
+ i
;
3392 for (i
= 0; i
< src
- dst
; ++i
)
3393 reordering
[dst
+ i
] = dst
+ n
+ i
;
3394 for (i
= 0; i
< len
- src
- n
; ++i
)
3395 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3397 for (i
= 0; i
< src
; ++i
)
3399 for (i
= 0; i
< n
; ++i
)
3400 reordering
[src
+ i
] = dst
+ i
;
3401 for (i
= 0; i
< dst
- src
; ++i
)
3402 reordering
[src
+ n
+ i
] = src
+ i
;
3403 for (i
= 0; i
< len
- dst
- n
; ++i
)
3404 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3410 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3411 __isl_take isl_qpolynomial
*qp
,
3412 enum isl_dim_type dst_type
, unsigned dst_pos
,
3413 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3422 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3423 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3424 "cannot move output/set dimension",
3426 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3427 return isl_qpolynomial_free(qp
);
3428 if (dst_type
== isl_dim_in
)
3429 dst_type
= isl_dim_set
;
3430 if (src_type
== isl_dim_in
)
3431 src_type
= isl_dim_set
;
3434 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3435 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3438 qp
= isl_qpolynomial_cow(qp
);
3442 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3443 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3444 if (dst_type
> src_type
)
3447 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3454 reordering
= reordering_move(qp
->dim
->ctx
,
3455 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3459 qp
->poly
= reorder(qp
->poly
, reordering
);
3464 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3470 isl_qpolynomial_free(qp
);
3474 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3475 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3480 space
= isl_space_domain(space
);
3484 d
= isl_space_dim(space
, isl_dim_all
);
3485 poly
= d
< 0 ? NULL
: isl_poly_from_affine(space
->ctx
, f
, denom
, 1 + d
);
3487 return isl_qpolynomial_alloc(space
, 0, poly
);
3490 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3494 isl_qpolynomial
*qp
;
3499 ctx
= isl_aff_get_ctx(aff
);
3500 poly
= isl_poly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3503 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3504 aff
->ls
->div
->n_row
, poly
);
3508 isl_mat_free(qp
->div
);
3509 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3510 qp
->div
= isl_mat_cow(qp
->div
);
3515 qp
= reduce_divs(qp
);
3516 qp
= remove_redundant_divs(qp
);
3520 return isl_qpolynomial_free(qp
);
3523 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3524 __isl_take isl_pw_aff
*pwaff
)
3527 isl_pw_qpolynomial
*pwqp
;
3532 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3535 for (i
= 0; i
< pwaff
->n
; ++i
) {
3537 isl_qpolynomial
*qp
;
3539 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3540 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3541 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3544 isl_pw_aff_free(pwaff
);
3548 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3549 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3553 aff
= isl_constraint_get_bound(c
, type
, pos
);
3554 isl_constraint_free(c
);
3555 return isl_qpolynomial_from_aff(aff
);
3558 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3559 * in "qp" by subs[i].
3561 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3562 __isl_take isl_qpolynomial
*qp
,
3563 enum isl_dim_type type
, unsigned first
, unsigned n
,
3564 __isl_keep isl_qpolynomial
**subs
)
3572 qp
= isl_qpolynomial_cow(qp
);
3576 if (type
== isl_dim_out
)
3577 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3578 "cannot substitute output/set dimension",
3580 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3581 return isl_qpolynomial_free(qp
);
3582 type
= domain_type(type
);
3584 for (i
= 0; i
< n
; ++i
)
3588 for (i
= 0; i
< n
; ++i
)
3589 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3592 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3593 for (i
= 0; i
< n
; ++i
)
3594 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3596 first
+= pos(qp
->dim
, type
);
3598 polys
= isl_alloc_array(qp
->dim
->ctx
, struct isl_poly
*, n
);
3601 for (i
= 0; i
< n
; ++i
)
3602 polys
[i
] = subs
[i
]->poly
;
3604 qp
->poly
= isl_poly_subs(qp
->poly
, first
, n
, polys
);
3613 isl_qpolynomial_free(qp
);
3617 /* Extend "bset" with extra set dimensions for each integer division
3618 * in "qp" and then call "fn" with the extended bset and the polynomial
3619 * that results from replacing each of the integer divisions by the
3620 * corresponding extra set dimension.
3622 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3623 __isl_keep isl_basic_set
*bset
,
3624 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3625 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3628 isl_local_space
*ls
;
3629 isl_qpolynomial
*poly
;
3632 return isl_stat_error
;
3633 if (qp
->div
->n_row
== 0)
3634 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3637 space
= isl_space_copy(qp
->dim
);
3638 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3639 poly
= isl_qpolynomial_alloc(space
, 0, isl_poly_copy(qp
->poly
));
3640 bset
= isl_basic_set_copy(bset
);
3641 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3642 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3644 return fn(bset
, poly
, user
);
3647 /* Return total degree in variables first (inclusive) up to last (exclusive).
3649 int isl_poly_degree(__isl_keep isl_poly
*poly
, int first
, int last
)
3653 isl_bool is_zero
, is_cst
;
3656 is_zero
= isl_poly_is_zero(poly
);
3661 is_cst
= isl_poly_is_cst(poly
);
3664 if (is_cst
|| poly
->var
< first
)
3667 rec
= isl_poly_as_rec(poly
);
3671 for (i
= 0; i
< rec
->n
; ++i
) {
3674 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3679 d
= isl_poly_degree(rec
->p
[i
], first
, last
);
3680 if (poly
->var
< last
)
3689 /* Return total degree in set variables.
3691 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3699 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3700 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3703 return isl_poly_degree(poly
->poly
, ovar
, ovar
+ nvar
);
3706 __isl_give isl_poly
*isl_poly_coeff(__isl_keep isl_poly
*poly
,
3707 unsigned pos
, int deg
)
3713 is_cst
= isl_poly_is_cst(poly
);
3716 if (is_cst
|| poly
->var
< pos
) {
3718 return isl_poly_copy(poly
);
3720 return isl_poly_zero(poly
->ctx
);
3723 rec
= isl_poly_as_rec(poly
);
3727 if (poly
->var
== pos
) {
3729 return isl_poly_copy(rec
->p
[deg
]);
3731 return isl_poly_zero(poly
->ctx
);
3734 poly
= isl_poly_copy(poly
);
3735 poly
= isl_poly_cow(poly
);
3736 rec
= isl_poly_as_rec(poly
);
3740 for (i
= 0; i
< rec
->n
; ++i
) {
3742 t
= isl_poly_coeff(rec
->p
[i
], pos
, deg
);
3745 isl_poly_free(rec
->p
[i
]);
3751 isl_poly_free(poly
);
3755 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3757 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3758 __isl_keep isl_qpolynomial
*qp
,
3759 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3768 if (type
== isl_dim_out
)
3769 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3770 "output/set dimension does not have a coefficient",
3772 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
3774 type
= domain_type(type
);
3776 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3777 poly
= isl_poly_coeff(qp
->poly
, g_pos
, deg
);
3779 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
),
3780 qp
->div
->n_row
, poly
);
3783 isl_mat_free(c
->div
);
3784 c
->div
= isl_mat_copy(qp
->div
);
3789 isl_qpolynomial_free(c
);
3793 /* Homogenize the polynomial in the variables first (inclusive) up to
3794 * last (exclusive) by inserting powers of variable first.
3795 * Variable first is assumed not to appear in the input.
3797 __isl_give isl_poly
*isl_poly_homogenize(__isl_take isl_poly
*poly
, int deg
,
3798 int target
, int first
, int last
)
3801 isl_bool is_zero
, is_cst
;
3804 is_zero
= isl_poly_is_zero(poly
);
3806 return isl_poly_free(poly
);
3811 is_cst
= isl_poly_is_cst(poly
);
3813 return isl_poly_free(poly
);
3814 if (is_cst
|| poly
->var
< first
) {
3817 hom
= isl_poly_var_pow(poly
->ctx
, first
, target
- deg
);
3820 rec
= isl_poly_as_rec(hom
);
3821 rec
->p
[target
- deg
] = isl_poly_mul(rec
->p
[target
- deg
], poly
);
3826 poly
= isl_poly_cow(poly
);
3827 rec
= isl_poly_as_rec(poly
);
3831 for (i
= 0; i
< rec
->n
; ++i
) {
3832 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3834 return isl_poly_free(poly
);
3837 rec
->p
[i
] = isl_poly_homogenize(rec
->p
[i
],
3838 poly
->var
< last
? deg
+ i
: i
, target
,
3846 isl_poly_free(poly
);
3850 /* Homogenize the polynomial in the set variables by introducing
3851 * powers of an extra set variable at position 0.
3853 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3854 __isl_take isl_qpolynomial
*poly
)
3858 int deg
= isl_qpolynomial_degree(poly
);
3863 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3864 poly
= isl_qpolynomial_cow(poly
);
3868 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3869 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3871 return isl_qpolynomial_free(poly
);
3872 poly
->poly
= isl_poly_homogenize(poly
->poly
, 0, deg
, ovar
, ovar
+ nvar
);
3878 isl_qpolynomial_free(poly
);
3882 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3883 __isl_take isl_mat
*div
)
3889 d
= isl_space_dim(space
, isl_dim_all
);
3895 term
= isl_calloc(space
->ctx
, struct isl_term
,
3896 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3903 isl_int_init(term
->n
);
3904 isl_int_init(term
->d
);
3908 isl_space_free(space
);
3913 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3922 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3928 total
= isl_term_dim(term
, isl_dim_all
);
3932 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3936 isl_int_set(dup
->n
, term
->n
);
3937 isl_int_set(dup
->d
, term
->d
);
3939 for (i
= 0; i
< total
; ++i
)
3940 dup
->pow
[i
] = term
->pow
[i
];
3945 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3953 return isl_term_dup(term
);
3956 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3961 if (--term
->ref
> 0)
3964 isl_space_free(term
->dim
);
3965 isl_mat_free(term
->div
);
3966 isl_int_clear(term
->n
);
3967 isl_int_clear(term
->d
);
3973 isl_size
isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3978 return isl_size_error
;
3983 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3984 case isl_dim_div
: return term
->div
->n_row
;
3985 case isl_dim_all
: dim
= isl_space_dim(term
->dim
, isl_dim_all
);
3987 return isl_size_error
;
3988 return dim
+ term
->div
->n_row
;
3989 default: return isl_size_error
;
3993 /* Return the space of "term".
3995 static __isl_keep isl_space
*isl_term_peek_space(__isl_keep isl_term
*term
)
3997 return term
? term
->dim
: NULL
;
4000 /* Return the offset of the first variable of type "type" within
4001 * the variables of "term".
4003 static int isl_term_offset(__isl_keep isl_term
*term
, enum isl_dim_type type
)
4007 space
= isl_term_peek_space(term
);
4013 case isl_dim_set
: return isl_space_offset(space
, type
);
4014 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
4016 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4017 "invalid dimension type", return -1);
4021 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
4023 return term
? term
->dim
->ctx
: NULL
;
4026 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
4030 isl_int_set(*n
, term
->n
);
4033 /* Return the coefficient of the term "term".
4035 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
4040 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
4045 #define TYPE isl_term
4047 #include "check_type_range_templ.c"
4049 isl_size
isl_term_get_exp(__isl_keep isl_term
*term
,
4050 enum isl_dim_type type
, unsigned pos
)
4054 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
4055 return isl_size_error
;
4056 offset
= isl_term_offset(term
, type
);
4058 return isl_size_error
;
4060 return term
->pow
[offset
+ pos
];
4063 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
4065 isl_local_space
*ls
;
4068 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
4071 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
4072 isl_mat_copy(term
->div
));
4073 aff
= isl_aff_alloc(ls
);
4077 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
4079 aff
= isl_aff_normalize(aff
);
4084 __isl_give isl_term
*isl_poly_foreach_term(__isl_keep isl_poly
*poly
,
4085 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
4086 __isl_take isl_term
*term
, void *user
)
4089 isl_bool is_zero
, is_bad
, is_cst
;
4092 is_zero
= isl_poly_is_zero(poly
);
4093 if (is_zero
< 0 || !term
)
4099 is_cst
= isl_poly_is_cst(poly
);
4100 is_bad
= isl_poly_is_nan(poly
);
4101 if (is_bad
>= 0 && !is_bad
)
4102 is_bad
= isl_poly_is_infty(poly
);
4103 if (is_bad
>= 0 && !is_bad
)
4104 is_bad
= isl_poly_is_neginfty(poly
);
4105 if (is_cst
< 0 || is_bad
< 0)
4106 return isl_term_free(term
);
4108 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4109 "cannot handle NaN/infty polynomial",
4110 return isl_term_free(term
));
4114 cst
= isl_poly_as_cst(poly
);
4117 term
= isl_term_cow(term
);
4120 isl_int_set(term
->n
, cst
->n
);
4121 isl_int_set(term
->d
, cst
->d
);
4122 if (fn(isl_term_copy(term
), user
) < 0)
4127 rec
= isl_poly_as_rec(poly
);
4131 for (i
= 0; i
< rec
->n
; ++i
) {
4132 term
= isl_term_cow(term
);
4135 term
->pow
[poly
->var
] = i
;
4136 term
= isl_poly_foreach_term(rec
->p
[i
], fn
, term
, user
);
4140 term
->pow
[poly
->var
] = 0;
4144 isl_term_free(term
);
4148 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
4149 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
4154 return isl_stat_error
;
4156 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
4158 return isl_stat_error
;
4160 term
= isl_poly_foreach_term(qp
->poly
, fn
, term
, user
);
4162 isl_term_free(term
);
4164 return term
? isl_stat_ok
: isl_stat_error
;
4167 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
4170 isl_qpolynomial
*qp
;
4174 n
= isl_term_dim(term
, isl_dim_all
);
4176 term
= isl_term_free(term
);
4180 poly
= isl_poly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4181 for (i
= 0; i
< n
; ++i
) {
4184 poly
= isl_poly_mul(poly
,
4185 isl_poly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4188 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
),
4189 term
->div
->n_row
, poly
);
4192 isl_mat_free(qp
->div
);
4193 qp
->div
= isl_mat_copy(term
->div
);
4197 isl_term_free(term
);
4200 isl_qpolynomial_free(qp
);
4201 isl_term_free(term
);
4205 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4206 __isl_take isl_space
*space
)
4210 isl_size total
, d_set
, d_qp
;
4215 if (isl_space_is_equal(qp
->dim
, space
)) {
4216 isl_space_free(space
);
4220 qp
= isl_qpolynomial_cow(qp
);
4224 d_set
= isl_space_dim(space
, isl_dim_set
);
4225 d_qp
= isl_qpolynomial_domain_dim(qp
, isl_dim_set
);
4226 extra
= d_set
- d_qp
;
4227 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4228 if (d_set
< 0 || d_qp
< 0 || total
< 0)
4230 if (qp
->div
->n_row
) {
4233 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4236 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4238 qp
->poly
= expand(qp
->poly
, exp
, total
);
4243 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4246 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4247 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4249 isl_space_free(qp
->dim
);
4254 isl_space_free(space
);
4255 isl_qpolynomial_free(qp
);
4259 /* For each parameter or variable that does not appear in qp,
4260 * first eliminate the variable from all constraints and then set it to zero.
4262 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4263 __isl_keep isl_qpolynomial
*qp
)
4271 d
= isl_set_dim(set
, isl_dim_all
);
4275 active
= isl_calloc_array(set
->ctx
, int, d
);
4276 if (set_active(qp
, active
) < 0)
4279 for (i
= 0; i
< d
; ++i
)
4288 nparam
= isl_set_dim(set
, isl_dim_param
);
4289 nvar
= isl_set_dim(set
, isl_dim_set
);
4290 if (nparam
< 0 || nvar
< 0)
4292 for (i
= 0; i
< nparam
; ++i
) {
4295 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4296 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4298 for (i
= 0; i
< nvar
; ++i
) {
4299 if (active
[nparam
+ i
])
4301 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4302 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4314 struct isl_opt_data
{
4315 isl_qpolynomial
*qp
;
4321 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4323 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4326 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4330 } else if (data
->max
) {
4331 data
->opt
= isl_val_max(data
->opt
, val
);
4333 data
->opt
= isl_val_min(data
->opt
, val
);
4339 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4340 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4342 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4348 is_cst
= isl_poly_is_cst(qp
->poly
);
4353 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4354 isl_qpolynomial_free(qp
);
4358 set
= fix_inactive(set
, qp
);
4361 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4365 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4368 isl_qpolynomial_free(qp
);
4372 isl_qpolynomial_free(qp
);
4373 isl_val_free(data
.opt
);
4377 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4378 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4384 isl_mat
*mat
, *diag
;
4386 qp
= isl_qpolynomial_cow(qp
);
4391 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4393 n_sub
= morph
->inv
->n_row
- 1;
4394 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4395 n_sub
+= qp
->div
->n_row
;
4396 subs
= isl_calloc_array(ctx
, struct isl_poly
*, n_sub
);
4400 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4401 subs
[i
] = isl_poly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4402 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4403 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4404 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4405 subs
[morph
->inv
->n_row
- 1 + i
] =
4406 isl_poly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4408 qp
->poly
= isl_poly_subs(qp
->poly
, 0, n_sub
, subs
);
4410 for (i
= 0; i
< n_sub
; ++i
)
4411 isl_poly_free(subs
[i
]);
4414 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4415 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4416 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4417 mat
= isl_mat_diagonal(mat
, diag
);
4418 qp
->div
= isl_mat_product(qp
->div
, mat
);
4419 isl_space_free(qp
->dim
);
4420 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4422 if (!qp
->poly
|| !qp
->div
|| !qp
->dim
)
4425 isl_morph_free(morph
);
4429 isl_qpolynomial_free(qp
);
4430 isl_morph_free(morph
);
4434 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4435 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4436 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4438 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4439 &isl_pw_qpolynomial_mul
);
4442 /* Reorder the dimension of "qp" according to the given reordering.
4444 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4445 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4449 qp
= isl_qpolynomial_cow(qp
);
4453 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4457 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4461 qp
->poly
= reorder(qp
->poly
, r
->pos
);
4465 space
= isl_reordering_get_space(r
);
4466 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4468 isl_reordering_free(r
);
4471 isl_qpolynomial_free(qp
);
4472 isl_reordering_free(r
);
4476 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4477 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4479 isl_bool equal_params
;
4484 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4485 if (equal_params
< 0)
4487 if (!equal_params
) {
4488 isl_reordering
*exp
;
4490 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4491 exp
= isl_reordering_extend_space(exp
,
4492 isl_qpolynomial_get_domain_space(qp
));
4493 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4496 isl_space_free(model
);
4499 isl_space_free(model
);
4500 isl_qpolynomial_free(qp
);
4504 struct isl_split_periods_data
{
4506 isl_pw_qpolynomial
*res
;
4509 /* Create a slice where the integer division "div" has the fixed value "v".
4510 * In particular, if "div" refers to floor(f/m), then create a slice
4512 * m v <= f <= m v + (m - 1)
4517 * -f + m v + (m - 1) >= 0
4519 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4520 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4523 isl_basic_set
*bset
= NULL
;
4526 total
= isl_space_dim(space
, isl_dim_all
);
4527 if (total
< 0 || !qp
)
4530 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4532 k
= isl_basic_set_alloc_inequality(bset
);
4535 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4536 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4538 k
= isl_basic_set_alloc_inequality(bset
);
4541 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4542 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4543 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4544 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4546 isl_space_free(space
);
4547 return isl_set_from_basic_set(bset
);
4549 isl_basic_set_free(bset
);
4550 isl_space_free(space
);
4554 static isl_stat
split_periods(__isl_take isl_set
*set
,
4555 __isl_take isl_qpolynomial
*qp
, void *user
);
4557 /* Create a slice of the domain "set" such that integer division "div"
4558 * has the fixed value "v" and add the results to data->res,
4559 * replacing the integer division by "v" in "qp".
4561 static isl_stat
set_div(__isl_take isl_set
*set
,
4562 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4563 struct isl_split_periods_data
*data
)
4570 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4571 set
= isl_set_intersect(set
, slice
);
4573 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4577 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4578 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ div
]))
4580 isl_int_addmul(qp
->div
->row
[i
][1],
4581 qp
->div
->row
[i
][2 + div_pos
+ div
], v
);
4582 isl_int_set_si(qp
->div
->row
[i
][2 + div_pos
+ div
], 0);
4585 cst
= isl_poly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4586 qp
= substitute_div(qp
, div
, cst
);
4588 return split_periods(set
, qp
, data
);
4591 isl_qpolynomial_free(qp
);
4592 return isl_stat_error
;
4595 /* Split the domain "set" such that integer division "div"
4596 * has a fixed value (ranging from "min" to "max") on each slice
4597 * and add the results to data->res.
4599 static isl_stat
split_div(__isl_take isl_set
*set
,
4600 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4601 struct isl_split_periods_data
*data
)
4603 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4604 isl_set
*set_i
= isl_set_copy(set
);
4605 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4607 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4611 isl_qpolynomial_free(qp
);
4615 isl_qpolynomial_free(qp
);
4616 return isl_stat_error
;
4619 /* If "qp" refers to any integer division
4620 * that can only attain "max_periods" distinct values on "set"
4621 * then split the domain along those distinct values.
4622 * Add the results (or the original if no splitting occurs)
4625 static isl_stat
split_periods(__isl_take isl_set
*set
,
4626 __isl_take isl_qpolynomial
*qp
, void *user
)
4629 isl_pw_qpolynomial
*pwqp
;
4630 struct isl_split_periods_data
*data
;
4633 isl_stat r
= isl_stat_ok
;
4635 data
= (struct isl_split_periods_data
*)user
;
4640 if (qp
->div
->n_row
== 0) {
4641 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4642 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4646 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4652 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4653 enum isl_lp_result lp_res
;
4655 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + div_pos
,
4656 qp
->div
->n_row
) != -1)
4659 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4660 set
->ctx
->one
, &min
, NULL
, NULL
);
4661 if (lp_res
== isl_lp_error
)
4663 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4665 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4667 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4668 set
->ctx
->one
, &max
, NULL
, NULL
);
4669 if (lp_res
== isl_lp_error
)
4671 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4673 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4675 isl_int_sub(max
, max
, min
);
4676 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4677 isl_int_add(max
, max
, min
);
4682 if (i
< qp
->div
->n_row
) {
4683 r
= split_div(set
, qp
, i
, min
, max
, data
);
4685 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4686 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4698 isl_qpolynomial_free(qp
);
4699 return isl_stat_error
;
4702 /* If any quasi-polynomial in pwqp refers to any integer division
4703 * that can only attain "max_periods" distinct values on its domain
4704 * then split the domain along those distinct values.
4706 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4707 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4709 struct isl_split_periods_data data
;
4711 data
.max_periods
= max_periods
;
4712 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4714 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4717 isl_pw_qpolynomial_free(pwqp
);
4721 isl_pw_qpolynomial_free(data
.res
);
4722 isl_pw_qpolynomial_free(pwqp
);
4726 /* Construct a piecewise quasipolynomial that is constant on the given
4727 * domain. In particular, it is
4730 * infinity if cst == -1
4732 * If cst == -1, then explicitly check whether the domain is empty and,
4733 * if so, return 0 instead.
4735 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4736 __isl_take isl_basic_set
*bset
, int cst
)
4739 isl_qpolynomial
*qp
;
4741 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4746 bset
= isl_basic_set_params(bset
);
4747 dim
= isl_basic_set_get_space(bset
);
4749 qp
= isl_qpolynomial_infty_on_domain(dim
);
4751 qp
= isl_qpolynomial_zero_on_domain(dim
);
4753 qp
= isl_qpolynomial_one_on_domain(dim
);
4754 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4757 /* Factor bset, call fn on each of the factors and return the product.
4759 * If no factors can be found, simply call fn on the input.
4760 * Otherwise, construct the factors based on the factorizer,
4761 * call fn on each factor and compute the product.
4763 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4764 __isl_take isl_basic_set
*bset
,
4765 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4771 isl_qpolynomial
*qp
;
4772 isl_pw_qpolynomial
*pwqp
;
4776 f
= isl_basic_set_factorizer(bset
);
4779 if (f
->n_group
== 0) {
4780 isl_factorizer_free(f
);
4784 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4785 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4786 if (nparam
< 0 || nvar
< 0)
4787 bset
= isl_basic_set_free(bset
);
4789 space
= isl_basic_set_get_space(bset
);
4790 space
= isl_space_params(space
);
4791 set
= isl_set_universe(isl_space_copy(space
));
4792 qp
= isl_qpolynomial_one_on_domain(space
);
4793 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4795 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4797 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4798 isl_basic_set
*bset_i
;
4799 isl_pw_qpolynomial
*pwqp_i
;
4801 bset_i
= isl_basic_set_copy(bset
);
4802 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4803 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4804 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4806 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4807 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4808 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4810 pwqp_i
= fn(bset_i
);
4811 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4816 isl_basic_set_free(bset
);
4817 isl_factorizer_free(f
);
4821 isl_basic_set_free(bset
);
4825 /* Factor bset, call fn on each of the factors and return the product.
4826 * The function is assumed to evaluate to zero on empty domains,
4827 * to one on zero-dimensional domains and to infinity on unbounded domains
4828 * and will not be called explicitly on zero-dimensional or unbounded domains.
4830 * We first check for some special cases and remove all equalities.
4831 * Then we hand over control to compressed_multiplicative_call.
4833 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4834 __isl_take isl_basic_set
*bset
,
4835 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4840 isl_pw_qpolynomial
*pwqp
;
4845 if (isl_basic_set_plain_is_empty(bset
))
4846 return constant_on_domain(bset
, 0);
4848 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
4852 return constant_on_domain(bset
, 1);
4854 bounded
= isl_basic_set_is_bounded(bset
);
4858 return constant_on_domain(bset
, -1);
4860 if (bset
->n_eq
== 0)
4861 return compressed_multiplicative_call(bset
, fn
);
4863 morph
= isl_basic_set_full_compression(bset
);
4864 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4866 pwqp
= compressed_multiplicative_call(bset
, fn
);
4868 morph
= isl_morph_dom_params(morph
);
4869 morph
= isl_morph_ran_params(morph
);
4870 morph
= isl_morph_inverse(morph
);
4872 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4876 isl_basic_set_free(bset
);
4880 /* Drop all floors in "qp", turning each integer division [a/m] into
4881 * a rational division a/m. If "down" is set, then the integer division
4882 * is replaced by (a-(m-1))/m instead.
4884 static __isl_give isl_qpolynomial
*qp_drop_floors(
4885 __isl_take isl_qpolynomial
*qp
, int down
)
4892 if (qp
->div
->n_row
== 0)
4895 qp
= isl_qpolynomial_cow(qp
);
4899 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4901 isl_int_sub(qp
->div
->row
[i
][1],
4902 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4903 isl_int_add_ui(qp
->div
->row
[i
][1],
4904 qp
->div
->row
[i
][1], 1);
4906 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4907 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4908 qp
= substitute_div(qp
, i
, s
);
4916 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4917 * a rational division a/m.
4919 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4920 __isl_take isl_pw_qpolynomial
*pwqp
)
4927 if (isl_pw_qpolynomial_is_zero(pwqp
))
4930 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4934 for (i
= 0; i
< pwqp
->n
; ++i
) {
4935 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4942 isl_pw_qpolynomial_free(pwqp
);
4946 /* Adjust all the integer divisions in "qp" such that they are at least
4947 * one over the given orthant (identified by "signs"). This ensures
4948 * that they will still be non-negative even after subtracting (m-1)/m.
4950 * In particular, f is replaced by f' + v, changing f = [a/m]
4951 * to f' = [(a - m v)/m].
4952 * If the constant term k in a is smaller than m,
4953 * the constant term of v is set to floor(k/m) - 1.
4954 * For any other term, if the coefficient c and the variable x have
4955 * the same sign, then no changes are needed.
4956 * Otherwise, if the variable is positive (and c is negative),
4957 * then the coefficient of x in v is set to floor(c/m).
4958 * If the variable is negative (and c is positive),
4959 * then the coefficient of x in v is set to ceil(c/m).
4961 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4969 qp
= isl_qpolynomial_cow(qp
);
4970 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4972 return isl_qpolynomial_free(qp
);
4973 qp
->div
= isl_mat_cow(qp
->div
);
4977 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4979 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4980 isl_int
*row
= qp
->div
->row
[i
];
4984 if (isl_int_lt(row
[1], row
[0])) {
4985 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4986 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4987 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4989 for (j
= 0; j
< div_pos
; ++j
) {
4990 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4993 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4995 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4996 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4998 for (j
= 0; j
< i
; ++j
) {
4999 if (isl_int_sgn(row
[2 + div_pos
+ j
]) >= 0)
5001 isl_int_fdiv_q(v
->el
[1 + div_pos
+ j
],
5002 row
[2 + div_pos
+ j
], row
[0]);
5003 isl_int_submul(row
[2 + div_pos
+ j
],
5004 row
[0], v
->el
[1 + div_pos
+ j
]);
5006 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
5007 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
5009 isl_seq_combine(qp
->div
->row
[j
] + 1,
5010 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
5011 qp
->div
->row
[j
][2 + div_pos
+ i
], v
->el
,
5014 isl_int_set_si(v
->el
[1 + div_pos
+ i
], 1);
5015 s
= isl_poly_from_affine(qp
->dim
->ctx
, v
->el
,
5016 qp
->div
->ctx
->one
, v
->size
);
5017 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ i
, 1, &s
);
5027 isl_qpolynomial_free(qp
);
5031 struct isl_to_poly_data
{
5033 isl_pw_qpolynomial
*res
;
5034 isl_qpolynomial
*qp
;
5037 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5038 * We first make all integer divisions positive and then split the
5039 * quasipolynomials into terms with sign data->sign (the direction
5040 * of the requested approximation) and terms with the opposite sign.
5041 * In the first set of terms, each integer division [a/m] is
5042 * overapproximated by a/m, while in the second it is underapproximated
5045 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
5046 int *signs
, void *user
)
5048 struct isl_to_poly_data
*data
= user
;
5049 isl_pw_qpolynomial
*t
;
5050 isl_qpolynomial
*qp
, *up
, *down
;
5052 qp
= isl_qpolynomial_copy(data
->qp
);
5053 qp
= make_divs_pos(qp
, signs
);
5055 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
5056 up
= qp_drop_floors(up
, 0);
5057 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
5058 down
= qp_drop_floors(down
, 1);
5060 isl_qpolynomial_free(qp
);
5061 qp
= isl_qpolynomial_add(up
, down
);
5063 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
5064 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
5069 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5070 * the polynomial will be an overapproximation. If "sign" is negative,
5071 * it will be an underapproximation. If "sign" is zero, the approximation
5072 * will lie somewhere in between.
5074 * In particular, is sign == 0, we simply drop the floors, turning
5075 * the integer divisions into rational divisions.
5076 * Otherwise, we split the domains into orthants, make all integer divisions
5077 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5078 * depending on the requested sign and the sign of the term in which
5079 * the integer division appears.
5081 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
5082 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
5085 struct isl_to_poly_data data
;
5088 return pwqp_drop_floors(pwqp
);
5094 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
5096 for (i
= 0; i
< pwqp
->n
; ++i
) {
5097 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
5098 isl_pw_qpolynomial
*t
;
5099 t
= isl_pw_qpolynomial_alloc(
5100 isl_set_copy(pwqp
->p
[i
].set
),
5101 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
5102 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
5105 data
.qp
= pwqp
->p
[i
].qp
;
5106 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
5107 &to_polynomial_on_orthant
, &data
) < 0)
5111 isl_pw_qpolynomial_free(pwqp
);
5115 isl_pw_qpolynomial_free(pwqp
);
5116 isl_pw_qpolynomial_free(data
.res
);
5120 static __isl_give isl_pw_qpolynomial
*poly_entry(
5121 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
5125 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
5128 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
5129 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
5131 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
5132 &poly_entry
, &sign
);
5135 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
5136 __isl_take isl_qpolynomial
*qp
)
5140 isl_vec
*aff
= NULL
;
5141 isl_basic_map
*bmap
= NULL
;
5148 is_affine
= isl_poly_is_affine(qp
->poly
);
5152 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
5153 "input quasi-polynomial not affine", goto error
);
5154 aff
= isl_qpolynomial_extract_affine(qp
);
5157 dim
= isl_qpolynomial_get_space(qp
);
5158 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
5159 n_div
= qp
->div
->n_row
;
5160 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
5162 for (i
= 0; i
< n_div
; ++i
) {
5163 k
= isl_basic_map_alloc_div(bmap
);
5166 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
5167 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
5168 bmap
= isl_basic_map_add_div_constraints(bmap
, k
);
5170 k
= isl_basic_map_alloc_equality(bmap
);
5173 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5174 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5175 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5178 isl_qpolynomial_free(qp
);
5179 bmap
= isl_basic_map_finalize(bmap
);
5183 isl_qpolynomial_free(qp
);
5184 isl_basic_map_free(bmap
);