isl_schedule: return EXIT_FAILURE on failure
[isl.git] / isl_mat.c
blob0e9666c8e5dfe814e9676956739502c3beffbcd1
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl/space.h>
15 #include <isl_seq.h>
16 #include <isl_mat_private.h>
17 #include <isl_vec_private.h>
18 #include <isl_space_private.h>
19 #include <isl_val_private.h>
20 #include <isl/deprecated/mat_int.h>
22 isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
24 return mat ? mat->ctx : NULL;
27 /* Return a hash value that digests "mat".
29 uint32_t isl_mat_get_hash(__isl_keep isl_mat *mat)
31 int i;
32 uint32_t hash;
34 if (!mat)
35 return 0;
37 hash = isl_hash_init();
38 isl_hash_byte(hash, mat->n_row & 0xFF);
39 isl_hash_byte(hash, mat->n_col & 0xFF);
40 for (i = 0; i < mat->n_row; ++i) {
41 uint32_t row_hash;
43 row_hash = isl_seq_get_hash(mat->row[i], mat->n_col);
44 isl_hash_hash(hash, row_hash);
47 return hash;
50 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
51 unsigned n_row, unsigned n_col)
53 int i;
54 struct isl_mat *mat;
56 mat = isl_alloc_type(ctx, struct isl_mat);
57 if (!mat)
58 return NULL;
60 mat->row = NULL;
61 mat->block = isl_blk_alloc(ctx, n_row * n_col);
62 if (isl_blk_is_error(mat->block))
63 goto error;
64 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
65 if (n_row && !mat->row)
66 goto error;
68 for (i = 0; i < n_row; ++i)
69 mat->row[i] = mat->block.data + i * n_col;
71 mat->ctx = ctx;
72 isl_ctx_ref(ctx);
73 mat->ref = 1;
74 mat->n_row = n_row;
75 mat->n_col = n_col;
76 mat->max_col = n_col;
77 mat->flags = 0;
79 return mat;
80 error:
81 isl_blk_free(ctx, mat->block);
82 free(mat);
83 return NULL;
86 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
87 unsigned n_row, unsigned n_col)
89 int i;
90 isl_int *old;
91 isl_int **row;
93 if (!mat)
94 return NULL;
96 if (mat->max_col >= n_col && mat->n_row >= n_row) {
97 if (mat->n_col < n_col)
98 mat->n_col = n_col;
99 return mat;
102 if (mat->max_col < n_col) {
103 struct isl_mat *new_mat;
105 if (n_row < mat->n_row)
106 n_row = mat->n_row;
107 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
108 if (!new_mat)
109 goto error;
110 for (i = 0; i < mat->n_row; ++i)
111 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
112 isl_mat_free(mat);
113 return new_mat;
116 mat = isl_mat_cow(mat);
117 if (!mat)
118 goto error;
120 old = mat->block.data;
121 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
122 if (isl_blk_is_error(mat->block))
123 goto error;
124 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
125 if (n_row && !row)
126 goto error;
127 mat->row = row;
129 for (i = 0; i < mat->n_row; ++i)
130 mat->row[i] = mat->block.data + (mat->row[i] - old);
131 for (i = mat->n_row; i < n_row; ++i)
132 mat->row[i] = mat->block.data + i * mat->max_col;
133 mat->n_row = n_row;
134 if (mat->n_col < n_col)
135 mat->n_col = n_col;
137 return mat;
138 error:
139 isl_mat_free(mat);
140 return NULL;
143 __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
144 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
146 int i;
147 struct isl_mat *mat;
149 mat = isl_alloc_type(ctx, struct isl_mat);
150 if (!mat)
151 return NULL;
152 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
153 if (n_row && !mat->row)
154 goto error;
155 for (i = 0; i < n_row; ++i)
156 mat->row[i] = row[first_row+i] + first_col;
157 mat->ctx = ctx;
158 isl_ctx_ref(ctx);
159 mat->ref = 1;
160 mat->n_row = n_row;
161 mat->n_col = n_col;
162 mat->block = isl_blk_empty();
163 mat->flags = ISL_MAT_BORROWED;
164 return mat;
165 error:
166 free(mat);
167 return NULL;
170 __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
171 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
173 if (!mat)
174 return NULL;
175 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
176 first_col, n_col);
179 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
180 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
182 int i;
184 for (i = 0; i < n_row; ++i)
185 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
188 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
189 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
191 int i;
193 for (i = 0; i < n_row; ++i)
194 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
197 __isl_give isl_mat *isl_mat_copy(__isl_keep isl_mat *mat)
199 if (!mat)
200 return NULL;
202 mat->ref++;
203 return mat;
206 __isl_give isl_mat *isl_mat_dup(__isl_keep isl_mat *mat)
208 int i;
209 struct isl_mat *mat2;
211 if (!mat)
212 return NULL;
213 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
214 if (!mat2)
215 return NULL;
216 for (i = 0; i < mat->n_row; ++i)
217 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
218 return mat2;
221 __isl_give isl_mat *isl_mat_cow(__isl_take isl_mat *mat)
223 struct isl_mat *mat2;
224 if (!mat)
225 return NULL;
227 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
228 return mat;
230 mat2 = isl_mat_dup(mat);
231 isl_mat_free(mat);
232 return mat2;
235 __isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
237 if (!mat)
238 return NULL;
240 if (--mat->ref > 0)
241 return NULL;
243 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
244 isl_blk_free(mat->ctx, mat->block);
245 isl_ctx_deref(mat->ctx);
246 free(mat->row);
247 free(mat);
249 return NULL;
252 int isl_mat_rows(__isl_keep isl_mat *mat)
254 return mat ? mat->n_row : -1;
257 int isl_mat_cols(__isl_keep isl_mat *mat)
259 return mat ? mat->n_col : -1;
262 /* Check that "col" is a valid column position for "mat".
264 static isl_stat check_col(__isl_keep isl_mat *mat, int col)
266 if (!mat)
267 return isl_stat_error;
268 if (col < 0 || col >= mat->n_col)
269 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
270 "column out of range", return isl_stat_error);
271 return isl_stat_ok;
274 /* Check that "row" is a valid row position for "mat".
276 static isl_stat check_row(__isl_keep isl_mat *mat, int row)
278 if (!mat)
279 return isl_stat_error;
280 if (row < 0 || row >= mat->n_row)
281 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
282 "row out of range", return isl_stat_error);
283 return isl_stat_ok;
286 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
288 if (check_row(mat, row) < 0)
289 return -1;
290 if (check_col(mat, col) < 0)
291 return -1;
292 isl_int_set(*v, mat->row[row][col]);
293 return 0;
296 /* Extract the element at row "row", oolumn "col" of "mat".
298 __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
299 int row, int col)
301 isl_ctx *ctx;
303 if (check_row(mat, row) < 0)
304 return NULL;
305 if (check_col(mat, col) < 0)
306 return NULL;
307 ctx = isl_mat_get_ctx(mat);
308 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
311 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
312 int row, int col, isl_int v)
314 mat = isl_mat_cow(mat);
315 if (check_row(mat, row) < 0)
316 return isl_mat_free(mat);
317 if (check_col(mat, col) < 0)
318 return isl_mat_free(mat);
319 isl_int_set(mat->row[row][col], v);
320 return mat;
323 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
324 int row, int col, int v)
326 mat = isl_mat_cow(mat);
327 if (check_row(mat, row) < 0)
328 return isl_mat_free(mat);
329 if (check_col(mat, col) < 0)
330 return isl_mat_free(mat);
331 isl_int_set_si(mat->row[row][col], v);
332 return mat;
335 /* Replace the element at row "row", column "col" of "mat" by "v".
337 __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
338 int row, int col, __isl_take isl_val *v)
340 if (!v)
341 return isl_mat_free(mat);
342 if (!isl_val_is_int(v))
343 isl_die(isl_val_get_ctx(v), isl_error_invalid,
344 "expecting integer value", goto error);
345 mat = isl_mat_set_element(mat, row, col, v->n);
346 isl_val_free(v);
347 return mat;
348 error:
349 isl_val_free(v);
350 return isl_mat_free(mat);
353 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
355 int i;
356 struct isl_mat *mat;
358 mat = isl_mat_alloc(ctx, n_row, n_row);
359 if (!mat)
360 return NULL;
361 for (i = 0; i < n_row; ++i) {
362 isl_seq_clr(mat->row[i], i);
363 isl_int_set(mat->row[i][i], d);
364 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
367 return mat;
370 /* Create an "n_row" by "n_col" matrix with zero elements.
372 __isl_give isl_mat *isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
374 int i;
375 isl_mat *mat;
377 mat = isl_mat_alloc(ctx, n_row, n_col);
378 if (!mat)
379 return NULL;
380 for (i = 0; i < n_row; ++i)
381 isl_seq_clr(mat->row[i], n_col);
383 return mat;
386 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
388 if (!ctx)
389 return NULL;
390 return isl_mat_diag(ctx, n_row, ctx->one);
393 /* Is "mat" a (possibly scaled) identity matrix?
395 int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
397 int i;
399 if (!mat)
400 return -1;
401 if (mat->n_row != mat->n_col)
402 return 0;
404 for (i = 0; i < mat->n_row; ++i) {
405 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
406 return 0;
407 if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
408 return 0;
409 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
410 mat->n_col - (i + 1)) != -1)
411 return 0;
414 return 1;
417 __isl_give isl_vec *isl_mat_vec_product(__isl_take isl_mat *mat,
418 __isl_take isl_vec *vec)
420 int i;
421 struct isl_vec *prod;
423 if (!mat || !vec)
424 goto error;
426 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
428 prod = isl_vec_alloc(mat->ctx, mat->n_row);
429 if (!prod)
430 goto error;
432 for (i = 0; i < prod->size; ++i)
433 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
434 &prod->block.data[i]);
435 isl_mat_free(mat);
436 isl_vec_free(vec);
437 return prod;
438 error:
439 isl_mat_free(mat);
440 isl_vec_free(vec);
441 return NULL;
444 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
445 __isl_take isl_vec *vec)
447 struct isl_mat *vec_mat;
448 int i;
450 if (!mat || !vec)
451 goto error;
452 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
453 if (!vec_mat)
454 goto error;
455 for (i = 0; i < vec->size; ++i)
456 isl_int_set(vec_mat->row[i][0], vec->el[i]);
457 vec_mat = isl_mat_inverse_product(mat, vec_mat);
458 isl_vec_free(vec);
459 if (!vec_mat)
460 return NULL;
461 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
462 if (vec)
463 for (i = 0; i < vec->size; ++i)
464 isl_int_set(vec->el[i], vec_mat->row[i][0]);
465 isl_mat_free(vec_mat);
466 return vec;
467 error:
468 isl_mat_free(mat);
469 isl_vec_free(vec);
470 return NULL;
473 __isl_give isl_vec *isl_vec_mat_product(__isl_take isl_vec *vec,
474 __isl_take isl_mat *mat)
476 int i, j;
477 struct isl_vec *prod;
479 if (!mat || !vec)
480 goto error;
482 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
484 prod = isl_vec_alloc(mat->ctx, mat->n_col);
485 if (!prod)
486 goto error;
488 for (i = 0; i < prod->size; ++i) {
489 isl_int_set_si(prod->el[i], 0);
490 for (j = 0; j < vec->size; ++j)
491 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
493 isl_mat_free(mat);
494 isl_vec_free(vec);
495 return prod;
496 error:
497 isl_mat_free(mat);
498 isl_vec_free(vec);
499 return NULL;
502 __isl_give isl_mat *isl_mat_aff_direct_sum(__isl_take isl_mat *left,
503 __isl_take isl_mat *right)
505 int i;
506 struct isl_mat *sum;
508 if (!left || !right)
509 goto error;
511 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
512 isl_assert(left->ctx, left->n_row >= 1, goto error);
513 isl_assert(left->ctx, left->n_col >= 1, goto error);
514 isl_assert(left->ctx, right->n_col >= 1, goto error);
515 isl_assert(left->ctx,
516 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
517 goto error);
518 isl_assert(left->ctx,
519 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
520 goto error);
522 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
523 if (!sum)
524 goto error;
525 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
526 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
527 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
529 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
530 for (i = 1; i < sum->n_row; ++i) {
531 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
532 isl_int_addmul(sum->row[i][0],
533 right->row[0][0], right->row[i][0]);
534 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
535 left->n_col-1);
536 isl_seq_scale(sum->row[i]+left->n_col,
537 right->row[i]+1, right->row[0][0],
538 right->n_col-1);
541 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
542 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
543 isl_mat_free(left);
544 isl_mat_free(right);
545 return sum;
546 error:
547 isl_mat_free(left);
548 isl_mat_free(right);
549 return NULL;
552 static void exchange(struct isl_mat *M, struct isl_mat **U,
553 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
555 int r;
556 for (r = row; r < M->n_row; ++r)
557 isl_int_swap(M->row[r][i], M->row[r][j]);
558 if (U) {
559 for (r = 0; r < (*U)->n_row; ++r)
560 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
562 if (Q)
563 isl_mat_swap_rows(*Q, i, j);
566 static void subtract(struct isl_mat *M, struct isl_mat **U,
567 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
569 int r;
570 for (r = row; r < M->n_row; ++r)
571 isl_int_submul(M->row[r][j], m, M->row[r][i]);
572 if (U) {
573 for (r = 0; r < (*U)->n_row; ++r)
574 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
576 if (Q) {
577 for (r = 0; r < (*Q)->n_col; ++r)
578 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
582 static void oppose(struct isl_mat *M, struct isl_mat **U,
583 struct isl_mat **Q, unsigned row, unsigned col)
585 int r;
586 for (r = row; r < M->n_row; ++r)
587 isl_int_neg(M->row[r][col], M->row[r][col]);
588 if (U) {
589 for (r = 0; r < (*U)->n_row; ++r)
590 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
592 if (Q)
593 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
596 /* Given matrix M, compute
598 * M U = H
599 * M = H Q
601 * with U and Q unimodular matrices and H a matrix in column echelon form
602 * such that on each echelon row the entries in the non-echelon column
603 * are non-negative (if neg == 0) or non-positive (if neg == 1)
604 * and strictly smaller (in absolute value) than the entries in the echelon
605 * column.
606 * If U or Q are NULL, then these matrices are not computed.
608 __isl_give isl_mat *isl_mat_left_hermite(__isl_take isl_mat *M, int neg,
609 __isl_give isl_mat **U, __isl_give isl_mat **Q)
611 isl_int c;
612 int row, col;
614 if (U)
615 *U = NULL;
616 if (Q)
617 *Q = NULL;
618 if (!M)
619 goto error;
620 M = isl_mat_cow(M);
621 if (!M)
622 goto error;
623 if (U) {
624 *U = isl_mat_identity(M->ctx, M->n_col);
625 if (!*U)
626 goto error;
628 if (Q) {
629 *Q = isl_mat_identity(M->ctx, M->n_col);
630 if (!*Q)
631 goto error;
634 col = 0;
635 isl_int_init(c);
636 for (row = 0; row < M->n_row; ++row) {
637 int first, i, off;
638 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
639 if (first == -1)
640 continue;
641 first += col;
642 if (first != col)
643 exchange(M, U, Q, row, first, col);
644 if (isl_int_is_neg(M->row[row][col]))
645 oppose(M, U, Q, row, col);
646 first = col+1;
647 while ((off = isl_seq_first_non_zero(M->row[row]+first,
648 M->n_col-first)) != -1) {
649 first += off;
650 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
651 subtract(M, U, Q, row, col, first, c);
652 if (!isl_int_is_zero(M->row[row][first]))
653 exchange(M, U, Q, row, first, col);
654 else
655 ++first;
657 for (i = 0; i < col; ++i) {
658 if (isl_int_is_zero(M->row[row][i]))
659 continue;
660 if (neg)
661 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
662 else
663 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
664 if (isl_int_is_zero(c))
665 continue;
666 subtract(M, U, Q, row, col, i, c);
668 ++col;
670 isl_int_clear(c);
672 return M;
673 error:
674 if (Q) {
675 isl_mat_free(*Q);
676 *Q = NULL;
678 if (U) {
679 isl_mat_free(*U);
680 *U = NULL;
682 isl_mat_free(M);
683 return NULL;
686 /* Use row "row" of "mat" to eliminate column "col" from all other rows.
688 static __isl_give isl_mat *eliminate(__isl_take isl_mat *mat, int row, int col)
690 int k, nr, nc;
691 isl_ctx *ctx;
693 if (!mat)
694 return NULL;
696 ctx = isl_mat_get_ctx(mat);
697 nr = isl_mat_rows(mat);
698 nc = isl_mat_cols(mat);
700 for (k = 0; k < nr; ++k) {
701 if (k == row)
702 continue;
703 if (isl_int_is_zero(mat->row[k][col]))
704 continue;
705 mat = isl_mat_cow(mat);
706 if (!mat)
707 return NULL;
708 isl_seq_elim(mat->row[k], mat->row[row], col, nc, NULL);
709 isl_seq_normalize(ctx, mat->row[k], nc);
712 return mat;
715 /* Perform Gaussian elimination on the rows of "mat", but start
716 * from the final row and the final column.
717 * Any zero rows that result from the elimination are removed.
719 * In particular, for each column from last to first,
720 * look for the last row with a non-zero coefficient in that column,
721 * move it last (but before other rows moved last in previous steps) and
722 * use it to eliminate the column from the other rows.
724 __isl_give isl_mat *isl_mat_reverse_gauss(__isl_take isl_mat *mat)
726 int k, row, last, nr, nc;
728 if (!mat)
729 return NULL;
731 nr = isl_mat_rows(mat);
732 nc = isl_mat_cols(mat);
734 last = nc - 1;
735 for (row = nr - 1; row >= 0; --row) {
736 for (; last >= 0; --last) {
737 for (k = row; k >= 0; --k)
738 if (!isl_int_is_zero(mat->row[k][last]))
739 break;
740 if (k >= 0)
741 break;
743 if (last < 0)
744 break;
745 if (k != row)
746 mat = isl_mat_swap_rows(mat, k, row);
747 if (!mat)
748 return NULL;
749 if (isl_int_is_neg(mat->row[row][last]))
750 mat = isl_mat_row_neg(mat, row);
751 mat = eliminate(mat, row, last);
752 if (!mat)
753 return NULL;
755 mat = isl_mat_drop_rows(mat, 0, row + 1);
757 return mat;
760 /* Negate the lexicographically negative rows of "mat" such that
761 * all rows in the result are lexicographically non-negative.
763 __isl_give isl_mat *isl_mat_lexnonneg_rows(__isl_take isl_mat *mat)
765 int i, nr, nc;
767 if (!mat)
768 return NULL;
770 nr = isl_mat_rows(mat);
771 nc = isl_mat_cols(mat);
773 for (i = 0; i < nr; ++i) {
774 int pos;
776 pos = isl_seq_first_non_zero(mat->row[i], nc);
777 if (pos < 0)
778 continue;
779 if (isl_int_is_nonneg(mat->row[i][pos]))
780 continue;
781 mat = isl_mat_row_neg(mat, i);
782 if (!mat)
783 return NULL;
786 return mat;
789 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
791 int i, rank;
792 struct isl_mat *U = NULL;
793 struct isl_mat *K;
795 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
796 if (!mat || !U)
797 goto error;
799 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
800 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
801 ++i;
802 if (i >= mat->n_row)
803 break;
805 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
806 if (!K)
807 goto error;
808 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
809 isl_mat_free(mat);
810 isl_mat_free(U);
811 return K;
812 error:
813 isl_mat_free(mat);
814 isl_mat_free(U);
815 return NULL;
818 __isl_give isl_mat *isl_mat_lin_to_aff(__isl_take isl_mat *mat)
820 int i;
821 struct isl_mat *mat2;
823 if (!mat)
824 return NULL;
825 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
826 if (!mat2)
827 goto error;
828 isl_int_set_si(mat2->row[0][0], 1);
829 isl_seq_clr(mat2->row[0]+1, mat->n_col);
830 for (i = 0; i < mat->n_row; ++i) {
831 isl_int_set_si(mat2->row[1+i][0], 0);
832 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
834 isl_mat_free(mat);
835 return mat2;
836 error:
837 isl_mat_free(mat);
838 return NULL;
841 /* Given two matrices M1 and M2, return the block matrix
843 * [ M1 0 ]
844 * [ 0 M2 ]
846 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
847 __isl_take isl_mat *mat2)
849 int i;
850 isl_mat *mat;
852 if (!mat1 || !mat2)
853 goto error;
855 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
856 mat1->n_col + mat2->n_col);
857 if (!mat)
858 goto error;
859 for (i = 0; i < mat1->n_row; ++i) {
860 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
861 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
863 for (i = 0; i < mat2->n_row; ++i) {
864 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
865 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
866 mat2->row[i], mat2->n_col);
868 isl_mat_free(mat1);
869 isl_mat_free(mat2);
870 return mat;
871 error:
872 isl_mat_free(mat1);
873 isl_mat_free(mat2);
874 return NULL;
877 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
879 int i;
881 for (i = 0; i < n_row; ++i)
882 if (!isl_int_is_zero(row[i][col]))
883 return i;
884 return -1;
887 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
889 int i, min = row_first_non_zero(row, n_row, col);
890 if (min < 0)
891 return -1;
892 for (i = min + 1; i < n_row; ++i) {
893 if (isl_int_is_zero(row[i][col]))
894 continue;
895 if (isl_int_abs_lt(row[i][col], row[min][col]))
896 min = i;
898 return min;
901 static isl_stat inv_exchange(__isl_keep isl_mat **left,
902 __isl_keep isl_mat **right, unsigned i, unsigned j)
904 *left = isl_mat_swap_rows(*left, i, j);
905 *right = isl_mat_swap_rows(*right, i, j);
907 if (!*left || !*right)
908 return isl_stat_error;
909 return isl_stat_ok;
912 static void inv_oppose(
913 struct isl_mat *left, struct isl_mat *right, unsigned row)
915 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
916 isl_seq_neg(right->row[row], right->row[row], right->n_col);
919 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
920 unsigned row, unsigned i, isl_int m)
922 isl_int_neg(m, m);
923 isl_seq_combine(left->row[i]+row,
924 left->ctx->one, left->row[i]+row,
925 m, left->row[row]+row,
926 left->n_col-row);
927 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
928 m, right->row[row], right->n_col);
931 /* Compute inv(left)*right
933 __isl_give isl_mat *isl_mat_inverse_product(__isl_take isl_mat *left,
934 __isl_take isl_mat *right)
936 int row;
937 isl_int a, b;
939 if (!left || !right)
940 goto error;
942 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
943 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
945 if (left->n_row == 0) {
946 isl_mat_free(left);
947 return right;
950 left = isl_mat_cow(left);
951 right = isl_mat_cow(right);
952 if (!left || !right)
953 goto error;
955 isl_int_init(a);
956 isl_int_init(b);
957 for (row = 0; row < left->n_row; ++row) {
958 int pivot, first, i, off;
959 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
960 if (pivot < 0) {
961 isl_int_clear(a);
962 isl_int_clear(b);
963 isl_assert(left->ctx, pivot >= 0, goto error);
965 pivot += row;
966 if (pivot != row)
967 if (inv_exchange(&left, &right, pivot, row) < 0)
968 goto error;
969 if (isl_int_is_neg(left->row[row][row]))
970 inv_oppose(left, right, row);
971 first = row+1;
972 while ((off = row_first_non_zero(left->row+first,
973 left->n_row-first, row)) != -1) {
974 first += off;
975 isl_int_fdiv_q(a, left->row[first][row],
976 left->row[row][row]);
977 inv_subtract(left, right, row, first, a);
978 if (!isl_int_is_zero(left->row[first][row])) {
979 if (inv_exchange(&left, &right, row, first) < 0)
980 goto error;
981 } else {
982 ++first;
985 for (i = 0; i < row; ++i) {
986 if (isl_int_is_zero(left->row[i][row]))
987 continue;
988 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
989 isl_int_divexact(b, left->row[i][row], a);
990 isl_int_divexact(a, left->row[row][row], a);
991 isl_int_neg(b, b);
992 isl_seq_combine(left->row[i] + i,
993 a, left->row[i] + i,
994 b, left->row[row] + i,
995 left->n_col - i);
996 isl_seq_combine(right->row[i], a, right->row[i],
997 b, right->row[row], right->n_col);
1000 isl_int_clear(b);
1002 isl_int_set(a, left->row[0][0]);
1003 for (row = 1; row < left->n_row; ++row)
1004 isl_int_lcm(a, a, left->row[row][row]);
1005 if (isl_int_is_zero(a)){
1006 isl_int_clear(a);
1007 isl_assert(left->ctx, 0, goto error);
1009 for (row = 0; row < left->n_row; ++row) {
1010 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
1011 if (isl_int_is_one(left->row[row][row]))
1012 continue;
1013 isl_seq_scale(right->row[row], right->row[row],
1014 left->row[row][row], right->n_col);
1016 isl_int_clear(a);
1018 isl_mat_free(left);
1019 return right;
1020 error:
1021 isl_mat_free(left);
1022 isl_mat_free(right);
1023 return NULL;
1026 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
1028 int i;
1030 for (i = 0; i < mat->n_row; ++i)
1031 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
1034 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
1035 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
1037 int i;
1038 isl_int tmp;
1040 isl_int_init(tmp);
1041 for (i = 0; i < mat->n_row; ++i) {
1042 isl_int_mul(tmp, m1, mat->row[i][src1]);
1043 isl_int_addmul(tmp, m2, mat->row[i][src2]);
1044 isl_int_set(mat->row[i][dst], tmp);
1046 isl_int_clear(tmp);
1049 __isl_give isl_mat *isl_mat_right_inverse(__isl_take isl_mat *mat)
1051 struct isl_mat *inv;
1052 int row;
1053 isl_int a, b;
1055 mat = isl_mat_cow(mat);
1056 if (!mat)
1057 return NULL;
1059 inv = isl_mat_identity(mat->ctx, mat->n_col);
1060 inv = isl_mat_cow(inv);
1061 if (!inv)
1062 goto error;
1064 isl_int_init(a);
1065 isl_int_init(b);
1066 for (row = 0; row < mat->n_row; ++row) {
1067 int pivot, first, i, off;
1068 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
1069 if (pivot < 0) {
1070 isl_int_clear(a);
1071 isl_int_clear(b);
1072 isl_assert(mat->ctx, pivot >= 0, goto error);
1074 pivot += row;
1075 if (pivot != row)
1076 exchange(mat, &inv, NULL, row, pivot, row);
1077 if (isl_int_is_neg(mat->row[row][row]))
1078 oppose(mat, &inv, NULL, row, row);
1079 first = row+1;
1080 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
1081 mat->n_col-first)) != -1) {
1082 first += off;
1083 isl_int_fdiv_q(a, mat->row[row][first],
1084 mat->row[row][row]);
1085 subtract(mat, &inv, NULL, row, row, first, a);
1086 if (!isl_int_is_zero(mat->row[row][first]))
1087 exchange(mat, &inv, NULL, row, row, first);
1088 else
1089 ++first;
1091 for (i = 0; i < row; ++i) {
1092 if (isl_int_is_zero(mat->row[row][i]))
1093 continue;
1094 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
1095 isl_int_divexact(b, mat->row[row][i], a);
1096 isl_int_divexact(a, mat->row[row][row], a);
1097 isl_int_neg(a, a);
1098 isl_mat_col_combine(mat, i, a, i, b, row);
1099 isl_mat_col_combine(inv, i, a, i, b, row);
1102 isl_int_clear(b);
1104 isl_int_set(a, mat->row[0][0]);
1105 for (row = 1; row < mat->n_row; ++row)
1106 isl_int_lcm(a, a, mat->row[row][row]);
1107 if (isl_int_is_zero(a)){
1108 isl_int_clear(a);
1109 goto error;
1111 for (row = 0; row < mat->n_row; ++row) {
1112 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
1113 if (isl_int_is_one(mat->row[row][row]))
1114 continue;
1115 isl_mat_col_scale(inv, row, mat->row[row][row]);
1117 isl_int_clear(a);
1119 isl_mat_free(mat);
1121 return inv;
1122 error:
1123 isl_mat_free(mat);
1124 isl_mat_free(inv);
1125 return NULL;
1128 __isl_give isl_mat *isl_mat_transpose(__isl_take isl_mat *mat)
1130 struct isl_mat *transpose = NULL;
1131 int i, j;
1133 if (!mat)
1134 return NULL;
1136 if (mat->n_col == mat->n_row) {
1137 mat = isl_mat_cow(mat);
1138 if (!mat)
1139 return NULL;
1140 for (i = 0; i < mat->n_row; ++i)
1141 for (j = i + 1; j < mat->n_col; ++j)
1142 isl_int_swap(mat->row[i][j], mat->row[j][i]);
1143 return mat;
1145 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1146 if (!transpose)
1147 goto error;
1148 for (i = 0; i < mat->n_row; ++i)
1149 for (j = 0; j < mat->n_col; ++j)
1150 isl_int_set(transpose->row[j][i], mat->row[i][j]);
1151 isl_mat_free(mat);
1152 return transpose;
1153 error:
1154 isl_mat_free(mat);
1155 return NULL;
1158 __isl_give isl_mat *isl_mat_swap_cols(__isl_take isl_mat *mat,
1159 unsigned i, unsigned j)
1161 int r;
1163 mat = isl_mat_cow(mat);
1164 if (!mat)
1165 return NULL;
1166 isl_assert(mat->ctx, i < mat->n_col, goto error);
1167 isl_assert(mat->ctx, j < mat->n_col, goto error);
1169 for (r = 0; r < mat->n_row; ++r)
1170 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1171 return mat;
1172 error:
1173 isl_mat_free(mat);
1174 return NULL;
1177 __isl_give isl_mat *isl_mat_swap_rows(__isl_take isl_mat *mat,
1178 unsigned i, unsigned j)
1180 isl_int *t;
1182 if (!mat)
1183 return NULL;
1184 mat = isl_mat_cow(mat);
1185 if (!mat)
1186 return NULL;
1187 t = mat->row[i];
1188 mat->row[i] = mat->row[j];
1189 mat->row[j] = t;
1190 return mat;
1193 /* Calculate the product of two matrices.
1195 * This function is optimized for operand matrices that contain many zeros and
1196 * skips multiplications where we know one of the operands is zero.
1198 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1199 __isl_take isl_mat *right)
1201 int i, j, k;
1202 struct isl_mat *prod;
1204 if (!left || !right)
1205 goto error;
1206 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1207 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1208 if (!prod)
1209 goto error;
1210 if (left->n_col == 0) {
1211 for (i = 0; i < prod->n_row; ++i)
1212 isl_seq_clr(prod->row[i], prod->n_col);
1213 isl_mat_free(left);
1214 isl_mat_free(right);
1215 return prod;
1217 for (i = 0; i < prod->n_row; ++i) {
1218 for (j = 0; j < prod->n_col; ++j)
1219 isl_int_mul(prod->row[i][j],
1220 left->row[i][0], right->row[0][j]);
1221 for (k = 1; k < left->n_col; ++k) {
1222 if (isl_int_is_zero(left->row[i][k]))
1223 continue;
1224 for (j = 0; j < prod->n_col; ++j)
1225 isl_int_addmul(prod->row[i][j],
1226 left->row[i][k], right->row[k][j]);
1229 isl_mat_free(left);
1230 isl_mat_free(right);
1231 return prod;
1232 error:
1233 isl_mat_free(left);
1234 isl_mat_free(right);
1235 return NULL;
1238 /* Replace the variables x in the rows q by x' given by x = M x',
1239 * with M the matrix mat.
1241 * If the number of new variables is greater than the original
1242 * number of variables, then the rows q have already been
1243 * preextended. If the new number is smaller, then the coefficients
1244 * of the divs, which are not changed, need to be shifted down.
1245 * The row q may be the equalities, the inequalities or the
1246 * div expressions. In the latter case, has_div is true and
1247 * we need to take into account the extra denominator column.
1249 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1250 unsigned n_div, int has_div, struct isl_mat *mat)
1252 int i;
1253 struct isl_mat *t;
1254 int e;
1256 if (mat->n_col >= mat->n_row)
1257 e = 0;
1258 else
1259 e = mat->n_row - mat->n_col;
1260 if (has_div)
1261 for (i = 0; i < n; ++i)
1262 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1263 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1264 t = isl_mat_product(t, mat);
1265 if (!t)
1266 return -1;
1267 for (i = 0; i < n; ++i) {
1268 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1269 isl_seq_cpy(q[i] + has_div + t->n_col,
1270 q[i] + has_div + t->n_col + e, n_div);
1271 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1273 isl_mat_free(t);
1274 return 0;
1277 /* Replace the variables x in bset by x' given by x = M x', with
1278 * M the matrix mat.
1280 * If there are fewer variables x' then there are x, then we perform
1281 * the transformation in place, which means that, in principle,
1282 * this frees up some extra variables as the number
1283 * of columns remains constant, but we would have to extend
1284 * the div array too as the number of rows in this array is assumed
1285 * to be equal to extra.
1287 __isl_give isl_basic_set *isl_basic_set_preimage(
1288 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
1290 struct isl_ctx *ctx;
1292 if (!bset || !mat)
1293 goto error;
1295 ctx = bset->ctx;
1296 bset = isl_basic_set_cow(bset);
1297 if (!bset)
1298 goto error;
1300 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1301 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1302 isl_assert(ctx, mat->n_col > 0, goto error);
1304 if (mat->n_col > mat->n_row) {
1305 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1306 if (!bset)
1307 goto error;
1308 } else if (mat->n_col < mat->n_row) {
1309 bset->dim = isl_space_cow(bset->dim);
1310 if (!bset->dim)
1311 goto error;
1312 bset->dim->n_out -= mat->n_row - mat->n_col;
1315 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1316 isl_mat_copy(mat)) < 0)
1317 goto error;
1319 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1320 isl_mat_copy(mat)) < 0)
1321 goto error;
1323 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1324 goto error2;
1326 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1327 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1328 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1329 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1330 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1332 bset = isl_basic_set_simplify(bset);
1333 bset = isl_basic_set_finalize(bset);
1335 return bset;
1336 error:
1337 isl_mat_free(mat);
1338 error2:
1339 isl_basic_set_free(bset);
1340 return NULL;
1343 __isl_give isl_set *isl_set_preimage(
1344 __isl_take isl_set *set, __isl_take isl_mat *mat)
1346 int i;
1348 set = isl_set_cow(set);
1349 if (!set)
1350 goto error;
1352 for (i = 0; i < set->n; ++i) {
1353 set->p[i] = isl_basic_set_preimage(set->p[i],
1354 isl_mat_copy(mat));
1355 if (!set->p[i])
1356 goto error;
1358 if (mat->n_col != mat->n_row) {
1359 set->dim = isl_space_cow(set->dim);
1360 if (!set->dim)
1361 goto error;
1362 set->dim->n_out += mat->n_col;
1363 set->dim->n_out -= mat->n_row;
1365 isl_mat_free(mat);
1366 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1367 return set;
1368 error:
1369 isl_set_free(set);
1370 isl_mat_free(mat);
1371 return NULL;
1374 /* Replace the variables x starting at "first_col" in the rows "rows"
1375 * of some coefficient matrix by x' with x = M x' with M the matrix mat.
1376 * That is, replace the corresponding coefficients c by c M.
1378 isl_stat isl_mat_sub_transform(isl_int **row, unsigned n_row,
1379 unsigned first_col, __isl_take isl_mat *mat)
1381 int i;
1382 isl_ctx *ctx;
1383 isl_mat *t;
1385 if (!mat)
1386 return isl_stat_error;
1387 ctx = isl_mat_get_ctx(mat);
1388 t = isl_mat_sub_alloc6(ctx, row, 0, n_row, first_col, mat->n_row);
1389 t = isl_mat_product(t, mat);
1390 if (!t)
1391 return isl_stat_error;
1392 for (i = 0; i < n_row; ++i)
1393 isl_seq_swp_or_cpy(row[i] + first_col, t->row[i], t->n_col);
1394 isl_mat_free(t);
1395 return isl_stat_ok;
1398 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1400 int i, j;
1402 if (!mat) {
1403 fprintf(out, "%*snull mat\n", indent, "");
1404 return;
1407 if (mat->n_row == 0)
1408 fprintf(out, "%*s[]\n", indent, "");
1410 for (i = 0; i < mat->n_row; ++i) {
1411 if (!i)
1412 fprintf(out, "%*s[[", indent, "");
1413 else
1414 fprintf(out, "%*s[", indent+1, "");
1415 for (j = 0; j < mat->n_col; ++j) {
1416 if (j)
1417 fprintf(out, ",");
1418 isl_int_print(out, mat->row[i][j], 0);
1420 if (i == mat->n_row-1)
1421 fprintf(out, "]]\n");
1422 else
1423 fprintf(out, "]\n");
1427 void isl_mat_dump(__isl_keep isl_mat *mat)
1429 isl_mat_print_internal(mat, stderr, 0);
1432 __isl_give isl_mat *isl_mat_drop_cols(__isl_take isl_mat *mat,
1433 unsigned col, unsigned n)
1435 int r;
1437 if (n == 0)
1438 return mat;
1440 mat = isl_mat_cow(mat);
1441 if (!mat)
1442 return NULL;
1444 if (col != mat->n_col-n) {
1445 for (r = 0; r < mat->n_row; ++r)
1446 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1447 mat->n_col - col - n);
1449 mat->n_col -= n;
1450 return mat;
1453 __isl_give isl_mat *isl_mat_drop_rows(__isl_take isl_mat *mat,
1454 unsigned row, unsigned n)
1456 int r;
1458 mat = isl_mat_cow(mat);
1459 if (!mat)
1460 return NULL;
1462 for (r = row; r+n < mat->n_row; ++r)
1463 mat->row[r] = mat->row[r+n];
1465 mat->n_row -= n;
1466 return mat;
1469 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1470 unsigned col, unsigned n)
1472 isl_mat *ext;
1474 if (!mat)
1475 return NULL;
1476 if (n == 0)
1477 return mat;
1479 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1480 if (!ext)
1481 goto error;
1483 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1484 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1485 col + n, col, mat->n_col - col);
1487 isl_mat_free(mat);
1488 return ext;
1489 error:
1490 isl_mat_free(mat);
1491 return NULL;
1494 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1495 unsigned first, unsigned n)
1497 int i;
1499 if (!mat)
1500 return NULL;
1501 mat = isl_mat_insert_cols(mat, first, n);
1502 if (!mat)
1503 return NULL;
1505 for (i = 0; i < mat->n_row; ++i)
1506 isl_seq_clr(mat->row[i] + first, n);
1508 return mat;
1511 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1513 if (!mat)
1514 return NULL;
1516 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1519 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1520 unsigned row, unsigned n)
1522 isl_mat *ext;
1524 if (!mat)
1525 return NULL;
1526 if (n == 0)
1527 return mat;
1529 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1530 if (!ext)
1531 goto error;
1533 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1534 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1535 mat->n_row - row, 0, 0, mat->n_col);
1537 isl_mat_free(mat);
1538 return ext;
1539 error:
1540 isl_mat_free(mat);
1541 return NULL;
1544 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1546 if (!mat)
1547 return NULL;
1549 return isl_mat_insert_rows(mat, mat->n_row, n);
1552 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1553 unsigned row, unsigned n)
1555 int i;
1557 mat = isl_mat_insert_rows(mat, row, n);
1558 if (!mat)
1559 return NULL;
1561 for (i = 0; i < n; ++i)
1562 isl_seq_clr(mat->row[row + i], mat->n_col);
1564 return mat;
1567 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1569 if (!mat)
1570 return NULL;
1572 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1575 void isl_mat_col_submul(struct isl_mat *mat,
1576 int dst_col, isl_int f, int src_col)
1578 int i;
1580 for (i = 0; i < mat->n_row; ++i)
1581 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1584 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1586 int i;
1588 if (!mat)
1589 return;
1591 for (i = 0; i < mat->n_row; ++i)
1592 isl_int_add(mat->row[i][dst_col],
1593 mat->row[i][dst_col], mat->row[i][src_col]);
1596 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1598 int i;
1600 for (i = 0; i < mat->n_row; ++i)
1601 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1604 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1605 * return the result.
1607 __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1608 isl_int f, int src_col)
1610 int i;
1612 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1613 return isl_mat_free(mat);
1615 for (i = 0; i < mat->n_row; ++i) {
1616 if (isl_int_is_zero(mat->row[i][src_col]))
1617 continue;
1618 mat = isl_mat_cow(mat);
1619 if (!mat)
1620 return NULL;
1621 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1624 return mat;
1627 /* Negate column "col" of "mat" and return the result.
1629 __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1631 int i;
1633 if (check_col(mat, col) < 0)
1634 return isl_mat_free(mat);
1636 for (i = 0; i < mat->n_row; ++i) {
1637 if (isl_int_is_zero(mat->row[i][col]))
1638 continue;
1639 mat = isl_mat_cow(mat);
1640 if (!mat)
1641 return NULL;
1642 isl_int_neg(mat->row[i][col], mat->row[i][col]);
1645 return mat;
1648 /* Negate row "row" of "mat" and return the result.
1650 __isl_give isl_mat *isl_mat_row_neg(__isl_take isl_mat *mat, int row)
1652 if (check_row(mat, row) < 0)
1653 return isl_mat_free(mat);
1654 if (isl_seq_first_non_zero(mat->row[row], mat->n_col) == -1)
1655 return mat;
1656 mat = isl_mat_cow(mat);
1657 if (!mat)
1658 return NULL;
1659 isl_seq_neg(mat->row[row], mat->row[row], mat->n_col);
1660 return mat;
1663 __isl_give isl_mat *isl_mat_unimodular_complete(__isl_take isl_mat *M, int row)
1665 int r;
1666 struct isl_mat *H = NULL, *Q = NULL;
1668 if (!M)
1669 return NULL;
1671 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1672 M->n_row = row;
1673 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1674 M->n_row = M->n_col;
1675 if (!H)
1676 goto error;
1677 for (r = 0; r < row; ++r)
1678 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1679 for (r = row; r < M->n_row; ++r)
1680 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1681 isl_mat_free(H);
1682 isl_mat_free(Q);
1683 return M;
1684 error:
1685 isl_mat_free(H);
1686 isl_mat_free(Q);
1687 isl_mat_free(M);
1688 return NULL;
1691 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1692 __isl_take isl_mat *bot)
1694 struct isl_mat *mat;
1696 if (!top || !bot)
1697 goto error;
1699 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1700 if (top->n_row == 0) {
1701 isl_mat_free(top);
1702 return bot;
1704 if (bot->n_row == 0) {
1705 isl_mat_free(bot);
1706 return top;
1709 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1710 if (!mat)
1711 goto error;
1712 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1713 0, 0, mat->n_col);
1714 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1715 0, 0, mat->n_col);
1716 isl_mat_free(top);
1717 isl_mat_free(bot);
1718 return mat;
1719 error:
1720 isl_mat_free(top);
1721 isl_mat_free(bot);
1722 return NULL;
1725 isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1727 int i;
1729 if (!mat1 || !mat2)
1730 return isl_bool_error;
1732 if (mat1->n_row != mat2->n_row)
1733 return isl_bool_false;
1735 if (mat1->n_col != mat2->n_col)
1736 return isl_bool_false;
1738 for (i = 0; i < mat1->n_row; ++i)
1739 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1740 return isl_bool_false;
1742 return isl_bool_true;
1745 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1747 struct isl_mat *mat;
1749 if (!vec)
1750 return NULL;
1751 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1752 if (!mat)
1753 goto error;
1755 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1757 isl_vec_free(vec);
1758 return mat;
1759 error:
1760 isl_vec_free(vec);
1761 return NULL;
1764 /* Return a copy of row "row" of "mat" as an isl_vec.
1766 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1768 isl_vec *v;
1770 if (!mat)
1771 return NULL;
1772 if (row >= mat->n_row)
1773 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1774 return NULL);
1776 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1777 if (!v)
1778 return NULL;
1779 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1781 return v;
1784 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1785 __isl_take isl_vec *bot)
1787 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1790 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1791 unsigned dst_col, unsigned src_col, unsigned n)
1793 isl_mat *res;
1795 if (!mat)
1796 return NULL;
1797 if (n == 0 || dst_col == src_col)
1798 return mat;
1800 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1801 if (!res)
1802 goto error;
1804 if (dst_col < src_col) {
1805 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1806 0, 0, dst_col);
1807 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1808 dst_col, src_col, n);
1809 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1810 dst_col + n, dst_col, src_col - dst_col);
1811 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1812 src_col + n, src_col + n,
1813 res->n_col - src_col - n);
1814 } else {
1815 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1816 0, 0, src_col);
1817 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1818 src_col, src_col + n, dst_col - src_col);
1819 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1820 dst_col, src_col, n);
1821 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1822 dst_col + n, dst_col + n,
1823 res->n_col - dst_col - n);
1825 isl_mat_free(mat);
1827 return res;
1828 error:
1829 isl_mat_free(mat);
1830 return NULL;
1833 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1834 * Return isl_stat_ok on success and isl_stat_error on failure.
1836 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1838 if (check_row(mat, row) < 0)
1839 return isl_stat_error;
1841 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1843 return isl_stat_ok;
1846 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1848 int i;
1849 isl_int g;
1851 isl_int_set_si(*gcd, 0);
1852 if (!mat)
1853 return;
1855 isl_int_init(g);
1856 for (i = 0; i < mat->n_row; ++i) {
1857 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1858 isl_int_gcd(*gcd, *gcd, g);
1860 isl_int_clear(g);
1863 /* Return the result of scaling "mat" by a factor of "m".
1865 __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1867 int i;
1869 if (isl_int_is_one(m))
1870 return mat;
1872 mat = isl_mat_cow(mat);
1873 if (!mat)
1874 return NULL;
1876 for (i = 0; i < mat->n_row; ++i)
1877 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1879 return mat;
1882 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1884 int i;
1886 if (isl_int_is_one(m))
1887 return mat;
1889 mat = isl_mat_cow(mat);
1890 if (!mat)
1891 return NULL;
1893 for (i = 0; i < mat->n_row; ++i)
1894 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1896 return mat;
1899 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1900 isl_int m)
1902 if (isl_int_is_one(m))
1903 return mat;
1905 mat = isl_mat_cow(mat);
1906 if (!mat)
1907 return NULL;
1909 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1911 return mat;
1914 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1916 isl_int gcd;
1918 if (!mat)
1919 return NULL;
1921 isl_int_init(gcd);
1922 isl_mat_gcd(mat, &gcd);
1923 mat = isl_mat_scale_down(mat, gcd);
1924 isl_int_clear(gcd);
1926 return mat;
1929 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1931 mat = isl_mat_cow(mat);
1932 if (!mat)
1933 return NULL;
1935 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1937 return mat;
1940 /* Number of initial non-zero columns.
1942 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1944 int i;
1946 if (!mat)
1947 return -1;
1949 for (i = 0; i < mat->n_col; ++i)
1950 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1951 break;
1953 return i;