1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2014 Ecole Normale Superieure
5 * Copyright 2017 Sven Verdoolaege
6 *
7 * Use of this software is governed by the MIT license
8 *
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
14 */
15
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/space.h>
19 #include <isl_seq.h>
20 #include <isl_mat_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_space_private.h>
23 #include <isl_val_private.h>
24
isl_mat_get_ctx(__isl_keep isl_mat * mat)25 isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
26 {
27 return mat ? mat->ctx : NULL;
28 }
29
30 /* Return a hash value that digests "mat".
31 */
isl_mat_get_hash(__isl_keep isl_mat * mat)32 uint32_t isl_mat_get_hash(__isl_keep isl_mat *mat)
33 {
34 int i;
35 uint32_t hash;
36
37 if (!mat)
38 return 0;
39
40 hash = isl_hash_init();
41 isl_hash_byte(hash, mat->n_row & 0xFF);
42 isl_hash_byte(hash, mat->n_col & 0xFF);
43 for (i = 0; i < mat->n_row; ++i) {
44 uint32_t row_hash;
45
46 row_hash = isl_seq_get_hash(mat->row[i], mat->n_col);
47 isl_hash_hash(hash, row_hash);
48 }
49
50 return hash;
51 }
52
isl_mat_alloc(isl_ctx * ctx,unsigned n_row,unsigned n_col)53 __isl_give isl_mat *isl_mat_alloc(isl_ctx *ctx,
54 unsigned n_row, unsigned n_col)
55 {
56 int i;
57 struct isl_mat *mat;
58
59 mat = isl_alloc_type(ctx, struct isl_mat);
60 if (!mat)
61 return NULL;
62
63 mat->row = NULL;
64 mat->block = isl_blk_alloc(ctx, n_row * n_col);
65 if (isl_blk_is_error(mat->block))
66 goto error;
67 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
68 if (n_row && !mat->row)
69 goto error;
70
71 for (i = 0; i < n_row; ++i)
72 mat->row[i] = mat->block.data + i * n_col;
73
74 mat->ctx = ctx;
75 isl_ctx_ref(ctx);
76 mat->ref = 1;
77 mat->n_row = n_row;
78 mat->n_col = n_col;
79 mat->max_col = n_col;
80 mat->flags = 0;
81
82 return mat;
83 error:
84 isl_blk_free(ctx, mat->block);
85 free(mat);
86 return NULL;
87 }
88
isl_mat_extend(__isl_take isl_mat * mat,unsigned n_row,unsigned n_col)89 __isl_give isl_mat *isl_mat_extend(__isl_take isl_mat *mat,
90 unsigned n_row, unsigned n_col)
91 {
92 int i;
93 isl_int *old;
94 isl_int **row;
95
96 if (!mat)
97 return NULL;
98
99 if (mat->max_col >= n_col && mat->n_row >= n_row) {
100 if (mat->n_col < n_col)
101 mat->n_col = n_col;
102 return mat;
103 }
104
105 if (mat->max_col < n_col) {
106 struct isl_mat *new_mat;
107
108 if (n_row < mat->n_row)
109 n_row = mat->n_row;
110 new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
111 if (!new_mat)
112 goto error;
113 for (i = 0; i < mat->n_row; ++i)
114 isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
115 isl_mat_free(mat);
116 return new_mat;
117 }
118
119 mat = isl_mat_cow(mat);
120 if (!mat)
121 goto error;
122
123 old = mat->block.data;
124 mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
125 if (isl_blk_is_error(mat->block))
126 goto error;
127 row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
128 if (n_row && !row)
129 goto error;
130 mat->row = row;
131
132 for (i = 0; i < mat->n_row; ++i)
133 mat->row[i] = mat->block.data + (mat->row[i] - old);
134 for (i = mat->n_row; i < n_row; ++i)
135 mat->row[i] = mat->block.data + i * mat->max_col;
136 mat->n_row = n_row;
137 if (mat->n_col < n_col)
138 mat->n_col = n_col;
139
140 return mat;
141 error:
142 isl_mat_free(mat);
143 return NULL;
144 }
145
isl_mat_sub_alloc6(isl_ctx * ctx,isl_int ** row,unsigned first_row,unsigned n_row,unsigned first_col,unsigned n_col)146 __isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
147 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
148 {
149 int i;
150 struct isl_mat *mat;
151
152 mat = isl_alloc_type(ctx, struct isl_mat);
153 if (!mat)
154 return NULL;
155 mat->row = isl_alloc_array(ctx, isl_int *, n_row);
156 if (n_row && !mat->row)
157 goto error;
158 for (i = 0; i < n_row; ++i)
159 mat->row[i] = row[first_row+i] + first_col;
160 mat->ctx = ctx;
161 isl_ctx_ref(ctx);
162 mat->ref = 1;
163 mat->n_row = n_row;
164 mat->n_col = n_col;
165 mat->block = isl_blk_empty();
166 mat->flags = ISL_MAT_BORROWED;
167 return mat;
168 error:
169 free(mat);
170 return NULL;
171 }
172
isl_mat_sub_alloc(__isl_keep isl_mat * mat,unsigned first_row,unsigned n_row,unsigned first_col,unsigned n_col)173 __isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
174 unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
175 {
176 if (!mat)
177 return NULL;
178 return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
179 first_col, n_col);
180 }
181
isl_mat_sub_copy(struct isl_ctx * ctx,isl_int ** dst,isl_int ** src,unsigned n_row,unsigned dst_col,unsigned src_col,unsigned n_col)182 void isl_mat_sub_copy(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
183 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
184 {
185 int i;
186
187 for (i = 0; i < n_row; ++i)
188 isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
189 }
190
isl_mat_sub_neg(struct isl_ctx * ctx,isl_int ** dst,isl_int ** src,unsigned n_row,unsigned dst_col,unsigned src_col,unsigned n_col)191 void isl_mat_sub_neg(struct isl_ctx *ctx, isl_int **dst, isl_int **src,
192 unsigned n_row, unsigned dst_col, unsigned src_col, unsigned n_col)
193 {
194 int i;
195
196 for (i = 0; i < n_row; ++i)
197 isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
198 }
199
isl_mat_copy(__isl_keep isl_mat * mat)200 __isl_give isl_mat *isl_mat_copy(__isl_keep isl_mat *mat)
201 {
202 if (!mat)
203 return NULL;
204
205 mat->ref++;
206 return mat;
207 }
208
isl_mat_dup(__isl_keep isl_mat * mat)209 __isl_give isl_mat *isl_mat_dup(__isl_keep isl_mat *mat)
210 {
211 int i;
212 struct isl_mat *mat2;
213
214 if (!mat)
215 return NULL;
216 mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
217 if (!mat2)
218 return NULL;
219 for (i = 0; i < mat->n_row; ++i)
220 isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
221 return mat2;
222 }
223
isl_mat_cow(__isl_take isl_mat * mat)224 __isl_give isl_mat *isl_mat_cow(__isl_take isl_mat *mat)
225 {
226 struct isl_mat *mat2;
227 if (!mat)
228 return NULL;
229
230 if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
231 return mat;
232
233 mat2 = isl_mat_dup(mat);
234 isl_mat_free(mat);
235 return mat2;
236 }
237
isl_mat_free(__isl_take isl_mat * mat)238 __isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
239 {
240 if (!mat)
241 return NULL;
242
243 if (--mat->ref > 0)
244 return NULL;
245
246 if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
247 isl_blk_free(mat->ctx, mat->block);
248 isl_ctx_deref(mat->ctx);
249 free(mat->row);
250 free(mat);
251
252 return NULL;
253 }
254
isl_mat_rows(__isl_keep isl_mat * mat)255 isl_size isl_mat_rows(__isl_keep isl_mat *mat)
256 {
257 return mat ? mat->n_row : isl_size_error;
258 }
259
isl_mat_cols(__isl_keep isl_mat * mat)260 isl_size isl_mat_cols(__isl_keep isl_mat *mat)
261 {
262 return mat ? mat->n_col : isl_size_error;
263 }
264
265 /* Check that "col" is a valid column position for "mat".
266 */
check_col(__isl_keep isl_mat * mat,int col)267 static isl_stat check_col(__isl_keep isl_mat *mat, int col)
268 {
269 if (!mat)
270 return isl_stat_error;
271 if (col < 0 || col >= mat->n_col)
272 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
273 "column out of range", return isl_stat_error);
274 return isl_stat_ok;
275 }
276
277 /* Check that "row" is a valid row position for "mat".
278 */
check_row(__isl_keep isl_mat * mat,int row)279 static isl_stat check_row(__isl_keep isl_mat *mat, int row)
280 {
281 if (!mat)
282 return isl_stat_error;
283 if (row < 0 || row >= mat->n_row)
284 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
285 "row out of range", return isl_stat_error);
286 return isl_stat_ok;
287 }
288
289 /* Check that there are "n" columns starting at position "first" in "mat".
290 */
check_col_range(__isl_keep isl_mat * mat,unsigned first,unsigned n)291 static isl_stat check_col_range(__isl_keep isl_mat *mat, unsigned first,
292 unsigned n)
293 {
294 if (!mat)
295 return isl_stat_error;
296 if (first + n > mat->n_col || first + n < first)
297 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
298 "column position or range out of bounds",
299 return isl_stat_error);
300 return isl_stat_ok;
301 }
302
303 /* Check that there are "n" rows starting at position "first" in "mat".
304 */
check_row_range(__isl_keep isl_mat * mat,unsigned first,unsigned n)305 static isl_stat check_row_range(__isl_keep isl_mat *mat, unsigned first,
306 unsigned n)
307 {
308 if (!mat)
309 return isl_stat_error;
310 if (first + n > mat->n_row || first + n < first)
311 isl_die(isl_mat_get_ctx(mat), isl_error_invalid,
312 "row position or range out of bounds",
313 return isl_stat_error);
314 return isl_stat_ok;
315 }
316
isl_mat_get_element(__isl_keep isl_mat * mat,int row,int col,isl_int * v)317 int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
318 {
319 if (check_row(mat, row) < 0)
320 return -1;
321 if (check_col(mat, col) < 0)
322 return -1;
323 isl_int_set(*v, mat->row[row][col]);
324 return 0;
325 }
326
327 /* Extract the element at row "row", oolumn "col" of "mat".
328 */
isl_mat_get_element_val(__isl_keep isl_mat * mat,int row,int col)329 __isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
330 int row, int col)
331 {
332 isl_ctx *ctx;
333
334 if (check_row(mat, row) < 0)
335 return NULL;
336 if (check_col(mat, col) < 0)
337 return NULL;
338 ctx = isl_mat_get_ctx(mat);
339 return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
340 }
341
isl_mat_set_element(__isl_take isl_mat * mat,int row,int col,isl_int v)342 __isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
343 int row, int col, isl_int v)
344 {
345 mat = isl_mat_cow(mat);
346 if (check_row(mat, row) < 0)
347 return isl_mat_free(mat);
348 if (check_col(mat, col) < 0)
349 return isl_mat_free(mat);
350 isl_int_set(mat->row[row][col], v);
351 return mat;
352 }
353
isl_mat_set_element_si(__isl_take isl_mat * mat,int row,int col,int v)354 __isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
355 int row, int col, int v)
356 {
357 mat = isl_mat_cow(mat);
358 if (check_row(mat, row) < 0)
359 return isl_mat_free(mat);
360 if (check_col(mat, col) < 0)
361 return isl_mat_free(mat);
362 isl_int_set_si(mat->row[row][col], v);
363 return mat;
364 }
365
366 /* Replace the element at row "row", column "col" of "mat" by "v".
367 */
isl_mat_set_element_val(__isl_take isl_mat * mat,int row,int col,__isl_take isl_val * v)368 __isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
369 int row, int col, __isl_take isl_val *v)
370 {
371 if (!v)
372 return isl_mat_free(mat);
373 if (!isl_val_is_int(v))
374 isl_die(isl_val_get_ctx(v), isl_error_invalid,
375 "expecting integer value", goto error);
376 mat = isl_mat_set_element(mat, row, col, v->n);
377 isl_val_free(v);
378 return mat;
379 error:
380 isl_val_free(v);
381 return isl_mat_free(mat);
382 }
383
isl_mat_diag(isl_ctx * ctx,unsigned n_row,isl_int d)384 __isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
385 {
386 int i;
387 struct isl_mat *mat;
388
389 mat = isl_mat_alloc(ctx, n_row, n_row);
390 if (!mat)
391 return NULL;
392 for (i = 0; i < n_row; ++i) {
393 isl_seq_clr(mat->row[i], i);
394 isl_int_set(mat->row[i][i], d);
395 isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
396 }
397
398 return mat;
399 }
400
401 /* Create an "n_row" by "n_col" matrix with zero elements.
402 */
isl_mat_zero(isl_ctx * ctx,unsigned n_row,unsigned n_col)403 __isl_give isl_mat *isl_mat_zero(isl_ctx *ctx, unsigned n_row, unsigned n_col)
404 {
405 int i;
406 isl_mat *mat;
407
408 mat = isl_mat_alloc(ctx, n_row, n_col);
409 if (!mat)
410 return NULL;
411 for (i = 0; i < n_row; ++i)
412 isl_seq_clr(mat->row[i], n_col);
413
414 return mat;
415 }
416
isl_mat_identity(isl_ctx * ctx,unsigned n_row)417 __isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
418 {
419 if (!ctx)
420 return NULL;
421 return isl_mat_diag(ctx, n_row, ctx->one);
422 }
423
424 /* Is "mat" a (possibly scaled) identity matrix?
425 */
isl_mat_is_scaled_identity(__isl_keep isl_mat * mat)426 isl_bool isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
427 {
428 int i;
429
430 if (!mat)
431 return isl_bool_error;
432 if (mat->n_row != mat->n_col)
433 return isl_bool_false;
434
435 for (i = 0; i < mat->n_row; ++i) {
436 if (isl_seq_first_non_zero(mat->row[i], i) != -1)
437 return isl_bool_false;
438 if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
439 return isl_bool_false;
440 if (isl_seq_first_non_zero(mat->row[i] + i + 1,
441 mat->n_col - (i + 1)) != -1)
442 return isl_bool_false;
443 }
444
445 return isl_bool_true;
446 }
447
isl_mat_vec_product(__isl_take isl_mat * mat,__isl_take isl_vec * vec)448 __isl_give isl_vec *isl_mat_vec_product(__isl_take isl_mat *mat,
449 __isl_take isl_vec *vec)
450 {
451 int i;
452 struct isl_vec *prod;
453
454 if (!mat || !vec)
455 goto error;
456
457 isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
458
459 prod = isl_vec_alloc(mat->ctx, mat->n_row);
460 if (!prod)
461 goto error;
462
463 for (i = 0; i < prod->size; ++i)
464 isl_seq_inner_product(mat->row[i], vec->el, vec->size,
465 &prod->block.data[i]);
466 isl_mat_free(mat);
467 isl_vec_free(vec);
468 return prod;
469 error:
470 isl_mat_free(mat);
471 isl_vec_free(vec);
472 return NULL;
473 }
474
isl_mat_vec_inverse_product(__isl_take isl_mat * mat,__isl_take isl_vec * vec)475 __isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
476 __isl_take isl_vec *vec)
477 {
478 struct isl_mat *vec_mat;
479 int i;
480
481 if (!mat || !vec)
482 goto error;
483 vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
484 if (!vec_mat)
485 goto error;
486 for (i = 0; i < vec->size; ++i)
487 isl_int_set(vec_mat->row[i][0], vec->el[i]);
488 vec_mat = isl_mat_inverse_product(mat, vec_mat);
489 isl_vec_free(vec);
490 if (!vec_mat)
491 return NULL;
492 vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
493 if (vec)
494 for (i = 0; i < vec->size; ++i)
495 isl_int_set(vec->el[i], vec_mat->row[i][0]);
496 isl_mat_free(vec_mat);
497 return vec;
498 error:
499 isl_mat_free(mat);
500 isl_vec_free(vec);
501 return NULL;
502 }
503
isl_vec_mat_product(__isl_take isl_vec * vec,__isl_take isl_mat * mat)504 __isl_give isl_vec *isl_vec_mat_product(__isl_take isl_vec *vec,
505 __isl_take isl_mat *mat)
506 {
507 int i, j;
508 struct isl_vec *prod;
509
510 if (!mat || !vec)
511 goto error;
512
513 isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
514
515 prod = isl_vec_alloc(mat->ctx, mat->n_col);
516 if (!prod)
517 goto error;
518
519 for (i = 0; i < prod->size; ++i) {
520 isl_int_set_si(prod->el[i], 0);
521 for (j = 0; j < vec->size; ++j)
522 isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
523 }
524 isl_mat_free(mat);
525 isl_vec_free(vec);
526 return prod;
527 error:
528 isl_mat_free(mat);
529 isl_vec_free(vec);
530 return NULL;
531 }
532
isl_mat_aff_direct_sum(__isl_take isl_mat * left,__isl_take isl_mat * right)533 __isl_give isl_mat *isl_mat_aff_direct_sum(__isl_take isl_mat *left,
534 __isl_take isl_mat *right)
535 {
536 int i;
537 struct isl_mat *sum;
538
539 if (!left || !right)
540 goto error;
541
542 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
543 isl_assert(left->ctx, left->n_row >= 1, goto error);
544 isl_assert(left->ctx, left->n_col >= 1, goto error);
545 isl_assert(left->ctx, right->n_col >= 1, goto error);
546 isl_assert(left->ctx,
547 isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
548 goto error);
549 isl_assert(left->ctx,
550 isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
551 goto error);
552
553 sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
554 if (!sum)
555 goto error;
556 isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
557 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
558 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
559
560 isl_seq_clr(sum->row[0]+1, sum->n_col-1);
561 for (i = 1; i < sum->n_row; ++i) {
562 isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
563 isl_int_addmul(sum->row[i][0],
564 right->row[0][0], right->row[i][0]);
565 isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
566 left->n_col-1);
567 isl_seq_scale(sum->row[i]+left->n_col,
568 right->row[i]+1, right->row[0][0],
569 right->n_col-1);
570 }
571
572 isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
573 isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
574 isl_mat_free(left);
575 isl_mat_free(right);
576 return sum;
577 error:
578 isl_mat_free(left);
579 isl_mat_free(right);
580 return NULL;
581 }
582
exchange(__isl_keep isl_mat * M,__isl_keep isl_mat ** U,__isl_keep isl_mat ** Q,unsigned row,unsigned i,unsigned j)583 static void exchange(__isl_keep isl_mat *M, __isl_keep isl_mat **U,
584 __isl_keep isl_mat **Q, unsigned row, unsigned i, unsigned j)
585 {
586 int r;
587 for (r = row; r < M->n_row; ++r)
588 isl_int_swap(M->row[r][i], M->row[r][j]);
589 if (U) {
590 for (r = 0; r < (*U)->n_row; ++r)
591 isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
592 }
593 if (Q)
594 isl_mat_swap_rows(*Q, i, j);
595 }
596
subtract(__isl_keep isl_mat * M,__isl_keep isl_mat ** U,__isl_keep isl_mat ** Q,unsigned row,unsigned i,unsigned j,isl_int m)597 static void subtract(__isl_keep isl_mat *M, __isl_keep isl_mat **U,
598 __isl_keep isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
599 {
600 int r;
601 for (r = row; r < M->n_row; ++r)
602 isl_int_submul(M->row[r][j], m, M->row[r][i]);
603 if (U) {
604 for (r = 0; r < (*U)->n_row; ++r)
605 isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
606 }
607 if (Q) {
608 for (r = 0; r < (*Q)->n_col; ++r)
609 isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
610 }
611 }
612
oppose(__isl_keep isl_mat * M,__isl_keep isl_mat ** U,__isl_keep isl_mat ** Q,unsigned row,unsigned col)613 static void oppose(__isl_keep isl_mat *M, __isl_keep isl_mat **U,
614 __isl_keep isl_mat **Q, unsigned row, unsigned col)
615 {
616 int r;
617 for (r = row; r < M->n_row; ++r)
618 isl_int_neg(M->row[r][col], M->row[r][col]);
619 if (U) {
620 for (r = 0; r < (*U)->n_row; ++r)
621 isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
622 }
623 if (Q)
624 isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
625 }
626
627 /* Given matrix M, compute
628 *
629 * M U = H
630 * M = H Q
631 *
632 * with U and Q unimodular matrices and H a matrix in column echelon form
633 * such that on each echelon row the entries in the non-echelon column
634 * are non-negative (if neg == 0) or non-positive (if neg == 1)
635 * and strictly smaller (in absolute value) than the entries in the echelon
636 * column.
637 * If U or Q are NULL, then these matrices are not computed.
638 */
isl_mat_left_hermite(__isl_take isl_mat * M,int neg,__isl_give isl_mat ** U,__isl_give isl_mat ** Q)639 __isl_give isl_mat *isl_mat_left_hermite(__isl_take isl_mat *M, int neg,
640 __isl_give isl_mat **U, __isl_give isl_mat **Q)
641 {
642 isl_int c;
643 int row, col;
644
645 if (U)
646 *U = NULL;
647 if (Q)
648 *Q = NULL;
649 if (!M)
650 goto error;
651 M = isl_mat_cow(M);
652 if (!M)
653 goto error;
654 if (U) {
655 *U = isl_mat_identity(M->ctx, M->n_col);
656 if (!*U)
657 goto error;
658 }
659 if (Q) {
660 *Q = isl_mat_identity(M->ctx, M->n_col);
661 if (!*Q)
662 goto error;
663 }
664
665 col = 0;
666 isl_int_init(c);
667 for (row = 0; row < M->n_row; ++row) {
668 int first, i, off;
669 first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
670 if (first == -1)
671 continue;
672 first += col;
673 if (first != col)
674 exchange(M, U, Q, row, first, col);
675 if (isl_int_is_neg(M->row[row][col]))
676 oppose(M, U, Q, row, col);
677 first = col+1;
678 while ((off = isl_seq_first_non_zero(M->row[row]+first,
679 M->n_col-first)) != -1) {
680 first += off;
681 isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
682 subtract(M, U, Q, row, col, first, c);
683 if (!isl_int_is_zero(M->row[row][first]))
684 exchange(M, U, Q, row, first, col);
685 else
686 ++first;
687 }
688 for (i = 0; i < col; ++i) {
689 if (isl_int_is_zero(M->row[row][i]))
690 continue;
691 if (neg)
692 isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
693 else
694 isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
695 if (isl_int_is_zero(c))
696 continue;
697 subtract(M, U, Q, row, col, i, c);
698 }
699 ++col;
700 }
701 isl_int_clear(c);
702
703 return M;
704 error:
705 if (Q) {
706 isl_mat_free(*Q);
707 *Q = NULL;
708 }
709 if (U) {
710 isl_mat_free(*U);
711 *U = NULL;
712 }
713 isl_mat_free(M);
714 return NULL;
715 }
716
717 /* Use row "row" of "mat" to eliminate column "col" from all other rows.
718 */
eliminate(__isl_take isl_mat * mat,int row,int col)719 static __isl_give isl_mat *eliminate(__isl_take isl_mat *mat, int row, int col)
720 {
721 int k;
722 isl_size nr, nc;
723 isl_ctx *ctx;
724
725 nr = isl_mat_rows(mat);
726 nc = isl_mat_cols(mat);
727 if (nr < 0 || nc < 0)
728 return isl_mat_free(mat);
729
730 ctx = isl_mat_get_ctx(mat);
731
732 for (k = 0; k < nr; ++k) {
733 if (k == row)
734 continue;
735 if (isl_int_is_zero(mat->row[k][col]))
736 continue;
737 mat = isl_mat_cow(mat);
738 if (!mat)
739 return NULL;
740 isl_seq_elim(mat->row[k], mat->row[row], col, nc, NULL);
741 isl_seq_normalize(ctx, mat->row[k], nc);
742 }
743
744 return mat;
745 }
746
747 /* Perform Gaussian elimination on the rows of "mat", but start
748 * from the final row and the final column.
749 * Any zero rows that result from the elimination are removed.
750 *
751 * In particular, for each column from last to first,
752 * look for the last row with a non-zero coefficient in that column,
753 * move it last (but before other rows moved last in previous steps) and
754 * use it to eliminate the column from the other rows.
755 */
isl_mat_reverse_gauss(__isl_take isl_mat * mat)756 __isl_give isl_mat *isl_mat_reverse_gauss(__isl_take isl_mat *mat)
757 {
758 int k, row, last;
759 isl_size nr, nc;
760
761 nr = isl_mat_rows(mat);
762 nc = isl_mat_cols(mat);
763 if (nr < 0 || nc < 0)
764 return isl_mat_free(mat);
765
766 last = nc - 1;
767 for (row = nr - 1; row >= 0; --row) {
768 for (; last >= 0; --last) {
769 for (k = row; k >= 0; --k)
770 if (!isl_int_is_zero(mat->row[k][last]))
771 break;
772 if (k >= 0)
773 break;
774 }
775 if (last < 0)
776 break;
777 if (k != row)
778 mat = isl_mat_swap_rows(mat, k, row);
779 if (!mat)
780 return NULL;
781 if (isl_int_is_neg(mat->row[row][last]))
782 mat = isl_mat_row_neg(mat, row);
783 mat = eliminate(mat, row, last);
784 if (!mat)
785 return NULL;
786 }
787 mat = isl_mat_drop_rows(mat, 0, row + 1);
788
789 return mat;
790 }
791
792 /* Negate the lexicographically negative rows of "mat" such that
793 * all rows in the result are lexicographically non-negative.
794 */
isl_mat_lexnonneg_rows(__isl_take isl_mat * mat)795 __isl_give isl_mat *isl_mat_lexnonneg_rows(__isl_take isl_mat *mat)
796 {
797 int i;
798 isl_size nr, nc;
799
800 nr = isl_mat_rows(mat);
801 nc = isl_mat_cols(mat);
802 if (nr < 0 || nc < 0)
803 return isl_mat_free(mat);
804
805 for (i = 0; i < nr; ++i) {
806 int pos;
807
808 pos = isl_seq_first_non_zero(mat->row[i], nc);
809 if (pos < 0)
810 continue;
811 if (isl_int_is_nonneg(mat->row[i][pos]))
812 continue;
813 mat = isl_mat_row_neg(mat, i);
814 if (!mat)
815 return NULL;
816 }
817
818 return mat;
819 }
820
821 /* Given a matrix "H" is column echelon form, what is the first
822 * zero column? That is how many initial columns are non-zero?
823 * Start looking at column "first_col" and only consider
824 * the columns to be of size "n_row".
825 * "H" is assumed to be non-NULL.
826 *
827 * Since "H" is in column echelon form, the first non-zero entry
828 * in a column is always in a later position compared to the previous column.
829 */
hermite_first_zero_col(__isl_keep isl_mat * H,int first_col,int n_row)830 static int hermite_first_zero_col(__isl_keep isl_mat *H, int first_col,
831 int n_row)
832 {
833 int row, col;
834
835 for (col = first_col, row = 0; col < H->n_col; ++col) {
836 for (; row < n_row; ++row)
837 if (!isl_int_is_zero(H->row[row][col]))
838 break;
839 if (row == n_row)
840 return col;
841 }
842
843 return H->n_col;
844 }
845
846 /* Return the rank of "mat", or isl_size_error in case of error.
847 */
isl_mat_rank(__isl_keep isl_mat * mat)848 isl_size isl_mat_rank(__isl_keep isl_mat *mat)
849 {
850 int rank;
851 isl_mat *H;
852
853 H = isl_mat_left_hermite(isl_mat_copy(mat), 0, NULL, NULL);
854 if (!H)
855 return isl_size_error;
856
857 rank = hermite_first_zero_col(H, 0, H->n_row);
858 isl_mat_free(H);
859
860 return rank;
861 }
862
isl_mat_right_kernel(__isl_take isl_mat * mat)863 __isl_give isl_mat *isl_mat_right_kernel(__isl_take isl_mat *mat)
864 {
865 int rank;
866 struct isl_mat *U = NULL;
867 struct isl_mat *K;
868
869 mat = isl_mat_left_hermite(mat, 0, &U, NULL);
870 if (!mat || !U)
871 goto error;
872
873 rank = hermite_first_zero_col(mat, 0, mat->n_row);
874 K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
875 if (!K)
876 goto error;
877 isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
878 isl_mat_free(mat);
879 isl_mat_free(U);
880 return K;
881 error:
882 isl_mat_free(mat);
883 isl_mat_free(U);
884 return NULL;
885 }
886
isl_mat_lin_to_aff(__isl_take isl_mat * mat)887 __isl_give isl_mat *isl_mat_lin_to_aff(__isl_take isl_mat *mat)
888 {
889 int i;
890 struct isl_mat *mat2;
891
892 if (!mat)
893 return NULL;
894 mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
895 if (!mat2)
896 goto error;
897 isl_int_set_si(mat2->row[0][0], 1);
898 isl_seq_clr(mat2->row[0]+1, mat->n_col);
899 for (i = 0; i < mat->n_row; ++i) {
900 isl_int_set_si(mat2->row[1+i][0], 0);
901 isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
902 }
903 isl_mat_free(mat);
904 return mat2;
905 error:
906 isl_mat_free(mat);
907 return NULL;
908 }
909
910 /* Given two matrices M1 and M2, return the block matrix
911 *
912 * [ M1 0 ]
913 * [ 0 M2 ]
914 */
isl_mat_diagonal(__isl_take isl_mat * mat1,__isl_take isl_mat * mat2)915 __isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
916 __isl_take isl_mat *mat2)
917 {
918 int i;
919 isl_mat *mat;
920
921 if (!mat1 || !mat2)
922 goto error;
923
924 mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
925 mat1->n_col + mat2->n_col);
926 if (!mat)
927 goto error;
928 for (i = 0; i < mat1->n_row; ++i) {
929 isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
930 isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
931 }
932 for (i = 0; i < mat2->n_row; ++i) {
933 isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
934 isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
935 mat2->row[i], mat2->n_col);
936 }
937 isl_mat_free(mat1);
938 isl_mat_free(mat2);
939 return mat;
940 error:
941 isl_mat_free(mat1);
942 isl_mat_free(mat2);
943 return NULL;
944 }
945
row_first_non_zero(isl_int ** row,unsigned n_row,unsigned col)946 static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
947 {
948 int i;
949
950 for (i = 0; i < n_row; ++i)
951 if (!isl_int_is_zero(row[i][col]))
952 return i;
953 return -1;
954 }
955
row_abs_min_non_zero(isl_int ** row,unsigned n_row,unsigned col)956 static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
957 {
958 int i, min = row_first_non_zero(row, n_row, col);
959 if (min < 0)
960 return -1;
961 for (i = min + 1; i < n_row; ++i) {
962 if (isl_int_is_zero(row[i][col]))
963 continue;
964 if (isl_int_abs_lt(row[i][col], row[min][col]))
965 min = i;
966 }
967 return min;
968 }
969
inv_exchange(__isl_keep isl_mat ** left,__isl_keep isl_mat ** right,unsigned i,unsigned j)970 static isl_stat inv_exchange(__isl_keep isl_mat **left,
971 __isl_keep isl_mat **right, unsigned i, unsigned j)
972 {
973 *left = isl_mat_swap_rows(*left, i, j);
974 *right = isl_mat_swap_rows(*right, i, j);
975
976 if (!*left || !*right)
977 return isl_stat_error;
978 return isl_stat_ok;
979 }
980
inv_oppose(__isl_keep isl_mat * left,__isl_keep isl_mat * right,unsigned row)981 static void inv_oppose(
982 __isl_keep isl_mat *left, __isl_keep isl_mat *right, unsigned row)
983 {
984 isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
985 isl_seq_neg(right->row[row], right->row[row], right->n_col);
986 }
987
inv_subtract(__isl_keep isl_mat * left,__isl_keep isl_mat * right,unsigned row,unsigned i,isl_int m)988 static void inv_subtract(__isl_keep isl_mat *left, __isl_keep isl_mat *right,
989 unsigned row, unsigned i, isl_int m)
990 {
991 isl_int_neg(m, m);
992 isl_seq_combine(left->row[i]+row,
993 left->ctx->one, left->row[i]+row,
994 m, left->row[row]+row,
995 left->n_col-row);
996 isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
997 m, right->row[row], right->n_col);
998 }
999
1000 /* Compute inv(left)*right
1001 */
isl_mat_inverse_product(__isl_take isl_mat * left,__isl_take isl_mat * right)1002 __isl_give isl_mat *isl_mat_inverse_product(__isl_take isl_mat *left,
1003 __isl_take isl_mat *right)
1004 {
1005 int row;
1006 isl_int a, b;
1007
1008 if (!left || !right)
1009 goto error;
1010
1011 isl_assert(left->ctx, left->n_row == left->n_col, goto error);
1012 isl_assert(left->ctx, left->n_row == right->n_row, goto error);
1013
1014 if (left->n_row == 0) {
1015 isl_mat_free(left);
1016 return right;
1017 }
1018
1019 left = isl_mat_cow(left);
1020 right = isl_mat_cow(right);
1021 if (!left || !right)
1022 goto error;
1023
1024 isl_int_init(a);
1025 isl_int_init(b);
1026 for (row = 0; row < left->n_row; ++row) {
1027 int pivot, first, i, off;
1028 pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
1029 if (pivot < 0) {
1030 isl_int_clear(a);
1031 isl_int_clear(b);
1032 isl_assert(left->ctx, pivot >= 0, goto error);
1033 }
1034 pivot += row;
1035 if (pivot != row)
1036 if (inv_exchange(&left, &right, pivot, row) < 0)
1037 goto error;
1038 if (isl_int_is_neg(left->row[row][row]))
1039 inv_oppose(left, right, row);
1040 first = row+1;
1041 while ((off = row_first_non_zero(left->row+first,
1042 left->n_row-first, row)) != -1) {
1043 first += off;
1044 isl_int_fdiv_q(a, left->row[first][row],
1045 left->row[row][row]);
1046 inv_subtract(left, right, row, first, a);
1047 if (!isl_int_is_zero(left->row[first][row])) {
1048 if (inv_exchange(&left, &right, row, first) < 0)
1049 goto error;
1050 } else {
1051 ++first;
1052 }
1053 }
1054 for (i = 0; i < row; ++i) {
1055 if (isl_int_is_zero(left->row[i][row]))
1056 continue;
1057 isl_int_gcd(a, left->row[row][row], left->row[i][row]);
1058 isl_int_divexact(b, left->row[i][row], a);
1059 isl_int_divexact(a, left->row[row][row], a);
1060 isl_int_neg(b, b);
1061 isl_seq_combine(left->row[i] + i,
1062 a, left->row[i] + i,
1063 b, left->row[row] + i,
1064 left->n_col - i);
1065 isl_seq_combine(right->row[i], a, right->row[i],
1066 b, right->row[row], right->n_col);
1067 }
1068 }
1069 isl_int_clear(b);
1070
1071 isl_int_set(a, left->row[0][0]);
1072 for (row = 1; row < left->n_row; ++row)
1073 isl_int_lcm(a, a, left->row[row][row]);
1074 if (isl_int_is_zero(a)){
1075 isl_int_clear(a);
1076 isl_assert(left->ctx, 0, goto error);
1077 }
1078 for (row = 0; row < left->n_row; ++row) {
1079 isl_int_divexact(left->row[row][row], a, left->row[row][row]);
1080 if (isl_int_is_one(left->row[row][row]))
1081 continue;
1082 isl_seq_scale(right->row[row], right->row[row],
1083 left->row[row][row], right->n_col);
1084 }
1085 isl_int_clear(a);
1086
1087 isl_mat_free(left);
1088 return right;
1089 error:
1090 isl_mat_free(left);
1091 isl_mat_free(right);
1092 return NULL;
1093 }
1094
isl_mat_col_scale(__isl_keep isl_mat * mat,unsigned col,isl_int m)1095 void isl_mat_col_scale(__isl_keep isl_mat *mat, unsigned col, isl_int m)
1096 {
1097 int i;
1098
1099 for (i = 0; i < mat->n_row; ++i)
1100 isl_int_mul(mat->row[i][col], mat->row[i][col], m);
1101 }
1102
isl_mat_col_combine(__isl_keep isl_mat * mat,unsigned dst,isl_int m1,unsigned src1,isl_int m2,unsigned src2)1103 void isl_mat_col_combine(__isl_keep isl_mat *mat, unsigned dst,
1104 isl_int m1, unsigned src1, isl_int m2, unsigned src2)
1105 {
1106 int i;
1107 isl_int tmp;
1108
1109 isl_int_init(tmp);
1110 for (i = 0; i < mat->n_row; ++i) {
1111 isl_int_mul(tmp, m1, mat->row[i][src1]);
1112 isl_int_addmul(tmp, m2, mat->row[i][src2]);
1113 isl_int_set(mat->row[i][dst], tmp);
1114 }
1115 isl_int_clear(tmp);
1116 }
1117
isl_mat_right_inverse(__isl_take isl_mat * mat)1118 __isl_give isl_mat *isl_mat_right_inverse(__isl_take isl_mat *mat)
1119 {
1120 struct isl_mat *inv;
1121 int row;
1122 isl_int a, b;
1123
1124 mat = isl_mat_cow(mat);
1125 if (!mat)
1126 return NULL;
1127
1128 inv = isl_mat_identity(mat->ctx, mat->n_col);
1129 inv = isl_mat_cow(inv);
1130 if (!inv)
1131 goto error;
1132
1133 isl_int_init(a);
1134 isl_int_init(b);
1135 for (row = 0; row < mat->n_row; ++row) {
1136 int pivot, first, i, off;
1137 pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
1138 if (pivot < 0) {
1139 isl_int_clear(a);
1140 isl_int_clear(b);
1141 isl_assert(mat->ctx, pivot >= 0, goto error);
1142 }
1143 pivot += row;
1144 if (pivot != row)
1145 exchange(mat, &inv, NULL, row, pivot, row);
1146 if (isl_int_is_neg(mat->row[row][row]))
1147 oppose(mat, &inv, NULL, row, row);
1148 first = row+1;
1149 while ((off = isl_seq_first_non_zero(mat->row[row]+first,
1150 mat->n_col-first)) != -1) {
1151 first += off;
1152 isl_int_fdiv_q(a, mat->row[row][first],
1153 mat->row[row][row]);
1154 subtract(mat, &inv, NULL, row, row, first, a);
1155 if (!isl_int_is_zero(mat->row[row][first]))
1156 exchange(mat, &inv, NULL, row, row, first);
1157 else
1158 ++first;
1159 }
1160 for (i = 0; i < row; ++i) {
1161 if (isl_int_is_zero(mat->row[row][i]))
1162 continue;
1163 isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
1164 isl_int_divexact(b, mat->row[row][i], a);
1165 isl_int_divexact(a, mat->row[row][row], a);
1166 isl_int_neg(a, a);
1167 isl_mat_col_combine(mat, i, a, i, b, row);
1168 isl_mat_col_combine(inv, i, a, i, b, row);
1169 }
1170 }
1171 isl_int_clear(b);
1172
1173 isl_int_set(a, mat->row[0][0]);
1174 for (row = 1; row < mat->n_row; ++row)
1175 isl_int_lcm(a, a, mat->row[row][row]);
1176 if (isl_int_is_zero(a)){
1177 isl_int_clear(a);
1178 goto error;
1179 }
1180 for (row = 0; row < mat->n_row; ++row) {
1181 isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
1182 if (isl_int_is_one(mat->row[row][row]))
1183 continue;
1184 isl_mat_col_scale(inv, row, mat->row[row][row]);
1185 }
1186 isl_int_clear(a);
1187
1188 isl_mat_free(mat);
1189
1190 return inv;
1191 error:
1192 isl_mat_free(mat);
1193 isl_mat_free(inv);
1194 return NULL;
1195 }
1196
isl_mat_transpose(__isl_take isl_mat * mat)1197 __isl_give isl_mat *isl_mat_transpose(__isl_take isl_mat *mat)
1198 {
1199 struct isl_mat *transpose = NULL;
1200 int i, j;
1201
1202 if (!mat)
1203 return NULL;
1204
1205 if (mat->n_col == mat->n_row) {
1206 mat = isl_mat_cow(mat);
1207 if (!mat)
1208 return NULL;
1209 for (i = 0; i < mat->n_row; ++i)
1210 for (j = i + 1; j < mat->n_col; ++j)
1211 isl_int_swap(mat->row[i][j], mat->row[j][i]);
1212 return mat;
1213 }
1214 transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
1215 if (!transpose)
1216 goto error;
1217 for (i = 0; i < mat->n_row; ++i)
1218 for (j = 0; j < mat->n_col; ++j)
1219 isl_int_set(transpose->row[j][i], mat->row[i][j]);
1220 isl_mat_free(mat);
1221 return transpose;
1222 error:
1223 isl_mat_free(mat);
1224 return NULL;
1225 }
1226
isl_mat_swap_cols(__isl_take isl_mat * mat,unsigned i,unsigned j)1227 __isl_give isl_mat *isl_mat_swap_cols(__isl_take isl_mat *mat,
1228 unsigned i, unsigned j)
1229 {
1230 int r;
1231
1232 mat = isl_mat_cow(mat);
1233 if (check_col_range(mat, i, 1) < 0 ||
1234 check_col_range(mat, j, 1) < 0)
1235 return isl_mat_free(mat);
1236
1237 for (r = 0; r < mat->n_row; ++r)
1238 isl_int_swap(mat->row[r][i], mat->row[r][j]);
1239 return mat;
1240 }
1241
isl_mat_swap_rows(__isl_take isl_mat * mat,unsigned i,unsigned j)1242 __isl_give isl_mat *isl_mat_swap_rows(__isl_take isl_mat *mat,
1243 unsigned i, unsigned j)
1244 {
1245 isl_int *t;
1246
1247 if (!mat)
1248 return NULL;
1249 mat = isl_mat_cow(mat);
1250 if (check_row_range(mat, i, 1) < 0 ||
1251 check_row_range(mat, j, 1) < 0)
1252 return isl_mat_free(mat);
1253
1254 t = mat->row[i];
1255 mat->row[i] = mat->row[j];
1256 mat->row[j] = t;
1257 return mat;
1258 }
1259
1260 /* Calculate the product of two matrices.
1261 *
1262 * This function is optimized for operand matrices that contain many zeros and
1263 * skips multiplications where we know one of the operands is zero.
1264 */
isl_mat_product(__isl_take isl_mat * left,__isl_take isl_mat * right)1265 __isl_give isl_mat *isl_mat_product(__isl_take isl_mat *left,
1266 __isl_take isl_mat *right)
1267 {
1268 int i, j, k;
1269 struct isl_mat *prod;
1270
1271 if (!left || !right)
1272 goto error;
1273 isl_assert(left->ctx, left->n_col == right->n_row, goto error);
1274 prod = isl_mat_alloc(left->ctx, left->n_row, right->n_col);
1275 if (!prod)
1276 goto error;
1277 if (left->n_col == 0) {
1278 for (i = 0; i < prod->n_row; ++i)
1279 isl_seq_clr(prod->row[i], prod->n_col);
1280 isl_mat_free(left);
1281 isl_mat_free(right);
1282 return prod;
1283 }
1284 for (i = 0; i < prod->n_row; ++i) {
1285 for (j = 0; j < prod->n_col; ++j)
1286 isl_int_mul(prod->row[i][j],
1287 left->row[i][0], right->row[0][j]);
1288 for (k = 1; k < left->n_col; ++k) {
1289 if (isl_int_is_zero(left->row[i][k]))
1290 continue;
1291 for (j = 0; j < prod->n_col; ++j)
1292 isl_int_addmul(prod->row[i][j],
1293 left->row[i][k], right->row[k][j]);
1294 }
1295 }
1296 isl_mat_free(left);
1297 isl_mat_free(right);
1298 return prod;
1299 error:
1300 isl_mat_free(left);
1301 isl_mat_free(right);
1302 return NULL;
1303 }
1304
1305 /* Replace the variables x in the rows q by x' given by x = M x',
1306 * with M the matrix mat.
1307 *
1308 * If the number of new variables is greater than the original
1309 * number of variables, then the rows q have already been
1310 * preextended. If the new number is smaller, then the coefficients
1311 * of the divs, which are not changed, need to be shifted down.
1312 * The row q may be the equalities, the inequalities or the
1313 * div expressions. In the latter case, has_div is true and
1314 * we need to take into account the extra denominator column.
1315 */
preimage(struct isl_ctx * ctx,isl_int ** q,unsigned n,unsigned n_div,int has_div,struct isl_mat * mat)1316 static int preimage(struct isl_ctx *ctx, isl_int **q, unsigned n,
1317 unsigned n_div, int has_div, struct isl_mat *mat)
1318 {
1319 int i;
1320 struct isl_mat *t;
1321 int e;
1322
1323 if (mat->n_col >= mat->n_row)
1324 e = 0;
1325 else
1326 e = mat->n_row - mat->n_col;
1327 if (has_div)
1328 for (i = 0; i < n; ++i)
1329 isl_int_mul(q[i][0], q[i][0], mat->row[0][0]);
1330 t = isl_mat_sub_alloc6(mat->ctx, q, 0, n, has_div, mat->n_row);
1331 t = isl_mat_product(t, mat);
1332 if (!t)
1333 return -1;
1334 for (i = 0; i < n; ++i) {
1335 isl_seq_swp_or_cpy(q[i] + has_div, t->row[i], t->n_col);
1336 isl_seq_cpy(q[i] + has_div + t->n_col,
1337 q[i] + has_div + t->n_col + e, n_div);
1338 isl_seq_clr(q[i] + has_div + t->n_col + n_div, e);
1339 }
1340 isl_mat_free(t);
1341 return 0;
1342 }
1343
1344 /* Replace the variables x in bset by x' given by x = M x', with
1345 * M the matrix mat.
1346 *
1347 * If there are fewer variables x' then there are x, then we perform
1348 * the transformation in place, which means that, in principle,
1349 * this frees up some extra variables as the number
1350 * of columns remains constant, but we would have to extend
1351 * the div array too as the number of rows in this array is assumed
1352 * to be equal to extra.
1353 */
isl_basic_set_preimage(__isl_take isl_basic_set * bset,__isl_take isl_mat * mat)1354 __isl_give isl_basic_set *isl_basic_set_preimage(
1355 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
1356 {
1357 struct isl_ctx *ctx;
1358
1359 if (!bset || !mat)
1360 goto error;
1361
1362 ctx = bset->ctx;
1363 bset = isl_basic_set_cow(bset);
1364 if (isl_basic_set_check_no_params(bset) < 0)
1365 goto error;
1366
1367 isl_assert(ctx, 1+bset->dim->n_out == mat->n_row, goto error);
1368 isl_assert(ctx, mat->n_col > 0, goto error);
1369
1370 if (mat->n_col > mat->n_row) {
1371 bset = isl_basic_set_add_dims(bset, isl_dim_set,
1372 mat->n_col - mat->n_row);
1373 if (!bset)
1374 goto error;
1375 } else if (mat->n_col < mat->n_row) {
1376 bset->dim = isl_space_cow(bset->dim);
1377 if (!bset->dim)
1378 goto error;
1379 bset->dim->n_out -= mat->n_row - mat->n_col;
1380 }
1381
1382 if (preimage(ctx, bset->eq, bset->n_eq, bset->n_div, 0,
1383 isl_mat_copy(mat)) < 0)
1384 goto error;
1385
1386 if (preimage(ctx, bset->ineq, bset->n_ineq, bset->n_div, 0,
1387 isl_mat_copy(mat)) < 0)
1388 goto error;
1389
1390 if (preimage(ctx, bset->div, bset->n_div, bset->n_div, 1, mat) < 0)
1391 goto error2;
1392
1393 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
1394 ISL_F_CLR(bset, ISL_BASIC_SET_NO_REDUNDANT);
1395 ISL_F_CLR(bset, ISL_BASIC_SET_SORTED);
1396 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED_DIVS);
1397 ISL_F_CLR(bset, ISL_BASIC_SET_ALL_EQUALITIES);
1398
1399 bset = isl_basic_set_simplify(bset);
1400 bset = isl_basic_set_finalize(bset);
1401
1402 return bset;
1403 error:
1404 isl_mat_free(mat);
1405 error2:
1406 isl_basic_set_free(bset);
1407 return NULL;
1408 }
1409
isl_set_preimage(__isl_take isl_set * set,__isl_take isl_mat * mat)1410 __isl_give isl_set *isl_set_preimage(
1411 __isl_take isl_set *set, __isl_take isl_mat *mat)
1412 {
1413 int i;
1414
1415 set = isl_set_cow(set);
1416 if (!set)
1417 goto error;
1418
1419 for (i = 0; i < set->n; ++i) {
1420 set->p[i] = isl_basic_set_preimage(set->p[i],
1421 isl_mat_copy(mat));
1422 if (!set->p[i])
1423 goto error;
1424 }
1425 if (mat->n_col != mat->n_row) {
1426 set->dim = isl_space_cow(set->dim);
1427 if (!set->dim)
1428 goto error;
1429 set->dim->n_out += mat->n_col;
1430 set->dim->n_out -= mat->n_row;
1431 }
1432 isl_mat_free(mat);
1433 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1434 return set;
1435 error:
1436 isl_set_free(set);
1437 isl_mat_free(mat);
1438 return NULL;
1439 }
1440
1441 /* Replace the variables x starting at "first_col" in the rows "rows"
1442 * of some coefficient matrix by x' with x = M x' with M the matrix mat.
1443 * That is, replace the corresponding coefficients c by c M.
1444 */
isl_mat_sub_transform(isl_int ** row,unsigned n_row,unsigned first_col,__isl_take isl_mat * mat)1445 isl_stat isl_mat_sub_transform(isl_int **row, unsigned n_row,
1446 unsigned first_col, __isl_take isl_mat *mat)
1447 {
1448 int i;
1449 isl_ctx *ctx;
1450 isl_mat *t;
1451
1452 if (!mat)
1453 return isl_stat_error;
1454 ctx = isl_mat_get_ctx(mat);
1455 t = isl_mat_sub_alloc6(ctx, row, 0, n_row, first_col, mat->n_row);
1456 t = isl_mat_product(t, mat);
1457 if (!t)
1458 return isl_stat_error;
1459 for (i = 0; i < n_row; ++i)
1460 isl_seq_swp_or_cpy(row[i] + first_col, t->row[i], t->n_col);
1461 isl_mat_free(t);
1462 return isl_stat_ok;
1463 }
1464
isl_mat_print_internal(__isl_keep isl_mat * mat,FILE * out,int indent)1465 void isl_mat_print_internal(__isl_keep isl_mat *mat, FILE *out, int indent)
1466 {
1467 int i, j;
1468
1469 if (!mat) {
1470 fprintf(out, "%*snull mat\n", indent, "");
1471 return;
1472 }
1473
1474 if (mat->n_row == 0)
1475 fprintf(out, "%*s[]\n", indent, "");
1476
1477 for (i = 0; i < mat->n_row; ++i) {
1478 if (!i)
1479 fprintf(out, "%*s[[", indent, "");
1480 else
1481 fprintf(out, "%*s[", indent+1, "");
1482 for (j = 0; j < mat->n_col; ++j) {
1483 if (j)
1484 fprintf(out, ",");
1485 isl_int_print(out, mat->row[i][j], 0);
1486 }
1487 if (i == mat->n_row-1)
1488 fprintf(out, "]]\n");
1489 else
1490 fprintf(out, "]\n");
1491 }
1492 }
1493
isl_mat_dump(__isl_keep isl_mat * mat)1494 void isl_mat_dump(__isl_keep isl_mat *mat)
1495 {
1496 isl_mat_print_internal(mat, stderr, 0);
1497 }
1498
isl_mat_drop_cols(__isl_take isl_mat * mat,unsigned col,unsigned n)1499 __isl_give isl_mat *isl_mat_drop_cols(__isl_take isl_mat *mat,
1500 unsigned col, unsigned n)
1501 {
1502 int r;
1503
1504 if (n == 0)
1505 return mat;
1506
1507 mat = isl_mat_cow(mat);
1508 if (check_col_range(mat, col, n) < 0)
1509 return isl_mat_free(mat);
1510
1511 if (col != mat->n_col-n) {
1512 for (r = 0; r < mat->n_row; ++r)
1513 isl_seq_cpy(mat->row[r]+col, mat->row[r]+col+n,
1514 mat->n_col - col - n);
1515 }
1516 mat->n_col -= n;
1517 return mat;
1518 }
1519
isl_mat_drop_rows(__isl_take isl_mat * mat,unsigned row,unsigned n)1520 __isl_give isl_mat *isl_mat_drop_rows(__isl_take isl_mat *mat,
1521 unsigned row, unsigned n)
1522 {
1523 int r;
1524
1525 mat = isl_mat_cow(mat);
1526 if (check_row_range(mat, row, n) < 0)
1527 return isl_mat_free(mat);
1528
1529 for (r = row; r+n < mat->n_row; ++r)
1530 mat->row[r] = mat->row[r+n];
1531
1532 mat->n_row -= n;
1533 return mat;
1534 }
1535
isl_mat_insert_cols(__isl_take isl_mat * mat,unsigned col,unsigned n)1536 __isl_give isl_mat *isl_mat_insert_cols(__isl_take isl_mat *mat,
1537 unsigned col, unsigned n)
1538 {
1539 isl_mat *ext;
1540
1541 if (check_col_range(mat, col, 0) < 0)
1542 return isl_mat_free(mat);
1543 if (n == 0)
1544 return mat;
1545
1546 ext = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col + n);
1547 if (!ext)
1548 goto error;
1549
1550 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row, 0, 0, col);
1551 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, mat->n_row,
1552 col + n, col, mat->n_col - col);
1553
1554 isl_mat_free(mat);
1555 return ext;
1556 error:
1557 isl_mat_free(mat);
1558 return NULL;
1559 }
1560
isl_mat_insert_zero_cols(__isl_take isl_mat * mat,unsigned first,unsigned n)1561 __isl_give isl_mat *isl_mat_insert_zero_cols(__isl_take isl_mat *mat,
1562 unsigned first, unsigned n)
1563 {
1564 int i;
1565
1566 if (!mat)
1567 return NULL;
1568 mat = isl_mat_insert_cols(mat, first, n);
1569 if (!mat)
1570 return NULL;
1571
1572 for (i = 0; i < mat->n_row; ++i)
1573 isl_seq_clr(mat->row[i] + first, n);
1574
1575 return mat;
1576 }
1577
isl_mat_add_zero_cols(__isl_take isl_mat * mat,unsigned n)1578 __isl_give isl_mat *isl_mat_add_zero_cols(__isl_take isl_mat *mat, unsigned n)
1579 {
1580 if (!mat)
1581 return NULL;
1582
1583 return isl_mat_insert_zero_cols(mat, mat->n_col, n);
1584 }
1585
isl_mat_insert_rows(__isl_take isl_mat * mat,unsigned row,unsigned n)1586 __isl_give isl_mat *isl_mat_insert_rows(__isl_take isl_mat *mat,
1587 unsigned row, unsigned n)
1588 {
1589 isl_mat *ext;
1590
1591 if (check_row_range(mat, row, 0) < 0)
1592 return isl_mat_free(mat);
1593 if (n == 0)
1594 return mat;
1595
1596 ext = isl_mat_alloc(mat->ctx, mat->n_row + n, mat->n_col);
1597 if (!ext)
1598 goto error;
1599
1600 isl_mat_sub_copy(mat->ctx, ext->row, mat->row, row, 0, 0, mat->n_col);
1601 isl_mat_sub_copy(mat->ctx, ext->row + row + n, mat->row + row,
1602 mat->n_row - row, 0, 0, mat->n_col);
1603
1604 isl_mat_free(mat);
1605 return ext;
1606 error:
1607 isl_mat_free(mat);
1608 return NULL;
1609 }
1610
isl_mat_add_rows(__isl_take isl_mat * mat,unsigned n)1611 __isl_give isl_mat *isl_mat_add_rows(__isl_take isl_mat *mat, unsigned n)
1612 {
1613 if (!mat)
1614 return NULL;
1615
1616 return isl_mat_insert_rows(mat, mat->n_row, n);
1617 }
1618
isl_mat_insert_zero_rows(__isl_take isl_mat * mat,unsigned row,unsigned n)1619 __isl_give isl_mat *isl_mat_insert_zero_rows(__isl_take isl_mat *mat,
1620 unsigned row, unsigned n)
1621 {
1622 int i;
1623
1624 mat = isl_mat_insert_rows(mat, row, n);
1625 if (!mat)
1626 return NULL;
1627
1628 for (i = 0; i < n; ++i)
1629 isl_seq_clr(mat->row[row + i], mat->n_col);
1630
1631 return mat;
1632 }
1633
isl_mat_add_zero_rows(__isl_take isl_mat * mat,unsigned n)1634 __isl_give isl_mat *isl_mat_add_zero_rows(__isl_take isl_mat *mat, unsigned n)
1635 {
1636 if (!mat)
1637 return NULL;
1638
1639 return isl_mat_insert_zero_rows(mat, mat->n_row, n);
1640 }
1641
isl_mat_col_submul(__isl_keep isl_mat * mat,int dst_col,isl_int f,int src_col)1642 void isl_mat_col_submul(__isl_keep isl_mat *mat,
1643 int dst_col, isl_int f, int src_col)
1644 {
1645 int i;
1646
1647 for (i = 0; i < mat->n_row; ++i)
1648 isl_int_submul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1649 }
1650
isl_mat_col_add(__isl_keep isl_mat * mat,int dst_col,int src_col)1651 void isl_mat_col_add(__isl_keep isl_mat *mat, int dst_col, int src_col)
1652 {
1653 int i;
1654
1655 if (!mat)
1656 return;
1657
1658 for (i = 0; i < mat->n_row; ++i)
1659 isl_int_add(mat->row[i][dst_col],
1660 mat->row[i][dst_col], mat->row[i][src_col]);
1661 }
1662
isl_mat_col_mul(__isl_keep isl_mat * mat,int dst_col,isl_int f,int src_col)1663 void isl_mat_col_mul(__isl_keep isl_mat *mat, int dst_col, isl_int f,
1664 int src_col)
1665 {
1666 int i;
1667
1668 for (i = 0; i < mat->n_row; ++i)
1669 isl_int_mul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1670 }
1671
1672 /* Add "f" times column "src_col" to column "dst_col" of "mat" and
1673 * return the result.
1674 */
isl_mat_col_addmul(__isl_take isl_mat * mat,int dst_col,isl_int f,int src_col)1675 __isl_give isl_mat *isl_mat_col_addmul(__isl_take isl_mat *mat, int dst_col,
1676 isl_int f, int src_col)
1677 {
1678 int i;
1679
1680 if (check_col(mat, dst_col) < 0 || check_col(mat, src_col) < 0)
1681 return isl_mat_free(mat);
1682
1683 for (i = 0; i < mat->n_row; ++i) {
1684 if (isl_int_is_zero(mat->row[i][src_col]))
1685 continue;
1686 mat = isl_mat_cow(mat);
1687 if (!mat)
1688 return NULL;
1689 isl_int_addmul(mat->row[i][dst_col], f, mat->row[i][src_col]);
1690 }
1691
1692 return mat;
1693 }
1694
1695 /* Negate column "col" of "mat" and return the result.
1696 */
isl_mat_col_neg(__isl_take isl_mat * mat,int col)1697 __isl_give isl_mat *isl_mat_col_neg(__isl_take isl_mat *mat, int col)
1698 {
1699 int i;
1700
1701 if (check_col(mat, col) < 0)
1702 return isl_mat_free(mat);
1703
1704 for (i = 0; i < mat->n_row; ++i) {
1705 if (isl_int_is_zero(mat->row[i][col]))
1706 continue;
1707 mat = isl_mat_cow(mat);
1708 if (!mat)
1709 return NULL;
1710 isl_int_neg(mat->row[i][col], mat->row[i][col]);
1711 }
1712
1713 return mat;
1714 }
1715
1716 /* Negate row "row" of "mat" and return the result.
1717 */
isl_mat_row_neg(__isl_take isl_mat * mat,int row)1718 __isl_give isl_mat *isl_mat_row_neg(__isl_take isl_mat *mat, int row)
1719 {
1720 if (check_row(mat, row) < 0)
1721 return isl_mat_free(mat);
1722 if (isl_seq_first_non_zero(mat->row[row], mat->n_col) == -1)
1723 return mat;
1724 mat = isl_mat_cow(mat);
1725 if (!mat)
1726 return NULL;
1727 isl_seq_neg(mat->row[row], mat->row[row], mat->n_col);
1728 return mat;
1729 }
1730
isl_mat_unimodular_complete(__isl_take isl_mat * M,int row)1731 __isl_give isl_mat *isl_mat_unimodular_complete(__isl_take isl_mat *M, int row)
1732 {
1733 int r;
1734 struct isl_mat *H = NULL, *Q = NULL;
1735
1736 if (!M)
1737 return NULL;
1738
1739 isl_assert(M->ctx, M->n_row == M->n_col, goto error);
1740 M->n_row = row;
1741 H = isl_mat_left_hermite(isl_mat_copy(M), 0, NULL, &Q);
1742 M->n_row = M->n_col;
1743 if (!H)
1744 goto error;
1745 for (r = 0; r < row; ++r)
1746 isl_assert(M->ctx, isl_int_is_one(H->row[r][r]), goto error);
1747 for (r = row; r < M->n_row; ++r)
1748 isl_seq_cpy(M->row[r], Q->row[r], M->n_col);
1749 isl_mat_free(H);
1750 isl_mat_free(Q);
1751 return M;
1752 error:
1753 isl_mat_free(H);
1754 isl_mat_free(Q);
1755 isl_mat_free(M);
1756 return NULL;
1757 }
1758
isl_mat_concat(__isl_take isl_mat * top,__isl_take isl_mat * bot)1759 __isl_give isl_mat *isl_mat_concat(__isl_take isl_mat *top,
1760 __isl_take isl_mat *bot)
1761 {
1762 struct isl_mat *mat;
1763
1764 if (!top || !bot)
1765 goto error;
1766
1767 isl_assert(top->ctx, top->n_col == bot->n_col, goto error);
1768 if (top->n_row == 0) {
1769 isl_mat_free(top);
1770 return bot;
1771 }
1772 if (bot->n_row == 0) {
1773 isl_mat_free(bot);
1774 return top;
1775 }
1776
1777 mat = isl_mat_alloc(top->ctx, top->n_row + bot->n_row, top->n_col);
1778 if (!mat)
1779 goto error;
1780 isl_mat_sub_copy(mat->ctx, mat->row, top->row, top->n_row,
1781 0, 0, mat->n_col);
1782 isl_mat_sub_copy(mat->ctx, mat->row + top->n_row, bot->row, bot->n_row,
1783 0, 0, mat->n_col);
1784 isl_mat_free(top);
1785 isl_mat_free(bot);
1786 return mat;
1787 error:
1788 isl_mat_free(top);
1789 isl_mat_free(bot);
1790 return NULL;
1791 }
1792
isl_mat_is_equal(__isl_keep isl_mat * mat1,__isl_keep isl_mat * mat2)1793 isl_bool isl_mat_is_equal(__isl_keep isl_mat *mat1, __isl_keep isl_mat *mat2)
1794 {
1795 int i;
1796
1797 if (!mat1 || !mat2)
1798 return isl_bool_error;
1799
1800 if (mat1->n_row != mat2->n_row)
1801 return isl_bool_false;
1802
1803 if (mat1->n_col != mat2->n_col)
1804 return isl_bool_false;
1805
1806 for (i = 0; i < mat1->n_row; ++i)
1807 if (!isl_seq_eq(mat1->row[i], mat2->row[i], mat1->n_col))
1808 return isl_bool_false;
1809
1810 return isl_bool_true;
1811 }
1812
isl_mat_from_row_vec(__isl_take isl_vec * vec)1813 __isl_give isl_mat *isl_mat_from_row_vec(__isl_take isl_vec *vec)
1814 {
1815 struct isl_mat *mat;
1816
1817 if (!vec)
1818 return NULL;
1819 mat = isl_mat_alloc(vec->ctx, 1, vec->size);
1820 if (!mat)
1821 goto error;
1822
1823 isl_seq_cpy(mat->row[0], vec->el, vec->size);
1824
1825 isl_vec_free(vec);
1826 return mat;
1827 error:
1828 isl_vec_free(vec);
1829 return NULL;
1830 }
1831
1832 /* Return a copy of row "row" of "mat" as an isl_vec.
1833 */
isl_mat_get_row(__isl_keep isl_mat * mat,unsigned row)1834 __isl_give isl_vec *isl_mat_get_row(__isl_keep isl_mat *mat, unsigned row)
1835 {
1836 isl_vec *v;
1837
1838 if (!mat)
1839 return NULL;
1840 if (row >= mat->n_row)
1841 isl_die(mat->ctx, isl_error_invalid, "row out of range",
1842 return NULL);
1843
1844 v = isl_vec_alloc(isl_mat_get_ctx(mat), mat->n_col);
1845 if (!v)
1846 return NULL;
1847 isl_seq_cpy(v->el, mat->row[row], mat->n_col);
1848
1849 return v;
1850 }
1851
isl_mat_vec_concat(__isl_take isl_mat * top,__isl_take isl_vec * bot)1852 __isl_give isl_mat *isl_mat_vec_concat(__isl_take isl_mat *top,
1853 __isl_take isl_vec *bot)
1854 {
1855 return isl_mat_concat(top, isl_mat_from_row_vec(bot));
1856 }
1857
isl_mat_move_cols(__isl_take isl_mat * mat,unsigned dst_col,unsigned src_col,unsigned n)1858 __isl_give isl_mat *isl_mat_move_cols(__isl_take isl_mat *mat,
1859 unsigned dst_col, unsigned src_col, unsigned n)
1860 {
1861 isl_mat *res;
1862
1863 if (!mat)
1864 return NULL;
1865 if (n == 0 || dst_col == src_col)
1866 return mat;
1867
1868 res = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
1869 if (!res)
1870 goto error;
1871
1872 if (dst_col < src_col) {
1873 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1874 0, 0, dst_col);
1875 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1876 dst_col, src_col, n);
1877 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1878 dst_col + n, dst_col, src_col - dst_col);
1879 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1880 src_col + n, src_col + n,
1881 res->n_col - src_col - n);
1882 } else {
1883 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1884 0, 0, src_col);
1885 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1886 src_col, src_col + n, dst_col - src_col);
1887 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1888 dst_col, src_col, n);
1889 isl_mat_sub_copy(res->ctx, res->row, mat->row, mat->n_row,
1890 dst_col + n, dst_col + n,
1891 res->n_col - dst_col - n);
1892 }
1893 isl_mat_free(mat);
1894
1895 return res;
1896 error:
1897 isl_mat_free(mat);
1898 return NULL;
1899 }
1900
1901 /* Return the gcd of the elements in row "row" of "mat" in *gcd.
1902 * Return isl_stat_ok on success and isl_stat_error on failure.
1903 */
isl_mat_row_gcd(__isl_keep isl_mat * mat,int row,isl_int * gcd)1904 isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
1905 {
1906 if (check_row(mat, row) < 0)
1907 return isl_stat_error;
1908
1909 isl_seq_gcd(mat->row[row], mat->n_col, gcd);
1910
1911 return isl_stat_ok;
1912 }
1913
isl_mat_gcd(__isl_keep isl_mat * mat,isl_int * gcd)1914 void isl_mat_gcd(__isl_keep isl_mat *mat, isl_int *gcd)
1915 {
1916 int i;
1917 isl_int g;
1918
1919 isl_int_set_si(*gcd, 0);
1920 if (!mat)
1921 return;
1922
1923 isl_int_init(g);
1924 for (i = 0; i < mat->n_row; ++i) {
1925 isl_seq_gcd(mat->row[i], mat->n_col, &g);
1926 isl_int_gcd(*gcd, *gcd, g);
1927 }
1928 isl_int_clear(g);
1929 }
1930
1931 /* Return the result of scaling "mat" by a factor of "m".
1932 */
isl_mat_scale(__isl_take isl_mat * mat,isl_int m)1933 __isl_give isl_mat *isl_mat_scale(__isl_take isl_mat *mat, isl_int m)
1934 {
1935 int i;
1936
1937 if (isl_int_is_one(m))
1938 return mat;
1939
1940 mat = isl_mat_cow(mat);
1941 if (!mat)
1942 return NULL;
1943
1944 for (i = 0; i < mat->n_row; ++i)
1945 isl_seq_scale(mat->row[i], mat->row[i], m, mat->n_col);
1946
1947 return mat;
1948 }
1949
isl_mat_scale_down(__isl_take isl_mat * mat,isl_int m)1950 __isl_give isl_mat *isl_mat_scale_down(__isl_take isl_mat *mat, isl_int m)
1951 {
1952 int i;
1953
1954 if (isl_int_is_one(m))
1955 return mat;
1956
1957 mat = isl_mat_cow(mat);
1958 if (!mat)
1959 return NULL;
1960
1961 for (i = 0; i < mat->n_row; ++i)
1962 isl_seq_scale_down(mat->row[i], mat->row[i], m, mat->n_col);
1963
1964 return mat;
1965 }
1966
isl_mat_scale_down_row(__isl_take isl_mat * mat,int row,isl_int m)1967 __isl_give isl_mat *isl_mat_scale_down_row(__isl_take isl_mat *mat, int row,
1968 isl_int m)
1969 {
1970 if (isl_int_is_one(m))
1971 return mat;
1972
1973 mat = isl_mat_cow(mat);
1974 if (!mat)
1975 return NULL;
1976
1977 isl_seq_scale_down(mat->row[row], mat->row[row], m, mat->n_col);
1978
1979 return mat;
1980 }
1981
isl_mat_normalize(__isl_take isl_mat * mat)1982 __isl_give isl_mat *isl_mat_normalize(__isl_take isl_mat *mat)
1983 {
1984 isl_int gcd;
1985
1986 if (!mat)
1987 return NULL;
1988
1989 isl_int_init(gcd);
1990 isl_mat_gcd(mat, &gcd);
1991 mat = isl_mat_scale_down(mat, gcd);
1992 isl_int_clear(gcd);
1993
1994 return mat;
1995 }
1996
isl_mat_normalize_row(__isl_take isl_mat * mat,int row)1997 __isl_give isl_mat *isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
1998 {
1999 mat = isl_mat_cow(mat);
2000 if (!mat)
2001 return NULL;
2002
2003 isl_seq_normalize(mat->ctx, mat->row[row], mat->n_col);
2004
2005 return mat;
2006 }
2007
2008 /* Number of initial non-zero columns.
2009 */
isl_mat_initial_non_zero_cols(__isl_keep isl_mat * mat)2010 int isl_mat_initial_non_zero_cols(__isl_keep isl_mat *mat)
2011 {
2012 int i;
2013
2014 if (!mat)
2015 return -1;
2016
2017 for (i = 0; i < mat->n_col; ++i)
2018 if (row_first_non_zero(mat->row, mat->n_row, i) < 0)
2019 break;
2020
2021 return i;
2022 }
2023
2024 /* Return a basis for the space spanned by the rows of "mat".
2025 * Any basis will do, so simply perform Gaussian elimination and
2026 * remove the empty rows.
2027 */
isl_mat_row_basis(__isl_take isl_mat * mat)2028 __isl_give isl_mat *isl_mat_row_basis(__isl_take isl_mat *mat)
2029 {
2030 return isl_mat_reverse_gauss(mat);
2031 }
2032
2033 /* Return rows that extend a basis of "mat1" to one
2034 * that covers both "mat1" and "mat2".
2035 * The Hermite normal form of the concatenation of the two matrices is
2036 *
2037 * [ Q1 ]
2038 * [ M1 ] = [ H1 0 0 ] [ Q2 ]
2039 * [ M2 ] = [ H2 H3 0 ] [ Q3 ]
2040 *
2041 * The number of columns in H1 and H3 determine the number of rows
2042 * in Q1 and Q2. Q1 is a basis for M1, while Q2 extends this basis
2043 * to also cover M2.
2044 */
isl_mat_row_basis_extension(__isl_take isl_mat * mat1,__isl_take isl_mat * mat2)2045 __isl_give isl_mat *isl_mat_row_basis_extension(
2046 __isl_take isl_mat *mat1, __isl_take isl_mat *mat2)
2047 {
2048 isl_size n_row;
2049 int r1, r;
2050 isl_size n1;
2051 isl_mat *H, *Q;
2052
2053 n1 = isl_mat_rows(mat1);
2054 H = isl_mat_concat(mat1, mat2);
2055 H = isl_mat_left_hermite(H, 0, NULL, &Q);
2056 if (n1 < 0 || !H || !Q)
2057 goto error;
2058
2059 r1 = hermite_first_zero_col(H, 0, n1);
2060 r = hermite_first_zero_col(H, r1, H->n_row);
2061 n_row = isl_mat_rows(Q);
2062 if (n_row < 0)
2063 goto error;
2064 Q = isl_mat_drop_rows(Q, r, n_row - r);
2065 Q = isl_mat_drop_rows(Q, 0, r1);
2066
2067 isl_mat_free(H);
2068 return Q;
2069 error:
2070 isl_mat_free(H);
2071 isl_mat_free(Q);
2072 return NULL;
2073 }
2074
2075 /* Are the rows of "mat1" linearly independent of those of "mat2"?
2076 * That is, is there no linear dependence among the combined rows
2077 * that is not already present in either "mat1" or "mat2"?
2078 * In other words, is the rank of "mat1" and "mat2" combined equal
2079 * to the sum of the ranks of "mat1" and "mat2"?
2080 */
isl_mat_has_linearly_independent_rows(__isl_keep isl_mat * mat1,__isl_keep isl_mat * mat2)2081 isl_bool isl_mat_has_linearly_independent_rows(__isl_keep isl_mat *mat1,
2082 __isl_keep isl_mat *mat2)
2083 {
2084 isl_size r1, r2, r;
2085 isl_mat *mat;
2086
2087 r1 = isl_mat_rank(mat1);
2088 if (r1 < 0)
2089 return isl_bool_error;
2090 if (r1 == 0)
2091 return isl_bool_true;
2092 r2 = isl_mat_rank(mat2);
2093 if (r2 < 0)
2094 return isl_bool_error;
2095 if (r2 == 0)
2096 return isl_bool_true;
2097
2098 mat = isl_mat_concat(isl_mat_copy(mat1), isl_mat_copy(mat2));
2099 r = isl_mat_rank(mat);
2100 isl_mat_free(mat);
2101 if (r < 0)
2102 return isl_bool_error;
2103 return isl_bool_ok(r == r1 + r2);
2104 }
2105