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
/*
Copyright (C) 2022 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 "gr_vec.h"
#include "gr_mat.h"
static void
_gr_mat_swap_rows(gr_mat_t mat, slong * perm, slong r, slong s, gr_ctx_t ctx)
{
if (r != s)
{
slong sz = ctx->sizeof_elem;
if (perm != NULL)
FLINT_SWAP(slong, perm[r], perm[s]);
_gr_vec_swap(GR_MAT_ENTRY(mat, r, 0, sz), GR_MAT_ENTRY(mat, s, 0, sz), mat->c, ctx);
}
}
int
gr_mat_fflu(slong * res_rank, slong * P, gr_mat_t LU, gr_ptr den, const gr_mat_t A, int rank_check, gr_ctx_t ctx)
{
gr_ptr d, e;
slong i, j, k, m, n, r, rank, row, col, sz;
int status = GR_SUCCESS;
int pivot_status;
if (gr_mat_is_empty(A, ctx) == T_TRUE)
{
*res_rank = 0;
return gr_one(den, ctx);
}
if (gr_ctx_is_integral_domain(ctx) != T_TRUE)
return GR_UNABLE;
GR_TMP_INIT2(d, e, ctx);
m = gr_mat_nrows(A, ctx);
n = gr_mat_ncols(A, ctx);
sz = ctx->sizeof_elem;
status |= gr_mat_set(LU, A, ctx);
#define ENTRY(ii, jj) GR_MAT_ENTRY(LU, ii, jj, sz)
rank = row = col = 0;
for (i = 0; i < m; i++)
P[i] = i;
while (row < m && col < n)
{
pivot_status = gr_mat_find_nonzero_pivot(&r, LU, row, m, col, ctx);
/* We don't know whether there is a nonzero pivot element,
so we can't determine the rank. */
if (pivot_status == GR_UNABLE)
{
status = GR_UNABLE;
break;
}
/* There is certainly no nonzero pivot element. */
if (pivot_status == GR_DOMAIN)
{
/* We proved that the matrix is rank-deficient,
accomplishing the goal. */
if (rank_check)
{
status = GR_SUCCESS;
rank = 0;
break;
}
/* Continue with next column. */
col++;
continue;
}
rank++;
if (r != row)
_gr_mat_swap_rows(LU, P, row, r, ctx);
/* Todo: when the pivot element is invertible, make
use of that property.
status |= gr_inv(d, GR_ENTRY(a[row], col, sz), ctx);
if (status != GR_SUCCESS)
break;
*/
for (j = row + 1; j < m; j++)
{
/*
status |= gr_mul(e, ENTRY(j, col), d, ctx);
status |= gr_neg(e, e, ctx);
status |= _gr_vec_addmul_scalar(ENTRY(j, col + 1), ENTRY(row, col + 1), n - col - 1, e, ctx);
status |= gr_zero(ENTRY(j, col), ctx);
status |= gr_neg(ENTRY(j, rank - 1), e, ctx);
*/
for (k = col + 1; k < n; k++)
{
status |= gr_mul(ENTRY(j, k), ENTRY(j, k), ENTRY(row, col), ctx);
status |= gr_mul(e, ENTRY(j, col), ENTRY(row, k), ctx);
status |= gr_sub(ENTRY(j, k), ENTRY(j, k), e, ctx);
if (row > 0)
{
status |= gr_divexact(ENTRY(j, k), ENTRY(j, k), den, ctx);
if (status != GR_SUCCESS)
goto cleanup;
}
}
}
status |= gr_set(den, ENTRY(row, col), ctx);
row++;
col++;
}
cleanup:
GR_TMP_CLEAR2(d, e, ctx);
#undef ENTRY
*res_rank = rank;
return status;
}