3 #include "isl_map_private.h"
5 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
6 unsigned n_row, unsigned n_col)
11 mat = isl_alloc_type(ctx, struct isl_mat);
16 mat->block = isl_blk_alloc(ctx, n_row * n_col);
17 if (isl_blk_is_error(mat->block))
19 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
23 for (i = 0; i < n_row; ++i)
24 mat->row[i] = mat->block.data + i * n_col;
33 isl_blk_free(ctx, mat->block);
38 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
39 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
44 mat = isl_alloc_type(ctx, struct isl_mat);
47 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
50 for (i = 0; i < n_row; ++i)
51 mat->row[i] = row[first_row+i] + first_col;
55 mat->block = isl_blk_empty();
56 mat->flags = ISL_MAT_BORROWED;
63 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
64 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
68 for (i = 0; i < n_row; ++i)
69 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
72 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
73 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
77 for (i = 0; i < n_row; ++i)
78 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
81 struct isl_mat *isl_mat_copy(struct isl_ctx *ctx, struct isl_mat *mat)
90 struct isl_mat *isl_mat_dup(struct isl_ctx *ctx, struct isl_mat *mat)
97 mat2 = isl_mat_alloc(ctx, mat->n_row, mat->n_col);
100 for (i = 0; i < mat->n_row; ++i)
101 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
105 struct isl_mat *isl_mat_cow(struct isl_ctx *ctx, struct isl_mat *mat)
107 struct isl_mat *mat2;
111 if (mat->ref == 1 && !F_ISSET(mat, ISL_MAT_BORROWED))
114 mat2 = isl_mat_dup(ctx, mat);
115 isl_mat_free(ctx, mat);
119 void isl_mat_free(struct isl_ctx *ctx, struct isl_mat *mat)
127 if (!F_ISSET(mat, ISL_MAT_BORROWED))
128 isl_blk_free(ctx, mat->block);
133 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
138 mat = isl_mat_alloc(ctx, n_row, n_row);
141 for (i = 0; i < n_row; ++i) {
142 isl_seq_clr(mat->row[i], i);
143 isl_int_set_si(mat->row[i][i], 1);
144 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
150 struct isl_vec *isl_mat_vec_product(struct isl_ctx *ctx,
151 struct isl_mat *mat, struct isl_vec *vec)
154 struct isl_vec *prod;
159 isl_assert(ctx, mat->n_col == vec->size, goto error);
161 prod = isl_vec_alloc(ctx, mat->n_row);
165 for (i = 0; i < prod->size; ++i)
166 isl_seq_inner_product(mat->row[i], vec->block.data, vec->size,
167 &prod->block.data[i]);
168 isl_mat_free(ctx, mat);
169 isl_vec_free(ctx, vec);
172 isl_mat_free(ctx, mat);
173 isl_vec_free(ctx, vec);
177 struct isl_mat *isl_mat_aff_direct_sum(struct isl_ctx *ctx,
178 struct isl_mat *left, struct isl_mat *right)
186 isl_assert(ctx, left->n_row == right->n_row, goto error);
187 isl_assert(ctx, left->n_row >= 1, goto error);
188 isl_assert(ctx, left->n_col >= 1, goto error);
189 isl_assert(ctx, right->n_col >= 1, goto error);
191 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
194 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
197 sum = isl_mat_alloc(ctx, left->n_row, left->n_col + right->n_col - 1);
200 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
201 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
202 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
204 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
205 for (i = 1; i < sum->n_row; ++i) {
206 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
207 isl_int_addmul(sum->row[i][0],
208 right->row[0][0], right->row[i][0]);
209 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
211 isl_seq_scale(sum->row[i]+left->n_col,
212 right->row[i]+1, right->row[0][0],
216 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
217 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
218 isl_mat_free(ctx, left);
219 isl_mat_free(ctx, right);
222 isl_mat_free(ctx, left);
223 isl_mat_free(ctx, right);
227 static void exchange(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
228 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
231 for (r = row; r < M->n_row; ++r)
232 isl_int_swap(M->row[r][i], M->row[r][j]);
234 for (r = 0; r < (*U)->n_row; ++r)
235 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
238 isl_mat_swap_rows(ctx, *Q, i, j);
241 static void subtract(struct isl_mat *M, struct isl_mat **U,
242 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
245 for (r = row; r < M->n_row; ++r)
246 isl_int_submul(M->row[r][j], m, M->row[r][i]);
248 for (r = 0; r < (*U)->n_row; ++r)
249 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
252 for (r = 0; r < (*Q)->n_col; ++r)
253 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
257 static void oppose(struct isl_ctx *ctx, struct isl_mat *M, struct isl_mat **U,
258 struct isl_mat **Q, unsigned row, unsigned col)
261 for (r = row; r < M->n_row; ++r)
262 isl_int_neg(M->row[r][col], M->row[r][col]);
264 for (r = 0; r < (*U)->n_row; ++r)
265 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
268 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
271 /* Given matrix M, compute
276 * with U and Q unimodular matrices and H a matrix in column echelon form
277 * such that on each echelon row the entries in the non-echelon column
278 * are non-negative (if neg == 0) or non-positive (if neg == 1)
279 * and stricly smaller (in absolute value) than the entries in the echelon
281 * If U or Q are NULL, then these matrices are not computed.
283 struct isl_mat *isl_mat_left_hermite(struct isl_ctx *ctx,
284 struct isl_mat *M, int neg, struct isl_mat **U, struct isl_mat **Q)
295 M = isl_mat_cow(ctx, M);
299 *U = isl_mat_identity(ctx, M->n_col);
304 *Q = isl_mat_identity(ctx, M->n_col);
311 for (row = 0; row < M->n_row; ++row) {
313 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
318 exchange(ctx, M, U, Q, row, first, col);
319 if (isl_int_is_neg(M->row[row][col]))
320 oppose(ctx, M, U, Q, row, col);
322 while ((off = isl_seq_first_non_zero(M->row[row]+first,
323 M->n_col-first)) != -1) {
325 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
326 subtract(M, U, Q, row, col, first, c);
327 if (!isl_int_is_zero(M->row[row][first]))
328 exchange(ctx, M, U, Q, row, first, col);
332 for (i = 0; i < col; ++i) {
333 if (isl_int_is_zero(M->row[row][i]))
336 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
338 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
339 if (isl_int_is_zero(c))
341 subtract(M, U, Q, row, col, i, c);
350 isl_mat_free(ctx, *Q);
354 isl_mat_free(ctx, *U);
360 struct isl_mat *isl_mat_lin_to_aff(struct isl_ctx *ctx, struct isl_mat *mat)
363 struct isl_mat *mat2;
367 mat2 = isl_mat_alloc(ctx, 1+mat->n_row, 1+mat->n_col);
370 isl_int_set_si(mat2->row[0][0], 1);
371 isl_seq_clr(mat2->row[0]+1, mat->n_col);
372 for (i = 0; i < mat->n_row; ++i) {
373 isl_int_set_si(mat2->row[1+i][0], 0);
374 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
376 isl_mat_free(ctx, mat);
380 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
384 for (i = 0; i < n_row; ++i)
385 if (!isl_int_is_zero(row[i][col]))
390 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
392 int i, min = row_first_non_zero(row, n_row, col);
395 for (i = min + 1; i < n_row; ++i) {
396 if (isl_int_is_zero(row[i][col]))
398 if (isl_int_abs_lt(row[i][col], row[min][col]))
404 static void inv_exchange(struct isl_ctx *ctx,
405 struct isl_mat *left, struct isl_mat *right,
406 unsigned i, unsigned j)
408 left = isl_mat_swap_rows(ctx, left, i, j);
409 right = isl_mat_swap_rows(ctx, right, i, j);
412 static void inv_oppose(struct isl_ctx *ctx,
413 struct isl_mat *left, struct isl_mat *right, unsigned row)
415 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
416 isl_seq_neg(right->row[row], right->row[row], right->n_col);
419 static void inv_subtract(struct isl_ctx *ctx,
420 struct isl_mat *left, struct isl_mat *right,
421 unsigned row, unsigned i, isl_int m)
424 isl_seq_combine(left->row[i]+row,
425 ctx->one, left->row[i]+row,
426 m, left->row[row]+row,
428 isl_seq_combine(right->row[i], ctx->one, right->row[i],
429 m, right->row[row], right->n_col);
432 /* Compute inv(left)*right
434 struct isl_mat *isl_mat_inverse_product(struct isl_ctx *ctx,
435 struct isl_mat *left, struct isl_mat *right)
443 isl_assert(ctx, left->n_row == left->n_col, goto error);
444 isl_assert(ctx, left->n_row == right->n_row, goto error);
446 if (left->n_row == 0) {
447 isl_mat_free(ctx, left);
451 left = isl_mat_cow(ctx, left);
452 right = isl_mat_cow(ctx, right);
458 for (row = 0; row < left->n_row; ++row) {
459 int pivot, first, i, off;
460 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
464 isl_assert(ctx, pivot >= 0, goto error);
468 inv_exchange(ctx, left, right, pivot, row);
469 if (isl_int_is_neg(left->row[row][row]))
470 inv_oppose(ctx, left, right, row);
472 while ((off = row_first_non_zero(left->row+first,
473 left->n_row-first, row)) != -1) {
475 isl_int_fdiv_q(a, left->row[first][row],
476 left->row[row][row]);
477 inv_subtract(ctx, left, right, row, first, a);
478 if (!isl_int_is_zero(left->row[first][row]))
479 inv_exchange(ctx, left, right, row, first);
483 for (i = 0; i < row; ++i) {
484 if (isl_int_is_zero(left->row[i][row]))
486 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
487 isl_int_divexact(b, left->row[i][row], a);
488 isl_int_divexact(a, left->row[row][row], a);
490 isl_seq_combine(left->row[i]+row,
492 b, left->row[row]+row,
494 isl_seq_combine(right->row[i], a, right->row[i],
495 b, right->row[row], right->n_col);
500 isl_int_set(a, left->row[0][0]);
501 for (row = 1; row < left->n_row; ++row)
502 isl_int_lcm(a, a, left->row[row][row]);
503 if (isl_int_is_zero(a)){
505 isl_assert(ctx, 0, goto error);
507 for (row = 0; row < left->n_row; ++row) {
508 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
509 if (isl_int_is_one(left->row[row][row]))
511 isl_seq_scale(right->row[row], right->row[row],
512 left->row[row][row], right->n_col);
516 isl_mat_free(ctx, left);
519 isl_mat_free(ctx, left);
520 isl_mat_free(ctx, right);
524 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
528 for (i = 0; i < mat->n_row; ++i)
529 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
532 isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
533 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
539 for (i = 0; i < mat->n_row; ++i) {
540 isl_int_mul(tmp, m1, mat->row[i][src1]);
541 isl_int_addmul(tmp, m2, mat->row[i][src2]);
542 isl_int_set(mat->row[i][dst], tmp);
547 struct isl_mat *isl_mat_right_inverse(struct isl_ctx *ctx,
554 mat = isl_mat_cow(ctx, mat);
558 inv = isl_mat_identity(ctx, mat->n_col);
559 inv = isl_mat_cow(ctx, inv);
565 for (row = 0; row < mat->n_row; ++row) {
566 int pivot, first, i, off;
567 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
575 exchange(ctx, mat, &inv, NULL, row, pivot, row);
576 if (isl_int_is_neg(mat->row[row][row]))
577 oppose(ctx, mat, &inv, NULL, row, row);
579 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
580 mat->n_col-first)) != -1) {
582 isl_int_fdiv_q(a, mat->row[row][first],
584 subtract(mat, &inv, NULL, row, row, first, a);
585 if (!isl_int_is_zero(mat->row[row][first]))
586 exchange(ctx, mat, &inv, NULL, row, row, first);
590 for (i = 0; i < row; ++i) {
591 if (isl_int_is_zero(mat->row[row][i]))
593 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
594 isl_int_divexact(b, mat->row[row][i], a);
595 isl_int_divexact(a, mat->row[row][row], a);
597 isl_mat_col_combine(mat, i, a, i, b, row);
598 isl_mat_col_combine(inv, i, a, i, b, row);
603 isl_int_set(a, mat->row[0][0]);
604 for (row = 1; row < mat->n_row; ++row)
605 isl_int_lcm(a, a, mat->row[row][row]);
606 if (isl_int_is_zero(a)){
610 for (row = 0; row < mat->n_row; ++row) {
611 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
612 if (isl_int_is_one(mat->row[row][row]))
614 isl_mat_col_scale(inv, row, mat->row[row][row]);
618 isl_mat_free(ctx, mat);
622 isl_mat_free(ctx, mat);
626 struct isl_mat *isl_mat_swap_rows(struct isl_ctx *ctx,
627 struct isl_mat *mat, unsigned i, unsigned j)
633 mat = isl_mat_cow(ctx, mat);
637 mat->row[i] = mat->row[j];
642 struct isl_mat *isl_mat_product(struct isl_ctx *ctx,
643 struct isl_mat *left, struct isl_mat *right)
646 struct isl_mat *prod;
650 isl_assert(ctx, left->n_col == right->n_row, goto error);
651 prod = isl_mat_alloc(ctx, left->n_row, right->n_col);
654 if (left->n_col == 0) {
655 for (i = 0; i < prod->n_row; ++i)
656 isl_seq_clr(prod->row[i], prod->n_col);
659 for (i = 0; i < prod->n_row; ++i) {
660 for (j = 0; j < prod->n_col; ++j) {
661 isl_int_mul(prod->row[i][j],
662 left->row[i][0], right->row[0][j]);
663 for (k = 1; k < left->n_col; ++k)
664 isl_int_addmul(prod->row[i][j],
665 left->row[i][k], right->row[k][j]);
668 isl_mat_free(ctx, left);
669 isl_mat_free(ctx, right);
672 isl_mat_free(ctx, left);
673 isl_mat_free(ctx, right);
677 struct isl_basic_set *isl_basic_set_preimage(struct isl_ctx *ctx,
678 struct isl_basic_set *bset, struct isl_mat *mat)
686 bset = isl_basic_set_cow(bset);
690 isl_assert(ctx, bset->nparam == 0, goto error);
691 isl_assert(ctx, bset->n_div == 0, goto error);
692 isl_assert(ctx, 1+bset->dim == mat->n_row, goto error);
694 if (mat->n_col > mat->n_row)
695 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
698 bset->dim -= mat->n_row - mat->n_col;
699 bset->extra += mat->n_row - mat->n_col;
702 t = isl_mat_sub_alloc(ctx, bset->eq, 0, bset->n_eq, 0, mat->n_row);
703 t = isl_mat_product(ctx, t, isl_mat_copy(ctx, mat));
706 for (i = 0; i < bset->n_eq; ++i) {
707 isl_seq_swp_or_cpy(bset->eq[i], t->row[i], t->n_col);
708 isl_seq_clr(bset->eq[i]+t->n_col, bset->extra);
710 isl_mat_free(ctx, t);
712 t = isl_mat_sub_alloc(ctx, bset->ineq, 0, bset->n_ineq, 0, mat->n_row);
713 t = isl_mat_product(ctx, t, mat);
716 for (i = 0; i < bset->n_ineq; ++i) {
717 isl_seq_swp_or_cpy(bset->ineq[i], t->row[i], t->n_col);
718 isl_seq_clr(bset->ineq[i]+t->n_col, bset->extra);
720 isl_mat_free(ctx, t);
722 bset = isl_basic_set_simplify(bset);
723 bset = isl_basic_set_finalize(bset);
727 isl_mat_free(ctx, mat);
729 isl_basic_set_free(bset);
733 struct isl_set *isl_set_preimage(struct isl_ctx *ctx,
734 struct isl_set *set, struct isl_mat *mat)
738 set = isl_set_cow(set);
743 for (i = 0; i < set->n; ++i) {
744 set->p[i] = isl_basic_set_preimage(ctx, set->p[i],
745 isl_mat_copy(ctx, mat));
749 set->dim += mat->n_col;
750 set->dim -= mat->n_row;
751 isl_mat_free(ctx, mat);
755 isl_mat_free(ctx, mat);
759 void isl_mat_dump(struct isl_ctx *ctx, struct isl_mat *mat,
760 FILE *out, int indent)
765 fprintf(out, "null mat\n");
770 fprintf(out, "%*s[]\n", indent, "");
772 for (i = 0; i < mat->n_row; ++i) {
774 fprintf(out, "%*s[[", indent, "");
776 fprintf(out, "%*s[", indent+1, "");
777 for (j = 0; j < mat->n_col; ++j) {
780 isl_int_print(out, mat->row[i][j]);
782 if (i == mat->n_row-1)
783 fprintf(out, "]]\n");
789 struct isl_mat *isl_mat_drop_cols(struct isl_ctx *ctx, struct isl_mat *mat,
790 unsigned col, unsigned n)
797 if (col != mat->n_col-n) {
798 for (r = 0; r < mat->n_row; ++r)
799 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
800 mat->n_col - col - n);
806 struct isl_mat *isl_mat_drop_rows(struct isl_ctx *ctx, struct isl_mat *mat,
807 unsigned row, unsigned n)
814 for (r = row; r+n < mat->n_row; ++r)
815 mat->row[r] = mat->row[r+n];