2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
32 #define BASE pw_qpolynomial
34 #include <isl_list_templ.c>
36 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
39 case isl_dim_param
: return 0;
40 case isl_dim_in
: return dim
->nparam
;
41 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
46 isl_bool
isl_poly_is_cst(__isl_keep isl_poly
*poly
)
49 return isl_bool_error
;
54 __isl_keep isl_poly_cst
*isl_poly_as_cst(__isl_keep isl_poly
*poly
)
59 isl_assert(poly
->ctx
, poly
->var
< 0, return NULL
);
61 return (isl_poly_cst
*) poly
;
64 __isl_keep isl_poly_rec
*isl_poly_as_rec(__isl_keep isl_poly
*poly
)
69 isl_assert(poly
->ctx
, poly
->var
>= 0, return NULL
);
71 return (isl_poly_rec
*) poly
;
74 /* Compare two polynomials.
76 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
77 * than "poly2" and 0 if they are equal.
79 static int isl_poly_plain_cmp(__isl_keep isl_poly
*poly1
,
80 __isl_keep isl_poly
*poly2
)
84 isl_poly_rec
*rec1
, *rec2
;
88 is_cst1
= isl_poly_is_cst(poly1
);
93 if (poly1
->var
!= poly2
->var
)
94 return poly1
->var
- poly2
->var
;
97 isl_poly_cst
*cst1
, *cst2
;
100 cst1
= isl_poly_as_cst(poly1
);
101 cst2
= isl_poly_as_cst(poly2
);
104 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
107 return isl_int_cmp(cst1
->d
, cst2
->d
);
110 rec1
= isl_poly_as_rec(poly1
);
111 rec2
= isl_poly_as_rec(poly2
);
115 if (rec1
->n
!= rec2
->n
)
116 return rec1
->n
- rec2
->n
;
118 for (i
= 0; i
< rec1
->n
; ++i
) {
119 int cmp
= isl_poly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
127 isl_bool
isl_poly_is_equal(__isl_keep isl_poly
*poly1
,
128 __isl_keep isl_poly
*poly2
)
132 isl_poly_rec
*rec1
, *rec2
;
134 is_cst1
= isl_poly_is_cst(poly1
);
135 if (is_cst1
< 0 || !poly2
)
136 return isl_bool_error
;
138 return isl_bool_true
;
139 if (poly1
->var
!= poly2
->var
)
140 return isl_bool_false
;
142 isl_poly_cst
*cst1
, *cst2
;
143 cst1
= isl_poly_as_cst(poly1
);
144 cst2
= isl_poly_as_cst(poly2
);
146 return isl_bool_error
;
147 return isl_int_eq(cst1
->n
, cst2
->n
) &&
148 isl_int_eq(cst1
->d
, cst2
->d
);
151 rec1
= isl_poly_as_rec(poly1
);
152 rec2
= isl_poly_as_rec(poly2
);
154 return isl_bool_error
;
156 if (rec1
->n
!= rec2
->n
)
157 return isl_bool_false
;
159 for (i
= 0; i
< rec1
->n
; ++i
) {
160 isl_bool eq
= isl_poly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
165 return isl_bool_true
;
168 isl_bool
isl_poly_is_zero(__isl_keep isl_poly
*poly
)
173 is_cst
= isl_poly_is_cst(poly
);
174 if (is_cst
< 0 || !is_cst
)
177 cst
= isl_poly_as_cst(poly
);
179 return isl_bool_error
;
181 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
184 int isl_poly_sgn(__isl_keep isl_poly
*poly
)
189 is_cst
= isl_poly_is_cst(poly
);
190 if (is_cst
< 0 || !is_cst
)
193 cst
= isl_poly_as_cst(poly
);
197 return isl_int_sgn(cst
->n
);
200 isl_bool
isl_poly_is_nan(__isl_keep isl_poly
*poly
)
205 is_cst
= isl_poly_is_cst(poly
);
206 if (is_cst
< 0 || !is_cst
)
209 cst
= isl_poly_as_cst(poly
);
211 return isl_bool_error
;
213 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
216 isl_bool
isl_poly_is_infty(__isl_keep isl_poly
*poly
)
221 is_cst
= isl_poly_is_cst(poly
);
222 if (is_cst
< 0 || !is_cst
)
225 cst
= isl_poly_as_cst(poly
);
227 return isl_bool_error
;
229 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
232 isl_bool
isl_poly_is_neginfty(__isl_keep isl_poly
*poly
)
237 is_cst
= isl_poly_is_cst(poly
);
238 if (is_cst
< 0 || !is_cst
)
241 cst
= isl_poly_as_cst(poly
);
243 return isl_bool_error
;
245 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
248 isl_bool
isl_poly_is_one(__isl_keep isl_poly
*poly
)
253 is_cst
= isl_poly_is_cst(poly
);
254 if (is_cst
< 0 || !is_cst
)
257 cst
= isl_poly_as_cst(poly
);
259 return isl_bool_error
;
261 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
264 isl_bool
isl_poly_is_negone(__isl_keep isl_poly
*poly
)
269 is_cst
= isl_poly_is_cst(poly
);
270 if (is_cst
< 0 || !is_cst
)
273 cst
= isl_poly_as_cst(poly
);
275 return isl_bool_error
;
277 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
280 __isl_give isl_poly_cst
*isl_poly_cst_alloc(isl_ctx
*ctx
)
284 cst
= isl_alloc_type(ctx
, struct isl_poly_cst
);
293 isl_int_init(cst
->n
);
294 isl_int_init(cst
->d
);
299 __isl_give isl_poly
*isl_poly_zero(isl_ctx
*ctx
)
303 cst
= isl_poly_cst_alloc(ctx
);
307 isl_int_set_si(cst
->n
, 0);
308 isl_int_set_si(cst
->d
, 1);
313 __isl_give isl_poly
*isl_poly_one(isl_ctx
*ctx
)
317 cst
= isl_poly_cst_alloc(ctx
);
321 isl_int_set_si(cst
->n
, 1);
322 isl_int_set_si(cst
->d
, 1);
327 __isl_give isl_poly
*isl_poly_infty(isl_ctx
*ctx
)
331 cst
= isl_poly_cst_alloc(ctx
);
335 isl_int_set_si(cst
->n
, 1);
336 isl_int_set_si(cst
->d
, 0);
341 __isl_give isl_poly
*isl_poly_neginfty(isl_ctx
*ctx
)
345 cst
= isl_poly_cst_alloc(ctx
);
349 isl_int_set_si(cst
->n
, -1);
350 isl_int_set_si(cst
->d
, 0);
355 __isl_give isl_poly
*isl_poly_nan(isl_ctx
*ctx
)
359 cst
= isl_poly_cst_alloc(ctx
);
363 isl_int_set_si(cst
->n
, 0);
364 isl_int_set_si(cst
->d
, 0);
369 __isl_give isl_poly
*isl_poly_rat_cst(isl_ctx
*ctx
, isl_int n
, isl_int d
)
373 cst
= isl_poly_cst_alloc(ctx
);
377 isl_int_set(cst
->n
, n
);
378 isl_int_set(cst
->d
, d
);
383 __isl_give isl_poly_rec
*isl_poly_alloc_rec(isl_ctx
*ctx
, int var
, int size
)
387 isl_assert(ctx
, var
>= 0, return NULL
);
388 isl_assert(ctx
, size
>= 0, return NULL
);
389 rec
= isl_calloc(ctx
, struct isl_poly_rec
,
390 sizeof(struct isl_poly_rec
) +
391 size
* sizeof(struct isl_poly
*));
406 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
407 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
409 qp
= isl_qpolynomial_cow(qp
);
413 isl_space_free(qp
->dim
);
418 isl_qpolynomial_free(qp
);
423 /* Reset the space of "qp". This function is called from isl_pw_templ.c
424 * and doesn't know if the space of an element object is represented
425 * directly or through its domain. It therefore passes along both.
427 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
428 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
429 __isl_take isl_space
*domain
)
431 isl_space_free(space
);
432 return isl_qpolynomial_reset_domain_space(qp
, domain
);
435 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
437 return qp
? qp
->dim
->ctx
: NULL
;
440 /* Return the domain space of "qp".
442 static __isl_keep isl_space
*isl_qpolynomial_peek_domain_space(
443 __isl_keep isl_qpolynomial
*qp
)
445 return qp
? qp
->dim
: NULL
;
448 /* Return a copy of the domain space of "qp".
450 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
451 __isl_keep isl_qpolynomial
*qp
)
453 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp
));
456 /* Return a copy of the local space on which "qp" is defined.
458 static __isl_give isl_local_space
*isl_qpolynomial_get_domain_local_space(
459 __isl_keep isl_qpolynomial
*qp
)
466 space
= isl_qpolynomial_get_domain_space(qp
);
467 return isl_local_space_alloc_div(space
, isl_mat_copy(qp
->div
));
470 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
475 space
= isl_space_copy(qp
->dim
);
476 space
= isl_space_from_domain(space
);
477 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
481 /* Return the number of variables of the given type in the domain of "qp".
483 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
484 enum isl_dim_type type
)
488 if (type
== isl_dim_div
)
489 return qp
->div
->n_row
;
490 if (type
== isl_dim_all
)
491 return isl_space_dim(qp
->dim
, isl_dim_all
) +
492 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
493 return isl_space_dim(qp
->dim
, type
);
496 /* Given the type of a dimension of an isl_qpolynomial,
497 * return the type of the corresponding dimension in its domain.
498 * This function is only called for "type" equal to isl_dim_in or
501 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
503 return type
== isl_dim_in
? isl_dim_set
: type
;
506 /* Externally, an isl_qpolynomial has a map space, but internally, the
507 * ls field corresponds to the domain of that space.
509 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
510 enum isl_dim_type type
)
514 if (type
== isl_dim_out
)
516 type
= domain_type(type
);
517 return isl_qpolynomial_domain_dim(qp
, type
);
520 /* Return the offset of the first variable of type "type" within
521 * the variables of the domain of "qp".
523 static int isl_qpolynomial_domain_var_offset(__isl_keep isl_qpolynomial
*qp
,
524 enum isl_dim_type type
)
528 space
= isl_qpolynomial_peek_domain_space(qp
);
534 case isl_dim_set
: return isl_space_offset(space
, type
);
535 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
538 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
539 "invalid dimension type", return -1);
543 /* Return the offset of the first coefficient of type "type" in
544 * the domain of "qp".
546 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
547 enum isl_dim_type type
)
555 return 1 + isl_qpolynomial_domain_var_offset(qp
, type
);
561 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
563 return qp
? isl_poly_is_zero(qp
->poly
) : isl_bool_error
;
566 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
568 return qp
? isl_poly_is_one(qp
->poly
) : isl_bool_error
;
571 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
573 return qp
? isl_poly_is_nan(qp
->poly
) : isl_bool_error
;
576 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
578 return qp
? isl_poly_is_infty(qp
->poly
) : isl_bool_error
;
581 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
583 return qp
? isl_poly_is_neginfty(qp
->poly
) : isl_bool_error
;
586 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
588 return qp
? isl_poly_sgn(qp
->poly
) : 0;
591 static void poly_free_cst(__isl_take isl_poly_cst
*cst
)
593 isl_int_clear(cst
->n
);
594 isl_int_clear(cst
->d
);
597 static void poly_free_rec(__isl_take isl_poly_rec
*rec
)
601 for (i
= 0; i
< rec
->n
; ++i
)
602 isl_poly_free(rec
->p
[i
]);
605 __isl_give isl_poly
*isl_poly_copy(__isl_keep isl_poly
*poly
)
614 __isl_give isl_poly
*isl_poly_dup_cst(__isl_keep isl_poly
*poly
)
619 cst
= isl_poly_as_cst(poly
);
623 dup
= isl_poly_as_cst(isl_poly_zero(poly
->ctx
));
626 isl_int_set(dup
->n
, cst
->n
);
627 isl_int_set(dup
->d
, cst
->d
);
632 __isl_give isl_poly
*isl_poly_dup_rec(__isl_keep isl_poly
*poly
)
638 rec
= isl_poly_as_rec(poly
);
642 dup
= isl_poly_alloc_rec(poly
->ctx
, poly
->var
, rec
->n
);
646 for (i
= 0; i
< rec
->n
; ++i
) {
647 dup
->p
[i
] = isl_poly_copy(rec
->p
[i
]);
655 isl_poly_free(&dup
->poly
);
659 __isl_give isl_poly
*isl_poly_dup(__isl_keep isl_poly
*poly
)
663 is_cst
= isl_poly_is_cst(poly
);
667 return isl_poly_dup_cst(poly
);
669 return isl_poly_dup_rec(poly
);
672 __isl_give isl_poly
*isl_poly_cow(__isl_take isl_poly
*poly
)
680 return isl_poly_dup(poly
);
683 __isl_null isl_poly
*isl_poly_free(__isl_take isl_poly
*poly
)
692 poly_free_cst((isl_poly_cst
*) poly
);
694 poly_free_rec((isl_poly_rec
*) poly
);
696 isl_ctx_deref(poly
->ctx
);
701 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst
*cst
)
706 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
707 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
708 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
709 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
714 __isl_give isl_poly
*isl_poly_sum_cst(__isl_take isl_poly
*poly1
,
715 __isl_take isl_poly
*poly2
)
720 poly1
= isl_poly_cow(poly1
);
721 if (!poly1
|| !poly2
)
724 cst1
= isl_poly_as_cst(poly1
);
725 cst2
= isl_poly_as_cst(poly2
);
727 if (isl_int_eq(cst1
->d
, cst2
->d
))
728 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
730 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
731 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
732 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
735 isl_poly_cst_reduce(cst1
);
737 isl_poly_free(poly2
);
740 isl_poly_free(poly1
);
741 isl_poly_free(poly2
);
745 static __isl_give isl_poly
*replace_by_zero(__isl_take isl_poly
*poly
)
753 return isl_poly_zero(ctx
);
756 static __isl_give isl_poly
*replace_by_constant_term(__isl_take isl_poly
*poly
)
764 rec
= isl_poly_as_rec(poly
);
767 cst
= isl_poly_copy(rec
->p
[0]);
775 __isl_give isl_poly
*isl_poly_sum(__isl_take isl_poly
*poly1
,
776 __isl_take isl_poly
*poly2
)
779 isl_bool is_zero
, is_nan
, is_cst
;
780 isl_poly_rec
*rec1
, *rec2
;
782 if (!poly1
|| !poly2
)
785 is_nan
= isl_poly_is_nan(poly1
);
789 isl_poly_free(poly2
);
793 is_nan
= isl_poly_is_nan(poly2
);
797 isl_poly_free(poly1
);
801 is_zero
= isl_poly_is_zero(poly1
);
805 isl_poly_free(poly1
);
809 is_zero
= isl_poly_is_zero(poly2
);
813 isl_poly_free(poly2
);
817 if (poly1
->var
< poly2
->var
)
818 return isl_poly_sum(poly2
, poly1
);
820 if (poly2
->var
< poly1
->var
) {
824 is_infty
= isl_poly_is_infty(poly2
);
825 if (is_infty
>= 0 && !is_infty
)
826 is_infty
= isl_poly_is_neginfty(poly2
);
830 isl_poly_free(poly1
);
833 poly1
= isl_poly_cow(poly1
);
834 rec
= isl_poly_as_rec(poly1
);
837 rec
->p
[0] = isl_poly_sum(rec
->p
[0], poly2
);
839 poly1
= replace_by_constant_term(poly1
);
843 is_cst
= isl_poly_is_cst(poly1
);
847 return isl_poly_sum_cst(poly1
, poly2
);
849 rec1
= isl_poly_as_rec(poly1
);
850 rec2
= isl_poly_as_rec(poly2
);
854 if (rec1
->n
< rec2
->n
)
855 return isl_poly_sum(poly2
, poly1
);
857 poly1
= isl_poly_cow(poly1
);
858 rec1
= isl_poly_as_rec(poly1
);
862 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
865 rec1
->p
[i
] = isl_poly_sum(rec1
->p
[i
],
866 isl_poly_copy(rec2
->p
[i
]));
869 if (i
!= rec1
->n
- 1)
871 is_zero
= isl_poly_is_zero(rec1
->p
[i
]);
875 isl_poly_free(rec1
->p
[i
]);
881 poly1
= replace_by_zero(poly1
);
882 else if (rec1
->n
== 1)
883 poly1
= replace_by_constant_term(poly1
);
885 isl_poly_free(poly2
);
889 isl_poly_free(poly1
);
890 isl_poly_free(poly2
);
894 __isl_give isl_poly
*isl_poly_cst_add_isl_int(__isl_take isl_poly
*poly
,
899 poly
= isl_poly_cow(poly
);
903 cst
= isl_poly_as_cst(poly
);
905 isl_int_addmul(cst
->n
, cst
->d
, v
);
910 __isl_give isl_poly
*isl_poly_add_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
915 is_cst
= isl_poly_is_cst(poly
);
917 return isl_poly_free(poly
);
919 return isl_poly_cst_add_isl_int(poly
, v
);
921 poly
= isl_poly_cow(poly
);
922 rec
= isl_poly_as_rec(poly
);
926 rec
->p
[0] = isl_poly_add_isl_int(rec
->p
[0], v
);
936 __isl_give isl_poly
*isl_poly_cst_mul_isl_int(__isl_take isl_poly
*poly
,
942 is_zero
= isl_poly_is_zero(poly
);
944 return isl_poly_free(poly
);
948 poly
= isl_poly_cow(poly
);
952 cst
= isl_poly_as_cst(poly
);
954 isl_int_mul(cst
->n
, cst
->n
, v
);
959 __isl_give isl_poly
*isl_poly_mul_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
965 is_cst
= isl_poly_is_cst(poly
);
967 return isl_poly_free(poly
);
969 return isl_poly_cst_mul_isl_int(poly
, v
);
971 poly
= isl_poly_cow(poly
);
972 rec
= isl_poly_as_rec(poly
);
976 for (i
= 0; i
< rec
->n
; ++i
) {
977 rec
->p
[i
] = isl_poly_mul_isl_int(rec
->p
[i
], v
);
988 /* Multiply the constant polynomial "poly" by "v".
990 static __isl_give isl_poly
*isl_poly_cst_scale_val(__isl_take isl_poly
*poly
,
991 __isl_keep isl_val
*v
)
996 is_zero
= isl_poly_is_zero(poly
);
998 return isl_poly_free(poly
);
1002 poly
= isl_poly_cow(poly
);
1006 cst
= isl_poly_as_cst(poly
);
1008 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
1009 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
1010 isl_poly_cst_reduce(cst
);
1015 /* Multiply the polynomial "poly" by "v".
1017 static __isl_give isl_poly
*isl_poly_scale_val(__isl_take isl_poly
*poly
,
1018 __isl_keep isl_val
*v
)
1024 is_cst
= isl_poly_is_cst(poly
);
1026 return isl_poly_free(poly
);
1028 return isl_poly_cst_scale_val(poly
, v
);
1030 poly
= isl_poly_cow(poly
);
1031 rec
= isl_poly_as_rec(poly
);
1035 for (i
= 0; i
< rec
->n
; ++i
) {
1036 rec
->p
[i
] = isl_poly_scale_val(rec
->p
[i
], v
);
1043 isl_poly_free(poly
);
1047 __isl_give isl_poly
*isl_poly_mul_cst(__isl_take isl_poly
*poly1
,
1048 __isl_take isl_poly
*poly2
)
1053 poly1
= isl_poly_cow(poly1
);
1054 if (!poly1
|| !poly2
)
1057 cst1
= isl_poly_as_cst(poly1
);
1058 cst2
= isl_poly_as_cst(poly2
);
1060 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
1061 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
1063 isl_poly_cst_reduce(cst1
);
1065 isl_poly_free(poly2
);
1068 isl_poly_free(poly1
);
1069 isl_poly_free(poly2
);
1073 __isl_give isl_poly
*isl_poly_mul_rec(__isl_take isl_poly
*poly1
,
1074 __isl_take isl_poly
*poly2
)
1078 isl_poly_rec
*res
= NULL
;
1082 rec1
= isl_poly_as_rec(poly1
);
1083 rec2
= isl_poly_as_rec(poly2
);
1086 size
= rec1
->n
+ rec2
->n
- 1;
1087 res
= isl_poly_alloc_rec(poly1
->ctx
, poly1
->var
, size
);
1091 for (i
= 0; i
< rec1
->n
; ++i
) {
1092 res
->p
[i
] = isl_poly_mul(isl_poly_copy(rec2
->p
[0]),
1093 isl_poly_copy(rec1
->p
[i
]));
1098 for (; i
< size
; ++i
) {
1099 res
->p
[i
] = isl_poly_zero(poly1
->ctx
);
1104 for (i
= 0; i
< rec1
->n
; ++i
) {
1105 for (j
= 1; j
< rec2
->n
; ++j
) {
1107 poly
= isl_poly_mul(isl_poly_copy(rec2
->p
[j
]),
1108 isl_poly_copy(rec1
->p
[i
]));
1109 res
->p
[i
+ j
] = isl_poly_sum(res
->p
[i
+ j
], poly
);
1115 isl_poly_free(poly1
);
1116 isl_poly_free(poly2
);
1120 isl_poly_free(poly1
);
1121 isl_poly_free(poly2
);
1122 isl_poly_free(&res
->poly
);
1126 __isl_give isl_poly
*isl_poly_mul(__isl_take isl_poly
*poly1
,
1127 __isl_take isl_poly
*poly2
)
1129 isl_bool is_zero
, is_nan
, is_one
, is_cst
;
1131 if (!poly1
|| !poly2
)
1134 is_nan
= isl_poly_is_nan(poly1
);
1138 isl_poly_free(poly2
);
1142 is_nan
= isl_poly_is_nan(poly2
);
1146 isl_poly_free(poly1
);
1150 is_zero
= isl_poly_is_zero(poly1
);
1154 isl_poly_free(poly2
);
1158 is_zero
= isl_poly_is_zero(poly2
);
1162 isl_poly_free(poly1
);
1166 is_one
= isl_poly_is_one(poly1
);
1170 isl_poly_free(poly1
);
1174 is_one
= isl_poly_is_one(poly2
);
1178 isl_poly_free(poly2
);
1182 if (poly1
->var
< poly2
->var
)
1183 return isl_poly_mul(poly2
, poly1
);
1185 if (poly2
->var
< poly1
->var
) {
1190 is_infty
= isl_poly_is_infty(poly2
);
1191 if (is_infty
>= 0 && !is_infty
)
1192 is_infty
= isl_poly_is_neginfty(poly2
);
1196 isl_ctx
*ctx
= poly1
->ctx
;
1197 isl_poly_free(poly1
);
1198 isl_poly_free(poly2
);
1199 return isl_poly_nan(ctx
);
1201 poly1
= isl_poly_cow(poly1
);
1202 rec
= isl_poly_as_rec(poly1
);
1206 for (i
= 0; i
< rec
->n
; ++i
) {
1207 rec
->p
[i
] = isl_poly_mul(rec
->p
[i
],
1208 isl_poly_copy(poly2
));
1212 isl_poly_free(poly2
);
1216 is_cst
= isl_poly_is_cst(poly1
);
1220 return isl_poly_mul_cst(poly1
, poly2
);
1222 return isl_poly_mul_rec(poly1
, poly2
);
1224 isl_poly_free(poly1
);
1225 isl_poly_free(poly2
);
1229 __isl_give isl_poly
*isl_poly_pow(__isl_take isl_poly
*poly
, unsigned power
)
1239 res
= isl_poly_copy(poly
);
1241 res
= isl_poly_one(poly
->ctx
);
1243 while (power
>>= 1) {
1244 poly
= isl_poly_mul(poly
, isl_poly_copy(poly
));
1246 res
= isl_poly_mul(res
, isl_poly_copy(poly
));
1249 isl_poly_free(poly
);
1253 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1254 unsigned n_div
, __isl_take isl_poly
*poly
)
1256 struct isl_qpolynomial
*qp
= NULL
;
1259 if (!space
|| !poly
)
1262 if (!isl_space_is_set(space
))
1263 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1264 "domain of polynomial should be a set", goto error
);
1266 total
= isl_space_dim(space
, isl_dim_all
);
1268 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1273 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1282 isl_space_free(space
);
1283 isl_poly_free(poly
);
1284 isl_qpolynomial_free(qp
);
1288 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1297 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1299 struct isl_qpolynomial
*dup
;
1304 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1305 isl_poly_copy(qp
->poly
));
1308 isl_mat_free(dup
->div
);
1309 dup
->div
= isl_mat_copy(qp
->div
);
1315 isl_qpolynomial_free(dup
);
1319 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1327 return isl_qpolynomial_dup(qp
);
1330 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1331 __isl_take isl_qpolynomial
*qp
)
1339 isl_space_free(qp
->dim
);
1340 isl_mat_free(qp
->div
);
1341 isl_poly_free(qp
->poly
);
1347 __isl_give isl_poly
*isl_poly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1353 rec
= isl_poly_alloc_rec(ctx
, pos
, 1 + power
);
1356 for (i
= 0; i
< 1 + power
; ++i
) {
1357 rec
->p
[i
] = isl_poly_zero(ctx
);
1362 cst
= isl_poly_as_cst(rec
->p
[power
]);
1363 isl_int_set_si(cst
->n
, 1);
1367 isl_poly_free(&rec
->poly
);
1371 /* r array maps original positions to new positions.
1373 static __isl_give isl_poly
*reorder(__isl_take isl_poly
*poly
, int *r
)
1381 is_cst
= isl_poly_is_cst(poly
);
1383 return isl_poly_free(poly
);
1387 rec
= isl_poly_as_rec(poly
);
1391 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
1393 base
= isl_poly_var_pow(poly
->ctx
, r
[poly
->var
], 1);
1394 res
= reorder(isl_poly_copy(rec
->p
[rec
->n
- 1]), r
);
1396 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1397 res
= isl_poly_mul(res
, isl_poly_copy(base
));
1398 res
= isl_poly_sum(res
, reorder(isl_poly_copy(rec
->p
[i
]), r
));
1401 isl_poly_free(base
);
1402 isl_poly_free(poly
);
1406 isl_poly_free(poly
);
1410 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1411 __isl_keep isl_mat
*div2
)
1416 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1417 div1
->n_col
>= div2
->n_col
,
1418 return isl_bool_error
);
1420 if (div1
->n_row
== div2
->n_row
)
1421 return isl_mat_is_equal(div1
, div2
);
1423 n_row
= div1
->n_row
;
1424 n_col
= div1
->n_col
;
1425 div1
->n_row
= div2
->n_row
;
1426 div1
->n_col
= div2
->n_col
;
1428 equal
= isl_mat_is_equal(div1
, div2
);
1430 div1
->n_row
= n_row
;
1431 div1
->n_col
= n_col
;
1436 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1440 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1441 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1446 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1449 struct isl_div_sort_info
{
1454 static int div_sort_cmp(const void *p1
, const void *p2
)
1456 const struct isl_div_sort_info
*i1
, *i2
;
1457 i1
= (const struct isl_div_sort_info
*) p1
;
1458 i2
= (const struct isl_div_sort_info
*) p2
;
1460 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1463 /* Sort divs and remove duplicates.
1465 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1470 struct isl_div_sort_info
*array
= NULL
;
1471 int *pos
= NULL
, *at
= NULL
;
1472 int *reordering
= NULL
;
1477 if (qp
->div
->n_row
<= 1)
1480 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
1482 return isl_qpolynomial_free(qp
);
1484 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1486 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1487 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1488 len
= qp
->div
->n_col
- 2;
1489 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1490 if (!array
|| !pos
|| !at
|| !reordering
)
1493 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1494 array
[i
].div
= qp
->div
;
1500 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1503 for (i
= 0; i
< div_pos
; ++i
)
1506 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1507 if (pos
[array
[i
].row
] == i
)
1509 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1510 pos
[at
[i
]] = pos
[array
[i
].row
];
1511 at
[pos
[array
[i
].row
]] = at
[i
];
1512 at
[i
] = array
[i
].row
;
1513 pos
[array
[i
].row
] = i
;
1517 for (i
= 0; i
< len
- div_pos
; ++i
) {
1519 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1520 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1521 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1522 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1523 2 + div_pos
+ i
- skip
);
1524 qp
->div
= isl_mat_drop_cols(qp
->div
,
1525 2 + div_pos
+ i
- skip
, 1);
1528 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1531 qp
->poly
= reorder(qp
->poly
, reordering
);
1533 if (!qp
->poly
|| !qp
->div
)
1547 isl_qpolynomial_free(qp
);
1551 static __isl_give isl_poly
*expand(__isl_take isl_poly
*poly
, int *exp
,
1558 is_cst
= isl_poly_is_cst(poly
);
1560 return isl_poly_free(poly
);
1564 if (poly
->var
< first
)
1567 if (exp
[poly
->var
- first
] == poly
->var
- first
)
1570 poly
= isl_poly_cow(poly
);
1574 poly
->var
= exp
[poly
->var
- first
] + first
;
1576 rec
= isl_poly_as_rec(poly
);
1580 for (i
= 0; i
< rec
->n
; ++i
) {
1581 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1588 isl_poly_free(poly
);
1592 static __isl_give isl_qpolynomial
*with_merged_divs(
1593 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1594 __isl_take isl_qpolynomial
*qp2
),
1595 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1599 isl_mat
*div
= NULL
;
1602 qp1
= isl_qpolynomial_cow(qp1
);
1603 qp2
= isl_qpolynomial_cow(qp2
);
1608 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1609 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1611 n_div1
= qp1
->div
->n_row
;
1612 n_div2
= qp2
->div
->n_row
;
1613 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1614 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1615 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1618 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1622 isl_mat_free(qp1
->div
);
1623 qp1
->div
= isl_mat_copy(div
);
1624 isl_mat_free(qp2
->div
);
1625 qp2
->div
= isl_mat_copy(div
);
1627 qp1
->poly
= expand(qp1
->poly
, exp1
, div
->n_col
- div
->n_row
- 2);
1628 qp2
->poly
= expand(qp2
->poly
, exp2
, div
->n_col
- div
->n_row
- 2);
1630 if (!qp1
->poly
|| !qp2
->poly
)
1637 return fn(qp1
, qp2
);
1642 isl_qpolynomial_free(qp1
);
1643 isl_qpolynomial_free(qp2
);
1647 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1648 __isl_take isl_qpolynomial
*qp2
)
1650 isl_bool compatible
;
1652 qp1
= isl_qpolynomial_cow(qp1
);
1657 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1658 return isl_qpolynomial_add(qp2
, qp1
);
1660 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1661 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1665 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1667 qp1
->poly
= isl_poly_sum(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1671 isl_qpolynomial_free(qp2
);
1675 isl_qpolynomial_free(qp1
);
1676 isl_qpolynomial_free(qp2
);
1680 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1681 __isl_keep isl_set
*dom
,
1682 __isl_take isl_qpolynomial
*qp1
,
1683 __isl_take isl_qpolynomial
*qp2
)
1685 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1686 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1690 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1691 __isl_take isl_qpolynomial
*qp2
)
1693 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1696 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1697 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1699 if (isl_int_is_zero(v
))
1702 qp
= isl_qpolynomial_cow(qp
);
1706 qp
->poly
= isl_poly_add_isl_int(qp
->poly
, v
);
1712 isl_qpolynomial_free(qp
);
1717 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1722 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1725 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1726 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1728 if (isl_int_is_one(v
))
1731 if (qp
&& isl_int_is_zero(v
)) {
1732 isl_qpolynomial
*zero
;
1733 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1734 isl_qpolynomial_free(qp
);
1738 qp
= isl_qpolynomial_cow(qp
);
1742 qp
->poly
= isl_poly_mul_isl_int(qp
->poly
, v
);
1748 isl_qpolynomial_free(qp
);
1752 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1753 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1755 return isl_qpolynomial_mul_isl_int(qp
, v
);
1758 /* Multiply "qp" by "v".
1760 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1761 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1766 if (!isl_val_is_rat(v
))
1767 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1768 "expecting rational factor", goto error
);
1770 if (isl_val_is_one(v
)) {
1775 if (isl_val_is_zero(v
)) {
1778 space
= isl_qpolynomial_get_domain_space(qp
);
1779 isl_qpolynomial_free(qp
);
1781 return isl_qpolynomial_zero_on_domain(space
);
1784 qp
= isl_qpolynomial_cow(qp
);
1788 qp
->poly
= isl_poly_scale_val(qp
->poly
, v
);
1790 qp
= isl_qpolynomial_free(qp
);
1796 isl_qpolynomial_free(qp
);
1800 /* Divide "qp" by "v".
1802 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1803 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1808 if (!isl_val_is_rat(v
))
1809 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1810 "expecting rational factor", goto error
);
1811 if (isl_val_is_zero(v
))
1812 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1813 "cannot scale down by zero", goto error
);
1815 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1818 isl_qpolynomial_free(qp
);
1822 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1823 __isl_take isl_qpolynomial
*qp2
)
1825 isl_bool compatible
;
1827 qp1
= isl_qpolynomial_cow(qp1
);
1832 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1833 return isl_qpolynomial_mul(qp2
, qp1
);
1835 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1836 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1840 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1842 qp1
->poly
= isl_poly_mul(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1846 isl_qpolynomial_free(qp2
);
1850 isl_qpolynomial_free(qp1
);
1851 isl_qpolynomial_free(qp2
);
1855 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1858 qp
= isl_qpolynomial_cow(qp
);
1863 qp
->poly
= isl_poly_pow(qp
->poly
, power
);
1869 isl_qpolynomial_free(qp
);
1873 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1874 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1881 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1885 for (i
= 0; i
< pwqp
->n
; ++i
) {
1886 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1888 return isl_pw_qpolynomial_free(pwqp
);
1894 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1895 __isl_take isl_space
*domain
)
1899 return isl_qpolynomial_alloc(domain
, 0, isl_poly_zero(domain
->ctx
));
1902 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1903 __isl_take isl_space
*domain
)
1907 return isl_qpolynomial_alloc(domain
, 0, isl_poly_one(domain
->ctx
));
1910 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1911 __isl_take isl_space
*domain
)
1915 return isl_qpolynomial_alloc(domain
, 0, isl_poly_infty(domain
->ctx
));
1918 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1919 __isl_take isl_space
*domain
)
1923 return isl_qpolynomial_alloc(domain
, 0, isl_poly_neginfty(domain
->ctx
));
1926 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1927 __isl_take isl_space
*domain
)
1931 return isl_qpolynomial_alloc(domain
, 0, isl_poly_nan(domain
->ctx
));
1934 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1935 __isl_take isl_space
*domain
,
1938 struct isl_qpolynomial
*qp
;
1941 qp
= isl_qpolynomial_zero_on_domain(domain
);
1945 cst
= isl_poly_as_cst(qp
->poly
);
1946 isl_int_set(cst
->n
, v
);
1951 isl_bool
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1952 isl_int
*n
, isl_int
*d
)
1958 return isl_bool_error
;
1960 is_cst
= isl_poly_is_cst(qp
->poly
);
1961 if (is_cst
< 0 || !is_cst
)
1964 cst
= isl_poly_as_cst(qp
->poly
);
1966 return isl_bool_error
;
1969 isl_int_set(*n
, cst
->n
);
1971 isl_int_set(*d
, cst
->d
);
1973 return isl_bool_true
;
1976 /* Return the constant term of "poly".
1978 static __isl_give isl_val
*isl_poly_get_constant_val(__isl_keep isl_poly
*poly
)
1986 while ((is_cst
= isl_poly_is_cst(poly
)) == isl_bool_false
) {
1989 rec
= isl_poly_as_rec(poly
);
1997 cst
= isl_poly_as_cst(poly
);
2000 return isl_val_rat_from_isl_int(cst
->poly
.ctx
, cst
->n
, cst
->d
);
2003 /* Return the constant term of "qp".
2005 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
2006 __isl_keep isl_qpolynomial
*qp
)
2011 return isl_poly_get_constant_val(qp
->poly
);
2014 isl_bool
isl_poly_is_affine(__isl_keep isl_poly
*poly
)
2020 return isl_bool_error
;
2023 return isl_bool_true
;
2025 rec
= isl_poly_as_rec(poly
);
2027 return isl_bool_error
;
2030 return isl_bool_false
;
2032 isl_assert(poly
->ctx
, rec
->n
> 1, return isl_bool_error
);
2034 is_cst
= isl_poly_is_cst(rec
->p
[1]);
2035 if (is_cst
< 0 || !is_cst
)
2038 return isl_poly_is_affine(rec
->p
[0]);
2041 isl_bool
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
2044 return isl_bool_error
;
2046 if (qp
->div
->n_row
> 0)
2047 return isl_bool_false
;
2049 return isl_poly_is_affine(qp
->poly
);
2052 static void update_coeff(__isl_keep isl_vec
*aff
,
2053 __isl_keep isl_poly_cst
*cst
, int pos
)
2058 if (isl_int_is_zero(cst
->n
))
2063 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
2064 isl_int_divexact(f
, cst
->d
, gcd
);
2065 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
2066 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
2067 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
2072 int isl_poly_update_affine(__isl_keep isl_poly
*poly
, __isl_keep isl_vec
*aff
)
2080 if (poly
->var
< 0) {
2083 cst
= isl_poly_as_cst(poly
);
2086 update_coeff(aff
, cst
, 0);
2090 rec
= isl_poly_as_rec(poly
);
2093 isl_assert(poly
->ctx
, rec
->n
== 2, return -1);
2095 cst
= isl_poly_as_cst(rec
->p
[1]);
2098 update_coeff(aff
, cst
, 1 + poly
->var
);
2100 return isl_poly_update_affine(rec
->p
[0], aff
);
2103 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
2104 __isl_keep isl_qpolynomial
*qp
)
2112 d
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2113 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
2117 isl_seq_clr(aff
->el
+ 1, 1 + d
);
2118 isl_int_set_si(aff
->el
[0], 1);
2120 if (isl_poly_update_affine(qp
->poly
, aff
) < 0)
2129 /* Compare two quasi-polynomials.
2131 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2132 * than "qp2" and 0 if they are equal.
2134 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2135 __isl_keep isl_qpolynomial
*qp2
)
2146 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2150 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2154 return isl_poly_plain_cmp(qp1
->poly
, qp2
->poly
);
2157 /* Is "qp1" obviously equal to "qp2"?
2159 * NaN is not equal to anything, not even to another NaN.
2161 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2162 __isl_keep isl_qpolynomial
*qp2
)
2167 return isl_bool_error
;
2169 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2170 return isl_bool_false
;
2172 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2173 if (equal
< 0 || !equal
)
2176 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2177 if (equal
< 0 || !equal
)
2180 return isl_poly_is_equal(qp1
->poly
, qp2
->poly
);
2183 static isl_stat
poly_update_den(__isl_keep isl_poly
*poly
, isl_int
*d
)
2189 is_cst
= isl_poly_is_cst(poly
);
2191 return isl_stat_error
;
2194 cst
= isl_poly_as_cst(poly
);
2196 return isl_stat_error
;
2197 isl_int_lcm(*d
, *d
, cst
->d
);
2201 rec
= isl_poly_as_rec(poly
);
2203 return isl_stat_error
;
2205 for (i
= 0; i
< rec
->n
; ++i
)
2206 poly_update_den(rec
->p
[i
], d
);
2211 __isl_give isl_val
*isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
)
2217 d
= isl_val_one(isl_qpolynomial_get_ctx(qp
));
2220 if (poly_update_den(qp
->poly
, &d
->n
) < 0)
2221 return isl_val_free(d
);
2225 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2226 __isl_take isl_space
*domain
, int pos
, int power
)
2228 struct isl_ctx
*ctx
;
2235 return isl_qpolynomial_alloc(domain
, 0,
2236 isl_poly_var_pow(ctx
, pos
, power
));
2239 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2240 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2242 if (isl_space_check_is_set(domain
) < 0)
2244 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2247 if (type
== isl_dim_set
)
2248 pos
+= isl_space_dim(domain
, isl_dim_param
);
2250 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2252 isl_space_free(domain
);
2256 __isl_give isl_poly
*isl_poly_subs(__isl_take isl_poly
*poly
,
2257 unsigned first
, unsigned n
, __isl_keep isl_poly
**subs
)
2262 isl_poly
*base
, *res
;
2264 is_cst
= isl_poly_is_cst(poly
);
2266 return isl_poly_free(poly
);
2270 if (poly
->var
< first
)
2273 rec
= isl_poly_as_rec(poly
);
2277 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
2279 if (poly
->var
>= first
+ n
)
2280 base
= isl_poly_var_pow(poly
->ctx
, poly
->var
, 1);
2282 base
= isl_poly_copy(subs
[poly
->var
- first
]);
2284 res
= isl_poly_subs(isl_poly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2285 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2287 t
= isl_poly_subs(isl_poly_copy(rec
->p
[i
]), first
, n
, subs
);
2288 res
= isl_poly_mul(res
, isl_poly_copy(base
));
2289 res
= isl_poly_sum(res
, t
);
2292 isl_poly_free(base
);
2293 isl_poly_free(poly
);
2297 isl_poly_free(poly
);
2301 __isl_give isl_poly
*isl_poly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2302 isl_int denom
, unsigned len
)
2307 isl_assert(ctx
, len
>= 1, return NULL
);
2309 poly
= isl_poly_rat_cst(ctx
, f
[0], denom
);
2310 for (i
= 0; i
< len
- 1; ++i
) {
2314 if (isl_int_is_zero(f
[1 + i
]))
2317 c
= isl_poly_rat_cst(ctx
, f
[1 + i
], denom
);
2318 t
= isl_poly_var_pow(ctx
, i
, 1);
2319 t
= isl_poly_mul(c
, t
);
2320 poly
= isl_poly_sum(poly
, t
);
2326 /* Remove common factor of non-constant terms and denominator.
2328 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2330 isl_ctx
*ctx
= qp
->div
->ctx
;
2331 unsigned total
= qp
->div
->n_col
- 2;
2333 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2334 isl_int_gcd(ctx
->normalize_gcd
,
2335 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2336 if (isl_int_is_one(ctx
->normalize_gcd
))
2339 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2340 ctx
->normalize_gcd
, total
);
2341 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2342 ctx
->normalize_gcd
);
2343 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2344 ctx
->normalize_gcd
);
2347 /* Replace the integer division identified by "div" by the polynomial "s".
2348 * The integer division is assumed not to appear in the definition
2349 * of any other integer divisions.
2351 static __isl_give isl_qpolynomial
*substitute_div(
2352 __isl_take isl_qpolynomial
*qp
, int div
, __isl_take isl_poly
*s
)
2361 qp
= isl_qpolynomial_cow(qp
);
2365 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2366 qp
->poly
= isl_poly_subs(qp
->poly
, total
+ div
, 1, &s
);
2370 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2373 for (i
= 0; i
< total
+ div
; ++i
)
2375 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2376 reordering
[i
] = i
- 1;
2377 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2378 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2379 qp
->poly
= reorder(qp
->poly
, reordering
);
2382 if (!qp
->poly
|| !qp
->div
)
2388 isl_qpolynomial_free(qp
);
2393 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2394 * divisions because d is equal to 1 by their definition, i.e., e.
2396 static __isl_give isl_qpolynomial
*substitute_non_divs(
2397 __isl_take isl_qpolynomial
*qp
)
2406 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2407 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2408 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2410 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2411 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2413 isl_seq_combine(qp
->div
->row
[j
] + 1,
2414 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2415 qp
->div
->row
[j
][2 + total
+ i
],
2416 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2417 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2418 normalize_div(qp
, j
);
2420 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2421 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2422 qp
= substitute_div(qp
, i
, s
);
2429 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2430 * with d the denominator. When replacing the coefficient e of x by
2431 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2432 * inside the division, so we need to add floor(e/d) * x outside.
2433 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2434 * to adjust the coefficient of x in each later div that depends on the
2435 * current div "div" and also in the affine expressions in the rows of "mat"
2436 * (if they too depend on "div").
2438 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2439 __isl_keep isl_mat
**mat
)
2443 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2446 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2447 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2448 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2450 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2451 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2452 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2453 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2454 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2455 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2457 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2458 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2464 /* Check if the last non-zero coefficient is bigger that half of the
2465 * denominator. If so, we will invert the div to further reduce the number
2466 * of distinct divs that may appear.
2467 * If the last non-zero coefficient is exactly half the denominator,
2468 * then we continue looking for earlier coefficients that are bigger
2469 * than half the denominator.
2471 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2476 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2477 if (isl_int_is_zero(div
->row
[row
][i
]))
2479 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2480 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2481 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2491 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2492 * We only invert the coefficients of e (and the coefficient of q in
2493 * later divs and in the rows of "mat"). After calling this function, the
2494 * coefficients of e should be reduced again.
2496 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2497 __isl_keep isl_mat
**mat
)
2499 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2501 isl_seq_neg(qp
->div
->row
[div
] + 1,
2502 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2503 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2504 isl_int_add(qp
->div
->row
[div
][1],
2505 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2506 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2507 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2508 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2511 /* Reduce all divs of "qp" to have coefficients
2512 * in the interval [0, d-1], with d the denominator and such that the
2513 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2514 * The modifications to the integer divisions need to be reflected
2515 * in the factors of the polynomial that refer to the original
2516 * integer divisions. To this end, the modifications are collected
2517 * as a set of affine expressions and then plugged into the polynomial.
2519 * After the reduction, some divs may have become redundant or identical,
2520 * so we call substitute_non_divs and sort_divs. If these functions
2521 * eliminate divs or merge two or more divs into one, the coefficients
2522 * of the enclosing divs may have to be reduced again, so we call
2523 * ourselves recursively if the number of divs decreases.
2525 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2531 unsigned o_div
, n_div
, total
;
2536 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2537 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2538 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2539 ctx
= isl_qpolynomial_get_ctx(qp
);
2540 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2542 for (i
= 0; i
< n_div
; ++i
)
2543 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2545 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2546 normalize_div(qp
, i
);
2547 reduce_div(qp
, i
, &mat
);
2548 if (needs_invert(qp
->div
, i
)) {
2549 invert_div(qp
, i
, &mat
);
2550 reduce_div(qp
, i
, &mat
);
2556 s
= isl_alloc_array(ctx
, struct isl_poly
*, n_div
);
2559 for (i
= 0; i
< n_div
; ++i
)
2560 s
[i
] = isl_poly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2562 qp
->poly
= isl_poly_subs(qp
->poly
, o_div
- 1, n_div
, s
);
2563 for (i
= 0; i
< n_div
; ++i
)
2564 isl_poly_free(s
[i
]);
2571 qp
= substitute_non_divs(qp
);
2573 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2574 return reduce_divs(qp
);
2578 isl_qpolynomial_free(qp
);
2583 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2584 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2586 struct isl_qpolynomial
*qp
;
2589 qp
= isl_qpolynomial_zero_on_domain(domain
);
2593 cst
= isl_poly_as_cst(qp
->poly
);
2594 isl_int_set(cst
->n
, n
);
2595 isl_int_set(cst
->d
, d
);
2600 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2602 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2603 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2605 isl_qpolynomial
*qp
;
2608 qp
= isl_qpolynomial_zero_on_domain(domain
);
2612 cst
= isl_poly_as_cst(qp
->poly
);
2613 isl_int_set(cst
->n
, val
->n
);
2614 isl_int_set(cst
->d
, val
->d
);
2620 isl_qpolynomial_free(qp
);
2624 static isl_stat
poly_set_active(__isl_keep isl_poly
*poly
, int *active
, int d
)
2630 is_cst
= isl_poly_is_cst(poly
);
2632 return isl_stat_error
;
2637 active
[poly
->var
] = 1;
2639 rec
= isl_poly_as_rec(poly
);
2640 for (i
= 0; i
< rec
->n
; ++i
)
2641 if (poly_set_active(rec
->p
[i
], active
, d
) < 0)
2642 return isl_stat_error
;
2647 static isl_stat
set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2650 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2653 return isl_stat_error
;
2655 for (i
= 0; i
< d
; ++i
)
2656 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2657 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2663 return poly_set_active(qp
->poly
, active
, d
);
2667 #define TYPE isl_qpolynomial
2669 #include "check_type_range_templ.c"
2671 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2672 enum isl_dim_type type
, unsigned first
, unsigned n
)
2676 isl_bool involves
= isl_bool_false
;
2679 return isl_bool_error
;
2681 return isl_bool_false
;
2683 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2684 return isl_bool_error
;
2685 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2686 type
== isl_dim_in
, return isl_bool_error
);
2688 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2689 isl_space_dim(qp
->dim
, isl_dim_all
));
2690 if (set_active(qp
, active
) < 0)
2693 if (type
== isl_dim_in
)
2694 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2695 for (i
= 0; i
< n
; ++i
)
2696 if (active
[first
+ i
]) {
2697 involves
= isl_bool_true
;
2706 return isl_bool_error
;
2709 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2710 * of the divs that do appear in the quasi-polynomial.
2712 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2713 __isl_take isl_qpolynomial
*qp
)
2720 int *reordering
= NULL
;
2727 if (qp
->div
->n_row
== 0)
2730 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2731 len
= qp
->div
->n_col
- 2;
2732 ctx
= isl_qpolynomial_get_ctx(qp
);
2733 active
= isl_calloc_array(ctx
, int, len
);
2737 if (poly_set_active(qp
->poly
, active
, len
) < 0)
2740 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2741 if (!active
[d
+ i
]) {
2745 for (j
= 0; j
< i
; ++j
) {
2746 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2758 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2762 for (i
= 0; i
< d
; ++i
)
2766 n_div
= qp
->div
->n_row
;
2767 for (i
= 0; i
< n_div
; ++i
) {
2768 if (!active
[d
+ i
]) {
2769 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2770 qp
->div
= isl_mat_drop_cols(qp
->div
,
2771 2 + d
+ i
- skip
, 1);
2774 reordering
[d
+ i
] = d
+ i
- skip
;
2777 qp
->poly
= reorder(qp
->poly
, reordering
);
2779 if (!qp
->poly
|| !qp
->div
)
2789 isl_qpolynomial_free(qp
);
2793 __isl_give isl_poly
*isl_poly_drop(__isl_take isl_poly
*poly
,
2794 unsigned first
, unsigned n
)
2801 if (n
== 0 || poly
->var
< 0 || poly
->var
< first
)
2803 if (poly
->var
< first
+ n
) {
2804 poly
= replace_by_constant_term(poly
);
2805 return isl_poly_drop(poly
, first
, n
);
2807 poly
= isl_poly_cow(poly
);
2811 rec
= isl_poly_as_rec(poly
);
2815 for (i
= 0; i
< rec
->n
; ++i
) {
2816 rec
->p
[i
] = isl_poly_drop(rec
->p
[i
], first
, n
);
2823 isl_poly_free(poly
);
2827 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2828 __isl_take isl_qpolynomial
*qp
,
2829 enum isl_dim_type type
, unsigned pos
, const char *s
)
2831 qp
= isl_qpolynomial_cow(qp
);
2834 if (type
== isl_dim_out
)
2835 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2836 "cannot set name of output/set dimension",
2837 return isl_qpolynomial_free(qp
));
2838 type
= domain_type(type
);
2839 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2844 isl_qpolynomial_free(qp
);
2848 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2849 __isl_take isl_qpolynomial
*qp
,
2850 enum isl_dim_type type
, unsigned first
, unsigned n
)
2854 if (type
== isl_dim_out
)
2855 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2856 "cannot drop output/set dimension",
2858 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2859 return isl_qpolynomial_free(qp
);
2860 type
= domain_type(type
);
2861 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2864 qp
= isl_qpolynomial_cow(qp
);
2868 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2869 type
== isl_dim_set
, goto error
);
2871 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2875 if (type
== isl_dim_set
)
2876 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2878 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2882 qp
->poly
= isl_poly_drop(qp
->poly
, first
, n
);
2888 isl_qpolynomial_free(qp
);
2892 /* Project the domain of the quasi-polynomial onto its parameter space.
2893 * The quasi-polynomial may not involve any of the domain dimensions.
2895 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2896 __isl_take isl_qpolynomial
*qp
)
2902 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2903 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2905 return isl_qpolynomial_free(qp
);
2907 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2908 "polynomial involves some of the domain dimensions",
2909 return isl_qpolynomial_free(qp
));
2910 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2911 space
= isl_qpolynomial_get_domain_space(qp
);
2912 space
= isl_space_params(space
);
2913 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2917 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2918 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2928 if (eq
->n_eq
== 0) {
2929 isl_basic_set_free(eq
);
2933 qp
= isl_qpolynomial_cow(qp
);
2936 qp
->div
= isl_mat_cow(qp
->div
);
2940 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2942 isl_int_init(denom
);
2943 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2944 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2945 if (j
< 0 || j
== 0 || j
>= total
)
2948 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2949 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2951 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2952 &qp
->div
->row
[k
][0]);
2953 normalize_div(qp
, k
);
2956 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2957 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2958 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2959 isl_int_set_si(eq
->eq
[i
][j
], 0);
2961 poly
= isl_poly_from_affine(qp
->dim
->ctx
,
2962 eq
->eq
[i
], denom
, total
);
2963 qp
->poly
= isl_poly_subs(qp
->poly
, j
- 1, 1, &poly
);
2964 isl_poly_free(poly
);
2966 isl_int_clear(denom
);
2971 isl_basic_set_free(eq
);
2973 qp
= substitute_non_divs(qp
);
2978 isl_basic_set_free(eq
);
2979 isl_qpolynomial_free(qp
);
2983 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2985 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2986 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2990 if (qp
->div
->n_row
> 0)
2991 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2992 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2994 isl_basic_set_free(eq
);
2995 isl_qpolynomial_free(qp
);
2999 /* Look for equalities among the variables shared by context and qp
3000 * and the integer divisions of qp, if any.
3001 * The equalities are then used to eliminate variables and/or integer
3002 * divisions from qp.
3004 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
3005 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3007 isl_local_space
*ls
;
3010 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3011 context
= isl_local_space_lift_set(ls
, context
);
3013 aff
= isl_set_affine_hull(context
);
3014 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
3017 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
3018 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3020 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
3021 isl_set
*dom_context
= isl_set_universe(space
);
3022 dom_context
= isl_set_intersect_params(dom_context
, context
);
3023 return isl_qpolynomial_gist(qp
, dom_context
);
3026 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
3027 __isl_take isl_qpolynomial
*qp
)
3033 if (isl_qpolynomial_is_zero(qp
)) {
3034 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
3035 isl_qpolynomial_free(qp
);
3036 return isl_pw_qpolynomial_zero(dim
);
3039 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
3040 return isl_pw_qpolynomial_alloc(dom
, qp
);
3043 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3046 #define PW isl_pw_qpolynomial
3048 #define EL isl_qpolynomial
3050 #define EL_IS_ZERO is_zero
3054 #define IS_ZERO is_zero
3057 #undef DEFAULT_IS_ZERO
3058 #define DEFAULT_IS_ZERO 1
3062 #include <isl_pw_templ.c>
3063 #include <isl_pw_eval.c>
3066 #define BASE pw_qpolynomial
3068 #include <isl_union_single.c>
3069 #include <isl_union_eval.c>
3070 #include <isl_union_neg.c>
3072 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
3080 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
3083 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
3086 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
3087 __isl_take isl_pw_qpolynomial
*pwqp1
,
3088 __isl_take isl_pw_qpolynomial
*pwqp2
)
3090 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
3093 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
3094 __isl_take isl_pw_qpolynomial
*pwqp1
,
3095 __isl_take isl_pw_qpolynomial
*pwqp2
)
3098 struct isl_pw_qpolynomial
*res
;
3100 if (!pwqp1
|| !pwqp2
)
3103 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3106 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3107 isl_pw_qpolynomial_free(pwqp2
);
3111 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3112 isl_pw_qpolynomial_free(pwqp1
);
3116 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3117 isl_pw_qpolynomial_free(pwqp1
);
3121 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3122 isl_pw_qpolynomial_free(pwqp2
);
3126 n
= pwqp1
->n
* pwqp2
->n
;
3127 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3129 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3130 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3131 struct isl_set
*common
;
3132 struct isl_qpolynomial
*prod
;
3133 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3134 isl_set_copy(pwqp2
->p
[j
].set
));
3135 if (isl_set_plain_is_empty(common
)) {
3136 isl_set_free(common
);
3140 prod
= isl_qpolynomial_mul(
3141 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3142 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3144 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3148 isl_pw_qpolynomial_free(pwqp1
);
3149 isl_pw_qpolynomial_free(pwqp2
);
3153 isl_pw_qpolynomial_free(pwqp1
);
3154 isl_pw_qpolynomial_free(pwqp2
);
3158 __isl_give isl_val
*isl_poly_eval(__isl_take isl_poly
*poly
,
3159 __isl_take isl_vec
*vec
)
3167 is_cst
= isl_poly_is_cst(poly
);
3172 res
= isl_poly_get_constant_val(poly
);
3173 isl_poly_free(poly
);
3177 rec
= isl_poly_as_rec(poly
);
3181 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
3183 base
= isl_val_rat_from_isl_int(poly
->ctx
,
3184 vec
->el
[1 + poly
->var
], vec
->el
[0]);
3186 res
= isl_poly_eval(isl_poly_copy(rec
->p
[rec
->n
- 1]),
3189 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3190 res
= isl_val_mul(res
, isl_val_copy(base
));
3191 res
= isl_val_add(res
, isl_poly_eval(isl_poly_copy(rec
->p
[i
]),
3192 isl_vec_copy(vec
)));
3196 isl_poly_free(poly
);
3200 isl_poly_free(poly
);
3205 /* Evaluate "qp" in the void point "pnt".
3206 * In particular, return the value NaN.
3208 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3209 __isl_take isl_point
*pnt
)
3213 ctx
= isl_point_get_ctx(pnt
);
3214 isl_qpolynomial_free(qp
);
3215 isl_point_free(pnt
);
3216 return isl_val_nan(ctx
);
3219 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3220 __isl_take isl_point
*pnt
)
3228 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3229 is_void
= isl_point_is_void(pnt
);
3233 return eval_void(qp
, pnt
);
3235 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3237 v
= isl_poly_eval(isl_poly_copy(qp
->poly
), ext
);
3239 isl_qpolynomial_free(qp
);
3240 isl_point_free(pnt
);
3244 isl_qpolynomial_free(qp
);
3245 isl_point_free(pnt
);
3249 int isl_poly_cmp(__isl_keep isl_poly_cst
*cst1
, __isl_keep isl_poly_cst
*cst2
)
3254 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3255 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3256 cmp
= isl_int_sgn(t
);
3261 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3262 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3263 unsigned first
, unsigned n
)
3271 if (type
== isl_dim_out
)
3272 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3273 "cannot insert output/set dimensions",
3275 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3276 return isl_qpolynomial_free(qp
);
3277 type
= domain_type(type
);
3278 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3281 qp
= isl_qpolynomial_cow(qp
);
3285 g_pos
= pos(qp
->dim
, type
) + first
;
3287 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3291 total
= qp
->div
->n_col
- 2;
3292 if (total
> g_pos
) {
3294 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3297 for (i
= 0; i
< total
- g_pos
; ++i
)
3299 qp
->poly
= expand(qp
->poly
, exp
, g_pos
);
3305 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3311 isl_qpolynomial_free(qp
);
3315 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3316 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3320 pos
= isl_qpolynomial_dim(qp
, type
);
3322 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3325 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3326 __isl_take isl_pw_qpolynomial
*pwqp
,
3327 enum isl_dim_type type
, unsigned n
)
3331 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3333 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3336 static int *reordering_move(isl_ctx
*ctx
,
3337 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3342 reordering
= isl_alloc_array(ctx
, int, len
);
3347 for (i
= 0; i
< dst
; ++i
)
3349 for (i
= 0; i
< n
; ++i
)
3350 reordering
[src
+ i
] = dst
+ i
;
3351 for (i
= 0; i
< src
- dst
; ++i
)
3352 reordering
[dst
+ i
] = dst
+ n
+ i
;
3353 for (i
= 0; i
< len
- src
- n
; ++i
)
3354 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3356 for (i
= 0; i
< src
; ++i
)
3358 for (i
= 0; i
< n
; ++i
)
3359 reordering
[src
+ i
] = dst
+ i
;
3360 for (i
= 0; i
< dst
- src
; ++i
)
3361 reordering
[src
+ n
+ i
] = src
+ i
;
3362 for (i
= 0; i
< len
- dst
- n
; ++i
)
3363 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3369 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3370 __isl_take isl_qpolynomial
*qp
,
3371 enum isl_dim_type dst_type
, unsigned dst_pos
,
3372 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3381 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3382 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3383 "cannot move output/set dimension",
3385 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3386 return isl_qpolynomial_free(qp
);
3387 if (dst_type
== isl_dim_in
)
3388 dst_type
= isl_dim_set
;
3389 if (src_type
== isl_dim_in
)
3390 src_type
= isl_dim_set
;
3393 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3394 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3397 qp
= isl_qpolynomial_cow(qp
);
3401 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3402 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3403 if (dst_type
> src_type
)
3406 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3413 reordering
= reordering_move(qp
->dim
->ctx
,
3414 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3418 qp
->poly
= reorder(qp
->poly
, reordering
);
3423 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3429 isl_qpolynomial_free(qp
);
3433 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3434 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3438 space
= isl_space_domain(space
);
3442 poly
= isl_poly_from_affine(space
->ctx
, f
, denom
,
3443 1 + isl_space_dim(space
, isl_dim_all
));
3445 return isl_qpolynomial_alloc(space
, 0, poly
);
3448 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3452 isl_qpolynomial
*qp
;
3457 ctx
= isl_aff_get_ctx(aff
);
3458 poly
= isl_poly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3461 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3462 aff
->ls
->div
->n_row
, poly
);
3466 isl_mat_free(qp
->div
);
3467 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3468 qp
->div
= isl_mat_cow(qp
->div
);
3473 qp
= reduce_divs(qp
);
3474 qp
= remove_redundant_divs(qp
);
3478 return isl_qpolynomial_free(qp
);
3481 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3482 __isl_take isl_pw_aff
*pwaff
)
3485 isl_pw_qpolynomial
*pwqp
;
3490 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3493 for (i
= 0; i
< pwaff
->n
; ++i
) {
3495 isl_qpolynomial
*qp
;
3497 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3498 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3499 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3502 isl_pw_aff_free(pwaff
);
3506 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3507 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3511 aff
= isl_constraint_get_bound(c
, type
, pos
);
3512 isl_constraint_free(c
);
3513 return isl_qpolynomial_from_aff(aff
);
3516 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3517 * in "qp" by subs[i].
3519 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3520 __isl_take isl_qpolynomial
*qp
,
3521 enum isl_dim_type type
, unsigned first
, unsigned n
,
3522 __isl_keep isl_qpolynomial
**subs
)
3530 qp
= isl_qpolynomial_cow(qp
);
3534 if (type
== isl_dim_out
)
3535 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3536 "cannot substitute output/set dimension",
3538 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3539 return isl_qpolynomial_free(qp
);
3540 type
= domain_type(type
);
3542 for (i
= 0; i
< n
; ++i
)
3546 for (i
= 0; i
< n
; ++i
)
3547 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3550 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3551 for (i
= 0; i
< n
; ++i
)
3552 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3554 first
+= pos(qp
->dim
, type
);
3556 polys
= isl_alloc_array(qp
->dim
->ctx
, struct isl_poly
*, n
);
3559 for (i
= 0; i
< n
; ++i
)
3560 polys
[i
] = subs
[i
]->poly
;
3562 qp
->poly
= isl_poly_subs(qp
->poly
, first
, n
, polys
);
3571 isl_qpolynomial_free(qp
);
3575 /* Extend "bset" with extra set dimensions for each integer division
3576 * in "qp" and then call "fn" with the extended bset and the polynomial
3577 * that results from replacing each of the integer divisions by the
3578 * corresponding extra set dimension.
3580 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3581 __isl_keep isl_basic_set
*bset
,
3582 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3583 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3586 isl_local_space
*ls
;
3587 isl_qpolynomial
*poly
;
3590 return isl_stat_error
;
3591 if (qp
->div
->n_row
== 0)
3592 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3595 space
= isl_space_copy(qp
->dim
);
3596 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3597 poly
= isl_qpolynomial_alloc(space
, 0, isl_poly_copy(qp
->poly
));
3598 bset
= isl_basic_set_copy(bset
);
3599 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3600 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3602 return fn(bset
, poly
, user
);
3605 /* Return total degree in variables first (inclusive) up to last (exclusive).
3607 int isl_poly_degree(__isl_keep isl_poly
*poly
, int first
, int last
)
3611 isl_bool is_zero
, is_cst
;
3614 is_zero
= isl_poly_is_zero(poly
);
3619 is_cst
= isl_poly_is_cst(poly
);
3622 if (is_cst
|| poly
->var
< first
)
3625 rec
= isl_poly_as_rec(poly
);
3629 for (i
= 0; i
< rec
->n
; ++i
) {
3632 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3637 d
= isl_poly_degree(rec
->p
[i
], first
, last
);
3638 if (poly
->var
< last
)
3647 /* Return total degree in set variables.
3649 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3657 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3658 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3659 return isl_poly_degree(poly
->poly
, ovar
, ovar
+ nvar
);
3662 __isl_give isl_poly
*isl_poly_coeff(__isl_keep isl_poly
*poly
,
3663 unsigned pos
, int deg
)
3669 is_cst
= isl_poly_is_cst(poly
);
3672 if (is_cst
|| poly
->var
< pos
) {
3674 return isl_poly_copy(poly
);
3676 return isl_poly_zero(poly
->ctx
);
3679 rec
= isl_poly_as_rec(poly
);
3683 if (poly
->var
== pos
) {
3685 return isl_poly_copy(rec
->p
[deg
]);
3687 return isl_poly_zero(poly
->ctx
);
3690 poly
= isl_poly_copy(poly
);
3691 poly
= isl_poly_cow(poly
);
3692 rec
= isl_poly_as_rec(poly
);
3696 for (i
= 0; i
< rec
->n
; ++i
) {
3698 t
= isl_poly_coeff(rec
->p
[i
], pos
, deg
);
3701 isl_poly_free(rec
->p
[i
]);
3707 isl_poly_free(poly
);
3711 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3713 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3714 __isl_keep isl_qpolynomial
*qp
,
3715 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3724 if (type
== isl_dim_out
)
3725 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3726 "output/set dimension does not have a coefficient",
3728 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
3730 type
= domain_type(type
);
3732 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3733 poly
= isl_poly_coeff(qp
->poly
, g_pos
, deg
);
3735 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
),
3736 qp
->div
->n_row
, poly
);
3739 isl_mat_free(c
->div
);
3740 c
->div
= isl_mat_copy(qp
->div
);
3745 isl_qpolynomial_free(c
);
3749 /* Homogenize the polynomial in the variables first (inclusive) up to
3750 * last (exclusive) by inserting powers of variable first.
3751 * Variable first is assumed not to appear in the input.
3753 __isl_give isl_poly
*isl_poly_homogenize(__isl_take isl_poly
*poly
, int deg
,
3754 int target
, int first
, int last
)
3757 isl_bool is_zero
, is_cst
;
3760 is_zero
= isl_poly_is_zero(poly
);
3762 return isl_poly_free(poly
);
3767 is_cst
= isl_poly_is_cst(poly
);
3769 return isl_poly_free(poly
);
3770 if (is_cst
|| poly
->var
< first
) {
3773 hom
= isl_poly_var_pow(poly
->ctx
, first
, target
- deg
);
3776 rec
= isl_poly_as_rec(hom
);
3777 rec
->p
[target
- deg
] = isl_poly_mul(rec
->p
[target
- deg
], poly
);
3782 poly
= isl_poly_cow(poly
);
3783 rec
= isl_poly_as_rec(poly
);
3787 for (i
= 0; i
< rec
->n
; ++i
) {
3788 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3790 return isl_poly_free(poly
);
3793 rec
->p
[i
] = isl_poly_homogenize(rec
->p
[i
],
3794 poly
->var
< last
? deg
+ i
: i
, target
,
3802 isl_poly_free(poly
);
3806 /* Homogenize the polynomial in the set variables by introducing
3807 * powers of an extra set variable at position 0.
3809 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3810 __isl_take isl_qpolynomial
*poly
)
3814 int deg
= isl_qpolynomial_degree(poly
);
3819 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3820 poly
= isl_qpolynomial_cow(poly
);
3824 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3825 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3826 poly
->poly
= isl_poly_homogenize(poly
->poly
, 0, deg
, ovar
, ovar
+ nvar
);
3832 isl_qpolynomial_free(poly
);
3836 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3837 __isl_take isl_mat
*div
)
3845 n
= isl_space_dim(space
, isl_dim_all
) + div
->n_row
;
3847 term
= isl_calloc(space
->ctx
, struct isl_term
,
3848 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3855 isl_int_init(term
->n
);
3856 isl_int_init(term
->d
);
3860 isl_space_free(space
);
3865 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3874 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3883 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3885 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3889 isl_int_set(dup
->n
, term
->n
);
3890 isl_int_set(dup
->d
, term
->d
);
3892 for (i
= 0; i
< total
; ++i
)
3893 dup
->pow
[i
] = term
->pow
[i
];
3898 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3906 return isl_term_dup(term
);
3909 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3914 if (--term
->ref
> 0)
3917 isl_space_free(term
->dim
);
3918 isl_mat_free(term
->div
);
3919 isl_int_clear(term
->n
);
3920 isl_int_clear(term
->d
);
3926 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3934 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3935 case isl_dim_div
: return term
->div
->n_row
;
3936 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3942 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3944 return term
? term
->dim
->ctx
: NULL
;
3947 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3951 isl_int_set(*n
, term
->n
);
3954 /* Return the coefficient of the term "term".
3956 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3961 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3966 #define TYPE isl_term
3968 #include "check_type_range_templ.c"
3970 int isl_term_get_exp(__isl_keep isl_term
*term
,
3971 enum isl_dim_type type
, unsigned pos
)
3973 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
3976 if (type
>= isl_dim_set
)
3977 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3978 if (type
>= isl_dim_div
)
3979 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3981 return term
->pow
[pos
];
3984 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3986 isl_local_space
*ls
;
3989 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
3992 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3993 isl_mat_copy(term
->div
));
3994 aff
= isl_aff_alloc(ls
);
3998 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
4000 aff
= isl_aff_normalize(aff
);
4005 __isl_give isl_term
*isl_poly_foreach_term(__isl_keep isl_poly
*poly
,
4006 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
4007 __isl_take isl_term
*term
, void *user
)
4010 isl_bool is_zero
, is_bad
, is_cst
;
4013 is_zero
= isl_poly_is_zero(poly
);
4014 if (is_zero
< 0 || !term
)
4020 is_cst
= isl_poly_is_cst(poly
);
4021 is_bad
= isl_poly_is_nan(poly
);
4022 if (is_bad
>= 0 && !is_bad
)
4023 is_bad
= isl_poly_is_infty(poly
);
4024 if (is_bad
>= 0 && !is_bad
)
4025 is_bad
= isl_poly_is_neginfty(poly
);
4026 if (is_cst
< 0 || is_bad
< 0)
4027 return isl_term_free(term
);
4029 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4030 "cannot handle NaN/infty polynomial",
4031 return isl_term_free(term
));
4035 cst
= isl_poly_as_cst(poly
);
4038 term
= isl_term_cow(term
);
4041 isl_int_set(term
->n
, cst
->n
);
4042 isl_int_set(term
->d
, cst
->d
);
4043 if (fn(isl_term_copy(term
), user
) < 0)
4048 rec
= isl_poly_as_rec(poly
);
4052 for (i
= 0; i
< rec
->n
; ++i
) {
4053 term
= isl_term_cow(term
);
4056 term
->pow
[poly
->var
] = i
;
4057 term
= isl_poly_foreach_term(rec
->p
[i
], fn
, term
, user
);
4061 term
->pow
[poly
->var
] = 0;
4065 isl_term_free(term
);
4069 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
4070 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
4075 return isl_stat_error
;
4077 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
4079 return isl_stat_error
;
4081 term
= isl_poly_foreach_term(qp
->poly
, fn
, term
, user
);
4083 isl_term_free(term
);
4085 return term
? isl_stat_ok
: isl_stat_error
;
4088 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
4091 isl_qpolynomial
*qp
;
4097 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
4099 poly
= isl_poly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4100 for (i
= 0; i
< n
; ++i
) {
4103 poly
= isl_poly_mul(poly
,
4104 isl_poly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4107 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
),
4108 term
->div
->n_row
, poly
);
4111 isl_mat_free(qp
->div
);
4112 qp
->div
= isl_mat_copy(term
->div
);
4116 isl_term_free(term
);
4119 isl_qpolynomial_free(qp
);
4120 isl_term_free(term
);
4124 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4125 __isl_take isl_space
*space
)
4134 if (isl_space_is_equal(qp
->dim
, space
)) {
4135 isl_space_free(space
);
4139 qp
= isl_qpolynomial_cow(qp
);
4143 extra
= isl_space_dim(space
, isl_dim_set
) -
4144 isl_space_dim(qp
->dim
, isl_dim_set
);
4145 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4146 if (qp
->div
->n_row
) {
4149 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4152 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4154 qp
->poly
= expand(qp
->poly
, exp
, total
);
4159 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4162 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4163 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4165 isl_space_free(qp
->dim
);
4170 isl_space_free(space
);
4171 isl_qpolynomial_free(qp
);
4175 /* For each parameter or variable that does not appear in qp,
4176 * first eliminate the variable from all constraints and then set it to zero.
4178 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4179 __isl_keep isl_qpolynomial
*qp
)
4190 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4191 active
= isl_calloc_array(set
->ctx
, int, d
);
4192 if (set_active(qp
, active
) < 0)
4195 for (i
= 0; i
< d
; ++i
)
4204 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4205 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4206 for (i
= 0; i
< nparam
; ++i
) {
4209 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4210 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4212 for (i
= 0; i
< nvar
; ++i
) {
4213 if (active
[nparam
+ i
])
4215 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4216 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4228 struct isl_opt_data
{
4229 isl_qpolynomial
*qp
;
4235 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4237 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4240 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4244 } else if (data
->max
) {
4245 data
->opt
= isl_val_max(data
->opt
, val
);
4247 data
->opt
= isl_val_min(data
->opt
, val
);
4253 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4254 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4256 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4262 is_cst
= isl_poly_is_cst(qp
->poly
);
4267 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4268 isl_qpolynomial_free(qp
);
4272 set
= fix_inactive(set
, qp
);
4275 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4279 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4282 isl_qpolynomial_free(qp
);
4286 isl_qpolynomial_free(qp
);
4287 isl_val_free(data
.opt
);
4291 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4292 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4298 isl_mat
*mat
, *diag
;
4300 qp
= isl_qpolynomial_cow(qp
);
4305 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4307 n_sub
= morph
->inv
->n_row
- 1;
4308 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4309 n_sub
+= qp
->div
->n_row
;
4310 subs
= isl_calloc_array(ctx
, struct isl_poly
*, n_sub
);
4314 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4315 subs
[i
] = isl_poly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4316 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4317 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4318 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4319 subs
[morph
->inv
->n_row
- 1 + i
] =
4320 isl_poly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4322 qp
->poly
= isl_poly_subs(qp
->poly
, 0, n_sub
, subs
);
4324 for (i
= 0; i
< n_sub
; ++i
)
4325 isl_poly_free(subs
[i
]);
4328 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4329 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4330 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4331 mat
= isl_mat_diagonal(mat
, diag
);
4332 qp
->div
= isl_mat_product(qp
->div
, mat
);
4333 isl_space_free(qp
->dim
);
4334 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4336 if (!qp
->poly
|| !qp
->div
|| !qp
->dim
)
4339 isl_morph_free(morph
);
4343 isl_qpolynomial_free(qp
);
4344 isl_morph_free(morph
);
4348 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4349 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4350 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4352 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4353 &isl_pw_qpolynomial_mul
);
4356 /* Reorder the dimension of "qp" according to the given reordering.
4358 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4359 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4363 qp
= isl_qpolynomial_cow(qp
);
4367 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4371 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4375 qp
->poly
= reorder(qp
->poly
, r
->pos
);
4379 space
= isl_reordering_get_space(r
);
4380 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4382 isl_reordering_free(r
);
4385 isl_qpolynomial_free(qp
);
4386 isl_reordering_free(r
);
4390 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4391 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4393 isl_bool equal_params
;
4398 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4399 if (equal_params
< 0)
4401 if (!equal_params
) {
4402 isl_reordering
*exp
;
4404 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4405 exp
= isl_reordering_extend_space(exp
,
4406 isl_qpolynomial_get_domain_space(qp
));
4407 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4410 isl_space_free(model
);
4413 isl_space_free(model
);
4414 isl_qpolynomial_free(qp
);
4418 struct isl_split_periods_data
{
4420 isl_pw_qpolynomial
*res
;
4423 /* Create a slice where the integer division "div" has the fixed value "v".
4424 * In particular, if "div" refers to floor(f/m), then create a slice
4426 * m v <= f <= m v + (m - 1)
4431 * -f + m v + (m - 1) >= 0
4433 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4434 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4437 isl_basic_set
*bset
= NULL
;
4443 total
= isl_space_dim(space
, isl_dim_all
);
4444 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4446 k
= isl_basic_set_alloc_inequality(bset
);
4449 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4450 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4452 k
= isl_basic_set_alloc_inequality(bset
);
4455 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4456 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4457 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4458 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4460 isl_space_free(space
);
4461 return isl_set_from_basic_set(bset
);
4463 isl_basic_set_free(bset
);
4464 isl_space_free(space
);
4468 static isl_stat
split_periods(__isl_take isl_set
*set
,
4469 __isl_take isl_qpolynomial
*qp
, void *user
);
4471 /* Create a slice of the domain "set" such that integer division "div"
4472 * has the fixed value "v" and add the results to data->res,
4473 * replacing the integer division by "v" in "qp".
4475 static isl_stat
set_div(__isl_take isl_set
*set
,
4476 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4477 struct isl_split_periods_data
*data
)
4484 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4485 set
= isl_set_intersect(set
, slice
);
4490 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4492 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4493 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4495 isl_int_addmul(qp
->div
->row
[i
][1],
4496 qp
->div
->row
[i
][2 + total
+ div
], v
);
4497 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4500 cst
= isl_poly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4501 qp
= substitute_div(qp
, div
, cst
);
4503 return split_periods(set
, qp
, data
);
4506 isl_qpolynomial_free(qp
);
4507 return isl_stat_error
;
4510 /* Split the domain "set" such that integer division "div"
4511 * has a fixed value (ranging from "min" to "max") on each slice
4512 * and add the results to data->res.
4514 static isl_stat
split_div(__isl_take isl_set
*set
,
4515 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4516 struct isl_split_periods_data
*data
)
4518 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4519 isl_set
*set_i
= isl_set_copy(set
);
4520 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4522 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4526 isl_qpolynomial_free(qp
);
4530 isl_qpolynomial_free(qp
);
4531 return isl_stat_error
;
4534 /* If "qp" refers to any integer division
4535 * that can only attain "max_periods" distinct values on "set"
4536 * then split the domain along those distinct values.
4537 * Add the results (or the original if no splitting occurs)
4540 static isl_stat
split_periods(__isl_take isl_set
*set
,
4541 __isl_take isl_qpolynomial
*qp
, void *user
)
4544 isl_pw_qpolynomial
*pwqp
;
4545 struct isl_split_periods_data
*data
;
4548 isl_stat r
= isl_stat_ok
;
4550 data
= (struct isl_split_periods_data
*)user
;
4555 if (qp
->div
->n_row
== 0) {
4556 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4557 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4563 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4564 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4565 enum isl_lp_result lp_res
;
4567 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4568 qp
->div
->n_row
) != -1)
4571 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4572 set
->ctx
->one
, &min
, NULL
, NULL
);
4573 if (lp_res
== isl_lp_error
)
4575 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4577 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4579 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4580 set
->ctx
->one
, &max
, NULL
, NULL
);
4581 if (lp_res
== isl_lp_error
)
4583 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4585 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4587 isl_int_sub(max
, max
, min
);
4588 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4589 isl_int_add(max
, max
, min
);
4594 if (i
< qp
->div
->n_row
) {
4595 r
= split_div(set
, qp
, i
, min
, max
, data
);
4597 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4598 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4610 isl_qpolynomial_free(qp
);
4611 return isl_stat_error
;
4614 /* If any quasi-polynomial in pwqp refers to any integer division
4615 * that can only attain "max_periods" distinct values on its domain
4616 * then split the domain along those distinct values.
4618 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4619 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4621 struct isl_split_periods_data data
;
4623 data
.max_periods
= max_periods
;
4624 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4626 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4629 isl_pw_qpolynomial_free(pwqp
);
4633 isl_pw_qpolynomial_free(data
.res
);
4634 isl_pw_qpolynomial_free(pwqp
);
4638 /* Construct a piecewise quasipolynomial that is constant on the given
4639 * domain. In particular, it is
4642 * infinity if cst == -1
4644 * If cst == -1, then explicitly check whether the domain is empty and,
4645 * if so, return 0 instead.
4647 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4648 __isl_take isl_basic_set
*bset
, int cst
)
4651 isl_qpolynomial
*qp
;
4653 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4658 bset
= isl_basic_set_params(bset
);
4659 dim
= isl_basic_set_get_space(bset
);
4661 qp
= isl_qpolynomial_infty_on_domain(dim
);
4663 qp
= isl_qpolynomial_zero_on_domain(dim
);
4665 qp
= isl_qpolynomial_one_on_domain(dim
);
4666 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4669 /* Factor bset, call fn on each of the factors and return the product.
4671 * If no factors can be found, simply call fn on the input.
4672 * Otherwise, construct the factors based on the factorizer,
4673 * call fn on each factor and compute the product.
4675 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4676 __isl_take isl_basic_set
*bset
,
4677 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4683 isl_qpolynomial
*qp
;
4684 isl_pw_qpolynomial
*pwqp
;
4688 f
= isl_basic_set_factorizer(bset
);
4691 if (f
->n_group
== 0) {
4692 isl_factorizer_free(f
);
4696 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4697 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4699 space
= isl_basic_set_get_space(bset
);
4700 space
= isl_space_params(space
);
4701 set
= isl_set_universe(isl_space_copy(space
));
4702 qp
= isl_qpolynomial_one_on_domain(space
);
4703 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4705 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4707 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4708 isl_basic_set
*bset_i
;
4709 isl_pw_qpolynomial
*pwqp_i
;
4711 bset_i
= isl_basic_set_copy(bset
);
4712 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4713 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4714 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4716 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4717 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4718 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4720 pwqp_i
= fn(bset_i
);
4721 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4726 isl_basic_set_free(bset
);
4727 isl_factorizer_free(f
);
4731 isl_basic_set_free(bset
);
4735 /* Factor bset, call fn on each of the factors and return the product.
4736 * The function is assumed to evaluate to zero on empty domains,
4737 * to one on zero-dimensional domains and to infinity on unbounded domains
4738 * and will not be called explicitly on zero-dimensional or unbounded domains.
4740 * We first check for some special cases and remove all equalities.
4741 * Then we hand over control to compressed_multiplicative_call.
4743 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4744 __isl_take isl_basic_set
*bset
,
4745 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4749 isl_pw_qpolynomial
*pwqp
;
4754 if (isl_basic_set_plain_is_empty(bset
))
4755 return constant_on_domain(bset
, 0);
4757 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4758 return constant_on_domain(bset
, 1);
4760 bounded
= isl_basic_set_is_bounded(bset
);
4764 return constant_on_domain(bset
, -1);
4766 if (bset
->n_eq
== 0)
4767 return compressed_multiplicative_call(bset
, fn
);
4769 morph
= isl_basic_set_full_compression(bset
);
4770 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4772 pwqp
= compressed_multiplicative_call(bset
, fn
);
4774 morph
= isl_morph_dom_params(morph
);
4775 morph
= isl_morph_ran_params(morph
);
4776 morph
= isl_morph_inverse(morph
);
4778 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4782 isl_basic_set_free(bset
);
4786 /* Drop all floors in "qp", turning each integer division [a/m] into
4787 * a rational division a/m. If "down" is set, then the integer division
4788 * is replaced by (a-(m-1))/m instead.
4790 static __isl_give isl_qpolynomial
*qp_drop_floors(
4791 __isl_take isl_qpolynomial
*qp
, int down
)
4798 if (qp
->div
->n_row
== 0)
4801 qp
= isl_qpolynomial_cow(qp
);
4805 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4807 isl_int_sub(qp
->div
->row
[i
][1],
4808 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4809 isl_int_add_ui(qp
->div
->row
[i
][1],
4810 qp
->div
->row
[i
][1], 1);
4812 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4813 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4814 qp
= substitute_div(qp
, i
, s
);
4822 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4823 * a rational division a/m.
4825 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4826 __isl_take isl_pw_qpolynomial
*pwqp
)
4833 if (isl_pw_qpolynomial_is_zero(pwqp
))
4836 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4840 for (i
= 0; i
< pwqp
->n
; ++i
) {
4841 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4848 isl_pw_qpolynomial_free(pwqp
);
4852 /* Adjust all the integer divisions in "qp" such that they are at least
4853 * one over the given orthant (identified by "signs"). This ensures
4854 * that they will still be non-negative even after subtracting (m-1)/m.
4856 * In particular, f is replaced by f' + v, changing f = [a/m]
4857 * to f' = [(a - m v)/m].
4858 * If the constant term k in a is smaller than m,
4859 * the constant term of v is set to floor(k/m) - 1.
4860 * For any other term, if the coefficient c and the variable x have
4861 * the same sign, then no changes are needed.
4862 * Otherwise, if the variable is positive (and c is negative),
4863 * then the coefficient of x in v is set to floor(c/m).
4864 * If the variable is negative (and c is positive),
4865 * then the coefficient of x in v is set to ceil(c/m).
4867 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4875 qp
= isl_qpolynomial_cow(qp
);
4878 qp
->div
= isl_mat_cow(qp
->div
);
4882 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4883 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4885 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4886 isl_int
*row
= qp
->div
->row
[i
];
4890 if (isl_int_lt(row
[1], row
[0])) {
4891 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4892 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4893 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4895 for (j
= 0; j
< total
; ++j
) {
4896 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4899 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4901 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4902 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4904 for (j
= 0; j
< i
; ++j
) {
4905 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4907 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4908 row
[2 + total
+ j
], row
[0]);
4909 isl_int_submul(row
[2 + total
+ j
],
4910 row
[0], v
->el
[1 + total
+ j
]);
4912 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4913 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4915 isl_seq_combine(qp
->div
->row
[j
] + 1,
4916 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4917 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4919 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4920 s
= isl_poly_from_affine(qp
->dim
->ctx
, v
->el
,
4921 qp
->div
->ctx
->one
, v
->size
);
4922 qp
->poly
= isl_poly_subs(qp
->poly
, total
+ i
, 1, &s
);
4932 isl_qpolynomial_free(qp
);
4936 struct isl_to_poly_data
{
4938 isl_pw_qpolynomial
*res
;
4939 isl_qpolynomial
*qp
;
4942 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4943 * We first make all integer divisions positive and then split the
4944 * quasipolynomials into terms with sign data->sign (the direction
4945 * of the requested approximation) and terms with the opposite sign.
4946 * In the first set of terms, each integer division [a/m] is
4947 * overapproximated by a/m, while in the second it is underapproximated
4950 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4951 int *signs
, void *user
)
4953 struct isl_to_poly_data
*data
= user
;
4954 isl_pw_qpolynomial
*t
;
4955 isl_qpolynomial
*qp
, *up
, *down
;
4957 qp
= isl_qpolynomial_copy(data
->qp
);
4958 qp
= make_divs_pos(qp
, signs
);
4960 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4961 up
= qp_drop_floors(up
, 0);
4962 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4963 down
= qp_drop_floors(down
, 1);
4965 isl_qpolynomial_free(qp
);
4966 qp
= isl_qpolynomial_add(up
, down
);
4968 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4969 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4974 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4975 * the polynomial will be an overapproximation. If "sign" is negative,
4976 * it will be an underapproximation. If "sign" is zero, the approximation
4977 * will lie somewhere in between.
4979 * In particular, is sign == 0, we simply drop the floors, turning
4980 * the integer divisions into rational divisions.
4981 * Otherwise, we split the domains into orthants, make all integer divisions
4982 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4983 * depending on the requested sign and the sign of the term in which
4984 * the integer division appears.
4986 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4987 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4990 struct isl_to_poly_data data
;
4993 return pwqp_drop_floors(pwqp
);
4999 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
5001 for (i
= 0; i
< pwqp
->n
; ++i
) {
5002 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
5003 isl_pw_qpolynomial
*t
;
5004 t
= isl_pw_qpolynomial_alloc(
5005 isl_set_copy(pwqp
->p
[i
].set
),
5006 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
5007 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
5010 data
.qp
= pwqp
->p
[i
].qp
;
5011 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
5012 &to_polynomial_on_orthant
, &data
) < 0)
5016 isl_pw_qpolynomial_free(pwqp
);
5020 isl_pw_qpolynomial_free(pwqp
);
5021 isl_pw_qpolynomial_free(data
.res
);
5025 static __isl_give isl_pw_qpolynomial
*poly_entry(
5026 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
5030 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
5033 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
5034 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
5036 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
5037 &poly_entry
, &sign
);
5040 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
5041 __isl_take isl_qpolynomial
*qp
)
5045 isl_vec
*aff
= NULL
;
5046 isl_basic_map
*bmap
= NULL
;
5053 is_affine
= isl_poly_is_affine(qp
->poly
);
5057 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
5058 "input quasi-polynomial not affine", goto error
);
5059 aff
= isl_qpolynomial_extract_affine(qp
);
5062 dim
= isl_qpolynomial_get_space(qp
);
5063 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
5064 n_div
= qp
->div
->n_row
;
5065 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
5067 for (i
= 0; i
< n_div
; ++i
) {
5068 k
= isl_basic_map_alloc_div(bmap
);
5071 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
5072 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
5073 bmap
= isl_basic_map_add_div_constraints(bmap
, k
);
5075 k
= isl_basic_map_alloc_equality(bmap
);
5078 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5079 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5080 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5083 isl_qpolynomial_free(qp
);
5084 bmap
= isl_basic_map_finalize(bmap
);
5088 isl_qpolynomial_free(qp
);
5089 isl_basic_map_free(bmap
);