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_factorization.h>
15 #include <isl_union_map_private.h>
16 #include <isl_polynomial_private.h>
17 #include <isl_point_private.h>
18 #include <isl_dim_private.h>
19 #include <isl_map_private.h>
20 #include <isl_mat_private.h>
22 static unsigned pos(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
25 case isl_dim_param
: return 0;
26 case isl_dim_in
: return dim
->nparam
;
27 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
32 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
40 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
45 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
47 return (struct isl_upoly_cst
*)up
;
50 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
55 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
57 return (struct isl_upoly_rec
*)up
;
60 int isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
61 __isl_keep
struct isl_upoly
*up2
)
64 struct isl_upoly_rec
*rec1
, *rec2
;
70 if (up1
->var
!= up2
->var
)
72 if (isl_upoly_is_cst(up1
)) {
73 struct isl_upoly_cst
*cst1
, *cst2
;
74 cst1
= isl_upoly_as_cst(up1
);
75 cst2
= isl_upoly_as_cst(up2
);
78 return isl_int_eq(cst1
->n
, cst2
->n
) &&
79 isl_int_eq(cst1
->d
, cst2
->d
);
82 rec1
= isl_upoly_as_rec(up1
);
83 rec2
= isl_upoly_as_rec(up2
);
87 if (rec1
->n
!= rec2
->n
)
90 for (i
= 0; i
< rec1
->n
; ++i
) {
91 int eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
99 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
101 struct isl_upoly_cst
*cst
;
105 if (!isl_upoly_is_cst(up
))
108 cst
= isl_upoly_as_cst(up
);
112 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
115 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
117 struct isl_upoly_cst
*cst
;
121 if (!isl_upoly_is_cst(up
))
124 cst
= isl_upoly_as_cst(up
);
128 return isl_int_sgn(cst
->n
);
131 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
133 struct isl_upoly_cst
*cst
;
137 if (!isl_upoly_is_cst(up
))
140 cst
= isl_upoly_as_cst(up
);
144 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
147 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
149 struct isl_upoly_cst
*cst
;
153 if (!isl_upoly_is_cst(up
))
156 cst
= isl_upoly_as_cst(up
);
160 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
163 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
165 struct isl_upoly_cst
*cst
;
169 if (!isl_upoly_is_cst(up
))
172 cst
= isl_upoly_as_cst(up
);
176 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
179 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
181 struct isl_upoly_cst
*cst
;
185 if (!isl_upoly_is_cst(up
))
188 cst
= isl_upoly_as_cst(up
);
192 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
195 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
197 struct isl_upoly_cst
*cst
;
201 if (!isl_upoly_is_cst(up
))
204 cst
= isl_upoly_as_cst(up
);
208 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
211 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
213 struct isl_upoly_cst
*cst
;
215 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
224 isl_int_init(cst
->n
);
225 isl_int_init(cst
->d
);
230 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
232 struct isl_upoly_cst
*cst
;
234 cst
= isl_upoly_cst_alloc(ctx
);
238 isl_int_set_si(cst
->n
, 0);
239 isl_int_set_si(cst
->d
, 1);
244 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
246 struct isl_upoly_cst
*cst
;
248 cst
= isl_upoly_cst_alloc(ctx
);
252 isl_int_set_si(cst
->n
, 1);
253 isl_int_set_si(cst
->d
, 1);
258 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
260 struct isl_upoly_cst
*cst
;
262 cst
= isl_upoly_cst_alloc(ctx
);
266 isl_int_set_si(cst
->n
, 1);
267 isl_int_set_si(cst
->d
, 0);
272 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
274 struct isl_upoly_cst
*cst
;
276 cst
= isl_upoly_cst_alloc(ctx
);
280 isl_int_set_si(cst
->n
, -1);
281 isl_int_set_si(cst
->d
, 0);
286 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
288 struct isl_upoly_cst
*cst
;
290 cst
= isl_upoly_cst_alloc(ctx
);
294 isl_int_set_si(cst
->n
, 0);
295 isl_int_set_si(cst
->d
, 0);
300 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
301 isl_int n
, isl_int d
)
303 struct isl_upoly_cst
*cst
;
305 cst
= isl_upoly_cst_alloc(ctx
);
309 isl_int_set(cst
->n
, n
);
310 isl_int_set(cst
->d
, d
);
315 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
318 struct isl_upoly_rec
*rec
;
320 isl_assert(ctx
, var
>= 0, return NULL
);
321 isl_assert(ctx
, size
>= 0, return NULL
);
322 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
323 sizeof(struct isl_upoly_rec
) +
324 (size
- 1) * sizeof(struct isl_upoly
*));
339 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_dim(
340 __isl_take isl_qpolynomial
*qp
, __isl_take isl_dim
*dim
)
342 qp
= isl_qpolynomial_cow(qp
);
346 isl_dim_free(qp
->dim
);
351 isl_qpolynomial_free(qp
);
356 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
358 return qp
? qp
->dim
->ctx
: NULL
;
361 __isl_give isl_dim
*isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial
*qp
)
363 return qp
? isl_dim_copy(qp
->dim
) : NULL
;
366 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
367 enum isl_dim_type type
)
369 return qp
? isl_dim_size(qp
->dim
, type
) : 0;
372 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
374 return qp
? isl_upoly_is_zero(qp
->upoly
) : -1;
377 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
379 return qp
? isl_upoly_is_one(qp
->upoly
) : -1;
382 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
384 return qp
? isl_upoly_is_nan(qp
->upoly
) : -1;
387 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
389 return qp
? isl_upoly_is_infty(qp
->upoly
) : -1;
392 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
394 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : -1;
397 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
399 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
402 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
404 isl_int_clear(cst
->n
);
405 isl_int_clear(cst
->d
);
408 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
412 for (i
= 0; i
< rec
->n
; ++i
)
413 isl_upoly_free(rec
->p
[i
]);
416 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
425 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
427 struct isl_upoly_cst
*cst
;
428 struct isl_upoly_cst
*dup
;
430 cst
= isl_upoly_as_cst(up
);
434 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
437 isl_int_set(dup
->n
, cst
->n
);
438 isl_int_set(dup
->d
, cst
->d
);
443 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
446 struct isl_upoly_rec
*rec
;
447 struct isl_upoly_rec
*dup
;
449 rec
= isl_upoly_as_rec(up
);
453 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
457 for (i
= 0; i
< rec
->n
; ++i
) {
458 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
466 isl_upoly_free(&dup
->up
);
470 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
472 struct isl_upoly
*dup
;
477 if (isl_upoly_is_cst(up
))
478 return isl_upoly_dup_cst(up
);
480 return isl_upoly_dup_rec(up
);
483 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
491 return isl_upoly_dup(up
);
494 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
503 upoly_free_cst((struct isl_upoly_cst
*)up
);
505 upoly_free_rec((struct isl_upoly_rec
*)up
);
507 isl_ctx_deref(up
->ctx
);
511 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
516 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
517 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
518 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
519 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
524 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
525 __isl_take
struct isl_upoly
*up2
)
527 struct isl_upoly_cst
*cst1
;
528 struct isl_upoly_cst
*cst2
;
530 up1
= isl_upoly_cow(up1
);
534 cst1
= isl_upoly_as_cst(up1
);
535 cst2
= isl_upoly_as_cst(up2
);
537 if (isl_int_eq(cst1
->d
, cst2
->d
))
538 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
540 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
541 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
542 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
545 isl_upoly_cst_reduce(cst1
);
555 static __isl_give
struct isl_upoly
*replace_by_zero(
556 __isl_take
struct isl_upoly
*up
)
564 return isl_upoly_zero(ctx
);
567 static __isl_give
struct isl_upoly
*replace_by_constant_term(
568 __isl_take
struct isl_upoly
*up
)
570 struct isl_upoly_rec
*rec
;
571 struct isl_upoly
*cst
;
576 rec
= isl_upoly_as_rec(up
);
579 cst
= isl_upoly_copy(rec
->p
[0]);
587 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
588 __isl_take
struct isl_upoly
*up2
)
591 struct isl_upoly_rec
*rec1
, *rec2
;
596 if (isl_upoly_is_nan(up1
)) {
601 if (isl_upoly_is_nan(up2
)) {
606 if (isl_upoly_is_zero(up1
)) {
611 if (isl_upoly_is_zero(up2
)) {
616 if (up1
->var
< up2
->var
)
617 return isl_upoly_sum(up2
, up1
);
619 if (up2
->var
< up1
->var
) {
620 struct isl_upoly_rec
*rec
;
621 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
625 up1
= isl_upoly_cow(up1
);
626 rec
= isl_upoly_as_rec(up1
);
629 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
631 up1
= replace_by_constant_term(up1
);
635 if (isl_upoly_is_cst(up1
))
636 return isl_upoly_sum_cst(up1
, up2
);
638 rec1
= isl_upoly_as_rec(up1
);
639 rec2
= isl_upoly_as_rec(up2
);
643 if (rec1
->n
< rec2
->n
)
644 return isl_upoly_sum(up2
, up1
);
646 up1
= isl_upoly_cow(up1
);
647 rec1
= isl_upoly_as_rec(up1
);
651 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
652 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
653 isl_upoly_copy(rec2
->p
[i
]));
656 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
657 isl_upoly_free(rec1
->p
[i
]);
663 up1
= replace_by_zero(up1
);
664 else if (rec1
->n
== 1)
665 up1
= replace_by_constant_term(up1
);
676 __isl_give
struct isl_upoly
*isl_upoly_neg_cst(__isl_take
struct isl_upoly
*up
)
678 struct isl_upoly_cst
*cst
;
680 if (isl_upoly_is_zero(up
))
683 up
= isl_upoly_cow(up
);
687 cst
= isl_upoly_as_cst(up
);
689 isl_int_neg(cst
->n
, cst
->n
);
694 __isl_give
struct isl_upoly
*isl_upoly_neg(__isl_take
struct isl_upoly
*up
)
697 struct isl_upoly_rec
*rec
;
702 if (isl_upoly_is_cst(up
))
703 return isl_upoly_neg_cst(up
);
705 up
= isl_upoly_cow(up
);
706 rec
= isl_upoly_as_rec(up
);
710 for (i
= 0; i
< rec
->n
; ++i
) {
711 rec
->p
[i
] = isl_upoly_neg(rec
->p
[i
]);
722 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
723 __isl_take
struct isl_upoly
*up2
)
725 struct isl_upoly_cst
*cst1
;
726 struct isl_upoly_cst
*cst2
;
728 up1
= isl_upoly_cow(up1
);
732 cst1
= isl_upoly_as_cst(up1
);
733 cst2
= isl_upoly_as_cst(up2
);
735 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
736 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
738 isl_upoly_cst_reduce(cst1
);
748 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
749 __isl_take
struct isl_upoly
*up2
)
751 struct isl_upoly_rec
*rec1
;
752 struct isl_upoly_rec
*rec2
;
753 struct isl_upoly_rec
*res
;
757 rec1
= isl_upoly_as_rec(up1
);
758 rec2
= isl_upoly_as_rec(up2
);
761 size
= rec1
->n
+ rec2
->n
- 1;
762 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
766 for (i
= 0; i
< rec1
->n
; ++i
) {
767 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
768 isl_upoly_copy(rec1
->p
[i
]));
773 for (; i
< size
; ++i
) {
774 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
779 for (i
= 0; i
< rec1
->n
; ++i
) {
780 for (j
= 1; j
< rec2
->n
; ++j
) {
781 struct isl_upoly
*up
;
782 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
783 isl_upoly_copy(rec1
->p
[i
]));
784 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
797 isl_upoly_free(&res
->up
);
801 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
802 __isl_take
struct isl_upoly
*up2
)
807 if (isl_upoly_is_nan(up1
)) {
812 if (isl_upoly_is_nan(up2
)) {
817 if (isl_upoly_is_zero(up1
)) {
822 if (isl_upoly_is_zero(up2
)) {
827 if (isl_upoly_is_one(up1
)) {
832 if (isl_upoly_is_one(up2
)) {
837 if (up1
->var
< up2
->var
)
838 return isl_upoly_mul(up2
, up1
);
840 if (up2
->var
< up1
->var
) {
842 struct isl_upoly_rec
*rec
;
843 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
844 isl_ctx
*ctx
= up1
->ctx
;
847 return isl_upoly_nan(ctx
);
849 up1
= isl_upoly_cow(up1
);
850 rec
= isl_upoly_as_rec(up1
);
854 for (i
= 0; i
< rec
->n
; ++i
) {
855 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
856 isl_upoly_copy(up2
));
864 if (isl_upoly_is_cst(up1
))
865 return isl_upoly_mul_cst(up1
, up2
);
867 return isl_upoly_mul_rec(up1
, up2
);
874 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_dim
*dim
,
875 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
877 struct isl_qpolynomial
*qp
= NULL
;
883 total
= isl_dim_total(dim
);
885 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
890 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
901 isl_qpolynomial_free(qp
);
905 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
914 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
916 struct isl_qpolynomial
*dup
;
921 dup
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
,
922 isl_upoly_copy(qp
->upoly
));
925 isl_mat_free(dup
->div
);
926 dup
->div
= isl_mat_copy(qp
->div
);
932 isl_qpolynomial_free(dup
);
936 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
944 return isl_qpolynomial_dup(qp
);
947 void isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
955 isl_dim_free(qp
->dim
);
956 isl_mat_free(qp
->div
);
957 isl_upoly_free(qp
->upoly
);
962 __isl_give
struct isl_upoly
*isl_upoly_pow(isl_ctx
*ctx
, int pos
, int power
)
965 struct isl_upoly
*up
;
966 struct isl_upoly_rec
*rec
;
967 struct isl_upoly_cst
*cst
;
969 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
972 for (i
= 0; i
< 1 + power
; ++i
) {
973 rec
->p
[i
] = isl_upoly_zero(ctx
);
978 cst
= isl_upoly_as_cst(rec
->p
[power
]);
979 isl_int_set_si(cst
->n
, 1);
983 isl_upoly_free(&rec
->up
);
987 /* r array maps original positions to new positions.
989 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
993 struct isl_upoly_rec
*rec
;
994 struct isl_upoly
*base
;
995 struct isl_upoly
*res
;
997 if (isl_upoly_is_cst(up
))
1000 rec
= isl_upoly_as_rec(up
);
1004 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1006 base
= isl_upoly_pow(up
->ctx
, r
[up
->var
], 1);
1007 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1009 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1010 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1011 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1014 isl_upoly_free(base
);
1023 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
1028 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1029 div1
->n_col
>= div2
->n_col
, return -1);
1031 if (div1
->n_row
== div2
->n_row
)
1032 return isl_mat_is_equal(div1
, div2
);
1034 n_row
= div1
->n_row
;
1035 n_col
= div1
->n_col
;
1036 div1
->n_row
= div2
->n_row
;
1037 div1
->n_col
= div2
->n_col
;
1039 equal
= isl_mat_is_equal(div1
, div2
);
1041 div1
->n_row
= n_row
;
1042 div1
->n_col
= n_col
;
1047 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
1048 __isl_keep isl_mat
*src
, int s
, int *exp
)
1051 unsigned c
= src
->n_col
- src
->n_row
;
1053 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
1054 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
1056 for (i
= 0; i
< s
; ++i
)
1057 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
1060 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1064 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1065 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1070 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1073 struct isl_div_sort_info
{
1078 static int div_sort_cmp(const void *p1
, const void *p2
)
1080 const struct isl_div_sort_info
*i1
, *i2
;
1081 i1
= (const struct isl_div_sort_info
*) p1
;
1082 i2
= (const struct isl_div_sort_info
*) p2
;
1084 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1087 /* Sort divs and remove duplicates.
1089 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1094 struct isl_div_sort_info
*array
= NULL
;
1095 int *pos
= NULL
, *at
= NULL
;
1096 int *reordering
= NULL
;
1101 if (qp
->div
->n_row
<= 1)
1104 div_pos
= isl_dim_total(qp
->dim
);
1106 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1108 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1109 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1110 len
= qp
->div
->n_col
- 2;
1111 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1112 if (!array
|| !pos
|| !at
|| !reordering
)
1115 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1116 array
[i
].div
= qp
->div
;
1122 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1125 for (i
= 0; i
< div_pos
; ++i
)
1128 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1129 if (pos
[array
[i
].row
] == i
)
1131 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1132 pos
[at
[i
]] = pos
[array
[i
].row
];
1133 at
[pos
[array
[i
].row
]] = at
[i
];
1134 at
[i
] = array
[i
].row
;
1135 pos
[array
[i
].row
] = i
;
1139 for (i
= 0; i
< len
- div_pos
; ++i
) {
1141 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1142 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1143 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1144 qp
->div
= isl_mat_drop_cols(qp
->div
,
1145 2 + div_pos
+ i
- skip
, 1);
1148 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1151 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1153 if (!qp
->upoly
|| !qp
->div
)
1167 isl_qpolynomial_free(qp
);
1171 static __isl_give isl_mat
*merge_divs(__isl_keep isl_mat
*div1
,
1172 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
1175 isl_mat
*div
= NULL
;
1176 unsigned d
= div1
->n_col
- div1
->n_row
;
1178 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
1179 d
+ div1
->n_row
+ div2
->n_row
);
1183 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
1186 expand_row(div
, k
, div1
, i
, exp1
);
1187 expand_row(div
, k
+ 1, div2
, j
, exp2
);
1189 cmp
= cmp_row(div
, k
, k
+ 1);
1193 } else if (cmp
< 0) {
1197 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
1200 for (; i
< div1
->n_row
; ++i
, ++k
) {
1201 expand_row(div
, k
, div1
, i
, exp1
);
1204 for (; j
< div2
->n_row
; ++j
, ++k
) {
1205 expand_row(div
, k
, div2
, j
, exp2
);
1215 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1216 int *exp
, int first
)
1219 struct isl_upoly_rec
*rec
;
1221 if (isl_upoly_is_cst(up
))
1224 if (up
->var
< first
)
1227 if (exp
[up
->var
- first
] == up
->var
- first
)
1230 up
= isl_upoly_cow(up
);
1234 up
->var
= exp
[up
->var
- first
] + first
;
1236 rec
= isl_upoly_as_rec(up
);
1240 for (i
= 0; i
< rec
->n
; ++i
) {
1241 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1252 static __isl_give isl_qpolynomial
*with_merged_divs(
1253 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1254 __isl_take isl_qpolynomial
*qp2
),
1255 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1259 isl_mat
*div
= NULL
;
1261 qp1
= isl_qpolynomial_cow(qp1
);
1262 qp2
= isl_qpolynomial_cow(qp2
);
1267 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1268 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1270 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
1271 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
1275 div
= merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1279 isl_mat_free(qp1
->div
);
1280 qp1
->div
= isl_mat_copy(div
);
1281 isl_mat_free(qp2
->div
);
1282 qp2
->div
= isl_mat_copy(div
);
1284 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1285 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1287 if (!qp1
->upoly
|| !qp2
->upoly
)
1294 return fn(qp1
, qp2
);
1299 isl_qpolynomial_free(qp1
);
1300 isl_qpolynomial_free(qp2
);
1304 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1305 __isl_take isl_qpolynomial
*qp2
)
1307 qp1
= isl_qpolynomial_cow(qp1
);
1312 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1313 return isl_qpolynomial_add(qp2
, qp1
);
1315 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1316 if (!compatible_divs(qp1
->div
, qp2
->div
))
1317 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1319 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1323 isl_qpolynomial_free(qp2
);
1327 isl_qpolynomial_free(qp1
);
1328 isl_qpolynomial_free(qp2
);
1332 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1333 __isl_keep isl_set
*dom
,
1334 __isl_take isl_qpolynomial
*qp1
,
1335 __isl_take isl_qpolynomial
*qp2
)
1337 return isl_qpolynomial_add(qp1
, qp2
);
1340 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1341 __isl_take isl_qpolynomial
*qp2
)
1343 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1346 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1348 qp
= isl_qpolynomial_cow(qp
);
1353 qp
->upoly
= isl_upoly_neg(qp
->upoly
);
1359 isl_qpolynomial_free(qp
);
1363 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1364 __isl_take isl_qpolynomial
*qp2
)
1366 qp1
= isl_qpolynomial_cow(qp1
);
1371 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1372 return isl_qpolynomial_mul(qp2
, qp1
);
1374 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1375 if (!compatible_divs(qp1
->div
, qp2
->div
))
1376 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1378 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1382 isl_qpolynomial_free(qp2
);
1386 isl_qpolynomial_free(qp1
);
1387 isl_qpolynomial_free(qp2
);
1391 __isl_give isl_qpolynomial
*isl_qpolynomial_zero(__isl_take isl_dim
*dim
)
1393 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1396 __isl_give isl_qpolynomial
*isl_qpolynomial_one(__isl_take isl_dim
*dim
)
1398 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1401 __isl_give isl_qpolynomial
*isl_qpolynomial_infty(__isl_take isl_dim
*dim
)
1403 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1406 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty(__isl_take isl_dim
*dim
)
1408 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1411 __isl_give isl_qpolynomial
*isl_qpolynomial_nan(__isl_take isl_dim
*dim
)
1413 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1416 __isl_give isl_qpolynomial
*isl_qpolynomial_cst(__isl_take isl_dim
*dim
,
1419 struct isl_qpolynomial
*qp
;
1420 struct isl_upoly_cst
*cst
;
1422 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1426 cst
= isl_upoly_as_cst(qp
->upoly
);
1427 isl_int_set(cst
->n
, v
);
1432 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1433 isl_int
*n
, isl_int
*d
)
1435 struct isl_upoly_cst
*cst
;
1440 if (!isl_upoly_is_cst(qp
->upoly
))
1443 cst
= isl_upoly_as_cst(qp
->upoly
);
1448 isl_int_set(*n
, cst
->n
);
1450 isl_int_set(*d
, cst
->d
);
1455 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1458 struct isl_upoly_rec
*rec
;
1466 rec
= isl_upoly_as_rec(up
);
1473 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1475 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1481 return isl_upoly_is_affine(rec
->p
[0]);
1484 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1489 if (qp
->div
->n_row
> 0)
1492 return isl_upoly_is_affine(qp
->upoly
);
1495 static void update_coeff(__isl_keep isl_vec
*aff
,
1496 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1501 if (isl_int_is_zero(cst
->n
))
1506 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1507 isl_int_divexact(f
, cst
->d
, gcd
);
1508 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1509 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1510 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1515 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1516 __isl_keep isl_vec
*aff
)
1518 struct isl_upoly_cst
*cst
;
1519 struct isl_upoly_rec
*rec
;
1525 struct isl_upoly_cst
*cst
;
1527 cst
= isl_upoly_as_cst(up
);
1530 update_coeff(aff
, cst
, 0);
1534 rec
= isl_upoly_as_rec(up
);
1537 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1539 cst
= isl_upoly_as_cst(rec
->p
[1]);
1542 update_coeff(aff
, cst
, 1 + up
->var
);
1544 return isl_upoly_update_affine(rec
->p
[0], aff
);
1547 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1548 __isl_keep isl_qpolynomial
*qp
)
1556 isl_assert(qp
->div
->ctx
, qp
->div
->n_row
== 0, return NULL
);
1557 d
= isl_dim_total(qp
->dim
);
1558 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
1562 isl_seq_clr(aff
->el
+ 1, 1 + d
);
1563 isl_int_set_si(aff
->el
[0], 1);
1565 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1574 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1575 __isl_keep isl_qpolynomial
*qp2
)
1580 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1583 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1586 struct isl_upoly_rec
*rec
;
1588 if (isl_upoly_is_cst(up
)) {
1589 struct isl_upoly_cst
*cst
;
1590 cst
= isl_upoly_as_cst(up
);
1593 isl_int_lcm(*d
, *d
, cst
->d
);
1597 rec
= isl_upoly_as_rec(up
);
1601 for (i
= 0; i
< rec
->n
; ++i
)
1602 upoly_update_den(rec
->p
[i
], d
);
1605 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1607 isl_int_set_si(*d
, 1);
1610 upoly_update_den(qp
->upoly
, d
);
1613 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_dim
*dim
,
1616 struct isl_ctx
*ctx
;
1623 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_pow(ctx
, pos
, power
));
1626 __isl_give isl_qpolynomial
*isl_qpolynomial_var(__isl_take isl_dim
*dim
,
1627 enum isl_dim_type type
, unsigned pos
)
1632 isl_assert(dim
->ctx
, isl_dim_size(dim
, isl_dim_in
) == 0, goto error
);
1633 isl_assert(dim
->ctx
, pos
< isl_dim_size(dim
, type
), goto error
);
1635 if (type
== isl_dim_set
)
1636 pos
+= isl_dim_size(dim
, isl_dim_param
);
1638 return isl_qpolynomial_pow(dim
, pos
, 1);
1644 __isl_give isl_qpolynomial
*isl_qpolynomial_div_pow(__isl_take isl_div
*div
,
1647 struct isl_qpolynomial
*qp
= NULL
;
1648 struct isl_upoly_rec
*rec
;
1649 struct isl_upoly_cst
*cst
;
1655 isl_assert(div
->ctx
, div
->bmap
->n_div
== 1, goto error
);
1657 pos
= isl_dim_total(div
->bmap
->dim
);
1658 rec
= isl_upoly_alloc_rec(div
->ctx
, pos
, 1 + power
);
1659 qp
= isl_qpolynomial_alloc(isl_basic_map_get_dim(div
->bmap
), 1,
1664 isl_seq_cpy(qp
->div
->row
[0], div
->line
[0], qp
->div
->n_col
- 1);
1665 isl_int_set_si(qp
->div
->row
[0][qp
->div
->n_col
- 1], 0);
1667 for (i
= 0; i
< 1 + power
; ++i
) {
1668 rec
->p
[i
] = isl_upoly_zero(div
->ctx
);
1673 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1674 isl_int_set_si(cst
->n
, 1);
1680 isl_qpolynomial_free(qp
);
1685 __isl_give isl_qpolynomial
*isl_qpolynomial_div(__isl_take isl_div
*div
)
1687 return isl_qpolynomial_div_pow(div
, 1);
1690 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst(__isl_take isl_dim
*dim
,
1691 const isl_int n
, const isl_int d
)
1693 struct isl_qpolynomial
*qp
;
1694 struct isl_upoly_cst
*cst
;
1696 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1700 cst
= isl_upoly_as_cst(qp
->upoly
);
1701 isl_int_set(cst
->n
, n
);
1702 isl_int_set(cst
->d
, d
);
1707 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
1709 struct isl_upoly_rec
*rec
;
1715 if (isl_upoly_is_cst(up
))
1719 active
[up
->var
] = 1;
1721 rec
= isl_upoly_as_rec(up
);
1722 for (i
= 0; i
< rec
->n
; ++i
)
1723 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
1729 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
1732 int d
= isl_dim_total(qp
->dim
);
1737 for (i
= 0; i
< d
; ++i
)
1738 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
1739 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
1745 return up_set_active(qp
->upoly
, active
, d
);
1748 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
1749 enum isl_dim_type type
, unsigned first
, unsigned n
)
1760 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1762 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1763 type
== isl_dim_set
, return -1);
1765 active
= isl_calloc_array(set
->ctx
, int, isl_dim_total(qp
->dim
));
1766 if (set_active(qp
, active
) < 0)
1769 if (type
== isl_dim_set
)
1770 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1771 for (i
= 0; i
< n
; ++i
)
1772 if (active
[first
+ i
]) {
1785 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
1786 unsigned first
, unsigned n
)
1789 struct isl_upoly_rec
*rec
;
1793 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
1795 if (up
->var
< first
+ n
) {
1796 up
= replace_by_constant_term(up
);
1797 return isl_upoly_drop(up
, first
, n
);
1799 up
= isl_upoly_cow(up
);
1803 rec
= isl_upoly_as_rec(up
);
1807 for (i
= 0; i
< rec
->n
; ++i
) {
1808 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
1819 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
1820 __isl_take isl_qpolynomial
*qp
,
1821 enum isl_dim_type type
, unsigned first
, unsigned n
)
1825 if (n
== 0 && !isl_dim_get_tuple_name(qp
->dim
, type
))
1828 qp
= isl_qpolynomial_cow(qp
);
1832 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1834 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1835 type
== isl_dim_set
, goto error
);
1837 qp
->dim
= isl_dim_drop(qp
->dim
, type
, first
, n
);
1841 if (type
== isl_dim_set
)
1842 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1844 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
1848 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
1854 isl_qpolynomial_free(qp
);
1858 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
1859 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
1862 struct isl_upoly_rec
*rec
;
1863 struct isl_upoly
*base
, *res
;
1868 if (isl_upoly_is_cst(up
))
1871 if (up
->var
< first
)
1874 rec
= isl_upoly_as_rec(up
);
1878 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1880 if (up
->var
>= first
+ n
)
1881 base
= isl_upoly_pow(up
->ctx
, up
->var
, 1);
1883 base
= isl_upoly_copy(subs
[up
->var
- first
]);
1885 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
1886 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1887 struct isl_upoly
*t
;
1888 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
1889 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1890 res
= isl_upoly_sum(res
, t
);
1893 isl_upoly_free(base
);
1902 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
1903 isl_int denom
, unsigned len
)
1906 struct isl_upoly
*up
;
1908 isl_assert(ctx
, len
>= 1, return NULL
);
1910 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
1911 for (i
= 0; i
< len
- 1; ++i
) {
1912 struct isl_upoly
*t
;
1913 struct isl_upoly
*c
;
1915 if (isl_int_is_zero(f
[1 + i
]))
1918 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
1919 t
= isl_upoly_pow(ctx
, i
, 1);
1920 t
= isl_upoly_mul(c
, t
);
1921 up
= isl_upoly_sum(up
, t
);
1927 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
1928 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
1933 struct isl_upoly
*up
;
1937 if (eq
->n_eq
== 0) {
1938 isl_basic_set_free(eq
);
1942 qp
= isl_qpolynomial_cow(qp
);
1945 qp
->div
= isl_mat_cow(qp
->div
);
1949 total
= 1 + isl_dim_total(eq
->dim
);
1950 isl_int_init(denom
);
1951 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1952 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
);
1953 if (j
< 0 || j
== 0)
1956 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
1957 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
1959 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
1960 &qp
->div
->row
[k
][0]);
1961 isl_seq_normalize(qp
->div
->ctx
,
1962 qp
->div
->row
[k
], 1 + total
);
1965 if (isl_int_is_pos(eq
->eq
[i
][j
]))
1966 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
1967 isl_int_abs(denom
, eq
->eq
[i
][j
]);
1968 isl_int_set_si(eq
->eq
[i
][j
], 0);
1970 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
1971 eq
->eq
[i
], denom
, total
);
1972 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
1975 isl_int_clear(denom
);
1980 isl_basic_set_free(eq
);
1986 isl_basic_set_free(eq
);
1987 isl_qpolynomial_free(qp
);
1992 #define PW isl_pw_qpolynomial
1994 #define EL isl_qpolynomial
1996 #define IS_ZERO is_zero
2000 #include <isl_pw_templ.c>
2003 #define UNION isl_union_pw_qpolynomial
2005 #define PART isl_pw_qpolynomial
2007 #define PARTS pw_qpolynomial
2009 #include <isl_union_templ.c>
2011 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2019 if (!isl_set_fast_is_universe(pwqp
->p
[0].set
))
2022 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2025 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2026 __isl_take isl_pw_qpolynomial
*pwqp1
,
2027 __isl_take isl_pw_qpolynomial
*pwqp2
)
2030 struct isl_pw_qpolynomial
*res
;
2033 if (!pwqp1
|| !pwqp2
)
2036 isl_assert(pwqp1
->dim
->ctx
, isl_dim_equal(pwqp1
->dim
, pwqp2
->dim
),
2039 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
2040 isl_pw_qpolynomial_free(pwqp2
);
2044 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2045 isl_pw_qpolynomial_free(pwqp1
);
2049 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2050 isl_pw_qpolynomial_free(pwqp1
);
2054 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
2055 isl_pw_qpolynomial_free(pwqp2
);
2059 n
= pwqp1
->n
* pwqp2
->n
;
2060 res
= isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1
->dim
), n
);
2062 for (i
= 0; i
< pwqp1
->n
; ++i
) {
2063 for (j
= 0; j
< pwqp2
->n
; ++j
) {
2064 struct isl_set
*common
;
2065 struct isl_qpolynomial
*prod
;
2066 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
2067 isl_set_copy(pwqp2
->p
[j
].set
));
2068 if (isl_set_fast_is_empty(common
)) {
2069 isl_set_free(common
);
2073 prod
= isl_qpolynomial_mul(
2074 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
2075 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
2077 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
2081 isl_pw_qpolynomial_free(pwqp1
);
2082 isl_pw_qpolynomial_free(pwqp2
);
2086 isl_pw_qpolynomial_free(pwqp1
);
2087 isl_pw_qpolynomial_free(pwqp2
);
2091 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_neg(
2092 __isl_take isl_pw_qpolynomial
*pwqp
)
2099 if (isl_pw_qpolynomial_is_zero(pwqp
))
2102 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
2106 for (i
= 0; i
< pwqp
->n
; ++i
) {
2107 pwqp
->p
[i
].qp
= isl_qpolynomial_neg(pwqp
->p
[i
].qp
);
2114 isl_pw_qpolynomial_free(pwqp
);
2118 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sub(
2119 __isl_take isl_pw_qpolynomial
*pwqp1
,
2120 __isl_take isl_pw_qpolynomial
*pwqp2
)
2122 return isl_pw_qpolynomial_add(pwqp1
, isl_pw_qpolynomial_neg(pwqp2
));
2125 __isl_give
struct isl_upoly
*isl_upoly_eval(
2126 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
2129 struct isl_upoly_rec
*rec
;
2130 struct isl_upoly
*res
;
2131 struct isl_upoly
*base
;
2133 if (isl_upoly_is_cst(up
)) {
2138 rec
= isl_upoly_as_rec(up
);
2142 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2144 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
2146 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
2149 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2150 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2151 res
= isl_upoly_sum(res
,
2152 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
2153 isl_vec_copy(vec
)));
2156 isl_upoly_free(base
);
2166 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
2167 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
2170 struct isl_upoly
*up
;
2175 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, qp
->dim
), goto error
);
2177 if (qp
->div
->n_row
== 0)
2178 ext
= isl_vec_copy(pnt
->vec
);
2181 unsigned dim
= isl_dim_total(qp
->dim
);
2182 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
2186 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
2187 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2188 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
2189 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
2190 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
2191 qp
->div
->row
[i
][0]);
2195 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
2199 dim
= isl_dim_copy(qp
->dim
);
2200 isl_qpolynomial_free(qp
);
2201 isl_point_free(pnt
);
2203 return isl_qpolynomial_alloc(dim
, 0, up
);
2205 isl_qpolynomial_free(qp
);
2206 isl_point_free(pnt
);
2210 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
2211 __isl_keep
struct isl_upoly_cst
*cst2
)
2216 isl_int_mul(t
, cst1
->n
, cst2
->d
);
2217 isl_int_submul(t
, cst2
->n
, cst1
->d
);
2218 cmp
= isl_int_sgn(t
);
2223 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial
*qp1
,
2224 __isl_keep isl_qpolynomial
*qp2
)
2226 struct isl_upoly_cst
*cst1
, *cst2
;
2230 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), return -1);
2231 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), return -1);
2232 if (isl_qpolynomial_is_nan(qp1
))
2234 if (isl_qpolynomial_is_nan(qp2
))
2236 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2237 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2239 return isl_upoly_cmp(cst1
, cst2
) <= 0;
2242 __isl_give isl_qpolynomial
*isl_qpolynomial_min_cst(
2243 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2245 struct isl_upoly_cst
*cst1
, *cst2
;
2250 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2251 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2252 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2253 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2254 cmp
= isl_upoly_cmp(cst1
, cst2
);
2257 isl_qpolynomial_free(qp2
);
2259 isl_qpolynomial_free(qp1
);
2264 isl_qpolynomial_free(qp1
);
2265 isl_qpolynomial_free(qp2
);
2269 __isl_give isl_qpolynomial
*isl_qpolynomial_max_cst(
2270 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2272 struct isl_upoly_cst
*cst1
, *cst2
;
2277 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2278 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2279 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2280 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2281 cmp
= isl_upoly_cmp(cst1
, cst2
);
2284 isl_qpolynomial_free(qp2
);
2286 isl_qpolynomial_free(qp1
);
2291 isl_qpolynomial_free(qp1
);
2292 isl_qpolynomial_free(qp2
);
2296 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
2297 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
2298 unsigned first
, unsigned n
)
2307 qp
= isl_qpolynomial_cow(qp
);
2311 isl_assert(qp
->div
->ctx
, first
<= isl_dim_size(qp
->dim
, type
),
2314 g_pos
= pos(qp
->dim
, type
) + first
;
2316 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + g_pos
, n
);
2320 total
= qp
->div
->n_col
- 2;
2321 if (total
> g_pos
) {
2323 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
2326 for (i
= 0; i
< total
- g_pos
; ++i
)
2328 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
2334 qp
->dim
= isl_dim_insert(qp
->dim
, type
, first
, n
);
2340 isl_qpolynomial_free(qp
);
2344 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
2345 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
2349 pos
= isl_qpolynomial_dim(qp
, type
);
2351 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
2354 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
2355 __isl_take isl_pw_qpolynomial
*pwqp
,
2356 enum isl_dim_type type
, unsigned n
)
2360 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
2362 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
2365 static int *reordering_move(isl_ctx
*ctx
,
2366 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
2371 reordering
= isl_alloc_array(ctx
, int, len
);
2376 for (i
= 0; i
< dst
; ++i
)
2378 for (i
= 0; i
< n
; ++i
)
2379 reordering
[src
+ i
] = dst
+ i
;
2380 for (i
= 0; i
< src
- dst
; ++i
)
2381 reordering
[dst
+ i
] = dst
+ n
+ i
;
2382 for (i
= 0; i
< len
- src
- n
; ++i
)
2383 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
2385 for (i
= 0; i
< src
; ++i
)
2387 for (i
= 0; i
< n
; ++i
)
2388 reordering
[src
+ i
] = dst
+ i
;
2389 for (i
= 0; i
< dst
- src
; ++i
)
2390 reordering
[src
+ n
+ i
] = src
+ i
;
2391 for (i
= 0; i
< len
- dst
- n
; ++i
)
2392 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
2398 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
2399 __isl_take isl_qpolynomial
*qp
,
2400 enum isl_dim_type dst_type
, unsigned dst_pos
,
2401 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2407 qp
= isl_qpolynomial_cow(qp
);
2411 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_dim_size(qp
->dim
, src_type
),
2414 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
2415 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
2416 if (dst_type
> src_type
)
2419 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
2426 reordering
= reordering_move(qp
->dim
->ctx
,
2427 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
2431 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2436 qp
->dim
= isl_dim_move(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2442 isl_qpolynomial_free(qp
);
2446 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_dim
*dim
,
2447 isl_int
*f
, isl_int denom
)
2449 struct isl_upoly
*up
;
2454 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
, 1 + isl_dim_total(dim
));
2456 return isl_qpolynomial_alloc(dim
, 0, up
);
2459 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
2460 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
2464 struct isl_upoly
*up
;
2465 isl_qpolynomial
*qp
;
2471 isl_int_init(denom
);
2473 isl_constraint_get_coefficient(c
, type
, pos
, &denom
);
2474 isl_constraint_set_coefficient(c
, type
, pos
, c
->ctx
->zero
);
2475 sgn
= isl_int_sgn(denom
);
2476 isl_int_abs(denom
, denom
);
2477 up
= isl_upoly_from_affine(c
->ctx
, c
->line
[0], denom
,
2478 1 + isl_constraint_dim(c
, isl_dim_all
));
2480 isl_int_neg(denom
, denom
);
2481 isl_constraint_set_coefficient(c
, type
, pos
, denom
);
2483 dim
= isl_dim_copy(c
->bmap
->dim
);
2485 isl_int_clear(denom
);
2486 isl_constraint_free(c
);
2488 qp
= isl_qpolynomial_alloc(dim
, 0, up
);
2490 qp
= isl_qpolynomial_neg(qp
);
2494 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2495 * in "qp" by subs[i].
2497 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
2498 __isl_take isl_qpolynomial
*qp
,
2499 enum isl_dim_type type
, unsigned first
, unsigned n
,
2500 __isl_keep isl_qpolynomial
**subs
)
2503 struct isl_upoly
**ups
;
2508 qp
= isl_qpolynomial_cow(qp
);
2511 for (i
= 0; i
< n
; ++i
)
2515 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
2518 for (i
= 0; i
< n
; ++i
)
2519 isl_assert(qp
->dim
->ctx
, isl_dim_equal(qp
->dim
, subs
[i
]->dim
),
2522 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
2523 for (i
= 0; i
< n
; ++i
)
2524 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
2526 first
+= pos(qp
->dim
, type
);
2528 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
2531 for (i
= 0; i
< n
; ++i
)
2532 ups
[i
] = subs
[i
]->upoly
;
2534 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
2543 isl_qpolynomial_free(qp
);
2547 __isl_give isl_basic_set
*add_div_constraints(__isl_take isl_basic_set
*bset
,
2548 __isl_take isl_mat
*div
)
2556 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2559 total
= isl_basic_set_total_dim(bset
);
2560 for (i
= 0; i
< div
->n_row
; ++i
)
2561 if (isl_basic_set_add_div_constraints_var(bset
,
2562 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2569 isl_basic_set_free(bset
);
2573 /* Extend "bset" with extra set dimensions for each integer division
2574 * in "qp" and then call "fn" with the extended bset and the polynomial
2575 * that results from replacing each of the integer divisions by the
2576 * corresponding extra set dimension.
2578 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
2579 __isl_keep isl_basic_set
*bset
,
2580 int (*fn
)(__isl_take isl_basic_set
*bset
,
2581 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
2585 isl_qpolynomial
*poly
;
2589 if (qp
->div
->n_row
== 0)
2590 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
2593 div
= isl_mat_copy(qp
->div
);
2594 dim
= isl_dim_copy(qp
->dim
);
2595 dim
= isl_dim_add(dim
, isl_dim_set
, qp
->div
->n_row
);
2596 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
2597 bset
= isl_basic_set_copy(bset
);
2598 bset
= isl_basic_set_add(bset
, isl_dim_set
, qp
->div
->n_row
);
2599 bset
= add_div_constraints(bset
, div
);
2601 return fn(bset
, poly
, user
);
2606 /* Return total degree in variables first (inclusive) up to last (exclusive).
2608 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
2612 struct isl_upoly_rec
*rec
;
2616 if (isl_upoly_is_zero(up
))
2618 if (isl_upoly_is_cst(up
) || up
->var
< first
)
2621 rec
= isl_upoly_as_rec(up
);
2625 for (i
= 0; i
< rec
->n
; ++i
) {
2628 if (isl_upoly_is_zero(rec
->p
[i
]))
2630 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
2640 /* Return total degree in set variables.
2642 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
2650 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
2651 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
2652 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
2655 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
2656 unsigned pos
, int deg
)
2659 struct isl_upoly_rec
*rec
;
2664 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
2666 return isl_upoly_copy(up
);
2668 return isl_upoly_zero(up
->ctx
);
2671 rec
= isl_upoly_as_rec(up
);
2675 if (up
->var
== pos
) {
2677 return isl_upoly_copy(rec
->p
[deg
]);
2679 return isl_upoly_zero(up
->ctx
);
2682 up
= isl_upoly_copy(up
);
2683 up
= isl_upoly_cow(up
);
2684 rec
= isl_upoly_as_rec(up
);
2688 for (i
= 0; i
< rec
->n
; ++i
) {
2689 struct isl_upoly
*t
;
2690 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
2693 isl_upoly_free(rec
->p
[i
]);
2703 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2705 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
2706 __isl_keep isl_qpolynomial
*qp
,
2707 enum isl_dim_type type
, unsigned t_pos
, int deg
)
2710 struct isl_upoly
*up
;
2716 isl_assert(qp
->div
->ctx
, t_pos
< isl_dim_size(qp
->dim
, type
),
2719 g_pos
= pos(qp
->dim
, type
) + t_pos
;
2720 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
2722 c
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
, up
);
2725 isl_mat_free(c
->div
);
2726 c
->div
= isl_mat_copy(qp
->div
);
2731 isl_qpolynomial_free(c
);
2735 /* Homogenize the polynomial in the variables first (inclusive) up to
2736 * last (exclusive) by inserting powers of variable first.
2737 * Variable first is assumed not to appear in the input.
2739 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
2740 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
2741 int first
, int last
)
2744 struct isl_upoly_rec
*rec
;
2748 if (isl_upoly_is_zero(up
))
2752 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
2753 struct isl_upoly
*hom
;
2755 hom
= isl_upoly_pow(up
->ctx
, first
, target
- deg
);
2758 rec
= isl_upoly_as_rec(hom
);
2759 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
2764 up
= isl_upoly_cow(up
);
2765 rec
= isl_upoly_as_rec(up
);
2769 for (i
= 0; i
< rec
->n
; ++i
) {
2770 if (isl_upoly_is_zero(rec
->p
[i
]))
2772 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
2773 up
->var
< last
? deg
+ i
: i
, target
,
2785 /* Homogenize the polynomial in the set variables by introducing
2786 * powers of an extra set variable at position 0.
2788 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
2789 __isl_take isl_qpolynomial
*poly
)
2793 int deg
= isl_qpolynomial_degree(poly
);
2798 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_set
, 0, 1);
2799 poly
= isl_qpolynomial_cow(poly
);
2803 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
2804 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
2805 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
2812 isl_qpolynomial_free(poly
);
2816 __isl_give isl_term
*isl_term_alloc(__isl_take isl_dim
*dim
,
2817 __isl_take isl_mat
*div
)
2825 n
= isl_dim_total(dim
) + div
->n_row
;
2827 term
= isl_calloc(dim
->ctx
, struct isl_term
,
2828 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
2835 isl_int_init(term
->n
);
2836 isl_int_init(term
->d
);
2845 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
2854 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
2863 total
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
2865 dup
= isl_term_alloc(isl_dim_copy(term
->dim
), isl_mat_copy(term
->div
));
2869 isl_int_set(dup
->n
, term
->n
);
2870 isl_int_set(dup
->d
, term
->d
);
2872 for (i
= 0; i
< total
; ++i
)
2873 dup
->pow
[i
] = term
->pow
[i
];
2878 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
2886 return isl_term_dup(term
);
2889 void isl_term_free(__isl_take isl_term
*term
)
2894 if (--term
->ref
> 0)
2897 isl_dim_free(term
->dim
);
2898 isl_mat_free(term
->div
);
2899 isl_int_clear(term
->n
);
2900 isl_int_clear(term
->d
);
2904 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
2912 case isl_dim_out
: return isl_dim_size(term
->dim
, type
);
2913 case isl_dim_div
: return term
->div
->n_row
;
2914 case isl_dim_all
: return isl_dim_total(term
->dim
) + term
->div
->n_row
;
2919 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
2921 return term
? term
->dim
->ctx
: NULL
;
2924 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
2928 isl_int_set(*n
, term
->n
);
2931 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
2935 isl_int_set(*d
, term
->d
);
2938 int isl_term_get_exp(__isl_keep isl_term
*term
,
2939 enum isl_dim_type type
, unsigned pos
)
2944 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
2946 if (type
>= isl_dim_set
)
2947 pos
+= isl_dim_size(term
->dim
, isl_dim_param
);
2948 if (type
>= isl_dim_div
)
2949 pos
+= isl_dim_size(term
->dim
, isl_dim_set
);
2951 return term
->pow
[pos
];
2954 __isl_give isl_div
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
2956 isl_basic_map
*bmap
;
2963 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
2966 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
2967 /* No nested divs for now */
2968 isl_assert(term
->dim
->ctx
,
2969 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
2970 term
->div
->n_row
) == -1,
2973 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(term
->dim
), 1, 0, 0);
2974 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
2977 isl_seq_cpy(bmap
->div
[k
], term
->div
->row
[pos
], 2 + total
);
2979 return isl_basic_map_div(bmap
, k
);
2981 isl_basic_map_free(bmap
);
2985 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
2986 int (*fn
)(__isl_take isl_term
*term
, void *user
),
2987 __isl_take isl_term
*term
, void *user
)
2990 struct isl_upoly_rec
*rec
;
2995 if (isl_upoly_is_zero(up
))
2998 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
2999 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3000 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3002 if (isl_upoly_is_cst(up
)) {
3003 struct isl_upoly_cst
*cst
;
3004 cst
= isl_upoly_as_cst(up
);
3007 term
= isl_term_cow(term
);
3010 isl_int_set(term
->n
, cst
->n
);
3011 isl_int_set(term
->d
, cst
->d
);
3012 if (fn(isl_term_copy(term
), user
) < 0)
3017 rec
= isl_upoly_as_rec(up
);
3021 for (i
= 0; i
< rec
->n
; ++i
) {
3022 term
= isl_term_cow(term
);
3025 term
->pow
[up
->var
] = i
;
3026 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3030 term
->pow
[up
->var
] = 0;
3034 isl_term_free(term
);
3038 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3039 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3046 term
= isl_term_alloc(isl_dim_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3050 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3052 isl_term_free(term
);
3054 return term
? 0 : -1;
3057 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3059 struct isl_upoly
*up
;
3060 isl_qpolynomial
*qp
;
3066 n
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
3068 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3069 for (i
= 0; i
< n
; ++i
) {
3072 up
= isl_upoly_mul(up
,
3073 isl_upoly_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3076 qp
= isl_qpolynomial_alloc(isl_dim_copy(term
->dim
), term
->div
->n_row
, up
);
3079 isl_mat_free(qp
->div
);
3080 qp
->div
= isl_mat_copy(term
->div
);
3084 isl_term_free(term
);
3087 isl_qpolynomial_free(qp
);
3088 isl_term_free(term
);
3092 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3093 __isl_take isl_dim
*dim
)
3102 if (isl_dim_equal(qp
->dim
, dim
)) {
3107 qp
= isl_qpolynomial_cow(qp
);
3111 extra
= isl_dim_size(dim
, isl_dim_set
) -
3112 isl_dim_size(qp
->dim
, isl_dim_set
);
3113 total
= isl_dim_total(qp
->dim
);
3114 if (qp
->div
->n_row
) {
3117 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
3120 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3122 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
3127 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
3130 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3131 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
3133 isl_dim_free(qp
->dim
);
3139 isl_qpolynomial_free(qp
);
3143 /* For each parameter or variable that does not appear in qp,
3144 * first eliminate the variable from all constraints and then set it to zero.
3146 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3147 __isl_keep isl_qpolynomial
*qp
)
3158 d
= isl_dim_total(set
->dim
);
3159 active
= isl_calloc_array(set
->ctx
, int, d
);
3160 if (set_active(qp
, active
) < 0)
3163 for (i
= 0; i
< d
; ++i
)
3172 nparam
= isl_dim_size(set
->dim
, isl_dim_param
);
3173 nvar
= isl_dim_size(set
->dim
, isl_dim_set
);
3174 for (i
= 0; i
< nparam
; ++i
) {
3177 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
3178 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
3180 for (i
= 0; i
< nvar
; ++i
) {
3181 if (active
[nparam
+ i
])
3183 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
3184 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
3196 struct isl_opt_data
{
3197 isl_qpolynomial
*qp
;
3199 isl_qpolynomial
*opt
;
3203 static int opt_fn(__isl_take isl_point
*pnt
, void *user
)
3205 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
3206 isl_qpolynomial
*val
;
3208 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
3212 } else if (data
->max
) {
3213 data
->opt
= isl_qpolynomial_max_cst(data
->opt
, val
);
3215 data
->opt
= isl_qpolynomial_min_cst(data
->opt
, val
);
3221 __isl_give isl_qpolynomial
*isl_qpolynomial_opt_on_domain(
3222 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
3224 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
3229 if (isl_upoly_is_cst(qp
->upoly
)) {
3234 set
= fix_inactive(set
, qp
);
3237 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
3241 data
.opt
= isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp
));
3244 isl_qpolynomial_free(qp
);
3248 isl_qpolynomial_free(qp
);
3249 isl_qpolynomial_free(data
.opt
);
3253 __isl_give isl_qpolynomial
*isl_qpolynomial_morph(__isl_take isl_qpolynomial
*qp
,
3254 __isl_take isl_morph
*morph
)
3258 struct isl_upoly
*up
;
3260 struct isl_upoly
**subs
;
3263 qp
= isl_qpolynomial_cow(qp
);
3268 isl_assert(ctx
, isl_dim_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
3270 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, morph
->inv
->n_row
- 1);
3274 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3275 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
3276 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
3278 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, morph
->inv
->n_row
- 1, subs
);
3280 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3281 isl_upoly_free(subs
[i
]);
3284 mat
= isl_mat_diagonal(isl_mat_identity(ctx
, 1), isl_mat_copy(morph
->inv
));
3285 mat
= isl_mat_diagonal(mat
, isl_mat_identity(ctx
, qp
->div
->n_row
));
3286 qp
->div
= isl_mat_product(qp
->div
, mat
);
3287 isl_dim_free(qp
->dim
);
3288 qp
->dim
= isl_dim_copy(morph
->ran
->dim
);
3290 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
3293 isl_morph_free(morph
);
3297 isl_qpolynomial_free(qp
);
3298 isl_morph_free(morph
);
3302 static int neg_entry(void **entry
, void *user
)
3304 isl_pw_qpolynomial
**pwqp
= (isl_pw_qpolynomial
**)entry
;
3306 *pwqp
= isl_pw_qpolynomial_neg(*pwqp
);
3308 return *pwqp
? 0 : -1;
3311 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_neg(
3312 __isl_take isl_union_pw_qpolynomial
*upwqp
)
3314 upwqp
= isl_union_pw_qpolynomial_cow(upwqp
);
3318 if (isl_hash_table_foreach(upwqp
->dim
->ctx
, &upwqp
->table
,
3319 &neg_entry
, NULL
) < 0)
3324 isl_union_pw_qpolynomial_free(upwqp
);
3328 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_sub(
3329 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3330 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3332 return isl_union_pw_qpolynomial_add(upwqp1
,
3333 isl_union_pw_qpolynomial_neg(upwqp2
));
3336 static int mul_entry(void **entry
, void *user
)
3338 struct isl_union_pw_qpolynomial_match_bin_data
*data
= user
;
3340 struct isl_hash_table_entry
*entry2
;
3341 isl_pw_qpolynomial
*pwpq
= *entry
;
3344 hash
= isl_dim_get_hash(pwpq
->dim
);
3345 entry2
= isl_hash_table_find(data
->u2
->dim
->ctx
, &data
->u2
->table
,
3346 hash
, &has_dim
, pwpq
->dim
, 0);
3350 pwpq
= isl_pw_qpolynomial_copy(pwpq
);
3351 pwpq
= isl_pw_qpolynomial_mul(pwpq
,
3352 isl_pw_qpolynomial_copy(entry2
->data
));
3354 empty
= isl_pw_qpolynomial_is_zero(pwpq
);
3356 isl_pw_qpolynomial_free(pwpq
);
3360 isl_pw_qpolynomial_free(pwpq
);
3364 data
->res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(data
->res
, pwpq
);
3369 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
3370 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3371 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3373 return match_bin_op(upwqp1
, upwqp2
, &mul_entry
);
3376 /* Reorder the columns of the given div definitions according to the
3379 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
3380 __isl_take isl_reordering
*r
)
3389 extra
= isl_dim_total(r
->dim
) + div
->n_row
- r
->len
;
3390 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
3394 for (i
= 0; i
< div
->n_row
; ++i
) {
3395 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
3396 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
3397 for (j
= 0; j
< r
->len
; ++j
)
3398 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
3399 div
->row
[i
][2 + j
]);
3402 isl_reordering_free(r
);
3406 isl_reordering_free(r
);
3411 /* Reorder the dimension of "qp" according to the given reordering.
3413 __isl_give isl_qpolynomial
*isl_qpolynomial_realign(
3414 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
3416 qp
= isl_qpolynomial_cow(qp
);
3420 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
3424 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
3428 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
3432 qp
= isl_qpolynomial_reset_dim(qp
, isl_dim_copy(r
->dim
));
3434 isl_reordering_free(r
);
3437 isl_qpolynomial_free(qp
);
3438 isl_reordering_free(r
);
3442 struct isl_split_periods_data
{
3444 isl_pw_qpolynomial
*res
;
3447 /* Create a slice where the integer division "div" has the fixed value "v".
3448 * In particular, if "div" refers to floor(f/m), then create a slice
3450 * m v <= f <= m v + (m - 1)
3455 * -f + m v + (m - 1) >= 0
3457 static __isl_give isl_set
*set_div_slice(__isl_take isl_dim
*dim
,
3458 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
3461 isl_basic_set
*bset
= NULL
;
3467 total
= isl_dim_total(dim
);
3468 bset
= isl_basic_set_alloc_dim(isl_dim_copy(dim
), 0, 0, 2);
3470 k
= isl_basic_set_alloc_inequality(bset
);
3473 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
3474 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
3476 k
= isl_basic_set_alloc_inequality(bset
);
3479 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
3480 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
3481 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
3482 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
3485 return isl_set_from_basic_set(bset
);
3487 isl_basic_set_free(bset
);
3492 static int split_periods(__isl_take isl_set
*set
,
3493 __isl_take isl_qpolynomial
*qp
, void *user
);
3495 /* Create a slice of the domain "set" such that integer division "div"
3496 * has the fixed value "v" and add the results to data->res,
3497 * replacing the integer division by "v" in "qp".
3499 static int set_div(__isl_take isl_set
*set
,
3500 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
3501 struct isl_split_periods_data
*data
)
3506 struct isl_upoly
*cst
;
3509 slice
= set_div_slice(isl_set_get_dim(set
), qp
, div
, v
);
3510 set
= isl_set_intersect(set
, slice
);
3512 qp
= isl_qpolynomial_cow(qp
);
3516 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
3519 total
= isl_dim_total(qp
->dim
);
3520 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &cst
);
3521 isl_upoly_free(cst
);
3525 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
3528 for (i
= 0; i
< total
+ div
; ++i
)
3530 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
3531 reordering
[i
] = i
- 1;
3532 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
3533 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
3534 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3537 if (!qp
->upoly
|| !qp
->div
)
3540 return split_periods(set
, qp
, data
);
3543 isl_qpolynomial_free(qp
);
3547 /* Split the domain "set" such that integer division "div"
3548 * has a fixed value (ranging from "min" to "max") on each slice
3549 * and add the results to data->res.
3551 static int split_div(__isl_take isl_set
*set
,
3552 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
3553 struct isl_split_periods_data
*data
)
3555 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
3556 isl_set
*set_i
= isl_set_copy(set
);
3557 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
3559 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
3563 isl_qpolynomial_free(qp
);
3567 isl_qpolynomial_free(qp
);
3571 /* If "qp" refers to any integer division
3572 * that can only attain "max_periods" distinct values on "set"
3573 * then split the domain along those distinct values.
3574 * Add the results (or the original if no splitting occurs)
3577 static int split_periods(__isl_take isl_set
*set
,
3578 __isl_take isl_qpolynomial
*qp
, void *user
)
3581 isl_pw_qpolynomial
*pwqp
;
3582 struct isl_split_periods_data
*data
;
3587 data
= (struct isl_split_periods_data
*)user
;
3592 if (qp
->div
->n_row
== 0) {
3593 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
3594 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
3600 total
= isl_dim_total(qp
->dim
);
3601 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
3602 enum isl_lp_result lp_res
;
3604 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
3605 qp
->div
->n_row
) != -1)
3608 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
3609 set
->ctx
->one
, &min
, NULL
, NULL
);
3610 if (lp_res
== isl_lp_error
)
3612 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
3614 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
3616 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
3617 set
->ctx
->one
, &max
, NULL
, NULL
);
3618 if (lp_res
== isl_lp_error
)
3620 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
3622 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
3624 isl_int_sub(max
, max
, min
);
3625 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
3626 isl_int_add(max
, max
, min
);
3631 if (i
< qp
->div
->n_row
) {
3632 r
= split_div(set
, qp
, i
, min
, max
, data
);
3634 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
3635 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
3647 isl_qpolynomial_free(qp
);
3651 /* If any quasi-polynomial in pwqp refers to any integer division
3652 * that can only attain "max_periods" distinct values on its domain
3653 * then split the domain along those distinct values.
3655 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
3656 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
3658 struct isl_split_periods_data data
;
3660 data
.max_periods
= max_periods
;
3661 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp
));
3663 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
3666 isl_pw_qpolynomial_free(pwqp
);
3670 isl_pw_qpolynomial_free(data
.res
);
3671 isl_pw_qpolynomial_free(pwqp
);
3675 /* Construct a piecewise quasipolynomial that is constant on the given
3676 * domain. In particular, it is
3679 * infinity if cst == -1
3681 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
3682 __isl_take isl_basic_set
*bset
, int cst
)
3685 isl_qpolynomial
*qp
;
3690 bset
= isl_basic_map_domain(isl_basic_map_from_range(bset
));
3691 dim
= isl_basic_set_get_dim(bset
);
3693 qp
= isl_qpolynomial_infty(dim
);
3695 qp
= isl_qpolynomial_zero(dim
);
3697 qp
= isl_qpolynomial_one(dim
);
3698 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
3701 /* Factor bset, call fn on each of the factors and return the product.
3703 * If no factors can be found, simply call fn on the input.
3704 * Otherwise, construct the factors based on the factorizer,
3705 * call fn on each factor and compute the product.
3707 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
3708 __isl_take isl_basic_set
*bset
,
3709 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
3715 isl_qpolynomial
*qp
;
3716 isl_pw_qpolynomial
*pwqp
;
3720 f
= isl_basic_set_factorizer(bset
);
3723 if (f
->n_group
== 0) {
3724 isl_factorizer_free(f
);
3728 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
3729 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
3731 dim
= isl_basic_set_get_dim(bset
);
3732 dim
= isl_dim_domain(dim
);
3733 set
= isl_set_universe(isl_dim_copy(dim
));
3734 qp
= isl_qpolynomial_one(dim
);
3735 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
3737 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
3739 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
3740 isl_basic_set
*bset_i
;
3741 isl_pw_qpolynomial
*pwqp_i
;
3743 bset_i
= isl_basic_set_copy(bset
);
3744 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
3745 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
3746 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
3748 bset_i
= isl_basic_set_drop_dims(bset_i
,
3749 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
3750 bset_i
= isl_basic_set_drop_dims(bset_i
, nparam
, n
);
3752 pwqp_i
= fn(bset_i
);
3753 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
3758 isl_basic_set_free(bset
);
3759 isl_factorizer_free(f
);
3763 isl_basic_set_free(bset
);
3767 /* Factor bset, call fn on each of the factors and return the product.
3768 * The function is assumed to evaluate to zero on empty domains,
3769 * to one on zero-dimensional domains and to infinity on unbounded domains
3770 * and will not be called explicitly on zero-dimensional or unbounded domains.
3772 * We first check for some special cases and remove all equalities.
3773 * Then we hand over control to compressed_multiplicative_call.
3775 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
3776 __isl_take isl_basic_set
*bset
,
3777 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
3781 isl_pw_qpolynomial
*pwqp
;
3782 unsigned orig_nvar
, final_nvar
;
3787 if (isl_basic_set_fast_is_empty(bset
))
3788 return constant_on_domain(bset
, 0);
3790 orig_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
3793 return constant_on_domain(bset
, 1);
3795 bounded
= isl_basic_set_is_bounded(bset
);
3799 return constant_on_domain(bset
, -1);
3801 if (bset
->n_eq
== 0)
3802 return compressed_multiplicative_call(bset
, fn
);
3804 morph
= isl_basic_set_full_compression(bset
);
3805 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
3807 final_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
3809 pwqp
= compressed_multiplicative_call(bset
, fn
);
3811 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, orig_nvar
);
3812 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, final_nvar
);
3813 morph
= isl_morph_inverse(morph
);
3815 pwqp
= isl_pw_qpolynomial_morph(pwqp
, morph
);
3819 isl_basic_set_free(bset
);