-
Notifications
You must be signed in to change notification settings - Fork 0
/
discriminant.cpp
278 lines (246 loc) · 7.96 KB
/
discriminant.cpp
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
#include <iostream>
#include "Polynomial.h"
#include "Quotient.h"
#include "VeryLong.h"
#include <vector>
#include <iostream>
#include "pow.h"
//#define LIDIA_RESULTANT 1
#ifdef LIDIA_RESULTANT
// Code taken from LiDIA
// The following two functions were contributed
// by Roland Dreier ([email protected])
// Calculate resultant of aa and bb via subresultant algorithm.
// Algorithm cribbed verbatim from Algorithm 3.3.7 of H. Cohen's "A
// course in computational algebraic number theory." (Even the
// variables are named pretty much the same as in his book!)
//
// Author : Roland Dreier ([email protected])
//
VeryLong
resultant(const Polynomial <VeryLong> &aa,
const Polynomial <VeryLong> &bb)
{
bool debug(false);
if (std::getenv("FACTOR_VERBOSE_OUTPUT")) debug = true;
const VeryLong zero(0L);
const VeryLong one(1L);
// Return zero if either polynomial is zero.
if ((aa == Polynomial<VeryLong>(0L)) || (bb == Polynomial<VeryLong>(0L)))
{
return zero;
}
// otherwise...
// Initialization steps:
VeryLong acont, bcont;
Polynomial <VeryLong> a, b;
bool neg = false;
// Maybe skip one reduction by making sure deg(a) >= deg(b).
if (aa.deg() >= bb.deg())
{
a = aa;
b = bb;
}
else
{
a = bb;
b = aa;
// Possibly change sign!!
neg = ((a.deg() % 2) && (b.deg() % 2));
}
if (debug) std::cout << "::::: resultant of " << a << " and " << b << std::endl;
acont = a.content();
bcont = b.content();
a.make_primitive();
b.make_primitive();
VeryLong g = 1L;
VeryLong h = 1L;
VeryLong t;
VeryLong pow_temp;
//t = exp(VeryLong(acont), VeryLong((long int)b.deg()));
t = pow<VeryLong, long int>(acont, (long int)b.deg());
//pow_temp = exp(VeryLong(bcont), VeryLong((long int)a.deg()));
pow_temp = pow<VeryLong, long int>(bcont, (long int)a.deg());
t = t * pow_temp;
// Loop over pseudo-division and reduction steps:
long int delta;
Polynomial <VeryLong> r;
do
{
delta = a.deg() - b.deg();
if ((a.deg() % 2) && (b.deg() % 2))
{
neg = !neg;
}
if (debug) std::cout << "::::: about to calculate remainder(" << a << "," << b << ")" << std::endl;
r = remainder(a, b);
a = b;
//pow_temp = exp(h, VeryLong(delta));
pow_temp = pow<VeryLong, long int>(h, delta);
if (debug) std::cout << "::::: 1. r = " << r << ", pow_temp = " << pow_temp << ", g = " << g << ", h = " << h << std::endl;
b = r / pow_temp;
b = b / g;
if (debug) std::cout << "::::: b = " << b << std::endl;
if (debug) std::cout << "::::: Top coeff of a = " << a.coefficient(a.deg()) << std::endl;
g = a.coefficient(a.deg());
//pow_temp = exp(g, VeryLong(delta--));
pow_temp = pow<VeryLong, long int>(g, delta--);
if (debug) std::cout << "::::: 2. r = " << r << ", pow_temp = " << pow_temp << ", g = " << g << ", h = " << h << std::endl;
if (delta<=0)
{
//h = exp(h, VeryLong(-delta));
h = pow<VeryLong, long int>(h, -delta);
h = h * pow_temp;
}
else
{
//h = exp(h, VeryLong(delta));
h = pow<VeryLong, long int>(h, delta);
h = pow_temp / h;
}
if (debug) std::cout << "::::: 3. r = " << r << ", pow_temp = " << pow_temp << ", g = " << g << ", h = " << h << std::endl;
}
while (b.deg() > 0);
// Finish up:
VeryLong temp(0L);
if (b.deg() > 0)
{
temp = b.coefficient(b.deg());
}
if (debug) std::cout << "::::: temp = " << temp << std::endl;
//pow_temp = exp(temp, VeryLong((long int)a.deg()));
pow_temp = pow<VeryLong, long int>(temp, (long int)a.deg());
delta = a.deg()-1;
if (delta <= 0)
{
//h = exp(h, VeryLong(-delta));
h = pow<VeryLong, long int>(h, -delta);
h = h * pow_temp;
}
else
{
//h = exp(h,VeryLong(delta));
h = pow<VeryLong, long int>(h,delta);
h = pow_temp / h;
}
if (neg)
t = -one * t;
VeryLong res = t*h;
if (debug) std::cout << "::::: Resultant = " << res << std::endl;
return res;
}
#else
// Algorithm 3.3.7 [Sub-Resultant]
VeryLong resultant(const Polynomial <VeryLong>& AA, const Polynomial <VeryLong>& BB)
{
bool debug(false);
if (std::getenv("FACTOR_VERBOSE_OUTPUT")) debug = true;
const VeryLong zero(0L);
const Polynomial<VeryLong> zeropoly(0L);
const VeryLong one(1L);
auto A(AA);
auto B(BB);
// Step 1. [Initializations and reductions]
if (A == zeropoly || B == zeropoly)
{
return zero;
}
VeryLong a = A.content();
VeryLong b = B.content();
A /= a;
B /= b;
VeryLong g(1L);
VeryLong h(1L);
long int s(1L);
auto t = pow<VeryLong, int>(a, B.deg()) * pow<VeryLong, int>(b, A.deg());
if (debug) std::cout << "::::: A = " << A << ", B = " << B << ", g = " << g << ", h = " << h << ", s = " << s << std::endl;
if (A.deg() < B.deg())
{
auto tmp = A;
A = B;
B = tmp;
if (A.deg() % 2 && B.deg() % 2)
{
s = -1L;
}
}
if (debug) std::cout << "::::: A = " << A << ", B = " << B << ", g = " << g << ", h = " << h << ", s = " << s << std::endl;
while (B.deg() > 0)
{
// Step 2. [Pseudo division]
auto delta = A.deg() - B.deg();
if (A.deg() % 2 && B.deg() % 2)
{
s = -s;
}
Polynomial<VeryLong> Q;
Polynomial<VeryLong> R;
pseudo_divide(A, B, Q, R);
if (debug) std::cout << "::::: Step 2. A = " << A << ", B = " << B << ", Q = " << Q << ", R = " << R << std::endl;
// Step 3. [Reduce remainder]
A = B;
if (delta < zero)
{
B = R * pow<VeryLong, int>(h, -delta) / g;
}
else
{
B = R / (g * pow<VeryLong, int>(h, delta));
}
if (debug) std::cout << "::::: Step 3. A = " << A << ", B = " << B << std::endl;
// Step 4. [Finished]
g = A.coefficient(A.deg());
if (delta <= 0)
{
h = pow<VeryLong, int>(h, 1 - delta) / pow<VeryLong, int>(g, -delta);
}
else
{
h = pow<VeryLong, int>(h, 1 - delta) * pow<VeryLong, int>(g, delta);
}
if (debug) std::cout << "::::: Step 4. A = " << A << ", B = " << B << ", g = " << g << ", h = " << h << ", s = " << s << std::endl;
}
if (debug) std::cout << "::::: A = " << A << ", B = " << B << ", g = " << g << ", h = " << h << ", s = " << s << std::endl;
auto l_B = B.coefficient(B.deg());
if (debug) std::cout << "::::: l_B = " << l_B << std::endl;
if (A.deg() <= 1)
{
h = pow<VeryLong, int>(h, 1 - A.deg()) * pow<VeryLong, int>(l_B, A.deg());
}
else
{
h = pow<VeryLong, int>(l_B, A.deg()) / pow<VeryLong, int>(h, A.deg() - 1);
}
if (debug) std::cout << "::::: h = " << h << ", s = " << s << ", t = " << t << ", s*t*h = " << s * t * h << std::endl;
return s * t * h;
}
#endif
//
// Calculate discriminant of a polynomial using the formula:
// disc(a) = (-1)^(d*(d-1)/2) * resultant(a, a') / lead_coeff(a)
// where d = deg(f).
//
// Rather than raising -1 to a power, just look at d mod 4.
// Remember, d*(d-1)/2 is even iff d = 0 or 1 mod 4.
//
// Author : Roland Dreier ([email protected])
//
VeryLong
discriminant(const Polynomial <VeryLong> &a)
{
// cout << "discriminant of " << a << endl;
if (a.deg() <= 0)
{
return(VeryLong(0L));
}
//VeryLong multiplier = exp(a.coefficient(a.deg()), VeryLong(a.deg()*2L - 3L));
VeryLong multiplier = pow<VeryLong, long int>(a.coefficient(a.deg()), a.deg()*2L - 3L);
if ((int(a.deg()) % 4) <= 1)
{
return(resultant(a, a.derivative()) / a.coefficient(a.deg()));
}
else
{
return(-(resultant(a, a.derivative()) / a.coefficient(a.deg())));
}
}