privately add isl_mat_zero
[isl.git] / isl_mat.c
blobcbe724f1434f4859f486a461d6f7154251187311
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 void inv_exchange(struct isl_mat *left, struct isl_mat *right,
803 unsigned i, unsigned j)
805 left = isl_mat_swap_rows(left, i, j);
806 right = isl_mat_swap_rows(right, i, j);
809 static void inv_oppose(
810 struct isl_mat *left, struct isl_mat *right, unsigned row)
812 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
813 isl_seq_neg(right->row[row], right->row[row], right->n_col);
816 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
817 unsigned row, unsigned i, isl_int m)
819 isl_int_neg(m, m);
820 isl_seq_combine(left->row[i]+row,
821 left->ctx->one, left->row[i]+row,
822 m, left->row[row]+row,
823 left->n_col-row);
824 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
825 m, right->row[row], right->n_col);
828 /* Compute inv(left)*right
830 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
831 struct isl_mat *right)
833 int row;
834 isl_int a, b;
836 if (!left || !right)
837 goto error;
839 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
840 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
842 if (left->n_row == 0) {
843 isl_mat_free(left);
844 return right;
847 left = isl_mat_cow(left);
848 right = isl_mat_cow(right);
849 if (!left || !right)
850 goto error;
852 isl_int_init(a);
853 isl_int_init(b);
854 for (row = 0; row < left->n_row; ++row) {
855 int pivot, first, i, off;
856 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
857 if (pivot < 0) {
858 isl_int_clear(a);
859 isl_int_clear(b);
860 isl_assert(left->ctx, pivot >= 0, goto error);
862 pivot += row;
863 if (pivot != row)
864 inv_exchange(left, right, pivot, row);
865 if (isl_int_is_neg(left->row[row][row]))
866 inv_oppose(left, right, row);
867 first = row+1;
868 while ((off = row_first_non_zero(left->row+first,
869 left->n_row-first, row)) != -1) {
870 first += off;
871 isl_int_fdiv_q(a, left->row[first][row],
872 left->row[row][row]);
873 inv_subtract(left, right, row, first, a);
874 if (!isl_int_is_zero(left->row[first][row]))
875 inv_exchange(left, right, row, first);
876 else
877 ++first;
879 for (i = 0; i < row; ++i) {
880 if (isl_int_is_zero(left->row[i][row]))
881 continue;
882 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
883 isl_int_divexact(b, left->row[i][row], a);
884 isl_int_divexact(a, left->row[row][row], a);
885 isl_int_neg(b, b);
886 isl_seq_combine(left->row[i] + i,
887 a, left->row[i] + i,
888 b, left->row[row] + i,
889 left->n_col - i);
890 isl_seq_combine(right->row[i], a, right->row[i],
891 b, right->row[row], right->n_col);
894 isl_int_clear(b);
896 isl_int_set(a, left->row[0][0]);
897 for (row = 1; row < left->n_row; ++row)
898 isl_int_lcm(a, a, left->row[row][row]);
899 if (isl_int_is_zero(a)){
900 isl_int_clear(a);
901 isl_assert(left->ctx, 0, goto error);
903 for (row = 0; row < left->n_row; ++row) {
904 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
905 if (isl_int_is_one(left->row[row][row]))
906 continue;
907 isl_seq_scale(right->row[row], right->row[row],
908 left->row[row][row], right->n_col);
910 isl_int_clear(a);
912 isl_mat_free(left);
913 return right;
914 error:
915 isl_mat_free(left);
916 isl_mat_free(right);
917 return NULL;
920 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
922 int i;
924 for (i = 0; i < mat->n_row; ++i)
925 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
928 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
929 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
931 int i;
932 isl_int tmp;
934 isl_int_init(tmp);
935 for (i = 0; i < mat->n_row; ++i) {
936 isl_int_mul(tmp, m1, mat->row[i][src1]);
937 isl_int_addmul(tmp, m2, mat->row[i][src2]);
938 isl_int_set(mat->row[i][dst], tmp);
940 isl_int_clear(tmp);
943 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
945 struct isl_mat *inv;
946 int row;
947 isl_int a, b;
949 mat = isl_mat_cow(mat);
950 if (!mat)
951 return NULL;
953 inv = isl_mat_identity(mat->ctx, mat->n_col);
954 inv = isl_mat_cow(inv);
955 if (!inv)
956 goto error;
958 isl_int_init(a);
959 isl_int_init(b);
960 for (row = 0; row < mat->n_row; ++row) {
961 int pivot, first, i, off;
962 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
963 if (pivot < 0) {
964 isl_int_clear(a);
965 isl_int_clear(b);
966 isl_assert(mat->ctx, pivot >= 0, goto error);
968 pivot += row;
969 if (pivot != row)
970 exchange(mat, &inv, NULL, row, pivot, row);
971 if (isl_int_is_neg(mat->row[row][row]))
972 oppose(mat, &inv, NULL, row, row);
973 first = row+1;
974 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
975 mat->n_col-first)) != -1) {
976 first += off;
977 isl_int_fdiv_q(a, mat->row[row][first],
978 mat->row[row][row]);
979 subtract(mat, &inv, NULL, row, row, first, a);
980 if (!isl_int_is_zero(mat->row[row][first]))
981 exchange(mat, &inv, NULL, row, row, first);
982 else
983 ++first;
985 for (i = 0; i < row; ++i) {
986 if (isl_int_is_zero(mat->row[row][i]))
987 continue;
988 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
989 isl_int_divexact(b, mat->row[row][i], a);
990 isl_int_divexact(a, mat->row[row][row], a);
991 isl_int_neg(a, a);
992 isl_mat_col_combine(mat, i, a, i, b, row);
993 isl_mat_col_combine(inv, i, a, i, b, row);
996 isl_int_clear(b);
998 isl_int_set(a, mat->row[0][0]);
999 for (row = 1; row < mat->n_row; ++row)
1000 isl_int_lcm(a, a, mat->row[row][row]);
1001 if (isl_int_is_zero(a)){
1002 isl_int_clear(a);
1003 goto error;
1005 for (row = 0; row < mat->n_row; ++row) {
1006 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
1007 if (isl_int_is_one(mat->row[row][row]))
1008 continue;
1009 isl_mat_col_scale(inv, row, mat->row[row][row]);
1011 isl_int_clear(a);
1013 isl_mat_free(mat);
1015 return inv;
1016 error:
1017 isl_mat_free(mat);
1018 isl_mat_free(inv);
1019 return NULL;
1022 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
1024 struct isl_mat *transpose = NULL;
1025 int i, j;
1027 if (!mat)
1028 return NULL;
1030 if (mat->n_col == mat->n_row) {
1031 mat = isl_mat_cow(mat);
1032 if (!mat)
1033 return NULL;
1034 for (i = 0; i < mat->n_row; ++i)
1035 for (j = i + 1; j < mat->n_col; ++j)
1036 isl_int_swap(mat->row[i][j], mat->row[j][i]);
1037 return mat;
1039 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1040 if (!transpose)
1041 goto error;
1042 for (i = 0; i < mat->n_row; ++i)
1043 for (j = 0; j < mat->n_col; ++j)
1044 isl_int_set(transpose->row[j][i], mat->row[i][j]);
1045 isl_mat_free(mat);
1046 return transpose;
1047 error:
1048 isl_mat_free(mat);
1049 return NULL;
1052 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
1054 int r;
1056 mat = isl_mat_cow(mat);
1057 if (!mat)
1058 return NULL;
1059 isl_assert(mat->ctx, i < mat->n_col, goto error);
1060 isl_assert(mat->ctx, j < mat->n_col, goto error);
1062 for (r = 0; r < mat->n_row; ++r)
1063 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1064 return mat;
1065 error:
1066 isl_mat_free(mat);
1067 return NULL;
1070 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
1072 isl_int *t;
1074 if (!mat)
1075 return NULL;
1076 mat = isl_mat_cow(mat);
1077 if (!mat)
1078 return NULL;
1079 t = mat->row[i];
1080 mat->row[i] = mat->row[j];
1081 mat->row[j] = t;
1082 return mat;
1085 /* Calculate the product of two matrices.
1087 * This function is optimized for operand matrices that contain many zeros and
1088 * skips multiplications where we know one of the operands is zero.
1090 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1091 __isl_take isl_mat *right)
1093 int i, j, k;
1094 struct isl_mat *prod;
1096 if (!left || !right)
1097 goto error;
1098 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1099 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1100 if (!prod)
1101 goto error;
1102 if (left->n_col == 0) {
1103 for (i = 0; i < prod->n_row; ++i)
1104 isl_seq_clr(prod->row[i], prod->n_col);
1105 isl_mat_free(left);
1106 isl_mat_free(right);
1107 return prod;
1109 for (i = 0; i < prod->n_row; ++i) {
1110 for (j = 0; j < prod->n_col; ++j)
1111 isl_int_mul(prod->row[i][j],
1112 left->row[i][0], right->row[0][j]);
1113 for (k = 1; k < left->n_col; ++k) {
1114 if (isl_int_is_zero(left->row[i][k]))
1115 continue;
1116 for (j = 0; j < prod->n_col; ++j)
1117 isl_int_addmul(prod->row[i][j],
1118 left->row[i][k], right->row[k][j]);
1121 isl_mat_free(left);
1122 isl_mat_free(right);
1123 return prod;
1124 error:
1125 isl_mat_free(left);
1126 isl_mat_free(right);
1127 return NULL;
1130 /* Replace the variables x in the rows q by x' given by x = M x',
1131 * with M the matrix mat.
1133 * If the number of new variables is greater than the original
1134 * number of variables, then the rows q have already been
1135 * preextended. If the new number is smaller, then the coefficients
1136 * of the divs, which are not changed, need to be shifted down.
1137 * The row q may be the equalities, the inequalities or the
1138 * div expressions. In the latter case, has_div is true and
1139 * we need to take into account the extra denominator column.
1141 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1142 unsigned n_div, int has_div, struct isl_mat *mat)
1144 int i;
1145 struct isl_mat *t;
1146 int e;
1148 if (mat->n_col >= mat->n_row)
1149 e = 0;
1150 else
1151 e = mat->n_row - mat->n_col;
1152 if (has_div)
1153 for (i = 0; i < n; ++i)
1154 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1155 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1156 t = isl_mat_product(t, mat);
1157 if (!t)
1158 return -1;
1159 for (i = 0; i < n; ++i) {
1160 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1161 isl_seq_cpy(q[i] + has_div + t->n_col,
1162 q[i] + has_div + t->n_col + e, n_div);
1163 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1165 isl_mat_free(t);
1166 return 0;
1169 /* Replace the variables x in bset by x' given by x = M x', with
1170 * M the matrix mat.
1172 * If there are fewer variables x' then there are x, then we perform
1173 * the transformation in place, which means that, in principle,
1174 * this frees up some extra variables as the number
1175 * of columns remains constant, but we would have to extend
1176 * the div array too as the number of rows in this array is assumed
1177 * to be equal to extra.
1179 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1180 struct isl_mat *mat)
1182 struct isl_ctx *ctx;
1184 if (!bset || !mat)
1185 goto error;
1187 ctx = bset->ctx;
1188 bset = isl_basic_set_cow(bset);
1189 if (!bset)
1190 goto error;
1192 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1193 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1194 isl_assert(ctx, mat->n_col > 0, goto error);
1196 if (mat->n_col > mat->n_row) {
1197 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1198 if (!bset)
1199 goto error;
1200 } else if (mat->n_col < mat->n_row) {
1201 bset->dim = isl_space_cow(bset->dim);
1202 if (!bset->dim)
1203 goto error;
1204 bset->dim->n_out -= mat->n_row - mat->n_col;
1207 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1208 isl_mat_copy(mat)) < 0)
1209 goto error;
1211 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1212 isl_mat_copy(mat)) < 0)
1213 goto error;
1215 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1216 goto error2;
1218 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1219 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1220 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1221 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1222 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1224 bset = isl_basic_set_simplify(bset);
1225 bset = isl_basic_set_finalize(bset);
1227 return bset;
1228 error:
1229 isl_mat_free(mat);
1230 error2:
1231 isl_basic_set_free(bset);
1232 return NULL;
1235 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1237 int i;
1239 set = isl_set_cow(set);
1240 if (!set)
1241 goto error;
1243 for (i = 0; i < set->n; ++i) {
1244 set->p[i] = isl_basic_set_preimage(set->p[i],
1245 isl_mat_copy(mat));
1246 if (!set->p[i])
1247 goto error;
1249 if (mat->n_col != mat->n_row) {
1250 set->dim = isl_space_cow(set->dim);
1251 if (!set->dim)
1252 goto error;
1253 set->dim->n_out += mat->n_col;
1254 set->dim->n_out -= mat->n_row;
1256 isl_mat_free(mat);
1257 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1258 return set;
1259 error:
1260 isl_set_free(set);
1261 isl_mat_free(mat);
1262 return NULL;
1265 /* Replace the variables x starting at pos in the rows q
1266 * by x' with x = M x' with M the matrix mat.
1267 * That is, replace the corresponding coefficients c by c M.
1269 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1270 unsigned pos, __isl_take isl_mat *mat)
1272 int i;
1273 isl_mat *t;
1275 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1276 t = isl_mat_product(t, mat);
1277 if (!t)
1278 return -1;
1279 for (i = 0; i < n; ++i)
1280 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1281 isl_mat_free(t);
1282 return 0;
1285 /* Replace the variables x of type "type" starting at "first" in "bmap"
1286 * by x' with x = M x' with M the matrix trans.
1287 * That is, replace the corresponding coefficients c by c M.
1289 * The transformation matrix should be a square matrix.
1291 __isl_give isl_basic_map *isl_basic_map_transform_dims(
1292 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
1293 __isl_take isl_mat *trans)
1295 isl_ctx *ctx;
1296 unsigned pos;
1298 bmap = isl_basic_map_cow(bmap);
1299 if (!bmap || !trans)
1300 goto error;
1302 ctx = isl_basic_map_get_ctx(bmap);
1303 if (trans->n_row != trans->n_col)
1304 isl_die(trans->ctx, isl_error_invalid,
1305 "expecting square transformation matrix", goto error);
1306 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
1307 isl_die(trans->ctx, isl_error_invalid,
1308 "oversized transformation matrix", goto error);
1310 pos = isl_basic_map_offset(bmap, type) + first;
1312 if (transform(ctx, bmap->eq, bmap->n_eq, pos, isl_mat_copy(trans)) < 0)
1313 goto error;
1314 if (transform(ctx, bmap->ineq, bmap->n_ineq, pos,
1315 isl_mat_copy(trans)) < 0)
1316 goto error;
1317 if (transform(ctx, bmap->div, bmap->n_div, 1 + pos,
1318 isl_mat_copy(trans)) < 0)
1319 goto error;
1321 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1322 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1324 isl_mat_free(trans);
1325 return bmap;
1326 error:
1327 isl_mat_free(trans);
1328 isl_basic_map_free(bmap);
1329 return NULL;
1332 /* Replace the variables x of type "type" starting at "first" in "bset"
1333 * by x' with x = M x' with M the matrix trans.
1334 * That is, replace the corresponding coefficients c by c M.
1336 * The transformation matrix should be a square matrix.
1338 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1339 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1340 __isl_take isl_mat *trans)
1342 return isl_basic_map_transform_dims(bset, type, first, trans);
1345 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1347 int i, j;
1349 if (!mat) {
1350 fprintf(out, "%*snull mat\n", indent, "");
1351 return;
1354 if (mat->n_row == 0)
1355 fprintf(out, "%*s[]\n", indent, "");
1357 for (i = 0; i < mat->n_row; ++i) {
1358 if (!i)
1359 fprintf(out, "%*s[[", indent, "");
1360 else
1361 fprintf(out, "%*s[", indent+1, "");
1362 for (j = 0; j < mat->n_col; ++j) {
1363 if (j)
1364 fprintf(out, ",");
1365 isl_int_print(out, mat->row[i][j], 0);
1367 if (i == mat->n_row-1)
1368 fprintf(out, "]]\n");
1369 else
1370 fprintf(out, "]\n");
1374 void isl_mat_dump(__isl_keep isl_mat *mat)
1376 isl_mat_print_internal(mat, stderr, 0);
1379 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1381 int r;
1383 if (n == 0)
1384 return mat;
1386 mat = isl_mat_cow(mat);
1387 if (!mat)
1388 return NULL;
1390 if (col != mat->n_col-n) {
1391 for (r = 0; r < mat->n_row; ++r)
1392 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1393 mat->n_col - col - n);
1395 mat->n_col -= n;
1396 return mat;
1399 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1401 int r;
1403 mat = isl_mat_cow(mat);
1404 if (!mat)
1405 return NULL;
1407 for (r = row; r+n < mat->n_row; ++r)
1408 mat->row[r] = mat->row[r+n];
1410 mat->n_row -= n;
1411 return mat;
1414 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1415 unsigned col, unsigned n)
1417 isl_mat *ext;
1419 if (!mat)
1420 return NULL;
1421 if (n == 0)
1422 return mat;
1424 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1425 if (!ext)
1426 goto error;
1428 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1429 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1430 col + n, col, mat->n_col - col);
1432 isl_mat_free(mat);
1433 return ext;
1434 error:
1435 isl_mat_free(mat);
1436 return NULL;
1439 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1440 unsigned first, unsigned n)
1442 int i;
1444 if (!mat)
1445 return NULL;
1446 mat = isl_mat_insert_cols(mat, first, n);
1447 if (!mat)
1448 return NULL;
1450 for (i = 0; i < mat->n_row; ++i)
1451 isl_seq_clr(mat->row[i] + first, n);
1453 return mat;
1456 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1458 if (!mat)
1459 return NULL;
1461 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1464 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1465 unsigned row, unsigned n)
1467 isl_mat *ext;
1469 if (!mat)
1470 return NULL;
1471 if (n == 0)
1472 return mat;
1474 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1475 if (!ext)
1476 goto error;
1478 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1479 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1480 mat->n_row - row, 0, 0, mat->n_col);
1482 isl_mat_free(mat);
1483 return ext;
1484 error:
1485 isl_mat_free(mat);
1486 return NULL;
1489 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1491 if (!mat)
1492 return NULL;
1494 return isl_mat_insert_rows(mat, mat->n_row, n);
1497 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1498 unsigned row, unsigned n)
1500 int i;
1502 mat = isl_mat_insert_rows(mat, row, n);
1503 if (!mat)
1504 return NULL;
1506 for (i = 0; i < n; ++i)
1507 isl_seq_clr(mat->row[row + i], mat->n_col);
1509 return mat;
1512 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1514 if (!mat)
1515 return NULL;
1517 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1520 void isl_mat_col_submul(struct isl_mat *mat,
1521 int dst_col, isl_int f, int src_col)
1523 int i;
1525 for (i = 0; i < mat->n_row; ++i)
1526 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1529 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1531 int i;
1533 if (!mat)
1534 return;
1536 for (i = 0; i < mat->n_row; ++i)
1537 isl_int_add(mat->row[i][dst_col],
1538 mat->row[i][dst_col], mat->row[i][src_col]);
1541 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1543 int i;
1545 for (i = 0; i < mat->n_row; ++i)
1546 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1549 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1550 * return the result.
1552 __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1553 isl_int f, int src_col)
1555 int i;
1557 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1558 return isl_mat_free(mat);
1560 for (i = 0; i < mat->n_row; ++i) {
1561 if (isl_int_is_zero(mat->row[i][src_col]))
1562 continue;
1563 mat = isl_mat_cow(mat);
1564 if (!mat)
1565 return NULL;
1566 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1569 return mat;
1572 /* Negate column "col" of "mat" and return the result.
1574 __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1576 int i;
1578 if (check_col(mat, col) < 0)
1579 return isl_mat_free(mat);
1581 for (i = 0; i < mat->n_row; ++i) {
1582 if (isl_int_is_zero(mat->row[i][col]))
1583 continue;
1584 mat = isl_mat_cow(mat);
1585 if (!mat)
1586 return NULL;
1587 isl_int_neg(mat->row[i][col], mat->row[i][col]);
1590 return mat;
1593 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1595 int r;
1596 struct isl_mat *H = NULL, *Q = NULL;
1598 if (!M)
1599 return NULL;
1601 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1602 M->n_row = row;
1603 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1604 M->n_row = M->n_col;
1605 if (!H)
1606 goto error;
1607 for (r = 0; r < row; ++r)
1608 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1609 for (r = row; r < M->n_row; ++r)
1610 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1611 isl_mat_free(H);
1612 isl_mat_free(Q);
1613 return M;
1614 error:
1615 isl_mat_free(H);
1616 isl_mat_free(Q);
1617 isl_mat_free(M);
1618 return NULL;
1621 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1622 __isl_take isl_mat *bot)
1624 struct isl_mat *mat;
1626 if (!top || !bot)
1627 goto error;
1629 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1630 if (top->n_row == 0) {
1631 isl_mat_free(top);
1632 return bot;
1634 if (bot->n_row == 0) {
1635 isl_mat_free(bot);
1636 return top;
1639 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1640 if (!mat)
1641 goto error;
1642 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1643 0, 0, mat->n_col);
1644 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1645 0, 0, mat->n_col);
1646 isl_mat_free(top);
1647 isl_mat_free(bot);
1648 return mat;
1649 error:
1650 isl_mat_free(top);
1651 isl_mat_free(bot);
1652 return NULL;
1655 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1657 int i;
1659 if (!mat1 || !mat2)
1660 return -1;
1662 if (mat1->n_row != mat2->n_row)
1663 return 0;
1665 if (mat1->n_col != mat2->n_col)
1666 return 0;
1668 for (i = 0; i < mat1->n_row; ++i)
1669 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1670 return 0;
1672 return 1;
1675 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1677 struct isl_mat *mat;
1679 if (!vec)
1680 return NULL;
1681 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1682 if (!mat)
1683 goto error;
1685 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1687 isl_vec_free(vec);
1688 return mat;
1689 error:
1690 isl_vec_free(vec);
1691 return NULL;
1694 /* Return a copy of row "row" of "mat" as an isl_vec.
1696 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1698 isl_vec *v;
1700 if (!mat)
1701 return NULL;
1702 if (row >= mat->n_row)
1703 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1704 return NULL);
1706 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1707 if (!v)
1708 return NULL;
1709 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1711 return v;
1714 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1715 __isl_take isl_vec *bot)
1717 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1720 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1721 unsigned dst_col, unsigned src_col, unsigned n)
1723 isl_mat *res;
1725 if (!mat)
1726 return NULL;
1727 if (n == 0 || dst_col == src_col)
1728 return mat;
1730 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1731 if (!res)
1732 goto error;
1734 if (dst_col < src_col) {
1735 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1736 0, 0, dst_col);
1737 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1738 dst_col, src_col, n);
1739 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1740 dst_col + n, dst_col, src_col - dst_col);
1741 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1742 src_col + n, src_col + n,
1743 res->n_col - src_col - n);
1744 } else {
1745 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1746 0, 0, src_col);
1747 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1748 src_col, src_col + n, dst_col - src_col);
1749 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1750 dst_col, src_col, n);
1751 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1752 dst_col + n, dst_col + n,
1753 res->n_col - dst_col - n);
1755 isl_mat_free(mat);
1757 return res;
1758 error:
1759 isl_mat_free(mat);
1760 return NULL;
1763 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1764 * Return isl_stat_ok on success and isl_stat_error on failure.
1766 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1768 if (!mat)
1769 return isl_stat_error;
1771 if (row < 0 || row >= mat->n_row)
1772 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
1773 "row out of range", return isl_stat_error);
1774 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1776 return isl_stat_ok;
1779 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1781 int i;
1782 isl_int g;
1784 isl_int_set_si(*gcd, 0);
1785 if (!mat)
1786 return;
1788 isl_int_init(g);
1789 for (i = 0; i < mat->n_row; ++i) {
1790 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1791 isl_int_gcd(*gcd, *gcd, g);
1793 isl_int_clear(g);
1796 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1798 int i;
1800 if (isl_int_is_one(m))
1801 return mat;
1803 mat = isl_mat_cow(mat);
1804 if (!mat)
1805 return NULL;
1807 for (i = 0; i < mat->n_row; ++i)
1808 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1810 return mat;
1813 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1814 isl_int m)
1816 if (isl_int_is_one(m))
1817 return mat;
1819 mat = isl_mat_cow(mat);
1820 if (!mat)
1821 return NULL;
1823 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1825 return mat;
1828 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1830 isl_int gcd;
1832 if (!mat)
1833 return NULL;
1835 isl_int_init(gcd);
1836 isl_mat_gcd(mat, &gcd);
1837 mat = isl_mat_scale_down(mat, gcd);
1838 isl_int_clear(gcd);
1840 return mat;
1843 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1845 mat = isl_mat_cow(mat);
1846 if (!mat)
1847 return NULL;
1849 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1851 return mat;
1854 /* Number of initial non-zero columns.
1856 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1858 int i;
1860 if (!mat)
1861 return -1;
1863 for (i = 0; i < mat->n_col; ++i)
1864 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1865 break;
1867 return i;