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 EL_BASE qpolynomial
34 #include <isl_list_templ.c>
37 #define EL_BASE pw_qpolynomial
39 #include <isl_list_templ.c>
41 static unsigned pos(__isl_keep isl_space
*space
, enum isl_dim_type type
)
44 case isl_dim_param
: return 0;
45 case isl_dim_in
: return space
->nparam
;
46 case isl_dim_out
: return space
->nparam
+ space
->n_in
;
51 isl_bool
isl_poly_is_cst(__isl_keep isl_poly
*poly
)
54 return isl_bool_error
;
56 return isl_bool_ok(poly
->var
< 0);
59 __isl_keep isl_poly_cst
*isl_poly_as_cst(__isl_keep isl_poly
*poly
)
64 isl_assert(poly
->ctx
, poly
->var
< 0, return NULL
);
66 return (isl_poly_cst
*) poly
;
69 __isl_keep isl_poly_rec
*isl_poly_as_rec(__isl_keep isl_poly
*poly
)
74 isl_assert(poly
->ctx
, poly
->var
>= 0, return NULL
);
76 return (isl_poly_rec
*) poly
;
79 /* Compare two polynomials.
81 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
82 * than "poly2" and 0 if they are equal.
84 static int isl_poly_plain_cmp(__isl_keep isl_poly
*poly1
,
85 __isl_keep isl_poly
*poly2
)
89 isl_poly_rec
*rec1
, *rec2
;
93 is_cst1
= isl_poly_is_cst(poly1
);
98 if (poly1
->var
!= poly2
->var
)
99 return poly1
->var
- poly2
->var
;
102 isl_poly_cst
*cst1
, *cst2
;
105 cst1
= isl_poly_as_cst(poly1
);
106 cst2
= isl_poly_as_cst(poly2
);
109 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
112 return isl_int_cmp(cst1
->d
, cst2
->d
);
115 rec1
= isl_poly_as_rec(poly1
);
116 rec2
= isl_poly_as_rec(poly2
);
120 if (rec1
->n
!= rec2
->n
)
121 return rec1
->n
- rec2
->n
;
123 for (i
= 0; i
< rec1
->n
; ++i
) {
124 int cmp
= isl_poly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
132 isl_bool
isl_poly_is_equal(__isl_keep isl_poly
*poly1
,
133 __isl_keep isl_poly
*poly2
)
137 isl_poly_rec
*rec1
, *rec2
;
139 is_cst1
= isl_poly_is_cst(poly1
);
140 if (is_cst1
< 0 || !poly2
)
141 return isl_bool_error
;
143 return isl_bool_true
;
144 if (poly1
->var
!= poly2
->var
)
145 return isl_bool_false
;
147 isl_poly_cst
*cst1
, *cst2
;
149 cst1
= isl_poly_as_cst(poly1
);
150 cst2
= isl_poly_as_cst(poly2
);
152 return isl_bool_error
;
153 r
= isl_int_eq(cst1
->n
, cst2
->n
) &&
154 isl_int_eq(cst1
->d
, cst2
->d
);
155 return isl_bool_ok(r
);
158 rec1
= isl_poly_as_rec(poly1
);
159 rec2
= isl_poly_as_rec(poly2
);
161 return isl_bool_error
;
163 if (rec1
->n
!= rec2
->n
)
164 return isl_bool_false
;
166 for (i
= 0; i
< rec1
->n
; ++i
) {
167 isl_bool eq
= isl_poly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
172 return isl_bool_true
;
175 isl_bool
isl_poly_is_zero(__isl_keep isl_poly
*poly
)
180 is_cst
= isl_poly_is_cst(poly
);
181 if (is_cst
< 0 || !is_cst
)
184 cst
= isl_poly_as_cst(poly
);
186 return isl_bool_error
;
188 return isl_bool_ok(isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
));
191 int isl_poly_sgn(__isl_keep isl_poly
*poly
)
196 is_cst
= isl_poly_is_cst(poly
);
197 if (is_cst
< 0 || !is_cst
)
200 cst
= isl_poly_as_cst(poly
);
204 return isl_int_sgn(cst
->n
);
207 isl_bool
isl_poly_is_nan(__isl_keep isl_poly
*poly
)
212 is_cst
= isl_poly_is_cst(poly
);
213 if (is_cst
< 0 || !is_cst
)
216 cst
= isl_poly_as_cst(poly
);
218 return isl_bool_error
;
220 return isl_bool_ok(isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
));
223 isl_bool
isl_poly_is_infty(__isl_keep isl_poly
*poly
)
228 is_cst
= isl_poly_is_cst(poly
);
229 if (is_cst
< 0 || !is_cst
)
232 cst
= isl_poly_as_cst(poly
);
234 return isl_bool_error
;
236 return isl_bool_ok(isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
));
239 isl_bool
isl_poly_is_neginfty(__isl_keep isl_poly
*poly
)
244 is_cst
= isl_poly_is_cst(poly
);
245 if (is_cst
< 0 || !is_cst
)
248 cst
= isl_poly_as_cst(poly
);
250 return isl_bool_error
;
252 return isl_bool_ok(isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
));
255 isl_bool
isl_poly_is_one(__isl_keep isl_poly
*poly
)
261 is_cst
= isl_poly_is_cst(poly
);
262 if (is_cst
< 0 || !is_cst
)
265 cst
= isl_poly_as_cst(poly
);
267 return isl_bool_error
;
269 r
= isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
270 return isl_bool_ok(r
);
273 isl_bool
isl_poly_is_negone(__isl_keep isl_poly
*poly
)
278 is_cst
= isl_poly_is_cst(poly
);
279 if (is_cst
< 0 || !is_cst
)
282 cst
= isl_poly_as_cst(poly
);
284 return isl_bool_error
;
286 return isl_bool_ok(isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
));
289 __isl_give isl_poly_cst
*isl_poly_cst_alloc(isl_ctx
*ctx
)
293 cst
= isl_alloc_type(ctx
, struct isl_poly_cst
);
302 isl_int_init(cst
->n
);
303 isl_int_init(cst
->d
);
308 __isl_give isl_poly
*isl_poly_zero(isl_ctx
*ctx
)
312 cst
= isl_poly_cst_alloc(ctx
);
316 isl_int_set_si(cst
->n
, 0);
317 isl_int_set_si(cst
->d
, 1);
322 __isl_give isl_poly
*isl_poly_one(isl_ctx
*ctx
)
326 cst
= isl_poly_cst_alloc(ctx
);
330 isl_int_set_si(cst
->n
, 1);
331 isl_int_set_si(cst
->d
, 1);
336 __isl_give isl_poly
*isl_poly_infty(isl_ctx
*ctx
)
340 cst
= isl_poly_cst_alloc(ctx
);
344 isl_int_set_si(cst
->n
, 1);
345 isl_int_set_si(cst
->d
, 0);
350 __isl_give isl_poly
*isl_poly_neginfty(isl_ctx
*ctx
)
354 cst
= isl_poly_cst_alloc(ctx
);
358 isl_int_set_si(cst
->n
, -1);
359 isl_int_set_si(cst
->d
, 0);
364 __isl_give isl_poly
*isl_poly_nan(isl_ctx
*ctx
)
368 cst
= isl_poly_cst_alloc(ctx
);
372 isl_int_set_si(cst
->n
, 0);
373 isl_int_set_si(cst
->d
, 0);
378 __isl_give isl_poly
*isl_poly_rat_cst(isl_ctx
*ctx
, isl_int n
, isl_int d
)
382 cst
= isl_poly_cst_alloc(ctx
);
386 isl_int_set(cst
->n
, n
);
387 isl_int_set(cst
->d
, d
);
392 __isl_give isl_poly_rec
*isl_poly_alloc_rec(isl_ctx
*ctx
, int var
, int size
)
396 isl_assert(ctx
, var
>= 0, return NULL
);
397 isl_assert(ctx
, size
>= 0, return NULL
);
398 rec
= isl_calloc(ctx
, struct isl_poly_rec
,
399 sizeof(struct isl_poly_rec
) +
400 size
* sizeof(struct isl_poly
*));
415 /* Return the domain space of "qp".
416 * This may be either a copy or the space itself
417 * if there is only one reference to "qp".
418 * This allows the space to be modified inplace
419 * if both the quasi-polynomial and its domain space
420 * have only a single reference.
421 * The caller is not allowed to modify "qp" between this call and
422 * a subsequent call to isl_qpolynomial_restore_domain_space.
423 * The only exception is that isl_qpolynomial_free can be called instead.
425 static __isl_give isl_space
*isl_qpolynomial_take_domain_space(
426 __isl_keep isl_qpolynomial
*qp
)
433 return isl_qpolynomial_get_domain_space(qp
);
439 /* Set the domain space of "qp" to "space",
440 * where the domain space of "qp" may be missing
441 * due to a preceding call to isl_qpolynomial_take_domain_space.
442 * However, in this case, "qp" only has a single reference and
443 * then the call to isl_qpolynomial_cow has no effect.
445 static __isl_give isl_qpolynomial
*isl_qpolynomial_restore_domain_space(
446 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
)
451 if (qp
->dim
== space
) {
452 isl_space_free(space
);
456 qp
= isl_qpolynomial_cow(qp
);
459 isl_space_free(qp
->dim
);
464 isl_qpolynomial_free(qp
);
465 isl_space_free(space
);
469 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
470 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
)
472 return isl_qpolynomial_restore_domain_space(qp
, space
);
475 /* Reset the space of "qp". This function is called from isl_pw_templ.c
476 * and doesn't know if the space of an element object is represented
477 * directly or through its domain. It therefore passes along both.
479 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
480 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
481 __isl_take isl_space
*domain
)
483 isl_space_free(space
);
484 return isl_qpolynomial_reset_domain_space(qp
, domain
);
487 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
489 return qp
? qp
->dim
->ctx
: NULL
;
492 /* Return the domain space of "qp".
494 static __isl_keep isl_space
*isl_qpolynomial_peek_domain_space(
495 __isl_keep isl_qpolynomial
*qp
)
497 return qp
? qp
->dim
: NULL
;
500 /* Return a copy of the domain space of "qp".
502 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
503 __isl_keep isl_qpolynomial
*qp
)
505 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp
));
509 #define TYPE isl_qpolynomial
511 #define PEEK_SPACE peek_domain_space
514 #include "isl_type_has_equal_space_bin_templ.c"
516 #include "isl_type_check_equal_space_templ.c"
520 /* Return a copy of the local variables of "qp".
522 __isl_keep isl_local
*isl_qpolynomial_get_local(
523 __isl_keep isl_qpolynomial
*qp
)
525 return qp
? isl_local_copy(qp
->div
) : NULL
;
528 /* Return the local variables of "qp".
529 * This may be either a copy or the local variables themselves
530 * if there is only one reference to "qp".
531 * This allows the local variables to be modified in-place
532 * if both the quasi-polynomial and its local variables
533 * have only a single reference.
534 * The caller is not allowed to modify "qp" between this call and
535 * the subsequent call to isl_qpolynomial_restore_local.
536 * The only exception is that isl_qpolynomial_free can be called instead.
538 static __isl_give isl_local
*isl_qpolynomial_take_local(
539 __isl_keep isl_qpolynomial
*qp
)
546 return isl_qpolynomial_get_local(qp
);
552 /* Set the local variables of "qp" to "local",
553 * where the local variables of "qp" may be missing
554 * due to a preceding call to isl_qpolynomial_take_local.
555 * However, in this case, "qp" only has a single reference and
556 * then the call to isl_qpolynomial_cow has no effect.
558 static __isl_give isl_qpolynomial
*isl_qpolynomial_restore_local(
559 __isl_keep isl_qpolynomial
*qp
, __isl_take isl_local
*local
)
564 if (qp
->div
== local
) {
565 isl_local_free(local
);
569 qp
= isl_qpolynomial_cow(qp
);
572 isl_local_free(qp
->div
);
577 isl_qpolynomial_free(qp
);
578 isl_local_free(local
);
582 /* Return a copy of the local space on which "qp" is defined.
584 static __isl_give isl_local_space
*isl_qpolynomial_get_domain_local_space(
585 __isl_keep isl_qpolynomial
*qp
)
593 space
= isl_qpolynomial_get_domain_space(qp
);
594 local
= isl_qpolynomial_get_local(qp
);
595 return isl_local_space_alloc_div(space
, local
);
598 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
603 space
= isl_space_copy(qp
->dim
);
604 space
= isl_space_from_domain(space
);
605 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
609 /* Return the number of variables of the given type in the domain of "qp".
611 isl_size
isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
612 enum isl_dim_type type
)
617 space
= isl_qpolynomial_peek_domain_space(qp
);
620 return isl_size_error
;
621 if (type
== isl_dim_div
)
622 return qp
->div
->n_row
;
623 dim
= isl_space_dim(space
, type
);
625 return isl_size_error
;
626 if (type
== isl_dim_all
) {
629 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
631 return isl_size_error
;
637 /* Given the type of a dimension of an isl_qpolynomial,
638 * return the type of the corresponding dimension in its domain.
639 * This function is only called for "type" equal to isl_dim_in or
642 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
644 return type
== isl_dim_in
? isl_dim_set
: type
;
647 /* Externally, an isl_qpolynomial has a map space, but internally, the
648 * ls field corresponds to the domain of that space.
650 isl_size
isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
651 enum isl_dim_type type
)
654 return isl_size_error
;
655 if (type
== isl_dim_out
)
657 type
= domain_type(type
);
658 return isl_qpolynomial_domain_dim(qp
, type
);
661 /* Return the offset of the first variable of type "type" within
662 * the variables of the domain of "qp".
664 static isl_size
isl_qpolynomial_domain_var_offset(
665 __isl_keep isl_qpolynomial
*qp
, enum isl_dim_type type
)
669 space
= isl_qpolynomial_peek_domain_space(qp
);
673 case isl_dim_set
: return isl_space_offset(space
, type
);
674 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
677 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
678 "invalid dimension type", return isl_size_error
);
682 /* Return the offset of the first coefficient of type "type" in
683 * the domain of "qp".
685 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
686 enum isl_dim_type type
)
694 return 1 + isl_qpolynomial_domain_var_offset(qp
, type
);
700 /* Return the polynomial expression of "qp".
702 static __isl_keep isl_poly
*isl_qpolynomial_peek_poly(
703 __isl_keep isl_qpolynomial
*qp
)
705 return qp
? qp
->poly
: NULL
;
708 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
710 return isl_poly_is_zero(isl_qpolynomial_peek_poly(qp
));
713 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
715 return isl_poly_is_one(isl_qpolynomial_peek_poly(qp
));
718 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
720 return isl_poly_is_nan(isl_qpolynomial_peek_poly(qp
));
723 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
725 return isl_poly_is_infty(isl_qpolynomial_peek_poly(qp
));
728 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
730 return isl_poly_is_neginfty(isl_qpolynomial_peek_poly(qp
));
733 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
735 return isl_poly_sgn(isl_qpolynomial_peek_poly(qp
));
738 static void poly_free_cst(__isl_take isl_poly_cst
*cst
)
740 isl_int_clear(cst
->n
);
741 isl_int_clear(cst
->d
);
744 static void poly_free_rec(__isl_take isl_poly_rec
*rec
)
748 for (i
= 0; i
< rec
->n
; ++i
)
749 isl_poly_free(rec
->p
[i
]);
752 __isl_give isl_poly
*isl_poly_copy(__isl_keep isl_poly
*poly
)
761 __isl_give isl_poly
*isl_poly_dup_cst(__isl_keep isl_poly
*poly
)
766 cst
= isl_poly_as_cst(poly
);
770 dup
= isl_poly_as_cst(isl_poly_zero(poly
->ctx
));
773 isl_int_set(dup
->n
, cst
->n
);
774 isl_int_set(dup
->d
, cst
->d
);
779 __isl_give isl_poly
*isl_poly_dup_rec(__isl_keep isl_poly
*poly
)
785 rec
= isl_poly_as_rec(poly
);
789 dup
= isl_poly_alloc_rec(poly
->ctx
, poly
->var
, rec
->n
);
793 for (i
= 0; i
< rec
->n
; ++i
) {
794 dup
->p
[i
] = isl_poly_copy(rec
->p
[i
]);
802 isl_poly_free(&dup
->poly
);
806 __isl_give isl_poly
*isl_poly_dup(__isl_keep isl_poly
*poly
)
810 is_cst
= isl_poly_is_cst(poly
);
814 return isl_poly_dup_cst(poly
);
816 return isl_poly_dup_rec(poly
);
819 __isl_give isl_poly
*isl_poly_cow(__isl_take isl_poly
*poly
)
827 return isl_poly_dup(poly
);
830 __isl_null isl_poly
*isl_poly_free(__isl_take isl_poly
*poly
)
839 poly_free_cst((isl_poly_cst
*) poly
);
841 poly_free_rec((isl_poly_rec
*) poly
);
843 isl_ctx_deref(poly
->ctx
);
848 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst
*cst
)
853 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
854 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
855 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
856 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
861 __isl_give isl_poly
*isl_poly_sum_cst(__isl_take isl_poly
*poly1
,
862 __isl_take isl_poly
*poly2
)
867 poly1
= isl_poly_cow(poly1
);
868 if (!poly1
|| !poly2
)
871 cst1
= isl_poly_as_cst(poly1
);
872 cst2
= isl_poly_as_cst(poly2
);
874 if (isl_int_eq(cst1
->d
, cst2
->d
))
875 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
877 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
878 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
879 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
882 isl_poly_cst_reduce(cst1
);
884 isl_poly_free(poly2
);
887 isl_poly_free(poly1
);
888 isl_poly_free(poly2
);
892 static __isl_give isl_poly
*replace_by_zero(__isl_take isl_poly
*poly
)
900 return isl_poly_zero(ctx
);
903 static __isl_give isl_poly
*replace_by_constant_term(__isl_take isl_poly
*poly
)
911 rec
= isl_poly_as_rec(poly
);
914 cst
= isl_poly_copy(rec
->p
[0]);
922 __isl_give isl_poly
*isl_poly_sum(__isl_take isl_poly
*poly1
,
923 __isl_take isl_poly
*poly2
)
926 isl_bool is_zero
, is_nan
, is_cst
;
927 isl_poly_rec
*rec1
, *rec2
;
929 if (!poly1
|| !poly2
)
932 is_nan
= isl_poly_is_nan(poly1
);
936 isl_poly_free(poly2
);
940 is_nan
= isl_poly_is_nan(poly2
);
944 isl_poly_free(poly1
);
948 is_zero
= isl_poly_is_zero(poly1
);
952 isl_poly_free(poly1
);
956 is_zero
= isl_poly_is_zero(poly2
);
960 isl_poly_free(poly2
);
964 if (poly1
->var
< poly2
->var
)
965 return isl_poly_sum(poly2
, poly1
);
967 if (poly2
->var
< poly1
->var
) {
971 is_infty
= isl_poly_is_infty(poly2
);
972 if (is_infty
>= 0 && !is_infty
)
973 is_infty
= isl_poly_is_neginfty(poly2
);
977 isl_poly_free(poly1
);
980 poly1
= isl_poly_cow(poly1
);
981 rec
= isl_poly_as_rec(poly1
);
984 rec
->p
[0] = isl_poly_sum(rec
->p
[0], poly2
);
986 poly1
= replace_by_constant_term(poly1
);
990 is_cst
= isl_poly_is_cst(poly1
);
994 return isl_poly_sum_cst(poly1
, poly2
);
996 rec1
= isl_poly_as_rec(poly1
);
997 rec2
= isl_poly_as_rec(poly2
);
1001 if (rec1
->n
< rec2
->n
)
1002 return isl_poly_sum(poly2
, poly1
);
1004 poly1
= isl_poly_cow(poly1
);
1005 rec1
= isl_poly_as_rec(poly1
);
1009 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
1012 rec1
->p
[i
] = isl_poly_sum(rec1
->p
[i
],
1013 isl_poly_copy(rec2
->p
[i
]));
1016 if (i
!= rec1
->n
- 1)
1018 is_zero
= isl_poly_is_zero(rec1
->p
[i
]);
1022 isl_poly_free(rec1
->p
[i
]);
1028 poly1
= replace_by_zero(poly1
);
1029 else if (rec1
->n
== 1)
1030 poly1
= replace_by_constant_term(poly1
);
1032 isl_poly_free(poly2
);
1036 isl_poly_free(poly1
);
1037 isl_poly_free(poly2
);
1041 __isl_give isl_poly
*isl_poly_cst_add_isl_int(__isl_take isl_poly
*poly
,
1046 poly
= isl_poly_cow(poly
);
1050 cst
= isl_poly_as_cst(poly
);
1052 isl_int_addmul(cst
->n
, cst
->d
, v
);
1057 __isl_give isl_poly
*isl_poly_add_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
1062 is_cst
= isl_poly_is_cst(poly
);
1064 return isl_poly_free(poly
);
1066 return isl_poly_cst_add_isl_int(poly
, v
);
1068 poly
= isl_poly_cow(poly
);
1069 rec
= isl_poly_as_rec(poly
);
1073 rec
->p
[0] = isl_poly_add_isl_int(rec
->p
[0], v
);
1079 isl_poly_free(poly
);
1083 __isl_give isl_poly
*isl_poly_cst_mul_isl_int(__isl_take isl_poly
*poly
,
1089 is_zero
= isl_poly_is_zero(poly
);
1091 return isl_poly_free(poly
);
1095 poly
= isl_poly_cow(poly
);
1099 cst
= isl_poly_as_cst(poly
);
1101 isl_int_mul(cst
->n
, cst
->n
, v
);
1106 __isl_give isl_poly
*isl_poly_mul_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
1112 is_cst
= isl_poly_is_cst(poly
);
1114 return isl_poly_free(poly
);
1116 return isl_poly_cst_mul_isl_int(poly
, v
);
1118 poly
= isl_poly_cow(poly
);
1119 rec
= isl_poly_as_rec(poly
);
1123 for (i
= 0; i
< rec
->n
; ++i
) {
1124 rec
->p
[i
] = isl_poly_mul_isl_int(rec
->p
[i
], v
);
1131 isl_poly_free(poly
);
1135 /* Multiply the constant polynomial "poly" by "v".
1137 static __isl_give isl_poly
*isl_poly_cst_scale_val(__isl_take isl_poly
*poly
,
1138 __isl_keep isl_val
*v
)
1143 is_zero
= isl_poly_is_zero(poly
);
1145 return isl_poly_free(poly
);
1149 poly
= isl_poly_cow(poly
);
1153 cst
= isl_poly_as_cst(poly
);
1155 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
1156 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
1157 isl_poly_cst_reduce(cst
);
1162 /* Multiply the polynomial "poly" by "v".
1164 static __isl_give isl_poly
*isl_poly_scale_val(__isl_take isl_poly
*poly
,
1165 __isl_keep isl_val
*v
)
1171 is_cst
= isl_poly_is_cst(poly
);
1173 return isl_poly_free(poly
);
1175 return isl_poly_cst_scale_val(poly
, v
);
1177 poly
= isl_poly_cow(poly
);
1178 rec
= isl_poly_as_rec(poly
);
1182 for (i
= 0; i
< rec
->n
; ++i
) {
1183 rec
->p
[i
] = isl_poly_scale_val(rec
->p
[i
], v
);
1190 isl_poly_free(poly
);
1194 __isl_give isl_poly
*isl_poly_mul_cst(__isl_take isl_poly
*poly1
,
1195 __isl_take isl_poly
*poly2
)
1200 poly1
= isl_poly_cow(poly1
);
1201 if (!poly1
|| !poly2
)
1204 cst1
= isl_poly_as_cst(poly1
);
1205 cst2
= isl_poly_as_cst(poly2
);
1207 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
1208 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
1210 isl_poly_cst_reduce(cst1
);
1212 isl_poly_free(poly2
);
1215 isl_poly_free(poly1
);
1216 isl_poly_free(poly2
);
1220 __isl_give isl_poly
*isl_poly_mul_rec(__isl_take isl_poly
*poly1
,
1221 __isl_take isl_poly
*poly2
)
1225 isl_poly_rec
*res
= NULL
;
1229 rec1
= isl_poly_as_rec(poly1
);
1230 rec2
= isl_poly_as_rec(poly2
);
1233 size
= rec1
->n
+ rec2
->n
- 1;
1234 res
= isl_poly_alloc_rec(poly1
->ctx
, poly1
->var
, size
);
1238 for (i
= 0; i
< rec1
->n
; ++i
) {
1239 res
->p
[i
] = isl_poly_mul(isl_poly_copy(rec2
->p
[0]),
1240 isl_poly_copy(rec1
->p
[i
]));
1245 for (; i
< size
; ++i
) {
1246 res
->p
[i
] = isl_poly_zero(poly1
->ctx
);
1251 for (i
= 0; i
< rec1
->n
; ++i
) {
1252 for (j
= 1; j
< rec2
->n
; ++j
) {
1254 poly
= isl_poly_mul(isl_poly_copy(rec2
->p
[j
]),
1255 isl_poly_copy(rec1
->p
[i
]));
1256 res
->p
[i
+ j
] = isl_poly_sum(res
->p
[i
+ j
], poly
);
1262 isl_poly_free(poly1
);
1263 isl_poly_free(poly2
);
1267 isl_poly_free(poly1
);
1268 isl_poly_free(poly2
);
1269 isl_poly_free(&res
->poly
);
1273 __isl_give isl_poly
*isl_poly_mul(__isl_take isl_poly
*poly1
,
1274 __isl_take isl_poly
*poly2
)
1276 isl_bool is_zero
, is_nan
, is_one
, is_cst
;
1278 if (!poly1
|| !poly2
)
1281 is_nan
= isl_poly_is_nan(poly1
);
1285 isl_poly_free(poly2
);
1289 is_nan
= isl_poly_is_nan(poly2
);
1293 isl_poly_free(poly1
);
1297 is_zero
= isl_poly_is_zero(poly1
);
1301 isl_poly_free(poly2
);
1305 is_zero
= isl_poly_is_zero(poly2
);
1309 isl_poly_free(poly1
);
1313 is_one
= isl_poly_is_one(poly1
);
1317 isl_poly_free(poly1
);
1321 is_one
= isl_poly_is_one(poly2
);
1325 isl_poly_free(poly2
);
1329 if (poly1
->var
< poly2
->var
)
1330 return isl_poly_mul(poly2
, poly1
);
1332 if (poly2
->var
< poly1
->var
) {
1337 is_infty
= isl_poly_is_infty(poly2
);
1338 if (is_infty
>= 0 && !is_infty
)
1339 is_infty
= isl_poly_is_neginfty(poly2
);
1343 isl_ctx
*ctx
= poly1
->ctx
;
1344 isl_poly_free(poly1
);
1345 isl_poly_free(poly2
);
1346 return isl_poly_nan(ctx
);
1348 poly1
= isl_poly_cow(poly1
);
1349 rec
= isl_poly_as_rec(poly1
);
1353 for (i
= 0; i
< rec
->n
; ++i
) {
1354 rec
->p
[i
] = isl_poly_mul(rec
->p
[i
],
1355 isl_poly_copy(poly2
));
1359 isl_poly_free(poly2
);
1363 is_cst
= isl_poly_is_cst(poly1
);
1367 return isl_poly_mul_cst(poly1
, poly2
);
1369 return isl_poly_mul_rec(poly1
, poly2
);
1371 isl_poly_free(poly1
);
1372 isl_poly_free(poly2
);
1376 __isl_give isl_poly
*isl_poly_pow(__isl_take isl_poly
*poly
, unsigned power
)
1386 res
= isl_poly_copy(poly
);
1388 res
= isl_poly_one(poly
->ctx
);
1390 while (power
>>= 1) {
1391 poly
= isl_poly_mul(poly
, isl_poly_copy(poly
));
1393 res
= isl_poly_mul(res
, isl_poly_copy(poly
));
1396 isl_poly_free(poly
);
1400 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1401 unsigned n_div
, __isl_take isl_poly
*poly
)
1403 struct isl_qpolynomial
*qp
= NULL
;
1406 total
= isl_space_dim(space
, isl_dim_all
);
1407 if (total
< 0 || !poly
)
1410 if (!isl_space_is_set(space
))
1411 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1412 "domain of polynomial should be a set", goto error
);
1414 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1419 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1428 isl_space_free(space
);
1429 isl_poly_free(poly
);
1430 isl_qpolynomial_free(qp
);
1434 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1443 /* Return a copy of the polynomial expression of "qp".
1445 __isl_give isl_poly
*isl_qpolynomial_get_poly(__isl_keep isl_qpolynomial
*qp
)
1447 return isl_poly_copy(isl_qpolynomial_peek_poly(qp
));
1450 /* Return the polynomial expression of "qp".
1451 * This may be either a copy or the polynomial expression itself
1452 * if there is only one reference to "qp".
1453 * This allows the polynomial expression to be modified inplace
1454 * if both the quasi-polynomial and its polynomial expression
1455 * have only a single reference.
1456 * The caller is not allowed to modify "qp" between this call and
1457 * a subsequent call to isl_qpolynomial_restore_poly.
1458 * The only exception is that isl_qpolynomial_free can be called instead.
1460 static __isl_give isl_poly
*isl_qpolynomial_take_poly(
1461 __isl_keep isl_qpolynomial
*qp
)
1468 return isl_qpolynomial_get_poly(qp
);
1474 /* Set the polynomial expression of "qp" to "space",
1475 * where the polynomial expression of "qp" may be missing
1476 * due to a preceding call to isl_qpolynomial_take_poly.
1477 * However, in this case, "qp" only has a single reference and
1478 * then the call to isl_qpolynomial_cow has no effect.
1480 static __isl_give isl_qpolynomial
*isl_qpolynomial_restore_poly(
1481 __isl_keep isl_qpolynomial
*qp
, __isl_take isl_poly
*poly
)
1486 if (qp
->poly
== poly
) {
1487 isl_poly_free(poly
);
1491 qp
= isl_qpolynomial_cow(qp
);
1494 isl_poly_free(qp
->poly
);
1499 isl_qpolynomial_free(qp
);
1500 isl_poly_free(poly
);
1504 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1507 struct isl_qpolynomial
*dup
;
1512 poly
= isl_qpolynomial_get_poly(qp
);
1513 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1517 isl_mat_free(dup
->div
);
1518 dup
->div
= isl_qpolynomial_get_local(qp
);
1524 isl_qpolynomial_free(dup
);
1528 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1536 return isl_qpolynomial_dup(qp
);
1539 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1540 __isl_take isl_qpolynomial
*qp
)
1548 isl_space_free(qp
->dim
);
1549 isl_mat_free(qp
->div
);
1550 isl_poly_free(qp
->poly
);
1556 __isl_give isl_poly
*isl_poly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1562 rec
= isl_poly_alloc_rec(ctx
, pos
, 1 + power
);
1565 for (i
= 0; i
< 1 + power
; ++i
) {
1566 rec
->p
[i
] = isl_poly_zero(ctx
);
1571 cst
= isl_poly_as_cst(rec
->p
[power
]);
1572 isl_int_set_si(cst
->n
, 1);
1576 isl_poly_free(&rec
->poly
);
1580 /* r array maps original positions to new positions.
1582 static __isl_give isl_poly
*reorder(__isl_take isl_poly
*poly
, int *r
)
1590 is_cst
= isl_poly_is_cst(poly
);
1592 return isl_poly_free(poly
);
1596 rec
= isl_poly_as_rec(poly
);
1600 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
1602 base
= isl_poly_var_pow(poly
->ctx
, r
[poly
->var
], 1);
1603 res
= reorder(isl_poly_copy(rec
->p
[rec
->n
- 1]), r
);
1605 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1606 res
= isl_poly_mul(res
, isl_poly_copy(base
));
1607 res
= isl_poly_sum(res
, reorder(isl_poly_copy(rec
->p
[i
]), r
));
1610 isl_poly_free(base
);
1611 isl_poly_free(poly
);
1615 isl_poly_free(poly
);
1619 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1620 __isl_keep isl_mat
*div2
)
1625 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1626 div1
->n_col
>= div2
->n_col
,
1627 return isl_bool_error
);
1629 if (div1
->n_row
== div2
->n_row
)
1630 return isl_mat_is_equal(div1
, div2
);
1632 n_row
= div1
->n_row
;
1633 n_col
= div1
->n_col
;
1634 div1
->n_row
= div2
->n_row
;
1635 div1
->n_col
= div2
->n_col
;
1637 equal
= isl_mat_is_equal(div1
, div2
);
1639 div1
->n_row
= n_row
;
1640 div1
->n_col
= n_col
;
1645 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1649 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1650 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1655 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1658 struct isl_div_sort_info
{
1663 static int div_sort_cmp(const void *p1
, const void *p2
)
1665 const struct isl_div_sort_info
*i1
, *i2
;
1666 i1
= (const struct isl_div_sort_info
*) p1
;
1667 i2
= (const struct isl_div_sort_info
*) p2
;
1669 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1672 /* Sort divs and remove duplicates.
1674 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1679 struct isl_div_sort_info
*array
= NULL
;
1680 int *pos
= NULL
, *at
= NULL
;
1681 int *reordering
= NULL
;
1686 if (qp
->div
->n_row
<= 1)
1689 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
1691 return isl_qpolynomial_free(qp
);
1693 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1695 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1696 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1697 len
= qp
->div
->n_col
- 2;
1698 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1699 if (!array
|| !pos
|| !at
|| !reordering
)
1702 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1703 array
[i
].div
= qp
->div
;
1709 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1712 for (i
= 0; i
< div_pos
; ++i
)
1715 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1716 if (pos
[array
[i
].row
] == i
)
1718 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1719 pos
[at
[i
]] = pos
[array
[i
].row
];
1720 at
[pos
[array
[i
].row
]] = at
[i
];
1721 at
[i
] = array
[i
].row
;
1722 pos
[array
[i
].row
] = i
;
1726 for (i
= 0; i
< len
- div_pos
; ++i
) {
1728 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1729 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1730 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1731 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1732 2 + div_pos
+ i
- skip
);
1733 qp
->div
= isl_mat_drop_cols(qp
->div
,
1734 2 + div_pos
+ i
- skip
, 1);
1737 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1740 qp
->poly
= reorder(qp
->poly
, reordering
);
1742 if (!qp
->poly
|| !qp
->div
)
1756 isl_qpolynomial_free(qp
);
1760 static __isl_give isl_poly
*expand(__isl_take isl_poly
*poly
, int *exp
,
1767 is_cst
= isl_poly_is_cst(poly
);
1769 return isl_poly_free(poly
);
1773 if (poly
->var
< first
)
1776 if (exp
[poly
->var
- first
] == poly
->var
- first
)
1779 poly
= isl_poly_cow(poly
);
1783 poly
->var
= exp
[poly
->var
- first
] + first
;
1785 rec
= isl_poly_as_rec(poly
);
1789 for (i
= 0; i
< rec
->n
; ++i
) {
1790 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1797 isl_poly_free(poly
);
1801 static __isl_give isl_qpolynomial
*with_merged_divs(
1802 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1803 __isl_take isl_qpolynomial
*qp2
),
1804 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1808 isl_mat
*div
= NULL
;
1811 qp1
= isl_qpolynomial_cow(qp1
);
1812 qp2
= isl_qpolynomial_cow(qp2
);
1817 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1818 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1820 n_div1
= qp1
->div
->n_row
;
1821 n_div2
= qp2
->div
->n_row
;
1822 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1823 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1824 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1827 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1831 isl_mat_free(qp1
->div
);
1832 qp1
->div
= isl_mat_copy(div
);
1833 isl_mat_free(qp2
->div
);
1834 qp2
->div
= isl_mat_copy(div
);
1836 qp1
->poly
= expand(qp1
->poly
, exp1
, div
->n_col
- div
->n_row
- 2);
1837 qp2
->poly
= expand(qp2
->poly
, exp2
, div
->n_col
- div
->n_row
- 2);
1839 if (!qp1
->poly
|| !qp2
->poly
)
1846 return fn(qp1
, qp2
);
1851 isl_qpolynomial_free(qp1
);
1852 isl_qpolynomial_free(qp2
);
1856 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1857 __isl_take isl_qpolynomial
*qp2
)
1859 isl_bool compatible
;
1862 if (isl_qpolynomial_check_equal_space(qp1
, qp2
) < 0)
1865 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1866 return isl_qpolynomial_add(qp2
, qp1
);
1868 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1872 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1874 poly
= isl_qpolynomial_take_poly(qp1
);
1875 poly
= isl_poly_sum(poly
, isl_qpolynomial_get_poly(qp2
));
1876 qp1
= isl_qpolynomial_restore_poly(qp1
, poly
);
1878 isl_qpolynomial_free(qp2
);
1882 isl_qpolynomial_free(qp1
);
1883 isl_qpolynomial_free(qp2
);
1887 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1888 __isl_keep isl_set
*dom
,
1889 __isl_take isl_qpolynomial
*qp1
,
1890 __isl_take isl_qpolynomial
*qp2
)
1892 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1893 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1897 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1898 __isl_take isl_qpolynomial
*qp2
)
1900 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1903 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1904 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1908 if (isl_int_is_zero(v
))
1911 poly
= isl_qpolynomial_take_poly(qp
);
1912 poly
= isl_poly_add_isl_int(poly
, v
);
1913 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
1918 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1923 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1926 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1927 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1931 if (isl_int_is_one(v
))
1934 if (qp
&& isl_int_is_zero(v
)) {
1935 isl_qpolynomial
*zero
;
1936 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1937 isl_qpolynomial_free(qp
);
1941 poly
= isl_qpolynomial_take_poly(qp
);
1942 poly
= isl_poly_mul_isl_int(poly
, v
);
1943 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
1948 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1949 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1951 return isl_qpolynomial_mul_isl_int(qp
, v
);
1954 /* Multiply "qp" by "v".
1956 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1957 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1964 if (!isl_val_is_rat(v
))
1965 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1966 "expecting rational factor", goto error
);
1968 if (isl_val_is_one(v
)) {
1973 if (isl_val_is_zero(v
)) {
1976 space
= isl_qpolynomial_get_domain_space(qp
);
1977 isl_qpolynomial_free(qp
);
1979 return isl_qpolynomial_zero_on_domain(space
);
1982 poly
= isl_qpolynomial_take_poly(qp
);
1983 poly
= isl_poly_scale_val(poly
, v
);
1984 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
1990 isl_qpolynomial_free(qp
);
1994 /* Divide "qp" by "v".
1996 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1997 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
2002 if (!isl_val_is_rat(v
))
2003 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2004 "expecting rational factor", goto error
);
2005 if (isl_val_is_zero(v
))
2006 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
2007 "cannot scale down by zero", goto error
);
2009 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
2012 isl_qpolynomial_free(qp
);
2016 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
2017 __isl_take isl_qpolynomial
*qp2
)
2019 isl_bool compatible
;
2022 if (isl_qpolynomial_check_equal_space(qp1
, qp2
) < 0)
2025 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
2026 return isl_qpolynomial_mul(qp2
, qp1
);
2028 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
2032 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
2034 poly
= isl_qpolynomial_take_poly(qp1
);
2035 poly
= isl_poly_mul(poly
, isl_qpolynomial_get_poly(qp2
));
2036 qp1
= isl_qpolynomial_restore_poly(qp1
, poly
);
2038 isl_qpolynomial_free(qp2
);
2042 isl_qpolynomial_free(qp1
);
2043 isl_qpolynomial_free(qp2
);
2047 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
2052 poly
= isl_qpolynomial_take_poly(qp
);
2053 poly
= isl_poly_pow(poly
, power
);
2054 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
2059 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
2060 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
2067 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
2071 for (i
= 0; i
< pwqp
->n
; ++i
) {
2072 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
2074 return isl_pw_qpolynomial_free(pwqp
);
2080 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
2081 __isl_take isl_space
*domain
)
2085 return isl_qpolynomial_alloc(domain
, 0, isl_poly_zero(domain
->ctx
));
2088 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
2089 __isl_take isl_space
*domain
)
2093 return isl_qpolynomial_alloc(domain
, 0, isl_poly_one(domain
->ctx
));
2096 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
2097 __isl_take isl_space
*domain
)
2101 return isl_qpolynomial_alloc(domain
, 0, isl_poly_infty(domain
->ctx
));
2104 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
2105 __isl_take isl_space
*domain
)
2109 return isl_qpolynomial_alloc(domain
, 0, isl_poly_neginfty(domain
->ctx
));
2112 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
2113 __isl_take isl_space
*domain
)
2117 return isl_qpolynomial_alloc(domain
, 0, isl_poly_nan(domain
->ctx
));
2120 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
2121 __isl_take isl_space
*domain
,
2124 struct isl_qpolynomial
*qp
;
2127 qp
= isl_qpolynomial_zero_on_domain(domain
);
2131 cst
= isl_poly_as_cst(qp
->poly
);
2132 isl_int_set(cst
->n
, v
);
2137 isl_bool
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
2138 isl_int
*n
, isl_int
*d
)
2144 poly
= isl_qpolynomial_peek_poly(qp
);
2145 is_cst
= isl_poly_is_cst(poly
);
2146 if (is_cst
< 0 || !is_cst
)
2149 cst
= isl_poly_as_cst(poly
);
2151 return isl_bool_error
;
2154 isl_int_set(*n
, cst
->n
);
2156 isl_int_set(*d
, cst
->d
);
2158 return isl_bool_true
;
2161 /* Return the constant term of "poly".
2163 static __isl_give isl_val
*isl_poly_get_constant_val(__isl_keep isl_poly
*poly
)
2171 while ((is_cst
= isl_poly_is_cst(poly
)) == isl_bool_false
) {
2174 rec
= isl_poly_as_rec(poly
);
2182 cst
= isl_poly_as_cst(poly
);
2185 return isl_val_rat_from_isl_int(cst
->poly
.ctx
, cst
->n
, cst
->d
);
2188 /* Return the constant term of "qp".
2190 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
2191 __isl_keep isl_qpolynomial
*qp
)
2193 return isl_poly_get_constant_val(isl_qpolynomial_peek_poly(qp
));
2196 isl_bool
isl_poly_is_affine(__isl_keep isl_poly
*poly
)
2202 return isl_bool_error
;
2205 return isl_bool_true
;
2207 rec
= isl_poly_as_rec(poly
);
2209 return isl_bool_error
;
2212 return isl_bool_false
;
2214 isl_assert(poly
->ctx
, rec
->n
> 1, return isl_bool_error
);
2216 is_cst
= isl_poly_is_cst(rec
->p
[1]);
2217 if (is_cst
< 0 || !is_cst
)
2220 return isl_poly_is_affine(rec
->p
[0]);
2223 /* Can "qp" be converted to an isl_aff?
2224 * That is, does it represent a quasi-affine expression?
2226 isl_bool
isl_qpolynomial_isa_aff(__isl_keep isl_qpolynomial
*qp
)
2228 return isl_poly_is_affine(isl_qpolynomial_peek_poly(qp
));
2231 isl_bool
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
2234 return isl_bool_error
;
2236 if (qp
->div
->n_row
> 0)
2237 return isl_bool_false
;
2239 return isl_qpolynomial_isa_aff(qp
);
2242 static void update_coeff(__isl_keep isl_vec
*aff
,
2243 __isl_keep isl_poly_cst
*cst
, int pos
)
2248 if (isl_int_is_zero(cst
->n
))
2253 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
2254 isl_int_divexact(f
, cst
->d
, gcd
);
2255 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
2256 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
2257 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
2262 int isl_poly_update_affine(__isl_keep isl_poly
*poly
, __isl_keep isl_vec
*aff
)
2270 if (poly
->var
< 0) {
2273 cst
= isl_poly_as_cst(poly
);
2276 update_coeff(aff
, cst
, 0);
2280 rec
= isl_poly_as_rec(poly
);
2283 isl_assert(poly
->ctx
, rec
->n
== 2, return -1);
2285 cst
= isl_poly_as_cst(rec
->p
[1]);
2288 update_coeff(aff
, cst
, 1 + poly
->var
);
2290 return isl_poly_update_affine(rec
->p
[0], aff
);
2293 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
2294 __isl_keep isl_qpolynomial
*qp
)
2299 d
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2303 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
2307 isl_seq_clr(aff
->el
+ 1, 1 + d
);
2308 isl_int_set_si(aff
->el
[0], 1);
2310 if (isl_poly_update_affine(qp
->poly
, aff
) < 0)
2319 /* Compare two quasi-polynomials.
2321 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2322 * than "qp2" and 0 if they are equal.
2324 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2325 __isl_keep isl_qpolynomial
*qp2
)
2336 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2340 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2344 return isl_poly_plain_cmp(qp1
->poly
, qp2
->poly
);
2347 /* Is "qp1" obviously equal to "qp2"?
2349 * NaN is not equal to anything, not even to another NaN.
2351 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2352 __isl_keep isl_qpolynomial
*qp2
)
2357 return isl_bool_error
;
2359 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2360 return isl_bool_false
;
2362 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2363 if (equal
< 0 || !equal
)
2366 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2367 if (equal
< 0 || !equal
)
2370 return isl_poly_is_equal(qp1
->poly
, qp2
->poly
);
2373 static isl_stat
poly_update_den(__isl_keep isl_poly
*poly
, isl_int
*d
)
2379 is_cst
= isl_poly_is_cst(poly
);
2381 return isl_stat_error
;
2384 cst
= isl_poly_as_cst(poly
);
2386 return isl_stat_error
;
2387 isl_int_lcm(*d
, *d
, cst
->d
);
2391 rec
= isl_poly_as_rec(poly
);
2393 return isl_stat_error
;
2395 for (i
= 0; i
< rec
->n
; ++i
)
2396 poly_update_den(rec
->p
[i
], d
);
2401 __isl_give isl_val
*isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
)
2407 d
= isl_val_one(isl_qpolynomial_get_ctx(qp
));
2410 if (poly_update_den(qp
->poly
, &d
->n
) < 0)
2411 return isl_val_free(d
);
2415 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2416 __isl_take isl_space
*domain
, int pos
, int power
)
2418 struct isl_ctx
*ctx
;
2425 return isl_qpolynomial_alloc(domain
, 0,
2426 isl_poly_var_pow(ctx
, pos
, power
));
2429 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2430 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2434 if (isl_space_check_is_set(domain
) < 0)
2436 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2439 off
= isl_space_offset(domain
, type
);
2443 return isl_qpolynomial_var_pow_on_domain(domain
, off
+ pos
, 1);
2445 isl_space_free(domain
);
2449 __isl_give isl_poly
*isl_poly_subs(__isl_take isl_poly
*poly
,
2450 unsigned first
, unsigned n
, __isl_keep isl_poly
**subs
)
2455 isl_poly
*base
, *res
;
2457 is_cst
= isl_poly_is_cst(poly
);
2459 return isl_poly_free(poly
);
2463 if (poly
->var
< first
)
2466 rec
= isl_poly_as_rec(poly
);
2470 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
2472 if (poly
->var
>= first
+ n
)
2473 base
= isl_poly_var_pow(poly
->ctx
, poly
->var
, 1);
2475 base
= isl_poly_copy(subs
[poly
->var
- first
]);
2477 res
= isl_poly_subs(isl_poly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2478 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2480 t
= isl_poly_subs(isl_poly_copy(rec
->p
[i
]), first
, n
, subs
);
2481 res
= isl_poly_mul(res
, isl_poly_copy(base
));
2482 res
= isl_poly_sum(res
, t
);
2485 isl_poly_free(base
);
2486 isl_poly_free(poly
);
2490 isl_poly_free(poly
);
2494 __isl_give isl_poly
*isl_poly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2495 isl_int denom
, unsigned len
)
2500 isl_assert(ctx
, len
>= 1, return NULL
);
2502 poly
= isl_poly_rat_cst(ctx
, f
[0], denom
);
2503 for (i
= 0; i
< len
- 1; ++i
) {
2507 if (isl_int_is_zero(f
[1 + i
]))
2510 c
= isl_poly_rat_cst(ctx
, f
[1 + i
], denom
);
2511 t
= isl_poly_var_pow(ctx
, i
, 1);
2512 t
= isl_poly_mul(c
, t
);
2513 poly
= isl_poly_sum(poly
, t
);
2519 /* Remove common factor of non-constant terms and denominator.
2521 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2523 isl_ctx
*ctx
= qp
->div
->ctx
;
2524 unsigned total
= qp
->div
->n_col
- 2;
2526 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2527 isl_int_gcd(ctx
->normalize_gcd
,
2528 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2529 if (isl_int_is_one(ctx
->normalize_gcd
))
2532 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2533 ctx
->normalize_gcd
, total
);
2534 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2535 ctx
->normalize_gcd
);
2536 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2537 ctx
->normalize_gcd
);
2540 /* Replace the integer division identified by "div" by the polynomial "s".
2541 * The integer division is assumed not to appear in the definition
2542 * of any other integer divisions.
2544 static __isl_give isl_qpolynomial
*substitute_div(
2545 __isl_take isl_qpolynomial
*qp
, int div
, __isl_take isl_poly
*s
)
2555 qp
= isl_qpolynomial_cow(qp
);
2559 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2562 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ div
, 1, &s
);
2566 ctx
= isl_qpolynomial_get_ctx(qp
);
2567 reordering
= isl_alloc_array(ctx
, int, div_pos
+ qp
->div
->n_row
);
2570 for (i
= 0; i
< div_pos
+ div
; ++i
)
2572 for (i
= div_pos
+ div
+ 1; i
< div_pos
+ qp
->div
->n_row
; ++i
)
2573 reordering
[i
] = i
- 1;
2574 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2575 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + div_pos
+ div
, 1);
2576 qp
->poly
= reorder(qp
->poly
, reordering
);
2579 if (!qp
->poly
|| !qp
->div
)
2585 isl_qpolynomial_free(qp
);
2590 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2591 * divisions because d is equal to 1 by their definition, i.e., e.
2593 static __isl_give isl_qpolynomial
*substitute_non_divs(
2594 __isl_take isl_qpolynomial
*qp
)
2600 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2602 return isl_qpolynomial_free(qp
);
2604 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2605 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2607 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2608 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
2610 isl_seq_combine(qp
->div
->row
[j
] + 1,
2611 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2612 qp
->div
->row
[j
][2 + div_pos
+ i
],
2613 qp
->div
->row
[i
] + 1, 1 + div_pos
+ i
);
2614 isl_int_set_si(qp
->div
->row
[j
][2 + div_pos
+ i
], 0);
2615 normalize_div(qp
, j
);
2617 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2618 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2619 qp
= substitute_div(qp
, i
, s
);
2626 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2627 * with d the denominator. When replacing the coefficient e of x by
2628 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2629 * inside the division, so we need to add floor(e/d) * x outside.
2630 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2631 * to adjust the coefficient of x in each later div that depends on the
2632 * current div "div" and also in the affine expressions in the rows of "mat"
2633 * (if they too depend on "div").
2635 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2636 __isl_keep isl_mat
**mat
)
2640 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2643 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2644 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2645 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2647 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2648 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2649 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2650 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2651 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2652 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2654 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2655 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2661 /* Check if the last non-zero coefficient is bigger that half of the
2662 * denominator. If so, we will invert the div to further reduce the number
2663 * of distinct divs that may appear.
2664 * If the last non-zero coefficient is exactly half the denominator,
2665 * then we continue looking for earlier coefficients that are bigger
2666 * than half the denominator.
2668 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2673 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2674 if (isl_int_is_zero(div
->row
[row
][i
]))
2676 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2677 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2678 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2688 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2689 * We only invert the coefficients of e (and the coefficient of q in
2690 * later divs and in the rows of "mat"). After calling this function, the
2691 * coefficients of e should be reduced again.
2693 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2694 __isl_keep isl_mat
**mat
)
2696 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2698 isl_seq_neg(qp
->div
->row
[div
] + 1,
2699 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2700 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2701 isl_int_add(qp
->div
->row
[div
][1],
2702 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2703 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2704 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2705 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2708 /* Reduce all divs of "qp" to have coefficients
2709 * in the interval [0, d-1], with d the denominator and such that the
2710 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2711 * The modifications to the integer divisions need to be reflected
2712 * in the factors of the polynomial that refer to the original
2713 * integer divisions. To this end, the modifications are collected
2714 * as a set of affine expressions and then plugged into the polynomial.
2716 * After the reduction, some divs may have become redundant or identical,
2717 * so we call substitute_non_divs and sort_divs. If these functions
2718 * eliminate divs or merge two or more divs into one, the coefficients
2719 * of the enclosing divs may have to be reduced again, so we call
2720 * ourselves recursively if the number of divs decreases.
2722 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2729 isl_size n_div
, total
, new_n_div
;
2731 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2732 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2733 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2734 if (total
< 0 || n_div
< 0)
2735 return isl_qpolynomial_free(qp
);
2736 ctx
= isl_qpolynomial_get_ctx(qp
);
2737 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2739 for (i
= 0; i
< n_div
; ++i
)
2740 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2742 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2743 normalize_div(qp
, i
);
2744 reduce_div(qp
, i
, &mat
);
2745 if (needs_invert(qp
->div
, i
)) {
2746 invert_div(qp
, i
, &mat
);
2747 reduce_div(qp
, i
, &mat
);
2753 s
= isl_alloc_array(ctx
, struct isl_poly
*, n_div
);
2756 for (i
= 0; i
< n_div
; ++i
)
2757 s
[i
] = isl_poly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2759 qp
->poly
= isl_poly_subs(qp
->poly
, o_div
- 1, n_div
, s
);
2760 for (i
= 0; i
< n_div
; ++i
)
2761 isl_poly_free(s
[i
]);
2768 qp
= substitute_non_divs(qp
);
2770 new_n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2772 return isl_qpolynomial_free(qp
);
2773 if (new_n_div
< n_div
)
2774 return reduce_divs(qp
);
2778 isl_qpolynomial_free(qp
);
2783 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2784 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2786 struct isl_qpolynomial
*qp
;
2789 qp
= isl_qpolynomial_zero_on_domain(domain
);
2793 cst
= isl_poly_as_cst(qp
->poly
);
2794 isl_int_set(cst
->n
, n
);
2795 isl_int_set(cst
->d
, d
);
2800 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2802 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2803 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2805 isl_qpolynomial
*qp
;
2808 qp
= isl_qpolynomial_zero_on_domain(domain
);
2812 cst
= isl_poly_as_cst(qp
->poly
);
2813 isl_int_set(cst
->n
, val
->n
);
2814 isl_int_set(cst
->d
, val
->d
);
2820 isl_qpolynomial_free(qp
);
2824 static isl_stat
poly_set_active(__isl_keep isl_poly
*poly
, int *active
, int d
)
2830 is_cst
= isl_poly_is_cst(poly
);
2832 return isl_stat_error
;
2837 active
[poly
->var
] = 1;
2839 rec
= isl_poly_as_rec(poly
);
2840 for (i
= 0; i
< rec
->n
; ++i
)
2841 if (poly_set_active(rec
->p
[i
], active
, d
) < 0)
2842 return isl_stat_error
;
2847 static isl_stat
set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2853 space
= isl_qpolynomial_peek_domain_space(qp
);
2854 d
= isl_space_dim(space
, isl_dim_all
);
2855 if (d
< 0 || !active
)
2856 return isl_stat_error
;
2858 for (i
= 0; i
< d
; ++i
)
2859 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2860 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2866 return poly_set_active(isl_qpolynomial_peek_poly(qp
), active
, d
);
2870 #define TYPE isl_qpolynomial
2872 #include "check_type_range_templ.c"
2874 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2875 enum isl_dim_type type
, unsigned first
, unsigned n
)
2879 isl_bool involves
= isl_bool_false
;
2885 return isl_bool_error
;
2887 return isl_bool_false
;
2889 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2890 return isl_bool_error
;
2891 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2892 type
== isl_dim_in
, return isl_bool_error
);
2894 space
= isl_qpolynomial_peek_domain_space(qp
);
2895 d
= isl_space_dim(space
, isl_dim_all
);
2897 return isl_bool_error
;
2898 active
= isl_calloc_array(qp
->dim
->ctx
, int, d
);
2899 if (set_active(qp
, active
) < 0)
2902 offset
= isl_qpolynomial_domain_var_offset(qp
, domain_type(type
));
2906 for (i
= 0; i
< n
; ++i
)
2907 if (active
[first
+ i
]) {
2908 involves
= isl_bool_true
;
2917 return isl_bool_error
;
2920 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2921 * of the divs that do appear in the quasi-polynomial.
2923 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2924 __isl_take isl_qpolynomial
*qp
)
2931 int *reordering
= NULL
;
2938 if (qp
->div
->n_row
== 0)
2941 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2943 return isl_qpolynomial_free(qp
);
2944 len
= qp
->div
->n_col
- 2;
2945 ctx
= isl_qpolynomial_get_ctx(qp
);
2946 active
= isl_calloc_array(ctx
, int, len
);
2950 if (poly_set_active(isl_qpolynomial_peek_poly(qp
), active
, len
) < 0)
2953 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2954 if (!active
[div_pos
+ i
]) {
2958 for (j
= 0; j
< i
; ++j
) {
2959 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ j
]))
2961 active
[div_pos
+ j
] = 1;
2971 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2975 for (i
= 0; i
< div_pos
; ++i
)
2979 n_div
= qp
->div
->n_row
;
2980 for (i
= 0; i
< n_div
; ++i
) {
2981 if (!active
[div_pos
+ i
]) {
2982 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2983 qp
->div
= isl_mat_drop_cols(qp
->div
,
2984 2 + div_pos
+ i
- skip
, 1);
2987 reordering
[div_pos
+ i
] = div_pos
+ i
- skip
;
2990 qp
->poly
= reorder(qp
->poly
, reordering
);
2992 if (!qp
->poly
|| !qp
->div
)
3002 isl_qpolynomial_free(qp
);
3006 __isl_give isl_poly
*isl_poly_drop(__isl_take isl_poly
*poly
,
3007 unsigned first
, unsigned n
)
3014 if (n
== 0 || poly
->var
< 0 || poly
->var
< first
)
3016 if (poly
->var
< first
+ n
) {
3017 poly
= replace_by_constant_term(poly
);
3018 return isl_poly_drop(poly
, first
, n
);
3020 poly
= isl_poly_cow(poly
);
3024 rec
= isl_poly_as_rec(poly
);
3028 for (i
= 0; i
< rec
->n
; ++i
) {
3029 rec
->p
[i
] = isl_poly_drop(rec
->p
[i
], first
, n
);
3036 isl_poly_free(poly
);
3040 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
3041 __isl_take isl_qpolynomial
*qp
,
3042 enum isl_dim_type type
, unsigned pos
, const char *s
)
3048 if (type
== isl_dim_out
)
3049 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
3050 "cannot set name of output/set dimension",
3051 return isl_qpolynomial_free(qp
));
3052 type
= domain_type(type
);
3053 space
= isl_qpolynomial_take_domain_space(qp
);
3054 space
= isl_space_set_dim_name(space
, type
, pos
, s
);
3055 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
3059 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
3060 __isl_take isl_qpolynomial
*qp
,
3061 enum isl_dim_type type
, unsigned first
, unsigned n
)
3068 if (type
== isl_dim_out
)
3069 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3070 "cannot drop output/set dimension",
3072 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3073 return isl_qpolynomial_free(qp
);
3074 type
= domain_type(type
);
3075 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3079 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
3080 type
== isl_dim_set
, goto error
);
3082 space
= isl_qpolynomial_take_domain_space(qp
);
3083 space
= isl_space_drop_dims(space
, type
, first
, n
);
3084 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
3086 qp
= isl_qpolynomial_cow(qp
);
3090 offset
= isl_qpolynomial_domain_var_offset(qp
, type
);
3095 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
3099 qp
->poly
= isl_poly_drop(qp
->poly
, first
, n
);
3105 isl_qpolynomial_free(qp
);
3109 /* Project the domain of the quasi-polynomial onto its parameter space.
3110 * The quasi-polynomial may not involve any of the domain dimensions.
3112 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
3113 __isl_take isl_qpolynomial
*qp
)
3119 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
3121 return isl_qpolynomial_free(qp
);
3122 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
3124 return isl_qpolynomial_free(qp
);
3126 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
3127 "polynomial involves some of the domain dimensions",
3128 return isl_qpolynomial_free(qp
));
3129 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
3130 space
= isl_qpolynomial_get_domain_space(qp
);
3131 space
= isl_space_params(space
);
3132 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
3136 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
3137 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
3147 if (eq
->n_eq
== 0) {
3148 isl_basic_set_free(eq
);
3152 qp
= isl_qpolynomial_cow(qp
);
3155 qp
->div
= isl_mat_cow(qp
->div
);
3159 total
= isl_basic_set_offset(eq
, isl_dim_div
);
3161 isl_int_init(denom
);
3162 for (i
= 0; i
< eq
->n_eq
; ++i
) {
3163 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
3164 if (j
< 0 || j
== 0 || j
>= total
)
3167 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
3168 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
3170 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
3171 &qp
->div
->row
[k
][0]);
3172 normalize_div(qp
, k
);
3175 if (isl_int_is_pos(eq
->eq
[i
][j
]))
3176 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
3177 isl_int_abs(denom
, eq
->eq
[i
][j
]);
3178 isl_int_set_si(eq
->eq
[i
][j
], 0);
3180 poly
= isl_poly_from_affine(qp
->dim
->ctx
,
3181 eq
->eq
[i
], denom
, total
);
3182 qp
->poly
= isl_poly_subs(qp
->poly
, j
- 1, 1, &poly
);
3183 isl_poly_free(poly
);
3185 isl_int_clear(denom
);
3190 isl_basic_set_free(eq
);
3192 qp
= substitute_non_divs(qp
);
3197 isl_basic_set_free(eq
);
3198 isl_qpolynomial_free(qp
);
3202 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3204 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
3205 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
3209 if (qp
->div
->n_row
> 0)
3210 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
3211 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
3213 isl_basic_set_free(eq
);
3214 isl_qpolynomial_free(qp
);
3218 /* Look for equalities among the variables shared by context and qp
3219 * and the integer divisions of qp, if any.
3220 * The equalities are then used to eliminate variables and/or integer
3221 * divisions from qp.
3223 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
3224 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3226 isl_local_space
*ls
;
3229 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3230 context
= isl_local_space_lift_set(ls
, context
);
3232 aff
= isl_set_affine_hull(context
);
3233 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
3236 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
3237 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3239 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
3240 isl_set
*dom_context
= isl_set_universe(space
);
3241 dom_context
= isl_set_intersect_params(dom_context
, context
);
3242 return isl_qpolynomial_gist(qp
, dom_context
);
3245 /* Return a zero isl_qpolynomial in the given space.
3247 * This is a helper function for isl_pw_*_as_* that ensures a uniform
3248 * interface over all piecewise types.
3250 static __isl_give isl_qpolynomial
*isl_qpolynomial_zero_in_space(
3251 __isl_take isl_space
*space
)
3253 return isl_qpolynomial_zero_on_domain(isl_space_domain(space
));
3256 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3259 #define PW isl_pw_qpolynomial
3261 #define BASE qpolynomial
3263 #define EL_IS_ZERO is_zero
3267 #define IS_ZERO is_zero
3270 #undef DEFAULT_IS_ZERO
3271 #define DEFAULT_IS_ZERO 1
3273 #include <isl_pw_templ.c>
3274 #include <isl_pw_un_op_templ.c>
3275 #include <isl_pw_add_disjoint_templ.c>
3276 #include <isl_pw_domain_reverse_templ.c>
3277 #include <isl_pw_eval.c>
3278 #include <isl_pw_fix_templ.c>
3279 #include <isl_pw_from_range_templ.c>
3280 #include <isl_pw_insert_dims_templ.c>
3281 #include <isl_pw_lift_templ.c>
3282 #include <isl_pw_morph_templ.c>
3283 #include <isl_pw_move_dims_templ.c>
3284 #include <isl_pw_neg_templ.c>
3285 #include <isl_pw_opt_templ.c>
3286 #include <isl_pw_split_dims_templ.c>
3287 #include <isl_pw_sub_templ.c>
3290 #define BASE pw_qpolynomial
3292 #include <isl_union_single.c>
3293 #include <isl_union_domain_reverse_templ.c>
3294 #include <isl_union_eval.c>
3295 #include <isl_union_neg.c>
3296 #include <isl_union_sub_templ.c>
3298 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
3306 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
3309 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
3312 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
3313 __isl_take isl_pw_qpolynomial
*pwqp1
,
3314 __isl_take isl_pw_qpolynomial
*pwqp2
)
3316 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
3319 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
3320 __isl_take isl_pw_qpolynomial
*pwqp1
,
3321 __isl_take isl_pw_qpolynomial
*pwqp2
)
3324 struct isl_pw_qpolynomial
*res
;
3326 if (!pwqp1
|| !pwqp2
)
3329 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3332 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3333 isl_pw_qpolynomial_free(pwqp2
);
3337 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3338 isl_pw_qpolynomial_free(pwqp1
);
3342 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3343 isl_pw_qpolynomial_free(pwqp1
);
3347 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3348 isl_pw_qpolynomial_free(pwqp2
);
3352 n
= pwqp1
->n
* pwqp2
->n
;
3353 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3355 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3356 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3357 struct isl_set
*common
;
3358 struct isl_qpolynomial
*prod
;
3359 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3360 isl_set_copy(pwqp2
->p
[j
].set
));
3361 if (isl_set_plain_is_empty(common
)) {
3362 isl_set_free(common
);
3366 prod
= isl_qpolynomial_mul(
3367 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3368 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3370 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3374 isl_pw_qpolynomial_free(pwqp1
);
3375 isl_pw_qpolynomial_free(pwqp2
);
3379 isl_pw_qpolynomial_free(pwqp1
);
3380 isl_pw_qpolynomial_free(pwqp2
);
3384 __isl_give isl_val
*isl_poly_eval(__isl_take isl_poly
*poly
,
3385 __isl_take isl_vec
*vec
)
3393 is_cst
= isl_poly_is_cst(poly
);
3398 res
= isl_poly_get_constant_val(poly
);
3399 isl_poly_free(poly
);
3403 rec
= isl_poly_as_rec(poly
);
3407 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
3409 base
= isl_val_rat_from_isl_int(poly
->ctx
,
3410 vec
->el
[1 + poly
->var
], vec
->el
[0]);
3412 res
= isl_poly_eval(isl_poly_copy(rec
->p
[rec
->n
- 1]),
3415 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3416 res
= isl_val_mul(res
, isl_val_copy(base
));
3417 res
= isl_val_add(res
, isl_poly_eval(isl_poly_copy(rec
->p
[i
]),
3418 isl_vec_copy(vec
)));
3422 isl_poly_free(poly
);
3426 isl_poly_free(poly
);
3431 /* Evaluate "qp" in the void point "pnt".
3432 * In particular, return the value NaN.
3434 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3435 __isl_take isl_point
*pnt
)
3439 ctx
= isl_point_get_ctx(pnt
);
3440 isl_qpolynomial_free(qp
);
3441 isl_point_free(pnt
);
3442 return isl_val_nan(ctx
);
3445 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3446 __isl_take isl_point
*pnt
)
3454 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3455 is_void
= isl_point_is_void(pnt
);
3459 return eval_void(qp
, pnt
);
3461 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3463 v
= isl_poly_eval(isl_qpolynomial_get_poly(qp
), ext
);
3465 isl_qpolynomial_free(qp
);
3466 isl_point_free(pnt
);
3470 isl_qpolynomial_free(qp
);
3471 isl_point_free(pnt
);
3475 int isl_poly_cmp(__isl_keep isl_poly_cst
*cst1
, __isl_keep isl_poly_cst
*cst2
)
3480 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3481 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3482 cmp
= isl_int_sgn(t
);
3487 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3488 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3489 unsigned first
, unsigned n
)
3498 if (type
== isl_dim_out
)
3499 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3500 "cannot insert output/set dimensions",
3502 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3503 return isl_qpolynomial_free(qp
);
3504 type
= domain_type(type
);
3505 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3508 qp
= isl_qpolynomial_cow(qp
);
3512 g_pos
= pos(qp
->dim
, type
) + first
;
3514 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3518 total
= qp
->div
->n_col
- 2;
3519 if (total
> g_pos
) {
3521 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3524 for (i
= 0; i
< total
- g_pos
; ++i
)
3526 qp
->poly
= expand(qp
->poly
, exp
, g_pos
);
3532 space
= isl_qpolynomial_take_domain_space(qp
);
3533 space
= isl_space_insert_dims(space
, type
, first
, n
);
3534 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
3538 isl_qpolynomial_free(qp
);
3542 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3543 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3547 pos
= isl_qpolynomial_dim(qp
, type
);
3549 return isl_qpolynomial_free(qp
);
3551 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3554 static int *reordering_move(isl_ctx
*ctx
,
3555 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3560 reordering
= isl_alloc_array(ctx
, int, len
);
3565 for (i
= 0; i
< dst
; ++i
)
3567 for (i
= 0; i
< n
; ++i
)
3568 reordering
[src
+ i
] = dst
+ i
;
3569 for (i
= 0; i
< src
- dst
; ++i
)
3570 reordering
[dst
+ i
] = dst
+ n
+ i
;
3571 for (i
= 0; i
< len
- src
- n
; ++i
)
3572 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3574 for (i
= 0; i
< src
; ++i
)
3576 for (i
= 0; i
< n
; ++i
)
3577 reordering
[src
+ i
] = dst
+ i
;
3578 for (i
= 0; i
< dst
- src
; ++i
)
3579 reordering
[src
+ n
+ i
] = src
+ i
;
3580 for (i
= 0; i
< len
- dst
- n
; ++i
)
3581 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3587 /* Move the "n" variables starting at "src_pos" of "qp" to "dst_pos".
3588 * Only modify the polynomial expression and the local variables of "qp".
3589 * The caller is responsible for modifying the space accordingly.
3591 static __isl_give isl_qpolynomial
*local_poly_move_dims(
3592 __isl_take isl_qpolynomial
*qp
,
3593 unsigned dst_pos
, unsigned src_pos
, unsigned n
)
3601 local
= isl_qpolynomial_take_local(qp
);
3602 local
= isl_local_move_vars(local
, dst_pos
, src_pos
, n
);
3603 qp
= isl_qpolynomial_restore_local(qp
, local
);
3606 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
3608 return isl_qpolynomial_free(qp
);
3609 ctx
= isl_qpolynomial_get_ctx(qp
);
3610 reordering
= reordering_move(ctx
, total
, dst_pos
, src_pos
, n
);
3612 return isl_qpolynomial_free(qp
);
3614 poly
= isl_qpolynomial_take_poly(qp
);
3615 poly
= reorder(poly
, reordering
);
3616 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
3622 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3623 __isl_take isl_qpolynomial
*qp
,
3624 enum isl_dim_type dst_type
, unsigned dst_pos
,
3625 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3630 isl_size src_off
, dst_off
;
3636 ctx
= isl_qpolynomial_get_ctx(qp
);
3637 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3638 isl_die(ctx
, isl_error_invalid
,
3639 "cannot move output/set dimension",
3640 return isl_qpolynomial_free(qp
));
3641 if (src_type
== isl_dim_div
|| dst_type
== isl_dim_div
)
3642 isl_die(ctx
, isl_error_invalid
, "cannot move local variables",
3643 return isl_qpolynomial_free(qp
));
3644 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3645 return isl_qpolynomial_free(qp
);
3646 if (dst_type
== isl_dim_in
)
3647 dst_type
= isl_dim_set
;
3648 if (src_type
== isl_dim_in
)
3649 src_type
= isl_dim_set
;
3652 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3653 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3656 src_off
= isl_qpolynomial_domain_var_offset(qp
, src_type
);
3657 dst_off
= isl_qpolynomial_domain_var_offset(qp
, dst_type
);
3658 if (src_off
< 0 || dst_off
< 0)
3659 return isl_qpolynomial_free(qp
);
3661 g_dst_pos
= dst_off
+ dst_pos
;
3662 g_src_pos
= src_off
+ src_pos
;
3663 if (dst_type
> src_type
)
3666 qp
= local_poly_move_dims(qp
, g_dst_pos
, g_src_pos
, n
);
3668 space
= isl_qpolynomial_take_domain_space(qp
);
3669 space
= isl_space_move_dims(space
, dst_type
, dst_pos
,
3670 src_type
, src_pos
, n
);
3671 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
3676 /* Given a quasi-polynomial on a domain (A -> B),
3677 * interchange A and B in the wrapped domain
3678 * to obtain a quasi-polynomial on the domain (B -> A).
3680 __isl_give isl_qpolynomial
*isl_qpolynomial_domain_reverse(
3681 __isl_take isl_qpolynomial
*qp
)
3684 isl_size n_in
, n_out
, offset
;
3686 space
= isl_qpolynomial_peek_domain_space(qp
);
3687 offset
= isl_space_offset(space
, isl_dim_set
);
3688 n_in
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_in
);
3689 n_out
= isl_space_wrapped_dim(space
, isl_dim_set
, isl_dim_out
);
3690 if (offset
< 0 || n_in
< 0 || n_out
< 0)
3691 return isl_qpolynomial_free(qp
);
3693 qp
= local_poly_move_dims(qp
, offset
, offset
+ n_in
, n_out
);
3695 space
= isl_qpolynomial_take_domain_space(qp
);
3696 space
= isl_space_wrapped_reverse(space
);
3697 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
3702 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3703 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3708 space
= isl_space_domain(space
);
3712 d
= isl_space_dim(space
, isl_dim_all
);
3713 poly
= d
< 0 ? NULL
: isl_poly_from_affine(space
->ctx
, f
, denom
, 1 + d
);
3715 return isl_qpolynomial_alloc(space
, 0, poly
);
3718 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3722 isl_qpolynomial
*qp
;
3727 ctx
= isl_aff_get_ctx(aff
);
3728 poly
= isl_poly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3731 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3732 aff
->ls
->div
->n_row
, poly
);
3736 isl_mat_free(qp
->div
);
3737 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3738 qp
->div
= isl_mat_cow(qp
->div
);
3743 qp
= reduce_divs(qp
);
3744 qp
= remove_redundant_divs(qp
);
3748 return isl_qpolynomial_free(qp
);
3751 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3752 __isl_take isl_pw_aff
*pwaff
)
3755 isl_pw_qpolynomial
*pwqp
;
3760 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3763 for (i
= 0; i
< pwaff
->n
; ++i
) {
3765 isl_qpolynomial
*qp
;
3767 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3768 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3769 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3772 isl_pw_aff_free(pwaff
);
3776 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3777 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3781 aff
= isl_constraint_get_bound(c
, type
, pos
);
3782 isl_constraint_free(c
);
3783 return isl_qpolynomial_from_aff(aff
);
3786 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3787 * in "qp" by subs[i].
3789 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3790 __isl_take isl_qpolynomial
*qp
,
3791 enum isl_dim_type type
, unsigned first
, unsigned n
,
3792 __isl_keep isl_qpolynomial
**subs
)
3804 if (type
== isl_dim_out
)
3805 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3806 "cannot substitute output/set dimension",
3808 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3809 return isl_qpolynomial_free(qp
);
3810 type
= domain_type(type
);
3812 for (i
= 0; i
< n
; ++i
)
3816 for (i
= 0; i
< n
; ++i
)
3817 if (isl_qpolynomial_check_equal_space(qp
, subs
[i
]) < 0)
3820 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3821 for (i
= 0; i
< n
; ++i
)
3822 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3824 first
+= pos(qp
->dim
, type
);
3826 polys
= isl_alloc_array(qp
->dim
->ctx
, struct isl_poly
*, n
);
3829 for (i
= 0; i
< n
; ++i
)
3830 polys
[i
] = subs
[i
]->poly
;
3832 poly
= isl_qpolynomial_take_poly(qp
);
3833 poly
= isl_poly_subs(poly
, first
, n
, polys
);
3834 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
3840 isl_qpolynomial_free(qp
);
3844 /* Extend "bset" with extra set dimensions for each integer division
3845 * in "qp" and then call "fn" with the extended bset and the polynomial
3846 * that results from replacing each of the integer divisions by the
3847 * corresponding extra set dimension.
3849 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3850 __isl_keep isl_basic_set
*bset
,
3851 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3852 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3855 isl_local_space
*ls
;
3857 isl_qpolynomial
*polynomial
;
3860 return isl_stat_error
;
3861 if (qp
->div
->n_row
== 0)
3862 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3865 space
= isl_space_copy(qp
->dim
);
3866 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3867 poly
= isl_qpolynomial_get_poly(qp
);
3868 polynomial
= isl_qpolynomial_alloc(space
, 0, poly
);
3869 bset
= isl_basic_set_copy(bset
);
3870 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3871 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3873 return fn(bset
, polynomial
, user
);
3876 /* Return total degree in variables first (inclusive) up to last (exclusive).
3878 int isl_poly_degree(__isl_keep isl_poly
*poly
, int first
, int last
)
3882 isl_bool is_zero
, is_cst
;
3885 is_zero
= isl_poly_is_zero(poly
);
3890 is_cst
= isl_poly_is_cst(poly
);
3893 if (is_cst
|| poly
->var
< first
)
3896 rec
= isl_poly_as_rec(poly
);
3900 for (i
= 0; i
< rec
->n
; ++i
) {
3903 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3908 d
= isl_poly_degree(rec
->p
[i
], first
, last
);
3909 if (poly
->var
< last
)
3918 /* Return total degree in set variables.
3920 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3928 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3929 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3930 if (ovar
< 0 || nvar
< 0)
3932 return isl_poly_degree(poly
->poly
, ovar
, ovar
+ nvar
);
3935 __isl_give isl_poly
*isl_poly_coeff(__isl_keep isl_poly
*poly
,
3936 unsigned pos
, int deg
)
3942 is_cst
= isl_poly_is_cst(poly
);
3945 if (is_cst
|| poly
->var
< pos
) {
3947 return isl_poly_copy(poly
);
3949 return isl_poly_zero(poly
->ctx
);
3952 rec
= isl_poly_as_rec(poly
);
3956 if (poly
->var
== pos
) {
3958 return isl_poly_copy(rec
->p
[deg
]);
3960 return isl_poly_zero(poly
->ctx
);
3963 poly
= isl_poly_copy(poly
);
3964 poly
= isl_poly_cow(poly
);
3965 rec
= isl_poly_as_rec(poly
);
3969 for (i
= 0; i
< rec
->n
; ++i
) {
3971 t
= isl_poly_coeff(rec
->p
[i
], pos
, deg
);
3974 isl_poly_free(rec
->p
[i
]);
3980 isl_poly_free(poly
);
3984 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3986 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3987 __isl_keep isl_qpolynomial
*qp
,
3988 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3997 if (type
== isl_dim_out
)
3998 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3999 "output/set dimension does not have a coefficient",
4001 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
4003 type
= domain_type(type
);
4005 g_pos
= pos(qp
->dim
, type
) + t_pos
;
4006 poly
= isl_poly_coeff(isl_qpolynomial_peek_poly(qp
), g_pos
, deg
);
4008 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
),
4009 qp
->div
->n_row
, poly
);
4012 isl_mat_free(c
->div
);
4013 c
->div
= isl_qpolynomial_get_local(qp
);
4018 isl_qpolynomial_free(c
);
4022 /* Homogenize the polynomial in the variables first (inclusive) up to
4023 * last (exclusive) by inserting powers of variable first.
4024 * Variable first is assumed not to appear in the input.
4026 __isl_give isl_poly
*isl_poly_homogenize(__isl_take isl_poly
*poly
, int deg
,
4027 int target
, int first
, int last
)
4030 isl_bool is_zero
, is_cst
;
4033 is_zero
= isl_poly_is_zero(poly
);
4035 return isl_poly_free(poly
);
4040 is_cst
= isl_poly_is_cst(poly
);
4042 return isl_poly_free(poly
);
4043 if (is_cst
|| poly
->var
< first
) {
4046 hom
= isl_poly_var_pow(poly
->ctx
, first
, target
- deg
);
4049 rec
= isl_poly_as_rec(hom
);
4050 rec
->p
[target
- deg
] = isl_poly_mul(rec
->p
[target
- deg
], poly
);
4055 poly
= isl_poly_cow(poly
);
4056 rec
= isl_poly_as_rec(poly
);
4060 for (i
= 0; i
< rec
->n
; ++i
) {
4061 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
4063 return isl_poly_free(poly
);
4066 rec
->p
[i
] = isl_poly_homogenize(rec
->p
[i
],
4067 poly
->var
< last
? deg
+ i
: i
, target
,
4075 isl_poly_free(poly
);
4079 /* Homogenize the polynomial in the set variables by introducing
4080 * powers of an extra set variable at position 0.
4082 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
4083 __isl_take isl_qpolynomial
*poly
)
4087 int deg
= isl_qpolynomial_degree(poly
);
4092 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
4093 poly
= isl_qpolynomial_cow(poly
);
4097 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
4098 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
4099 if (ovar
< 0 || nvar
< 0)
4100 return isl_qpolynomial_free(poly
);
4101 poly
->poly
= isl_poly_homogenize(poly
->poly
, 0, deg
, ovar
, ovar
+ nvar
);
4107 isl_qpolynomial_free(poly
);
4111 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
4112 __isl_take isl_mat
*div
)
4118 d
= isl_space_dim(space
, isl_dim_all
);
4124 term
= isl_calloc(space
->ctx
, struct isl_term
,
4125 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
4132 isl_int_init(term
->n
);
4133 isl_int_init(term
->d
);
4137 isl_space_free(space
);
4142 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
4151 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
4157 total
= isl_term_dim(term
, isl_dim_all
);
4161 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
4165 isl_int_set(dup
->n
, term
->n
);
4166 isl_int_set(dup
->d
, term
->d
);
4168 for (i
= 0; i
< total
; ++i
)
4169 dup
->pow
[i
] = term
->pow
[i
];
4174 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
4182 return isl_term_dup(term
);
4185 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
4190 if (--term
->ref
> 0)
4193 isl_space_free(term
->dim
);
4194 isl_mat_free(term
->div
);
4195 isl_int_clear(term
->n
);
4196 isl_int_clear(term
->d
);
4202 isl_size
isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
4207 return isl_size_error
;
4212 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
4213 case isl_dim_div
: return term
->div
->n_row
;
4214 case isl_dim_all
: dim
= isl_space_dim(term
->dim
, isl_dim_all
);
4216 return isl_size_error
;
4217 return dim
+ term
->div
->n_row
;
4218 default: return isl_size_error
;
4222 /* Return the space of "term".
4224 static __isl_keep isl_space
*isl_term_peek_space(__isl_keep isl_term
*term
)
4226 return term
? term
->dim
: NULL
;
4229 /* Return the offset of the first variable of type "type" within
4230 * the variables of "term".
4232 static isl_size
isl_term_offset(__isl_keep isl_term
*term
,
4233 enum isl_dim_type type
)
4237 space
= isl_term_peek_space(term
);
4239 return isl_size_error
;
4243 case isl_dim_set
: return isl_space_offset(space
, type
);
4244 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
4246 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4247 "invalid dimension type", return isl_size_error
);
4251 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
4253 return term
? term
->dim
->ctx
: NULL
;
4256 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
4260 isl_int_set(*n
, term
->n
);
4263 /* Return the coefficient of the term "term".
4265 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
4270 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
4275 #define TYPE isl_term
4277 #include "check_type_range_templ.c"
4279 isl_size
isl_term_get_exp(__isl_keep isl_term
*term
,
4280 enum isl_dim_type type
, unsigned pos
)
4284 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
4285 return isl_size_error
;
4286 offset
= isl_term_offset(term
, type
);
4288 return isl_size_error
;
4290 return term
->pow
[offset
+ pos
];
4293 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
4295 isl_local_space
*ls
;
4298 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
4301 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
4302 isl_mat_copy(term
->div
));
4303 aff
= isl_aff_alloc(ls
);
4307 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
4309 aff
= isl_aff_normalize(aff
);
4314 __isl_give isl_term
*isl_poly_foreach_term(__isl_keep isl_poly
*poly
,
4315 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
4316 __isl_take isl_term
*term
, void *user
)
4319 isl_bool is_zero
, is_bad
, is_cst
;
4322 is_zero
= isl_poly_is_zero(poly
);
4323 if (is_zero
< 0 || !term
)
4329 is_cst
= isl_poly_is_cst(poly
);
4330 is_bad
= isl_poly_is_nan(poly
);
4331 if (is_bad
>= 0 && !is_bad
)
4332 is_bad
= isl_poly_is_infty(poly
);
4333 if (is_bad
>= 0 && !is_bad
)
4334 is_bad
= isl_poly_is_neginfty(poly
);
4335 if (is_cst
< 0 || is_bad
< 0)
4336 return isl_term_free(term
);
4338 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4339 "cannot handle NaN/infty polynomial",
4340 return isl_term_free(term
));
4344 cst
= isl_poly_as_cst(poly
);
4347 term
= isl_term_cow(term
);
4350 isl_int_set(term
->n
, cst
->n
);
4351 isl_int_set(term
->d
, cst
->d
);
4352 if (fn(isl_term_copy(term
), user
) < 0)
4357 rec
= isl_poly_as_rec(poly
);
4361 for (i
= 0; i
< rec
->n
; ++i
) {
4362 term
= isl_term_cow(term
);
4365 term
->pow
[poly
->var
] = i
;
4366 term
= isl_poly_foreach_term(rec
->p
[i
], fn
, term
, user
);
4370 term
= isl_term_cow(term
);
4373 term
->pow
[poly
->var
] = 0;
4377 isl_term_free(term
);
4381 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
4382 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
4388 return isl_stat_error
;
4390 local
= isl_qpolynomial_get_local(qp
);
4391 term
= isl_term_alloc(isl_space_copy(qp
->dim
), local
);
4393 return isl_stat_error
;
4395 term
= isl_poly_foreach_term(isl_qpolynomial_peek_poly(qp
),
4398 isl_term_free(term
);
4400 return term
? isl_stat_ok
: isl_stat_error
;
4403 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
4406 isl_qpolynomial
*qp
;
4410 n
= isl_term_dim(term
, isl_dim_all
);
4412 term
= isl_term_free(term
);
4416 poly
= isl_poly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4417 for (i
= 0; i
< n
; ++i
) {
4420 poly
= isl_poly_mul(poly
,
4421 isl_poly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4424 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
),
4425 term
->div
->n_row
, poly
);
4428 isl_mat_free(qp
->div
);
4429 qp
->div
= isl_mat_copy(term
->div
);
4433 isl_term_free(term
);
4436 isl_qpolynomial_free(qp
);
4437 isl_term_free(term
);
4441 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4442 __isl_take isl_space
*space
)
4446 isl_size total
, d_set
, d_qp
;
4451 if (isl_space_is_equal(qp
->dim
, space
)) {
4452 isl_space_free(space
);
4456 qp
= isl_qpolynomial_cow(qp
);
4460 d_set
= isl_space_dim(space
, isl_dim_set
);
4461 d_qp
= isl_qpolynomial_domain_dim(qp
, isl_dim_set
);
4462 extra
= d_set
- d_qp
;
4463 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4464 if (d_set
< 0 || d_qp
< 0 || total
< 0)
4466 if (qp
->div
->n_row
) {
4469 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4472 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4474 qp
->poly
= expand(qp
->poly
, exp
, total
);
4479 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4482 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4483 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4485 isl_space_free(isl_qpolynomial_take_domain_space(qp
));
4486 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
4490 isl_space_free(space
);
4491 isl_qpolynomial_free(qp
);
4495 /* For each parameter or variable that does not appear in qp,
4496 * first eliminate the variable from all constraints and then set it to zero.
4498 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4499 __isl_keep isl_qpolynomial
*qp
)
4507 d
= isl_set_dim(set
, isl_dim_all
);
4511 active
= isl_calloc_array(set
->ctx
, int, d
);
4512 if (set_active(qp
, active
) < 0)
4515 for (i
= 0; i
< d
; ++i
)
4524 nparam
= isl_set_dim(set
, isl_dim_param
);
4525 nvar
= isl_set_dim(set
, isl_dim_set
);
4526 if (nparam
< 0 || nvar
< 0)
4528 for (i
= 0; i
< nparam
; ++i
) {
4531 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4532 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4534 for (i
= 0; i
< nvar
; ++i
) {
4535 if (active
[nparam
+ i
])
4537 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4538 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4550 struct isl_opt_data
{
4551 isl_qpolynomial
*qp
;
4557 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4559 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4562 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4566 } else if (data
->max
) {
4567 data
->opt
= isl_val_max(data
->opt
, val
);
4569 data
->opt
= isl_val_min(data
->opt
, val
);
4575 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4576 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4578 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4584 is_cst
= isl_poly_is_cst(isl_qpolynomial_peek_poly(qp
));
4589 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4590 isl_qpolynomial_free(qp
);
4594 set
= fix_inactive(set
, qp
);
4597 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4601 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4604 isl_qpolynomial_free(qp
);
4608 isl_qpolynomial_free(qp
);
4609 isl_val_free(data
.opt
);
4613 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4614 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4621 isl_mat
*mat
, *diag
;
4623 qp
= isl_qpolynomial_cow(qp
);
4625 space
= isl_qpolynomial_peek_domain_space(qp
);
4626 if (isl_morph_check_applies(morph
, space
) < 0)
4629 ctx
= isl_qpolynomial_get_ctx(qp
);
4630 n_sub
= morph
->inv
->n_row
- 1;
4631 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4632 n_sub
+= qp
->div
->n_row
;
4633 subs
= isl_calloc_array(ctx
, struct isl_poly
*, n_sub
);
4637 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4638 subs
[i
] = isl_poly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4639 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4640 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4641 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4642 subs
[morph
->inv
->n_row
- 1 + i
] =
4643 isl_poly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4645 qp
->poly
= isl_poly_subs(qp
->poly
, 0, n_sub
, subs
);
4647 for (i
= 0; i
< n_sub
; ++i
)
4648 isl_poly_free(subs
[i
]);
4651 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4652 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4653 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4654 mat
= isl_mat_diagonal(mat
, diag
);
4655 qp
->div
= isl_mat_product(qp
->div
, mat
);
4657 if (!qp
->poly
|| !qp
->div
)
4660 isl_space_free(isl_qpolynomial_take_domain_space(qp
));
4661 space
= isl_space_copy(morph
->ran
->dim
);
4662 qp
= isl_qpolynomial_restore_domain_space(qp
, space
);
4664 isl_morph_free(morph
);
4668 isl_qpolynomial_free(qp
);
4669 isl_morph_free(morph
);
4673 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4674 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4675 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4677 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4678 &isl_pw_qpolynomial_mul
);
4681 /* Reorder the dimension of "qp" according to the given reordering.
4683 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4684 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4693 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4697 local
= isl_qpolynomial_take_local(qp
);
4698 local
= isl_local_reorder(local
, isl_reordering_copy(r
));
4699 qp
= isl_qpolynomial_restore_local(qp
, local
);
4701 poly
= isl_qpolynomial_take_poly(qp
);
4702 poly
= reorder(poly
, r
->pos
);
4703 qp
= isl_qpolynomial_restore_poly(qp
, poly
);
4705 space
= isl_reordering_get_space(r
);
4706 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4708 isl_reordering_free(r
);
4711 isl_qpolynomial_free(qp
);
4712 isl_reordering_free(r
);
4716 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4717 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4719 isl_space
*domain_space
;
4720 isl_bool equal_params
;
4722 domain_space
= isl_qpolynomial_peek_domain_space(qp
);
4723 equal_params
= isl_space_has_equal_params(domain_space
, model
);
4724 if (equal_params
< 0)
4726 if (!equal_params
) {
4727 isl_reordering
*exp
;
4729 exp
= isl_parameter_alignment_reordering(domain_space
, model
);
4730 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4733 isl_space_free(model
);
4736 isl_space_free(model
);
4737 isl_qpolynomial_free(qp
);
4741 struct isl_split_periods_data
{
4743 isl_pw_qpolynomial
*res
;
4746 /* Create a slice where the integer division "div" has the fixed value "v".
4747 * In particular, if "div" refers to floor(f/m), then create a slice
4749 * m v <= f <= m v + (m - 1)
4754 * -f + m v + (m - 1) >= 0
4756 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4757 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4760 isl_basic_set
*bset
= NULL
;
4763 total
= isl_space_dim(space
, isl_dim_all
);
4764 if (total
< 0 || !qp
)
4767 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4769 k
= isl_basic_set_alloc_inequality(bset
);
4772 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4773 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4775 k
= isl_basic_set_alloc_inequality(bset
);
4778 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4779 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4780 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4781 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4783 isl_space_free(space
);
4784 return isl_set_from_basic_set(bset
);
4786 isl_basic_set_free(bset
);
4787 isl_space_free(space
);
4791 static isl_stat
split_periods(__isl_take isl_set
*set
,
4792 __isl_take isl_qpolynomial
*qp
, void *user
);
4794 /* Create a slice of the domain "set" such that integer division "div"
4795 * has the fixed value "v" and add the results to data->res,
4796 * replacing the integer division by "v" in "qp".
4798 static isl_stat
set_div(__isl_take isl_set
*set
,
4799 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4800 struct isl_split_periods_data
*data
)
4807 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4808 set
= isl_set_intersect(set
, slice
);
4810 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4814 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4815 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ div
]))
4817 isl_int_addmul(qp
->div
->row
[i
][1],
4818 qp
->div
->row
[i
][2 + div_pos
+ div
], v
);
4819 isl_int_set_si(qp
->div
->row
[i
][2 + div_pos
+ div
], 0);
4822 cst
= isl_poly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4823 qp
= substitute_div(qp
, div
, cst
);
4825 return split_periods(set
, qp
, data
);
4828 isl_qpolynomial_free(qp
);
4829 return isl_stat_error
;
4832 /* Split the domain "set" such that integer division "div"
4833 * has a fixed value (ranging from "min" to "max") on each slice
4834 * and add the results to data->res.
4836 static isl_stat
split_div(__isl_take isl_set
*set
,
4837 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4838 struct isl_split_periods_data
*data
)
4840 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4841 isl_set
*set_i
= isl_set_copy(set
);
4842 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4844 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4848 isl_qpolynomial_free(qp
);
4852 isl_qpolynomial_free(qp
);
4853 return isl_stat_error
;
4856 /* If "qp" refers to any integer division
4857 * that can only attain "max_periods" distinct values on "set"
4858 * then split the domain along those distinct values.
4859 * Add the results (or the original if no splitting occurs)
4862 static isl_stat
split_periods(__isl_take isl_set
*set
,
4863 __isl_take isl_qpolynomial
*qp
, void *user
)
4866 isl_pw_qpolynomial
*pwqp
;
4867 struct isl_split_periods_data
*data
;
4870 isl_stat r
= isl_stat_ok
;
4872 data
= (struct isl_split_periods_data
*)user
;
4877 if (qp
->div
->n_row
== 0) {
4878 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4879 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4883 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4889 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4890 enum isl_lp_result lp_res
;
4892 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + div_pos
,
4893 qp
->div
->n_row
) != -1)
4896 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4897 set
->ctx
->one
, &min
, NULL
, NULL
);
4898 if (lp_res
== isl_lp_error
)
4900 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4902 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4904 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4905 set
->ctx
->one
, &max
, NULL
, NULL
);
4906 if (lp_res
== isl_lp_error
)
4908 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4910 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4912 isl_int_sub(max
, max
, min
);
4913 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4914 isl_int_add(max
, max
, min
);
4919 if (i
< qp
->div
->n_row
) {
4920 r
= split_div(set
, qp
, i
, min
, max
, data
);
4922 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4923 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4935 isl_qpolynomial_free(qp
);
4936 return isl_stat_error
;
4939 /* If any quasi-polynomial in pwqp refers to any integer division
4940 * that can only attain "max_periods" distinct values on its domain
4941 * then split the domain along those distinct values.
4943 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4944 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4946 struct isl_split_periods_data data
;
4948 data
.max_periods
= max_periods
;
4949 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4951 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4954 isl_pw_qpolynomial_free(pwqp
);
4958 isl_pw_qpolynomial_free(data
.res
);
4959 isl_pw_qpolynomial_free(pwqp
);
4963 /* Construct a piecewise quasipolynomial that is constant on the given
4964 * domain. In particular, it is
4967 * infinity if cst == -1
4969 * If cst == -1, then explicitly check whether the domain is empty and,
4970 * if so, return 0 instead.
4972 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4973 __isl_take isl_basic_set
*bset
, int cst
)
4976 isl_qpolynomial
*qp
;
4978 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4983 bset
= isl_basic_set_params(bset
);
4984 space
= isl_basic_set_get_space(bset
);
4986 qp
= isl_qpolynomial_infty_on_domain(space
);
4988 qp
= isl_qpolynomial_zero_on_domain(space
);
4990 qp
= isl_qpolynomial_one_on_domain(space
);
4991 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4994 /* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4995 * "fn" is the function that is called on each factor.
4996 * "pwpq" collects the results.
4998 struct isl_multiplicative_call_data_pw_qpolynomial
{
4999 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
);
5000 isl_pw_qpolynomial
*pwqp
;
5003 /* Call "fn" on "bset" and return the result,
5004 * but first check if "bset" has any redundant constraints or
5005 * implicit equality constraints.
5006 * If so, there may be further opportunities for detecting factors or
5007 * removing equality constraints, so recursively call
5008 * the top-level isl_basic_set_multiplicative_call.
5010 static __isl_give isl_pw_qpolynomial
*multiplicative_call_base(
5011 __isl_take isl_basic_set
*bset
,
5012 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
5014 isl_size n1
, n2
, n_eq
;
5016 n1
= isl_basic_set_n_constraint(bset
);
5018 bset
= isl_basic_set_free(bset
);
5019 bset
= isl_basic_set_remove_redundancies(bset
);
5020 bset
= isl_basic_set_detect_equalities(bset
);
5021 n2
= isl_basic_set_n_constraint(bset
);
5022 n_eq
= isl_basic_set_n_equality(bset
);
5023 if (n2
< 0 || n_eq
< 0)
5024 bset
= isl_basic_set_free(bset
);
5025 else if (n2
< n1
|| n_eq
> 0)
5026 return isl_basic_set_multiplicative_call(bset
, fn
);
5030 /* isl_factorizer_every_factor_basic_set callback that applies
5031 * data->fn to the factor "bset" and multiplies in the result
5034 static isl_bool
multiplicative_call_factor_pw_qpolynomial(
5035 __isl_keep isl_basic_set
*bset
, void *user
)
5037 struct isl_multiplicative_call_data_pw_qpolynomial
*data
= user
;
5038 isl_pw_qpolynomial
*res
;
5040 bset
= isl_basic_set_copy(bset
);
5041 res
= multiplicative_call_base(bset
, data
->fn
);
5042 data
->pwqp
= isl_pw_qpolynomial_mul(data
->pwqp
, res
);
5044 return isl_bool_error
;
5046 return isl_bool_true
;
5049 /* Factor bset, call fn on each of the factors and return the product.
5051 * If no factors can be found, simply call fn on the input.
5052 * Otherwise, construct the factors based on the factorizer,
5053 * call fn on each factor and compute the product.
5055 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
5056 __isl_take isl_basic_set
*bset
,
5057 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
5059 struct isl_multiplicative_call_data_pw_qpolynomial data
= { fn
};
5063 isl_qpolynomial
*qp
;
5066 f
= isl_basic_set_factorizer(bset
);
5069 if (f
->n_group
== 0) {
5070 isl_factorizer_free(f
);
5071 return multiplicative_call_base(bset
, fn
);
5074 space
= isl_basic_set_get_space(bset
);
5075 space
= isl_space_params(space
);
5076 set
= isl_set_universe(isl_space_copy(space
));
5077 qp
= isl_qpolynomial_one_on_domain(space
);
5078 data
.pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
5080 every
= isl_factorizer_every_factor_basic_set(f
,
5081 &multiplicative_call_factor_pw_qpolynomial
, &data
);
5083 data
.pwqp
= isl_pw_qpolynomial_free(data
.pwqp
);
5085 isl_basic_set_free(bset
);
5086 isl_factorizer_free(f
);
5090 isl_basic_set_free(bset
);
5094 /* Factor bset, call fn on each of the factors and return the product.
5095 * The function is assumed to evaluate to zero on empty domains,
5096 * to one on zero-dimensional domains and to infinity on unbounded domains
5097 * and will not be called explicitly on zero-dimensional or unbounded domains.
5099 * We first check for some special cases and remove all equalities.
5100 * Then we hand over control to compressed_multiplicative_call.
5102 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
5103 __isl_take isl_basic_set
*bset
,
5104 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
5109 isl_pw_qpolynomial
*pwqp
;
5114 if (isl_basic_set_plain_is_empty(bset
))
5115 return constant_on_domain(bset
, 0);
5117 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
5121 return constant_on_domain(bset
, 1);
5123 bounded
= isl_basic_set_is_bounded(bset
);
5127 return constant_on_domain(bset
, -1);
5129 if (bset
->n_eq
== 0)
5130 return compressed_multiplicative_call(bset
, fn
);
5132 morph
= isl_basic_set_full_compression(bset
);
5133 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
5135 pwqp
= compressed_multiplicative_call(bset
, fn
);
5137 morph
= isl_morph_dom_params(morph
);
5138 morph
= isl_morph_ran_params(morph
);
5139 morph
= isl_morph_inverse(morph
);
5141 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
5145 isl_basic_set_free(bset
);
5149 /* Drop all floors in "qp", turning each integer division [a/m] into
5150 * a rational division a/m. If "down" is set, then the integer division
5151 * is replaced by (a-(m-1))/m instead.
5153 static __isl_give isl_qpolynomial
*qp_drop_floors(
5154 __isl_take isl_qpolynomial
*qp
, int down
)
5161 if (qp
->div
->n_row
== 0)
5164 qp
= isl_qpolynomial_cow(qp
);
5168 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
5170 isl_int_sub(qp
->div
->row
[i
][1],
5171 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
5172 isl_int_add_ui(qp
->div
->row
[i
][1],
5173 qp
->div
->row
[i
][1], 1);
5175 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
5176 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
5177 qp
= substitute_div(qp
, i
, s
);
5185 /* Drop all floors in "pwqp", turning each integer division [a/m] into
5186 * a rational division a/m.
5188 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
5189 __isl_take isl_pw_qpolynomial
*pwqp
)
5196 if (isl_pw_qpolynomial_is_zero(pwqp
))
5199 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
5203 for (i
= 0; i
< pwqp
->n
; ++i
) {
5204 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
5211 isl_pw_qpolynomial_free(pwqp
);
5215 /* Adjust all the integer divisions in "qp" such that they are at least
5216 * one over the given orthant (identified by "signs"). This ensures
5217 * that they will still be non-negative even after subtracting (m-1)/m.
5219 * In particular, f is replaced by f' + v, changing f = [a/m]
5220 * to f' = [(a - m v)/m].
5221 * If the constant term k in a is smaller than m,
5222 * the constant term of v is set to floor(k/m) - 1.
5223 * For any other term, if the coefficient c and the variable x have
5224 * the same sign, then no changes are needed.
5225 * Otherwise, if the variable is positive (and c is negative),
5226 * then the coefficient of x in v is set to floor(c/m).
5227 * If the variable is negative (and c is positive),
5228 * then the coefficient of x in v is set to ceil(c/m).
5230 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
5238 qp
= isl_qpolynomial_cow(qp
);
5239 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
5241 return isl_qpolynomial_free(qp
);
5242 qp
->div
= isl_mat_cow(qp
->div
);
5246 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
5248 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
5249 isl_int
*row
= qp
->div
->row
[i
];
5253 if (isl_int_lt(row
[1], row
[0])) {
5254 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
5255 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
5256 isl_int_submul(row
[1], row
[0], v
->el
[0]);
5258 for (j
= 0; j
< div_pos
; ++j
) {
5259 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
5262 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
5264 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
5265 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
5267 for (j
= 0; j
< i
; ++j
) {
5268 if (isl_int_sgn(row
[2 + div_pos
+ j
]) >= 0)
5270 isl_int_fdiv_q(v
->el
[1 + div_pos
+ j
],
5271 row
[2 + div_pos
+ j
], row
[0]);
5272 isl_int_submul(row
[2 + div_pos
+ j
],
5273 row
[0], v
->el
[1 + div_pos
+ j
]);
5275 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
5276 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
5278 isl_seq_combine(qp
->div
->row
[j
] + 1,
5279 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
5280 qp
->div
->row
[j
][2 + div_pos
+ i
], v
->el
,
5283 isl_int_set_si(v
->el
[1 + div_pos
+ i
], 1);
5284 s
= isl_poly_from_affine(qp
->dim
->ctx
, v
->el
,
5285 qp
->div
->ctx
->one
, v
->size
);
5286 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ i
, 1, &s
);
5296 isl_qpolynomial_free(qp
);
5300 struct isl_to_poly_data
{
5302 isl_pw_qpolynomial
*res
;
5303 isl_qpolynomial
*qp
;
5306 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5307 * We first make all integer divisions positive and then split the
5308 * quasipolynomials into terms with sign data->sign (the direction
5309 * of the requested approximation) and terms with the opposite sign.
5310 * In the first set of terms, each integer division [a/m] is
5311 * overapproximated by a/m, while in the second it is underapproximated
5314 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
5315 int *signs
, void *user
)
5317 struct isl_to_poly_data
*data
= user
;
5318 isl_pw_qpolynomial
*t
;
5319 isl_qpolynomial
*qp
, *up
, *down
;
5321 qp
= isl_qpolynomial_copy(data
->qp
);
5322 qp
= make_divs_pos(qp
, signs
);
5324 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
5325 up
= qp_drop_floors(up
, 0);
5326 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
5327 down
= qp_drop_floors(down
, 1);
5329 isl_qpolynomial_free(qp
);
5330 qp
= isl_qpolynomial_add(up
, down
);
5332 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
5333 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
5338 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5339 * the polynomial will be an overapproximation. If "sign" is negative,
5340 * it will be an underapproximation. If "sign" is zero, the approximation
5341 * will lie somewhere in between.
5343 * In particular, is sign == 0, we simply drop the floors, turning
5344 * the integer divisions into rational divisions.
5345 * Otherwise, we split the domains into orthants, make all integer divisions
5346 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5347 * depending on the requested sign and the sign of the term in which
5348 * the integer division appears.
5350 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
5351 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
5354 struct isl_to_poly_data data
;
5357 return pwqp_drop_floors(pwqp
);
5363 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
5365 for (i
= 0; i
< pwqp
->n
; ++i
) {
5366 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
5367 isl_pw_qpolynomial
*t
;
5368 t
= isl_pw_qpolynomial_alloc(
5369 isl_set_copy(pwqp
->p
[i
].set
),
5370 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
5371 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
5374 data
.qp
= pwqp
->p
[i
].qp
;
5375 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
5376 &to_polynomial_on_orthant
, &data
) < 0)
5380 isl_pw_qpolynomial_free(pwqp
);
5384 isl_pw_qpolynomial_free(pwqp
);
5385 isl_pw_qpolynomial_free(data
.res
);
5389 static __isl_give isl_pw_qpolynomial
*poly_entry(
5390 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
5394 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
5397 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
5398 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
5400 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
5401 &poly_entry
, &sign
);
5404 /* Return an isl_aff that is equivalent to "qp".
5406 __isl_give isl_aff
*isl_qpolynomial_as_aff(__isl_take isl_qpolynomial
*qp
)
5408 isl_local_space
*ls
;
5413 is_affine
= isl_qpolynomial_isa_aff(qp
);
5417 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
5418 "input quasi-polynomial not affine", goto error
);
5419 ls
= isl_qpolynomial_get_domain_local_space(qp
);
5420 vec
= isl_qpolynomial_extract_affine(qp
);
5421 aff
= isl_aff_alloc_vec(ls
, vec
);
5422 isl_qpolynomial_free(qp
);
5425 isl_qpolynomial_free(qp
);
5429 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
5430 __isl_take isl_qpolynomial
*qp
)
5432 return isl_basic_map_from_aff(isl_qpolynomial_as_aff(qp
));