isl_stream_eat: print error message on EOF
[isl.git] / isl_mat.c
blob415ee3668cea08809531d97f60c59a625625bc8e
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 struct isl_mat *isl_mat_copy(struct 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 there are "n" columns starting at position "first" in "mat".
276 static isl_stat check_col_range(__isl_keep isl_mat *mat, unsigned first,
277 unsigned n)
279 if (!mat)
280 return isl_stat_error;
281 if (first + n > mat->n_col || first + n < first)
282 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
283 "column position or range out of bounds",
284 return isl_stat_error);
285 return isl_stat_ok;
288 /* Check that there are "n" rows starting at position "first" in "mat".
290 static isl_stat check_row_range(__isl_keep isl_mat *mat, unsigned first,
291 unsigned n)
293 if (!mat)
294 return isl_stat_error;
295 if (first + n > mat->n_row || first + n < first)
296 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
297 "row position or range out of bounds",
298 return isl_stat_error);
299 return isl_stat_ok;
302 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
304 if (!mat)
305 return -1;
306 if (row < 0 || row >= mat->n_row)
307 isl_die(mat->ctx, isl_error_invalid, "row out of range",
308 return -1);
309 if (check_col(mat, col) < 0)
310 return -1;
311 isl_int_set(*v, mat->row[row][col]);
312 return 0;
315 /* Extract the element at row "row", oolumn "col" of "mat".
317 __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
318 int row, int col)
320 isl_ctx *ctx;
322 if (!mat)
323 return NULL;
324 ctx = isl_mat_get_ctx(mat);
325 if (row < 0 || row >= mat->n_row)
326 isl_die(ctx, isl_error_invalid, "row out of range",
327 return NULL);
328 if (check_col(mat, col) < 0)
329 return NULL;
330 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
333 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
334 int row, int col, isl_int v)
336 mat = isl_mat_cow(mat);
337 if (!mat)
338 return NULL;
339 if (row < 0 || row >= mat->n_row)
340 isl_die(mat->ctx, isl_error_invalid, "row out of range",
341 goto error);
342 if (check_col(mat, col) < 0)
343 return isl_mat_free(mat);
344 isl_int_set(mat->row[row][col], v);
345 return mat;
346 error:
347 isl_mat_free(mat);
348 return NULL;
351 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
352 int row, int col, int v)
354 mat = isl_mat_cow(mat);
355 if (!mat)
356 return NULL;
357 if (row < 0 || row >= mat->n_row)
358 isl_die(mat->ctx, isl_error_invalid, "row out of range",
359 goto error);
360 if (check_col(mat, col) < 0)
361 return isl_mat_free(mat);
362 isl_int_set_si(mat->row[row][col], v);
363 return mat;
364 error:
365 isl_mat_free(mat);
366 return NULL;
369 /* Replace the element at row "row", column "col" of "mat" by "v".
371 __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
372 int row, int col, __isl_take isl_val *v)
374 if (!v)
375 return isl_mat_free(mat);
376 if (!isl_val_is_int(v))
377 isl_die(isl_val_get_ctx(v), isl_error_invalid,
378 "expecting integer value", goto error);
379 mat = isl_mat_set_element(mat, row, col, v->n);
380 isl_val_free(v);
381 return mat;
382 error:
383 isl_val_free(v);
384 return isl_mat_free(mat);
387 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
389 int i;
390 struct isl_mat *mat;
392 mat = isl_mat_alloc(ctx, n_row, n_row);
393 if (!mat)
394 return NULL;
395 for (i = 0; i < n_row; ++i) {
396 isl_seq_clr(mat->row[i], i);
397 isl_int_set(mat->row[i][i], d);
398 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
401 return mat;
404 /* Create an "n_row" by "n_col" matrix with zero elements.
406 __isl_give isl_mat *isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
408 int i;
409 isl_mat *mat;
411 mat = isl_mat_alloc(ctx, n_row, n_col);
412 if (!mat)
413 return NULL;
414 for (i = 0; i < n_row; ++i)
415 isl_seq_clr(mat->row[i], n_col);
417 return mat;
420 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
422 if (!ctx)
423 return NULL;
424 return isl_mat_diag(ctx, n_row, ctx->one);
427 /* Is "mat" a (possibly scaled) identity matrix?
429 int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
431 int i;
433 if (!mat)
434 return -1;
435 if (mat->n_row != mat->n_col)
436 return 0;
438 for (i = 0; i < mat->n_row; ++i) {
439 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
440 return 0;
441 if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
442 return 0;
443 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
444 mat->n_col - (i + 1)) != -1)
445 return 0;
448 return 1;
451 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
453 int i;
454 struct isl_vec *prod;
456 if (!mat || !vec)
457 goto error;
459 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
461 prod = isl_vec_alloc(mat->ctx, mat->n_row);
462 if (!prod)
463 goto error;
465 for (i = 0; i < prod->size; ++i)
466 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
467 &prod->block.data[i]);
468 isl_mat_free(mat);
469 isl_vec_free(vec);
470 return prod;
471 error:
472 isl_mat_free(mat);
473 isl_vec_free(vec);
474 return NULL;
477 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
478 __isl_take isl_vec *vec)
480 struct isl_mat *vec_mat;
481 int i;
483 if (!mat || !vec)
484 goto error;
485 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
486 if (!vec_mat)
487 goto error;
488 for (i = 0; i < vec->size; ++i)
489 isl_int_set(vec_mat->row[i][0], vec->el[i]);
490 vec_mat = isl_mat_inverse_product(mat, vec_mat);
491 isl_vec_free(vec);
492 if (!vec_mat)
493 return NULL;
494 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
495 if (vec)
496 for (i = 0; i < vec->size; ++i)
497 isl_int_set(vec->el[i], vec_mat->row[i][0]);
498 isl_mat_free(vec_mat);
499 return vec;
500 error:
501 isl_mat_free(mat);
502 isl_vec_free(vec);
503 return NULL;
506 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
508 int i, j;
509 struct isl_vec *prod;
511 if (!mat || !vec)
512 goto error;
514 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
516 prod = isl_vec_alloc(mat->ctx, mat->n_col);
517 if (!prod)
518 goto error;
520 for (i = 0; i < prod->size; ++i) {
521 isl_int_set_si(prod->el[i], 0);
522 for (j = 0; j < vec->size; ++j)
523 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
525 isl_mat_free(mat);
526 isl_vec_free(vec);
527 return prod;
528 error:
529 isl_mat_free(mat);
530 isl_vec_free(vec);
531 return NULL;
534 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
535 struct isl_mat *right)
537 int i;
538 struct isl_mat *sum;
540 if (!left || !right)
541 goto error;
543 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
544 isl_assert(left->ctx, left->n_row >= 1, goto error);
545 isl_assert(left->ctx, left->n_col >= 1, goto error);
546 isl_assert(left->ctx, right->n_col >= 1, goto error);
547 isl_assert(left->ctx,
548 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
549 goto error);
550 isl_assert(left->ctx,
551 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
552 goto error);
554 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
555 if (!sum)
556 goto error;
557 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
558 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
559 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
561 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
562 for (i = 1; i < sum->n_row; ++i) {
563 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
564 isl_int_addmul(sum->row[i][0],
565 right->row[0][0], right->row[i][0]);
566 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
567 left->n_col-1);
568 isl_seq_scale(sum->row[i]+left->n_col,
569 right->row[i]+1, right->row[0][0],
570 right->n_col-1);
573 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
574 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
575 isl_mat_free(left);
576 isl_mat_free(right);
577 return sum;
578 error:
579 isl_mat_free(left);
580 isl_mat_free(right);
581 return NULL;
584 static void exchange(struct isl_mat *M, struct isl_mat **U,
585 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
587 int r;
588 for (r = row; r < M->n_row; ++r)
589 isl_int_swap(M->row[r][i], M->row[r][j]);
590 if (U) {
591 for (r = 0; r < (*U)->n_row; ++r)
592 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
594 if (Q)
595 isl_mat_swap_rows(*Q, i, j);
598 static void subtract(struct isl_mat *M, struct isl_mat **U,
599 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
601 int r;
602 for (r = row; r < M->n_row; ++r)
603 isl_int_submul(M->row[r][j], m, M->row[r][i]);
604 if (U) {
605 for (r = 0; r < (*U)->n_row; ++r)
606 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
608 if (Q) {
609 for (r = 0; r < (*Q)->n_col; ++r)
610 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
614 static void oppose(struct isl_mat *M, struct isl_mat **U,
615 struct isl_mat **Q, unsigned row, unsigned col)
617 int r;
618 for (r = row; r < M->n_row; ++r)
619 isl_int_neg(M->row[r][col], M->row[r][col]);
620 if (U) {
621 for (r = 0; r < (*U)->n_row; ++r)
622 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
624 if (Q)
625 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
628 /* Given matrix M, compute
630 * M U = H
631 * M = H Q
633 * with U and Q unimodular matrices and H a matrix in column echelon form
634 * such that on each echelon row the entries in the non-echelon column
635 * are non-negative (if neg == 0) or non-positive (if neg == 1)
636 * and strictly smaller (in absolute value) than the entries in the echelon
637 * column.
638 * If U or Q are NULL, then these matrices are not computed.
640 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
641 struct isl_mat **U, struct isl_mat **Q)
643 isl_int c;
644 int row, col;
646 if (U)
647 *U = NULL;
648 if (Q)
649 *Q = NULL;
650 if (!M)
651 goto error;
652 M = isl_mat_cow(M);
653 if (!M)
654 goto error;
655 if (U) {
656 *U = isl_mat_identity(M->ctx, M->n_col);
657 if (!*U)
658 goto error;
660 if (Q) {
661 *Q = isl_mat_identity(M->ctx, M->n_col);
662 if (!*Q)
663 goto error;
666 col = 0;
667 isl_int_init(c);
668 for (row = 0; row < M->n_row; ++row) {
669 int first, i, off;
670 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
671 if (first == -1)
672 continue;
673 first += col;
674 if (first != col)
675 exchange(M, U, Q, row, first, col);
676 if (isl_int_is_neg(M->row[row][col]))
677 oppose(M, U, Q, row, col);
678 first = col+1;
679 while ((off = isl_seq_first_non_zero(M->row[row]+first,
680 M->n_col-first)) != -1) {
681 first += off;
682 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
683 subtract(M, U, Q, row, col, first, c);
684 if (!isl_int_is_zero(M->row[row][first]))
685 exchange(M, U, Q, row, first, col);
686 else
687 ++first;
689 for (i = 0; i < col; ++i) {
690 if (isl_int_is_zero(M->row[row][i]))
691 continue;
692 if (neg)
693 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
694 else
695 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
696 if (isl_int_is_zero(c))
697 continue;
698 subtract(M, U, Q, row, col, i, c);
700 ++col;
702 isl_int_clear(c);
704 return M;
705 error:
706 if (Q) {
707 isl_mat_free(*Q);
708 *Q = NULL;
710 if (U) {
711 isl_mat_free(*U);
712 *U = NULL;
714 isl_mat_free(M);
715 return NULL;
718 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
720 int i, rank;
721 struct isl_mat *U = NULL;
722 struct isl_mat *K;
724 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
725 if (!mat || !U)
726 goto error;
728 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
729 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
730 ++i;
731 if (i >= mat->n_row)
732 break;
734 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
735 if (!K)
736 goto error;
737 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
738 isl_mat_free(mat);
739 isl_mat_free(U);
740 return K;
741 error:
742 isl_mat_free(mat);
743 isl_mat_free(U);
744 return NULL;
747 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
749 int i;
750 struct isl_mat *mat2;
752 if (!mat)
753 return NULL;
754 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
755 if (!mat2)
756 goto error;
757 isl_int_set_si(mat2->row[0][0], 1);
758 isl_seq_clr(mat2->row[0]+1, mat->n_col);
759 for (i = 0; i < mat->n_row; ++i) {
760 isl_int_set_si(mat2->row[1+i][0], 0);
761 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
763 isl_mat_free(mat);
764 return mat2;
765 error:
766 isl_mat_free(mat);
767 return NULL;
770 /* Given two matrices M1 and M2, return the block matrix
772 * [ M1 0 ]
773 * [ 0 M2 ]
775 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
776 __isl_take isl_mat *mat2)
778 int i;
779 isl_mat *mat;
781 if (!mat1 || !mat2)
782 goto error;
784 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
785 mat1->n_col + mat2->n_col);
786 if (!mat)
787 goto error;
788 for (i = 0; i < mat1->n_row; ++i) {
789 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
790 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
792 for (i = 0; i < mat2->n_row; ++i) {
793 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
794 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
795 mat2->row[i], mat2->n_col);
797 isl_mat_free(mat1);
798 isl_mat_free(mat2);
799 return mat;
800 error:
801 isl_mat_free(mat1);
802 isl_mat_free(mat2);
803 return NULL;
806 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
808 int i;
810 for (i = 0; i < n_row; ++i)
811 if (!isl_int_is_zero(row[i][col]))
812 return i;
813 return -1;
816 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
818 int i, min = row_first_non_zero(row, n_row, col);
819 if (min < 0)
820 return -1;
821 for (i = min + 1; i < n_row; ++i) {
822 if (isl_int_is_zero(row[i][col]))
823 continue;
824 if (isl_int_abs_lt(row[i][col], row[min][col]))
825 min = i;
827 return min;
830 static isl_stat inv_exchange(__isl_keep isl_mat **left,
831 __isl_keep isl_mat **right, unsigned i, unsigned j)
833 *left = isl_mat_swap_rows(*left, i, j);
834 *right = isl_mat_swap_rows(*right, i, j);
836 if (!*left || !*right)
837 return isl_stat_error;
838 return isl_stat_ok;
841 static void inv_oppose(
842 struct isl_mat *left, struct isl_mat *right, unsigned row)
844 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
845 isl_seq_neg(right->row[row], right->row[row], right->n_col);
848 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
849 unsigned row, unsigned i, isl_int m)
851 isl_int_neg(m, m);
852 isl_seq_combine(left->row[i]+row,
853 left->ctx->one, left->row[i]+row,
854 m, left->row[row]+row,
855 left->n_col-row);
856 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
857 m, right->row[row], right->n_col);
860 /* Compute inv(left)*right
862 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
863 struct isl_mat *right)
865 int row;
866 isl_int a, b;
868 if (!left || !right)
869 goto error;
871 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
872 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
874 if (left->n_row == 0) {
875 isl_mat_free(left);
876 return right;
879 left = isl_mat_cow(left);
880 right = isl_mat_cow(right);
881 if (!left || !right)
882 goto error;
884 isl_int_init(a);
885 isl_int_init(b);
886 for (row = 0; row < left->n_row; ++row) {
887 int pivot, first, i, off;
888 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
889 if (pivot < 0) {
890 isl_int_clear(a);
891 isl_int_clear(b);
892 isl_assert(left->ctx, pivot >= 0, goto error);
894 pivot += row;
895 if (pivot != row)
896 if (inv_exchange(&left, &right, pivot, row) < 0)
897 goto error;
898 if (isl_int_is_neg(left->row[row][row]))
899 inv_oppose(left, right, row);
900 first = row+1;
901 while ((off = row_first_non_zero(left->row+first,
902 left->n_row-first, row)) != -1) {
903 first += off;
904 isl_int_fdiv_q(a, left->row[first][row],
905 left->row[row][row]);
906 inv_subtract(left, right, row, first, a);
907 if (!isl_int_is_zero(left->row[first][row])) {
908 if (inv_exchange(&left, &right, row, first) < 0)
909 goto error;
910 } else {
911 ++first;
914 for (i = 0; i < row; ++i) {
915 if (isl_int_is_zero(left->row[i][row]))
916 continue;
917 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
918 isl_int_divexact(b, left->row[i][row], a);
919 isl_int_divexact(a, left->row[row][row], a);
920 isl_int_neg(b, b);
921 isl_seq_combine(left->row[i] + i,
922 a, left->row[i] + i,
923 b, left->row[row] + i,
924 left->n_col - i);
925 isl_seq_combine(right->row[i], a, right->row[i],
926 b, right->row[row], right->n_col);
929 isl_int_clear(b);
931 isl_int_set(a, left->row[0][0]);
932 for (row = 1; row < left->n_row; ++row)
933 isl_int_lcm(a, a, left->row[row][row]);
934 if (isl_int_is_zero(a)){
935 isl_int_clear(a);
936 isl_assert(left->ctx, 0, goto error);
938 for (row = 0; row < left->n_row; ++row) {
939 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
940 if (isl_int_is_one(left->row[row][row]))
941 continue;
942 isl_seq_scale(right->row[row], right->row[row],
943 left->row[row][row], right->n_col);
945 isl_int_clear(a);
947 isl_mat_free(left);
948 return right;
949 error:
950 isl_mat_free(left);
951 isl_mat_free(right);
952 return NULL;
955 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
957 int i;
959 for (i = 0; i < mat->n_row; ++i)
960 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
963 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
964 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
966 int i;
967 isl_int tmp;
969 isl_int_init(tmp);
970 for (i = 0; i < mat->n_row; ++i) {
971 isl_int_mul(tmp, m1, mat->row[i][src1]);
972 isl_int_addmul(tmp, m2, mat->row[i][src2]);
973 isl_int_set(mat->row[i][dst], tmp);
975 isl_int_clear(tmp);
978 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
980 struct isl_mat *inv;
981 int row;
982 isl_int a, b;
984 mat = isl_mat_cow(mat);
985 if (!mat)
986 return NULL;
988 inv = isl_mat_identity(mat->ctx, mat->n_col);
989 inv = isl_mat_cow(inv);
990 if (!inv)
991 goto error;
993 isl_int_init(a);
994 isl_int_init(b);
995 for (row = 0; row < mat->n_row; ++row) {
996 int pivot, first, i, off;
997 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
998 if (pivot < 0) {
999 isl_int_clear(a);
1000 isl_int_clear(b);
1001 isl_assert(mat->ctx, pivot >= 0, goto error);
1003 pivot += row;
1004 if (pivot != row)
1005 exchange(mat, &inv, NULL, row, pivot, row);
1006 if (isl_int_is_neg(mat->row[row][row]))
1007 oppose(mat, &inv, NULL, row, row);
1008 first = row+1;
1009 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
1010 mat->n_col-first)) != -1) {
1011 first += off;
1012 isl_int_fdiv_q(a, mat->row[row][first],
1013 mat->row[row][row]);
1014 subtract(mat, &inv, NULL, row, row, first, a);
1015 if (!isl_int_is_zero(mat->row[row][first]))
1016 exchange(mat, &inv, NULL, row, row, first);
1017 else
1018 ++first;
1020 for (i = 0; i < row; ++i) {
1021 if (isl_int_is_zero(mat->row[row][i]))
1022 continue;
1023 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
1024 isl_int_divexact(b, mat->row[row][i], a);
1025 isl_int_divexact(a, mat->row[row][row], a);
1026 isl_int_neg(a, a);
1027 isl_mat_col_combine(mat, i, a, i, b, row);
1028 isl_mat_col_combine(inv, i, a, i, b, row);
1031 isl_int_clear(b);
1033 isl_int_set(a, mat->row[0][0]);
1034 for (row = 1; row < mat->n_row; ++row)
1035 isl_int_lcm(a, a, mat->row[row][row]);
1036 if (isl_int_is_zero(a)){
1037 isl_int_clear(a);
1038 goto error;
1040 for (row = 0; row < mat->n_row; ++row) {
1041 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
1042 if (isl_int_is_one(mat->row[row][row]))
1043 continue;
1044 isl_mat_col_scale(inv, row, mat->row[row][row]);
1046 isl_int_clear(a);
1048 isl_mat_free(mat);
1050 return inv;
1051 error:
1052 isl_mat_free(mat);
1053 isl_mat_free(inv);
1054 return NULL;
1057 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
1059 struct isl_mat *transpose = NULL;
1060 int i, j;
1062 if (!mat)
1063 return NULL;
1065 if (mat->n_col == mat->n_row) {
1066 mat = isl_mat_cow(mat);
1067 if (!mat)
1068 return NULL;
1069 for (i = 0; i < mat->n_row; ++i)
1070 for (j = i + 1; j < mat->n_col; ++j)
1071 isl_int_swap(mat->row[i][j], mat->row[j][i]);
1072 return mat;
1074 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1075 if (!transpose)
1076 goto error;
1077 for (i = 0; i < mat->n_row; ++i)
1078 for (j = 0; j < mat->n_col; ++j)
1079 isl_int_set(transpose->row[j][i], mat->row[i][j]);
1080 isl_mat_free(mat);
1081 return transpose;
1082 error:
1083 isl_mat_free(mat);
1084 return NULL;
1087 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
1089 int r;
1091 mat = isl_mat_cow(mat);
1092 if (check_col_range(mat, i, 1) < 0 ||
1093 check_col_range(mat, j, 1) < 0)
1094 return isl_mat_free(mat);
1096 for (r = 0; r < mat->n_row; ++r)
1097 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1098 return mat;
1101 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
1103 isl_int *t;
1105 if (!mat)
1106 return NULL;
1107 mat = isl_mat_cow(mat);
1108 if (check_row_range(mat, i, 1) < 0 ||
1109 check_row_range(mat, j, 1) < 0)
1110 return isl_mat_free(mat);
1112 t = mat->row[i];
1113 mat->row[i] = mat->row[j];
1114 mat->row[j] = t;
1115 return mat;
1118 /* Calculate the product of two matrices.
1120 * This function is optimized for operand matrices that contain many zeros and
1121 * skips multiplications where we know one of the operands is zero.
1123 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1124 __isl_take isl_mat *right)
1126 int i, j, k;
1127 struct isl_mat *prod;
1129 if (!left || !right)
1130 goto error;
1131 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1132 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1133 if (!prod)
1134 goto error;
1135 if (left->n_col == 0) {
1136 for (i = 0; i < prod->n_row; ++i)
1137 isl_seq_clr(prod->row[i], prod->n_col);
1138 isl_mat_free(left);
1139 isl_mat_free(right);
1140 return prod;
1142 for (i = 0; i < prod->n_row; ++i) {
1143 for (j = 0; j < prod->n_col; ++j)
1144 isl_int_mul(prod->row[i][j],
1145 left->row[i][0], right->row[0][j]);
1146 for (k = 1; k < left->n_col; ++k) {
1147 if (isl_int_is_zero(left->row[i][k]))
1148 continue;
1149 for (j = 0; j < prod->n_col; ++j)
1150 isl_int_addmul(prod->row[i][j],
1151 left->row[i][k], right->row[k][j]);
1154 isl_mat_free(left);
1155 isl_mat_free(right);
1156 return prod;
1157 error:
1158 isl_mat_free(left);
1159 isl_mat_free(right);
1160 return NULL;
1163 /* Replace the variables x in the rows q by x' given by x = M x',
1164 * with M the matrix mat.
1166 * If the number of new variables is greater than the original
1167 * number of variables, then the rows q have already been
1168 * preextended. If the new number is smaller, then the coefficients
1169 * of the divs, which are not changed, need to be shifted down.
1170 * The row q may be the equalities, the inequalities or the
1171 * div expressions. In the latter case, has_div is true and
1172 * we need to take into account the extra denominator column.
1174 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1175 unsigned n_div, int has_div, struct isl_mat *mat)
1177 int i;
1178 struct isl_mat *t;
1179 int e;
1181 if (mat->n_col >= mat->n_row)
1182 e = 0;
1183 else
1184 e = mat->n_row - mat->n_col;
1185 if (has_div)
1186 for (i = 0; i < n; ++i)
1187 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1188 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1189 t = isl_mat_product(t, mat);
1190 if (!t)
1191 return -1;
1192 for (i = 0; i < n; ++i) {
1193 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1194 isl_seq_cpy(q[i] + has_div + t->n_col,
1195 q[i] + has_div + t->n_col + e, n_div);
1196 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1198 isl_mat_free(t);
1199 return 0;
1202 /* Replace the variables x in bset by x' given by x = M x', with
1203 * M the matrix mat.
1205 * If there are fewer variables x' then there are x, then we perform
1206 * the transformation in place, which means that, in principle,
1207 * this frees up some extra variables as the number
1208 * of columns remains constant, but we would have to extend
1209 * the div array too as the number of rows in this array is assumed
1210 * to be equal to extra.
1212 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1213 struct isl_mat *mat)
1215 struct isl_ctx *ctx;
1217 if (!bset || !mat)
1218 goto error;
1220 ctx = bset->ctx;
1221 bset = isl_basic_set_cow(bset);
1222 if (!bset)
1223 goto error;
1225 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1226 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1227 isl_assert(ctx, mat->n_col > 0, goto error);
1229 if (mat->n_col > mat->n_row) {
1230 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1231 if (!bset)
1232 goto error;
1233 } else if (mat->n_col < mat->n_row) {
1234 bset->dim = isl_space_cow(bset->dim);
1235 if (!bset->dim)
1236 goto error;
1237 bset->dim->n_out -= mat->n_row - mat->n_col;
1240 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1241 isl_mat_copy(mat)) < 0)
1242 goto error;
1244 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1245 isl_mat_copy(mat)) < 0)
1246 goto error;
1248 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1249 goto error2;
1251 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1252 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1253 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1254 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1255 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1257 bset = isl_basic_set_simplify(bset);
1258 bset = isl_basic_set_finalize(bset);
1260 return bset;
1261 error:
1262 isl_mat_free(mat);
1263 error2:
1264 isl_basic_set_free(bset);
1265 return NULL;
1268 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1270 int i;
1272 set = isl_set_cow(set);
1273 if (!set)
1274 goto error;
1276 for (i = 0; i < set->n; ++i) {
1277 set->p[i] = isl_basic_set_preimage(set->p[i],
1278 isl_mat_copy(mat));
1279 if (!set->p[i])
1280 goto error;
1282 if (mat->n_col != mat->n_row) {
1283 set->dim = isl_space_cow(set->dim);
1284 if (!set->dim)
1285 goto error;
1286 set->dim->n_out += mat->n_col;
1287 set->dim->n_out -= mat->n_row;
1289 isl_mat_free(mat);
1290 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1291 return set;
1292 error:
1293 isl_set_free(set);
1294 isl_mat_free(mat);
1295 return NULL;
1298 /* Replace the variables x starting at pos in the rows q
1299 * by x' with x = M x' with M the matrix mat.
1300 * That is, replace the corresponding coefficients c by c M.
1302 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1303 unsigned pos, __isl_take isl_mat *mat)
1305 int i;
1306 isl_mat *t;
1308 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1309 t = isl_mat_product(t, mat);
1310 if (!t)
1311 return -1;
1312 for (i = 0; i < n; ++i)
1313 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1314 isl_mat_free(t);
1315 return 0;
1318 /* Replace the variables x of type "type" starting at "first" in "bmap"
1319 * by x' with x = M x' with M the matrix trans.
1320 * That is, replace the corresponding coefficients c by c M.
1322 * The transformation matrix should be a square matrix.
1324 __isl_give isl_basic_map *isl_basic_map_transform_dims(
1325 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
1326 __isl_take isl_mat *trans)
1328 isl_ctx *ctx;
1329 unsigned pos;
1331 bmap = isl_basic_map_cow(bmap);
1332 if (!bmap || !trans)
1333 goto error;
1335 ctx = isl_basic_map_get_ctx(bmap);
1336 if (trans->n_row != trans->n_col)
1337 isl_die(trans->ctx, isl_error_invalid,
1338 "expecting square transformation matrix", goto error);
1339 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
1340 isl_die(trans->ctx, isl_error_invalid,
1341 "oversized transformation matrix", goto error);
1343 pos = isl_basic_map_offset(bmap, type) + first;
1345 if (transform(ctx, bmap->eq, bmap->n_eq, pos, isl_mat_copy(trans)) < 0)
1346 goto error;
1347 if (transform(ctx, bmap->ineq, bmap->n_ineq, pos,
1348 isl_mat_copy(trans)) < 0)
1349 goto error;
1350 if (transform(ctx, bmap->div, bmap->n_div, 1 + pos,
1351 isl_mat_copy(trans)) < 0)
1352 goto error;
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1357 isl_mat_free(trans);
1358 return bmap;
1359 error:
1360 isl_mat_free(trans);
1361 isl_basic_map_free(bmap);
1362 return NULL;
1365 /* Replace the variables x of type "type" starting at "first" in "bset"
1366 * by x' with x = M x' with M the matrix trans.
1367 * That is, replace the corresponding coefficients c by c M.
1369 * The transformation matrix should be a square matrix.
1371 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1372 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1373 __isl_take isl_mat *trans)
1375 return isl_basic_map_transform_dims(bset, type, first, trans);
1378 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1380 int i, j;
1382 if (!mat) {
1383 fprintf(out, "%*snull mat\n", indent, "");
1384 return;
1387 if (mat->n_row == 0)
1388 fprintf(out, "%*s[]\n", indent, "");
1390 for (i = 0; i < mat->n_row; ++i) {
1391 if (!i)
1392 fprintf(out, "%*s[[", indent, "");
1393 else
1394 fprintf(out, "%*s[", indent+1, "");
1395 for (j = 0; j < mat->n_col; ++j) {
1396 if (j)
1397 fprintf(out, ",");
1398 isl_int_print(out, mat->row[i][j], 0);
1400 if (i == mat->n_row-1)
1401 fprintf(out, "]]\n");
1402 else
1403 fprintf(out, "]\n");
1407 void isl_mat_dump(__isl_keep isl_mat *mat)
1409 isl_mat_print_internal(mat, stderr, 0);
1412 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1414 int r;
1416 if (n == 0)
1417 return mat;
1419 mat = isl_mat_cow(mat);
1420 if (check_col_range(mat, col, n) < 0)
1421 return isl_mat_free(mat);
1423 if (col != mat->n_col-n) {
1424 for (r = 0; r < mat->n_row; ++r)
1425 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1426 mat->n_col - col - n);
1428 mat->n_col -= n;
1429 return mat;
1432 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1434 int r;
1436 mat = isl_mat_cow(mat);
1437 if (check_row_range(mat, row, n) < 0)
1438 return isl_mat_free(mat);
1440 for (r = row; r+n < mat->n_row; ++r)
1441 mat->row[r] = mat->row[r+n];
1443 mat->n_row -= n;
1444 return mat;
1447 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1448 unsigned col, unsigned n)
1450 isl_mat *ext;
1452 if (check_col_range(mat, col, 0) < 0)
1453 return isl_mat_free(mat);
1454 if (n == 0)
1455 return mat;
1457 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1458 if (!ext)
1459 goto error;
1461 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1462 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1463 col + n, col, mat->n_col - col);
1465 isl_mat_free(mat);
1466 return ext;
1467 error:
1468 isl_mat_free(mat);
1469 return NULL;
1472 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1473 unsigned first, unsigned n)
1475 int i;
1477 if (!mat)
1478 return NULL;
1479 mat = isl_mat_insert_cols(mat, first, n);
1480 if (!mat)
1481 return NULL;
1483 for (i = 0; i < mat->n_row; ++i)
1484 isl_seq_clr(mat->row[i] + first, n);
1486 return mat;
1489 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1491 if (!mat)
1492 return NULL;
1494 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1497 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1498 unsigned row, unsigned n)
1500 isl_mat *ext;
1502 if (check_row_range(mat, row, 0) < 0)
1503 return isl_mat_free(mat);
1504 if (n == 0)
1505 return mat;
1507 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1508 if (!ext)
1509 goto error;
1511 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1512 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1513 mat->n_row - row, 0, 0, mat->n_col);
1515 isl_mat_free(mat);
1516 return ext;
1517 error:
1518 isl_mat_free(mat);
1519 return NULL;
1522 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1524 if (!mat)
1525 return NULL;
1527 return isl_mat_insert_rows(mat, mat->n_row, n);
1530 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1531 unsigned row, unsigned n)
1533 int i;
1535 mat = isl_mat_insert_rows(mat, row, n);
1536 if (!mat)
1537 return NULL;
1539 for (i = 0; i < n; ++i)
1540 isl_seq_clr(mat->row[row + i], mat->n_col);
1542 return mat;
1545 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1547 if (!mat)
1548 return NULL;
1550 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1553 void isl_mat_col_submul(struct isl_mat *mat,
1554 int dst_col, isl_int f, int src_col)
1556 int i;
1558 for (i = 0; i < mat->n_row; ++i)
1559 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1562 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1564 int i;
1566 if (!mat)
1567 return;
1569 for (i = 0; i < mat->n_row; ++i)
1570 isl_int_add(mat->row[i][dst_col],
1571 mat->row[i][dst_col], mat->row[i][src_col]);
1574 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1576 int i;
1578 for (i = 0; i < mat->n_row; ++i)
1579 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1582 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1583 * return the result.
1585 __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1586 isl_int f, int src_col)
1588 int i;
1590 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1591 return isl_mat_free(mat);
1593 for (i = 0; i < mat->n_row; ++i) {
1594 if (isl_int_is_zero(mat->row[i][src_col]))
1595 continue;
1596 mat = isl_mat_cow(mat);
1597 if (!mat)
1598 return NULL;
1599 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1602 return mat;
1605 /* Negate column "col" of "mat" and return the result.
1607 __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1609 int i;
1611 if (check_col(mat, col) < 0)
1612 return isl_mat_free(mat);
1614 for (i = 0; i < mat->n_row; ++i) {
1615 if (isl_int_is_zero(mat->row[i][col]))
1616 continue;
1617 mat = isl_mat_cow(mat);
1618 if (!mat)
1619 return NULL;
1620 isl_int_neg(mat->row[i][col], mat->row[i][col]);
1623 return mat;
1626 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1628 int r;
1629 struct isl_mat *H = NULL, *Q = NULL;
1631 if (!M)
1632 return NULL;
1634 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1635 M->n_row = row;
1636 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1637 M->n_row = M->n_col;
1638 if (!H)
1639 goto error;
1640 for (r = 0; r < row; ++r)
1641 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1642 for (r = row; r < M->n_row; ++r)
1643 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1644 isl_mat_free(H);
1645 isl_mat_free(Q);
1646 return M;
1647 error:
1648 isl_mat_free(H);
1649 isl_mat_free(Q);
1650 isl_mat_free(M);
1651 return NULL;
1654 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1655 __isl_take isl_mat *bot)
1657 struct isl_mat *mat;
1659 if (!top || !bot)
1660 goto error;
1662 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1663 if (top->n_row == 0) {
1664 isl_mat_free(top);
1665 return bot;
1667 if (bot->n_row == 0) {
1668 isl_mat_free(bot);
1669 return top;
1672 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1673 if (!mat)
1674 goto error;
1675 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1676 0, 0, mat->n_col);
1677 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1678 0, 0, mat->n_col);
1679 isl_mat_free(top);
1680 isl_mat_free(bot);
1681 return mat;
1682 error:
1683 isl_mat_free(top);
1684 isl_mat_free(bot);
1685 return NULL;
1688 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1690 int i;
1692 if (!mat1 || !mat2)
1693 return -1;
1695 if (mat1->n_row != mat2->n_row)
1696 return 0;
1698 if (mat1->n_col != mat2->n_col)
1699 return 0;
1701 for (i = 0; i < mat1->n_row; ++i)
1702 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1703 return 0;
1705 return 1;
1708 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1710 struct isl_mat *mat;
1712 if (!vec)
1713 return NULL;
1714 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1715 if (!mat)
1716 goto error;
1718 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1720 isl_vec_free(vec);
1721 return mat;
1722 error:
1723 isl_vec_free(vec);
1724 return NULL;
1727 /* Return a copy of row "row" of "mat" as an isl_vec.
1729 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1731 isl_vec *v;
1733 if (!mat)
1734 return NULL;
1735 if (row >= mat->n_row)
1736 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1737 return NULL);
1739 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1740 if (!v)
1741 return NULL;
1742 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1744 return v;
1747 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1748 __isl_take isl_vec *bot)
1750 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1753 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1754 unsigned dst_col, unsigned src_col, unsigned n)
1756 isl_mat *res;
1758 if (!mat)
1759 return NULL;
1760 if (n == 0 || dst_col == src_col)
1761 return mat;
1763 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1764 if (!res)
1765 goto error;
1767 if (dst_col < src_col) {
1768 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1769 0, 0, dst_col);
1770 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1771 dst_col, src_col, n);
1772 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1773 dst_col + n, dst_col, src_col - dst_col);
1774 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1775 src_col + n, src_col + n,
1776 res->n_col - src_col - n);
1777 } else {
1778 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1779 0, 0, src_col);
1780 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1781 src_col, src_col + n, dst_col - src_col);
1782 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1783 dst_col, src_col, n);
1784 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1785 dst_col + n, dst_col + n,
1786 res->n_col - dst_col - n);
1788 isl_mat_free(mat);
1790 return res;
1791 error:
1792 isl_mat_free(mat);
1793 return NULL;
1796 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1797 * Return isl_stat_ok on success and isl_stat_error on failure.
1799 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1801 if (!mat)
1802 return isl_stat_error;
1804 if (row < 0 || row >= mat->n_row)
1805 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
1806 "row out of range", return isl_stat_error);
1807 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1809 return isl_stat_ok;
1812 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1814 int i;
1815 isl_int g;
1817 isl_int_set_si(*gcd, 0);
1818 if (!mat)
1819 return;
1821 isl_int_init(g);
1822 for (i = 0; i < mat->n_row; ++i) {
1823 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1824 isl_int_gcd(*gcd, *gcd, g);
1826 isl_int_clear(g);
1829 /* Return the result of scaling "mat" by a factor of "m".
1831 __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1833 int i;
1835 if (isl_int_is_one(m))
1836 return mat;
1838 mat = isl_mat_cow(mat);
1839 if (!mat)
1840 return NULL;
1842 for (i = 0; i < mat->n_row; ++i)
1843 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1845 return mat;
1848 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1850 int i;
1852 if (isl_int_is_one(m))
1853 return mat;
1855 mat = isl_mat_cow(mat);
1856 if (!mat)
1857 return NULL;
1859 for (i = 0; i < mat->n_row; ++i)
1860 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1862 return mat;
1865 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1866 isl_int m)
1868 if (isl_int_is_one(m))
1869 return mat;
1871 mat = isl_mat_cow(mat);
1872 if (!mat)
1873 return NULL;
1875 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1877 return mat;
1880 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1882 isl_int gcd;
1884 if (!mat)
1885 return NULL;
1887 isl_int_init(gcd);
1888 isl_mat_gcd(mat, &gcd);
1889 mat = isl_mat_scale_down(mat, gcd);
1890 isl_int_clear(gcd);
1892 return mat;
1895 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1897 mat = isl_mat_cow(mat);
1898 if (!mat)
1899 return NULL;
1901 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1903 return mat;
1906 /* Number of initial non-zero columns.
1908 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1910 int i;
1912 if (!mat)
1913 return -1;
1915 for (i = 0; i < mat->n_col; ++i)
1916 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1917 break;
1919 return i;