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 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
441 __isl_keep isl_qpolynomial
*qp
)
443 return qp
? isl_space_copy(qp
->dim
) : NULL
;
446 /* Return a copy of the local space on which "qp" is defined.
448 static __isl_give isl_local_space
*isl_qpolynomial_get_domain_local_space(
449 __isl_keep isl_qpolynomial
*qp
)
456 space
= isl_qpolynomial_get_domain_space(qp
);
457 return isl_local_space_alloc_div(space
, isl_mat_copy(qp
->div
));
460 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
465 space
= isl_space_copy(qp
->dim
);
466 space
= isl_space_from_domain(space
);
467 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
471 /* Return the number of variables of the given type in the domain of "qp".
473 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
474 enum isl_dim_type type
)
478 if (type
== isl_dim_div
)
479 return qp
->div
->n_row
;
480 if (type
== isl_dim_all
)
481 return isl_space_dim(qp
->dim
, isl_dim_all
) +
482 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
483 return isl_space_dim(qp
->dim
, type
);
486 /* Given the type of a dimension of an isl_qpolynomial,
487 * return the type of the corresponding dimension in its domain.
488 * This function is only called for "type" equal to isl_dim_in or
491 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
493 return type
== isl_dim_in
? isl_dim_set
: type
;
496 /* Externally, an isl_qpolynomial has a map space, but internally, the
497 * ls field corresponds to the domain of that space.
499 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
500 enum isl_dim_type type
)
504 if (type
== isl_dim_out
)
506 type
= domain_type(type
);
507 return isl_qpolynomial_domain_dim(qp
, type
);
510 /* Return the offset of the first coefficient of type "type" in
511 * the domain of "qp".
513 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
514 enum isl_dim_type type
)
523 return 1 + isl_space_offset(qp
->dim
, type
);
525 return 1 + isl_space_dim(qp
->dim
, isl_dim_all
);
531 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
533 return qp
? isl_poly_is_zero(qp
->poly
) : isl_bool_error
;
536 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
538 return qp
? isl_poly_is_one(qp
->poly
) : isl_bool_error
;
541 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
543 return qp
? isl_poly_is_nan(qp
->poly
) : isl_bool_error
;
546 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
548 return qp
? isl_poly_is_infty(qp
->poly
) : isl_bool_error
;
551 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
553 return qp
? isl_poly_is_neginfty(qp
->poly
) : isl_bool_error
;
556 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
558 return qp
? isl_poly_sgn(qp
->poly
) : 0;
561 static void poly_free_cst(__isl_take isl_poly_cst
*cst
)
563 isl_int_clear(cst
->n
);
564 isl_int_clear(cst
->d
);
567 static void poly_free_rec(__isl_take isl_poly_rec
*rec
)
571 for (i
= 0; i
< rec
->n
; ++i
)
572 isl_poly_free(rec
->p
[i
]);
575 __isl_give isl_poly
*isl_poly_copy(__isl_keep isl_poly
*poly
)
584 __isl_give isl_poly
*isl_poly_dup_cst(__isl_keep isl_poly
*poly
)
589 cst
= isl_poly_as_cst(poly
);
593 dup
= isl_poly_as_cst(isl_poly_zero(poly
->ctx
));
596 isl_int_set(dup
->n
, cst
->n
);
597 isl_int_set(dup
->d
, cst
->d
);
602 __isl_give isl_poly
*isl_poly_dup_rec(__isl_keep isl_poly
*poly
)
608 rec
= isl_poly_as_rec(poly
);
612 dup
= isl_poly_alloc_rec(poly
->ctx
, poly
->var
, rec
->n
);
616 for (i
= 0; i
< rec
->n
; ++i
) {
617 dup
->p
[i
] = isl_poly_copy(rec
->p
[i
]);
625 isl_poly_free(&dup
->poly
);
629 __isl_give isl_poly
*isl_poly_dup(__isl_keep isl_poly
*poly
)
633 is_cst
= isl_poly_is_cst(poly
);
637 return isl_poly_dup_cst(poly
);
639 return isl_poly_dup_rec(poly
);
642 __isl_give isl_poly
*isl_poly_cow(__isl_take isl_poly
*poly
)
650 return isl_poly_dup(poly
);
653 __isl_null isl_poly
*isl_poly_free(__isl_take isl_poly
*poly
)
662 poly_free_cst((isl_poly_cst
*) poly
);
664 poly_free_rec((isl_poly_rec
*) poly
);
666 isl_ctx_deref(poly
->ctx
);
671 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst
*cst
)
676 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
677 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
678 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
679 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
684 __isl_give isl_poly
*isl_poly_sum_cst(__isl_take isl_poly
*poly1
,
685 __isl_take isl_poly
*poly2
)
690 poly1
= isl_poly_cow(poly1
);
691 if (!poly1
|| !poly2
)
694 cst1
= isl_poly_as_cst(poly1
);
695 cst2
= isl_poly_as_cst(poly2
);
697 if (isl_int_eq(cst1
->d
, cst2
->d
))
698 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
700 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
701 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
702 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
705 isl_poly_cst_reduce(cst1
);
707 isl_poly_free(poly2
);
710 isl_poly_free(poly1
);
711 isl_poly_free(poly2
);
715 static __isl_give isl_poly
*replace_by_zero(__isl_take isl_poly
*poly
)
723 return isl_poly_zero(ctx
);
726 static __isl_give isl_poly
*replace_by_constant_term(__isl_take isl_poly
*poly
)
734 rec
= isl_poly_as_rec(poly
);
737 cst
= isl_poly_copy(rec
->p
[0]);
745 __isl_give isl_poly
*isl_poly_sum(__isl_take isl_poly
*poly1
,
746 __isl_take isl_poly
*poly2
)
749 isl_bool is_zero
, is_nan
, is_cst
;
750 isl_poly_rec
*rec1
, *rec2
;
752 if (!poly1
|| !poly2
)
755 is_nan
= isl_poly_is_nan(poly1
);
759 isl_poly_free(poly2
);
763 is_nan
= isl_poly_is_nan(poly2
);
767 isl_poly_free(poly1
);
771 is_zero
= isl_poly_is_zero(poly1
);
775 isl_poly_free(poly1
);
779 is_zero
= isl_poly_is_zero(poly2
);
783 isl_poly_free(poly2
);
787 if (poly1
->var
< poly2
->var
)
788 return isl_poly_sum(poly2
, poly1
);
790 if (poly2
->var
< poly1
->var
) {
794 is_infty
= isl_poly_is_infty(poly2
);
795 if (is_infty
>= 0 && !is_infty
)
796 is_infty
= isl_poly_is_neginfty(poly2
);
800 isl_poly_free(poly1
);
803 poly1
= isl_poly_cow(poly1
);
804 rec
= isl_poly_as_rec(poly1
);
807 rec
->p
[0] = isl_poly_sum(rec
->p
[0], poly2
);
809 poly1
= replace_by_constant_term(poly1
);
813 is_cst
= isl_poly_is_cst(poly1
);
817 return isl_poly_sum_cst(poly1
, poly2
);
819 rec1
= isl_poly_as_rec(poly1
);
820 rec2
= isl_poly_as_rec(poly2
);
824 if (rec1
->n
< rec2
->n
)
825 return isl_poly_sum(poly2
, poly1
);
827 poly1
= isl_poly_cow(poly1
);
828 rec1
= isl_poly_as_rec(poly1
);
832 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
835 rec1
->p
[i
] = isl_poly_sum(rec1
->p
[i
],
836 isl_poly_copy(rec2
->p
[i
]));
839 if (i
!= rec1
->n
- 1)
841 is_zero
= isl_poly_is_zero(rec1
->p
[i
]);
845 isl_poly_free(rec1
->p
[i
]);
851 poly1
= replace_by_zero(poly1
);
852 else if (rec1
->n
== 1)
853 poly1
= replace_by_constant_term(poly1
);
855 isl_poly_free(poly2
);
859 isl_poly_free(poly1
);
860 isl_poly_free(poly2
);
864 __isl_give isl_poly
*isl_poly_cst_add_isl_int(__isl_take isl_poly
*poly
,
869 poly
= isl_poly_cow(poly
);
873 cst
= isl_poly_as_cst(poly
);
875 isl_int_addmul(cst
->n
, cst
->d
, v
);
880 __isl_give isl_poly
*isl_poly_add_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
885 is_cst
= isl_poly_is_cst(poly
);
887 return isl_poly_free(poly
);
889 return isl_poly_cst_add_isl_int(poly
, v
);
891 poly
= isl_poly_cow(poly
);
892 rec
= isl_poly_as_rec(poly
);
896 rec
->p
[0] = isl_poly_add_isl_int(rec
->p
[0], v
);
906 __isl_give isl_poly
*isl_poly_cst_mul_isl_int(__isl_take isl_poly
*poly
,
912 is_zero
= isl_poly_is_zero(poly
);
914 return isl_poly_free(poly
);
918 poly
= isl_poly_cow(poly
);
922 cst
= isl_poly_as_cst(poly
);
924 isl_int_mul(cst
->n
, cst
->n
, v
);
929 __isl_give isl_poly
*isl_poly_mul_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
935 is_cst
= isl_poly_is_cst(poly
);
937 return isl_poly_free(poly
);
939 return isl_poly_cst_mul_isl_int(poly
, v
);
941 poly
= isl_poly_cow(poly
);
942 rec
= isl_poly_as_rec(poly
);
946 for (i
= 0; i
< rec
->n
; ++i
) {
947 rec
->p
[i
] = isl_poly_mul_isl_int(rec
->p
[i
], v
);
958 /* Multiply the constant polynomial "poly" by "v".
960 static __isl_give isl_poly
*isl_poly_cst_scale_val(__isl_take isl_poly
*poly
,
961 __isl_keep isl_val
*v
)
966 is_zero
= isl_poly_is_zero(poly
);
968 return isl_poly_free(poly
);
972 poly
= isl_poly_cow(poly
);
976 cst
= isl_poly_as_cst(poly
);
978 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
979 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
980 isl_poly_cst_reduce(cst
);
985 /* Multiply the polynomial "poly" by "v".
987 static __isl_give isl_poly
*isl_poly_scale_val(__isl_take isl_poly
*poly
,
988 __isl_keep isl_val
*v
)
994 is_cst
= isl_poly_is_cst(poly
);
996 return isl_poly_free(poly
);
998 return isl_poly_cst_scale_val(poly
, v
);
1000 poly
= isl_poly_cow(poly
);
1001 rec
= isl_poly_as_rec(poly
);
1005 for (i
= 0; i
< rec
->n
; ++i
) {
1006 rec
->p
[i
] = isl_poly_scale_val(rec
->p
[i
], v
);
1013 isl_poly_free(poly
);
1017 __isl_give isl_poly
*isl_poly_mul_cst(__isl_take isl_poly
*poly1
,
1018 __isl_take isl_poly
*poly2
)
1023 poly1
= isl_poly_cow(poly1
);
1024 if (!poly1
|| !poly2
)
1027 cst1
= isl_poly_as_cst(poly1
);
1028 cst2
= isl_poly_as_cst(poly2
);
1030 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
1031 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
1033 isl_poly_cst_reduce(cst1
);
1035 isl_poly_free(poly2
);
1038 isl_poly_free(poly1
);
1039 isl_poly_free(poly2
);
1043 __isl_give isl_poly
*isl_poly_mul_rec(__isl_take isl_poly
*poly1
,
1044 __isl_take isl_poly
*poly2
)
1048 isl_poly_rec
*res
= NULL
;
1052 rec1
= isl_poly_as_rec(poly1
);
1053 rec2
= isl_poly_as_rec(poly2
);
1056 size
= rec1
->n
+ rec2
->n
- 1;
1057 res
= isl_poly_alloc_rec(poly1
->ctx
, poly1
->var
, size
);
1061 for (i
= 0; i
< rec1
->n
; ++i
) {
1062 res
->p
[i
] = isl_poly_mul(isl_poly_copy(rec2
->p
[0]),
1063 isl_poly_copy(rec1
->p
[i
]));
1068 for (; i
< size
; ++i
) {
1069 res
->p
[i
] = isl_poly_zero(poly1
->ctx
);
1074 for (i
= 0; i
< rec1
->n
; ++i
) {
1075 for (j
= 1; j
< rec2
->n
; ++j
) {
1077 poly
= isl_poly_mul(isl_poly_copy(rec2
->p
[j
]),
1078 isl_poly_copy(rec1
->p
[i
]));
1079 res
->p
[i
+ j
] = isl_poly_sum(res
->p
[i
+ j
], poly
);
1085 isl_poly_free(poly1
);
1086 isl_poly_free(poly2
);
1090 isl_poly_free(poly1
);
1091 isl_poly_free(poly2
);
1092 isl_poly_free(&res
->poly
);
1096 __isl_give isl_poly
*isl_poly_mul(__isl_take isl_poly
*poly1
,
1097 __isl_take isl_poly
*poly2
)
1099 isl_bool is_zero
, is_nan
, is_one
, is_cst
;
1101 if (!poly1
|| !poly2
)
1104 is_nan
= isl_poly_is_nan(poly1
);
1108 isl_poly_free(poly2
);
1112 is_nan
= isl_poly_is_nan(poly2
);
1116 isl_poly_free(poly1
);
1120 is_zero
= isl_poly_is_zero(poly1
);
1124 isl_poly_free(poly2
);
1128 is_zero
= isl_poly_is_zero(poly2
);
1132 isl_poly_free(poly1
);
1136 is_one
= isl_poly_is_one(poly1
);
1140 isl_poly_free(poly1
);
1144 is_one
= isl_poly_is_one(poly2
);
1148 isl_poly_free(poly2
);
1152 if (poly1
->var
< poly2
->var
)
1153 return isl_poly_mul(poly2
, poly1
);
1155 if (poly2
->var
< poly1
->var
) {
1160 is_infty
= isl_poly_is_infty(poly2
);
1161 if (is_infty
>= 0 && !is_infty
)
1162 is_infty
= isl_poly_is_neginfty(poly2
);
1166 isl_ctx
*ctx
= poly1
->ctx
;
1167 isl_poly_free(poly1
);
1168 isl_poly_free(poly2
);
1169 return isl_poly_nan(ctx
);
1171 poly1
= isl_poly_cow(poly1
);
1172 rec
= isl_poly_as_rec(poly1
);
1176 for (i
= 0; i
< rec
->n
; ++i
) {
1177 rec
->p
[i
] = isl_poly_mul(rec
->p
[i
],
1178 isl_poly_copy(poly2
));
1182 isl_poly_free(poly2
);
1186 is_cst
= isl_poly_is_cst(poly1
);
1190 return isl_poly_mul_cst(poly1
, poly2
);
1192 return isl_poly_mul_rec(poly1
, poly2
);
1194 isl_poly_free(poly1
);
1195 isl_poly_free(poly2
);
1199 __isl_give isl_poly
*isl_poly_pow(__isl_take isl_poly
*poly
, unsigned power
)
1209 res
= isl_poly_copy(poly
);
1211 res
= isl_poly_one(poly
->ctx
);
1213 while (power
>>= 1) {
1214 poly
= isl_poly_mul(poly
, isl_poly_copy(poly
));
1216 res
= isl_poly_mul(res
, isl_poly_copy(poly
));
1219 isl_poly_free(poly
);
1223 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1224 unsigned n_div
, __isl_take isl_poly
*poly
)
1226 struct isl_qpolynomial
*qp
= NULL
;
1229 if (!space
|| !poly
)
1232 if (!isl_space_is_set(space
))
1233 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1234 "domain of polynomial should be a set", goto error
);
1236 total
= isl_space_dim(space
, isl_dim_all
);
1238 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1243 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1252 isl_space_free(space
);
1253 isl_poly_free(poly
);
1254 isl_qpolynomial_free(qp
);
1258 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1267 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1269 struct isl_qpolynomial
*dup
;
1274 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1275 isl_poly_copy(qp
->poly
));
1278 isl_mat_free(dup
->div
);
1279 dup
->div
= isl_mat_copy(qp
->div
);
1285 isl_qpolynomial_free(dup
);
1289 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1297 return isl_qpolynomial_dup(qp
);
1300 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1301 __isl_take isl_qpolynomial
*qp
)
1309 isl_space_free(qp
->dim
);
1310 isl_mat_free(qp
->div
);
1311 isl_poly_free(qp
->poly
);
1317 __isl_give isl_poly
*isl_poly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1323 rec
= isl_poly_alloc_rec(ctx
, pos
, 1 + power
);
1326 for (i
= 0; i
< 1 + power
; ++i
) {
1327 rec
->p
[i
] = isl_poly_zero(ctx
);
1332 cst
= isl_poly_as_cst(rec
->p
[power
]);
1333 isl_int_set_si(cst
->n
, 1);
1337 isl_poly_free(&rec
->poly
);
1341 /* r array maps original positions to new positions.
1343 static __isl_give isl_poly
*reorder(__isl_take isl_poly
*poly
, int *r
)
1351 is_cst
= isl_poly_is_cst(poly
);
1353 return isl_poly_free(poly
);
1357 rec
= isl_poly_as_rec(poly
);
1361 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
1363 base
= isl_poly_var_pow(poly
->ctx
, r
[poly
->var
], 1);
1364 res
= reorder(isl_poly_copy(rec
->p
[rec
->n
- 1]), r
);
1366 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1367 res
= isl_poly_mul(res
, isl_poly_copy(base
));
1368 res
= isl_poly_sum(res
, reorder(isl_poly_copy(rec
->p
[i
]), r
));
1371 isl_poly_free(base
);
1372 isl_poly_free(poly
);
1376 isl_poly_free(poly
);
1380 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1381 __isl_keep isl_mat
*div2
)
1386 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1387 div1
->n_col
>= div2
->n_col
,
1388 return isl_bool_error
);
1390 if (div1
->n_row
== div2
->n_row
)
1391 return isl_mat_is_equal(div1
, div2
);
1393 n_row
= div1
->n_row
;
1394 n_col
= div1
->n_col
;
1395 div1
->n_row
= div2
->n_row
;
1396 div1
->n_col
= div2
->n_col
;
1398 equal
= isl_mat_is_equal(div1
, div2
);
1400 div1
->n_row
= n_row
;
1401 div1
->n_col
= n_col
;
1406 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1410 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1411 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1416 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1419 struct isl_div_sort_info
{
1424 static int div_sort_cmp(const void *p1
, const void *p2
)
1426 const struct isl_div_sort_info
*i1
, *i2
;
1427 i1
= (const struct isl_div_sort_info
*) p1
;
1428 i2
= (const struct isl_div_sort_info
*) p2
;
1430 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1433 /* Sort divs and remove duplicates.
1435 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1440 struct isl_div_sort_info
*array
= NULL
;
1441 int *pos
= NULL
, *at
= NULL
;
1442 int *reordering
= NULL
;
1447 if (qp
->div
->n_row
<= 1)
1450 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1452 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1454 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1455 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1456 len
= qp
->div
->n_col
- 2;
1457 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1458 if (!array
|| !pos
|| !at
|| !reordering
)
1461 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1462 array
[i
].div
= qp
->div
;
1468 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1471 for (i
= 0; i
< div_pos
; ++i
)
1474 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1475 if (pos
[array
[i
].row
] == i
)
1477 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1478 pos
[at
[i
]] = pos
[array
[i
].row
];
1479 at
[pos
[array
[i
].row
]] = at
[i
];
1480 at
[i
] = array
[i
].row
;
1481 pos
[array
[i
].row
] = i
;
1485 for (i
= 0; i
< len
- div_pos
; ++i
) {
1487 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1488 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1489 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1490 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1491 2 + div_pos
+ i
- skip
);
1492 qp
->div
= isl_mat_drop_cols(qp
->div
,
1493 2 + div_pos
+ i
- skip
, 1);
1496 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1499 qp
->poly
= reorder(qp
->poly
, reordering
);
1501 if (!qp
->poly
|| !qp
->div
)
1515 isl_qpolynomial_free(qp
);
1519 static __isl_give isl_poly
*expand(__isl_take isl_poly
*poly
, int *exp
,
1526 is_cst
= isl_poly_is_cst(poly
);
1528 return isl_poly_free(poly
);
1532 if (poly
->var
< first
)
1535 if (exp
[poly
->var
- first
] == poly
->var
- first
)
1538 poly
= isl_poly_cow(poly
);
1542 poly
->var
= exp
[poly
->var
- first
] + first
;
1544 rec
= isl_poly_as_rec(poly
);
1548 for (i
= 0; i
< rec
->n
; ++i
) {
1549 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1556 isl_poly_free(poly
);
1560 static __isl_give isl_qpolynomial
*with_merged_divs(
1561 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1562 __isl_take isl_qpolynomial
*qp2
),
1563 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1567 isl_mat
*div
= NULL
;
1570 qp1
= isl_qpolynomial_cow(qp1
);
1571 qp2
= isl_qpolynomial_cow(qp2
);
1576 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1577 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1579 n_div1
= qp1
->div
->n_row
;
1580 n_div2
= qp2
->div
->n_row
;
1581 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1582 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1583 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1586 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1590 isl_mat_free(qp1
->div
);
1591 qp1
->div
= isl_mat_copy(div
);
1592 isl_mat_free(qp2
->div
);
1593 qp2
->div
= isl_mat_copy(div
);
1595 qp1
->poly
= expand(qp1
->poly
, exp1
, div
->n_col
- div
->n_row
- 2);
1596 qp2
->poly
= expand(qp2
->poly
, exp2
, div
->n_col
- div
->n_row
- 2);
1598 if (!qp1
->poly
|| !qp2
->poly
)
1605 return fn(qp1
, qp2
);
1610 isl_qpolynomial_free(qp1
);
1611 isl_qpolynomial_free(qp2
);
1615 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1616 __isl_take isl_qpolynomial
*qp2
)
1618 isl_bool compatible
;
1620 qp1
= isl_qpolynomial_cow(qp1
);
1625 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1626 return isl_qpolynomial_add(qp2
, qp1
);
1628 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1629 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1633 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1635 qp1
->poly
= isl_poly_sum(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1639 isl_qpolynomial_free(qp2
);
1643 isl_qpolynomial_free(qp1
);
1644 isl_qpolynomial_free(qp2
);
1648 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1649 __isl_keep isl_set
*dom
,
1650 __isl_take isl_qpolynomial
*qp1
,
1651 __isl_take isl_qpolynomial
*qp2
)
1653 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1654 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1658 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1659 __isl_take isl_qpolynomial
*qp2
)
1661 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1664 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1665 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1667 if (isl_int_is_zero(v
))
1670 qp
= isl_qpolynomial_cow(qp
);
1674 qp
->poly
= isl_poly_add_isl_int(qp
->poly
, v
);
1680 isl_qpolynomial_free(qp
);
1685 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1690 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1693 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1694 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1696 if (isl_int_is_one(v
))
1699 if (qp
&& isl_int_is_zero(v
)) {
1700 isl_qpolynomial
*zero
;
1701 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1702 isl_qpolynomial_free(qp
);
1706 qp
= isl_qpolynomial_cow(qp
);
1710 qp
->poly
= isl_poly_mul_isl_int(qp
->poly
, v
);
1716 isl_qpolynomial_free(qp
);
1720 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1721 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1723 return isl_qpolynomial_mul_isl_int(qp
, v
);
1726 /* Multiply "qp" by "v".
1728 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1729 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1734 if (!isl_val_is_rat(v
))
1735 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1736 "expecting rational factor", goto error
);
1738 if (isl_val_is_one(v
)) {
1743 if (isl_val_is_zero(v
)) {
1746 space
= isl_qpolynomial_get_domain_space(qp
);
1747 isl_qpolynomial_free(qp
);
1749 return isl_qpolynomial_zero_on_domain(space
);
1752 qp
= isl_qpolynomial_cow(qp
);
1756 qp
->poly
= isl_poly_scale_val(qp
->poly
, v
);
1758 qp
= isl_qpolynomial_free(qp
);
1764 isl_qpolynomial_free(qp
);
1768 /* Divide "qp" by "v".
1770 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1771 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1776 if (!isl_val_is_rat(v
))
1777 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1778 "expecting rational factor", goto error
);
1779 if (isl_val_is_zero(v
))
1780 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1781 "cannot scale down by zero", goto error
);
1783 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1786 isl_qpolynomial_free(qp
);
1790 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1791 __isl_take isl_qpolynomial
*qp2
)
1793 isl_bool compatible
;
1795 qp1
= isl_qpolynomial_cow(qp1
);
1800 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1801 return isl_qpolynomial_mul(qp2
, qp1
);
1803 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1804 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1808 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1810 qp1
->poly
= isl_poly_mul(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1814 isl_qpolynomial_free(qp2
);
1818 isl_qpolynomial_free(qp1
);
1819 isl_qpolynomial_free(qp2
);
1823 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1826 qp
= isl_qpolynomial_cow(qp
);
1831 qp
->poly
= isl_poly_pow(qp
->poly
, power
);
1837 isl_qpolynomial_free(qp
);
1841 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1842 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1849 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1853 for (i
= 0; i
< pwqp
->n
; ++i
) {
1854 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1856 return isl_pw_qpolynomial_free(pwqp
);
1862 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1863 __isl_take isl_space
*domain
)
1867 return isl_qpolynomial_alloc(domain
, 0, isl_poly_zero(domain
->ctx
));
1870 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1871 __isl_take isl_space
*domain
)
1875 return isl_qpolynomial_alloc(domain
, 0, isl_poly_one(domain
->ctx
));
1878 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1879 __isl_take isl_space
*domain
)
1883 return isl_qpolynomial_alloc(domain
, 0, isl_poly_infty(domain
->ctx
));
1886 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1887 __isl_take isl_space
*domain
)
1891 return isl_qpolynomial_alloc(domain
, 0, isl_poly_neginfty(domain
->ctx
));
1894 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1895 __isl_take isl_space
*domain
)
1899 return isl_qpolynomial_alloc(domain
, 0, isl_poly_nan(domain
->ctx
));
1902 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1903 __isl_take isl_space
*domain
,
1906 struct isl_qpolynomial
*qp
;
1909 qp
= isl_qpolynomial_zero_on_domain(domain
);
1913 cst
= isl_poly_as_cst(qp
->poly
);
1914 isl_int_set(cst
->n
, v
);
1919 isl_bool
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1920 isl_int
*n
, isl_int
*d
)
1926 return isl_bool_error
;
1928 is_cst
= isl_poly_is_cst(qp
->poly
);
1929 if (is_cst
< 0 || !is_cst
)
1932 cst
= isl_poly_as_cst(qp
->poly
);
1934 return isl_bool_error
;
1937 isl_int_set(*n
, cst
->n
);
1939 isl_int_set(*d
, cst
->d
);
1941 return isl_bool_true
;
1944 /* Return the constant term of "poly".
1946 static __isl_give isl_val
*isl_poly_get_constant_val(__isl_keep isl_poly
*poly
)
1954 while ((is_cst
= isl_poly_is_cst(poly
)) == isl_bool_false
) {
1957 rec
= isl_poly_as_rec(poly
);
1965 cst
= isl_poly_as_cst(poly
);
1968 return isl_val_rat_from_isl_int(cst
->poly
.ctx
, cst
->n
, cst
->d
);
1971 /* Return the constant term of "qp".
1973 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1974 __isl_keep isl_qpolynomial
*qp
)
1979 return isl_poly_get_constant_val(qp
->poly
);
1982 isl_bool
isl_poly_is_affine(__isl_keep isl_poly
*poly
)
1988 return isl_bool_error
;
1991 return isl_bool_true
;
1993 rec
= isl_poly_as_rec(poly
);
1995 return isl_bool_error
;
1998 return isl_bool_false
;
2000 isl_assert(poly
->ctx
, rec
->n
> 1, return isl_bool_error
);
2002 is_cst
= isl_poly_is_cst(rec
->p
[1]);
2003 if (is_cst
< 0 || !is_cst
)
2006 return isl_poly_is_affine(rec
->p
[0]);
2009 isl_bool
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
2012 return isl_bool_error
;
2014 if (qp
->div
->n_row
> 0)
2015 return isl_bool_false
;
2017 return isl_poly_is_affine(qp
->poly
);
2020 static void update_coeff(__isl_keep isl_vec
*aff
,
2021 __isl_keep isl_poly_cst
*cst
, int pos
)
2026 if (isl_int_is_zero(cst
->n
))
2031 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
2032 isl_int_divexact(f
, cst
->d
, gcd
);
2033 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
2034 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
2035 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
2040 int isl_poly_update_affine(__isl_keep isl_poly
*poly
, __isl_keep isl_vec
*aff
)
2048 if (poly
->var
< 0) {
2051 cst
= isl_poly_as_cst(poly
);
2054 update_coeff(aff
, cst
, 0);
2058 rec
= isl_poly_as_rec(poly
);
2061 isl_assert(poly
->ctx
, rec
->n
== 2, return -1);
2063 cst
= isl_poly_as_cst(rec
->p
[1]);
2066 update_coeff(aff
, cst
, 1 + poly
->var
);
2068 return isl_poly_update_affine(rec
->p
[0], aff
);
2071 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
2072 __isl_keep isl_qpolynomial
*qp
)
2080 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2081 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
2085 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
2086 isl_int_set_si(aff
->el
[0], 1);
2088 if (isl_poly_update_affine(qp
->poly
, aff
) < 0)
2097 /* Compare two quasi-polynomials.
2099 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2100 * than "qp2" and 0 if they are equal.
2102 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2103 __isl_keep isl_qpolynomial
*qp2
)
2114 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2118 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2122 return isl_poly_plain_cmp(qp1
->poly
, qp2
->poly
);
2125 /* Is "qp1" obviously equal to "qp2"?
2127 * NaN is not equal to anything, not even to another NaN.
2129 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2130 __isl_keep isl_qpolynomial
*qp2
)
2135 return isl_bool_error
;
2137 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2138 return isl_bool_false
;
2140 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2141 if (equal
< 0 || !equal
)
2144 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2145 if (equal
< 0 || !equal
)
2148 return isl_poly_is_equal(qp1
->poly
, qp2
->poly
);
2151 static isl_stat
poly_update_den(__isl_keep isl_poly
*poly
, isl_int
*d
)
2157 is_cst
= isl_poly_is_cst(poly
);
2159 return isl_stat_error
;
2162 cst
= isl_poly_as_cst(poly
);
2164 return isl_stat_error
;
2165 isl_int_lcm(*d
, *d
, cst
->d
);
2169 rec
= isl_poly_as_rec(poly
);
2171 return isl_stat_error
;
2173 for (i
= 0; i
< rec
->n
; ++i
)
2174 poly_update_den(rec
->p
[i
], d
);
2179 __isl_give isl_val
*isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
)
2185 d
= isl_val_one(isl_qpolynomial_get_ctx(qp
));
2188 if (poly_update_den(qp
->poly
, &d
->n
) < 0)
2189 return isl_val_free(d
);
2193 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2194 __isl_take isl_space
*domain
, int pos
, int power
)
2196 struct isl_ctx
*ctx
;
2203 return isl_qpolynomial_alloc(domain
, 0,
2204 isl_poly_var_pow(ctx
, pos
, power
));
2207 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2208 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2210 if (isl_space_check_is_set(domain
) < 0)
2212 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2215 if (type
== isl_dim_set
)
2216 pos
+= isl_space_dim(domain
, isl_dim_param
);
2218 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2220 isl_space_free(domain
);
2224 __isl_give isl_poly
*isl_poly_subs(__isl_take isl_poly
*poly
,
2225 unsigned first
, unsigned n
, __isl_keep isl_poly
**subs
)
2230 isl_poly
*base
, *res
;
2232 is_cst
= isl_poly_is_cst(poly
);
2234 return isl_poly_free(poly
);
2238 if (poly
->var
< first
)
2241 rec
= isl_poly_as_rec(poly
);
2245 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
2247 if (poly
->var
>= first
+ n
)
2248 base
= isl_poly_var_pow(poly
->ctx
, poly
->var
, 1);
2250 base
= isl_poly_copy(subs
[poly
->var
- first
]);
2252 res
= isl_poly_subs(isl_poly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2253 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2255 t
= isl_poly_subs(isl_poly_copy(rec
->p
[i
]), first
, n
, subs
);
2256 res
= isl_poly_mul(res
, isl_poly_copy(base
));
2257 res
= isl_poly_sum(res
, t
);
2260 isl_poly_free(base
);
2261 isl_poly_free(poly
);
2265 isl_poly_free(poly
);
2269 __isl_give isl_poly
*isl_poly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2270 isl_int denom
, unsigned len
)
2275 isl_assert(ctx
, len
>= 1, return NULL
);
2277 poly
= isl_poly_rat_cst(ctx
, f
[0], denom
);
2278 for (i
= 0; i
< len
- 1; ++i
) {
2282 if (isl_int_is_zero(f
[1 + i
]))
2285 c
= isl_poly_rat_cst(ctx
, f
[1 + i
], denom
);
2286 t
= isl_poly_var_pow(ctx
, i
, 1);
2287 t
= isl_poly_mul(c
, t
);
2288 poly
= isl_poly_sum(poly
, t
);
2294 /* Remove common factor of non-constant terms and denominator.
2296 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2298 isl_ctx
*ctx
= qp
->div
->ctx
;
2299 unsigned total
= qp
->div
->n_col
- 2;
2301 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2302 isl_int_gcd(ctx
->normalize_gcd
,
2303 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2304 if (isl_int_is_one(ctx
->normalize_gcd
))
2307 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2308 ctx
->normalize_gcd
, total
);
2309 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2310 ctx
->normalize_gcd
);
2311 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2312 ctx
->normalize_gcd
);
2315 /* Replace the integer division identified by "div" by the polynomial "s".
2316 * The integer division is assumed not to appear in the definition
2317 * of any other integer divisions.
2319 static __isl_give isl_qpolynomial
*substitute_div(
2320 __isl_take isl_qpolynomial
*qp
, int div
, __isl_take isl_poly
*s
)
2329 qp
= isl_qpolynomial_cow(qp
);
2333 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2334 qp
->poly
= isl_poly_subs(qp
->poly
, total
+ div
, 1, &s
);
2338 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2341 for (i
= 0; i
< total
+ div
; ++i
)
2343 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2344 reordering
[i
] = i
- 1;
2345 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2346 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2347 qp
->poly
= reorder(qp
->poly
, reordering
);
2350 if (!qp
->poly
|| !qp
->div
)
2356 isl_qpolynomial_free(qp
);
2361 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2362 * divisions because d is equal to 1 by their definition, i.e., e.
2364 static __isl_give isl_qpolynomial
*substitute_non_divs(
2365 __isl_take isl_qpolynomial
*qp
)
2374 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2375 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2376 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2378 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2379 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2381 isl_seq_combine(qp
->div
->row
[j
] + 1,
2382 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2383 qp
->div
->row
[j
][2 + total
+ i
],
2384 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2385 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2386 normalize_div(qp
, j
);
2388 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2389 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2390 qp
= substitute_div(qp
, i
, s
);
2397 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2398 * with d the denominator. When replacing the coefficient e of x by
2399 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2400 * inside the division, so we need to add floor(e/d) * x outside.
2401 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2402 * to adjust the coefficient of x in each later div that depends on the
2403 * current div "div" and also in the affine expressions in the rows of "mat"
2404 * (if they too depend on "div").
2406 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2407 __isl_keep isl_mat
**mat
)
2411 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2414 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2415 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2416 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2418 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2419 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2420 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2421 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2422 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2423 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2425 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2426 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2432 /* Check if the last non-zero coefficient is bigger that half of the
2433 * denominator. If so, we will invert the div to further reduce the number
2434 * of distinct divs that may appear.
2435 * If the last non-zero coefficient is exactly half the denominator,
2436 * then we continue looking for earlier coefficients that are bigger
2437 * than half the denominator.
2439 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2444 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2445 if (isl_int_is_zero(div
->row
[row
][i
]))
2447 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2448 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2449 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2459 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2460 * We only invert the coefficients of e (and the coefficient of q in
2461 * later divs and in the rows of "mat"). After calling this function, the
2462 * coefficients of e should be reduced again.
2464 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2465 __isl_keep isl_mat
**mat
)
2467 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2469 isl_seq_neg(qp
->div
->row
[div
] + 1,
2470 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2471 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2472 isl_int_add(qp
->div
->row
[div
][1],
2473 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2474 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2475 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2476 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2479 /* Reduce all divs of "qp" to have coefficients
2480 * in the interval [0, d-1], with d the denominator and such that the
2481 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2482 * The modifications to the integer divisions need to be reflected
2483 * in the factors of the polynomial that refer to the original
2484 * integer divisions. To this end, the modifications are collected
2485 * as a set of affine expressions and then plugged into the polynomial.
2487 * After the reduction, some divs may have become redundant or identical,
2488 * so we call substitute_non_divs and sort_divs. If these functions
2489 * eliminate divs or merge two or more divs into one, the coefficients
2490 * of the enclosing divs may have to be reduced again, so we call
2491 * ourselves recursively if the number of divs decreases.
2493 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2499 unsigned o_div
, n_div
, total
;
2504 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2505 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2506 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2507 ctx
= isl_qpolynomial_get_ctx(qp
);
2508 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2510 for (i
= 0; i
< n_div
; ++i
)
2511 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2513 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2514 normalize_div(qp
, i
);
2515 reduce_div(qp
, i
, &mat
);
2516 if (needs_invert(qp
->div
, i
)) {
2517 invert_div(qp
, i
, &mat
);
2518 reduce_div(qp
, i
, &mat
);
2524 s
= isl_alloc_array(ctx
, struct isl_poly
*, n_div
);
2527 for (i
= 0; i
< n_div
; ++i
)
2528 s
[i
] = isl_poly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2530 qp
->poly
= isl_poly_subs(qp
->poly
, o_div
- 1, n_div
, s
);
2531 for (i
= 0; i
< n_div
; ++i
)
2532 isl_poly_free(s
[i
]);
2539 qp
= substitute_non_divs(qp
);
2541 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2542 return reduce_divs(qp
);
2546 isl_qpolynomial_free(qp
);
2551 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2552 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2554 struct isl_qpolynomial
*qp
;
2557 qp
= isl_qpolynomial_zero_on_domain(domain
);
2561 cst
= isl_poly_as_cst(qp
->poly
);
2562 isl_int_set(cst
->n
, n
);
2563 isl_int_set(cst
->d
, d
);
2568 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2570 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2571 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2573 isl_qpolynomial
*qp
;
2576 qp
= isl_qpolynomial_zero_on_domain(domain
);
2580 cst
= isl_poly_as_cst(qp
->poly
);
2581 isl_int_set(cst
->n
, val
->n
);
2582 isl_int_set(cst
->d
, val
->d
);
2588 isl_qpolynomial_free(qp
);
2592 static isl_stat
poly_set_active(__isl_keep isl_poly
*poly
, int *active
, int d
)
2598 is_cst
= isl_poly_is_cst(poly
);
2600 return isl_stat_error
;
2605 active
[poly
->var
] = 1;
2607 rec
= isl_poly_as_rec(poly
);
2608 for (i
= 0; i
< rec
->n
; ++i
)
2609 if (poly_set_active(rec
->p
[i
], active
, d
) < 0)
2610 return isl_stat_error
;
2615 static isl_stat
set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2618 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2621 return isl_stat_error
;
2623 for (i
= 0; i
< d
; ++i
)
2624 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2625 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2631 return poly_set_active(qp
->poly
, active
, d
);
2635 #define TYPE isl_qpolynomial
2637 #include "check_type_range_templ.c"
2639 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2640 enum isl_dim_type type
, unsigned first
, unsigned n
)
2644 isl_bool involves
= isl_bool_false
;
2647 return isl_bool_error
;
2649 return isl_bool_false
;
2651 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2652 return isl_bool_error
;
2653 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2654 type
== isl_dim_in
, return isl_bool_error
);
2656 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2657 isl_space_dim(qp
->dim
, isl_dim_all
));
2658 if (set_active(qp
, active
) < 0)
2661 if (type
== isl_dim_in
)
2662 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2663 for (i
= 0; i
< n
; ++i
)
2664 if (active
[first
+ i
]) {
2665 involves
= isl_bool_true
;
2674 return isl_bool_error
;
2677 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2678 * of the divs that do appear in the quasi-polynomial.
2680 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2681 __isl_take isl_qpolynomial
*qp
)
2688 int *reordering
= NULL
;
2695 if (qp
->div
->n_row
== 0)
2698 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2699 len
= qp
->div
->n_col
- 2;
2700 ctx
= isl_qpolynomial_get_ctx(qp
);
2701 active
= isl_calloc_array(ctx
, int, len
);
2705 if (poly_set_active(qp
->poly
, active
, len
) < 0)
2708 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2709 if (!active
[d
+ i
]) {
2713 for (j
= 0; j
< i
; ++j
) {
2714 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2726 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2730 for (i
= 0; i
< d
; ++i
)
2734 n_div
= qp
->div
->n_row
;
2735 for (i
= 0; i
< n_div
; ++i
) {
2736 if (!active
[d
+ i
]) {
2737 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2738 qp
->div
= isl_mat_drop_cols(qp
->div
,
2739 2 + d
+ i
- skip
, 1);
2742 reordering
[d
+ i
] = d
+ i
- skip
;
2745 qp
->poly
= reorder(qp
->poly
, reordering
);
2747 if (!qp
->poly
|| !qp
->div
)
2757 isl_qpolynomial_free(qp
);
2761 __isl_give isl_poly
*isl_poly_drop(__isl_take isl_poly
*poly
,
2762 unsigned first
, unsigned n
)
2769 if (n
== 0 || poly
->var
< 0 || poly
->var
< first
)
2771 if (poly
->var
< first
+ n
) {
2772 poly
= replace_by_constant_term(poly
);
2773 return isl_poly_drop(poly
, first
, n
);
2775 poly
= isl_poly_cow(poly
);
2779 rec
= isl_poly_as_rec(poly
);
2783 for (i
= 0; i
< rec
->n
; ++i
) {
2784 rec
->p
[i
] = isl_poly_drop(rec
->p
[i
], first
, n
);
2791 isl_poly_free(poly
);
2795 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2796 __isl_take isl_qpolynomial
*qp
,
2797 enum isl_dim_type type
, unsigned pos
, const char *s
)
2799 qp
= isl_qpolynomial_cow(qp
);
2802 if (type
== isl_dim_out
)
2803 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2804 "cannot set name of output/set dimension",
2805 return isl_qpolynomial_free(qp
));
2806 type
= domain_type(type
);
2807 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2812 isl_qpolynomial_free(qp
);
2816 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2817 __isl_take isl_qpolynomial
*qp
,
2818 enum isl_dim_type type
, unsigned first
, unsigned n
)
2822 if (type
== isl_dim_out
)
2823 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2824 "cannot drop output/set dimension",
2826 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2827 return isl_qpolynomial_free(qp
);
2828 type
= domain_type(type
);
2829 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2832 qp
= isl_qpolynomial_cow(qp
);
2836 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2837 type
== isl_dim_set
, goto error
);
2839 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2843 if (type
== isl_dim_set
)
2844 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2846 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2850 qp
->poly
= isl_poly_drop(qp
->poly
, first
, n
);
2856 isl_qpolynomial_free(qp
);
2860 /* Project the domain of the quasi-polynomial onto its parameter space.
2861 * The quasi-polynomial may not involve any of the domain dimensions.
2863 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2864 __isl_take isl_qpolynomial
*qp
)
2870 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2871 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2873 return isl_qpolynomial_free(qp
);
2875 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2876 "polynomial involves some of the domain dimensions",
2877 return isl_qpolynomial_free(qp
));
2878 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2879 space
= isl_qpolynomial_get_domain_space(qp
);
2880 space
= isl_space_params(space
);
2881 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2885 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2886 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2896 if (eq
->n_eq
== 0) {
2897 isl_basic_set_free(eq
);
2901 qp
= isl_qpolynomial_cow(qp
);
2904 qp
->div
= isl_mat_cow(qp
->div
);
2908 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2910 isl_int_init(denom
);
2911 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2912 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2913 if (j
< 0 || j
== 0 || j
>= total
)
2916 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2917 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2919 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2920 &qp
->div
->row
[k
][0]);
2921 normalize_div(qp
, k
);
2924 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2925 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2926 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2927 isl_int_set_si(eq
->eq
[i
][j
], 0);
2929 poly
= isl_poly_from_affine(qp
->dim
->ctx
,
2930 eq
->eq
[i
], denom
, total
);
2931 qp
->poly
= isl_poly_subs(qp
->poly
, j
- 1, 1, &poly
);
2932 isl_poly_free(poly
);
2934 isl_int_clear(denom
);
2939 isl_basic_set_free(eq
);
2941 qp
= substitute_non_divs(qp
);
2946 isl_basic_set_free(eq
);
2947 isl_qpolynomial_free(qp
);
2951 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2953 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2954 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2958 if (qp
->div
->n_row
> 0)
2959 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2960 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2962 isl_basic_set_free(eq
);
2963 isl_qpolynomial_free(qp
);
2967 /* Look for equalities among the variables shared by context and qp
2968 * and the integer divisions of qp, if any.
2969 * The equalities are then used to eliminate variables and/or integer
2970 * divisions from qp.
2972 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2973 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2975 isl_local_space
*ls
;
2978 ls
= isl_qpolynomial_get_domain_local_space(qp
);
2979 context
= isl_local_space_lift_set(ls
, context
);
2981 aff
= isl_set_affine_hull(context
);
2982 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2985 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2986 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2988 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2989 isl_set
*dom_context
= isl_set_universe(space
);
2990 dom_context
= isl_set_intersect_params(dom_context
, context
);
2991 return isl_qpolynomial_gist(qp
, dom_context
);
2994 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2995 __isl_take isl_qpolynomial
*qp
)
3001 if (isl_qpolynomial_is_zero(qp
)) {
3002 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
3003 isl_qpolynomial_free(qp
);
3004 return isl_pw_qpolynomial_zero(dim
);
3007 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
3008 return isl_pw_qpolynomial_alloc(dom
, qp
);
3011 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3014 #define PW isl_pw_qpolynomial
3016 #define EL isl_qpolynomial
3018 #define EL_IS_ZERO is_zero
3022 #define IS_ZERO is_zero
3025 #undef DEFAULT_IS_ZERO
3026 #define DEFAULT_IS_ZERO 1
3030 #include <isl_pw_templ.c>
3031 #include <isl_pw_eval.c>
3034 #define BASE pw_qpolynomial
3036 #include <isl_union_single.c>
3037 #include <isl_union_eval.c>
3038 #include <isl_union_neg.c>
3040 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
3048 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
3051 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
3054 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
3055 __isl_take isl_pw_qpolynomial
*pwqp1
,
3056 __isl_take isl_pw_qpolynomial
*pwqp2
)
3058 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
3061 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
3062 __isl_take isl_pw_qpolynomial
*pwqp1
,
3063 __isl_take isl_pw_qpolynomial
*pwqp2
)
3066 struct isl_pw_qpolynomial
*res
;
3068 if (!pwqp1
|| !pwqp2
)
3071 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3074 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3075 isl_pw_qpolynomial_free(pwqp2
);
3079 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3080 isl_pw_qpolynomial_free(pwqp1
);
3084 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3085 isl_pw_qpolynomial_free(pwqp1
);
3089 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3090 isl_pw_qpolynomial_free(pwqp2
);
3094 n
= pwqp1
->n
* pwqp2
->n
;
3095 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3097 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3098 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3099 struct isl_set
*common
;
3100 struct isl_qpolynomial
*prod
;
3101 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3102 isl_set_copy(pwqp2
->p
[j
].set
));
3103 if (isl_set_plain_is_empty(common
)) {
3104 isl_set_free(common
);
3108 prod
= isl_qpolynomial_mul(
3109 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3110 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3112 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3116 isl_pw_qpolynomial_free(pwqp1
);
3117 isl_pw_qpolynomial_free(pwqp2
);
3121 isl_pw_qpolynomial_free(pwqp1
);
3122 isl_pw_qpolynomial_free(pwqp2
);
3126 __isl_give isl_val
*isl_poly_eval(__isl_take isl_poly
*poly
,
3127 __isl_take isl_vec
*vec
)
3135 is_cst
= isl_poly_is_cst(poly
);
3140 res
= isl_poly_get_constant_val(poly
);
3141 isl_poly_free(poly
);
3145 rec
= isl_poly_as_rec(poly
);
3149 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
3151 base
= isl_val_rat_from_isl_int(poly
->ctx
,
3152 vec
->el
[1 + poly
->var
], vec
->el
[0]);
3154 res
= isl_poly_eval(isl_poly_copy(rec
->p
[rec
->n
- 1]),
3157 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3158 res
= isl_val_mul(res
, isl_val_copy(base
));
3159 res
= isl_val_add(res
, isl_poly_eval(isl_poly_copy(rec
->p
[i
]),
3160 isl_vec_copy(vec
)));
3164 isl_poly_free(poly
);
3168 isl_poly_free(poly
);
3173 /* Evaluate "qp" in the void point "pnt".
3174 * In particular, return the value NaN.
3176 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3177 __isl_take isl_point
*pnt
)
3181 ctx
= isl_point_get_ctx(pnt
);
3182 isl_qpolynomial_free(qp
);
3183 isl_point_free(pnt
);
3184 return isl_val_nan(ctx
);
3187 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3188 __isl_take isl_point
*pnt
)
3196 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3197 is_void
= isl_point_is_void(pnt
);
3201 return eval_void(qp
, pnt
);
3203 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3205 v
= isl_poly_eval(isl_poly_copy(qp
->poly
), ext
);
3207 isl_qpolynomial_free(qp
);
3208 isl_point_free(pnt
);
3212 isl_qpolynomial_free(qp
);
3213 isl_point_free(pnt
);
3217 int isl_poly_cmp(__isl_keep isl_poly_cst
*cst1
, __isl_keep isl_poly_cst
*cst2
)
3222 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3223 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3224 cmp
= isl_int_sgn(t
);
3229 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3230 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3231 unsigned first
, unsigned n
)
3239 if (type
== isl_dim_out
)
3240 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3241 "cannot insert output/set dimensions",
3243 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3244 return isl_qpolynomial_free(qp
);
3245 type
= domain_type(type
);
3246 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3249 qp
= isl_qpolynomial_cow(qp
);
3253 g_pos
= pos(qp
->dim
, type
) + first
;
3255 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3259 total
= qp
->div
->n_col
- 2;
3260 if (total
> g_pos
) {
3262 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3265 for (i
= 0; i
< total
- g_pos
; ++i
)
3267 qp
->poly
= expand(qp
->poly
, exp
, g_pos
);
3273 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3279 isl_qpolynomial_free(qp
);
3283 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3284 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3288 pos
= isl_qpolynomial_dim(qp
, type
);
3290 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3293 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3294 __isl_take isl_pw_qpolynomial
*pwqp
,
3295 enum isl_dim_type type
, unsigned n
)
3299 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3301 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3304 static int *reordering_move(isl_ctx
*ctx
,
3305 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3310 reordering
= isl_alloc_array(ctx
, int, len
);
3315 for (i
= 0; i
< dst
; ++i
)
3317 for (i
= 0; i
< n
; ++i
)
3318 reordering
[src
+ i
] = dst
+ i
;
3319 for (i
= 0; i
< src
- dst
; ++i
)
3320 reordering
[dst
+ i
] = dst
+ n
+ i
;
3321 for (i
= 0; i
< len
- src
- n
; ++i
)
3322 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3324 for (i
= 0; i
< src
; ++i
)
3326 for (i
= 0; i
< n
; ++i
)
3327 reordering
[src
+ i
] = dst
+ i
;
3328 for (i
= 0; i
< dst
- src
; ++i
)
3329 reordering
[src
+ n
+ i
] = src
+ i
;
3330 for (i
= 0; i
< len
- dst
- n
; ++i
)
3331 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3337 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3338 __isl_take isl_qpolynomial
*qp
,
3339 enum isl_dim_type dst_type
, unsigned dst_pos
,
3340 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3349 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3350 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3351 "cannot move output/set dimension",
3353 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3354 return isl_qpolynomial_free(qp
);
3355 if (dst_type
== isl_dim_in
)
3356 dst_type
= isl_dim_set
;
3357 if (src_type
== isl_dim_in
)
3358 src_type
= isl_dim_set
;
3361 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3362 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3365 qp
= isl_qpolynomial_cow(qp
);
3369 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3370 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3371 if (dst_type
> src_type
)
3374 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3381 reordering
= reordering_move(qp
->dim
->ctx
,
3382 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3386 qp
->poly
= reorder(qp
->poly
, reordering
);
3391 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3397 isl_qpolynomial_free(qp
);
3401 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3402 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3406 space
= isl_space_domain(space
);
3410 poly
= isl_poly_from_affine(space
->ctx
, f
, denom
,
3411 1 + isl_space_dim(space
, isl_dim_all
));
3413 return isl_qpolynomial_alloc(space
, 0, poly
);
3416 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3420 isl_qpolynomial
*qp
;
3425 ctx
= isl_aff_get_ctx(aff
);
3426 poly
= isl_poly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3429 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3430 aff
->ls
->div
->n_row
, poly
);
3434 isl_mat_free(qp
->div
);
3435 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3436 qp
->div
= isl_mat_cow(qp
->div
);
3441 qp
= reduce_divs(qp
);
3442 qp
= remove_redundant_divs(qp
);
3446 return isl_qpolynomial_free(qp
);
3449 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3450 __isl_take isl_pw_aff
*pwaff
)
3453 isl_pw_qpolynomial
*pwqp
;
3458 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3461 for (i
= 0; i
< pwaff
->n
; ++i
) {
3463 isl_qpolynomial
*qp
;
3465 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3466 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3467 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3470 isl_pw_aff_free(pwaff
);
3474 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3475 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3479 aff
= isl_constraint_get_bound(c
, type
, pos
);
3480 isl_constraint_free(c
);
3481 return isl_qpolynomial_from_aff(aff
);
3484 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3485 * in "qp" by subs[i].
3487 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3488 __isl_take isl_qpolynomial
*qp
,
3489 enum isl_dim_type type
, unsigned first
, unsigned n
,
3490 __isl_keep isl_qpolynomial
**subs
)
3498 qp
= isl_qpolynomial_cow(qp
);
3502 if (type
== isl_dim_out
)
3503 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3504 "cannot substitute output/set dimension",
3506 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3507 return isl_qpolynomial_free(qp
);
3508 type
= domain_type(type
);
3510 for (i
= 0; i
< n
; ++i
)
3514 for (i
= 0; i
< n
; ++i
)
3515 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3518 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3519 for (i
= 0; i
< n
; ++i
)
3520 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3522 first
+= pos(qp
->dim
, type
);
3524 polys
= isl_alloc_array(qp
->dim
->ctx
, struct isl_poly
*, n
);
3527 for (i
= 0; i
< n
; ++i
)
3528 polys
[i
] = subs
[i
]->poly
;
3530 qp
->poly
= isl_poly_subs(qp
->poly
, first
, n
, polys
);
3539 isl_qpolynomial_free(qp
);
3543 /* Extend "bset" with extra set dimensions for each integer division
3544 * in "qp" and then call "fn" with the extended bset and the polynomial
3545 * that results from replacing each of the integer divisions by the
3546 * corresponding extra set dimension.
3548 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3549 __isl_keep isl_basic_set
*bset
,
3550 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3551 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3554 isl_local_space
*ls
;
3555 isl_qpolynomial
*poly
;
3558 return isl_stat_error
;
3559 if (qp
->div
->n_row
== 0)
3560 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3563 space
= isl_space_copy(qp
->dim
);
3564 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3565 poly
= isl_qpolynomial_alloc(space
, 0, isl_poly_copy(qp
->poly
));
3566 bset
= isl_basic_set_copy(bset
);
3567 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3568 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3570 return fn(bset
, poly
, user
);
3573 /* Return total degree in variables first (inclusive) up to last (exclusive).
3575 int isl_poly_degree(__isl_keep isl_poly
*poly
, int first
, int last
)
3579 isl_bool is_zero
, is_cst
;
3582 is_zero
= isl_poly_is_zero(poly
);
3587 is_cst
= isl_poly_is_cst(poly
);
3590 if (is_cst
|| poly
->var
< first
)
3593 rec
= isl_poly_as_rec(poly
);
3597 for (i
= 0; i
< rec
->n
; ++i
) {
3600 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3605 d
= isl_poly_degree(rec
->p
[i
], first
, last
);
3606 if (poly
->var
< last
)
3615 /* Return total degree in set variables.
3617 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3625 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3626 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3627 return isl_poly_degree(poly
->poly
, ovar
, ovar
+ nvar
);
3630 __isl_give isl_poly
*isl_poly_coeff(__isl_keep isl_poly
*poly
,
3631 unsigned pos
, int deg
)
3637 is_cst
= isl_poly_is_cst(poly
);
3640 if (is_cst
|| poly
->var
< pos
) {
3642 return isl_poly_copy(poly
);
3644 return isl_poly_zero(poly
->ctx
);
3647 rec
= isl_poly_as_rec(poly
);
3651 if (poly
->var
== pos
) {
3653 return isl_poly_copy(rec
->p
[deg
]);
3655 return isl_poly_zero(poly
->ctx
);
3658 poly
= isl_poly_copy(poly
);
3659 poly
= isl_poly_cow(poly
);
3660 rec
= isl_poly_as_rec(poly
);
3664 for (i
= 0; i
< rec
->n
; ++i
) {
3666 t
= isl_poly_coeff(rec
->p
[i
], pos
, deg
);
3669 isl_poly_free(rec
->p
[i
]);
3675 isl_poly_free(poly
);
3679 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3681 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3682 __isl_keep isl_qpolynomial
*qp
,
3683 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3692 if (type
== isl_dim_out
)
3693 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3694 "output/set dimension does not have a coefficient",
3696 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
3698 type
= domain_type(type
);
3700 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3701 poly
= isl_poly_coeff(qp
->poly
, g_pos
, deg
);
3703 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
),
3704 qp
->div
->n_row
, poly
);
3707 isl_mat_free(c
->div
);
3708 c
->div
= isl_mat_copy(qp
->div
);
3713 isl_qpolynomial_free(c
);
3717 /* Homogenize the polynomial in the variables first (inclusive) up to
3718 * last (exclusive) by inserting powers of variable first.
3719 * Variable first is assumed not to appear in the input.
3721 __isl_give isl_poly
*isl_poly_homogenize(__isl_take isl_poly
*poly
, int deg
,
3722 int target
, int first
, int last
)
3725 isl_bool is_zero
, is_cst
;
3728 is_zero
= isl_poly_is_zero(poly
);
3730 return isl_poly_free(poly
);
3735 is_cst
= isl_poly_is_cst(poly
);
3737 return isl_poly_free(poly
);
3738 if (is_cst
|| poly
->var
< first
) {
3741 hom
= isl_poly_var_pow(poly
->ctx
, first
, target
- deg
);
3744 rec
= isl_poly_as_rec(hom
);
3745 rec
->p
[target
- deg
] = isl_poly_mul(rec
->p
[target
- deg
], poly
);
3750 poly
= isl_poly_cow(poly
);
3751 rec
= isl_poly_as_rec(poly
);
3755 for (i
= 0; i
< rec
->n
; ++i
) {
3756 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3758 return isl_poly_free(poly
);
3761 rec
->p
[i
] = isl_poly_homogenize(rec
->p
[i
],
3762 poly
->var
< last
? deg
+ i
: i
, target
,
3770 isl_poly_free(poly
);
3774 /* Homogenize the polynomial in the set variables by introducing
3775 * powers of an extra set variable at position 0.
3777 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3778 __isl_take isl_qpolynomial
*poly
)
3782 int deg
= isl_qpolynomial_degree(poly
);
3787 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3788 poly
= isl_qpolynomial_cow(poly
);
3792 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3793 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3794 poly
->poly
= isl_poly_homogenize(poly
->poly
, 0, deg
, ovar
, ovar
+ nvar
);
3800 isl_qpolynomial_free(poly
);
3804 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3805 __isl_take isl_mat
*div
)
3813 n
= isl_space_dim(space
, isl_dim_all
) + div
->n_row
;
3815 term
= isl_calloc(space
->ctx
, struct isl_term
,
3816 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3823 isl_int_init(term
->n
);
3824 isl_int_init(term
->d
);
3828 isl_space_free(space
);
3833 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3842 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3851 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3853 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3857 isl_int_set(dup
->n
, term
->n
);
3858 isl_int_set(dup
->d
, term
->d
);
3860 for (i
= 0; i
< total
; ++i
)
3861 dup
->pow
[i
] = term
->pow
[i
];
3866 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3874 return isl_term_dup(term
);
3877 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3882 if (--term
->ref
> 0)
3885 isl_space_free(term
->dim
);
3886 isl_mat_free(term
->div
);
3887 isl_int_clear(term
->n
);
3888 isl_int_clear(term
->d
);
3894 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3902 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3903 case isl_dim_div
: return term
->div
->n_row
;
3904 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3910 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3912 return term
? term
->dim
->ctx
: NULL
;
3915 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3919 isl_int_set(*n
, term
->n
);
3922 /* Return the coefficient of the term "term".
3924 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3929 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3934 #define TYPE isl_term
3936 #include "check_type_range_templ.c"
3938 int isl_term_get_exp(__isl_keep isl_term
*term
,
3939 enum isl_dim_type type
, unsigned pos
)
3941 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
3944 if (type
>= isl_dim_set
)
3945 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3946 if (type
>= isl_dim_div
)
3947 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3949 return term
->pow
[pos
];
3952 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3954 isl_local_space
*ls
;
3957 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
3960 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3961 isl_mat_copy(term
->div
));
3962 aff
= isl_aff_alloc(ls
);
3966 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3968 aff
= isl_aff_normalize(aff
);
3973 __isl_give isl_term
*isl_poly_foreach_term(__isl_keep isl_poly
*poly
,
3974 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3975 __isl_take isl_term
*term
, void *user
)
3978 isl_bool is_zero
, is_bad
, is_cst
;
3981 is_zero
= isl_poly_is_zero(poly
);
3982 if (is_zero
< 0 || !term
)
3988 is_cst
= isl_poly_is_cst(poly
);
3989 is_bad
= isl_poly_is_nan(poly
);
3990 if (is_bad
>= 0 && !is_bad
)
3991 is_bad
= isl_poly_is_infty(poly
);
3992 if (is_bad
>= 0 && !is_bad
)
3993 is_bad
= isl_poly_is_neginfty(poly
);
3994 if (is_cst
< 0 || is_bad
< 0)
3995 return isl_term_free(term
);
3997 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
3998 "cannot handle NaN/infty polynomial",
3999 return isl_term_free(term
));
4003 cst
= isl_poly_as_cst(poly
);
4006 term
= isl_term_cow(term
);
4009 isl_int_set(term
->n
, cst
->n
);
4010 isl_int_set(term
->d
, cst
->d
);
4011 if (fn(isl_term_copy(term
), user
) < 0)
4016 rec
= isl_poly_as_rec(poly
);
4020 for (i
= 0; i
< rec
->n
; ++i
) {
4021 term
= isl_term_cow(term
);
4024 term
->pow
[poly
->var
] = i
;
4025 term
= isl_poly_foreach_term(rec
->p
[i
], fn
, term
, user
);
4029 term
->pow
[poly
->var
] = 0;
4033 isl_term_free(term
);
4037 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
4038 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
4043 return isl_stat_error
;
4045 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
4047 return isl_stat_error
;
4049 term
= isl_poly_foreach_term(qp
->poly
, fn
, term
, user
);
4051 isl_term_free(term
);
4053 return term
? isl_stat_ok
: isl_stat_error
;
4056 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
4059 isl_qpolynomial
*qp
;
4065 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
4067 poly
= isl_poly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4068 for (i
= 0; i
< n
; ++i
) {
4071 poly
= isl_poly_mul(poly
,
4072 isl_poly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4075 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
),
4076 term
->div
->n_row
, poly
);
4079 isl_mat_free(qp
->div
);
4080 qp
->div
= isl_mat_copy(term
->div
);
4084 isl_term_free(term
);
4087 isl_qpolynomial_free(qp
);
4088 isl_term_free(term
);
4092 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4093 __isl_take isl_space
*space
)
4102 if (isl_space_is_equal(qp
->dim
, space
)) {
4103 isl_space_free(space
);
4107 qp
= isl_qpolynomial_cow(qp
);
4111 extra
= isl_space_dim(space
, isl_dim_set
) -
4112 isl_space_dim(qp
->dim
, isl_dim_set
);
4113 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4114 if (qp
->div
->n_row
) {
4117 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4120 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4122 qp
->poly
= expand(qp
->poly
, exp
, total
);
4127 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4130 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4131 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4133 isl_space_free(qp
->dim
);
4138 isl_space_free(space
);
4139 isl_qpolynomial_free(qp
);
4143 /* For each parameter or variable that does not appear in qp,
4144 * first eliminate the variable from all constraints and then set it to zero.
4146 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4147 __isl_keep isl_qpolynomial
*qp
)
4158 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4159 active
= isl_calloc_array(set
->ctx
, int, d
);
4160 if (set_active(qp
, active
) < 0)
4163 for (i
= 0; i
< d
; ++i
)
4172 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4173 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4174 for (i
= 0; i
< nparam
; ++i
) {
4177 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4178 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4180 for (i
= 0; i
< nvar
; ++i
) {
4181 if (active
[nparam
+ i
])
4183 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4184 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4196 struct isl_opt_data
{
4197 isl_qpolynomial
*qp
;
4203 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4205 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4208 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4212 } else if (data
->max
) {
4213 data
->opt
= isl_val_max(data
->opt
, val
);
4215 data
->opt
= isl_val_min(data
->opt
, val
);
4221 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4222 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4224 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4230 is_cst
= isl_poly_is_cst(qp
->poly
);
4235 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4236 isl_qpolynomial_free(qp
);
4240 set
= fix_inactive(set
, qp
);
4243 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4247 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4250 isl_qpolynomial_free(qp
);
4254 isl_qpolynomial_free(qp
);
4255 isl_val_free(data
.opt
);
4259 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4260 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4266 isl_mat
*mat
, *diag
;
4268 qp
= isl_qpolynomial_cow(qp
);
4273 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4275 n_sub
= morph
->inv
->n_row
- 1;
4276 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4277 n_sub
+= qp
->div
->n_row
;
4278 subs
= isl_calloc_array(ctx
, struct isl_poly
*, n_sub
);
4282 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4283 subs
[i
] = isl_poly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4284 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4285 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4286 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4287 subs
[morph
->inv
->n_row
- 1 + i
] =
4288 isl_poly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4290 qp
->poly
= isl_poly_subs(qp
->poly
, 0, n_sub
, subs
);
4292 for (i
= 0; i
< n_sub
; ++i
)
4293 isl_poly_free(subs
[i
]);
4296 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4297 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4298 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4299 mat
= isl_mat_diagonal(mat
, diag
);
4300 qp
->div
= isl_mat_product(qp
->div
, mat
);
4301 isl_space_free(qp
->dim
);
4302 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4304 if (!qp
->poly
|| !qp
->div
|| !qp
->dim
)
4307 isl_morph_free(morph
);
4311 isl_qpolynomial_free(qp
);
4312 isl_morph_free(morph
);
4316 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4317 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4318 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4320 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4321 &isl_pw_qpolynomial_mul
);
4324 /* Reorder the dimension of "qp" according to the given reordering.
4326 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4327 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4331 qp
= isl_qpolynomial_cow(qp
);
4335 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4339 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4343 qp
->poly
= reorder(qp
->poly
, r
->pos
);
4347 space
= isl_reordering_get_space(r
);
4348 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4350 isl_reordering_free(r
);
4353 isl_qpolynomial_free(qp
);
4354 isl_reordering_free(r
);
4358 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4359 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4361 isl_bool equal_params
;
4366 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4367 if (equal_params
< 0)
4369 if (!equal_params
) {
4370 isl_reordering
*exp
;
4372 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4373 exp
= isl_reordering_extend_space(exp
,
4374 isl_qpolynomial_get_domain_space(qp
));
4375 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4378 isl_space_free(model
);
4381 isl_space_free(model
);
4382 isl_qpolynomial_free(qp
);
4386 struct isl_split_periods_data
{
4388 isl_pw_qpolynomial
*res
;
4391 /* Create a slice where the integer division "div" has the fixed value "v".
4392 * In particular, if "div" refers to floor(f/m), then create a slice
4394 * m v <= f <= m v + (m - 1)
4399 * -f + m v + (m - 1) >= 0
4401 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4402 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4405 isl_basic_set
*bset
= NULL
;
4411 total
= isl_space_dim(space
, isl_dim_all
);
4412 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4414 k
= isl_basic_set_alloc_inequality(bset
);
4417 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4418 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4420 k
= isl_basic_set_alloc_inequality(bset
);
4423 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4424 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4425 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4426 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4428 isl_space_free(space
);
4429 return isl_set_from_basic_set(bset
);
4431 isl_basic_set_free(bset
);
4432 isl_space_free(space
);
4436 static isl_stat
split_periods(__isl_take isl_set
*set
,
4437 __isl_take isl_qpolynomial
*qp
, void *user
);
4439 /* Create a slice of the domain "set" such that integer division "div"
4440 * has the fixed value "v" and add the results to data->res,
4441 * replacing the integer division by "v" in "qp".
4443 static isl_stat
set_div(__isl_take isl_set
*set
,
4444 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4445 struct isl_split_periods_data
*data
)
4452 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4453 set
= isl_set_intersect(set
, slice
);
4458 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4460 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4461 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4463 isl_int_addmul(qp
->div
->row
[i
][1],
4464 qp
->div
->row
[i
][2 + total
+ div
], v
);
4465 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4468 cst
= isl_poly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4469 qp
= substitute_div(qp
, div
, cst
);
4471 return split_periods(set
, qp
, data
);
4474 isl_qpolynomial_free(qp
);
4475 return isl_stat_error
;
4478 /* Split the domain "set" such that integer division "div"
4479 * has a fixed value (ranging from "min" to "max") on each slice
4480 * and add the results to data->res.
4482 static isl_stat
split_div(__isl_take isl_set
*set
,
4483 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4484 struct isl_split_periods_data
*data
)
4486 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4487 isl_set
*set_i
= isl_set_copy(set
);
4488 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4490 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4494 isl_qpolynomial_free(qp
);
4498 isl_qpolynomial_free(qp
);
4499 return isl_stat_error
;
4502 /* If "qp" refers to any integer division
4503 * that can only attain "max_periods" distinct values on "set"
4504 * then split the domain along those distinct values.
4505 * Add the results (or the original if no splitting occurs)
4508 static isl_stat
split_periods(__isl_take isl_set
*set
,
4509 __isl_take isl_qpolynomial
*qp
, void *user
)
4512 isl_pw_qpolynomial
*pwqp
;
4513 struct isl_split_periods_data
*data
;
4516 isl_stat r
= isl_stat_ok
;
4518 data
= (struct isl_split_periods_data
*)user
;
4523 if (qp
->div
->n_row
== 0) {
4524 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4525 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4531 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4532 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4533 enum isl_lp_result lp_res
;
4535 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4536 qp
->div
->n_row
) != -1)
4539 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4540 set
->ctx
->one
, &min
, NULL
, NULL
);
4541 if (lp_res
== isl_lp_error
)
4543 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4545 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4547 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4548 set
->ctx
->one
, &max
, NULL
, NULL
);
4549 if (lp_res
== isl_lp_error
)
4551 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4553 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4555 isl_int_sub(max
, max
, min
);
4556 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4557 isl_int_add(max
, max
, min
);
4562 if (i
< qp
->div
->n_row
) {
4563 r
= split_div(set
, qp
, i
, min
, max
, data
);
4565 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4566 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4578 isl_qpolynomial_free(qp
);
4579 return isl_stat_error
;
4582 /* If any quasi-polynomial in pwqp refers to any integer division
4583 * that can only attain "max_periods" distinct values on its domain
4584 * then split the domain along those distinct values.
4586 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4587 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4589 struct isl_split_periods_data data
;
4591 data
.max_periods
= max_periods
;
4592 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4594 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4597 isl_pw_qpolynomial_free(pwqp
);
4601 isl_pw_qpolynomial_free(data
.res
);
4602 isl_pw_qpolynomial_free(pwqp
);
4606 /* Construct a piecewise quasipolynomial that is constant on the given
4607 * domain. In particular, it is
4610 * infinity if cst == -1
4612 * If cst == -1, then explicitly check whether the domain is empty and,
4613 * if so, return 0 instead.
4615 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4616 __isl_take isl_basic_set
*bset
, int cst
)
4619 isl_qpolynomial
*qp
;
4621 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4626 bset
= isl_basic_set_params(bset
);
4627 dim
= isl_basic_set_get_space(bset
);
4629 qp
= isl_qpolynomial_infty_on_domain(dim
);
4631 qp
= isl_qpolynomial_zero_on_domain(dim
);
4633 qp
= isl_qpolynomial_one_on_domain(dim
);
4634 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4637 /* Factor bset, call fn on each of the factors and return the product.
4639 * If no factors can be found, simply call fn on the input.
4640 * Otherwise, construct the factors based on the factorizer,
4641 * call fn on each factor and compute the product.
4643 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4644 __isl_take isl_basic_set
*bset
,
4645 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4651 isl_qpolynomial
*qp
;
4652 isl_pw_qpolynomial
*pwqp
;
4656 f
= isl_basic_set_factorizer(bset
);
4659 if (f
->n_group
== 0) {
4660 isl_factorizer_free(f
);
4664 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4665 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4667 space
= isl_basic_set_get_space(bset
);
4668 space
= isl_space_params(space
);
4669 set
= isl_set_universe(isl_space_copy(space
));
4670 qp
= isl_qpolynomial_one_on_domain(space
);
4671 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4673 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4675 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4676 isl_basic_set
*bset_i
;
4677 isl_pw_qpolynomial
*pwqp_i
;
4679 bset_i
= isl_basic_set_copy(bset
);
4680 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4681 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4682 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4684 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4685 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4686 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4688 pwqp_i
= fn(bset_i
);
4689 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4694 isl_basic_set_free(bset
);
4695 isl_factorizer_free(f
);
4699 isl_basic_set_free(bset
);
4703 /* Factor bset, call fn on each of the factors and return the product.
4704 * The function is assumed to evaluate to zero on empty domains,
4705 * to one on zero-dimensional domains and to infinity on unbounded domains
4706 * and will not be called explicitly on zero-dimensional or unbounded domains.
4708 * We first check for some special cases and remove all equalities.
4709 * Then we hand over control to compressed_multiplicative_call.
4711 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4712 __isl_take isl_basic_set
*bset
,
4713 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4717 isl_pw_qpolynomial
*pwqp
;
4722 if (isl_basic_set_plain_is_empty(bset
))
4723 return constant_on_domain(bset
, 0);
4725 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4726 return constant_on_domain(bset
, 1);
4728 bounded
= isl_basic_set_is_bounded(bset
);
4732 return constant_on_domain(bset
, -1);
4734 if (bset
->n_eq
== 0)
4735 return compressed_multiplicative_call(bset
, fn
);
4737 morph
= isl_basic_set_full_compression(bset
);
4738 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4740 pwqp
= compressed_multiplicative_call(bset
, fn
);
4742 morph
= isl_morph_dom_params(morph
);
4743 morph
= isl_morph_ran_params(morph
);
4744 morph
= isl_morph_inverse(morph
);
4746 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4750 isl_basic_set_free(bset
);
4754 /* Drop all floors in "qp", turning each integer division [a/m] into
4755 * a rational division a/m. If "down" is set, then the integer division
4756 * is replaced by (a-(m-1))/m instead.
4758 static __isl_give isl_qpolynomial
*qp_drop_floors(
4759 __isl_take isl_qpolynomial
*qp
, int down
)
4766 if (qp
->div
->n_row
== 0)
4769 qp
= isl_qpolynomial_cow(qp
);
4773 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4775 isl_int_sub(qp
->div
->row
[i
][1],
4776 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4777 isl_int_add_ui(qp
->div
->row
[i
][1],
4778 qp
->div
->row
[i
][1], 1);
4780 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4781 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4782 qp
= substitute_div(qp
, i
, s
);
4790 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4791 * a rational division a/m.
4793 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4794 __isl_take isl_pw_qpolynomial
*pwqp
)
4801 if (isl_pw_qpolynomial_is_zero(pwqp
))
4804 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4808 for (i
= 0; i
< pwqp
->n
; ++i
) {
4809 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4816 isl_pw_qpolynomial_free(pwqp
);
4820 /* Adjust all the integer divisions in "qp" such that they are at least
4821 * one over the given orthant (identified by "signs"). This ensures
4822 * that they will still be non-negative even after subtracting (m-1)/m.
4824 * In particular, f is replaced by f' + v, changing f = [a/m]
4825 * to f' = [(a - m v)/m].
4826 * If the constant term k in a is smaller than m,
4827 * the constant term of v is set to floor(k/m) - 1.
4828 * For any other term, if the coefficient c and the variable x have
4829 * the same sign, then no changes are needed.
4830 * Otherwise, if the variable is positive (and c is negative),
4831 * then the coefficient of x in v is set to floor(c/m).
4832 * If the variable is negative (and c is positive),
4833 * then the coefficient of x in v is set to ceil(c/m).
4835 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4843 qp
= isl_qpolynomial_cow(qp
);
4846 qp
->div
= isl_mat_cow(qp
->div
);
4850 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4851 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4853 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4854 isl_int
*row
= qp
->div
->row
[i
];
4858 if (isl_int_lt(row
[1], row
[0])) {
4859 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4860 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4861 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4863 for (j
= 0; j
< total
; ++j
) {
4864 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4867 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4869 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4870 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4872 for (j
= 0; j
< i
; ++j
) {
4873 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4875 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4876 row
[2 + total
+ j
], row
[0]);
4877 isl_int_submul(row
[2 + total
+ j
],
4878 row
[0], v
->el
[1 + total
+ j
]);
4880 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4881 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4883 isl_seq_combine(qp
->div
->row
[j
] + 1,
4884 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4885 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4887 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4888 s
= isl_poly_from_affine(qp
->dim
->ctx
, v
->el
,
4889 qp
->div
->ctx
->one
, v
->size
);
4890 qp
->poly
= isl_poly_subs(qp
->poly
, total
+ i
, 1, &s
);
4900 isl_qpolynomial_free(qp
);
4904 struct isl_to_poly_data
{
4906 isl_pw_qpolynomial
*res
;
4907 isl_qpolynomial
*qp
;
4910 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4911 * We first make all integer divisions positive and then split the
4912 * quasipolynomials into terms with sign data->sign (the direction
4913 * of the requested approximation) and terms with the opposite sign.
4914 * In the first set of terms, each integer division [a/m] is
4915 * overapproximated by a/m, while in the second it is underapproximated
4918 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4919 int *signs
, void *user
)
4921 struct isl_to_poly_data
*data
= user
;
4922 isl_pw_qpolynomial
*t
;
4923 isl_qpolynomial
*qp
, *up
, *down
;
4925 qp
= isl_qpolynomial_copy(data
->qp
);
4926 qp
= make_divs_pos(qp
, signs
);
4928 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4929 up
= qp_drop_floors(up
, 0);
4930 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4931 down
= qp_drop_floors(down
, 1);
4933 isl_qpolynomial_free(qp
);
4934 qp
= isl_qpolynomial_add(up
, down
);
4936 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4937 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4942 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4943 * the polynomial will be an overapproximation. If "sign" is negative,
4944 * it will be an underapproximation. If "sign" is zero, the approximation
4945 * will lie somewhere in between.
4947 * In particular, is sign == 0, we simply drop the floors, turning
4948 * the integer divisions into rational divisions.
4949 * Otherwise, we split the domains into orthants, make all integer divisions
4950 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4951 * depending on the requested sign and the sign of the term in which
4952 * the integer division appears.
4954 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4955 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4958 struct isl_to_poly_data data
;
4961 return pwqp_drop_floors(pwqp
);
4967 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4969 for (i
= 0; i
< pwqp
->n
; ++i
) {
4970 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4971 isl_pw_qpolynomial
*t
;
4972 t
= isl_pw_qpolynomial_alloc(
4973 isl_set_copy(pwqp
->p
[i
].set
),
4974 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4975 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4978 data
.qp
= pwqp
->p
[i
].qp
;
4979 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4980 &to_polynomial_on_orthant
, &data
) < 0)
4984 isl_pw_qpolynomial_free(pwqp
);
4988 isl_pw_qpolynomial_free(pwqp
);
4989 isl_pw_qpolynomial_free(data
.res
);
4993 static __isl_give isl_pw_qpolynomial
*poly_entry(
4994 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4998 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
5001 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
5002 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
5004 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
5005 &poly_entry
, &sign
);
5008 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
5009 __isl_take isl_qpolynomial
*qp
)
5013 isl_vec
*aff
= NULL
;
5014 isl_basic_map
*bmap
= NULL
;
5021 is_affine
= isl_poly_is_affine(qp
->poly
);
5025 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
5026 "input quasi-polynomial not affine", goto error
);
5027 aff
= isl_qpolynomial_extract_affine(qp
);
5030 dim
= isl_qpolynomial_get_space(qp
);
5031 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
5032 n_div
= qp
->div
->n_row
;
5033 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
5035 for (i
= 0; i
< n_div
; ++i
) {
5036 k
= isl_basic_map_alloc_div(bmap
);
5039 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
5040 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
5041 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
5044 k
= isl_basic_map_alloc_equality(bmap
);
5047 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5048 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5049 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5052 isl_qpolynomial_free(qp
);
5053 bmap
= isl_basic_map_finalize(bmap
);
5057 isl_qpolynomial_free(qp
);
5058 isl_basic_map_free(bmap
);