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