2 #include <isl_polynomial_private.h>
3 #include <isl_point_private.h>
4 #include <isl_dim_private.h>
5 #include <isl_map_private.h>
7 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
15 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
20 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
22 return (struct isl_upoly_cst
*)up
;
25 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
30 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
32 return (struct isl_upoly_rec
*)up
;
35 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
37 struct isl_upoly_cst
*cst
;
41 if (!isl_upoly_is_cst(up
))
44 cst
= isl_upoly_as_cst(up
);
48 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
51 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
53 struct isl_upoly_cst
*cst
;
57 if (!isl_upoly_is_cst(up
))
60 cst
= isl_upoly_as_cst(up
);
64 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
67 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
69 struct isl_upoly_cst
*cst
;
73 if (!isl_upoly_is_cst(up
))
76 cst
= isl_upoly_as_cst(up
);
80 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
83 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
85 struct isl_upoly_cst
*cst
;
89 if (!isl_upoly_is_cst(up
))
92 cst
= isl_upoly_as_cst(up
);
96 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
99 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
101 struct isl_upoly_cst
*cst
;
105 if (!isl_upoly_is_cst(up
))
108 cst
= isl_upoly_as_cst(up
);
112 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
115 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
117 struct isl_upoly_cst
*cst
;
121 if (!isl_upoly_is_cst(up
))
124 cst
= isl_upoly_as_cst(up
);
128 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
131 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
133 struct isl_upoly_cst
*cst
;
135 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
144 isl_int_init(cst
->n
);
145 isl_int_init(cst
->d
);
150 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
152 struct isl_upoly_cst
*cst
;
154 cst
= isl_upoly_cst_alloc(ctx
);
158 isl_int_set_si(cst
->n
, 0);
159 isl_int_set_si(cst
->d
, 1);
164 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
166 struct isl_upoly_cst
*cst
;
168 cst
= isl_upoly_cst_alloc(ctx
);
172 isl_int_set_si(cst
->n
, 1);
173 isl_int_set_si(cst
->d
, 0);
178 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
180 struct isl_upoly_cst
*cst
;
182 cst
= isl_upoly_cst_alloc(ctx
);
186 isl_int_set_si(cst
->n
, 0);
187 isl_int_set_si(cst
->d
, 0);
192 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
193 isl_int n
, isl_int d
)
195 struct isl_upoly_cst
*cst
;
197 cst
= isl_upoly_cst_alloc(ctx
);
201 isl_int_set(cst
->n
, n
);
202 isl_int_set(cst
->d
, d
);
207 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
210 struct isl_upoly_rec
*rec
;
212 isl_assert(ctx
, var
>= 0, return NULL
);
213 isl_assert(ctx
, size
>= 0, return NULL
);
214 rec
= isl_calloc(dim
->ctx
, struct isl_upoly_rec
,
215 sizeof(struct isl_upoly_rec
) +
216 (size
- 1) * sizeof(struct isl_upoly
*));
230 isl_upoly_free(&rec
->up
);
234 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
236 struct isl_upoly_cst
*cst
;
241 return isl_upoly_is_zero(qp
->upoly
);
244 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
246 struct isl_upoly_cst
*cst
;
251 return isl_upoly_is_one(qp
->upoly
);
254 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
256 isl_int_clear(cst
->n
);
257 isl_int_clear(cst
->d
);
260 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
264 for (i
= 0; i
< rec
->n
; ++i
)
265 isl_upoly_free(rec
->p
[i
]);
268 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
277 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
279 struct isl_upoly_cst
*cst
;
280 struct isl_upoly_cst
*dup
;
282 cst
= isl_upoly_as_cst(up
);
286 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
289 isl_int_set(dup
->n
, cst
->n
);
290 isl_int_set(dup
->d
, cst
->d
);
295 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
298 struct isl_upoly_rec
*rec
;
299 struct isl_upoly_rec
*dup
;
301 rec
= isl_upoly_as_rec(up
);
305 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
309 for (i
= 0; i
< rec
->n
; ++i
) {
310 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
318 isl_upoly_free(&dup
->up
);
322 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
324 struct isl_upoly
*dup
;
329 if (isl_upoly_is_cst(up
))
330 return isl_upoly_dup_cst(up
);
332 return isl_upoly_dup_rec(up
);
335 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
343 return isl_upoly_dup(up
);
346 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
355 upoly_free_cst((struct isl_upoly_cst
*)up
);
357 upoly_free_rec((struct isl_upoly_rec
*)up
);
359 isl_ctx_deref(up
->ctx
);
363 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
368 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
369 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
370 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
371 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
376 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
377 __isl_take
struct isl_upoly
*up2
)
379 struct isl_upoly_cst
*cst1
;
380 struct isl_upoly_cst
*cst2
;
382 up1
= isl_upoly_cow(up1
);
386 cst1
= isl_upoly_as_cst(up1
);
387 cst2
= isl_upoly_as_cst(up2
);
389 if (isl_int_eq(cst1
->d
, cst2
->d
))
390 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
392 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
393 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
394 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
397 isl_upoly_cst_reduce(cst1
);
407 static __isl_give
struct isl_upoly
*replace_by_zero(
408 __isl_take
struct isl_upoly
*up
)
416 return isl_upoly_zero(ctx
);
419 static __isl_give
struct isl_upoly
*replace_by_constant_term(
420 __isl_take
struct isl_upoly
*up
)
422 struct isl_upoly_rec
*rec
;
423 struct isl_upoly
*cst
;
428 rec
= isl_upoly_as_rec(up
);
431 isl_assert(up
->ctx
, rec
->n
== 1, goto error
);
432 cst
= isl_upoly_copy(rec
->p
[0]);
440 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
441 __isl_take
struct isl_upoly
*up2
)
444 struct isl_upoly_rec
*rec1
, *rec2
;
449 if (isl_upoly_is_nan(up1
)) {
454 if (isl_upoly_is_nan(up2
)) {
459 if (isl_upoly_is_zero(up1
)) {
464 if (isl_upoly_is_zero(up2
)) {
469 if (up1
->var
< up2
->var
)
470 return isl_upoly_sum(up2
, up1
);
472 if (up2
->var
< up1
->var
) {
473 struct isl_upoly_rec
*rec
;
474 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
478 up1
= isl_upoly_cow(up1
);
479 rec
= isl_upoly_as_rec(up1
);
482 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
484 up1
= replace_by_constant_term(up1
);
488 if (isl_upoly_is_cst(up1
))
489 return isl_upoly_sum_cst(up1
, up2
);
491 rec1
= isl_upoly_as_rec(up1
);
492 rec2
= isl_upoly_as_rec(up2
);
496 if (rec1
->n
< rec2
->n
)
497 return isl_upoly_sum(up2
, up1
);
499 up1
= isl_upoly_cow(up1
);
500 rec1
= isl_upoly_as_rec(up1
);
504 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
505 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
506 isl_upoly_copy(rec2
->p
[i
]));
509 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
510 isl_upoly_free(rec1
->p
[i
]);
516 up1
= replace_by_zero(up1
);
517 else if (rec1
->n
== 1)
518 up1
= replace_by_constant_term(up1
);
529 __isl_give
struct isl_upoly
*isl_upoly_neg_cst(__isl_take
struct isl_upoly
*up
)
531 struct isl_upoly_cst
*cst
;
533 if (isl_upoly_is_zero(up
))
536 up
= isl_upoly_cow(up
);
540 cst
= isl_upoly_as_cst(up
);
542 isl_int_neg(cst
->n
, cst
->n
);
547 __isl_give
struct isl_upoly
*isl_upoly_neg(__isl_take
struct isl_upoly
*up
)
550 struct isl_upoly_rec
*rec
;
555 if (isl_upoly_is_cst(up
))
556 return isl_upoly_neg_cst(up
);
558 up
= isl_upoly_cow(up
);
559 rec
= isl_upoly_as_rec(up
);
563 for (i
= 0; i
< rec
->n
; ++i
) {
564 rec
->p
[i
] = isl_upoly_neg(rec
->p
[i
]);
575 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
576 __isl_take
struct isl_upoly
*up2
)
578 struct isl_upoly_cst
*cst1
;
579 struct isl_upoly_cst
*cst2
;
581 up1
= isl_upoly_cow(up1
);
585 cst1
= isl_upoly_as_cst(up1
);
586 cst2
= isl_upoly_as_cst(up2
);
588 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
589 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
591 isl_upoly_cst_reduce(cst1
);
601 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
602 __isl_take
struct isl_upoly
*up2
)
604 struct isl_upoly_rec
*rec1
;
605 struct isl_upoly_rec
*rec2
;
606 struct isl_upoly_rec
*res
;
610 rec1
= isl_upoly_as_rec(up1
);
611 rec2
= isl_upoly_as_rec(up2
);
614 size
= rec1
->n
+ rec2
->n
- 1;
615 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
619 for (i
= 0; i
< rec1
->n
; ++i
) {
620 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
621 isl_upoly_copy(rec1
->p
[i
]));
626 for (; i
< size
; ++i
) {
627 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
632 for (i
= 0; i
< rec1
->n
; ++i
) {
633 for (j
= 1; j
< rec2
->n
; ++j
) {
634 struct isl_upoly
*up
;
635 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
636 isl_upoly_copy(rec1
->p
[i
]));
637 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
650 isl_upoly_free(&res
->up
);
654 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
655 __isl_take
struct isl_upoly
*up2
)
660 if (isl_upoly_is_nan(up1
)) {
665 if (isl_upoly_is_nan(up2
)) {
670 if (isl_upoly_is_zero(up1
)) {
675 if (isl_upoly_is_zero(up2
)) {
680 if (isl_upoly_is_one(up1
)) {
685 if (isl_upoly_is_one(up2
)) {
690 if (up1
->var
< up2
->var
)
691 return isl_upoly_mul(up2
, up1
);
693 if (up2
->var
< up1
->var
) {
695 struct isl_upoly_rec
*rec
;
696 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
697 isl_ctx
*ctx
= up1
->ctx
;
700 return isl_upoly_nan(ctx
);
702 up1
= isl_upoly_cow(up1
);
703 rec
= isl_upoly_as_rec(up1
);
707 for (i
= 0; i
< rec
->n
; ++i
) {
708 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
709 isl_upoly_copy(up2
));
717 if (isl_upoly_is_cst(up1
))
718 return isl_upoly_mul_cst(up1
, up2
);
720 return isl_upoly_mul_rec(up1
, up2
);
727 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_dim
*dim
,
730 struct isl_qpolynomial
*qp
;
736 total
= isl_dim_total(dim
);
738 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
743 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
752 isl_qpolynomial_free(qp
);
756 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
765 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
767 struct isl_qpolynomial
*dup
;
772 dup
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
);
775 isl_mat_free(dup
->div
);
776 dup
->div
= isl_mat_copy(qp
->div
);
779 dup
->upoly
= isl_upoly_copy(qp
->upoly
);
785 isl_qpolynomial_free(dup
);
789 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
797 return isl_qpolynomial_dup(qp
);
800 void isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
808 isl_dim_free(qp
->dim
);
809 isl_mat_free(qp
->div
);
810 isl_upoly_free(qp
->upoly
);
815 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
820 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
821 div1
->n_col
>= div2
->n_col
, return -1);
823 if (div1
->n_row
== div2
->n_row
)
824 return isl_mat_is_equal(div1
, div2
);
828 div1
->n_row
= div2
->n_row
;
829 div1
->n_col
= div2
->n_col
;
831 equal
= isl_mat_is_equal(div1
, div2
);
839 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
840 __isl_keep isl_mat
*src
, int s
, int *exp
)
843 unsigned c
= src
->n_col
- src
->n_row
;
845 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
846 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
848 for (i
= 0; i
< s
; ++i
)
849 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
852 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
856 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
857 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
862 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
865 static __isl_give isl_mat
*merge_divs(__isl_keep isl_mat
*div1
,
866 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
870 unsigned d
= div1
->n_col
- div1
->n_row
;
872 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
873 d
+ div1
->n_row
+ div2
->n_row
);
877 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
880 expand_row(div
, k
, div1
, i
, exp1
);
881 expand_row(div
, k
+ 1, div2
, j
, exp2
);
883 cmp
= cmp_row(div
, k
, k
+ 1);
887 } else if (cmp
< 0) {
891 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
894 for (; i
< div1
->n_row
; ++i
, ++k
) {
895 expand_row(div
, k
, div1
, i
, exp1
);
898 for (; j
< div2
->n_row
; ++j
, ++k
) {
899 expand_row(div
, k
, div2
, j
, exp2
);
909 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
913 struct isl_upoly_rec
*rec
;
915 if (isl_upoly_is_cst(up
))
921 if (exp
[up
->var
- first
] == up
->var
- first
)
924 up
= isl_upoly_cow(up
);
928 up
->var
= exp
[up
->var
- first
] + first
;
930 rec
= isl_upoly_as_rec(up
);
934 for (i
= 0; i
< rec
->n
; ++i
) {
935 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
946 static __isl_give isl_qpolynomial
*with_merged_divs(
947 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
948 __isl_take isl_qpolynomial
*qp2
),
949 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
955 qp1
= isl_qpolynomial_cow(qp1
);
956 qp2
= isl_qpolynomial_cow(qp2
);
961 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
962 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
964 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
965 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
969 div
= merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
973 isl_mat_free(qp1
->div
);
974 qp1
->div
= isl_mat_copy(div
);
975 isl_mat_free(qp2
->div
);
976 qp2
->div
= isl_mat_copy(div
);
978 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
979 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
981 if (!qp1
->upoly
|| !qp2
->upoly
)
993 isl_qpolynomial_free(qp1
);
994 isl_qpolynomial_free(qp2
);
998 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
999 __isl_take isl_qpolynomial
*qp2
)
1001 qp1
= isl_qpolynomial_cow(qp1
);
1006 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1007 return isl_qpolynomial_add(qp2
, qp1
);
1009 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1010 if (!compatible_divs(qp1
->div
, qp2
->div
))
1011 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1013 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1017 isl_qpolynomial_free(qp2
);
1021 isl_qpolynomial_free(qp1
);
1022 isl_qpolynomial_free(qp2
);
1026 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1027 __isl_take isl_qpolynomial
*qp2
)
1029 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1032 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1034 qp
= isl_qpolynomial_cow(qp
);
1039 qp
->upoly
= isl_upoly_neg(qp
->upoly
);
1045 isl_qpolynomial_free(qp
);
1049 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1050 __isl_take isl_qpolynomial
*qp2
)
1052 qp1
= isl_qpolynomial_cow(qp1
);
1057 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1058 return isl_qpolynomial_mul(qp2
, qp1
);
1060 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1061 if (!compatible_divs(qp1
->div
, qp2
->div
))
1062 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1064 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1068 isl_qpolynomial_free(qp2
);
1072 isl_qpolynomial_free(qp1
);
1073 isl_qpolynomial_free(qp2
);
1077 __isl_give isl_qpolynomial
*isl_qpolynomial_zero(__isl_take isl_dim
*dim
)
1079 struct isl_qpolynomial
*qp
;
1080 struct isl_upoly_cst
*cst
;
1082 qp
= isl_qpolynomial_alloc(dim
, 0);
1086 qp
->upoly
= isl_upoly_zero(dim
->ctx
);
1092 isl_qpolynomial_free(qp
);
1096 __isl_give isl_qpolynomial
*isl_qpolynomial_infty(__isl_take isl_dim
*dim
)
1098 struct isl_qpolynomial
*qp
;
1099 struct isl_upoly_cst
*cst
;
1101 qp
= isl_qpolynomial_alloc(dim
, 0);
1105 qp
->upoly
= isl_upoly_infty(dim
->ctx
);
1111 isl_qpolynomial_free(qp
);
1115 __isl_give isl_qpolynomial
*isl_qpolynomial_nan(__isl_take isl_dim
*dim
)
1117 struct isl_qpolynomial
*qp
;
1118 struct isl_upoly_cst
*cst
;
1120 qp
= isl_qpolynomial_alloc(dim
, 0);
1124 qp
->upoly
= isl_upoly_nan(dim
->ctx
);
1130 isl_qpolynomial_free(qp
);
1134 __isl_give isl_qpolynomial
*isl_qpolynomial_cst(__isl_take isl_dim
*dim
,
1137 struct isl_qpolynomial
*qp
;
1138 struct isl_upoly_cst
*cst
;
1140 qp
= isl_qpolynomial_alloc(dim
, 0);
1144 qp
->upoly
= isl_upoly_zero(dim
->ctx
);
1147 cst
= isl_upoly_as_cst(qp
->upoly
);
1148 isl_int_set(cst
->n
, v
);
1152 isl_qpolynomial_free(qp
);
1156 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1157 isl_int
*n
, isl_int
*d
)
1159 struct isl_upoly_cst
*cst
;
1164 if (!isl_upoly_is_cst(qp
->upoly
))
1167 cst
= isl_upoly_as_cst(qp
->upoly
);
1172 isl_int_set(*n
, cst
->n
);
1174 isl_int_set(*d
, cst
->d
);
1179 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_dim
*dim
,
1183 struct isl_qpolynomial
*qp
;
1184 struct isl_upoly_rec
*rec
;
1185 struct isl_upoly_cst
*cst
;
1186 struct isl_ctx
*ctx
;
1193 qp
= isl_qpolynomial_alloc(dim
, 0);
1197 qp
->upoly
= &isl_upoly_alloc_rec(ctx
, pos
, 1 + power
)->up
;
1200 rec
= isl_upoly_as_rec(qp
->upoly
);
1201 for (i
= 0; i
< 1 + power
; ++i
) {
1202 rec
->p
[i
] = isl_upoly_zero(ctx
);
1207 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1208 isl_int_set_si(cst
->n
, 1);
1212 isl_qpolynomial_free(qp
);
1216 __isl_give isl_qpolynomial
*isl_qpolynomial_var(__isl_take isl_dim
*dim
,
1217 enum isl_dim_type type
, unsigned pos
)
1222 isl_assert(dim
->ctx
, isl_dim_size(dim
, isl_dim_in
) == 0, goto error
);
1223 isl_assert(dim
->ctx
, pos
< isl_dim_size(dim
, type
), goto error
);
1225 if (type
== isl_dim_set
)
1226 pos
+= isl_dim_size(dim
, isl_dim_param
);
1228 return isl_qpolynomial_pow(dim
, pos
, 1);
1234 __isl_give isl_qpolynomial
*isl_qpolynomial_div_pow(__isl_take isl_div
*div
,
1237 struct isl_qpolynomial
*qp
= NULL
;
1238 struct isl_upoly_rec
*rec
;
1239 struct isl_upoly_cst
*cst
;
1245 isl_assert(div
->ctx
, div
->bmap
->n_div
== 1, goto error
);
1247 qp
= isl_qpolynomial_alloc(isl_basic_map_get_dim(div
->bmap
), 1);
1251 isl_seq_cpy(qp
->div
->row
[0], div
->line
[0], qp
->div
->n_col
- 1);
1252 isl_int_set_si(qp
->div
->row
[0][qp
->div
->n_col
- 1], 0);
1254 pos
= isl_dim_total(qp
->dim
);
1255 qp
->upoly
= &isl_upoly_alloc_rec(div
->ctx
, pos
, 1 + power
)->up
;
1258 rec
= isl_upoly_as_rec(qp
->upoly
);
1259 for (i
= 0; i
< 1 + power
; ++i
) {
1260 rec
->p
[i
] = isl_upoly_zero(div
->ctx
);
1265 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1266 isl_int_set_si(cst
->n
, 1);
1272 isl_qpolynomial_free(qp
);
1277 __isl_give isl_qpolynomial
*isl_qpolynomial_div(__isl_take isl_div
*div
)
1279 return isl_qpolynomial_div_pow(div
, 1);
1282 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst(__isl_take isl_dim
*dim
,
1283 const isl_int n
, const isl_int d
)
1285 struct isl_qpolynomial
*qp
;
1286 struct isl_upoly_cst
*cst
;
1288 qp
= isl_qpolynomial_alloc(dim
, 0);
1292 qp
->upoly
= isl_upoly_zero(dim
->ctx
);
1295 cst
= isl_upoly_as_cst(qp
->upoly
);
1296 isl_int_set(cst
->n
, n
);
1297 isl_int_set(cst
->d
, d
);
1301 isl_qpolynomial_free(qp
);
1306 #define PW isl_pw_qpolynomial
1308 #define EL isl_qpolynomial
1310 #define IS_ZERO is_zero
1316 #include <isl_pw_templ.c>
1319 #define PW isl_pw_qpolynomial_fold
1321 #define EL isl_qpolynomial_fold
1323 #define IS_ZERO is_empty
1329 #include <isl_pw_templ.c>
1331 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
1339 if (!isl_set_fast_is_universe(pwqp
->p
[0].set
))
1342 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
1345 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
1346 __isl_take isl_pw_qpolynomial
*pwqp1
,
1347 __isl_take isl_pw_qpolynomial
*pwqp2
)
1350 struct isl_pw_qpolynomial
*res
;
1353 if (!pwqp1
|| !pwqp2
)
1356 isl_assert(pwqp1
->dim
->ctx
, isl_dim_equal(pwqp1
->dim
, pwqp2
->dim
),
1359 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
1360 isl_pw_qpolynomial_free(pwqp2
);
1364 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
1365 isl_pw_qpolynomial_free(pwqp1
);
1369 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
1370 isl_pw_qpolynomial_free(pwqp1
);
1374 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
1375 isl_pw_qpolynomial_free(pwqp2
);
1379 n
= pwqp1
->n
* pwqp2
->n
;
1380 res
= isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1
->dim
), n
);
1382 for (i
= 0; i
< pwqp1
->n
; ++i
) {
1383 for (j
= 0; j
< pwqp2
->n
; ++j
) {
1384 struct isl_set
*common
;
1385 struct isl_qpolynomial
*prod
;
1386 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
1387 isl_set_copy(pwqp2
->p
[j
].set
));
1388 if (isl_set_fast_is_empty(common
)) {
1389 isl_set_free(common
);
1393 prod
= isl_qpolynomial_mul(
1394 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
1395 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
1397 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
1401 isl_pw_qpolynomial_free(pwqp1
);
1402 isl_pw_qpolynomial_free(pwqp2
);
1406 isl_pw_qpolynomial_free(pwqp1
);
1407 isl_pw_qpolynomial_free(pwqp2
);
1411 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_neg(
1412 __isl_take isl_pw_qpolynomial
*pwqp
)
1415 struct isl_pw_qpolynomial
*res
;
1421 if (isl_pw_qpolynomial_is_zero(pwqp
))
1424 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1426 for (i
= 0; i
< pwqp
->n
; ++i
) {
1427 pwqp
->p
[i
].qp
= isl_qpolynomial_neg(pwqp
->p
[i
].qp
);
1434 isl_pw_qpolynomial_free(pwqp
);
1438 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sub(
1439 __isl_take isl_pw_qpolynomial
*pwqp1
,
1440 __isl_take isl_pw_qpolynomial
*pwqp2
)
1442 return isl_pw_qpolynomial_add(pwqp1
, isl_pw_qpolynomial_neg(pwqp2
));
1445 __isl_give
struct isl_upoly
*isl_upoly_eval(
1446 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
1449 struct isl_upoly_rec
*rec
;
1450 struct isl_upoly
*res
;
1451 struct isl_upoly
*base
;
1453 if (isl_upoly_is_cst(up
)) {
1458 rec
= isl_upoly_as_rec(up
);
1462 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1464 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
1466 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
1469 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1470 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1471 res
= isl_upoly_sum(res
,
1472 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
1473 isl_vec_copy(vec
)));
1476 isl_upoly_free(base
);
1486 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
1487 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
1490 struct isl_upoly
*up
;
1495 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, qp
->dim
), goto error
);
1497 if (qp
->div
->n_row
== 0)
1498 ext
= isl_vec_copy(pnt
->vec
);
1501 unsigned dim
= isl_dim_total(qp
->dim
);
1502 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
1506 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
1507 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1508 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
1509 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
1510 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
1511 qp
->div
->row
[i
][0]);
1515 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
1519 dim
= isl_dim_copy(qp
->dim
);
1520 isl_qpolynomial_free(qp
);
1521 isl_point_free(pnt
);
1523 qp
= isl_qpolynomial_alloc(dim
, 0);
1531 isl_qpolynomial_free(qp
);
1532 isl_point_free(pnt
);
1536 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
1537 __isl_keep
struct isl_upoly_cst
*cst2
)
1542 isl_int_mul(t
, cst1
->n
, cst2
->d
);
1543 isl_int_submul(t
, cst2
->n
, cst1
->d
);
1544 cmp
= isl_int_sgn(t
);
1549 static __isl_give isl_qpolynomial
*qpolynomial_min(
1550 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1552 struct isl_upoly_cst
*cst1
, *cst2
;
1557 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
1558 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
1559 cst1
= isl_upoly_as_cst(qp1
->upoly
);
1560 cst2
= isl_upoly_as_cst(qp2
->upoly
);
1561 cmp
= isl_upoly_cmp(cst1
, cst2
);
1564 isl_qpolynomial_free(qp2
);
1566 isl_qpolynomial_free(qp1
);
1571 isl_qpolynomial_free(qp1
);
1572 isl_qpolynomial_free(qp2
);
1576 static __isl_give isl_qpolynomial
*qpolynomial_max(
1577 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1579 struct isl_upoly_cst
*cst1
, *cst2
;
1584 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
1585 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
1586 cst1
= isl_upoly_as_cst(qp1
->upoly
);
1587 cst2
= isl_upoly_as_cst(qp2
->upoly
);
1588 cmp
= isl_upoly_cmp(cst1
, cst2
);
1591 isl_qpolynomial_free(qp2
);
1593 isl_qpolynomial_free(qp1
);
1598 isl_qpolynomial_free(qp1
);
1599 isl_qpolynomial_free(qp2
);
1603 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_eval(
1604 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
1606 isl_qpolynomial
*qp
;
1610 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, fold
->dim
), goto error
);
1611 isl_assert(pnt
->dim
->ctx
,
1612 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
1616 qp
= isl_qpolynomial_zero(isl_dim_copy(fold
->dim
));
1619 qp
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
1620 isl_point_copy(pnt
));
1621 for (i
= 1; i
< fold
->n
; ++i
) {
1622 isl_qpolynomial
*qp_i
;
1623 qp_i
= isl_qpolynomial_eval(
1624 isl_qpolynomial_copy(fold
->qp
[i
]),
1625 isl_point_copy(pnt
));
1626 if (fold
->type
== isl_fold_max
)
1627 qp
= qpolynomial_max(qp
, qp_i
);
1629 qp
= qpolynomial_min(qp
, qp_i
);
1632 isl_qpolynomial_fold_free(fold
);
1633 isl_point_free(pnt
);
1637 isl_qpolynomial_fold_free(fold
);
1638 isl_point_free(pnt
);
1642 __isl_give isl_qpolynomial
*isl_qpolynomial_move(__isl_take isl_qpolynomial
*qp
,
1643 enum isl_dim_type dst_type
, unsigned dst_pos
,
1644 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1649 qp
->dim
= isl_dim_move(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
1653 /* Do something to polynomials when needed; later */
1657 isl_qpolynomial_free(qp
);
1661 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_move(
1662 __isl_take isl_pw_qpolynomial
*pwqp
,
1663 enum isl_dim_type dst_type
, unsigned dst_pos
,
1664 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1671 pwqp
->dim
= isl_dim_move(pwqp
->dim
,
1672 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1676 for (i
= 0; i
< pwqp
->n
; ++i
) {
1677 pwqp
->p
[i
].set
= isl_set_move(pwqp
->p
[i
].set
, dst_type
, dst_pos
,
1678 src_type
, src_pos
, n
);
1679 if (!pwqp
->p
[i
].set
)
1681 pwqp
->p
[i
].qp
= isl_qpolynomial_move(pwqp
->p
[i
].qp
,
1682 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1689 isl_pw_qpolynomial_free(pwqp
);
1693 __isl_give isl_dim
*isl_pw_qpolynomial_get_dim(
1694 __isl_keep isl_pw_qpolynomial
*pwqp
)
1699 return isl_dim_copy(pwqp
->dim
);
1702 unsigned isl_pw_qpolynomial_dim(__isl_keep isl_pw_qpolynomial
*pwqp
,
1703 enum isl_dim_type type
)
1705 return pwqp
? isl_dim_size(pwqp
->dim
, type
) : 0;
1708 __isl_give isl_term
*isl_term_alloc(__isl_take isl_dim
*dim
,
1709 __isl_take isl_mat
*div
)
1717 n
= isl_dim_total(dim
) + div
->n_row
;
1719 term
= isl_calloc(dim
->ctx
, struct isl_term
,
1720 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
1727 isl_int_init(term
->n
);
1728 isl_int_init(term
->d
);
1737 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
1746 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
1755 total
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
1757 dup
= isl_term_alloc(isl_dim_copy(term
->dim
), isl_mat_copy(term
->div
));
1761 isl_int_set(dup
->n
, term
->n
);
1762 isl_int_set(dup
->d
, term
->d
);
1764 for (i
= 0; i
< total
; ++i
)
1765 dup
->pow
[i
] = term
->pow
[i
];
1770 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
1778 return isl_term_dup(term
);
1781 void isl_term_free(__isl_take isl_term
*term
)
1786 if (--term
->ref
> 0)
1789 isl_dim_free(term
->dim
);
1790 isl_mat_free(term
->div
);
1791 isl_int_clear(term
->n
);
1792 isl_int_clear(term
->d
);
1796 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
1804 case isl_dim_out
: return isl_dim_size(term
->dim
, type
);
1805 case isl_dim_div
: return term
->div
->n_row
;
1806 case isl_dim_all
: return isl_dim_total(term
->dim
) + term
->div
->n_row
;
1810 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
1812 return term
? term
->dim
->ctx
: NULL
;
1815 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
1819 isl_int_set(*n
, term
->n
);
1822 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
1826 isl_int_set(*d
, term
->d
);
1829 int isl_term_get_exp(__isl_keep isl_term
*term
,
1830 enum isl_dim_type type
, unsigned pos
)
1835 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
1837 if (type
>= isl_dim_set
)
1838 pos
+= isl_dim_size(term
->dim
, isl_dim_param
);
1839 if (type
>= isl_dim_div
)
1840 pos
+= isl_dim_size(term
->dim
, isl_dim_set
);
1842 return term
->pow
[pos
];
1845 __isl_give isl_div
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
1847 isl_basic_map
*bmap
;
1854 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
1857 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
1858 /* No nested divs for now */
1859 isl_assert(term
->dim
->ctx
,
1860 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
1861 term
->div
->n_row
) == -1,
1864 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(term
->dim
), 1, 0, 0);
1865 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
1868 isl_seq_cpy(bmap
->div
[k
], term
->div
->row
[pos
], 2 + total
);
1870 return isl_basic_map_div(bmap
, k
);
1872 isl_basic_map_free(bmap
);
1876 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
1877 int (*fn
)(__isl_take isl_term
*term
, void *user
),
1878 __isl_take isl_term
*term
, void *user
)
1881 struct isl_upoly_rec
*rec
;
1886 if (isl_upoly_is_zero(up
))
1889 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
1890 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
1891 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
1893 if (isl_upoly_is_cst(up
)) {
1894 struct isl_upoly_cst
*cst
;
1895 cst
= isl_upoly_as_cst(up
);
1898 term
= isl_term_cow(term
);
1901 isl_int_set(term
->n
, cst
->n
);
1902 isl_int_set(term
->d
, cst
->d
);
1903 if (fn(isl_term_copy(term
), user
) < 0)
1908 rec
= isl_upoly_as_rec(up
);
1912 for (i
= 0; i
< rec
->n
; ++i
) {
1913 term
= isl_term_cow(term
);
1916 term
->pow
[up
->var
] = i
;
1917 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
1921 term
->pow
[up
->var
] = 0;
1925 isl_term_free(term
);
1929 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
1930 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
1937 term
= isl_term_alloc(isl_dim_copy(qp
->dim
), isl_mat_copy(qp
->div
));
1941 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
1943 isl_term_free(term
);
1945 return term
? 0 : -1;
1948 int isl_pw_qpolynomial_foreach_piece(__isl_keep isl_pw_qpolynomial
*pwqp
,
1949 int (*fn
)(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
1950 void *user
), void *user
)
1957 for (i
= 0; i
< pwqp
->n
; ++i
)
1958 if (fn(isl_set_copy(pwqp
->p
[i
].set
),
1959 isl_qpolynomial_copy(pwqp
->p
[i
].qp
), user
) < 0)
1965 static int any_divs(__isl_keep isl_set
*set
)
1972 for (i
= 0; i
< set
->n
; ++i
)
1973 if (set
->p
[i
]->n_div
> 0)
1979 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
1980 __isl_take isl_dim
*dim
)
1985 if (isl_dim_equal(qp
->dim
, dim
)) {
1990 qp
= isl_qpolynomial_cow(qp
);
1994 if (qp
->div
->n_row
) {
2000 extra
= isl_dim_size(dim
, isl_dim_set
) -
2001 isl_dim_size(qp
->dim
, isl_dim_set
);
2002 total
= isl_dim_total(qp
->dim
);
2003 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
2006 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
2008 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
2012 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
2015 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
2016 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
2019 isl_dim_free(qp
->dim
);
2025 isl_qpolynomial_free(qp
);
2029 static int foreach_lifted_subset(__isl_take isl_set
*set
,
2030 __isl_take isl_qpolynomial
*qp
,
2031 int (*fn
)(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
2032 void *user
), void *user
)
2039 for (i
= 0; i
< set
->n
; ++i
) {
2041 isl_qpolynomial
*copy
;
2043 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
2044 lift
= isl_set_lift(lift
);
2046 copy
= isl_qpolynomial_copy(qp
);
2047 copy
= isl_qpolynomial_lift(copy
, isl_set_get_dim(lift
));
2049 if (fn(lift
, copy
, user
) < 0)
2054 isl_qpolynomial_free(qp
);
2059 isl_qpolynomial_free(qp
);
2063 int isl_pw_qpolynomial_foreach_lifted_piece(__isl_keep isl_pw_qpolynomial
*pwqp
,
2064 int (*fn
)(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
2065 void *user
), void *user
)
2072 for (i
= 0; i
< pwqp
->n
; ++i
) {
2074 isl_qpolynomial
*qp
;
2076 set
= isl_set_copy(pwqp
->p
[i
].set
);
2077 qp
= isl_qpolynomial_copy(pwqp
->p
[i
].qp
);
2078 if (!any_divs(set
)) {
2079 if (fn(set
, qp
, user
) < 0)
2083 if (foreach_lifted_subset(set
, qp
, fn
, user
) < 0)
2090 static __isl_give isl_qpolynomial_fold
*qpolynomial_fold_alloc(
2091 enum isl_fold type
, __isl_take isl_dim
*dim
, int n
)
2093 isl_qpolynomial_fold
*fold
;
2098 isl_assert(dim
->ctx
, n
>= 0, goto error
);
2099 fold
= isl_alloc(dim
->ctx
, struct isl_qpolynomial_fold
,
2100 sizeof(struct isl_qpolynomial_fold
) +
2101 (n
- 1) * sizeof(struct isl_qpolynomial
*));
2117 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_empty(enum isl_fold type
,
2118 __isl_take isl_dim
*dim
)
2120 return qpolynomial_fold_alloc(type
, dim
, 0);
2123 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_alloc(
2124 enum isl_fold type
, __isl_take isl_qpolynomial
*qp
)
2126 isl_qpolynomial_fold
*fold
;
2131 fold
= qpolynomial_fold_alloc(type
, isl_dim_copy(qp
->dim
), 1);
2140 isl_qpolynomial_fold_free(fold
);
2141 isl_qpolynomial_free(qp
);
2145 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_copy(
2146 __isl_keep isl_qpolynomial_fold
*fold
)
2155 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold
*fold
)
2161 if (--fold
->ref
> 0)
2164 for (i
= 0; i
< fold
->n
; ++i
)
2165 isl_qpolynomial_free(fold
->qp
[i
]);
2166 isl_dim_free(fold
->dim
);
2170 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
2175 return fold
->n
== 0;
2178 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
2179 __isl_take isl_qpolynomial_fold
*fold1
,
2180 __isl_take isl_qpolynomial_fold
*fold2
)
2183 struct isl_qpolynomial_fold
*res
= NULL
;
2185 if (!fold1
|| !fold2
)
2188 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
2189 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
2192 if (isl_qpolynomial_fold_is_empty(fold1
)) {
2193 isl_qpolynomial_fold_free(fold1
);
2197 if (isl_qpolynomial_fold_is_empty(fold2
)) {
2198 isl_qpolynomial_fold_free(fold2
);
2202 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
2203 fold1
->n
+ fold2
->n
);
2207 for (i
= 0; i
< fold1
->n
; ++i
) {
2208 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
2209 if (!res
->qp
[res
->n
])
2214 for (i
= 0; i
< fold2
->n
; ++i
) {
2215 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
2216 if (!res
->qp
[res
->n
])
2221 isl_qpolynomial_fold_free(fold1
);
2222 isl_qpolynomial_fold_free(fold2
);
2226 isl_qpolynomial_fold_free(res
);
2227 isl_qpolynomial_fold_free(fold1
);
2228 isl_qpolynomial_fold_free(fold2
);
2232 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
2233 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
2236 isl_pw_qpolynomial_fold
*pwf
;
2241 pwf
= isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp
->dim
), pwqp
->n
);
2243 for (i
= 0; i
< pwqp
->n
; ++i
)
2244 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
2245 isl_set_copy(pwqp
->p
[i
].set
),
2246 isl_qpolynomial_fold_alloc(type
,
2247 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
2249 isl_pw_qpolynomial_free(pwqp
);
2254 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2256 struct isl_upoly_rec
*rec
;
2262 if (isl_upoly_is_cst(up
))
2266 active
[up
->var
] = 1;
2268 rec
= isl_upoly_as_rec(up
);
2269 for (i
= 0; i
< rec
->n
; ++i
)
2270 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2276 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2279 int d
= isl_dim_total(qp
->dim
);
2281 for (i
= 0; i
< d
; ++i
)
2282 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2283 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2289 return up_set_active(qp
->upoly
, active
, d
);
2292 /* For each parameter or variable that does not appear in qp,
2293 * first eliminate the variable from all constraints and then set it to zero.
2295 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
2296 __isl_keep isl_qpolynomial
*qp
)
2307 d
= isl_dim_total(set
->dim
);
2308 active
= isl_calloc_array(set
->ctx
, int, d
);
2309 if (set_active(qp
, active
) < 0)
2312 for (i
= 0; i
< d
; ++i
)
2321 nparam
= isl_dim_size(set
->dim
, isl_dim_param
);
2322 nvar
= isl_dim_size(set
->dim
, isl_dim_set
);
2323 for (i
= 0; i
< nparam
; ++i
) {
2326 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
2327 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
2329 for (i
= 0; i
< nvar
; ++i
) {
2332 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
2333 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
2345 struct isl_max_data
{
2346 isl_qpolynomial
*qp
;
2348 isl_qpolynomial
*max
;
2351 static int max_fn(__isl_take isl_point
*pnt
, void *user
)
2353 struct isl_max_data
*data
= (struct isl_max_data
*)user
;
2354 isl_qpolynomial
*val
;
2356 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
2361 data
->max
= qpolynomial_max(data
->max
, val
);
2367 static __isl_give isl_qpolynomial
*guarded_qpolynomial_max(
2368 __isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
)
2370 struct isl_max_data data
= { NULL
, 1, NULL
};
2375 if (isl_upoly_is_cst(qp
->upoly
)) {
2380 set
= fix_inactive(set
, qp
);
2383 if (isl_set_foreach_point(set
, max_fn
, &data
) < 0)
2387 isl_qpolynomial_free(qp
);
2391 isl_qpolynomial_free(qp
);
2392 isl_qpolynomial_free(data
.max
);
2396 /* Compute the maximal value attained by the piecewise quasipolynomial
2397 * on its domain or zero if the domain is empty.
2398 * In the worst case, the domain is scanned completely,
2399 * so the domain is assumed to be bounded.
2401 __isl_give isl_qpolynomial
*isl_pw_qpolynomial_max(
2402 __isl_take isl_pw_qpolynomial
*pwqp
)
2405 isl_qpolynomial
*max
;
2411 isl_dim
*dim
= isl_dim_copy(pwqp
->dim
);
2412 isl_pw_qpolynomial_free(pwqp
);
2413 return isl_qpolynomial_zero(dim
);
2416 max
= guarded_qpolynomial_max(isl_set_copy(pwqp
->p
[0].set
),
2417 isl_qpolynomial_copy(pwqp
->p
[0].qp
));
2418 for (i
= 1; i
< pwqp
->n
; ++i
) {
2419 isl_qpolynomial
*max_i
;
2420 max_i
= guarded_qpolynomial_max(isl_set_copy(pwqp
->p
[i
].set
),
2421 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
2422 max
= qpolynomial_max(max
, max_i
);
2425 isl_pw_qpolynomial_free(pwqp
);