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
10 #include <isl_ctx_private.h>
13 #include <isl_mat_private.h>
14 #include "isl_map_private.h"
15 #include <isl_dim_private.h>
17 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
18 unsigned n_row, unsigned n_col)
23 mat = isl_alloc_type(ctx, struct isl_mat);
28 mat->block = isl_blk_alloc(ctx, n_row * n_col);
29 if (isl_blk_is_error(mat->block))
31 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
35 for (i = 0; i < n_row; ++i)
36 mat->row[i] = mat->block.data + i * n_col;
48 isl_blk_free(ctx, mat->block);
53 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
54 unsigned n_row, unsigned n_col)
63 if (mat->max_col >= n_col && mat->n_row >= n_row) {
64 if (mat->n_col < n_col)
69 if (mat->max_col < n_col) {
70 struct isl_mat *new_mat;
72 if (n_row < mat->n_row)
74 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
77 for (i = 0; i < mat->n_row; ++i)
78 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
83 mat = isl_mat_cow(mat);
87 old = mat->block.data;
88 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
89 if (isl_blk_is_error(mat->block))
91 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
96 for (i = 0; i < mat->n_row; ++i)
97 mat->row[i] = mat->block.data + (mat->row[i] - old);
98 for (i = mat->n_row; i < n_row; ++i)
99 mat->row[i] = mat->block.data + i * mat->max_col;
101 if (mat->n_col < n_col)
110 __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
111 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
116 mat = isl_alloc_type(ctx, struct isl_mat);
119 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
122 for (i = 0; i < n_row; ++i)
123 mat->row[i] = row[first_row+i] + first_col;
129 mat->block = isl_blk_empty();
130 mat->flags = ISL_MAT_BORROWED;
137 __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
138 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
142 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
146 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
147 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
151 for (i = 0; i < n_row; ++i)
152 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
155 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
156 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
160 for (i = 0; i < n_row; ++i)
161 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
164 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
173 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
176 struct isl_mat *mat2;
180 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
183 for (i = 0; i < mat->n_row; ++i)
184 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
188 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
190 struct isl_mat *mat2;
194 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
197 mat2 = isl_mat_dup(mat);
202 void isl_mat_free(struct isl_mat *mat)
210 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
211 isl_blk_free(mat->ctx, mat->block);
212 isl_ctx_deref(mat->ctx);
217 int isl_mat_rows(__isl_keep isl_mat *mat)
219 return mat ? mat->n_row : -1;
222 int isl_mat_cols(__isl_keep isl_mat *mat)
224 return mat ? mat->n_col : -1;
227 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
231 if (row < 0 || row >= mat->n_row)
232 isl_die(mat->ctx, isl_error_invalid, "row out of range",
234 if (col < 0 || col >= mat->n_col)
235 isl_die(mat->ctx, isl_error_invalid, "column out of range",
237 isl_int_set(*v, mat->row[row][col]);
241 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
242 int row, int col, isl_int v)
244 mat = isl_mat_cow(mat);
247 if (row < 0 || row >= mat->n_row)
248 isl_die(mat->ctx, isl_error_invalid, "row out of range",
250 if (col < 0 || col >= mat->n_col)
251 isl_die(mat->ctx, isl_error_invalid, "column out of range",
253 isl_int_set(mat->row[row][col], v);
260 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
261 int row, int col, int v)
263 mat = isl_mat_cow(mat);
266 if (row < 0 || row >= mat->n_row)
267 isl_die(mat->ctx, isl_error_invalid, "row out of range",
269 if (col < 0 || col >= mat->n_col)
270 isl_die(mat->ctx, isl_error_invalid, "column out of range",
272 isl_int_set_si(mat->row[row][col], v);
279 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
284 mat = isl_mat_alloc(ctx, n_row, n_row);
287 for (i = 0; i < n_row; ++i) {
288 isl_seq_clr(mat->row[i], i);
289 isl_int_set_si(mat->row[i][i], 1);
290 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
296 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
299 struct isl_vec *prod;
304 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
306 prod = isl_vec_alloc(mat->ctx, mat->n_row);
310 for (i = 0; i < prod->size; ++i)
311 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
312 &prod->block.data[i]);
322 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
323 __isl_take isl_vec *vec)
325 struct isl_mat *vec_mat;
330 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
333 for (i = 0; i < vec->size; ++i)
334 isl_int_set(vec_mat->row[i][0], vec->el[i]);
335 vec_mat = isl_mat_inverse_product(mat, vec_mat);
339 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
341 for (i = 0; i < vec->size; ++i)
342 isl_int_set(vec->el[i], vec_mat->row[i][0]);
343 isl_mat_free(vec_mat);
351 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
354 struct isl_vec *prod;
359 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
361 prod = isl_vec_alloc(mat->ctx, mat->n_col);
365 for (i = 0; i < prod->size; ++i) {
366 isl_int_set_si(prod->el[i], 0);
367 for (j = 0; j < vec->size; ++j)
368 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
379 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
380 struct isl_mat *right)
388 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
389 isl_assert(left->ctx, left->n_row >= 1, goto error);
390 isl_assert(left->ctx, left->n_col >= 1, goto error);
391 isl_assert(left->ctx, right->n_col >= 1, goto error);
392 isl_assert(left->ctx,
393 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
395 isl_assert(left->ctx,
396 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
399 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
402 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
403 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
404 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
406 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
407 for (i = 1; i < sum->n_row; ++i) {
408 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
409 isl_int_addmul(sum->row[i][0],
410 right->row[0][0], right->row[i][0]);
411 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
413 isl_seq_scale(sum->row[i]+left->n_col,
414 right->row[i]+1, right->row[0][0],
418 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
419 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
429 static void exchange(struct isl_mat *M, struct isl_mat **U,
430 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
433 for (r = row; r < M->n_row; ++r)
434 isl_int_swap(M->row[r][i], M->row[r][j]);
436 for (r = 0; r < (*U)->n_row; ++r)
437 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
440 isl_mat_swap_rows(*Q, i, j);
443 static void subtract(struct isl_mat *M, struct isl_mat **U,
444 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
447 for (r = row; r < M->n_row; ++r)
448 isl_int_submul(M->row[r][j], m, M->row[r][i]);
450 for (r = 0; r < (*U)->n_row; ++r)
451 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
454 for (r = 0; r < (*Q)->n_col; ++r)
455 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
459 static void oppose(struct isl_mat *M, struct isl_mat **U,
460 struct isl_mat **Q, unsigned row, unsigned col)
463 for (r = row; r < M->n_row; ++r)
464 isl_int_neg(M->row[r][col], M->row[r][col]);
466 for (r = 0; r < (*U)->n_row; ++r)
467 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
470 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
473 /* Given matrix M, compute
478 * with U and Q unimodular matrices and H a matrix in column echelon form
479 * such that on each echelon row the entries in the non-echelon column
480 * are non-negative (if neg == 0) or non-positive (if neg == 1)
481 * and stricly smaller (in absolute value) than the entries in the echelon
483 * If U or Q are NULL, then these matrices are not computed.
485 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
486 struct isl_mat **U, struct isl_mat **Q)
501 *U = isl_mat_identity(M->ctx, M->n_col);
506 *Q = isl_mat_identity(M->ctx, M->n_col);
513 for (row = 0; row < M->n_row; ++row) {
515 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
520 exchange(M, U, Q, row, first, col);
521 if (isl_int_is_neg(M->row[row][col]))
522 oppose(M, U, Q, row, col);
524 while ((off = isl_seq_first_non_zero(M->row[row]+first,
525 M->n_col-first)) != -1) {
527 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
528 subtract(M, U, Q, row, col, first, c);
529 if (!isl_int_is_zero(M->row[row][first]))
530 exchange(M, U, Q, row, first, col);
534 for (i = 0; i < col; ++i) {
535 if (isl_int_is_zero(M->row[row][i]))
538 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
540 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
541 if (isl_int_is_zero(c))
543 subtract(M, U, Q, row, col, i, c);
563 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
566 struct isl_mat *U = NULL;
569 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
573 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
574 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
579 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
582 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
592 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
595 struct isl_mat *mat2;
599 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
602 isl_int_set_si(mat2->row[0][0], 1);
603 isl_seq_clr(mat2->row[0]+1, mat->n_col);
604 for (i = 0; i < mat->n_row; ++i) {
605 isl_int_set_si(mat2->row[1+i][0], 0);
606 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
615 /* Given two matrices M1 and M2, return the block matrix
620 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
621 __isl_take isl_mat *mat2)
629 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
630 mat1->n_col + mat2->n_col);
633 for (i = 0; i < mat1->n_row; ++i) {
634 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
635 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
637 for (i = 0; i < mat2->n_row; ++i) {
638 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
639 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
640 mat2->row[i], mat2->n_col);
651 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
655 for (i = 0; i < n_row; ++i)
656 if (!isl_int_is_zero(row[i][col]))
661 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
663 int i, min = row_first_non_zero(row, n_row, col);
666 for (i = min + 1; i < n_row; ++i) {
667 if (isl_int_is_zero(row[i][col]))
669 if (isl_int_abs_lt(row[i][col], row[min][col]))
675 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
676 unsigned i, unsigned j)
678 left = isl_mat_swap_rows(left, i, j);
679 right = isl_mat_swap_rows(right, i, j);
682 static void inv_oppose(
683 struct isl_mat *left, struct isl_mat *right, unsigned row)
685 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
686 isl_seq_neg(right->row[row], right->row[row], right->n_col);
689 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
690 unsigned row, unsigned i, isl_int m)
693 isl_seq_combine(left->row[i]+row,
694 left->ctx->one, left->row[i]+row,
695 m, left->row[row]+row,
697 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
698 m, right->row[row], right->n_col);
701 /* Compute inv(left)*right
703 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
704 struct isl_mat *right)
712 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
713 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
715 if (left->n_row == 0) {
720 left = isl_mat_cow(left);
721 right = isl_mat_cow(right);
727 for (row = 0; row < left->n_row; ++row) {
728 int pivot, first, i, off;
729 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
733 isl_assert(left->ctx, pivot >= 0, goto error);
737 inv_exchange(left, right, pivot, row);
738 if (isl_int_is_neg(left->row[row][row]))
739 inv_oppose(left, right, row);
741 while ((off = row_first_non_zero(left->row+first,
742 left->n_row-first, row)) != -1) {
744 isl_int_fdiv_q(a, left->row[first][row],
745 left->row[row][row]);
746 inv_subtract(left, right, row, first, a);
747 if (!isl_int_is_zero(left->row[first][row]))
748 inv_exchange(left, right, row, first);
752 for (i = 0; i < row; ++i) {
753 if (isl_int_is_zero(left->row[i][row]))
755 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
756 isl_int_divexact(b, left->row[i][row], a);
757 isl_int_divexact(a, left->row[row][row], a);
759 isl_seq_combine(left->row[i] + i,
761 b, left->row[row] + i,
763 isl_seq_combine(right->row[i], a, right->row[i],
764 b, right->row[row], right->n_col);
769 isl_int_set(a, left->row[0][0]);
770 for (row = 1; row < left->n_row; ++row)
771 isl_int_lcm(a, a, left->row[row][row]);
772 if (isl_int_is_zero(a)){
774 isl_assert(left->ctx, 0, goto error);
776 for (row = 0; row < left->n_row; ++row) {
777 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
778 if (isl_int_is_one(left->row[row][row]))
780 isl_seq_scale(right->row[row], right->row[row],
781 left->row[row][row], right->n_col);
793 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
797 for (i = 0; i < mat->n_row; ++i)
798 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
801 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
802 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
808 for (i = 0; i < mat->n_row; ++i) {
809 isl_int_mul(tmp, m1, mat->row[i][src1]);
810 isl_int_addmul(tmp, m2, mat->row[i][src2]);
811 isl_int_set(mat->row[i][dst], tmp);
816 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
822 mat = isl_mat_cow(mat);
826 inv = isl_mat_identity(mat->ctx, mat->n_col);
827 inv = isl_mat_cow(inv);
833 for (row = 0; row < mat->n_row; ++row) {
834 int pivot, first, i, off;
835 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
839 isl_assert(mat->ctx, pivot >= 0, goto error);
843 exchange(mat, &inv, NULL, row, pivot, row);
844 if (isl_int_is_neg(mat->row[row][row]))
845 oppose(mat, &inv, NULL, row, row);
847 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
848 mat->n_col-first)) != -1) {
850 isl_int_fdiv_q(a, mat->row[row][first],
852 subtract(mat, &inv, NULL, row, row, first, a);
853 if (!isl_int_is_zero(mat->row[row][first]))
854 exchange(mat, &inv, NULL, row, row, first);
858 for (i = 0; i < row; ++i) {
859 if (isl_int_is_zero(mat->row[row][i]))
861 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
862 isl_int_divexact(b, mat->row[row][i], a);
863 isl_int_divexact(a, mat->row[row][row], a);
865 isl_mat_col_combine(mat, i, a, i, b, row);
866 isl_mat_col_combine(inv, i, a, i, b, row);
871 isl_int_set(a, mat->row[0][0]);
872 for (row = 1; row < mat->n_row; ++row)
873 isl_int_lcm(a, a, mat->row[row][row]);
874 if (isl_int_is_zero(a)){
878 for (row = 0; row < mat->n_row; ++row) {
879 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
880 if (isl_int_is_one(mat->row[row][row]))
882 isl_mat_col_scale(inv, row, mat->row[row][row]);
895 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
897 struct isl_mat *transpose = NULL;
900 if (mat->n_col == mat->n_row) {
901 mat = isl_mat_cow(mat);
904 for (i = 0; i < mat->n_row; ++i)
905 for (j = i + 1; j < mat->n_col; ++j)
906 isl_int_swap(mat->row[i][j], mat->row[j][i]);
909 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
912 for (i = 0; i < mat->n_row; ++i)
913 for (j = 0; j < mat->n_col; ++j)
914 isl_int_set(transpose->row[j][i], mat->row[i][j]);
922 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
926 mat = isl_mat_cow(mat);
929 isl_assert(mat->ctx, i < mat->n_col, goto error);
930 isl_assert(mat->ctx, j < mat->n_col, goto error);
932 for (r = 0; r < mat->n_row; ++r)
933 isl_int_swap(mat->row[r][i], mat->row[r][j]);
940 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
946 mat = isl_mat_cow(mat);
950 mat->row[i] = mat->row[j];
955 struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
958 struct isl_mat *prod;
962 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
963 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
966 if (left->n_col == 0) {
967 for (i = 0; i < prod->n_row; ++i)
968 isl_seq_clr(prod->row[i], prod->n_col);
973 for (i = 0; i < prod->n_row; ++i) {
974 for (j = 0; j < prod->n_col; ++j) {
975 isl_int_mul(prod->row[i][j],
976 left->row[i][0], right->row[0][j]);
977 for (k = 1; k < left->n_col; ++k)
978 isl_int_addmul(prod->row[i][j],
979 left->row[i][k], right->row[k][j]);
991 /* Replace the variables x in the rows q by x' given by x = M x',
992 * with M the matrix mat.
994 * If the number of new variables is greater than the original
995 * number of variables, then the rows q have already been
996 * preextended. If the new number is smaller, then the coefficients
997 * of the divs, which are not changed, need to be shifted down.
998 * The row q may be the equalities, the inequalities or the
999 * div expressions. In the latter case, has_div is true and
1000 * we need to take into account the extra denominator column.
1002 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1003 unsigned n_div, int has_div, struct isl_mat *mat)
1009 if (mat->n_col >= mat->n_row)
1012 e = mat->n_row - mat->n_col;
1014 for (i = 0; i < n; ++i)
1015 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1016 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1017 t = isl_mat_product(t, mat);
1020 for (i = 0; i < n; ++i) {
1021 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1022 isl_seq_cpy(q[i] + has_div + t->n_col,
1023 q[i] + has_div + t->n_col + e, n_div);
1024 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1030 /* Replace the variables x in bset by x' given by x = M x', with
1033 * If there are fewer variables x' then there are x, then we perform
1034 * the transformation in place, which that, in principle,
1035 * this frees up some extra variables as the number
1036 * of columns remains constant, but we would have to extend
1037 * the div array too as the number of rows in this array is assumed
1038 * to be equal to extra.
1040 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1041 struct isl_mat *mat)
1043 struct isl_ctx *ctx;
1049 bset = isl_basic_set_cow(bset);
1053 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1054 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1055 isl_assert(ctx, mat->n_col > 0, goto error);
1057 if (mat->n_col > mat->n_row) {
1058 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1061 } else if (mat->n_col < mat->n_row) {
1062 bset->dim = isl_dim_cow(bset->dim);
1065 bset->dim->n_out -= mat->n_row - mat->n_col;
1068 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1069 isl_mat_copy(mat)) < 0)
1072 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1073 isl_mat_copy(mat)) < 0)
1076 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1079 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1080 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1081 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1082 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1083 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1085 bset = isl_basic_set_simplify(bset);
1086 bset = isl_basic_set_finalize(bset);
1092 isl_basic_set_free(bset);
1096 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1098 struct isl_ctx *ctx;
1101 set = isl_set_cow(set);
1106 for (i = 0; i < set->n; ++i) {
1107 set->p[i] = isl_basic_set_preimage(set->p[i],
1112 if (mat->n_col != mat->n_row) {
1113 set->dim = isl_dim_cow(set->dim);
1116 set->dim->n_out += mat->n_col;
1117 set->dim->n_out -= mat->n_row;
1120 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1128 /* Replace the variables x starting at pos in the rows q
1129 * by x' with x = M x' with M the matrix mat.
1130 * That is, replace the corresponding coefficients c by c M.
1132 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1133 unsigned pos, __isl_take isl_mat *mat)
1138 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1139 t = isl_mat_product(t, mat);
1142 for (i = 0; i < n; ++i)
1143 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1148 /* Replace the variables x of type "type" starting at "first" in "bset"
1149 * by x' with x = M x' with M the matrix trans.
1150 * That is, replace the corresponding coefficients c by c M.
1152 * The transformation matrix should be a square matrix.
1154 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1155 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1156 __isl_take isl_mat *trans)
1161 bset = isl_basic_set_cow(bset);
1162 if (!bset || !trans)
1165 ctx = isl_basic_set_get_ctx(bset);
1166 if (trans->n_row != trans->n_col)
1167 isl_die(trans->ctx, isl_error_invalid,
1168 "expecting square transformation matrix", goto error);
1169 if (first + trans->n_row > isl_basic_set_dim(bset, type))
1170 isl_die(trans->ctx, isl_error_invalid,
1171 "oversized transformation matrix", goto error);
1173 pos = isl_basic_set_offset(bset, type) + first;
1175 if (transform(ctx, bset->eq, bset->n_eq, pos, isl_mat_copy(trans)) < 0)
1177 if (transform(ctx, bset->ineq, bset->n_ineq, pos,
1178 isl_mat_copy(trans)) < 0)
1180 if (transform(ctx, bset->div, bset->n_div, 1 + pos,
1181 isl_mat_copy(trans)) < 0)
1184 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1185 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1187 isl_mat_free(trans);
1190 isl_mat_free(trans);
1191 isl_basic_set_free(bset);
1195 void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
1200 fprintf(out, "%*snull mat\n", indent, "");
1204 if (mat->n_row == 0)
1205 fprintf(out, "%*s[]\n", indent, "");
1207 for (i = 0; i < mat->n_row; ++i) {
1209 fprintf(out, "%*s[[", indent, "");
1211 fprintf(out, "%*s[", indent+1, "");
1212 for (j = 0; j < mat->n_col; ++j) {
1215 isl_int_print(out, mat->row[i][j], 0);
1217 if (i == mat->n_row-1)
1218 fprintf(out, "]]\n");
1220 fprintf(out, "]\n");
1224 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1228 mat = isl_mat_cow(mat);
1232 if (col != mat->n_col-n) {
1233 for (r = 0; r < mat->n_row; ++r)
1234 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1235 mat->n_col - col - n);
1241 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1245 mat = isl_mat_cow(mat);
1249 for (r = row; r+n < mat->n_row; ++r)
1250 mat->row[r] = mat->row[r+n];
1256 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1257 unsigned col, unsigned n)
1266 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1270 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1271 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1272 col + n, col, mat->n_col - col);
1281 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1282 unsigned first, unsigned n)
1288 mat = isl_mat_insert_cols(mat, first, n);
1292 for (i = 0; i < mat->n_row; ++i)
1293 isl_seq_clr(mat->row[i] + first, n);
1298 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1303 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1306 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1307 unsigned row, unsigned n)
1316 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1320 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1321 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1322 mat->n_row - row, 0, 0, mat->n_col);
1331 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1336 return isl_mat_insert_rows(mat, mat->n_row, n);
1339 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1340 unsigned row, unsigned n)
1344 mat = isl_mat_insert_rows(mat, row, n);
1348 for (i = 0; i < n; ++i)
1349 isl_seq_clr(mat->row[row + i], mat->n_col);
1354 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1359 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1362 void isl_mat_col_submul(struct isl_mat *mat,
1363 int dst_col, isl_int f, int src_col)
1367 for (i = 0; i < mat->n_row; ++i)
1368 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1371 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1378 for (i = 0; i < mat->n_row; ++i)
1379 isl_int_add(mat->row[i][dst_col],
1380 mat->row[i][dst_col], mat->row[i][src_col]);
1383 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1387 for (i = 0; i < mat->n_row; ++i)
1388 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1391 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1394 struct isl_mat *H = NULL, *Q = NULL;
1399 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1401 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1402 M->n_row = M->n_col;
1405 for (r = 0; r < row; ++r)
1406 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1407 for (r = row; r < M->n_row; ++r)
1408 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1419 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1420 __isl_take isl_mat *bot)
1422 struct isl_mat *mat;
1427 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1428 if (top->n_row == 0) {
1432 if (bot->n_row == 0) {
1437 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1440 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1442 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1453 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1460 if (mat1->n_row != mat2->n_row)
1463 if (mat1->n_col != mat2->n_col)
1466 for (i = 0; i < mat1->n_row; ++i)
1467 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1473 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1475 struct isl_mat *mat;
1479 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1483 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1492 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1493 __isl_take isl_vec *bot)
1495 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1498 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1499 unsigned dst_col, unsigned src_col, unsigned n)
1505 if (n == 0 || dst_col == src_col)
1508 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1512 if (dst_col < src_col) {
1513 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1515 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1516 dst_col, src_col, n);
1517 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1518 dst_col + n, dst_col, src_col - dst_col);
1519 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1520 src_col + n, src_col + n,
1521 res->n_col - src_col - n);
1523 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1525 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1526 src_col, src_col + n, dst_col - src_col);
1527 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1528 dst_col, src_col, n);
1529 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1530 dst_col + n, dst_col + n,
1531 res->n_col - dst_col - n);
1541 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1546 isl_int_set_si(*gcd, 0);
1551 for (i = 0; i < mat->n_row; ++i) {
1552 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1553 isl_int_gcd(*gcd, *gcd, g);
1558 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1565 for (i = 0; i < mat->n_row; ++i)
1566 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1571 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1579 isl_mat_gcd(mat, &gcd);
1580 mat = isl_mat_scale_down(mat, gcd);
1586 /* Number of initial non-zero columns.
1588 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1595 for (i = 0; i < mat->n_col; ++i)
1596 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)