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>
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
)
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
) {
43 row_hash
= isl_seq_get_hash(mat
->row
[i
], mat
->n_col
);
44 isl_hash_hash(hash
, row_hash
);
50 struct isl_mat
*isl_mat_alloc(struct isl_ctx
*ctx
,
51 unsigned n_row
, unsigned n_col
)
56 mat
= isl_alloc_type(ctx
, struct isl_mat
);
61 mat
->block
= isl_blk_alloc(ctx
, n_row
* n_col
);
62 if (isl_blk_is_error(mat
->block
))
64 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
65 if (n_row
&& !mat
->row
)
68 for (i
= 0; i
< n_row
; ++i
)
69 mat
->row
[i
] = mat
->block
.data
+ i
* n_col
;
81 isl_blk_free(ctx
, mat
->block
);
86 struct isl_mat
*isl_mat_extend(struct isl_mat
*mat
,
87 unsigned n_row
, unsigned n_col
)
96 if (mat
->max_col
>= n_col
&& mat
->n_row
>= n_row
) {
97 if (mat
->n_col
< n_col
)
102 if (mat
->max_col
< n_col
) {
103 struct isl_mat
*new_mat
;
105 if (n_row
< mat
->n_row
)
107 new_mat
= isl_mat_alloc(mat
->ctx
, n_row
, n_col
);
110 for (i
= 0; i
< mat
->n_row
; ++i
)
111 isl_seq_cpy(new_mat
->row
[i
], mat
->row
[i
], mat
->n_col
);
116 mat
= isl_mat_cow(mat
);
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
))
124 row
= isl_realloc_array(mat
->ctx
, mat
->row
, isl_int
*, n_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
;
134 if (mat
->n_col
< n_col
)
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
)
149 mat
= isl_alloc_type(ctx
, struct isl_mat
);
152 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
153 if (n_row
&& !mat
->row
)
155 for (i
= 0; i
< n_row
; ++i
)
156 mat
->row
[i
] = row
[first_row
+i
] + first_col
;
162 mat
->block
= isl_blk_empty();
163 mat
->flags
= ISL_MAT_BORROWED
;
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
)
175 return isl_mat_sub_alloc6(mat
->ctx
, mat
->row
, first_row
, n_row
,
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
)
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
)
193 for (i
= 0; i
< n_row
; ++i
)
194 isl_seq_neg(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
197 __isl_give isl_mat
*isl_mat_copy(__isl_keep isl_mat
*mat
)
206 struct isl_mat
*isl_mat_dup(struct isl_mat
*mat
)
209 struct isl_mat
*mat2
;
213 mat2
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
216 for (i
= 0; i
< mat
->n_row
; ++i
)
217 isl_seq_cpy(mat2
->row
[i
], mat
->row
[i
], mat
->n_col
);
221 struct isl_mat
*isl_mat_cow(struct isl_mat
*mat
)
223 struct isl_mat
*mat2
;
227 if (mat
->ref
== 1 && !ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
230 mat2
= isl_mat_dup(mat
);
235 __isl_null isl_mat
*isl_mat_free(__isl_take isl_mat
*mat
)
243 if (!ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
244 isl_blk_free(mat
->ctx
, mat
->block
);
245 isl_ctx_deref(mat
->ctx
);
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 /* Check that "col" is a valid column position for "mat".
264 static isl_stat
check_col(__isl_keep isl_mat
*mat
, int col
)
267 return isl_stat_error
;
268 if (col
< 0 || col
>= mat
->n_col
)
269 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
270 "column out of range", return isl_stat_error
);
274 /* Check that "row" is a valid row position for "mat".
276 static isl_stat
check_row(__isl_keep isl_mat
*mat
, int row
)
279 return isl_stat_error
;
280 if (row
< 0 || row
>= mat
->n_row
)
281 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
282 "row out of range", return isl_stat_error
);
286 int isl_mat_get_element(__isl_keep isl_mat
*mat
, int row
, int col
, isl_int
*v
)
288 if (check_row(mat
, row
) < 0)
290 if (check_col(mat
, col
) < 0)
292 isl_int_set(*v
, mat
->row
[row
][col
]);
296 /* Extract the element at row "row", oolumn "col" of "mat".
298 __isl_give isl_val
*isl_mat_get_element_val(__isl_keep isl_mat
*mat
,
303 if (check_row(mat
, row
) < 0)
305 if (check_col(mat
, col
) < 0)
307 ctx
= isl_mat_get_ctx(mat
);
308 return isl_val_int_from_isl_int(ctx
, mat
->row
[row
][col
]);
311 __isl_give isl_mat
*isl_mat_set_element(__isl_take isl_mat
*mat
,
312 int row
, int col
, isl_int v
)
314 mat
= isl_mat_cow(mat
);
315 if (check_row(mat
, row
) < 0)
316 return isl_mat_free(mat
);
317 if (check_col(mat
, col
) < 0)
318 return isl_mat_free(mat
);
319 isl_int_set(mat
->row
[row
][col
], v
);
323 __isl_give isl_mat
*isl_mat_set_element_si(__isl_take isl_mat
*mat
,
324 int row
, int col
, int v
)
326 mat
= isl_mat_cow(mat
);
327 if (check_row(mat
, row
) < 0)
328 return isl_mat_free(mat
);
329 if (check_col(mat
, col
) < 0)
330 return isl_mat_free(mat
);
331 isl_int_set_si(mat
->row
[row
][col
], v
);
335 /* Replace the element at row "row", column "col" of "mat" by "v".
337 __isl_give isl_mat
*isl_mat_set_element_val(__isl_take isl_mat
*mat
,
338 int row
, int col
, __isl_take isl_val
*v
)
341 return isl_mat_free(mat
);
342 if (!isl_val_is_int(v
))
343 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
344 "expecting integer value", goto error
);
345 mat
= isl_mat_set_element(mat
, row
, col
, v
->n
);
350 return isl_mat_free(mat
);
353 __isl_give isl_mat
*isl_mat_diag(isl_ctx
*ctx
, unsigned n_row
, isl_int d
)
358 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
361 for (i
= 0; i
< n_row
; ++i
) {
362 isl_seq_clr(mat
->row
[i
], i
);
363 isl_int_set(mat
->row
[i
][i
], d
);
364 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
370 /* Create an "n_row" by "n_col" matrix with zero elements.
372 __isl_give isl_mat
*isl_mat_zero(isl_ctx
*ctx
, unsigned n_row
, unsigned n_col
)
377 mat
= isl_mat_alloc(ctx
, n_row
, n_col
);
380 for (i
= 0; i
< n_row
; ++i
)
381 isl_seq_clr(mat
->row
[i
], n_col
);
386 __isl_give isl_mat
*isl_mat_identity(isl_ctx
*ctx
, unsigned n_row
)
390 return isl_mat_diag(ctx
, n_row
, ctx
->one
);
393 /* Is "mat" a (possibly scaled) identity matrix?
395 int isl_mat_is_scaled_identity(__isl_keep isl_mat
*mat
)
401 if (mat
->n_row
!= mat
->n_col
)
404 for (i
= 0; i
< mat
->n_row
; ++i
) {
405 if (isl_seq_first_non_zero(mat
->row
[i
], i
) != -1)
407 if (isl_int_ne(mat
->row
[0][0], mat
->row
[i
][i
]))
409 if (isl_seq_first_non_zero(mat
->row
[i
] + i
+ 1,
410 mat
->n_col
- (i
+ 1)) != -1)
417 __isl_give isl_vec
*isl_mat_vec_product(__isl_take isl_mat
*mat
,
418 __isl_take isl_vec
*vec
)
421 struct isl_vec
*prod
;
426 isl_assert(mat
->ctx
, mat
->n_col
== vec
->size
, goto error
);
428 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
432 for (i
= 0; i
< prod
->size
; ++i
)
433 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
434 &prod
->block
.data
[i
]);
444 __isl_give isl_vec
*isl_mat_vec_inverse_product(__isl_take isl_mat
*mat
,
445 __isl_take isl_vec
*vec
)
447 struct isl_mat
*vec_mat
;
452 vec_mat
= isl_mat_alloc(vec
->ctx
, vec
->size
, 1);
455 for (i
= 0; i
< vec
->size
; ++i
)
456 isl_int_set(vec_mat
->row
[i
][0], vec
->el
[i
]);
457 vec_mat
= isl_mat_inverse_product(mat
, vec_mat
);
461 vec
= isl_vec_alloc(vec_mat
->ctx
, vec_mat
->n_row
);
463 for (i
= 0; i
< vec
->size
; ++i
)
464 isl_int_set(vec
->el
[i
], vec_mat
->row
[i
][0]);
465 isl_mat_free(vec_mat
);
473 __isl_give isl_vec
*isl_vec_mat_product(__isl_take isl_vec
*vec
,
474 __isl_take isl_mat
*mat
)
477 struct isl_vec
*prod
;
482 isl_assert(mat
->ctx
, mat
->n_row
== vec
->size
, goto error
);
484 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_col
);
488 for (i
= 0; i
< prod
->size
; ++i
) {
489 isl_int_set_si(prod
->el
[i
], 0);
490 for (j
= 0; j
< vec
->size
; ++j
)
491 isl_int_addmul(prod
->el
[i
], vec
->el
[j
], mat
->row
[j
][i
]);
502 __isl_give isl_mat
*isl_mat_aff_direct_sum(__isl_take isl_mat
*left
,
503 __isl_take isl_mat
*right
)
511 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
512 isl_assert(left
->ctx
, left
->n_row
>= 1, goto error
);
513 isl_assert(left
->ctx
, left
->n_col
>= 1, goto error
);
514 isl_assert(left
->ctx
, right
->n_col
>= 1, goto error
);
515 isl_assert(left
->ctx
,
516 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
518 isl_assert(left
->ctx
,
519 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
522 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
525 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
526 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
527 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
529 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
530 for (i
= 1; i
< sum
->n_row
; ++i
) {
531 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
532 isl_int_addmul(sum
->row
[i
][0],
533 right
->row
[0][0], right
->row
[i
][0]);
534 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
536 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
537 right
->row
[i
]+1, right
->row
[0][0],
541 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
542 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
552 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
553 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
556 for (r
= row
; r
< M
->n_row
; ++r
)
557 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
559 for (r
= 0; r
< (*U
)->n_row
; ++r
)
560 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
563 isl_mat_swap_rows(*Q
, i
, j
);
566 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
567 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
570 for (r
= row
; r
< M
->n_row
; ++r
)
571 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
573 for (r
= 0; r
< (*U
)->n_row
; ++r
)
574 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
577 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
578 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
582 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
583 struct isl_mat
**Q
, unsigned row
, unsigned col
)
586 for (r
= row
; r
< M
->n_row
; ++r
)
587 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
589 for (r
= 0; r
< (*U
)->n_row
; ++r
)
590 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
593 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
596 /* Given matrix M, compute
601 * with U and Q unimodular matrices and H a matrix in column echelon form
602 * such that on each echelon row the entries in the non-echelon column
603 * are non-negative (if neg == 0) or non-positive (if neg == 1)
604 * and strictly smaller (in absolute value) than the entries in the echelon
606 * If U or Q are NULL, then these matrices are not computed.
608 __isl_give isl_mat
*isl_mat_left_hermite(__isl_take isl_mat
*M
, int neg
,
609 __isl_give isl_mat
**U
, __isl_give isl_mat
**Q
)
624 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
629 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
636 for (row
= 0; row
< M
->n_row
; ++row
) {
638 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
643 exchange(M
, U
, Q
, row
, first
, col
);
644 if (isl_int_is_neg(M
->row
[row
][col
]))
645 oppose(M
, U
, Q
, row
, col
);
647 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
648 M
->n_col
-first
)) != -1) {
650 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
651 subtract(M
, U
, Q
, row
, col
, first
, c
);
652 if (!isl_int_is_zero(M
->row
[row
][first
]))
653 exchange(M
, U
, Q
, row
, first
, col
);
657 for (i
= 0; i
< col
; ++i
) {
658 if (isl_int_is_zero(M
->row
[row
][i
]))
661 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
663 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
664 if (isl_int_is_zero(c
))
666 subtract(M
, U
, Q
, row
, col
, i
, c
);
686 /* Use row "row" of "mat" to eliminate column "col" from all other rows.
688 static __isl_give isl_mat
*eliminate(__isl_take isl_mat
*mat
, int row
, int col
)
696 ctx
= isl_mat_get_ctx(mat
);
697 nr
= isl_mat_rows(mat
);
698 nc
= isl_mat_cols(mat
);
700 for (k
= 0; k
< nr
; ++k
) {
703 if (isl_int_is_zero(mat
->row
[k
][col
]))
705 mat
= isl_mat_cow(mat
);
708 isl_seq_elim(mat
->row
[k
], mat
->row
[row
], col
, nc
, NULL
);
709 isl_seq_normalize(ctx
, mat
->row
[k
], nc
);
715 /* Perform Gaussian elimination on the rows of "mat", but start
716 * from the final row and the final column.
717 * Any zero rows that result from the elimination are removed.
719 * In particular, for each column from last to first,
720 * look for the last row with a non-zero coefficient in that column,
721 * move it last (but before other rows moved last in previous steps) and
722 * use it to eliminate the column from the other rows.
724 __isl_give isl_mat
*isl_mat_reverse_gauss(__isl_take isl_mat
*mat
)
726 int k
, row
, last
, nr
, nc
;
731 nr
= isl_mat_rows(mat
);
732 nc
= isl_mat_cols(mat
);
735 for (row
= nr
- 1; row
>= 0; --row
) {
736 for (; last
>= 0; --last
) {
737 for (k
= row
; k
>= 0; --k
)
738 if (!isl_int_is_zero(mat
->row
[k
][last
]))
746 mat
= isl_mat_swap_rows(mat
, k
, row
);
749 if (isl_int_is_neg(mat
->row
[row
][last
]))
750 mat
= isl_mat_row_neg(mat
, row
);
751 mat
= eliminate(mat
, row
, last
);
755 mat
= isl_mat_drop_rows(mat
, 0, row
+ 1);
760 /* Negate the lexicographically negative rows of "mat" such that
761 * all rows in the result are lexicographically non-negative.
763 __isl_give isl_mat
*isl_mat_lexnonneg_rows(__isl_take isl_mat
*mat
)
770 nr
= isl_mat_rows(mat
);
771 nc
= isl_mat_cols(mat
);
773 for (i
= 0; i
< nr
; ++i
) {
776 pos
= isl_seq_first_non_zero(mat
->row
[i
], nc
);
779 if (isl_int_is_nonneg(mat
->row
[i
][pos
]))
781 mat
= isl_mat_row_neg(mat
, i
);
789 struct isl_mat
*isl_mat_right_kernel(struct isl_mat
*mat
)
792 struct isl_mat
*U
= NULL
;
795 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
799 for (i
= 0, rank
= 0; rank
< mat
->n_col
; ++rank
) {
800 while (i
< mat
->n_row
&& isl_int_is_zero(mat
->row
[i
][rank
]))
805 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
808 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
818 __isl_give isl_mat
*isl_mat_lin_to_aff(__isl_take isl_mat
*mat
)
821 struct isl_mat
*mat2
;
825 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
828 isl_int_set_si(mat2
->row
[0][0], 1);
829 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
830 for (i
= 0; i
< mat
->n_row
; ++i
) {
831 isl_int_set_si(mat2
->row
[1+i
][0], 0);
832 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
841 /* Given two matrices M1 and M2, return the block matrix
846 __isl_give isl_mat
*isl_mat_diagonal(__isl_take isl_mat
*mat1
,
847 __isl_take isl_mat
*mat2
)
855 mat
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
856 mat1
->n_col
+ mat2
->n_col
);
859 for (i
= 0; i
< mat1
->n_row
; ++i
) {
860 isl_seq_cpy(mat
->row
[i
], mat1
->row
[i
], mat1
->n_col
);
861 isl_seq_clr(mat
->row
[i
] + mat1
->n_col
, mat2
->n_col
);
863 for (i
= 0; i
< mat2
->n_row
; ++i
) {
864 isl_seq_clr(mat
->row
[mat1
->n_row
+ i
], mat1
->n_col
);
865 isl_seq_cpy(mat
->row
[mat1
->n_row
+ i
] + mat1
->n_col
,
866 mat2
->row
[i
], mat2
->n_col
);
877 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
881 for (i
= 0; i
< n_row
; ++i
)
882 if (!isl_int_is_zero(row
[i
][col
]))
887 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
889 int i
, min
= row_first_non_zero(row
, n_row
, col
);
892 for (i
= min
+ 1; i
< n_row
; ++i
) {
893 if (isl_int_is_zero(row
[i
][col
]))
895 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
901 static isl_stat
inv_exchange(__isl_keep isl_mat
**left
,
902 __isl_keep isl_mat
**right
, unsigned i
, unsigned j
)
904 *left
= isl_mat_swap_rows(*left
, i
, j
);
905 *right
= isl_mat_swap_rows(*right
, i
, j
);
907 if (!*left
|| !*right
)
908 return isl_stat_error
;
912 static void inv_oppose(
913 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
915 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
916 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
919 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
920 unsigned row
, unsigned i
, isl_int m
)
923 isl_seq_combine(left
->row
[i
]+row
,
924 left
->ctx
->one
, left
->row
[i
]+row
,
925 m
, left
->row
[row
]+row
,
927 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
928 m
, right
->row
[row
], right
->n_col
);
931 /* Compute inv(left)*right
933 __isl_give isl_mat
*isl_mat_inverse_product(__isl_take isl_mat
*left
,
934 __isl_take isl_mat
*right
)
942 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
943 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
945 if (left
->n_row
== 0) {
950 left
= isl_mat_cow(left
);
951 right
= isl_mat_cow(right
);
957 for (row
= 0; row
< left
->n_row
; ++row
) {
958 int pivot
, first
, i
, off
;
959 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
963 isl_assert(left
->ctx
, pivot
>= 0, goto error
);
967 if (inv_exchange(&left
, &right
, pivot
, row
) < 0)
969 if (isl_int_is_neg(left
->row
[row
][row
]))
970 inv_oppose(left
, right
, row
);
972 while ((off
= row_first_non_zero(left
->row
+first
,
973 left
->n_row
-first
, row
)) != -1) {
975 isl_int_fdiv_q(a
, left
->row
[first
][row
],
976 left
->row
[row
][row
]);
977 inv_subtract(left
, right
, row
, first
, a
);
978 if (!isl_int_is_zero(left
->row
[first
][row
])) {
979 if (inv_exchange(&left
, &right
, row
, first
) < 0)
985 for (i
= 0; i
< row
; ++i
) {
986 if (isl_int_is_zero(left
->row
[i
][row
]))
988 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
989 isl_int_divexact(b
, left
->row
[i
][row
], a
);
990 isl_int_divexact(a
, left
->row
[row
][row
], a
);
992 isl_seq_combine(left
->row
[i
] + i
,
994 b
, left
->row
[row
] + i
,
996 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
997 b
, right
->row
[row
], right
->n_col
);
1002 isl_int_set(a
, left
->row
[0][0]);
1003 for (row
= 1; row
< left
->n_row
; ++row
)
1004 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
1005 if (isl_int_is_zero(a
)){
1007 isl_assert(left
->ctx
, 0, goto error
);
1009 for (row
= 0; row
< left
->n_row
; ++row
) {
1010 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
1011 if (isl_int_is_one(left
->row
[row
][row
]))
1013 isl_seq_scale(right
->row
[row
], right
->row
[row
],
1014 left
->row
[row
][row
], right
->n_col
);
1022 isl_mat_free(right
);
1026 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
1030 for (i
= 0; i
< mat
->n_row
; ++i
)
1031 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
1034 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
1035 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
1041 for (i
= 0; i
< mat
->n_row
; ++i
) {
1042 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
1043 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
1044 isl_int_set(mat
->row
[i
][dst
], tmp
);
1049 __isl_give isl_mat
*isl_mat_right_inverse(__isl_take isl_mat
*mat
)
1051 struct isl_mat
*inv
;
1055 mat
= isl_mat_cow(mat
);
1059 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
1060 inv
= isl_mat_cow(inv
);
1066 for (row
= 0; row
< mat
->n_row
; ++row
) {
1067 int pivot
, first
, i
, off
;
1068 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
1072 isl_assert(mat
->ctx
, pivot
>= 0, goto error
);
1076 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
1077 if (isl_int_is_neg(mat
->row
[row
][row
]))
1078 oppose(mat
, &inv
, NULL
, row
, row
);
1080 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
1081 mat
->n_col
-first
)) != -1) {
1083 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
1084 mat
->row
[row
][row
]);
1085 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
1086 if (!isl_int_is_zero(mat
->row
[row
][first
]))
1087 exchange(mat
, &inv
, NULL
, row
, row
, first
);
1091 for (i
= 0; i
< row
; ++i
) {
1092 if (isl_int_is_zero(mat
->row
[row
][i
]))
1094 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
1095 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
1096 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
1098 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
1099 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
1104 isl_int_set(a
, mat
->row
[0][0]);
1105 for (row
= 1; row
< mat
->n_row
; ++row
)
1106 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
1107 if (isl_int_is_zero(a
)){
1111 for (row
= 0; row
< mat
->n_row
; ++row
) {
1112 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
1113 if (isl_int_is_one(mat
->row
[row
][row
]))
1115 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
1128 __isl_give isl_mat
*isl_mat_transpose(__isl_take isl_mat
*mat
)
1130 struct isl_mat
*transpose
= NULL
;
1136 if (mat
->n_col
== mat
->n_row
) {
1137 mat
= isl_mat_cow(mat
);
1140 for (i
= 0; i
< mat
->n_row
; ++i
)
1141 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
1142 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
1145 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
1148 for (i
= 0; i
< mat
->n_row
; ++i
)
1149 for (j
= 0; j
< mat
->n_col
; ++j
)
1150 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
1158 __isl_give isl_mat
*isl_mat_swap_cols(__isl_take isl_mat
*mat
,
1159 unsigned i
, unsigned j
)
1163 mat
= isl_mat_cow(mat
);
1166 isl_assert(mat
->ctx
, i
< mat
->n_col
, goto error
);
1167 isl_assert(mat
->ctx
, j
< mat
->n_col
, goto error
);
1169 for (r
= 0; r
< mat
->n_row
; ++r
)
1170 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
1177 __isl_give isl_mat
*isl_mat_swap_rows(__isl_take isl_mat
*mat
,
1178 unsigned i
, unsigned j
)
1184 mat
= isl_mat_cow(mat
);
1188 mat
->row
[i
] = mat
->row
[j
];
1193 /* Calculate the product of two matrices.
1195 * This function is optimized for operand matrices that contain many zeros and
1196 * skips multiplications where we know one of the operands is zero.
1198 __isl_give isl_mat
*isl_mat_product(__isl_take isl_mat
*left
,
1199 __isl_take isl_mat
*right
)
1202 struct isl_mat
*prod
;
1204 if (!left
|| !right
)
1206 isl_assert(left
->ctx
, left
->n_col
== right
->n_row
, goto error
);
1207 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
1210 if (left
->n_col
== 0) {
1211 for (i
= 0; i
< prod
->n_row
; ++i
)
1212 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
1214 isl_mat_free(right
);
1217 for (i
= 0; i
< prod
->n_row
; ++i
) {
1218 for (j
= 0; j
< prod
->n_col
; ++j
)
1219 isl_int_mul(prod
->row
[i
][j
],
1220 left
->row
[i
][0], right
->row
[0][j
]);
1221 for (k
= 1; k
< left
->n_col
; ++k
) {
1222 if (isl_int_is_zero(left
->row
[i
][k
]))
1224 for (j
= 0; j
< prod
->n_col
; ++j
)
1225 isl_int_addmul(prod
->row
[i
][j
],
1226 left
->row
[i
][k
], right
->row
[k
][j
]);
1230 isl_mat_free(right
);
1234 isl_mat_free(right
);
1238 /* Replace the variables x in the rows q by x' given by x = M x',
1239 * with M the matrix mat.
1241 * If the number of new variables is greater than the original
1242 * number of variables, then the rows q have already been
1243 * preextended. If the new number is smaller, then the coefficients
1244 * of the divs, which are not changed, need to be shifted down.
1245 * The row q may be the equalities, the inequalities or the
1246 * div expressions. In the latter case, has_div is true and
1247 * we need to take into account the extra denominator column.
1249 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1250 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
1256 if (mat
->n_col
>= mat
->n_row
)
1259 e
= mat
->n_row
- mat
->n_col
;
1261 for (i
= 0; i
< n
; ++i
)
1262 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
1263 t
= isl_mat_sub_alloc6(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
1264 t
= isl_mat_product(t
, mat
);
1267 for (i
= 0; i
< n
; ++i
) {
1268 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
1269 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
1270 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
1271 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
1277 /* Replace the variables x in bset by x' given by x = M x', with
1280 * If there are fewer variables x' then there are x, then we perform
1281 * the transformation in place, which means that, in principle,
1282 * this frees up some extra variables as the number
1283 * of columns remains constant, but we would have to extend
1284 * the div array too as the number of rows in this array is assumed
1285 * to be equal to extra.
1287 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
1288 struct isl_mat
*mat
)
1290 struct isl_ctx
*ctx
;
1296 bset
= isl_basic_set_cow(bset
);
1300 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
1301 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
1302 isl_assert(ctx
, mat
->n_col
> 0, goto error
);
1304 if (mat
->n_col
> mat
->n_row
) {
1305 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0, 0, 0);
1308 } else if (mat
->n_col
< mat
->n_row
) {
1309 bset
->dim
= isl_space_cow(bset
->dim
);
1312 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
1315 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
1316 isl_mat_copy(mat
)) < 0)
1319 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
1320 isl_mat_copy(mat
)) < 0)
1323 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
1326 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1327 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1328 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1329 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
1330 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
1332 bset
= isl_basic_set_simplify(bset
);
1333 bset
= isl_basic_set_finalize(bset
);
1339 isl_basic_set_free(bset
);
1343 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
1347 set
= isl_set_cow(set
);
1351 for (i
= 0; i
< set
->n
; ++i
) {
1352 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
1357 if (mat
->n_col
!= mat
->n_row
) {
1358 set
->dim
= isl_space_cow(set
->dim
);
1361 set
->dim
->n_out
+= mat
->n_col
;
1362 set
->dim
->n_out
-= mat
->n_row
;
1365 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1373 /* Replace the variables x starting at "first_col" in the rows "rows"
1374 * of some coefficient matrix by x' with x = M x' with M the matrix mat.
1375 * That is, replace the corresponding coefficients c by c M.
1377 isl_stat
isl_mat_sub_transform(isl_int
**row
, unsigned n_row
,
1378 unsigned first_col
, __isl_take isl_mat
*mat
)
1385 return isl_stat_error
;
1386 ctx
= isl_mat_get_ctx(mat
);
1387 t
= isl_mat_sub_alloc6(ctx
, row
, 0, n_row
, first_col
, mat
->n_row
);
1388 t
= isl_mat_product(t
, mat
);
1390 return isl_stat_error
;
1391 for (i
= 0; i
< n_row
; ++i
)
1392 isl_seq_swp_or_cpy(row
[i
] + first_col
, t
->row
[i
], t
->n_col
);
1397 void isl_mat_print_internal(__isl_keep isl_mat
*mat
, FILE *out
, int indent
)
1402 fprintf(out
, "%*snull mat\n", indent
, "");
1406 if (mat
->n_row
== 0)
1407 fprintf(out
, "%*s[]\n", indent
, "");
1409 for (i
= 0; i
< mat
->n_row
; ++i
) {
1411 fprintf(out
, "%*s[[", indent
, "");
1413 fprintf(out
, "%*s[", indent
+1, "");
1414 for (j
= 0; j
< mat
->n_col
; ++j
) {
1417 isl_int_print(out
, mat
->row
[i
][j
], 0);
1419 if (i
== mat
->n_row
-1)
1420 fprintf(out
, "]]\n");
1422 fprintf(out
, "]\n");
1426 void isl_mat_dump(__isl_keep isl_mat
*mat
)
1428 isl_mat_print_internal(mat
, stderr
, 0);
1431 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
1438 mat
= isl_mat_cow(mat
);
1442 if (col
!= mat
->n_col
-n
) {
1443 for (r
= 0; r
< mat
->n_row
; ++r
)
1444 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
1445 mat
->n_col
- col
- n
);
1451 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
1455 mat
= isl_mat_cow(mat
);
1459 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
1460 mat
->row
[r
] = mat
->row
[r
+n
];
1466 __isl_give isl_mat
*isl_mat_insert_cols(__isl_take isl_mat
*mat
,
1467 unsigned col
, unsigned n
)
1476 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
+ n
);
1480 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
, 0, 0, col
);
1481 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
,
1482 col
+ n
, col
, mat
->n_col
- col
);
1491 __isl_give isl_mat
*isl_mat_insert_zero_cols(__isl_take isl_mat
*mat
,
1492 unsigned first
, unsigned n
)
1498 mat
= isl_mat_insert_cols(mat
, first
, n
);
1502 for (i
= 0; i
< mat
->n_row
; ++i
)
1503 isl_seq_clr(mat
->row
[i
] + first
, n
);
1508 __isl_give isl_mat
*isl_mat_add_zero_cols(__isl_take isl_mat
*mat
, unsigned n
)
1513 return isl_mat_insert_zero_cols(mat
, mat
->n_col
, n
);
1516 __isl_give isl_mat
*isl_mat_insert_rows(__isl_take isl_mat
*mat
,
1517 unsigned row
, unsigned n
)
1526 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
+ n
, mat
->n_col
);
1530 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, row
, 0, 0, mat
->n_col
);
1531 isl_mat_sub_copy(mat
->ctx
, ext
->row
+ row
+ n
, mat
->row
+ row
,
1532 mat
->n_row
- row
, 0, 0, mat
->n_col
);
1541 __isl_give isl_mat
*isl_mat_add_rows(__isl_take isl_mat
*mat
, unsigned n
)
1546 return isl_mat_insert_rows(mat
, mat
->n_row
, n
);
1549 __isl_give isl_mat
*isl_mat_insert_zero_rows(__isl_take isl_mat
*mat
,
1550 unsigned row
, unsigned n
)
1554 mat
= isl_mat_insert_rows(mat
, row
, n
);
1558 for (i
= 0; i
< n
; ++i
)
1559 isl_seq_clr(mat
->row
[row
+ i
], mat
->n_col
);
1564 __isl_give isl_mat
*isl_mat_add_zero_rows(__isl_take isl_mat
*mat
, unsigned n
)
1569 return isl_mat_insert_zero_rows(mat
, mat
->n_row
, n
);
1572 void isl_mat_col_submul(struct isl_mat
*mat
,
1573 int dst_col
, isl_int f
, int src_col
)
1577 for (i
= 0; i
< mat
->n_row
; ++i
)
1578 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1581 void isl_mat_col_add(__isl_keep isl_mat
*mat
, int dst_col
, int src_col
)
1588 for (i
= 0; i
< mat
->n_row
; ++i
)
1589 isl_int_add(mat
->row
[i
][dst_col
],
1590 mat
->row
[i
][dst_col
], mat
->row
[i
][src_col
]);
1593 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1597 for (i
= 0; i
< mat
->n_row
; ++i
)
1598 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1601 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1602 * return the result.
1604 __isl_give isl_mat
*isl_mat_col_addmul(__isl_take isl_mat
*mat
, int dst_col
,
1605 isl_int f
, int src_col
)
1609 if (check_col(mat
, dst_col
) < 0 || check_col(mat
, src_col
) < 0)
1610 return isl_mat_free(mat
);
1612 for (i
= 0; i
< mat
->n_row
; ++i
) {
1613 if (isl_int_is_zero(mat
->row
[i
][src_col
]))
1615 mat
= isl_mat_cow(mat
);
1618 isl_int_addmul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1624 /* Negate column "col" of "mat" and return the result.
1626 __isl_give isl_mat
*isl_mat_col_neg(__isl_take isl_mat
*mat
, int col
)
1630 if (check_col(mat
, col
) < 0)
1631 return isl_mat_free(mat
);
1633 for (i
= 0; i
< mat
->n_row
; ++i
) {
1634 if (isl_int_is_zero(mat
->row
[i
][col
]))
1636 mat
= isl_mat_cow(mat
);
1639 isl_int_neg(mat
->row
[i
][col
], mat
->row
[i
][col
]);
1645 /* Negate row "row" of "mat" and return the result.
1647 __isl_give isl_mat
*isl_mat_row_neg(__isl_take isl_mat
*mat
, int row
)
1649 if (check_row(mat
, row
) < 0)
1650 return isl_mat_free(mat
);
1651 if (isl_seq_first_non_zero(mat
->row
[row
], mat
->n_col
) == -1)
1653 mat
= isl_mat_cow(mat
);
1656 isl_seq_neg(mat
->row
[row
], mat
->row
[row
], mat
->n_col
);
1660 __isl_give isl_mat
*isl_mat_unimodular_complete(__isl_take isl_mat
*M
, int row
)
1663 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1668 isl_assert(M
->ctx
, M
->n_row
== M
->n_col
, goto error
);
1670 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1671 M
->n_row
= M
->n_col
;
1674 for (r
= 0; r
< row
; ++r
)
1675 isl_assert(M
->ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1676 for (r
= row
; r
< M
->n_row
; ++r
)
1677 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);
1688 __isl_give isl_mat
*isl_mat_concat(__isl_take isl_mat
*top
,
1689 __isl_take isl_mat
*bot
)
1691 struct isl_mat
*mat
;
1696 isl_assert(top
->ctx
, top
->n_col
== bot
->n_col
, goto error
);
1697 if (top
->n_row
== 0) {
1701 if (bot
->n_row
== 0) {
1706 mat
= isl_mat_alloc(top
->ctx
, top
->n_row
+ bot
->n_row
, top
->n_col
);
1709 isl_mat_sub_copy(mat
->ctx
, mat
->row
, top
->row
, top
->n_row
,
1711 isl_mat_sub_copy(mat
->ctx
, mat
->row
+ top
->n_row
, bot
->row
, bot
->n_row
,
1722 isl_bool
isl_mat_is_equal(__isl_keep isl_mat
*mat1
, __isl_keep isl_mat
*mat2
)
1727 return isl_bool_error
;
1729 if (mat1
->n_row
!= mat2
->n_row
)
1730 return isl_bool_false
;
1732 if (mat1
->n_col
!= mat2
->n_col
)
1733 return isl_bool_false
;
1735 for (i
= 0; i
< mat1
->n_row
; ++i
)
1736 if (!isl_seq_eq(mat1
->row
[i
], mat2
->row
[i
], mat1
->n_col
))
1737 return isl_bool_false
;
1739 return isl_bool_true
;
1742 __isl_give isl_mat
*isl_mat_from_row_vec(__isl_take isl_vec
*vec
)
1744 struct isl_mat
*mat
;
1748 mat
= isl_mat_alloc(vec
->ctx
, 1, vec
->size
);
1752 isl_seq_cpy(mat
->row
[0], vec
->el
, vec
->size
);
1761 /* Return a copy of row "row" of "mat" as an isl_vec.
1763 __isl_give isl_vec
*isl_mat_get_row(__isl_keep isl_mat
*mat
, unsigned row
)
1769 if (row
>= mat
->n_row
)
1770 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
1773 v
= isl_vec_alloc(isl_mat_get_ctx(mat
), mat
->n_col
);
1776 isl_seq_cpy(v
->el
, mat
->row
[row
], mat
->n_col
);
1781 __isl_give isl_mat
*isl_mat_vec_concat(__isl_take isl_mat
*top
,
1782 __isl_take isl_vec
*bot
)
1784 return isl_mat_concat(top
, isl_mat_from_row_vec(bot
));
1787 __isl_give isl_mat
*isl_mat_move_cols(__isl_take isl_mat
*mat
,
1788 unsigned dst_col
, unsigned src_col
, unsigned n
)
1794 if (n
== 0 || dst_col
== src_col
)
1797 res
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
1801 if (dst_col
< src_col
) {
1802 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1804 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1805 dst_col
, src_col
, n
);
1806 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1807 dst_col
+ n
, dst_col
, src_col
- dst_col
);
1808 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1809 src_col
+ n
, src_col
+ n
,
1810 res
->n_col
- src_col
- n
);
1812 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1814 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1815 src_col
, src_col
+ n
, dst_col
- src_col
);
1816 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1817 dst_col
, src_col
, n
);
1818 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1819 dst_col
+ n
, dst_col
+ n
,
1820 res
->n_col
- dst_col
- n
);
1830 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1831 * Return isl_stat_ok on success and isl_stat_error on failure.
1833 isl_stat
isl_mat_row_gcd(__isl_keep isl_mat
*mat
, int row
, isl_int
*gcd
)
1835 if (check_row(mat
, row
) < 0)
1836 return isl_stat_error
;
1838 isl_seq_gcd(mat
->row
[row
], mat
->n_col
, gcd
);
1843 void isl_mat_gcd(__isl_keep isl_mat
*mat
, isl_int
*gcd
)
1848 isl_int_set_si(*gcd
, 0);
1853 for (i
= 0; i
< mat
->n_row
; ++i
) {
1854 isl_seq_gcd(mat
->row
[i
], mat
->n_col
, &g
);
1855 isl_int_gcd(*gcd
, *gcd
, g
);
1860 /* Return the result of scaling "mat" by a factor of "m".
1862 __isl_give isl_mat
*isl_mat_scale(__isl_take isl_mat
*mat
, isl_int m
)
1866 if (isl_int_is_one(m
))
1869 mat
= isl_mat_cow(mat
);
1873 for (i
= 0; i
< mat
->n_row
; ++i
)
1874 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1879 __isl_give isl_mat
*isl_mat_scale_down(__isl_take isl_mat
*mat
, isl_int m
)
1883 if (isl_int_is_one(m
))
1886 mat
= isl_mat_cow(mat
);
1890 for (i
= 0; i
< mat
->n_row
; ++i
)
1891 isl_seq_scale_down(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1896 __isl_give isl_mat
*isl_mat_scale_down_row(__isl_take isl_mat
*mat
, int row
,
1899 if (isl_int_is_one(m
))
1902 mat
= isl_mat_cow(mat
);
1906 isl_seq_scale_down(mat
->row
[row
], mat
->row
[row
], m
, mat
->n_col
);
1911 __isl_give isl_mat
*isl_mat_normalize(__isl_take isl_mat
*mat
)
1919 isl_mat_gcd(mat
, &gcd
);
1920 mat
= isl_mat_scale_down(mat
, gcd
);
1926 __isl_give isl_mat
*isl_mat_normalize_row(__isl_take isl_mat
*mat
, int row
)
1928 mat
= isl_mat_cow(mat
);
1932 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], mat
->n_col
);
1937 /* Number of initial non-zero columns.
1939 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat
*mat
)
1946 for (i
= 0; i
< mat
->n_col
; ++i
)
1947 if (row_first_non_zero(mat
->row
, mat
->n_row
, i
) < 0)