isl_map.c: add_lower_div_constraint: return isl_stat
[isl.git] / isl_mat.c
blobec514bdd2faeb3cab684afee3be7778d65073b31
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 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
276 if (!mat)
277 return -1;
278 if (row < 0 || row >= mat->n_row)
279 isl_die(mat->ctx, isl_error_invalid, "row out of range",
280 return -1);
281 if (check_col(mat, col) < 0)
282 return -1;
283 isl_int_set(*v, mat->row[row][col]);
284 return 0;
287 /* Extract the element at row "row", oolumn "col" of "mat".
289 __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
290 int row, int col)
292 isl_ctx *ctx;
294 if (!mat)
295 return NULL;
296 ctx = isl_mat_get_ctx(mat);
297 if (row < 0 || row >= mat->n_row)
298 isl_die(ctx, isl_error_invalid, "row out of range",
299 return NULL);
300 if (check_col(mat, col) < 0)
301 return NULL;
302 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
305 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
306 int row, int col, isl_int v)
308 mat = isl_mat_cow(mat);
309 if (!mat)
310 return NULL;
311 if (row < 0 || row >= mat->n_row)
312 isl_die(mat->ctx, isl_error_invalid, "row out of range",
313 goto error);
314 if (check_col(mat, col) < 0)
315 return isl_mat_free(mat);
316 isl_int_set(mat->row[row][col], v);
317 return mat;
318 error:
319 isl_mat_free(mat);
320 return NULL;
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 (!mat)
328 return NULL;
329 if (row < 0 || row >= mat->n_row)
330 isl_die(mat->ctx, isl_error_invalid, "row out of range",
331 goto error);
332 if (check_col(mat, col) < 0)
333 return isl_mat_free(mat);
334 isl_int_set_si(mat->row[row][col], v);
335 return mat;
336 error:
337 isl_mat_free(mat);
338 return NULL;
341 /* Replace the element at row "row", column "col" of "mat" by "v".
343 __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
344 int row, int col, __isl_take isl_val *v)
346 if (!v)
347 return isl_mat_free(mat);
348 if (!isl_val_is_int(v))
349 isl_die(isl_val_get_ctx(v), isl_error_invalid,
350 "expecting integer value", goto error);
351 mat = isl_mat_set_element(mat, row, col, v->n);
352 isl_val_free(v);
353 return mat;
354 error:
355 isl_val_free(v);
356 return isl_mat_free(mat);
359 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
361 int i;
362 struct isl_mat *mat;
364 mat = isl_mat_alloc(ctx, n_row, n_row);
365 if (!mat)
366 return NULL;
367 for (i = 0; i < n_row; ++i) {
368 isl_seq_clr(mat->row[i], i);
369 isl_int_set(mat->row[i][i], d);
370 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
373 return mat;
376 /* Create an "n_row" by "n_col" matrix with zero elements.
378 __isl_give isl_mat *isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
380 int i;
381 isl_mat *mat;
383 mat = isl_mat_alloc(ctx, n_row, n_col);
384 if (!mat)
385 return NULL;
386 for (i = 0; i < n_row; ++i)
387 isl_seq_clr(mat->row[i], n_col);
389 return mat;
392 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
394 if (!ctx)
395 return NULL;
396 return isl_mat_diag(ctx, n_row, ctx->one);
399 /* Is "mat" a (possibly scaled) identity matrix?
401 int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
403 int i;
405 if (!mat)
406 return -1;
407 if (mat->n_row != mat->n_col)
408 return 0;
410 for (i = 0; i < mat->n_row; ++i) {
411 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
412 return 0;
413 if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
414 return 0;
415 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
416 mat->n_col - (i + 1)) != -1)
417 return 0;
420 return 1;
423 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
425 int i;
426 struct isl_vec *prod;
428 if (!mat || !vec)
429 goto error;
431 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
433 prod = isl_vec_alloc(mat->ctx, mat->n_row);
434 if (!prod)
435 goto error;
437 for (i = 0; i < prod->size; ++i)
438 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
439 &prod->block.data[i]);
440 isl_mat_free(mat);
441 isl_vec_free(vec);
442 return prod;
443 error:
444 isl_mat_free(mat);
445 isl_vec_free(vec);
446 return NULL;
449 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
450 __isl_take isl_vec *vec)
452 struct isl_mat *vec_mat;
453 int i;
455 if (!mat || !vec)
456 goto error;
457 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
458 if (!vec_mat)
459 goto error;
460 for (i = 0; i < vec->size; ++i)
461 isl_int_set(vec_mat->row[i][0], vec->el[i]);
462 vec_mat = isl_mat_inverse_product(mat, vec_mat);
463 isl_vec_free(vec);
464 if (!vec_mat)
465 return NULL;
466 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
467 if (vec)
468 for (i = 0; i < vec->size; ++i)
469 isl_int_set(vec->el[i], vec_mat->row[i][0]);
470 isl_mat_free(vec_mat);
471 return vec;
472 error:
473 isl_mat_free(mat);
474 isl_vec_free(vec);
475 return NULL;
478 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
480 int i, j;
481 struct isl_vec *prod;
483 if (!mat || !vec)
484 goto error;
486 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
488 prod = isl_vec_alloc(mat->ctx, mat->n_col);
489 if (!prod)
490 goto error;
492 for (i = 0; i < prod->size; ++i) {
493 isl_int_set_si(prod->el[i], 0);
494 for (j = 0; j < vec->size; ++j)
495 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
497 isl_mat_free(mat);
498 isl_vec_free(vec);
499 return prod;
500 error:
501 isl_mat_free(mat);
502 isl_vec_free(vec);
503 return NULL;
506 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
507 struct isl_mat *right)
509 int i;
510 struct isl_mat *sum;
512 if (!left || !right)
513 goto error;
515 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
516 isl_assert(left->ctx, left->n_row >= 1, goto error);
517 isl_assert(left->ctx, left->n_col >= 1, goto error);
518 isl_assert(left->ctx, right->n_col >= 1, goto error);
519 isl_assert(left->ctx,
520 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
521 goto error);
522 isl_assert(left->ctx,
523 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
524 goto error);
526 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
527 if (!sum)
528 goto error;
529 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
530 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
531 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
533 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
534 for (i = 1; i < sum->n_row; ++i) {
535 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
536 isl_int_addmul(sum->row[i][0],
537 right->row[0][0], right->row[i][0]);
538 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
539 left->n_col-1);
540 isl_seq_scale(sum->row[i]+left->n_col,
541 right->row[i]+1, right->row[0][0],
542 right->n_col-1);
545 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
546 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
547 isl_mat_free(left);
548 isl_mat_free(right);
549 return sum;
550 error:
551 isl_mat_free(left);
552 isl_mat_free(right);
553 return NULL;
556 static void exchange(struct isl_mat *M, struct isl_mat **U,
557 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
559 int r;
560 for (r = row; r < M->n_row; ++r)
561 isl_int_swap(M->row[r][i], M->row[r][j]);
562 if (U) {
563 for (r = 0; r < (*U)->n_row; ++r)
564 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
566 if (Q)
567 isl_mat_swap_rows(*Q, i, j);
570 static void subtract(struct isl_mat *M, struct isl_mat **U,
571 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
573 int r;
574 for (r = row; r < M->n_row; ++r)
575 isl_int_submul(M->row[r][j], m, M->row[r][i]);
576 if (U) {
577 for (r = 0; r < (*U)->n_row; ++r)
578 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
580 if (Q) {
581 for (r = 0; r < (*Q)->n_col; ++r)
582 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
586 static void oppose(struct isl_mat *M, struct isl_mat **U,
587 struct isl_mat **Q, unsigned row, unsigned col)
589 int r;
590 for (r = row; r < M->n_row; ++r)
591 isl_int_neg(M->row[r][col], M->row[r][col]);
592 if (U) {
593 for (r = 0; r < (*U)->n_row; ++r)
594 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
596 if (Q)
597 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
600 /* Given matrix M, compute
602 * M U = H
603 * M = H Q
605 * with U and Q unimodular matrices and H a matrix in column echelon form
606 * such that on each echelon row the entries in the non-echelon column
607 * are non-negative (if neg == 0) or non-positive (if neg == 1)
608 * and strictly smaller (in absolute value) than the entries in the echelon
609 * column.
610 * If U or Q are NULL, then these matrices are not computed.
612 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
613 struct isl_mat **U, struct isl_mat **Q)
615 isl_int c;
616 int row, col;
618 if (U)
619 *U = NULL;
620 if (Q)
621 *Q = NULL;
622 if (!M)
623 goto error;
624 M = isl_mat_cow(M);
625 if (!M)
626 goto error;
627 if (U) {
628 *U = isl_mat_identity(M->ctx, M->n_col);
629 if (!*U)
630 goto error;
632 if (Q) {
633 *Q = isl_mat_identity(M->ctx, M->n_col);
634 if (!*Q)
635 goto error;
638 col = 0;
639 isl_int_init(c);
640 for (row = 0; row < M->n_row; ++row) {
641 int first, i, off;
642 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
643 if (first == -1)
644 continue;
645 first += col;
646 if (first != col)
647 exchange(M, U, Q, row, first, col);
648 if (isl_int_is_neg(M->row[row][col]))
649 oppose(M, U, Q, row, col);
650 first = col+1;
651 while ((off = isl_seq_first_non_zero(M->row[row]+first,
652 M->n_col-first)) != -1) {
653 first += off;
654 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
655 subtract(M, U, Q, row, col, first, c);
656 if (!isl_int_is_zero(M->row[row][first]))
657 exchange(M, U, Q, row, first, col);
658 else
659 ++first;
661 for (i = 0; i < col; ++i) {
662 if (isl_int_is_zero(M->row[row][i]))
663 continue;
664 if (neg)
665 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
666 else
667 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
668 if (isl_int_is_zero(c))
669 continue;
670 subtract(M, U, Q, row, col, i, c);
672 ++col;
674 isl_int_clear(c);
676 return M;
677 error:
678 if (Q) {
679 isl_mat_free(*Q);
680 *Q = NULL;
682 if (U) {
683 isl_mat_free(*U);
684 *U = NULL;
686 isl_mat_free(M);
687 return NULL;
690 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
692 int i, rank;
693 struct isl_mat *U = NULL;
694 struct isl_mat *K;
696 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
697 if (!mat || !U)
698 goto error;
700 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
701 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
702 ++i;
703 if (i >= mat->n_row)
704 break;
706 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
707 if (!K)
708 goto error;
709 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
710 isl_mat_free(mat);
711 isl_mat_free(U);
712 return K;
713 error:
714 isl_mat_free(mat);
715 isl_mat_free(U);
716 return NULL;
719 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
721 int i;
722 struct isl_mat *mat2;
724 if (!mat)
725 return NULL;
726 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
727 if (!mat2)
728 goto error;
729 isl_int_set_si(mat2->row[0][0], 1);
730 isl_seq_clr(mat2->row[0]+1, mat->n_col);
731 for (i = 0; i < mat->n_row; ++i) {
732 isl_int_set_si(mat2->row[1+i][0], 0);
733 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
735 isl_mat_free(mat);
736 return mat2;
737 error:
738 isl_mat_free(mat);
739 return NULL;
742 /* Given two matrices M1 and M2, return the block matrix
744 * [ M1 0 ]
745 * [ 0 M2 ]
747 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
748 __isl_take isl_mat *mat2)
750 int i;
751 isl_mat *mat;
753 if (!mat1 || !mat2)
754 goto error;
756 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
757 mat1->n_col + mat2->n_col);
758 if (!mat)
759 goto error;
760 for (i = 0; i < mat1->n_row; ++i) {
761 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
762 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
764 for (i = 0; i < mat2->n_row; ++i) {
765 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
766 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
767 mat2->row[i], mat2->n_col);
769 isl_mat_free(mat1);
770 isl_mat_free(mat2);
771 return mat;
772 error:
773 isl_mat_free(mat1);
774 isl_mat_free(mat2);
775 return NULL;
778 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
780 int i;
782 for (i = 0; i < n_row; ++i)
783 if (!isl_int_is_zero(row[i][col]))
784 return i;
785 return -1;
788 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
790 int i, min = row_first_non_zero(row, n_row, col);
791 if (min < 0)
792 return -1;
793 for (i = min + 1; i < n_row; ++i) {
794 if (isl_int_is_zero(row[i][col]))
795 continue;
796 if (isl_int_abs_lt(row[i][col], row[min][col]))
797 min = i;
799 return min;
802 static isl_stat inv_exchange(__isl_keep isl_mat **left,
803 __isl_keep isl_mat **right, unsigned i, unsigned j)
805 *left = isl_mat_swap_rows(*left, i, j);
806 *right = isl_mat_swap_rows(*right, i, j);
808 if (!*left || !*right)
809 return isl_stat_error;
810 return isl_stat_ok;
813 static void inv_oppose(
814 struct isl_mat *left, struct isl_mat *right, unsigned row)
816 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
817 isl_seq_neg(right->row[row], right->row[row], right->n_col);
820 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
821 unsigned row, unsigned i, isl_int m)
823 isl_int_neg(m, m);
824 isl_seq_combine(left->row[i]+row,
825 left->ctx->one, left->row[i]+row,
826 m, left->row[row]+row,
827 left->n_col-row);
828 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
829 m, right->row[row], right->n_col);
832 /* Compute inv(left)*right
834 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
835 struct isl_mat *right)
837 int row;
838 isl_int a, b;
840 if (!left || !right)
841 goto error;
843 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
844 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
846 if (left->n_row == 0) {
847 isl_mat_free(left);
848 return right;
851 left = isl_mat_cow(left);
852 right = isl_mat_cow(right);
853 if (!left || !right)
854 goto error;
856 isl_int_init(a);
857 isl_int_init(b);
858 for (row = 0; row < left->n_row; ++row) {
859 int pivot, first, i, off;
860 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
861 if (pivot < 0) {
862 isl_int_clear(a);
863 isl_int_clear(b);
864 isl_assert(left->ctx, pivot >= 0, goto error);
866 pivot += row;
867 if (pivot != row)
868 if (inv_exchange(&left, &right, pivot, row) < 0)
869 goto error;
870 if (isl_int_is_neg(left->row[row][row]))
871 inv_oppose(left, right, row);
872 first = row+1;
873 while ((off = row_first_non_zero(left->row+first,
874 left->n_row-first, row)) != -1) {
875 first += off;
876 isl_int_fdiv_q(a, left->row[first][row],
877 left->row[row][row]);
878 inv_subtract(left, right, row, first, a);
879 if (!isl_int_is_zero(left->row[first][row])) {
880 if (inv_exchange(&left, &right, row, first) < 0)
881 goto error;
882 } else {
883 ++first;
886 for (i = 0; i < row; ++i) {
887 if (isl_int_is_zero(left->row[i][row]))
888 continue;
889 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
890 isl_int_divexact(b, left->row[i][row], a);
891 isl_int_divexact(a, left->row[row][row], a);
892 isl_int_neg(b, b);
893 isl_seq_combine(left->row[i] + i,
894 a, left->row[i] + i,
895 b, left->row[row] + i,
896 left->n_col - i);
897 isl_seq_combine(right->row[i], a, right->row[i],
898 b, right->row[row], right->n_col);
901 isl_int_clear(b);
903 isl_int_set(a, left->row[0][0]);
904 for (row = 1; row < left->n_row; ++row)
905 isl_int_lcm(a, a, left->row[row][row]);
906 if (isl_int_is_zero(a)){
907 isl_int_clear(a);
908 isl_assert(left->ctx, 0, goto error);
910 for (row = 0; row < left->n_row; ++row) {
911 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
912 if (isl_int_is_one(left->row[row][row]))
913 continue;
914 isl_seq_scale(right->row[row], right->row[row],
915 left->row[row][row], right->n_col);
917 isl_int_clear(a);
919 isl_mat_free(left);
920 return right;
921 error:
922 isl_mat_free(left);
923 isl_mat_free(right);
924 return NULL;
927 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
929 int i;
931 for (i = 0; i < mat->n_row; ++i)
932 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
935 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
936 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
938 int i;
939 isl_int tmp;
941 isl_int_init(tmp);
942 for (i = 0; i < mat->n_row; ++i) {
943 isl_int_mul(tmp, m1, mat->row[i][src1]);
944 isl_int_addmul(tmp, m2, mat->row[i][src2]);
945 isl_int_set(mat->row[i][dst], tmp);
947 isl_int_clear(tmp);
950 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
952 struct isl_mat *inv;
953 int row;
954 isl_int a, b;
956 mat = isl_mat_cow(mat);
957 if (!mat)
958 return NULL;
960 inv = isl_mat_identity(mat->ctx, mat->n_col);
961 inv = isl_mat_cow(inv);
962 if (!inv)
963 goto error;
965 isl_int_init(a);
966 isl_int_init(b);
967 for (row = 0; row < mat->n_row; ++row) {
968 int pivot, first, i, off;
969 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
970 if (pivot < 0) {
971 isl_int_clear(a);
972 isl_int_clear(b);
973 isl_assert(mat->ctx, pivot >= 0, goto error);
975 pivot += row;
976 if (pivot != row)
977 exchange(mat, &inv, NULL, row, pivot, row);
978 if (isl_int_is_neg(mat->row[row][row]))
979 oppose(mat, &inv, NULL, row, row);
980 first = row+1;
981 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
982 mat->n_col-first)) != -1) {
983 first += off;
984 isl_int_fdiv_q(a, mat->row[row][first],
985 mat->row[row][row]);
986 subtract(mat, &inv, NULL, row, row, first, a);
987 if (!isl_int_is_zero(mat->row[row][first]))
988 exchange(mat, &inv, NULL, row, row, first);
989 else
990 ++first;
992 for (i = 0; i < row; ++i) {
993 if (isl_int_is_zero(mat->row[row][i]))
994 continue;
995 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
996 isl_int_divexact(b, mat->row[row][i], a);
997 isl_int_divexact(a, mat->row[row][row], a);
998 isl_int_neg(a, a);
999 isl_mat_col_combine(mat, i, a, i, b, row);
1000 isl_mat_col_combine(inv, i, a, i, b, row);
1003 isl_int_clear(b);
1005 isl_int_set(a, mat->row[0][0]);
1006 for (row = 1; row < mat->n_row; ++row)
1007 isl_int_lcm(a, a, mat->row[row][row]);
1008 if (isl_int_is_zero(a)){
1009 isl_int_clear(a);
1010 goto error;
1012 for (row = 0; row < mat->n_row; ++row) {
1013 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
1014 if (isl_int_is_one(mat->row[row][row]))
1015 continue;
1016 isl_mat_col_scale(inv, row, mat->row[row][row]);
1018 isl_int_clear(a);
1020 isl_mat_free(mat);
1022 return inv;
1023 error:
1024 isl_mat_free(mat);
1025 isl_mat_free(inv);
1026 return NULL;
1029 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
1031 struct isl_mat *transpose = NULL;
1032 int i, j;
1034 if (!mat)
1035 return NULL;
1037 if (mat->n_col == mat->n_row) {
1038 mat = isl_mat_cow(mat);
1039 if (!mat)
1040 return NULL;
1041 for (i = 0; i < mat->n_row; ++i)
1042 for (j = i + 1; j < mat->n_col; ++j)
1043 isl_int_swap(mat->row[i][j], mat->row[j][i]);
1044 return mat;
1046 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1047 if (!transpose)
1048 goto error;
1049 for (i = 0; i < mat->n_row; ++i)
1050 for (j = 0; j < mat->n_col; ++j)
1051 isl_int_set(transpose->row[j][i], mat->row[i][j]);
1052 isl_mat_free(mat);
1053 return transpose;
1054 error:
1055 isl_mat_free(mat);
1056 return NULL;
1059 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
1061 int r;
1063 mat = isl_mat_cow(mat);
1064 if (!mat)
1065 return NULL;
1066 isl_assert(mat->ctx, i < mat->n_col, goto error);
1067 isl_assert(mat->ctx, j < mat->n_col, goto error);
1069 for (r = 0; r < mat->n_row; ++r)
1070 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1071 return mat;
1072 error:
1073 isl_mat_free(mat);
1074 return NULL;
1077 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
1079 isl_int *t;
1081 if (!mat)
1082 return NULL;
1083 mat = isl_mat_cow(mat);
1084 if (!mat)
1085 return NULL;
1086 t = mat->row[i];
1087 mat->row[i] = mat->row[j];
1088 mat->row[j] = t;
1089 return mat;
1092 /* Calculate the product of two matrices.
1094 * This function is optimized for operand matrices that contain many zeros and
1095 * skips multiplications where we know one of the operands is zero.
1097 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1098 __isl_take isl_mat *right)
1100 int i, j, k;
1101 struct isl_mat *prod;
1103 if (!left || !right)
1104 goto error;
1105 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1106 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1107 if (!prod)
1108 goto error;
1109 if (left->n_col == 0) {
1110 for (i = 0; i < prod->n_row; ++i)
1111 isl_seq_clr(prod->row[i], prod->n_col);
1112 isl_mat_free(left);
1113 isl_mat_free(right);
1114 return prod;
1116 for (i = 0; i < prod->n_row; ++i) {
1117 for (j = 0; j < prod->n_col; ++j)
1118 isl_int_mul(prod->row[i][j],
1119 left->row[i][0], right->row[0][j]);
1120 for (k = 1; k < left->n_col; ++k) {
1121 if (isl_int_is_zero(left->row[i][k]))
1122 continue;
1123 for (j = 0; j < prod->n_col; ++j)
1124 isl_int_addmul(prod->row[i][j],
1125 left->row[i][k], right->row[k][j]);
1128 isl_mat_free(left);
1129 isl_mat_free(right);
1130 return prod;
1131 error:
1132 isl_mat_free(left);
1133 isl_mat_free(right);
1134 return NULL;
1137 /* Replace the variables x in the rows q by x' given by x = M x',
1138 * with M the matrix mat.
1140 * If the number of new variables is greater than the original
1141 * number of variables, then the rows q have already been
1142 * preextended. If the new number is smaller, then the coefficients
1143 * of the divs, which are not changed, need to be shifted down.
1144 * The row q may be the equalities, the inequalities or the
1145 * div expressions. In the latter case, has_div is true and
1146 * we need to take into account the extra denominator column.
1148 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1149 unsigned n_div, int has_div, struct isl_mat *mat)
1151 int i;
1152 struct isl_mat *t;
1153 int e;
1155 if (mat->n_col >= mat->n_row)
1156 e = 0;
1157 else
1158 e = mat->n_row - mat->n_col;
1159 if (has_div)
1160 for (i = 0; i < n; ++i)
1161 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1162 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1163 t = isl_mat_product(t, mat);
1164 if (!t)
1165 return -1;
1166 for (i = 0; i < n; ++i) {
1167 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1168 isl_seq_cpy(q[i] + has_div + t->n_col,
1169 q[i] + has_div + t->n_col + e, n_div);
1170 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1172 isl_mat_free(t);
1173 return 0;
1176 /* Replace the variables x in bset by x' given by x = M x', with
1177 * M the matrix mat.
1179 * If there are fewer variables x' then there are x, then we perform
1180 * the transformation in place, which means that, in principle,
1181 * this frees up some extra variables as the number
1182 * of columns remains constant, but we would have to extend
1183 * the div array too as the number of rows in this array is assumed
1184 * to be equal to extra.
1186 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1187 struct isl_mat *mat)
1189 struct isl_ctx *ctx;
1191 if (!bset || !mat)
1192 goto error;
1194 ctx = bset->ctx;
1195 bset = isl_basic_set_cow(bset);
1196 if (!bset)
1197 goto error;
1199 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1200 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1201 isl_assert(ctx, mat->n_col > 0, goto error);
1203 if (mat->n_col > mat->n_row) {
1204 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1205 if (!bset)
1206 goto error;
1207 } else if (mat->n_col < mat->n_row) {
1208 bset->dim = isl_space_cow(bset->dim);
1209 if (!bset->dim)
1210 goto error;
1211 bset->dim->n_out -= mat->n_row - mat->n_col;
1214 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1215 isl_mat_copy(mat)) < 0)
1216 goto error;
1218 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1219 isl_mat_copy(mat)) < 0)
1220 goto error;
1222 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1223 goto error2;
1225 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1226 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1227 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1228 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1229 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1231 bset = isl_basic_set_simplify(bset);
1232 bset = isl_basic_set_finalize(bset);
1234 return bset;
1235 error:
1236 isl_mat_free(mat);
1237 error2:
1238 isl_basic_set_free(bset);
1239 return NULL;
1242 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1244 int i;
1246 set = isl_set_cow(set);
1247 if (!set)
1248 goto error;
1250 for (i = 0; i < set->n; ++i) {
1251 set->p[i] = isl_basic_set_preimage(set->p[i],
1252 isl_mat_copy(mat));
1253 if (!set->p[i])
1254 goto error;
1256 if (mat->n_col != mat->n_row) {
1257 set->dim = isl_space_cow(set->dim);
1258 if (!set->dim)
1259 goto error;
1260 set->dim->n_out += mat->n_col;
1261 set->dim->n_out -= mat->n_row;
1263 isl_mat_free(mat);
1264 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1265 return set;
1266 error:
1267 isl_set_free(set);
1268 isl_mat_free(mat);
1269 return NULL;
1272 /* Replace the variables x starting at pos in the rows q
1273 * by x' with x = M x' with M the matrix mat.
1274 * That is, replace the corresponding coefficients c by c M.
1276 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1277 unsigned pos, __isl_take isl_mat *mat)
1279 int i;
1280 isl_mat *t;
1282 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1283 t = isl_mat_product(t, mat);
1284 if (!t)
1285 return -1;
1286 for (i = 0; i < n; ++i)
1287 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1288 isl_mat_free(t);
1289 return 0;
1292 /* Replace the variables x of type "type" starting at "first" in "bmap"
1293 * by x' with x = M x' with M the matrix trans.
1294 * That is, replace the corresponding coefficients c by c M.
1296 * The transformation matrix should be a square matrix.
1298 __isl_give isl_basic_map *isl_basic_map_transform_dims(
1299 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
1300 __isl_take isl_mat *trans)
1302 isl_ctx *ctx;
1303 unsigned pos;
1305 bmap = isl_basic_map_cow(bmap);
1306 if (!bmap || !trans)
1307 goto error;
1309 ctx = isl_basic_map_get_ctx(bmap);
1310 if (trans->n_row != trans->n_col)
1311 isl_die(trans->ctx, isl_error_invalid,
1312 "expecting square transformation matrix", goto error);
1313 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
1314 isl_die(trans->ctx, isl_error_invalid,
1315 "oversized transformation matrix", goto error);
1317 pos = isl_basic_map_offset(bmap, type) + first;
1319 if (transform(ctx, bmap->eq, bmap->n_eq, pos, isl_mat_copy(trans)) < 0)
1320 goto error;
1321 if (transform(ctx, bmap->ineq, bmap->n_ineq, pos,
1322 isl_mat_copy(trans)) < 0)
1323 goto error;
1324 if (transform(ctx, bmap->div, bmap->n_div, 1 + pos,
1325 isl_mat_copy(trans)) < 0)
1326 goto error;
1328 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1329 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1331 isl_mat_free(trans);
1332 return bmap;
1333 error:
1334 isl_mat_free(trans);
1335 isl_basic_map_free(bmap);
1336 return NULL;
1339 /* Replace the variables x of type "type" starting at "first" in "bset"
1340 * by x' with x = M x' with M the matrix trans.
1341 * That is, replace the corresponding coefficients c by c M.
1343 * The transformation matrix should be a square matrix.
1345 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1346 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1347 __isl_take isl_mat *trans)
1349 return isl_basic_map_transform_dims(bset, type, first, trans);
1352 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1354 int i, j;
1356 if (!mat) {
1357 fprintf(out, "%*snull mat\n", indent, "");
1358 return;
1361 if (mat->n_row == 0)
1362 fprintf(out, "%*s[]\n", indent, "");
1364 for (i = 0; i < mat->n_row; ++i) {
1365 if (!i)
1366 fprintf(out, "%*s[[", indent, "");
1367 else
1368 fprintf(out, "%*s[", indent+1, "");
1369 for (j = 0; j < mat->n_col; ++j) {
1370 if (j)
1371 fprintf(out, ",");
1372 isl_int_print(out, mat->row[i][j], 0);
1374 if (i == mat->n_row-1)
1375 fprintf(out, "]]\n");
1376 else
1377 fprintf(out, "]\n");
1381 void isl_mat_dump(__isl_keep isl_mat *mat)
1383 isl_mat_print_internal(mat, stderr, 0);
1386 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1388 int r;
1390 if (n == 0)
1391 return mat;
1393 mat = isl_mat_cow(mat);
1394 if (!mat)
1395 return NULL;
1397 if (col != mat->n_col-n) {
1398 for (r = 0; r < mat->n_row; ++r)
1399 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1400 mat->n_col - col - n);
1402 mat->n_col -= n;
1403 return mat;
1406 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1408 int r;
1410 mat = isl_mat_cow(mat);
1411 if (!mat)
1412 return NULL;
1414 for (r = row; r+n < mat->n_row; ++r)
1415 mat->row[r] = mat->row[r+n];
1417 mat->n_row -= n;
1418 return mat;
1421 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1422 unsigned col, unsigned n)
1424 isl_mat *ext;
1426 if (!mat)
1427 return NULL;
1428 if (n == 0)
1429 return mat;
1431 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1432 if (!ext)
1433 goto error;
1435 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1436 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1437 col + n, col, mat->n_col - col);
1439 isl_mat_free(mat);
1440 return ext;
1441 error:
1442 isl_mat_free(mat);
1443 return NULL;
1446 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1447 unsigned first, unsigned n)
1449 int i;
1451 if (!mat)
1452 return NULL;
1453 mat = isl_mat_insert_cols(mat, first, n);
1454 if (!mat)
1455 return NULL;
1457 for (i = 0; i < mat->n_row; ++i)
1458 isl_seq_clr(mat->row[i] + first, n);
1460 return mat;
1463 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1465 if (!mat)
1466 return NULL;
1468 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1471 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1472 unsigned row, unsigned n)
1474 isl_mat *ext;
1476 if (!mat)
1477 return NULL;
1478 if (n == 0)
1479 return mat;
1481 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1482 if (!ext)
1483 goto error;
1485 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1486 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1487 mat->n_row - row, 0, 0, mat->n_col);
1489 isl_mat_free(mat);
1490 return ext;
1491 error:
1492 isl_mat_free(mat);
1493 return NULL;
1496 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1498 if (!mat)
1499 return NULL;
1501 return isl_mat_insert_rows(mat, mat->n_row, n);
1504 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1505 unsigned row, unsigned n)
1507 int i;
1509 mat = isl_mat_insert_rows(mat, row, n);
1510 if (!mat)
1511 return NULL;
1513 for (i = 0; i < n; ++i)
1514 isl_seq_clr(mat->row[row + i], mat->n_col);
1516 return mat;
1519 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1521 if (!mat)
1522 return NULL;
1524 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1527 void isl_mat_col_submul(struct isl_mat *mat,
1528 int dst_col, isl_int f, int src_col)
1530 int i;
1532 for (i = 0; i < mat->n_row; ++i)
1533 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1536 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1538 int i;
1540 if (!mat)
1541 return;
1543 for (i = 0; i < mat->n_row; ++i)
1544 isl_int_add(mat->row[i][dst_col],
1545 mat->row[i][dst_col], mat->row[i][src_col]);
1548 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1550 int i;
1552 for (i = 0; i < mat->n_row; ++i)
1553 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1556 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1557 * return the result.
1559 __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1560 isl_int f, int src_col)
1562 int i;
1564 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1565 return isl_mat_free(mat);
1567 for (i = 0; i < mat->n_row; ++i) {
1568 if (isl_int_is_zero(mat->row[i][src_col]))
1569 continue;
1570 mat = isl_mat_cow(mat);
1571 if (!mat)
1572 return NULL;
1573 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1576 return mat;
1579 /* Negate column "col" of "mat" and return the result.
1581 __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1583 int i;
1585 if (check_col(mat, col) < 0)
1586 return isl_mat_free(mat);
1588 for (i = 0; i < mat->n_row; ++i) {
1589 if (isl_int_is_zero(mat->row[i][col]))
1590 continue;
1591 mat = isl_mat_cow(mat);
1592 if (!mat)
1593 return NULL;
1594 isl_int_neg(mat->row[i][col], mat->row[i][col]);
1597 return mat;
1600 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1602 int r;
1603 struct isl_mat *H = NULL, *Q = NULL;
1605 if (!M)
1606 return NULL;
1608 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1609 M->n_row = row;
1610 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1611 M->n_row = M->n_col;
1612 if (!H)
1613 goto error;
1614 for (r = 0; r < row; ++r)
1615 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1616 for (r = row; r < M->n_row; ++r)
1617 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1618 isl_mat_free(H);
1619 isl_mat_free(Q);
1620 return M;
1621 error:
1622 isl_mat_free(H);
1623 isl_mat_free(Q);
1624 isl_mat_free(M);
1625 return NULL;
1628 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1629 __isl_take isl_mat *bot)
1631 struct isl_mat *mat;
1633 if (!top || !bot)
1634 goto error;
1636 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1637 if (top->n_row == 0) {
1638 isl_mat_free(top);
1639 return bot;
1641 if (bot->n_row == 0) {
1642 isl_mat_free(bot);
1643 return top;
1646 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1647 if (!mat)
1648 goto error;
1649 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1650 0, 0, mat->n_col);
1651 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1652 0, 0, mat->n_col);
1653 isl_mat_free(top);
1654 isl_mat_free(bot);
1655 return mat;
1656 error:
1657 isl_mat_free(top);
1658 isl_mat_free(bot);
1659 return NULL;
1662 isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1664 int i;
1666 if (!mat1 || !mat2)
1667 return isl_bool_error;
1669 if (mat1->n_row != mat2->n_row)
1670 return isl_bool_false;
1672 if (mat1->n_col != mat2->n_col)
1673 return isl_bool_false;
1675 for (i = 0; i < mat1->n_row; ++i)
1676 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1677 return isl_bool_false;
1679 return isl_bool_true;
1682 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1684 struct isl_mat *mat;
1686 if (!vec)
1687 return NULL;
1688 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1689 if (!mat)
1690 goto error;
1692 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1694 isl_vec_free(vec);
1695 return mat;
1696 error:
1697 isl_vec_free(vec);
1698 return NULL;
1701 /* Return a copy of row "row" of "mat" as an isl_vec.
1703 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1705 isl_vec *v;
1707 if (!mat)
1708 return NULL;
1709 if (row >= mat->n_row)
1710 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1711 return NULL);
1713 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1714 if (!v)
1715 return NULL;
1716 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1718 return v;
1721 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1722 __isl_take isl_vec *bot)
1724 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1727 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1728 unsigned dst_col, unsigned src_col, unsigned n)
1730 isl_mat *res;
1732 if (!mat)
1733 return NULL;
1734 if (n == 0 || dst_col == src_col)
1735 return mat;
1737 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1738 if (!res)
1739 goto error;
1741 if (dst_col < src_col) {
1742 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1743 0, 0, dst_col);
1744 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1745 dst_col, src_col, n);
1746 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1747 dst_col + n, dst_col, src_col - dst_col);
1748 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1749 src_col + n, src_col + n,
1750 res->n_col - src_col - n);
1751 } else {
1752 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1753 0, 0, src_col);
1754 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1755 src_col, src_col + n, dst_col - src_col);
1756 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1757 dst_col, src_col, n);
1758 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1759 dst_col + n, dst_col + n,
1760 res->n_col - dst_col - n);
1762 isl_mat_free(mat);
1764 return res;
1765 error:
1766 isl_mat_free(mat);
1767 return NULL;
1770 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1771 * Return isl_stat_ok on success and isl_stat_error on failure.
1773 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1775 if (!mat)
1776 return isl_stat_error;
1778 if (row < 0 || row >= mat->n_row)
1779 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
1780 "row out of range", return isl_stat_error);
1781 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1783 return isl_stat_ok;
1786 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1788 int i;
1789 isl_int g;
1791 isl_int_set_si(*gcd, 0);
1792 if (!mat)
1793 return;
1795 isl_int_init(g);
1796 for (i = 0; i < mat->n_row; ++i) {
1797 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1798 isl_int_gcd(*gcd, *gcd, g);
1800 isl_int_clear(g);
1803 /* Return the result of scaling "mat" by a factor of "m".
1805 __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1807 int i;
1809 if (isl_int_is_one(m))
1810 return mat;
1812 mat = isl_mat_cow(mat);
1813 if (!mat)
1814 return NULL;
1816 for (i = 0; i < mat->n_row; ++i)
1817 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1819 return mat;
1822 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1824 int i;
1826 if (isl_int_is_one(m))
1827 return mat;
1829 mat = isl_mat_cow(mat);
1830 if (!mat)
1831 return NULL;
1833 for (i = 0; i < mat->n_row; ++i)
1834 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1836 return mat;
1839 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1840 isl_int m)
1842 if (isl_int_is_one(m))
1843 return mat;
1845 mat = isl_mat_cow(mat);
1846 if (!mat)
1847 return NULL;
1849 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1851 return mat;
1854 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1856 isl_int gcd;
1858 if (!mat)
1859 return NULL;
1861 isl_int_init(gcd);
1862 isl_mat_gcd(mat, &gcd);
1863 mat = isl_mat_scale_down(mat, gcd);
1864 isl_int_clear(gcd);
1866 return mat;
1869 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1871 mat = isl_mat_cow(mat);
1872 if (!mat)
1873 return NULL;
1875 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1877 return mat;
1880 /* Number of initial non-zero columns.
1882 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1884 int i;
1886 if (!mat)
1887 return -1;
1889 for (i = 0; i < mat->n_col; ++i)
1890 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1891 break;
1893 return i;