Merge branch 'maint'
[platform/upstream/isl.git] / isl_polynomial.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 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_factorization.h>
13 #include <isl/lp.h>
14 #include <isl/seq.h>
15 #include <isl_union_map_private.h>
16 #include <isl_polynomial_private.h>
17 #include <isl_point_private.h>
18 #include <isl_dim_private.h>
19 #include <isl_map_private.h>
20 #include <isl_mat_private.h>
21 #include <isl_range.h>
22
23 static unsigned pos(__isl_keep isl_dim *dim, enum isl_dim_type type)
24 {
25         switch (type) {
26         case isl_dim_param:     return 0;
27         case isl_dim_in:        return dim->nparam;
28         case isl_dim_out:       return dim->nparam + dim->n_in;
29         default:                return 0;
30         }
31 }
32
33 int isl_upoly_is_cst(__isl_keep struct isl_upoly *up)
34 {
35         if (!up)
36                 return -1;
37
38         return up->var < 0;
39 }
40
41 __isl_keep struct isl_upoly_cst *isl_upoly_as_cst(__isl_keep struct isl_upoly *up)
42 {
43         if (!up)
44                 return NULL;
45
46         isl_assert(up->ctx, up->var < 0, return NULL);
47
48         return (struct isl_upoly_cst *)up;
49 }
50
51 __isl_keep struct isl_upoly_rec *isl_upoly_as_rec(__isl_keep struct isl_upoly *up)
52 {
53         if (!up)
54                 return NULL;
55
56         isl_assert(up->ctx, up->var >= 0, return NULL);
57
58         return (struct isl_upoly_rec *)up;
59 }
60
61 int isl_upoly_is_equal(__isl_keep struct isl_upoly *up1,
62         __isl_keep struct isl_upoly *up2)
63 {
64         int i;
65         struct isl_upoly_rec *rec1, *rec2;
66
67         if (!up1 || !up2)
68                 return -1;
69         if (up1 == up2)
70                 return 1;
71         if (up1->var != up2->var)
72                 return 0;
73         if (isl_upoly_is_cst(up1)) {
74                 struct isl_upoly_cst *cst1, *cst2;
75                 cst1 = isl_upoly_as_cst(up1);
76                 cst2 = isl_upoly_as_cst(up2);
77                 if (!cst1 || !cst2)
78                         return -1;
79                 return isl_int_eq(cst1->n, cst2->n) &&
80                        isl_int_eq(cst1->d, cst2->d);
81         }
82
83         rec1 = isl_upoly_as_rec(up1);
84         rec2 = isl_upoly_as_rec(up2);
85         if (!rec1 || !rec2)
86                 return -1;
87
88         if (rec1->n != rec2->n)
89                 return 0;
90
91         for (i = 0; i < rec1->n; ++i) {
92                 int eq = isl_upoly_is_equal(rec1->p[i], rec2->p[i]);
93                 if (eq < 0 || !eq)
94                         return eq;
95         }
96
97         return 1;
98 }
99
100 int isl_upoly_is_zero(__isl_keep struct isl_upoly *up)
101 {
102         struct isl_upoly_cst *cst;
103
104         if (!up)
105                 return -1;
106         if (!isl_upoly_is_cst(up))
107                 return 0;
108
109         cst = isl_upoly_as_cst(up);
110         if (!cst)
111                 return -1;
112
113         return isl_int_is_zero(cst->n) && isl_int_is_pos(cst->d);
114 }
115
116 int isl_upoly_sgn(__isl_keep struct isl_upoly *up)
117 {
118         struct isl_upoly_cst *cst;
119
120         if (!up)
121                 return 0;
122         if (!isl_upoly_is_cst(up))
123                 return 0;
124
125         cst = isl_upoly_as_cst(up);
126         if (!cst)
127                 return 0;
128
129         return isl_int_sgn(cst->n);
130 }
131
132 int isl_upoly_is_nan(__isl_keep struct isl_upoly *up)
133 {
134         struct isl_upoly_cst *cst;
135
136         if (!up)
137                 return -1;
138         if (!isl_upoly_is_cst(up))
139                 return 0;
140
141         cst = isl_upoly_as_cst(up);
142         if (!cst)
143                 return -1;
144
145         return isl_int_is_zero(cst->n) && isl_int_is_zero(cst->d);
146 }
147
148 int isl_upoly_is_infty(__isl_keep struct isl_upoly *up)
149 {
150         struct isl_upoly_cst *cst;
151
152         if (!up)
153                 return -1;
154         if (!isl_upoly_is_cst(up))
155                 return 0;
156
157         cst = isl_upoly_as_cst(up);
158         if (!cst)
159                 return -1;
160
161         return isl_int_is_pos(cst->n) && isl_int_is_zero(cst->d);
162 }
163
164 int isl_upoly_is_neginfty(__isl_keep struct isl_upoly *up)
165 {
166         struct isl_upoly_cst *cst;
167
168         if (!up)
169                 return -1;
170         if (!isl_upoly_is_cst(up))
171                 return 0;
172
173         cst = isl_upoly_as_cst(up);
174         if (!cst)
175                 return -1;
176
177         return isl_int_is_neg(cst->n) && isl_int_is_zero(cst->d);
178 }
179
180 int isl_upoly_is_one(__isl_keep struct isl_upoly *up)
181 {
182         struct isl_upoly_cst *cst;
183
184         if (!up)
185                 return -1;
186         if (!isl_upoly_is_cst(up))
187                 return 0;
188
189         cst = isl_upoly_as_cst(up);
190         if (!cst)
191                 return -1;
192
193         return isl_int_eq(cst->n, cst->d) && isl_int_is_pos(cst->d);
194 }
195
196 int isl_upoly_is_negone(__isl_keep struct isl_upoly *up)
197 {
198         struct isl_upoly_cst *cst;
199
200         if (!up)
201                 return -1;
202         if (!isl_upoly_is_cst(up))
203                 return 0;
204
205         cst = isl_upoly_as_cst(up);
206         if (!cst)
207                 return -1;
208
209         return isl_int_is_negone(cst->n) && isl_int_is_one(cst->d);
210 }
211
212 __isl_give struct isl_upoly_cst *isl_upoly_cst_alloc(struct isl_ctx *ctx)
213 {
214         struct isl_upoly_cst *cst;
215
216         cst = isl_alloc_type(ctx, struct isl_upoly_cst);
217         if (!cst)
218                 return NULL;
219
220         cst->up.ref = 1;
221         cst->up.ctx = ctx;
222         isl_ctx_ref(ctx);
223         cst->up.var = -1;
224
225         isl_int_init(cst->n);
226         isl_int_init(cst->d);
227
228         return cst;
229 }
230
231 __isl_give struct isl_upoly *isl_upoly_zero(struct isl_ctx *ctx)
232 {
233         struct isl_upoly_cst *cst;
234
235         cst = isl_upoly_cst_alloc(ctx);
236         if (!cst)
237                 return NULL;
238
239         isl_int_set_si(cst->n, 0);
240         isl_int_set_si(cst->d, 1);
241
242         return &cst->up;
243 }
244
245 __isl_give struct isl_upoly *isl_upoly_one(struct isl_ctx *ctx)
246 {
247         struct isl_upoly_cst *cst;
248
249         cst = isl_upoly_cst_alloc(ctx);
250         if (!cst)
251                 return NULL;
252
253         isl_int_set_si(cst->n, 1);
254         isl_int_set_si(cst->d, 1);
255
256         return &cst->up;
257 }
258
259 __isl_give struct isl_upoly *isl_upoly_infty(struct isl_ctx *ctx)
260 {
261         struct isl_upoly_cst *cst;
262
263         cst = isl_upoly_cst_alloc(ctx);
264         if (!cst)
265                 return NULL;
266
267         isl_int_set_si(cst->n, 1);
268         isl_int_set_si(cst->d, 0);
269
270         return &cst->up;
271 }
272
273 __isl_give struct isl_upoly *isl_upoly_neginfty(struct isl_ctx *ctx)
274 {
275         struct isl_upoly_cst *cst;
276
277         cst = isl_upoly_cst_alloc(ctx);
278         if (!cst)
279                 return NULL;
280
281         isl_int_set_si(cst->n, -1);
282         isl_int_set_si(cst->d, 0);
283
284         return &cst->up;
285 }
286
287 __isl_give struct isl_upoly *isl_upoly_nan(struct isl_ctx *ctx)
288 {
289         struct isl_upoly_cst *cst;
290
291         cst = isl_upoly_cst_alloc(ctx);
292         if (!cst)
293                 return NULL;
294
295         isl_int_set_si(cst->n, 0);
296         isl_int_set_si(cst->d, 0);
297
298         return &cst->up;
299 }
300
301 __isl_give struct isl_upoly *isl_upoly_rat_cst(struct isl_ctx *ctx,
302         isl_int n, isl_int d)
303 {
304         struct isl_upoly_cst *cst;
305
306         cst = isl_upoly_cst_alloc(ctx);
307         if (!cst)
308                 return NULL;
309
310         isl_int_set(cst->n, n);
311         isl_int_set(cst->d, d);
312
313         return &cst->up;
314 }
315
316 __isl_give struct isl_upoly_rec *isl_upoly_alloc_rec(struct isl_ctx *ctx,
317         int var, int size)
318 {
319         struct isl_upoly_rec *rec;
320
321         isl_assert(ctx, var >= 0, return NULL);
322         isl_assert(ctx, size >= 0, return NULL);
323         rec = isl_calloc(ctx, struct isl_upoly_rec,
324                         sizeof(struct isl_upoly_rec) +
325                         (size - 1) * sizeof(struct isl_upoly *));
326         if (!rec)
327                 return NULL;
328
329         rec->up.ref = 1;
330         rec->up.ctx = ctx;
331         isl_ctx_ref(ctx);
332         rec->up.var = var;
333
334         rec->n = 0;
335         rec->size = size;
336
337         return rec;
338 }
339
340 __isl_give isl_qpolynomial *isl_qpolynomial_reset_dim(
341         __isl_take isl_qpolynomial *qp, __isl_take isl_dim *dim)
342 {
343         qp = isl_qpolynomial_cow(qp);
344         if (!qp || !dim)
345                 goto error;
346
347         isl_dim_free(qp->dim);
348         qp->dim = dim;
349
350         return qp;
351 error:
352         isl_qpolynomial_free(qp);
353         isl_dim_free(dim);
354         return NULL;
355 }
356
357 isl_ctx *isl_qpolynomial_get_ctx(__isl_keep isl_qpolynomial *qp)
358 {
359         return qp ? qp->dim->ctx : NULL;
360 }
361
362 __isl_give isl_dim *isl_qpolynomial_get_dim(__isl_keep isl_qpolynomial *qp)
363 {
364         return qp ? isl_dim_copy(qp->dim) : NULL;
365 }
366
367 unsigned isl_qpolynomial_dim(__isl_keep isl_qpolynomial *qp,
368         enum isl_dim_type type)
369 {
370         return qp ? isl_dim_size(qp->dim, type) : 0;
371 }
372
373 int isl_qpolynomial_is_zero(__isl_keep isl_qpolynomial *qp)
374 {
375         return qp ? isl_upoly_is_zero(qp->upoly) : -1;
376 }
377
378 int isl_qpolynomial_is_one(__isl_keep isl_qpolynomial *qp)
379 {
380         return qp ? isl_upoly_is_one(qp->upoly) : -1;
381 }
382
383 int isl_qpolynomial_is_nan(__isl_keep isl_qpolynomial *qp)
384 {
385         return qp ? isl_upoly_is_nan(qp->upoly) : -1;
386 }
387
388 int isl_qpolynomial_is_infty(__isl_keep isl_qpolynomial *qp)
389 {
390         return qp ? isl_upoly_is_infty(qp->upoly) : -1;
391 }
392
393 int isl_qpolynomial_is_neginfty(__isl_keep isl_qpolynomial *qp)
394 {
395         return qp ? isl_upoly_is_neginfty(qp->upoly) : -1;
396 }
397
398 int isl_qpolynomial_sgn(__isl_keep isl_qpolynomial *qp)
399 {
400         return qp ? isl_upoly_sgn(qp->upoly) : 0;
401 }
402
403 static void upoly_free_cst(__isl_take struct isl_upoly_cst *cst)
404 {
405         isl_int_clear(cst->n);
406         isl_int_clear(cst->d);
407 }
408
409 static void upoly_free_rec(__isl_take struct isl_upoly_rec *rec)
410 {
411         int i;
412
413         for (i = 0; i < rec->n; ++i)
414                 isl_upoly_free(rec->p[i]);
415 }
416
417 __isl_give struct isl_upoly *isl_upoly_copy(__isl_keep struct isl_upoly *up)
418 {
419         if (!up)
420                 return NULL;
421
422         up->ref++;
423         return up;
424 }
425
426 __isl_give struct isl_upoly *isl_upoly_dup_cst(__isl_keep struct isl_upoly *up)
427 {
428         struct isl_upoly_cst *cst;
429         struct isl_upoly_cst *dup;
430
431         cst = isl_upoly_as_cst(up);
432         if (!cst)
433                 return NULL;
434
435         dup = isl_upoly_as_cst(isl_upoly_zero(up->ctx));
436         if (!dup)
437                 return NULL;
438         isl_int_set(dup->n, cst->n);
439         isl_int_set(dup->d, cst->d);
440
441         return &dup->up;
442 }
443
444 __isl_give struct isl_upoly *isl_upoly_dup_rec(__isl_keep struct isl_upoly *up)
445 {
446         int i;
447         struct isl_upoly_rec *rec;
448         struct isl_upoly_rec *dup;
449
450         rec = isl_upoly_as_rec(up);
451         if (!rec)
452                 return NULL;
453
454         dup = isl_upoly_alloc_rec(up->ctx, up->var, rec->n);
455         if (!dup)
456                 return NULL;
457
458         for (i = 0; i < rec->n; ++i) {
459                 dup->p[i] = isl_upoly_copy(rec->p[i]);
460                 if (!dup->p[i])
461                         goto error;
462                 dup->n++;
463         }
464
465         return &dup->up;
466 error:
467         isl_upoly_free(&dup->up);
468         return NULL;
469 }
470
471 __isl_give struct isl_upoly *isl_upoly_dup(__isl_keep struct isl_upoly *up)
472 {
473         struct isl_upoly *dup;
474
475         if (!up)
476                 return NULL;
477
478         if (isl_upoly_is_cst(up))
479                 return isl_upoly_dup_cst(up);
480         else
481                 return isl_upoly_dup_rec(up);
482 }
483
484 __isl_give struct isl_upoly *isl_upoly_cow(__isl_take struct isl_upoly *up)
485 {
486         if (!up)
487                 return NULL;
488
489         if (up->ref == 1)
490                 return up;
491         up->ref--;
492         return isl_upoly_dup(up);
493 }
494
495 void isl_upoly_free(__isl_take struct isl_upoly *up)
496 {
497         if (!up)
498                 return;
499
500         if (--up->ref > 0)
501                 return;
502
503         if (up->var < 0)
504                 upoly_free_cst((struct isl_upoly_cst *)up);
505         else
506                 upoly_free_rec((struct isl_upoly_rec *)up);
507
508         isl_ctx_deref(up->ctx);
509         free(up);
510 }
511
512 static void isl_upoly_cst_reduce(__isl_keep struct isl_upoly_cst *cst)
513 {
514         isl_int gcd;
515
516         isl_int_init(gcd);
517         isl_int_gcd(gcd, cst->n, cst->d);
518         if (!isl_int_is_zero(gcd) && !isl_int_is_one(gcd)) {
519                 isl_int_divexact(cst->n, cst->n, gcd);
520                 isl_int_divexact(cst->d, cst->d, gcd);
521         }
522         isl_int_clear(gcd);
523 }
524
525 __isl_give struct isl_upoly *isl_upoly_sum_cst(__isl_take struct isl_upoly *up1,
526         __isl_take struct isl_upoly *up2)
527 {
528         struct isl_upoly_cst *cst1;
529         struct isl_upoly_cst *cst2;
530
531         up1 = isl_upoly_cow(up1);
532         if (!up1 || !up2)
533                 goto error;
534
535         cst1 = isl_upoly_as_cst(up1);
536         cst2 = isl_upoly_as_cst(up2);
537
538         if (isl_int_eq(cst1->d, cst2->d))
539                 isl_int_add(cst1->n, cst1->n, cst2->n);
540         else {
541                 isl_int_mul(cst1->n, cst1->n, cst2->d);
542                 isl_int_addmul(cst1->n, cst2->n, cst1->d);
543                 isl_int_mul(cst1->d, cst1->d, cst2->d);
544         }
545
546         isl_upoly_cst_reduce(cst1);
547
548         isl_upoly_free(up2);
549         return up1;
550 error:
551         isl_upoly_free(up1);
552         isl_upoly_free(up2);
553         return NULL;
554 }
555
556 static __isl_give struct isl_upoly *replace_by_zero(
557         __isl_take struct isl_upoly *up)
558 {
559         struct isl_ctx *ctx;
560
561         if (!up)
562                 return NULL;
563         ctx = up->ctx;
564         isl_upoly_free(up);
565         return isl_upoly_zero(ctx);
566 }
567
568 static __isl_give struct isl_upoly *replace_by_constant_term(
569         __isl_take struct isl_upoly *up)
570 {
571         struct isl_upoly_rec *rec;
572         struct isl_upoly *cst;
573
574         if (!up)
575                 return NULL;
576
577         rec = isl_upoly_as_rec(up);
578         if (!rec)
579                 goto error;
580         cst = isl_upoly_copy(rec->p[0]);
581         isl_upoly_free(up);
582         return cst;
583 error:
584         isl_upoly_free(up);
585         return NULL;
586 }
587
588 __isl_give struct isl_upoly *isl_upoly_sum(__isl_take struct isl_upoly *up1,
589         __isl_take struct isl_upoly *up2)
590 {
591         int i;
592         struct isl_upoly_rec *rec1, *rec2;
593
594         if (!up1 || !up2)
595                 goto error;
596
597         if (isl_upoly_is_nan(up1)) {
598                 isl_upoly_free(up2);
599                 return up1;
600         }
601
602         if (isl_upoly_is_nan(up2)) {
603                 isl_upoly_free(up1);
604                 return up2;
605         }
606
607         if (isl_upoly_is_zero(up1)) {
608                 isl_upoly_free(up1);
609                 return up2;
610         }
611
612         if (isl_upoly_is_zero(up2)) {
613                 isl_upoly_free(up2);
614                 return up1;
615         }
616
617         if (up1->var < up2->var)
618                 return isl_upoly_sum(up2, up1);
619
620         if (up2->var < up1->var) {
621                 struct isl_upoly_rec *rec;
622                 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
623                         isl_upoly_free(up1);
624                         return up2;
625                 }
626                 up1 = isl_upoly_cow(up1);
627                 rec = isl_upoly_as_rec(up1);
628                 if (!rec)
629                         goto error;
630                 rec->p[0] = isl_upoly_sum(rec->p[0], up2);
631                 if (rec->n == 1)
632                         up1 = replace_by_constant_term(up1);
633                 return up1;
634         }
635
636         if (isl_upoly_is_cst(up1))
637                 return isl_upoly_sum_cst(up1, up2);
638
639         rec1 = isl_upoly_as_rec(up1);
640         rec2 = isl_upoly_as_rec(up2);
641         if (!rec1 || !rec2)
642                 goto error;
643
644         if (rec1->n < rec2->n)
645                 return isl_upoly_sum(up2, up1);
646
647         up1 = isl_upoly_cow(up1);
648         rec1 = isl_upoly_as_rec(up1);
649         if (!rec1)
650                 goto error;
651
652         for (i = rec2->n - 1; i >= 0; --i) {
653                 rec1->p[i] = isl_upoly_sum(rec1->p[i],
654                                             isl_upoly_copy(rec2->p[i]));
655                 if (!rec1->p[i])
656                         goto error;
657                 if (i == rec1->n - 1 && isl_upoly_is_zero(rec1->p[i])) {
658                         isl_upoly_free(rec1->p[i]);
659                         rec1->n--;
660                 }
661         }
662
663         if (rec1->n == 0)
664                 up1 = replace_by_zero(up1);
665         else if (rec1->n == 1)
666                 up1 = replace_by_constant_term(up1);
667
668         isl_upoly_free(up2);
669
670         return up1;
671 error:
672         isl_upoly_free(up1);
673         isl_upoly_free(up2);
674         return NULL;
675 }
676
677 __isl_give struct isl_upoly *isl_upoly_cst_add_isl_int(
678         __isl_take struct isl_upoly *up, isl_int v)
679 {
680         struct isl_upoly_cst *cst;
681
682         up = isl_upoly_cow(up);
683         if (!up)
684                 return NULL;
685
686         cst = isl_upoly_as_cst(up);
687
688         isl_int_addmul(cst->n, cst->d, v);
689
690         return up;
691 }
692
693 __isl_give struct isl_upoly *isl_upoly_add_isl_int(
694         __isl_take struct isl_upoly *up, isl_int v)
695 {
696         struct isl_upoly_rec *rec;
697
698         if (!up)
699                 return NULL;
700
701         if (isl_upoly_is_cst(up))
702                 return isl_upoly_cst_add_isl_int(up, v);
703
704         up = isl_upoly_cow(up);
705         rec = isl_upoly_as_rec(up);
706         if (!rec)
707                 goto error;
708
709         rec->p[0] = isl_upoly_add_isl_int(rec->p[0], v);
710         if (!rec->p[0])
711                 goto error;
712
713         return up;
714 error:
715         isl_upoly_free(up);
716         return NULL;
717 }
718
719 __isl_give struct isl_upoly *isl_upoly_neg_cst(__isl_take struct isl_upoly *up)
720 {
721         struct isl_upoly_cst *cst;
722
723         if (isl_upoly_is_zero(up))
724                 return up;
725
726         up = isl_upoly_cow(up);
727         if (!up)
728                 return NULL;
729
730         cst = isl_upoly_as_cst(up);
731
732         isl_int_neg(cst->n, cst->n);
733
734         return up;
735 }
736
737 __isl_give struct isl_upoly *isl_upoly_neg(__isl_take struct isl_upoly *up)
738 {
739         int i;
740         struct isl_upoly_rec *rec;
741
742         if (!up)
743                 return NULL;
744
745         if (isl_upoly_is_cst(up))
746                 return isl_upoly_neg_cst(up);
747
748         up = isl_upoly_cow(up);
749         rec = isl_upoly_as_rec(up);
750         if (!rec)
751                 goto error;
752
753         for (i = 0; i < rec->n; ++i) {
754                 rec->p[i] = isl_upoly_neg(rec->p[i]);
755                 if (!rec->p[i])
756                         goto error;
757         }
758
759         return up;
760 error:
761         isl_upoly_free(up);
762         return NULL;
763 }
764
765 __isl_give struct isl_upoly *isl_upoly_mul_cst(__isl_take struct isl_upoly *up1,
766         __isl_take struct isl_upoly *up2)
767 {
768         struct isl_upoly_cst *cst1;
769         struct isl_upoly_cst *cst2;
770
771         up1 = isl_upoly_cow(up1);
772         if (!up1 || !up2)
773                 goto error;
774
775         cst1 = isl_upoly_as_cst(up1);
776         cst2 = isl_upoly_as_cst(up2);
777
778         isl_int_mul(cst1->n, cst1->n, cst2->n);
779         isl_int_mul(cst1->d, cst1->d, cst2->d);
780
781         isl_upoly_cst_reduce(cst1);
782
783         isl_upoly_free(up2);
784         return up1;
785 error:
786         isl_upoly_free(up1);
787         isl_upoly_free(up2);
788         return NULL;
789 }
790
791 __isl_give struct isl_upoly *isl_upoly_mul_rec(__isl_take struct isl_upoly *up1,
792         __isl_take struct isl_upoly *up2)
793 {
794         struct isl_upoly_rec *rec1;
795         struct isl_upoly_rec *rec2;
796         struct isl_upoly_rec *res;
797         int i, j;
798         int size;
799
800         rec1 = isl_upoly_as_rec(up1);
801         rec2 = isl_upoly_as_rec(up2);
802         if (!rec1 || !rec2)
803                 goto error;
804         size = rec1->n + rec2->n - 1;
805         res = isl_upoly_alloc_rec(up1->ctx, up1->var, size);
806         if (!res)
807                 goto error;
808
809         for (i = 0; i < rec1->n; ++i) {
810                 res->p[i] = isl_upoly_mul(isl_upoly_copy(rec2->p[0]),
811                                             isl_upoly_copy(rec1->p[i]));
812                 if (!res->p[i])
813                         goto error;
814                 res->n++;
815         }
816         for (; i < size; ++i) {
817                 res->p[i] = isl_upoly_zero(up1->ctx);
818                 if (!res->p[i])
819                         goto error;
820                 res->n++;
821         }
822         for (i = 0; i < rec1->n; ++i) {
823                 for (j = 1; j < rec2->n; ++j) {
824                         struct isl_upoly *up;
825                         up = isl_upoly_mul(isl_upoly_copy(rec2->p[j]),
826                                             isl_upoly_copy(rec1->p[i]));
827                         res->p[i + j] = isl_upoly_sum(res->p[i + j], up);
828                         if (!res->p[i + j])
829                                 goto error;
830                 }
831         }
832
833         isl_upoly_free(up1);
834         isl_upoly_free(up2);
835
836         return &res->up;
837 error:
838         isl_upoly_free(up1);
839         isl_upoly_free(up2);
840         isl_upoly_free(&res->up);
841         return NULL;
842 }
843
844 __isl_give struct isl_upoly *isl_upoly_mul(__isl_take struct isl_upoly *up1,
845         __isl_take struct isl_upoly *up2)
846 {
847         if (!up1 || !up2)
848                 goto error;
849
850         if (isl_upoly_is_nan(up1)) {
851                 isl_upoly_free(up2);
852                 return up1;
853         }
854
855         if (isl_upoly_is_nan(up2)) {
856                 isl_upoly_free(up1);
857                 return up2;
858         }
859
860         if (isl_upoly_is_zero(up1)) {
861                 isl_upoly_free(up2);
862                 return up1;
863         }
864
865         if (isl_upoly_is_zero(up2)) {
866                 isl_upoly_free(up1);
867                 return up2;
868         }
869
870         if (isl_upoly_is_one(up1)) {
871                 isl_upoly_free(up1);
872                 return up2;
873         }
874
875         if (isl_upoly_is_one(up2)) {
876                 isl_upoly_free(up2);
877                 return up1;
878         }
879
880         if (up1->var < up2->var)
881                 return isl_upoly_mul(up2, up1);
882
883         if (up2->var < up1->var) {
884                 int i;
885                 struct isl_upoly_rec *rec;
886                 if (isl_upoly_is_infty(up2) || isl_upoly_is_neginfty(up2)) {
887                         isl_ctx *ctx = up1->ctx;
888                         isl_upoly_free(up1);
889                         isl_upoly_free(up2);
890                         return isl_upoly_nan(ctx);
891                 }
892                 up1 = isl_upoly_cow(up1);
893                 rec = isl_upoly_as_rec(up1);
894                 if (!rec)
895                         goto error;
896
897                 for (i = 0; i < rec->n; ++i) {
898                         rec->p[i] = isl_upoly_mul(rec->p[i],
899                                                     isl_upoly_copy(up2));
900                         if (!rec->p[i])
901                                 goto error;
902                 }
903                 isl_upoly_free(up2);
904                 return up1;
905         }
906
907         if (isl_upoly_is_cst(up1))
908                 return isl_upoly_mul_cst(up1, up2);
909
910         return isl_upoly_mul_rec(up1, up2);
911 error:
912         isl_upoly_free(up1);
913         isl_upoly_free(up2);
914         return NULL;
915 }
916
917 __isl_give struct isl_upoly *isl_upoly_pow(__isl_take struct isl_upoly *up,
918         unsigned power)
919 {
920         struct isl_upoly *res;
921
922         if (!up)
923                 return NULL;
924         if (power == 1)
925                 return up;
926
927         if (power % 2)
928                 res = isl_upoly_copy(up);
929         else
930                 res = isl_upoly_one(up->ctx);
931
932         while (power >>= 1) {
933                 up = isl_upoly_mul(up, isl_upoly_copy(up));
934                 if (power % 2)
935                         res = isl_upoly_mul(res, isl_upoly_copy(up));
936         }
937
938         isl_upoly_free(up);
939         return res;
940 }
941
942 __isl_give isl_qpolynomial *isl_qpolynomial_alloc(__isl_take isl_dim *dim,
943         unsigned n_div, __isl_take struct isl_upoly *up)
944 {
945         struct isl_qpolynomial *qp = NULL;
946         unsigned total;
947
948         if (!dim || !up)
949                 goto error;
950
951         total = isl_dim_total(dim);
952
953         qp = isl_calloc_type(dim->ctx, struct isl_qpolynomial);
954         if (!qp)
955                 goto error;
956
957         qp->ref = 1;
958         qp->div = isl_mat_alloc(dim->ctx, n_div, 1 + 1 + total + n_div);
959         if (!qp->div)
960                 goto error;
961
962         qp->dim = dim;
963         qp->upoly = up;
964
965         return qp;
966 error:
967         isl_dim_free(dim);
968         isl_upoly_free(up);
969         isl_qpolynomial_free(qp);
970         return NULL;
971 }
972
973 __isl_give isl_qpolynomial *isl_qpolynomial_copy(__isl_keep isl_qpolynomial *qp)
974 {
975         if (!qp)
976                 return NULL;
977
978         qp->ref++;
979         return qp;
980 }
981
982 __isl_give isl_qpolynomial *isl_qpolynomial_dup(__isl_keep isl_qpolynomial *qp)
983 {
984         struct isl_qpolynomial *dup;
985
986         if (!qp)
987                 return NULL;
988
989         dup = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row,
990                                     isl_upoly_copy(qp->upoly));
991         if (!dup)
992                 return NULL;
993         isl_mat_free(dup->div);
994         dup->div = isl_mat_copy(qp->div);
995         if (!dup->div)
996                 goto error;
997
998         return dup;
999 error:
1000         isl_qpolynomial_free(dup);
1001         return NULL;
1002 }
1003
1004 __isl_give isl_qpolynomial *isl_qpolynomial_cow(__isl_take isl_qpolynomial *qp)
1005 {
1006         if (!qp)
1007                 return NULL;
1008
1009         if (qp->ref == 1)
1010                 return qp;
1011         qp->ref--;
1012         return isl_qpolynomial_dup(qp);
1013 }
1014
1015 void isl_qpolynomial_free(__isl_take isl_qpolynomial *qp)
1016 {
1017         if (!qp)
1018                 return;
1019
1020         if (--qp->ref > 0)
1021                 return;
1022
1023         isl_dim_free(qp->dim);
1024         isl_mat_free(qp->div);
1025         isl_upoly_free(qp->upoly);
1026
1027         free(qp);
1028 }
1029
1030 __isl_give struct isl_upoly *isl_upoly_var_pow(isl_ctx *ctx, int pos, int power)
1031 {
1032         int i;
1033         struct isl_upoly *up;
1034         struct isl_upoly_rec *rec;
1035         struct isl_upoly_cst *cst;
1036
1037         rec = isl_upoly_alloc_rec(ctx, pos, 1 + power);
1038         if (!rec)
1039                 return NULL;
1040         for (i = 0; i < 1 + power; ++i) {
1041                 rec->p[i] = isl_upoly_zero(ctx);
1042                 if (!rec->p[i])
1043                         goto error;
1044                 rec->n++;
1045         }
1046         cst = isl_upoly_as_cst(rec->p[power]);
1047         isl_int_set_si(cst->n, 1);
1048
1049         return &rec->up;
1050 error:
1051         isl_upoly_free(&rec->up);
1052         return NULL;
1053 }
1054
1055 /* r array maps original positions to new positions.
1056  */
1057 static __isl_give struct isl_upoly *reorder(__isl_take struct isl_upoly *up,
1058         int *r)
1059 {
1060         int i;
1061         struct isl_upoly_rec *rec;
1062         struct isl_upoly *base;
1063         struct isl_upoly *res;
1064
1065         if (isl_upoly_is_cst(up))
1066                 return up;
1067
1068         rec = isl_upoly_as_rec(up);
1069         if (!rec)
1070                 goto error;
1071
1072         isl_assert(up->ctx, rec->n >= 1, goto error);
1073
1074         base = isl_upoly_var_pow(up->ctx, r[up->var], 1);
1075         res = reorder(isl_upoly_copy(rec->p[rec->n - 1]), r);
1076
1077         for (i = rec->n - 2; i >= 0; --i) {
1078                 res = isl_upoly_mul(res, isl_upoly_copy(base));
1079                 res = isl_upoly_sum(res, reorder(isl_upoly_copy(rec->p[i]), r));
1080         }
1081
1082         isl_upoly_free(base);
1083         isl_upoly_free(up);
1084
1085         return res;
1086 error:
1087         isl_upoly_free(up);
1088         return NULL;
1089 }
1090
1091 static int compatible_divs(__isl_keep isl_mat *div1, __isl_keep isl_mat *div2)
1092 {
1093         int n_row, n_col;
1094         int equal;
1095
1096         isl_assert(div1->ctx, div1->n_row >= div2->n_row &&
1097                                 div1->n_col >= div2->n_col, return -1);
1098
1099         if (div1->n_row == div2->n_row)
1100                 return isl_mat_is_equal(div1, div2);
1101
1102         n_row = div1->n_row;
1103         n_col = div1->n_col;
1104         div1->n_row = div2->n_row;
1105         div1->n_col = div2->n_col;
1106
1107         equal = isl_mat_is_equal(div1, div2);
1108
1109         div1->n_row = n_row;
1110         div1->n_col = n_col;
1111
1112         return equal;
1113 }
1114
1115 static void expand_row(__isl_keep isl_mat *dst, int d,
1116         __isl_keep isl_mat *src, int s, int *exp)
1117 {
1118         int i;
1119         unsigned c = src->n_col - src->n_row;
1120
1121         isl_seq_cpy(dst->row[d], src->row[s], c);
1122         isl_seq_clr(dst->row[d] + c, dst->n_col - c);
1123
1124         for (i = 0; i < s; ++i)
1125                 isl_int_set(dst->row[d][c + exp[i]], src->row[s][c + i]);
1126 }
1127
1128 static int cmp_row(__isl_keep isl_mat *div, int i, int j)
1129 {
1130         int li, lj;
1131
1132         li = isl_seq_last_non_zero(div->row[i], div->n_col);
1133         lj = isl_seq_last_non_zero(div->row[j], div->n_col);
1134
1135         if (li != lj)
1136                 return li - lj;
1137
1138         return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
1139 }
1140
1141 struct isl_div_sort_info {
1142         isl_mat *div;
1143         int      row;
1144 };
1145
1146 static int div_sort_cmp(const void *p1, const void *p2)
1147 {
1148         const struct isl_div_sort_info *i1, *i2;
1149         i1 = (const struct isl_div_sort_info *) p1;
1150         i2 = (const struct isl_div_sort_info *) p2;
1151
1152         return cmp_row(i1->div, i1->row, i2->row);
1153 }
1154
1155 /* Sort divs and remove duplicates.
1156  */
1157 static __isl_give isl_qpolynomial *sort_divs(__isl_take isl_qpolynomial *qp)
1158 {
1159         int i;
1160         int skip;
1161         int len;
1162         struct isl_div_sort_info *array = NULL;
1163         int *pos = NULL, *at = NULL;
1164         int *reordering = NULL;
1165         unsigned div_pos;
1166
1167         if (!qp)
1168                 return NULL;
1169         if (qp->div->n_row <= 1)
1170                 return qp;
1171
1172         div_pos = isl_dim_total(qp->dim);
1173
1174         array = isl_alloc_array(qp->div->ctx, struct isl_div_sort_info,
1175                                 qp->div->n_row);
1176         pos = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1177         at = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
1178         len = qp->div->n_col - 2;
1179         reordering = isl_alloc_array(qp->div->ctx, int, len);
1180         if (!array || !pos || !at || !reordering)
1181                 goto error;
1182
1183         for (i = 0; i < qp->div->n_row; ++i) {
1184                 array[i].div = qp->div;
1185                 array[i].row = i;
1186                 pos[i] = i;
1187                 at[i] = i;
1188         }
1189
1190         qsort(array, qp->div->n_row, sizeof(struct isl_div_sort_info),
1191                 div_sort_cmp);
1192
1193         for (i = 0; i < div_pos; ++i)
1194                 reordering[i] = i;
1195
1196         for (i = 0; i < qp->div->n_row; ++i) {
1197                 if (pos[array[i].row] == i)
1198                         continue;
1199                 qp->div = isl_mat_swap_rows(qp->div, i, pos[array[i].row]);
1200                 pos[at[i]] = pos[array[i].row];
1201                 at[pos[array[i].row]] = at[i];
1202                 at[i] = array[i].row;
1203                 pos[array[i].row] = i;
1204         }
1205
1206         skip = 0;
1207         for (i = 0; i < len - div_pos; ++i) {
1208                 if (i > 0 &&
1209                     isl_seq_eq(qp->div->row[i - skip - 1],
1210                                qp->div->row[i - skip], qp->div->n_col)) {
1211                         qp->div = isl_mat_drop_rows(qp->div, i - skip, 1);
1212                         isl_mat_col_add(qp->div, 2 + div_pos + i - skip - 1,
1213                                                  2 + div_pos + i - skip);
1214                         qp->div = isl_mat_drop_cols(qp->div,
1215                                                     2 + div_pos + i - skip, 1);
1216                         skip++;
1217                 }
1218                 reordering[div_pos + array[i].row] = div_pos + i - skip;
1219         }
1220
1221         qp->upoly = reorder(qp->upoly, reordering);
1222
1223         if (!qp->upoly || !qp->div)
1224                 goto error;
1225
1226         free(at);
1227         free(pos);
1228         free(array);
1229         free(reordering);
1230
1231         return qp;
1232 error:
1233         free(at);
1234         free(pos);
1235         free(array);
1236         free(reordering);
1237         isl_qpolynomial_free(qp);
1238         return NULL;
1239 }
1240
1241 static __isl_give isl_mat *merge_divs(__isl_keep isl_mat *div1,
1242         __isl_keep isl_mat *div2, int *exp1, int *exp2)
1243 {
1244         int i, j, k;
1245         isl_mat *div = NULL;
1246         unsigned d = div1->n_col - div1->n_row;
1247
1248         div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
1249                                 d + div1->n_row + div2->n_row);
1250         if (!div)
1251                 return NULL;
1252
1253         for (i = 0, j = 0, k = 0; i < div1->n_row && j < div2->n_row; ++k) {
1254                 int cmp;
1255
1256                 expand_row(div, k, div1, i, exp1);
1257                 expand_row(div, k + 1, div2, j, exp2);
1258
1259                 cmp = cmp_row(div, k, k + 1);
1260                 if (cmp == 0) {
1261                         exp1[i++] = k;
1262                         exp2[j++] = k;
1263                 } else if (cmp < 0) {
1264                         exp1[i++] = k;
1265                 } else {
1266                         exp2[j++] = k;
1267                         isl_seq_cpy(div->row[k], div->row[k + 1], div->n_col);
1268                 }
1269         }
1270         for (; i < div1->n_row; ++i, ++k) {
1271                 expand_row(div, k, div1, i, exp1);
1272                 exp1[i] = k;
1273         }
1274         for (; j < div2->n_row; ++j, ++k) {
1275                 expand_row(div, k, div2, j, exp2);
1276                 exp2[j] = k;
1277         }
1278
1279         div->n_row = k;
1280         div->n_col = d + k;
1281
1282         return div;
1283 }
1284
1285 static __isl_give struct isl_upoly *expand(__isl_take struct isl_upoly *up,
1286         int *exp, int first)
1287 {
1288         int i;
1289         struct isl_upoly_rec *rec;
1290
1291         if (isl_upoly_is_cst(up))
1292                 return up;
1293
1294         if (up->var < first)
1295                 return up;
1296
1297         if (exp[up->var - first] == up->var - first)
1298                 return up;
1299
1300         up = isl_upoly_cow(up);
1301         if (!up)
1302                 goto error;
1303
1304         up->var = exp[up->var - first] + first;
1305
1306         rec = isl_upoly_as_rec(up);
1307         if (!rec)
1308                 goto error;
1309
1310         for (i = 0; i < rec->n; ++i) {
1311                 rec->p[i] = expand(rec->p[i], exp, first);
1312                 if (!rec->p[i])
1313                         goto error;
1314         }
1315
1316         return up;
1317 error:
1318         isl_upoly_free(up);
1319         return NULL;
1320 }
1321
1322 static __isl_give isl_qpolynomial *with_merged_divs(
1323         __isl_give isl_qpolynomial *(*fn)(__isl_take isl_qpolynomial *qp1,
1324                                           __isl_take isl_qpolynomial *qp2),
1325         __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
1326 {
1327         int *exp1 = NULL;
1328         int *exp2 = NULL;
1329         isl_mat *div = NULL;
1330
1331         qp1 = isl_qpolynomial_cow(qp1);
1332         qp2 = isl_qpolynomial_cow(qp2);
1333
1334         if (!qp1 || !qp2)
1335                 goto error;
1336
1337         isl_assert(qp1->div->ctx, qp1->div->n_row >= qp2->div->n_row &&
1338                                 qp1->div->n_col >= qp2->div->n_col, goto error);
1339
1340         exp1 = isl_alloc_array(qp1->div->ctx, int, qp1->div->n_row);
1341         exp2 = isl_alloc_array(qp2->div->ctx, int, qp2->div->n_row);
1342         if (!exp1 || !exp2)
1343                 goto error;
1344
1345         div = merge_divs(qp1->div, qp2->div, exp1, exp2);
1346         if (!div)
1347                 goto error;
1348
1349         isl_mat_free(qp1->div);
1350         qp1->div = isl_mat_copy(div);
1351         isl_mat_free(qp2->div);
1352         qp2->div = isl_mat_copy(div);
1353
1354         qp1->upoly = expand(qp1->upoly, exp1, div->n_col - div->n_row - 2);
1355         qp2->upoly = expand(qp2->upoly, exp2, div->n_col - div->n_row - 2);
1356
1357         if (!qp1->upoly || !qp2->upoly)
1358                 goto error;
1359
1360         isl_mat_free(div);
1361         free(exp1);
1362         free(exp2);
1363
1364         return fn(qp1, qp2);
1365 error:
1366         isl_mat_free(div);
1367         free(exp1);
1368         free(exp2);
1369         isl_qpolynomial_free(qp1);
1370         isl_qpolynomial_free(qp2);
1371         return NULL;
1372 }
1373
1374 __isl_give isl_qpolynomial *isl_qpolynomial_add(__isl_take isl_qpolynomial *qp1,
1375         __isl_take isl_qpolynomial *qp2)
1376 {
1377         qp1 = isl_qpolynomial_cow(qp1);
1378
1379         if (!qp1 || !qp2)
1380                 goto error;
1381
1382         if (qp1->div->n_row < qp2->div->n_row)
1383                 return isl_qpolynomial_add(qp2, qp1);
1384
1385         isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1386         if (!compatible_divs(qp1->div, qp2->div))
1387                 return with_merged_divs(isl_qpolynomial_add, qp1, qp2);
1388
1389         qp1->upoly = isl_upoly_sum(qp1->upoly, isl_upoly_copy(qp2->upoly));
1390         if (!qp1->upoly)
1391                 goto error;
1392
1393         isl_qpolynomial_free(qp2);
1394
1395         return qp1;
1396 error:
1397         isl_qpolynomial_free(qp1);
1398         isl_qpolynomial_free(qp2);
1399         return NULL;
1400 }
1401
1402 __isl_give isl_qpolynomial *isl_qpolynomial_add_on_domain(
1403         __isl_keep isl_set *dom,
1404         __isl_take isl_qpolynomial *qp1,
1405         __isl_take isl_qpolynomial *qp2)
1406 {
1407         qp1 = isl_qpolynomial_add(qp1, qp2);
1408         qp1 = isl_qpolynomial_gist(qp1, isl_set_copy(dom));
1409         return qp1;
1410 }
1411
1412 __isl_give isl_qpolynomial *isl_qpolynomial_sub(__isl_take isl_qpolynomial *qp1,
1413         __isl_take isl_qpolynomial *qp2)
1414 {
1415         return isl_qpolynomial_add(qp1, isl_qpolynomial_neg(qp2));
1416 }
1417
1418 __isl_give isl_qpolynomial *isl_qpolynomial_add_isl_int(
1419         __isl_take isl_qpolynomial *qp, isl_int v)
1420 {
1421         if (isl_int_is_zero(v))
1422                 return qp;
1423
1424         qp = isl_qpolynomial_cow(qp);
1425         if (!qp)
1426                 return NULL;
1427
1428         qp->upoly = isl_upoly_add_isl_int(qp->upoly, v);
1429         if (!qp->upoly)
1430                 goto error;
1431
1432         return qp;
1433 error:
1434         isl_qpolynomial_free(qp);
1435         return NULL;
1436
1437 }
1438
1439 __isl_give isl_qpolynomial *isl_qpolynomial_neg(__isl_take isl_qpolynomial *qp)
1440 {
1441         qp = isl_qpolynomial_cow(qp);
1442
1443         if (!qp)
1444                 return NULL;
1445
1446         qp->upoly = isl_upoly_neg(qp->upoly);
1447         if (!qp->upoly)
1448                 goto error;
1449
1450         return qp;
1451 error:
1452         isl_qpolynomial_free(qp);
1453         return NULL;
1454 }
1455
1456 __isl_give isl_qpolynomial *isl_qpolynomial_mul(__isl_take isl_qpolynomial *qp1,
1457         __isl_take isl_qpolynomial *qp2)
1458 {
1459         qp1 = isl_qpolynomial_cow(qp1);
1460
1461         if (!qp1 || !qp2)
1462                 goto error;
1463
1464         if (qp1->div->n_row < qp2->div->n_row)
1465                 return isl_qpolynomial_mul(qp2, qp1);
1466
1467         isl_assert(qp1->dim->ctx, isl_dim_equal(qp1->dim, qp2->dim), goto error);
1468         if (!compatible_divs(qp1->div, qp2->div))
1469                 return with_merged_divs(isl_qpolynomial_mul, qp1, qp2);
1470
1471         qp1->upoly = isl_upoly_mul(qp1->upoly, isl_upoly_copy(qp2->upoly));
1472         if (!qp1->upoly)
1473                 goto error;
1474
1475         isl_qpolynomial_free(qp2);
1476
1477         return qp1;
1478 error:
1479         isl_qpolynomial_free(qp1);
1480         isl_qpolynomial_free(qp2);
1481         return NULL;
1482 }
1483
1484 __isl_give isl_qpolynomial *isl_qpolynomial_pow(__isl_take isl_qpolynomial *qp,
1485         unsigned power)
1486 {
1487         qp = isl_qpolynomial_cow(qp);
1488
1489         if (!qp)
1490                 return NULL;
1491
1492         qp->upoly = isl_upoly_pow(qp->upoly, power);
1493         if (!qp->upoly)
1494                 goto error;
1495
1496         return qp;
1497 error:
1498         isl_qpolynomial_free(qp);
1499         return NULL;
1500 }
1501
1502 __isl_give isl_qpolynomial *isl_qpolynomial_zero(__isl_take isl_dim *dim)
1503 {
1504         return isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1505 }
1506
1507 __isl_give isl_qpolynomial *isl_qpolynomial_one(__isl_take isl_dim *dim)
1508 {
1509         return isl_qpolynomial_alloc(dim, 0, isl_upoly_one(dim->ctx));
1510 }
1511
1512 __isl_give isl_qpolynomial *isl_qpolynomial_infty(__isl_take isl_dim *dim)
1513 {
1514         return isl_qpolynomial_alloc(dim, 0, isl_upoly_infty(dim->ctx));
1515 }
1516
1517 __isl_give isl_qpolynomial *isl_qpolynomial_neginfty(__isl_take isl_dim *dim)
1518 {
1519         return isl_qpolynomial_alloc(dim, 0, isl_upoly_neginfty(dim->ctx));
1520 }
1521
1522 __isl_give isl_qpolynomial *isl_qpolynomial_nan(__isl_take isl_dim *dim)
1523 {
1524         return isl_qpolynomial_alloc(dim, 0, isl_upoly_nan(dim->ctx));
1525 }
1526
1527 __isl_give isl_qpolynomial *isl_qpolynomial_cst(__isl_take isl_dim *dim,
1528         isl_int v)
1529 {
1530         struct isl_qpolynomial *qp;
1531         struct isl_upoly_cst *cst;
1532
1533         qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
1534         if (!qp)
1535                 return NULL;
1536
1537         cst = isl_upoly_as_cst(qp->upoly);
1538         isl_int_set(cst->n, v);
1539
1540         return qp;
1541 }
1542
1543 int isl_qpolynomial_is_cst(__isl_keep isl_qpolynomial *qp,
1544         isl_int *n, isl_int *d)
1545 {
1546         struct isl_upoly_cst *cst;
1547
1548         if (!qp)
1549                 return -1;
1550
1551         if (!isl_upoly_is_cst(qp->upoly))
1552                 return 0;
1553
1554         cst = isl_upoly_as_cst(qp->upoly);
1555         if (!cst)
1556                 return -1;
1557
1558         if (n)
1559                 isl_int_set(*n, cst->n);
1560         if (d)
1561                 isl_int_set(*d, cst->d);
1562
1563         return 1;
1564 }
1565
1566 int isl_upoly_is_affine(__isl_keep struct isl_upoly *up)
1567 {
1568         int is_cst;
1569         struct isl_upoly_rec *rec;
1570
1571         if (!up)
1572                 return -1;
1573
1574         if (up->var < 0)
1575                 return 1;
1576
1577         rec = isl_upoly_as_rec(up);
1578         if (!rec)
1579                 return -1;
1580
1581         if (rec->n > 2)
1582                 return 0;
1583
1584         isl_assert(up->ctx, rec->n > 1, return -1);
1585
1586         is_cst = isl_upoly_is_cst(rec->p[1]);
1587         if (is_cst < 0)
1588                 return -1;
1589         if (!is_cst)
1590                 return 0;
1591
1592         return isl_upoly_is_affine(rec->p[0]);
1593 }
1594
1595 int isl_qpolynomial_is_affine(__isl_keep isl_qpolynomial *qp)
1596 {
1597         if (!qp)
1598                 return -1;
1599
1600         if (qp->div->n_row > 0)
1601                 return 0;
1602
1603         return isl_upoly_is_affine(qp->upoly);
1604 }
1605
1606 static void update_coeff(__isl_keep isl_vec *aff,
1607         __isl_keep struct isl_upoly_cst *cst, int pos)
1608 {
1609         isl_int gcd;
1610         isl_int f;
1611
1612         if (isl_int_is_zero(cst->n))
1613                 return;
1614
1615         isl_int_init(gcd);
1616         isl_int_init(f);
1617         isl_int_gcd(gcd, cst->d, aff->el[0]);
1618         isl_int_divexact(f, cst->d, gcd);
1619         isl_int_divexact(gcd, aff->el[0], gcd);
1620         isl_seq_scale(aff->el, aff->el, f, aff->size);
1621         isl_int_mul(aff->el[1 + pos], gcd, cst->n);
1622         isl_int_clear(gcd);
1623         isl_int_clear(f);
1624 }
1625
1626 int isl_upoly_update_affine(__isl_keep struct isl_upoly *up,
1627         __isl_keep isl_vec *aff)
1628 {
1629         struct isl_upoly_cst *cst;
1630         struct isl_upoly_rec *rec;
1631
1632         if (!up || !aff)
1633                 return -1;
1634
1635         if (up->var < 0) {
1636                 struct isl_upoly_cst *cst;
1637
1638                 cst = isl_upoly_as_cst(up);
1639                 if (!cst)
1640                         return -1;
1641                 update_coeff(aff, cst, 0);
1642                 return 0;
1643         }
1644
1645         rec = isl_upoly_as_rec(up);
1646         if (!rec)
1647                 return -1;
1648         isl_assert(up->ctx, rec->n == 2, return -1);
1649
1650         cst = isl_upoly_as_cst(rec->p[1]);
1651         if (!cst)
1652                 return -1;
1653         update_coeff(aff, cst, 1 + up->var);
1654
1655         return isl_upoly_update_affine(rec->p[0], aff);
1656 }
1657
1658 __isl_give isl_vec *isl_qpolynomial_extract_affine(
1659         __isl_keep isl_qpolynomial *qp)
1660 {
1661         isl_vec *aff;
1662         unsigned d;
1663
1664         if (!qp)
1665                 return NULL;
1666
1667         isl_assert(qp->div->ctx, qp->div->n_row == 0, return NULL);
1668         d = isl_dim_total(qp->dim);
1669         aff = isl_vec_alloc(qp->div->ctx, 2 + d);
1670         if (!aff)
1671                 return NULL;
1672
1673         isl_seq_clr(aff->el + 1, 1 + d);
1674         isl_int_set_si(aff->el[0], 1);
1675
1676         if (isl_upoly_update_affine(qp->upoly, aff) < 0)
1677                 goto error;
1678
1679         return aff;
1680 error:
1681         isl_vec_free(aff);
1682         return NULL;
1683 }
1684
1685 int isl_qpolynomial_is_equal(__isl_keep isl_qpolynomial *qp1,
1686         __isl_keep isl_qpolynomial *qp2)
1687 {
1688         if (!qp1 || !qp2)
1689                 return -1;
1690
1691         return isl_upoly_is_equal(qp1->upoly, qp2->upoly);
1692 }
1693
1694 static void upoly_update_den(__isl_keep struct isl_upoly *up, isl_int *d)
1695 {
1696         int i;
1697         struct isl_upoly_rec *rec;
1698
1699         if (isl_upoly_is_cst(up)) {
1700                 struct isl_upoly_cst *cst;
1701                 cst = isl_upoly_as_cst(up);
1702                 if (!cst)
1703                         return;
1704                 isl_int_lcm(*d, *d, cst->d);
1705                 return;
1706         }
1707
1708         rec = isl_upoly_as_rec(up);
1709         if (!rec)
1710                 return;
1711
1712         for (i = 0; i < rec->n; ++i)
1713                 upoly_update_den(rec->p[i], d);
1714 }
1715
1716 void isl_qpolynomial_get_den(__isl_keep isl_qpolynomial *qp, isl_int *d)
1717 {
1718         isl_int_set_si(*d, 1);
1719         if (!qp)
1720                 return;
1721         upoly_update_den(qp->upoly, d);
1722 }
1723
1724 __isl_give isl_qpolynomial *isl_qpolynomial_var_pow(__isl_take isl_dim *dim,
1725         int pos, int power)
1726 {
1727         struct isl_ctx *ctx;
1728
1729         if (!dim)
1730                 return NULL;
1731
1732         ctx = dim->ctx;
1733
1734         return isl_qpolynomial_alloc(dim, 0, isl_upoly_var_pow(ctx, pos, power));
1735 }
1736
1737 __isl_give isl_qpolynomial *isl_qpolynomial_var(__isl_take isl_dim *dim,
1738         enum isl_dim_type type, unsigned pos)
1739 {
1740         if (!dim)
1741                 return NULL;
1742
1743         isl_assert(dim->ctx, isl_dim_size(dim, isl_dim_in) == 0, goto error);
1744         isl_assert(dim->ctx, pos < isl_dim_size(dim, type), goto error);
1745
1746         if (type == isl_dim_set)
1747                 pos += isl_dim_size(dim, isl_dim_param);
1748
1749         return isl_qpolynomial_var_pow(dim, pos, 1);
1750 error:
1751         isl_dim_free(dim);
1752         return NULL;
1753 }
1754
1755 __isl_give struct isl_upoly *isl_upoly_subs(__isl_take struct isl_upoly *up,
1756         unsigned first, unsigned n, __isl_keep struct isl_upoly **subs)
1757 {
1758         int i;
1759         struct isl_upoly_rec *rec;
1760         struct isl_upoly *base, *res;
1761
1762         if (!up)
1763                 return NULL;
1764
1765         if (isl_upoly_is_cst(up))
1766                 return up;
1767
1768         if (up->var < first)
1769                 return up;
1770
1771         rec = isl_upoly_as_rec(up);
1772         if (!rec)
1773                 goto error;
1774
1775         isl_assert(up->ctx, rec->n >= 1, goto error);
1776
1777         if (up->var >= first + n)
1778                 base = isl_upoly_var_pow(up->ctx, up->var, 1);
1779         else
1780                 base = isl_upoly_copy(subs[up->var - first]);
1781
1782         res = isl_upoly_subs(isl_upoly_copy(rec->p[rec->n - 1]), first, n, subs);
1783         for (i = rec->n - 2; i >= 0; --i) {
1784                 struct isl_upoly *t;
1785                 t = isl_upoly_subs(isl_upoly_copy(rec->p[i]), first, n, subs);
1786                 res = isl_upoly_mul(res, isl_upoly_copy(base));
1787                 res = isl_upoly_sum(res, t);
1788         }
1789
1790         isl_upoly_free(base);
1791         isl_upoly_free(up);
1792                                 
1793         return res;
1794 error:
1795         isl_upoly_free(up);
1796         return NULL;
1797 }       
1798
1799 __isl_give struct isl_upoly *isl_upoly_from_affine(isl_ctx *ctx, isl_int *f,
1800         isl_int denom, unsigned len)
1801 {
1802         int i;
1803         struct isl_upoly *up;
1804
1805         isl_assert(ctx, len >= 1, return NULL);
1806
1807         up = isl_upoly_rat_cst(ctx, f[0], denom);
1808         for (i = 0; i < len - 1; ++i) {
1809                 struct isl_upoly *t;
1810                 struct isl_upoly *c;
1811
1812                 if (isl_int_is_zero(f[1 + i]))
1813                         continue;
1814
1815                 c = isl_upoly_rat_cst(ctx, f[1 + i], denom);
1816                 t = isl_upoly_var_pow(ctx, i, 1);
1817                 t = isl_upoly_mul(c, t);
1818                 up = isl_upoly_sum(up, t);
1819         }
1820
1821         return up;
1822 }
1823
1824 /* Remove common factor of non-constant terms and denominator.
1825  */
1826 static void normalize_div(__isl_keep isl_qpolynomial *qp, int div)
1827 {
1828         isl_ctx *ctx = qp->div->ctx;
1829         unsigned total = qp->div->n_col - 2;
1830
1831         isl_seq_gcd(qp->div->row[div] + 2, total, &ctx->normalize_gcd);
1832         isl_int_gcd(ctx->normalize_gcd,
1833                     ctx->normalize_gcd, qp->div->row[div][0]);
1834         if (isl_int_is_one(ctx->normalize_gcd))
1835                 return;
1836
1837         isl_seq_scale_down(qp->div->row[div] + 2, qp->div->row[div] + 2,
1838                             ctx->normalize_gcd, total);
1839         isl_int_divexact(qp->div->row[div][0], qp->div->row[div][0],
1840                             ctx->normalize_gcd);
1841         isl_int_fdiv_q(qp->div->row[div][1], qp->div->row[div][1],
1842                             ctx->normalize_gcd);
1843 }
1844
1845 /* Replace the integer division identified by "div" by the polynomial "s".
1846  * The integer division is assumed not to appear in the definition
1847  * of any other integer divisions.
1848  */
1849 static __isl_give isl_qpolynomial *substitute_div(
1850         __isl_take isl_qpolynomial *qp,
1851         int div, __isl_take struct isl_upoly *s)
1852 {
1853         int i;
1854         int total;
1855         int *reordering;
1856
1857         if (!qp || !s)
1858                 goto error;
1859
1860         qp = isl_qpolynomial_cow(qp);
1861         if (!qp)
1862                 goto error;
1863
1864         total = isl_dim_total(qp->dim);
1865         qp->upoly = isl_upoly_subs(qp->upoly, total + div, 1, &s);
1866         if (!qp->upoly)
1867                 goto error;
1868
1869         reordering = isl_alloc_array(qp->dim->ctx, int, total + qp->div->n_row);
1870         if (!reordering)
1871                 goto error;
1872         for (i = 0; i < total + div; ++i)
1873                 reordering[i] = i;
1874         for (i = total + div + 1; i < total + qp->div->n_row; ++i)
1875                 reordering[i] = i - 1;
1876         qp->div = isl_mat_drop_rows(qp->div, div, 1);
1877         qp->div = isl_mat_drop_cols(qp->div, 2 + total + div, 1);
1878         qp->upoly = reorder(qp->upoly, reordering);
1879         free(reordering);
1880
1881         if (!qp->upoly || !qp->div)
1882                 goto error;
1883
1884         isl_upoly_free(s);
1885         return qp;
1886 error:
1887         isl_qpolynomial_free(qp);
1888         isl_upoly_free(s);
1889         return NULL;
1890 }
1891
1892 /* Replace all integer divisions [e/d] that turn out to not actually be integer
1893  * divisions because d is equal to 1 by their definition, i.e., e.
1894  */
1895 static __isl_give isl_qpolynomial *substitute_non_divs(
1896         __isl_take isl_qpolynomial *qp)
1897 {
1898         int i, j;
1899         int total;
1900         struct isl_upoly *s;
1901
1902         if (!qp)
1903                 return NULL;
1904
1905         total = isl_dim_total(qp->dim);
1906         for (i = 0; qp && i < qp->div->n_row; ++i) {
1907                 if (!isl_int_is_one(qp->div->row[i][0]))
1908                         continue;
1909                 for (j = i + 1; j < qp->div->n_row; ++j) {
1910                         if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
1911                                 continue;
1912                         isl_seq_combine(qp->div->row[j] + 1,
1913                                 qp->div->ctx->one, qp->div->row[j] + 1,
1914                                 qp->div->row[j][2 + total + i],
1915                                 qp->div->row[i] + 1, 1 + total + i);
1916                         isl_int_set_si(qp->div->row[j][2 + total + i], 0);
1917                         normalize_div(qp, j);
1918                 }
1919                 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
1920                                         qp->div->row[i][0], qp->div->n_col - 1);
1921                 qp = substitute_div(qp, i, s);
1922                 --i;
1923         }
1924
1925         return qp;
1926 }
1927
1928 /* Reduce the coefficients of div "div" to lie in the interval [0, d-1],
1929  * with d the denominator.  When replacing the coefficient e of x by
1930  * d * frac(e/d) = e - d * floor(e/d), we are subtracting d * floor(e/d) * x
1931  * inside the division, so we need to add floor(e/d) * x outside.
1932  * That is, we replace q by q' + floor(e/d) * x and we therefore need
1933  * to adjust the coefficient of x in each later div that depends on the
1934  * current div "div" and also in the affine expression "aff"
1935  * (if it too depends on "div").
1936  */
1937 static void reduce_div(__isl_keep isl_qpolynomial *qp, int div,
1938         __isl_keep isl_vec *aff)
1939 {
1940         int i, j;
1941         isl_int v;
1942         unsigned total = qp->div->n_col - qp->div->n_row - 2;
1943
1944         isl_int_init(v);
1945         for (i = 0; i < 1 + total + div; ++i) {
1946                 if (isl_int_is_nonneg(qp->div->row[div][1 + i]) &&
1947                     isl_int_lt(qp->div->row[div][1 + i], qp->div->row[div][0]))
1948                         continue;
1949                 isl_int_fdiv_q(v, qp->div->row[div][1 + i], qp->div->row[div][0]);
1950                 isl_int_fdiv_r(qp->div->row[div][1 + i],
1951                                 qp->div->row[div][1 + i], qp->div->row[div][0]);
1952                 if (!isl_int_is_zero(aff->el[1 + total + div]))
1953                         isl_int_addmul(aff->el[i], v, aff->el[1 + total + div]);
1954                 for (j = div + 1; j < qp->div->n_row; ++j) {
1955                         if (isl_int_is_zero(qp->div->row[j][2 + total + div]))
1956                                 continue;
1957                         isl_int_addmul(qp->div->row[j][1 + i],
1958                                         v, qp->div->row[j][2 + total + div]);
1959                 }
1960         }
1961         isl_int_clear(v);
1962 }
1963
1964 /* Check if the last non-zero coefficient is bigger that half of the
1965  * denominator.  If so, we will invert the div to further reduce the number
1966  * of distinct divs that may appear.
1967  * If the last non-zero coefficient is exactly half the denominator,
1968  * then we continue looking for earlier coefficients that are bigger
1969  * than half the denominator.
1970  */
1971 static int needs_invert(__isl_keep isl_mat *div, int row)
1972 {
1973         int i;
1974         int cmp;
1975
1976         for (i = div->n_col - 1; i >= 1; --i) {
1977                 if (isl_int_is_zero(div->row[row][i]))
1978                         continue;
1979                 isl_int_mul_ui(div->row[row][i], div->row[row][i], 2);
1980                 cmp = isl_int_cmp(div->row[row][i], div->row[row][0]);
1981                 isl_int_divexact_ui(div->row[row][i], div->row[row][i], 2);
1982                 if (cmp)
1983                         return cmp > 0;
1984                 if (i == 1)
1985                         return 1;
1986         }
1987
1988         return 0;
1989 }
1990
1991 /* Replace div "div" q = [e/d] by -[(-e+(d-1))/d].
1992  * We only invert the coefficients of e (and the coefficient of q in
1993  * later divs and in "aff").  After calling this function, the
1994  * coefficients of e should be reduced again.
1995  */
1996 static void invert_div(__isl_keep isl_qpolynomial *qp, int div,
1997         __isl_keep isl_vec *aff)
1998 {
1999         unsigned total = qp->div->n_col - qp->div->n_row - 2;
2000
2001         isl_seq_neg(qp->div->row[div] + 1,
2002                     qp->div->row[div] + 1, qp->div->n_col - 1);
2003         isl_int_sub_ui(qp->div->row[div][1], qp->div->row[div][1], 1);
2004         isl_int_add(qp->div->row[div][1],
2005                     qp->div->row[div][1], qp->div->row[div][0]);
2006         if (!isl_int_is_zero(aff->el[1 + total + div]))
2007                 isl_int_neg(aff->el[1 + total + div], aff->el[1 + total + div]);
2008         isl_mat_col_mul(qp->div, 2 + total + div,
2009                         qp->div->ctx->negone, 2 + total + div);
2010 }
2011
2012 /* Assuming "qp" is a monomial, reduce all its divs to have coefficients
2013  * in the interval [0, d-1], with d the denominator and such that the
2014  * last non-zero coefficient that is not equal to d/2 is smaller than d/2.
2015  *
2016  * After the reduction, some divs may have become redundant or identical,
2017  * so we call substitute_non_divs and sort_divs.  If these functions
2018  * eliminate divs of merge * two or more divs into one, the coefficients
2019  * of the enclosing divs may have to be reduced again, so we call
2020  * ourselves recursively if the number of divs decreases.
2021  */
2022 static __isl_give isl_qpolynomial *reduce_divs(__isl_take isl_qpolynomial *qp)
2023 {
2024         int i, j;
2025         isl_vec *aff = NULL;
2026         struct isl_upoly *s;
2027         unsigned n_div;
2028
2029         if (!qp)
2030                 return NULL;
2031
2032         aff = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
2033         aff = isl_vec_clr(aff);
2034         if (!aff)
2035                 goto error;
2036
2037         isl_int_set_si(aff->el[1 + qp->upoly->var], 1);
2038
2039         for (i = 0; i < qp->div->n_row; ++i) {
2040                 normalize_div(qp, i);
2041                 reduce_div(qp, i, aff);
2042                 if (needs_invert(qp->div, i)) {
2043                         invert_div(qp, i, aff);
2044                         reduce_div(qp, i, aff);
2045                 }
2046         }
2047
2048         s = isl_upoly_from_affine(qp->div->ctx, aff->el,
2049                                   qp->div->ctx->one, aff->size);
2050         qp->upoly = isl_upoly_subs(qp->upoly, qp->upoly->var, 1, &s);
2051         isl_upoly_free(s);
2052         if (!qp->upoly)
2053                 goto error;
2054
2055         isl_vec_free(aff);
2056
2057         n_div = qp->div->n_row;
2058         qp = substitute_non_divs(qp);
2059         qp = sort_divs(qp);
2060         if (qp && qp->div->n_row < n_div)
2061                 return reduce_divs(qp);
2062
2063         return qp;
2064 error:
2065         isl_qpolynomial_free(qp);
2066         isl_vec_free(aff);
2067         return NULL;
2068 }
2069
2070 /* Assumes each div only depends on earlier divs.
2071  */
2072 __isl_give isl_qpolynomial *isl_qpolynomial_div_pow(__isl_take isl_div *div,
2073         int power)
2074 {
2075         struct isl_qpolynomial *qp = NULL;
2076         struct isl_upoly_rec *rec;
2077         struct isl_upoly_cst *cst;
2078         int i, d;
2079         int pos;
2080
2081         if (!div)
2082                 return NULL;
2083
2084         d = div->line - div->bmap->div;
2085
2086         pos = isl_dim_total(div->bmap->dim) + d;
2087         rec = isl_upoly_alloc_rec(div->ctx, pos, 1 + power);
2088         qp = isl_qpolynomial_alloc(isl_basic_map_get_dim(div->bmap),
2089                                    div->bmap->n_div, &rec->up);
2090         if (!qp)
2091                 goto error;
2092
2093         for (i = 0; i < div->bmap->n_div; ++i)
2094                 isl_seq_cpy(qp->div->row[i], div->bmap->div[i], qp->div->n_col);
2095
2096         for (i = 0; i < 1 + power; ++i) {
2097                 rec->p[i] = isl_upoly_zero(div->ctx);
2098                 if (!rec->p[i])
2099                         goto error;
2100                 rec->n++;
2101         }
2102         cst = isl_upoly_as_cst(rec->p[power]);
2103         isl_int_set_si(cst->n, 1);
2104
2105         isl_div_free(div);
2106
2107         qp = reduce_divs(qp);
2108
2109         return qp;
2110 error:
2111         isl_qpolynomial_free(qp);
2112         isl_div_free(div);
2113         return NULL;
2114 }
2115
2116 __isl_give isl_qpolynomial *isl_qpolynomial_div(__isl_take isl_div *div)
2117 {
2118         return isl_qpolynomial_div_pow(div, 1);
2119 }
2120
2121 __isl_give isl_qpolynomial *isl_qpolynomial_rat_cst(__isl_take isl_dim *dim,
2122         const isl_int n, const isl_int d)
2123 {
2124         struct isl_qpolynomial *qp;
2125         struct isl_upoly_cst *cst;
2126
2127         qp = isl_qpolynomial_alloc(dim, 0, isl_upoly_zero(dim->ctx));
2128         if (!qp)
2129                 return NULL;
2130
2131         cst = isl_upoly_as_cst(qp->upoly);
2132         isl_int_set(cst->n, n);
2133         isl_int_set(cst->d, d);
2134
2135         return qp;
2136 }
2137
2138 static int up_set_active(__isl_keep struct isl_upoly *up, int *active, int d)
2139 {
2140         struct isl_upoly_rec *rec;
2141         int i;
2142
2143         if (!up)
2144                 return -1;
2145
2146         if (isl_upoly_is_cst(up))
2147                 return 0;
2148
2149         if (up->var < d)
2150                 active[up->var] = 1;
2151
2152         rec = isl_upoly_as_rec(up);
2153         for (i = 0; i < rec->n; ++i)
2154                 if (up_set_active(rec->p[i], active, d) < 0)
2155                         return -1;
2156
2157         return 0;
2158 }
2159
2160 static int set_active(__isl_keep isl_qpolynomial *qp, int *active)
2161 {
2162         int i, j;
2163         int d = isl_dim_total(qp->dim);
2164
2165         if (!qp || !active)
2166                 return -1;
2167
2168         for (i = 0; i < d; ++i)
2169                 for (j = 0; j < qp->div->n_row; ++j) {
2170                         if (isl_int_is_zero(qp->div->row[j][2 + i]))
2171                                 continue;
2172                         active[i] = 1;
2173                         break;
2174                 }
2175
2176         return up_set_active(qp->upoly, active, d);
2177 }
2178
2179 int isl_qpolynomial_involves_dims(__isl_keep isl_qpolynomial *qp,
2180         enum isl_dim_type type, unsigned first, unsigned n)
2181 {
2182         int i;
2183         int *active = NULL;
2184         int involves = 0;
2185
2186         if (!qp)
2187                 return -1;
2188         if (n == 0)
2189                 return 0;
2190
2191         isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2192                         return -1);
2193         isl_assert(qp->dim->ctx, type == isl_dim_param ||
2194                                  type == isl_dim_set, return -1);
2195
2196         active = isl_calloc_array(set->ctx, int, isl_dim_total(qp->dim));
2197         if (set_active(qp, active) < 0)
2198                 goto error;
2199
2200         if (type == isl_dim_set)
2201                 first += isl_dim_size(qp->dim, isl_dim_param);
2202         for (i = 0; i < n; ++i)
2203                 if (active[first + i]) {
2204                         involves = 1;
2205                         break;
2206                 }
2207
2208         free(active);
2209
2210         return involves;
2211 error:
2212         free(active);
2213         return -1;
2214 }
2215
2216 __isl_give struct isl_upoly *isl_upoly_drop(__isl_take struct isl_upoly *up,
2217         unsigned first, unsigned n)
2218 {
2219         int i;
2220         struct isl_upoly_rec *rec;
2221
2222         if (!up)
2223                 return NULL;
2224         if (n == 0 || up->var < 0 || up->var < first)
2225                 return up;
2226         if (up->var < first + n) {
2227                 up = replace_by_constant_term(up);
2228                 return isl_upoly_drop(up, first, n);
2229         }
2230         up = isl_upoly_cow(up);
2231         if (!up)
2232                 return NULL;
2233         up->var -= n;
2234         rec = isl_upoly_as_rec(up);
2235         if (!rec)
2236                 goto error;
2237
2238         for (i = 0; i < rec->n; ++i) {
2239                 rec->p[i] = isl_upoly_drop(rec->p[i], first, n);
2240                 if (!rec->p[i])
2241                         goto error;
2242         }
2243
2244         return up;
2245 error:
2246         isl_upoly_free(up);
2247         return NULL;
2248 }
2249
2250 __isl_give isl_qpolynomial *isl_qpolynomial_set_dim_name(
2251         __isl_take isl_qpolynomial *qp,
2252         enum isl_dim_type type, unsigned pos, const char *s)
2253 {
2254         qp = isl_qpolynomial_cow(qp);
2255         if (!qp)
2256                 return NULL;
2257         qp->dim = isl_dim_set_name(qp->dim, type, pos, s);
2258         if (!qp->dim)
2259                 goto error;
2260         return qp;
2261 error:
2262         isl_qpolynomial_free(qp);
2263         return NULL;
2264 }
2265
2266 __isl_give isl_qpolynomial *isl_qpolynomial_drop_dims(
2267         __isl_take isl_qpolynomial *qp,
2268         enum isl_dim_type type, unsigned first, unsigned n)
2269 {
2270         if (!qp)
2271                 return NULL;
2272         if (n == 0 && !isl_dim_get_tuple_name(qp->dim, type))
2273                 return qp;
2274
2275         qp = isl_qpolynomial_cow(qp);
2276         if (!qp)
2277                 return NULL;
2278
2279         isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2280                         goto error);
2281         isl_assert(qp->dim->ctx, type == isl_dim_param ||
2282                                  type == isl_dim_set, goto error);
2283
2284         qp->dim = isl_dim_drop(qp->dim, type, first, n);
2285         if (!qp->dim)
2286                 goto error;
2287
2288         if (type == isl_dim_set)
2289                 first += isl_dim_size(qp->dim, isl_dim_param);
2290
2291         qp->div = isl_mat_drop_cols(qp->div, 2 + first, n);
2292         if (!qp->div)
2293                 goto error;
2294
2295         qp->upoly = isl_upoly_drop(qp->upoly, first, n);
2296         if (!qp->upoly)
2297                 goto error;
2298
2299         return qp;
2300 error:
2301         isl_qpolynomial_free(qp);
2302         return NULL;
2303 }
2304
2305 __isl_give isl_qpolynomial *isl_qpolynomial_substitute_equalities(
2306         __isl_take isl_qpolynomial *qp, __isl_take isl_basic_set *eq)
2307 {
2308         int i, j, k;
2309         isl_int denom;
2310         unsigned total;
2311         unsigned n_div;
2312         struct isl_upoly *up;
2313
2314         if (!eq)
2315                 goto error;
2316         if (eq->n_eq == 0) {
2317                 isl_basic_set_free(eq);
2318                 return qp;
2319         }
2320
2321         qp = isl_qpolynomial_cow(qp);
2322         if (!qp)
2323                 goto error;
2324         qp->div = isl_mat_cow(qp->div);
2325         if (!qp->div)
2326                 goto error;
2327
2328         total = 1 + isl_dim_total(eq->dim);
2329         n_div = eq->n_div;
2330         isl_int_init(denom);
2331         for (i = 0; i < eq->n_eq; ++i) {
2332                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2333                 if (j < 0 || j == 0 || j >= total)
2334                         continue;
2335
2336                 for (k = 0; k < qp->div->n_row; ++k) {
2337                         if (isl_int_is_zero(qp->div->row[k][1 + j]))
2338                                 continue;
2339                         isl_seq_elim(qp->div->row[k] + 1, eq->eq[i], j, total,
2340                                         &qp->div->row[k][0]);
2341                         normalize_div(qp, k);
2342                 }
2343
2344                 if (isl_int_is_pos(eq->eq[i][j]))
2345                         isl_seq_neg(eq->eq[i], eq->eq[i], total);
2346                 isl_int_abs(denom, eq->eq[i][j]);
2347                 isl_int_set_si(eq->eq[i][j], 0);
2348
2349                 up = isl_upoly_from_affine(qp->dim->ctx,
2350                                                    eq->eq[i], denom, total);
2351                 qp->upoly = isl_upoly_subs(qp->upoly, j - 1, 1, &up);
2352                 isl_upoly_free(up);
2353         }
2354         isl_int_clear(denom);
2355
2356         if (!qp->upoly)
2357                 goto error;
2358
2359         isl_basic_set_free(eq);
2360
2361         qp = substitute_non_divs(qp);
2362         qp = sort_divs(qp);
2363
2364         return qp;
2365 error:
2366         isl_basic_set_free(eq);
2367         isl_qpolynomial_free(qp);
2368         return NULL;
2369 }
2370
2371 static __isl_give isl_basic_set *add_div_constraints(
2372         __isl_take isl_basic_set *bset, __isl_take isl_mat *div)
2373 {
2374         int i;
2375         unsigned total;
2376
2377         if (!bset || !div)
2378                 goto error;
2379
2380         bset = isl_basic_set_extend_constraints(bset, 0, 2 * div->n_row);
2381         if (!bset)
2382                 goto error;
2383         total = isl_basic_set_total_dim(bset);
2384         for (i = 0; i < div->n_row; ++i)
2385                 if (isl_basic_set_add_div_constraints_var(bset,
2386                                     total - div->n_row + i, div->row[i]) < 0)
2387                         goto error;
2388
2389         isl_mat_free(div);
2390         return bset;
2391 error:
2392         isl_mat_free(div);
2393         isl_basic_set_free(bset);
2394         return NULL;
2395 }
2396
2397 /* Look for equalities among the variables shared by context and qp
2398  * and the integer divisions of qp, if any.
2399  * The equalities are then used to eliminate variables and/or integer
2400  * divisions from qp.
2401  */
2402 __isl_give isl_qpolynomial *isl_qpolynomial_gist(
2403         __isl_take isl_qpolynomial *qp, __isl_take isl_set *context)
2404 {
2405         isl_basic_set *aff;
2406
2407         if (!qp)
2408                 goto error;
2409         if (qp->div->n_row > 0) {
2410                 isl_basic_set *bset;
2411                 context = isl_set_add_dims(context, isl_dim_set,
2412                                             qp->div->n_row);
2413                 bset = isl_basic_set_universe(isl_set_get_dim(context));
2414                 bset = add_div_constraints(bset, isl_mat_copy(qp->div));
2415                 context = isl_set_intersect(context,
2416                                             isl_set_from_basic_set(bset));
2417         }
2418
2419         aff = isl_set_affine_hull(context);
2420         return isl_qpolynomial_substitute_equalities(qp, aff);
2421 error:
2422         isl_qpolynomial_free(qp);
2423         isl_set_free(context);
2424         return NULL;
2425 }
2426
2427 #undef PW
2428 #define PW isl_pw_qpolynomial
2429 #undef EL
2430 #define EL isl_qpolynomial
2431 #undef IS_ZERO
2432 #define IS_ZERO is_zero
2433 #undef FIELD
2434 #define FIELD qp
2435
2436 #include <isl_pw_templ.c>
2437
2438 #undef UNION
2439 #define UNION isl_union_pw_qpolynomial
2440 #undef PART
2441 #define PART isl_pw_qpolynomial
2442 #undef PARTS
2443 #define PARTS pw_qpolynomial
2444
2445 #include <isl_union_templ.c>
2446
2447 int isl_pw_qpolynomial_is_one(__isl_keep isl_pw_qpolynomial *pwqp)
2448 {
2449         if (!pwqp)
2450                 return -1;
2451
2452         if (pwqp->n != -1)
2453                 return 0;
2454
2455         if (!isl_set_fast_is_universe(pwqp->p[0].set))
2456                 return 0;
2457
2458         return isl_qpolynomial_is_one(pwqp->p[0].qp);
2459 }
2460
2461 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_mul(
2462         __isl_take isl_pw_qpolynomial *pwqp1,
2463         __isl_take isl_pw_qpolynomial *pwqp2)
2464 {
2465         int i, j, n;
2466         struct isl_pw_qpolynomial *res;
2467         isl_set *set;
2468
2469         if (!pwqp1 || !pwqp2)
2470                 goto error;
2471
2472         isl_assert(pwqp1->dim->ctx, isl_dim_equal(pwqp1->dim, pwqp2->dim),
2473                         goto error);
2474
2475         if (isl_pw_qpolynomial_is_zero(pwqp1)) {
2476                 isl_pw_qpolynomial_free(pwqp2);
2477                 return pwqp1;
2478         }
2479
2480         if (isl_pw_qpolynomial_is_zero(pwqp2)) {
2481                 isl_pw_qpolynomial_free(pwqp1);
2482                 return pwqp2;
2483         }
2484
2485         if (isl_pw_qpolynomial_is_one(pwqp1)) {
2486                 isl_pw_qpolynomial_free(pwqp1);
2487                 return pwqp2;
2488         }
2489
2490         if (isl_pw_qpolynomial_is_one(pwqp2)) {
2491                 isl_pw_qpolynomial_free(pwqp2);
2492                 return pwqp1;
2493         }
2494
2495         n = pwqp1->n * pwqp2->n;
2496         res = isl_pw_qpolynomial_alloc_(isl_dim_copy(pwqp1->dim), n);
2497
2498         for (i = 0; i < pwqp1->n; ++i) {
2499                 for (j = 0; j < pwqp2->n; ++j) {
2500                         struct isl_set *common;
2501                         struct isl_qpolynomial *prod;
2502                         common = isl_set_intersect(isl_set_copy(pwqp1->p[i].set),
2503                                                 isl_set_copy(pwqp2->p[j].set));
2504                         if (isl_set_fast_is_empty(common)) {
2505                                 isl_set_free(common);
2506                                 continue;
2507                         }
2508
2509                         prod = isl_qpolynomial_mul(
2510                                 isl_qpolynomial_copy(pwqp1->p[i].qp),
2511                                 isl_qpolynomial_copy(pwqp2->p[j].qp));
2512
2513                         res = isl_pw_qpolynomial_add_piece(res, common, prod);
2514                 }
2515         }
2516
2517         isl_pw_qpolynomial_free(pwqp1);
2518         isl_pw_qpolynomial_free(pwqp2);
2519
2520         return res;
2521 error:
2522         isl_pw_qpolynomial_free(pwqp1);
2523         isl_pw_qpolynomial_free(pwqp2);
2524         return NULL;
2525 }
2526
2527 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_neg(
2528         __isl_take isl_pw_qpolynomial *pwqp)
2529 {
2530         int i;
2531
2532         if (!pwqp)
2533                 return NULL;
2534
2535         if (isl_pw_qpolynomial_is_zero(pwqp))
2536                 return pwqp;
2537
2538         pwqp = isl_pw_qpolynomial_cow(pwqp);
2539         if (!pwqp)
2540                 return NULL;
2541
2542         for (i = 0; i < pwqp->n; ++i) {
2543                 pwqp->p[i].qp = isl_qpolynomial_neg(pwqp->p[i].qp);
2544                 if (!pwqp->p[i].qp)
2545                         goto error;
2546         }
2547
2548         return pwqp;
2549 error:
2550         isl_pw_qpolynomial_free(pwqp);
2551         return NULL;
2552 }
2553
2554 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sub(
2555         __isl_take isl_pw_qpolynomial *pwqp1,
2556         __isl_take isl_pw_qpolynomial *pwqp2)
2557 {
2558         return isl_pw_qpolynomial_add(pwqp1, isl_pw_qpolynomial_neg(pwqp2));
2559 }
2560
2561 __isl_give struct isl_upoly *isl_upoly_eval(
2562         __isl_take struct isl_upoly *up, __isl_take isl_vec *vec)
2563 {
2564         int i;
2565         struct isl_upoly_rec *rec;
2566         struct isl_upoly *res;
2567         struct isl_upoly *base;
2568
2569         if (isl_upoly_is_cst(up)) {
2570                 isl_vec_free(vec);
2571                 return up;
2572         }
2573
2574         rec = isl_upoly_as_rec(up);
2575         if (!rec)
2576                 goto error;
2577
2578         isl_assert(up->ctx, rec->n >= 1, goto error);
2579
2580         base = isl_upoly_rat_cst(up->ctx, vec->el[1 + up->var], vec->el[0]);
2581
2582         res = isl_upoly_eval(isl_upoly_copy(rec->p[rec->n - 1]),
2583                                 isl_vec_copy(vec));
2584
2585         for (i = rec->n - 2; i >= 0; --i) {
2586                 res = isl_upoly_mul(res, isl_upoly_copy(base));
2587                 res = isl_upoly_sum(res, 
2588                             isl_upoly_eval(isl_upoly_copy(rec->p[i]),
2589                                                             isl_vec_copy(vec)));
2590         }
2591
2592         isl_upoly_free(base);
2593         isl_upoly_free(up);
2594         isl_vec_free(vec);
2595         return res;
2596 error:
2597         isl_upoly_free(up);
2598         isl_vec_free(vec);
2599         return NULL;
2600 }
2601
2602 __isl_give isl_qpolynomial *isl_qpolynomial_eval(
2603         __isl_take isl_qpolynomial *qp, __isl_take isl_point *pnt)
2604 {
2605         isl_vec *ext;
2606         struct isl_upoly *up;
2607         isl_dim *dim;
2608
2609         if (!qp || !pnt)
2610                 goto error;
2611         isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, qp->dim), goto error);
2612
2613         if (qp->div->n_row == 0)
2614                 ext = isl_vec_copy(pnt->vec);
2615         else {
2616                 int i;
2617                 unsigned dim = isl_dim_total(qp->dim);
2618                 ext = isl_vec_alloc(qp->dim->ctx, 1 + dim + qp->div->n_row);
2619                 if (!ext)
2620                         goto error;
2621
2622                 isl_seq_cpy(ext->el, pnt->vec->el, pnt->vec->size);
2623                 for (i = 0; i < qp->div->n_row; ++i) {
2624                         isl_seq_inner_product(qp->div->row[i] + 1, ext->el,
2625                                                 1 + dim + i, &ext->el[1+dim+i]);
2626                         isl_int_fdiv_q(ext->el[1+dim+i], ext->el[1+dim+i],
2627                                         qp->div->row[i][0]);
2628                 }
2629         }
2630
2631         up = isl_upoly_eval(isl_upoly_copy(qp->upoly), ext);
2632         if (!up)
2633                 goto error;
2634
2635         dim = isl_dim_copy(qp->dim);
2636         isl_qpolynomial_free(qp);
2637         isl_point_free(pnt);
2638
2639         return isl_qpolynomial_alloc(dim, 0, up);
2640 error:
2641         isl_qpolynomial_free(qp);
2642         isl_point_free(pnt);
2643         return NULL;
2644 }
2645
2646 int isl_upoly_cmp(__isl_keep struct isl_upoly_cst *cst1,
2647         __isl_keep struct isl_upoly_cst *cst2)
2648 {
2649         int cmp;
2650         isl_int t;
2651         isl_int_init(t);
2652         isl_int_mul(t, cst1->n, cst2->d);
2653         isl_int_submul(t, cst2->n, cst1->d);
2654         cmp = isl_int_sgn(t);
2655         isl_int_clear(t);
2656         return cmp;
2657 }
2658
2659 int isl_qpolynomial_le_cst(__isl_keep isl_qpolynomial *qp1,
2660         __isl_keep isl_qpolynomial *qp2)
2661 {
2662         struct isl_upoly_cst *cst1, *cst2;
2663
2664         if (!qp1 || !qp2)
2665                 return -1;
2666         isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), return -1);
2667         isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), return -1);
2668         if (isl_qpolynomial_is_nan(qp1))
2669                 return -1;
2670         if (isl_qpolynomial_is_nan(qp2))
2671                 return -1;
2672         cst1 = isl_upoly_as_cst(qp1->upoly);
2673         cst2 = isl_upoly_as_cst(qp2->upoly);
2674
2675         return isl_upoly_cmp(cst1, cst2) <= 0;
2676 }
2677
2678 __isl_give isl_qpolynomial *isl_qpolynomial_min_cst(
2679         __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2680 {
2681         struct isl_upoly_cst *cst1, *cst2;
2682         int cmp;
2683
2684         if (!qp1 || !qp2)
2685                 goto error;
2686         isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2687         isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2688         cst1 = isl_upoly_as_cst(qp1->upoly);
2689         cst2 = isl_upoly_as_cst(qp2->upoly);
2690         cmp = isl_upoly_cmp(cst1, cst2);
2691
2692         if (cmp <= 0) {
2693                 isl_qpolynomial_free(qp2);
2694         } else {
2695                 isl_qpolynomial_free(qp1);
2696                 qp1 = qp2;
2697         }
2698         return qp1;
2699 error:
2700         isl_qpolynomial_free(qp1);
2701         isl_qpolynomial_free(qp2);
2702         return NULL;
2703 }
2704
2705 __isl_give isl_qpolynomial *isl_qpolynomial_max_cst(
2706         __isl_take isl_qpolynomial *qp1, __isl_take isl_qpolynomial *qp2)
2707 {
2708         struct isl_upoly_cst *cst1, *cst2;
2709         int cmp;
2710
2711         if (!qp1 || !qp2)
2712                 goto error;
2713         isl_assert(qp1->dim->ctx, isl_upoly_is_cst(qp1->upoly), goto error);
2714         isl_assert(qp2->dim->ctx, isl_upoly_is_cst(qp2->upoly), goto error);
2715         cst1 = isl_upoly_as_cst(qp1->upoly);
2716         cst2 = isl_upoly_as_cst(qp2->upoly);
2717         cmp = isl_upoly_cmp(cst1, cst2);
2718
2719         if (cmp >= 0) {
2720                 isl_qpolynomial_free(qp2);
2721         } else {
2722                 isl_qpolynomial_free(qp1);
2723                 qp1 = qp2;
2724         }
2725         return qp1;
2726 error:
2727         isl_qpolynomial_free(qp1);
2728         isl_qpolynomial_free(qp2);
2729         return NULL;
2730 }
2731
2732 __isl_give isl_qpolynomial *isl_qpolynomial_insert_dims(
2733         __isl_take isl_qpolynomial *qp, enum isl_dim_type type,
2734         unsigned first, unsigned n)
2735 {
2736         unsigned total;
2737         unsigned g_pos;
2738         int *exp;
2739
2740         if (n == 0)
2741                 return qp;
2742
2743         qp = isl_qpolynomial_cow(qp);
2744         if (!qp)
2745                 return NULL;
2746
2747         isl_assert(qp->div->ctx, first <= isl_dim_size(qp->dim, type),
2748                     goto error);
2749
2750         g_pos = pos(qp->dim, type) + first;
2751
2752         qp->div = isl_mat_insert_cols(qp->div, 2 + g_pos, n);
2753         if (!qp->div)
2754                 goto error;
2755
2756         total = qp->div->n_col - 2;
2757         if (total > g_pos) {
2758                 int i;
2759                 exp = isl_alloc_array(qp->div->ctx, int, total - g_pos);
2760                 if (!exp)
2761                         goto error;
2762                 for (i = 0; i < total - g_pos; ++i)
2763                         exp[i] = i + n;
2764                 qp->upoly = expand(qp->upoly, exp, g_pos);
2765                 free(exp);
2766                 if (!qp->upoly)
2767                         goto error;
2768         }
2769
2770         qp->dim = isl_dim_insert(qp->dim, type, first, n);
2771         if (!qp->dim)
2772                 goto error;
2773
2774         return qp;
2775 error:
2776         isl_qpolynomial_free(qp);
2777         return NULL;
2778 }
2779
2780 __isl_give isl_qpolynomial *isl_qpolynomial_add_dims(
2781         __isl_take isl_qpolynomial *qp, enum isl_dim_type type, unsigned n)
2782 {
2783         unsigned pos;
2784
2785         pos = isl_qpolynomial_dim(qp, type);
2786
2787         return isl_qpolynomial_insert_dims(qp, type, pos, n);
2788 }
2789
2790 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_add_dims(
2791         __isl_take isl_pw_qpolynomial *pwqp,
2792         enum isl_dim_type type, unsigned n)
2793 {
2794         unsigned pos;
2795
2796         pos = isl_pw_qpolynomial_dim(pwqp, type);
2797
2798         return isl_pw_qpolynomial_insert_dims(pwqp, type, pos, n);
2799 }
2800
2801 static int *reordering_move(isl_ctx *ctx,
2802         unsigned len, unsigned dst, unsigned src, unsigned n)
2803 {
2804         int i;
2805         int *reordering;
2806
2807         reordering = isl_alloc_array(ctx, int, len);
2808         if (!reordering)
2809                 return NULL;
2810
2811         if (dst <= src) {
2812                 for (i = 0; i < dst; ++i)
2813                         reordering[i] = i;
2814                 for (i = 0; i < n; ++i)
2815                         reordering[src + i] = dst + i;
2816                 for (i = 0; i < src - dst; ++i)
2817                         reordering[dst + i] = dst + n + i;
2818                 for (i = 0; i < len - src - n; ++i)
2819                         reordering[src + n + i] = src + n + i;
2820         } else {
2821                 for (i = 0; i < src; ++i)
2822                         reordering[i] = i;
2823                 for (i = 0; i < n; ++i)
2824                         reordering[src + i] = dst + i;
2825                 for (i = 0; i < dst - src; ++i)
2826                         reordering[src + n + i] = src + i;
2827                 for (i = 0; i < len - dst - n; ++i)
2828                         reordering[dst + n + i] = dst + n + i;
2829         }
2830
2831         return reordering;
2832 }
2833
2834 __isl_give isl_qpolynomial *isl_qpolynomial_move_dims(
2835         __isl_take isl_qpolynomial *qp,
2836         enum isl_dim_type dst_type, unsigned dst_pos,
2837         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2838 {
2839         unsigned g_dst_pos;
2840         unsigned g_src_pos;
2841         int *reordering;
2842
2843         qp = isl_qpolynomial_cow(qp);
2844         if (!qp)
2845                 return NULL;
2846
2847         isl_assert(qp->dim->ctx, src_pos + n <= isl_dim_size(qp->dim, src_type),
2848                 goto error);
2849
2850         g_dst_pos = pos(qp->dim, dst_type) + dst_pos;
2851         g_src_pos = pos(qp->dim, src_type) + src_pos;
2852         if (dst_type > src_type)
2853                 g_dst_pos -= n;
2854
2855         qp->div = isl_mat_move_cols(qp->div, 2 + g_dst_pos, 2 + g_src_pos, n);
2856         if (!qp->div)
2857                 goto error;
2858         qp = sort_divs(qp);
2859         if (!qp)
2860                 goto error;
2861
2862         reordering = reordering_move(qp->dim->ctx,
2863                                 qp->div->n_col - 2, g_dst_pos, g_src_pos, n);
2864         if (!reordering)
2865                 goto error;
2866
2867         qp->upoly = reorder(qp->upoly, reordering);
2868         free(reordering);
2869         if (!qp->upoly)
2870                 goto error;
2871
2872         qp->dim = isl_dim_move(qp->dim, dst_type, dst_pos, src_type, src_pos, n);
2873         if (!qp->dim)
2874                 goto error;
2875
2876         return qp;
2877 error:
2878         isl_qpolynomial_free(qp);
2879         return NULL;
2880 }
2881
2882 __isl_give isl_qpolynomial *isl_qpolynomial_from_affine(__isl_take isl_dim *dim,
2883         isl_int *f, isl_int denom)
2884 {
2885         struct isl_upoly *up;
2886
2887         if (!dim)
2888                 return NULL;
2889
2890         up = isl_upoly_from_affine(dim->ctx, f, denom, 1 + isl_dim_total(dim));
2891
2892         return isl_qpolynomial_alloc(dim, 0, up);
2893 }
2894
2895 __isl_give isl_qpolynomial *isl_qpolynomial_from_constraint(
2896         __isl_take isl_constraint *c, enum isl_dim_type type, unsigned pos)
2897 {
2898         isl_int denom;
2899         isl_dim *dim;
2900         struct isl_upoly *up;
2901         isl_qpolynomial *qp;
2902         int sgn;
2903
2904         if (!c)
2905                 return NULL;
2906
2907         isl_int_init(denom);
2908
2909         isl_constraint_get_coefficient(c, type, pos, &denom);
2910         isl_constraint_set_coefficient(c, type, pos, c->ctx->zero);
2911         sgn = isl_int_sgn(denom);
2912         isl_int_abs(denom, denom);
2913         up = isl_upoly_from_affine(c->ctx, c->line[0], denom,
2914                                         1 + isl_constraint_dim(c, isl_dim_all));
2915         if (sgn < 0)
2916                 isl_int_neg(denom, denom);
2917         isl_constraint_set_coefficient(c, type, pos, denom);
2918
2919         dim = isl_dim_copy(c->bmap->dim);
2920
2921         isl_int_clear(denom);
2922         isl_constraint_free(c);
2923
2924         qp = isl_qpolynomial_alloc(dim, 0, up);
2925         if (sgn > 0)
2926                 qp = isl_qpolynomial_neg(qp);
2927         return qp;
2928 }
2929
2930 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
2931  * in "qp" by subs[i].
2932  */
2933 __isl_give isl_qpolynomial *isl_qpolynomial_substitute(
2934         __isl_take isl_qpolynomial *qp,
2935         enum isl_dim_type type, unsigned first, unsigned n,
2936         __isl_keep isl_qpolynomial **subs)
2937 {
2938         int i;
2939         struct isl_upoly **ups;
2940
2941         if (n == 0)
2942                 return qp;
2943
2944         qp = isl_qpolynomial_cow(qp);
2945         if (!qp)
2946                 return NULL;
2947         for (i = 0; i < n; ++i)
2948                 if (!subs[i])
2949                         goto error;
2950
2951         isl_assert(qp->dim->ctx, first + n <= isl_dim_size(qp->dim, type),
2952                         goto error);
2953
2954         for (i = 0; i < n; ++i)
2955                 isl_assert(qp->dim->ctx, isl_dim_equal(qp->dim, subs[i]->dim),
2956                                 goto error);
2957
2958         isl_assert(qp->dim->ctx, qp->div->n_row == 0, goto error);
2959         for (i = 0; i < n; ++i)
2960                 isl_assert(qp->dim->ctx, subs[i]->div->n_row == 0, goto error);
2961
2962         first += pos(qp->dim, type);
2963
2964         ups = isl_alloc_array(qp->dim->ctx, struct isl_upoly *, n);
2965         if (!ups)
2966                 goto error;
2967         for (i = 0; i < n; ++i)
2968                 ups[i] = subs[i]->upoly;
2969
2970         qp->upoly = isl_upoly_subs(qp->upoly, first, n, ups);
2971
2972         free(ups);
2973
2974         if (!qp->upoly)
2975                 goto error;
2976
2977         return qp;
2978 error:
2979         isl_qpolynomial_free(qp);
2980         return NULL;
2981 }
2982
2983 /* Extend "bset" with extra set dimensions for each integer division
2984  * in "qp" and then call "fn" with the extended bset and the polynomial
2985  * that results from replacing each of the integer divisions by the
2986  * corresponding extra set dimension.
2987  */
2988 int isl_qpolynomial_as_polynomial_on_domain(__isl_keep isl_qpolynomial *qp,
2989         __isl_keep isl_basic_set *bset,
2990         int (*fn)(__isl_take isl_basic_set *bset,
2991                   __isl_take isl_qpolynomial *poly, void *user), void *user)
2992 {
2993         isl_dim *dim;
2994         isl_mat *div;
2995         isl_qpolynomial *poly;
2996
2997         if (!qp || !bset)
2998                 goto error;
2999         if (qp->div->n_row == 0)
3000                 return fn(isl_basic_set_copy(bset), isl_qpolynomial_copy(qp),
3001                           user);
3002
3003         div = isl_mat_copy(qp->div);
3004         dim = isl_dim_copy(qp->dim);
3005         dim = isl_dim_add(dim, isl_dim_set, qp->div->n_row);
3006         poly = isl_qpolynomial_alloc(dim, 0, isl_upoly_copy(qp->upoly));
3007         bset = isl_basic_set_copy(bset);
3008         bset = isl_basic_set_add(bset, isl_dim_set, qp->div->n_row);
3009         bset = add_div_constraints(bset, div);
3010
3011         return fn(bset, poly, user);
3012 error:
3013         return -1;
3014 }
3015
3016 /* Return total degree in variables first (inclusive) up to last (exclusive).
3017  */
3018 int isl_upoly_degree(__isl_keep struct isl_upoly *up, int first, int last)
3019 {
3020         int deg = -1;
3021         int i;
3022         struct isl_upoly_rec *rec;
3023
3024         if (!up)
3025                 return -2;
3026         if (isl_upoly_is_zero(up))
3027                 return -1;
3028         if (isl_upoly_is_cst(up) || up->var < first)
3029                 return 0;
3030
3031         rec = isl_upoly_as_rec(up);
3032         if (!rec)
3033                 return -2;
3034
3035         for (i = 0; i < rec->n; ++i) {
3036                 int d;
3037
3038                 if (isl_upoly_is_zero(rec->p[i]))
3039                         continue;
3040                 d = isl_upoly_degree(rec->p[i], first, last);
3041                 if (up->var < last)
3042                         d += i;
3043                 if (d > deg)
3044                         deg = d;
3045         }
3046
3047         return deg;
3048 }
3049
3050 /* Return total degree in set variables.
3051  */
3052 int isl_qpolynomial_degree(__isl_keep isl_qpolynomial *poly)
3053 {
3054         unsigned ovar;
3055         unsigned nvar;
3056
3057         if (!poly)
3058                 return -2;
3059
3060         ovar = isl_dim_offset(poly->dim, isl_dim_set);
3061         nvar = isl_dim_size(poly->dim, isl_dim_set);
3062         return isl_upoly_degree(poly->upoly, ovar, ovar + nvar);
3063 }
3064
3065 __isl_give struct isl_upoly *isl_upoly_coeff(__isl_keep struct isl_upoly *up,
3066         unsigned pos, int deg)
3067 {
3068         int i;
3069         struct isl_upoly_rec *rec;
3070
3071         if (!up)
3072                 return NULL;
3073
3074         if (isl_upoly_is_cst(up) || up->var < pos) {
3075                 if (deg == 0)
3076                         return isl_upoly_copy(up);
3077                 else
3078                         return isl_upoly_zero(up->ctx);
3079         }
3080
3081         rec = isl_upoly_as_rec(up);
3082         if (!rec)
3083                 return NULL;
3084
3085         if (up->var == pos) {
3086                 if (deg < rec->n)
3087                         return isl_upoly_copy(rec->p[deg]);
3088                 else
3089                         return isl_upoly_zero(up->ctx);
3090         }
3091
3092         up = isl_upoly_copy(up);
3093         up = isl_upoly_cow(up);
3094         rec = isl_upoly_as_rec(up);
3095         if (!rec)
3096                 goto error;
3097
3098         for (i = 0; i < rec->n; ++i) {
3099                 struct isl_upoly *t;
3100                 t = isl_upoly_coeff(rec->p[i], pos, deg);
3101                 if (!t)
3102                         goto error;
3103                 isl_upoly_free(rec->p[i]);
3104                 rec->p[i] = t;
3105         }
3106
3107         return up;
3108 error:
3109         isl_upoly_free(up);
3110         return NULL;
3111 }
3112
3113 /* Return coefficient of power "deg" of variable "t_pos" of type "type".
3114  */
3115 __isl_give isl_qpolynomial *isl_qpolynomial_coeff(
3116         __isl_keep isl_qpolynomial *qp,
3117         enum isl_dim_type type, unsigned t_pos, int deg)
3118 {
3119         unsigned g_pos;
3120         struct isl_upoly *up;
3121         isl_qpolynomial *c;
3122
3123         if (!qp)
3124                 return NULL;
3125
3126         isl_assert(qp->div->ctx, t_pos < isl_dim_size(qp->dim, type),
3127                         return NULL);
3128
3129         g_pos = pos(qp->dim, type) + t_pos;
3130         up = isl_upoly_coeff(qp->upoly, g_pos, deg);
3131
3132         c = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), qp->div->n_row, up);
3133         if (!c)
3134                 return NULL;
3135         isl_mat_free(c->div);
3136         c->div = isl_mat_copy(qp->div);
3137         if (!c->div)
3138                 goto error;
3139         return c;
3140 error:
3141         isl_qpolynomial_free(c);
3142         return NULL;
3143 }
3144
3145 /* Homogenize the polynomial in the variables first (inclusive) up to
3146  * last (exclusive) by inserting powers of variable first.
3147  * Variable first is assumed not to appear in the input.
3148  */
3149 __isl_give struct isl_upoly *isl_upoly_homogenize(
3150         __isl_take struct isl_upoly *up, int deg, int target,
3151         int first, int last)
3152 {
3153         int i;
3154         struct isl_upoly_rec *rec;
3155
3156         if (!up)
3157                 return NULL;
3158         if (isl_upoly_is_zero(up))
3159                 return up;
3160         if (deg == target)
3161                 return up;
3162         if (isl_upoly_is_cst(up) || up->var < first) {
3163                 struct isl_upoly *hom;
3164
3165                 hom = isl_upoly_var_pow(up->ctx, first, target - deg);
3166                 if (!hom)
3167                         goto error;
3168                 rec = isl_upoly_as_rec(hom);
3169                 rec->p[target - deg] = isl_upoly_mul(rec->p[target - deg], up);
3170
3171                 return hom;
3172         }
3173
3174         up = isl_upoly_cow(up);
3175         rec = isl_upoly_as_rec(up);
3176         if (!rec)
3177                 goto error;
3178
3179         for (i = 0; i < rec->n; ++i) {
3180                 if (isl_upoly_is_zero(rec->p[i]))
3181                         continue;
3182                 rec->p[i] = isl_upoly_homogenize(rec->p[i],
3183                                 up->var < last ? deg + i : i, target,
3184                                 first, last);
3185                 if (!rec->p[i])
3186                         goto error;
3187         }
3188
3189         return up;
3190 error:
3191         isl_upoly_free(up);
3192         return NULL;
3193 }
3194
3195 /* Homogenize the polynomial in the set variables by introducing
3196  * powers of an extra set variable at position 0.
3197  */
3198 __isl_give isl_qpolynomial *isl_qpolynomial_homogenize(
3199         __isl_take isl_qpolynomial *poly)
3200 {
3201         unsigned ovar;
3202         unsigned nvar;
3203         int deg = isl_qpolynomial_degree(poly);
3204
3205         if (deg < -1)
3206                 goto error;
3207
3208         poly = isl_qpolynomial_insert_dims(poly, isl_dim_set, 0, 1);
3209         poly = isl_qpolynomial_cow(poly);
3210         if (!poly)
3211                 goto error;
3212
3213         ovar = isl_dim_offset(poly->dim, isl_dim_set);
3214         nvar = isl_dim_size(poly->dim, isl_dim_set);
3215         poly->upoly = isl_upoly_homogenize(poly->upoly, 0, deg,
3216                                                 ovar, ovar + nvar);
3217         if (!poly->upoly)
3218                 goto error;
3219
3220         return poly;
3221 error:
3222         isl_qpolynomial_free(poly);
3223         return NULL;
3224 }
3225
3226 __isl_give isl_term *isl_term_alloc(__isl_take isl_dim *dim,
3227         __isl_take isl_mat *div)
3228 {
3229         isl_term *term;
3230         int n;
3231
3232         if (!dim || !div)
3233                 goto error;
3234
3235         n = isl_dim_total(dim) + div->n_row;
3236
3237         term = isl_calloc(dim->ctx, struct isl_term,
3238                         sizeof(struct isl_term) + (n - 1) * sizeof(int));
3239         if (!term)
3240                 goto error;
3241
3242         term->ref = 1;
3243         term->dim = dim;
3244         term->div = div;
3245         isl_int_init(term->n);
3246         isl_int_init(term->d);
3247         
3248         return term;
3249 error:
3250         isl_dim_free(dim);
3251         isl_mat_free(div);
3252         return NULL;
3253 }
3254
3255 __isl_give isl_term *isl_term_copy(__isl_keep isl_term *term)
3256 {
3257         if (!term)
3258                 return NULL;
3259
3260         term->ref++;
3261         return term;
3262 }
3263
3264 __isl_give isl_term *isl_term_dup(__isl_keep isl_term *term)
3265 {
3266         int i;
3267         isl_term *dup;
3268         unsigned total;
3269
3270         if (term)
3271                 return NULL;
3272
3273         total = isl_dim_total(term->dim) + term->div->n_row;
3274
3275         dup = isl_term_alloc(isl_dim_copy(term->dim), isl_mat_copy(term->div));
3276         if (!dup)
3277                 return NULL;
3278
3279         isl_int_set(dup->n, term->n);
3280         isl_int_set(dup->d, term->d);
3281
3282         for (i = 0; i < total; ++i)
3283                 dup->pow[i] = term->pow[i];
3284
3285         return dup;
3286 }
3287
3288 __isl_give isl_term *isl_term_cow(__isl_take isl_term *term)
3289 {
3290         if (!term)
3291                 return NULL;
3292
3293         if (term->ref == 1)
3294                 return term;
3295         term->ref--;
3296         return isl_term_dup(term);
3297 }
3298
3299 void isl_term_free(__isl_take isl_term *term)
3300 {
3301         if (!term)
3302                 return;
3303
3304         if (--term->ref > 0)
3305                 return;
3306
3307         isl_dim_free(term->dim);
3308         isl_mat_free(term->div);
3309         isl_int_clear(term->n);
3310         isl_int_clear(term->d);
3311         free(term);
3312 }
3313
3314 unsigned isl_term_dim(__isl_keep isl_term *term, enum isl_dim_type type)
3315 {
3316         if (!term)
3317                 return 0;
3318
3319         switch (type) {
3320         case isl_dim_param:
3321         case isl_dim_in:
3322         case isl_dim_out:       return isl_dim_size(term->dim, type);
3323         case isl_dim_div:       return term->div->n_row;
3324         case isl_dim_all:       return isl_dim_total(term->dim) + term->div->n_row;
3325         default:                return 0;
3326         }
3327 }
3328
3329 isl_ctx *isl_term_get_ctx(__isl_keep isl_term *term)
3330 {
3331         return term ? term->dim->ctx : NULL;
3332 }
3333
3334 void isl_term_get_num(__isl_keep isl_term *term, isl_int *n)
3335 {
3336         if (!term)
3337                 return;
3338         isl_int_set(*n, term->n);
3339 }
3340
3341 void isl_term_get_den(__isl_keep isl_term *term, isl_int *d)
3342 {
3343         if (!term)
3344                 return;
3345         isl_int_set(*d, term->d);
3346 }
3347
3348 int isl_term_get_exp(__isl_keep isl_term *term,
3349         enum isl_dim_type type, unsigned pos)
3350 {
3351         if (!term)
3352                 return -1;
3353
3354         isl_assert(term->dim->ctx, pos < isl_term_dim(term, type), return -1);
3355
3356         if (type >= isl_dim_set)
3357                 pos += isl_dim_size(term->dim, isl_dim_param);
3358         if (type >= isl_dim_div)
3359                 pos += isl_dim_size(term->dim, isl_dim_set);
3360
3361         return term->pow[pos];
3362 }
3363
3364 __isl_give isl_div *isl_term_get_div(__isl_keep isl_term *term, unsigned pos)
3365 {
3366         isl_basic_map *bmap;
3367         unsigned total;
3368         int k;
3369
3370         if (!term)
3371                 return NULL;
3372
3373         isl_assert(term->dim->ctx, pos < isl_term_dim(term, isl_dim_div),
3374                         return NULL);
3375
3376         total = term->div->n_col - term->div->n_row - 2;
3377         /* No nested divs for now */
3378         isl_assert(term->dim->ctx,
3379                 isl_seq_first_non_zero(term->div->row[pos] + 2 + total,
3380                                         term->div->n_row) == -1,
3381                 return NULL);
3382
3383         bmap = isl_basic_map_alloc_dim(isl_dim_copy(term->dim), 1, 0, 0);
3384         if ((k = isl_basic_map_alloc_div(bmap)) < 0)
3385                 goto error;
3386
3387         isl_seq_cpy(bmap->div[k], term->div->row[pos], 2 + total);
3388
3389         return isl_basic_map_div(bmap, k);
3390 error:
3391         isl_basic_map_free(bmap);
3392         return NULL;
3393 }
3394
3395 __isl_give isl_term *isl_upoly_foreach_term(__isl_keep struct isl_upoly *up,
3396         int (*fn)(__isl_take isl_term *term, void *user),
3397         __isl_take isl_term *term, void *user)
3398 {
3399         int i;
3400         struct isl_upoly_rec *rec;
3401
3402         if (!up || !term)
3403                 goto error;
3404
3405         if (isl_upoly_is_zero(up))
3406                 return term;
3407
3408         isl_assert(up->ctx, !isl_upoly_is_nan(up), goto error);
3409         isl_assert(up->ctx, !isl_upoly_is_infty(up), goto error);
3410         isl_assert(up->ctx, !isl_upoly_is_neginfty(up), goto error);
3411
3412         if (isl_upoly_is_cst(up)) {
3413                 struct isl_upoly_cst *cst;
3414                 cst = isl_upoly_as_cst(up);
3415                 if (!cst)
3416                         goto error;
3417                 term = isl_term_cow(term);
3418                 if (!term)
3419                         goto error;
3420                 isl_int_set(term->n, cst->n);
3421                 isl_int_set(term->d, cst->d);
3422                 if (fn(isl_term_copy(term), user) < 0)
3423                         goto error;
3424                 return term;
3425         }
3426
3427         rec = isl_upoly_as_rec(up);
3428         if (!rec)
3429                 goto error;
3430
3431         for (i = 0; i < rec->n; ++i) {
3432                 term = isl_term_cow(term);
3433                 if (!term)
3434                         goto error;
3435                 term->pow[up->var] = i;
3436                 term = isl_upoly_foreach_term(rec->p[i], fn, term, user);
3437                 if (!term)
3438                         goto error;
3439         }
3440         term->pow[up->var] = 0;
3441
3442         return term;
3443 error:
3444         isl_term_free(term);
3445         return NULL;
3446 }
3447
3448 int isl_qpolynomial_foreach_term(__isl_keep isl_qpolynomial *qp,
3449         int (*fn)(__isl_take isl_term *term, void *user), void *user)
3450 {
3451         isl_term *term;
3452
3453         if (!qp)
3454                 return -1;
3455
3456         term = isl_term_alloc(isl_dim_copy(qp->dim), isl_mat_copy(qp->div));
3457         if (!term)
3458                 return -1;
3459
3460         term = isl_upoly_foreach_term(qp->upoly, fn, term, user);
3461
3462         isl_term_free(term);
3463
3464         return term ? 0 : -1;
3465 }
3466
3467 __isl_give isl_qpolynomial *isl_qpolynomial_from_term(__isl_take isl_term *term)
3468 {
3469         struct isl_upoly *up;
3470         isl_qpolynomial *qp;
3471         int i, n;
3472
3473         if (!term)
3474                 return NULL;
3475
3476         n = isl_dim_total(term->dim) + term->div->n_row;
3477
3478         up = isl_upoly_rat_cst(term->dim->ctx, term->n, term->d);
3479         for (i = 0; i < n; ++i) {
3480                 if (!term->pow[i])
3481                         continue;
3482                 up = isl_upoly_mul(up,
3483                         isl_upoly_var_pow(term->dim->ctx, i, term->pow[i]));
3484         }
3485
3486         qp = isl_qpolynomial_alloc(isl_dim_copy(term->dim), term->div->n_row, up);
3487         if (!qp)
3488                 goto error;
3489         isl_mat_free(qp->div);
3490         qp->div = isl_mat_copy(term->div);
3491         if (!qp->div)
3492                 goto error;
3493
3494         isl_term_free(term);
3495         return qp;
3496 error:
3497         isl_qpolynomial_free(qp);
3498         isl_term_free(term);
3499         return NULL;
3500 }
3501
3502 __isl_give isl_qpolynomial *isl_qpolynomial_lift(__isl_take isl_qpolynomial *qp,
3503         __isl_take isl_dim *dim)
3504 {
3505         int i;
3506         int extra;
3507         unsigned total;
3508
3509         if (!qp || !dim)
3510                 goto error;
3511
3512         if (isl_dim_equal(qp->dim, dim)) {
3513                 isl_dim_free(dim);
3514                 return qp;
3515         }
3516
3517         qp = isl_qpolynomial_cow(qp);
3518         if (!qp)
3519                 goto error;
3520
3521         extra = isl_dim_size(dim, isl_dim_set) -
3522                         isl_dim_size(qp->dim, isl_dim_set);
3523         total = isl_dim_total(qp->dim);
3524         if (qp->div->n_row) {
3525                 int *exp;
3526
3527                 exp = isl_alloc_array(qp->div->ctx, int, qp->div->n_row);
3528                 if (!exp)
3529                         goto error;
3530                 for (i = 0; i < qp->div->n_row; ++i)
3531                         exp[i] = extra + i;
3532                 qp->upoly = expand(qp->upoly, exp, total);
3533                 free(exp);
3534                 if (!qp->upoly)
3535                         goto error;
3536         }
3537         qp->div = isl_mat_insert_cols(qp->div, 2 + total, extra);
3538         if (!qp->div)
3539                 goto error;
3540         for (i = 0; i < qp->div->n_row; ++i)
3541                 isl_seq_clr(qp->div->row[i] + 2 + total, extra);
3542
3543         isl_dim_free(qp->dim);
3544         qp->dim = dim;
3545
3546         return qp;
3547 error:
3548         isl_dim_free(dim);
3549         isl_qpolynomial_free(qp);
3550         return NULL;
3551 }
3552
3553 /* For each parameter or variable that does not appear in qp,
3554  * first eliminate the variable from all constraints and then set it to zero.
3555  */
3556 static __isl_give isl_set *fix_inactive(__isl_take isl_set *set,
3557         __isl_keep isl_qpolynomial *qp)
3558 {
3559         int *active = NULL;
3560         int i;
3561         int d;
3562         unsigned nparam;
3563         unsigned nvar;
3564
3565         if (!set || !qp)
3566                 goto error;
3567
3568         d = isl_dim_total(set->dim);
3569         active = isl_calloc_array(set->ctx, int, d);
3570         if (set_active(qp, active) < 0)
3571                 goto error;
3572
3573         for (i = 0; i < d; ++i)
3574                 if (!active[i])
3575                         break;
3576
3577         if (i == d) {
3578                 free(active);
3579                 return set;
3580         }
3581
3582         nparam = isl_dim_size(set->dim, isl_dim_param);
3583         nvar = isl_dim_size(set->dim, isl_dim_set);
3584         for (i = 0; i < nparam; ++i) {
3585                 if (active[i])
3586                         continue;
3587                 set = isl_set_eliminate(set, isl_dim_param, i, 1);
3588                 set = isl_set_fix_si(set, isl_dim_param, i, 0);
3589         }
3590         for (i = 0; i < nvar; ++i) {
3591                 if (active[nparam + i])
3592                         continue;
3593                 set = isl_set_eliminate(set, isl_dim_set, i, 1);
3594                 set = isl_set_fix_si(set, isl_dim_set, i, 0);
3595         }
3596
3597         free(active);
3598
3599         return set;
3600 error:
3601         free(active);
3602         isl_set_free(set);
3603         return NULL;
3604 }
3605
3606 struct isl_opt_data {
3607         isl_qpolynomial *qp;
3608         int first;
3609         isl_qpolynomial *opt;
3610         int max;
3611 };
3612
3613 static int opt_fn(__isl_take isl_point *pnt, void *user)
3614 {
3615         struct isl_opt_data *data = (struct isl_opt_data *)user;
3616         isl_qpolynomial *val;
3617
3618         val = isl_qpolynomial_eval(isl_qpolynomial_copy(data->qp), pnt);
3619         if (data->first) {
3620                 data->first = 0;
3621                 data->opt = val;
3622         } else if (data->max) {
3623                 data->opt = isl_qpolynomial_max_cst(data->opt, val);
3624         } else {
3625                 data->opt = isl_qpolynomial_min_cst(data->opt, val);
3626         }
3627
3628         return 0;
3629 }
3630
3631 __isl_give isl_qpolynomial *isl_qpolynomial_opt_on_domain(
3632         __isl_take isl_qpolynomial *qp, __isl_take isl_set *set, int max)
3633 {
3634         struct isl_opt_data data = { NULL, 1, NULL, max };
3635
3636         if (!set || !qp)
3637                 goto error;
3638
3639         if (isl_upoly_is_cst(qp->upoly)) {
3640                 isl_set_free(set);
3641                 return qp;
3642         }
3643
3644         set = fix_inactive(set, qp);
3645
3646         data.qp = qp;
3647         if (isl_set_foreach_point(set, opt_fn, &data) < 0)
3648                 goto error;
3649
3650         if (data.first)
3651                 data.opt = isl_qpolynomial_zero(isl_qpolynomial_get_dim(qp));
3652
3653         isl_set_free(set);
3654         isl_qpolynomial_free(qp);
3655         return data.opt;
3656 error:
3657         isl_set_free(set);
3658         isl_qpolynomial_free(qp);
3659         isl_qpolynomial_free(data.opt);
3660         return NULL;
3661 }
3662
3663 __isl_give isl_qpolynomial *isl_qpolynomial_morph(__isl_take isl_qpolynomial *qp,
3664         __isl_take isl_morph *morph)
3665 {
3666         int i;
3667         int n_sub;
3668         isl_ctx *ctx;
3669         struct isl_upoly *up;
3670         unsigned n_div;
3671         struct isl_upoly **subs;
3672         isl_mat *mat;
3673
3674         qp = isl_qpolynomial_cow(qp);
3675         if (!qp || !morph)
3676                 goto error;
3677
3678         ctx = qp->dim->ctx;
3679         isl_assert(ctx, isl_dim_equal(qp->dim, morph->dom->dim), goto error);
3680
3681         n_sub = morph->inv->n_row - 1;
3682         if (morph->inv->n_row != morph->inv->n_col)
3683                 n_sub += qp->div->n_row;
3684         subs = isl_calloc_array(ctx, struct isl_upoly *, n_sub);
3685         if (!subs)
3686                 goto error;
3687
3688         for (i = 0; 1 + i < morph->inv->n_row; ++i)
3689                 subs[i] = isl_upoly_from_affine(ctx, morph->inv->row[1 + i],
3690                                         morph->inv->row[0][0], morph->inv->n_col);
3691         if (morph->inv->n_row != morph->inv->n_col)
3692                 for (i = 0; i < qp->div->n_row; ++i)
3693                         subs[morph->inv->n_row - 1 + i] =
3694                             isl_upoly_var_pow(ctx, morph->inv->n_col - 1 + i, 1);
3695
3696         qp->upoly = isl_upoly_subs(qp->upoly, 0, n_sub, subs);
3697
3698         for (i = 0; i < n_sub; ++i)
3699                 isl_upoly_free(subs[i]);
3700         free(subs);
3701
3702         mat = isl_mat_diagonal(isl_mat_identity(ctx, 1), isl_mat_copy(morph->inv));
3703         mat = isl_mat_diagonal(mat, isl_mat_identity(ctx, qp->div->n_row));
3704         qp->div = isl_mat_product(qp->div, mat);
3705         isl_dim_free(qp->dim);
3706         qp->dim = isl_dim_copy(morph->ran->dim);
3707
3708         if (!qp->upoly || !qp->div || !qp->dim)
3709                 goto error;
3710
3711         isl_morph_free(morph);
3712
3713         return qp;
3714 error:
3715         isl_qpolynomial_free(qp);
3716         isl_morph_free(morph);
3717         return NULL;
3718 }
3719
3720 static int neg_entry(void **entry, void *user)
3721 {
3722         isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
3723
3724         *pwqp = isl_pw_qpolynomial_neg(*pwqp);
3725
3726         return *pwqp ? 0 : -1;
3727 }
3728
3729 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_neg(
3730         __isl_take isl_union_pw_qpolynomial *upwqp)
3731 {
3732         upwqp = isl_union_pw_qpolynomial_cow(upwqp);
3733         if (!upwqp)
3734                 return NULL;
3735
3736         if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
3737                                    &neg_entry, NULL) < 0)
3738                 goto error;
3739
3740         return upwqp;
3741 error:
3742         isl_union_pw_qpolynomial_free(upwqp);
3743         return NULL;
3744 }
3745
3746 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sub(
3747         __isl_take isl_union_pw_qpolynomial *upwqp1,
3748         __isl_take isl_union_pw_qpolynomial *upwqp2)
3749 {
3750         return isl_union_pw_qpolynomial_add(upwqp1,
3751                                         isl_union_pw_qpolynomial_neg(upwqp2));
3752 }
3753
3754 static int mul_entry(void **entry, void *user)
3755 {
3756         struct isl_union_pw_qpolynomial_match_bin_data *data = user;
3757         uint32_t hash;
3758         struct isl_hash_table_entry *entry2;
3759         isl_pw_qpolynomial *pwpq = *entry;
3760         int empty;
3761
3762         hash = isl_dim_get_hash(pwpq->dim);
3763         entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
3764                                      hash, &has_dim, pwpq->dim, 0);
3765         if (!entry2)
3766                 return 0;
3767
3768         pwpq = isl_pw_qpolynomial_copy(pwpq);
3769         pwpq = isl_pw_qpolynomial_mul(pwpq,
3770                                       isl_pw_qpolynomial_copy(entry2->data));
3771
3772         empty = isl_pw_qpolynomial_is_zero(pwpq);
3773         if (empty < 0) {
3774                 isl_pw_qpolynomial_free(pwpq);
3775                 return -1;
3776         }
3777         if (empty) {
3778                 isl_pw_qpolynomial_free(pwpq);
3779                 return 0;
3780         }
3781
3782         data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(data->res, pwpq);
3783
3784         return 0;
3785 }
3786
3787 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_mul(
3788         __isl_take isl_union_pw_qpolynomial *upwqp1,
3789         __isl_take isl_union_pw_qpolynomial *upwqp2)
3790 {
3791         return match_bin_op(upwqp1, upwqp2, &mul_entry);
3792 }
3793
3794 /* Reorder the columns of the given div definitions according to the
3795  * given reordering.
3796  */
3797 static __isl_give isl_mat *reorder_divs(__isl_take isl_mat *div,
3798         __isl_take isl_reordering *r)
3799 {
3800         int i, j;
3801         isl_mat *mat;
3802         int extra;
3803
3804         if (!div || !r)
3805                 goto error;
3806
3807         extra = isl_dim_total(r->dim) + div->n_row - r->len;
3808         mat = isl_mat_alloc(div->ctx, div->n_row, div->n_col + extra);
3809         if (!mat)
3810                 goto error;
3811
3812         for (i = 0; i < div->n_row; ++i) {
3813                 isl_seq_cpy(mat->row[i], div->row[i], 2);
3814                 isl_seq_clr(mat->row[i] + 2, mat->n_col - 2);
3815                 for (j = 0; j < r->len; ++j)
3816                         isl_int_set(mat->row[i][2 + r->pos[j]],
3817                                     div->row[i][2 + j]);
3818         }
3819
3820         isl_reordering_free(r);
3821         isl_mat_free(div);
3822         return mat;
3823 error:
3824         isl_reordering_free(r);
3825         isl_mat_free(div);
3826         return NULL;
3827 }
3828
3829 /* Reorder the dimension of "qp" according to the given reordering.
3830  */
3831 __isl_give isl_qpolynomial *isl_qpolynomial_realign(
3832         __isl_take isl_qpolynomial *qp, __isl_take isl_reordering *r)
3833 {
3834         qp = isl_qpolynomial_cow(qp);
3835         if (!qp)
3836                 goto error;
3837
3838         r = isl_reordering_extend(r, qp->div->n_row);
3839         if (!r)
3840                 goto error;
3841
3842         qp->div = reorder_divs(qp->div, isl_reordering_copy(r));
3843         if (!qp->div)
3844                 goto error;
3845
3846         qp->upoly = reorder(qp->upoly, r->pos);
3847         if (!qp->upoly)
3848                 goto error;
3849
3850         qp = isl_qpolynomial_reset_dim(qp, isl_dim_copy(r->dim));
3851
3852         isl_reordering_free(r);
3853         return qp;
3854 error:
3855         isl_qpolynomial_free(qp);
3856         isl_reordering_free(r);
3857         return NULL;
3858 }
3859
3860 struct isl_split_periods_data {
3861         int max_periods;
3862         isl_pw_qpolynomial *res;
3863 };
3864
3865 /* Create a slice where the integer division "div" has the fixed value "v".
3866  * In particular, if "div" refers to floor(f/m), then create a slice
3867  *
3868  *      m v <= f <= m v + (m - 1)
3869  *
3870  * or
3871  *
3872  *      f - m v >= 0
3873  *      -f + m v + (m - 1) >= 0
3874  */
3875 static __isl_give isl_set *set_div_slice(__isl_take isl_dim *dim,
3876         __isl_keep isl_qpolynomial *qp, int div, isl_int v)
3877 {
3878         int total;
3879         isl_basic_set *bset = NULL;
3880         int k;
3881
3882         if (!dim || !qp)
3883                 goto error;
3884
3885         total = isl_dim_total(dim);
3886         bset = isl_basic_set_alloc_dim(isl_dim_copy(dim), 0, 0, 2);
3887
3888         k = isl_basic_set_alloc_inequality(bset);
3889         if (k < 0)
3890                 goto error;
3891         isl_seq_cpy(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3892         isl_int_submul(bset->ineq[k][0], v, qp->div->row[div][0]);
3893
3894         k = isl_basic_set_alloc_inequality(bset);
3895         if (k < 0)
3896                 goto error;
3897         isl_seq_neg(bset->ineq[k], qp->div->row[div] + 1, 1 + total);
3898         isl_int_addmul(bset->ineq[k][0], v, qp->div->row[div][0]);
3899         isl_int_add(bset->ineq[k][0], bset->ineq[k][0], qp->div->row[div][0]);
3900         isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
3901
3902         isl_dim_free(dim);
3903         return isl_set_from_basic_set(bset);
3904 error:
3905         isl_basic_set_free(bset);
3906         isl_dim_free(dim);
3907         return NULL;
3908 }
3909
3910 static int split_periods(__isl_take isl_set *set,
3911         __isl_take isl_qpolynomial *qp, void *user);
3912
3913 /* Create a slice of the domain "set" such that integer division "div"
3914  * has the fixed value "v" and add the results to data->res,
3915  * replacing the integer division by "v" in "qp".
3916  */
3917 static int set_div(__isl_take isl_set *set,
3918         __isl_take isl_qpolynomial *qp, int div, isl_int v,
3919         struct isl_split_periods_data *data)
3920 {
3921         int i;
3922         int total;
3923         isl_set *slice;
3924         struct isl_upoly *cst;
3925
3926         slice = set_div_slice(isl_set_get_dim(set), qp, div, v);
3927         set = isl_set_intersect(set, slice);
3928
3929         if (!qp)
3930                 goto error;
3931
3932         total = isl_dim_total(qp->dim);
3933
3934         for (i = div + 1; i < qp->div->n_row; ++i) {
3935                 if (isl_int_is_zero(qp->div->row[i][2 + total + div]))
3936                         continue;
3937                 isl_int_addmul(qp->div->row[i][1],
3938                                 qp->div->row[i][2 + total + div], v);
3939                 isl_int_set_si(qp->div->row[i][2 + total + div], 0);
3940         }
3941
3942         cst = isl_upoly_rat_cst(qp->dim->ctx, v, qp->dim->ctx->one);
3943         qp = substitute_div(qp, div, cst);
3944
3945         return split_periods(set, qp, data);
3946 error:
3947         isl_set_free(set);
3948         isl_qpolynomial_free(qp);
3949         return -1;
3950 }
3951
3952 /* Split the domain "set" such that integer division "div"
3953  * has a fixed value (ranging from "min" to "max") on each slice
3954  * and add the results to data->res.
3955  */
3956 static int split_div(__isl_take isl_set *set,
3957         __isl_take isl_qpolynomial *qp, int div, isl_int min, isl_int max,
3958         struct isl_split_periods_data *data)
3959 {
3960         for (; isl_int_le(min, max); isl_int_add_ui(min, min, 1)) {
3961                 isl_set *set_i = isl_set_copy(set);
3962                 isl_qpolynomial *qp_i = isl_qpolynomial_copy(qp);
3963
3964                 if (set_div(set_i, qp_i, div, min, data) < 0)
3965                         goto error;
3966         }
3967         isl_set_free(set);
3968         isl_qpolynomial_free(qp);
3969         return 0;
3970 error:
3971         isl_set_free(set);
3972         isl_qpolynomial_free(qp);
3973         return -1;
3974 }
3975
3976 /* If "qp" refers to any integer division
3977  * that can only attain "max_periods" distinct values on "set"
3978  * then split the domain along those distinct values.
3979  * Add the results (or the original if no splitting occurs)
3980  * to data->res.
3981  */
3982 static int split_periods(__isl_take isl_set *set,
3983         __isl_take isl_qpolynomial *qp, void *user)
3984 {
3985         int i;
3986         isl_pw_qpolynomial *pwqp;
3987         struct isl_split_periods_data *data;
3988         isl_int min, max;
3989         int total;
3990         int r = 0;
3991
3992         data = (struct isl_split_periods_data *)user;
3993
3994         if (!set || !qp)
3995                 goto error;
3996
3997         if (qp->div->n_row == 0) {
3998                 pwqp = isl_pw_qpolynomial_alloc(set, qp);
3999                 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4000                 return 0;
4001         }
4002
4003         isl_int_init(min);
4004         isl_int_init(max);
4005         total = isl_dim_total(qp->dim);
4006         for (i = 0; i < qp->div->n_row; ++i) {
4007                 enum isl_lp_result lp_res;
4008
4009                 if (isl_seq_first_non_zero(qp->div->row[i] + 2 + total,
4010                                                 qp->div->n_row) != -1)
4011                         continue;
4012
4013                 lp_res = isl_set_solve_lp(set, 0, qp->div->row[i] + 1,
4014                                           set->ctx->one, &min, NULL, NULL);
4015                 if (lp_res == isl_lp_error)
4016                         goto error2;
4017                 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4018                         continue;
4019                 isl_int_fdiv_q(min, min, qp->div->row[i][0]);
4020
4021                 lp_res = isl_set_solve_lp(set, 1, qp->div->row[i] + 1,
4022                                           set->ctx->one, &max, NULL, NULL);
4023                 if (lp_res == isl_lp_error)
4024                         goto error2;
4025                 if (lp_res == isl_lp_unbounded || lp_res == isl_lp_empty)
4026                         continue;
4027                 isl_int_fdiv_q(max, max, qp->div->row[i][0]);
4028
4029                 isl_int_sub(max, max, min);
4030                 if (isl_int_cmp_si(max, data->max_periods) < 0) {
4031                         isl_int_add(max, max, min);
4032                         break;
4033                 }
4034         }
4035
4036         if (i < qp->div->n_row) {
4037                 r = split_div(set, qp, i, min, max, data);
4038         } else {
4039                 pwqp = isl_pw_qpolynomial_alloc(set, qp);
4040                 data->res = isl_pw_qpolynomial_add_disjoint(data->res, pwqp);
4041         }
4042
4043         isl_int_clear(max);
4044         isl_int_clear(min);
4045
4046         return r;
4047 error2:
4048         isl_int_clear(max);
4049         isl_int_clear(min);
4050 error:
4051         isl_set_free(set);
4052         isl_qpolynomial_free(qp);
4053         return -1;
4054 }
4055
4056 /* If any quasi-polynomial in pwqp refers to any integer division
4057  * that can only attain "max_periods" distinct values on its domain
4058  * then split the domain along those distinct values.
4059  */
4060 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_split_periods(
4061         __isl_take isl_pw_qpolynomial *pwqp, int max_periods)
4062 {
4063         struct isl_split_periods_data data;
4064
4065         data.max_periods = max_periods;
4066         data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4067
4068         if (isl_pw_qpolynomial_foreach_piece(pwqp, &split_periods, &data) < 0)
4069                 goto error;
4070
4071         isl_pw_qpolynomial_free(pwqp);
4072
4073         return data.res;
4074 error:
4075         isl_pw_qpolynomial_free(data.res);
4076         isl_pw_qpolynomial_free(pwqp);
4077         return NULL;
4078 }
4079
4080 /* Construct a piecewise quasipolynomial that is constant on the given
4081  * domain.  In particular, it is
4082  *      0       if cst == 0
4083  *      1       if cst == 1
4084  *  infinity    if cst == -1
4085  */
4086 static __isl_give isl_pw_qpolynomial *constant_on_domain(
4087         __isl_take isl_basic_set *bset, int cst)
4088 {
4089         isl_dim *dim;
4090         isl_qpolynomial *qp;
4091
4092         if (!bset)
4093                 return NULL;
4094
4095         bset = isl_basic_map_domain(isl_basic_map_from_range(bset));
4096         dim = isl_basic_set_get_dim(bset);
4097         if (cst < 0)
4098                 qp = isl_qpolynomial_infty(dim);
4099         else if (cst == 0)
4100                 qp = isl_qpolynomial_zero(dim);
4101         else
4102                 qp = isl_qpolynomial_one(dim);
4103         return isl_pw_qpolynomial_alloc(isl_set_from_basic_set(bset), qp);
4104 }
4105
4106 /* Factor bset, call fn on each of the factors and return the product.
4107  *
4108  * If no factors can be found, simply call fn on the input.
4109  * Otherwise, construct the factors based on the factorizer,
4110  * call fn on each factor and compute the product.
4111  */
4112 static __isl_give isl_pw_qpolynomial *compressed_multiplicative_call(
4113         __isl_take isl_basic_set *bset,
4114         __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4115 {
4116         int i, n;
4117         isl_dim *dim;
4118         isl_set *set;
4119         isl_factorizer *f;
4120         isl_qpolynomial *qp;
4121         isl_pw_qpolynomial *pwqp;
4122         unsigned nparam;
4123         unsigned nvar;
4124
4125         f = isl_basic_set_factorizer(bset);
4126         if (!f)
4127                 goto error;
4128         if (f->n_group == 0) {
4129                 isl_factorizer_free(f);
4130                 return fn(bset);
4131         }
4132
4133         nparam = isl_basic_set_dim(bset, isl_dim_param);
4134         nvar = isl_basic_set_dim(bset, isl_dim_set);
4135
4136         dim = isl_basic_set_get_dim(bset);
4137         dim = isl_dim_domain(dim);
4138         set = isl_set_universe(isl_dim_copy(dim));
4139         qp = isl_qpolynomial_one(dim);
4140         pwqp = isl_pw_qpolynomial_alloc(set, qp);
4141
4142         bset = isl_morph_basic_set(isl_morph_copy(f->morph), bset);
4143
4144         for (i = 0, n = 0; i < f->n_group; ++i) {
4145                 isl_basic_set *bset_i;
4146                 isl_pw_qpolynomial *pwqp_i;
4147
4148                 bset_i = isl_basic_set_copy(bset);
4149                 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4150                             nparam + n + f->len[i], nvar - n - f->len[i]);
4151                 bset_i = isl_basic_set_drop_constraints_involving(bset_i,
4152                             nparam, n);
4153                 bset_i = isl_basic_set_drop(bset_i, isl_dim_set,
4154                             n + f->len[i], nvar - n - f->len[i]);
4155                 bset_i = isl_basic_set_drop(bset_i, isl_dim_set, 0, n);
4156
4157                 pwqp_i = fn(bset_i);
4158                 pwqp = isl_pw_qpolynomial_mul(pwqp, pwqp_i);
4159
4160                 n += f->len[i];
4161         }
4162
4163         isl_basic_set_free(bset);
4164         isl_factorizer_free(f);
4165
4166         return pwqp;
4167 error:
4168         isl_basic_set_free(bset);
4169         return NULL;
4170 }
4171
4172 /* Factor bset, call fn on each of the factors and return the product.
4173  * The function is assumed to evaluate to zero on empty domains,
4174  * to one on zero-dimensional domains and to infinity on unbounded domains
4175  * and will not be called explicitly on zero-dimensional or unbounded domains.
4176  *
4177  * We first check for some special cases and remove all equalities.
4178  * Then we hand over control to compressed_multiplicative_call.
4179  */
4180 __isl_give isl_pw_qpolynomial *isl_basic_set_multiplicative_call(
4181         __isl_take isl_basic_set *bset,
4182         __isl_give isl_pw_qpolynomial *(*fn)(__isl_take isl_basic_set *bset))
4183 {
4184         int bounded;
4185         isl_morph *morph;
4186         isl_pw_qpolynomial *pwqp;
4187         unsigned orig_nvar, final_nvar;
4188
4189         if (!bset)
4190                 return NULL;
4191
4192         if (isl_basic_set_fast_is_empty(bset))
4193                 return constant_on_domain(bset, 0);
4194
4195         orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
4196
4197         if (orig_nvar == 0)
4198                 return constant_on_domain(bset, 1);
4199
4200         bounded = isl_basic_set_is_bounded(bset);
4201         if (bounded < 0)
4202                 goto error;
4203         if (!bounded)
4204                 return constant_on_domain(bset, -1);
4205
4206         if (bset->n_eq == 0)
4207                 return compressed_multiplicative_call(bset, fn);
4208
4209         morph = isl_basic_set_full_compression(bset);
4210         bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
4211
4212         final_nvar = isl_basic_set_dim(bset, isl_dim_set);
4213
4214         pwqp = compressed_multiplicative_call(bset, fn);
4215
4216         morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
4217         morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
4218         morph = isl_morph_inverse(morph);
4219
4220         pwqp = isl_pw_qpolynomial_morph(pwqp, morph);
4221
4222         return pwqp;
4223 error:
4224         isl_basic_set_free(bset);
4225         return NULL;
4226 }
4227
4228 /* Drop all floors in "qp", turning each integer division [a/m] into
4229  * a rational division a/m.  If "down" is set, then the integer division
4230  * is replaces by (a-(m-1))/m instead.
4231  */
4232 static __isl_give isl_qpolynomial *qp_drop_floors(
4233         __isl_take isl_qpolynomial *qp, int down)
4234 {
4235         int i;
4236         struct isl_upoly *s;
4237
4238         if (!qp)
4239                 return NULL;
4240         if (qp->div->n_row == 0)
4241                 return qp;
4242
4243         qp = isl_qpolynomial_cow(qp);
4244         if (!qp)
4245                 return NULL;
4246
4247         for (i = qp->div->n_row - 1; i >= 0; --i) {
4248                 if (down) {
4249                         isl_int_sub(qp->div->row[i][1],
4250                                     qp->div->row[i][1], qp->div->row[i][0]);
4251                         isl_int_add_ui(qp->div->row[i][1],
4252                                        qp->div->row[i][1], 1);
4253                 }
4254                 s = isl_upoly_from_affine(qp->dim->ctx, qp->div->row[i] + 1,
4255                                         qp->div->row[i][0], qp->div->n_col - 1);
4256                 qp = substitute_div(qp, i, s);
4257                 if (!qp)
4258                         return NULL;
4259         }
4260
4261         return qp;
4262 }
4263
4264 /* Drop all floors in "pwqp", turning each integer division [a/m] into
4265  * a rational division a/m.
4266  */
4267 static __isl_give isl_pw_qpolynomial *pwqp_drop_floors(
4268         __isl_take isl_pw_qpolynomial *pwqp)
4269 {
4270         int i;
4271
4272         if (!pwqp)
4273                 return NULL;
4274
4275         if (isl_pw_qpolynomial_is_zero(pwqp))
4276                 return pwqp;
4277
4278         pwqp = isl_pw_qpolynomial_cow(pwqp);
4279         if (!pwqp)
4280                 return NULL;
4281
4282         for (i = 0; i < pwqp->n; ++i) {
4283                 pwqp->p[i].qp = qp_drop_floors(pwqp->p[i].qp, 0);
4284                 if (!pwqp->p[i].qp)
4285                         goto error;
4286         }
4287
4288         return pwqp;
4289 error:
4290         isl_pw_qpolynomial_free(pwqp);
4291         return NULL;
4292 }
4293
4294 /* Adjust all the integer divisions in "qp" such that they are at least
4295  * one over the given orthant (identified by "signs").  This ensures
4296  * that they will still be non-negative even after subtracting (m-1)/m.
4297  *
4298  * In particular, f is replaced by f' + v, changing f = [a/m]
4299  * to f' = [(a - m v)/m].
4300  * If the constant term k in a is smaller than m,
4301  * the constant term of v is set to floor(k/m) - 1.
4302  * For any other term, if the coefficient c and the variable x have
4303  * the same sign, then no changes are needed.
4304  * Otherwise, if the variable is positive (and c is negative),
4305  * then the coefficient of x in v is set to floor(c/m).
4306  * If the variable is negative (and c is positive),
4307  * then the coefficient of x in v is set to ceil(c/m).
4308  */
4309 static __isl_give isl_qpolynomial *make_divs_pos(__isl_take isl_qpolynomial *qp,
4310         int *signs)
4311 {
4312         int i, j;
4313         int total;
4314         isl_vec *v = NULL;
4315         struct isl_upoly *s;
4316
4317         qp = isl_qpolynomial_cow(qp);
4318         if (!qp)
4319                 return NULL;
4320         qp->div = isl_mat_cow(qp->div);
4321         if (!qp->div)
4322                 goto error;
4323
4324         total = isl_dim_total(qp->dim);
4325         v = isl_vec_alloc(qp->div->ctx, qp->div->n_col - 1);
4326
4327         for (i = 0; i < qp->div->n_row; ++i) {
4328                 isl_int *row = qp->div->row[i];
4329                 v = isl_vec_clr(v);
4330                 if (!v)
4331                         goto error;
4332                 if (isl_int_lt(row[1], row[0])) {
4333                         isl_int_fdiv_q(v->el[0], row[1], row[0]);
4334                         isl_int_sub_ui(v->el[0], v->el[0], 1);
4335                         isl_int_submul(row[1], row[0], v->el[0]);
4336                 }
4337                 for (j = 0; j < total; ++j) {
4338                         if (isl_int_sgn(row[2 + j]) * signs[j] >= 0)
4339                                 continue;
4340                         if (signs[j] < 0)
4341                                 isl_int_cdiv_q(v->el[1 + j], row[2 + j], row[0]);
4342                         else
4343                                 isl_int_fdiv_q(v->el[1 + j], row[2 + j], row[0]);
4344                         isl_int_submul(row[2 + j], row[0], v->el[1 + j]);
4345                 }
4346                 for (j = 0; j < i; ++j) {
4347                         if (isl_int_sgn(row[2 + total + j]) >= 0)
4348                                 continue;
4349                         isl_int_fdiv_q(v->el[1 + total + j],
4350                                         row[2 + total + j], row[0]);
4351                         isl_int_submul(row[2 + total + j],
4352                                         row[0], v->el[1 + total + j]);
4353                 }
4354                 for (j = i + 1; j < qp->div->n_row; ++j) {
4355                         if (isl_int_is_zero(qp->div->row[j][2 + total + i]))
4356                                 continue;
4357                         isl_seq_combine(qp->div->row[j] + 1,
4358                                 qp->div->ctx->one, qp->div->row[j] + 1,
4359                                 qp->div->row[j][2 + total + i], v->el, v->size);
4360                 }
4361                 isl_int_set_si(v->el[1 + total + i], 1);
4362                 s = isl_upoly_from_affine(qp->dim->ctx, v->el,
4363                                         qp->div->ctx->one, v->size);
4364                 qp->upoly = isl_upoly_subs(qp->upoly, total + i, 1, &s);
4365                 isl_upoly_free(s);
4366                 if (!qp->upoly)
4367                         goto error;
4368         }
4369
4370         isl_vec_free(v);
4371         return qp;
4372 error:
4373         isl_vec_free(v);
4374         isl_qpolynomial_free(qp);
4375         return NULL;
4376 }
4377
4378 struct isl_to_poly_data {
4379         int sign;
4380         isl_pw_qpolynomial *res;
4381         isl_qpolynomial *qp;
4382 };
4383
4384 /* Appoximate data->qp by a polynomial on the orthant identified by "signs".
4385  * We first make all integer divisions positive and then split the
4386  * quasipolynomials into terms with sign data->sign (the direction
4387  * of the requested approximation) and terms with the opposite sign.
4388  * In the first set of terms, each integer division [a/m] is
4389  * overapproximated by a/m, while in the second it is underapproximated
4390  * by (a-(m-1))/m.
4391  */
4392 static int to_polynomial_on_orthant(__isl_take isl_set *orthant, int *signs,
4393         void *user)
4394 {
4395         struct isl_to_poly_data *data = user;
4396         isl_pw_qpolynomial *t;
4397         isl_qpolynomial *qp, *up, *down;
4398
4399         qp = isl_qpolynomial_copy(data->qp);
4400         qp = make_divs_pos(qp, signs);
4401
4402         up = isl_qpolynomial_terms_of_sign(qp, signs, data->sign);
4403         up = qp_drop_floors(up, 0);
4404         down = isl_qpolynomial_terms_of_sign(qp, signs, -data->sign);
4405         down = qp_drop_floors(down, 1);
4406
4407         isl_qpolynomial_free(qp);
4408         qp = isl_qpolynomial_add(up, down);
4409
4410         t = isl_pw_qpolynomial_alloc(orthant, qp);
4411         data->res = isl_pw_qpolynomial_add_disjoint(data->res, t);
4412
4413         return 0;
4414 }
4415
4416 /* Approximate each quasipolynomial by a polynomial.  If "sign" is positive,
4417  * the polynomial will be an overapproximation.  If "sign" is negative,
4418  * it will be an underapproximation.  If "sign" is zero, the approximation
4419  * will lie somewhere in between.
4420  *
4421  * In particular, is sign == 0, we simply drop the floors, turning
4422  * the integer divisions into rational divisions.
4423  * Otherwise, we split the domains into orthants, make all integer divisions
4424  * positive and then approximate each [a/m] by either a/m or (a-(m-1))/m,
4425  * depending on the requested sign and the sign of the term in which
4426  * the integer division appears.
4427  */
4428 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_to_polynomial(
4429         __isl_take isl_pw_qpolynomial *pwqp, int sign)
4430 {
4431         int i;
4432         struct isl_to_poly_data data;
4433
4434         if (sign == 0)
4435                 return pwqp_drop_floors(pwqp);
4436
4437         if (!pwqp)
4438                 return NULL;
4439
4440         data.sign = sign;
4441         data.res = isl_pw_qpolynomial_zero(isl_pw_qpolynomial_get_dim(pwqp));
4442
4443         for (i = 0; i < pwqp->n; ++i) {
4444                 if (pwqp->p[i].qp->div->n_row == 0) {
4445                         isl_pw_qpolynomial *t;
4446                         t = isl_pw_qpolynomial_alloc(
4447                                         isl_set_copy(pwqp->p[i].set),
4448                                         isl_qpolynomial_copy(pwqp->p[i].qp));
4449                         data.res = isl_pw_qpolynomial_add_disjoint(data.res, t);
4450                         continue;
4451                 }
4452                 data.qp = pwqp->p[i].qp;
4453                 if (isl_set_foreach_orthant(pwqp->p[i].set,
4454                                         &to_polynomial_on_orthant, &data) < 0)
4455                         goto error;
4456         }
4457
4458         isl_pw_qpolynomial_free(pwqp);
4459
4460         return data.res;
4461 error:
4462         isl_pw_qpolynomial_free(pwqp);
4463         isl_pw_qpolynomial_free(data.res);
4464         return NULL;
4465 }
4466
4467 static int poly_entry(void **entry, void *user)
4468 {
4469         int *sign = user;
4470         isl_pw_qpolynomial **pwqp = (isl_pw_qpolynomial **)entry;
4471
4472         *pwqp = isl_pw_qpolynomial_to_polynomial(*pwqp, *sign);
4473
4474         return *pwqp ? 0 : -1;
4475 }
4476
4477 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_to_polynomial(
4478         __isl_take isl_union_pw_qpolynomial *upwqp, int sign)
4479 {
4480         upwqp = isl_union_pw_qpolynomial_cow(upwqp);
4481         if (!upwqp)
4482                 return NULL;
4483
4484         if (isl_hash_table_foreach(upwqp->dim->ctx, &upwqp->table,
4485                                    &poly_entry, &sign) < 0)
4486                 goto error;
4487
4488         return upwqp;
4489 error:
4490         isl_union_pw_qpolynomial_free(upwqp);
4491         return NULL;
4492 }