declare isl_union_pw_multi_aff_gist
[isl.git] / isl_mat.c
blob3a8d31034be0ae9a99513f312d0892ea118ef554
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 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
28 unsigned n_row, unsigned n_col)
30 int i;
31 struct isl_mat *mat;
33 mat = isl_alloc_type(ctx, struct isl_mat);
34 if (!mat)
35 return NULL;
37 mat->row = NULL;
38 mat->block = isl_blk_alloc(ctx, n_row * n_col);
39 if (isl_blk_is_error(mat->block))
40 goto error;
41 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
42 if (n_row && !mat->row)
43 goto error;
45 for (i = 0; i < n_row; ++i)
46 mat->row[i] = mat->block.data + i * n_col;
48 mat->ctx = ctx;
49 isl_ctx_ref(ctx);
50 mat->ref = 1;
51 mat->n_row = n_row;
52 mat->n_col = n_col;
53 mat->max_col = n_col;
54 mat->flags = 0;
56 return mat;
57 error:
58 isl_blk_free(ctx, mat->block);
59 free(mat);
60 return NULL;
63 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
64 unsigned n_row, unsigned n_col)
66 int i;
67 isl_int *old;
68 isl_int **row;
70 if (!mat)
71 return NULL;
73 if (mat->max_col >= n_col && mat->n_row >= n_row) {
74 if (mat->n_col < n_col)
75 mat->n_col = n_col;
76 return mat;
79 if (mat->max_col < n_col) {
80 struct isl_mat *new_mat;
82 if (n_row < mat->n_row)
83 n_row = mat->n_row;
84 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
85 if (!new_mat)
86 goto error;
87 for (i = 0; i < mat->n_row; ++i)
88 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
89 isl_mat_free(mat);
90 return new_mat;
93 mat = isl_mat_cow(mat);
94 if (!mat)
95 goto error;
97 old = mat->block.data;
98 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
99 if (isl_blk_is_error(mat->block))
100 goto error;
101 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
102 if (n_row && !row)
103 goto error;
104 mat->row = row;
106 for (i = 0; i < mat->n_row; ++i)
107 mat->row[i] = mat->block.data + (mat->row[i] - old);
108 for (i = mat->n_row; i < n_row; ++i)
109 mat->row[i] = mat->block.data + i * mat->max_col;
110 mat->n_row = n_row;
111 if (mat->n_col < n_col)
112 mat->n_col = n_col;
114 return mat;
115 error:
116 isl_mat_free(mat);
117 return NULL;
120 __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
121 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
123 int i;
124 struct isl_mat *mat;
126 mat = isl_alloc_type(ctx, struct isl_mat);
127 if (!mat)
128 return NULL;
129 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
130 if (n_row && !mat->row)
131 goto error;
132 for (i = 0; i < n_row; ++i)
133 mat->row[i] = row[first_row+i] + first_col;
134 mat->ctx = ctx;
135 isl_ctx_ref(ctx);
136 mat->ref = 1;
137 mat->n_row = n_row;
138 mat->n_col = n_col;
139 mat->block = isl_blk_empty();
140 mat->flags = ISL_MAT_BORROWED;
141 return mat;
142 error:
143 free(mat);
144 return NULL;
147 __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
148 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
150 if (!mat)
151 return NULL;
152 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
153 first_col, n_col);
156 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
157 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
159 int i;
161 for (i = 0; i < n_row; ++i)
162 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
165 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
166 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
168 int i;
170 for (i = 0; i < n_row; ++i)
171 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
174 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
176 if (!mat)
177 return NULL;
179 mat->ref++;
180 return mat;
183 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
185 int i;
186 struct isl_mat *mat2;
188 if (!mat)
189 return NULL;
190 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
191 if (!mat2)
192 return NULL;
193 for (i = 0; i < mat->n_row; ++i)
194 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
195 return mat2;
198 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
200 struct isl_mat *mat2;
201 if (!mat)
202 return NULL;
204 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
205 return mat;
207 mat2 = isl_mat_dup(mat);
208 isl_mat_free(mat);
209 return mat2;
212 __isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
214 if (!mat)
215 return NULL;
217 if (--mat->ref > 0)
218 return NULL;
220 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
221 isl_blk_free(mat->ctx, mat->block);
222 isl_ctx_deref(mat->ctx);
223 free(mat->row);
224 free(mat);
226 return NULL;
229 int isl_mat_rows(__isl_keep isl_mat *mat)
231 return mat ? mat->n_row : -1;
234 int isl_mat_cols(__isl_keep isl_mat *mat)
236 return mat ? mat->n_col : -1;
239 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
241 if (!mat)
242 return -1;
243 if (row < 0 || row >= mat->n_row)
244 isl_die(mat->ctx, isl_error_invalid, "row out of range",
245 return -1);
246 if (col < 0 || col >= mat->n_col)
247 isl_die(mat->ctx, isl_error_invalid, "column out of range",
248 return -1);
249 isl_int_set(*v, mat->row[row][col]);
250 return 0;
253 /* Extract the element at row "row", oolumn "col" of "mat".
255 __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
256 int row, int col)
258 isl_ctx *ctx;
260 if (!mat)
261 return NULL;
262 ctx = isl_mat_get_ctx(mat);
263 if (row < 0 || row >= mat->n_row)
264 isl_die(ctx, isl_error_invalid, "row out of range",
265 return NULL);
266 if (col < 0 || col >= mat->n_col)
267 isl_die(ctx, isl_error_invalid, "column out of range",
268 return NULL);
269 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
272 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
273 int row, int col, isl_int v)
275 mat = isl_mat_cow(mat);
276 if (!mat)
277 return NULL;
278 if (row < 0 || row >= mat->n_row)
279 isl_die(mat->ctx, isl_error_invalid, "row out of range",
280 goto error);
281 if (col < 0 || col >= mat->n_col)
282 isl_die(mat->ctx, isl_error_invalid, "column out of range",
283 goto error);
284 isl_int_set(mat->row[row][col], v);
285 return mat;
286 error:
287 isl_mat_free(mat);
288 return NULL;
291 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
292 int row, int col, int v)
294 mat = isl_mat_cow(mat);
295 if (!mat)
296 return NULL;
297 if (row < 0 || row >= mat->n_row)
298 isl_die(mat->ctx, isl_error_invalid, "row out of range",
299 goto error);
300 if (col < 0 || col >= mat->n_col)
301 isl_die(mat->ctx, isl_error_invalid, "column out of range",
302 goto error);
303 isl_int_set_si(mat->row[row][col], v);
304 return mat;
305 error:
306 isl_mat_free(mat);
307 return NULL;
310 /* Replace the element at row "row", column "col" of "mat" by "v".
312 __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
313 int row, int col, __isl_take isl_val *v)
315 if (!v)
316 return isl_mat_free(mat);
317 if (!isl_val_is_int(v))
318 isl_die(isl_val_get_ctx(v), isl_error_invalid,
319 "expecting integer value", goto error);
320 mat = isl_mat_set_element(mat, row, col, v->n);
321 isl_val_free(v);
322 return mat;
323 error:
324 isl_val_free(v);
325 return isl_mat_free(mat);
328 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
330 int i;
331 struct isl_mat *mat;
333 mat = isl_mat_alloc(ctx, n_row, n_row);
334 if (!mat)
335 return NULL;
336 for (i = 0; i < n_row; ++i) {
337 isl_seq_clr(mat->row[i], i);
338 isl_int_set(mat->row[i][i], d);
339 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
342 return mat;
345 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
347 if (!ctx)
348 return NULL;
349 return isl_mat_diag(ctx, n_row, ctx->one);
352 /* Is "mat" a (possibly scaled) identity matrix?
354 int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
356 int i;
358 if (!mat)
359 return -1;
360 if (mat->n_row != mat->n_col)
361 return 0;
363 for (i = 0; i < mat->n_row; ++i) {
364 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
365 return 0;
366 if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
367 return 0;
368 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
369 mat->n_col - (i + 1)) != -1)
370 return 0;
373 return 1;
376 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
378 int i;
379 struct isl_vec *prod;
381 if (!mat || !vec)
382 goto error;
384 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
386 prod = isl_vec_alloc(mat->ctx, mat->n_row);
387 if (!prod)
388 goto error;
390 for (i = 0; i < prod->size; ++i)
391 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
392 &prod->block.data[i]);
393 isl_mat_free(mat);
394 isl_vec_free(vec);
395 return prod;
396 error:
397 isl_mat_free(mat);
398 isl_vec_free(vec);
399 return NULL;
402 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
403 __isl_take isl_vec *vec)
405 struct isl_mat *vec_mat;
406 int i;
408 if (!mat || !vec)
409 goto error;
410 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
411 if (!vec_mat)
412 goto error;
413 for (i = 0; i < vec->size; ++i)
414 isl_int_set(vec_mat->row[i][0], vec->el[i]);
415 vec_mat = isl_mat_inverse_product(mat, vec_mat);
416 isl_vec_free(vec);
417 if (!vec_mat)
418 return NULL;
419 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
420 if (vec)
421 for (i = 0; i < vec->size; ++i)
422 isl_int_set(vec->el[i], vec_mat->row[i][0]);
423 isl_mat_free(vec_mat);
424 return vec;
425 error:
426 isl_mat_free(mat);
427 isl_vec_free(vec);
428 return NULL;
431 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
433 int i, j;
434 struct isl_vec *prod;
436 if (!mat || !vec)
437 goto error;
439 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
441 prod = isl_vec_alloc(mat->ctx, mat->n_col);
442 if (!prod)
443 goto error;
445 for (i = 0; i < prod->size; ++i) {
446 isl_int_set_si(prod->el[i], 0);
447 for (j = 0; j < vec->size; ++j)
448 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
450 isl_mat_free(mat);
451 isl_vec_free(vec);
452 return prod;
453 error:
454 isl_mat_free(mat);
455 isl_vec_free(vec);
456 return NULL;
459 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
460 struct isl_mat *right)
462 int i;
463 struct isl_mat *sum;
465 if (!left || !right)
466 goto error;
468 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
469 isl_assert(left->ctx, left->n_row >= 1, goto error);
470 isl_assert(left->ctx, left->n_col >= 1, goto error);
471 isl_assert(left->ctx, right->n_col >= 1, goto error);
472 isl_assert(left->ctx,
473 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
474 goto error);
475 isl_assert(left->ctx,
476 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
477 goto error);
479 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
480 if (!sum)
481 goto error;
482 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
483 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
484 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
486 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
487 for (i = 1; i < sum->n_row; ++i) {
488 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
489 isl_int_addmul(sum->row[i][0],
490 right->row[0][0], right->row[i][0]);
491 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
492 left->n_col-1);
493 isl_seq_scale(sum->row[i]+left->n_col,
494 right->row[i]+1, right->row[0][0],
495 right->n_col-1);
498 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
499 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
500 isl_mat_free(left);
501 isl_mat_free(right);
502 return sum;
503 error:
504 isl_mat_free(left);
505 isl_mat_free(right);
506 return NULL;
509 static void exchange(struct isl_mat *M, struct isl_mat **U,
510 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
512 int r;
513 for (r = row; r < M->n_row; ++r)
514 isl_int_swap(M->row[r][i], M->row[r][j]);
515 if (U) {
516 for (r = 0; r < (*U)->n_row; ++r)
517 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
519 if (Q)
520 isl_mat_swap_rows(*Q, i, j);
523 static void subtract(struct isl_mat *M, struct isl_mat **U,
524 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
526 int r;
527 for (r = row; r < M->n_row; ++r)
528 isl_int_submul(M->row[r][j], m, M->row[r][i]);
529 if (U) {
530 for (r = 0; r < (*U)->n_row; ++r)
531 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
533 if (Q) {
534 for (r = 0; r < (*Q)->n_col; ++r)
535 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
539 static void oppose(struct isl_mat *M, struct isl_mat **U,
540 struct isl_mat **Q, unsigned row, unsigned col)
542 int r;
543 for (r = row; r < M->n_row; ++r)
544 isl_int_neg(M->row[r][col], M->row[r][col]);
545 if (U) {
546 for (r = 0; r < (*U)->n_row; ++r)
547 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
549 if (Q)
550 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
553 /* Given matrix M, compute
555 * M U = H
556 * M = H Q
558 * with U and Q unimodular matrices and H a matrix in column echelon form
559 * such that on each echelon row the entries in the non-echelon column
560 * are non-negative (if neg == 0) or non-positive (if neg == 1)
561 * and strictly smaller (in absolute value) than the entries in the echelon
562 * column.
563 * If U or Q are NULL, then these matrices are not computed.
565 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
566 struct isl_mat **U, struct isl_mat **Q)
568 isl_int c;
569 int row, col;
571 if (U)
572 *U = NULL;
573 if (Q)
574 *Q = NULL;
575 if (!M)
576 goto error;
577 M = isl_mat_cow(M);
578 if (!M)
579 goto error;
580 if (U) {
581 *U = isl_mat_identity(M->ctx, M->n_col);
582 if (!*U)
583 goto error;
585 if (Q) {
586 *Q = isl_mat_identity(M->ctx, M->n_col);
587 if (!*Q)
588 goto error;
591 col = 0;
592 isl_int_init(c);
593 for (row = 0; row < M->n_row; ++row) {
594 int first, i, off;
595 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
596 if (first == -1)
597 continue;
598 first += col;
599 if (first != col)
600 exchange(M, U, Q, row, first, col);
601 if (isl_int_is_neg(M->row[row][col]))
602 oppose(M, U, Q, row, col);
603 first = col+1;
604 while ((off = isl_seq_first_non_zero(M->row[row]+first,
605 M->n_col-first)) != -1) {
606 first += off;
607 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
608 subtract(M, U, Q, row, col, first, c);
609 if (!isl_int_is_zero(M->row[row][first]))
610 exchange(M, U, Q, row, first, col);
611 else
612 ++first;
614 for (i = 0; i < col; ++i) {
615 if (isl_int_is_zero(M->row[row][i]))
616 continue;
617 if (neg)
618 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
619 else
620 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
621 if (isl_int_is_zero(c))
622 continue;
623 subtract(M, U, Q, row, col, i, c);
625 ++col;
627 isl_int_clear(c);
629 return M;
630 error:
631 if (Q) {
632 isl_mat_free(*Q);
633 *Q = NULL;
635 if (U) {
636 isl_mat_free(*U);
637 *U = NULL;
639 isl_mat_free(M);
640 return NULL;
643 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
645 int i, rank;
646 struct isl_mat *U = NULL;
647 struct isl_mat *K;
649 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
650 if (!mat || !U)
651 goto error;
653 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
654 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
655 ++i;
656 if (i >= mat->n_row)
657 break;
659 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
660 if (!K)
661 goto error;
662 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
663 isl_mat_free(mat);
664 isl_mat_free(U);
665 return K;
666 error:
667 isl_mat_free(mat);
668 isl_mat_free(U);
669 return NULL;
672 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
674 int i;
675 struct isl_mat *mat2;
677 if (!mat)
678 return NULL;
679 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
680 if (!mat2)
681 goto error;
682 isl_int_set_si(mat2->row[0][0], 1);
683 isl_seq_clr(mat2->row[0]+1, mat->n_col);
684 for (i = 0; i < mat->n_row; ++i) {
685 isl_int_set_si(mat2->row[1+i][0], 0);
686 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
688 isl_mat_free(mat);
689 return mat2;
690 error:
691 isl_mat_free(mat);
692 return NULL;
695 /* Given two matrices M1 and M2, return the block matrix
697 * [ M1 0 ]
698 * [ 0 M2 ]
700 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
701 __isl_take isl_mat *mat2)
703 int i;
704 isl_mat *mat;
706 if (!mat1 || !mat2)
707 goto error;
709 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
710 mat1->n_col + mat2->n_col);
711 if (!mat)
712 goto error;
713 for (i = 0; i < mat1->n_row; ++i) {
714 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
715 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
717 for (i = 0; i < mat2->n_row; ++i) {
718 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
719 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
720 mat2->row[i], mat2->n_col);
722 isl_mat_free(mat1);
723 isl_mat_free(mat2);
724 return mat;
725 error:
726 isl_mat_free(mat1);
727 isl_mat_free(mat2);
728 return NULL;
731 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
733 int i;
735 for (i = 0; i < n_row; ++i)
736 if (!isl_int_is_zero(row[i][col]))
737 return i;
738 return -1;
741 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
743 int i, min = row_first_non_zero(row, n_row, col);
744 if (min < 0)
745 return -1;
746 for (i = min + 1; i < n_row; ++i) {
747 if (isl_int_is_zero(row[i][col]))
748 continue;
749 if (isl_int_abs_lt(row[i][col], row[min][col]))
750 min = i;
752 return min;
755 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
756 unsigned i, unsigned j)
758 left = isl_mat_swap_rows(left, i, j);
759 right = isl_mat_swap_rows(right, i, j);
762 static void inv_oppose(
763 struct isl_mat *left, struct isl_mat *right, unsigned row)
765 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
766 isl_seq_neg(right->row[row], right->row[row], right->n_col);
769 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
770 unsigned row, unsigned i, isl_int m)
772 isl_int_neg(m, m);
773 isl_seq_combine(left->row[i]+row,
774 left->ctx->one, left->row[i]+row,
775 m, left->row[row]+row,
776 left->n_col-row);
777 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
778 m, right->row[row], right->n_col);
781 /* Compute inv(left)*right
783 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
784 struct isl_mat *right)
786 int row;
787 isl_int a, b;
789 if (!left || !right)
790 goto error;
792 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
793 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
795 if (left->n_row == 0) {
796 isl_mat_free(left);
797 return right;
800 left = isl_mat_cow(left);
801 right = isl_mat_cow(right);
802 if (!left || !right)
803 goto error;
805 isl_int_init(a);
806 isl_int_init(b);
807 for (row = 0; row < left->n_row; ++row) {
808 int pivot, first, i, off;
809 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
810 if (pivot < 0) {
811 isl_int_clear(a);
812 isl_int_clear(b);
813 isl_assert(left->ctx, pivot >= 0, goto error);
815 pivot += row;
816 if (pivot != row)
817 inv_exchange(left, right, pivot, row);
818 if (isl_int_is_neg(left->row[row][row]))
819 inv_oppose(left, right, row);
820 first = row+1;
821 while ((off = row_first_non_zero(left->row+first,
822 left->n_row-first, row)) != -1) {
823 first += off;
824 isl_int_fdiv_q(a, left->row[first][row],
825 left->row[row][row]);
826 inv_subtract(left, right, row, first, a);
827 if (!isl_int_is_zero(left->row[first][row]))
828 inv_exchange(left, right, row, first);
829 else
830 ++first;
832 for (i = 0; i < row; ++i) {
833 if (isl_int_is_zero(left->row[i][row]))
834 continue;
835 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
836 isl_int_divexact(b, left->row[i][row], a);
837 isl_int_divexact(a, left->row[row][row], a);
838 isl_int_neg(b, b);
839 isl_seq_combine(left->row[i] + i,
840 a, left->row[i] + i,
841 b, left->row[row] + i,
842 left->n_col - i);
843 isl_seq_combine(right->row[i], a, right->row[i],
844 b, right->row[row], right->n_col);
847 isl_int_clear(b);
849 isl_int_set(a, left->row[0][0]);
850 for (row = 1; row < left->n_row; ++row)
851 isl_int_lcm(a, a, left->row[row][row]);
852 if (isl_int_is_zero(a)){
853 isl_int_clear(a);
854 isl_assert(left->ctx, 0, goto error);
856 for (row = 0; row < left->n_row; ++row) {
857 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
858 if (isl_int_is_one(left->row[row][row]))
859 continue;
860 isl_seq_scale(right->row[row], right->row[row],
861 left->row[row][row], right->n_col);
863 isl_int_clear(a);
865 isl_mat_free(left);
866 return right;
867 error:
868 isl_mat_free(left);
869 isl_mat_free(right);
870 return NULL;
873 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
875 int i;
877 for (i = 0; i < mat->n_row; ++i)
878 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
881 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
882 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
884 int i;
885 isl_int tmp;
887 isl_int_init(tmp);
888 for (i = 0; i < mat->n_row; ++i) {
889 isl_int_mul(tmp, m1, mat->row[i][src1]);
890 isl_int_addmul(tmp, m2, mat->row[i][src2]);
891 isl_int_set(mat->row[i][dst], tmp);
893 isl_int_clear(tmp);
896 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
898 struct isl_mat *inv;
899 int row;
900 isl_int a, b;
902 mat = isl_mat_cow(mat);
903 if (!mat)
904 return NULL;
906 inv = isl_mat_identity(mat->ctx, mat->n_col);
907 inv = isl_mat_cow(inv);
908 if (!inv)
909 goto error;
911 isl_int_init(a);
912 isl_int_init(b);
913 for (row = 0; row < mat->n_row; ++row) {
914 int pivot, first, i, off;
915 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
916 if (pivot < 0) {
917 isl_int_clear(a);
918 isl_int_clear(b);
919 isl_assert(mat->ctx, pivot >= 0, goto error);
921 pivot += row;
922 if (pivot != row)
923 exchange(mat, &inv, NULL, row, pivot, row);
924 if (isl_int_is_neg(mat->row[row][row]))
925 oppose(mat, &inv, NULL, row, row);
926 first = row+1;
927 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
928 mat->n_col-first)) != -1) {
929 first += off;
930 isl_int_fdiv_q(a, mat->row[row][first],
931 mat->row[row][row]);
932 subtract(mat, &inv, NULL, row, row, first, a);
933 if (!isl_int_is_zero(mat->row[row][first]))
934 exchange(mat, &inv, NULL, row, row, first);
935 else
936 ++first;
938 for (i = 0; i < row; ++i) {
939 if (isl_int_is_zero(mat->row[row][i]))
940 continue;
941 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
942 isl_int_divexact(b, mat->row[row][i], a);
943 isl_int_divexact(a, mat->row[row][row], a);
944 isl_int_neg(a, a);
945 isl_mat_col_combine(mat, i, a, i, b, row);
946 isl_mat_col_combine(inv, i, a, i, b, row);
949 isl_int_clear(b);
951 isl_int_set(a, mat->row[0][0]);
952 for (row = 1; row < mat->n_row; ++row)
953 isl_int_lcm(a, a, mat->row[row][row]);
954 if (isl_int_is_zero(a)){
955 isl_int_clear(a);
956 goto error;
958 for (row = 0; row < mat->n_row; ++row) {
959 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
960 if (isl_int_is_one(mat->row[row][row]))
961 continue;
962 isl_mat_col_scale(inv, row, mat->row[row][row]);
964 isl_int_clear(a);
966 isl_mat_free(mat);
968 return inv;
969 error:
970 isl_mat_free(mat);
971 isl_mat_free(inv);
972 return NULL;
975 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
977 struct isl_mat *transpose = NULL;
978 int i, j;
980 if (!mat)
981 return NULL;
983 if (mat->n_col == mat->n_row) {
984 mat = isl_mat_cow(mat);
985 if (!mat)
986 return NULL;
987 for (i = 0; i < mat->n_row; ++i)
988 for (j = i + 1; j < mat->n_col; ++j)
989 isl_int_swap(mat->row[i][j], mat->row[j][i]);
990 return mat;
992 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
993 if (!transpose)
994 goto error;
995 for (i = 0; i < mat->n_row; ++i)
996 for (j = 0; j < mat->n_col; ++j)
997 isl_int_set(transpose->row[j][i], mat->row[i][j]);
998 isl_mat_free(mat);
999 return transpose;
1000 error:
1001 isl_mat_free(mat);
1002 return NULL;
1005 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
1007 int r;
1009 mat = isl_mat_cow(mat);
1010 if (!mat)
1011 return NULL;
1012 isl_assert(mat->ctx, i < mat->n_col, goto error);
1013 isl_assert(mat->ctx, j < mat->n_col, goto error);
1015 for (r = 0; r < mat->n_row; ++r)
1016 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1017 return mat;
1018 error:
1019 isl_mat_free(mat);
1020 return NULL;
1023 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
1025 isl_int *t;
1027 if (!mat)
1028 return NULL;
1029 mat = isl_mat_cow(mat);
1030 if (!mat)
1031 return NULL;
1032 t = mat->row[i];
1033 mat->row[i] = mat->row[j];
1034 mat->row[j] = t;
1035 return mat;
1038 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1039 __isl_take isl_mat *right)
1041 int i, j, k;
1042 struct isl_mat *prod;
1044 if (!left || !right)
1045 goto error;
1046 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1047 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1048 if (!prod)
1049 goto error;
1050 if (left->n_col == 0) {
1051 for (i = 0; i < prod->n_row; ++i)
1052 isl_seq_clr(prod->row[i], prod->n_col);
1053 isl_mat_free(left);
1054 isl_mat_free(right);
1055 return prod;
1057 for (i = 0; i < prod->n_row; ++i) {
1058 for (j = 0; j < prod->n_col; ++j) {
1059 isl_int_mul(prod->row[i][j],
1060 left->row[i][0], right->row[0][j]);
1061 for (k = 1; k < left->n_col; ++k)
1062 isl_int_addmul(prod->row[i][j],
1063 left->row[i][k], right->row[k][j]);
1066 isl_mat_free(left);
1067 isl_mat_free(right);
1068 return prod;
1069 error:
1070 isl_mat_free(left);
1071 isl_mat_free(right);
1072 return NULL;
1075 /* Replace the variables x in the rows q by x' given by x = M x',
1076 * with M the matrix mat.
1078 * If the number of new variables is greater than the original
1079 * number of variables, then the rows q have already been
1080 * preextended. If the new number is smaller, then the coefficients
1081 * of the divs, which are not changed, need to be shifted down.
1082 * The row q may be the equalities, the inequalities or the
1083 * div expressions. In the latter case, has_div is true and
1084 * we need to take into account the extra denominator column.
1086 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1087 unsigned n_div, int has_div, struct isl_mat *mat)
1089 int i;
1090 struct isl_mat *t;
1091 int e;
1093 if (mat->n_col >= mat->n_row)
1094 e = 0;
1095 else
1096 e = mat->n_row - mat->n_col;
1097 if (has_div)
1098 for (i = 0; i < n; ++i)
1099 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1100 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1101 t = isl_mat_product(t, mat);
1102 if (!t)
1103 return -1;
1104 for (i = 0; i < n; ++i) {
1105 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1106 isl_seq_cpy(q[i] + has_div + t->n_col,
1107 q[i] + has_div + t->n_col + e, n_div);
1108 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1110 isl_mat_free(t);
1111 return 0;
1114 /* Replace the variables x in bset by x' given by x = M x', with
1115 * M the matrix mat.
1117 * If there are fewer variables x' then there are x, then we perform
1118 * the transformation in place, which that, in principle,
1119 * this frees up some extra variables as the number
1120 * of columns remains constant, but we would have to extend
1121 * the div array too as the number of rows in this array is assumed
1122 * to be equal to extra.
1124 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1125 struct isl_mat *mat)
1127 struct isl_ctx *ctx;
1129 if (!bset || !mat)
1130 goto error;
1132 ctx = bset->ctx;
1133 bset = isl_basic_set_cow(bset);
1134 if (!bset)
1135 goto error;
1137 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1138 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1139 isl_assert(ctx, mat->n_col > 0, goto error);
1141 if (mat->n_col > mat->n_row) {
1142 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1143 if (!bset)
1144 goto error;
1145 } else if (mat->n_col < mat->n_row) {
1146 bset->dim = isl_space_cow(bset->dim);
1147 if (!bset->dim)
1148 goto error;
1149 bset->dim->n_out -= mat->n_row - mat->n_col;
1152 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1153 isl_mat_copy(mat)) < 0)
1154 goto error;
1156 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1157 isl_mat_copy(mat)) < 0)
1158 goto error;
1160 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1161 goto error2;
1163 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1164 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1165 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1166 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1167 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1169 bset = isl_basic_set_simplify(bset);
1170 bset = isl_basic_set_finalize(bset);
1172 return bset;
1173 error:
1174 isl_mat_free(mat);
1175 error2:
1176 isl_basic_set_free(bset);
1177 return NULL;
1180 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1182 struct isl_ctx *ctx;
1183 int i;
1185 set = isl_set_cow(set);
1186 if (!set)
1187 return NULL;
1189 ctx = set->ctx;
1190 for (i = 0; i < set->n; ++i) {
1191 set->p[i] = isl_basic_set_preimage(set->p[i],
1192 isl_mat_copy(mat));
1193 if (!set->p[i])
1194 goto error;
1196 if (mat->n_col != mat->n_row) {
1197 set->dim = isl_space_cow(set->dim);
1198 if (!set->dim)
1199 goto error;
1200 set->dim->n_out += mat->n_col;
1201 set->dim->n_out -= mat->n_row;
1203 isl_mat_free(mat);
1204 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1205 return set;
1206 error:
1207 isl_set_free(set);
1208 isl_mat_free(mat);
1209 return NULL;
1212 /* Replace the variables x starting at pos in the rows q
1213 * by x' with x = M x' with M the matrix mat.
1214 * That is, replace the corresponding coefficients c by c M.
1216 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1217 unsigned pos, __isl_take isl_mat *mat)
1219 int i;
1220 isl_mat *t;
1222 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1223 t = isl_mat_product(t, mat);
1224 if (!t)
1225 return -1;
1226 for (i = 0; i < n; ++i)
1227 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1228 isl_mat_free(t);
1229 return 0;
1232 /* Replace the variables x of type "type" starting at "first" in "bset"
1233 * by x' with x = M x' with M the matrix trans.
1234 * That is, replace the corresponding coefficients c by c M.
1236 * The transformation matrix should be a square matrix.
1238 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1239 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1240 __isl_take isl_mat *trans)
1242 isl_ctx *ctx;
1243 unsigned pos;
1245 bset = isl_basic_set_cow(bset);
1246 if (!bset || !trans)
1247 goto error;
1249 ctx = isl_basic_set_get_ctx(bset);
1250 if (trans->n_row != trans->n_col)
1251 isl_die(trans->ctx, isl_error_invalid,
1252 "expecting square transformation matrix", goto error);
1253 if (first + trans->n_row > isl_basic_set_dim(bset, type))
1254 isl_die(trans->ctx, isl_error_invalid,
1255 "oversized transformation matrix", goto error);
1257 pos = isl_basic_set_offset(bset, type) + first;
1259 if (transform(ctx, bset->eq, bset->n_eq, pos, isl_mat_copy(trans)) < 0)
1260 goto error;
1261 if (transform(ctx, bset->ineq, bset->n_ineq, pos,
1262 isl_mat_copy(trans)) < 0)
1263 goto error;
1264 if (transform(ctx, bset->div, bset->n_div, 1 + pos,
1265 isl_mat_copy(trans)) < 0)
1266 goto error;
1268 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1269 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1271 isl_mat_free(trans);
1272 return bset;
1273 error:
1274 isl_mat_free(trans);
1275 isl_basic_set_free(bset);
1276 return NULL;
1279 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1281 int i, j;
1283 if (!mat) {
1284 fprintf(out, "%*snull mat\n", indent, "");
1285 return;
1288 if (mat->n_row == 0)
1289 fprintf(out, "%*s[]\n", indent, "");
1291 for (i = 0; i < mat->n_row; ++i) {
1292 if (!i)
1293 fprintf(out, "%*s[[", indent, "");
1294 else
1295 fprintf(out, "%*s[", indent+1, "");
1296 for (j = 0; j < mat->n_col; ++j) {
1297 if (j)
1298 fprintf(out, ",");
1299 isl_int_print(out, mat->row[i][j], 0);
1301 if (i == mat->n_row-1)
1302 fprintf(out, "]]\n");
1303 else
1304 fprintf(out, "]\n");
1308 void isl_mat_dump(__isl_keep isl_mat *mat)
1310 isl_mat_print_internal(mat, stderr, 0);
1313 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1315 int r;
1317 if (n == 0)
1318 return mat;
1320 mat = isl_mat_cow(mat);
1321 if (!mat)
1322 return NULL;
1324 if (col != mat->n_col-n) {
1325 for (r = 0; r < mat->n_row; ++r)
1326 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1327 mat->n_col - col - n);
1329 mat->n_col -= n;
1330 return mat;
1333 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1335 int r;
1337 mat = isl_mat_cow(mat);
1338 if (!mat)
1339 return NULL;
1341 for (r = row; r+n < mat->n_row; ++r)
1342 mat->row[r] = mat->row[r+n];
1344 mat->n_row -= n;
1345 return mat;
1348 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1349 unsigned col, unsigned n)
1351 isl_mat *ext;
1353 if (!mat)
1354 return NULL;
1355 if (n == 0)
1356 return mat;
1358 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1359 if (!ext)
1360 goto error;
1362 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1363 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1364 col + n, col, mat->n_col - col);
1366 isl_mat_free(mat);
1367 return ext;
1368 error:
1369 isl_mat_free(mat);
1370 return NULL;
1373 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1374 unsigned first, unsigned n)
1376 int i;
1378 if (!mat)
1379 return NULL;
1380 mat = isl_mat_insert_cols(mat, first, n);
1381 if (!mat)
1382 return NULL;
1384 for (i = 0; i < mat->n_row; ++i)
1385 isl_seq_clr(mat->row[i] + first, n);
1387 return mat;
1390 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1392 if (!mat)
1393 return NULL;
1395 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1398 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1399 unsigned row, unsigned n)
1401 isl_mat *ext;
1403 if (!mat)
1404 return NULL;
1405 if (n == 0)
1406 return mat;
1408 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1409 if (!ext)
1410 goto error;
1412 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1413 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1414 mat->n_row - row, 0, 0, mat->n_col);
1416 isl_mat_free(mat);
1417 return ext;
1418 error:
1419 isl_mat_free(mat);
1420 return NULL;
1423 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1425 if (!mat)
1426 return NULL;
1428 return isl_mat_insert_rows(mat, mat->n_row, n);
1431 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1432 unsigned row, unsigned n)
1434 int i;
1436 mat = isl_mat_insert_rows(mat, row, n);
1437 if (!mat)
1438 return NULL;
1440 for (i = 0; i < n; ++i)
1441 isl_seq_clr(mat->row[row + i], mat->n_col);
1443 return mat;
1446 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1448 if (!mat)
1449 return NULL;
1451 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1454 void isl_mat_col_submul(struct isl_mat *mat,
1455 int dst_col, isl_int f, int src_col)
1457 int i;
1459 for (i = 0; i < mat->n_row; ++i)
1460 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1463 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1465 int i;
1467 if (!mat)
1468 return;
1470 for (i = 0; i < mat->n_row; ++i)
1471 isl_int_add(mat->row[i][dst_col],
1472 mat->row[i][dst_col], mat->row[i][src_col]);
1475 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1477 int i;
1479 for (i = 0; i < mat->n_row; ++i)
1480 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1483 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1485 int r;
1486 struct isl_mat *H = NULL, *Q = NULL;
1488 if (!M)
1489 return NULL;
1491 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1492 M->n_row = row;
1493 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1494 M->n_row = M->n_col;
1495 if (!H)
1496 goto error;
1497 for (r = 0; r < row; ++r)
1498 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1499 for (r = row; r < M->n_row; ++r)
1500 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1501 isl_mat_free(H);
1502 isl_mat_free(Q);
1503 return M;
1504 error:
1505 isl_mat_free(H);
1506 isl_mat_free(Q);
1507 isl_mat_free(M);
1508 return NULL;
1511 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1512 __isl_take isl_mat *bot)
1514 struct isl_mat *mat;
1516 if (!top || !bot)
1517 goto error;
1519 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1520 if (top->n_row == 0) {
1521 isl_mat_free(top);
1522 return bot;
1524 if (bot->n_row == 0) {
1525 isl_mat_free(bot);
1526 return top;
1529 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1530 if (!mat)
1531 goto error;
1532 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1533 0, 0, mat->n_col);
1534 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1535 0, 0, mat->n_col);
1536 isl_mat_free(top);
1537 isl_mat_free(bot);
1538 return mat;
1539 error:
1540 isl_mat_free(top);
1541 isl_mat_free(bot);
1542 return NULL;
1545 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1547 int i;
1549 if (!mat1 || !mat2)
1550 return -1;
1552 if (mat1->n_row != mat2->n_row)
1553 return 0;
1555 if (mat1->n_col != mat2->n_col)
1556 return 0;
1558 for (i = 0; i < mat1->n_row; ++i)
1559 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1560 return 0;
1562 return 1;
1565 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1567 struct isl_mat *mat;
1569 if (!vec)
1570 return NULL;
1571 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1572 if (!mat)
1573 goto error;
1575 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1577 isl_vec_free(vec);
1578 return mat;
1579 error:
1580 isl_vec_free(vec);
1581 return NULL;
1584 /* Return a copy of row "row" of "mat" as an isl_vec.
1586 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1588 isl_vec *v;
1590 if (!mat)
1591 return NULL;
1592 if (row >= mat->n_row)
1593 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1594 return NULL);
1596 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1597 if (!v)
1598 return NULL;
1599 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1601 return v;
1604 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1605 __isl_take isl_vec *bot)
1607 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1610 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1611 unsigned dst_col, unsigned src_col, unsigned n)
1613 isl_mat *res;
1615 if (!mat)
1616 return NULL;
1617 if (n == 0 || dst_col == src_col)
1618 return mat;
1620 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1621 if (!res)
1622 goto error;
1624 if (dst_col < src_col) {
1625 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1626 0, 0, dst_col);
1627 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1628 dst_col, src_col, n);
1629 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1630 dst_col + n, dst_col, src_col - dst_col);
1631 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1632 src_col + n, src_col + n,
1633 res->n_col - src_col - n);
1634 } else {
1635 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1636 0, 0, src_col);
1637 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1638 src_col, src_col + n, dst_col - src_col);
1639 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1640 dst_col, src_col, n);
1641 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1642 dst_col + n, dst_col + n,
1643 res->n_col - dst_col - n);
1645 isl_mat_free(mat);
1647 return res;
1648 error:
1649 isl_mat_free(mat);
1650 return NULL;
1653 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1655 int i;
1656 isl_int g;
1658 isl_int_set_si(*gcd, 0);
1659 if (!mat)
1660 return;
1662 isl_int_init(g);
1663 for (i = 0; i < mat->n_row; ++i) {
1664 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1665 isl_int_gcd(*gcd, *gcd, g);
1667 isl_int_clear(g);
1670 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1672 int i;
1674 if (isl_int_is_one(m))
1675 return mat;
1677 mat = isl_mat_cow(mat);
1678 if (!mat)
1679 return NULL;
1681 for (i = 0; i < mat->n_row; ++i)
1682 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1684 return mat;
1687 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1688 isl_int m)
1690 if (isl_int_is_one(m))
1691 return mat;
1693 mat = isl_mat_cow(mat);
1694 if (!mat)
1695 return NULL;
1697 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1699 return mat;
1702 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1704 isl_int gcd;
1706 if (!mat)
1707 return NULL;
1709 isl_int_init(gcd);
1710 isl_mat_gcd(mat, &gcd);
1711 mat = isl_mat_scale_down(mat, gcd);
1712 isl_int_clear(gcd);
1714 return mat;
1717 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1719 mat = isl_mat_cow(mat);
1720 if (!mat)
1721 return NULL;
1723 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1725 return mat;
1728 /* Number of initial non-zero columns.
1730 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1732 int i;
1734 if (!mat)
1735 return -1;
1737 for (i = 0; i < mat->n_col; ++i)
1738 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1739 break;
1741 return i;