add missing annotations to isl_basic_set_gauss
[isl.git] / isl_mat.c
blob3d0eb297ab9988dd09200256922b96f9f515713e
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 struct isl_mat *isl_mat_dup(struct 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 struct isl_mat *isl_mat_cow(struct 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 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1288 struct 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 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1345 int i;
1347 set = isl_set_cow(set);
1348 if (!set)
1349 goto error;
1351 for (i = 0; i < set->n; ++i) {
1352 set->p[i] = isl_basic_set_preimage(set->p[i],
1353 isl_mat_copy(mat));
1354 if (!set->p[i])
1355 goto error;
1357 if (mat->n_col != mat->n_row) {
1358 set->dim = isl_space_cow(set->dim);
1359 if (!set->dim)
1360 goto error;
1361 set->dim->n_out += mat->n_col;
1362 set->dim->n_out -= mat->n_row;
1364 isl_mat_free(mat);
1365 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1366 return set;
1367 error:
1368 isl_set_free(set);
1369 isl_mat_free(mat);
1370 return NULL;
1373 /* Replace the variables x starting at "first_col" in the rows "rows"
1374 * of some coefficient matrix by x' with x = M x' with M the matrix mat.
1375 * That is, replace the corresponding coefficients c by c M.
1377 isl_stat isl_mat_sub_transform(isl_int **row, unsigned n_row,
1378 unsigned first_col, __isl_take isl_mat *mat)
1380 int i;
1381 isl_ctx *ctx;
1382 isl_mat *t;
1384 if (!mat)
1385 return isl_stat_error;
1386 ctx = isl_mat_get_ctx(mat);
1387 t = isl_mat_sub_alloc6(ctx, row, 0, n_row, first_col, mat->n_row);
1388 t = isl_mat_product(t, mat);
1389 if (!t)
1390 return isl_stat_error;
1391 for (i = 0; i < n_row; ++i)
1392 isl_seq_swp_or_cpy(row[i] + first_col, t->row[i], t->n_col);
1393 isl_mat_free(t);
1394 return isl_stat_ok;
1397 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1399 int i, j;
1401 if (!mat) {
1402 fprintf(out, "%*snull mat\n", indent, "");
1403 return;
1406 if (mat->n_row == 0)
1407 fprintf(out, "%*s[]\n", indent, "");
1409 for (i = 0; i < mat->n_row; ++i) {
1410 if (!i)
1411 fprintf(out, "%*s[[", indent, "");
1412 else
1413 fprintf(out, "%*s[", indent+1, "");
1414 for (j = 0; j < mat->n_col; ++j) {
1415 if (j)
1416 fprintf(out, ",");
1417 isl_int_print(out, mat->row[i][j], 0);
1419 if (i == mat->n_row-1)
1420 fprintf(out, "]]\n");
1421 else
1422 fprintf(out, "]\n");
1426 void isl_mat_dump(__isl_keep isl_mat *mat)
1428 isl_mat_print_internal(mat, stderr, 0);
1431 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1433 int r;
1435 if (n == 0)
1436 return mat;
1438 mat = isl_mat_cow(mat);
1439 if (!mat)
1440 return NULL;
1442 if (col != mat->n_col-n) {
1443 for (r = 0; r < mat->n_row; ++r)
1444 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1445 mat->n_col - col - n);
1447 mat->n_col -= n;
1448 return mat;
1451 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1453 int r;
1455 mat = isl_mat_cow(mat);
1456 if (!mat)
1457 return NULL;
1459 for (r = row; r+n < mat->n_row; ++r)
1460 mat->row[r] = mat->row[r+n];
1462 mat->n_row -= n;
1463 return mat;
1466 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1467 unsigned col, unsigned n)
1469 isl_mat *ext;
1471 if (!mat)
1472 return NULL;
1473 if (n == 0)
1474 return mat;
1476 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1477 if (!ext)
1478 goto error;
1480 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1481 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1482 col + n, col, mat->n_col - col);
1484 isl_mat_free(mat);
1485 return ext;
1486 error:
1487 isl_mat_free(mat);
1488 return NULL;
1491 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1492 unsigned first, unsigned n)
1494 int i;
1496 if (!mat)
1497 return NULL;
1498 mat = isl_mat_insert_cols(mat, first, n);
1499 if (!mat)
1500 return NULL;
1502 for (i = 0; i < mat->n_row; ++i)
1503 isl_seq_clr(mat->row[i] + first, n);
1505 return mat;
1508 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1510 if (!mat)
1511 return NULL;
1513 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1516 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1517 unsigned row, unsigned n)
1519 isl_mat *ext;
1521 if (!mat)
1522 return NULL;
1523 if (n == 0)
1524 return mat;
1526 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1527 if (!ext)
1528 goto error;
1530 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1531 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1532 mat->n_row - row, 0, 0, mat->n_col);
1534 isl_mat_free(mat);
1535 return ext;
1536 error:
1537 isl_mat_free(mat);
1538 return NULL;
1541 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1543 if (!mat)
1544 return NULL;
1546 return isl_mat_insert_rows(mat, mat->n_row, n);
1549 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1550 unsigned row, unsigned n)
1552 int i;
1554 mat = isl_mat_insert_rows(mat, row, n);
1555 if (!mat)
1556 return NULL;
1558 for (i = 0; i < n; ++i)
1559 isl_seq_clr(mat->row[row + i], mat->n_col);
1561 return mat;
1564 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1566 if (!mat)
1567 return NULL;
1569 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1572 void isl_mat_col_submul(struct isl_mat *mat,
1573 int dst_col, isl_int f, int src_col)
1575 int i;
1577 for (i = 0; i < mat->n_row; ++i)
1578 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1581 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1583 int i;
1585 if (!mat)
1586 return;
1588 for (i = 0; i < mat->n_row; ++i)
1589 isl_int_add(mat->row[i][dst_col],
1590 mat->row[i][dst_col], mat->row[i][src_col]);
1593 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1595 int i;
1597 for (i = 0; i < mat->n_row; ++i)
1598 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1601 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1602 * return the result.
1604 __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1605 isl_int f, int src_col)
1607 int i;
1609 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1610 return isl_mat_free(mat);
1612 for (i = 0; i < mat->n_row; ++i) {
1613 if (isl_int_is_zero(mat->row[i][src_col]))
1614 continue;
1615 mat = isl_mat_cow(mat);
1616 if (!mat)
1617 return NULL;
1618 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1621 return mat;
1624 /* Negate column "col" of "mat" and return the result.
1626 __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1628 int i;
1630 if (check_col(mat, col) < 0)
1631 return isl_mat_free(mat);
1633 for (i = 0; i < mat->n_row; ++i) {
1634 if (isl_int_is_zero(mat->row[i][col]))
1635 continue;
1636 mat = isl_mat_cow(mat);
1637 if (!mat)
1638 return NULL;
1639 isl_int_neg(mat->row[i][col], mat->row[i][col]);
1642 return mat;
1645 /* Negate row "row" of "mat" and return the result.
1647 __isl_give isl_mat *isl_mat_row_neg(__isl_take isl_mat *mat, int row)
1649 if (check_row(mat, row) < 0)
1650 return isl_mat_free(mat);
1651 if (isl_seq_first_non_zero(mat->row[row], mat->n_col) == -1)
1652 return mat;
1653 mat = isl_mat_cow(mat);
1654 if (!mat)
1655 return NULL;
1656 isl_seq_neg(mat->row[row], mat->row[row], mat->n_col);
1657 return mat;
1660 __isl_give isl_mat *isl_mat_unimodular_complete(__isl_take isl_mat *M, int row)
1662 int r;
1663 struct isl_mat *H = NULL, *Q = NULL;
1665 if (!M)
1666 return NULL;
1668 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1669 M->n_row = row;
1670 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1671 M->n_row = M->n_col;
1672 if (!H)
1673 goto error;
1674 for (r = 0; r < row; ++r)
1675 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1676 for (r = row; r < M->n_row; ++r)
1677 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1678 isl_mat_free(H);
1679 isl_mat_free(Q);
1680 return M;
1681 error:
1682 isl_mat_free(H);
1683 isl_mat_free(Q);
1684 isl_mat_free(M);
1685 return NULL;
1688 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1689 __isl_take isl_mat *bot)
1691 struct isl_mat *mat;
1693 if (!top || !bot)
1694 goto error;
1696 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1697 if (top->n_row == 0) {
1698 isl_mat_free(top);
1699 return bot;
1701 if (bot->n_row == 0) {
1702 isl_mat_free(bot);
1703 return top;
1706 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1707 if (!mat)
1708 goto error;
1709 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1710 0, 0, mat->n_col);
1711 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1712 0, 0, mat->n_col);
1713 isl_mat_free(top);
1714 isl_mat_free(bot);
1715 return mat;
1716 error:
1717 isl_mat_free(top);
1718 isl_mat_free(bot);
1719 return NULL;
1722 isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1724 int i;
1726 if (!mat1 || !mat2)
1727 return isl_bool_error;
1729 if (mat1->n_row != mat2->n_row)
1730 return isl_bool_false;
1732 if (mat1->n_col != mat2->n_col)
1733 return isl_bool_false;
1735 for (i = 0; i < mat1->n_row; ++i)
1736 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1737 return isl_bool_false;
1739 return isl_bool_true;
1742 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1744 struct isl_mat *mat;
1746 if (!vec)
1747 return NULL;
1748 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1749 if (!mat)
1750 goto error;
1752 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1754 isl_vec_free(vec);
1755 return mat;
1756 error:
1757 isl_vec_free(vec);
1758 return NULL;
1761 /* Return a copy of row "row" of "mat" as an isl_vec.
1763 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1765 isl_vec *v;
1767 if (!mat)
1768 return NULL;
1769 if (row >= mat->n_row)
1770 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1771 return NULL);
1773 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1774 if (!v)
1775 return NULL;
1776 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1778 return v;
1781 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1782 __isl_take isl_vec *bot)
1784 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1787 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1788 unsigned dst_col, unsigned src_col, unsigned n)
1790 isl_mat *res;
1792 if (!mat)
1793 return NULL;
1794 if (n == 0 || dst_col == src_col)
1795 return mat;
1797 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1798 if (!res)
1799 goto error;
1801 if (dst_col < src_col) {
1802 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1803 0, 0, dst_col);
1804 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1805 dst_col, src_col, n);
1806 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1807 dst_col + n, dst_col, src_col - dst_col);
1808 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1809 src_col + n, src_col + n,
1810 res->n_col - src_col - n);
1811 } else {
1812 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1813 0, 0, src_col);
1814 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1815 src_col, src_col + n, dst_col - src_col);
1816 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1817 dst_col, src_col, n);
1818 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1819 dst_col + n, dst_col + n,
1820 res->n_col - dst_col - n);
1822 isl_mat_free(mat);
1824 return res;
1825 error:
1826 isl_mat_free(mat);
1827 return NULL;
1830 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1831 * Return isl_stat_ok on success and isl_stat_error on failure.
1833 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1835 if (check_row(mat, row) < 0)
1836 return isl_stat_error;
1838 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1840 return isl_stat_ok;
1843 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1845 int i;
1846 isl_int g;
1848 isl_int_set_si(*gcd, 0);
1849 if (!mat)
1850 return;
1852 isl_int_init(g);
1853 for (i = 0; i < mat->n_row; ++i) {
1854 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1855 isl_int_gcd(*gcd, *gcd, g);
1857 isl_int_clear(g);
1860 /* Return the result of scaling "mat" by a factor of "m".
1862 __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1864 int i;
1866 if (isl_int_is_one(m))
1867 return mat;
1869 mat = isl_mat_cow(mat);
1870 if (!mat)
1871 return NULL;
1873 for (i = 0; i < mat->n_row; ++i)
1874 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1876 return mat;
1879 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1881 int i;
1883 if (isl_int_is_one(m))
1884 return mat;
1886 mat = isl_mat_cow(mat);
1887 if (!mat)
1888 return NULL;
1890 for (i = 0; i < mat->n_row; ++i)
1891 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1893 return mat;
1896 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1897 isl_int m)
1899 if (isl_int_is_one(m))
1900 return mat;
1902 mat = isl_mat_cow(mat);
1903 if (!mat)
1904 return NULL;
1906 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1908 return mat;
1911 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1913 isl_int gcd;
1915 if (!mat)
1916 return NULL;
1918 isl_int_init(gcd);
1919 isl_mat_gcd(mat, &gcd);
1920 mat = isl_mat_scale_down(mat, gcd);
1921 isl_int_clear(gcd);
1923 return mat;
1926 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1928 mat = isl_mat_cow(mat);
1929 if (!mat)
1930 return NULL;
1932 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1934 return mat;
1937 /* Number of initial non-zero columns.
1939 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1941 int i;
1943 if (!mat)
1944 return -1;
1946 for (i = 0; i < mat->n_col; ++i)
1947 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1948 break;
1950 return i;