-
Notifications
You must be signed in to change notification settings - Fork 0
/
isl_mat.c
1742 lines (1504 loc) · 38.8 KB
/
isl_mat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2014 Ecole Normale Superieure
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
* and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
*/
#include <isl_ctx_private.h>
#include <isl_map_private.h>
#include <isl/space.h>
#include <isl_seq.h>
#include <isl_mat_private.h>
#include <isl_vec_private.h>
#include <isl_space_private.h>
#include <isl_val_private.h>
#include <isl/deprecated/mat_int.h>
isl_ctx *isl_mat_get_ctx(__isl_keep isl_mat *mat)
{
return mat ? mat->ctx : NULL;
}
struct isl_mat *isl_mat_alloc(struct isl_ctx *ctx,
unsigned n_row, unsigned n_col)
{
int i;
struct isl_mat *mat;
mat = isl_alloc_type(ctx, struct isl_mat);
if (!mat)
return NULL;
mat->row = NULL;
mat->block = isl_blk_alloc(ctx, n_row * n_col);
if (isl_blk_is_error(mat->block))
goto error;
mat->row = isl_alloc_array(ctx, isl_int *, n_row);
if (n_row && !mat->row)
goto error;
for (i = 0; i < n_row; ++i)
mat->row[i] = mat->block.data + i * n_col;
mat->ctx = ctx;
isl_ctx_ref(ctx);
mat->ref = 1;
mat->n_row = n_row;
mat->n_col = n_col;
mat->max_col = n_col;
mat->flags = 0;
return mat;
error:
isl_blk_free(ctx, mat->block);
free(mat);
return NULL;
}
struct isl_mat *isl_mat_extend(struct isl_mat *mat,
unsigned n_row, unsigned n_col)
{
int i;
isl_int *old;
isl_int **row;
if (!mat)
return NULL;
if (mat->max_col >= n_col && mat->n_row >= n_row) {
if (mat->n_col < n_col)
mat->n_col = n_col;
return mat;
}
if (mat->max_col < n_col) {
struct isl_mat *new_mat;
if (n_row < mat->n_row)
n_row = mat->n_row;
new_mat = isl_mat_alloc(mat->ctx, n_row, n_col);
if (!new_mat)
goto error;
for (i = 0; i < mat->n_row; ++i)
isl_seq_cpy(new_mat->row[i], mat->row[i], mat->n_col);
isl_mat_free(mat);
return new_mat;
}
mat = isl_mat_cow(mat);
if (!mat)
goto error;
old = mat->block.data;
mat->block = isl_blk_extend(mat->ctx, mat->block, n_row * mat->max_col);
if (isl_blk_is_error(mat->block))
goto error;
row = isl_realloc_array(mat->ctx, mat->row, isl_int *, n_row);
if (n_row && !row)
goto error;
mat->row = row;
for (i = 0; i < mat->n_row; ++i)
mat->row[i] = mat->block.data + (mat->row[i] - old);
for (i = mat->n_row; i < n_row; ++i)
mat->row[i] = mat->block.data + i * mat->max_col;
mat->n_row = n_row;
if (mat->n_col < n_col)
mat->n_col = n_col;
return mat;
error:
isl_mat_free(mat);
return NULL;
}
__isl_give isl_mat *isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row,
unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
{
int i;
struct isl_mat *mat;
mat = isl_alloc_type(ctx, struct isl_mat);
if (!mat)
return NULL;
mat->row = isl_alloc_array(ctx, isl_int *, n_row);
if (n_row && !mat->row)
goto error;
for (i = 0; i < n_row; ++i)
mat->row[i] = row[first_row+i] + first_col;
mat->ctx = ctx;
isl_ctx_ref(ctx);
mat->ref = 1;
mat->n_row = n_row;
mat->n_col = n_col;
mat->block = isl_blk_empty();
mat->flags = ISL_MAT_BORROWED;
return mat;
error:
free(mat);
return NULL;
}
__isl_give isl_mat *isl_mat_sub_alloc(__isl_keep isl_mat *mat,
unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
{
if (!mat)
return NULL;
return isl_mat_sub_alloc6(mat->ctx, mat->row, first_row, n_row,
first_col, n_col);
}
void 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)
{
int i;
for (i = 0; i < n_row; ++i)
isl_seq_cpy(dst[i]+dst_col, src[i]+src_col, n_col);
}
void 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)
{
int i;
for (i = 0; i < n_row; ++i)
isl_seq_neg(dst[i]+dst_col, src[i]+src_col, n_col);
}
struct isl_mat *isl_mat_copy(struct isl_mat *mat)
{
if (!mat)
return NULL;
mat->ref++;
return mat;
}
struct isl_mat *isl_mat_dup(struct isl_mat *mat)
{
int i;
struct isl_mat *mat2;
if (!mat)
return NULL;
mat2 = isl_mat_alloc(mat->ctx, mat->n_row, mat->n_col);
if (!mat2)
return NULL;
for (i = 0; i < mat->n_row; ++i)
isl_seq_cpy(mat2->row[i], mat->row[i], mat->n_col);
return mat2;
}
struct isl_mat *isl_mat_cow(struct isl_mat *mat)
{
struct isl_mat *mat2;
if (!mat)
return NULL;
if (mat->ref == 1 && !ISL_F_ISSET(mat, ISL_MAT_BORROWED))
return mat;
mat2 = isl_mat_dup(mat);
isl_mat_free(mat);
return mat2;
}
__isl_null isl_mat *isl_mat_free(__isl_take isl_mat *mat)
{
if (!mat)
return NULL;
if (--mat->ref > 0)
return NULL;
if (!ISL_F_ISSET(mat, ISL_MAT_BORROWED))
isl_blk_free(mat->ctx, mat->block);
isl_ctx_deref(mat->ctx);
free(mat->row);
free(mat);
return NULL;
}
int isl_mat_rows(__isl_keep isl_mat *mat)
{
return mat ? mat->n_row : -1;
}
int isl_mat_cols(__isl_keep isl_mat *mat)
{
return mat ? mat->n_col : -1;
}
int isl_mat_get_element(__isl_keep isl_mat *mat, int row, int col, isl_int *v)
{
if (!mat)
return -1;
if (row < 0 || row >= mat->n_row)
isl_die(mat->ctx, isl_error_invalid, "row out of range",
return -1);
if (col < 0 || col >= mat->n_col)
isl_die(mat->ctx, isl_error_invalid, "column out of range",
return -1);
isl_int_set(*v, mat->row[row][col]);
return 0;
}
/* Extract the element at row "row", oolumn "col" of "mat".
*/
__isl_give isl_val *isl_mat_get_element_val(__isl_keep isl_mat *mat,
int row, int col)
{
isl_ctx *ctx;
if (!mat)
return NULL;
ctx = isl_mat_get_ctx(mat);
if (row < 0 || row >= mat->n_row)
isl_die(ctx, isl_error_invalid, "row out of range",
return NULL);
if (col < 0 || col >= mat->n_col)
isl_die(ctx, isl_error_invalid, "column out of range",
return NULL);
return isl_val_int_from_isl_int(ctx, mat->row[row][col]);
}
__isl_give isl_mat *isl_mat_set_element(__isl_take isl_mat *mat,
int row, int col, isl_int v)
{
mat = isl_mat_cow(mat);
if (!mat)
return NULL;
if (row < 0 || row >= mat->n_row)
isl_die(mat->ctx, isl_error_invalid, "row out of range",
goto error);
if (col < 0 || col >= mat->n_col)
isl_die(mat->ctx, isl_error_invalid, "column out of range",
goto error);
isl_int_set(mat->row[row][col], v);
return mat;
error:
isl_mat_free(mat);
return NULL;
}
__isl_give isl_mat *isl_mat_set_element_si(__isl_take isl_mat *mat,
int row, int col, int v)
{
mat = isl_mat_cow(mat);
if (!mat)
return NULL;
if (row < 0 || row >= mat->n_row)
isl_die(mat->ctx, isl_error_invalid, "row out of range",
goto error);
if (col < 0 || col >= mat->n_col)
isl_die(mat->ctx, isl_error_invalid, "column out of range",
goto error);
isl_int_set_si(mat->row[row][col], v);
return mat;
error:
isl_mat_free(mat);
return NULL;
}
/* Replace the element at row "row", column "col" of "mat" by "v".
*/
__isl_give isl_mat *isl_mat_set_element_val(__isl_take isl_mat *mat,
int row, int col, __isl_take isl_val *v)
{
if (!v)
return isl_mat_free(mat);
if (!isl_val_is_int(v))
isl_die(isl_val_get_ctx(v), isl_error_invalid,
"expecting integer value", goto error);
mat = isl_mat_set_element(mat, row, col, v->n);
isl_val_free(v);
return mat;
error:
isl_val_free(v);
return isl_mat_free(mat);
}
__isl_give isl_mat *isl_mat_diag(isl_ctx *ctx, unsigned n_row, isl_int d)
{
int i;
struct isl_mat *mat;
mat = isl_mat_alloc(ctx, n_row, n_row);
if (!mat)
return NULL;
for (i = 0; i < n_row; ++i) {
isl_seq_clr(mat->row[i], i);
isl_int_set(mat->row[i][i], d);
isl_seq_clr(mat->row[i]+i+1, n_row-(i+1));
}
return mat;
}
__isl_give isl_mat *isl_mat_identity(isl_ctx *ctx, unsigned n_row)
{
if (!ctx)
return NULL;
return isl_mat_diag(ctx, n_row, ctx->one);
}
/* Is "mat" a (possibly scaled) identity matrix?
*/
int isl_mat_is_scaled_identity(__isl_keep isl_mat *mat)
{
int i;
if (!mat)
return -1;
if (mat->n_row != mat->n_col)
return 0;
for (i = 0; i < mat->n_row; ++i) {
if (isl_seq_first_non_zero(mat->row[i], i) != -1)
return 0;
if (isl_int_ne(mat->row[0][0], mat->row[i][i]))
return 0;
if (isl_seq_first_non_zero(mat->row[i] + i + 1,
mat->n_col - (i + 1)) != -1)
return 0;
}
return 1;
}
struct isl_vec *isl_mat_vec_product(struct isl_mat *mat, struct isl_vec *vec)
{
int i;
struct isl_vec *prod;
if (!mat || !vec)
goto error;
isl_assert(mat->ctx, mat->n_col == vec->size, goto error);
prod = isl_vec_alloc(mat->ctx, mat->n_row);
if (!prod)
goto error;
for (i = 0; i < prod->size; ++i)
isl_seq_inner_product(mat->row[i], vec->el, vec->size,
&prod->block.data[i]);
isl_mat_free(mat);
isl_vec_free(vec);
return prod;
error:
isl_mat_free(mat);
isl_vec_free(vec);
return NULL;
}
__isl_give isl_vec *isl_mat_vec_inverse_product(__isl_take isl_mat *mat,
__isl_take isl_vec *vec)
{
struct isl_mat *vec_mat;
int i;
if (!mat || !vec)
goto error;
vec_mat = isl_mat_alloc(vec->ctx, vec->size, 1);
if (!vec_mat)
goto error;
for (i = 0; i < vec->size; ++i)
isl_int_set(vec_mat->row[i][0], vec->el[i]);
vec_mat = isl_mat_inverse_product(mat, vec_mat);
isl_vec_free(vec);
if (!vec_mat)
return NULL;
vec = isl_vec_alloc(vec_mat->ctx, vec_mat->n_row);
if (vec)
for (i = 0; i < vec->size; ++i)
isl_int_set(vec->el[i], vec_mat->row[i][0]);
isl_mat_free(vec_mat);
return vec;
error:
isl_mat_free(mat);
isl_vec_free(vec);
return NULL;
}
struct isl_vec *isl_vec_mat_product(struct isl_vec *vec, struct isl_mat *mat)
{
int i, j;
struct isl_vec *prod;
if (!mat || !vec)
goto error;
isl_assert(mat->ctx, mat->n_row == vec->size, goto error);
prod = isl_vec_alloc(mat->ctx, mat->n_col);
if (!prod)
goto error;
for (i = 0; i < prod->size; ++i) {
isl_int_set_si(prod->el[i], 0);
for (j = 0; j < vec->size; ++j)
isl_int_addmul(prod->el[i], vec->el[j], mat->row[j][i]);
}
isl_mat_free(mat);
isl_vec_free(vec);
return prod;
error:
isl_mat_free(mat);
isl_vec_free(vec);
return NULL;
}
struct isl_mat *isl_mat_aff_direct_sum(struct isl_mat *left,
struct isl_mat *right)
{
int i;
struct isl_mat *sum;
if (!left || !right)
goto error;
isl_assert(left->ctx, left->n_row == right->n_row, goto error);
isl_assert(left->ctx, left->n_row >= 1, goto error);
isl_assert(left->ctx, left->n_col >= 1, goto error);
isl_assert(left->ctx, right->n_col >= 1, goto error);
isl_assert(left->ctx,
isl_seq_first_non_zero(left->row[0]+1, left->n_col-1) == -1,
goto error);
isl_assert(left->ctx,
isl_seq_first_non_zero(right->row[0]+1, right->n_col-1) == -1,
goto error);
sum = isl_mat_alloc(left->ctx, left->n_row, left->n_col + right->n_col - 1);
if (!sum)
goto error;
isl_int_lcm(sum->row[0][0], left->row[0][0], right->row[0][0]);
isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
isl_seq_clr(sum->row[0]+1, sum->n_col-1);
for (i = 1; i < sum->n_row; ++i) {
isl_int_mul(sum->row[i][0], left->row[0][0], left->row[i][0]);
isl_int_addmul(sum->row[i][0],
right->row[0][0], right->row[i][0]);
isl_seq_scale(sum->row[i]+1, left->row[i]+1, left->row[0][0],
left->n_col-1);
isl_seq_scale(sum->row[i]+left->n_col,
right->row[i]+1, right->row[0][0],
right->n_col-1);
}
isl_int_divexact(left->row[0][0], sum->row[0][0], left->row[0][0]);
isl_int_divexact(right->row[0][0], sum->row[0][0], right->row[0][0]);
isl_mat_free(left);
isl_mat_free(right);
return sum;
error:
isl_mat_free(left);
isl_mat_free(right);
return NULL;
}
static void exchange(struct isl_mat *M, struct isl_mat **U,
struct isl_mat **Q, unsigned row, unsigned i, unsigned j)
{
int r;
for (r = row; r < M->n_row; ++r)
isl_int_swap(M->row[r][i], M->row[r][j]);
if (U) {
for (r = 0; r < (*U)->n_row; ++r)
isl_int_swap((*U)->row[r][i], (*U)->row[r][j]);
}
if (Q)
isl_mat_swap_rows(*Q, i, j);
}
static void subtract(struct isl_mat *M, struct isl_mat **U,
struct isl_mat **Q, unsigned row, unsigned i, unsigned j, isl_int m)
{
int r;
for (r = row; r < M->n_row; ++r)
isl_int_submul(M->row[r][j], m, M->row[r][i]);
if (U) {
for (r = 0; r < (*U)->n_row; ++r)
isl_int_submul((*U)->row[r][j], m, (*U)->row[r][i]);
}
if (Q) {
for (r = 0; r < (*Q)->n_col; ++r)
isl_int_addmul((*Q)->row[i][r], m, (*Q)->row[j][r]);
}
}
static void oppose(struct isl_mat *M, struct isl_mat **U,
struct isl_mat **Q, unsigned row, unsigned col)
{
int r;
for (r = row; r < M->n_row; ++r)
isl_int_neg(M->row[r][col], M->row[r][col]);
if (U) {
for (r = 0; r < (*U)->n_row; ++r)
isl_int_neg((*U)->row[r][col], (*U)->row[r][col]);
}
if (Q)
isl_seq_neg((*Q)->row[col], (*Q)->row[col], (*Q)->n_col);
}
/* Given matrix M, compute
*
* M U = H
* M = H Q
*
* with U and Q unimodular matrices and H a matrix in column echelon form
* such that on each echelon row the entries in the non-echelon column
* are non-negative (if neg == 0) or non-positive (if neg == 1)
* and strictly smaller (in absolute value) than the entries in the echelon
* column.
* If U or Q are NULL, then these matrices are not computed.
*/
struct isl_mat *isl_mat_left_hermite(struct isl_mat *M, int neg,
struct isl_mat **U, struct isl_mat **Q)
{
isl_int c;
int row, col;
if (U)
*U = NULL;
if (Q)
*Q = NULL;
if (!M)
goto error;
M = isl_mat_cow(M);
if (!M)
goto error;
if (U) {
*U = isl_mat_identity(M->ctx, M->n_col);
if (!*U)
goto error;
}
if (Q) {
*Q = isl_mat_identity(M->ctx, M->n_col);
if (!*Q)
goto error;
}
col = 0;
isl_int_init(c);
for (row = 0; row < M->n_row; ++row) {
int first, i, off;
first = isl_seq_abs_min_non_zero(M->row[row]+col, M->n_col-col);
if (first == -1)
continue;
first += col;
if (first != col)
exchange(M, U, Q, row, first, col);
if (isl_int_is_neg(M->row[row][col]))
oppose(M, U, Q, row, col);
first = col+1;
while ((off = isl_seq_first_non_zero(M->row[row]+first,
M->n_col-first)) != -1) {
first += off;
isl_int_fdiv_q(c, M->row[row][first], M->row[row][col]);
subtract(M, U, Q, row, col, first, c);
if (!isl_int_is_zero(M->row[row][first]))
exchange(M, U, Q, row, first, col);
else
++first;
}
for (i = 0; i < col; ++i) {
if (isl_int_is_zero(M->row[row][i]))
continue;
if (neg)
isl_int_cdiv_q(c, M->row[row][i], M->row[row][col]);
else
isl_int_fdiv_q(c, M->row[row][i], M->row[row][col]);
if (isl_int_is_zero(c))
continue;
subtract(M, U, Q, row, col, i, c);
}
++col;
}
isl_int_clear(c);
return M;
error:
if (Q) {
isl_mat_free(*Q);
*Q = NULL;
}
if (U) {
isl_mat_free(*U);
*U = NULL;
}
isl_mat_free(M);
return NULL;
}
struct isl_mat *isl_mat_right_kernel(struct isl_mat *mat)
{
int i, rank;
struct isl_mat *U = NULL;
struct isl_mat *K;
mat = isl_mat_left_hermite(mat, 0, &U, NULL);
if (!mat || !U)
goto error;
for (i = 0, rank = 0; rank < mat->n_col; ++rank) {
while (i < mat->n_row && isl_int_is_zero(mat->row[i][rank]))
++i;
if (i >= mat->n_row)
break;
}
K = isl_mat_alloc(U->ctx, U->n_row, U->n_col - rank);
if (!K)
goto error;
isl_mat_sub_copy(K->ctx, K->row, U->row, U->n_row, 0, rank, U->n_col-rank);
isl_mat_free(mat);
isl_mat_free(U);
return K;
error:
isl_mat_free(mat);
isl_mat_free(U);
return NULL;
}
struct isl_mat *isl_mat_lin_to_aff(struct isl_mat *mat)
{
int i;
struct isl_mat *mat2;
if (!mat)
return NULL;
mat2 = isl_mat_alloc(mat->ctx, 1+mat->n_row, 1+mat->n_col);
if (!mat2)
goto error;
isl_int_set_si(mat2->row[0][0], 1);
isl_seq_clr(mat2->row[0]+1, mat->n_col);
for (i = 0; i < mat->n_row; ++i) {
isl_int_set_si(mat2->row[1+i][0], 0);
isl_seq_cpy(mat2->row[1+i]+1, mat->row[i], mat->n_col);
}
isl_mat_free(mat);
return mat2;
error:
isl_mat_free(mat);
return NULL;
}
/* Given two matrices M1 and M2, return the block matrix
*
* [ M1 0 ]
* [ 0 M2 ]
*/
__isl_give isl_mat *isl_mat_diagonal(__isl_take isl_mat *mat1,
__isl_take isl_mat *mat2)
{
int i;
isl_mat *mat;
if (!mat1 || !mat2)
goto error;
mat = isl_mat_alloc(mat1->ctx, mat1->n_row + mat2->n_row,
mat1->n_col + mat2->n_col);
if (!mat)
goto error;
for (i = 0; i < mat1->n_row; ++i) {
isl_seq_cpy(mat->row[i], mat1->row[i], mat1->n_col);
isl_seq_clr(mat->row[i] + mat1->n_col, mat2->n_col);
}
for (i = 0; i < mat2->n_row; ++i) {
isl_seq_clr(mat->row[mat1->n_row + i], mat1->n_col);
isl_seq_cpy(mat->row[mat1->n_row + i] + mat1->n_col,
mat2->row[i], mat2->n_col);
}
isl_mat_free(mat1);
isl_mat_free(mat2);
return mat;
error:
isl_mat_free(mat1);
isl_mat_free(mat2);
return NULL;
}
static int row_first_non_zero(isl_int **row, unsigned n_row, unsigned col)
{
int i;
for (i = 0; i < n_row; ++i)
if (!isl_int_is_zero(row[i][col]))
return i;
return -1;
}
static int row_abs_min_non_zero(isl_int **row, unsigned n_row, unsigned col)
{
int i, min = row_first_non_zero(row, n_row, col);
if (min < 0)
return -1;
for (i = min + 1; i < n_row; ++i) {
if (isl_int_is_zero(row[i][col]))
continue;
if (isl_int_abs_lt(row[i][col], row[min][col]))
min = i;
}
return min;
}
static void inv_exchange(struct isl_mat *left, struct isl_mat *right,
unsigned i, unsigned j)
{
left = isl_mat_swap_rows(left, i, j);
right = isl_mat_swap_rows(right, i, j);
}
static void inv_oppose(
struct isl_mat *left, struct isl_mat *right, unsigned row)
{
isl_seq_neg(left->row[row]+row, left->row[row]+row, left->n_col-row);
isl_seq_neg(right->row[row], right->row[row], right->n_col);
}
static void inv_subtract(struct isl_mat *left, struct isl_mat *right,
unsigned row, unsigned i, isl_int m)
{
isl_int_neg(m, m);
isl_seq_combine(left->row[i]+row,
left->ctx->one, left->row[i]+row,
m, left->row[row]+row,
left->n_col-row);
isl_seq_combine(right->row[i], right->ctx->one, right->row[i],
m, right->row[row], right->n_col);
}
/* Compute inv(left)*right
*/
struct isl_mat *isl_mat_inverse_product(struct isl_mat *left,
struct isl_mat *right)
{
int row;
isl_int a, b;
if (!left || !right)
goto error;
isl_assert(left->ctx, left->n_row == left->n_col, goto error);
isl_assert(left->ctx, left->n_row == right->n_row, goto error);
if (left->n_row == 0) {
isl_mat_free(left);
return right;
}
left = isl_mat_cow(left);
right = isl_mat_cow(right);
if (!left || !right)
goto error;
isl_int_init(a);
isl_int_init(b);
for (row = 0; row < left->n_row; ++row) {
int pivot, first, i, off;
pivot = row_abs_min_non_zero(left->row+row, left->n_row-row, row);
if (pivot < 0) {
isl_int_clear(a);
isl_int_clear(b);
isl_assert(left->ctx, pivot >= 0, goto error);
}
pivot += row;
if (pivot != row)
inv_exchange(left, right, pivot, row);
if (isl_int_is_neg(left->row[row][row]))
inv_oppose(left, right, row);
first = row+1;
while ((off = row_first_non_zero(left->row+first,
left->n_row-first, row)) != -1) {
first += off;
isl_int_fdiv_q(a, left->row[first][row],
left->row[row][row]);
inv_subtract(left, right, row, first, a);
if (!isl_int_is_zero(left->row[first][row]))
inv_exchange(left, right, row, first);
else
++first;
}
for (i = 0; i < row; ++i) {
if (isl_int_is_zero(left->row[i][row]))
continue;
isl_int_gcd(a, left->row[row][row], left->row[i][row]);
isl_int_divexact(b, left->row[i][row], a);
isl_int_divexact(a, left->row[row][row], a);
isl_int_neg(b, b);
isl_seq_combine(left->row[i] + i,
a, left->row[i] + i,
b, left->row[row] + i,
left->n_col - i);
isl_seq_combine(right->row[i], a, right->row[i],
b, right->row[row], right->n_col);
}
}
isl_int_clear(b);
isl_int_set(a, left->row[0][0]);
for (row = 1; row < left->n_row; ++row)
isl_int_lcm(a, a, left->row[row][row]);
if (isl_int_is_zero(a)){
isl_int_clear(a);
isl_assert(left->ctx, 0, goto error);
}
for (row = 0; row < left->n_row; ++row) {
isl_int_divexact(left->row[row][row], a, left->row[row][row]);
if (isl_int_is_one(left->row[row][row]))
continue;
isl_seq_scale(right->row[row], right->row[row],
left->row[row][row], right->n_col);
}
isl_int_clear(a);
isl_mat_free(left);
return right;
error:
isl_mat_free(left);
isl_mat_free(right);
return NULL;
}
void isl_mat_col_scale(struct isl_mat *mat, unsigned col, isl_int m)
{
int i;
for (i = 0; i < mat->n_row; ++i)
isl_int_mul(mat->row[i][col], mat->row[i][col], m);
}
void isl_mat_col_combine(struct isl_mat *mat, unsigned dst,
isl_int m1, unsigned src1, isl_int m2, unsigned src2)
{
int i;
isl_int tmp;
isl_int_init(tmp);
for (i = 0; i < mat->n_row; ++i) {
isl_int_mul(tmp, m1, mat->row[i][src1]);
isl_int_addmul(tmp, m2, mat->row[i][src2]);
isl_int_set(mat->row[i][dst], tmp);
}
isl_int_clear(tmp);
}
struct isl_mat *isl_mat_right_inverse(struct isl_mat *mat)
{
struct isl_mat *inv;
int row;
isl_int a, b;
mat = isl_mat_cow(mat);
if (!mat)
return NULL;
inv = isl_mat_identity(mat->ctx, mat->n_col);
inv = isl_mat_cow(inv);
if (!inv)
goto error;
isl_int_init(a);
isl_int_init(b);
for (row = 0; row < mat->n_row; ++row) {
int pivot, first, i, off;
pivot = isl_seq_abs_min_non_zero(mat->row[row]+row, mat->n_col-row);
if (pivot < 0) {
isl_int_clear(a);
isl_int_clear(b);
isl_assert(mat->ctx, pivot >= 0, goto error);
}
pivot += row;
if (pivot != row)
exchange(mat, &inv, NULL, row, pivot, row);
if (isl_int_is_neg(mat->row[row][row]))
oppose(mat, &inv, NULL, row, row);
first = row+1;
while ((off = isl_seq_first_non_zero(mat->row[row]+first,
mat->n_col-first)) != -1) {
first += off;
isl_int_fdiv_q(a, mat->row[row][first],
mat->row[row][row]);
subtract(mat, &inv, NULL, row, row, first, a);
if (!isl_int_is_zero(mat->row[row][first]))
exchange(mat, &inv, NULL, row, row, first);
else
++first;
}
for (i = 0; i < row; ++i) {
if (isl_int_is_zero(mat->row[row][i]))
continue;
isl_int_gcd(a, mat->row[row][row], mat->row[row][i]);
isl_int_divexact(b, mat->row[row][i], a);
isl_int_divexact(a, mat->row[row][row], a);
isl_int_neg(a, a);
isl_mat_col_combine(mat, i, a, i, b, row);
isl_mat_col_combine(inv, i, a, i, b, row);
}
}
isl_int_clear(b);
isl_int_set(a, mat->row[0][0]);
for (row = 1; row < mat->n_row; ++row)
isl_int_lcm(a, a, mat->row[row][row]);
if (isl_int_is_zero(a)){
isl_int_clear(a);
goto error;
}
for (row = 0; row < mat->n_row; ++row) {
isl_int_divexact(mat->row[row][row], a, mat->row[row][row]);
if (isl_int_is_one(mat->row[row][row]))
continue;
isl_mat_col_scale(inv, row, mat->row[row][row]);
}
isl_int_clear(a);
isl_mat_free(mat);
return inv;
error:
isl_mat_free(mat);
isl_mat_free(inv);
return NULL;
}
struct isl_mat *isl_mat_transpose(struct isl_mat *mat)
{
struct isl_mat *transpose = NULL;
int i, j;
if (!mat)
return NULL;
if (mat->n_col == mat->n_row) {
mat = isl_mat_cow(mat);
if (!mat)
return NULL;
for (i = 0; i < mat->n_row; ++i)
for (j = i + 1; j < mat->n_col; ++j)
isl_int_swap(mat->row[i][j], mat->row[j][i]);
return mat;
}
transpose = isl_mat_alloc(mat->ctx, mat->n_col, mat->n_row);
if (!transpose)
goto error;
for (i = 0; i < mat->n_row; ++i)
for (j = 0; j < mat->n_col; ++j)
isl_int_set(transpose->row[j][i], mat->row[i][j]);
isl_mat_free(mat);
return transpose;
error: