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 void inv_exchange(struct isl_mat
*left
, struct isl_mat
*right
,
803 unsigned i
, unsigned j
)
805 left
= isl_mat_swap_rows(left
, i
, j
);
806 right
= isl_mat_swap_rows(right
, i
, j
);
809 static void inv_oppose(
810 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
812 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
813 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
816 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
817 unsigned row
, unsigned i
, isl_int m
)
820 isl_seq_combine(left
->row
[i
]+row
,
821 left
->ctx
->one
, left
->row
[i
]+row
,
822 m
, left
->row
[row
]+row
,
824 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
825 m
, right
->row
[row
], right
->n_col
);
828 /* Compute inv(left)*right
830 struct isl_mat
*isl_mat_inverse_product(struct isl_mat
*left
,
831 struct isl_mat
*right
)
839 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
840 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
842 if (left
->n_row
== 0) {
847 left
= isl_mat_cow(left
);
848 right
= isl_mat_cow(right
);
854 for (row
= 0; row
< left
->n_row
; ++row
) {
855 int pivot
, first
, i
, off
;
856 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
860 isl_assert(left
->ctx
, pivot
>= 0, goto error
);
864 inv_exchange(left
, right
, pivot
, row
);
865 if (isl_int_is_neg(left
->row
[row
][row
]))
866 inv_oppose(left
, right
, row
);
868 while ((off
= row_first_non_zero(left
->row
+first
,
869 left
->n_row
-first
, row
)) != -1) {
871 isl_int_fdiv_q(a
, left
->row
[first
][row
],
872 left
->row
[row
][row
]);
873 inv_subtract(left
, right
, row
, first
, a
);
874 if (!isl_int_is_zero(left
->row
[first
][row
]))
875 inv_exchange(left
, right
, row
, first
);
879 for (i
= 0; i
< row
; ++i
) {
880 if (isl_int_is_zero(left
->row
[i
][row
]))
882 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
883 isl_int_divexact(b
, left
->row
[i
][row
], a
);
884 isl_int_divexact(a
, left
->row
[row
][row
], a
);
886 isl_seq_combine(left
->row
[i
] + i
,
888 b
, left
->row
[row
] + i
,
890 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
891 b
, right
->row
[row
], right
->n_col
);
896 isl_int_set(a
, left
->row
[0][0]);
897 for (row
= 1; row
< left
->n_row
; ++row
)
898 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
899 if (isl_int_is_zero(a
)){
901 isl_assert(left
->ctx
, 0, goto error
);
903 for (row
= 0; row
< left
->n_row
; ++row
) {
904 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
905 if (isl_int_is_one(left
->row
[row
][row
]))
907 isl_seq_scale(right
->row
[row
], right
->row
[row
],
908 left
->row
[row
][row
], right
->n_col
);
920 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
924 for (i
= 0; i
< mat
->n_row
; ++i
)
925 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
928 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
929 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
935 for (i
= 0; i
< mat
->n_row
; ++i
) {
936 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
937 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
938 isl_int_set(mat
->row
[i
][dst
], tmp
);
943 struct isl_mat
*isl_mat_right_inverse(struct isl_mat
*mat
)
949 mat
= isl_mat_cow(mat
);
953 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
954 inv
= isl_mat_cow(inv
);
960 for (row
= 0; row
< mat
->n_row
; ++row
) {
961 int pivot
, first
, i
, off
;
962 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
966 isl_assert(mat
->ctx
, pivot
>= 0, goto error
);
970 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
971 if (isl_int_is_neg(mat
->row
[row
][row
]))
972 oppose(mat
, &inv
, NULL
, row
, row
);
974 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
975 mat
->n_col
-first
)) != -1) {
977 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
979 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
980 if (!isl_int_is_zero(mat
->row
[row
][first
]))
981 exchange(mat
, &inv
, NULL
, row
, row
, first
);
985 for (i
= 0; i
< row
; ++i
) {
986 if (isl_int_is_zero(mat
->row
[row
][i
]))
988 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
989 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
990 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
992 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
993 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
998 isl_int_set(a
, mat
->row
[0][0]);
999 for (row
= 1; row
< mat
->n_row
; ++row
)
1000 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
1001 if (isl_int_is_zero(a
)){
1005 for (row
= 0; row
< mat
->n_row
; ++row
) {
1006 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
1007 if (isl_int_is_one(mat
->row
[row
][row
]))
1009 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
1022 struct isl_mat
*isl_mat_transpose(struct isl_mat
*mat
)
1024 struct isl_mat
*transpose
= NULL
;
1030 if (mat
->n_col
== mat
->n_row
) {
1031 mat
= isl_mat_cow(mat
);
1034 for (i
= 0; i
< mat
->n_row
; ++i
)
1035 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
1036 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
1039 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
1042 for (i
= 0; i
< mat
->n_row
; ++i
)
1043 for (j
= 0; j
< mat
->n_col
; ++j
)
1044 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
1052 struct isl_mat
*isl_mat_swap_cols(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1056 mat
= isl_mat_cow(mat
);
1059 isl_assert(mat
->ctx
, i
< mat
->n_col
, goto error
);
1060 isl_assert(mat
->ctx
, j
< mat
->n_col
, goto error
);
1062 for (r
= 0; r
< mat
->n_row
; ++r
)
1063 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
1070 struct isl_mat
*isl_mat_swap_rows(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1076 mat
= isl_mat_cow(mat
);
1080 mat
->row
[i
] = mat
->row
[j
];
1085 /* Calculate the product of two matrices.
1087 * This function is optimized for operand matrices that contain many zeros and
1088 * skips multiplications where we know one of the operands is zero.
1090 __isl_give isl_mat
*isl_mat_product(__isl_take isl_mat
*left
,
1091 __isl_take isl_mat
*right
)
1094 struct isl_mat
*prod
;
1096 if (!left
|| !right
)
1098 isl_assert(left
->ctx
, left
->n_col
== right
->n_row
, goto error
);
1099 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
1102 if (left
->n_col
== 0) {
1103 for (i
= 0; i
< prod
->n_row
; ++i
)
1104 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
1106 isl_mat_free(right
);
1109 for (i
= 0; i
< prod
->n_row
; ++i
) {
1110 for (j
= 0; j
< prod
->n_col
; ++j
)
1111 isl_int_mul(prod
->row
[i
][j
],
1112 left
->row
[i
][0], right
->row
[0][j
]);
1113 for (k
= 1; k
< left
->n_col
; ++k
) {
1114 if (isl_int_is_zero(left
->row
[i
][k
]))
1116 for (j
= 0; j
< prod
->n_col
; ++j
)
1117 isl_int_addmul(prod
->row
[i
][j
],
1118 left
->row
[i
][k
], right
->row
[k
][j
]);
1122 isl_mat_free(right
);
1126 isl_mat_free(right
);
1130 /* Replace the variables x in the rows q by x' given by x = M x',
1131 * with M the matrix mat.
1133 * If the number of new variables is greater than the original
1134 * number of variables, then the rows q have already been
1135 * preextended. If the new number is smaller, then the coefficients
1136 * of the divs, which are not changed, need to be shifted down.
1137 * The row q may be the equalities, the inequalities or the
1138 * div expressions. In the latter case, has_div is true and
1139 * we need to take into account the extra denominator column.
1141 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1142 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
1148 if (mat
->n_col
>= mat
->n_row
)
1151 e
= mat
->n_row
- mat
->n_col
;
1153 for (i
= 0; i
< n
; ++i
)
1154 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
1155 t
= isl_mat_sub_alloc6(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
1156 t
= isl_mat_product(t
, mat
);
1159 for (i
= 0; i
< n
; ++i
) {
1160 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
1161 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
1162 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
1163 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
1169 /* Replace the variables x in bset by x' given by x = M x', with
1172 * If there are fewer variables x' then there are x, then we perform
1173 * the transformation in place, which means that, in principle,
1174 * this frees up some extra variables as the number
1175 * of columns remains constant, but we would have to extend
1176 * the div array too as the number of rows in this array is assumed
1177 * to be equal to extra.
1179 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
1180 struct isl_mat
*mat
)
1182 struct isl_ctx
*ctx
;
1188 bset
= isl_basic_set_cow(bset
);
1192 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
1193 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
1194 isl_assert(ctx
, mat
->n_col
> 0, goto error
);
1196 if (mat
->n_col
> mat
->n_row
) {
1197 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0, 0, 0);
1200 } else if (mat
->n_col
< mat
->n_row
) {
1201 bset
->dim
= isl_space_cow(bset
->dim
);
1204 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
1207 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
1208 isl_mat_copy(mat
)) < 0)
1211 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
1212 isl_mat_copy(mat
)) < 0)
1215 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
1218 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1219 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1220 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1221 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
1222 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
1224 bset
= isl_basic_set_simplify(bset
);
1225 bset
= isl_basic_set_finalize(bset
);
1231 isl_basic_set_free(bset
);
1235 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
1239 set
= isl_set_cow(set
);
1243 for (i
= 0; i
< set
->n
; ++i
) {
1244 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
1249 if (mat
->n_col
!= mat
->n_row
) {
1250 set
->dim
= isl_space_cow(set
->dim
);
1253 set
->dim
->n_out
+= mat
->n_col
;
1254 set
->dim
->n_out
-= mat
->n_row
;
1257 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1265 /* Replace the variables x starting at pos in the rows q
1266 * by x' with x = M x' with M the matrix mat.
1267 * That is, replace the corresponding coefficients c by c M.
1269 static int transform(isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1270 unsigned pos
, __isl_take isl_mat
*mat
)
1275 t
= isl_mat_sub_alloc6(ctx
, q
, 0, n
, pos
, mat
->n_row
);
1276 t
= isl_mat_product(t
, mat
);
1279 for (i
= 0; i
< n
; ++i
)
1280 isl_seq_swp_or_cpy(q
[i
] + pos
, t
->row
[i
], t
->n_col
);
1285 /* Replace the variables x of type "type" starting at "first" in "bmap"
1286 * by x' with x = M x' with M the matrix trans.
1287 * That is, replace the corresponding coefficients c by c M.
1289 * The transformation matrix should be a square matrix.
1291 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
1292 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
1293 __isl_take isl_mat
*trans
)
1298 bmap
= isl_basic_map_cow(bmap
);
1299 if (!bmap
|| !trans
)
1302 ctx
= isl_basic_map_get_ctx(bmap
);
1303 if (trans
->n_row
!= trans
->n_col
)
1304 isl_die(trans
->ctx
, isl_error_invalid
,
1305 "expecting square transformation matrix", goto error
);
1306 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
1307 isl_die(trans
->ctx
, isl_error_invalid
,
1308 "oversized transformation matrix", goto error
);
1310 pos
= isl_basic_map_offset(bmap
, type
) + first
;
1312 if (transform(ctx
, bmap
->eq
, bmap
->n_eq
, pos
, isl_mat_copy(trans
)) < 0)
1314 if (transform(ctx
, bmap
->ineq
, bmap
->n_ineq
, pos
,
1315 isl_mat_copy(trans
)) < 0)
1317 if (transform(ctx
, bmap
->div
, bmap
->n_div
, 1 + pos
,
1318 isl_mat_copy(trans
)) < 0)
1321 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1322 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1324 isl_mat_free(trans
);
1327 isl_mat_free(trans
);
1328 isl_basic_map_free(bmap
);
1332 /* Replace the variables x of type "type" starting at "first" in "bset"
1333 * by x' with x = M x' with M the matrix trans.
1334 * That is, replace the corresponding coefficients c by c M.
1336 * The transformation matrix should be a square matrix.
1338 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
1339 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
1340 __isl_take isl_mat
*trans
)
1342 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);
1345 void isl_mat_print_internal(__isl_keep isl_mat
*mat
, FILE *out
, int indent
)
1350 fprintf(out
, "%*snull mat\n", indent
, "");
1354 if (mat
->n_row
== 0)
1355 fprintf(out
, "%*s[]\n", indent
, "");
1357 for (i
= 0; i
< mat
->n_row
; ++i
) {
1359 fprintf(out
, "%*s[[", indent
, "");
1361 fprintf(out
, "%*s[", indent
+1, "");
1362 for (j
= 0; j
< mat
->n_col
; ++j
) {
1365 isl_int_print(out
, mat
->row
[i
][j
], 0);
1367 if (i
== mat
->n_row
-1)
1368 fprintf(out
, "]]\n");
1370 fprintf(out
, "]\n");
1374 void isl_mat_dump(__isl_keep isl_mat
*mat
)
1376 isl_mat_print_internal(mat
, stderr
, 0);
1379 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
1386 mat
= isl_mat_cow(mat
);
1390 if (col
!= mat
->n_col
-n
) {
1391 for (r
= 0; r
< mat
->n_row
; ++r
)
1392 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
1393 mat
->n_col
- col
- n
);
1399 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
1403 mat
= isl_mat_cow(mat
);
1407 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
1408 mat
->row
[r
] = mat
->row
[r
+n
];
1414 __isl_give isl_mat
*isl_mat_insert_cols(__isl_take isl_mat
*mat
,
1415 unsigned col
, unsigned n
)
1424 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
+ n
);
1428 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
, 0, 0, col
);
1429 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
,
1430 col
+ n
, col
, mat
->n_col
- col
);
1439 __isl_give isl_mat
*isl_mat_insert_zero_cols(__isl_take isl_mat
*mat
,
1440 unsigned first
, unsigned n
)
1446 mat
= isl_mat_insert_cols(mat
, first
, n
);
1450 for (i
= 0; i
< mat
->n_row
; ++i
)
1451 isl_seq_clr(mat
->row
[i
] + first
, n
);
1456 __isl_give isl_mat
*isl_mat_add_zero_cols(__isl_take isl_mat
*mat
, unsigned n
)
1461 return isl_mat_insert_zero_cols(mat
, mat
->n_col
, n
);
1464 __isl_give isl_mat
*isl_mat_insert_rows(__isl_take isl_mat
*mat
,
1465 unsigned row
, unsigned n
)
1474 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
+ n
, mat
->n_col
);
1478 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, row
, 0, 0, mat
->n_col
);
1479 isl_mat_sub_copy(mat
->ctx
, ext
->row
+ row
+ n
, mat
->row
+ row
,
1480 mat
->n_row
- row
, 0, 0, mat
->n_col
);
1489 __isl_give isl_mat
*isl_mat_add_rows(__isl_take isl_mat
*mat
, unsigned n
)
1494 return isl_mat_insert_rows(mat
, mat
->n_row
, n
);
1497 __isl_give isl_mat
*isl_mat_insert_zero_rows(__isl_take isl_mat
*mat
,
1498 unsigned row
, unsigned n
)
1502 mat
= isl_mat_insert_rows(mat
, row
, n
);
1506 for (i
= 0; i
< n
; ++i
)
1507 isl_seq_clr(mat
->row
[row
+ i
], mat
->n_col
);
1512 __isl_give isl_mat
*isl_mat_add_zero_rows(__isl_take isl_mat
*mat
, unsigned n
)
1517 return isl_mat_insert_zero_rows(mat
, mat
->n_row
, n
);
1520 void isl_mat_col_submul(struct isl_mat
*mat
,
1521 int dst_col
, isl_int f
, int src_col
)
1525 for (i
= 0; i
< mat
->n_row
; ++i
)
1526 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1529 void isl_mat_col_add(__isl_keep isl_mat
*mat
, int dst_col
, int src_col
)
1536 for (i
= 0; i
< mat
->n_row
; ++i
)
1537 isl_int_add(mat
->row
[i
][dst_col
],
1538 mat
->row
[i
][dst_col
], mat
->row
[i
][src_col
]);
1541 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1545 for (i
= 0; i
< mat
->n_row
; ++i
)
1546 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1549 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1550 * return the result.
1552 __isl_give isl_mat
*isl_mat_col_addmul(__isl_take isl_mat
*mat
, int dst_col
,
1553 isl_int f
, int src_col
)
1557 if (check_col(mat
, dst_col
) < 0 || check_col(mat
, src_col
) < 0)
1558 return isl_mat_free(mat
);
1560 for (i
= 0; i
< mat
->n_row
; ++i
) {
1561 if (isl_int_is_zero(mat
->row
[i
][src_col
]))
1563 mat
= isl_mat_cow(mat
);
1566 isl_int_addmul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1572 /* Negate column "col" of "mat" and return the result.
1574 __isl_give isl_mat
*isl_mat_col_neg(__isl_take isl_mat
*mat
, int col
)
1578 if (check_col(mat
, col
) < 0)
1579 return isl_mat_free(mat
);
1581 for (i
= 0; i
< mat
->n_row
; ++i
) {
1582 if (isl_int_is_zero(mat
->row
[i
][col
]))
1584 mat
= isl_mat_cow(mat
);
1587 isl_int_neg(mat
->row
[i
][col
], mat
->row
[i
][col
]);
1593 struct isl_mat
*isl_mat_unimodular_complete(struct isl_mat
*M
, int row
)
1596 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1601 isl_assert(M
->ctx
, M
->n_row
== M
->n_col
, goto error
);
1603 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1604 M
->n_row
= M
->n_col
;
1607 for (r
= 0; r
< row
; ++r
)
1608 isl_assert(M
->ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1609 for (r
= row
; r
< M
->n_row
; ++r
)
1610 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);
1621 __isl_give isl_mat
*isl_mat_concat(__isl_take isl_mat
*top
,
1622 __isl_take isl_mat
*bot
)
1624 struct isl_mat
*mat
;
1629 isl_assert(top
->ctx
, top
->n_col
== bot
->n_col
, goto error
);
1630 if (top
->n_row
== 0) {
1634 if (bot
->n_row
== 0) {
1639 mat
= isl_mat_alloc(top
->ctx
, top
->n_row
+ bot
->n_row
, top
->n_col
);
1642 isl_mat_sub_copy(mat
->ctx
, mat
->row
, top
->row
, top
->n_row
,
1644 isl_mat_sub_copy(mat
->ctx
, mat
->row
+ top
->n_row
, bot
->row
, bot
->n_row
,
1655 int isl_mat_is_equal(__isl_keep isl_mat
*mat1
, __isl_keep isl_mat
*mat2
)
1662 if (mat1
->n_row
!= mat2
->n_row
)
1665 if (mat1
->n_col
!= mat2
->n_col
)
1668 for (i
= 0; i
< mat1
->n_row
; ++i
)
1669 if (!isl_seq_eq(mat1
->row
[i
], mat2
->row
[i
], mat1
->n_col
))
1675 __isl_give isl_mat
*isl_mat_from_row_vec(__isl_take isl_vec
*vec
)
1677 struct isl_mat
*mat
;
1681 mat
= isl_mat_alloc(vec
->ctx
, 1, vec
->size
);
1685 isl_seq_cpy(mat
->row
[0], vec
->el
, vec
->size
);
1694 /* Return a copy of row "row" of "mat" as an isl_vec.
1696 __isl_give isl_vec
*isl_mat_get_row(__isl_keep isl_mat
*mat
, unsigned row
)
1702 if (row
>= mat
->n_row
)
1703 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
1706 v
= isl_vec_alloc(isl_mat_get_ctx(mat
), mat
->n_col
);
1709 isl_seq_cpy(v
->el
, mat
->row
[row
], mat
->n_col
);
1714 __isl_give isl_mat
*isl_mat_vec_concat(__isl_take isl_mat
*top
,
1715 __isl_take isl_vec
*bot
)
1717 return isl_mat_concat(top
, isl_mat_from_row_vec(bot
));
1720 __isl_give isl_mat
*isl_mat_move_cols(__isl_take isl_mat
*mat
,
1721 unsigned dst_col
, unsigned src_col
, unsigned n
)
1727 if (n
== 0 || dst_col
== src_col
)
1730 res
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
1734 if (dst_col
< src_col
) {
1735 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1737 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1738 dst_col
, src_col
, n
);
1739 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1740 dst_col
+ n
, dst_col
, src_col
- dst_col
);
1741 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1742 src_col
+ n
, src_col
+ n
,
1743 res
->n_col
- src_col
- n
);
1745 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1747 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1748 src_col
, src_col
+ n
, dst_col
- src_col
);
1749 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1750 dst_col
, src_col
, n
);
1751 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1752 dst_col
+ n
, dst_col
+ n
,
1753 res
->n_col
- dst_col
- n
);
1763 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1764 * Return isl_stat_ok on success and isl_stat_error on failure.
1766 isl_stat
isl_mat_row_gcd(__isl_keep isl_mat
*mat
, int row
, isl_int
*gcd
)
1769 return isl_stat_error
;
1771 if (row
< 0 || row
>= mat
->n_row
)
1772 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
1773 "row out of range", return isl_stat_error
);
1774 isl_seq_gcd(mat
->row
[row
], mat
->n_col
, gcd
);
1779 void isl_mat_gcd(__isl_keep isl_mat
*mat
, isl_int
*gcd
)
1784 isl_int_set_si(*gcd
, 0);
1789 for (i
= 0; i
< mat
->n_row
; ++i
) {
1790 isl_seq_gcd(mat
->row
[i
], mat
->n_col
, &g
);
1791 isl_int_gcd(*gcd
, *gcd
, g
);
1796 __isl_give isl_mat
*isl_mat_scale_down(__isl_take isl_mat
*mat
, isl_int m
)
1800 if (isl_int_is_one(m
))
1803 mat
= isl_mat_cow(mat
);
1807 for (i
= 0; i
< mat
->n_row
; ++i
)
1808 isl_seq_scale_down(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1813 __isl_give isl_mat
*isl_mat_scale_down_row(__isl_take isl_mat
*mat
, int row
,
1816 if (isl_int_is_one(m
))
1819 mat
= isl_mat_cow(mat
);
1823 isl_seq_scale_down(mat
->row
[row
], mat
->row
[row
], m
, mat
->n_col
);
1828 __isl_give isl_mat
*isl_mat_normalize(__isl_take isl_mat
*mat
)
1836 isl_mat_gcd(mat
, &gcd
);
1837 mat
= isl_mat_scale_down(mat
, gcd
);
1843 __isl_give isl_mat
*isl_mat_normalize_row(__isl_take isl_mat
*mat
, int row
)
1845 mat
= isl_mat_cow(mat
);
1849 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], mat
->n_col
);
1854 /* Number of initial non-zero columns.
1856 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat
*mat
)
1863 for (i
= 0; i
< mat
->n_col
; ++i
)
1864 if (row_first_non_zero(mat
->row
, mat
->n_row
, i
) < 0)