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,
13 #include <isl_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_factorization.h>
16 #include <isl_lp_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl_constraint_private.h>
20 #include <isl_polynomial_private.h>
21 #include <isl_point_private.h>
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_range.h>
26 #include <isl_local.h>
27 #include <isl_local_space_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_val_private.h>
30 #include <isl_config.h>
31 #include <isl/deprecated/polynomial_int.h>
33 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
36 case isl_dim_param
: return 0;
37 case isl_dim_in
: return dim
->nparam
;
38 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
43 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
51 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
56 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
58 return (struct isl_upoly_cst
*)up
;
61 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
66 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
68 return (struct isl_upoly_rec
*)up
;
71 /* Compare two polynomials.
73 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
74 * than "up2" and 0 if they are equal.
76 static int isl_upoly_plain_cmp(__isl_keep
struct isl_upoly
*up1
,
77 __isl_keep
struct isl_upoly
*up2
)
80 struct isl_upoly_rec
*rec1
, *rec2
;
88 if (up1
->var
!= up2
->var
)
89 return up1
->var
- up2
->var
;
91 if (isl_upoly_is_cst(up1
)) {
92 struct isl_upoly_cst
*cst1
, *cst2
;
95 cst1
= isl_upoly_as_cst(up1
);
96 cst2
= isl_upoly_as_cst(up2
);
99 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
102 return isl_int_cmp(cst1
->d
, cst2
->d
);
105 rec1
= isl_upoly_as_rec(up1
);
106 rec2
= isl_upoly_as_rec(up2
);
110 if (rec1
->n
!= rec2
->n
)
111 return rec1
->n
- rec2
->n
;
113 for (i
= 0; i
< rec1
->n
; ++i
) {
114 int cmp
= isl_upoly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
122 isl_bool
isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
123 __isl_keep
struct isl_upoly
*up2
)
126 struct isl_upoly_rec
*rec1
, *rec2
;
129 return isl_bool_error
;
131 return isl_bool_true
;
132 if (up1
->var
!= up2
->var
)
133 return isl_bool_false
;
134 if (isl_upoly_is_cst(up1
)) {
135 struct isl_upoly_cst
*cst1
, *cst2
;
136 cst1
= isl_upoly_as_cst(up1
);
137 cst2
= isl_upoly_as_cst(up2
);
139 return isl_bool_error
;
140 return isl_int_eq(cst1
->n
, cst2
->n
) &&
141 isl_int_eq(cst1
->d
, cst2
->d
);
144 rec1
= isl_upoly_as_rec(up1
);
145 rec2
= isl_upoly_as_rec(up2
);
147 return isl_bool_error
;
149 if (rec1
->n
!= rec2
->n
)
150 return isl_bool_false
;
152 for (i
= 0; i
< rec1
->n
; ++i
) {
153 isl_bool eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
158 return isl_bool_true
;
161 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
163 struct isl_upoly_cst
*cst
;
167 if (!isl_upoly_is_cst(up
))
170 cst
= isl_upoly_as_cst(up
);
174 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
177 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
179 struct isl_upoly_cst
*cst
;
183 if (!isl_upoly_is_cst(up
))
186 cst
= isl_upoly_as_cst(up
);
190 return isl_int_sgn(cst
->n
);
193 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
195 struct isl_upoly_cst
*cst
;
199 if (!isl_upoly_is_cst(up
))
202 cst
= isl_upoly_as_cst(up
);
206 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
209 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
211 struct isl_upoly_cst
*cst
;
215 if (!isl_upoly_is_cst(up
))
218 cst
= isl_upoly_as_cst(up
);
222 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
225 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
227 struct isl_upoly_cst
*cst
;
231 if (!isl_upoly_is_cst(up
))
234 cst
= isl_upoly_as_cst(up
);
238 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
241 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
243 struct isl_upoly_cst
*cst
;
247 if (!isl_upoly_is_cst(up
))
250 cst
= isl_upoly_as_cst(up
);
254 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
257 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
259 struct isl_upoly_cst
*cst
;
263 if (!isl_upoly_is_cst(up
))
266 cst
= isl_upoly_as_cst(up
);
270 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
273 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
275 struct isl_upoly_cst
*cst
;
277 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
286 isl_int_init(cst
->n
);
287 isl_int_init(cst
->d
);
292 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
294 struct isl_upoly_cst
*cst
;
296 cst
= isl_upoly_cst_alloc(ctx
);
300 isl_int_set_si(cst
->n
, 0);
301 isl_int_set_si(cst
->d
, 1);
306 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
308 struct isl_upoly_cst
*cst
;
310 cst
= isl_upoly_cst_alloc(ctx
);
314 isl_int_set_si(cst
->n
, 1);
315 isl_int_set_si(cst
->d
, 1);
320 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
322 struct isl_upoly_cst
*cst
;
324 cst
= isl_upoly_cst_alloc(ctx
);
328 isl_int_set_si(cst
->n
, 1);
329 isl_int_set_si(cst
->d
, 0);
334 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
336 struct isl_upoly_cst
*cst
;
338 cst
= isl_upoly_cst_alloc(ctx
);
342 isl_int_set_si(cst
->n
, -1);
343 isl_int_set_si(cst
->d
, 0);
348 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
350 struct isl_upoly_cst
*cst
;
352 cst
= isl_upoly_cst_alloc(ctx
);
356 isl_int_set_si(cst
->n
, 0);
357 isl_int_set_si(cst
->d
, 0);
362 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
363 isl_int n
, isl_int d
)
365 struct isl_upoly_cst
*cst
;
367 cst
= isl_upoly_cst_alloc(ctx
);
371 isl_int_set(cst
->n
, n
);
372 isl_int_set(cst
->d
, d
);
377 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
380 struct isl_upoly_rec
*rec
;
382 isl_assert(ctx
, var
>= 0, return NULL
);
383 isl_assert(ctx
, size
>= 0, return NULL
);
384 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
385 sizeof(struct isl_upoly_rec
) +
386 size
* sizeof(struct isl_upoly
*));
401 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
402 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
404 qp
= isl_qpolynomial_cow(qp
);
408 isl_space_free(qp
->dim
);
413 isl_qpolynomial_free(qp
);
418 /* Reset the space of "qp". This function is called from isl_pw_templ.c
419 * and doesn't know if the space of an element object is represented
420 * directly or through its domain. It therefore passes along both.
422 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
423 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
424 __isl_take isl_space
*domain
)
426 isl_space_free(space
);
427 return isl_qpolynomial_reset_domain_space(qp
, domain
);
430 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
432 return qp
? qp
->dim
->ctx
: NULL
;
435 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
436 __isl_keep isl_qpolynomial
*qp
)
438 return qp
? isl_space_copy(qp
->dim
) : NULL
;
441 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
446 space
= isl_space_copy(qp
->dim
);
447 space
= isl_space_from_domain(space
);
448 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
452 /* Externally, an isl_qpolynomial has a map space, but internally, the
453 * ls field corresponds to the domain of that space.
455 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
456 enum isl_dim_type type
)
460 if (type
== isl_dim_out
)
462 if (type
== isl_dim_in
)
464 return isl_space_dim(qp
->dim
, type
);
467 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
469 return qp
? isl_upoly_is_zero(qp
->upoly
) : isl_bool_error
;
472 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
474 return qp
? isl_upoly_is_one(qp
->upoly
) : isl_bool_error
;
477 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
479 return qp
? isl_upoly_is_nan(qp
->upoly
) : isl_bool_error
;
482 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
484 return qp
? isl_upoly_is_infty(qp
->upoly
) : isl_bool_error
;
487 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
489 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : isl_bool_error
;
492 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
494 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
497 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
499 isl_int_clear(cst
->n
);
500 isl_int_clear(cst
->d
);
503 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
507 for (i
= 0; i
< rec
->n
; ++i
)
508 isl_upoly_free(rec
->p
[i
]);
511 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
520 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
522 struct isl_upoly_cst
*cst
;
523 struct isl_upoly_cst
*dup
;
525 cst
= isl_upoly_as_cst(up
);
529 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
532 isl_int_set(dup
->n
, cst
->n
);
533 isl_int_set(dup
->d
, cst
->d
);
538 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
541 struct isl_upoly_rec
*rec
;
542 struct isl_upoly_rec
*dup
;
544 rec
= isl_upoly_as_rec(up
);
548 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
552 for (i
= 0; i
< rec
->n
; ++i
) {
553 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
561 isl_upoly_free(&dup
->up
);
565 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
570 if (isl_upoly_is_cst(up
))
571 return isl_upoly_dup_cst(up
);
573 return isl_upoly_dup_rec(up
);
576 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
584 return isl_upoly_dup(up
);
587 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
596 upoly_free_cst((struct isl_upoly_cst
*)up
);
598 upoly_free_rec((struct isl_upoly_rec
*)up
);
600 isl_ctx_deref(up
->ctx
);
604 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
609 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
610 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
611 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
612 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
617 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
618 __isl_take
struct isl_upoly
*up2
)
620 struct isl_upoly_cst
*cst1
;
621 struct isl_upoly_cst
*cst2
;
623 up1
= isl_upoly_cow(up1
);
627 cst1
= isl_upoly_as_cst(up1
);
628 cst2
= isl_upoly_as_cst(up2
);
630 if (isl_int_eq(cst1
->d
, cst2
->d
))
631 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
633 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
634 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
635 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
638 isl_upoly_cst_reduce(cst1
);
648 static __isl_give
struct isl_upoly
*replace_by_zero(
649 __isl_take
struct isl_upoly
*up
)
657 return isl_upoly_zero(ctx
);
660 static __isl_give
struct isl_upoly
*replace_by_constant_term(
661 __isl_take
struct isl_upoly
*up
)
663 struct isl_upoly_rec
*rec
;
664 struct isl_upoly
*cst
;
669 rec
= isl_upoly_as_rec(up
);
672 cst
= isl_upoly_copy(rec
->p
[0]);
680 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
681 __isl_take
struct isl_upoly
*up2
)
684 struct isl_upoly_rec
*rec1
, *rec2
;
689 if (isl_upoly_is_nan(up1
)) {
694 if (isl_upoly_is_nan(up2
)) {
699 if (isl_upoly_is_zero(up1
)) {
704 if (isl_upoly_is_zero(up2
)) {
709 if (up1
->var
< up2
->var
)
710 return isl_upoly_sum(up2
, up1
);
712 if (up2
->var
< up1
->var
) {
713 struct isl_upoly_rec
*rec
;
714 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
718 up1
= isl_upoly_cow(up1
);
719 rec
= isl_upoly_as_rec(up1
);
722 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
724 up1
= replace_by_constant_term(up1
);
728 if (isl_upoly_is_cst(up1
))
729 return isl_upoly_sum_cst(up1
, up2
);
731 rec1
= isl_upoly_as_rec(up1
);
732 rec2
= isl_upoly_as_rec(up2
);
736 if (rec1
->n
< rec2
->n
)
737 return isl_upoly_sum(up2
, up1
);
739 up1
= isl_upoly_cow(up1
);
740 rec1
= isl_upoly_as_rec(up1
);
744 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
745 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
746 isl_upoly_copy(rec2
->p
[i
]));
749 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
750 isl_upoly_free(rec1
->p
[i
]);
756 up1
= replace_by_zero(up1
);
757 else if (rec1
->n
== 1)
758 up1
= replace_by_constant_term(up1
);
769 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
770 __isl_take
struct isl_upoly
*up
, isl_int v
)
772 struct isl_upoly_cst
*cst
;
774 up
= isl_upoly_cow(up
);
778 cst
= isl_upoly_as_cst(up
);
780 isl_int_addmul(cst
->n
, cst
->d
, v
);
785 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
786 __isl_take
struct isl_upoly
*up
, isl_int v
)
788 struct isl_upoly_rec
*rec
;
793 if (isl_upoly_is_cst(up
))
794 return isl_upoly_cst_add_isl_int(up
, v
);
796 up
= isl_upoly_cow(up
);
797 rec
= isl_upoly_as_rec(up
);
801 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
811 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
812 __isl_take
struct isl_upoly
*up
, isl_int v
)
814 struct isl_upoly_cst
*cst
;
816 if (isl_upoly_is_zero(up
))
819 up
= isl_upoly_cow(up
);
823 cst
= isl_upoly_as_cst(up
);
825 isl_int_mul(cst
->n
, cst
->n
, v
);
830 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
831 __isl_take
struct isl_upoly
*up
, isl_int v
)
834 struct isl_upoly_rec
*rec
;
839 if (isl_upoly_is_cst(up
))
840 return isl_upoly_cst_mul_isl_int(up
, v
);
842 up
= isl_upoly_cow(up
);
843 rec
= isl_upoly_as_rec(up
);
847 for (i
= 0; i
< rec
->n
; ++i
) {
848 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
859 /* Multiply the constant polynomial "up" by "v".
861 static __isl_give
struct isl_upoly
*isl_upoly_cst_scale_val(
862 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
864 struct isl_upoly_cst
*cst
;
866 if (isl_upoly_is_zero(up
))
869 up
= isl_upoly_cow(up
);
873 cst
= isl_upoly_as_cst(up
);
875 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
876 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
877 isl_upoly_cst_reduce(cst
);
882 /* Multiply the polynomial "up" by "v".
884 static __isl_give
struct isl_upoly
*isl_upoly_scale_val(
885 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
888 struct isl_upoly_rec
*rec
;
893 if (isl_upoly_is_cst(up
))
894 return isl_upoly_cst_scale_val(up
, v
);
896 up
= isl_upoly_cow(up
);
897 rec
= isl_upoly_as_rec(up
);
901 for (i
= 0; i
< rec
->n
; ++i
) {
902 rec
->p
[i
] = isl_upoly_scale_val(rec
->p
[i
], v
);
913 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
914 __isl_take
struct isl_upoly
*up2
)
916 struct isl_upoly_cst
*cst1
;
917 struct isl_upoly_cst
*cst2
;
919 up1
= isl_upoly_cow(up1
);
923 cst1
= isl_upoly_as_cst(up1
);
924 cst2
= isl_upoly_as_cst(up2
);
926 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
927 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
929 isl_upoly_cst_reduce(cst1
);
939 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
940 __isl_take
struct isl_upoly
*up2
)
942 struct isl_upoly_rec
*rec1
;
943 struct isl_upoly_rec
*rec2
;
944 struct isl_upoly_rec
*res
= NULL
;
948 rec1
= isl_upoly_as_rec(up1
);
949 rec2
= isl_upoly_as_rec(up2
);
952 size
= rec1
->n
+ rec2
->n
- 1;
953 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
957 for (i
= 0; i
< rec1
->n
; ++i
) {
958 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
959 isl_upoly_copy(rec1
->p
[i
]));
964 for (; i
< size
; ++i
) {
965 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
970 for (i
= 0; i
< rec1
->n
; ++i
) {
971 for (j
= 1; j
< rec2
->n
; ++j
) {
972 struct isl_upoly
*up
;
973 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
974 isl_upoly_copy(rec1
->p
[i
]));
975 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
988 isl_upoly_free(&res
->up
);
992 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
993 __isl_take
struct isl_upoly
*up2
)
998 if (isl_upoly_is_nan(up1
)) {
1003 if (isl_upoly_is_nan(up2
)) {
1004 isl_upoly_free(up1
);
1008 if (isl_upoly_is_zero(up1
)) {
1009 isl_upoly_free(up2
);
1013 if (isl_upoly_is_zero(up2
)) {
1014 isl_upoly_free(up1
);
1018 if (isl_upoly_is_one(up1
)) {
1019 isl_upoly_free(up1
);
1023 if (isl_upoly_is_one(up2
)) {
1024 isl_upoly_free(up2
);
1028 if (up1
->var
< up2
->var
)
1029 return isl_upoly_mul(up2
, up1
);
1031 if (up2
->var
< up1
->var
) {
1033 struct isl_upoly_rec
*rec
;
1034 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
1035 isl_ctx
*ctx
= up1
->ctx
;
1036 isl_upoly_free(up1
);
1037 isl_upoly_free(up2
);
1038 return isl_upoly_nan(ctx
);
1040 up1
= isl_upoly_cow(up1
);
1041 rec
= isl_upoly_as_rec(up1
);
1045 for (i
= 0; i
< rec
->n
; ++i
) {
1046 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
1047 isl_upoly_copy(up2
));
1051 isl_upoly_free(up2
);
1055 if (isl_upoly_is_cst(up1
))
1056 return isl_upoly_mul_cst(up1
, up2
);
1058 return isl_upoly_mul_rec(up1
, up2
);
1060 isl_upoly_free(up1
);
1061 isl_upoly_free(up2
);
1065 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
1068 struct isl_upoly
*res
;
1076 res
= isl_upoly_copy(up
);
1078 res
= isl_upoly_one(up
->ctx
);
1080 while (power
>>= 1) {
1081 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
1083 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
1090 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*dim
,
1091 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1093 struct isl_qpolynomial
*qp
= NULL
;
1099 if (!isl_space_is_set(dim
))
1100 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1101 "domain of polynomial should be a set", goto error
);
1103 total
= isl_space_dim(dim
, isl_dim_all
);
1105 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
1110 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1119 isl_space_free(dim
);
1121 isl_qpolynomial_free(qp
);
1125 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1134 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1136 struct isl_qpolynomial
*dup
;
1141 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1142 isl_upoly_copy(qp
->upoly
));
1145 isl_mat_free(dup
->div
);
1146 dup
->div
= isl_mat_copy(qp
->div
);
1152 isl_qpolynomial_free(dup
);
1156 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1164 return isl_qpolynomial_dup(qp
);
1167 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1168 __isl_take isl_qpolynomial
*qp
)
1176 isl_space_free(qp
->dim
);
1177 isl_mat_free(qp
->div
);
1178 isl_upoly_free(qp
->upoly
);
1184 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1187 struct isl_upoly_rec
*rec
;
1188 struct isl_upoly_cst
*cst
;
1190 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1193 for (i
= 0; i
< 1 + power
; ++i
) {
1194 rec
->p
[i
] = isl_upoly_zero(ctx
);
1199 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1200 isl_int_set_si(cst
->n
, 1);
1204 isl_upoly_free(&rec
->up
);
1208 /* r array maps original positions to new positions.
1210 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1214 struct isl_upoly_rec
*rec
;
1215 struct isl_upoly
*base
;
1216 struct isl_upoly
*res
;
1218 if (isl_upoly_is_cst(up
))
1221 rec
= isl_upoly_as_rec(up
);
1225 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1227 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1228 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1230 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1231 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1232 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1235 isl_upoly_free(base
);
1244 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
1249 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1250 div1
->n_col
>= div2
->n_col
, return -1);
1252 if (div1
->n_row
== div2
->n_row
)
1253 return isl_mat_is_equal(div1
, div2
);
1255 n_row
= div1
->n_row
;
1256 n_col
= div1
->n_col
;
1257 div1
->n_row
= div2
->n_row
;
1258 div1
->n_col
= div2
->n_col
;
1260 equal
= isl_mat_is_equal(div1
, div2
);
1262 div1
->n_row
= n_row
;
1263 div1
->n_col
= n_col
;
1268 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1272 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1273 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1278 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1281 struct isl_div_sort_info
{
1286 static int div_sort_cmp(const void *p1
, const void *p2
)
1288 const struct isl_div_sort_info
*i1
, *i2
;
1289 i1
= (const struct isl_div_sort_info
*) p1
;
1290 i2
= (const struct isl_div_sort_info
*) p2
;
1292 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1295 /* Sort divs and remove duplicates.
1297 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1302 struct isl_div_sort_info
*array
= NULL
;
1303 int *pos
= NULL
, *at
= NULL
;
1304 int *reordering
= NULL
;
1309 if (qp
->div
->n_row
<= 1)
1312 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1314 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1316 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1317 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1318 len
= qp
->div
->n_col
- 2;
1319 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1320 if (!array
|| !pos
|| !at
|| !reordering
)
1323 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1324 array
[i
].div
= qp
->div
;
1330 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1333 for (i
= 0; i
< div_pos
; ++i
)
1336 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1337 if (pos
[array
[i
].row
] == i
)
1339 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1340 pos
[at
[i
]] = pos
[array
[i
].row
];
1341 at
[pos
[array
[i
].row
]] = at
[i
];
1342 at
[i
] = array
[i
].row
;
1343 pos
[array
[i
].row
] = i
;
1347 for (i
= 0; i
< len
- div_pos
; ++i
) {
1349 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1350 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1351 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1352 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1353 2 + div_pos
+ i
- skip
);
1354 qp
->div
= isl_mat_drop_cols(qp
->div
,
1355 2 + div_pos
+ i
- skip
, 1);
1358 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1361 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1363 if (!qp
->upoly
|| !qp
->div
)
1377 isl_qpolynomial_free(qp
);
1381 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1382 int *exp
, int first
)
1385 struct isl_upoly_rec
*rec
;
1387 if (isl_upoly_is_cst(up
))
1390 if (up
->var
< first
)
1393 if (exp
[up
->var
- first
] == up
->var
- first
)
1396 up
= isl_upoly_cow(up
);
1400 up
->var
= exp
[up
->var
- first
] + first
;
1402 rec
= isl_upoly_as_rec(up
);
1406 for (i
= 0; i
< rec
->n
; ++i
) {
1407 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1418 static __isl_give isl_qpolynomial
*with_merged_divs(
1419 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1420 __isl_take isl_qpolynomial
*qp2
),
1421 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1425 isl_mat
*div
= NULL
;
1428 qp1
= isl_qpolynomial_cow(qp1
);
1429 qp2
= isl_qpolynomial_cow(qp2
);
1434 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1435 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1437 n_div1
= qp1
->div
->n_row
;
1438 n_div2
= qp2
->div
->n_row
;
1439 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1440 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1441 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1444 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1448 isl_mat_free(qp1
->div
);
1449 qp1
->div
= isl_mat_copy(div
);
1450 isl_mat_free(qp2
->div
);
1451 qp2
->div
= isl_mat_copy(div
);
1453 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1454 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1456 if (!qp1
->upoly
|| !qp2
->upoly
)
1463 return fn(qp1
, qp2
);
1468 isl_qpolynomial_free(qp1
);
1469 isl_qpolynomial_free(qp2
);
1473 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1474 __isl_take isl_qpolynomial
*qp2
)
1476 qp1
= isl_qpolynomial_cow(qp1
);
1481 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1482 return isl_qpolynomial_add(qp2
, qp1
);
1484 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1485 if (!compatible_divs(qp1
->div
, qp2
->div
))
1486 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1488 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1492 isl_qpolynomial_free(qp2
);
1496 isl_qpolynomial_free(qp1
);
1497 isl_qpolynomial_free(qp2
);
1501 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1502 __isl_keep isl_set
*dom
,
1503 __isl_take isl_qpolynomial
*qp1
,
1504 __isl_take isl_qpolynomial
*qp2
)
1506 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1507 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1511 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1512 __isl_take isl_qpolynomial
*qp2
)
1514 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1517 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1518 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1520 if (isl_int_is_zero(v
))
1523 qp
= isl_qpolynomial_cow(qp
);
1527 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1533 isl_qpolynomial_free(qp
);
1538 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1543 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1546 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1547 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1549 if (isl_int_is_one(v
))
1552 if (qp
&& isl_int_is_zero(v
)) {
1553 isl_qpolynomial
*zero
;
1554 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1555 isl_qpolynomial_free(qp
);
1559 qp
= isl_qpolynomial_cow(qp
);
1563 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1569 isl_qpolynomial_free(qp
);
1573 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1574 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1576 return isl_qpolynomial_mul_isl_int(qp
, v
);
1579 /* Multiply "qp" by "v".
1581 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1582 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1587 if (!isl_val_is_rat(v
))
1588 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1589 "expecting rational factor", goto error
);
1591 if (isl_val_is_one(v
)) {
1596 if (isl_val_is_zero(v
)) {
1599 space
= isl_qpolynomial_get_domain_space(qp
);
1600 isl_qpolynomial_free(qp
);
1602 return isl_qpolynomial_zero_on_domain(space
);
1605 qp
= isl_qpolynomial_cow(qp
);
1609 qp
->upoly
= isl_upoly_scale_val(qp
->upoly
, v
);
1611 qp
= isl_qpolynomial_free(qp
);
1617 isl_qpolynomial_free(qp
);
1621 /* Divide "qp" by "v".
1623 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1624 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1629 if (!isl_val_is_rat(v
))
1630 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1631 "expecting rational factor", goto error
);
1632 if (isl_val_is_zero(v
))
1633 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1634 "cannot scale down by zero", goto error
);
1636 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1639 isl_qpolynomial_free(qp
);
1643 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1644 __isl_take isl_qpolynomial
*qp2
)
1646 qp1
= isl_qpolynomial_cow(qp1
);
1651 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1652 return isl_qpolynomial_mul(qp2
, qp1
);
1654 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1655 if (!compatible_divs(qp1
->div
, qp2
->div
))
1656 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1658 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1662 isl_qpolynomial_free(qp2
);
1666 isl_qpolynomial_free(qp1
);
1667 isl_qpolynomial_free(qp2
);
1671 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1674 qp
= isl_qpolynomial_cow(qp
);
1679 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1685 isl_qpolynomial_free(qp
);
1689 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1690 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1697 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1701 for (i
= 0; i
< pwqp
->n
; ++i
) {
1702 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1704 return isl_pw_qpolynomial_free(pwqp
);
1710 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1711 __isl_take isl_space
*dim
)
1715 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1718 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1719 __isl_take isl_space
*dim
)
1723 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1726 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1727 __isl_take isl_space
*dim
)
1731 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1734 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1735 __isl_take isl_space
*dim
)
1739 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1742 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1743 __isl_take isl_space
*dim
)
1747 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1750 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1751 __isl_take isl_space
*dim
,
1754 struct isl_qpolynomial
*qp
;
1755 struct isl_upoly_cst
*cst
;
1760 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1764 cst
= isl_upoly_as_cst(qp
->upoly
);
1765 isl_int_set(cst
->n
, v
);
1770 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1771 isl_int
*n
, isl_int
*d
)
1773 struct isl_upoly_cst
*cst
;
1778 if (!isl_upoly_is_cst(qp
->upoly
))
1781 cst
= isl_upoly_as_cst(qp
->upoly
);
1786 isl_int_set(*n
, cst
->n
);
1788 isl_int_set(*d
, cst
->d
);
1793 /* Return the constant term of "up".
1795 static __isl_give isl_val
*isl_upoly_get_constant_val(
1796 __isl_keep
struct isl_upoly
*up
)
1798 struct isl_upoly_cst
*cst
;
1803 while (!isl_upoly_is_cst(up
)) {
1804 struct isl_upoly_rec
*rec
;
1806 rec
= isl_upoly_as_rec(up
);
1812 cst
= isl_upoly_as_cst(up
);
1815 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1818 /* Return the constant term of "qp".
1820 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1821 __isl_keep isl_qpolynomial
*qp
)
1826 return isl_upoly_get_constant_val(qp
->upoly
);
1829 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1832 struct isl_upoly_rec
*rec
;
1840 rec
= isl_upoly_as_rec(up
);
1847 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1849 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1855 return isl_upoly_is_affine(rec
->p
[0]);
1858 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1863 if (qp
->div
->n_row
> 0)
1866 return isl_upoly_is_affine(qp
->upoly
);
1869 static void update_coeff(__isl_keep isl_vec
*aff
,
1870 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1875 if (isl_int_is_zero(cst
->n
))
1880 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1881 isl_int_divexact(f
, cst
->d
, gcd
);
1882 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1883 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1884 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1889 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1890 __isl_keep isl_vec
*aff
)
1892 struct isl_upoly_cst
*cst
;
1893 struct isl_upoly_rec
*rec
;
1899 struct isl_upoly_cst
*cst
;
1901 cst
= isl_upoly_as_cst(up
);
1904 update_coeff(aff
, cst
, 0);
1908 rec
= isl_upoly_as_rec(up
);
1911 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1913 cst
= isl_upoly_as_cst(rec
->p
[1]);
1916 update_coeff(aff
, cst
, 1 + up
->var
);
1918 return isl_upoly_update_affine(rec
->p
[0], aff
);
1921 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1922 __isl_keep isl_qpolynomial
*qp
)
1930 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1931 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1935 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1936 isl_int_set_si(aff
->el
[0], 1);
1938 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1947 /* Compare two quasi-polynomials.
1949 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
1950 * than "qp2" and 0 if they are equal.
1952 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
1953 __isl_keep isl_qpolynomial
*qp2
)
1964 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
1968 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
1972 return isl_upoly_plain_cmp(qp1
->upoly
, qp2
->upoly
);
1975 /* Is "qp1" obviously equal to "qp2"?
1977 * NaN is not equal to anything, not even to another NaN.
1979 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1980 __isl_keep isl_qpolynomial
*qp2
)
1985 return isl_bool_error
;
1987 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
1988 return isl_bool_false
;
1990 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
1991 if (equal
< 0 || !equal
)
1994 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
1995 if (equal
< 0 || !equal
)
1998 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
2001 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
2004 struct isl_upoly_rec
*rec
;
2006 if (isl_upoly_is_cst(up
)) {
2007 struct isl_upoly_cst
*cst
;
2008 cst
= isl_upoly_as_cst(up
);
2011 isl_int_lcm(*d
, *d
, cst
->d
);
2015 rec
= isl_upoly_as_rec(up
);
2019 for (i
= 0; i
< rec
->n
; ++i
)
2020 upoly_update_den(rec
->p
[i
], d
);
2023 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
2025 isl_int_set_si(*d
, 1);
2028 upoly_update_den(qp
->upoly
, d
);
2031 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2032 __isl_take isl_space
*dim
, int pos
, int power
)
2034 struct isl_ctx
*ctx
;
2041 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
2044 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(__isl_take isl_space
*dim
,
2045 enum isl_dim_type type
, unsigned pos
)
2050 isl_assert(dim
->ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, goto error
);
2051 isl_assert(dim
->ctx
, pos
< isl_space_dim(dim
, type
), goto error
);
2053 if (type
== isl_dim_set
)
2054 pos
+= isl_space_dim(dim
, isl_dim_param
);
2056 return isl_qpolynomial_var_pow_on_domain(dim
, pos
, 1);
2058 isl_space_free(dim
);
2062 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2063 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2066 struct isl_upoly_rec
*rec
;
2067 struct isl_upoly
*base
, *res
;
2072 if (isl_upoly_is_cst(up
))
2075 if (up
->var
< first
)
2078 rec
= isl_upoly_as_rec(up
);
2082 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2084 if (up
->var
>= first
+ n
)
2085 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2087 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2089 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2090 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2091 struct isl_upoly
*t
;
2092 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2093 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2094 res
= isl_upoly_sum(res
, t
);
2097 isl_upoly_free(base
);
2106 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2107 isl_int denom
, unsigned len
)
2110 struct isl_upoly
*up
;
2112 isl_assert(ctx
, len
>= 1, return NULL
);
2114 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2115 for (i
= 0; i
< len
- 1; ++i
) {
2116 struct isl_upoly
*t
;
2117 struct isl_upoly
*c
;
2119 if (isl_int_is_zero(f
[1 + i
]))
2122 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2123 t
= isl_upoly_var_pow(ctx
, i
, 1);
2124 t
= isl_upoly_mul(c
, t
);
2125 up
= isl_upoly_sum(up
, t
);
2131 /* Remove common factor of non-constant terms and denominator.
2133 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2135 isl_ctx
*ctx
= qp
->div
->ctx
;
2136 unsigned total
= qp
->div
->n_col
- 2;
2138 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2139 isl_int_gcd(ctx
->normalize_gcd
,
2140 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2141 if (isl_int_is_one(ctx
->normalize_gcd
))
2144 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2145 ctx
->normalize_gcd
, total
);
2146 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2147 ctx
->normalize_gcd
);
2148 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2149 ctx
->normalize_gcd
);
2152 /* Replace the integer division identified by "div" by the polynomial "s".
2153 * The integer division is assumed not to appear in the definition
2154 * of any other integer divisions.
2156 static __isl_give isl_qpolynomial
*substitute_div(
2157 __isl_take isl_qpolynomial
*qp
,
2158 int div
, __isl_take
struct isl_upoly
*s
)
2167 qp
= isl_qpolynomial_cow(qp
);
2171 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2172 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2176 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2179 for (i
= 0; i
< total
+ div
; ++i
)
2181 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2182 reordering
[i
] = i
- 1;
2183 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2184 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2185 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2188 if (!qp
->upoly
|| !qp
->div
)
2194 isl_qpolynomial_free(qp
);
2199 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2200 * divisions because d is equal to 1 by their definition, i.e., e.
2202 static __isl_give isl_qpolynomial
*substitute_non_divs(
2203 __isl_take isl_qpolynomial
*qp
)
2207 struct isl_upoly
*s
;
2212 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2213 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2214 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2216 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2217 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2219 isl_seq_combine(qp
->div
->row
[j
] + 1,
2220 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2221 qp
->div
->row
[j
][2 + total
+ i
],
2222 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2223 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2224 normalize_div(qp
, j
);
2226 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2227 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2228 qp
= substitute_div(qp
, i
, s
);
2235 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2236 * with d the denominator. When replacing the coefficient e of x by
2237 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2238 * inside the division, so we need to add floor(e/d) * x outside.
2239 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2240 * to adjust the coefficient of x in each later div that depends on the
2241 * current div "div" and also in the affine expression "aff"
2242 * (if it too depends on "div").
2244 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2245 __isl_keep isl_vec
*aff
)
2249 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2252 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2253 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2254 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2256 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2257 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2258 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2259 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2260 isl_int_addmul(aff
->el
[i
], v
, aff
->el
[1 + total
+ div
]);
2261 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2262 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2264 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2265 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2271 /* Check if the last non-zero coefficient is bigger that half of the
2272 * denominator. If so, we will invert the div to further reduce the number
2273 * of distinct divs that may appear.
2274 * If the last non-zero coefficient is exactly half the denominator,
2275 * then we continue looking for earlier coefficients that are bigger
2276 * than half the denominator.
2278 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2283 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2284 if (isl_int_is_zero(div
->row
[row
][i
]))
2286 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2287 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2288 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2298 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2299 * We only invert the coefficients of e (and the coefficient of q in
2300 * later divs and in "aff"). After calling this function, the
2301 * coefficients of e should be reduced again.
2303 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2304 __isl_keep isl_vec
*aff
)
2306 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2308 isl_seq_neg(qp
->div
->row
[div
] + 1,
2309 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2310 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2311 isl_int_add(qp
->div
->row
[div
][1],
2312 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2313 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2314 isl_int_neg(aff
->el
[1 + total
+ div
], aff
->el
[1 + total
+ div
]);
2315 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2316 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2319 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2320 * in the interval [0, d-1], with d the denominator and such that the
2321 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2323 * After the reduction, some divs may have become redundant or identical,
2324 * so we call substitute_non_divs and sort_divs. If these functions
2325 * eliminate divs or merge two or more divs into one, the coefficients
2326 * of the enclosing divs may have to be reduced again, so we call
2327 * ourselves recursively if the number of divs decreases.
2329 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2332 isl_vec
*aff
= NULL
;
2333 struct isl_upoly
*s
;
2339 aff
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
2340 aff
= isl_vec_clr(aff
);
2344 isl_int_set_si(aff
->el
[1 + qp
->upoly
->var
], 1);
2346 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2347 normalize_div(qp
, i
);
2348 reduce_div(qp
, i
, aff
);
2349 if (needs_invert(qp
->div
, i
)) {
2350 invert_div(qp
, i
, aff
);
2351 reduce_div(qp
, i
, aff
);
2355 s
= isl_upoly_from_affine(qp
->div
->ctx
, aff
->el
,
2356 qp
->div
->ctx
->one
, aff
->size
);
2357 qp
->upoly
= isl_upoly_subs(qp
->upoly
, qp
->upoly
->var
, 1, &s
);
2364 n_div
= qp
->div
->n_row
;
2365 qp
= substitute_non_divs(qp
);
2367 if (qp
&& qp
->div
->n_row
< n_div
)
2368 return reduce_divs(qp
);
2372 isl_qpolynomial_free(qp
);
2377 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2378 __isl_take isl_space
*dim
, const isl_int n
, const isl_int d
)
2380 struct isl_qpolynomial
*qp
;
2381 struct isl_upoly_cst
*cst
;
2386 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2390 cst
= isl_upoly_as_cst(qp
->upoly
);
2391 isl_int_set(cst
->n
, n
);
2392 isl_int_set(cst
->d
, d
);
2397 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2399 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2400 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2402 isl_qpolynomial
*qp
;
2403 struct isl_upoly_cst
*cst
;
2405 if (!domain
|| !val
)
2408 qp
= isl_qpolynomial_alloc(isl_space_copy(domain
), 0,
2409 isl_upoly_zero(domain
->ctx
));
2413 cst
= isl_upoly_as_cst(qp
->upoly
);
2414 isl_int_set(cst
->n
, val
->n
);
2415 isl_int_set(cst
->d
, val
->d
);
2417 isl_space_free(domain
);
2421 isl_space_free(domain
);
2426 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2428 struct isl_upoly_rec
*rec
;
2434 if (isl_upoly_is_cst(up
))
2438 active
[up
->var
] = 1;
2440 rec
= isl_upoly_as_rec(up
);
2441 for (i
= 0; i
< rec
->n
; ++i
)
2442 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2448 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2451 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2456 for (i
= 0; i
< d
; ++i
)
2457 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2458 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2464 return up_set_active(qp
->upoly
, active
, d
);
2467 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2468 enum isl_dim_type type
, unsigned first
, unsigned n
)
2472 isl_bool involves
= isl_bool_false
;
2475 return isl_bool_error
;
2477 return isl_bool_false
;
2479 isl_assert(qp
->dim
->ctx
,
2480 first
+ n
<= isl_qpolynomial_dim(qp
, type
),
2481 return isl_bool_error
);
2482 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2483 type
== isl_dim_in
, return isl_bool_error
);
2485 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2486 isl_space_dim(qp
->dim
, isl_dim_all
));
2487 if (set_active(qp
, active
) < 0)
2490 if (type
== isl_dim_in
)
2491 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2492 for (i
= 0; i
< n
; ++i
)
2493 if (active
[first
+ i
]) {
2494 involves
= isl_bool_true
;
2503 return isl_bool_error
;
2506 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2507 * of the divs that do appear in the quasi-polynomial.
2509 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2510 __isl_take isl_qpolynomial
*qp
)
2517 int *reordering
= NULL
;
2524 if (qp
->div
->n_row
== 0)
2527 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2528 len
= qp
->div
->n_col
- 2;
2529 ctx
= isl_qpolynomial_get_ctx(qp
);
2530 active
= isl_calloc_array(ctx
, int, len
);
2534 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2537 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2538 if (!active
[d
+ i
]) {
2542 for (j
= 0; j
< i
; ++j
) {
2543 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2555 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2559 for (i
= 0; i
< d
; ++i
)
2563 n_div
= qp
->div
->n_row
;
2564 for (i
= 0; i
< n_div
; ++i
) {
2565 if (!active
[d
+ i
]) {
2566 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2567 qp
->div
= isl_mat_drop_cols(qp
->div
,
2568 2 + d
+ i
- skip
, 1);
2571 reordering
[d
+ i
] = d
+ i
- skip
;
2574 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2576 if (!qp
->upoly
|| !qp
->div
)
2586 isl_qpolynomial_free(qp
);
2590 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2591 unsigned first
, unsigned n
)
2594 struct isl_upoly_rec
*rec
;
2598 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2600 if (up
->var
< first
+ n
) {
2601 up
= replace_by_constant_term(up
);
2602 return isl_upoly_drop(up
, first
, n
);
2604 up
= isl_upoly_cow(up
);
2608 rec
= isl_upoly_as_rec(up
);
2612 for (i
= 0; i
< rec
->n
; ++i
) {
2613 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2624 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2625 __isl_take isl_qpolynomial
*qp
,
2626 enum isl_dim_type type
, unsigned pos
, const char *s
)
2628 qp
= isl_qpolynomial_cow(qp
);
2631 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2636 isl_qpolynomial_free(qp
);
2640 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2641 __isl_take isl_qpolynomial
*qp
,
2642 enum isl_dim_type type
, unsigned first
, unsigned n
)
2646 if (type
== isl_dim_out
)
2647 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2648 "cannot drop output/set dimension",
2650 if (type
== isl_dim_in
)
2652 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2655 qp
= isl_qpolynomial_cow(qp
);
2659 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2661 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2662 type
== isl_dim_set
, goto error
);
2664 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2668 if (type
== isl_dim_set
)
2669 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2671 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2675 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2681 isl_qpolynomial_free(qp
);
2685 /* Project the domain of the quasi-polynomial onto its parameter space.
2686 * The quasi-polynomial may not involve any of the domain dimensions.
2688 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2689 __isl_take isl_qpolynomial
*qp
)
2695 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2696 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2698 return isl_qpolynomial_free(qp
);
2700 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2701 "polynomial involves some of the domain dimensions",
2702 return isl_qpolynomial_free(qp
));
2703 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2704 space
= isl_qpolynomial_get_domain_space(qp
);
2705 space
= isl_space_params(space
);
2706 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2710 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2711 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2717 struct isl_upoly
*up
;
2721 if (eq
->n_eq
== 0) {
2722 isl_basic_set_free(eq
);
2726 qp
= isl_qpolynomial_cow(qp
);
2729 qp
->div
= isl_mat_cow(qp
->div
);
2733 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2735 isl_int_init(denom
);
2736 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2737 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2738 if (j
< 0 || j
== 0 || j
>= total
)
2741 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2742 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2744 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2745 &qp
->div
->row
[k
][0]);
2746 normalize_div(qp
, k
);
2749 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2750 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2751 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2752 isl_int_set_si(eq
->eq
[i
][j
], 0);
2754 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2755 eq
->eq
[i
], denom
, total
);
2756 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2759 isl_int_clear(denom
);
2764 isl_basic_set_free(eq
);
2766 qp
= substitute_non_divs(qp
);
2771 isl_basic_set_free(eq
);
2772 isl_qpolynomial_free(qp
);
2776 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2778 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2779 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2783 if (qp
->div
->n_row
> 0)
2784 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2785 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2787 isl_basic_set_free(eq
);
2788 isl_qpolynomial_free(qp
);
2792 static __isl_give isl_basic_set
*add_div_constraints(
2793 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2801 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2804 total
= isl_basic_set_total_dim(bset
);
2805 for (i
= 0; i
< div
->n_row
; ++i
)
2806 if (isl_basic_set_add_div_constraints_var(bset
,
2807 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2814 isl_basic_set_free(bset
);
2818 /* Look for equalities among the variables shared by context and qp
2819 * and the integer divisions of qp, if any.
2820 * The equalities are then used to eliminate variables and/or integer
2821 * divisions from qp.
2823 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2824 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2830 if (qp
->div
->n_row
> 0) {
2831 isl_basic_set
*bset
;
2832 context
= isl_set_add_dims(context
, isl_dim_set
,
2834 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2835 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2836 context
= isl_set_intersect(context
,
2837 isl_set_from_basic_set(bset
));
2840 aff
= isl_set_affine_hull(context
);
2841 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2843 isl_qpolynomial_free(qp
);
2844 isl_set_free(context
);
2848 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2849 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2851 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2852 isl_set
*dom_context
= isl_set_universe(space
);
2853 dom_context
= isl_set_intersect_params(dom_context
, context
);
2854 return isl_qpolynomial_gist(qp
, dom_context
);
2857 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2858 __isl_take isl_qpolynomial
*qp
)
2864 if (isl_qpolynomial_is_zero(qp
)) {
2865 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2866 isl_qpolynomial_free(qp
);
2867 return isl_pw_qpolynomial_zero(dim
);
2870 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2871 return isl_pw_qpolynomial_alloc(dom
, qp
);
2875 #define PW isl_pw_qpolynomial
2877 #define EL isl_qpolynomial
2879 #define EL_IS_ZERO is_zero
2883 #define IS_ZERO is_zero
2886 #undef DEFAULT_IS_ZERO
2887 #define DEFAULT_IS_ZERO 1
2891 #include <isl_pw_templ.c>
2894 #define UNION isl_union_pw_qpolynomial
2896 #define PART isl_pw_qpolynomial
2898 #define PARTS pw_qpolynomial
2900 #include <isl_union_single.c>
2901 #include <isl_union_eval.c>
2902 #include <isl_union_neg.c>
2904 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2912 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2915 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2918 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2919 __isl_take isl_pw_qpolynomial
*pwqp1
,
2920 __isl_take isl_pw_qpolynomial
*pwqp2
)
2922 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2925 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2926 __isl_take isl_pw_qpolynomial
*pwqp1
,
2927 __isl_take isl_pw_qpolynomial
*pwqp2
)
2930 struct isl_pw_qpolynomial
*res
;
2932 if (!pwqp1
|| !pwqp2
)
2935 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
2938 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
2939 isl_pw_qpolynomial_free(pwqp2
);
2943 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2944 isl_pw_qpolynomial_free(pwqp1
);
2948 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2949 isl_pw_qpolynomial_free(pwqp1
);
2953 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
2954 isl_pw_qpolynomial_free(pwqp2
);
2958 n
= pwqp1
->n
* pwqp2
->n
;
2959 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
2961 for (i
= 0; i
< pwqp1
->n
; ++i
) {
2962 for (j
= 0; j
< pwqp2
->n
; ++j
) {
2963 struct isl_set
*common
;
2964 struct isl_qpolynomial
*prod
;
2965 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
2966 isl_set_copy(pwqp2
->p
[j
].set
));
2967 if (isl_set_plain_is_empty(common
)) {
2968 isl_set_free(common
);
2972 prod
= isl_qpolynomial_mul(
2973 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
2974 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
2976 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
2980 isl_pw_qpolynomial_free(pwqp1
);
2981 isl_pw_qpolynomial_free(pwqp2
);
2985 isl_pw_qpolynomial_free(pwqp1
);
2986 isl_pw_qpolynomial_free(pwqp2
);
2990 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
2991 __isl_take isl_vec
*vec
)
2994 struct isl_upoly_rec
*rec
;
2998 if (isl_upoly_is_cst(up
)) {
3000 res
= isl_upoly_get_constant_val(up
);
3005 rec
= isl_upoly_as_rec(up
);
3009 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
3011 base
= isl_val_rat_from_isl_int(up
->ctx
,
3012 vec
->el
[1 + up
->var
], vec
->el
[0]);
3014 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
3017 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3018 res
= isl_val_mul(res
, isl_val_copy(base
));
3019 res
= isl_val_add(res
,
3020 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
3021 isl_vec_copy(vec
)));
3034 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3035 __isl_take isl_point
*pnt
)
3042 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3044 if (qp
->div
->n_row
== 0)
3045 ext
= isl_vec_copy(pnt
->vec
);
3048 unsigned dim
= isl_space_dim(qp
->dim
, isl_dim_all
);
3049 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
3053 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
3054 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
3055 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
3056 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
3057 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
3058 qp
->div
->row
[i
][0]);
3062 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
3064 isl_qpolynomial_free(qp
);
3065 isl_point_free(pnt
);
3069 isl_qpolynomial_free(qp
);
3070 isl_point_free(pnt
);
3074 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
3075 __isl_keep
struct isl_upoly_cst
*cst2
)
3080 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3081 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3082 cmp
= isl_int_sgn(t
);
3087 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3088 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3089 unsigned first
, unsigned n
)
3097 if (type
== isl_dim_out
)
3098 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3099 "cannot insert output/set dimensions",
3101 if (type
== isl_dim_in
)
3103 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3106 qp
= isl_qpolynomial_cow(qp
);
3110 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
3113 g_pos
= pos(qp
->dim
, type
) + first
;
3115 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3119 total
= qp
->div
->n_col
- 2;
3120 if (total
> g_pos
) {
3122 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3125 for (i
= 0; i
< total
- g_pos
; ++i
)
3127 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3133 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3139 isl_qpolynomial_free(qp
);
3143 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3144 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3148 pos
= isl_qpolynomial_dim(qp
, type
);
3150 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3153 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3154 __isl_take isl_pw_qpolynomial
*pwqp
,
3155 enum isl_dim_type type
, unsigned n
)
3159 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3161 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3164 static int *reordering_move(isl_ctx
*ctx
,
3165 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3170 reordering
= isl_alloc_array(ctx
, int, len
);
3175 for (i
= 0; i
< dst
; ++i
)
3177 for (i
= 0; i
< n
; ++i
)
3178 reordering
[src
+ i
] = dst
+ i
;
3179 for (i
= 0; i
< src
- dst
; ++i
)
3180 reordering
[dst
+ i
] = dst
+ n
+ i
;
3181 for (i
= 0; i
< len
- src
- n
; ++i
)
3182 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3184 for (i
= 0; i
< src
; ++i
)
3186 for (i
= 0; i
< n
; ++i
)
3187 reordering
[src
+ i
] = dst
+ i
;
3188 for (i
= 0; i
< dst
- src
; ++i
)
3189 reordering
[src
+ n
+ i
] = src
+ i
;
3190 for (i
= 0; i
< len
- dst
- n
; ++i
)
3191 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3197 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3198 __isl_take isl_qpolynomial
*qp
,
3199 enum isl_dim_type dst_type
, unsigned dst_pos
,
3200 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3209 qp
= isl_qpolynomial_cow(qp
);
3213 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3214 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3215 "cannot move output/set dimension",
3217 if (dst_type
== isl_dim_in
)
3218 dst_type
= isl_dim_set
;
3219 if (src_type
== isl_dim_in
)
3220 src_type
= isl_dim_set
;
3222 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3225 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3226 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3227 if (dst_type
> src_type
)
3230 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3237 reordering
= reordering_move(qp
->dim
->ctx
,
3238 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3242 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3247 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3253 isl_qpolynomial_free(qp
);
3257 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_space
*dim
,
3258 isl_int
*f
, isl_int denom
)
3260 struct isl_upoly
*up
;
3262 dim
= isl_space_domain(dim
);
3266 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
,
3267 1 + isl_space_dim(dim
, isl_dim_all
));
3269 return isl_qpolynomial_alloc(dim
, 0, up
);
3272 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3275 struct isl_upoly
*up
;
3276 isl_qpolynomial
*qp
;
3281 ctx
= isl_aff_get_ctx(aff
);
3282 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3285 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3286 aff
->ls
->div
->n_row
, up
);
3290 isl_mat_free(qp
->div
);
3291 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3292 qp
->div
= isl_mat_cow(qp
->div
);
3297 qp
= reduce_divs(qp
);
3298 qp
= remove_redundant_divs(qp
);
3302 return isl_qpolynomial_free(qp
);
3305 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3306 __isl_take isl_pw_aff
*pwaff
)
3309 isl_pw_qpolynomial
*pwqp
;
3314 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3317 for (i
= 0; i
< pwaff
->n
; ++i
) {
3319 isl_qpolynomial
*qp
;
3321 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3322 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3323 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3326 isl_pw_aff_free(pwaff
);
3330 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3331 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3335 aff
= isl_constraint_get_bound(c
, type
, pos
);
3336 isl_constraint_free(c
);
3337 return isl_qpolynomial_from_aff(aff
);
3340 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3341 * in "qp" by subs[i].
3343 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3344 __isl_take isl_qpolynomial
*qp
,
3345 enum isl_dim_type type
, unsigned first
, unsigned n
,
3346 __isl_keep isl_qpolynomial
**subs
)
3349 struct isl_upoly
**ups
;
3354 qp
= isl_qpolynomial_cow(qp
);
3358 if (type
== isl_dim_out
)
3359 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3360 "cannot substitute output/set dimension",
3362 if (type
== isl_dim_in
)
3365 for (i
= 0; i
< n
; ++i
)
3369 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3372 for (i
= 0; i
< n
; ++i
)
3373 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3376 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3377 for (i
= 0; i
< n
; ++i
)
3378 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3380 first
+= pos(qp
->dim
, type
);
3382 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3385 for (i
= 0; i
< n
; ++i
)
3386 ups
[i
] = subs
[i
]->upoly
;
3388 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3397 isl_qpolynomial_free(qp
);
3401 /* Extend "bset" with extra set dimensions for each integer division
3402 * in "qp" and then call "fn" with the extended bset and the polynomial
3403 * that results from replacing each of the integer divisions by the
3404 * corresponding extra set dimension.
3406 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3407 __isl_keep isl_basic_set
*bset
,
3408 int (*fn
)(__isl_take isl_basic_set
*bset
,
3409 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3413 isl_qpolynomial
*poly
;
3417 if (qp
->div
->n_row
== 0)
3418 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3421 div
= isl_mat_copy(qp
->div
);
3422 dim
= isl_space_copy(qp
->dim
);
3423 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3424 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3425 bset
= isl_basic_set_copy(bset
);
3426 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, qp
->div
->n_row
);
3427 bset
= add_div_constraints(bset
, div
);
3429 return fn(bset
, poly
, user
);
3434 /* Return total degree in variables first (inclusive) up to last (exclusive).
3436 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3440 struct isl_upoly_rec
*rec
;
3444 if (isl_upoly_is_zero(up
))
3446 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3449 rec
= isl_upoly_as_rec(up
);
3453 for (i
= 0; i
< rec
->n
; ++i
) {
3456 if (isl_upoly_is_zero(rec
->p
[i
]))
3458 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3468 /* Return total degree in set variables.
3470 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3478 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3479 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3480 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3483 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3484 unsigned pos
, int deg
)
3487 struct isl_upoly_rec
*rec
;
3492 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3494 return isl_upoly_copy(up
);
3496 return isl_upoly_zero(up
->ctx
);
3499 rec
= isl_upoly_as_rec(up
);
3503 if (up
->var
== pos
) {
3505 return isl_upoly_copy(rec
->p
[deg
]);
3507 return isl_upoly_zero(up
->ctx
);
3510 up
= isl_upoly_copy(up
);
3511 up
= isl_upoly_cow(up
);
3512 rec
= isl_upoly_as_rec(up
);
3516 for (i
= 0; i
< rec
->n
; ++i
) {
3517 struct isl_upoly
*t
;
3518 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3521 isl_upoly_free(rec
->p
[i
]);
3531 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3533 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3534 __isl_keep isl_qpolynomial
*qp
,
3535 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3538 struct isl_upoly
*up
;
3544 if (type
== isl_dim_out
)
3545 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3546 "output/set dimension does not have a coefficient",
3548 if (type
== isl_dim_in
)
3551 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3554 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3555 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3557 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3560 isl_mat_free(c
->div
);
3561 c
->div
= isl_mat_copy(qp
->div
);
3566 isl_qpolynomial_free(c
);
3570 /* Homogenize the polynomial in the variables first (inclusive) up to
3571 * last (exclusive) by inserting powers of variable first.
3572 * Variable first is assumed not to appear in the input.
3574 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3575 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3576 int first
, int last
)
3579 struct isl_upoly_rec
*rec
;
3583 if (isl_upoly_is_zero(up
))
3587 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3588 struct isl_upoly
*hom
;
3590 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3593 rec
= isl_upoly_as_rec(hom
);
3594 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3599 up
= isl_upoly_cow(up
);
3600 rec
= isl_upoly_as_rec(up
);
3604 for (i
= 0; i
< rec
->n
; ++i
) {
3605 if (isl_upoly_is_zero(rec
->p
[i
]))
3607 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3608 up
->var
< last
? deg
+ i
: i
, target
,
3620 /* Homogenize the polynomial in the set variables by introducing
3621 * powers of an extra set variable at position 0.
3623 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3624 __isl_take isl_qpolynomial
*poly
)
3628 int deg
= isl_qpolynomial_degree(poly
);
3633 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3634 poly
= isl_qpolynomial_cow(poly
);
3638 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3639 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3640 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3647 isl_qpolynomial_free(poly
);
3651 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*dim
,
3652 __isl_take isl_mat
*div
)
3660 n
= isl_space_dim(dim
, isl_dim_all
) + div
->n_row
;
3662 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3663 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3670 isl_int_init(term
->n
);
3671 isl_int_init(term
->d
);
3675 isl_space_free(dim
);
3680 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3689 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3698 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3700 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3704 isl_int_set(dup
->n
, term
->n
);
3705 isl_int_set(dup
->d
, term
->d
);
3707 for (i
= 0; i
< total
; ++i
)
3708 dup
->pow
[i
] = term
->pow
[i
];
3713 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3721 return isl_term_dup(term
);
3724 void isl_term_free(__isl_take isl_term
*term
)
3729 if (--term
->ref
> 0)
3732 isl_space_free(term
->dim
);
3733 isl_mat_free(term
->div
);
3734 isl_int_clear(term
->n
);
3735 isl_int_clear(term
->d
);
3739 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3747 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3748 case isl_dim_div
: return term
->div
->n_row
;
3749 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3755 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3757 return term
? term
->dim
->ctx
: NULL
;
3760 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3764 isl_int_set(*n
, term
->n
);
3767 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
3771 isl_int_set(*d
, term
->d
);
3774 /* Return the coefficient of the term "term".
3776 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3781 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3785 int isl_term_get_exp(__isl_keep isl_term
*term
,
3786 enum isl_dim_type type
, unsigned pos
)
3791 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3793 if (type
>= isl_dim_set
)
3794 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3795 if (type
>= isl_dim_div
)
3796 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3798 return term
->pow
[pos
];
3801 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3803 isl_local_space
*ls
;
3809 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3812 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3813 isl_mat_copy(term
->div
));
3814 aff
= isl_aff_alloc(ls
);
3818 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3820 aff
= isl_aff_normalize(aff
);
3825 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3826 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3827 __isl_take isl_term
*term
, void *user
)
3830 struct isl_upoly_rec
*rec
;
3835 if (isl_upoly_is_zero(up
))
3838 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3839 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3840 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3842 if (isl_upoly_is_cst(up
)) {
3843 struct isl_upoly_cst
*cst
;
3844 cst
= isl_upoly_as_cst(up
);
3847 term
= isl_term_cow(term
);
3850 isl_int_set(term
->n
, cst
->n
);
3851 isl_int_set(term
->d
, cst
->d
);
3852 if (fn(isl_term_copy(term
), user
) < 0)
3857 rec
= isl_upoly_as_rec(up
);
3861 for (i
= 0; i
< rec
->n
; ++i
) {
3862 term
= isl_term_cow(term
);
3865 term
->pow
[up
->var
] = i
;
3866 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3870 term
->pow
[up
->var
] = 0;
3874 isl_term_free(term
);
3878 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3879 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3884 return isl_stat_error
;
3886 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3888 return isl_stat_error
;
3890 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3892 isl_term_free(term
);
3894 return term
? isl_stat_ok
: isl_stat_error
;
3897 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3899 struct isl_upoly
*up
;
3900 isl_qpolynomial
*qp
;
3906 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3908 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3909 for (i
= 0; i
< n
; ++i
) {
3912 up
= isl_upoly_mul(up
,
3913 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3916 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
3919 isl_mat_free(qp
->div
);
3920 qp
->div
= isl_mat_copy(term
->div
);
3924 isl_term_free(term
);
3927 isl_qpolynomial_free(qp
);
3928 isl_term_free(term
);
3932 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3933 __isl_take isl_space
*dim
)
3942 if (isl_space_is_equal(qp
->dim
, dim
)) {
3943 isl_space_free(dim
);
3947 qp
= isl_qpolynomial_cow(qp
);
3951 extra
= isl_space_dim(dim
, isl_dim_set
) -
3952 isl_space_dim(qp
->dim
, isl_dim_set
);
3953 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
3954 if (qp
->div
->n_row
) {
3957 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
3960 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3962 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
3967 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
3970 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3971 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
3973 isl_space_free(qp
->dim
);
3978 isl_space_free(dim
);
3979 isl_qpolynomial_free(qp
);
3983 /* For each parameter or variable that does not appear in qp,
3984 * first eliminate the variable from all constraints and then set it to zero.
3986 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3987 __isl_keep isl_qpolynomial
*qp
)
3998 d
= isl_space_dim(set
->dim
, isl_dim_all
);
3999 active
= isl_calloc_array(set
->ctx
, int, d
);
4000 if (set_active(qp
, active
) < 0)
4003 for (i
= 0; i
< d
; ++i
)
4012 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4013 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4014 for (i
= 0; i
< nparam
; ++i
) {
4017 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4018 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4020 for (i
= 0; i
< nvar
; ++i
) {
4021 if (active
[nparam
+ i
])
4023 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4024 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4036 struct isl_opt_data
{
4037 isl_qpolynomial
*qp
;
4043 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4045 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4048 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4052 } else if (data
->max
) {
4053 data
->opt
= isl_val_max(data
->opt
, val
);
4055 data
->opt
= isl_val_min(data
->opt
, val
);
4061 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4062 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4064 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4069 if (isl_upoly_is_cst(qp
->upoly
)) {
4071 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4072 isl_qpolynomial_free(qp
);
4076 set
= fix_inactive(set
, qp
);
4079 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4083 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4086 isl_qpolynomial_free(qp
);
4090 isl_qpolynomial_free(qp
);
4091 isl_val_free(data
.opt
);
4095 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4096 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4101 struct isl_upoly
**subs
;
4102 isl_mat
*mat
, *diag
;
4104 qp
= isl_qpolynomial_cow(qp
);
4109 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4111 n_sub
= morph
->inv
->n_row
- 1;
4112 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4113 n_sub
+= qp
->div
->n_row
;
4114 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4118 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4119 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4120 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4121 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4122 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4123 subs
[morph
->inv
->n_row
- 1 + i
] =
4124 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4126 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4128 for (i
= 0; i
< n_sub
; ++i
)
4129 isl_upoly_free(subs
[i
]);
4132 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4133 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4134 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4135 mat
= isl_mat_diagonal(mat
, diag
);
4136 qp
->div
= isl_mat_product(qp
->div
, mat
);
4137 isl_space_free(qp
->dim
);
4138 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4140 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4143 isl_morph_free(morph
);
4147 isl_qpolynomial_free(qp
);
4148 isl_morph_free(morph
);
4152 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4153 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4154 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4156 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4157 &isl_pw_qpolynomial_mul
);
4160 /* Reorder the columns of the given div definitions according to the
4163 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
4164 __isl_take isl_reordering
*r
)
4173 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
4174 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
4178 for (i
= 0; i
< div
->n_row
; ++i
) {
4179 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
4180 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
4181 for (j
= 0; j
< r
->len
; ++j
)
4182 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
4183 div
->row
[i
][2 + j
]);
4186 isl_reordering_free(r
);
4190 isl_reordering_free(r
);
4195 /* Reorder the dimension of "qp" according to the given reordering.
4197 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4198 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4200 qp
= isl_qpolynomial_cow(qp
);
4204 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4208 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
4212 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4216 qp
= isl_qpolynomial_reset_domain_space(qp
, isl_space_copy(r
->dim
));
4218 isl_reordering_free(r
);
4221 isl_qpolynomial_free(qp
);
4222 isl_reordering_free(r
);
4226 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4227 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4232 if (!isl_space_match(qp
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
4233 isl_reordering
*exp
;
4235 model
= isl_space_drop_dims(model
, isl_dim_in
,
4236 0, isl_space_dim(model
, isl_dim_in
));
4237 model
= isl_space_drop_dims(model
, isl_dim_out
,
4238 0, isl_space_dim(model
, isl_dim_out
));
4239 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4240 exp
= isl_reordering_extend_space(exp
,
4241 isl_qpolynomial_get_domain_space(qp
));
4242 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4245 isl_space_free(model
);
4248 isl_space_free(model
);
4249 isl_qpolynomial_free(qp
);
4253 struct isl_split_periods_data
{
4255 isl_pw_qpolynomial
*res
;
4258 /* Create a slice where the integer division "div" has the fixed value "v".
4259 * In particular, if "div" refers to floor(f/m), then create a slice
4261 * m v <= f <= m v + (m - 1)
4266 * -f + m v + (m - 1) >= 0
4268 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*dim
,
4269 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4272 isl_basic_set
*bset
= NULL
;
4278 total
= isl_space_dim(dim
, isl_dim_all
);
4279 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0, 0, 2);
4281 k
= isl_basic_set_alloc_inequality(bset
);
4284 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4285 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4287 k
= isl_basic_set_alloc_inequality(bset
);
4290 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4291 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4292 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4293 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4295 isl_space_free(dim
);
4296 return isl_set_from_basic_set(bset
);
4298 isl_basic_set_free(bset
);
4299 isl_space_free(dim
);
4303 static isl_stat
split_periods(__isl_take isl_set
*set
,
4304 __isl_take isl_qpolynomial
*qp
, void *user
);
4306 /* Create a slice of the domain "set" such that integer division "div"
4307 * has the fixed value "v" and add the results to data->res,
4308 * replacing the integer division by "v" in "qp".
4310 static isl_stat
set_div(__isl_take isl_set
*set
,
4311 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4312 struct isl_split_periods_data
*data
)
4317 struct isl_upoly
*cst
;
4319 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4320 set
= isl_set_intersect(set
, slice
);
4325 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4327 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4328 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4330 isl_int_addmul(qp
->div
->row
[i
][1],
4331 qp
->div
->row
[i
][2 + total
+ div
], v
);
4332 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4335 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4336 qp
= substitute_div(qp
, div
, cst
);
4338 return split_periods(set
, qp
, data
);
4341 isl_qpolynomial_free(qp
);
4345 /* Split the domain "set" such that integer division "div"
4346 * has a fixed value (ranging from "min" to "max") on each slice
4347 * and add the results to data->res.
4349 static isl_stat
split_div(__isl_take isl_set
*set
,
4350 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4351 struct isl_split_periods_data
*data
)
4353 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4354 isl_set
*set_i
= isl_set_copy(set
);
4355 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4357 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4361 isl_qpolynomial_free(qp
);
4365 isl_qpolynomial_free(qp
);
4366 return isl_stat_error
;
4369 /* If "qp" refers to any integer division
4370 * that can only attain "max_periods" distinct values on "set"
4371 * then split the domain along those distinct values.
4372 * Add the results (or the original if no splitting occurs)
4375 static isl_stat
split_periods(__isl_take isl_set
*set
,
4376 __isl_take isl_qpolynomial
*qp
, void *user
)
4379 isl_pw_qpolynomial
*pwqp
;
4380 struct isl_split_periods_data
*data
;
4383 isl_stat r
= isl_stat_ok
;
4385 data
= (struct isl_split_periods_data
*)user
;
4390 if (qp
->div
->n_row
== 0) {
4391 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4392 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4398 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4399 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4400 enum isl_lp_result lp_res
;
4402 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4403 qp
->div
->n_row
) != -1)
4406 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4407 set
->ctx
->one
, &min
, NULL
, NULL
);
4408 if (lp_res
== isl_lp_error
)
4410 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4412 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4414 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4415 set
->ctx
->one
, &max
, NULL
, NULL
);
4416 if (lp_res
== isl_lp_error
)
4418 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4420 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4422 isl_int_sub(max
, max
, min
);
4423 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4424 isl_int_add(max
, max
, min
);
4429 if (i
< qp
->div
->n_row
) {
4430 r
= split_div(set
, qp
, i
, min
, max
, data
);
4432 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4433 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4445 isl_qpolynomial_free(qp
);
4446 return isl_stat_error
;
4449 /* If any quasi-polynomial in pwqp refers to any integer division
4450 * that can only attain "max_periods" distinct values on its domain
4451 * then split the domain along those distinct values.
4453 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4454 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4456 struct isl_split_periods_data data
;
4458 data
.max_periods
= max_periods
;
4459 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4461 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4464 isl_pw_qpolynomial_free(pwqp
);
4468 isl_pw_qpolynomial_free(data
.res
);
4469 isl_pw_qpolynomial_free(pwqp
);
4473 /* Construct a piecewise quasipolynomial that is constant on the given
4474 * domain. In particular, it is
4477 * infinity if cst == -1
4479 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4480 __isl_take isl_basic_set
*bset
, int cst
)
4483 isl_qpolynomial
*qp
;
4488 bset
= isl_basic_set_params(bset
);
4489 dim
= isl_basic_set_get_space(bset
);
4491 qp
= isl_qpolynomial_infty_on_domain(dim
);
4493 qp
= isl_qpolynomial_zero_on_domain(dim
);
4495 qp
= isl_qpolynomial_one_on_domain(dim
);
4496 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4499 /* Factor bset, call fn on each of the factors and return the product.
4501 * If no factors can be found, simply call fn on the input.
4502 * Otherwise, construct the factors based on the factorizer,
4503 * call fn on each factor and compute the product.
4505 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4506 __isl_take isl_basic_set
*bset
,
4507 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4513 isl_qpolynomial
*qp
;
4514 isl_pw_qpolynomial
*pwqp
;
4518 f
= isl_basic_set_factorizer(bset
);
4521 if (f
->n_group
== 0) {
4522 isl_factorizer_free(f
);
4526 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4527 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4529 dim
= isl_basic_set_get_space(bset
);
4530 dim
= isl_space_domain(dim
);
4531 set
= isl_set_universe(isl_space_copy(dim
));
4532 qp
= isl_qpolynomial_one_on_domain(dim
);
4533 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4535 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4537 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4538 isl_basic_set
*bset_i
;
4539 isl_pw_qpolynomial
*pwqp_i
;
4541 bset_i
= isl_basic_set_copy(bset
);
4542 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4543 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4544 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4546 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4547 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4548 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4550 pwqp_i
= fn(bset_i
);
4551 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4556 isl_basic_set_free(bset
);
4557 isl_factorizer_free(f
);
4561 isl_basic_set_free(bset
);
4565 /* Factor bset, call fn on each of the factors and return the product.
4566 * The function is assumed to evaluate to zero on empty domains,
4567 * to one on zero-dimensional domains and to infinity on unbounded domains
4568 * and will not be called explicitly on zero-dimensional or unbounded domains.
4570 * We first check for some special cases and remove all equalities.
4571 * Then we hand over control to compressed_multiplicative_call.
4573 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4574 __isl_take isl_basic_set
*bset
,
4575 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4579 isl_pw_qpolynomial
*pwqp
;
4584 if (isl_basic_set_plain_is_empty(bset
))
4585 return constant_on_domain(bset
, 0);
4587 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4588 return constant_on_domain(bset
, 1);
4590 bounded
= isl_basic_set_is_bounded(bset
);
4594 return constant_on_domain(bset
, -1);
4596 if (bset
->n_eq
== 0)
4597 return compressed_multiplicative_call(bset
, fn
);
4599 morph
= isl_basic_set_full_compression(bset
);
4600 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4602 pwqp
= compressed_multiplicative_call(bset
, fn
);
4604 morph
= isl_morph_dom_params(morph
);
4605 morph
= isl_morph_ran_params(morph
);
4606 morph
= isl_morph_inverse(morph
);
4608 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4612 isl_basic_set_free(bset
);
4616 /* Drop all floors in "qp", turning each integer division [a/m] into
4617 * a rational division a/m. If "down" is set, then the integer division
4618 * is replaced by (a-(m-1))/m instead.
4620 static __isl_give isl_qpolynomial
*qp_drop_floors(
4621 __isl_take isl_qpolynomial
*qp
, int down
)
4624 struct isl_upoly
*s
;
4628 if (qp
->div
->n_row
== 0)
4631 qp
= isl_qpolynomial_cow(qp
);
4635 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4637 isl_int_sub(qp
->div
->row
[i
][1],
4638 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4639 isl_int_add_ui(qp
->div
->row
[i
][1],
4640 qp
->div
->row
[i
][1], 1);
4642 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4643 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4644 qp
= substitute_div(qp
, i
, s
);
4652 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4653 * a rational division a/m.
4655 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4656 __isl_take isl_pw_qpolynomial
*pwqp
)
4663 if (isl_pw_qpolynomial_is_zero(pwqp
))
4666 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4670 for (i
= 0; i
< pwqp
->n
; ++i
) {
4671 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4678 isl_pw_qpolynomial_free(pwqp
);
4682 /* Adjust all the integer divisions in "qp" such that they are at least
4683 * one over the given orthant (identified by "signs"). This ensures
4684 * that they will still be non-negative even after subtracting (m-1)/m.
4686 * In particular, f is replaced by f' + v, changing f = [a/m]
4687 * to f' = [(a - m v)/m].
4688 * If the constant term k in a is smaller than m,
4689 * the constant term of v is set to floor(k/m) - 1.
4690 * For any other term, if the coefficient c and the variable x have
4691 * the same sign, then no changes are needed.
4692 * Otherwise, if the variable is positive (and c is negative),
4693 * then the coefficient of x in v is set to floor(c/m).
4694 * If the variable is negative (and c is positive),
4695 * then the coefficient of x in v is set to ceil(c/m).
4697 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4703 struct isl_upoly
*s
;
4705 qp
= isl_qpolynomial_cow(qp
);
4708 qp
->div
= isl_mat_cow(qp
->div
);
4712 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4713 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4715 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4716 isl_int
*row
= qp
->div
->row
[i
];
4720 if (isl_int_lt(row
[1], row
[0])) {
4721 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4722 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4723 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4725 for (j
= 0; j
< total
; ++j
) {
4726 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4729 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4731 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4732 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4734 for (j
= 0; j
< i
; ++j
) {
4735 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4737 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4738 row
[2 + total
+ j
], row
[0]);
4739 isl_int_submul(row
[2 + total
+ j
],
4740 row
[0], v
->el
[1 + total
+ j
]);
4742 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4743 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4745 isl_seq_combine(qp
->div
->row
[j
] + 1,
4746 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4747 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4749 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4750 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4751 qp
->div
->ctx
->one
, v
->size
);
4752 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4762 isl_qpolynomial_free(qp
);
4766 struct isl_to_poly_data
{
4768 isl_pw_qpolynomial
*res
;
4769 isl_qpolynomial
*qp
;
4772 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4773 * We first make all integer divisions positive and then split the
4774 * quasipolynomials into terms with sign data->sign (the direction
4775 * of the requested approximation) and terms with the opposite sign.
4776 * In the first set of terms, each integer division [a/m] is
4777 * overapproximated by a/m, while in the second it is underapproximated
4780 static int to_polynomial_on_orthant(__isl_take isl_set
*orthant
, int *signs
,
4783 struct isl_to_poly_data
*data
= user
;
4784 isl_pw_qpolynomial
*t
;
4785 isl_qpolynomial
*qp
, *up
, *down
;
4787 qp
= isl_qpolynomial_copy(data
->qp
);
4788 qp
= make_divs_pos(qp
, signs
);
4790 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4791 up
= qp_drop_floors(up
, 0);
4792 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4793 down
= qp_drop_floors(down
, 1);
4795 isl_qpolynomial_free(qp
);
4796 qp
= isl_qpolynomial_add(up
, down
);
4798 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4799 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4804 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4805 * the polynomial will be an overapproximation. If "sign" is negative,
4806 * it will be an underapproximation. If "sign" is zero, the approximation
4807 * will lie somewhere in between.
4809 * In particular, is sign == 0, we simply drop the floors, turning
4810 * the integer divisions into rational divisions.
4811 * Otherwise, we split the domains into orthants, make all integer divisions
4812 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4813 * depending on the requested sign and the sign of the term in which
4814 * the integer division appears.
4816 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4817 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4820 struct isl_to_poly_data data
;
4823 return pwqp_drop_floors(pwqp
);
4829 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4831 for (i
= 0; i
< pwqp
->n
; ++i
) {
4832 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4833 isl_pw_qpolynomial
*t
;
4834 t
= isl_pw_qpolynomial_alloc(
4835 isl_set_copy(pwqp
->p
[i
].set
),
4836 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4837 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4840 data
.qp
= pwqp
->p
[i
].qp
;
4841 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4842 &to_polynomial_on_orthant
, &data
) < 0)
4846 isl_pw_qpolynomial_free(pwqp
);
4850 isl_pw_qpolynomial_free(pwqp
);
4851 isl_pw_qpolynomial_free(data
.res
);
4855 static __isl_give isl_pw_qpolynomial
*poly_entry(
4856 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4860 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4863 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4864 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4866 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4867 &poly_entry
, &sign
);
4870 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4871 __isl_take isl_qpolynomial
*qp
)
4875 isl_vec
*aff
= NULL
;
4876 isl_basic_map
*bmap
= NULL
;
4882 if (!isl_upoly_is_affine(qp
->upoly
))
4883 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4884 "input quasi-polynomial not affine", goto error
);
4885 aff
= isl_qpolynomial_extract_affine(qp
);
4888 dim
= isl_qpolynomial_get_space(qp
);
4889 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4890 n_div
= qp
->div
->n_row
;
4891 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4893 for (i
= 0; i
< n_div
; ++i
) {
4894 k
= isl_basic_map_alloc_div(bmap
);
4897 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4898 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4899 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4902 k
= isl_basic_map_alloc_equality(bmap
);
4905 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4906 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4907 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4910 isl_qpolynomial_free(qp
);
4911 bmap
= isl_basic_map_finalize(bmap
);
4915 isl_qpolynomial_free(qp
);
4916 isl_basic_map_free(bmap
);