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
;
36 isl_blk_free(ctx
, mat
->block
);
41 struct isl_mat
*isl_mat_extend(struct isl_mat
*mat
,
42 unsigned n_row
, unsigned n_col
)
50 if (mat
->n_col
>= n_col
&& mat
->n_row
>= n_row
)
53 if (mat
->n_col
< n_col
) {
54 struct isl_mat
*new_mat
;
56 new_mat
= isl_mat_alloc(mat
->ctx
, n_row
, n_col
);
59 for (i
= 0; i
< mat
->n_row
; ++i
)
60 isl_seq_cpy(new_mat
->row
[i
], mat
->row
[i
], mat
->n_col
);
65 mat
= isl_mat_cow(mat
);
69 assert(mat
->ref
== 1);
70 old
= mat
->block
.data
;
71 mat
->block
= isl_blk_extend(mat
->ctx
, mat
->block
, n_row
* mat
->n_col
);
72 if (isl_blk_is_error(mat
->block
))
74 mat
->row
= isl_realloc_array(mat
->ctx
, mat
->row
, isl_int
*, n_row
);
78 for (i
= 0; i
< mat
->n_row
; ++i
)
79 mat
->row
[i
] = mat
->block
.data
+ (mat
->row
[i
] - old
);
80 for (i
= mat
->n_row
; i
< n_row
; ++i
)
81 mat
->row
[i
] = mat
->block
.data
+ i
* mat
->n_col
;
90 struct isl_mat
*isl_mat_sub_alloc(struct isl_ctx
*ctx
, isl_int
**row
,
91 unsigned first_row
, unsigned n_row
, unsigned first_col
, unsigned n_col
)
96 mat
= isl_alloc_type(ctx
, struct isl_mat
);
99 mat
->row
= isl_alloc_array(ctx
, isl_int
*, n_row
);
102 for (i
= 0; i
< n_row
; ++i
)
103 mat
->row
[i
] = row
[first_row
+i
] + first_col
;
109 mat
->block
= isl_blk_empty();
110 mat
->flags
= ISL_MAT_BORROWED
;
117 void isl_mat_sub_copy(struct isl_ctx
*ctx
, isl_int
**dst
, isl_int
**src
,
118 unsigned n_row
, unsigned dst_col
, unsigned src_col
, unsigned n_col
)
122 for (i
= 0; i
< n_row
; ++i
)
123 isl_seq_cpy(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
126 void isl_mat_sub_neg(struct isl_ctx
*ctx
, isl_int
**dst
, isl_int
**src
,
127 unsigned n_row
, unsigned dst_col
, unsigned src_col
, unsigned n_col
)
131 for (i
= 0; i
< n_row
; ++i
)
132 isl_seq_neg(dst
[i
]+dst_col
, src
[i
]+src_col
, n_col
);
135 struct isl_mat
*isl_mat_copy(struct isl_mat
*mat
)
144 struct isl_mat
*isl_mat_dup(struct isl_mat
*mat
)
147 struct isl_mat
*mat2
;
151 mat2
= isl_mat_alloc(mat
->ctx
, mat
->n_row
, mat
->n_col
);
154 for (i
= 0; i
< mat
->n_row
; ++i
)
155 isl_seq_cpy(mat2
->row
[i
], mat
->row
[i
], mat
->n_col
);
159 struct isl_mat
*isl_mat_cow(struct isl_mat
*mat
)
161 struct isl_mat
*mat2
;
165 if (mat
->ref
== 1 && !ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
168 mat2
= isl_mat_dup(mat
);
173 void isl_mat_free(struct isl_mat
*mat
)
181 if (!ISL_F_ISSET(mat
, ISL_MAT_BORROWED
))
182 isl_blk_free(mat
->ctx
, mat
->block
);
183 isl_ctx_deref(mat
->ctx
);
188 struct isl_mat
*isl_mat_identity(struct isl_ctx
*ctx
, unsigned n_row
)
193 mat
= isl_mat_alloc(ctx
, n_row
, n_row
);
196 for (i
= 0; i
< n_row
; ++i
) {
197 isl_seq_clr(mat
->row
[i
], i
);
198 isl_int_set_si(mat
->row
[i
][i
], 1);
199 isl_seq_clr(mat
->row
[i
]+i
+1, n_row
-(i
+1));
205 struct isl_vec
*isl_mat_vec_product(struct isl_mat
*mat
, struct isl_vec
*vec
)
208 struct isl_vec
*prod
;
213 isl_assert(ctx
, mat
->n_col
== vec
->size
, goto error
);
215 prod
= isl_vec_alloc(mat
->ctx
, mat
->n_row
);
219 for (i
= 0; i
< prod
->size
; ++i
)
220 isl_seq_inner_product(mat
->row
[i
], vec
->el
, vec
->size
,
221 &prod
->block
.data
[i
]);
231 struct isl_mat
*isl_mat_aff_direct_sum(struct isl_mat
*left
,
232 struct isl_mat
*right
)
240 isl_assert(ctx
, left
->n_row
== right
->n_row
, goto error
);
241 isl_assert(ctx
, left
->n_row
>= 1, goto error
);
242 isl_assert(ctx
, left
->n_col
>= 1, goto error
);
243 isl_assert(ctx
, right
->n_col
>= 1, goto error
);
245 isl_seq_first_non_zero(left
->row
[0]+1, left
->n_col
-1) == -1,
248 isl_seq_first_non_zero(right
->row
[0]+1, right
->n_col
-1) == -1,
251 sum
= isl_mat_alloc(left
->ctx
, left
->n_row
, left
->n_col
+ right
->n_col
- 1);
254 isl_int_lcm(sum
->row
[0][0], left
->row
[0][0], right
->row
[0][0]);
255 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
256 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
258 isl_seq_clr(sum
->row
[0]+1, sum
->n_col
-1);
259 for (i
= 1; i
< sum
->n_row
; ++i
) {
260 isl_int_mul(sum
->row
[i
][0], left
->row
[0][0], left
->row
[i
][0]);
261 isl_int_addmul(sum
->row
[i
][0],
262 right
->row
[0][0], right
->row
[i
][0]);
263 isl_seq_scale(sum
->row
[i
]+1, left
->row
[i
]+1, left
->row
[0][0],
265 isl_seq_scale(sum
->row
[i
]+left
->n_col
,
266 right
->row
[i
]+1, right
->row
[0][0],
270 isl_int_divexact(left
->row
[0][0], sum
->row
[0][0], left
->row
[0][0]);
271 isl_int_divexact(right
->row
[0][0], sum
->row
[0][0], right
->row
[0][0]);
281 static void exchange(struct isl_mat
*M
, struct isl_mat
**U
,
282 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
)
285 for (r
= row
; r
< M
->n_row
; ++r
)
286 isl_int_swap(M
->row
[r
][i
], M
->row
[r
][j
]);
288 for (r
= 0; r
< (*U
)->n_row
; ++r
)
289 isl_int_swap((*U
)->row
[r
][i
], (*U
)->row
[r
][j
]);
292 isl_mat_swap_rows(*Q
, i
, j
);
295 static void subtract(struct isl_mat
*M
, struct isl_mat
**U
,
296 struct isl_mat
**Q
, unsigned row
, unsigned i
, unsigned j
, isl_int m
)
299 for (r
= row
; r
< M
->n_row
; ++r
)
300 isl_int_submul(M
->row
[r
][j
], m
, M
->row
[r
][i
]);
302 for (r
= 0; r
< (*U
)->n_row
; ++r
)
303 isl_int_submul((*U
)->row
[r
][j
], m
, (*U
)->row
[r
][i
]);
306 for (r
= 0; r
< (*Q
)->n_col
; ++r
)
307 isl_int_addmul((*Q
)->row
[i
][r
], m
, (*Q
)->row
[j
][r
]);
311 static void oppose(struct isl_mat
*M
, struct isl_mat
**U
,
312 struct isl_mat
**Q
, unsigned row
, unsigned col
)
315 for (r
= row
; r
< M
->n_row
; ++r
)
316 isl_int_neg(M
->row
[r
][col
], M
->row
[r
][col
]);
318 for (r
= 0; r
< (*U
)->n_row
; ++r
)
319 isl_int_neg((*U
)->row
[r
][col
], (*U
)->row
[r
][col
]);
322 isl_seq_neg((*Q
)->row
[col
], (*Q
)->row
[col
], (*Q
)->n_col
);
325 /* Given matrix M, compute
330 * with U and Q unimodular matrices and H a matrix in column echelon form
331 * such that on each echelon row the entries in the non-echelon column
332 * are non-negative (if neg == 0) or non-positive (if neg == 1)
333 * and stricly smaller (in absolute value) than the entries in the echelon
335 * If U or Q are NULL, then these matrices are not computed.
337 struct isl_mat
*isl_mat_left_hermite(struct isl_mat
*M
, int neg
,
338 struct isl_mat
**U
, struct isl_mat
**Q
)
353 *U
= isl_mat_identity(M
->ctx
, M
->n_col
);
358 *Q
= isl_mat_identity(M
->ctx
, M
->n_col
);
365 for (row
= 0; row
< M
->n_row
; ++row
) {
367 first
= isl_seq_abs_min_non_zero(M
->row
[row
]+col
, M
->n_col
-col
);
372 exchange(M
, U
, Q
, row
, first
, col
);
373 if (isl_int_is_neg(M
->row
[row
][col
]))
374 oppose(M
, U
, Q
, row
, col
);
376 while ((off
= isl_seq_first_non_zero(M
->row
[row
]+first
,
377 M
->n_col
-first
)) != -1) {
379 isl_int_fdiv_q(c
, M
->row
[row
][first
], M
->row
[row
][col
]);
380 subtract(M
, U
, Q
, row
, col
, first
, c
);
381 if (!isl_int_is_zero(M
->row
[row
][first
]))
382 exchange(M
, U
, Q
, row
, first
, col
);
386 for (i
= 0; i
< col
; ++i
) {
387 if (isl_int_is_zero(M
->row
[row
][i
]))
390 isl_int_cdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
392 isl_int_fdiv_q(c
, M
->row
[row
][i
], M
->row
[row
][col
]);
393 if (isl_int_is_zero(c
))
395 subtract(M
, U
, Q
, row
, col
, i
, c
);
414 struct isl_mat
*isl_mat_right_kernel(struct isl_mat
*mat
)
417 struct isl_mat
*U
= NULL
;
420 mat
= isl_mat_left_hermite(mat
, 0, &U
, NULL
);
424 for (i
= 0, rank
= 0; rank
< mat
->n_col
; ++rank
) {
425 while (i
< mat
->n_row
&& isl_int_is_zero(mat
->row
[i
][rank
]))
430 K
= isl_mat_alloc(U
->ctx
, U
->n_row
, U
->n_col
- rank
);
433 isl_mat_sub_copy(K
->ctx
, K
->row
, U
->row
, U
->n_row
, 0, rank
, U
->n_col
-rank
);
443 struct isl_mat
*isl_mat_lin_to_aff(struct isl_mat
*mat
)
446 struct isl_mat
*mat2
;
450 mat2
= isl_mat_alloc(mat
->ctx
, 1+mat
->n_row
, 1+mat
->n_col
);
453 isl_int_set_si(mat2
->row
[0][0], 1);
454 isl_seq_clr(mat2
->row
[0]+1, mat
->n_col
);
455 for (i
= 0; i
< mat
->n_row
; ++i
) {
456 isl_int_set_si(mat2
->row
[1+i
][0], 0);
457 isl_seq_cpy(mat2
->row
[1+i
]+1, mat
->row
[i
], mat
->n_col
);
463 static int row_first_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
467 for (i
= 0; i
< n_row
; ++i
)
468 if (!isl_int_is_zero(row
[i
][col
]))
473 static int row_abs_min_non_zero(isl_int
**row
, unsigned n_row
, unsigned col
)
475 int i
, min
= row_first_non_zero(row
, n_row
, col
);
478 for (i
= min
+ 1; i
< n_row
; ++i
) {
479 if (isl_int_is_zero(row
[i
][col
]))
481 if (isl_int_abs_lt(row
[i
][col
], row
[min
][col
]))
487 static void inv_exchange(struct isl_mat
*left
, struct isl_mat
*right
,
488 unsigned i
, unsigned j
)
490 left
= isl_mat_swap_rows(left
, i
, j
);
491 right
= isl_mat_swap_rows(right
, i
, j
);
494 static void inv_oppose(
495 struct isl_mat
*left
, struct isl_mat
*right
, unsigned row
)
497 isl_seq_neg(left
->row
[row
]+row
, left
->row
[row
]+row
, left
->n_col
-row
);
498 isl_seq_neg(right
->row
[row
], right
->row
[row
], right
->n_col
);
501 static void inv_subtract(struct isl_mat
*left
, struct isl_mat
*right
,
502 unsigned row
, unsigned i
, isl_int m
)
505 isl_seq_combine(left
->row
[i
]+row
,
506 left
->ctx
->one
, left
->row
[i
]+row
,
507 m
, left
->row
[row
]+row
,
509 isl_seq_combine(right
->row
[i
], right
->ctx
->one
, right
->row
[i
],
510 m
, right
->row
[row
], right
->n_col
);
513 /* Compute inv(left)*right
515 struct isl_mat
*isl_mat_inverse_product(struct isl_mat
*left
,
516 struct isl_mat
*right
)
524 isl_assert(left
->ctx
, left
->n_row
== left
->n_col
, goto error
);
525 isl_assert(left
->ctx
, left
->n_row
== right
->n_row
, goto error
);
527 if (left
->n_row
== 0) {
532 left
= isl_mat_cow(left
);
533 right
= isl_mat_cow(right
);
539 for (row
= 0; row
< left
->n_row
; ++row
) {
540 int pivot
, first
, i
, off
;
541 pivot
= row_abs_min_non_zero(left
->row
+row
, left
->n_row
-row
, row
);
545 isl_assert(ctx
, pivot
>= 0, goto error
);
549 inv_exchange(left
, right
, pivot
, row
);
550 if (isl_int_is_neg(left
->row
[row
][row
]))
551 inv_oppose(left
, right
, row
);
553 while ((off
= row_first_non_zero(left
->row
+first
,
554 left
->n_row
-first
, row
)) != -1) {
556 isl_int_fdiv_q(a
, left
->row
[first
][row
],
557 left
->row
[row
][row
]);
558 inv_subtract(left
, right
, row
, first
, a
);
559 if (!isl_int_is_zero(left
->row
[first
][row
]))
560 inv_exchange(left
, right
, row
, first
);
564 for (i
= 0; i
< row
; ++i
) {
565 if (isl_int_is_zero(left
->row
[i
][row
]))
567 isl_int_gcd(a
, left
->row
[row
][row
], left
->row
[i
][row
]);
568 isl_int_divexact(b
, left
->row
[i
][row
], a
);
569 isl_int_divexact(a
, left
->row
[row
][row
], a
);
571 isl_seq_combine(left
->row
[i
]+row
,
573 b
, left
->row
[row
]+row
,
575 isl_seq_combine(right
->row
[i
], a
, right
->row
[i
],
576 b
, right
->row
[row
], right
->n_col
);
581 isl_int_set(a
, left
->row
[0][0]);
582 for (row
= 1; row
< left
->n_row
; ++row
)
583 isl_int_lcm(a
, a
, left
->row
[row
][row
]);
584 if (isl_int_is_zero(a
)){
586 isl_assert(ctx
, 0, goto error
);
588 for (row
= 0; row
< left
->n_row
; ++row
) {
589 isl_int_divexact(left
->row
[row
][row
], a
, left
->row
[row
][row
]);
590 if (isl_int_is_one(left
->row
[row
][row
]))
592 isl_seq_scale(right
->row
[row
], right
->row
[row
],
593 left
->row
[row
][row
], right
->n_col
);
605 void isl_mat_col_scale(struct isl_mat
*mat
, unsigned col
, isl_int m
)
609 for (i
= 0; i
< mat
->n_row
; ++i
)
610 isl_int_mul(mat
->row
[i
][col
], mat
->row
[i
][col
], m
);
613 void isl_mat_col_combine(struct isl_mat
*mat
, unsigned dst
,
614 isl_int m1
, unsigned src1
, isl_int m2
, unsigned src2
)
620 for (i
= 0; i
< mat
->n_row
; ++i
) {
621 isl_int_mul(tmp
, m1
, mat
->row
[i
][src1
]);
622 isl_int_addmul(tmp
, m2
, mat
->row
[i
][src2
]);
623 isl_int_set(mat
->row
[i
][dst
], tmp
);
628 struct isl_mat
*isl_mat_right_inverse(struct isl_mat
*mat
)
634 mat
= isl_mat_cow(mat
);
638 inv
= isl_mat_identity(mat
->ctx
, mat
->n_col
);
639 inv
= isl_mat_cow(inv
);
645 for (row
= 0; row
< mat
->n_row
; ++row
) {
646 int pivot
, first
, i
, off
;
647 pivot
= isl_seq_abs_min_non_zero(mat
->row
[row
]+row
, mat
->n_col
-row
);
655 exchange(mat
, &inv
, NULL
, row
, pivot
, row
);
656 if (isl_int_is_neg(mat
->row
[row
][row
]))
657 oppose(mat
, &inv
, NULL
, row
, row
);
659 while ((off
= isl_seq_first_non_zero(mat
->row
[row
]+first
,
660 mat
->n_col
-first
)) != -1) {
662 isl_int_fdiv_q(a
, mat
->row
[row
][first
],
664 subtract(mat
, &inv
, NULL
, row
, row
, first
, a
);
665 if (!isl_int_is_zero(mat
->row
[row
][first
]))
666 exchange(mat
, &inv
, NULL
, row
, row
, first
);
670 for (i
= 0; i
< row
; ++i
) {
671 if (isl_int_is_zero(mat
->row
[row
][i
]))
673 isl_int_gcd(a
, mat
->row
[row
][row
], mat
->row
[row
][i
]);
674 isl_int_divexact(b
, mat
->row
[row
][i
], a
);
675 isl_int_divexact(a
, mat
->row
[row
][row
], a
);
677 isl_mat_col_combine(mat
, i
, a
, i
, b
, row
);
678 isl_mat_col_combine(inv
, i
, a
, i
, b
, row
);
683 isl_int_set(a
, mat
->row
[0][0]);
684 for (row
= 1; row
< mat
->n_row
; ++row
)
685 isl_int_lcm(a
, a
, mat
->row
[row
][row
]);
686 if (isl_int_is_zero(a
)){
690 for (row
= 0; row
< mat
->n_row
; ++row
) {
691 isl_int_divexact(mat
->row
[row
][row
], a
, mat
->row
[row
][row
]);
692 if (isl_int_is_one(mat
->row
[row
][row
]))
694 isl_mat_col_scale(inv
, row
, mat
->row
[row
][row
]);
706 struct isl_mat
*isl_mat_transpose(struct isl_mat
*mat
)
708 struct isl_mat
*transpose
= NULL
;
711 if (mat
->n_col
== mat
->n_row
) {
712 mat
= isl_mat_cow(mat
);
715 for (i
= 0; i
< mat
->n_row
; ++i
)
716 for (j
= i
+ 1; j
< mat
->n_col
; ++j
)
717 isl_int_swap(mat
->row
[i
][j
], mat
->row
[j
][i
]);
720 transpose
= isl_mat_alloc(mat
->ctx
, mat
->n_col
, mat
->n_row
);
723 for (i
= 0; i
< mat
->n_row
; ++i
)
724 for (j
= 0; j
< mat
->n_col
; ++j
)
725 isl_int_set(transpose
->row
[j
][i
], mat
->row
[i
][j
]);
733 struct isl_mat
*isl_mat_swap_cols(struct isl_mat
*mat
, unsigned i
, unsigned j
)
737 mat
= isl_mat_cow(mat
);
740 isl_assert(ctx
, i
< mat
->n_col
, goto error
);
741 isl_assert(ctx
, j
< mat
->n_col
, goto error
);
743 for (r
= 0; r
< mat
->n_row
; ++r
)
744 isl_int_swap(mat
->row
[r
][i
], mat
->row
[r
][j
]);
751 struct isl_mat
*isl_mat_swap_rows(struct isl_mat
*mat
, unsigned i
, unsigned j
)
757 mat
= isl_mat_cow(mat
);
761 mat
->row
[i
] = mat
->row
[j
];
766 struct isl_mat
*isl_mat_product(struct isl_mat
*left
, struct isl_mat
*right
)
769 struct isl_mat
*prod
;
773 isl_assert(ctx
, left
->n_col
== right
->n_row
, goto error
);
774 prod
= isl_mat_alloc(left
->ctx
, left
->n_row
, right
->n_col
);
777 if (left
->n_col
== 0) {
778 for (i
= 0; i
< prod
->n_row
; ++i
)
779 isl_seq_clr(prod
->row
[i
], prod
->n_col
);
782 for (i
= 0; i
< prod
->n_row
; ++i
) {
783 for (j
= 0; j
< prod
->n_col
; ++j
) {
784 isl_int_mul(prod
->row
[i
][j
],
785 left
->row
[i
][0], right
->row
[0][j
]);
786 for (k
= 1; k
< left
->n_col
; ++k
)
787 isl_int_addmul(prod
->row
[i
][j
],
788 left
->row
[i
][k
], right
->row
[k
][j
]);
800 /* Replace the variables x in the rows q by x' given by x = M x',
801 * with M the matrix mat.
803 * If the number of new variables is greater than the original
804 * number of variables, then the rows q have already been
805 * preextended. If the new number is smaller, then the coefficients
806 * of the divs, which are not changed, need to be shifted down.
807 * The row q may be the equalities, the inequalities or the
808 * div expressions. In the latter case, has_div is true and
809 * we need to take into account the extra denominator column.
811 static int preimage(struct isl_ctx
*ctx
, isl_int
**q
, unsigned n
,
812 unsigned n_div
, int has_div
, struct isl_mat
*mat
)
818 if (mat
->n_col
>= mat
->n_row
)
821 e
= mat
->n_row
- mat
->n_col
;
823 for (i
= 0; i
< n
; ++i
)
824 isl_int_mul(q
[i
][0], q
[i
][0], mat
->row
[0][0]);
825 t
= isl_mat_sub_alloc(mat
->ctx
, q
, 0, n
, has_div
, mat
->n_row
);
826 t
= isl_mat_product(t
, mat
);
829 for (i
= 0; i
< n
; ++i
) {
830 isl_seq_swp_or_cpy(q
[i
] + has_div
, t
->row
[i
], t
->n_col
);
831 isl_seq_cpy(q
[i
] + has_div
+ t
->n_col
,
832 q
[i
] + has_div
+ t
->n_col
+ e
, n_div
);
833 isl_seq_clr(q
[i
] + has_div
+ t
->n_col
+ n_div
, e
);
839 /* Replace the variables x in bset by x' given by x = M x', with
842 * If there are fewer variables x' then there are x, then we perform
843 * the transformation in place, which that, in principle,
844 * this frees up some extra variables as the number
845 * of columns remains constant, but we would have to extend
846 * the div array too as the number of rows in this array is assumed
847 * to be equal to extra.
849 struct isl_basic_set
*isl_basic_set_preimage(struct isl_basic_set
*bset
,
858 bset
= isl_basic_set_cow(bset
);
862 isl_assert(ctx
, bset
->dim
->nparam
== 0, goto error
);
863 isl_assert(ctx
, 1+bset
->dim
->n_out
== mat
->n_row
, goto error
);
865 if (mat
->n_col
> mat
->n_row
)
866 bset
= isl_basic_set_extend(bset
, 0, mat
->n_col
-1, 0,
868 else if (mat
->n_col
< mat
->n_row
) {
869 bset
->dim
= isl_dim_cow(bset
->dim
);
872 bset
->dim
->n_out
-= mat
->n_row
- mat
->n_col
;
875 if (preimage(ctx
, bset
->eq
, bset
->n_eq
, bset
->n_div
, 0,
876 isl_mat_copy(mat
)) < 0)
879 if (preimage(ctx
, bset
->ineq
, bset
->n_ineq
, bset
->n_div
, 0,
880 isl_mat_copy(mat
)) < 0)
883 if (preimage(ctx
, bset
->div
, bset
->n_div
, bset
->n_div
, 1, mat
) < 0)
886 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
887 ISL_F_CLR(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
888 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
889 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED_DIVS
);
890 ISL_F_CLR(bset
, ISL_BASIC_SET_ALL_EQUALITIES
);
892 bset
= isl_basic_set_simplify(bset
);
893 bset
= isl_basic_set_finalize(bset
);
899 isl_basic_set_free(bset
);
903 struct isl_set
*isl_set_preimage(struct isl_set
*set
, struct isl_mat
*mat
)
908 set
= isl_set_cow(set
);
913 for (i
= 0; i
< set
->n
; ++i
) {
914 set
->p
[i
] = isl_basic_set_preimage(set
->p
[i
],
919 if (mat
->n_col
!= mat
->n_row
) {
920 set
->dim
= isl_dim_cow(set
->dim
);
923 set
->dim
->n_out
+= mat
->n_col
;
924 set
->dim
->n_out
-= mat
->n_row
;
927 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
935 void isl_mat_dump(struct isl_mat
*mat
, FILE *out
, int indent
)
940 fprintf(out
, "%*snull mat\n", indent
, "");
945 fprintf(out
, "%*s[]\n", indent
, "");
947 for (i
= 0; i
< mat
->n_row
; ++i
) {
949 fprintf(out
, "%*s[[", indent
, "");
951 fprintf(out
, "%*s[", indent
+1, "");
952 for (j
= 0; j
< mat
->n_col
; ++j
) {
955 isl_int_print(out
, mat
->row
[i
][j
], 0);
957 if (i
== mat
->n_row
-1)
958 fprintf(out
, "]]\n");
964 struct isl_mat
*isl_mat_drop_cols(struct isl_mat
*mat
, unsigned col
, unsigned n
)
968 mat
= isl_mat_cow(mat
);
972 if (col
!= mat
->n_col
-n
) {
973 for (r
= 0; r
< mat
->n_row
; ++r
)
974 isl_seq_cpy(mat
->row
[r
]+col
, mat
->row
[r
]+col
+n
,
975 mat
->n_col
- col
- n
);
981 struct isl_mat
*isl_mat_drop_rows(struct isl_mat
*mat
, unsigned row
, unsigned n
)
985 mat
= isl_mat_cow(mat
);
989 for (r
= row
; r
+n
< mat
->n_row
; ++r
)
990 mat
->row
[r
] = mat
->row
[r
+n
];
996 void isl_mat_col_submul(struct isl_mat
*mat
,
997 int dst_col
, isl_int f
, int src_col
)
1001 for (i
= 0; i
< mat
->n_row
; ++i
)
1002 isl_int_submul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1005 void isl_mat_col_mul(struct isl_mat
*mat
, int dst_col
, isl_int f
, int src_col
)
1009 for (i
= 0; i
< mat
->n_row
; ++i
)
1010 isl_int_mul(mat
->row
[i
][dst_col
], f
, mat
->row
[i
][src_col
]);
1013 struct isl_mat
*isl_mat_unimodular_complete(struct isl_mat
*M
, int row
)
1016 struct isl_mat
*H
= NULL
, *Q
= NULL
;
1018 isl_assert(ctx
, M
->n_row
== M
->n_col
, goto error
);
1020 H
= isl_mat_left_hermite(isl_mat_copy(M
), 0, NULL
, &Q
);
1021 M
->n_row
= M
->n_col
;
1024 for (r
= 0; r
< row
; ++r
)
1025 isl_assert(ctx
, isl_int_is_one(H
->row
[r
][r
]), goto error
);
1026 for (r
= row
; r
< M
->n_row
; ++r
)
1027 isl_seq_cpy(M
->row
[r
], Q
->row
[r
], M
->n_col
);