2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 #include "isl_map_private.h"
15 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
16 unsigned n_row, unsigned n_col)
21 mat = isl_alloc_type(ctx, struct isl_mat);
26 mat->block = isl_blk_alloc(ctx, n_row * n_col);
27 if (isl_blk_is_error(mat->block))
29 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
33 for (i = 0; i < n_row; ++i)
34 mat->row[i] = mat->block.data + i * n_col;
46 isl_blk_free(ctx, mat->block);
51 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
52 unsigned n_row, unsigned n_col)
60 if (mat->max_col >= n_col && mat->n_row >= n_row) {
61 if (mat->n_col < n_col)
66 if (mat->max_col < n_col) {
67 struct isl_mat *new_mat;
69 if (n_row < mat->n_row)
71 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
74 for (i = 0; i < mat->n_row; ++i)
75 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
80 mat = isl_mat_cow(mat);
84 old = mat->block.data;
85 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
86 if (isl_blk_is_error(mat->block))
88 mat->row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
92 for (i = 0; i < mat->n_row; ++i)
93 mat->row[i] = mat->block.data + (mat->row[i] - old);
94 for (i = mat->n_row; i < n_row; ++i)
95 mat->row[i] = mat->block.data + i * mat->max_col;
97 if (mat->n_col < n_col)
106 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
107 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
112 mat = isl_alloc_type(ctx, struct isl_mat);
115 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
118 for (i = 0; i < n_row; ++i)
119 mat->row[i] = row[first_row+i] + first_col;
125 mat->block = isl_blk_empty();
126 mat->flags = ISL_MAT_BORROWED;
133 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
134 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
138 for (i = 0; i < n_row; ++i)
139 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
142 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
143 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
147 for (i = 0; i < n_row; ++i)
148 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
151 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
160 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
163 struct isl_mat *mat2;
167 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
170 for (i = 0; i < mat->n_row; ++i)
171 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
175 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
177 struct isl_mat *mat2;
181 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
184 mat2 = isl_mat_dup(mat);
189 void isl_mat_free(struct isl_mat *mat)
197 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
198 isl_blk_free(mat->ctx, mat->block);
199 isl_ctx_deref(mat->ctx);
204 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
209 mat = isl_mat_alloc(ctx, n_row, n_row);
212 for (i = 0; i < n_row; ++i) {
213 isl_seq_clr(mat->row[i], i);
214 isl_int_set_si(mat->row[i][i], 1);
215 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
221 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
224 struct isl_vec *prod;
229 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
231 prod = isl_vec_alloc(mat->ctx, mat->n_row);
235 for (i = 0; i < prod->size; ++i)
236 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
237 &prod->block.data[i]);
247 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
248 __isl_take isl_vec *vec)
250 struct isl_mat *vec_mat;
255 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
258 for (i = 0; i < vec->size; ++i)
259 isl_int_set(vec_mat->row[i][0], vec->el[i]);
260 vec_mat = isl_mat_inverse_product(mat, vec_mat);
264 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
266 for (i = 0; i < vec->size; ++i)
267 isl_int_set(vec->el[i], vec_mat->row[i][0]);
268 isl_mat_free(vec_mat);
276 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
279 struct isl_vec *prod;
284 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
286 prod = isl_vec_alloc(mat->ctx, mat->n_col);
290 for (i = 0; i < prod->size; ++i) {
291 isl_int_set_si(prod->el[i], 0);
292 for (j = 0; j < vec->size; ++j)
293 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
304 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
305 struct isl_mat *right)
313 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
314 isl_assert(left->ctx, left->n_row >= 1, goto error);
315 isl_assert(left->ctx, left->n_col >= 1, goto error);
316 isl_assert(left->ctx, right->n_col >= 1, goto error);
317 isl_assert(left->ctx,
318 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
320 isl_assert(left->ctx,
321 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
324 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
327 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
328 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
329 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
331 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
332 for (i = 1; i < sum->n_row; ++i) {
333 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
334 isl_int_addmul(sum->row[i][0],
335 right->row[0][0], right->row[i][0]);
336 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
338 isl_seq_scale(sum->row[i]+left->n_col,
339 right->row[i]+1, right->row[0][0],
343 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
344 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
354 static void exchange(struct isl_mat *M, struct isl_mat **U,
355 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
358 for (r = row; r < M->n_row; ++r)
359 isl_int_swap(M->row[r][i], M->row[r][j]);
361 for (r = 0; r < (*U)->n_row; ++r)
362 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
365 isl_mat_swap_rows(*Q, i, j);
368 static void subtract(struct isl_mat *M, struct isl_mat **U,
369 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
372 for (r = row; r < M->n_row; ++r)
373 isl_int_submul(M->row[r][j], m, M->row[r][i]);
375 for (r = 0; r < (*U)->n_row; ++r)
376 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
379 for (r = 0; r < (*Q)->n_col; ++r)
380 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
384 static void oppose(struct isl_mat *M, struct isl_mat **U,
385 struct isl_mat **Q, unsigned row, unsigned col)
388 for (r = row; r < M->n_row; ++r)
389 isl_int_neg(M->row[r][col], M->row[r][col]);
391 for (r = 0; r < (*U)->n_row; ++r)
392 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
395 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
398 /* Given matrix M, compute
403 * with U and Q unimodular matrices and H a matrix in column echelon form
404 * such that on each echelon row the entries in the non-echelon column
405 * are non-negative (if neg == 0) or non-positive (if neg == 1)
406 * and stricly smaller (in absolute value) than the entries in the echelon
408 * If U or Q are NULL, then these matrices are not computed.
410 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
411 struct isl_mat **U, struct isl_mat **Q)
426 *U = isl_mat_identity(M->ctx, M->n_col);
431 *Q = isl_mat_identity(M->ctx, M->n_col);
438 for (row = 0; row < M->n_row; ++row) {
440 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
445 exchange(M, U, Q, row, first, col);
446 if (isl_int_is_neg(M->row[row][col]))
447 oppose(M, U, Q, row, col);
449 while ((off = isl_seq_first_non_zero(M->row[row]+first,
450 M->n_col-first)) != -1) {
452 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
453 subtract(M, U, Q, row, col, first, c);
454 if (!isl_int_is_zero(M->row[row][first]))
455 exchange(M, U, Q, row, first, col);
459 for (i = 0; i < col; ++i) {
460 if (isl_int_is_zero(M->row[row][i]))
463 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
465 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
466 if (isl_int_is_zero(c))
468 subtract(M, U, Q, row, col, i, c);
487 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
490 struct isl_mat *U = NULL;
493 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
497 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
498 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
503 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
506 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
516 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
519 struct isl_mat *mat2;
523 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
526 isl_int_set_si(mat2->row[0][0], 1);
527 isl_seq_clr(mat2->row[0]+1, mat->n_col);
528 for (i = 0; i < mat->n_row; ++i) {
529 isl_int_set_si(mat2->row[1+i][0], 0);
530 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
539 /* Given two matrices M1 and M2, return the block matrix
544 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
545 __isl_take isl_mat *mat2)
553 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
554 mat1->n_col + mat2->n_col);
557 for (i = 0; i < mat1->n_row; ++i) {
558 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
559 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
561 for (i = 0; i < mat2->n_row; ++i) {
562 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
563 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
564 mat2->row[i], mat2->n_col);
575 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
579 for (i = 0; i < n_row; ++i)
580 if (!isl_int_is_zero(row[i][col]))
585 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
587 int i, min = row_first_non_zero(row, n_row, col);
590 for (i = min + 1; i < n_row; ++i) {
591 if (isl_int_is_zero(row[i][col]))
593 if (isl_int_abs_lt(row[i][col], row[min][col]))
599 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
600 unsigned i, unsigned j)
602 left = isl_mat_swap_rows(left, i, j);
603 right = isl_mat_swap_rows(right, i, j);
606 static void inv_oppose(
607 struct isl_mat *left, struct isl_mat *right, unsigned row)
609 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
610 isl_seq_neg(right->row[row], right->row[row], right->n_col);
613 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
614 unsigned row, unsigned i, isl_int m)
617 isl_seq_combine(left->row[i]+row,
618 left->ctx->one, left->row[i]+row,
619 m, left->row[row]+row,
621 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
622 m, right->row[row], right->n_col);
625 /* Compute inv(left)*right
627 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
628 struct isl_mat *right)
636 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
637 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
639 if (left->n_row == 0) {
644 left = isl_mat_cow(left);
645 right = isl_mat_cow(right);
651 for (row = 0; row < left->n_row; ++row) {
652 int pivot, first, i, off;
653 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
657 isl_assert(left->ctx, pivot >= 0, goto error);
661 inv_exchange(left, right, pivot, row);
662 if (isl_int_is_neg(left->row[row][row]))
663 inv_oppose(left, right, row);
665 while ((off = row_first_non_zero(left->row+first,
666 left->n_row-first, row)) != -1) {
668 isl_int_fdiv_q(a, left->row[first][row],
669 left->row[row][row]);
670 inv_subtract(left, right, row, first, a);
671 if (!isl_int_is_zero(left->row[first][row]))
672 inv_exchange(left, right, row, first);
676 for (i = 0; i < row; ++i) {
677 if (isl_int_is_zero(left->row[i][row]))
679 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
680 isl_int_divexact(b, left->row[i][row], a);
681 isl_int_divexact(a, left->row[row][row], a);
683 isl_seq_combine(left->row[i] + i,
685 b, left->row[row] + i,
687 isl_seq_combine(right->row[i], a, right->row[i],
688 b, right->row[row], right->n_col);
693 isl_int_set(a, left->row[0][0]);
694 for (row = 1; row < left->n_row; ++row)
695 isl_int_lcm(a, a, left->row[row][row]);
696 if (isl_int_is_zero(a)){
698 isl_assert(left->ctx, 0, goto error);
700 for (row = 0; row < left->n_row; ++row) {
701 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
702 if (isl_int_is_one(left->row[row][row]))
704 isl_seq_scale(right->row[row], right->row[row],
705 left->row[row][row], right->n_col);
717 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
721 for (i = 0; i < mat->n_row; ++i)
722 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
725 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
726 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
732 for (i = 0; i < mat->n_row; ++i) {
733 isl_int_mul(tmp, m1, mat->row[i][src1]);
734 isl_int_addmul(tmp, m2, mat->row[i][src2]);
735 isl_int_set(mat->row[i][dst], tmp);
740 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
746 mat = isl_mat_cow(mat);
750 inv = isl_mat_identity(mat->ctx, mat->n_col);
751 inv = isl_mat_cow(inv);
757 for (row = 0; row < mat->n_row; ++row) {
758 int pivot, first, i, off;
759 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
763 isl_assert(mat->ctx, pivot >= 0, goto error);
767 exchange(mat, &inv, NULL, row, pivot, row);
768 if (isl_int_is_neg(mat->row[row][row]))
769 oppose(mat, &inv, NULL, row, row);
771 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
772 mat->n_col-first)) != -1) {
774 isl_int_fdiv_q(a, mat->row[row][first],
776 subtract(mat, &inv, NULL, row, row, first, a);
777 if (!isl_int_is_zero(mat->row[row][first]))
778 exchange(mat, &inv, NULL, row, row, first);
782 for (i = 0; i < row; ++i) {
783 if (isl_int_is_zero(mat->row[row][i]))
785 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
786 isl_int_divexact(b, mat->row[row][i], a);
787 isl_int_divexact(a, mat->row[row][row], a);
789 isl_mat_col_combine(mat, i, a, i, b, row);
790 isl_mat_col_combine(inv, i, a, i, b, row);
795 isl_int_set(a, mat->row[0][0]);
796 for (row = 1; row < mat->n_row; ++row)
797 isl_int_lcm(a, a, mat->row[row][row]);
798 if (isl_int_is_zero(a)){
802 for (row = 0; row < mat->n_row; ++row) {
803 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
804 if (isl_int_is_one(mat->row[row][row]))
806 isl_mat_col_scale(inv, row, mat->row[row][row]);
818 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
820 struct isl_mat *transpose = NULL;
823 if (mat->n_col == mat->n_row) {
824 mat = isl_mat_cow(mat);
827 for (i = 0; i < mat->n_row; ++i)
828 for (j = i + 1; j < mat->n_col; ++j)
829 isl_int_swap(mat->row[i][j], mat->row[j][i]);
832 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
835 for (i = 0; i < mat->n_row; ++i)
836 for (j = 0; j < mat->n_col; ++j)
837 isl_int_set(transpose->row[j][i], mat->row[i][j]);
845 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
849 mat = isl_mat_cow(mat);
852 isl_assert(mat->ctx, i < mat->n_col, goto error);
853 isl_assert(mat->ctx, j < mat->n_col, goto error);
855 for (r = 0; r < mat->n_row; ++r)
856 isl_int_swap(mat->row[r][i], mat->row[r][j]);
863 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
869 mat = isl_mat_cow(mat);
873 mat->row[i] = mat->row[j];
878 struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
881 struct isl_mat *prod;
885 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
886 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
889 if (left->n_col == 0) {
890 for (i = 0; i < prod->n_row; ++i)
891 isl_seq_clr(prod->row[i], prod->n_col);
894 for (i = 0; i < prod->n_row; ++i) {
895 for (j = 0; j < prod->n_col; ++j) {
896 isl_int_mul(prod->row[i][j],
897 left->row[i][0], right->row[0][j]);
898 for (k = 1; k < left->n_col; ++k)
899 isl_int_addmul(prod->row[i][j],
900 left->row[i][k], right->row[k][j]);
912 /* Replace the variables x in the rows q by x' given by x = M x',
913 * with M the matrix mat.
915 * If the number of new variables is greater than the original
916 * number of variables, then the rows q have already been
917 * preextended. If the new number is smaller, then the coefficients
918 * of the divs, which are not changed, need to be shifted down.
919 * The row q may be the equalities, the inequalities or the
920 * div expressions. In the latter case, has_div is true and
921 * we need to take into account the extra denominator column.
923 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
924 unsigned n_div, int has_div, struct isl_mat *mat)
930 if (mat->n_col >= mat->n_row)
933 e = mat->n_row - mat->n_col;
935 for (i = 0; i < n; ++i)
936 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
937 t = isl_mat_sub_alloc(mat->ctx, q, 0, n, has_div, mat->n_row);
938 t = isl_mat_product(t, mat);
941 for (i = 0; i < n; ++i) {
942 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
943 isl_seq_cpy(q[i] + has_div + t->n_col,
944 q[i] + has_div + t->n_col + e, n_div);
945 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
951 /* Replace the variables x in bset by x' given by x = M x', with
954 * If there are fewer variables x' then there are x, then we perform
955 * the transformation in place, which that, in principle,
956 * this frees up some extra variables as the number
957 * of columns remains constant, but we would have to extend
958 * the div array too as the number of rows in this array is assumed
959 * to be equal to extra.
961 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
970 bset = isl_basic_set_cow(bset);
974 isl_assert(ctx, bset->dim->nparam == 0, goto error);
975 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
976 isl_assert(ctx, mat->n_col > 0, goto error);
978 if (mat->n_col > mat->n_row)
979 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
981 else if (mat->n_col < mat->n_row) {
982 bset->dim = isl_dim_cow(bset->dim);
985 bset->dim->n_out -= mat->n_row - mat->n_col;
988 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
989 isl_mat_copy(mat)) < 0)
992 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
993 isl_mat_copy(mat)) < 0)
996 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
999 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1000 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1001 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1002 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1003 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1005 bset = isl_basic_set_simplify(bset);
1006 bset = isl_basic_set_finalize(bset);
1012 isl_basic_set_free(bset);
1016 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1018 struct isl_ctx *ctx;
1021 set = isl_set_cow(set);
1026 for (i = 0; i < set->n; ++i) {
1027 set->p[i] = isl_basic_set_preimage(set->p[i],
1032 if (mat->n_col != mat->n_row) {
1033 set->dim = isl_dim_cow(set->dim);
1036 set->dim->n_out += mat->n_col;
1037 set->dim->n_out -= mat->n_row;
1040 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1048 void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
1053 fprintf(out, "%*snull mat\n", indent, "");
1057 if (mat->n_row == 0)
1058 fprintf(out, "%*s[]\n", indent, "");
1060 for (i = 0; i < mat->n_row; ++i) {
1062 fprintf(out, "%*s[[", indent, "");
1064 fprintf(out, "%*s[", indent+1, "");
1065 for (j = 0; j < mat->n_col; ++j) {
1068 isl_int_print(out, mat->row[i][j], 0);
1070 if (i == mat->n_row-1)
1071 fprintf(out, "]]\n");
1073 fprintf(out, "]\n");
1077 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1081 mat = isl_mat_cow(mat);
1085 if (col != mat->n_col-n) {
1086 for (r = 0; r < mat->n_row; ++r)
1087 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1088 mat->n_col - col - n);
1094 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1098 mat = isl_mat_cow(mat);
1102 for (r = row; r+n < mat->n_row; ++r)
1103 mat->row[r] = mat->row[r+n];
1109 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1110 unsigned col, unsigned n)
1119 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1123 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1124 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1125 col + n, col, mat->n_col - col);
1134 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1135 unsigned row, unsigned n)
1144 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1148 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1149 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1150 mat->n_row - row, 0, 0, mat->n_col);
1159 void isl_mat_col_submul(struct isl_mat *mat,
1160 int dst_col, isl_int f, int src_col)
1164 for (i = 0; i < mat->n_row; ++i)
1165 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1168 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1172 for (i = 0; i < mat->n_row; ++i)
1173 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1176 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1179 struct isl_mat *H = NULL, *Q = NULL;
1184 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1186 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1187 M->n_row = M->n_col;
1190 for (r = 0; r < row; ++r)
1191 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1192 for (r = row; r < M->n_row; ++r)
1193 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1204 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1205 __isl_take isl_mat *bot)
1207 struct isl_mat *mat;
1212 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1213 if (top->n_row == 0) {
1217 if (bot->n_row == 0) {
1222 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1225 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1227 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1238 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1245 if (mat1->n_row != mat2->n_row)
1248 if (mat1->n_col != mat2->n_col)
1251 for (i = 0; i < mat1->n_row; ++i)
1252 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1258 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1260 struct isl_mat *mat;
1264 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1268 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1277 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1278 __isl_take isl_vec *bot)
1280 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1283 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1284 unsigned dst_col, unsigned src_col, unsigned n)
1290 if (n == 0 || dst_col == src_col)
1293 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1297 if (dst_col < src_col) {
1298 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1300 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1301 dst_col, src_col, n);
1302 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1303 dst_col + n, dst_col, src_col - dst_col);
1304 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1305 src_col + n, src_col + n,
1306 res->n_col - src_col - n);
1308 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1310 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1311 src_col, src_col + n, dst_col - src_col);
1312 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1313 dst_col, src_col, n);
1314 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1315 dst_col + n, dst_col + n,
1316 res->n_col - dst_col - n);
1326 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1331 isl_int_set_si(*gcd, 0);
1336 for (i = 0; i < mat->n_row; ++i) {
1337 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1338 isl_int_gcd(*gcd, *gcd, g);
1343 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1350 for (i = 0; i < mat->n_row; ++i)
1351 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1356 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1364 isl_mat_gcd(mat, &gcd);
1365 mat = isl_mat_scale_down(mat, gcd);