isl_tab: optionally keep track of row signs
[isl.git] / isl_mat.c
blobc1ba26e41444068c22a66c1d96ba639727c6d732
1 #include "isl_dim.h"
2 #include "isl_seq.h"
3 #include "isl_mat.h"
4 #include "isl_map_private.h"
6 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
7 unsigned n_row, unsigned n_col)
9 int i;
10 struct isl_mat *mat;
12 mat = isl_alloc_type(ctx, struct isl_mat);
13 if (!mat)
14 return NULL;
16 mat->row = NULL;
17 mat->block = isl_blk_alloc(ctx, n_row * n_col);
18 if (isl_blk_is_error(mat->block))
19 goto error;
20 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
21 if (!mat->row)
22 goto error;
24 for (i = 0; i < n_row; ++i)
25 mat->row[i] = mat->block.data + i * n_col;
27 mat->ctx = ctx;
28 isl_ctx_ref(ctx);
29 mat->ref = 1;
30 mat->n_row = n_row;
31 mat->n_col = n_col;
32 mat->max_col = n_col;
33 mat->flags = 0;
35 return mat;
36 error:
37 isl_blk_free(ctx, mat->block);
38 free(mat);
39 return NULL;
42 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
43 unsigned n_row, unsigned n_col)
45 int i;
46 isl_int *old;
48 if (!mat)
49 return NULL;
51 if (mat->max_col >= n_col && mat->n_row >= n_row) {
52 if (mat->n_col < n_col)
53 mat->n_col = n_col;
54 return mat;
57 if (mat->max_col < n_col) {
58 struct isl_mat *new_mat;
60 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
61 if (!new_mat)
62 goto error;
63 for (i = 0; i < mat->n_row; ++i)
64 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
65 isl_mat_free(mat);
66 return new_mat;
69 mat = isl_mat_cow(mat);
70 if (!mat)
71 goto error;
73 assert(mat->ref == 1);
74 old = mat->block.data;
75 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
76 if (isl_blk_is_error(mat->block))
77 goto error;
78 mat->row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
79 if (!mat->row)
80 goto error;
82 for (i = 0; i < mat->n_row; ++i)
83 mat->row[i] = mat->block.data + (mat->row[i] - old);
84 for (i = mat->n_row; i < n_row; ++i)
85 mat->row[i] = mat->block.data + i * mat->max_col;
86 mat->n_row = n_row;
87 if (mat->n_col < n_col)
88 mat->n_col = n_col;
90 return mat;
91 error:
92 isl_mat_free(mat);
93 return NULL;
96 struct isl_mat *isl_mat_sub_alloc(struct isl_ctx *ctx, isl_int **row,
97 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
99 int i;
100 struct isl_mat *mat;
102 mat = isl_alloc_type(ctx, struct isl_mat);
103 if (!mat)
104 return NULL;
105 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
106 if (!mat->row)
107 goto error;
108 for (i = 0; i < n_row; ++i)
109 mat->row[i] = row[first_row+i] + first_col;
110 mat->ctx = ctx;
111 isl_ctx_ref(ctx);
112 mat->ref = 1;
113 mat->n_row = n_row;
114 mat->n_col = n_col;
115 mat->block = isl_blk_empty();
116 mat->flags = ISL_MAT_BORROWED;
117 return mat;
118 error:
119 free(mat);
120 return NULL;
123 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
124 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
126 int i;
128 for (i = 0; i < n_row; ++i)
129 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
132 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
133 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
135 int i;
137 for (i = 0; i < n_row; ++i)
138 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
141 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
143 if (!mat)
144 return NULL;
146 mat->ref++;
147 return mat;
150 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
152 int i;
153 struct isl_mat *mat2;
155 if (!mat)
156 return NULL;
157 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
158 if (!mat2)
159 return NULL;
160 for (i = 0; i < mat->n_row; ++i)
161 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
162 return mat2;
165 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
167 struct isl_mat *mat2;
168 if (!mat)
169 return NULL;
171 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
172 return mat;
174 mat2 = isl_mat_dup(mat);
175 isl_mat_free(mat);
176 return mat2;
179 void isl_mat_free(struct isl_mat *mat)
181 if (!mat)
182 return;
184 if (--mat->ref > 0)
185 return;
187 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
188 isl_blk_free(mat->ctx, mat->block);
189 isl_ctx_deref(mat->ctx);
190 free(mat->row);
191 free(mat);
194 struct isl_mat *isl_mat_identity(struct isl_ctx *ctx, unsigned n_row)
196 int i;
197 struct isl_mat *mat;
199 mat = isl_mat_alloc(ctx, n_row, n_row);
200 if (!mat)
201 return NULL;
202 for (i = 0; i < n_row; ++i) {
203 isl_seq_clr(mat->row[i], i);
204 isl_int_set_si(mat->row[i][i], 1);
205 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
208 return mat;
211 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
213 int i;
214 struct isl_vec *prod;
216 if (!mat || !vec)
217 goto error;
219 isl_assert(ctx, mat->n_col == vec->size, goto error);
221 prod = isl_vec_alloc(mat->ctx, mat->n_row);
222 if (!prod)
223 goto error;
225 for (i = 0; i < prod->size; ++i)
226 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
227 &prod->block.data[i]);
228 isl_mat_free(mat);
229 isl_vec_free(vec);
230 return prod;
231 error:
232 isl_mat_free(mat);
233 isl_vec_free(vec);
234 return NULL;
237 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
238 struct isl_mat *right)
240 int i;
241 struct isl_mat *sum;
243 if (!left || !right)
244 goto error;
246 isl_assert(ctx, left->n_row == right->n_row, goto error);
247 isl_assert(ctx, left->n_row >= 1, goto error);
248 isl_assert(ctx, left->n_col >= 1, goto error);
249 isl_assert(ctx, right->n_col >= 1, goto error);
250 isl_assert(ctx,
251 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
252 goto error);
253 isl_assert(ctx,
254 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
255 goto error);
257 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
258 if (!sum)
259 goto error;
260 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
261 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
262 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
264 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
265 for (i = 1; i < sum->n_row; ++i) {
266 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
267 isl_int_addmul(sum->row[i][0],
268 right->row[0][0], right->row[i][0]);
269 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
270 left->n_col-1);
271 isl_seq_scale(sum->row[i]+left->n_col,
272 right->row[i]+1, right->row[0][0],
273 right->n_col-1);
276 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
277 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
278 isl_mat_free(left);
279 isl_mat_free(right);
280 return sum;
281 error:
282 isl_mat_free(left);
283 isl_mat_free(right);
284 return NULL;
287 static void exchange(struct isl_mat *M, struct isl_mat **U,
288 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
290 int r;
291 for (r = row; r < M->n_row; ++r)
292 isl_int_swap(M->row[r][i], M->row[r][j]);
293 if (U) {
294 for (r = 0; r < (*U)->n_row; ++r)
295 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
297 if (Q)
298 isl_mat_swap_rows(*Q, i, j);
301 static void subtract(struct isl_mat *M, struct isl_mat **U,
302 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
304 int r;
305 for (r = row; r < M->n_row; ++r)
306 isl_int_submul(M->row[r][j], m, M->row[r][i]);
307 if (U) {
308 for (r = 0; r < (*U)->n_row; ++r)
309 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
311 if (Q) {
312 for (r = 0; r < (*Q)->n_col; ++r)
313 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
317 static void oppose(struct isl_mat *M, struct isl_mat **U,
318 struct isl_mat **Q, unsigned row, unsigned col)
320 int r;
321 for (r = row; r < M->n_row; ++r)
322 isl_int_neg(M->row[r][col], M->row[r][col]);
323 if (U) {
324 for (r = 0; r < (*U)->n_row; ++r)
325 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
327 if (Q)
328 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
331 /* Given matrix M, compute
333 * M U = H
334 * M = H Q
336 * with U and Q unimodular matrices and H a matrix in column echelon form
337 * such that on each echelon row the entries in the non-echelon column
338 * are non-negative (if neg == 0) or non-positive (if neg == 1)
339 * and stricly smaller (in absolute value) than the entries in the echelon
340 * column.
341 * If U or Q are NULL, then these matrices are not computed.
343 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
344 struct isl_mat **U, struct isl_mat **Q)
346 isl_int c;
347 int row, col;
349 if (U)
350 *U = NULL;
351 if (Q)
352 *Q = NULL;
353 if (!M)
354 goto error;
355 M = isl_mat_cow(M);
356 if (!M)
357 goto error;
358 if (U) {
359 *U = isl_mat_identity(M->ctx, M->n_col);
360 if (!*U)
361 goto error;
363 if (Q) {
364 *Q = isl_mat_identity(M->ctx, M->n_col);
365 if (!*Q)
366 goto error;
369 col = 0;
370 isl_int_init(c);
371 for (row = 0; row < M->n_row; ++row) {
372 int first, i, off;
373 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
374 if (first == -1)
375 continue;
376 first += col;
377 if (first != col)
378 exchange(M, U, Q, row, first, col);
379 if (isl_int_is_neg(M->row[row][col]))
380 oppose(M, U, Q, row, col);
381 first = col+1;
382 while ((off = isl_seq_first_non_zero(M->row[row]+first,
383 M->n_col-first)) != -1) {
384 first += off;
385 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
386 subtract(M, U, Q, row, col, first, c);
387 if (!isl_int_is_zero(M->row[row][first]))
388 exchange(M, U, Q, row, first, col);
389 else
390 ++first;
392 for (i = 0; i < col; ++i) {
393 if (isl_int_is_zero(M->row[row][i]))
394 continue;
395 if (neg)
396 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
397 else
398 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
399 if (isl_int_is_zero(c))
400 continue;
401 subtract(M, U, Q, row, col, i, c);
403 ++col;
405 isl_int_clear(c);
407 return M;
408 error:
409 if (Q) {
410 isl_mat_free(*Q);
411 *Q = NULL;
413 if (U) {
414 isl_mat_free(*U);
415 *U = NULL;
417 return NULL;
420 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
422 int i, rank;
423 struct isl_mat *U = NULL;
424 struct isl_mat *K;
426 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
427 if (!mat || !U)
428 goto error;
430 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
431 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
432 ++i;
433 if (i >= mat->n_row)
434 break;
436 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
437 if (!K)
438 goto error;
439 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
440 isl_mat_free(mat);
441 isl_mat_free(U);
442 return K;
443 error:
444 isl_mat_free(mat);
445 isl_mat_free(U);
446 return NULL;
449 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
451 int i;
452 struct isl_mat *mat2;
454 if (!mat)
455 return NULL;
456 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
457 if (!mat2)
458 return NULL;
459 isl_int_set_si(mat2->row[0][0], 1);
460 isl_seq_clr(mat2->row[0]+1, mat->n_col);
461 for (i = 0; i < mat->n_row; ++i) {
462 isl_int_set_si(mat2->row[1+i][0], 0);
463 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
465 isl_mat_free(mat);
466 return mat2;
469 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
471 int i;
473 for (i = 0; i < n_row; ++i)
474 if (!isl_int_is_zero(row[i][col]))
475 return i;
476 return -1;
479 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
481 int i, min = row_first_non_zero(row, n_row, col);
482 if (min < 0)
483 return -1;
484 for (i = min + 1; i < n_row; ++i) {
485 if (isl_int_is_zero(row[i][col]))
486 continue;
487 if (isl_int_abs_lt(row[i][col], row[min][col]))
488 min = i;
490 return min;
493 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
494 unsigned i, unsigned j)
496 left = isl_mat_swap_rows(left, i, j);
497 right = isl_mat_swap_rows(right, i, j);
500 static void inv_oppose(
501 struct isl_mat *left, struct isl_mat *right, unsigned row)
503 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
504 isl_seq_neg(right->row[row], right->row[row], right->n_col);
507 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
508 unsigned row, unsigned i, isl_int m)
510 isl_int_neg(m, m);
511 isl_seq_combine(left->row[i]+row,
512 left->ctx->one, left->row[i]+row,
513 m, left->row[row]+row,
514 left->n_col-row);
515 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
516 m, right->row[row], right->n_col);
519 /* Compute inv(left)*right
521 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
522 struct isl_mat *right)
524 int row;
525 isl_int a, b;
527 if (!left || !right)
528 goto error;
530 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
531 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
533 if (left->n_row == 0) {
534 isl_mat_free(left);
535 return right;
538 left = isl_mat_cow(left);
539 right = isl_mat_cow(right);
540 if (!left || !right)
541 goto error;
543 isl_int_init(a);
544 isl_int_init(b);
545 for (row = 0; row < left->n_row; ++row) {
546 int pivot, first, i, off;
547 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
548 if (pivot < 0) {
549 isl_int_clear(a);
550 isl_int_clear(b);
551 isl_assert(ctx, pivot >= 0, goto error);
553 pivot += row;
554 if (pivot != row)
555 inv_exchange(left, right, pivot, row);
556 if (isl_int_is_neg(left->row[row][row]))
557 inv_oppose(left, right, row);
558 first = row+1;
559 while ((off = row_first_non_zero(left->row+first,
560 left->n_row-first, row)) != -1) {
561 first += off;
562 isl_int_fdiv_q(a, left->row[first][row],
563 left->row[row][row]);
564 inv_subtract(left, right, row, first, a);
565 if (!isl_int_is_zero(left->row[first][row]))
566 inv_exchange(left, right, row, first);
567 else
568 ++first;
570 for (i = 0; i < row; ++i) {
571 if (isl_int_is_zero(left->row[i][row]))
572 continue;
573 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
574 isl_int_divexact(b, left->row[i][row], a);
575 isl_int_divexact(a, left->row[row][row], a);
576 isl_int_neg(a, a);
577 isl_seq_combine(left->row[i]+row,
578 a, left->row[i]+row,
579 b, left->row[row]+row,
580 left->n_col-row);
581 isl_seq_combine(right->row[i], a, right->row[i],
582 b, right->row[row], right->n_col);
585 isl_int_clear(b);
587 isl_int_set(a, left->row[0][0]);
588 for (row = 1; row < left->n_row; ++row)
589 isl_int_lcm(a, a, left->row[row][row]);
590 if (isl_int_is_zero(a)){
591 isl_int_clear(a);
592 isl_assert(ctx, 0, goto error);
594 for (row = 0; row < left->n_row; ++row) {
595 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
596 if (isl_int_is_one(left->row[row][row]))
597 continue;
598 isl_seq_scale(right->row[row], right->row[row],
599 left->row[row][row], right->n_col);
601 isl_int_clear(a);
603 isl_mat_free(left);
604 return right;
605 error:
606 isl_mat_free(left);
607 isl_mat_free(right);
608 return NULL;
611 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
613 int i;
615 for (i = 0; i < mat->n_row; ++i)
616 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
619 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
620 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
622 int i;
623 isl_int tmp;
625 isl_int_init(tmp);
626 for (i = 0; i < mat->n_row; ++i) {
627 isl_int_mul(tmp, m1, mat->row[i][src1]);
628 isl_int_addmul(tmp, m2, mat->row[i][src2]);
629 isl_int_set(mat->row[i][dst], tmp);
631 isl_int_clear(tmp);
634 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
636 struct isl_mat *inv;
637 int row;
638 isl_int a, b;
640 mat = isl_mat_cow(mat);
641 if (!mat)
642 return NULL;
644 inv = isl_mat_identity(mat->ctx, mat->n_col);
645 inv = isl_mat_cow(inv);
646 if (!inv)
647 goto error;
649 isl_int_init(a);
650 isl_int_init(b);
651 for (row = 0; row < mat->n_row; ++row) {
652 int pivot, first, i, off;
653 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
654 if (pivot < 0) {
655 isl_int_clear(a);
656 isl_int_clear(b);
657 goto error;
659 pivot += row;
660 if (pivot != row)
661 exchange(mat, &inv, NULL, row, pivot, row);
662 if (isl_int_is_neg(mat->row[row][row]))
663 oppose(mat, &inv, NULL, row, row);
664 first = row+1;
665 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
666 mat->n_col-first)) != -1) {
667 first += off;
668 isl_int_fdiv_q(a, mat->row[row][first],
669 mat->row[row][row]);
670 subtract(mat, &inv, NULL, row, row, first, a);
671 if (!isl_int_is_zero(mat->row[row][first]))
672 exchange(mat, &inv, NULL, row, row, first);
673 else
674 ++first;
676 for (i = 0; i < row; ++i) {
677 if (isl_int_is_zero(mat->row[row][i]))
678 continue;
679 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
680 isl_int_divexact(b, mat->row[row][i], a);
681 isl_int_divexact(a, mat->row[row][row], a);
682 isl_int_neg(a, a);
683 isl_mat_col_combine(mat, i, a, i, b, row);
684 isl_mat_col_combine(inv, i, a, i, b, row);
687 isl_int_clear(b);
689 isl_int_set(a, mat->row[0][0]);
690 for (row = 1; row < mat->n_row; ++row)
691 isl_int_lcm(a, a, mat->row[row][row]);
692 if (isl_int_is_zero(a)){
693 isl_int_clear(a);
694 goto error;
696 for (row = 0; row < mat->n_row; ++row) {
697 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
698 if (isl_int_is_one(mat->row[row][row]))
699 continue;
700 isl_mat_col_scale(inv, row, mat->row[row][row]);
702 isl_int_clear(a);
704 isl_mat_free(mat);
706 return inv;
707 error:
708 isl_mat_free(mat);
709 return NULL;
712 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
714 struct isl_mat *transpose = NULL;
715 int i, j;
717 if (mat->n_col == mat->n_row) {
718 mat = isl_mat_cow(mat);
719 if (!mat)
720 return NULL;
721 for (i = 0; i < mat->n_row; ++i)
722 for (j = i + 1; j < mat->n_col; ++j)
723 isl_int_swap(mat->row[i][j], mat->row[j][i]);
724 return mat;
726 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
727 if (!transpose)
728 goto error;
729 for (i = 0; i < mat->n_row; ++i)
730 for (j = 0; j < mat->n_col; ++j)
731 isl_int_set(transpose->row[j][i], mat->row[i][j]);
732 isl_mat_free(mat);
733 return transpose;
734 error:
735 isl_mat_free(mat);
736 return NULL;
739 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
741 int r;
743 mat = isl_mat_cow(mat);
744 if (!mat)
745 return NULL;
746 isl_assert(ctx, i < mat->n_col, goto error);
747 isl_assert(ctx, j < mat->n_col, goto error);
749 for (r = 0; r < mat->n_row; ++r)
750 isl_int_swap(mat->row[r][i], mat->row[r][j]);
751 return mat;
752 error:
753 isl_mat_free(mat);
754 return NULL;
757 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
759 isl_int *t;
761 if (!mat)
762 return NULL;
763 mat = isl_mat_cow(mat);
764 if (!mat)
765 return NULL;
766 t = mat->row[i];
767 mat->row[i] = mat->row[j];
768 mat->row[j] = t;
769 return mat;
772 struct isl_mat *isl_mat_product(struct isl_mat *left, struct isl_mat *right)
774 int i, j, k;
775 struct isl_mat *prod;
777 if (!left || !right)
778 goto error;
779 isl_assert(ctx, left->n_col == right->n_row, goto error);
780 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
781 if (!prod)
782 goto error;
783 if (left->n_col == 0) {
784 for (i = 0; i < prod->n_row; ++i)
785 isl_seq_clr(prod->row[i], prod->n_col);
786 return prod;
788 for (i = 0; i < prod->n_row; ++i) {
789 for (j = 0; j < prod->n_col; ++j) {
790 isl_int_mul(prod->row[i][j],
791 left->row[i][0], right->row[0][j]);
792 for (k = 1; k < left->n_col; ++k)
793 isl_int_addmul(prod->row[i][j],
794 left->row[i][k], right->row[k][j]);
797 isl_mat_free(left);
798 isl_mat_free(right);
799 return prod;
800 error:
801 isl_mat_free(left);
802 isl_mat_free(right);
803 return NULL;
806 /* Replace the variables x in the rows q by x' given by x = M x',
807 * with M the matrix mat.
809 * If the number of new variables is greater than the original
810 * number of variables, then the rows q have already been
811 * preextended. If the new number is smaller, then the coefficients
812 * of the divs, which are not changed, need to be shifted down.
813 * The row q may be the equalities, the inequalities or the
814 * div expressions. In the latter case, has_div is true and
815 * we need to take into account the extra denominator column.
817 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
818 unsigned n_div, int has_div, struct isl_mat *mat)
820 int i;
821 struct isl_mat *t;
822 int e;
824 if (mat->n_col >= mat->n_row)
825 e = 0;
826 else
827 e = mat->n_row - mat->n_col;
828 if (has_div)
829 for (i = 0; i < n; ++i)
830 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
831 t = isl_mat_sub_alloc(mat->ctx, q, 0, n, has_div, mat->n_row);
832 t = isl_mat_product(t, mat);
833 if (!t)
834 return -1;
835 for (i = 0; i < n; ++i) {
836 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
837 isl_seq_cpy(q[i] + has_div + t->n_col,
838 q[i] + has_div + t->n_col + e, n_div);
839 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
841 isl_mat_free(t);
842 return 0;
845 /* Replace the variables x in bset by x' given by x = M x', with
846 * M the matrix mat.
848 * If there are fewer variables x' then there are x, then we perform
849 * the transformation in place, which that, in principle,
850 * this frees up some extra variables as the number
851 * of columns remains constant, but we would have to extend
852 * the div array too as the number of rows in this array is assumed
853 * to be equal to extra.
855 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
856 struct isl_mat *mat)
858 struct isl_ctx *ctx;
860 if (!bset || !mat)
861 goto error;
863 ctx = bset->ctx;
864 bset = isl_basic_set_cow(bset);
865 if (!bset)
866 goto error;
868 isl_assert(ctx, bset->dim->nparam == 0, goto error);
869 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
871 if (mat->n_col > mat->n_row)
872 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0,
873 0, 0);
874 else if (mat->n_col < mat->n_row) {
875 bset->dim = isl_dim_cow(bset->dim);
876 if (!bset->dim)
877 goto error;
878 bset->dim->n_out -= mat->n_row - mat->n_col;
881 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
882 isl_mat_copy(mat)) < 0)
883 goto error;
885 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
886 isl_mat_copy(mat)) < 0)
887 goto error;
889 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
890 goto error2;
892 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
893 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
894 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
895 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
896 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
898 bset = isl_basic_set_simplify(bset);
899 bset = isl_basic_set_finalize(bset);
901 return bset;
902 error:
903 isl_mat_free(mat);
904 error2:
905 isl_basic_set_free(bset);
906 return NULL;
909 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
911 struct isl_ctx *ctx;
912 int i;
914 set = isl_set_cow(set);
915 if (!set)
916 return NULL;
918 ctx = set->ctx;
919 for (i = 0; i < set->n; ++i) {
920 set->p[i] = isl_basic_set_preimage(set->p[i],
921 isl_mat_copy(mat));
922 if (!set->p[i])
923 goto error;
925 if (mat->n_col != mat->n_row) {
926 set->dim = isl_dim_cow(set->dim);
927 if (!set->dim)
928 goto error;
929 set->dim->n_out += mat->n_col;
930 set->dim->n_out -= mat->n_row;
932 isl_mat_free(mat);
933 ISL_F_CLR(set, ISL_SET_NORMALIZED);
934 return set;
935 error:
936 isl_set_free(set);
937 isl_mat_free(mat);
938 return NULL;
941 void isl_mat_dump(struct isl_mat *mat, FILE *out, int indent)
943 int i, j;
945 if (!mat) {
946 fprintf(out, "%*snull mat\n", indent, "");
947 return;
950 if (mat->n_row == 0)
951 fprintf(out, "%*s[]\n", indent, "");
953 for (i = 0; i < mat->n_row; ++i) {
954 if (!i)
955 fprintf(out, "%*s[[", indent, "");
956 else
957 fprintf(out, "%*s[", indent+1, "");
958 for (j = 0; j < mat->n_col; ++j) {
959 if (j)
960 fprintf(out, ",");
961 isl_int_print(out, mat->row[i][j], 0);
963 if (i == mat->n_row-1)
964 fprintf(out, "]]\n");
965 else
966 fprintf(out, "]\n");
970 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
972 int r;
974 mat = isl_mat_cow(mat);
975 if (!mat)
976 return NULL;
978 if (col != mat->n_col-n) {
979 for (r = 0; r < mat->n_row; ++r)
980 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
981 mat->n_col - col - n);
983 mat->n_col -= n;
984 return mat;
987 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
989 int r;
991 mat = isl_mat_cow(mat);
992 if (!mat)
993 return NULL;
995 for (r = row; r+n < mat->n_row; ++r)
996 mat->row[r] = mat->row[r+n];
998 mat->n_row -= n;
999 return mat;
1002 void isl_mat_col_submul(struct isl_mat *mat,
1003 int dst_col, isl_int f, int src_col)
1005 int i;
1007 for (i = 0; i < mat->n_row; ++i)
1008 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1011 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1013 int i;
1015 for (i = 0; i < mat->n_row; ++i)
1016 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1019 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1021 int r;
1022 struct isl_mat *H = NULL, *Q = NULL;
1024 isl_assert(ctx, M->n_row == M->n_col, goto error);
1025 M->n_row = row;
1026 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1027 M->n_row = M->n_col;
1028 if (!H)
1029 goto error;
1030 for (r = 0; r < row; ++r)
1031 isl_assert(ctx, isl_int_is_one(H->row[r][r]), goto error);
1032 for (r = row; r < M->n_row; ++r)
1033 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1034 isl_mat_free(H);
1035 isl_mat_free(Q);
1036 return M;
1037 error:
1038 isl_mat_free(H);
1039 isl_mat_free(Q);
1040 isl_mat_free(M);
1041 return NULL;