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 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
416 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
)
418 qp
= isl_qpolynomial_cow(qp
);
422 isl_space_free(qp
->dim
);
427 isl_qpolynomial_free(qp
);
428 isl_space_free(space
);
432 /* Reset the space of "qp". This function is called from isl_pw_templ.c
433 * and doesn't know if the space of an element object is represented
434 * directly or through its domain. It therefore passes along both.
436 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
437 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
438 __isl_take isl_space
*domain
)
440 isl_space_free(space
);
441 return isl_qpolynomial_reset_domain_space(qp
, domain
);
444 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
446 return qp
? qp
->dim
->ctx
: NULL
;
449 /* Return the domain space of "qp".
451 static __isl_keep isl_space
*isl_qpolynomial_peek_domain_space(
452 __isl_keep isl_qpolynomial
*qp
)
454 return qp
? qp
->dim
: NULL
;
457 /* Return a copy of the domain space of "qp".
459 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
460 __isl_keep isl_qpolynomial
*qp
)
462 return isl_space_copy(isl_qpolynomial_peek_domain_space(qp
));
466 #define TYPE isl_qpolynomial
468 #define PEEK_SPACE peek_domain_space
471 #include "isl_type_has_equal_space_bin_templ.c"
473 #include "isl_type_check_equal_space_templ.c"
477 /* Return a copy of the local space on which "qp" is defined.
479 static __isl_give isl_local_space
*isl_qpolynomial_get_domain_local_space(
480 __isl_keep isl_qpolynomial
*qp
)
487 space
= isl_qpolynomial_get_domain_space(qp
);
488 return isl_local_space_alloc_div(space
, isl_mat_copy(qp
->div
));
491 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
496 space
= isl_space_copy(qp
->dim
);
497 space
= isl_space_from_domain(space
);
498 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
502 /* Return the number of variables of the given type in the domain of "qp".
504 isl_size
isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
505 enum isl_dim_type type
)
510 space
= isl_qpolynomial_peek_domain_space(qp
);
513 return isl_size_error
;
514 if (type
== isl_dim_div
)
515 return qp
->div
->n_row
;
516 dim
= isl_space_dim(space
, type
);
518 return isl_size_error
;
519 if (type
== isl_dim_all
) {
522 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
524 return isl_size_error
;
530 /* Given the type of a dimension of an isl_qpolynomial,
531 * return the type of the corresponding dimension in its domain.
532 * This function is only called for "type" equal to isl_dim_in or
535 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
537 return type
== isl_dim_in
? isl_dim_set
: type
;
540 /* Externally, an isl_qpolynomial has a map space, but internally, the
541 * ls field corresponds to the domain of that space.
543 isl_size
isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
544 enum isl_dim_type type
)
547 return isl_size_error
;
548 if (type
== isl_dim_out
)
550 type
= domain_type(type
);
551 return isl_qpolynomial_domain_dim(qp
, type
);
554 /* Return the offset of the first variable of type "type" within
555 * the variables of the domain of "qp".
557 static isl_size
isl_qpolynomial_domain_var_offset(
558 __isl_keep isl_qpolynomial
*qp
, enum isl_dim_type type
)
562 space
= isl_qpolynomial_peek_domain_space(qp
);
564 return isl_size_error
;
568 case isl_dim_set
: return isl_space_offset(space
, type
);
569 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
572 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
573 "invalid dimension type", return isl_size_error
);
577 /* Return the offset of the first coefficient of type "type" in
578 * the domain of "qp".
580 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
581 enum isl_dim_type type
)
589 return 1 + isl_qpolynomial_domain_var_offset(qp
, type
);
595 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
597 return qp
? isl_poly_is_zero(qp
->poly
) : isl_bool_error
;
600 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
602 return qp
? isl_poly_is_one(qp
->poly
) : isl_bool_error
;
605 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
607 return qp
? isl_poly_is_nan(qp
->poly
) : isl_bool_error
;
610 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
612 return qp
? isl_poly_is_infty(qp
->poly
) : isl_bool_error
;
615 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
617 return qp
? isl_poly_is_neginfty(qp
->poly
) : isl_bool_error
;
620 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
622 return qp
? isl_poly_sgn(qp
->poly
) : 0;
625 static void poly_free_cst(__isl_take isl_poly_cst
*cst
)
627 isl_int_clear(cst
->n
);
628 isl_int_clear(cst
->d
);
631 static void poly_free_rec(__isl_take isl_poly_rec
*rec
)
635 for (i
= 0; i
< rec
->n
; ++i
)
636 isl_poly_free(rec
->p
[i
]);
639 __isl_give isl_poly
*isl_poly_copy(__isl_keep isl_poly
*poly
)
648 __isl_give isl_poly
*isl_poly_dup_cst(__isl_keep isl_poly
*poly
)
653 cst
= isl_poly_as_cst(poly
);
657 dup
= isl_poly_as_cst(isl_poly_zero(poly
->ctx
));
660 isl_int_set(dup
->n
, cst
->n
);
661 isl_int_set(dup
->d
, cst
->d
);
666 __isl_give isl_poly
*isl_poly_dup_rec(__isl_keep isl_poly
*poly
)
672 rec
= isl_poly_as_rec(poly
);
676 dup
= isl_poly_alloc_rec(poly
->ctx
, poly
->var
, rec
->n
);
680 for (i
= 0; i
< rec
->n
; ++i
) {
681 dup
->p
[i
] = isl_poly_copy(rec
->p
[i
]);
689 isl_poly_free(&dup
->poly
);
693 __isl_give isl_poly
*isl_poly_dup(__isl_keep isl_poly
*poly
)
697 is_cst
= isl_poly_is_cst(poly
);
701 return isl_poly_dup_cst(poly
);
703 return isl_poly_dup_rec(poly
);
706 __isl_give isl_poly
*isl_poly_cow(__isl_take isl_poly
*poly
)
714 return isl_poly_dup(poly
);
717 __isl_null isl_poly
*isl_poly_free(__isl_take isl_poly
*poly
)
726 poly_free_cst((isl_poly_cst
*) poly
);
728 poly_free_rec((isl_poly_rec
*) poly
);
730 isl_ctx_deref(poly
->ctx
);
735 static void isl_poly_cst_reduce(__isl_keep isl_poly_cst
*cst
)
740 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
741 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
742 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
743 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
748 __isl_give isl_poly
*isl_poly_sum_cst(__isl_take isl_poly
*poly1
,
749 __isl_take isl_poly
*poly2
)
754 poly1
= isl_poly_cow(poly1
);
755 if (!poly1
|| !poly2
)
758 cst1
= isl_poly_as_cst(poly1
);
759 cst2
= isl_poly_as_cst(poly2
);
761 if (isl_int_eq(cst1
->d
, cst2
->d
))
762 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
764 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
765 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
766 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
769 isl_poly_cst_reduce(cst1
);
771 isl_poly_free(poly2
);
774 isl_poly_free(poly1
);
775 isl_poly_free(poly2
);
779 static __isl_give isl_poly
*replace_by_zero(__isl_take isl_poly
*poly
)
787 return isl_poly_zero(ctx
);
790 static __isl_give isl_poly
*replace_by_constant_term(__isl_take isl_poly
*poly
)
798 rec
= isl_poly_as_rec(poly
);
801 cst
= isl_poly_copy(rec
->p
[0]);
809 __isl_give isl_poly
*isl_poly_sum(__isl_take isl_poly
*poly1
,
810 __isl_take isl_poly
*poly2
)
813 isl_bool is_zero
, is_nan
, is_cst
;
814 isl_poly_rec
*rec1
, *rec2
;
816 if (!poly1
|| !poly2
)
819 is_nan
= isl_poly_is_nan(poly1
);
823 isl_poly_free(poly2
);
827 is_nan
= isl_poly_is_nan(poly2
);
831 isl_poly_free(poly1
);
835 is_zero
= isl_poly_is_zero(poly1
);
839 isl_poly_free(poly1
);
843 is_zero
= isl_poly_is_zero(poly2
);
847 isl_poly_free(poly2
);
851 if (poly1
->var
< poly2
->var
)
852 return isl_poly_sum(poly2
, poly1
);
854 if (poly2
->var
< poly1
->var
) {
858 is_infty
= isl_poly_is_infty(poly2
);
859 if (is_infty
>= 0 && !is_infty
)
860 is_infty
= isl_poly_is_neginfty(poly2
);
864 isl_poly_free(poly1
);
867 poly1
= isl_poly_cow(poly1
);
868 rec
= isl_poly_as_rec(poly1
);
871 rec
->p
[0] = isl_poly_sum(rec
->p
[0], poly2
);
873 poly1
= replace_by_constant_term(poly1
);
877 is_cst
= isl_poly_is_cst(poly1
);
881 return isl_poly_sum_cst(poly1
, poly2
);
883 rec1
= isl_poly_as_rec(poly1
);
884 rec2
= isl_poly_as_rec(poly2
);
888 if (rec1
->n
< rec2
->n
)
889 return isl_poly_sum(poly2
, poly1
);
891 poly1
= isl_poly_cow(poly1
);
892 rec1
= isl_poly_as_rec(poly1
);
896 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
899 rec1
->p
[i
] = isl_poly_sum(rec1
->p
[i
],
900 isl_poly_copy(rec2
->p
[i
]));
903 if (i
!= rec1
->n
- 1)
905 is_zero
= isl_poly_is_zero(rec1
->p
[i
]);
909 isl_poly_free(rec1
->p
[i
]);
915 poly1
= replace_by_zero(poly1
);
916 else if (rec1
->n
== 1)
917 poly1
= replace_by_constant_term(poly1
);
919 isl_poly_free(poly2
);
923 isl_poly_free(poly1
);
924 isl_poly_free(poly2
);
928 __isl_give isl_poly
*isl_poly_cst_add_isl_int(__isl_take isl_poly
*poly
,
933 poly
= isl_poly_cow(poly
);
937 cst
= isl_poly_as_cst(poly
);
939 isl_int_addmul(cst
->n
, cst
->d
, v
);
944 __isl_give isl_poly
*isl_poly_add_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
949 is_cst
= isl_poly_is_cst(poly
);
951 return isl_poly_free(poly
);
953 return isl_poly_cst_add_isl_int(poly
, v
);
955 poly
= isl_poly_cow(poly
);
956 rec
= isl_poly_as_rec(poly
);
960 rec
->p
[0] = isl_poly_add_isl_int(rec
->p
[0], v
);
970 __isl_give isl_poly
*isl_poly_cst_mul_isl_int(__isl_take isl_poly
*poly
,
976 is_zero
= isl_poly_is_zero(poly
);
978 return isl_poly_free(poly
);
982 poly
= isl_poly_cow(poly
);
986 cst
= isl_poly_as_cst(poly
);
988 isl_int_mul(cst
->n
, cst
->n
, v
);
993 __isl_give isl_poly
*isl_poly_mul_isl_int(__isl_take isl_poly
*poly
, isl_int v
)
999 is_cst
= isl_poly_is_cst(poly
);
1001 return isl_poly_free(poly
);
1003 return isl_poly_cst_mul_isl_int(poly
, v
);
1005 poly
= isl_poly_cow(poly
);
1006 rec
= isl_poly_as_rec(poly
);
1010 for (i
= 0; i
< rec
->n
; ++i
) {
1011 rec
->p
[i
] = isl_poly_mul_isl_int(rec
->p
[i
], v
);
1018 isl_poly_free(poly
);
1022 /* Multiply the constant polynomial "poly" by "v".
1024 static __isl_give isl_poly
*isl_poly_cst_scale_val(__isl_take isl_poly
*poly
,
1025 __isl_keep isl_val
*v
)
1030 is_zero
= isl_poly_is_zero(poly
);
1032 return isl_poly_free(poly
);
1036 poly
= isl_poly_cow(poly
);
1040 cst
= isl_poly_as_cst(poly
);
1042 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
1043 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
1044 isl_poly_cst_reduce(cst
);
1049 /* Multiply the polynomial "poly" by "v".
1051 static __isl_give isl_poly
*isl_poly_scale_val(__isl_take isl_poly
*poly
,
1052 __isl_keep isl_val
*v
)
1058 is_cst
= isl_poly_is_cst(poly
);
1060 return isl_poly_free(poly
);
1062 return isl_poly_cst_scale_val(poly
, v
);
1064 poly
= isl_poly_cow(poly
);
1065 rec
= isl_poly_as_rec(poly
);
1069 for (i
= 0; i
< rec
->n
; ++i
) {
1070 rec
->p
[i
] = isl_poly_scale_val(rec
->p
[i
], v
);
1077 isl_poly_free(poly
);
1081 __isl_give isl_poly
*isl_poly_mul_cst(__isl_take isl_poly
*poly1
,
1082 __isl_take isl_poly
*poly2
)
1087 poly1
= isl_poly_cow(poly1
);
1088 if (!poly1
|| !poly2
)
1091 cst1
= isl_poly_as_cst(poly1
);
1092 cst2
= isl_poly_as_cst(poly2
);
1094 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
1095 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
1097 isl_poly_cst_reduce(cst1
);
1099 isl_poly_free(poly2
);
1102 isl_poly_free(poly1
);
1103 isl_poly_free(poly2
);
1107 __isl_give isl_poly
*isl_poly_mul_rec(__isl_take isl_poly
*poly1
,
1108 __isl_take isl_poly
*poly2
)
1112 isl_poly_rec
*res
= NULL
;
1116 rec1
= isl_poly_as_rec(poly1
);
1117 rec2
= isl_poly_as_rec(poly2
);
1120 size
= rec1
->n
+ rec2
->n
- 1;
1121 res
= isl_poly_alloc_rec(poly1
->ctx
, poly1
->var
, size
);
1125 for (i
= 0; i
< rec1
->n
; ++i
) {
1126 res
->p
[i
] = isl_poly_mul(isl_poly_copy(rec2
->p
[0]),
1127 isl_poly_copy(rec1
->p
[i
]));
1132 for (; i
< size
; ++i
) {
1133 res
->p
[i
] = isl_poly_zero(poly1
->ctx
);
1138 for (i
= 0; i
< rec1
->n
; ++i
) {
1139 for (j
= 1; j
< rec2
->n
; ++j
) {
1141 poly
= isl_poly_mul(isl_poly_copy(rec2
->p
[j
]),
1142 isl_poly_copy(rec1
->p
[i
]));
1143 res
->p
[i
+ j
] = isl_poly_sum(res
->p
[i
+ j
], poly
);
1149 isl_poly_free(poly1
);
1150 isl_poly_free(poly2
);
1154 isl_poly_free(poly1
);
1155 isl_poly_free(poly2
);
1156 isl_poly_free(&res
->poly
);
1160 __isl_give isl_poly
*isl_poly_mul(__isl_take isl_poly
*poly1
,
1161 __isl_take isl_poly
*poly2
)
1163 isl_bool is_zero
, is_nan
, is_one
, is_cst
;
1165 if (!poly1
|| !poly2
)
1168 is_nan
= isl_poly_is_nan(poly1
);
1172 isl_poly_free(poly2
);
1176 is_nan
= isl_poly_is_nan(poly2
);
1180 isl_poly_free(poly1
);
1184 is_zero
= isl_poly_is_zero(poly1
);
1188 isl_poly_free(poly2
);
1192 is_zero
= isl_poly_is_zero(poly2
);
1196 isl_poly_free(poly1
);
1200 is_one
= isl_poly_is_one(poly1
);
1204 isl_poly_free(poly1
);
1208 is_one
= isl_poly_is_one(poly2
);
1212 isl_poly_free(poly2
);
1216 if (poly1
->var
< poly2
->var
)
1217 return isl_poly_mul(poly2
, poly1
);
1219 if (poly2
->var
< poly1
->var
) {
1224 is_infty
= isl_poly_is_infty(poly2
);
1225 if (is_infty
>= 0 && !is_infty
)
1226 is_infty
= isl_poly_is_neginfty(poly2
);
1230 isl_ctx
*ctx
= poly1
->ctx
;
1231 isl_poly_free(poly1
);
1232 isl_poly_free(poly2
);
1233 return isl_poly_nan(ctx
);
1235 poly1
= isl_poly_cow(poly1
);
1236 rec
= isl_poly_as_rec(poly1
);
1240 for (i
= 0; i
< rec
->n
; ++i
) {
1241 rec
->p
[i
] = isl_poly_mul(rec
->p
[i
],
1242 isl_poly_copy(poly2
));
1246 isl_poly_free(poly2
);
1250 is_cst
= isl_poly_is_cst(poly1
);
1254 return isl_poly_mul_cst(poly1
, poly2
);
1256 return isl_poly_mul_rec(poly1
, poly2
);
1258 isl_poly_free(poly1
);
1259 isl_poly_free(poly2
);
1263 __isl_give isl_poly
*isl_poly_pow(__isl_take isl_poly
*poly
, unsigned power
)
1273 res
= isl_poly_copy(poly
);
1275 res
= isl_poly_one(poly
->ctx
);
1277 while (power
>>= 1) {
1278 poly
= isl_poly_mul(poly
, isl_poly_copy(poly
));
1280 res
= isl_poly_mul(res
, isl_poly_copy(poly
));
1283 isl_poly_free(poly
);
1287 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1288 unsigned n_div
, __isl_take isl_poly
*poly
)
1290 struct isl_qpolynomial
*qp
= NULL
;
1293 total
= isl_space_dim(space
, isl_dim_all
);
1294 if (total
< 0 || !poly
)
1297 if (!isl_space_is_set(space
))
1298 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1299 "domain of polynomial should be a set", goto error
);
1301 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1306 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1315 isl_space_free(space
);
1316 isl_poly_free(poly
);
1317 isl_qpolynomial_free(qp
);
1321 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1330 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1332 struct isl_qpolynomial
*dup
;
1337 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1338 isl_poly_copy(qp
->poly
));
1341 isl_mat_free(dup
->div
);
1342 dup
->div
= isl_mat_copy(qp
->div
);
1348 isl_qpolynomial_free(dup
);
1352 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1360 return isl_qpolynomial_dup(qp
);
1363 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1364 __isl_take isl_qpolynomial
*qp
)
1372 isl_space_free(qp
->dim
);
1373 isl_mat_free(qp
->div
);
1374 isl_poly_free(qp
->poly
);
1380 __isl_give isl_poly
*isl_poly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1386 rec
= isl_poly_alloc_rec(ctx
, pos
, 1 + power
);
1389 for (i
= 0; i
< 1 + power
; ++i
) {
1390 rec
->p
[i
] = isl_poly_zero(ctx
);
1395 cst
= isl_poly_as_cst(rec
->p
[power
]);
1396 isl_int_set_si(cst
->n
, 1);
1400 isl_poly_free(&rec
->poly
);
1404 /* r array maps original positions to new positions.
1406 static __isl_give isl_poly
*reorder(__isl_take isl_poly
*poly
, int *r
)
1414 is_cst
= isl_poly_is_cst(poly
);
1416 return isl_poly_free(poly
);
1420 rec
= isl_poly_as_rec(poly
);
1424 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
1426 base
= isl_poly_var_pow(poly
->ctx
, r
[poly
->var
], 1);
1427 res
= reorder(isl_poly_copy(rec
->p
[rec
->n
- 1]), r
);
1429 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1430 res
= isl_poly_mul(res
, isl_poly_copy(base
));
1431 res
= isl_poly_sum(res
, reorder(isl_poly_copy(rec
->p
[i
]), r
));
1434 isl_poly_free(base
);
1435 isl_poly_free(poly
);
1439 isl_poly_free(poly
);
1443 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1444 __isl_keep isl_mat
*div2
)
1449 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1450 div1
->n_col
>= div2
->n_col
,
1451 return isl_bool_error
);
1453 if (div1
->n_row
== div2
->n_row
)
1454 return isl_mat_is_equal(div1
, div2
);
1456 n_row
= div1
->n_row
;
1457 n_col
= div1
->n_col
;
1458 div1
->n_row
= div2
->n_row
;
1459 div1
->n_col
= div2
->n_col
;
1461 equal
= isl_mat_is_equal(div1
, div2
);
1463 div1
->n_row
= n_row
;
1464 div1
->n_col
= n_col
;
1469 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1473 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1474 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1479 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1482 struct isl_div_sort_info
{
1487 static int div_sort_cmp(const void *p1
, const void *p2
)
1489 const struct isl_div_sort_info
*i1
, *i2
;
1490 i1
= (const struct isl_div_sort_info
*) p1
;
1491 i2
= (const struct isl_div_sort_info
*) p2
;
1493 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1496 /* Sort divs and remove duplicates.
1498 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1503 struct isl_div_sort_info
*array
= NULL
;
1504 int *pos
= NULL
, *at
= NULL
;
1505 int *reordering
= NULL
;
1510 if (qp
->div
->n_row
<= 1)
1513 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
1515 return isl_qpolynomial_free(qp
);
1517 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1519 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1520 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1521 len
= qp
->div
->n_col
- 2;
1522 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1523 if (!array
|| !pos
|| !at
|| !reordering
)
1526 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1527 array
[i
].div
= qp
->div
;
1533 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1536 for (i
= 0; i
< div_pos
; ++i
)
1539 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1540 if (pos
[array
[i
].row
] == i
)
1542 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1543 pos
[at
[i
]] = pos
[array
[i
].row
];
1544 at
[pos
[array
[i
].row
]] = at
[i
];
1545 at
[i
] = array
[i
].row
;
1546 pos
[array
[i
].row
] = i
;
1550 for (i
= 0; i
< len
- div_pos
; ++i
) {
1552 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1553 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1554 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1555 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1556 2 + div_pos
+ i
- skip
);
1557 qp
->div
= isl_mat_drop_cols(qp
->div
,
1558 2 + div_pos
+ i
- skip
, 1);
1561 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1564 qp
->poly
= reorder(qp
->poly
, reordering
);
1566 if (!qp
->poly
|| !qp
->div
)
1580 isl_qpolynomial_free(qp
);
1584 static __isl_give isl_poly
*expand(__isl_take isl_poly
*poly
, int *exp
,
1591 is_cst
= isl_poly_is_cst(poly
);
1593 return isl_poly_free(poly
);
1597 if (poly
->var
< first
)
1600 if (exp
[poly
->var
- first
] == poly
->var
- first
)
1603 poly
= isl_poly_cow(poly
);
1607 poly
->var
= exp
[poly
->var
- first
] + first
;
1609 rec
= isl_poly_as_rec(poly
);
1613 for (i
= 0; i
< rec
->n
; ++i
) {
1614 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1621 isl_poly_free(poly
);
1625 static __isl_give isl_qpolynomial
*with_merged_divs(
1626 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1627 __isl_take isl_qpolynomial
*qp2
),
1628 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1632 isl_mat
*div
= NULL
;
1635 qp1
= isl_qpolynomial_cow(qp1
);
1636 qp2
= isl_qpolynomial_cow(qp2
);
1641 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1642 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1644 n_div1
= qp1
->div
->n_row
;
1645 n_div2
= qp2
->div
->n_row
;
1646 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1647 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1648 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1651 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1655 isl_mat_free(qp1
->div
);
1656 qp1
->div
= isl_mat_copy(div
);
1657 isl_mat_free(qp2
->div
);
1658 qp2
->div
= isl_mat_copy(div
);
1660 qp1
->poly
= expand(qp1
->poly
, exp1
, div
->n_col
- div
->n_row
- 2);
1661 qp2
->poly
= expand(qp2
->poly
, exp2
, div
->n_col
- div
->n_row
- 2);
1663 if (!qp1
->poly
|| !qp2
->poly
)
1670 return fn(qp1
, qp2
);
1675 isl_qpolynomial_free(qp1
);
1676 isl_qpolynomial_free(qp2
);
1680 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1681 __isl_take isl_qpolynomial
*qp2
)
1683 isl_bool compatible
;
1685 qp1
= isl_qpolynomial_cow(qp1
);
1687 if (isl_qpolynomial_check_equal_space(qp1
, qp2
) < 0)
1690 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1691 return isl_qpolynomial_add(qp2
, qp1
);
1693 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1697 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1699 qp1
->poly
= isl_poly_sum(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1703 isl_qpolynomial_free(qp2
);
1707 isl_qpolynomial_free(qp1
);
1708 isl_qpolynomial_free(qp2
);
1712 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1713 __isl_keep isl_set
*dom
,
1714 __isl_take isl_qpolynomial
*qp1
,
1715 __isl_take isl_qpolynomial
*qp2
)
1717 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1718 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1722 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1723 __isl_take isl_qpolynomial
*qp2
)
1725 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1728 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1729 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1731 if (isl_int_is_zero(v
))
1734 qp
= isl_qpolynomial_cow(qp
);
1738 qp
->poly
= isl_poly_add_isl_int(qp
->poly
, v
);
1744 isl_qpolynomial_free(qp
);
1749 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1754 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1757 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1758 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1760 if (isl_int_is_one(v
))
1763 if (qp
&& isl_int_is_zero(v
)) {
1764 isl_qpolynomial
*zero
;
1765 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1766 isl_qpolynomial_free(qp
);
1770 qp
= isl_qpolynomial_cow(qp
);
1774 qp
->poly
= isl_poly_mul_isl_int(qp
->poly
, v
);
1780 isl_qpolynomial_free(qp
);
1784 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1785 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1787 return isl_qpolynomial_mul_isl_int(qp
, v
);
1790 /* Multiply "qp" by "v".
1792 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1793 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1798 if (!isl_val_is_rat(v
))
1799 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1800 "expecting rational factor", goto error
);
1802 if (isl_val_is_one(v
)) {
1807 if (isl_val_is_zero(v
)) {
1810 space
= isl_qpolynomial_get_domain_space(qp
);
1811 isl_qpolynomial_free(qp
);
1813 return isl_qpolynomial_zero_on_domain(space
);
1816 qp
= isl_qpolynomial_cow(qp
);
1820 qp
->poly
= isl_poly_scale_val(qp
->poly
, v
);
1822 qp
= isl_qpolynomial_free(qp
);
1828 isl_qpolynomial_free(qp
);
1832 /* Divide "qp" by "v".
1834 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1835 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1840 if (!isl_val_is_rat(v
))
1841 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1842 "expecting rational factor", goto error
);
1843 if (isl_val_is_zero(v
))
1844 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1845 "cannot scale down by zero", goto error
);
1847 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1850 isl_qpolynomial_free(qp
);
1854 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1855 __isl_take isl_qpolynomial
*qp2
)
1857 isl_bool compatible
;
1859 qp1
= isl_qpolynomial_cow(qp1
);
1861 if (isl_qpolynomial_check_equal_space(qp1
, qp2
) < 0)
1864 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1865 return isl_qpolynomial_mul(qp2
, qp1
);
1867 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1871 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1873 qp1
->poly
= isl_poly_mul(qp1
->poly
, isl_poly_copy(qp2
->poly
));
1877 isl_qpolynomial_free(qp2
);
1881 isl_qpolynomial_free(qp1
);
1882 isl_qpolynomial_free(qp2
);
1886 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1889 qp
= isl_qpolynomial_cow(qp
);
1894 qp
->poly
= isl_poly_pow(qp
->poly
, power
);
1900 isl_qpolynomial_free(qp
);
1904 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1905 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1912 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1916 for (i
= 0; i
< pwqp
->n
; ++i
) {
1917 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1919 return isl_pw_qpolynomial_free(pwqp
);
1925 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1926 __isl_take isl_space
*domain
)
1930 return isl_qpolynomial_alloc(domain
, 0, isl_poly_zero(domain
->ctx
));
1933 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1934 __isl_take isl_space
*domain
)
1938 return isl_qpolynomial_alloc(domain
, 0, isl_poly_one(domain
->ctx
));
1941 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1942 __isl_take isl_space
*domain
)
1946 return isl_qpolynomial_alloc(domain
, 0, isl_poly_infty(domain
->ctx
));
1949 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1950 __isl_take isl_space
*domain
)
1954 return isl_qpolynomial_alloc(domain
, 0, isl_poly_neginfty(domain
->ctx
));
1957 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1958 __isl_take isl_space
*domain
)
1962 return isl_qpolynomial_alloc(domain
, 0, isl_poly_nan(domain
->ctx
));
1965 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1966 __isl_take isl_space
*domain
,
1969 struct isl_qpolynomial
*qp
;
1972 qp
= isl_qpolynomial_zero_on_domain(domain
);
1976 cst
= isl_poly_as_cst(qp
->poly
);
1977 isl_int_set(cst
->n
, v
);
1982 isl_bool
isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1983 isl_int
*n
, isl_int
*d
)
1989 return isl_bool_error
;
1991 is_cst
= isl_poly_is_cst(qp
->poly
);
1992 if (is_cst
< 0 || !is_cst
)
1995 cst
= isl_poly_as_cst(qp
->poly
);
1997 return isl_bool_error
;
2000 isl_int_set(*n
, cst
->n
);
2002 isl_int_set(*d
, cst
->d
);
2004 return isl_bool_true
;
2007 /* Return the constant term of "poly".
2009 static __isl_give isl_val
*isl_poly_get_constant_val(__isl_keep isl_poly
*poly
)
2017 while ((is_cst
= isl_poly_is_cst(poly
)) == isl_bool_false
) {
2020 rec
= isl_poly_as_rec(poly
);
2028 cst
= isl_poly_as_cst(poly
);
2031 return isl_val_rat_from_isl_int(cst
->poly
.ctx
, cst
->n
, cst
->d
);
2034 /* Return the constant term of "qp".
2036 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
2037 __isl_keep isl_qpolynomial
*qp
)
2042 return isl_poly_get_constant_val(qp
->poly
);
2045 isl_bool
isl_poly_is_affine(__isl_keep isl_poly
*poly
)
2051 return isl_bool_error
;
2054 return isl_bool_true
;
2056 rec
= isl_poly_as_rec(poly
);
2058 return isl_bool_error
;
2061 return isl_bool_false
;
2063 isl_assert(poly
->ctx
, rec
->n
> 1, return isl_bool_error
);
2065 is_cst
= isl_poly_is_cst(rec
->p
[1]);
2066 if (is_cst
< 0 || !is_cst
)
2069 return isl_poly_is_affine(rec
->p
[0]);
2072 isl_bool
isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
2075 return isl_bool_error
;
2077 if (qp
->div
->n_row
> 0)
2078 return isl_bool_false
;
2080 return isl_poly_is_affine(qp
->poly
);
2083 static void update_coeff(__isl_keep isl_vec
*aff
,
2084 __isl_keep isl_poly_cst
*cst
, int pos
)
2089 if (isl_int_is_zero(cst
->n
))
2094 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
2095 isl_int_divexact(f
, cst
->d
, gcd
);
2096 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
2097 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
2098 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
2103 int isl_poly_update_affine(__isl_keep isl_poly
*poly
, __isl_keep isl_vec
*aff
)
2111 if (poly
->var
< 0) {
2114 cst
= isl_poly_as_cst(poly
);
2117 update_coeff(aff
, cst
, 0);
2121 rec
= isl_poly_as_rec(poly
);
2124 isl_assert(poly
->ctx
, rec
->n
== 2, return -1);
2126 cst
= isl_poly_as_cst(rec
->p
[1]);
2129 update_coeff(aff
, cst
, 1 + poly
->var
);
2131 return isl_poly_update_affine(rec
->p
[0], aff
);
2134 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
2135 __isl_keep isl_qpolynomial
*qp
)
2140 d
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2144 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
2148 isl_seq_clr(aff
->el
+ 1, 1 + d
);
2149 isl_int_set_si(aff
->el
[0], 1);
2151 if (isl_poly_update_affine(qp
->poly
, aff
) < 0)
2160 /* Compare two quasi-polynomials.
2162 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2163 * than "qp2" and 0 if they are equal.
2165 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2166 __isl_keep isl_qpolynomial
*qp2
)
2177 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2181 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2185 return isl_poly_plain_cmp(qp1
->poly
, qp2
->poly
);
2188 /* Is "qp1" obviously equal to "qp2"?
2190 * NaN is not equal to anything, not even to another NaN.
2192 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2193 __isl_keep isl_qpolynomial
*qp2
)
2198 return isl_bool_error
;
2200 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2201 return isl_bool_false
;
2203 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2204 if (equal
< 0 || !equal
)
2207 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2208 if (equal
< 0 || !equal
)
2211 return isl_poly_is_equal(qp1
->poly
, qp2
->poly
);
2214 static isl_stat
poly_update_den(__isl_keep isl_poly
*poly
, isl_int
*d
)
2220 is_cst
= isl_poly_is_cst(poly
);
2222 return isl_stat_error
;
2225 cst
= isl_poly_as_cst(poly
);
2227 return isl_stat_error
;
2228 isl_int_lcm(*d
, *d
, cst
->d
);
2232 rec
= isl_poly_as_rec(poly
);
2234 return isl_stat_error
;
2236 for (i
= 0; i
< rec
->n
; ++i
)
2237 poly_update_den(rec
->p
[i
], d
);
2242 __isl_give isl_val
*isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
)
2248 d
= isl_val_one(isl_qpolynomial_get_ctx(qp
));
2251 if (poly_update_den(qp
->poly
, &d
->n
) < 0)
2252 return isl_val_free(d
);
2256 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2257 __isl_take isl_space
*domain
, int pos
, int power
)
2259 struct isl_ctx
*ctx
;
2266 return isl_qpolynomial_alloc(domain
, 0,
2267 isl_poly_var_pow(ctx
, pos
, power
));
2270 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2271 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2273 if (isl_space_check_is_set(domain
) < 0)
2275 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2278 pos
+= isl_space_offset(domain
, type
);
2280 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2282 isl_space_free(domain
);
2286 __isl_give isl_poly
*isl_poly_subs(__isl_take isl_poly
*poly
,
2287 unsigned first
, unsigned n
, __isl_keep isl_poly
**subs
)
2292 isl_poly
*base
, *res
;
2294 is_cst
= isl_poly_is_cst(poly
);
2296 return isl_poly_free(poly
);
2300 if (poly
->var
< first
)
2303 rec
= isl_poly_as_rec(poly
);
2307 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
2309 if (poly
->var
>= first
+ n
)
2310 base
= isl_poly_var_pow(poly
->ctx
, poly
->var
, 1);
2312 base
= isl_poly_copy(subs
[poly
->var
- first
]);
2314 res
= isl_poly_subs(isl_poly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2315 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2317 t
= isl_poly_subs(isl_poly_copy(rec
->p
[i
]), first
, n
, subs
);
2318 res
= isl_poly_mul(res
, isl_poly_copy(base
));
2319 res
= isl_poly_sum(res
, t
);
2322 isl_poly_free(base
);
2323 isl_poly_free(poly
);
2327 isl_poly_free(poly
);
2331 __isl_give isl_poly
*isl_poly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2332 isl_int denom
, unsigned len
)
2337 isl_assert(ctx
, len
>= 1, return NULL
);
2339 poly
= isl_poly_rat_cst(ctx
, f
[0], denom
);
2340 for (i
= 0; i
< len
- 1; ++i
) {
2344 if (isl_int_is_zero(f
[1 + i
]))
2347 c
= isl_poly_rat_cst(ctx
, f
[1 + i
], denom
);
2348 t
= isl_poly_var_pow(ctx
, i
, 1);
2349 t
= isl_poly_mul(c
, t
);
2350 poly
= isl_poly_sum(poly
, t
);
2356 /* Remove common factor of non-constant terms and denominator.
2358 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2360 isl_ctx
*ctx
= qp
->div
->ctx
;
2361 unsigned total
= qp
->div
->n_col
- 2;
2363 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2364 isl_int_gcd(ctx
->normalize_gcd
,
2365 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2366 if (isl_int_is_one(ctx
->normalize_gcd
))
2369 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2370 ctx
->normalize_gcd
, total
);
2371 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2372 ctx
->normalize_gcd
);
2373 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2374 ctx
->normalize_gcd
);
2377 /* Replace the integer division identified by "div" by the polynomial "s".
2378 * The integer division is assumed not to appear in the definition
2379 * of any other integer divisions.
2381 static __isl_give isl_qpolynomial
*substitute_div(
2382 __isl_take isl_qpolynomial
*qp
, int div
, __isl_take isl_poly
*s
)
2392 qp
= isl_qpolynomial_cow(qp
);
2396 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2399 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ div
, 1, &s
);
2403 ctx
= isl_qpolynomial_get_ctx(qp
);
2404 reordering
= isl_alloc_array(ctx
, int, div_pos
+ qp
->div
->n_row
);
2407 for (i
= 0; i
< div_pos
+ div
; ++i
)
2409 for (i
= div_pos
+ div
+ 1; i
< div_pos
+ qp
->div
->n_row
; ++i
)
2410 reordering
[i
] = i
- 1;
2411 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2412 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + div_pos
+ div
, 1);
2413 qp
->poly
= reorder(qp
->poly
, reordering
);
2416 if (!qp
->poly
|| !qp
->div
)
2422 isl_qpolynomial_free(qp
);
2427 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2428 * divisions because d is equal to 1 by their definition, i.e., e.
2430 static __isl_give isl_qpolynomial
*substitute_non_divs(
2431 __isl_take isl_qpolynomial
*qp
)
2437 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2439 return isl_qpolynomial_free(qp
);
2441 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2442 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2444 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2445 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
2447 isl_seq_combine(qp
->div
->row
[j
] + 1,
2448 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2449 qp
->div
->row
[j
][2 + div_pos
+ i
],
2450 qp
->div
->row
[i
] + 1, 1 + div_pos
+ i
);
2451 isl_int_set_si(qp
->div
->row
[j
][2 + div_pos
+ i
], 0);
2452 normalize_div(qp
, j
);
2454 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2455 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2456 qp
= substitute_div(qp
, i
, s
);
2463 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2464 * with d the denominator. When replacing the coefficient e of x by
2465 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2466 * inside the division, so we need to add floor(e/d) * x outside.
2467 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2468 * to adjust the coefficient of x in each later div that depends on the
2469 * current div "div" and also in the affine expressions in the rows of "mat"
2470 * (if they too depend on "div").
2472 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2473 __isl_keep isl_mat
**mat
)
2477 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2480 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2481 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2482 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2484 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2485 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2486 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2487 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2488 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2489 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2491 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2492 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2498 /* Check if the last non-zero coefficient is bigger that half of the
2499 * denominator. If so, we will invert the div to further reduce the number
2500 * of distinct divs that may appear.
2501 * If the last non-zero coefficient is exactly half the denominator,
2502 * then we continue looking for earlier coefficients that are bigger
2503 * than half the denominator.
2505 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2510 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2511 if (isl_int_is_zero(div
->row
[row
][i
]))
2513 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2514 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2515 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2525 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2526 * We only invert the coefficients of e (and the coefficient of q in
2527 * later divs and in the rows of "mat"). After calling this function, the
2528 * coefficients of e should be reduced again.
2530 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2531 __isl_keep isl_mat
**mat
)
2533 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2535 isl_seq_neg(qp
->div
->row
[div
] + 1,
2536 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2537 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2538 isl_int_add(qp
->div
->row
[div
][1],
2539 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2540 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2541 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2542 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2545 /* Reduce all divs of "qp" to have coefficients
2546 * in the interval [0, d-1], with d the denominator and such that the
2547 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2548 * The modifications to the integer divisions need to be reflected
2549 * in the factors of the polynomial that refer to the original
2550 * integer divisions. To this end, the modifications are collected
2551 * as a set of affine expressions and then plugged into the polynomial.
2553 * After the reduction, some divs may have become redundant or identical,
2554 * so we call substitute_non_divs and sort_divs. If these functions
2555 * eliminate divs or merge two or more divs into one, the coefficients
2556 * of the enclosing divs may have to be reduced again, so we call
2557 * ourselves recursively if the number of divs decreases.
2559 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2566 isl_size n_div
, total
, new_n_div
;
2568 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2569 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2570 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2571 if (total
< 0 || n_div
< 0)
2572 return isl_qpolynomial_free(qp
);
2573 ctx
= isl_qpolynomial_get_ctx(qp
);
2574 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2576 for (i
= 0; i
< n_div
; ++i
)
2577 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2579 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2580 normalize_div(qp
, i
);
2581 reduce_div(qp
, i
, &mat
);
2582 if (needs_invert(qp
->div
, i
)) {
2583 invert_div(qp
, i
, &mat
);
2584 reduce_div(qp
, i
, &mat
);
2590 s
= isl_alloc_array(ctx
, struct isl_poly
*, n_div
);
2593 for (i
= 0; i
< n_div
; ++i
)
2594 s
[i
] = isl_poly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2596 qp
->poly
= isl_poly_subs(qp
->poly
, o_div
- 1, n_div
, s
);
2597 for (i
= 0; i
< n_div
; ++i
)
2598 isl_poly_free(s
[i
]);
2605 qp
= substitute_non_divs(qp
);
2607 new_n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2609 return isl_qpolynomial_free(qp
);
2610 if (new_n_div
< n_div
)
2611 return reduce_divs(qp
);
2615 isl_qpolynomial_free(qp
);
2620 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2621 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2623 struct isl_qpolynomial
*qp
;
2626 qp
= isl_qpolynomial_zero_on_domain(domain
);
2630 cst
= isl_poly_as_cst(qp
->poly
);
2631 isl_int_set(cst
->n
, n
);
2632 isl_int_set(cst
->d
, d
);
2637 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2639 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2640 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2642 isl_qpolynomial
*qp
;
2645 qp
= isl_qpolynomial_zero_on_domain(domain
);
2649 cst
= isl_poly_as_cst(qp
->poly
);
2650 isl_int_set(cst
->n
, val
->n
);
2651 isl_int_set(cst
->d
, val
->d
);
2657 isl_qpolynomial_free(qp
);
2661 static isl_stat
poly_set_active(__isl_keep isl_poly
*poly
, int *active
, int d
)
2667 is_cst
= isl_poly_is_cst(poly
);
2669 return isl_stat_error
;
2674 active
[poly
->var
] = 1;
2676 rec
= isl_poly_as_rec(poly
);
2677 for (i
= 0; i
< rec
->n
; ++i
)
2678 if (poly_set_active(rec
->p
[i
], active
, d
) < 0)
2679 return isl_stat_error
;
2684 static isl_stat
set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2690 space
= isl_qpolynomial_peek_domain_space(qp
);
2691 d
= isl_space_dim(space
, isl_dim_all
);
2692 if (d
< 0 || !active
)
2693 return isl_stat_error
;
2695 for (i
= 0; i
< d
; ++i
)
2696 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2697 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2703 return poly_set_active(qp
->poly
, active
, d
);
2707 #define TYPE isl_qpolynomial
2709 #include "check_type_range_templ.c"
2711 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2712 enum isl_dim_type type
, unsigned first
, unsigned n
)
2716 isl_bool involves
= isl_bool_false
;
2722 return isl_bool_error
;
2724 return isl_bool_false
;
2726 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2727 return isl_bool_error
;
2728 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2729 type
== isl_dim_in
, return isl_bool_error
);
2731 space
= isl_qpolynomial_peek_domain_space(qp
);
2732 d
= isl_space_dim(space
, isl_dim_all
);
2734 return isl_bool_error
;
2735 active
= isl_calloc_array(qp
->dim
->ctx
, int, d
);
2736 if (set_active(qp
, active
) < 0)
2739 offset
= isl_qpolynomial_domain_var_offset(qp
, domain_type(type
));
2743 for (i
= 0; i
< n
; ++i
)
2744 if (active
[first
+ i
]) {
2745 involves
= isl_bool_true
;
2754 return isl_bool_error
;
2757 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2758 * of the divs that do appear in the quasi-polynomial.
2760 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2761 __isl_take isl_qpolynomial
*qp
)
2768 int *reordering
= NULL
;
2775 if (qp
->div
->n_row
== 0)
2778 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
2780 return isl_qpolynomial_free(qp
);
2781 len
= qp
->div
->n_col
- 2;
2782 ctx
= isl_qpolynomial_get_ctx(qp
);
2783 active
= isl_calloc_array(ctx
, int, len
);
2787 if (poly_set_active(qp
->poly
, active
, len
) < 0)
2790 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2791 if (!active
[div_pos
+ i
]) {
2795 for (j
= 0; j
< i
; ++j
) {
2796 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ j
]))
2798 active
[div_pos
+ j
] = 1;
2808 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2812 for (i
= 0; i
< div_pos
; ++i
)
2816 n_div
= qp
->div
->n_row
;
2817 for (i
= 0; i
< n_div
; ++i
) {
2818 if (!active
[div_pos
+ i
]) {
2819 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2820 qp
->div
= isl_mat_drop_cols(qp
->div
,
2821 2 + div_pos
+ i
- skip
, 1);
2824 reordering
[div_pos
+ i
] = div_pos
+ i
- skip
;
2827 qp
->poly
= reorder(qp
->poly
, reordering
);
2829 if (!qp
->poly
|| !qp
->div
)
2839 isl_qpolynomial_free(qp
);
2843 __isl_give isl_poly
*isl_poly_drop(__isl_take isl_poly
*poly
,
2844 unsigned first
, unsigned n
)
2851 if (n
== 0 || poly
->var
< 0 || poly
->var
< first
)
2853 if (poly
->var
< first
+ n
) {
2854 poly
= replace_by_constant_term(poly
);
2855 return isl_poly_drop(poly
, first
, n
);
2857 poly
= isl_poly_cow(poly
);
2861 rec
= isl_poly_as_rec(poly
);
2865 for (i
= 0; i
< rec
->n
; ++i
) {
2866 rec
->p
[i
] = isl_poly_drop(rec
->p
[i
], first
, n
);
2873 isl_poly_free(poly
);
2877 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2878 __isl_take isl_qpolynomial
*qp
,
2879 enum isl_dim_type type
, unsigned pos
, const char *s
)
2881 qp
= isl_qpolynomial_cow(qp
);
2884 if (type
== isl_dim_out
)
2885 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2886 "cannot set name of output/set dimension",
2887 return isl_qpolynomial_free(qp
));
2888 type
= domain_type(type
);
2889 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2894 isl_qpolynomial_free(qp
);
2898 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2899 __isl_take isl_qpolynomial
*qp
,
2900 enum isl_dim_type type
, unsigned first
, unsigned n
)
2906 if (type
== isl_dim_out
)
2907 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2908 "cannot drop output/set dimension",
2910 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2911 return isl_qpolynomial_free(qp
);
2912 type
= domain_type(type
);
2913 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2916 qp
= isl_qpolynomial_cow(qp
);
2920 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2921 type
== isl_dim_set
, goto error
);
2923 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2927 offset
= isl_qpolynomial_domain_var_offset(qp
, type
);
2932 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2936 qp
->poly
= isl_poly_drop(qp
->poly
, first
, n
);
2942 isl_qpolynomial_free(qp
);
2946 /* Project the domain of the quasi-polynomial onto its parameter space.
2947 * The quasi-polynomial may not involve any of the domain dimensions.
2949 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2950 __isl_take isl_qpolynomial
*qp
)
2956 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2958 return isl_qpolynomial_free(qp
);
2959 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2961 return isl_qpolynomial_free(qp
);
2963 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2964 "polynomial involves some of the domain dimensions",
2965 return isl_qpolynomial_free(qp
));
2966 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2967 space
= isl_qpolynomial_get_domain_space(qp
);
2968 space
= isl_space_params(space
);
2969 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2973 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2974 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2984 if (eq
->n_eq
== 0) {
2985 isl_basic_set_free(eq
);
2989 qp
= isl_qpolynomial_cow(qp
);
2992 qp
->div
= isl_mat_cow(qp
->div
);
2996 total
= isl_basic_set_offset(eq
, isl_dim_div
);
2998 isl_int_init(denom
);
2999 for (i
= 0; i
< eq
->n_eq
; ++i
) {
3000 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
3001 if (j
< 0 || j
== 0 || j
>= total
)
3004 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
3005 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
3007 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
3008 &qp
->div
->row
[k
][0]);
3009 normalize_div(qp
, k
);
3012 if (isl_int_is_pos(eq
->eq
[i
][j
]))
3013 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
3014 isl_int_abs(denom
, eq
->eq
[i
][j
]);
3015 isl_int_set_si(eq
->eq
[i
][j
], 0);
3017 poly
= isl_poly_from_affine(qp
->dim
->ctx
,
3018 eq
->eq
[i
], denom
, total
);
3019 qp
->poly
= isl_poly_subs(qp
->poly
, j
- 1, 1, &poly
);
3020 isl_poly_free(poly
);
3022 isl_int_clear(denom
);
3027 isl_basic_set_free(eq
);
3029 qp
= substitute_non_divs(qp
);
3034 isl_basic_set_free(eq
);
3035 isl_qpolynomial_free(qp
);
3039 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3041 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
3042 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
3046 if (qp
->div
->n_row
> 0)
3047 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
3048 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
3050 isl_basic_set_free(eq
);
3051 isl_qpolynomial_free(qp
);
3055 /* Look for equalities among the variables shared by context and qp
3056 * and the integer divisions of qp, if any.
3057 * The equalities are then used to eliminate variables and/or integer
3058 * divisions from qp.
3060 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
3061 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3063 isl_local_space
*ls
;
3066 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3067 context
= isl_local_space_lift_set(ls
, context
);
3069 aff
= isl_set_affine_hull(context
);
3070 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
3073 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
3074 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
3076 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
3077 isl_set
*dom_context
= isl_set_universe(space
);
3078 dom_context
= isl_set_intersect_params(dom_context
, context
);
3079 return isl_qpolynomial_gist(qp
, dom_context
);
3082 /* Return a zero isl_qpolynomial in the given space.
3084 * This is a helper function for isl_pw_*_as_* that ensures a uniform
3085 * interface over all piecewise types.
3087 static __isl_give isl_qpolynomial
*isl_qpolynomial_zero_in_space(
3088 __isl_take isl_space
*space
)
3090 return isl_qpolynomial_zero_on_domain(isl_space_domain(space
));
3093 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3096 #define PW isl_pw_qpolynomial
3098 #define BASE qpolynomial
3100 #define EL_IS_ZERO is_zero
3104 #define IS_ZERO is_zero
3107 #undef DEFAULT_IS_ZERO
3108 #define DEFAULT_IS_ZERO 1
3110 #include <isl_pw_templ.c>
3111 #include <isl_pw_un_op_templ.c>
3112 #include <isl_pw_eval.c>
3113 #include <isl_pw_insert_dims_templ.c>
3114 #include <isl_pw_lift_templ.c>
3115 #include <isl_pw_morph_templ.c>
3116 #include <isl_pw_move_dims_templ.c>
3117 #include <isl_pw_neg_templ.c>
3118 #include <isl_pw_opt_templ.c>
3119 #include <isl_pw_sub_templ.c>
3122 #define BASE pw_qpolynomial
3124 #include <isl_union_single.c>
3125 #include <isl_union_eval.c>
3126 #include <isl_union_neg.c>
3128 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
3136 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
3139 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
3142 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
3143 __isl_take isl_pw_qpolynomial
*pwqp1
,
3144 __isl_take isl_pw_qpolynomial
*pwqp2
)
3146 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
3149 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
3150 __isl_take isl_pw_qpolynomial
*pwqp1
,
3151 __isl_take isl_pw_qpolynomial
*pwqp2
)
3154 struct isl_pw_qpolynomial
*res
;
3156 if (!pwqp1
|| !pwqp2
)
3159 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3162 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3163 isl_pw_qpolynomial_free(pwqp2
);
3167 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3168 isl_pw_qpolynomial_free(pwqp1
);
3172 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3173 isl_pw_qpolynomial_free(pwqp1
);
3177 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3178 isl_pw_qpolynomial_free(pwqp2
);
3182 n
= pwqp1
->n
* pwqp2
->n
;
3183 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3185 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3186 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3187 struct isl_set
*common
;
3188 struct isl_qpolynomial
*prod
;
3189 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3190 isl_set_copy(pwqp2
->p
[j
].set
));
3191 if (isl_set_plain_is_empty(common
)) {
3192 isl_set_free(common
);
3196 prod
= isl_qpolynomial_mul(
3197 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3198 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3200 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3204 isl_pw_qpolynomial_free(pwqp1
);
3205 isl_pw_qpolynomial_free(pwqp2
);
3209 isl_pw_qpolynomial_free(pwqp1
);
3210 isl_pw_qpolynomial_free(pwqp2
);
3214 __isl_give isl_val
*isl_poly_eval(__isl_take isl_poly
*poly
,
3215 __isl_take isl_vec
*vec
)
3223 is_cst
= isl_poly_is_cst(poly
);
3228 res
= isl_poly_get_constant_val(poly
);
3229 isl_poly_free(poly
);
3233 rec
= isl_poly_as_rec(poly
);
3237 isl_assert(poly
->ctx
, rec
->n
>= 1, goto error
);
3239 base
= isl_val_rat_from_isl_int(poly
->ctx
,
3240 vec
->el
[1 + poly
->var
], vec
->el
[0]);
3242 res
= isl_poly_eval(isl_poly_copy(rec
->p
[rec
->n
- 1]),
3245 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3246 res
= isl_val_mul(res
, isl_val_copy(base
));
3247 res
= isl_val_add(res
, isl_poly_eval(isl_poly_copy(rec
->p
[i
]),
3248 isl_vec_copy(vec
)));
3252 isl_poly_free(poly
);
3256 isl_poly_free(poly
);
3261 /* Evaluate "qp" in the void point "pnt".
3262 * In particular, return the value NaN.
3264 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3265 __isl_take isl_point
*pnt
)
3269 ctx
= isl_point_get_ctx(pnt
);
3270 isl_qpolynomial_free(qp
);
3271 isl_point_free(pnt
);
3272 return isl_val_nan(ctx
);
3275 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3276 __isl_take isl_point
*pnt
)
3284 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3285 is_void
= isl_point_is_void(pnt
);
3289 return eval_void(qp
, pnt
);
3291 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3293 v
= isl_poly_eval(isl_poly_copy(qp
->poly
), ext
);
3295 isl_qpolynomial_free(qp
);
3296 isl_point_free(pnt
);
3300 isl_qpolynomial_free(qp
);
3301 isl_point_free(pnt
);
3305 int isl_poly_cmp(__isl_keep isl_poly_cst
*cst1
, __isl_keep isl_poly_cst
*cst2
)
3310 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3311 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3312 cmp
= isl_int_sgn(t
);
3317 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3318 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3319 unsigned first
, unsigned n
)
3327 if (type
== isl_dim_out
)
3328 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3329 "cannot insert output/set dimensions",
3331 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3332 return isl_qpolynomial_free(qp
);
3333 type
= domain_type(type
);
3334 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3337 qp
= isl_qpolynomial_cow(qp
);
3341 g_pos
= pos(qp
->dim
, type
) + first
;
3343 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3347 total
= qp
->div
->n_col
- 2;
3348 if (total
> g_pos
) {
3350 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3353 for (i
= 0; i
< total
- g_pos
; ++i
)
3355 qp
->poly
= expand(qp
->poly
, exp
, g_pos
);
3361 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3367 isl_qpolynomial_free(qp
);
3371 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3372 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3376 pos
= isl_qpolynomial_dim(qp
, type
);
3378 return isl_qpolynomial_free(qp
);
3380 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3383 static int *reordering_move(isl_ctx
*ctx
,
3384 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3389 reordering
= isl_alloc_array(ctx
, int, len
);
3394 for (i
= 0; i
< dst
; ++i
)
3396 for (i
= 0; i
< n
; ++i
)
3397 reordering
[src
+ i
] = dst
+ i
;
3398 for (i
= 0; i
< src
- dst
; ++i
)
3399 reordering
[dst
+ i
] = dst
+ n
+ i
;
3400 for (i
= 0; i
< len
- src
- n
; ++i
)
3401 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3403 for (i
= 0; i
< src
; ++i
)
3405 for (i
= 0; i
< n
; ++i
)
3406 reordering
[src
+ i
] = dst
+ i
;
3407 for (i
= 0; i
< dst
- src
; ++i
)
3408 reordering
[src
+ n
+ i
] = src
+ i
;
3409 for (i
= 0; i
< len
- dst
- n
; ++i
)
3410 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3416 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3417 __isl_take isl_qpolynomial
*qp
,
3418 enum isl_dim_type dst_type
, unsigned dst_pos
,
3419 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3428 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3429 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3430 "cannot move output/set dimension",
3432 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3433 return isl_qpolynomial_free(qp
);
3434 if (dst_type
== isl_dim_in
)
3435 dst_type
= isl_dim_set
;
3436 if (src_type
== isl_dim_in
)
3437 src_type
= isl_dim_set
;
3440 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3441 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3444 qp
= isl_qpolynomial_cow(qp
);
3448 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3449 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3450 if (dst_type
> src_type
)
3453 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3460 reordering
= reordering_move(qp
->dim
->ctx
,
3461 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3465 qp
->poly
= reorder(qp
->poly
, reordering
);
3470 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3476 isl_qpolynomial_free(qp
);
3480 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3481 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3486 space
= isl_space_domain(space
);
3490 d
= isl_space_dim(space
, isl_dim_all
);
3491 poly
= d
< 0 ? NULL
: isl_poly_from_affine(space
->ctx
, f
, denom
, 1 + d
);
3493 return isl_qpolynomial_alloc(space
, 0, poly
);
3496 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3500 isl_qpolynomial
*qp
;
3505 ctx
= isl_aff_get_ctx(aff
);
3506 poly
= isl_poly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3509 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3510 aff
->ls
->div
->n_row
, poly
);
3514 isl_mat_free(qp
->div
);
3515 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3516 qp
->div
= isl_mat_cow(qp
->div
);
3521 qp
= reduce_divs(qp
);
3522 qp
= remove_redundant_divs(qp
);
3526 return isl_qpolynomial_free(qp
);
3529 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3530 __isl_take isl_pw_aff
*pwaff
)
3533 isl_pw_qpolynomial
*pwqp
;
3538 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3541 for (i
= 0; i
< pwaff
->n
; ++i
) {
3543 isl_qpolynomial
*qp
;
3545 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3546 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3547 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3550 isl_pw_aff_free(pwaff
);
3554 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3555 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3559 aff
= isl_constraint_get_bound(c
, type
, pos
);
3560 isl_constraint_free(c
);
3561 return isl_qpolynomial_from_aff(aff
);
3564 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3565 * in "qp" by subs[i].
3567 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3568 __isl_take isl_qpolynomial
*qp
,
3569 enum isl_dim_type type
, unsigned first
, unsigned n
,
3570 __isl_keep isl_qpolynomial
**subs
)
3578 qp
= isl_qpolynomial_cow(qp
);
3582 if (type
== isl_dim_out
)
3583 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3584 "cannot substitute output/set dimension",
3586 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3587 return isl_qpolynomial_free(qp
);
3588 type
= domain_type(type
);
3590 for (i
= 0; i
< n
; ++i
)
3594 for (i
= 0; i
< n
; ++i
)
3595 if (isl_qpolynomial_check_equal_space(qp
, subs
[i
]) < 0)
3598 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3599 for (i
= 0; i
< n
; ++i
)
3600 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3602 first
+= pos(qp
->dim
, type
);
3604 polys
= isl_alloc_array(qp
->dim
->ctx
, struct isl_poly
*, n
);
3607 for (i
= 0; i
< n
; ++i
)
3608 polys
[i
] = subs
[i
]->poly
;
3610 qp
->poly
= isl_poly_subs(qp
->poly
, first
, n
, polys
);
3619 isl_qpolynomial_free(qp
);
3623 /* Extend "bset" with extra set dimensions for each integer division
3624 * in "qp" and then call "fn" with the extended bset and the polynomial
3625 * that results from replacing each of the integer divisions by the
3626 * corresponding extra set dimension.
3628 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3629 __isl_keep isl_basic_set
*bset
,
3630 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3631 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3634 isl_local_space
*ls
;
3635 isl_qpolynomial
*poly
;
3638 return isl_stat_error
;
3639 if (qp
->div
->n_row
== 0)
3640 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3643 space
= isl_space_copy(qp
->dim
);
3644 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3645 poly
= isl_qpolynomial_alloc(space
, 0, isl_poly_copy(qp
->poly
));
3646 bset
= isl_basic_set_copy(bset
);
3647 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3648 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3650 return fn(bset
, poly
, user
);
3653 /* Return total degree in variables first (inclusive) up to last (exclusive).
3655 int isl_poly_degree(__isl_keep isl_poly
*poly
, int first
, int last
)
3659 isl_bool is_zero
, is_cst
;
3662 is_zero
= isl_poly_is_zero(poly
);
3667 is_cst
= isl_poly_is_cst(poly
);
3670 if (is_cst
|| poly
->var
< first
)
3673 rec
= isl_poly_as_rec(poly
);
3677 for (i
= 0; i
< rec
->n
; ++i
) {
3680 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3685 d
= isl_poly_degree(rec
->p
[i
], first
, last
);
3686 if (poly
->var
< last
)
3695 /* Return total degree in set variables.
3697 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3705 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3706 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3709 return isl_poly_degree(poly
->poly
, ovar
, ovar
+ nvar
);
3712 __isl_give isl_poly
*isl_poly_coeff(__isl_keep isl_poly
*poly
,
3713 unsigned pos
, int deg
)
3719 is_cst
= isl_poly_is_cst(poly
);
3722 if (is_cst
|| poly
->var
< pos
) {
3724 return isl_poly_copy(poly
);
3726 return isl_poly_zero(poly
->ctx
);
3729 rec
= isl_poly_as_rec(poly
);
3733 if (poly
->var
== pos
) {
3735 return isl_poly_copy(rec
->p
[deg
]);
3737 return isl_poly_zero(poly
->ctx
);
3740 poly
= isl_poly_copy(poly
);
3741 poly
= isl_poly_cow(poly
);
3742 rec
= isl_poly_as_rec(poly
);
3746 for (i
= 0; i
< rec
->n
; ++i
) {
3748 t
= isl_poly_coeff(rec
->p
[i
], pos
, deg
);
3751 isl_poly_free(rec
->p
[i
]);
3757 isl_poly_free(poly
);
3761 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3763 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3764 __isl_keep isl_qpolynomial
*qp
,
3765 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3774 if (type
== isl_dim_out
)
3775 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3776 "output/set dimension does not have a coefficient",
3778 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
3780 type
= domain_type(type
);
3782 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3783 poly
= isl_poly_coeff(qp
->poly
, g_pos
, deg
);
3785 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
),
3786 qp
->div
->n_row
, poly
);
3789 isl_mat_free(c
->div
);
3790 c
->div
= isl_mat_copy(qp
->div
);
3795 isl_qpolynomial_free(c
);
3799 /* Homogenize the polynomial in the variables first (inclusive) up to
3800 * last (exclusive) by inserting powers of variable first.
3801 * Variable first is assumed not to appear in the input.
3803 __isl_give isl_poly
*isl_poly_homogenize(__isl_take isl_poly
*poly
, int deg
,
3804 int target
, int first
, int last
)
3807 isl_bool is_zero
, is_cst
;
3810 is_zero
= isl_poly_is_zero(poly
);
3812 return isl_poly_free(poly
);
3817 is_cst
= isl_poly_is_cst(poly
);
3819 return isl_poly_free(poly
);
3820 if (is_cst
|| poly
->var
< first
) {
3823 hom
= isl_poly_var_pow(poly
->ctx
, first
, target
- deg
);
3826 rec
= isl_poly_as_rec(hom
);
3827 rec
->p
[target
- deg
] = isl_poly_mul(rec
->p
[target
- deg
], poly
);
3832 poly
= isl_poly_cow(poly
);
3833 rec
= isl_poly_as_rec(poly
);
3837 for (i
= 0; i
< rec
->n
; ++i
) {
3838 is_zero
= isl_poly_is_zero(rec
->p
[i
]);
3840 return isl_poly_free(poly
);
3843 rec
->p
[i
] = isl_poly_homogenize(rec
->p
[i
],
3844 poly
->var
< last
? deg
+ i
: i
, target
,
3852 isl_poly_free(poly
);
3856 /* Homogenize the polynomial in the set variables by introducing
3857 * powers of an extra set variable at position 0.
3859 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3860 __isl_take isl_qpolynomial
*poly
)
3864 int deg
= isl_qpolynomial_degree(poly
);
3869 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3870 poly
= isl_qpolynomial_cow(poly
);
3874 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3875 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3877 return isl_qpolynomial_free(poly
);
3878 poly
->poly
= isl_poly_homogenize(poly
->poly
, 0, deg
, ovar
, ovar
+ nvar
);
3884 isl_qpolynomial_free(poly
);
3888 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3889 __isl_take isl_mat
*div
)
3895 d
= isl_space_dim(space
, isl_dim_all
);
3901 term
= isl_calloc(space
->ctx
, struct isl_term
,
3902 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3909 isl_int_init(term
->n
);
3910 isl_int_init(term
->d
);
3914 isl_space_free(space
);
3919 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3928 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3934 total
= isl_term_dim(term
, isl_dim_all
);
3938 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3942 isl_int_set(dup
->n
, term
->n
);
3943 isl_int_set(dup
->d
, term
->d
);
3945 for (i
= 0; i
< total
; ++i
)
3946 dup
->pow
[i
] = term
->pow
[i
];
3951 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3959 return isl_term_dup(term
);
3962 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3967 if (--term
->ref
> 0)
3970 isl_space_free(term
->dim
);
3971 isl_mat_free(term
->div
);
3972 isl_int_clear(term
->n
);
3973 isl_int_clear(term
->d
);
3979 isl_size
isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3984 return isl_size_error
;
3989 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3990 case isl_dim_div
: return term
->div
->n_row
;
3991 case isl_dim_all
: dim
= isl_space_dim(term
->dim
, isl_dim_all
);
3993 return isl_size_error
;
3994 return dim
+ term
->div
->n_row
;
3995 default: return isl_size_error
;
3999 /* Return the space of "term".
4001 static __isl_keep isl_space
*isl_term_peek_space(__isl_keep isl_term
*term
)
4003 return term
? term
->dim
: NULL
;
4006 /* Return the offset of the first variable of type "type" within
4007 * the variables of "term".
4009 static isl_size
isl_term_offset(__isl_keep isl_term
*term
,
4010 enum isl_dim_type type
)
4014 space
= isl_term_peek_space(term
);
4016 return isl_size_error
;
4020 case isl_dim_set
: return isl_space_offset(space
, type
);
4021 case isl_dim_div
: return isl_space_dim(space
, isl_dim_all
);
4023 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4024 "invalid dimension type", return isl_size_error
);
4028 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
4030 return term
? term
->dim
->ctx
: NULL
;
4033 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
4037 isl_int_set(*n
, term
->n
);
4040 /* Return the coefficient of the term "term".
4042 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
4047 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
4052 #define TYPE isl_term
4054 #include "check_type_range_templ.c"
4056 isl_size
isl_term_get_exp(__isl_keep isl_term
*term
,
4057 enum isl_dim_type type
, unsigned pos
)
4061 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
4062 return isl_size_error
;
4063 offset
= isl_term_offset(term
, type
);
4065 return isl_size_error
;
4067 return term
->pow
[offset
+ pos
];
4070 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
4072 isl_local_space
*ls
;
4075 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
4078 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
4079 isl_mat_copy(term
->div
));
4080 aff
= isl_aff_alloc(ls
);
4084 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
4086 aff
= isl_aff_normalize(aff
);
4091 __isl_give isl_term
*isl_poly_foreach_term(__isl_keep isl_poly
*poly
,
4092 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
4093 __isl_take isl_term
*term
, void *user
)
4096 isl_bool is_zero
, is_bad
, is_cst
;
4099 is_zero
= isl_poly_is_zero(poly
);
4100 if (is_zero
< 0 || !term
)
4106 is_cst
= isl_poly_is_cst(poly
);
4107 is_bad
= isl_poly_is_nan(poly
);
4108 if (is_bad
>= 0 && !is_bad
)
4109 is_bad
= isl_poly_is_infty(poly
);
4110 if (is_bad
>= 0 && !is_bad
)
4111 is_bad
= isl_poly_is_neginfty(poly
);
4112 if (is_cst
< 0 || is_bad
< 0)
4113 return isl_term_free(term
);
4115 isl_die(isl_term_get_ctx(term
), isl_error_invalid
,
4116 "cannot handle NaN/infty polynomial",
4117 return isl_term_free(term
));
4121 cst
= isl_poly_as_cst(poly
);
4124 term
= isl_term_cow(term
);
4127 isl_int_set(term
->n
, cst
->n
);
4128 isl_int_set(term
->d
, cst
->d
);
4129 if (fn(isl_term_copy(term
), user
) < 0)
4134 rec
= isl_poly_as_rec(poly
);
4138 for (i
= 0; i
< rec
->n
; ++i
) {
4139 term
= isl_term_cow(term
);
4142 term
->pow
[poly
->var
] = i
;
4143 term
= isl_poly_foreach_term(rec
->p
[i
], fn
, term
, user
);
4147 term
= isl_term_cow(term
);
4150 term
->pow
[poly
->var
] = 0;
4154 isl_term_free(term
);
4158 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
4159 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
4164 return isl_stat_error
;
4166 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
4168 return isl_stat_error
;
4170 term
= isl_poly_foreach_term(qp
->poly
, fn
, term
, user
);
4172 isl_term_free(term
);
4174 return term
? isl_stat_ok
: isl_stat_error
;
4177 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
4180 isl_qpolynomial
*qp
;
4184 n
= isl_term_dim(term
, isl_dim_all
);
4186 term
= isl_term_free(term
);
4190 poly
= isl_poly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4191 for (i
= 0; i
< n
; ++i
) {
4194 poly
= isl_poly_mul(poly
,
4195 isl_poly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4198 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
),
4199 term
->div
->n_row
, poly
);
4202 isl_mat_free(qp
->div
);
4203 qp
->div
= isl_mat_copy(term
->div
);
4207 isl_term_free(term
);
4210 isl_qpolynomial_free(qp
);
4211 isl_term_free(term
);
4215 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4216 __isl_take isl_space
*space
)
4220 isl_size total
, d_set
, d_qp
;
4225 if (isl_space_is_equal(qp
->dim
, space
)) {
4226 isl_space_free(space
);
4230 qp
= isl_qpolynomial_cow(qp
);
4234 d_set
= isl_space_dim(space
, isl_dim_set
);
4235 d_qp
= isl_qpolynomial_domain_dim(qp
, isl_dim_set
);
4236 extra
= d_set
- d_qp
;
4237 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4238 if (d_set
< 0 || d_qp
< 0 || total
< 0)
4240 if (qp
->div
->n_row
) {
4243 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4246 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4248 qp
->poly
= expand(qp
->poly
, exp
, total
);
4253 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4256 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4257 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4259 isl_space_free(qp
->dim
);
4264 isl_space_free(space
);
4265 isl_qpolynomial_free(qp
);
4269 /* For each parameter or variable that does not appear in qp,
4270 * first eliminate the variable from all constraints and then set it to zero.
4272 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4273 __isl_keep isl_qpolynomial
*qp
)
4281 d
= isl_set_dim(set
, isl_dim_all
);
4285 active
= isl_calloc_array(set
->ctx
, int, d
);
4286 if (set_active(qp
, active
) < 0)
4289 for (i
= 0; i
< d
; ++i
)
4298 nparam
= isl_set_dim(set
, isl_dim_param
);
4299 nvar
= isl_set_dim(set
, isl_dim_set
);
4300 if (nparam
< 0 || nvar
< 0)
4302 for (i
= 0; i
< nparam
; ++i
) {
4305 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4306 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4308 for (i
= 0; i
< nvar
; ++i
) {
4309 if (active
[nparam
+ i
])
4311 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4312 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4324 struct isl_opt_data
{
4325 isl_qpolynomial
*qp
;
4331 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4333 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4336 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4340 } else if (data
->max
) {
4341 data
->opt
= isl_val_max(data
->opt
, val
);
4343 data
->opt
= isl_val_min(data
->opt
, val
);
4349 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4350 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4352 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4358 is_cst
= isl_poly_is_cst(qp
->poly
);
4363 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4364 isl_qpolynomial_free(qp
);
4368 set
= fix_inactive(set
, qp
);
4371 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4375 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4378 isl_qpolynomial_free(qp
);
4382 isl_qpolynomial_free(qp
);
4383 isl_val_free(data
.opt
);
4387 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4388 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4395 isl_mat
*mat
, *diag
;
4397 qp
= isl_qpolynomial_cow(qp
);
4399 space
= isl_qpolynomial_peek_domain_space(qp
);
4400 if (isl_morph_check_applies(morph
, space
) < 0)
4403 ctx
= isl_qpolynomial_get_ctx(qp
);
4404 n_sub
= morph
->inv
->n_row
- 1;
4405 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4406 n_sub
+= qp
->div
->n_row
;
4407 subs
= isl_calloc_array(ctx
, struct isl_poly
*, n_sub
);
4411 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4412 subs
[i
] = isl_poly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4413 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4414 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4415 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4416 subs
[morph
->inv
->n_row
- 1 + i
] =
4417 isl_poly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4419 qp
->poly
= isl_poly_subs(qp
->poly
, 0, n_sub
, subs
);
4421 for (i
= 0; i
< n_sub
; ++i
)
4422 isl_poly_free(subs
[i
]);
4425 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4426 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4427 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4428 mat
= isl_mat_diagonal(mat
, diag
);
4429 qp
->div
= isl_mat_product(qp
->div
, mat
);
4430 isl_space_free(qp
->dim
);
4431 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4433 if (!qp
->poly
|| !qp
->div
|| !qp
->dim
)
4436 isl_morph_free(morph
);
4440 isl_qpolynomial_free(qp
);
4441 isl_morph_free(morph
);
4445 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4446 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4447 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4449 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4450 &isl_pw_qpolynomial_mul
);
4453 /* Reorder the dimension of "qp" according to the given reordering.
4455 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4456 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4460 qp
= isl_qpolynomial_cow(qp
);
4464 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4468 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4472 qp
->poly
= reorder(qp
->poly
, r
->pos
);
4476 space
= isl_reordering_get_space(r
);
4477 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4479 isl_reordering_free(r
);
4482 isl_qpolynomial_free(qp
);
4483 isl_reordering_free(r
);
4487 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4488 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4490 isl_bool equal_params
;
4495 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4496 if (equal_params
< 0)
4498 if (!equal_params
) {
4499 isl_reordering
*exp
;
4501 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4502 exp
= isl_reordering_extend_space(exp
,
4503 isl_qpolynomial_get_domain_space(qp
));
4504 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4507 isl_space_free(model
);
4510 isl_space_free(model
);
4511 isl_qpolynomial_free(qp
);
4515 struct isl_split_periods_data
{
4517 isl_pw_qpolynomial
*res
;
4520 /* Create a slice where the integer division "div" has the fixed value "v".
4521 * In particular, if "div" refers to floor(f/m), then create a slice
4523 * m v <= f <= m v + (m - 1)
4528 * -f + m v + (m - 1) >= 0
4530 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4531 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4534 isl_basic_set
*bset
= NULL
;
4537 total
= isl_space_dim(space
, isl_dim_all
);
4538 if (total
< 0 || !qp
)
4541 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4543 k
= isl_basic_set_alloc_inequality(bset
);
4546 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4547 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4549 k
= isl_basic_set_alloc_inequality(bset
);
4552 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4553 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4554 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4555 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4557 isl_space_free(space
);
4558 return isl_set_from_basic_set(bset
);
4560 isl_basic_set_free(bset
);
4561 isl_space_free(space
);
4565 static isl_stat
split_periods(__isl_take isl_set
*set
,
4566 __isl_take isl_qpolynomial
*qp
, void *user
);
4568 /* Create a slice of the domain "set" such that integer division "div"
4569 * has the fixed value "v" and add the results to data->res,
4570 * replacing the integer division by "v" in "qp".
4572 static isl_stat
set_div(__isl_take isl_set
*set
,
4573 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4574 struct isl_split_periods_data
*data
)
4581 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4582 set
= isl_set_intersect(set
, slice
);
4584 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4588 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4589 if (isl_int_is_zero(qp
->div
->row
[i
][2 + div_pos
+ div
]))
4591 isl_int_addmul(qp
->div
->row
[i
][1],
4592 qp
->div
->row
[i
][2 + div_pos
+ div
], v
);
4593 isl_int_set_si(qp
->div
->row
[i
][2 + div_pos
+ div
], 0);
4596 cst
= isl_poly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4597 qp
= substitute_div(qp
, div
, cst
);
4599 return split_periods(set
, qp
, data
);
4602 isl_qpolynomial_free(qp
);
4603 return isl_stat_error
;
4606 /* Split the domain "set" such that integer division "div"
4607 * has a fixed value (ranging from "min" to "max") on each slice
4608 * and add the results to data->res.
4610 static isl_stat
split_div(__isl_take isl_set
*set
,
4611 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4612 struct isl_split_periods_data
*data
)
4614 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4615 isl_set
*set_i
= isl_set_copy(set
);
4616 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4618 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4622 isl_qpolynomial_free(qp
);
4626 isl_qpolynomial_free(qp
);
4627 return isl_stat_error
;
4630 /* If "qp" refers to any integer division
4631 * that can only attain "max_periods" distinct values on "set"
4632 * then split the domain along those distinct values.
4633 * Add the results (or the original if no splitting occurs)
4636 static isl_stat
split_periods(__isl_take isl_set
*set
,
4637 __isl_take isl_qpolynomial
*qp
, void *user
)
4640 isl_pw_qpolynomial
*pwqp
;
4641 struct isl_split_periods_data
*data
;
4644 isl_stat r
= isl_stat_ok
;
4646 data
= (struct isl_split_periods_data
*)user
;
4651 if (qp
->div
->n_row
== 0) {
4652 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4653 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4657 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
4663 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4664 enum isl_lp_result lp_res
;
4666 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + div_pos
,
4667 qp
->div
->n_row
) != -1)
4670 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4671 set
->ctx
->one
, &min
, NULL
, NULL
);
4672 if (lp_res
== isl_lp_error
)
4674 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4676 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4678 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4679 set
->ctx
->one
, &max
, NULL
, NULL
);
4680 if (lp_res
== isl_lp_error
)
4682 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4684 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4686 isl_int_sub(max
, max
, min
);
4687 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4688 isl_int_add(max
, max
, min
);
4693 if (i
< qp
->div
->n_row
) {
4694 r
= split_div(set
, qp
, i
, min
, max
, data
);
4696 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4697 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4709 isl_qpolynomial_free(qp
);
4710 return isl_stat_error
;
4713 /* If any quasi-polynomial in pwqp refers to any integer division
4714 * that can only attain "max_periods" distinct values on its domain
4715 * then split the domain along those distinct values.
4717 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4718 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4720 struct isl_split_periods_data data
;
4722 data
.max_periods
= max_periods
;
4723 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4725 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4728 isl_pw_qpolynomial_free(pwqp
);
4732 isl_pw_qpolynomial_free(data
.res
);
4733 isl_pw_qpolynomial_free(pwqp
);
4737 /* Construct a piecewise quasipolynomial that is constant on the given
4738 * domain. In particular, it is
4741 * infinity if cst == -1
4743 * If cst == -1, then explicitly check whether the domain is empty and,
4744 * if so, return 0 instead.
4746 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4747 __isl_take isl_basic_set
*bset
, int cst
)
4750 isl_qpolynomial
*qp
;
4752 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4757 bset
= isl_basic_set_params(bset
);
4758 space
= isl_basic_set_get_space(bset
);
4760 qp
= isl_qpolynomial_infty_on_domain(space
);
4762 qp
= isl_qpolynomial_zero_on_domain(space
);
4764 qp
= isl_qpolynomial_one_on_domain(space
);
4765 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4768 /* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4769 * "fn" is the function that is called on each factor.
4770 * "pwpq" collects the results.
4772 struct isl_multiplicative_call_data_pw_qpolynomial
{
4773 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
);
4774 isl_pw_qpolynomial
*pwqp
;
4777 /* Call "fn" on "bset" and return the result,
4778 * but first check if "bset" has any redundant constraints or
4779 * implicit equality constraints.
4780 * If so, there may be further opportunities for detecting factors or
4781 * removing equality constraints, so recursively call
4782 * the top-level isl_basic_set_multiplicative_call.
4784 static __isl_give isl_pw_qpolynomial
*multiplicative_call_base(
4785 __isl_take isl_basic_set
*bset
,
4786 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4788 isl_size n1
, n2
, n_eq
;
4790 n1
= isl_basic_set_n_constraint(bset
);
4792 bset
= isl_basic_set_free(bset
);
4793 bset
= isl_basic_set_remove_redundancies(bset
);
4794 bset
= isl_basic_set_detect_equalities(bset
);
4795 n2
= isl_basic_set_n_constraint(bset
);
4796 n_eq
= isl_basic_set_n_equality(bset
);
4797 if (n2
< 0 || n_eq
< 0)
4798 bset
= isl_basic_set_free(bset
);
4799 else if (n2
< n1
|| n_eq
> 0)
4800 return isl_basic_set_multiplicative_call(bset
, fn
);
4804 /* isl_factorizer_every_factor_basic_set callback that applies
4805 * data->fn to the factor "bset" and multiplies in the result
4808 static isl_bool
multiplicative_call_factor_pw_qpolynomial(
4809 __isl_keep isl_basic_set
*bset
, void *user
)
4811 struct isl_multiplicative_call_data_pw_qpolynomial
*data
= user
;
4812 isl_pw_qpolynomial
*res
;
4814 bset
= isl_basic_set_copy(bset
);
4815 res
= multiplicative_call_base(bset
, data
->fn
);
4816 data
->pwqp
= isl_pw_qpolynomial_mul(data
->pwqp
, res
);
4818 return isl_bool_error
;
4820 return isl_bool_true
;
4823 /* Factor bset, call fn on each of the factors and return the product.
4825 * If no factors can be found, simply call fn on the input.
4826 * Otherwise, construct the factors based on the factorizer,
4827 * call fn on each factor and compute the product.
4829 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4830 __isl_take isl_basic_set
*bset
,
4831 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4833 struct isl_multiplicative_call_data_pw_qpolynomial data
= { fn
};
4837 isl_qpolynomial
*qp
;
4840 f
= isl_basic_set_factorizer(bset
);
4843 if (f
->n_group
== 0) {
4844 isl_factorizer_free(f
);
4845 return multiplicative_call_base(bset
, fn
);
4848 space
= isl_basic_set_get_space(bset
);
4849 space
= isl_space_params(space
);
4850 set
= isl_set_universe(isl_space_copy(space
));
4851 qp
= isl_qpolynomial_one_on_domain(space
);
4852 data
.pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4854 every
= isl_factorizer_every_factor_basic_set(f
,
4855 &multiplicative_call_factor_pw_qpolynomial
, &data
);
4857 data
.pwqp
= isl_pw_qpolynomial_free(data
.pwqp
);
4859 isl_basic_set_free(bset
);
4860 isl_factorizer_free(f
);
4864 isl_basic_set_free(bset
);
4868 /* Factor bset, call fn on each of the factors and return the product.
4869 * The function is assumed to evaluate to zero on empty domains,
4870 * to one on zero-dimensional domains and to infinity on unbounded domains
4871 * and will not be called explicitly on zero-dimensional or unbounded domains.
4873 * We first check for some special cases and remove all equalities.
4874 * Then we hand over control to compressed_multiplicative_call.
4876 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4877 __isl_take isl_basic_set
*bset
,
4878 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4883 isl_pw_qpolynomial
*pwqp
;
4888 if (isl_basic_set_plain_is_empty(bset
))
4889 return constant_on_domain(bset
, 0);
4891 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
4895 return constant_on_domain(bset
, 1);
4897 bounded
= isl_basic_set_is_bounded(bset
);
4901 return constant_on_domain(bset
, -1);
4903 if (bset
->n_eq
== 0)
4904 return compressed_multiplicative_call(bset
, fn
);
4906 morph
= isl_basic_set_full_compression(bset
);
4907 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4909 pwqp
= compressed_multiplicative_call(bset
, fn
);
4911 morph
= isl_morph_dom_params(morph
);
4912 morph
= isl_morph_ran_params(morph
);
4913 morph
= isl_morph_inverse(morph
);
4915 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4919 isl_basic_set_free(bset
);
4923 /* Drop all floors in "qp", turning each integer division [a/m] into
4924 * a rational division a/m. If "down" is set, then the integer division
4925 * is replaced by (a-(m-1))/m instead.
4927 static __isl_give isl_qpolynomial
*qp_drop_floors(
4928 __isl_take isl_qpolynomial
*qp
, int down
)
4935 if (qp
->div
->n_row
== 0)
4938 qp
= isl_qpolynomial_cow(qp
);
4942 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4944 isl_int_sub(qp
->div
->row
[i
][1],
4945 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4946 isl_int_add_ui(qp
->div
->row
[i
][1],
4947 qp
->div
->row
[i
][1], 1);
4949 s
= isl_poly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4950 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4951 qp
= substitute_div(qp
, i
, s
);
4959 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4960 * a rational division a/m.
4962 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4963 __isl_take isl_pw_qpolynomial
*pwqp
)
4970 if (isl_pw_qpolynomial_is_zero(pwqp
))
4973 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4977 for (i
= 0; i
< pwqp
->n
; ++i
) {
4978 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4985 isl_pw_qpolynomial_free(pwqp
);
4989 /* Adjust all the integer divisions in "qp" such that they are at least
4990 * one over the given orthant (identified by "signs"). This ensures
4991 * that they will still be non-negative even after subtracting (m-1)/m.
4993 * In particular, f is replaced by f' + v, changing f = [a/m]
4994 * to f' = [(a - m v)/m].
4995 * If the constant term k in a is smaller than m,
4996 * the constant term of v is set to floor(k/m) - 1.
4997 * For any other term, if the coefficient c and the variable x have
4998 * the same sign, then no changes are needed.
4999 * Otherwise, if the variable is positive (and c is negative),
5000 * then the coefficient of x in v is set to floor(c/m).
5001 * If the variable is negative (and c is positive),
5002 * then the coefficient of x in v is set to ceil(c/m).
5004 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
5012 qp
= isl_qpolynomial_cow(qp
);
5013 div_pos
= isl_qpolynomial_domain_var_offset(qp
, isl_dim_div
);
5015 return isl_qpolynomial_free(qp
);
5016 qp
->div
= isl_mat_cow(qp
->div
);
5020 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
5022 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
5023 isl_int
*row
= qp
->div
->row
[i
];
5027 if (isl_int_lt(row
[1], row
[0])) {
5028 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
5029 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
5030 isl_int_submul(row
[1], row
[0], v
->el
[0]);
5032 for (j
= 0; j
< div_pos
; ++j
) {
5033 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
5036 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
5038 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
5039 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
5041 for (j
= 0; j
< i
; ++j
) {
5042 if (isl_int_sgn(row
[2 + div_pos
+ j
]) >= 0)
5044 isl_int_fdiv_q(v
->el
[1 + div_pos
+ j
],
5045 row
[2 + div_pos
+ j
], row
[0]);
5046 isl_int_submul(row
[2 + div_pos
+ j
],
5047 row
[0], v
->el
[1 + div_pos
+ j
]);
5049 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
5050 if (isl_int_is_zero(qp
->div
->row
[j
][2 + div_pos
+ i
]))
5052 isl_seq_combine(qp
->div
->row
[j
] + 1,
5053 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
5054 qp
->div
->row
[j
][2 + div_pos
+ i
], v
->el
,
5057 isl_int_set_si(v
->el
[1 + div_pos
+ i
], 1);
5058 s
= isl_poly_from_affine(qp
->dim
->ctx
, v
->el
,
5059 qp
->div
->ctx
->one
, v
->size
);
5060 qp
->poly
= isl_poly_subs(qp
->poly
, div_pos
+ i
, 1, &s
);
5070 isl_qpolynomial_free(qp
);
5074 struct isl_to_poly_data
{
5076 isl_pw_qpolynomial
*res
;
5077 isl_qpolynomial
*qp
;
5080 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5081 * We first make all integer divisions positive and then split the
5082 * quasipolynomials into terms with sign data->sign (the direction
5083 * of the requested approximation) and terms with the opposite sign.
5084 * In the first set of terms, each integer division [a/m] is
5085 * overapproximated by a/m, while in the second it is underapproximated
5088 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
5089 int *signs
, void *user
)
5091 struct isl_to_poly_data
*data
= user
;
5092 isl_pw_qpolynomial
*t
;
5093 isl_qpolynomial
*qp
, *up
, *down
;
5095 qp
= isl_qpolynomial_copy(data
->qp
);
5096 qp
= make_divs_pos(qp
, signs
);
5098 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
5099 up
= qp_drop_floors(up
, 0);
5100 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
5101 down
= qp_drop_floors(down
, 1);
5103 isl_qpolynomial_free(qp
);
5104 qp
= isl_qpolynomial_add(up
, down
);
5106 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
5107 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
5112 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5113 * the polynomial will be an overapproximation. If "sign" is negative,
5114 * it will be an underapproximation. If "sign" is zero, the approximation
5115 * will lie somewhere in between.
5117 * In particular, is sign == 0, we simply drop the floors, turning
5118 * the integer divisions into rational divisions.
5119 * Otherwise, we split the domains into orthants, make all integer divisions
5120 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5121 * depending on the requested sign and the sign of the term in which
5122 * the integer division appears.
5124 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
5125 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
5128 struct isl_to_poly_data data
;
5131 return pwqp_drop_floors(pwqp
);
5137 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
5139 for (i
= 0; i
< pwqp
->n
; ++i
) {
5140 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
5141 isl_pw_qpolynomial
*t
;
5142 t
= isl_pw_qpolynomial_alloc(
5143 isl_set_copy(pwqp
->p
[i
].set
),
5144 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
5145 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
5148 data
.qp
= pwqp
->p
[i
].qp
;
5149 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
5150 &to_polynomial_on_orthant
, &data
) < 0)
5154 isl_pw_qpolynomial_free(pwqp
);
5158 isl_pw_qpolynomial_free(pwqp
);
5159 isl_pw_qpolynomial_free(data
.res
);
5163 static __isl_give isl_pw_qpolynomial
*poly_entry(
5164 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
5168 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
5171 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
5172 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
5174 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
5175 &poly_entry
, &sign
);
5178 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
5179 __isl_take isl_qpolynomial
*qp
)
5183 isl_vec
*aff
= NULL
;
5184 isl_basic_map
*bmap
= NULL
;
5191 is_affine
= isl_poly_is_affine(qp
->poly
);
5195 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
5196 "input quasi-polynomial not affine", goto error
);
5197 aff
= isl_qpolynomial_extract_affine(qp
);
5200 space
= isl_qpolynomial_get_space(qp
);
5201 pos
= 1 + isl_space_offset(space
, isl_dim_out
);
5202 n_div
= qp
->div
->n_row
;
5203 bmap
= isl_basic_map_alloc_space(space
, n_div
, 1, 2 * n_div
);
5205 for (i
= 0; i
< n_div
; ++i
) {
5206 k
= isl_basic_map_alloc_div(bmap
);
5209 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
5210 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
5211 bmap
= isl_basic_map_add_div_constraints(bmap
, k
);
5213 k
= isl_basic_map_alloc_equality(bmap
);
5216 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5217 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5218 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5221 isl_qpolynomial_free(qp
);
5222 bmap
= isl_basic_map_finalize(bmap
);
5226 isl_qpolynomial_free(qp
);
5227 isl_basic_map_free(bmap
);