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
*space
,
1131 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1133 struct isl_qpolynomial
*qp
= NULL
;
1139 if (!isl_space_is_set(space
))
1140 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1141 "domain of polynomial should be a set", goto error
);
1143 total
= isl_space_dim(space
, isl_dim_all
);
1145 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1150 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1159 isl_space_free(space
);
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
*domain
)
1767 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_zero(domain
->ctx
));
1770 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1771 __isl_take isl_space
*domain
)
1775 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_one(domain
->ctx
));
1778 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1779 __isl_take isl_space
*domain
)
1783 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_infty(domain
->ctx
));
1786 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1787 __isl_take isl_space
*domain
)
1791 return isl_qpolynomial_alloc(domain
, 0,
1792 isl_upoly_neginfty(domain
->ctx
));
1795 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1796 __isl_take isl_space
*domain
)
1800 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_nan(domain
->ctx
));
1803 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1804 __isl_take isl_space
*domain
,
1807 struct isl_qpolynomial
*qp
;
1808 struct isl_upoly_cst
*cst
;
1813 qp
= isl_qpolynomial_alloc(domain
, 0, isl_upoly_zero(domain
->ctx
));
1817 cst
= isl_upoly_as_cst(qp
->upoly
);
1818 isl_int_set(cst
->n
, v
);
1823 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1824 isl_int
*n
, isl_int
*d
)
1826 struct isl_upoly_cst
*cst
;
1831 if (!isl_upoly_is_cst(qp
->upoly
))
1834 cst
= isl_upoly_as_cst(qp
->upoly
);
1839 isl_int_set(*n
, cst
->n
);
1841 isl_int_set(*d
, cst
->d
);
1846 /* Return the constant term of "up".
1848 static __isl_give isl_val
*isl_upoly_get_constant_val(
1849 __isl_keep
struct isl_upoly
*up
)
1851 struct isl_upoly_cst
*cst
;
1856 while (!isl_upoly_is_cst(up
)) {
1857 struct isl_upoly_rec
*rec
;
1859 rec
= isl_upoly_as_rec(up
);
1865 cst
= isl_upoly_as_cst(up
);
1868 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1871 /* Return the constant term of "qp".
1873 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1874 __isl_keep isl_qpolynomial
*qp
)
1879 return isl_upoly_get_constant_val(qp
->upoly
);
1882 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1885 struct isl_upoly_rec
*rec
;
1893 rec
= isl_upoly_as_rec(up
);
1900 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1902 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1908 return isl_upoly_is_affine(rec
->p
[0]);
1911 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1916 if (qp
->div
->n_row
> 0)
1919 return isl_upoly_is_affine(qp
->upoly
);
1922 static void update_coeff(__isl_keep isl_vec
*aff
,
1923 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1928 if (isl_int_is_zero(cst
->n
))
1933 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1934 isl_int_divexact(f
, cst
->d
, gcd
);
1935 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1936 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1937 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1942 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1943 __isl_keep isl_vec
*aff
)
1945 struct isl_upoly_cst
*cst
;
1946 struct isl_upoly_rec
*rec
;
1952 struct isl_upoly_cst
*cst
;
1954 cst
= isl_upoly_as_cst(up
);
1957 update_coeff(aff
, cst
, 0);
1961 rec
= isl_upoly_as_rec(up
);
1964 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1966 cst
= isl_upoly_as_cst(rec
->p
[1]);
1969 update_coeff(aff
, cst
, 1 + up
->var
);
1971 return isl_upoly_update_affine(rec
->p
[0], aff
);
1974 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1975 __isl_keep isl_qpolynomial
*qp
)
1983 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1984 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1988 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1989 isl_int_set_si(aff
->el
[0], 1);
1991 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
2000 /* Compare two quasi-polynomials.
2002 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2003 * than "qp2" and 0 if they are equal.
2005 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2006 __isl_keep isl_qpolynomial
*qp2
)
2017 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2021 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2025 return isl_upoly_plain_cmp(qp1
->upoly
, qp2
->upoly
);
2028 /* Is "qp1" obviously equal to "qp2"?
2030 * NaN is not equal to anything, not even to another NaN.
2032 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2033 __isl_keep isl_qpolynomial
*qp2
)
2038 return isl_bool_error
;
2040 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2041 return isl_bool_false
;
2043 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2044 if (equal
< 0 || !equal
)
2047 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2048 if (equal
< 0 || !equal
)
2051 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
2054 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
2057 struct isl_upoly_rec
*rec
;
2059 if (isl_upoly_is_cst(up
)) {
2060 struct isl_upoly_cst
*cst
;
2061 cst
= isl_upoly_as_cst(up
);
2064 isl_int_lcm(*d
, *d
, cst
->d
);
2068 rec
= isl_upoly_as_rec(up
);
2072 for (i
= 0; i
< rec
->n
; ++i
)
2073 upoly_update_den(rec
->p
[i
], d
);
2076 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
2078 isl_int_set_si(*d
, 1);
2081 upoly_update_den(qp
->upoly
, d
);
2084 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2085 __isl_take isl_space
*domain
, int pos
, int power
)
2087 struct isl_ctx
*ctx
;
2094 return isl_qpolynomial_alloc(domain
, 0,
2095 isl_upoly_var_pow(ctx
, pos
, power
));
2098 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2099 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2104 isl_assert(domain
->ctx
,
2105 isl_space_dim(domain
, isl_dim_in
) == 0, goto error
);
2106 isl_assert(domain
->ctx
, pos
< isl_space_dim(domain
, type
), goto error
);
2108 if (type
== isl_dim_set
)
2109 pos
+= isl_space_dim(domain
, isl_dim_param
);
2111 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2113 isl_space_free(domain
);
2117 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2118 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2121 struct isl_upoly_rec
*rec
;
2122 struct isl_upoly
*base
, *res
;
2127 if (isl_upoly_is_cst(up
))
2130 if (up
->var
< first
)
2133 rec
= isl_upoly_as_rec(up
);
2137 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2139 if (up
->var
>= first
+ n
)
2140 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2142 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2144 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2145 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2146 struct isl_upoly
*t
;
2147 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2148 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2149 res
= isl_upoly_sum(res
, t
);
2152 isl_upoly_free(base
);
2161 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2162 isl_int denom
, unsigned len
)
2165 struct isl_upoly
*up
;
2167 isl_assert(ctx
, len
>= 1, return NULL
);
2169 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2170 for (i
= 0; i
< len
- 1; ++i
) {
2171 struct isl_upoly
*t
;
2172 struct isl_upoly
*c
;
2174 if (isl_int_is_zero(f
[1 + i
]))
2177 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2178 t
= isl_upoly_var_pow(ctx
, i
, 1);
2179 t
= isl_upoly_mul(c
, t
);
2180 up
= isl_upoly_sum(up
, t
);
2186 /* Remove common factor of non-constant terms and denominator.
2188 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2190 isl_ctx
*ctx
= qp
->div
->ctx
;
2191 unsigned total
= qp
->div
->n_col
- 2;
2193 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2194 isl_int_gcd(ctx
->normalize_gcd
,
2195 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2196 if (isl_int_is_one(ctx
->normalize_gcd
))
2199 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2200 ctx
->normalize_gcd
, total
);
2201 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2202 ctx
->normalize_gcd
);
2203 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2204 ctx
->normalize_gcd
);
2207 /* Replace the integer division identified by "div" by the polynomial "s".
2208 * The integer division is assumed not to appear in the definition
2209 * of any other integer divisions.
2211 static __isl_give isl_qpolynomial
*substitute_div(
2212 __isl_take isl_qpolynomial
*qp
,
2213 int div
, __isl_take
struct isl_upoly
*s
)
2222 qp
= isl_qpolynomial_cow(qp
);
2226 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2227 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2231 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2234 for (i
= 0; i
< total
+ div
; ++i
)
2236 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2237 reordering
[i
] = i
- 1;
2238 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2239 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2240 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2243 if (!qp
->upoly
|| !qp
->div
)
2249 isl_qpolynomial_free(qp
);
2254 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2255 * divisions because d is equal to 1 by their definition, i.e., e.
2257 static __isl_give isl_qpolynomial
*substitute_non_divs(
2258 __isl_take isl_qpolynomial
*qp
)
2262 struct isl_upoly
*s
;
2267 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2268 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2269 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2271 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2272 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2274 isl_seq_combine(qp
->div
->row
[j
] + 1,
2275 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2276 qp
->div
->row
[j
][2 + total
+ i
],
2277 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2278 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2279 normalize_div(qp
, j
);
2281 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2282 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2283 qp
= substitute_div(qp
, i
, s
);
2290 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2291 * with d the denominator. When replacing the coefficient e of x by
2292 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2293 * inside the division, so we need to add floor(e/d) * x outside.
2294 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2295 * to adjust the coefficient of x in each later div that depends on the
2296 * current div "div" and also in the affine expressions in the rows of "mat"
2297 * (if they too depend on "div").
2299 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2300 __isl_keep isl_mat
**mat
)
2304 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2307 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2308 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2309 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2311 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2312 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2313 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2314 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2315 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2316 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2318 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2319 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2325 /* Check if the last non-zero coefficient is bigger that half of the
2326 * denominator. If so, we will invert the div to further reduce the number
2327 * of distinct divs that may appear.
2328 * If the last non-zero coefficient is exactly half the denominator,
2329 * then we continue looking for earlier coefficients that are bigger
2330 * than half the denominator.
2332 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2337 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2338 if (isl_int_is_zero(div
->row
[row
][i
]))
2340 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2341 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2342 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2352 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2353 * We only invert the coefficients of e (and the coefficient of q in
2354 * later divs and in the rows of "mat"). After calling this function, the
2355 * coefficients of e should be reduced again.
2357 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2358 __isl_keep isl_mat
**mat
)
2360 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2362 isl_seq_neg(qp
->div
->row
[div
] + 1,
2363 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2364 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2365 isl_int_add(qp
->div
->row
[div
][1],
2366 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2367 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2368 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2369 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2372 /* Reduce all divs of "qp" to have coefficients
2373 * in the interval [0, d-1], with d the denominator and such that the
2374 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2375 * The modifications to the integer divisions need to be reflected
2376 * in the factors of the polynomial that refer to the original
2377 * integer divisions. To this end, the modifications are collected
2378 * as a set of affine expressions and then plugged into the polynomial.
2380 * After the reduction, some divs may have become redundant or identical,
2381 * so we call substitute_non_divs and sort_divs. If these functions
2382 * eliminate divs or merge two or more divs into one, the coefficients
2383 * of the enclosing divs may have to be reduced again, so we call
2384 * ourselves recursively if the number of divs decreases.
2386 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2391 struct isl_upoly
**s
;
2392 unsigned o_div
, n_div
, total
;
2397 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2398 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2399 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2400 ctx
= isl_qpolynomial_get_ctx(qp
);
2401 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2403 for (i
= 0; i
< n_div
; ++i
)
2404 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2406 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2407 normalize_div(qp
, i
);
2408 reduce_div(qp
, i
, &mat
);
2409 if (needs_invert(qp
->div
, i
)) {
2410 invert_div(qp
, i
, &mat
);
2411 reduce_div(qp
, i
, &mat
);
2417 s
= isl_alloc_array(ctx
, struct isl_upoly
*, n_div
);
2420 for (i
= 0; i
< n_div
; ++i
)
2421 s
[i
] = isl_upoly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2423 qp
->upoly
= isl_upoly_subs(qp
->upoly
, o_div
- 1, n_div
, s
);
2424 for (i
= 0; i
< n_div
; ++i
)
2425 isl_upoly_free(s
[i
]);
2432 qp
= substitute_non_divs(qp
);
2434 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2435 return reduce_divs(qp
);
2439 isl_qpolynomial_free(qp
);
2444 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2445 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2447 struct isl_qpolynomial
*qp
;
2448 struct isl_upoly_cst
*cst
;
2453 qp
= isl_qpolynomial_alloc(domain
, 0, isl_upoly_zero(domain
->ctx
));
2457 cst
= isl_upoly_as_cst(qp
->upoly
);
2458 isl_int_set(cst
->n
, n
);
2459 isl_int_set(cst
->d
, d
);
2464 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2466 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2467 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2469 isl_qpolynomial
*qp
;
2470 struct isl_upoly_cst
*cst
;
2472 if (!domain
|| !val
)
2475 qp
= isl_qpolynomial_alloc(isl_space_copy(domain
), 0,
2476 isl_upoly_zero(domain
->ctx
));
2480 cst
= isl_upoly_as_cst(qp
->upoly
);
2481 isl_int_set(cst
->n
, val
->n
);
2482 isl_int_set(cst
->d
, val
->d
);
2484 isl_space_free(domain
);
2488 isl_space_free(domain
);
2493 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2495 struct isl_upoly_rec
*rec
;
2501 if (isl_upoly_is_cst(up
))
2505 active
[up
->var
] = 1;
2507 rec
= isl_upoly_as_rec(up
);
2508 for (i
= 0; i
< rec
->n
; ++i
)
2509 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2515 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2518 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2523 for (i
= 0; i
< d
; ++i
)
2524 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2525 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2531 return up_set_active(qp
->upoly
, active
, d
);
2534 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2535 enum isl_dim_type type
, unsigned first
, unsigned n
)
2539 isl_bool involves
= isl_bool_false
;
2542 return isl_bool_error
;
2544 return isl_bool_false
;
2546 isl_assert(qp
->dim
->ctx
,
2547 first
+ n
<= isl_qpolynomial_dim(qp
, type
),
2548 return isl_bool_error
);
2549 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2550 type
== isl_dim_in
, return isl_bool_error
);
2552 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2553 isl_space_dim(qp
->dim
, isl_dim_all
));
2554 if (set_active(qp
, active
) < 0)
2557 if (type
== isl_dim_in
)
2558 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2559 for (i
= 0; i
< n
; ++i
)
2560 if (active
[first
+ i
]) {
2561 involves
= isl_bool_true
;
2570 return isl_bool_error
;
2573 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2574 * of the divs that do appear in the quasi-polynomial.
2576 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2577 __isl_take isl_qpolynomial
*qp
)
2584 int *reordering
= NULL
;
2591 if (qp
->div
->n_row
== 0)
2594 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2595 len
= qp
->div
->n_col
- 2;
2596 ctx
= isl_qpolynomial_get_ctx(qp
);
2597 active
= isl_calloc_array(ctx
, int, len
);
2601 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2604 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2605 if (!active
[d
+ i
]) {
2609 for (j
= 0; j
< i
; ++j
) {
2610 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2622 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2626 for (i
= 0; i
< d
; ++i
)
2630 n_div
= qp
->div
->n_row
;
2631 for (i
= 0; i
< n_div
; ++i
) {
2632 if (!active
[d
+ i
]) {
2633 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2634 qp
->div
= isl_mat_drop_cols(qp
->div
,
2635 2 + d
+ i
- skip
, 1);
2638 reordering
[d
+ i
] = d
+ i
- skip
;
2641 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2643 if (!qp
->upoly
|| !qp
->div
)
2653 isl_qpolynomial_free(qp
);
2657 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2658 unsigned first
, unsigned n
)
2661 struct isl_upoly_rec
*rec
;
2665 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2667 if (up
->var
< first
+ n
) {
2668 up
= replace_by_constant_term(up
);
2669 return isl_upoly_drop(up
, first
, n
);
2671 up
= isl_upoly_cow(up
);
2675 rec
= isl_upoly_as_rec(up
);
2679 for (i
= 0; i
< rec
->n
; ++i
) {
2680 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2691 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2692 __isl_take isl_qpolynomial
*qp
,
2693 enum isl_dim_type type
, unsigned pos
, const char *s
)
2695 qp
= isl_qpolynomial_cow(qp
);
2698 if (type
== isl_dim_out
)
2699 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2700 "cannot set name of output/set dimension",
2701 return isl_qpolynomial_free(qp
));
2702 if (type
== isl_dim_in
)
2704 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2709 isl_qpolynomial_free(qp
);
2713 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2714 __isl_take isl_qpolynomial
*qp
,
2715 enum isl_dim_type type
, unsigned first
, unsigned n
)
2719 if (type
== isl_dim_out
)
2720 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2721 "cannot drop output/set dimension",
2723 if (type
== isl_dim_in
)
2725 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2728 qp
= isl_qpolynomial_cow(qp
);
2732 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2734 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2735 type
== isl_dim_set
, goto error
);
2737 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2741 if (type
== isl_dim_set
)
2742 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2744 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2748 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2754 isl_qpolynomial_free(qp
);
2758 /* Project the domain of the quasi-polynomial onto its parameter space.
2759 * The quasi-polynomial may not involve any of the domain dimensions.
2761 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2762 __isl_take isl_qpolynomial
*qp
)
2768 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2769 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2771 return isl_qpolynomial_free(qp
);
2773 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2774 "polynomial involves some of the domain dimensions",
2775 return isl_qpolynomial_free(qp
));
2776 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2777 space
= isl_qpolynomial_get_domain_space(qp
);
2778 space
= isl_space_params(space
);
2779 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2783 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2784 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2790 struct isl_upoly
*up
;
2794 if (eq
->n_eq
== 0) {
2795 isl_basic_set_free(eq
);
2799 qp
= isl_qpolynomial_cow(qp
);
2802 qp
->div
= isl_mat_cow(qp
->div
);
2806 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2808 isl_int_init(denom
);
2809 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2810 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2811 if (j
< 0 || j
== 0 || j
>= total
)
2814 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2815 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2817 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2818 &qp
->div
->row
[k
][0]);
2819 normalize_div(qp
, k
);
2822 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2823 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2824 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2825 isl_int_set_si(eq
->eq
[i
][j
], 0);
2827 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2828 eq
->eq
[i
], denom
, total
);
2829 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2832 isl_int_clear(denom
);
2837 isl_basic_set_free(eq
);
2839 qp
= substitute_non_divs(qp
);
2844 isl_basic_set_free(eq
);
2845 isl_qpolynomial_free(qp
);
2849 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2851 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2852 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2856 if (qp
->div
->n_row
> 0)
2857 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2858 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2860 isl_basic_set_free(eq
);
2861 isl_qpolynomial_free(qp
);
2865 static __isl_give isl_basic_set
*add_div_constraints(
2866 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2874 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2877 total
= isl_basic_set_total_dim(bset
);
2878 for (i
= 0; i
< div
->n_row
; ++i
)
2879 if (isl_basic_set_add_div_constraints_var(bset
,
2880 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2887 isl_basic_set_free(bset
);
2891 /* Look for equalities among the variables shared by context and qp
2892 * and the integer divisions of qp, if any.
2893 * The equalities are then used to eliminate variables and/or integer
2894 * divisions from qp.
2896 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2897 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2903 if (qp
->div
->n_row
> 0) {
2904 isl_basic_set
*bset
;
2905 context
= isl_set_add_dims(context
, isl_dim_set
,
2907 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2908 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2909 context
= isl_set_intersect(context
,
2910 isl_set_from_basic_set(bset
));
2913 aff
= isl_set_affine_hull(context
);
2914 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2916 isl_qpolynomial_free(qp
);
2917 isl_set_free(context
);
2921 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2922 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2924 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2925 isl_set
*dom_context
= isl_set_universe(space
);
2926 dom_context
= isl_set_intersect_params(dom_context
, context
);
2927 return isl_qpolynomial_gist(qp
, dom_context
);
2930 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2931 __isl_take isl_qpolynomial
*qp
)
2937 if (isl_qpolynomial_is_zero(qp
)) {
2938 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2939 isl_qpolynomial_free(qp
);
2940 return isl_pw_qpolynomial_zero(dim
);
2943 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2944 return isl_pw_qpolynomial_alloc(dom
, qp
);
2947 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2950 #define PW isl_pw_qpolynomial
2952 #define EL isl_qpolynomial
2954 #define EL_IS_ZERO is_zero
2958 #define IS_ZERO is_zero
2961 #undef DEFAULT_IS_ZERO
2962 #define DEFAULT_IS_ZERO 1
2966 #include <isl_pw_templ.c>
2967 #include <isl_pw_eval.c>
2970 #define BASE pw_qpolynomial
2972 #include <isl_union_single.c>
2973 #include <isl_union_eval.c>
2974 #include <isl_union_neg.c>
2976 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2984 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2987 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2990 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2991 __isl_take isl_pw_qpolynomial
*pwqp1
,
2992 __isl_take isl_pw_qpolynomial
*pwqp2
)
2994 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2997 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2998 __isl_take isl_pw_qpolynomial
*pwqp1
,
2999 __isl_take isl_pw_qpolynomial
*pwqp2
)
3002 struct isl_pw_qpolynomial
*res
;
3004 if (!pwqp1
|| !pwqp2
)
3007 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3010 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3011 isl_pw_qpolynomial_free(pwqp2
);
3015 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3016 isl_pw_qpolynomial_free(pwqp1
);
3020 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3021 isl_pw_qpolynomial_free(pwqp1
);
3025 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3026 isl_pw_qpolynomial_free(pwqp2
);
3030 n
= pwqp1
->n
* pwqp2
->n
;
3031 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3033 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3034 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3035 struct isl_set
*common
;
3036 struct isl_qpolynomial
*prod
;
3037 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3038 isl_set_copy(pwqp2
->p
[j
].set
));
3039 if (isl_set_plain_is_empty(common
)) {
3040 isl_set_free(common
);
3044 prod
= isl_qpolynomial_mul(
3045 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3046 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3048 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3052 isl_pw_qpolynomial_free(pwqp1
);
3053 isl_pw_qpolynomial_free(pwqp2
);
3057 isl_pw_qpolynomial_free(pwqp1
);
3058 isl_pw_qpolynomial_free(pwqp2
);
3062 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
3063 __isl_take isl_vec
*vec
)
3066 struct isl_upoly_rec
*rec
;
3070 if (isl_upoly_is_cst(up
)) {
3072 res
= isl_upoly_get_constant_val(up
);
3077 rec
= isl_upoly_as_rec(up
);
3081 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
3083 base
= isl_val_rat_from_isl_int(up
->ctx
,
3084 vec
->el
[1 + up
->var
], vec
->el
[0]);
3086 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
3089 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3090 res
= isl_val_mul(res
, isl_val_copy(base
));
3091 res
= isl_val_add(res
,
3092 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
3093 isl_vec_copy(vec
)));
3106 /* Evaluate "qp" in the void point "pnt".
3107 * In particular, return the value NaN.
3109 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3110 __isl_take isl_point
*pnt
)
3114 ctx
= isl_point_get_ctx(pnt
);
3115 isl_qpolynomial_free(qp
);
3116 isl_point_free(pnt
);
3117 return isl_val_nan(ctx
);
3120 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3121 __isl_take isl_point
*pnt
)
3129 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3130 is_void
= isl_point_is_void(pnt
);
3134 return eval_void(qp
, pnt
);
3136 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3138 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
3140 isl_qpolynomial_free(qp
);
3141 isl_point_free(pnt
);
3145 isl_qpolynomial_free(qp
);
3146 isl_point_free(pnt
);
3150 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
3151 __isl_keep
struct isl_upoly_cst
*cst2
)
3156 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3157 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3158 cmp
= isl_int_sgn(t
);
3163 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3164 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3165 unsigned first
, unsigned n
)
3173 if (type
== isl_dim_out
)
3174 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3175 "cannot insert output/set dimensions",
3177 if (type
== isl_dim_in
)
3179 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3182 qp
= isl_qpolynomial_cow(qp
);
3186 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
3189 g_pos
= pos(qp
->dim
, type
) + first
;
3191 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3195 total
= qp
->div
->n_col
- 2;
3196 if (total
> g_pos
) {
3198 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3201 for (i
= 0; i
< total
- g_pos
; ++i
)
3203 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3209 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3215 isl_qpolynomial_free(qp
);
3219 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3220 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3224 pos
= isl_qpolynomial_dim(qp
, type
);
3226 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3229 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3230 __isl_take isl_pw_qpolynomial
*pwqp
,
3231 enum isl_dim_type type
, unsigned n
)
3235 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3237 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3240 static int *reordering_move(isl_ctx
*ctx
,
3241 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3246 reordering
= isl_alloc_array(ctx
, int, len
);
3251 for (i
= 0; i
< dst
; ++i
)
3253 for (i
= 0; i
< n
; ++i
)
3254 reordering
[src
+ i
] = dst
+ i
;
3255 for (i
= 0; i
< src
- dst
; ++i
)
3256 reordering
[dst
+ i
] = dst
+ n
+ i
;
3257 for (i
= 0; i
< len
- src
- n
; ++i
)
3258 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3260 for (i
= 0; i
< src
; ++i
)
3262 for (i
= 0; i
< n
; ++i
)
3263 reordering
[src
+ i
] = dst
+ i
;
3264 for (i
= 0; i
< dst
- src
; ++i
)
3265 reordering
[src
+ n
+ i
] = src
+ i
;
3266 for (i
= 0; i
< len
- dst
- n
; ++i
)
3267 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3273 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3274 __isl_take isl_qpolynomial
*qp
,
3275 enum isl_dim_type dst_type
, unsigned dst_pos
,
3276 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3285 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3286 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3287 "cannot move output/set dimension",
3289 if (dst_type
== isl_dim_in
)
3290 dst_type
= isl_dim_set
;
3291 if (src_type
== isl_dim_in
)
3292 src_type
= isl_dim_set
;
3295 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3296 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3299 qp
= isl_qpolynomial_cow(qp
);
3303 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3306 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3307 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3308 if (dst_type
> src_type
)
3311 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3318 reordering
= reordering_move(qp
->dim
->ctx
,
3319 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3323 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3328 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3334 isl_qpolynomial_free(qp
);
3338 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3339 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3341 struct isl_upoly
*up
;
3343 space
= isl_space_domain(space
);
3347 up
= isl_upoly_from_affine(space
->ctx
, f
, denom
,
3348 1 + isl_space_dim(space
, isl_dim_all
));
3350 return isl_qpolynomial_alloc(space
, 0, up
);
3353 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3356 struct isl_upoly
*up
;
3357 isl_qpolynomial
*qp
;
3362 ctx
= isl_aff_get_ctx(aff
);
3363 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3366 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3367 aff
->ls
->div
->n_row
, up
);
3371 isl_mat_free(qp
->div
);
3372 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3373 qp
->div
= isl_mat_cow(qp
->div
);
3378 qp
= reduce_divs(qp
);
3379 qp
= remove_redundant_divs(qp
);
3383 return isl_qpolynomial_free(qp
);
3386 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3387 __isl_take isl_pw_aff
*pwaff
)
3390 isl_pw_qpolynomial
*pwqp
;
3395 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3398 for (i
= 0; i
< pwaff
->n
; ++i
) {
3400 isl_qpolynomial
*qp
;
3402 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3403 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3404 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3407 isl_pw_aff_free(pwaff
);
3411 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3412 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3416 aff
= isl_constraint_get_bound(c
, type
, pos
);
3417 isl_constraint_free(c
);
3418 return isl_qpolynomial_from_aff(aff
);
3421 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3422 * in "qp" by subs[i].
3424 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3425 __isl_take isl_qpolynomial
*qp
,
3426 enum isl_dim_type type
, unsigned first
, unsigned n
,
3427 __isl_keep isl_qpolynomial
**subs
)
3430 struct isl_upoly
**ups
;
3435 qp
= isl_qpolynomial_cow(qp
);
3439 if (type
== isl_dim_out
)
3440 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3441 "cannot substitute output/set dimension",
3443 if (type
== isl_dim_in
)
3446 for (i
= 0; i
< n
; ++i
)
3450 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3453 for (i
= 0; i
< n
; ++i
)
3454 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3457 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3458 for (i
= 0; i
< n
; ++i
)
3459 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3461 first
+= pos(qp
->dim
, type
);
3463 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3466 for (i
= 0; i
< n
; ++i
)
3467 ups
[i
] = subs
[i
]->upoly
;
3469 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3478 isl_qpolynomial_free(qp
);
3482 /* Extend "bset" with extra set dimensions for each integer division
3483 * in "qp" and then call "fn" with the extended bset and the polynomial
3484 * that results from replacing each of the integer divisions by the
3485 * corresponding extra set dimension.
3487 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3488 __isl_keep isl_basic_set
*bset
,
3489 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3490 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3494 isl_qpolynomial
*poly
;
3497 return isl_stat_error
;
3498 if (qp
->div
->n_row
== 0)
3499 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3502 div
= isl_mat_copy(qp
->div
);
3503 dim
= isl_space_copy(qp
->dim
);
3504 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3505 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3506 bset
= isl_basic_set_copy(bset
);
3507 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, qp
->div
->n_row
);
3508 bset
= add_div_constraints(bset
, div
);
3510 return fn(bset
, poly
, user
);
3513 /* Return total degree in variables first (inclusive) up to last (exclusive).
3515 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3519 struct isl_upoly_rec
*rec
;
3523 if (isl_upoly_is_zero(up
))
3525 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3528 rec
= isl_upoly_as_rec(up
);
3532 for (i
= 0; i
< rec
->n
; ++i
) {
3535 if (isl_upoly_is_zero(rec
->p
[i
]))
3537 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3547 /* Return total degree in set variables.
3549 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3557 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3558 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3559 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3562 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3563 unsigned pos
, int deg
)
3566 struct isl_upoly_rec
*rec
;
3571 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3573 return isl_upoly_copy(up
);
3575 return isl_upoly_zero(up
->ctx
);
3578 rec
= isl_upoly_as_rec(up
);
3582 if (up
->var
== pos
) {
3584 return isl_upoly_copy(rec
->p
[deg
]);
3586 return isl_upoly_zero(up
->ctx
);
3589 up
= isl_upoly_copy(up
);
3590 up
= isl_upoly_cow(up
);
3591 rec
= isl_upoly_as_rec(up
);
3595 for (i
= 0; i
< rec
->n
; ++i
) {
3596 struct isl_upoly
*t
;
3597 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3600 isl_upoly_free(rec
->p
[i
]);
3610 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3612 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3613 __isl_keep isl_qpolynomial
*qp
,
3614 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3617 struct isl_upoly
*up
;
3623 if (type
== isl_dim_out
)
3624 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3625 "output/set dimension does not have a coefficient",
3627 if (type
== isl_dim_in
)
3630 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3633 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3634 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3636 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3639 isl_mat_free(c
->div
);
3640 c
->div
= isl_mat_copy(qp
->div
);
3645 isl_qpolynomial_free(c
);
3649 /* Homogenize the polynomial in the variables first (inclusive) up to
3650 * last (exclusive) by inserting powers of variable first.
3651 * Variable first is assumed not to appear in the input.
3653 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3654 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3655 int first
, int last
)
3658 struct isl_upoly_rec
*rec
;
3662 if (isl_upoly_is_zero(up
))
3666 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3667 struct isl_upoly
*hom
;
3669 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3672 rec
= isl_upoly_as_rec(hom
);
3673 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3678 up
= isl_upoly_cow(up
);
3679 rec
= isl_upoly_as_rec(up
);
3683 for (i
= 0; i
< rec
->n
; ++i
) {
3684 if (isl_upoly_is_zero(rec
->p
[i
]))
3686 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3687 up
->var
< last
? deg
+ i
: i
, target
,
3699 /* Homogenize the polynomial in the set variables by introducing
3700 * powers of an extra set variable at position 0.
3702 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3703 __isl_take isl_qpolynomial
*poly
)
3707 int deg
= isl_qpolynomial_degree(poly
);
3712 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3713 poly
= isl_qpolynomial_cow(poly
);
3717 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3718 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3719 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3726 isl_qpolynomial_free(poly
);
3730 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3731 __isl_take isl_mat
*div
)
3739 n
= isl_space_dim(space
, isl_dim_all
) + div
->n_row
;
3741 term
= isl_calloc(space
->ctx
, struct isl_term
,
3742 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3749 isl_int_init(term
->n
);
3750 isl_int_init(term
->d
);
3754 isl_space_free(space
);
3759 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3768 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3777 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3779 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3783 isl_int_set(dup
->n
, term
->n
);
3784 isl_int_set(dup
->d
, term
->d
);
3786 for (i
= 0; i
< total
; ++i
)
3787 dup
->pow
[i
] = term
->pow
[i
];
3792 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3800 return isl_term_dup(term
);
3803 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3808 if (--term
->ref
> 0)
3811 isl_space_free(term
->dim
);
3812 isl_mat_free(term
->div
);
3813 isl_int_clear(term
->n
);
3814 isl_int_clear(term
->d
);
3820 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3828 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3829 case isl_dim_div
: return term
->div
->n_row
;
3830 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3836 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3838 return term
? term
->dim
->ctx
: NULL
;
3841 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3845 isl_int_set(*n
, term
->n
);
3848 /* Return the coefficient of the term "term".
3850 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3855 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3859 int isl_term_get_exp(__isl_keep isl_term
*term
,
3860 enum isl_dim_type type
, unsigned pos
)
3865 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3867 if (type
>= isl_dim_set
)
3868 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3869 if (type
>= isl_dim_div
)
3870 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3872 return term
->pow
[pos
];
3875 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3877 isl_local_space
*ls
;
3883 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3886 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3887 isl_mat_copy(term
->div
));
3888 aff
= isl_aff_alloc(ls
);
3892 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3894 aff
= isl_aff_normalize(aff
);
3899 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3900 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3901 __isl_take isl_term
*term
, void *user
)
3904 struct isl_upoly_rec
*rec
;
3909 if (isl_upoly_is_zero(up
))
3912 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3913 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3914 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3916 if (isl_upoly_is_cst(up
)) {
3917 struct isl_upoly_cst
*cst
;
3918 cst
= isl_upoly_as_cst(up
);
3921 term
= isl_term_cow(term
);
3924 isl_int_set(term
->n
, cst
->n
);
3925 isl_int_set(term
->d
, cst
->d
);
3926 if (fn(isl_term_copy(term
), user
) < 0)
3931 rec
= isl_upoly_as_rec(up
);
3935 for (i
= 0; i
< rec
->n
; ++i
) {
3936 term
= isl_term_cow(term
);
3939 term
->pow
[up
->var
] = i
;
3940 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3944 term
->pow
[up
->var
] = 0;
3948 isl_term_free(term
);
3952 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3953 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3958 return isl_stat_error
;
3960 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3962 return isl_stat_error
;
3964 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3966 isl_term_free(term
);
3968 return term
? isl_stat_ok
: isl_stat_error
;
3971 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3973 struct isl_upoly
*up
;
3974 isl_qpolynomial
*qp
;
3980 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3982 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3983 for (i
= 0; i
< n
; ++i
) {
3986 up
= isl_upoly_mul(up
,
3987 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3990 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
3993 isl_mat_free(qp
->div
);
3994 qp
->div
= isl_mat_copy(term
->div
);
3998 isl_term_free(term
);
4001 isl_qpolynomial_free(qp
);
4002 isl_term_free(term
);
4006 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4007 __isl_take isl_space
*space
)
4016 if (isl_space_is_equal(qp
->dim
, space
)) {
4017 isl_space_free(space
);
4021 qp
= isl_qpolynomial_cow(qp
);
4025 extra
= isl_space_dim(space
, isl_dim_set
) -
4026 isl_space_dim(qp
->dim
, isl_dim_set
);
4027 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4028 if (qp
->div
->n_row
) {
4031 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4034 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4036 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
4041 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4044 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4045 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4047 isl_space_free(qp
->dim
);
4052 isl_space_free(space
);
4053 isl_qpolynomial_free(qp
);
4057 /* For each parameter or variable that does not appear in qp,
4058 * first eliminate the variable from all constraints and then set it to zero.
4060 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4061 __isl_keep isl_qpolynomial
*qp
)
4072 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4073 active
= isl_calloc_array(set
->ctx
, int, d
);
4074 if (set_active(qp
, active
) < 0)
4077 for (i
= 0; i
< d
; ++i
)
4086 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4087 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4088 for (i
= 0; i
< nparam
; ++i
) {
4091 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4092 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4094 for (i
= 0; i
< nvar
; ++i
) {
4095 if (active
[nparam
+ i
])
4097 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4098 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4110 struct isl_opt_data
{
4111 isl_qpolynomial
*qp
;
4117 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4119 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4122 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4126 } else if (data
->max
) {
4127 data
->opt
= isl_val_max(data
->opt
, val
);
4129 data
->opt
= isl_val_min(data
->opt
, val
);
4135 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4136 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4138 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4143 if (isl_upoly_is_cst(qp
->upoly
)) {
4145 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4146 isl_qpolynomial_free(qp
);
4150 set
= fix_inactive(set
, qp
);
4153 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4157 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4160 isl_qpolynomial_free(qp
);
4164 isl_qpolynomial_free(qp
);
4165 isl_val_free(data
.opt
);
4169 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4170 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4175 struct isl_upoly
**subs
;
4176 isl_mat
*mat
, *diag
;
4178 qp
= isl_qpolynomial_cow(qp
);
4183 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4185 n_sub
= morph
->inv
->n_row
- 1;
4186 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4187 n_sub
+= qp
->div
->n_row
;
4188 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4192 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4193 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4194 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4195 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4196 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4197 subs
[morph
->inv
->n_row
- 1 + i
] =
4198 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4200 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4202 for (i
= 0; i
< n_sub
; ++i
)
4203 isl_upoly_free(subs
[i
]);
4206 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4207 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4208 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4209 mat
= isl_mat_diagonal(mat
, diag
);
4210 qp
->div
= isl_mat_product(qp
->div
, mat
);
4211 isl_space_free(qp
->dim
);
4212 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4214 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4217 isl_morph_free(morph
);
4221 isl_qpolynomial_free(qp
);
4222 isl_morph_free(morph
);
4226 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4227 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4228 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4230 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4231 &isl_pw_qpolynomial_mul
);
4234 /* Reorder the dimension of "qp" according to the given reordering.
4236 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4237 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4241 qp
= isl_qpolynomial_cow(qp
);
4245 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4249 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4253 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4257 space
= isl_reordering_get_space(r
);
4258 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4260 isl_reordering_free(r
);
4263 isl_qpolynomial_free(qp
);
4264 isl_reordering_free(r
);
4268 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4269 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4271 isl_bool equal_params
;
4276 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4277 if (equal_params
< 0)
4279 if (!equal_params
) {
4280 isl_reordering
*exp
;
4282 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4283 exp
= isl_reordering_extend_space(exp
,
4284 isl_qpolynomial_get_domain_space(qp
));
4285 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4288 isl_space_free(model
);
4291 isl_space_free(model
);
4292 isl_qpolynomial_free(qp
);
4296 struct isl_split_periods_data
{
4298 isl_pw_qpolynomial
*res
;
4301 /* Create a slice where the integer division "div" has the fixed value "v".
4302 * In particular, if "div" refers to floor(f/m), then create a slice
4304 * m v <= f <= m v + (m - 1)
4309 * -f + m v + (m - 1) >= 0
4311 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4312 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4315 isl_basic_set
*bset
= NULL
;
4321 total
= isl_space_dim(space
, isl_dim_all
);
4322 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4324 k
= isl_basic_set_alloc_inequality(bset
);
4327 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4328 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4330 k
= isl_basic_set_alloc_inequality(bset
);
4333 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4334 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4335 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4336 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4338 isl_space_free(space
);
4339 return isl_set_from_basic_set(bset
);
4341 isl_basic_set_free(bset
);
4342 isl_space_free(space
);
4346 static isl_stat
split_periods(__isl_take isl_set
*set
,
4347 __isl_take isl_qpolynomial
*qp
, void *user
);
4349 /* Create a slice of the domain "set" such that integer division "div"
4350 * has the fixed value "v" and add the results to data->res,
4351 * replacing the integer division by "v" in "qp".
4353 static isl_stat
set_div(__isl_take isl_set
*set
,
4354 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4355 struct isl_split_periods_data
*data
)
4360 struct isl_upoly
*cst
;
4362 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4363 set
= isl_set_intersect(set
, slice
);
4368 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4370 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4371 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4373 isl_int_addmul(qp
->div
->row
[i
][1],
4374 qp
->div
->row
[i
][2 + total
+ div
], v
);
4375 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4378 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4379 qp
= substitute_div(qp
, div
, cst
);
4381 return split_periods(set
, qp
, data
);
4384 isl_qpolynomial_free(qp
);
4385 return isl_stat_error
;
4388 /* Split the domain "set" such that integer division "div"
4389 * has a fixed value (ranging from "min" to "max") on each slice
4390 * and add the results to data->res.
4392 static isl_stat
split_div(__isl_take isl_set
*set
,
4393 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4394 struct isl_split_periods_data
*data
)
4396 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4397 isl_set
*set_i
= isl_set_copy(set
);
4398 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4400 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4404 isl_qpolynomial_free(qp
);
4408 isl_qpolynomial_free(qp
);
4409 return isl_stat_error
;
4412 /* If "qp" refers to any integer division
4413 * that can only attain "max_periods" distinct values on "set"
4414 * then split the domain along those distinct values.
4415 * Add the results (or the original if no splitting occurs)
4418 static isl_stat
split_periods(__isl_take isl_set
*set
,
4419 __isl_take isl_qpolynomial
*qp
, void *user
)
4422 isl_pw_qpolynomial
*pwqp
;
4423 struct isl_split_periods_data
*data
;
4426 isl_stat r
= isl_stat_ok
;
4428 data
= (struct isl_split_periods_data
*)user
;
4433 if (qp
->div
->n_row
== 0) {
4434 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4435 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4441 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4442 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4443 enum isl_lp_result lp_res
;
4445 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4446 qp
->div
->n_row
) != -1)
4449 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4450 set
->ctx
->one
, &min
, NULL
, NULL
);
4451 if (lp_res
== isl_lp_error
)
4453 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4455 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4457 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4458 set
->ctx
->one
, &max
, NULL
, NULL
);
4459 if (lp_res
== isl_lp_error
)
4461 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4463 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4465 isl_int_sub(max
, max
, min
);
4466 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4467 isl_int_add(max
, max
, min
);
4472 if (i
< qp
->div
->n_row
) {
4473 r
= split_div(set
, qp
, i
, min
, max
, data
);
4475 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4476 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4488 isl_qpolynomial_free(qp
);
4489 return isl_stat_error
;
4492 /* If any quasi-polynomial in pwqp refers to any integer division
4493 * that can only attain "max_periods" distinct values on its domain
4494 * then split the domain along those distinct values.
4496 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4497 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4499 struct isl_split_periods_data data
;
4501 data
.max_periods
= max_periods
;
4502 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4504 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4507 isl_pw_qpolynomial_free(pwqp
);
4511 isl_pw_qpolynomial_free(data
.res
);
4512 isl_pw_qpolynomial_free(pwqp
);
4516 /* Construct a piecewise quasipolynomial that is constant on the given
4517 * domain. In particular, it is
4520 * infinity if cst == -1
4522 * If cst == -1, then explicitly check whether the domain is empty and,
4523 * if so, return 0 instead.
4525 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4526 __isl_take isl_basic_set
*bset
, int cst
)
4529 isl_qpolynomial
*qp
;
4531 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4536 bset
= isl_basic_set_params(bset
);
4537 dim
= isl_basic_set_get_space(bset
);
4539 qp
= isl_qpolynomial_infty_on_domain(dim
);
4541 qp
= isl_qpolynomial_zero_on_domain(dim
);
4543 qp
= isl_qpolynomial_one_on_domain(dim
);
4544 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4547 /* Factor bset, call fn on each of the factors and return the product.
4549 * If no factors can be found, simply call fn on the input.
4550 * Otherwise, construct the factors based on the factorizer,
4551 * call fn on each factor and compute the product.
4553 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4554 __isl_take isl_basic_set
*bset
,
4555 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4561 isl_qpolynomial
*qp
;
4562 isl_pw_qpolynomial
*pwqp
;
4566 f
= isl_basic_set_factorizer(bset
);
4569 if (f
->n_group
== 0) {
4570 isl_factorizer_free(f
);
4574 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4575 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4577 space
= isl_basic_set_get_space(bset
);
4578 space
= isl_space_params(space
);
4579 set
= isl_set_universe(isl_space_copy(space
));
4580 qp
= isl_qpolynomial_one_on_domain(space
);
4581 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4583 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4585 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4586 isl_basic_set
*bset_i
;
4587 isl_pw_qpolynomial
*pwqp_i
;
4589 bset_i
= isl_basic_set_copy(bset
);
4590 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4591 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4592 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4594 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4595 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4596 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4598 pwqp_i
= fn(bset_i
);
4599 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4604 isl_basic_set_free(bset
);
4605 isl_factorizer_free(f
);
4609 isl_basic_set_free(bset
);
4613 /* Factor bset, call fn on each of the factors and return the product.
4614 * The function is assumed to evaluate to zero on empty domains,
4615 * to one on zero-dimensional domains and to infinity on unbounded domains
4616 * and will not be called explicitly on zero-dimensional or unbounded domains.
4618 * We first check for some special cases and remove all equalities.
4619 * Then we hand over control to compressed_multiplicative_call.
4621 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4622 __isl_take isl_basic_set
*bset
,
4623 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4627 isl_pw_qpolynomial
*pwqp
;
4632 if (isl_basic_set_plain_is_empty(bset
))
4633 return constant_on_domain(bset
, 0);
4635 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4636 return constant_on_domain(bset
, 1);
4638 bounded
= isl_basic_set_is_bounded(bset
);
4642 return constant_on_domain(bset
, -1);
4644 if (bset
->n_eq
== 0)
4645 return compressed_multiplicative_call(bset
, fn
);
4647 morph
= isl_basic_set_full_compression(bset
);
4648 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4650 pwqp
= compressed_multiplicative_call(bset
, fn
);
4652 morph
= isl_morph_dom_params(morph
);
4653 morph
= isl_morph_ran_params(morph
);
4654 morph
= isl_morph_inverse(morph
);
4656 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4660 isl_basic_set_free(bset
);
4664 /* Drop all floors in "qp", turning each integer division [a/m] into
4665 * a rational division a/m. If "down" is set, then the integer division
4666 * is replaced by (a-(m-1))/m instead.
4668 static __isl_give isl_qpolynomial
*qp_drop_floors(
4669 __isl_take isl_qpolynomial
*qp
, int down
)
4672 struct isl_upoly
*s
;
4676 if (qp
->div
->n_row
== 0)
4679 qp
= isl_qpolynomial_cow(qp
);
4683 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4685 isl_int_sub(qp
->div
->row
[i
][1],
4686 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4687 isl_int_add_ui(qp
->div
->row
[i
][1],
4688 qp
->div
->row
[i
][1], 1);
4690 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4691 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4692 qp
= substitute_div(qp
, i
, s
);
4700 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4701 * a rational division a/m.
4703 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4704 __isl_take isl_pw_qpolynomial
*pwqp
)
4711 if (isl_pw_qpolynomial_is_zero(pwqp
))
4714 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4718 for (i
= 0; i
< pwqp
->n
; ++i
) {
4719 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4726 isl_pw_qpolynomial_free(pwqp
);
4730 /* Adjust all the integer divisions in "qp" such that they are at least
4731 * one over the given orthant (identified by "signs"). This ensures
4732 * that they will still be non-negative even after subtracting (m-1)/m.
4734 * In particular, f is replaced by f' + v, changing f = [a/m]
4735 * to f' = [(a - m v)/m].
4736 * If the constant term k in a is smaller than m,
4737 * the constant term of v is set to floor(k/m) - 1.
4738 * For any other term, if the coefficient c and the variable x have
4739 * the same sign, then no changes are needed.
4740 * Otherwise, if the variable is positive (and c is negative),
4741 * then the coefficient of x in v is set to floor(c/m).
4742 * If the variable is negative (and c is positive),
4743 * then the coefficient of x in v is set to ceil(c/m).
4745 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4751 struct isl_upoly
*s
;
4753 qp
= isl_qpolynomial_cow(qp
);
4756 qp
->div
= isl_mat_cow(qp
->div
);
4760 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4761 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4763 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4764 isl_int
*row
= qp
->div
->row
[i
];
4768 if (isl_int_lt(row
[1], row
[0])) {
4769 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4770 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4771 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4773 for (j
= 0; j
< total
; ++j
) {
4774 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4777 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4779 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4780 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4782 for (j
= 0; j
< i
; ++j
) {
4783 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4785 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4786 row
[2 + total
+ j
], row
[0]);
4787 isl_int_submul(row
[2 + total
+ j
],
4788 row
[0], v
->el
[1 + total
+ j
]);
4790 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4791 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4793 isl_seq_combine(qp
->div
->row
[j
] + 1,
4794 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4795 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4797 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4798 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4799 qp
->div
->ctx
->one
, v
->size
);
4800 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4810 isl_qpolynomial_free(qp
);
4814 struct isl_to_poly_data
{
4816 isl_pw_qpolynomial
*res
;
4817 isl_qpolynomial
*qp
;
4820 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4821 * We first make all integer divisions positive and then split the
4822 * quasipolynomials into terms with sign data->sign (the direction
4823 * of the requested approximation) and terms with the opposite sign.
4824 * In the first set of terms, each integer division [a/m] is
4825 * overapproximated by a/m, while in the second it is underapproximated
4828 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4829 int *signs
, void *user
)
4831 struct isl_to_poly_data
*data
= user
;
4832 isl_pw_qpolynomial
*t
;
4833 isl_qpolynomial
*qp
, *up
, *down
;
4835 qp
= isl_qpolynomial_copy(data
->qp
);
4836 qp
= make_divs_pos(qp
, signs
);
4838 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4839 up
= qp_drop_floors(up
, 0);
4840 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4841 down
= qp_drop_floors(down
, 1);
4843 isl_qpolynomial_free(qp
);
4844 qp
= isl_qpolynomial_add(up
, down
);
4846 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4847 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4852 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4853 * the polynomial will be an overapproximation. If "sign" is negative,
4854 * it will be an underapproximation. If "sign" is zero, the approximation
4855 * will lie somewhere in between.
4857 * In particular, is sign == 0, we simply drop the floors, turning
4858 * the integer divisions into rational divisions.
4859 * Otherwise, we split the domains into orthants, make all integer divisions
4860 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4861 * depending on the requested sign and the sign of the term in which
4862 * the integer division appears.
4864 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4865 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4868 struct isl_to_poly_data data
;
4871 return pwqp_drop_floors(pwqp
);
4877 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4879 for (i
= 0; i
< pwqp
->n
; ++i
) {
4880 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4881 isl_pw_qpolynomial
*t
;
4882 t
= isl_pw_qpolynomial_alloc(
4883 isl_set_copy(pwqp
->p
[i
].set
),
4884 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4885 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4888 data
.qp
= pwqp
->p
[i
].qp
;
4889 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4890 &to_polynomial_on_orthant
, &data
) < 0)
4894 isl_pw_qpolynomial_free(pwqp
);
4898 isl_pw_qpolynomial_free(pwqp
);
4899 isl_pw_qpolynomial_free(data
.res
);
4903 static __isl_give isl_pw_qpolynomial
*poly_entry(
4904 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4908 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4911 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4912 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4914 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4915 &poly_entry
, &sign
);
4918 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4919 __isl_take isl_qpolynomial
*qp
)
4923 isl_vec
*aff
= NULL
;
4924 isl_basic_map
*bmap
= NULL
;
4930 if (!isl_upoly_is_affine(qp
->upoly
))
4931 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4932 "input quasi-polynomial not affine", goto error
);
4933 aff
= isl_qpolynomial_extract_affine(qp
);
4936 dim
= isl_qpolynomial_get_space(qp
);
4937 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4938 n_div
= qp
->div
->n_row
;
4939 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4941 for (i
= 0; i
< n_div
; ++i
) {
4942 k
= isl_basic_map_alloc_div(bmap
);
4945 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4946 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4947 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4950 k
= isl_basic_map_alloc_equality(bmap
);
4953 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4954 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4955 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4958 isl_qpolynomial_free(qp
);
4959 bmap
= isl_basic_map_finalize(bmap
);
4963 isl_qpolynomial_free(qp
);
4964 isl_basic_map_free(bmap
);