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.h>
27 #include <isl_local_space_private.h>
28 #include <isl_aff_private.h>
29 #include <isl_val_private.h>
30 #include <isl_config.h>
31 #include <isl/deprecated/polynomial_int.h>
33 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
36 case isl_dim_param
: return 0;
37 case isl_dim_in
: return dim
->nparam
;
38 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
43 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
51 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
56 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
58 return (struct isl_upoly_cst
*)up
;
61 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
66 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
68 return (struct isl_upoly_rec
*)up
;
71 /* Compare two polynomials.
73 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
74 * than "up2" and 0 if they are equal.
76 static int isl_upoly_plain_cmp(__isl_keep
struct isl_upoly
*up1
,
77 __isl_keep
struct isl_upoly
*up2
)
80 struct isl_upoly_rec
*rec1
, *rec2
;
88 if (up1
->var
!= up2
->var
)
89 return up1
->var
- up2
->var
;
91 if (isl_upoly_is_cst(up1
)) {
92 struct isl_upoly_cst
*cst1
, *cst2
;
95 cst1
= isl_upoly_as_cst(up1
);
96 cst2
= isl_upoly_as_cst(up2
);
99 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
102 return isl_int_cmp(cst1
->d
, cst2
->d
);
105 rec1
= isl_upoly_as_rec(up1
);
106 rec2
= isl_upoly_as_rec(up2
);
110 if (rec1
->n
!= rec2
->n
)
111 return rec1
->n
- rec2
->n
;
113 for (i
= 0; i
< rec1
->n
; ++i
) {
114 int cmp
= isl_upoly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
122 isl_bool
isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
123 __isl_keep
struct isl_upoly
*up2
)
126 struct isl_upoly_rec
*rec1
, *rec2
;
129 return isl_bool_error
;
131 return isl_bool_true
;
132 if (up1
->var
!= up2
->var
)
133 return isl_bool_false
;
134 if (isl_upoly_is_cst(up1
)) {
135 struct isl_upoly_cst
*cst1
, *cst2
;
136 cst1
= isl_upoly_as_cst(up1
);
137 cst2
= isl_upoly_as_cst(up2
);
139 return isl_bool_error
;
140 return isl_int_eq(cst1
->n
, cst2
->n
) &&
141 isl_int_eq(cst1
->d
, cst2
->d
);
144 rec1
= isl_upoly_as_rec(up1
);
145 rec2
= isl_upoly_as_rec(up2
);
147 return isl_bool_error
;
149 if (rec1
->n
!= rec2
->n
)
150 return isl_bool_false
;
152 for (i
= 0; i
< rec1
->n
; ++i
) {
153 isl_bool eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
158 return isl_bool_true
;
161 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
163 struct isl_upoly_cst
*cst
;
167 if (!isl_upoly_is_cst(up
))
170 cst
= isl_upoly_as_cst(up
);
174 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
177 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
179 struct isl_upoly_cst
*cst
;
183 if (!isl_upoly_is_cst(up
))
186 cst
= isl_upoly_as_cst(up
);
190 return isl_int_sgn(cst
->n
);
193 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
195 struct isl_upoly_cst
*cst
;
199 if (!isl_upoly_is_cst(up
))
202 cst
= isl_upoly_as_cst(up
);
206 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
209 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
211 struct isl_upoly_cst
*cst
;
215 if (!isl_upoly_is_cst(up
))
218 cst
= isl_upoly_as_cst(up
);
222 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
225 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
227 struct isl_upoly_cst
*cst
;
231 if (!isl_upoly_is_cst(up
))
234 cst
= isl_upoly_as_cst(up
);
238 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
241 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
243 struct isl_upoly_cst
*cst
;
247 if (!isl_upoly_is_cst(up
))
250 cst
= isl_upoly_as_cst(up
);
254 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
257 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
259 struct isl_upoly_cst
*cst
;
263 if (!isl_upoly_is_cst(up
))
266 cst
= isl_upoly_as_cst(up
);
270 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
273 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
275 struct isl_upoly_cst
*cst
;
277 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
286 isl_int_init(cst
->n
);
287 isl_int_init(cst
->d
);
292 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
294 struct isl_upoly_cst
*cst
;
296 cst
= isl_upoly_cst_alloc(ctx
);
300 isl_int_set_si(cst
->n
, 0);
301 isl_int_set_si(cst
->d
, 1);
306 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
308 struct isl_upoly_cst
*cst
;
310 cst
= isl_upoly_cst_alloc(ctx
);
314 isl_int_set_si(cst
->n
, 1);
315 isl_int_set_si(cst
->d
, 1);
320 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
322 struct isl_upoly_cst
*cst
;
324 cst
= isl_upoly_cst_alloc(ctx
);
328 isl_int_set_si(cst
->n
, 1);
329 isl_int_set_si(cst
->d
, 0);
334 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
336 struct isl_upoly_cst
*cst
;
338 cst
= isl_upoly_cst_alloc(ctx
);
342 isl_int_set_si(cst
->n
, -1);
343 isl_int_set_si(cst
->d
, 0);
348 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
350 struct isl_upoly_cst
*cst
;
352 cst
= isl_upoly_cst_alloc(ctx
);
356 isl_int_set_si(cst
->n
, 0);
357 isl_int_set_si(cst
->d
, 0);
362 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
363 isl_int n
, isl_int d
)
365 struct isl_upoly_cst
*cst
;
367 cst
= isl_upoly_cst_alloc(ctx
);
371 isl_int_set(cst
->n
, n
);
372 isl_int_set(cst
->d
, d
);
377 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
380 struct isl_upoly_rec
*rec
;
382 isl_assert(ctx
, var
>= 0, return NULL
);
383 isl_assert(ctx
, size
>= 0, return NULL
);
384 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
385 sizeof(struct isl_upoly_rec
) +
386 size
* sizeof(struct isl_upoly
*));
401 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
402 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
404 qp
= isl_qpolynomial_cow(qp
);
408 isl_space_free(qp
->dim
);
413 isl_qpolynomial_free(qp
);
418 /* Reset the space of "qp". This function is called from isl_pw_templ.c
419 * and doesn't know if the space of an element object is represented
420 * directly or through its domain. It therefore passes along both.
422 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
423 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
424 __isl_take isl_space
*domain
)
426 isl_space_free(space
);
427 return isl_qpolynomial_reset_domain_space(qp
, domain
);
430 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
432 return qp
? qp
->dim
->ctx
: NULL
;
435 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
436 __isl_keep isl_qpolynomial
*qp
)
438 return qp
? isl_space_copy(qp
->dim
) : NULL
;
441 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
446 space
= isl_space_copy(qp
->dim
);
447 space
= isl_space_from_domain(space
);
448 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
452 /* Return the number of variables of the given type in the domain of "qp".
454 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
455 enum isl_dim_type type
)
459 if (type
== isl_dim_div
)
460 return qp
->div
->n_row
;
461 if (type
== isl_dim_all
)
462 return isl_space_dim(qp
->dim
, isl_dim_all
) +
463 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
464 return isl_space_dim(qp
->dim
, type
);
467 /* Externally, an isl_qpolynomial has a map space, but internally, the
468 * ls field corresponds to the domain of that space.
470 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
471 enum isl_dim_type type
)
475 if (type
== isl_dim_out
)
477 if (type
== isl_dim_in
)
479 return isl_qpolynomial_domain_dim(qp
, type
);
482 /* Return the offset of the first coefficient of type "type" in
483 * the domain of "qp".
485 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
486 enum isl_dim_type type
)
495 return 1 + isl_space_offset(qp
->dim
, type
);
497 return 1 + isl_space_dim(qp
->dim
, isl_dim_all
);
503 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
505 return qp
? isl_upoly_is_zero(qp
->upoly
) : isl_bool_error
;
508 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
510 return qp
? isl_upoly_is_one(qp
->upoly
) : isl_bool_error
;
513 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
515 return qp
? isl_upoly_is_nan(qp
->upoly
) : isl_bool_error
;
518 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
520 return qp
? isl_upoly_is_infty(qp
->upoly
) : isl_bool_error
;
523 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
525 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : isl_bool_error
;
528 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
530 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
533 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
535 isl_int_clear(cst
->n
);
536 isl_int_clear(cst
->d
);
539 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
543 for (i
= 0; i
< rec
->n
; ++i
)
544 isl_upoly_free(rec
->p
[i
]);
547 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
556 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
558 struct isl_upoly_cst
*cst
;
559 struct isl_upoly_cst
*dup
;
561 cst
= isl_upoly_as_cst(up
);
565 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
568 isl_int_set(dup
->n
, cst
->n
);
569 isl_int_set(dup
->d
, cst
->d
);
574 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
577 struct isl_upoly_rec
*rec
;
578 struct isl_upoly_rec
*dup
;
580 rec
= isl_upoly_as_rec(up
);
584 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
588 for (i
= 0; i
< rec
->n
; ++i
) {
589 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
597 isl_upoly_free(&dup
->up
);
601 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
606 if (isl_upoly_is_cst(up
))
607 return isl_upoly_dup_cst(up
);
609 return isl_upoly_dup_rec(up
);
612 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
620 return isl_upoly_dup(up
);
623 __isl_null
struct isl_upoly
*isl_upoly_free(__isl_take
struct isl_upoly
*up
)
632 upoly_free_cst((struct isl_upoly_cst
*)up
);
634 upoly_free_rec((struct isl_upoly_rec
*)up
);
636 isl_ctx_deref(up
->ctx
);
641 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
646 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
647 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
648 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
649 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
654 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
655 __isl_take
struct isl_upoly
*up2
)
657 struct isl_upoly_cst
*cst1
;
658 struct isl_upoly_cst
*cst2
;
660 up1
= isl_upoly_cow(up1
);
664 cst1
= isl_upoly_as_cst(up1
);
665 cst2
= isl_upoly_as_cst(up2
);
667 if (isl_int_eq(cst1
->d
, cst2
->d
))
668 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
670 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
671 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
672 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
675 isl_upoly_cst_reduce(cst1
);
685 static __isl_give
struct isl_upoly
*replace_by_zero(
686 __isl_take
struct isl_upoly
*up
)
694 return isl_upoly_zero(ctx
);
697 static __isl_give
struct isl_upoly
*replace_by_constant_term(
698 __isl_take
struct isl_upoly
*up
)
700 struct isl_upoly_rec
*rec
;
701 struct isl_upoly
*cst
;
706 rec
= isl_upoly_as_rec(up
);
709 cst
= isl_upoly_copy(rec
->p
[0]);
717 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
718 __isl_take
struct isl_upoly
*up2
)
721 struct isl_upoly_rec
*rec1
, *rec2
;
726 if (isl_upoly_is_nan(up1
)) {
731 if (isl_upoly_is_nan(up2
)) {
736 if (isl_upoly_is_zero(up1
)) {
741 if (isl_upoly_is_zero(up2
)) {
746 if (up1
->var
< up2
->var
)
747 return isl_upoly_sum(up2
, up1
);
749 if (up2
->var
< up1
->var
) {
750 struct isl_upoly_rec
*rec
;
751 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
755 up1
= isl_upoly_cow(up1
);
756 rec
= isl_upoly_as_rec(up1
);
759 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
761 up1
= replace_by_constant_term(up1
);
765 if (isl_upoly_is_cst(up1
))
766 return isl_upoly_sum_cst(up1
, up2
);
768 rec1
= isl_upoly_as_rec(up1
);
769 rec2
= isl_upoly_as_rec(up2
);
773 if (rec1
->n
< rec2
->n
)
774 return isl_upoly_sum(up2
, up1
);
776 up1
= isl_upoly_cow(up1
);
777 rec1
= isl_upoly_as_rec(up1
);
781 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
782 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
783 isl_upoly_copy(rec2
->p
[i
]));
786 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
787 isl_upoly_free(rec1
->p
[i
]);
793 up1
= replace_by_zero(up1
);
794 else if (rec1
->n
== 1)
795 up1
= replace_by_constant_term(up1
);
806 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
807 __isl_take
struct isl_upoly
*up
, isl_int v
)
809 struct isl_upoly_cst
*cst
;
811 up
= isl_upoly_cow(up
);
815 cst
= isl_upoly_as_cst(up
);
817 isl_int_addmul(cst
->n
, cst
->d
, v
);
822 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
823 __isl_take
struct isl_upoly
*up
, isl_int v
)
825 struct isl_upoly_rec
*rec
;
830 if (isl_upoly_is_cst(up
))
831 return isl_upoly_cst_add_isl_int(up
, v
);
833 up
= isl_upoly_cow(up
);
834 rec
= isl_upoly_as_rec(up
);
838 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
848 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
849 __isl_take
struct isl_upoly
*up
, isl_int v
)
851 struct isl_upoly_cst
*cst
;
853 if (isl_upoly_is_zero(up
))
856 up
= isl_upoly_cow(up
);
860 cst
= isl_upoly_as_cst(up
);
862 isl_int_mul(cst
->n
, cst
->n
, v
);
867 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
868 __isl_take
struct isl_upoly
*up
, isl_int v
)
871 struct isl_upoly_rec
*rec
;
876 if (isl_upoly_is_cst(up
))
877 return isl_upoly_cst_mul_isl_int(up
, v
);
879 up
= isl_upoly_cow(up
);
880 rec
= isl_upoly_as_rec(up
);
884 for (i
= 0; i
< rec
->n
; ++i
) {
885 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
896 /* Multiply the constant polynomial "up" by "v".
898 static __isl_give
struct isl_upoly
*isl_upoly_cst_scale_val(
899 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
901 struct isl_upoly_cst
*cst
;
903 if (isl_upoly_is_zero(up
))
906 up
= isl_upoly_cow(up
);
910 cst
= isl_upoly_as_cst(up
);
912 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
913 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
914 isl_upoly_cst_reduce(cst
);
919 /* Multiply the polynomial "up" by "v".
921 static __isl_give
struct isl_upoly
*isl_upoly_scale_val(
922 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
925 struct isl_upoly_rec
*rec
;
930 if (isl_upoly_is_cst(up
))
931 return isl_upoly_cst_scale_val(up
, v
);
933 up
= isl_upoly_cow(up
);
934 rec
= isl_upoly_as_rec(up
);
938 for (i
= 0; i
< rec
->n
; ++i
) {
939 rec
->p
[i
] = isl_upoly_scale_val(rec
->p
[i
], v
);
950 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
951 __isl_take
struct isl_upoly
*up2
)
953 struct isl_upoly_cst
*cst1
;
954 struct isl_upoly_cst
*cst2
;
956 up1
= isl_upoly_cow(up1
);
960 cst1
= isl_upoly_as_cst(up1
);
961 cst2
= isl_upoly_as_cst(up2
);
963 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
964 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
966 isl_upoly_cst_reduce(cst1
);
976 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
977 __isl_take
struct isl_upoly
*up2
)
979 struct isl_upoly_rec
*rec1
;
980 struct isl_upoly_rec
*rec2
;
981 struct isl_upoly_rec
*res
= NULL
;
985 rec1
= isl_upoly_as_rec(up1
);
986 rec2
= isl_upoly_as_rec(up2
);
989 size
= rec1
->n
+ rec2
->n
- 1;
990 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
994 for (i
= 0; i
< rec1
->n
; ++i
) {
995 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
996 isl_upoly_copy(rec1
->p
[i
]));
1001 for (; i
< size
; ++i
) {
1002 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
1007 for (i
= 0; i
< rec1
->n
; ++i
) {
1008 for (j
= 1; j
< rec2
->n
; ++j
) {
1009 struct isl_upoly
*up
;
1010 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
1011 isl_upoly_copy(rec1
->p
[i
]));
1012 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
1018 isl_upoly_free(up1
);
1019 isl_upoly_free(up2
);
1023 isl_upoly_free(up1
);
1024 isl_upoly_free(up2
);
1025 isl_upoly_free(&res
->up
);
1029 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
1030 __isl_take
struct isl_upoly
*up2
)
1035 if (isl_upoly_is_nan(up1
)) {
1036 isl_upoly_free(up2
);
1040 if (isl_upoly_is_nan(up2
)) {
1041 isl_upoly_free(up1
);
1045 if (isl_upoly_is_zero(up1
)) {
1046 isl_upoly_free(up2
);
1050 if (isl_upoly_is_zero(up2
)) {
1051 isl_upoly_free(up1
);
1055 if (isl_upoly_is_one(up1
)) {
1056 isl_upoly_free(up1
);
1060 if (isl_upoly_is_one(up2
)) {
1061 isl_upoly_free(up2
);
1065 if (up1
->var
< up2
->var
)
1066 return isl_upoly_mul(up2
, up1
);
1068 if (up2
->var
< up1
->var
) {
1070 struct isl_upoly_rec
*rec
;
1071 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
1072 isl_ctx
*ctx
= up1
->ctx
;
1073 isl_upoly_free(up1
);
1074 isl_upoly_free(up2
);
1075 return isl_upoly_nan(ctx
);
1077 up1
= isl_upoly_cow(up1
);
1078 rec
= isl_upoly_as_rec(up1
);
1082 for (i
= 0; i
< rec
->n
; ++i
) {
1083 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
1084 isl_upoly_copy(up2
));
1088 isl_upoly_free(up2
);
1092 if (isl_upoly_is_cst(up1
))
1093 return isl_upoly_mul_cst(up1
, up2
);
1095 return isl_upoly_mul_rec(up1
, up2
);
1097 isl_upoly_free(up1
);
1098 isl_upoly_free(up2
);
1102 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
1105 struct isl_upoly
*res
;
1113 res
= isl_upoly_copy(up
);
1115 res
= isl_upoly_one(up
->ctx
);
1117 while (power
>>= 1) {
1118 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
1120 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
1127 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*dim
,
1128 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1130 struct isl_qpolynomial
*qp
= NULL
;
1136 if (!isl_space_is_set(dim
))
1137 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1138 "domain of polynomial should be a set", goto error
);
1140 total
= isl_space_dim(dim
, isl_dim_all
);
1142 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
1147 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1156 isl_space_free(dim
);
1158 isl_qpolynomial_free(qp
);
1162 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1171 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1173 struct isl_qpolynomial
*dup
;
1178 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1179 isl_upoly_copy(qp
->upoly
));
1182 isl_mat_free(dup
->div
);
1183 dup
->div
= isl_mat_copy(qp
->div
);
1189 isl_qpolynomial_free(dup
);
1193 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1201 return isl_qpolynomial_dup(qp
);
1204 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1205 __isl_take isl_qpolynomial
*qp
)
1213 isl_space_free(qp
->dim
);
1214 isl_mat_free(qp
->div
);
1215 isl_upoly_free(qp
->upoly
);
1221 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1224 struct isl_upoly_rec
*rec
;
1225 struct isl_upoly_cst
*cst
;
1227 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1230 for (i
= 0; i
< 1 + power
; ++i
) {
1231 rec
->p
[i
] = isl_upoly_zero(ctx
);
1236 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1237 isl_int_set_si(cst
->n
, 1);
1241 isl_upoly_free(&rec
->up
);
1245 /* r array maps original positions to new positions.
1247 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1251 struct isl_upoly_rec
*rec
;
1252 struct isl_upoly
*base
;
1253 struct isl_upoly
*res
;
1255 if (isl_upoly_is_cst(up
))
1258 rec
= isl_upoly_as_rec(up
);
1262 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1264 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1265 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1267 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1268 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1269 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1272 isl_upoly_free(base
);
1281 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1282 __isl_keep isl_mat
*div2
)
1287 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1288 div1
->n_col
>= div2
->n_col
,
1289 return isl_bool_error
);
1291 if (div1
->n_row
== div2
->n_row
)
1292 return isl_mat_is_equal(div1
, div2
);
1294 n_row
= div1
->n_row
;
1295 n_col
= div1
->n_col
;
1296 div1
->n_row
= div2
->n_row
;
1297 div1
->n_col
= div2
->n_col
;
1299 equal
= isl_mat_is_equal(div1
, div2
);
1301 div1
->n_row
= n_row
;
1302 div1
->n_col
= n_col
;
1307 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1311 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1312 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1317 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1320 struct isl_div_sort_info
{
1325 static int div_sort_cmp(const void *p1
, const void *p2
)
1327 const struct isl_div_sort_info
*i1
, *i2
;
1328 i1
= (const struct isl_div_sort_info
*) p1
;
1329 i2
= (const struct isl_div_sort_info
*) p2
;
1331 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1334 /* Sort divs and remove duplicates.
1336 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1341 struct isl_div_sort_info
*array
= NULL
;
1342 int *pos
= NULL
, *at
= NULL
;
1343 int *reordering
= NULL
;
1348 if (qp
->div
->n_row
<= 1)
1351 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1353 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1355 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1356 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1357 len
= qp
->div
->n_col
- 2;
1358 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1359 if (!array
|| !pos
|| !at
|| !reordering
)
1362 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1363 array
[i
].div
= qp
->div
;
1369 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1372 for (i
= 0; i
< div_pos
; ++i
)
1375 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1376 if (pos
[array
[i
].row
] == i
)
1378 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1379 pos
[at
[i
]] = pos
[array
[i
].row
];
1380 at
[pos
[array
[i
].row
]] = at
[i
];
1381 at
[i
] = array
[i
].row
;
1382 pos
[array
[i
].row
] = i
;
1386 for (i
= 0; i
< len
- div_pos
; ++i
) {
1388 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1389 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1390 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1391 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1392 2 + div_pos
+ i
- skip
);
1393 qp
->div
= isl_mat_drop_cols(qp
->div
,
1394 2 + div_pos
+ i
- skip
, 1);
1397 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1400 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1402 if (!qp
->upoly
|| !qp
->div
)
1416 isl_qpolynomial_free(qp
);
1420 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1421 int *exp
, int first
)
1424 struct isl_upoly_rec
*rec
;
1426 if (isl_upoly_is_cst(up
))
1429 if (up
->var
< first
)
1432 if (exp
[up
->var
- first
] == up
->var
- first
)
1435 up
= isl_upoly_cow(up
);
1439 up
->var
= exp
[up
->var
- first
] + first
;
1441 rec
= isl_upoly_as_rec(up
);
1445 for (i
= 0; i
< rec
->n
; ++i
) {
1446 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1457 static __isl_give isl_qpolynomial
*with_merged_divs(
1458 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1459 __isl_take isl_qpolynomial
*qp2
),
1460 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1464 isl_mat
*div
= NULL
;
1467 qp1
= isl_qpolynomial_cow(qp1
);
1468 qp2
= isl_qpolynomial_cow(qp2
);
1473 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1474 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1476 n_div1
= qp1
->div
->n_row
;
1477 n_div2
= qp2
->div
->n_row
;
1478 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1479 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1480 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1483 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1487 isl_mat_free(qp1
->div
);
1488 qp1
->div
= isl_mat_copy(div
);
1489 isl_mat_free(qp2
->div
);
1490 qp2
->div
= isl_mat_copy(div
);
1492 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1493 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1495 if (!qp1
->upoly
|| !qp2
->upoly
)
1502 return fn(qp1
, qp2
);
1507 isl_qpolynomial_free(qp1
);
1508 isl_qpolynomial_free(qp2
);
1512 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1513 __isl_take isl_qpolynomial
*qp2
)
1515 isl_bool compatible
;
1517 qp1
= isl_qpolynomial_cow(qp1
);
1522 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1523 return isl_qpolynomial_add(qp2
, qp1
);
1525 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1526 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1530 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1532 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1536 isl_qpolynomial_free(qp2
);
1540 isl_qpolynomial_free(qp1
);
1541 isl_qpolynomial_free(qp2
);
1545 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1546 __isl_keep isl_set
*dom
,
1547 __isl_take isl_qpolynomial
*qp1
,
1548 __isl_take isl_qpolynomial
*qp2
)
1550 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1551 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1555 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1556 __isl_take isl_qpolynomial
*qp2
)
1558 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1561 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1562 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1564 if (isl_int_is_zero(v
))
1567 qp
= isl_qpolynomial_cow(qp
);
1571 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1577 isl_qpolynomial_free(qp
);
1582 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1587 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1590 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1591 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1593 if (isl_int_is_one(v
))
1596 if (qp
&& isl_int_is_zero(v
)) {
1597 isl_qpolynomial
*zero
;
1598 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1599 isl_qpolynomial_free(qp
);
1603 qp
= isl_qpolynomial_cow(qp
);
1607 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1613 isl_qpolynomial_free(qp
);
1617 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1618 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1620 return isl_qpolynomial_mul_isl_int(qp
, v
);
1623 /* Multiply "qp" by "v".
1625 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1626 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1631 if (!isl_val_is_rat(v
))
1632 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1633 "expecting rational factor", goto error
);
1635 if (isl_val_is_one(v
)) {
1640 if (isl_val_is_zero(v
)) {
1643 space
= isl_qpolynomial_get_domain_space(qp
);
1644 isl_qpolynomial_free(qp
);
1646 return isl_qpolynomial_zero_on_domain(space
);
1649 qp
= isl_qpolynomial_cow(qp
);
1653 qp
->upoly
= isl_upoly_scale_val(qp
->upoly
, v
);
1655 qp
= isl_qpolynomial_free(qp
);
1661 isl_qpolynomial_free(qp
);
1665 /* Divide "qp" by "v".
1667 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1668 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1673 if (!isl_val_is_rat(v
))
1674 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1675 "expecting rational factor", goto error
);
1676 if (isl_val_is_zero(v
))
1677 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1678 "cannot scale down by zero", goto error
);
1680 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1683 isl_qpolynomial_free(qp
);
1687 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1688 __isl_take isl_qpolynomial
*qp2
)
1690 isl_bool compatible
;
1692 qp1
= isl_qpolynomial_cow(qp1
);
1697 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1698 return isl_qpolynomial_mul(qp2
, qp1
);
1700 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1701 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1705 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1707 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1711 isl_qpolynomial_free(qp2
);
1715 isl_qpolynomial_free(qp1
);
1716 isl_qpolynomial_free(qp2
);
1720 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1723 qp
= isl_qpolynomial_cow(qp
);
1728 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1734 isl_qpolynomial_free(qp
);
1738 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1739 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1746 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1750 for (i
= 0; i
< pwqp
->n
; ++i
) {
1751 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1753 return isl_pw_qpolynomial_free(pwqp
);
1759 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1760 __isl_take isl_space
*dim
)
1764 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1767 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1768 __isl_take isl_space
*dim
)
1772 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1775 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1776 __isl_take isl_space
*dim
)
1780 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1783 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1784 __isl_take isl_space
*dim
)
1788 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1791 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1792 __isl_take isl_space
*dim
)
1796 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1799 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1800 __isl_take isl_space
*dim
,
1803 struct isl_qpolynomial
*qp
;
1804 struct isl_upoly_cst
*cst
;
1809 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1813 cst
= isl_upoly_as_cst(qp
->upoly
);
1814 isl_int_set(cst
->n
, v
);
1819 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1820 isl_int
*n
, isl_int
*d
)
1822 struct isl_upoly_cst
*cst
;
1827 if (!isl_upoly_is_cst(qp
->upoly
))
1830 cst
= isl_upoly_as_cst(qp
->upoly
);
1835 isl_int_set(*n
, cst
->n
);
1837 isl_int_set(*d
, cst
->d
);
1842 /* Return the constant term of "up".
1844 static __isl_give isl_val
*isl_upoly_get_constant_val(
1845 __isl_keep
struct isl_upoly
*up
)
1847 struct isl_upoly_cst
*cst
;
1852 while (!isl_upoly_is_cst(up
)) {
1853 struct isl_upoly_rec
*rec
;
1855 rec
= isl_upoly_as_rec(up
);
1861 cst
= isl_upoly_as_cst(up
);
1864 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1867 /* Return the constant term of "qp".
1869 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1870 __isl_keep isl_qpolynomial
*qp
)
1875 return isl_upoly_get_constant_val(qp
->upoly
);
1878 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1881 struct isl_upoly_rec
*rec
;
1889 rec
= isl_upoly_as_rec(up
);
1896 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1898 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1904 return isl_upoly_is_affine(rec
->p
[0]);
1907 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1912 if (qp
->div
->n_row
> 0)
1915 return isl_upoly_is_affine(qp
->upoly
);
1918 static void update_coeff(__isl_keep isl_vec
*aff
,
1919 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1924 if (isl_int_is_zero(cst
->n
))
1929 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1930 isl_int_divexact(f
, cst
->d
, gcd
);
1931 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1932 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1933 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1938 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1939 __isl_keep isl_vec
*aff
)
1941 struct isl_upoly_cst
*cst
;
1942 struct isl_upoly_rec
*rec
;
1948 struct isl_upoly_cst
*cst
;
1950 cst
= isl_upoly_as_cst(up
);
1953 update_coeff(aff
, cst
, 0);
1957 rec
= isl_upoly_as_rec(up
);
1960 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1962 cst
= isl_upoly_as_cst(rec
->p
[1]);
1965 update_coeff(aff
, cst
, 1 + up
->var
);
1967 return isl_upoly_update_affine(rec
->p
[0], aff
);
1970 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1971 __isl_keep isl_qpolynomial
*qp
)
1979 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1980 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1984 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1985 isl_int_set_si(aff
->el
[0], 1);
1987 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1996 /* Compare two quasi-polynomials.
1998 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
1999 * than "qp2" and 0 if they are equal.
2001 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2002 __isl_keep isl_qpolynomial
*qp2
)
2013 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2017 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2021 return isl_upoly_plain_cmp(qp1
->upoly
, qp2
->upoly
);
2024 /* Is "qp1" obviously equal to "qp2"?
2026 * NaN is not equal to anything, not even to another NaN.
2028 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2029 __isl_keep isl_qpolynomial
*qp2
)
2034 return isl_bool_error
;
2036 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2037 return isl_bool_false
;
2039 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2040 if (equal
< 0 || !equal
)
2043 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2044 if (equal
< 0 || !equal
)
2047 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
2050 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
2053 struct isl_upoly_rec
*rec
;
2055 if (isl_upoly_is_cst(up
)) {
2056 struct isl_upoly_cst
*cst
;
2057 cst
= isl_upoly_as_cst(up
);
2060 isl_int_lcm(*d
, *d
, cst
->d
);
2064 rec
= isl_upoly_as_rec(up
);
2068 for (i
= 0; i
< rec
->n
; ++i
)
2069 upoly_update_den(rec
->p
[i
], d
);
2072 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
2074 isl_int_set_si(*d
, 1);
2077 upoly_update_den(qp
->upoly
, d
);
2080 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2081 __isl_take isl_space
*dim
, int pos
, int power
)
2083 struct isl_ctx
*ctx
;
2090 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
2093 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(__isl_take isl_space
*dim
,
2094 enum isl_dim_type type
, unsigned pos
)
2099 isl_assert(dim
->ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, goto error
);
2100 isl_assert(dim
->ctx
, pos
< isl_space_dim(dim
, type
), goto error
);
2102 if (type
== isl_dim_set
)
2103 pos
+= isl_space_dim(dim
, isl_dim_param
);
2105 return isl_qpolynomial_var_pow_on_domain(dim
, pos
, 1);
2107 isl_space_free(dim
);
2111 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2112 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2115 struct isl_upoly_rec
*rec
;
2116 struct isl_upoly
*base
, *res
;
2121 if (isl_upoly_is_cst(up
))
2124 if (up
->var
< first
)
2127 rec
= isl_upoly_as_rec(up
);
2131 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2133 if (up
->var
>= first
+ n
)
2134 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2136 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2138 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2139 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2140 struct isl_upoly
*t
;
2141 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2142 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2143 res
= isl_upoly_sum(res
, t
);
2146 isl_upoly_free(base
);
2155 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2156 isl_int denom
, unsigned len
)
2159 struct isl_upoly
*up
;
2161 isl_assert(ctx
, len
>= 1, return NULL
);
2163 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2164 for (i
= 0; i
< len
- 1; ++i
) {
2165 struct isl_upoly
*t
;
2166 struct isl_upoly
*c
;
2168 if (isl_int_is_zero(f
[1 + i
]))
2171 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2172 t
= isl_upoly_var_pow(ctx
, i
, 1);
2173 t
= isl_upoly_mul(c
, t
);
2174 up
= isl_upoly_sum(up
, t
);
2180 /* Remove common factor of non-constant terms and denominator.
2182 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2184 isl_ctx
*ctx
= qp
->div
->ctx
;
2185 unsigned total
= qp
->div
->n_col
- 2;
2187 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2188 isl_int_gcd(ctx
->normalize_gcd
,
2189 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2190 if (isl_int_is_one(ctx
->normalize_gcd
))
2193 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2194 ctx
->normalize_gcd
, total
);
2195 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2196 ctx
->normalize_gcd
);
2197 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2198 ctx
->normalize_gcd
);
2201 /* Replace the integer division identified by "div" by the polynomial "s".
2202 * The integer division is assumed not to appear in the definition
2203 * of any other integer divisions.
2205 static __isl_give isl_qpolynomial
*substitute_div(
2206 __isl_take isl_qpolynomial
*qp
,
2207 int div
, __isl_take
struct isl_upoly
*s
)
2216 qp
= isl_qpolynomial_cow(qp
);
2220 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2221 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2225 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2228 for (i
= 0; i
< total
+ div
; ++i
)
2230 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2231 reordering
[i
] = i
- 1;
2232 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2233 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2234 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2237 if (!qp
->upoly
|| !qp
->div
)
2243 isl_qpolynomial_free(qp
);
2248 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2249 * divisions because d is equal to 1 by their definition, i.e., e.
2251 static __isl_give isl_qpolynomial
*substitute_non_divs(
2252 __isl_take isl_qpolynomial
*qp
)
2256 struct isl_upoly
*s
;
2261 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2262 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2263 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2265 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2266 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2268 isl_seq_combine(qp
->div
->row
[j
] + 1,
2269 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2270 qp
->div
->row
[j
][2 + total
+ i
],
2271 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2272 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2273 normalize_div(qp
, j
);
2275 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2276 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2277 qp
= substitute_div(qp
, i
, s
);
2284 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2285 * with d the denominator. When replacing the coefficient e of x by
2286 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2287 * inside the division, so we need to add floor(e/d) * x outside.
2288 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2289 * to adjust the coefficient of x in each later div that depends on the
2290 * current div "div" and also in the affine expressions in the rows of "mat"
2291 * (if they too depend on "div").
2293 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2294 __isl_keep isl_mat
**mat
)
2298 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2301 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2302 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2303 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2305 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2306 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2307 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2308 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2309 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2310 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2312 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2313 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2319 /* Check if the last non-zero coefficient is bigger that half of the
2320 * denominator. If so, we will invert the div to further reduce the number
2321 * of distinct divs that may appear.
2322 * If the last non-zero coefficient is exactly half the denominator,
2323 * then we continue looking for earlier coefficients that are bigger
2324 * than half the denominator.
2326 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2331 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2332 if (isl_int_is_zero(div
->row
[row
][i
]))
2334 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2335 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2336 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2346 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2347 * We only invert the coefficients of e (and the coefficient of q in
2348 * later divs and in the rows of "mat"). After calling this function, the
2349 * coefficients of e should be reduced again.
2351 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2352 __isl_keep isl_mat
**mat
)
2354 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2356 isl_seq_neg(qp
->div
->row
[div
] + 1,
2357 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2358 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2359 isl_int_add(qp
->div
->row
[div
][1],
2360 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2361 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2362 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2363 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2366 /* Reduce all divs of "qp" to have coefficients
2367 * in the interval [0, d-1], with d the denominator and such that the
2368 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2369 * The modifications to the integer divisions need to be reflected
2370 * in the factors of the polynomial that refer to the original
2371 * integer divisions. To this end, the modifications are collected
2372 * as a set of affine expressions and then plugged into the polynomial.
2374 * After the reduction, some divs may have become redundant or identical,
2375 * so we call substitute_non_divs and sort_divs. If these functions
2376 * eliminate divs or merge two or more divs into one, the coefficients
2377 * of the enclosing divs may have to be reduced again, so we call
2378 * ourselves recursively if the number of divs decreases.
2380 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2385 struct isl_upoly
**s
;
2386 unsigned o_div
, n_div
, total
;
2391 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2392 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2393 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2394 ctx
= isl_qpolynomial_get_ctx(qp
);
2395 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2397 for (i
= 0; i
< n_div
; ++i
)
2398 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2400 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2401 normalize_div(qp
, i
);
2402 reduce_div(qp
, i
, &mat
);
2403 if (needs_invert(qp
->div
, i
)) {
2404 invert_div(qp
, i
, &mat
);
2405 reduce_div(qp
, i
, &mat
);
2411 s
= isl_alloc_array(ctx
, struct isl_upoly
*, n_div
);
2414 for (i
= 0; i
< n_div
; ++i
)
2415 s
[i
] = isl_upoly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2417 qp
->upoly
= isl_upoly_subs(qp
->upoly
, o_div
- 1, n_div
, s
);
2418 for (i
= 0; i
< n_div
; ++i
)
2419 isl_upoly_free(s
[i
]);
2426 qp
= substitute_non_divs(qp
);
2428 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2429 return reduce_divs(qp
);
2433 isl_qpolynomial_free(qp
);
2438 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2439 __isl_take isl_space
*dim
, const isl_int n
, const isl_int d
)
2441 struct isl_qpolynomial
*qp
;
2442 struct isl_upoly_cst
*cst
;
2447 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2451 cst
= isl_upoly_as_cst(qp
->upoly
);
2452 isl_int_set(cst
->n
, n
);
2453 isl_int_set(cst
->d
, d
);
2458 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2460 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2461 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2463 isl_qpolynomial
*qp
;
2464 struct isl_upoly_cst
*cst
;
2466 if (!domain
|| !val
)
2469 qp
= isl_qpolynomial_alloc(isl_space_copy(domain
), 0,
2470 isl_upoly_zero(domain
->ctx
));
2474 cst
= isl_upoly_as_cst(qp
->upoly
);
2475 isl_int_set(cst
->n
, val
->n
);
2476 isl_int_set(cst
->d
, val
->d
);
2478 isl_space_free(domain
);
2482 isl_space_free(domain
);
2487 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2489 struct isl_upoly_rec
*rec
;
2495 if (isl_upoly_is_cst(up
))
2499 active
[up
->var
] = 1;
2501 rec
= isl_upoly_as_rec(up
);
2502 for (i
= 0; i
< rec
->n
; ++i
)
2503 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2509 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2512 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2517 for (i
= 0; i
< d
; ++i
)
2518 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2519 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2525 return up_set_active(qp
->upoly
, active
, d
);
2528 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2529 enum isl_dim_type type
, unsigned first
, unsigned n
)
2533 isl_bool involves
= isl_bool_false
;
2536 return isl_bool_error
;
2538 return isl_bool_false
;
2540 isl_assert(qp
->dim
->ctx
,
2541 first
+ n
<= isl_qpolynomial_dim(qp
, type
),
2542 return isl_bool_error
);
2543 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2544 type
== isl_dim_in
, return isl_bool_error
);
2546 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2547 isl_space_dim(qp
->dim
, isl_dim_all
));
2548 if (set_active(qp
, active
) < 0)
2551 if (type
== isl_dim_in
)
2552 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2553 for (i
= 0; i
< n
; ++i
)
2554 if (active
[first
+ i
]) {
2555 involves
= isl_bool_true
;
2564 return isl_bool_error
;
2567 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2568 * of the divs that do appear in the quasi-polynomial.
2570 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2571 __isl_take isl_qpolynomial
*qp
)
2578 int *reordering
= NULL
;
2585 if (qp
->div
->n_row
== 0)
2588 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2589 len
= qp
->div
->n_col
- 2;
2590 ctx
= isl_qpolynomial_get_ctx(qp
);
2591 active
= isl_calloc_array(ctx
, int, len
);
2595 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2598 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2599 if (!active
[d
+ i
]) {
2603 for (j
= 0; j
< i
; ++j
) {
2604 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2616 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2620 for (i
= 0; i
< d
; ++i
)
2624 n_div
= qp
->div
->n_row
;
2625 for (i
= 0; i
< n_div
; ++i
) {
2626 if (!active
[d
+ i
]) {
2627 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2628 qp
->div
= isl_mat_drop_cols(qp
->div
,
2629 2 + d
+ i
- skip
, 1);
2632 reordering
[d
+ i
] = d
+ i
- skip
;
2635 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2637 if (!qp
->upoly
|| !qp
->div
)
2647 isl_qpolynomial_free(qp
);
2651 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2652 unsigned first
, unsigned n
)
2655 struct isl_upoly_rec
*rec
;
2659 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2661 if (up
->var
< first
+ n
) {
2662 up
= replace_by_constant_term(up
);
2663 return isl_upoly_drop(up
, first
, n
);
2665 up
= isl_upoly_cow(up
);
2669 rec
= isl_upoly_as_rec(up
);
2673 for (i
= 0; i
< rec
->n
; ++i
) {
2674 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2685 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2686 __isl_take isl_qpolynomial
*qp
,
2687 enum isl_dim_type type
, unsigned pos
, const char *s
)
2689 qp
= isl_qpolynomial_cow(qp
);
2692 if (type
== isl_dim_out
)
2693 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2694 "cannot set name of output/set dimension",
2695 return isl_qpolynomial_free(qp
));
2696 if (type
== isl_dim_in
)
2698 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2703 isl_qpolynomial_free(qp
);
2707 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2708 __isl_take isl_qpolynomial
*qp
,
2709 enum isl_dim_type type
, unsigned first
, unsigned n
)
2713 if (type
== isl_dim_out
)
2714 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2715 "cannot drop output/set dimension",
2717 if (type
== isl_dim_in
)
2719 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2722 qp
= isl_qpolynomial_cow(qp
);
2726 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2728 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2729 type
== isl_dim_set
, goto error
);
2731 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2735 if (type
== isl_dim_set
)
2736 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2738 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2742 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2748 isl_qpolynomial_free(qp
);
2752 /* Project the domain of the quasi-polynomial onto its parameter space.
2753 * The quasi-polynomial may not involve any of the domain dimensions.
2755 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2756 __isl_take isl_qpolynomial
*qp
)
2762 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2763 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2765 return isl_qpolynomial_free(qp
);
2767 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2768 "polynomial involves some of the domain dimensions",
2769 return isl_qpolynomial_free(qp
));
2770 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2771 space
= isl_qpolynomial_get_domain_space(qp
);
2772 space
= isl_space_params(space
);
2773 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2777 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2778 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2784 struct isl_upoly
*up
;
2788 if (eq
->n_eq
== 0) {
2789 isl_basic_set_free(eq
);
2793 qp
= isl_qpolynomial_cow(qp
);
2796 qp
->div
= isl_mat_cow(qp
->div
);
2800 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2802 isl_int_init(denom
);
2803 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2804 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2805 if (j
< 0 || j
== 0 || j
>= total
)
2808 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2809 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2811 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2812 &qp
->div
->row
[k
][0]);
2813 normalize_div(qp
, k
);
2816 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2817 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2818 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2819 isl_int_set_si(eq
->eq
[i
][j
], 0);
2821 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2822 eq
->eq
[i
], denom
, total
);
2823 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2826 isl_int_clear(denom
);
2831 isl_basic_set_free(eq
);
2833 qp
= substitute_non_divs(qp
);
2838 isl_basic_set_free(eq
);
2839 isl_qpolynomial_free(qp
);
2843 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2845 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2846 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2850 if (qp
->div
->n_row
> 0)
2851 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2852 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2854 isl_basic_set_free(eq
);
2855 isl_qpolynomial_free(qp
);
2859 static __isl_give isl_basic_set
*add_div_constraints(
2860 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2868 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2871 total
= isl_basic_set_total_dim(bset
);
2872 for (i
= 0; i
< div
->n_row
; ++i
)
2873 if (isl_basic_set_add_div_constraints_var(bset
,
2874 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2881 isl_basic_set_free(bset
);
2885 /* Look for equalities among the variables shared by context and qp
2886 * and the integer divisions of qp, if any.
2887 * The equalities are then used to eliminate variables and/or integer
2888 * divisions from qp.
2890 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2891 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2897 if (qp
->div
->n_row
> 0) {
2898 isl_basic_set
*bset
;
2899 context
= isl_set_add_dims(context
, isl_dim_set
,
2901 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2902 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2903 context
= isl_set_intersect(context
,
2904 isl_set_from_basic_set(bset
));
2907 aff
= isl_set_affine_hull(context
);
2908 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2910 isl_qpolynomial_free(qp
);
2911 isl_set_free(context
);
2915 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2916 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2918 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2919 isl_set
*dom_context
= isl_set_universe(space
);
2920 dom_context
= isl_set_intersect_params(dom_context
, context
);
2921 return isl_qpolynomial_gist(qp
, dom_context
);
2924 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2925 __isl_take isl_qpolynomial
*qp
)
2931 if (isl_qpolynomial_is_zero(qp
)) {
2932 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2933 isl_qpolynomial_free(qp
);
2934 return isl_pw_qpolynomial_zero(dim
);
2937 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2938 return isl_pw_qpolynomial_alloc(dom
, qp
);
2941 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2944 #define PW isl_pw_qpolynomial
2946 #define EL isl_qpolynomial
2948 #define EL_IS_ZERO is_zero
2952 #define IS_ZERO is_zero
2955 #undef DEFAULT_IS_ZERO
2956 #define DEFAULT_IS_ZERO 1
2960 #include <isl_pw_templ.c>
2963 #define UNION isl_union_pw_qpolynomial
2965 #define PART isl_pw_qpolynomial
2967 #define PARTS pw_qpolynomial
2969 #include <isl_union_single.c>
2970 #include <isl_union_eval.c>
2971 #include <isl_union_neg.c>
2973 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2981 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2984 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2987 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2988 __isl_take isl_pw_qpolynomial
*pwqp1
,
2989 __isl_take isl_pw_qpolynomial
*pwqp2
)
2991 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2994 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2995 __isl_take isl_pw_qpolynomial
*pwqp1
,
2996 __isl_take isl_pw_qpolynomial
*pwqp2
)
2999 struct isl_pw_qpolynomial
*res
;
3001 if (!pwqp1
|| !pwqp2
)
3004 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3007 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3008 isl_pw_qpolynomial_free(pwqp2
);
3012 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3013 isl_pw_qpolynomial_free(pwqp1
);
3017 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3018 isl_pw_qpolynomial_free(pwqp1
);
3022 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3023 isl_pw_qpolynomial_free(pwqp2
);
3027 n
= pwqp1
->n
* pwqp2
->n
;
3028 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3030 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3031 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3032 struct isl_set
*common
;
3033 struct isl_qpolynomial
*prod
;
3034 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3035 isl_set_copy(pwqp2
->p
[j
].set
));
3036 if (isl_set_plain_is_empty(common
)) {
3037 isl_set_free(common
);
3041 prod
= isl_qpolynomial_mul(
3042 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3043 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3045 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3049 isl_pw_qpolynomial_free(pwqp1
);
3050 isl_pw_qpolynomial_free(pwqp2
);
3054 isl_pw_qpolynomial_free(pwqp1
);
3055 isl_pw_qpolynomial_free(pwqp2
);
3059 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
3060 __isl_take isl_vec
*vec
)
3063 struct isl_upoly_rec
*rec
;
3067 if (isl_upoly_is_cst(up
)) {
3069 res
= isl_upoly_get_constant_val(up
);
3074 rec
= isl_upoly_as_rec(up
);
3078 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
3080 base
= isl_val_rat_from_isl_int(up
->ctx
,
3081 vec
->el
[1 + up
->var
], vec
->el
[0]);
3083 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
3086 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3087 res
= isl_val_mul(res
, isl_val_copy(base
));
3088 res
= isl_val_add(res
,
3089 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
3090 isl_vec_copy(vec
)));
3103 /* Evaluate "qp" in the void point "pnt".
3104 * In particular, return the value NaN.
3106 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3107 __isl_take isl_point
*pnt
)
3111 ctx
= isl_point_get_ctx(pnt
);
3112 isl_qpolynomial_free(qp
);
3113 isl_point_free(pnt
);
3114 return isl_val_nan(ctx
);
3117 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3118 __isl_take isl_point
*pnt
)
3126 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3127 is_void
= isl_point_is_void(pnt
);
3131 return eval_void(qp
, pnt
);
3133 if (qp
->div
->n_row
== 0)
3134 ext
= isl_vec_copy(pnt
->vec
);
3137 unsigned dim
= isl_space_dim(qp
->dim
, isl_dim_all
);
3138 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
3142 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
3143 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
3144 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
3145 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
3146 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
3147 qp
->div
->row
[i
][0]);
3151 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
3153 isl_qpolynomial_free(qp
);
3154 isl_point_free(pnt
);
3158 isl_qpolynomial_free(qp
);
3159 isl_point_free(pnt
);
3163 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
3164 __isl_keep
struct isl_upoly_cst
*cst2
)
3169 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3170 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3171 cmp
= isl_int_sgn(t
);
3176 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3177 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3178 unsigned first
, unsigned n
)
3186 if (type
== isl_dim_out
)
3187 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3188 "cannot insert output/set dimensions",
3190 if (type
== isl_dim_in
)
3192 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3195 qp
= isl_qpolynomial_cow(qp
);
3199 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
3202 g_pos
= pos(qp
->dim
, type
) + first
;
3204 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3208 total
= qp
->div
->n_col
- 2;
3209 if (total
> g_pos
) {
3211 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3214 for (i
= 0; i
< total
- g_pos
; ++i
)
3216 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3222 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3228 isl_qpolynomial_free(qp
);
3232 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3233 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3237 pos
= isl_qpolynomial_dim(qp
, type
);
3239 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3242 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3243 __isl_take isl_pw_qpolynomial
*pwqp
,
3244 enum isl_dim_type type
, unsigned n
)
3248 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3250 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3253 static int *reordering_move(isl_ctx
*ctx
,
3254 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3259 reordering
= isl_alloc_array(ctx
, int, len
);
3264 for (i
= 0; i
< dst
; ++i
)
3266 for (i
= 0; i
< n
; ++i
)
3267 reordering
[src
+ i
] = dst
+ i
;
3268 for (i
= 0; i
< src
- dst
; ++i
)
3269 reordering
[dst
+ i
] = dst
+ n
+ i
;
3270 for (i
= 0; i
< len
- src
- n
; ++i
)
3271 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3273 for (i
= 0; i
< src
; ++i
)
3275 for (i
= 0; i
< n
; ++i
)
3276 reordering
[src
+ i
] = dst
+ i
;
3277 for (i
= 0; i
< dst
- src
; ++i
)
3278 reordering
[src
+ n
+ i
] = src
+ i
;
3279 for (i
= 0; i
< len
- dst
- n
; ++i
)
3280 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3286 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3287 __isl_take isl_qpolynomial
*qp
,
3288 enum isl_dim_type dst_type
, unsigned dst_pos
,
3289 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3298 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3299 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3300 "cannot move output/set dimension",
3302 if (dst_type
== isl_dim_in
)
3303 dst_type
= isl_dim_set
;
3304 if (src_type
== isl_dim_in
)
3305 src_type
= isl_dim_set
;
3308 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3309 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3312 qp
= isl_qpolynomial_cow(qp
);
3316 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3319 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3320 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3321 if (dst_type
> src_type
)
3324 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3331 reordering
= reordering_move(qp
->dim
->ctx
,
3332 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3336 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3341 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3347 isl_qpolynomial_free(qp
);
3351 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_space
*dim
,
3352 isl_int
*f
, isl_int denom
)
3354 struct isl_upoly
*up
;
3356 dim
= isl_space_domain(dim
);
3360 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
,
3361 1 + isl_space_dim(dim
, isl_dim_all
));
3363 return isl_qpolynomial_alloc(dim
, 0, up
);
3366 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3369 struct isl_upoly
*up
;
3370 isl_qpolynomial
*qp
;
3375 ctx
= isl_aff_get_ctx(aff
);
3376 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3379 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3380 aff
->ls
->div
->n_row
, up
);
3384 isl_mat_free(qp
->div
);
3385 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3386 qp
->div
= isl_mat_cow(qp
->div
);
3391 qp
= reduce_divs(qp
);
3392 qp
= remove_redundant_divs(qp
);
3396 return isl_qpolynomial_free(qp
);
3399 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3400 __isl_take isl_pw_aff
*pwaff
)
3403 isl_pw_qpolynomial
*pwqp
;
3408 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3411 for (i
= 0; i
< pwaff
->n
; ++i
) {
3413 isl_qpolynomial
*qp
;
3415 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3416 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3417 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3420 isl_pw_aff_free(pwaff
);
3424 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3425 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3429 aff
= isl_constraint_get_bound(c
, type
, pos
);
3430 isl_constraint_free(c
);
3431 return isl_qpolynomial_from_aff(aff
);
3434 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3435 * in "qp" by subs[i].
3437 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3438 __isl_take isl_qpolynomial
*qp
,
3439 enum isl_dim_type type
, unsigned first
, unsigned n
,
3440 __isl_keep isl_qpolynomial
**subs
)
3443 struct isl_upoly
**ups
;
3448 qp
= isl_qpolynomial_cow(qp
);
3452 if (type
== isl_dim_out
)
3453 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3454 "cannot substitute output/set dimension",
3456 if (type
== isl_dim_in
)
3459 for (i
= 0; i
< n
; ++i
)
3463 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3466 for (i
= 0; i
< n
; ++i
)
3467 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3470 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3471 for (i
= 0; i
< n
; ++i
)
3472 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3474 first
+= pos(qp
->dim
, type
);
3476 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3479 for (i
= 0; i
< n
; ++i
)
3480 ups
[i
] = subs
[i
]->upoly
;
3482 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3491 isl_qpolynomial_free(qp
);
3495 /* Extend "bset" with extra set dimensions for each integer division
3496 * in "qp" and then call "fn" with the extended bset and the polynomial
3497 * that results from replacing each of the integer divisions by the
3498 * corresponding extra set dimension.
3500 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3501 __isl_keep isl_basic_set
*bset
,
3502 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3503 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3507 isl_qpolynomial
*poly
;
3510 return isl_stat_error
;
3511 if (qp
->div
->n_row
== 0)
3512 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3515 div
= isl_mat_copy(qp
->div
);
3516 dim
= isl_space_copy(qp
->dim
);
3517 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3518 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3519 bset
= isl_basic_set_copy(bset
);
3520 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, qp
->div
->n_row
);
3521 bset
= add_div_constraints(bset
, div
);
3523 return fn(bset
, poly
, user
);
3526 /* Return total degree in variables first (inclusive) up to last (exclusive).
3528 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3532 struct isl_upoly_rec
*rec
;
3536 if (isl_upoly_is_zero(up
))
3538 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3541 rec
= isl_upoly_as_rec(up
);
3545 for (i
= 0; i
< rec
->n
; ++i
) {
3548 if (isl_upoly_is_zero(rec
->p
[i
]))
3550 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3560 /* Return total degree in set variables.
3562 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3570 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3571 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3572 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3575 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3576 unsigned pos
, int deg
)
3579 struct isl_upoly_rec
*rec
;
3584 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3586 return isl_upoly_copy(up
);
3588 return isl_upoly_zero(up
->ctx
);
3591 rec
= isl_upoly_as_rec(up
);
3595 if (up
->var
== pos
) {
3597 return isl_upoly_copy(rec
->p
[deg
]);
3599 return isl_upoly_zero(up
->ctx
);
3602 up
= isl_upoly_copy(up
);
3603 up
= isl_upoly_cow(up
);
3604 rec
= isl_upoly_as_rec(up
);
3608 for (i
= 0; i
< rec
->n
; ++i
) {
3609 struct isl_upoly
*t
;
3610 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3613 isl_upoly_free(rec
->p
[i
]);
3623 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3625 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3626 __isl_keep isl_qpolynomial
*qp
,
3627 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3630 struct isl_upoly
*up
;
3636 if (type
== isl_dim_out
)
3637 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3638 "output/set dimension does not have a coefficient",
3640 if (type
== isl_dim_in
)
3643 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3646 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3647 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3649 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3652 isl_mat_free(c
->div
);
3653 c
->div
= isl_mat_copy(qp
->div
);
3658 isl_qpolynomial_free(c
);
3662 /* Homogenize the polynomial in the variables first (inclusive) up to
3663 * last (exclusive) by inserting powers of variable first.
3664 * Variable first is assumed not to appear in the input.
3666 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3667 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3668 int first
, int last
)
3671 struct isl_upoly_rec
*rec
;
3675 if (isl_upoly_is_zero(up
))
3679 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3680 struct isl_upoly
*hom
;
3682 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3685 rec
= isl_upoly_as_rec(hom
);
3686 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3691 up
= isl_upoly_cow(up
);
3692 rec
= isl_upoly_as_rec(up
);
3696 for (i
= 0; i
< rec
->n
; ++i
) {
3697 if (isl_upoly_is_zero(rec
->p
[i
]))
3699 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3700 up
->var
< last
? deg
+ i
: i
, target
,
3712 /* Homogenize the polynomial in the set variables by introducing
3713 * powers of an extra set variable at position 0.
3715 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3716 __isl_take isl_qpolynomial
*poly
)
3720 int deg
= isl_qpolynomial_degree(poly
);
3725 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3726 poly
= isl_qpolynomial_cow(poly
);
3730 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3731 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3732 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3739 isl_qpolynomial_free(poly
);
3743 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*dim
,
3744 __isl_take isl_mat
*div
)
3752 n
= isl_space_dim(dim
, isl_dim_all
) + div
->n_row
;
3754 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3755 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3762 isl_int_init(term
->n
);
3763 isl_int_init(term
->d
);
3767 isl_space_free(dim
);
3772 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3781 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3790 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3792 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3796 isl_int_set(dup
->n
, term
->n
);
3797 isl_int_set(dup
->d
, term
->d
);
3799 for (i
= 0; i
< total
; ++i
)
3800 dup
->pow
[i
] = term
->pow
[i
];
3805 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3813 return isl_term_dup(term
);
3816 void isl_term_free(__isl_take isl_term
*term
)
3821 if (--term
->ref
> 0)
3824 isl_space_free(term
->dim
);
3825 isl_mat_free(term
->div
);
3826 isl_int_clear(term
->n
);
3827 isl_int_clear(term
->d
);
3831 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3839 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3840 case isl_dim_div
: return term
->div
->n_row
;
3841 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3847 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3849 return term
? term
->dim
->ctx
: NULL
;
3852 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3856 isl_int_set(*n
, term
->n
);
3859 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
3863 isl_int_set(*d
, term
->d
);
3866 /* Return the coefficient of the term "term".
3868 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3873 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3877 int isl_term_get_exp(__isl_keep isl_term
*term
,
3878 enum isl_dim_type type
, unsigned pos
)
3883 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3885 if (type
>= isl_dim_set
)
3886 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3887 if (type
>= isl_dim_div
)
3888 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3890 return term
->pow
[pos
];
3893 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3895 isl_local_space
*ls
;
3901 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3904 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3905 isl_mat_copy(term
->div
));
3906 aff
= isl_aff_alloc(ls
);
3910 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3912 aff
= isl_aff_normalize(aff
);
3917 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3918 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3919 __isl_take isl_term
*term
, void *user
)
3922 struct isl_upoly_rec
*rec
;
3927 if (isl_upoly_is_zero(up
))
3930 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3931 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3932 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3934 if (isl_upoly_is_cst(up
)) {
3935 struct isl_upoly_cst
*cst
;
3936 cst
= isl_upoly_as_cst(up
);
3939 term
= isl_term_cow(term
);
3942 isl_int_set(term
->n
, cst
->n
);
3943 isl_int_set(term
->d
, cst
->d
);
3944 if (fn(isl_term_copy(term
), user
) < 0)
3949 rec
= isl_upoly_as_rec(up
);
3953 for (i
= 0; i
< rec
->n
; ++i
) {
3954 term
= isl_term_cow(term
);
3957 term
->pow
[up
->var
] = i
;
3958 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3962 term
->pow
[up
->var
] = 0;
3966 isl_term_free(term
);
3970 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3971 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3976 return isl_stat_error
;
3978 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3980 return isl_stat_error
;
3982 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3984 isl_term_free(term
);
3986 return term
? isl_stat_ok
: isl_stat_error
;
3989 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3991 struct isl_upoly
*up
;
3992 isl_qpolynomial
*qp
;
3998 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
4000 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
4001 for (i
= 0; i
< n
; ++i
) {
4004 up
= isl_upoly_mul(up
,
4005 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4008 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
4011 isl_mat_free(qp
->div
);
4012 qp
->div
= isl_mat_copy(term
->div
);
4016 isl_term_free(term
);
4019 isl_qpolynomial_free(qp
);
4020 isl_term_free(term
);
4024 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4025 __isl_take isl_space
*dim
)
4034 if (isl_space_is_equal(qp
->dim
, dim
)) {
4035 isl_space_free(dim
);
4039 qp
= isl_qpolynomial_cow(qp
);
4043 extra
= isl_space_dim(dim
, isl_dim_set
) -
4044 isl_space_dim(qp
->dim
, isl_dim_set
);
4045 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4046 if (qp
->div
->n_row
) {
4049 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4052 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4054 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
4059 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4062 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4063 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4065 isl_space_free(qp
->dim
);
4070 isl_space_free(dim
);
4071 isl_qpolynomial_free(qp
);
4075 /* For each parameter or variable that does not appear in qp,
4076 * first eliminate the variable from all constraints and then set it to zero.
4078 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4079 __isl_keep isl_qpolynomial
*qp
)
4090 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4091 active
= isl_calloc_array(set
->ctx
, int, d
);
4092 if (set_active(qp
, active
) < 0)
4095 for (i
= 0; i
< d
; ++i
)
4104 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4105 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4106 for (i
= 0; i
< nparam
; ++i
) {
4109 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4110 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4112 for (i
= 0; i
< nvar
; ++i
) {
4113 if (active
[nparam
+ i
])
4115 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4116 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4128 struct isl_opt_data
{
4129 isl_qpolynomial
*qp
;
4135 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4137 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4140 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4144 } else if (data
->max
) {
4145 data
->opt
= isl_val_max(data
->opt
, val
);
4147 data
->opt
= isl_val_min(data
->opt
, val
);
4153 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4154 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4156 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4161 if (isl_upoly_is_cst(qp
->upoly
)) {
4163 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4164 isl_qpolynomial_free(qp
);
4168 set
= fix_inactive(set
, qp
);
4171 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4175 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4178 isl_qpolynomial_free(qp
);
4182 isl_qpolynomial_free(qp
);
4183 isl_val_free(data
.opt
);
4187 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4188 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4193 struct isl_upoly
**subs
;
4194 isl_mat
*mat
, *diag
;
4196 qp
= isl_qpolynomial_cow(qp
);
4201 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4203 n_sub
= morph
->inv
->n_row
- 1;
4204 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4205 n_sub
+= qp
->div
->n_row
;
4206 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4210 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4211 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4212 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4213 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4214 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4215 subs
[morph
->inv
->n_row
- 1 + i
] =
4216 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4218 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4220 for (i
= 0; i
< n_sub
; ++i
)
4221 isl_upoly_free(subs
[i
]);
4224 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4225 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4226 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4227 mat
= isl_mat_diagonal(mat
, diag
);
4228 qp
->div
= isl_mat_product(qp
->div
, mat
);
4229 isl_space_free(qp
->dim
);
4230 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4232 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4235 isl_morph_free(morph
);
4239 isl_qpolynomial_free(qp
);
4240 isl_morph_free(morph
);
4244 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4245 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4246 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4248 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4249 &isl_pw_qpolynomial_mul
);
4252 /* Reorder the columns of the given div definitions according to the
4255 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
4256 __isl_take isl_reordering
*r
)
4265 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
4266 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
4270 for (i
= 0; i
< div
->n_row
; ++i
) {
4271 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
4272 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
4273 for (j
= 0; j
< r
->len
; ++j
)
4274 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
4275 div
->row
[i
][2 + j
]);
4278 isl_reordering_free(r
);
4282 isl_reordering_free(r
);
4287 /* Reorder the dimension of "qp" according to the given reordering.
4289 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4290 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4292 qp
= isl_qpolynomial_cow(qp
);
4296 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4300 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
4304 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4308 qp
= isl_qpolynomial_reset_domain_space(qp
, isl_space_copy(r
->dim
));
4310 isl_reordering_free(r
);
4313 isl_qpolynomial_free(qp
);
4314 isl_reordering_free(r
);
4318 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4319 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4321 isl_bool equal_params
;
4326 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4327 if (equal_params
< 0)
4329 if (!equal_params
) {
4330 isl_reordering
*exp
;
4332 model
= isl_space_drop_dims(model
, isl_dim_in
,
4333 0, isl_space_dim(model
, isl_dim_in
));
4334 model
= isl_space_drop_dims(model
, isl_dim_out
,
4335 0, isl_space_dim(model
, isl_dim_out
));
4336 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4337 exp
= isl_reordering_extend_space(exp
,
4338 isl_qpolynomial_get_domain_space(qp
));
4339 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4342 isl_space_free(model
);
4345 isl_space_free(model
);
4346 isl_qpolynomial_free(qp
);
4350 struct isl_split_periods_data
{
4352 isl_pw_qpolynomial
*res
;
4355 /* Create a slice where the integer division "div" has the fixed value "v".
4356 * In particular, if "div" refers to floor(f/m), then create a slice
4358 * m v <= f <= m v + (m - 1)
4363 * -f + m v + (m - 1) >= 0
4365 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*dim
,
4366 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4369 isl_basic_set
*bset
= NULL
;
4375 total
= isl_space_dim(dim
, isl_dim_all
);
4376 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0, 0, 2);
4378 k
= isl_basic_set_alloc_inequality(bset
);
4381 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4382 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4384 k
= isl_basic_set_alloc_inequality(bset
);
4387 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4388 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4389 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4390 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4392 isl_space_free(dim
);
4393 return isl_set_from_basic_set(bset
);
4395 isl_basic_set_free(bset
);
4396 isl_space_free(dim
);
4400 static isl_stat
split_periods(__isl_take isl_set
*set
,
4401 __isl_take isl_qpolynomial
*qp
, void *user
);
4403 /* Create a slice of the domain "set" such that integer division "div"
4404 * has the fixed value "v" and add the results to data->res,
4405 * replacing the integer division by "v" in "qp".
4407 static isl_stat
set_div(__isl_take isl_set
*set
,
4408 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4409 struct isl_split_periods_data
*data
)
4414 struct isl_upoly
*cst
;
4416 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4417 set
= isl_set_intersect(set
, slice
);
4422 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4424 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4425 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4427 isl_int_addmul(qp
->div
->row
[i
][1],
4428 qp
->div
->row
[i
][2 + total
+ div
], v
);
4429 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4432 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4433 qp
= substitute_div(qp
, div
, cst
);
4435 return split_periods(set
, qp
, data
);
4438 isl_qpolynomial_free(qp
);
4442 /* Split the domain "set" such that integer division "div"
4443 * has a fixed value (ranging from "min" to "max") on each slice
4444 * and add the results to data->res.
4446 static isl_stat
split_div(__isl_take isl_set
*set
,
4447 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4448 struct isl_split_periods_data
*data
)
4450 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4451 isl_set
*set_i
= isl_set_copy(set
);
4452 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4454 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4458 isl_qpolynomial_free(qp
);
4462 isl_qpolynomial_free(qp
);
4463 return isl_stat_error
;
4466 /* If "qp" refers to any integer division
4467 * that can only attain "max_periods" distinct values on "set"
4468 * then split the domain along those distinct values.
4469 * Add the results (or the original if no splitting occurs)
4472 static isl_stat
split_periods(__isl_take isl_set
*set
,
4473 __isl_take isl_qpolynomial
*qp
, void *user
)
4476 isl_pw_qpolynomial
*pwqp
;
4477 struct isl_split_periods_data
*data
;
4480 isl_stat r
= isl_stat_ok
;
4482 data
= (struct isl_split_periods_data
*)user
;
4487 if (qp
->div
->n_row
== 0) {
4488 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4489 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4495 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4496 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4497 enum isl_lp_result lp_res
;
4499 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4500 qp
->div
->n_row
) != -1)
4503 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4504 set
->ctx
->one
, &min
, NULL
, NULL
);
4505 if (lp_res
== isl_lp_error
)
4507 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4509 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4511 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4512 set
->ctx
->one
, &max
, NULL
, NULL
);
4513 if (lp_res
== isl_lp_error
)
4515 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4517 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4519 isl_int_sub(max
, max
, min
);
4520 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4521 isl_int_add(max
, max
, min
);
4526 if (i
< qp
->div
->n_row
) {
4527 r
= split_div(set
, qp
, i
, min
, max
, data
);
4529 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4530 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4542 isl_qpolynomial_free(qp
);
4543 return isl_stat_error
;
4546 /* If any quasi-polynomial in pwqp refers to any integer division
4547 * that can only attain "max_periods" distinct values on its domain
4548 * then split the domain along those distinct values.
4550 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4551 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4553 struct isl_split_periods_data data
;
4555 data
.max_periods
= max_periods
;
4556 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4558 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4561 isl_pw_qpolynomial_free(pwqp
);
4565 isl_pw_qpolynomial_free(data
.res
);
4566 isl_pw_qpolynomial_free(pwqp
);
4570 /* Construct a piecewise quasipolynomial that is constant on the given
4571 * domain. In particular, it is
4574 * infinity if cst == -1
4576 * If cst == -1, then explicitly check whether the domain is empty and,
4577 * if so, return 0 instead.
4579 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4580 __isl_take isl_basic_set
*bset
, int cst
)
4583 isl_qpolynomial
*qp
;
4585 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4590 bset
= isl_basic_set_params(bset
);
4591 dim
= isl_basic_set_get_space(bset
);
4593 qp
= isl_qpolynomial_infty_on_domain(dim
);
4595 qp
= isl_qpolynomial_zero_on_domain(dim
);
4597 qp
= isl_qpolynomial_one_on_domain(dim
);
4598 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4601 /* Factor bset, call fn on each of the factors and return the product.
4603 * If no factors can be found, simply call fn on the input.
4604 * Otherwise, construct the factors based on the factorizer,
4605 * call fn on each factor and compute the product.
4607 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4608 __isl_take isl_basic_set
*bset
,
4609 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4615 isl_qpolynomial
*qp
;
4616 isl_pw_qpolynomial
*pwqp
;
4620 f
= isl_basic_set_factorizer(bset
);
4623 if (f
->n_group
== 0) {
4624 isl_factorizer_free(f
);
4628 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4629 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4631 space
= isl_basic_set_get_space(bset
);
4632 space
= isl_space_params(space
);
4633 set
= isl_set_universe(isl_space_copy(space
));
4634 qp
= isl_qpolynomial_one_on_domain(space
);
4635 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4637 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4639 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4640 isl_basic_set
*bset_i
;
4641 isl_pw_qpolynomial
*pwqp_i
;
4643 bset_i
= isl_basic_set_copy(bset
);
4644 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4645 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4646 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4648 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4649 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4650 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4652 pwqp_i
= fn(bset_i
);
4653 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4658 isl_basic_set_free(bset
);
4659 isl_factorizer_free(f
);
4663 isl_basic_set_free(bset
);
4667 /* Factor bset, call fn on each of the factors and return the product.
4668 * The function is assumed to evaluate to zero on empty domains,
4669 * to one on zero-dimensional domains and to infinity on unbounded domains
4670 * and will not be called explicitly on zero-dimensional or unbounded domains.
4672 * We first check for some special cases and remove all equalities.
4673 * Then we hand over control to compressed_multiplicative_call.
4675 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4676 __isl_take isl_basic_set
*bset
,
4677 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4681 isl_pw_qpolynomial
*pwqp
;
4686 if (isl_basic_set_plain_is_empty(bset
))
4687 return constant_on_domain(bset
, 0);
4689 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4690 return constant_on_domain(bset
, 1);
4692 bounded
= isl_basic_set_is_bounded(bset
);
4696 return constant_on_domain(bset
, -1);
4698 if (bset
->n_eq
== 0)
4699 return compressed_multiplicative_call(bset
, fn
);
4701 morph
= isl_basic_set_full_compression(bset
);
4702 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4704 pwqp
= compressed_multiplicative_call(bset
, fn
);
4706 morph
= isl_morph_dom_params(morph
);
4707 morph
= isl_morph_ran_params(morph
);
4708 morph
= isl_morph_inverse(morph
);
4710 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4714 isl_basic_set_free(bset
);
4718 /* Drop all floors in "qp", turning each integer division [a/m] into
4719 * a rational division a/m. If "down" is set, then the integer division
4720 * is replaced by (a-(m-1))/m instead.
4722 static __isl_give isl_qpolynomial
*qp_drop_floors(
4723 __isl_take isl_qpolynomial
*qp
, int down
)
4726 struct isl_upoly
*s
;
4730 if (qp
->div
->n_row
== 0)
4733 qp
= isl_qpolynomial_cow(qp
);
4737 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4739 isl_int_sub(qp
->div
->row
[i
][1],
4740 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4741 isl_int_add_ui(qp
->div
->row
[i
][1],
4742 qp
->div
->row
[i
][1], 1);
4744 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4745 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4746 qp
= substitute_div(qp
, i
, s
);
4754 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4755 * a rational division a/m.
4757 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4758 __isl_take isl_pw_qpolynomial
*pwqp
)
4765 if (isl_pw_qpolynomial_is_zero(pwqp
))
4768 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4772 for (i
= 0; i
< pwqp
->n
; ++i
) {
4773 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4780 isl_pw_qpolynomial_free(pwqp
);
4784 /* Adjust all the integer divisions in "qp" such that they are at least
4785 * one over the given orthant (identified by "signs"). This ensures
4786 * that they will still be non-negative even after subtracting (m-1)/m.
4788 * In particular, f is replaced by f' + v, changing f = [a/m]
4789 * to f' = [(a - m v)/m].
4790 * If the constant term k in a is smaller than m,
4791 * the constant term of v is set to floor(k/m) - 1.
4792 * For any other term, if the coefficient c and the variable x have
4793 * the same sign, then no changes are needed.
4794 * Otherwise, if the variable is positive (and c is negative),
4795 * then the coefficient of x in v is set to floor(c/m).
4796 * If the variable is negative (and c is positive),
4797 * then the coefficient of x in v is set to ceil(c/m).
4799 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4805 struct isl_upoly
*s
;
4807 qp
= isl_qpolynomial_cow(qp
);
4810 qp
->div
= isl_mat_cow(qp
->div
);
4814 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4815 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4817 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4818 isl_int
*row
= qp
->div
->row
[i
];
4822 if (isl_int_lt(row
[1], row
[0])) {
4823 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4824 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4825 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4827 for (j
= 0; j
< total
; ++j
) {
4828 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4831 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4833 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4834 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4836 for (j
= 0; j
< i
; ++j
) {
4837 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4839 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4840 row
[2 + total
+ j
], row
[0]);
4841 isl_int_submul(row
[2 + total
+ j
],
4842 row
[0], v
->el
[1 + total
+ j
]);
4844 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4845 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4847 isl_seq_combine(qp
->div
->row
[j
] + 1,
4848 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4849 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4851 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4852 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4853 qp
->div
->ctx
->one
, v
->size
);
4854 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4864 isl_qpolynomial_free(qp
);
4868 struct isl_to_poly_data
{
4870 isl_pw_qpolynomial
*res
;
4871 isl_qpolynomial
*qp
;
4874 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4875 * We first make all integer divisions positive and then split the
4876 * quasipolynomials into terms with sign data->sign (the direction
4877 * of the requested approximation) and terms with the opposite sign.
4878 * In the first set of terms, each integer division [a/m] is
4879 * overapproximated by a/m, while in the second it is underapproximated
4882 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4883 int *signs
, void *user
)
4885 struct isl_to_poly_data
*data
= user
;
4886 isl_pw_qpolynomial
*t
;
4887 isl_qpolynomial
*qp
, *up
, *down
;
4889 qp
= isl_qpolynomial_copy(data
->qp
);
4890 qp
= make_divs_pos(qp
, signs
);
4892 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4893 up
= qp_drop_floors(up
, 0);
4894 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4895 down
= qp_drop_floors(down
, 1);
4897 isl_qpolynomial_free(qp
);
4898 qp
= isl_qpolynomial_add(up
, down
);
4900 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4901 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4906 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4907 * the polynomial will be an overapproximation. If "sign" is negative,
4908 * it will be an underapproximation. If "sign" is zero, the approximation
4909 * will lie somewhere in between.
4911 * In particular, is sign == 0, we simply drop the floors, turning
4912 * the integer divisions into rational divisions.
4913 * Otherwise, we split the domains into orthants, make all integer divisions
4914 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4915 * depending on the requested sign and the sign of the term in which
4916 * the integer division appears.
4918 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4919 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4922 struct isl_to_poly_data data
;
4925 return pwqp_drop_floors(pwqp
);
4931 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4933 for (i
= 0; i
< pwqp
->n
; ++i
) {
4934 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4935 isl_pw_qpolynomial
*t
;
4936 t
= isl_pw_qpolynomial_alloc(
4937 isl_set_copy(pwqp
->p
[i
].set
),
4938 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4939 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4942 data
.qp
= pwqp
->p
[i
].qp
;
4943 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4944 &to_polynomial_on_orthant
, &data
) < 0)
4948 isl_pw_qpolynomial_free(pwqp
);
4952 isl_pw_qpolynomial_free(pwqp
);
4953 isl_pw_qpolynomial_free(data
.res
);
4957 static __isl_give isl_pw_qpolynomial
*poly_entry(
4958 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4962 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4965 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4966 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4968 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4969 &poly_entry
, &sign
);
4972 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4973 __isl_take isl_qpolynomial
*qp
)
4977 isl_vec
*aff
= NULL
;
4978 isl_basic_map
*bmap
= NULL
;
4984 if (!isl_upoly_is_affine(qp
->upoly
))
4985 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4986 "input quasi-polynomial not affine", goto error
);
4987 aff
= isl_qpolynomial_extract_affine(qp
);
4990 dim
= isl_qpolynomial_get_space(qp
);
4991 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4992 n_div
= qp
->div
->n_row
;
4993 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4995 for (i
= 0; i
< n_div
; ++i
) {
4996 k
= isl_basic_map_alloc_div(bmap
);
4999 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
5000 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
5001 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
5004 k
= isl_basic_map_alloc_equality(bmap
);
5007 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5008 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5009 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5012 isl_qpolynomial_free(qp
);
5013 bmap
= isl_basic_map_finalize(bmap
);
5017 isl_qpolynomial_free(qp
);
5018 isl_basic_map_free(bmap
);