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
/*
Copyright (C) 2015 Tommy Hofmann
Copyright (C) 2021 William Hart
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 "nmod.h"
#include "nmod_mat.h"
/*
We do no quite need the full Howell form. We just need to reduce to upper
triangular form using row swaps and Euclidean row operations (i.e. that
use the Euclidean xgcd to replace the pivot with its gcd with the entry
in the row in question (mod n). Of course we need a special version of the
Euclidean xgcd whose first cofactor is a unit mod n.
*/
/*
Find s, t such that g = s*a - t*b is the gcd of a and b mod n and where
s is a unit mod n. Assumes a and b are reduced mod n and no aliasing.
*/
static inline ulong
_nmod_xgcd_unit(ulong * s, ulong * t, ulong a, ulong b, nmod_t mod)
{
ulong g, ag, bg;
if (a >= b)
g = n_xgcd(s, t, a, b);
else /* b > a */
{
g = n_xgcd(t, s, b, a);
*s = nmod_neg(*s, mod);
*t = nmod_neg(*t, mod);
}
ag = a/g;
bg = b/g;
while (n_gcd(*s, mod.n) != 1)
{
*s = nmod_add(*s, bg, mod);
*t = nmod_add(*t, ag, mod);
}
return g;
}
static inline int
_nmod_mat_pivot(nmod_mat_t A, slong start_row, slong col)
{
slong j;
if (nmod_mat_entry(A, start_row, col) != 0)
return 1;
for (j = start_row + 1; j < A->r; j++)
{
if (nmod_mat_entry(A, j, col) != 0)
{
nmod_mat_swap_rows(A, NULL, j, start_row);
return -1;
}
}
return 0;
}
/* test whether q*a = b mod N has a solution */
static int
_n_is_divisible(nn_ptr q, ulong b, ulong a, nmod_t N)
{
ulong e, g;
g = n_gcdinv(&e, a, N.n);
if (( b % g ) == 0)
{
*q = nmod_mul(e, b/g, N);
return 1;
}
return 0;
}
ulong _nmod_mat_det_howell(nmod_mat_t A)
{
ulong s, t, t1, det = 1, unit = 1;
slong m, n, row, col, i, k;
nmod_t mod = A->mod;
if (nmod_mat_is_empty(A))
return mod.n != 1;
n = A->r;
m = A->c;
row = col = 0;
while (row < n && col < m)
{
int pivswap = _nmod_mat_pivot(A, row, col);
if (pivswap == 0)
return 0;
if (pivswap == -1)
det = nmod_neg(det, mod);
for (i = row + 1; i < n; i++)
{
if (nmod_mat_entry(A, i, col) == 0)
continue;
if (_n_is_divisible(&s, nmod_mat_entry(A, i, col), nmod_mat_entry(A, row, col), mod))
{
for (k = col; k < m; k++)
{
t = nmod_sub(nmod_mat_entry(A, i, k), nmod_mul(s, nmod_mat_entry(A, row, k), mod), mod);
nmod_mat_entry(A, i, k) = t;
}
}
else
{
_nmod_xgcd_unit(&s, &t, nmod_mat_entry(A, row, col), nmod_mat_entry(A, i, col), mod);
/* now g = s*x - t*y mod n */
unit = nmod_mul(unit, s, mod);
for (k = col; k < m; k++)
{
t1 = nmod_sub(nmod_mul(s, nmod_mat_entry(A, row, k), mod), nmod_mul(t, nmod_mat_entry(A, i, k), mod), mod);
nmod_mat_entry(A, row, k) = t1;
}
/* now it's divisible, restart this row */
i--;
continue;
}
}
det = nmod_mul(det, nmod_mat_entry(A, row, col), mod);
row++;
col++;
}
unit = nmod_inv(unit, mod);
return nmod_mul(det, unit, mod);
}
ulong
nmod_mat_det_howell(const nmod_mat_t A)
{
nmod_mat_t tmp;
ulong det;
slong dim = A->r;
if (dim != A->c)
{
flint_throw(FLINT_ERROR, "Exception (nmod_mat_det_howell). Non-square matrix.\n");
}
nmod_mat_init_set(tmp, A);
det = _nmod_mat_det_howell(tmp);
nmod_mat_clear(tmp);
return det;
}