2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 #include <isl_union_map_private.h>
14 #include <isl_polynomial_private.h>
15 #include <isl_point_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_map_private.h>
19 static unsigned pos(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
22 case isl_dim_param
: return 0;
23 case isl_dim_in
: return dim
->nparam
;
24 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
29 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
37 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
42 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
44 return (struct isl_upoly_cst
*)up
;
47 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
52 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
54 return (struct isl_upoly_rec
*)up
;
57 int isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
58 __isl_keep
struct isl_upoly
*up2
)
61 struct isl_upoly_rec
*rec1
, *rec2
;
67 if (up1
->var
!= up2
->var
)
69 if (isl_upoly_is_cst(up1
)) {
70 struct isl_upoly_cst
*cst1
, *cst2
;
71 cst1
= isl_upoly_as_cst(up1
);
72 cst2
= isl_upoly_as_cst(up2
);
75 return isl_int_eq(cst1
->n
, cst2
->n
) &&
76 isl_int_eq(cst1
->d
, cst2
->d
);
79 rec1
= isl_upoly_as_rec(up1
);
80 rec2
= isl_upoly_as_rec(up2
);
84 if (rec1
->n
!= rec2
->n
)
87 for (i
= 0; i
< rec1
->n
; ++i
) {
88 int eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
96 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
98 struct isl_upoly_cst
*cst
;
102 if (!isl_upoly_is_cst(up
))
105 cst
= isl_upoly_as_cst(up
);
109 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
112 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
114 struct isl_upoly_cst
*cst
;
118 if (!isl_upoly_is_cst(up
))
121 cst
= isl_upoly_as_cst(up
);
125 return isl_int_sgn(cst
->n
);
128 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
130 struct isl_upoly_cst
*cst
;
134 if (!isl_upoly_is_cst(up
))
137 cst
= isl_upoly_as_cst(up
);
141 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
144 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
146 struct isl_upoly_cst
*cst
;
150 if (!isl_upoly_is_cst(up
))
153 cst
= isl_upoly_as_cst(up
);
157 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
160 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
162 struct isl_upoly_cst
*cst
;
166 if (!isl_upoly_is_cst(up
))
169 cst
= isl_upoly_as_cst(up
);
173 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
176 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
178 struct isl_upoly_cst
*cst
;
182 if (!isl_upoly_is_cst(up
))
185 cst
= isl_upoly_as_cst(up
);
189 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
192 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
194 struct isl_upoly_cst
*cst
;
198 if (!isl_upoly_is_cst(up
))
201 cst
= isl_upoly_as_cst(up
);
205 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
208 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
210 struct isl_upoly_cst
*cst
;
212 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
221 isl_int_init(cst
->n
);
222 isl_int_init(cst
->d
);
227 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
229 struct isl_upoly_cst
*cst
;
231 cst
= isl_upoly_cst_alloc(ctx
);
235 isl_int_set_si(cst
->n
, 0);
236 isl_int_set_si(cst
->d
, 1);
241 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
243 struct isl_upoly_cst
*cst
;
245 cst
= isl_upoly_cst_alloc(ctx
);
249 isl_int_set_si(cst
->n
, 1);
250 isl_int_set_si(cst
->d
, 0);
255 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
257 struct isl_upoly_cst
*cst
;
259 cst
= isl_upoly_cst_alloc(ctx
);
263 isl_int_set_si(cst
->n
, -1);
264 isl_int_set_si(cst
->d
, 0);
269 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
271 struct isl_upoly_cst
*cst
;
273 cst
= isl_upoly_cst_alloc(ctx
);
277 isl_int_set_si(cst
->n
, 0);
278 isl_int_set_si(cst
->d
, 0);
283 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
284 isl_int n
, isl_int d
)
286 struct isl_upoly_cst
*cst
;
288 cst
= isl_upoly_cst_alloc(ctx
);
292 isl_int_set(cst
->n
, n
);
293 isl_int_set(cst
->d
, d
);
298 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
301 struct isl_upoly_rec
*rec
;
303 isl_assert(ctx
, var
>= 0, return NULL
);
304 isl_assert(ctx
, size
>= 0, return NULL
);
305 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
306 sizeof(struct isl_upoly_rec
) +
307 (size
- 1) * sizeof(struct isl_upoly
*));
322 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_dim(
323 __isl_take isl_qpolynomial
*qp
, __isl_take isl_dim
*dim
)
328 isl_dim_free(qp
->dim
);
333 isl_qpolynomial_free(qp
);
338 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
340 return qp
? qp
->dim
->ctx
: NULL
;
343 __isl_give isl_dim
*isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial
*qp
)
345 return qp
? isl_dim_copy(qp
->dim
) : NULL
;
348 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
349 enum isl_dim_type type
)
351 return qp
? isl_dim_size(qp
->dim
, type
) : 0;
354 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
356 return qp
? isl_upoly_is_zero(qp
->upoly
) : -1;
359 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
361 return qp
? isl_upoly_is_one(qp
->upoly
) : -1;
364 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
366 return qp
? isl_upoly_is_nan(qp
->upoly
) : -1;
369 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
371 return qp
? isl_upoly_is_infty(qp
->upoly
) : -1;
374 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
376 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : -1;
379 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
381 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
384 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
386 isl_int_clear(cst
->n
);
387 isl_int_clear(cst
->d
);
390 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
394 for (i
= 0; i
< rec
->n
; ++i
)
395 isl_upoly_free(rec
->p
[i
]);
398 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
407 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
409 struct isl_upoly_cst
*cst
;
410 struct isl_upoly_cst
*dup
;
412 cst
= isl_upoly_as_cst(up
);
416 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
419 isl_int_set(dup
->n
, cst
->n
);
420 isl_int_set(dup
->d
, cst
->d
);
425 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
428 struct isl_upoly_rec
*rec
;
429 struct isl_upoly_rec
*dup
;
431 rec
= isl_upoly_as_rec(up
);
435 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
439 for (i
= 0; i
< rec
->n
; ++i
) {
440 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
448 isl_upoly_free(&dup
->up
);
452 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
454 struct isl_upoly
*dup
;
459 if (isl_upoly_is_cst(up
))
460 return isl_upoly_dup_cst(up
);
462 return isl_upoly_dup_rec(up
);
465 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
473 return isl_upoly_dup(up
);
476 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
485 upoly_free_cst((struct isl_upoly_cst
*)up
);
487 upoly_free_rec((struct isl_upoly_rec
*)up
);
489 isl_ctx_deref(up
->ctx
);
493 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
498 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
499 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
500 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
501 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
506 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
507 __isl_take
struct isl_upoly
*up2
)
509 struct isl_upoly_cst
*cst1
;
510 struct isl_upoly_cst
*cst2
;
512 up1
= isl_upoly_cow(up1
);
516 cst1
= isl_upoly_as_cst(up1
);
517 cst2
= isl_upoly_as_cst(up2
);
519 if (isl_int_eq(cst1
->d
, cst2
->d
))
520 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
522 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
523 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
524 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
527 isl_upoly_cst_reduce(cst1
);
537 static __isl_give
struct isl_upoly
*replace_by_zero(
538 __isl_take
struct isl_upoly
*up
)
546 return isl_upoly_zero(ctx
);
549 static __isl_give
struct isl_upoly
*replace_by_constant_term(
550 __isl_take
struct isl_upoly
*up
)
552 struct isl_upoly_rec
*rec
;
553 struct isl_upoly
*cst
;
558 rec
= isl_upoly_as_rec(up
);
561 cst
= isl_upoly_copy(rec
->p
[0]);
569 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
570 __isl_take
struct isl_upoly
*up2
)
573 struct isl_upoly_rec
*rec1
, *rec2
;
578 if (isl_upoly_is_nan(up1
)) {
583 if (isl_upoly_is_nan(up2
)) {
588 if (isl_upoly_is_zero(up1
)) {
593 if (isl_upoly_is_zero(up2
)) {
598 if (up1
->var
< up2
->var
)
599 return isl_upoly_sum(up2
, up1
);
601 if (up2
->var
< up1
->var
) {
602 struct isl_upoly_rec
*rec
;
603 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
607 up1
= isl_upoly_cow(up1
);
608 rec
= isl_upoly_as_rec(up1
);
611 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
613 up1
= replace_by_constant_term(up1
);
617 if (isl_upoly_is_cst(up1
))
618 return isl_upoly_sum_cst(up1
, up2
);
620 rec1
= isl_upoly_as_rec(up1
);
621 rec2
= isl_upoly_as_rec(up2
);
625 if (rec1
->n
< rec2
->n
)
626 return isl_upoly_sum(up2
, up1
);
628 up1
= isl_upoly_cow(up1
);
629 rec1
= isl_upoly_as_rec(up1
);
633 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
634 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
635 isl_upoly_copy(rec2
->p
[i
]));
638 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
639 isl_upoly_free(rec1
->p
[i
]);
645 up1
= replace_by_zero(up1
);
646 else if (rec1
->n
== 1)
647 up1
= replace_by_constant_term(up1
);
658 __isl_give
struct isl_upoly
*isl_upoly_neg_cst(__isl_take
struct isl_upoly
*up
)
660 struct isl_upoly_cst
*cst
;
662 if (isl_upoly_is_zero(up
))
665 up
= isl_upoly_cow(up
);
669 cst
= isl_upoly_as_cst(up
);
671 isl_int_neg(cst
->n
, cst
->n
);
676 __isl_give
struct isl_upoly
*isl_upoly_neg(__isl_take
struct isl_upoly
*up
)
679 struct isl_upoly_rec
*rec
;
684 if (isl_upoly_is_cst(up
))
685 return isl_upoly_neg_cst(up
);
687 up
= isl_upoly_cow(up
);
688 rec
= isl_upoly_as_rec(up
);
692 for (i
= 0; i
< rec
->n
; ++i
) {
693 rec
->p
[i
] = isl_upoly_neg(rec
->p
[i
]);
704 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
705 __isl_take
struct isl_upoly
*up2
)
707 struct isl_upoly_cst
*cst1
;
708 struct isl_upoly_cst
*cst2
;
710 up1
= isl_upoly_cow(up1
);
714 cst1
= isl_upoly_as_cst(up1
);
715 cst2
= isl_upoly_as_cst(up2
);
717 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
718 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
720 isl_upoly_cst_reduce(cst1
);
730 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
731 __isl_take
struct isl_upoly
*up2
)
733 struct isl_upoly_rec
*rec1
;
734 struct isl_upoly_rec
*rec2
;
735 struct isl_upoly_rec
*res
;
739 rec1
= isl_upoly_as_rec(up1
);
740 rec2
= isl_upoly_as_rec(up2
);
743 size
= rec1
->n
+ rec2
->n
- 1;
744 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
748 for (i
= 0; i
< rec1
->n
; ++i
) {
749 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
750 isl_upoly_copy(rec1
->p
[i
]));
755 for (; i
< size
; ++i
) {
756 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
761 for (i
= 0; i
< rec1
->n
; ++i
) {
762 for (j
= 1; j
< rec2
->n
; ++j
) {
763 struct isl_upoly
*up
;
764 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
765 isl_upoly_copy(rec1
->p
[i
]));
766 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
779 isl_upoly_free(&res
->up
);
783 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
784 __isl_take
struct isl_upoly
*up2
)
789 if (isl_upoly_is_nan(up1
)) {
794 if (isl_upoly_is_nan(up2
)) {
799 if (isl_upoly_is_zero(up1
)) {
804 if (isl_upoly_is_zero(up2
)) {
809 if (isl_upoly_is_one(up1
)) {
814 if (isl_upoly_is_one(up2
)) {
819 if (up1
->var
< up2
->var
)
820 return isl_upoly_mul(up2
, up1
);
822 if (up2
->var
< up1
->var
) {
824 struct isl_upoly_rec
*rec
;
825 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
826 isl_ctx
*ctx
= up1
->ctx
;
829 return isl_upoly_nan(ctx
);
831 up1
= isl_upoly_cow(up1
);
832 rec
= isl_upoly_as_rec(up1
);
836 for (i
= 0; i
< rec
->n
; ++i
) {
837 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
838 isl_upoly_copy(up2
));
846 if (isl_upoly_is_cst(up1
))
847 return isl_upoly_mul_cst(up1
, up2
);
849 return isl_upoly_mul_rec(up1
, up2
);
856 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_dim
*dim
,
857 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
859 struct isl_qpolynomial
*qp
= NULL
;
865 total
= isl_dim_total(dim
);
867 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
872 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
883 isl_qpolynomial_free(qp
);
887 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
896 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
898 struct isl_qpolynomial
*dup
;
903 dup
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
,
904 isl_upoly_copy(qp
->upoly
));
907 isl_mat_free(dup
->div
);
908 dup
->div
= isl_mat_copy(qp
->div
);
914 isl_qpolynomial_free(dup
);
918 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
926 return isl_qpolynomial_dup(qp
);
929 void isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
937 isl_dim_free(qp
->dim
);
938 isl_mat_free(qp
->div
);
939 isl_upoly_free(qp
->upoly
);
944 __isl_give
struct isl_upoly
*isl_upoly_pow(isl_ctx
*ctx
, int pos
, int power
)
947 struct isl_upoly
*up
;
948 struct isl_upoly_rec
*rec
;
949 struct isl_upoly_cst
*cst
;
951 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
954 for (i
= 0; i
< 1 + power
; ++i
) {
955 rec
->p
[i
] = isl_upoly_zero(ctx
);
960 cst
= isl_upoly_as_cst(rec
->p
[power
]);
961 isl_int_set_si(cst
->n
, 1);
965 isl_upoly_free(&rec
->up
);
969 /* r array maps original positions to new positions.
971 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
975 struct isl_upoly_rec
*rec
;
976 struct isl_upoly
*base
;
977 struct isl_upoly
*res
;
979 if (isl_upoly_is_cst(up
))
982 rec
= isl_upoly_as_rec(up
);
986 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
988 base
= isl_upoly_pow(up
->ctx
, r
[up
->var
], 1);
989 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
991 for (i
= rec
->n
- 2; i
>= 0; --i
) {
992 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
993 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
996 isl_upoly_free(base
);
1005 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
1010 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1011 div1
->n_col
>= div2
->n_col
, return -1);
1013 if (div1
->n_row
== div2
->n_row
)
1014 return isl_mat_is_equal(div1
, div2
);
1016 n_row
= div1
->n_row
;
1017 n_col
= div1
->n_col
;
1018 div1
->n_row
= div2
->n_row
;
1019 div1
->n_col
= div2
->n_col
;
1021 equal
= isl_mat_is_equal(div1
, div2
);
1023 div1
->n_row
= n_row
;
1024 div1
->n_col
= n_col
;
1029 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
1030 __isl_keep isl_mat
*src
, int s
, int *exp
)
1033 unsigned c
= src
->n_col
- src
->n_row
;
1035 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
1036 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
1038 for (i
= 0; i
< s
; ++i
)
1039 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
1042 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1046 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1047 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1052 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1055 struct isl_div_sort_info
{
1060 static int div_sort_cmp(const void *p1
, const void *p2
)
1062 const struct isl_div_sort_info
*i1
, *i2
;
1063 i1
= (const struct isl_div_sort_info
*) p1
;
1064 i2
= (const struct isl_div_sort_info
*) p2
;
1066 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1069 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1072 struct isl_div_sort_info
*array
= NULL
;
1074 int *reordering
= NULL
;
1079 if (qp
->div
->n_row
<= 1)
1082 div_pos
= isl_dim_total(qp
->dim
);
1084 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1086 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1087 reordering
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_col
- 2);
1088 if (!array
|| !pos
|| !reordering
)
1091 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1092 array
[i
].div
= qp
->div
;
1097 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1100 for (i
= 0; i
< div_pos
; ++i
)
1103 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
1104 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
;
1106 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1108 if (pos
[array
[i
].row
] == i
)
1110 qp
->div
= isl_mat_cow(qp
->div
);
1111 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1112 t
= pos
[array
[i
].row
];
1113 pos
[array
[i
].row
] = pos
[i
];
1117 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1119 if (!qp
->upoly
|| !qp
->div
)
1131 isl_qpolynomial_free(qp
);
1135 static __isl_give isl_mat
*merge_divs(__isl_keep isl_mat
*div1
,
1136 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
1139 isl_mat
*div
= NULL
;
1140 unsigned d
= div1
->n_col
- div1
->n_row
;
1142 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
1143 d
+ div1
->n_row
+ div2
->n_row
);
1147 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
1150 expand_row(div
, k
, div1
, i
, exp1
);
1151 expand_row(div
, k
+ 1, div2
, j
, exp2
);
1153 cmp
= cmp_row(div
, k
, k
+ 1);
1157 } else if (cmp
< 0) {
1161 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
1164 for (; i
< div1
->n_row
; ++i
, ++k
) {
1165 expand_row(div
, k
, div1
, i
, exp1
);
1168 for (; j
< div2
->n_row
; ++j
, ++k
) {
1169 expand_row(div
, k
, div2
, j
, exp2
);
1179 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1180 int *exp
, int first
)
1183 struct isl_upoly_rec
*rec
;
1185 if (isl_upoly_is_cst(up
))
1188 if (up
->var
< first
)
1191 if (exp
[up
->var
- first
] == up
->var
- first
)
1194 up
= isl_upoly_cow(up
);
1198 up
->var
= exp
[up
->var
- first
] + first
;
1200 rec
= isl_upoly_as_rec(up
);
1204 for (i
= 0; i
< rec
->n
; ++i
) {
1205 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1216 static __isl_give isl_qpolynomial
*with_merged_divs(
1217 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1218 __isl_take isl_qpolynomial
*qp2
),
1219 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1223 isl_mat
*div
= NULL
;
1225 qp1
= isl_qpolynomial_cow(qp1
);
1226 qp2
= isl_qpolynomial_cow(qp2
);
1231 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1232 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1234 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
1235 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
1239 div
= merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1243 isl_mat_free(qp1
->div
);
1244 qp1
->div
= isl_mat_copy(div
);
1245 isl_mat_free(qp2
->div
);
1246 qp2
->div
= isl_mat_copy(div
);
1248 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1249 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1251 if (!qp1
->upoly
|| !qp2
->upoly
)
1258 return fn(qp1
, qp2
);
1263 isl_qpolynomial_free(qp1
);
1264 isl_qpolynomial_free(qp2
);
1268 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1269 __isl_take isl_qpolynomial
*qp2
)
1271 qp1
= isl_qpolynomial_cow(qp1
);
1276 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1277 return isl_qpolynomial_add(qp2
, qp1
);
1279 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1280 if (!compatible_divs(qp1
->div
, qp2
->div
))
1281 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1283 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1287 isl_qpolynomial_free(qp2
);
1291 isl_qpolynomial_free(qp1
);
1292 isl_qpolynomial_free(qp2
);
1296 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1297 __isl_keep isl_set
*dom
,
1298 __isl_take isl_qpolynomial
*qp1
,
1299 __isl_take isl_qpolynomial
*qp2
)
1301 return isl_qpolynomial_add(qp1
, qp2
);
1304 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1305 __isl_take isl_qpolynomial
*qp2
)
1307 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1310 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1312 qp
= isl_qpolynomial_cow(qp
);
1317 qp
->upoly
= isl_upoly_neg(qp
->upoly
);
1323 isl_qpolynomial_free(qp
);
1327 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1328 __isl_take isl_qpolynomial
*qp2
)
1330 qp1
= isl_qpolynomial_cow(qp1
);
1335 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1336 return isl_qpolynomial_mul(qp2
, qp1
);
1338 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1339 if (!compatible_divs(qp1
->div
, qp2
->div
))
1340 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1342 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1346 isl_qpolynomial_free(qp2
);
1350 isl_qpolynomial_free(qp1
);
1351 isl_qpolynomial_free(qp2
);
1355 __isl_give isl_qpolynomial
*isl_qpolynomial_zero(__isl_take isl_dim
*dim
)
1357 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1360 __isl_give isl_qpolynomial
*isl_qpolynomial_infty(__isl_take isl_dim
*dim
)
1362 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1365 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty(__isl_take isl_dim
*dim
)
1367 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1370 __isl_give isl_qpolynomial
*isl_qpolynomial_nan(__isl_take isl_dim
*dim
)
1372 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1375 __isl_give isl_qpolynomial
*isl_qpolynomial_cst(__isl_take isl_dim
*dim
,
1378 struct isl_qpolynomial
*qp
;
1379 struct isl_upoly_cst
*cst
;
1381 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1385 cst
= isl_upoly_as_cst(qp
->upoly
);
1386 isl_int_set(cst
->n
, v
);
1391 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1392 isl_int
*n
, isl_int
*d
)
1394 struct isl_upoly_cst
*cst
;
1399 if (!isl_upoly_is_cst(qp
->upoly
))
1402 cst
= isl_upoly_as_cst(qp
->upoly
);
1407 isl_int_set(*n
, cst
->n
);
1409 isl_int_set(*d
, cst
->d
);
1414 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1417 struct isl_upoly_rec
*rec
;
1425 rec
= isl_upoly_as_rec(up
);
1432 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1434 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1440 return isl_upoly_is_affine(rec
->p
[0]);
1443 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1448 if (qp
->div
->n_row
> 0)
1451 return isl_upoly_is_affine(qp
->upoly
);
1454 static void update_coeff(__isl_keep isl_vec
*aff
,
1455 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1460 if (isl_int_is_zero(cst
->n
))
1465 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1466 isl_int_divexact(f
, cst
->d
, gcd
);
1467 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1468 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1469 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1474 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1475 __isl_keep isl_vec
*aff
)
1477 struct isl_upoly_cst
*cst
;
1478 struct isl_upoly_rec
*rec
;
1484 struct isl_upoly_cst
*cst
;
1486 cst
= isl_upoly_as_cst(up
);
1489 update_coeff(aff
, cst
, 0);
1493 rec
= isl_upoly_as_rec(up
);
1496 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1498 cst
= isl_upoly_as_cst(rec
->p
[1]);
1501 update_coeff(aff
, cst
, 1 + up
->var
);
1503 return isl_upoly_update_affine(rec
->p
[0], aff
);
1506 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1507 __isl_keep isl_qpolynomial
*qp
)
1515 isl_assert(qp
->div
->ctx
, qp
->div
->n_row
== 0, return NULL
);
1516 d
= isl_dim_total(qp
->dim
);
1517 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
1521 isl_seq_clr(aff
->el
+ 1, 1 + d
);
1522 isl_int_set_si(aff
->el
[0], 1);
1524 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1533 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1534 __isl_keep isl_qpolynomial
*qp2
)
1539 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1542 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1545 struct isl_upoly_rec
*rec
;
1547 if (isl_upoly_is_cst(up
)) {
1548 struct isl_upoly_cst
*cst
;
1549 cst
= isl_upoly_as_cst(up
);
1552 isl_int_lcm(*d
, *d
, cst
->d
);
1556 rec
= isl_upoly_as_rec(up
);
1560 for (i
= 0; i
< rec
->n
; ++i
)
1561 upoly_update_den(rec
->p
[i
], d
);
1564 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1566 isl_int_set_si(*d
, 1);
1569 upoly_update_den(qp
->upoly
, d
);
1572 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_dim
*dim
,
1575 struct isl_ctx
*ctx
;
1582 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_pow(ctx
, pos
, power
));
1585 __isl_give isl_qpolynomial
*isl_qpolynomial_var(__isl_take isl_dim
*dim
,
1586 enum isl_dim_type type
, unsigned pos
)
1591 isl_assert(dim
->ctx
, isl_dim_size(dim
, isl_dim_in
) == 0, goto error
);
1592 isl_assert(dim
->ctx
, pos
< isl_dim_size(dim
, type
), goto error
);
1594 if (type
== isl_dim_set
)
1595 pos
+= isl_dim_size(dim
, isl_dim_param
);
1597 return isl_qpolynomial_pow(dim
, pos
, 1);
1603 __isl_give isl_qpolynomial
*isl_qpolynomial_div_pow(__isl_take isl_div
*div
,
1606 struct isl_qpolynomial
*qp
= NULL
;
1607 struct isl_upoly_rec
*rec
;
1608 struct isl_upoly_cst
*cst
;
1614 isl_assert(div
->ctx
, div
->bmap
->n_div
== 1, goto error
);
1616 pos
= isl_dim_total(div
->bmap
->dim
);
1617 rec
= isl_upoly_alloc_rec(div
->ctx
, pos
, 1 + power
);
1618 qp
= isl_qpolynomial_alloc(isl_basic_map_get_dim(div
->bmap
), 1,
1623 isl_seq_cpy(qp
->div
->row
[0], div
->line
[0], qp
->div
->n_col
- 1);
1624 isl_int_set_si(qp
->div
->row
[0][qp
->div
->n_col
- 1], 0);
1626 for (i
= 0; i
< 1 + power
; ++i
) {
1627 rec
->p
[i
] = isl_upoly_zero(div
->ctx
);
1632 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1633 isl_int_set_si(cst
->n
, 1);
1639 isl_qpolynomial_free(qp
);
1644 __isl_give isl_qpolynomial
*isl_qpolynomial_div(__isl_take isl_div
*div
)
1646 return isl_qpolynomial_div_pow(div
, 1);
1649 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst(__isl_take isl_dim
*dim
,
1650 const isl_int n
, const isl_int d
)
1652 struct isl_qpolynomial
*qp
;
1653 struct isl_upoly_cst
*cst
;
1655 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1659 cst
= isl_upoly_as_cst(qp
->upoly
);
1660 isl_int_set(cst
->n
, n
);
1661 isl_int_set(cst
->d
, d
);
1666 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
1668 struct isl_upoly_rec
*rec
;
1674 if (isl_upoly_is_cst(up
))
1678 active
[up
->var
] = 1;
1680 rec
= isl_upoly_as_rec(up
);
1681 for (i
= 0; i
< rec
->n
; ++i
)
1682 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
1688 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
1691 int d
= isl_dim_total(qp
->dim
);
1696 for (i
= 0; i
< d
; ++i
)
1697 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
1698 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
1704 return up_set_active(qp
->upoly
, active
, d
);
1707 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
1708 enum isl_dim_type type
, unsigned first
, unsigned n
)
1719 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1721 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1722 type
== isl_dim_set
, return -1);
1724 active
= isl_calloc_array(set
->ctx
, int, isl_dim_total(qp
->dim
));
1725 if (set_active(qp
, active
) < 0)
1728 if (type
== isl_dim_set
)
1729 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1730 for (i
= 0; i
< n
; ++i
)
1731 if (active
[first
+ i
]) {
1744 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
1745 unsigned first
, unsigned n
)
1748 struct isl_upoly_rec
*rec
;
1752 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
1754 if (up
->var
< first
+ n
) {
1755 up
= replace_by_constant_term(up
);
1756 return isl_upoly_drop(up
, first
, n
);
1758 up
= isl_upoly_cow(up
);
1762 rec
= isl_upoly_as_rec(up
);
1766 for (i
= 0; i
< rec
->n
; ++i
) {
1767 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
1778 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
1779 __isl_take isl_qpolynomial
*qp
,
1780 enum isl_dim_type type
, unsigned first
, unsigned n
)
1784 if (n
== 0 && !isl_dim_get_tuple_name(qp
->dim
, type
))
1787 qp
= isl_qpolynomial_cow(qp
);
1791 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1793 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1794 type
== isl_dim_set
, goto error
);
1796 qp
->dim
= isl_dim_drop(qp
->dim
, type
, first
, n
);
1800 if (type
== isl_dim_set
)
1801 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1803 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
1807 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
1813 isl_qpolynomial_free(qp
);
1817 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
1818 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
1821 struct isl_upoly_rec
*rec
;
1822 struct isl_upoly
*base
, *res
;
1827 if (isl_upoly_is_cst(up
))
1830 if (up
->var
< first
)
1833 rec
= isl_upoly_as_rec(up
);
1837 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1839 if (up
->var
>= first
+ n
)
1840 base
= isl_upoly_pow(up
->ctx
, up
->var
, 1);
1842 base
= isl_upoly_copy(subs
[up
->var
- first
]);
1844 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
1845 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1846 struct isl_upoly
*t
;
1847 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
1848 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1849 res
= isl_upoly_sum(res
, t
);
1852 isl_upoly_free(base
);
1861 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
1862 isl_int denom
, unsigned len
)
1865 struct isl_upoly
*up
;
1867 isl_assert(ctx
, len
>= 1, return NULL
);
1869 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
1870 for (i
= 0; i
< len
- 1; ++i
) {
1871 struct isl_upoly
*t
;
1872 struct isl_upoly
*c
;
1874 if (isl_int_is_zero(f
[1 + i
]))
1877 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
1878 t
= isl_upoly_pow(ctx
, i
, 1);
1879 t
= isl_upoly_mul(c
, t
);
1880 up
= isl_upoly_sum(up
, t
);
1886 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
1887 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
1892 struct isl_upoly
*up
;
1896 if (eq
->n_eq
== 0) {
1897 isl_basic_set_free(eq
);
1901 qp
= isl_qpolynomial_cow(qp
);
1904 qp
->div
= isl_mat_cow(qp
->div
);
1908 total
= 1 + isl_dim_total(eq
->dim
);
1909 isl_int_init(denom
);
1910 for (i
= 0; i
< eq
->n_eq
; ++i
) {
1911 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
);
1912 if (j
< 0 || j
== 0)
1915 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
1916 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
1918 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
1919 &qp
->div
->row
[k
][0]);
1920 isl_seq_normalize(qp
->div
->ctx
,
1921 qp
->div
->row
[k
], 1 + total
);
1924 if (isl_int_is_pos(eq
->eq
[i
][j
]))
1925 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
1926 isl_int_abs(denom
, eq
->eq
[i
][j
]);
1927 isl_int_set_si(eq
->eq
[i
][j
], 0);
1929 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
1930 eq
->eq
[i
], denom
, total
);
1931 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
1934 isl_int_clear(denom
);
1939 isl_basic_set_free(eq
);
1945 isl_basic_set_free(eq
);
1946 isl_qpolynomial_free(qp
);
1951 #define PW isl_pw_qpolynomial
1953 #define EL isl_qpolynomial
1955 #define IS_ZERO is_zero
1959 #include <isl_pw_templ.c>
1962 #define UNION isl_union_pw_qpolynomial
1964 #define PART isl_pw_qpolynomial
1966 #define PARTS pw_qpolynomial
1968 #include <isl_union_templ.c>
1970 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
1978 if (!isl_set_fast_is_universe(pwqp
->p
[0].set
))
1981 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
1984 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
1985 __isl_take isl_pw_qpolynomial
*pwqp1
,
1986 __isl_take isl_pw_qpolynomial
*pwqp2
)
1989 struct isl_pw_qpolynomial
*res
;
1992 if (!pwqp1
|| !pwqp2
)
1995 isl_assert(pwqp1
->dim
->ctx
, isl_dim_equal(pwqp1
->dim
, pwqp2
->dim
),
1998 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
1999 isl_pw_qpolynomial_free(pwqp2
);
2003 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2004 isl_pw_qpolynomial_free(pwqp1
);
2008 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2009 isl_pw_qpolynomial_free(pwqp1
);
2013 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
2014 isl_pw_qpolynomial_free(pwqp2
);
2018 n
= pwqp1
->n
* pwqp2
->n
;
2019 res
= isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1
->dim
), n
);
2021 for (i
= 0; i
< pwqp1
->n
; ++i
) {
2022 for (j
= 0; j
< pwqp2
->n
; ++j
) {
2023 struct isl_set
*common
;
2024 struct isl_qpolynomial
*prod
;
2025 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
2026 isl_set_copy(pwqp2
->p
[j
].set
));
2027 if (isl_set_fast_is_empty(common
)) {
2028 isl_set_free(common
);
2032 prod
= isl_qpolynomial_mul(
2033 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
2034 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
2036 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
2040 isl_pw_qpolynomial_free(pwqp1
);
2041 isl_pw_qpolynomial_free(pwqp2
);
2045 isl_pw_qpolynomial_free(pwqp1
);
2046 isl_pw_qpolynomial_free(pwqp2
);
2050 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_neg(
2051 __isl_take isl_pw_qpolynomial
*pwqp
)
2054 struct isl_pw_qpolynomial
*res
;
2060 if (isl_pw_qpolynomial_is_zero(pwqp
))
2063 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
2065 for (i
= 0; i
< pwqp
->n
; ++i
) {
2066 pwqp
->p
[i
].qp
= isl_qpolynomial_neg(pwqp
->p
[i
].qp
);
2073 isl_pw_qpolynomial_free(pwqp
);
2077 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sub(
2078 __isl_take isl_pw_qpolynomial
*pwqp1
,
2079 __isl_take isl_pw_qpolynomial
*pwqp2
)
2081 return isl_pw_qpolynomial_add(pwqp1
, isl_pw_qpolynomial_neg(pwqp2
));
2084 __isl_give
struct isl_upoly
*isl_upoly_eval(
2085 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
2088 struct isl_upoly_rec
*rec
;
2089 struct isl_upoly
*res
;
2090 struct isl_upoly
*base
;
2092 if (isl_upoly_is_cst(up
)) {
2097 rec
= isl_upoly_as_rec(up
);
2101 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2103 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
2105 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
2108 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2109 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2110 res
= isl_upoly_sum(res
,
2111 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
2112 isl_vec_copy(vec
)));
2115 isl_upoly_free(base
);
2125 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
2126 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
2129 struct isl_upoly
*up
;
2134 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, qp
->dim
), goto error
);
2136 if (qp
->div
->n_row
== 0)
2137 ext
= isl_vec_copy(pnt
->vec
);
2140 unsigned dim
= isl_dim_total(qp
->dim
);
2141 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
2145 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
2146 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2147 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
2148 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
2149 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
2150 qp
->div
->row
[i
][0]);
2154 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
2158 dim
= isl_dim_copy(qp
->dim
);
2159 isl_qpolynomial_free(qp
);
2160 isl_point_free(pnt
);
2162 return isl_qpolynomial_alloc(dim
, 0, up
);
2164 isl_qpolynomial_free(qp
);
2165 isl_point_free(pnt
);
2169 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
2170 __isl_keep
struct isl_upoly_cst
*cst2
)
2175 isl_int_mul(t
, cst1
->n
, cst2
->d
);
2176 isl_int_submul(t
, cst2
->n
, cst1
->d
);
2177 cmp
= isl_int_sgn(t
);
2182 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial
*qp1
,
2183 __isl_keep isl_qpolynomial
*qp2
)
2185 struct isl_upoly_cst
*cst1
, *cst2
;
2189 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), return -1);
2190 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), return -1);
2191 if (isl_qpolynomial_is_nan(qp1
))
2193 if (isl_qpolynomial_is_nan(qp2
))
2195 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2196 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2198 return isl_upoly_cmp(cst1
, cst2
) <= 0;
2201 __isl_give isl_qpolynomial
*isl_qpolynomial_min_cst(
2202 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2204 struct isl_upoly_cst
*cst1
, *cst2
;
2209 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2210 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2211 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2212 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2213 cmp
= isl_upoly_cmp(cst1
, cst2
);
2216 isl_qpolynomial_free(qp2
);
2218 isl_qpolynomial_free(qp1
);
2223 isl_qpolynomial_free(qp1
);
2224 isl_qpolynomial_free(qp2
);
2228 __isl_give isl_qpolynomial
*isl_qpolynomial_max_cst(
2229 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2231 struct isl_upoly_cst
*cst1
, *cst2
;
2236 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2237 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2238 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2239 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2240 cmp
= isl_upoly_cmp(cst1
, cst2
);
2243 isl_qpolynomial_free(qp2
);
2245 isl_qpolynomial_free(qp1
);
2250 isl_qpolynomial_free(qp1
);
2251 isl_qpolynomial_free(qp2
);
2255 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
2256 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
2257 unsigned first
, unsigned n
)
2266 qp
= isl_qpolynomial_cow(qp
);
2270 isl_assert(qp
->div
->ctx
, first
<= isl_dim_size(qp
->dim
, type
),
2273 g_pos
= pos(qp
->dim
, type
) + first
;
2275 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + g_pos
, n
);
2279 total
= qp
->div
->n_col
- 2;
2280 if (total
> g_pos
) {
2282 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
2285 for (i
= 0; i
< total
- g_pos
; ++i
)
2287 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
2293 qp
->dim
= isl_dim_insert(qp
->dim
, type
, first
, n
);
2299 isl_qpolynomial_free(qp
);
2303 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
2304 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
2308 pos
= isl_qpolynomial_dim(qp
, type
);
2310 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
2313 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_insert_dims(
2314 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_dim_type type
,
2315 unsigned first
, unsigned n
)
2322 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
2326 pwqp
->dim
= isl_dim_insert(pwqp
->dim
, type
, first
, n
);
2330 for (i
= 0; i
< pwqp
->n
; ++i
) {
2331 pwqp
->p
[i
].set
= isl_set_insert(pwqp
->p
[i
].set
, type
, first
, n
);
2332 if (!pwqp
->p
[i
].set
)
2334 pwqp
->p
[i
].qp
= isl_qpolynomial_insert_dims(pwqp
->p
[i
].qp
,
2342 isl_pw_qpolynomial_free(pwqp
);
2346 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
2347 __isl_take isl_pw_qpolynomial
*pwqp
,
2348 enum isl_dim_type type
, unsigned n
)
2352 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
2354 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
2357 static int *reordering_move(isl_ctx
*ctx
,
2358 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
2363 reordering
= isl_alloc_array(ctx
, int, len
);
2368 for (i
= 0; i
< dst
; ++i
)
2370 for (i
= 0; i
< n
; ++i
)
2371 reordering
[src
+ i
] = dst
+ i
;
2372 for (i
= 0; i
< src
- dst
; ++i
)
2373 reordering
[dst
+ i
] = dst
+ n
+ i
;
2374 for (i
= 0; i
< len
- src
- n
; ++i
)
2375 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
2377 for (i
= 0; i
< src
; ++i
)
2379 for (i
= 0; i
< n
; ++i
)
2380 reordering
[src
+ i
] = dst
+ i
;
2381 for (i
= 0; i
< dst
- src
; ++i
)
2382 reordering
[src
+ n
+ i
] = src
+ i
;
2383 for (i
= 0; i
< len
- dst
- n
; ++i
)
2384 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
2390 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
2391 __isl_take isl_qpolynomial
*qp
,
2392 enum isl_dim_type dst_type
, unsigned dst_pos
,
2393 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2399 qp
= isl_qpolynomial_cow(qp
);
2403 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_dim_size(qp
->dim
, src_type
),
2406 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
2407 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
2408 if (dst_type
> src_type
)
2411 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
2418 reordering
= reordering_move(qp
->dim
->ctx
,
2419 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
2423 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2428 qp
->dim
= isl_dim_move(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2434 isl_qpolynomial_free(qp
);
2438 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_dim
*dim
,
2439 isl_int
*f
, isl_int denom
)
2441 struct isl_upoly
*up
;
2446 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
, 1 + isl_dim_total(dim
));
2448 return isl_qpolynomial_alloc(dim
, 0, up
);
2451 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
2452 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
2456 struct isl_upoly
*up
;
2457 isl_qpolynomial
*qp
;
2463 isl_int_init(denom
);
2465 isl_constraint_get_coefficient(c
, type
, pos
, &denom
);
2466 isl_constraint_set_coefficient(c
, type
, pos
, c
->ctx
->zero
);
2467 sgn
= isl_int_sgn(denom
);
2468 isl_int_abs(denom
, denom
);
2469 up
= isl_upoly_from_affine(c
->ctx
, c
->line
[0], denom
,
2470 1 + isl_constraint_dim(c
, isl_dim_all
));
2472 isl_int_neg(denom
, denom
);
2473 isl_constraint_set_coefficient(c
, type
, pos
, denom
);
2475 dim
= isl_dim_copy(c
->bmap
->dim
);
2477 isl_int_clear(denom
);
2478 isl_constraint_free(c
);
2480 qp
= isl_qpolynomial_alloc(dim
, 0, up
);
2482 qp
= isl_qpolynomial_neg(qp
);
2486 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2487 * in "qp" by subs[i].
2489 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
2490 __isl_take isl_qpolynomial
*qp
,
2491 enum isl_dim_type type
, unsigned first
, unsigned n
,
2492 __isl_keep isl_qpolynomial
**subs
)
2495 struct isl_upoly
**ups
;
2500 qp
= isl_qpolynomial_cow(qp
);
2503 for (i
= 0; i
< n
; ++i
)
2507 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
2510 for (i
= 0; i
< n
; ++i
)
2511 isl_assert(qp
->dim
->ctx
, isl_dim_equal(qp
->dim
, subs
[i
]->dim
),
2514 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
2515 for (i
= 0; i
< n
; ++i
)
2516 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
2518 first
+= pos(qp
->dim
, type
);
2520 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
2523 for (i
= 0; i
< n
; ++i
)
2524 ups
[i
] = subs
[i
]->upoly
;
2526 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
2535 isl_qpolynomial_free(qp
);
2539 __isl_give isl_basic_set
*add_div_constraints(__isl_take isl_basic_set
*bset
,
2540 __isl_take isl_mat
*div
)
2548 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2551 total
= isl_basic_set_total_dim(bset
);
2552 for (i
= 0; i
< div
->n_row
; ++i
)
2553 if (isl_basic_set_add_div_constraints_var(bset
,
2554 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2561 isl_basic_set_free(bset
);
2565 /* Extend "bset" with extra set dimensions for each integer division
2566 * in "qp" and then call "fn" with the extended bset and the polynomial
2567 * that results from replacing each of the integer divisions by the
2568 * corresponding extra set dimension.
2570 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
2571 __isl_keep isl_basic_set
*bset
,
2572 int (*fn
)(__isl_take isl_basic_set
*bset
,
2573 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
2577 isl_qpolynomial
*poly
;
2581 if (qp
->div
->n_row
== 0)
2582 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
2585 div
= isl_mat_copy(qp
->div
);
2586 dim
= isl_dim_copy(qp
->dim
);
2587 dim
= isl_dim_add(dim
, isl_dim_set
, qp
->div
->n_row
);
2588 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
2589 bset
= isl_basic_set_copy(bset
);
2590 bset
= isl_basic_set_add(bset
, isl_dim_set
, qp
->div
->n_row
);
2591 bset
= add_div_constraints(bset
, div
);
2593 return fn(bset
, poly
, user
);
2598 /* Return total degree in variables first (inclusive) up to last (exclusive).
2600 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
2604 struct isl_upoly_rec
*rec
;
2608 if (isl_upoly_is_zero(up
))
2610 if (isl_upoly_is_cst(up
) || up
->var
< first
)
2613 rec
= isl_upoly_as_rec(up
);
2617 for (i
= 0; i
< rec
->n
; ++i
) {
2620 if (isl_upoly_is_zero(rec
->p
[i
]))
2622 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
2632 /* Return total degree in set variables.
2634 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
2642 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
2643 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
2644 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
2647 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
2648 unsigned pos
, int deg
)
2651 struct isl_upoly_rec
*rec
;
2656 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
2658 return isl_upoly_copy(up
);
2660 return isl_upoly_zero(up
->ctx
);
2663 rec
= isl_upoly_as_rec(up
);
2667 if (up
->var
== pos
) {
2669 return isl_upoly_copy(rec
->p
[deg
]);
2671 return isl_upoly_zero(up
->ctx
);
2674 up
= isl_upoly_copy(up
);
2675 up
= isl_upoly_cow(up
);
2676 rec
= isl_upoly_as_rec(up
);
2680 for (i
= 0; i
< rec
->n
; ++i
) {
2681 struct isl_upoly
*t
;
2682 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
2685 isl_upoly_free(rec
->p
[i
]);
2695 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2697 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
2698 __isl_keep isl_qpolynomial
*qp
,
2699 enum isl_dim_type type
, unsigned t_pos
, int deg
)
2702 struct isl_upoly
*up
;
2708 isl_assert(qp
->div
->ctx
, t_pos
< isl_dim_size(qp
->dim
, type
),
2711 g_pos
= pos(qp
->dim
, type
) + t_pos
;
2712 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
2714 c
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
, up
);
2717 isl_mat_free(c
->div
);
2718 c
->div
= isl_mat_copy(qp
->div
);
2723 isl_qpolynomial_free(c
);
2727 /* Homogenize the polynomial in the variables first (inclusive) up to
2728 * last (exclusive) by inserting powers of variable first.
2729 * Variable first is assumed not to appear in the input.
2731 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
2732 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
2733 int first
, int last
)
2736 struct isl_upoly_rec
*rec
;
2740 if (isl_upoly_is_zero(up
))
2744 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
2745 struct isl_upoly
*hom
;
2747 hom
= isl_upoly_pow(up
->ctx
, first
, target
- deg
);
2750 rec
= isl_upoly_as_rec(hom
);
2751 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
2756 up
= isl_upoly_cow(up
);
2757 rec
= isl_upoly_as_rec(up
);
2761 for (i
= 0; i
< rec
->n
; ++i
) {
2762 if (isl_upoly_is_zero(rec
->p
[i
]))
2764 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
2765 up
->var
< last
? deg
+ i
: i
, target
,
2777 /* Homogenize the polynomial in the set variables by introducing
2778 * powers of an extra set variable at position 0.
2780 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
2781 __isl_take isl_qpolynomial
*poly
)
2785 int deg
= isl_qpolynomial_degree(poly
);
2790 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_set
, 0, 1);
2791 poly
= isl_qpolynomial_cow(poly
);
2795 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
2796 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
2797 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
2804 isl_qpolynomial_free(poly
);
2808 __isl_give isl_term
*isl_term_alloc(__isl_take isl_dim
*dim
,
2809 __isl_take isl_mat
*div
)
2817 n
= isl_dim_total(dim
) + div
->n_row
;
2819 term
= isl_calloc(dim
->ctx
, struct isl_term
,
2820 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
2827 isl_int_init(term
->n
);
2828 isl_int_init(term
->d
);
2837 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
2846 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
2855 total
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
2857 dup
= isl_term_alloc(isl_dim_copy(term
->dim
), isl_mat_copy(term
->div
));
2861 isl_int_set(dup
->n
, term
->n
);
2862 isl_int_set(dup
->d
, term
->d
);
2864 for (i
= 0; i
< total
; ++i
)
2865 dup
->pow
[i
] = term
->pow
[i
];
2870 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
2878 return isl_term_dup(term
);
2881 void isl_term_free(__isl_take isl_term
*term
)
2886 if (--term
->ref
> 0)
2889 isl_dim_free(term
->dim
);
2890 isl_mat_free(term
->div
);
2891 isl_int_clear(term
->n
);
2892 isl_int_clear(term
->d
);
2896 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
2904 case isl_dim_out
: return isl_dim_size(term
->dim
, type
);
2905 case isl_dim_div
: return term
->div
->n_row
;
2906 case isl_dim_all
: return isl_dim_total(term
->dim
) + term
->div
->n_row
;
2911 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
2913 return term
? term
->dim
->ctx
: NULL
;
2916 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
2920 isl_int_set(*n
, term
->n
);
2923 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
2927 isl_int_set(*d
, term
->d
);
2930 int isl_term_get_exp(__isl_keep isl_term
*term
,
2931 enum isl_dim_type type
, unsigned pos
)
2936 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
2938 if (type
>= isl_dim_set
)
2939 pos
+= isl_dim_size(term
->dim
, isl_dim_param
);
2940 if (type
>= isl_dim_div
)
2941 pos
+= isl_dim_size(term
->dim
, isl_dim_set
);
2943 return term
->pow
[pos
];
2946 __isl_give isl_div
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
2948 isl_basic_map
*bmap
;
2955 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
2958 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
2959 /* No nested divs for now */
2960 isl_assert(term
->dim
->ctx
,
2961 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
2962 term
->div
->n_row
) == -1,
2965 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(term
->dim
), 1, 0, 0);
2966 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
2969 isl_seq_cpy(bmap
->div
[k
], term
->div
->row
[pos
], 2 + total
);
2971 return isl_basic_map_div(bmap
, k
);
2973 isl_basic_map_free(bmap
);
2977 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
2978 int (*fn
)(__isl_take isl_term
*term
, void *user
),
2979 __isl_take isl_term
*term
, void *user
)
2982 struct isl_upoly_rec
*rec
;
2987 if (isl_upoly_is_zero(up
))
2990 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
2991 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
2992 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
2994 if (isl_upoly_is_cst(up
)) {
2995 struct isl_upoly_cst
*cst
;
2996 cst
= isl_upoly_as_cst(up
);
2999 term
= isl_term_cow(term
);
3002 isl_int_set(term
->n
, cst
->n
);
3003 isl_int_set(term
->d
, cst
->d
);
3004 if (fn(isl_term_copy(term
), user
) < 0)
3009 rec
= isl_upoly_as_rec(up
);
3013 for (i
= 0; i
< rec
->n
; ++i
) {
3014 term
= isl_term_cow(term
);
3017 term
->pow
[up
->var
] = i
;
3018 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3022 term
->pow
[up
->var
] = 0;
3026 isl_term_free(term
);
3030 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3031 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3038 term
= isl_term_alloc(isl_dim_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3042 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3044 isl_term_free(term
);
3046 return term
? 0 : -1;
3049 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3051 struct isl_upoly
*up
;
3052 isl_qpolynomial
*qp
;
3058 n
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
3060 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3061 for (i
= 0; i
< n
; ++i
) {
3064 up
= isl_upoly_mul(up
,
3065 isl_upoly_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3068 qp
= isl_qpolynomial_alloc(isl_dim_copy(term
->dim
), term
->div
->n_row
, up
);
3071 isl_mat_free(qp
->div
);
3072 qp
->div
= isl_mat_copy(term
->div
);
3076 isl_term_free(term
);
3079 isl_qpolynomial_free(qp
);
3080 isl_term_free(term
);
3084 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3085 __isl_take isl_dim
*dim
)
3094 if (isl_dim_equal(qp
->dim
, dim
)) {
3099 qp
= isl_qpolynomial_cow(qp
);
3103 extra
= isl_dim_size(dim
, isl_dim_set
) -
3104 isl_dim_size(qp
->dim
, isl_dim_set
);
3105 total
= isl_dim_total(qp
->dim
);
3106 if (qp
->div
->n_row
) {
3109 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
3112 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3114 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
3119 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
3122 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3123 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
3125 isl_dim_free(qp
->dim
);
3131 isl_qpolynomial_free(qp
);
3135 /* For each parameter or variable that does not appear in qp,
3136 * first eliminate the variable from all constraints and then set it to zero.
3138 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3139 __isl_keep isl_qpolynomial
*qp
)
3150 d
= isl_dim_total(set
->dim
);
3151 active
= isl_calloc_array(set
->ctx
, int, d
);
3152 if (set_active(qp
, active
) < 0)
3155 for (i
= 0; i
< d
; ++i
)
3164 nparam
= isl_dim_size(set
->dim
, isl_dim_param
);
3165 nvar
= isl_dim_size(set
->dim
, isl_dim_set
);
3166 for (i
= 0; i
< nparam
; ++i
) {
3169 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
3170 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
3172 for (i
= 0; i
< nvar
; ++i
) {
3173 if (active
[nparam
+ i
])
3175 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
3176 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
3188 struct isl_opt_data
{
3189 isl_qpolynomial
*qp
;
3191 isl_qpolynomial
*opt
;
3195 static int opt_fn(__isl_take isl_point
*pnt
, void *user
)
3197 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
3198 isl_qpolynomial
*val
;
3200 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
3204 } else if (data
->max
) {
3205 data
->opt
= isl_qpolynomial_max_cst(data
->opt
, val
);
3207 data
->opt
= isl_qpolynomial_min_cst(data
->opt
, val
);
3213 __isl_give isl_qpolynomial
*isl_qpolynomial_opt_on_domain(
3214 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
3216 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
3221 if (isl_upoly_is_cst(qp
->upoly
)) {
3226 set
= fix_inactive(set
, qp
);
3229 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
3233 data
.opt
= isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp
));
3236 isl_qpolynomial_free(qp
);
3240 isl_qpolynomial_free(qp
);
3241 isl_qpolynomial_free(data
.opt
);
3245 __isl_give isl_qpolynomial
*isl_qpolynomial_morph(__isl_take isl_qpolynomial
*qp
,
3246 __isl_take isl_morph
*morph
)
3250 struct isl_upoly
*up
;
3252 struct isl_upoly
**subs
;
3255 qp
= isl_qpolynomial_cow(qp
);
3260 isl_assert(ctx
, isl_dim_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
3262 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, morph
->inv
->n_row
- 1);
3266 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3267 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
3268 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
3270 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, morph
->inv
->n_row
- 1, subs
);
3272 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3273 isl_upoly_free(subs
[i
]);
3276 mat
= isl_mat_diagonal(isl_mat_identity(ctx
, 1), isl_mat_copy(morph
->inv
));
3277 mat
= isl_mat_diagonal(mat
, isl_mat_identity(ctx
, qp
->div
->n_row
));
3278 qp
->div
= isl_mat_product(qp
->div
, mat
);
3279 isl_dim_free(qp
->dim
);
3280 qp
->dim
= isl_dim_copy(morph
->ran
->dim
);
3282 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
3285 isl_morph_free(morph
);
3289 isl_qpolynomial_free(qp
);
3290 isl_morph_free(morph
);
3294 static int neg_entry(void **entry
, void *user
)
3296 isl_pw_qpolynomial
**pwqp
= (isl_pw_qpolynomial
**)entry
;
3298 *pwqp
= isl_pw_qpolynomial_neg(*pwqp
);
3300 return *pwqp
? 0 : -1;
3303 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_neg(
3304 __isl_take isl_union_pw_qpolynomial
*upwqp
)
3306 upwqp
= isl_union_pw_qpolynomial_cow(upwqp
);
3310 if (isl_hash_table_foreach(upwqp
->dim
->ctx
, &upwqp
->table
,
3311 &neg_entry
, NULL
) < 0)
3316 isl_union_pw_qpolynomial_free(upwqp
);
3320 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_sub(
3321 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3322 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3324 return isl_union_pw_qpolynomial_add(upwqp1
,
3325 isl_union_pw_qpolynomial_neg(upwqp2
));
3328 static int mul_entry(void **entry
, void *user
)
3330 struct isl_union_pw_qpolynomial_match_bin_data
*data
= user
;
3332 struct isl_hash_table_entry
*entry2
;
3333 isl_pw_qpolynomial
*pwpq
= *entry
;
3336 hash
= isl_dim_get_hash(pwpq
->dim
);
3337 entry2
= isl_hash_table_find(data
->upwqp2
->dim
->ctx
,
3338 &data
->upwqp2
->table
,
3339 hash
, &has_dim
, pwpq
->dim
, 0);
3343 pwpq
= isl_pw_qpolynomial_copy(pwpq
);
3344 pwpq
= isl_pw_qpolynomial_mul(pwpq
,
3345 isl_pw_qpolynomial_copy(entry2
->data
));
3347 empty
= isl_pw_qpolynomial_is_zero(pwpq
);
3349 isl_pw_qpolynomial_free(pwpq
);
3353 isl_pw_qpolynomial_free(pwpq
);
3357 data
->res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(data
->res
, pwpq
);
3362 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
3363 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
3364 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
3366 return match_bin_op(upwqp1
, upwqp2
, &mul_entry
);