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,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_dim_private.h>
22 #include <isl_div_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_dim
*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_dim(
347 __isl_take isl_qpolynomial
*qp
, __isl_take isl_dim
*dim
)
349 qp
= isl_qpolynomial_cow(qp
);
353 isl_dim_free(qp
->dim
);
358 isl_qpolynomial_free(qp
);
363 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
365 return qp
? qp
->dim
->ctx
: NULL
;
368 __isl_give isl_dim
*isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial
*qp
)
370 return qp
? isl_dim_copy(qp
->dim
) : NULL
;
373 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
374 enum isl_dim_type type
)
376 return qp
? isl_dim_size(qp
->dim
, type
) : 0;
379 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
381 return qp
? isl_upoly_is_zero(qp
->upoly
) : -1;
384 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
386 return qp
? isl_upoly_is_one(qp
->upoly
) : -1;
389 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
391 return qp
? isl_upoly_is_nan(qp
->upoly
) : -1;
394 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
396 return qp
? isl_upoly_is_infty(qp
->upoly
) : -1;
399 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
401 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : -1;
404 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
406 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
409 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
411 isl_int_clear(cst
->n
);
412 isl_int_clear(cst
->d
);
415 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
419 for (i
= 0; i
< rec
->n
; ++i
)
420 isl_upoly_free(rec
->p
[i
]);
423 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
432 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
434 struct isl_upoly_cst
*cst
;
435 struct isl_upoly_cst
*dup
;
437 cst
= isl_upoly_as_cst(up
);
441 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
444 isl_int_set(dup
->n
, cst
->n
);
445 isl_int_set(dup
->d
, cst
->d
);
450 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
453 struct isl_upoly_rec
*rec
;
454 struct isl_upoly_rec
*dup
;
456 rec
= isl_upoly_as_rec(up
);
460 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
464 for (i
= 0; i
< rec
->n
; ++i
) {
465 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
473 isl_upoly_free(&dup
->up
);
477 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
482 if (isl_upoly_is_cst(up
))
483 return isl_upoly_dup_cst(up
);
485 return isl_upoly_dup_rec(up
);
488 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
496 return isl_upoly_dup(up
);
499 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
508 upoly_free_cst((struct isl_upoly_cst
*)up
);
510 upoly_free_rec((struct isl_upoly_rec
*)up
);
512 isl_ctx_deref(up
->ctx
);
516 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
521 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
522 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
523 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
524 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
529 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
530 __isl_take
struct isl_upoly
*up2
)
532 struct isl_upoly_cst
*cst1
;
533 struct isl_upoly_cst
*cst2
;
535 up1
= isl_upoly_cow(up1
);
539 cst1
= isl_upoly_as_cst(up1
);
540 cst2
= isl_upoly_as_cst(up2
);
542 if (isl_int_eq(cst1
->d
, cst2
->d
))
543 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
545 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
546 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
547 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
550 isl_upoly_cst_reduce(cst1
);
560 static __isl_give
struct isl_upoly
*replace_by_zero(
561 __isl_take
struct isl_upoly
*up
)
569 return isl_upoly_zero(ctx
);
572 static __isl_give
struct isl_upoly
*replace_by_constant_term(
573 __isl_take
struct isl_upoly
*up
)
575 struct isl_upoly_rec
*rec
;
576 struct isl_upoly
*cst
;
581 rec
= isl_upoly_as_rec(up
);
584 cst
= isl_upoly_copy(rec
->p
[0]);
592 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
593 __isl_take
struct isl_upoly
*up2
)
596 struct isl_upoly_rec
*rec1
, *rec2
;
601 if (isl_upoly_is_nan(up1
)) {
606 if (isl_upoly_is_nan(up2
)) {
611 if (isl_upoly_is_zero(up1
)) {
616 if (isl_upoly_is_zero(up2
)) {
621 if (up1
->var
< up2
->var
)
622 return isl_upoly_sum(up2
, up1
);
624 if (up2
->var
< up1
->var
) {
625 struct isl_upoly_rec
*rec
;
626 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
630 up1
= isl_upoly_cow(up1
);
631 rec
= isl_upoly_as_rec(up1
);
634 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
636 up1
= replace_by_constant_term(up1
);
640 if (isl_upoly_is_cst(up1
))
641 return isl_upoly_sum_cst(up1
, up2
);
643 rec1
= isl_upoly_as_rec(up1
);
644 rec2
= isl_upoly_as_rec(up2
);
648 if (rec1
->n
< rec2
->n
)
649 return isl_upoly_sum(up2
, up1
);
651 up1
= isl_upoly_cow(up1
);
652 rec1
= isl_upoly_as_rec(up1
);
656 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
657 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
658 isl_upoly_copy(rec2
->p
[i
]));
661 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
662 isl_upoly_free(rec1
->p
[i
]);
668 up1
= replace_by_zero(up1
);
669 else if (rec1
->n
== 1)
670 up1
= replace_by_constant_term(up1
);
681 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
682 __isl_take
struct isl_upoly
*up
, isl_int v
)
684 struct isl_upoly_cst
*cst
;
686 up
= isl_upoly_cow(up
);
690 cst
= isl_upoly_as_cst(up
);
692 isl_int_addmul(cst
->n
, cst
->d
, v
);
697 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
698 __isl_take
struct isl_upoly
*up
, isl_int v
)
700 struct isl_upoly_rec
*rec
;
705 if (isl_upoly_is_cst(up
))
706 return isl_upoly_cst_add_isl_int(up
, v
);
708 up
= isl_upoly_cow(up
);
709 rec
= isl_upoly_as_rec(up
);
713 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
723 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
724 __isl_take
struct isl_upoly
*up
, isl_int v
)
726 struct isl_upoly_cst
*cst
;
728 if (isl_upoly_is_zero(up
))
731 up
= isl_upoly_cow(up
);
735 cst
= isl_upoly_as_cst(up
);
737 isl_int_mul(cst
->n
, cst
->n
, v
);
742 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
743 __isl_take
struct isl_upoly
*up
, isl_int v
)
746 struct isl_upoly_rec
*rec
;
751 if (isl_upoly_is_cst(up
))
752 return isl_upoly_cst_mul_isl_int(up
, v
);
754 up
= isl_upoly_cow(up
);
755 rec
= isl_upoly_as_rec(up
);
759 for (i
= 0; i
< rec
->n
; ++i
) {
760 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
771 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
772 __isl_take
struct isl_upoly
*up2
)
774 struct isl_upoly_cst
*cst1
;
775 struct isl_upoly_cst
*cst2
;
777 up1
= isl_upoly_cow(up1
);
781 cst1
= isl_upoly_as_cst(up1
);
782 cst2
= isl_upoly_as_cst(up2
);
784 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
785 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
787 isl_upoly_cst_reduce(cst1
);
797 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
798 __isl_take
struct isl_upoly
*up2
)
800 struct isl_upoly_rec
*rec1
;
801 struct isl_upoly_rec
*rec2
;
802 struct isl_upoly_rec
*res
= NULL
;
806 rec1
= isl_upoly_as_rec(up1
);
807 rec2
= isl_upoly_as_rec(up2
);
810 size
= rec1
->n
+ rec2
->n
- 1;
811 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
815 for (i
= 0; i
< rec1
->n
; ++i
) {
816 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
817 isl_upoly_copy(rec1
->p
[i
]));
822 for (; i
< size
; ++i
) {
823 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
828 for (i
= 0; i
< rec1
->n
; ++i
) {
829 for (j
= 1; j
< rec2
->n
; ++j
) {
830 struct isl_upoly
*up
;
831 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
832 isl_upoly_copy(rec1
->p
[i
]));
833 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
846 isl_upoly_free(&res
->up
);
850 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
851 __isl_take
struct isl_upoly
*up2
)
856 if (isl_upoly_is_nan(up1
)) {
861 if (isl_upoly_is_nan(up2
)) {
866 if (isl_upoly_is_zero(up1
)) {
871 if (isl_upoly_is_zero(up2
)) {
876 if (isl_upoly_is_one(up1
)) {
881 if (isl_upoly_is_one(up2
)) {
886 if (up1
->var
< up2
->var
)
887 return isl_upoly_mul(up2
, up1
);
889 if (up2
->var
< up1
->var
) {
891 struct isl_upoly_rec
*rec
;
892 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
893 isl_ctx
*ctx
= up1
->ctx
;
896 return isl_upoly_nan(ctx
);
898 up1
= isl_upoly_cow(up1
);
899 rec
= isl_upoly_as_rec(up1
);
903 for (i
= 0; i
< rec
->n
; ++i
) {
904 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
905 isl_upoly_copy(up2
));
913 if (isl_upoly_is_cst(up1
))
914 return isl_upoly_mul_cst(up1
, up2
);
916 return isl_upoly_mul_rec(up1
, up2
);
923 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
926 struct isl_upoly
*res
;
934 res
= isl_upoly_copy(up
);
936 res
= isl_upoly_one(up
->ctx
);
938 while (power
>>= 1) {
939 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
941 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
948 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_dim
*dim
,
949 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
951 struct isl_qpolynomial
*qp
= NULL
;
957 total
= isl_dim_total(dim
);
959 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
964 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
975 isl_qpolynomial_free(qp
);
979 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
988 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
990 struct isl_qpolynomial
*dup
;
995 dup
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
,
996 isl_upoly_copy(qp
->upoly
));
999 isl_mat_free(dup
->div
);
1000 dup
->div
= isl_mat_copy(qp
->div
);
1006 isl_qpolynomial_free(dup
);
1010 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1018 return isl_qpolynomial_dup(qp
);
1021 void isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
1029 isl_dim_free(qp
->dim
);
1030 isl_mat_free(qp
->div
);
1031 isl_upoly_free(qp
->upoly
);
1036 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1039 struct isl_upoly_rec
*rec
;
1040 struct isl_upoly_cst
*cst
;
1042 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1045 for (i
= 0; i
< 1 + power
; ++i
) {
1046 rec
->p
[i
] = isl_upoly_zero(ctx
);
1051 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1052 isl_int_set_si(cst
->n
, 1);
1056 isl_upoly_free(&rec
->up
);
1060 /* r array maps original positions to new positions.
1062 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1066 struct isl_upoly_rec
*rec
;
1067 struct isl_upoly
*base
;
1068 struct isl_upoly
*res
;
1070 if (isl_upoly_is_cst(up
))
1073 rec
= isl_upoly_as_rec(up
);
1077 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1079 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1080 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1082 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1083 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1084 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1087 isl_upoly_free(base
);
1096 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
1101 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1102 div1
->n_col
>= div2
->n_col
, return -1);
1104 if (div1
->n_row
== div2
->n_row
)
1105 return isl_mat_is_equal(div1
, div2
);
1107 n_row
= div1
->n_row
;
1108 n_col
= div1
->n_col
;
1109 div1
->n_row
= div2
->n_row
;
1110 div1
->n_col
= div2
->n_col
;
1112 equal
= isl_mat_is_equal(div1
, div2
);
1114 div1
->n_row
= n_row
;
1115 div1
->n_col
= n_col
;
1120 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1124 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1125 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1130 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1133 struct isl_div_sort_info
{
1138 static int div_sort_cmp(const void *p1
, const void *p2
)
1140 const struct isl_div_sort_info
*i1
, *i2
;
1141 i1
= (const struct isl_div_sort_info
*) p1
;
1142 i2
= (const struct isl_div_sort_info
*) p2
;
1144 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1147 /* Sort divs and remove duplicates.
1149 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1154 struct isl_div_sort_info
*array
= NULL
;
1155 int *pos
= NULL
, *at
= NULL
;
1156 int *reordering
= NULL
;
1161 if (qp
->div
->n_row
<= 1)
1164 div_pos
= isl_dim_total(qp
->dim
);
1166 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1168 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1169 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1170 len
= qp
->div
->n_col
- 2;
1171 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1172 if (!array
|| !pos
|| !at
|| !reordering
)
1175 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1176 array
[i
].div
= qp
->div
;
1182 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1185 for (i
= 0; i
< div_pos
; ++i
)
1188 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1189 if (pos
[array
[i
].row
] == i
)
1191 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1192 pos
[at
[i
]] = pos
[array
[i
].row
];
1193 at
[pos
[array
[i
].row
]] = at
[i
];
1194 at
[i
] = array
[i
].row
;
1195 pos
[array
[i
].row
] = i
;
1199 for (i
= 0; i
< len
- div_pos
; ++i
) {
1201 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1202 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1203 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1204 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1205 2 + div_pos
+ i
- skip
);
1206 qp
->div
= isl_mat_drop_cols(qp
->div
,
1207 2 + div_pos
+ i
- skip
, 1);
1210 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1213 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1215 if (!qp
->upoly
|| !qp
->div
)
1229 isl_qpolynomial_free(qp
);
1233 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1234 int *exp
, int first
)
1237 struct isl_upoly_rec
*rec
;
1239 if (isl_upoly_is_cst(up
))
1242 if (up
->var
< first
)
1245 if (exp
[up
->var
- first
] == up
->var
- first
)
1248 up
= isl_upoly_cow(up
);
1252 up
->var
= exp
[up
->var
- first
] + first
;
1254 rec
= isl_upoly_as_rec(up
);
1258 for (i
= 0; i
< rec
->n
; ++i
) {
1259 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1270 static __isl_give isl_qpolynomial
*with_merged_divs(
1271 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1272 __isl_take isl_qpolynomial
*qp2
),
1273 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1277 isl_mat
*div
= NULL
;
1279 qp1
= isl_qpolynomial_cow(qp1
);
1280 qp2
= isl_qpolynomial_cow(qp2
);
1285 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1286 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1288 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
1289 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
1293 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1297 isl_mat_free(qp1
->div
);
1298 qp1
->div
= isl_mat_copy(div
);
1299 isl_mat_free(qp2
->div
);
1300 qp2
->div
= isl_mat_copy(div
);
1302 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1303 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1305 if (!qp1
->upoly
|| !qp2
->upoly
)
1312 return fn(qp1
, qp2
);
1317 isl_qpolynomial_free(qp1
);
1318 isl_qpolynomial_free(qp2
);
1322 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1323 __isl_take isl_qpolynomial
*qp2
)
1325 qp1
= isl_qpolynomial_cow(qp1
);
1330 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1331 return isl_qpolynomial_add(qp2
, qp1
);
1333 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1334 if (!compatible_divs(qp1
->div
, qp2
->div
))
1335 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1337 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1341 isl_qpolynomial_free(qp2
);
1345 isl_qpolynomial_free(qp1
);
1346 isl_qpolynomial_free(qp2
);
1350 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1351 __isl_keep isl_set
*dom
,
1352 __isl_take isl_qpolynomial
*qp1
,
1353 __isl_take isl_qpolynomial
*qp2
)
1355 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1356 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1360 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1361 __isl_take isl_qpolynomial
*qp2
)
1363 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1366 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1367 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1369 if (isl_int_is_zero(v
))
1372 qp
= isl_qpolynomial_cow(qp
);
1376 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1382 isl_qpolynomial_free(qp
);
1387 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1392 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1395 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1396 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1398 if (isl_int_is_one(v
))
1401 if (qp
&& isl_int_is_zero(v
)) {
1402 isl_qpolynomial
*zero
;
1403 zero
= isl_qpolynomial_zero(isl_dim_copy(qp
->dim
));
1404 isl_qpolynomial_free(qp
);
1408 qp
= isl_qpolynomial_cow(qp
);
1412 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1418 isl_qpolynomial_free(qp
);
1422 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1423 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1425 return isl_qpolynomial_mul_isl_int(qp
, v
);
1428 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1429 __isl_take isl_qpolynomial
*qp2
)
1431 qp1
= isl_qpolynomial_cow(qp1
);
1436 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1437 return isl_qpolynomial_mul(qp2
, qp1
);
1439 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1440 if (!compatible_divs(qp1
->div
, qp2
->div
))
1441 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1443 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1447 isl_qpolynomial_free(qp2
);
1451 isl_qpolynomial_free(qp1
);
1452 isl_qpolynomial_free(qp2
);
1456 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1459 qp
= isl_qpolynomial_cow(qp
);
1464 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1470 isl_qpolynomial_free(qp
);
1474 __isl_give isl_qpolynomial
*isl_qpolynomial_zero(__isl_take isl_dim
*dim
)
1478 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1481 __isl_give isl_qpolynomial
*isl_qpolynomial_one(__isl_take isl_dim
*dim
)
1485 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1488 __isl_give isl_qpolynomial
*isl_qpolynomial_infty(__isl_take isl_dim
*dim
)
1492 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1495 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty(__isl_take isl_dim
*dim
)
1499 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1502 __isl_give isl_qpolynomial
*isl_qpolynomial_nan(__isl_take isl_dim
*dim
)
1506 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1509 __isl_give isl_qpolynomial
*isl_qpolynomial_cst(__isl_take isl_dim
*dim
,
1512 struct isl_qpolynomial
*qp
;
1513 struct isl_upoly_cst
*cst
;
1518 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1522 cst
= isl_upoly_as_cst(qp
->upoly
);
1523 isl_int_set(cst
->n
, v
);
1528 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1529 isl_int
*n
, isl_int
*d
)
1531 struct isl_upoly_cst
*cst
;
1536 if (!isl_upoly_is_cst(qp
->upoly
))
1539 cst
= isl_upoly_as_cst(qp
->upoly
);
1544 isl_int_set(*n
, cst
->n
);
1546 isl_int_set(*d
, cst
->d
);
1551 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1554 struct isl_upoly_rec
*rec
;
1562 rec
= isl_upoly_as_rec(up
);
1569 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1571 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1577 return isl_upoly_is_affine(rec
->p
[0]);
1580 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1585 if (qp
->div
->n_row
> 0)
1588 return isl_upoly_is_affine(qp
->upoly
);
1591 static void update_coeff(__isl_keep isl_vec
*aff
,
1592 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1597 if (isl_int_is_zero(cst
->n
))
1602 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1603 isl_int_divexact(f
, cst
->d
, gcd
);
1604 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1605 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1606 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1611 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1612 __isl_keep isl_vec
*aff
)
1614 struct isl_upoly_cst
*cst
;
1615 struct isl_upoly_rec
*rec
;
1621 struct isl_upoly_cst
*cst
;
1623 cst
= isl_upoly_as_cst(up
);
1626 update_coeff(aff
, cst
, 0);
1630 rec
= isl_upoly_as_rec(up
);
1633 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1635 cst
= isl_upoly_as_cst(rec
->p
[1]);
1638 update_coeff(aff
, cst
, 1 + up
->var
);
1640 return isl_upoly_update_affine(rec
->p
[0], aff
);
1643 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1644 __isl_keep isl_qpolynomial
*qp
)
1652 d
= isl_dim_total(qp
->dim
);
1653 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1657 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1658 isl_int_set_si(aff
->el
[0], 1);
1660 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1669 int isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1670 __isl_keep isl_qpolynomial
*qp2
)
1677 equal
= isl_dim_equal(qp1
->dim
, qp2
->dim
);
1678 if (equal
< 0 || !equal
)
1681 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
1682 if (equal
< 0 || !equal
)
1685 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1688 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1691 struct isl_upoly_rec
*rec
;
1693 if (isl_upoly_is_cst(up
)) {
1694 struct isl_upoly_cst
*cst
;
1695 cst
= isl_upoly_as_cst(up
);
1698 isl_int_lcm(*d
, *d
, cst
->d
);
1702 rec
= isl_upoly_as_rec(up
);
1706 for (i
= 0; i
< rec
->n
; ++i
)
1707 upoly_update_den(rec
->p
[i
], d
);
1710 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1712 isl_int_set_si(*d
, 1);
1715 upoly_update_den(qp
->upoly
, d
);
1718 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow(__isl_take isl_dim
*dim
,
1721 struct isl_ctx
*ctx
;
1728 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
1731 __isl_give isl_qpolynomial
*isl_qpolynomial_var(__isl_take isl_dim
*dim
,
1732 enum isl_dim_type type
, unsigned pos
)
1737 isl_assert(dim
->ctx
, isl_dim_size(dim
, isl_dim_in
) == 0, goto error
);
1738 isl_assert(dim
->ctx
, pos
< isl_dim_size(dim
, type
), goto error
);
1740 if (type
== isl_dim_set
)
1741 pos
+= isl_dim_size(dim
, isl_dim_param
);
1743 return isl_qpolynomial_var_pow(dim
, pos
, 1);
1749 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
1750 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
1753 struct isl_upoly_rec
*rec
;
1754 struct isl_upoly
*base
, *res
;
1759 if (isl_upoly_is_cst(up
))
1762 if (up
->var
< first
)
1765 rec
= isl_upoly_as_rec(up
);
1769 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1771 if (up
->var
>= first
+ n
)
1772 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
1774 base
= isl_upoly_copy(subs
[up
->var
- first
]);
1776 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
1777 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1778 struct isl_upoly
*t
;
1779 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
1780 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1781 res
= isl_upoly_sum(res
, t
);
1784 isl_upoly_free(base
);
1793 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
1794 isl_int denom
, unsigned len
)
1797 struct isl_upoly
*up
;
1799 isl_assert(ctx
, len
>= 1, return NULL
);
1801 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
1802 for (i
= 0; i
< len
- 1; ++i
) {
1803 struct isl_upoly
*t
;
1804 struct isl_upoly
*c
;
1806 if (isl_int_is_zero(f
[1 + i
]))
1809 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
1810 t
= isl_upoly_var_pow(ctx
, i
, 1);
1811 t
= isl_upoly_mul(c
, t
);
1812 up
= isl_upoly_sum(up
, t
);
1818 /* Remove common factor of non-constant terms and denominator.
1820 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
1822 isl_ctx
*ctx
= qp
->div
->ctx
;
1823 unsigned total
= qp
->div
->n_col
- 2;
1825 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
1826 isl_int_gcd(ctx
->normalize_gcd
,
1827 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
1828 if (isl_int_is_one(ctx
->normalize_gcd
))
1831 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
1832 ctx
->normalize_gcd
, total
);
1833 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
1834 ctx
->normalize_gcd
);
1835 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
1836 ctx
->normalize_gcd
);
1839 /* Replace the integer division identified by "div" by the polynomial "s".
1840 * The integer division is assumed not to appear in the definition
1841 * of any other integer divisions.
1843 static __isl_give isl_qpolynomial
*substitute_div(
1844 __isl_take isl_qpolynomial
*qp
,
1845 int div
, __isl_take
struct isl_upoly
*s
)
1854 qp
= isl_qpolynomial_cow(qp
);
1858 total
= isl_dim_total(qp
->dim
);
1859 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
1863 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
1866 for (i
= 0; i
< total
+ div
; ++i
)
1868 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
1869 reordering
[i
] = i
- 1;
1870 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
1871 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
1872 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1875 if (!qp
->upoly
|| !qp
->div
)
1881 isl_qpolynomial_free(qp
);
1886 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1887 * divisions because d is equal to 1 by their definition, i.e., e.
1889 static __isl_give isl_qpolynomial
*substitute_non_divs(
1890 __isl_take isl_qpolynomial
*qp
)
1894 struct isl_upoly
*s
;
1899 total
= isl_dim_total(qp
->dim
);
1900 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
1901 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
1903 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
1904 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
1906 isl_seq_combine(qp
->div
->row
[j
] + 1,
1907 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
1908 qp
->div
->row
[j
][2 + total
+ i
],
1909 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
1910 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
1911 normalize_div(qp
, j
);
1913 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
1914 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
1915 qp
= substitute_div(qp
, i
, s
);
1922 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1923 * with d the denominator. When replacing the coefficient e of x by
1924 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1925 * inside the division, so we need to add floor(e/d) * x outside.
1926 * That is, we replace q by q' + floor(e/d) * x and we therefore need
1927 * to adjust the coefficient of x in each later div that depends on the
1928 * current div "div" and also in the affine expression "aff"
1929 * (if it too depends on "div").
1931 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
1932 __isl_keep isl_vec
*aff
)
1936 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
1939 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
1940 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
1941 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
1943 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
1944 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
1945 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
1946 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
1947 isl_int_addmul(aff
->el
[i
], v
, aff
->el
[1 + total
+ div
]);
1948 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
1949 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
1951 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
1952 v
, qp
->div
->row
[j
][2 + total
+ div
]);
1958 /* Check if the last non-zero coefficient is bigger that half of the
1959 * denominator. If so, we will invert the div to further reduce the number
1960 * of distinct divs that may appear.
1961 * If the last non-zero coefficient is exactly half the denominator,
1962 * then we continue looking for earlier coefficients that are bigger
1963 * than half the denominator.
1965 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
1970 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
1971 if (isl_int_is_zero(div
->row
[row
][i
]))
1973 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
1974 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
1975 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
1985 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
1986 * We only invert the coefficients of e (and the coefficient of q in
1987 * later divs and in "aff"). After calling this function, the
1988 * coefficients of e should be reduced again.
1990 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
1991 __isl_keep isl_vec
*aff
)
1993 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
1995 isl_seq_neg(qp
->div
->row
[div
] + 1,
1996 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
1997 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
1998 isl_int_add(qp
->div
->row
[div
][1],
1999 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2000 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2001 isl_int_neg(aff
->el
[1 + total
+ div
], aff
->el
[1 + total
+ div
]);
2002 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2003 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2006 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2007 * in the interval [0, d-1], with d the denominator and such that the
2008 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2010 * After the reduction, some divs may have become redundant or identical,
2011 * so we call substitute_non_divs and sort_divs. If these functions
2012 * eliminate divs or merge two or more divs into one, the coefficients
2013 * of the enclosing divs may have to be reduced again, so we call
2014 * ourselves recursively if the number of divs decreases.
2016 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2019 isl_vec
*aff
= NULL
;
2020 struct isl_upoly
*s
;
2026 aff
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
2027 aff
= isl_vec_clr(aff
);
2031 isl_int_set_si(aff
->el
[1 + qp
->upoly
->var
], 1);
2033 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2034 normalize_div(qp
, i
);
2035 reduce_div(qp
, i
, aff
);
2036 if (needs_invert(qp
->div
, i
)) {
2037 invert_div(qp
, i
, aff
);
2038 reduce_div(qp
, i
, aff
);
2042 s
= isl_upoly_from_affine(qp
->div
->ctx
, aff
->el
,
2043 qp
->div
->ctx
->one
, aff
->size
);
2044 qp
->upoly
= isl_upoly_subs(qp
->upoly
, qp
->upoly
->var
, 1, &s
);
2051 n_div
= qp
->div
->n_row
;
2052 qp
= substitute_non_divs(qp
);
2054 if (qp
&& qp
->div
->n_row
< n_div
)
2055 return reduce_divs(qp
);
2059 isl_qpolynomial_free(qp
);
2064 /* Assumes each div only depends on earlier divs.
2066 __isl_give isl_qpolynomial
*isl_qpolynomial_div_pow(__isl_take isl_div
*div
,
2069 struct isl_qpolynomial
*qp
= NULL
;
2070 struct isl_upoly_rec
*rec
;
2071 struct isl_upoly_cst
*cst
;
2078 d
= div
->line
- div
->bmap
->div
;
2080 pos
= isl_dim_total(div
->bmap
->dim
) + d
;
2081 rec
= isl_upoly_alloc_rec(div
->ctx
, pos
, 1 + power
);
2082 qp
= isl_qpolynomial_alloc(isl_basic_map_get_dim(div
->bmap
),
2083 div
->bmap
->n_div
, &rec
->up
);
2087 for (i
= 0; i
< div
->bmap
->n_div
; ++i
)
2088 isl_seq_cpy(qp
->div
->row
[i
], div
->bmap
->div
[i
], qp
->div
->n_col
);
2090 for (i
= 0; i
< 1 + power
; ++i
) {
2091 rec
->p
[i
] = isl_upoly_zero(div
->ctx
);
2096 cst
= isl_upoly_as_cst(rec
->p
[power
]);
2097 isl_int_set_si(cst
->n
, 1);
2101 qp
= reduce_divs(qp
);
2105 isl_qpolynomial_free(qp
);
2110 __isl_give isl_qpolynomial
*isl_qpolynomial_div(__isl_take isl_div
*div
)
2112 return isl_qpolynomial_div_pow(div
, 1);
2115 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst(__isl_take isl_dim
*dim
,
2116 const isl_int n
, const isl_int d
)
2118 struct isl_qpolynomial
*qp
;
2119 struct isl_upoly_cst
*cst
;
2121 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2125 cst
= isl_upoly_as_cst(qp
->upoly
);
2126 isl_int_set(cst
->n
, n
);
2127 isl_int_set(cst
->d
, d
);
2132 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2134 struct isl_upoly_rec
*rec
;
2140 if (isl_upoly_is_cst(up
))
2144 active
[up
->var
] = 1;
2146 rec
= isl_upoly_as_rec(up
);
2147 for (i
= 0; i
< rec
->n
; ++i
)
2148 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2154 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2157 int d
= isl_dim_total(qp
->dim
);
2162 for (i
= 0; i
< d
; ++i
)
2163 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2164 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2170 return up_set_active(qp
->upoly
, active
, d
);
2173 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2174 enum isl_dim_type type
, unsigned first
, unsigned n
)
2185 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
2187 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2188 type
== isl_dim_set
, return -1);
2190 active
= isl_calloc_array(qp
->dim
->ctx
, int, isl_dim_total(qp
->dim
));
2191 if (set_active(qp
, active
) < 0)
2194 if (type
== isl_dim_set
)
2195 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
2196 for (i
= 0; i
< n
; ++i
)
2197 if (active
[first
+ i
]) {
2210 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2211 * of the divs that do appear in the quasi-polynomial.
2213 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2214 __isl_take isl_qpolynomial
*qp
)
2221 int *reordering
= NULL
;
2228 if (qp
->div
->n_row
== 0)
2231 d
= isl_dim_total(qp
->dim
);
2232 len
= qp
->div
->n_col
- 2;
2233 ctx
= isl_qpolynomial_get_ctx(qp
);
2234 active
= isl_calloc_array(ctx
, int, len
);
2238 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2241 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2242 if (!active
[d
+ i
]) {
2246 for (j
= 0; j
< i
; ++j
) {
2247 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2259 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2263 for (i
= 0; i
< d
; ++i
)
2267 n_div
= qp
->div
->n_row
;
2268 for (i
= 0; i
< n_div
; ++i
) {
2269 if (!active
[d
+ i
]) {
2270 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2271 qp
->div
= isl_mat_drop_cols(qp
->div
,
2272 2 + d
+ i
- skip
, 1);
2275 reordering
[d
+ i
] = d
+ i
- skip
;
2278 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2280 if (!qp
->upoly
|| !qp
->div
)
2290 isl_qpolynomial_free(qp
);
2294 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2295 unsigned first
, unsigned n
)
2298 struct isl_upoly_rec
*rec
;
2302 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2304 if (up
->var
< first
+ n
) {
2305 up
= replace_by_constant_term(up
);
2306 return isl_upoly_drop(up
, first
, n
);
2308 up
= isl_upoly_cow(up
);
2312 rec
= isl_upoly_as_rec(up
);
2316 for (i
= 0; i
< rec
->n
; ++i
) {
2317 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2328 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2329 __isl_take isl_qpolynomial
*qp
,
2330 enum isl_dim_type type
, unsigned pos
, const char *s
)
2332 qp
= isl_qpolynomial_cow(qp
);
2335 qp
->dim
= isl_dim_set_name(qp
->dim
, type
, pos
, s
);
2340 isl_qpolynomial_free(qp
);
2344 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2345 __isl_take isl_qpolynomial
*qp
,
2346 enum isl_dim_type type
, unsigned first
, unsigned n
)
2350 if (n
== 0 && !isl_dim_is_named_or_nested(qp
->dim
, type
))
2353 qp
= isl_qpolynomial_cow(qp
);
2357 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
2359 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2360 type
== isl_dim_set
, goto error
);
2362 qp
->dim
= isl_dim_drop(qp
->dim
, type
, first
, n
);
2366 if (type
== isl_dim_set
)
2367 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
2369 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2373 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2379 isl_qpolynomial_free(qp
);
2383 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2384 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2390 struct isl_upoly
*up
;
2394 if (eq
->n_eq
== 0) {
2395 isl_basic_set_free(eq
);
2399 qp
= isl_qpolynomial_cow(qp
);
2402 qp
->div
= isl_mat_cow(qp
->div
);
2406 total
= 1 + isl_dim_total(eq
->dim
);
2408 isl_int_init(denom
);
2409 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2410 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2411 if (j
< 0 || j
== 0 || j
>= total
)
2414 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2415 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2417 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2418 &qp
->div
->row
[k
][0]);
2419 normalize_div(qp
, k
);
2422 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2423 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2424 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2425 isl_int_set_si(eq
->eq
[i
][j
], 0);
2427 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2428 eq
->eq
[i
], denom
, total
);
2429 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2432 isl_int_clear(denom
);
2437 isl_basic_set_free(eq
);
2439 qp
= substitute_non_divs(qp
);
2444 isl_basic_set_free(eq
);
2445 isl_qpolynomial_free(qp
);
2449 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2451 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2452 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2456 if (qp
->div
->n_row
> 0)
2457 eq
= isl_basic_set_add(eq
, isl_dim_set
, qp
->div
->n_row
);
2458 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2460 isl_basic_set_free(eq
);
2461 isl_qpolynomial_free(qp
);
2465 static __isl_give isl_basic_set
*add_div_constraints(
2466 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2474 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2477 total
= isl_basic_set_total_dim(bset
);
2478 for (i
= 0; i
< div
->n_row
; ++i
)
2479 if (isl_basic_set_add_div_constraints_var(bset
,
2480 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2487 isl_basic_set_free(bset
);
2491 /* Look for equalities among the variables shared by context and qp
2492 * and the integer divisions of qp, if any.
2493 * The equalities are then used to eliminate variables and/or integer
2494 * divisions from qp.
2496 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2497 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2503 if (qp
->div
->n_row
> 0) {
2504 isl_basic_set
*bset
;
2505 context
= isl_set_add_dims(context
, isl_dim_set
,
2507 bset
= isl_basic_set_universe(isl_set_get_dim(context
));
2508 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2509 context
= isl_set_intersect(context
,
2510 isl_set_from_basic_set(bset
));
2513 aff
= isl_set_affine_hull(context
);
2514 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2516 isl_qpolynomial_free(qp
);
2517 isl_set_free(context
);
2522 #define PW isl_pw_qpolynomial
2524 #define EL isl_qpolynomial
2526 #define EL_IS_ZERO is_zero
2530 #define IS_ZERO is_zero
2534 #include <isl_pw_templ.c>
2537 #define UNION isl_union_pw_qpolynomial
2539 #define PART isl_pw_qpolynomial
2541 #define PARTS pw_qpolynomial
2543 #include <isl_union_templ.c>
2545 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2553 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2556 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2559 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2560 __isl_take isl_pw_qpolynomial
*pwqp1
,
2561 __isl_take isl_pw_qpolynomial
*pwqp2
)
2564 struct isl_pw_qpolynomial
*res
;
2566 if (!pwqp1
|| !pwqp2
)
2569 isl_assert(pwqp1
->dim
->ctx
, isl_dim_equal(pwqp1
->dim
, pwqp2
->dim
),
2572 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
2573 isl_pw_qpolynomial_free(pwqp2
);
2577 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2578 isl_pw_qpolynomial_free(pwqp1
);
2582 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2583 isl_pw_qpolynomial_free(pwqp1
);
2587 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
2588 isl_pw_qpolynomial_free(pwqp2
);
2592 n
= pwqp1
->n
* pwqp2
->n
;
2593 res
= isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1
->dim
), n
);
2595 for (i
= 0; i
< pwqp1
->n
; ++i
) {
2596 for (j
= 0; j
< pwqp2
->n
; ++j
) {
2597 struct isl_set
*common
;
2598 struct isl_qpolynomial
*prod
;
2599 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
2600 isl_set_copy(pwqp2
->p
[j
].set
));
2601 if (isl_set_plain_is_empty(common
)) {
2602 isl_set_free(common
);
2606 prod
= isl_qpolynomial_mul(
2607 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
2608 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
2610 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
2614 isl_pw_qpolynomial_free(pwqp1
);
2615 isl_pw_qpolynomial_free(pwqp2
);
2619 isl_pw_qpolynomial_free(pwqp1
);
2620 isl_pw_qpolynomial_free(pwqp2
);
2624 __isl_give
struct isl_upoly
*isl_upoly_eval(
2625 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
2628 struct isl_upoly_rec
*rec
;
2629 struct isl_upoly
*res
;
2630 struct isl_upoly
*base
;
2632 if (isl_upoly_is_cst(up
)) {
2637 rec
= isl_upoly_as_rec(up
);
2641 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2643 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
2645 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
2648 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2649 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2650 res
= isl_upoly_sum(res
,
2651 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
2652 isl_vec_copy(vec
)));
2655 isl_upoly_free(base
);
2665 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
2666 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
2669 struct isl_upoly
*up
;
2674 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, qp
->dim
), goto error
);
2676 if (qp
->div
->n_row
== 0)
2677 ext
= isl_vec_copy(pnt
->vec
);
2680 unsigned dim
= isl_dim_total(qp
->dim
);
2681 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
2685 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
2686 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2687 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
2688 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
2689 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
2690 qp
->div
->row
[i
][0]);
2694 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
2698 dim
= isl_dim_copy(qp
->dim
);
2699 isl_qpolynomial_free(qp
);
2700 isl_point_free(pnt
);
2702 return isl_qpolynomial_alloc(dim
, 0, up
);
2704 isl_qpolynomial_free(qp
);
2705 isl_point_free(pnt
);
2709 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
2710 __isl_keep
struct isl_upoly_cst
*cst2
)
2715 isl_int_mul(t
, cst1
->n
, cst2
->d
);
2716 isl_int_submul(t
, cst2
->n
, cst1
->d
);
2717 cmp
= isl_int_sgn(t
);
2722 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial
*qp1
,
2723 __isl_keep isl_qpolynomial
*qp2
)
2725 struct isl_upoly_cst
*cst1
, *cst2
;
2729 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), return -1);
2730 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), return -1);
2731 if (isl_qpolynomial_is_nan(qp1
))
2733 if (isl_qpolynomial_is_nan(qp2
))
2735 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2736 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2738 return isl_upoly_cmp(cst1
, cst2
) <= 0;
2741 __isl_give isl_qpolynomial
*isl_qpolynomial_min_cst(
2742 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2744 struct isl_upoly_cst
*cst1
, *cst2
;
2749 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2750 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2751 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2752 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2753 cmp
= isl_upoly_cmp(cst1
, cst2
);
2756 isl_qpolynomial_free(qp2
);
2758 isl_qpolynomial_free(qp1
);
2763 isl_qpolynomial_free(qp1
);
2764 isl_qpolynomial_free(qp2
);
2768 __isl_give isl_qpolynomial
*isl_qpolynomial_max_cst(
2769 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2771 struct isl_upoly_cst
*cst1
, *cst2
;
2776 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2777 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2778 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2779 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2780 cmp
= isl_upoly_cmp(cst1
, cst2
);
2783 isl_qpolynomial_free(qp2
);
2785 isl_qpolynomial_free(qp1
);
2790 isl_qpolynomial_free(qp1
);
2791 isl_qpolynomial_free(qp2
);
2795 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
2796 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
2797 unsigned first
, unsigned n
)
2803 if (n
== 0 && !isl_dim_is_named_or_nested(qp
->dim
, type
))
2806 qp
= isl_qpolynomial_cow(qp
);
2810 isl_assert(qp
->div
->ctx
, first
<= isl_dim_size(qp
->dim
, type
),
2813 g_pos
= pos(qp
->dim
, type
) + first
;
2815 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
2819 total
= qp
->div
->n_col
- 2;
2820 if (total
> g_pos
) {
2822 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
2825 for (i
= 0; i
< total
- g_pos
; ++i
)
2827 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
2833 qp
->dim
= isl_dim_insert(qp
->dim
, type
, first
, n
);
2839 isl_qpolynomial_free(qp
);
2843 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
2844 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
2848 pos
= isl_qpolynomial_dim(qp
, type
);
2850 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
2853 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
2854 __isl_take isl_pw_qpolynomial
*pwqp
,
2855 enum isl_dim_type type
, unsigned n
)
2859 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
2861 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
2864 static int *reordering_move(isl_ctx
*ctx
,
2865 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
2870 reordering
= isl_alloc_array(ctx
, int, len
);
2875 for (i
= 0; i
< dst
; ++i
)
2877 for (i
= 0; i
< n
; ++i
)
2878 reordering
[src
+ i
] = dst
+ i
;
2879 for (i
= 0; i
< src
- dst
; ++i
)
2880 reordering
[dst
+ i
] = dst
+ n
+ i
;
2881 for (i
= 0; i
< len
- src
- n
; ++i
)
2882 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
2884 for (i
= 0; i
< src
; ++i
)
2886 for (i
= 0; i
< n
; ++i
)
2887 reordering
[src
+ i
] = dst
+ i
;
2888 for (i
= 0; i
< dst
- src
; ++i
)
2889 reordering
[src
+ n
+ i
] = src
+ i
;
2890 for (i
= 0; i
< len
- dst
- n
; ++i
)
2891 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
2897 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
2898 __isl_take isl_qpolynomial
*qp
,
2899 enum isl_dim_type dst_type
, unsigned dst_pos
,
2900 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2906 qp
= isl_qpolynomial_cow(qp
);
2910 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_dim_size(qp
->dim
, src_type
),
2913 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
2914 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
2915 if (dst_type
> src_type
)
2918 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
2925 reordering
= reordering_move(qp
->dim
->ctx
,
2926 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
2930 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2935 qp
->dim
= isl_dim_move(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2941 isl_qpolynomial_free(qp
);
2945 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_dim
*dim
,
2946 isl_int
*f
, isl_int denom
)
2948 struct isl_upoly
*up
;
2953 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
, 1 + isl_dim_total(dim
));
2955 return isl_qpolynomial_alloc(dim
, 0, up
);
2958 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
2961 struct isl_upoly
*up
;
2962 isl_qpolynomial
*qp
;
2967 ctx
= isl_aff_get_ctx(aff
);
2968 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
2971 qp
= isl_qpolynomial_alloc(isl_aff_get_dim(aff
),
2972 aff
->ls
->div
->n_row
, up
);
2976 isl_mat_free(qp
->div
);
2977 qp
->div
= isl_mat_copy(aff
->ls
->div
);
2978 qp
->div
= isl_mat_cow(qp
->div
);
2983 qp
= reduce_divs(qp
);
2984 qp
= remove_redundant_divs(qp
);
2991 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
2992 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
2996 aff
= isl_constraint_get_bound(c
, type
, pos
);
2997 isl_constraint_free(c
);
2998 return isl_qpolynomial_from_aff(aff
);
3001 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3002 * in "qp" by subs[i].
3004 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3005 __isl_take isl_qpolynomial
*qp
,
3006 enum isl_dim_type type
, unsigned first
, unsigned n
,
3007 __isl_keep isl_qpolynomial
**subs
)
3010 struct isl_upoly
**ups
;
3015 qp
= isl_qpolynomial_cow(qp
);
3018 for (i
= 0; i
< n
; ++i
)
3022 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
3025 for (i
= 0; i
< n
; ++i
)
3026 isl_assert(qp
->dim
->ctx
, isl_dim_equal(qp
->dim
, subs
[i
]->dim
),
3029 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3030 for (i
= 0; i
< n
; ++i
)
3031 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3033 first
+= pos(qp
->dim
, type
);
3035 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3038 for (i
= 0; i
< n
; ++i
)
3039 ups
[i
] = subs
[i
]->upoly
;
3041 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3050 isl_qpolynomial_free(qp
);
3054 /* Extend "bset" with extra set dimensions for each integer division
3055 * in "qp" and then call "fn" with the extended bset and the polynomial
3056 * that results from replacing each of the integer divisions by the
3057 * corresponding extra set dimension.
3059 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3060 __isl_keep isl_basic_set
*bset
,
3061 int (*fn
)(__isl_take isl_basic_set
*bset
,
3062 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3066 isl_qpolynomial
*poly
;
3070 if (qp
->div
->n_row
== 0)
3071 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3074 div
= isl_mat_copy(qp
->div
);
3075 dim
= isl_dim_copy(qp
->dim
);
3076 dim
= isl_dim_add(dim
, isl_dim_set
, qp
->div
->n_row
);
3077 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3078 bset
= isl_basic_set_copy(bset
);
3079 bset
= isl_basic_set_add(bset
, isl_dim_set
, qp
->div
->n_row
);
3080 bset
= add_div_constraints(bset
, div
);
3082 return fn(bset
, poly
, user
);
3087 /* Return total degree in variables first (inclusive) up to last (exclusive).
3089 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3093 struct isl_upoly_rec
*rec
;
3097 if (isl_upoly_is_zero(up
))
3099 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3102 rec
= isl_upoly_as_rec(up
);
3106 for (i
= 0; i
< rec
->n
; ++i
) {
3109 if (isl_upoly_is_zero(rec
->p
[i
]))
3111 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3121 /* Return total degree in set variables.
3123 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3131 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
3132 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
3133 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3136 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3137 unsigned pos
, int deg
)
3140 struct isl_upoly_rec
*rec
;
3145 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3147 return isl_upoly_copy(up
);
3149 return isl_upoly_zero(up
->ctx
);
3152 rec
= isl_upoly_as_rec(up
);
3156 if (up
->var
== pos
) {
3158 return isl_upoly_copy(rec
->p
[deg
]);
3160 return isl_upoly_zero(up
->ctx
);
3163 up
= isl_upoly_copy(up
);
3164 up
= isl_upoly_cow(up
);
3165 rec
= isl_upoly_as_rec(up
);
3169 for (i
= 0; i
< rec
->n
; ++i
) {
3170 struct isl_upoly
*t
;
3171 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3174 isl_upoly_free(rec
->p
[i
]);
3184 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3186 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3187 __isl_keep isl_qpolynomial
*qp
,
3188 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3191 struct isl_upoly
*up
;
3197 isl_assert(qp
->div
->ctx
, t_pos
< isl_dim_size(qp
->dim
, type
),
3200 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3201 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3203 c
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
, up
);
3206 isl_mat_free(c
->div
);
3207 c
->div
= isl_mat_copy(qp
->div
);
3212 isl_qpolynomial_free(c
);
3216 /* Homogenize the polynomial in the variables first (inclusive) up to
3217 * last (exclusive) by inserting powers of variable first.
3218 * Variable first is assumed not to appear in the input.
3220 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3221 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3222 int first
, int last
)
3225 struct isl_upoly_rec
*rec
;
3229 if (isl_upoly_is_zero(up
))
3233 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3234 struct isl_upoly
*hom
;
3236 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3239 rec
= isl_upoly_as_rec(hom
);
3240 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3245 up
= isl_upoly_cow(up
);
3246 rec
= isl_upoly_as_rec(up
);
3250 for (i
= 0; i
< rec
->n
; ++i
) {
3251 if (isl_upoly_is_zero(rec
->p
[i
]))
3253 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3254 up
->var
< last
? deg
+ i
: i
, target
,
3266 /* Homogenize the polynomial in the set variables by introducing
3267 * powers of an extra set variable at position 0.
3269 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3270 __isl_take isl_qpolynomial
*poly
)
3274 int deg
= isl_qpolynomial_degree(poly
);
3279 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_set
, 0, 1);
3280 poly
= isl_qpolynomial_cow(poly
);
3284 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
3285 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
3286 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3293 isl_qpolynomial_free(poly
);
3297 __isl_give isl_term
*isl_term_alloc(__isl_take isl_dim
*dim
,
3298 __isl_take isl_mat
*div
)
3306 n
= isl_dim_total(dim
) + div
->n_row
;
3308 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3309 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3316 isl_int_init(term
->n
);
3317 isl_int_init(term
->d
);
3326 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3335 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3344 total
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
3346 dup
= isl_term_alloc(isl_dim_copy(term
->dim
), isl_mat_copy(term
->div
));
3350 isl_int_set(dup
->n
, term
->n
);
3351 isl_int_set(dup
->d
, term
->d
);
3353 for (i
= 0; i
< total
; ++i
)
3354 dup
->pow
[i
] = term
->pow
[i
];
3359 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3367 return isl_term_dup(term
);
3370 void isl_term_free(__isl_take isl_term
*term
)
3375 if (--term
->ref
> 0)
3378 isl_dim_free(term
->dim
);
3379 isl_mat_free(term
->div
);
3380 isl_int_clear(term
->n
);
3381 isl_int_clear(term
->d
);
3385 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3393 case isl_dim_out
: return isl_dim_size(term
->dim
, type
);
3394 case isl_dim_div
: return term
->div
->n_row
;
3395 case isl_dim_all
: return isl_dim_total(term
->dim
) + term
->div
->n_row
;
3400 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3402 return term
? term
->dim
->ctx
: NULL
;
3405 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3409 isl_int_set(*n
, term
->n
);
3412 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
3416 isl_int_set(*d
, term
->d
);
3419 int isl_term_get_exp(__isl_keep isl_term
*term
,
3420 enum isl_dim_type type
, unsigned pos
)
3425 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3427 if (type
>= isl_dim_set
)
3428 pos
+= isl_dim_size(term
->dim
, isl_dim_param
);
3429 if (type
>= isl_dim_div
)
3430 pos
+= isl_dim_size(term
->dim
, isl_dim_set
);
3432 return term
->pow
[pos
];
3435 __isl_give isl_div
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3437 isl_basic_map
*bmap
;
3444 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3447 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
3448 /* No nested divs for now */
3449 isl_assert(term
->dim
->ctx
,
3450 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
3451 term
->div
->n_row
) == -1,
3454 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(term
->dim
), 1, 0, 0);
3455 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
3458 isl_seq_cpy(bmap
->div
[k
], term
->div
->row
[pos
], 2 + total
);
3460 return isl_basic_map_div(bmap
, k
);
3462 isl_basic_map_free(bmap
);
3466 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3467 int (*fn
)(__isl_take isl_term
*term
, void *user
),
3468 __isl_take isl_term
*term
, void *user
)
3471 struct isl_upoly_rec
*rec
;
3476 if (isl_upoly_is_zero(up
))
3479 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3480 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3481 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3483 if (isl_upoly_is_cst(up
)) {
3484 struct isl_upoly_cst
*cst
;
3485 cst
= isl_upoly_as_cst(up
);
3488 term
= isl_term_cow(term
);
3491 isl_int_set(term
->n
, cst
->n
);
3492 isl_int_set(term
->d
, cst
->d
);
3493 if (fn(isl_term_copy(term
), user
) < 0)
3498 rec
= isl_upoly_as_rec(up
);
3502 for (i
= 0; i
< rec
->n
; ++i
) {
3503 term
= isl_term_cow(term
);
3506 term
->pow
[up
->var
] = i
;
3507 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3511 term
->pow
[up
->var
] = 0;
3515 isl_term_free(term
);
3519 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3520 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3527 term
= isl_term_alloc(isl_dim_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3531 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3533 isl_term_free(term
);
3535 return term
? 0 : -1;
3538 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3540 struct isl_upoly
*up
;
3541 isl_qpolynomial
*qp
;
3547 n
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
3549 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3550 for (i
= 0; i
< n
; ++i
) {
3553 up
= isl_upoly_mul(up
,
3554 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3557 qp
= isl_qpolynomial_alloc(isl_dim_copy(term
->dim
), term
->div
->n_row
, up
);
3560 isl_mat_free(qp
->div
);
3561 qp
->div
= isl_mat_copy(term
->div
);
3565 isl_term_free(term
);
3568 isl_qpolynomial_free(qp
);
3569 isl_term_free(term
);
3573 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3574 __isl_take isl_dim
*dim
)
3583 if (isl_dim_equal(qp
->dim
, dim
)) {
3588 qp
= isl_qpolynomial_cow(qp
);
3592 extra
= isl_dim_size(dim
, isl_dim_set
) -
3593 isl_dim_size(qp
->dim
, isl_dim_set
);
3594 total
= isl_dim_total(qp
->dim
);
3595 if (qp
->div
->n_row
) {
3598 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
3601 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3603 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
3608 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
3611 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3612 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
3614 isl_dim_free(qp
->dim
);
3620 isl_qpolynomial_free(qp
);
3624 /* For each parameter or variable that does not appear in qp,
3625 * first eliminate the variable from all constraints and then set it to zero.
3627 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3628 __isl_keep isl_qpolynomial
*qp
)
3639 d
= isl_dim_total(set
->dim
);
3640 active
= isl_calloc_array(set
->ctx
, int, d
);
3641 if (set_active(qp
, active
) < 0)
3644 for (i
= 0; i
< d
; ++i
)
3653 nparam
= isl_dim_size(set
->dim
, isl_dim_param
);
3654 nvar
= isl_dim_size(set
->dim
, isl_dim_set
);
3655 for (i
= 0; i
< nparam
; ++i
) {
3658 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
3659 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
3661 for (i
= 0; i
< nvar
; ++i
) {
3662 if (active
[nparam
+ i
])
3664 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
3665 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
3677 struct isl_opt_data
{
3678 isl_qpolynomial
*qp
;
3680 isl_qpolynomial
*opt
;
3684 static int opt_fn(__isl_take isl_point
*pnt
, void *user
)
3686 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
3687 isl_qpolynomial
*val
;
3689 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
3693 } else if (data
->max
) {
3694 data
->opt
= isl_qpolynomial_max_cst(data
->opt
, val
);
3696 data
->opt
= isl_qpolynomial_min_cst(data
->opt
, val
);
3702 __isl_give isl_qpolynomial
*isl_qpolynomial_opt_on_domain(
3703 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
3705 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
3710 if (isl_upoly_is_cst(qp
->upoly
)) {
3715 set
= fix_inactive(set
, qp
);
3718 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
3722 data
.opt
= isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp
));
3725 isl_qpolynomial_free(qp
);
3729 isl_qpolynomial_free(qp
);
3730 isl_qpolynomial_free(data
.opt
);
3734 __isl_give isl_qpolynomial
*isl_qpolynomial_morph(__isl_take isl_qpolynomial
*qp
,
3735 __isl_take isl_morph
*morph
)
3740 struct isl_upoly
**subs
;
3743 qp
= isl_qpolynomial_cow(qp
);
3748 isl_assert(ctx
, isl_dim_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
3750 n_sub
= morph
->inv
->n_row
- 1;
3751 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
3752 n_sub
+= qp
->div
->n_row
;
3753 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
3757 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3758 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
3759 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
3760 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
3761 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3762 subs
[morph
->inv
->n_row
- 1 + i
] =
3763 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
3765 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
3767 for (i
= 0; i
< n_sub
; ++i
)
3768 isl_upoly_free(subs
[i
]);
3771 mat
= isl_mat_diagonal(isl_mat_identity(ctx
, 1), isl_mat_copy(morph
->inv
));
3772 mat
= isl_mat_diagonal(mat
, isl_mat_identity(ctx
, qp
->div
->n_row
));
3773 qp
->div
= isl_mat_product(qp
->div
, mat
);
3774 isl_dim_free(qp
->dim
);
3775 qp
->dim
= isl_dim_copy(morph
->ran
->dim
);
3777 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
3780 isl_morph_free(morph
);
3784 isl_qpolynomial_free(qp
);
3785 isl_morph_free(morph
);
3789 static int neg_entry(void **entry
, void *user
)
3791 isl_pw_qpolynomial
**pwqp
= (isl_pw_qpolynomial
**)entry
;
3793 *pwqp
= isl_pw_qpolynomial_neg(*pwqp
);
3795 return *pwqp
? 0 : -1;
3798 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_neg(
3799 __isl_take isl_union_pw_qpolynomial
*upwqp
)
3801 upwqp
= isl_union_pw_qpolynomial_cow(upwqp
);
3805 if (isl_hash_table_foreach(upwqp
->dim
->ctx
, &upwqp
->table
,
3806 &neg_entry
, NULL
) < 0)
3811 isl_union_pw_qpolynomial_free(upwqp
);
3815 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_sub(
3816 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3817 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3819 return isl_union_pw_qpolynomial_add(upwqp1
,
3820 isl_union_pw_qpolynomial_neg(upwqp2
));
3823 static int mul_entry(void **entry
, void *user
)
3825 struct isl_union_pw_qpolynomial_match_bin_data
*data
= user
;
3827 struct isl_hash_table_entry
*entry2
;
3828 isl_pw_qpolynomial
*pwpq
= *entry
;
3831 hash
= isl_dim_get_hash(pwpq
->dim
);
3832 entry2
= isl_hash_table_find(data
->u2
->dim
->ctx
, &data
->u2
->table
,
3833 hash
, &has_dim
, pwpq
->dim
, 0);
3837 pwpq
= isl_pw_qpolynomial_copy(pwpq
);
3838 pwpq
= isl_pw_qpolynomial_mul(pwpq
,
3839 isl_pw_qpolynomial_copy(entry2
->data
));
3841 empty
= isl_pw_qpolynomial_is_zero(pwpq
);
3843 isl_pw_qpolynomial_free(pwpq
);
3847 isl_pw_qpolynomial_free(pwpq
);
3851 data
->res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(data
->res
, pwpq
);
3856 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
3857 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3858 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3860 return match_bin_op(upwqp1
, upwqp2
, &mul_entry
);
3863 /* Reorder the columns of the given div definitions according to the
3866 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
3867 __isl_take isl_reordering
*r
)
3876 extra
= isl_dim_total(r
->dim
) + div
->n_row
- r
->len
;
3877 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
3881 for (i
= 0; i
< div
->n_row
; ++i
) {
3882 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
3883 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
3884 for (j
= 0; j
< r
->len
; ++j
)
3885 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
3886 div
->row
[i
][2 + j
]);
3889 isl_reordering_free(r
);
3893 isl_reordering_free(r
);
3898 /* Reorder the dimension of "qp" according to the given reordering.
3900 __isl_give isl_qpolynomial
*isl_qpolynomial_realign(
3901 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
3903 qp
= isl_qpolynomial_cow(qp
);
3907 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
3911 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
3915 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
3919 qp
= isl_qpolynomial_reset_dim(qp
, isl_dim_copy(r
->dim
));
3921 isl_reordering_free(r
);
3924 isl_qpolynomial_free(qp
);
3925 isl_reordering_free(r
);
3929 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
3930 __isl_take isl_qpolynomial
*qp
, __isl_take isl_dim
*model
)
3935 if (!isl_dim_match(qp
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
3936 isl_reordering
*exp
;
3938 model
= isl_dim_drop(model
, isl_dim_in
,
3939 0, isl_dim_size(model
, isl_dim_in
));
3940 model
= isl_dim_drop(model
, isl_dim_out
,
3941 0, isl_dim_size(model
, isl_dim_out
));
3942 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
3943 exp
= isl_reordering_extend_dim(exp
,
3944 isl_qpolynomial_get_dim(qp
));
3945 qp
= isl_qpolynomial_realign(qp
, exp
);
3948 isl_dim_free(model
);
3951 isl_dim_free(model
);
3952 isl_qpolynomial_free(qp
);
3956 struct isl_split_periods_data
{
3958 isl_pw_qpolynomial
*res
;
3961 /* Create a slice where the integer division "div" has the fixed value "v".
3962 * In particular, if "div" refers to floor(f/m), then create a slice
3964 * m v <= f <= m v + (m - 1)
3969 * -f + m v + (m - 1) >= 0
3971 static __isl_give isl_set
*set_div_slice(__isl_take isl_dim
*dim
,
3972 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
3975 isl_basic_set
*bset
= NULL
;
3981 total
= isl_dim_total(dim
);
3982 bset
= isl_basic_set_alloc_dim(isl_dim_copy(dim
), 0, 0, 2);
3984 k
= isl_basic_set_alloc_inequality(bset
);
3987 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
3988 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
3990 k
= isl_basic_set_alloc_inequality(bset
);
3993 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
3994 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
3995 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
3996 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
3999 return isl_set_from_basic_set(bset
);
4001 isl_basic_set_free(bset
);
4006 static int split_periods(__isl_take isl_set
*set
,
4007 __isl_take isl_qpolynomial
*qp
, void *user
);
4009 /* Create a slice of the domain "set" such that integer division "div"
4010 * has the fixed value "v" and add the results to data->res,
4011 * replacing the integer division by "v" in "qp".
4013 static int set_div(__isl_take isl_set
*set
,
4014 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4015 struct isl_split_periods_data
*data
)
4020 struct isl_upoly
*cst
;
4022 slice
= set_div_slice(isl_set_get_dim(set
), qp
, div
, v
);
4023 set
= isl_set_intersect(set
, slice
);
4028 total
= isl_dim_total(qp
->dim
);
4030 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4031 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4033 isl_int_addmul(qp
->div
->row
[i
][1],
4034 qp
->div
->row
[i
][2 + total
+ div
], v
);
4035 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4038 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4039 qp
= substitute_div(qp
, div
, cst
);
4041 return split_periods(set
, qp
, data
);
4044 isl_qpolynomial_free(qp
);
4048 /* Split the domain "set" such that integer division "div"
4049 * has a fixed value (ranging from "min" to "max") on each slice
4050 * and add the results to data->res.
4052 static int split_div(__isl_take isl_set
*set
,
4053 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4054 struct isl_split_periods_data
*data
)
4056 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4057 isl_set
*set_i
= isl_set_copy(set
);
4058 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4060 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4064 isl_qpolynomial_free(qp
);
4068 isl_qpolynomial_free(qp
);
4072 /* If "qp" refers to any integer division
4073 * that can only attain "max_periods" distinct values on "set"
4074 * then split the domain along those distinct values.
4075 * Add the results (or the original if no splitting occurs)
4078 static int split_periods(__isl_take isl_set
*set
,
4079 __isl_take isl_qpolynomial
*qp
, void *user
)
4082 isl_pw_qpolynomial
*pwqp
;
4083 struct isl_split_periods_data
*data
;
4088 data
= (struct isl_split_periods_data
*)user
;
4093 if (qp
->div
->n_row
== 0) {
4094 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4095 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4101 total
= isl_dim_total(qp
->dim
);
4102 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4103 enum isl_lp_result lp_res
;
4105 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4106 qp
->div
->n_row
) != -1)
4109 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4110 set
->ctx
->one
, &min
, NULL
, NULL
);
4111 if (lp_res
== isl_lp_error
)
4113 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4115 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4117 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4118 set
->ctx
->one
, &max
, NULL
, NULL
);
4119 if (lp_res
== isl_lp_error
)
4121 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4123 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4125 isl_int_sub(max
, max
, min
);
4126 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4127 isl_int_add(max
, max
, min
);
4132 if (i
< qp
->div
->n_row
) {
4133 r
= split_div(set
, qp
, i
, min
, max
, data
);
4135 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4136 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4148 isl_qpolynomial_free(qp
);
4152 /* If any quasi-polynomial in pwqp refers to any integer division
4153 * that can only attain "max_periods" distinct values on its domain
4154 * then split the domain along those distinct values.
4156 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4157 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4159 struct isl_split_periods_data data
;
4161 data
.max_periods
= max_periods
;
4162 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp
));
4164 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4167 isl_pw_qpolynomial_free(pwqp
);
4171 isl_pw_qpolynomial_free(data
.res
);
4172 isl_pw_qpolynomial_free(pwqp
);
4176 /* Construct a piecewise quasipolynomial that is constant on the given
4177 * domain. In particular, it is
4180 * infinity if cst == -1
4182 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4183 __isl_take isl_basic_set
*bset
, int cst
)
4186 isl_qpolynomial
*qp
;
4191 bset
= isl_basic_map_domain(isl_basic_map_from_range(bset
));
4192 dim
= isl_basic_set_get_dim(bset
);
4194 qp
= isl_qpolynomial_infty(dim
);
4196 qp
= isl_qpolynomial_zero(dim
);
4198 qp
= isl_qpolynomial_one(dim
);
4199 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4202 /* Factor bset, call fn on each of the factors and return the product.
4204 * If no factors can be found, simply call fn on the input.
4205 * Otherwise, construct the factors based on the factorizer,
4206 * call fn on each factor and compute the product.
4208 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4209 __isl_take isl_basic_set
*bset
,
4210 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4216 isl_qpolynomial
*qp
;
4217 isl_pw_qpolynomial
*pwqp
;
4221 f
= isl_basic_set_factorizer(bset
);
4224 if (f
->n_group
== 0) {
4225 isl_factorizer_free(f
);
4229 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4230 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4232 dim
= isl_basic_set_get_dim(bset
);
4233 dim
= isl_dim_domain(dim
);
4234 set
= isl_set_universe(isl_dim_copy(dim
));
4235 qp
= isl_qpolynomial_one(dim
);
4236 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4238 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4240 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4241 isl_basic_set
*bset_i
;
4242 isl_pw_qpolynomial
*pwqp_i
;
4244 bset_i
= isl_basic_set_copy(bset
);
4245 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4246 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4247 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4249 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4250 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4251 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4253 pwqp_i
= fn(bset_i
);
4254 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4259 isl_basic_set_free(bset
);
4260 isl_factorizer_free(f
);
4264 isl_basic_set_free(bset
);
4268 /* Factor bset, call fn on each of the factors and return the product.
4269 * The function is assumed to evaluate to zero on empty domains,
4270 * to one on zero-dimensional domains and to infinity on unbounded domains
4271 * and will not be called explicitly on zero-dimensional or unbounded domains.
4273 * We first check for some special cases and remove all equalities.
4274 * Then we hand over control to compressed_multiplicative_call.
4276 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4277 __isl_take isl_basic_set
*bset
,
4278 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4282 isl_pw_qpolynomial
*pwqp
;
4283 unsigned orig_nvar
, final_nvar
;
4288 if (isl_basic_set_plain_is_empty(bset
))
4289 return constant_on_domain(bset
, 0);
4291 orig_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4294 return constant_on_domain(bset
, 1);
4296 bounded
= isl_basic_set_is_bounded(bset
);
4300 return constant_on_domain(bset
, -1);
4302 if (bset
->n_eq
== 0)
4303 return compressed_multiplicative_call(bset
, fn
);
4305 morph
= isl_basic_set_full_compression(bset
);
4306 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4308 final_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4310 pwqp
= compressed_multiplicative_call(bset
, fn
);
4312 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, orig_nvar
);
4313 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, final_nvar
);
4314 morph
= isl_morph_inverse(morph
);
4316 pwqp
= isl_pw_qpolynomial_morph(pwqp
, morph
);
4320 isl_basic_set_free(bset
);
4324 /* Drop all floors in "qp", turning each integer division [a/m] into
4325 * a rational division a/m. If "down" is set, then the integer division
4326 * is replaces by (a-(m-1))/m instead.
4328 static __isl_give isl_qpolynomial
*qp_drop_floors(
4329 __isl_take isl_qpolynomial
*qp
, int down
)
4332 struct isl_upoly
*s
;
4336 if (qp
->div
->n_row
== 0)
4339 qp
= isl_qpolynomial_cow(qp
);
4343 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4345 isl_int_sub(qp
->div
->row
[i
][1],
4346 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4347 isl_int_add_ui(qp
->div
->row
[i
][1],
4348 qp
->div
->row
[i
][1], 1);
4350 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4351 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4352 qp
= substitute_div(qp
, i
, s
);
4360 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4361 * a rational division a/m.
4363 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4364 __isl_take isl_pw_qpolynomial
*pwqp
)
4371 if (isl_pw_qpolynomial_is_zero(pwqp
))
4374 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4378 for (i
= 0; i
< pwqp
->n
; ++i
) {
4379 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4386 isl_pw_qpolynomial_free(pwqp
);
4390 /* Adjust all the integer divisions in "qp" such that they are at least
4391 * one over the given orthant (identified by "signs"). This ensures
4392 * that they will still be non-negative even after subtracting (m-1)/m.
4394 * In particular, f is replaced by f' + v, changing f = [a/m]
4395 * to f' = [(a - m v)/m].
4396 * If the constant term k in a is smaller than m,
4397 * the constant term of v is set to floor(k/m) - 1.
4398 * For any other term, if the coefficient c and the variable x have
4399 * the same sign, then no changes are needed.
4400 * Otherwise, if the variable is positive (and c is negative),
4401 * then the coefficient of x in v is set to floor(c/m).
4402 * If the variable is negative (and c is positive),
4403 * then the coefficient of x in v is set to ceil(c/m).
4405 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4411 struct isl_upoly
*s
;
4413 qp
= isl_qpolynomial_cow(qp
);
4416 qp
->div
= isl_mat_cow(qp
->div
);
4420 total
= isl_dim_total(qp
->dim
);
4421 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4423 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4424 isl_int
*row
= qp
->div
->row
[i
];
4428 if (isl_int_lt(row
[1], row
[0])) {
4429 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4430 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4431 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4433 for (j
= 0; j
< total
; ++j
) {
4434 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4437 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4439 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4440 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4442 for (j
= 0; j
< i
; ++j
) {
4443 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4445 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4446 row
[2 + total
+ j
], row
[0]);
4447 isl_int_submul(row
[2 + total
+ j
],
4448 row
[0], v
->el
[1 + total
+ j
]);
4450 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4451 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4453 isl_seq_combine(qp
->div
->row
[j
] + 1,
4454 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4455 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4457 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4458 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4459 qp
->div
->ctx
->one
, v
->size
);
4460 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4470 isl_qpolynomial_free(qp
);
4474 struct isl_to_poly_data
{
4476 isl_pw_qpolynomial
*res
;
4477 isl_qpolynomial
*qp
;
4480 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4481 * We first make all integer divisions positive and then split the
4482 * quasipolynomials into terms with sign data->sign (the direction
4483 * of the requested approximation) and terms with the opposite sign.
4484 * In the first set of terms, each integer division [a/m] is
4485 * overapproximated by a/m, while in the second it is underapproximated
4488 static int to_polynomial_on_orthant(__isl_take isl_set
*orthant
, int *signs
,
4491 struct isl_to_poly_data
*data
= user
;
4492 isl_pw_qpolynomial
*t
;
4493 isl_qpolynomial
*qp
, *up
, *down
;
4495 qp
= isl_qpolynomial_copy(data
->qp
);
4496 qp
= make_divs_pos(qp
, signs
);
4498 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4499 up
= qp_drop_floors(up
, 0);
4500 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4501 down
= qp_drop_floors(down
, 1);
4503 isl_qpolynomial_free(qp
);
4504 qp
= isl_qpolynomial_add(up
, down
);
4506 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4507 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4512 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4513 * the polynomial will be an overapproximation. If "sign" is negative,
4514 * it will be an underapproximation. If "sign" is zero, the approximation
4515 * will lie somewhere in between.
4517 * In particular, is sign == 0, we simply drop the floors, turning
4518 * the integer divisions into rational divisions.
4519 * Otherwise, we split the domains into orthants, make all integer divisions
4520 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4521 * depending on the requested sign and the sign of the term in which
4522 * the integer division appears.
4524 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4525 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4528 struct isl_to_poly_data data
;
4531 return pwqp_drop_floors(pwqp
);
4537 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp
));
4539 for (i
= 0; i
< pwqp
->n
; ++i
) {
4540 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4541 isl_pw_qpolynomial
*t
;
4542 t
= isl_pw_qpolynomial_alloc(
4543 isl_set_copy(pwqp
->p
[i
].set
),
4544 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4545 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4548 data
.qp
= pwqp
->p
[i
].qp
;
4549 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4550 &to_polynomial_on_orthant
, &data
) < 0)
4554 isl_pw_qpolynomial_free(pwqp
);
4558 isl_pw_qpolynomial_free(pwqp
);
4559 isl_pw_qpolynomial_free(data
.res
);
4563 static int poly_entry(void **entry
, void *user
)
4566 isl_pw_qpolynomial
**pwqp
= (isl_pw_qpolynomial
**)entry
;
4568 *pwqp
= isl_pw_qpolynomial_to_polynomial(*pwqp
, *sign
);
4570 return *pwqp
? 0 : -1;
4573 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4574 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4576 upwqp
= isl_union_pw_qpolynomial_cow(upwqp
);
4580 if (isl_hash_table_foreach(upwqp
->dim
->ctx
, &upwqp
->table
,
4581 &poly_entry
, &sign
) < 0)
4586 isl_union_pw_qpolynomial_free(upwqp
);
4590 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4591 __isl_take isl_qpolynomial
*qp
)
4595 isl_vec
*aff
= NULL
;
4596 isl_basic_map
*bmap
= NULL
;
4602 if (!isl_upoly_is_affine(qp
->upoly
))
4603 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4604 "input quasi-polynomial not affine", goto error
);
4605 aff
= isl_qpolynomial_extract_affine(qp
);
4608 dim
= isl_qpolynomial_get_dim(qp
);
4609 dim
= isl_dim_from_domain(dim
);
4610 pos
= 1 + isl_dim_offset(dim
, isl_dim_out
);
4611 dim
= isl_dim_add(dim
, isl_dim_out
, 1);
4612 n_div
= qp
->div
->n_row
;
4613 bmap
= isl_basic_map_alloc_dim(dim
, n_div
, 1, 2 * n_div
);
4615 for (i
= 0; i
< n_div
; ++i
) {
4616 k
= isl_basic_map_alloc_div(bmap
);
4619 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4620 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4621 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4624 k
= isl_basic_map_alloc_equality(bmap
);
4627 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4628 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4629 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4632 isl_qpolynomial_free(qp
);
4633 bmap
= isl_basic_map_finalize(bmap
);
4637 isl_qpolynomial_free(qp
);
4638 isl_basic_map_free(bmap
);