2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
32 #define BASE pw_qpolynomial
34 #include <isl_list_templ.c>
36 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
39 case isl_dim_param
: return 0;
40 case isl_dim_in
: return dim
->nparam
;
41 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
46 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
54 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
59 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
61 return (struct isl_upoly_cst
*)up
;
64 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
69 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
71 return (struct isl_upoly_rec
*)up
;
74 /* Compare two polynomials.
76 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
77 * than "up2" and 0 if they are equal.
79 static int isl_upoly_plain_cmp(__isl_keep
struct isl_upoly
*up1
,
80 __isl_keep
struct isl_upoly
*up2
)
83 struct isl_upoly_rec
*rec1
, *rec2
;
91 if (up1
->var
!= up2
->var
)
92 return up1
->var
- up2
->var
;
94 if (isl_upoly_is_cst(up1
)) {
95 struct isl_upoly_cst
*cst1
, *cst2
;
98 cst1
= isl_upoly_as_cst(up1
);
99 cst2
= isl_upoly_as_cst(up2
);
102 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
105 return isl_int_cmp(cst1
->d
, cst2
->d
);
108 rec1
= isl_upoly_as_rec(up1
);
109 rec2
= isl_upoly_as_rec(up2
);
113 if (rec1
->n
!= rec2
->n
)
114 return rec1
->n
- rec2
->n
;
116 for (i
= 0; i
< rec1
->n
; ++i
) {
117 int cmp
= isl_upoly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
125 isl_bool
isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
126 __isl_keep
struct isl_upoly
*up2
)
129 struct isl_upoly_rec
*rec1
, *rec2
;
132 return isl_bool_error
;
134 return isl_bool_true
;
135 if (up1
->var
!= up2
->var
)
136 return isl_bool_false
;
137 if (isl_upoly_is_cst(up1
)) {
138 struct isl_upoly_cst
*cst1
, *cst2
;
139 cst1
= isl_upoly_as_cst(up1
);
140 cst2
= isl_upoly_as_cst(up2
);
142 return isl_bool_error
;
143 return isl_int_eq(cst1
->n
, cst2
->n
) &&
144 isl_int_eq(cst1
->d
, cst2
->d
);
147 rec1
= isl_upoly_as_rec(up1
);
148 rec2
= isl_upoly_as_rec(up2
);
150 return isl_bool_error
;
152 if (rec1
->n
!= rec2
->n
)
153 return isl_bool_false
;
155 for (i
= 0; i
< rec1
->n
; ++i
) {
156 isl_bool eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
161 return isl_bool_true
;
164 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
166 struct isl_upoly_cst
*cst
;
170 if (!isl_upoly_is_cst(up
))
173 cst
= isl_upoly_as_cst(up
);
177 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
180 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
182 struct isl_upoly_cst
*cst
;
186 if (!isl_upoly_is_cst(up
))
189 cst
= isl_upoly_as_cst(up
);
193 return isl_int_sgn(cst
->n
);
196 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
198 struct isl_upoly_cst
*cst
;
202 if (!isl_upoly_is_cst(up
))
205 cst
= isl_upoly_as_cst(up
);
209 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
212 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
214 struct isl_upoly_cst
*cst
;
218 if (!isl_upoly_is_cst(up
))
221 cst
= isl_upoly_as_cst(up
);
225 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
228 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
230 struct isl_upoly_cst
*cst
;
234 if (!isl_upoly_is_cst(up
))
237 cst
= isl_upoly_as_cst(up
);
241 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
244 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
246 struct isl_upoly_cst
*cst
;
250 if (!isl_upoly_is_cst(up
))
253 cst
= isl_upoly_as_cst(up
);
257 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
260 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
262 struct isl_upoly_cst
*cst
;
266 if (!isl_upoly_is_cst(up
))
269 cst
= isl_upoly_as_cst(up
);
273 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
276 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
278 struct isl_upoly_cst
*cst
;
280 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
289 isl_int_init(cst
->n
);
290 isl_int_init(cst
->d
);
295 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
297 struct isl_upoly_cst
*cst
;
299 cst
= isl_upoly_cst_alloc(ctx
);
303 isl_int_set_si(cst
->n
, 0);
304 isl_int_set_si(cst
->d
, 1);
309 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
311 struct isl_upoly_cst
*cst
;
313 cst
= isl_upoly_cst_alloc(ctx
);
317 isl_int_set_si(cst
->n
, 1);
318 isl_int_set_si(cst
->d
, 1);
323 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
325 struct isl_upoly_cst
*cst
;
327 cst
= isl_upoly_cst_alloc(ctx
);
331 isl_int_set_si(cst
->n
, 1);
332 isl_int_set_si(cst
->d
, 0);
337 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
339 struct isl_upoly_cst
*cst
;
341 cst
= isl_upoly_cst_alloc(ctx
);
345 isl_int_set_si(cst
->n
, -1);
346 isl_int_set_si(cst
->d
, 0);
351 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
353 struct isl_upoly_cst
*cst
;
355 cst
= isl_upoly_cst_alloc(ctx
);
359 isl_int_set_si(cst
->n
, 0);
360 isl_int_set_si(cst
->d
, 0);
365 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
366 isl_int n
, isl_int d
)
368 struct isl_upoly_cst
*cst
;
370 cst
= isl_upoly_cst_alloc(ctx
);
374 isl_int_set(cst
->n
, n
);
375 isl_int_set(cst
->d
, d
);
380 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
383 struct isl_upoly_rec
*rec
;
385 isl_assert(ctx
, var
>= 0, return NULL
);
386 isl_assert(ctx
, size
>= 0, return NULL
);
387 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
388 sizeof(struct isl_upoly_rec
) +
389 size
* sizeof(struct isl_upoly
*));
404 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
405 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
407 qp
= isl_qpolynomial_cow(qp
);
411 isl_space_free(qp
->dim
);
416 isl_qpolynomial_free(qp
);
421 /* Reset the space of "qp". This function is called from isl_pw_templ.c
422 * and doesn't know if the space of an element object is represented
423 * directly or through its domain. It therefore passes along both.
425 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
426 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
427 __isl_take isl_space
*domain
)
429 isl_space_free(space
);
430 return isl_qpolynomial_reset_domain_space(qp
, domain
);
433 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
435 return qp
? qp
->dim
->ctx
: NULL
;
438 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
439 __isl_keep isl_qpolynomial
*qp
)
441 return qp
? isl_space_copy(qp
->dim
) : NULL
;
444 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
449 space
= isl_space_copy(qp
->dim
);
450 space
= isl_space_from_domain(space
);
451 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
455 /* Return the number of variables of the given type in the domain of "qp".
457 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
458 enum isl_dim_type type
)
462 if (type
== isl_dim_div
)
463 return qp
->div
->n_row
;
464 if (type
== isl_dim_all
)
465 return isl_space_dim(qp
->dim
, isl_dim_all
) +
466 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
467 return isl_space_dim(qp
->dim
, type
);
470 /* Externally, an isl_qpolynomial has a map space, but internally, the
471 * ls field corresponds to the domain of that space.
473 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
474 enum isl_dim_type type
)
478 if (type
== isl_dim_out
)
480 if (type
== isl_dim_in
)
482 return isl_qpolynomial_domain_dim(qp
, type
);
485 /* Return the offset of the first coefficient of type "type" in
486 * the domain of "qp".
488 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
489 enum isl_dim_type type
)
498 return 1 + isl_space_offset(qp
->dim
, type
);
500 return 1 + isl_space_dim(qp
->dim
, isl_dim_all
);
506 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
508 return qp
? isl_upoly_is_zero(qp
->upoly
) : isl_bool_error
;
511 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
513 return qp
? isl_upoly_is_one(qp
->upoly
) : isl_bool_error
;
516 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
518 return qp
? isl_upoly_is_nan(qp
->upoly
) : isl_bool_error
;
521 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
523 return qp
? isl_upoly_is_infty(qp
->upoly
) : isl_bool_error
;
526 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
528 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : isl_bool_error
;
531 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
533 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
536 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
538 isl_int_clear(cst
->n
);
539 isl_int_clear(cst
->d
);
542 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
546 for (i
= 0; i
< rec
->n
; ++i
)
547 isl_upoly_free(rec
->p
[i
]);
550 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
559 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
561 struct isl_upoly_cst
*cst
;
562 struct isl_upoly_cst
*dup
;
564 cst
= isl_upoly_as_cst(up
);
568 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
571 isl_int_set(dup
->n
, cst
->n
);
572 isl_int_set(dup
->d
, cst
->d
);
577 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
580 struct isl_upoly_rec
*rec
;
581 struct isl_upoly_rec
*dup
;
583 rec
= isl_upoly_as_rec(up
);
587 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
591 for (i
= 0; i
< rec
->n
; ++i
) {
592 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
600 isl_upoly_free(&dup
->up
);
604 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
609 if (isl_upoly_is_cst(up
))
610 return isl_upoly_dup_cst(up
);
612 return isl_upoly_dup_rec(up
);
615 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
623 return isl_upoly_dup(up
);
626 __isl_null
struct isl_upoly
*isl_upoly_free(__isl_take
struct isl_upoly
*up
)
635 upoly_free_cst((struct isl_upoly_cst
*)up
);
637 upoly_free_rec((struct isl_upoly_rec
*)up
);
639 isl_ctx_deref(up
->ctx
);
644 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
649 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
650 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
651 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
652 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
657 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
658 __isl_take
struct isl_upoly
*up2
)
660 struct isl_upoly_cst
*cst1
;
661 struct isl_upoly_cst
*cst2
;
663 up1
= isl_upoly_cow(up1
);
667 cst1
= isl_upoly_as_cst(up1
);
668 cst2
= isl_upoly_as_cst(up2
);
670 if (isl_int_eq(cst1
->d
, cst2
->d
))
671 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
673 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
674 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
675 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
678 isl_upoly_cst_reduce(cst1
);
688 static __isl_give
struct isl_upoly
*replace_by_zero(
689 __isl_take
struct isl_upoly
*up
)
697 return isl_upoly_zero(ctx
);
700 static __isl_give
struct isl_upoly
*replace_by_constant_term(
701 __isl_take
struct isl_upoly
*up
)
703 struct isl_upoly_rec
*rec
;
704 struct isl_upoly
*cst
;
709 rec
= isl_upoly_as_rec(up
);
712 cst
= isl_upoly_copy(rec
->p
[0]);
720 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
721 __isl_take
struct isl_upoly
*up2
)
724 struct isl_upoly_rec
*rec1
, *rec2
;
729 if (isl_upoly_is_nan(up1
)) {
734 if (isl_upoly_is_nan(up2
)) {
739 if (isl_upoly_is_zero(up1
)) {
744 if (isl_upoly_is_zero(up2
)) {
749 if (up1
->var
< up2
->var
)
750 return isl_upoly_sum(up2
, up1
);
752 if (up2
->var
< up1
->var
) {
753 struct isl_upoly_rec
*rec
;
754 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
758 up1
= isl_upoly_cow(up1
);
759 rec
= isl_upoly_as_rec(up1
);
762 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
764 up1
= replace_by_constant_term(up1
);
768 if (isl_upoly_is_cst(up1
))
769 return isl_upoly_sum_cst(up1
, up2
);
771 rec1
= isl_upoly_as_rec(up1
);
772 rec2
= isl_upoly_as_rec(up2
);
776 if (rec1
->n
< rec2
->n
)
777 return isl_upoly_sum(up2
, up1
);
779 up1
= isl_upoly_cow(up1
);
780 rec1
= isl_upoly_as_rec(up1
);
784 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
785 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
786 isl_upoly_copy(rec2
->p
[i
]));
789 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
790 isl_upoly_free(rec1
->p
[i
]);
796 up1
= replace_by_zero(up1
);
797 else if (rec1
->n
== 1)
798 up1
= replace_by_constant_term(up1
);
809 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
810 __isl_take
struct isl_upoly
*up
, isl_int v
)
812 struct isl_upoly_cst
*cst
;
814 up
= isl_upoly_cow(up
);
818 cst
= isl_upoly_as_cst(up
);
820 isl_int_addmul(cst
->n
, cst
->d
, v
);
825 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
826 __isl_take
struct isl_upoly
*up
, isl_int v
)
828 struct isl_upoly_rec
*rec
;
833 if (isl_upoly_is_cst(up
))
834 return isl_upoly_cst_add_isl_int(up
, v
);
836 up
= isl_upoly_cow(up
);
837 rec
= isl_upoly_as_rec(up
);
841 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
851 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
852 __isl_take
struct isl_upoly
*up
, isl_int v
)
854 struct isl_upoly_cst
*cst
;
856 if (isl_upoly_is_zero(up
))
859 up
= isl_upoly_cow(up
);
863 cst
= isl_upoly_as_cst(up
);
865 isl_int_mul(cst
->n
, cst
->n
, v
);
870 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
871 __isl_take
struct isl_upoly
*up
, isl_int v
)
874 struct isl_upoly_rec
*rec
;
879 if (isl_upoly_is_cst(up
))
880 return isl_upoly_cst_mul_isl_int(up
, v
);
882 up
= isl_upoly_cow(up
);
883 rec
= isl_upoly_as_rec(up
);
887 for (i
= 0; i
< rec
->n
; ++i
) {
888 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
899 /* Multiply the constant polynomial "up" by "v".
901 static __isl_give
struct isl_upoly
*isl_upoly_cst_scale_val(
902 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
904 struct isl_upoly_cst
*cst
;
906 if (isl_upoly_is_zero(up
))
909 up
= isl_upoly_cow(up
);
913 cst
= isl_upoly_as_cst(up
);
915 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
916 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
917 isl_upoly_cst_reduce(cst
);
922 /* Multiply the polynomial "up" by "v".
924 static __isl_give
struct isl_upoly
*isl_upoly_scale_val(
925 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
928 struct isl_upoly_rec
*rec
;
933 if (isl_upoly_is_cst(up
))
934 return isl_upoly_cst_scale_val(up
, v
);
936 up
= isl_upoly_cow(up
);
937 rec
= isl_upoly_as_rec(up
);
941 for (i
= 0; i
< rec
->n
; ++i
) {
942 rec
->p
[i
] = isl_upoly_scale_val(rec
->p
[i
], v
);
953 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
954 __isl_take
struct isl_upoly
*up2
)
956 struct isl_upoly_cst
*cst1
;
957 struct isl_upoly_cst
*cst2
;
959 up1
= isl_upoly_cow(up1
);
963 cst1
= isl_upoly_as_cst(up1
);
964 cst2
= isl_upoly_as_cst(up2
);
966 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
967 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
969 isl_upoly_cst_reduce(cst1
);
979 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
980 __isl_take
struct isl_upoly
*up2
)
982 struct isl_upoly_rec
*rec1
;
983 struct isl_upoly_rec
*rec2
;
984 struct isl_upoly_rec
*res
= NULL
;
988 rec1
= isl_upoly_as_rec(up1
);
989 rec2
= isl_upoly_as_rec(up2
);
992 size
= rec1
->n
+ rec2
->n
- 1;
993 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
997 for (i
= 0; i
< rec1
->n
; ++i
) {
998 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
999 isl_upoly_copy(rec1
->p
[i
]));
1004 for (; i
< size
; ++i
) {
1005 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
1010 for (i
= 0; i
< rec1
->n
; ++i
) {
1011 for (j
= 1; j
< rec2
->n
; ++j
) {
1012 struct isl_upoly
*up
;
1013 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
1014 isl_upoly_copy(rec1
->p
[i
]));
1015 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
1021 isl_upoly_free(up1
);
1022 isl_upoly_free(up2
);
1026 isl_upoly_free(up1
);
1027 isl_upoly_free(up2
);
1028 isl_upoly_free(&res
->up
);
1032 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
1033 __isl_take
struct isl_upoly
*up2
)
1038 if (isl_upoly_is_nan(up1
)) {
1039 isl_upoly_free(up2
);
1043 if (isl_upoly_is_nan(up2
)) {
1044 isl_upoly_free(up1
);
1048 if (isl_upoly_is_zero(up1
)) {
1049 isl_upoly_free(up2
);
1053 if (isl_upoly_is_zero(up2
)) {
1054 isl_upoly_free(up1
);
1058 if (isl_upoly_is_one(up1
)) {
1059 isl_upoly_free(up1
);
1063 if (isl_upoly_is_one(up2
)) {
1064 isl_upoly_free(up2
);
1068 if (up1
->var
< up2
->var
)
1069 return isl_upoly_mul(up2
, up1
);
1071 if (up2
->var
< up1
->var
) {
1073 struct isl_upoly_rec
*rec
;
1074 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
1075 isl_ctx
*ctx
= up1
->ctx
;
1076 isl_upoly_free(up1
);
1077 isl_upoly_free(up2
);
1078 return isl_upoly_nan(ctx
);
1080 up1
= isl_upoly_cow(up1
);
1081 rec
= isl_upoly_as_rec(up1
);
1085 for (i
= 0; i
< rec
->n
; ++i
) {
1086 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
1087 isl_upoly_copy(up2
));
1091 isl_upoly_free(up2
);
1095 if (isl_upoly_is_cst(up1
))
1096 return isl_upoly_mul_cst(up1
, up2
);
1098 return isl_upoly_mul_rec(up1
, up2
);
1100 isl_upoly_free(up1
);
1101 isl_upoly_free(up2
);
1105 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
1108 struct isl_upoly
*res
;
1116 res
= isl_upoly_copy(up
);
1118 res
= isl_upoly_one(up
->ctx
);
1120 while (power
>>= 1) {
1121 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
1123 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
1130 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*dim
,
1131 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1133 struct isl_qpolynomial
*qp
= NULL
;
1139 if (!isl_space_is_set(dim
))
1140 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1141 "domain of polynomial should be a set", goto error
);
1143 total
= isl_space_dim(dim
, isl_dim_all
);
1145 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
1150 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1159 isl_space_free(dim
);
1161 isl_qpolynomial_free(qp
);
1165 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1174 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1176 struct isl_qpolynomial
*dup
;
1181 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1182 isl_upoly_copy(qp
->upoly
));
1185 isl_mat_free(dup
->div
);
1186 dup
->div
= isl_mat_copy(qp
->div
);
1192 isl_qpolynomial_free(dup
);
1196 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1204 return isl_qpolynomial_dup(qp
);
1207 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1208 __isl_take isl_qpolynomial
*qp
)
1216 isl_space_free(qp
->dim
);
1217 isl_mat_free(qp
->div
);
1218 isl_upoly_free(qp
->upoly
);
1224 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1227 struct isl_upoly_rec
*rec
;
1228 struct isl_upoly_cst
*cst
;
1230 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1233 for (i
= 0; i
< 1 + power
; ++i
) {
1234 rec
->p
[i
] = isl_upoly_zero(ctx
);
1239 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1240 isl_int_set_si(cst
->n
, 1);
1244 isl_upoly_free(&rec
->up
);
1248 /* r array maps original positions to new positions.
1250 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1254 struct isl_upoly_rec
*rec
;
1255 struct isl_upoly
*base
;
1256 struct isl_upoly
*res
;
1258 if (isl_upoly_is_cst(up
))
1261 rec
= isl_upoly_as_rec(up
);
1265 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1267 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1268 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1270 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1271 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1272 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1275 isl_upoly_free(base
);
1284 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1285 __isl_keep isl_mat
*div2
)
1290 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1291 div1
->n_col
>= div2
->n_col
,
1292 return isl_bool_error
);
1294 if (div1
->n_row
== div2
->n_row
)
1295 return isl_mat_is_equal(div1
, div2
);
1297 n_row
= div1
->n_row
;
1298 n_col
= div1
->n_col
;
1299 div1
->n_row
= div2
->n_row
;
1300 div1
->n_col
= div2
->n_col
;
1302 equal
= isl_mat_is_equal(div1
, div2
);
1304 div1
->n_row
= n_row
;
1305 div1
->n_col
= n_col
;
1310 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1314 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1315 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1320 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1323 struct isl_div_sort_info
{
1328 static int div_sort_cmp(const void *p1
, const void *p2
)
1330 const struct isl_div_sort_info
*i1
, *i2
;
1331 i1
= (const struct isl_div_sort_info
*) p1
;
1332 i2
= (const struct isl_div_sort_info
*) p2
;
1334 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1337 /* Sort divs and remove duplicates.
1339 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1344 struct isl_div_sort_info
*array
= NULL
;
1345 int *pos
= NULL
, *at
= NULL
;
1346 int *reordering
= NULL
;
1351 if (qp
->div
->n_row
<= 1)
1354 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1356 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1358 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1359 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1360 len
= qp
->div
->n_col
- 2;
1361 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1362 if (!array
|| !pos
|| !at
|| !reordering
)
1365 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1366 array
[i
].div
= qp
->div
;
1372 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1375 for (i
= 0; i
< div_pos
; ++i
)
1378 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1379 if (pos
[array
[i
].row
] == i
)
1381 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1382 pos
[at
[i
]] = pos
[array
[i
].row
];
1383 at
[pos
[array
[i
].row
]] = at
[i
];
1384 at
[i
] = array
[i
].row
;
1385 pos
[array
[i
].row
] = i
;
1389 for (i
= 0; i
< len
- div_pos
; ++i
) {
1391 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1392 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1393 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1394 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1395 2 + div_pos
+ i
- skip
);
1396 qp
->div
= isl_mat_drop_cols(qp
->div
,
1397 2 + div_pos
+ i
- skip
, 1);
1400 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1403 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1405 if (!qp
->upoly
|| !qp
->div
)
1419 isl_qpolynomial_free(qp
);
1423 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1424 int *exp
, int first
)
1427 struct isl_upoly_rec
*rec
;
1429 if (isl_upoly_is_cst(up
))
1432 if (up
->var
< first
)
1435 if (exp
[up
->var
- first
] == up
->var
- first
)
1438 up
= isl_upoly_cow(up
);
1442 up
->var
= exp
[up
->var
- first
] + first
;
1444 rec
= isl_upoly_as_rec(up
);
1448 for (i
= 0; i
< rec
->n
; ++i
) {
1449 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1460 static __isl_give isl_qpolynomial
*with_merged_divs(
1461 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1462 __isl_take isl_qpolynomial
*qp2
),
1463 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1467 isl_mat
*div
= NULL
;
1470 qp1
= isl_qpolynomial_cow(qp1
);
1471 qp2
= isl_qpolynomial_cow(qp2
);
1476 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1477 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1479 n_div1
= qp1
->div
->n_row
;
1480 n_div2
= qp2
->div
->n_row
;
1481 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1482 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1483 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1486 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1490 isl_mat_free(qp1
->div
);
1491 qp1
->div
= isl_mat_copy(div
);
1492 isl_mat_free(qp2
->div
);
1493 qp2
->div
= isl_mat_copy(div
);
1495 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1496 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1498 if (!qp1
->upoly
|| !qp2
->upoly
)
1505 return fn(qp1
, qp2
);
1510 isl_qpolynomial_free(qp1
);
1511 isl_qpolynomial_free(qp2
);
1515 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1516 __isl_take isl_qpolynomial
*qp2
)
1518 isl_bool compatible
;
1520 qp1
= isl_qpolynomial_cow(qp1
);
1525 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1526 return isl_qpolynomial_add(qp2
, qp1
);
1528 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1529 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1533 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1535 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1539 isl_qpolynomial_free(qp2
);
1543 isl_qpolynomial_free(qp1
);
1544 isl_qpolynomial_free(qp2
);
1548 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1549 __isl_keep isl_set
*dom
,
1550 __isl_take isl_qpolynomial
*qp1
,
1551 __isl_take isl_qpolynomial
*qp2
)
1553 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1554 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1558 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1559 __isl_take isl_qpolynomial
*qp2
)
1561 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1564 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1565 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1567 if (isl_int_is_zero(v
))
1570 qp
= isl_qpolynomial_cow(qp
);
1574 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1580 isl_qpolynomial_free(qp
);
1585 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1590 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1593 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1594 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1596 if (isl_int_is_one(v
))
1599 if (qp
&& isl_int_is_zero(v
)) {
1600 isl_qpolynomial
*zero
;
1601 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1602 isl_qpolynomial_free(qp
);
1606 qp
= isl_qpolynomial_cow(qp
);
1610 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1616 isl_qpolynomial_free(qp
);
1620 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1621 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1623 return isl_qpolynomial_mul_isl_int(qp
, v
);
1626 /* Multiply "qp" by "v".
1628 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1629 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1634 if (!isl_val_is_rat(v
))
1635 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1636 "expecting rational factor", goto error
);
1638 if (isl_val_is_one(v
)) {
1643 if (isl_val_is_zero(v
)) {
1646 space
= isl_qpolynomial_get_domain_space(qp
);
1647 isl_qpolynomial_free(qp
);
1649 return isl_qpolynomial_zero_on_domain(space
);
1652 qp
= isl_qpolynomial_cow(qp
);
1656 qp
->upoly
= isl_upoly_scale_val(qp
->upoly
, v
);
1658 qp
= isl_qpolynomial_free(qp
);
1664 isl_qpolynomial_free(qp
);
1668 /* Divide "qp" by "v".
1670 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1671 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1676 if (!isl_val_is_rat(v
))
1677 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1678 "expecting rational factor", goto error
);
1679 if (isl_val_is_zero(v
))
1680 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1681 "cannot scale down by zero", goto error
);
1683 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1686 isl_qpolynomial_free(qp
);
1690 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1691 __isl_take isl_qpolynomial
*qp2
)
1693 isl_bool compatible
;
1695 qp1
= isl_qpolynomial_cow(qp1
);
1700 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1701 return isl_qpolynomial_mul(qp2
, qp1
);
1703 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1704 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1708 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1710 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1714 isl_qpolynomial_free(qp2
);
1718 isl_qpolynomial_free(qp1
);
1719 isl_qpolynomial_free(qp2
);
1723 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1726 qp
= isl_qpolynomial_cow(qp
);
1731 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1737 isl_qpolynomial_free(qp
);
1741 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1742 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1749 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1753 for (i
= 0; i
< pwqp
->n
; ++i
) {
1754 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1756 return isl_pw_qpolynomial_free(pwqp
);
1762 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1763 __isl_take isl_space
*dim
)
1767 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1770 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1771 __isl_take isl_space
*dim
)
1775 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1778 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1779 __isl_take isl_space
*dim
)
1783 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1786 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1787 __isl_take isl_space
*dim
)
1791 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1794 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1795 __isl_take isl_space
*dim
)
1799 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1802 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1803 __isl_take isl_space
*dim
,
1806 struct isl_qpolynomial
*qp
;
1807 struct isl_upoly_cst
*cst
;
1812 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1816 cst
= isl_upoly_as_cst(qp
->upoly
);
1817 isl_int_set(cst
->n
, v
);
1822 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1823 isl_int
*n
, isl_int
*d
)
1825 struct isl_upoly_cst
*cst
;
1830 if (!isl_upoly_is_cst(qp
->upoly
))
1833 cst
= isl_upoly_as_cst(qp
->upoly
);
1838 isl_int_set(*n
, cst
->n
);
1840 isl_int_set(*d
, cst
->d
);
1845 /* Return the constant term of "up".
1847 static __isl_give isl_val
*isl_upoly_get_constant_val(
1848 __isl_keep
struct isl_upoly
*up
)
1850 struct isl_upoly_cst
*cst
;
1855 while (!isl_upoly_is_cst(up
)) {
1856 struct isl_upoly_rec
*rec
;
1858 rec
= isl_upoly_as_rec(up
);
1864 cst
= isl_upoly_as_cst(up
);
1867 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1870 /* Return the constant term of "qp".
1872 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1873 __isl_keep isl_qpolynomial
*qp
)
1878 return isl_upoly_get_constant_val(qp
->upoly
);
1881 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1884 struct isl_upoly_rec
*rec
;
1892 rec
= isl_upoly_as_rec(up
);
1899 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1901 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1907 return isl_upoly_is_affine(rec
->p
[0]);
1910 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1915 if (qp
->div
->n_row
> 0)
1918 return isl_upoly_is_affine(qp
->upoly
);
1921 static void update_coeff(__isl_keep isl_vec
*aff
,
1922 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1927 if (isl_int_is_zero(cst
->n
))
1932 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1933 isl_int_divexact(f
, cst
->d
, gcd
);
1934 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1935 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1936 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1941 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1942 __isl_keep isl_vec
*aff
)
1944 struct isl_upoly_cst
*cst
;
1945 struct isl_upoly_rec
*rec
;
1951 struct isl_upoly_cst
*cst
;
1953 cst
= isl_upoly_as_cst(up
);
1956 update_coeff(aff
, cst
, 0);
1960 rec
= isl_upoly_as_rec(up
);
1963 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1965 cst
= isl_upoly_as_cst(rec
->p
[1]);
1968 update_coeff(aff
, cst
, 1 + up
->var
);
1970 return isl_upoly_update_affine(rec
->p
[0], aff
);
1973 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1974 __isl_keep isl_qpolynomial
*qp
)
1982 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1983 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1987 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1988 isl_int_set_si(aff
->el
[0], 1);
1990 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1999 /* Compare two quasi-polynomials.
2001 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2002 * than "qp2" and 0 if they are equal.
2004 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2005 __isl_keep isl_qpolynomial
*qp2
)
2016 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2020 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2024 return isl_upoly_plain_cmp(qp1
->upoly
, qp2
->upoly
);
2027 /* Is "qp1" obviously equal to "qp2"?
2029 * NaN is not equal to anything, not even to another NaN.
2031 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2032 __isl_keep isl_qpolynomial
*qp2
)
2037 return isl_bool_error
;
2039 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2040 return isl_bool_false
;
2042 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2043 if (equal
< 0 || !equal
)
2046 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2047 if (equal
< 0 || !equal
)
2050 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
2053 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
2056 struct isl_upoly_rec
*rec
;
2058 if (isl_upoly_is_cst(up
)) {
2059 struct isl_upoly_cst
*cst
;
2060 cst
= isl_upoly_as_cst(up
);
2063 isl_int_lcm(*d
, *d
, cst
->d
);
2067 rec
= isl_upoly_as_rec(up
);
2071 for (i
= 0; i
< rec
->n
; ++i
)
2072 upoly_update_den(rec
->p
[i
], d
);
2075 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
2077 isl_int_set_si(*d
, 1);
2080 upoly_update_den(qp
->upoly
, d
);
2083 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2084 __isl_take isl_space
*dim
, int pos
, int power
)
2086 struct isl_ctx
*ctx
;
2093 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
2096 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(__isl_take isl_space
*dim
,
2097 enum isl_dim_type type
, unsigned pos
)
2102 isl_assert(dim
->ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, goto error
);
2103 isl_assert(dim
->ctx
, pos
< isl_space_dim(dim
, type
), goto error
);
2105 if (type
== isl_dim_set
)
2106 pos
+= isl_space_dim(dim
, isl_dim_param
);
2108 return isl_qpolynomial_var_pow_on_domain(dim
, pos
, 1);
2110 isl_space_free(dim
);
2114 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2115 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2118 struct isl_upoly_rec
*rec
;
2119 struct isl_upoly
*base
, *res
;
2124 if (isl_upoly_is_cst(up
))
2127 if (up
->var
< first
)
2130 rec
= isl_upoly_as_rec(up
);
2134 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2136 if (up
->var
>= first
+ n
)
2137 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2139 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2141 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2142 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2143 struct isl_upoly
*t
;
2144 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2145 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2146 res
= isl_upoly_sum(res
, t
);
2149 isl_upoly_free(base
);
2158 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2159 isl_int denom
, unsigned len
)
2162 struct isl_upoly
*up
;
2164 isl_assert(ctx
, len
>= 1, return NULL
);
2166 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2167 for (i
= 0; i
< len
- 1; ++i
) {
2168 struct isl_upoly
*t
;
2169 struct isl_upoly
*c
;
2171 if (isl_int_is_zero(f
[1 + i
]))
2174 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2175 t
= isl_upoly_var_pow(ctx
, i
, 1);
2176 t
= isl_upoly_mul(c
, t
);
2177 up
= isl_upoly_sum(up
, t
);
2183 /* Remove common factor of non-constant terms and denominator.
2185 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2187 isl_ctx
*ctx
= qp
->div
->ctx
;
2188 unsigned total
= qp
->div
->n_col
- 2;
2190 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2191 isl_int_gcd(ctx
->normalize_gcd
,
2192 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2193 if (isl_int_is_one(ctx
->normalize_gcd
))
2196 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2197 ctx
->normalize_gcd
, total
);
2198 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2199 ctx
->normalize_gcd
);
2200 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2201 ctx
->normalize_gcd
);
2204 /* Replace the integer division identified by "div" by the polynomial "s".
2205 * The integer division is assumed not to appear in the definition
2206 * of any other integer divisions.
2208 static __isl_give isl_qpolynomial
*substitute_div(
2209 __isl_take isl_qpolynomial
*qp
,
2210 int div
, __isl_take
struct isl_upoly
*s
)
2219 qp
= isl_qpolynomial_cow(qp
);
2223 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2224 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2228 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2231 for (i
= 0; i
< total
+ div
; ++i
)
2233 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2234 reordering
[i
] = i
- 1;
2235 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2236 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2237 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2240 if (!qp
->upoly
|| !qp
->div
)
2246 isl_qpolynomial_free(qp
);
2251 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2252 * divisions because d is equal to 1 by their definition, i.e., e.
2254 static __isl_give isl_qpolynomial
*substitute_non_divs(
2255 __isl_take isl_qpolynomial
*qp
)
2259 struct isl_upoly
*s
;
2264 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2265 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2266 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2268 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2269 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2271 isl_seq_combine(qp
->div
->row
[j
] + 1,
2272 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2273 qp
->div
->row
[j
][2 + total
+ i
],
2274 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2275 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2276 normalize_div(qp
, j
);
2278 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2279 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2280 qp
= substitute_div(qp
, i
, s
);
2287 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2288 * with d the denominator. When replacing the coefficient e of x by
2289 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2290 * inside the division, so we need to add floor(e/d) * x outside.
2291 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2292 * to adjust the coefficient of x in each later div that depends on the
2293 * current div "div" and also in the affine expressions in the rows of "mat"
2294 * (if they too depend on "div").
2296 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2297 __isl_keep isl_mat
**mat
)
2301 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2304 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2305 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2306 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2308 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2309 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2310 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2311 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2312 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2313 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2315 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2316 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2322 /* Check if the last non-zero coefficient is bigger that half of the
2323 * denominator. If so, we will invert the div to further reduce the number
2324 * of distinct divs that may appear.
2325 * If the last non-zero coefficient is exactly half the denominator,
2326 * then we continue looking for earlier coefficients that are bigger
2327 * than half the denominator.
2329 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2334 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2335 if (isl_int_is_zero(div
->row
[row
][i
]))
2337 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2338 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2339 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2349 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2350 * We only invert the coefficients of e (and the coefficient of q in
2351 * later divs and in the rows of "mat"). After calling this function, the
2352 * coefficients of e should be reduced again.
2354 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2355 __isl_keep isl_mat
**mat
)
2357 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2359 isl_seq_neg(qp
->div
->row
[div
] + 1,
2360 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2361 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2362 isl_int_add(qp
->div
->row
[div
][1],
2363 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2364 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2365 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2366 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2369 /* Reduce all divs of "qp" to have coefficients
2370 * in the interval [0, d-1], with d the denominator and such that the
2371 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2372 * The modifications to the integer divisions need to be reflected
2373 * in the factors of the polynomial that refer to the original
2374 * integer divisions. To this end, the modifications are collected
2375 * as a set of affine expressions and then plugged into the polynomial.
2377 * After the reduction, some divs may have become redundant or identical,
2378 * so we call substitute_non_divs and sort_divs. If these functions
2379 * eliminate divs or merge two or more divs into one, the coefficients
2380 * of the enclosing divs may have to be reduced again, so we call
2381 * ourselves recursively if the number of divs decreases.
2383 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2388 struct isl_upoly
**s
;
2389 unsigned o_div
, n_div
, total
;
2394 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2395 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2396 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2397 ctx
= isl_qpolynomial_get_ctx(qp
);
2398 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2400 for (i
= 0; i
< n_div
; ++i
)
2401 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2403 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2404 normalize_div(qp
, i
);
2405 reduce_div(qp
, i
, &mat
);
2406 if (needs_invert(qp
->div
, i
)) {
2407 invert_div(qp
, i
, &mat
);
2408 reduce_div(qp
, i
, &mat
);
2414 s
= isl_alloc_array(ctx
, struct isl_upoly
*, n_div
);
2417 for (i
= 0; i
< n_div
; ++i
)
2418 s
[i
] = isl_upoly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2420 qp
->upoly
= isl_upoly_subs(qp
->upoly
, o_div
- 1, n_div
, s
);
2421 for (i
= 0; i
< n_div
; ++i
)
2422 isl_upoly_free(s
[i
]);
2429 qp
= substitute_non_divs(qp
);
2431 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2432 return reduce_divs(qp
);
2436 isl_qpolynomial_free(qp
);
2441 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2442 __isl_take isl_space
*dim
, const isl_int n
, const isl_int d
)
2444 struct isl_qpolynomial
*qp
;
2445 struct isl_upoly_cst
*cst
;
2450 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2454 cst
= isl_upoly_as_cst(qp
->upoly
);
2455 isl_int_set(cst
->n
, n
);
2456 isl_int_set(cst
->d
, d
);
2461 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2463 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2464 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2466 isl_qpolynomial
*qp
;
2467 struct isl_upoly_cst
*cst
;
2469 if (!domain
|| !val
)
2472 qp
= isl_qpolynomial_alloc(isl_space_copy(domain
), 0,
2473 isl_upoly_zero(domain
->ctx
));
2477 cst
= isl_upoly_as_cst(qp
->upoly
);
2478 isl_int_set(cst
->n
, val
->n
);
2479 isl_int_set(cst
->d
, val
->d
);
2481 isl_space_free(domain
);
2485 isl_space_free(domain
);
2490 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2492 struct isl_upoly_rec
*rec
;
2498 if (isl_upoly_is_cst(up
))
2502 active
[up
->var
] = 1;
2504 rec
= isl_upoly_as_rec(up
);
2505 for (i
= 0; i
< rec
->n
; ++i
)
2506 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2512 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2515 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2520 for (i
= 0; i
< d
; ++i
)
2521 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2522 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2528 return up_set_active(qp
->upoly
, active
, d
);
2531 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2532 enum isl_dim_type type
, unsigned first
, unsigned n
)
2536 isl_bool involves
= isl_bool_false
;
2539 return isl_bool_error
;
2541 return isl_bool_false
;
2543 isl_assert(qp
->dim
->ctx
,
2544 first
+ n
<= isl_qpolynomial_dim(qp
, type
),
2545 return isl_bool_error
);
2546 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2547 type
== isl_dim_in
, return isl_bool_error
);
2549 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2550 isl_space_dim(qp
->dim
, isl_dim_all
));
2551 if (set_active(qp
, active
) < 0)
2554 if (type
== isl_dim_in
)
2555 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2556 for (i
= 0; i
< n
; ++i
)
2557 if (active
[first
+ i
]) {
2558 involves
= isl_bool_true
;
2567 return isl_bool_error
;
2570 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2571 * of the divs that do appear in the quasi-polynomial.
2573 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2574 __isl_take isl_qpolynomial
*qp
)
2581 int *reordering
= NULL
;
2588 if (qp
->div
->n_row
== 0)
2591 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2592 len
= qp
->div
->n_col
- 2;
2593 ctx
= isl_qpolynomial_get_ctx(qp
);
2594 active
= isl_calloc_array(ctx
, int, len
);
2598 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2601 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2602 if (!active
[d
+ i
]) {
2606 for (j
= 0; j
< i
; ++j
) {
2607 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2619 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2623 for (i
= 0; i
< d
; ++i
)
2627 n_div
= qp
->div
->n_row
;
2628 for (i
= 0; i
< n_div
; ++i
) {
2629 if (!active
[d
+ i
]) {
2630 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2631 qp
->div
= isl_mat_drop_cols(qp
->div
,
2632 2 + d
+ i
- skip
, 1);
2635 reordering
[d
+ i
] = d
+ i
- skip
;
2638 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2640 if (!qp
->upoly
|| !qp
->div
)
2650 isl_qpolynomial_free(qp
);
2654 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2655 unsigned first
, unsigned n
)
2658 struct isl_upoly_rec
*rec
;
2662 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2664 if (up
->var
< first
+ n
) {
2665 up
= replace_by_constant_term(up
);
2666 return isl_upoly_drop(up
, first
, n
);
2668 up
= isl_upoly_cow(up
);
2672 rec
= isl_upoly_as_rec(up
);
2676 for (i
= 0; i
< rec
->n
; ++i
) {
2677 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2688 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2689 __isl_take isl_qpolynomial
*qp
,
2690 enum isl_dim_type type
, unsigned pos
, const char *s
)
2692 qp
= isl_qpolynomial_cow(qp
);
2695 if (type
== isl_dim_out
)
2696 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2697 "cannot set name of output/set dimension",
2698 return isl_qpolynomial_free(qp
));
2699 if (type
== isl_dim_in
)
2701 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2706 isl_qpolynomial_free(qp
);
2710 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2711 __isl_take isl_qpolynomial
*qp
,
2712 enum isl_dim_type type
, unsigned first
, unsigned n
)
2716 if (type
== isl_dim_out
)
2717 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2718 "cannot drop output/set dimension",
2720 if (type
== isl_dim_in
)
2722 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2725 qp
= isl_qpolynomial_cow(qp
);
2729 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2731 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2732 type
== isl_dim_set
, goto error
);
2734 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2738 if (type
== isl_dim_set
)
2739 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2741 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2745 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2751 isl_qpolynomial_free(qp
);
2755 /* Project the domain of the quasi-polynomial onto its parameter space.
2756 * The quasi-polynomial may not involve any of the domain dimensions.
2758 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2759 __isl_take isl_qpolynomial
*qp
)
2765 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2766 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2768 return isl_qpolynomial_free(qp
);
2770 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2771 "polynomial involves some of the domain dimensions",
2772 return isl_qpolynomial_free(qp
));
2773 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2774 space
= isl_qpolynomial_get_domain_space(qp
);
2775 space
= isl_space_params(space
);
2776 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2780 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2781 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2787 struct isl_upoly
*up
;
2791 if (eq
->n_eq
== 0) {
2792 isl_basic_set_free(eq
);
2796 qp
= isl_qpolynomial_cow(qp
);
2799 qp
->div
= isl_mat_cow(qp
->div
);
2803 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2805 isl_int_init(denom
);
2806 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2807 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2808 if (j
< 0 || j
== 0 || j
>= total
)
2811 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2812 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2814 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2815 &qp
->div
->row
[k
][0]);
2816 normalize_div(qp
, k
);
2819 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2820 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2821 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2822 isl_int_set_si(eq
->eq
[i
][j
], 0);
2824 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2825 eq
->eq
[i
], denom
, total
);
2826 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2829 isl_int_clear(denom
);
2834 isl_basic_set_free(eq
);
2836 qp
= substitute_non_divs(qp
);
2841 isl_basic_set_free(eq
);
2842 isl_qpolynomial_free(qp
);
2846 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2848 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2849 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2853 if (qp
->div
->n_row
> 0)
2854 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2855 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2857 isl_basic_set_free(eq
);
2858 isl_qpolynomial_free(qp
);
2862 static __isl_give isl_basic_set
*add_div_constraints(
2863 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2871 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2874 total
= isl_basic_set_total_dim(bset
);
2875 for (i
= 0; i
< div
->n_row
; ++i
)
2876 if (isl_basic_set_add_div_constraints_var(bset
,
2877 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2884 isl_basic_set_free(bset
);
2888 /* Look for equalities among the variables shared by context and qp
2889 * and the integer divisions of qp, if any.
2890 * The equalities are then used to eliminate variables and/or integer
2891 * divisions from qp.
2893 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2894 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2900 if (qp
->div
->n_row
> 0) {
2901 isl_basic_set
*bset
;
2902 context
= isl_set_add_dims(context
, isl_dim_set
,
2904 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2905 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2906 context
= isl_set_intersect(context
,
2907 isl_set_from_basic_set(bset
));
2910 aff
= isl_set_affine_hull(context
);
2911 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2913 isl_qpolynomial_free(qp
);
2914 isl_set_free(context
);
2918 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2919 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2921 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2922 isl_set
*dom_context
= isl_set_universe(space
);
2923 dom_context
= isl_set_intersect_params(dom_context
, context
);
2924 return isl_qpolynomial_gist(qp
, dom_context
);
2927 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2928 __isl_take isl_qpolynomial
*qp
)
2934 if (isl_qpolynomial_is_zero(qp
)) {
2935 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2936 isl_qpolynomial_free(qp
);
2937 return isl_pw_qpolynomial_zero(dim
);
2940 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2941 return isl_pw_qpolynomial_alloc(dom
, qp
);
2944 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2947 #define PW isl_pw_qpolynomial
2949 #define EL isl_qpolynomial
2951 #define EL_IS_ZERO is_zero
2955 #define IS_ZERO is_zero
2958 #undef DEFAULT_IS_ZERO
2959 #define DEFAULT_IS_ZERO 1
2963 #include <isl_pw_templ.c>
2964 #include <isl_pw_eval.c>
2967 #define BASE pw_qpolynomial
2969 #include <isl_union_single.c>
2970 #include <isl_union_eval.c>
2971 #include <isl_union_neg.c>
2973 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2981 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2984 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2987 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2988 __isl_take isl_pw_qpolynomial
*pwqp1
,
2989 __isl_take isl_pw_qpolynomial
*pwqp2
)
2991 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2994 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2995 __isl_take isl_pw_qpolynomial
*pwqp1
,
2996 __isl_take isl_pw_qpolynomial
*pwqp2
)
2999 struct isl_pw_qpolynomial
*res
;
3001 if (!pwqp1
|| !pwqp2
)
3004 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3007 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3008 isl_pw_qpolynomial_free(pwqp2
);
3012 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3013 isl_pw_qpolynomial_free(pwqp1
);
3017 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3018 isl_pw_qpolynomial_free(pwqp1
);
3022 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3023 isl_pw_qpolynomial_free(pwqp2
);
3027 n
= pwqp1
->n
* pwqp2
->n
;
3028 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3030 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3031 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3032 struct isl_set
*common
;
3033 struct isl_qpolynomial
*prod
;
3034 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3035 isl_set_copy(pwqp2
->p
[j
].set
));
3036 if (isl_set_plain_is_empty(common
)) {
3037 isl_set_free(common
);
3041 prod
= isl_qpolynomial_mul(
3042 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3043 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3045 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3049 isl_pw_qpolynomial_free(pwqp1
);
3050 isl_pw_qpolynomial_free(pwqp2
);
3054 isl_pw_qpolynomial_free(pwqp1
);
3055 isl_pw_qpolynomial_free(pwqp2
);
3059 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
3060 __isl_take isl_vec
*vec
)
3063 struct isl_upoly_rec
*rec
;
3067 if (isl_upoly_is_cst(up
)) {
3069 res
= isl_upoly_get_constant_val(up
);
3074 rec
= isl_upoly_as_rec(up
);
3078 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
3080 base
= isl_val_rat_from_isl_int(up
->ctx
,
3081 vec
->el
[1 + up
->var
], vec
->el
[0]);
3083 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
3086 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3087 res
= isl_val_mul(res
, isl_val_copy(base
));
3088 res
= isl_val_add(res
,
3089 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
3090 isl_vec_copy(vec
)));
3103 /* Evaluate "qp" in the void point "pnt".
3104 * In particular, return the value NaN.
3106 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3107 __isl_take isl_point
*pnt
)
3111 ctx
= isl_point_get_ctx(pnt
);
3112 isl_qpolynomial_free(qp
);
3113 isl_point_free(pnt
);
3114 return isl_val_nan(ctx
);
3117 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3118 __isl_take isl_point
*pnt
)
3126 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3127 is_void
= isl_point_is_void(pnt
);
3131 return eval_void(qp
, pnt
);
3133 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3135 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
3137 isl_qpolynomial_free(qp
);
3138 isl_point_free(pnt
);
3142 isl_qpolynomial_free(qp
);
3143 isl_point_free(pnt
);
3147 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
3148 __isl_keep
struct isl_upoly_cst
*cst2
)
3153 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3154 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3155 cmp
= isl_int_sgn(t
);
3160 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3161 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3162 unsigned first
, unsigned n
)
3170 if (type
== isl_dim_out
)
3171 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3172 "cannot insert output/set dimensions",
3174 if (type
== isl_dim_in
)
3176 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3179 qp
= isl_qpolynomial_cow(qp
);
3183 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
3186 g_pos
= pos(qp
->dim
, type
) + first
;
3188 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3192 total
= qp
->div
->n_col
- 2;
3193 if (total
> g_pos
) {
3195 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3198 for (i
= 0; i
< total
- g_pos
; ++i
)
3200 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3206 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3212 isl_qpolynomial_free(qp
);
3216 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3217 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3221 pos
= isl_qpolynomial_dim(qp
, type
);
3223 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3226 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3227 __isl_take isl_pw_qpolynomial
*pwqp
,
3228 enum isl_dim_type type
, unsigned n
)
3232 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3234 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3237 static int *reordering_move(isl_ctx
*ctx
,
3238 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3243 reordering
= isl_alloc_array(ctx
, int, len
);
3248 for (i
= 0; i
< dst
; ++i
)
3250 for (i
= 0; i
< n
; ++i
)
3251 reordering
[src
+ i
] = dst
+ i
;
3252 for (i
= 0; i
< src
- dst
; ++i
)
3253 reordering
[dst
+ i
] = dst
+ n
+ i
;
3254 for (i
= 0; i
< len
- src
- n
; ++i
)
3255 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3257 for (i
= 0; i
< src
; ++i
)
3259 for (i
= 0; i
< n
; ++i
)
3260 reordering
[src
+ i
] = dst
+ i
;
3261 for (i
= 0; i
< dst
- src
; ++i
)
3262 reordering
[src
+ n
+ i
] = src
+ i
;
3263 for (i
= 0; i
< len
- dst
- n
; ++i
)
3264 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3270 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3271 __isl_take isl_qpolynomial
*qp
,
3272 enum isl_dim_type dst_type
, unsigned dst_pos
,
3273 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3282 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3283 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3284 "cannot move output/set dimension",
3286 if (dst_type
== isl_dim_in
)
3287 dst_type
= isl_dim_set
;
3288 if (src_type
== isl_dim_in
)
3289 src_type
= isl_dim_set
;
3292 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3293 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3296 qp
= isl_qpolynomial_cow(qp
);
3300 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3303 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3304 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3305 if (dst_type
> src_type
)
3308 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3315 reordering
= reordering_move(qp
->dim
->ctx
,
3316 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3320 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3325 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3331 isl_qpolynomial_free(qp
);
3335 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_space
*dim
,
3336 isl_int
*f
, isl_int denom
)
3338 struct isl_upoly
*up
;
3340 dim
= isl_space_domain(dim
);
3344 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
,
3345 1 + isl_space_dim(dim
, isl_dim_all
));
3347 return isl_qpolynomial_alloc(dim
, 0, up
);
3350 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3353 struct isl_upoly
*up
;
3354 isl_qpolynomial
*qp
;
3359 ctx
= isl_aff_get_ctx(aff
);
3360 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3363 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3364 aff
->ls
->div
->n_row
, up
);
3368 isl_mat_free(qp
->div
);
3369 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3370 qp
->div
= isl_mat_cow(qp
->div
);
3375 qp
= reduce_divs(qp
);
3376 qp
= remove_redundant_divs(qp
);
3380 return isl_qpolynomial_free(qp
);
3383 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3384 __isl_take isl_pw_aff
*pwaff
)
3387 isl_pw_qpolynomial
*pwqp
;
3392 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3395 for (i
= 0; i
< pwaff
->n
; ++i
) {
3397 isl_qpolynomial
*qp
;
3399 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3400 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3401 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3404 isl_pw_aff_free(pwaff
);
3408 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3409 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3413 aff
= isl_constraint_get_bound(c
, type
, pos
);
3414 isl_constraint_free(c
);
3415 return isl_qpolynomial_from_aff(aff
);
3418 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3419 * in "qp" by subs[i].
3421 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3422 __isl_take isl_qpolynomial
*qp
,
3423 enum isl_dim_type type
, unsigned first
, unsigned n
,
3424 __isl_keep isl_qpolynomial
**subs
)
3427 struct isl_upoly
**ups
;
3432 qp
= isl_qpolynomial_cow(qp
);
3436 if (type
== isl_dim_out
)
3437 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3438 "cannot substitute output/set dimension",
3440 if (type
== isl_dim_in
)
3443 for (i
= 0; i
< n
; ++i
)
3447 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3450 for (i
= 0; i
< n
; ++i
)
3451 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3454 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3455 for (i
= 0; i
< n
; ++i
)
3456 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3458 first
+= pos(qp
->dim
, type
);
3460 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3463 for (i
= 0; i
< n
; ++i
)
3464 ups
[i
] = subs
[i
]->upoly
;
3466 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3475 isl_qpolynomial_free(qp
);
3479 /* Extend "bset" with extra set dimensions for each integer division
3480 * in "qp" and then call "fn" with the extended bset and the polynomial
3481 * that results from replacing each of the integer divisions by the
3482 * corresponding extra set dimension.
3484 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3485 __isl_keep isl_basic_set
*bset
,
3486 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3487 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3491 isl_qpolynomial
*poly
;
3494 return isl_stat_error
;
3495 if (qp
->div
->n_row
== 0)
3496 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3499 div
= isl_mat_copy(qp
->div
);
3500 dim
= isl_space_copy(qp
->dim
);
3501 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3502 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3503 bset
= isl_basic_set_copy(bset
);
3504 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, qp
->div
->n_row
);
3505 bset
= add_div_constraints(bset
, div
);
3507 return fn(bset
, poly
, user
);
3510 /* Return total degree in variables first (inclusive) up to last (exclusive).
3512 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3516 struct isl_upoly_rec
*rec
;
3520 if (isl_upoly_is_zero(up
))
3522 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3525 rec
= isl_upoly_as_rec(up
);
3529 for (i
= 0; i
< rec
->n
; ++i
) {
3532 if (isl_upoly_is_zero(rec
->p
[i
]))
3534 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3544 /* Return total degree in set variables.
3546 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3554 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3555 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3556 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3559 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3560 unsigned pos
, int deg
)
3563 struct isl_upoly_rec
*rec
;
3568 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3570 return isl_upoly_copy(up
);
3572 return isl_upoly_zero(up
->ctx
);
3575 rec
= isl_upoly_as_rec(up
);
3579 if (up
->var
== pos
) {
3581 return isl_upoly_copy(rec
->p
[deg
]);
3583 return isl_upoly_zero(up
->ctx
);
3586 up
= isl_upoly_copy(up
);
3587 up
= isl_upoly_cow(up
);
3588 rec
= isl_upoly_as_rec(up
);
3592 for (i
= 0; i
< rec
->n
; ++i
) {
3593 struct isl_upoly
*t
;
3594 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3597 isl_upoly_free(rec
->p
[i
]);
3607 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3609 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3610 __isl_keep isl_qpolynomial
*qp
,
3611 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3614 struct isl_upoly
*up
;
3620 if (type
== isl_dim_out
)
3621 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3622 "output/set dimension does not have a coefficient",
3624 if (type
== isl_dim_in
)
3627 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3630 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3631 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3633 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3636 isl_mat_free(c
->div
);
3637 c
->div
= isl_mat_copy(qp
->div
);
3642 isl_qpolynomial_free(c
);
3646 /* Homogenize the polynomial in the variables first (inclusive) up to
3647 * last (exclusive) by inserting powers of variable first.
3648 * Variable first is assumed not to appear in the input.
3650 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3651 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3652 int first
, int last
)
3655 struct isl_upoly_rec
*rec
;
3659 if (isl_upoly_is_zero(up
))
3663 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3664 struct isl_upoly
*hom
;
3666 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3669 rec
= isl_upoly_as_rec(hom
);
3670 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3675 up
= isl_upoly_cow(up
);
3676 rec
= isl_upoly_as_rec(up
);
3680 for (i
= 0; i
< rec
->n
; ++i
) {
3681 if (isl_upoly_is_zero(rec
->p
[i
]))
3683 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3684 up
->var
< last
? deg
+ i
: i
, target
,
3696 /* Homogenize the polynomial in the set variables by introducing
3697 * powers of an extra set variable at position 0.
3699 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3700 __isl_take isl_qpolynomial
*poly
)
3704 int deg
= isl_qpolynomial_degree(poly
);
3709 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3710 poly
= isl_qpolynomial_cow(poly
);
3714 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3715 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3716 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3723 isl_qpolynomial_free(poly
);
3727 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*dim
,
3728 __isl_take isl_mat
*div
)
3736 n
= isl_space_dim(dim
, isl_dim_all
) + div
->n_row
;
3738 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3739 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3746 isl_int_init(term
->n
);
3747 isl_int_init(term
->d
);
3751 isl_space_free(dim
);
3756 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3765 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3774 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3776 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3780 isl_int_set(dup
->n
, term
->n
);
3781 isl_int_set(dup
->d
, term
->d
);
3783 for (i
= 0; i
< total
; ++i
)
3784 dup
->pow
[i
] = term
->pow
[i
];
3789 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3797 return isl_term_dup(term
);
3800 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3805 if (--term
->ref
> 0)
3808 isl_space_free(term
->dim
);
3809 isl_mat_free(term
->div
);
3810 isl_int_clear(term
->n
);
3811 isl_int_clear(term
->d
);
3817 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3825 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3826 case isl_dim_div
: return term
->div
->n_row
;
3827 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3833 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3835 return term
? term
->dim
->ctx
: NULL
;
3838 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3842 isl_int_set(*n
, term
->n
);
3845 /* Return the coefficient of the term "term".
3847 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3852 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3856 int isl_term_get_exp(__isl_keep isl_term
*term
,
3857 enum isl_dim_type type
, unsigned pos
)
3862 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3864 if (type
>= isl_dim_set
)
3865 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3866 if (type
>= isl_dim_div
)
3867 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3869 return term
->pow
[pos
];
3872 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3874 isl_local_space
*ls
;
3880 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3883 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3884 isl_mat_copy(term
->div
));
3885 aff
= isl_aff_alloc(ls
);
3889 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3891 aff
= isl_aff_normalize(aff
);
3896 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3897 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3898 __isl_take isl_term
*term
, void *user
)
3901 struct isl_upoly_rec
*rec
;
3906 if (isl_upoly_is_zero(up
))
3909 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3910 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3911 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3913 if (isl_upoly_is_cst(up
)) {
3914 struct isl_upoly_cst
*cst
;
3915 cst
= isl_upoly_as_cst(up
);
3918 term
= isl_term_cow(term
);
3921 isl_int_set(term
->n
, cst
->n
);
3922 isl_int_set(term
->d
, cst
->d
);
3923 if (fn(isl_term_copy(term
), user
) < 0)
3928 rec
= isl_upoly_as_rec(up
);
3932 for (i
= 0; i
< rec
->n
; ++i
) {
3933 term
= isl_term_cow(term
);
3936 term
->pow
[up
->var
] = i
;
3937 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3941 term
->pow
[up
->var
] = 0;
3945 isl_term_free(term
);
3949 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3950 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3955 return isl_stat_error
;
3957 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3959 return isl_stat_error
;
3961 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3963 isl_term_free(term
);
3965 return term
? isl_stat_ok
: isl_stat_error
;
3968 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3970 struct isl_upoly
*up
;
3971 isl_qpolynomial
*qp
;
3977 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3979 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3980 for (i
= 0; i
< n
; ++i
) {
3983 up
= isl_upoly_mul(up
,
3984 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3987 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
3990 isl_mat_free(qp
->div
);
3991 qp
->div
= isl_mat_copy(term
->div
);
3995 isl_term_free(term
);
3998 isl_qpolynomial_free(qp
);
3999 isl_term_free(term
);
4003 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4004 __isl_take isl_space
*dim
)
4013 if (isl_space_is_equal(qp
->dim
, dim
)) {
4014 isl_space_free(dim
);
4018 qp
= isl_qpolynomial_cow(qp
);
4022 extra
= isl_space_dim(dim
, isl_dim_set
) -
4023 isl_space_dim(qp
->dim
, isl_dim_set
);
4024 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4025 if (qp
->div
->n_row
) {
4028 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4031 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4033 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
4038 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4041 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4042 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4044 isl_space_free(qp
->dim
);
4049 isl_space_free(dim
);
4050 isl_qpolynomial_free(qp
);
4054 /* For each parameter or variable that does not appear in qp,
4055 * first eliminate the variable from all constraints and then set it to zero.
4057 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4058 __isl_keep isl_qpolynomial
*qp
)
4069 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4070 active
= isl_calloc_array(set
->ctx
, int, d
);
4071 if (set_active(qp
, active
) < 0)
4074 for (i
= 0; i
< d
; ++i
)
4083 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4084 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4085 for (i
= 0; i
< nparam
; ++i
) {
4088 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4089 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4091 for (i
= 0; i
< nvar
; ++i
) {
4092 if (active
[nparam
+ i
])
4094 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4095 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4107 struct isl_opt_data
{
4108 isl_qpolynomial
*qp
;
4114 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4116 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4119 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4123 } else if (data
->max
) {
4124 data
->opt
= isl_val_max(data
->opt
, val
);
4126 data
->opt
= isl_val_min(data
->opt
, val
);
4132 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4133 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4135 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4140 if (isl_upoly_is_cst(qp
->upoly
)) {
4142 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4143 isl_qpolynomial_free(qp
);
4147 set
= fix_inactive(set
, qp
);
4150 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4154 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4157 isl_qpolynomial_free(qp
);
4161 isl_qpolynomial_free(qp
);
4162 isl_val_free(data
.opt
);
4166 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4167 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4172 struct isl_upoly
**subs
;
4173 isl_mat
*mat
, *diag
;
4175 qp
= isl_qpolynomial_cow(qp
);
4180 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4182 n_sub
= morph
->inv
->n_row
- 1;
4183 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4184 n_sub
+= qp
->div
->n_row
;
4185 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4189 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4190 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4191 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4192 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4193 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4194 subs
[morph
->inv
->n_row
- 1 + i
] =
4195 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4197 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4199 for (i
= 0; i
< n_sub
; ++i
)
4200 isl_upoly_free(subs
[i
]);
4203 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4204 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4205 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4206 mat
= isl_mat_diagonal(mat
, diag
);
4207 qp
->div
= isl_mat_product(qp
->div
, mat
);
4208 isl_space_free(qp
->dim
);
4209 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4211 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4214 isl_morph_free(morph
);
4218 isl_qpolynomial_free(qp
);
4219 isl_morph_free(morph
);
4223 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4224 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4225 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4227 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4228 &isl_pw_qpolynomial_mul
);
4231 /* Reorder the dimension of "qp" according to the given reordering.
4233 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4234 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4238 qp
= isl_qpolynomial_cow(qp
);
4242 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4246 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4250 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4254 space
= isl_reordering_get_space(r
);
4255 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4257 isl_reordering_free(r
);
4260 isl_qpolynomial_free(qp
);
4261 isl_reordering_free(r
);
4265 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4266 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4268 isl_bool equal_params
;
4273 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4274 if (equal_params
< 0)
4276 if (!equal_params
) {
4277 isl_reordering
*exp
;
4279 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4280 exp
= isl_reordering_extend_space(exp
,
4281 isl_qpolynomial_get_domain_space(qp
));
4282 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4285 isl_space_free(model
);
4288 isl_space_free(model
);
4289 isl_qpolynomial_free(qp
);
4293 struct isl_split_periods_data
{
4295 isl_pw_qpolynomial
*res
;
4298 /* Create a slice where the integer division "div" has the fixed value "v".
4299 * In particular, if "div" refers to floor(f/m), then create a slice
4301 * m v <= f <= m v + (m - 1)
4306 * -f + m v + (m - 1) >= 0
4308 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*dim
,
4309 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4312 isl_basic_set
*bset
= NULL
;
4318 total
= isl_space_dim(dim
, isl_dim_all
);
4319 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0, 0, 2);
4321 k
= isl_basic_set_alloc_inequality(bset
);
4324 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4325 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4327 k
= isl_basic_set_alloc_inequality(bset
);
4330 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4331 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4332 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4333 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4335 isl_space_free(dim
);
4336 return isl_set_from_basic_set(bset
);
4338 isl_basic_set_free(bset
);
4339 isl_space_free(dim
);
4343 static isl_stat
split_periods(__isl_take isl_set
*set
,
4344 __isl_take isl_qpolynomial
*qp
, void *user
);
4346 /* Create a slice of the domain "set" such that integer division "div"
4347 * has the fixed value "v" and add the results to data->res,
4348 * replacing the integer division by "v" in "qp".
4350 static isl_stat
set_div(__isl_take isl_set
*set
,
4351 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4352 struct isl_split_periods_data
*data
)
4357 struct isl_upoly
*cst
;
4359 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4360 set
= isl_set_intersect(set
, slice
);
4365 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4367 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4368 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4370 isl_int_addmul(qp
->div
->row
[i
][1],
4371 qp
->div
->row
[i
][2 + total
+ div
], v
);
4372 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4375 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4376 qp
= substitute_div(qp
, div
, cst
);
4378 return split_periods(set
, qp
, data
);
4381 isl_qpolynomial_free(qp
);
4382 return isl_stat_error
;
4385 /* Split the domain "set" such that integer division "div"
4386 * has a fixed value (ranging from "min" to "max") on each slice
4387 * and add the results to data->res.
4389 static isl_stat
split_div(__isl_take isl_set
*set
,
4390 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4391 struct isl_split_periods_data
*data
)
4393 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4394 isl_set
*set_i
= isl_set_copy(set
);
4395 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4397 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4401 isl_qpolynomial_free(qp
);
4405 isl_qpolynomial_free(qp
);
4406 return isl_stat_error
;
4409 /* If "qp" refers to any integer division
4410 * that can only attain "max_periods" distinct values on "set"
4411 * then split the domain along those distinct values.
4412 * Add the results (or the original if no splitting occurs)
4415 static isl_stat
split_periods(__isl_take isl_set
*set
,
4416 __isl_take isl_qpolynomial
*qp
, void *user
)
4419 isl_pw_qpolynomial
*pwqp
;
4420 struct isl_split_periods_data
*data
;
4423 isl_stat r
= isl_stat_ok
;
4425 data
= (struct isl_split_periods_data
*)user
;
4430 if (qp
->div
->n_row
== 0) {
4431 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4432 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4438 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4439 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4440 enum isl_lp_result lp_res
;
4442 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4443 qp
->div
->n_row
) != -1)
4446 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4447 set
->ctx
->one
, &min
, NULL
, NULL
);
4448 if (lp_res
== isl_lp_error
)
4450 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4452 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4454 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4455 set
->ctx
->one
, &max
, NULL
, NULL
);
4456 if (lp_res
== isl_lp_error
)
4458 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4460 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4462 isl_int_sub(max
, max
, min
);
4463 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4464 isl_int_add(max
, max
, min
);
4469 if (i
< qp
->div
->n_row
) {
4470 r
= split_div(set
, qp
, i
, min
, max
, data
);
4472 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4473 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4485 isl_qpolynomial_free(qp
);
4486 return isl_stat_error
;
4489 /* If any quasi-polynomial in pwqp refers to any integer division
4490 * that can only attain "max_periods" distinct values on its domain
4491 * then split the domain along those distinct values.
4493 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4494 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4496 struct isl_split_periods_data data
;
4498 data
.max_periods
= max_periods
;
4499 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4501 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4504 isl_pw_qpolynomial_free(pwqp
);
4508 isl_pw_qpolynomial_free(data
.res
);
4509 isl_pw_qpolynomial_free(pwqp
);
4513 /* Construct a piecewise quasipolynomial that is constant on the given
4514 * domain. In particular, it is
4517 * infinity if cst == -1
4519 * If cst == -1, then explicitly check whether the domain is empty and,
4520 * if so, return 0 instead.
4522 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4523 __isl_take isl_basic_set
*bset
, int cst
)
4526 isl_qpolynomial
*qp
;
4528 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4533 bset
= isl_basic_set_params(bset
);
4534 dim
= isl_basic_set_get_space(bset
);
4536 qp
= isl_qpolynomial_infty_on_domain(dim
);
4538 qp
= isl_qpolynomial_zero_on_domain(dim
);
4540 qp
= isl_qpolynomial_one_on_domain(dim
);
4541 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4544 /* Factor bset, call fn on each of the factors and return the product.
4546 * If no factors can be found, simply call fn on the input.
4547 * Otherwise, construct the factors based on the factorizer,
4548 * call fn on each factor and compute the product.
4550 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4551 __isl_take isl_basic_set
*bset
,
4552 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4558 isl_qpolynomial
*qp
;
4559 isl_pw_qpolynomial
*pwqp
;
4563 f
= isl_basic_set_factorizer(bset
);
4566 if (f
->n_group
== 0) {
4567 isl_factorizer_free(f
);
4571 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4572 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4574 space
= isl_basic_set_get_space(bset
);
4575 space
= isl_space_params(space
);
4576 set
= isl_set_universe(isl_space_copy(space
));
4577 qp
= isl_qpolynomial_one_on_domain(space
);
4578 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4580 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4582 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4583 isl_basic_set
*bset_i
;
4584 isl_pw_qpolynomial
*pwqp_i
;
4586 bset_i
= isl_basic_set_copy(bset
);
4587 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4588 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4589 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4591 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4592 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4593 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4595 pwqp_i
= fn(bset_i
);
4596 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4601 isl_basic_set_free(bset
);
4602 isl_factorizer_free(f
);
4606 isl_basic_set_free(bset
);
4610 /* Factor bset, call fn on each of the factors and return the product.
4611 * The function is assumed to evaluate to zero on empty domains,
4612 * to one on zero-dimensional domains and to infinity on unbounded domains
4613 * and will not be called explicitly on zero-dimensional or unbounded domains.
4615 * We first check for some special cases and remove all equalities.
4616 * Then we hand over control to compressed_multiplicative_call.
4618 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4619 __isl_take isl_basic_set
*bset
,
4620 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4624 isl_pw_qpolynomial
*pwqp
;
4629 if (isl_basic_set_plain_is_empty(bset
))
4630 return constant_on_domain(bset
, 0);
4632 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4633 return constant_on_domain(bset
, 1);
4635 bounded
= isl_basic_set_is_bounded(bset
);
4639 return constant_on_domain(bset
, -1);
4641 if (bset
->n_eq
== 0)
4642 return compressed_multiplicative_call(bset
, fn
);
4644 morph
= isl_basic_set_full_compression(bset
);
4645 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4647 pwqp
= compressed_multiplicative_call(bset
, fn
);
4649 morph
= isl_morph_dom_params(morph
);
4650 morph
= isl_morph_ran_params(morph
);
4651 morph
= isl_morph_inverse(morph
);
4653 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4657 isl_basic_set_free(bset
);
4661 /* Drop all floors in "qp", turning each integer division [a/m] into
4662 * a rational division a/m. If "down" is set, then the integer division
4663 * is replaced by (a-(m-1))/m instead.
4665 static __isl_give isl_qpolynomial
*qp_drop_floors(
4666 __isl_take isl_qpolynomial
*qp
, int down
)
4669 struct isl_upoly
*s
;
4673 if (qp
->div
->n_row
== 0)
4676 qp
= isl_qpolynomial_cow(qp
);
4680 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4682 isl_int_sub(qp
->div
->row
[i
][1],
4683 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4684 isl_int_add_ui(qp
->div
->row
[i
][1],
4685 qp
->div
->row
[i
][1], 1);
4687 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4688 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4689 qp
= substitute_div(qp
, i
, s
);
4697 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4698 * a rational division a/m.
4700 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4701 __isl_take isl_pw_qpolynomial
*pwqp
)
4708 if (isl_pw_qpolynomial_is_zero(pwqp
))
4711 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4715 for (i
= 0; i
< pwqp
->n
; ++i
) {
4716 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4723 isl_pw_qpolynomial_free(pwqp
);
4727 /* Adjust all the integer divisions in "qp" such that they are at least
4728 * one over the given orthant (identified by "signs"). This ensures
4729 * that they will still be non-negative even after subtracting (m-1)/m.
4731 * In particular, f is replaced by f' + v, changing f = [a/m]
4732 * to f' = [(a - m v)/m].
4733 * If the constant term k in a is smaller than m,
4734 * the constant term of v is set to floor(k/m) - 1.
4735 * For any other term, if the coefficient c and the variable x have
4736 * the same sign, then no changes are needed.
4737 * Otherwise, if the variable is positive (and c is negative),
4738 * then the coefficient of x in v is set to floor(c/m).
4739 * If the variable is negative (and c is positive),
4740 * then the coefficient of x in v is set to ceil(c/m).
4742 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4748 struct isl_upoly
*s
;
4750 qp
= isl_qpolynomial_cow(qp
);
4753 qp
->div
= isl_mat_cow(qp
->div
);
4757 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4758 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4760 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4761 isl_int
*row
= qp
->div
->row
[i
];
4765 if (isl_int_lt(row
[1], row
[0])) {
4766 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4767 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4768 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4770 for (j
= 0; j
< total
; ++j
) {
4771 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4774 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4776 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4777 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4779 for (j
= 0; j
< i
; ++j
) {
4780 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4782 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4783 row
[2 + total
+ j
], row
[0]);
4784 isl_int_submul(row
[2 + total
+ j
],
4785 row
[0], v
->el
[1 + total
+ j
]);
4787 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4788 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4790 isl_seq_combine(qp
->div
->row
[j
] + 1,
4791 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4792 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4794 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4795 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4796 qp
->div
->ctx
->one
, v
->size
);
4797 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4807 isl_qpolynomial_free(qp
);
4811 struct isl_to_poly_data
{
4813 isl_pw_qpolynomial
*res
;
4814 isl_qpolynomial
*qp
;
4817 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4818 * We first make all integer divisions positive and then split the
4819 * quasipolynomials into terms with sign data->sign (the direction
4820 * of the requested approximation) and terms with the opposite sign.
4821 * In the first set of terms, each integer division [a/m] is
4822 * overapproximated by a/m, while in the second it is underapproximated
4825 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4826 int *signs
, void *user
)
4828 struct isl_to_poly_data
*data
= user
;
4829 isl_pw_qpolynomial
*t
;
4830 isl_qpolynomial
*qp
, *up
, *down
;
4832 qp
= isl_qpolynomial_copy(data
->qp
);
4833 qp
= make_divs_pos(qp
, signs
);
4835 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4836 up
= qp_drop_floors(up
, 0);
4837 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4838 down
= qp_drop_floors(down
, 1);
4840 isl_qpolynomial_free(qp
);
4841 qp
= isl_qpolynomial_add(up
, down
);
4843 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4844 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4849 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4850 * the polynomial will be an overapproximation. If "sign" is negative,
4851 * it will be an underapproximation. If "sign" is zero, the approximation
4852 * will lie somewhere in between.
4854 * In particular, is sign == 0, we simply drop the floors, turning
4855 * the integer divisions into rational divisions.
4856 * Otherwise, we split the domains into orthants, make all integer divisions
4857 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4858 * depending on the requested sign and the sign of the term in which
4859 * the integer division appears.
4861 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4862 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4865 struct isl_to_poly_data data
;
4868 return pwqp_drop_floors(pwqp
);
4874 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4876 for (i
= 0; i
< pwqp
->n
; ++i
) {
4877 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4878 isl_pw_qpolynomial
*t
;
4879 t
= isl_pw_qpolynomial_alloc(
4880 isl_set_copy(pwqp
->p
[i
].set
),
4881 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4882 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4885 data
.qp
= pwqp
->p
[i
].qp
;
4886 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4887 &to_polynomial_on_orthant
, &data
) < 0)
4891 isl_pw_qpolynomial_free(pwqp
);
4895 isl_pw_qpolynomial_free(pwqp
);
4896 isl_pw_qpolynomial_free(data
.res
);
4900 static __isl_give isl_pw_qpolynomial
*poly_entry(
4901 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4905 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4908 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4909 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4911 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4912 &poly_entry
, &sign
);
4915 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4916 __isl_take isl_qpolynomial
*qp
)
4920 isl_vec
*aff
= NULL
;
4921 isl_basic_map
*bmap
= NULL
;
4927 if (!isl_upoly_is_affine(qp
->upoly
))
4928 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4929 "input quasi-polynomial not affine", goto error
);
4930 aff
= isl_qpolynomial_extract_affine(qp
);
4933 dim
= isl_qpolynomial_get_space(qp
);
4934 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4935 n_div
= qp
->div
->n_row
;
4936 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4938 for (i
= 0; i
< n_div
; ++i
) {
4939 k
= isl_basic_map_alloc_div(bmap
);
4942 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4943 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4944 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4947 k
= isl_basic_map_alloc_equality(bmap
);
4950 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4951 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4952 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4955 isl_qpolynomial_free(qp
);
4956 bmap
= isl_basic_map_finalize(bmap
);
4960 isl_qpolynomial_free(qp
);
4961 isl_basic_map_free(bmap
);