2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 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>
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_range.h>
25 #include <isl_local_space_private.h>
26 #include <isl_aff_private.h>
27 #include <isl_config.h>
29 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
32 case isl_dim_param
: return 0;
33 case isl_dim_in
: return dim
->nparam
;
34 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
39 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
47 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
52 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
54 return (struct isl_upoly_cst
*)up
;
57 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
62 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
64 return (struct isl_upoly_rec
*)up
;
67 int isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
68 __isl_keep
struct isl_upoly
*up2
)
71 struct isl_upoly_rec
*rec1
, *rec2
;
77 if (up1
->var
!= up2
->var
)
79 if (isl_upoly_is_cst(up1
)) {
80 struct isl_upoly_cst
*cst1
, *cst2
;
81 cst1
= isl_upoly_as_cst(up1
);
82 cst2
= isl_upoly_as_cst(up2
);
85 return isl_int_eq(cst1
->n
, cst2
->n
) &&
86 isl_int_eq(cst1
->d
, cst2
->d
);
89 rec1
= isl_upoly_as_rec(up1
);
90 rec2
= isl_upoly_as_rec(up2
);
94 if (rec1
->n
!= rec2
->n
)
97 for (i
= 0; i
< rec1
->n
; ++i
) {
98 int eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
106 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
108 struct isl_upoly_cst
*cst
;
112 if (!isl_upoly_is_cst(up
))
115 cst
= isl_upoly_as_cst(up
);
119 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
122 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
124 struct isl_upoly_cst
*cst
;
128 if (!isl_upoly_is_cst(up
))
131 cst
= isl_upoly_as_cst(up
);
135 return isl_int_sgn(cst
->n
);
138 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
140 struct isl_upoly_cst
*cst
;
144 if (!isl_upoly_is_cst(up
))
147 cst
= isl_upoly_as_cst(up
);
151 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
154 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
156 struct isl_upoly_cst
*cst
;
160 if (!isl_upoly_is_cst(up
))
163 cst
= isl_upoly_as_cst(up
);
167 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
170 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
172 struct isl_upoly_cst
*cst
;
176 if (!isl_upoly_is_cst(up
))
179 cst
= isl_upoly_as_cst(up
);
183 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
186 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
188 struct isl_upoly_cst
*cst
;
192 if (!isl_upoly_is_cst(up
))
195 cst
= isl_upoly_as_cst(up
);
199 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
202 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
204 struct isl_upoly_cst
*cst
;
208 if (!isl_upoly_is_cst(up
))
211 cst
= isl_upoly_as_cst(up
);
215 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
218 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
220 struct isl_upoly_cst
*cst
;
222 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
231 isl_int_init(cst
->n
);
232 isl_int_init(cst
->d
);
237 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
239 struct isl_upoly_cst
*cst
;
241 cst
= isl_upoly_cst_alloc(ctx
);
245 isl_int_set_si(cst
->n
, 0);
246 isl_int_set_si(cst
->d
, 1);
251 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
253 struct isl_upoly_cst
*cst
;
255 cst
= isl_upoly_cst_alloc(ctx
);
259 isl_int_set_si(cst
->n
, 1);
260 isl_int_set_si(cst
->d
, 1);
265 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
267 struct isl_upoly_cst
*cst
;
269 cst
= isl_upoly_cst_alloc(ctx
);
273 isl_int_set_si(cst
->n
, 1);
274 isl_int_set_si(cst
->d
, 0);
279 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
281 struct isl_upoly_cst
*cst
;
283 cst
= isl_upoly_cst_alloc(ctx
);
287 isl_int_set_si(cst
->n
, -1);
288 isl_int_set_si(cst
->d
, 0);
293 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
295 struct isl_upoly_cst
*cst
;
297 cst
= isl_upoly_cst_alloc(ctx
);
301 isl_int_set_si(cst
->n
, 0);
302 isl_int_set_si(cst
->d
, 0);
307 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
308 isl_int n
, isl_int d
)
310 struct isl_upoly_cst
*cst
;
312 cst
= isl_upoly_cst_alloc(ctx
);
316 isl_int_set(cst
->n
, n
);
317 isl_int_set(cst
->d
, d
);
322 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
325 struct isl_upoly_rec
*rec
;
327 isl_assert(ctx
, var
>= 0, return NULL
);
328 isl_assert(ctx
, size
>= 0, return NULL
);
329 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
330 sizeof(struct isl_upoly_rec
) +
331 size
* sizeof(struct isl_upoly
*));
346 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
347 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
349 qp
= isl_qpolynomial_cow(qp
);
353 isl_space_free(qp
->dim
);
358 isl_qpolynomial_free(qp
);
363 /* Reset the space of "qp". This function is called from isl_pw_templ.c
364 * and doesn't know if the space of an element object is represented
365 * directly or through its domain. It therefore passes along both.
367 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
368 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
369 __isl_take isl_space
*domain
)
371 isl_space_free(space
);
372 return isl_qpolynomial_reset_domain_space(qp
, domain
);
375 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
377 return qp
? qp
->dim
->ctx
: NULL
;
380 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
381 __isl_keep isl_qpolynomial
*qp
)
383 return qp
? isl_space_copy(qp
->dim
) : NULL
;
386 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
391 space
= isl_space_copy(qp
->dim
);
392 space
= isl_space_from_domain(space
);
393 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
397 /* Externally, an isl_qpolynomial has a map space, but internally, the
398 * ls field corresponds to the domain of that space.
400 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
401 enum isl_dim_type type
)
405 if (type
== isl_dim_out
)
407 if (type
== isl_dim_in
)
409 return isl_space_dim(qp
->dim
, type
);
412 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
414 return qp
? isl_upoly_is_zero(qp
->upoly
) : -1;
417 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
419 return qp
? isl_upoly_is_one(qp
->upoly
) : -1;
422 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
424 return qp
? isl_upoly_is_nan(qp
->upoly
) : -1;
427 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
429 return qp
? isl_upoly_is_infty(qp
->upoly
) : -1;
432 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
434 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : -1;
437 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
439 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
442 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
444 isl_int_clear(cst
->n
);
445 isl_int_clear(cst
->d
);
448 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
452 for (i
= 0; i
< rec
->n
; ++i
)
453 isl_upoly_free(rec
->p
[i
]);
456 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
465 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
467 struct isl_upoly_cst
*cst
;
468 struct isl_upoly_cst
*dup
;
470 cst
= isl_upoly_as_cst(up
);
474 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
477 isl_int_set(dup
->n
, cst
->n
);
478 isl_int_set(dup
->d
, cst
->d
);
483 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
486 struct isl_upoly_rec
*rec
;
487 struct isl_upoly_rec
*dup
;
489 rec
= isl_upoly_as_rec(up
);
493 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
497 for (i
= 0; i
< rec
->n
; ++i
) {
498 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
506 isl_upoly_free(&dup
->up
);
510 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
515 if (isl_upoly_is_cst(up
))
516 return isl_upoly_dup_cst(up
);
518 return isl_upoly_dup_rec(up
);
521 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
529 return isl_upoly_dup(up
);
532 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
541 upoly_free_cst((struct isl_upoly_cst
*)up
);
543 upoly_free_rec((struct isl_upoly_rec
*)up
);
545 isl_ctx_deref(up
->ctx
);
549 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
554 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
555 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
556 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
557 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
562 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
563 __isl_take
struct isl_upoly
*up2
)
565 struct isl_upoly_cst
*cst1
;
566 struct isl_upoly_cst
*cst2
;
568 up1
= isl_upoly_cow(up1
);
572 cst1
= isl_upoly_as_cst(up1
);
573 cst2
= isl_upoly_as_cst(up2
);
575 if (isl_int_eq(cst1
->d
, cst2
->d
))
576 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
578 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
579 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
580 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
583 isl_upoly_cst_reduce(cst1
);
593 static __isl_give
struct isl_upoly
*replace_by_zero(
594 __isl_take
struct isl_upoly
*up
)
602 return isl_upoly_zero(ctx
);
605 static __isl_give
struct isl_upoly
*replace_by_constant_term(
606 __isl_take
struct isl_upoly
*up
)
608 struct isl_upoly_rec
*rec
;
609 struct isl_upoly
*cst
;
614 rec
= isl_upoly_as_rec(up
);
617 cst
= isl_upoly_copy(rec
->p
[0]);
625 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
626 __isl_take
struct isl_upoly
*up2
)
629 struct isl_upoly_rec
*rec1
, *rec2
;
634 if (isl_upoly_is_nan(up1
)) {
639 if (isl_upoly_is_nan(up2
)) {
644 if (isl_upoly_is_zero(up1
)) {
649 if (isl_upoly_is_zero(up2
)) {
654 if (up1
->var
< up2
->var
)
655 return isl_upoly_sum(up2
, up1
);
657 if (up2
->var
< up1
->var
) {
658 struct isl_upoly_rec
*rec
;
659 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
663 up1
= isl_upoly_cow(up1
);
664 rec
= isl_upoly_as_rec(up1
);
667 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
669 up1
= replace_by_constant_term(up1
);
673 if (isl_upoly_is_cst(up1
))
674 return isl_upoly_sum_cst(up1
, up2
);
676 rec1
= isl_upoly_as_rec(up1
);
677 rec2
= isl_upoly_as_rec(up2
);
681 if (rec1
->n
< rec2
->n
)
682 return isl_upoly_sum(up2
, up1
);
684 up1
= isl_upoly_cow(up1
);
685 rec1
= isl_upoly_as_rec(up1
);
689 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
690 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
691 isl_upoly_copy(rec2
->p
[i
]));
694 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
695 isl_upoly_free(rec1
->p
[i
]);
701 up1
= replace_by_zero(up1
);
702 else if (rec1
->n
== 1)
703 up1
= replace_by_constant_term(up1
);
714 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
715 __isl_take
struct isl_upoly
*up
, isl_int v
)
717 struct isl_upoly_cst
*cst
;
719 up
= isl_upoly_cow(up
);
723 cst
= isl_upoly_as_cst(up
);
725 isl_int_addmul(cst
->n
, cst
->d
, v
);
730 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
731 __isl_take
struct isl_upoly
*up
, isl_int v
)
733 struct isl_upoly_rec
*rec
;
738 if (isl_upoly_is_cst(up
))
739 return isl_upoly_cst_add_isl_int(up
, v
);
741 up
= isl_upoly_cow(up
);
742 rec
= isl_upoly_as_rec(up
);
746 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
756 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
757 __isl_take
struct isl_upoly
*up
, isl_int v
)
759 struct isl_upoly_cst
*cst
;
761 if (isl_upoly_is_zero(up
))
764 up
= isl_upoly_cow(up
);
768 cst
= isl_upoly_as_cst(up
);
770 isl_int_mul(cst
->n
, cst
->n
, v
);
775 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
776 __isl_take
struct isl_upoly
*up
, isl_int v
)
779 struct isl_upoly_rec
*rec
;
784 if (isl_upoly_is_cst(up
))
785 return isl_upoly_cst_mul_isl_int(up
, v
);
787 up
= isl_upoly_cow(up
);
788 rec
= isl_upoly_as_rec(up
);
792 for (i
= 0; i
< rec
->n
; ++i
) {
793 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
804 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
805 __isl_take
struct isl_upoly
*up2
)
807 struct isl_upoly_cst
*cst1
;
808 struct isl_upoly_cst
*cst2
;
810 up1
= isl_upoly_cow(up1
);
814 cst1
= isl_upoly_as_cst(up1
);
815 cst2
= isl_upoly_as_cst(up2
);
817 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
818 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
820 isl_upoly_cst_reduce(cst1
);
830 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
831 __isl_take
struct isl_upoly
*up2
)
833 struct isl_upoly_rec
*rec1
;
834 struct isl_upoly_rec
*rec2
;
835 struct isl_upoly_rec
*res
= NULL
;
839 rec1
= isl_upoly_as_rec(up1
);
840 rec2
= isl_upoly_as_rec(up2
);
843 size
= rec1
->n
+ rec2
->n
- 1;
844 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
848 for (i
= 0; i
< rec1
->n
; ++i
) {
849 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
850 isl_upoly_copy(rec1
->p
[i
]));
855 for (; i
< size
; ++i
) {
856 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
861 for (i
= 0; i
< rec1
->n
; ++i
) {
862 for (j
= 1; j
< rec2
->n
; ++j
) {
863 struct isl_upoly
*up
;
864 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
865 isl_upoly_copy(rec1
->p
[i
]));
866 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
879 isl_upoly_free(&res
->up
);
883 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
884 __isl_take
struct isl_upoly
*up2
)
889 if (isl_upoly_is_nan(up1
)) {
894 if (isl_upoly_is_nan(up2
)) {
899 if (isl_upoly_is_zero(up1
)) {
904 if (isl_upoly_is_zero(up2
)) {
909 if (isl_upoly_is_one(up1
)) {
914 if (isl_upoly_is_one(up2
)) {
919 if (up1
->var
< up2
->var
)
920 return isl_upoly_mul(up2
, up1
);
922 if (up2
->var
< up1
->var
) {
924 struct isl_upoly_rec
*rec
;
925 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
926 isl_ctx
*ctx
= up1
->ctx
;
929 return isl_upoly_nan(ctx
);
931 up1
= isl_upoly_cow(up1
);
932 rec
= isl_upoly_as_rec(up1
);
936 for (i
= 0; i
< rec
->n
; ++i
) {
937 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
938 isl_upoly_copy(up2
));
946 if (isl_upoly_is_cst(up1
))
947 return isl_upoly_mul_cst(up1
, up2
);
949 return isl_upoly_mul_rec(up1
, up2
);
956 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
959 struct isl_upoly
*res
;
967 res
= isl_upoly_copy(up
);
969 res
= isl_upoly_one(up
->ctx
);
971 while (power
>>= 1) {
972 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
974 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
981 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*dim
,
982 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
984 struct isl_qpolynomial
*qp
= NULL
;
990 if (!isl_space_is_set(dim
))
991 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
992 "domain of polynomial should be a set", goto error
);
994 total
= isl_space_dim(dim
, isl_dim_all
);
996 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
1001 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1010 isl_space_free(dim
);
1012 isl_qpolynomial_free(qp
);
1016 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1025 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1027 struct isl_qpolynomial
*dup
;
1032 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1033 isl_upoly_copy(qp
->upoly
));
1036 isl_mat_free(dup
->div
);
1037 dup
->div
= isl_mat_copy(qp
->div
);
1043 isl_qpolynomial_free(dup
);
1047 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1055 return isl_qpolynomial_dup(qp
);
1058 void *isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
1066 isl_space_free(qp
->dim
);
1067 isl_mat_free(qp
->div
);
1068 isl_upoly_free(qp
->upoly
);
1074 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1077 struct isl_upoly_rec
*rec
;
1078 struct isl_upoly_cst
*cst
;
1080 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1083 for (i
= 0; i
< 1 + power
; ++i
) {
1084 rec
->p
[i
] = isl_upoly_zero(ctx
);
1089 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1090 isl_int_set_si(cst
->n
, 1);
1094 isl_upoly_free(&rec
->up
);
1098 /* r array maps original positions to new positions.
1100 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1104 struct isl_upoly_rec
*rec
;
1105 struct isl_upoly
*base
;
1106 struct isl_upoly
*res
;
1108 if (isl_upoly_is_cst(up
))
1111 rec
= isl_upoly_as_rec(up
);
1115 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1117 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1118 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1120 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1121 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1122 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1125 isl_upoly_free(base
);
1134 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
1139 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1140 div1
->n_col
>= div2
->n_col
, return -1);
1142 if (div1
->n_row
== div2
->n_row
)
1143 return isl_mat_is_equal(div1
, div2
);
1145 n_row
= div1
->n_row
;
1146 n_col
= div1
->n_col
;
1147 div1
->n_row
= div2
->n_row
;
1148 div1
->n_col
= div2
->n_col
;
1150 equal
= isl_mat_is_equal(div1
, div2
);
1152 div1
->n_row
= n_row
;
1153 div1
->n_col
= n_col
;
1158 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1162 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1163 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1168 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1171 struct isl_div_sort_info
{
1176 static int div_sort_cmp(const void *p1
, const void *p2
)
1178 const struct isl_div_sort_info
*i1
, *i2
;
1179 i1
= (const struct isl_div_sort_info
*) p1
;
1180 i2
= (const struct isl_div_sort_info
*) p2
;
1182 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1185 /* Sort divs and remove duplicates.
1187 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1192 struct isl_div_sort_info
*array
= NULL
;
1193 int *pos
= NULL
, *at
= NULL
;
1194 int *reordering
= NULL
;
1199 if (qp
->div
->n_row
<= 1)
1202 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1204 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1206 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1207 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1208 len
= qp
->div
->n_col
- 2;
1209 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1210 if (!array
|| !pos
|| !at
|| !reordering
)
1213 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1214 array
[i
].div
= qp
->div
;
1220 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1223 for (i
= 0; i
< div_pos
; ++i
)
1226 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1227 if (pos
[array
[i
].row
] == i
)
1229 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1230 pos
[at
[i
]] = pos
[array
[i
].row
];
1231 at
[pos
[array
[i
].row
]] = at
[i
];
1232 at
[i
] = array
[i
].row
;
1233 pos
[array
[i
].row
] = i
;
1237 for (i
= 0; i
< len
- div_pos
; ++i
) {
1239 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1240 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1241 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1242 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1243 2 + div_pos
+ i
- skip
);
1244 qp
->div
= isl_mat_drop_cols(qp
->div
,
1245 2 + div_pos
+ i
- skip
, 1);
1248 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1251 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1253 if (!qp
->upoly
|| !qp
->div
)
1267 isl_qpolynomial_free(qp
);
1271 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1272 int *exp
, int first
)
1275 struct isl_upoly_rec
*rec
;
1277 if (isl_upoly_is_cst(up
))
1280 if (up
->var
< first
)
1283 if (exp
[up
->var
- first
] == up
->var
- first
)
1286 up
= isl_upoly_cow(up
);
1290 up
->var
= exp
[up
->var
- first
] + first
;
1292 rec
= isl_upoly_as_rec(up
);
1296 for (i
= 0; i
< rec
->n
; ++i
) {
1297 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1308 static __isl_give isl_qpolynomial
*with_merged_divs(
1309 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1310 __isl_take isl_qpolynomial
*qp2
),
1311 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1315 isl_mat
*div
= NULL
;
1317 qp1
= isl_qpolynomial_cow(qp1
);
1318 qp2
= isl_qpolynomial_cow(qp2
);
1323 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1324 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1326 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
1327 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
1331 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1335 isl_mat_free(qp1
->div
);
1336 qp1
->div
= isl_mat_copy(div
);
1337 isl_mat_free(qp2
->div
);
1338 qp2
->div
= isl_mat_copy(div
);
1340 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1341 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1343 if (!qp1
->upoly
|| !qp2
->upoly
)
1350 return fn(qp1
, qp2
);
1355 isl_qpolynomial_free(qp1
);
1356 isl_qpolynomial_free(qp2
);
1360 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1361 __isl_take isl_qpolynomial
*qp2
)
1363 qp1
= isl_qpolynomial_cow(qp1
);
1368 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1369 return isl_qpolynomial_add(qp2
, qp1
);
1371 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1372 if (!compatible_divs(qp1
->div
, qp2
->div
))
1373 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1375 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1379 isl_qpolynomial_free(qp2
);
1383 isl_qpolynomial_free(qp1
);
1384 isl_qpolynomial_free(qp2
);
1388 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1389 __isl_keep isl_set
*dom
,
1390 __isl_take isl_qpolynomial
*qp1
,
1391 __isl_take isl_qpolynomial
*qp2
)
1393 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1394 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1398 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1399 __isl_take isl_qpolynomial
*qp2
)
1401 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1404 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1405 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1407 if (isl_int_is_zero(v
))
1410 qp
= isl_qpolynomial_cow(qp
);
1414 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1420 isl_qpolynomial_free(qp
);
1425 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1430 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1433 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1434 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1436 if (isl_int_is_one(v
))
1439 if (qp
&& isl_int_is_zero(v
)) {
1440 isl_qpolynomial
*zero
;
1441 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1442 isl_qpolynomial_free(qp
);
1446 qp
= isl_qpolynomial_cow(qp
);
1450 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1456 isl_qpolynomial_free(qp
);
1460 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1461 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1463 return isl_qpolynomial_mul_isl_int(qp
, v
);
1466 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1467 __isl_take isl_qpolynomial
*qp2
)
1469 qp1
= isl_qpolynomial_cow(qp1
);
1474 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1475 return isl_qpolynomial_mul(qp2
, qp1
);
1477 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1478 if (!compatible_divs(qp1
->div
, qp2
->div
))
1479 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1481 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1485 isl_qpolynomial_free(qp2
);
1489 isl_qpolynomial_free(qp1
);
1490 isl_qpolynomial_free(qp2
);
1494 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1497 qp
= isl_qpolynomial_cow(qp
);
1502 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1508 isl_qpolynomial_free(qp
);
1512 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1513 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1520 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1524 for (i
= 0; i
< pwqp
->n
; ++i
) {
1525 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1527 return isl_pw_qpolynomial_free(pwqp
);
1533 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1534 __isl_take isl_space
*dim
)
1538 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1541 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1542 __isl_take isl_space
*dim
)
1546 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1549 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1550 __isl_take isl_space
*dim
)
1554 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1557 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1558 __isl_take isl_space
*dim
)
1562 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1565 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1566 __isl_take isl_space
*dim
)
1570 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1573 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1574 __isl_take isl_space
*dim
,
1577 struct isl_qpolynomial
*qp
;
1578 struct isl_upoly_cst
*cst
;
1583 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1587 cst
= isl_upoly_as_cst(qp
->upoly
);
1588 isl_int_set(cst
->n
, v
);
1593 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1594 isl_int
*n
, isl_int
*d
)
1596 struct isl_upoly_cst
*cst
;
1601 if (!isl_upoly_is_cst(qp
->upoly
))
1604 cst
= isl_upoly_as_cst(qp
->upoly
);
1609 isl_int_set(*n
, cst
->n
);
1611 isl_int_set(*d
, cst
->d
);
1616 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1619 struct isl_upoly_rec
*rec
;
1627 rec
= isl_upoly_as_rec(up
);
1634 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1636 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1642 return isl_upoly_is_affine(rec
->p
[0]);
1645 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1650 if (qp
->div
->n_row
> 0)
1653 return isl_upoly_is_affine(qp
->upoly
);
1656 static void update_coeff(__isl_keep isl_vec
*aff
,
1657 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1662 if (isl_int_is_zero(cst
->n
))
1667 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1668 isl_int_divexact(f
, cst
->d
, gcd
);
1669 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1670 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1671 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1676 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1677 __isl_keep isl_vec
*aff
)
1679 struct isl_upoly_cst
*cst
;
1680 struct isl_upoly_rec
*rec
;
1686 struct isl_upoly_cst
*cst
;
1688 cst
= isl_upoly_as_cst(up
);
1691 update_coeff(aff
, cst
, 0);
1695 rec
= isl_upoly_as_rec(up
);
1698 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1700 cst
= isl_upoly_as_cst(rec
->p
[1]);
1703 update_coeff(aff
, cst
, 1 + up
->var
);
1705 return isl_upoly_update_affine(rec
->p
[0], aff
);
1708 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1709 __isl_keep isl_qpolynomial
*qp
)
1717 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1718 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1722 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1723 isl_int_set_si(aff
->el
[0], 1);
1725 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1734 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1735 __isl_keep isl_qpolynomial
*qp2
)
1742 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
1743 if (equal
< 0 || !equal
)
1746 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
1747 if (equal
< 0 || !equal
)
1750 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1753 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1756 struct isl_upoly_rec
*rec
;
1758 if (isl_upoly_is_cst(up
)) {
1759 struct isl_upoly_cst
*cst
;
1760 cst
= isl_upoly_as_cst(up
);
1763 isl_int_lcm(*d
, *d
, cst
->d
);
1767 rec
= isl_upoly_as_rec(up
);
1771 for (i
= 0; i
< rec
->n
; ++i
)
1772 upoly_update_den(rec
->p
[i
], d
);
1775 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1777 isl_int_set_si(*d
, 1);
1780 upoly_update_den(qp
->upoly
, d
);
1783 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
1784 __isl_take isl_space
*dim
, int pos
, int power
)
1786 struct isl_ctx
*ctx
;
1793 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
1796 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(__isl_take isl_space
*dim
,
1797 enum isl_dim_type type
, unsigned pos
)
1802 isl_assert(dim
->ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, goto error
);
1803 isl_assert(dim
->ctx
, pos
< isl_space_dim(dim
, type
), goto error
);
1805 if (type
== isl_dim_set
)
1806 pos
+= isl_space_dim(dim
, isl_dim_param
);
1808 return isl_qpolynomial_var_pow_on_domain(dim
, pos
, 1);
1810 isl_space_free(dim
);
1814 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
1815 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
1818 struct isl_upoly_rec
*rec
;
1819 struct isl_upoly
*base
, *res
;
1824 if (isl_upoly_is_cst(up
))
1827 if (up
->var
< first
)
1830 rec
= isl_upoly_as_rec(up
);
1834 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1836 if (up
->var
>= first
+ n
)
1837 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
1839 base
= isl_upoly_copy(subs
[up
->var
- first
]);
1841 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
1842 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1843 struct isl_upoly
*t
;
1844 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
1845 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1846 res
= isl_upoly_sum(res
, t
);
1849 isl_upoly_free(base
);
1858 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
1859 isl_int denom
, unsigned len
)
1862 struct isl_upoly
*up
;
1864 isl_assert(ctx
, len
>= 1, return NULL
);
1866 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
1867 for (i
= 0; i
< len
- 1; ++i
) {
1868 struct isl_upoly
*t
;
1869 struct isl_upoly
*c
;
1871 if (isl_int_is_zero(f
[1 + i
]))
1874 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
1875 t
= isl_upoly_var_pow(ctx
, i
, 1);
1876 t
= isl_upoly_mul(c
, t
);
1877 up
= isl_upoly_sum(up
, t
);
1883 /* Remove common factor of non-constant terms and denominator.
1885 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
1887 isl_ctx
*ctx
= qp
->div
->ctx
;
1888 unsigned total
= qp
->div
->n_col
- 2;
1890 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
1891 isl_int_gcd(ctx
->normalize_gcd
,
1892 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
1893 if (isl_int_is_one(ctx
->normalize_gcd
))
1896 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
1897 ctx
->normalize_gcd
, total
);
1898 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
1899 ctx
->normalize_gcd
);
1900 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
1901 ctx
->normalize_gcd
);
1904 /* Replace the integer division identified by "div" by the polynomial "s".
1905 * The integer division is assumed not to appear in the definition
1906 * of any other integer divisions.
1908 static __isl_give isl_qpolynomial
*substitute_div(
1909 __isl_take isl_qpolynomial
*qp
,
1910 int div
, __isl_take
struct isl_upoly
*s
)
1919 qp
= isl_qpolynomial_cow(qp
);
1923 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
1924 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
1928 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
1931 for (i
= 0; i
< total
+ div
; ++i
)
1933 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
1934 reordering
[i
] = i
- 1;
1935 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
1936 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
1937 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1940 if (!qp
->upoly
|| !qp
->div
)
1946 isl_qpolynomial_free(qp
);
1951 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1952 * divisions because d is equal to 1 by their definition, i.e., e.
1954 static __isl_give isl_qpolynomial
*substitute_non_divs(
1955 __isl_take isl_qpolynomial
*qp
)
1959 struct isl_upoly
*s
;
1964 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
1965 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
1966 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
1968 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
1969 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
1971 isl_seq_combine(qp
->div
->row
[j
] + 1,
1972 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
1973 qp
->div
->row
[j
][2 + total
+ i
],
1974 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
1975 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
1976 normalize_div(qp
, j
);
1978 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
1979 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
1980 qp
= substitute_div(qp
, i
, s
);
1987 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1988 * with d the denominator. When replacing the coefficient e of x by
1989 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1990 * inside the division, so we need to add floor(e/d) * x outside.
1991 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1992 * to adjust the coefficient of x in each later div that depends on the
1993 * current div "div" and also in the affine expression "aff"
1994 * (if it too depends on "div").
1996 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
1997 __isl_keep isl_vec
*aff
)
2001 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2004 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2005 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2006 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2008 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2009 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2010 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2011 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2012 isl_int_addmul(aff
->el
[i
], v
, aff
->el
[1 + total
+ div
]);
2013 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2014 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2016 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2017 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2023 /* Check if the last non-zero coefficient is bigger that half of the
2024 * denominator. If so, we will invert the div to further reduce the number
2025 * of distinct divs that may appear.
2026 * If the last non-zero coefficient is exactly half the denominator,
2027 * then we continue looking for earlier coefficients that are bigger
2028 * than half the denominator.
2030 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2035 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2036 if (isl_int_is_zero(div
->row
[row
][i
]))
2038 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2039 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2040 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2050 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2051 * We only invert the coefficients of e (and the coefficient of q in
2052 * later divs and in "aff"). After calling this function, the
2053 * coefficients of e should be reduced again.
2055 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2056 __isl_keep isl_vec
*aff
)
2058 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2060 isl_seq_neg(qp
->div
->row
[div
] + 1,
2061 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2062 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2063 isl_int_add(qp
->div
->row
[div
][1],
2064 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2065 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2066 isl_int_neg(aff
->el
[1 + total
+ div
], aff
->el
[1 + total
+ div
]);
2067 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2068 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2071 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2072 * in the interval [0, d-1], with d the denominator and such that the
2073 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2075 * After the reduction, some divs may have become redundant or identical,
2076 * so we call substitute_non_divs and sort_divs. If these functions
2077 * eliminate divs or merge two or more divs into one, the coefficients
2078 * of the enclosing divs may have to be reduced again, so we call
2079 * ourselves recursively if the number of divs decreases.
2081 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2084 isl_vec
*aff
= NULL
;
2085 struct isl_upoly
*s
;
2091 aff
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
2092 aff
= isl_vec_clr(aff
);
2096 isl_int_set_si(aff
->el
[1 + qp
->upoly
->var
], 1);
2098 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2099 normalize_div(qp
, i
);
2100 reduce_div(qp
, i
, aff
);
2101 if (needs_invert(qp
->div
, i
)) {
2102 invert_div(qp
, i
, aff
);
2103 reduce_div(qp
, i
, aff
);
2107 s
= isl_upoly_from_affine(qp
->div
->ctx
, aff
->el
,
2108 qp
->div
->ctx
->one
, aff
->size
);
2109 qp
->upoly
= isl_upoly_subs(qp
->upoly
, qp
->upoly
->var
, 1, &s
);
2116 n_div
= qp
->div
->n_row
;
2117 qp
= substitute_non_divs(qp
);
2119 if (qp
&& qp
->div
->n_row
< n_div
)
2120 return reduce_divs(qp
);
2124 isl_qpolynomial_free(qp
);
2129 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2130 __isl_take isl_space
*dim
, const isl_int n
, const isl_int d
)
2132 struct isl_qpolynomial
*qp
;
2133 struct isl_upoly_cst
*cst
;
2138 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2142 cst
= isl_upoly_as_cst(qp
->upoly
);
2143 isl_int_set(cst
->n
, n
);
2144 isl_int_set(cst
->d
, d
);
2149 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2151 struct isl_upoly_rec
*rec
;
2157 if (isl_upoly_is_cst(up
))
2161 active
[up
->var
] = 1;
2163 rec
= isl_upoly_as_rec(up
);
2164 for (i
= 0; i
< rec
->n
; ++i
)
2165 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2171 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2174 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2179 for (i
= 0; i
< d
; ++i
)
2180 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2181 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2187 return up_set_active(qp
->upoly
, active
, d
);
2190 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2191 enum isl_dim_type type
, unsigned first
, unsigned n
)
2202 isl_assert(qp
->dim
->ctx
,
2203 first
+ n
<= isl_qpolynomial_dim(qp
, type
), return -1);
2204 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2205 type
== isl_dim_in
, return -1);
2207 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2208 isl_space_dim(qp
->dim
, isl_dim_all
));
2209 if (set_active(qp
, active
) < 0)
2212 if (type
== isl_dim_in
)
2213 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2214 for (i
= 0; i
< n
; ++i
)
2215 if (active
[first
+ i
]) {
2228 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2229 * of the divs that do appear in the quasi-polynomial.
2231 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2232 __isl_take isl_qpolynomial
*qp
)
2239 int *reordering
= NULL
;
2246 if (qp
->div
->n_row
== 0)
2249 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2250 len
= qp
->div
->n_col
- 2;
2251 ctx
= isl_qpolynomial_get_ctx(qp
);
2252 active
= isl_calloc_array(ctx
, int, len
);
2256 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2259 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2260 if (!active
[d
+ i
]) {
2264 for (j
= 0; j
< i
; ++j
) {
2265 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2277 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2281 for (i
= 0; i
< d
; ++i
)
2285 n_div
= qp
->div
->n_row
;
2286 for (i
= 0; i
< n_div
; ++i
) {
2287 if (!active
[d
+ i
]) {
2288 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2289 qp
->div
= isl_mat_drop_cols(qp
->div
,
2290 2 + d
+ i
- skip
, 1);
2293 reordering
[d
+ i
] = d
+ i
- skip
;
2296 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2298 if (!qp
->upoly
|| !qp
->div
)
2308 isl_qpolynomial_free(qp
);
2312 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2313 unsigned first
, unsigned n
)
2316 struct isl_upoly_rec
*rec
;
2320 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2322 if (up
->var
< first
+ n
) {
2323 up
= replace_by_constant_term(up
);
2324 return isl_upoly_drop(up
, first
, n
);
2326 up
= isl_upoly_cow(up
);
2330 rec
= isl_upoly_as_rec(up
);
2334 for (i
= 0; i
< rec
->n
; ++i
) {
2335 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2346 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2347 __isl_take isl_qpolynomial
*qp
,
2348 enum isl_dim_type type
, unsigned pos
, const char *s
)
2350 qp
= isl_qpolynomial_cow(qp
);
2353 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2358 isl_qpolynomial_free(qp
);
2362 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2363 __isl_take isl_qpolynomial
*qp
,
2364 enum isl_dim_type type
, unsigned first
, unsigned n
)
2368 if (type
== isl_dim_out
)
2369 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2370 "cannot drop output/set dimension",
2372 if (type
== isl_dim_in
)
2374 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2377 qp
= isl_qpolynomial_cow(qp
);
2381 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2383 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2384 type
== isl_dim_set
, goto error
);
2386 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2390 if (type
== isl_dim_set
)
2391 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2393 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2397 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2403 isl_qpolynomial_free(qp
);
2407 /* Project the domain of the quasi-polynomial onto its parameter space.
2408 * The quasi-polynomial may not involve any of the domain dimensions.
2410 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2411 __isl_take isl_qpolynomial
*qp
)
2417 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2418 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2420 return isl_qpolynomial_free(qp
);
2422 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2423 "polynomial involves some of the domain dimensions",
2424 return isl_qpolynomial_free(qp
));
2425 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2426 space
= isl_qpolynomial_get_domain_space(qp
);
2427 space
= isl_space_params(space
);
2428 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2432 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2433 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2439 struct isl_upoly
*up
;
2443 if (eq
->n_eq
== 0) {
2444 isl_basic_set_free(eq
);
2448 qp
= isl_qpolynomial_cow(qp
);
2451 qp
->div
= isl_mat_cow(qp
->div
);
2455 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2457 isl_int_init(denom
);
2458 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2459 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2460 if (j
< 0 || j
== 0 || j
>= total
)
2463 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2464 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2466 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2467 &qp
->div
->row
[k
][0]);
2468 normalize_div(qp
, k
);
2471 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2472 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2473 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2474 isl_int_set_si(eq
->eq
[i
][j
], 0);
2476 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2477 eq
->eq
[i
], denom
, total
);
2478 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2481 isl_int_clear(denom
);
2486 isl_basic_set_free(eq
);
2488 qp
= substitute_non_divs(qp
);
2493 isl_basic_set_free(eq
);
2494 isl_qpolynomial_free(qp
);
2498 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2500 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2501 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2505 if (qp
->div
->n_row
> 0)
2506 eq
= isl_basic_set_add(eq
, isl_dim_set
, qp
->div
->n_row
);
2507 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2509 isl_basic_set_free(eq
);
2510 isl_qpolynomial_free(qp
);
2514 static __isl_give isl_basic_set
*add_div_constraints(
2515 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2523 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2526 total
= isl_basic_set_total_dim(bset
);
2527 for (i
= 0; i
< div
->n_row
; ++i
)
2528 if (isl_basic_set_add_div_constraints_var(bset
,
2529 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2536 isl_basic_set_free(bset
);
2540 /* Look for equalities among the variables shared by context and qp
2541 * and the integer divisions of qp, if any.
2542 * The equalities are then used to eliminate variables and/or integer
2543 * divisions from qp.
2545 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2546 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2552 if (qp
->div
->n_row
> 0) {
2553 isl_basic_set
*bset
;
2554 context
= isl_set_add_dims(context
, isl_dim_set
,
2556 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2557 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2558 context
= isl_set_intersect(context
,
2559 isl_set_from_basic_set(bset
));
2562 aff
= isl_set_affine_hull(context
);
2563 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2565 isl_qpolynomial_free(qp
);
2566 isl_set_free(context
);
2570 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2571 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2573 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2574 isl_set
*dom_context
= isl_set_universe(space
);
2575 dom_context
= isl_set_intersect_params(dom_context
, context
);
2576 return isl_qpolynomial_gist(qp
, dom_context
);
2579 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2580 __isl_take isl_qpolynomial
*qp
)
2586 if (isl_qpolynomial_is_zero(qp
)) {
2587 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2588 isl_qpolynomial_free(qp
);
2589 return isl_pw_qpolynomial_zero(dim
);
2592 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2593 return isl_pw_qpolynomial_alloc(dom
, qp
);
2597 #define PW isl_pw_qpolynomial
2599 #define EL isl_qpolynomial
2601 #define EL_IS_ZERO is_zero
2605 #define IS_ZERO is_zero
2608 #undef DEFAULT_IS_ZERO
2609 #define DEFAULT_IS_ZERO 1
2611 #include <isl_pw_templ.c>
2614 #define UNION isl_union_pw_qpolynomial
2616 #define PART isl_pw_qpolynomial
2618 #define PARTS pw_qpolynomial
2619 #define ALIGN_DOMAIN
2621 #include <isl_union_templ.c>
2623 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2631 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2634 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2637 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2638 __isl_take isl_pw_qpolynomial
*pwqp1
,
2639 __isl_take isl_pw_qpolynomial
*pwqp2
)
2641 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2644 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2645 __isl_take isl_pw_qpolynomial
*pwqp1
,
2646 __isl_take isl_pw_qpolynomial
*pwqp2
)
2649 struct isl_pw_qpolynomial
*res
;
2651 if (!pwqp1
|| !pwqp2
)
2654 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
2657 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
2658 isl_pw_qpolynomial_free(pwqp2
);
2662 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2663 isl_pw_qpolynomial_free(pwqp1
);
2667 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2668 isl_pw_qpolynomial_free(pwqp1
);
2672 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
2673 isl_pw_qpolynomial_free(pwqp2
);
2677 n
= pwqp1
->n
* pwqp2
->n
;
2678 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
2680 for (i
= 0; i
< pwqp1
->n
; ++i
) {
2681 for (j
= 0; j
< pwqp2
->n
; ++j
) {
2682 struct isl_set
*common
;
2683 struct isl_qpolynomial
*prod
;
2684 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
2685 isl_set_copy(pwqp2
->p
[j
].set
));
2686 if (isl_set_plain_is_empty(common
)) {
2687 isl_set_free(common
);
2691 prod
= isl_qpolynomial_mul(
2692 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
2693 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
2695 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
2699 isl_pw_qpolynomial_free(pwqp1
);
2700 isl_pw_qpolynomial_free(pwqp2
);
2704 isl_pw_qpolynomial_free(pwqp1
);
2705 isl_pw_qpolynomial_free(pwqp2
);
2709 __isl_give
struct isl_upoly
*isl_upoly_eval(
2710 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
2713 struct isl_upoly_rec
*rec
;
2714 struct isl_upoly
*res
;
2715 struct isl_upoly
*base
;
2717 if (isl_upoly_is_cst(up
)) {
2722 rec
= isl_upoly_as_rec(up
);
2726 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2728 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
2730 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
2733 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2734 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2735 res
= isl_upoly_sum(res
,
2736 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
2737 isl_vec_copy(vec
)));
2740 isl_upoly_free(base
);
2750 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
2751 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
2754 struct isl_upoly
*up
;
2759 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
2761 if (qp
->div
->n_row
== 0)
2762 ext
= isl_vec_copy(pnt
->vec
);
2765 unsigned dim
= isl_space_dim(qp
->dim
, isl_dim_all
);
2766 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
2770 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
2771 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2772 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
2773 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
2774 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
2775 qp
->div
->row
[i
][0]);
2779 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
2783 dim
= isl_space_copy(qp
->dim
);
2784 isl_qpolynomial_free(qp
);
2785 isl_point_free(pnt
);
2787 return isl_qpolynomial_alloc(dim
, 0, up
);
2789 isl_qpolynomial_free(qp
);
2790 isl_point_free(pnt
);
2794 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
2795 __isl_keep
struct isl_upoly_cst
*cst2
)
2800 isl_int_mul(t
, cst1
->n
, cst2
->d
);
2801 isl_int_submul(t
, cst2
->n
, cst1
->d
);
2802 cmp
= isl_int_sgn(t
);
2807 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial
*qp1
,
2808 __isl_keep isl_qpolynomial
*qp2
)
2810 struct isl_upoly_cst
*cst1
, *cst2
;
2814 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), return -1);
2815 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), return -1);
2816 if (isl_qpolynomial_is_nan(qp1
))
2818 if (isl_qpolynomial_is_nan(qp2
))
2820 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2821 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2823 return isl_upoly_cmp(cst1
, cst2
) <= 0;
2826 __isl_give isl_qpolynomial
*isl_qpolynomial_min_cst(
2827 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2829 struct isl_upoly_cst
*cst1
, *cst2
;
2834 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2835 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2836 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2837 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2838 cmp
= isl_upoly_cmp(cst1
, cst2
);
2841 isl_qpolynomial_free(qp2
);
2843 isl_qpolynomial_free(qp1
);
2848 isl_qpolynomial_free(qp1
);
2849 isl_qpolynomial_free(qp2
);
2853 __isl_give isl_qpolynomial
*isl_qpolynomial_max_cst(
2854 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2856 struct isl_upoly_cst
*cst1
, *cst2
;
2861 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2862 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2863 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2864 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2865 cmp
= isl_upoly_cmp(cst1
, cst2
);
2868 isl_qpolynomial_free(qp2
);
2870 isl_qpolynomial_free(qp1
);
2875 isl_qpolynomial_free(qp1
);
2876 isl_qpolynomial_free(qp2
);
2880 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
2881 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
2882 unsigned first
, unsigned n
)
2890 if (type
== isl_dim_out
)
2891 isl_die(qp
->div
->ctx
, isl_error_invalid
,
2892 "cannot insert output/set dimensions",
2894 if (type
== isl_dim_in
)
2896 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2899 qp
= isl_qpolynomial_cow(qp
);
2903 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
2906 g_pos
= pos(qp
->dim
, type
) + first
;
2908 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
2912 total
= qp
->div
->n_col
- 2;
2913 if (total
> g_pos
) {
2915 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
2918 for (i
= 0; i
< total
- g_pos
; ++i
)
2920 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
2926 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
2932 isl_qpolynomial_free(qp
);
2936 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
2937 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
2941 pos
= isl_qpolynomial_dim(qp
, type
);
2943 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
2946 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
2947 __isl_take isl_pw_qpolynomial
*pwqp
,
2948 enum isl_dim_type type
, unsigned n
)
2952 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
2954 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
2957 static int *reordering_move(isl_ctx
*ctx
,
2958 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
2963 reordering
= isl_alloc_array(ctx
, int, len
);
2968 for (i
= 0; i
< dst
; ++i
)
2970 for (i
= 0; i
< n
; ++i
)
2971 reordering
[src
+ i
] = dst
+ i
;
2972 for (i
= 0; i
< src
- dst
; ++i
)
2973 reordering
[dst
+ i
] = dst
+ n
+ i
;
2974 for (i
= 0; i
< len
- src
- n
; ++i
)
2975 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
2977 for (i
= 0; i
< src
; ++i
)
2979 for (i
= 0; i
< n
; ++i
)
2980 reordering
[src
+ i
] = dst
+ i
;
2981 for (i
= 0; i
< dst
- src
; ++i
)
2982 reordering
[src
+ n
+ i
] = src
+ i
;
2983 for (i
= 0; i
< len
- dst
- n
; ++i
)
2984 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
2990 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
2991 __isl_take isl_qpolynomial
*qp
,
2992 enum isl_dim_type dst_type
, unsigned dst_pos
,
2993 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2999 qp
= isl_qpolynomial_cow(qp
);
3003 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3004 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3005 "cannot move output/set dimension",
3007 if (dst_type
== isl_dim_in
)
3008 dst_type
= isl_dim_set
;
3009 if (src_type
== isl_dim_in
)
3010 src_type
= isl_dim_set
;
3012 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3015 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3016 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3017 if (dst_type
> src_type
)
3020 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3027 reordering
= reordering_move(qp
->dim
->ctx
,
3028 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3032 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3037 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3043 isl_qpolynomial_free(qp
);
3047 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_space
*dim
,
3048 isl_int
*f
, isl_int denom
)
3050 struct isl_upoly
*up
;
3052 dim
= isl_space_domain(dim
);
3056 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
,
3057 1 + isl_space_dim(dim
, isl_dim_all
));
3059 return isl_qpolynomial_alloc(dim
, 0, up
);
3062 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3065 struct isl_upoly
*up
;
3066 isl_qpolynomial
*qp
;
3071 ctx
= isl_aff_get_ctx(aff
);
3072 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3075 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3076 aff
->ls
->div
->n_row
, up
);
3080 isl_mat_free(qp
->div
);
3081 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3082 qp
->div
= isl_mat_cow(qp
->div
);
3087 qp
= reduce_divs(qp
);
3088 qp
= remove_redundant_divs(qp
);
3095 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3096 __isl_take isl_pw_aff
*pwaff
)
3099 isl_pw_qpolynomial
*pwqp
;
3104 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3107 for (i
= 0; i
< pwaff
->n
; ++i
) {
3109 isl_qpolynomial
*qp
;
3111 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3112 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3113 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3116 isl_pw_aff_free(pwaff
);
3120 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3121 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3125 aff
= isl_constraint_get_bound(c
, type
, pos
);
3126 isl_constraint_free(c
);
3127 return isl_qpolynomial_from_aff(aff
);
3130 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3131 * in "qp" by subs[i].
3133 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3134 __isl_take isl_qpolynomial
*qp
,
3135 enum isl_dim_type type
, unsigned first
, unsigned n
,
3136 __isl_keep isl_qpolynomial
**subs
)
3139 struct isl_upoly
**ups
;
3144 qp
= isl_qpolynomial_cow(qp
);
3148 if (type
== isl_dim_out
)
3149 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3150 "cannot substitute output/set dimension",
3152 if (type
== isl_dim_in
)
3155 for (i
= 0; i
< n
; ++i
)
3159 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3162 for (i
= 0; i
< n
; ++i
)
3163 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3166 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3167 for (i
= 0; i
< n
; ++i
)
3168 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3170 first
+= pos(qp
->dim
, type
);
3172 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3175 for (i
= 0; i
< n
; ++i
)
3176 ups
[i
] = subs
[i
]->upoly
;
3178 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3187 isl_qpolynomial_free(qp
);
3191 /* Extend "bset" with extra set dimensions for each integer division
3192 * in "qp" and then call "fn" with the extended bset and the polynomial
3193 * that results from replacing each of the integer divisions by the
3194 * corresponding extra set dimension.
3196 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3197 __isl_keep isl_basic_set
*bset
,
3198 int (*fn
)(__isl_take isl_basic_set
*bset
,
3199 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3203 isl_qpolynomial
*poly
;
3207 if (qp
->div
->n_row
== 0)
3208 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3211 div
= isl_mat_copy(qp
->div
);
3212 dim
= isl_space_copy(qp
->dim
);
3213 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3214 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3215 bset
= isl_basic_set_copy(bset
);
3216 bset
= isl_basic_set_add(bset
, isl_dim_set
, qp
->div
->n_row
);
3217 bset
= add_div_constraints(bset
, div
);
3219 return fn(bset
, poly
, user
);
3224 /* Return total degree in variables first (inclusive) up to last (exclusive).
3226 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3230 struct isl_upoly_rec
*rec
;
3234 if (isl_upoly_is_zero(up
))
3236 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3239 rec
= isl_upoly_as_rec(up
);
3243 for (i
= 0; i
< rec
->n
; ++i
) {
3246 if (isl_upoly_is_zero(rec
->p
[i
]))
3248 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3258 /* Return total degree in set variables.
3260 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3268 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3269 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3270 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3273 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3274 unsigned pos
, int deg
)
3277 struct isl_upoly_rec
*rec
;
3282 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3284 return isl_upoly_copy(up
);
3286 return isl_upoly_zero(up
->ctx
);
3289 rec
= isl_upoly_as_rec(up
);
3293 if (up
->var
== pos
) {
3295 return isl_upoly_copy(rec
->p
[deg
]);
3297 return isl_upoly_zero(up
->ctx
);
3300 up
= isl_upoly_copy(up
);
3301 up
= isl_upoly_cow(up
);
3302 rec
= isl_upoly_as_rec(up
);
3306 for (i
= 0; i
< rec
->n
; ++i
) {
3307 struct isl_upoly
*t
;
3308 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3311 isl_upoly_free(rec
->p
[i
]);
3321 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3323 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3324 __isl_keep isl_qpolynomial
*qp
,
3325 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3328 struct isl_upoly
*up
;
3334 if (type
== isl_dim_out
)
3335 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3336 "output/set dimension does not have a coefficient",
3338 if (type
== isl_dim_in
)
3341 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3344 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3345 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3347 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3350 isl_mat_free(c
->div
);
3351 c
->div
= isl_mat_copy(qp
->div
);
3356 isl_qpolynomial_free(c
);
3360 /* Homogenize the polynomial in the variables first (inclusive) up to
3361 * last (exclusive) by inserting powers of variable first.
3362 * Variable first is assumed not to appear in the input.
3364 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3365 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3366 int first
, int last
)
3369 struct isl_upoly_rec
*rec
;
3373 if (isl_upoly_is_zero(up
))
3377 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3378 struct isl_upoly
*hom
;
3380 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3383 rec
= isl_upoly_as_rec(hom
);
3384 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3389 up
= isl_upoly_cow(up
);
3390 rec
= isl_upoly_as_rec(up
);
3394 for (i
= 0; i
< rec
->n
; ++i
) {
3395 if (isl_upoly_is_zero(rec
->p
[i
]))
3397 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3398 up
->var
< last
? deg
+ i
: i
, target
,
3410 /* Homogenize the polynomial in the set variables by introducing
3411 * powers of an extra set variable at position 0.
3413 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3414 __isl_take isl_qpolynomial
*poly
)
3418 int deg
= isl_qpolynomial_degree(poly
);
3423 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3424 poly
= isl_qpolynomial_cow(poly
);
3428 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3429 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3430 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3437 isl_qpolynomial_free(poly
);
3441 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*dim
,
3442 __isl_take isl_mat
*div
)
3450 n
= isl_space_dim(dim
, isl_dim_all
) + div
->n_row
;
3452 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3453 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3460 isl_int_init(term
->n
);
3461 isl_int_init(term
->d
);
3465 isl_space_free(dim
);
3470 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3479 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3488 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3490 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3494 isl_int_set(dup
->n
, term
->n
);
3495 isl_int_set(dup
->d
, term
->d
);
3497 for (i
= 0; i
< total
; ++i
)
3498 dup
->pow
[i
] = term
->pow
[i
];
3503 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3511 return isl_term_dup(term
);
3514 void isl_term_free(__isl_take isl_term
*term
)
3519 if (--term
->ref
> 0)
3522 isl_space_free(term
->dim
);
3523 isl_mat_free(term
->div
);
3524 isl_int_clear(term
->n
);
3525 isl_int_clear(term
->d
);
3529 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3537 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3538 case isl_dim_div
: return term
->div
->n_row
;
3539 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3545 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3547 return term
? term
->dim
->ctx
: NULL
;
3550 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3554 isl_int_set(*n
, term
->n
);
3557 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
3561 isl_int_set(*d
, term
->d
);
3564 int isl_term_get_exp(__isl_keep isl_term
*term
,
3565 enum isl_dim_type type
, unsigned pos
)
3570 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3572 if (type
>= isl_dim_set
)
3573 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3574 if (type
>= isl_dim_div
)
3575 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3577 return term
->pow
[pos
];
3580 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3582 isl_local_space
*ls
;
3589 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3592 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
3593 /* No nested divs for now */
3594 isl_assert(term
->dim
->ctx
,
3595 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
3596 term
->div
->n_row
) == -1,
3599 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3600 isl_mat_copy(term
->div
));
3601 aff
= isl_aff_alloc(ls
);
3605 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3610 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3611 int (*fn
)(__isl_take isl_term
*term
, void *user
),
3612 __isl_take isl_term
*term
, void *user
)
3615 struct isl_upoly_rec
*rec
;
3620 if (isl_upoly_is_zero(up
))
3623 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3624 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3625 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3627 if (isl_upoly_is_cst(up
)) {
3628 struct isl_upoly_cst
*cst
;
3629 cst
= isl_upoly_as_cst(up
);
3632 term
= isl_term_cow(term
);
3635 isl_int_set(term
->n
, cst
->n
);
3636 isl_int_set(term
->d
, cst
->d
);
3637 if (fn(isl_term_copy(term
), user
) < 0)
3642 rec
= isl_upoly_as_rec(up
);
3646 for (i
= 0; i
< rec
->n
; ++i
) {
3647 term
= isl_term_cow(term
);
3650 term
->pow
[up
->var
] = i
;
3651 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3655 term
->pow
[up
->var
] = 0;
3659 isl_term_free(term
);
3663 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3664 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3671 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3675 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3677 isl_term_free(term
);
3679 return term
? 0 : -1;
3682 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3684 struct isl_upoly
*up
;
3685 isl_qpolynomial
*qp
;
3691 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3693 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3694 for (i
= 0; i
< n
; ++i
) {
3697 up
= isl_upoly_mul(up
,
3698 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3701 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
3704 isl_mat_free(qp
->div
);
3705 qp
->div
= isl_mat_copy(term
->div
);
3709 isl_term_free(term
);
3712 isl_qpolynomial_free(qp
);
3713 isl_term_free(term
);
3717 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3718 __isl_take isl_space
*dim
)
3727 if (isl_space_is_equal(qp
->dim
, dim
)) {
3728 isl_space_free(dim
);
3732 qp
= isl_qpolynomial_cow(qp
);
3736 extra
= isl_space_dim(dim
, isl_dim_set
) -
3737 isl_space_dim(qp
->dim
, isl_dim_set
);
3738 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
3739 if (qp
->div
->n_row
) {
3742 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
3745 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3747 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
3752 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
3755 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3756 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
3758 isl_space_free(qp
->dim
);
3763 isl_space_free(dim
);
3764 isl_qpolynomial_free(qp
);
3768 /* For each parameter or variable that does not appear in qp,
3769 * first eliminate the variable from all constraints and then set it to zero.
3771 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3772 __isl_keep isl_qpolynomial
*qp
)
3783 d
= isl_space_dim(set
->dim
, isl_dim_all
);
3784 active
= isl_calloc_array(set
->ctx
, int, d
);
3785 if (set_active(qp
, active
) < 0)
3788 for (i
= 0; i
< d
; ++i
)
3797 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
3798 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
3799 for (i
= 0; i
< nparam
; ++i
) {
3802 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
3803 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
3805 for (i
= 0; i
< nvar
; ++i
) {
3806 if (active
[nparam
+ i
])
3808 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
3809 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
3821 struct isl_opt_data
{
3822 isl_qpolynomial
*qp
;
3824 isl_qpolynomial
*opt
;
3828 static int opt_fn(__isl_take isl_point
*pnt
, void *user
)
3830 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
3831 isl_qpolynomial
*val
;
3833 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
3837 } else if (data
->max
) {
3838 data
->opt
= isl_qpolynomial_max_cst(data
->opt
, val
);
3840 data
->opt
= isl_qpolynomial_min_cst(data
->opt
, val
);
3846 __isl_give isl_qpolynomial
*isl_qpolynomial_opt_on_domain(
3847 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
3849 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
3854 if (isl_upoly_is_cst(qp
->upoly
)) {
3859 set
= fix_inactive(set
, qp
);
3862 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
3866 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
3867 data
.opt
= isl_qpolynomial_zero_on_domain(space
);
3871 isl_qpolynomial_free(qp
);
3875 isl_qpolynomial_free(qp
);
3876 isl_qpolynomial_free(data
.opt
);
3880 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
3881 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
3886 struct isl_upoly
**subs
;
3887 isl_mat
*mat
, *diag
;
3889 qp
= isl_qpolynomial_cow(qp
);
3894 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
3896 n_sub
= morph
->inv
->n_row
- 1;
3897 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
3898 n_sub
+= qp
->div
->n_row
;
3899 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
3903 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3904 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
3905 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
3906 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
3907 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3908 subs
[morph
->inv
->n_row
- 1 + i
] =
3909 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
3911 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
3913 for (i
= 0; i
< n_sub
; ++i
)
3914 isl_upoly_free(subs
[i
]);
3917 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
3918 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
3919 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
3920 mat
= isl_mat_diagonal(mat
, diag
);
3921 qp
->div
= isl_mat_product(qp
->div
, mat
);
3922 isl_space_free(qp
->dim
);
3923 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
3925 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
3928 isl_morph_free(morph
);
3932 isl_qpolynomial_free(qp
);
3933 isl_morph_free(morph
);
3937 static int neg_entry(void **entry
, void *user
)
3939 isl_pw_qpolynomial
**pwqp
= (isl_pw_qpolynomial
**)entry
;
3941 *pwqp
= isl_pw_qpolynomial_neg(*pwqp
);
3943 return *pwqp
? 0 : -1;
3946 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_neg(
3947 __isl_take isl_union_pw_qpolynomial
*upwqp
)
3949 upwqp
= isl_union_pw_qpolynomial_cow(upwqp
);
3953 if (isl_hash_table_foreach(upwqp
->dim
->ctx
, &upwqp
->table
,
3954 &neg_entry
, NULL
) < 0)
3959 isl_union_pw_qpolynomial_free(upwqp
);
3963 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_sub(
3964 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3965 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3967 return isl_union_pw_qpolynomial_add(upwqp1
,
3968 isl_union_pw_qpolynomial_neg(upwqp2
));
3971 static int mul_entry(void **entry
, void *user
)
3973 struct isl_union_pw_qpolynomial_match_bin_data
*data
= user
;
3975 struct isl_hash_table_entry
*entry2
;
3976 isl_pw_qpolynomial
*pwpq
= *entry
;
3979 hash
= isl_space_get_hash(pwpq
->dim
);
3980 entry2
= isl_hash_table_find(data
->u2
->dim
->ctx
, &data
->u2
->table
,
3981 hash
, &has_dim
, pwpq
->dim
, 0);
3985 pwpq
= isl_pw_qpolynomial_copy(pwpq
);
3986 pwpq
= isl_pw_qpolynomial_mul(pwpq
,
3987 isl_pw_qpolynomial_copy(entry2
->data
));
3989 empty
= isl_pw_qpolynomial_is_zero(pwpq
);
3991 isl_pw_qpolynomial_free(pwpq
);
3995 isl_pw_qpolynomial_free(pwpq
);
3999 data
->res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(data
->res
, pwpq
);
4004 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4005 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4006 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4008 return match_bin_op(upwqp1
, upwqp2
, &mul_entry
);
4011 /* Reorder the columns of the given div definitions according to the
4014 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
4015 __isl_take isl_reordering
*r
)
4024 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
4025 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
4029 for (i
= 0; i
< div
->n_row
; ++i
) {
4030 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
4031 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
4032 for (j
= 0; j
< r
->len
; ++j
)
4033 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
4034 div
->row
[i
][2 + j
]);
4037 isl_reordering_free(r
);
4041 isl_reordering_free(r
);
4046 /* Reorder the dimension of "qp" according to the given reordering.
4048 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4049 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4051 qp
= isl_qpolynomial_cow(qp
);
4055 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4059 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
4063 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4067 qp
= isl_qpolynomial_reset_domain_space(qp
, isl_space_copy(r
->dim
));
4069 isl_reordering_free(r
);
4072 isl_qpolynomial_free(qp
);
4073 isl_reordering_free(r
);
4077 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4078 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4083 if (!isl_space_match(qp
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
4084 isl_reordering
*exp
;
4086 model
= isl_space_drop_dims(model
, isl_dim_in
,
4087 0, isl_space_dim(model
, isl_dim_in
));
4088 model
= isl_space_drop_dims(model
, isl_dim_out
,
4089 0, isl_space_dim(model
, isl_dim_out
));
4090 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4091 exp
= isl_reordering_extend_space(exp
,
4092 isl_qpolynomial_get_domain_space(qp
));
4093 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4096 isl_space_free(model
);
4099 isl_space_free(model
);
4100 isl_qpolynomial_free(qp
);
4104 struct isl_split_periods_data
{
4106 isl_pw_qpolynomial
*res
;
4109 /* Create a slice where the integer division "div" has the fixed value "v".
4110 * In particular, if "div" refers to floor(f/m), then create a slice
4112 * m v <= f <= m v + (m - 1)
4117 * -f + m v + (m - 1) >= 0
4119 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*dim
,
4120 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4123 isl_basic_set
*bset
= NULL
;
4129 total
= isl_space_dim(dim
, isl_dim_all
);
4130 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0, 0, 2);
4132 k
= isl_basic_set_alloc_inequality(bset
);
4135 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4136 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4138 k
= isl_basic_set_alloc_inequality(bset
);
4141 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4142 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4143 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4144 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4146 isl_space_free(dim
);
4147 return isl_set_from_basic_set(bset
);
4149 isl_basic_set_free(bset
);
4150 isl_space_free(dim
);
4154 static int split_periods(__isl_take isl_set
*set
,
4155 __isl_take isl_qpolynomial
*qp
, void *user
);
4157 /* Create a slice of the domain "set" such that integer division "div"
4158 * has the fixed value "v" and add the results to data->res,
4159 * replacing the integer division by "v" in "qp".
4161 static int set_div(__isl_take isl_set
*set
,
4162 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4163 struct isl_split_periods_data
*data
)
4168 struct isl_upoly
*cst
;
4170 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4171 set
= isl_set_intersect(set
, slice
);
4176 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4178 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4179 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4181 isl_int_addmul(qp
->div
->row
[i
][1],
4182 qp
->div
->row
[i
][2 + total
+ div
], v
);
4183 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4186 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4187 qp
= substitute_div(qp
, div
, cst
);
4189 return split_periods(set
, qp
, data
);
4192 isl_qpolynomial_free(qp
);
4196 /* Split the domain "set" such that integer division "div"
4197 * has a fixed value (ranging from "min" to "max") on each slice
4198 * and add the results to data->res.
4200 static int split_div(__isl_take isl_set
*set
,
4201 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4202 struct isl_split_periods_data
*data
)
4204 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4205 isl_set
*set_i
= isl_set_copy(set
);
4206 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4208 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4212 isl_qpolynomial_free(qp
);
4216 isl_qpolynomial_free(qp
);
4220 /* If "qp" refers to any integer division
4221 * that can only attain "max_periods" distinct values on "set"
4222 * then split the domain along those distinct values.
4223 * Add the results (or the original if no splitting occurs)
4226 static int split_periods(__isl_take isl_set
*set
,
4227 __isl_take isl_qpolynomial
*qp
, void *user
)
4230 isl_pw_qpolynomial
*pwqp
;
4231 struct isl_split_periods_data
*data
;
4236 data
= (struct isl_split_periods_data
*)user
;
4241 if (qp
->div
->n_row
== 0) {
4242 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4243 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4249 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4250 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4251 enum isl_lp_result lp_res
;
4253 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4254 qp
->div
->n_row
) != -1)
4257 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4258 set
->ctx
->one
, &min
, NULL
, NULL
);
4259 if (lp_res
== isl_lp_error
)
4261 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4263 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4265 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4266 set
->ctx
->one
, &max
, NULL
, NULL
);
4267 if (lp_res
== isl_lp_error
)
4269 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4271 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4273 isl_int_sub(max
, max
, min
);
4274 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4275 isl_int_add(max
, max
, min
);
4280 if (i
< qp
->div
->n_row
) {
4281 r
= split_div(set
, qp
, i
, min
, max
, data
);
4283 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4284 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4296 isl_qpolynomial_free(qp
);
4300 /* If any quasi-polynomial in pwqp refers to any integer division
4301 * that can only attain "max_periods" distinct values on its domain
4302 * then split the domain along those distinct values.
4304 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4305 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4307 struct isl_split_periods_data data
;
4309 data
.max_periods
= max_periods
;
4310 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4312 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4315 isl_pw_qpolynomial_free(pwqp
);
4319 isl_pw_qpolynomial_free(data
.res
);
4320 isl_pw_qpolynomial_free(pwqp
);
4324 /* Construct a piecewise quasipolynomial that is constant on the given
4325 * domain. In particular, it is
4328 * infinity if cst == -1
4330 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4331 __isl_take isl_basic_set
*bset
, int cst
)
4334 isl_qpolynomial
*qp
;
4339 bset
= isl_basic_set_params(bset
);
4340 dim
= isl_basic_set_get_space(bset
);
4342 qp
= isl_qpolynomial_infty_on_domain(dim
);
4344 qp
= isl_qpolynomial_zero_on_domain(dim
);
4346 qp
= isl_qpolynomial_one_on_domain(dim
);
4347 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4350 /* Factor bset, call fn on each of the factors and return the product.
4352 * If no factors can be found, simply call fn on the input.
4353 * Otherwise, construct the factors based on the factorizer,
4354 * call fn on each factor and compute the product.
4356 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4357 __isl_take isl_basic_set
*bset
,
4358 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4364 isl_qpolynomial
*qp
;
4365 isl_pw_qpolynomial
*pwqp
;
4369 f
= isl_basic_set_factorizer(bset
);
4372 if (f
->n_group
== 0) {
4373 isl_factorizer_free(f
);
4377 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4378 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4380 dim
= isl_basic_set_get_space(bset
);
4381 dim
= isl_space_domain(dim
);
4382 set
= isl_set_universe(isl_space_copy(dim
));
4383 qp
= isl_qpolynomial_one_on_domain(dim
);
4384 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4386 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4388 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4389 isl_basic_set
*bset_i
;
4390 isl_pw_qpolynomial
*pwqp_i
;
4392 bset_i
= isl_basic_set_copy(bset
);
4393 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4394 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4395 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4397 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4398 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4399 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4401 pwqp_i
= fn(bset_i
);
4402 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4407 isl_basic_set_free(bset
);
4408 isl_factorizer_free(f
);
4412 isl_basic_set_free(bset
);
4416 /* Factor bset, call fn on each of the factors and return the product.
4417 * The function is assumed to evaluate to zero on empty domains,
4418 * to one on zero-dimensional domains and to infinity on unbounded domains
4419 * and will not be called explicitly on zero-dimensional or unbounded domains.
4421 * We first check for some special cases and remove all equalities.
4422 * Then we hand over control to compressed_multiplicative_call.
4424 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4425 __isl_take isl_basic_set
*bset
,
4426 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4430 isl_pw_qpolynomial
*pwqp
;
4435 if (isl_basic_set_plain_is_empty(bset
))
4436 return constant_on_domain(bset
, 0);
4438 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4439 return constant_on_domain(bset
, 1);
4441 bounded
= isl_basic_set_is_bounded(bset
);
4445 return constant_on_domain(bset
, -1);
4447 if (bset
->n_eq
== 0)
4448 return compressed_multiplicative_call(bset
, fn
);
4450 morph
= isl_basic_set_full_compression(bset
);
4451 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4453 pwqp
= compressed_multiplicative_call(bset
, fn
);
4455 morph
= isl_morph_dom_params(morph
);
4456 morph
= isl_morph_ran_params(morph
);
4457 morph
= isl_morph_inverse(morph
);
4459 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4463 isl_basic_set_free(bset
);
4467 /* Drop all floors in "qp", turning each integer division [a/m] into
4468 * a rational division a/m. If "down" is set, then the integer division
4469 * is replaces by (a-(m-1))/m instead.
4471 static __isl_give isl_qpolynomial
*qp_drop_floors(
4472 __isl_take isl_qpolynomial
*qp
, int down
)
4475 struct isl_upoly
*s
;
4479 if (qp
->div
->n_row
== 0)
4482 qp
= isl_qpolynomial_cow(qp
);
4486 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4488 isl_int_sub(qp
->div
->row
[i
][1],
4489 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4490 isl_int_add_ui(qp
->div
->row
[i
][1],
4491 qp
->div
->row
[i
][1], 1);
4493 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4494 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4495 qp
= substitute_div(qp
, i
, s
);
4503 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4504 * a rational division a/m.
4506 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4507 __isl_take isl_pw_qpolynomial
*pwqp
)
4514 if (isl_pw_qpolynomial_is_zero(pwqp
))
4517 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4521 for (i
= 0; i
< pwqp
->n
; ++i
) {
4522 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4529 isl_pw_qpolynomial_free(pwqp
);
4533 /* Adjust all the integer divisions in "qp" such that they are at least
4534 * one over the given orthant (identified by "signs"). This ensures
4535 * that they will still be non-negative even after subtracting (m-1)/m.
4537 * In particular, f is replaced by f' + v, changing f = [a/m]
4538 * to f' = [(a - m v)/m].
4539 * If the constant term k in a is smaller than m,
4540 * the constant term of v is set to floor(k/m) - 1.
4541 * For any other term, if the coefficient c and the variable x have
4542 * the same sign, then no changes are needed.
4543 * Otherwise, if the variable is positive (and c is negative),
4544 * then the coefficient of x in v is set to floor(c/m).
4545 * If the variable is negative (and c is positive),
4546 * then the coefficient of x in v is set to ceil(c/m).
4548 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4554 struct isl_upoly
*s
;
4556 qp
= isl_qpolynomial_cow(qp
);
4559 qp
->div
= isl_mat_cow(qp
->div
);
4563 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4564 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4566 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4567 isl_int
*row
= qp
->div
->row
[i
];
4571 if (isl_int_lt(row
[1], row
[0])) {
4572 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4573 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4574 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4576 for (j
= 0; j
< total
; ++j
) {
4577 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4580 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4582 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4583 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4585 for (j
= 0; j
< i
; ++j
) {
4586 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4588 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4589 row
[2 + total
+ j
], row
[0]);
4590 isl_int_submul(row
[2 + total
+ j
],
4591 row
[0], v
->el
[1 + total
+ j
]);
4593 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4594 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4596 isl_seq_combine(qp
->div
->row
[j
] + 1,
4597 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4598 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4600 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4601 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4602 qp
->div
->ctx
->one
, v
->size
);
4603 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4613 isl_qpolynomial_free(qp
);
4617 struct isl_to_poly_data
{
4619 isl_pw_qpolynomial
*res
;
4620 isl_qpolynomial
*qp
;
4623 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4624 * We first make all integer divisions positive and then split the
4625 * quasipolynomials into terms with sign data->sign (the direction
4626 * of the requested approximation) and terms with the opposite sign.
4627 * In the first set of terms, each integer division [a/m] is
4628 * overapproximated by a/m, while in the second it is underapproximated
4631 static int to_polynomial_on_orthant(__isl_take isl_set
*orthant
, int *signs
,
4634 struct isl_to_poly_data
*data
= user
;
4635 isl_pw_qpolynomial
*t
;
4636 isl_qpolynomial
*qp
, *up
, *down
;
4638 qp
= isl_qpolynomial_copy(data
->qp
);
4639 qp
= make_divs_pos(qp
, signs
);
4641 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4642 up
= qp_drop_floors(up
, 0);
4643 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4644 down
= qp_drop_floors(down
, 1);
4646 isl_qpolynomial_free(qp
);
4647 qp
= isl_qpolynomial_add(up
, down
);
4649 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4650 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4655 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4656 * the polynomial will be an overapproximation. If "sign" is negative,
4657 * it will be an underapproximation. If "sign" is zero, the approximation
4658 * will lie somewhere in between.
4660 * In particular, is sign == 0, we simply drop the floors, turning
4661 * the integer divisions into rational divisions.
4662 * Otherwise, we split the domains into orthants, make all integer divisions
4663 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4664 * depending on the requested sign and the sign of the term in which
4665 * the integer division appears.
4667 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4668 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4671 struct isl_to_poly_data data
;
4674 return pwqp_drop_floors(pwqp
);
4680 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4682 for (i
= 0; i
< pwqp
->n
; ++i
) {
4683 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4684 isl_pw_qpolynomial
*t
;
4685 t
= isl_pw_qpolynomial_alloc(
4686 isl_set_copy(pwqp
->p
[i
].set
),
4687 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4688 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4691 data
.qp
= pwqp
->p
[i
].qp
;
4692 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4693 &to_polynomial_on_orthant
, &data
) < 0)
4697 isl_pw_qpolynomial_free(pwqp
);
4701 isl_pw_qpolynomial_free(pwqp
);
4702 isl_pw_qpolynomial_free(data
.res
);
4706 static int poly_entry(void **entry
, void *user
)
4709 isl_pw_qpolynomial
**pwqp
= (isl_pw_qpolynomial
**)entry
;
4711 *pwqp
= isl_pw_qpolynomial_to_polynomial(*pwqp
, *sign
);
4713 return *pwqp
? 0 : -1;
4716 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4717 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4719 upwqp
= isl_union_pw_qpolynomial_cow(upwqp
);
4723 if (isl_hash_table_foreach(upwqp
->dim
->ctx
, &upwqp
->table
,
4724 &poly_entry
, &sign
) < 0)
4729 isl_union_pw_qpolynomial_free(upwqp
);
4733 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4734 __isl_take isl_qpolynomial
*qp
)
4738 isl_vec
*aff
= NULL
;
4739 isl_basic_map
*bmap
= NULL
;
4745 if (!isl_upoly_is_affine(qp
->upoly
))
4746 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4747 "input quasi-polynomial not affine", goto error
);
4748 aff
= isl_qpolynomial_extract_affine(qp
);
4751 dim
= isl_qpolynomial_get_space(qp
);
4752 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4753 n_div
= qp
->div
->n_row
;
4754 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4756 for (i
= 0; i
< n_div
; ++i
) {
4757 k
= isl_basic_map_alloc_div(bmap
);
4760 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4761 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4762 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4765 k
= isl_basic_map_alloc_equality(bmap
);
4768 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4769 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4770 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4773 isl_qpolynomial_free(qp
);
4774 bmap
= isl_basic_map_finalize(bmap
);
4778 isl_qpolynomial_free(qp
);
4779 isl_basic_map_free(bmap
);