2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
16 #include <isl_map_private.h>
18 static unsigned pos(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
21 case isl_dim_param
: return 0;
22 case isl_dim_in
: return dim
->nparam
;
23 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
28 int isl_upoly_is_cst(__isl_keep
struct isl_upoly
*up
)
36 __isl_keep
struct isl_upoly_cst
*isl_upoly_as_cst(__isl_keep
struct isl_upoly
*up
)
41 isl_assert(up
->ctx
, up
->var
< 0, return NULL
);
43 return (struct isl_upoly_cst
*)up
;
46 __isl_keep
struct isl_upoly_rec
*isl_upoly_as_rec(__isl_keep
struct isl_upoly
*up
)
51 isl_assert(up
->ctx
, up
->var
>= 0, return NULL
);
53 return (struct isl_upoly_rec
*)up
;
56 int isl_upoly_is_equal(__isl_keep
struct isl_upoly
*up1
,
57 __isl_keep
struct isl_upoly
*up2
)
60 struct isl_upoly_rec
*rec1
, *rec2
;
66 if (up1
->var
!= up2
->var
)
68 if (isl_upoly_is_cst(up1
)) {
69 struct isl_upoly_cst
*cst1
, *cst2
;
70 cst1
= isl_upoly_as_cst(up1
);
71 cst2
= isl_upoly_as_cst(up2
);
74 return isl_int_eq(cst1
->n
, cst2
->n
) &&
75 isl_int_eq(cst1
->d
, cst2
->d
);
78 rec1
= isl_upoly_as_rec(up1
);
79 rec2
= isl_upoly_as_rec(up2
);
83 if (rec1
->n
!= rec2
->n
)
86 for (i
= 0; i
< rec1
->n
; ++i
) {
87 int eq
= isl_upoly_is_equal(rec1
->p
[i
], rec2
->p
[i
]);
95 int isl_upoly_is_zero(__isl_keep
struct isl_upoly
*up
)
97 struct isl_upoly_cst
*cst
;
101 if (!isl_upoly_is_cst(up
))
104 cst
= isl_upoly_as_cst(up
);
108 return isl_int_is_zero(cst
->n
) && isl_int_is_pos(cst
->d
);
111 int isl_upoly_sgn(__isl_keep
struct isl_upoly
*up
)
113 struct isl_upoly_cst
*cst
;
117 if (!isl_upoly_is_cst(up
))
120 cst
= isl_upoly_as_cst(up
);
124 return isl_int_sgn(cst
->n
);
127 int isl_upoly_is_nan(__isl_keep
struct isl_upoly
*up
)
129 struct isl_upoly_cst
*cst
;
133 if (!isl_upoly_is_cst(up
))
136 cst
= isl_upoly_as_cst(up
);
140 return isl_int_is_zero(cst
->n
) && isl_int_is_zero(cst
->d
);
143 int isl_upoly_is_infty(__isl_keep
struct isl_upoly
*up
)
145 struct isl_upoly_cst
*cst
;
149 if (!isl_upoly_is_cst(up
))
152 cst
= isl_upoly_as_cst(up
);
156 return isl_int_is_pos(cst
->n
) && isl_int_is_zero(cst
->d
);
159 int isl_upoly_is_neginfty(__isl_keep
struct isl_upoly
*up
)
161 struct isl_upoly_cst
*cst
;
165 if (!isl_upoly_is_cst(up
))
168 cst
= isl_upoly_as_cst(up
);
172 return isl_int_is_neg(cst
->n
) && isl_int_is_zero(cst
->d
);
175 int isl_upoly_is_one(__isl_keep
struct isl_upoly
*up
)
177 struct isl_upoly_cst
*cst
;
181 if (!isl_upoly_is_cst(up
))
184 cst
= isl_upoly_as_cst(up
);
188 return isl_int_eq(cst
->n
, cst
->d
) && isl_int_is_pos(cst
->d
);
191 int isl_upoly_is_negone(__isl_keep
struct isl_upoly
*up
)
193 struct isl_upoly_cst
*cst
;
197 if (!isl_upoly_is_cst(up
))
200 cst
= isl_upoly_as_cst(up
);
204 return isl_int_is_negone(cst
->n
) && isl_int_is_one(cst
->d
);
207 __isl_give
struct isl_upoly_cst
*isl_upoly_cst_alloc(struct isl_ctx
*ctx
)
209 struct isl_upoly_cst
*cst
;
211 cst
= isl_alloc_type(ctx
, struct isl_upoly_cst
);
220 isl_int_init(cst
->n
);
221 isl_int_init(cst
->d
);
226 __isl_give
struct isl_upoly
*isl_upoly_zero(struct isl_ctx
*ctx
)
228 struct isl_upoly_cst
*cst
;
230 cst
= isl_upoly_cst_alloc(ctx
);
234 isl_int_set_si(cst
->n
, 0);
235 isl_int_set_si(cst
->d
, 1);
240 __isl_give
struct isl_upoly
*isl_upoly_infty(struct isl_ctx
*ctx
)
242 struct isl_upoly_cst
*cst
;
244 cst
= isl_upoly_cst_alloc(ctx
);
248 isl_int_set_si(cst
->n
, 1);
249 isl_int_set_si(cst
->d
, 0);
254 __isl_give
struct isl_upoly
*isl_upoly_neginfty(struct isl_ctx
*ctx
)
256 struct isl_upoly_cst
*cst
;
258 cst
= isl_upoly_cst_alloc(ctx
);
262 isl_int_set_si(cst
->n
, -1);
263 isl_int_set_si(cst
->d
, 0);
268 __isl_give
struct isl_upoly
*isl_upoly_nan(struct isl_ctx
*ctx
)
270 struct isl_upoly_cst
*cst
;
272 cst
= isl_upoly_cst_alloc(ctx
);
276 isl_int_set_si(cst
->n
, 0);
277 isl_int_set_si(cst
->d
, 0);
282 __isl_give
struct isl_upoly
*isl_upoly_rat_cst(struct isl_ctx
*ctx
,
283 isl_int n
, isl_int d
)
285 struct isl_upoly_cst
*cst
;
287 cst
= isl_upoly_cst_alloc(ctx
);
291 isl_int_set(cst
->n
, n
);
292 isl_int_set(cst
->d
, d
);
297 __isl_give
struct isl_upoly_rec
*isl_upoly_alloc_rec(struct isl_ctx
*ctx
,
300 struct isl_upoly_rec
*rec
;
302 isl_assert(ctx
, var
>= 0, return NULL
);
303 isl_assert(ctx
, size
>= 0, return NULL
);
304 rec
= isl_calloc(dim
->ctx
, struct isl_upoly_rec
,
305 sizeof(struct isl_upoly_rec
) +
306 (size
- 1) * sizeof(struct isl_upoly
*));
321 isl_ctx
*isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial
*qp
)
323 return qp
? qp
->dim
->ctx
: NULL
;
326 __isl_give isl_dim
*isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial
*qp
)
328 return qp
? isl_dim_copy(qp
->dim
) : NULL
;
331 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial
*qp
)
333 return qp
? isl_upoly_is_zero(qp
->upoly
) : -1;
336 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial
*qp
)
338 return qp
? isl_upoly_is_one(qp
->upoly
) : -1;
341 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial
*qp
)
343 return qp
? isl_upoly_is_nan(qp
->upoly
) : -1;
346 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial
*qp
)
348 return qp
? isl_upoly_is_infty(qp
->upoly
) : -1;
351 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial
*qp
)
353 return qp
? isl_upoly_is_neginfty(qp
->upoly
) : -1;
356 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial
*qp
)
358 return qp
? isl_upoly_sgn(qp
->upoly
) : 0;
361 static void upoly_free_cst(__isl_take
struct isl_upoly_cst
*cst
)
363 isl_int_clear(cst
->n
);
364 isl_int_clear(cst
->d
);
367 static void upoly_free_rec(__isl_take
struct isl_upoly_rec
*rec
)
371 for (i
= 0; i
< rec
->n
; ++i
)
372 isl_upoly_free(rec
->p
[i
]);
375 __isl_give
struct isl_upoly
*isl_upoly_copy(__isl_keep
struct isl_upoly
*up
)
384 __isl_give
struct isl_upoly
*isl_upoly_dup_cst(__isl_keep
struct isl_upoly
*up
)
386 struct isl_upoly_cst
*cst
;
387 struct isl_upoly_cst
*dup
;
389 cst
= isl_upoly_as_cst(up
);
393 dup
= isl_upoly_as_cst(isl_upoly_zero(up
->ctx
));
396 isl_int_set(dup
->n
, cst
->n
);
397 isl_int_set(dup
->d
, cst
->d
);
402 __isl_give
struct isl_upoly
*isl_upoly_dup_rec(__isl_keep
struct isl_upoly
*up
)
405 struct isl_upoly_rec
*rec
;
406 struct isl_upoly_rec
*dup
;
408 rec
= isl_upoly_as_rec(up
);
412 dup
= isl_upoly_alloc_rec(up
->ctx
, up
->var
, rec
->n
);
416 for (i
= 0; i
< rec
->n
; ++i
) {
417 dup
->p
[i
] = isl_upoly_copy(rec
->p
[i
]);
425 isl_upoly_free(&dup
->up
);
429 __isl_give
struct isl_upoly
*isl_upoly_dup(__isl_keep
struct isl_upoly
*up
)
431 struct isl_upoly
*dup
;
436 if (isl_upoly_is_cst(up
))
437 return isl_upoly_dup_cst(up
);
439 return isl_upoly_dup_rec(up
);
442 __isl_give
struct isl_upoly
*isl_upoly_cow(__isl_take
struct isl_upoly
*up
)
450 return isl_upoly_dup(up
);
453 void isl_upoly_free(__isl_take
struct isl_upoly
*up
)
462 upoly_free_cst((struct isl_upoly_cst
*)up
);
464 upoly_free_rec((struct isl_upoly_rec
*)up
);
466 isl_ctx_deref(up
->ctx
);
470 static void isl_upoly_cst_reduce(__isl_keep
struct isl_upoly_cst
*cst
)
475 isl_int_gcd(gcd
, cst
->n
, cst
->d
);
476 if (!isl_int_is_zero(gcd
) && !isl_int_is_one(gcd
)) {
477 isl_int_divexact(cst
->n
, cst
->n
, gcd
);
478 isl_int_divexact(cst
->d
, cst
->d
, gcd
);
483 __isl_give
struct isl_upoly
*isl_upoly_sum_cst(__isl_take
struct isl_upoly
*up1
,
484 __isl_take
struct isl_upoly
*up2
)
486 struct isl_upoly_cst
*cst1
;
487 struct isl_upoly_cst
*cst2
;
489 up1
= isl_upoly_cow(up1
);
493 cst1
= isl_upoly_as_cst(up1
);
494 cst2
= isl_upoly_as_cst(up2
);
496 if (isl_int_eq(cst1
->d
, cst2
->d
))
497 isl_int_add(cst1
->n
, cst1
->n
, cst2
->n
);
499 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->d
);
500 isl_int_addmul(cst1
->n
, cst2
->n
, cst1
->d
);
501 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
504 isl_upoly_cst_reduce(cst1
);
514 static __isl_give
struct isl_upoly
*replace_by_zero(
515 __isl_take
struct isl_upoly
*up
)
523 return isl_upoly_zero(ctx
);
526 static __isl_give
struct isl_upoly
*replace_by_constant_term(
527 __isl_take
struct isl_upoly
*up
)
529 struct isl_upoly_rec
*rec
;
530 struct isl_upoly
*cst
;
535 rec
= isl_upoly_as_rec(up
);
538 cst
= isl_upoly_copy(rec
->p
[0]);
546 __isl_give
struct isl_upoly
*isl_upoly_sum(__isl_take
struct isl_upoly
*up1
,
547 __isl_take
struct isl_upoly
*up2
)
550 struct isl_upoly_rec
*rec1
, *rec2
;
555 if (isl_upoly_is_nan(up1
)) {
560 if (isl_upoly_is_nan(up2
)) {
565 if (isl_upoly_is_zero(up1
)) {
570 if (isl_upoly_is_zero(up2
)) {
575 if (up1
->var
< up2
->var
)
576 return isl_upoly_sum(up2
, up1
);
578 if (up2
->var
< up1
->var
) {
579 struct isl_upoly_rec
*rec
;
580 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
584 up1
= isl_upoly_cow(up1
);
585 rec
= isl_upoly_as_rec(up1
);
588 rec
->p
[0] = isl_upoly_sum(rec
->p
[0], up2
);
590 up1
= replace_by_constant_term(up1
);
594 if (isl_upoly_is_cst(up1
))
595 return isl_upoly_sum_cst(up1
, up2
);
597 rec1
= isl_upoly_as_rec(up1
);
598 rec2
= isl_upoly_as_rec(up2
);
602 if (rec1
->n
< rec2
->n
)
603 return isl_upoly_sum(up2
, up1
);
605 up1
= isl_upoly_cow(up1
);
606 rec1
= isl_upoly_as_rec(up1
);
610 for (i
= rec2
->n
- 1; i
>= 0; --i
) {
611 rec1
->p
[i
] = isl_upoly_sum(rec1
->p
[i
],
612 isl_upoly_copy(rec2
->p
[i
]));
615 if (i
== rec1
->n
- 1 && isl_upoly_is_zero(rec1
->p
[i
])) {
616 isl_upoly_free(rec1
->p
[i
]);
622 up1
= replace_by_zero(up1
);
623 else if (rec1
->n
== 1)
624 up1
= replace_by_constant_term(up1
);
635 __isl_give
struct isl_upoly
*isl_upoly_neg_cst(__isl_take
struct isl_upoly
*up
)
637 struct isl_upoly_cst
*cst
;
639 if (isl_upoly_is_zero(up
))
642 up
= isl_upoly_cow(up
);
646 cst
= isl_upoly_as_cst(up
);
648 isl_int_neg(cst
->n
, cst
->n
);
653 __isl_give
struct isl_upoly
*isl_upoly_neg(__isl_take
struct isl_upoly
*up
)
656 struct isl_upoly_rec
*rec
;
661 if (isl_upoly_is_cst(up
))
662 return isl_upoly_neg_cst(up
);
664 up
= isl_upoly_cow(up
);
665 rec
= isl_upoly_as_rec(up
);
669 for (i
= 0; i
< rec
->n
; ++i
) {
670 rec
->p
[i
] = isl_upoly_neg(rec
->p
[i
]);
681 __isl_give
struct isl_upoly
*isl_upoly_mul_cst(__isl_take
struct isl_upoly
*up1
,
682 __isl_take
struct isl_upoly
*up2
)
684 struct isl_upoly_cst
*cst1
;
685 struct isl_upoly_cst
*cst2
;
687 up1
= isl_upoly_cow(up1
);
691 cst1
= isl_upoly_as_cst(up1
);
692 cst2
= isl_upoly_as_cst(up2
);
694 isl_int_mul(cst1
->n
, cst1
->n
, cst2
->n
);
695 isl_int_mul(cst1
->d
, cst1
->d
, cst2
->d
);
697 isl_upoly_cst_reduce(cst1
);
707 __isl_give
struct isl_upoly
*isl_upoly_mul_rec(__isl_take
struct isl_upoly
*up1
,
708 __isl_take
struct isl_upoly
*up2
)
710 struct isl_upoly_rec
*rec1
;
711 struct isl_upoly_rec
*rec2
;
712 struct isl_upoly_rec
*res
;
716 rec1
= isl_upoly_as_rec(up1
);
717 rec2
= isl_upoly_as_rec(up2
);
720 size
= rec1
->n
+ rec2
->n
- 1;
721 res
= isl_upoly_alloc_rec(up1
->ctx
, up1
->var
, size
);
725 for (i
= 0; i
< rec1
->n
; ++i
) {
726 res
->p
[i
] = isl_upoly_mul(isl_upoly_copy(rec2
->p
[0]),
727 isl_upoly_copy(rec1
->p
[i
]));
732 for (; i
< size
; ++i
) {
733 res
->p
[i
] = isl_upoly_zero(up1
->ctx
);
738 for (i
= 0; i
< rec1
->n
; ++i
) {
739 for (j
= 1; j
< rec2
->n
; ++j
) {
740 struct isl_upoly
*up
;
741 up
= isl_upoly_mul(isl_upoly_copy(rec2
->p
[j
]),
742 isl_upoly_copy(rec1
->p
[i
]));
743 res
->p
[i
+ j
] = isl_upoly_sum(res
->p
[i
+ j
], up
);
756 isl_upoly_free(&res
->up
);
760 __isl_give
struct isl_upoly
*isl_upoly_mul(__isl_take
struct isl_upoly
*up1
,
761 __isl_take
struct isl_upoly
*up2
)
766 if (isl_upoly_is_nan(up1
)) {
771 if (isl_upoly_is_nan(up2
)) {
776 if (isl_upoly_is_zero(up1
)) {
781 if (isl_upoly_is_zero(up2
)) {
786 if (isl_upoly_is_one(up1
)) {
791 if (isl_upoly_is_one(up2
)) {
796 if (up1
->var
< up2
->var
)
797 return isl_upoly_mul(up2
, up1
);
799 if (up2
->var
< up1
->var
) {
801 struct isl_upoly_rec
*rec
;
802 if (isl_upoly_is_infty(up2
) || isl_upoly_is_neginfty(up2
)) {
803 isl_ctx
*ctx
= up1
->ctx
;
806 return isl_upoly_nan(ctx
);
808 up1
= isl_upoly_cow(up1
);
809 rec
= isl_upoly_as_rec(up1
);
813 for (i
= 0; i
< rec
->n
; ++i
) {
814 rec
->p
[i
] = isl_upoly_mul(rec
->p
[i
],
815 isl_upoly_copy(up2
));
823 if (isl_upoly_is_cst(up1
))
824 return isl_upoly_mul_cst(up1
, up2
);
826 return isl_upoly_mul_rec(up1
, up2
);
833 __isl_give isl_qpolynomial
*isl_qpolynomial_alloc(__isl_take isl_dim
*dim
,
834 unsigned n_div
, __isl_take
struct isl_upoly
*up
)
836 struct isl_qpolynomial
*qp
= NULL
;
842 total
= isl_dim_total(dim
);
844 qp
= isl_calloc_type(dim
->ctx
, struct isl_qpolynomial
);
849 qp
->div
= isl_mat_alloc(dim
->ctx
, n_div
, 1 + 1 + total
+ n_div
);
860 isl_qpolynomial_free(qp
);
864 __isl_give isl_qpolynomial
*isl_qpolynomial_copy(__isl_keep isl_qpolynomial
*qp
)
873 __isl_give isl_qpolynomial
*isl_qpolynomial_dup(__isl_keep isl_qpolynomial
*qp
)
875 struct isl_qpolynomial
*dup
;
880 dup
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), qp
->div
->n_row
,
881 isl_upoly_copy(qp
->upoly
));
884 isl_mat_free(dup
->div
);
885 dup
->div
= isl_mat_copy(qp
->div
);
891 isl_qpolynomial_free(dup
);
895 __isl_give isl_qpolynomial
*isl_qpolynomial_cow(__isl_take isl_qpolynomial
*qp
)
903 return isl_qpolynomial_dup(qp
);
906 void isl_qpolynomial_free(__isl_take isl_qpolynomial
*qp
)
914 isl_dim_free(qp
->dim
);
915 isl_mat_free(qp
->div
);
916 isl_upoly_free(qp
->upoly
);
921 static int compatible_divs(__isl_keep isl_mat
*div1
, __isl_keep isl_mat
*div2
)
926 isl_assert(div1
->ctx
, div1
->n_row
>= div2
->n_row
&&
927 div1
->n_col
>= div2
->n_col
, return -1);
929 if (div1
->n_row
== div2
->n_row
)
930 return isl_mat_is_equal(div1
, div2
);
934 div1
->n_row
= div2
->n_row
;
935 div1
->n_col
= div2
->n_col
;
937 equal
= isl_mat_is_equal(div1
, div2
);
945 static void expand_row(__isl_keep isl_mat
*dst
, int d
,
946 __isl_keep isl_mat
*src
, int s
, int *exp
)
949 unsigned c
= src
->n_col
- src
->n_row
;
951 isl_seq_cpy(dst
->row
[d
], src
->row
[s
], c
);
952 isl_seq_clr(dst
->row
[d
] + c
, dst
->n_col
- c
);
954 for (i
= 0; i
< s
; ++i
)
955 isl_int_set(dst
->row
[d
][c
+ exp
[i
]], src
->row
[s
][c
+ i
]);
958 static int cmp_row(__isl_keep isl_mat
*div
, int i
, int j
)
962 li
= isl_seq_last_non_zero(div
->row
[i
], div
->n_col
);
963 lj
= isl_seq_last_non_zero(div
->row
[j
], div
->n_col
);
968 return isl_seq_cmp(div
->row
[i
], div
->row
[j
], div
->n_col
);
971 struct isl_div_sort_info
{
976 static int div_sort_cmp(const void *p1
, const void *p2
)
978 const struct isl_div_sort_info
*i1
, *i2
;
979 i1
= (const struct isl_div_sort_info
*) p1
;
980 i2
= (const struct isl_div_sort_info
*) p2
;
982 return cmp_row(i1
->div
, i1
->row
, i2
->row
);
985 static __isl_give isl_mat
*sort_divs(__isl_take isl_mat
*div
)
988 struct isl_div_sort_info
*array
= NULL
;
996 array
= isl_alloc_array(div
->ctx
, struct isl_div_sort_info
, div
->n_row
);
997 pos
= isl_alloc_array(div
->ctx
, int, div
->n_row
);
1001 for (i
= 0; i
< div
->n_row
; ++i
) {
1007 qsort(array
, div
->n_row
, sizeof(struct isl_div_sort_info
),
1010 for (i
= 0; i
< div
->n_row
; ++i
) {
1012 if (pos
[array
[i
].row
] == i
)
1014 div
= isl_mat_cow(div
);
1015 div
= isl_mat_swap_rows(div
, i
, pos
[array
[i
].row
]);
1016 t
= pos
[array
[i
].row
];
1017 pos
[array
[i
].row
] = pos
[i
];
1031 static __isl_give isl_mat
*merge_divs(__isl_keep isl_mat
*div1
,
1032 __isl_keep isl_mat
*div2
, int *exp1
, int *exp2
)
1035 isl_mat
*div
= NULL
;
1036 unsigned d
= div1
->n_col
- div1
->n_row
;
1038 div
= isl_mat_alloc(div1
->ctx
, 1 + div1
->n_row
+ div2
->n_row
,
1039 d
+ div1
->n_row
+ div2
->n_row
);
1043 for (i
= 0, j
= 0, k
= 0; i
< div1
->n_row
&& j
< div2
->n_row
; ++k
) {
1046 expand_row(div
, k
, div1
, i
, exp1
);
1047 expand_row(div
, k
+ 1, div2
, j
, exp2
);
1049 cmp
= cmp_row(div
, k
, k
+ 1);
1053 } else if (cmp
< 0) {
1057 isl_seq_cpy(div
->row
[k
], div
->row
[k
+ 1], div
->n_col
);
1060 for (; i
< div1
->n_row
; ++i
, ++k
) {
1061 expand_row(div
, k
, div1
, i
, exp1
);
1064 for (; j
< div2
->n_row
; ++j
, ++k
) {
1065 expand_row(div
, k
, div2
, j
, exp2
);
1075 static __isl_give
struct isl_upoly
*expand(__isl_take
struct isl_upoly
*up
,
1076 int *exp
, int first
)
1079 struct isl_upoly_rec
*rec
;
1081 if (isl_upoly_is_cst(up
))
1084 if (up
->var
< first
)
1087 if (exp
[up
->var
- first
] == up
->var
- first
)
1090 up
= isl_upoly_cow(up
);
1094 up
->var
= exp
[up
->var
- first
] + first
;
1096 rec
= isl_upoly_as_rec(up
);
1100 for (i
= 0; i
< rec
->n
; ++i
) {
1101 rec
->p
[i
] = expand(rec
->p
[i
], exp
, first
);
1112 static __isl_give isl_qpolynomial
*with_merged_divs(
1113 __isl_give isl_qpolynomial
*(*fn
)(__isl_take isl_qpolynomial
*qp1
,
1114 __isl_take isl_qpolynomial
*qp2
),
1115 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1119 isl_mat
*div
= NULL
;
1121 qp1
= isl_qpolynomial_cow(qp1
);
1122 qp2
= isl_qpolynomial_cow(qp2
);
1127 isl_assert(qp1
->div
->ctx
, qp1
->div
->n_row
>= qp2
->div
->n_row
&&
1128 qp1
->div
->n_col
>= qp2
->div
->n_col
, goto error
);
1130 exp1
= isl_alloc_array(qp1
->div
->ctx
, int, qp1
->div
->n_row
);
1131 exp2
= isl_alloc_array(qp2
->div
->ctx
, int, qp2
->div
->n_row
);
1135 div
= merge_divs(qp1
->div
, qp2
->div
, exp1
, exp2
);
1139 isl_mat_free(qp1
->div
);
1140 qp1
->div
= isl_mat_copy(div
);
1141 isl_mat_free(qp2
->div
);
1142 qp2
->div
= isl_mat_copy(div
);
1144 qp1
->upoly
= expand(qp1
->upoly
, exp1
, div
->n_col
- div
->n_row
- 2);
1145 qp2
->upoly
= expand(qp2
->upoly
, exp2
, div
->n_col
- div
->n_row
- 2);
1147 if (!qp1
->upoly
|| !qp2
->upoly
)
1154 return fn(qp1
, qp2
);
1159 isl_qpolynomial_free(qp1
);
1160 isl_qpolynomial_free(qp2
);
1164 __isl_give isl_qpolynomial
*isl_qpolynomial_add(__isl_take isl_qpolynomial
*qp1
,
1165 __isl_take isl_qpolynomial
*qp2
)
1167 qp1
= isl_qpolynomial_cow(qp1
);
1172 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1173 return isl_qpolynomial_add(qp2
, qp1
);
1175 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1176 if (!compatible_divs(qp1
->div
, qp2
->div
))
1177 return with_merged_divs(isl_qpolynomial_add
, qp1
, qp2
);
1179 qp1
->upoly
= isl_upoly_sum(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1183 isl_qpolynomial_free(qp2
);
1187 isl_qpolynomial_free(qp1
);
1188 isl_qpolynomial_free(qp2
);
1192 __isl_give isl_qpolynomial
*isl_qpolynomial_sub(__isl_take isl_qpolynomial
*qp1
,
1193 __isl_take isl_qpolynomial
*qp2
)
1195 return isl_qpolynomial_add(qp1
, isl_qpolynomial_neg(qp2
));
1198 __isl_give isl_qpolynomial
*isl_qpolynomial_neg(__isl_take isl_qpolynomial
*qp
)
1200 qp
= isl_qpolynomial_cow(qp
);
1205 qp
->upoly
= isl_upoly_neg(qp
->upoly
);
1211 isl_qpolynomial_free(qp
);
1215 __isl_give isl_qpolynomial
*isl_qpolynomial_mul(__isl_take isl_qpolynomial
*qp1
,
1216 __isl_take isl_qpolynomial
*qp2
)
1218 qp1
= isl_qpolynomial_cow(qp1
);
1223 if (qp1
->div
->n_row
< qp2
->div
->n_row
)
1224 return isl_qpolynomial_mul(qp2
, qp1
);
1226 isl_assert(qp1
->dim
->ctx
, isl_dim_equal(qp1
->dim
, qp2
->dim
), goto error
);
1227 if (!compatible_divs(qp1
->div
, qp2
->div
))
1228 return with_merged_divs(isl_qpolynomial_mul
, qp1
, qp2
);
1230 qp1
->upoly
= isl_upoly_mul(qp1
->upoly
, isl_upoly_copy(qp2
->upoly
));
1234 isl_qpolynomial_free(qp2
);
1238 isl_qpolynomial_free(qp1
);
1239 isl_qpolynomial_free(qp2
);
1243 __isl_give isl_qpolynomial
*isl_qpolynomial_zero(__isl_take isl_dim
*dim
)
1245 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1248 __isl_give isl_qpolynomial
*isl_qpolynomial_infty(__isl_take isl_dim
*dim
)
1250 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_infty(dim
->ctx
));
1253 __isl_give isl_qpolynomial
*isl_qpolynomial_neginfty(__isl_take isl_dim
*dim
)
1255 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_neginfty(dim
->ctx
));
1258 __isl_give isl_qpolynomial
*isl_qpolynomial_nan(__isl_take isl_dim
*dim
)
1260 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_nan(dim
->ctx
));
1263 __isl_give isl_qpolynomial
*isl_qpolynomial_cst(__isl_take isl_dim
*dim
,
1266 struct isl_qpolynomial
*qp
;
1267 struct isl_upoly_cst
*cst
;
1269 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1273 cst
= isl_upoly_as_cst(qp
->upoly
);
1274 isl_int_set(cst
->n
, v
);
1279 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial
*qp
,
1280 isl_int
*n
, isl_int
*d
)
1282 struct isl_upoly_cst
*cst
;
1287 if (!isl_upoly_is_cst(qp
->upoly
))
1290 cst
= isl_upoly_as_cst(qp
->upoly
);
1295 isl_int_set(*n
, cst
->n
);
1297 isl_int_set(*d
, cst
->d
);
1302 int isl_upoly_is_affine(__isl_keep
struct isl_upoly
*up
)
1305 struct isl_upoly_rec
*rec
;
1313 rec
= isl_upoly_as_rec(up
);
1320 isl_assert(up
->ctx
, rec
->n
> 1, return -1);
1322 is_cst
= isl_upoly_is_cst(rec
->p
[1]);
1328 return isl_upoly_is_affine(rec
->p
[0]);
1331 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial
*qp
)
1336 if (qp
->div
->n_row
> 0)
1339 return isl_upoly_is_affine(qp
->upoly
);
1342 static void update_coeff(__isl_keep isl_vec
*aff
,
1343 __isl_keep
struct isl_upoly_cst
*cst
, int pos
)
1348 if (isl_int_is_zero(cst
->n
))
1353 isl_int_gcd(gcd
, cst
->d
, aff
->el
[0]);
1354 isl_int_divexact(f
, cst
->d
, gcd
);
1355 isl_int_divexact(gcd
, aff
->el
[0], gcd
);
1356 isl_seq_scale(aff
->el
, aff
->el
, f
, aff
->size
);
1357 isl_int_mul(aff
->el
[1 + pos
], gcd
, cst
->n
);
1362 int isl_upoly_update_affine(__isl_keep
struct isl_upoly
*up
,
1363 __isl_keep isl_vec
*aff
)
1365 struct isl_upoly_cst
*cst
;
1366 struct isl_upoly_rec
*rec
;
1372 struct isl_upoly_cst
*cst
;
1374 cst
= isl_upoly_as_cst(up
);
1377 update_coeff(aff
, cst
, 0);
1381 rec
= isl_upoly_as_rec(up
);
1384 isl_assert(up
->ctx
, rec
->n
== 2, return -1);
1386 cst
= isl_upoly_as_cst(rec
->p
[1]);
1389 update_coeff(aff
, cst
, 1 + up
->var
);
1391 return isl_upoly_update_affine(rec
->p
[0], aff
);
1394 __isl_give isl_vec
*isl_qpolynomial_extract_affine(
1395 __isl_keep isl_qpolynomial
*qp
)
1403 isl_assert(qp
->div
->ctx
, qp
->div
->n_row
== 0, return NULL
);
1404 d
= isl_dim_total(qp
->dim
);
1405 aff
= isl_vec_alloc(qp
->div
->ctx
, 2 + d
);
1409 isl_seq_clr(aff
->el
+ 1, 1 + d
);
1410 isl_int_set_si(aff
->el
[0], 1);
1412 if (isl_upoly_update_affine(qp
->upoly
, aff
) < 0)
1421 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial
*qp1
,
1422 __isl_keep isl_qpolynomial
*qp2
)
1427 return isl_upoly_is_equal(qp1
->upoly
, qp2
->upoly
);
1430 static void upoly_update_den(__isl_keep
struct isl_upoly
*up
, isl_int
*d
)
1433 struct isl_upoly_rec
*rec
;
1435 if (isl_upoly_is_cst(up
)) {
1436 struct isl_upoly_cst
*cst
;
1437 cst
= isl_upoly_as_cst(up
);
1440 isl_int_lcm(*d
, *d
, cst
->d
);
1444 rec
= isl_upoly_as_rec(up
);
1448 for (i
= 0; i
< rec
->n
; ++i
)
1449 upoly_update_den(rec
->p
[i
], d
);
1452 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial
*qp
, isl_int
*d
)
1454 isl_int_set_si(*d
, 1);
1457 upoly_update_den(qp
->upoly
, d
);
1460 __isl_give
struct isl_upoly
*isl_upoly_pow(isl_ctx
*ctx
, int pos
, int power
)
1463 struct isl_upoly
*up
;
1464 struct isl_upoly_rec
*rec
;
1465 struct isl_upoly_cst
*cst
;
1467 rec
= isl_upoly_alloc_rec(ctx
, pos
, 1 + power
);
1470 for (i
= 0; i
< 1 + power
; ++i
) {
1471 rec
->p
[i
] = isl_upoly_zero(ctx
);
1476 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1477 isl_int_set_si(cst
->n
, 1);
1481 isl_upoly_free(&rec
->up
);
1485 __isl_give isl_qpolynomial
*isl_qpolynomial_pow(__isl_take isl_dim
*dim
,
1488 struct isl_ctx
*ctx
;
1495 return isl_qpolynomial_alloc(dim
, 0, isl_upoly_pow(ctx
, pos
, power
));
1498 __isl_give isl_qpolynomial
*isl_qpolynomial_var(__isl_take isl_dim
*dim
,
1499 enum isl_dim_type type
, unsigned pos
)
1504 isl_assert(dim
->ctx
, isl_dim_size(dim
, isl_dim_in
) == 0, goto error
);
1505 isl_assert(dim
->ctx
, pos
< isl_dim_size(dim
, type
), goto error
);
1507 if (type
== isl_dim_set
)
1508 pos
+= isl_dim_size(dim
, isl_dim_param
);
1510 return isl_qpolynomial_pow(dim
, pos
, 1);
1516 __isl_give isl_qpolynomial
*isl_qpolynomial_div_pow(__isl_take isl_div
*div
,
1519 struct isl_qpolynomial
*qp
= NULL
;
1520 struct isl_upoly_rec
*rec
;
1521 struct isl_upoly_cst
*cst
;
1527 isl_assert(div
->ctx
, div
->bmap
->n_div
== 1, goto error
);
1529 pos
= isl_dim_total(div
->bmap
->dim
);
1530 rec
= isl_upoly_alloc_rec(div
->ctx
, pos
, 1 + power
);
1531 qp
= isl_qpolynomial_alloc(isl_basic_map_get_dim(div
->bmap
), 1,
1536 isl_seq_cpy(qp
->div
->row
[0], div
->line
[0], qp
->div
->n_col
- 1);
1537 isl_int_set_si(qp
->div
->row
[0][qp
->div
->n_col
- 1], 0);
1539 for (i
= 0; i
< 1 + power
; ++i
) {
1540 rec
->p
[i
] = isl_upoly_zero(div
->ctx
);
1545 cst
= isl_upoly_as_cst(rec
->p
[power
]);
1546 isl_int_set_si(cst
->n
, 1);
1552 isl_qpolynomial_free(qp
);
1557 __isl_give isl_qpolynomial
*isl_qpolynomial_div(__isl_take isl_div
*div
)
1559 return isl_qpolynomial_div_pow(div
, 1);
1562 __isl_give isl_qpolynomial
*isl_qpolynomial_rat_cst(__isl_take isl_dim
*dim
,
1563 const isl_int n
, const isl_int d
)
1565 struct isl_qpolynomial
*qp
;
1566 struct isl_upoly_cst
*cst
;
1568 qp
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_zero(dim
->ctx
));
1572 cst
= isl_upoly_as_cst(qp
->upoly
);
1573 isl_int_set(cst
->n
, n
);
1574 isl_int_set(cst
->d
, d
);
1579 static int up_set_active(__isl_keep
struct isl_upoly
*up
, int *active
, int d
)
1581 struct isl_upoly_rec
*rec
;
1587 if (isl_upoly_is_cst(up
))
1591 active
[up
->var
] = 1;
1593 rec
= isl_upoly_as_rec(up
);
1594 for (i
= 0; i
< rec
->n
; ++i
)
1595 if (up_set_active(rec
->p
[i
], active
, d
) < 0)
1601 static int set_active(__isl_keep isl_qpolynomial
*qp
, int *active
)
1604 int d
= isl_dim_total(qp
->dim
);
1609 for (i
= 0; i
< d
; ++i
)
1610 for (j
= 0; j
< qp
->div
->n_row
; ++j
) {
1611 if (isl_int_is_zero(qp
->div
->row
[j
][2 + i
]))
1617 return up_set_active(qp
->upoly
, active
, d
);
1620 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial
*qp
,
1621 enum isl_dim_type type
, unsigned first
, unsigned n
)
1632 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1634 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1635 type
== isl_dim_set
, return -1);
1637 active
= isl_calloc_array(set
->ctx
, int, isl_dim_total(qp
->dim
));
1638 if (set_active(qp
, active
) < 0)
1641 if (type
== isl_dim_set
)
1642 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1643 for (i
= 0; i
< n
; ++i
)
1644 if (active
[first
+ i
]) {
1657 __isl_give
struct isl_upoly
*isl_upoly_drop(__isl_take
struct isl_upoly
*up
,
1658 unsigned first
, unsigned n
)
1661 struct isl_upoly_rec
*rec
;
1665 if (n
== 0 || up
->var
< 0 || up
->var
< first
)
1667 if (up
->var
< first
+ n
) {
1668 up
= replace_by_constant_term(up
);
1669 return isl_upoly_drop(up
, first
, n
);
1671 up
= isl_upoly_cow(up
);
1675 rec
= isl_upoly_as_rec(up
);
1679 for (i
= 0; i
< rec
->n
; ++i
) {
1680 rec
->p
[i
] = isl_upoly_drop(rec
->p
[i
], first
, n
);
1691 __isl_give isl_qpolynomial
*isl_qpolynomial_drop_dims(
1692 __isl_take isl_qpolynomial
*qp
,
1693 enum isl_dim_type type
, unsigned first
, unsigned n
)
1700 qp
= isl_qpolynomial_cow(qp
);
1704 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
1706 isl_assert(qp
->dim
->ctx
, type
== isl_dim_param
||
1707 type
== isl_dim_set
, goto error
);
1709 qp
->dim
= isl_dim_drop(qp
->dim
, type
, first
, n
);
1713 if (type
== isl_dim_set
)
1714 first
+= isl_dim_size(qp
->dim
, isl_dim_param
);
1716 qp
->div
= isl_mat_drop_cols(qp
->div
, 2 + first
, n
);
1720 qp
->upoly
= isl_upoly_drop(qp
->upoly
, first
, n
);
1726 isl_qpolynomial_free(qp
);
1731 #define PW isl_pw_qpolynomial
1733 #define EL isl_qpolynomial
1735 #define IS_ZERO is_zero
1739 #define ADD(d,e1,e2) isl_qpolynomial_add(e1,e2)
1741 #include <isl_pw_templ.c>
1743 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial
*pwqp
)
1751 if (!isl_set_fast_is_universe(pwqp
->p
[0].set
))
1754 return isl_qpolynomial_is_one(pwqp
->p
[0].qp
);
1757 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_mul(
1758 __isl_take isl_pw_qpolynomial
*pwqp1
,
1759 __isl_take isl_pw_qpolynomial
*pwqp2
)
1762 struct isl_pw_qpolynomial
*res
;
1765 if (!pwqp1
|| !pwqp2
)
1768 isl_assert(pwqp1
->dim
->ctx
, isl_dim_equal(pwqp1
->dim
, pwqp2
->dim
),
1771 if (isl_pw_qpolynomial_is_zero(pwqp1
)) {
1772 isl_pw_qpolynomial_free(pwqp2
);
1776 if (isl_pw_qpolynomial_is_zero(pwqp2
)) {
1777 isl_pw_qpolynomial_free(pwqp1
);
1781 if (isl_pw_qpolynomial_is_one(pwqp1
)) {
1782 isl_pw_qpolynomial_free(pwqp1
);
1786 if (isl_pw_qpolynomial_is_one(pwqp2
)) {
1787 isl_pw_qpolynomial_free(pwqp2
);
1791 n
= pwqp1
->n
* pwqp2
->n
;
1792 res
= isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1
->dim
), n
);
1794 for (i
= 0; i
< pwqp1
->n
; ++i
) {
1795 for (j
= 0; j
< pwqp2
->n
; ++j
) {
1796 struct isl_set
*common
;
1797 struct isl_qpolynomial
*prod
;
1798 common
= isl_set_intersect(isl_set_copy(pwqp1
->p
[i
].set
),
1799 isl_set_copy(pwqp2
->p
[j
].set
));
1800 if (isl_set_fast_is_empty(common
)) {
1801 isl_set_free(common
);
1805 prod
= isl_qpolynomial_mul(
1806 isl_qpolynomial_copy(pwqp1
->p
[i
].qp
),
1807 isl_qpolynomial_copy(pwqp2
->p
[j
].qp
));
1809 res
= isl_pw_qpolynomial_add_piece(res
, common
, prod
);
1813 isl_pw_qpolynomial_free(pwqp1
);
1814 isl_pw_qpolynomial_free(pwqp2
);
1818 isl_pw_qpolynomial_free(pwqp1
);
1819 isl_pw_qpolynomial_free(pwqp2
);
1823 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_neg(
1824 __isl_take isl_pw_qpolynomial
*pwqp
)
1827 struct isl_pw_qpolynomial
*res
;
1833 if (isl_pw_qpolynomial_is_zero(pwqp
))
1836 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
1838 for (i
= 0; i
< pwqp
->n
; ++i
) {
1839 pwqp
->p
[i
].qp
= isl_qpolynomial_neg(pwqp
->p
[i
].qp
);
1846 isl_pw_qpolynomial_free(pwqp
);
1850 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sub(
1851 __isl_take isl_pw_qpolynomial
*pwqp1
,
1852 __isl_take isl_pw_qpolynomial
*pwqp2
)
1854 return isl_pw_qpolynomial_add(pwqp1
, isl_pw_qpolynomial_neg(pwqp2
));
1857 __isl_give
struct isl_upoly
*isl_upoly_eval(
1858 __isl_take
struct isl_upoly
*up
, __isl_take isl_vec
*vec
)
1861 struct isl_upoly_rec
*rec
;
1862 struct isl_upoly
*res
;
1863 struct isl_upoly
*base
;
1865 if (isl_upoly_is_cst(up
)) {
1870 rec
= isl_upoly_as_rec(up
);
1874 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
1876 base
= isl_upoly_rat_cst(up
->ctx
, vec
->el
[1 + up
->var
], vec
->el
[0]);
1878 res
= isl_upoly_eval(isl_upoly_copy(rec
->p
[rec
->n
- 1]),
1881 for (i
= rec
->n
- 2; i
>= 0; --i
) {
1882 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
1883 res
= isl_upoly_sum(res
,
1884 isl_upoly_eval(isl_upoly_copy(rec
->p
[i
]),
1885 isl_vec_copy(vec
)));
1888 isl_upoly_free(base
);
1898 __isl_give isl_qpolynomial
*isl_qpolynomial_eval(
1899 __isl_take isl_qpolynomial
*qp
, __isl_take isl_point
*pnt
)
1902 struct isl_upoly
*up
;
1907 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, qp
->dim
), goto error
);
1909 if (qp
->div
->n_row
== 0)
1910 ext
= isl_vec_copy(pnt
->vec
);
1913 unsigned dim
= isl_dim_total(qp
->dim
);
1914 ext
= isl_vec_alloc(qp
->dim
->ctx
, 1 + dim
+ qp
->div
->n_row
);
1918 isl_seq_cpy(ext
->el
, pnt
->vec
->el
, pnt
->vec
->size
);
1919 for (i
= 0; i
< qp
->div
->n_row
; ++i
) {
1920 isl_seq_inner_product(qp
->div
->row
[i
] + 1, ext
->el
,
1921 1 + dim
+ i
, &ext
->el
[1+dim
+i
]);
1922 isl_int_fdiv_q(ext
->el
[1+dim
+i
], ext
->el
[1+dim
+i
],
1923 qp
->div
->row
[i
][0]);
1927 up
= isl_upoly_eval(isl_upoly_copy(qp
->upoly
), ext
);
1931 dim
= isl_dim_copy(qp
->dim
);
1932 isl_qpolynomial_free(qp
);
1933 isl_point_free(pnt
);
1935 return isl_qpolynomial_alloc(dim
, 0, up
);
1937 isl_qpolynomial_free(qp
);
1938 isl_point_free(pnt
);
1942 int isl_upoly_cmp(__isl_keep
struct isl_upoly_cst
*cst1
,
1943 __isl_keep
struct isl_upoly_cst
*cst2
)
1948 isl_int_mul(t
, cst1
->n
, cst2
->d
);
1949 isl_int_submul(t
, cst2
->n
, cst1
->d
);
1950 cmp
= isl_int_sgn(t
);
1955 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial
*qp1
,
1956 __isl_keep isl_qpolynomial
*qp2
)
1958 struct isl_upoly_cst
*cst1
, *cst2
;
1962 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), return -1);
1963 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), return -1);
1964 if (isl_qpolynomial_is_nan(qp1
))
1966 if (isl_qpolynomial_is_nan(qp2
))
1968 cst1
= isl_upoly_as_cst(qp1
->upoly
);
1969 cst2
= isl_upoly_as_cst(qp2
->upoly
);
1971 return isl_upoly_cmp(cst1
, cst2
) <= 0;
1974 __isl_give isl_qpolynomial
*isl_qpolynomial_min_cst(
1975 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
1977 struct isl_upoly_cst
*cst1
, *cst2
;
1982 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
1983 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
1984 cst1
= isl_upoly_as_cst(qp1
->upoly
);
1985 cst2
= isl_upoly_as_cst(qp2
->upoly
);
1986 cmp
= isl_upoly_cmp(cst1
, cst2
);
1989 isl_qpolynomial_free(qp2
);
1991 isl_qpolynomial_free(qp1
);
1996 isl_qpolynomial_free(qp1
);
1997 isl_qpolynomial_free(qp2
);
2001 __isl_give isl_qpolynomial
*isl_qpolynomial_max_cst(
2002 __isl_take isl_qpolynomial
*qp1
, __isl_take isl_qpolynomial
*qp2
)
2004 struct isl_upoly_cst
*cst1
, *cst2
;
2009 isl_assert(qp1
->dim
->ctx
, isl_upoly_is_cst(qp1
->upoly
), goto error
);
2010 isl_assert(qp2
->dim
->ctx
, isl_upoly_is_cst(qp2
->upoly
), goto error
);
2011 cst1
= isl_upoly_as_cst(qp1
->upoly
);
2012 cst2
= isl_upoly_as_cst(qp2
->upoly
);
2013 cmp
= isl_upoly_cmp(cst1
, cst2
);
2016 isl_qpolynomial_free(qp2
);
2018 isl_qpolynomial_free(qp1
);
2023 isl_qpolynomial_free(qp1
);
2024 isl_qpolynomial_free(qp2
);
2028 __isl_give isl_qpolynomial
*isl_qpolynomial_add_dims(
2029 __isl_take isl_qpolynomial
*qp
, enum isl_dim_type type
, unsigned n
)
2038 qp
= isl_qpolynomial_cow(qp
);
2042 g_pos
= pos(qp
->dim
, type
) + isl_dim_size(qp
->dim
, type
);
2044 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + g_pos
, n
);
2048 total
= qp
->div
->n_col
- 2;
2049 if (total
> g_pos
) {
2051 exp
= isl_alloc_array(qp
->div
->ctx
, int, total
- g_pos
);
2054 for (i
= 0; i
< total
- g_pos
; ++i
)
2056 qp
->upoly
= expand(qp
->upoly
, exp
, g_pos
);
2062 qp
->dim
= isl_dim_add(qp
->dim
, type
, n
);
2068 isl_qpolynomial_free(qp
);
2072 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_add_dims(
2073 __isl_take isl_pw_qpolynomial
*pwqp
,
2074 enum isl_dim_type type
, unsigned n
)
2081 pwqp
= isl_pw_qpolynomial_cow(pwqp
);
2085 pwqp
->dim
= isl_dim_add(pwqp
->dim
, type
, n
);
2089 for (i
= 0; i
< pwqp
->n
; ++i
) {
2090 pwqp
->p
[i
].set
= isl_set_add(pwqp
->p
[i
].set
, type
, n
);
2091 if (!pwqp
->p
[i
].set
)
2093 pwqp
->p
[i
].qp
= isl_qpolynomial_add_dims(pwqp
->p
[i
].qp
, type
, n
);
2100 isl_pw_qpolynomial_free(pwqp
);
2104 static int *reordering_move(isl_ctx
*ctx
,
2105 unsigned len
, unsigned dst
, unsigned src
, unsigned n
)
2110 reordering
= isl_alloc_array(ctx
, int, len
);
2115 for (i
= 0; i
< dst
; ++i
)
2117 for (i
= 0; i
< n
; ++i
)
2118 reordering
[src
+ i
] = dst
+ i
;
2119 for (i
= 0; i
< src
- dst
; ++i
)
2120 reordering
[dst
+ i
] = dst
+ n
+ i
;
2121 for (i
= 0; i
< len
- src
- n
; ++i
)
2122 reordering
[src
+ n
+ i
] = src
+ n
+ i
;
2124 for (i
= 0; i
< src
; ++i
)
2126 for (i
= 0; i
< n
; ++i
)
2127 reordering
[src
+ i
] = dst
+ i
;
2128 for (i
= 0; i
< dst
- src
; ++i
)
2129 reordering
[src
+ n
+ i
] = src
+ i
;
2130 for (i
= 0; i
< len
- dst
- n
; ++i
)
2131 reordering
[dst
+ n
+ i
] = dst
+ n
+ i
;
2137 static __isl_give
struct isl_upoly
*reorder(__isl_take
struct isl_upoly
*up
,
2141 struct isl_upoly_rec
*rec
;
2142 struct isl_upoly
*base
;
2143 struct isl_upoly
*res
;
2145 if (isl_upoly_is_cst(up
))
2148 rec
= isl_upoly_as_rec(up
);
2152 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2154 base
= isl_upoly_pow(up
->ctx
, r
[up
->var
], 1);
2155 res
= reorder(isl_upoly_copy(rec
->p
[rec
->n
- 1]), r
);
2157 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2158 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2159 res
= isl_upoly_sum(res
, reorder(isl_upoly_copy(rec
->p
[i
]), r
));
2162 isl_upoly_free(base
);
2171 __isl_give isl_qpolynomial
*isl_qpolynomial_move_dims(
2172 __isl_take isl_qpolynomial
*qp
,
2173 enum isl_dim_type dst_type
, unsigned dst_pos
,
2174 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
2180 qp
= isl_qpolynomial_cow(qp
);
2184 isl_assert(qp
->dim
->ctx
, src_pos
+ n
<= isl_dim_size(qp
->dim
, src_type
),
2187 g_dst_pos
= pos(qp
->dim
, dst_type
) + dst_pos
;
2188 g_src_pos
= pos(qp
->dim
, src_type
) + src_pos
;
2189 if (dst_type
> src_type
)
2192 qp
->div
= isl_mat_move_cols(qp
->div
, 2 + g_dst_pos
, 2 + g_src_pos
, n
);
2193 qp
->div
= sort_divs(qp
->div
);
2197 reordering
= reordering_move(qp
->dim
->ctx
,
2198 qp
->div
->n_col
- 2, g_dst_pos
, g_src_pos
, n
);
2202 qp
->upoly
= reorder(qp
->upoly
, reordering
);
2207 qp
->dim
= isl_dim_move(qp
->dim
, dst_type
, dst_pos
, src_type
, src_pos
, n
);
2213 isl_qpolynomial_free(qp
);
2217 __isl_give
struct isl_upoly
*isl_upoly_from_affine(isl_ctx
*ctx
, isl_int
*f
,
2218 isl_int denom
, unsigned len
)
2221 struct isl_upoly
*up
;
2223 isl_assert(ctx
, len
>= 1, return NULL
);
2225 up
= isl_upoly_rat_cst(ctx
, f
[0], denom
);
2226 for (i
= 0; i
< len
- 1; ++i
) {
2227 struct isl_upoly
*t
;
2228 struct isl_upoly
*c
;
2230 if (isl_int_is_zero(f
[1 + i
]))
2233 c
= isl_upoly_rat_cst(ctx
, f
[1 + i
], denom
);
2234 t
= isl_upoly_pow(ctx
, i
, 1);
2235 t
= isl_upoly_mul(c
, t
);
2236 up
= isl_upoly_sum(up
, t
);
2242 __isl_give isl_qpolynomial
*isl_qpolynomial_from_constraint(
2243 __isl_take isl_constraint
*c
, enum isl_dim_type type
, unsigned pos
)
2247 struct isl_upoly
*up
;
2248 isl_qpolynomial
*qp
;
2254 isl_int_init(denom
);
2256 isl_constraint_get_coefficient(c
, type
, pos
, &denom
);
2257 isl_constraint_set_coefficient(c
, type
, pos
, c
->ctx
->zero
);
2258 sgn
= isl_int_sgn(denom
);
2259 isl_int_abs(denom
, denom
);
2260 up
= isl_upoly_from_affine(c
->ctx
, c
->line
[0], denom
,
2261 1 + isl_constraint_dim(c
, isl_dim_all
));
2263 isl_int_neg(denom
, denom
);
2264 isl_constraint_set_coefficient(c
, type
, pos
, denom
);
2266 dim
= isl_dim_copy(c
->bmap
->dim
);
2268 isl_int_clear(denom
);
2269 isl_constraint_free(c
);
2271 qp
= isl_qpolynomial_alloc(dim
, 0, up
);
2273 qp
= isl_qpolynomial_neg(qp
);
2277 __isl_give
struct isl_upoly
*isl_upoly_subs(__isl_take
struct isl_upoly
*up
,
2278 unsigned first
, unsigned n
, __isl_keep
struct isl_upoly
**subs
)
2281 struct isl_upoly_rec
*rec
;
2282 struct isl_upoly
*base
, *res
;
2287 if (isl_upoly_is_cst(up
))
2290 if (up
->var
< first
)
2293 rec
= isl_upoly_as_rec(up
);
2297 isl_assert(up
->ctx
, rec
->n
>= 1, goto error
);
2299 if (up
->var
>= first
+ n
)
2300 base
= isl_upoly_pow(up
->ctx
, up
->var
, 1);
2302 base
= isl_upoly_copy(subs
[up
->var
- first
]);
2304 res
= isl_upoly_subs(isl_upoly_copy(rec
->p
[rec
->n
- 1]), first
, n
, subs
);
2305 for (i
= rec
->n
- 2; i
>= 0; --i
) {
2306 struct isl_upoly
*t
;
2307 t
= isl_upoly_subs(isl_upoly_copy(rec
->p
[i
]), first
, n
, subs
);
2308 res
= isl_upoly_mul(res
, isl_upoly_copy(base
));
2309 res
= isl_upoly_sum(res
, t
);
2312 isl_upoly_free(base
);
2321 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2322 * in "qp" by subs[i].
2324 __isl_give isl_qpolynomial
*isl_qpolynomial_substitute(
2325 __isl_take isl_qpolynomial
*qp
,
2326 enum isl_dim_type type
, unsigned first
, unsigned n
,
2327 __isl_keep isl_qpolynomial
**subs
)
2330 struct isl_upoly
**ups
;
2335 qp
= isl_qpolynomial_cow(qp
);
2338 for (i
= 0; i
< n
; ++i
)
2342 isl_assert(qp
->dim
->ctx
, first
+ n
<= isl_dim_size(qp
->dim
, type
),
2345 for (i
= 0; i
< n
; ++i
)
2346 isl_assert(qp
->dim
->ctx
, isl_dim_equal(qp
->dim
, subs
[i
]->dim
),
2349 isl_assert(qp
->dim
->ctx
, qp
->div
->n_row
== 0, goto error
);
2350 for (i
= 0; i
< n
; ++i
)
2351 isl_assert(qp
->dim
->ctx
, subs
[i
]->div
->n_row
== 0, goto error
);
2353 first
+= pos(qp
->dim
, type
);
2355 ups
= isl_alloc_array(qp
->dim
->ctx
, struct isl_upoly
*, n
);
2358 for (i
= 0; i
< n
; ++i
)
2359 ups
[i
] = subs
[i
]->upoly
;
2361 qp
->upoly
= isl_upoly_subs(qp
->upoly
, first
, n
, ups
);
2370 isl_qpolynomial_free(qp
);
2374 __isl_give isl_basic_set
*add_div_constraints(__isl_take isl_basic_set
*bset
,
2375 __isl_take isl_mat
*div
)
2383 bset
= isl_basic_set_extend_constraints(bset
, 0, 2 * div
->n_row
);
2386 total
= isl_basic_set_total_dim(bset
);
2387 for (i
= 0; i
< div
->n_row
; ++i
)
2388 if (isl_basic_set_add_div_constraints_var(bset
,
2389 total
- div
->n_row
+ i
, div
->row
[i
]) < 0)
2396 isl_basic_set_free(bset
);
2400 /* Extend "bset" with extra set dimensions for each integer division
2401 * in "qp" and then call "fn" with the extended bset and the polynomial
2402 * that results from replacing each of the integer divisions by the
2403 * corresponding extra set dimension.
2405 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial
*qp
,
2406 __isl_keep isl_basic_set
*bset
,
2407 int (*fn
)(__isl_take isl_basic_set
*bset
,
2408 __isl_take isl_qpolynomial
*poly
, void *user
), void *user
)
2412 isl_qpolynomial
*poly
;
2416 if (qp
->div
->n_row
== 0)
2417 return fn(isl_basic_set_copy(bset
), isl_qpolynomial_copy(qp
),
2420 div
= isl_mat_copy(qp
->div
);
2421 dim
= isl_dim_copy(qp
->dim
);
2422 dim
= isl_dim_add(dim
, isl_dim_set
, qp
->div
->n_row
);
2423 poly
= isl_qpolynomial_alloc(dim
, 0, isl_upoly_copy(qp
->upoly
));
2424 bset
= isl_basic_set_copy(bset
);
2425 bset
= isl_basic_set_add(bset
, isl_dim_set
, qp
->div
->n_row
);
2426 bset
= add_div_constraints(bset
, div
);
2428 return fn(bset
, poly
, user
);
2433 __isl_give isl_term
*isl_term_alloc(__isl_take isl_dim
*dim
,
2434 __isl_take isl_mat
*div
)
2442 n
= isl_dim_total(dim
) + div
->n_row
;
2444 term
= isl_calloc(dim
->ctx
, struct isl_term
,
2445 sizeof(struct isl_term
) + (n
- 1) * sizeof(int));
2452 isl_int_init(term
->n
);
2453 isl_int_init(term
->d
);
2462 __isl_give isl_term
*isl_term_copy(__isl_keep isl_term
*term
)
2471 __isl_give isl_term
*isl_term_dup(__isl_keep isl_term
*term
)
2480 total
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
2482 dup
= isl_term_alloc(isl_dim_copy(term
->dim
), isl_mat_copy(term
->div
));
2486 isl_int_set(dup
->n
, term
->n
);
2487 isl_int_set(dup
->d
, term
->d
);
2489 for (i
= 0; i
< total
; ++i
)
2490 dup
->pow
[i
] = term
->pow
[i
];
2495 __isl_give isl_term
*isl_term_cow(__isl_take isl_term
*term
)
2503 return isl_term_dup(term
);
2506 void isl_term_free(__isl_take isl_term
*term
)
2511 if (--term
->ref
> 0)
2514 isl_dim_free(term
->dim
);
2515 isl_mat_free(term
->div
);
2516 isl_int_clear(term
->n
);
2517 isl_int_clear(term
->d
);
2521 unsigned isl_term_dim(__isl_keep isl_term
*term
, enum isl_dim_type type
)
2529 case isl_dim_out
: return isl_dim_size(term
->dim
, type
);
2530 case isl_dim_div
: return term
->div
->n_row
;
2531 case isl_dim_all
: return isl_dim_total(term
->dim
) + term
->div
->n_row
;
2536 isl_ctx
*isl_term_get_ctx(__isl_keep isl_term
*term
)
2538 return term
? term
->dim
->ctx
: NULL
;
2541 void isl_term_get_num(__isl_keep isl_term
*term
, isl_int
*n
)
2545 isl_int_set(*n
, term
->n
);
2548 void isl_term_get_den(__isl_keep isl_term
*term
, isl_int
*d
)
2552 isl_int_set(*d
, term
->d
);
2555 int isl_term_get_exp(__isl_keep isl_term
*term
,
2556 enum isl_dim_type type
, unsigned pos
)
2561 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, type
), return -1);
2563 if (type
>= isl_dim_set
)
2564 pos
+= isl_dim_size(term
->dim
, isl_dim_param
);
2565 if (type
>= isl_dim_div
)
2566 pos
+= isl_dim_size(term
->dim
, isl_dim_set
);
2568 return term
->pow
[pos
];
2571 __isl_give isl_div
*isl_term_get_div(__isl_keep isl_term
*term
, unsigned pos
)
2573 isl_basic_map
*bmap
;
2580 isl_assert(term
->dim
->ctx
, pos
< isl_term_dim(term
, isl_dim_div
),
2583 total
= term
->div
->n_col
- term
->div
->n_row
- 2;
2584 /* No nested divs for now */
2585 isl_assert(term
->dim
->ctx
,
2586 isl_seq_first_non_zero(term
->div
->row
[pos
] + 2 + total
,
2587 term
->div
->n_row
) == -1,
2590 bmap
= isl_basic_map_alloc_dim(isl_dim_copy(term
->dim
), 1, 0, 0);
2591 if ((k
= isl_basic_map_alloc_div(bmap
)) < 0)
2594 isl_seq_cpy(bmap
->div
[k
], term
->div
->row
[pos
], 2 + total
);
2596 return isl_basic_map_div(bmap
, k
);
2598 isl_basic_map_free(bmap
);
2602 __isl_give isl_term
*isl_upoly_foreach_term(__isl_keep
struct isl_upoly
*up
,
2603 int (*fn
)(__isl_take isl_term
*term
, void *user
),
2604 __isl_take isl_term
*term
, void *user
)
2607 struct isl_upoly_rec
*rec
;
2612 if (isl_upoly_is_zero(up
))
2615 isl_assert(up
->ctx
, !isl_upoly_is_nan(up
), goto error
);
2616 isl_assert(up
->ctx
, !isl_upoly_is_infty(up
), goto error
);
2617 isl_assert(up
->ctx
, !isl_upoly_is_neginfty(up
), goto error
);
2619 if (isl_upoly_is_cst(up
)) {
2620 struct isl_upoly_cst
*cst
;
2621 cst
= isl_upoly_as_cst(up
);
2624 term
= isl_term_cow(term
);
2627 isl_int_set(term
->n
, cst
->n
);
2628 isl_int_set(term
->d
, cst
->d
);
2629 if (fn(isl_term_copy(term
), user
) < 0)
2634 rec
= isl_upoly_as_rec(up
);
2638 for (i
= 0; i
< rec
->n
; ++i
) {
2639 term
= isl_term_cow(term
);
2642 term
->pow
[up
->var
] = i
;
2643 term
= isl_upoly_foreach_term(rec
->p
[i
], fn
, term
, user
);
2647 term
->pow
[up
->var
] = 0;
2651 isl_term_free(term
);
2655 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial
*qp
,
2656 int (*fn
)(__isl_take isl_term
*term
, void *user
), void *user
)
2663 term
= isl_term_alloc(isl_dim_copy(qp
->dim
), isl_mat_copy(qp
->div
));
2667 term
= isl_upoly_foreach_term(qp
->upoly
, fn
, term
, user
);
2669 isl_term_free(term
);
2671 return term
? 0 : -1;
2674 __isl_give isl_qpolynomial
*isl_qpolynomial_from_term(__isl_take isl_term
*term
)
2676 struct isl_upoly
*up
;
2677 isl_qpolynomial
*qp
;
2683 n
= isl_dim_total(term
->dim
) + term
->div
->n_row
;
2685 up
= isl_upoly_rat_cst(term
->dim
->ctx
, term
->n
, term
->d
);
2686 for (i
= 0; i
< n
; ++i
) {
2689 up
= isl_upoly_mul(up
,
2690 isl_upoly_pow(term
->dim
->ctx
, i
, term
->pow
[i
]));
2693 qp
= isl_qpolynomial_alloc(isl_dim_copy(term
->dim
), term
->div
->n_row
, up
);
2696 isl_mat_free(qp
->div
);
2697 qp
->div
= isl_mat_copy(term
->div
);
2701 isl_term_free(term
);
2704 isl_qpolynomial_free(qp
);
2705 isl_term_free(term
);
2709 int isl_pw_qpolynomial_foreach_piece(__isl_keep isl_pw_qpolynomial
*pwqp
,
2710 int (*fn
)(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
2711 void *user
), void *user
)
2718 for (i
= 0; i
< pwqp
->n
; ++i
)
2719 if (fn(isl_set_copy(pwqp
->p
[i
].set
),
2720 isl_qpolynomial_copy(pwqp
->p
[i
].qp
), user
) < 0)
2726 static int any_divs(__isl_keep isl_set
*set
)
2733 for (i
= 0; i
< set
->n
; ++i
)
2734 if (set
->p
[i
]->n_div
> 0)
2740 __isl_give isl_qpolynomial
*isl_qpolynomial_lift(__isl_take isl_qpolynomial
*qp
,
2741 __isl_take isl_dim
*dim
)
2746 if (isl_dim_equal(qp
->dim
, dim
)) {
2751 qp
= isl_qpolynomial_cow(qp
);
2755 if (qp
->div
->n_row
) {
2761 extra
= isl_dim_size(dim
, isl_dim_set
) -
2762 isl_dim_size(qp
->dim
, isl_dim_set
);
2763 total
= isl_dim_total(qp
->dim
);
2764 exp
= isl_alloc_array(qp
->div
->ctx
, int, qp
->div
->n_row
);
2767 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
2769 qp
->upoly
= expand(qp
->upoly
, exp
, total
);
2773 qp
->div
= isl_mat_insert_cols(qp
->div
, 2 + total
, extra
);
2776 for (i
= 0; i
< qp
->div
->n_row
; ++i
)
2777 isl_seq_clr(qp
->div
->row
[i
] + 2 + total
, extra
);
2780 isl_dim_free(qp
->dim
);
2786 isl_qpolynomial_free(qp
);
2790 static int foreach_lifted_subset(__isl_take isl_set
*set
,
2791 __isl_take isl_qpolynomial
*qp
,
2792 int (*fn
)(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
2793 void *user
), void *user
)
2800 for (i
= 0; i
< set
->n
; ++i
) {
2802 isl_qpolynomial
*copy
;
2804 lift
= isl_set_from_basic_set(isl_basic_set_copy(set
->p
[i
]));
2805 lift
= isl_set_lift(lift
);
2807 copy
= isl_qpolynomial_copy(qp
);
2808 copy
= isl_qpolynomial_lift(copy
, isl_set_get_dim(lift
));
2810 if (fn(lift
, copy
, user
) < 0)
2815 isl_qpolynomial_free(qp
);
2820 isl_qpolynomial_free(qp
);
2824 int isl_pw_qpolynomial_foreach_lifted_piece(__isl_keep isl_pw_qpolynomial
*pwqp
,
2825 int (*fn
)(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
2826 void *user
), void *user
)
2833 for (i
= 0; i
< pwqp
->n
; ++i
) {
2835 isl_qpolynomial
*qp
;
2837 set
= isl_set_copy(pwqp
->p
[i
].set
);
2838 qp
= isl_qpolynomial_copy(pwqp
->p
[i
].qp
);
2839 if (!any_divs(set
)) {
2840 if (fn(set
, qp
, user
) < 0)
2844 if (foreach_lifted_subset(set
, qp
, fn
, user
) < 0)
2851 /* For each parameter or variable that does not appear in qp,
2852 * first eliminate the variable from all constraints and then set it to zero.
2854 static __isl_give isl_set
*fix_inactive(__isl_take isl_set
*set
,
2855 __isl_keep isl_qpolynomial
*qp
)
2866 d
= isl_dim_total(set
->dim
);
2867 active
= isl_calloc_array(set
->ctx
, int, d
);
2868 if (set_active(qp
, active
) < 0)
2871 for (i
= 0; i
< d
; ++i
)
2880 nparam
= isl_dim_size(set
->dim
, isl_dim_param
);
2881 nvar
= isl_dim_size(set
->dim
, isl_dim_set
);
2882 for (i
= 0; i
< nparam
; ++i
) {
2885 set
= isl_set_eliminate(set
, isl_dim_param
, i
, 1);
2886 set
= isl_set_fix_si(set
, isl_dim_param
, i
, 0);
2888 for (i
= 0; i
< nvar
; ++i
) {
2889 if (active
[nparam
+ i
])
2891 set
= isl_set_eliminate(set
, isl_dim_set
, i
, 1);
2892 set
= isl_set_fix_si(set
, isl_dim_set
, i
, 0);
2904 struct isl_opt_data
{
2905 isl_qpolynomial
*qp
;
2907 isl_qpolynomial
*opt
;
2911 static int opt_fn(__isl_take isl_point
*pnt
, void *user
)
2913 struct isl_opt_data
*data
= (struct isl_opt_data
*)user
;
2914 isl_qpolynomial
*val
;
2916 val
= isl_qpolynomial_eval(isl_qpolynomial_copy(data
->qp
), pnt
);
2920 } else if (data
->max
) {
2921 data
->opt
= isl_qpolynomial_max_cst(data
->opt
, val
);
2923 data
->opt
= isl_qpolynomial_min_cst(data
->opt
, val
);
2929 __isl_give isl_qpolynomial
*isl_qpolynomial_opt_on_domain(
2930 __isl_take isl_qpolynomial
*qp
, __isl_take isl_set
*set
, int max
)
2932 struct isl_opt_data data
= { NULL
, 1, NULL
, max
};
2937 if (isl_upoly_is_cst(qp
->upoly
)) {
2942 set
= fix_inactive(set
, qp
);
2945 if (isl_set_foreach_point(set
, opt_fn
, &data
) < 0)
2949 data
.opt
= isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp
));
2952 isl_qpolynomial_free(qp
);
2956 isl_qpolynomial_free(qp
);
2957 isl_qpolynomial_free(data
.opt
);
2961 __isl_give isl_qpolynomial
*isl_qpolynomial_morph(__isl_take isl_qpolynomial
*qp
,
2962 __isl_take isl_morph
*morph
)
2966 struct isl_upoly
*up
;
2968 struct isl_upoly
**subs
;
2971 qp
= isl_qpolynomial_cow(qp
);
2976 isl_assert(ctx
, isl_dim_equal(qp
->dim
, morph
->dom
->dim
), goto error
);
2978 subs
= isl_calloc_array(ctx
, struct isl_upoly
*, morph
->inv
->n_row
- 1);
2982 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
2983 subs
[i
] = isl_upoly_from_affine(ctx
, morph
->inv
->row
[1 + i
],
2984 morph
->inv
->row
[0][0], morph
->inv
->n_col
);
2986 qp
->upoly
= isl_upoly_subs(qp
->upoly
, 0, morph
->inv
->n_row
- 1, subs
);
2988 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
)
2989 isl_upoly_free(subs
[i
]);
2992 mat
= isl_mat_diagonal(isl_mat_identity(ctx
, 1), isl_mat_copy(morph
->inv
));
2993 mat
= isl_mat_diagonal(mat
, isl_mat_identity(ctx
, qp
->div
->n_row
));
2994 qp
->div
= isl_mat_product(qp
->div
, mat
);
2995 isl_dim_free(qp
->dim
);
2996 qp
->dim
= isl_dim_copy(morph
->ran
->dim
);
2998 if (!qp
->upoly
|| !qp
->div
|| !qp
->dim
)
3001 isl_morph_free(morph
);
3005 isl_qpolynomial_free(qp
);
3006 isl_morph_free(morph
);