Line data Source code
1 : /* Copyright (C) 2000 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 :
15 : /*******************************************************************/
16 : /* */
17 : /* ROOTS OF COMPLEX POLYNOMIALS */
18 : /* (original code contributed by Xavier Gourdon, INRIA RR 1852) */
19 : /* */
20 : /*******************************************************************/
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_polroots
25 :
26 : static const double pariINFINITY = 1./0.;
27 :
28 : static long
29 1229675 : isvalidcoeff(GEN x)
30 : {
31 1229675 : switch (typ(x))
32 : {
33 1205149 : case t_INT: case t_REAL: case t_FRAC: return 1;
34 24512 : case t_COMPLEX: return isvalidcoeff(gel(x,1)) && isvalidcoeff(gel(x,2));
35 : }
36 14 : return 0;
37 : }
38 :
39 : static void
40 266941 : checkvalidpol(GEN p, const char *f)
41 : {
42 266941 : long i,n = lg(p);
43 1447571 : for (i=2; i<n; i++)
44 1180637 : if (!isvalidcoeff(gel(p,i))) pari_err_TYPE(f, gel(p,i));
45 266934 : }
46 :
47 : /********************************************************************/
48 : /** **/
49 : /** FAST ARITHMETIC over Z[i] **/
50 : /** **/
51 : /********************************************************************/
52 :
53 : static GEN
54 16453043 : ZX_to_ZiX(GEN Pr, GEN Pi)
55 : {
56 16453043 : long i, lr = lg(Pr), li = lg(Pi), l = maxss(lr, li), m = minss(lr, li);
57 16454731 : GEN P = cgetg(l, t_POL);
58 16459283 : P[1] = Pr[1];
59 66747159 : for(i = 2; i < m; i++)
60 50287958 : gel(P,i) = signe(gel(Pi,i)) ? mkcomplex(gel(Pr,i), gel(Pi,i))
61 50287958 : : gel(Pr,i);
62 22360269 : for( ; i < lr; i++)
63 5901068 : gel(P,i) = gel(Pr, i);
64 16494062 : for( ; i < li; i++)
65 34861 : gel(P,i) = mkcomplex(gen_0, gel(Pi, i));
66 16459201 : return normalizepol_lg(P, l);
67 : }
68 :
69 : static GEN
70 99005947 : ZiX_sqr(GEN P)
71 : {
72 99005947 : pari_sp av = avma;
73 : GEN Pr2, Pi2, Qr, Qi;
74 99005947 : GEN Pr = real_i(P), Pi = imag_i(P);
75 98998024 : if (signe(Pi)==0) return gc_upto(av, ZX_sqr(Pr));
76 16519156 : if (signe(Pr)==0) return gc_upto(av, ZX_neg(ZX_sqr(Pi)));
77 16461836 : Pr2 = ZX_sqr(Pr); Pi2 = ZX_sqr(Pi);
78 16454914 : Qr = ZX_sub(Pr2, Pi2);
79 16455394 : if (degpol(Pr)==degpol(Pi))
80 10701442 : Qi = ZX_sub(ZX_sqr(ZX_add(Pr, Pi)), ZX_add(Pr2, Pi2));
81 : else
82 5757412 : Qi = ZX_shifti(ZX_mul(Pr, Pi), 1);
83 16457327 : return gc_GEN(av, ZX_to_ZiX(Qr, Qi));
84 : }
85 :
86 : static GEN
87 49494210 : graeffe(GEN p)
88 : {
89 49494210 : pari_sp av = avma;
90 : GEN p0, p1, s0, s1;
91 49494210 : long n = degpol(p);
92 :
93 49498454 : if (!n) return RgX_copy(p);
94 49498454 : RgX_even_odd(p, &p0, &p1);
95 : /* p = p0(x^2) + x p1(x^2) */
96 49508152 : s0 = ZiX_sqr(p0);
97 49512202 : s1 = ZiX_sqr(p1);
98 49511827 : return gc_upto(av, RgX_sub(s0, RgX_shift_shallow(s1,1)));
99 : }
100 :
101 : GEN
102 5383 : ZX_graeffe(GEN p)
103 : {
104 5383 : pari_sp av = avma;
105 : GEN p0, p1, s0, s1;
106 5383 : long n = degpol(p);
107 :
108 5383 : if (!n) return ZX_copy(p);
109 5383 : RgX_even_odd(p, &p0, &p1);
110 : /* p = p0(x^2) + x p1(x^2) */
111 5383 : s0 = ZX_sqr(p0);
112 5383 : s1 = ZX_sqr(p1);
113 5383 : return gc_upto(av, ZX_sub(s0, RgX_shift_shallow(s1,1)));
114 : }
115 : GEN
116 14 : polgraeffe(GEN p)
117 : {
118 14 : pari_sp av = avma;
119 : GEN p0, p1, s0, s1;
120 14 : long n = degpol(p);
121 :
122 14 : if (typ(p) != t_POL) pari_err_TYPE("polgraeffe",p);
123 14 : n = degpol(p);
124 14 : if (!n) return gcopy(p);
125 14 : RgX_even_odd(p, &p0, &p1);
126 : /* p = p0(x^2) + x p1(x^2) */
127 14 : s0 = RgX_sqr(p0);
128 14 : s1 = RgX_sqr(p1);
129 14 : return gc_upto(av, RgX_sub(s0, RgX_shift_shallow(s1,1)));
130 : }
131 :
132 : /********************************************************************/
133 : /** **/
134 : /** MODULUS OF ROOTS **/
135 : /** **/
136 : /********************************************************************/
137 :
138 : /* Quick approximation to log2(|x|); first define y s.t. |y-x| < 2^-32 then
139 : * return y rounded to 2 ulp. In particular, if result < 2^21, absolute error
140 : * is bounded by 2^-31. If result > 2^21, it is correct to 2 ulp */
141 : static double
142 224299427 : mydbllog2i(GEN x)
143 : {
144 : #ifdef LONG_IS_64BIT
145 192919184 : const double W = 1/(4294967296. * 4294967296.); /* 2^-64 */
146 : #else
147 31380243 : const double W = 1/4294967296.; /*2^-32*/
148 : #endif
149 : GEN m;
150 224299427 : long lx = lgefint(x);
151 : double l;
152 224299427 : if (lx == 2) return -pariINFINITY;
153 223482485 : m = int_MSW(x);
154 223482485 : l = (double)(ulong)*m;
155 223482485 : if (lx == 3) return log2(l);
156 70080396 : l += ((double)(ulong)*int_precW(m)) * W;
157 : /* at least m = min(53,BIL) bits are correct in the mantissa, thus log2
158 : * is correct with error < log(1 + 2^-m) ~ 2^-m. Adding the correct
159 : * exponent BIL(lx-3) causes 1ulp further round-off error */
160 70080396 : return log2(l) + (double)(BITS_IN_LONG*(lx-3));
161 : }
162 :
163 : /* return log(|x|) or -pariINFINITY */
164 : static double
165 9544685 : mydbllogr(GEN x) {
166 9544685 : if (!signe(x)) return -pariINFINITY;
167 9544685 : return M_LN2*dbllog2r(x);
168 : }
169 :
170 : /* return log2(|x|) or -pariINFINITY */
171 : static double
172 56778728 : mydbllog2r(GEN x) {
173 56778728 : if (!signe(x)) return -pariINFINITY;
174 56340288 : return dbllog2r(x);
175 : }
176 : double
177 300586072 : dbllog2(GEN z)
178 : {
179 : double x, y;
180 300586072 : switch(typ(z))
181 : {
182 224196489 : case t_INT: return mydbllog2i(z);
183 22364 : case t_FRAC: return mydbllog2i(gel(z,1))-mydbllog2i(gel(z,2));
184 49325123 : case t_REAL: return mydbllog2r(z);
185 27042096 : default: /*t_COMPLEX*/
186 27042096 : x = dbllog2(gel(z,1));
187 27124208 : y = dbllog2(gel(z,2));
188 27124064 : if (x == -pariINFINITY) return y;
189 26880958 : if (y == -pariINFINITY) return x;
190 26677157 : if (fabs(x-y) > 10) return maxdd(x,y);
191 26058505 : return x + 0.5*log2(1 + exp2(2*(y-x)));
192 : }
193 : }
194 : static GEN /* beware overflow */
195 6547384 : dblexp(double x) { return fabs(x) < 100.? dbltor(exp(x)): mpexp(dbltor(x)); }
196 :
197 : /* find s such that A_h <= 2^s <= 2 A_i for one h and all i < n = deg(p),
198 : * with A_i := (binom(n,i) lc(p) / p_i) ^ 1/(n-i), and p = sum p_i X^i */
199 : static long
200 40944932 : findpower(GEN p)
201 : {
202 40944932 : double x, L, mins = pariINFINITY;
203 40944932 : long n = degpol(p),i;
204 :
205 40944445 : L = dbllog2(gel(p,n+2)); /* log2(lc * binom(n,i)) */
206 164617587 : for (i=n-1; i>=0; i--)
207 : {
208 123670965 : L += log2((double)(i+1) / (double)(n-i));
209 123670965 : x = dbllog2(gel(p,i+2));
210 123668041 : if (x != -pariINFINITY)
211 : {
212 122877569 : double s = (L - x) / (double)(n-i);
213 122877569 : if (s < mins) mins = s;
214 : }
215 : }
216 40946622 : i = (long)ceil(mins);
217 40946622 : if (i - mins > 1 - 1e-12) i--;
218 40946622 : return i;
219 : }
220 :
221 : /* returns the exponent for logmodulus(), from the Newton diagram */
222 : static long
223 5505959 : newton_polygon(GEN p, long k)
224 : {
225 5505959 : pari_sp av = avma;
226 5505959 : long n = degpol(p), i, j, h, l, *vertex = (long*)new_chunk(n+1);
227 5505946 : double *L = (double*)stack_malloc_align((n+1)*sizeof(double), sizeof(double));
228 :
229 : /* vertex[i] = 1 if i a vertex of convex hull, 0 otherwise */
230 26450031 : for (i=0; i<=n; i++) { L[i] = dbllog2(gel(p,2+i)); vertex[i] = 0; }
231 5505954 : vertex[0] = 1; /* sentinel */
232 19603101 : for (i=0; i < n; i=h)
233 : {
234 : double slope;
235 14097147 : h = i+1;
236 14102016 : while (L[i] == -pariINFINITY) { vertex[h] = 1; i = h; h = i+1; }
237 14097147 : slope = L[h] - L[i];
238 37961055 : for (j = i+2; j<=n; j++) if (L[j] != -pariINFINITY)
239 : {
240 23857670 : double pij = (L[j] - L[i])/(double)(j - i);
241 23857670 : if (slope < pij) { slope = pij; h = j; }
242 : }
243 14097147 : vertex[h] = 1;
244 : }
245 6221302 : h = k; while (!vertex[h]) h++;
246 5694361 : l = k-1; while (!vertex[l]) l--;
247 5505954 : set_avma(av);
248 5505984 : return (long)floor((L[h]-L[l])/(double)(h-l) + 0.5);
249 : }
250 :
251 : /* change z into z*2^e, where z is real or complex of real */
252 : static void
253 35499814 : myshiftrc(GEN z, long e)
254 : {
255 35499814 : if (typ(z)==t_COMPLEX)
256 : {
257 6410252 : if (signe(gel(z,1))) shiftr_inplace(gel(z,1), e);
258 6410271 : if (signe(gel(z,2))) shiftr_inplace(gel(z,2), e);
259 : }
260 : else
261 29089562 : if (signe(z)) shiftr_inplace(z, e);
262 35499931 : }
263 :
264 : /* return z*2^e, where z is integer or complex of integer (destroy z) */
265 : static GEN
266 135795891 : myshiftic(GEN z, long e)
267 : {
268 135795891 : if (typ(z)==t_COMPLEX)
269 : {
270 17892806 : gel(z,1) = signe(gel(z,1))? mpshift(gel(z,1),e): gen_0;
271 17891093 : gel(z,2) = mpshift(gel(z,2),e);
272 17888440 : return z;
273 : }
274 117903085 : return signe(z)? mpshift(z,e): gen_0;
275 : }
276 :
277 : static GEN
278 7023592 : RgX_gtofp_bit(GEN q, long bit) { return RgX_gtofp(q, nbits2prec(bit)); }
279 :
280 : static GEN
281 214996438 : mygprecrc(GEN x, long prec, long e)
282 : {
283 : GEN y;
284 214996438 : switch(typ(x))
285 : {
286 160452890 : case t_REAL:
287 160452890 : if (!signe(x)) return real_0_bit(e);
288 157179012 : return realprec(x) == prec? x: rtor(x, prec);
289 37187140 : case t_COMPLEX:
290 37187140 : y = cgetg(3,t_COMPLEX);
291 37186847 : gel(y,1) = mygprecrc(gel(x,1),prec,e);
292 37186750 : gel(y,2) = mygprecrc(gel(x,2),prec,e);
293 37186379 : return y;
294 17356408 : default: return x;
295 : }
296 : }
297 :
298 : /* gprec behaves badly with the zero for polynomials.
299 : The second parameter in mygprec is the precision in base 2 */
300 : static GEN
301 63699924 : mygprec(GEN x, long bit)
302 : {
303 : long e, prec;
304 63699924 : if (bit < 0) bit = 0; /* should rarely happen */
305 63699924 : e = gexpo(x) - bit;
306 63701300 : prec = nbits2prec(bit);
307 63707211 : switch(typ(x))
308 : {
309 167965716 : case t_POL: pari_APPLY_pol_normalized(mygprecrc(gel(x,i),prec,e));
310 17249316 : default: return mygprecrc(x,prec,e);
311 : }
312 : }
313 :
314 : /* normalize a polynomial x, that is change it with coefficients in Z[i],
315 : after making product by 2^shift */
316 : static GEN
317 17584877 : pol_to_gaussint(GEN x, long shift)
318 100300877 : { pari_APPLY_pol_normalized(gtrunc2n(gel(x,i), shift)); }
319 :
320 : /* returns a polynomial q in Z[i][x] keeping bit bits of p */
321 : static GEN
322 13135058 : eval_rel_pol(GEN p, long bit)
323 : {
324 : long i;
325 73983438 : for (i = 2; i < lg(p); i++)
326 60848265 : if (gequal0(gel(p,i))) gel(p,i) = gen_0; /* bad behavior of gexpo */
327 13135173 : return pol_to_gaussint(p, bit-gexpo(p)+1);
328 : }
329 :
330 : /* returns p(R*x)/R^n (in R or R[i]), R = exp(lrho), bit bits of precision */
331 : static GEN
332 1609690 : homothetie(GEN p, double lrho, long bit)
333 : {
334 : GEN q, r, t, iR;
335 1609690 : long n = degpol(p), i;
336 :
337 1609686 : iR = mygprec(dblexp(-lrho),bit);
338 1609659 : q = mygprec(p, bit);
339 1609694 : r = cgetg(n+3,t_POL); r[1] = p[1];
340 1609692 : t = iR; r[n+2] = q[n+2];
341 6791268 : for (i=n-1; i>0; i--)
342 : {
343 5181621 : gel(r,i+2) = gmul(t, gel(q,i+2));
344 5181553 : t = mulrr(t, iR);
345 : }
346 1609647 : gel(r,2) = gmul(t, gel(q,2)); return r;
347 : }
348 :
349 : /* change q in 2^(n*e) p(x*2^(-e)), n=deg(q) [ ~as above with R = 2^-e ]*/
350 : static void
351 9955359 : homothetie2n(GEN p, long e)
352 : {
353 9955359 : if (e)
354 : {
355 7973606 : long i,n = lg(p)-1;
356 43473408 : for (i=2; i<=n; i++) myshiftrc(gel(p,i), (n-i)*e);
357 : }
358 9955504 : }
359 :
360 : /* return 2^f * 2^(n*e) p(x*2^(-e)), n=deg(q) */
361 : static void
362 36487480 : homothetie_gauss(GEN p, long e, long f)
363 : {
364 36487480 : if (e || f)
365 : {
366 32595605 : long i, n = lg(p)-1;
367 168331709 : for (i=2; i<=n; i++) gel(p,i) = myshiftic(gel(p,i), f+(n-i)*e);
368 : }
369 36430773 : }
370 :
371 : /* Lower bound on the modulus of the largest root z_0
372 : * k is set to an upper bound for #{z roots, |z-z_0| < eps} */
373 : static double
374 40944601 : lower_bound(GEN p, long *k, double eps)
375 : {
376 40944601 : long n = degpol(p), i, j;
377 40945166 : pari_sp ltop = avma;
378 : GEN a, s, S, ilc;
379 : double r, R, rho;
380 :
381 40945166 : if (n < 4) { *k = n; return 0.; }
382 8236574 : S = cgetg(5,t_VEC);
383 8237488 : a = cgetg(5,t_VEC); ilc = gdiv(real_1(DEFAULTPREC), gel(p,n+2));
384 41163073 : for (i=1; i<=4; i++) gel(a,i) = gmul(ilc,gel(p,n+2-i));
385 : /* i = 1 split out from next loop for efficiency and initialization */
386 8236193 : s = gel(a,1);
387 8236193 : gel(S,1) = gneg(s); /* Newton sum S_i */
388 8236882 : rho = r = gtodouble(gabs(s,3));
389 8237230 : R = r / n;
390 32943468 : for (i=2; i<=4; i++)
391 : {
392 24706403 : s = gmulsg(i,gel(a,i));
393 74036878 : for (j=1; j<i; j++) s = gadd(s, gmul(gel(S,j),gel(a,i-j)));
394 24689655 : gel(S,i) = gneg(s); /* Newton sum S_i */
395 24700836 : r = gtodouble(gabs(s,3));
396 24706238 : if (r > 0.)
397 : {
398 24659857 : r = exp(log(r/n) / (double)i);
399 24659857 : if (r > R) R = r;
400 : }
401 : }
402 8237065 : if (R > 0. && eps < 1.2)
403 8233338 : *k = (long)floor((rho/R + n) / (1 + exp(-eps)*cos(eps)));
404 : else
405 3727 : *k = n;
406 8237065 : return gc_double(ltop, R);
407 : }
408 :
409 : /* return R such that exp(R - tau) <= rho_n(P) <= exp(R + tau)
410 : * P(0) != 0 and P non constant */
411 : static double
412 4449764 : logmax_modulus(GEN p, double tau)
413 : {
414 : GEN r, q, aux, gunr;
415 4449764 : pari_sp av, ltop = avma;
416 4449764 : long i,k,n=degpol(p),nn,bit,M,e;
417 4449759 : double rho,eps, tau2 = (tau > 3.0)? 0.5: tau/6.;
418 :
419 4449759 : r = cgeti(BIGDEFAULTPREC);
420 4449744 : av = avma;
421 :
422 4449744 : eps = - 1/log(1.5*tau2); /* > 0 */
423 4449744 : bit = (long) ((double) n*log2(1./tau2)+3*log2((double) n))+1;
424 4449744 : gunr = real_1_bit(bit+2*n);
425 4449710 : aux = gdiv(gunr, gel(p,2+n));
426 4449729 : q = RgX_Rg_mul(p, aux); gel(q,2+n) = gunr;
427 4449491 : e = findpower(q);
428 4449691 : homothetie2n(q,e);
429 4449745 : affsi(e, r);
430 4449744 : q = pol_to_gaussint(q, bit);
431 4449284 : M = (long) (log2( log(4.*n) / (2*tau2) )) + 2;
432 4449284 : nn = n;
433 4449284 : for (i=0,e=0;;)
434 : { /* nn = deg(q) */
435 40947595 : rho = lower_bound(q, &k, eps);
436 40945003 : if (rho > exp2(-(double)e)) e = (long)-floor(log2(rho));
437 40945003 : affii(shifti(addis(r,e), 1), r);
438 40903477 : if (++i == M) break;
439 :
440 36454461 : bit = (long) ((double)k * log2(1./tau2) +
441 36454461 : (double)(nn-k)*log2(1./eps) + 3*log2((double)nn)) + 1;
442 36454461 : homothetie_gauss(q, e, bit-(long)floor(dbllog2(gel(q,2+nn))+0.5));
443 36471902 : nn -= RgX_valrem(q, &q);
444 36476555 : q = gc_upto(av, graeffe(q));
445 36496774 : tau2 *= 1.5; if (tau2 > 0.9) tau2 = 0.5;
446 36496774 : eps = -1/log(tau2); /* > 0 */
447 36496774 : e = findpower(q);
448 : }
449 4449016 : if (!signe(r)) return gc_double(ltop,0.);
450 4026771 : r = itor(r, DEFAULTPREC); shiftr_inplace(r, -M);
451 4027215 : return gc_double(ltop, -rtodbl(r) * M_LN2); /* -log(2) sum e_i 2^-i */
452 : }
453 :
454 : static GEN
455 35887 : RgX_normalize1(GEN x)
456 : {
457 35887 : long i, n = lg(x)-1;
458 : GEN y;
459 35901 : for (i = n; i > 1; i--)
460 35894 : if (!gequal0( gel(x,i) )) break;
461 35887 : if (i == n) return x;
462 14 : pari_warn(warner,"normalizing a polynomial with 0 leading term");
463 14 : if (i == 1) pari_err_ROOTS0("roots");
464 14 : y = cgetg(i+1, t_POL); y[1] = x[1];
465 42 : for (; i > 1; i--) gel(y,i) = gel(x,i);
466 14 : return y;
467 : }
468 :
469 : static GEN
470 27293 : polrootsbound_i(GEN P, double TAU)
471 : {
472 27293 : pari_sp av = avma;
473 : double d;
474 27293 : (void)RgX_valrem_inexact(P,&P);
475 27293 : P = RgX_normalize1(P);
476 27293 : switch(degpol(P))
477 : {
478 7 : case -1: pari_err_ROOTS0("roots");
479 140 : case 0: set_avma(av); return gen_0;
480 : }
481 27146 : d = logmax_modulus(P, TAU) + TAU;
482 : /* not dblexp: result differs on ARM emulator */
483 27146 : return gc_leaf(av, mpexp(dbltor(d)));
484 : }
485 : GEN
486 27300 : polrootsbound(GEN P, GEN tau)
487 : {
488 27300 : if (typ(P) != t_POL) pari_err_TYPE("polrootsbound",P);
489 27293 : checkvalidpol(P, "polrootsbound");
490 27293 : return polrootsbound_i(P, tau? gtodouble(tau): 0.01);
491 : }
492 :
493 : /* log of modulus of the smallest root of p, with relative error tau */
494 : static double
495 1614395 : logmin_modulus(GEN p, double tau)
496 : {
497 1614395 : pari_sp av = avma;
498 1614395 : if (gequal0(gel(p,2))) return -pariINFINITY;
499 1614392 : return gc_double(av, - logmax_modulus(RgX_recip_i(p),tau));
500 : }
501 :
502 : /* return the log of the k-th modulus (ascending order) of p, rel. error tau*/
503 : static double
504 606620 : logmodulus(GEN p, long k, double tau)
505 : {
506 : GEN q;
507 606620 : long i, kk = k, imax, n = degpol(p), nn, bit, e;
508 606620 : pari_sp av, ltop=avma;
509 606620 : double r, tau2 = tau/6;
510 :
511 606620 : bit = (long)(n * (2. + log2(3.*n/tau2)));
512 606620 : av = avma;
513 606620 : q = gprec_w(p, nbits2prec(bit));
514 606623 : q = RgX_gtofp_bit(q, bit);
515 606624 : e = newton_polygon(q,k);
516 606624 : r = (double)e;
517 606624 : homothetie2n(q,e);
518 606635 : imax = (long)(log2(3./tau) + log2(log(4.*n)))+1;
519 5505958 : for (i=1; i<imax; i++)
520 : {
521 4899337 : q = eval_rel_pol(q,bit);
522 4899018 : kk -= RgX_valrem(q, &q);
523 4899140 : nn = degpol(q);
524 :
525 4899141 : q = gc_upto(av, graeffe(q));
526 4899336 : e = newton_polygon(q,kk);
527 4899372 : r += e / exp2((double)i);
528 4899372 : q = RgX_gtofp_bit(q, bit);
529 4899253 : homothetie2n(q,e);
530 :
531 4899323 : tau2 *= 1.5; if (tau2 > 1.) tau2 = 1.;
532 4899323 : bit = 1 + (long)(nn*(2. + log2(3.*nn/tau2)));
533 : }
534 606621 : return gc_double(ltop, -r * M_LN2);
535 : }
536 :
537 : /* return the log of the k-th modulus r_k of p, rel. error tau, knowing that
538 : * rmin < r_k < rmax. This information helps because we may reduce precision
539 : * quicker */
540 : static double
541 606620 : logpre_modulus(GEN p, long k, double tau, double lrmin, double lrmax)
542 : {
543 : GEN q;
544 606620 : long n = degpol(p), i, imax, imax2, bit;
545 606620 : pari_sp ltop = avma, av;
546 606620 : double lrho, aux, tau2 = tau/6.;
547 :
548 606620 : aux = (lrmax - lrmin) / 2. + 4*tau2;
549 606620 : imax = (long) log2(log((double)n)/ aux);
550 606620 : if (imax <= 0) return logmodulus(p,k,tau);
551 :
552 597504 : lrho = (lrmin + lrmax) / 2;
553 597504 : av = avma;
554 597504 : bit = (long)(n*(2. + aux / M_LN2 - log2(tau2)));
555 597504 : q = homothetie(p, lrho, bit);
556 597497 : imax2 = (long)(log2(3./tau * log(4.*n))) + 1;
557 597497 : if (imax > imax2) imax = imax2;
558 :
559 1584449 : for (i=0; i<imax; i++)
560 : {
561 986945 : q = eval_rel_pol(q,bit);
562 986944 : q = gc_upto(av, graeffe(q));
563 986955 : aux = 2*aux + 2*tau2;
564 986955 : tau2 *= 1.5;
565 986955 : bit = (long)(n*(2. + aux / M_LN2 - log2(1-exp(-tau2))));
566 986955 : q = RgX_gtofp_bit(q, bit);
567 : }
568 597504 : aux = exp2((double)imax);
569 597504 : return gc_double(ltop, lrho + logmodulus(q,k, aux*tau/3.) / aux);
570 : }
571 :
572 : static double
573 902412 : ind_maxlog2(GEN q)
574 : {
575 902412 : long i, k = -1;
576 902412 : double L = - pariINFINITY;
577 2221689 : for (i=0; i<=degpol(q); i++)
578 : {
579 1319267 : double d = dbllog2(gel(q,2+i));
580 1319277 : if (d > L) { L = d; k = i; }
581 : }
582 902413 : return k;
583 : }
584 :
585 : /* Returns k such that r_k e^(-tau) < R < r_{k+1} e^tau.
586 : * Assume that l <= k <= n-l */
587 : static long
588 1012191 : dual_modulus(GEN p, double lrho, double tau, long l)
589 : {
590 1012191 : long i, imax, delta_k = 0, n = degpol(p), nn, v2, v, bit, ll = l;
591 1012191 : double tau2 = tau * 7./8.;
592 1012191 : pari_sp av = avma;
593 : GEN q;
594 :
595 1012191 : bit = 6*n - 5*l + (long)(n*(-log2(tau2) + tau2 * 8./7.));
596 1012191 : q = homothetie(p, lrho, bit);
597 1012176 : imax = (long)(log(log(2.*n)/tau2)/log(7./4.)+1);
598 :
599 8151509 : for (i=0; i<imax; i++)
600 : {
601 7249097 : q = eval_rel_pol(q,bit); v2 = n - degpol(q);
602 7247977 : v = RgX_valrem(q, &q);
603 7248583 : ll -= maxss(v, v2); if (ll < 0) ll = 0;
604 :
605 7248718 : nn = degpol(q); delta_k += v;
606 7248722 : if (!nn) return delta_k;
607 :
608 7138944 : q = gc_upto(av, graeffe(q));
609 7139333 : tau2 *= 7./4.;
610 7139333 : bit = 6*nn - 5*ll + (long)(nn*(-log2(tau2) + tau2 * 8./7.));
611 : }
612 902412 : return gc_long(av, delta_k + (long)ind_maxlog2(q));
613 : }
614 :
615 : /********************************************************************/
616 : /** **/
617 : /** FACTORS THROUGH CIRCLE INTEGRATION **/
618 : /** **/
619 : /********************************************************************/
620 : /* l power of 2, W[step*j] = w_j; set f[j] = p(w_j)
621 : * if inv, w_j = exp(2IPi*j/l), else exp(-2IPi*j/l) */
622 :
623 : static void
624 7462 : fft2(GEN W, GEN p, GEN f, long step, long l)
625 : {
626 : pari_sp av;
627 : long i, s1, l1, step2;
628 :
629 7462 : if (l == 2)
630 : {
631 3766 : gel(f,0) = gadd(gel(p,0), gel(p,step));
632 3766 : gel(f,1) = gsub(gel(p,0), gel(p,step)); return;
633 : }
634 3696 : av = avma;
635 3696 : l1 = l>>1; step2 = step<<1;
636 3696 : fft2(W,p, f, step2,l1);
637 3696 : fft2(W,p+step, f+l1,step2,l1);
638 32760 : for (i = s1 = 0; i < l1; i++, s1 += step)
639 : {
640 29064 : GEN f0 = gel(f,i);
641 29064 : GEN f1 = gmul(gel(W,s1), gel(f,i+l1));
642 29064 : gel(f,i) = gadd(f0, f1);
643 29064 : gel(f,i+l1) = gsub(f0, f1);
644 : }
645 3696 : gc_slice(av, f, l);
646 : }
647 :
648 : static void
649 14157796 : fft(GEN W, GEN p, GEN f, long step, long l, long inv)
650 : {
651 : pari_sp av;
652 : long i, s1, l1, l2, l3, step4;
653 : GEN f1, f2, f3, f02;
654 :
655 14157796 : if (l == 2)
656 : {
657 6639795 : gel(f,0) = gadd(gel(p,0), gel(p,step));
658 6639510 : gel(f,1) = gsub(gel(p,0), gel(p,step)); return;
659 : }
660 7518001 : av = avma;
661 7518001 : if (l == 4)
662 : {
663 : pari_sp av2;
664 5317203 : f1 = gadd(gel(p,0), gel(p,step<<1));
665 5316617 : f2 = gsub(gel(p,0), gel(p,step<<1));
666 5316643 : f3 = gadd(gel(p,step), gel(p,3*step));
667 5316617 : f02 = gsub(gel(p,step), gel(p,3*step));
668 5316672 : f02 = inv? mulcxI(f02): mulcxmI(f02);
669 5317084 : av2 = avma;
670 5317084 : gel(f,0) = gadd(f1, f3);
671 5316435 : gel(f,1) = gadd(f2, f02);
672 5316645 : gel(f,2) = gsub(f1, f3);
673 5316497 : gel(f,3) = gsub(f2, f02);
674 5316763 : gc_all_unsafe(av,av2,4,&gel(f,0),&gel(f,1),&gel(f,2),&gel(f,3));
675 5317330 : return;
676 : }
677 2200798 : l1 = l>>2; l2 = 2*l1; l3 = l1+l2; step4 = step<<2;
678 2200798 : fft(W,p, f, step4,l1,inv);
679 2201184 : fft(W,p+step, f+l1,step4,l1,inv);
680 2201199 : fft(W,p+(step<<1),f+l2,step4,l1,inv);
681 2201210 : fft(W,p+3*step, f+l3,step4,l1,inv);
682 8194925 : for (i = s1 = 0; i < l1; i++, s1 += step)
683 : {
684 5993762 : long s2 = s1 << 1, s3 = s1 + s2;
685 : GEN g02, g13, f13;
686 5993762 : f1 = gmul(gel(W,s1), gel(f,i+l1));
687 5993970 : f2 = gmul(gel(W,s2), gel(f,i+l2));
688 5993846 : f3 = gmul(gel(W,s3), gel(f,i+l3));
689 :
690 5993942 : f02 = gadd(gel(f,i),f2);
691 5993391 : g02 = gsub(gel(f,i),f2);
692 5993481 : f13 = gadd(f1,f3);
693 5993346 : g13 = gsub(f1,f3); g13 = inv? mulcxI(g13): mulcxmI(g13);
694 :
695 5993913 : gel(f,i) = gadd(f02, f13);
696 5993470 : gel(f,i+l1) = gadd(g02, g13);
697 5993552 : gel(f,i+l2) = gsub(f02, f13);
698 5993515 : gel(f,i+l3) = gsub(g02, g13);
699 : }
700 2201163 : gc_slice(av, f, l);
701 : }
702 :
703 : static GEN
704 98 : FFT_i(GEN W, GEN x)
705 : {
706 98 : long i, l = lg(W), n = lg(x), tx = typ(x), tw, pa;
707 : GEN y, z, p, pol;
708 98 : if (l==1 || ((l-1) & (l-2))) pari_err_DIM("fft");
709 84 : tw = RgV_type(W, &p, &pol, &pa);
710 84 : if (tx == t_POL) { x++; n--; }
711 49 : else if (!is_vec_t(tx)) pari_err_TYPE("fft",x);
712 84 : if (n > l) pari_err_DIM("fft");
713 84 : if (n < l) {
714 0 : z = cgetg(l, t_VECSMALL); /* cf stackdummy */
715 0 : for (i = 1; i < n; i++) gel(z,i) = gel(x,i);
716 0 : for ( ; i < l; i++) gel(z,i) = gen_0;
717 : }
718 84 : else z = x;
719 84 : if (l == 2) return mkveccopy(gel(z,1));
720 70 : y = cgetg(l, t_VEC);
721 70 : if (tw == RgX_type_code(t_COMPLEX,t_INT) ||
722 : tw == RgX_type_code(t_COMPLEX,t_REAL))
723 0 : {
724 0 : long inv = (l >= 5 && signe(imag_i(gel(W,1+(l>>2))))==1) ? 1 : 0;
725 0 : fft(W+1, z+1, y+1, 1, l-1, inv);
726 : } else
727 70 : fft2(W+1, z+1, y+1, 1, l-1);
728 70 : return y;
729 : }
730 :
731 : GEN
732 56 : FFT(GEN W, GEN x)
733 : {
734 56 : if (!is_vec_t(typ(W))) pari_err_TYPE("fft",W);
735 56 : return FFT_i(W, x);
736 : }
737 :
738 : GEN
739 56 : FFTinv(GEN W, GEN x)
740 : {
741 56 : long l = lg(W), i;
742 : GEN w;
743 56 : if (!is_vec_t(typ(W))) pari_err_TYPE("fft",W);
744 56 : if (l==1 || ((l-1) & (l-2))) pari_err_DIM("fft");
745 42 : w = cgetg(l, t_VECSMALL); /* cf stackdummy */
746 42 : gel(w,1) = gel(W,1); /* w = gconj(W), faster */
747 3773 : for (i = 2; i < l; i++) gel(w, i) = gel(W, l-i+1);
748 42 : return FFT_i(w, x);
749 : }
750 :
751 : /* returns 1 if p has only real coefficients, 0 else */
752 : static int
753 961626 : isreal(GEN p)
754 : {
755 : long i;
756 4858734 : for (i = lg(p)-1; i > 1; i--)
757 4058550 : if (typ(gel(p,i)) == t_COMPLEX) return 0;
758 800184 : return 1;
759 : }
760 :
761 : /* x non complex */
762 : static GEN
763 778427 : abs_update_r(GEN x, double *mu) {
764 778427 : GEN y = gtofp(x, DEFAULTPREC);
765 778433 : double ly = mydbllogr(y); if (ly < *mu) *mu = ly;
766 778432 : setabssign(y); return y;
767 : }
768 : /* return |x|, low accuracy. Set *mu = min(log(y), *mu) */
769 : static GEN
770 8003667 : abs_update(GEN x, double *mu) {
771 : GEN y, xr, yr;
772 : double ly;
773 8003667 : if (typ(x) != t_COMPLEX) return abs_update_r(x, mu);
774 7238573 : xr = gel(x,1);
775 7238573 : yr = gel(x,2);
776 7238573 : if (gequal0(xr)) return abs_update_r(yr,mu);
777 7236649 : if (gequal0(yr)) return abs_update_r(xr,mu);
778 : /* have to treat 0 specially: 0E-10 + 1e-20 = 0E-10 */
779 7225444 : xr = gtofp(xr, DEFAULTPREC);
780 7226247 : yr = gtofp(yr, DEFAULTPREC);
781 7226341 : y = sqrtr(addrr(sqrr(xr), sqrr(yr)));
782 7226060 : ly = mydbllogr(y); if (ly < *mu) *mu = ly;
783 7226303 : return y;
784 : }
785 :
786 : static void
787 996064 : initdft(GEN *Omega, GEN *prim, long N, long Lmax, long bit)
788 : {
789 996064 : long prec = nbits2prec(bit);
790 996065 : *Omega = grootsof1(Lmax, prec) + 1;
791 996060 : *prim = rootsof1u_cx(N, prec);
792 996066 : }
793 :
794 : static void
795 493838 : parameters(GEN p, long *LMAX, double *mu, double *gamma,
796 : int polreal, double param, double param2)
797 : {
798 : GEN q, pc, Omega, A, RU, prim, g, TWO;
799 493838 : long n = degpol(p), bit, NN, K, i, j, Lmax;
800 493838 : pari_sp av2, av = avma;
801 :
802 493838 : bit = gexpo(p) + (long)param2+8;
803 683541 : Lmax = 4; while (Lmax <= n) Lmax <<= 1;
804 493839 : NN = (long)(param*3.14)+1; if (NN < Lmax) NN = Lmax;
805 493839 : K = NN/Lmax; if (K & 1) K++;
806 493839 : NN = Lmax*K;
807 493839 : if (polreal) K = K/2+1;
808 :
809 493839 : initdft(&Omega, &prim, NN, Lmax, bit);
810 493838 : q = mygprec(p,bit) + 2;
811 493837 : A = cgetg(Lmax+1,t_VEC); A++;
812 493835 : pc= cgetg(Lmax+1,t_VEC); pc++;
813 2955635 : for (i=0; i <= n; i++) gel(pc,i)= gel(q,i);
814 968139 : for ( ; i<Lmax; i++) gel(pc,i) = gen_0;
815 :
816 493837 : *mu = pariINFINITY;
817 493837 : g = real_0_bit(-bit);
818 493837 : TWO = real2n(1, DEFAULTPREC);
819 493843 : av2 = avma;
820 493843 : RU = gen_1;
821 1737119 : for (i=0; i<K; i++)
822 : {
823 1243285 : if (i) {
824 749453 : GEN z = RU;
825 3442145 : for (j=1; j<n; j++)
826 : {
827 2692685 : gel(pc,j) = gmul(gel(q,j),z);
828 2692658 : z = gmul(z,RU); /* RU = prim^i, z=prim^(ij) */
829 : }
830 749460 : gel(pc,n) = gmul(gel(q,n),z);
831 : }
832 :
833 1243287 : fft(Omega,pc,A,1,Lmax,1);
834 1243302 : if (polreal && i>0 && i<K-1)
835 1141661 : for (j=0; j<Lmax; j++) g = addrr(g, divrr(TWO, abs_update(gel(A,j),mu)));
836 : else
837 8105161 : for (j=0; j<Lmax; j++) g = addrr(g, invr(abs_update(gel(A,j),mu)));
838 1242967 : RU = gmul(RU, prim);
839 1243276 : if (gc_needed(av,1))
840 : {
841 0 : if(DEBUGMEM>1) pari_warn(warnmem,"parameters");
842 0 : (void)gc_all(av2,2, &g,&RU);
843 : }
844 : }
845 493834 : *gamma = mydbllog2r(divru(g,NN));
846 493830 : *LMAX = Lmax; set_avma(av);
847 493830 : }
848 :
849 : /* NN is a multiple of Lmax */
850 : static void
851 502227 : dft(GEN p, long k, long NN, long Lmax, long bit, GEN F, GEN H, long polreal)
852 : {
853 : GEN Omega, q, qd, pc, pd, A, B, C, RU, aux, U, W, prim, prim2;
854 502227 : long n = degpol(p), i, j, K;
855 : pari_sp ltop;
856 :
857 502227 : initdft(&Omega, &prim, NN, Lmax, bit);
858 502230 : RU = cgetg(n+2,t_VEC) + 1;
859 :
860 502228 : K = NN/Lmax; if (polreal) K = K/2+1;
861 502228 : q = mygprec(p,bit);
862 502228 : qd = RgX_deriv(q);
863 :
864 502225 : A = cgetg(Lmax+1,t_VEC); A++;
865 502225 : B = cgetg(Lmax+1,t_VEC); B++;
866 502225 : C = cgetg(Lmax+1,t_VEC); C++;
867 502226 : pc = cgetg(Lmax+1,t_VEC); pc++;
868 502226 : pd = cgetg(Lmax+1,t_VEC); pd++;
869 1018666 : gel(pc,0) = gel(q,2); for (i=n+1; i<Lmax; i++) gel(pc,i) = gen_0;
870 1520892 : gel(pd,0) = gel(qd,2); for (i=n; i<Lmax; i++) gel(pd,i) = gen_0;
871 :
872 502226 : ltop = avma;
873 502226 : W = cgetg(k+1,t_VEC);
874 502225 : U = cgetg(k+1,t_VEC);
875 1201713 : for (i=1; i<=k; i++) gel(W,i) = gel(U,i) = gen_0;
876 :
877 502225 : gel(RU,0) = gen_1;
878 502225 : prim2 = gen_1;
879 1529899 : for (i=0; i<K; i++)
880 : {
881 1027668 : gel(RU,1) = prim2;
882 4444260 : for (j=1; j<n; j++) gel(RU,j+1) = gmul(gel(RU,j),prim2);
883 : /* RU[j] = prim^(ij)= prim2^j */
884 :
885 4444233 : for (j=1; j<n; j++) gel(pd,j) = gmul(gel(qd,j+2),gel(RU,j));
886 1027631 : fft(Omega,pd,A,1,Lmax,1);
887 5471872 : for (j=1; j<=n; j++) gel(pc,j) = gmul(gel(q,j+2),gel(RU,j));
888 1027628 : fft(Omega,pc,B,1,Lmax,1);
889 7663536 : for (j=0; j<Lmax; j++) gel(C,j) = ginv(gel(B,j));
890 7663500 : for (j=0; j<Lmax; j++) gel(B,j) = gmul(gel(A,j),gel(C,j));
891 1027551 : fft(Omega,B,A,1,Lmax,1);
892 1027672 : fft(Omega,C,B,1,Lmax,1);
893 :
894 1027669 : if (polreal) /* p has real coefficients */
895 : {
896 796636 : if (i>0 && i<K-1)
897 : {
898 102738 : for (j=1; j<=k; j++)
899 : {
900 86099 : gel(W,j) = gadd(gel(W,j), gshift(mulreal(gel(A,j+1),gel(RU,j+1)),1));
901 86099 : gel(U,j) = gadd(gel(U,j), gshift(mulreal(gel(B,j),gel(RU,j)),1));
902 : }
903 : }
904 : else
905 : {
906 1829980 : for (j=1; j<=k; j++)
907 : {
908 1049992 : gel(W,j) = gadd(gel(W,j), mulreal(gel(A,j+1),gel(RU,j+1)));
909 1049977 : gel(U,j) = gadd(gel(U,j), mulreal(gel(B,j),gel(RU,j)));
910 : }
911 : }
912 : }
913 : else
914 : {
915 604946 : for (j=1; j<=k; j++)
916 : {
917 373916 : gel(W,j) = gadd(gel(W,j), gmul(gel(A,j+1),gel(RU,j+1)));
918 373908 : gel(U,j) = gadd(gel(U,j), gmul(gel(B,j),gel(RU,j)));
919 : }
920 : }
921 1027657 : prim2 = gmul(prim2,prim);
922 1027665 : (void)gc_all(ltop,3, &W,&U,&prim2);
923 : }
924 :
925 1201712 : for (i=1; i<=k; i++)
926 : {
927 699492 : aux=gel(W,i);
928 1098231 : for (j=1; j<i; j++) aux = gadd(aux, gmul(gel(W,i-j),gel(F,k+2-j)));
929 699494 : gel(F,k+2-i) = gdivgs(aux,-i*NN);
930 : }
931 1201697 : for (i=0; i<k; i++)
932 : {
933 699480 : aux=gel(U,k-i);
934 1098220 : for (j=1+i; j<k; j++) aux = gadd(aux,gmul(gel(F,2+j),gel(U,j-i)));
935 699482 : gel(H,i+2) = gdivgu(aux,NN);
936 : }
937 502217 : }
938 :
939 : #define NEWTON_MAX 10
940 : static GEN
941 2459795 : refine_H(GEN F, GEN G, GEN HH, long bit, long Sbit)
942 : {
943 2459795 : GEN H = HH, D, aux;
944 2459795 : pari_sp ltop = avma;
945 : long error, i, bit1, bit2;
946 :
947 2459795 : D = Rg_RgX_sub(gen_1, RgX_rem(RgX_mul(H,G),F)); error = gexpo(D);
948 2459767 : bit2 = bit + Sbit;
949 4495855 : for (i=0; error>-bit && i<NEWTON_MAX && error<=0; i++)
950 : {
951 2036101 : if (gc_needed(ltop,1))
952 : {
953 0 : if(DEBUGMEM>1) pari_warn(warnmem,"refine_H");
954 0 : (void)gc_all(ltop,2, &D,&H);
955 : }
956 2036101 : bit1 = -error + Sbit;
957 2036101 : aux = RgX_mul(mygprec(H,bit1), mygprec(D,bit1));
958 2036097 : aux = RgX_rem(mygprec(aux,bit1), mygprec(F,bit1));
959 :
960 2036106 : bit1 = -error*2 + Sbit; if (bit1 > bit2) bit1 = bit2;
961 2036106 : H = RgX_add(mygprec(H,bit1), aux);
962 2036042 : D = Rg_RgX_sub(gen_1, RgX_rem(RgX_mul(H,G),F));
963 2036083 : error = gexpo(D); if (error < -bit1) error = -bit1;
964 : }
965 2459754 : if (error > -bit/2) return NULL; /* FAIL */
966 2459430 : return gc_GEN(ltop,H);
967 : }
968 :
969 : /* return 0 if fails, 1 else */
970 : static long
971 502218 : refine_F(GEN p, GEN *F, GEN *G, GEN H, long bit, double gamma)
972 : {
973 502218 : GEN f0, FF, GG, r, HH = H;
974 502218 : long error, i, bit1 = 0, bit2, Sbit, Sbit2, enh, normF, normG, n = degpol(p);
975 502217 : pari_sp av = avma;
976 :
977 502217 : FF = *F; GG = RgX_divrem(p, FF, &r);
978 502230 : error = gexpo(r); if (error <= -bit) error = 1-bit;
979 502230 : normF = gexpo(FF);
980 502230 : normG = gexpo(GG);
981 502230 : enh = gexpo(H); if (enh < 0) enh = 0;
982 502230 : Sbit = normF + 2*normG + enh + (long)(4.*log2((double)n)+gamma) + 1;
983 502230 : Sbit2 = enh + 2*(normF+normG) + (long)(2.*gamma+5.*log2((double)n)) + 1;
984 502230 : bit2 = bit + Sbit;
985 2961679 : for (i=0; error>-bit && i<NEWTON_MAX && error<=0; i++)
986 : {
987 2459788 : if (bit1 == bit2 && i >= 2) { Sbit += n; Sbit2 += n; bit2 += n; }
988 2459788 : if (gc_needed(av,1))
989 : {
990 0 : if(DEBUGMEM>1) pari_warn(warnmem,"refine_F");
991 0 : (void)gc_all(av,4, &FF,&GG,&r,&HH);
992 : }
993 :
994 2459788 : bit1 = -error + Sbit2;
995 2459788 : HH = refine_H(mygprec(FF,bit1), mygprec(GG,bit1), mygprec(HH,bit1),
996 : 1-error, Sbit2);
997 2459804 : if (!HH) return 0; /* FAIL */
998 :
999 2459480 : bit1 = -error + Sbit;
1000 2459480 : r = RgX_mul(mygprec(HH,bit1), mygprec(r,bit1));
1001 2459434 : f0 = RgX_rem(mygprec(r,bit1), mygprec(FF,bit1));
1002 :
1003 2459464 : bit1 = -2*error + Sbit; if (bit1 > bit2) bit1 = bit2;
1004 2459464 : FF = gadd(mygprec(FF,bit1),f0);
1005 :
1006 2459435 : bit1 = -3*error + Sbit; if (bit1 > bit2) bit1 = bit2;
1007 2459435 : GG = RgX_divrem(mygprec(p,bit1), mygprec(FF,bit1), &r);
1008 2459454 : error = gexpo(r); if (error < -bit1) error = -bit1;
1009 : }
1010 501891 : if (error>-bit) return 0; /* FAIL */
1011 493823 : *F = FF; *G = GG; return 1;
1012 : }
1013 :
1014 : /* returns F and G from the unit circle U such that |p-FG|<2^(-bit) |cd|,
1015 : where cd is the leading coefficient of p */
1016 : static void
1017 493839 : split_fromU(GEN p, long k, double delta, long bit,
1018 : GEN *F, GEN *G, double param, double param2)
1019 : {
1020 : GEN pp, FF, GG, H;
1021 493839 : long n = degpol(p), NN, bit2, Lmax;
1022 493839 : int polreal = isreal(p);
1023 : pari_sp ltop;
1024 : double mu, gamma;
1025 :
1026 493839 : pp = gdiv(p, gel(p,2+n));
1027 493836 : parameters(pp, &Lmax,&mu,&gamma, polreal,param,param2);
1028 :
1029 493830 : H = cgetg(k+2,t_POL); H[1] = p[1];
1030 493830 : FF = cgetg(k+3,t_POL); FF[1]= p[1];
1031 493833 : gel(FF,k+2) = gen_1;
1032 :
1033 493833 : NN = (long)(0.5/delta); NN |= 1; if (NN < 2) NN = 2;
1034 493833 : NN *= Lmax; ltop = avma;
1035 : for(;;)
1036 : {
1037 502225 : bit2 = (long)(((double)NN*delta-mu)/M_LN2) + gexpo(pp) + 8;
1038 502227 : dft(pp, k, NN, Lmax, bit2, FF, H, polreal);
1039 502218 : if (refine_F(pp,&FF,&GG,H,bit,gamma)) break;
1040 8392 : NN <<= 1; set_avma(ltop);
1041 : }
1042 493838 : *G = gmul(GG,gel(p,2+n)); *F = FF;
1043 493835 : }
1044 :
1045 : static void
1046 493839 : optimize_split(GEN p, long k, double delta, long bit,
1047 : GEN *F, GEN *G, double param, double param2)
1048 : {
1049 493839 : long n = degpol(p);
1050 : GEN FF, GG;
1051 :
1052 493839 : if (k <= n/2)
1053 383017 : split_fromU(p,k,delta,bit,F,G,param,param2);
1054 : else
1055 : {
1056 110822 : split_fromU(RgX_recip_i(p),n-k,delta,bit,&FF,&GG,param,param2);
1057 110821 : *F = RgX_recip_i(GG);
1058 110821 : *G = RgX_recip_i(FF);
1059 : }
1060 493835 : }
1061 :
1062 : /********************************************************************/
1063 : /** **/
1064 : /** SEARCH FOR SEPARATING CIRCLE **/
1065 : /** **/
1066 : /********************************************************************/
1067 :
1068 : /* return p(2^e*x) *2^(-n*e) */
1069 : static void
1070 0 : scalepol2n(GEN p, long e)
1071 : {
1072 0 : long i,n=lg(p)-1;
1073 0 : for (i=2; i<=n; i++) gel(p,i) = gmul2n(gel(p,i),(i-n)*e);
1074 0 : }
1075 :
1076 : /* returns p(x/R)*R^n; assume R is at the correct accuracy */
1077 : static GEN
1078 4288159 : scalepol(GEN p, GEN R, long bit)
1079 4288159 : { return RgX_rescale(mygprec(p, bit), R); }
1080 :
1081 : /* return (conj(a)X-1)^n * p[ (X-a) / (conj(a)X-1) ] */
1082 : static GEN
1083 1403472 : conformal_basecase(GEN p, GEN a)
1084 : {
1085 : GEN z, r, ma, ca;
1086 1403472 : long i, n = degpol(p);
1087 : pari_sp av;
1088 :
1089 1403472 : if (n <= 0) return p;
1090 1403472 : ma = gneg(a); ca = conj_i(a);
1091 1403475 : av = avma;
1092 1403475 : z = deg1pol_shallow(ca, gen_m1, 0);
1093 1403471 : r = scalarpol_shallow(gel(p,2+n), 0);
1094 3640385 : for (i=n-1; ; i--)
1095 : {
1096 3640385 : r = RgX_addmulXn_shallow(r, gmul(ma,r), 1); /* r *= (X - a) */
1097 3640362 : r = gadd(r, gmul(z, gel(p,2+i)));
1098 3640342 : if (i == 0) return gc_upto(av, r);
1099 2236885 : z = RgX_addmulXn_shallow(gmul(z,ca), gneg(z), 1); /* z *= conj(a)X - 1 */
1100 2236915 : if (gc_needed(av,2))
1101 : {
1102 0 : if(DEBUGMEM>1) pari_warn(warnmem,"conformal_pol (%ld/%ld)",n-i, n);
1103 0 : (void)gc_all(av,2, &r,&z);
1104 : }
1105 : }
1106 : }
1107 : static GEN
1108 1403592 : conformal_pol(GEN p, GEN a)
1109 : {
1110 1403592 : pari_sp av = avma;
1111 1403592 : long d, nR, n = degpol(p), v;
1112 : GEN Q, R, S, T;
1113 1403591 : if (n < 35) return conformal_basecase(p, a);
1114 119 : d = (n+1) >> 1; v = varn(p);
1115 119 : Q = RgX_shift_shallow(p, -d);
1116 119 : R = RgXn_red_shallow(p, d);
1117 119 : Q = conformal_pol(Q, a);
1118 119 : R = conformal_pol(R, a);
1119 119 : S = gpowgs(deg1pol_shallow(gen_1, gneg(a), v), d);
1120 119 : T = RgX_recip_i(S);
1121 119 : if (typ(a) == t_COMPLEX) T = gconj(T);
1122 119 : if (odd(d)) T = RgX_neg(T);
1123 : /* S = (X - a)^d, T = (conj(a) X - 1)^d */
1124 119 : nR = n - degpol(R) - d; /* >= 0 */
1125 119 : if (nR) T = RgX_mul(T, gpowgs(deg1pol_shallow(gconj(a), gen_m1, v), nR));
1126 119 : return gc_upto(av, RgX_add(RgX_mul(Q, S), RgX_mul(R, T)));
1127 : }
1128 :
1129 : static const double UNDEF = -100000.;
1130 :
1131 : static double
1132 493834 : logradius(double *radii, GEN p, long k, double aux, double *delta)
1133 : {
1134 493834 : long i, n = degpol(p);
1135 : double lrho, lrmin, lrmax;
1136 493834 : if (k > 1)
1137 : {
1138 282490 : i = k-1; while (i>0 && radii[i] == UNDEF) i--;
1139 207029 : lrmin = logpre_modulus(p,k,aux, radii[i], radii[k]);
1140 : }
1141 : else /* k=1 */
1142 286805 : lrmin = logmin_modulus(p,aux);
1143 493838 : radii[k] = lrmin;
1144 :
1145 493838 : if (k+1<n)
1146 : {
1147 590828 : i = k+2; while (i<=n && radii[i] == UNDEF) i++;
1148 399592 : lrmax = logpre_modulus(p,k+1,aux, radii[k+1], radii[i]);
1149 : }
1150 : else /* k+1=n */
1151 94246 : lrmax = logmax_modulus(p,aux);
1152 493837 : radii[k+1] = lrmax;
1153 :
1154 493837 : lrho = radii[k];
1155 824054 : for (i=k-1; i>=1; i--)
1156 : {
1157 330217 : if (radii[i] == UNDEF || radii[i] > lrho)
1158 242324 : radii[i] = lrho;
1159 : else
1160 87893 : lrho = radii[i];
1161 : }
1162 493837 : lrho = radii[k+1];
1163 1637744 : for (i=k+1; i<=n; i++)
1164 : {
1165 1143907 : if (radii[i] == UNDEF || radii[i] < lrho)
1166 567091 : radii[i] = lrho;
1167 : else
1168 576816 : lrho = radii[i];
1169 : }
1170 493837 : *delta = (lrmax - lrmin) / 2;
1171 493837 : if (*delta > 1.) *delta = 1.;
1172 493837 : return (lrmin + lrmax) / 2;
1173 : }
1174 :
1175 : static void
1176 493837 : update_radius(long n, double *radii, double lrho, double *par, double *par2)
1177 : {
1178 493837 : double t, param = 0., param2 = 0.;
1179 : long i;
1180 2461694 : for (i=1; i<=n; i++)
1181 : {
1182 1967885 : radii[i] -= lrho;
1183 1967885 : t = fabs(rtodbl( invr(subsr(1, dblexp(radii[i]))) ));
1184 1967857 : param += t; if (t > 1.) param2 += log2(t);
1185 : }
1186 493809 : *par = param; *par2 = param2;
1187 493809 : }
1188 :
1189 : /* apply the conformal mapping then split from U */
1190 : static void
1191 467788 : conformal_mapping(double *radii, GEN ctr, GEN p, long k, long bit,
1192 : double aux, GEN *F,GEN *G)
1193 : {
1194 467788 : long bit2, n = degpol(p), i;
1195 467788 : pari_sp ltop = avma, av;
1196 : GEN q, FF, GG, a, R;
1197 : double lrho, delta, param, param2;
1198 : /* n * (2.*log2(2.732)+log2(1.5)) + 1 */
1199 467788 : bit2 = bit + (long)(n*3.4848775) + 1;
1200 467788 : a = sqrtr_abs( utor(3, precdbl(MEDDEFAULTPREC)) );
1201 467788 : a = divrs(a, -6);
1202 467787 : a = gmul(mygprec(a,bit2), mygprec(ctr,bit2)); /* a = -ctr/2sqrt(3) */
1203 :
1204 467789 : av = avma;
1205 467789 : q = conformal_pol(mygprec(p,bit2), a);
1206 2288026 : for (i=1; i<=n; i++)
1207 1820242 : if (radii[i] != UNDEF) /* update array radii */
1208 : {
1209 1540661 : pari_sp av2 = avma;
1210 1540661 : GEN t, r = dblexp(radii[i]), r2 = sqrr(r);
1211 : /* 2(r^2 - 1) / (r^2 - 3(r-1)) */
1212 1540604 : t = divrr(shiftr((subrs(r2,1)),1), subrr(r2, mulur(3,subrs(r,1))));
1213 1540659 : radii[i] = mydbllogr(addsr(1,t)) / 2;
1214 1540645 : set_avma(av2);
1215 : }
1216 467784 : lrho = logradius(radii, q,k,aux/10., &delta);
1217 467787 : update_radius(n, radii, lrho, ¶m, ¶m2);
1218 :
1219 467783 : bit2 += (long)(n * fabs(lrho)/M_LN2 + 1.);
1220 467783 : R = mygprec(dblexp(-lrho), bit2);
1221 467786 : q = scalepol(q,R,bit2);
1222 467786 : (void)gc_all(av,2, &q,&R);
1223 :
1224 467789 : optimize_split(q,k,delta,bit2,&FF,&GG,param,param2);
1225 467785 : bit2 += n; R = invr(R);
1226 467786 : FF = scalepol(FF,R,bit2);
1227 467785 : GG = scalepol(GG,R,bit2);
1228 :
1229 467786 : a = mygprec(a,bit2);
1230 467787 : FF = conformal_pol(FF,a);
1231 467787 : GG = conformal_pol(GG,a);
1232 :
1233 467788 : a = invr(subsr(1, gnorm(a)));
1234 467786 : FF = RgX_Rg_mul(FF, powru(a,k));
1235 467787 : GG = RgX_Rg_mul(GG, powru(a,n-k));
1236 :
1237 467788 : *F = mygprec(FF,bit+n);
1238 467789 : *G = mygprec(GG,bit+n); (void)gc_all(ltop,2, F,G);
1239 467789 : }
1240 :
1241 : /* split p, this time without scaling. returns in F and G two polynomials
1242 : * such that |p-FG|< 2^(-bit)|p| */
1243 : static void
1244 493839 : split_2(GEN p, long bit, GEN ctr, double thickness, GEN *F, GEN *G)
1245 : {
1246 : GEN q, FF, GG, R;
1247 : double aux, delta, param, param2;
1248 493839 : long n = degpol(p), i, j, k, bit2;
1249 : double lrmin, lrmax, lrho, *radii;
1250 :
1251 493839 : radii = (double*) stack_malloc_align((n+1) * sizeof(double), sizeof(double));
1252 :
1253 1474128 : for (i=2; i<n; i++) radii[i] = UNDEF;
1254 493839 : aux = thickness/(double)(4 * n);
1255 493839 : lrmin = logmin_modulus(p, aux);
1256 493836 : lrmax = logmax_modulus(p, aux);
1257 493834 : radii[1] = lrmin;
1258 493834 : radii[n] = lrmax;
1259 493834 : i = 1; j = n;
1260 493834 : lrho = (lrmin + lrmax) / 2;
1261 493834 : k = dual_modulus(p, lrho, aux, 1);
1262 493839 : if (5*k < n || (n < 2*k && 5*k < 4*n))
1263 77972 : { lrmax = lrho; j=k+1; radii[j] = lrho; }
1264 : else
1265 415867 : { lrmin = lrho; i=k; radii[i] = lrho; }
1266 1012193 : while (j > i+1)
1267 : {
1268 518357 : if (i+j == n+1)
1269 372091 : lrho = (lrmin + lrmax) / 2;
1270 : else
1271 : {
1272 146266 : double kappa = 2. - log(1. + minss(i,n-j)) / log(1. + minss(j,n-i));
1273 146266 : if (i+j < n+1) lrho = lrmax * kappa + lrmin;
1274 116672 : else lrho = lrmin * kappa + lrmax;
1275 146266 : lrho /= 1+kappa;
1276 : }
1277 518357 : aux = (lrmax - lrmin) / (4*(j-i));
1278 518357 : k = dual_modulus(p, lrho, aux, minss(i,n+1-j));
1279 518354 : if (k-i < j-k-1 || (k-i == j-k-1 && 2*k > n))
1280 387148 : { lrmax = lrho; j=k+1; radii[j] = lrho - aux; }
1281 : else
1282 131206 : { lrmin = lrho; i=k; radii[i] = lrho + aux; }
1283 : }
1284 493836 : aux = lrmax - lrmin;
1285 :
1286 493836 : if (ctr)
1287 : {
1288 467786 : lrho = (lrmax + lrmin) / 2;
1289 2288054 : for (i=1; i<=n; i++)
1290 1820268 : if (radii[i] != UNDEF) radii[i] -= lrho;
1291 :
1292 467786 : bit2 = bit + (long)(n * fabs(lrho)/M_LN2 + 1.);
1293 467786 : R = mygprec(dblexp(-lrho), bit2);
1294 467785 : q = scalepol(p,R,bit2);
1295 467788 : conformal_mapping(radii, ctr, q, k, bit2, aux, &FF, &GG);
1296 : }
1297 : else
1298 : {
1299 26050 : lrho = logradius(radii, p, k, aux/10., &delta);
1300 26050 : update_radius(n, radii, lrho, ¶m, ¶m2);
1301 :
1302 26050 : bit2 = bit + (long)(n * fabs(lrho)/M_LN2 + 1.);
1303 26050 : R = mygprec(dblexp(-lrho), bit2);
1304 26050 : q = scalepol(p,R,bit2);
1305 26050 : optimize_split(q, k, delta, bit2, &FF, &GG, param, param2);
1306 : }
1307 493839 : bit += n;
1308 493839 : bit2 += n; R = invr(mygprec(R,bit2));
1309 493839 : *F = mygprec(scalepol(FF,R,bit2), bit);
1310 493838 : *G = mygprec(scalepol(GG,R,bit2), bit);
1311 493838 : }
1312 :
1313 : /* procedure corresponding to steps 5,6,.. page 44 in RR n. 1852 */
1314 : /* put in F and G two polynomial such that |p-FG|<2^(-bit)|p|
1315 : * where the maximum modulus of the roots of p is <=1.
1316 : * Assume sum of roots is 0. */
1317 : static void
1318 467787 : split_1(GEN p, long bit, GEN *F, GEN *G)
1319 : {
1320 467787 : long i, imax, n = degpol(p), polreal = isreal(p), ep = gexpo(p), bit2 = bit+n;
1321 : GEN ctr, q, qq, FF, GG, v, gr, r, newq;
1322 : double lrmin, lrmax, lthick;
1323 467787 : const double LOG3 = 1.098613;
1324 :
1325 467787 : lrmax = logmax_modulus(p, 0.01);
1326 467784 : gr = mygprec(dblexp(-lrmax), bit2);
1327 467787 : q = scalepol(p,gr,bit2);
1328 :
1329 467786 : bit2 = bit + gexpo(q) - ep + (long)((double)n*2.*log2(3.)+1);
1330 467787 : v = cgetg(5,t_VEC);
1331 467787 : gel(v,1) = gen_2;
1332 467787 : gel(v,2) = gen_m2;
1333 467787 : gel(v,3) = mkcomplex(gen_0, gel(v,1));
1334 467787 : gel(v,4) = mkcomplex(gen_0, gel(v,2));
1335 467787 : q = mygprec(q,bit2); lthick = 0;
1336 467787 : newq = ctr = NULL; /* -Wall */
1337 467787 : imax = polreal? 3: 4;
1338 840373 : for (i=1; i<=imax; i++)
1339 : {
1340 833754 : qq = RgX_Rg_translate(q, gel(v,i));
1341 833758 : lrmin = logmin_modulus(qq,0.05);
1342 833759 : if (LOG3 > lrmin + lthick)
1343 : {
1344 821605 : double lquo = logmax_modulus(qq,0.05) - lrmin;
1345 821598 : if (lquo > lthick) { lthick = lquo; newq = qq; ctr = gel(v,i); }
1346 : }
1347 833752 : if (lthick > M_LN2) break;
1348 423633 : if (polreal && i==2 && lthick > LOG3 - M_LN2) break;
1349 : }
1350 467785 : bit2 = bit + gexpo(newq) - ep + (long)(n*LOG3/M_LN2 + 1);
1351 467789 : split_2(newq, bit2, ctr, lthick, &FF, &GG);
1352 467788 : r = gneg(mygprec(ctr,bit2));
1353 467789 : FF = RgX_Rg_translate(FF,r);
1354 467789 : GG = RgX_Rg_translate(GG,r);
1355 :
1356 467789 : gr = invr(gr); bit2 = bit - ep + gexpo(FF)+gexpo(GG);
1357 467789 : *F = scalepol(FF,gr,bit2);
1358 467788 : *G = scalepol(GG,gr,bit2);
1359 467789 : }
1360 :
1361 : /* put in F and G two polynomials such that |P-FG|<2^(-bit)|P|,
1362 : where the maximum modulus of the roots of p is < 0.5 */
1363 : static int
1364 468110 : split_0_2(GEN p, long bit, GEN *F, GEN *G)
1365 : {
1366 : GEN q, b;
1367 468110 : long n = degpol(p), k, bit2, eq;
1368 468110 : double aux0 = dbllog2(gel(p,n+2)); /* != -oo */
1369 468110 : double aux1 = dbllog2(gel(p,n+1)), aux;
1370 :
1371 468111 : if (aux1 == -pariINFINITY) /* p1 = 0 */
1372 9892 : aux = 0;
1373 : else
1374 : {
1375 458219 : aux = aux1 - aux0; /* log2(p1/p0) */
1376 : /* beware double overflow */
1377 458219 : if (aux >= 0 && (aux > 1e4 || exp2(aux) > 2.5*n)) return 0;
1378 458219 : aux = (aux < -300)? 0.: n*log2(1 + exp2(aux)/(double)n);
1379 : }
1380 468111 : bit2 = bit+1 + (long)(log2((double)n) + aux);
1381 468111 : q = mygprec(p,bit2);
1382 468112 : if (aux1 == -pariINFINITY) b = NULL;
1383 : else
1384 : {
1385 458220 : b = gdivgs(gdiv(gel(q,n+1),gel(q,n+2)),-n);
1386 458220 : q = RgX_Rg_translate(q,b);
1387 : }
1388 468113 : gel(q,n+1) = gen_0; eq = gexpo(q);
1389 468111 : k = 0;
1390 468665 : while (k <= n/2 && (- gexpo(gel(q,k+2)) > bit2 + 2*(n-k) + eq
1391 468537 : || gequal0(gel(q,k+2)))) k++;
1392 468111 : if (k > 0)
1393 : {
1394 324 : if (k > n/2) k = n/2;
1395 324 : bit2 += k<<1;
1396 324 : *F = pol_xn(k, 0);
1397 324 : *G = RgX_shift_shallow(q, -k);
1398 : }
1399 : else
1400 : {
1401 467787 : split_1(q,bit2,F,G);
1402 467789 : bit2 = bit + gexpo(*F) + gexpo(*G) - gexpo(p) + (long)aux+1;
1403 467788 : *F = mygprec(*F,bit2);
1404 : }
1405 468113 : *G = mygprec(*G,bit2);
1406 468111 : if (b)
1407 : {
1408 458219 : GEN mb = mygprec(gneg(b), bit2);
1409 458219 : *F = RgX_Rg_translate(*F, mb);
1410 458221 : *G = RgX_Rg_translate(*G, mb);
1411 : }
1412 468113 : return 1;
1413 : }
1414 :
1415 : /* put in F and G two polynomials such that |P-FG|<2^(-bit)|P|.
1416 : * Assume max_modulus(p) < 2 */
1417 : static void
1418 468110 : split_0_1(GEN p, long bit, GEN *F, GEN *G)
1419 : {
1420 : GEN FF, GG;
1421 : long n, bit2, normp;
1422 :
1423 468110 : if (split_0_2(p,bit,F,G)) return;
1424 :
1425 0 : normp = gexpo(p);
1426 0 : scalepol2n(p,2); /* p := 4^(-n) p(4*x) */
1427 0 : n = degpol(p); bit2 = bit + 2*n + gexpo(p) - normp;
1428 0 : split_1(mygprec(p,bit2), bit2,&FF,&GG);
1429 0 : scalepol2n(FF,-2);
1430 0 : scalepol2n(GG,-2); bit2 = bit + gexpo(FF) + gexpo(GG) - normp;
1431 0 : *F = mygprec(FF,bit2);
1432 0 : *G = mygprec(GG,bit2);
1433 : }
1434 :
1435 : /* put in F and G two polynomials such that |P-FG|<2^(-bit)|P| */
1436 : static void
1437 494158 : split_0(GEN p, long bit, GEN *F, GEN *G)
1438 : {
1439 494158 : const double LOG1_9 = 0.6418539;
1440 494158 : long n = degpol(p), k = 0;
1441 : GEN q;
1442 :
1443 494159 : while (gexpo(gel(p,k+2)) < -bit && k <= n/2) k++;
1444 494159 : if (k > 0)
1445 : {
1446 0 : if (k > n/2) k = n/2;
1447 0 : *F = pol_xn(k, 0);
1448 0 : *G = RgX_shift_shallow(p, -k);
1449 : }
1450 : else
1451 : {
1452 494159 : double lr = logmax_modulus(p, 0.05);
1453 494158 : if (lr < LOG1_9) split_0_1(p, bit, F, G);
1454 : else
1455 : {
1456 436673 : q = RgX_recip_i(p);
1457 436674 : lr = logmax_modulus(q,0.05);
1458 436675 : if (lr < LOG1_9)
1459 : {
1460 410625 : split_0_1(q, bit, F, G);
1461 410628 : *F = RgX_recip_i(*F);
1462 410628 : *G = RgX_recip_i(*G);
1463 : }
1464 : else
1465 26050 : split_2(p,bit,NULL, 1.2837,F,G);
1466 : }
1467 : }
1468 494163 : }
1469 :
1470 : /********************************************************************/
1471 : /** **/
1472 : /** ERROR ESTIMATE FOR THE ROOTS **/
1473 : /** **/
1474 : /********************************************************************/
1475 :
1476 : static GEN
1477 1899226 : root_error(long n, long k, GEN roots_pol, long err, GEN shatzle)
1478 : {
1479 1899226 : GEN rho, d, eps, epsbis, eps2, aux, rap = NULL;
1480 : long i, j;
1481 :
1482 1899226 : d = cgetg(n+1,t_VEC);
1483 12131135 : for (i=1; i<=n; i++)
1484 : {
1485 10232076 : if (i!=k)
1486 : {
1487 8332933 : aux = gsub(gel(roots_pol,i), gel(roots_pol,k));
1488 8332330 : gel(d,i) = gabs(mygprec(aux,31), DEFAULTPREC);
1489 : }
1490 : }
1491 1899059 : rho = gabs(mygprec(gel(roots_pol,k),31), DEFAULTPREC);
1492 1899226 : if (expo(rho) < 0) rho = real_1(DEFAULTPREC);
1493 1899226 : eps = mulrr(rho, shatzle);
1494 1899155 : aux = shiftr(powru(rho,n), err);
1495 :
1496 5764958 : for (j=1; j<=2 || (j<=5 && cmprr(rap, dbltor(1.2)) > 0); j++)
1497 : {
1498 3865865 : GEN prod = NULL; /* 1. */
1499 3865865 : long m = n;
1500 3865865 : epsbis = mulrr(eps, dbltor(1.25));
1501 26546665 : for (i=1; i<=n; i++)
1502 : {
1503 22680684 : if (i != k && cmprr(gel(d,i),epsbis) > 0)
1504 : {
1505 18775483 : GEN dif = subrr(gel(d,i),eps);
1506 18772803 : prod = prod? mulrr(prod, dif): dif;
1507 18774481 : m--;
1508 : }
1509 : }
1510 3865981 : eps2 = prod? divrr(aux, prod): aux;
1511 3865854 : if (m > 1) eps2 = sqrtnr(shiftr(eps2, 2*m-2), m);
1512 3865854 : rap = divrr(eps,eps2); eps = eps2;
1513 : }
1514 1899010 : return eps;
1515 : }
1516 :
1517 : /* round a complex or real number x to an absolute value of 2^(-bit) */
1518 : static GEN
1519 4300420 : mygprec_absolute(GEN x, long bit)
1520 : {
1521 : long e;
1522 : GEN y;
1523 :
1524 4300420 : switch(typ(x))
1525 : {
1526 2952711 : case t_REAL:
1527 2952711 : e = expo(x) + bit;
1528 2952711 : return (e <= 0 || !signe(x))? real_0_bit(-bit): rtor(x, nbits2prec(e));
1529 1217938 : case t_COMPLEX:
1530 1217938 : if (gexpo(gel(x,2)) < -bit) return mygprec_absolute(gel(x,1),bit);
1531 1183378 : y = cgetg(3,t_COMPLEX);
1532 1183380 : gel(y,1) = mygprec_absolute(gel(x,1),bit);
1533 1183387 : gel(y,2) = mygprec_absolute(gel(x,2),bit);
1534 1183401 : return y;
1535 129771 : default: return x;
1536 : }
1537 : }
1538 :
1539 : static long
1540 530663 : a_posteriori_errors(GEN p, GEN roots_pol, long err)
1541 : {
1542 530663 : long i, n = degpol(p), e_max = -(long)EXPOBITS;
1543 : GEN sigma, shatzle;
1544 :
1545 530663 : err += (long)log2((double)n) + 1;
1546 530663 : if (err > -2) return 0;
1547 530663 : sigma = real2n(-err, LOWDEFAULTPREC);
1548 : /* 2 / ((s - 1)^(1/n) - 1) */
1549 530663 : shatzle = divur(2, subrs(sqrtnr(subrs(sigma,1),n), 1));
1550 2429887 : for (i=1; i<=n; i++)
1551 : {
1552 1899223 : pari_sp av = avma;
1553 1899223 : GEN x = root_error(n,i,roots_pol,err,shatzle);
1554 1899004 : long e = gexpo(x);
1555 1899055 : set_avma(av); if (e > e_max) e_max = e;
1556 1899141 : gel(roots_pol,i) = mygprec_absolute(gel(roots_pol,i), -e);
1557 : }
1558 530664 : return e_max;
1559 : }
1560 :
1561 : /********************************************************************/
1562 : /** **/
1563 : /** MAIN **/
1564 : /** **/
1565 : /********************************************************************/
1566 : static GEN
1567 1603057 : append_clone(GEN r, GEN a) { a = gclone(a); vectrunc_append(r, a); return a; }
1568 :
1569 : /* put roots in placeholder roots_pol so that |P - L_1...L_n| < 2^(-bit)|P|
1570 : * returns prod (x-roots_pol[i]) */
1571 : static GEN
1572 1518981 : split_complete(GEN p, long bit, GEN roots_pol)
1573 : {
1574 1518981 : long n = degpol(p);
1575 : pari_sp ltop;
1576 : GEN p1, F, G, a, b, m1, m2;
1577 :
1578 1518979 : if (n == 1)
1579 : {
1580 446575 : a = gneg_i(gdiv(gel(p,2), gel(p,3)));
1581 446578 : (void)append_clone(roots_pol,a); return p;
1582 : }
1583 1072404 : ltop = avma;
1584 1072404 : if (n == 2)
1585 : {
1586 578246 : F = gsub(gsqr(gel(p,3)), gmul2n(gmul(gel(p,2),gel(p,4)), 2));
1587 578238 : F = gsqrt(F, nbits2prec(bit));
1588 578247 : p1 = ginv(gmul2n(gel(p,4),1));
1589 578246 : a = gneg_i(gmul(gadd(F,gel(p,3)), p1));
1590 578247 : b = gmul(gsub(F,gel(p,3)), p1);
1591 578240 : a = append_clone(roots_pol,a);
1592 578247 : b = append_clone(roots_pol,b); set_avma(ltop);
1593 578246 : a = mygprec(a, 3*bit);
1594 578242 : b = mygprec(b, 3*bit);
1595 578245 : return gmul(gel(p,4), mkpoln(3, gen_1, gneg(gadd(a,b)), gmul(a,b)));
1596 : }
1597 494158 : split_0(p,bit,&F,&G);
1598 494163 : m1 = split_complete(F,bit,roots_pol);
1599 494162 : m2 = split_complete(G,bit,roots_pol);
1600 494158 : return gc_upto(ltop, gmul(m1,m2));
1601 : }
1602 :
1603 : static GEN
1604 6961290 : quicktofp(GEN x)
1605 : {
1606 6961290 : const long prec = DEFAULTPREC;
1607 6961290 : switch(typ(x))
1608 : {
1609 6939833 : case t_INT: return itor(x, prec);
1610 9064 : case t_REAL: return rtor(x, prec);
1611 0 : case t_FRAC: return fractor(x, prec);
1612 12395 : case t_COMPLEX: {
1613 12395 : GEN a = gel(x,1), b = gel(x,2);
1614 : /* avoid problem with 0, e.g. x = 0 + I*1e-100. We don't want |x| = 0. */
1615 12395 : if (isintzero(a)) return cxcompotor(b, prec);
1616 12353 : if (isintzero(b)) return cxcompotor(a, prec);
1617 12353 : a = cxcompotor(a, prec);
1618 12353 : b = cxcompotor(b, prec); return sqrtr(addrr(sqrr(a), sqrr(b)));
1619 : }
1620 0 : default: pari_err_TYPE("quicktofp",x);
1621 : return NULL;/*LCOV_EXCL_LINE*/
1622 : }
1623 :
1624 : }
1625 :
1626 : /* bound log_2 |largest root of p| (Fujiwara's bound) */
1627 : double
1628 2265135 : fujiwara_bound(GEN p)
1629 : {
1630 2265135 : pari_sp av = avma;
1631 2265135 : long i, n = degpol(p);
1632 : GEN cc;
1633 : double loglc, Lmax;
1634 :
1635 2265134 : if (n <= 0) pari_err_CONSTPOL("fujiwara_bound");
1636 2265134 : loglc = mydbllog2r( quicktofp(gel(p,n+2)) ); /* log_2 |lc(p)| */
1637 2265109 : cc = gel(p, 2);
1638 2265109 : if (gequal0(cc))
1639 783533 : Lmax = -pariINFINITY-1;
1640 : else
1641 1481592 : Lmax = (mydbllog2r(quicktofp(cc)) - loglc - 1) / n;
1642 7393745 : for (i = 1; i < n; i++)
1643 : {
1644 5128646 : GEN y = gel(p,i+2);
1645 : double L;
1646 5128646 : if (gequal0(y)) continue;
1647 3214676 : L = (mydbllog2r(quicktofp(y)) - loglc) / (n-i);
1648 3214673 : if (L > Lmax) Lmax = L;
1649 : }
1650 2265099 : return gc_double(av, Lmax+1);
1651 : }
1652 :
1653 : /* Fujiwara's bound, real roots. Based on the following remark: if
1654 : * p = x^n + sum a_i x^i and q = x^n + sum min(a_i,0)x^i
1655 : * then for all x >= 0, p(x) >= q(x). Thus any bound for the (positive) roots
1656 : * of q is a bound for the positive roots of p. */
1657 : double
1658 1406878 : fujiwara_bound_real(GEN p, long sign)
1659 : {
1660 1406878 : pari_sp av = avma;
1661 : GEN x;
1662 1406878 : long n = degpol(p), i, signodd, signeven;
1663 1406877 : if (n <= 0) pari_err_CONSTPOL("fujiwara_bound");
1664 1406877 : x = shallowcopy(p);
1665 1406880 : if (gsigne(gel(x, n+2)) > 0)
1666 1406859 : { signeven = 1; signodd = sign; }
1667 : else
1668 21 : { signeven = -1; signodd = -sign; }
1669 5780265 : for (i = 0; i < n; i++)
1670 : {
1671 4373384 : if ((n - i) % 2)
1672 2491040 : { if (gsigne(gel(x, i+2)) == signodd ) gel(x, i+2) = gen_0; }
1673 : else
1674 1882344 : { if (gsigne(gel(x, i+2)) == signeven) gel(x, i+2) = gen_0; }
1675 : }
1676 1406881 : return gc_double(av, fujiwara_bound(x));
1677 : }
1678 :
1679 : static GEN
1680 2161081 : mygprecrc_special(GEN x, long prec, long e)
1681 : {
1682 : GEN y;
1683 2161081 : switch(typ(x))
1684 : {
1685 37263 : case t_REAL:
1686 37263 : if (!signe(x)) return real_0_bit(minss(e, expo(x)));
1687 36115 : return (prec > realprec(x))? rtor(x, prec): x;
1688 13678 : case t_COMPLEX:
1689 13678 : y = cgetg(3,t_COMPLEX);
1690 13678 : gel(y,1) = mygprecrc_special(gel(x,1),prec,e);
1691 13678 : gel(y,2) = mygprecrc_special(gel(x,2),prec,e);
1692 13678 : return y;
1693 2110140 : default: return x;
1694 : }
1695 : }
1696 :
1697 : /* like mygprec but keep at least the same precision as before */
1698 : static GEN
1699 530662 : mygprec_special(GEN x, long bit)
1700 : {
1701 530662 : long e = gexpo(x) - bit, prec = nbits2prec(bit);
1702 530660 : switch(typ(x))
1703 : {
1704 2664385 : case t_POL: pari_APPLY_pol_normalized(mygprecrc_special(gel(x,i),prec,e));
1705 0 : default: return mygprecrc_special(x,prec,e);
1706 : }
1707 : }
1708 :
1709 : static GEN
1710 394088 : fix_roots1(GEN R)
1711 : {
1712 394088 : long i, l = lg(R);
1713 394088 : GEN v = cgetg(l, t_VEC);
1714 1751895 : for (i=1; i < l; i++) { GEN r = gel(R,i); gel(v,i) = gcopy(r); gunclone(r); }
1715 394091 : return v;
1716 : }
1717 : static GEN
1718 530662 : fix_roots(GEN R, long h, long bit)
1719 : {
1720 : long i, j, c, n, prec;
1721 : GEN v, Z, gh;
1722 :
1723 530662 : if (h == 1) return fix_roots1(R);
1724 136574 : prec = nbits2prec(bit); Z = grootsof1(h, prec); gh = utoipos(h);
1725 136574 : n = lg(R)-1; v = cgetg(h*n + 1, t_VEC);
1726 381834 : for (c = i = 1; i <= n; i++)
1727 : {
1728 245262 : GEN s, r = gel(R,i);
1729 245262 : s = (h == 2)? gsqrt(r, prec): gsqrtn(r, gh, NULL, prec);
1730 786693 : for (j = 1; j <= h; j++) gel(v, c++) = gmul(s, gel(Z,j));
1731 245242 : gunclone(r);
1732 : }
1733 136572 : return v;
1734 : }
1735 :
1736 : static GEN
1737 529613 : all_roots(GEN p, long bit)
1738 : {
1739 529613 : long bit2, i, e, h, n = degpol(p), elc = gexpo(leading_coeff(p));
1740 529614 : GEN q, R, m, pd = RgX_deflate_max(p, &h);
1741 529613 : double fb = fujiwara_bound(pd);
1742 : pari_sp av;
1743 :
1744 529614 : if (fb < 0) fb = 0;
1745 529614 : bit2 = bit + maxss(gexpo(p), 0) + (long)ceil(log2(n / h) + 2 * fb);
1746 530663 : for (av = avma, i = 1, e = 0;; i++, set_avma(av))
1747 : {
1748 530663 : R = vectrunc_init(n+1);
1749 530663 : bit2 += e + (n << i);
1750 530663 : q = RgX_gtofp_bit(mygprec(pd,bit2), bit2);
1751 530658 : q[1] = evalsigne(1)|evalvarn(0);
1752 530658 : m = split_complete(q, bit2, R);
1753 530662 : R = fix_roots(R, h, bit2);
1754 530662 : q = mygprec_special(pd,bit2);
1755 530658 : q[1] = evalsigne(1)|evalvarn(0);
1756 530658 : e = gexpo(RgX_sub(q, m)) - elc + (long)log2((double)n) + 1;
1757 530663 : if (e < 0)
1758 : {
1759 530663 : if (e < -2*bit2) e = -2*bit2; /* avoid e = -oo */
1760 530663 : e = bit + a_posteriori_errors(p, R, e);
1761 530664 : if (e < 0) return R;
1762 : }
1763 1044 : if (DEBUGLEVEL)
1764 0 : err_printf("all_roots: restarting, i = %ld, e = %ld\n", i,e);
1765 : }
1766 : }
1767 :
1768 : INLINE int
1769 931280 : isexactscalar(GEN x) { long tx = typ(x); return is_rational_t(tx); }
1770 :
1771 : static int
1772 239634 : isexactpol(GEN p)
1773 : {
1774 239634 : long i,n = degpol(p);
1775 1162320 : for (i=0; i<=n; i++)
1776 931280 : if (!isexactscalar(gel(p,i+2))) return 0;
1777 231040 : return 1;
1778 : }
1779 :
1780 : /* p(0) != 0 [for efficiency] */
1781 : static GEN
1782 231040 : solve_exact_pol(GEN p, long bit)
1783 : {
1784 231040 : long i, j, k, m, n = degpol(p), iroots = 0;
1785 231040 : GEN ex, factors, v = zerovec(n);
1786 :
1787 231040 : factors = ZX_squff(Q_primpart(p), &ex);
1788 462080 : for (i=1; i<lg(factors); i++)
1789 : {
1790 231040 : GEN roots_fact = all_roots(gel(factors,i), bit);
1791 231040 : n = degpol(gel(factors,i));
1792 231040 : m = ex[i];
1793 922042 : for (j=1; j<=n; j++)
1794 1382004 : for (k=1; k<=m; k++) v[++iroots] = roots_fact[j];
1795 : }
1796 231040 : return v;
1797 : }
1798 :
1799 : /* return the roots of p with absolute error bit */
1800 : static GEN
1801 239634 : roots_com(GEN q, long bit)
1802 : {
1803 : GEN L, p;
1804 239634 : long v = RgX_valrem_inexact(q, &p);
1805 239634 : int ex = isexactpol(p);
1806 239634 : if (!ex) p = RgX_normalize1(p);
1807 239634 : if (lg(p) == 3)
1808 0 : L = cgetg(1,t_VEC); /* constant polynomial */
1809 : else
1810 239634 : L = ex? solve_exact_pol(p,bit): all_roots(p,bit);
1811 239634 : if (v)
1812 : {
1813 3935 : GEN M, z, t = gel(q,2);
1814 : long i, x, y, l, n;
1815 :
1816 3935 : if (isrationalzero(t)) x = -bit;
1817 : else
1818 : {
1819 7 : n = gexpo(t);
1820 7 : x = n / v; l = degpol(q);
1821 35 : for (i = v; i <= l; i++)
1822 : {
1823 28 : t = gel(q,i+2);
1824 28 : if (isrationalzero(t)) continue;
1825 28 : y = (n - gexpo(t)) / i;
1826 28 : if (y < x) x = y;
1827 : }
1828 : }
1829 3935 : z = real_0_bit(x); l = v + lg(L);
1830 3935 : M = cgetg(l, t_VEC); L -= v;
1831 7933 : for (i = 1; i <= v; i++) gel(M,i) = z;
1832 11826 : for ( ; i < l; i++) gel(M,i) = gel(L,i);
1833 3935 : L = M;
1834 : }
1835 239634 : return L;
1836 : }
1837 :
1838 : static GEN
1839 1201037 : tocomplex(GEN x, long l, long bit)
1840 : {
1841 : GEN y;
1842 1201037 : if (typ(x) == t_COMPLEX)
1843 : {
1844 1181630 : if (signe(gel(x,1))) return mygprecrc(x, l, -bit);
1845 137453 : x = gel(x,2);
1846 137453 : y = cgetg(3,t_COMPLEX);
1847 137456 : gel(y,1) = real_0_bit(-bit);
1848 137455 : gel(y,2) = mygprecrc(x, l, -bit);
1849 : }
1850 : else
1851 : {
1852 19407 : y = cgetg(3,t_COMPLEX);
1853 19407 : gel(y,1) = mygprecrc(x, l, -bit);
1854 19407 : gel(y,2) = real_0_bit(-bit);
1855 : }
1856 156859 : return y;
1857 : }
1858 :
1859 : /* x,y are t_COMPLEX of t_REALs or t_REAL, compare wrt |Im x| - |Im y|,
1860 : * then Re x - Re y, up to 2^-e absolute error */
1861 : static int
1862 2231705 : cmp_complex_appr(void *E, GEN x, GEN y)
1863 : {
1864 2231705 : long e = (long)E;
1865 : GEN z, xi, yi, xr, yr;
1866 : long sz, sxi, syi;
1867 2231705 : if (typ(x) == t_COMPLEX) { xr = gel(x,1); xi = gel(x,2); sxi = signe(xi); }
1868 837608 : else { xr = x; xi = NULL; sxi = 0; }
1869 2231705 : if (typ(y) == t_COMPLEX) { yr = gel(y,1); yi = gel(y,2); syi = signe(yi); }
1870 559416 : else { yr = y; yi = NULL; syi = 0; }
1871 : /* Compare absolute values of imaginary parts */
1872 2231705 : if (!sxi)
1873 : {
1874 856987 : if (syi && expo(yi) >= e) return -1;
1875 : /* |Im x| ~ |Im y| ~ 0 */
1876 : }
1877 1374718 : else if (!syi)
1878 : {
1879 50169 : if (sxi && expo(xi) >= e) return 1;
1880 : /* |Im x| ~ |Im y| ~ 0 */
1881 : }
1882 : else
1883 : {
1884 1324549 : z = addrr_sign(xi, 1, yi, -1); sz = signe(z);
1885 1324493 : if (sz && expo(z) >= e) return (int)sz;
1886 : }
1887 : /* |Im x| ~ |Im y|, sort according to real parts */
1888 1331940 : z = subrr(xr, yr); sz = signe(z);
1889 1331983 : if (sz && expo(z) >= e) return (int)sz;
1890 : /* Re x ~ Re y. Place negative imaginary part before positive */
1891 585769 : return (int) (sxi - syi);
1892 : }
1893 :
1894 : static GEN
1895 529662 : clean_roots(GEN L, long l, long bit, long clean)
1896 : {
1897 529662 : long i, n = lg(L), ex = 5 - bit;
1898 529662 : GEN res = cgetg(n,t_COL);
1899 2428859 : for (i=1; i<n; i++)
1900 : {
1901 1899204 : GEN c = gel(L,i);
1902 1899204 : if (clean && isrealappr(c,ex))
1903 : {
1904 698172 : if (typ(c) == t_COMPLEX) c = gel(c,1);
1905 698172 : c = mygprecrc(c, l, -bit);
1906 : }
1907 : else
1908 1201031 : c = tocomplex(c, l, bit);
1909 1899198 : gel(res,i) = c;
1910 : }
1911 529655 : gen_sort_inplace(res, (void*)ex, &cmp_complex_appr, NULL);
1912 529657 : return res;
1913 : }
1914 :
1915 : /* the vector of roots of p, with absolute error 2^(- prec2nbits(l)) */
1916 : static GEN
1917 239662 : roots_aux(GEN p, long l, long clean)
1918 : {
1919 239662 : pari_sp av = avma;
1920 : long bit;
1921 : GEN L;
1922 :
1923 239662 : if (typ(p) != t_POL)
1924 : {
1925 21 : if (gequal0(p)) pari_err_ROOTS0("roots");
1926 14 : if (!isvalidcoeff(p)) pari_err_TYPE("roots",p);
1927 7 : return cgetg(1,t_COL); /* constant polynomial */
1928 : }
1929 239641 : if (!signe(p)) pari_err_ROOTS0("roots");
1930 239641 : checkvalidpol(p,"roots");
1931 239634 : if (lg(p) == 3) return cgetg(1,t_COL); /* constant polynomial */
1932 239634 : if (l < LOWDEFAULTPREC) l = LOWDEFAULTPREC;
1933 239634 : bit = prec2nbits(l);
1934 239634 : L = roots_com(p, bit);
1935 239634 : return gc_GEN(av, clean_roots(L, l, bit, clean));
1936 : }
1937 : GEN
1938 8018 : roots(GEN p, long l) { return roots_aux(p,l, 0); }
1939 : /* clean up roots. If root is real replace it by its real part */
1940 : GEN
1941 231644 : cleanroots(GEN p, long l) { return roots_aux(p,l, 1); }
1942 :
1943 : /* private variant of conjvec. Allow non rational coefficients, shallow
1944 : * function. */
1945 : GEN
1946 84 : polmod_to_embed(GEN x, long prec)
1947 : {
1948 84 : GEN v, T = gel(x,1), A = gel(x,2);
1949 : long i, l;
1950 84 : if (typ(A) != t_POL || varn(A) != varn(T))
1951 : {
1952 7 : checkvalidpol(T,"polmod_to_embed");
1953 7 : return const_col(degpol(T), A);
1954 : }
1955 77 : v = cleanroots(T,prec); l = lg(v);
1956 231 : for (i=1; i<l; i++) gel(v,i) = poleval(A,gel(v,i));
1957 77 : return v;
1958 : }
1959 :
1960 : GEN
1961 290025 : QX_complex_roots(GEN p, long l)
1962 : {
1963 290025 : pari_sp av = avma;
1964 : long bit, v;
1965 : GEN L;
1966 :
1967 290025 : if (!signe(p)) pari_err_ROOTS0("QX_complex_roots");
1968 290025 : if (lg(p) == 3) return cgetg(1,t_COL); /* constant polynomial */
1969 290025 : if (l < LOWDEFAULTPREC) l = LOWDEFAULTPREC;
1970 290025 : bit = prec2nbits(l);
1971 290025 : v = RgX_valrem(p, &p);
1972 290024 : L = lg(p) > 3? all_roots(Q_primpart(p), bit): cgetg(1,t_COL);
1973 290028 : if (v) L = shallowconcat(const_vec(v, real_0_bit(-bit)), L);
1974 290028 : return gc_GEN(av, clean_roots(L, l, bit, 1));
1975 : }
1976 :
1977 : /********************************************************************/
1978 : /** **/
1979 : /** REAL ROOTS OF INTEGER POLYNOMIAL **/
1980 : /** **/
1981 : /********************************************************************/
1982 :
1983 : /* Count sign changes in the coefficients of (x+1)^deg(P)*P(1/(x+1)), P
1984 : * has no rational root. The inversion is implicit (we take coefficients
1985 : * backwards). */
1986 : static long
1987 5990427 : X2XP1(GEN P, GEN *Premapped)
1988 : {
1989 5990427 : const pari_sp av = avma;
1990 5990427 : GEN v = shallowcopy(P);
1991 5990508 : long i, j, nb, s, dP = degpol(P), vlim = dP+2;
1992 :
1993 34757227 : for (j = 2; j < vlim; j++) gel(v, j+1) = addii(gel(v, j), gel(v, j+1));
1994 5990120 : s = -signe(gel(v, vlim));
1995 5990120 : vlim--; nb = 0;
1996 16667763 : for (i = 1; i < dP; i++)
1997 : {
1998 14144740 : long s2 = -signe(gel(v, 2));
1999 14144740 : int flag = (s2 == s);
2000 90211964 : for (j = 2; j < vlim; j++)
2001 : {
2002 76067104 : gel(v, j+1) = addii(gel(v, j), gel(v, j+1));
2003 76067224 : if (flag) flag = (s2 != signe(gel(v, j+1)));
2004 : }
2005 14144860 : if (s == signe(gel(v, vlim)))
2006 : {
2007 5027001 : if (++nb >= 2) return gc_long(av,2);
2008 3767833 : s = -s;
2009 : }
2010 : /* if flag is set there will be no further sign changes */
2011 12885692 : if (flag && (!Premapped || !nb)) return gc_long(av, nb);
2012 10677274 : vlim--;
2013 10677274 : if (gc_needed(av, 3))
2014 : {
2015 0 : if (DEBUGMEM>1) pari_warn(warnmem, "X2XP1, i = %ld/%ld", i, dP-1);
2016 0 : if (!Premapped) setlg(v, vlim + 2);
2017 0 : v = gc_GEN(av, v);
2018 : }
2019 : }
2020 2523023 : if (vlim >= 2 && s == signe(gel(v, vlim))) nb++;
2021 2523023 : if (Premapped && nb == 1) *Premapped = v; else set_avma(av);
2022 2522732 : return nb;
2023 : }
2024 :
2025 : static long
2026 0 : _intervalcmp(GEN x, GEN y)
2027 : {
2028 0 : if (typ(x) == t_VEC) x = gel(x, 1);
2029 0 : if (typ(y) == t_VEC) y = gel(y, 1);
2030 0 : return gcmp(x, y);
2031 : }
2032 :
2033 : static GEN
2034 11177808 : _gen_nored(void *E, GEN x) { (void)E; return x; }
2035 : static GEN
2036 24650962 : _mp_add(void *E, GEN x, GEN y) { (void)E; return mpadd(x, y); }
2037 : static GEN
2038 0 : _mp_sub(void *E, GEN x, GEN y) { (void)E; return mpsub(x, y); }
2039 : static GEN
2040 4373444 : _mp_mul(void *E, GEN x, GEN y) { (void)E; return mpmul(x, y); }
2041 : static GEN
2042 6291845 : _mp_sqr(void *E, GEN x) { (void)E; return mpsqr(x); }
2043 : static GEN
2044 14442923 : _gen_one(void *E) { (void)E; return gen_1; }
2045 : static GEN
2046 326001 : _gen_zero(void *E) { (void)E; return gen_0; }
2047 :
2048 : static struct bb_algebra mp_algebra = { _gen_nored, _mp_add, _mp_sub,
2049 : _mp_mul, _mp_sqr, _gen_one, _gen_zero };
2050 :
2051 : static GEN
2052 34730624 : _mp_cmul(void *E, GEN P, long a, GEN x) {(void)E; return mpmul(gel(P,a+2), x);}
2053 :
2054 : /* Split the polynom P in two parts, whose coeffs have constant sign:
2055 : * P(X) = X^D*Pp + Pm. Also compute the two parts of the derivative of P,
2056 : * Pprimem = Pm', Pprimep = X*Pp'+ D*Pp => P' = X^(D-1)*Pprimep + Pprimem;
2057 : * Pprimep[i] = (i+D) Pp[i]. Return D */
2058 : static long
2059 166633 : split_pols(GEN P, GEN *pPp, GEN *pPm, GEN *pPprimep, GEN *pPprimem)
2060 : {
2061 166633 : long i, D, dP = degpol(P), s0 = signe(gel(P,2));
2062 : GEN Pp, Pm, Pprimep, Pprimem;
2063 512096 : for(i=1; i <= dP; i++)
2064 512096 : if (signe(gel(P, i+2)) == -s0) break;
2065 166634 : D = i;
2066 166634 : Pm = cgetg(D + 2, t_POL);
2067 166641 : Pprimem = cgetg(D + 1, t_POL);
2068 166636 : Pp = cgetg(dP-D + 3, t_POL);
2069 166636 : Pprimep = cgetg(dP-D + 3, t_POL);
2070 166638 : Pm[1] = Pp[1] = Pprimem[1] = Pprimep[1] = P[1];
2071 678724 : for(i=0; i < D; i++)
2072 : {
2073 512089 : GEN c = gel(P, i+2);
2074 512089 : gel(Pm, i+2) = c;
2075 512089 : if (i) gel(Pprimem, i+1) = mului(i, c);
2076 : }
2077 693309 : for(; i <= dP; i++)
2078 : {
2079 526678 : GEN c = gel(P, i+2);
2080 526678 : gel(Pp, i+2-D) = c;
2081 526678 : gel(Pprimep, i+2-D) = mului(i, c);
2082 : }
2083 166631 : *pPm = normalizepol_lg(Pm, D+2);
2084 166636 : *pPprimem = normalizepol_lg(Pprimem, D+1);
2085 166642 : *pPp = normalizepol_lg(Pp, dP-D+3);
2086 166642 : *pPprimep = normalizepol_lg(Pprimep, dP-D+3);
2087 166642 : return dP - degpol(*pPp);
2088 : }
2089 :
2090 : static GEN
2091 5223636 : bkeval_single_power(long d, GEN V)
2092 : {
2093 5223636 : long mp = lg(V) - 2;
2094 5223636 : if (d > mp) return gmul(gpowgs(gel(V, mp+1), d/mp), gel(V, (d%mp)+1));
2095 5223636 : return gel(V, d+1);
2096 : }
2097 :
2098 : static GEN
2099 5223640 : splitpoleval(GEN Pp, GEN Pm, GEN pows, long D, long bitprec)
2100 : {
2101 5223640 : GEN vp = gen_bkeval_powers(Pp, degpol(Pp), pows, NULL, &mp_algebra, _mp_cmul);
2102 5223199 : GEN vm = gen_bkeval_powers(Pm, degpol(Pm), pows, NULL, &mp_algebra, _mp_cmul);
2103 5223562 : GEN xa = bkeval_single_power(D, pows);
2104 : GEN r;
2105 5223668 : if (!signe(vp)) return vm;
2106 5223668 : vp = gmul(vp, xa);
2107 5222597 : r = gadd(vp, vm);
2108 5219502 : if (gexpo(vp) - (signe(r)? gexpo(r): 0) > prec2nbits(realprec(vp)) - bitprec)
2109 342065 : return NULL;
2110 4878457 : return r;
2111 : }
2112 :
2113 : /* optimized Cauchy bound for P = X^D*Pp + Pm, D > deg(Pm) */
2114 : static GEN
2115 166643 : splitcauchy(GEN Pp, GEN Pm, long prec)
2116 : {
2117 166643 : GEN S = gel(Pp,2), A = gel(Pm,2);
2118 166643 : long i, lPm = lg(Pm), lPp = lg(Pp);
2119 509021 : for (i=3; i < lPm; i++) { GEN c = gel(Pm,i); if (abscmpii(A, c) < 0) A = c; }
2120 526695 : for (i=3; i < lPp; i++) S = addii(S, gel(Pp, i));
2121 166637 : return subsr(1, rdivii(A, S, prec)); /* 1 + |Pm|_oo / |Pp|_1 */
2122 : }
2123 :
2124 : static GEN
2125 15275 : ZX_deg1root(GEN P, long prec)
2126 : {
2127 15275 : GEN a = gel(P,3), b = gel(P,2);
2128 15275 : if (is_pm1(a))
2129 : {
2130 15275 : b = itor(b, prec); if (signe(a) > 0) togglesign(b);
2131 15275 : return b;
2132 : }
2133 0 : return rdivii(negi(b), a, prec);
2134 : }
2135 :
2136 : /* Newton for polynom P, P(0)!=0, with unique sign change => one root in ]0,oo[
2137 : * P' has also at most one zero there */
2138 : static GEN
2139 166629 : polsolve(GEN P, long bitprec)
2140 : {
2141 : pari_sp av;
2142 : GEN Pp, Pm, Pprimep, Pprimem, Pprime, Pprime2, ra, rb, rc, Pc;
2143 166629 : long dP = degpol(P), prec = nbits2prec(bitprec);
2144 : long expoold, iter, D, rt, s0, bitaddprec, cprec, PREC;
2145 :
2146 166630 : if (dP == 1) return ZX_deg1root(P, prec);
2147 166630 : Pprime = ZX_deriv(P);
2148 166634 : Pprime2 = ZX_deriv(Pprime);
2149 166635 : bitaddprec = 1 + 2*expu(dP); PREC = prec + nbits2prec(bitaddprec);
2150 166633 : D = split_pols(P, &Pp, &Pm, &Pprimep, &Pprimem); /* P = X^D*Pp + Pm */
2151 166642 : s0 = signe(gel(P, 2));
2152 166642 : rt = maxss(D, brent_kung_optpow(maxss(degpol(Pp), degpol(Pm)), 2, 1));
2153 166642 : rb = splitcauchy(Pp, Pm, DEFAULTPREC);
2154 166636 : for (cprec = DEFAULTPREC, expoold = LONG_MAX;;)
2155 0 : {
2156 166636 : GEN pows = gen_powers(rb, rt, 1, NULL, _mp_sqr, _mp_mul, _gen_one);
2157 166641 : Pc = splitpoleval(Pp, Pm, pows, D, bitaddprec);
2158 166637 : if (!Pc) { cprec += EXTRAPREC64; rb = rtor(rb, cprec); continue; }
2159 166637 : if (signe(Pc) != s0) break;
2160 0 : shiftr_inplace(rb,1);
2161 : }
2162 166637 : for (iter = 0, ra = NULL;;)
2163 1817237 : {
2164 : GEN wdth;
2165 1983874 : iter++;
2166 1983874 : if (ra)
2167 907184 : rc = shiftr(addrr(ra, rb), -1);
2168 : else
2169 1076690 : rc = shiftr(rb, -1);
2170 : for(;;)
2171 0 : {
2172 1984044 : GEN pows = gen_powers(rc, rt, 1, NULL, _mp_sqr, _mp_mul, _gen_one);
2173 1983845 : Pc = splitpoleval(Pp, Pm, pows, D, bitaddprec+2);
2174 1983645 : if (Pc) break;
2175 0 : cprec += EXTRAPREC64;
2176 0 : rc = rtor(rc, cprec);
2177 : }
2178 1983645 : if (signe(Pc) == s0)
2179 596681 : ra = rc;
2180 : else
2181 1386964 : rb = rc;
2182 1983645 : if (!ra) continue;
2183 1073564 : wdth = subrr(rb, ra);
2184 1073703 : if (!(iter % 8))
2185 : {
2186 167813 : GEN m1 = poleval(Pprime, ra), M2;
2187 167813 : if (signe(m1) == s0) continue;
2188 166658 : M2 = poleval(Pprime2, rb);
2189 166657 : if (abscmprr(gmul(M2, wdth), shiftr(m1, 1)) > 0) continue;
2190 163500 : break;
2191 : }
2192 905890 : else if (gexpo(wdth) <= -bitprec)
2193 3168 : break;
2194 : }
2195 166668 : rc = rb; av = avma;
2196 1371337 : for(;; rc = gc_leaf(av, rc))
2197 1371532 : {
2198 : long exponew;
2199 1538200 : GEN Ppc, dist, rcold = rc;
2200 1538200 : GEN pows = gen_powers(rc, rt, 1, NULL, _mp_sqr, _mp_mul, _gen_one);
2201 1537930 : Ppc = splitpoleval(Pprimep, Pprimem, pows, D-1, bitaddprec+4);
2202 1537655 : if (Ppc) Pc = splitpoleval(Pp, Pm, pows, D, bitaddprec+4);
2203 1537831 : if (!Ppc || !Pc)
2204 : {
2205 342069 : if (cprec >= PREC)
2206 44286 : cprec += EXTRAPREC64;
2207 : else
2208 297783 : cprec = minss(2*cprec, PREC);
2209 342073 : rc = rtor(rc, cprec); continue; /* backtrack one step */
2210 : }
2211 1195762 : dist = typ(Ppc) == t_REAL? divrr(Pc, Ppc): divri(Pc, Ppc);
2212 1195984 : rc = subrr(rc, dist);
2213 1195481 : if (cmprr(ra, rc) > 0 || cmprr(rb, rc) < 0)
2214 : {
2215 0 : if (cprec >= PREC) break;
2216 0 : cprec = minss(2*cprec, PREC);
2217 0 : rc = rtor(rcold, cprec); continue; /* backtrack one step */
2218 : }
2219 1195894 : if (expoold == LONG_MAX) { expoold = expo(dist); continue; }
2220 975450 : exponew = expo(dist);
2221 975450 : if (exponew < -bitprec - 1)
2222 : {
2223 232397 : if (cprec >= PREC) break;
2224 65759 : cprec = minss(2*cprec, PREC);
2225 65761 : rc = rtor(rc, cprec); continue;
2226 : }
2227 743053 : if (exponew > expoold - 2)
2228 : {
2229 53817 : if (cprec >= PREC) break;
2230 53817 : expoold = LONG_MAX;
2231 53817 : cprec = minss(2*cprec, PREC);
2232 53817 : rc = rtor(rc, cprec); continue;
2233 : }
2234 689236 : expoold = exponew;
2235 : }
2236 166638 : return rtor(rc, prec);
2237 : }
2238 :
2239 : /* Return primpart(P(x / 2)) */
2240 : static GEN
2241 2233796 : ZX_rescale2prim(GEN P)
2242 : {
2243 2233796 : long i, l = lg(P), v, n;
2244 : GEN Q;
2245 2233796 : if (l==2) return pol_0(varn(P));
2246 2233796 : Q = cgetg(l,t_POL); v = vali(gel(P,l-1));
2247 10777316 : for (i = l-2, n = 1; v > n && i >= 2; i--, n++)
2248 8543463 : v = minss(v, vali(gel(P,i)) + n);
2249 2233853 : gel(Q,l-1) = v? shifti(gel(P,l-1), -v): gel(P,l-1);
2250 12721012 : for (i = l-2, n = 1-v; i >= 2; i--, n++)
2251 10487245 : gel(Q,i) = shifti(gel(P,i), n);
2252 2233767 : Q[1] = P[1]; return Q;
2253 : }
2254 :
2255 : /* assume Q0 has no rational root */
2256 : static GEN
2257 1126032 : usp(GEN Q0, long flag, long bitprec)
2258 : {
2259 1126032 : const pari_sp av = avma;
2260 : GEN Qremapped, Q, c, Lc, Lk, sol;
2261 1126032 : GEN *pQremapped = flag == 1? &Qremapped: NULL;
2262 1126032 : const long prec = nbits2prec(bitprec), deg = degpol(Q0);
2263 1126026 : long listsize = 64, nbr = 0, nb_todo, ind, indf, i, k, nb;
2264 :
2265 1126026 : sol = zerocol(deg);
2266 1126049 : Lc = zerovec(listsize);
2267 1126074 : Lk = cgetg(listsize+1, t_VECSMALL);
2268 1126073 : k = Lk[1] = 0;
2269 1126073 : ind = 1; indf = 2;
2270 1126073 : Q = Q0;
2271 1126073 : c = gen_0;
2272 1126073 : nb_todo = 1;
2273 7116294 : while (nb_todo)
2274 : {
2275 5990252 : GEN nc = gel(Lc, ind);
2276 : pari_sp av2;
2277 5990252 : if (Lk[ind] == k + 1)
2278 : {
2279 2233795 : Q = Q0 = ZX_rescale2prim(Q0);
2280 2233802 : c = gen_0;
2281 : }
2282 5990259 : if (!equalii(nc, c)) Q = ZX_Z_translate(Q, subii(nc, c));
2283 5990291 : av2 = avma;
2284 5990291 : k = Lk[ind];
2285 5990291 : ind++;
2286 5990291 : c = nc;
2287 5990291 : nb_todo--;
2288 5990291 : nb = X2XP1(Q, pQremapped);
2289 :
2290 5990050 : if (nb == 1)
2291 : { /* exactly one root */
2292 1911213 : GEN s = gen_0;
2293 1911213 : if (flag == 0)
2294 : {
2295 0 : s = mkvec2(gmul2n(c,-k), gmul2n(addiu(c,1),-k));
2296 0 : s = gc_GEN(av2, s);
2297 : }
2298 1911213 : else if (flag == 1) /* Caveat: Qremapped is the reciprocal polynomial */
2299 : {
2300 166630 : s = polsolve(*pQremapped, bitprec+1);
2301 166642 : s = addir(c, divrr(s, addsr(1, s)));
2302 166632 : shiftr_inplace(s, -k);
2303 166631 : if (realprec(s) != prec) s = rtor(s, prec);
2304 166639 : s = gc_upto(av2, s);
2305 : }
2306 1744583 : else set_avma(av2);
2307 1911240 : gel(sol, ++nbr) = s;
2308 : }
2309 4078837 : else if (nb)
2310 : { /* unknown, add two nodes to refine */
2311 2432191 : if (indf + 2 > listsize)
2312 : {
2313 1788 : if (ind>1)
2314 : {
2315 5297 : for (i = ind; i < indf; i++)
2316 : {
2317 3509 : gel(Lc, i-ind+1) = gel(Lc, i);
2318 3509 : Lk[i-ind+1] = Lk[i];
2319 : }
2320 1788 : indf -= ind-1;
2321 1788 : ind = 1;
2322 : }
2323 1788 : if (indf + 2 > listsize)
2324 : {
2325 0 : listsize *= 2;
2326 0 : Lc = vec_lengthen(Lc, listsize);
2327 0 : Lk = vecsmall_lengthen(Lk, listsize);
2328 : }
2329 112711 : for (i = indf; i <= listsize; i++) gel(Lc, i) = gen_0;
2330 : }
2331 2432191 : gel(Lc, indf) = nc = shifti(c, 1);
2332 2432193 : gel(Lc, indf + 1) = addiu(nc, 1);
2333 2432198 : Lk[indf] = Lk[indf + 1] = k + 1;
2334 2432198 : indf += 2;
2335 2432198 : nb_todo += 2;
2336 : }
2337 5990084 : if (gc_needed(av, 2))
2338 : {
2339 0 : (void)gc_all(av, 6, &Q0, &Q, &c, &Lc, &Lk, &sol);
2340 0 : if (DEBUGMEM > 1) pari_warn(warnmem, "ZX_Uspensky", avma);
2341 : }
2342 : }
2343 1126042 : setlg(sol, nbr+1);
2344 1126043 : return gc_GEN(av, sol);
2345 : }
2346 :
2347 : static GEN
2348 14 : ZX_Uspensky_equal_yes(GEN a, long flag, long bit)
2349 : {
2350 14 : if (flag == 2) return gen_1;
2351 7 : if (flag == 1 && typ(a) != t_REAL)
2352 : {
2353 7 : if (typ(a) == t_INT && !signe(a))
2354 0 : a = real_0_bit(bit);
2355 : else
2356 7 : a = gtofp(a, nbits2prec(bit));
2357 : }
2358 7 : return mkcol(a);
2359 : }
2360 : static GEN
2361 21 : ZX_Uspensky_no(long flag)
2362 21 : { return flag <= 1 ? cgetg(1, t_COL) : gen_0; }
2363 : /* ZX_Uspensky(P, [a,a], flag) */
2364 : static GEN
2365 28 : ZX_Uspensky_equal(GEN P, GEN a, long flag, long bit)
2366 : {
2367 28 : if (typ(a) != t_INFINITY && gequal0(poleval(P, a)))
2368 14 : return ZX_Uspensky_equal_yes(a, flag, bit);
2369 : else
2370 14 : return ZX_Uspensky_no(flag);
2371 : }
2372 : static int
2373 3378 : sol_ok(GEN r, GEN a, GEN b) { return gcmp(a, r) <= 0 && gcmp(r, b) <= 0; }
2374 :
2375 : /* P a ZX without real double roots; better if primitive and squarefree but
2376 : * caller should ensure that. If flag & 4 assume that P has no rational root
2377 : * (modest speedup) */
2378 : GEN
2379 1313886 : ZX_Uspensky(GEN P, GEN ab, long flag, long bitprec)
2380 : {
2381 1313886 : pari_sp av = avma;
2382 : GEN a, b, res, sol;
2383 : double fb;
2384 : long l, nbz, deg;
2385 :
2386 1313886 : if (ab)
2387 : {
2388 1206305 : if (typ(ab) == t_VEC)
2389 : {
2390 1178613 : if (lg(ab) != 3) pari_err_DIM("ZX_Uspensky");
2391 1178612 : a = gel(ab, 1);
2392 1178612 : b = gel(ab, 2);
2393 : }
2394 : else
2395 : {
2396 27692 : a = ab;
2397 27692 : b = mkoo();
2398 : }
2399 : }
2400 : else
2401 : {
2402 107581 : a = mkmoo();
2403 107582 : b = mkoo();
2404 : }
2405 1313886 : if (flag & 4)
2406 : {
2407 129320 : if (gcmp(a, b) >= 0) { set_avma(av); return ZX_Uspensky_no(flag); }
2408 129320 : flag &= ~4;
2409 129320 : sol = cgetg(1, t_COL);
2410 : }
2411 : else
2412 : {
2413 1184566 : switch (gcmp(a, b))
2414 : {
2415 7 : case 1: set_avma(av); return ZX_Uspensky_no(flag);
2416 28 : case 0: return gc_GEN(av, ZX_Uspensky_equal(P, a, flag, bitprec));
2417 : }
2418 1184531 : sol = nfrootsQ(P);
2419 : }
2420 1313855 : nbz = 0; l = lg(sol);
2421 1313855 : if (l > 1)
2422 : {
2423 : long i, j;
2424 2706 : P = RgX_div(P, roots_to_pol(sol, varn(P)));
2425 2706 : if (!RgV_is_ZV(sol)) P = Q_primpart(P);
2426 6084 : for (i = j = 1; i < l; i++)
2427 3378 : if (sol_ok(gel(sol,i), a, b)) gel(sol,j++) = gel(sol,i);
2428 2706 : setlg(sol, j);
2429 2706 : if (flag == 2) { nbz = j-1; sol = utoi(nbz); }
2430 2559 : else if (flag == 1) sol = RgC_gtofp(sol, nbits2prec(bitprec));
2431 : }
2432 1311149 : else if (flag == 2) sol = gen_0;
2433 1313855 : deg = degpol(P);
2434 1313854 : if (deg == 0) return gc_GEN(av, sol);
2435 1311907 : if (typ(a) == t_INFINITY && typ(b) != t_INFINITY && gsigne(b))
2436 : {
2437 28 : fb = fujiwara_bound_real(P, -1);
2438 28 : if (fb <= -pariINFINITY) a = gen_0;
2439 21 : else if (fb < 0) a = gen_m1;
2440 21 : else a = negi(int2n((long)ceil(fb)));
2441 : }
2442 1311908 : if (typ(b) == t_INFINITY && typ(a) != t_INFINITY && gsigne(a))
2443 : {
2444 21 : fb = fujiwara_bound_real(P, 1);
2445 21 : if (fb <= -pariINFINITY) b = gen_0;
2446 21 : else if (fb < 0) b = gen_1;
2447 7 : else b = int2n((long)ceil(fb));
2448 : }
2449 1311907 : if (typ(a) != t_INFINITY && typ(b) != t_INFINITY)
2450 : {
2451 : GEN d, ad, bd, diff;
2452 : long i;
2453 : /* can occur if one of a,b was initially a t_INFINITY */
2454 12582 : if (gequal(a,b)) return gc_GEN(av, sol);
2455 12575 : d = lcmii(Q_denom(a), Q_denom(b));
2456 12575 : if (is_pm1(d)) { d = NULL; ad = a; bd = b; }
2457 : else
2458 14 : { P = ZX_rescale(P, d); ad = gmul(a, d); bd = gmul(b, d); }
2459 12575 : diff = subii(bd, ad);
2460 12575 : P = ZX_affine(P, diff, ad);
2461 12575 : res = usp(P, flag, bitprec);
2462 12575 : if (flag <= 1)
2463 : {
2464 34176 : for (i = 1; i < lg(res); i++)
2465 : {
2466 21916 : GEN z = gmul(diff, gel(res, i));
2467 21916 : if (typ(z) == t_VEC)
2468 : {
2469 0 : gel(z, 1) = gadd(ad, gel(z, 1));
2470 0 : gel(z, 2) = gadd(ad, gel(z, 2));
2471 : }
2472 : else
2473 21916 : z = gadd(ad, z);
2474 21916 : if (d) z = gdiv(z, d);
2475 21916 : gel(res, i) = z;
2476 : }
2477 12260 : sol = shallowconcat(sol, res);
2478 : }
2479 : else
2480 315 : nbz += lg(res) - 1;
2481 : }
2482 1311900 : if (typ(b) == t_INFINITY && (fb=fujiwara_bound_real(P, 1)) > -pariINFINITY)
2483 : {
2484 1023116 : long bp = maxss((long)ceil(fb), 0);
2485 1023116 : res = usp(ZX_unscale2n(P, bp), flag, bitprec);
2486 1023126 : if (flag <= 1)
2487 71241 : sol = shallowconcat(sol, gmul2n(res, bp));
2488 : else
2489 951885 : nbz += lg(res)-1;
2490 : }
2491 1311913 : if (typ(a) == t_INFINITY && (fb=fujiwara_bound_real(P,-1)) > -pariINFINITY)
2492 : {
2493 90373 : long i, bm = maxss((long)ceil(fb), 0);
2494 90373 : res = usp(ZX_unscale2n(ZX_z_unscale(P, -1), bm), flag, bitprec);
2495 90372 : if (flag <= 1)
2496 : {
2497 75625 : for (i = 1; i < lg(res); i++)
2498 : {
2499 47405 : GEN z = gneg(gmul2n(gel(res, i), bm));
2500 47405 : if (typ(z) == t_VEC) swap(gel(z, 1), gel(z, 2));
2501 47405 : gel(res, i) = z;
2502 : }
2503 28220 : sol = shallowconcat(res, sol);
2504 : }
2505 : else
2506 62152 : nbz += lg(res)-1;
2507 : }
2508 1311911 : if (flag >= 2) return utoi(nbz);
2509 83584 : if (flag)
2510 83584 : sol = sort(sol);
2511 : else
2512 0 : sol = gen_sort(sol, (void *)_intervalcmp, cmp_nodata);
2513 83584 : return gc_upto(av, sol);
2514 : }
2515 :
2516 : /* x a scalar */
2517 : static GEN
2518 42 : rootsdeg0(GEN x)
2519 : {
2520 42 : if (!is_real_t(typ(x))) pari_err_TYPE("realroots",x);
2521 35 : if (gequal0(x)) pari_err_ROOTS0("realroots");
2522 14 : return cgetg(1,t_COL); /* constant polynomial */
2523 : }
2524 : static void
2525 2354935 : checkbound(GEN a)
2526 : {
2527 2354935 : switch(typ(a))
2528 : {
2529 2354935 : case t_INT: case t_FRAC: case t_INFINITY: break;
2530 0 : default: pari_err_TYPE("polrealroots", a);
2531 : }
2532 2354935 : }
2533 : static GEN
2534 1178903 : check_ab(GEN ab)
2535 : {
2536 : GEN a, b;
2537 1178903 : if (!ab) return NULL;
2538 1177468 : if (typ(ab) != t_VEC || lg(ab) != 3) pari_err_TYPE("polrootsreal",ab);
2539 1177468 : a = gel(ab,1); checkbound(a);
2540 1177467 : b = gel(ab,2); checkbound(b);
2541 1177470 : if (typ(a) == t_INFINITY && inf_get_sign(a) < 0 &&
2542 448 : typ(b) == t_INFINITY && inf_get_sign(b) > 0) ab = NULL;
2543 1177470 : return ab;
2544 : }
2545 : /* e^(1/h) assuming the h-th root is real, beware that sqrtnr assumes e >= 0 */
2546 : static GEN
2547 22710 : _sqrtnr(GEN e, long h)
2548 : {
2549 : long s;
2550 : GEN r;
2551 22710 : if (h == 2) return sqrtr(e);
2552 14 : s = signe(e); setsigne(e, 1); /* e < 0 is possible, implies h is odd */
2553 14 : r = sqrtnr(e, h); if (s < 0) setsigne(r, -1);
2554 14 : return r;
2555 : }
2556 : GEN
2557 50627 : realroots(GEN P, GEN ab, long prec)
2558 : {
2559 50627 : pari_sp av = avma;
2560 50627 : GEN sol = NULL, fa, ex;
2561 : long i, j, v, l;
2562 :
2563 50627 : ab = check_ab(ab);
2564 50627 : if (typ(P) != t_POL) return rootsdeg0(P);
2565 50606 : if (!RgX_is_ZX(P)) P = RgX_rescale_to_int(P);
2566 50606 : switch(degpol(P))
2567 : {
2568 14 : case -1: return rootsdeg0(gen_0);
2569 7 : case 0: return rootsdeg0(gel(P,2));
2570 : }
2571 50585 : v = ZX_valrem(Q_primpart(P), &P);
2572 50585 : fa = ZX_squff(P, &ex); l = lg(fa); sol = cgetg(l + 1, t_VEC);
2573 102711 : for (i = 1; i < l; i++)
2574 : {
2575 52126 : GEN Pi = gel(fa, i), soli, soli2;
2576 : long n, h;
2577 52126 : if (ab) h = 1; else Pi = ZX_deflate_max(Pi, &h);
2578 52126 : soli = ZX_Uspensky(Pi, odd(h)? ab: gen_0, 1, prec2nbits(prec));
2579 52126 : n = lg(soli); soli2 = odd(h)? NULL: cgetg(n, t_COL);
2580 119122 : for (j = 1; j < n; j++)
2581 : {
2582 66996 : GEN r = gel(soli, j); /* != 0 */
2583 66996 : if (typ(r) != t_REAL) gel(soli, j) = r = gtofp(r, prec);
2584 66996 : if (h > 1)
2585 : {
2586 77 : gel(soli, j) = r = _sqrtnr(r, h);
2587 77 : if (soli2) gel(soli2, j) = negr(r);
2588 : }
2589 : }
2590 52126 : if (soli2) soli = shallowconcat(soli, soli2);
2591 52126 : if (ex[i] > 1) soli = shallowconcat1( const_vec(ex[i], soli) );
2592 52126 : gel(sol, i) = soli;
2593 : }
2594 50585 : if (v && (!ab || (gsigne(gel(ab,1)) <= 0 && gsigne(gel(ab,2)) >= 0)))
2595 84 : gel(sol, i++) = const_col(v, real_0(prec));
2596 50585 : setlg(sol, i); if (i == 1) retgc_const(av, cgetg(1, t_COL));
2597 50571 : return gc_upto(av, sort(shallowconcat1(sol)));
2598 : }
2599 : GEN
2600 48625 : ZX_realroots_irred(GEN P, long prec)
2601 : {
2602 48625 : long dP = degpol(P), j, n, h;
2603 : GEN sol, sol2;
2604 : pari_sp av;
2605 48625 : if (dP == 1) retmkvec(ZX_deg1root(P, prec));
2606 45317 : av = avma; P = ZX_deflate_max(P, &h);
2607 45317 : if (h == dP)
2608 : {
2609 11967 : GEN r = _sqrtnr(ZX_deg1root(P, prec), h);
2610 11967 : return gc_GEN(av, odd(h)? mkvec(r): mkvec2(negr(r), r));
2611 : }
2612 33350 : sol = ZX_Uspensky(P, odd(h)? NULL: gen_0, 1 | 4, prec2nbits(prec));
2613 33350 : n = lg(sol); sol2 = odd(h)? NULL: cgetg(n, t_COL);
2614 133755 : for (j = 1; j < n; j++)
2615 : {
2616 100405 : GEN r = gel(sol, j);
2617 100405 : if (typ(r) != t_REAL) gel(sol, j) = r = gtofp(r, prec);
2618 100405 : if (h > 1)
2619 : {
2620 10666 : gel(sol, j) = r = _sqrtnr(r, h);
2621 10666 : if (sol2) gel(sol2, j) = negr(r);
2622 : }
2623 : }
2624 33350 : if (sol2) sol = shallowconcat(sol, sol2);
2625 33350 : return gc_upto(av, sort(sol));
2626 : }
2627 :
2628 : static long
2629 123156 : ZX_sturm_i(GEN P, long flag)
2630 : {
2631 : pari_sp av;
2632 123156 : long h, r, dP = degpol(P);
2633 123156 : if (dP == 1) return 1;
2634 119814 : av = avma; P = ZX_deflate_max(P, &h);
2635 119814 : if (h == dP)
2636 : { /* now deg P = 1 */
2637 18275 : if (odd(h))
2638 665 : r = 1;
2639 : else
2640 17610 : r = (signe(gel(P,2)) != signe(gel(P,3)))? 2: 0;
2641 18275 : return gc_long(av, r);
2642 : }
2643 101539 : if (odd(h))
2644 78767 : r = itou(ZX_Uspensky(P, NULL, flag, 0));
2645 : else
2646 22772 : r = 2*itou(ZX_Uspensky(P, gen_0, flag, 0));
2647 101539 : return gc_long(av,r);
2648 : }
2649 : /* P nonconstant, squarefree ZX */
2650 : long
2651 1128275 : ZX_sturmpart(GEN P, GEN ab)
2652 : {
2653 1128275 : pari_sp av = avma;
2654 1128275 : if (!check_ab(ab)) return ZX_sturm(P);
2655 1126872 : return gc_long(av, itou(ZX_Uspensky(P, ab, 2, 0)));
2656 : }
2657 : /* P nonconstant, squarefree ZX */
2658 : long
2659 6395 : ZX_sturm(GEN P) { return ZX_sturm_i(P, 2); }
2660 : /* P irreducible ZX */
2661 : long
2662 116761 : ZX_sturm_irred(GEN P) { return ZX_sturm_i(P, 2 + 4); }
|