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>
32 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
35 case isl_dim_param
: return 0;
36 case isl_dim_in
: return dim
->nparam
;
37 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
42 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
50 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
55 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
57 return (struct isl_upoly_cst
*)up
;
60 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
65 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
67 return (struct isl_upoly_rec
*)up
;
70 /* Compare two polynomials.
72 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
73 * than "up2" and 0 if they are equal.
75 static int isl_upoly_plain_cmp(__isl_keep
struct isl_upoly
*up1
,
76 __isl_keep
struct isl_upoly
*up2
)
79 struct isl_upoly_rec
*rec1
, *rec2
;
87 if (up1
->var
!= up2
->var
)
88 return up1
->var
- up2
->var
;
90 if (isl_upoly_is_cst(up1
)) {
91 struct isl_upoly_cst
*cst1
, *cst2
;
94 cst1
= isl_upoly_as_cst(up1
);
95 cst2
= isl_upoly_as_cst(up2
);
98 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
101 return isl_int_cmp(cst1
->d
, cst2
->d
);
104 rec1
= isl_upoly_as_rec(up1
);
105 rec2
= isl_upoly_as_rec(up2
);
109 if (rec1
->n
!= rec2
->n
)
110 return rec1
->n
- rec2
->n
;
112 for (i
= 0; i
< rec1
->n
; ++i
) {
113 int cmp
= isl_upoly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
121 isl_bool
isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
122 __isl_keep
struct isl_upoly
*up2
)
125 struct isl_upoly_rec
*rec1
, *rec2
;
128 return isl_bool_error
;
130 return isl_bool_true
;
131 if (up1
->var
!= up2
->var
)
132 return isl_bool_false
;
133 if (isl_upoly_is_cst(up1
)) {
134 struct isl_upoly_cst
*cst1
, *cst2
;
135 cst1
= isl_upoly_as_cst(up1
);
136 cst2
= isl_upoly_as_cst(up2
);
138 return isl_bool_error
;
139 return isl_int_eq(cst1
->n
, cst2
->n
) &&
140 isl_int_eq(cst1
->d
, cst2
->d
);
143 rec1
= isl_upoly_as_rec(up1
);
144 rec2
= isl_upoly_as_rec(up2
);
146 return isl_bool_error
;
148 if (rec1
->n
!= rec2
->n
)
149 return isl_bool_false
;
151 for (i
= 0; i
< rec1
->n
; ++i
) {
152 isl_bool eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
157 return isl_bool_true
;
160 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
162 struct isl_upoly_cst
*cst
;
166 if (!isl_upoly_is_cst(up
))
169 cst
= isl_upoly_as_cst(up
);
173 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
176 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
178 struct isl_upoly_cst
*cst
;
182 if (!isl_upoly_is_cst(up
))
185 cst
= isl_upoly_as_cst(up
);
189 return isl_int_sgn(cst
->n
);
192 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
194 struct isl_upoly_cst
*cst
;
198 if (!isl_upoly_is_cst(up
))
201 cst
= isl_upoly_as_cst(up
);
205 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
208 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
210 struct isl_upoly_cst
*cst
;
214 if (!isl_upoly_is_cst(up
))
217 cst
= isl_upoly_as_cst(up
);
221 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
224 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
226 struct isl_upoly_cst
*cst
;
230 if (!isl_upoly_is_cst(up
))
233 cst
= isl_upoly_as_cst(up
);
237 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
240 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
242 struct isl_upoly_cst
*cst
;
246 if (!isl_upoly_is_cst(up
))
249 cst
= isl_upoly_as_cst(up
);
253 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
256 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
258 struct isl_upoly_cst
*cst
;
262 if (!isl_upoly_is_cst(up
))
265 cst
= isl_upoly_as_cst(up
);
269 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
272 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
274 struct isl_upoly_cst
*cst
;
276 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
285 isl_int_init(cst
->n
);
286 isl_int_init(cst
->d
);
291 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
293 struct isl_upoly_cst
*cst
;
295 cst
= isl_upoly_cst_alloc(ctx
);
299 isl_int_set_si(cst
->n
, 0);
300 isl_int_set_si(cst
->d
, 1);
305 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
307 struct isl_upoly_cst
*cst
;
309 cst
= isl_upoly_cst_alloc(ctx
);
313 isl_int_set_si(cst
->n
, 1);
314 isl_int_set_si(cst
->d
, 1);
319 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
321 struct isl_upoly_cst
*cst
;
323 cst
= isl_upoly_cst_alloc(ctx
);
327 isl_int_set_si(cst
->n
, 1);
328 isl_int_set_si(cst
->d
, 0);
333 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
335 struct isl_upoly_cst
*cst
;
337 cst
= isl_upoly_cst_alloc(ctx
);
341 isl_int_set_si(cst
->n
, -1);
342 isl_int_set_si(cst
->d
, 0);
347 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
349 struct isl_upoly_cst
*cst
;
351 cst
= isl_upoly_cst_alloc(ctx
);
355 isl_int_set_si(cst
->n
, 0);
356 isl_int_set_si(cst
->d
, 0);
361 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
362 isl_int n
, isl_int d
)
364 struct isl_upoly_cst
*cst
;
366 cst
= isl_upoly_cst_alloc(ctx
);
370 isl_int_set(cst
->n
, n
);
371 isl_int_set(cst
->d
, d
);
376 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
379 struct isl_upoly_rec
*rec
;
381 isl_assert(ctx
, var
>= 0, return NULL
);
382 isl_assert(ctx
, size
>= 0, return NULL
);
383 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
384 sizeof(struct isl_upoly_rec
) +
385 size
* sizeof(struct isl_upoly
*));
400 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
401 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
403 qp
= isl_qpolynomial_cow(qp
);
407 isl_space_free(qp
->dim
);
412 isl_qpolynomial_free(qp
);
417 /* Reset the space of "qp". This function is called from isl_pw_templ.c
418 * and doesn't know if the space of an element object is represented
419 * directly or through its domain. It therefore passes along both.
421 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
422 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
423 __isl_take isl_space
*domain
)
425 isl_space_free(space
);
426 return isl_qpolynomial_reset_domain_space(qp
, domain
);
429 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
431 return qp
? qp
->dim
->ctx
: NULL
;
434 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
435 __isl_keep isl_qpolynomial
*qp
)
437 return qp
? isl_space_copy(qp
->dim
) : NULL
;
440 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
445 space
= isl_space_copy(qp
->dim
);
446 space
= isl_space_from_domain(space
);
447 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
451 /* Return the number of variables of the given type in the domain of "qp".
453 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
454 enum isl_dim_type type
)
458 if (type
== isl_dim_div
)
459 return qp
->div
->n_row
;
460 if (type
== isl_dim_all
)
461 return isl_space_dim(qp
->dim
, isl_dim_all
) +
462 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
463 return isl_space_dim(qp
->dim
, type
);
466 /* Externally, an isl_qpolynomial has a map space, but internally, the
467 * ls field corresponds to the domain of that space.
469 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
470 enum isl_dim_type type
)
474 if (type
== isl_dim_out
)
476 if (type
== isl_dim_in
)
478 return isl_qpolynomial_domain_dim(qp
, type
);
481 /* Return the offset of the first coefficient of type "type" in
482 * the domain of "qp".
484 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
485 enum isl_dim_type type
)
494 return 1 + isl_space_offset(qp
->dim
, type
);
496 return 1 + isl_space_dim(qp
->dim
, isl_dim_all
);
502 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
504 return qp
? isl_upoly_is_zero(qp
->upoly
) : isl_bool_error
;
507 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
509 return qp
? isl_upoly_is_one(qp
->upoly
) : isl_bool_error
;
512 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
514 return qp
? isl_upoly_is_nan(qp
->upoly
) : isl_bool_error
;
517 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
519 return qp
? isl_upoly_is_infty(qp
->upoly
) : isl_bool_error
;
522 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
524 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : isl_bool_error
;
527 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
529 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
532 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
534 isl_int_clear(cst
->n
);
535 isl_int_clear(cst
->d
);
538 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
542 for (i
= 0; i
< rec
->n
; ++i
)
543 isl_upoly_free(rec
->p
[i
]);
546 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
555 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
557 struct isl_upoly_cst
*cst
;
558 struct isl_upoly_cst
*dup
;
560 cst
= isl_upoly_as_cst(up
);
564 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
567 isl_int_set(dup
->n
, cst
->n
);
568 isl_int_set(dup
->d
, cst
->d
);
573 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
576 struct isl_upoly_rec
*rec
;
577 struct isl_upoly_rec
*dup
;
579 rec
= isl_upoly_as_rec(up
);
583 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
587 for (i
= 0; i
< rec
->n
; ++i
) {
588 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
596 isl_upoly_free(&dup
->up
);
600 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
605 if (isl_upoly_is_cst(up
))
606 return isl_upoly_dup_cst(up
);
608 return isl_upoly_dup_rec(up
);
611 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
619 return isl_upoly_dup(up
);
622 __isl_null
struct isl_upoly
*isl_upoly_free(__isl_take
struct isl_upoly
*up
)
631 upoly_free_cst((struct isl_upoly_cst
*)up
);
633 upoly_free_rec((struct isl_upoly_rec
*)up
);
635 isl_ctx_deref(up
->ctx
);
640 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
645 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
646 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
647 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
648 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
653 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
654 __isl_take
struct isl_upoly
*up2
)
656 struct isl_upoly_cst
*cst1
;
657 struct isl_upoly_cst
*cst2
;
659 up1
= isl_upoly_cow(up1
);
663 cst1
= isl_upoly_as_cst(up1
);
664 cst2
= isl_upoly_as_cst(up2
);
666 if (isl_int_eq(cst1
->d
, cst2
->d
))
667 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
669 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
670 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
671 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
674 isl_upoly_cst_reduce(cst1
);
684 static __isl_give
struct isl_upoly
*replace_by_zero(
685 __isl_take
struct isl_upoly
*up
)
693 return isl_upoly_zero(ctx
);
696 static __isl_give
struct isl_upoly
*replace_by_constant_term(
697 __isl_take
struct isl_upoly
*up
)
699 struct isl_upoly_rec
*rec
;
700 struct isl_upoly
*cst
;
705 rec
= isl_upoly_as_rec(up
);
708 cst
= isl_upoly_copy(rec
->p
[0]);
716 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
717 __isl_take
struct isl_upoly
*up2
)
720 struct isl_upoly_rec
*rec1
, *rec2
;
725 if (isl_upoly_is_nan(up1
)) {
730 if (isl_upoly_is_nan(up2
)) {
735 if (isl_upoly_is_zero(up1
)) {
740 if (isl_upoly_is_zero(up2
)) {
745 if (up1
->var
< up2
->var
)
746 return isl_upoly_sum(up2
, up1
);
748 if (up2
->var
< up1
->var
) {
749 struct isl_upoly_rec
*rec
;
750 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
754 up1
= isl_upoly_cow(up1
);
755 rec
= isl_upoly_as_rec(up1
);
758 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
760 up1
= replace_by_constant_term(up1
);
764 if (isl_upoly_is_cst(up1
))
765 return isl_upoly_sum_cst(up1
, up2
);
767 rec1
= isl_upoly_as_rec(up1
);
768 rec2
= isl_upoly_as_rec(up2
);
772 if (rec1
->n
< rec2
->n
)
773 return isl_upoly_sum(up2
, up1
);
775 up1
= isl_upoly_cow(up1
);
776 rec1
= isl_upoly_as_rec(up1
);
780 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
781 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
782 isl_upoly_copy(rec2
->p
[i
]));
785 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
786 isl_upoly_free(rec1
->p
[i
]);
792 up1
= replace_by_zero(up1
);
793 else if (rec1
->n
== 1)
794 up1
= replace_by_constant_term(up1
);
805 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
806 __isl_take
struct isl_upoly
*up
, isl_int v
)
808 struct isl_upoly_cst
*cst
;
810 up
= isl_upoly_cow(up
);
814 cst
= isl_upoly_as_cst(up
);
816 isl_int_addmul(cst
->n
, cst
->d
, v
);
821 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
822 __isl_take
struct isl_upoly
*up
, isl_int v
)
824 struct isl_upoly_rec
*rec
;
829 if (isl_upoly_is_cst(up
))
830 return isl_upoly_cst_add_isl_int(up
, v
);
832 up
= isl_upoly_cow(up
);
833 rec
= isl_upoly_as_rec(up
);
837 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
847 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
848 __isl_take
struct isl_upoly
*up
, isl_int v
)
850 struct isl_upoly_cst
*cst
;
852 if (isl_upoly_is_zero(up
))
855 up
= isl_upoly_cow(up
);
859 cst
= isl_upoly_as_cst(up
);
861 isl_int_mul(cst
->n
, cst
->n
, v
);
866 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
867 __isl_take
struct isl_upoly
*up
, isl_int v
)
870 struct isl_upoly_rec
*rec
;
875 if (isl_upoly_is_cst(up
))
876 return isl_upoly_cst_mul_isl_int(up
, v
);
878 up
= isl_upoly_cow(up
);
879 rec
= isl_upoly_as_rec(up
);
883 for (i
= 0; i
< rec
->n
; ++i
) {
884 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
895 /* Multiply the constant polynomial "up" by "v".
897 static __isl_give
struct isl_upoly
*isl_upoly_cst_scale_val(
898 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
900 struct isl_upoly_cst
*cst
;
902 if (isl_upoly_is_zero(up
))
905 up
= isl_upoly_cow(up
);
909 cst
= isl_upoly_as_cst(up
);
911 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
912 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
913 isl_upoly_cst_reduce(cst
);
918 /* Multiply the polynomial "up" by "v".
920 static __isl_give
struct isl_upoly
*isl_upoly_scale_val(
921 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
924 struct isl_upoly_rec
*rec
;
929 if (isl_upoly_is_cst(up
))
930 return isl_upoly_cst_scale_val(up
, v
);
932 up
= isl_upoly_cow(up
);
933 rec
= isl_upoly_as_rec(up
);
937 for (i
= 0; i
< rec
->n
; ++i
) {
938 rec
->p
[i
] = isl_upoly_scale_val(rec
->p
[i
], v
);
949 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
950 __isl_take
struct isl_upoly
*up2
)
952 struct isl_upoly_cst
*cst1
;
953 struct isl_upoly_cst
*cst2
;
955 up1
= isl_upoly_cow(up1
);
959 cst1
= isl_upoly_as_cst(up1
);
960 cst2
= isl_upoly_as_cst(up2
);
962 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
963 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
965 isl_upoly_cst_reduce(cst1
);
975 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
976 __isl_take
struct isl_upoly
*up2
)
978 struct isl_upoly_rec
*rec1
;
979 struct isl_upoly_rec
*rec2
;
980 struct isl_upoly_rec
*res
= NULL
;
984 rec1
= isl_upoly_as_rec(up1
);
985 rec2
= isl_upoly_as_rec(up2
);
988 size
= rec1
->n
+ rec2
->n
- 1;
989 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
993 for (i
= 0; i
< rec1
->n
; ++i
) {
994 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
995 isl_upoly_copy(rec1
->p
[i
]));
1000 for (; i
< size
; ++i
) {
1001 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
1006 for (i
= 0; i
< rec1
->n
; ++i
) {
1007 for (j
= 1; j
< rec2
->n
; ++j
) {
1008 struct isl_upoly
*up
;
1009 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
1010 isl_upoly_copy(rec1
->p
[i
]));
1011 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
1017 isl_upoly_free(up1
);
1018 isl_upoly_free(up2
);
1022 isl_upoly_free(up1
);
1023 isl_upoly_free(up2
);
1024 isl_upoly_free(&res
->up
);
1028 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
1029 __isl_take
struct isl_upoly
*up2
)
1034 if (isl_upoly_is_nan(up1
)) {
1035 isl_upoly_free(up2
);
1039 if (isl_upoly_is_nan(up2
)) {
1040 isl_upoly_free(up1
);
1044 if (isl_upoly_is_zero(up1
)) {
1045 isl_upoly_free(up2
);
1049 if (isl_upoly_is_zero(up2
)) {
1050 isl_upoly_free(up1
);
1054 if (isl_upoly_is_one(up1
)) {
1055 isl_upoly_free(up1
);
1059 if (isl_upoly_is_one(up2
)) {
1060 isl_upoly_free(up2
);
1064 if (up1
->var
< up2
->var
)
1065 return isl_upoly_mul(up2
, up1
);
1067 if (up2
->var
< up1
->var
) {
1069 struct isl_upoly_rec
*rec
;
1070 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
1071 isl_ctx
*ctx
= up1
->ctx
;
1072 isl_upoly_free(up1
);
1073 isl_upoly_free(up2
);
1074 return isl_upoly_nan(ctx
);
1076 up1
= isl_upoly_cow(up1
);
1077 rec
= isl_upoly_as_rec(up1
);
1081 for (i
= 0; i
< rec
->n
; ++i
) {
1082 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
1083 isl_upoly_copy(up2
));
1087 isl_upoly_free(up2
);
1091 if (isl_upoly_is_cst(up1
))
1092 return isl_upoly_mul_cst(up1
, up2
);
1094 return isl_upoly_mul_rec(up1
, up2
);
1096 isl_upoly_free(up1
);
1097 isl_upoly_free(up2
);
1101 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
1104 struct isl_upoly
*res
;
1112 res
= isl_upoly_copy(up
);
1114 res
= isl_upoly_one(up
->ctx
);
1116 while (power
>>= 1) {
1117 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
1119 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
1126 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*dim
,
1127 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1129 struct isl_qpolynomial
*qp
= NULL
;
1135 if (!isl_space_is_set(dim
))
1136 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1137 "domain of polynomial should be a set", goto error
);
1139 total
= isl_space_dim(dim
, isl_dim_all
);
1141 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
1146 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1155 isl_space_free(dim
);
1157 isl_qpolynomial_free(qp
);
1161 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1170 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1172 struct isl_qpolynomial
*dup
;
1177 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1178 isl_upoly_copy(qp
->upoly
));
1181 isl_mat_free(dup
->div
);
1182 dup
->div
= isl_mat_copy(qp
->div
);
1188 isl_qpolynomial_free(dup
);
1192 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1200 return isl_qpolynomial_dup(qp
);
1203 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1204 __isl_take isl_qpolynomial
*qp
)
1212 isl_space_free(qp
->dim
);
1213 isl_mat_free(qp
->div
);
1214 isl_upoly_free(qp
->upoly
);
1220 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1223 struct isl_upoly_rec
*rec
;
1224 struct isl_upoly_cst
*cst
;
1226 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1229 for (i
= 0; i
< 1 + power
; ++i
) {
1230 rec
->p
[i
] = isl_upoly_zero(ctx
);
1235 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1236 isl_int_set_si(cst
->n
, 1);
1240 isl_upoly_free(&rec
->up
);
1244 /* r array maps original positions to new positions.
1246 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1250 struct isl_upoly_rec
*rec
;
1251 struct isl_upoly
*base
;
1252 struct isl_upoly
*res
;
1254 if (isl_upoly_is_cst(up
))
1257 rec
= isl_upoly_as_rec(up
);
1261 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1263 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1264 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1266 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1267 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1268 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1271 isl_upoly_free(base
);
1280 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1281 __isl_keep isl_mat
*div2
)
1286 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1287 div1
->n_col
>= div2
->n_col
,
1288 return isl_bool_error
);
1290 if (div1
->n_row
== div2
->n_row
)
1291 return isl_mat_is_equal(div1
, div2
);
1293 n_row
= div1
->n_row
;
1294 n_col
= div1
->n_col
;
1295 div1
->n_row
= div2
->n_row
;
1296 div1
->n_col
= div2
->n_col
;
1298 equal
= isl_mat_is_equal(div1
, div2
);
1300 div1
->n_row
= n_row
;
1301 div1
->n_col
= n_col
;
1306 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1310 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1311 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1316 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1319 struct isl_div_sort_info
{
1324 static int div_sort_cmp(const void *p1
, const void *p2
)
1326 const struct isl_div_sort_info
*i1
, *i2
;
1327 i1
= (const struct isl_div_sort_info
*) p1
;
1328 i2
= (const struct isl_div_sort_info
*) p2
;
1330 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1333 /* Sort divs and remove duplicates.
1335 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1340 struct isl_div_sort_info
*array
= NULL
;
1341 int *pos
= NULL
, *at
= NULL
;
1342 int *reordering
= NULL
;
1347 if (qp
->div
->n_row
<= 1)
1350 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1352 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1354 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1355 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1356 len
= qp
->div
->n_col
- 2;
1357 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1358 if (!array
|| !pos
|| !at
|| !reordering
)
1361 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1362 array
[i
].div
= qp
->div
;
1368 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1371 for (i
= 0; i
< div_pos
; ++i
)
1374 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1375 if (pos
[array
[i
].row
] == i
)
1377 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1378 pos
[at
[i
]] = pos
[array
[i
].row
];
1379 at
[pos
[array
[i
].row
]] = at
[i
];
1380 at
[i
] = array
[i
].row
;
1381 pos
[array
[i
].row
] = i
;
1385 for (i
= 0; i
< len
- div_pos
; ++i
) {
1387 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1388 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1389 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1390 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1391 2 + div_pos
+ i
- skip
);
1392 qp
->div
= isl_mat_drop_cols(qp
->div
,
1393 2 + div_pos
+ i
- skip
, 1);
1396 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1399 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1401 if (!qp
->upoly
|| !qp
->div
)
1415 isl_qpolynomial_free(qp
);
1419 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1420 int *exp
, int first
)
1423 struct isl_upoly_rec
*rec
;
1425 if (isl_upoly_is_cst(up
))
1428 if (up
->var
< first
)
1431 if (exp
[up
->var
- first
] == up
->var
- first
)
1434 up
= isl_upoly_cow(up
);
1438 up
->var
= exp
[up
->var
- first
] + first
;
1440 rec
= isl_upoly_as_rec(up
);
1444 for (i
= 0; i
< rec
->n
; ++i
) {
1445 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1456 static __isl_give isl_qpolynomial
*with_merged_divs(
1457 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1458 __isl_take isl_qpolynomial
*qp2
),
1459 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1463 isl_mat
*div
= NULL
;
1466 qp1
= isl_qpolynomial_cow(qp1
);
1467 qp2
= isl_qpolynomial_cow(qp2
);
1472 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1473 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1475 n_div1
= qp1
->div
->n_row
;
1476 n_div2
= qp2
->div
->n_row
;
1477 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1478 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1479 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1482 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1486 isl_mat_free(qp1
->div
);
1487 qp1
->div
= isl_mat_copy(div
);
1488 isl_mat_free(qp2
->div
);
1489 qp2
->div
= isl_mat_copy(div
);
1491 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1492 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1494 if (!qp1
->upoly
|| !qp2
->upoly
)
1501 return fn(qp1
, qp2
);
1506 isl_qpolynomial_free(qp1
);
1507 isl_qpolynomial_free(qp2
);
1511 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1512 __isl_take isl_qpolynomial
*qp2
)
1514 isl_bool compatible
;
1516 qp1
= isl_qpolynomial_cow(qp1
);
1521 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1522 return isl_qpolynomial_add(qp2
, qp1
);
1524 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1525 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1529 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1531 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1535 isl_qpolynomial_free(qp2
);
1539 isl_qpolynomial_free(qp1
);
1540 isl_qpolynomial_free(qp2
);
1544 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1545 __isl_keep isl_set
*dom
,
1546 __isl_take isl_qpolynomial
*qp1
,
1547 __isl_take isl_qpolynomial
*qp2
)
1549 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1550 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1554 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1555 __isl_take isl_qpolynomial
*qp2
)
1557 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1560 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1561 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1563 if (isl_int_is_zero(v
))
1566 qp
= isl_qpolynomial_cow(qp
);
1570 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1576 isl_qpolynomial_free(qp
);
1581 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1586 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1589 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1590 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1592 if (isl_int_is_one(v
))
1595 if (qp
&& isl_int_is_zero(v
)) {
1596 isl_qpolynomial
*zero
;
1597 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1598 isl_qpolynomial_free(qp
);
1602 qp
= isl_qpolynomial_cow(qp
);
1606 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1612 isl_qpolynomial_free(qp
);
1616 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1617 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1619 return isl_qpolynomial_mul_isl_int(qp
, v
);
1622 /* Multiply "qp" by "v".
1624 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1625 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1630 if (!isl_val_is_rat(v
))
1631 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1632 "expecting rational factor", goto error
);
1634 if (isl_val_is_one(v
)) {
1639 if (isl_val_is_zero(v
)) {
1642 space
= isl_qpolynomial_get_domain_space(qp
);
1643 isl_qpolynomial_free(qp
);
1645 return isl_qpolynomial_zero_on_domain(space
);
1648 qp
= isl_qpolynomial_cow(qp
);
1652 qp
->upoly
= isl_upoly_scale_val(qp
->upoly
, v
);
1654 qp
= isl_qpolynomial_free(qp
);
1660 isl_qpolynomial_free(qp
);
1664 /* Divide "qp" by "v".
1666 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1667 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1672 if (!isl_val_is_rat(v
))
1673 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1674 "expecting rational factor", goto error
);
1675 if (isl_val_is_zero(v
))
1676 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1677 "cannot scale down by zero", goto error
);
1679 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1682 isl_qpolynomial_free(qp
);
1686 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1687 __isl_take isl_qpolynomial
*qp2
)
1689 isl_bool compatible
;
1691 qp1
= isl_qpolynomial_cow(qp1
);
1696 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1697 return isl_qpolynomial_mul(qp2
, qp1
);
1699 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1700 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1704 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1706 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1710 isl_qpolynomial_free(qp2
);
1714 isl_qpolynomial_free(qp1
);
1715 isl_qpolynomial_free(qp2
);
1719 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1722 qp
= isl_qpolynomial_cow(qp
);
1727 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1733 isl_qpolynomial_free(qp
);
1737 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1738 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1745 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1749 for (i
= 0; i
< pwqp
->n
; ++i
) {
1750 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1752 return isl_pw_qpolynomial_free(pwqp
);
1758 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1759 __isl_take isl_space
*dim
)
1763 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1766 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1767 __isl_take isl_space
*dim
)
1771 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_one(dim
->ctx
));
1774 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1775 __isl_take isl_space
*dim
)
1779 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1782 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1783 __isl_take isl_space
*dim
)
1787 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1790 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1791 __isl_take isl_space
*dim
)
1795 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1798 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1799 __isl_take isl_space
*dim
,
1802 struct isl_qpolynomial
*qp
;
1803 struct isl_upoly_cst
*cst
;
1808 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1812 cst
= isl_upoly_as_cst(qp
->upoly
);
1813 isl_int_set(cst
->n
, v
);
1818 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1819 isl_int
*n
, isl_int
*d
)
1821 struct isl_upoly_cst
*cst
;
1826 if (!isl_upoly_is_cst(qp
->upoly
))
1829 cst
= isl_upoly_as_cst(qp
->upoly
);
1834 isl_int_set(*n
, cst
->n
);
1836 isl_int_set(*d
, cst
->d
);
1841 /* Return the constant term of "up".
1843 static __isl_give isl_val
*isl_upoly_get_constant_val(
1844 __isl_keep
struct isl_upoly
*up
)
1846 struct isl_upoly_cst
*cst
;
1851 while (!isl_upoly_is_cst(up
)) {
1852 struct isl_upoly_rec
*rec
;
1854 rec
= isl_upoly_as_rec(up
);
1860 cst
= isl_upoly_as_cst(up
);
1863 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1866 /* Return the constant term of "qp".
1868 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1869 __isl_keep isl_qpolynomial
*qp
)
1874 return isl_upoly_get_constant_val(qp
->upoly
);
1877 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1880 struct isl_upoly_rec
*rec
;
1888 rec
= isl_upoly_as_rec(up
);
1895 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1897 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1903 return isl_upoly_is_affine(rec
->p
[0]);
1906 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1911 if (qp
->div
->n_row
> 0)
1914 return isl_upoly_is_affine(qp
->upoly
);
1917 static void update_coeff(__isl_keep isl_vec
*aff
,
1918 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1923 if (isl_int_is_zero(cst
->n
))
1928 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1929 isl_int_divexact(f
, cst
->d
, gcd
);
1930 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1931 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1932 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1937 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1938 __isl_keep isl_vec
*aff
)
1940 struct isl_upoly_cst
*cst
;
1941 struct isl_upoly_rec
*rec
;
1947 struct isl_upoly_cst
*cst
;
1949 cst
= isl_upoly_as_cst(up
);
1952 update_coeff(aff
, cst
, 0);
1956 rec
= isl_upoly_as_rec(up
);
1959 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1961 cst
= isl_upoly_as_cst(rec
->p
[1]);
1964 update_coeff(aff
, cst
, 1 + up
->var
);
1966 return isl_upoly_update_affine(rec
->p
[0], aff
);
1969 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1970 __isl_keep isl_qpolynomial
*qp
)
1978 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
1979 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
1983 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
1984 isl_int_set_si(aff
->el
[0], 1);
1986 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1995 /* Compare two quasi-polynomials.
1997 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
1998 * than "qp2" and 0 if they are equal.
2000 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2001 __isl_keep isl_qpolynomial
*qp2
)
2012 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2016 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2020 return isl_upoly_plain_cmp(qp1
->upoly
, qp2
->upoly
);
2023 /* Is "qp1" obviously equal to "qp2"?
2025 * NaN is not equal to anything, not even to another NaN.
2027 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2028 __isl_keep isl_qpolynomial
*qp2
)
2033 return isl_bool_error
;
2035 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2036 return isl_bool_false
;
2038 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2039 if (equal
< 0 || !equal
)
2042 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2043 if (equal
< 0 || !equal
)
2046 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
2049 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
2052 struct isl_upoly_rec
*rec
;
2054 if (isl_upoly_is_cst(up
)) {
2055 struct isl_upoly_cst
*cst
;
2056 cst
= isl_upoly_as_cst(up
);
2059 isl_int_lcm(*d
, *d
, cst
->d
);
2063 rec
= isl_upoly_as_rec(up
);
2067 for (i
= 0; i
< rec
->n
; ++i
)
2068 upoly_update_den(rec
->p
[i
], d
);
2071 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
2073 isl_int_set_si(*d
, 1);
2076 upoly_update_den(qp
->upoly
, d
);
2079 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2080 __isl_take isl_space
*dim
, int pos
, int power
)
2082 struct isl_ctx
*ctx
;
2089 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_var_pow(ctx
, pos
, power
));
2092 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(__isl_take isl_space
*dim
,
2093 enum isl_dim_type type
, unsigned pos
)
2098 isl_assert(dim
->ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, goto error
);
2099 isl_assert(dim
->ctx
, pos
< isl_space_dim(dim
, type
), goto error
);
2101 if (type
== isl_dim_set
)
2102 pos
+= isl_space_dim(dim
, isl_dim_param
);
2104 return isl_qpolynomial_var_pow_on_domain(dim
, pos
, 1);
2106 isl_space_free(dim
);
2110 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2111 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2114 struct isl_upoly_rec
*rec
;
2115 struct isl_upoly
*base
, *res
;
2120 if (isl_upoly_is_cst(up
))
2123 if (up
->var
< first
)
2126 rec
= isl_upoly_as_rec(up
);
2130 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2132 if (up
->var
>= first
+ n
)
2133 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2135 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2137 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2138 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2139 struct isl_upoly
*t
;
2140 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2141 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2142 res
= isl_upoly_sum(res
, t
);
2145 isl_upoly_free(base
);
2154 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2155 isl_int denom
, unsigned len
)
2158 struct isl_upoly
*up
;
2160 isl_assert(ctx
, len
>= 1, return NULL
);
2162 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2163 for (i
= 0; i
< len
- 1; ++i
) {
2164 struct isl_upoly
*t
;
2165 struct isl_upoly
*c
;
2167 if (isl_int_is_zero(f
[1 + i
]))
2170 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2171 t
= isl_upoly_var_pow(ctx
, i
, 1);
2172 t
= isl_upoly_mul(c
, t
);
2173 up
= isl_upoly_sum(up
, t
);
2179 /* Remove common factor of non-constant terms and denominator.
2181 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2183 isl_ctx
*ctx
= qp
->div
->ctx
;
2184 unsigned total
= qp
->div
->n_col
- 2;
2186 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2187 isl_int_gcd(ctx
->normalize_gcd
,
2188 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2189 if (isl_int_is_one(ctx
->normalize_gcd
))
2192 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2193 ctx
->normalize_gcd
, total
);
2194 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2195 ctx
->normalize_gcd
);
2196 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2197 ctx
->normalize_gcd
);
2200 /* Replace the integer division identified by "div" by the polynomial "s".
2201 * The integer division is assumed not to appear in the definition
2202 * of any other integer divisions.
2204 static __isl_give isl_qpolynomial
*substitute_div(
2205 __isl_take isl_qpolynomial
*qp
,
2206 int div
, __isl_take
struct isl_upoly
*s
)
2215 qp
= isl_qpolynomial_cow(qp
);
2219 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2220 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2224 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2227 for (i
= 0; i
< total
+ div
; ++i
)
2229 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2230 reordering
[i
] = i
- 1;
2231 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2232 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2233 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2236 if (!qp
->upoly
|| !qp
->div
)
2242 isl_qpolynomial_free(qp
);
2247 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2248 * divisions because d is equal to 1 by their definition, i.e., e.
2250 static __isl_give isl_qpolynomial
*substitute_non_divs(
2251 __isl_take isl_qpolynomial
*qp
)
2255 struct isl_upoly
*s
;
2260 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2261 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2262 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2264 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2265 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2267 isl_seq_combine(qp
->div
->row
[j
] + 1,
2268 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2269 qp
->div
->row
[j
][2 + total
+ i
],
2270 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2271 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2272 normalize_div(qp
, j
);
2274 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2275 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2276 qp
= substitute_div(qp
, i
, s
);
2283 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2284 * with d the denominator. When replacing the coefficient e of x by
2285 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2286 * inside the division, so we need to add floor(e/d) * x outside.
2287 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2288 * to adjust the coefficient of x in each later div that depends on the
2289 * current div "div" and also in the affine expressions in the rows of "mat"
2290 * (if they too depend on "div").
2292 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2293 __isl_keep isl_mat
**mat
)
2297 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2300 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2301 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2302 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2304 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2305 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2306 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2307 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2308 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2309 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2311 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2312 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2318 /* Check if the last non-zero coefficient is bigger that half of the
2319 * denominator. If so, we will invert the div to further reduce the number
2320 * of distinct divs that may appear.
2321 * If the last non-zero coefficient is exactly half the denominator,
2322 * then we continue looking for earlier coefficients that are bigger
2323 * than half the denominator.
2325 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2330 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2331 if (isl_int_is_zero(div
->row
[row
][i
]))
2333 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2334 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2335 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2345 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2346 * We only invert the coefficients of e (and the coefficient of q in
2347 * later divs and in the rows of "mat"). After calling this function, the
2348 * coefficients of e should be reduced again.
2350 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2351 __isl_keep isl_mat
**mat
)
2353 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2355 isl_seq_neg(qp
->div
->row
[div
] + 1,
2356 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2357 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2358 isl_int_add(qp
->div
->row
[div
][1],
2359 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2360 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2361 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2362 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2365 /* Reduce all divs of "qp" to have coefficients
2366 * in the interval [0, d-1], with d the denominator and such that the
2367 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2368 * The modifications to the integer divisions need to be reflected
2369 * in the factors of the polynomial that refer to the original
2370 * integer divisions. To this end, the modifications are collected
2371 * as a set of affine expressions and then plugged into the polynomial.
2373 * After the reduction, some divs may have become redundant or identical,
2374 * so we call substitute_non_divs and sort_divs. If these functions
2375 * eliminate divs or merge two or more divs into one, the coefficients
2376 * of the enclosing divs may have to be reduced again, so we call
2377 * ourselves recursively if the number of divs decreases.
2379 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2384 struct isl_upoly
**s
;
2385 unsigned o_div
, n_div
, total
;
2390 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2391 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2392 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2393 ctx
= isl_qpolynomial_get_ctx(qp
);
2394 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2396 for (i
= 0; i
< n_div
; ++i
)
2397 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2399 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2400 normalize_div(qp
, i
);
2401 reduce_div(qp
, i
, &mat
);
2402 if (needs_invert(qp
->div
, i
)) {
2403 invert_div(qp
, i
, &mat
);
2404 reduce_div(qp
, i
, &mat
);
2410 s
= isl_alloc_array(ctx
, struct isl_upoly
*, n_div
);
2413 for (i
= 0; i
< n_div
; ++i
)
2414 s
[i
] = isl_upoly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2416 qp
->upoly
= isl_upoly_subs(qp
->upoly
, o_div
- 1, n_div
, s
);
2417 for (i
= 0; i
< n_div
; ++i
)
2418 isl_upoly_free(s
[i
]);
2425 qp
= substitute_non_divs(qp
);
2427 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2428 return reduce_divs(qp
);
2432 isl_qpolynomial_free(qp
);
2437 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2438 __isl_take isl_space
*dim
, const isl_int n
, const isl_int d
)
2440 struct isl_qpolynomial
*qp
;
2441 struct isl_upoly_cst
*cst
;
2446 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
2450 cst
= isl_upoly_as_cst(qp
->upoly
);
2451 isl_int_set(cst
->n
, n
);
2452 isl_int_set(cst
->d
, d
);
2457 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2459 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2460 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2462 isl_qpolynomial
*qp
;
2463 struct isl_upoly_cst
*cst
;
2465 if (!domain
|| !val
)
2468 qp
= isl_qpolynomial_alloc(isl_space_copy(domain
), 0,
2469 isl_upoly_zero(domain
->ctx
));
2473 cst
= isl_upoly_as_cst(qp
->upoly
);
2474 isl_int_set(cst
->n
, val
->n
);
2475 isl_int_set(cst
->d
, val
->d
);
2477 isl_space_free(domain
);
2481 isl_space_free(domain
);
2486 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2488 struct isl_upoly_rec
*rec
;
2494 if (isl_upoly_is_cst(up
))
2498 active
[up
->var
] = 1;
2500 rec
= isl_upoly_as_rec(up
);
2501 for (i
= 0; i
< rec
->n
; ++i
)
2502 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2508 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2511 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2516 for (i
= 0; i
< d
; ++i
)
2517 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2518 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2524 return up_set_active(qp
->upoly
, active
, d
);
2527 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2528 enum isl_dim_type type
, unsigned first
, unsigned n
)
2532 isl_bool involves
= isl_bool_false
;
2535 return isl_bool_error
;
2537 return isl_bool_false
;
2539 isl_assert(qp
->dim
->ctx
,
2540 first
+ n
<= isl_qpolynomial_dim(qp
, type
),
2541 return isl_bool_error
);
2542 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2543 type
== isl_dim_in
, return isl_bool_error
);
2545 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2546 isl_space_dim(qp
->dim
, isl_dim_all
));
2547 if (set_active(qp
, active
) < 0)
2550 if (type
== isl_dim_in
)
2551 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2552 for (i
= 0; i
< n
; ++i
)
2553 if (active
[first
+ i
]) {
2554 involves
= isl_bool_true
;
2563 return isl_bool_error
;
2566 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2567 * of the divs that do appear in the quasi-polynomial.
2569 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2570 __isl_take isl_qpolynomial
*qp
)
2577 int *reordering
= NULL
;
2584 if (qp
->div
->n_row
== 0)
2587 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2588 len
= qp
->div
->n_col
- 2;
2589 ctx
= isl_qpolynomial_get_ctx(qp
);
2590 active
= isl_calloc_array(ctx
, int, len
);
2594 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2597 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2598 if (!active
[d
+ i
]) {
2602 for (j
= 0; j
< i
; ++j
) {
2603 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2615 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2619 for (i
= 0; i
< d
; ++i
)
2623 n_div
= qp
->div
->n_row
;
2624 for (i
= 0; i
< n_div
; ++i
) {
2625 if (!active
[d
+ i
]) {
2626 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2627 qp
->div
= isl_mat_drop_cols(qp
->div
,
2628 2 + d
+ i
- skip
, 1);
2631 reordering
[d
+ i
] = d
+ i
- skip
;
2634 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2636 if (!qp
->upoly
|| !qp
->div
)
2646 isl_qpolynomial_free(qp
);
2650 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2651 unsigned first
, unsigned n
)
2654 struct isl_upoly_rec
*rec
;
2658 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2660 if (up
->var
< first
+ n
) {
2661 up
= replace_by_constant_term(up
);
2662 return isl_upoly_drop(up
, first
, n
);
2664 up
= isl_upoly_cow(up
);
2668 rec
= isl_upoly_as_rec(up
);
2672 for (i
= 0; i
< rec
->n
; ++i
) {
2673 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2684 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2685 __isl_take isl_qpolynomial
*qp
,
2686 enum isl_dim_type type
, unsigned pos
, const char *s
)
2688 qp
= isl_qpolynomial_cow(qp
);
2691 if (type
== isl_dim_out
)
2692 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2693 "cannot set name of output/set dimension",
2694 return isl_qpolynomial_free(qp
));
2695 if (type
== isl_dim_in
)
2697 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2702 isl_qpolynomial_free(qp
);
2706 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2707 __isl_take isl_qpolynomial
*qp
,
2708 enum isl_dim_type type
, unsigned first
, unsigned n
)
2712 if (type
== isl_dim_out
)
2713 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2714 "cannot drop output/set dimension",
2716 if (type
== isl_dim_in
)
2718 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2721 qp
= isl_qpolynomial_cow(qp
);
2725 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
2727 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2728 type
== isl_dim_set
, goto error
);
2730 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2734 if (type
== isl_dim_set
)
2735 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2737 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2741 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2747 isl_qpolynomial_free(qp
);
2751 /* Project the domain of the quasi-polynomial onto its parameter space.
2752 * The quasi-polynomial may not involve any of the domain dimensions.
2754 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2755 __isl_take isl_qpolynomial
*qp
)
2761 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2762 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2764 return isl_qpolynomial_free(qp
);
2766 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2767 "polynomial involves some of the domain dimensions",
2768 return isl_qpolynomial_free(qp
));
2769 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2770 space
= isl_qpolynomial_get_domain_space(qp
);
2771 space
= isl_space_params(space
);
2772 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2776 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2777 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2783 struct isl_upoly
*up
;
2787 if (eq
->n_eq
== 0) {
2788 isl_basic_set_free(eq
);
2792 qp
= isl_qpolynomial_cow(qp
);
2795 qp
->div
= isl_mat_cow(qp
->div
);
2799 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2801 isl_int_init(denom
);
2802 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2803 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2804 if (j
< 0 || j
== 0 || j
>= total
)
2807 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2808 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2810 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2811 &qp
->div
->row
[k
][0]);
2812 normalize_div(qp
, k
);
2815 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2816 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2817 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2818 isl_int_set_si(eq
->eq
[i
][j
], 0);
2820 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2821 eq
->eq
[i
], denom
, total
);
2822 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2825 isl_int_clear(denom
);
2830 isl_basic_set_free(eq
);
2832 qp
= substitute_non_divs(qp
);
2837 isl_basic_set_free(eq
);
2838 isl_qpolynomial_free(qp
);
2842 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2844 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2845 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2849 if (qp
->div
->n_row
> 0)
2850 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2851 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2853 isl_basic_set_free(eq
);
2854 isl_qpolynomial_free(qp
);
2858 static __isl_give isl_basic_set
*add_div_constraints(
2859 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*div
)
2867 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2870 total
= isl_basic_set_total_dim(bset
);
2871 for (i
= 0; i
< div
->n_row
; ++i
)
2872 if (isl_basic_set_add_div_constraints_var(bset
,
2873 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2880 isl_basic_set_free(bset
);
2884 /* Look for equalities among the variables shared by context and qp
2885 * and the integer divisions of qp, if any.
2886 * The equalities are then used to eliminate variables and/or integer
2887 * divisions from qp.
2889 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2890 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2896 if (qp
->div
->n_row
> 0) {
2897 isl_basic_set
*bset
;
2898 context
= isl_set_add_dims(context
, isl_dim_set
,
2900 bset
= isl_basic_set_universe(isl_set_get_space(context
));
2901 bset
= add_div_constraints(bset
, isl_mat_copy(qp
->div
));
2902 context
= isl_set_intersect(context
,
2903 isl_set_from_basic_set(bset
));
2906 aff
= isl_set_affine_hull(context
);
2907 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2909 isl_qpolynomial_free(qp
);
2910 isl_set_free(context
);
2914 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2915 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2917 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2918 isl_set
*dom_context
= isl_set_universe(space
);
2919 dom_context
= isl_set_intersect_params(dom_context
, context
);
2920 return isl_qpolynomial_gist(qp
, dom_context
);
2923 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2924 __isl_take isl_qpolynomial
*qp
)
2930 if (isl_qpolynomial_is_zero(qp
)) {
2931 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2932 isl_qpolynomial_free(qp
);
2933 return isl_pw_qpolynomial_zero(dim
);
2936 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2937 return isl_pw_qpolynomial_alloc(dom
, qp
);
2940 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2943 #define PW isl_pw_qpolynomial
2945 #define EL isl_qpolynomial
2947 #define EL_IS_ZERO is_zero
2951 #define IS_ZERO is_zero
2954 #undef DEFAULT_IS_ZERO
2955 #define DEFAULT_IS_ZERO 1
2959 #include <isl_pw_templ.c>
2962 #define UNION isl_union_pw_qpolynomial
2964 #define PART isl_pw_qpolynomial
2966 #define PARTS pw_qpolynomial
2968 #include <isl_union_single.c>
2969 #include <isl_union_eval.c>
2970 #include <isl_union_neg.c>
2972 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2980 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2983 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2986 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2987 __isl_take isl_pw_qpolynomial
*pwqp1
,
2988 __isl_take isl_pw_qpolynomial
*pwqp2
)
2990 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2993 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2994 __isl_take isl_pw_qpolynomial
*pwqp1
,
2995 __isl_take isl_pw_qpolynomial
*pwqp2
)
2998 struct isl_pw_qpolynomial
*res
;
3000 if (!pwqp1
|| !pwqp2
)
3003 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
3006 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
3007 isl_pw_qpolynomial_free(pwqp2
);
3011 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
3012 isl_pw_qpolynomial_free(pwqp1
);
3016 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
3017 isl_pw_qpolynomial_free(pwqp1
);
3021 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3022 isl_pw_qpolynomial_free(pwqp2
);
3026 n
= pwqp1
->n
* pwqp2
->n
;
3027 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3029 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3030 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3031 struct isl_set
*common
;
3032 struct isl_qpolynomial
*prod
;
3033 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3034 isl_set_copy(pwqp2
->p
[j
].set
));
3035 if (isl_set_plain_is_empty(common
)) {
3036 isl_set_free(common
);
3040 prod
= isl_qpolynomial_mul(
3041 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3042 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3044 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3048 isl_pw_qpolynomial_free(pwqp1
);
3049 isl_pw_qpolynomial_free(pwqp2
);
3053 isl_pw_qpolynomial_free(pwqp1
);
3054 isl_pw_qpolynomial_free(pwqp2
);
3058 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
3059 __isl_take isl_vec
*vec
)
3062 struct isl_upoly_rec
*rec
;
3066 if (isl_upoly_is_cst(up
)) {
3068 res
= isl_upoly_get_constant_val(up
);
3073 rec
= isl_upoly_as_rec(up
);
3077 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
3079 base
= isl_val_rat_from_isl_int(up
->ctx
,
3080 vec
->el
[1 + up
->var
], vec
->el
[0]);
3082 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
3085 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3086 res
= isl_val_mul(res
, isl_val_copy(base
));
3087 res
= isl_val_add(res
,
3088 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
3089 isl_vec_copy(vec
)));
3102 /* Evaluate "qp" in the void point "pnt".
3103 * In particular, return the value NaN.
3105 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3106 __isl_take isl_point
*pnt
)
3110 ctx
= isl_point_get_ctx(pnt
);
3111 isl_qpolynomial_free(qp
);
3112 isl_point_free(pnt
);
3113 return isl_val_nan(ctx
);
3116 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3117 __isl_take isl_point
*pnt
)
3125 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3126 is_void
= isl_point_is_void(pnt
);
3130 return eval_void(qp
, pnt
);
3132 if (qp
->div
->n_row
== 0)
3133 ext
= isl_vec_copy(pnt
->vec
);
3136 unsigned dim
= isl_space_dim(qp
->dim
, isl_dim_all
);
3137 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
3141 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
3142 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
3143 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
3144 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
3145 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
3146 qp
->div
->row
[i
][0]);
3150 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
3152 isl_qpolynomial_free(qp
);
3153 isl_point_free(pnt
);
3157 isl_qpolynomial_free(qp
);
3158 isl_point_free(pnt
);
3162 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
3163 __isl_keep
struct isl_upoly_cst
*cst2
)
3168 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3169 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3170 cmp
= isl_int_sgn(t
);
3175 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3176 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3177 unsigned first
, unsigned n
)
3185 if (type
== isl_dim_out
)
3186 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3187 "cannot insert output/set dimensions",
3189 if (type
== isl_dim_in
)
3191 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3194 qp
= isl_qpolynomial_cow(qp
);
3198 isl_assert(qp
->div
->ctx
, first
<= isl_space_dim(qp
->dim
, type
),
3201 g_pos
= pos(qp
->dim
, type
) + first
;
3203 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3207 total
= qp
->div
->n_col
- 2;
3208 if (total
> g_pos
) {
3210 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3213 for (i
= 0; i
< total
- g_pos
; ++i
)
3215 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3221 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3227 isl_qpolynomial_free(qp
);
3231 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3232 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3236 pos
= isl_qpolynomial_dim(qp
, type
);
3238 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3241 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3242 __isl_take isl_pw_qpolynomial
*pwqp
,
3243 enum isl_dim_type type
, unsigned n
)
3247 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3249 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3252 static int *reordering_move(isl_ctx
*ctx
,
3253 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3258 reordering
= isl_alloc_array(ctx
, int, len
);
3263 for (i
= 0; i
< dst
; ++i
)
3265 for (i
= 0; i
< n
; ++i
)
3266 reordering
[src
+ i
] = dst
+ i
;
3267 for (i
= 0; i
< src
- dst
; ++i
)
3268 reordering
[dst
+ i
] = dst
+ n
+ i
;
3269 for (i
= 0; i
< len
- src
- n
; ++i
)
3270 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3272 for (i
= 0; i
< src
; ++i
)
3274 for (i
= 0; i
< n
; ++i
)
3275 reordering
[src
+ i
] = dst
+ i
;
3276 for (i
= 0; i
< dst
- src
; ++i
)
3277 reordering
[src
+ n
+ i
] = src
+ i
;
3278 for (i
= 0; i
< len
- dst
- n
; ++i
)
3279 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3285 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3286 __isl_take isl_qpolynomial
*qp
,
3287 enum isl_dim_type dst_type
, unsigned dst_pos
,
3288 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3297 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3298 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3299 "cannot move output/set dimension",
3301 if (dst_type
== isl_dim_in
)
3302 dst_type
= isl_dim_set
;
3303 if (src_type
== isl_dim_in
)
3304 src_type
= isl_dim_set
;
3307 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3308 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3311 qp
= isl_qpolynomial_cow(qp
);
3315 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_space_dim(qp
->dim
, src_type
),
3318 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3319 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3320 if (dst_type
> src_type
)
3323 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3330 reordering
= reordering_move(qp
->dim
->ctx
,
3331 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3335 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3340 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3346 isl_qpolynomial_free(qp
);
3350 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(__isl_take isl_space
*dim
,
3351 isl_int
*f
, isl_int denom
)
3353 struct isl_upoly
*up
;
3355 dim
= isl_space_domain(dim
);
3359 up
= isl_upoly_from_affine(dim
->ctx
, f
, denom
,
3360 1 + isl_space_dim(dim
, isl_dim_all
));
3362 return isl_qpolynomial_alloc(dim
, 0, up
);
3365 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3368 struct isl_upoly
*up
;
3369 isl_qpolynomial
*qp
;
3374 ctx
= isl_aff_get_ctx(aff
);
3375 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3378 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3379 aff
->ls
->div
->n_row
, up
);
3383 isl_mat_free(qp
->div
);
3384 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3385 qp
->div
= isl_mat_cow(qp
->div
);
3390 qp
= reduce_divs(qp
);
3391 qp
= remove_redundant_divs(qp
);
3395 return isl_qpolynomial_free(qp
);
3398 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3399 __isl_take isl_pw_aff
*pwaff
)
3402 isl_pw_qpolynomial
*pwqp
;
3407 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3410 for (i
= 0; i
< pwaff
->n
; ++i
) {
3412 isl_qpolynomial
*qp
;
3414 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3415 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3416 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3419 isl_pw_aff_free(pwaff
);
3423 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3424 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3428 aff
= isl_constraint_get_bound(c
, type
, pos
);
3429 isl_constraint_free(c
);
3430 return isl_qpolynomial_from_aff(aff
);
3433 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3434 * in "qp" by subs[i].
3436 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3437 __isl_take isl_qpolynomial
*qp
,
3438 enum isl_dim_type type
, unsigned first
, unsigned n
,
3439 __isl_keep isl_qpolynomial
**subs
)
3442 struct isl_upoly
**ups
;
3447 qp
= isl_qpolynomial_cow(qp
);
3451 if (type
== isl_dim_out
)
3452 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3453 "cannot substitute output/set dimension",
3455 if (type
== isl_dim_in
)
3458 for (i
= 0; i
< n
; ++i
)
3462 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_space_dim(qp
->dim
, type
),
3465 for (i
= 0; i
< n
; ++i
)
3466 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3469 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3470 for (i
= 0; i
< n
; ++i
)
3471 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3473 first
+= pos(qp
->dim
, type
);
3475 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3478 for (i
= 0; i
< n
; ++i
)
3479 ups
[i
] = subs
[i
]->upoly
;
3481 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3490 isl_qpolynomial_free(qp
);
3494 /* Extend "bset" with extra set dimensions for each integer division
3495 * in "qp" and then call "fn" with the extended bset and the polynomial
3496 * that results from replacing each of the integer divisions by the
3497 * corresponding extra set dimension.
3499 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3500 __isl_keep isl_basic_set
*bset
,
3501 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3502 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3506 isl_qpolynomial
*poly
;
3509 return isl_stat_error
;
3510 if (qp
->div
->n_row
== 0)
3511 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3514 div
= isl_mat_copy(qp
->div
);
3515 dim
= isl_space_copy(qp
->dim
);
3516 dim
= isl_space_add_dims(dim
, isl_dim_set
, qp
->div
->n_row
);
3517 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
3518 bset
= isl_basic_set_copy(bset
);
3519 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, qp
->div
->n_row
);
3520 bset
= add_div_constraints(bset
, div
);
3522 return fn(bset
, poly
, user
);
3525 /* Return total degree in variables first (inclusive) up to last (exclusive).
3527 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3531 struct isl_upoly_rec
*rec
;
3535 if (isl_upoly_is_zero(up
))
3537 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3540 rec
= isl_upoly_as_rec(up
);
3544 for (i
= 0; i
< rec
->n
; ++i
) {
3547 if (isl_upoly_is_zero(rec
->p
[i
]))
3549 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3559 /* Return total degree in set variables.
3561 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3569 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3570 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3571 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3574 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3575 unsigned pos
, int deg
)
3578 struct isl_upoly_rec
*rec
;
3583 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3585 return isl_upoly_copy(up
);
3587 return isl_upoly_zero(up
->ctx
);
3590 rec
= isl_upoly_as_rec(up
);
3594 if (up
->var
== pos
) {
3596 return isl_upoly_copy(rec
->p
[deg
]);
3598 return isl_upoly_zero(up
->ctx
);
3601 up
= isl_upoly_copy(up
);
3602 up
= isl_upoly_cow(up
);
3603 rec
= isl_upoly_as_rec(up
);
3607 for (i
= 0; i
< rec
->n
; ++i
) {
3608 struct isl_upoly
*t
;
3609 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3612 isl_upoly_free(rec
->p
[i
]);
3622 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3624 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3625 __isl_keep isl_qpolynomial
*qp
,
3626 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3629 struct isl_upoly
*up
;
3635 if (type
== isl_dim_out
)
3636 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3637 "output/set dimension does not have a coefficient",
3639 if (type
== isl_dim_in
)
3642 isl_assert(qp
->div
->ctx
, t_pos
< isl_space_dim(qp
->dim
, type
),
3645 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3646 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3648 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3651 isl_mat_free(c
->div
);
3652 c
->div
= isl_mat_copy(qp
->div
);
3657 isl_qpolynomial_free(c
);
3661 /* Homogenize the polynomial in the variables first (inclusive) up to
3662 * last (exclusive) by inserting powers of variable first.
3663 * Variable first is assumed not to appear in the input.
3665 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3666 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3667 int first
, int last
)
3670 struct isl_upoly_rec
*rec
;
3674 if (isl_upoly_is_zero(up
))
3678 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3679 struct isl_upoly
*hom
;
3681 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3684 rec
= isl_upoly_as_rec(hom
);
3685 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3690 up
= isl_upoly_cow(up
);
3691 rec
= isl_upoly_as_rec(up
);
3695 for (i
= 0; i
< rec
->n
; ++i
) {
3696 if (isl_upoly_is_zero(rec
->p
[i
]))
3698 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3699 up
->var
< last
? deg
+ i
: i
, target
,
3711 /* Homogenize the polynomial in the set variables by introducing
3712 * powers of an extra set variable at position 0.
3714 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3715 __isl_take isl_qpolynomial
*poly
)
3719 int deg
= isl_qpolynomial_degree(poly
);
3724 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3725 poly
= isl_qpolynomial_cow(poly
);
3729 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3730 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3731 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3738 isl_qpolynomial_free(poly
);
3742 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*dim
,
3743 __isl_take isl_mat
*div
)
3751 n
= isl_space_dim(dim
, isl_dim_all
) + div
->n_row
;
3753 term
= isl_calloc(dim
->ctx
, struct isl_term
,
3754 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3761 isl_int_init(term
->n
);
3762 isl_int_init(term
->d
);
3766 isl_space_free(dim
);
3771 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3780 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3789 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3791 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3795 isl_int_set(dup
->n
, term
->n
);
3796 isl_int_set(dup
->d
, term
->d
);
3798 for (i
= 0; i
< total
; ++i
)
3799 dup
->pow
[i
] = term
->pow
[i
];
3804 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3812 return isl_term_dup(term
);
3815 void isl_term_free(__isl_take isl_term
*term
)
3820 if (--term
->ref
> 0)
3823 isl_space_free(term
->dim
);
3824 isl_mat_free(term
->div
);
3825 isl_int_clear(term
->n
);
3826 isl_int_clear(term
->d
);
3830 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3838 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3839 case isl_dim_div
: return term
->div
->n_row
;
3840 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3846 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3848 return term
? term
->dim
->ctx
: NULL
;
3851 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3855 isl_int_set(*n
, term
->n
);
3858 /* Return the coefficient of the term "term".
3860 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3865 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3869 int isl_term_get_exp(__isl_keep isl_term
*term
,
3870 enum isl_dim_type type
, unsigned pos
)
3875 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
3877 if (type
>= isl_dim_set
)
3878 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3879 if (type
>= isl_dim_div
)
3880 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3882 return term
->pow
[pos
];
3885 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3887 isl_local_space
*ls
;
3893 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
3896 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3897 isl_mat_copy(term
->div
));
3898 aff
= isl_aff_alloc(ls
);
3902 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3904 aff
= isl_aff_normalize(aff
);
3909 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3910 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3911 __isl_take isl_term
*term
, void *user
)
3914 struct isl_upoly_rec
*rec
;
3919 if (isl_upoly_is_zero(up
))
3922 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3923 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3924 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3926 if (isl_upoly_is_cst(up
)) {
3927 struct isl_upoly_cst
*cst
;
3928 cst
= isl_upoly_as_cst(up
);
3931 term
= isl_term_cow(term
);
3934 isl_int_set(term
->n
, cst
->n
);
3935 isl_int_set(term
->d
, cst
->d
);
3936 if (fn(isl_term_copy(term
), user
) < 0)
3941 rec
= isl_upoly_as_rec(up
);
3945 for (i
= 0; i
< rec
->n
; ++i
) {
3946 term
= isl_term_cow(term
);
3949 term
->pow
[up
->var
] = i
;
3950 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3954 term
->pow
[up
->var
] = 0;
3958 isl_term_free(term
);
3962 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3963 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3968 return isl_stat_error
;
3970 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3972 return isl_stat_error
;
3974 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3976 isl_term_free(term
);
3978 return term
? isl_stat_ok
: isl_stat_error
;
3981 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3983 struct isl_upoly
*up
;
3984 isl_qpolynomial
*qp
;
3990 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3992 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3993 for (i
= 0; i
< n
; ++i
) {
3996 up
= isl_upoly_mul(up
,
3997 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
4000 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
4003 isl_mat_free(qp
->div
);
4004 qp
->div
= isl_mat_copy(term
->div
);
4008 isl_term_free(term
);
4011 isl_qpolynomial_free(qp
);
4012 isl_term_free(term
);
4016 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
4017 __isl_take isl_space
*dim
)
4026 if (isl_space_is_equal(qp
->dim
, dim
)) {
4027 isl_space_free(dim
);
4031 qp
= isl_qpolynomial_cow(qp
);
4035 extra
= isl_space_dim(dim
, isl_dim_set
) -
4036 isl_space_dim(qp
->dim
, isl_dim_set
);
4037 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4038 if (qp
->div
->n_row
) {
4041 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4044 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4046 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
4051 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4054 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4055 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4057 isl_space_free(qp
->dim
);
4062 isl_space_free(dim
);
4063 isl_qpolynomial_free(qp
);
4067 /* For each parameter or variable that does not appear in qp,
4068 * first eliminate the variable from all constraints and then set it to zero.
4070 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4071 __isl_keep isl_qpolynomial
*qp
)
4082 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4083 active
= isl_calloc_array(set
->ctx
, int, d
);
4084 if (set_active(qp
, active
) < 0)
4087 for (i
= 0; i
< d
; ++i
)
4096 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4097 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4098 for (i
= 0; i
< nparam
; ++i
) {
4101 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4102 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4104 for (i
= 0; i
< nvar
; ++i
) {
4105 if (active
[nparam
+ i
])
4107 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4108 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4120 struct isl_opt_data
{
4121 isl_qpolynomial
*qp
;
4127 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4129 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4132 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4136 } else if (data
->max
) {
4137 data
->opt
= isl_val_max(data
->opt
, val
);
4139 data
->opt
= isl_val_min(data
->opt
, val
);
4145 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4146 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4148 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4153 if (isl_upoly_is_cst(qp
->upoly
)) {
4155 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4156 isl_qpolynomial_free(qp
);
4160 set
= fix_inactive(set
, qp
);
4163 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4167 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4170 isl_qpolynomial_free(qp
);
4174 isl_qpolynomial_free(qp
);
4175 isl_val_free(data
.opt
);
4179 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4180 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4185 struct isl_upoly
**subs
;
4186 isl_mat
*mat
, *diag
;
4188 qp
= isl_qpolynomial_cow(qp
);
4193 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4195 n_sub
= morph
->inv
->n_row
- 1;
4196 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4197 n_sub
+= qp
->div
->n_row
;
4198 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4202 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4203 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4204 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4205 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4206 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4207 subs
[morph
->inv
->n_row
- 1 + i
] =
4208 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4210 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4212 for (i
= 0; i
< n_sub
; ++i
)
4213 isl_upoly_free(subs
[i
]);
4216 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4217 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4218 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4219 mat
= isl_mat_diagonal(mat
, diag
);
4220 qp
->div
= isl_mat_product(qp
->div
, mat
);
4221 isl_space_free(qp
->dim
);
4222 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4224 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4227 isl_morph_free(morph
);
4231 isl_qpolynomial_free(qp
);
4232 isl_morph_free(morph
);
4236 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4237 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4238 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4240 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4241 &isl_pw_qpolynomial_mul
);
4244 /* Reorder the columns of the given div definitions according to the
4247 static __isl_give isl_mat
*reorder_divs(__isl_take isl_mat
*div
,
4248 __isl_take isl_reordering
*r
)
4257 extra
= isl_space_dim(r
->dim
, isl_dim_all
) + div
->n_row
- r
->len
;
4258 mat
= isl_mat_alloc(div
->ctx
, div
->n_row
, div
->n_col
+ extra
);
4262 for (i
= 0; i
< div
->n_row
; ++i
) {
4263 isl_seq_cpy(mat
->row
[i
], div
->row
[i
], 2);
4264 isl_seq_clr(mat
->row
[i
] + 2, mat
->n_col
- 2);
4265 for (j
= 0; j
< r
->len
; ++j
)
4266 isl_int_set(mat
->row
[i
][2 + r
->pos
[j
]],
4267 div
->row
[i
][2 + j
]);
4270 isl_reordering_free(r
);
4274 isl_reordering_free(r
);
4279 /* Reorder the dimension of "qp" according to the given reordering.
4281 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4282 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4284 qp
= isl_qpolynomial_cow(qp
);
4288 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4292 qp
->div
= reorder_divs(qp
->div
, isl_reordering_copy(r
));
4296 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4300 qp
= isl_qpolynomial_reset_domain_space(qp
, isl_space_copy(r
->dim
));
4302 isl_reordering_free(r
);
4305 isl_qpolynomial_free(qp
);
4306 isl_reordering_free(r
);
4310 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4311 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4313 isl_bool equal_params
;
4318 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4319 if (equal_params
< 0)
4321 if (!equal_params
) {
4322 isl_reordering
*exp
;
4324 model
= isl_space_drop_dims(model
, isl_dim_in
,
4325 0, isl_space_dim(model
, isl_dim_in
));
4326 model
= isl_space_drop_dims(model
, isl_dim_out
,
4327 0, isl_space_dim(model
, isl_dim_out
));
4328 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4329 exp
= isl_reordering_extend_space(exp
,
4330 isl_qpolynomial_get_domain_space(qp
));
4331 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4334 isl_space_free(model
);
4337 isl_space_free(model
);
4338 isl_qpolynomial_free(qp
);
4342 struct isl_split_periods_data
{
4344 isl_pw_qpolynomial
*res
;
4347 /* Create a slice where the integer division "div" has the fixed value "v".
4348 * In particular, if "div" refers to floor(f/m), then create a slice
4350 * m v <= f <= m v + (m - 1)
4355 * -f + m v + (m - 1) >= 0
4357 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*dim
,
4358 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4361 isl_basic_set
*bset
= NULL
;
4367 total
= isl_space_dim(dim
, isl_dim_all
);
4368 bset
= isl_basic_set_alloc_space(isl_space_copy(dim
), 0, 0, 2);
4370 k
= isl_basic_set_alloc_inequality(bset
);
4373 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4374 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4376 k
= isl_basic_set_alloc_inequality(bset
);
4379 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4380 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4381 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4382 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4384 isl_space_free(dim
);
4385 return isl_set_from_basic_set(bset
);
4387 isl_basic_set_free(bset
);
4388 isl_space_free(dim
);
4392 static isl_stat
split_periods(__isl_take isl_set
*set
,
4393 __isl_take isl_qpolynomial
*qp
, void *user
);
4395 /* Create a slice of the domain "set" such that integer division "div"
4396 * has the fixed value "v" and add the results to data->res,
4397 * replacing the integer division by "v" in "qp".
4399 static isl_stat
set_div(__isl_take isl_set
*set
,
4400 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4401 struct isl_split_periods_data
*data
)
4406 struct isl_upoly
*cst
;
4408 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4409 set
= isl_set_intersect(set
, slice
);
4414 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4416 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4417 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4419 isl_int_addmul(qp
->div
->row
[i
][1],
4420 qp
->div
->row
[i
][2 + total
+ div
], v
);
4421 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4424 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4425 qp
= substitute_div(qp
, div
, cst
);
4427 return split_periods(set
, qp
, data
);
4430 isl_qpolynomial_free(qp
);
4434 /* Split the domain "set" such that integer division "div"
4435 * has a fixed value (ranging from "min" to "max") on each slice
4436 * and add the results to data->res.
4438 static isl_stat
split_div(__isl_take isl_set
*set
,
4439 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4440 struct isl_split_periods_data
*data
)
4442 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4443 isl_set
*set_i
= isl_set_copy(set
);
4444 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4446 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4450 isl_qpolynomial_free(qp
);
4454 isl_qpolynomial_free(qp
);
4455 return isl_stat_error
;
4458 /* If "qp" refers to any integer division
4459 * that can only attain "max_periods" distinct values on "set"
4460 * then split the domain along those distinct values.
4461 * Add the results (or the original if no splitting occurs)
4464 static isl_stat
split_periods(__isl_take isl_set
*set
,
4465 __isl_take isl_qpolynomial
*qp
, void *user
)
4468 isl_pw_qpolynomial
*pwqp
;
4469 struct isl_split_periods_data
*data
;
4472 isl_stat r
= isl_stat_ok
;
4474 data
= (struct isl_split_periods_data
*)user
;
4479 if (qp
->div
->n_row
== 0) {
4480 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4481 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4487 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4488 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4489 enum isl_lp_result lp_res
;
4491 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4492 qp
->div
->n_row
) != -1)
4495 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4496 set
->ctx
->one
, &min
, NULL
, NULL
);
4497 if (lp_res
== isl_lp_error
)
4499 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4501 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4503 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4504 set
->ctx
->one
, &max
, 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(max
, max
, qp
->div
->row
[i
][0]);
4511 isl_int_sub(max
, max
, min
);
4512 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4513 isl_int_add(max
, max
, min
);
4518 if (i
< qp
->div
->n_row
) {
4519 r
= split_div(set
, qp
, i
, min
, max
, data
);
4521 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4522 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4534 isl_qpolynomial_free(qp
);
4535 return isl_stat_error
;
4538 /* If any quasi-polynomial in pwqp refers to any integer division
4539 * that can only attain "max_periods" distinct values on its domain
4540 * then split the domain along those distinct values.
4542 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4543 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4545 struct isl_split_periods_data data
;
4547 data
.max_periods
= max_periods
;
4548 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4550 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4553 isl_pw_qpolynomial_free(pwqp
);
4557 isl_pw_qpolynomial_free(data
.res
);
4558 isl_pw_qpolynomial_free(pwqp
);
4562 /* Construct a piecewise quasipolynomial that is constant on the given
4563 * domain. In particular, it is
4566 * infinity if cst == -1
4568 * If cst == -1, then explicitly check whether the domain is empty and,
4569 * if so, return 0 instead.
4571 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4572 __isl_take isl_basic_set
*bset
, int cst
)
4575 isl_qpolynomial
*qp
;
4577 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4582 bset
= isl_basic_set_params(bset
);
4583 dim
= isl_basic_set_get_space(bset
);
4585 qp
= isl_qpolynomial_infty_on_domain(dim
);
4587 qp
= isl_qpolynomial_zero_on_domain(dim
);
4589 qp
= isl_qpolynomial_one_on_domain(dim
);
4590 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4593 /* Factor bset, call fn on each of the factors and return the product.
4595 * If no factors can be found, simply call fn on the input.
4596 * Otherwise, construct the factors based on the factorizer,
4597 * call fn on each factor and compute the product.
4599 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4600 __isl_take isl_basic_set
*bset
,
4601 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4607 isl_qpolynomial
*qp
;
4608 isl_pw_qpolynomial
*pwqp
;
4612 f
= isl_basic_set_factorizer(bset
);
4615 if (f
->n_group
== 0) {
4616 isl_factorizer_free(f
);
4620 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4621 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4623 space
= isl_basic_set_get_space(bset
);
4624 space
= isl_space_params(space
);
4625 set
= isl_set_universe(isl_space_copy(space
));
4626 qp
= isl_qpolynomial_one_on_domain(space
);
4627 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4629 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4631 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4632 isl_basic_set
*bset_i
;
4633 isl_pw_qpolynomial
*pwqp_i
;
4635 bset_i
= isl_basic_set_copy(bset
);
4636 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4637 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4638 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4640 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4641 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4642 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4644 pwqp_i
= fn(bset_i
);
4645 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4650 isl_basic_set_free(bset
);
4651 isl_factorizer_free(f
);
4655 isl_basic_set_free(bset
);
4659 /* Factor bset, call fn on each of the factors and return the product.
4660 * The function is assumed to evaluate to zero on empty domains,
4661 * to one on zero-dimensional domains and to infinity on unbounded domains
4662 * and will not be called explicitly on zero-dimensional or unbounded domains.
4664 * We first check for some special cases and remove all equalities.
4665 * Then we hand over control to compressed_multiplicative_call.
4667 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4668 __isl_take isl_basic_set
*bset
,
4669 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4673 isl_pw_qpolynomial
*pwqp
;
4678 if (isl_basic_set_plain_is_empty(bset
))
4679 return constant_on_domain(bset
, 0);
4681 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4682 return constant_on_domain(bset
, 1);
4684 bounded
= isl_basic_set_is_bounded(bset
);
4688 return constant_on_domain(bset
, -1);
4690 if (bset
->n_eq
== 0)
4691 return compressed_multiplicative_call(bset
, fn
);
4693 morph
= isl_basic_set_full_compression(bset
);
4694 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4696 pwqp
= compressed_multiplicative_call(bset
, fn
);
4698 morph
= isl_morph_dom_params(morph
);
4699 morph
= isl_morph_ran_params(morph
);
4700 morph
= isl_morph_inverse(morph
);
4702 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4706 isl_basic_set_free(bset
);
4710 /* Drop all floors in "qp", turning each integer division [a/m] into
4711 * a rational division a/m. If "down" is set, then the integer division
4712 * is replaced by (a-(m-1))/m instead.
4714 static __isl_give isl_qpolynomial
*qp_drop_floors(
4715 __isl_take isl_qpolynomial
*qp
, int down
)
4718 struct isl_upoly
*s
;
4722 if (qp
->div
->n_row
== 0)
4725 qp
= isl_qpolynomial_cow(qp
);
4729 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4731 isl_int_sub(qp
->div
->row
[i
][1],
4732 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4733 isl_int_add_ui(qp
->div
->row
[i
][1],
4734 qp
->div
->row
[i
][1], 1);
4736 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4737 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4738 qp
= substitute_div(qp
, i
, s
);
4746 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4747 * a rational division a/m.
4749 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4750 __isl_take isl_pw_qpolynomial
*pwqp
)
4757 if (isl_pw_qpolynomial_is_zero(pwqp
))
4760 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4764 for (i
= 0; i
< pwqp
->n
; ++i
) {
4765 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4772 isl_pw_qpolynomial_free(pwqp
);
4776 /* Adjust all the integer divisions in "qp" such that they are at least
4777 * one over the given orthant (identified by "signs"). This ensures
4778 * that they will still be non-negative even after subtracting (m-1)/m.
4780 * In particular, f is replaced by f' + v, changing f = [a/m]
4781 * to f' = [(a - m v)/m].
4782 * If the constant term k in a is smaller than m,
4783 * the constant term of v is set to floor(k/m) - 1.
4784 * For any other term, if the coefficient c and the variable x have
4785 * the same sign, then no changes are needed.
4786 * Otherwise, if the variable is positive (and c is negative),
4787 * then the coefficient of x in v is set to floor(c/m).
4788 * If the variable is negative (and c is positive),
4789 * then the coefficient of x in v is set to ceil(c/m).
4791 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4797 struct isl_upoly
*s
;
4799 qp
= isl_qpolynomial_cow(qp
);
4802 qp
->div
= isl_mat_cow(qp
->div
);
4806 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4807 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4809 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4810 isl_int
*row
= qp
->div
->row
[i
];
4814 if (isl_int_lt(row
[1], row
[0])) {
4815 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4816 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4817 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4819 for (j
= 0; j
< total
; ++j
) {
4820 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4823 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4825 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4826 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4828 for (j
= 0; j
< i
; ++j
) {
4829 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4831 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4832 row
[2 + total
+ j
], row
[0]);
4833 isl_int_submul(row
[2 + total
+ j
],
4834 row
[0], v
->el
[1 + total
+ j
]);
4836 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4837 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4839 isl_seq_combine(qp
->div
->row
[j
] + 1,
4840 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4841 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4843 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4844 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4845 qp
->div
->ctx
->one
, v
->size
);
4846 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4856 isl_qpolynomial_free(qp
);
4860 struct isl_to_poly_data
{
4862 isl_pw_qpolynomial
*res
;
4863 isl_qpolynomial
*qp
;
4866 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4867 * We first make all integer divisions positive and then split the
4868 * quasipolynomials into terms with sign data->sign (the direction
4869 * of the requested approximation) and terms with the opposite sign.
4870 * In the first set of terms, each integer division [a/m] is
4871 * overapproximated by a/m, while in the second it is underapproximated
4874 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4875 int *signs
, void *user
)
4877 struct isl_to_poly_data
*data
= user
;
4878 isl_pw_qpolynomial
*t
;
4879 isl_qpolynomial
*qp
, *up
, *down
;
4881 qp
= isl_qpolynomial_copy(data
->qp
);
4882 qp
= make_divs_pos(qp
, signs
);
4884 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4885 up
= qp_drop_floors(up
, 0);
4886 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4887 down
= qp_drop_floors(down
, 1);
4889 isl_qpolynomial_free(qp
);
4890 qp
= isl_qpolynomial_add(up
, down
);
4892 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4893 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4898 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4899 * the polynomial will be an overapproximation. If "sign" is negative,
4900 * it will be an underapproximation. If "sign" is zero, the approximation
4901 * will lie somewhere in between.
4903 * In particular, is sign == 0, we simply drop the floors, turning
4904 * the integer divisions into rational divisions.
4905 * Otherwise, we split the domains into orthants, make all integer divisions
4906 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4907 * depending on the requested sign and the sign of the term in which
4908 * the integer division appears.
4910 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4911 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4914 struct isl_to_poly_data data
;
4917 return pwqp_drop_floors(pwqp
);
4923 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4925 for (i
= 0; i
< pwqp
->n
; ++i
) {
4926 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4927 isl_pw_qpolynomial
*t
;
4928 t
= isl_pw_qpolynomial_alloc(
4929 isl_set_copy(pwqp
->p
[i
].set
),
4930 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4931 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4934 data
.qp
= pwqp
->p
[i
].qp
;
4935 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4936 &to_polynomial_on_orthant
, &data
) < 0)
4940 isl_pw_qpolynomial_free(pwqp
);
4944 isl_pw_qpolynomial_free(pwqp
);
4945 isl_pw_qpolynomial_free(data
.res
);
4949 static __isl_give isl_pw_qpolynomial
*poly_entry(
4950 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4954 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4957 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4958 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4960 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4961 &poly_entry
, &sign
);
4964 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4965 __isl_take isl_qpolynomial
*qp
)
4969 isl_vec
*aff
= NULL
;
4970 isl_basic_map
*bmap
= NULL
;
4976 if (!isl_upoly_is_affine(qp
->upoly
))
4977 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4978 "input quasi-polynomial not affine", goto error
);
4979 aff
= isl_qpolynomial_extract_affine(qp
);
4982 dim
= isl_qpolynomial_get_space(qp
);
4983 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4984 n_div
= qp
->div
->n_row
;
4985 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4987 for (i
= 0; i
< n_div
; ++i
) {
4988 k
= isl_basic_map_alloc_div(bmap
);
4991 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4992 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4993 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4996 k
= isl_basic_map_alloc_equality(bmap
);
4999 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
5000 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
5001 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
5004 isl_qpolynomial_free(qp
);
5005 bmap
= isl_basic_map_finalize(bmap
);
5009 isl_qpolynomial_free(qp
);
5010 isl_basic_map_free(bmap
);