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
/*
Copyright (C) 2020 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "perm.h"
#include "ca_mat.h"
int
ca_mat_rref_lu(slong * res_rank, ca_mat_t R, const ca_mat_t A, ca_ctx_t ctx)
{
slong i, j, k, n, rank;
slong *pivots;
slong *nonpivots;
slong *P;
ca_mat_t U, V;
int success;
if (ca_mat_check_is_zero(A, ctx) == T_TRUE)
{
*res_rank = 0;
return 1;
}
/* Todo: fast path for nrows == 1 */
n = A->c;
P = _perm_init(ca_mat_nrows(A));
success = ca_mat_lu(&rank, P, R, A, 0, ctx);
_perm_clear(P);
if (!success)
return 0;
if (rank == 0)
{
*res_rank = 0;
return 1;
}
/* Clear L */
for (i = 0; i < A->r; i++)
for (j = 0; j < FLINT_MIN(i, rank); j++)
ca_zero(ca_mat_entry(R, i, j), ctx);
/* We now reorder U to proper upper triangular form U | V
with U full-rank triangular, set V = U^(-1) V, and then
put the column back in the original order.
An improvement for some matrices would be to compress V by
discarding columns containing nothing but zeros. */
ca_mat_init(U, rank, rank, ctx);
ca_mat_init(V, rank, n - rank, ctx);
pivots = flint_malloc(sizeof(slong) * rank);
nonpivots = flint_malloc(sizeof(slong) * (n - rank));
for (i = j = k = 0; i < rank; i++)
{
while (1)
{
/* Todo: this should not be T_UNKNOWN. Should we save
the pivot data in the lu algorithm? */
truth_t is_zero = ca_check_is_zero(ca_mat_entry(R, i, j), ctx);
if (is_zero == T_FALSE)
{
break;
}
else if (is_zero == T_TRUE)
{
nonpivots[k] = j;
k++;
j++;
}
else
{
success = 0;
goto cleanup1;
}
}
pivots[i] = j;
j++;
}
while (k < n - rank)
{
nonpivots[k] = j;
k++;
j++;
}
for (i = 0; i < rank; i++)
for (j = 0; j <= i; j++)
ca_set(ca_mat_entry(U, j, i), ca_mat_entry(R, j, pivots[i]), ctx);
for (i = 0; i < n - rank; i++)
for (j = 0; j < rank; j++)
ca_set(ca_mat_entry(V, j, i), ca_mat_entry(R, j, nonpivots[i]), ctx);
ca_mat_solve_triu(V, U, V, 0, ctx);
/* Clear pivot columns */
for (i = 0; i < rank; i++)
{
for (j = 0; j <= i; j++)
{
if (i == j)
ca_one(ca_mat_entry(R, j, pivots[i]), ctx);
else
ca_zero(ca_mat_entry(R, j, pivots[i]), ctx);
}
}
/* Write back the actual content */
for (i = 0; i < n - rank; i++)
for (j = 0; j < rank; j++)
ca_set(ca_mat_entry(R, j, nonpivots[i]), ca_mat_entry(V, j, i), ctx);
cleanup1:
ca_mat_clear(U, ctx);
ca_mat_clear(V, ctx);
flint_free(pivots);
flint_free(nonpivots);
*res_rank = rank;
return 1;
}