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
10 #include <isl_ctx_private.h>
11 #include <isl/space.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)
28 mat = isl_alloc_type(ctx, struct isl_mat);
33 mat->block = isl_blk_alloc(ctx, n_row * n_col);
34 if (isl_blk_is_error(mat->block))
36 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
40 for (i = 0; i < n_row; ++i)
41 mat->row[i] = mat->block.data + i * n_col;
53 isl_blk_free(ctx, mat->block);
58 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
59 unsigned n_row, unsigned n_col)
68 if (mat->max_col >= n_col && mat->n_row >= n_row) {
69 if (mat->n_col < n_col)
74 if (mat->max_col < n_col) {
75 struct isl_mat *new_mat;
77 if (n_row < mat->n_row)
79 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
82 for (i = 0; i < mat->n_row; ++i)
83 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
88 mat = isl_mat_cow(mat);
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))
96 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_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;
106 if (mat->n_col < n_col)
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)
121 mat = isl_alloc_type(ctx, struct isl_mat);
124 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
127 for (i = 0; i < n_row; ++i)
128 mat->row[i] = row[first_row+i] + first_col;
134 mat->block = isl_blk_empty();
135 mat->flags = ISL_MAT_BORROWED;
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)
147 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
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)
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)
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)
178 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
181 struct isl_mat *mat2;
185 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
188 for (i = 0; i < mat->n_row; ++i)
189 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
193 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
195 struct isl_mat *mat2;
199 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
202 mat2 = isl_mat_dup(mat);
207 void isl_mat_free(struct isl_mat *mat)
215 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
216 isl_blk_free(mat->ctx, mat->block);
217 isl_ctx_deref(mat->ctx);
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)
236 if (row < 0 || row >= mat->n_row)
237 isl_die(mat->ctx, isl_error_invalid, "row out of range",
239 if (col < 0 || col >= mat->n_col)
240 isl_die(mat->ctx, isl_error_invalid, "column out of range",
242 isl_int_set(*v, mat->row[row][col]);
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);
252 if (row < 0 || row >= mat->n_row)
253 isl_die(mat->ctx, isl_error_invalid, "row out of range",
255 if (col < 0 || col >= mat->n_col)
256 isl_die(mat->ctx, isl_error_invalid, "column out of range",
258 isl_int_set(mat->row[row][col], v);
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);
271 if (row < 0 || row >= mat->n_row)
272 isl_die(mat->ctx, isl_error_invalid, "row out of range",
274 if (col < 0 || col >= mat->n_col)
275 isl_die(mat->ctx, isl_error_invalid, "column out of range",
277 isl_int_set_si(mat->row[row][col], v);
284 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
289 mat = isl_mat_alloc(ctx, n_row, n_row);
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));
301 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
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)
311 struct isl_vec *prod;
316 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
318 prod = isl_vec_alloc(mat->ctx, mat->n_row);
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]);
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;
342 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
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);
351 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
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);
363 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
366 struct isl_vec *prod;
371 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
373 prod = isl_vec_alloc(mat->ctx, mat->n_col);
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]);
391 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
392 struct isl_mat *right)
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,
407 isl_assert(left->ctx,
408 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
411 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
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],
425 isl_seq_scale(sum->row[i]+left->n_col,
426 right->row[i]+1, right->row[0][0],
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]);
441 static void exchange(struct isl_mat *M, struct isl_mat **U,
442 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
445 for (r = row; r < M->n_row; ++r)
446 isl_int_swap(M->row[r][i], M->row[r][j]);
448 for (r = 0; r < (*U)->n_row; ++r)
449 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
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)
459 for (r = row; r < M->n_row; ++r)
460 isl_int_submul(M->row[r][j], m, M->row[r][i]);
462 for (r = 0; r < (*U)->n_row; ++r)
463 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
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)
475 for (r = row; r < M->n_row; ++r)
476 isl_int_neg(M->row[r][col], M->row[r][col]);
478 for (r = 0; r < (*U)->n_row; ++r)
479 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
482 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
485 /* Given matrix M, compute
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
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)
513 *U = isl_mat_identity(M->ctx, M->n_col);
518 *Q = isl_mat_identity(M->ctx, M->n_col);
525 for (row = 0; row < M->n_row; ++row) {
527 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-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);
536 while ((off = isl_seq_first_non_zero(M->row[row]+first,
537 M->n_col-first)) != -1) {
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);
546 for (i = 0; i < col; ++i) {
547 if (isl_int_is_zero(M->row[row][i]))
550 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
552 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
553 if (isl_int_is_zero(c))
555 subtract(M, U, Q, row, col, i, c);
575 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
578 struct isl_mat *U = NULL;
581 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
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]))
591 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
594 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
604 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
607 struct isl_mat *mat2;
611 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
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);
627 /* Given two matrices M1 and M2, return the block matrix
632 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
633 __isl_take isl_mat *mat2)
641 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
642 mat1->n_col + mat2->n_col);
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);
663 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
667 for (i = 0; i < n_row; ++i)
668 if (!isl_int_is_zero(row[i][col]))
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);
678 for (i = min + 1; i < n_row; ++i) {
679 if (isl_int_is_zero(row[i][col]))
681 if (isl_int_abs_lt(row[i][col], row[min][col]))
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)
705 isl_seq_combine(left->row[i]+row,
706 left->ctx->one, left->row[i]+row,
707 m, left->row[row]+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)
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) {
732 left = isl_mat_cow(left);
733 right = isl_mat_cow(right);
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);
745 isl_assert(left->ctx, pivot >= 0, goto error);
749 inv_exchange(left, right, pivot, row);
750 if (isl_int_is_neg(left->row[row][row]))
751 inv_oppose(left, right, row);
753 while ((off = row_first_non_zero(left->row+first,
754 left->n_row-first, row)) != -1) {
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);
764 for (i = 0; i < row; ++i) {
765 if (isl_int_is_zero(left->row[i][row]))
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);
771 isl_seq_combine(left->row[i] + i,
773 b, left->row[row] + i,
775 isl_seq_combine(right->row[i], a, right->row[i],
776 b, right->row[row], right->n_col);
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)){
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]))
792 isl_seq_scale(right->row[row], right->row[row],
793 left->row[row][row], right->n_col);
805 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
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)
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);
828 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
834 mat = isl_mat_cow(mat);
838 inv = isl_mat_identity(mat->ctx, mat->n_col);
839 inv = isl_mat_cow(inv);
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);
851 isl_assert(mat->ctx, pivot >= 0, goto error);
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);
859 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
860 mat->n_col-first)) != -1) {
862 isl_int_fdiv_q(a, mat->row[row][first],
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);
870 for (i = 0; i < row; ++i) {
871 if (isl_int_is_zero(mat->row[row][i]))
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);
877 isl_mat_col_combine(mat, i, a, i, b, row);
878 isl_mat_col_combine(inv, i, a, i, b, row);
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)){
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]))
894 isl_mat_col_scale(inv, row, mat->row[row][row]);
907 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
909 struct isl_mat *transpose = NULL;
912 if (mat->n_col == mat->n_row) {
913 mat = isl_mat_cow(mat);
916 for (i = 0; i < mat->n_row; ++i)
917 for (j = i + 1; j < mat->n_col; ++j)
918 isl_int_swap(mat->row[i][j], mat->row[j][i]);
921 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
924 for (i = 0; i < mat->n_row; ++i)
925 for (j = 0; j < mat->n_col; ++j)
926 isl_int_set(transpose->row[j][i], mat->row[i][j]);
934 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
938 mat = isl_mat_cow(mat);
941 isl_assert(mat->ctx, i < mat->n_col, goto error);
942 isl_assert(mat->ctx, j < mat->n_col, goto error);
944 for (r = 0; r < mat->n_row; ++r)
945 isl_int_swap(mat->row[r][i], mat->row[r][j]);
952 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
958 mat = isl_mat_cow(mat);
962 mat->row[i] = mat->row[j];
967 struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
970 struct isl_mat *prod;
974 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
975 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
978 if (left->n_col == 0) {
979 for (i = 0; i < prod->n_row; ++i)
980 isl_seq_clr(prod->row[i], prod->n_col);
985 for (i = 0; i < prod->n_row; ++i) {
986 for (j = 0; j < prod->n_col; ++j) {
987 isl_int_mul(prod->row[i][j],
988 left->row[i][0], right->row[0][j]);
989 for (k = 1; k < left->n_col; ++k)
990 isl_int_addmul(prod->row[i][j],
991 left->row[i][k], right->row[k][j]);
1003 /* Replace the variables x in the rows q by x' given by x = M x',
1004 * with M the matrix mat.
1006 * If the number of new variables is greater than the original
1007 * number of variables, then the rows q have already been
1008 * preextended. If the new number is smaller, then the coefficients
1009 * of the divs, which are not changed, need to be shifted down.
1010 * The row q may be the equalities, the inequalities or the
1011 * div expressions. In the latter case, has_div is true and
1012 * we need to take into account the extra denominator column.
1014 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1015 unsigned n_div, int has_div, struct isl_mat *mat)
1021 if (mat->n_col >= mat->n_row)
1024 e = mat->n_row - mat->n_col;
1026 for (i = 0; i < n; ++i)
1027 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1028 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1029 t = isl_mat_product(t, mat);
1032 for (i = 0; i < n; ++i) {
1033 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1034 isl_seq_cpy(q[i] + has_div + t->n_col,
1035 q[i] + has_div + t->n_col + e, n_div);
1036 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1042 /* Replace the variables x in bset by x' given by x = M x', with
1045 * If there are fewer variables x' then there are x, then we perform
1046 * the transformation in place, which that, in principle,
1047 * this frees up some extra variables as the number
1048 * of columns remains constant, but we would have to extend
1049 * the div array too as the number of rows in this array is assumed
1050 * to be equal to extra.
1052 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1053 struct isl_mat *mat)
1055 struct isl_ctx *ctx;
1061 bset = isl_basic_set_cow(bset);
1065 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1066 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1067 isl_assert(ctx, mat->n_col > 0, goto error);
1069 if (mat->n_col > mat->n_row) {
1070 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1073 } else if (mat->n_col < mat->n_row) {
1074 bset->dim = isl_space_cow(bset->dim);
1077 bset->dim->n_out -= mat->n_row - mat->n_col;
1080 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1081 isl_mat_copy(mat)) < 0)
1084 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1085 isl_mat_copy(mat)) < 0)
1088 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1091 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1092 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1093 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1094 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1095 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1097 bset = isl_basic_set_simplify(bset);
1098 bset = isl_basic_set_finalize(bset);
1104 isl_basic_set_free(bset);
1108 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1110 struct isl_ctx *ctx;
1113 set = isl_set_cow(set);
1118 for (i = 0; i < set->n; ++i) {
1119 set->p[i] = isl_basic_set_preimage(set->p[i],
1124 if (mat->n_col != mat->n_row) {
1125 set->dim = isl_space_cow(set->dim);
1128 set->dim->n_out += mat->n_col;
1129 set->dim->n_out -= mat->n_row;
1132 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1140 /* Replace the variables x starting at pos in the rows q
1141 * by x' with x = M x' with M the matrix mat.
1142 * That is, replace the corresponding coefficients c by c M.
1144 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1145 unsigned pos, __isl_take isl_mat *mat)
1150 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1151 t = isl_mat_product(t, mat);
1154 for (i = 0; i < n; ++i)
1155 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1160 /* Replace the variables x of type "type" starting at "first" in "bset"
1161 * by x' with x = M x' with M the matrix trans.
1162 * That is, replace the corresponding coefficients c by c M.
1164 * The transformation matrix should be a square matrix.
1166 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1167 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1168 __isl_take isl_mat *trans)
1173 bset = isl_basic_set_cow(bset);
1174 if (!bset || !trans)
1177 ctx = isl_basic_set_get_ctx(bset);
1178 if (trans->n_row != trans->n_col)
1179 isl_die(trans->ctx, isl_error_invalid,
1180 "expecting square transformation matrix", goto error);
1181 if (first + trans->n_row > isl_basic_set_dim(bset, type))
1182 isl_die(trans->ctx, isl_error_invalid,
1183 "oversized transformation matrix", goto error);
1185 pos = isl_basic_set_offset(bset, type) + first;
1187 if (transform(ctx, bset->eq, bset->n_eq, pos, isl_mat_copy(trans)) < 0)
1189 if (transform(ctx, bset->ineq, bset->n_ineq, pos,
1190 isl_mat_copy(trans)) < 0)
1192 if (transform(ctx, bset->div, bset->n_div, 1 + pos,
1193 isl_mat_copy(trans)) < 0)
1196 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1197 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1199 isl_mat_free(trans);
1202 isl_mat_free(trans);
1203 isl_basic_set_free(bset);
1207 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1212 fprintf(out, "%*snull mat\n", indent, "");
1216 if (mat->n_row == 0)
1217 fprintf(out, "%*s[]\n", indent, "");
1219 for (i = 0; i < mat->n_row; ++i) {
1221 fprintf(out, "%*s[[", indent, "");
1223 fprintf(out, "%*s[", indent+1, "");
1224 for (j = 0; j < mat->n_col; ++j) {
1227 isl_int_print(out, mat->row[i][j], 0);
1229 if (i == mat->n_row-1)
1230 fprintf(out, "]]\n");
1232 fprintf(out, "]\n");
1236 void isl_mat_dump(__isl_keep isl_mat *mat)
1238 isl_mat_print_internal(mat, stderr, 0);
1241 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1245 mat = isl_mat_cow(mat);
1249 if (col != mat->n_col-n) {
1250 for (r = 0; r < mat->n_row; ++r)
1251 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1252 mat->n_col - col - n);
1258 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1262 mat = isl_mat_cow(mat);
1266 for (r = row; r+n < mat->n_row; ++r)
1267 mat->row[r] = mat->row[r+n];
1273 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1274 unsigned col, unsigned n)
1283 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1287 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1288 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1289 col + n, col, mat->n_col - col);
1298 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1299 unsigned first, unsigned n)
1305 mat = isl_mat_insert_cols(mat, first, n);
1309 for (i = 0; i < mat->n_row; ++i)
1310 isl_seq_clr(mat->row[i] + first, n);
1315 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1320 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1323 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1324 unsigned row, unsigned n)
1333 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1337 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1338 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1339 mat->n_row - row, 0, 0, mat->n_col);
1348 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1353 return isl_mat_insert_rows(mat, mat->n_row, n);
1356 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1357 unsigned row, unsigned n)
1361 mat = isl_mat_insert_rows(mat, row, n);
1365 for (i = 0; i < n; ++i)
1366 isl_seq_clr(mat->row[row + i], mat->n_col);
1371 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1376 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1379 void isl_mat_col_submul(struct isl_mat *mat,
1380 int dst_col, isl_int f, int src_col)
1384 for (i = 0; i < mat->n_row; ++i)
1385 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1388 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1395 for (i = 0; i < mat->n_row; ++i)
1396 isl_int_add(mat->row[i][dst_col],
1397 mat->row[i][dst_col], mat->row[i][src_col]);
1400 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1404 for (i = 0; i < mat->n_row; ++i)
1405 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1408 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1411 struct isl_mat *H = NULL, *Q = NULL;
1416 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1418 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1419 M->n_row = M->n_col;
1422 for (r = 0; r < row; ++r)
1423 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1424 for (r = row; r < M->n_row; ++r)
1425 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1436 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1437 __isl_take isl_mat *bot)
1439 struct isl_mat *mat;
1444 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1445 if (top->n_row == 0) {
1449 if (bot->n_row == 0) {
1454 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1457 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1459 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1470 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1477 if (mat1->n_row != mat2->n_row)
1480 if (mat1->n_col != mat2->n_col)
1483 for (i = 0; i < mat1->n_row; ++i)
1484 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1490 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1492 struct isl_mat *mat;
1496 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1500 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1509 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1510 __isl_take isl_vec *bot)
1512 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1515 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1516 unsigned dst_col, unsigned src_col, unsigned n)
1522 if (n == 0 || dst_col == src_col)
1525 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1529 if (dst_col < src_col) {
1530 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1532 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1533 dst_col, src_col, n);
1534 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1535 dst_col + n, dst_col, src_col - dst_col);
1536 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1537 src_col + n, src_col + n,
1538 res->n_col - src_col - n);
1540 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1542 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1543 src_col, src_col + n, dst_col - src_col);
1544 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1545 dst_col, src_col, n);
1546 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1547 dst_col + n, dst_col + n,
1548 res->n_col - dst_col - n);
1558 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1563 isl_int_set_si(*gcd, 0);
1568 for (i = 0; i < mat->n_row; ++i) {
1569 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1570 isl_int_gcd(*gcd, *gcd, g);
1575 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1579 if (isl_int_is_one(m))
1582 mat = isl_mat_cow(mat);
1586 for (i = 0; i < mat->n_row; ++i)
1587 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1592 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1595 if (isl_int_is_one(m))
1598 mat = isl_mat_cow(mat);
1602 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1607 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1615 isl_mat_gcd(mat, &gcd);
1616 mat = isl_mat_scale_down(mat, gcd);
1622 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1624 mat = isl_mat_cow(mat);
1628 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1633 /* Number of initial non-zero columns.
1635 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1642 for (i = 0; i < mat->n_col; ++i)
1643 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)