isl_ast_build_expr.c: ast_expr_from_pw_aff: delay construction of expression
[isl.git] / isl_mat.c
blob483eaf078e9d8c03c6411b40643c6cedd6ffc2ef
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 /* Calculate the product of two matrices.
1040 * This function is optimized for operand matrices that contain many zeros and
1041 * skips multiplications where we know one of the operands is zero.
1043 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1044 __isl_take isl_mat *right)
1046 int i, j, k;
1047 struct isl_mat *prod;
1049 if (!left || !right)
1050 goto error;
1051 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1052 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1053 if (!prod)
1054 goto error;
1055 if (left->n_col == 0) {
1056 for (i = 0; i < prod->n_row; ++i)
1057 isl_seq_clr(prod->row[i], prod->n_col);
1058 isl_mat_free(left);
1059 isl_mat_free(right);
1060 return prod;
1062 for (i = 0; i < prod->n_row; ++i) {
1063 for (j = 0; j < prod->n_col; ++j)
1064 isl_int_mul(prod->row[i][j],
1065 left->row[i][0], right->row[0][j]);
1066 for (k = 1; k < left->n_col; ++k) {
1067 if (isl_int_is_zero(left->row[i][k]))
1068 continue;
1069 for (j = 0; j < prod->n_col; ++j)
1070 isl_int_addmul(prod->row[i][j],
1071 left->row[i][k], right->row[k][j]);
1074 isl_mat_free(left);
1075 isl_mat_free(right);
1076 return prod;
1077 error:
1078 isl_mat_free(left);
1079 isl_mat_free(right);
1080 return NULL;
1083 /* Replace the variables x in the rows q by x' given by x = M x',
1084 * with M the matrix mat.
1086 * If the number of new variables is greater than the original
1087 * number of variables, then the rows q have already been
1088 * preextended. If the new number is smaller, then the coefficients
1089 * of the divs, which are not changed, need to be shifted down.
1090 * The row q may be the equalities, the inequalities or the
1091 * div expressions. In the latter case, has_div is true and
1092 * we need to take into account the extra denominator column.
1094 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1095 unsigned n_div, int has_div, struct isl_mat *mat)
1097 int i;
1098 struct isl_mat *t;
1099 int e;
1101 if (mat->n_col >= mat->n_row)
1102 e = 0;
1103 else
1104 e = mat->n_row - mat->n_col;
1105 if (has_div)
1106 for (i = 0; i < n; ++i)
1107 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1108 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1109 t = isl_mat_product(t, mat);
1110 if (!t)
1111 return -1;
1112 for (i = 0; i < n; ++i) {
1113 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1114 isl_seq_cpy(q[i] + has_div + t->n_col,
1115 q[i] + has_div + t->n_col + e, n_div);
1116 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1118 isl_mat_free(t);
1119 return 0;
1122 /* Replace the variables x in bset by x' given by x = M x', with
1123 * M the matrix mat.
1125 * If there are fewer variables x' then there are x, then we perform
1126 * the transformation in place, which means that, in principle,
1127 * this frees up some extra variables as the number
1128 * of columns remains constant, but we would have to extend
1129 * the div array too as the number of rows in this array is assumed
1130 * to be equal to extra.
1132 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1133 struct isl_mat *mat)
1135 struct isl_ctx *ctx;
1137 if (!bset || !mat)
1138 goto error;
1140 ctx = bset->ctx;
1141 bset = isl_basic_set_cow(bset);
1142 if (!bset)
1143 goto error;
1145 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1146 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1147 isl_assert(ctx, mat->n_col > 0, goto error);
1149 if (mat->n_col > mat->n_row) {
1150 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1151 if (!bset)
1152 goto error;
1153 } else if (mat->n_col < mat->n_row) {
1154 bset->dim = isl_space_cow(bset->dim);
1155 if (!bset->dim)
1156 goto error;
1157 bset->dim->n_out -= mat->n_row - mat->n_col;
1160 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1161 isl_mat_copy(mat)) < 0)
1162 goto error;
1164 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1165 isl_mat_copy(mat)) < 0)
1166 goto error;
1168 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1169 goto error2;
1171 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1172 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1173 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1174 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1175 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1177 bset = isl_basic_set_simplify(bset);
1178 bset = isl_basic_set_finalize(bset);
1180 return bset;
1181 error:
1182 isl_mat_free(mat);
1183 error2:
1184 isl_basic_set_free(bset);
1185 return NULL;
1188 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1190 int i;
1192 set = isl_set_cow(set);
1193 if (!set)
1194 return NULL;
1196 for (i = 0; i < set->n; ++i) {
1197 set->p[i] = isl_basic_set_preimage(set->p[i],
1198 isl_mat_copy(mat));
1199 if (!set->p[i])
1200 goto error;
1202 if (mat->n_col != mat->n_row) {
1203 set->dim = isl_space_cow(set->dim);
1204 if (!set->dim)
1205 goto error;
1206 set->dim->n_out += mat->n_col;
1207 set->dim->n_out -= mat->n_row;
1209 isl_mat_free(mat);
1210 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1211 return set;
1212 error:
1213 isl_set_free(set);
1214 isl_mat_free(mat);
1215 return NULL;
1218 /* Replace the variables x starting at pos in the rows q
1219 * by x' with x = M x' with M the matrix mat.
1220 * That is, replace the corresponding coefficients c by c M.
1222 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1223 unsigned pos, __isl_take isl_mat *mat)
1225 int i;
1226 isl_mat *t;
1228 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1229 t = isl_mat_product(t, mat);
1230 if (!t)
1231 return -1;
1232 for (i = 0; i < n; ++i)
1233 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1234 isl_mat_free(t);
1235 return 0;
1238 /* Replace the variables x of type "type" starting at "first" in "bmap"
1239 * by x' with x = M x' with M the matrix trans.
1240 * That is, replace the corresponding coefficients c by c M.
1242 * The transformation matrix should be a square matrix.
1244 __isl_give isl_basic_map *isl_basic_map_transform_dims(
1245 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
1246 __isl_take isl_mat *trans)
1248 isl_ctx *ctx;
1249 unsigned pos;
1251 bmap = isl_basic_map_cow(bmap);
1252 if (!bmap || !trans)
1253 goto error;
1255 ctx = isl_basic_map_get_ctx(bmap);
1256 if (trans->n_row != trans->n_col)
1257 isl_die(trans->ctx, isl_error_invalid,
1258 "expecting square transformation matrix", goto error);
1259 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
1260 isl_die(trans->ctx, isl_error_invalid,
1261 "oversized transformation matrix", goto error);
1263 pos = isl_basic_map_offset(bmap, type) + first;
1265 if (transform(ctx, bmap->eq, bmap->n_eq, pos, isl_mat_copy(trans)) < 0)
1266 goto error;
1267 if (transform(ctx, bmap->ineq, bmap->n_ineq, pos,
1268 isl_mat_copy(trans)) < 0)
1269 goto error;
1270 if (transform(ctx, bmap->div, bmap->n_div, 1 + pos,
1271 isl_mat_copy(trans)) < 0)
1272 goto error;
1274 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1275 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1277 isl_mat_free(trans);
1278 return bmap;
1279 error:
1280 isl_mat_free(trans);
1281 isl_basic_map_free(bmap);
1282 return NULL;
1285 /* Replace the variables x of type "type" starting at "first" in "bset"
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_set *isl_basic_set_transform_dims(
1292 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1293 __isl_take isl_mat *trans)
1295 return isl_basic_map_transform_dims(bset, type, first, trans);
1298 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1300 int i, j;
1302 if (!mat) {
1303 fprintf(out, "%*snull mat\n", indent, "");
1304 return;
1307 if (mat->n_row == 0)
1308 fprintf(out, "%*s[]\n", indent, "");
1310 for (i = 0; i < mat->n_row; ++i) {
1311 if (!i)
1312 fprintf(out, "%*s[[", indent, "");
1313 else
1314 fprintf(out, "%*s[", indent+1, "");
1315 for (j = 0; j < mat->n_col; ++j) {
1316 if (j)
1317 fprintf(out, ",");
1318 isl_int_print(out, mat->row[i][j], 0);
1320 if (i == mat->n_row-1)
1321 fprintf(out, "]]\n");
1322 else
1323 fprintf(out, "]\n");
1327 void isl_mat_dump(__isl_keep isl_mat *mat)
1329 isl_mat_print_internal(mat, stderr, 0);
1332 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1334 int r;
1336 if (n == 0)
1337 return mat;
1339 mat = isl_mat_cow(mat);
1340 if (!mat)
1341 return NULL;
1343 if (col != mat->n_col-n) {
1344 for (r = 0; r < mat->n_row; ++r)
1345 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1346 mat->n_col - col - n);
1348 mat->n_col -= n;
1349 return mat;
1352 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1354 int r;
1356 mat = isl_mat_cow(mat);
1357 if (!mat)
1358 return NULL;
1360 for (r = row; r+n < mat->n_row; ++r)
1361 mat->row[r] = mat->row[r+n];
1363 mat->n_row -= n;
1364 return mat;
1367 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1368 unsigned col, unsigned n)
1370 isl_mat *ext;
1372 if (!mat)
1373 return NULL;
1374 if (n == 0)
1375 return mat;
1377 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1378 if (!ext)
1379 goto error;
1381 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1382 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1383 col + n, col, mat->n_col - col);
1385 isl_mat_free(mat);
1386 return ext;
1387 error:
1388 isl_mat_free(mat);
1389 return NULL;
1392 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1393 unsigned first, unsigned n)
1395 int i;
1397 if (!mat)
1398 return NULL;
1399 mat = isl_mat_insert_cols(mat, first, n);
1400 if (!mat)
1401 return NULL;
1403 for (i = 0; i < mat->n_row; ++i)
1404 isl_seq_clr(mat->row[i] + first, n);
1406 return mat;
1409 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1411 if (!mat)
1412 return NULL;
1414 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1417 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1418 unsigned row, unsigned n)
1420 isl_mat *ext;
1422 if (!mat)
1423 return NULL;
1424 if (n == 0)
1425 return mat;
1427 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1428 if (!ext)
1429 goto error;
1431 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1432 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1433 mat->n_row - row, 0, 0, mat->n_col);
1435 isl_mat_free(mat);
1436 return ext;
1437 error:
1438 isl_mat_free(mat);
1439 return NULL;
1442 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1444 if (!mat)
1445 return NULL;
1447 return isl_mat_insert_rows(mat, mat->n_row, n);
1450 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1451 unsigned row, unsigned n)
1453 int i;
1455 mat = isl_mat_insert_rows(mat, row, n);
1456 if (!mat)
1457 return NULL;
1459 for (i = 0; i < n; ++i)
1460 isl_seq_clr(mat->row[row + i], mat->n_col);
1462 return mat;
1465 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1467 if (!mat)
1468 return NULL;
1470 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1473 void isl_mat_col_submul(struct isl_mat *mat,
1474 int dst_col, isl_int f, int src_col)
1476 int i;
1478 for (i = 0; i < mat->n_row; ++i)
1479 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1482 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1484 int i;
1486 if (!mat)
1487 return;
1489 for (i = 0; i < mat->n_row; ++i)
1490 isl_int_add(mat->row[i][dst_col],
1491 mat->row[i][dst_col], mat->row[i][src_col]);
1494 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1496 int i;
1498 for (i = 0; i < mat->n_row; ++i)
1499 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1502 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1504 int r;
1505 struct isl_mat *H = NULL, *Q = NULL;
1507 if (!M)
1508 return NULL;
1510 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1511 M->n_row = row;
1512 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1513 M->n_row = M->n_col;
1514 if (!H)
1515 goto error;
1516 for (r = 0; r < row; ++r)
1517 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1518 for (r = row; r < M->n_row; ++r)
1519 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1520 isl_mat_free(H);
1521 isl_mat_free(Q);
1522 return M;
1523 error:
1524 isl_mat_free(H);
1525 isl_mat_free(Q);
1526 isl_mat_free(M);
1527 return NULL;
1530 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1531 __isl_take isl_mat *bot)
1533 struct isl_mat *mat;
1535 if (!top || !bot)
1536 goto error;
1538 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1539 if (top->n_row == 0) {
1540 isl_mat_free(top);
1541 return bot;
1543 if (bot->n_row == 0) {
1544 isl_mat_free(bot);
1545 return top;
1548 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1549 if (!mat)
1550 goto error;
1551 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1552 0, 0, mat->n_col);
1553 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1554 0, 0, mat->n_col);
1555 isl_mat_free(top);
1556 isl_mat_free(bot);
1557 return mat;
1558 error:
1559 isl_mat_free(top);
1560 isl_mat_free(bot);
1561 return NULL;
1564 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1566 int i;
1568 if (!mat1 || !mat2)
1569 return -1;
1571 if (mat1->n_row != mat2->n_row)
1572 return 0;
1574 if (mat1->n_col != mat2->n_col)
1575 return 0;
1577 for (i = 0; i < mat1->n_row; ++i)
1578 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1579 return 0;
1581 return 1;
1584 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1586 struct isl_mat *mat;
1588 if (!vec)
1589 return NULL;
1590 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1591 if (!mat)
1592 goto error;
1594 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1596 isl_vec_free(vec);
1597 return mat;
1598 error:
1599 isl_vec_free(vec);
1600 return NULL;
1603 /* Return a copy of row "row" of "mat" as an isl_vec.
1605 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1607 isl_vec *v;
1609 if (!mat)
1610 return NULL;
1611 if (row >= mat->n_row)
1612 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1613 return NULL);
1615 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1616 if (!v)
1617 return NULL;
1618 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1620 return v;
1623 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1624 __isl_take isl_vec *bot)
1626 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1629 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1630 unsigned dst_col, unsigned src_col, unsigned n)
1632 isl_mat *res;
1634 if (!mat)
1635 return NULL;
1636 if (n == 0 || dst_col == src_col)
1637 return mat;
1639 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1640 if (!res)
1641 goto error;
1643 if (dst_col < src_col) {
1644 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1645 0, 0, dst_col);
1646 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1647 dst_col, src_col, n);
1648 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1649 dst_col + n, dst_col, src_col - dst_col);
1650 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1651 src_col + n, src_col + n,
1652 res->n_col - src_col - n);
1653 } else {
1654 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1655 0, 0, src_col);
1656 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1657 src_col, src_col + n, dst_col - src_col);
1658 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1659 dst_col, src_col, n);
1660 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1661 dst_col + n, dst_col + n,
1662 res->n_col - dst_col - n);
1664 isl_mat_free(mat);
1666 return res;
1667 error:
1668 isl_mat_free(mat);
1669 return NULL;
1672 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1674 int i;
1675 isl_int g;
1677 isl_int_set_si(*gcd, 0);
1678 if (!mat)
1679 return;
1681 isl_int_init(g);
1682 for (i = 0; i < mat->n_row; ++i) {
1683 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1684 isl_int_gcd(*gcd, *gcd, g);
1686 isl_int_clear(g);
1689 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1691 int i;
1693 if (isl_int_is_one(m))
1694 return mat;
1696 mat = isl_mat_cow(mat);
1697 if (!mat)
1698 return NULL;
1700 for (i = 0; i < mat->n_row; ++i)
1701 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1703 return mat;
1706 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1707 isl_int m)
1709 if (isl_int_is_one(m))
1710 return mat;
1712 mat = isl_mat_cow(mat);
1713 if (!mat)
1714 return NULL;
1716 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1718 return mat;
1721 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1723 isl_int gcd;
1725 if (!mat)
1726 return NULL;
1728 isl_int_init(gcd);
1729 isl_mat_gcd(mat, &gcd);
1730 mat = isl_mat_scale_down(mat, gcd);
1731 isl_int_clear(gcd);
1733 return mat;
1736 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1738 mat = isl_mat_cow(mat);
1739 if (!mat)
1740 return NULL;
1742 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1744 return mat;
1747 /* Number of initial non-zero columns.
1749 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1751 int i;
1753 if (!mat)
1754 return -1;
1756 for (i = 0; i < mat->n_col; ++i)
1757 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1758 break;
1760 return i;