Polly 23.0.0git
isl_polynomial.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 INRIA Saclay
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
10
11#include <stdlib.h>
12#include <isl_ctx_private.h>
13#include <isl_map_private.h>
14#include <isl_factorization.h>
15#include <isl_lp_private.h>
16#include <isl_seq.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>
27#include <isl_aff_private.h>
28#include <isl_val_private.h>
29#include <isl_config.h>
30
31#undef EL_BASE
32#define EL_BASE qpolynomial
33
34#include <isl_list_templ.c>
35
36#undef EL_BASE
37#define EL_BASE pw_qpolynomial
38
39#include <isl_list_templ.c>
40
41static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
42{
43 switch (type) {
44 case isl_dim_param: return 0;
45 case isl_dim_in: return space->nparam;
46 case isl_dim_out: return space->nparam + space->n_in;
47 default: return 0;
48 }
49}
50
52{
53 if (!poly)
54 return isl_bool_error;
55
56 return isl_bool_ok(poly->var < 0);
57}
58
60{
61 if (!poly)
62 return NULL;
63
64 isl_assert(poly->ctx, poly->var < 0, return NULL);
65
66 return (isl_poly_cst *) poly;
67}
68
70{
71 if (!poly)
72 return NULL;
73
74 isl_assert(poly->ctx, poly->var >= 0, return NULL);
75
76 return (isl_poly_rec *) poly;
77}
78
79/* Compare two polynomials.
80 *
81 * Return -1 if "poly1" is "smaller" than "poly2", 1 if "poly1" is "greater"
82 * than "poly2" and 0 if they are equal.
83 */
85 __isl_keep isl_poly *poly2)
86{
87 int i;
88 isl_bool is_cst1;
89 isl_poly_rec *rec1, *rec2;
90
91 if (poly1 == poly2)
92 return 0;
93 is_cst1 = isl_poly_is_cst(poly1);
94 if (is_cst1 < 0)
95 return -1;
96 if (!poly2)
97 return 1;
98 if (poly1->var != poly2->var)
99 return poly1->var - poly2->var;
100
101 if (is_cst1) {
102 isl_poly_cst *cst1, *cst2;
103 int cmp;
104
105 cst1 = isl_poly_as_cst(poly1);
106 cst2 = isl_poly_as_cst(poly2);
107 if (!cst1 || !cst2)
108 return 0;
109 cmp = isl_int_cmp(cst1->n, cst2->n);
110 if (cmp != 0)
111 return cmp;
112 return isl_int_cmp(cst1->d, cst2->d);
113 }
114
115 rec1 = isl_poly_as_rec(poly1);
116 rec2 = isl_poly_as_rec(poly2);
117 if (!rec1 || !rec2)
118 return 0;
119
120 if (rec1->n != rec2->n)
121 return rec1->n - rec2->n;
122
123 for (i = 0; i < rec1->n; ++i) {
124 int cmp = isl_poly_plain_cmp(rec1->p[i], rec2->p[i]);
125 if (cmp != 0)
126 return cmp;
127 }
128
129 return 0;
130}
131
133 __isl_keep isl_poly *poly2)
134{
135 int i;
136 isl_bool is_cst1;
137 isl_poly_rec *rec1, *rec2;
138
139 is_cst1 = isl_poly_is_cst(poly1);
140 if (is_cst1 < 0 || !poly2)
141 return isl_bool_error;
142 if (poly1 == poly2)
143 return isl_bool_true;
144 if (poly1->var != poly2->var)
145 return isl_bool_false;
146 if (is_cst1) {
147 isl_poly_cst *cst1, *cst2;
148 int r;
149 cst1 = isl_poly_as_cst(poly1);
150 cst2 = isl_poly_as_cst(poly2);
151 if (!cst1 || !cst2)
152 return isl_bool_error;
153 r = isl_int_eq(cst1->n, cst2->n) &&
154 isl_int_eq(cst1->d, cst2->d);
155 return isl_bool_ok(r);
156 }
157
158 rec1 = isl_poly_as_rec(poly1);
159 rec2 = isl_poly_as_rec(poly2);
160 if (!rec1 || !rec2)
161 return isl_bool_error;
162
163 if (rec1->n != rec2->n)
164 return isl_bool_false;
165
166 for (i = 0; i < rec1->n; ++i) {
167 isl_bool eq = isl_poly_is_equal(rec1->p[i], rec2->p[i]);
168 if (eq < 0 || !eq)
169 return eq;
170 }
171
172 return isl_bool_true;
173}
174
176{
177 isl_bool is_cst;
178 isl_poly_cst *cst;
179
180 is_cst = isl_poly_is_cst(poly);
181 if (is_cst < 0 || !is_cst)
182 return is_cst;
183
184 cst = isl_poly_as_cst(poly);
185 if (!cst)
186 return isl_bool_error;
187
188 return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d));
189}
190
192{
193 isl_bool is_cst;
194 isl_poly_cst *cst;
195
196 is_cst = isl_poly_is_cst(poly);
197 if (is_cst < 0 || !is_cst)
198 return 0;
199
200 cst = isl_poly_as_cst(poly);
201 if (!cst)
202 return 0;
203
204 return isl_int_sgn(cst->n);
205}
206
208{
209 isl_bool is_cst;
210 isl_poly_cst *cst;
211
212 is_cst = isl_poly_is_cst(poly);
213 if (is_cst < 0 || !is_cst)
214 return is_cst;
215
216 cst = isl_poly_as_cst(poly);
217 if (!cst)
218 return isl_bool_error;
219
220 return isl_bool_ok(isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d));
221}
222
224{
225 isl_bool is_cst;
226 isl_poly_cst *cst;
227
228 is_cst = isl_poly_is_cst(poly);
229 if (is_cst < 0 || !is_cst)
230 return is_cst;
231
232 cst = isl_poly_as_cst(poly);
233 if (!cst)
234 return isl_bool_error;
235
236 return isl_bool_ok(isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d));
237}
238
240{
241 isl_bool is_cst;
242 isl_poly_cst *cst;
243
244 is_cst = isl_poly_is_cst(poly);
245 if (is_cst < 0 || !is_cst)
246 return is_cst;
247
248 cst = isl_poly_as_cst(poly);
249 if (!cst)
250 return isl_bool_error;
251
252 return isl_bool_ok(isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d));
253}
254
256{
257 isl_bool is_cst;
258 isl_poly_cst *cst;
259 int r;
260
261 is_cst = isl_poly_is_cst(poly);
262 if (is_cst < 0 || !is_cst)
263 return is_cst;
264
265 cst = isl_poly_as_cst(poly);
266 if (!cst)
267 return isl_bool_error;
268
269 r = isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
270 return isl_bool_ok(r);
271}
272
274{
275 isl_bool is_cst;
276 isl_poly_cst *cst;
277
278 is_cst = isl_poly_is_cst(poly);
279 if (is_cst < 0 || !is_cst)
280 return is_cst;
281
282 cst = isl_poly_as_cst(poly);
283 if (!cst)
284 return isl_bool_error;
285
286 return isl_bool_ok(isl_int_is_negone(cst->n) && isl_int_is_one(cst->d));
287}
288
290{
291 isl_poly_cst *cst;
292
293 cst = isl_alloc_type(ctx, struct isl_poly_cst);
294 if (!cst)
295 return NULL;
296
297 cst->poly.ref = 1;
298 cst->poly.ctx = ctx;
299 isl_ctx_ref(ctx);
300 cst->poly.var = -1;
301
302 isl_int_init(cst->n);
303 isl_int_init(cst->d);
304
305 return cst;
306}
307
309{
310 isl_poly_cst *cst;
311
312 cst = isl_poly_cst_alloc(ctx);
313 if (!cst)
314 return NULL;
315
316 isl_int_set_si(cst->n, 0);
317 isl_int_set_si(cst->d, 1);
318
319 return &cst->poly;
320}
321
323{
324 isl_poly_cst *cst;
325
326 cst = isl_poly_cst_alloc(ctx);
327 if (!cst)
328 return NULL;
329
330 isl_int_set_si(cst->n, 1);
331 isl_int_set_si(cst->d, 1);
332
333 return &cst->poly;
334}
335
337{
338 isl_poly_cst *cst;
339
340 cst = isl_poly_cst_alloc(ctx);
341 if (!cst)
342 return NULL;
343
344 isl_int_set_si(cst->n, 1);
345 isl_int_set_si(cst->d, 0);
346
347 return &cst->poly;
348}
349
351{
352 isl_poly_cst *cst;
353
354 cst = isl_poly_cst_alloc(ctx);
355 if (!cst)
356 return NULL;
357
358 isl_int_set_si(cst->n, -1);
359 isl_int_set_si(cst->d, 0);
360
361 return &cst->poly;
362}
363
365{
366 isl_poly_cst *cst;
367
368 cst = isl_poly_cst_alloc(ctx);
369 if (!cst)
370 return NULL;
371
372 isl_int_set_si(cst->n, 0);
373 isl_int_set_si(cst->d, 0);
374
375 return &cst->poly;
376}
377
379{
380 isl_poly_cst *cst;
381
382 cst = isl_poly_cst_alloc(ctx);
383 if (!cst)
384 return NULL;
385
386 isl_int_set(cst->n, n);
387 isl_int_set(cst->d, d);
388
389 return &cst->poly;
390}
391
393{
394 isl_poly_rec *rec;
395
396 isl_assert(ctx, var >= 0, return NULL);
397 isl_assert(ctx, size >= 0, return NULL);
398 rec = isl_calloc(ctx, struct isl_poly_rec,
399 sizeof(struct isl_poly_rec) +
400 size * sizeof(struct isl_poly *));
401 if (!rec)
402 return NULL;
403
404 rec->poly.ref = 1;
405 rec->poly.ctx = ctx;
406 isl_ctx_ref(ctx);
407 rec->poly.var = var;
408
409 rec->n = 0;
410 rec->size = size;
411
412 return rec;
413}
414
415/* Return the domain space of "qp".
416 * This may be either a copy or the space itself
417 * if there is only one reference to "qp".
418 * This allows the space to be modified inplace
419 * if both the quasi-polynomial and its domain space
420 * have only a single reference.
421 * The caller is not allowed to modify "qp" between this call and
422 * a subsequent call to isl_qpolynomial_restore_domain_space.
423 * The only exception is that isl_qpolynomial_free can be called instead.
424 */
427{
428 isl_space *space;
429
430 if (!qp)
431 return NULL;
432 if (qp->ref != 1)
434 space = qp->dim;
435 qp->dim = NULL;
436 return space;
437}
438
439/* Set the domain space of "qp" to "space",
440 * where the domain space of "qp" may be missing
441 * due to a preceding call to isl_qpolynomial_take_domain_space.
442 * However, in this case, "qp" only has a single reference and
443 * then the call to isl_qpolynomial_cow has no effect.
444 */
447{
448 if (!qp || !space)
449 goto error;
450
451 if (qp->dim == space) {
452 isl_space_free(space);
453 return qp;
454 }
455
456 qp = isl_qpolynomial_cow(qp);
457 if (!qp)
458 goto error;
459 isl_space_free(qp->dim);
460 qp->dim = space;
461
462 return qp;
463error:
465 isl_space_free(space);
466 return NULL;
467}
468
474
475/* Reset the space of "qp". This function is called from isl_pw_templ.c
476 * and doesn't know if the space of an element object is represented
477 * directly or through its domain. It therefore passes along both.
478 */
486
488{
489 return qp ? qp->dim->ctx : NULL;
490}
491
492/* Return the domain space of "qp".
493 */
496{
497 return qp ? qp->dim : NULL;
498}
499
500/* Return a copy of the domain space of "qp".
501 */
507
508#undef TYPE
509#define TYPE isl_qpolynomial
510#undef PEEK_SPACE
511#define PEEK_SPACE peek_domain_space
512
513static
515static
517
518#undef PEEK_SPACE
519
520/* Return a copy of the local variables of "qp".
521 */
524{
525 return qp ? isl_local_copy(qp->div) : NULL;
526}
527
528/* Return the local variables of "qp".
529 * This may be either a copy or the local variables themselves
530 * if there is only one reference to "qp".
531 * This allows the local variables to be modified in-place
532 * if both the quasi-polynomial and its local variables
533 * have only a single reference.
534 * The caller is not allowed to modify "qp" between this call and
535 * the subsequent call to isl_qpolynomial_restore_local.
536 * The only exception is that isl_qpolynomial_free can be called instead.
537 */
540{
541 isl_local *local;
542
543 if (!qp)
544 return NULL;
545 if (qp->ref != 1)
546 return isl_qpolynomial_get_local(qp);
547 local = qp->div;
548 qp->div = NULL;
549 return local;
550}
551
552/* Set the local variables of "qp" to "local",
553 * where the local variables of "qp" may be missing
554 * due to a preceding call to isl_qpolynomial_take_local.
555 * However, in this case, "qp" only has a single reference and
556 * then the call to isl_qpolynomial_cow has no effect.
557 */
560{
561 if (!qp || !local)
562 goto error;
563
564 if (qp->div == local) {
565 isl_local_free(local);
566 return qp;
567 }
568
569 qp = isl_qpolynomial_cow(qp);
570 if (!qp)
571 goto error;
572 isl_local_free(qp->div);
573 qp->div = local;
574
575 return qp;
576error:
578 isl_local_free(local);
579 return NULL;
580}
581
582/* Return a copy of the local space on which "qp" is defined.
583 */
586{
587 isl_space *space;
588 isl_local *local;
589
590 if (!qp)
591 return NULL;
592
594 local = isl_qpolynomial_get_local(qp);
595 return isl_local_space_alloc_div(space, local);
596}
597
599{
600 isl_space *space;
601 if (!qp)
602 return NULL;
603 space = isl_space_copy(qp->dim);
604 space = isl_space_from_domain(space);
605 space = isl_space_add_dims(space, isl_dim_out, 1);
606 return space;
607}
608
609/* Return the number of variables of the given type in the domain of "qp".
610 */
612 enum isl_dim_type type)
613{
614 isl_space *space;
615 isl_size dim;
616
618
619 if (!space)
620 return isl_size_error;
621 if (type == isl_dim_div)
622 return qp->div->n_row;
623 dim = isl_space_dim(space, type);
624 if (dim < 0)
625 return isl_size_error;
626 if (type == isl_dim_all) {
627 isl_size n_div;
628
630 if (n_div < 0)
631 return isl_size_error;
632 dim += n_div;
633 }
634 return dim;
635}
636
637/* Given the type of a dimension of an isl_qpolynomial,
638 * return the type of the corresponding dimension in its domain.
639 * This function is only called for "type" equal to isl_dim_in or
640 * isl_dim_param.
641 */
643{
644 return type == isl_dim_in ? isl_dim_set : type;
645}
646
647/* Externally, an isl_qpolynomial has a map space, but internally, the
648 * ls field corresponds to the domain of that space.
649 */
651 enum isl_dim_type type)
652{
653 if (!qp)
654 return isl_size_error;
655 if (type == isl_dim_out)
656 return 1;
659}
660
661/* Return the offset of the first variable of type "type" within
662 * the variables of the domain of "qp".
663 */
666{
667 isl_space *space;
668
670
671 switch (type) {
672 case isl_dim_param:
673 case isl_dim_set: return isl_space_offset(space, type);
674 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
675 case isl_dim_cst:
676 default:
678 "invalid dimension type", return isl_size_error);
679 }
680}
681
682/* Return the offset of the first coefficient of type "type" in
683 * the domain of "qp".
684 */
686 enum isl_dim_type type)
687{
688 switch (type) {
689 case isl_dim_cst:
690 return 0;
691 case isl_dim_param:
692 case isl_dim_set:
693 case isl_dim_div:
695 default:
696 return 0;
697 }
698}
699
700/* Return the polynomial expression of "qp".
701 */
704{
705 return qp ? qp->poly : NULL;
706}
707
712
717
722
727
732
737
739{
740 isl_int_clear(cst->n);
741 isl_int_clear(cst->d);
742}
743
745{
746 int i;
747
748 for (i = 0; i < rec->n; ++i)
749 isl_poly_free(rec->p[i]);
750}
751
753{
754 if (!poly)
755 return NULL;
756
757 poly->ref++;
758 return poly;
759}
760
762{
763 isl_poly_cst *cst;
765
766 cst = isl_poly_as_cst(poly);
767 if (!cst)
768 return NULL;
769
771 if (!dup)
772 return NULL;
773 isl_int_set(dup->n, cst->n);
774 isl_int_set(dup->d, cst->d);
775
776 return &dup->poly;
777}
778
780{
781 int i;
782 isl_poly_rec *rec;
784
785 rec = isl_poly_as_rec(poly);
786 if (!rec)
787 return NULL;
788
789 dup = isl_poly_alloc_rec(poly->ctx, poly->var, rec->n);
790 if (!dup)
791 return NULL;
792
793 for (i = 0; i < rec->n; ++i) {
794 dup->p[i] = isl_poly_copy(rec->p[i]);
795 if (!dup->p[i])
796 goto error;
797 dup->n++;
798 }
799
800 return &dup->poly;
801error:
802 isl_poly_free(&dup->poly);
803 return NULL;
804}
805
807{
808 isl_bool is_cst;
809
810 is_cst = isl_poly_is_cst(poly);
811 if (is_cst < 0)
812 return NULL;
813 if (is_cst)
814 return isl_poly_dup_cst(poly);
815 else
816 return isl_poly_dup_rec(poly);
817}
818
820{
821 if (!poly)
822 return NULL;
823
824 if (poly->ref == 1)
825 return poly;
826 poly->ref--;
827 return isl_poly_dup(poly);
828}
829
831{
832 if (!poly)
833 return NULL;
834
835 if (--poly->ref > 0)
836 return NULL;
837
838 if (poly->var < 0)
840 else
842
843 isl_ctx_deref(poly->ctx);
844 free(poly);
845 return NULL;
846}
847
849{
850 isl_int gcd;
851
853 isl_int_gcd(gcd, cst->n, cst->d);
855 isl_int_divexact(cst->n, cst->n, gcd);
856 isl_int_divexact(cst->d, cst->d, gcd);
857 }
859}
860
862 __isl_take isl_poly *poly2)
863{
864 isl_poly_cst *cst1;
865 isl_poly_cst *cst2;
866
867 poly1 = isl_poly_cow(poly1);
868 if (!poly1 || !poly2)
869 goto error;
870
871 cst1 = isl_poly_as_cst(poly1);
872 cst2 = isl_poly_as_cst(poly2);
873
874 if (isl_int_eq(cst1->d, cst2->d))
875 isl_int_add(cst1->n, cst1->n, cst2->n);
876 else {
877 isl_int_mul(cst1->n, cst1->n, cst2->d);
878 isl_int_addmul(cst1->n, cst2->n, cst1->d);
879 isl_int_mul(cst1->d, cst1->d, cst2->d);
880 }
881
883
884 isl_poly_free(poly2);
885 return poly1;
886error:
887 isl_poly_free(poly1);
888 isl_poly_free(poly2);
889 return NULL;
890}
891
893{
894 struct isl_ctx *ctx;
895
896 if (!poly)
897 return NULL;
898 ctx = poly->ctx;
900 return isl_poly_zero(ctx);
901}
902
904{
905 isl_poly_rec *rec;
906 isl_poly *cst;
907
908 if (!poly)
909 return NULL;
910
911 rec = isl_poly_as_rec(poly);
912 if (!rec)
913 goto error;
914 cst = isl_poly_copy(rec->p[0]);
916 return cst;
917error:
919 return NULL;
920}
921
923 __isl_take isl_poly *poly2)
924{
925 int i;
926 isl_bool is_zero, is_nan, is_cst;
927 isl_poly_rec *rec1, *rec2;
928
929 if (!poly1 || !poly2)
930 goto error;
931
932 is_nan = isl_poly_is_nan(poly1);
933 if (is_nan < 0)
934 goto error;
935 if (is_nan) {
936 isl_poly_free(poly2);
937 return poly1;
938 }
939
940 is_nan = isl_poly_is_nan(poly2);
941 if (is_nan < 0)
942 goto error;
943 if (is_nan) {
944 isl_poly_free(poly1);
945 return poly2;
946 }
947
948 is_zero = isl_poly_is_zero(poly1);
949 if (is_zero < 0)
950 goto error;
951 if (is_zero) {
952 isl_poly_free(poly1);
953 return poly2;
954 }
955
956 is_zero = isl_poly_is_zero(poly2);
957 if (is_zero < 0)
958 goto error;
959 if (is_zero) {
960 isl_poly_free(poly2);
961 return poly1;
962 }
963
964 if (poly1->var < poly2->var)
965 return isl_poly_sum(poly2, poly1);
966
967 if (poly2->var < poly1->var) {
968 isl_poly_rec *rec;
969 isl_bool is_infty;
970
971 is_infty = isl_poly_is_infty(poly2);
972 if (is_infty >= 0 && !is_infty)
973 is_infty = isl_poly_is_neginfty(poly2);
974 if (is_infty < 0)
975 goto error;
976 if (is_infty) {
977 isl_poly_free(poly1);
978 return poly2;
979 }
980 poly1 = isl_poly_cow(poly1);
981 rec = isl_poly_as_rec(poly1);
982 if (!rec)
983 goto error;
984 rec->p[0] = isl_poly_sum(rec->p[0], poly2);
985 if (rec->n == 1)
986 poly1 = replace_by_constant_term(poly1);
987 return poly1;
988 }
989
990 is_cst = isl_poly_is_cst(poly1);
991 if (is_cst < 0)
992 goto error;
993 if (is_cst)
994 return isl_poly_sum_cst(poly1, poly2);
995
996 rec1 = isl_poly_as_rec(poly1);
997 rec2 = isl_poly_as_rec(poly2);
998 if (!rec1 || !rec2)
999 goto error;
1000
1001 if (rec1->n < rec2->n)
1002 return isl_poly_sum(poly2, poly1);
1003
1004 poly1 = isl_poly_cow(poly1);
1005 rec1 = isl_poly_as_rec(poly1);
1006 if (!rec1)
1007 goto error;
1008
1009 for (i = rec2->n - 1; i >= 0; --i) {
1010 isl_bool is_zero;
1011
1012 rec1->p[i] = isl_poly_sum(rec1->p[i],
1013 isl_poly_copy(rec2->p[i]));
1014 if (!rec1->p[i])
1015 goto error;
1016 if (i != rec1->n - 1)
1017 continue;
1018 is_zero = isl_poly_is_zero(rec1->p[i]);
1019 if (is_zero < 0)
1020 goto error;
1021 if (is_zero) {
1022 isl_poly_free(rec1->p[i]);
1023 rec1->n--;
1024 }
1025 }
1026
1027 if (rec1->n == 0)
1028 poly1 = replace_by_zero(poly1);
1029 else if (rec1->n == 1)
1030 poly1 = replace_by_constant_term(poly1);
1031
1032 isl_poly_free(poly2);
1033
1034 return poly1;
1035error:
1036 isl_poly_free(poly1);
1037 isl_poly_free(poly2);
1038 return NULL;
1039}
1040
1042 isl_int v)
1043{
1044 isl_poly_cst *cst;
1045
1047 if (!poly)
1048 return NULL;
1049
1050 cst = isl_poly_as_cst(poly);
1051
1052 isl_int_addmul(cst->n, cst->d, v);
1053
1054 return poly;
1055}
1056
1058{
1059 isl_bool is_cst;
1060 isl_poly_rec *rec;
1061
1062 is_cst = isl_poly_is_cst(poly);
1063 if (is_cst < 0)
1064 return isl_poly_free(poly);
1065 if (is_cst)
1066 return isl_poly_cst_add_isl_int(poly, v);
1067
1069 rec = isl_poly_as_rec(poly);
1070 if (!rec)
1071 goto error;
1072
1073 rec->p[0] = isl_poly_add_isl_int(rec->p[0], v);
1074 if (!rec->p[0])
1075 goto error;
1076
1077 return poly;
1078error:
1080 return NULL;
1081}
1082
1084 isl_int v)
1085{
1086 isl_bool is_zero;
1087 isl_poly_cst *cst;
1088
1089 is_zero = isl_poly_is_zero(poly);
1090 if (is_zero < 0)
1091 return isl_poly_free(poly);
1092 if (is_zero)
1093 return poly;
1094
1096 if (!poly)
1097 return NULL;
1098
1099 cst = isl_poly_as_cst(poly);
1100
1101 isl_int_mul(cst->n, cst->n, v);
1102
1103 return poly;
1104}
1105
1107{
1108 int i;
1109 isl_bool is_cst;
1110 isl_poly_rec *rec;
1111
1112 is_cst = isl_poly_is_cst(poly);
1113 if (is_cst < 0)
1114 return isl_poly_free(poly);
1115 if (is_cst)
1116 return isl_poly_cst_mul_isl_int(poly, v);
1117
1119 rec = isl_poly_as_rec(poly);
1120 if (!rec)
1121 goto error;
1122
1123 for (i = 0; i < rec->n; ++i) {
1124 rec->p[i] = isl_poly_mul_isl_int(rec->p[i], v);
1125 if (!rec->p[i])
1126 goto error;
1127 }
1128
1129 return poly;
1130error:
1132 return NULL;
1133}
1134
1135/* Multiply the constant polynomial "poly" by "v".
1136 */
1139{
1140 isl_bool is_zero;
1141 isl_poly_cst *cst;
1142
1143 is_zero = isl_poly_is_zero(poly);
1144 if (is_zero < 0)
1145 return isl_poly_free(poly);
1146 if (is_zero)
1147 return poly;
1148
1150 if (!poly)
1151 return NULL;
1152
1153 cst = isl_poly_as_cst(poly);
1154
1155 isl_int_mul(cst->n, cst->n, v->n);
1156 isl_int_mul(cst->d, cst->d, v->d);
1158
1159 return poly;
1160}
1161
1162/* Multiply the polynomial "poly" by "v".
1163 */
1166{
1167 int i;
1168 isl_bool is_cst;
1169 isl_poly_rec *rec;
1170
1171 is_cst = isl_poly_is_cst(poly);
1172 if (is_cst < 0)
1173 return isl_poly_free(poly);
1174 if (is_cst)
1175 return isl_poly_cst_scale_val(poly, v);
1176
1178 rec = isl_poly_as_rec(poly);
1179 if (!rec)
1180 goto error;
1181
1182 for (i = 0; i < rec->n; ++i) {
1183 rec->p[i] = isl_poly_scale_val(rec->p[i], v);
1184 if (!rec->p[i])
1185 goto error;
1186 }
1187
1188 return poly;
1189error:
1191 return NULL;
1192}
1193
1195 __isl_take isl_poly *poly2)
1196{
1197 isl_poly_cst *cst1;
1198 isl_poly_cst *cst2;
1199
1200 poly1 = isl_poly_cow(poly1);
1201 if (!poly1 || !poly2)
1202 goto error;
1203
1204 cst1 = isl_poly_as_cst(poly1);
1205 cst2 = isl_poly_as_cst(poly2);
1206
1207 isl_int_mul(cst1->n, cst1->n, cst2->n);
1208 isl_int_mul(cst1->d, cst1->d, cst2->d);
1209
1210 isl_poly_cst_reduce(cst1);
1211
1212 isl_poly_free(poly2);
1213 return poly1;
1214error:
1215 isl_poly_free(poly1);
1216 isl_poly_free(poly2);
1217 return NULL;
1218}
1219
1221 __isl_take isl_poly *poly2)
1222{
1223 isl_poly_rec *rec1;
1224 isl_poly_rec *rec2;
1225 isl_poly_rec *res = NULL;
1226 int i, j;
1227 int size;
1228
1229 rec1 = isl_poly_as_rec(poly1);
1230 rec2 = isl_poly_as_rec(poly2);
1231 if (!rec1 || !rec2)
1232 goto error;
1233 size = rec1->n + rec2->n - 1;
1234 res = isl_poly_alloc_rec(poly1->ctx, poly1->var, size);
1235 if (!res)
1236 goto error;
1237
1238 for (i = 0; i < rec1->n; ++i) {
1239 res->p[i] = isl_poly_mul(isl_poly_copy(rec2->p[0]),
1240 isl_poly_copy(rec1->p[i]));
1241 if (!res->p[i])
1242 goto error;
1243 res->n++;
1244 }
1245 for (; i < size; ++i) {
1246 res->p[i] = isl_poly_zero(poly1->ctx);
1247 if (!res->p[i])
1248 goto error;
1249 res->n++;
1250 }
1251 for (i = 0; i < rec1->n; ++i) {
1252 for (j = 1; j < rec2->n; ++j) {
1253 isl_poly *poly;
1254 poly = isl_poly_mul(isl_poly_copy(rec2->p[j]),
1255 isl_poly_copy(rec1->p[i]));
1256 res->p[i + j] = isl_poly_sum(res->p[i + j], poly);
1257 if (!res->p[i + j])
1258 goto error;
1259 }
1260 }
1261
1262 isl_poly_free(poly1);
1263 isl_poly_free(poly2);
1264
1265 return &res->poly;
1266error:
1267 isl_poly_free(poly1);
1268 isl_poly_free(poly2);
1269 isl_poly_free(&res->poly);
1270 return NULL;
1271}
1272
1274 __isl_take isl_poly *poly2)
1275{
1276 isl_bool is_zero, is_nan, is_one, is_cst;
1277
1278 if (!poly1 || !poly2)
1279 goto error;
1280
1281 is_nan = isl_poly_is_nan(poly1);
1282 if (is_nan < 0)
1283 goto error;
1284 if (is_nan) {
1285 isl_poly_free(poly2);
1286 return poly1;
1287 }
1288
1289 is_nan = isl_poly_is_nan(poly2);
1290 if (is_nan < 0)
1291 goto error;
1292 if (is_nan) {
1293 isl_poly_free(poly1);
1294 return poly2;
1295 }
1296
1297 is_zero = isl_poly_is_zero(poly1);
1298 if (is_zero < 0)
1299 goto error;
1300 if (is_zero) {
1301 isl_poly_free(poly2);
1302 return poly1;
1303 }
1304
1305 is_zero = isl_poly_is_zero(poly2);
1306 if (is_zero < 0)
1307 goto error;
1308 if (is_zero) {
1309 isl_poly_free(poly1);
1310 return poly2;
1311 }
1312
1313 is_one = isl_poly_is_one(poly1);
1314 if (is_one < 0)
1315 goto error;
1316 if (is_one) {
1317 isl_poly_free(poly1);
1318 return poly2;
1319 }
1320
1321 is_one = isl_poly_is_one(poly2);
1322 if (is_one < 0)
1323 goto error;
1324 if (is_one) {
1325 isl_poly_free(poly2);
1326 return poly1;
1327 }
1328
1329 if (poly1->var < poly2->var)
1330 return isl_poly_mul(poly2, poly1);
1331
1332 if (poly2->var < poly1->var) {
1333 int i;
1334 isl_poly_rec *rec;
1335 isl_bool is_infty;
1336
1337 is_infty = isl_poly_is_infty(poly2);
1338 if (is_infty >= 0 && !is_infty)
1339 is_infty = isl_poly_is_neginfty(poly2);
1340 if (is_infty < 0)
1341 goto error;
1342 if (is_infty) {
1343 isl_ctx *ctx = poly1->ctx;
1344 isl_poly_free(poly1);
1345 isl_poly_free(poly2);
1346 return isl_poly_nan(ctx);
1347 }
1348 poly1 = isl_poly_cow(poly1);
1349 rec = isl_poly_as_rec(poly1);
1350 if (!rec)
1351 goto error;
1352
1353 for (i = 0; i < rec->n; ++i) {
1354 rec->p[i] = isl_poly_mul(rec->p[i],
1355 isl_poly_copy(poly2));
1356 if (!rec->p[i])
1357 goto error;
1358 }
1359 isl_poly_free(poly2);
1360 return poly1;
1361 }
1362
1363 is_cst = isl_poly_is_cst(poly1);
1364 if (is_cst < 0)
1365 goto error;
1366 if (is_cst)
1367 return isl_poly_mul_cst(poly1, poly2);
1368
1369 return isl_poly_mul_rec(poly1, poly2);
1370error:
1371 isl_poly_free(poly1);
1372 isl_poly_free(poly2);
1373 return NULL;
1374}
1375
1377{
1378 isl_poly *res;
1379
1380 if (!poly)
1381 return NULL;
1382 if (power == 1)
1383 return poly;
1384
1385 if (power % 2)
1387 else
1388 res = isl_poly_one(poly->ctx);
1389
1390 while (power >>= 1) {
1392 if (power % 2)
1394 }
1395
1397 return res;
1398}
1399
1401 unsigned n_div, __isl_take isl_poly *poly)
1402{
1403 struct isl_qpolynomial *qp = NULL;
1405
1407 if (total < 0 || !poly)
1408 goto error;
1409
1410 if (!isl_space_is_set(space))
1412 "domain of polynomial should be a set", goto error);
1413
1414 qp = isl_calloc_type(space->ctx, struct isl_qpolynomial);
1415 if (!qp)
1416 goto error;
1417
1418 qp->ref = 1;
1419 qp->div = isl_mat_alloc(space->ctx, n_div, 1 + 1 + total + n_div);
1420 if (!qp->div)
1421 goto error;
1422
1423 qp->dim = space;
1424 qp->poly = poly;
1425
1426 return qp;
1427error:
1428 isl_space_free(space);
1431 return NULL;
1432}
1433
1435{
1436 if (!qp)
1437 return NULL;
1438
1439 qp->ref++;
1440 return qp;
1441}
1442
1443/* Return a copy of the polynomial expression of "qp".
1444 */
1449
1450/* Return the polynomial expression of "qp".
1451 * This may be either a copy or the polynomial expression itself
1452 * if there is only one reference to "qp".
1453 * This allows the polynomial expression to be modified inplace
1454 * if both the quasi-polynomial and its polynomial expression
1455 * have only a single reference.
1456 * The caller is not allowed to modify "qp" between this call and
1457 * a subsequent call to isl_qpolynomial_restore_poly.
1458 * The only exception is that isl_qpolynomial_free can be called instead.
1459 */
1462{
1463 isl_poly *poly;
1464
1465 if (!qp)
1466 return NULL;
1467 if (qp->ref != 1)
1468 return isl_qpolynomial_get_poly(qp);
1469 poly = qp->poly;
1470 qp->poly = NULL;
1471 return poly;
1472}
1473
1474/* Set the polynomial expression of "qp" to "space",
1475 * where the polynomial expression of "qp" may be missing
1476 * due to a preceding call to isl_qpolynomial_take_poly.
1477 * However, in this case, "qp" only has a single reference and
1478 * then the call to isl_qpolynomial_cow has no effect.
1479 */
1482{
1483 if (!qp || !poly)
1484 goto error;
1485
1486 if (qp->poly == poly) {
1488 return qp;
1489 }
1490
1491 qp = isl_qpolynomial_cow(qp);
1492 if (!qp)
1493 goto error;
1494 isl_poly_free(qp->poly);
1495 qp->poly = poly;
1496
1497 return qp;
1498error:
1501 return NULL;
1502}
1503
1505{
1506 isl_poly *poly;
1507 struct isl_qpolynomial *dup;
1508
1509 if (!qp)
1510 return NULL;
1511
1513 dup = isl_qpolynomial_alloc(isl_space_copy(qp->dim), qp->div->n_row,
1514 poly);
1515 if (!dup)
1516 return NULL;
1517 isl_mat_free(dup->div);
1518 dup->div = isl_qpolynomial_get_local(qp);
1519 if (!dup->div)
1520 goto error;
1521
1522 return dup;
1523error:
1525 return NULL;
1526}
1527
1529{
1530 if (!qp)
1531 return NULL;
1532
1533 if (qp->ref == 1)
1534 return qp;
1535 qp->ref--;
1536 return isl_qpolynomial_dup(qp);
1537}
1538
1541{
1542 if (!qp)
1543 return NULL;
1544
1545 if (--qp->ref > 0)
1546 return NULL;
1547
1548 isl_space_free(qp->dim);
1549 isl_mat_free(qp->div);
1550 isl_poly_free(qp->poly);
1551
1552 free(qp);
1553 return NULL;
1554}
1555
1557{
1558 int i;
1559 isl_poly_rec *rec;
1560 isl_poly_cst *cst;
1561
1562 rec = isl_poly_alloc_rec(ctx, pos, 1 + power);
1563 if (!rec)
1564 return NULL;
1565 for (i = 0; i < 1 + power; ++i) {
1566 rec->p[i] = isl_poly_zero(ctx);
1567 if (!rec->p[i])
1568 goto error;
1569 rec->n++;
1570 }
1571 cst = isl_poly_as_cst(rec->p[power]);
1572 isl_int_set_si(cst->n, 1);
1573
1574 return &rec->poly;
1575error:
1576 isl_poly_free(&rec->poly);
1577 return NULL;
1578}
1579
1580/* r array maps original positions to new positions.
1581 */
1583{
1584 int i;
1585 isl_bool is_cst;
1586 isl_poly_rec *rec;
1587 isl_poly *base;
1588 isl_poly *res;
1589
1590 is_cst = isl_poly_is_cst(poly);
1591 if (is_cst < 0)
1592 return isl_poly_free(poly);
1593 if (is_cst)
1594 return poly;
1595
1596 rec = isl_poly_as_rec(poly);
1597 if (!rec)
1598 goto error;
1599
1600 isl_assert(poly->ctx, rec->n >= 1, goto error);
1601
1602 base = isl_poly_var_pow(poly->ctx, r[poly->var], 1);
1603 res = reorder(isl_poly_copy(rec->p[rec->n - 1]), r);
1604
1605 for (i = rec->n - 2; i >= 0; --i) {
1607 res = isl_poly_sum(res, reorder(isl_poly_copy(rec->p[i]), r));
1608 }
1609
1610 isl_poly_free(base);
1612
1613 return res;
1614error:
1616 return NULL;
1617}
1618
1620 __isl_keep isl_mat *div2)
1621{
1622 int n_row, n_col;
1624
1625 isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1626 div1->n_col >= div2->n_col,
1627 return isl_bool_error);
1628
1629 if (div1->n_row == div2->n_row)
1630 return isl_mat_is_equal(div1, div2);
1631
1632 n_row = div1->n_row;
1633 n_col = div1->n_col;
1634 div1->n_row = div2->n_row;
1635 div1->n_col = div2->n_col;
1636
1637 equal = isl_mat_is_equal(div1, div2);
1638
1639 div1->n_row = n_row;
1640 div1->n_col = n_col;
1641
1642 return equal;
1643}
1644
1645static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1646{
1647 int li, lj;
1648
1651
1652 if (li != lj)
1653 return li - lj;
1654
1655 return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1656}
1657
1662
1663static int div_sort_cmp(const void *p1, const void *p2)
1664{
1665 const struct isl_div_sort_info *i1, *i2;
1666 i1 = (const struct isl_div_sort_info *) p1;
1667 i2 = (const struct isl_div_sort_info *) p2;
1668
1669 return cmp_row(i1->div, i1->row, i2->row);
1670}
1671
1672/* Sort divs and remove duplicates.
1673 */
1675{
1676 int i;
1677 int skip;
1678 int len;
1679 struct isl_div_sort_info *array = NULL;
1680 int *pos = NULL, *at = NULL;
1681 int *reordering = NULL;
1682 isl_size div_pos;
1683
1684 if (!qp)
1685 return NULL;
1686 if (qp->div->n_row <= 1)
1687 return qp;
1688
1690 if (div_pos < 0)
1691 return isl_qpolynomial_free(qp);
1692
1693 array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1694 qp->div->n_row);
1695 pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1696 at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1697 len = qp->div->n_col - 2;
1698 reordering = isl_alloc_array(qp->div->ctx, int, len);
1699 if (!array || !pos || !at || !reordering)
1700 goto error;
1701
1702 for (i = 0; i < qp->div->n_row; ++i) {
1703 array[i].div = qp->div;
1704 array[i].row = i;
1705 pos[i] = i;
1706 at[i] = i;
1707 }
1708
1709 qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1710 div_sort_cmp);
1711
1712 for (i = 0; i < div_pos; ++i)
1713 reordering[i] = i;
1714
1715 for (i = 0; i < qp->div->n_row; ++i) {
1716 if (pos[array[i].row] == i)
1717 continue;
1718 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1719 pos[at[i]] = pos[array[i].row];
1720 at[pos[array[i].row]] = at[i];
1721 at[i] = array[i].row;
1722 pos[array[i].row] = i;
1723 }
1724
1725 skip = 0;
1726 for (i = 0; i < len - div_pos; ++i) {
1727 if (i > 0 &&
1728 isl_seq_eq(qp->div->row[i - skip - 1],
1729 qp->div->row[i - skip], qp->div->n_col)) {
1730 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1731 isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1732 2 + div_pos + i - skip);
1733 qp->div = isl_mat_drop_cols(qp->div,
1734 2 + div_pos + i - skip, 1);
1735 skip++;
1736 }
1737 reordering[div_pos + array[i].row] = div_pos + i - skip;
1738 }
1739
1740 qp->poly = reorder(qp->poly, reordering);
1741
1742 if (!qp->poly || !qp->div)
1743 goto error;
1744
1745 free(at);
1746 free(pos);
1747 free(array);
1748 free(reordering);
1749
1750 return qp;
1751error:
1752 free(at);
1753 free(pos);
1754 free(array);
1755 free(reordering);
1757 return NULL;
1758}
1759
1761 int first)
1762{
1763 int i;
1764 isl_bool is_cst;
1765 isl_poly_rec *rec;
1766
1767 is_cst = isl_poly_is_cst(poly);
1768 if (is_cst < 0)
1769 return isl_poly_free(poly);
1770 if (is_cst)
1771 return poly;
1772
1773 if (poly->var < first)
1774 return poly;
1775
1776 if (exp[poly->var - first] == poly->var - first)
1777 return poly;
1778
1780 if (!poly)
1781 goto error;
1782
1783 poly->var = exp[poly->var - first] + first;
1784
1785 rec = isl_poly_as_rec(poly);
1786 if (!rec)
1787 goto error;
1788
1789 for (i = 0; i < rec->n; ++i) {
1790 rec->p[i] = expand(rec->p[i], exp, first);
1791 if (!rec->p[i])
1792 goto error;
1793 }
1794
1795 return poly;
1796error:
1798 return NULL;
1799}
1800
1805{
1806 int *exp1 = NULL;
1807 int *exp2 = NULL;
1808 isl_mat *div = NULL;
1809 int n_div1, n_div2;
1810
1811 qp1 = isl_qpolynomial_cow(qp1);
1812 qp2 = isl_qpolynomial_cow(qp2);
1813
1814 if (!qp1 || !qp2)
1815 goto error;
1816
1817 isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1818 qp1->div->n_col >= qp2->div->n_col, goto error);
1819
1820 n_div1 = qp1->div->n_row;
1821 n_div2 = qp2->div->n_row;
1822 exp1 = isl_alloc_array(qp1->div->ctx, int, n_div1);
1823 exp2 = isl_alloc_array(qp2->div->ctx, int, n_div2);
1824 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1825 goto error;
1826
1827 div = isl_merge_divs(qp1->div, qp2->div, exp1, exp2);
1828 if (!div)
1829 goto error;
1830
1831 isl_mat_free(qp1->div);
1832 qp1->div = isl_mat_copy(div);
1833 isl_mat_free(qp2->div);
1834 qp2->div = isl_mat_copy(div);
1835
1836 qp1->poly = expand(qp1->poly, exp1, div->n_col - div->n_row - 2);
1837 qp2->poly = expand(qp2->poly, exp2, div->n_col - div->n_row - 2);
1838
1839 if (!qp1->poly || !qp2->poly)
1840 goto error;
1841
1843 free(exp1);
1844 free(exp2);
1845
1846 return fn(qp1, qp2);
1847error:
1849 free(exp1);
1850 free(exp2);
1853 return NULL;
1854}
1855
1858{
1859 isl_bool compatible;
1860 isl_poly *poly;
1861
1862 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
1863 goto error;
1864
1865 if (qp1->div->n_row < qp2->div->n_row)
1866 return isl_qpolynomial_add(qp2, qp1);
1867
1868 compatible = compatible_divs(qp1->div, qp2->div);
1869 if (compatible < 0)
1870 goto error;
1871 if (!compatible)
1872 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1873
1877
1879
1880 return qp1;
1881error:
1884 return NULL;
1885}
1886
1888 __isl_keep isl_set *dom,
1891{
1892 qp1 = isl_qpolynomial_add(qp1, qp2);
1893 qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1894 return qp1;
1895}
1896
1902
1905{
1906 isl_poly *poly;
1907
1908 if (isl_int_is_zero(v))
1909 return qp;
1910
1914
1915 return qp;
1916}
1917
1919{
1920 if (!qp)
1921 return NULL;
1922
1923 return isl_qpolynomial_mul_isl_int(qp, qp->dim->ctx->negone);
1924}
1925
1928{
1929 isl_poly *poly;
1930
1931 if (isl_int_is_one(v))
1932 return qp;
1933
1934 if (qp && isl_int_is_zero(v)) {
1935 isl_qpolynomial *zero;
1938 return zero;
1939 }
1940
1944
1945 return qp;
1946}
1947
1953
1954/* Multiply "qp" by "v".
1955 */
1958{
1959 isl_poly *poly;
1960
1961 if (!qp || !v)
1962 goto error;
1963
1964 if (!isl_val_is_rat(v))
1966 "expecting rational factor", goto error);
1967
1968 if (isl_val_is_one(v)) {
1969 isl_val_free(v);
1970 return qp;
1971 }
1972
1973 if (isl_val_is_zero(v)) {
1974 isl_space *space;
1975
1978 isl_val_free(v);
1979 return isl_qpolynomial_zero_on_domain(space);
1980 }
1981
1985
1986 isl_val_free(v);
1987 return qp;
1988error:
1989 isl_val_free(v);
1991 return NULL;
1992}
1993
1994/* Divide "qp" by "v".
1995 */
1998{
1999 if (!qp || !v)
2000 goto error;
2001
2002 if (!isl_val_is_rat(v))
2004 "expecting rational factor", goto error);
2005 if (isl_val_is_zero(v))
2007 "cannot scale down by zero", goto error);
2008
2010error:
2011 isl_val_free(v);
2013 return NULL;
2014}
2015
2018{
2019 isl_bool compatible;
2020 isl_poly *poly;
2021
2022 if (isl_qpolynomial_check_equal_space(qp1, qp2) < 0)
2023 goto error;
2024
2025 if (qp1->div->n_row < qp2->div->n_row)
2026 return isl_qpolynomial_mul(qp2, qp1);
2027
2028 compatible = compatible_divs(qp1->div, qp2->div);
2029 if (compatible < 0)
2030 goto error;
2031 if (!compatible)
2032 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
2033
2037
2039
2040 return qp1;
2041error:
2044 return NULL;
2045}
2046
2048 unsigned power)
2049{
2050 isl_poly *poly;
2051
2055
2056 return qp;
2057}
2058
2061{
2062 int i;
2063
2064 if (power == 1)
2065 return pwqp;
2066
2068 if (!pwqp)
2069 return NULL;
2070
2071 for (i = 0; i < pwqp->n; ++i) {
2072 pwqp->p[i].qp = isl_qpolynomial_pow(pwqp->p[i].qp, power);
2073 if (!pwqp->p[i].qp)
2075 }
2076
2077 return pwqp;
2078}
2079
2087
2095
2103
2111
2119
2122 isl_int v)
2123{
2124 struct isl_qpolynomial *qp;
2125 isl_poly_cst *cst;
2126
2128 if (!qp)
2129 return NULL;
2130
2131 cst = isl_poly_as_cst(qp->poly);
2132 isl_int_set(cst->n, v);
2133
2134 return qp;
2135}
2136
2138 isl_int *n, isl_int *d)
2139{
2140 isl_bool is_cst;
2141 isl_poly *poly;
2142 isl_poly_cst *cst;
2143
2145 is_cst = isl_poly_is_cst(poly);
2146 if (is_cst < 0 || !is_cst)
2147 return is_cst;
2148
2149 cst = isl_poly_as_cst(poly);
2150 if (!cst)
2151 return isl_bool_error;
2152
2153 if (n)
2154 isl_int_set(*n, cst->n);
2155 if (d)
2156 isl_int_set(*d, cst->d);
2157
2158 return isl_bool_true;
2159}
2160
2161/* Return the constant term of "poly".
2162 */
2164{
2165 isl_bool is_cst;
2166 isl_poly_cst *cst;
2167
2168 if (!poly)
2169 return NULL;
2170
2171 while ((is_cst = isl_poly_is_cst(poly)) == isl_bool_false) {
2172 isl_poly_rec *rec;
2173
2174 rec = isl_poly_as_rec(poly);
2175 if (!rec)
2176 return NULL;
2177 poly = rec->p[0];
2178 }
2179 if (is_cst < 0)
2180 return NULL;
2181
2182 cst = isl_poly_as_cst(poly);
2183 if (!cst)
2184 return NULL;
2185 return isl_val_rat_from_isl_int(cst->poly.ctx, cst->n, cst->d);
2186}
2187
2188/* Return the constant term of "qp".
2189 */
2195
2197{
2198 isl_bool is_cst;
2199 isl_poly_rec *rec;
2200
2201 if (!poly)
2202 return isl_bool_error;
2203
2204 if (poly->var < 0)
2205 return isl_bool_true;
2206
2207 rec = isl_poly_as_rec(poly);
2208 if (!rec)
2209 return isl_bool_error;
2210
2211 if (rec->n > 2)
2212 return isl_bool_false;
2213
2214 isl_assert(poly->ctx, rec->n > 1, return isl_bool_error);
2215
2216 is_cst = isl_poly_is_cst(rec->p[1]);
2217 if (is_cst < 0 || !is_cst)
2218 return is_cst;
2219
2220 return isl_poly_is_affine(rec->p[0]);
2221}
2222
2223/* Can "qp" be converted to an isl_aff?
2224 * That is, does it represent a quasi-affine expression?
2225 */
2230
2232{
2233 if (!qp)
2234 return isl_bool_error;
2235
2236 if (qp->div->n_row > 0)
2237 return isl_bool_false;
2238
2239 return isl_qpolynomial_isa_aff(qp);
2240}
2241
2243 __isl_keep isl_poly_cst *cst, int pos)
2244{
2245 isl_int gcd;
2246 isl_int f;
2247
2248 if (isl_int_is_zero(cst->n))
2249 return;
2250
2252 isl_int_init(f);
2253 isl_int_gcd(gcd, cst->d, aff->el[0]);
2254 isl_int_divexact(f, cst->d, gcd);
2255 isl_int_divexact(gcd, aff->el[0], gcd);
2256 isl_seq_scale(aff->el, aff->el, f, aff->size);
2257 isl_int_mul(aff->el[1 + pos], gcd, cst->n);
2260}
2261
2263{
2264 isl_poly_cst *cst;
2265 isl_poly_rec *rec;
2266
2267 if (!poly || !aff)
2268 return -1;
2269
2270 if (poly->var < 0) {
2271 isl_poly_cst *cst;
2272
2273 cst = isl_poly_as_cst(poly);
2274 if (!cst)
2275 return -1;
2276 update_coeff(aff, cst, 0);
2277 return 0;
2278 }
2279
2280 rec = isl_poly_as_rec(poly);
2281 if (!rec)
2282 return -1;
2283 isl_assert(poly->ctx, rec->n == 2, return -1);
2284
2285 cst = isl_poly_as_cst(rec->p[1]);
2286 if (!cst)
2287 return -1;
2288 update_coeff(aff, cst, 1 + poly->var);
2289
2290 return isl_poly_update_affine(rec->p[0], aff);
2291}
2292
2295{
2296 isl_vec *aff;
2297 isl_size d;
2298
2300 if (d < 0)
2301 return NULL;
2302
2303 aff = isl_vec_alloc(qp->div->ctx, 2 + d);
2304 if (!aff)
2305 return NULL;
2306
2307 isl_seq_clr(aff->el + 1, 1 + d);
2308 isl_int_set_si(aff->el[0], 1);
2309
2310 if (isl_poly_update_affine(qp->poly, aff) < 0)
2311 goto error;
2312
2313 return aff;
2314error:
2316 return NULL;
2317}
2318
2319/* Compare two quasi-polynomials.
2320 *
2321 * Return -1 if "qp1" is "smaller" than "qp2", 1 if "qp1" is "greater"
2322 * than "qp2" and 0 if they are equal.
2323 */
2326{
2327 int cmp;
2328
2329 if (qp1 == qp2)
2330 return 0;
2331 if (!qp1)
2332 return -1;
2333 if (!qp2)
2334 return 1;
2335
2336 cmp = isl_space_cmp(qp1->dim, qp2->dim);
2337 if (cmp != 0)
2338 return cmp;
2339
2340 cmp = isl_local_cmp(qp1->div, qp2->div);
2341 if (cmp != 0)
2342 return cmp;
2343
2344 return isl_poly_plain_cmp(qp1->poly, qp2->poly);
2345}
2346
2347/* Is "qp1" obviously equal to "qp2"?
2348 *
2349 * NaN is not equal to anything, not even to another NaN.
2350 */
2353{
2355
2356 if (!qp1 || !qp2)
2357 return isl_bool_error;
2358
2360 return isl_bool_false;
2361
2362 equal = isl_space_is_equal(qp1->dim, qp2->dim);
2363 if (equal < 0 || !equal)
2364 return equal;
2365
2366 equal = isl_mat_is_equal(qp1->div, qp2->div);
2367 if (equal < 0 || !equal)
2368 return equal;
2369
2370 return isl_poly_is_equal(qp1->poly, qp2->poly);
2371}
2372
2374{
2375 int i;
2376 isl_bool is_cst;
2377 isl_poly_rec *rec;
2378
2379 is_cst = isl_poly_is_cst(poly);
2380 if (is_cst < 0)
2381 return isl_stat_error;
2382 if (is_cst) {
2383 isl_poly_cst *cst;
2384 cst = isl_poly_as_cst(poly);
2385 if (!cst)
2386 return isl_stat_error;
2387 isl_int_lcm(*d, *d, cst->d);
2388 return isl_stat_ok;
2389 }
2390
2391 rec = isl_poly_as_rec(poly);
2392 if (!rec)
2393 return isl_stat_error;
2394
2395 for (i = 0; i < rec->n; ++i)
2396 poly_update_den(rec->p[i], d);
2397
2398 return isl_stat_ok;
2399}
2400
2402{
2403 isl_val *d;
2404
2405 if (!qp)
2406 return NULL;
2408 if (!d)
2409 return NULL;
2410 if (poly_update_den(qp->poly, &d->n) < 0)
2411 return isl_val_free(d);
2412 return d;
2413}
2414
2416 __isl_take isl_space *domain, int pos, int power)
2417{
2418 struct isl_ctx *ctx;
2419
2420 if (!domain)
2421 return NULL;
2422
2423 ctx = domain->ctx;
2424
2425 return isl_qpolynomial_alloc(domain, 0,
2426 isl_poly_var_pow(ctx, pos, power));
2427}
2428
2431{
2432 isl_size off;
2433
2435 goto error;
2436 if (isl_space_check_range(domain, type, pos, 1) < 0)
2437 goto error;
2438
2440 if (off < 0)
2441 goto error;
2442
2444error:
2446 return NULL;
2447}
2448
2450 unsigned first, unsigned n, __isl_keep isl_poly **subs)
2451{
2452 int i;
2453 isl_bool is_cst;
2454 isl_poly_rec *rec;
2455 isl_poly *base, *res;
2456
2457 is_cst = isl_poly_is_cst(poly);
2458 if (is_cst < 0)
2459 return isl_poly_free(poly);
2460 if (is_cst)
2461 return poly;
2462
2463 if (poly->var < first)
2464 return poly;
2465
2466 rec = isl_poly_as_rec(poly);
2467 if (!rec)
2468 goto error;
2469
2470 isl_assert(poly->ctx, rec->n >= 1, goto error);
2471
2472 if (poly->var >= first + n)
2473 base = isl_poly_var_pow(poly->ctx, poly->var, 1);
2474 else
2475 base = isl_poly_copy(subs[poly->var - first]);
2476
2477 res = isl_poly_subs(isl_poly_copy(rec->p[rec->n - 1]), first, n, subs);
2478 for (i = rec->n - 2; i >= 0; --i) {
2479 isl_poly *t;
2480 t = isl_poly_subs(isl_poly_copy(rec->p[i]), first, n, subs);
2482 res = isl_poly_sum(res, t);
2483 }
2484
2485 isl_poly_free(base);
2487
2488 return res;
2489error:
2491 return NULL;
2492}
2493
2495 isl_int denom, unsigned len)
2496{
2497 int i;
2498 isl_poly *poly;
2499
2500 isl_assert(ctx, len >= 1, return NULL);
2501
2502 poly = isl_poly_rat_cst(ctx, f[0], denom);
2503 for (i = 0; i < len - 1; ++i) {
2504 isl_poly *t;
2505 isl_poly *c;
2506
2507 if (isl_int_is_zero(f[1 + i]))
2508 continue;
2509
2510 c = isl_poly_rat_cst(ctx, f[1 + i], denom);
2511 t = isl_poly_var_pow(ctx, i, 1);
2512 t = isl_poly_mul(c, t);
2513 poly = isl_poly_sum(poly, t);
2514 }
2515
2516 return poly;
2517}
2518
2519/* Remove common factor of non-constant terms and denominator.
2520 */
2522{
2523 isl_ctx *ctx = qp->div->ctx;
2524 unsigned total = qp->div->n_col - 2;
2525
2526 isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
2528 ctx->normalize_gcd, qp->div->row[div][0]);
2529 if (isl_int_is_one(ctx->normalize_gcd))
2530 return;
2531
2532 isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
2533 ctx->normalize_gcd, total);
2534 isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
2535 ctx->normalize_gcd);
2536 isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
2537 ctx->normalize_gcd);
2538}
2539
2540/* Replace the integer division identified by "div" by the polynomial "s".
2541 * The integer division is assumed not to appear in the definition
2542 * of any other integer divisions.
2543 */
2546{
2547 int i;
2548 isl_size div_pos;
2549 int *reordering;
2550 isl_ctx *ctx;
2551
2552 if (!qp || !s)
2553 goto error;
2554
2555 qp = isl_qpolynomial_cow(qp);
2556 if (!qp)
2557 goto error;
2558
2560 if (div_pos < 0)
2561 goto error;
2562 qp->poly = isl_poly_subs(qp->poly, div_pos + div, 1, &s);
2563 if (!qp->poly)
2564 goto error;
2565
2566 ctx = isl_qpolynomial_get_ctx(qp);
2567 reordering = isl_alloc_array(ctx, int, div_pos + qp->div->n_row);
2568 if (!reordering)
2569 goto error;
2570 for (i = 0; i < div_pos + div; ++i)
2571 reordering[i] = i;
2572 for (i = div_pos + div + 1; i < div_pos + qp->div->n_row; ++i)
2573 reordering[i] = i - 1;
2574 qp->div = isl_mat_drop_rows(qp->div, div, 1);
2575 qp->div = isl_mat_drop_cols(qp->div, 2 + div_pos + div, 1);
2576 qp->poly = reorder(qp->poly, reordering);
2577 free(reordering);
2578
2579 if (!qp->poly || !qp->div)
2580 goto error;
2581
2582 isl_poly_free(s);
2583 return qp;
2584error:
2586 isl_poly_free(s);
2587 return NULL;
2588}
2589
2590/* Replace all integer divisions [e/d] that turn out to not actually be integer
2591 * divisions because d is equal to 1 by their definition, i.e., e.
2592 */
2595{
2596 int i, j;
2597 isl_size div_pos;
2598 isl_poly *s;
2599
2601 if (div_pos < 0)
2602 return isl_qpolynomial_free(qp);
2603
2604 for (i = 0; qp && i < qp->div->n_row; ++i) {
2605 if (!isl_int_is_one(qp->div->row[i][0]))
2606 continue;
2607 for (j = i + 1; j < qp->div->n_row; ++j) {
2608 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
2609 continue;
2610 isl_seq_combine(qp->div->row[j] + 1,
2611 qp->div->ctx->one, qp->div->row[j] + 1,
2612 qp->div->row[j][2 + div_pos + i],
2613 qp->div->row[i] + 1, 1 + div_pos + i);
2614 isl_int_set_si(qp->div->row[j][2 + div_pos + i], 0);
2615 normalize_div(qp, j);
2616 }
2617 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
2618 qp->div->row[i][0], qp->div->n_col - 1);
2619 qp = substitute_div(qp, i, s);
2620 --i;
2621 }
2622
2623 return qp;
2624}
2625
2626/* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
2627 * with d the denominator. When replacing the coefficient e of x by
2628 * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
2629 * inside the division, so we need to add floor(e/d) * x outside.
2630 * That is, we replace q by q' + floor(e/d) * x and we therefore need
2631 * to adjust the coefficient of x in each later div that depends on the
2632 * current div "div" and also in the affine expressions in the rows of "mat"
2633 * (if they too depend on "div").
2634 */
2635static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
2636 __isl_keep isl_mat **mat)
2637{
2638 int i, j;
2639 isl_int v;
2640 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2641
2642 isl_int_init(v);
2643 for (i = 0; i < 1 + total + div; ++i) {
2644 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
2645 isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
2646 continue;
2647 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
2648 isl_int_fdiv_r(qp->div->row[div][1 + i],
2649 qp->div->row[div][1 + i], qp->div->row[div][0]);
2650 *mat = isl_mat_col_addmul(*mat, i, v, 1 + total + div);
2651 for (j = div + 1; j < qp->div->n_row; ++j) {
2652 if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
2653 continue;
2654 isl_int_addmul(qp->div->row[j][1 + i],
2655 v, qp->div->row[j][2 + total + div]);
2656 }
2657 }
2658 isl_int_clear(v);
2659}
2660
2661/* Check if the last non-zero coefficient is bigger that half of the
2662 * denominator. If so, we will invert the div to further reduce the number
2663 * of distinct divs that may appear.
2664 * If the last non-zero coefficient is exactly half the denominator,
2665 * then we continue looking for earlier coefficients that are bigger
2666 * than half the denominator.
2667 */
2668static int needs_invert(__isl_keep isl_mat *div, int row)
2669{
2670 int i;
2671 int cmp;
2672
2673 for (i = div->n_col - 1; i >= 1; --i) {
2674 if (isl_int_is_zero(div->row[row][i]))
2675 continue;
2676 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
2677 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
2678 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
2679 if (cmp)
2680 return cmp > 0;
2681 if (i == 1)
2682 return 1;
2683 }
2684
2685 return 0;
2686}
2687
2688/* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
2689 * We only invert the coefficients of e (and the coefficient of q in
2690 * later divs and in the rows of "mat"). After calling this function, the
2691 * coefficients of e should be reduced again.
2692 */
2693static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
2694 __isl_keep isl_mat **mat)
2695{
2696 unsigned total = qp->div->n_col - qp->div->n_row - 2;
2697
2698 isl_seq_neg(qp->div->row[div] + 1,
2699 qp->div->row[div] + 1, qp->div->n_col - 1);
2700 isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2701 isl_int_add(qp->div->row[div][1],
2702 qp->div->row[div][1], qp->div->row[div][0]);
2703 *mat = isl_mat_col_neg(*mat, 1 + total + div);
2704 isl_mat_col_mul(qp->div, 2 + total + div,
2705 qp->div->ctx->negone, 2 + total + div);
2706}
2707
2708/* Reduce all divs of "qp" to have coefficients
2709 * in the interval [0, d-1], with d the denominator and such that the
2710 * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2711 * The modifications to the integer divisions need to be reflected
2712 * in the factors of the polynomial that refer to the original
2713 * integer divisions. To this end, the modifications are collected
2714 * as a set of affine expressions and then plugged into the polynomial.
2715 *
2716 * After the reduction, some divs may have become redundant or identical,
2717 * so we call substitute_non_divs and sort_divs. If these functions
2718 * eliminate divs or merge two or more divs into one, the coefficients
2719 * of the enclosing divs may have to be reduced again, so we call
2720 * ourselves recursively if the number of divs decreases.
2721 */
2723{
2724 int i;
2725 isl_ctx *ctx;
2726 isl_mat *mat;
2727 isl_poly **s;
2728 unsigned o_div;
2729 isl_size n_div, total, new_n_div;
2730
2734 if (total < 0 || n_div < 0)
2735 return isl_qpolynomial_free(qp);
2736 ctx = isl_qpolynomial_get_ctx(qp);
2737 mat = isl_mat_zero(ctx, n_div, 1 + total);
2738
2739 for (i = 0; i < n_div; ++i)
2740 mat = isl_mat_set_element_si(mat, i, o_div + i, 1);
2741
2742 for (i = 0; i < qp->div->n_row; ++i) {
2743 normalize_div(qp, i);
2744 reduce_div(qp, i, &mat);
2745 if (needs_invert(qp->div, i)) {
2746 invert_div(qp, i, &mat);
2747 reduce_div(qp, i, &mat);
2748 }
2749 }
2750 if (!mat)
2751 goto error;
2752
2753 s = isl_alloc_array(ctx, struct isl_poly *, n_div);
2754 if (n_div && !s)
2755 goto error;
2756 for (i = 0; i < n_div; ++i)
2757 s[i] = isl_poly_from_affine(ctx, mat->row[i], ctx->one,
2758 1 + total);
2759 qp->poly = isl_poly_subs(qp->poly, o_div - 1, n_div, s);
2760 for (i = 0; i < n_div; ++i)
2761 isl_poly_free(s[i]);
2762 free(s);
2763 if (!qp->poly)
2764 goto error;
2765
2766 isl_mat_free(mat);
2767
2768 qp = substitute_non_divs(qp);
2769 qp = sort_divs(qp);
2770 new_n_div = isl_qpolynomial_domain_dim(qp, isl_dim_div);
2771 if (new_n_div < 0)
2772 return isl_qpolynomial_free(qp);
2773 if (new_n_div < n_div)
2774 return reduce_divs(qp);
2775
2776 return qp;
2777error:
2779 isl_mat_free(mat);
2780 return NULL;
2781}
2782
2784 __isl_take isl_space *domain, const isl_int n, const isl_int d)
2785{
2786 struct isl_qpolynomial *qp;
2787 isl_poly_cst *cst;
2788
2790 if (!qp)
2791 return NULL;
2792
2793 cst = isl_poly_as_cst(qp->poly);
2794 isl_int_set(cst->n, n);
2795 isl_int_set(cst->d, d);
2796
2797 return qp;
2798}
2799
2800/* Return an isl_qpolynomial that is equal to "val" on domain space "domain".
2801 */
2804{
2805 isl_qpolynomial *qp;
2806 isl_poly_cst *cst;
2807
2809 if (!qp || !val)
2810 goto error;
2811
2812 cst = isl_poly_as_cst(qp->poly);
2813 isl_int_set(cst->n, val->n);
2814 isl_int_set(cst->d, val->d);
2815
2817 return qp;
2818error:
2821 return NULL;
2822}
2823
2825{
2826 isl_bool is_cst;
2827 isl_poly_rec *rec;
2828 int i;
2829
2830 is_cst = isl_poly_is_cst(poly);
2831 if (is_cst < 0)
2832 return isl_stat_error;
2833 if (is_cst)
2834 return isl_stat_ok;
2835
2836 if (poly->var < d)
2837 active[poly->var] = 1;
2838
2839 rec = isl_poly_as_rec(poly);
2840 for (i = 0; i < rec->n; ++i)
2841 if (poly_set_active(rec->p[i], active, d) < 0)
2842 return isl_stat_error;
2843
2844 return isl_stat_ok;
2845}
2846
2848{
2849 int i, j;
2850 isl_size d;
2851 isl_space *space;
2852
2854 d = isl_space_dim(space, isl_dim_all);
2855 if (d < 0 || !active)
2856 return isl_stat_error;
2857
2858 for (i = 0; i < d; ++i)
2859 for (j = 0; j < qp->div->n_row; ++j) {
2860 if (isl_int_is_zero(qp->div->row[j][2 + i]))
2861 continue;
2862 active[i] = 1;
2863 break;
2864 }
2865
2866 return poly_set_active(isl_qpolynomial_peek_poly(qp), active, d);
2867}
2868
2869#undef TYPE
2870#define TYPE isl_qpolynomial
2871static
2872#include "check_type_range_templ.c"
2873
2875 enum isl_dim_type type, unsigned first, unsigned n)
2876{
2877 int i;
2878 int *active = NULL;
2879 isl_bool involves = isl_bool_false;
2881 isl_size d;
2882 isl_space *space;
2883
2884 if (!qp)
2885 return isl_bool_error;
2886 if (n == 0)
2887 return isl_bool_false;
2888
2889 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
2890 return isl_bool_error;
2891 isl_assert(qp->dim->ctx, type == isl_dim_param ||
2892 type == isl_dim_in, return isl_bool_error);
2893
2895 d = isl_space_dim(space, isl_dim_all);
2896 if (d < 0)
2897 return isl_bool_error;
2898 active = isl_calloc_array(qp->dim->ctx, int, d);
2899 if (set_active(qp, active) < 0)
2900 goto error;
2901
2903 if (offset < 0)
2904 goto error;
2905 first += offset;
2906 for (i = 0; i < n; ++i)
2907 if (active[first + i]) {
2908 involves = isl_bool_true;
2909 break;
2910 }
2911
2912 free(active);
2913
2914 return involves;
2915error:
2916 free(active);
2917 return isl_bool_error;
2918}
2919
2920/* Remove divs that do not appear in the quasi-polynomial, nor in any
2921 * of the divs that do appear in the quasi-polynomial.
2922 */
2925{
2926 int i, j;
2927 isl_size div_pos;
2928 int len;
2929 int skip;
2930 int *active = NULL;
2931 int *reordering = NULL;
2932 int redundant = 0;
2933 int n_div;
2934 isl_ctx *ctx;
2935
2936 if (!qp)
2937 return NULL;
2938 if (qp->div->n_row == 0)
2939 return qp;
2940
2942 if (div_pos < 0)
2943 return isl_qpolynomial_free(qp);
2944 len = qp->div->n_col - 2;
2945 ctx = isl_qpolynomial_get_ctx(qp);
2946 active = isl_calloc_array(ctx, int, len);
2947 if (!active)
2948 goto error;
2949
2950 if (poly_set_active(isl_qpolynomial_peek_poly(qp), active, len) < 0)
2951 goto error;
2952
2953 for (i = qp->div->n_row - 1; i >= 0; --i) {
2954 if (!active[div_pos + i]) {
2955 redundant = 1;
2956 continue;
2957 }
2958 for (j = 0; j < i; ++j) {
2959 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + j]))
2960 continue;
2961 active[div_pos + j] = 1;
2962 break;
2963 }
2964 }
2965
2966 if (!redundant) {
2967 free(active);
2968 return qp;
2969 }
2970
2971 reordering = isl_alloc_array(qp->div->ctx, int, len);
2972 if (!reordering)
2973 goto error;
2974
2975 for (i = 0; i < div_pos; ++i)
2976 reordering[i] = i;
2977
2978 skip = 0;
2979 n_div = qp->div->n_row;
2980 for (i = 0; i < n_div; ++i) {
2981 if (!active[div_pos + i]) {
2982 qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
2983 qp->div = isl_mat_drop_cols(qp->div,
2984 2 + div_pos + i - skip, 1);
2985 skip++;
2986 }
2987 reordering[div_pos + i] = div_pos + i - skip;
2988 }
2989
2990 qp->poly = reorder(qp->poly, reordering);
2991
2992 if (!qp->poly || !qp->div)
2993 goto error;
2994
2995 free(active);
2996 free(reordering);
2997
2998 return qp;
2999error:
3000 free(active);
3001 free(reordering);
3003 return NULL;
3004}
3005
3007 unsigned first, unsigned n)
3008{
3009 int i;
3010 isl_poly_rec *rec;
3011
3012 if (!poly)
3013 return NULL;
3014 if (n == 0 || poly->var < 0 || poly->var < first)
3015 return poly;
3016 if (poly->var < first + n) {
3018 return isl_poly_drop(poly, first, n);
3019 }
3021 if (!poly)
3022 return NULL;
3023 poly->var -= n;
3024 rec = isl_poly_as_rec(poly);
3025 if (!rec)
3026 goto error;
3027
3028 for (i = 0; i < rec->n; ++i) {
3029 rec->p[i] = isl_poly_drop(rec->p[i], first, n);
3030 if (!rec->p[i])
3031 goto error;
3032 }
3033
3034 return poly;
3035error:
3037 return NULL;
3038}
3039
3042 enum isl_dim_type type, unsigned pos, const char *s)
3043{
3044 isl_space *space;
3045
3046 if (!qp)
3047 return NULL;
3048 if (type == isl_dim_out)
3050 "cannot set name of output/set dimension",
3051 return isl_qpolynomial_free(qp));
3054 space = isl_space_set_dim_name(space, type, pos, s);
3056 return qp;
3057}
3058
3061 enum isl_dim_type type, unsigned first, unsigned n)
3062{
3063 isl_space *space;
3065
3066 if (!qp)
3067 return NULL;
3068 if (type == isl_dim_out)
3069 isl_die(qp->dim->ctx, isl_error_invalid,
3070 "cannot drop output/set dimension",
3071 goto error);
3072 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3073 return isl_qpolynomial_free(qp);
3075 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3076 return qp;
3077
3078
3079 isl_assert(qp->dim->ctx, type == isl_dim_param ||
3080 type == isl_dim_set, goto error);
3081
3083 space = isl_space_drop_dims(space, type, first, n);
3085
3086 qp = isl_qpolynomial_cow(qp);
3087 if (!qp)
3088 return NULL;
3089
3091 if (offset < 0)
3092 goto error;
3093 first += offset;
3094
3095 qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
3096 if (!qp->div)
3097 goto error;
3098
3099 qp->poly = isl_poly_drop(qp->poly, first, n);
3100 if (!qp->poly)
3101 goto error;
3102
3103 return qp;
3104error:
3106 return NULL;
3107}
3108
3109/* Project the domain of the quasi-polynomial onto its parameter space.
3110 * The quasi-polynomial may not involve any of the domain dimensions.
3111 */
3114{
3115 isl_space *space;
3116 isl_size n;
3117 isl_bool involves;
3118
3120 if (n < 0)
3121 return isl_qpolynomial_free(qp);
3122 involves = isl_qpolynomial_involves_dims(qp, isl_dim_in, 0, n);
3123 if (involves < 0)
3124 return isl_qpolynomial_free(qp);
3125 if (involves)
3127 "polynomial involves some of the domain dimensions",
3128 return isl_qpolynomial_free(qp));
3131 space = isl_space_params(space);
3132 qp = isl_qpolynomial_reset_domain_space(qp, space);
3133 return qp;
3134}
3135
3138{
3139 int i, j, k;
3140 isl_int denom;
3141 unsigned total;
3142 unsigned n_div;
3143 isl_poly *poly;
3144
3145 if (!eq)
3146 goto error;
3147 if (eq->n_eq == 0) {
3149 return qp;
3150 }
3151
3152 qp = isl_qpolynomial_cow(qp);
3153 if (!qp)
3154 goto error;
3155 qp->div = isl_mat_cow(qp->div);
3156 if (!qp->div)
3157 goto error;
3158
3160 n_div = eq->n_div;
3161 isl_int_init(denom);
3162 for (i = 0; i < eq->n_eq; ++i) {
3163 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
3164 if (j < 0 || j == 0 || j >= total)
3165 continue;
3166
3167 for (k = 0; k < qp->div->n_row; ++k) {
3168 if (isl_int_is_zero(qp->div->row[k][1 + j]))
3169 continue;
3170 isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
3171 &qp->div->row[k][0]);
3172 normalize_div(qp, k);
3173 }
3174
3175 if (isl_int_is_pos(eq->eq[i][j]))
3176 isl_seq_neg(eq->eq[i], eq->eq[i], total);
3177 isl_int_abs(denom, eq->eq[i][j]);
3178 isl_int_set_si(eq->eq[i][j], 0);
3179
3180 poly = isl_poly_from_affine(qp->dim->ctx,
3181 eq->eq[i], denom, total);
3182 qp->poly = isl_poly_subs(qp->poly, j - 1, 1, &poly);
3184 }
3185 isl_int_clear(denom);
3186
3187 if (!qp->poly)
3188 goto error;
3189
3191
3192 qp = substitute_non_divs(qp);
3193 qp = sort_divs(qp);
3194
3195 return qp;
3196error:
3199 return NULL;
3200}
3201
3202/* Exploit the equalities in "eq" to simplify the quasi-polynomial.
3203 */
3206{
3207 if (!qp || !eq)
3208 goto error;
3209 if (qp->div->n_row > 0)
3210 eq = isl_basic_set_add_dims(eq, isl_dim_set, qp->div->n_row);
3212error:
3215 return NULL;
3216}
3217
3218/* Look for equalities among the variables shared by context and qp
3219 * and the integer divisions of qp, if any.
3220 * The equalities are then used to eliminate variables and/or integer
3221 * divisions from qp.
3222 */
3235
3238{
3240 isl_set *dom_context = isl_set_universe(space);
3241 dom_context = isl_set_intersect_params(dom_context, context);
3242 return isl_qpolynomial_gist(qp, dom_context);
3243}
3244
3245/* Return a zero isl_qpolynomial in the given space.
3246 *
3247 * This is a helper function for isl_pw_*_as_* that ensures a uniform
3248 * interface over all piecewise types.
3249 */
3255
3256#define isl_qpolynomial_involves_nan isl_qpolynomial_is_nan
3257
3258#undef PW
3259#define PW isl_pw_qpolynomial
3260#undef BASE
3261#define BASE qpolynomial
3262#undef EL_IS_ZERO
3263#define EL_IS_ZERO is_zero
3264#undef ZERO
3265#define ZERO zero
3266#undef IS_ZERO
3267#define IS_ZERO is_zero
3268#undef FIELD
3269#define FIELD qp
3270#undef DEFAULT_IS_ZERO
3271#define DEFAULT_IS_ZERO 1
3272
3273#include <isl_pw_templ.c>
3274#include <isl_pw_un_op_templ.c>
3277#include <isl_pw_eval.c>
3278#include <isl_pw_fix_templ.c>
3281#include <isl_pw_lift_templ.c>
3282#include <isl_pw_morph_templ.c>
3283#include <isl_pw_move_dims_templ.c>
3284#include <isl_pw_neg_templ.c>
3285#include <isl_pw_opt_templ.c>
3287#include <isl_pw_sub_templ.c>
3288
3289#undef BASE
3290#define BASE pw_qpolynomial
3291
3292#include <isl_union_single.c>
3294#include <isl_union_eval.c>
3295#include <isl_union_neg.c>
3296#include <isl_union_sub_templ.c>
3297
3299{
3300 if (!pwqp)
3301 return -1;
3302
3303 if (pwqp->n != -1)
3304 return 0;
3305
3306 if (!isl_set_plain_is_universe(pwqp->p[0].set))
3307 return 0;
3308
3309 return isl_qpolynomial_is_one(pwqp->p[0].qp);
3310}
3311
3315{
3316 return isl_pw_qpolynomial_union_add_(pwqp1, pwqp2);
3317}
3318
3322{
3323 int i, j, n;
3324 struct isl_pw_qpolynomial *res;
3325
3326 if (!pwqp1 || !pwqp2)
3327 goto error;
3328
3329 isl_assert(pwqp1->dim->ctx, isl_space_is_equal(pwqp1->dim, pwqp2->dim),
3330 goto error);
3331
3332 if (isl_pw_qpolynomial_is_zero(pwqp1)) {
3334 return pwqp1;
3335 }
3336
3337 if (isl_pw_qpolynomial_is_zero(pwqp2)) {
3339 return pwqp2;
3340 }
3341
3342 if (isl_pw_qpolynomial_is_one(pwqp1)) {
3344 return pwqp2;
3345 }
3346
3347 if (isl_pw_qpolynomial_is_one(pwqp2)) {
3349 return pwqp1;
3350 }
3351
3352 n = pwqp1->n * pwqp2->n;
3353 res = isl_pw_qpolynomial_alloc_size(isl_space_copy(pwqp1->dim), n);
3354
3355 for (i = 0; i < pwqp1->n; ++i) {
3356 for (j = 0; j < pwqp2->n; ++j) {
3357 struct isl_set *common;
3358 struct isl_qpolynomial *prod;
3359 common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
3360 isl_set_copy(pwqp2->p[j].set));
3361 if (isl_set_plain_is_empty(common)) {
3362 isl_set_free(common);
3363 continue;
3364 }
3365
3366 prod = isl_qpolynomial_mul(
3367 isl_qpolynomial_copy(pwqp1->p[i].qp),
3368 isl_qpolynomial_copy(pwqp2->p[j].qp));
3369
3370 res = isl_pw_qpolynomial_add_piece(res, common, prod);
3371 }
3372 }
3373
3376
3377 return res;
3378error:
3381 return NULL;
3382}
3383
3385 __isl_take isl_vec *vec)
3386{
3387 int i;
3388 isl_bool is_cst;
3389 isl_poly_rec *rec;
3390 isl_val *res;
3391 isl_val *base;
3392
3393 is_cst = isl_poly_is_cst(poly);
3394 if (is_cst < 0)
3395 goto error;
3396 if (is_cst) {
3397 isl_vec_free(vec);
3400 return res;
3401 }
3402
3403 rec = isl_poly_as_rec(poly);
3404 if (!rec || !vec)
3405 goto error;
3406
3407 isl_assert(poly->ctx, rec->n >= 1, goto error);
3408
3409 base = isl_val_rat_from_isl_int(poly->ctx,
3410 vec->el[1 + poly->var], vec->el[0]);
3411
3412 res = isl_poly_eval(isl_poly_copy(rec->p[rec->n - 1]),
3413 isl_vec_copy(vec));
3414
3415 for (i = rec->n - 2; i >= 0; --i) {
3416 res = isl_val_mul(res, isl_val_copy(base));
3418 isl_vec_copy(vec)));
3419 }
3420
3421 isl_val_free(base);
3423 isl_vec_free(vec);
3424 return res;
3425error:
3427 isl_vec_free(vec);
3428 return NULL;
3429}
3430
3431/* Evaluate "qp" in the void point "pnt".
3432 * In particular, return the value NaN.
3433 */
3435 __isl_take isl_point *pnt)
3436{
3437 isl_ctx *ctx;
3438
3439 ctx = isl_point_get_ctx(pnt);
3441 isl_point_free(pnt);
3442 return isl_val_nan(ctx);
3443}
3444
3446 __isl_take isl_point *pnt)
3447{
3448 isl_bool is_void;
3449 isl_vec *ext;
3450 isl_val *v;
3451
3452 if (!qp || !pnt)
3453 goto error;
3454 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, qp->dim), goto error);
3455 is_void = isl_point_is_void(pnt);
3456 if (is_void < 0)
3457 goto error;
3458 if (is_void)
3459 return eval_void(qp, pnt);
3460
3461 ext = isl_local_extend_point_vec(qp->div, isl_vec_copy(pnt->vec));
3462
3464
3466 isl_point_free(pnt);
3467
3468 return v;
3469error:
3471 isl_point_free(pnt);
3472 return NULL;
3473}
3474
3476{
3477 int cmp;
3478 isl_int t;
3479 isl_int_init(t);
3480 isl_int_mul(t, cst1->n, cst2->d);
3481 isl_int_submul(t, cst2->n, cst1->d);
3482 cmp = isl_int_sgn(t);
3484 return cmp;
3485}
3486
3489 unsigned first, unsigned n)
3490{
3491 unsigned total;
3492 unsigned g_pos;
3493 int *exp;
3494 isl_space *space;
3495
3496 if (!qp)
3497 return NULL;
3498 if (type == isl_dim_out)
3499 isl_die(qp->div->ctx, isl_error_invalid,
3500 "cannot insert output/set dimensions",
3501 goto error);
3502 if (isl_qpolynomial_check_range(qp, type, first, 0) < 0)
3503 return isl_qpolynomial_free(qp);
3505 if (n == 0 && !isl_space_is_named_or_nested(qp->dim, type))
3506 return qp;
3507
3508 qp = isl_qpolynomial_cow(qp);
3509 if (!qp)
3510 return NULL;
3511
3512 g_pos = pos(qp->dim, type) + first;
3513
3514 qp->div = isl_mat_insert_zero_cols(qp->div, 2 + g_pos, n);
3515 if (!qp->div)
3516 goto error;
3517
3518 total = qp->div->n_col - 2;
3519 if (total > g_pos) {
3520 int i;
3521 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
3522 if (!exp)
3523 goto error;
3524 for (i = 0; i < total - g_pos; ++i)
3525 exp[i] = i + n;
3526 qp->poly = expand(qp->poly, exp, g_pos);
3527 free(exp);
3528 if (!qp->poly)
3529 goto error;
3530 }
3531
3533 space = isl_space_insert_dims(space, type, first, n);
3535
3536 return qp;
3537error:
3539 return NULL;
3540}
3541
3543 __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
3544{
3545 isl_size pos;
3546
3548 if (pos < 0)
3549 return isl_qpolynomial_free(qp);
3550
3551 return isl_qpolynomial_insert_dims(qp, type, pos, n);
3552}
3553
3554static int *reordering_move(isl_ctx *ctx,
3555 unsigned len, unsigned dst, unsigned src, unsigned n)
3556{
3557 int i;
3558 int *reordering;
3559
3560 reordering = isl_alloc_array(ctx, int, len);
3561 if (!reordering)
3562 return NULL;
3563
3564 if (dst <= src) {
3565 for (i = 0; i < dst; ++i)
3566 reordering[i] = i;
3567 for (i = 0; i < n; ++i)
3568 reordering[src + i] = dst + i;
3569 for (i = 0; i < src - dst; ++i)
3570 reordering[dst + i] = dst + n + i;
3571 for (i = 0; i < len - src - n; ++i)
3572 reordering[src + n + i] = src + n + i;
3573 } else {
3574 for (i = 0; i < src; ++i)
3575 reordering[i] = i;
3576 for (i = 0; i < n; ++i)
3577 reordering[src + i] = dst + i;
3578 for (i = 0; i < dst - src; ++i)
3579 reordering[src + n + i] = src + i;
3580 for (i = 0; i < len - dst - n; ++i)
3581 reordering[dst + n + i] = dst + n + i;
3582 }
3583
3584 return reordering;
3585}
3586
3587/* Move the "n" variables starting at "src_pos" of "qp" to "dst_pos".
3588 * Only modify the polynomial expression and the local variables of "qp".
3589 * The caller is responsible for modifying the space accordingly.
3590 */
3593 unsigned dst_pos, unsigned src_pos, unsigned n)
3594{
3595 isl_ctx *ctx;
3597 int *reordering;
3598 isl_local *local;
3599 isl_poly *poly;
3600
3601 local = isl_qpolynomial_take_local(qp);
3602 local = isl_local_move_vars(local, dst_pos, src_pos, n);
3603 qp = isl_qpolynomial_restore_local(qp, local);
3604 qp = sort_divs(qp);
3605
3607 if (total < 0)
3608 return isl_qpolynomial_free(qp);
3609 ctx = isl_qpolynomial_get_ctx(qp);
3610 reordering = reordering_move(ctx, total, dst_pos, src_pos, n);
3611 if (!reordering)
3612 return isl_qpolynomial_free(qp);
3613
3615 poly = reorder(poly, reordering);
3617 free(reordering);
3618
3619 return qp;
3620}
3621
3624 enum isl_dim_type dst_type, unsigned dst_pos,
3625 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3626{
3627 isl_ctx *ctx;
3628 unsigned g_dst_pos;
3629 unsigned g_src_pos;
3630 isl_size src_off, dst_off;
3631 isl_space *space;
3632
3633 if (!qp)
3634 return NULL;
3635
3636 ctx = isl_qpolynomial_get_ctx(qp);
3637 if (dst_type == isl_dim_out || src_type == isl_dim_out)
3639 "cannot move output/set dimension",
3640 return isl_qpolynomial_free(qp));
3641 if (src_type == isl_dim_div || dst_type == isl_dim_div)
3642 isl_die(ctx, isl_error_invalid, "cannot move local variables",
3643 return isl_qpolynomial_free(qp));
3644 if (isl_qpolynomial_check_range(qp, src_type, src_pos, n) < 0)
3645 return isl_qpolynomial_free(qp);
3646 if (dst_type == isl_dim_in)
3647 dst_type = isl_dim_set;
3648 if (src_type == isl_dim_in)
3649 src_type = isl_dim_set;
3650
3651 if (n == 0 &&
3652 !isl_space_is_named_or_nested(qp->dim, src_type) &&
3653 !isl_space_is_named_or_nested(qp->dim, dst_type))
3654 return qp;
3655
3656 src_off = isl_qpolynomial_domain_var_offset(qp, src_type);
3657 dst_off = isl_qpolynomial_domain_var_offset(qp, dst_type);
3658 if (src_off < 0 || dst_off < 0)
3659 return isl_qpolynomial_free(qp);
3660
3661 g_dst_pos = dst_off + dst_pos;
3662 g_src_pos = src_off + src_pos;
3663 if (dst_type > src_type)
3664 g_dst_pos -= n;
3665
3666 qp = local_poly_move_dims(qp, g_dst_pos, g_src_pos, n);
3667
3669 space = isl_space_move_dims(space, dst_type, dst_pos,
3670 src_type, src_pos, n);
3672
3673 return qp;
3674}
3675
3676/* Given a quasi-polynomial on a domain (A -> B),
3677 * interchange A and B in the wrapped domain
3678 * to obtain a quasi-polynomial on the domain (B -> A).
3679 */
3682{
3683 isl_space *space;
3684 isl_size n_in, n_out, offset;
3685
3690 if (offset < 0 || n_in < 0 || n_out < 0)
3691 return isl_qpolynomial_free(qp);
3692
3693 qp = local_poly_move_dims(qp, offset, offset + n_in, n_out);
3694
3696 space = isl_space_wrapped_reverse(space);
3698
3699 return qp;
3700}
3701
3703 __isl_take isl_space *space, isl_int *f, isl_int denom)
3704{
3705 isl_size d;
3706 isl_poly *poly;
3707
3708 space = isl_space_domain(space);
3709 if (!space)
3710 return NULL;
3711
3712 d = isl_space_dim(space, isl_dim_all);
3713 poly = d < 0 ? NULL : isl_poly_from_affine(space->ctx, f, denom, 1 + d);
3714
3715 return isl_qpolynomial_alloc(space, 0, poly);
3716}
3717
3719{
3720 isl_ctx *ctx;
3721 isl_poly *poly;
3722 isl_qpolynomial *qp;
3723
3724 if (!aff)
3725 return NULL;
3726
3727 ctx = isl_aff_get_ctx(aff);
3728 poly = isl_poly_from_affine(ctx, aff->v->el + 1, aff->v->el[0],
3729 aff->v->size - 1);
3730
3732 aff->ls->div->n_row, poly);
3733 if (!qp)
3734 goto error;
3735
3736 isl_mat_free(qp->div);
3737 qp->div = isl_mat_copy(aff->ls->div);
3738 qp->div = isl_mat_cow(qp->div);
3739 if (!qp->div)
3740 goto error;
3741
3743 qp = reduce_divs(qp);
3744 qp = remove_redundant_divs(qp);
3745 return qp;
3746error:
3748 return isl_qpolynomial_free(qp);
3749}
3750
3752 __isl_take isl_pw_aff *pwaff)
3753{
3754 int i;
3756
3757 if (!pwaff)
3758 return NULL;
3759
3760 pwqp = isl_pw_qpolynomial_alloc_size(isl_pw_aff_get_space(pwaff),
3761 pwaff->n);
3762
3763 for (i = 0; i < pwaff->n; ++i) {
3764 isl_set *dom;
3765 isl_qpolynomial *qp;
3766
3767 dom = isl_set_copy(pwaff->p[i].set);
3768 qp = isl_qpolynomial_from_aff(isl_aff_copy(pwaff->p[i].aff));
3770 }
3771
3772 isl_pw_aff_free(pwaff);
3773 return pwqp;
3774}
3775
3785
3786/* For each 0 <= i < "n", replace variable "first" + i of type "type"
3787 * in "qp" by subs[i].
3788 */
3791 enum isl_dim_type type, unsigned first, unsigned n,
3793{
3794 int i;
3795 isl_poly *poly;
3796 isl_poly **polys;
3797
3798 if (n == 0)
3799 return qp;
3800
3801 if (!qp)
3802 return NULL;
3803
3804 if (type == isl_dim_out)
3805 isl_die(qp->dim->ctx, isl_error_invalid,
3806 "cannot substitute output/set dimension",
3807 goto error);
3808 if (isl_qpolynomial_check_range(qp, type, first, n) < 0)
3809 return isl_qpolynomial_free(qp);
3811
3812 for (i = 0; i < n; ++i)
3813 if (!subs[i])
3814 goto error;
3815
3816 for (i = 0; i < n; ++i)
3817 if (isl_qpolynomial_check_equal_space(qp, subs[i]) < 0)
3818 goto error;
3819
3820 isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
3821 for (i = 0; i < n; ++i)
3822 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
3823
3824 first += pos(qp->dim, type);
3825
3826 polys = isl_alloc_array(qp->dim->ctx, struct isl_poly *, n);
3827 if (!polys)
3828 goto error;
3829 for (i = 0; i < n; ++i)
3830 polys[i] = subs[i]->poly;
3831
3833 poly = isl_poly_subs(poly, first, n, polys);
3835
3836 free(polys);
3837
3838 return qp;
3839error:
3841 return NULL;
3842}
3843
3844/* Extend "bset" with extra set dimensions for each integer division
3845 * in "qp" and then call "fn" with the extended bset and the polynomial
3846 * that results from replacing each of the integer divisions by the
3847 * corresponding extra set dimension.
3848 */
3852 __isl_take isl_qpolynomial *poly, void *user), void *user)
3853{
3854 isl_space *space;
3855 isl_local_space *ls;
3856 isl_poly *poly;
3857 isl_qpolynomial *polynomial;
3858
3859 if (!qp || !bset)
3860 return isl_stat_error;
3861 if (qp->div->n_row == 0)
3862 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3863 user);
3864
3865 space = isl_space_copy(qp->dim);
3866 space = isl_space_add_dims(space, isl_dim_set, qp->div->n_row);
3868 polynomial = isl_qpolynomial_alloc(space, 0, poly);
3869 bset = isl_basic_set_copy(bset);
3871 bset = isl_local_space_lift_basic_set(ls, bset);
3872
3873 return fn(bset, polynomial, user);
3874}
3875
3876/* Return total degree in variables first (inclusive) up to last (exclusive).
3877 */
3878int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
3879{
3880 int deg = -1;
3881 int i;
3882 isl_bool is_zero, is_cst;
3883 isl_poly_rec *rec;
3884
3885 is_zero = isl_poly_is_zero(poly);
3886 if (is_zero < 0)
3887 return -2;
3888 if (is_zero)
3889 return -1;
3890 is_cst = isl_poly_is_cst(poly);
3891 if (is_cst < 0)
3892 return -2;
3893 if (is_cst || poly->var < first)
3894 return 0;
3895
3896 rec = isl_poly_as_rec(poly);
3897 if (!rec)
3898 return -2;
3899
3900 for (i = 0; i < rec->n; ++i) {
3901 int d;
3902
3903 is_zero = isl_poly_is_zero(rec->p[i]);
3904 if (is_zero < 0)
3905 return -2;
3906 if (is_zero)
3907 continue;
3908 d = isl_poly_degree(rec->p[i], first, last);
3909 if (poly->var < last)
3910 d += i;
3911 if (d > deg)
3912 deg = d;
3913 }
3914
3915 return deg;
3916}
3917
3918/* Return total degree in set variables.
3919 */
3921{
3922 isl_size ovar;
3923 isl_size nvar;
3924
3925 if (!poly)
3926 return -2;
3927
3928 ovar = isl_space_offset(poly->dim, isl_dim_set);
3929 nvar = isl_space_dim(poly->dim, isl_dim_set);
3930 if (ovar < 0 || nvar < 0)
3931 return -2;
3932 return isl_poly_degree(poly->poly, ovar, ovar + nvar);
3933}
3934
3936 unsigned pos, int deg)
3937{
3938 int i;
3939 isl_bool is_cst;
3940 isl_poly_rec *rec;
3941
3942 is_cst = isl_poly_is_cst(poly);
3943 if (is_cst < 0)
3944 return NULL;
3945 if (is_cst || poly->var < pos) {
3946 if (deg == 0)
3947 return isl_poly_copy(poly);
3948 else
3949 return isl_poly_zero(poly->ctx);
3950 }
3951
3952 rec = isl_poly_as_rec(poly);
3953 if (!rec)
3954 return NULL;
3955
3956 if (poly->var == pos) {
3957 if (deg < rec->n)
3958 return isl_poly_copy(rec->p[deg]);
3959 else
3960 return isl_poly_zero(poly->ctx);
3961 }
3962
3965 rec = isl_poly_as_rec(poly);
3966 if (!rec)
3967 goto error;
3968
3969 for (i = 0; i < rec->n; ++i) {
3970 isl_poly *t;
3971 t = isl_poly_coeff(rec->p[i], pos, deg);
3972 if (!t)
3973 goto error;
3974 isl_poly_free(rec->p[i]);
3975 rec->p[i] = t;
3976 }
3977
3978 return poly;
3979error:
3981 return NULL;
3982}
3983
3984/* Return coefficient of power "deg" of variable "t_pos" of type "type".
3985 */
3988 enum isl_dim_type type, unsigned t_pos, int deg)
3989{
3990 unsigned g_pos;
3991 isl_poly *poly;
3992 isl_qpolynomial *c;
3993
3994 if (!qp)
3995 return NULL;
3996
3997 if (type == isl_dim_out)
3998 isl_die(qp->div->ctx, isl_error_invalid,
3999 "output/set dimension does not have a coefficient",
4000 return NULL);
4001 if (isl_qpolynomial_check_range(qp, type, t_pos, 1) < 0)
4002 return NULL;
4004
4005 g_pos = pos(qp->dim, type) + t_pos;
4007
4009 qp->div->n_row, poly);
4010 if (!c)
4011 return NULL;
4012 isl_mat_free(c->div);
4014 if (!c->div)
4015 goto error;
4016 return c;
4017error:
4019 return NULL;
4020}
4021
4022/* Homogenize the polynomial in the variables first (inclusive) up to
4023 * last (exclusive) by inserting powers of variable first.
4024 * Variable first is assumed not to appear in the input.
4025 */
4027 int target, int first, int last)
4028{
4029 int i;
4030 isl_bool is_zero, is_cst;
4031 isl_poly_rec *rec;
4032
4033 is_zero = isl_poly_is_zero(poly);
4034 if (is_zero < 0)
4035 return isl_poly_free(poly);
4036 if (is_zero)
4037 return poly;
4038 if (deg == target)
4039 return poly;
4040 is_cst = isl_poly_is_cst(poly);
4041 if (is_cst < 0)
4042 return isl_poly_free(poly);
4043 if (is_cst || poly->var < first) {
4044 isl_poly *hom;
4045
4046 hom = isl_poly_var_pow(poly->ctx, first, target - deg);
4047 if (!hom)
4048 goto error;
4049 rec = isl_poly_as_rec(hom);
4050 rec->p[target - deg] = isl_poly_mul(rec->p[target - deg], poly);
4051
4052 return hom;
4053 }
4054
4056 rec = isl_poly_as_rec(poly);
4057 if (!rec)
4058 goto error;
4059
4060 for (i = 0; i < rec->n; ++i) {
4061 is_zero = isl_poly_is_zero(rec->p[i]);
4062 if (is_zero < 0)
4063 return isl_poly_free(poly);
4064 if (is_zero)
4065 continue;
4066 rec->p[i] = isl_poly_homogenize(rec->p[i],
4067 poly->var < last ? deg + i : i, target,
4068 first, last);
4069 if (!rec->p[i])
4070 goto error;
4071 }
4072
4073 return poly;
4074error:
4076 return NULL;
4077}
4078
4079/* Homogenize the polynomial in the set variables by introducing
4080 * powers of an extra set variable at position 0.
4081 */
4084{
4085 isl_size ovar;
4086 isl_size nvar;
4087 int deg = isl_qpolynomial_degree(poly);
4088
4089 if (deg < -1)
4090 goto error;
4091
4094 if (!poly)
4095 goto error;
4096
4097 ovar = isl_space_offset(poly->dim, isl_dim_set);
4098 nvar = isl_space_dim(poly->dim, isl_dim_set);
4099 if (ovar < 0 || nvar < 0)
4100 return isl_qpolynomial_free(poly);
4101 poly->poly = isl_poly_homogenize(poly->poly, 0, deg, ovar, ovar + nvar);
4102 if (!poly->poly)
4103 goto error;
4104
4105 return poly;
4106error:
4108 return NULL;
4109}
4110
4113{
4114 isl_term *term;
4115 isl_size d;
4116 int n;
4117
4118 d = isl_space_dim(space, isl_dim_all);
4119 if (d < 0 || !div)
4120 goto error;
4121
4122 n = d + div->n_row;
4123
4124 term = isl_calloc(space->ctx, struct isl_term,
4125 sizeof(struct isl_term) + (n - 1) * sizeof(int));
4126 if (!term)
4127 goto error;
4128
4129 term->ref = 1;
4130 term->dim = space;
4131 term->div = div;
4132 isl_int_init(term->n);
4133 isl_int_init(term->d);
4134
4135 return term;
4136error:
4137 isl_space_free(space);
4139 return NULL;
4140}
4141
4143{
4144 if (!term)
4145 return NULL;
4146
4147 term->ref++;
4148 return term;
4149}
4150
4152{
4153 int i;
4154 isl_term *dup;
4156
4158 if (total < 0)
4159 return NULL;
4160
4161 dup = isl_term_alloc(isl_space_copy(term->dim), isl_mat_copy(term->div));
4162 if (!dup)
4163 return NULL;
4164
4165 isl_int_set(dup->n, term->n);
4166 isl_int_set(dup->d, term->d);
4167
4168 for (i = 0; i < total; ++i)
4169 dup->pow[i] = term->pow[i];
4170
4171 return dup;
4172}
4173
4175{
4176 if (!term)
4177 return NULL;
4178
4179 if (term->ref == 1)
4180 return term;
4181 term->ref--;
4182 return isl_term_dup(term);
4183}
4184
4186{
4187 if (!term)
4188 return NULL;
4189
4190 if (--term->ref > 0)
4191 return NULL;
4192
4193 isl_space_free(term->dim);
4194 isl_mat_free(term->div);
4195 isl_int_clear(term->n);
4196 isl_int_clear(term->d);
4197 free(term);
4198
4199 return NULL;
4200}
4201
4203{
4204 isl_size dim;
4205
4206 if (!term)
4207 return isl_size_error;
4208
4209 switch (type) {
4210 case isl_dim_param:
4211 case isl_dim_in:
4212 case isl_dim_out: return isl_space_dim(term->dim, type);
4213 case isl_dim_div: return term->div->n_row;
4214 case isl_dim_all: dim = isl_space_dim(term->dim, isl_dim_all);
4215 if (dim < 0)
4216 return isl_size_error;
4217 return dim + term->div->n_row;
4218 default: return isl_size_error;
4219 }
4220}
4221
4222/* Return the space of "term".
4223 */
4225{
4226 return term ? term->dim : NULL;
4227}
4228
4229/* Return the offset of the first variable of type "type" within
4230 * the variables of "term".
4231 */
4233 enum isl_dim_type type)
4234{
4235 isl_space *space;
4236
4237 space = isl_term_peek_space(term);
4238 if (!space)
4239 return isl_size_error;
4240
4241 switch (type) {
4242 case isl_dim_param:
4243 case isl_dim_set: return isl_space_offset(space, type);
4244 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
4245 default:
4247 "invalid dimension type", return isl_size_error);
4248 }
4249}
4250
4252{
4253 return term ? term->dim->ctx : NULL;
4254}
4255
4257{
4258 if (!term)
4259 return;
4260 isl_int_set(*n, term->n);
4261}
4262
4263/* Return the coefficient of the term "term".
4264 */
4266{
4267 if (!term)
4268 return NULL;
4269
4271 term->n, term->d);
4272}
4273
4274#undef TYPE
4275#define TYPE isl_term
4276static
4277#include "check_type_range_templ.c"
4278
4280 enum isl_dim_type type, unsigned pos)
4281{
4283
4284 if (isl_term_check_range(term, type, pos, 1) < 0)
4285 return isl_size_error;
4286 offset = isl_term_offset(term, type);
4287 if (offset < 0)
4288 return isl_size_error;
4289
4290 return term->pow[offset + pos];
4291}
4292
4294{
4295 isl_local_space *ls;
4296 isl_aff *aff;
4297
4298 if (isl_term_check_range(term, isl_dim_div, pos, 1) < 0)
4299 return NULL;
4300
4302 isl_mat_copy(term->div));
4303 aff = isl_aff_alloc(ls);
4304 if (!aff)
4305 return NULL;
4306
4307 isl_seq_cpy(aff->v->el, term->div->row[pos], aff->v->size);
4308
4310
4311 return aff;
4312}
4313
4315 isl_stat (*fn)(__isl_take isl_term *term, void *user),
4316 __isl_take isl_term *term, void *user)
4317{
4318 int i;
4319 isl_bool is_zero, is_bad, is_cst;
4320 isl_poly_rec *rec;
4321
4322 is_zero = isl_poly_is_zero(poly);
4323 if (is_zero < 0 || !term)
4324 goto error;
4325
4326 if (is_zero)
4327 return term;
4328
4329 is_cst = isl_poly_is_cst(poly);
4330 is_bad = isl_poly_is_nan(poly);
4331 if (is_bad >= 0 && !is_bad)
4332 is_bad = isl_poly_is_infty(poly);
4333 if (is_bad >= 0 && !is_bad)
4334 is_bad = isl_poly_is_neginfty(poly);
4335 if (is_cst < 0 || is_bad < 0)
4336 return isl_term_free(term);
4337 if (is_bad)
4339 "cannot handle NaN/infty polynomial",
4340 return isl_term_free(term));
4341
4342 if (is_cst) {
4343 isl_poly_cst *cst;
4344 cst = isl_poly_as_cst(poly);
4345 if (!cst)
4346 goto error;
4347 term = isl_term_cow(term);
4348 if (!term)
4349 goto error;
4350 isl_int_set(term->n, cst->n);
4351 isl_int_set(term->d, cst->d);
4352 if (fn(isl_term_copy(term), user) < 0)
4353 goto error;
4354 return term;
4355 }
4356
4357 rec = isl_poly_as_rec(poly);
4358 if (!rec)
4359 goto error;
4360
4361 for (i = 0; i < rec->n; ++i) {
4362 term = isl_term_cow(term);
4363 if (!term)
4364 goto error;
4365 term->pow[poly->var] = i;
4366 term = isl_poly_foreach_term(rec->p[i], fn, term, user);
4367 if (!term)
4368 goto error;
4369 }
4370 term = isl_term_cow(term);
4371 if (!term)
4372 return NULL;
4373 term->pow[poly->var] = 0;
4374
4375 return term;
4376error:
4377 isl_term_free(term);
4378 return NULL;
4379}
4380
4382 isl_stat (*fn)(__isl_take isl_term *term, void *user), void *user)
4383{
4384 isl_local *local;
4385 isl_term *term;
4386
4387 if (!qp)
4388 return isl_stat_error;
4389
4390 local = isl_qpolynomial_get_local(qp);
4391 term = isl_term_alloc(isl_space_copy(qp->dim), local);
4392 if (!term)
4393 return isl_stat_error;
4394
4396 fn, term, user);
4397
4398 isl_term_free(term);
4399
4400 return term ? isl_stat_ok : isl_stat_error;
4401}
4402
4404{
4405 isl_poly *poly;
4406 isl_qpolynomial *qp;
4407 int i;
4408 isl_size n;
4409
4410 n = isl_term_dim(term, isl_dim_all);
4411 if (n < 0)
4412 term = isl_term_free(term);
4413 if (!term)
4414 return NULL;
4415
4416 poly = isl_poly_rat_cst(term->dim->ctx, term->n, term->d);
4417 for (i = 0; i < n; ++i) {
4418 if (!term->pow[i])
4419 continue;
4421 isl_poly_var_pow(term->dim->ctx, i, term->pow[i]));
4422 }
4423
4424 qp = isl_qpolynomial_alloc(isl_space_copy(term->dim),
4425 term->div->n_row, poly);
4426 if (!qp)
4427 goto error;
4428 isl_mat_free(qp->div);
4429 qp->div = isl_mat_copy(term->div);
4430 if (!qp->div)
4431 goto error;
4432
4433 isl_term_free(term);
4434 return qp;
4435error:
4437 isl_term_free(term);
4438 return NULL;
4439}
4440
4442 __isl_take isl_space *space)
4443{
4444 int i;
4445 int extra;
4446 isl_size total, d_set, d_qp;
4447
4448 if (!qp || !space)
4449 goto error;
4450
4451 if (isl_space_is_equal(qp->dim, space)) {
4452 isl_space_free(space);
4453 return qp;
4454 }
4455
4456 qp = isl_qpolynomial_cow(qp);
4457 if (!qp)
4458 goto error;
4459
4460 d_set = isl_space_dim(space, isl_dim_set);
4462 extra = d_set - d_qp;
4463 total = isl_space_dim(qp->dim, isl_dim_all);
4464 if (d_set < 0 || d_qp < 0 || total < 0)
4465 goto error;
4466 if (qp->div->n_row) {
4467 int *exp;
4468
4469 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
4470 if (!exp)
4471 goto error;
4472 for (i = 0; i < qp->div->n_row; ++i)
4473 exp[i] = extra + i;
4474 qp->poly = expand(qp->poly, exp, total);
4475 free(exp);
4476 if (!qp->poly)
4477 goto error;
4478 }
4479 qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
4480 if (!qp->div)
4481 goto error;
4482 for (i = 0; i < qp->div->n_row; ++i)
4483 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
4484
4487
4488 return qp;
4489error:
4490 isl_space_free(space);
4492 return NULL;
4493}
4494
4495/* For each parameter or variable that does not appear in qp,
4496 * first eliminate the variable from all constraints and then set it to zero.
4497 */
4500{
4501 int *active = NULL;
4502 int i;
4503 isl_size d;
4504 isl_size nparam;
4505 isl_size nvar;
4506
4508 if (d < 0 || !qp)
4509 goto error;
4510
4511 active = isl_calloc_array(set->ctx, int, d);
4512 if (set_active(qp, active) < 0)
4513 goto error;
4514
4515 for (i = 0; i < d; ++i)
4516 if (!active[i])
4517 break;
4518
4519 if (i == d) {
4520 free(active);
4521 return set;
4522 }
4523
4524 nparam = isl_set_dim(set, isl_dim_param);
4525 nvar = isl_set_dim(set, isl_dim_set);
4526 if (nparam < 0 || nvar < 0)
4527 goto error;
4528 for (i = 0; i < nparam; ++i) {
4529 if (active[i])
4530 continue;
4533 }
4534 for (i = 0; i < nvar; ++i) {
4535 if (active[nparam + i])
4536 continue;
4539 }
4540
4541 free(active);
4542
4543 return set;
4544error:
4545 free(active);
4547 return NULL;
4548}
4549
4556
4558{
4559 struct isl_opt_data *data = (struct isl_opt_data *)user;
4560 isl_val *val;
4561
4563 if (data->first) {
4564 data->first = 0;
4565 data->opt = val;
4566 } else if (data->max) {
4567 data->opt = isl_val_max(data->opt, val);
4568 } else {
4569 data->opt = isl_val_min(data->opt, val);
4570 }
4571
4572 return isl_stat_ok;
4573}
4574
4577{
4578 struct isl_opt_data data = { NULL, 1, NULL, max };
4579 isl_bool is_cst;
4580
4581 if (!set)
4582 goto error;
4583
4585 if (is_cst < 0)
4586 goto error;
4587 if (is_cst) {
4591 return data.opt;
4592 }
4593
4594 set = fix_inactive(set, qp);
4595
4596 data.qp = qp;
4597 if (isl_set_foreach_point(set, opt_fn, &data) < 0)
4598 goto error;
4599
4600 if (data.first)
4602
4605 return data.opt;
4606error:
4609 isl_val_free(data.opt);
4610 return NULL;
4611}
4612
4615{
4616 int i;
4617 int n_sub;
4618 isl_ctx *ctx;
4619 isl_space *space;
4620 isl_poly **subs;
4621 isl_mat *mat, *diag;
4622
4624
4626 if (isl_morph_check_applies(morph, space) < 0)
4627 goto error;
4628
4630 n_sub = morph->inv->n_row - 1;
4631 if (morph->inv->n_row != morph->inv->n_col)
4632 n_sub += qp->div->n_row;
4633 subs = isl_calloc_array(ctx, struct isl_poly *, n_sub);
4634 if (n_sub && !subs)
4635 goto error;
4636
4637 for (i = 0; 1 + i < morph->inv->n_row; ++i)
4638 subs[i] = isl_poly_from_affine(ctx, morph->inv->row[1 + i],
4639 morph->inv->row[0][0], morph->inv->n_col);
4640 if (morph->inv->n_row != morph->inv->n_col)
4641 for (i = 0; i < qp->div->n_row; ++i)
4642 subs[morph->inv->n_row - 1 + i] =
4643 isl_poly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
4644
4645 qp->poly = isl_poly_subs(qp->poly, 0, n_sub, subs);
4646
4647 for (i = 0; i < n_sub; ++i)
4648 isl_poly_free(subs[i]);
4649 free(subs);
4650
4651 diag = isl_mat_diag(ctx, 1, morph->inv->row[0][0]);
4652 mat = isl_mat_diagonal(diag, isl_mat_copy(morph->inv));
4653 diag = isl_mat_diag(ctx, qp->div->n_row, morph->inv->row[0][0]);
4654 mat = isl_mat_diagonal(mat, diag);
4655 qp->div = isl_mat_product(qp->div, mat);
4656
4657 if (!qp->poly || !qp->div)
4658 goto error;
4659
4661 space = isl_space_copy(morph->ran->dim);
4663
4664 isl_morph_free(morph);
4665
4666 return qp;
4667error:
4669 isl_morph_free(morph);
4670 return NULL;
4671}
4672
4676{
4677 return isl_union_pw_qpolynomial_match_bin_op(upwqp1, upwqp2,
4679}
4680
4681/* Reorder the dimension of "qp" according to the given reordering.
4682 */
4685{
4686 isl_space *space;
4687 isl_poly *poly;
4688 isl_local *local;
4689
4690 if (!qp)
4691 goto error;
4692
4694 if (!r)
4695 goto error;
4696
4698 local = isl_local_reorder(local, isl_reordering_copy(r));
4700
4702 poly = reorder(poly, r->pos);
4704
4705 space = isl_reordering_get_space(r);
4707
4709 return qp;
4710error:
4713 return NULL;
4714}
4715
4718{
4719 isl_space *domain_space;
4720 isl_bool equal_params;
4721
4722 domain_space = isl_qpolynomial_peek_domain_space(qp);
4723 equal_params = isl_space_has_equal_params(domain_space, model);
4724 if (equal_params < 0)
4725 goto error;
4726 if (!equal_params) {
4727 isl_reordering *exp;
4728
4729 exp = isl_parameter_alignment_reordering(domain_space, model);
4731 }
4732
4733 isl_space_free(model);
4734 return qp;
4735error:
4736 isl_space_free(model);
4738 return NULL;
4739}
4740
4745
4746/* Create a slice where the integer division "div" has the fixed value "v".
4747 * In particular, if "div" refers to floor(f/m), then create a slice
4748 *
4749 * m v <= f <= m v + (m - 1)
4750 *
4751 * or
4752 *
4753 * f - m v >= 0
4754 * -f + m v + (m - 1) >= 0
4755 */
4757 __isl_keep isl_qpolynomial *qp, int div, isl_int v)
4758{
4760 isl_basic_set *bset = NULL;
4761 int k;
4762
4764 if (total < 0 || !qp)
4765 goto error;
4766
4767 bset = isl_basic_set_alloc_space(isl_space_copy(space), 0, 0, 2);
4768
4770 if (k < 0)
4771 goto error;
4772 isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4773 isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
4774
4776 if (k < 0)
4777 goto error;
4778 isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
4779 isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
4780 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
4781 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
4782
4783 isl_space_free(space);
4784 return isl_set_from_basic_set(bset);
4785error:
4786 isl_basic_set_free(bset);
4787 isl_space_free(space);
4788 return NULL;
4789}
4790
4792 __isl_take isl_qpolynomial *qp, void *user);
4793
4794/* Create a slice of the domain "set" such that integer division "div"
4795 * has the fixed value "v" and add the results to data->res,
4796 * replacing the integer division by "v" in "qp".
4797 */
4799 __isl_take isl_qpolynomial *qp, int div, isl_int v,
4800 struct isl_split_periods_data *data)
4801{
4802 int i;
4803 isl_size div_pos;
4804 isl_set *slice;
4805 isl_poly *cst;
4806
4807 slice = set_div_slice(isl_set_get_space(set), qp, div, v);
4808 set = isl_set_intersect(set, slice);
4809
4811 if (div_pos < 0)
4812 goto error;
4813
4814 for (i = div + 1; i < qp->div->n_row; ++i) {
4815 if (isl_int_is_zero(qp->div->row[i][2 + div_pos + div]))
4816 continue;
4817 isl_int_addmul(qp->div->row[i][1],
4818 qp->div->row[i][2 + div_pos + div], v);
4819 isl_int_set_si(qp->div->row[i][2 + div_pos + div], 0);
4820 }
4821
4822 cst = isl_poly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
4823 qp = substitute_div(qp, div, cst);
4824
4825 return split_periods(set, qp, data);
4826error:
4829 return isl_stat_error;
4830}
4831
4832/* Split the domain "set" such that integer division "div"
4833 * has a fixed value (ranging from "min" to "max") on each slice
4834 * and add the results to data->res.
4835 */
4837 __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
4838 struct isl_split_periods_data *data)
4839{
4840 for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
4841 isl_set *set_i = isl_set_copy(set);
4843
4844 if (set_div(set_i, qp_i, div, min, data) < 0)
4845 goto error;
4846 }
4849 return isl_stat_ok;
4850error:
4853 return isl_stat_error;
4854}
4855
4856/* If "qp" refers to any integer division
4857 * that can only attain "max_periods" distinct values on "set"
4858 * then split the domain along those distinct values.
4859 * Add the results (or the original if no splitting occurs)
4860 * to data->res.
4861 */
4863 __isl_take isl_qpolynomial *qp, void *user)
4864{
4865 int i;
4867 struct isl_split_periods_data *data;
4868 isl_int min, max;
4869 isl_size div_pos;
4871
4872 data = (struct isl_split_periods_data *)user;
4873
4874 if (!set || !qp)
4875 goto error;
4876
4877 if (qp->div->n_row == 0) {
4880 return isl_stat_ok;
4881 }
4882
4884 if (div_pos < 0)
4885 goto error;
4886
4887 isl_int_init(min);
4888 isl_int_init(max);
4889 for (i = 0; i < qp->div->n_row; ++i) {
4890 enum isl_lp_result lp_res;
4891
4892 if (isl_seq_any_non_zero(qp->div->row[i] + 2 + div_pos,
4893 qp->div->n_row))
4894 continue;
4895
4896 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4897 set->ctx->one, &min, NULL, NULL);
4898 if (lp_res == isl_lp_error)
4899 goto error2;
4900 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4901 continue;
4902 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4903
4904 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4905 set->ctx->one, &max, NULL, NULL);
4906 if (lp_res == isl_lp_error)
4907 goto error2;
4908 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4909 continue;
4910 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4911
4912 isl_int_sub(max, max, min);
4913 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4914 isl_int_add(max, max, min);
4915 break;
4916 }
4917 }
4918
4919 if (i < qp->div->n_row) {
4920 r = split_div(set, qp, i, min, max, data);
4921 } else {
4924 }
4925
4926 isl_int_clear(max);
4927 isl_int_clear(min);
4928
4929 return r;
4930error2:
4931 isl_int_clear(max);
4932 isl_int_clear(min);
4933error:
4936 return isl_stat_error;
4937}
4938
4939/* If any quasi-polynomial in pwqp refers to any integer division
4940 * that can only attain "max_periods" distinct values on its domain
4941 * then split the domain along those distinct values.
4942 */
4945{
4946 struct isl_split_periods_data data;
4947
4948 data.max_periods = max_periods;
4950
4952 goto error;
4953
4955
4956 return data.res;
4957error:
4960 return NULL;
4961}
4962
4963/* Construct a piecewise quasipolynomial that is constant on the given
4964 * domain. In particular, it is
4965 * 0 if cst == 0
4966 * 1 if cst == 1
4967 * infinity if cst == -1
4968 *
4969 * If cst == -1, then explicitly check whether the domain is empty and,
4970 * if so, return 0 instead.
4971 */
4973 __isl_take isl_basic_set *bset, int cst)
4974{
4975 isl_space *space;
4976 isl_qpolynomial *qp;
4977
4978 if (cst < 0 && isl_basic_set_is_empty(bset) == isl_bool_true)
4979 cst = 0;
4980 if (!bset)
4981 return NULL;
4982
4983 bset = isl_basic_set_params(bset);
4984 space = isl_basic_set_get_space(bset);
4985 if (cst < 0)
4987 else if (cst == 0)
4989 else
4992}
4993
4994/* Internal data structure for multiplicative_call_factor_pw_qpolynomial.
4995 * "fn" is the function that is called on each factor.
4996 * "pwpq" collects the results.
4997 */
5002
5003/* Call "fn" on "bset" and return the result,
5004 * but first check if "bset" has any redundant constraints or
5005 * implicit equality constraints.
5006 * If so, there may be further opportunities for detecting factors or
5007 * removing equality constraints, so recursively call
5008 * the top-level isl_basic_set_multiplicative_call.
5009 */
5013{
5014 isl_size n1, n2, n_eq;
5015
5016 n1 = isl_basic_set_n_constraint(bset);
5017 if (n1 < 0)
5018 bset = isl_basic_set_free(bset);
5021 n2 = isl_basic_set_n_constraint(bset);
5022 n_eq = isl_basic_set_n_equality(bset);
5023 if (n2 < 0 || n_eq < 0)
5024 bset = isl_basic_set_free(bset);
5025 else if (n2 < n1 || n_eq > 0)
5027 return fn(bset);
5028}
5029
5030/* isl_factorizer_every_factor_basic_set callback that applies
5031 * data->fn to the factor "bset" and multiplies in the result
5032 * in data->pwqp.
5033 */
5035 __isl_keep isl_basic_set *bset, void *user)
5036{
5039
5040 bset = isl_basic_set_copy(bset);
5041 res = multiplicative_call_base(bset, data->fn);
5042 data->pwqp = isl_pw_qpolynomial_mul(data->pwqp, res);
5043 if (!data->pwqp)
5044 return isl_bool_error;
5045
5046 return isl_bool_true;
5047}
5048
5049/* Factor bset, call fn on each of the factors and return the product.
5050 *
5051 * If no factors can be found, simply call fn on the input.
5052 * Otherwise, construct the factors based on the factorizer,
5053 * call fn on each factor and compute the product.
5054 */
5058{
5060 isl_space *space;
5061 isl_set *set;
5063 isl_qpolynomial *qp;
5064 isl_bool every;
5065
5067 if (!f)
5068 goto error;
5069 if (f->n_group == 0) {
5071 return multiplicative_call_base(bset, fn);
5072 }
5073
5074 space = isl_basic_set_get_space(bset);
5075 space = isl_space_params(space);
5078 data.pwqp = isl_pw_qpolynomial_alloc(set, qp);
5079
5082 if (every < 0)
5083 data.pwqp = isl_pw_qpolynomial_free(data.pwqp);
5084
5085 isl_basic_set_free(bset);
5087
5088 return data.pwqp;
5089error:
5090 isl_basic_set_free(bset);
5091 return NULL;
5092}
5093
5094/* Factor bset, call fn on each of the factors and return the product.
5095 * The function is assumed to evaluate to zero on empty domains,
5096 * to one on zero-dimensional domains and to infinity on unbounded domains
5097 * and will not be called explicitly on zero-dimensional or unbounded domains.
5098 *
5099 * We first check for some special cases and remove all equalities.
5100 * Then we hand over control to compressed_multiplicative_call.
5101 */
5105{
5106 isl_bool bounded;
5107 isl_size dim;
5108 isl_morph *morph;
5110
5111 if (!bset)
5112 return NULL;
5113
5115 return constant_on_domain(bset, 0);
5116
5117 dim = isl_basic_set_dim(bset, isl_dim_set);
5118 if (dim < 0)
5119 goto error;
5120 if (dim == 0)
5121 return constant_on_domain(bset, 1);
5122
5123 bounded = isl_basic_set_is_bounded(bset);
5124 if (bounded < 0)
5125 goto error;
5126 if (!bounded)
5127 return constant_on_domain(bset, -1);
5128
5129 if (bset->n_eq == 0)
5130 return compressed_multiplicative_call(bset, fn);
5131
5132 morph = isl_basic_set_full_compression(bset);
5133 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
5134
5136
5137 morph = isl_morph_dom_params(morph);
5138 morph = isl_morph_ran_params(morph);
5139 morph = isl_morph_inverse(morph);
5140
5142
5143 return pwqp;
5144error:
5145 isl_basic_set_free(bset);
5146 return NULL;
5147}
5148
5149/* Drop all floors in "qp", turning each integer division [a/m] into
5150 * a rational division a/m. If "down" is set, then the integer division
5151 * is replaced by (a-(m-1))/m instead.
5152 */
5154 __isl_take isl_qpolynomial *qp, int down)
5155{
5156 int i;
5157 isl_poly *s;
5158
5159 if (!qp)
5160 return NULL;
5161 if (qp->div->n_row == 0)
5162 return qp;
5163
5164 qp = isl_qpolynomial_cow(qp);
5165 if (!qp)
5166 return NULL;
5167
5168 for (i = qp->div->n_row - 1; i >= 0; --i) {
5169 if (down) {
5170 isl_int_sub(qp->div->row[i][1],
5171 qp->div->row[i][1], qp->div->row[i][0]);
5172 isl_int_add_ui(qp->div->row[i][1],
5173 qp->div->row[i][1], 1);
5174 }
5175 s = isl_poly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
5176 qp->div->row[i][0], qp->div->n_col - 1);
5177 qp = substitute_div(qp, i, s);
5178 if (!qp)
5179 return NULL;
5180 }
5181
5182 return qp;
5183}
5184
5185/* Drop all floors in "pwqp", turning each integer division [a/m] into
5186 * a rational division a/m.
5187 */
5190{
5191 int i;
5192
5193 if (!pwqp)
5194 return NULL;
5195
5197 return pwqp;
5198
5200 if (!pwqp)
5201 return NULL;
5202
5203 for (i = 0; i < pwqp->n; ++i) {
5204 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
5205 if (!pwqp->p[i].qp)
5206 goto error;
5207 }
5208
5209 return pwqp;
5210error:
5212 return NULL;
5213}
5214
5215/* Adjust all the integer divisions in "qp" such that they are at least
5216 * one over the given orthant (identified by "signs"). This ensures
5217 * that they will still be non-negative even after subtracting (m-1)/m.
5218 *
5219 * In particular, f is replaced by f' + v, changing f = [a/m]
5220 * to f' = [(a - m v)/m].
5221 * If the constant term k in a is smaller than m,
5222 * the constant term of v is set to floor(k/m) - 1.
5223 * For any other term, if the coefficient c and the variable x have
5224 * the same sign, then no changes are needed.
5225 * Otherwise, if the variable is positive (and c is negative),
5226 * then the coefficient of x in v is set to floor(c/m).
5227 * If the variable is negative (and c is positive),
5228 * then the coefficient of x in v is set to ceil(c/m).
5229 */
5231 int *signs)
5232{
5233 int i, j;
5234 isl_size div_pos;
5235 isl_vec *v = NULL;
5236 isl_poly *s;
5237
5238 qp = isl_qpolynomial_cow(qp);
5240 if (div_pos < 0)
5241 return isl_qpolynomial_free(qp);
5242 qp->div = isl_mat_cow(qp->div);
5243 if (!qp->div)
5244 goto error;
5245
5246 v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
5247
5248 for (i = 0; i < qp->div->n_row; ++i) {
5249 isl_int *row = qp->div->row[i];
5250 v = isl_vec_clr(v);
5251 if (!v)
5252 goto error;
5253 if (isl_int_lt(row[1], row[0])) {
5254 isl_int_fdiv_q(v->el[0], row[1], row[0]);
5255 isl_int_sub_ui(v->el[0], v->el[0], 1);
5256 isl_int_submul(row[1], row[0], v->el[0]);
5257 }
5258 for (j = 0; j < div_pos; ++j) {
5259 if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
5260 continue;
5261 if (signs[j] < 0)
5262 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
5263 else
5264 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
5265 isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
5266 }
5267 for (j = 0; j < i; ++j) {
5268 if (isl_int_sgn(row[2 + div_pos + j]) >= 0)
5269 continue;
5270 isl_int_fdiv_q(v->el[1 + div_pos + j],
5271 row[2 + div_pos + j], row[0]);
5272 isl_int_submul(row[2 + div_pos + j],
5273 row[0], v->el[1 + div_pos + j]);
5274 }
5275 for (j = i + 1; j < qp->div->n_row; ++j) {
5276 if (isl_int_is_zero(qp->div->row[j][2 + div_pos + i]))
5277 continue;
5278 isl_seq_combine(qp->div->row[j] + 1,
5279 qp->div->ctx->one, qp->div->row[j] + 1,
5280 qp->div->row[j][2 + div_pos + i], v->el,
5281 v->size);
5282 }
5283 isl_int_set_si(v->el[1 + div_pos + i], 1);
5284 s = isl_poly_from_affine(qp->dim->ctx, v->el,
5285 qp->div->ctx->one, v->size);
5286 qp->poly = isl_poly_subs(qp->poly, div_pos + i, 1, &s);
5287 isl_poly_free(s);
5288 if (!qp->poly)
5289 goto error;
5290 }
5291
5292 isl_vec_free(v);
5293 return qp;
5294error:
5295 isl_vec_free(v);
5297 return NULL;
5298}
5299
5305
5306/* Appoximate data->qp by a polynomial on the orthant identified by "signs".
5307 * We first make all integer divisions positive and then split the
5308 * quasipolynomials into terms with sign data->sign (the direction
5309 * of the requested approximation) and terms with the opposite sign.
5310 * In the first set of terms, each integer division [a/m] is
5311 * overapproximated by a/m, while in the second it is underapproximated
5312 * by (a-(m-1))/m.
5313 */
5315 int *signs, void *user)
5316{
5317 struct isl_to_poly_data *data = user;
5319 isl_qpolynomial *qp, *up, *down;
5320
5321 qp = isl_qpolynomial_copy(data->qp);
5322 qp = make_divs_pos(qp, signs);
5323
5324 up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
5325 up = qp_drop_floors(up, 0);
5326 down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
5327 down = qp_drop_floors(down, 1);
5328
5330 qp = isl_qpolynomial_add(up, down);
5331
5332 t = isl_pw_qpolynomial_alloc(orthant, qp);
5333 data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
5334
5335 return isl_stat_ok;
5336}
5337
5338/* Approximate each quasipolynomial by a polynomial. If "sign" is positive,
5339 * the polynomial will be an overapproximation. If "sign" is negative,
5340 * it will be an underapproximation. If "sign" is zero, the approximation
5341 * will lie somewhere in between.
5342 *
5343 * In particular, is sign == 0, we simply drop the floors, turning
5344 * the integer divisions into rational divisions.
5345 * Otherwise, we split the domains into orthants, make all integer divisions
5346 * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
5347 * depending on the requested sign and the sign of the term in which
5348 * the integer division appears.
5349 */
5352{
5353 int i;
5354 struct isl_to_poly_data data;
5355
5356 if (sign == 0)
5357 return pwqp_drop_floors(pwqp);
5358
5359 if (!pwqp)
5360 return NULL;
5361
5362 data.sign = sign;
5364
5365 for (i = 0; i < pwqp->n; ++i) {
5366 if (pwqp->p[i].qp->div->n_row == 0) {
5369 isl_set_copy(pwqp->p[i].set),
5370 isl_qpolynomial_copy(pwqp->p[i].qp));
5372 continue;
5373 }
5374 data.qp = pwqp->p[i].qp;
5375 if (isl_set_foreach_orthant(pwqp->p[i].set,
5376 &to_polynomial_on_orthant, &data) < 0)
5377 goto error;
5378 }
5379
5381
5382 return data.res;
5383error:
5386 return NULL;
5387}
5388
5396
5399{
5400 return isl_union_pw_qpolynomial_transform_inplace(upwqp,
5401 &poly_entry, &sign);
5402}
5403
5404/* Return an isl_aff that is equivalent to "qp".
5405 */
5407{
5408 isl_local_space *ls;
5409 isl_vec *vec;
5410 isl_aff *aff;
5411 isl_bool is_affine;
5412
5413 is_affine = isl_qpolynomial_isa_aff(qp);
5414 if (is_affine < 0)
5415 goto error;
5416 if (!is_affine)
5418 "input quasi-polynomial not affine", goto error);
5421 aff = isl_aff_alloc_vec(ls, vec);
5423 return aff;
5424error:
5426 return NULL;
5427}
5428
isl_ctx * isl_aff_get_ctx(__isl_keep isl_aff *aff)
Definition isl_aff.c:465
__isl_null isl_aff * isl_aff_free(__isl_take isl_aff *aff)
Definition isl_aff.c:449
__isl_null isl_pw_aff * isl_pw_aff_free(__isl_take isl_pw_aff *pwaff)
__isl_export __isl_give isl_space * isl_pw_aff_get_space(__isl_keep isl_pw_aff *pwaff)
__isl_give isl_aff * isl_aff_copy(__isl_keep isl_aff *aff)
Definition isl_aff.c:145
__isl_give isl_space * isl_aff_get_domain_space(__isl_keep isl_aff *aff)
Definition isl_aff.c:568
__isl_give isl_aff * isl_constraint_get_bound(__isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
__isl_null isl_constraint * isl_constraint_free(__isl_take isl_constraint *c)
isl_size isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
#define __isl_take
Definition ctx.h:22
#define isl_calloc_type(ctx, type)
Definition ctx.h:130
isl_stat
Definition ctx.h:84
@ isl_stat_error
Definition ctx.h:85
@ isl_stat_ok
Definition ctx.h:86
#define __isl_give
Definition ctx.h:19
#define isl_size_error
Definition ctx.h:98
#define __isl_null
Definition ctx.h:28
#define isl_die(ctx, errno, msg, code)
Definition ctx.h:138
void isl_ctx_deref(struct isl_ctx *ctx)
Definition isl_ctx.c:287
#define isl_assert(ctx, test, code)
Definition ctx.h:153
isl_bool isl_bool_ok(int b)
Definition isl_ctx.c:58
@ isl_error_invalid
Definition ctx.h:80
#define isl_alloc_array(ctx, type, n)
Definition ctx.h:132
#define isl_calloc_array(ctx, type, n)
Definition ctx.h:133
#define __isl_keep
Definition ctx.h:25
#define isl_calloc(ctx, type, size)
Definition ctx.h:125
int isl_size
Definition ctx.h:97
#define isl_alloc_type(ctx, type)
Definition ctx.h:129
void isl_ctx_ref(struct isl_ctx *ctx)
Definition isl_ctx.c:282
isl_bool
Definition ctx.h:89
@ isl_bool_false
Definition ctx.h:91
@ isl_bool_true
Definition ctx.h:92
@ isl_bool_error
Definition ctx.h:90
__isl_export __isl_give ISL_HMAP __isl_take ISL_KEY __isl_take ISL_VAL * val
Definition hmap.h:32
isl_stat isl_stat(* fn)(__isl_take ISL_KEY *key, __isl_take ISL_VAL *val, void *user)
Definition hmap.h:37
isl_stat isl_stat void * user
Definition hmap.h:39
int GMPQAPI cmp(mp_rat op1, mp_rat op2)
void GMPZAPI gcd(mp_int rop, mp_int op1, mp_int op2)
__isl_give isl_aff * isl_aff_normalize(__isl_take isl_aff *aff)
Definition isl_aff.c:1704
__isl_give isl_aff * isl_aff_alloc_vec(__isl_take isl_local_space *ls, __isl_take isl_vec *v)
Definition isl_aff.c:101
__isl_give isl_aff * isl_aff_alloc(__isl_take isl_local_space *ls)
Definition isl_aff.c:124
static __isl_give isl_ast_expr * var(struct isl_ast_add_term_data *data, enum isl_dim_type type, int pos)
__isl_give isl_factorizer * isl_basic_set_factorizer(__isl_keep isl_basic_set *bset)
__isl_give isl_bool isl_factorizer_every_factor_basic_set(__isl_keep isl_factorizer *f, isl_bool(*test)(__isl_keep isl_basic_set *bset, void *user), void *user)
__isl_null isl_factorizer * isl_factorizer_free(__isl_take isl_factorizer *f)
#define isl_int_is_nonneg(i)
Definition isl_int.h:37
#define isl_int_is_zero(i)
Definition isl_int.h:31
#define isl_int_is_one(i)
Definition isl_int.h:32
#define isl_int_is_pos(i)
Definition isl_int.h:34
#define isl_int_is_negone(i)
Definition isl_int.h:33
#define isl_int_is_neg(i)
Definition isl_int.h:35
#define isl_int_divexact_ui(r, i, j)
Definition isl_int_gmp.h:45
#define isl_int_gcd(r, i, j)
Definition isl_int_gmp.h:42
#define isl_int_add_ui(r, i, j)
Definition isl_int_gmp.h:27
#define isl_int_le(i, j)
Definition isl_int_gmp.h:60
#define isl_int_add(r, i, j)
Definition isl_int_gmp.h:30
#define isl_int_cmp(i, j)
Definition isl_int_gmp.h:55
#define isl_int_addmul(r, i, j)
Definition isl_int_gmp.h:37
#define isl_int_eq(i, j)
Definition isl_int_gmp.h:57
#define isl_int_fdiv_r(r, i, j)
Definition isl_int_gmp.h:50
#define isl_int_set(r, i)
Definition isl_int_gmp.h:14
#define isl_int_cdiv_q(r, i, j)
Definition isl_int_gmp.h:47
#define isl_int_mul_ui(r, i, j)
Definition isl_int_gmp.h:35
#define isl_int_lcm(r, i, j)
Definition isl_int_gmp.h:43
#define isl_int_divexact(r, i, j)
Definition isl_int_gmp.h:44
#define isl_int_sgn(i)
Definition isl_int_gmp.h:54
#define isl_int_mul(r, i, j)
Definition isl_int_gmp.h:32
#define isl_int_lt(i, j)
Definition isl_int_gmp.h:59
#define isl_int_set_si(r, i)
Definition isl_int_gmp.h:15
mpz_t isl_int
Definition isl_int_gmp.h:9
#define isl_int_sub_ui(r, i, j)
Definition isl_int_gmp.h:28
#define isl_int_fdiv_q(r, i, j)
Definition isl_int_gmp.h:49
#define isl_int_sub(r, i, j)
Definition isl_int_gmp.h:31
#define isl_int_init(i)
Definition isl_int_gmp.h:11
#define isl_int_abs(r, i)
Definition isl_int_gmp.h:23
#define isl_int_clear(i)
Definition isl_int_gmp.h:12
#define isl_int_cmp_si(i, si)
Definition isl_int_gmp.h:56
#define isl_int_submul(r, i, j)
Definition isl_int_gmp.h:39
__isl_give dup(__isl_keep LIST(EL) *list)
__isl_give isl_local * isl_local_copy(__isl_keep isl_local *local)
Definition isl_local.c:44
__isl_give isl_vec * isl_local_extend_point_vec(__isl_keep isl_local *local, __isl_take isl_vec *v)
Definition isl_local.c:363
__isl_give isl_local * isl_local_reorder(__isl_take isl_local *local, __isl_take isl_reordering *r)
Definition isl_local.c:263
__isl_null isl_local * isl_local_free(__isl_take isl_local *local)
Definition isl_local.c:51
__isl_give isl_local * isl_local_move_vars(__isl_take isl_local *local, unsigned dst_pos, unsigned src_pos, unsigned n)
Definition isl_local.c:300
int isl_local_cmp(__isl_keep isl_local *local1, __isl_keep isl_local *local2)
Definition isl_local.c:189
isl_mat isl_local
Definition isl_local.h:7
__isl_give isl_mat * isl_merge_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2, int *exp1, int *exp2)
__isl_give isl_set * isl_local_space_lift_set(__isl_take isl_local_space *ls, __isl_take isl_set *set)
__isl_give isl_basic_set * isl_local_space_lift_basic_set(__isl_take isl_local_space *ls, __isl_take isl_basic_set *bset)
__isl_give isl_local_space * isl_local_space_alloc_div(__isl_take isl_space *space, __isl_take isl_mat *div)
enum isl_lp_result isl_set_solve_lp(__isl_keep isl_set *set, int max, isl_int *f, isl_int d, isl_int *opt, isl_int *opt_denom, __isl_give isl_vec **sol)
Definition isl_lp.c:198
__isl_give isl_basic_set * isl_basic_set_alloc_space(__isl_take isl_space *space, unsigned extra, unsigned n_eq, unsigned n_ineq)
Definition isl_map.c:1361
isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
Definition isl_map.c:258
unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition isl_map.c:191
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_map.c:73
isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set, isl_stat(*fn)(__isl_take isl_set *orthant, int *signs, void *user), void *user)
Definition isl_map.c:9719
int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
Definition isl_map.c:1762
#define isl_set
#define isl_basic_set
__isl_give isl_mat * isl_mat_cow(__isl_take isl_mat *mat)
Definition isl_mat.c:226
__isl_give isl_mat * isl_mat_col_neg(__isl_take isl_mat *mat, int col)
Definition isl_mat.c:1704
__isl_give isl_mat * isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
Definition isl_mat.c:386
__isl_give isl_mat * isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
Definition isl_mat.c:405
void isl_mat_col_mul(__isl_keep isl_mat *mat, int dst_col, isl_int f, int src_col)
Definition isl_mat.c:1670
__isl_give isl_mat * isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col, isl_int f, int src_col)
Definition isl_mat.c:1682
isl_stat isl_morph_check_applies(__isl_keep isl_morph *morph, __isl_keep isl_space *space)
Definition isl_morph.c:215
__isl_give isl_morph * isl_basic_set_full_compression(__isl_keep isl_basic_set *bset)
Definition isl_morph.c:770
__isl_give isl_morph * isl_morph_inverse(__isl_take isl_morph *morph)
Definition isl_morph.c:743
__isl_give isl_morph * isl_morph_ran_params(__isl_take isl_morph *morph)
Definition isl_morph.c:333
__isl_give isl_morph * isl_morph_dom_params(__isl_take isl_morph *morph)
Definition isl_morph.c:310
__isl_null isl_morph * isl_morph_free(__isl_take isl_morph *morph)
Definition isl_morph.c:89
__isl_give isl_basic_set * isl_morph_basic_set(__isl_take isl_morph *morph, __isl_take isl_basic_set *bset)
Definition isl_morph.c:645
__isl_give isl_morph * isl_morph_copy(__isl_keep isl_morph *morph)
Definition isl_morph.c:59
static __isl_give isl_qpolynomial * with_merged_divs(__isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2), __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_add(__isl_take isl_pw_qpolynomial *pwqp1, __isl_take isl_pw_qpolynomial *pwqp2)
__isl_give isl_basic_map * isl_basic_map_from_qpolynomial(__isl_take isl_qpolynomial *qp)
isl_bool isl_poly_is_infty(__isl_keep isl_poly *poly)
__isl_give isl_poly_rec * isl_poly_alloc_rec(isl_ctx *ctx, int var, int size)
static isl_stat set_div(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp, int div, isl_int v, struct isl_split_periods_data *data)
__isl_give isl_poly * isl_poly_cst_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_mul(__isl_take isl_pw_qpolynomial *pwqp1, __isl_take isl_pw_qpolynomial *pwqp2)
static int div_sort_cmp(const void *p1, const void *p2)
__isl_give isl_qpolynomial * isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
__isl_give isl_qpolynomial * isl_qpolynomial_alloc(__isl_take isl_space *space, unsigned n_div, __isl_take isl_poly *poly)
__isl_give isl_union_pw_qpolynomial * isl_union_pw_qpolynomial_to_polynomial(__isl_take isl_union_pw_qpolynomial *upwqp, int sign)
static isl_stat poly_set_active(__isl_keep isl_poly *poly, int *active, int d)
static isl_bool multiplicative_call_factor_pw_qpolynomial(__isl_keep isl_basic_set *bset, void *user)
int isl_poly_update_affine(__isl_keep isl_poly *poly, __isl_keep isl_vec *aff)
static isl_bool isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp, enum isl_dim_type type, unsigned first, unsigned n)
__isl_give isl_qpolynomial * isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
__isl_keep isl_poly_cst * isl_poly_as_cst(__isl_keep isl_poly *poly)
__isl_give isl_poly * isl_poly_sum_cst(__isl_take isl_poly *poly1, __isl_take isl_poly *poly2)
static __isl_give isl_qpolynomial * isl_qpolynomial_substitute_equalities_lifted(__isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
__isl_give isl_qpolynomial * isl_qpolynomial_from_affine(__isl_take isl_space *space, isl_int *f, isl_int denom)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_split_periods(__isl_take isl_pw_qpolynomial *pwqp, int max_periods)
static int * reordering_move(isl_ctx *ctx, unsigned len, unsigned dst, unsigned src, unsigned n)
__isl_give isl_qpolynomial * isl_qpolynomial_var_on_domain(__isl_take isl_space *domain, enum isl_dim_type type, unsigned pos)
__isl_give isl_qpolynomial * isl_qpolynomial_reset_domain_space(__isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
static __isl_give isl_val * eval_void(__isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
__isl_give isl_poly * isl_qpolynomial_get_poly(__isl_keep isl_qpolynomial *qp)
__isl_give isl_poly * isl_poly_cow(__isl_take isl_poly *poly)
static __isl_give isl_pw_qpolynomial * compressed_multiplicative_call(__isl_take isl_basic_set *bset, __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
static __isl_give isl_poly * reorder(__isl_take isl_poly *poly, int *r)
isl_ctx * isl_term_get_ctx(__isl_keep isl_term *term)
static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
__isl_give isl_qpolynomial * isl_qpolynomial_set_dim_name(__isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned pos, const char *s)
static __isl_give isl_poly * isl_qpolynomial_take_poly(__isl_keep isl_qpolynomial *qp)
static __isl_give isl_pw_qpolynomial * constant_on_domain(__isl_take isl_basic_set *bset, int cst)
__isl_give isl_qpolynomial * isl_qpolynomial_realign_domain(__isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
__isl_give isl_poly * isl_poly_dup_cst(__isl_keep isl_poly *poly)
__isl_give isl_term * isl_term_dup(__isl_keep isl_term *term)
static isl_stat split_periods(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp, void *user)
__isl_give isl_poly * isl_poly_cst_add_isl_int(__isl_take isl_poly *poly, isl_int v)
static isl_stat opt_fn(__isl_take isl_point *pnt, void *user)
__isl_give isl_qpolynomial * isl_qpolynomial_infty_on_domain(__isl_take isl_space *domain)
static __isl_give isl_pw_qpolynomial * pwqp_drop_floors(__isl_take isl_pw_qpolynomial *pwqp)
isl_bool isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_to_polynomial(__isl_take isl_pw_qpolynomial *pwqp, int sign)
__isl_give isl_qpolynomial * isl_qpolynomial_coeff(__isl_keep isl_qpolynomial *qp, enum isl_dim_type type, unsigned t_pos, int deg)
static __isl_give isl_qpolynomial * local_poly_move_dims(__isl_take isl_qpolynomial *qp, unsigned dst_pos, unsigned src_pos, unsigned n)
__isl_keep isl_poly_rec * isl_poly_as_rec(__isl_keep isl_poly *poly)
static void update_coeff(__isl_keep isl_vec *aff, __isl_keep isl_poly_cst *cst, int pos)
__isl_give isl_poly * isl_poly_one(isl_ctx *ctx)
__isl_give isl_qpolynomial * isl_qpolynomial_homogenize(__isl_take isl_qpolynomial *poly)
static void invert_div(__isl_keep isl_qpolynomial *qp, int div, __isl_keep isl_mat **mat)
__isl_give isl_qpolynomial * isl_qpolynomial_move_dims(__isl_take isl_qpolynomial *qp, enum isl_dim_type dst_type, unsigned dst_pos, enum isl_dim_type src_type, unsigned src_pos, unsigned n)
__isl_give isl_qpolynomial * isl_qpolynomial_from_constraint(__isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
__isl_give isl_poly * isl_poly_homogenize(__isl_take isl_poly *poly, int deg, int target, int first, int last)
__isl_give isl_poly * isl_poly_subs(__isl_take isl_poly *poly, unsigned first, unsigned n, __isl_keep isl_poly **subs)
isl_bool isl_poly_is_cst(__isl_keep isl_poly *poly)
static __isl_give isl_pw_qpolynomial * poly_entry(__isl_take isl_pw_qpolynomial *pwqp, void *user)
__isl_give isl_poly * isl_poly_rat_cst(isl_ctx *ctx, isl_int n, isl_int d)
__isl_give isl_qpolynomial * isl_qpolynomial_val_on_domain(__isl_take isl_space *domain, __isl_take isl_val *val)
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
static __isl_give isl_poly * expand(__isl_take isl_poly *poly, int *exp, int first)
static __isl_keep isl_space * isl_qpolynomial_peek_domain_space(__isl_keep isl_qpolynomial *qp)
static __isl_give isl_pw_qpolynomial * multiplicative_call_base(__isl_take isl_basic_set *bset, __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
static enum isl_dim_type domain_type(enum isl_dim_type type)
static __isl_give isl_poly * replace_by_constant_term(__isl_take isl_poly *poly)
__isl_give isl_qpolynomial * isl_qpolynomial_from_aff(__isl_take isl_aff *aff)
__isl_give isl_qpolynomial * isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
static int cmp_row(__isl_keep isl_mat *div, int i, int j)
isl_bool isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
__isl_give isl_qpolynomial * isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
static __isl_give isl_poly * replace_by_zero(__isl_take isl_poly *poly)
__isl_give isl_space * isl_qpolynomial_get_domain_space(__isl_keep isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_substitute_equalities(__isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
__isl_null isl_qpolynomial * isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_nan_on_domain(__isl_take isl_space *domain)
static isl_bool compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
__isl_give isl_term * isl_term_copy(__isl_keep isl_term *term)
static __isl_give isl_qpolynomial * substitute_div(__isl_take isl_qpolynomial *qp, int div, __isl_take isl_poly *s)
__isl_give isl_poly * isl_poly_mul_rec(__isl_take isl_poly *poly1, __isl_take isl_poly *poly2)
__isl_give isl_poly * isl_poly_var_pow(isl_ctx *ctx, int pos, int power)
__isl_give isl_qpolynomial * isl_qpolynomial_scale_down_val(__isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
__isl_give isl_qpolynomial * isl_qpolynomial_one_on_domain(__isl_take isl_space *domain)
__isl_give isl_qpolynomial * isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
int isl_poly_sgn(__isl_keep isl_poly *poly)
__isl_give isl_poly * isl_poly_mul_isl_int(__isl_take isl_poly *poly, isl_int v)
isl_stat isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp, __isl_keep isl_basic_set *bset, isl_stat(*fn)(__isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly, void *user), void *user)
static __isl_give isl_qpolynomial * isl_qpolynomial_zero_in_space(__isl_take isl_space *space)
isl_bool isl_poly_is_affine(__isl_keep isl_poly *poly)
__isl_give isl_vec * isl_qpolynomial_extract_affine(__isl_keep isl_qpolynomial *qp)
static __isl_give isl_qpolynomial * substitute_non_divs(__isl_take isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_scale_val(__isl_take isl_qpolynomial *qp, __isl_take isl_val *v)
isl_stat isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp, isl_stat(*fn)(__isl_take isl_term *term, void *user), void *user)
__isl_null isl_term * isl_term_free(__isl_take isl_term *term)
__isl_give isl_qpolynomial * isl_qpolynomial_project_domain_on_params(__isl_take isl_qpolynomial *qp)
__isl_give isl_poly * isl_poly_mul_cst(__isl_take isl_poly *poly1, __isl_take isl_poly *poly2)
int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
__isl_give isl_poly * isl_poly_add_isl_int(__isl_take isl_poly *poly, isl_int v)
__isl_give isl_qpolynomial * isl_qpolynomial_cst_on_domain(__isl_take isl_space *domain, isl_int v)
static __isl_give isl_qpolynomial * qp_drop_floors(__isl_take isl_qpolynomial *qp, int down)
static __isl_give isl_local * isl_qpolynomial_take_local(__isl_keep isl_qpolynomial *qp)
static __isl_give isl_qpolynomial * reduce_divs(__isl_take isl_qpolynomial *qp)
__isl_give isl_val * isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp)
isl_bool isl_poly_is_zero(__isl_keep isl_poly *poly)
static __isl_give isl_qpolynomial * isl_qpolynomial_restore_poly(__isl_keep isl_qpolynomial *qp, __isl_take isl_poly *poly)
__isl_give isl_qpolynomial * isl_qpolynomial_neginfty_on_domain(__isl_take isl_space *domain)
static void isl_poly_cst_reduce(__isl_keep isl_poly_cst *cst)
__isl_give isl_qpolynomial * isl_qpolynomial_var_pow_on_domain(__isl_take isl_space *domain, int pos, int power)
static isl_stat to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs, void *user)
__isl_give isl_val * isl_poly_eval(__isl_take isl_poly *poly, __isl_take isl_vec *vec)
__isl_give isl_pw_qpolynomial * isl_basic_set_multiplicative_call(__isl_take isl_basic_set *bset, __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
__isl_give isl_poly * isl_poly_nan(isl_ctx *ctx)
__isl_give isl_qpolynomial * isl_qpolynomial_scale(__isl_take isl_qpolynomial *qp, isl_int v)
static __isl_give isl_poly * isl_poly_scale_val(__isl_take isl_poly *poly, __isl_keep isl_val *v)
__isl_give isl_qpolynomial * isl_qpolynomial_zero_on_domain(__isl_take isl_space *domain)
__isl_null isl_poly * isl_poly_free(__isl_take isl_poly *poly)
isl_bool isl_poly_is_equal(__isl_keep isl_poly *poly1, __isl_keep isl_poly *poly2)
__isl_give isl_poly_cst * isl_poly_cst_alloc(isl_ctx *ctx)
__isl_give isl_val * isl_term_get_coefficient_val(__isl_keep isl_term *term)
static __isl_keep isl_space * isl_term_peek_space(__isl_keep isl_term *term)
static isl_stat poly_update_den(__isl_keep isl_poly *poly, isl_int *d)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_from_pw_aff(__isl_take isl_pw_aff *pwaff)
static __isl_give isl_local_space * isl_qpolynomial_get_domain_local_space(__isl_keep isl_qpolynomial *qp)
__isl_give isl_poly * isl_poly_dup(__isl_keep isl_poly *poly)
isl_bool isl_poly_is_one(__isl_keep isl_poly *poly)
isl_bool isl_poly_is_nan(__isl_keep isl_poly *poly)
int isl_poly_cmp(__isl_keep isl_poly_cst *cst1, __isl_keep isl_poly_cst *cst2)
isl_size isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
int isl_qpolynomial_plain_cmp(__isl_keep isl_qpolynomial *qp1, __isl_keep isl_qpolynomial *qp2)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_pow(__isl_take isl_pw_qpolynomial *pwqp, unsigned power)
__isl_give isl_qpolynomial * isl_qpolynomial_rat_cst_on_domain(__isl_take isl_space *domain, const isl_int n, const isl_int d)
static static __isl_keep isl_local * isl_qpolynomial_get_local(__isl_keep isl_qpolynomial *qp)
static int needs_invert(__isl_keep isl_mat *div, int row)
__isl_give isl_qpolynomial * isl_qpolynomial_morph_domain(__isl_take isl_qpolynomial *qp, __isl_take isl_morph *morph)
__isl_give isl_poly * isl_poly_zero(isl_ctx *ctx)
__isl_give isl_qpolynomial * isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
static __isl_give isl_qpolynomial * sort_divs(__isl_take isl_qpolynomial *qp)
static __isl_give isl_qpolynomial * remove_redundant_divs(__isl_take isl_qpolynomial *qp)
isl_size isl_qpolynomial_domain_dim(__isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
__isl_give isl_term * isl_term_alloc(__isl_take isl_space *space, __isl_take isl_mat *div)
__isl_give isl_qpolynomial * isl_qpolynomial_gist_params(__isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
static __isl_give isl_set * fix_inactive(__isl_take isl_set *set, __isl_keep isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_add_dims(__isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
unsigned isl_qpolynomial_domain_offset(__isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
__isl_give isl_poly * isl_poly_coeff(__isl_keep isl_poly *poly, unsigned pos, int deg)
static isl_size isl_term_get_exp(__isl_keep isl_term *term, enum isl_dim_type type, unsigned pos)
__isl_give isl_qpolynomial * isl_qpolynomial_add_isl_int(__isl_take isl_qpolynomial *qp, isl_int v)
isl_bool isl_qpolynomial_isa_aff(__isl_keep isl_qpolynomial *qp)
__isl_give isl_poly * isl_poly_sum(__isl_take isl_poly *poly1, __isl_take isl_poly *poly2)
isl_bool isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp, isl_int *n, isl_int *d)
isl_bool isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
__isl_give isl_val * isl_qpolynomial_get_constant_val(__isl_keep isl_qpolynomial *qp)
static void poly_free_rec(__isl_take isl_poly_rec *rec)
__isl_give isl_qpolynomial * isl_qpolynomial_align_params(__isl_take isl_qpolynomial *qp, __isl_take isl_space *model)
isl_bool isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_gist(__isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
__isl_give isl_poly * isl_poly_dup_rec(__isl_keep isl_poly *poly)
static isl_size isl_term_offset(__isl_keep isl_term *term, enum isl_dim_type type)
static isl_stat set_active(__isl_keep isl_qpolynomial *qp, int *active)
static __isl_give isl_qpolynomial * make_divs_pos(__isl_take isl_qpolynomial *qp, int *signs)
__isl_give isl_term * isl_term_cow(__isl_take isl_term *term)
isl_bool isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
__isl_give isl_union_pw_qpolynomial * isl_union_pw_qpolynomial_mul(__isl_take isl_union_pw_qpolynomial *upwqp1, __isl_take isl_union_pw_qpolynomial *upwqp2)
__isl_give isl_poly * isl_poly_mul(__isl_take isl_poly *poly1, __isl_take isl_poly *poly2)
__isl_give isl_term * isl_poly_foreach_term(__isl_keep isl_poly *poly, isl_stat(*fn)(__isl_take isl_term *term, void *user), __isl_take isl_term *term, void *user)
__isl_give isl_qpolynomial * isl_qpolynomial_mul_isl_int(__isl_take isl_qpolynomial *qp, isl_int v)
__isl_give isl_qpolynomial * isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
static void poly_free_cst(__isl_take isl_poly_cst *cst)
static isl_stat split_div(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max, struct isl_split_periods_data *data)
static __isl_give isl_poly * isl_poly_cst_scale_val(__isl_take isl_poly *poly, __isl_keep isl_val *v)
isl_bool isl_poly_is_neginfty(__isl_keep isl_poly *poly)
int isl_poly_degree(__isl_keep isl_poly *poly, int first, int last)
isl_size isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
__isl_give isl_qpolynomial * isl_qpolynomial_substitute(__isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned first, unsigned n, __isl_keep isl_qpolynomial **subs)
static __isl_give isl_space * isl_qpolynomial_take_domain_space(__isl_keep isl_qpolynomial *qp)
static __isl_give isl_set * set_div_slice(__isl_take isl_space *space, __isl_keep isl_qpolynomial *qp, int div, isl_int v)
__isl_give isl_poly * isl_poly_drop(__isl_take isl_poly *poly, unsigned first, unsigned n)
__isl_give isl_poly * isl_poly_infty(isl_ctx *ctx)
void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
__isl_give isl_qpolynomial * isl_qpolynomial_add_on_domain(__isl_keep isl_set *dom, __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
isl_bool isl_qpolynomial_plain_is_equal(__isl_keep isl_qpolynomial *qp1, __isl_keep isl_qpolynomial *qp2)
static __isl_keep isl_poly * isl_qpolynomial_peek_poly(__isl_keep isl_qpolynomial *qp)
static void reduce_div(__isl_keep isl_qpolynomial *qp, int div, __isl_keep isl_mat **mat)
__isl_give isl_qpolynomial * isl_qpolynomial_reset_space_and_domain(__isl_take isl_qpolynomial *qp, __isl_take isl_space *space, __isl_take isl_space *domain)
__isl_give isl_qpolynomial * isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp, unsigned power)
__isl_give isl_space * isl_qpolynomial_get_space(__isl_keep isl_qpolynomial *qp)
isl_bool isl_poly_is_negone(__isl_keep isl_poly *poly)
__isl_give isl_val * isl_qpolynomial_opt_on_domain(__isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
static __isl_give isl_val * isl_poly_get_constant_val(__isl_keep isl_poly *poly)
isl_bool isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_from_term(__isl_take isl_term *term)
static int isl_poly_plain_cmp(__isl_keep isl_poly *poly1, __isl_keep isl_poly *poly2)
__isl_give isl_qpolynomial * isl_qpolynomial_domain_reverse(__isl_take isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_drop_dims(__isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned first, unsigned n)
static isl_size isl_qpolynomial_domain_var_offset(__isl_keep isl_qpolynomial *qp, enum isl_dim_type type)
isl_ctx * isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
__isl_give isl_val * isl_qpolynomial_eval(__isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
static __isl_give isl_qpolynomial * isl_qpolynomial_restore_local(__isl_keep isl_qpolynomial *qp, __isl_take isl_local *local)
__isl_give isl_poly * isl_poly_pow(__isl_take isl_poly *poly, unsigned power)
__isl_give isl_poly * isl_poly_from_affine(isl_ctx *ctx, isl_int *f, isl_int denom, unsigned len)
__isl_give isl_qpolynomial * isl_qpolynomial_insert_dims(__isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned first, unsigned n)
__isl_give isl_aff * isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
__isl_give isl_poly * isl_poly_copy(__isl_keep isl_poly *poly)
__isl_give isl_poly * isl_poly_neginfty(isl_ctx *ctx)
static __isl_give isl_qpolynomial * isl_qpolynomial_restore_domain_space(__isl_take isl_qpolynomial *qp, __isl_take isl_space *space)
__isl_give isl_aff * isl_qpolynomial_as_aff(__isl_take isl_qpolynomial *qp)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_cow(__isl_take isl_pw_qpolynomial *pwqp)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_morph_domain(__isl_take isl_pw_qpolynomial *pwqp, __isl_take isl_morph *morph)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_add_piece(__isl_take isl_pw_qpolynomial *pwqp, __isl_take isl_set *set, __isl_take isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_terms_of_sign(__isl_keep isl_qpolynomial *poly, int *signs, int sign)
Definition isl_range.c:247
__isl_null isl_reordering * isl_reordering_free(__isl_take isl_reordering *exp)
__isl_give isl_reordering * isl_reordering_extend(__isl_take isl_reordering *exp, unsigned extra)
__isl_give isl_reordering * isl_parameter_alignment_reordering(__isl_keep isl_space *alignee, __isl_keep isl_space *aligner)
__isl_give isl_space * isl_reordering_get_space(__isl_keep isl_reordering *r)
__isl_give isl_reordering * isl_reordering_copy(__isl_keep isl_reordering *exp)
static int at(int i, void *user)
void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1, isl_int m2, isl_int *src2, unsigned len)
Definition isl_seq.c:116
int isl_seq_last_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:217
void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
Definition isl_seq.c:88
void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
Definition isl_seq.c:260
void isl_seq_clr(isl_int *p, unsigned len)
Definition isl_seq.c:14
void isl_seq_scale(isl_int *dst, isl_int *src, isl_int m, unsigned len)
Definition isl_seq.c:81
void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len, isl_int *m)
Definition isl_seq.c:146
int isl_seq_cmp(isl_int *p1, isl_int *p2, unsigned len)
Definition isl_seq.c:182
void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:42
int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
Definition isl_seq.c:173
int isl_seq_any_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:230
void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:35
isl_stat isl_space_check_is_set(__isl_keep isl_space *space)
Definition isl_space.c:83
isl_size isl_space_wrapped_dim(__isl_keep isl_space *space, enum isl_dim_type outer, enum isl_dim_type inner)
Definition isl_space.c:382
isl_size isl_space_offset(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:397
isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:2971
int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
Definition isl_space.c:3460
isl_stat isl_space_check_range(__isl_keep isl_space *space, enum isl_dim_type type, unsigned first, unsigned n)
const char * pwqp
Definition isl_test.c:3636
const char * poly
Definition isl_test.c:3868
enum isl_fold type
Definition isl_test.c:3867
const char * set
Definition isl_test.c:1364
int equal
Definition isl_test.c:7720
const char * offset
Definition isl_test.c:1577
const char * context
Definition isl_test.c:1792
const char * aff
Definition isl_test.c:7130
const char * res
Definition isl_test.c:783
const char * size
Definition isl_test.c:1578
const char * f
Definition isl_test.c:8453
static isl_stat power(__isl_take isl_map *map, void *user)
static __isl_give isl_union_map * total(__isl_take isl_union_map *umap, __isl_give isl_map *(*fn)(__isl_take isl_map *))
__isl_give isl_val * isl_val_rat_from_isl_int(isl_ctx *ctx, isl_int n, isl_int d)
Definition isl_val.c:202
t0 *a *b *t *a *b * t
isl_lp_result
Definition lp.h:17
@ isl_lp_error
Definition lp.h:18
@ isl_lp_empty
Definition lp.h:21
@ isl_lp_unbounded
Definition lp.h:20
__isl_give isl_basic_map * isl_basic_map_from_aff(__isl_take isl_aff *aff)
__isl_give isl_mat * isl_mat_copy(__isl_keep isl_mat *mat)
Definition isl_mat.c:202
__isl_give isl_mat * isl_mat_product(__isl_take isl_mat *left, __isl_take isl_mat *right)
Definition isl_mat.c:1271
__isl_give isl_mat * isl_mat_drop_rows(__isl_take isl_mat *mat, unsigned row, unsigned n)
Definition isl_mat.c:1527
__isl_give isl_mat * isl_mat_diagonal(__isl_take isl_mat *mat1, __isl_take isl_mat *mat2)
Definition isl_mat.c:921
__isl_give isl_mat * isl_mat_swap_rows(__isl_take isl_mat *mat, unsigned i, unsigned j)
Definition isl_mat.c:1248
__isl_null isl_mat * isl_mat_free(__isl_take isl_mat *mat)
Definition isl_mat.c:240
__isl_give isl_mat * isl_mat_insert_zero_cols(__isl_take isl_mat *mat, unsigned first, unsigned n)
Definition isl_mat.c:1568
void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
Definition isl_mat.c:1658
__isl_give isl_mat * isl_mat_alloc(isl_ctx *ctx, unsigned n_row, unsigned n_col)
Definition isl_mat.c:53
isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
Definition isl_mat.c:1800
__isl_give isl_mat * isl_mat_set_element_si(__isl_take isl_mat *mat, int row, int col, int v)
Definition isl_mat.c:356
__isl_give isl_mat * isl_mat_insert_cols(__isl_take isl_mat *mat, unsigned col, unsigned n)
Definition isl_mat.c:1543
__isl_give isl_mat * isl_mat_drop_cols(__isl_take isl_mat *mat, unsigned col, unsigned n)
Definition isl_mat.c:1506
isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
Definition isl_point.c:172
__isl_null isl_point * isl_point_free(__isl_take isl_point *pnt)
Definition isl_point.c:150
isl_ctx * isl_point_get_ctx(__isl_keep isl_point *pnt)
Definition isl_point.c:32
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_mul(__isl_take isl_pw_qpolynomial *pwqp1, __isl_take isl_pw_qpolynomial *pwqp2)
isl_bool isl_pw_qpolynomial_is_zero(__isl_keep isl_pw_qpolynomial *pwqp)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_zero(__isl_take isl_space *space)
__isl_null isl_pw_qpolynomial * isl_pw_qpolynomial_free(__isl_take isl_pw_qpolynomial *pwqp)
__isl_give isl_qpolynomial * isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
__isl_give isl_qpolynomial * isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
__isl_give isl_qpolynomial * isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_alloc(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp)
isl_stat isl_pw_qpolynomial_foreach_piece(__isl_keep isl_pw_qpolynomial *pwqp, isl_stat(*fn)(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp, void *user), void *user)
__isl_give isl_space * isl_pw_qpolynomial_get_space(__isl_keep isl_pw_qpolynomial *pwqp)
__isl_give isl_pw_qpolynomial * isl_pw_qpolynomial_add_disjoint(__isl_take isl_pw_qpolynomial *pwqp1, __isl_take isl_pw_qpolynomial *pwqp2)
struct isl_union_pw_qpolynomial isl_union_pw_qpolynomial
__isl_export __isl_give isl_set * isl_set_universe(__isl_take isl_space *space)
Definition isl_map.c:6985
isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition isl_map.c:202
__isl_export __isl_give isl_basic_set * isl_basic_set_detect_equalities(__isl_take isl_basic_set *bset)
isl_ctx * isl_set_get_ctx(__isl_keep isl_set *set)
Definition isl_map.c:397
__isl_export __isl_give isl_space * isl_set_get_space(__isl_keep isl_set *set)
Definition isl_map.c:604
isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
Definition isl_map.c:9823
__isl_give isl_space * isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
Definition isl_map.c:422
__isl_export __isl_give isl_set * isl_set_intersect_params(__isl_take isl_set *set, __isl_take isl_set *params)
Definition isl_map.c:4538
__isl_export __isl_give isl_basic_set * isl_basic_set_params(__isl_take isl_basic_set *bset)
Definition isl_map.c:6531
__isl_give isl_set * isl_set_eliminate(__isl_take isl_set *set, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:2747
__isl_null isl_basic_set * isl_basic_set_free(__isl_take isl_basic_set *bset)
Definition isl_map.c:1523
isl_bool isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Definition isl_map.c:10096
__isl_null isl_set * isl_set_free(__isl_take isl_set *set)
Definition isl_map.c:4055
__isl_give isl_set * isl_set_copy(__isl_keep isl_set *set)
Definition isl_map.c:1470
__isl_give isl_basic_set * isl_basic_set_remove_redundancies(__isl_take isl_basic_set *bset)
__isl_export isl_stat isl_set_foreach_point(__isl_keep isl_set *set, isl_stat(*fn)(__isl_take isl_point *pnt, void *user), void *user)
Definition isl_point.c:579
__isl_give isl_basic_set * isl_basic_set_add_dims(__isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned n)
Definition isl_map.c:4734
isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
Definition isl_map.c:132
isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
Definition isl_map.c:10039
__isl_export isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Definition isl_map.c:10123
__isl_export __isl_give isl_set * isl_set_intersect(__isl_take isl_set *set1, __isl_take isl_set *set2)
Definition isl_map.c:4521
__isl_constructor __isl_give isl_set * isl_set_from_basic_set(__isl_take isl_basic_set *bset)
Definition isl_map.c:4024
__isl_give isl_basic_set * isl_basic_set_copy(__isl_keep isl_basic_set *bset)
Definition isl_map.c:1465
__isl_export __isl_give isl_basic_set * isl_set_affine_hull(__isl_take isl_set *set)
__isl_give isl_set * isl_set_fix_si(__isl_take isl_set *set, enum isl_dim_type type, unsigned pos, int value)
Definition isl_map.c:7253
isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
Definition isl_space.c:1173
__isl_null isl_space * isl_space_free(__isl_take isl_space *space)
Definition isl_space.c:478
__isl_give isl_space * isl_space_from_domain(__isl_take isl_space *space)
Definition isl_space.c:2242
__isl_export __isl_give isl_space * isl_space_params(__isl_take isl_space *space)
Definition isl_space.c:2305
__isl_give isl_space * isl_space_insert_dims(__isl_take isl_space *space, enum isl_dim_type type, unsigned pos, unsigned n)
Definition isl_space.c:1345
isl_ctx * isl_space_get_ctx(__isl_keep isl_space *space)
Definition isl_space.c:23
__isl_give isl_space * isl_space_copy(__isl_keep isl_space *space)
Definition isl_space.c:469
__isl_give isl_space * isl_space_set_dim_name(__isl_take isl_space *space, enum isl_dim_type type, unsigned pos, __isl_keep const char *name)
__isl_export isl_bool isl_space_is_equal(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
Definition isl_space.c:2605
__isl_give isl_space * isl_space_drop_dims(__isl_take isl_space *space, enum isl_dim_type type, unsigned first, unsigned num)
Definition isl_space.c:2141
isl_size isl_space_dim(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:372
isl_bool isl_space_is_set(__isl_keep isl_space *space)
Definition isl_space.c:70
__isl_export __isl_give isl_space * isl_space_wrapped_reverse(__isl_take isl_space *space)
Definition isl_space.c:2098
__isl_give isl_space * isl_space_move_dims(__isl_take isl_space *space, enum isl_dim_type dst_type, unsigned dst_pos, enum isl_dim_type src_type, unsigned src_pos, unsigned n)
Definition isl_space.c:1422
__isl_give isl_space * isl_space_add_dims(__isl_take isl_space *space, enum isl_dim_type type, unsigned n)
Definition isl_space.c:1262
__isl_export __isl_give isl_space * isl_space_domain(__isl_take isl_space *space)
Definition isl_space.c:2232
isl_dim_type
Definition space_type.h:13
@ isl_dim_param
Definition space_type.h:15
@ isl_dim_in
Definition space_type.h:16
@ isl_dim_set
Definition space_type.h:18
@ isl_dim_all
Definition space_type.h:20
@ isl_dim_div
Definition space_type.h:19
@ isl_dim_out
Definition space_type.h:17
@ isl_dim_cst
Definition space_type.h:14
enum isl_error error
isl_int normalize_gcd
isl_int one
unsigned n_row
unsigned n_col
isl_int ** row
__isl_give isl_pw_qpolynomial *(* fn)(__isl_take isl_basic_set *bset)
isl_qpolynomial * qp
struct isl_poly poly
struct isl_ctx * ctx
struct isl_ctx * ctx
isl_pw_qpolynomial * res
struct isl_mat * div
isl_qpolynomial * qp
isl_pw_qpolynomial * res
isl_int n
isl_int * el
unsigned size
static Signature domain
__isl_give isl_val * isl_val_copy(__isl_keep isl_val *v)
Definition isl_val.c:219
__isl_export __isl_give isl_val * isl_val_inv(__isl_take isl_val *v)
Definition isl_val.c:429
__isl_export __isl_give isl_val * isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
Definition isl_val.c:598
__isl_export __isl_give isl_val * isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
Definition isl_val.c:626
__isl_export __isl_give isl_val * isl_val_one(isl_ctx *ctx)
Definition isl_val.c:48
__isl_export __isl_give isl_val * isl_val_zero(isl_ctx *ctx)
Definition isl_val.c:41
isl_ctx * isl_val_get_ctx(__isl_keep isl_val *val)
Definition isl_val.c:355
__isl_export __isl_give isl_val * isl_val_nan(isl_ctx *ctx)
Definition isl_val.c:62
__isl_null isl_val * isl_val_free(__isl_take isl_val *v)
Definition isl_val.c:263
__isl_export isl_bool isl_val_is_zero(__isl_keep isl_val *v)
Definition isl_val.c:1191
__isl_export isl_bool isl_val_is_one(__isl_keep isl_val *v)
Definition isl_val.c:1201
__isl_export __isl_give isl_val * isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
Definition isl_val.c:570
__isl_export isl_bool isl_val_is_rat(__isl_keep isl_val *v)
Definition isl_val.c:1151
__isl_export __isl_give isl_val * isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
Definition isl_val.c:782
__isl_null isl_vec * isl_vec_free(__isl_take isl_vec *vec)
Definition isl_vec.c:234
__isl_give isl_vec * isl_vec_copy(__isl_keep isl_vec *vec)
Definition isl_vec.c:198
__isl_give isl_vec * isl_vec_clr(__isl_take isl_vec *vec)
Definition isl_vec.c:426
__isl_give isl_vec * isl_vec_alloc(isl_ctx *ctx, unsigned size)
Definition isl_vec.c:33
n
Definition youcefn.c:8