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,
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl_factorization.h>
15 #include <isl_lp_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_constraint_private.h>
19 #include <isl_polynomial_private.h>
20 #include <isl_point_private.h>
21 #include <isl_space_private.h>
22 #include <isl_mat_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_range.h>
25 #include <isl_local.h>
26 #include <isl_local_space_private.h>
27 #include <isl_aff_private.h>
28 #include <isl_val_private.h>
29 #include <isl_config.h>
32 #define BASE pw_qpolynomial
34 #include <isl_list_templ.c>
36 static unsigned pos(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
39 case isl_dim_param
: return 0;
40 case isl_dim_in
: return dim
->nparam
;
41 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
46 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
54 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
59 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
61 return (struct isl_upoly_cst
*)up
;
64 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
69 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
71 return (struct isl_upoly_rec
*)up
;
74 /* Compare two polynomials.
76 * Return -1 if "up1" is "smaller" than "up2", 1 if "up1" is "greater"
77 * than "up2" and 0 if they are equal.
79 static int isl_upoly_plain_cmp(__isl_keep
struct isl_upoly
*up1
,
80 __isl_keep
struct isl_upoly
*up2
)
83 struct isl_upoly_rec
*rec1
, *rec2
;
91 if (up1
->var
!= up2
->var
)
92 return up1
->var
- up2
->var
;
94 if (isl_upoly_is_cst(up1
)) {
95 struct isl_upoly_cst
*cst1
, *cst2
;
98 cst1
= isl_upoly_as_cst(up1
);
99 cst2
= isl_upoly_as_cst(up2
);
102 cmp
= isl_int_cmp(cst1
->n
, cst2
->n
);
105 return isl_int_cmp(cst1
->d
, cst2
->d
);
108 rec1
= isl_upoly_as_rec(up1
);
109 rec2
= isl_upoly_as_rec(up2
);
113 if (rec1
->n
!= rec2
->n
)
114 return rec1
->n
- rec2
->n
;
116 for (i
= 0; i
< rec1
->n
; ++i
) {
117 int cmp
= isl_upoly_plain_cmp(rec1
->p
[i
], rec2
->p
[i
]);
125 isl_bool
isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
126 __isl_keep
struct isl_upoly
*up2
)
129 struct isl_upoly_rec
*rec1
, *rec2
;
132 return isl_bool_error
;
134 return isl_bool_true
;
135 if (up1
->var
!= up2
->var
)
136 return isl_bool_false
;
137 if (isl_upoly_is_cst(up1
)) {
138 struct isl_upoly_cst
*cst1
, *cst2
;
139 cst1
= isl_upoly_as_cst(up1
);
140 cst2
= isl_upoly_as_cst(up2
);
142 return isl_bool_error
;
143 return isl_int_eq(cst1
->n
, cst2
->n
) &&
144 isl_int_eq(cst1
->d
, cst2
->d
);
147 rec1
= isl_upoly_as_rec(up1
);
148 rec2
= isl_upoly_as_rec(up2
);
150 return isl_bool_error
;
152 if (rec1
->n
!= rec2
->n
)
153 return isl_bool_false
;
155 for (i
= 0; i
< rec1
->n
; ++i
) {
156 isl_bool eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
161 return isl_bool_true
;
164 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
166 struct isl_upoly_cst
*cst
;
170 if (!isl_upoly_is_cst(up
))
173 cst
= isl_upoly_as_cst(up
);
177 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
180 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
182 struct isl_upoly_cst
*cst
;
186 if (!isl_upoly_is_cst(up
))
189 cst
= isl_upoly_as_cst(up
);
193 return isl_int_sgn(cst
->n
);
196 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
198 struct isl_upoly_cst
*cst
;
202 if (!isl_upoly_is_cst(up
))
205 cst
= isl_upoly_as_cst(up
);
209 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
212 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
214 struct isl_upoly_cst
*cst
;
218 if (!isl_upoly_is_cst(up
))
221 cst
= isl_upoly_as_cst(up
);
225 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
228 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
230 struct isl_upoly_cst
*cst
;
234 if (!isl_upoly_is_cst(up
))
237 cst
= isl_upoly_as_cst(up
);
241 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
244 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
246 struct isl_upoly_cst
*cst
;
250 if (!isl_upoly_is_cst(up
))
253 cst
= isl_upoly_as_cst(up
);
257 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
260 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
262 struct isl_upoly_cst
*cst
;
266 if (!isl_upoly_is_cst(up
))
269 cst
= isl_upoly_as_cst(up
);
273 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
276 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
278 struct isl_upoly_cst
*cst
;
280 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
289 isl_int_init(cst
->n
);
290 isl_int_init(cst
->d
);
295 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
297 struct isl_upoly_cst
*cst
;
299 cst
= isl_upoly_cst_alloc(ctx
);
303 isl_int_set_si(cst
->n
, 0);
304 isl_int_set_si(cst
->d
, 1);
309 __isl_give
struct isl_upoly
*isl_upoly_one(struct isl_ctx
*ctx
)
311 struct isl_upoly_cst
*cst
;
313 cst
= isl_upoly_cst_alloc(ctx
);
317 isl_int_set_si(cst
->n
, 1);
318 isl_int_set_si(cst
->d
, 1);
323 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
325 struct isl_upoly_cst
*cst
;
327 cst
= isl_upoly_cst_alloc(ctx
);
331 isl_int_set_si(cst
->n
, 1);
332 isl_int_set_si(cst
->d
, 0);
337 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
339 struct isl_upoly_cst
*cst
;
341 cst
= isl_upoly_cst_alloc(ctx
);
345 isl_int_set_si(cst
->n
, -1);
346 isl_int_set_si(cst
->d
, 0);
351 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
353 struct isl_upoly_cst
*cst
;
355 cst
= isl_upoly_cst_alloc(ctx
);
359 isl_int_set_si(cst
->n
, 0);
360 isl_int_set_si(cst
->d
, 0);
365 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
366 isl_int n
, isl_int d
)
368 struct isl_upoly_cst
*cst
;
370 cst
= isl_upoly_cst_alloc(ctx
);
374 isl_int_set(cst
->n
, n
);
375 isl_int_set(cst
->d
, d
);
380 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
383 struct isl_upoly_rec
*rec
;
385 isl_assert(ctx
, var
>= 0, return NULL
);
386 isl_assert(ctx
, size
>= 0, return NULL
);
387 rec
= isl_calloc(ctx
, struct isl_upoly_rec
,
388 sizeof(struct isl_upoly_rec
) +
389 size
* sizeof(struct isl_upoly
*));
404 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_domain_space(
405 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*dim
)
407 qp
= isl_qpolynomial_cow(qp
);
411 isl_space_free(qp
->dim
);
416 isl_qpolynomial_free(qp
);
421 /* Reset the space of "qp". This function is called from isl_pw_templ.c
422 * and doesn't know if the space of an element object is represented
423 * directly or through its domain. It therefore passes along both.
425 __isl_give isl_qpolynomial
*isl_qpolynomial_reset_space_and_domain(
426 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*space
,
427 __isl_take isl_space
*domain
)
429 isl_space_free(space
);
430 return isl_qpolynomial_reset_domain_space(qp
, domain
);
433 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
435 return qp
? qp
->dim
->ctx
: NULL
;
438 __isl_give isl_space
*isl_qpolynomial_get_domain_space(
439 __isl_keep isl_qpolynomial
*qp
)
441 return qp
? isl_space_copy(qp
->dim
) : NULL
;
444 /* Return a copy of the local space on which "qp" is defined.
446 static __isl_give isl_local_space
*isl_qpolynomial_get_domain_local_space(
447 __isl_keep isl_qpolynomial
*qp
)
454 space
= isl_qpolynomial_get_domain_space(qp
);
455 return isl_local_space_alloc_div(space
, isl_mat_copy(qp
->div
));
458 __isl_give isl_space
*isl_qpolynomial_get_space(__isl_keep isl_qpolynomial
*qp
)
463 space
= isl_space_copy(qp
->dim
);
464 space
= isl_space_from_domain(space
);
465 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
469 /* Return the number of variables of the given type in the domain of "qp".
471 unsigned isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial
*qp
,
472 enum isl_dim_type type
)
476 if (type
== isl_dim_div
)
477 return qp
->div
->n_row
;
478 if (type
== isl_dim_all
)
479 return isl_space_dim(qp
->dim
, isl_dim_all
) +
480 isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
481 return isl_space_dim(qp
->dim
, type
);
484 /* Given the type of a dimension of an isl_qpolynomial,
485 * return the type of the corresponding dimension in its domain.
486 * This function is only called for "type" equal to isl_dim_in or
489 static enum isl_dim_type
domain_type(enum isl_dim_type type
)
491 return type
== isl_dim_in
? isl_dim_set
: type
;
494 /* Externally, an isl_qpolynomial has a map space, but internally, the
495 * ls field corresponds to the domain of that space.
497 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial
*qp
,
498 enum isl_dim_type type
)
502 if (type
== isl_dim_out
)
504 type
= domain_type(type
);
505 return isl_qpolynomial_domain_dim(qp
, type
);
508 /* Return the offset of the first coefficient of type "type" in
509 * the domain of "qp".
511 unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial
*qp
,
512 enum isl_dim_type type
)
521 return 1 + isl_space_offset(qp
->dim
, type
);
523 return 1 + isl_space_dim(qp
->dim
, isl_dim_all
);
529 isl_bool
isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
531 return qp
? isl_upoly_is_zero(qp
->upoly
) : isl_bool_error
;
534 isl_bool
isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
536 return qp
? isl_upoly_is_one(qp
->upoly
) : isl_bool_error
;
539 isl_bool
isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
541 return qp
? isl_upoly_is_nan(qp
->upoly
) : isl_bool_error
;
544 isl_bool
isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
546 return qp
? isl_upoly_is_infty(qp
->upoly
) : isl_bool_error
;
549 isl_bool
isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
551 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : isl_bool_error
;
554 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
556 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
559 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
561 isl_int_clear(cst
->n
);
562 isl_int_clear(cst
->d
);
565 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
569 for (i
= 0; i
< rec
->n
; ++i
)
570 isl_upoly_free(rec
->p
[i
]);
573 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
582 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
584 struct isl_upoly_cst
*cst
;
585 struct isl_upoly_cst
*dup
;
587 cst
= isl_upoly_as_cst(up
);
591 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
594 isl_int_set(dup
->n
, cst
->n
);
595 isl_int_set(dup
->d
, cst
->d
);
600 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
603 struct isl_upoly_rec
*rec
;
604 struct isl_upoly_rec
*dup
;
606 rec
= isl_upoly_as_rec(up
);
610 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
614 for (i
= 0; i
< rec
->n
; ++i
) {
615 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
623 isl_upoly_free(&dup
->up
);
627 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
632 if (isl_upoly_is_cst(up
))
633 return isl_upoly_dup_cst(up
);
635 return isl_upoly_dup_rec(up
);
638 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
646 return isl_upoly_dup(up
);
649 __isl_null
struct isl_upoly
*isl_upoly_free(__isl_take
struct isl_upoly
*up
)
658 upoly_free_cst((struct isl_upoly_cst
*)up
);
660 upoly_free_rec((struct isl_upoly_rec
*)up
);
662 isl_ctx_deref(up
->ctx
);
667 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
672 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
673 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
674 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
675 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
680 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
681 __isl_take
struct isl_upoly
*up2
)
683 struct isl_upoly_cst
*cst1
;
684 struct isl_upoly_cst
*cst2
;
686 up1
= isl_upoly_cow(up1
);
690 cst1
= isl_upoly_as_cst(up1
);
691 cst2
= isl_upoly_as_cst(up2
);
693 if (isl_int_eq(cst1
->d
, cst2
->d
))
694 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
696 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
697 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
698 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
701 isl_upoly_cst_reduce(cst1
);
711 static __isl_give
struct isl_upoly
*replace_by_zero(
712 __isl_take
struct isl_upoly
*up
)
720 return isl_upoly_zero(ctx
);
723 static __isl_give
struct isl_upoly
*replace_by_constant_term(
724 __isl_take
struct isl_upoly
*up
)
726 struct isl_upoly_rec
*rec
;
727 struct isl_upoly
*cst
;
732 rec
= isl_upoly_as_rec(up
);
735 cst
= isl_upoly_copy(rec
->p
[0]);
743 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
744 __isl_take
struct isl_upoly
*up2
)
747 struct isl_upoly_rec
*rec1
, *rec2
;
752 if (isl_upoly_is_nan(up1
)) {
757 if (isl_upoly_is_nan(up2
)) {
762 if (isl_upoly_is_zero(up1
)) {
767 if (isl_upoly_is_zero(up2
)) {
772 if (up1
->var
< up2
->var
)
773 return isl_upoly_sum(up2
, up1
);
775 if (up2
->var
< up1
->var
) {
776 struct isl_upoly_rec
*rec
;
777 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
781 up1
= isl_upoly_cow(up1
);
782 rec
= isl_upoly_as_rec(up1
);
785 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
787 up1
= replace_by_constant_term(up1
);
791 if (isl_upoly_is_cst(up1
))
792 return isl_upoly_sum_cst(up1
, up2
);
794 rec1
= isl_upoly_as_rec(up1
);
795 rec2
= isl_upoly_as_rec(up2
);
799 if (rec1
->n
< rec2
->n
)
800 return isl_upoly_sum(up2
, up1
);
802 up1
= isl_upoly_cow(up1
);
803 rec1
= isl_upoly_as_rec(up1
);
807 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
808 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
809 isl_upoly_copy(rec2
->p
[i
]));
812 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
813 isl_upoly_free(rec1
->p
[i
]);
819 up1
= replace_by_zero(up1
);
820 else if (rec1
->n
== 1)
821 up1
= replace_by_constant_term(up1
);
832 __isl_give
struct isl_upoly
*isl_upoly_cst_add_isl_int(
833 __isl_take
struct isl_upoly
*up
, isl_int v
)
835 struct isl_upoly_cst
*cst
;
837 up
= isl_upoly_cow(up
);
841 cst
= isl_upoly_as_cst(up
);
843 isl_int_addmul(cst
->n
, cst
->d
, v
);
848 __isl_give
struct isl_upoly
*isl_upoly_add_isl_int(
849 __isl_take
struct isl_upoly
*up
, isl_int v
)
851 struct isl_upoly_rec
*rec
;
856 if (isl_upoly_is_cst(up
))
857 return isl_upoly_cst_add_isl_int(up
, v
);
859 up
= isl_upoly_cow(up
);
860 rec
= isl_upoly_as_rec(up
);
864 rec
->p
[0] = isl_upoly_add_isl_int(rec
->p
[0], v
);
874 __isl_give
struct isl_upoly
*isl_upoly_cst_mul_isl_int(
875 __isl_take
struct isl_upoly
*up
, isl_int v
)
877 struct isl_upoly_cst
*cst
;
879 if (isl_upoly_is_zero(up
))
882 up
= isl_upoly_cow(up
);
886 cst
= isl_upoly_as_cst(up
);
888 isl_int_mul(cst
->n
, cst
->n
, v
);
893 __isl_give
struct isl_upoly
*isl_upoly_mul_isl_int(
894 __isl_take
struct isl_upoly
*up
, isl_int v
)
897 struct isl_upoly_rec
*rec
;
902 if (isl_upoly_is_cst(up
))
903 return isl_upoly_cst_mul_isl_int(up
, v
);
905 up
= isl_upoly_cow(up
);
906 rec
= isl_upoly_as_rec(up
);
910 for (i
= 0; i
< rec
->n
; ++i
) {
911 rec
->p
[i
] = isl_upoly_mul_isl_int(rec
->p
[i
], v
);
922 /* Multiply the constant polynomial "up" by "v".
924 static __isl_give
struct isl_upoly
*isl_upoly_cst_scale_val(
925 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
927 struct isl_upoly_cst
*cst
;
929 if (isl_upoly_is_zero(up
))
932 up
= isl_upoly_cow(up
);
936 cst
= isl_upoly_as_cst(up
);
938 isl_int_mul(cst
->n
, cst
->n
, v
->n
);
939 isl_int_mul(cst
->d
, cst
->d
, v
->d
);
940 isl_upoly_cst_reduce(cst
);
945 /* Multiply the polynomial "up" by "v".
947 static __isl_give
struct isl_upoly
*isl_upoly_scale_val(
948 __isl_take
struct isl_upoly
*up
, __isl_keep isl_val
*v
)
951 struct isl_upoly_rec
*rec
;
956 if (isl_upoly_is_cst(up
))
957 return isl_upoly_cst_scale_val(up
, v
);
959 up
= isl_upoly_cow(up
);
960 rec
= isl_upoly_as_rec(up
);
964 for (i
= 0; i
< rec
->n
; ++i
) {
965 rec
->p
[i
] = isl_upoly_scale_val(rec
->p
[i
], v
);
976 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
977 __isl_take
struct isl_upoly
*up2
)
979 struct isl_upoly_cst
*cst1
;
980 struct isl_upoly_cst
*cst2
;
982 up1
= isl_upoly_cow(up1
);
986 cst1
= isl_upoly_as_cst(up1
);
987 cst2
= isl_upoly_as_cst(up2
);
989 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
990 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
992 isl_upoly_cst_reduce(cst1
);
1002 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
1003 __isl_take
struct isl_upoly
*up2
)
1005 struct isl_upoly_rec
*rec1
;
1006 struct isl_upoly_rec
*rec2
;
1007 struct isl_upoly_rec
*res
= NULL
;
1011 rec1
= isl_upoly_as_rec(up1
);
1012 rec2
= isl_upoly_as_rec(up2
);
1015 size
= rec1
->n
+ rec2
->n
- 1;
1016 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
1020 for (i
= 0; i
< rec1
->n
; ++i
) {
1021 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
1022 isl_upoly_copy(rec1
->p
[i
]));
1027 for (; i
< size
; ++i
) {
1028 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
1033 for (i
= 0; i
< rec1
->n
; ++i
) {
1034 for (j
= 1; j
< rec2
->n
; ++j
) {
1035 struct isl_upoly
*up
;
1036 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
1037 isl_upoly_copy(rec1
->p
[i
]));
1038 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
1044 isl_upoly_free(up1
);
1045 isl_upoly_free(up2
);
1049 isl_upoly_free(up1
);
1050 isl_upoly_free(up2
);
1051 isl_upoly_free(&res
->up
);
1055 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
1056 __isl_take
struct isl_upoly
*up2
)
1061 if (isl_upoly_is_nan(up1
)) {
1062 isl_upoly_free(up2
);
1066 if (isl_upoly_is_nan(up2
)) {
1067 isl_upoly_free(up1
);
1071 if (isl_upoly_is_zero(up1
)) {
1072 isl_upoly_free(up2
);
1076 if (isl_upoly_is_zero(up2
)) {
1077 isl_upoly_free(up1
);
1081 if (isl_upoly_is_one(up1
)) {
1082 isl_upoly_free(up1
);
1086 if (isl_upoly_is_one(up2
)) {
1087 isl_upoly_free(up2
);
1091 if (up1
->var
< up2
->var
)
1092 return isl_upoly_mul(up2
, up1
);
1094 if (up2
->var
< up1
->var
) {
1096 struct isl_upoly_rec
*rec
;
1097 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
1098 isl_ctx
*ctx
= up1
->ctx
;
1099 isl_upoly_free(up1
);
1100 isl_upoly_free(up2
);
1101 return isl_upoly_nan(ctx
);
1103 up1
= isl_upoly_cow(up1
);
1104 rec
= isl_upoly_as_rec(up1
);
1108 for (i
= 0; i
< rec
->n
; ++i
) {
1109 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
1110 isl_upoly_copy(up2
));
1114 isl_upoly_free(up2
);
1118 if (isl_upoly_is_cst(up1
))
1119 return isl_upoly_mul_cst(up1
, up2
);
1121 return isl_upoly_mul_rec(up1
, up2
);
1123 isl_upoly_free(up1
);
1124 isl_upoly_free(up2
);
1128 __isl_give
struct isl_upoly
*isl_upoly_pow(__isl_take
struct isl_upoly
*up
,
1131 struct isl_upoly
*res
;
1139 res
= isl_upoly_copy(up
);
1141 res
= isl_upoly_one(up
->ctx
);
1143 while (power
>>= 1) {
1144 up
= isl_upoly_mul(up
, isl_upoly_copy(up
));
1146 res
= isl_upoly_mul(res
, isl_upoly_copy(up
));
1153 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_space
*space
,
1154 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
1156 struct isl_qpolynomial
*qp
= NULL
;
1162 if (!isl_space_is_set(space
))
1163 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1164 "domain of polynomial should be a set", goto error
);
1166 total
= isl_space_dim(space
, isl_dim_all
);
1168 qp
= isl_calloc_type(space
->ctx
, struct isl_qpolynomial
);
1173 qp
->div
= isl_mat_alloc(space
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
1182 isl_space_free(space
);
1184 isl_qpolynomial_free(qp
);
1188 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
1197 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
1199 struct isl_qpolynomial
*dup
;
1204 dup
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
,
1205 isl_upoly_copy(qp
->upoly
));
1208 isl_mat_free(dup
->div
);
1209 dup
->div
= isl_mat_copy(qp
->div
);
1215 isl_qpolynomial_free(dup
);
1219 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
1227 return isl_qpolynomial_dup(qp
);
1230 __isl_null isl_qpolynomial
*isl_qpolynomial_free(
1231 __isl_take isl_qpolynomial
*qp
)
1239 isl_space_free(qp
->dim
);
1240 isl_mat_free(qp
->div
);
1241 isl_upoly_free(qp
->upoly
);
1247 __isl_give
struct isl_upoly
*isl_upoly_var_pow(isl_ctx
*ctx
, int pos
, int power
)
1250 struct isl_upoly_rec
*rec
;
1251 struct isl_upoly_cst
*cst
;
1253 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1256 for (i
= 0; i
< 1 + power
; ++i
) {
1257 rec
->p
[i
] = isl_upoly_zero(ctx
);
1262 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1263 isl_int_set_si(cst
->n
, 1);
1267 isl_upoly_free(&rec
->up
);
1271 /* r array maps original positions to new positions.
1273 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
1277 struct isl_upoly_rec
*rec
;
1278 struct isl_upoly
*base
;
1279 struct isl_upoly
*res
;
1281 if (isl_upoly_is_cst(up
))
1284 rec
= isl_upoly_as_rec(up
);
1288 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1290 base
= isl_upoly_var_pow(up
->ctx
, r
[up
->var
], 1);
1291 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
1293 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1294 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1295 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
1298 isl_upoly_free(base
);
1307 static isl_bool
compatible_divs(__isl_keep isl_mat
*div1
,
1308 __isl_keep isl_mat
*div2
)
1313 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
1314 div1
->n_col
>= div2
->n_col
,
1315 return isl_bool_error
);
1317 if (div1
->n_row
== div2
->n_row
)
1318 return isl_mat_is_equal(div1
, div2
);
1320 n_row
= div1
->n_row
;
1321 n_col
= div1
->n_col
;
1322 div1
->n_row
= div2
->n_row
;
1323 div1
->n_col
= div2
->n_col
;
1325 equal
= isl_mat_is_equal(div1
, div2
);
1327 div1
->n_row
= n_row
;
1328 div1
->n_col
= n_col
;
1333 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
1337 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
1338 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
1343 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
1346 struct isl_div_sort_info
{
1351 static int div_sort_cmp(const void *p1
, const void *p2
)
1353 const struct isl_div_sort_info
*i1
, *i2
;
1354 i1
= (const struct isl_div_sort_info
*) p1
;
1355 i2
= (const struct isl_div_sort_info
*) p2
;
1357 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
1360 /* Sort divs and remove duplicates.
1362 static __isl_give isl_qpolynomial
*sort_divs(__isl_take isl_qpolynomial
*qp
)
1367 struct isl_div_sort_info
*array
= NULL
;
1368 int *pos
= NULL
, *at
= NULL
;
1369 int *reordering
= NULL
;
1374 if (qp
->div
->n_row
<= 1)
1377 div_pos
= isl_space_dim(qp
->dim
, isl_dim_all
);
1379 array
= isl_alloc_array(qp
->div
->ctx
, struct isl_div_sort_info
,
1381 pos
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1382 at
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
1383 len
= qp
->div
->n_col
- 2;
1384 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
1385 if (!array
|| !pos
|| !at
|| !reordering
)
1388 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1389 array
[i
].div
= qp
->div
;
1395 qsort(array
, qp
->div
->n_row
, sizeof(struct isl_div_sort_info
),
1398 for (i
= 0; i
< div_pos
; ++i
)
1401 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1402 if (pos
[array
[i
].row
] == i
)
1404 qp
->div
= isl_mat_swap_rows(qp
->div
, i
, pos
[array
[i
].row
]);
1405 pos
[at
[i
]] = pos
[array
[i
].row
];
1406 at
[pos
[array
[i
].row
]] = at
[i
];
1407 at
[i
] = array
[i
].row
;
1408 pos
[array
[i
].row
] = i
;
1412 for (i
= 0; i
< len
- div_pos
; ++i
) {
1414 isl_seq_eq(qp
->div
->row
[i
- skip
- 1],
1415 qp
->div
->row
[i
- skip
], qp
->div
->n_col
)) {
1416 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
1417 isl_mat_col_add(qp
->div
, 2 + div_pos
+ i
- skip
- 1,
1418 2 + div_pos
+ i
- skip
);
1419 qp
->div
= isl_mat_drop_cols(qp
->div
,
1420 2 + div_pos
+ i
- skip
, 1);
1423 reordering
[div_pos
+ array
[i
].row
] = div_pos
+ i
- skip
;
1426 qp
->upoly
= reorder(qp
->upoly
, reordering
);
1428 if (!qp
->upoly
|| !qp
->div
)
1442 isl_qpolynomial_free(qp
);
1446 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1447 int *exp
, int first
)
1450 struct isl_upoly_rec
*rec
;
1452 if (isl_upoly_is_cst(up
))
1455 if (up
->var
< first
)
1458 if (exp
[up
->var
- first
] == up
->var
- first
)
1461 up
= isl_upoly_cow(up
);
1465 up
->var
= exp
[up
->var
- first
] + first
;
1467 rec
= isl_upoly_as_rec(up
);
1471 for (i
= 0; i
< rec
->n
; ++i
) {
1472 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1483 static __isl_give isl_qpolynomial
*with_merged_divs(
1484 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1485 __isl_take isl_qpolynomial
*qp2
),
1486 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1490 isl_mat
*div
= NULL
;
1493 qp1
= isl_qpolynomial_cow(qp1
);
1494 qp2
= isl_qpolynomial_cow(qp2
);
1499 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1500 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1502 n_div1
= qp1
->div
->n_row
;
1503 n_div2
= qp2
->div
->n_row
;
1504 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, n_div1
);
1505 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, n_div2
);
1506 if ((n_div1
&& !exp1
) || (n_div2
&& !exp2
))
1509 div
= isl_merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1513 isl_mat_free(qp1
->div
);
1514 qp1
->div
= isl_mat_copy(div
);
1515 isl_mat_free(qp2
->div
);
1516 qp2
->div
= isl_mat_copy(div
);
1518 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1519 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1521 if (!qp1
->upoly
|| !qp2
->upoly
)
1528 return fn(qp1
, qp2
);
1533 isl_qpolynomial_free(qp1
);
1534 isl_qpolynomial_free(qp2
);
1538 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1539 __isl_take isl_qpolynomial
*qp2
)
1541 isl_bool compatible
;
1543 qp1
= isl_qpolynomial_cow(qp1
);
1548 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1549 return isl_qpolynomial_add(qp2
, qp1
);
1551 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1552 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1556 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1558 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1562 isl_qpolynomial_free(qp2
);
1566 isl_qpolynomial_free(qp1
);
1567 isl_qpolynomial_free(qp2
);
1571 __isl_give isl_qpolynomial
*isl_qpolynomial_add_on_domain(
1572 __isl_keep isl_set
*dom
,
1573 __isl_take isl_qpolynomial
*qp1
,
1574 __isl_take isl_qpolynomial
*qp2
)
1576 qp1
= isl_qpolynomial_add(qp1
, qp2
);
1577 qp1
= isl_qpolynomial_gist(qp1
, isl_set_copy(dom
));
1581 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1582 __isl_take isl_qpolynomial
*qp2
)
1584 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1587 __isl_give isl_qpolynomial
*isl_qpolynomial_add_isl_int(
1588 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1590 if (isl_int_is_zero(v
))
1593 qp
= isl_qpolynomial_cow(qp
);
1597 qp
->upoly
= isl_upoly_add_isl_int(qp
->upoly
, v
);
1603 isl_qpolynomial_free(qp
);
1608 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1613 return isl_qpolynomial_mul_isl_int(qp
, qp
->dim
->ctx
->negone
);
1616 __isl_give isl_qpolynomial
*isl_qpolynomial_mul_isl_int(
1617 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1619 if (isl_int_is_one(v
))
1622 if (qp
&& isl_int_is_zero(v
)) {
1623 isl_qpolynomial
*zero
;
1624 zero
= isl_qpolynomial_zero_on_domain(isl_space_copy(qp
->dim
));
1625 isl_qpolynomial_free(qp
);
1629 qp
= isl_qpolynomial_cow(qp
);
1633 qp
->upoly
= isl_upoly_mul_isl_int(qp
->upoly
, v
);
1639 isl_qpolynomial_free(qp
);
1643 __isl_give isl_qpolynomial
*isl_qpolynomial_scale(
1644 __isl_take isl_qpolynomial
*qp
, isl_int v
)
1646 return isl_qpolynomial_mul_isl_int(qp
, v
);
1649 /* Multiply "qp" by "v".
1651 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_val(
1652 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1657 if (!isl_val_is_rat(v
))
1658 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1659 "expecting rational factor", goto error
);
1661 if (isl_val_is_one(v
)) {
1666 if (isl_val_is_zero(v
)) {
1669 space
= isl_qpolynomial_get_domain_space(qp
);
1670 isl_qpolynomial_free(qp
);
1672 return isl_qpolynomial_zero_on_domain(space
);
1675 qp
= isl_qpolynomial_cow(qp
);
1679 qp
->upoly
= isl_upoly_scale_val(qp
->upoly
, v
);
1681 qp
= isl_qpolynomial_free(qp
);
1687 isl_qpolynomial_free(qp
);
1691 /* Divide "qp" by "v".
1693 __isl_give isl_qpolynomial
*isl_qpolynomial_scale_down_val(
1694 __isl_take isl_qpolynomial
*qp
, __isl_take isl_val
*v
)
1699 if (!isl_val_is_rat(v
))
1700 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
1701 "expecting rational factor", goto error
);
1702 if (isl_val_is_zero(v
))
1703 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1704 "cannot scale down by zero", goto error
);
1706 return isl_qpolynomial_scale_val(qp
, isl_val_inv(v
));
1709 isl_qpolynomial_free(qp
);
1713 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1714 __isl_take isl_qpolynomial
*qp2
)
1716 isl_bool compatible
;
1718 qp1
= isl_qpolynomial_cow(qp1
);
1723 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1724 return isl_qpolynomial_mul(qp2
, qp1
);
1726 isl_assert(qp1
->dim
->ctx
, isl_space_is_equal(qp1
->dim
, qp2
->dim
), goto error
);
1727 compatible
= compatible_divs(qp1
->div
, qp2
->div
);
1731 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1733 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1737 isl_qpolynomial_free(qp2
);
1741 isl_qpolynomial_free(qp1
);
1742 isl_qpolynomial_free(qp2
);
1746 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_qpolynomial
*qp
,
1749 qp
= isl_qpolynomial_cow(qp
);
1754 qp
->upoly
= isl_upoly_pow(qp
->upoly
, power
);
1760 isl_qpolynomial_free(qp
);
1764 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_pow(
1765 __isl_take isl_pw_qpolynomial
*pwqp
, unsigned power
)
1772 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1776 for (i
= 0; i
< pwqp
->n
; ++i
) {
1777 pwqp
->p
[i
].qp
= isl_qpolynomial_pow(pwqp
->p
[i
].qp
, power
);
1779 return isl_pw_qpolynomial_free(pwqp
);
1785 __isl_give isl_qpolynomial
*isl_qpolynomial_zero_on_domain(
1786 __isl_take isl_space
*domain
)
1790 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_zero(domain
->ctx
));
1793 __isl_give isl_qpolynomial
*isl_qpolynomial_one_on_domain(
1794 __isl_take isl_space
*domain
)
1798 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_one(domain
->ctx
));
1801 __isl_give isl_qpolynomial
*isl_qpolynomial_infty_on_domain(
1802 __isl_take isl_space
*domain
)
1806 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_infty(domain
->ctx
));
1809 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty_on_domain(
1810 __isl_take isl_space
*domain
)
1814 return isl_qpolynomial_alloc(domain
, 0,
1815 isl_upoly_neginfty(domain
->ctx
));
1818 __isl_give isl_qpolynomial
*isl_qpolynomial_nan_on_domain(
1819 __isl_take isl_space
*domain
)
1823 return isl_qpolynomial_alloc(domain
, 0, isl_upoly_nan(domain
->ctx
));
1826 __isl_give isl_qpolynomial
*isl_qpolynomial_cst_on_domain(
1827 __isl_take isl_space
*domain
,
1830 struct isl_qpolynomial
*qp
;
1831 struct isl_upoly_cst
*cst
;
1833 qp
= isl_qpolynomial_zero_on_domain(domain
);
1837 cst
= isl_upoly_as_cst(qp
->upoly
);
1838 isl_int_set(cst
->n
, v
);
1843 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1844 isl_int
*n
, isl_int
*d
)
1846 struct isl_upoly_cst
*cst
;
1851 if (!isl_upoly_is_cst(qp
->upoly
))
1854 cst
= isl_upoly_as_cst(qp
->upoly
);
1859 isl_int_set(*n
, cst
->n
);
1861 isl_int_set(*d
, cst
->d
);
1866 /* Return the constant term of "up".
1868 static __isl_give isl_val
*isl_upoly_get_constant_val(
1869 __isl_keep
struct isl_upoly
*up
)
1871 struct isl_upoly_cst
*cst
;
1876 while (!isl_upoly_is_cst(up
)) {
1877 struct isl_upoly_rec
*rec
;
1879 rec
= isl_upoly_as_rec(up
);
1885 cst
= isl_upoly_as_cst(up
);
1888 return isl_val_rat_from_isl_int(cst
->up
.ctx
, cst
->n
, cst
->d
);
1891 /* Return the constant term of "qp".
1893 __isl_give isl_val
*isl_qpolynomial_get_constant_val(
1894 __isl_keep isl_qpolynomial
*qp
)
1899 return isl_upoly_get_constant_val(qp
->upoly
);
1902 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1905 struct isl_upoly_rec
*rec
;
1913 rec
= isl_upoly_as_rec(up
);
1920 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1922 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1928 return isl_upoly_is_affine(rec
->p
[0]);
1931 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1936 if (qp
->div
->n_row
> 0)
1939 return isl_upoly_is_affine(qp
->upoly
);
1942 static void update_coeff(__isl_keep isl_vec
*aff
,
1943 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1948 if (isl_int_is_zero(cst
->n
))
1953 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1954 isl_int_divexact(f
, cst
->d
, gcd
);
1955 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1956 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1957 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1962 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1963 __isl_keep isl_vec
*aff
)
1965 struct isl_upoly_cst
*cst
;
1966 struct isl_upoly_rec
*rec
;
1972 struct isl_upoly_cst
*cst
;
1974 cst
= isl_upoly_as_cst(up
);
1977 update_coeff(aff
, cst
, 0);
1981 rec
= isl_upoly_as_rec(up
);
1984 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1986 cst
= isl_upoly_as_cst(rec
->p
[1]);
1989 update_coeff(aff
, cst
, 1 + up
->var
);
1991 return isl_upoly_update_affine(rec
->p
[0], aff
);
1994 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1995 __isl_keep isl_qpolynomial
*qp
)
2003 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2004 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
+ qp
->div
->n_row
);
2008 isl_seq_clr(aff
->el
+ 1, 1 + d
+ qp
->div
->n_row
);
2009 isl_int_set_si(aff
->el
[0], 1);
2011 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
2020 /* Compare two quasi-polynomials.
2022 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2023 * than "qp2" and 0 if they are equal.
2025 int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial
*qp1
,
2026 __isl_keep isl_qpolynomial
*qp2
)
2037 cmp
= isl_space_cmp(qp1
->dim
, qp2
->dim
);
2041 cmp
= isl_local_cmp(qp1
->div
, qp2
->div
);
2045 return isl_upoly_plain_cmp(qp1
->upoly
, qp2
->upoly
);
2048 /* Is "qp1" obviously equal to "qp2"?
2050 * NaN is not equal to anything, not even to another NaN.
2052 isl_bool
isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial
*qp1
,
2053 __isl_keep isl_qpolynomial
*qp2
)
2058 return isl_bool_error
;
2060 if (isl_qpolynomial_is_nan(qp1
) || isl_qpolynomial_is_nan(qp2
))
2061 return isl_bool_false
;
2063 equal
= isl_space_is_equal(qp1
->dim
, qp2
->dim
);
2064 if (equal
< 0 || !equal
)
2067 equal
= isl_mat_is_equal(qp1
->div
, qp2
->div
);
2068 if (equal
< 0 || !equal
)
2071 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
2074 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
2077 struct isl_upoly_rec
*rec
;
2079 if (isl_upoly_is_cst(up
)) {
2080 struct isl_upoly_cst
*cst
;
2081 cst
= isl_upoly_as_cst(up
);
2084 isl_int_lcm(*d
, *d
, cst
->d
);
2088 rec
= isl_upoly_as_rec(up
);
2092 for (i
= 0; i
< rec
->n
; ++i
)
2093 upoly_update_den(rec
->p
[i
], d
);
2096 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
2098 isl_int_set_si(*d
, 1);
2101 upoly_update_den(qp
->upoly
, d
);
2104 __isl_give isl_qpolynomial
*isl_qpolynomial_var_pow_on_domain(
2105 __isl_take isl_space
*domain
, int pos
, int power
)
2107 struct isl_ctx
*ctx
;
2114 return isl_qpolynomial_alloc(domain
, 0,
2115 isl_upoly_var_pow(ctx
, pos
, power
));
2118 __isl_give isl_qpolynomial
*isl_qpolynomial_var_on_domain(
2119 __isl_take isl_space
*domain
, enum isl_dim_type type
, unsigned pos
)
2121 if (isl_space_check_is_set(domain
) < 0)
2123 if (isl_space_check_range(domain
, type
, pos
, 1) < 0)
2126 if (type
== isl_dim_set
)
2127 pos
+= isl_space_dim(domain
, isl_dim_param
);
2129 return isl_qpolynomial_var_pow_on_domain(domain
, pos
, 1);
2131 isl_space_free(domain
);
2135 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2136 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2139 struct isl_upoly_rec
*rec
;
2140 struct isl_upoly
*base
, *res
;
2145 if (isl_upoly_is_cst(up
))
2148 if (up
->var
< first
)
2151 rec
= isl_upoly_as_rec(up
);
2155 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2157 if (up
->var
>= first
+ n
)
2158 base
= isl_upoly_var_pow(up
->ctx
, up
->var
, 1);
2160 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2162 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2163 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2164 struct isl_upoly
*t
;
2165 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2166 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2167 res
= isl_upoly_sum(res
, t
);
2170 isl_upoly_free(base
);
2179 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2180 isl_int denom
, unsigned len
)
2183 struct isl_upoly
*up
;
2185 isl_assert(ctx
, len
>= 1, return NULL
);
2187 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2188 for (i
= 0; i
< len
- 1; ++i
) {
2189 struct isl_upoly
*t
;
2190 struct isl_upoly
*c
;
2192 if (isl_int_is_zero(f
[1 + i
]))
2195 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2196 t
= isl_upoly_var_pow(ctx
, i
, 1);
2197 t
= isl_upoly_mul(c
, t
);
2198 up
= isl_upoly_sum(up
, t
);
2204 /* Remove common factor of non-constant terms and denominator.
2206 static void normalize_div(__isl_keep isl_qpolynomial
*qp
, int div
)
2208 isl_ctx
*ctx
= qp
->div
->ctx
;
2209 unsigned total
= qp
->div
->n_col
- 2;
2211 isl_seq_gcd(qp
->div
->row
[div
] + 2, total
, &ctx
->normalize_gcd
);
2212 isl_int_gcd(ctx
->normalize_gcd
,
2213 ctx
->normalize_gcd
, qp
->div
->row
[div
][0]);
2214 if (isl_int_is_one(ctx
->normalize_gcd
))
2217 isl_seq_scale_down(qp
->div
->row
[div
] + 2, qp
->div
->row
[div
] + 2,
2218 ctx
->normalize_gcd
, total
);
2219 isl_int_divexact(qp
->div
->row
[div
][0], qp
->div
->row
[div
][0],
2220 ctx
->normalize_gcd
);
2221 isl_int_fdiv_q(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1],
2222 ctx
->normalize_gcd
);
2225 /* Replace the integer division identified by "div" by the polynomial "s".
2226 * The integer division is assumed not to appear in the definition
2227 * of any other integer divisions.
2229 static __isl_give isl_qpolynomial
*substitute_div(
2230 __isl_take isl_qpolynomial
*qp
,
2231 int div
, __isl_take
struct isl_upoly
*s
)
2240 qp
= isl_qpolynomial_cow(qp
);
2244 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2245 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ div
, 1, &s
);
2249 reordering
= isl_alloc_array(qp
->dim
->ctx
, int, total
+ qp
->div
->n_row
);
2252 for (i
= 0; i
< total
+ div
; ++i
)
2254 for (i
= total
+ div
+ 1; i
< total
+ qp
->div
->n_row
; ++i
)
2255 reordering
[i
] = i
- 1;
2256 qp
->div
= isl_mat_drop_rows(qp
->div
, div
, 1);
2257 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + total
+ div
, 1);
2258 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2261 if (!qp
->upoly
|| !qp
->div
)
2267 isl_qpolynomial_free(qp
);
2272 /* Replace all integer divisions [e/d] that turn out to not actually be integer
2273 * divisions because d is equal to 1 by their definition, i.e., e.
2275 static __isl_give isl_qpolynomial
*substitute_non_divs(
2276 __isl_take isl_qpolynomial
*qp
)
2280 struct isl_upoly
*s
;
2285 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
2286 for (i
= 0; qp
&& i
< qp
->div
->n_row
; ++i
) {
2287 if (!isl_int_is_one(qp
->div
->row
[i
][0]))
2289 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
2290 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
2292 isl_seq_combine(qp
->div
->row
[j
] + 1,
2293 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
2294 qp
->div
->row
[j
][2 + total
+ i
],
2295 qp
->div
->row
[i
] + 1, 1 + total
+ i
);
2296 isl_int_set_si(qp
->div
->row
[j
][2 + total
+ i
], 0);
2297 normalize_div(qp
, j
);
2299 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
2300 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
2301 qp
= substitute_div(qp
, i
, s
);
2308 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2309 * with d the denominator. When replacing the coefficient e of x by
2310 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2311 * inside the division, so we need to add floor(e/d) * x outside.
2312 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2313 * to adjust the coefficient of x in each later div that depends on the
2314 * current div "div" and also in the affine expressions in the rows of "mat"
2315 * (if they too depend on "div").
2317 static void reduce_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2318 __isl_keep isl_mat
**mat
)
2322 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2325 for (i
= 0; i
< 1 + total
+ div
; ++i
) {
2326 if (isl_int_is_nonneg(qp
->div
->row
[div
][1 + i
]) &&
2327 isl_int_lt(qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]))
2329 isl_int_fdiv_q(v
, qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2330 isl_int_fdiv_r(qp
->div
->row
[div
][1 + i
],
2331 qp
->div
->row
[div
][1 + i
], qp
->div
->row
[div
][0]);
2332 *mat
= isl_mat_col_addmul(*mat
, i
, v
, 1 + total
+ div
);
2333 for (j
= div
+ 1; j
< qp
->div
->n_row
; ++j
) {
2334 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ div
]))
2336 isl_int_addmul(qp
->div
->row
[j
][1 + i
],
2337 v
, qp
->div
->row
[j
][2 + total
+ div
]);
2343 /* Check if the last non-zero coefficient is bigger that half of the
2344 * denominator. If so, we will invert the div to further reduce the number
2345 * of distinct divs that may appear.
2346 * If the last non-zero coefficient is exactly half the denominator,
2347 * then we continue looking for earlier coefficients that are bigger
2348 * than half the denominator.
2350 static int needs_invert(__isl_keep isl_mat
*div
, int row
)
2355 for (i
= div
->n_col
- 1; i
>= 1; --i
) {
2356 if (isl_int_is_zero(div
->row
[row
][i
]))
2358 isl_int_mul_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2359 cmp
= isl_int_cmp(div
->row
[row
][i
], div
->row
[row
][0]);
2360 isl_int_divexact_ui(div
->row
[row
][i
], div
->row
[row
][i
], 2);
2370 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2371 * We only invert the coefficients of e (and the coefficient of q in
2372 * later divs and in the rows of "mat"). After calling this function, the
2373 * coefficients of e should be reduced again.
2375 static void invert_div(__isl_keep isl_qpolynomial
*qp
, int div
,
2376 __isl_keep isl_mat
**mat
)
2378 unsigned total
= qp
->div
->n_col
- qp
->div
->n_row
- 2;
2380 isl_seq_neg(qp
->div
->row
[div
] + 1,
2381 qp
->div
->row
[div
] + 1, qp
->div
->n_col
- 1);
2382 isl_int_sub_ui(qp
->div
->row
[div
][1], qp
->div
->row
[div
][1], 1);
2383 isl_int_add(qp
->div
->row
[div
][1],
2384 qp
->div
->row
[div
][1], qp
->div
->row
[div
][0]);
2385 *mat
= isl_mat_col_neg(*mat
, 1 + total
+ div
);
2386 isl_mat_col_mul(qp
->div
, 2 + total
+ div
,
2387 qp
->div
->ctx
->negone
, 2 + total
+ div
);
2390 /* Reduce all divs of "qp" to have coefficients
2391 * in the interval [0, d-1], with d the denominator and such that the
2392 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2393 * The modifications to the integer divisions need to be reflected
2394 * in the factors of the polynomial that refer to the original
2395 * integer divisions. To this end, the modifications are collected
2396 * as a set of affine expressions and then plugged into the polynomial.
2398 * After the reduction, some divs may have become redundant or identical,
2399 * so we call substitute_non_divs and sort_divs. If these functions
2400 * eliminate divs or merge two or more divs into one, the coefficients
2401 * of the enclosing divs may have to be reduced again, so we call
2402 * ourselves recursively if the number of divs decreases.
2404 static __isl_give isl_qpolynomial
*reduce_divs(__isl_take isl_qpolynomial
*qp
)
2409 struct isl_upoly
**s
;
2410 unsigned o_div
, n_div
, total
;
2415 total
= isl_qpolynomial_domain_dim(qp
, isl_dim_all
);
2416 n_div
= isl_qpolynomial_domain_dim(qp
, isl_dim_div
);
2417 o_div
= isl_qpolynomial_domain_offset(qp
, isl_dim_div
);
2418 ctx
= isl_qpolynomial_get_ctx(qp
);
2419 mat
= isl_mat_zero(ctx
, n_div
, 1 + total
);
2421 for (i
= 0; i
< n_div
; ++i
)
2422 mat
= isl_mat_set_element_si(mat
, i
, o_div
+ i
, 1);
2424 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
2425 normalize_div(qp
, i
);
2426 reduce_div(qp
, i
, &mat
);
2427 if (needs_invert(qp
->div
, i
)) {
2428 invert_div(qp
, i
, &mat
);
2429 reduce_div(qp
, i
, &mat
);
2435 s
= isl_alloc_array(ctx
, struct isl_upoly
*, n_div
);
2438 for (i
= 0; i
< n_div
; ++i
)
2439 s
[i
] = isl_upoly_from_affine(ctx
, mat
->row
[i
], ctx
->one
,
2441 qp
->upoly
= isl_upoly_subs(qp
->upoly
, o_div
- 1, n_div
, s
);
2442 for (i
= 0; i
< n_div
; ++i
)
2443 isl_upoly_free(s
[i
]);
2450 qp
= substitute_non_divs(qp
);
2452 if (qp
&& isl_qpolynomial_domain_dim(qp
, isl_dim_div
) < n_div
)
2453 return reduce_divs(qp
);
2457 isl_qpolynomial_free(qp
);
2462 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst_on_domain(
2463 __isl_take isl_space
*domain
, const isl_int n
, const isl_int d
)
2465 struct isl_qpolynomial
*qp
;
2466 struct isl_upoly_cst
*cst
;
2468 qp
= isl_qpolynomial_zero_on_domain(domain
);
2472 cst
= isl_upoly_as_cst(qp
->upoly
);
2473 isl_int_set(cst
->n
, n
);
2474 isl_int_set(cst
->d
, d
);
2479 /* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2481 __isl_give isl_qpolynomial
*isl_qpolynomial_val_on_domain(
2482 __isl_take isl_space
*domain
, __isl_take isl_val
*val
)
2484 isl_qpolynomial
*qp
;
2485 struct isl_upoly_cst
*cst
;
2487 qp
= isl_qpolynomial_zero_on_domain(domain
);
2491 cst
= isl_upoly_as_cst(qp
->upoly
);
2492 isl_int_set(cst
->n
, val
->n
);
2493 isl_int_set(cst
->d
, val
->d
);
2499 isl_qpolynomial_free(qp
);
2503 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
2505 struct isl_upoly_rec
*rec
;
2511 if (isl_upoly_is_cst(up
))
2515 active
[up
->var
] = 1;
2517 rec
= isl_upoly_as_rec(up
);
2518 for (i
= 0; i
< rec
->n
; ++i
)
2519 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
2525 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
2528 int d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2533 for (i
= 0; i
< d
; ++i
)
2534 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
2535 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
2541 return up_set_active(qp
->upoly
, active
, d
);
2545 #define TYPE isl_qpolynomial
2547 #include "check_type_range_templ.c"
2549 isl_bool
isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
2550 enum isl_dim_type type
, unsigned first
, unsigned n
)
2554 isl_bool involves
= isl_bool_false
;
2557 return isl_bool_error
;
2559 return isl_bool_false
;
2561 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2562 return isl_bool_error
;
2563 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2564 type
== isl_dim_in
, return isl_bool_error
);
2566 active
= isl_calloc_array(qp
->dim
->ctx
, int,
2567 isl_space_dim(qp
->dim
, isl_dim_all
));
2568 if (set_active(qp
, active
) < 0)
2571 if (type
== isl_dim_in
)
2572 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2573 for (i
= 0; i
< n
; ++i
)
2574 if (active
[first
+ i
]) {
2575 involves
= isl_bool_true
;
2584 return isl_bool_error
;
2587 /* Remove divs that do not appear in the quasi-polynomial, nor in any
2588 * of the divs that do appear in the quasi-polynomial.
2590 static __isl_give isl_qpolynomial
*remove_redundant_divs(
2591 __isl_take isl_qpolynomial
*qp
)
2598 int *reordering
= NULL
;
2605 if (qp
->div
->n_row
== 0)
2608 d
= isl_space_dim(qp
->dim
, isl_dim_all
);
2609 len
= qp
->div
->n_col
- 2;
2610 ctx
= isl_qpolynomial_get_ctx(qp
);
2611 active
= isl_calloc_array(ctx
, int, len
);
2615 if (up_set_active(qp
->upoly
, active
, len
) < 0)
2618 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
2619 if (!active
[d
+ i
]) {
2623 for (j
= 0; j
< i
; ++j
) {
2624 if (isl_int_is_zero(qp
->div
->row
[i
][2 + d
+ j
]))
2636 reordering
= isl_alloc_array(qp
->div
->ctx
, int, len
);
2640 for (i
= 0; i
< d
; ++i
)
2644 n_div
= qp
->div
->n_row
;
2645 for (i
= 0; i
< n_div
; ++i
) {
2646 if (!active
[d
+ i
]) {
2647 qp
->div
= isl_mat_drop_rows(qp
->div
, i
- skip
, 1);
2648 qp
->div
= isl_mat_drop_cols(qp
->div
,
2649 2 + d
+ i
- skip
, 1);
2652 reordering
[d
+ i
] = d
+ i
- skip
;
2655 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2657 if (!qp
->upoly
|| !qp
->div
)
2667 isl_qpolynomial_free(qp
);
2671 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
2672 unsigned first
, unsigned n
)
2675 struct isl_upoly_rec
*rec
;
2679 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
2681 if (up
->var
< first
+ n
) {
2682 up
= replace_by_constant_term(up
);
2683 return isl_upoly_drop(up
, first
, n
);
2685 up
= isl_upoly_cow(up
);
2689 rec
= isl_upoly_as_rec(up
);
2693 for (i
= 0; i
< rec
->n
; ++i
) {
2694 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
2705 __isl_give isl_qpolynomial
*isl_qpolynomial_set_dim_name(
2706 __isl_take isl_qpolynomial
*qp
,
2707 enum isl_dim_type type
, unsigned pos
, const char *s
)
2709 qp
= isl_qpolynomial_cow(qp
);
2712 if (type
== isl_dim_out
)
2713 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2714 "cannot set name of output/set dimension",
2715 return isl_qpolynomial_free(qp
));
2716 type
= domain_type(type
);
2717 qp
->dim
= isl_space_set_dim_name(qp
->dim
, type
, pos
, s
);
2722 isl_qpolynomial_free(qp
);
2726 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
2727 __isl_take isl_qpolynomial
*qp
,
2728 enum isl_dim_type type
, unsigned first
, unsigned n
)
2732 if (type
== isl_dim_out
)
2733 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
2734 "cannot drop output/set dimension",
2736 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
2737 return isl_qpolynomial_free(qp
);
2738 type
= domain_type(type
);
2739 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
2742 qp
= isl_qpolynomial_cow(qp
);
2746 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
2747 type
== isl_dim_set
, goto error
);
2749 qp
->dim
= isl_space_drop_dims(qp
->dim
, type
, first
, n
);
2753 if (type
== isl_dim_set
)
2754 first
+= isl_space_dim(qp
->dim
, isl_dim_param
);
2756 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
2760 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
2766 isl_qpolynomial_free(qp
);
2770 /* Project the domain of the quasi-polynomial onto its parameter space.
2771 * The quasi-polynomial may not involve any of the domain dimensions.
2773 __isl_give isl_qpolynomial
*isl_qpolynomial_project_domain_on_params(
2774 __isl_take isl_qpolynomial
*qp
)
2780 n
= isl_qpolynomial_dim(qp
, isl_dim_in
);
2781 involves
= isl_qpolynomial_involves_dims(qp
, isl_dim_in
, 0, n
);
2783 return isl_qpolynomial_free(qp
);
2785 isl_die(isl_qpolynomial_get_ctx(qp
), isl_error_invalid
,
2786 "polynomial involves some of the domain dimensions",
2787 return isl_qpolynomial_free(qp
));
2788 qp
= isl_qpolynomial_drop_dims(qp
, isl_dim_in
, 0, n
);
2789 space
= isl_qpolynomial_get_domain_space(qp
);
2790 space
= isl_space_params(space
);
2791 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
2795 static __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities_lifted(
2796 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2802 struct isl_upoly
*up
;
2806 if (eq
->n_eq
== 0) {
2807 isl_basic_set_free(eq
);
2811 qp
= isl_qpolynomial_cow(qp
);
2814 qp
->div
= isl_mat_cow(qp
->div
);
2818 total
= 1 + isl_space_dim(eq
->dim
, isl_dim_all
);
2820 isl_int_init(denom
);
2821 for (i
= 0; i
< eq
->n_eq
; ++i
) {
2822 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
2823 if (j
< 0 || j
== 0 || j
>= total
)
2826 for (k
= 0; k
< qp
->div
->n_row
; ++k
) {
2827 if (isl_int_is_zero(qp
->div
->row
[k
][1 + j
]))
2829 isl_seq_elim(qp
->div
->row
[k
] + 1, eq
->eq
[i
], j
, total
,
2830 &qp
->div
->row
[k
][0]);
2831 normalize_div(qp
, k
);
2834 if (isl_int_is_pos(eq
->eq
[i
][j
]))
2835 isl_seq_neg(eq
->eq
[i
], eq
->eq
[i
], total
);
2836 isl_int_abs(denom
, eq
->eq
[i
][j
]);
2837 isl_int_set_si(eq
->eq
[i
][j
], 0);
2839 up
= isl_upoly_from_affine(qp
->dim
->ctx
,
2840 eq
->eq
[i
], denom
, total
);
2841 qp
->upoly
= isl_upoly_subs(qp
->upoly
, j
- 1, 1, &up
);
2844 isl_int_clear(denom
);
2849 isl_basic_set_free(eq
);
2851 qp
= substitute_non_divs(qp
);
2856 isl_basic_set_free(eq
);
2857 isl_qpolynomial_free(qp
);
2861 /* Exploit the equalities in "eq" to simplify the quasi-polynomial.
2863 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute_equalities(
2864 __isl_take isl_qpolynomial
*qp
, __isl_take isl_basic_set
*eq
)
2868 if (qp
->div
->n_row
> 0)
2869 eq
= isl_basic_set_add_dims(eq
, isl_dim_set
, qp
->div
->n_row
);
2870 return isl_qpolynomial_substitute_equalities_lifted(qp
, eq
);
2872 isl_basic_set_free(eq
);
2873 isl_qpolynomial_free(qp
);
2877 /* Look for equalities among the variables shared by context and qp
2878 * and the integer divisions of qp, if any.
2879 * The equalities are then used to eliminate variables and/or integer
2880 * divisions from qp.
2882 __isl_give isl_qpolynomial
*isl_qpolynomial_gist(
2883 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2885 isl_local_space
*ls
;
2888 ls
= isl_qpolynomial_get_domain_local_space(qp
);
2889 context
= isl_local_space_lift_set(ls
, context
);
2891 aff
= isl_set_affine_hull(context
);
2892 return isl_qpolynomial_substitute_equalities_lifted(qp
, aff
);
2895 __isl_give isl_qpolynomial
*isl_qpolynomial_gist_params(
2896 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*context
)
2898 isl_space
*space
= isl_qpolynomial_get_domain_space(qp
);
2899 isl_set
*dom_context
= isl_set_universe(space
);
2900 dom_context
= isl_set_intersect_params(dom_context
, context
);
2901 return isl_qpolynomial_gist(qp
, dom_context
);
2904 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_qpolynomial(
2905 __isl_take isl_qpolynomial
*qp
)
2911 if (isl_qpolynomial_is_zero(qp
)) {
2912 isl_space
*dim
= isl_qpolynomial_get_space(qp
);
2913 isl_qpolynomial_free(qp
);
2914 return isl_pw_qpolynomial_zero(dim
);
2917 dom
= isl_set_universe(isl_qpolynomial_get_domain_space(qp
));
2918 return isl_pw_qpolynomial_alloc(dom
, qp
);
2921 #define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
2924 #define PW isl_pw_qpolynomial
2926 #define EL isl_qpolynomial
2928 #define EL_IS_ZERO is_zero
2932 #define IS_ZERO is_zero
2935 #undef DEFAULT_IS_ZERO
2936 #define DEFAULT_IS_ZERO 1
2940 #include <isl_pw_templ.c>
2941 #include <isl_pw_eval.c>
2944 #define BASE pw_qpolynomial
2946 #include <isl_union_single.c>
2947 #include <isl_union_eval.c>
2948 #include <isl_union_neg.c>
2950 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
2958 if (!isl_set_plain_is_universe(pwqp
->p
[0].set
))
2961 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
2964 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add(
2965 __isl_take isl_pw_qpolynomial
*pwqp1
,
2966 __isl_take isl_pw_qpolynomial
*pwqp2
)
2968 return isl_pw_qpolynomial_union_add_(pwqp1
, pwqp2
);
2971 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
2972 __isl_take isl_pw_qpolynomial
*pwqp1
,
2973 __isl_take isl_pw_qpolynomial
*pwqp2
)
2976 struct isl_pw_qpolynomial
*res
;
2978 if (!pwqp1
|| !pwqp2
)
2981 isl_assert(pwqp1
->dim
->ctx
, isl_space_is_equal(pwqp1
->dim
, pwqp2
->dim
),
2984 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
2985 isl_pw_qpolynomial_free(pwqp2
);
2989 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
2990 isl_pw_qpolynomial_free(pwqp1
);
2994 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
2995 isl_pw_qpolynomial_free(pwqp1
);
2999 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
3000 isl_pw_qpolynomial_free(pwqp2
);
3004 n
= pwqp1
->n
* pwqp2
->n
;
3005 res
= isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1
->dim
), n
);
3007 for (i
= 0; i
< pwqp1
->n
; ++i
) {
3008 for (j
= 0; j
< pwqp2
->n
; ++j
) {
3009 struct isl_set
*common
;
3010 struct isl_qpolynomial
*prod
;
3011 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
3012 isl_set_copy(pwqp2
->p
[j
].set
));
3013 if (isl_set_plain_is_empty(common
)) {
3014 isl_set_free(common
);
3018 prod
= isl_qpolynomial_mul(
3019 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
3020 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
3022 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
3026 isl_pw_qpolynomial_free(pwqp1
);
3027 isl_pw_qpolynomial_free(pwqp2
);
3031 isl_pw_qpolynomial_free(pwqp1
);
3032 isl_pw_qpolynomial_free(pwqp2
);
3036 __isl_give isl_val
*isl_upoly_eval(__isl_take
struct isl_upoly
*up
,
3037 __isl_take isl_vec
*vec
)
3040 struct isl_upoly_rec
*rec
;
3044 if (isl_upoly_is_cst(up
)) {
3046 res
= isl_upoly_get_constant_val(up
);
3051 rec
= isl_upoly_as_rec(up
);
3055 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
3057 base
= isl_val_rat_from_isl_int(up
->ctx
,
3058 vec
->el
[1 + up
->var
], vec
->el
[0]);
3060 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
3063 for (i
= rec
->n
- 2; i
>= 0; --i
) {
3064 res
= isl_val_mul(res
, isl_val_copy(base
));
3065 res
= isl_val_add(res
,
3066 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
3067 isl_vec_copy(vec
)));
3080 /* Evaluate "qp" in the void point "pnt".
3081 * In particular, return the value NaN.
3083 static __isl_give isl_val
*eval_void(__isl_take isl_qpolynomial
*qp
,
3084 __isl_take isl_point
*pnt
)
3088 ctx
= isl_point_get_ctx(pnt
);
3089 isl_qpolynomial_free(qp
);
3090 isl_point_free(pnt
);
3091 return isl_val_nan(ctx
);
3094 __isl_give isl_val
*isl_qpolynomial_eval(__isl_take isl_qpolynomial
*qp
,
3095 __isl_take isl_point
*pnt
)
3103 isl_assert(pnt
->dim
->ctx
, isl_space_is_equal(pnt
->dim
, qp
->dim
), goto error
);
3104 is_void
= isl_point_is_void(pnt
);
3108 return eval_void(qp
, pnt
);
3110 ext
= isl_local_extend_point_vec(qp
->div
, isl_vec_copy(pnt
->vec
));
3112 v
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
3114 isl_qpolynomial_free(qp
);
3115 isl_point_free(pnt
);
3119 isl_qpolynomial_free(qp
);
3120 isl_point_free(pnt
);
3124 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
3125 __isl_keep
struct isl_upoly_cst
*cst2
)
3130 isl_int_mul(t
, cst1
->n
, cst2
->d
);
3131 isl_int_submul(t
, cst2
->n
, cst1
->d
);
3132 cmp
= isl_int_sgn(t
);
3137 __isl_give isl_qpolynomial
*isl_qpolynomial_insert_dims(
3138 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
,
3139 unsigned first
, unsigned n
)
3147 if (type
== isl_dim_out
)
3148 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3149 "cannot insert output/set dimensions",
3151 if (isl_qpolynomial_check_range(qp
, type
, first
, 0) < 0)
3152 return isl_qpolynomial_free(qp
);
3153 type
= domain_type(type
);
3154 if (n
== 0 && !isl_space_is_named_or_nested(qp
->dim
, type
))
3157 qp
= isl_qpolynomial_cow(qp
);
3161 g_pos
= pos(qp
->dim
, type
) + first
;
3163 qp
->div
= isl_mat_insert_zero_cols(qp
->div
, 2 + g_pos
, n
);
3167 total
= qp
->div
->n_col
- 2;
3168 if (total
> g_pos
) {
3170 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
3173 for (i
= 0; i
< total
- g_pos
; ++i
)
3175 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
3181 qp
->dim
= isl_space_insert_dims(qp
->dim
, type
, first
, n
);
3187 isl_qpolynomial_free(qp
);
3191 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
3192 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
3196 pos
= isl_qpolynomial_dim(qp
, type
);
3198 return isl_qpolynomial_insert_dims(qp
, type
, pos
, n
);
3201 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
3202 __isl_take isl_pw_qpolynomial
*pwqp
,
3203 enum isl_dim_type type
, unsigned n
)
3207 pos
= isl_pw_qpolynomial_dim(pwqp
, type
);
3209 return isl_pw_qpolynomial_insert_dims(pwqp
, type
, pos
, n
);
3212 static int *reordering_move(isl_ctx
*ctx
,
3213 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
3218 reordering
= isl_alloc_array(ctx
, int, len
);
3223 for (i
= 0; i
< dst
; ++i
)
3225 for (i
= 0; i
< n
; ++i
)
3226 reordering
[src
+ i
] = dst
+ i
;
3227 for (i
= 0; i
< src
- dst
; ++i
)
3228 reordering
[dst
+ i
] = dst
+ n
+ i
;
3229 for (i
= 0; i
< len
- src
- n
; ++i
)
3230 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
3232 for (i
= 0; i
< src
; ++i
)
3234 for (i
= 0; i
< n
; ++i
)
3235 reordering
[src
+ i
] = dst
+ i
;
3236 for (i
= 0; i
< dst
- src
; ++i
)
3237 reordering
[src
+ n
+ i
] = src
+ i
;
3238 for (i
= 0; i
< len
- dst
- n
; ++i
)
3239 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
3245 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
3246 __isl_take isl_qpolynomial
*qp
,
3247 enum isl_dim_type dst_type
, unsigned dst_pos
,
3248 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
3257 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
3258 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3259 "cannot move output/set dimension",
3261 if (isl_qpolynomial_check_range(qp
, src_type
, src_pos
, n
) < 0)
3262 return isl_qpolynomial_free(qp
);
3263 if (dst_type
== isl_dim_in
)
3264 dst_type
= isl_dim_set
;
3265 if (src_type
== isl_dim_in
)
3266 src_type
= isl_dim_set
;
3269 !isl_space_is_named_or_nested(qp
->dim
, src_type
) &&
3270 !isl_space_is_named_or_nested(qp
->dim
, dst_type
))
3273 qp
= isl_qpolynomial_cow(qp
);
3277 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
3278 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
3279 if (dst_type
> src_type
)
3282 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
3289 reordering
= reordering_move(qp
->dim
->ctx
,
3290 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
3294 qp
->upoly
= reorder(qp
->upoly
, reordering
);
3299 qp
->dim
= isl_space_move_dims(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
3305 isl_qpolynomial_free(qp
);
3309 __isl_give isl_qpolynomial
*isl_qpolynomial_from_affine(
3310 __isl_take isl_space
*space
, isl_int
*f
, isl_int denom
)
3312 struct isl_upoly
*up
;
3314 space
= isl_space_domain(space
);
3318 up
= isl_upoly_from_affine(space
->ctx
, f
, denom
,
3319 1 + isl_space_dim(space
, isl_dim_all
));
3321 return isl_qpolynomial_alloc(space
, 0, up
);
3324 __isl_give isl_qpolynomial
*isl_qpolynomial_from_aff(__isl_take isl_aff
*aff
)
3327 struct isl_upoly
*up
;
3328 isl_qpolynomial
*qp
;
3333 ctx
= isl_aff_get_ctx(aff
);
3334 up
= isl_upoly_from_affine(ctx
, aff
->v
->el
+ 1, aff
->v
->el
[0],
3337 qp
= isl_qpolynomial_alloc(isl_aff_get_domain_space(aff
),
3338 aff
->ls
->div
->n_row
, up
);
3342 isl_mat_free(qp
->div
);
3343 qp
->div
= isl_mat_copy(aff
->ls
->div
);
3344 qp
->div
= isl_mat_cow(qp
->div
);
3349 qp
= reduce_divs(qp
);
3350 qp
= remove_redundant_divs(qp
);
3354 return isl_qpolynomial_free(qp
);
3357 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_pw_aff(
3358 __isl_take isl_pw_aff
*pwaff
)
3361 isl_pw_qpolynomial
*pwqp
;
3366 pwqp
= isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff
),
3369 for (i
= 0; i
< pwaff
->n
; ++i
) {
3371 isl_qpolynomial
*qp
;
3373 dom
= isl_set_copy(pwaff
->p
[i
].set
);
3374 qp
= isl_qpolynomial_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
3375 pwqp
= isl_pw_qpolynomial_add_piece(pwqp
, dom
, qp
);
3378 isl_pw_aff_free(pwaff
);
3382 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
3383 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
3387 aff
= isl_constraint_get_bound(c
, type
, pos
);
3388 isl_constraint_free(c
);
3389 return isl_qpolynomial_from_aff(aff
);
3392 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
3393 * in "qp" by subs[i].
3395 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
3396 __isl_take isl_qpolynomial
*qp
,
3397 enum isl_dim_type type
, unsigned first
, unsigned n
,
3398 __isl_keep isl_qpolynomial
**subs
)
3401 struct isl_upoly
**ups
;
3406 qp
= isl_qpolynomial_cow(qp
);
3410 if (type
== isl_dim_out
)
3411 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
3412 "cannot substitute output/set dimension",
3414 if (isl_qpolynomial_check_range(qp
, type
, first
, n
) < 0)
3415 return isl_qpolynomial_free(qp
);
3416 type
= domain_type(type
);
3418 for (i
= 0; i
< n
; ++i
)
3422 for (i
= 0; i
< n
; ++i
)
3423 isl_assert(qp
->dim
->ctx
, isl_space_is_equal(qp
->dim
, subs
[i
]->dim
),
3426 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
3427 for (i
= 0; i
< n
; ++i
)
3428 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
3430 first
+= pos(qp
->dim
, type
);
3432 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
3435 for (i
= 0; i
< n
; ++i
)
3436 ups
[i
] = subs
[i
]->upoly
;
3438 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
3447 isl_qpolynomial_free(qp
);
3451 /* Extend "bset" with extra set dimensions for each integer division
3452 * in "qp" and then call "fn" with the extended bset and the polynomial
3453 * that results from replacing each of the integer divisions by the
3454 * corresponding extra set dimension.
3456 isl_stat
isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
3457 __isl_keep isl_basic_set
*bset
,
3458 isl_stat (*fn
)(__isl_take isl_basic_set
*bset
,
3459 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
3462 isl_local_space
*ls
;
3463 isl_qpolynomial
*poly
;
3466 return isl_stat_error
;
3467 if (qp
->div
->n_row
== 0)
3468 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
3471 space
= isl_space_copy(qp
->dim
);
3472 space
= isl_space_add_dims(space
, isl_dim_set
, qp
->div
->n_row
);
3473 poly
= isl_qpolynomial_alloc(space
, 0, isl_upoly_copy(qp
->upoly
));
3474 bset
= isl_basic_set_copy(bset
);
3475 ls
= isl_qpolynomial_get_domain_local_space(qp
);
3476 bset
= isl_local_space_lift_basic_set(ls
, bset
);
3478 return fn(bset
, poly
, user
);
3481 /* Return total degree in variables first (inclusive) up to last (exclusive).
3483 int isl_upoly_degree(__isl_keep
struct isl_upoly
*up
, int first
, int last
)
3487 struct isl_upoly_rec
*rec
;
3491 if (isl_upoly_is_zero(up
))
3493 if (isl_upoly_is_cst(up
) || up
->var
< first
)
3496 rec
= isl_upoly_as_rec(up
);
3500 for (i
= 0; i
< rec
->n
; ++i
) {
3503 if (isl_upoly_is_zero(rec
->p
[i
]))
3505 d
= isl_upoly_degree(rec
->p
[i
], first
, last
);
3515 /* Return total degree in set variables.
3517 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial
*poly
)
3525 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3526 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3527 return isl_upoly_degree(poly
->upoly
, ovar
, ovar
+ nvar
);
3530 __isl_give
struct isl_upoly
*isl_upoly_coeff(__isl_keep
struct isl_upoly
*up
,
3531 unsigned pos
, int deg
)
3534 struct isl_upoly_rec
*rec
;
3539 if (isl_upoly_is_cst(up
) || up
->var
< pos
) {
3541 return isl_upoly_copy(up
);
3543 return isl_upoly_zero(up
->ctx
);
3546 rec
= isl_upoly_as_rec(up
);
3550 if (up
->var
== pos
) {
3552 return isl_upoly_copy(rec
->p
[deg
]);
3554 return isl_upoly_zero(up
->ctx
);
3557 up
= isl_upoly_copy(up
);
3558 up
= isl_upoly_cow(up
);
3559 rec
= isl_upoly_as_rec(up
);
3563 for (i
= 0; i
< rec
->n
; ++i
) {
3564 struct isl_upoly
*t
;
3565 t
= isl_upoly_coeff(rec
->p
[i
], pos
, deg
);
3568 isl_upoly_free(rec
->p
[i
]);
3578 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3580 __isl_give isl_qpolynomial
*isl_qpolynomial_coeff(
3581 __isl_keep isl_qpolynomial
*qp
,
3582 enum isl_dim_type type
, unsigned t_pos
, int deg
)
3585 struct isl_upoly
*up
;
3591 if (type
== isl_dim_out
)
3592 isl_die(qp
->div
->ctx
, isl_error_invalid
,
3593 "output/set dimension does not have a coefficient",
3595 if (isl_qpolynomial_check_range(qp
, type
, t_pos
, 1) < 0)
3597 type
= domain_type(type
);
3599 g_pos
= pos(qp
->dim
, type
) + t_pos
;
3600 up
= isl_upoly_coeff(qp
->upoly
, g_pos
, deg
);
3602 c
= isl_qpolynomial_alloc(isl_space_copy(qp
->dim
), qp
->div
->n_row
, up
);
3605 isl_mat_free(c
->div
);
3606 c
->div
= isl_mat_copy(qp
->div
);
3611 isl_qpolynomial_free(c
);
3615 /* Homogenize the polynomial in the variables first (inclusive) up to
3616 * last (exclusive) by inserting powers of variable first.
3617 * Variable first is assumed not to appear in the input.
3619 __isl_give
struct isl_upoly
*isl_upoly_homogenize(
3620 __isl_take
struct isl_upoly
*up
, int deg
, int target
,
3621 int first
, int last
)
3624 struct isl_upoly_rec
*rec
;
3628 if (isl_upoly_is_zero(up
))
3632 if (isl_upoly_is_cst(up
) || up
->var
< first
) {
3633 struct isl_upoly
*hom
;
3635 hom
= isl_upoly_var_pow(up
->ctx
, first
, target
- deg
);
3638 rec
= isl_upoly_as_rec(hom
);
3639 rec
->p
[target
- deg
] = isl_upoly_mul(rec
->p
[target
- deg
], up
);
3644 up
= isl_upoly_cow(up
);
3645 rec
= isl_upoly_as_rec(up
);
3649 for (i
= 0; i
< rec
->n
; ++i
) {
3650 if (isl_upoly_is_zero(rec
->p
[i
]))
3652 rec
->p
[i
] = isl_upoly_homogenize(rec
->p
[i
],
3653 up
->var
< last
? deg
+ i
: i
, target
,
3665 /* Homogenize the polynomial in the set variables by introducing
3666 * powers of an extra set variable at position 0.
3668 __isl_give isl_qpolynomial
*isl_qpolynomial_homogenize(
3669 __isl_take isl_qpolynomial
*poly
)
3673 int deg
= isl_qpolynomial_degree(poly
);
3678 poly
= isl_qpolynomial_insert_dims(poly
, isl_dim_in
, 0, 1);
3679 poly
= isl_qpolynomial_cow(poly
);
3683 ovar
= isl_space_offset(poly
->dim
, isl_dim_set
);
3684 nvar
= isl_space_dim(poly
->dim
, isl_dim_set
);
3685 poly
->upoly
= isl_upoly_homogenize(poly
->upoly
, 0, deg
,
3692 isl_qpolynomial_free(poly
);
3696 __isl_give isl_term
*isl_term_alloc(__isl_take isl_space
*space
,
3697 __isl_take isl_mat
*div
)
3705 n
= isl_space_dim(space
, isl_dim_all
) + div
->n_row
;
3707 term
= isl_calloc(space
->ctx
, struct isl_term
,
3708 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
3715 isl_int_init(term
->n
);
3716 isl_int_init(term
->d
);
3720 isl_space_free(space
);
3725 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
3734 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
3743 total
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3745 dup
= isl_term_alloc(isl_space_copy(term
->dim
), isl_mat_copy(term
->div
));
3749 isl_int_set(dup
->n
, term
->n
);
3750 isl_int_set(dup
->d
, term
->d
);
3752 for (i
= 0; i
< total
; ++i
)
3753 dup
->pow
[i
] = term
->pow
[i
];
3758 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
3766 return isl_term_dup(term
);
3769 __isl_null isl_term
*isl_term_free(__isl_take isl_term
*term
)
3774 if (--term
->ref
> 0)
3777 isl_space_free(term
->dim
);
3778 isl_mat_free(term
->div
);
3779 isl_int_clear(term
->n
);
3780 isl_int_clear(term
->d
);
3786 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
3794 case isl_dim_out
: return isl_space_dim(term
->dim
, type
);
3795 case isl_dim_div
: return term
->div
->n_row
;
3796 case isl_dim_all
: return isl_space_dim(term
->dim
, isl_dim_all
) +
3802 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
3804 return term
? term
->dim
->ctx
: NULL
;
3807 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
3811 isl_int_set(*n
, term
->n
);
3814 /* Return the coefficient of the term "term".
3816 __isl_give isl_val
*isl_term_get_coefficient_val(__isl_keep isl_term
*term
)
3821 return isl_val_rat_from_isl_int(isl_term_get_ctx(term
),
3826 #define TYPE isl_term
3828 #include "check_type_range_templ.c"
3830 int isl_term_get_exp(__isl_keep isl_term
*term
,
3831 enum isl_dim_type type
, unsigned pos
)
3833 if (isl_term_check_range(term
, type
, pos
, 1) < 0)
3836 if (type
>= isl_dim_set
)
3837 pos
+= isl_space_dim(term
->dim
, isl_dim_param
);
3838 if (type
>= isl_dim_div
)
3839 pos
+= isl_space_dim(term
->dim
, isl_dim_set
);
3841 return term
->pow
[pos
];
3844 __isl_give isl_aff
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
3846 isl_local_space
*ls
;
3849 if (isl_term_check_range(term
, isl_dim_div
, pos
, 1) < 0)
3852 ls
= isl_local_space_alloc_div(isl_space_copy(term
->dim
),
3853 isl_mat_copy(term
->div
));
3854 aff
= isl_aff_alloc(ls
);
3858 isl_seq_cpy(aff
->v
->el
, term
->div
->row
[pos
], aff
->v
->size
);
3860 aff
= isl_aff_normalize(aff
);
3865 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
3866 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
),
3867 __isl_take isl_term
*term
, void *user
)
3870 struct isl_upoly_rec
*rec
;
3875 if (isl_upoly_is_zero(up
))
3878 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
3879 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
3880 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
3882 if (isl_upoly_is_cst(up
)) {
3883 struct isl_upoly_cst
*cst
;
3884 cst
= isl_upoly_as_cst(up
);
3887 term
= isl_term_cow(term
);
3890 isl_int_set(term
->n
, cst
->n
);
3891 isl_int_set(term
->d
, cst
->d
);
3892 if (fn(isl_term_copy(term
), user
) < 0)
3897 rec
= isl_upoly_as_rec(up
);
3901 for (i
= 0; i
< rec
->n
; ++i
) {
3902 term
= isl_term_cow(term
);
3905 term
->pow
[up
->var
] = i
;
3906 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
3910 term
->pow
[up
->var
] = 0;
3914 isl_term_free(term
);
3918 isl_stat
isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
3919 isl_stat (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
3924 return isl_stat_error
;
3926 term
= isl_term_alloc(isl_space_copy(qp
->dim
), isl_mat_copy(qp
->div
));
3928 return isl_stat_error
;
3930 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
3932 isl_term_free(term
);
3934 return term
? isl_stat_ok
: isl_stat_error
;
3937 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
3939 struct isl_upoly
*up
;
3940 isl_qpolynomial
*qp
;
3946 n
= isl_space_dim(term
->dim
, isl_dim_all
) + term
->div
->n_row
;
3948 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
3949 for (i
= 0; i
< n
; ++i
) {
3952 up
= isl_upoly_mul(up
,
3953 isl_upoly_var_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
3956 qp
= isl_qpolynomial_alloc(isl_space_copy(term
->dim
), term
->div
->n_row
, up
);
3959 isl_mat_free(qp
->div
);
3960 qp
->div
= isl_mat_copy(term
->div
);
3964 isl_term_free(term
);
3967 isl_qpolynomial_free(qp
);
3968 isl_term_free(term
);
3972 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
3973 __isl_take isl_space
*space
)
3982 if (isl_space_is_equal(qp
->dim
, space
)) {
3983 isl_space_free(space
);
3987 qp
= isl_qpolynomial_cow(qp
);
3991 extra
= isl_space_dim(space
, isl_dim_set
) -
3992 isl_space_dim(qp
->dim
, isl_dim_set
);
3993 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
3994 if (qp
->div
->n_row
) {
3997 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
4000 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4002 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
4007 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
4010 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4011 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
4013 isl_space_free(qp
->dim
);
4018 isl_space_free(space
);
4019 isl_qpolynomial_free(qp
);
4023 /* For each parameter or variable that does not appear in qp,
4024 * first eliminate the variable from all constraints and then set it to zero.
4026 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
4027 __isl_keep isl_qpolynomial
*qp
)
4038 d
= isl_space_dim(set
->dim
, isl_dim_all
);
4039 active
= isl_calloc_array(set
->ctx
, int, d
);
4040 if (set_active(qp
, active
) < 0)
4043 for (i
= 0; i
< d
; ++i
)
4052 nparam
= isl_space_dim(set
->dim
, isl_dim_param
);
4053 nvar
= isl_space_dim(set
->dim
, isl_dim_set
);
4054 for (i
= 0; i
< nparam
; ++i
) {
4057 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
4058 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
4060 for (i
= 0; i
< nvar
; ++i
) {
4061 if (active
[nparam
+ i
])
4063 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
4064 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
4076 struct isl_opt_data
{
4077 isl_qpolynomial
*qp
;
4083 static isl_stat
opt_fn(__isl_take isl_point
*pnt
, void *user
)
4085 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
4088 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
4092 } else if (data
->max
) {
4093 data
->opt
= isl_val_max(data
->opt
, val
);
4095 data
->opt
= isl_val_min(data
->opt
, val
);
4101 __isl_give isl_val
*isl_qpolynomial_opt_on_domain(
4102 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
4104 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
4109 if (isl_upoly_is_cst(qp
->upoly
)) {
4111 data
.opt
= isl_qpolynomial_get_constant_val(qp
);
4112 isl_qpolynomial_free(qp
);
4116 set
= fix_inactive(set
, qp
);
4119 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
4123 data
.opt
= isl_val_zero(isl_set_get_ctx(set
));
4126 isl_qpolynomial_free(qp
);
4130 isl_qpolynomial_free(qp
);
4131 isl_val_free(data
.opt
);
4135 __isl_give isl_qpolynomial
*isl_qpolynomial_morph_domain(
4136 __isl_take isl_qpolynomial
*qp
, __isl_take isl_morph
*morph
)
4141 struct isl_upoly
**subs
;
4142 isl_mat
*mat
, *diag
;
4144 qp
= isl_qpolynomial_cow(qp
);
4149 isl_assert(ctx
, isl_space_is_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
4151 n_sub
= morph
->inv
->n_row
- 1;
4152 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4153 n_sub
+= qp
->div
->n_row
;
4154 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, n_sub
);
4158 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
4159 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
4160 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
4161 if (morph
->inv
->n_row
!= morph
->inv
->n_col
)
4162 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
4163 subs
[morph
->inv
->n_row
- 1 + i
] =
4164 isl_upoly_var_pow(ctx
, morph
->inv
->n_col
- 1 + i
, 1);
4166 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, n_sub
, subs
);
4168 for (i
= 0; i
< n_sub
; ++i
)
4169 isl_upoly_free(subs
[i
]);
4172 diag
= isl_mat_diag(ctx
, 1, morph
->inv
->row
[0][0]);
4173 mat
= isl_mat_diagonal(diag
, isl_mat_copy(morph
->inv
));
4174 diag
= isl_mat_diag(ctx
, qp
->div
->n_row
, morph
->inv
->row
[0][0]);
4175 mat
= isl_mat_diagonal(mat
, diag
);
4176 qp
->div
= isl_mat_product(qp
->div
, mat
);
4177 isl_space_free(qp
->dim
);
4178 qp
->dim
= isl_space_copy(morph
->ran
->dim
);
4180 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
4183 isl_morph_free(morph
);
4187 isl_qpolynomial_free(qp
);
4188 isl_morph_free(morph
);
4192 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_mul(
4193 __isl_take isl_union_pw_qpolynomial
*upwqp1
,
4194 __isl_take isl_union_pw_qpolynomial
*upwqp2
)
4196 return isl_union_pw_qpolynomial_match_bin_op(upwqp1
, upwqp2
,
4197 &isl_pw_qpolynomial_mul
);
4200 /* Reorder the dimension of "qp" according to the given reordering.
4202 __isl_give isl_qpolynomial
*isl_qpolynomial_realign_domain(
4203 __isl_take isl_qpolynomial
*qp
, __isl_take isl_reordering
*r
)
4207 qp
= isl_qpolynomial_cow(qp
);
4211 r
= isl_reordering_extend(r
, qp
->div
->n_row
);
4215 qp
->div
= isl_local_reorder(qp
->div
, isl_reordering_copy(r
));
4219 qp
->upoly
= reorder(qp
->upoly
, r
->pos
);
4223 space
= isl_reordering_get_space(r
);
4224 qp
= isl_qpolynomial_reset_domain_space(qp
, space
);
4226 isl_reordering_free(r
);
4229 isl_qpolynomial_free(qp
);
4230 isl_reordering_free(r
);
4234 __isl_give isl_qpolynomial
*isl_qpolynomial_align_params(
4235 __isl_take isl_qpolynomial
*qp
, __isl_take isl_space
*model
)
4237 isl_bool equal_params
;
4242 equal_params
= isl_space_has_equal_params(qp
->dim
, model
);
4243 if (equal_params
< 0)
4245 if (!equal_params
) {
4246 isl_reordering
*exp
;
4248 exp
= isl_parameter_alignment_reordering(qp
->dim
, model
);
4249 exp
= isl_reordering_extend_space(exp
,
4250 isl_qpolynomial_get_domain_space(qp
));
4251 qp
= isl_qpolynomial_realign_domain(qp
, exp
);
4254 isl_space_free(model
);
4257 isl_space_free(model
);
4258 isl_qpolynomial_free(qp
);
4262 struct isl_split_periods_data
{
4264 isl_pw_qpolynomial
*res
;
4267 /* Create a slice where the integer division "div" has the fixed value "v".
4268 * In particular, if "div" refers to floor(f/m), then create a slice
4270 * m v <= f <= m v + (m - 1)
4275 * -f + m v + (m - 1) >= 0
4277 static __isl_give isl_set
*set_div_slice(__isl_take isl_space
*space
,
4278 __isl_keep isl_qpolynomial
*qp
, int div
, isl_int v
)
4281 isl_basic_set
*bset
= NULL
;
4287 total
= isl_space_dim(space
, isl_dim_all
);
4288 bset
= isl_basic_set_alloc_space(isl_space_copy(space
), 0, 0, 2);
4290 k
= isl_basic_set_alloc_inequality(bset
);
4293 isl_seq_cpy(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4294 isl_int_submul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4296 k
= isl_basic_set_alloc_inequality(bset
);
4299 isl_seq_neg(bset
->ineq
[k
], qp
->div
->row
[div
] + 1, 1 + total
);
4300 isl_int_addmul(bset
->ineq
[k
][0], v
, qp
->div
->row
[div
][0]);
4301 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], qp
->div
->row
[div
][0]);
4302 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
4304 isl_space_free(space
);
4305 return isl_set_from_basic_set(bset
);
4307 isl_basic_set_free(bset
);
4308 isl_space_free(space
);
4312 static isl_stat
split_periods(__isl_take isl_set
*set
,
4313 __isl_take isl_qpolynomial
*qp
, void *user
);
4315 /* Create a slice of the domain "set" such that integer division "div"
4316 * has the fixed value "v" and add the results to data->res,
4317 * replacing the integer division by "v" in "qp".
4319 static isl_stat
set_div(__isl_take isl_set
*set
,
4320 __isl_take isl_qpolynomial
*qp
, int div
, isl_int v
,
4321 struct isl_split_periods_data
*data
)
4326 struct isl_upoly
*cst
;
4328 slice
= set_div_slice(isl_set_get_space(set
), qp
, div
, v
);
4329 set
= isl_set_intersect(set
, slice
);
4334 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4336 for (i
= div
+ 1; i
< qp
->div
->n_row
; ++i
) {
4337 if (isl_int_is_zero(qp
->div
->row
[i
][2 + total
+ div
]))
4339 isl_int_addmul(qp
->div
->row
[i
][1],
4340 qp
->div
->row
[i
][2 + total
+ div
], v
);
4341 isl_int_set_si(qp
->div
->row
[i
][2 + total
+ div
], 0);
4344 cst
= isl_upoly_rat_cst(qp
->dim
->ctx
, v
, qp
->dim
->ctx
->one
);
4345 qp
= substitute_div(qp
, div
, cst
);
4347 return split_periods(set
, qp
, data
);
4350 isl_qpolynomial_free(qp
);
4351 return isl_stat_error
;
4354 /* Split the domain "set" such that integer division "div"
4355 * has a fixed value (ranging from "min" to "max") on each slice
4356 * and add the results to data->res.
4358 static isl_stat
split_div(__isl_take isl_set
*set
,
4359 __isl_take isl_qpolynomial
*qp
, int div
, isl_int min
, isl_int max
,
4360 struct isl_split_periods_data
*data
)
4362 for (; isl_int_le(min
, max
); isl_int_add_ui(min
, min
, 1)) {
4363 isl_set
*set_i
= isl_set_copy(set
);
4364 isl_qpolynomial
*qp_i
= isl_qpolynomial_copy(qp
);
4366 if (set_div(set_i
, qp_i
, div
, min
, data
) < 0)
4370 isl_qpolynomial_free(qp
);
4374 isl_qpolynomial_free(qp
);
4375 return isl_stat_error
;
4378 /* If "qp" refers to any integer division
4379 * that can only attain "max_periods" distinct values on "set"
4380 * then split the domain along those distinct values.
4381 * Add the results (or the original if no splitting occurs)
4384 static isl_stat
split_periods(__isl_take isl_set
*set
,
4385 __isl_take isl_qpolynomial
*qp
, void *user
)
4388 isl_pw_qpolynomial
*pwqp
;
4389 struct isl_split_periods_data
*data
;
4392 isl_stat r
= isl_stat_ok
;
4394 data
= (struct isl_split_periods_data
*)user
;
4399 if (qp
->div
->n_row
== 0) {
4400 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4401 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4407 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4408 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4409 enum isl_lp_result lp_res
;
4411 if (isl_seq_first_non_zero(qp
->div
->row
[i
] + 2 + total
,
4412 qp
->div
->n_row
) != -1)
4415 lp_res
= isl_set_solve_lp(set
, 0, qp
->div
->row
[i
] + 1,
4416 set
->ctx
->one
, &min
, NULL
, NULL
);
4417 if (lp_res
== isl_lp_error
)
4419 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4421 isl_int_fdiv_q(min
, min
, qp
->div
->row
[i
][0]);
4423 lp_res
= isl_set_solve_lp(set
, 1, qp
->div
->row
[i
] + 1,
4424 set
->ctx
->one
, &max
, NULL
, NULL
);
4425 if (lp_res
== isl_lp_error
)
4427 if (lp_res
== isl_lp_unbounded
|| lp_res
== isl_lp_empty
)
4429 isl_int_fdiv_q(max
, max
, qp
->div
->row
[i
][0]);
4431 isl_int_sub(max
, max
, min
);
4432 if (isl_int_cmp_si(max
, data
->max_periods
) < 0) {
4433 isl_int_add(max
, max
, min
);
4438 if (i
< qp
->div
->n_row
) {
4439 r
= split_div(set
, qp
, i
, min
, max
, data
);
4441 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4442 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, pwqp
);
4454 isl_qpolynomial_free(qp
);
4455 return isl_stat_error
;
4458 /* If any quasi-polynomial in pwqp refers to any integer division
4459 * that can only attain "max_periods" distinct values on its domain
4460 * then split the domain along those distinct values.
4462 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_split_periods(
4463 __isl_take isl_pw_qpolynomial
*pwqp
, int max_periods
)
4465 struct isl_split_periods_data data
;
4467 data
.max_periods
= max_periods
;
4468 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4470 if (isl_pw_qpolynomial_foreach_piece(pwqp
, &split_periods
, &data
) < 0)
4473 isl_pw_qpolynomial_free(pwqp
);
4477 isl_pw_qpolynomial_free(data
.res
);
4478 isl_pw_qpolynomial_free(pwqp
);
4482 /* Construct a piecewise quasipolynomial that is constant on the given
4483 * domain. In particular, it is
4486 * infinity if cst == -1
4488 * If cst == -1, then explicitly check whether the domain is empty and,
4489 * if so, return 0 instead.
4491 static __isl_give isl_pw_qpolynomial
*constant_on_domain(
4492 __isl_take isl_basic_set
*bset
, int cst
)
4495 isl_qpolynomial
*qp
;
4497 if (cst
< 0 && isl_basic_set_is_empty(bset
) == isl_bool_true
)
4502 bset
= isl_basic_set_params(bset
);
4503 dim
= isl_basic_set_get_space(bset
);
4505 qp
= isl_qpolynomial_infty_on_domain(dim
);
4507 qp
= isl_qpolynomial_zero_on_domain(dim
);
4509 qp
= isl_qpolynomial_one_on_domain(dim
);
4510 return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset
), qp
);
4513 /* Factor bset, call fn on each of the factors and return the product.
4515 * If no factors can be found, simply call fn on the input.
4516 * Otherwise, construct the factors based on the factorizer,
4517 * call fn on each factor and compute the product.
4519 static __isl_give isl_pw_qpolynomial
*compressed_multiplicative_call(
4520 __isl_take isl_basic_set
*bset
,
4521 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4527 isl_qpolynomial
*qp
;
4528 isl_pw_qpolynomial
*pwqp
;
4532 f
= isl_basic_set_factorizer(bset
);
4535 if (f
->n_group
== 0) {
4536 isl_factorizer_free(f
);
4540 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
4541 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
4543 space
= isl_basic_set_get_space(bset
);
4544 space
= isl_space_params(space
);
4545 set
= isl_set_universe(isl_space_copy(space
));
4546 qp
= isl_qpolynomial_one_on_domain(space
);
4547 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
4549 bset
= isl_morph_basic_set(isl_morph_copy(f
->morph
), bset
);
4551 for (i
= 0, n
= 0; i
< f
->n_group
; ++i
) {
4552 isl_basic_set
*bset_i
;
4553 isl_pw_qpolynomial
*pwqp_i
;
4555 bset_i
= isl_basic_set_copy(bset
);
4556 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4557 nparam
+ n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4558 bset_i
= isl_basic_set_drop_constraints_involving(bset_i
,
4560 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
,
4561 n
+ f
->len
[i
], nvar
- n
- f
->len
[i
]);
4562 bset_i
= isl_basic_set_drop(bset_i
, isl_dim_set
, 0, n
);
4564 pwqp_i
= fn(bset_i
);
4565 pwqp
= isl_pw_qpolynomial_mul(pwqp
, pwqp_i
);
4570 isl_basic_set_free(bset
);
4571 isl_factorizer_free(f
);
4575 isl_basic_set_free(bset
);
4579 /* Factor bset, call fn on each of the factors and return the product.
4580 * The function is assumed to evaluate to zero on empty domains,
4581 * to one on zero-dimensional domains and to infinity on unbounded domains
4582 * and will not be called explicitly on zero-dimensional or unbounded domains.
4584 * We first check for some special cases and remove all equalities.
4585 * Then we hand over control to compressed_multiplicative_call.
4587 __isl_give isl_pw_qpolynomial
*isl_basic_set_multiplicative_call(
4588 __isl_take isl_basic_set
*bset
,
4589 __isl_give isl_pw_qpolynomial
*(*fn
)(__isl_take isl_basic_set
*bset
))
4593 isl_pw_qpolynomial
*pwqp
;
4598 if (isl_basic_set_plain_is_empty(bset
))
4599 return constant_on_domain(bset
, 0);
4601 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
4602 return constant_on_domain(bset
, 1);
4604 bounded
= isl_basic_set_is_bounded(bset
);
4608 return constant_on_domain(bset
, -1);
4610 if (bset
->n_eq
== 0)
4611 return compressed_multiplicative_call(bset
, fn
);
4613 morph
= isl_basic_set_full_compression(bset
);
4614 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
4616 pwqp
= compressed_multiplicative_call(bset
, fn
);
4618 morph
= isl_morph_dom_params(morph
);
4619 morph
= isl_morph_ran_params(morph
);
4620 morph
= isl_morph_inverse(morph
);
4622 pwqp
= isl_pw_qpolynomial_morph_domain(pwqp
, morph
);
4626 isl_basic_set_free(bset
);
4630 /* Drop all floors in "qp", turning each integer division [a/m] into
4631 * a rational division a/m. If "down" is set, then the integer division
4632 * is replaced by (a-(m-1))/m instead.
4634 static __isl_give isl_qpolynomial
*qp_drop_floors(
4635 __isl_take isl_qpolynomial
*qp
, int down
)
4638 struct isl_upoly
*s
;
4642 if (qp
->div
->n_row
== 0)
4645 qp
= isl_qpolynomial_cow(qp
);
4649 for (i
= qp
->div
->n_row
- 1; i
>= 0; --i
) {
4651 isl_int_sub(qp
->div
->row
[i
][1],
4652 qp
->div
->row
[i
][1], qp
->div
->row
[i
][0]);
4653 isl_int_add_ui(qp
->div
->row
[i
][1],
4654 qp
->div
->row
[i
][1], 1);
4656 s
= isl_upoly_from_affine(qp
->dim
->ctx
, qp
->div
->row
[i
] + 1,
4657 qp
->div
->row
[i
][0], qp
->div
->n_col
- 1);
4658 qp
= substitute_div(qp
, i
, s
);
4666 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4667 * a rational division a/m.
4669 static __isl_give isl_pw_qpolynomial
*pwqp_drop_floors(
4670 __isl_take isl_pw_qpolynomial
*pwqp
)
4677 if (isl_pw_qpolynomial_is_zero(pwqp
))
4680 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
4684 for (i
= 0; i
< pwqp
->n
; ++i
) {
4685 pwqp
->p
[i
].qp
= qp_drop_floors(pwqp
->p
[i
].qp
, 0);
4692 isl_pw_qpolynomial_free(pwqp
);
4696 /* Adjust all the integer divisions in "qp" such that they are at least
4697 * one over the given orthant (identified by "signs"). This ensures
4698 * that they will still be non-negative even after subtracting (m-1)/m.
4700 * In particular, f is replaced by f' + v, changing f = [a/m]
4701 * to f' = [(a - m v)/m].
4702 * If the constant term k in a is smaller than m,
4703 * the constant term of v is set to floor(k/m) - 1.
4704 * For any other term, if the coefficient c and the variable x have
4705 * the same sign, then no changes are needed.
4706 * Otherwise, if the variable is positive (and c is negative),
4707 * then the coefficient of x in v is set to floor(c/m).
4708 * If the variable is negative (and c is positive),
4709 * then the coefficient of x in v is set to ceil(c/m).
4711 static __isl_give isl_qpolynomial
*make_divs_pos(__isl_take isl_qpolynomial
*qp
,
4717 struct isl_upoly
*s
;
4719 qp
= isl_qpolynomial_cow(qp
);
4722 qp
->div
= isl_mat_cow(qp
->div
);
4726 total
= isl_space_dim(qp
->dim
, isl_dim_all
);
4727 v
= isl_vec_alloc(qp
->div
->ctx
, qp
->div
->n_col
- 1);
4729 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
4730 isl_int
*row
= qp
->div
->row
[i
];
4734 if (isl_int_lt(row
[1], row
[0])) {
4735 isl_int_fdiv_q(v
->el
[0], row
[1], row
[0]);
4736 isl_int_sub_ui(v
->el
[0], v
->el
[0], 1);
4737 isl_int_submul(row
[1], row
[0], v
->el
[0]);
4739 for (j
= 0; j
< total
; ++j
) {
4740 if (isl_int_sgn(row
[2 + j
]) * signs
[j
] >= 0)
4743 isl_int_cdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4745 isl_int_fdiv_q(v
->el
[1 + j
], row
[2 + j
], row
[0]);
4746 isl_int_submul(row
[2 + j
], row
[0], v
->el
[1 + j
]);
4748 for (j
= 0; j
< i
; ++j
) {
4749 if (isl_int_sgn(row
[2 + total
+ j
]) >= 0)
4751 isl_int_fdiv_q(v
->el
[1 + total
+ j
],
4752 row
[2 + total
+ j
], row
[0]);
4753 isl_int_submul(row
[2 + total
+ j
],
4754 row
[0], v
->el
[1 + total
+ j
]);
4756 for (j
= i
+ 1; j
< qp
->div
->n_row
; ++j
) {
4757 if (isl_int_is_zero(qp
->div
->row
[j
][2 + total
+ i
]))
4759 isl_seq_combine(qp
->div
->row
[j
] + 1,
4760 qp
->div
->ctx
->one
, qp
->div
->row
[j
] + 1,
4761 qp
->div
->row
[j
][2 + total
+ i
], v
->el
, v
->size
);
4763 isl_int_set_si(v
->el
[1 + total
+ i
], 1);
4764 s
= isl_upoly_from_affine(qp
->dim
->ctx
, v
->el
,
4765 qp
->div
->ctx
->one
, v
->size
);
4766 qp
->upoly
= isl_upoly_subs(qp
->upoly
, total
+ i
, 1, &s
);
4776 isl_qpolynomial_free(qp
);
4780 struct isl_to_poly_data
{
4782 isl_pw_qpolynomial
*res
;
4783 isl_qpolynomial
*qp
;
4786 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4787 * We first make all integer divisions positive and then split the
4788 * quasipolynomials into terms with sign data->sign (the direction
4789 * of the requested approximation) and terms with the opposite sign.
4790 * In the first set of terms, each integer division [a/m] is
4791 * overapproximated by a/m, while in the second it is underapproximated
4794 static isl_stat
to_polynomial_on_orthant(__isl_take isl_set
*orthant
,
4795 int *signs
, void *user
)
4797 struct isl_to_poly_data
*data
= user
;
4798 isl_pw_qpolynomial
*t
;
4799 isl_qpolynomial
*qp
, *up
, *down
;
4801 qp
= isl_qpolynomial_copy(data
->qp
);
4802 qp
= make_divs_pos(qp
, signs
);
4804 up
= isl_qpolynomial_terms_of_sign(qp
, signs
, data
->sign
);
4805 up
= qp_drop_floors(up
, 0);
4806 down
= isl_qpolynomial_terms_of_sign(qp
, signs
, -data
->sign
);
4807 down
= qp_drop_floors(down
, 1);
4809 isl_qpolynomial_free(qp
);
4810 qp
= isl_qpolynomial_add(up
, down
);
4812 t
= isl_pw_qpolynomial_alloc(orthant
, qp
);
4813 data
->res
= isl_pw_qpolynomial_add_disjoint(data
->res
, t
);
4818 /* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
4819 * the polynomial will be an overapproximation. If "sign" is negative,
4820 * it will be an underapproximation. If "sign" is zero, the approximation
4821 * will lie somewhere in between.
4823 * In particular, is sign == 0, we simply drop the floors, turning
4824 * the integer divisions into rational divisions.
4825 * Otherwise, we split the domains into orthants, make all integer divisions
4826 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4827 * depending on the requested sign and the sign of the term in which
4828 * the integer division appears.
4830 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_to_polynomial(
4831 __isl_take isl_pw_qpolynomial
*pwqp
, int sign
)
4834 struct isl_to_poly_data data
;
4837 return pwqp_drop_floors(pwqp
);
4843 data
.res
= isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_space(pwqp
));
4845 for (i
= 0; i
< pwqp
->n
; ++i
) {
4846 if (pwqp
->p
[i
].qp
->div
->n_row
== 0) {
4847 isl_pw_qpolynomial
*t
;
4848 t
= isl_pw_qpolynomial_alloc(
4849 isl_set_copy(pwqp
->p
[i
].set
),
4850 isl_qpolynomial_copy(pwqp
->p
[i
].qp
));
4851 data
.res
= isl_pw_qpolynomial_add_disjoint(data
.res
, t
);
4854 data
.qp
= pwqp
->p
[i
].qp
;
4855 if (isl_set_foreach_orthant(pwqp
->p
[i
].set
,
4856 &to_polynomial_on_orthant
, &data
) < 0)
4860 isl_pw_qpolynomial_free(pwqp
);
4864 isl_pw_qpolynomial_free(pwqp
);
4865 isl_pw_qpolynomial_free(data
.res
);
4869 static __isl_give isl_pw_qpolynomial
*poly_entry(
4870 __isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
4874 return isl_pw_qpolynomial_to_polynomial(pwqp
, *sign
);
4877 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_to_polynomial(
4878 __isl_take isl_union_pw_qpolynomial
*upwqp
, int sign
)
4880 return isl_union_pw_qpolynomial_transform_inplace(upwqp
,
4881 &poly_entry
, &sign
);
4884 __isl_give isl_basic_map
*isl_basic_map_from_qpolynomial(
4885 __isl_take isl_qpolynomial
*qp
)
4889 isl_vec
*aff
= NULL
;
4890 isl_basic_map
*bmap
= NULL
;
4896 if (!isl_upoly_is_affine(qp
->upoly
))
4897 isl_die(qp
->dim
->ctx
, isl_error_invalid
,
4898 "input quasi-polynomial not affine", goto error
);
4899 aff
= isl_qpolynomial_extract_affine(qp
);
4902 dim
= isl_qpolynomial_get_space(qp
);
4903 pos
= 1 + isl_space_offset(dim
, isl_dim_out
);
4904 n_div
= qp
->div
->n_row
;
4905 bmap
= isl_basic_map_alloc_space(dim
, n_div
, 1, 2 * n_div
);
4907 for (i
= 0; i
< n_div
; ++i
) {
4908 k
= isl_basic_map_alloc_div(bmap
);
4911 isl_seq_cpy(bmap
->div
[k
], qp
->div
->row
[i
], qp
->div
->n_col
);
4912 isl_int_set_si(bmap
->div
[k
][qp
->div
->n_col
], 0);
4913 if (isl_basic_map_add_div_constraints(bmap
, k
) < 0)
4916 k
= isl_basic_map_alloc_equality(bmap
);
4919 isl_int_neg(bmap
->eq
[k
][pos
], aff
->el
[0]);
4920 isl_seq_cpy(bmap
->eq
[k
], aff
->el
+ 1, pos
);
4921 isl_seq_cpy(bmap
->eq
[k
] + pos
+ 1, aff
->el
+ 1 + pos
, n_div
);
4924 isl_qpolynomial_free(qp
);
4925 bmap
= isl_basic_map_finalize(bmap
);
4929 isl_qpolynomial_free(qp
);
4930 isl_basic_map_free(bmap
);