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 struct isl_mat
*isl_mat_copy(struct 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 int isl_mat_get_element(__isl_keep isl_mat
*mat
, int row
, int col
, isl_int
*v
)
278 if (row
< 0 || row
>= mat
->n_row
)
279 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
281 if (check_col(mat
, col
) < 0)
283 isl_int_set(*v
, mat
->row
[row
][col
]);
287 /* Extract the element at row "row", oolumn "col" of "mat".
289 __isl_give isl_val
*isl_mat_get_element_val(__isl_keep isl_mat
*mat
,
296 ctx
= isl_mat_get_ctx(mat
);
297 if (row
< 0 || row
>= mat
->n_row
)
298 isl_die(ctx
, isl_error_invalid
, "row out of range",
300 if (check_col(mat
, col
) < 0)
302 return isl_val_int_from_isl_int(ctx
, mat
->row
[row
][col
]);
305 __isl_give isl_mat
*isl_mat_set_element(__isl_take isl_mat
*mat
,
306 int row
, int col
, isl_int v
)
308 mat
= isl_mat_cow(mat
);
311 if (row
< 0 || row
>= mat
->n_row
)
312 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
314 if (check_col(mat
, col
) < 0)
315 return isl_mat_free(mat
);
316 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
);
329 if (row
< 0 || row
>= mat
->n_row
)
330 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
332 if (check_col(mat
, col
) < 0)
333 return isl_mat_free(mat
);
334 isl_int_set_si(mat
->row
[row
][col
], v
);
341 /* Replace the element at row "row", column "col" of "mat" by "v".
343 __isl_give isl_mat
*isl_mat_set_element_val(__isl_take isl_mat
*mat
,
344 int row
, int col
, __isl_take isl_val
*v
)
347 return isl_mat_free(mat
);
348 if (!isl_val_is_int(v
))
349 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
350 "expecting integer value", goto error
);
351 mat
= isl_mat_set_element(mat
, row
, col
, v
->n
);
356 return isl_mat_free(mat
);
359 __isl_give isl_mat
*isl_mat_diag(isl_ctx
*ctx
, unsigned n_row
, isl_int d
)
364 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
367 for (i
= 0; i
< n_row
; ++i
) {
368 isl_seq_clr(mat
->row
[i
], i
);
369 isl_int_set(mat
->row
[i
][i
], d
);
370 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
376 /* Create an "n_row" by "n_col" matrix with zero elements.
378 __isl_give isl_mat
*isl_mat_zero(isl_ctx
*ctx
, unsigned n_row
, unsigned n_col
)
383 mat
= isl_mat_alloc(ctx
, n_row
, n_col
);
386 for (i
= 0; i
< n_row
; ++i
)
387 isl_seq_clr(mat
->row
[i
], n_col
);
392 __isl_give isl_mat
*isl_mat_identity(isl_ctx
*ctx
, unsigned n_row
)
396 return isl_mat_diag(ctx
, n_row
, ctx
->one
);
399 /* Is "mat" a (possibly scaled) identity matrix?
401 int isl_mat_is_scaled_identity(__isl_keep isl_mat
*mat
)
407 if (mat
->n_row
!= mat
->n_col
)
410 for (i
= 0; i
< mat
->n_row
; ++i
) {
411 if (isl_seq_first_non_zero(mat
->row
[i
], i
) != -1)
413 if (isl_int_ne(mat
->row
[0][0], mat
->row
[i
][i
]))
415 if (isl_seq_first_non_zero(mat
->row
[i
] + i
+ 1,
416 mat
->n_col
- (i
+ 1)) != -1)
423 struct isl_vec
*isl_mat_vec_product(struct isl_mat
*mat
, struct isl_vec
*vec
)
426 struct isl_vec
*prod
;
431 isl_assert(mat
->ctx
, mat
->n_col
== vec
->size
, goto error
);
433 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
437 for (i
= 0; i
< prod
->size
; ++i
)
438 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
439 &prod
->block
.data
[i
]);
449 __isl_give isl_vec
*isl_mat_vec_inverse_product(__isl_take isl_mat
*mat
,
450 __isl_take isl_vec
*vec
)
452 struct isl_mat
*vec_mat
;
457 vec_mat
= isl_mat_alloc(vec
->ctx
, vec
->size
, 1);
460 for (i
= 0; i
< vec
->size
; ++i
)
461 isl_int_set(vec_mat
->row
[i
][0], vec
->el
[i
]);
462 vec_mat
= isl_mat_inverse_product(mat
, vec_mat
);
466 vec
= isl_vec_alloc(vec_mat
->ctx
, vec_mat
->n_row
);
468 for (i
= 0; i
< vec
->size
; ++i
)
469 isl_int_set(vec
->el
[i
], vec_mat
->row
[i
][0]);
470 isl_mat_free(vec_mat
);
478 struct isl_vec
*isl_vec_mat_product(struct isl_vec
*vec
, struct isl_mat
*mat
)
481 struct isl_vec
*prod
;
486 isl_assert(mat
->ctx
, mat
->n_row
== vec
->size
, goto error
);
488 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_col
);
492 for (i
= 0; i
< prod
->size
; ++i
) {
493 isl_int_set_si(prod
->el
[i
], 0);
494 for (j
= 0; j
< vec
->size
; ++j
)
495 isl_int_addmul(prod
->el
[i
], vec
->el
[j
], mat
->row
[j
][i
]);
506 struct isl_mat
*isl_mat_aff_direct_sum(struct isl_mat
*left
,
507 struct isl_mat
*right
)
515 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
516 isl_assert(left
->ctx
, left
->n_row
>= 1, goto error
);
517 isl_assert(left
->ctx
, left
->n_col
>= 1, goto error
);
518 isl_assert(left
->ctx
, right
->n_col
>= 1, goto error
);
519 isl_assert(left
->ctx
,
520 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
522 isl_assert(left
->ctx
,
523 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
526 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
529 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
530 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
531 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
533 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
534 for (i
= 1; i
< sum
->n_row
; ++i
) {
535 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
536 isl_int_addmul(sum
->row
[i
][0],
537 right
->row
[0][0], right
->row
[i
][0]);
538 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
540 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
541 right
->row
[i
]+1, right
->row
[0][0],
545 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
546 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
556 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
557 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
560 for (r
= row
; r
< M
->n_row
; ++r
)
561 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
563 for (r
= 0; r
< (*U
)->n_row
; ++r
)
564 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
567 isl_mat_swap_rows(*Q
, i
, j
);
570 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
571 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
574 for (r
= row
; r
< M
->n_row
; ++r
)
575 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
577 for (r
= 0; r
< (*U
)->n_row
; ++r
)
578 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
581 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
582 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
586 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
587 struct isl_mat
**Q
, unsigned row
, unsigned col
)
590 for (r
= row
; r
< M
->n_row
; ++r
)
591 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
593 for (r
= 0; r
< (*U
)->n_row
; ++r
)
594 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
597 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
600 /* Given matrix M, compute
605 * with U and Q unimodular matrices and H a matrix in column echelon form
606 * such that on each echelon row the entries in the non-echelon column
607 * are non-negative (if neg == 0) or non-positive (if neg == 1)
608 * and strictly smaller (in absolute value) than the entries in the echelon
610 * If U or Q are NULL, then these matrices are not computed.
612 struct isl_mat
*isl_mat_left_hermite(struct isl_mat
*M
, int neg
,
613 struct isl_mat
**U
, struct isl_mat
**Q
)
628 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
633 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
640 for (row
= 0; row
< M
->n_row
; ++row
) {
642 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
647 exchange(M
, U
, Q
, row
, first
, col
);
648 if (isl_int_is_neg(M
->row
[row
][col
]))
649 oppose(M
, U
, Q
, row
, col
);
651 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
652 M
->n_col
-first
)) != -1) {
654 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
655 subtract(M
, U
, Q
, row
, col
, first
, c
);
656 if (!isl_int_is_zero(M
->row
[row
][first
]))
657 exchange(M
, U
, Q
, row
, first
, col
);
661 for (i
= 0; i
< col
; ++i
) {
662 if (isl_int_is_zero(M
->row
[row
][i
]))
665 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
667 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
668 if (isl_int_is_zero(c
))
670 subtract(M
, U
, Q
, row
, col
, i
, c
);
690 struct isl_mat
*isl_mat_right_kernel(struct isl_mat
*mat
)
693 struct isl_mat
*U
= NULL
;
696 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
700 for (i
= 0, rank
= 0; rank
< mat
->n_col
; ++rank
) {
701 while (i
< mat
->n_row
&& isl_int_is_zero(mat
->row
[i
][rank
]))
706 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
709 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
719 struct isl_mat
*isl_mat_lin_to_aff(struct isl_mat
*mat
)
722 struct isl_mat
*mat2
;
726 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
729 isl_int_set_si(mat2
->row
[0][0], 1);
730 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
731 for (i
= 0; i
< mat
->n_row
; ++i
) {
732 isl_int_set_si(mat2
->row
[1+i
][0], 0);
733 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
742 /* Given two matrices M1 and M2, return the block matrix
747 __isl_give isl_mat
*isl_mat_diagonal(__isl_take isl_mat
*mat1
,
748 __isl_take isl_mat
*mat2
)
756 mat
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
757 mat1
->n_col
+ mat2
->n_col
);
760 for (i
= 0; i
< mat1
->n_row
; ++i
) {
761 isl_seq_cpy(mat
->row
[i
], mat1
->row
[i
], mat1
->n_col
);
762 isl_seq_clr(mat
->row
[i
] + mat1
->n_col
, mat2
->n_col
);
764 for (i
= 0; i
< mat2
->n_row
; ++i
) {
765 isl_seq_clr(mat
->row
[mat1
->n_row
+ i
], mat1
->n_col
);
766 isl_seq_cpy(mat
->row
[mat1
->n_row
+ i
] + mat1
->n_col
,
767 mat2
->row
[i
], mat2
->n_col
);
778 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
782 for (i
= 0; i
< n_row
; ++i
)
783 if (!isl_int_is_zero(row
[i
][col
]))
788 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
790 int i
, min
= row_first_non_zero(row
, n_row
, col
);
793 for (i
= min
+ 1; i
< n_row
; ++i
) {
794 if (isl_int_is_zero(row
[i
][col
]))
796 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
802 static isl_stat
inv_exchange(__isl_keep isl_mat
**left
,
803 __isl_keep isl_mat
**right
, unsigned i
, unsigned j
)
805 *left
= isl_mat_swap_rows(*left
, i
, j
);
806 *right
= isl_mat_swap_rows(*right
, i
, j
);
808 if (!*left
|| !*right
)
809 return isl_stat_error
;
813 static void inv_oppose(
814 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
816 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
817 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
820 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
821 unsigned row
, unsigned i
, isl_int m
)
824 isl_seq_combine(left
->row
[i
]+row
,
825 left
->ctx
->one
, left
->row
[i
]+row
,
826 m
, left
->row
[row
]+row
,
828 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
829 m
, right
->row
[row
], right
->n_col
);
832 /* Compute inv(left)*right
834 struct isl_mat
*isl_mat_inverse_product(struct isl_mat
*left
,
835 struct isl_mat
*right
)
843 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
844 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
846 if (left
->n_row
== 0) {
851 left
= isl_mat_cow(left
);
852 right
= isl_mat_cow(right
);
858 for (row
= 0; row
< left
->n_row
; ++row
) {
859 int pivot
, first
, i
, off
;
860 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
864 isl_assert(left
->ctx
, pivot
>= 0, goto error
);
868 if (inv_exchange(&left
, &right
, pivot
, row
) < 0)
870 if (isl_int_is_neg(left
->row
[row
][row
]))
871 inv_oppose(left
, right
, row
);
873 while ((off
= row_first_non_zero(left
->row
+first
,
874 left
->n_row
-first
, row
)) != -1) {
876 isl_int_fdiv_q(a
, left
->row
[first
][row
],
877 left
->row
[row
][row
]);
878 inv_subtract(left
, right
, row
, first
, a
);
879 if (!isl_int_is_zero(left
->row
[first
][row
])) {
880 if (inv_exchange(&left
, &right
, row
, first
) < 0)
886 for (i
= 0; i
< row
; ++i
) {
887 if (isl_int_is_zero(left
->row
[i
][row
]))
889 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
890 isl_int_divexact(b
, left
->row
[i
][row
], a
);
891 isl_int_divexact(a
, left
->row
[row
][row
], a
);
893 isl_seq_combine(left
->row
[i
] + i
,
895 b
, left
->row
[row
] + i
,
897 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
898 b
, right
->row
[row
], right
->n_col
);
903 isl_int_set(a
, left
->row
[0][0]);
904 for (row
= 1; row
< left
->n_row
; ++row
)
905 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
906 if (isl_int_is_zero(a
)){
908 isl_assert(left
->ctx
, 0, goto error
);
910 for (row
= 0; row
< left
->n_row
; ++row
) {
911 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
912 if (isl_int_is_one(left
->row
[row
][row
]))
914 isl_seq_scale(right
->row
[row
], right
->row
[row
],
915 left
->row
[row
][row
], right
->n_col
);
927 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
931 for (i
= 0; i
< mat
->n_row
; ++i
)
932 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
935 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
936 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
942 for (i
= 0; i
< mat
->n_row
; ++i
) {
943 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
944 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
945 isl_int_set(mat
->row
[i
][dst
], tmp
);
950 struct isl_mat
*isl_mat_right_inverse(struct isl_mat
*mat
)
956 mat
= isl_mat_cow(mat
);
960 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
961 inv
= isl_mat_cow(inv
);
967 for (row
= 0; row
< mat
->n_row
; ++row
) {
968 int pivot
, first
, i
, off
;
969 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
973 isl_assert(mat
->ctx
, pivot
>= 0, goto error
);
977 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
978 if (isl_int_is_neg(mat
->row
[row
][row
]))
979 oppose(mat
, &inv
, NULL
, row
, row
);
981 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
982 mat
->n_col
-first
)) != -1) {
984 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
986 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
987 if (!isl_int_is_zero(mat
->row
[row
][first
]))
988 exchange(mat
, &inv
, NULL
, row
, row
, first
);
992 for (i
= 0; i
< row
; ++i
) {
993 if (isl_int_is_zero(mat
->row
[row
][i
]))
995 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
996 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
997 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
999 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
1000 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
1005 isl_int_set(a
, mat
->row
[0][0]);
1006 for (row
= 1; row
< mat
->n_row
; ++row
)
1007 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
1008 if (isl_int_is_zero(a
)){
1012 for (row
= 0; row
< mat
->n_row
; ++row
) {
1013 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
1014 if (isl_int_is_one(mat
->row
[row
][row
]))
1016 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
1029 struct isl_mat
*isl_mat_transpose(struct isl_mat
*mat
)
1031 struct isl_mat
*transpose
= NULL
;
1037 if (mat
->n_col
== mat
->n_row
) {
1038 mat
= isl_mat_cow(mat
);
1041 for (i
= 0; i
< mat
->n_row
; ++i
)
1042 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
1043 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
1046 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
1049 for (i
= 0; i
< mat
->n_row
; ++i
)
1050 for (j
= 0; j
< mat
->n_col
; ++j
)
1051 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
1059 struct isl_mat
*isl_mat_swap_cols(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1063 mat
= isl_mat_cow(mat
);
1066 isl_assert(mat
->ctx
, i
< mat
->n_col
, goto error
);
1067 isl_assert(mat
->ctx
, j
< mat
->n_col
, goto error
);
1069 for (r
= 0; r
< mat
->n_row
; ++r
)
1070 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
1077 struct isl_mat
*isl_mat_swap_rows(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1083 mat
= isl_mat_cow(mat
);
1087 mat
->row
[i
] = mat
->row
[j
];
1092 /* Calculate the product of two matrices.
1094 * This function is optimized for operand matrices that contain many zeros and
1095 * skips multiplications where we know one of the operands is zero.
1097 __isl_give isl_mat
*isl_mat_product(__isl_take isl_mat
*left
,
1098 __isl_take isl_mat
*right
)
1101 struct isl_mat
*prod
;
1103 if (!left
|| !right
)
1105 isl_assert(left
->ctx
, left
->n_col
== right
->n_row
, goto error
);
1106 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
1109 if (left
->n_col
== 0) {
1110 for (i
= 0; i
< prod
->n_row
; ++i
)
1111 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
1113 isl_mat_free(right
);
1116 for (i
= 0; i
< prod
->n_row
; ++i
) {
1117 for (j
= 0; j
< prod
->n_col
; ++j
)
1118 isl_int_mul(prod
->row
[i
][j
],
1119 left
->row
[i
][0], right
->row
[0][j
]);
1120 for (k
= 1; k
< left
->n_col
; ++k
) {
1121 if (isl_int_is_zero(left
->row
[i
][k
]))
1123 for (j
= 0; j
< prod
->n_col
; ++j
)
1124 isl_int_addmul(prod
->row
[i
][j
],
1125 left
->row
[i
][k
], right
->row
[k
][j
]);
1129 isl_mat_free(right
);
1133 isl_mat_free(right
);
1137 /* Replace the variables x in the rows q by x' given by x = M x',
1138 * with M the matrix mat.
1140 * If the number of new variables is greater than the original
1141 * number of variables, then the rows q have already been
1142 * preextended. If the new number is smaller, then the coefficients
1143 * of the divs, which are not changed, need to be shifted down.
1144 * The row q may be the equalities, the inequalities or the
1145 * div expressions. In the latter case, has_div is true and
1146 * we need to take into account the extra denominator column.
1148 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1149 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
1155 if (mat
->n_col
>= mat
->n_row
)
1158 e
= mat
->n_row
- mat
->n_col
;
1160 for (i
= 0; i
< n
; ++i
)
1161 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
1162 t
= isl_mat_sub_alloc6(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
1163 t
= isl_mat_product(t
, mat
);
1166 for (i
= 0; i
< n
; ++i
) {
1167 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
1168 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
1169 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
1170 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
1176 /* Replace the variables x in bset by x' given by x = M x', with
1179 * If there are fewer variables x' then there are x, then we perform
1180 * the transformation in place, which means that, in principle,
1181 * this frees up some extra variables as the number
1182 * of columns remains constant, but we would have to extend
1183 * the div array too as the number of rows in this array is assumed
1184 * to be equal to extra.
1186 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
1187 struct isl_mat
*mat
)
1189 struct isl_ctx
*ctx
;
1195 bset
= isl_basic_set_cow(bset
);
1199 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
1200 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
1201 isl_assert(ctx
, mat
->n_col
> 0, goto error
);
1203 if (mat
->n_col
> mat
->n_row
) {
1204 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0, 0, 0);
1207 } else if (mat
->n_col
< mat
->n_row
) {
1208 bset
->dim
= isl_space_cow(bset
->dim
);
1211 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
1214 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
1215 isl_mat_copy(mat
)) < 0)
1218 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
1219 isl_mat_copy(mat
)) < 0)
1222 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
1225 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1226 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1227 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1228 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
1229 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
1231 bset
= isl_basic_set_simplify(bset
);
1232 bset
= isl_basic_set_finalize(bset
);
1238 isl_basic_set_free(bset
);
1242 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
1246 set
= isl_set_cow(set
);
1250 for (i
= 0; i
< set
->n
; ++i
) {
1251 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
1256 if (mat
->n_col
!= mat
->n_row
) {
1257 set
->dim
= isl_space_cow(set
->dim
);
1260 set
->dim
->n_out
+= mat
->n_col
;
1261 set
->dim
->n_out
-= mat
->n_row
;
1264 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1272 /* Replace the variables x starting at pos in the rows q
1273 * by x' with x = M x' with M the matrix mat.
1274 * That is, replace the corresponding coefficients c by c M.
1276 static int transform(isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1277 unsigned pos
, __isl_take isl_mat
*mat
)
1282 t
= isl_mat_sub_alloc6(ctx
, q
, 0, n
, pos
, mat
->n_row
);
1283 t
= isl_mat_product(t
, mat
);
1286 for (i
= 0; i
< n
; ++i
)
1287 isl_seq_swp_or_cpy(q
[i
] + pos
, t
->row
[i
], t
->n_col
);
1292 /* Replace the variables x of type "type" starting at "first" in "bmap"
1293 * by x' with x = M x' with M the matrix trans.
1294 * That is, replace the corresponding coefficients c by c M.
1296 * The transformation matrix should be a square matrix.
1298 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
1299 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
1300 __isl_take isl_mat
*trans
)
1305 bmap
= isl_basic_map_cow(bmap
);
1306 if (!bmap
|| !trans
)
1309 ctx
= isl_basic_map_get_ctx(bmap
);
1310 if (trans
->n_row
!= trans
->n_col
)
1311 isl_die(trans
->ctx
, isl_error_invalid
,
1312 "expecting square transformation matrix", goto error
);
1313 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
1314 isl_die(trans
->ctx
, isl_error_invalid
,
1315 "oversized transformation matrix", goto error
);
1317 pos
= isl_basic_map_offset(bmap
, type
) + first
;
1319 if (transform(ctx
, bmap
->eq
, bmap
->n_eq
, pos
, isl_mat_copy(trans
)) < 0)
1321 if (transform(ctx
, bmap
->ineq
, bmap
->n_ineq
, pos
,
1322 isl_mat_copy(trans
)) < 0)
1324 if (transform(ctx
, bmap
->div
, bmap
->n_div
, 1 + pos
,
1325 isl_mat_copy(trans
)) < 0)
1328 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1329 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1331 isl_mat_free(trans
);
1334 isl_mat_free(trans
);
1335 isl_basic_map_free(bmap
);
1339 /* Replace the variables x of type "type" starting at "first" in "bset"
1340 * by x' with x = M x' with M the matrix trans.
1341 * That is, replace the corresponding coefficients c by c M.
1343 * The transformation matrix should be a square matrix.
1345 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
1346 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
1347 __isl_take isl_mat
*trans
)
1349 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);
1352 void isl_mat_print_internal(__isl_keep isl_mat
*mat
, FILE *out
, int indent
)
1357 fprintf(out
, "%*snull mat\n", indent
, "");
1361 if (mat
->n_row
== 0)
1362 fprintf(out
, "%*s[]\n", indent
, "");
1364 for (i
= 0; i
< mat
->n_row
; ++i
) {
1366 fprintf(out
, "%*s[[", indent
, "");
1368 fprintf(out
, "%*s[", indent
+1, "");
1369 for (j
= 0; j
< mat
->n_col
; ++j
) {
1372 isl_int_print(out
, mat
->row
[i
][j
], 0);
1374 if (i
== mat
->n_row
-1)
1375 fprintf(out
, "]]\n");
1377 fprintf(out
, "]\n");
1381 void isl_mat_dump(__isl_keep isl_mat
*mat
)
1383 isl_mat_print_internal(mat
, stderr
, 0);
1386 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
1393 mat
= isl_mat_cow(mat
);
1397 if (col
!= mat
->n_col
-n
) {
1398 for (r
= 0; r
< mat
->n_row
; ++r
)
1399 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
1400 mat
->n_col
- col
- n
);
1406 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
1410 mat
= isl_mat_cow(mat
);
1414 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
1415 mat
->row
[r
] = mat
->row
[r
+n
];
1421 __isl_give isl_mat
*isl_mat_insert_cols(__isl_take isl_mat
*mat
,
1422 unsigned col
, unsigned n
)
1431 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
+ n
);
1435 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
, 0, 0, col
);
1436 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
,
1437 col
+ n
, col
, mat
->n_col
- col
);
1446 __isl_give isl_mat
*isl_mat_insert_zero_cols(__isl_take isl_mat
*mat
,
1447 unsigned first
, unsigned n
)
1453 mat
= isl_mat_insert_cols(mat
, first
, n
);
1457 for (i
= 0; i
< mat
->n_row
; ++i
)
1458 isl_seq_clr(mat
->row
[i
] + first
, n
);
1463 __isl_give isl_mat
*isl_mat_add_zero_cols(__isl_take isl_mat
*mat
, unsigned n
)
1468 return isl_mat_insert_zero_cols(mat
, mat
->n_col
, n
);
1471 __isl_give isl_mat
*isl_mat_insert_rows(__isl_take isl_mat
*mat
,
1472 unsigned row
, unsigned n
)
1481 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
+ n
, mat
->n_col
);
1485 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, row
, 0, 0, mat
->n_col
);
1486 isl_mat_sub_copy(mat
->ctx
, ext
->row
+ row
+ n
, mat
->row
+ row
,
1487 mat
->n_row
- row
, 0, 0, mat
->n_col
);
1496 __isl_give isl_mat
*isl_mat_add_rows(__isl_take isl_mat
*mat
, unsigned n
)
1501 return isl_mat_insert_rows(mat
, mat
->n_row
, n
);
1504 __isl_give isl_mat
*isl_mat_insert_zero_rows(__isl_take isl_mat
*mat
,
1505 unsigned row
, unsigned n
)
1509 mat
= isl_mat_insert_rows(mat
, row
, n
);
1513 for (i
= 0; i
< n
; ++i
)
1514 isl_seq_clr(mat
->row
[row
+ i
], mat
->n_col
);
1519 __isl_give isl_mat
*isl_mat_add_zero_rows(__isl_take isl_mat
*mat
, unsigned n
)
1524 return isl_mat_insert_zero_rows(mat
, mat
->n_row
, n
);
1527 void isl_mat_col_submul(struct isl_mat
*mat
,
1528 int dst_col
, isl_int f
, int src_col
)
1532 for (i
= 0; i
< mat
->n_row
; ++i
)
1533 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1536 void isl_mat_col_add(__isl_keep isl_mat
*mat
, int dst_col
, int src_col
)
1543 for (i
= 0; i
< mat
->n_row
; ++i
)
1544 isl_int_add(mat
->row
[i
][dst_col
],
1545 mat
->row
[i
][dst_col
], mat
->row
[i
][src_col
]);
1548 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1552 for (i
= 0; i
< mat
->n_row
; ++i
)
1553 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1556 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1557 * return the result.
1559 __isl_give isl_mat
*isl_mat_col_addmul(__isl_take isl_mat
*mat
, int dst_col
,
1560 isl_int f
, int src_col
)
1564 if (check_col(mat
, dst_col
) < 0 || check_col(mat
, src_col
) < 0)
1565 return isl_mat_free(mat
);
1567 for (i
= 0; i
< mat
->n_row
; ++i
) {
1568 if (isl_int_is_zero(mat
->row
[i
][src_col
]))
1570 mat
= isl_mat_cow(mat
);
1573 isl_int_addmul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1579 /* Negate column "col" of "mat" and return the result.
1581 __isl_give isl_mat
*isl_mat_col_neg(__isl_take isl_mat
*mat
, int col
)
1585 if (check_col(mat
, col
) < 0)
1586 return isl_mat_free(mat
);
1588 for (i
= 0; i
< mat
->n_row
; ++i
) {
1589 if (isl_int_is_zero(mat
->row
[i
][col
]))
1591 mat
= isl_mat_cow(mat
);
1594 isl_int_neg(mat
->row
[i
][col
], mat
->row
[i
][col
]);
1600 struct isl_mat
*isl_mat_unimodular_complete(struct isl_mat
*M
, int row
)
1603 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1608 isl_assert(M
->ctx
, M
->n_row
== M
->n_col
, goto error
);
1610 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1611 M
->n_row
= M
->n_col
;
1614 for (r
= 0; r
< row
; ++r
)
1615 isl_assert(M
->ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1616 for (r
= row
; r
< M
->n_row
; ++r
)
1617 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);
1628 __isl_give isl_mat
*isl_mat_concat(__isl_take isl_mat
*top
,
1629 __isl_take isl_mat
*bot
)
1631 struct isl_mat
*mat
;
1636 isl_assert(top
->ctx
, top
->n_col
== bot
->n_col
, goto error
);
1637 if (top
->n_row
== 0) {
1641 if (bot
->n_row
== 0) {
1646 mat
= isl_mat_alloc(top
->ctx
, top
->n_row
+ bot
->n_row
, top
->n_col
);
1649 isl_mat_sub_copy(mat
->ctx
, mat
->row
, top
->row
, top
->n_row
,
1651 isl_mat_sub_copy(mat
->ctx
, mat
->row
+ top
->n_row
, bot
->row
, bot
->n_row
,
1662 isl_bool
isl_mat_is_equal(__isl_keep isl_mat
*mat1
, __isl_keep isl_mat
*mat2
)
1667 return isl_bool_error
;
1669 if (mat1
->n_row
!= mat2
->n_row
)
1670 return isl_bool_false
;
1672 if (mat1
->n_col
!= mat2
->n_col
)
1673 return isl_bool_false
;
1675 for (i
= 0; i
< mat1
->n_row
; ++i
)
1676 if (!isl_seq_eq(mat1
->row
[i
], mat2
->row
[i
], mat1
->n_col
))
1677 return isl_bool_false
;
1679 return isl_bool_true
;
1682 __isl_give isl_mat
*isl_mat_from_row_vec(__isl_take isl_vec
*vec
)
1684 struct isl_mat
*mat
;
1688 mat
= isl_mat_alloc(vec
->ctx
, 1, vec
->size
);
1692 isl_seq_cpy(mat
->row
[0], vec
->el
, vec
->size
);
1701 /* Return a copy of row "row" of "mat" as an isl_vec.
1703 __isl_give isl_vec
*isl_mat_get_row(__isl_keep isl_mat
*mat
, unsigned row
)
1709 if (row
>= mat
->n_row
)
1710 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
1713 v
= isl_vec_alloc(isl_mat_get_ctx(mat
), mat
->n_col
);
1716 isl_seq_cpy(v
->el
, mat
->row
[row
], mat
->n_col
);
1721 __isl_give isl_mat
*isl_mat_vec_concat(__isl_take isl_mat
*top
,
1722 __isl_take isl_vec
*bot
)
1724 return isl_mat_concat(top
, isl_mat_from_row_vec(bot
));
1727 __isl_give isl_mat
*isl_mat_move_cols(__isl_take isl_mat
*mat
,
1728 unsigned dst_col
, unsigned src_col
, unsigned n
)
1734 if (n
== 0 || dst_col
== src_col
)
1737 res
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
1741 if (dst_col
< src_col
) {
1742 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1744 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1745 dst_col
, src_col
, n
);
1746 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1747 dst_col
+ n
, dst_col
, src_col
- dst_col
);
1748 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1749 src_col
+ n
, src_col
+ n
,
1750 res
->n_col
- src_col
- n
);
1752 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1754 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1755 src_col
, src_col
+ n
, dst_col
- src_col
);
1756 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1757 dst_col
, src_col
, n
);
1758 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1759 dst_col
+ n
, dst_col
+ n
,
1760 res
->n_col
- dst_col
- n
);
1770 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1771 * Return isl_stat_ok on success and isl_stat_error on failure.
1773 isl_stat
isl_mat_row_gcd(__isl_keep isl_mat
*mat
, int row
, isl_int
*gcd
)
1776 return isl_stat_error
;
1778 if (row
< 0 || row
>= mat
->n_row
)
1779 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
1780 "row out of range", return isl_stat_error
);
1781 isl_seq_gcd(mat
->row
[row
], mat
->n_col
, gcd
);
1786 void isl_mat_gcd(__isl_keep isl_mat
*mat
, isl_int
*gcd
)
1791 isl_int_set_si(*gcd
, 0);
1796 for (i
= 0; i
< mat
->n_row
; ++i
) {
1797 isl_seq_gcd(mat
->row
[i
], mat
->n_col
, &g
);
1798 isl_int_gcd(*gcd
, *gcd
, g
);
1803 /* Return the result of scaling "mat" by a factor of "m".
1805 __isl_give isl_mat
*isl_mat_scale(__isl_take isl_mat
*mat
, isl_int m
)
1809 if (isl_int_is_one(m
))
1812 mat
= isl_mat_cow(mat
);
1816 for (i
= 0; i
< mat
->n_row
; ++i
)
1817 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1822 __isl_give isl_mat
*isl_mat_scale_down(__isl_take isl_mat
*mat
, isl_int m
)
1826 if (isl_int_is_one(m
))
1829 mat
= isl_mat_cow(mat
);
1833 for (i
= 0; i
< mat
->n_row
; ++i
)
1834 isl_seq_scale_down(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1839 __isl_give isl_mat
*isl_mat_scale_down_row(__isl_take isl_mat
*mat
, int row
,
1842 if (isl_int_is_one(m
))
1845 mat
= isl_mat_cow(mat
);
1849 isl_seq_scale_down(mat
->row
[row
], mat
->row
[row
], m
, mat
->n_col
);
1854 __isl_give isl_mat
*isl_mat_normalize(__isl_take isl_mat
*mat
)
1862 isl_mat_gcd(mat
, &gcd
);
1863 mat
= isl_mat_scale_down(mat
, gcd
);
1869 __isl_give isl_mat
*isl_mat_normalize_row(__isl_take isl_mat
*mat
, int row
)
1871 mat
= isl_mat_cow(mat
);
1875 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], mat
->n_col
);
1880 /* Number of initial non-zero columns.
1882 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat
*mat
)
1889 for (i
= 0; i
< mat
->n_col
; ++i
)
1890 if (row_first_non_zero(mat
->row
, mat
->n_row
, i
) < 0)