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 /* Check that there are "n" columns starting at position "first" in "mat".
276 static isl_stat
check_col_range(__isl_keep isl_mat
*mat
, unsigned first
,
280 return isl_stat_error
;
281 if (first
+ n
> mat
->n_col
|| first
+ n
< first
)
282 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
283 "column position or range out of bounds",
284 return isl_stat_error
);
288 /* Check that there are "n" rows starting at position "first" in "mat".
290 static isl_stat
check_row_range(__isl_keep isl_mat
*mat
, unsigned first
,
294 return isl_stat_error
;
295 if (first
+ n
> mat
->n_row
|| first
+ n
< first
)
296 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
297 "row position or range out of bounds",
298 return isl_stat_error
);
302 int isl_mat_get_element(__isl_keep isl_mat
*mat
, int row
, int col
, isl_int
*v
)
306 if (row
< 0 || row
>= mat
->n_row
)
307 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
309 if (check_col(mat
, col
) < 0)
311 isl_int_set(*v
, mat
->row
[row
][col
]);
315 /* Extract the element at row "row", oolumn "col" of "mat".
317 __isl_give isl_val
*isl_mat_get_element_val(__isl_keep isl_mat
*mat
,
324 ctx
= isl_mat_get_ctx(mat
);
325 if (row
< 0 || row
>= mat
->n_row
)
326 isl_die(ctx
, isl_error_invalid
, "row out of range",
328 if (check_col(mat
, col
) < 0)
330 return isl_val_int_from_isl_int(ctx
, mat
->row
[row
][col
]);
333 __isl_give isl_mat
*isl_mat_set_element(__isl_take isl_mat
*mat
,
334 int row
, int col
, isl_int v
)
336 mat
= isl_mat_cow(mat
);
339 if (row
< 0 || row
>= mat
->n_row
)
340 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
342 if (check_col(mat
, col
) < 0)
343 return isl_mat_free(mat
);
344 isl_int_set(mat
->row
[row
][col
], v
);
351 __isl_give isl_mat
*isl_mat_set_element_si(__isl_take isl_mat
*mat
,
352 int row
, int col
, int v
)
354 mat
= isl_mat_cow(mat
);
357 if (row
< 0 || row
>= mat
->n_row
)
358 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
360 if (check_col(mat
, col
) < 0)
361 return isl_mat_free(mat
);
362 isl_int_set_si(mat
->row
[row
][col
], v
);
369 /* Replace the element at row "row", column "col" of "mat" by "v".
371 __isl_give isl_mat
*isl_mat_set_element_val(__isl_take isl_mat
*mat
,
372 int row
, int col
, __isl_take isl_val
*v
)
375 return isl_mat_free(mat
);
376 if (!isl_val_is_int(v
))
377 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
378 "expecting integer value", goto error
);
379 mat
= isl_mat_set_element(mat
, row
, col
, v
->n
);
384 return isl_mat_free(mat
);
387 __isl_give isl_mat
*isl_mat_diag(isl_ctx
*ctx
, unsigned n_row
, isl_int d
)
392 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
395 for (i
= 0; i
< n_row
; ++i
) {
396 isl_seq_clr(mat
->row
[i
], i
);
397 isl_int_set(mat
->row
[i
][i
], d
);
398 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
404 /* Create an "n_row" by "n_col" matrix with zero elements.
406 __isl_give isl_mat
*isl_mat_zero(isl_ctx
*ctx
, unsigned n_row
, unsigned n_col
)
411 mat
= isl_mat_alloc(ctx
, n_row
, n_col
);
414 for (i
= 0; i
< n_row
; ++i
)
415 isl_seq_clr(mat
->row
[i
], n_col
);
420 __isl_give isl_mat
*isl_mat_identity(isl_ctx
*ctx
, unsigned n_row
)
424 return isl_mat_diag(ctx
, n_row
, ctx
->one
);
427 /* Is "mat" a (possibly scaled) identity matrix?
429 int isl_mat_is_scaled_identity(__isl_keep isl_mat
*mat
)
435 if (mat
->n_row
!= mat
->n_col
)
438 for (i
= 0; i
< mat
->n_row
; ++i
) {
439 if (isl_seq_first_non_zero(mat
->row
[i
], i
) != -1)
441 if (isl_int_ne(mat
->row
[0][0], mat
->row
[i
][i
]))
443 if (isl_seq_first_non_zero(mat
->row
[i
] + i
+ 1,
444 mat
->n_col
- (i
+ 1)) != -1)
451 struct isl_vec
*isl_mat_vec_product(struct isl_mat
*mat
, struct isl_vec
*vec
)
454 struct isl_vec
*prod
;
459 isl_assert(mat
->ctx
, mat
->n_col
== vec
->size
, goto error
);
461 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
465 for (i
= 0; i
< prod
->size
; ++i
)
466 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
467 &prod
->block
.data
[i
]);
477 __isl_give isl_vec
*isl_mat_vec_inverse_product(__isl_take isl_mat
*mat
,
478 __isl_take isl_vec
*vec
)
480 struct isl_mat
*vec_mat
;
485 vec_mat
= isl_mat_alloc(vec
->ctx
, vec
->size
, 1);
488 for (i
= 0; i
< vec
->size
; ++i
)
489 isl_int_set(vec_mat
->row
[i
][0], vec
->el
[i
]);
490 vec_mat
= isl_mat_inverse_product(mat
, vec_mat
);
494 vec
= isl_vec_alloc(vec_mat
->ctx
, vec_mat
->n_row
);
496 for (i
= 0; i
< vec
->size
; ++i
)
497 isl_int_set(vec
->el
[i
], vec_mat
->row
[i
][0]);
498 isl_mat_free(vec_mat
);
506 struct isl_vec
*isl_vec_mat_product(struct isl_vec
*vec
, struct isl_mat
*mat
)
509 struct isl_vec
*prod
;
514 isl_assert(mat
->ctx
, mat
->n_row
== vec
->size
, goto error
);
516 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_col
);
520 for (i
= 0; i
< prod
->size
; ++i
) {
521 isl_int_set_si(prod
->el
[i
], 0);
522 for (j
= 0; j
< vec
->size
; ++j
)
523 isl_int_addmul(prod
->el
[i
], vec
->el
[j
], mat
->row
[j
][i
]);
534 struct isl_mat
*isl_mat_aff_direct_sum(struct isl_mat
*left
,
535 struct isl_mat
*right
)
543 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
544 isl_assert(left
->ctx
, left
->n_row
>= 1, goto error
);
545 isl_assert(left
->ctx
, left
->n_col
>= 1, goto error
);
546 isl_assert(left
->ctx
, right
->n_col
>= 1, goto error
);
547 isl_assert(left
->ctx
,
548 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
550 isl_assert(left
->ctx
,
551 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
554 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
557 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
558 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
559 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
561 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
562 for (i
= 1; i
< sum
->n_row
; ++i
) {
563 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
564 isl_int_addmul(sum
->row
[i
][0],
565 right
->row
[0][0], right
->row
[i
][0]);
566 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
568 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
569 right
->row
[i
]+1, right
->row
[0][0],
573 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
574 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
584 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
585 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
588 for (r
= row
; r
< M
->n_row
; ++r
)
589 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
591 for (r
= 0; r
< (*U
)->n_row
; ++r
)
592 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
595 isl_mat_swap_rows(*Q
, i
, j
);
598 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
599 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
602 for (r
= row
; r
< M
->n_row
; ++r
)
603 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
605 for (r
= 0; r
< (*U
)->n_row
; ++r
)
606 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
609 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
610 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
614 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
615 struct isl_mat
**Q
, unsigned row
, unsigned col
)
618 for (r
= row
; r
< M
->n_row
; ++r
)
619 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
621 for (r
= 0; r
< (*U
)->n_row
; ++r
)
622 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
625 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
628 /* Given matrix M, compute
633 * with U and Q unimodular matrices and H a matrix in column echelon form
634 * such that on each echelon row the entries in the non-echelon column
635 * are non-negative (if neg == 0) or non-positive (if neg == 1)
636 * and strictly smaller (in absolute value) than the entries in the echelon
638 * If U or Q are NULL, then these matrices are not computed.
640 struct isl_mat
*isl_mat_left_hermite(struct isl_mat
*M
, int neg
,
641 struct isl_mat
**U
, struct isl_mat
**Q
)
656 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
661 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
668 for (row
= 0; row
< M
->n_row
; ++row
) {
670 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
675 exchange(M
, U
, Q
, row
, first
, col
);
676 if (isl_int_is_neg(M
->row
[row
][col
]))
677 oppose(M
, U
, Q
, row
, col
);
679 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
680 M
->n_col
-first
)) != -1) {
682 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
683 subtract(M
, U
, Q
, row
, col
, first
, c
);
684 if (!isl_int_is_zero(M
->row
[row
][first
]))
685 exchange(M
, U
, Q
, row
, first
, col
);
689 for (i
= 0; i
< col
; ++i
) {
690 if (isl_int_is_zero(M
->row
[row
][i
]))
693 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
695 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
696 if (isl_int_is_zero(c
))
698 subtract(M
, U
, Q
, row
, col
, i
, c
);
718 struct isl_mat
*isl_mat_right_kernel(struct isl_mat
*mat
)
721 struct isl_mat
*U
= NULL
;
724 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
728 for (i
= 0, rank
= 0; rank
< mat
->n_col
; ++rank
) {
729 while (i
< mat
->n_row
&& isl_int_is_zero(mat
->row
[i
][rank
]))
734 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
737 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
747 struct isl_mat
*isl_mat_lin_to_aff(struct isl_mat
*mat
)
750 struct isl_mat
*mat2
;
754 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
757 isl_int_set_si(mat2
->row
[0][0], 1);
758 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
759 for (i
= 0; i
< mat
->n_row
; ++i
) {
760 isl_int_set_si(mat2
->row
[1+i
][0], 0);
761 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
770 /* Given two matrices M1 and M2, return the block matrix
775 __isl_give isl_mat
*isl_mat_diagonal(__isl_take isl_mat
*mat1
,
776 __isl_take isl_mat
*mat2
)
784 mat
= isl_mat_alloc(mat1
->ctx
, mat1
->n_row
+ mat2
->n_row
,
785 mat1
->n_col
+ mat2
->n_col
);
788 for (i
= 0; i
< mat1
->n_row
; ++i
) {
789 isl_seq_cpy(mat
->row
[i
], mat1
->row
[i
], mat1
->n_col
);
790 isl_seq_clr(mat
->row
[i
] + mat1
->n_col
, mat2
->n_col
);
792 for (i
= 0; i
< mat2
->n_row
; ++i
) {
793 isl_seq_clr(mat
->row
[mat1
->n_row
+ i
], mat1
->n_col
);
794 isl_seq_cpy(mat
->row
[mat1
->n_row
+ i
] + mat1
->n_col
,
795 mat2
->row
[i
], mat2
->n_col
);
806 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
810 for (i
= 0; i
< n_row
; ++i
)
811 if (!isl_int_is_zero(row
[i
][col
]))
816 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
818 int i
, min
= row_first_non_zero(row
, n_row
, col
);
821 for (i
= min
+ 1; i
< n_row
; ++i
) {
822 if (isl_int_is_zero(row
[i
][col
]))
824 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
830 static isl_stat
inv_exchange(__isl_keep isl_mat
**left
,
831 __isl_keep isl_mat
**right
, unsigned i
, unsigned j
)
833 *left
= isl_mat_swap_rows(*left
, i
, j
);
834 *right
= isl_mat_swap_rows(*right
, i
, j
);
836 if (!*left
|| !*right
)
837 return isl_stat_error
;
841 static void inv_oppose(
842 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
844 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
845 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
848 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
849 unsigned row
, unsigned i
, isl_int m
)
852 isl_seq_combine(left
->row
[i
]+row
,
853 left
->ctx
->one
, left
->row
[i
]+row
,
854 m
, left
->row
[row
]+row
,
856 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
857 m
, right
->row
[row
], right
->n_col
);
860 /* Compute inv(left)*right
862 struct isl_mat
*isl_mat_inverse_product(struct isl_mat
*left
,
863 struct isl_mat
*right
)
871 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
872 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
874 if (left
->n_row
== 0) {
879 left
= isl_mat_cow(left
);
880 right
= isl_mat_cow(right
);
886 for (row
= 0; row
< left
->n_row
; ++row
) {
887 int pivot
, first
, i
, off
;
888 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
892 isl_assert(left
->ctx
, pivot
>= 0, goto error
);
896 if (inv_exchange(&left
, &right
, pivot
, row
) < 0)
898 if (isl_int_is_neg(left
->row
[row
][row
]))
899 inv_oppose(left
, right
, row
);
901 while ((off
= row_first_non_zero(left
->row
+first
,
902 left
->n_row
-first
, row
)) != -1) {
904 isl_int_fdiv_q(a
, left
->row
[first
][row
],
905 left
->row
[row
][row
]);
906 inv_subtract(left
, right
, row
, first
, a
);
907 if (!isl_int_is_zero(left
->row
[first
][row
])) {
908 if (inv_exchange(&left
, &right
, row
, first
) < 0)
914 for (i
= 0; i
< row
; ++i
) {
915 if (isl_int_is_zero(left
->row
[i
][row
]))
917 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
918 isl_int_divexact(b
, left
->row
[i
][row
], a
);
919 isl_int_divexact(a
, left
->row
[row
][row
], a
);
921 isl_seq_combine(left
->row
[i
] + i
,
923 b
, left
->row
[row
] + i
,
925 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
926 b
, right
->row
[row
], right
->n_col
);
931 isl_int_set(a
, left
->row
[0][0]);
932 for (row
= 1; row
< left
->n_row
; ++row
)
933 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
934 if (isl_int_is_zero(a
)){
936 isl_assert(left
->ctx
, 0, goto error
);
938 for (row
= 0; row
< left
->n_row
; ++row
) {
939 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
940 if (isl_int_is_one(left
->row
[row
][row
]))
942 isl_seq_scale(right
->row
[row
], right
->row
[row
],
943 left
->row
[row
][row
], right
->n_col
);
955 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
959 for (i
= 0; i
< mat
->n_row
; ++i
)
960 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
963 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
964 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
970 for (i
= 0; i
< mat
->n_row
; ++i
) {
971 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
972 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
973 isl_int_set(mat
->row
[i
][dst
], tmp
);
978 struct isl_mat
*isl_mat_right_inverse(struct isl_mat
*mat
)
984 mat
= isl_mat_cow(mat
);
988 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
989 inv
= isl_mat_cow(inv
);
995 for (row
= 0; row
< mat
->n_row
; ++row
) {
996 int pivot
, first
, i
, off
;
997 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
1001 isl_assert(mat
->ctx
, pivot
>= 0, goto error
);
1005 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
1006 if (isl_int_is_neg(mat
->row
[row
][row
]))
1007 oppose(mat
, &inv
, NULL
, row
, row
);
1009 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
1010 mat
->n_col
-first
)) != -1) {
1012 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
1013 mat
->row
[row
][row
]);
1014 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
1015 if (!isl_int_is_zero(mat
->row
[row
][first
]))
1016 exchange(mat
, &inv
, NULL
, row
, row
, first
);
1020 for (i
= 0; i
< row
; ++i
) {
1021 if (isl_int_is_zero(mat
->row
[row
][i
]))
1023 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
1024 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
1025 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
1027 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
1028 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
1033 isl_int_set(a
, mat
->row
[0][0]);
1034 for (row
= 1; row
< mat
->n_row
; ++row
)
1035 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
1036 if (isl_int_is_zero(a
)){
1040 for (row
= 0; row
< mat
->n_row
; ++row
) {
1041 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
1042 if (isl_int_is_one(mat
->row
[row
][row
]))
1044 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
1057 struct isl_mat
*isl_mat_transpose(struct isl_mat
*mat
)
1059 struct isl_mat
*transpose
= NULL
;
1065 if (mat
->n_col
== mat
->n_row
) {
1066 mat
= isl_mat_cow(mat
);
1069 for (i
= 0; i
< mat
->n_row
; ++i
)
1070 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
1071 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
1074 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
1077 for (i
= 0; i
< mat
->n_row
; ++i
)
1078 for (j
= 0; j
< mat
->n_col
; ++j
)
1079 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
1087 struct isl_mat
*isl_mat_swap_cols(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1091 mat
= isl_mat_cow(mat
);
1092 if (check_col_range(mat
, i
, 1) < 0 ||
1093 check_col_range(mat
, j
, 1) < 0)
1094 return isl_mat_free(mat
);
1096 for (r
= 0; r
< mat
->n_row
; ++r
)
1097 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
1101 struct isl_mat
*isl_mat_swap_rows(struct isl_mat
*mat
, unsigned i
, unsigned j
)
1107 mat
= isl_mat_cow(mat
);
1108 if (check_row_range(mat
, i
, 1) < 0 ||
1109 check_row_range(mat
, j
, 1) < 0)
1110 return isl_mat_free(mat
);
1113 mat
->row
[i
] = mat
->row
[j
];
1118 /* Calculate the product of two matrices.
1120 * This function is optimized for operand matrices that contain many zeros and
1121 * skips multiplications where we know one of the operands is zero.
1123 __isl_give isl_mat
*isl_mat_product(__isl_take isl_mat
*left
,
1124 __isl_take isl_mat
*right
)
1127 struct isl_mat
*prod
;
1129 if (!left
|| !right
)
1131 isl_assert(left
->ctx
, left
->n_col
== right
->n_row
, goto error
);
1132 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
1135 if (left
->n_col
== 0) {
1136 for (i
= 0; i
< prod
->n_row
; ++i
)
1137 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
1139 isl_mat_free(right
);
1142 for (i
= 0; i
< prod
->n_row
; ++i
) {
1143 for (j
= 0; j
< prod
->n_col
; ++j
)
1144 isl_int_mul(prod
->row
[i
][j
],
1145 left
->row
[i
][0], right
->row
[0][j
]);
1146 for (k
= 1; k
< left
->n_col
; ++k
) {
1147 if (isl_int_is_zero(left
->row
[i
][k
]))
1149 for (j
= 0; j
< prod
->n_col
; ++j
)
1150 isl_int_addmul(prod
->row
[i
][j
],
1151 left
->row
[i
][k
], right
->row
[k
][j
]);
1155 isl_mat_free(right
);
1159 isl_mat_free(right
);
1163 /* Replace the variables x in the rows q by x' given by x = M x',
1164 * with M the matrix mat.
1166 * If the number of new variables is greater than the original
1167 * number of variables, then the rows q have already been
1168 * preextended. If the new number is smaller, then the coefficients
1169 * of the divs, which are not changed, need to be shifted down.
1170 * The row q may be the equalities, the inequalities or the
1171 * div expressions. In the latter case, has_div is true and
1172 * we need to take into account the extra denominator column.
1174 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1175 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
1181 if (mat
->n_col
>= mat
->n_row
)
1184 e
= mat
->n_row
- mat
->n_col
;
1186 for (i
= 0; i
< n
; ++i
)
1187 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
1188 t
= isl_mat_sub_alloc6(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
1189 t
= isl_mat_product(t
, mat
);
1192 for (i
= 0; i
< n
; ++i
) {
1193 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
1194 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
1195 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
1196 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
1202 /* Replace the variables x in bset by x' given by x = M x', with
1205 * If there are fewer variables x' then there are x, then we perform
1206 * the transformation in place, which means that, in principle,
1207 * this frees up some extra variables as the number
1208 * of columns remains constant, but we would have to extend
1209 * the div array too as the number of rows in this array is assumed
1210 * to be equal to extra.
1212 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
1213 struct isl_mat
*mat
)
1215 struct isl_ctx
*ctx
;
1221 bset
= isl_basic_set_cow(bset
);
1225 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
1226 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
1227 isl_assert(ctx
, mat
->n_col
> 0, goto error
);
1229 if (mat
->n_col
> mat
->n_row
) {
1230 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0, 0, 0);
1233 } else if (mat
->n_col
< mat
->n_row
) {
1234 bset
->dim
= isl_space_cow(bset
->dim
);
1237 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
1240 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
1241 isl_mat_copy(mat
)) < 0)
1244 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
1245 isl_mat_copy(mat
)) < 0)
1248 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
1251 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
1252 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
1253 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
1254 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
1255 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
1257 bset
= isl_basic_set_simplify(bset
);
1258 bset
= isl_basic_set_finalize(bset
);
1264 isl_basic_set_free(bset
);
1268 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
1272 set
= isl_set_cow(set
);
1276 for (i
= 0; i
< set
->n
; ++i
) {
1277 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
1282 if (mat
->n_col
!= mat
->n_row
) {
1283 set
->dim
= isl_space_cow(set
->dim
);
1286 set
->dim
->n_out
+= mat
->n_col
;
1287 set
->dim
->n_out
-= mat
->n_row
;
1290 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
1298 /* Replace the variables x starting at pos in the rows q
1299 * by x' with x = M x' with M the matrix mat.
1300 * That is, replace the corresponding coefficients c by c M.
1302 static int transform(isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
1303 unsigned pos
, __isl_take isl_mat
*mat
)
1308 t
= isl_mat_sub_alloc6(ctx
, q
, 0, n
, pos
, mat
->n_row
);
1309 t
= isl_mat_product(t
, mat
);
1312 for (i
= 0; i
< n
; ++i
)
1313 isl_seq_swp_or_cpy(q
[i
] + pos
, t
->row
[i
], t
->n_col
);
1318 /* Replace the variables x of type "type" starting at "first" in "bmap"
1319 * by x' with x = M x' with M the matrix trans.
1320 * That is, replace the corresponding coefficients c by c M.
1322 * The transformation matrix should be a square matrix.
1324 __isl_give isl_basic_map
*isl_basic_map_transform_dims(
1325 __isl_take isl_basic_map
*bmap
, enum isl_dim_type type
, unsigned first
,
1326 __isl_take isl_mat
*trans
)
1331 bmap
= isl_basic_map_cow(bmap
);
1332 if (!bmap
|| !trans
)
1335 ctx
= isl_basic_map_get_ctx(bmap
);
1336 if (trans
->n_row
!= trans
->n_col
)
1337 isl_die(trans
->ctx
, isl_error_invalid
,
1338 "expecting square transformation matrix", goto error
);
1339 if (first
+ trans
->n_row
> isl_basic_map_dim(bmap
, type
))
1340 isl_die(trans
->ctx
, isl_error_invalid
,
1341 "oversized transformation matrix", goto error
);
1343 pos
= isl_basic_map_offset(bmap
, type
) + first
;
1345 if (transform(ctx
, bmap
->eq
, bmap
->n_eq
, pos
, isl_mat_copy(trans
)) < 0)
1347 if (transform(ctx
, bmap
->ineq
, bmap
->n_ineq
, pos
,
1348 isl_mat_copy(trans
)) < 0)
1350 if (transform(ctx
, bmap
->div
, bmap
->n_div
, 1 + pos
,
1351 isl_mat_copy(trans
)) < 0)
1354 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1355 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1357 isl_mat_free(trans
);
1360 isl_mat_free(trans
);
1361 isl_basic_map_free(bmap
);
1365 /* Replace the variables x of type "type" starting at "first" in "bset"
1366 * by x' with x = M x' with M the matrix trans.
1367 * That is, replace the corresponding coefficients c by c M.
1369 * The transformation matrix should be a square matrix.
1371 __isl_give isl_basic_set
*isl_basic_set_transform_dims(
1372 __isl_take isl_basic_set
*bset
, enum isl_dim_type type
, unsigned first
,
1373 __isl_take isl_mat
*trans
)
1375 return isl_basic_map_transform_dims(bset
, type
, first
, trans
);
1378 void isl_mat_print_internal(__isl_keep isl_mat
*mat
, FILE *out
, int indent
)
1383 fprintf(out
, "%*snull mat\n", indent
, "");
1387 if (mat
->n_row
== 0)
1388 fprintf(out
, "%*s[]\n", indent
, "");
1390 for (i
= 0; i
< mat
->n_row
; ++i
) {
1392 fprintf(out
, "%*s[[", indent
, "");
1394 fprintf(out
, "%*s[", indent
+1, "");
1395 for (j
= 0; j
< mat
->n_col
; ++j
) {
1398 isl_int_print(out
, mat
->row
[i
][j
], 0);
1400 if (i
== mat
->n_row
-1)
1401 fprintf(out
, "]]\n");
1403 fprintf(out
, "]\n");
1407 void isl_mat_dump(__isl_keep isl_mat
*mat
)
1409 isl_mat_print_internal(mat
, stderr
, 0);
1412 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
1419 mat
= isl_mat_cow(mat
);
1420 if (check_col_range(mat
, col
, n
) < 0)
1421 return isl_mat_free(mat
);
1423 if (col
!= mat
->n_col
-n
) {
1424 for (r
= 0; r
< mat
->n_row
; ++r
)
1425 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
1426 mat
->n_col
- col
- n
);
1432 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
1436 mat
= isl_mat_cow(mat
);
1437 if (check_row_range(mat
, row
, n
) < 0)
1438 return isl_mat_free(mat
);
1440 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
1441 mat
->row
[r
] = mat
->row
[r
+n
];
1447 __isl_give isl_mat
*isl_mat_insert_cols(__isl_take isl_mat
*mat
,
1448 unsigned col
, unsigned n
)
1452 if (check_col_range(mat
, col
, 0) < 0)
1453 return isl_mat_free(mat
);
1457 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
+ n
);
1461 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
, 0, 0, col
);
1462 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, mat
->n_row
,
1463 col
+ n
, col
, mat
->n_col
- col
);
1472 __isl_give isl_mat
*isl_mat_insert_zero_cols(__isl_take isl_mat
*mat
,
1473 unsigned first
, unsigned n
)
1479 mat
= isl_mat_insert_cols(mat
, first
, n
);
1483 for (i
= 0; i
< mat
->n_row
; ++i
)
1484 isl_seq_clr(mat
->row
[i
] + first
, n
);
1489 __isl_give isl_mat
*isl_mat_add_zero_cols(__isl_take isl_mat
*mat
, unsigned n
)
1494 return isl_mat_insert_zero_cols(mat
, mat
->n_col
, n
);
1497 __isl_give isl_mat
*isl_mat_insert_rows(__isl_take isl_mat
*mat
,
1498 unsigned row
, unsigned n
)
1502 if (check_row_range(mat
, row
, 0) < 0)
1503 return isl_mat_free(mat
);
1507 ext
= isl_mat_alloc(mat
->ctx
, mat
->n_row
+ n
, mat
->n_col
);
1511 isl_mat_sub_copy(mat
->ctx
, ext
->row
, mat
->row
, row
, 0, 0, mat
->n_col
);
1512 isl_mat_sub_copy(mat
->ctx
, ext
->row
+ row
+ n
, mat
->row
+ row
,
1513 mat
->n_row
- row
, 0, 0, mat
->n_col
);
1522 __isl_give isl_mat
*isl_mat_add_rows(__isl_take isl_mat
*mat
, unsigned n
)
1527 return isl_mat_insert_rows(mat
, mat
->n_row
, n
);
1530 __isl_give isl_mat
*isl_mat_insert_zero_rows(__isl_take isl_mat
*mat
,
1531 unsigned row
, unsigned n
)
1535 mat
= isl_mat_insert_rows(mat
, row
, n
);
1539 for (i
= 0; i
< n
; ++i
)
1540 isl_seq_clr(mat
->row
[row
+ i
], mat
->n_col
);
1545 __isl_give isl_mat
*isl_mat_add_zero_rows(__isl_take isl_mat
*mat
, unsigned n
)
1550 return isl_mat_insert_zero_rows(mat
, mat
->n_row
, n
);
1553 void isl_mat_col_submul(struct isl_mat
*mat
,
1554 int dst_col
, isl_int f
, int src_col
)
1558 for (i
= 0; i
< mat
->n_row
; ++i
)
1559 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1562 void isl_mat_col_add(__isl_keep isl_mat
*mat
, int dst_col
, int src_col
)
1569 for (i
= 0; i
< mat
->n_row
; ++i
)
1570 isl_int_add(mat
->row
[i
][dst_col
],
1571 mat
->row
[i
][dst_col
], mat
->row
[i
][src_col
]);
1574 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1578 for (i
= 0; i
< mat
->n_row
; ++i
)
1579 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1582 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1583 * return the result.
1585 __isl_give isl_mat
*isl_mat_col_addmul(__isl_take isl_mat
*mat
, int dst_col
,
1586 isl_int f
, int src_col
)
1590 if (check_col(mat
, dst_col
) < 0 || check_col(mat
, src_col
) < 0)
1591 return isl_mat_free(mat
);
1593 for (i
= 0; i
< mat
->n_row
; ++i
) {
1594 if (isl_int_is_zero(mat
->row
[i
][src_col
]))
1596 mat
= isl_mat_cow(mat
);
1599 isl_int_addmul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1605 /* Negate column "col" of "mat" and return the result.
1607 __isl_give isl_mat
*isl_mat_col_neg(__isl_take isl_mat
*mat
, int col
)
1611 if (check_col(mat
, col
) < 0)
1612 return isl_mat_free(mat
);
1614 for (i
= 0; i
< mat
->n_row
; ++i
) {
1615 if (isl_int_is_zero(mat
->row
[i
][col
]))
1617 mat
= isl_mat_cow(mat
);
1620 isl_int_neg(mat
->row
[i
][col
], mat
->row
[i
][col
]);
1626 struct isl_mat
*isl_mat_unimodular_complete(struct isl_mat
*M
, int row
)
1629 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1634 isl_assert(M
->ctx
, M
->n_row
== M
->n_col
, goto error
);
1636 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1637 M
->n_row
= M
->n_col
;
1640 for (r
= 0; r
< row
; ++r
)
1641 isl_assert(M
->ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1642 for (r
= row
; r
< M
->n_row
; ++r
)
1643 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);
1654 __isl_give isl_mat
*isl_mat_concat(__isl_take isl_mat
*top
,
1655 __isl_take isl_mat
*bot
)
1657 struct isl_mat
*mat
;
1662 isl_assert(top
->ctx
, top
->n_col
== bot
->n_col
, goto error
);
1663 if (top
->n_row
== 0) {
1667 if (bot
->n_row
== 0) {
1672 mat
= isl_mat_alloc(top
->ctx
, top
->n_row
+ bot
->n_row
, top
->n_col
);
1675 isl_mat_sub_copy(mat
->ctx
, mat
->row
, top
->row
, top
->n_row
,
1677 isl_mat_sub_copy(mat
->ctx
, mat
->row
+ top
->n_row
, bot
->row
, bot
->n_row
,
1688 int isl_mat_is_equal(__isl_keep isl_mat
*mat1
, __isl_keep isl_mat
*mat2
)
1695 if (mat1
->n_row
!= mat2
->n_row
)
1698 if (mat1
->n_col
!= mat2
->n_col
)
1701 for (i
= 0; i
< mat1
->n_row
; ++i
)
1702 if (!isl_seq_eq(mat1
->row
[i
], mat2
->row
[i
], mat1
->n_col
))
1708 __isl_give isl_mat
*isl_mat_from_row_vec(__isl_take isl_vec
*vec
)
1710 struct isl_mat
*mat
;
1714 mat
= isl_mat_alloc(vec
->ctx
, 1, vec
->size
);
1718 isl_seq_cpy(mat
->row
[0], vec
->el
, vec
->size
);
1727 /* Return a copy of row "row" of "mat" as an isl_vec.
1729 __isl_give isl_vec
*isl_mat_get_row(__isl_keep isl_mat
*mat
, unsigned row
)
1735 if (row
>= mat
->n_row
)
1736 isl_die(mat
->ctx
, isl_error_invalid
, "row out of range",
1739 v
= isl_vec_alloc(isl_mat_get_ctx(mat
), mat
->n_col
);
1742 isl_seq_cpy(v
->el
, mat
->row
[row
], mat
->n_col
);
1747 __isl_give isl_mat
*isl_mat_vec_concat(__isl_take isl_mat
*top
,
1748 __isl_take isl_vec
*bot
)
1750 return isl_mat_concat(top
, isl_mat_from_row_vec(bot
));
1753 __isl_give isl_mat
*isl_mat_move_cols(__isl_take isl_mat
*mat
,
1754 unsigned dst_col
, unsigned src_col
, unsigned n
)
1760 if (n
== 0 || dst_col
== src_col
)
1763 res
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
1767 if (dst_col
< src_col
) {
1768 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1770 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1771 dst_col
, src_col
, n
);
1772 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1773 dst_col
+ n
, dst_col
, src_col
- dst_col
);
1774 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1775 src_col
+ n
, src_col
+ n
,
1776 res
->n_col
- src_col
- n
);
1778 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1780 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1781 src_col
, src_col
+ n
, dst_col
- src_col
);
1782 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1783 dst_col
, src_col
, n
);
1784 isl_mat_sub_copy(res
->ctx
, res
->row
, mat
->row
, mat
->n_row
,
1785 dst_col
+ n
, dst_col
+ n
,
1786 res
->n_col
- dst_col
- n
);
1796 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1797 * Return isl_stat_ok on success and isl_stat_error on failure.
1799 isl_stat
isl_mat_row_gcd(__isl_keep isl_mat
*mat
, int row
, isl_int
*gcd
)
1802 return isl_stat_error
;
1804 if (row
< 0 || row
>= mat
->n_row
)
1805 isl_die(isl_mat_get_ctx(mat
), isl_error_invalid
,
1806 "row out of range", return isl_stat_error
);
1807 isl_seq_gcd(mat
->row
[row
], mat
->n_col
, gcd
);
1812 void isl_mat_gcd(__isl_keep isl_mat
*mat
, isl_int
*gcd
)
1817 isl_int_set_si(*gcd
, 0);
1822 for (i
= 0; i
< mat
->n_row
; ++i
) {
1823 isl_seq_gcd(mat
->row
[i
], mat
->n_col
, &g
);
1824 isl_int_gcd(*gcd
, *gcd
, g
);
1829 /* Return the result of scaling "mat" by a factor of "m".
1831 __isl_give isl_mat
*isl_mat_scale(__isl_take isl_mat
*mat
, isl_int m
)
1835 if (isl_int_is_one(m
))
1838 mat
= isl_mat_cow(mat
);
1842 for (i
= 0; i
< mat
->n_row
; ++i
)
1843 isl_seq_scale(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1848 __isl_give isl_mat
*isl_mat_scale_down(__isl_take isl_mat
*mat
, isl_int m
)
1852 if (isl_int_is_one(m
))
1855 mat
= isl_mat_cow(mat
);
1859 for (i
= 0; i
< mat
->n_row
; ++i
)
1860 isl_seq_scale_down(mat
->row
[i
], mat
->row
[i
], m
, mat
->n_col
);
1865 __isl_give isl_mat
*isl_mat_scale_down_row(__isl_take isl_mat
*mat
, int row
,
1868 if (isl_int_is_one(m
))
1871 mat
= isl_mat_cow(mat
);
1875 isl_seq_scale_down(mat
->row
[row
], mat
->row
[row
], m
, mat
->n_col
);
1880 __isl_give isl_mat
*isl_mat_normalize(__isl_take isl_mat
*mat
)
1888 isl_mat_gcd(mat
, &gcd
);
1889 mat
= isl_mat_scale_down(mat
, gcd
);
1895 __isl_give isl_mat
*isl_mat_normalize_row(__isl_take isl_mat
*mat
, int row
)
1897 mat
= isl_mat_cow(mat
);
1901 isl_seq_normalize(mat
->ctx
, mat
->row
[row
], mat
->n_col
);
1906 /* Number of initial non-zero columns.
1908 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat
*mat
)
1915 for (i
= 0; i
< mat
->n_col
; ++i
)
1916 if (row_first_non_zero(mat
->row
, mat
->n_row
, i
) < 0)