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 int isl_mat_get_element(__isl_keep isl_mat
*mat
, int row
, int col
, isl_int
*v
)
266 if (row
< 0 || row
>= mat
->n_row
)
267 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
269 if (col
< 0 || col
>= mat
->n_col
)
270 isl_die(mat
->ctx
, isl_error_invalid
, "column out of range",
272 isl_int_set(*v
, mat
->row
[row
][col
]);
276 /* Extract the element at row "row", oolumn "col" of "mat".
278 __isl_give isl_val
*isl_mat_get_element_val(__isl_keep isl_mat
*mat
,
285 ctx
= isl_mat_get_ctx(mat
);
286 if (row
< 0 || row
>= mat
->n_row
)
287 isl_die(ctx
, isl_error_invalid
, "row out of range",
289 if (col
< 0 || col
>= mat
->n_col
)
290 isl_die(ctx
, isl_error_invalid
, "column out of range",
292 return isl_val_int_from_isl_int(ctx
, mat
->row
[row
][col
]);
295 __isl_give isl_mat
*isl_mat_set_element(__isl_take isl_mat
*mat
,
296 int row
, int col
, isl_int v
)
298 mat
= isl_mat_cow(mat
);
301 if (row
< 0 || row
>= mat
->n_row
)
302 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
304 if (col
< 0 || col
>= mat
->n_col
)
305 isl_die(mat
->ctx
, isl_error_invalid
, "column out of range",
307 isl_int_set(mat
->row
[row
][col
], v
);
314 __isl_give isl_mat
*isl_mat_set_element_si(__isl_take isl_mat
*mat
,
315 int row
, int col
, int v
)
317 mat
= isl_mat_cow(mat
);
320 if (row
< 0 || row
>= mat
->n_row
)
321 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
323 if (col
< 0 || col
>= mat
->n_col
)
324 isl_die(mat
->ctx
, isl_error_invalid
, "column out of range",
326 isl_int_set_si(mat
->row
[row
][col
], v
);
333 /* Replace the element at row "row", column "col" of "mat" by "v".
335 __isl_give isl_mat
*isl_mat_set_element_val(__isl_take isl_mat
*mat
,
336 int row
, int col
, __isl_take isl_val
*v
)
339 return isl_mat_free(mat
);
340 if (!isl_val_is_int(v
))
341 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
342 "expecting integer value", goto error
);
343 mat
= isl_mat_set_element(mat
, row
, col
, v
->n
);
348 return isl_mat_free(mat
);
351 __isl_give isl_mat
*isl_mat_diag(isl_ctx
*ctx
, unsigned n_row
, isl_int d
)
356 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
359 for (i
= 0; i
< n_row
; ++i
) {
360 isl_seq_clr(mat
->row
[i
], i
);
361 isl_int_set(mat
->row
[i
][i
], d
);
362 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
368 __isl_give isl_mat
*isl_mat_identity(isl_ctx
*ctx
, unsigned n_row
)
372 return isl_mat_diag(ctx
, n_row
, ctx
->one
);
375 /* Is "mat" a (possibly scaled) identity matrix?
377 int isl_mat_is_scaled_identity(__isl_keep isl_mat
*mat
)
383 if (mat
->n_row
!= mat
->n_col
)
386 for (i
= 0; i
< mat
->n_row
; ++i
) {
387 if (isl_seq_first_non_zero(mat
->row
[i
], i
) != -1)
389 if (isl_int_ne(mat
->row
[0][0], mat
->row
[i
][i
]))
391 if (isl_seq_first_non_zero(mat
->row
[i
] + i
+ 1,
392 mat
->n_col
- (i
+ 1)) != -1)
399 struct isl_vec
*isl_mat_vec_product(struct isl_mat
*mat
, struct isl_vec
*vec
)
402 struct isl_vec
*prod
;
407 isl_assert(mat
->ctx
, mat
->n_col
== vec
->size
, goto error
);
409 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
413 for (i
= 0; i
< prod
->size
; ++i
)
414 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
415 &prod
->block
.data
[i
]);
425 __isl_give isl_vec
*isl_mat_vec_inverse_product(__isl_take isl_mat
*mat
,
426 __isl_take isl_vec
*vec
)
428 struct isl_mat
*vec_mat
;
433 vec_mat
= isl_mat_alloc(vec
->ctx
, vec
->size
, 1);
436 for (i
= 0; i
< vec
->size
; ++i
)
437 isl_int_set(vec_mat
->row
[i
][0], vec
->el
[i
]);
438 vec_mat
= isl_mat_inverse_product(mat
, vec_mat
);
442 vec
= isl_vec_alloc(vec_mat
->ctx
, vec_mat
->n_row
);
444 for (i
= 0; i
< vec
->size
; ++i
)
445 isl_int_set(vec
->el
[i
], vec_mat
->row
[i
][0]);
446 isl_mat_free(vec_mat
);
454 struct isl_vec
*isl_vec_mat_product(struct isl_vec
*vec
, struct isl_mat
*mat
)
457 struct isl_vec
*prod
;
462 isl_assert(mat
->ctx
, mat
->n_row
== vec
->size
, goto error
);
464 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_col
);
468 for (i
= 0; i
< prod
->size
; ++i
) {
469 isl_int_set_si(prod
->el
[i
], 0);
470 for (j
= 0; j
< vec
->size
; ++j
)
471 isl_int_addmul(prod
->el
[i
], vec
->el
[j
], mat
->row
[j
][i
]);
482 struct isl_mat
*isl_mat_aff_direct_sum(struct isl_mat
*left
,
483 struct isl_mat
*right
)
491 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
492 isl_assert(left
->ctx
, left
->n_row
>= 1, goto error
);
493 isl_assert(left
->ctx
, left
->n_col
>= 1, goto error
);
494 isl_assert(left
->ctx
, right
->n_col
>= 1, goto error
);
495 isl_assert(left
->ctx
,
496 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
498 isl_assert(left
->ctx
,
499 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
502 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
505 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
506 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
507 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
509 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
510 for (i
= 1; i
< sum
->n_row
; ++i
) {
511 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
512 isl_int_addmul(sum
->row
[i
][0],
513 right
->row
[0][0], right
->row
[i
][0]);
514 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
516 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
517 right
->row
[i
]+1, right
->row
[0][0],
521 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
522 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
532 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
533 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
536 for (r
= row
; r
< M
->n_row
; ++r
)
537 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
539 for (r
= 0; r
< (*U
)->n_row
; ++r
)
540 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
543 isl_mat_swap_rows(*Q
, i
, j
);
546 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
547 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
550 for (r
= row
; r
< M
->n_row
; ++r
)
551 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
553 for (r
= 0; r
< (*U
)->n_row
; ++r
)
554 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
557 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
558 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
562 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
563 struct isl_mat
**Q
, unsigned row
, unsigned col
)
566 for (r
= row
; r
< M
->n_row
; ++r
)
567 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
569 for (r
= 0; r
< (*U
)->n_row
; ++r
)
570 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
573 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
576 /* Given matrix M, compute
581 * with U and Q unimodular matrices and H a matrix in column echelon form
582 * such that on each echelon row the entries in the non-echelon column
583 * are non-negative (if neg == 0) or non-positive (if neg == 1)
584 * and strictly smaller (in absolute value) than the entries in the echelon
586 * If U or Q are NULL, then these matrices are not computed.
588 struct isl_mat
*isl_mat_left_hermite(struct isl_mat
*M
, int neg
,
589 struct isl_mat
**U
, struct isl_mat
**Q
)
604 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
609 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
616 for (row
= 0; row
< M
->n_row
; ++row
) {
618 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
623 exchange(M
, U
, Q
, row
, first
, col
);
624 if (isl_int_is_neg(M
->row
[row
][col
]))
625 oppose(M
, U
, Q
, row
, col
);
627 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
628 M
->n_col
-first
)) != -1) {
630 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
631 subtract(M
, U
, Q
, row
, col
, first
, c
);
632 if (!isl_int_is_zero(M
->row
[row
][first
]))
633 exchange(M
, U
, Q
, row
, first
, col
);
637 for (i
= 0; i
< col
; ++i
) {
638 if (isl_int_is_zero(M
->row
[row
][i
]))
641 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
643 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
644 if (isl_int_is_zero(c
))
646 subtract(M
, U
, Q
, row
, col
, i
, c
);
666 struct isl_mat
*isl_mat_right_kernel(struct isl_mat
*mat
)
669 struct isl_mat
*U
= NULL
;
672 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
676 for (i
= 0, rank
= 0; rank
< mat
->n_col
; ++rank
) {
677 while (i
< mat
->n_row
&& isl_int_is_zero(mat
->row
[i
][rank
]))
682 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
685 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
695 struct isl_mat
*isl_mat_lin_to_aff(struct isl_mat
*mat
)
698 struct isl_mat
*mat2
;
702 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
705 isl_int_set_si(mat2
->row
[0][0], 1);
706 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
707 for (i
= 0; i
< mat
->n_row
; ++i
) {
708 isl_int_set_si(mat2
->row
[1+i
][0], 0);
709 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
718 /* Given two matrices M1 and M2, return the block matrix
723 __isl_give isl_mat
*isl_mat_diagonal(__isl_take isl_mat
*mat1
,
724 __isl_take isl_mat
*mat2
)
732 mat
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
733 mat1
->n_col
+ mat2
->n_col
);
736 for (i
= 0; i
< mat1
->n_row
; ++i
) {
737 isl_seq_cpy(mat
->row
[i
], mat1
->row
[i
], mat1
->n_col
);
738 isl_seq_clr(mat
->row
[i
] + mat1
->n_col
, mat2
->n_col
);
740 for (i
= 0; i
< mat2
->n_row
; ++i
) {
741 isl_seq_clr(mat
->row
[mat1
->n_row
+ i
], mat1
->n_col
);
742 isl_seq_cpy(mat
->row
[mat1
->n_row
+ i
] + mat1
->n_col
,
743 mat2
->row
[i
], mat2
->n_col
);
754 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
758 for (i
= 0; i
< n_row
; ++i
)
759 if (!isl_int_is_zero(row
[i
][col
]))
764 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
766 int i
, min
= row_first_non_zero(row
, n_row
, col
);
769 for (i
= min
+ 1; i
< n_row
; ++i
) {
770 if (isl_int_is_zero(row
[i
][col
]))
772 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
778 static void inv_exchange(struct isl_mat
*left
, struct isl_mat
*right
,
779 unsigned i
, unsigned j
)
781 left
= isl_mat_swap_rows(left
, i
, j
);
782 right
= isl_mat_swap_rows(right
, i
, j
);
785 static void inv_oppose(
786 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
788 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
789 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
792 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
793 unsigned row
, unsigned i
, isl_int m
)
796 isl_seq_combine(left
->row
[i
]+row
,
797 left
->ctx
->one
, left
->row
[i
]+row
,
798 m
, left
->row
[row
]+row
,
800 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
801 m
, right
->row
[row
], right
->n_col
);
804 /* Compute inv(left)*right
806 struct isl_mat
*isl_mat_inverse_product(struct isl_mat
*left
,
807 struct isl_mat
*right
)
815 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
816 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
818 if (left
->n_row
== 0) {
823 left
= isl_mat_cow(left
);
824 right
= isl_mat_cow(right
);
830 for (row
= 0; row
< left
->n_row
; ++row
) {
831 int pivot
, first
, i
, off
;
832 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
836 isl_assert(left
->ctx
, pivot
>= 0, goto error
);
840 inv_exchange(left
, right
, pivot
, row
);
841 if (isl_int_is_neg(left
->row
[row
][row
]))
842 inv_oppose(left
, right
, row
);
844 while ((off
= row_first_non_zero(left
->row
+first
,
845 left
->n_row
-first
, row
)) != -1) {
847 isl_int_fdiv_q(a
, left
->row
[first
][row
],
848 left
->row
[row
][row
]);
849 inv_subtract(left
, right
, row
, first
, a
);
850 if (!isl_int_is_zero(left
->row
[first
][row
]))
851 inv_exchange(left
, right
, row
, first
);
855 for (i
= 0; i
< row
; ++i
) {
856 if (isl_int_is_zero(left
->row
[i
][row
]))
858 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
859 isl_int_divexact(b
, left
->row
[i
][row
], a
);
860 isl_int_divexact(a
, left
->row
[row
][row
], a
);
862 isl_seq_combine(left
->row
[i
] + i
,
864 b
, left
->row
[row
] + i
,
866 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
867 b
, right
->row
[row
], right
->n_col
);
872 isl_int_set(a
, left
->row
[0][0]);
873 for (row
= 1; row
< left
->n_row
; ++row
)
874 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
875 if (isl_int_is_zero(a
)){
877 isl_assert(left
->ctx
, 0, goto error
);
879 for (row
= 0; row
< left
->n_row
; ++row
) {
880 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
881 if (isl_int_is_one(left
->row
[row
][row
]))
883 isl_seq_scale(right
->row
[row
], right
->row
[row
],
884 left
->row
[row
][row
], right
->n_col
);
896 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
900 for (i
= 0; i
< mat
->n_row
; ++i
)
901 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
904 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
905 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
911 for (i
= 0; i
< mat
->n_row
; ++i
) {
912 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
913 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
914 isl_int_set(mat
->row
[i
][dst
], tmp
);
919 struct isl_mat
*isl_mat_right_inverse(struct isl_mat
*mat
)
925 mat
= isl_mat_cow(mat
);
929 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
930 inv
= isl_mat_cow(inv
);
936 for (row
= 0; row
< mat
->n_row
; ++row
) {
937 int pivot
, first
, i
, off
;
938 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
942 isl_assert(mat
->ctx
, pivot
>= 0, goto error
);
946 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
947 if (isl_int_is_neg(mat
->row
[row
][row
]))
948 oppose(mat
, &inv
, NULL
, row
, row
);
950 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
951 mat
->n_col
-first
)) != -1) {
953 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
955 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
956 if (!isl_int_is_zero(mat
->row
[row
][first
]))
957 exchange(mat
, &inv
, NULL
, row
, row
, first
);
961 for (i
= 0; i
< row
; ++i
) {
962 if (isl_int_is_zero(mat
->row
[row
][i
]))
964 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
965 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
966 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
968 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
969 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
974 isl_int_set(a
, mat
->row
[0][0]);
975 for (row
= 1; row
< mat
->n_row
; ++row
)
976 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
977 if (isl_int_is_zero(a
)){
981 for (row
= 0; row
< mat
->n_row
; ++row
) {
982 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
983 if (isl_int_is_one(mat
->row
[row
][row
]))
985 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
998 struct isl_mat
*isl_mat_transpose(struct isl_mat
*mat
)
1000 struct isl_mat
*transpose
= NULL
;
1006 if (mat
->n_col
== mat
->n_row
) {
1007 mat
= isl_mat_cow(mat
);
1010 for (i
= 0; i
< mat
->n_row
; ++i
)
1011 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
1012 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
1015 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
1018 for (i
= 0; i
< mat
->n_row
; ++i
)
1019 for (j
= 0; j
< mat
->n_col
; ++j
)
1020 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
1028 struct isl_mat
*isl_mat_swap_cols(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1032 mat
= isl_mat_cow(mat
);
1035 isl_assert(mat
->ctx
, i
< mat
->n_col
, goto error
);
1036 isl_assert(mat
->ctx
, j
< mat
->n_col
, goto error
);
1038 for (r
= 0; r
< mat
->n_row
; ++r
)
1039 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
1046 struct isl_mat
*isl_mat_swap_rows(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1052 mat
= isl_mat_cow(mat
);
1056 mat
->row
[i
] = mat
->row
[j
];
1061 /* Calculate the product of two matrices.
1063 * This function is optimized for operand matrices that contain many zeros and
1064 * skips multiplications where we know one of the operands is zero.
1066 __isl_give isl_mat
*isl_mat_product(__isl_take isl_mat
*left
,
1067 __isl_take isl_mat
*right
)
1070 struct isl_mat
*prod
;
1072 if (!left
|| !right
)
1074 isl_assert(left
->ctx
, left
->n_col
== right
->n_row
, goto error
);
1075 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
1078 if (left
->n_col
== 0) {
1079 for (i
= 0; i
< prod
->n_row
; ++i
)
1080 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
1082 isl_mat_free(right
);
1085 for (i
= 0; i
< prod
->n_row
; ++i
) {
1086 for (j
= 0; j
< prod
->n_col
; ++j
)
1087 isl_int_mul(prod
->row
[i
][j
],
1088 left
->row
[i
][0], right
->row
[0][j
]);
1089 for (k
= 1; k
< left
->n_col
; ++k
) {
1090 if (isl_int_is_zero(left
->row
[i
][k
]))
1092 for (j
= 0; j
< prod
->n_col
; ++j
)
1093 isl_int_addmul(prod
->row
[i
][j
],
1094 left
->row
[i
][k
], right
->row
[k
][j
]);
1098 isl_mat_free(right
);
1102 isl_mat_free(right
);
1106 /* Replace the variables x in the rows q by x' given by x = M x',
1107 * with M the matrix mat.
1109 * If the number of new variables is greater than the original
1110 * number of variables, then the rows q have already been
1111 * preextended. If the new number is smaller, then the coefficients
1112 * of the divs, which are not changed, need to be shifted down.
1113 * The row q may be the equalities, the inequalities or the
1114 * div expressions. In the latter case, has_div is true and
1115 * we need to take into account the extra denominator column.
1117 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1118 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
1124 if (mat
->n_col
>= mat
->n_row
)
1127 e
= mat
->n_row
- mat
->n_col
;
1129 for (i
= 0; i
< n
; ++i
)
1130 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
1131 t
= isl_mat_sub_alloc6(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
1132 t
= isl_mat_product(t
, mat
);
1135 for (i
= 0; i
< n
; ++i
) {
1136 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
1137 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
1138 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
1139 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
1145 /* Replace the variables x in bset by x' given by x = M x', with
1148 * If there are fewer variables x' then there are x, then we perform
1149 * the transformation in place, which means that, in principle,
1150 * this frees up some extra variables as the number
1151 * of columns remains constant, but we would have to extend
1152 * the div array too as the number of rows in this array is assumed
1153 * to be equal to extra.
1155 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
1156 struct isl_mat
*mat
)
1158 struct isl_ctx
*ctx
;
1164 bset
= isl_basic_set_cow(bset
);
1168 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
1169 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
1170 isl_assert(ctx
, mat
->n_col
> 0, goto error
);
1172 if (mat
->n_col
> mat
->n_row
) {
1173 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0, 0, 0);
1176 } else if (mat
->n_col
< mat
->n_row
) {
1177 bset
->dim
= isl_space_cow(bset
->dim
);
1180 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
1183 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
1184 isl_mat_copy(mat
)) < 0)
1187 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
1188 isl_mat_copy(mat
)) < 0)
1191 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
1194 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1195 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1196 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1197 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
1198 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
1200 bset
= isl_basic_set_simplify(bset
);
1201 bset
= isl_basic_set_finalize(bset
);
1207 isl_basic_set_free(bset
);
1211 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
1215 set
= isl_set_cow(set
);
1219 for (i
= 0; i
< set
->n
; ++i
) {
1220 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
1225 if (mat
->n_col
!= mat
->n_row
) {
1226 set
->dim
= isl_space_cow(set
->dim
);
1229 set
->dim
->n_out
+= mat
->n_col
;
1230 set
->dim
->n_out
-= mat
->n_row
;
1233 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1241 /* Replace the variables x starting at pos in the rows q
1242 * by x' with x = M x' with M the matrix mat.
1243 * That is, replace the corresponding coefficients c by c M.
1245 static int transform(isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1246 unsigned pos
, __isl_take isl_mat
*mat
)
1251 t
= isl_mat_sub_alloc6(ctx
, q
, 0, n
, pos
, mat
->n_row
);
1252 t
= isl_mat_product(t
, mat
);
1255 for (i
= 0; i
< n
; ++i
)
1256 isl_seq_swp_or_cpy(q
[i
] + pos
, t
->row
[i
], t
->n_col
);
1261 /* Replace the variables x of type "type" starting at "first" in "bmap"
1262 * by x' with x = M x' with M the matrix trans.
1263 * That is, replace the corresponding coefficients c by c M.
1265 * The transformation matrix should be a square matrix.
1267 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
1268 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
1269 __isl_take isl_mat
*trans
)
1274 bmap
= isl_basic_map_cow(bmap
);
1275 if (!bmap
|| !trans
)
1278 ctx
= isl_basic_map_get_ctx(bmap
);
1279 if (trans
->n_row
!= trans
->n_col
)
1280 isl_die(trans
->ctx
, isl_error_invalid
,
1281 "expecting square transformation matrix", goto error
);
1282 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
1283 isl_die(trans
->ctx
, isl_error_invalid
,
1284 "oversized transformation matrix", goto error
);
1286 pos
= isl_basic_map_offset(bmap
, type
) + first
;
1288 if (transform(ctx
, bmap
->eq
, bmap
->n_eq
, pos
, isl_mat_copy(trans
)) < 0)
1290 if (transform(ctx
, bmap
->ineq
, bmap
->n_ineq
, pos
,
1291 isl_mat_copy(trans
)) < 0)
1293 if (transform(ctx
, bmap
->div
, bmap
->n_div
, 1 + pos
,
1294 isl_mat_copy(trans
)) < 0)
1297 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1298 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1300 isl_mat_free(trans
);
1303 isl_mat_free(trans
);
1304 isl_basic_map_free(bmap
);
1308 /* Replace the variables x of type "type" starting at "first" in "bset"
1309 * by x' with x = M x' with M the matrix trans.
1310 * That is, replace the corresponding coefficients c by c M.
1312 * The transformation matrix should be a square matrix.
1314 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
1315 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
1316 __isl_take isl_mat
*trans
)
1318 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);
1321 void isl_mat_print_internal(__isl_keep isl_mat
*mat
, FILE *out
, int indent
)
1326 fprintf(out
, "%*snull mat\n", indent
, "");
1330 if (mat
->n_row
== 0)
1331 fprintf(out
, "%*s[]\n", indent
, "");
1333 for (i
= 0; i
< mat
->n_row
; ++i
) {
1335 fprintf(out
, "%*s[[", indent
, "");
1337 fprintf(out
, "%*s[", indent
+1, "");
1338 for (j
= 0; j
< mat
->n_col
; ++j
) {
1341 isl_int_print(out
, mat
->row
[i
][j
], 0);
1343 if (i
== mat
->n_row
-1)
1344 fprintf(out
, "]]\n");
1346 fprintf(out
, "]\n");
1350 void isl_mat_dump(__isl_keep isl_mat
*mat
)
1352 isl_mat_print_internal(mat
, stderr
, 0);
1355 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
1362 mat
= isl_mat_cow(mat
);
1366 if (col
!= mat
->n_col
-n
) {
1367 for (r
= 0; r
< mat
->n_row
; ++r
)
1368 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
1369 mat
->n_col
- col
- n
);
1375 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
1379 mat
= isl_mat_cow(mat
);
1383 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
1384 mat
->row
[r
] = mat
->row
[r
+n
];
1390 __isl_give isl_mat
*isl_mat_insert_cols(__isl_take isl_mat
*mat
,
1391 unsigned col
, unsigned n
)
1400 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
+ n
);
1404 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
, 0, 0, col
);
1405 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
,
1406 col
+ n
, col
, mat
->n_col
- col
);
1415 __isl_give isl_mat
*isl_mat_insert_zero_cols(__isl_take isl_mat
*mat
,
1416 unsigned first
, unsigned n
)
1422 mat
= isl_mat_insert_cols(mat
, first
, n
);
1426 for (i
= 0; i
< mat
->n_row
; ++i
)
1427 isl_seq_clr(mat
->row
[i
] + first
, n
);
1432 __isl_give isl_mat
*isl_mat_add_zero_cols(__isl_take isl_mat
*mat
, unsigned n
)
1437 return isl_mat_insert_zero_cols(mat
, mat
->n_col
, n
);
1440 __isl_give isl_mat
*isl_mat_insert_rows(__isl_take isl_mat
*mat
,
1441 unsigned row
, unsigned n
)
1450 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
+ n
, mat
->n_col
);
1454 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, row
, 0, 0, mat
->n_col
);
1455 isl_mat_sub_copy(mat
->ctx
, ext
->row
+ row
+ n
, mat
->row
+ row
,
1456 mat
->n_row
- row
, 0, 0, mat
->n_col
);
1465 __isl_give isl_mat
*isl_mat_add_rows(__isl_take isl_mat
*mat
, unsigned n
)
1470 return isl_mat_insert_rows(mat
, mat
->n_row
, n
);
1473 __isl_give isl_mat
*isl_mat_insert_zero_rows(__isl_take isl_mat
*mat
,
1474 unsigned row
, unsigned n
)
1478 mat
= isl_mat_insert_rows(mat
, row
, n
);
1482 for (i
= 0; i
< n
; ++i
)
1483 isl_seq_clr(mat
->row
[row
+ i
], mat
->n_col
);
1488 __isl_give isl_mat
*isl_mat_add_zero_rows(__isl_take isl_mat
*mat
, unsigned n
)
1493 return isl_mat_insert_zero_rows(mat
, mat
->n_row
, n
);
1496 void isl_mat_col_submul(struct isl_mat
*mat
,
1497 int dst_col
, isl_int f
, int src_col
)
1501 for (i
= 0; i
< mat
->n_row
; ++i
)
1502 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1505 void isl_mat_col_add(__isl_keep isl_mat
*mat
, int dst_col
, int src_col
)
1512 for (i
= 0; i
< mat
->n_row
; ++i
)
1513 isl_int_add(mat
->row
[i
][dst_col
],
1514 mat
->row
[i
][dst_col
], mat
->row
[i
][src_col
]);
1517 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1521 for (i
= 0; i
< mat
->n_row
; ++i
)
1522 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1525 struct isl_mat
*isl_mat_unimodular_complete(struct isl_mat
*M
, int row
)
1528 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1533 isl_assert(M
->ctx
, M
->n_row
== M
->n_col
, goto error
);
1535 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1536 M
->n_row
= M
->n_col
;
1539 for (r
= 0; r
< row
; ++r
)
1540 isl_assert(M
->ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1541 for (r
= row
; r
< M
->n_row
; ++r
)
1542 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);
1553 __isl_give isl_mat
*isl_mat_concat(__isl_take isl_mat
*top
,
1554 __isl_take isl_mat
*bot
)
1556 struct isl_mat
*mat
;
1561 isl_assert(top
->ctx
, top
->n_col
== bot
->n_col
, goto error
);
1562 if (top
->n_row
== 0) {
1566 if (bot
->n_row
== 0) {
1571 mat
= isl_mat_alloc(top
->ctx
, top
->n_row
+ bot
->n_row
, top
->n_col
);
1574 isl_mat_sub_copy(mat
->ctx
, mat
->row
, top
->row
, top
->n_row
,
1576 isl_mat_sub_copy(mat
->ctx
, mat
->row
+ top
->n_row
, bot
->row
, bot
->n_row
,
1587 int isl_mat_is_equal(__isl_keep isl_mat
*mat1
, __isl_keep isl_mat
*mat2
)
1594 if (mat1
->n_row
!= mat2
->n_row
)
1597 if (mat1
->n_col
!= mat2
->n_col
)
1600 for (i
= 0; i
< mat1
->n_row
; ++i
)
1601 if (!isl_seq_eq(mat1
->row
[i
], mat2
->row
[i
], mat1
->n_col
))
1607 __isl_give isl_mat
*isl_mat_from_row_vec(__isl_take isl_vec
*vec
)
1609 struct isl_mat
*mat
;
1613 mat
= isl_mat_alloc(vec
->ctx
, 1, vec
->size
);
1617 isl_seq_cpy(mat
->row
[0], vec
->el
, vec
->size
);
1626 /* Return a copy of row "row" of "mat" as an isl_vec.
1628 __isl_give isl_vec
*isl_mat_get_row(__isl_keep isl_mat
*mat
, unsigned row
)
1634 if (row
>= mat
->n_row
)
1635 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
1638 v
= isl_vec_alloc(isl_mat_get_ctx(mat
), mat
->n_col
);
1641 isl_seq_cpy(v
->el
, mat
->row
[row
], mat
->n_col
);
1646 __isl_give isl_mat
*isl_mat_vec_concat(__isl_take isl_mat
*top
,
1647 __isl_take isl_vec
*bot
)
1649 return isl_mat_concat(top
, isl_mat_from_row_vec(bot
));
1652 __isl_give isl_mat
*isl_mat_move_cols(__isl_take isl_mat
*mat
,
1653 unsigned dst_col
, unsigned src_col
, unsigned n
)
1659 if (n
== 0 || dst_col
== src_col
)
1662 res
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
1666 if (dst_col
< src_col
) {
1667 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1669 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1670 dst_col
, src_col
, n
);
1671 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1672 dst_col
+ n
, dst_col
, src_col
- dst_col
);
1673 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1674 src_col
+ n
, src_col
+ n
,
1675 res
->n_col
- src_col
- n
);
1677 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1679 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1680 src_col
, src_col
+ n
, dst_col
- src_col
);
1681 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1682 dst_col
, src_col
, n
);
1683 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1684 dst_col
+ n
, dst_col
+ n
,
1685 res
->n_col
- dst_col
- n
);
1695 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1696 * Return isl_stat_ok on success and isl_stat_error on failure.
1698 isl_stat
isl_mat_row_gcd(__isl_keep isl_mat
*mat
, int row
, isl_int
*gcd
)
1701 return isl_stat_error
;
1703 if (row
< 0 || row
>= mat
->n_row
)
1704 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
1705 "row out of range", return isl_stat_error
);
1706 isl_seq_gcd(mat
->row
[row
], mat
->n_col
, gcd
);
1711 void isl_mat_gcd(__isl_keep isl_mat
*mat
, isl_int
*gcd
)
1716 isl_int_set_si(*gcd
, 0);
1721 for (i
= 0; i
< mat
->n_row
; ++i
) {
1722 isl_seq_gcd(mat
->row
[i
], mat
->n_col
, &g
);
1723 isl_int_gcd(*gcd
, *gcd
, g
);
1728 /* Return the result of scaling "mat" by a factor of "m".
1730 __isl_give isl_mat
*isl_mat_scale(__isl_take isl_mat
*mat
, isl_int m
)
1734 if (isl_int_is_one(m
))
1737 mat
= isl_mat_cow(mat
);
1741 for (i
= 0; i
< mat
->n_row
; ++i
)
1742 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1747 __isl_give isl_mat
*isl_mat_scale_down(__isl_take isl_mat
*mat
, isl_int m
)
1751 if (isl_int_is_one(m
))
1754 mat
= isl_mat_cow(mat
);
1758 for (i
= 0; i
< mat
->n_row
; ++i
)
1759 isl_seq_scale_down(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1764 __isl_give isl_mat
*isl_mat_scale_down_row(__isl_take isl_mat
*mat
, int row
,
1767 if (isl_int_is_one(m
))
1770 mat
= isl_mat_cow(mat
);
1774 isl_seq_scale_down(mat
->row
[row
], mat
->row
[row
], m
, mat
->n_col
);
1779 __isl_give isl_mat
*isl_mat_normalize(__isl_take isl_mat
*mat
)
1787 isl_mat_gcd(mat
, &gcd
);
1788 mat
= isl_mat_scale_down(mat
, gcd
);
1794 __isl_give isl_mat
*isl_mat_normalize_row(__isl_take isl_mat
*mat
, int row
)
1796 mat
= isl_mat_cow(mat
);
1800 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], mat
->n_col
);
1805 /* Number of initial non-zero columns.
1807 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat
*mat
)
1814 for (i
= 0; i
< mat
->n_col
; ++i
)
1815 if (row_first_non_zero(mat
->row
, mat
->n_row
, i
) < 0)