2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT 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_ctx_private.h>
14 #include <isl_map_private.h>
15 #include <isl_factorization.h>
16 #include <isl_lp_private.h>
18 #include <isl_union_map_private.h>
19 #include <isl_constraint_private.h>
20 #include <isl_polynomial_private.h>
21 #include <isl_point_private.h>
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_range.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
30 #include <isl/deprecated/polynomial_int.h>
32 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
35 case isl_dim_param
: return 0;
36 case isl_dim_in
: return dim
->nparam
;
37 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
42 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
50 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
55 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
57 return (struct isl_upoly_cst
*)up
;
60 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
65 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
67 return (struct isl_upoly_rec
*)up
;
70 isl_bool
isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
71 __isl_keep
struct isl_upoly
*up2
)
74 struct isl_upoly_rec
*rec1
, *rec2
;
77 return isl_bool_error
;
80 if (up1
->var
!= up2
->var
)
81 return isl_bool_false
;
82 if (isl_upoly_is_cst(up1
)) {
83 struct isl_upoly_cst
*cst1
, *cst2
;
84 cst1
= isl_upoly_as_cst(up1
);
85 cst2
= isl_upoly_as_cst(up2
);
87 return isl_bool_error
;
88 return isl_int_eq(cst1
->n
, cst2
->n
) &&
89 isl_int_eq(cst1
->d
, cst2
->d
);
92 rec1
= isl_upoly_as_rec(up1
);
93 rec2
= isl_upoly_as_rec(up2
);
95 return isl_bool_error
;
97 if (rec1
->n
!= rec2
->n
)
98 return isl_bool_false
;
100 for (i
= 0; i
< rec1
->n
; ++i
) {
101 isl_bool eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
106 return isl_bool_true
;
109 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
111 struct isl_upoly_cst
*cst
;
115 if (!isl_upoly_is_cst(up
))
118 cst
= isl_upoly_as_cst(up
);
122 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
125 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
127 struct isl_upoly_cst
*cst
;
131 if (!isl_upoly_is_cst(up
))
134 cst
= isl_upoly_as_cst(up
);
138 return isl_int_sgn(cst
->n
);
141 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
143 struct isl_upoly_cst
*cst
;
147 if (!isl_upoly_is_cst(up
))
150 cst
= isl_upoly_as_cst(up
);
154 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
157 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
159 struct isl_upoly_cst
*cst
;
163 if (!isl_upoly_is_cst(up
))
166 cst
= isl_upoly_as_cst(up
);
170 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
173 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
175 struct isl_upoly_cst
*cst
;
179 if (!isl_upoly_is_cst(up
))
182 cst
= isl_upoly_as_cst(up
);
186 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
189 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
191 struct isl_upoly_cst
*cst
;
195 if (!isl_upoly_is_cst(up
))
198 cst
= isl_upoly_as_cst(up
);
202 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
205 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
207 struct isl_upoly_cst
*cst
;
211 if (!isl_upoly_is_cst(up
))
214 cst
= isl_upoly_as_cst(up
);
218 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
221 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
223 struct isl_upoly_cst
*cst
;
225 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
234 isl_int_init(cst
->n
);
235 isl_int_init(cst
->d
);
240 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
242 struct isl_upoly_cst
*cst
;
244 cst
= isl_upoly_cst_alloc(ctx
);
248 isl_int_set_si(cst
->n
, 0);
249 isl_int_set_si(cst
->d
, 1);
254 __isl_give
struct isl_upoly
*isl_upoly_one(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
, 1);
268 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
270 struct isl_upoly_cst
*cst
;
272 cst
= isl_upoly_cst_alloc(ctx
);
276 isl_int_set_si(cst
->n
, 1);
277 isl_int_set_si(cst
->d
, 0);
282 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
284 struct isl_upoly_cst
*cst
;
286 cst
= isl_upoly_cst_alloc(ctx
);
290 isl_int_set_si(cst
->n
, -1);
291 isl_int_set_si(cst
->d
, 0);
296 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
298 struct isl_upoly_cst
*cst
;
300 cst
= isl_upoly_cst_alloc(ctx
);
304 isl_int_set_si(cst
->n
, 0);
305 isl_int_set_si(cst
->d
, 0);
310 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
311 isl_int n
, isl_int d
)
313 struct isl_upoly_cst
*cst
;
315 cst
= isl_upoly_cst_alloc(ctx
);
319 isl_int_set(cst
->n
, n
);
320 isl_int_set(cst
->d
, d
);
325 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
328 struct isl_upoly_rec
*rec
;
330 isl_assert(ctx
, var
>= 0, return NULL
);
331 isl_assert(ctx
, size
>= 0, return NULL
);
332 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
333 sizeof(struct isl_upoly_rec
) +
334 size
* sizeof(struct isl_upoly
*));
349 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
350 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
352 qp
= isl_qpolynomial_cow(qp
);
356 isl_space_free(qp
->dim
);
361 isl_qpolynomial_free(qp
);
366 /* Reset the space of "qp". This function is called from isl_pw_templ.c
367 * and doesn't know if the space of an element object is represented
368 * directly or through its domain. It therefore passes along both.
370 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
371 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
372 __isl_take isl_space
*domain
)
374 isl_space_free(space
);
375 return isl_qpolynomial_reset_domain_space(qp
, domain
);
378 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
380 return qp
? qp
->dim
->ctx
: NULL
;
383 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
384 __isl_keep isl_qpolynomial
*qp
)
386 return qp
? isl_space_copy(qp
->dim
) : NULL
;
389 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
394 space
= isl_space_copy(qp
->dim
);
395 space
= isl_space_from_domain(space
);
396 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
400 /* Externally, an isl_qpolynomial has a map space, but internally, the
401 * ls field corresponds to the domain of that space.
403 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
404 enum isl_dim_type type
)
408 if (type
== isl_dim_out
)
410 if (type
== isl_dim_in
)
412 return isl_space_dim(qp
->dim
, type
);
415 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
417 return qp
? isl_upoly_is_zero(qp
->upoly
) : isl_bool_error
;
420 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
422 return qp
? isl_upoly_is_one(qp
->upoly
) : isl_bool_error
;
425 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
427 return qp
? isl_upoly_is_nan(qp
->upoly
) : isl_bool_error
;
430 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
432 return qp
? isl_upoly_is_infty(qp
->upoly
) : isl_bool_error
;
435 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
437 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : isl_bool_error
;
440 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
442 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
445 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
447 isl_int_clear(cst
->n
);
448 isl_int_clear(cst
->d
);
451 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
455 for (i
= 0; i
< rec
->n
; ++i
)
456 isl_upoly_free(rec
->p
[i
]);
459 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
468 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
470 struct isl_upoly_cst
*cst
;
471 struct isl_upoly_cst
*dup
;
473 cst
= isl_upoly_as_cst(up
);
477 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
480 isl_int_set(dup
->n
, cst
->n
);
481 isl_int_set(dup
->d
, cst
->d
);
486 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
489 struct isl_upoly_rec
*rec
;
490 struct isl_upoly_rec
*dup
;
492 rec
= isl_upoly_as_rec(up
);
496 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
500 for (i
= 0; i
< rec
->n
; ++i
) {
501 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
509 isl_upoly_free(&dup
->up
);
513 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
518 if (isl_upoly_is_cst(up
))
519 return isl_upoly_dup_cst(up
);
521 return isl_upoly_dup_rec(up
);
524 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
532 return isl_upoly_dup(up
);
535 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
544 upoly_free_cst((struct isl_upoly_cst
*)up
);
546 upoly_free_rec((struct isl_upoly_rec
*)up
);
548 isl_ctx_deref(up
->ctx
);
552 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
557 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
558 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
559 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
560 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
565 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
566 __isl_take
struct isl_upoly
*up2
)
568 struct isl_upoly_cst
*cst1
;
569 struct isl_upoly_cst
*cst2
;
571 up1
= isl_upoly_cow(up1
);
575 cst1
= isl_upoly_as_cst(up1
);
576 cst2
= isl_upoly_as_cst(up2
);
578 if (isl_int_eq(cst1
->d
, cst2
->d
))
579 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
581 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
582 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
583 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
586 isl_upoly_cst_reduce(cst1
);
596 static __isl_give
struct isl_upoly
*replace_by_zero(
597 __isl_take
struct isl_upoly
*up
)
605 return isl_upoly_zero(ctx
);
608 static __isl_give
struct isl_upoly
*replace_by_constant_term(
609 __isl_take
struct isl_upoly
*up
)
611 struct isl_upoly_rec
*rec
;
612 struct isl_upoly
*cst
;
617 rec
= isl_upoly_as_rec(up
);
620 cst
= isl_upoly_copy(rec
->p
[0]);
628 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
629 __isl_take
struct isl_upoly
*up2
)
632 struct isl_upoly_rec
*rec1
, *rec2
;
637 if (isl_upoly_is_nan(up1
)) {
642 if (isl_upoly_is_nan(up2
)) {
647 if (isl_upoly_is_zero(up1
)) {
652 if (isl_upoly_is_zero(up2
)) {
657 if (up1
->var
< up2
->var
)
658 return isl_upoly_sum(up2
, up1
);
660 if (up2
->var
< up1
->var
) {
661 struct isl_upoly_rec
*rec
;
662 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
666 up1
= isl_upoly_cow(up1
);
667 rec
= isl_upoly_as_rec(up1
);
670 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
672 up1
= replace_by_constant_term(up1
);
676 if (isl_upoly_is_cst(up1
))
677 return isl_upoly_sum_cst(up1
, up2
);
679 rec1
= isl_upoly_as_rec(up1
);
680 rec2
= isl_upoly_as_rec(up2
);
684 if (rec1
->n
< rec2
->n
)
685 return isl_upoly_sum(up2
, up1
);
687 up1
= isl_upoly_cow(up1
);
688 rec1
= isl_upoly_as_rec(up1
);
692 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
693 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
694 isl_upoly_copy(rec2
->p
[i
]));
697 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
698 isl_upoly_free(rec1
->p
[i
]);
704 up1
= replace_by_zero(up1
);
705 else if (rec1
->n
== 1)
706 up1
= replace_by_constant_term(up1
);
717 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
718 __isl_take
struct isl_upoly
*up
, isl_int v
)
720 struct isl_upoly_cst
*cst
;
722 up
= isl_upoly_cow(up
);
726 cst
= isl_upoly_as_cst(up
);
728 isl_int_addmul(cst
->n
, cst
->d
, v
);
733 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
734 __isl_take
struct isl_upoly
*up
, isl_int v
)
736 struct isl_upoly_rec
*rec
;
741 if (isl_upoly_is_cst(up
))
742 return isl_upoly_cst_add_isl_int(up
, v
);
744 up
= isl_upoly_cow(up
);
745 rec
= isl_upoly_as_rec(up
);
749 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
759 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
760 __isl_take
struct isl_upoly
*up
, isl_int v
)
762 struct isl_upoly_cst
*cst
;
764 if (isl_upoly_is_zero(up
))
767 up
= isl_upoly_cow(up
);
771 cst
= isl_upoly_as_cst(up
);
773 isl_int_mul(cst
->n
, cst
->n
, v
);
778 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
779 __isl_take
struct isl_upoly
*up
, isl_int v
)
782 struct isl_upoly_rec
*rec
;
787 if (isl_upoly_is_cst(up
))
788 return isl_upoly_cst_mul_isl_int(up
, v
);
790 up
= isl_upoly_cow(up
);
791 rec
= isl_upoly_as_rec(up
);
795 for (i
= 0; i
< rec
->n
; ++i
) {
796 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
807 /* Multiply the constant polynomial "up" by "v".
809 static __isl_give
struct isl_upoly
*isl_upoly_cst_scale_val(
810 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
812 struct isl_upoly_cst
*cst
;
814 if (isl_upoly_is_zero(up
))
817 up
= isl_upoly_cow(up
);
821 cst
= isl_upoly_as_cst(up
);
823 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
824 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
825 isl_upoly_cst_reduce(cst
);
830 /* Multiply the polynomial "up" by "v".
832 static __isl_give
struct isl_upoly
*isl_upoly_scale_val(
833 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
836 struct isl_upoly_rec
*rec
;
841 if (isl_upoly_is_cst(up
))
842 return isl_upoly_cst_scale_val(up
, v
);
844 up
= isl_upoly_cow(up
);
845 rec
= isl_upoly_as_rec(up
);
849 for (i
= 0; i
< rec
->n
; ++i
) {
850 rec
->p
[i
] = isl_upoly_scale_val(rec
->p
[i
], v
);
861 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
862 __isl_take
struct isl_upoly
*up2
)
864 struct isl_upoly_cst
*cst1
;
865 struct isl_upoly_cst
*cst2
;
867 up1
= isl_upoly_cow(up1
);
871 cst1
= isl_upoly_as_cst(up1
);
872 cst2
= isl_upoly_as_cst(up2
);
874 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
875 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
877 isl_upoly_cst_reduce(cst1
);
887 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
888 __isl_take
struct isl_upoly
*up2
)
890 struct isl_upoly_rec
*rec1
;
891 struct isl_upoly_rec
*rec2
;
892 struct isl_upoly_rec
*res
= NULL
;
896 rec1
= isl_upoly_as_rec(up1
);
897 rec2
= isl_upoly_as_rec(up2
);
900 size
= rec1
->n
+ rec2
->n
- 1;
901 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
905 for (i
= 0; i
< rec1
->n
; ++i
) {
906 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
907 isl_upoly_copy(rec1
->p
[i
]));
912 for (; i
< size
; ++i
) {
913 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
918 for (i
= 0; i
< rec1
->n
; ++i
) {
919 for (j
= 1; j
< rec2
->n
; ++j
) {
920 struct isl_upoly
*up
;
921 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
922 isl_upoly_copy(rec1
->p
[i
]));
923 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
936 isl_upoly_free(&res
->up
);
940 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
941 __isl_take
struct isl_upoly
*up2
)
946 if (isl_upoly_is_nan(up1
)) {
951 if (isl_upoly_is_nan(up2
)) {
956 if (isl_upoly_is_zero(up1
)) {
961 if (isl_upoly_is_zero(up2
)) {
966 if (isl_upoly_is_one(up1
)) {
971 if (isl_upoly_is_one(up2
)) {
976 if (up1
->var
< up2
->var
)
977 return isl_upoly_mul(up2
, up1
);
979 if (up2
->var
< up1
->var
) {
981 struct isl_upoly_rec
*rec
;
982 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
983 isl_ctx
*ctx
= up1
->ctx
;
986 return isl_upoly_nan(ctx
);
988 up1
= isl_upoly_cow(up1
);
989 rec
= isl_upoly_as_rec(up1
);
993 for (i
= 0; i
< rec
->n
; ++i
) {
994 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
995 isl_upoly_copy(up2
));
1003 if (isl_upoly_is_cst(up1
))
1004 return isl_upoly_mul_cst(up1
, up2
);
1006 return isl_upoly_mul_rec(up1
, up2
);
1008 isl_upoly_free(up1
);
1009 isl_upoly_free(up2
);
1013 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
1016 struct isl_upoly
*res
;
1024 res
= isl_upoly_copy(up
);
1026 res
= isl_upoly_one(up
->ctx
);
1028 while (power
>>= 1) {
1029 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
1031 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
1038 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*dim
,
1039 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1041 struct isl_qpolynomial
*qp
= NULL
;
1047 if (!isl_space_is_set(dim
))
1048 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1049 "domain of polynomial should be a set", goto error
);
1051 total
= isl_space_dim(dim
, isl_dim_all
);
1053 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
1058 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1067 isl_space_free(dim
);
1069 isl_qpolynomial_free(qp
);
1073 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1082 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1084 struct isl_qpolynomial
*dup
;
1089 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1090 isl_upoly_copy(qp
->upoly
));
1093 isl_mat_free(dup
->div
);
1094 dup
->div
= isl_mat_copy(qp
->div
);
1100 isl_qpolynomial_free(dup
);
1104 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1112 return isl_qpolynomial_dup(qp
);
1115 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1116 __isl_take isl_qpolynomial
*qp
)
1124 isl_space_free(qp
->dim
);
1125 isl_mat_free(qp
->div
);
1126 isl_upoly_free(qp
->upoly
);
1132 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1135 struct isl_upoly_rec
*rec
;
1136 struct isl_upoly_cst
*cst
;
1138 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1141 for (i
= 0; i
< 1 + power
; ++i
) {
1142 rec
->p
[i
] = isl_upoly_zero(ctx
);
1147 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1148 isl_int_set_si(cst
->n
, 1);
1152 isl_upoly_free(&rec
->up
);
1156 /* r array maps original positions to new positions.
1158 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1162 struct isl_upoly_rec
*rec
;
1163 struct isl_upoly
*base
;
1164 struct isl_upoly
*res
;
1166 if (isl_upoly_is_cst(up
))
1169 rec
= isl_upoly_as_rec(up
);
1173 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1175 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1176 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1178 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1179 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1180 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1183 isl_upoly_free(base
);
1192 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
1197 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1198 div1
->n_col
>= div2
->n_col
, return -1);
1200 if (div1
->n_row
== div2
->n_row
)
1201 return isl_mat_is_equal(div1
, div2
);
1203 n_row
= div1
->n_row
;
1204 n_col
= div1
->n_col
;
1205 div1
->n_row
= div2
->n_row
;
1206 div1
->n_col
= div2
->n_col
;
1208 equal
= isl_mat_is_equal(div1
, div2
);
1210 div1
->n_row
= n_row
;
1211 div1
->n_col
= n_col
;
1216 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1220 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1221 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1226 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1229 struct isl_div_sort_info
{
1234 static int div_sort_cmp(const void *p1
, const void *p2
)
1236 const struct isl_div_sort_info
*i1
, *i2
;
1237 i1
= (const struct isl_div_sort_info
*) p1
;
1238 i2
= (const struct isl_div_sort_info
*) p2
;
1240 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1243 /* Sort divs and remove duplicates.
1245 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1250 struct isl_div_sort_info
*array
= NULL
;
1251 int *pos
= NULL
, *at
= NULL
;
1252 int *reordering
= NULL
;
1257 if (qp
->div
->n_row
<= 1)
1260 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1262 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1264 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1265 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1266 len
= qp
->div
->n_col
- 2;
1267 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1268 if (!array
|| !pos
|| !at
|| !reordering
)
1271 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1272 array
[i
].div
= qp
->div
;
1278 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1281 for (i
= 0; i
< div_pos
; ++i
)
1284 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1285 if (pos
[array
[i
].row
] == i
)
1287 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1288 pos
[at
[i
]] = pos
[array
[i
].row
];
1289 at
[pos
[array
[i
].row
]] = at
[i
];
1290 at
[i
] = array
[i
].row
;
1291 pos
[array
[i
].row
] = i
;
1295 for (i
= 0; i
< len
- div_pos
; ++i
) {
1297 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1298 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1299 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1300 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1301 2 + div_pos
+ i
- skip
);
1302 qp
->div
= isl_mat_drop_cols(qp
->div
,
1303 2 + div_pos
+ i
- skip
, 1);
1306 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1309 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1311 if (!qp
->upoly
|| !qp
->div
)
1325 isl_qpolynomial_free(qp
);
1329 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1330 int *exp
, int first
)
1333 struct isl_upoly_rec
*rec
;
1335 if (isl_upoly_is_cst(up
))
1338 if (up
->var
< first
)
1341 if (exp
[up
->var
- first
] == up
->var
- first
)
1344 up
= isl_upoly_cow(up
);
1348 up
->var
= exp
[up
->var
- first
] + first
;
1350 rec
= isl_upoly_as_rec(up
);
1354 for (i
= 0; i
< rec
->n
; ++i
) {
1355 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1366 static __isl_give isl_qpolynomial
*with_merged_divs(
1367 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1368 __isl_take isl_qpolynomial
*qp2
),
1369 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1373 isl_mat
*div
= NULL
;
1376 qp1
= isl_qpolynomial_cow(qp1
);
1377 qp2
= isl_qpolynomial_cow(qp2
);
1382 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1383 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1385 n_div1
= qp1
->div
->n_row
;
1386 n_div2
= qp2
->div
->n_row
;
1387 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1388 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1389 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1392 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1396 isl_mat_free(qp1
->div
);
1397 qp1
->div
= isl_mat_copy(div
);
1398 isl_mat_free(qp2
->div
);
1399 qp2
->div
= isl_mat_copy(div
);
1401 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1402 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1404 if (!qp1
->upoly
|| !qp2
->upoly
)
1411 return fn(qp1
, qp2
);
1416 isl_qpolynomial_free(qp1
);
1417 isl_qpolynomial_free(qp2
);
1421 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1422 __isl_take isl_qpolynomial
*qp2
)
1424 qp1
= isl_qpolynomial_cow(qp1
);
1429 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1430 return isl_qpolynomial_add(qp2
, qp1
);
1432 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1433 if (!compatible_divs(qp1
->div
, qp2
->div
))
1434 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1436 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1440 isl_qpolynomial_free(qp2
);
1444 isl_qpolynomial_free(qp1
);
1445 isl_qpolynomial_free(qp2
);
1449 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1450 __isl_keep isl_set
*dom
,
1451 __isl_take isl_qpolynomial
*qp1
,
1452 __isl_take isl_qpolynomial
*qp2
)
1454 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1455 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1459 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1460 __isl_take isl_qpolynomial
*qp2
)
1462 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1465 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1466 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1468 if (isl_int_is_zero(v
))
1471 qp
= isl_qpolynomial_cow(qp
);
1475 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1481 isl_qpolynomial_free(qp
);
1486 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1491 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1494 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1495 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1497 if (isl_int_is_one(v
))
1500 if (qp
&& isl_int_is_zero(v
)) {
1501 isl_qpolynomial
*zero
;
1502 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1503 isl_qpolynomial_free(qp
);
1507 qp
= isl_qpolynomial_cow(qp
);
1511 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1517 isl_qpolynomial_free(qp
);
1521 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1522 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1524 return isl_qpolynomial_mul_isl_int(qp
, v
);
1527 /* Multiply "qp" by "v".
1529 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1530 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1535 if (!isl_val_is_rat(v
))
1536 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1537 "expecting rational factor", goto error
);
1539 if (isl_val_is_one(v
)) {
1544 if (isl_val_is_zero(v
)) {
1547 space
= isl_qpolynomial_get_domain_space(qp
);
1548 isl_qpolynomial_free(qp
);
1550 return isl_qpolynomial_zero_on_domain(space
);
1553 qp
= isl_qpolynomial_cow(qp
);
1557 qp
->upoly
= isl_upoly_scale_val(qp
->upoly
, v
);
1559 qp
= isl_qpolynomial_free(qp
);
1565 isl_qpolynomial_free(qp
);
1569 /* Divide "qp" by "v".
1571 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1572 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1577 if (!isl_val_is_rat(v
))
1578 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1579 "expecting rational factor", goto error
);
1580 if (isl_val_is_zero(v
))
1581 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1582 "cannot scale down by zero", goto error
);
1584 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1587 isl_qpolynomial_free(qp
);
1591 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1592 __isl_take isl_qpolynomial
*qp2
)
1594 qp1
= isl_qpolynomial_cow(qp1
);
1599 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1600 return isl_qpolynomial_mul(qp2
, qp1
);
1602 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1603 if (!compatible_divs(qp1
->div
, qp2
->div
))
1604 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1606 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1610 isl_qpolynomial_free(qp2
);
1614 isl_qpolynomial_free(qp1
);
1615 isl_qpolynomial_free(qp2
);
1619 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1622 qp
= isl_qpolynomial_cow(qp
);
1627 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1633 isl_qpolynomial_free(qp
);
1637 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1638 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1645 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1649 for (i
= 0; i
< pwqp
->n
; ++i
) {
1650 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1652 return isl_pw_qpolynomial_free(pwqp
);
1658 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1659 __isl_take isl_space
*dim
)
1663 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1666 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1667 __isl_take isl_space
*dim
)
1671 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1674 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1675 __isl_take isl_space
*dim
)
1679 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1682 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1683 __isl_take isl_space
*dim
)
1687 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1690 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1691 __isl_take isl_space
*dim
)
1695 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1698 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1699 __isl_take isl_space
*dim
,
1702 struct isl_qpolynomial
*qp
;
1703 struct isl_upoly_cst
*cst
;
1708 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1712 cst
= isl_upoly_as_cst(qp
->upoly
);
1713 isl_int_set(cst
->n
, v
);
1718 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1719 isl_int
*n
, isl_int
*d
)
1721 struct isl_upoly_cst
*cst
;
1726 if (!isl_upoly_is_cst(qp
->upoly
))
1729 cst
= isl_upoly_as_cst(qp
->upoly
);
1734 isl_int_set(*n
, cst
->n
);
1736 isl_int_set(*d
, cst
->d
);
1741 /* Return the constant term of "up".
1743 static __isl_give isl_val
*isl_upoly_get_constant_val(
1744 __isl_keep
struct isl_upoly
*up
)
1746 struct isl_upoly_cst
*cst
;
1751 while (!isl_upoly_is_cst(up
)) {
1752 struct isl_upoly_rec
*rec
;
1754 rec
= isl_upoly_as_rec(up
);
1760 cst
= isl_upoly_as_cst(up
);
1763 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1766 /* Return the constant term of "qp".
1768 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1769 __isl_keep isl_qpolynomial
*qp
)
1774 return isl_upoly_get_constant_val(qp
->upoly
);
1777 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1780 struct isl_upoly_rec
*rec
;
1788 rec
= isl_upoly_as_rec(up
);
1795 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1797 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1803 return isl_upoly_is_affine(rec
->p
[0]);
1806 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1811 if (qp
->div
->n_row
> 0)
1814 return isl_upoly_is_affine(qp
->upoly
);
1817 static void update_coeff(__isl_keep isl_vec
*aff
,
1818 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1823 if (isl_int_is_zero(cst
->n
))
1828 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1829 isl_int_divexact(f
, cst
->d
, gcd
);
1830 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1831 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1832 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1837 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1838 __isl_keep isl_vec
*aff
)
1840 struct isl_upoly_cst
*cst
;
1841 struct isl_upoly_rec
*rec
;
1847 struct isl_upoly_cst
*cst
;
1849 cst
= isl_upoly_as_cst(up
);
1852 update_coeff(aff
, cst
, 0);
1856 rec
= isl_upoly_as_rec(up
);
1859 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1861 cst
= isl_upoly_as_cst(rec
->p
[1]);
1864 update_coeff(aff
, cst
, 1 + up
->var
);
1866 return isl_upoly_update_affine(rec
->p
[0], aff
);
1869 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1870 __isl_keep isl_qpolynomial
*qp
)
1878 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1879 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1883 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1884 isl_int_set_si(aff
->el
[0], 1);
1886 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1895 /* Is "qp1" obviously equal to "qp2"?
1897 * NaN is not equal to anything, not even to another NaN.
1899 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1900 __isl_keep isl_qpolynomial
*qp2
)
1905 return isl_bool_error
;
1907 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
1908 return isl_bool_false
;
1910 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
1911 if (equal
< 0 || !equal
)
1914 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
1915 if (equal
< 0 || !equal
)
1918 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1921 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1924 struct isl_upoly_rec
*rec
;
1926 if (isl_upoly_is_cst(up
)) {
1927 struct isl_upoly_cst
*cst
;
1928 cst
= isl_upoly_as_cst(up
);
1931 isl_int_lcm(*d
, *d
, cst
->d
);
1935 rec
= isl_upoly_as_rec(up
);
1939 for (i
= 0; i
< rec
->n
; ++i
)
1940 upoly_update_den(rec
->p
[i
], d
);
1943 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1945 isl_int_set_si(*d
, 1);
1948 upoly_update_den(qp
->upoly
, d
);
1951 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
1952 __isl_take isl_space
*dim
, int pos
, int power
)
1954 struct isl_ctx
*ctx
;
1961 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
1964 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(__isl_take isl_space
*dim
,
1965 enum isl_dim_type type
, unsigned pos
)
1970 isl_assert(dim
->ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, goto error
);
1971 isl_assert(dim
->ctx
, pos
< isl_space_dim(dim
, type
), goto error
);
1973 if (type
== isl_dim_set
)
1974 pos
+= isl_space_dim(dim
, isl_dim_param
);
1976 return isl_qpolynomial_var_pow_on_domain(dim
, pos
, 1);
1978 isl_space_free(dim
);
1982 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
1983 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
1986 struct isl_upoly_rec
*rec
;
1987 struct isl_upoly
*base
, *res
;
1992 if (isl_upoly_is_cst(up
))
1995 if (up
->var
< first
)
1998 rec
= isl_upoly_as_rec(up
);
2002 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2004 if (up
->var
>= first
+ n
)
2005 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2007 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2009 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2010 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2011 struct isl_upoly
*t
;
2012 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2013 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2014 res
= isl_upoly_sum(res
, t
);
2017 isl_upoly_free(base
);
2026 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2027 isl_int denom
, unsigned len
)
2030 struct isl_upoly
*up
;
2032 isl_assert(ctx
, len
>= 1, return NULL
);
2034 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2035 for (i
= 0; i
< len
- 1; ++i
) {
2036 struct isl_upoly
*t
;
2037 struct isl_upoly
*c
;
2039 if (isl_int_is_zero(f
[1 + i
]))
2042 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2043 t
= isl_upoly_var_pow(ctx
, i
, 1);
2044 t
= isl_upoly_mul(c
, t
);
2045 up
= isl_upoly_sum(up
, t
);
2051 /* Remove common factor of non-constant terms and denominator.
2053 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2055 isl_ctx
*ctx
= qp
->div
->ctx
;
2056 unsigned total
= qp
->div
->n_col
- 2;
2058 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2059 isl_int_gcd(ctx
->normalize_gcd
,
2060 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2061 if (isl_int_is_one(ctx
->normalize_gcd
))
2064 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2065 ctx
->normalize_gcd
, total
);
2066 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2067 ctx
->normalize_gcd
);
2068 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2069 ctx
->normalize_gcd
);
2072 /* Replace the integer division identified by "div" by the polynomial "s".
2073 * The integer division is assumed not to appear in the definition
2074 * of any other integer divisions.
2076 static __isl_give isl_qpolynomial
*substitute_div(
2077 __isl_take isl_qpolynomial
*qp
,
2078 int div
, __isl_take
struct isl_upoly
*s
)
2087 qp
= isl_qpolynomial_cow(qp
);
2091 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2092 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2096 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2099 for (i
= 0; i
< total
+ div
; ++i
)
2101 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2102 reordering
[i
] = i
- 1;
2103 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2104 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2105 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2108 if (!qp
->upoly
|| !qp
->div
)
2114 isl_qpolynomial_free(qp
);
2119 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2120 * divisions because d is equal to 1 by their definition, i.e., e.
2122 static __isl_give isl_qpolynomial
*substitute_non_divs(
2123 __isl_take isl_qpolynomial
*qp
)
2127 struct isl_upoly
*s
;
2132 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2133 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2134 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2136 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2137 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2139 isl_seq_combine(qp
->div
->row
[j
] + 1,
2140 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2141 qp
->div
->row
[j
][2 + total
+ i
],
2142 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2143 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2144 normalize_div(qp
, j
);
2146 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2147 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2148 qp
= substitute_div(qp
, i
, s
);
2155 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2156 * with d the denominator. When replacing the coefficient e of x by
2157 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2158 * inside the division, so we need to add floor(e/d) * x outside.
2159 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2160 * to adjust the coefficient of x in each later div that depends on the
2161 * current div "div" and also in the affine expression "aff"
2162 * (if it too depends on "div").
2164 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2165 __isl_keep isl_vec
*aff
)
2169 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2172 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2173 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2174 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2176 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2177 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2178 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2179 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2180 isl_int_addmul(aff
->el
[i
], v
, aff
->el
[1 + total
+ div
]);
2181 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2182 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2184 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2185 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2191 /* Check if the last non-zero coefficient is bigger that half of the
2192 * denominator. If so, we will invert the div to further reduce the number
2193 * of distinct divs that may appear.
2194 * If the last non-zero coefficient is exactly half the denominator,
2195 * then we continue looking for earlier coefficients that are bigger
2196 * than half the denominator.
2198 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2203 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2204 if (isl_int_is_zero(div
->row
[row
][i
]))
2206 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2207 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2208 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2218 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2219 * We only invert the coefficients of e (and the coefficient of q in
2220 * later divs and in "aff"). After calling this function, the
2221 * coefficients of e should be reduced again.
2223 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2224 __isl_keep isl_vec
*aff
)
2226 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2228 isl_seq_neg(qp
->div
->row
[div
] + 1,
2229 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2230 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2231 isl_int_add(qp
->div
->row
[div
][1],
2232 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2233 if (!isl_int_is_zero(aff
->el
[1 + total
+ div
]))
2234 isl_int_neg(aff
->el
[1 + total
+ div
], aff
->el
[1 + total
+ div
]);
2235 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2236 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2239 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2240 * in the interval [0, d-1], with d the denominator and such that the
2241 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2243 * After the reduction, some divs may have become redundant or identical,
2244 * so we call substitute_non_divs and sort_divs. If these functions
2245 * eliminate divs or merge two or more divs into one, the coefficients
2246 * of the enclosing divs may have to be reduced again, so we call
2247 * ourselves recursively if the number of divs decreases.
2249 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2252 isl_vec
*aff
= NULL
;
2253 struct isl_upoly
*s
;
2259 aff
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
2260 aff
= isl_vec_clr(aff
);
2264 isl_int_set_si(aff
->el
[1 + qp
->upoly
->var
], 1);
2266 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2267 normalize_div(qp
, i
);
2268 reduce_div(qp
, i
, aff
);
2269 if (needs_invert(qp
->div
, i
)) {
2270 invert_div(qp
, i
, aff
);
2271 reduce_div(qp
, i
, aff
);
2275 s
= isl_upoly_from_affine(qp
->div
->ctx
, aff
->el
,
2276 qp
->div
->ctx
->one
, aff
->size
);
2277 qp
->upoly
= isl_upoly_subs(qp
->upoly
, qp
->upoly
->var
, 1, &s
);
2284 n_div
= qp
->div
->n_row
;
2285 qp
= substitute_non_divs(qp
);
2287 if (qp
&& qp
->div
->n_row
< n_div
)
2288 return reduce_divs(qp
);
2292 isl_qpolynomial_free(qp
);
2297 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2298 __isl_take isl_space
*dim
, const isl_int n
, const isl_int d
)
2300 struct isl_qpolynomial
*qp
;
2301 struct isl_upoly_cst
*cst
;
2306 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2310 cst
= isl_upoly_as_cst(qp
->upoly
);
2311 isl_int_set(cst
->n
, n
);
2312 isl_int_set(cst
->d
, d
);
2317 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2319 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2320 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2322 isl_qpolynomial
*qp
;
2323 struct isl_upoly_cst
*cst
;
2325 if (!domain
|| !val
)
2328 qp
= isl_qpolynomial_alloc(isl_space_copy(domain
), 0,
2329 isl_upoly_zero(domain
->ctx
));
2333 cst
= isl_upoly_as_cst(qp
->upoly
);
2334 isl_int_set(cst
->n
, val
->n
);
2335 isl_int_set(cst
->d
, val
->d
);
2337 isl_space_free(domain
);
2341 isl_space_free(domain
);
2346 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2348 struct isl_upoly_rec
*rec
;
2354 if (isl_upoly_is_cst(up
))
2358 active
[up
->var
] = 1;
2360 rec
= isl_upoly_as_rec(up
);
2361 for (i
= 0; i
< rec
->n
; ++i
)
2362 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2368 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2371 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2376 for (i
= 0; i
< d
; ++i
)
2377 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2378 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2384 return up_set_active(qp
->upoly
, active
, d
);
2387 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2388 enum isl_dim_type type
, unsigned first
, unsigned n
)
2392 isl_bool involves
= isl_bool_false
;
2395 return isl_bool_error
;
2397 return isl_bool_false
;
2399 isl_assert(qp
->dim
->ctx
,
2400 first
+ n
<= isl_qpolynomial_dim(qp
, type
),
2401 return isl_bool_error
);
2402 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2403 type
== isl_dim_in
, return isl_bool_error
);
2405 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2406 isl_space_dim(qp
->dim
, isl_dim_all
));
2407 if (set_active(qp
, active
) < 0)
2410 if (type
== isl_dim_in
)
2411 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2412 for (i
= 0; i
< n
; ++i
)
2413 if (active
[first
+ i
]) {
2414 involves
= isl_bool_true
;
2423 return isl_bool_error
;
2426 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2427 * of the divs that do appear in the quasi-polynomial.
2429 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2430 __isl_take isl_qpolynomial
*qp
)
2437 int *reordering
= NULL
;
2444 if (qp
->div
->n_row
== 0)
2447 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2448 len
= qp
->div
->n_col
- 2;
2449 ctx
= isl_qpolynomial_get_ctx(qp
);
2450 active
= isl_calloc_array(ctx
, int, len
);
2454 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2457 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2458 if (!active
[d
+ i
]) {
2462 for (j
= 0; j
< i
; ++j
) {
2463 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2475 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2479 for (i
= 0; i
< d
; ++i
)
2483 n_div
= qp
->div
->n_row
;
2484 for (i
= 0; i
< n_div
; ++i
) {
2485 if (!active
[d
+ i
]) {
2486 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2487 qp
->div
= isl_mat_drop_cols(qp
->div
,
2488 2 + d
+ i
- skip
, 1);
2491 reordering
[d
+ i
] = d
+ i
- skip
;
2494 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2496 if (!qp
->upoly
|| !qp
->div
)
2506 isl_qpolynomial_free(qp
);
2510 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2511 unsigned first
, unsigned n
)
2514 struct isl_upoly_rec
*rec
;
2518 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2520 if (up
->var
< first
+ n
) {
2521 up
= replace_by_constant_term(up
);
2522 return isl_upoly_drop(up
, first
, n
);
2524 up
= isl_upoly_cow(up
);
2528 rec
= isl_upoly_as_rec(up
);
2532 for (i
= 0; i
< rec
->n
; ++i
) {
2533 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2544 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2545 __isl_take isl_qpolynomial
*qp
,
2546 enum isl_dim_type type
, unsigned pos
, const char *s
)
2548 qp
= isl_qpolynomial_cow(qp
);
2551 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2556 isl_qpolynomial_free(qp
);
2560 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2561 __isl_take isl_qpolynomial
*qp
,
2562 enum isl_dim_type type
, unsigned first
, unsigned n
)
2566 if (type
== isl_dim_out
)
2567 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2568 "cannot drop output/set dimension",
2570 if (type
== isl_dim_in
)
2572 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2575 qp
= isl_qpolynomial_cow(qp
);
2579 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2581 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2582 type
== isl_dim_set
, goto error
);
2584 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2588 if (type
== isl_dim_set
)
2589 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2591 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2595 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2601 isl_qpolynomial_free(qp
);
2605 /* Project the domain of the quasi-polynomial onto its parameter space.
2606 * The quasi-polynomial may not involve any of the domain dimensions.
2608 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2609 __isl_take isl_qpolynomial
*qp
)
2615 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2616 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2618 return isl_qpolynomial_free(qp
);
2620 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2621 "polynomial involves some of the domain dimensions",
2622 return isl_qpolynomial_free(qp
));
2623 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2624 space
= isl_qpolynomial_get_domain_space(qp
);
2625 space
= isl_space_params(space
);
2626 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2630 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2631 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2637 struct isl_upoly
*up
;
2641 if (eq
->n_eq
== 0) {
2642 isl_basic_set_free(eq
);
2646 qp
= isl_qpolynomial_cow(qp
);
2649 qp
->div
= isl_mat_cow(qp
->div
);
2653 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2655 isl_int_init(denom
);
2656 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2657 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2658 if (j
< 0 || j
== 0 || j
>= total
)
2661 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2662 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2664 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2665 &qp
->div
->row
[k
][0]);
2666 normalize_div(qp
, k
);
2669 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2670 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2671 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2672 isl_int_set_si(eq
->eq
[i
][j
], 0);
2674 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2675 eq
->eq
[i
], denom
, total
);
2676 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2679 isl_int_clear(denom
);
2684 isl_basic_set_free(eq
);
2686 qp
= substitute_non_divs(qp
);
2691 isl_basic_set_free(eq
);
2692 isl_qpolynomial_free(qp
);
2696 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2698 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2699 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2703 if (qp
->div
->n_row
> 0)
2704 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2705 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2707 isl_basic_set_free(eq
);
2708 isl_qpolynomial_free(qp
);
2712 static __isl_give isl_basic_set
*add_div_constraints(
2713 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2721 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2724 total
= isl_basic_set_total_dim(bset
);
2725 for (i
= 0; i
< div
->n_row
; ++i
)
2726 if (isl_basic_set_add_div_constraints_var(bset
,
2727 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2734 isl_basic_set_free(bset
);
2738 /* Look for equalities among the variables shared by context and qp
2739 * and the integer divisions of qp, if any.
2740 * The equalities are then used to eliminate variables and/or integer
2741 * divisions from qp.
2743 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2744 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2750 if (qp
->div
->n_row
> 0) {
2751 isl_basic_set
*bset
;
2752 context
= isl_set_add_dims(context
, isl_dim_set
,
2754 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2755 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2756 context
= isl_set_intersect(context
,
2757 isl_set_from_basic_set(bset
));
2760 aff
= isl_set_affine_hull(context
);
2761 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2763 isl_qpolynomial_free(qp
);
2764 isl_set_free(context
);
2768 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2769 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2771 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2772 isl_set
*dom_context
= isl_set_universe(space
);
2773 dom_context
= isl_set_intersect_params(dom_context
, context
);
2774 return isl_qpolynomial_gist(qp
, dom_context
);
2777 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2778 __isl_take isl_qpolynomial
*qp
)
2784 if (isl_qpolynomial_is_zero(qp
)) {
2785 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2786 isl_qpolynomial_free(qp
);
2787 return isl_pw_qpolynomial_zero(dim
);
2790 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2791 return isl_pw_qpolynomial_alloc(dom
, qp
);
2795 #define PW isl_pw_qpolynomial
2797 #define EL isl_qpolynomial
2799 #define EL_IS_ZERO is_zero
2803 #define IS_ZERO is_zero
2806 #undef DEFAULT_IS_ZERO
2807 #define DEFAULT_IS_ZERO 1
2811 #include <isl_pw_templ.c>
2814 #define UNION isl_union_pw_qpolynomial
2816 #define PART isl_pw_qpolynomial
2818 #define PARTS pw_qpolynomial
2820 #include <isl_union_single.c>
2821 #include <isl_union_eval.c>
2822 #include <isl_union_neg.c>
2824 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2832 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2835 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2838 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2839 __isl_take isl_pw_qpolynomial
*pwqp1
,
2840 __isl_take isl_pw_qpolynomial
*pwqp2
)
2842 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2845 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2846 __isl_take isl_pw_qpolynomial
*pwqp1
,
2847 __isl_take isl_pw_qpolynomial
*pwqp2
)
2850 struct isl_pw_qpolynomial
*res
;
2852 if (!pwqp1
|| !pwqp2
)
2855 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
2858 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
2859 isl_pw_qpolynomial_free(pwqp2
);
2863 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2864 isl_pw_qpolynomial_free(pwqp1
);
2868 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2869 isl_pw_qpolynomial_free(pwqp1
);
2873 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
2874 isl_pw_qpolynomial_free(pwqp2
);
2878 n
= pwqp1
->n
* pwqp2
->n
;
2879 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
2881 for (i
= 0; i
< pwqp1
->n
; ++i
) {
2882 for (j
= 0; j
< pwqp2
->n
; ++j
) {
2883 struct isl_set
*common
;
2884 struct isl_qpolynomial
*prod
;
2885 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
2886 isl_set_copy(pwqp2
->p
[j
].set
));
2887 if (isl_set_plain_is_empty(common
)) {
2888 isl_set_free(common
);
2892 prod
= isl_qpolynomial_mul(
2893 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
2894 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
2896 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
2900 isl_pw_qpolynomial_free(pwqp1
);
2901 isl_pw_qpolynomial_free(pwqp2
);
2905 isl_pw_qpolynomial_free(pwqp1
);
2906 isl_pw_qpolynomial_free(pwqp2
);
2910 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
2911 __isl_take isl_vec
*vec
)
2914 struct isl_upoly_rec
*rec
;
2918 if (isl_upoly_is_cst(up
)) {
2920 res
= isl_upoly_get_constant_val(up
);
2925 rec
= isl_upoly_as_rec(up
);
2929 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2931 base
= isl_val_rat_from_isl_int(up
->ctx
,
2932 vec
->el
[1 + up
->var
], vec
->el
[0]);
2934 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
2937 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2938 res
= isl_val_mul(res
, isl_val_copy(base
));
2939 res
= isl_val_add(res
,
2940 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
2941 isl_vec_copy(vec
)));
2954 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
2955 __isl_take isl_point
*pnt
)
2962 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
2964 if (qp
->div
->n_row
== 0)
2965 ext
= isl_vec_copy(pnt
->vec
);
2968 unsigned dim
= isl_space_dim(qp
->dim
, isl_dim_all
);
2969 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
2973 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
2974 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2975 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
2976 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
2977 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
2978 qp
->div
->row
[i
][0]);
2982 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
2984 isl_qpolynomial_free(qp
);
2985 isl_point_free(pnt
);
2989 isl_qpolynomial_free(qp
);
2990 isl_point_free(pnt
);
2994 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
2995 __isl_keep
struct isl_upoly_cst
*cst2
)
3000 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3001 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3002 cmp
= isl_int_sgn(t
);
3007 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3008 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3009 unsigned first
, unsigned n
)
3017 if (type
== isl_dim_out
)
3018 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3019 "cannot insert output/set dimensions",
3021 if (type
== isl_dim_in
)
3023 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3026 qp
= isl_qpolynomial_cow(qp
);
3030 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
3033 g_pos
= pos(qp
->dim
, type
) + first
;
3035 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3039 total
= qp
->div
->n_col
- 2;
3040 if (total
> g_pos
) {
3042 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3045 for (i
= 0; i
< total
- g_pos
; ++i
)
3047 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3053 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3059 isl_qpolynomial_free(qp
);
3063 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3064 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3068 pos
= isl_qpolynomial_dim(qp
, type
);
3070 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3073 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3074 __isl_take isl_pw_qpolynomial
*pwqp
,
3075 enum isl_dim_type type
, unsigned n
)
3079 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3081 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3084 static int *reordering_move(isl_ctx
*ctx
,
3085 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3090 reordering
= isl_alloc_array(ctx
, int, len
);
3095 for (i
= 0; i
< dst
; ++i
)
3097 for (i
= 0; i
< n
; ++i
)
3098 reordering
[src
+ i
] = dst
+ i
;
3099 for (i
= 0; i
< src
- dst
; ++i
)
3100 reordering
[dst
+ i
] = dst
+ n
+ i
;
3101 for (i
= 0; i
< len
- src
- n
; ++i
)
3102 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3104 for (i
= 0; i
< src
; ++i
)
3106 for (i
= 0; i
< n
; ++i
)
3107 reordering
[src
+ i
] = dst
+ i
;
3108 for (i
= 0; i
< dst
- src
; ++i
)
3109 reordering
[src
+ n
+ i
] = src
+ i
;
3110 for (i
= 0; i
< len
- dst
- n
; ++i
)
3111 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3117 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3118 __isl_take isl_qpolynomial
*qp
,
3119 enum isl_dim_type dst_type
, unsigned dst_pos
,
3120 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3129 qp
= isl_qpolynomial_cow(qp
);
3133 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3134 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3135 "cannot move output/set dimension",
3137 if (dst_type
== isl_dim_in
)
3138 dst_type
= isl_dim_set
;
3139 if (src_type
== isl_dim_in
)
3140 src_type
= isl_dim_set
;
3142 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3145 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3146 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3147 if (dst_type
> src_type
)
3150 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3157 reordering
= reordering_move(qp
->dim
->ctx
,
3158 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3162 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3167 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3173 isl_qpolynomial_free(qp
);
3177 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_space
*dim
,
3178 isl_int
*f
, isl_int denom
)
3180 struct isl_upoly
*up
;
3182 dim
= isl_space_domain(dim
);
3186 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
,
3187 1 + isl_space_dim(dim
, isl_dim_all
));
3189 return isl_qpolynomial_alloc(dim
, 0, up
);
3192 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3195 struct isl_upoly
*up
;
3196 isl_qpolynomial
*qp
;
3201 ctx
= isl_aff_get_ctx(aff
);
3202 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3205 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3206 aff
->ls
->div
->n_row
, up
);
3210 isl_mat_free(qp
->div
);
3211 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3212 qp
->div
= isl_mat_cow(qp
->div
);
3217 qp
= reduce_divs(qp
);
3218 qp
= remove_redundant_divs(qp
);
3222 return isl_qpolynomial_free(qp
);
3225 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3226 __isl_take isl_pw_aff
*pwaff
)
3229 isl_pw_qpolynomial
*pwqp
;
3234 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3237 for (i
= 0; i
< pwaff
->n
; ++i
) {
3239 isl_qpolynomial
*qp
;
3241 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3242 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3243 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3246 isl_pw_aff_free(pwaff
);
3250 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3251 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3255 aff
= isl_constraint_get_bound(c
, type
, pos
);
3256 isl_constraint_free(c
);
3257 return isl_qpolynomial_from_aff(aff
);
3260 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3261 * in "qp" by subs[i].
3263 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3264 __isl_take isl_qpolynomial
*qp
,
3265 enum isl_dim_type type
, unsigned first
, unsigned n
,
3266 __isl_keep isl_qpolynomial
**subs
)
3269 struct isl_upoly
**ups
;
3274 qp
= isl_qpolynomial_cow(qp
);
3278 if (type
== isl_dim_out
)
3279 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3280 "cannot substitute output/set dimension",
3282 if (type
== isl_dim_in
)
3285 for (i
= 0; i
< n
; ++i
)
3289 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3292 for (i
= 0; i
< n
; ++i
)
3293 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3296 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3297 for (i
= 0; i
< n
; ++i
)
3298 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3300 first
+= pos(qp
->dim
, type
);
3302 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3305 for (i
= 0; i
< n
; ++i
)
3306 ups
[i
] = subs
[i
]->upoly
;
3308 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3317 isl_qpolynomial_free(qp
);
3321 /* Extend "bset" with extra set dimensions for each integer division
3322 * in "qp" and then call "fn" with the extended bset and the polynomial
3323 * that results from replacing each of the integer divisions by the
3324 * corresponding extra set dimension.
3326 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3327 __isl_keep isl_basic_set
*bset
,
3328 int (*fn
)(__isl_take isl_basic_set
*bset
,
3329 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3333 isl_qpolynomial
*poly
;
3337 if (qp
->div
->n_row
== 0)
3338 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3341 div
= isl_mat_copy(qp
->div
);
3342 dim
= isl_space_copy(qp
->dim
);
3343 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3344 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3345 bset
= isl_basic_set_copy(bset
);
3346 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, qp
->div
->n_row
);
3347 bset
= add_div_constraints(bset
, div
);
3349 return fn(bset
, poly
, user
);
3354 /* Return total degree in variables first (inclusive) up to last (exclusive).
3356 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3360 struct isl_upoly_rec
*rec
;
3364 if (isl_upoly_is_zero(up
))
3366 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3369 rec
= isl_upoly_as_rec(up
);
3373 for (i
= 0; i
< rec
->n
; ++i
) {
3376 if (isl_upoly_is_zero(rec
->p
[i
]))
3378 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3388 /* Return total degree in set variables.
3390 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3398 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3399 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3400 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3403 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3404 unsigned pos
, int deg
)
3407 struct isl_upoly_rec
*rec
;
3412 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3414 return isl_upoly_copy(up
);
3416 return isl_upoly_zero(up
->ctx
);
3419 rec
= isl_upoly_as_rec(up
);
3423 if (up
->var
== pos
) {
3425 return isl_upoly_copy(rec
->p
[deg
]);
3427 return isl_upoly_zero(up
->ctx
);
3430 up
= isl_upoly_copy(up
);
3431 up
= isl_upoly_cow(up
);
3432 rec
= isl_upoly_as_rec(up
);
3436 for (i
= 0; i
< rec
->n
; ++i
) {
3437 struct isl_upoly
*t
;
3438 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3441 isl_upoly_free(rec
->p
[i
]);
3451 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3453 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3454 __isl_keep isl_qpolynomial
*qp
,
3455 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3458 struct isl_upoly
*up
;
3464 if (type
== isl_dim_out
)
3465 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3466 "output/set dimension does not have a coefficient",
3468 if (type
== isl_dim_in
)
3471 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3474 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3475 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3477 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3480 isl_mat_free(c
->div
);
3481 c
->div
= isl_mat_copy(qp
->div
);
3486 isl_qpolynomial_free(c
);
3490 /* Homogenize the polynomial in the variables first (inclusive) up to
3491 * last (exclusive) by inserting powers of variable first.
3492 * Variable first is assumed not to appear in the input.
3494 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3495 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3496 int first
, int last
)
3499 struct isl_upoly_rec
*rec
;
3503 if (isl_upoly_is_zero(up
))
3507 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3508 struct isl_upoly
*hom
;
3510 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3513 rec
= isl_upoly_as_rec(hom
);
3514 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3519 up
= isl_upoly_cow(up
);
3520 rec
= isl_upoly_as_rec(up
);
3524 for (i
= 0; i
< rec
->n
; ++i
) {
3525 if (isl_upoly_is_zero(rec
->p
[i
]))
3527 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3528 up
->var
< last
? deg
+ i
: i
, target
,
3540 /* Homogenize the polynomial in the set variables by introducing
3541 * powers of an extra set variable at position 0.
3543 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3544 __isl_take isl_qpolynomial
*poly
)
3548 int deg
= isl_qpolynomial_degree(poly
);
3553 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3554 poly
= isl_qpolynomial_cow(poly
);
3558 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3559 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3560 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3567 isl_qpolynomial_free(poly
);
3571 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*dim
,
3572 __isl_take isl_mat
*div
)
3580 n
= isl_space_dim(dim
, isl_dim_all
) + div
->n_row
;
3582 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3583 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3590 isl_int_init(term
->n
);
3591 isl_int_init(term
->d
);
3595 isl_space_free(dim
);
3600 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3609 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3618 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3620 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3624 isl_int_set(dup
->n
, term
->n
);
3625 isl_int_set(dup
->d
, term
->d
);
3627 for (i
= 0; i
< total
; ++i
)
3628 dup
->pow
[i
] = term
->pow
[i
];
3633 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3641 return isl_term_dup(term
);
3644 void isl_term_free(__isl_take isl_term
*term
)
3649 if (--term
->ref
> 0)
3652 isl_space_free(term
->dim
);
3653 isl_mat_free(term
->div
);
3654 isl_int_clear(term
->n
);
3655 isl_int_clear(term
->d
);
3659 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3667 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3668 case isl_dim_div
: return term
->div
->n_row
;
3669 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3675 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3677 return term
? term
->dim
->ctx
: NULL
;
3680 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3684 isl_int_set(*n
, term
->n
);
3687 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
3691 isl_int_set(*d
, term
->d
);
3694 /* Return the coefficient of the term "term".
3696 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3701 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3705 int isl_term_get_exp(__isl_keep isl_term
*term
,
3706 enum isl_dim_type type
, unsigned pos
)
3711 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3713 if (type
>= isl_dim_set
)
3714 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3715 if (type
>= isl_dim_div
)
3716 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3718 return term
->pow
[pos
];
3721 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3723 isl_local_space
*ls
;
3729 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3732 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3733 isl_mat_copy(term
->div
));
3734 aff
= isl_aff_alloc(ls
);
3738 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3740 aff
= isl_aff_normalize(aff
);
3745 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3746 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3747 __isl_take isl_term
*term
, void *user
)
3750 struct isl_upoly_rec
*rec
;
3755 if (isl_upoly_is_zero(up
))
3758 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3759 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3760 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3762 if (isl_upoly_is_cst(up
)) {
3763 struct isl_upoly_cst
*cst
;
3764 cst
= isl_upoly_as_cst(up
);
3767 term
= isl_term_cow(term
);
3770 isl_int_set(term
->n
, cst
->n
);
3771 isl_int_set(term
->d
, cst
->d
);
3772 if (fn(isl_term_copy(term
), user
) < 0)
3777 rec
= isl_upoly_as_rec(up
);
3781 for (i
= 0; i
< rec
->n
; ++i
) {
3782 term
= isl_term_cow(term
);
3785 term
->pow
[up
->var
] = i
;
3786 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3790 term
->pow
[up
->var
] = 0;
3794 isl_term_free(term
);
3798 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3799 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3804 return isl_stat_error
;
3806 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3808 return isl_stat_error
;
3810 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3812 isl_term_free(term
);
3814 return term
? isl_stat_ok
: isl_stat_error
;
3817 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3819 struct isl_upoly
*up
;
3820 isl_qpolynomial
*qp
;
3826 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3828 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3829 for (i
= 0; i
< n
; ++i
) {
3832 up
= isl_upoly_mul(up
,
3833 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3836 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
3839 isl_mat_free(qp
->div
);
3840 qp
->div
= isl_mat_copy(term
->div
);
3844 isl_term_free(term
);
3847 isl_qpolynomial_free(qp
);
3848 isl_term_free(term
);
3852 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3853 __isl_take isl_space
*dim
)
3862 if (isl_space_is_equal(qp
->dim
, dim
)) {
3863 isl_space_free(dim
);
3867 qp
= isl_qpolynomial_cow(qp
);
3871 extra
= isl_space_dim(dim
, isl_dim_set
) -
3872 isl_space_dim(qp
->dim
, isl_dim_set
);
3873 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
3874 if (qp
->div
->n_row
) {
3877 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
3880 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3882 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
3887 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
3890 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
3891 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
3893 isl_space_free(qp
->dim
);
3898 isl_space_free(dim
);
3899 isl_qpolynomial_free(qp
);
3903 /* For each parameter or variable that does not appear in qp,
3904 * first eliminate the variable from all constraints and then set it to zero.
3906 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
3907 __isl_keep isl_qpolynomial
*qp
)
3918 d
= isl_space_dim(set
->dim
, isl_dim_all
);
3919 active
= isl_calloc_array(set
->ctx
, int, d
);
3920 if (set_active(qp
, active
) < 0)
3923 for (i
= 0; i
< d
; ++i
)
3932 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
3933 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
3934 for (i
= 0; i
< nparam
; ++i
) {
3937 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
3938 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
3940 for (i
= 0; i
< nvar
; ++i
) {
3941 if (active
[nparam
+ i
])
3943 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
3944 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
3956 struct isl_opt_data
{
3957 isl_qpolynomial
*qp
;
3963 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
3965 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
3968 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
3972 } else if (data
->max
) {
3973 data
->opt
= isl_val_max(data
->opt
, val
);
3975 data
->opt
= isl_val_min(data
->opt
, val
);
3981 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
3982 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
3984 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
3989 if (isl_upoly_is_cst(qp
->upoly
)) {
3991 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
3992 isl_qpolynomial_free(qp
);
3996 set
= fix_inactive(set
, qp
);
3999 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4003 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4006 isl_qpolynomial_free(qp
);
4010 isl_qpolynomial_free(qp
);
4011 isl_val_free(data
.opt
);
4015 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4016 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4021 struct isl_upoly
**subs
;
4022 isl_mat
*mat
, *diag
;
4024 qp
= isl_qpolynomial_cow(qp
);
4029 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4031 n_sub
= morph
->inv
->n_row
- 1;
4032 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4033 n_sub
+= qp
->div
->n_row
;
4034 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4038 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4039 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4040 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4041 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4042 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4043 subs
[morph
->inv
->n_row
- 1 + i
] =
4044 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4046 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4048 for (i
= 0; i
< n_sub
; ++i
)
4049 isl_upoly_free(subs
[i
]);
4052 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4053 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4054 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4055 mat
= isl_mat_diagonal(mat
, diag
);
4056 qp
->div
= isl_mat_product(qp
->div
, mat
);
4057 isl_space_free(qp
->dim
);
4058 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4060 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4063 isl_morph_free(morph
);
4067 isl_qpolynomial_free(qp
);
4068 isl_morph_free(morph
);
4072 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4073 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4074 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4076 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4077 &isl_pw_qpolynomial_mul
);
4080 /* Reorder the columns of the given div definitions according to the
4083 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
4084 __isl_take isl_reordering
*r
)
4093 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
4094 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
4098 for (i
= 0; i
< div
->n_row
; ++i
) {
4099 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
4100 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
4101 for (j
= 0; j
< r
->len
; ++j
)
4102 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
4103 div
->row
[i
][2 + j
]);
4106 isl_reordering_free(r
);
4110 isl_reordering_free(r
);
4115 /* Reorder the dimension of "qp" according to the given reordering.
4117 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4118 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4120 qp
= isl_qpolynomial_cow(qp
);
4124 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4128 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
4132 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4136 qp
= isl_qpolynomial_reset_domain_space(qp
, isl_space_copy(r
->dim
));
4138 isl_reordering_free(r
);
4141 isl_qpolynomial_free(qp
);
4142 isl_reordering_free(r
);
4146 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4147 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4152 if (!isl_space_match(qp
->dim
, isl_dim_param
, model
, isl_dim_param
)) {
4153 isl_reordering
*exp
;
4155 model
= isl_space_drop_dims(model
, isl_dim_in
,
4156 0, isl_space_dim(model
, isl_dim_in
));
4157 model
= isl_space_drop_dims(model
, isl_dim_out
,
4158 0, isl_space_dim(model
, isl_dim_out
));
4159 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4160 exp
= isl_reordering_extend_space(exp
,
4161 isl_qpolynomial_get_domain_space(qp
));
4162 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4165 isl_space_free(model
);
4168 isl_space_free(model
);
4169 isl_qpolynomial_free(qp
);
4173 struct isl_split_periods_data
{
4175 isl_pw_qpolynomial
*res
;
4178 /* Create a slice where the integer division "div" has the fixed value "v".
4179 * In particular, if "div" refers to floor(f/m), then create a slice
4181 * m v <= f <= m v + (m - 1)
4186 * -f + m v + (m - 1) >= 0
4188 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*dim
,
4189 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4192 isl_basic_set
*bset
= NULL
;
4198 total
= isl_space_dim(dim
, isl_dim_all
);
4199 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0, 0, 2);
4201 k
= isl_basic_set_alloc_inequality(bset
);
4204 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4205 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4207 k
= isl_basic_set_alloc_inequality(bset
);
4210 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4211 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4212 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4213 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4215 isl_space_free(dim
);
4216 return isl_set_from_basic_set(bset
);
4218 isl_basic_set_free(bset
);
4219 isl_space_free(dim
);
4223 static isl_stat
split_periods(__isl_take isl_set
*set
,
4224 __isl_take isl_qpolynomial
*qp
, void *user
);
4226 /* Create a slice of the domain "set" such that integer division "div"
4227 * has the fixed value "v" and add the results to data->res,
4228 * replacing the integer division by "v" in "qp".
4230 static isl_stat
set_div(__isl_take isl_set
*set
,
4231 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4232 struct isl_split_periods_data
*data
)
4237 struct isl_upoly
*cst
;
4239 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4240 set
= isl_set_intersect(set
, slice
);
4245 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4247 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4248 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4250 isl_int_addmul(qp
->div
->row
[i
][1],
4251 qp
->div
->row
[i
][2 + total
+ div
], v
);
4252 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4255 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4256 qp
= substitute_div(qp
, div
, cst
);
4258 return split_periods(set
, qp
, data
);
4261 isl_qpolynomial_free(qp
);
4265 /* Split the domain "set" such that integer division "div"
4266 * has a fixed value (ranging from "min" to "max") on each slice
4267 * and add the results to data->res.
4269 static isl_stat
split_div(__isl_take isl_set
*set
,
4270 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4271 struct isl_split_periods_data
*data
)
4273 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4274 isl_set
*set_i
= isl_set_copy(set
);
4275 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4277 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4281 isl_qpolynomial_free(qp
);
4285 isl_qpolynomial_free(qp
);
4286 return isl_stat_error
;
4289 /* If "qp" refers to any integer division
4290 * that can only attain "max_periods" distinct values on "set"
4291 * then split the domain along those distinct values.
4292 * Add the results (or the original if no splitting occurs)
4295 static isl_stat
split_periods(__isl_take isl_set
*set
,
4296 __isl_take isl_qpolynomial
*qp
, void *user
)
4299 isl_pw_qpolynomial
*pwqp
;
4300 struct isl_split_periods_data
*data
;
4303 isl_stat r
= isl_stat_ok
;
4305 data
= (struct isl_split_periods_data
*)user
;
4310 if (qp
->div
->n_row
== 0) {
4311 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4312 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4318 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4319 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4320 enum isl_lp_result lp_res
;
4322 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4323 qp
->div
->n_row
) != -1)
4326 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4327 set
->ctx
->one
, &min
, NULL
, NULL
);
4328 if (lp_res
== isl_lp_error
)
4330 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4332 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4334 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4335 set
->ctx
->one
, &max
, NULL
, NULL
);
4336 if (lp_res
== isl_lp_error
)
4338 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4340 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4342 isl_int_sub(max
, max
, min
);
4343 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4344 isl_int_add(max
, max
, min
);
4349 if (i
< qp
->div
->n_row
) {
4350 r
= split_div(set
, qp
, i
, min
, max
, data
);
4352 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4353 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4365 isl_qpolynomial_free(qp
);
4366 return isl_stat_error
;
4369 /* If any quasi-polynomial in pwqp refers to any integer division
4370 * that can only attain "max_periods" distinct values on its domain
4371 * then split the domain along those distinct values.
4373 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4374 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4376 struct isl_split_periods_data data
;
4378 data
.max_periods
= max_periods
;
4379 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4381 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4384 isl_pw_qpolynomial_free(pwqp
);
4388 isl_pw_qpolynomial_free(data
.res
);
4389 isl_pw_qpolynomial_free(pwqp
);
4393 /* Construct a piecewise quasipolynomial that is constant on the given
4394 * domain. In particular, it is
4397 * infinity if cst == -1
4399 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4400 __isl_take isl_basic_set
*bset
, int cst
)
4403 isl_qpolynomial
*qp
;
4408 bset
= isl_basic_set_params(bset
);
4409 dim
= isl_basic_set_get_space(bset
);
4411 qp
= isl_qpolynomial_infty_on_domain(dim
);
4413 qp
= isl_qpolynomial_zero_on_domain(dim
);
4415 qp
= isl_qpolynomial_one_on_domain(dim
);
4416 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4419 /* Factor bset, call fn on each of the factors and return the product.
4421 * If no factors can be found, simply call fn on the input.
4422 * Otherwise, construct the factors based on the factorizer,
4423 * call fn on each factor and compute the product.
4425 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4426 __isl_take isl_basic_set
*bset
,
4427 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4433 isl_qpolynomial
*qp
;
4434 isl_pw_qpolynomial
*pwqp
;
4438 f
= isl_basic_set_factorizer(bset
);
4441 if (f
->n_group
== 0) {
4442 isl_factorizer_free(f
);
4446 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4447 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4449 dim
= isl_basic_set_get_space(bset
);
4450 dim
= isl_space_domain(dim
);
4451 set
= isl_set_universe(isl_space_copy(dim
));
4452 qp
= isl_qpolynomial_one_on_domain(dim
);
4453 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4455 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4457 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4458 isl_basic_set
*bset_i
;
4459 isl_pw_qpolynomial
*pwqp_i
;
4461 bset_i
= isl_basic_set_copy(bset
);
4462 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4463 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4464 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4466 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4467 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4468 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4470 pwqp_i
= fn(bset_i
);
4471 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4476 isl_basic_set_free(bset
);
4477 isl_factorizer_free(f
);
4481 isl_basic_set_free(bset
);
4485 /* Factor bset, call fn on each of the factors and return the product.
4486 * The function is assumed to evaluate to zero on empty domains,
4487 * to one on zero-dimensional domains and to infinity on unbounded domains
4488 * and will not be called explicitly on zero-dimensional or unbounded domains.
4490 * We first check for some special cases and remove all equalities.
4491 * Then we hand over control to compressed_multiplicative_call.
4493 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4494 __isl_take isl_basic_set
*bset
,
4495 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4499 isl_pw_qpolynomial
*pwqp
;
4504 if (isl_basic_set_plain_is_empty(bset
))
4505 return constant_on_domain(bset
, 0);
4507 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4508 return constant_on_domain(bset
, 1);
4510 bounded
= isl_basic_set_is_bounded(bset
);
4514 return constant_on_domain(bset
, -1);
4516 if (bset
->n_eq
== 0)
4517 return compressed_multiplicative_call(bset
, fn
);
4519 morph
= isl_basic_set_full_compression(bset
);
4520 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4522 pwqp
= compressed_multiplicative_call(bset
, fn
);
4524 morph
= isl_morph_dom_params(morph
);
4525 morph
= isl_morph_ran_params(morph
);
4526 morph
= isl_morph_inverse(morph
);
4528 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4532 isl_basic_set_free(bset
);
4536 /* Drop all floors in "qp", turning each integer division [a/m] into
4537 * a rational division a/m. If "down" is set, then the integer division
4538 * is replaced by (a-(m-1))/m instead.
4540 static __isl_give isl_qpolynomial
*qp_drop_floors(
4541 __isl_take isl_qpolynomial
*qp
, int down
)
4544 struct isl_upoly
*s
;
4548 if (qp
->div
->n_row
== 0)
4551 qp
= isl_qpolynomial_cow(qp
);
4555 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4557 isl_int_sub(qp
->div
->row
[i
][1],
4558 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4559 isl_int_add_ui(qp
->div
->row
[i
][1],
4560 qp
->div
->row
[i
][1], 1);
4562 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4563 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4564 qp
= substitute_div(qp
, i
, s
);
4572 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4573 * a rational division a/m.
4575 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4576 __isl_take isl_pw_qpolynomial
*pwqp
)
4583 if (isl_pw_qpolynomial_is_zero(pwqp
))
4586 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4590 for (i
= 0; i
< pwqp
->n
; ++i
) {
4591 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4598 isl_pw_qpolynomial_free(pwqp
);
4602 /* Adjust all the integer divisions in "qp" such that they are at least
4603 * one over the given orthant (identified by "signs"). This ensures
4604 * that they will still be non-negative even after subtracting (m-1)/m.
4606 * In particular, f is replaced by f' + v, changing f = [a/m]
4607 * to f' = [(a - m v)/m].
4608 * If the constant term k in a is smaller than m,
4609 * the constant term of v is set to floor(k/m) - 1.
4610 * For any other term, if the coefficient c and the variable x have
4611 * the same sign, then no changes are needed.
4612 * Otherwise, if the variable is positive (and c is negative),
4613 * then the coefficient of x in v is set to floor(c/m).
4614 * If the variable is negative (and c is positive),
4615 * then the coefficient of x in v is set to ceil(c/m).
4617 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4623 struct isl_upoly
*s
;
4625 qp
= isl_qpolynomial_cow(qp
);
4628 qp
->div
= isl_mat_cow(qp
->div
);
4632 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4633 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4635 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4636 isl_int
*row
= qp
->div
->row
[i
];
4640 if (isl_int_lt(row
[1], row
[0])) {
4641 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4642 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4643 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4645 for (j
= 0; j
< total
; ++j
) {
4646 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4649 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4651 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4652 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4654 for (j
= 0; j
< i
; ++j
) {
4655 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4657 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4658 row
[2 + total
+ j
], row
[0]);
4659 isl_int_submul(row
[2 + total
+ j
],
4660 row
[0], v
->el
[1 + total
+ j
]);
4662 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4663 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4665 isl_seq_combine(qp
->div
->row
[j
] + 1,
4666 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4667 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4669 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4670 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4671 qp
->div
->ctx
->one
, v
->size
);
4672 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4682 isl_qpolynomial_free(qp
);
4686 struct isl_to_poly_data
{
4688 isl_pw_qpolynomial
*res
;
4689 isl_qpolynomial
*qp
;
4692 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4693 * We first make all integer divisions positive and then split the
4694 * quasipolynomials into terms with sign data->sign (the direction
4695 * of the requested approximation) and terms with the opposite sign.
4696 * In the first set of terms, each integer division [a/m] is
4697 * overapproximated by a/m, while in the second it is underapproximated
4700 static int to_polynomial_on_orthant(__isl_take isl_set
*orthant
, int *signs
,
4703 struct isl_to_poly_data
*data
= user
;
4704 isl_pw_qpolynomial
*t
;
4705 isl_qpolynomial
*qp
, *up
, *down
;
4707 qp
= isl_qpolynomial_copy(data
->qp
);
4708 qp
= make_divs_pos(qp
, signs
);
4710 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4711 up
= qp_drop_floors(up
, 0);
4712 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4713 down
= qp_drop_floors(down
, 1);
4715 isl_qpolynomial_free(qp
);
4716 qp
= isl_qpolynomial_add(up
, down
);
4718 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4719 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4724 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4725 * the polynomial will be an overapproximation. If "sign" is negative,
4726 * it will be an underapproximation. If "sign" is zero, the approximation
4727 * will lie somewhere in between.
4729 * In particular, is sign == 0, we simply drop the floors, turning
4730 * the integer divisions into rational divisions.
4731 * Otherwise, we split the domains into orthants, make all integer divisions
4732 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4733 * depending on the requested sign and the sign of the term in which
4734 * the integer division appears.
4736 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4737 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4740 struct isl_to_poly_data data
;
4743 return pwqp_drop_floors(pwqp
);
4749 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4751 for (i
= 0; i
< pwqp
->n
; ++i
) {
4752 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4753 isl_pw_qpolynomial
*t
;
4754 t
= isl_pw_qpolynomial_alloc(
4755 isl_set_copy(pwqp
->p
[i
].set
),
4756 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4757 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4760 data
.qp
= pwqp
->p
[i
].qp
;
4761 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4762 &to_polynomial_on_orthant
, &data
) < 0)
4766 isl_pw_qpolynomial_free(pwqp
);
4770 isl_pw_qpolynomial_free(pwqp
);
4771 isl_pw_qpolynomial_free(data
.res
);
4775 static __isl_give isl_pw_qpolynomial
*poly_entry(
4776 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4780 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4783 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4784 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4786 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4787 &poly_entry
, &sign
);
4790 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4791 __isl_take isl_qpolynomial
*qp
)
4795 isl_vec
*aff
= NULL
;
4796 isl_basic_map
*bmap
= NULL
;
4802 if (!isl_upoly_is_affine(qp
->upoly
))
4803 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4804 "input quasi-polynomial not affine", goto error
);
4805 aff
= isl_qpolynomial_extract_affine(qp
);
4808 dim
= isl_qpolynomial_get_space(qp
);
4809 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4810 n_div
= qp
->div
->n_row
;
4811 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4813 for (i
= 0; i
< n_div
; ++i
) {
4814 k
= isl_basic_map_alloc_div(bmap
);
4817 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4818 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4819 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4822 k
= isl_basic_map_alloc_equality(bmap
);
4825 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4826 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4827 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4830 isl_qpolynomial_free(qp
);
4831 bmap
= isl_basic_map_finalize(bmap
);
4835 isl_qpolynomial_free(qp
);
4836 isl_basic_map_free(bmap
);