rename isl_ast_graft_list_sort to isl_ast_graft_list_sort_guard
[isl.git] / isl_mat.c
blob2848e0a5e99b9bbb0ba75bf571ea94674d9b535c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl/space.h>
12 #include <isl/seq.h>
13 #include <isl_mat_private.h>
14 #include "isl_map_private.h"
15 #include <isl_space_private.h>
17 isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
19 return mat ? mat->ctx : NULL;
22 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
23 unsigned n_row, unsigned n_col)
25 int i;
26 struct isl_mat *mat;
28 mat = isl_alloc_type(ctx, struct isl_mat);
29 if (!mat)
30 return NULL;
32 mat->row = NULL;
33 mat->block = isl_blk_alloc(ctx, n_row * n_col);
34 if (isl_blk_is_error(mat->block))
35 goto error;
36 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
37 if (!mat->row)
38 goto error;
40 for (i = 0; i < n_row; ++i)
41 mat->row[i] = mat->block.data + i * n_col;
43 mat->ctx = ctx;
44 isl_ctx_ref(ctx);
45 mat->ref = 1;
46 mat->n_row = n_row;
47 mat->n_col = n_col;
48 mat->max_col = n_col;
49 mat->flags = 0;
51 return mat;
52 error:
53 isl_blk_free(ctx, mat->block);
54 free(mat);
55 return NULL;
58 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
59 unsigned n_row, unsigned n_col)
61 int i;
62 isl_int *old;
63 isl_int **row;
65 if (!mat)
66 return NULL;
68 if (mat->max_col >= n_col && mat->n_row >= n_row) {
69 if (mat->n_col < n_col)
70 mat->n_col = n_col;
71 return mat;
74 if (mat->max_col < n_col) {
75 struct isl_mat *new_mat;
77 if (n_row < mat->n_row)
78 n_row = mat->n_row;
79 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
80 if (!new_mat)
81 goto error;
82 for (i = 0; i < mat->n_row; ++i)
83 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
84 isl_mat_free(mat);
85 return new_mat;
88 mat = isl_mat_cow(mat);
89 if (!mat)
90 goto error;
92 old = mat->block.data;
93 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
94 if (isl_blk_is_error(mat->block))
95 goto error;
96 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
97 if (!row)
98 goto error;
99 mat->row = row;
101 for (i = 0; i < mat->n_row; ++i)
102 mat->row[i] = mat->block.data + (mat->row[i] - old);
103 for (i = mat->n_row; i < n_row; ++i)
104 mat->row[i] = mat->block.data + i * mat->max_col;
105 mat->n_row = n_row;
106 if (mat->n_col < n_col)
107 mat->n_col = n_col;
109 return mat;
110 error:
111 isl_mat_free(mat);
112 return NULL;
115 __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
116 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
118 int i;
119 struct isl_mat *mat;
121 mat = isl_alloc_type(ctx, struct isl_mat);
122 if (!mat)
123 return NULL;
124 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
125 if (!mat->row)
126 goto error;
127 for (i = 0; i < n_row; ++i)
128 mat->row[i] = row[first_row+i] + first_col;
129 mat->ctx = ctx;
130 isl_ctx_ref(ctx);
131 mat->ref = 1;
132 mat->n_row = n_row;
133 mat->n_col = n_col;
134 mat->block = isl_blk_empty();
135 mat->flags = ISL_MAT_BORROWED;
136 return mat;
137 error:
138 free(mat);
139 return NULL;
142 __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
143 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
145 if (!mat)
146 return NULL;
147 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
148 first_col, n_col);
151 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
152 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
154 int i;
156 for (i = 0; i < n_row; ++i)
157 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
160 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
161 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
163 int i;
165 for (i = 0; i < n_row; ++i)
166 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
169 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
171 if (!mat)
172 return NULL;
174 mat->ref++;
175 return mat;
178 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
180 int i;
181 struct isl_mat *mat2;
183 if (!mat)
184 return NULL;
185 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
186 if (!mat2)
187 return NULL;
188 for (i = 0; i < mat->n_row; ++i)
189 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
190 return mat2;
193 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
195 struct isl_mat *mat2;
196 if (!mat)
197 return NULL;
199 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
200 return mat;
202 mat2 = isl_mat_dup(mat);
203 isl_mat_free(mat);
204 return mat2;
207 void isl_mat_free(struct isl_mat *mat)
209 if (!mat)
210 return;
212 if (--mat->ref > 0)
213 return;
215 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
216 isl_blk_free(mat->ctx, mat->block);
217 isl_ctx_deref(mat->ctx);
218 free(mat->row);
219 free(mat);
222 int isl_mat_rows(__isl_keep isl_mat *mat)
224 return mat ? mat->n_row : -1;
227 int isl_mat_cols(__isl_keep isl_mat *mat)
229 return mat ? mat->n_col : -1;
232 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
234 if (!mat)
235 return -1;
236 if (row < 0 || row >= mat->n_row)
237 isl_die(mat->ctx, isl_error_invalid, "row out of range",
238 return -1);
239 if (col < 0 || col >= mat->n_col)
240 isl_die(mat->ctx, isl_error_invalid, "column out of range",
241 return -1);
242 isl_int_set(*v, mat->row[row][col]);
243 return 0;
246 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
247 int row, int col, isl_int v)
249 mat = isl_mat_cow(mat);
250 if (!mat)
251 return NULL;
252 if (row < 0 || row >= mat->n_row)
253 isl_die(mat->ctx, isl_error_invalid, "row out of range",
254 goto error);
255 if (col < 0 || col >= mat->n_col)
256 isl_die(mat->ctx, isl_error_invalid, "column out of range",
257 goto error);
258 isl_int_set(mat->row[row][col], v);
259 return mat;
260 error:
261 isl_mat_free(mat);
262 return NULL;
265 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
266 int row, int col, int v)
268 mat = isl_mat_cow(mat);
269 if (!mat)
270 return NULL;
271 if (row < 0 || row >= mat->n_row)
272 isl_die(mat->ctx, isl_error_invalid, "row out of range",
273 goto error);
274 if (col < 0 || col >= mat->n_col)
275 isl_die(mat->ctx, isl_error_invalid, "column out of range",
276 goto error);
277 isl_int_set_si(mat->row[row][col], v);
278 return mat;
279 error:
280 isl_mat_free(mat);
281 return NULL;
284 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
286 int i;
287 struct isl_mat *mat;
289 mat = isl_mat_alloc(ctx, n_row, n_row);
290 if (!mat)
291 return NULL;
292 for (i = 0; i < n_row; ++i) {
293 isl_seq_clr(mat->row[i], i);
294 isl_int_set(mat->row[i][i], d);
295 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
298 return mat;
301 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
303 if (!ctx)
304 return NULL;
305 return isl_mat_diag(ctx, n_row, ctx->one);
308 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
310 int i;
311 struct isl_vec *prod;
313 if (!mat || !vec)
314 goto error;
316 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
318 prod = isl_vec_alloc(mat->ctx, mat->n_row);
319 if (!prod)
320 goto error;
322 for (i = 0; i < prod->size; ++i)
323 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
324 &prod->block.data[i]);
325 isl_mat_free(mat);
326 isl_vec_free(vec);
327 return prod;
328 error:
329 isl_mat_free(mat);
330 isl_vec_free(vec);
331 return NULL;
334 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
335 __isl_take isl_vec *vec)
337 struct isl_mat *vec_mat;
338 int i;
340 if (!mat || !vec)
341 goto error;
342 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
343 if (!vec_mat)
344 goto error;
345 for (i = 0; i < vec->size; ++i)
346 isl_int_set(vec_mat->row[i][0], vec->el[i]);
347 vec_mat = isl_mat_inverse_product(mat, vec_mat);
348 isl_vec_free(vec);
349 if (!vec_mat)
350 return NULL;
351 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
352 if (vec)
353 for (i = 0; i < vec->size; ++i)
354 isl_int_set(vec->el[i], vec_mat->row[i][0]);
355 isl_mat_free(vec_mat);
356 return vec;
357 error:
358 isl_mat_free(mat);
359 isl_vec_free(vec);
360 return NULL;
363 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
365 int i, j;
366 struct isl_vec *prod;
368 if (!mat || !vec)
369 goto error;
371 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
373 prod = isl_vec_alloc(mat->ctx, mat->n_col);
374 if (!prod)
375 goto error;
377 for (i = 0; i < prod->size; ++i) {
378 isl_int_set_si(prod->el[i], 0);
379 for (j = 0; j < vec->size; ++j)
380 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
382 isl_mat_free(mat);
383 isl_vec_free(vec);
384 return prod;
385 error:
386 isl_mat_free(mat);
387 isl_vec_free(vec);
388 return NULL;
391 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
392 struct isl_mat *right)
394 int i;
395 struct isl_mat *sum;
397 if (!left || !right)
398 goto error;
400 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
401 isl_assert(left->ctx, left->n_row >= 1, goto error);
402 isl_assert(left->ctx, left->n_col >= 1, goto error);
403 isl_assert(left->ctx, right->n_col >= 1, goto error);
404 isl_assert(left->ctx,
405 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
406 goto error);
407 isl_assert(left->ctx,
408 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
409 goto error);
411 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
412 if (!sum)
413 goto error;
414 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
415 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
416 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
418 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
419 for (i = 1; i < sum->n_row; ++i) {
420 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
421 isl_int_addmul(sum->row[i][0],
422 right->row[0][0], right->row[i][0]);
423 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
424 left->n_col-1);
425 isl_seq_scale(sum->row[i]+left->n_col,
426 right->row[i]+1, right->row[0][0],
427 right->n_col-1);
430 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
431 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
432 isl_mat_free(left);
433 isl_mat_free(right);
434 return sum;
435 error:
436 isl_mat_free(left);
437 isl_mat_free(right);
438 return NULL;
441 static void exchange(struct isl_mat *M, struct isl_mat **U,
442 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
444 int r;
445 for (r = row; r < M->n_row; ++r)
446 isl_int_swap(M->row[r][i], M->row[r][j]);
447 if (U) {
448 for (r = 0; r < (*U)->n_row; ++r)
449 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
451 if (Q)
452 isl_mat_swap_rows(*Q, i, j);
455 static void subtract(struct isl_mat *M, struct isl_mat **U,
456 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
458 int r;
459 for (r = row; r < M->n_row; ++r)
460 isl_int_submul(M->row[r][j], m, M->row[r][i]);
461 if (U) {
462 for (r = 0; r < (*U)->n_row; ++r)
463 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
465 if (Q) {
466 for (r = 0; r < (*Q)->n_col; ++r)
467 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
471 static void oppose(struct isl_mat *M, struct isl_mat **U,
472 struct isl_mat **Q, unsigned row, unsigned col)
474 int r;
475 for (r = row; r < M->n_row; ++r)
476 isl_int_neg(M->row[r][col], M->row[r][col]);
477 if (U) {
478 for (r = 0; r < (*U)->n_row; ++r)
479 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
481 if (Q)
482 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
485 /* Given matrix M, compute
487 * M U = H
488 * M = H Q
490 * with U and Q unimodular matrices and H a matrix in column echelon form
491 * such that on each echelon row the entries in the non-echelon column
492 * are non-negative (if neg == 0) or non-positive (if neg == 1)
493 * and stricly smaller (in absolute value) than the entries in the echelon
494 * column.
495 * If U or Q are NULL, then these matrices are not computed.
497 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
498 struct isl_mat **U, struct isl_mat **Q)
500 isl_int c;
501 int row, col;
503 if (U)
504 *U = NULL;
505 if (Q)
506 *Q = NULL;
507 if (!M)
508 goto error;
509 M = isl_mat_cow(M);
510 if (!M)
511 goto error;
512 if (U) {
513 *U = isl_mat_identity(M->ctx, M->n_col);
514 if (!*U)
515 goto error;
517 if (Q) {
518 *Q = isl_mat_identity(M->ctx, M->n_col);
519 if (!*Q)
520 goto error;
523 col = 0;
524 isl_int_init(c);
525 for (row = 0; row < M->n_row; ++row) {
526 int first, i, off;
527 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
528 if (first == -1)
529 continue;
530 first += col;
531 if (first != col)
532 exchange(M, U, Q, row, first, col);
533 if (isl_int_is_neg(M->row[row][col]))
534 oppose(M, U, Q, row, col);
535 first = col+1;
536 while ((off = isl_seq_first_non_zero(M->row[row]+first,
537 M->n_col-first)) != -1) {
538 first += off;
539 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
540 subtract(M, U, Q, row, col, first, c);
541 if (!isl_int_is_zero(M->row[row][first]))
542 exchange(M, U, Q, row, first, col);
543 else
544 ++first;
546 for (i = 0; i < col; ++i) {
547 if (isl_int_is_zero(M->row[row][i]))
548 continue;
549 if (neg)
550 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
551 else
552 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
553 if (isl_int_is_zero(c))
554 continue;
555 subtract(M, U, Q, row, col, i, c);
557 ++col;
559 isl_int_clear(c);
561 return M;
562 error:
563 if (Q) {
564 isl_mat_free(*Q);
565 *Q = NULL;
567 if (U) {
568 isl_mat_free(*U);
569 *U = NULL;
571 isl_mat_free(M);
572 return NULL;
575 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
577 int i, rank;
578 struct isl_mat *U = NULL;
579 struct isl_mat *K;
581 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
582 if (!mat || !U)
583 goto error;
585 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
586 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
587 ++i;
588 if (i >= mat->n_row)
589 break;
591 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
592 if (!K)
593 goto error;
594 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
595 isl_mat_free(mat);
596 isl_mat_free(U);
597 return K;
598 error:
599 isl_mat_free(mat);
600 isl_mat_free(U);
601 return NULL;
604 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
606 int i;
607 struct isl_mat *mat2;
609 if (!mat)
610 return NULL;
611 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
612 if (!mat2)
613 goto error;
614 isl_int_set_si(mat2->row[0][0], 1);
615 isl_seq_clr(mat2->row[0]+1, mat->n_col);
616 for (i = 0; i < mat->n_row; ++i) {
617 isl_int_set_si(mat2->row[1+i][0], 0);
618 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
620 isl_mat_free(mat);
621 return mat2;
622 error:
623 isl_mat_free(mat);
624 return NULL;
627 /* Given two matrices M1 and M2, return the block matrix
629 * [ M1 0 ]
630 * [ 0 M2 ]
632 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
633 __isl_take isl_mat *mat2)
635 int i;
636 isl_mat *mat;
638 if (!mat1 || !mat2)
639 goto error;
641 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
642 mat1->n_col + mat2->n_col);
643 if (!mat)
644 goto error;
645 for (i = 0; i < mat1->n_row; ++i) {
646 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
647 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
649 for (i = 0; i < mat2->n_row; ++i) {
650 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
651 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
652 mat2->row[i], mat2->n_col);
654 isl_mat_free(mat1);
655 isl_mat_free(mat2);
656 return mat;
657 error:
658 isl_mat_free(mat1);
659 isl_mat_free(mat2);
660 return NULL;
663 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
665 int i;
667 for (i = 0; i < n_row; ++i)
668 if (!isl_int_is_zero(row[i][col]))
669 return i;
670 return -1;
673 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
675 int i, min = row_first_non_zero(row, n_row, col);
676 if (min < 0)
677 return -1;
678 for (i = min + 1; i < n_row; ++i) {
679 if (isl_int_is_zero(row[i][col]))
680 continue;
681 if (isl_int_abs_lt(row[i][col], row[min][col]))
682 min = i;
684 return min;
687 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
688 unsigned i, unsigned j)
690 left = isl_mat_swap_rows(left, i, j);
691 right = isl_mat_swap_rows(right, i, j);
694 static void inv_oppose(
695 struct isl_mat *left, struct isl_mat *right, unsigned row)
697 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
698 isl_seq_neg(right->row[row], right->row[row], right->n_col);
701 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
702 unsigned row, unsigned i, isl_int m)
704 isl_int_neg(m, m);
705 isl_seq_combine(left->row[i]+row,
706 left->ctx->one, left->row[i]+row,
707 m, left->row[row]+row,
708 left->n_col-row);
709 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
710 m, right->row[row], right->n_col);
713 /* Compute inv(left)*right
715 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
716 struct isl_mat *right)
718 int row;
719 isl_int a, b;
721 if (!left || !right)
722 goto error;
724 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
725 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
727 if (left->n_row == 0) {
728 isl_mat_free(left);
729 return right;
732 left = isl_mat_cow(left);
733 right = isl_mat_cow(right);
734 if (!left || !right)
735 goto error;
737 isl_int_init(a);
738 isl_int_init(b);
739 for (row = 0; row < left->n_row; ++row) {
740 int pivot, first, i, off;
741 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
742 if (pivot < 0) {
743 isl_int_clear(a);
744 isl_int_clear(b);
745 isl_assert(left->ctx, pivot >= 0, goto error);
747 pivot += row;
748 if (pivot != row)
749 inv_exchange(left, right, pivot, row);
750 if (isl_int_is_neg(left->row[row][row]))
751 inv_oppose(left, right, row);
752 first = row+1;
753 while ((off = row_first_non_zero(left->row+first,
754 left->n_row-first, row)) != -1) {
755 first += off;
756 isl_int_fdiv_q(a, left->row[first][row],
757 left->row[row][row]);
758 inv_subtract(left, right, row, first, a);
759 if (!isl_int_is_zero(left->row[first][row]))
760 inv_exchange(left, right, row, first);
761 else
762 ++first;
764 for (i = 0; i < row; ++i) {
765 if (isl_int_is_zero(left->row[i][row]))
766 continue;
767 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
768 isl_int_divexact(b, left->row[i][row], a);
769 isl_int_divexact(a, left->row[row][row], a);
770 isl_int_neg(b, b);
771 isl_seq_combine(left->row[i] + i,
772 a, left->row[i] + i,
773 b, left->row[row] + i,
774 left->n_col - i);
775 isl_seq_combine(right->row[i], a, right->row[i],
776 b, right->row[row], right->n_col);
779 isl_int_clear(b);
781 isl_int_set(a, left->row[0][0]);
782 for (row = 1; row < left->n_row; ++row)
783 isl_int_lcm(a, a, left->row[row][row]);
784 if (isl_int_is_zero(a)){
785 isl_int_clear(a);
786 isl_assert(left->ctx, 0, goto error);
788 for (row = 0; row < left->n_row; ++row) {
789 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
790 if (isl_int_is_one(left->row[row][row]))
791 continue;
792 isl_seq_scale(right->row[row], right->row[row],
793 left->row[row][row], right->n_col);
795 isl_int_clear(a);
797 isl_mat_free(left);
798 return right;
799 error:
800 isl_mat_free(left);
801 isl_mat_free(right);
802 return NULL;
805 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
807 int i;
809 for (i = 0; i < mat->n_row; ++i)
810 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
813 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
814 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
816 int i;
817 isl_int tmp;
819 isl_int_init(tmp);
820 for (i = 0; i < mat->n_row; ++i) {
821 isl_int_mul(tmp, m1, mat->row[i][src1]);
822 isl_int_addmul(tmp, m2, mat->row[i][src2]);
823 isl_int_set(mat->row[i][dst], tmp);
825 isl_int_clear(tmp);
828 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
830 struct isl_mat *inv;
831 int row;
832 isl_int a, b;
834 mat = isl_mat_cow(mat);
835 if (!mat)
836 return NULL;
838 inv = isl_mat_identity(mat->ctx, mat->n_col);
839 inv = isl_mat_cow(inv);
840 if (!inv)
841 goto error;
843 isl_int_init(a);
844 isl_int_init(b);
845 for (row = 0; row < mat->n_row; ++row) {
846 int pivot, first, i, off;
847 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
848 if (pivot < 0) {
849 isl_int_clear(a);
850 isl_int_clear(b);
851 isl_assert(mat->ctx, pivot >= 0, goto error);
853 pivot += row;
854 if (pivot != row)
855 exchange(mat, &inv, NULL, row, pivot, row);
856 if (isl_int_is_neg(mat->row[row][row]))
857 oppose(mat, &inv, NULL, row, row);
858 first = row+1;
859 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
860 mat->n_col-first)) != -1) {
861 first += off;
862 isl_int_fdiv_q(a, mat->row[row][first],
863 mat->row[row][row]);
864 subtract(mat, &inv, NULL, row, row, first, a);
865 if (!isl_int_is_zero(mat->row[row][first]))
866 exchange(mat, &inv, NULL, row, row, first);
867 else
868 ++first;
870 for (i = 0; i < row; ++i) {
871 if (isl_int_is_zero(mat->row[row][i]))
872 continue;
873 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
874 isl_int_divexact(b, mat->row[row][i], a);
875 isl_int_divexact(a, mat->row[row][row], a);
876 isl_int_neg(a, a);
877 isl_mat_col_combine(mat, i, a, i, b, row);
878 isl_mat_col_combine(inv, i, a, i, b, row);
881 isl_int_clear(b);
883 isl_int_set(a, mat->row[0][0]);
884 for (row = 1; row < mat->n_row; ++row)
885 isl_int_lcm(a, a, mat->row[row][row]);
886 if (isl_int_is_zero(a)){
887 isl_int_clear(a);
888 goto error;
890 for (row = 0; row < mat->n_row; ++row) {
891 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
892 if (isl_int_is_one(mat->row[row][row]))
893 continue;
894 isl_mat_col_scale(inv, row, mat->row[row][row]);
896 isl_int_clear(a);
898 isl_mat_free(mat);
900 return inv;
901 error:
902 isl_mat_free(mat);
903 isl_mat_free(inv);
904 return NULL;
907 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
909 struct isl_mat *transpose = NULL;
910 int i, j;
912 if (!mat)
913 return NULL;
915 if (mat->n_col == mat->n_row) {
916 mat = isl_mat_cow(mat);
917 if (!mat)
918 return NULL;
919 for (i = 0; i < mat->n_row; ++i)
920 for (j = i + 1; j < mat->n_col; ++j)
921 isl_int_swap(mat->row[i][j], mat->row[j][i]);
922 return mat;
924 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
925 if (!transpose)
926 goto error;
927 for (i = 0; i < mat->n_row; ++i)
928 for (j = 0; j < mat->n_col; ++j)
929 isl_int_set(transpose->row[j][i], mat->row[i][j]);
930 isl_mat_free(mat);
931 return transpose;
932 error:
933 isl_mat_free(mat);
934 return NULL;
937 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
939 int r;
941 mat = isl_mat_cow(mat);
942 if (!mat)
943 return NULL;
944 isl_assert(mat->ctx, i < mat->n_col, goto error);
945 isl_assert(mat->ctx, j < mat->n_col, goto error);
947 for (r = 0; r < mat->n_row; ++r)
948 isl_int_swap(mat->row[r][i], mat->row[r][j]);
949 return mat;
950 error:
951 isl_mat_free(mat);
952 return NULL;
955 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
957 isl_int *t;
959 if (!mat)
960 return NULL;
961 mat = isl_mat_cow(mat);
962 if (!mat)
963 return NULL;
964 t = mat->row[i];
965 mat->row[i] = mat->row[j];
966 mat->row[j] = t;
967 return mat;
970 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
971 __isl_take isl_mat *right)
973 int i, j, k;
974 struct isl_mat *prod;
976 if (!left || !right)
977 goto error;
978 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
979 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
980 if (!prod)
981 goto error;
982 if (left->n_col == 0) {
983 for (i = 0; i < prod->n_row; ++i)
984 isl_seq_clr(prod->row[i], prod->n_col);
985 isl_mat_free(left);
986 isl_mat_free(right);
987 return prod;
989 for (i = 0; i < prod->n_row; ++i) {
990 for (j = 0; j < prod->n_col; ++j) {
991 isl_int_mul(prod->row[i][j],
992 left->row[i][0], right->row[0][j]);
993 for (k = 1; k < left->n_col; ++k)
994 isl_int_addmul(prod->row[i][j],
995 left->row[i][k], right->row[k][j]);
998 isl_mat_free(left);
999 isl_mat_free(right);
1000 return prod;
1001 error:
1002 isl_mat_free(left);
1003 isl_mat_free(right);
1004 return NULL;
1007 /* Replace the variables x in the rows q by x' given by x = M x',
1008 * with M the matrix mat.
1010 * If the number of new variables is greater than the original
1011 * number of variables, then the rows q have already been
1012 * preextended. If the new number is smaller, then the coefficients
1013 * of the divs, which are not changed, need to be shifted down.
1014 * The row q may be the equalities, the inequalities or the
1015 * div expressions. In the latter case, has_div is true and
1016 * we need to take into account the extra denominator column.
1018 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1019 unsigned n_div, int has_div, struct isl_mat *mat)
1021 int i;
1022 struct isl_mat *t;
1023 int e;
1025 if (mat->n_col >= mat->n_row)
1026 e = 0;
1027 else
1028 e = mat->n_row - mat->n_col;
1029 if (has_div)
1030 for (i = 0; i < n; ++i)
1031 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1032 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1033 t = isl_mat_product(t, mat);
1034 if (!t)
1035 return -1;
1036 for (i = 0; i < n; ++i) {
1037 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1038 isl_seq_cpy(q[i] + has_div + t->n_col,
1039 q[i] + has_div + t->n_col + e, n_div);
1040 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1042 isl_mat_free(t);
1043 return 0;
1046 /* Replace the variables x in bset by x' given by x = M x', with
1047 * M the matrix mat.
1049 * If there are fewer variables x' then there are x, then we perform
1050 * the transformation in place, which that, in principle,
1051 * this frees up some extra variables as the number
1052 * of columns remains constant, but we would have to extend
1053 * the div array too as the number of rows in this array is assumed
1054 * to be equal to extra.
1056 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1057 struct isl_mat *mat)
1059 struct isl_ctx *ctx;
1061 if (!bset || !mat)
1062 goto error;
1064 ctx = bset->ctx;
1065 bset = isl_basic_set_cow(bset);
1066 if (!bset)
1067 goto error;
1069 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1070 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1071 isl_assert(ctx, mat->n_col > 0, goto error);
1073 if (mat->n_col > mat->n_row) {
1074 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1075 if (!bset)
1076 goto error;
1077 } else if (mat->n_col < mat->n_row) {
1078 bset->dim = isl_space_cow(bset->dim);
1079 if (!bset->dim)
1080 goto error;
1081 bset->dim->n_out -= mat->n_row - mat->n_col;
1084 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1085 isl_mat_copy(mat)) < 0)
1086 goto error;
1088 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1089 isl_mat_copy(mat)) < 0)
1090 goto error;
1092 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1093 goto error2;
1095 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1096 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1097 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1098 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1099 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1101 bset = isl_basic_set_simplify(bset);
1102 bset = isl_basic_set_finalize(bset);
1104 return bset;
1105 error:
1106 isl_mat_free(mat);
1107 error2:
1108 isl_basic_set_free(bset);
1109 return NULL;
1112 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1114 struct isl_ctx *ctx;
1115 int i;
1117 set = isl_set_cow(set);
1118 if (!set)
1119 return NULL;
1121 ctx = set->ctx;
1122 for (i = 0; i < set->n; ++i) {
1123 set->p[i] = isl_basic_set_preimage(set->p[i],
1124 isl_mat_copy(mat));
1125 if (!set->p[i])
1126 goto error;
1128 if (mat->n_col != mat->n_row) {
1129 set->dim = isl_space_cow(set->dim);
1130 if (!set->dim)
1131 goto error;
1132 set->dim->n_out += mat->n_col;
1133 set->dim->n_out -= mat->n_row;
1135 isl_mat_free(mat);
1136 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1137 return set;
1138 error:
1139 isl_set_free(set);
1140 isl_mat_free(mat);
1141 return NULL;
1144 /* Replace the variables x starting at pos in the rows q
1145 * by x' with x = M x' with M the matrix mat.
1146 * That is, replace the corresponding coefficients c by c M.
1148 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1149 unsigned pos, __isl_take isl_mat *mat)
1151 int i;
1152 isl_mat *t;
1154 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1155 t = isl_mat_product(t, mat);
1156 if (!t)
1157 return -1;
1158 for (i = 0; i < n; ++i)
1159 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1160 isl_mat_free(t);
1161 return 0;
1164 /* Replace the variables x of type "type" starting at "first" in "bset"
1165 * by x' with x = M x' with M the matrix trans.
1166 * That is, replace the corresponding coefficients c by c M.
1168 * The transformation matrix should be a square matrix.
1170 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1171 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1172 __isl_take isl_mat *trans)
1174 isl_ctx *ctx;
1175 unsigned pos;
1177 bset = isl_basic_set_cow(bset);
1178 if (!bset || !trans)
1179 goto error;
1181 ctx = isl_basic_set_get_ctx(bset);
1182 if (trans->n_row != trans->n_col)
1183 isl_die(trans->ctx, isl_error_invalid,
1184 "expecting square transformation matrix", goto error);
1185 if (first + trans->n_row > isl_basic_set_dim(bset, type))
1186 isl_die(trans->ctx, isl_error_invalid,
1187 "oversized transformation matrix", goto error);
1189 pos = isl_basic_set_offset(bset, type) + first;
1191 if (transform(ctx, bset->eq, bset->n_eq, pos, isl_mat_copy(trans)) < 0)
1192 goto error;
1193 if (transform(ctx, bset->ineq, bset->n_ineq, pos,
1194 isl_mat_copy(trans)) < 0)
1195 goto error;
1196 if (transform(ctx, bset->div, bset->n_div, 1 + pos,
1197 isl_mat_copy(trans)) < 0)
1198 goto error;
1200 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1201 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1203 isl_mat_free(trans);
1204 return bset;
1205 error:
1206 isl_mat_free(trans);
1207 isl_basic_set_free(bset);
1208 return NULL;
1211 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1213 int i, j;
1215 if (!mat) {
1216 fprintf(out, "%*snull mat\n", indent, "");
1217 return;
1220 if (mat->n_row == 0)
1221 fprintf(out, "%*s[]\n", indent, "");
1223 for (i = 0; i < mat->n_row; ++i) {
1224 if (!i)
1225 fprintf(out, "%*s[[", indent, "");
1226 else
1227 fprintf(out, "%*s[", indent+1, "");
1228 for (j = 0; j < mat->n_col; ++j) {
1229 if (j)
1230 fprintf(out, ",");
1231 isl_int_print(out, mat->row[i][j], 0);
1233 if (i == mat->n_row-1)
1234 fprintf(out, "]]\n");
1235 else
1236 fprintf(out, "]\n");
1240 void isl_mat_dump(__isl_keep isl_mat *mat)
1242 isl_mat_print_internal(mat, stderr, 0);
1245 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1247 int r;
1249 if (n == 0)
1250 return mat;
1252 mat = isl_mat_cow(mat);
1253 if (!mat)
1254 return NULL;
1256 if (col != mat->n_col-n) {
1257 for (r = 0; r < mat->n_row; ++r)
1258 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1259 mat->n_col - col - n);
1261 mat->n_col -= n;
1262 return mat;
1265 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1267 int r;
1269 mat = isl_mat_cow(mat);
1270 if (!mat)
1271 return NULL;
1273 for (r = row; r+n < mat->n_row; ++r)
1274 mat->row[r] = mat->row[r+n];
1276 mat->n_row -= n;
1277 return mat;
1280 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1281 unsigned col, unsigned n)
1283 isl_mat *ext;
1285 if (!mat)
1286 return NULL;
1287 if (n == 0)
1288 return mat;
1290 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1291 if (!ext)
1292 goto error;
1294 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1295 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1296 col + n, col, mat->n_col - col);
1298 isl_mat_free(mat);
1299 return ext;
1300 error:
1301 isl_mat_free(mat);
1302 return NULL;
1305 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1306 unsigned first, unsigned n)
1308 int i;
1310 if (!mat)
1311 return NULL;
1312 mat = isl_mat_insert_cols(mat, first, n);
1313 if (!mat)
1314 return NULL;
1316 for (i = 0; i < mat->n_row; ++i)
1317 isl_seq_clr(mat->row[i] + first, n);
1319 return mat;
1322 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1324 if (!mat)
1325 return NULL;
1327 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1330 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1331 unsigned row, unsigned n)
1333 isl_mat *ext;
1335 if (!mat)
1336 return NULL;
1337 if (n == 0)
1338 return mat;
1340 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1341 if (!ext)
1342 goto error;
1344 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1345 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1346 mat->n_row - row, 0, 0, mat->n_col);
1348 isl_mat_free(mat);
1349 return ext;
1350 error:
1351 isl_mat_free(mat);
1352 return NULL;
1355 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1357 if (!mat)
1358 return NULL;
1360 return isl_mat_insert_rows(mat, mat->n_row, n);
1363 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1364 unsigned row, unsigned n)
1366 int i;
1368 mat = isl_mat_insert_rows(mat, row, n);
1369 if (!mat)
1370 return NULL;
1372 for (i = 0; i < n; ++i)
1373 isl_seq_clr(mat->row[row + i], mat->n_col);
1375 return mat;
1378 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1380 if (!mat)
1381 return NULL;
1383 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1386 void isl_mat_col_submul(struct isl_mat *mat,
1387 int dst_col, isl_int f, int src_col)
1389 int i;
1391 for (i = 0; i < mat->n_row; ++i)
1392 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1395 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1397 int i;
1399 if (!mat)
1400 return;
1402 for (i = 0; i < mat->n_row; ++i)
1403 isl_int_add(mat->row[i][dst_col],
1404 mat->row[i][dst_col], mat->row[i][src_col]);
1407 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1409 int i;
1411 for (i = 0; i < mat->n_row; ++i)
1412 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1415 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1417 int r;
1418 struct isl_mat *H = NULL, *Q = NULL;
1420 if (!M)
1421 return NULL;
1423 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1424 M->n_row = row;
1425 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1426 M->n_row = M->n_col;
1427 if (!H)
1428 goto error;
1429 for (r = 0; r < row; ++r)
1430 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1431 for (r = row; r < M->n_row; ++r)
1432 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1433 isl_mat_free(H);
1434 isl_mat_free(Q);
1435 return M;
1436 error:
1437 isl_mat_free(H);
1438 isl_mat_free(Q);
1439 isl_mat_free(M);
1440 return NULL;
1443 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1444 __isl_take isl_mat *bot)
1446 struct isl_mat *mat;
1448 if (!top || !bot)
1449 goto error;
1451 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1452 if (top->n_row == 0) {
1453 isl_mat_free(top);
1454 return bot;
1456 if (bot->n_row == 0) {
1457 isl_mat_free(bot);
1458 return top;
1461 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1462 if (!mat)
1463 goto error;
1464 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1465 0, 0, mat->n_col);
1466 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1467 0, 0, mat->n_col);
1468 isl_mat_free(top);
1469 isl_mat_free(bot);
1470 return mat;
1471 error:
1472 isl_mat_free(top);
1473 isl_mat_free(bot);
1474 return NULL;
1477 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1479 int i;
1481 if (!mat1 || !mat2)
1482 return -1;
1484 if (mat1->n_row != mat2->n_row)
1485 return 0;
1487 if (mat1->n_col != mat2->n_col)
1488 return 0;
1490 for (i = 0; i < mat1->n_row; ++i)
1491 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1492 return 0;
1494 return 1;
1497 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1499 struct isl_mat *mat;
1501 if (!vec)
1502 return NULL;
1503 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1504 if (!mat)
1505 goto error;
1507 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1509 isl_vec_free(vec);
1510 return mat;
1511 error:
1512 isl_vec_free(vec);
1513 return NULL;
1516 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1517 __isl_take isl_vec *bot)
1519 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1522 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1523 unsigned dst_col, unsigned src_col, unsigned n)
1525 isl_mat *res;
1527 if (!mat)
1528 return NULL;
1529 if (n == 0 || dst_col == src_col)
1530 return mat;
1532 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1533 if (!res)
1534 goto error;
1536 if (dst_col < src_col) {
1537 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1538 0, 0, dst_col);
1539 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1540 dst_col, src_col, n);
1541 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1542 dst_col + n, dst_col, src_col - dst_col);
1543 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1544 src_col + n, src_col + n,
1545 res->n_col - src_col - n);
1546 } else {
1547 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1548 0, 0, src_col);
1549 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1550 src_col, src_col + n, dst_col - src_col);
1551 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1552 dst_col, src_col, n);
1553 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1554 dst_col + n, dst_col + n,
1555 res->n_col - dst_col - n);
1557 isl_mat_free(mat);
1559 return res;
1560 error:
1561 isl_mat_free(mat);
1562 return NULL;
1565 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1567 int i;
1568 isl_int g;
1570 isl_int_set_si(*gcd, 0);
1571 if (!mat)
1572 return;
1574 isl_int_init(g);
1575 for (i = 0; i < mat->n_row; ++i) {
1576 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1577 isl_int_gcd(*gcd, *gcd, g);
1579 isl_int_clear(g);
1582 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1584 int i;
1586 if (isl_int_is_one(m))
1587 return mat;
1589 mat = isl_mat_cow(mat);
1590 if (!mat)
1591 return NULL;
1593 for (i = 0; i < mat->n_row; ++i)
1594 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1596 return mat;
1599 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1600 isl_int m)
1602 if (isl_int_is_one(m))
1603 return mat;
1605 mat = isl_mat_cow(mat);
1606 if (!mat)
1607 return NULL;
1609 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1611 return mat;
1614 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1616 isl_int gcd;
1618 if (!mat)
1619 return NULL;
1621 isl_int_init(gcd);
1622 isl_mat_gcd(mat, &gcd);
1623 mat = isl_mat_scale_down(mat, gcd);
1624 isl_int_clear(gcd);
1626 return mat;
1629 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1631 mat = isl_mat_cow(mat);
1632 if (!mat)
1633 return NULL;
1635 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1637 return mat;
1640 /* Number of initial non-zero columns.
1642 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1644 int i;
1646 if (!mat)
1647 return -1;
1649 for (i = 0; i < mat->n_col; ++i)
1650 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1651 break;
1653 return i;