isl_tab_basic_map_partial_lexopt: add memory management annotations
[isl.git] / isl_mat.c
blob1182ab012636533bd026f555ce70687820b9d85b
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_map_private.h>
14 #include <isl/space.h>
15 #include <isl_seq.h>
16 #include <isl_mat_private.h>
17 #include <isl_vec_private.h>
18 #include <isl_space_private.h>
19 #include <isl_val_private.h>
20 #include <isl/deprecated/mat_int.h>
22 isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
24 return mat ? mat->ctx : NULL;
27 /* Return a hash value that digests "mat".
29 uint32_t isl_mat_get_hash(__isl_keep isl_mat *mat)
31 int i;
32 uint32_t hash;
34 if (!mat)
35 return 0;
37 hash = isl_hash_init();
38 isl_hash_byte(hash, mat->n_row & 0xFF);
39 isl_hash_byte(hash, mat->n_col & 0xFF);
40 for (i = 0; i < mat->n_row; ++i) {
41 uint32_t row_hash;
43 row_hash = isl_seq_get_hash(mat->row[i], mat->n_col);
44 isl_hash_hash(hash, row_hash);
47 return hash;
50 struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
51 unsigned n_row, unsigned n_col)
53 int i;
54 struct isl_mat *mat;
56 mat = isl_alloc_type(ctx, struct isl_mat);
57 if (!mat)
58 return NULL;
60 mat->row = NULL;
61 mat->block = isl_blk_alloc(ctx, n_row * n_col);
62 if (isl_blk_is_error(mat->block))
63 goto error;
64 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
65 if (n_row && !mat->row)
66 goto error;
68 for (i = 0; i < n_row; ++i)
69 mat->row[i] = mat->block.data + i * n_col;
71 mat->ctx = ctx;
72 isl_ctx_ref(ctx);
73 mat->ref = 1;
74 mat->n_row = n_row;
75 mat->n_col = n_col;
76 mat->max_col = n_col;
77 mat->flags = 0;
79 return mat;
80 error:
81 isl_blk_free(ctx, mat->block);
82 free(mat);
83 return NULL;
86 struct isl_mat *isl_mat_extend(struct isl_mat *mat,
87 unsigned n_row, unsigned n_col)
89 int i;
90 isl_int *old;
91 isl_int **row;
93 if (!mat)
94 return NULL;
96 if (mat->max_col >= n_col && mat->n_row >= n_row) {
97 if (mat->n_col < n_col)
98 mat->n_col = n_col;
99 return mat;
102 if (mat->max_col < n_col) {
103 struct isl_mat *new_mat;
105 if (n_row < mat->n_row)
106 n_row = mat->n_row;
107 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
108 if (!new_mat)
109 goto error;
110 for (i = 0; i < mat->n_row; ++i)
111 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
112 isl_mat_free(mat);
113 return new_mat;
116 mat = isl_mat_cow(mat);
117 if (!mat)
118 goto error;
120 old = mat->block.data;
121 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
122 if (isl_blk_is_error(mat->block))
123 goto error;
124 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
125 if (n_row && !row)
126 goto error;
127 mat->row = row;
129 for (i = 0; i < mat->n_row; ++i)
130 mat->row[i] = mat->block.data + (mat->row[i] - old);
131 for (i = mat->n_row; i < n_row; ++i)
132 mat->row[i] = mat->block.data + i * mat->max_col;
133 mat->n_row = n_row;
134 if (mat->n_col < n_col)
135 mat->n_col = n_col;
137 return mat;
138 error:
139 isl_mat_free(mat);
140 return NULL;
143 __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
144 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
146 int i;
147 struct isl_mat *mat;
149 mat = isl_alloc_type(ctx, struct isl_mat);
150 if (!mat)
151 return NULL;
152 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
153 if (n_row && !mat->row)
154 goto error;
155 for (i = 0; i < n_row; ++i)
156 mat->row[i] = row[first_row+i] + first_col;
157 mat->ctx = ctx;
158 isl_ctx_ref(ctx);
159 mat->ref = 1;
160 mat->n_row = n_row;
161 mat->n_col = n_col;
162 mat->block = isl_blk_empty();
163 mat->flags = ISL_MAT_BORROWED;
164 return mat;
165 error:
166 free(mat);
167 return NULL;
170 __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
171 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
173 if (!mat)
174 return NULL;
175 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
176 first_col, n_col);
179 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
180 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
182 int i;
184 for (i = 0; i < n_row; ++i)
185 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
188 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
189 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
191 int i;
193 for (i = 0; i < n_row; ++i)
194 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
197 struct isl_mat *isl_mat_copy(struct isl_mat *mat)
199 if (!mat)
200 return NULL;
202 mat->ref++;
203 return mat;
206 struct isl_mat *isl_mat_dup(struct isl_mat *mat)
208 int i;
209 struct isl_mat *mat2;
211 if (!mat)
212 return NULL;
213 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
214 if (!mat2)
215 return NULL;
216 for (i = 0; i < mat->n_row; ++i)
217 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
218 return mat2;
221 struct isl_mat *isl_mat_cow(struct isl_mat *mat)
223 struct isl_mat *mat2;
224 if (!mat)
225 return NULL;
227 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
228 return mat;
230 mat2 = isl_mat_dup(mat);
231 isl_mat_free(mat);
232 return mat2;
235 __isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
237 if (!mat)
238 return NULL;
240 if (--mat->ref > 0)
241 return NULL;
243 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
244 isl_blk_free(mat->ctx, mat->block);
245 isl_ctx_deref(mat->ctx);
246 free(mat->row);
247 free(mat);
249 return NULL;
252 int isl_mat_rows(__isl_keep isl_mat *mat)
254 return mat ? mat->n_row : -1;
257 int isl_mat_cols(__isl_keep isl_mat *mat)
259 return mat ? mat->n_col : -1;
262 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
264 if (!mat)
265 return -1;
266 if (row < 0 || row >= mat->n_row)
267 isl_die(mat->ctx, isl_error_invalid, "row out of range",
268 return -1);
269 if (col < 0 || col >= mat->n_col)
270 isl_die(mat->ctx, isl_error_invalid, "column out of range",
271 return -1);
272 isl_int_set(*v, mat->row[row][col]);
273 return 0;
276 /* Extract the element at row "row", oolumn "col" of "mat".
278 __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
279 int row, int col)
281 isl_ctx *ctx;
283 if (!mat)
284 return NULL;
285 ctx = isl_mat_get_ctx(mat);
286 if (row < 0 || row >= mat->n_row)
287 isl_die(ctx, isl_error_invalid, "row out of range",
288 return NULL);
289 if (col < 0 || col >= mat->n_col)
290 isl_die(ctx, isl_error_invalid, "column out of range",
291 return NULL);
292 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
295 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
296 int row, int col, isl_int v)
298 mat = isl_mat_cow(mat);
299 if (!mat)
300 return NULL;
301 if (row < 0 || row >= mat->n_row)
302 isl_die(mat->ctx, isl_error_invalid, "row out of range",
303 goto error);
304 if (col < 0 || col >= mat->n_col)
305 isl_die(mat->ctx, isl_error_invalid, "column out of range",
306 goto error);
307 isl_int_set(mat->row[row][col], v);
308 return mat;
309 error:
310 isl_mat_free(mat);
311 return NULL;
314 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
315 int row, int col, int v)
317 mat = isl_mat_cow(mat);
318 if (!mat)
319 return NULL;
320 if (row < 0 || row >= mat->n_row)
321 isl_die(mat->ctx, isl_error_invalid, "row out of range",
322 goto error);
323 if (col < 0 || col >= mat->n_col)
324 isl_die(mat->ctx, isl_error_invalid, "column out of range",
325 goto error);
326 isl_int_set_si(mat->row[row][col], v);
327 return mat;
328 error:
329 isl_mat_free(mat);
330 return NULL;
333 /* Replace the element at row "row", column "col" of "mat" by "v".
335 __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
336 int row, int col, __isl_take isl_val *v)
338 if (!v)
339 return isl_mat_free(mat);
340 if (!isl_val_is_int(v))
341 isl_die(isl_val_get_ctx(v), isl_error_invalid,
342 "expecting integer value", goto error);
343 mat = isl_mat_set_element(mat, row, col, v->n);
344 isl_val_free(v);
345 return mat;
346 error:
347 isl_val_free(v);
348 return isl_mat_free(mat);
351 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
353 int i;
354 struct isl_mat *mat;
356 mat = isl_mat_alloc(ctx, n_row, n_row);
357 if (!mat)
358 return NULL;
359 for (i = 0; i < n_row; ++i) {
360 isl_seq_clr(mat->row[i], i);
361 isl_int_set(mat->row[i][i], d);
362 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
365 return mat;
368 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
370 if (!ctx)
371 return NULL;
372 return isl_mat_diag(ctx, n_row, ctx->one);
375 /* Is "mat" a (possibly scaled) identity matrix?
377 int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
379 int i;
381 if (!mat)
382 return -1;
383 if (mat->n_row != mat->n_col)
384 return 0;
386 for (i = 0; i < mat->n_row; ++i) {
387 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
388 return 0;
389 if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
390 return 0;
391 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
392 mat->n_col - (i + 1)) != -1)
393 return 0;
396 return 1;
399 struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
401 int i;
402 struct isl_vec *prod;
404 if (!mat || !vec)
405 goto error;
407 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
409 prod = isl_vec_alloc(mat->ctx, mat->n_row);
410 if (!prod)
411 goto error;
413 for (i = 0; i < prod->size; ++i)
414 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
415 &prod->block.data[i]);
416 isl_mat_free(mat);
417 isl_vec_free(vec);
418 return prod;
419 error:
420 isl_mat_free(mat);
421 isl_vec_free(vec);
422 return NULL;
425 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
426 __isl_take isl_vec *vec)
428 struct isl_mat *vec_mat;
429 int i;
431 if (!mat || !vec)
432 goto error;
433 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
434 if (!vec_mat)
435 goto error;
436 for (i = 0; i < vec->size; ++i)
437 isl_int_set(vec_mat->row[i][0], vec->el[i]);
438 vec_mat = isl_mat_inverse_product(mat, vec_mat);
439 isl_vec_free(vec);
440 if (!vec_mat)
441 return NULL;
442 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
443 if (vec)
444 for (i = 0; i < vec->size; ++i)
445 isl_int_set(vec->el[i], vec_mat->row[i][0]);
446 isl_mat_free(vec_mat);
447 return vec;
448 error:
449 isl_mat_free(mat);
450 isl_vec_free(vec);
451 return NULL;
454 struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
456 int i, j;
457 struct isl_vec *prod;
459 if (!mat || !vec)
460 goto error;
462 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
464 prod = isl_vec_alloc(mat->ctx, mat->n_col);
465 if (!prod)
466 goto error;
468 for (i = 0; i < prod->size; ++i) {
469 isl_int_set_si(prod->el[i], 0);
470 for (j = 0; j < vec->size; ++j)
471 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
473 isl_mat_free(mat);
474 isl_vec_free(vec);
475 return prod;
476 error:
477 isl_mat_free(mat);
478 isl_vec_free(vec);
479 return NULL;
482 struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
483 struct isl_mat *right)
485 int i;
486 struct isl_mat *sum;
488 if (!left || !right)
489 goto error;
491 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
492 isl_assert(left->ctx, left->n_row >= 1, goto error);
493 isl_assert(left->ctx, left->n_col >= 1, goto error);
494 isl_assert(left->ctx, right->n_col >= 1, goto error);
495 isl_assert(left->ctx,
496 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
497 goto error);
498 isl_assert(left->ctx,
499 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
500 goto error);
502 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
503 if (!sum)
504 goto error;
505 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
506 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
507 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
509 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
510 for (i = 1; i < sum->n_row; ++i) {
511 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
512 isl_int_addmul(sum->row[i][0],
513 right->row[0][0], right->row[i][0]);
514 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
515 left->n_col-1);
516 isl_seq_scale(sum->row[i]+left->n_col,
517 right->row[i]+1, right->row[0][0],
518 right->n_col-1);
521 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
522 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
523 isl_mat_free(left);
524 isl_mat_free(right);
525 return sum;
526 error:
527 isl_mat_free(left);
528 isl_mat_free(right);
529 return NULL;
532 static void exchange(struct isl_mat *M, struct isl_mat **U,
533 struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
535 int r;
536 for (r = row; r < M->n_row; ++r)
537 isl_int_swap(M->row[r][i], M->row[r][j]);
538 if (U) {
539 for (r = 0; r < (*U)->n_row; ++r)
540 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
542 if (Q)
543 isl_mat_swap_rows(*Q, i, j);
546 static void subtract(struct isl_mat *M, struct isl_mat **U,
547 struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
549 int r;
550 for (r = row; r < M->n_row; ++r)
551 isl_int_submul(M->row[r][j], m, M->row[r][i]);
552 if (U) {
553 for (r = 0; r < (*U)->n_row; ++r)
554 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
556 if (Q) {
557 for (r = 0; r < (*Q)->n_col; ++r)
558 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
562 static void oppose(struct isl_mat *M, struct isl_mat **U,
563 struct isl_mat **Q, unsigned row, unsigned col)
565 int r;
566 for (r = row; r < M->n_row; ++r)
567 isl_int_neg(M->row[r][col], M->row[r][col]);
568 if (U) {
569 for (r = 0; r < (*U)->n_row; ++r)
570 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
572 if (Q)
573 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
576 /* Given matrix M, compute
578 * M U = H
579 * M = H Q
581 * with U and Q unimodular matrices and H a matrix in column echelon form
582 * such that on each echelon row the entries in the non-echelon column
583 * are non-negative (if neg == 0) or non-positive (if neg == 1)
584 * and strictly smaller (in absolute value) than the entries in the echelon
585 * column.
586 * If U or Q are NULL, then these matrices are not computed.
588 struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
589 struct isl_mat **U, struct isl_mat **Q)
591 isl_int c;
592 int row, col;
594 if (U)
595 *U = NULL;
596 if (Q)
597 *Q = NULL;
598 if (!M)
599 goto error;
600 M = isl_mat_cow(M);
601 if (!M)
602 goto error;
603 if (U) {
604 *U = isl_mat_identity(M->ctx, M->n_col);
605 if (!*U)
606 goto error;
608 if (Q) {
609 *Q = isl_mat_identity(M->ctx, M->n_col);
610 if (!*Q)
611 goto error;
614 col = 0;
615 isl_int_init(c);
616 for (row = 0; row < M->n_row; ++row) {
617 int first, i, off;
618 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
619 if (first == -1)
620 continue;
621 first += col;
622 if (first != col)
623 exchange(M, U, Q, row, first, col);
624 if (isl_int_is_neg(M->row[row][col]))
625 oppose(M, U, Q, row, col);
626 first = col+1;
627 while ((off = isl_seq_first_non_zero(M->row[row]+first,
628 M->n_col-first)) != -1) {
629 first += off;
630 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
631 subtract(M, U, Q, row, col, first, c);
632 if (!isl_int_is_zero(M->row[row][first]))
633 exchange(M, U, Q, row, first, col);
634 else
635 ++first;
637 for (i = 0; i < col; ++i) {
638 if (isl_int_is_zero(M->row[row][i]))
639 continue;
640 if (neg)
641 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
642 else
643 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
644 if (isl_int_is_zero(c))
645 continue;
646 subtract(M, U, Q, row, col, i, c);
648 ++col;
650 isl_int_clear(c);
652 return M;
653 error:
654 if (Q) {
655 isl_mat_free(*Q);
656 *Q = NULL;
658 if (U) {
659 isl_mat_free(*U);
660 *U = NULL;
662 isl_mat_free(M);
663 return NULL;
666 struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
668 int i, rank;
669 struct isl_mat *U = NULL;
670 struct isl_mat *K;
672 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
673 if (!mat || !U)
674 goto error;
676 for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
677 while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
678 ++i;
679 if (i >= mat->n_row)
680 break;
682 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
683 if (!K)
684 goto error;
685 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
686 isl_mat_free(mat);
687 isl_mat_free(U);
688 return K;
689 error:
690 isl_mat_free(mat);
691 isl_mat_free(U);
692 return NULL;
695 struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
697 int i;
698 struct isl_mat *mat2;
700 if (!mat)
701 return NULL;
702 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
703 if (!mat2)
704 goto error;
705 isl_int_set_si(mat2->row[0][0], 1);
706 isl_seq_clr(mat2->row[0]+1, mat->n_col);
707 for (i = 0; i < mat->n_row; ++i) {
708 isl_int_set_si(mat2->row[1+i][0], 0);
709 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
711 isl_mat_free(mat);
712 return mat2;
713 error:
714 isl_mat_free(mat);
715 return NULL;
718 /* Given two matrices M1 and M2, return the block matrix
720 * [ M1 0 ]
721 * [ 0 M2 ]
723 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
724 __isl_take isl_mat *mat2)
726 int i;
727 isl_mat *mat;
729 if (!mat1 || !mat2)
730 goto error;
732 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
733 mat1->n_col + mat2->n_col);
734 if (!mat)
735 goto error;
736 for (i = 0; i < mat1->n_row; ++i) {
737 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
738 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
740 for (i = 0; i < mat2->n_row; ++i) {
741 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
742 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
743 mat2->row[i], mat2->n_col);
745 isl_mat_free(mat1);
746 isl_mat_free(mat2);
747 return mat;
748 error:
749 isl_mat_free(mat1);
750 isl_mat_free(mat2);
751 return NULL;
754 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
756 int i;
758 for (i = 0; i < n_row; ++i)
759 if (!isl_int_is_zero(row[i][col]))
760 return i;
761 return -1;
764 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
766 int i, min = row_first_non_zero(row, n_row, col);
767 if (min < 0)
768 return -1;
769 for (i = min + 1; i < n_row; ++i) {
770 if (isl_int_is_zero(row[i][col]))
771 continue;
772 if (isl_int_abs_lt(row[i][col], row[min][col]))
773 min = i;
775 return min;
778 static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
779 unsigned i, unsigned j)
781 left = isl_mat_swap_rows(left, i, j);
782 right = isl_mat_swap_rows(right, i, j);
785 static void inv_oppose(
786 struct isl_mat *left, struct isl_mat *right, unsigned row)
788 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
789 isl_seq_neg(right->row[row], right->row[row], right->n_col);
792 static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
793 unsigned row, unsigned i, isl_int m)
795 isl_int_neg(m, m);
796 isl_seq_combine(left->row[i]+row,
797 left->ctx->one, left->row[i]+row,
798 m, left->row[row]+row,
799 left->n_col-row);
800 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
801 m, right->row[row], right->n_col);
804 /* Compute inv(left)*right
806 struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
807 struct isl_mat *right)
809 int row;
810 isl_int a, b;
812 if (!left || !right)
813 goto error;
815 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
816 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
818 if (left->n_row == 0) {
819 isl_mat_free(left);
820 return right;
823 left = isl_mat_cow(left);
824 right = isl_mat_cow(right);
825 if (!left || !right)
826 goto error;
828 isl_int_init(a);
829 isl_int_init(b);
830 for (row = 0; row < left->n_row; ++row) {
831 int pivot, first, i, off;
832 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
833 if (pivot < 0) {
834 isl_int_clear(a);
835 isl_int_clear(b);
836 isl_assert(left->ctx, pivot >= 0, goto error);
838 pivot += row;
839 if (pivot != row)
840 inv_exchange(left, right, pivot, row);
841 if (isl_int_is_neg(left->row[row][row]))
842 inv_oppose(left, right, row);
843 first = row+1;
844 while ((off = row_first_non_zero(left->row+first,
845 left->n_row-first, row)) != -1) {
846 first += off;
847 isl_int_fdiv_q(a, left->row[first][row],
848 left->row[row][row]);
849 inv_subtract(left, right, row, first, a);
850 if (!isl_int_is_zero(left->row[first][row]))
851 inv_exchange(left, right, row, first);
852 else
853 ++first;
855 for (i = 0; i < row; ++i) {
856 if (isl_int_is_zero(left->row[i][row]))
857 continue;
858 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
859 isl_int_divexact(b, left->row[i][row], a);
860 isl_int_divexact(a, left->row[row][row], a);
861 isl_int_neg(b, b);
862 isl_seq_combine(left->row[i] + i,
863 a, left->row[i] + i,
864 b, left->row[row] + i,
865 left->n_col - i);
866 isl_seq_combine(right->row[i], a, right->row[i],
867 b, right->row[row], right->n_col);
870 isl_int_clear(b);
872 isl_int_set(a, left->row[0][0]);
873 for (row = 1; row < left->n_row; ++row)
874 isl_int_lcm(a, a, left->row[row][row]);
875 if (isl_int_is_zero(a)){
876 isl_int_clear(a);
877 isl_assert(left->ctx, 0, goto error);
879 for (row = 0; row < left->n_row; ++row) {
880 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
881 if (isl_int_is_one(left->row[row][row]))
882 continue;
883 isl_seq_scale(right->row[row], right->row[row],
884 left->row[row][row], right->n_col);
886 isl_int_clear(a);
888 isl_mat_free(left);
889 return right;
890 error:
891 isl_mat_free(left);
892 isl_mat_free(right);
893 return NULL;
896 void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
898 int i;
900 for (i = 0; i < mat->n_row; ++i)
901 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
904 void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
905 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
907 int i;
908 isl_int tmp;
910 isl_int_init(tmp);
911 for (i = 0; i < mat->n_row; ++i) {
912 isl_int_mul(tmp, m1, mat->row[i][src1]);
913 isl_int_addmul(tmp, m2, mat->row[i][src2]);
914 isl_int_set(mat->row[i][dst], tmp);
916 isl_int_clear(tmp);
919 struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
921 struct isl_mat *inv;
922 int row;
923 isl_int a, b;
925 mat = isl_mat_cow(mat);
926 if (!mat)
927 return NULL;
929 inv = isl_mat_identity(mat->ctx, mat->n_col);
930 inv = isl_mat_cow(inv);
931 if (!inv)
932 goto error;
934 isl_int_init(a);
935 isl_int_init(b);
936 for (row = 0; row < mat->n_row; ++row) {
937 int pivot, first, i, off;
938 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
939 if (pivot < 0) {
940 isl_int_clear(a);
941 isl_int_clear(b);
942 isl_assert(mat->ctx, pivot >= 0, goto error);
944 pivot += row;
945 if (pivot != row)
946 exchange(mat, &inv, NULL, row, pivot, row);
947 if (isl_int_is_neg(mat->row[row][row]))
948 oppose(mat, &inv, NULL, row, row);
949 first = row+1;
950 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
951 mat->n_col-first)) != -1) {
952 first += off;
953 isl_int_fdiv_q(a, mat->row[row][first],
954 mat->row[row][row]);
955 subtract(mat, &inv, NULL, row, row, first, a);
956 if (!isl_int_is_zero(mat->row[row][first]))
957 exchange(mat, &inv, NULL, row, row, first);
958 else
959 ++first;
961 for (i = 0; i < row; ++i) {
962 if (isl_int_is_zero(mat->row[row][i]))
963 continue;
964 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
965 isl_int_divexact(b, mat->row[row][i], a);
966 isl_int_divexact(a, mat->row[row][row], a);
967 isl_int_neg(a, a);
968 isl_mat_col_combine(mat, i, a, i, b, row);
969 isl_mat_col_combine(inv, i, a, i, b, row);
972 isl_int_clear(b);
974 isl_int_set(a, mat->row[0][0]);
975 for (row = 1; row < mat->n_row; ++row)
976 isl_int_lcm(a, a, mat->row[row][row]);
977 if (isl_int_is_zero(a)){
978 isl_int_clear(a);
979 goto error;
981 for (row = 0; row < mat->n_row; ++row) {
982 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
983 if (isl_int_is_one(mat->row[row][row]))
984 continue;
985 isl_mat_col_scale(inv, row, mat->row[row][row]);
987 isl_int_clear(a);
989 isl_mat_free(mat);
991 return inv;
992 error:
993 isl_mat_free(mat);
994 isl_mat_free(inv);
995 return NULL;
998 struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
1000 struct isl_mat *transpose = NULL;
1001 int i, j;
1003 if (!mat)
1004 return NULL;
1006 if (mat->n_col == mat->n_row) {
1007 mat = isl_mat_cow(mat);
1008 if (!mat)
1009 return NULL;
1010 for (i = 0; i < mat->n_row; ++i)
1011 for (j = i + 1; j < mat->n_col; ++j)
1012 isl_int_swap(mat->row[i][j], mat->row[j][i]);
1013 return mat;
1015 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1016 if (!transpose)
1017 goto error;
1018 for (i = 0; i < mat->n_row; ++i)
1019 for (j = 0; j < mat->n_col; ++j)
1020 isl_int_set(transpose->row[j][i], mat->row[i][j]);
1021 isl_mat_free(mat);
1022 return transpose;
1023 error:
1024 isl_mat_free(mat);
1025 return NULL;
1028 struct isl_mat *isl_mat_swap_cols(struct isl_mat *mat, unsigned i, unsigned j)
1030 int r;
1032 mat = isl_mat_cow(mat);
1033 if (!mat)
1034 return NULL;
1035 isl_assert(mat->ctx, i < mat->n_col, goto error);
1036 isl_assert(mat->ctx, j < mat->n_col, goto error);
1038 for (r = 0; r < mat->n_row; ++r)
1039 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1040 return mat;
1041 error:
1042 isl_mat_free(mat);
1043 return NULL;
1046 struct isl_mat *isl_mat_swap_rows(struct isl_mat *mat, unsigned i, unsigned j)
1048 isl_int *t;
1050 if (!mat)
1051 return NULL;
1052 mat = isl_mat_cow(mat);
1053 if (!mat)
1054 return NULL;
1055 t = mat->row[i];
1056 mat->row[i] = mat->row[j];
1057 mat->row[j] = t;
1058 return mat;
1061 /* Calculate the product of two matrices.
1063 * This function is optimized for operand matrices that contain many zeros and
1064 * skips multiplications where we know one of the operands is zero.
1066 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1067 __isl_take isl_mat *right)
1069 int i, j, k;
1070 struct isl_mat *prod;
1072 if (!left || !right)
1073 goto error;
1074 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1075 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1076 if (!prod)
1077 goto error;
1078 if (left->n_col == 0) {
1079 for (i = 0; i < prod->n_row; ++i)
1080 isl_seq_clr(prod->row[i], prod->n_col);
1081 isl_mat_free(left);
1082 isl_mat_free(right);
1083 return prod;
1085 for (i = 0; i < prod->n_row; ++i) {
1086 for (j = 0; j < prod->n_col; ++j)
1087 isl_int_mul(prod->row[i][j],
1088 left->row[i][0], right->row[0][j]);
1089 for (k = 1; k < left->n_col; ++k) {
1090 if (isl_int_is_zero(left->row[i][k]))
1091 continue;
1092 for (j = 0; j < prod->n_col; ++j)
1093 isl_int_addmul(prod->row[i][j],
1094 left->row[i][k], right->row[k][j]);
1097 isl_mat_free(left);
1098 isl_mat_free(right);
1099 return prod;
1100 error:
1101 isl_mat_free(left);
1102 isl_mat_free(right);
1103 return NULL;
1106 /* Replace the variables x in the rows q by x' given by x = M x',
1107 * with M the matrix mat.
1109 * If the number of new variables is greater than the original
1110 * number of variables, then the rows q have already been
1111 * preextended. If the new number is smaller, then the coefficients
1112 * of the divs, which are not changed, need to be shifted down.
1113 * The row q may be the equalities, the inequalities or the
1114 * div expressions. In the latter case, has_div is true and
1115 * we need to take into account the extra denominator column.
1117 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1118 unsigned n_div, int has_div, struct isl_mat *mat)
1120 int i;
1121 struct isl_mat *t;
1122 int e;
1124 if (mat->n_col >= mat->n_row)
1125 e = 0;
1126 else
1127 e = mat->n_row - mat->n_col;
1128 if (has_div)
1129 for (i = 0; i < n; ++i)
1130 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1131 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1132 t = isl_mat_product(t, mat);
1133 if (!t)
1134 return -1;
1135 for (i = 0; i < n; ++i) {
1136 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1137 isl_seq_cpy(q[i] + has_div + t->n_col,
1138 q[i] + has_div + t->n_col + e, n_div);
1139 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1141 isl_mat_free(t);
1142 return 0;
1145 /* Replace the variables x in bset by x' given by x = M x', with
1146 * M the matrix mat.
1148 * If there are fewer variables x' then there are x, then we perform
1149 * the transformation in place, which means that, in principle,
1150 * this frees up some extra variables as the number
1151 * of columns remains constant, but we would have to extend
1152 * the div array too as the number of rows in this array is assumed
1153 * to be equal to extra.
1155 struct isl_basic_set *isl_basic_set_preimage(struct isl_basic_set *bset,
1156 struct isl_mat *mat)
1158 struct isl_ctx *ctx;
1160 if (!bset || !mat)
1161 goto error;
1163 ctx = bset->ctx;
1164 bset = isl_basic_set_cow(bset);
1165 if (!bset)
1166 goto error;
1168 isl_assert(ctx, bset->dim->nparam == 0, goto error);
1169 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1170 isl_assert(ctx, mat->n_col > 0, goto error);
1172 if (mat->n_col > mat->n_row) {
1173 bset = isl_basic_set_extend(bset, 0, mat->n_col-1, 0, 0, 0);
1174 if (!bset)
1175 goto error;
1176 } else if (mat->n_col < mat->n_row) {
1177 bset->dim = isl_space_cow(bset->dim);
1178 if (!bset->dim)
1179 goto error;
1180 bset->dim->n_out -= mat->n_row - mat->n_col;
1183 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1184 isl_mat_copy(mat)) < 0)
1185 goto error;
1187 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1188 isl_mat_copy(mat)) < 0)
1189 goto error;
1191 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1192 goto error2;
1194 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1195 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1196 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1197 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1198 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1200 bset = isl_basic_set_simplify(bset);
1201 bset = isl_basic_set_finalize(bset);
1203 return bset;
1204 error:
1205 isl_mat_free(mat);
1206 error2:
1207 isl_basic_set_free(bset);
1208 return NULL;
1211 struct isl_set *isl_set_preimage(struct isl_set *set, struct isl_mat *mat)
1213 int i;
1215 set = isl_set_cow(set);
1216 if (!set)
1217 goto error;
1219 for (i = 0; i < set->n; ++i) {
1220 set->p[i] = isl_basic_set_preimage(set->p[i],
1221 isl_mat_copy(mat));
1222 if (!set->p[i])
1223 goto error;
1225 if (mat->n_col != mat->n_row) {
1226 set->dim = isl_space_cow(set->dim);
1227 if (!set->dim)
1228 goto error;
1229 set->dim->n_out += mat->n_col;
1230 set->dim->n_out -= mat->n_row;
1232 isl_mat_free(mat);
1233 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1234 return set;
1235 error:
1236 isl_set_free(set);
1237 isl_mat_free(mat);
1238 return NULL;
1241 /* Replace the variables x starting at pos in the rows q
1242 * by x' with x = M x' with M the matrix mat.
1243 * That is, replace the corresponding coefficients c by c M.
1245 static int transform(isl_ctx *ctx, isl_int **q, unsigned n,
1246 unsigned pos, __isl_take isl_mat *mat)
1248 int i;
1249 isl_mat *t;
1251 t = isl_mat_sub_alloc6(ctx, q, 0, n, pos, mat->n_row);
1252 t = isl_mat_product(t, mat);
1253 if (!t)
1254 return -1;
1255 for (i = 0; i < n; ++i)
1256 isl_seq_swp_or_cpy(q[i] + pos, t->row[i], t->n_col);
1257 isl_mat_free(t);
1258 return 0;
1261 /* Replace the variables x of type "type" starting at "first" in "bmap"
1262 * by x' with x = M x' with M the matrix trans.
1263 * That is, replace the corresponding coefficients c by c M.
1265 * The transformation matrix should be a square matrix.
1267 __isl_give isl_basic_map *isl_basic_map_transform_dims(
1268 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
1269 __isl_take isl_mat *trans)
1271 isl_ctx *ctx;
1272 unsigned pos;
1274 bmap = isl_basic_map_cow(bmap);
1275 if (!bmap || !trans)
1276 goto error;
1278 ctx = isl_basic_map_get_ctx(bmap);
1279 if (trans->n_row != trans->n_col)
1280 isl_die(trans->ctx, isl_error_invalid,
1281 "expecting square transformation matrix", goto error);
1282 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
1283 isl_die(trans->ctx, isl_error_invalid,
1284 "oversized transformation matrix", goto error);
1286 pos = isl_basic_map_offset(bmap, type) + first;
1288 if (transform(ctx, bmap->eq, bmap->n_eq, pos, isl_mat_copy(trans)) < 0)
1289 goto error;
1290 if (transform(ctx, bmap->ineq, bmap->n_ineq, pos,
1291 isl_mat_copy(trans)) < 0)
1292 goto error;
1293 if (transform(ctx, bmap->div, bmap->n_div, 1 + pos,
1294 isl_mat_copy(trans)) < 0)
1295 goto error;
1297 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1298 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1300 isl_mat_free(trans);
1301 return bmap;
1302 error:
1303 isl_mat_free(trans);
1304 isl_basic_map_free(bmap);
1305 return NULL;
1308 /* Replace the variables x of type "type" starting at "first" in "bset"
1309 * by x' with x = M x' with M the matrix trans.
1310 * That is, replace the corresponding coefficients c by c M.
1312 * The transformation matrix should be a square matrix.
1314 __isl_give isl_basic_set *isl_basic_set_transform_dims(
1315 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
1316 __isl_take isl_mat *trans)
1318 return isl_basic_map_transform_dims(bset, type, first, trans);
1321 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1323 int i, j;
1325 if (!mat) {
1326 fprintf(out, "%*snull mat\n", indent, "");
1327 return;
1330 if (mat->n_row == 0)
1331 fprintf(out, "%*s[]\n", indent, "");
1333 for (i = 0; i < mat->n_row; ++i) {
1334 if (!i)
1335 fprintf(out, "%*s[[", indent, "");
1336 else
1337 fprintf(out, "%*s[", indent+1, "");
1338 for (j = 0; j < mat->n_col; ++j) {
1339 if (j)
1340 fprintf(out, ",");
1341 isl_int_print(out, mat->row[i][j], 0);
1343 if (i == mat->n_row-1)
1344 fprintf(out, "]]\n");
1345 else
1346 fprintf(out, "]\n");
1350 void isl_mat_dump(__isl_keep isl_mat *mat)
1352 isl_mat_print_internal(mat, stderr, 0);
1355 struct isl_mat *isl_mat_drop_cols(struct isl_mat *mat, unsigned col, unsigned n)
1357 int r;
1359 if (n == 0)
1360 return mat;
1362 mat = isl_mat_cow(mat);
1363 if (!mat)
1364 return NULL;
1366 if (col != mat->n_col-n) {
1367 for (r = 0; r < mat->n_row; ++r)
1368 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1369 mat->n_col - col - n);
1371 mat->n_col -= n;
1372 return mat;
1375 struct isl_mat *isl_mat_drop_rows(struct isl_mat *mat, unsigned row, unsigned n)
1377 int r;
1379 mat = isl_mat_cow(mat);
1380 if (!mat)
1381 return NULL;
1383 for (r = row; r+n < mat->n_row; ++r)
1384 mat->row[r] = mat->row[r+n];
1386 mat->n_row -= n;
1387 return mat;
1390 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1391 unsigned col, unsigned n)
1393 isl_mat *ext;
1395 if (!mat)
1396 return NULL;
1397 if (n == 0)
1398 return mat;
1400 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1401 if (!ext)
1402 goto error;
1404 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1405 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1406 col + n, col, mat->n_col - col);
1408 isl_mat_free(mat);
1409 return ext;
1410 error:
1411 isl_mat_free(mat);
1412 return NULL;
1415 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1416 unsigned first, unsigned n)
1418 int i;
1420 if (!mat)
1421 return NULL;
1422 mat = isl_mat_insert_cols(mat, first, n);
1423 if (!mat)
1424 return NULL;
1426 for (i = 0; i < mat->n_row; ++i)
1427 isl_seq_clr(mat->row[i] + first, n);
1429 return mat;
1432 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1434 if (!mat)
1435 return NULL;
1437 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1440 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1441 unsigned row, unsigned n)
1443 isl_mat *ext;
1445 if (!mat)
1446 return NULL;
1447 if (n == 0)
1448 return mat;
1450 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1451 if (!ext)
1452 goto error;
1454 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1455 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1456 mat->n_row - row, 0, 0, mat->n_col);
1458 isl_mat_free(mat);
1459 return ext;
1460 error:
1461 isl_mat_free(mat);
1462 return NULL;
1465 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1467 if (!mat)
1468 return NULL;
1470 return isl_mat_insert_rows(mat, mat->n_row, n);
1473 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1474 unsigned row, unsigned n)
1476 int i;
1478 mat = isl_mat_insert_rows(mat, row, n);
1479 if (!mat)
1480 return NULL;
1482 for (i = 0; i < n; ++i)
1483 isl_seq_clr(mat->row[row + i], mat->n_col);
1485 return mat;
1488 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1490 if (!mat)
1491 return NULL;
1493 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1496 void isl_mat_col_submul(struct isl_mat *mat,
1497 int dst_col, isl_int f, int src_col)
1499 int i;
1501 for (i = 0; i < mat->n_row; ++i)
1502 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1505 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1507 int i;
1509 if (!mat)
1510 return;
1512 for (i = 0; i < mat->n_row; ++i)
1513 isl_int_add(mat->row[i][dst_col],
1514 mat->row[i][dst_col], mat->row[i][src_col]);
1517 void isl_mat_col_mul(struct isl_mat *mat, int dst_col, isl_int f, int src_col)
1519 int i;
1521 for (i = 0; i < mat->n_row; ++i)
1522 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1525 struct isl_mat *isl_mat_unimodular_complete(struct isl_mat *M, int row)
1527 int r;
1528 struct isl_mat *H = NULL, *Q = NULL;
1530 if (!M)
1531 return NULL;
1533 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1534 M->n_row = row;
1535 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1536 M->n_row = M->n_col;
1537 if (!H)
1538 goto error;
1539 for (r = 0; r < row; ++r)
1540 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1541 for (r = row; r < M->n_row; ++r)
1542 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1543 isl_mat_free(H);
1544 isl_mat_free(Q);
1545 return M;
1546 error:
1547 isl_mat_free(H);
1548 isl_mat_free(Q);
1549 isl_mat_free(M);
1550 return NULL;
1553 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1554 __isl_take isl_mat *bot)
1556 struct isl_mat *mat;
1558 if (!top || !bot)
1559 goto error;
1561 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1562 if (top->n_row == 0) {
1563 isl_mat_free(top);
1564 return bot;
1566 if (bot->n_row == 0) {
1567 isl_mat_free(bot);
1568 return top;
1571 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1572 if (!mat)
1573 goto error;
1574 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1575 0, 0, mat->n_col);
1576 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1577 0, 0, mat->n_col);
1578 isl_mat_free(top);
1579 isl_mat_free(bot);
1580 return mat;
1581 error:
1582 isl_mat_free(top);
1583 isl_mat_free(bot);
1584 return NULL;
1587 int isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1589 int i;
1591 if (!mat1 || !mat2)
1592 return -1;
1594 if (mat1->n_row != mat2->n_row)
1595 return 0;
1597 if (mat1->n_col != mat2->n_col)
1598 return 0;
1600 for (i = 0; i < mat1->n_row; ++i)
1601 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1602 return 0;
1604 return 1;
1607 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1609 struct isl_mat *mat;
1611 if (!vec)
1612 return NULL;
1613 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1614 if (!mat)
1615 goto error;
1617 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1619 isl_vec_free(vec);
1620 return mat;
1621 error:
1622 isl_vec_free(vec);
1623 return NULL;
1626 /* Return a copy of row "row" of "mat" as an isl_vec.
1628 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1630 isl_vec *v;
1632 if (!mat)
1633 return NULL;
1634 if (row >= mat->n_row)
1635 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1636 return NULL);
1638 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1639 if (!v)
1640 return NULL;
1641 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1643 return v;
1646 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1647 __isl_take isl_vec *bot)
1649 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1652 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1653 unsigned dst_col, unsigned src_col, unsigned n)
1655 isl_mat *res;
1657 if (!mat)
1658 return NULL;
1659 if (n == 0 || dst_col == src_col)
1660 return mat;
1662 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1663 if (!res)
1664 goto error;
1666 if (dst_col < src_col) {
1667 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1668 0, 0, dst_col);
1669 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1670 dst_col, src_col, n);
1671 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1672 dst_col + n, dst_col, src_col - dst_col);
1673 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1674 src_col + n, src_col + n,
1675 res->n_col - src_col - n);
1676 } else {
1677 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1678 0, 0, src_col);
1679 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1680 src_col, src_col + n, dst_col - src_col);
1681 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1682 dst_col, src_col, n);
1683 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1684 dst_col + n, dst_col + n,
1685 res->n_col - dst_col - n);
1687 isl_mat_free(mat);
1689 return res;
1690 error:
1691 isl_mat_free(mat);
1692 return NULL;
1695 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1696 * Return isl_stat_ok on success and isl_stat_error on failure.
1698 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1700 if (!mat)
1701 return isl_stat_error;
1703 if (row < 0 || row >= mat->n_row)
1704 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
1705 "row out of range", return isl_stat_error);
1706 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1708 return isl_stat_ok;
1711 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1713 int i;
1714 isl_int g;
1716 isl_int_set_si(*gcd, 0);
1717 if (!mat)
1718 return;
1720 isl_int_init(g);
1721 for (i = 0; i < mat->n_row; ++i) {
1722 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1723 isl_int_gcd(*gcd, *gcd, g);
1725 isl_int_clear(g);
1728 /* Return the result of scaling "mat" by a factor of "m".
1730 __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1732 int i;
1734 if (isl_int_is_one(m))
1735 return mat;
1737 mat = isl_mat_cow(mat);
1738 if (!mat)
1739 return NULL;
1741 for (i = 0; i < mat->n_row; ++i)
1742 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1744 return mat;
1747 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1749 int i;
1751 if (isl_int_is_one(m))
1752 return mat;
1754 mat = isl_mat_cow(mat);
1755 if (!mat)
1756 return NULL;
1758 for (i = 0; i < mat->n_row; ++i)
1759 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1761 return mat;
1764 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1765 isl_int m)
1767 if (isl_int_is_one(m))
1768 return mat;
1770 mat = isl_mat_cow(mat);
1771 if (!mat)
1772 return NULL;
1774 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1776 return mat;
1779 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1781 isl_int gcd;
1783 if (!mat)
1784 return NULL;
1786 isl_int_init(gcd);
1787 isl_mat_gcd(mat, &gcd);
1788 mat = isl_mat_scale_down(mat, gcd);
1789 isl_int_clear(gcd);
1791 return mat;
1794 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1796 mat = isl_mat_cow(mat);
1797 if (!mat)
1798 return NULL;
1800 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
1802 return mat;
1805 /* Number of initial non-zero columns.
1807 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
1809 int i;
1811 if (!mat)
1812 return -1;
1814 for (i = 0; i < mat->n_col; ++i)
1815 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
1816 break;
1818 return i;