isl_mat_lin_to_aff: fix error handling
[platform/upstream/isl.git] / isl_mat.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include "isl_dim.h"
11 #include "isl_seq.h"
12 #include "isl_mat.h"
13 #include "isl_map_private.h"
14
15 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
16         unsigned n_row, unsigned n_col)
17 {
18         int i;
19         struct isl_mat *mat;
20
21         mat = isl_alloc_type(ctx, struct isl_mat);
22         if (!mat)
23                 return NULL;
24
25         mat->row = NULL;
26         mat->block = isl_blk_alloc(ctx, n_row * n_col);
27         if (isl_blk_is_error(mat->block))
28                 goto error;
29         mat->row = isl_alloc_array(ctx, isl_int *, n_row);
30         if (!mat->row)
31                 goto error;
32
33         for (i = 0; i < n_row; ++i)
34                 mat->row[i] = mat->block.data + i * n_col;
35
36         mat->ctx = ctx;
37         isl_ctx_ref(ctx);
38         mat->ref = 1;
39         mat->n_row = n_row;
40         mat->n_col = n_col;
41         mat->max_col = n_col;
42         mat->flags = 0;
43
44         return mat;
45 error:
46         isl_blk_free(ctx, mat->block);
47         free(mat);
48         return NULL;
49 }
50
51 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
52         unsigned n_row, unsigned n_col)
53 {
54         int i;
55         isl_int *old;
56
57         if (!mat)
58                 return NULL;
59
60         if (mat->max_col >= n_col && mat->n_row >= n_row) {
61                 if (mat->n_col < n_col)
62                         mat->n_col = n_col;
63                 return mat;
64         }
65
66         if (mat->max_col < n_col) {
67                 struct isl_mat *new_mat;
68
69                 if (n_row < mat->n_row)
70                         n_row = mat->n_row;
71                 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
72                 if (!new_mat)
73                         goto error;
74                 for (i = 0; i < mat->n_row; ++i)
75                         isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
76                 isl_mat_free(mat);
77                 return new_mat;
78         }
79
80         mat = isl_mat_cow(mat);
81         if (!mat)
82                 goto error;
83
84         assert(mat->ref == 1);
85         old = mat->block.data;
86         mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
87         if (isl_blk_is_error(mat->block))
88                 goto error;
89         mat->row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
90         if (!mat->row)
91                 goto error;
92
93         for (i = 0; i < mat->n_row; ++i)
94                 mat->row[i] = mat->block.data + (mat->row[i] - old);
95         for (i = mat->n_row; i < n_row; ++i)
96                 mat->row[i] = mat->block.data + i * mat->max_col;
97         mat->n_row = n_row;
98         if (mat->n_col < n_col)
99                 mat->n_col = n_col;
100
101         return mat;
102 error:
103         isl_mat_free(mat);
104         return NULL;
105 }
106
107 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
108         unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
109 {
110         int i;
111         struct isl_mat *mat;
112
113         mat = isl_alloc_type(ctx, struct isl_mat);
114         if (!mat)
115                 return NULL;
116         mat->row = isl_alloc_array(ctx, isl_int *, n_row);
117         if (!mat->row)
118                 goto error;
119         for (i = 0; i < n_row; ++i)
120                 mat->row[i] = row[first_row+i] + first_col;
121         mat->ctx = ctx;
122         isl_ctx_ref(ctx);
123         mat->ref = 1;
124         mat->n_row = n_row;
125         mat->n_col = n_col;
126         mat->block = isl_blk_empty();
127         mat->flags = ISL_MAT_BORROWED;
128         return mat;
129 error:
130         free(mat);
131         return NULL;
132 }
133
134 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
135         unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
136 {
137         int i;
138
139         for (i = 0; i < n_row; ++i)
140                 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
141 }
142
143 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
144         unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
145 {
146         int i;
147
148         for (i = 0; i < n_row; ++i)
149                 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
150 }
151
152 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
153 {
154         if (!mat)
155                 return NULL;
156
157         mat->ref++;
158         return mat;
159 }
160
161 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
162 {
163         int i;
164         struct isl_mat *mat2;
165
166         if (!mat)
167                 return NULL;
168         mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
169         if (!mat2)
170                 return NULL;
171         for (i = 0; i < mat->n_row; ++i)
172                 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
173         return mat2;
174 }
175
176 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
177 {
178         struct isl_mat *mat2;
179         if (!mat)
180                 return NULL;
181
182         if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
183                 return mat;
184
185         mat2 = isl_mat_dup(mat);
186         isl_mat_free(mat);
187         return mat2;
188 }
189
190 void isl_mat_free(struct isl_mat *mat)
191 {
192         if (!mat)
193                 return;
194
195         if (--mat->ref > 0)
196                 return;
197
198         if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
199                 isl_blk_free(mat->ctx, mat->block);
200         isl_ctx_deref(mat->ctx);
201         free(mat->row);
202         free(mat);
203 }
204
205 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
206 {
207         int i;
208         struct isl_mat *mat;
209
210         mat = isl_mat_alloc(ctx, n_row, n_row);
211         if (!mat)
212                 return NULL;
213         for (i = 0; i < n_row; ++i) {
214                 isl_seq_clr(mat->row[i], i);
215                 isl_int_set_si(mat->row[i][i], 1);
216                 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
217         }
218
219         return mat;
220 }
221
222 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
223 {
224         int i;
225         struct isl_vec *prod;
226
227         if (!mat || !vec)
228                 goto error;
229
230         isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
231
232         prod = isl_vec_alloc(mat->ctx, mat->n_row);
233         if (!prod)
234                 goto error;
235
236         for (i = 0; i < prod->size; ++i)
237                 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
238                                         &prod->block.data[i]);
239         isl_mat_free(mat);
240         isl_vec_free(vec);
241         return prod;
242 error:
243         isl_mat_free(mat);
244         isl_vec_free(vec);
245         return NULL;
246 }
247
248 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
249         __isl_take isl_vec *vec)
250 {
251         struct isl_mat *vec_mat;
252         int i;
253
254         if (!mat || !vec)
255                 goto error;
256         vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
257         if (!vec_mat)
258                 goto error;
259         for (i = 0; i < vec->size; ++i)
260                 isl_int_set(vec_mat->row[i][0], vec->el[i]);
261         vec_mat = isl_mat_inverse_product(mat, vec_mat);
262         isl_vec_free(vec);
263         if (!vec_mat)
264                 return NULL;
265         vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
266         if (vec)
267                 for (i = 0; i < vec->size; ++i)
268                         isl_int_set(vec->el[i], vec_mat->row[i][0]);
269         isl_mat_free(vec_mat);
270         return vec;
271 error:
272         isl_mat_free(mat);
273         isl_vec_free(vec);
274         return NULL;
275 }
276
277 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
278 {
279         int i, j;
280         struct isl_vec *prod;
281
282         if (!mat || !vec)
283                 goto error;
284
285         isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
286
287         prod = isl_vec_alloc(mat->ctx, mat->n_col);
288         if (!prod)
289                 goto error;
290
291         for (i = 0; i < prod->size; ++i) {
292                 isl_int_set_si(prod->el[i], 0);
293                 for (j = 0; j < vec->size; ++j)
294                         isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
295         }
296         isl_mat_free(mat);
297         isl_vec_free(vec);
298         return prod;
299 error:
300         isl_mat_free(mat);
301         isl_vec_free(vec);
302         return NULL;
303 }
304
305 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
306         struct isl_mat *right)
307 {
308         int i;
309         struct isl_mat *sum;
310
311         if (!left || !right)
312                 goto error;
313
314         isl_assert(left->ctx, left->n_row == right->n_row, goto error);
315         isl_assert(left->ctx, left->n_row >= 1, goto error);
316         isl_assert(left->ctx, left->n_col >= 1, goto error);
317         isl_assert(left->ctx, right->n_col >= 1, goto error);
318         isl_assert(left->ctx,
319             isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
320             goto error);
321         isl_assert(left->ctx,
322             isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
323             goto error);
324
325         sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
326         if (!sum)
327                 goto error;
328         isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
329         isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
330         isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
331
332         isl_seq_clr(sum->row[0]+1, sum->n_col-1);
333         for (i = 1; i < sum->n_row; ++i) {
334                 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
335                 isl_int_addmul(sum->row[i][0],
336                                 right->row[0][0], right->row[i][0]);
337                 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
338                                 left->n_col-1);
339                 isl_seq_scale(sum->row[i]+left->n_col,
340                                 right->row[i]+1, right->row[0][0],
341                                 right->n_col-1);
342         }
343
344         isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
345         isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
346         isl_mat_free(left);
347         isl_mat_free(right);
348         return sum;
349 error:
350         isl_mat_free(left);
351         isl_mat_free(right);
352         return NULL;
353 }
354
355 static void exchange(struct isl_mat *M, struct isl_mat **U,
356         struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
357 {
358         int r;
359         for (r = row; r < M->n_row; ++r)
360                 isl_int_swap(M->row[r][i], M->row[r][j]);
361         if (U) {
362                 for (r = 0; r < (*U)->n_row; ++r)
363                         isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
364         }
365         if (Q)
366                 isl_mat_swap_rows(*Q, i, j);
367 }
368
369 static void subtract(struct isl_mat *M, struct isl_mat **U,
370         struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
371 {
372         int r;
373         for (r = row; r < M->n_row; ++r)
374                 isl_int_submul(M->row[r][j], m, M->row[r][i]);
375         if (U) {
376                 for (r = 0; r < (*U)->n_row; ++r)
377                         isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
378         }
379         if (Q) {
380                 for (r = 0; r < (*Q)->n_col; ++r)
381                         isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
382         }
383 }
384
385 static void oppose(struct isl_mat *M, struct isl_mat **U,
386         struct isl_mat **Q, unsigned row, unsigned col)
387 {
388         int r;
389         for (r = row; r < M->n_row; ++r)
390                 isl_int_neg(M->row[r][col], M->row[r][col]);
391         if (U) {
392                 for (r = 0; r < (*U)->n_row; ++r)
393                         isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
394         }
395         if (Q)
396                 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
397 }
398
399 /* Given matrix M, compute
400  *
401  *              M U = H
402  *              M   = H Q
403  *
404  * with U and Q unimodular matrices and H a matrix in column echelon form
405  * such that on each echelon row the entries in the non-echelon column
406  * are non-negative (if neg == 0) or non-positive (if neg == 1)
407  * and stricly smaller (in absolute value) than the entries in the echelon
408  * column.
409  * If U or Q are NULL, then these matrices are not computed.
410  */
411 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
412         struct isl_mat **U, struct isl_mat **Q)
413 {
414         isl_int c;
415         int row, col;
416
417         if (U)
418                 *U = NULL;
419         if (Q)
420                 *Q = NULL;
421         if (!M)
422                 goto error;
423         M = isl_mat_cow(M);
424         if (!M)
425                 goto error;
426         if (U) {
427                 *U = isl_mat_identity(M->ctx, M->n_col);
428                 if (!*U)
429                         goto error;
430         }
431         if (Q) {
432                 *Q = isl_mat_identity(M->ctx, M->n_col);
433                 if (!*Q)
434                         goto error;
435         }
436
437         col = 0;
438         isl_int_init(c);
439         for (row = 0; row < M->n_row; ++row) {
440                 int first, i, off;
441                 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
442                 if (first == -1)
443                         continue;
444                 first += col;
445                 if (first != col)
446                         exchange(M, U, Q, row, first, col);
447                 if (isl_int_is_neg(M->row[row][col]))
448                         oppose(M, U, Q, row, col);
449                 first = col+1;
450                 while ((off = isl_seq_first_non_zero(M->row[row]+first,
451                                                        M->n_col-first)) != -1) {
452                         first += off;
453                         isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
454                         subtract(M, U, Q, row, col, first, c);
455                         if (!isl_int_is_zero(M->row[row][first]))
456                                 exchange(M, U, Q, row, first, col);
457                         else
458                                 ++first;
459                 }
460                 for (i = 0; i < col; ++i) {
461                         if (isl_int_is_zero(M->row[row][i]))
462                                 continue;
463                         if (neg)
464                                 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
465                         else
466                                 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
467                         if (isl_int_is_zero(c))
468                                 continue;
469                         subtract(M, U, Q, row, col, i, c);
470                 }
471                 ++col;
472         }
473         isl_int_clear(c);
474
475         return M;
476 error:
477         if (Q) {
478                 isl_mat_free(*Q);
479                 *Q = NULL;
480         }
481         if (U) {
482                 isl_mat_free(*U);
483                 *U = NULL;
484         }
485         return NULL;
486 }
487
488 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
489 {
490         int i, rank;
491         struct isl_mat *U = NULL;
492         struct isl_mat *K;
493
494         mat = isl_mat_left_hermite(mat, 0, &U, NULL);
495         if (!mat || !U)
496                 goto error;
497
498         for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
499                 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
500                         ++i;
501                 if (i >= mat->n_row)
502                         break;
503         }
504         K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
505         if (!K)
506                 goto error;
507         isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
508         isl_mat_free(mat);
509         isl_mat_free(U);
510         return K;
511 error:
512         isl_mat_free(mat);
513         isl_mat_free(U);
514         return NULL;
515 }
516
517 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
518 {
519         int i;
520         struct isl_mat *mat2;
521
522         if (!mat)
523                 return NULL;
524         mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
525         if (!mat2)
526                 goto error;
527         isl_int_set_si(mat2->row[0][0], 1);
528         isl_seq_clr(mat2->row[0]+1, mat->n_col);
529         for (i = 0; i < mat->n_row; ++i) {
530                 isl_int_set_si(mat2->row[1+i][0], 0);
531                 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
532         }
533         isl_mat_free(mat);
534         return mat2;
535 error:
536         isl_mat_free(mat);
537         return NULL;
538 }
539
540 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
541 {
542         int i;
543
544         for (i = 0; i < n_row; ++i)
545                 if (!isl_int_is_zero(row[i][col]))
546                         return i;
547         return -1;
548 }
549
550 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
551 {
552         int i, min = row_first_non_zero(row, n_row, col);
553         if (min < 0)
554                 return -1;
555         for (i = min + 1; i < n_row; ++i) {
556                 if (isl_int_is_zero(row[i][col]))
557                         continue;
558                 if (isl_int_abs_lt(row[i][col], row[min][col]))
559                         min = i;
560         }
561         return min;
562 }
563
564 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
565         unsigned i, unsigned j)
566 {
567         left = isl_mat_swap_rows(left, i, j);
568         right = isl_mat_swap_rows(right, i, j);
569 }
570
571 static void inv_oppose(
572         struct isl_mat *left, struct isl_mat *right, unsigned row)
573 {
574         isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
575         isl_seq_neg(right->row[row], right->row[row], right->n_col);
576 }
577
578 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
579         unsigned row, unsigned i, isl_int m)
580 {
581         isl_int_neg(m, m);
582         isl_seq_combine(left->row[i]+row,
583                         left->ctx->one, left->row[i]+row,
584                         m, left->row[row]+row,
585                         left->n_col-row);
586         isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
587                         m, right->row[row], right->n_col);
588 }
589
590 /* Compute inv(left)*right
591  */
592 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
593         struct isl_mat *right)
594 {
595         int row;
596         isl_int a, b;
597
598         if (!left || !right)
599                 goto error;
600
601         isl_assert(left->ctx, left->n_row == left->n_col, goto error);
602         isl_assert(left->ctx, left->n_row == right->n_row, goto error);
603
604         if (left->n_row == 0) {
605                 isl_mat_free(left);
606                 return right;
607         }
608
609         left = isl_mat_cow(left);
610         right = isl_mat_cow(right);
611         if (!left || !right)
612                 goto error;
613
614         isl_int_init(a);
615         isl_int_init(b);
616         for (row = 0; row < left->n_row; ++row) {
617                 int pivot, first, i, off;
618                 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
619                 if (pivot < 0) {
620                         isl_int_clear(a);
621                         isl_int_clear(b);
622                         isl_assert(left->ctx, pivot >= 0, goto error);
623                 }
624                 pivot += row;
625                 if (pivot != row)
626                         inv_exchange(left, right, pivot, row);
627                 if (isl_int_is_neg(left->row[row][row]))
628                         inv_oppose(left, right, row);
629                 first = row+1;
630                 while ((off = row_first_non_zero(left->row+first,
631                                         left->n_row-first, row)) != -1) {
632                         first += off;
633                         isl_int_fdiv_q(a, left->row[first][row],
634                                         left->row[row][row]);
635                         inv_subtract(left, right, row, first, a);
636                         if (!isl_int_is_zero(left->row[first][row]))
637                                 inv_exchange(left, right, row, first);
638                         else
639                                 ++first;
640                 }
641                 for (i = 0; i < row; ++i) {
642                         if (isl_int_is_zero(left->row[i][row]))
643                                 continue;
644                         isl_int_gcd(a, left->row[row][row], left->row[i][row]);
645                         isl_int_divexact(b, left->row[i][row], a);
646                         isl_int_divexact(a, left->row[row][row], a);
647                         isl_int_neg(b, b);
648                         isl_seq_combine(left->row[i] + i,
649                                         a, left->row[i] + i,
650                                         b, left->row[row] + i,
651                                         left->n_col - i);
652                         isl_seq_combine(right->row[i], a, right->row[i],
653                                         b, right->row[row], right->n_col);
654                 }
655         }
656         isl_int_clear(b);
657
658         isl_int_set(a, left->row[0][0]);
659         for (row = 1; row < left->n_row; ++row)
660                 isl_int_lcm(a, a, left->row[row][row]);
661         if (isl_int_is_zero(a)){
662                 isl_int_clear(a);
663                 isl_assert(left->ctx, 0, goto error);
664         }
665         for (row = 0; row < left->n_row; ++row) {
666                 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
667                 if (isl_int_is_one(left->row[row][row]))
668                         continue;
669                 isl_seq_scale(right->row[row], right->row[row],
670                                 left->row[row][row], right->n_col);
671         }
672         isl_int_clear(a);
673
674         isl_mat_free(left);
675         return right;
676 error:
677         isl_mat_free(left);
678         isl_mat_free(right);
679         return NULL;
680 }
681
682 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
683 {
684         int i;
685
686         for (i = 0; i < mat->n_row; ++i)
687                 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
688 }
689
690 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
691         isl_int m1, unsigned src1, isl_int m2, unsigned src2)
692 {
693         int i;
694         isl_int tmp;
695
696         isl_int_init(tmp);
697         for (i = 0; i < mat->n_row; ++i) {
698                 isl_int_mul(tmp, m1, mat->row[i][src1]);
699                 isl_int_addmul(tmp, m2, mat->row[i][src2]);
700                 isl_int_set(mat->row[i][dst], tmp);
701         }
702         isl_int_clear(tmp);
703 }
704
705 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
706 {
707         struct isl_mat *inv;
708         int row;
709         isl_int a, b;
710
711         mat = isl_mat_cow(mat);
712         if (!mat)
713                 return NULL;
714
715         inv = isl_mat_identity(mat->ctx, mat->n_col);
716         inv = isl_mat_cow(inv);
717         if (!inv)
718                 goto error;
719
720         isl_int_init(a);
721         isl_int_init(b);
722         for (row = 0; row < mat->n_row; ++row) {
723                 int pivot, first, i, off;
724                 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
725                 if (pivot < 0) {
726                         isl_int_clear(a);
727                         isl_int_clear(b);
728                         isl_assert(mat->ctx, pivot >= 0, goto error);
729                 }
730                 pivot += row;
731                 if (pivot != row)
732                         exchange(mat, &inv, NULL, row, pivot, row);
733                 if (isl_int_is_neg(mat->row[row][row]))
734                         oppose(mat, &inv, NULL, row, row);
735                 first = row+1;
736                 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
737                                                     mat->n_col-first)) != -1) {
738                         first += off;
739                         isl_int_fdiv_q(a, mat->row[row][first],
740                                                     mat->row[row][row]);
741                         subtract(mat, &inv, NULL, row, row, first, a);
742                         if (!isl_int_is_zero(mat->row[row][first]))
743                                 exchange(mat, &inv, NULL, row, row, first);
744                         else
745                                 ++first;
746                 }
747                 for (i = 0; i < row; ++i) {
748                         if (isl_int_is_zero(mat->row[row][i]))
749                                 continue;
750                         isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
751                         isl_int_divexact(b, mat->row[row][i], a);
752                         isl_int_divexact(a, mat->row[row][row], a);
753                         isl_int_neg(a, a);
754                         isl_mat_col_combine(mat, i, a, i, b, row);
755                         isl_mat_col_combine(inv, i, a, i, b, row);
756                 }
757         }
758         isl_int_clear(b);
759
760         isl_int_set(a, mat->row[0][0]);
761         for (row = 1; row < mat->n_row; ++row)
762                 isl_int_lcm(a, a, mat->row[row][row]);
763         if (isl_int_is_zero(a)){
764                 isl_int_clear(a);
765                 goto error;
766         }
767         for (row = 0; row < mat->n_row; ++row) {
768                 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
769                 if (isl_int_is_one(mat->row[row][row]))
770                         continue;
771                 isl_mat_col_scale(inv, row, mat->row[row][row]);
772         }
773         isl_int_clear(a);
774
775         isl_mat_free(mat);
776
777         return inv;
778 error:
779         isl_mat_free(mat);
780         return NULL;
781 }
782
783 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
784 {
785         struct isl_mat *transpose = NULL;
786         int i, j;
787
788         if (mat->n_col == mat->n_row) {
789                 mat = isl_mat_cow(mat);
790                 if (!mat)
791                         return NULL;
792                 for (i = 0; i < mat->n_row; ++i)
793                         for (j = i + 1; j < mat->n_col; ++j)
794                                 isl_int_swap(mat->row[i][j], mat->row[j][i]);
795                 return mat;
796         }
797         transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
798         if (!transpose)
799                 goto error;
800         for (i = 0; i < mat->n_row; ++i)
801                 for (j = 0; j < mat->n_col; ++j)
802                         isl_int_set(transpose->row[j][i], mat->row[i][j]);
803         isl_mat_free(mat);
804         return transpose;
805 error:
806         isl_mat_free(mat);
807         return NULL;
808 }
809
810 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
811 {
812         int r;
813
814         mat = isl_mat_cow(mat);
815         if (!mat)
816                 return NULL;
817         isl_assert(mat->ctx, i < mat->n_col, goto error);
818         isl_assert(mat->ctx, j < mat->n_col, goto error);
819
820         for (r = 0; r < mat->n_row; ++r)
821                 isl_int_swap(mat->row[r][i], mat->row[r][j]);
822         return mat;
823 error:
824         isl_mat_free(mat);
825         return NULL;
826 }
827
828 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
829 {
830         isl_int *t;
831
832         if (!mat)
833                 return NULL;
834         mat = isl_mat_cow(mat);
835         if (!mat)
836                 return NULL;
837         t = mat->row[i];
838         mat->row[i] = mat->row[j];
839         mat->row[j] = t;
840         return mat;
841 }
842
843 struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
844 {
845         int i, j, k;
846         struct isl_mat *prod;
847
848         if (!left || !right)
849                 goto error;
850         isl_assert(left->ctx, left->n_col == right->n_row, goto error);
851         prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
852         if (!prod)
853                 goto error;
854         if (left->n_col == 0) {
855                 for (i = 0; i < prod->n_row; ++i)
856                         isl_seq_clr(prod->row[i], prod->n_col);
857                 return prod;
858         }
859         for (i = 0; i < prod->n_row; ++i) {
860                 for (j = 0; j < prod->n_col; ++j) {
861                         isl_int_mul(prod->row[i][j],
862                                     left->row[i][0], right->row[0][j]);
863                         for (k = 1; k < left->n_col; ++k)
864                                 isl_int_addmul(prod->row[i][j],
865                                             left->row[i][k], right->row[k][j]);
866                 }
867         }
868         isl_mat_free(left);
869         isl_mat_free(right);
870         return prod;
871 error:
872         isl_mat_free(left);
873         isl_mat_free(right);
874         return NULL;
875 }
876
877 /* Replace the variables x in the rows q by x' given by x = M x',
878  * with M the matrix mat.
879  *
880  * If the number of new variables is greater than the original
881  * number of variables, then the rows q have already been
882  * preextended.  If the new number is smaller, then the coefficients
883  * of the divs, which are not changed, need to be shifted down.
884  * The row q may be the equalities, the inequalities or the
885  * div expressions.  In the latter case, has_div is true and
886  * we need to take into account the extra denominator column.
887  */
888 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
889         unsigned n_div, int has_div, struct isl_mat *mat)
890 {
891         int i;
892         struct isl_mat *t;
893         int e;
894
895         if (mat->n_col >= mat->n_row)
896                 e = 0;
897         else
898                 e = mat->n_row - mat->n_col;
899         if (has_div)
900                 for (i = 0; i < n; ++i)
901                         isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
902         t = isl_mat_sub_alloc(mat->ctx, q, 0, n, has_div, mat->n_row);
903         t = isl_mat_product(t, mat);
904         if (!t)
905                 return -1;
906         for (i = 0; i < n; ++i) {
907                 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
908                 isl_seq_cpy(q[i] + has_div + t->n_col,
909                             q[i] + has_div + t->n_col + e, n_div);
910                 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
911         }
912         isl_mat_free(t);
913         return 0;
914 }
915
916 /* Replace the variables x in bset by x' given by x = M x', with
917  * M the matrix mat.
918  *
919  * If there are fewer variables x' then there are x, then we perform
920  * the transformation in place, which that, in principle,
921  * this frees up some extra variables as the number
922  * of columns remains constant, but we would have to extend
923  * the div array too as the number of rows in this array is assumed
924  * to be equal to extra.
925  */
926 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
927         struct isl_mat *mat)
928 {
929         struct isl_ctx *ctx;
930
931         if (!bset || !mat)
932                 goto error;
933
934         ctx = bset->ctx;
935         bset = isl_basic_set_cow(bset);
936         if (!bset)
937                 goto error;
938
939         isl_assert(ctx, bset->dim->nparam == 0, goto error);
940         isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
941         isl_assert(ctx, mat->n_col > 0, goto error);
942
943         if (mat->n_col > mat->n_row)
944                 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
945                                                 0, 0);
946         else if (mat->n_col < mat->n_row) {
947                 bset->dim = isl_dim_cow(bset->dim);
948                 if (!bset->dim)
949                         goto error;
950                 bset->dim->n_out -= mat->n_row - mat->n_col;
951         }
952
953         if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
954                         isl_mat_copy(mat)) < 0)
955                 goto error;
956
957         if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
958                         isl_mat_copy(mat)) < 0)
959                 goto error;
960
961         if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
962                 goto error2;
963
964         ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
965         ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
966         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
967         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
968         ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
969
970         bset = isl_basic_set_simplify(bset);
971         bset = isl_basic_set_finalize(bset);
972
973         return bset;
974 error:
975         isl_mat_free(mat);
976 error2:
977         isl_basic_set_free(bset);
978         return NULL;
979 }
980
981 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
982 {
983         struct isl_ctx *ctx;
984         int i;
985
986         set = isl_set_cow(set);
987         if (!set)
988                 return NULL;
989
990         ctx = set->ctx;
991         for (i = 0; i < set->n; ++i) {
992                 set->p[i] = isl_basic_set_preimage(set->p[i],
993                                                     isl_mat_copy(mat));
994                 if (!set->p[i])
995                         goto error;
996         }
997         if (mat->n_col != mat->n_row) {
998                 set->dim = isl_dim_cow(set->dim);
999                 if (!set->dim)
1000                         goto error;
1001                 set->dim->n_out += mat->n_col;
1002                 set->dim->n_out -= mat->n_row;
1003         }
1004         isl_mat_free(mat);
1005         ISL_F_CLR(set, ISL_SET_NORMALIZED);
1006         return set;
1007 error:
1008         isl_set_free(set);
1009         isl_mat_free(mat);
1010         return NULL;
1011 }
1012
1013 void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
1014 {
1015         int i, j;
1016
1017         if (!mat) {
1018                 fprintf(out, "%*snull mat\n", indent, "");
1019                 return;
1020         }
1021
1022         if (mat->n_row == 0)
1023                 fprintf(out, "%*s[]\n", indent, "");
1024
1025         for (i = 0; i < mat->n_row; ++i) {
1026                 if (!i)
1027                         fprintf(out, "%*s[[", indent, "");
1028                 else
1029                         fprintf(out, "%*s[", indent+1, "");
1030                 for (j = 0; j < mat->n_col; ++j) {
1031                         if (j)
1032                             fprintf(out, ",");
1033                         isl_int_print(out, mat->row[i][j], 0);
1034                 }
1035                 if (i == mat->n_row-1)
1036                         fprintf(out, "]]\n");
1037                 else
1038                         fprintf(out, "]\n");
1039         }
1040 }
1041
1042 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1043 {
1044         int r;
1045
1046         mat = isl_mat_cow(mat);
1047         if (!mat)
1048                 return NULL;
1049
1050         if (col != mat->n_col-n) {
1051                 for (r = 0; r < mat->n_row; ++r)
1052                         isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1053                                         mat->n_col - col - n);
1054         }
1055         mat->n_col -= n;
1056         return mat;
1057 }
1058
1059 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1060 {
1061         int r;
1062
1063         mat = isl_mat_cow(mat);
1064         if (!mat)
1065                 return NULL;
1066
1067         for (r = row; r+n < mat->n_row; ++r)
1068                 mat->row[r] = mat->row[r+n];
1069
1070         mat->n_row -= n;
1071         return mat;
1072 }
1073
1074 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1075                                 unsigned col, unsigned n)
1076 {
1077         isl_mat *ext;
1078
1079         if (!mat)
1080                 return NULL;
1081         if (n == 0)
1082                 return mat;
1083
1084         ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1085         if (!ext)
1086                 goto error;
1087
1088         isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1089         isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1090                                 col + n, col, mat->n_col - col);
1091
1092         isl_mat_free(mat);
1093         return ext;
1094 error:
1095         isl_mat_free(mat);
1096         return NULL;
1097 }
1098
1099 void isl_mat_col_submul(struct isl_mat *mat,
1100                         int dst_col, isl_int f, int src_col)
1101 {
1102         int i;
1103
1104         for (i = 0; i < mat->n_row; ++i)
1105                 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1106 }
1107
1108 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1109 {
1110         int i;
1111
1112         for (i = 0; i < mat->n_row; ++i)
1113                 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1114 }
1115
1116 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1117 {
1118         int r;
1119         struct isl_mat *H = NULL, *Q = NULL;
1120
1121         if (!M)
1122                 return NULL;
1123
1124         isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1125         M->n_row = row;
1126         H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1127         M->n_row = M->n_col;
1128         if (!H)
1129                 goto error;
1130         for (r = 0; r < row; ++r)
1131                 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1132         for (r = row; r < M->n_row; ++r)
1133                 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1134         isl_mat_free(H);
1135         isl_mat_free(Q);
1136         return M;
1137 error:
1138         isl_mat_free(H);
1139         isl_mat_free(Q);
1140         isl_mat_free(M);
1141         return NULL;
1142 }
1143
1144 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1145         __isl_take isl_mat *bot)
1146 {
1147         struct isl_mat *mat;
1148
1149         if (!top || !bot)
1150                 goto error;
1151
1152         isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1153         if (top->n_row == 0) {
1154                 isl_mat_free(top);
1155                 return bot;
1156         }
1157         if (bot->n_row == 0) {
1158                 isl_mat_free(bot);
1159                 return top;
1160         }
1161
1162         mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1163         if (!mat)
1164                 goto error;
1165         isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1166                          0, 0, mat->n_col);
1167         isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1168                          0, 0, mat->n_col);
1169         isl_mat_free(top);
1170         isl_mat_free(bot);
1171         return mat;
1172 error:
1173         isl_mat_free(top);
1174         isl_mat_free(bot);
1175         return NULL;
1176 }
1177
1178 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1179 {
1180         int i;
1181
1182         if (!mat1 || !mat2)
1183                 return -1;
1184
1185         if (mat1->n_row != mat2->n_row)
1186                 return 0;
1187
1188         if (mat1->n_col != mat2->n_col)
1189                 return 0;
1190
1191         for (i = 0; i < mat1->n_row; ++i)
1192                 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1193                         return 0;
1194
1195         return 1;
1196 }
1197
1198 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1199 {
1200         struct isl_mat *mat;
1201
1202         if (!vec)
1203                 return NULL;
1204         mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1205         if (!mat)
1206                 goto error;
1207
1208         isl_seq_cpy(mat->row[0], vec->el, vec->size);
1209
1210         isl_vec_free(vec);
1211         return mat;
1212 error:
1213         isl_vec_free(vec);
1214         return NULL;
1215 }
1216
1217 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1218         __isl_take isl_vec *bot)
1219 {
1220         return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1221 }
1222
1223 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1224         unsigned dst_col, unsigned src_col, unsigned n)
1225 {
1226         isl_mat *res;
1227
1228         if (!mat)
1229                 return NULL;
1230         if (n == 0 || dst_col == src_col)
1231                 return mat;
1232
1233         res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1234         if (!res)
1235                 goto error;
1236
1237         if (dst_col < src_col) {
1238                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1239                                  0, 0, dst_col);
1240                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1241                                  dst_col, src_col, n);
1242                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1243                                  dst_col + n, dst_col, src_col - dst_col);
1244                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1245                                  src_col + n, src_col + n,
1246                                  res->n_col - src_col - n);
1247         } else {
1248                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1249                                  0, 0, src_col);
1250                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1251                                  src_col, src_col + n, dst_col - src_col);
1252                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1253                                  dst_col, src_col, n);
1254                 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1255                                  dst_col + n, dst_col + n,
1256                                  res->n_col - dst_col - n);
1257         }
1258         isl_mat_free(mat);
1259
1260         return res;
1261 error:
1262         isl_mat_free(mat);
1263         return NULL;
1264 }