2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2014 Ecole Normale Superieure
5 * Copyright 2017 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/space.h>
20 #include <isl_mat_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_space_private.h>
23 #include <isl_val_private.h>
25 isl_ctx
*isl_mat_get_ctx(__isl_keep isl_mat
*mat
)
27 return mat
? mat
->ctx
: NULL
;
30 /* Return a hash value that digests "mat".
32 uint32_t isl_mat_get_hash(__isl_keep isl_mat
*mat
)
40 hash
= isl_hash_init();
41 isl_hash_byte(hash
, mat
->n_row
& 0xFF);
42 isl_hash_byte(hash
, mat
->n_col
& 0xFF);
43 for (i
= 0; i
< mat
->n_row
; ++i
) {
46 row_hash
= isl_seq_get_hash(mat
->row
[i
], mat
->n_col
);
47 isl_hash_hash(hash
, row_hash
);
53 struct isl_mat
*isl_mat_alloc(struct isl_ctx
*ctx
,
54 unsigned n_row
, unsigned n_col
)
59 mat
= isl_alloc_type(ctx
, struct isl_mat
);
64 mat
->block
= isl_blk_alloc(ctx
, n_row
* n_col
);
65 if (isl_blk_is_error(mat
->block
))
67 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
68 if (n_row
&& !mat
->row
)
71 for (i
= 0; i
< n_row
; ++i
)
72 mat
->row
[i
] = mat
->block
.data
+ i
* n_col
;
84 isl_blk_free(ctx
, mat
->block
);
89 struct isl_mat
*isl_mat_extend(struct isl_mat
*mat
,
90 unsigned n_row
, unsigned n_col
)
99 if (mat
->max_col
>= n_col
&& mat
->n_row
>= n_row
) {
100 if (mat
->n_col
< n_col
)
105 if (mat
->max_col
< n_col
) {
106 struct isl_mat
*new_mat
;
108 if (n_row
< mat
->n_row
)
110 new_mat
= isl_mat_alloc(mat
->ctx
, n_row
, n_col
);
113 for (i
= 0; i
< mat
->n_row
; ++i
)
114 isl_seq_cpy(new_mat
->row
[i
], mat
->row
[i
], mat
->n_col
);
119 mat
= isl_mat_cow(mat
);
123 old
= mat
->block
.data
;
124 mat
->block
= isl_blk_extend(mat
->ctx
, mat
->block
, n_row
* mat
->max_col
);
125 if (isl_blk_is_error(mat
->block
))
127 row
= isl_realloc_array(mat
->ctx
, mat
->row
, isl_int
*, n_row
);
132 for (i
= 0; i
< mat
->n_row
; ++i
)
133 mat
->row
[i
] = mat
->block
.data
+ (mat
->row
[i
] - old
);
134 for (i
= mat
->n_row
; i
< n_row
; ++i
)
135 mat
->row
[i
] = mat
->block
.data
+ i
* mat
->max_col
;
137 if (mat
->n_col
< n_col
)
146 __isl_give isl_mat
*isl_mat_sub_alloc6(isl_ctx
*ctx
, isl_int
**row
,
147 unsigned first_row
, unsigned n_row
, unsigned first_col
, unsigned n_col
)
152 mat
= isl_alloc_type(ctx
, struct isl_mat
);
155 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
156 if (n_row
&& !mat
->row
)
158 for (i
= 0; i
< n_row
; ++i
)
159 mat
->row
[i
] = row
[first_row
+i
] + first_col
;
165 mat
->block
= isl_blk_empty();
166 mat
->flags
= ISL_MAT_BORROWED
;
173 __isl_give isl_mat
*isl_mat_sub_alloc(__isl_keep isl_mat
*mat
,
174 unsigned first_row
, unsigned n_row
, unsigned first_col
, unsigned n_col
)
178 return isl_mat_sub_alloc6(mat
->ctx
, mat
->row
, first_row
, n_row
,
182 void isl_mat_sub_copy(struct isl_ctx
*ctx
, isl_int
**dst
, isl_int
**src
,
183 unsigned n_row
, unsigned dst_col
, unsigned src_col
, unsigned n_col
)
187 for (i
= 0; i
< n_row
; ++i
)
188 isl_seq_cpy(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
191 void isl_mat_sub_neg(struct isl_ctx
*ctx
, isl_int
**dst
, isl_int
**src
,
192 unsigned n_row
, unsigned dst_col
, unsigned src_col
, unsigned n_col
)
196 for (i
= 0; i
< n_row
; ++i
)
197 isl_seq_neg(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
200 __isl_give isl_mat
*isl_mat_copy(__isl_keep isl_mat
*mat
)
209 __isl_give isl_mat
*isl_mat_dup(__isl_keep isl_mat
*mat
)
212 struct isl_mat
*mat2
;
216 mat2
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
219 for (i
= 0; i
< mat
->n_row
; ++i
)
220 isl_seq_cpy(mat2
->row
[i
], mat
->row
[i
], mat
->n_col
);
224 __isl_give isl_mat
*isl_mat_cow(__isl_take isl_mat
*mat
)
226 struct isl_mat
*mat2
;
230 if (mat
->ref
== 1 && !ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
233 mat2
= isl_mat_dup(mat
);
238 __isl_null isl_mat
*isl_mat_free(__isl_take isl_mat
*mat
)
246 if (!ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
247 isl_blk_free(mat
->ctx
, mat
->block
);
248 isl_ctx_deref(mat
->ctx
);
255 int isl_mat_rows(__isl_keep isl_mat
*mat
)
257 return mat
? mat
->n_row
: -1;
260 int isl_mat_cols(__isl_keep isl_mat
*mat
)
262 return mat
? mat
->n_col
: -1;
265 /* Check that "col" is a valid column position for "mat".
267 static isl_stat
check_col(__isl_keep isl_mat
*mat
, int col
)
270 return isl_stat_error
;
271 if (col
< 0 || col
>= mat
->n_col
)
272 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
273 "column out of range", return isl_stat_error
);
277 /* Check that "row" is a valid row position for "mat".
279 static isl_stat
check_row(__isl_keep isl_mat
*mat
, int row
)
282 return isl_stat_error
;
283 if (row
< 0 || row
>= mat
->n_row
)
284 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
285 "row out of range", return isl_stat_error
);
289 /* Check that there are "n" columns starting at position "first" in "mat".
291 static isl_stat
check_col_range(__isl_keep isl_mat
*mat
, unsigned first
,
295 return isl_stat_error
;
296 if (first
+ n
> mat
->n_col
|| first
+ n
< first
)
297 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
298 "column position or range out of bounds",
299 return isl_stat_error
);
303 /* Check that there are "n" rows starting at position "first" in "mat".
305 static isl_stat
check_row_range(__isl_keep isl_mat
*mat
, unsigned first
,
309 return isl_stat_error
;
310 if (first
+ n
> mat
->n_row
|| first
+ n
< first
)
311 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
312 "row position or range out of bounds",
313 return isl_stat_error
);
317 int isl_mat_get_element(__isl_keep isl_mat
*mat
, int row
, int col
, isl_int
*v
)
319 if (check_row(mat
, row
) < 0)
321 if (check_col(mat
, col
) < 0)
323 isl_int_set(*v
, mat
->row
[row
][col
]);
327 /* Extract the element at row "row", oolumn "col" of "mat".
329 __isl_give isl_val
*isl_mat_get_element_val(__isl_keep isl_mat
*mat
,
334 if (check_row(mat
, row
) < 0)
336 if (check_col(mat
, col
) < 0)
338 ctx
= isl_mat_get_ctx(mat
);
339 return isl_val_int_from_isl_int(ctx
, mat
->row
[row
][col
]);
342 __isl_give isl_mat
*isl_mat_set_element(__isl_take isl_mat
*mat
,
343 int row
, int col
, isl_int v
)
345 mat
= isl_mat_cow(mat
);
346 if (check_row(mat
, row
) < 0)
347 return isl_mat_free(mat
);
348 if (check_col(mat
, col
) < 0)
349 return isl_mat_free(mat
);
350 isl_int_set(mat
->row
[row
][col
], v
);
354 __isl_give isl_mat
*isl_mat_set_element_si(__isl_take isl_mat
*mat
,
355 int row
, int col
, int v
)
357 mat
= isl_mat_cow(mat
);
358 if (check_row(mat
, row
) < 0)
359 return isl_mat_free(mat
);
360 if (check_col(mat
, col
) < 0)
361 return isl_mat_free(mat
);
362 isl_int_set_si(mat
->row
[row
][col
], v
);
366 /* Replace the element at row "row", column "col" of "mat" by "v".
368 __isl_give isl_mat
*isl_mat_set_element_val(__isl_take isl_mat
*mat
,
369 int row
, int col
, __isl_take isl_val
*v
)
372 return isl_mat_free(mat
);
373 if (!isl_val_is_int(v
))
374 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
375 "expecting integer value", goto error
);
376 mat
= isl_mat_set_element(mat
, row
, col
, v
->n
);
381 return isl_mat_free(mat
);
384 __isl_give isl_mat
*isl_mat_diag(isl_ctx
*ctx
, unsigned n_row
, isl_int d
)
389 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
392 for (i
= 0; i
< n_row
; ++i
) {
393 isl_seq_clr(mat
->row
[i
], i
);
394 isl_int_set(mat
->row
[i
][i
], d
);
395 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
401 /* Create an "n_row" by "n_col" matrix with zero elements.
403 __isl_give isl_mat
*isl_mat_zero(isl_ctx
*ctx
, unsigned n_row
, unsigned n_col
)
408 mat
= isl_mat_alloc(ctx
, n_row
, n_col
);
411 for (i
= 0; i
< n_row
; ++i
)
412 isl_seq_clr(mat
->row
[i
], n_col
);
417 __isl_give isl_mat
*isl_mat_identity(isl_ctx
*ctx
, unsigned n_row
)
421 return isl_mat_diag(ctx
, n_row
, ctx
->one
);
424 /* Is "mat" a (possibly scaled) identity matrix?
426 int isl_mat_is_scaled_identity(__isl_keep isl_mat
*mat
)
432 if (mat
->n_row
!= mat
->n_col
)
435 for (i
= 0; i
< mat
->n_row
; ++i
) {
436 if (isl_seq_first_non_zero(mat
->row
[i
], i
) != -1)
438 if (isl_int_ne(mat
->row
[0][0], mat
->row
[i
][i
]))
440 if (isl_seq_first_non_zero(mat
->row
[i
] + i
+ 1,
441 mat
->n_col
- (i
+ 1)) != -1)
448 __isl_give isl_vec
*isl_mat_vec_product(__isl_take isl_mat
*mat
,
449 __isl_take isl_vec
*vec
)
452 struct isl_vec
*prod
;
457 isl_assert(mat
->ctx
, mat
->n_col
== vec
->size
, goto error
);
459 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
463 for (i
= 0; i
< prod
->size
; ++i
)
464 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
465 &prod
->block
.data
[i
]);
475 __isl_give isl_vec
*isl_mat_vec_inverse_product(__isl_take isl_mat
*mat
,
476 __isl_take isl_vec
*vec
)
478 struct isl_mat
*vec_mat
;
483 vec_mat
= isl_mat_alloc(vec
->ctx
, vec
->size
, 1);
486 for (i
= 0; i
< vec
->size
; ++i
)
487 isl_int_set(vec_mat
->row
[i
][0], vec
->el
[i
]);
488 vec_mat
= isl_mat_inverse_product(mat
, vec_mat
);
492 vec
= isl_vec_alloc(vec_mat
->ctx
, vec_mat
->n_row
);
494 for (i
= 0; i
< vec
->size
; ++i
)
495 isl_int_set(vec
->el
[i
], vec_mat
->row
[i
][0]);
496 isl_mat_free(vec_mat
);
504 __isl_give isl_vec
*isl_vec_mat_product(__isl_take isl_vec
*vec
,
505 __isl_take isl_mat
*mat
)
508 struct isl_vec
*prod
;
513 isl_assert(mat
->ctx
, mat
->n_row
== vec
->size
, goto error
);
515 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_col
);
519 for (i
= 0; i
< prod
->size
; ++i
) {
520 isl_int_set_si(prod
->el
[i
], 0);
521 for (j
= 0; j
< vec
->size
; ++j
)
522 isl_int_addmul(prod
->el
[i
], vec
->el
[j
], mat
->row
[j
][i
]);
533 __isl_give isl_mat
*isl_mat_aff_direct_sum(__isl_take isl_mat
*left
,
534 __isl_take isl_mat
*right
)
542 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
543 isl_assert(left
->ctx
, left
->n_row
>= 1, goto error
);
544 isl_assert(left
->ctx
, left
->n_col
>= 1, goto error
);
545 isl_assert(left
->ctx
, right
->n_col
>= 1, goto error
);
546 isl_assert(left
->ctx
,
547 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
549 isl_assert(left
->ctx
,
550 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
553 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
556 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
557 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
558 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
560 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
561 for (i
= 1; i
< sum
->n_row
; ++i
) {
562 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
563 isl_int_addmul(sum
->row
[i
][0],
564 right
->row
[0][0], right
->row
[i
][0]);
565 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
567 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
568 right
->row
[i
]+1, right
->row
[0][0],
572 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
573 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
583 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
584 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
587 for (r
= row
; r
< M
->n_row
; ++r
)
588 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
590 for (r
= 0; r
< (*U
)->n_row
; ++r
)
591 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
594 isl_mat_swap_rows(*Q
, i
, j
);
597 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
598 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
601 for (r
= row
; r
< M
->n_row
; ++r
)
602 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
604 for (r
= 0; r
< (*U
)->n_row
; ++r
)
605 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
608 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
609 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
613 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
614 struct isl_mat
**Q
, unsigned row
, unsigned col
)
617 for (r
= row
; r
< M
->n_row
; ++r
)
618 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
620 for (r
= 0; r
< (*U
)->n_row
; ++r
)
621 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
624 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
627 /* Given matrix M, compute
632 * with U and Q unimodular matrices and H a matrix in column echelon form
633 * such that on each echelon row the entries in the non-echelon column
634 * are non-negative (if neg == 0) or non-positive (if neg == 1)
635 * and strictly smaller (in absolute value) than the entries in the echelon
637 * If U or Q are NULL, then these matrices are not computed.
639 __isl_give isl_mat
*isl_mat_left_hermite(__isl_take isl_mat
*M
, int neg
,
640 __isl_give isl_mat
**U
, __isl_give isl_mat
**Q
)
655 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
660 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
667 for (row
= 0; row
< M
->n_row
; ++row
) {
669 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
674 exchange(M
, U
, Q
, row
, first
, col
);
675 if (isl_int_is_neg(M
->row
[row
][col
]))
676 oppose(M
, U
, Q
, row
, col
);
678 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
679 M
->n_col
-first
)) != -1) {
681 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
682 subtract(M
, U
, Q
, row
, col
, first
, c
);
683 if (!isl_int_is_zero(M
->row
[row
][first
]))
684 exchange(M
, U
, Q
, row
, first
, col
);
688 for (i
= 0; i
< col
; ++i
) {
689 if (isl_int_is_zero(M
->row
[row
][i
]))
692 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
694 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
695 if (isl_int_is_zero(c
))
697 subtract(M
, U
, Q
, row
, col
, i
, c
);
717 /* Use row "row" of "mat" to eliminate column "col" from all other rows.
719 static __isl_give isl_mat
*eliminate(__isl_take isl_mat
*mat
, int row
, int col
)
727 ctx
= isl_mat_get_ctx(mat
);
728 nr
= isl_mat_rows(mat
);
729 nc
= isl_mat_cols(mat
);
731 for (k
= 0; k
< nr
; ++k
) {
734 if (isl_int_is_zero(mat
->row
[k
][col
]))
736 mat
= isl_mat_cow(mat
);
739 isl_seq_elim(mat
->row
[k
], mat
->row
[row
], col
, nc
, NULL
);
740 isl_seq_normalize(ctx
, mat
->row
[k
], nc
);
746 /* Perform Gaussian elimination on the rows of "mat", but start
747 * from the final row and the final column.
748 * Any zero rows that result from the elimination are removed.
750 * In particular, for each column from last to first,
751 * look for the last row with a non-zero coefficient in that column,
752 * move it last (but before other rows moved last in previous steps) and
753 * use it to eliminate the column from the other rows.
755 __isl_give isl_mat
*isl_mat_reverse_gauss(__isl_take isl_mat
*mat
)
757 int k
, row
, last
, nr
, nc
;
762 nr
= isl_mat_rows(mat
);
763 nc
= isl_mat_cols(mat
);
766 for (row
= nr
- 1; row
>= 0; --row
) {
767 for (; last
>= 0; --last
) {
768 for (k
= row
; k
>= 0; --k
)
769 if (!isl_int_is_zero(mat
->row
[k
][last
]))
777 mat
= isl_mat_swap_rows(mat
, k
, row
);
780 if (isl_int_is_neg(mat
->row
[row
][last
]))
781 mat
= isl_mat_row_neg(mat
, row
);
782 mat
= eliminate(mat
, row
, last
);
786 mat
= isl_mat_drop_rows(mat
, 0, row
+ 1);
791 /* Negate the lexicographically negative rows of "mat" such that
792 * all rows in the result are lexicographically non-negative.
794 __isl_give isl_mat
*isl_mat_lexnonneg_rows(__isl_take isl_mat
*mat
)
801 nr
= isl_mat_rows(mat
);
802 nc
= isl_mat_cols(mat
);
804 for (i
= 0; i
< nr
; ++i
) {
807 pos
= isl_seq_first_non_zero(mat
->row
[i
], nc
);
810 if (isl_int_is_nonneg(mat
->row
[i
][pos
]))
812 mat
= isl_mat_row_neg(mat
, i
);
820 /* Given a matrix "H" is column echelon form, what is the first
821 * zero column? That is how many initial columns are non-zero?
822 * Start looking at column "first_col" and only consider
823 * the columns to be of size "n_row".
824 * "H" is assumed to be non-NULL.
826 * Since "H" is in column echelon form, the first non-zero entry
827 * in a column is always in a later position compared to the previous column.
829 static int hermite_first_zero_col(__isl_keep isl_mat
*H
, int first_col
,
834 for (col
= first_col
, row
= 0; col
< H
->n_col
; ++col
) {
835 for (; row
< n_row
; ++row
)
836 if (!isl_int_is_zero(H
->row
[row
][col
]))
845 /* Return the rank of "mat", or -1 in case of error.
847 int isl_mat_rank(__isl_keep isl_mat
*mat
)
852 H
= isl_mat_left_hermite(isl_mat_copy(mat
), 0, NULL
, NULL
);
856 rank
= hermite_first_zero_col(H
, 0, H
->n_row
);
862 __isl_give isl_mat
*isl_mat_right_kernel(__isl_take isl_mat
*mat
)
865 struct isl_mat
*U
= NULL
;
868 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
872 rank
= hermite_first_zero_col(mat
, 0, mat
->n_row
);
873 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
876 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
886 __isl_give isl_mat
*isl_mat_lin_to_aff(__isl_take isl_mat
*mat
)
889 struct isl_mat
*mat2
;
893 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
896 isl_int_set_si(mat2
->row
[0][0], 1);
897 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
898 for (i
= 0; i
< mat
->n_row
; ++i
) {
899 isl_int_set_si(mat2
->row
[1+i
][0], 0);
900 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
909 /* Given two matrices M1 and M2, return the block matrix
914 __isl_give isl_mat
*isl_mat_diagonal(__isl_take isl_mat
*mat1
,
915 __isl_take isl_mat
*mat2
)
923 mat
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
924 mat1
->n_col
+ mat2
->n_col
);
927 for (i
= 0; i
< mat1
->n_row
; ++i
) {
928 isl_seq_cpy(mat
->row
[i
], mat1
->row
[i
], mat1
->n_col
);
929 isl_seq_clr(mat
->row
[i
] + mat1
->n_col
, mat2
->n_col
);
931 for (i
= 0; i
< mat2
->n_row
; ++i
) {
932 isl_seq_clr(mat
->row
[mat1
->n_row
+ i
], mat1
->n_col
);
933 isl_seq_cpy(mat
->row
[mat1
->n_row
+ i
] + mat1
->n_col
,
934 mat2
->row
[i
], mat2
->n_col
);
945 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
949 for (i
= 0; i
< n_row
; ++i
)
950 if (!isl_int_is_zero(row
[i
][col
]))
955 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
957 int i
, min
= row_first_non_zero(row
, n_row
, col
);
960 for (i
= min
+ 1; i
< n_row
; ++i
) {
961 if (isl_int_is_zero(row
[i
][col
]))
963 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
969 static isl_stat
inv_exchange(__isl_keep isl_mat
**left
,
970 __isl_keep isl_mat
**right
, unsigned i
, unsigned j
)
972 *left
= isl_mat_swap_rows(*left
, i
, j
);
973 *right
= isl_mat_swap_rows(*right
, i
, j
);
975 if (!*left
|| !*right
)
976 return isl_stat_error
;
980 static void inv_oppose(
981 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
983 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
984 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
987 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
988 unsigned row
, unsigned i
, isl_int m
)
991 isl_seq_combine(left
->row
[i
]+row
,
992 left
->ctx
->one
, left
->row
[i
]+row
,
993 m
, left
->row
[row
]+row
,
995 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
996 m
, right
->row
[row
], right
->n_col
);
999 /* Compute inv(left)*right
1001 __isl_give isl_mat
*isl_mat_inverse_product(__isl_take isl_mat
*left
,
1002 __isl_take isl_mat
*right
)
1007 if (!left
|| !right
)
1010 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
1011 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
1013 if (left
->n_row
== 0) {
1018 left
= isl_mat_cow(left
);
1019 right
= isl_mat_cow(right
);
1020 if (!left
|| !right
)
1025 for (row
= 0; row
< left
->n_row
; ++row
) {
1026 int pivot
, first
, i
, off
;
1027 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
1031 isl_assert(left
->ctx
, pivot
>= 0, goto error
);
1035 if (inv_exchange(&left
, &right
, pivot
, row
) < 0)
1037 if (isl_int_is_neg(left
->row
[row
][row
]))
1038 inv_oppose(left
, right
, row
);
1040 while ((off
= row_first_non_zero(left
->row
+first
,
1041 left
->n_row
-first
, row
)) != -1) {
1043 isl_int_fdiv_q(a
, left
->row
[first
][row
],
1044 left
->row
[row
][row
]);
1045 inv_subtract(left
, right
, row
, first
, a
);
1046 if (!isl_int_is_zero(left
->row
[first
][row
])) {
1047 if (inv_exchange(&left
, &right
, row
, first
) < 0)
1053 for (i
= 0; i
< row
; ++i
) {
1054 if (isl_int_is_zero(left
->row
[i
][row
]))
1056 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
1057 isl_int_divexact(b
, left
->row
[i
][row
], a
);
1058 isl_int_divexact(a
, left
->row
[row
][row
], a
);
1060 isl_seq_combine(left
->row
[i
] + i
,
1061 a
, left
->row
[i
] + i
,
1062 b
, left
->row
[row
] + i
,
1064 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
1065 b
, right
->row
[row
], right
->n_col
);
1070 isl_int_set(a
, left
->row
[0][0]);
1071 for (row
= 1; row
< left
->n_row
; ++row
)
1072 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
1073 if (isl_int_is_zero(a
)){
1075 isl_assert(left
->ctx
, 0, goto error
);
1077 for (row
= 0; row
< left
->n_row
; ++row
) {
1078 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
1079 if (isl_int_is_one(left
->row
[row
][row
]))
1081 isl_seq_scale(right
->row
[row
], right
->row
[row
],
1082 left
->row
[row
][row
], right
->n_col
);
1090 isl_mat_free(right
);
1094 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
1098 for (i
= 0; i
< mat
->n_row
; ++i
)
1099 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
1102 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
1103 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
1109 for (i
= 0; i
< mat
->n_row
; ++i
) {
1110 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
1111 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
1112 isl_int_set(mat
->row
[i
][dst
], tmp
);
1117 __isl_give isl_mat
*isl_mat_right_inverse(__isl_take isl_mat
*mat
)
1119 struct isl_mat
*inv
;
1123 mat
= isl_mat_cow(mat
);
1127 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
1128 inv
= isl_mat_cow(inv
);
1134 for (row
= 0; row
< mat
->n_row
; ++row
) {
1135 int pivot
, first
, i
, off
;
1136 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
1140 isl_assert(mat
->ctx
, pivot
>= 0, goto error
);
1144 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
1145 if (isl_int_is_neg(mat
->row
[row
][row
]))
1146 oppose(mat
, &inv
, NULL
, row
, row
);
1148 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
1149 mat
->n_col
-first
)) != -1) {
1151 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
1152 mat
->row
[row
][row
]);
1153 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
1154 if (!isl_int_is_zero(mat
->row
[row
][first
]))
1155 exchange(mat
, &inv
, NULL
, row
, row
, first
);
1159 for (i
= 0; i
< row
; ++i
) {
1160 if (isl_int_is_zero(mat
->row
[row
][i
]))
1162 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
1163 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
1164 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
1166 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
1167 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
1172 isl_int_set(a
, mat
->row
[0][0]);
1173 for (row
= 1; row
< mat
->n_row
; ++row
)
1174 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
1175 if (isl_int_is_zero(a
)){
1179 for (row
= 0; row
< mat
->n_row
; ++row
) {
1180 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
1181 if (isl_int_is_one(mat
->row
[row
][row
]))
1183 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
1196 __isl_give isl_mat
*isl_mat_transpose(__isl_take isl_mat
*mat
)
1198 struct isl_mat
*transpose
= NULL
;
1204 if (mat
->n_col
== mat
->n_row
) {
1205 mat
= isl_mat_cow(mat
);
1208 for (i
= 0; i
< mat
->n_row
; ++i
)
1209 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
1210 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
1213 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
1216 for (i
= 0; i
< mat
->n_row
; ++i
)
1217 for (j
= 0; j
< mat
->n_col
; ++j
)
1218 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
1226 __isl_give isl_mat
*isl_mat_swap_cols(__isl_take isl_mat
*mat
,
1227 unsigned i
, unsigned j
)
1231 mat
= isl_mat_cow(mat
);
1232 if (check_col_range(mat
, i
, 1) < 0 ||
1233 check_col_range(mat
, j
, 1) < 0)
1234 return isl_mat_free(mat
);
1236 for (r
= 0; r
< mat
->n_row
; ++r
)
1237 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
1241 __isl_give isl_mat
*isl_mat_swap_rows(__isl_take isl_mat
*mat
,
1242 unsigned i
, unsigned j
)
1248 mat
= isl_mat_cow(mat
);
1249 if (check_row_range(mat
, i
, 1) < 0 ||
1250 check_row_range(mat
, j
, 1) < 0)
1251 return isl_mat_free(mat
);
1254 mat
->row
[i
] = mat
->row
[j
];
1259 /* Calculate the product of two matrices.
1261 * This function is optimized for operand matrices that contain many zeros and
1262 * skips multiplications where we know one of the operands is zero.
1264 __isl_give isl_mat
*isl_mat_product(__isl_take isl_mat
*left
,
1265 __isl_take isl_mat
*right
)
1268 struct isl_mat
*prod
;
1270 if (!left
|| !right
)
1272 isl_assert(left
->ctx
, left
->n_col
== right
->n_row
, goto error
);
1273 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
1276 if (left
->n_col
== 0) {
1277 for (i
= 0; i
< prod
->n_row
; ++i
)
1278 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
1280 isl_mat_free(right
);
1283 for (i
= 0; i
< prod
->n_row
; ++i
) {
1284 for (j
= 0; j
< prod
->n_col
; ++j
)
1285 isl_int_mul(prod
->row
[i
][j
],
1286 left
->row
[i
][0], right
->row
[0][j
]);
1287 for (k
= 1; k
< left
->n_col
; ++k
) {
1288 if (isl_int_is_zero(left
->row
[i
][k
]))
1290 for (j
= 0; j
< prod
->n_col
; ++j
)
1291 isl_int_addmul(prod
->row
[i
][j
],
1292 left
->row
[i
][k
], right
->row
[k
][j
]);
1296 isl_mat_free(right
);
1300 isl_mat_free(right
);
1304 /* Replace the variables x in the rows q by x' given by x = M x',
1305 * with M the matrix mat.
1307 * If the number of new variables is greater than the original
1308 * number of variables, then the rows q have already been
1309 * preextended. If the new number is smaller, then the coefficients
1310 * of the divs, which are not changed, need to be shifted down.
1311 * The row q may be the equalities, the inequalities or the
1312 * div expressions. In the latter case, has_div is true and
1313 * we need to take into account the extra denominator column.
1315 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1316 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
1322 if (mat
->n_col
>= mat
->n_row
)
1325 e
= mat
->n_row
- mat
->n_col
;
1327 for (i
= 0; i
< n
; ++i
)
1328 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
1329 t
= isl_mat_sub_alloc6(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
1330 t
= isl_mat_product(t
, mat
);
1333 for (i
= 0; i
< n
; ++i
) {
1334 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
1335 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
1336 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
1337 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
1343 /* Replace the variables x in bset by x' given by x = M x', with
1346 * If there are fewer variables x' then there are x, then we perform
1347 * the transformation in place, which means that, in principle,
1348 * this frees up some extra variables as the number
1349 * of columns remains constant, but we would have to extend
1350 * the div array too as the number of rows in this array is assumed
1351 * to be equal to extra.
1353 __isl_give isl_basic_set
*isl_basic_set_preimage(
1354 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*mat
)
1356 struct isl_ctx
*ctx
;
1362 bset
= isl_basic_set_cow(bset
);
1366 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
1367 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
1368 isl_assert(ctx
, mat
->n_col
> 0, goto error
);
1370 if (mat
->n_col
> mat
->n_row
) {
1371 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0, 0, 0);
1374 } else if (mat
->n_col
< mat
->n_row
) {
1375 bset
->dim
= isl_space_cow(bset
->dim
);
1378 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
1381 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
1382 isl_mat_copy(mat
)) < 0)
1385 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
1386 isl_mat_copy(mat
)) < 0)
1389 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
1392 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1393 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1394 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1395 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
1396 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
1398 bset
= isl_basic_set_simplify(bset
);
1399 bset
= isl_basic_set_finalize(bset
);
1405 isl_basic_set_free(bset
);
1409 __isl_give isl_set
*isl_set_preimage(
1410 __isl_take isl_set
*set
, __isl_take isl_mat
*mat
)
1414 set
= isl_set_cow(set
);
1418 for (i
= 0; i
< set
->n
; ++i
) {
1419 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
1424 if (mat
->n_col
!= mat
->n_row
) {
1425 set
->dim
= isl_space_cow(set
->dim
);
1428 set
->dim
->n_out
+= mat
->n_col
;
1429 set
->dim
->n_out
-= mat
->n_row
;
1432 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1440 /* Replace the variables x starting at "first_col" in the rows "rows"
1441 * of some coefficient matrix by x' with x = M x' with M the matrix mat.
1442 * That is, replace the corresponding coefficients c by c M.
1444 isl_stat
isl_mat_sub_transform(isl_int
**row
, unsigned n_row
,
1445 unsigned first_col
, __isl_take isl_mat
*mat
)
1452 return isl_stat_error
;
1453 ctx
= isl_mat_get_ctx(mat
);
1454 t
= isl_mat_sub_alloc6(ctx
, row
, 0, n_row
, first_col
, mat
->n_row
);
1455 t
= isl_mat_product(t
, mat
);
1457 return isl_stat_error
;
1458 for (i
= 0; i
< n_row
; ++i
)
1459 isl_seq_swp_or_cpy(row
[i
] + first_col
, t
->row
[i
], t
->n_col
);
1464 void isl_mat_print_internal(__isl_keep isl_mat
*mat
, FILE *out
, int indent
)
1469 fprintf(out
, "%*snull mat\n", indent
, "");
1473 if (mat
->n_row
== 0)
1474 fprintf(out
, "%*s[]\n", indent
, "");
1476 for (i
= 0; i
< mat
->n_row
; ++i
) {
1478 fprintf(out
, "%*s[[", indent
, "");
1480 fprintf(out
, "%*s[", indent
+1, "");
1481 for (j
= 0; j
< mat
->n_col
; ++j
) {
1484 isl_int_print(out
, mat
->row
[i
][j
], 0);
1486 if (i
== mat
->n_row
-1)
1487 fprintf(out
, "]]\n");
1489 fprintf(out
, "]\n");
1493 void isl_mat_dump(__isl_keep isl_mat
*mat
)
1495 isl_mat_print_internal(mat
, stderr
, 0);
1498 __isl_give isl_mat
*isl_mat_drop_cols(__isl_take isl_mat
*mat
,
1499 unsigned col
, unsigned n
)
1506 mat
= isl_mat_cow(mat
);
1507 if (check_col_range(mat
, col
, n
) < 0)
1508 return isl_mat_free(mat
);
1510 if (col
!= mat
->n_col
-n
) {
1511 for (r
= 0; r
< mat
->n_row
; ++r
)
1512 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
1513 mat
->n_col
- col
- n
);
1519 __isl_give isl_mat
*isl_mat_drop_rows(__isl_take isl_mat
*mat
,
1520 unsigned row
, unsigned n
)
1524 mat
= isl_mat_cow(mat
);
1525 if (check_row_range(mat
, row
, n
) < 0)
1526 return isl_mat_free(mat
);
1528 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
1529 mat
->row
[r
] = mat
->row
[r
+n
];
1535 __isl_give isl_mat
*isl_mat_insert_cols(__isl_take isl_mat
*mat
,
1536 unsigned col
, unsigned n
)
1540 if (check_col_range(mat
, col
, 0) < 0)
1541 return isl_mat_free(mat
);
1545 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
+ n
);
1549 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
, 0, 0, col
);
1550 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
,
1551 col
+ n
, col
, mat
->n_col
- col
);
1560 __isl_give isl_mat
*isl_mat_insert_zero_cols(__isl_take isl_mat
*mat
,
1561 unsigned first
, unsigned n
)
1567 mat
= isl_mat_insert_cols(mat
, first
, n
);
1571 for (i
= 0; i
< mat
->n_row
; ++i
)
1572 isl_seq_clr(mat
->row
[i
] + first
, n
);
1577 __isl_give isl_mat
*isl_mat_add_zero_cols(__isl_take isl_mat
*mat
, unsigned n
)
1582 return isl_mat_insert_zero_cols(mat
, mat
->n_col
, n
);
1585 __isl_give isl_mat
*isl_mat_insert_rows(__isl_take isl_mat
*mat
,
1586 unsigned row
, unsigned n
)
1590 if (check_row_range(mat
, row
, 0) < 0)
1591 return isl_mat_free(mat
);
1595 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
+ n
, mat
->n_col
);
1599 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, row
, 0, 0, mat
->n_col
);
1600 isl_mat_sub_copy(mat
->ctx
, ext
->row
+ row
+ n
, mat
->row
+ row
,
1601 mat
->n_row
- row
, 0, 0, mat
->n_col
);
1610 __isl_give isl_mat
*isl_mat_add_rows(__isl_take isl_mat
*mat
, unsigned n
)
1615 return isl_mat_insert_rows(mat
, mat
->n_row
, n
);
1618 __isl_give isl_mat
*isl_mat_insert_zero_rows(__isl_take isl_mat
*mat
,
1619 unsigned row
, unsigned n
)
1623 mat
= isl_mat_insert_rows(mat
, row
, n
);
1627 for (i
= 0; i
< n
; ++i
)
1628 isl_seq_clr(mat
->row
[row
+ i
], mat
->n_col
);
1633 __isl_give isl_mat
*isl_mat_add_zero_rows(__isl_take isl_mat
*mat
, unsigned n
)
1638 return isl_mat_insert_zero_rows(mat
, mat
->n_row
, n
);
1641 void isl_mat_col_submul(struct isl_mat
*mat
,
1642 int dst_col
, isl_int f
, int src_col
)
1646 for (i
= 0; i
< mat
->n_row
; ++i
)
1647 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1650 void isl_mat_col_add(__isl_keep isl_mat
*mat
, int dst_col
, int src_col
)
1657 for (i
= 0; i
< mat
->n_row
; ++i
)
1658 isl_int_add(mat
->row
[i
][dst_col
],
1659 mat
->row
[i
][dst_col
], mat
->row
[i
][src_col
]);
1662 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1666 for (i
= 0; i
< mat
->n_row
; ++i
)
1667 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1670 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1671 * return the result.
1673 __isl_give isl_mat
*isl_mat_col_addmul(__isl_take isl_mat
*mat
, int dst_col
,
1674 isl_int f
, int src_col
)
1678 if (check_col(mat
, dst_col
) < 0 || check_col(mat
, src_col
) < 0)
1679 return isl_mat_free(mat
);
1681 for (i
= 0; i
< mat
->n_row
; ++i
) {
1682 if (isl_int_is_zero(mat
->row
[i
][src_col
]))
1684 mat
= isl_mat_cow(mat
);
1687 isl_int_addmul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1693 /* Negate column "col" of "mat" and return the result.
1695 __isl_give isl_mat
*isl_mat_col_neg(__isl_take isl_mat
*mat
, int col
)
1699 if (check_col(mat
, col
) < 0)
1700 return isl_mat_free(mat
);
1702 for (i
= 0; i
< mat
->n_row
; ++i
) {
1703 if (isl_int_is_zero(mat
->row
[i
][col
]))
1705 mat
= isl_mat_cow(mat
);
1708 isl_int_neg(mat
->row
[i
][col
], mat
->row
[i
][col
]);
1714 /* Negate row "row" of "mat" and return the result.
1716 __isl_give isl_mat
*isl_mat_row_neg(__isl_take isl_mat
*mat
, int row
)
1718 if (check_row(mat
, row
) < 0)
1719 return isl_mat_free(mat
);
1720 if (isl_seq_first_non_zero(mat
->row
[row
], mat
->n_col
) == -1)
1722 mat
= isl_mat_cow(mat
);
1725 isl_seq_neg(mat
->row
[row
], mat
->row
[row
], mat
->n_col
);
1729 __isl_give isl_mat
*isl_mat_unimodular_complete(__isl_take isl_mat
*M
, int row
)
1732 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1737 isl_assert(M
->ctx
, M
->n_row
== M
->n_col
, goto error
);
1739 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1740 M
->n_row
= M
->n_col
;
1743 for (r
= 0; r
< row
; ++r
)
1744 isl_assert(M
->ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1745 for (r
= row
; r
< M
->n_row
; ++r
)
1746 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);
1757 __isl_give isl_mat
*isl_mat_concat(__isl_take isl_mat
*top
,
1758 __isl_take isl_mat
*bot
)
1760 struct isl_mat
*mat
;
1765 isl_assert(top
->ctx
, top
->n_col
== bot
->n_col
, goto error
);
1766 if (top
->n_row
== 0) {
1770 if (bot
->n_row
== 0) {
1775 mat
= isl_mat_alloc(top
->ctx
, top
->n_row
+ bot
->n_row
, top
->n_col
);
1778 isl_mat_sub_copy(mat
->ctx
, mat
->row
, top
->row
, top
->n_row
,
1780 isl_mat_sub_copy(mat
->ctx
, mat
->row
+ top
->n_row
, bot
->row
, bot
->n_row
,
1791 isl_bool
isl_mat_is_equal(__isl_keep isl_mat
*mat1
, __isl_keep isl_mat
*mat2
)
1796 return isl_bool_error
;
1798 if (mat1
->n_row
!= mat2
->n_row
)
1799 return isl_bool_false
;
1801 if (mat1
->n_col
!= mat2
->n_col
)
1802 return isl_bool_false
;
1804 for (i
= 0; i
< mat1
->n_row
; ++i
)
1805 if (!isl_seq_eq(mat1
->row
[i
], mat2
->row
[i
], mat1
->n_col
))
1806 return isl_bool_false
;
1808 return isl_bool_true
;
1811 __isl_give isl_mat
*isl_mat_from_row_vec(__isl_take isl_vec
*vec
)
1813 struct isl_mat
*mat
;
1817 mat
= isl_mat_alloc(vec
->ctx
, 1, vec
->size
);
1821 isl_seq_cpy(mat
->row
[0], vec
->el
, vec
->size
);
1830 /* Return a copy of row "row" of "mat" as an isl_vec.
1832 __isl_give isl_vec
*isl_mat_get_row(__isl_keep isl_mat
*mat
, unsigned row
)
1838 if (row
>= mat
->n_row
)
1839 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
1842 v
= isl_vec_alloc(isl_mat_get_ctx(mat
), mat
->n_col
);
1845 isl_seq_cpy(v
->el
, mat
->row
[row
], mat
->n_col
);
1850 __isl_give isl_mat
*isl_mat_vec_concat(__isl_take isl_mat
*top
,
1851 __isl_take isl_vec
*bot
)
1853 return isl_mat_concat(top
, isl_mat_from_row_vec(bot
));
1856 __isl_give isl_mat
*isl_mat_move_cols(__isl_take isl_mat
*mat
,
1857 unsigned dst_col
, unsigned src_col
, unsigned n
)
1863 if (n
== 0 || dst_col
== src_col
)
1866 res
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
1870 if (dst_col
< src_col
) {
1871 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1873 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1874 dst_col
, src_col
, n
);
1875 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1876 dst_col
+ n
, dst_col
, src_col
- dst_col
);
1877 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1878 src_col
+ n
, src_col
+ n
,
1879 res
->n_col
- src_col
- n
);
1881 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1883 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1884 src_col
, src_col
+ n
, dst_col
- src_col
);
1885 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1886 dst_col
, src_col
, n
);
1887 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1888 dst_col
+ n
, dst_col
+ n
,
1889 res
->n_col
- dst_col
- n
);
1899 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1900 * Return isl_stat_ok on success and isl_stat_error on failure.
1902 isl_stat
isl_mat_row_gcd(__isl_keep isl_mat
*mat
, int row
, isl_int
*gcd
)
1904 if (check_row(mat
, row
) < 0)
1905 return isl_stat_error
;
1907 isl_seq_gcd(mat
->row
[row
], mat
->n_col
, gcd
);
1912 void isl_mat_gcd(__isl_keep isl_mat
*mat
, isl_int
*gcd
)
1917 isl_int_set_si(*gcd
, 0);
1922 for (i
= 0; i
< mat
->n_row
; ++i
) {
1923 isl_seq_gcd(mat
->row
[i
], mat
->n_col
, &g
);
1924 isl_int_gcd(*gcd
, *gcd
, g
);
1929 /* Return the result of scaling "mat" by a factor of "m".
1931 __isl_give isl_mat
*isl_mat_scale(__isl_take isl_mat
*mat
, isl_int m
)
1935 if (isl_int_is_one(m
))
1938 mat
= isl_mat_cow(mat
);
1942 for (i
= 0; i
< mat
->n_row
; ++i
)
1943 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1948 __isl_give isl_mat
*isl_mat_scale_down(__isl_take isl_mat
*mat
, isl_int m
)
1952 if (isl_int_is_one(m
))
1955 mat
= isl_mat_cow(mat
);
1959 for (i
= 0; i
< mat
->n_row
; ++i
)
1960 isl_seq_scale_down(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1965 __isl_give isl_mat
*isl_mat_scale_down_row(__isl_take isl_mat
*mat
, int row
,
1968 if (isl_int_is_one(m
))
1971 mat
= isl_mat_cow(mat
);
1975 isl_seq_scale_down(mat
->row
[row
], mat
->row
[row
], m
, mat
->n_col
);
1980 __isl_give isl_mat
*isl_mat_normalize(__isl_take isl_mat
*mat
)
1988 isl_mat_gcd(mat
, &gcd
);
1989 mat
= isl_mat_scale_down(mat
, gcd
);
1995 __isl_give isl_mat
*isl_mat_normalize_row(__isl_take isl_mat
*mat
, int row
)
1997 mat
= isl_mat_cow(mat
);
2001 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], mat
->n_col
);
2006 /* Number of initial non-zero columns.
2008 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat
*mat
)
2015 for (i
= 0; i
< mat
->n_col
; ++i
)
2016 if (row_first_non_zero(mat
->row
, mat
->n_row
, i
) < 0)
2022 /* Return a basis for the space spanned by the rows of "mat".
2023 * Any basis will do, so simply perform Gaussian elimination and
2024 * remove the empty rows.
2026 __isl_give isl_mat
*isl_mat_row_basis(__isl_take isl_mat
*mat
)
2028 return isl_mat_reverse_gauss(mat
);
2031 /* Return rows that extend a basis of "mat1" to one
2032 * that covers both "mat1" and "mat2".
2033 * The Hermite normal form of the concatenation of the two matrices is
2036 * [ M1 ] = [ H1 0 0 ] [ Q2 ]
2037 * [ M2 ] = [ H2 H3 0 ] [ Q3 ]
2039 * The number of columns in H1 and H3 determine the number of rows
2040 * in Q1 and Q2. Q1 is a basis for M1, while Q2 extends this basis
2043 __isl_give isl_mat
*isl_mat_row_basis_extension(
2044 __isl_take isl_mat
*mat1
, __isl_take isl_mat
*mat2
)
2050 n1
= isl_mat_rows(mat1
);
2051 H
= isl_mat_concat(mat1
, mat2
);
2052 H
= isl_mat_left_hermite(H
, 0, NULL
, &Q
);
2056 r1
= hermite_first_zero_col(H
, 0, n1
);
2057 r
= hermite_first_zero_col(H
, r1
, H
->n_row
);
2058 n_row
= isl_mat_rows(Q
);
2059 Q
= isl_mat_drop_rows(Q
, r
, n_row
- r
);
2060 Q
= isl_mat_drop_rows(Q
, 0, r1
);
2070 /* Are the rows of "mat1" linearly independent of those of "mat2"?
2071 * That is, is there no linear dependence among the combined rows
2072 * that is not already present in either "mat1" or "mat2"?
2073 * In other words, is the rank of "mat1" and "mat2" combined equal
2074 * to the sum of the ranks of "mat1" and "mat2"?
2076 isl_bool
isl_mat_has_linearly_independent_rows(__isl_keep isl_mat
*mat1
,
2077 __isl_keep isl_mat
*mat2
)
2082 r1
= isl_mat_rank(mat1
);
2084 return isl_bool_error
;
2086 return isl_bool_true
;
2087 r2
= isl_mat_rank(mat2
);
2089 return isl_bool_error
;
2091 return isl_bool_true
;
2093 mat
= isl_mat_concat(isl_mat_copy(mat1
), isl_mat_copy(mat2
));
2094 r
= isl_mat_rank(mat
);
2097 return isl_bool_error
;
2098 return r
== r1
+ r2
;