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_polynomial_private.h>
14 #include <isl_point_private.h>
15 #include <isl_dim_private.h>
16 #include <isl_map_private.h>
18 static unsigned pos(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
21 case isl_dim_param
: return 0;
22 case isl_dim_in
: return dim
->nparam
;
23 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
28 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
36 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
41 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
43 return (struct isl_upoly_cst
*)up
;
46 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
51 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
53 return (struct isl_upoly_rec
*)up
;
56 int isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
57 __isl_keep
struct isl_upoly
*up2
)
60 struct isl_upoly_rec
*rec1
, *rec2
;
66 if (up1
->var
!= up2
->var
)
68 if (isl_upoly_is_cst(up1
)) {
69 struct isl_upoly_cst
*cst1
, *cst2
;
70 cst1
= isl_upoly_as_cst(up1
);
71 cst2
= isl_upoly_as_cst(up2
);
74 return isl_int_eq(cst1
->n
, cst2
->n
) &&
75 isl_int_eq(cst1
->d
, cst2
->d
);
78 rec1
= isl_upoly_as_rec(up1
);
79 rec2
= isl_upoly_as_rec(up2
);
83 if (rec1
->n
!= rec2
->n
)
86 for (i
= 0; i
< rec1
->n
; ++i
) {
87 int eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
95 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
97 struct isl_upoly_cst
*cst
;
101 if (!isl_upoly_is_cst(up
))
104 cst
= isl_upoly_as_cst(up
);
108 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
111 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
113 struct isl_upoly_cst
*cst
;
117 if (!isl_upoly_is_cst(up
))
120 cst
= isl_upoly_as_cst(up
);
124 return isl_int_sgn(cst
->n
);
127 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
129 struct isl_upoly_cst
*cst
;
133 if (!isl_upoly_is_cst(up
))
136 cst
= isl_upoly_as_cst(up
);
140 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
143 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
145 struct isl_upoly_cst
*cst
;
149 if (!isl_upoly_is_cst(up
))
152 cst
= isl_upoly_as_cst(up
);
156 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
159 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
161 struct isl_upoly_cst
*cst
;
165 if (!isl_upoly_is_cst(up
))
168 cst
= isl_upoly_as_cst(up
);
172 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
175 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
177 struct isl_upoly_cst
*cst
;
181 if (!isl_upoly_is_cst(up
))
184 cst
= isl_upoly_as_cst(up
);
188 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
191 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
193 struct isl_upoly_cst
*cst
;
197 if (!isl_upoly_is_cst(up
))
200 cst
= isl_upoly_as_cst(up
);
204 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
207 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
209 struct isl_upoly_cst
*cst
;
211 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
220 isl_int_init(cst
->n
);
221 isl_int_init(cst
->d
);
226 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
228 struct isl_upoly_cst
*cst
;
230 cst
= isl_upoly_cst_alloc(ctx
);
234 isl_int_set_si(cst
->n
, 0);
235 isl_int_set_si(cst
->d
, 1);
240 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
242 struct isl_upoly_cst
*cst
;
244 cst
= isl_upoly_cst_alloc(ctx
);
248 isl_int_set_si(cst
->n
, 1);
249 isl_int_set_si(cst
->d
, 0);
254 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
256 struct isl_upoly_cst
*cst
;
258 cst
= isl_upoly_cst_alloc(ctx
);
262 isl_int_set_si(cst
->n
, -1);
263 isl_int_set_si(cst
->d
, 0);
268 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
270 struct isl_upoly_cst
*cst
;
272 cst
= isl_upoly_cst_alloc(ctx
);
276 isl_int_set_si(cst
->n
, 0);
277 isl_int_set_si(cst
->d
, 0);
282 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
283 isl_int n
, isl_int d
)
285 struct isl_upoly_cst
*cst
;
287 cst
= isl_upoly_cst_alloc(ctx
);
291 isl_int_set(cst
->n
, n
);
292 isl_int_set(cst
->d
, d
);
297 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
300 struct isl_upoly_rec
*rec
;
302 isl_assert(ctx
, var
>= 0, return NULL
);
303 isl_assert(ctx
, size
>= 0, return NULL
);
304 rec
= isl_calloc(dim
->ctx
, struct isl_upoly_rec
,
305 sizeof(struct isl_upoly_rec
) +
306 (size
- 1) * sizeof(struct isl_upoly
*));
321 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
323 return qp
? qp
->dim
->ctx
: NULL
;
326 __isl_give isl_dim
*isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial
*qp
)
328 return qp
? isl_dim_copy(qp
->dim
) : NULL
;
331 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
332 enum isl_dim_type type
)
334 return qp
? isl_dim_size(qp
->dim
, type
) : 0;
337 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
339 return qp
? isl_upoly_is_zero(qp
->upoly
) : -1;
342 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
344 return qp
? isl_upoly_is_one(qp
->upoly
) : -1;
347 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
349 return qp
? isl_upoly_is_nan(qp
->upoly
) : -1;
352 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
354 return qp
? isl_upoly_is_infty(qp
->upoly
) : -1;
357 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
359 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : -1;
362 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
364 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
367 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
369 isl_int_clear(cst
->n
);
370 isl_int_clear(cst
->d
);
373 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
377 for (i
= 0; i
< rec
->n
; ++i
)
378 isl_upoly_free(rec
->p
[i
]);
381 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
390 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
392 struct isl_upoly_cst
*cst
;
393 struct isl_upoly_cst
*dup
;
395 cst
= isl_upoly_as_cst(up
);
399 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
402 isl_int_set(dup
->n
, cst
->n
);
403 isl_int_set(dup
->d
, cst
->d
);
408 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
411 struct isl_upoly_rec
*rec
;
412 struct isl_upoly_rec
*dup
;
414 rec
= isl_upoly_as_rec(up
);
418 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
422 for (i
= 0; i
< rec
->n
; ++i
) {
423 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
431 isl_upoly_free(&dup
->up
);
435 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
437 struct isl_upoly
*dup
;
442 if (isl_upoly_is_cst(up
))
443 return isl_upoly_dup_cst(up
);
445 return isl_upoly_dup_rec(up
);
448 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
456 return isl_upoly_dup(up
);
459 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
468 upoly_free_cst((struct isl_upoly_cst
*)up
);
470 upoly_free_rec((struct isl_upoly_rec
*)up
);
472 isl_ctx_deref(up
->ctx
);
476 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
481 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
482 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
483 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
484 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
489 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
490 __isl_take
struct isl_upoly
*up2
)
492 struct isl_upoly_cst
*cst1
;
493 struct isl_upoly_cst
*cst2
;
495 up1
= isl_upoly_cow(up1
);
499 cst1
= isl_upoly_as_cst(up1
);
500 cst2
= isl_upoly_as_cst(up2
);
502 if (isl_int_eq(cst1
->d
, cst2
->d
))
503 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
505 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
506 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
507 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
510 isl_upoly_cst_reduce(cst1
);
520 static __isl_give
struct isl_upoly
*replace_by_zero(
521 __isl_take
struct isl_upoly
*up
)
529 return isl_upoly_zero(ctx
);
532 static __isl_give
struct isl_upoly
*replace_by_constant_term(
533 __isl_take
struct isl_upoly
*up
)
535 struct isl_upoly_rec
*rec
;
536 struct isl_upoly
*cst
;
541 rec
= isl_upoly_as_rec(up
);
544 cst
= isl_upoly_copy(rec
->p
[0]);
552 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
553 __isl_take
struct isl_upoly
*up2
)
556 struct isl_upoly_rec
*rec1
, *rec2
;
561 if (isl_upoly_is_nan(up1
)) {
566 if (isl_upoly_is_nan(up2
)) {
571 if (isl_upoly_is_zero(up1
)) {
576 if (isl_upoly_is_zero(up2
)) {
581 if (up1
->var
< up2
->var
)
582 return isl_upoly_sum(up2
, up1
);
584 if (up2
->var
< up1
->var
) {
585 struct isl_upoly_rec
*rec
;
586 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
590 up1
= isl_upoly_cow(up1
);
591 rec
= isl_upoly_as_rec(up1
);
594 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
596 up1
= replace_by_constant_term(up1
);
600 if (isl_upoly_is_cst(up1
))
601 return isl_upoly_sum_cst(up1
, up2
);
603 rec1
= isl_upoly_as_rec(up1
);
604 rec2
= isl_upoly_as_rec(up2
);
608 if (rec1
->n
< rec2
->n
)
609 return isl_upoly_sum(up2
, up1
);
611 up1
= isl_upoly_cow(up1
);
612 rec1
= isl_upoly_as_rec(up1
);
616 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
617 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
618 isl_upoly_copy(rec2
->p
[i
]));
621 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
622 isl_upoly_free(rec1
->p
[i
]);
628 up1
= replace_by_zero(up1
);
629 else if (rec1
->n
== 1)
630 up1
= replace_by_constant_term(up1
);
641 __isl_give
struct isl_upoly
*isl_upoly_neg_cst(__isl_take
struct isl_upoly
*up
)
643 struct isl_upoly_cst
*cst
;
645 if (isl_upoly_is_zero(up
))
648 up
= isl_upoly_cow(up
);
652 cst
= isl_upoly_as_cst(up
);
654 isl_int_neg(cst
->n
, cst
->n
);
659 __isl_give
struct isl_upoly
*isl_upoly_neg(__isl_take
struct isl_upoly
*up
)
662 struct isl_upoly_rec
*rec
;
667 if (isl_upoly_is_cst(up
))
668 return isl_upoly_neg_cst(up
);
670 up
= isl_upoly_cow(up
);
671 rec
= isl_upoly_as_rec(up
);
675 for (i
= 0; i
< rec
->n
; ++i
) {
676 rec
->p
[i
] = isl_upoly_neg(rec
->p
[i
]);
687 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
688 __isl_take
struct isl_upoly
*up2
)
690 struct isl_upoly_cst
*cst1
;
691 struct isl_upoly_cst
*cst2
;
693 up1
= isl_upoly_cow(up1
);
697 cst1
= isl_upoly_as_cst(up1
);
698 cst2
= isl_upoly_as_cst(up2
);
700 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
701 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
703 isl_upoly_cst_reduce(cst1
);
713 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
714 __isl_take
struct isl_upoly
*up2
)
716 struct isl_upoly_rec
*rec1
;
717 struct isl_upoly_rec
*rec2
;
718 struct isl_upoly_rec
*res
;
722 rec1
= isl_upoly_as_rec(up1
);
723 rec2
= isl_upoly_as_rec(up2
);
726 size
= rec1
->n
+ rec2
->n
- 1;
727 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
731 for (i
= 0; i
< rec1
->n
; ++i
) {
732 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
733 isl_upoly_copy(rec1
->p
[i
]));
738 for (; i
< size
; ++i
) {
739 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
744 for (i
= 0; i
< rec1
->n
; ++i
) {
745 for (j
= 1; j
< rec2
->n
; ++j
) {
746 struct isl_upoly
*up
;
747 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
748 isl_upoly_copy(rec1
->p
[i
]));
749 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
762 isl_upoly_free(&res
->up
);
766 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
767 __isl_take
struct isl_upoly
*up2
)
772 if (isl_upoly_is_nan(up1
)) {
777 if (isl_upoly_is_nan(up2
)) {
782 if (isl_upoly_is_zero(up1
)) {
787 if (isl_upoly_is_zero(up2
)) {
792 if (isl_upoly_is_one(up1
)) {
797 if (isl_upoly_is_one(up2
)) {
802 if (up1
->var
< up2
->var
)
803 return isl_upoly_mul(up2
, up1
);
805 if (up2
->var
< up1
->var
) {
807 struct isl_upoly_rec
*rec
;
808 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
809 isl_ctx
*ctx
= up1
->ctx
;
812 return isl_upoly_nan(ctx
);
814 up1
= isl_upoly_cow(up1
);
815 rec
= isl_upoly_as_rec(up1
);
819 for (i
= 0; i
< rec
->n
; ++i
) {
820 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
821 isl_upoly_copy(up2
));
829 if (isl_upoly_is_cst(up1
))
830 return isl_upoly_mul_cst(up1
, up2
);
832 return isl_upoly_mul_rec(up1
, up2
);
839 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_dim
*dim
,
840 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
842 struct isl_qpolynomial
*qp
= NULL
;
848 total
= isl_dim_total(dim
);
850 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
855 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
866 isl_qpolynomial_free(qp
);
870 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
879 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
881 struct isl_qpolynomial
*dup
;
886 dup
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
,
887 isl_upoly_copy(qp
->upoly
));
890 isl_mat_free(dup
->div
);
891 dup
->div
= isl_mat_copy(qp
->div
);
897 isl_qpolynomial_free(dup
);
901 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
909 return isl_qpolynomial_dup(qp
);
912 void isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
920 isl_dim_free(qp
->dim
);
921 isl_mat_free(qp
->div
);
922 isl_upoly_free(qp
->upoly
);
927 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
932 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
933 div1
->n_col
>= div2
->n_col
, return -1);
935 if (div1
->n_row
== div2
->n_row
)
936 return isl_mat_is_equal(div1
, div2
);
940 div1
->n_row
= div2
->n_row
;
941 div1
->n_col
= div2
->n_col
;
943 equal
= isl_mat_is_equal(div1
, div2
);
951 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
952 __isl_keep isl_mat
*src
, int s
, int *exp
)
955 unsigned c
= src
->n_col
- src
->n_row
;
957 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
958 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
960 for (i
= 0; i
< s
; ++i
)
961 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
964 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
968 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
969 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
974 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
977 struct isl_div_sort_info
{
982 static int div_sort_cmp(const void *p1
, const void *p2
)
984 const struct isl_div_sort_info
*i1
, *i2
;
985 i1
= (const struct isl_div_sort_info
*) p1
;
986 i2
= (const struct isl_div_sort_info
*) p2
;
988 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
991 static __isl_give isl_mat
*sort_divs(__isl_take isl_mat
*div
)
994 struct isl_div_sort_info
*array
= NULL
;
1002 array
= isl_alloc_array(div
->ctx
, struct isl_div_sort_info
, div
->n_row
);
1003 pos
= isl_alloc_array(div
->ctx
, int, div
->n_row
);
1007 for (i
= 0; i
< div
->n_row
; ++i
) {
1013 qsort(array
, div
->n_row
, sizeof(struct isl_div_sort_info
),
1016 for (i
= 0; i
< div
->n_row
; ++i
) {
1018 if (pos
[array
[i
].row
] == i
)
1020 div
= isl_mat_cow(div
);
1021 div
= isl_mat_swap_rows(div
, i
, pos
[array
[i
].row
]);
1022 t
= pos
[array
[i
].row
];
1023 pos
[array
[i
].row
] = pos
[i
];
1037 static __isl_give isl_mat
*merge_divs(__isl_keep isl_mat
*div1
,
1038 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
1041 isl_mat
*div
= NULL
;
1042 unsigned d
= div1
->n_col
- div1
->n_row
;
1044 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
1045 d
+ div1
->n_row
+ div2
->n_row
);
1049 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
1052 expand_row(div
, k
, div1
, i
, exp1
);
1053 expand_row(div
, k
+ 1, div2
, j
, exp2
);
1055 cmp
= cmp_row(div
, k
, k
+ 1);
1059 } else if (cmp
< 0) {
1063 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
1066 for (; i
< div1
->n_row
; ++i
, ++k
) {
1067 expand_row(div
, k
, div1
, i
, exp1
);
1070 for (; j
< div2
->n_row
; ++j
, ++k
) {
1071 expand_row(div
, k
, div2
, j
, exp2
);
1081 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1082 int *exp
, int first
)
1085 struct isl_upoly_rec
*rec
;
1087 if (isl_upoly_is_cst(up
))
1090 if (up
->var
< first
)
1093 if (exp
[up
->var
- first
] == up
->var
- first
)
1096 up
= isl_upoly_cow(up
);
1100 up
->var
= exp
[up
->var
- first
] + first
;
1102 rec
= isl_upoly_as_rec(up
);
1106 for (i
= 0; i
< rec
->n
; ++i
) {
1107 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1118 static __isl_give isl_qpolynomial
*with_merged_divs(
1119 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1120 __isl_take isl_qpolynomial
*qp2
),
1121 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1125 isl_mat
*div
= NULL
;
1127 qp1
= isl_qpolynomial_cow(qp1
);
1128 qp2
= isl_qpolynomial_cow(qp2
);
1133 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1134 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1136 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
1137 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
1141 div
= merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1145 isl_mat_free(qp1
->div
);
1146 qp1
->div
= isl_mat_copy(div
);
1147 isl_mat_free(qp2
->div
);
1148 qp2
->div
= isl_mat_copy(div
);
1150 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1151 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1153 if (!qp1
->upoly
|| !qp2
->upoly
)
1160 return fn(qp1
, qp2
);
1165 isl_qpolynomial_free(qp1
);
1166 isl_qpolynomial_free(qp2
);
1170 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1171 __isl_take isl_qpolynomial
*qp2
)
1173 qp1
= isl_qpolynomial_cow(qp1
);
1178 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1179 return isl_qpolynomial_add(qp2
, qp1
);
1181 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1182 if (!compatible_divs(qp1
->div
, qp2
->div
))
1183 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1185 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1189 isl_qpolynomial_free(qp2
);
1193 isl_qpolynomial_free(qp1
);
1194 isl_qpolynomial_free(qp2
);
1198 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1199 __isl_take isl_qpolynomial
*qp2
)
1201 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1204 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1206 qp
= isl_qpolynomial_cow(qp
);
1211 qp
->upoly
= isl_upoly_neg(qp
->upoly
);
1217 isl_qpolynomial_free(qp
);
1221 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1222 __isl_take isl_qpolynomial
*qp2
)
1224 qp1
= isl_qpolynomial_cow(qp1
);
1229 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1230 return isl_qpolynomial_mul(qp2
, qp1
);
1232 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1233 if (!compatible_divs(qp1
->div
, qp2
->div
))
1234 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1236 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1240 isl_qpolynomial_free(qp2
);
1244 isl_qpolynomial_free(qp1
);
1245 isl_qpolynomial_free(qp2
);
1249 __isl_give isl_qpolynomial
*isl_qpolynomial_zero(__isl_take isl_dim
*dim
)
1251 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1254 __isl_give isl_qpolynomial
*isl_qpolynomial_infty(__isl_take isl_dim
*dim
)
1256 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1259 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty(__isl_take isl_dim
*dim
)
1261 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1264 __isl_give isl_qpolynomial
*isl_qpolynomial_nan(__isl_take isl_dim
*dim
)
1266 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1269 __isl_give isl_qpolynomial
*isl_qpolynomial_cst(__isl_take isl_dim
*dim
,
1272 struct isl_qpolynomial
*qp
;
1273 struct isl_upoly_cst
*cst
;
1275 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1279 cst
= isl_upoly_as_cst(qp
->upoly
);
1280 isl_int_set(cst
->n
, v
);
1285 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1286 isl_int
*n
, isl_int
*d
)
1288 struct isl_upoly_cst
*cst
;
1293 if (!isl_upoly_is_cst(qp
->upoly
))
1296 cst
= isl_upoly_as_cst(qp
->upoly
);
1301 isl_int_set(*n
, cst
->n
);
1303 isl_int_set(*d
, cst
->d
);
1308 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1311 struct isl_upoly_rec
*rec
;
1319 rec
= isl_upoly_as_rec(up
);
1326 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1328 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1334 return isl_upoly_is_affine(rec
->p
[0]);
1337 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1342 if (qp
->div
->n_row
> 0)
1345 return isl_upoly_is_affine(qp
->upoly
);
1348 static void update_coeff(__isl_keep isl_vec
*aff
,
1349 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1354 if (isl_int_is_zero(cst
->n
))
1359 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1360 isl_int_divexact(f
, cst
->d
, gcd
);
1361 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1362 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1363 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1368 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1369 __isl_keep isl_vec
*aff
)
1371 struct isl_upoly_cst
*cst
;
1372 struct isl_upoly_rec
*rec
;
1378 struct isl_upoly_cst
*cst
;
1380 cst
= isl_upoly_as_cst(up
);
1383 update_coeff(aff
, cst
, 0);
1387 rec
= isl_upoly_as_rec(up
);
1390 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1392 cst
= isl_upoly_as_cst(rec
->p
[1]);
1395 update_coeff(aff
, cst
, 1 + up
->var
);
1397 return isl_upoly_update_affine(rec
->p
[0], aff
);
1400 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1401 __isl_keep isl_qpolynomial
*qp
)
1409 isl_assert(qp
->div
->ctx
, qp
->div
->n_row
== 0, return NULL
);
1410 d
= isl_dim_total(qp
->dim
);
1411 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
1415 isl_seq_clr(aff
->el
+ 1, 1 + d
);
1416 isl_int_set_si(aff
->el
[0], 1);
1418 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1427 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1428 __isl_keep isl_qpolynomial
*qp2
)
1433 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1436 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1439 struct isl_upoly_rec
*rec
;
1441 if (isl_upoly_is_cst(up
)) {
1442 struct isl_upoly_cst
*cst
;
1443 cst
= isl_upoly_as_cst(up
);
1446 isl_int_lcm(*d
, *d
, cst
->d
);
1450 rec
= isl_upoly_as_rec(up
);
1454 for (i
= 0; i
< rec
->n
; ++i
)
1455 upoly_update_den(rec
->p
[i
], d
);
1458 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1460 isl_int_set_si(*d
, 1);
1463 upoly_update_den(qp
->upoly
, d
);
1466 __isl_give
struct isl_upoly
*isl_upoly_pow(isl_ctx
*ctx
, int pos
, int power
)
1469 struct isl_upoly
*up
;
1470 struct isl_upoly_rec
*rec
;
1471 struct isl_upoly_cst
*cst
;
1473 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1476 for (i
= 0; i
< 1 + power
; ++i
) {
1477 rec
->p
[i
] = isl_upoly_zero(ctx
);
1482 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1483 isl_int_set_si(cst
->n
, 1);
1487 isl_upoly_free(&rec
->up
);
1491 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_dim
*dim
,
1494 struct isl_ctx
*ctx
;
1501 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_pow(ctx
, pos
, power
));
1504 __isl_give isl_qpolynomial
*isl_qpolynomial_var(__isl_take isl_dim
*dim
,
1505 enum isl_dim_type type
, unsigned pos
)
1510 isl_assert(dim
->ctx
, isl_dim_size(dim
, isl_dim_in
) == 0, goto error
);
1511 isl_assert(dim
->ctx
, pos
< isl_dim_size(dim
, type
), goto error
);
1513 if (type
== isl_dim_set
)
1514 pos
+= isl_dim_size(dim
, isl_dim_param
);
1516 return isl_qpolynomial_pow(dim
, pos
, 1);
1522 __isl_give isl_qpolynomial
*isl_qpolynomial_div_pow(__isl_take isl_div
*div
,
1525 struct isl_qpolynomial
*qp
= NULL
;
1526 struct isl_upoly_rec
*rec
;
1527 struct isl_upoly_cst
*cst
;
1533 isl_assert(div
->ctx
, div
->bmap
->n_div
== 1, goto error
);
1535 pos
= isl_dim_total(div
->bmap
->dim
);
1536 rec
= isl_upoly_alloc_rec(div
->ctx
, pos
, 1 + power
);
1537 qp
= isl_qpolynomial_alloc(isl_basic_map_get_dim(div
->bmap
), 1,
1542 isl_seq_cpy(qp
->div
->row
[0], div
->line
[0], qp
->div
->n_col
- 1);
1543 isl_int_set_si(qp
->div
->row
[0][qp
->div
->n_col
- 1], 0);
1545 for (i
= 0; i
< 1 + power
; ++i
) {
1546 rec
->p
[i
] = isl_upoly_zero(div
->ctx
);
1551 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1552 isl_int_set_si(cst
->n
, 1);
1558 isl_qpolynomial_free(qp
);
1563 __isl_give isl_qpolynomial
*isl_qpolynomial_div(__isl_take isl_div
*div
)
1565 return isl_qpolynomial_div_pow(div
, 1);
1568 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst(__isl_take isl_dim
*dim
,
1569 const isl_int n
, const isl_int d
)
1571 struct isl_qpolynomial
*qp
;
1572 struct isl_upoly_cst
*cst
;
1574 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1578 cst
= isl_upoly_as_cst(qp
->upoly
);
1579 isl_int_set(cst
->n
, n
);
1580 isl_int_set(cst
->d
, d
);
1585 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
1587 struct isl_upoly_rec
*rec
;
1593 if (isl_upoly_is_cst(up
))
1597 active
[up
->var
] = 1;
1599 rec
= isl_upoly_as_rec(up
);
1600 for (i
= 0; i
< rec
->n
; ++i
)
1601 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
1607 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
1610 int d
= isl_dim_total(qp
->dim
);
1615 for (i
= 0; i
< d
; ++i
)
1616 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
1617 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
1623 return up_set_active(qp
->upoly
, active
, d
);
1626 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
1627 enum isl_dim_type type
, unsigned first
, unsigned n
)
1638 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1640 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1641 type
== isl_dim_set
, return -1);
1643 active
= isl_calloc_array(set
->ctx
, int, isl_dim_total(qp
->dim
));
1644 if (set_active(qp
, active
) < 0)
1647 if (type
== isl_dim_set
)
1648 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1649 for (i
= 0; i
< n
; ++i
)
1650 if (active
[first
+ i
]) {
1663 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
1664 unsigned first
, unsigned n
)
1667 struct isl_upoly_rec
*rec
;
1671 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
1673 if (up
->var
< first
+ n
) {
1674 up
= replace_by_constant_term(up
);
1675 return isl_upoly_drop(up
, first
, n
);
1677 up
= isl_upoly_cow(up
);
1681 rec
= isl_upoly_as_rec(up
);
1685 for (i
= 0; i
< rec
->n
; ++i
) {
1686 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
1697 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
1698 __isl_take isl_qpolynomial
*qp
,
1699 enum isl_dim_type type
, unsigned first
, unsigned n
)
1706 qp
= isl_qpolynomial_cow(qp
);
1710 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1712 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1713 type
== isl_dim_set
, goto error
);
1715 qp
->dim
= isl_dim_drop(qp
->dim
, type
, first
, n
);
1719 if (type
== isl_dim_set
)
1720 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1722 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
1726 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
1732 isl_qpolynomial_free(qp
);
1737 #define PW isl_pw_qpolynomial
1739 #define EL isl_qpolynomial
1741 #define IS_ZERO is_zero
1745 #define ADD(d,e1,e2) isl_qpolynomial_add(e1,e2)
1747 #include <isl_pw_templ.c>
1749 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
1757 if (!isl_set_fast_is_universe(pwqp
->p
[0].set
))
1760 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
1763 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
1764 __isl_take isl_pw_qpolynomial
*pwqp1
,
1765 __isl_take isl_pw_qpolynomial
*pwqp2
)
1768 struct isl_pw_qpolynomial
*res
;
1771 if (!pwqp1
|| !pwqp2
)
1774 isl_assert(pwqp1
->dim
->ctx
, isl_dim_equal(pwqp1
->dim
, pwqp2
->dim
),
1777 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
1778 isl_pw_qpolynomial_free(pwqp2
);
1782 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
1783 isl_pw_qpolynomial_free(pwqp1
);
1787 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
1788 isl_pw_qpolynomial_free(pwqp1
);
1792 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
1793 isl_pw_qpolynomial_free(pwqp2
);
1797 n
= pwqp1
->n
* pwqp2
->n
;
1798 res
= isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1
->dim
), n
);
1800 for (i
= 0; i
< pwqp1
->n
; ++i
) {
1801 for (j
= 0; j
< pwqp2
->n
; ++j
) {
1802 struct isl_set
*common
;
1803 struct isl_qpolynomial
*prod
;
1804 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
1805 isl_set_copy(pwqp2
->p
[j
].set
));
1806 if (isl_set_fast_is_empty(common
)) {
1807 isl_set_free(common
);
1811 prod
= isl_qpolynomial_mul(
1812 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
1813 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
1815 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
1819 isl_pw_qpolynomial_free(pwqp1
);
1820 isl_pw_qpolynomial_free(pwqp2
);
1824 isl_pw_qpolynomial_free(pwqp1
);
1825 isl_pw_qpolynomial_free(pwqp2
);
1829 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_neg(
1830 __isl_take isl_pw_qpolynomial
*pwqp
)
1833 struct isl_pw_qpolynomial
*res
;
1839 if (isl_pw_qpolynomial_is_zero(pwqp
))
1842 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1844 for (i
= 0; i
< pwqp
->n
; ++i
) {
1845 pwqp
->p
[i
].qp
= isl_qpolynomial_neg(pwqp
->p
[i
].qp
);
1852 isl_pw_qpolynomial_free(pwqp
);
1856 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sub(
1857 __isl_take isl_pw_qpolynomial
*pwqp1
,
1858 __isl_take isl_pw_qpolynomial
*pwqp2
)
1860 return isl_pw_qpolynomial_add(pwqp1
, isl_pw_qpolynomial_neg(pwqp2
));
1863 __isl_give
struct isl_upoly
*isl_upoly_eval(
1864 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
1867 struct isl_upoly_rec
*rec
;
1868 struct isl_upoly
*res
;
1869 struct isl_upoly
*base
;
1871 if (isl_upoly_is_cst(up
)) {
1876 rec
= isl_upoly_as_rec(up
);
1880 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1882 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
1884 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
1887 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1888 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1889 res
= isl_upoly_sum(res
,
1890 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
1891 isl_vec_copy(vec
)));
1894 isl_upoly_free(base
);
1904 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
1905 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
1908 struct isl_upoly
*up
;
1913 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, qp
->dim
), goto error
);
1915 if (qp
->div
->n_row
== 0)
1916 ext
= isl_vec_copy(pnt
->vec
);
1919 unsigned dim
= isl_dim_total(qp
->dim
);
1920 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
1924 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
1925 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1926 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
1927 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
1928 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
1929 qp
->div
->row
[i
][0]);
1933 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
1937 dim
= isl_dim_copy(qp
->dim
);
1938 isl_qpolynomial_free(qp
);
1939 isl_point_free(pnt
);
1941 return isl_qpolynomial_alloc(dim
, 0, up
);
1943 isl_qpolynomial_free(qp
);
1944 isl_point_free(pnt
);
1948 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
1949 __isl_keep
struct isl_upoly_cst
*cst2
)
1954 isl_int_mul(t
, cst1
->n
, cst2
->d
);
1955 isl_int_submul(t
, cst2
->n
, cst1
->d
);
1956 cmp
= isl_int_sgn(t
);
1961 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial
*qp1
,
1962 __isl_keep isl_qpolynomial
*qp2
)
1964 struct isl_upoly_cst
*cst1
, *cst2
;
1968 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), return -1);
1969 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), return -1);
1970 if (isl_qpolynomial_is_nan(qp1
))
1972 if (isl_qpolynomial_is_nan(qp2
))
1974 cst1
= isl_upoly_as_cst(qp1
->upoly
);
1975 cst2
= isl_upoly_as_cst(qp2
->upoly
);
1977 return isl_upoly_cmp(cst1
, cst2
) <= 0;
1980 __isl_give isl_qpolynomial
*isl_qpolynomial_min_cst(
1981 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1983 struct isl_upoly_cst
*cst1
, *cst2
;
1988 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
1989 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
1990 cst1
= isl_upoly_as_cst(qp1
->upoly
);
1991 cst2
= isl_upoly_as_cst(qp2
->upoly
);
1992 cmp
= isl_upoly_cmp(cst1
, cst2
);
1995 isl_qpolynomial_free(qp2
);
1997 isl_qpolynomial_free(qp1
);
2002 isl_qpolynomial_free(qp1
);
2003 isl_qpolynomial_free(qp2
);
2007 __isl_give isl_qpolynomial
*isl_qpolynomial_max_cst(
2008 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2010 struct isl_upoly_cst
*cst1
, *cst2
;
2015 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2016 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2017 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2018 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2019 cmp
= isl_upoly_cmp(cst1
, cst2
);
2022 isl_qpolynomial_free(qp2
);
2024 isl_qpolynomial_free(qp1
);
2029 isl_qpolynomial_free(qp1
);
2030 isl_qpolynomial_free(qp2
);
2034 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
2035 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
2036 unsigned first
, unsigned n
)
2045 qp
= isl_qpolynomial_cow(qp
);
2049 isl_assert(qp
->div
->ctx
, first
<= isl_dim_size(qp
->dim
, type
),
2052 g_pos
= pos(qp
->dim
, type
) + first
;
2054 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + g_pos
, n
);
2058 total
= qp
->div
->n_col
- 2;
2059 if (total
> g_pos
) {
2061 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
2064 for (i
= 0; i
< total
- g_pos
; ++i
)
2066 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
2072 qp
->dim
= isl_dim_insert(qp
->dim
, type
, first
, n
);
2078 isl_qpolynomial_free(qp
);
2082 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
2083 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
2087 pos
= isl_qpolynomial_dim(qp
, type
);
2089 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
2092 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
2093 __isl_take isl_pw_qpolynomial
*pwqp
,
2094 enum isl_dim_type type
, unsigned n
)
2101 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
2105 pwqp
->dim
= isl_dim_add(pwqp
->dim
, type
, n
);
2109 for (i
= 0; i
< pwqp
->n
; ++i
) {
2110 pwqp
->p
[i
].set
= isl_set_add(pwqp
->p
[i
].set
, type
, n
);
2111 if (!pwqp
->p
[i
].set
)
2113 pwqp
->p
[i
].qp
= isl_qpolynomial_add_dims(pwqp
->p
[i
].qp
, type
, n
);
2120 isl_pw_qpolynomial_free(pwqp
);
2124 static int *reordering_move(isl_ctx
*ctx
,
2125 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
2130 reordering
= isl_alloc_array(ctx
, int, len
);
2135 for (i
= 0; i
< dst
; ++i
)
2137 for (i
= 0; i
< n
; ++i
)
2138 reordering
[src
+ i
] = dst
+ i
;
2139 for (i
= 0; i
< src
- dst
; ++i
)
2140 reordering
[dst
+ i
] = dst
+ n
+ i
;
2141 for (i
= 0; i
< len
- src
- n
; ++i
)
2142 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
2144 for (i
= 0; i
< src
; ++i
)
2146 for (i
= 0; i
< n
; ++i
)
2147 reordering
[src
+ i
] = dst
+ i
;
2148 for (i
= 0; i
< dst
- src
; ++i
)
2149 reordering
[src
+ n
+ i
] = src
+ i
;
2150 for (i
= 0; i
< len
- dst
- n
; ++i
)
2151 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
2157 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
2161 struct isl_upoly_rec
*rec
;
2162 struct isl_upoly
*base
;
2163 struct isl_upoly
*res
;
2165 if (isl_upoly_is_cst(up
))
2168 rec
= isl_upoly_as_rec(up
);
2172 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2174 base
= isl_upoly_pow(up
->ctx
, r
[up
->var
], 1);
2175 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
2177 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2178 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2179 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
2182 isl_upoly_free(base
);
2191 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
2192 __isl_take isl_qpolynomial
*qp
,
2193 enum isl_dim_type dst_type
, unsigned dst_pos
,
2194 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2200 qp
= isl_qpolynomial_cow(qp
);
2204 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_dim_size(qp
->dim
, src_type
),
2207 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
2208 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
2209 if (dst_type
> src_type
)
2212 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
2213 qp
->div
= sort_divs(qp
->div
);
2217 reordering
= reordering_move(qp
->dim
->ctx
,
2218 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
2222 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2227 qp
->dim
= isl_dim_move(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2233 isl_qpolynomial_free(qp
);
2237 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2238 isl_int denom
, unsigned len
)
2241 struct isl_upoly
*up
;
2243 isl_assert(ctx
, len
>= 1, return NULL
);
2245 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2246 for (i
= 0; i
< len
- 1; ++i
) {
2247 struct isl_upoly
*t
;
2248 struct isl_upoly
*c
;
2250 if (isl_int_is_zero(f
[1 + i
]))
2253 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2254 t
= isl_upoly_pow(ctx
, i
, 1);
2255 t
= isl_upoly_mul(c
, t
);
2256 up
= isl_upoly_sum(up
, t
);
2262 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_dim
*dim
,
2263 isl_int
*f
, isl_int denom
)
2265 struct isl_upoly
*up
;
2270 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
, 1 + isl_dim_total(dim
));
2272 return isl_qpolynomial_alloc(dim
, 0, up
);
2275 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
2276 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
2280 struct isl_upoly
*up
;
2281 isl_qpolynomial
*qp
;
2287 isl_int_init(denom
);
2289 isl_constraint_get_coefficient(c
, type
, pos
, &denom
);
2290 isl_constraint_set_coefficient(c
, type
, pos
, c
->ctx
->zero
);
2291 sgn
= isl_int_sgn(denom
);
2292 isl_int_abs(denom
, denom
);
2293 up
= isl_upoly_from_affine(c
->ctx
, c
->line
[0], denom
,
2294 1 + isl_constraint_dim(c
, isl_dim_all
));
2296 isl_int_neg(denom
, denom
);
2297 isl_constraint_set_coefficient(c
, type
, pos
, denom
);
2299 dim
= isl_dim_copy(c
->bmap
->dim
);
2301 isl_int_clear(denom
);
2302 isl_constraint_free(c
);
2304 qp
= isl_qpolynomial_alloc(dim
, 0, up
);
2306 qp
= isl_qpolynomial_neg(qp
);
2310 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2311 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2314 struct isl_upoly_rec
*rec
;
2315 struct isl_upoly
*base
, *res
;
2320 if (isl_upoly_is_cst(up
))
2323 if (up
->var
< first
)
2326 rec
= isl_upoly_as_rec(up
);
2330 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2332 if (up
->var
>= first
+ n
)
2333 base
= isl_upoly_pow(up
->ctx
, up
->var
, 1);
2335 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2337 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2338 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2339 struct isl_upoly
*t
;
2340 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2341 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2342 res
= isl_upoly_sum(res
, t
);
2345 isl_upoly_free(base
);
2354 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2355 * in "qp" by subs[i].
2357 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
2358 __isl_take isl_qpolynomial
*qp
,
2359 enum isl_dim_type type
, unsigned first
, unsigned n
,
2360 __isl_keep isl_qpolynomial
**subs
)
2363 struct isl_upoly
**ups
;
2368 qp
= isl_qpolynomial_cow(qp
);
2371 for (i
= 0; i
< n
; ++i
)
2375 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
2378 for (i
= 0; i
< n
; ++i
)
2379 isl_assert(qp
->dim
->ctx
, isl_dim_equal(qp
->dim
, subs
[i
]->dim
),
2382 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
2383 for (i
= 0; i
< n
; ++i
)
2384 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
2386 first
+= pos(qp
->dim
, type
);
2388 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
2391 for (i
= 0; i
< n
; ++i
)
2392 ups
[i
] = subs
[i
]->upoly
;
2394 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
2403 isl_qpolynomial_free(qp
);
2407 __isl_give isl_basic_set
*add_div_constraints(__isl_take isl_basic_set
*bset
,
2408 __isl_take isl_mat
*div
)
2416 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2419 total
= isl_basic_set_total_dim(bset
);
2420 for (i
= 0; i
< div
->n_row
; ++i
)
2421 if (isl_basic_set_add_div_constraints_var(bset
,
2422 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2429 isl_basic_set_free(bset
);
2433 /* Extend "bset" with extra set dimensions for each integer division
2434 * in "qp" and then call "fn" with the extended bset and the polynomial
2435 * that results from replacing each of the integer divisions by the
2436 * corresponding extra set dimension.
2438 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
2439 __isl_keep isl_basic_set
*bset
,
2440 int (*fn
)(__isl_take isl_basic_set
*bset
,
2441 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
2445 isl_qpolynomial
*poly
;
2449 if (qp
->div
->n_row
== 0)
2450 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
2453 div
= isl_mat_copy(qp
->div
);
2454 dim
= isl_dim_copy(qp
->dim
);
2455 dim
= isl_dim_add(dim
, isl_dim_set
, qp
->div
->n_row
);
2456 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
2457 bset
= isl_basic_set_copy(bset
);
2458 bset
= isl_basic_set_add(bset
, isl_dim_set
, qp
->div
->n_row
);
2459 bset
= add_div_constraints(bset
, div
);
2461 return fn(bset
, poly
, user
);
2466 /* Return total degree in variables first (inclusive) up to last (exclusive).
2468 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
2472 struct isl_upoly_rec
*rec
;
2476 if (isl_upoly_is_zero(up
))
2478 if (isl_upoly_is_cst(up
) || up
->var
< first
)
2481 rec
= isl_upoly_as_rec(up
);
2485 for (i
= 0; i
< rec
->n
; ++i
) {
2488 if (isl_upoly_is_zero(rec
->p
[i
]))
2490 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
2500 /* Return total degree in set variables.
2502 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
2510 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
2511 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
2512 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
2515 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
2516 unsigned pos
, int deg
)
2519 struct isl_upoly_rec
*rec
;
2524 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
2526 return isl_upoly_copy(up
);
2528 return isl_upoly_zero(up
->ctx
);
2531 rec
= isl_upoly_as_rec(up
);
2535 if (up
->var
== pos
) {
2537 return isl_upoly_copy(rec
->p
[deg
]);
2539 return isl_upoly_zero(up
->ctx
);
2542 up
= isl_upoly_copy(up
);
2543 up
= isl_upoly_cow(up
);
2544 rec
= isl_upoly_as_rec(up
);
2548 for (i
= 0; i
< rec
->n
; ++i
) {
2549 struct isl_upoly
*t
;
2550 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
2553 isl_upoly_free(rec
->p
[i
]);
2563 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
2565 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
2566 __isl_keep isl_qpolynomial
*qp
,
2567 enum isl_dim_type type
, unsigned t_pos
, int deg
)
2570 struct isl_upoly
*up
;
2576 isl_assert(qp
->div
->ctx
, t_pos
< isl_dim_size(qp
->dim
, type
),
2579 g_pos
= pos(qp
->dim
, type
) + t_pos
;
2580 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
2582 c
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
, up
);
2585 isl_mat_free(c
->div
);
2586 c
->div
= isl_mat_copy(qp
->div
);
2591 isl_qpolynomial_free(c
);
2595 /* Homogenize the polynomial in the variables first (inclusive) up to
2596 * last (exclusive) by inserting powers of variable first.
2597 * Variable first is assumed not to appear in the input.
2599 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
2600 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
2601 int first
, int last
)
2604 struct isl_upoly_rec
*rec
;
2608 if (isl_upoly_is_zero(up
))
2612 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
2613 struct isl_upoly
*hom
;
2615 hom
= isl_upoly_pow(up
->ctx
, first
, target
- deg
);
2618 rec
= isl_upoly_as_rec(hom
);
2619 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
2624 up
= isl_upoly_cow(up
);
2625 rec
= isl_upoly_as_rec(up
);
2629 for (i
= 0; i
< rec
->n
; ++i
) {
2630 if (isl_upoly_is_zero(rec
->p
[i
]))
2632 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
2633 up
->var
< last
? deg
+ i
: i
, target
,
2645 /* Homogenize the polynomial in the set variables by introducing
2646 * powers of an extra set variable at position 0.
2648 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
2649 __isl_take isl_qpolynomial
*poly
)
2653 int deg
= isl_qpolynomial_degree(poly
);
2658 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_set
, 0, 1);
2659 poly
= isl_qpolynomial_cow(poly
);
2663 ovar
= isl_dim_offset(poly
->dim
, isl_dim_set
);
2664 nvar
= isl_dim_size(poly
->dim
, isl_dim_set
);
2665 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
2672 isl_qpolynomial_free(poly
);
2676 __isl_give isl_term
*isl_term_alloc(__isl_take isl_dim
*dim
,
2677 __isl_take isl_mat
*div
)
2685 n
= isl_dim_total(dim
) + div
->n_row
;
2687 term
= isl_calloc(dim
->ctx
, struct isl_term
,
2688 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
2695 isl_int_init(term
->n
);
2696 isl_int_init(term
->d
);
2705 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
2714 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
2723 total
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
2725 dup
= isl_term_alloc(isl_dim_copy(term
->dim
), isl_mat_copy(term
->div
));
2729 isl_int_set(dup
->n
, term
->n
);
2730 isl_int_set(dup
->d
, term
->d
);
2732 for (i
= 0; i
< total
; ++i
)
2733 dup
->pow
[i
] = term
->pow
[i
];
2738 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
2746 return isl_term_dup(term
);
2749 void isl_term_free(__isl_take isl_term
*term
)
2754 if (--term
->ref
> 0)
2757 isl_dim_free(term
->dim
);
2758 isl_mat_free(term
->div
);
2759 isl_int_clear(term
->n
);
2760 isl_int_clear(term
->d
);
2764 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
2772 case isl_dim_out
: return isl_dim_size(term
->dim
, type
);
2773 case isl_dim_div
: return term
->div
->n_row
;
2774 case isl_dim_all
: return isl_dim_total(term
->dim
) + term
->div
->n_row
;
2779 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
2781 return term
? term
->dim
->ctx
: NULL
;
2784 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
2788 isl_int_set(*n
, term
->n
);
2791 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
2795 isl_int_set(*d
, term
->d
);
2798 int isl_term_get_exp(__isl_keep isl_term
*term
,
2799 enum isl_dim_type type
, unsigned pos
)
2804 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
2806 if (type
>= isl_dim_set
)
2807 pos
+= isl_dim_size(term
->dim
, isl_dim_param
);
2808 if (type
>= isl_dim_div
)
2809 pos
+= isl_dim_size(term
->dim
, isl_dim_set
);
2811 return term
->pow
[pos
];
2814 __isl_give isl_div
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
2816 isl_basic_map
*bmap
;
2823 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
2826 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
2827 /* No nested divs for now */
2828 isl_assert(term
->dim
->ctx
,
2829 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
2830 term
->div
->n_row
) == -1,
2833 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(term
->dim
), 1, 0, 0);
2834 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
2837 isl_seq_cpy(bmap
->div
[k
], term
->div
->row
[pos
], 2 + total
);
2839 return isl_basic_map_div(bmap
, k
);
2841 isl_basic_map_free(bmap
);
2845 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
2846 int (*fn
)(__isl_take isl_term
*term
, void *user
),
2847 __isl_take isl_term
*term
, void *user
)
2850 struct isl_upoly_rec
*rec
;
2855 if (isl_upoly_is_zero(up
))
2858 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
2859 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
2860 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
2862 if (isl_upoly_is_cst(up
)) {
2863 struct isl_upoly_cst
*cst
;
2864 cst
= isl_upoly_as_cst(up
);
2867 term
= isl_term_cow(term
);
2870 isl_int_set(term
->n
, cst
->n
);
2871 isl_int_set(term
->d
, cst
->d
);
2872 if (fn(isl_term_copy(term
), user
) < 0)
2877 rec
= isl_upoly_as_rec(up
);
2881 for (i
= 0; i
< rec
->n
; ++i
) {
2882 term
= isl_term_cow(term
);
2885 term
->pow
[up
->var
] = i
;
2886 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
2890 term
->pow
[up
->var
] = 0;
2894 isl_term_free(term
);
2898 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
2899 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
2906 term
= isl_term_alloc(isl_dim_copy(qp
->dim
), isl_mat_copy(qp
->div
));
2910 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
2912 isl_term_free(term
);
2914 return term
? 0 : -1;
2917 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
2919 struct isl_upoly
*up
;
2920 isl_qpolynomial
*qp
;
2926 n
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
2928 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
2929 for (i
= 0; i
< n
; ++i
) {
2932 up
= isl_upoly_mul(up
,
2933 isl_upoly_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
2936 qp
= isl_qpolynomial_alloc(isl_dim_copy(term
->dim
), term
->div
->n_row
, up
);
2939 isl_mat_free(qp
->div
);
2940 qp
->div
= isl_mat_copy(term
->div
);
2944 isl_term_free(term
);
2947 isl_qpolynomial_free(qp
);
2948 isl_term_free(term
);
2952 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
2953 __isl_take isl_dim
*dim
)
2962 if (isl_dim_equal(qp
->dim
, dim
)) {
2967 qp
= isl_qpolynomial_cow(qp
);
2971 extra
= isl_dim_size(dim
, isl_dim_set
) -
2972 isl_dim_size(qp
->dim
, isl_dim_set
);
2973 total
= isl_dim_total(qp
->dim
);
2974 if (qp
->div
->n_row
) {
2977 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
2980 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
2982 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
2987 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
2990 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
2991 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
2993 isl_dim_free(qp
->dim
);
2999 isl_qpolynomial_free(qp
);
3003 /* For each parameter or variable that does not appear in qp,
3004 * first eliminate the variable from all constraints and then set it to zero.
3006 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3007 __isl_keep isl_qpolynomial
*qp
)
3018 d
= isl_dim_total(set
->dim
);
3019 active
= isl_calloc_array(set
->ctx
, int, d
);
3020 if (set_active(qp
, active
) < 0)
3023 for (i
= 0; i
< d
; ++i
)
3032 nparam
= isl_dim_size(set
->dim
, isl_dim_param
);
3033 nvar
= isl_dim_size(set
->dim
, isl_dim_set
);
3034 for (i
= 0; i
< nparam
; ++i
) {
3037 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
3038 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
3040 for (i
= 0; i
< nvar
; ++i
) {
3041 if (active
[nparam
+ i
])
3043 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
3044 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
3056 struct isl_opt_data
{
3057 isl_qpolynomial
*qp
;
3059 isl_qpolynomial
*opt
;
3063 static int opt_fn(__isl_take isl_point
*pnt
, void *user
)
3065 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
3066 isl_qpolynomial
*val
;
3068 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
3072 } else if (data
->max
) {
3073 data
->opt
= isl_qpolynomial_max_cst(data
->opt
, val
);
3075 data
->opt
= isl_qpolynomial_min_cst(data
->opt
, val
);
3081 __isl_give isl_qpolynomial
*isl_qpolynomial_opt_on_domain(
3082 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
3084 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
3089 if (isl_upoly_is_cst(qp
->upoly
)) {
3094 set
= fix_inactive(set
, qp
);
3097 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
3101 data
.opt
= isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp
));
3104 isl_qpolynomial_free(qp
);
3108 isl_qpolynomial_free(qp
);
3109 isl_qpolynomial_free(data
.opt
);
3113 __isl_give isl_qpolynomial
*isl_qpolynomial_morph(__isl_take isl_qpolynomial
*qp
,
3114 __isl_take isl_morph
*morph
)
3118 struct isl_upoly
*up
;
3120 struct isl_upoly
**subs
;
3123 qp
= isl_qpolynomial_cow(qp
);
3128 isl_assert(ctx
, isl_dim_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
3130 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, morph
->inv
->n_row
- 1);
3134 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3135 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
3136 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
3138 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, morph
->inv
->n_row
- 1, subs
);
3140 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
3141 isl_upoly_free(subs
[i
]);
3144 mat
= isl_mat_diagonal(isl_mat_identity(ctx
, 1), isl_mat_copy(morph
->inv
));
3145 mat
= isl_mat_diagonal(mat
, isl_mat_identity(ctx
, qp
->div
->n_row
));
3146 qp
->div
= isl_mat_product(qp
->div
, mat
);
3147 isl_dim_free(qp
->dim
);
3148 qp
->dim
= isl_dim_copy(morph
->ran
->dim
);
3150 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
3153 isl_morph_free(morph
);
3157 isl_qpolynomial_free(qp
);
3158 isl_morph_free(morph
);