4 #include "isl_map_private.h"
6 struct isl_mat
*isl_mat_alloc(struct isl_ctx
*ctx
,
7 unsigned n_row
, unsigned n_col
)
12 mat
= isl_alloc_type(ctx
, struct isl_mat
);
17 mat
->block
= isl_blk_alloc(ctx
, n_row
* n_col
);
18 if (isl_blk_is_error(mat
->block
))
20 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
24 for (i
= 0; i
< n_row
; ++i
)
25 mat
->row
[i
] = mat
->block
.data
+ i
* n_col
;
37 isl_blk_free(ctx
, mat
->block
);
42 struct isl_mat
*isl_mat_extend(struct isl_mat
*mat
,
43 unsigned n_row
, unsigned n_col
)
51 if (mat
->max_col
>= n_col
&& mat
->n_row
>= n_row
) {
52 if (mat
->n_col
< n_col
)
57 if (mat
->max_col
< n_col
) {
58 struct isl_mat
*new_mat
;
60 if (n_row
< mat
->n_row
)
62 new_mat
= isl_mat_alloc(mat
->ctx
, n_row
, n_col
);
65 for (i
= 0; i
< mat
->n_row
; ++i
)
66 isl_seq_cpy(new_mat
->row
[i
], mat
->row
[i
], mat
->n_col
);
71 mat
= isl_mat_cow(mat
);
75 assert(mat
->ref
== 1);
76 old
= mat
->block
.data
;
77 mat
->block
= isl_blk_extend(mat
->ctx
, mat
->block
, n_row
* mat
->max_col
);
78 if (isl_blk_is_error(mat
->block
))
80 mat
->row
= isl_realloc_array(mat
->ctx
, mat
->row
, isl_int
*, n_row
);
84 for (i
= 0; i
< mat
->n_row
; ++i
)
85 mat
->row
[i
] = mat
->block
.data
+ (mat
->row
[i
] - old
);
86 for (i
= mat
->n_row
; i
< n_row
; ++i
)
87 mat
->row
[i
] = mat
->block
.data
+ i
* mat
->max_col
;
89 if (mat
->n_col
< n_col
)
98 struct isl_mat
*isl_mat_sub_alloc(struct isl_ctx
*ctx
, isl_int
**row
,
99 unsigned first_row
, unsigned n_row
, unsigned first_col
, unsigned n_col
)
104 mat
= isl_alloc_type(ctx
, struct isl_mat
);
107 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
110 for (i
= 0; i
< n_row
; ++i
)
111 mat
->row
[i
] = row
[first_row
+i
] + first_col
;
117 mat
->block
= isl_blk_empty();
118 mat
->flags
= ISL_MAT_BORROWED
;
125 void isl_mat_sub_copy(struct isl_ctx
*ctx
, isl_int
**dst
, isl_int
**src
,
126 unsigned n_row
, unsigned dst_col
, unsigned src_col
, unsigned n_col
)
130 for (i
= 0; i
< n_row
; ++i
)
131 isl_seq_cpy(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
134 void isl_mat_sub_neg(struct isl_ctx
*ctx
, isl_int
**dst
, isl_int
**src
,
135 unsigned n_row
, unsigned dst_col
, unsigned src_col
, unsigned n_col
)
139 for (i
= 0; i
< n_row
; ++i
)
140 isl_seq_neg(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
143 struct isl_mat
*isl_mat_copy(struct isl_mat
*mat
)
152 struct isl_mat
*isl_mat_dup(struct isl_mat
*mat
)
155 struct isl_mat
*mat2
;
159 mat2
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
162 for (i
= 0; i
< mat
->n_row
; ++i
)
163 isl_seq_cpy(mat2
->row
[i
], mat
->row
[i
], mat
->n_col
);
167 struct isl_mat
*isl_mat_cow(struct isl_mat
*mat
)
169 struct isl_mat
*mat2
;
173 if (mat
->ref
== 1 && !ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
176 mat2
= isl_mat_dup(mat
);
181 void isl_mat_free(struct isl_mat
*mat
)
189 if (!ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
190 isl_blk_free(mat
->ctx
, mat
->block
);
191 isl_ctx_deref(mat
->ctx
);
196 struct isl_mat
*isl_mat_identity(struct isl_ctx
*ctx
, unsigned n_row
)
201 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
204 for (i
= 0; i
< n_row
; ++i
) {
205 isl_seq_clr(mat
->row
[i
], i
);
206 isl_int_set_si(mat
->row
[i
][i
], 1);
207 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
213 struct isl_vec
*isl_mat_vec_product(struct isl_mat
*mat
, struct isl_vec
*vec
)
216 struct isl_vec
*prod
;
221 isl_assert(ctx
, mat
->n_col
== vec
->size
, goto error
);
223 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
227 for (i
= 0; i
< prod
->size
; ++i
)
228 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
229 &prod
->block
.data
[i
]);
239 struct isl_mat
*isl_mat_aff_direct_sum(struct isl_mat
*left
,
240 struct isl_mat
*right
)
248 isl_assert(ctx
, left
->n_row
== right
->n_row
, goto error
);
249 isl_assert(ctx
, left
->n_row
>= 1, goto error
);
250 isl_assert(ctx
, left
->n_col
>= 1, goto error
);
251 isl_assert(ctx
, right
->n_col
>= 1, goto error
);
253 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
256 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
259 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
262 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
263 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
264 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
266 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
267 for (i
= 1; i
< sum
->n_row
; ++i
) {
268 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
269 isl_int_addmul(sum
->row
[i
][0],
270 right
->row
[0][0], right
->row
[i
][0]);
271 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
273 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
274 right
->row
[i
]+1, right
->row
[0][0],
278 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
279 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
289 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
290 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
293 for (r
= row
; r
< M
->n_row
; ++r
)
294 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
296 for (r
= 0; r
< (*U
)->n_row
; ++r
)
297 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
300 isl_mat_swap_rows(*Q
, i
, j
);
303 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
304 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
307 for (r
= row
; r
< M
->n_row
; ++r
)
308 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
310 for (r
= 0; r
< (*U
)->n_row
; ++r
)
311 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
314 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
315 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
319 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
320 struct isl_mat
**Q
, unsigned row
, unsigned col
)
323 for (r
= row
; r
< M
->n_row
; ++r
)
324 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
326 for (r
= 0; r
< (*U
)->n_row
; ++r
)
327 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
330 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
333 /* Given matrix M, compute
338 * with U and Q unimodular matrices and H a matrix in column echelon form
339 * such that on each echelon row the entries in the non-echelon column
340 * are non-negative (if neg == 0) or non-positive (if neg == 1)
341 * and stricly smaller (in absolute value) than the entries in the echelon
343 * If U or Q are NULL, then these matrices are not computed.
345 struct isl_mat
*isl_mat_left_hermite(struct isl_mat
*M
, int neg
,
346 struct isl_mat
**U
, struct isl_mat
**Q
)
361 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
366 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
373 for (row
= 0; row
< M
->n_row
; ++row
) {
375 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
380 exchange(M
, U
, Q
, row
, first
, col
);
381 if (isl_int_is_neg(M
->row
[row
][col
]))
382 oppose(M
, U
, Q
, row
, col
);
384 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
385 M
->n_col
-first
)) != -1) {
387 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
388 subtract(M
, U
, Q
, row
, col
, first
, c
);
389 if (!isl_int_is_zero(M
->row
[row
][first
]))
390 exchange(M
, U
, Q
, row
, first
, col
);
394 for (i
= 0; i
< col
; ++i
) {
395 if (isl_int_is_zero(M
->row
[row
][i
]))
398 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
400 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
401 if (isl_int_is_zero(c
))
403 subtract(M
, U
, Q
, row
, col
, i
, c
);
422 struct isl_mat
*isl_mat_right_kernel(struct isl_mat
*mat
)
425 struct isl_mat
*U
= NULL
;
428 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
432 for (i
= 0, rank
= 0; rank
< mat
->n_col
; ++rank
) {
433 while (i
< mat
->n_row
&& isl_int_is_zero(mat
->row
[i
][rank
]))
438 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
441 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
451 struct isl_mat
*isl_mat_lin_to_aff(struct isl_mat
*mat
)
454 struct isl_mat
*mat2
;
458 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
461 isl_int_set_si(mat2
->row
[0][0], 1);
462 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
463 for (i
= 0; i
< mat
->n_row
; ++i
) {
464 isl_int_set_si(mat2
->row
[1+i
][0], 0);
465 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
471 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
475 for (i
= 0; i
< n_row
; ++i
)
476 if (!isl_int_is_zero(row
[i
][col
]))
481 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
483 int i
, min
= row_first_non_zero(row
, n_row
, col
);
486 for (i
= min
+ 1; i
< n_row
; ++i
) {
487 if (isl_int_is_zero(row
[i
][col
]))
489 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
495 static void inv_exchange(struct isl_mat
*left
, struct isl_mat
*right
,
496 unsigned i
, unsigned j
)
498 left
= isl_mat_swap_rows(left
, i
, j
);
499 right
= isl_mat_swap_rows(right
, i
, j
);
502 static void inv_oppose(
503 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
505 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
506 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
509 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
510 unsigned row
, unsigned i
, isl_int m
)
513 isl_seq_combine(left
->row
[i
]+row
,
514 left
->ctx
->one
, left
->row
[i
]+row
,
515 m
, left
->row
[row
]+row
,
517 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
518 m
, right
->row
[row
], right
->n_col
);
521 /* Compute inv(left)*right
523 struct isl_mat
*isl_mat_inverse_product(struct isl_mat
*left
,
524 struct isl_mat
*right
)
532 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
533 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
535 if (left
->n_row
== 0) {
540 left
= isl_mat_cow(left
);
541 right
= isl_mat_cow(right
);
547 for (row
= 0; row
< left
->n_row
; ++row
) {
548 int pivot
, first
, i
, off
;
549 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
553 isl_assert(ctx
, pivot
>= 0, goto error
);
557 inv_exchange(left
, right
, pivot
, row
);
558 if (isl_int_is_neg(left
->row
[row
][row
]))
559 inv_oppose(left
, right
, row
);
561 while ((off
= row_first_non_zero(left
->row
+first
,
562 left
->n_row
-first
, row
)) != -1) {
564 isl_int_fdiv_q(a
, left
->row
[first
][row
],
565 left
->row
[row
][row
]);
566 inv_subtract(left
, right
, row
, first
, a
);
567 if (!isl_int_is_zero(left
->row
[first
][row
]))
568 inv_exchange(left
, right
, row
, first
);
572 for (i
= 0; i
< row
; ++i
) {
573 if (isl_int_is_zero(left
->row
[i
][row
]))
575 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
576 isl_int_divexact(b
, left
->row
[i
][row
], a
);
577 isl_int_divexact(a
, left
->row
[row
][row
], a
);
579 isl_seq_combine(left
->row
[i
]+row
,
581 b
, left
->row
[row
]+row
,
583 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
584 b
, right
->row
[row
], right
->n_col
);
589 isl_int_set(a
, left
->row
[0][0]);
590 for (row
= 1; row
< left
->n_row
; ++row
)
591 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
592 if (isl_int_is_zero(a
)){
594 isl_assert(ctx
, 0, goto error
);
596 for (row
= 0; row
< left
->n_row
; ++row
) {
597 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
598 if (isl_int_is_one(left
->row
[row
][row
]))
600 isl_seq_scale(right
->row
[row
], right
->row
[row
],
601 left
->row
[row
][row
], right
->n_col
);
613 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
617 for (i
= 0; i
< mat
->n_row
; ++i
)
618 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
621 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
622 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
628 for (i
= 0; i
< mat
->n_row
; ++i
) {
629 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
630 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
631 isl_int_set(mat
->row
[i
][dst
], tmp
);
636 struct isl_mat
*isl_mat_right_inverse(struct isl_mat
*mat
)
642 mat
= isl_mat_cow(mat
);
646 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
647 inv
= isl_mat_cow(inv
);
653 for (row
= 0; row
< mat
->n_row
; ++row
) {
654 int pivot
, first
, i
, off
;
655 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
663 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
664 if (isl_int_is_neg(mat
->row
[row
][row
]))
665 oppose(mat
, &inv
, NULL
, row
, row
);
667 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
668 mat
->n_col
-first
)) != -1) {
670 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
672 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
673 if (!isl_int_is_zero(mat
->row
[row
][first
]))
674 exchange(mat
, &inv
, NULL
, row
, row
, first
);
678 for (i
= 0; i
< row
; ++i
) {
679 if (isl_int_is_zero(mat
->row
[row
][i
]))
681 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
682 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
683 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
685 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
686 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
691 isl_int_set(a
, mat
->row
[0][0]);
692 for (row
= 1; row
< mat
->n_row
; ++row
)
693 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
694 if (isl_int_is_zero(a
)){
698 for (row
= 0; row
< mat
->n_row
; ++row
) {
699 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
700 if (isl_int_is_one(mat
->row
[row
][row
]))
702 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
714 struct isl_mat
*isl_mat_transpose(struct isl_mat
*mat
)
716 struct isl_mat
*transpose
= NULL
;
719 if (mat
->n_col
== mat
->n_row
) {
720 mat
= isl_mat_cow(mat
);
723 for (i
= 0; i
< mat
->n_row
; ++i
)
724 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
725 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
728 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
731 for (i
= 0; i
< mat
->n_row
; ++i
)
732 for (j
= 0; j
< mat
->n_col
; ++j
)
733 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
741 struct isl_mat
*isl_mat_swap_cols(struct isl_mat
*mat
, unsigned i
, unsigned j
)
745 mat
= isl_mat_cow(mat
);
748 isl_assert(ctx
, i
< mat
->n_col
, goto error
);
749 isl_assert(ctx
, j
< mat
->n_col
, goto error
);
751 for (r
= 0; r
< mat
->n_row
; ++r
)
752 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
759 struct isl_mat
*isl_mat_swap_rows(struct isl_mat
*mat
, unsigned i
, unsigned j
)
765 mat
= isl_mat_cow(mat
);
769 mat
->row
[i
] = mat
->row
[j
];
774 struct isl_mat
*isl_mat_product(struct isl_mat
*left
, struct isl_mat
*right
)
777 struct isl_mat
*prod
;
781 isl_assert(ctx
, left
->n_col
== right
->n_row
, goto error
);
782 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
785 if (left
->n_col
== 0) {
786 for (i
= 0; i
< prod
->n_row
; ++i
)
787 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
790 for (i
= 0; i
< prod
->n_row
; ++i
) {
791 for (j
= 0; j
< prod
->n_col
; ++j
) {
792 isl_int_mul(prod
->row
[i
][j
],
793 left
->row
[i
][0], right
->row
[0][j
]);
794 for (k
= 1; k
< left
->n_col
; ++k
)
795 isl_int_addmul(prod
->row
[i
][j
],
796 left
->row
[i
][k
], right
->row
[k
][j
]);
808 /* Replace the variables x in the rows q by x' given by x = M x',
809 * with M the matrix mat.
811 * If the number of new variables is greater than the original
812 * number of variables, then the rows q have already been
813 * preextended. If the new number is smaller, then the coefficients
814 * of the divs, which are not changed, need to be shifted down.
815 * The row q may be the equalities, the inequalities or the
816 * div expressions. In the latter case, has_div is true and
817 * we need to take into account the extra denominator column.
819 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
820 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
826 if (mat
->n_col
>= mat
->n_row
)
829 e
= mat
->n_row
- mat
->n_col
;
831 for (i
= 0; i
< n
; ++i
)
832 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
833 t
= isl_mat_sub_alloc(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
834 t
= isl_mat_product(t
, mat
);
837 for (i
= 0; i
< n
; ++i
) {
838 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
839 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
840 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
841 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
847 /* Replace the variables x in bset by x' given by x = M x', with
850 * If there are fewer variables x' then there are x, then we perform
851 * the transformation in place, which that, in principle,
852 * this frees up some extra variables as the number
853 * of columns remains constant, but we would have to extend
854 * the div array too as the number of rows in this array is assumed
855 * to be equal to extra.
857 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
866 bset
= isl_basic_set_cow(bset
);
870 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
871 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
873 if (mat
->n_col
> mat
->n_row
)
874 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0,
876 else if (mat
->n_col
< mat
->n_row
) {
877 bset
->dim
= isl_dim_cow(bset
->dim
);
880 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
883 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
884 isl_mat_copy(mat
)) < 0)
887 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
888 isl_mat_copy(mat
)) < 0)
891 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
894 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
895 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
896 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
897 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
898 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
900 bset
= isl_basic_set_simplify(bset
);
901 bset
= isl_basic_set_finalize(bset
);
907 isl_basic_set_free(bset
);
911 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
916 set
= isl_set_cow(set
);
921 for (i
= 0; i
< set
->n
; ++i
) {
922 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
927 if (mat
->n_col
!= mat
->n_row
) {
928 set
->dim
= isl_dim_cow(set
->dim
);
931 set
->dim
->n_out
+= mat
->n_col
;
932 set
->dim
->n_out
-= mat
->n_row
;
935 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
943 void isl_mat_dump(struct isl_mat
*mat
, FILE *out
, int indent
)
948 fprintf(out
, "%*snull mat\n", indent
, "");
953 fprintf(out
, "%*s[]\n", indent
, "");
955 for (i
= 0; i
< mat
->n_row
; ++i
) {
957 fprintf(out
, "%*s[[", indent
, "");
959 fprintf(out
, "%*s[", indent
+1, "");
960 for (j
= 0; j
< mat
->n_col
; ++j
) {
963 isl_int_print(out
, mat
->row
[i
][j
], 0);
965 if (i
== mat
->n_row
-1)
966 fprintf(out
, "]]\n");
972 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
976 mat
= isl_mat_cow(mat
);
980 if (col
!= mat
->n_col
-n
) {
981 for (r
= 0; r
< mat
->n_row
; ++r
)
982 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
983 mat
->n_col
- col
- n
);
989 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
993 mat
= isl_mat_cow(mat
);
997 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
998 mat
->row
[r
] = mat
->row
[r
+n
];
1004 void isl_mat_col_submul(struct isl_mat
*mat
,
1005 int dst_col
, isl_int f
, int src_col
)
1009 for (i
= 0; i
< mat
->n_row
; ++i
)
1010 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1013 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1017 for (i
= 0; i
< mat
->n_row
; ++i
)
1018 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1021 struct isl_mat
*isl_mat_unimodular_complete(struct isl_mat
*M
, int row
)
1024 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1026 isl_assert(ctx
, M
->n_row
== M
->n_col
, goto error
);
1028 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1029 M
->n_row
= M
->n_col
;
1032 for (r
= 0; r
< row
; ++r
)
1033 isl_assert(ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1034 for (r
= row
; r
< M
->n_row
; ++r
)
1035 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);