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
/*
Copyright (C) 2011, 2014 William Hart
Copyright (C) 2011 Sebastian Pancratz
Copyright (C) 2023 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 "nmod_poly.h"
#include "gr_poly.h"
/*
XXX: Currently supports aliasing between {A,a} and {B,b}.
*/
slong _nmod_poly_hgcd(nn_ptr *M, slong *lenM,
nn_ptr A, slong *lenA, nn_ptr B, slong *lenB,
nn_srcptr a, slong lena, nn_srcptr b, slong lenb,
nmod_t mod)
{
slong sgnM;
gr_ctx_t ctx;
_gr_ctx_init_nmod(ctx, &mod);
GR_MUST_SUCCEED(_gr_poly_hgcd(NULL, &sgnM, (gr_ptr *) M, lenM, A, lenA, B, lenB, a, lena, b, lenb, nmod_poly_hgcd_iter_recursive_cutoff(mod), ctx));
return sgnM;
}
/*
Assuming deg(a) > deg(b) (in particular b could be zero, but a must not be)
compute a matrix M = [[m11, m12] [m21 m22]] and A, B so that
[ a ] = [ m11 m12 ][ A ]
[ b ] [ m21 m22 ][ B ]
with
(1) A and B are consecutive remainders in the euclidean remainder
sequence for a, b satisfying 2*deg(A) >= deg(a) > 2*deg(B)
(2) M is a product of [[qi 1][1 0]] where the qi are the quotients
obtained in (1)
No aliasing. The return is det(M), which is +-1. All of the moduli of the
arguments should be the same and prime. Here is a reference implementation
in case something is wrong with _nmod_poly_hgcd (there doesn't seem to be).
*/
slong nmod_poly_hgcd_ref(
nmod_poly_t m11, nmod_poly_t m12,
nmod_poly_t m21, nmod_poly_t m22,
nmod_poly_t A, nmod_poly_t B,
const nmod_poly_t a, const nmod_poly_t b)
{
slong sgnM;
slong dega = nmod_poly_degree(a);
nmod_poly_t q, r, t;
if (nmod_poly_degree(a) <= nmod_poly_degree(b))
{
flint_throw(FLINT_ERROR, "Exception in nmod_poly_hgcd_ref:"
" Input degrees are invalid.\n");
}
nmod_poly_init_mod(q, a->mod);
nmod_poly_init_mod(r, a->mod);
nmod_poly_init_mod(t, a->mod);
nmod_poly_one(m11);
nmod_poly_zero(m12);
nmod_poly_zero(m21);
nmod_poly_one(m22);
nmod_poly_set(A, a);
nmod_poly_set(B, b);
sgnM = 1;
while (dega <= 2*nmod_poly_degree(B))
{
nmod_poly_divrem(q, r, A, B);
nmod_poly_swap(A, B);
nmod_poly_swap(B, r);
/* multipliy M by [[q 1] [1 0]] on the right */
nmod_poly_mul(t, q, m11);
nmod_poly_add(r, m12, t);
nmod_poly_swap(m11, m12);
nmod_poly_swap(m11, r);
nmod_poly_mul(t, q, m21);
nmod_poly_add(r, m22, t);
nmod_poly_swap(m21, m22);
nmod_poly_swap(m21, r);
sgnM = -sgnM;
}
nmod_poly_clear(q);
nmod_poly_clear(r);
nmod_poly_clear(t);
return sgnM;
}
slong nmod_poly_hgcd(
nmod_poly_t m11, nmod_poly_t m12,
nmod_poly_t m21, nmod_poly_t m22,
nmod_poly_t A, nmod_poly_t B,
const nmod_poly_t a, const nmod_poly_t b)
{
ulong * M[4];
slong lenM[4];
slong sgnM;
if (nmod_poly_degree(a) <= nmod_poly_degree(b))
{
flint_throw(FLINT_ERROR, "Exception in nmod_poly_hgcd:"
" Input degrees are invalid.\n");
}
if (nmod_poly_length(b) == 0)
{
nmod_poly_one(m11);
nmod_poly_zero(m12);
nmod_poly_zero(m21);
nmod_poly_one(m22);
nmod_poly_set(A, a);
nmod_poly_set(B, b);
return 1;
}
/* now nmod_poly_length(a) > nmod_poly_length(b) > 0 */
nmod_poly_fit_length(m11, nmod_poly_length(a));
nmod_poly_fit_length(m12, nmod_poly_length(a));
nmod_poly_fit_length(m21, nmod_poly_length(a));
nmod_poly_fit_length(m22, nmod_poly_length(a));
nmod_poly_fit_length(A, nmod_poly_length(a));
nmod_poly_fit_length(B, nmod_poly_length(a));
/*
Looks like _nmod_poly_hgcd produces an M with det(M) = sgnM = +-1 and
[ a ] = [ m11 m12 ] [ A ]
[ b ] [ m21 m22 ] [ B ]
*/
M[0] = m11->coeffs;
M[1] = m12->coeffs;
M[2] = m21->coeffs;
M[3] = m22->coeffs;
sgnM = _nmod_poly_hgcd(M, lenM, A->coeffs, &A->length, B->coeffs, &B->length,
a->coeffs, a->length, b->coeffs, b->length, A->mod);
m11->length = lenM[0];
m12->length = lenM[1];
m21->length = lenM[2];
m22->length = lenM[3];
FLINT_ASSERT(2*nmod_poly_degree(A) >= nmod_poly_degree(a));
FLINT_ASSERT(nmod_poly_degree(a) > 2*nmod_poly_degree(B));
return sgnM;
}