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 288431 : isvalidcoeff(GEN x)
30 : {
31 288431 : switch (typ(x))
32 : {
33 269233 : case t_INT: case t_REAL: case t_FRAC: return 1;
34 19184 : case t_COMPLEX: return isvalidcoeff(gel(x,1)) && isvalidcoeff(gel(x,2));
35 : }
36 14 : return 0;
37 : }
38 :
39 : static void
40 37830 : checkvalidpol(GEN p, const char *f)
41 : {
42 37830 : long i,n = lg(p);
43 287872 : for (i=2; i<n; i++)
44 250049 : if (!isvalidcoeff(gel(p,i))) pari_err_TYPE(f, gel(p,i));
45 37823 : }
46 :
47 : /********************************************************************/
48 : /** **/
49 : /** FAST ARITHMETIC over Z[i] **/
50 : /** **/
51 : /********************************************************************/
52 :
53 : static GEN
54 15424334 : ZX_to_ZiX(GEN Pr, GEN Pi)
55 : {
56 15424334 : long i, lr = lg(Pr), li = lg(Pi), l = maxss(lr, li), m = minss(lr, li);
57 15425056 : GEN P = cgetg(l, t_POL);
58 15428774 : P[1] = Pr[1];
59 63318048 : for(i = 2; i < m; i++)
60 47889147 : gel(P,i) = signe(gel(Pi,i)) ? mkcomplex(gel(Pr,i), gel(Pi,i))
61 47889147 : : gel(Pr,i);
62 20957029 : for( ; i < lr; i++)
63 5528128 : gel(P,i) = gel(Pr, i);
64 15461559 : for( ; i < li; i++)
65 32658 : gel(P,i) = mkcomplex(gen_0, gel(Pi, i));
66 15428901 : return normalizepol_lg(P, l);
67 : }
68 :
69 : static GEN
70 58181109 : ZiX_sqr(GEN P)
71 : {
72 58181109 : pari_sp av = avma;
73 : GEN Pr2, Pi2, Qr, Qi;
74 58181109 : GEN Pr = real_i(P), Pi = imag_i(P);
75 58173921 : if (signe(Pi)==0) return gerepileupto(av, ZX_sqr(Pr));
76 15477847 : if (signe(Pr)==0) return gerepileupto(av, ZX_neg(ZX_sqr(Pi)));
77 15429626 : Pr2 = ZX_sqr(Pr); Pi2 = ZX_sqr(Pi);
78 15419325 : Qr = ZX_sub(Pr2, Pi2);
79 15425590 : if (degpol(Pr)==degpol(Pi))
80 10048642 : Qi = ZX_sub(ZX_sqr(ZX_add(Pr, Pi)), ZX_add(Pr2, Pi2));
81 : else
82 5379254 : Qi = ZX_shifti(ZX_mul(Pr, Pi), 1);
83 15427103 : return gerepilecopy(av, ZX_to_ZiX(Qr, Qi));
84 : }
85 :
86 : static GEN
87 29099128 : graeffe(GEN p)
88 : {
89 29099128 : pari_sp av = avma;
90 : GEN p0, p1, s0, s1;
91 29099128 : long n = degpol(p);
92 :
93 29099040 : if (!n) return RgX_copy(p);
94 29099040 : RgX_even_odd(p, &p0, &p1);
95 : /* p = p0(x^2) + x p1(x^2) */
96 29095882 : s0 = ZiX_sqr(p0);
97 29103428 : s1 = ZiX_sqr(p1);
98 29102411 : return gerepileupto(av, RgX_sub(s0, RgX_shift_shallow(s1,1)));
99 : }
100 :
101 : GEN
102 15225 : ZX_graeffe(GEN p)
103 : {
104 15225 : pari_sp av = avma;
105 : GEN p0, p1, s0, s1;
106 15225 : long n = degpol(p);
107 :
108 15225 : if (!n) return ZX_copy(p);
109 15225 : RgX_even_odd(p, &p0, &p1);
110 : /* p = p0(x^2) + x p1(x^2) */
111 15225 : s0 = ZX_sqr(p0);
112 15224 : s1 = ZX_sqr(p1);
113 15225 : return gerepileupto(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 gerepileupto(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 151992889 : mydbllog2i(GEN x)
143 : {
144 : #ifdef LONG_IS_64BIT
145 131097143 : const double W = 1/(4294967296. * 4294967296.); /* 2^-64 */
146 : #else
147 20895746 : const double W = 1/4294967296.; /*2^-32*/
148 : #endif
149 : GEN m;
150 151992889 : long lx = lgefint(x);
151 : double l;
152 151992889 : if (lx == 2) return -pariINFINITY;
153 151413566 : m = int_MSW(x);
154 151413566 : l = (double)(ulong)*m;
155 151413566 : if (lx == 3) return log2(l);
156 59352264 : 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 59352264 : return log2(l) + (double)(BITS_IN_LONG*(lx-3));
161 : }
162 :
163 : /* return log(|x|) or -pariINFINITY */
164 : static double
165 6964995 : mydbllogr(GEN x) {
166 6964995 : if (!signe(x)) return -pariINFINITY;
167 6964995 : return M_LN2*dbllog2r(x);
168 : }
169 :
170 : /* return log2(|x|) or -pariINFINITY */
171 : static double
172 37545246 : mydbllog2r(GEN x) {
173 37545246 : if (!signe(x)) return -pariINFINITY;
174 37163801 : return dbllog2r(x);
175 : }
176 : double
177 211759416 : dbllog2(GEN z)
178 : {
179 : double x, y;
180 211759416 : switch(typ(z))
181 : {
182 151939193 : case t_INT: return mydbllog2i(z);
183 19544 : case t_FRAC: return mydbllog2i(gel(z,1))-mydbllog2i(gel(z,2));
184 34137342 : case t_REAL: return mydbllog2r(z);
185 25663337 : default: /*t_COMPLEX*/
186 25663337 : x = dbllog2(gel(z,1));
187 25717578 : y = dbllog2(gel(z,2));
188 25717476 : if (x == -pariINFINITY) return y;
189 25494722 : if (y == -pariINFINITY) return x;
190 25292989 : if (fabs(x-y) > 10) return maxdd(x,y);
191 24738432 : return x + 0.5*log2(1 + exp2(2*(y-x)));
192 : }
193 : }
194 : static GEN /* beware overflow */
195 3969769 : 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 23448729 : findpower(GEN p)
201 : {
202 23448729 : double x, L, mins = pariINFINITY;
203 23448729 : long n = degpol(p),i;
204 :
205 23448101 : L = dbllog2(gel(p,n+2)); /* log2(lc * binom(n,i)) */
206 106673430 : for (i=n-1; i>=0; i--)
207 : {
208 83221486 : L += log2((double)(i+1) / (double)(n-i));
209 83221486 : x = dbllog2(gel(p,i+2));
210 83224360 : if (x != -pariINFINITY)
211 : {
212 82704696 : double s = (L - x) / (double)(n-i);
213 82704696 : if (s < mins) mins = s;
214 : }
215 : }
216 23451944 : i = (long)ceil(mins);
217 23451944 : if (i - mins > 1 - 1e-12) i--;
218 23451944 : return i;
219 : }
220 :
221 : /* returns the exponent for logmodulus(), from the Newton diagram */
222 : static long
223 3567513 : newton_polygon(GEN p, long k)
224 : {
225 3567513 : pari_sp av = avma;
226 : double *logcoef, slope;
227 3567513 : long n = degpol(p), i, j, h, l, *vertex;
228 :
229 3567498 : logcoef = (double*)stack_malloc_align((n+1)*sizeof(double), sizeof(double));
230 3567469 : vertex = (long*)new_chunk(n+1);
231 :
232 : /* vertex[i] = 1 if i a vertex of convex hull, 0 otherwise */
233 18274398 : for (i=0; i<=n; i++) { logcoef[i] = dbllog2(gel(p,2+i)); vertex[i] = 0; }
234 3567511 : vertex[0] = 1; /* sentinel */
235 13673326 : for (i=0; i < n; i=h)
236 : {
237 10105815 : slope = logcoef[i+1]-logcoef[i];
238 41071315 : for (j = h = i+1; j<=n; j++)
239 : {
240 30965500 : double pij = (logcoef[j]-logcoef[i])/(double)(j-i);
241 30965500 : if (slope < pij) { slope = pij; h = j; }
242 : }
243 10105815 : vertex[h] = 1;
244 : }
245 3970860 : h = k; while (!vertex[h]) h++;
246 3758927 : l = k-1; while (!vertex[l]) l--;
247 3567511 : set_avma(av);
248 3567556 : return (long)floor((logcoef[h]-logcoef[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 23548917 : myshiftrc(GEN z, long e)
254 : {
255 23548917 : if (typ(z)==t_COMPLEX)
256 : {
257 6040010 : if (signe(gel(z,1))) shiftr_inplace(gel(z,1), e);
258 6040030 : if (signe(gel(z,2))) shiftr_inplace(gel(z,2), e);
259 : }
260 : else
261 17508907 : if (signe(z)) shiftr_inplace(z, e);
262 23549026 : }
263 :
264 : /* return z*2^e, where z is integer or complex of integer (destroy z) */
265 : static GEN
266 89953073 : myshiftic(GEN z, long e)
267 : {
268 89953073 : if (typ(z)==t_COMPLEX)
269 : {
270 17028773 : gel(z,1) = signe(gel(z,1))? mpshift(gel(z,1),e): gen_0;
271 17022652 : gel(z,2) = mpshift(gel(z,2),e);
272 17021163 : return z;
273 : }
274 72924300 : return signe(z)? mpshift(z,e): gen_0;
275 : }
276 :
277 : static GEN
278 4578737 : RgX_gtofp_bit(GEN q, long bit) { return RgX_gtofp(q, nbits2prec(bit)); }
279 :
280 : static GEN
281 159933471 : mygprecrc(GEN x, long prec, long e)
282 : {
283 : GEN y;
284 159933471 : switch(typ(x))
285 : {
286 116235076 : case t_REAL:
287 116235076 : if (!signe(x)) return real_0_bit(e);
288 113507733 : return realprec(x) == prec? x: rtor(x, prec);
289 33545829 : case t_COMPLEX:
290 33545829 : y = cgetg(3,t_COMPLEX);
291 33545329 : gel(y,1) = mygprecrc(gel(x,1),prec,e);
292 33545254 : gel(y,2) = mygprecrc(gel(x,2),prec,e);
293 33545625 : return y;
294 10152566 : 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 38336796 : mygprec(GEN x, long bit)
302 : {
303 : long lx, i, e, prec;
304 : GEN y;
305 :
306 38336796 : if (bit < 0) bit = 0; /* should rarely happen */
307 38336796 : e = gexpo(x) - bit;
308 38343670 : prec = nbits2prec(bit);
309 38346550 : switch(typ(x))
310 : {
311 26335804 : case t_POL:
312 26335804 : y = cgetg_copy(x, &lx); y[1] = x[1];
313 106044771 : for (i=2; i<lx; i++) gel(y,i) = mygprecrc(gel(x,i),prec,e);
314 26337822 : break;
315 :
316 12010746 : default: y = mygprecrc(x,prec,e);
317 : }
318 38345940 : return y;
319 : }
320 :
321 : /* normalize a polynomial p, that is change it with coefficients in Z[i],
322 : after making product by 2^shift */
323 : static GEN
324 10820520 : pol_to_gaussint(GEN p, long shift)
325 : {
326 10820520 : long i, l = lg(p);
327 10820520 : GEN q = cgetg(l, t_POL); q[1] = p[1];
328 68279560 : for (i=2; i<l; i++) gel(q,i) = gtrunc2n(gel(p,i), shift);
329 10798093 : return q;
330 : }
331 :
332 : /* returns a polynomial q in Z[i][x] keeping bit bits of p */
333 : static GEN
334 8288036 : eval_rel_pol(GEN p, long bit)
335 : {
336 : long i;
337 51558121 : for (i = 2; i < lg(p); i++)
338 43269644 : if (gequal0(gel(p,i))) gel(p,i) = gen_0; /* bad behavior of gexpo */
339 8288477 : return pol_to_gaussint(p, bit-gexpo(p)+1);
340 : }
341 :
342 : /* returns p(R*x)/R^n (in R or R[i]), R = exp(lrho), bit bits of precision */
343 : static GEN
344 958631 : homothetie(GEN p, double lrho, long bit)
345 : {
346 : GEN q, r, t, iR;
347 958631 : long n = degpol(p), i;
348 :
349 958630 : iR = mygprec(dblexp(-lrho),bit);
350 958613 : q = mygprec(p, bit);
351 958633 : r = cgetg(n+3,t_POL); r[1] = p[1];
352 958633 : t = iR; r[n+2] = q[n+2];
353 4838591 : for (i=n-1; i>0; i--)
354 : {
355 3880007 : gel(r,i+2) = gmul(t, gel(q,i+2));
356 3879896 : t = mulrr(t, iR);
357 : }
358 958584 : gel(r,2) = gmul(t, gel(q,2)); return r;
359 : }
360 :
361 : /* change q in 2^(n*e) p(x*2^(-e)), n=deg(q) [ ~as above with R = 2^-e ]*/
362 : static void
363 6099491 : homothetie2n(GEN p, long e)
364 : {
365 6099491 : if (e)
366 : {
367 4748895 : long i,n = lg(p)-1;
368 28297774 : for (i=2; i<=n; i++) myshiftrc(gel(p,i), (n-i)*e);
369 : }
370 6099658 : }
371 :
372 : /* return 2^f * 2^(n*e) p(x*2^(-e)), n=deg(q) */
373 : static void
374 20914066 : homothetie_gauss(GEN p, long e, long f)
375 : {
376 20914066 : if (e || f)
377 : {
378 18931501 : long i, n = lg(p)-1;
379 108683290 : for (i=2; i<=n; i++) gel(p,i) = myshiftic(gel(p,i), f+(n-i)*e);
380 : }
381 20794140 : }
382 :
383 : /* Lower bound on the modulus of the largest root z_0
384 : * k is set to an upper bound for #{z roots, |z-z_0| < eps} */
385 : static double
386 23450431 : lower_bound(GEN p, long *k, double eps)
387 : {
388 23450431 : long n = degpol(p), i, j;
389 23450817 : pari_sp ltop = avma;
390 : GEN a, s, S, ilc;
391 : double r, R, rho;
392 :
393 23450817 : if (n < 4) { *k = n; return 0.; }
394 8254692 : S = cgetg(5,t_VEC);
395 8255522 : a = cgetg(5,t_VEC); ilc = gdiv(real_1(DEFAULTPREC), gel(p,n+2));
396 41248202 : for (i=1; i<=4; i++) gel(a,i) = gmul(ilc,gel(p,n+2-i));
397 : /* i = 1 split out from next loop for efficiency and initialization */
398 8253952 : s = gel(a,1);
399 8253952 : gel(S,1) = gneg(s); /* Newton sum S_i */
400 8254923 : rho = r = gtodouble(gabs(s,3));
401 8255386 : R = r / n;
402 33015706 : for (i=2; i<=4; i++)
403 : {
404 24760651 : s = gmulsg(i,gel(a,i));
405 74188699 : for (j=1; j<i; j++) s = gadd(s, gmul(gel(S,j),gel(a,i-j)));
406 24741795 : gel(S,i) = gneg(s); /* Newton sum S_i */
407 24755360 : r = gtodouble(gabs(s,3));
408 24760320 : if (r > 0.)
409 : {
410 24717059 : r = exp(log(r/n) / (double)i);
411 24717059 : if (r > R) R = r;
412 : }
413 : }
414 8255055 : if (R > 0. && eps < 1.2)
415 8251637 : *k = (long)floor((rho/R + n) / (1 + exp(-eps)*cos(eps)));
416 : else
417 3418 : *k = n;
418 8255055 : return gc_double(ltop, R);
419 : }
420 :
421 : /* return R such that exp(R - tau) <= rho_n(P) <= exp(R + tau)
422 : * P(0) != 0 and P non constant */
423 : static double
424 2532505 : logmax_modulus(GEN p, double tau)
425 : {
426 : GEN r, q, aux, gunr;
427 2532505 : pari_sp av, ltop = avma;
428 2532505 : long i,k,n=degpol(p),nn,bit,M,e;
429 2532489 : double rho,eps, tau2 = (tau > 3.0)? 0.5: tau/6.;
430 :
431 2532489 : r = cgeti(BIGDEFAULTPREC);
432 2532448 : av = avma;
433 :
434 2532448 : eps = - 1/log(1.5*tau2); /* > 0 */
435 2532448 : bit = (long) ((double) n*log2(1./tau2)+3*log2((double) n))+1;
436 2532448 : gunr = real_1_bit(bit+2*n);
437 2532421 : aux = gdiv(gunr, gel(p,2+n));
438 2532211 : q = RgX_Rg_mul(p, aux); gel(q,2+n) = gunr;
439 2532302 : e = findpower(q);
440 2532420 : homothetie2n(q,e);
441 2532477 : affsi(e, r);
442 2532453 : q = pol_to_gaussint(q, bit);
443 2532258 : M = (long) (log2( log(4.*n) / (2*tau2) )) + 2;
444 2532258 : nn = n;
445 2532258 : for (i=0,e=0;;)
446 : { /* nn = deg(q) */
447 23452719 : rho = lower_bound(q, &k, eps);
448 23449658 : if (rho > exp2(-(double)e)) e = (long)-floor(log2(rho));
449 23449658 : affii(shifti(addis(r,e), 1), r);
450 23436407 : if (++i == M) break;
451 :
452 62712582 : bit = (long) ((double)k * log2(1./tau2) +
453 41808388 : (double)(nn-k)*log2(1./eps) + 3*log2((double)nn)) + 1;
454 20904194 : homothetie_gauss(q, e, bit-(long)floor(dbllog2(gel(q,2+nn))+0.5));
455 20879532 : nn -= RgX_valrem(q, &q);
456 20911336 : q = gerepileupto(av, graeffe(q));
457 20918653 : tau2 *= 1.5; if (tau2 > 0.9) tau2 = 0.5;
458 20918653 : eps = -1/log(tau2); /* > 0 */
459 20918653 : e = findpower(q);
460 : }
461 2532213 : if (!signe(r)) return gc_double(ltop,0.);
462 2404217 : r = itor(r, DEFAULTPREC); shiftr_inplace(r, -M);
463 2404285 : return gc_double(ltop, -rtodbl(r) * M_LN2); /* -log(2) sum e_i 2^-i */
464 : }
465 :
466 : static GEN
467 26319 : RgX_normalize1(GEN x)
468 : {
469 26319 : long i, n = lg(x)-1;
470 : GEN y;
471 26333 : for (i = n; i > 1; i--)
472 26326 : if (!gequal0( gel(x,i) )) break;
473 26319 : if (i == n) return x;
474 14 : pari_warn(warner,"normalizing a polynomial with 0 leading term");
475 14 : if (i == 1) pari_err_ROOTS0("roots");
476 14 : y = cgetg(i+1, t_POL); y[1] = x[1];
477 42 : for (; i > 1; i--) gel(y,i) = gel(x,i);
478 14 : return y;
479 : }
480 :
481 : static GEN
482 18808 : polrootsbound_i(GEN P, double TAU)
483 : {
484 18808 : pari_sp av = avma;
485 : double d;
486 18808 : (void)RgX_valrem_inexact(P,&P);
487 18808 : P = RgX_normalize1(P);
488 18808 : switch(degpol(P))
489 : {
490 7 : case -1: pari_err_ROOTS0("roots");
491 63 : case 0: set_avma(av); return gen_0;
492 : }
493 18738 : d = logmax_modulus(P, TAU) + TAU;
494 : /* not dblexp: result differs on ARM emulator */
495 18738 : return gerepileuptoleaf(av, mpexp(dbltor(d)));
496 : }
497 : GEN
498 18815 : polrootsbound(GEN P, GEN tau)
499 : {
500 18815 : if (typ(P) != t_POL) pari_err_TYPE("polrootsbound",P);
501 18808 : checkvalidpol(P, "polrootsbound");
502 18808 : return polrootsbound_i(P, tau? gtodouble(tau): 0.01);
503 : }
504 :
505 : /* log of modulus of the smallest root of p, with relative error tau */
506 : static double
507 914003 : logmin_modulus(GEN p, double tau)
508 : {
509 914003 : pari_sp av = avma;
510 914003 : if (gequal0(gel(p,2))) return -pariINFINITY;
511 913997 : return gc_double(av, - logmax_modulus(RgX_recip_i(p),tau));
512 : }
513 :
514 : /* return the log of the k-th modulus (ascending order) of p, rel. error tau*/
515 : static double
516 389885 : logmodulus(GEN p, long k, double tau)
517 : {
518 : GEN q;
519 389885 : long i, kk = k, imax, n = degpol(p), nn, bit, e;
520 389884 : pari_sp av, ltop=avma;
521 389884 : double r, tau2 = tau/6;
522 :
523 389884 : bit = (long)(n * (2. + log2(3.*n/tau2)));
524 389884 : av = avma;
525 389884 : q = gprec_w(p, nbits2prec(bit));
526 389887 : q = RgX_gtofp_bit(q, bit);
527 389888 : e = newton_polygon(q,k);
528 389885 : r = (double)e;
529 389885 : homothetie2n(q,e);
530 389898 : imax = (long)(log2(3./tau) + log2(log(4.*n)))+1;
531 3567599 : for (i=1; i<imax; i++)
532 : {
533 3177712 : q = eval_rel_pol(q,bit);
534 3177420 : kk -= RgX_valrem(q, &q);
535 3177540 : nn = degpol(q);
536 :
537 3177524 : q = gerepileupto(av, graeffe(q));
538 3177675 : e = newton_polygon(q,kk);
539 3177673 : r += e / exp2((double)i);
540 3177673 : q = RgX_gtofp_bit(q, bit);
541 3177451 : homothetie2n(q,e);
542 :
543 3177701 : tau2 *= 1.5; if (tau2 > 1.) tau2 = 1.;
544 3177701 : bit = 1 + (long)(nn*(2. + log2(3.*nn/tau2)));
545 : }
546 389887 : return gc_double(ltop, -r * M_LN2);
547 : }
548 :
549 : /* return the log of the k-th modulus r_k of p, rel. error tau, knowing that
550 : * rmin < r_k < rmax. This information helps because we may reduce precision
551 : * quicker */
552 : static double
553 389881 : logpre_modulus(GEN p, long k, double tau, double lrmin, double lrmax)
554 : {
555 : GEN q;
556 389881 : long n = degpol(p), i, imax, imax2, bit;
557 389881 : pari_sp ltop = avma, av;
558 389881 : double lrho, aux, tau2 = tau/6.;
559 :
560 389881 : aux = (lrmax - lrmin) / 2. + 4*tau2;
561 389881 : imax = (long) log2(log((double)n)/ aux);
562 389881 : if (imax <= 0) return logmodulus(p,k,tau);
563 :
564 382040 : lrho = (lrmin + lrmax) / 2;
565 382040 : av = avma;
566 382040 : bit = (long)(n*(2. + aux / M_LN2 - log2(tau2)));
567 382040 : q = homothetie(p, lrho, bit);
568 382035 : imax2 = (long)(log2(3./tau * log(4.*n))) + 1;
569 382035 : if (imax > imax2) imax = imax2;
570 :
571 1109992 : for (i=0; i<imax; i++)
572 : {
573 727951 : q = eval_rel_pol(q,bit);
574 727947 : q = gerepileupto(av, graeffe(q));
575 727958 : aux = 2*aux + 2*tau2;
576 727958 : tau2 *= 1.5;
577 727958 : bit = (long)(n*(2. + aux / M_LN2 - log2(1-exp(-tau2))));
578 727958 : q = RgX_gtofp_bit(q, bit);
579 : }
580 382041 : aux = exp2((double)imax);
581 382041 : return gc_double(ltop, lrho + logmodulus(q,k, aux*tau/3.) / aux);
582 : }
583 :
584 : static double
585 483227 : ind_maxlog2(GEN q)
586 : {
587 483227 : long i, k = -1;
588 483227 : double L = - pariINFINITY;
589 1263853 : for (i=0; i<=degpol(q); i++)
590 : {
591 780621 : double d = dbllog2(gel(q,2+i));
592 780626 : if (d > L) { L = d; k = i; }
593 : }
594 483228 : return k;
595 : }
596 :
597 : /* Returns k such that r_k e^(-tau) < R < r_{k+1} e^tau.
598 : * Assume that l <= k <= n-l */
599 : static long
600 576589 : dual_modulus(GEN p, double lrho, double tau, long l)
601 : {
602 576589 : long i, imax, delta_k = 0, n = degpol(p), nn, v2, v, bit, ll = l;
603 576590 : double tau2 = tau * 7./8.;
604 576590 : pari_sp av = avma;
605 : GEN q;
606 :
607 576590 : bit = 6*n - 5*l + (long)(n*(-log2(tau2) + tau2 * 8./7.));
608 576590 : q = homothetie(p, lrho, bit);
609 576574 : imax = (long)(log(log(2.*n)/tau2)/log(7./4.)+1);
610 :
611 4866042 : for (i=0; i<imax; i++)
612 : {
613 4382814 : q = eval_rel_pol(q,bit); v2 = n - degpol(q);
614 4381727 : v = RgX_valrem(q, &q);
615 4382561 : ll -= maxss(v, v2); if (ll < 0) ll = 0;
616 :
617 4382567 : nn = degpol(q); delta_k += v;
618 4382563 : if (!nn) return delta_k;
619 :
620 4289206 : q = gerepileupto(av, graeffe(q));
621 4289468 : tau2 *= 7./4.;
622 4289468 : bit = 6*nn - 5*ll + (long)(nn*(-log2(tau2) + tau2 * 8./7.));
623 : }
624 483228 : return gc_long(av, delta_k + (long)ind_maxlog2(q));
625 : }
626 :
627 : /********************************************************************/
628 : /** **/
629 : /** FACTORS THROUGH CIRCLE INTEGRATION **/
630 : /** **/
631 : /********************************************************************/
632 : /* l power of 2, W[step*j] = w_j; set f[j] = p(w_j)
633 : * if inv, w_j = exp(2IPi*j/l), else exp(-2IPi*j/l) */
634 :
635 : static void
636 7462 : fft2(GEN W, GEN p, GEN f, long step, long l)
637 : {
638 : pari_sp av;
639 : long i, s1, l1, step2;
640 :
641 7462 : if (l == 2)
642 : {
643 3766 : gel(f,0) = gadd(gel(p,0), gel(p,step));
644 3766 : gel(f,1) = gsub(gel(p,0), gel(p,step)); return;
645 : }
646 3696 : av = avma;
647 3696 : l1 = l>>1; step2 = step<<1;
648 3696 : fft2(W,p, f, step2,l1);
649 3696 : fft2(W,p+step, f+l1,step2,l1);
650 32760 : for (i = s1 = 0; i < l1; i++, s1 += step)
651 : {
652 29064 : GEN f0 = gel(f,i);
653 29064 : GEN f1 = gmul(gel(W,s1), gel(f,i+l1));
654 29064 : gel(f,i) = gadd(f0, f1);
655 29064 : gel(f,i+l1) = gsub(f0, f1);
656 : }
657 3696 : gerepilecoeffs(av, f, l);
658 : }
659 :
660 : static void
661 11885497 : fft(GEN W, GEN p, GEN f, long step, long l, long inv)
662 : {
663 : pari_sp av;
664 : long i, s1, l1, l2, l3, step4;
665 : GEN f1, f2, f3, f02;
666 :
667 11885497 : if (l == 2)
668 : {
669 6612151 : gel(f,0) = gadd(gel(p,0), gel(p,step));
670 6611813 : gel(f,1) = gsub(gel(p,0), gel(p,step)); return;
671 : }
672 5273346 : av = avma;
673 5273346 : if (l == 4)
674 : {
675 : pari_sp av2;
676 3081802 : f1 = gadd(gel(p,0), gel(p,step<<1));
677 3081239 : f2 = gsub(gel(p,0), gel(p,step<<1));
678 3081346 : f3 = gadd(gel(p,step), gel(p,3*step));
679 3081204 : f02 = gsub(gel(p,step), gel(p,3*step));
680 3081222 : f02 = inv? mulcxI(f02): mulcxmI(f02);
681 3081772 : av2 = avma;
682 3081772 : gel(f,0) = gadd(f1, f3);
683 3081104 : gel(f,1) = gadd(f2, f02);
684 3081336 : gel(f,2) = gsub(f1, f3);
685 3081096 : gel(f,3) = gsub(f2, f02);
686 3081355 : gerepileallsp(av,av2,4,&gel(f,0),&gel(f,1),&gel(f,2),&gel(f,3));
687 3081927 : return;
688 : }
689 2191544 : l1 = l>>2; l2 = 2*l1; l3 = l1+l2; step4 = step<<2;
690 2191544 : fft(W,p, f, step4,l1,inv);
691 2192042 : fft(W,p+step, f+l1,step4,l1,inv);
692 2192024 : fft(W,p+(step<<1),f+l2,step4,l1,inv);
693 2192035 : fft(W,p+3*step, f+l3,step4,l1,inv);
694 8161144 : for (i = s1 = 0; i < l1; i++, s1 += step)
695 : {
696 5969186 : long s2 = s1 << 1, s3 = s1 + s2;
697 : GEN g02, g13, f13;
698 5969186 : f1 = gmul(gel(W,s1), gel(f,i+l1));
699 5969423 : f2 = gmul(gel(W,s2), gel(f,i+l2));
700 5969291 : f3 = gmul(gel(W,s3), gel(f,i+l3));
701 :
702 5969400 : f02 = gadd(gel(f,i),f2);
703 5968824 : g02 = gsub(gel(f,i),f2);
704 5968784 : f13 = gadd(f1,f3);
705 5968776 : g13 = gsub(f1,f3); g13 = inv? mulcxI(g13): mulcxmI(g13);
706 :
707 5969346 : gel(f,i) = gadd(f02, f13);
708 5968847 : gel(f,i+l1) = gadd(g02, g13);
709 5968865 : gel(f,i+l2) = gsub(f02, f13);
710 5968880 : gel(f,i+l3) = gsub(g02, g13);
711 : }
712 2191958 : gerepilecoeffs(av, f, l);
713 : }
714 :
715 : #define code(t1,t2) ((t1 << 6) | t2)
716 :
717 : static GEN
718 84 : FFT_i(GEN W, GEN x)
719 : {
720 84 : long i, l = lg(W), n = lg(x), tx = typ(x), tw, pa;
721 : GEN y, z, p, pol;
722 84 : if ((l-1) & (l-2)) pari_err_DIM("fft");
723 70 : tw = RgV_type(W, &p, &pol, &pa);
724 70 : if (tx == t_POL) { x++; n--; }
725 35 : else if (!is_vec_t(tx)) pari_err_TYPE("fft",x);
726 70 : if (n > l) pari_err_DIM("fft");
727 :
728 70 : if (n < l) {
729 0 : z = cgetg(l, t_VECSMALL); /* cf stackdummy */
730 0 : for (i = 1; i < n; i++) gel(z,i) = gel(x,i);
731 0 : for ( ; i < l; i++) gel(z,i) = gen_0;
732 : }
733 70 : else z = x;
734 70 : y = cgetg(l, t_VEC);
735 70 : if (tw==code(t_COMPLEX,t_INT) || tw==code(t_COMPLEX,t_REAL))
736 0 : {
737 0 : long inv = (l >= 5 && signe(imag_i(gel(W,1+(l>>2))))==1) ? 1 : 0;
738 0 : fft(W+1, z+1, y+1, 1, l-1, inv);
739 : } else
740 70 : fft2(W+1, z+1, y+1, 1, l-1);
741 70 : return y;
742 : }
743 :
744 : #undef code
745 :
746 : GEN
747 42 : FFT(GEN W, GEN x)
748 : {
749 42 : if (!is_vec_t(typ(W))) pari_err_TYPE("fft",W);
750 42 : return FFT_i(W, x);
751 : }
752 :
753 : GEN
754 42 : FFTinv(GEN W, GEN x)
755 : {
756 42 : long l = lg(W), i;
757 : GEN w;
758 42 : if (!is_vec_t(typ(W))) pari_err_TYPE("fft",W);
759 42 : w = cgetg(l, t_VECSMALL); /* cf stackdummy */
760 42 : gel(w,1) = gel(W,1); /* w = gconj(W), faster */
761 3787 : for (i = 2; i < l; i++) gel(w, i) = gel(W, l-i+1);
762 42 : return FFT_i(w, x);
763 : }
764 :
765 : /* returns 1 if p has only real coefficients, 0 else */
766 : static int
767 532499 : isreal(GEN p)
768 : {
769 : long i;
770 2765157 : for (i = lg(p)-1; i > 1; i--)
771 2382334 : if (typ(gel(p,i)) == t_COMPLEX) return 0;
772 382823 : return 1;
773 : }
774 :
775 : /* x non complex */
776 : static GEN
777 361684 : abs_update_r(GEN x, double *mu) {
778 361684 : GEN y = gtofp(x, DEFAULTPREC);
779 361691 : double ly = mydbllogr(y); if (ly < *mu) *mu = ly;
780 361688 : setabssign(y); return y;
781 : }
782 : /* return |x|, low accuracy. Set *mu = min(log(y), *mu) */
783 : static GEN
784 6058238 : abs_update(GEN x, double *mu) {
785 : GEN y, xr, yr;
786 : double ly;
787 6058238 : if (typ(x) != t_COMPLEX) return abs_update_r(x, mu);
788 5707492 : xr = gel(x,1);
789 5707492 : yr = gel(x,2);
790 5707492 : if (gequal0(xr)) return abs_update_r(yr,mu);
791 5705860 : if (gequal0(yr)) return abs_update_r(xr,mu);
792 : /* have to treat 0 specially: 0E-10 + 1e-20 = 0E-10 */
793 5696807 : xr = gtofp(xr, DEFAULTPREC);
794 5697598 : yr = gtofp(yr, DEFAULTPREC);
795 5697616 : y = sqrtr(addrr(sqrr(xr), sqrr(yr)));
796 5697767 : ly = mydbllogr(y); if (ly < *mu) *mu = ly;
797 5697755 : return y;
798 : }
799 :
800 : static void
801 560288 : initdft(GEN *Omega, GEN *prim, long N, long Lmax, long bit)
802 : {
803 560288 : long prec = nbits2prec(bit);
804 560289 : *Omega = grootsof1(Lmax, prec) + 1;
805 560284 : *prim = rootsof1u_cx(N, prec);
806 560289 : }
807 :
808 : static void
809 276420 : parameters(GEN p, long *LMAX, double *mu, double *gamma,
810 : int polreal, double param, double param2)
811 : {
812 : GEN q, pc, Omega, A, RU, prim, g, TWO;
813 276420 : long n = degpol(p), bit, NN, K, i, j, Lmax;
814 276419 : pari_sp av2, av = avma;
815 :
816 276419 : bit = gexpo(p) + (long)param2+8;
817 467258 : Lmax = 4; while (Lmax <= n) Lmax <<= 1;
818 276422 : NN = (long)(param*3.14)+1; if (NN < Lmax) NN = Lmax;
819 276422 : K = NN/Lmax; if (K & 1) K++;
820 276422 : NN = Lmax*K;
821 276422 : if (polreal) K = K/2+1;
822 :
823 276422 : initdft(&Omega, &prim, NN, Lmax, bit);
824 276424 : q = mygprec(p,bit) + 2;
825 276425 : A = cgetg(Lmax+1,t_VEC); A++;
826 276425 : pc= cgetg(Lmax+1,t_VEC); pc++;
827 1869861 : for (i=0; i <= n; i++) gel(pc,i)= gel(q,i);
828 756402 : for ( ; i<Lmax; i++) gel(pc,i) = gen_0;
829 :
830 276424 : *mu = pariINFINITY;
831 276424 : g = real_0_bit(-bit);
832 276424 : TWO = real2n(1, DEFAULTPREC);
833 276421 : av2 = avma;
834 276421 : RU = gen_1;
835 1036604 : for (i=0; i<K; i++)
836 : {
837 760180 : if (i) {
838 483764 : GEN z = RU;
839 2627404 : for (j=1; j<n; j++)
840 : {
841 2143633 : gel(pc,j) = gmul(gel(q,j),z);
842 2143595 : z = gmul(z,RU); /* RU = prim^i, z=prim^(ij) */
843 : }
844 483771 : gel(pc,n) = gmul(gel(q,n),z);
845 : }
846 :
847 760184 : fft(Omega,pc,A,1,Lmax,1);
848 833676 : if (polreal && i>0 && i<K-1)
849 994832 : for (j=0; j<Lmax; j++) g = addrr(g, divrr(TWO, abs_update(gel(A,j),mu)));
850 : else
851 5823341 : for (j=0; j<Lmax; j++) g = addrr(g, invr(abs_update(gel(A,j),mu)));
852 759857 : RU = gmul(RU, prim);
853 760183 : if (gc_needed(av,1))
854 : {
855 0 : if(DEBUGMEM>1) pari_warn(warnmem,"parameters");
856 0 : gerepileall(av2,2, &g,&RU);
857 : }
858 : }
859 276424 : *gamma = mydbllog2r(divru(g,NN));
860 276417 : *LMAX = Lmax; set_avma(av);
861 276420 : }
862 :
863 : /* NN is a multiple of Lmax */
864 : static void
865 283867 : dft(GEN p, long k, long NN, long Lmax, long bit, GEN F, GEN H, long polreal)
866 : {
867 : GEN Omega, q, qd, pc, pd, A, B, C, RU, aux, U, W, prim, prim2;
868 283867 : long n = degpol(p), i, j, K;
869 : pari_sp ltop;
870 :
871 283867 : initdft(&Omega, &prim, NN, Lmax, bit);
872 283868 : RU = cgetg(n+2,t_VEC) + 1;
873 :
874 283865 : K = NN/Lmax; if (polreal) K = K/2+1;
875 283865 : q = mygprec(p,bit);
876 283868 : qd = RgX_deriv(q);
877 :
878 283863 : A = cgetg(Lmax+1,t_VEC); A++;
879 283864 : B = cgetg(Lmax+1,t_VEC); B++;
880 283864 : C = cgetg(Lmax+1,t_VEC); C++;
881 283864 : pc = cgetg(Lmax+1,t_VEC); pc++;
882 283865 : pd = cgetg(Lmax+1,t_VEC); pd++;
883 803746 : gel(pc,0) = gel(q,2); for (i=n+1; i<Lmax; i++) gel(pc,i) = gen_0;
884 1087618 : gel(pd,0) = gel(qd,2); for (i=n; i<Lmax; i++) gel(pd,i) = gen_0;
885 :
886 283869 : ltop = avma;
887 283869 : W = cgetg(k+1,t_VEC);
888 283868 : U = cgetg(k+1,t_VEC);
889 763640 : for (i=1; i<=k; i++) gel(W,i) = gel(U,i) = gen_0;
890 :
891 283868 : gel(RU,0) = gen_1;
892 283868 : prim2 = gen_1;
893 873465 : for (i=0; i<K; i++)
894 : {
895 589598 : gel(RU,1) = prim2;
896 3116057 : for (j=1; j<n; j++) gel(RU,j+1) = gmul(gel(RU,j),prim2);
897 : /* RU[j] = prim^(ij)= prim2^j */
898 :
899 3116018 : for (j=1; j<n; j++) gel(pd,j) = gmul(gel(qd,j+2),gel(RU,j));
900 589526 : fft(Omega,pd,A,1,Lmax,1);
901 3705565 : for (j=1; j<=n; j++) gel(pc,j) = gmul(gel(q,j+2),gel(RU,j));
902 589527 : fft(Omega,pc,B,1,Lmax,1);
903 5462650 : for (j=0; j<Lmax; j++) gel(C,j) = ginv(gel(B,j));
904 5462583 : for (j=0; j<Lmax; j++) gel(B,j) = gmul(gel(A,j),gel(C,j));
905 589456 : fft(Omega,B,A,1,Lmax,1);
906 589596 : fft(Omega,C,B,1,Lmax,1);
907 :
908 589599 : if (polreal) /* p has real coefficients */
909 : {
910 395147 : if (i>0 && i<K-1)
911 : {
912 98937 : for (j=1; j<=k; j++)
913 : {
914 83297 : gel(W,j) = gadd(gel(W,j), gshift(mulreal(gel(A,j+1),gel(RU,j+1)),1));
915 83297 : gel(U,j) = gadd(gel(U,j), gshift(mulreal(gel(B,j),gel(RU,j)),1));
916 : }
917 : }
918 : else
919 : {
920 998991 : for (j=1; j<=k; j++)
921 : {
922 635138 : gel(W,j) = gadd(gel(W,j), mulreal(gel(A,j+1),gel(RU,j+1)));
923 635119 : gel(U,j) = gadd(gel(U,j), mulreal(gel(B,j),gel(RU,j)));
924 : }
925 : }
926 : }
927 : else
928 : {
929 557679 : for (j=1; j<=k; j++)
930 : {
931 347591 : gel(W,j) = gadd(gel(W,j), gmul(gel(A,j+1),gel(RU,j+1)));
932 347592 : gel(U,j) = gadd(gel(U,j), gmul(gel(B,j),gel(RU,j)));
933 : }
934 : }
935 589581 : prim2 = gmul(prim2,prim);
936 589585 : gerepileall(ltop,3, &W,&U,&prim2);
937 : }
938 :
939 763629 : for (i=1; i<=k; i++)
940 : {
941 479771 : aux=gel(W,i);
942 871904 : for (j=1; j<i; j++) aux = gadd(aux, gmul(gel(W,i-j),gel(F,k+2-j)));
943 479767 : gel(F,k+2-i) = gdivgs(aux,-i*NN);
944 : }
945 763620 : for (i=0; i<k; i++)
946 : {
947 479767 : aux=gel(U,k-i);
948 871902 : for (j=1+i; j<k; j++) aux = gadd(aux,gmul(gel(F,2+j),gel(U,j-i)));
949 479767 : gel(H,i+2) = gdivgu(aux,NN);
950 : }
951 283853 : }
952 :
953 : #define NEWTON_MAX 10
954 : static GEN
955 1393883 : refine_H(GEN F, GEN G, GEN HH, long bit, long Sbit)
956 : {
957 1393883 : GEN H = HH, D, aux;
958 1393883 : pari_sp ltop = avma;
959 : long error, i, bit1, bit2;
960 :
961 1393883 : D = Rg_RgX_sub(gen_1, RgX_rem(RgX_mul(H,G),F)); error = gexpo(D);
962 1393836 : bit2 = bit + Sbit;
963 2572413 : for (i=0; error>-bit && i<NEWTON_MAX && error<=0; i++)
964 : {
965 1178573 : if (gc_needed(ltop,1))
966 : {
967 0 : if(DEBUGMEM>1) pari_warn(warnmem,"refine_H");
968 0 : gerepileall(ltop,2, &D,&H);
969 : }
970 1178573 : bit1 = -error + Sbit;
971 1178573 : aux = RgX_mul(mygprec(H,bit1), mygprec(D,bit1));
972 1178558 : aux = RgX_rem(mygprec(aux,bit1), mygprec(F,bit1));
973 :
974 1178573 : bit1 = -error*2 + Sbit; if (bit1 > bit2) bit1 = bit2;
975 1178573 : H = RgX_add(mygprec(H,bit1), aux);
976 1178559 : D = Rg_RgX_sub(gen_1, RgX_rem(RgX_mul(H,G),F));
977 1178572 : error = gexpo(D); if (error < -bit1) error = -bit1;
978 : }
979 1393840 : if (error > -bit/2) return NULL; /* FAIL */
980 1393522 : return gerepilecopy(ltop,H);
981 : }
982 :
983 : /* return 0 if fails, 1 else */
984 : static long
985 283866 : refine_F(GEN p, GEN *F, GEN *G, GEN H, long bit, double gamma)
986 : {
987 283866 : GEN f0, FF, GG, r, HH = H;
988 283866 : long error, i, bit1 = 0, bit2, Sbit, Sbit2, enh, normF, normG, n = degpol(p);
989 283866 : pari_sp av = avma;
990 :
991 283866 : FF = *F; GG = RgX_divrem(p, FF, &r);
992 283865 : error = gexpo(r); if (error <= -bit) error = 1-bit;
993 283865 : normF = gexpo(FF);
994 283868 : normG = gexpo(GG);
995 283867 : enh = gexpo(H); if (enh < 0) enh = 0;
996 283868 : Sbit = normF + 2*normG + enh + (long)(4.*log2((double)n)+gamma) + 1;
997 283868 : Sbit2 = enh + 2*(normF+normG) + (long)(2.*gamma+5.*log2((double)n)) + 1;
998 283868 : bit2 = bit + Sbit;
999 1677392 : for (i=0; error>-bit && i<NEWTON_MAX && error<=0; i++)
1000 : {
1001 1393860 : if (bit1 == bit2 && i >= 2) { Sbit += n; Sbit2 += n; bit2 += n; }
1002 1393860 : if (gc_needed(av,1))
1003 : {
1004 0 : if(DEBUGMEM>1) pari_warn(warnmem,"refine_F");
1005 0 : gerepileall(av,4, &FF,&GG,&r,&HH);
1006 : }
1007 :
1008 1393860 : bit1 = -error + Sbit2;
1009 1393860 : HH = refine_H(mygprec(FF,bit1), mygprec(GG,bit1), mygprec(HH,bit1),
1010 : 1-error, Sbit2);
1011 1393854 : if (!HH) return 0; /* FAIL */
1012 :
1013 1393536 : bit1 = -error + Sbit;
1014 1393536 : r = RgX_mul(mygprec(HH,bit1), mygprec(r,bit1));
1015 1393509 : f0 = RgX_rem(mygprec(r,bit1), mygprec(FF,bit1));
1016 :
1017 1393527 : bit1 = -2*error + Sbit; if (bit1 > bit2) bit1 = bit2;
1018 1393527 : FF = gadd(mygprec(FF,bit1),f0);
1019 :
1020 1393511 : bit1 = -3*error + Sbit; if (bit1 > bit2) bit1 = bit2;
1021 1393511 : GG = RgX_divrem(mygprec(p,bit1), mygprec(FF,bit1), &r);
1022 1393527 : error = gexpo(r); if (error < -bit1) error = -bit1;
1023 : }
1024 283532 : if (error>-bit) return 0; /* FAIL */
1025 276405 : *F = FF; *G = GG; return 1;
1026 : }
1027 :
1028 : /* returns F and G from the unit circle U such that |p-FG|<2^(-bit) |cd|,
1029 : where cd is the leading coefficient of p */
1030 : static void
1031 276424 : split_fromU(GEN p, long k, double delta, long bit,
1032 : GEN *F, GEN *G, double param, double param2)
1033 : {
1034 : GEN pp, FF, GG, H;
1035 276424 : long n = degpol(p), NN, bit2, Lmax;
1036 276424 : int polreal = isreal(p);
1037 : pari_sp ltop;
1038 : double mu, gamma;
1039 :
1040 276423 : pp = gdiv(p, gel(p,2+n));
1041 276422 : parameters(pp, &Lmax,&mu,&gamma, polreal,param,param2);
1042 :
1043 276421 : H = cgetg(k+2,t_POL); H[1] = p[1];
1044 276420 : FF = cgetg(k+3,t_POL); FF[1]= p[1];
1045 276421 : gel(FF,k+2) = gen_1;
1046 :
1047 276421 : NN = (long)(0.5/delta); NN |= 1; if (NN < 2) NN = 2;
1048 276421 : NN *= Lmax; ltop = avma;
1049 : for(;;)
1050 : {
1051 283866 : bit2 = (long)(((double)NN*delta-mu)/M_LN2) + gexpo(pp) + 8;
1052 283867 : dft(pp, k, NN, Lmax, bit2, FF, H, polreal);
1053 283863 : if (refine_F(pp,&FF,&GG,H,bit,gamma)) break;
1054 7445 : NN <<= 1; set_avma(ltop);
1055 : }
1056 276418 : *G = gmul(GG,gel(p,2+n)); *F = FF;
1057 276419 : }
1058 :
1059 : static void
1060 276424 : optimize_split(GEN p, long k, double delta, long bit,
1061 : GEN *F, GEN *G, double param, double param2)
1062 : {
1063 276424 : long n = degpol(p);
1064 : GEN FF, GG;
1065 :
1066 276423 : if (k <= n/2)
1067 209745 : split_fromU(p,k,delta,bit,F,G,param,param2);
1068 : else
1069 : {
1070 66678 : split_fromU(RgX_recip_i(p),n-k,delta,bit,&FF,&GG,param,param2);
1071 66677 : *F = RgX_recip_i(GG);
1072 66677 : *G = RgX_recip_i(FF);
1073 : }
1074 276419 : }
1075 :
1076 : /********************************************************************/
1077 : /** **/
1078 : /** SEARCH FOR SEPARATING CIRCLE **/
1079 : /** **/
1080 : /********************************************************************/
1081 :
1082 : /* return p(2^e*x) *2^(-n*e) */
1083 : static void
1084 0 : scalepol2n(GEN p, long e)
1085 : {
1086 0 : long i,n=lg(p)-1;
1087 0 : for (i=2; i<=n; i++) gel(p,i) = gmul2n(gel(p,i),(i-n)*e);
1088 0 : }
1089 :
1090 : /* returns p(x/R)*R^n; assume R is at the correct accuracy */
1091 : static GEN
1092 2365614 : scalepol(GEN p, GEN R, long bit)
1093 2365614 : { return RgX_rescale(mygprec(p, bit), R); }
1094 :
1095 : /* return (conj(a)X-1)^n * p[ (X-a) / (conj(a)X-1) ] */
1096 : static GEN
1097 768340 : conformal_basecase(GEN p, GEN a)
1098 : {
1099 : GEN z, r, ma, ca;
1100 768340 : long i, n = degpol(p);
1101 : pari_sp av;
1102 :
1103 768340 : if (n <= 0) return p;
1104 768340 : ma = gneg(a); ca = conj_i(a);
1105 768341 : av = avma;
1106 768341 : z = deg1pol_shallow(ca, gen_m1, 0);
1107 768333 : r = scalarpol_shallow(gel(p,2+n), 0);
1108 768334 : for (i=n-1; ; i--)
1109 : {
1110 2372851 : r = RgX_addmulXn_shallow(r, gmul(ma,r), 1); /* r *= (X - a) */
1111 2372819 : r = gadd(r, gmul(z, gel(p,2+i)));
1112 2372839 : if (i == 0) return gerepileupto(av, r);
1113 1604506 : z = RgX_addmulXn_shallow(gmul(z,ca), gneg(z), 1); /* z *= conj(a)X - 1 */
1114 1604515 : if (gc_needed(av,2))
1115 : {
1116 0 : if(DEBUGMEM>1) pari_warn(warnmem,"conformal_pol (%ld/%ld)",n-i, n);
1117 0 : gerepileall(av,2, &r,&z);
1118 : }
1119 : }
1120 : }
1121 : static GEN
1122 768458 : conformal_pol(GEN p, GEN a)
1123 : {
1124 768458 : pari_sp av = avma;
1125 768458 : long d, nR, n = degpol(p), v;
1126 : GEN Q, R, S, T;
1127 768457 : if (n < 35) return conformal_basecase(p, a);
1128 118 : d = (n+1) >> 1; v = varn(p);
1129 118 : Q = RgX_shift_shallow(p, -d);
1130 118 : R = RgXn_red_shallow(p, d);
1131 118 : Q = conformal_pol(Q, a);
1132 118 : R = conformal_pol(R, a);
1133 118 : S = gpowgs(deg1pol_shallow(gen_1, gneg(a), v), d);
1134 118 : T = RgX_recip_i(S);
1135 118 : if (typ(a) == t_COMPLEX) T = gconj(T);
1136 118 : if (odd(d)) T = RgX_neg(T);
1137 : /* S = (X - a)^d, T = (conj(a) X - 1)^d */
1138 118 : nR = n - degpol(R) - d; /* >= 0 */
1139 118 : if (nR) T = RgX_mul(T, gpowgs(deg1pol_shallow(gconj(a), gen_m1, v), nR));
1140 118 : return gerepileupto(av, RgX_add(RgX_mul(Q, S), RgX_mul(R, T)));
1141 : }
1142 :
1143 : static const double UNDEF = -100000.;
1144 :
1145 : static double
1146 276418 : logradius(double *radii, GEN p, long k, double aux, double *delta)
1147 : {
1148 276418 : long i, n = degpol(p);
1149 : double lrho, lrmin, lrmax;
1150 276419 : if (k > 1)
1151 : {
1152 238884 : i = k-1; while (i>0 && radii[i] == UNDEF) i--;
1153 163908 : lrmin = logpre_modulus(p,k,aux, radii[i], radii[k]);
1154 : }
1155 : else /* k=1 */
1156 112511 : lrmin = logmin_modulus(p,aux);
1157 276422 : radii[k] = lrmin;
1158 :
1159 276422 : if (k+1<n)
1160 : {
1161 419323 : i = k+2; while (i<=n && radii[i] == UNDEF) i++;
1162 225975 : lrmax = logpre_modulus(p,k+1,aux, radii[k+1], radii[i]);
1163 : }
1164 : else /* k+1=n */
1165 50447 : lrmax = logmax_modulus(p,aux);
1166 276423 : radii[k+1] = lrmax;
1167 :
1168 276423 : lrho = radii[k];
1169 563234 : for (i=k-1; i>=1; i--)
1170 : {
1171 286811 : if (radii[i] == UNDEF || radii[i] > lrho)
1172 206422 : radii[i] = lrho;
1173 : else
1174 80389 : lrho = radii[i];
1175 : }
1176 276423 : lrho = radii[k+1];
1177 1030196 : for (i=k+1; i<=n; i++)
1178 : {
1179 753773 : if (radii[i] == UNDEF || radii[i] < lrho)
1180 406743 : radii[i] = lrho;
1181 : else
1182 347030 : lrho = radii[i];
1183 : }
1184 276423 : *delta = (lrmax - lrmin) / 2;
1185 276423 : if (*delta > 1.) *delta = 1.;
1186 276423 : return (lrmin + lrmax) / 2;
1187 : }
1188 :
1189 : static void
1190 276423 : update_radius(long n, double *radii, double lrho, double *par, double *par2)
1191 : {
1192 276423 : double t, param = 0., param2 = 0.;
1193 : long i;
1194 1593362 : for (i=1; i<=n; i++)
1195 : {
1196 1316964 : radii[i] -= lrho;
1197 1316964 : t = fabs(rtodbl( invr(subsr(1, dblexp(radii[i]))) ));
1198 1316939 : param += t; if (t > 1.) param2 += log2(t);
1199 : }
1200 276398 : *par = param; *par2 = param2;
1201 276398 : }
1202 :
1203 : /* apply the conformal mapping then split from U */
1204 : static void
1205 256071 : conformal_mapping(double *radii, GEN ctr, GEN p, long k, long bit,
1206 : double aux, GEN *F,GEN *G)
1207 : {
1208 256071 : long bit2, n = degpol(p), i;
1209 256073 : pari_sp ltop = avma, av;
1210 : GEN q, FF, GG, a, R;
1211 : double lrho, delta, param, param2;
1212 : /* n * (2.*log2(2.732)+log2(1.5)) + 1 */
1213 256073 : bit2 = bit + (long)(n*3.4848775) + 1;
1214 256073 : a = sqrtr_abs( utor(3, 2*MEDDEFAULTPREC - 2) );
1215 256071 : a = divrs(a, -6);
1216 256074 : a = gmul(mygprec(a,bit2), mygprec(ctr,bit2)); /* a = -ctr/2sqrt(3) */
1217 :
1218 256070 : av = avma;
1219 256070 : q = conformal_pol(mygprec(p,bit2), a);
1220 1442517 : for (i=1; i<=n; i++)
1221 1186446 : if (radii[i] != UNDEF) /* update array radii */
1222 : {
1223 905765 : pari_sp av2 = avma;
1224 905765 : GEN t, r = dblexp(radii[i]), r2 = sqrr(r);
1225 : /* 2(r^2 - 1) / (r^2 - 3(r-1)) */
1226 905758 : t = divrr(shiftr((subrs(r2,1)),1), subrr(r2, mulur(3,subrs(r,1))));
1227 905744 : radii[i] = mydbllogr(addsr(1,t)) / 2;
1228 905755 : set_avma(av2);
1229 : }
1230 256071 : lrho = logradius(radii, q,k,aux/10., &delta);
1231 256078 : update_radius(n, radii, lrho, ¶m, ¶m2);
1232 :
1233 256074 : bit2 += (long)(n * fabs(lrho)/M_LN2 + 1.);
1234 256074 : R = mygprec(dblexp(-lrho), bit2);
1235 256080 : q = scalepol(q,R,bit2);
1236 256074 : gerepileall(av,2, &q,&R);
1237 :
1238 256079 : optimize_split(q,k,delta,bit2,&FF,&GG,param,param2);
1239 256075 : bit2 += n; R = invr(R);
1240 256079 : FF = scalepol(FF,R,bit2);
1241 256072 : GG = scalepol(GG,R,bit2);
1242 :
1243 256073 : a = mygprec(a,bit2);
1244 256077 : FF = conformal_pol(FF,a);
1245 256077 : GG = conformal_pol(GG,a);
1246 :
1247 256079 : a = invr(subsr(1, gnorm(a)));
1248 256074 : FF = RgX_Rg_mul(FF, powru(a,k));
1249 256073 : GG = RgX_Rg_mul(GG, powru(a,n-k));
1250 :
1251 256076 : *F = mygprec(FF,bit+n);
1252 256079 : *G = mygprec(GG,bit+n); gerepileall(ltop,2, F,G);
1253 256080 : }
1254 :
1255 : /* split p, this time without scaling. returns in F and G two polynomials
1256 : * such that |p-FG|< 2^(-bit)|p| */
1257 : static void
1258 276423 : split_2(GEN p, long bit, GEN ctr, double thickness, GEN *F, GEN *G)
1259 : {
1260 : GEN q, FF, GG, R;
1261 : double aux, delta, param, param2;
1262 276423 : long n = degpol(p), i, j, k, bit2;
1263 : double lrmin, lrmax, lrho, *radii;
1264 :
1265 276423 : radii = (double*) stack_malloc_align((n+1) * sizeof(double), sizeof(double));
1266 :
1267 1040576 : for (i=2; i<n; i++) radii[i] = UNDEF;
1268 276422 : aux = thickness/(double)(4 * n);
1269 276422 : lrmin = logmin_modulus(p, aux);
1270 276420 : lrmax = logmax_modulus(p, aux);
1271 276421 : radii[1] = lrmin;
1272 276421 : radii[n] = lrmax;
1273 276421 : i = 1; j = n;
1274 276421 : lrho = (lrmin + lrmax) / 2;
1275 276421 : k = dual_modulus(p, lrho, aux, 1);
1276 276420 : if (5*k < n || (n < 2*k && 5*k < 4*n))
1277 44349 : { lrmax = lrho; j=k+1; radii[j] = lrho; }
1278 : else
1279 232071 : { lrmin = lrho; i=k; radii[i] = lrho; }
1280 576588 : while (j > i+1)
1281 : {
1282 300168 : if (i+j == n+1)
1283 153868 : lrho = (lrmin + lrmax) / 2;
1284 : else
1285 : {
1286 146300 : double kappa = 2. - log(1. + minss(i,n-j)) / log(1. + minss(j,n-i));
1287 146300 : if (i+j < n+1) lrho = lrmax * kappa + lrmin;
1288 117292 : else lrho = lrmin * kappa + lrmax;
1289 146300 : lrho /= 1+kappa;
1290 : }
1291 300168 : aux = (lrmax - lrmin) / (4*(j-i));
1292 300168 : k = dual_modulus(p, lrho, aux, minss(i,n+1-j));
1293 300168 : if (k-i < j-k-1 || (k-i == j-k-1 && 2*k > n))
1294 213493 : { lrmax = lrho; j=k+1; radii[j] = lrho - aux; }
1295 : else
1296 86675 : { lrmin = lrho; i=k; radii[i] = lrho + aux; }
1297 : }
1298 276420 : aux = lrmax - lrmin;
1299 :
1300 276420 : if (ctr)
1301 : {
1302 256075 : lrho = (lrmax + lrmin) / 2;
1303 1442572 : for (i=1; i<=n; i++)
1304 1186497 : if (radii[i] != UNDEF) radii[i] -= lrho;
1305 :
1306 256075 : bit2 = bit + (long)(n * fabs(lrho)/M_LN2 + 1.);
1307 256075 : R = mygprec(dblexp(-lrho), bit2);
1308 256076 : q = scalepol(p,R,bit2);
1309 256071 : conformal_mapping(radii, ctr, q, k, bit2, aux, &FF, &GG);
1310 : }
1311 : else
1312 : {
1313 20345 : lrho = logradius(radii, p, k, aux/10., &delta);
1314 20345 : update_radius(n, radii, lrho, ¶m, ¶m2);
1315 :
1316 20345 : bit2 = bit + (long)(n * fabs(lrho)/M_LN2 + 1.);
1317 20345 : R = mygprec(dblexp(-lrho), bit2);
1318 20345 : q = scalepol(p,R,bit2);
1319 20345 : optimize_split(q, k, delta, bit2, &FF, &GG, param, param2);
1320 : }
1321 276421 : bit += n;
1322 276421 : bit2 += n; R = invr(mygprec(R,bit2));
1323 276416 : *F = mygprec(scalepol(FF,R,bit2), bit);
1324 276422 : *G = mygprec(scalepol(GG,R,bit2), bit);
1325 276422 : }
1326 :
1327 : /* procedure corresponding to steps 5,6,.. page 44 in RR n. 1852 */
1328 : /* put in F and G two polynomial such that |p-FG|<2^(-bit)|p|
1329 : * where the maximum modulus of the roots of p is <=1.
1330 : * Assume sum of roots is 0. */
1331 : static void
1332 256078 : split_1(GEN p, long bit, GEN *F, GEN *G)
1333 : {
1334 256078 : long i, imax, n = degpol(p), polreal = isreal(p), ep = gexpo(p), bit2 = bit+n;
1335 : GEN ctr, q, qq, FF, GG, v, gr, r, newq;
1336 : double lrmin, lrmax, lthick;
1337 256077 : const double LOG3 = 1.098613;
1338 :
1339 256077 : lrmax = logmax_modulus(p, 0.01);
1340 256078 : gr = mygprec(dblexp(-lrmax), bit2);
1341 256075 : q = scalepol(p,gr,bit2);
1342 :
1343 256076 : bit2 = bit + gexpo(q) - ep + (long)((double)n*2.*log2(3.)+1);
1344 256078 : v = cgetg(5,t_VEC);
1345 256076 : gel(v,1) = gen_2;
1346 256076 : gel(v,2) = gen_m2;
1347 256076 : gel(v,3) = mkcomplex(gen_0, gel(v,1));
1348 256076 : gel(v,4) = mkcomplex(gen_0, gel(v,2));
1349 256078 : q = mygprec(q,bit2); lthick = 0;
1350 256078 : newq = ctr = NULL; /* -Wall */
1351 256078 : imax = polreal? 3: 4;
1352 531655 : for (i=1; i<=imax; i++)
1353 : {
1354 525071 : qq = RgX_translate(q, gel(v,i));
1355 525073 : lrmin = logmin_modulus(qq,0.05);
1356 525066 : if (LOG3 > lrmin + lthick)
1357 : {
1358 514178 : double lquo = logmax_modulus(qq,0.05) - lrmin;
1359 514184 : if (lquo > lthick) { lthick = lquo; newq = qq; ctr = gel(v,i); }
1360 : }
1361 525072 : if (lthick > M_LN2) break;
1362 319761 : if (polreal && i==2 && lthick > LOG3 - M_LN2) break;
1363 : }
1364 256079 : bit2 = bit + gexpo(newq) - ep + (long)(n*LOG3/M_LN2 + 1);
1365 256079 : split_2(newq, bit2, ctr, lthick, &FF, &GG);
1366 256075 : r = gneg(mygprec(ctr,bit2));
1367 256076 : FF = RgX_translate(FF,r);
1368 256076 : GG = RgX_translate(GG,r);
1369 :
1370 256079 : gr = invr(gr); bit2 = bit - ep + gexpo(FF)+gexpo(GG);
1371 256080 : *F = scalepol(FF,gr,bit2);
1372 256076 : *G = scalepol(GG,gr,bit2);
1373 256078 : }
1374 :
1375 : /* put in F and G two polynomials such that |P-FG|<2^(-bit)|P|,
1376 : where the maximum modulus of the roots of p is < 0.5 */
1377 : static int
1378 256214 : split_0_2(GEN p, long bit, GEN *F, GEN *G)
1379 : {
1380 : GEN q, b;
1381 256214 : long n = degpol(p), k, bit2, eq;
1382 256214 : double aux0 = dbllog2(gel(p,n+2)); /* != -oo */
1383 256214 : double aux1 = dbllog2(gel(p,n+1)), aux;
1384 :
1385 256214 : if (aux1 == -pariINFINITY) /* p1 = 0 */
1386 7912 : aux = 0;
1387 : else
1388 : {
1389 248302 : aux = aux1 - aux0; /* log2(p1/p0) */
1390 : /* beware double overflow */
1391 248302 : if (aux >= 0 && (aux > 1e4 || exp2(aux) > 2.5*n)) return 0;
1392 248302 : aux = (aux < -300)? 0.: n*log2(1 + exp2(aux)/(double)n);
1393 : }
1394 256214 : bit2 = bit+1 + (long)(log2((double)n) + aux);
1395 256214 : q = mygprec(p,bit2);
1396 256215 : if (aux1 == -pariINFINITY) b = NULL;
1397 : else
1398 : {
1399 248303 : b = gdivgs(gdiv(gel(q,n+1),gel(q,n+2)),-n);
1400 248297 : q = RgX_translate(q,b);
1401 : }
1402 256214 : gel(q,n+1) = gen_0; eq = gexpo(q);
1403 256213 : k = 0;
1404 256578 : while (k <= n/2 && (- gexpo(gel(q,k+2)) > bit2 + 2*(n-k) + eq
1405 256450 : || gequal0(gel(q,k+2)))) k++;
1406 256213 : if (k > 0)
1407 : {
1408 135 : if (k > n/2) k = n/2;
1409 135 : bit2 += k<<1;
1410 135 : *F = pol_xn(k, 0);
1411 135 : *G = RgX_shift_shallow(q, -k);
1412 : }
1413 : else
1414 : {
1415 256078 : split_1(q,bit2,F,G);
1416 256078 : bit2 = bit + gexpo(*F) + gexpo(*G) - gexpo(p) + (long)aux+1;
1417 256079 : *F = mygprec(*F,bit2);
1418 : }
1419 256214 : *G = mygprec(*G,bit2);
1420 256214 : if (b)
1421 : {
1422 248302 : GEN mb = mygprec(gneg(b), bit2);
1423 248300 : *F = RgX_translate(*F, mb);
1424 248302 : *G = RgX_translate(*G, mb);
1425 : }
1426 256215 : return 1;
1427 : }
1428 :
1429 : /* put in F and G two polynomials such that |P-FG|<2^(-bit)|P|.
1430 : * Assume max_modulus(p) < 2 */
1431 : static void
1432 256214 : split_0_1(GEN p, long bit, GEN *F, GEN *G)
1433 : {
1434 : GEN FF, GG;
1435 : long n, bit2, normp;
1436 :
1437 256214 : if (split_0_2(p,bit,F,G)) return;
1438 :
1439 0 : normp = gexpo(p);
1440 0 : scalepol2n(p,2); /* p := 4^(-n) p(4*x) */
1441 0 : n = degpol(p); bit2 = bit + 2*n + gexpo(p) - normp;
1442 0 : split_1(mygprec(p,bit2), bit2,&FF,&GG);
1443 0 : scalepol2n(FF,-2);
1444 0 : scalepol2n(GG,-2); bit2 = bit + gexpo(FF) + gexpo(GG) - normp;
1445 0 : *F = mygprec(FF,bit2);
1446 0 : *G = mygprec(GG,bit2);
1447 : }
1448 :
1449 : /* put in F and G two polynomials such that |P-FG|<2^(-bit)|P| */
1450 : static void
1451 276557 : split_0(GEN p, long bit, GEN *F, GEN *G)
1452 : {
1453 276557 : const double LOG1_9 = 0.6418539;
1454 276557 : long n = degpol(p), k = 0;
1455 : GEN q;
1456 :
1457 276557 : while (gexpo(gel(p,k+2)) < -bit && k <= n/2) k++;
1458 276557 : if (k > 0)
1459 : {
1460 0 : if (k > n/2) k = n/2;
1461 0 : *F = pol_xn(k, 0);
1462 0 : *G = RgX_shift_shallow(p, -k);
1463 : }
1464 : else
1465 : {
1466 276557 : double lr = logmax_modulus(p, 0.05);
1467 276556 : if (lr < LOG1_9) split_0_1(p, bit, F, G);
1468 : else
1469 : {
1470 226150 : q = RgX_recip_i(p);
1471 226151 : lr = logmax_modulus(q,0.05);
1472 226153 : if (lr < LOG1_9)
1473 : {
1474 205808 : split_0_1(q, bit, F, G);
1475 205809 : *F = RgX_recip_i(*F);
1476 205808 : *G = RgX_recip_i(*G);
1477 : }
1478 : else
1479 20345 : split_2(p,bit,NULL, 1.2837,F,G);
1480 : }
1481 : }
1482 276559 : }
1483 :
1484 : /********************************************************************/
1485 : /** **/
1486 : /** ERROR ESTIMATE FOR THE ROOTS **/
1487 : /** **/
1488 : /********************************************************************/
1489 :
1490 : static GEN
1491 1154252 : root_error(long n, long k, GEN roots_pol, long err, GEN shatzle)
1492 : {
1493 1154252 : GEN rho, d, eps, epsbis, eps2, aux, rap = NULL;
1494 : long i, j;
1495 :
1496 1154252 : d = cgetg(n+1,t_VEC);
1497 9150637 : for (i=1; i<=n; i++)
1498 : {
1499 7996541 : if (i!=k)
1500 : {
1501 6842362 : aux = gsub(gel(roots_pol,i), gel(roots_pol,k));
1502 6841241 : gel(d,i) = gabs(mygprec(aux,31), DEFAULTPREC);
1503 : }
1504 : }
1505 1154096 : rho = gabs(mygprec(gel(roots_pol,k),31), DEFAULTPREC);
1506 1154252 : if (expo(rho) < 0) rho = real_1(DEFAULTPREC);
1507 1154252 : eps = mulrr(rho, shatzle);
1508 1154161 : aux = shiftr(powru(rho,n), err);
1509 :
1510 3525554 : for (j=1; j<=2 || (j<=5 && cmprr(rap, dbltor(1.2)) > 0); j++)
1511 : {
1512 2371525 : GEN prod = NULL; /* 1. */
1513 2371525 : long m = n;
1514 2371525 : epsbis = mulrr(eps, dbltor(1.25));
1515 20477767 : for (i=1; i<=n; i++)
1516 : {
1517 18105925 : if (i != k && cmprr(gel(d,i),epsbis) > 0)
1518 : {
1519 15698328 : GEN dif = subrr(gel(d,i),eps);
1520 15694166 : prod = prod? mulrr(prod, dif): dif;
1521 15696330 : m--;
1522 : }
1523 : }
1524 2371842 : eps2 = prod? divrr(aux, prod): aux;
1525 2371290 : if (m > 1) eps2 = sqrtnr(shiftr(eps2, 2*m-2), m);
1526 2371290 : rap = divrr(eps,eps2); eps = eps2;
1527 : }
1528 1154152 : return eps;
1529 : }
1530 :
1531 : /* round a complex or real number x to an absolute value of 2^(-bit) */
1532 : static GEN
1533 2945451 : mygprec_absolute(GEN x, long bit)
1534 : {
1535 : long e;
1536 : GEN y;
1537 :
1538 2945451 : switch(typ(x))
1539 : {
1540 1911201 : case t_REAL:
1541 1911201 : e = expo(x) + bit;
1542 1911201 : return (e <= 0 || !signe(x))? real_0_bit(-bit): rtor(x, nbits2prec(e));
1543 907561 : case t_COMPLEX:
1544 907561 : if (gexpo(gel(x,2)) < -bit) return mygprec_absolute(gel(x,1),bit);
1545 883689 : y = cgetg(3,t_COMPLEX);
1546 883693 : gel(y,1) = mygprec_absolute(gel(x,1),bit);
1547 883711 : gel(y,2) = mygprec_absolute(gel(x,2),bit);
1548 883722 : return y;
1549 126689 : default: return x;
1550 : }
1551 : }
1552 :
1553 : static long
1554 283253 : a_posteriori_errors(GEN p, GEN roots_pol, long err)
1555 : {
1556 283253 : long i, n = degpol(p), e_max = -(long)EXPOBITS;
1557 : GEN sigma, shatzle;
1558 :
1559 283252 : err += (long)log2((double)n) + 1;
1560 283252 : if (err > -2) return 0;
1561 283252 : sigma = real2n(-err, LOWDEFAULTPREC);
1562 : /* 2 / ((s - 1)^(1/n) - 1) */
1563 283256 : shatzle = divur(2, subrs(sqrtnr(subrs(sigma,1),n), 1));
1564 1437490 : for (i=1; i<=n; i++)
1565 : {
1566 1154233 : pari_sp av = avma;
1567 1154233 : GEN x = root_error(n,i,roots_pol,err,shatzle);
1568 1154188 : long e = gexpo(x);
1569 1154204 : set_avma(av); if (e > e_max) e_max = e;
1570 1154212 : gel(roots_pol,i) = mygprec_absolute(gel(roots_pol,i), -e);
1571 : }
1572 283257 : return e_max;
1573 : }
1574 :
1575 : /********************************************************************/
1576 : /** **/
1577 : /** MAIN **/
1578 : /** **/
1579 : /********************************************************************/
1580 : static GEN
1581 919434 : append_clone(GEN r, GEN a) { a = gclone(a); vectrunc_append(r, a); return a; }
1582 :
1583 : /* put roots in placeholder roots_pol so that |P - L_1...L_n| < 2^(-bit)|P|
1584 : * returns prod (x-roots_pol[i]) */
1585 : static GEN
1586 836371 : split_complete(GEN p, long bit, GEN roots_pol)
1587 : {
1588 836371 : long n = degpol(p);
1589 : pari_sp ltop;
1590 : GEN p1, F, G, a, b, m1, m2;
1591 :
1592 836369 : if (n == 1)
1593 : {
1594 200162 : a = gneg_i(gdiv(gel(p,2), gel(p,3)));
1595 200164 : (void)append_clone(roots_pol,a); return p;
1596 : }
1597 636207 : ltop = avma;
1598 636207 : if (n == 2)
1599 : {
1600 359651 : F = gsub(gsqr(gel(p,3)), gmul2n(gmul(gel(p,2),gel(p,4)), 2));
1601 359633 : F = gsqrt(F, nbits2prec(bit));
1602 359646 : p1 = ginv(gmul2n(gel(p,4),1));
1603 359646 : a = gneg_i(gmul(gadd(F,gel(p,3)), p1));
1604 359651 : b = gmul(gsub(F,gel(p,3)), p1);
1605 359639 : a = append_clone(roots_pol,a);
1606 359650 : b = append_clone(roots_pol,b); set_avma(ltop);
1607 359652 : a = mygprec(a, 3*bit);
1608 359651 : b = mygprec(b, 3*bit);
1609 359653 : return gmul(gel(p,4), mkpoln(3, gen_1, gneg(gadd(a,b)), gmul(a,b)));
1610 : }
1611 276556 : split_0(p,bit,&F,&G);
1612 276559 : m1 = split_complete(F,bit,roots_pol);
1613 276560 : m2 = split_complete(G,bit,roots_pol);
1614 276559 : return gerepileupto(ltop, gmul(m1,m2));
1615 : }
1616 :
1617 : static GEN
1618 3134079 : quicktofp(GEN x)
1619 : {
1620 3134079 : const long prec = DEFAULTPREC;
1621 3134079 : switch(typ(x))
1622 : {
1623 3114826 : case t_INT: return itor(x, prec);
1624 8169 : case t_REAL: return rtor(x, prec);
1625 0 : case t_FRAC: return fractor(x, prec);
1626 11090 : case t_COMPLEX: {
1627 11090 : GEN a = gel(x,1), b = gel(x,2);
1628 : /* avoid problem with 0, e.g. x = 0 + I*1e-100. We don't want |x| = 0. */
1629 11090 : if (isintzero(a)) return cxcompotor(b, prec);
1630 11048 : if (isintzero(b)) return cxcompotor(a, prec);
1631 11048 : a = cxcompotor(a, prec);
1632 11048 : b = cxcompotor(b, prec); return sqrtr(addrr(sqrr(a), sqrr(b)));
1633 : }
1634 0 : default: pari_err_TYPE("quicktofp",x);
1635 : return NULL;/*LCOV_EXCL_LINE*/
1636 : }
1637 :
1638 : }
1639 :
1640 : /* bound log_2 |largest root of p| (Fujiwara's bound) */
1641 : double
1642 827590 : fujiwara_bound(GEN p)
1643 : {
1644 827590 : pari_sp av = avma;
1645 827590 : long i, n = degpol(p);
1646 : GEN cc;
1647 : double loglc, Lmax;
1648 :
1649 827590 : if (n <= 0) pari_err_CONSTPOL("fujiwara_bound");
1650 827590 : loglc = mydbllog2r( quicktofp(gel(p,n+2)) ); /* log_2 |lc(p)| */
1651 827538 : cc = gel(p, 2);
1652 827538 : if (gequal0(cc))
1653 143148 : Lmax = -pariINFINITY-1;
1654 : else
1655 684426 : Lmax = (mydbllog2r(quicktofp(cc)) - loglc - 1) / n;
1656 3146518 : for (i = 1; i < n; i++)
1657 : {
1658 2318959 : GEN y = gel(p,i+2);
1659 : double L;
1660 2318959 : if (gequal0(y)) continue;
1661 1622151 : L = (mydbllog2r(quicktofp(y)) - loglc) / (n-i);
1662 1622159 : if (L > Lmax) Lmax = L;
1663 : }
1664 827559 : return gc_double(av, Lmax+1);
1665 : }
1666 :
1667 : /* Fujiwara's bound, real roots. Based on the following remark: if
1668 : * p = x^n + sum a_i x^i and q = x^n + sum min(a_i,0)x^i
1669 : * then for all x >= 0, p(x) >= q(x). Thus any bound for the (positive) roots
1670 : * of q is a bound for the positive roots of p. */
1671 : double
1672 267331 : fujiwara_bound_real(GEN p, long sign)
1673 : {
1674 267331 : pari_sp av = avma;
1675 : GEN x;
1676 267331 : long n = degpol(p), i, signodd, signeven;
1677 267332 : if (n <= 0) pari_err_CONSTPOL("fujiwara_bound");
1678 267332 : x = shallowcopy(p);
1679 267331 : if (gsigne(gel(x, n+2)) > 0)
1680 267309 : { signeven = 1; signodd = sign; }
1681 : else
1682 21 : { signeven = -1; signodd = -sign; }
1683 1231323 : for (i = 0; i < n; i++)
1684 : {
1685 963994 : if ((n - i) % 2)
1686 532976 : { if (gsigne(gel(x, i+2)) == signodd ) gel(x, i+2) = gen_0; }
1687 : else
1688 431018 : { if (gsigne(gel(x, i+2)) == signeven) gel(x, i+2) = gen_0; }
1689 : }
1690 267329 : return gc_double(av, fujiwara_bound(x));
1691 : }
1692 :
1693 : static GEN
1694 1226409 : mygprecrc_special(GEN x, long prec, long e)
1695 : {
1696 : GEN y;
1697 1226409 : switch(typ(x))
1698 : {
1699 32477 : case t_REAL:
1700 32477 : if (!signe(x)) return real_0_bit(minss(e, expo(x)));
1701 31605 : return (prec > realprec(x))? rtor(x, prec): x;
1702 11848 : case t_COMPLEX:
1703 11848 : y = cgetg(3,t_COMPLEX);
1704 11848 : gel(y,1) = mygprecrc_special(gel(x,1),prec,e);
1705 11848 : gel(y,2) = mygprecrc_special(gel(x,2),prec,e);
1706 11848 : return y;
1707 1182084 : default: return x;
1708 : }
1709 : }
1710 :
1711 : /* like mygprec but keep at least the same precision as before */
1712 : static GEN
1713 283253 : mygprec_special(GEN x, long bit)
1714 : {
1715 : long lx, i, e, prec;
1716 : GEN y;
1717 :
1718 283253 : if (bit < 0) bit = 0; /* should not happen */
1719 283253 : e = gexpo(x) - bit;
1720 283256 : prec = nbits2prec(bit);
1721 283252 : switch(typ(x))
1722 : {
1723 283252 : case t_POL:
1724 283252 : y = cgetg_copy(x, &lx); y[1] = x[1];
1725 1485964 : for (i=2; i<lx; i++) gel(y,i) = mygprecrc_special(gel(x,i),prec,e);
1726 283255 : break;
1727 :
1728 0 : default: y = mygprecrc_special(x,prec,e);
1729 : }
1730 283256 : return y;
1731 : }
1732 :
1733 : static GEN
1734 176855 : fix_roots1(GEN R)
1735 : {
1736 176855 : long i, l = lg(R);
1737 176855 : GEN v = cgetg(l, t_VEC);
1738 882373 : for (i=1; i < l; i++) { GEN r = gel(R,i); gel(v,i) = gcopy(r); gunclone(r); }
1739 176860 : return v;
1740 : }
1741 : static GEN
1742 283253 : fix_roots(GEN R, long h, long bit)
1743 : {
1744 : long i, j, c, n, prec;
1745 : GEN v, Z, gh;
1746 :
1747 283253 : if (h == 1) return fix_roots1(R);
1748 106398 : prec = nbits2prec(bit); Z = grootsof1(h, prec); gh = utoipos(h);
1749 106399 : n = lg(R)-1; v = cgetg(h*n + 1, t_VEC);
1750 320345 : for (c = i = 1; i <= n; i++)
1751 : {
1752 213944 : GEN s, r = gel(R,i);
1753 213944 : s = (h == 2)? gsqrt(r, prec): gsqrtn(r, gh, NULL, prec);
1754 662668 : for (j = 1; j <= h; j++) gel(v, c++) = gmul(s, gel(Z,j));
1755 213919 : gunclone(r);
1756 : }
1757 106401 : return v;
1758 : }
1759 :
1760 : static GEN
1761 282765 : all_roots(GEN p, long bit)
1762 : {
1763 282765 : long bit2, i, e, h, n = degpol(p), elc = gexpo(leading_coeff(p));
1764 282766 : GEN q, R, m, pd = RgX_deflate_max(p, &h);
1765 282766 : double fb = fujiwara_bound(pd);
1766 : pari_sp av;
1767 :
1768 282768 : if (fb < 0) fb = 0;
1769 282768 : bit2 = bit + maxss(gexpo(p), 0) + (long)ceil(log2(n / h) + 2 * fb);
1770 283259 : for (av = avma, i = 1, e = 0;; i++, set_avma(av))
1771 : {
1772 283260 : R = vectrunc_init(n+1);
1773 283260 : bit2 += e + (n << i);
1774 283260 : q = RgX_gtofp_bit(mygprec(pd,bit2), bit2);
1775 283255 : q[1] = evalsigne(1)|evalvarn(0);
1776 283255 : m = split_complete(q, bit2, R);
1777 283253 : R = fix_roots(R, h, bit2);
1778 283257 : q = mygprec_special(pd,bit2);
1779 283256 : q[1] = evalsigne(1)|evalvarn(0);
1780 283256 : e = gexpo(RgX_sub(q, m)) - elc + (long)log2((double)n) + 1;
1781 283255 : if (e < 0)
1782 : {
1783 283256 : if (e < -2*bit2) e = -2*bit2; /* avoid e = -oo */
1784 283256 : e = bit + a_posteriori_errors(p, R, e);
1785 283252 : if (e < 0) return R;
1786 : }
1787 490 : if (DEBUGLEVEL)
1788 0 : err_printf("all_roots: restarting, i = %ld, e = %ld\n", i,e);
1789 : }
1790 : }
1791 :
1792 : INLINE int
1793 56688 : isexactscalar(GEN x) { long tx = typ(x); return is_rational_t(tx); }
1794 :
1795 : static int
1796 19008 : isexactpol(GEN p)
1797 : {
1798 19008 : long i,n = degpol(p);
1799 68185 : for (i=0; i<=n; i++)
1800 56688 : if (!isexactscalar(gel(p,i+2))) return 0;
1801 11497 : return 1;
1802 : }
1803 :
1804 : /* p(0) != 0 [for efficiency] */
1805 : static GEN
1806 11497 : solve_exact_pol(GEN p, long bit)
1807 : {
1808 11497 : long i, j, k, m, n = degpol(p), iroots = 0;
1809 11497 : GEN ex, factors, v = zerovec(n);
1810 :
1811 11497 : factors = ZX_squff(Q_primpart(p), &ex);
1812 22994 : for (i=1; i<lg(factors); i++)
1813 : {
1814 11497 : GEN roots_fact = all_roots(gel(factors,i), bit);
1815 11497 : n = degpol(gel(factors,i));
1816 11497 : m = ex[i];
1817 48533 : for (j=1; j<=n; j++)
1818 74072 : for (k=1; k<=m; k++) v[++iroots] = roots_fact[j];
1819 : }
1820 11497 : return v;
1821 : }
1822 :
1823 : /* return the roots of p with absolute error bit */
1824 : static GEN
1825 19008 : roots_com(GEN q, long bit)
1826 : {
1827 : GEN L, p;
1828 19008 : long v = RgX_valrem_inexact(q, &p);
1829 19008 : int ex = isexactpol(p);
1830 19008 : if (!ex) p = RgX_normalize1(p);
1831 19008 : if (lg(p) == 3)
1832 0 : L = cgetg(1,t_VEC); /* constant polynomial */
1833 : else
1834 19008 : L = ex? solve_exact_pol(p,bit): all_roots(p,bit);
1835 19008 : if (v)
1836 : {
1837 210 : GEN M, z, t = gel(q,2);
1838 : long i, x, y, l, n;
1839 :
1840 210 : if (isrationalzero(t)) x = -bit;
1841 : else
1842 : {
1843 7 : n = gexpo(t);
1844 7 : x = n / v; l = degpol(q);
1845 35 : for (i = v; i <= l; i++)
1846 : {
1847 28 : t = gel(q,i+2);
1848 28 : if (isrationalzero(t)) continue;
1849 28 : y = (n - gexpo(t)) / i;
1850 28 : if (y < x) x = y;
1851 : }
1852 : }
1853 210 : z = real_0_bit(x); l = v + lg(L);
1854 210 : M = cgetg(l, t_VEC); L -= v;
1855 483 : for (i = 1; i <= v; i++) gel(M,i) = z;
1856 651 : for ( ; i < l; i++) gel(M,i) = gel(L,i);
1857 210 : L = M;
1858 : }
1859 19008 : return L;
1860 : }
1861 :
1862 : static GEN
1863 901252 : tocomplex(GEN x, long l, long bit)
1864 : {
1865 : GEN y;
1866 901252 : if (typ(x) == t_COMPLEX)
1867 : {
1868 882466 : if (signe(gel(x,1))) return mygprecrc(x, l, -bit);
1869 130606 : x = gel(x,2);
1870 130606 : y = cgetg(3,t_COMPLEX);
1871 130614 : gel(y,1) = real_0_bit(-bit);
1872 130614 : gel(y,2) = mygprecrc(x, l, -bit);
1873 : }
1874 : else
1875 : {
1876 18786 : y = cgetg(3,t_COMPLEX);
1877 18788 : gel(y,1) = mygprecrc(x, l, -bit);
1878 18788 : gel(y,2) = real_0_bit(-bit);
1879 : }
1880 149401 : return y;
1881 : }
1882 :
1883 : /* x,y are t_COMPLEX of t_REALs or t_REAL, compare wrt |Im x| - |Im y|,
1884 : * then Re x - Re y, up to 2^-e absolute error */
1885 : static int
1886 1579314 : cmp_complex_appr(void *E, GEN x, GEN y)
1887 : {
1888 1579314 : long e = (long)E;
1889 : GEN z, xi, yi, xr, yr;
1890 : long sz, sxi, syi;
1891 1579314 : if (typ(x) == t_COMPLEX) { xr = gel(x,1); xi = gel(x,2); sxi = signe(xi); }
1892 346821 : else { xr = x; xi = NULL; sxi = 0; }
1893 1579314 : if (typ(y) == t_COMPLEX) { yr = gel(y,1); yi = gel(y,2); syi = signe(yi); }
1894 268640 : else { yr = y; yi = NULL; syi = 0; }
1895 : /* Compare absolute values of imaginary parts */
1896 1579314 : if (!sxi)
1897 : {
1898 365459 : if (syi && expo(yi) >= e) return -1;
1899 : /* |Im x| ~ |Im y| ~ 0 */
1900 : }
1901 1213855 : else if (!syi)
1902 : {
1903 41016 : if (sxi && expo(xi) >= e) return 1;
1904 : /* |Im x| ~ |Im y| ~ 0 */
1905 : }
1906 : else
1907 : {
1908 1172839 : z = addrr_sign(xi, 1, yi, -1); sz = signe(z);
1909 1172725 : if (sz && expo(z) >= e) return (int)sz;
1910 : }
1911 : /* |Im x| ~ |Im y|, sort according to real parts */
1912 898200 : z = subrr(xr, yr); sz = signe(z);
1913 898257 : if (sz && expo(z) >= e) return (int)sz;
1914 : /* Re x ~ Re y. Place negative imaginary part before positive */
1915 436480 : return (int) (sxi - syi);
1916 : }
1917 :
1918 : static GEN
1919 283645 : clean_roots(GEN L, long l, long bit, long clean)
1920 : {
1921 283645 : long i, n = lg(L), ex = 5 - bit;
1922 283645 : GEN res = cgetg(n,t_COL);
1923 1436715 : for (i=1; i<n; i++)
1924 : {
1925 1153073 : GEN c = gel(L,i);
1926 1153073 : if (clean && isrealappr(c,ex))
1927 : {
1928 251815 : if (typ(c) == t_COMPLEX) c = gel(c,1);
1929 251815 : c = mygprecrc(c, l, -bit);
1930 : }
1931 : else
1932 901253 : c = tocomplex(c, l, bit);
1933 1153067 : gel(res,i) = c;
1934 : }
1935 283642 : gen_sort_inplace(res, (void*)ex, &cmp_complex_appr, NULL);
1936 283642 : return res;
1937 : }
1938 :
1939 : /* the vector of roots of p, with absolute error 2^(- prec2nbits(l)) */
1940 : static GEN
1941 19036 : roots_aux(GEN p, long l, long clean)
1942 : {
1943 19036 : pari_sp av = avma;
1944 : long bit;
1945 : GEN L;
1946 :
1947 19036 : if (typ(p) != t_POL)
1948 : {
1949 21 : if (gequal0(p)) pari_err_ROOTS0("roots");
1950 14 : if (!isvalidcoeff(p)) pari_err_TYPE("roots",p);
1951 7 : return cgetg(1,t_COL); /* constant polynomial */
1952 : }
1953 19015 : if (!signe(p)) pari_err_ROOTS0("roots");
1954 19015 : checkvalidpol(p,"roots");
1955 19008 : if (lg(p) == 3) return cgetg(1,t_COL); /* constant polynomial */
1956 19008 : if (l < LOWDEFAULTPREC) l = LOWDEFAULTPREC;
1957 19008 : bit = prec2nbits(l);
1958 19008 : L = roots_com(p, bit);
1959 19008 : return gerepilecopy(av, clean_roots(L, l, bit, clean));
1960 : }
1961 : GEN
1962 7820 : roots(GEN p, long l) { return roots_aux(p,l, 0); }
1963 : /* clean up roots. If root is real replace it by its real part */
1964 : GEN
1965 11216 : cleanroots(GEN p, long l) { return roots_aux(p,l, 1); }
1966 :
1967 : /* private variant of conjvec. Allow non rational coefficients, shallow
1968 : * function. */
1969 : GEN
1970 84 : polmod_to_embed(GEN x, long prec)
1971 : {
1972 84 : GEN v, T = gel(x,1), A = gel(x,2);
1973 : long i, l;
1974 84 : if (typ(A) != t_POL || varn(A) != varn(T))
1975 : {
1976 7 : checkvalidpol(T,"polmod_to_embed");
1977 7 : return const_col(degpol(T), A);
1978 : }
1979 77 : v = cleanroots(T,prec); l = lg(v);
1980 231 : for (i=1; i<l; i++) gel(v,i) = poleval(A,gel(v,i));
1981 77 : return v;
1982 : }
1983 :
1984 : GEN
1985 264646 : QX_complex_roots(GEN p, long l)
1986 : {
1987 264646 : pari_sp av = avma;
1988 : long bit, v;
1989 : GEN L;
1990 :
1991 264646 : if (!signe(p)) pari_err_ROOTS0("QX_complex_roots");
1992 264646 : if (lg(p) == 3) return cgetg(1,t_COL); /* constant polynomial */
1993 264646 : if (l < LOWDEFAULTPREC) l = LOWDEFAULTPREC;
1994 264646 : bit = prec2nbits(l);
1995 264645 : v = RgX_valrem(p, &p);
1996 264644 : L = lg(p) > 3? all_roots(Q_primpart(p), bit): cgetg(1,t_COL);
1997 264639 : if (v) L = shallowconcat(const_vec(v, real_0_bit(-bit)), L);
1998 264639 : return gerepilecopy(av, clean_roots(L, l, bit, 1));
1999 : }
2000 :
2001 : /********************************************************************/
2002 : /** **/
2003 : /** REAL ROOTS OF INTEGER POLYNOMIAL **/
2004 : /** **/
2005 : /********************************************************************/
2006 :
2007 : /* Count sign changes in the coefficients of (x+1)^deg(P)*P(1/(x+1)), P
2008 : * has no rational root. The inversion is implicit (we take coefficients
2009 : * backwards). */
2010 : static long
2011 1251146 : X2XP1(GEN P, GEN *Premapped)
2012 : {
2013 1251146 : const pari_sp av = avma;
2014 1251146 : GEN v = shallowcopy(P);
2015 1251149 : long i, j, nb, s, dP = degpol(P), vlim = dP+2;
2016 :
2017 8070352 : for (j = 2; j < vlim; j++) gel(v, j+1) = addii(gel(v, j), gel(v, j+1));
2018 1250949 : s = -signe(gel(v, vlim));
2019 1250949 : vlim--; nb = 0;
2020 3737096 : for (i = 1; i < dP; i++)
2021 : {
2022 3336479 : long s2 = -signe(gel(v, 2));
2023 3336479 : int flag = (s2 == s);
2024 27577875 : for (j = 2; j < vlim; j++)
2025 : {
2026 24241775 : gel(v, j+1) = addii(gel(v, j), gel(v, j+1));
2027 24241396 : if (flag) flag = (s2 != signe(gel(v, j+1)));
2028 : }
2029 3336100 : if (s == signe(gel(v, vlim)))
2030 : {
2031 1061611 : if (++nb >= 2) return gc_long(av,2);
2032 722375 : s = -s;
2033 : }
2034 : /* if flag is set there will be no further sign changes */
2035 2996864 : if (flag && (!Premapped || !nb)) return gc_long(av, nb);
2036 2485666 : vlim--;
2037 2485666 : if (gc_needed(av, 3))
2038 : {
2039 0 : if (DEBUGMEM>1) pari_warn(warnmem, "X2XP1, i = %ld/%ld", i, dP-1);
2040 0 : if (!Premapped) setlg(v, vlim + 2);
2041 0 : v = gerepilecopy(av, v);
2042 : }
2043 : }
2044 400617 : if (vlim >= 2 && s == signe(gel(v, vlim))) nb++;
2045 400617 : if (Premapped && nb == 1) *Premapped = v; else set_avma(av);
2046 400650 : return nb;
2047 : }
2048 :
2049 : static long
2050 0 : _intervalcmp(GEN x, GEN y)
2051 : {
2052 0 : if (typ(x) == t_VEC) x = gel(x, 1);
2053 0 : if (typ(y) == t_VEC) y = gel(y, 1);
2054 0 : return gcmp(x, y);
2055 : }
2056 :
2057 : static GEN
2058 10421106 : _gen_nored(void *E, GEN x) { (void)E; return x; }
2059 : static GEN
2060 23000006 : _mp_add(void *E, GEN x, GEN y) { (void)E; return mpadd(x, y); }
2061 : static GEN
2062 0 : _mp_sub(void *E, GEN x, GEN y) { (void)E; return mpsub(x, y); }
2063 : static GEN
2064 4127323 : _mp_mul(void *E, GEN x, GEN y) { (void)E; return mpmul(x, y); }
2065 : static GEN
2066 5965543 : _mp_sqr(void *E, GEN x) { (void)E; return mpsqr(x); }
2067 : static GEN
2068 13526788 : _gen_one(void *E) { (void)E; return gen_1; }
2069 : static GEN
2070 278896 : _gen_zero(void *E) { (void)E; return gen_0; }
2071 :
2072 : static struct bb_algebra mp_algebra = { _gen_nored, _mp_add, _mp_sub,
2073 : _mp_mul, _mp_sqr, _gen_one, _gen_zero };
2074 :
2075 : static GEN
2076 32399929 : _mp_cmul(void *E, GEN P, long a, GEN x) {(void)E; return mpmul(gel(P,a+2), x);}
2077 :
2078 : /* Split the polynom P in two parts, whose coeffs have constant sign:
2079 : * P(X) = X^D*Pp + Pm. Also compute the two parts of the derivative of P,
2080 : * Pprimem = Pm', Pprimep = X*Pp'+ D*Pp => P' = X^(D-1)*Pprimep + Pprimem;
2081 : * Pprimep[i] = (i+D) Pp[i]. Return D */
2082 : static long
2083 163094 : split_pols(GEN P, GEN *pPp, GEN *pPm, GEN *pPprimep, GEN *pPprimem)
2084 : {
2085 163094 : long i, D, dP = degpol(P), s0 = signe(gel(P,2));
2086 : GEN Pp, Pm, Pprimep, Pprimem;
2087 503030 : for(i=1; i <= dP; i++)
2088 503032 : if (signe(gel(P, i+2)) == -s0) break;
2089 163094 : D = i;
2090 163094 : Pm = cgetg(D + 2, t_POL);
2091 163096 : Pprimem = cgetg(D + 1, t_POL);
2092 163098 : Pp = cgetg(dP-D + 3, t_POL);
2093 163099 : Pprimep = cgetg(dP-D + 3, t_POL);
2094 163100 : Pm[1] = Pp[1] = Pprimem[1] = Pprimep[1] = P[1];
2095 666098 : for(i=0; i < D; i++)
2096 : {
2097 503007 : GEN c = gel(P, i+2);
2098 503007 : gel(Pm, i+2) = c;
2099 503007 : if (i) gel(Pprimem, i+1) = mului(i, c);
2100 : }
2101 680686 : for(; i <= dP; i++)
2102 : {
2103 517603 : GEN c = gel(P, i+2);
2104 517603 : gel(Pp, i+2-D) = c;
2105 517603 : gel(Pprimep, i+2-D) = mului(i, c);
2106 : }
2107 163083 : *pPm = normalizepol_lg(Pm, D+2);
2108 163091 : *pPprimem = normalizepol_lg(Pprimem, D+1);
2109 163096 : *pPp = normalizepol_lg(Pp, dP-D+3);
2110 163099 : *pPprimep = normalizepol_lg(Pprimep, dP-D+3);
2111 163096 : return dP - degpol(*pPp);
2112 : }
2113 :
2114 : static GEN
2115 4860014 : bkeval_single_power(long d, GEN V)
2116 : {
2117 4860014 : long mp = lg(V) - 2;
2118 4860014 : if (d > mp) return gmul(gpowgs(gel(V, mp+1), d/mp), gel(V, (d%mp)+1));
2119 4860014 : return gel(V, d+1);
2120 : }
2121 :
2122 : static GEN
2123 4858498 : splitpoleval(GEN Pp, GEN Pm, GEN pows, long D, long bitprec)
2124 : {
2125 4858498 : GEN vp = gen_bkeval_powers(Pp, degpol(Pp), pows, NULL, &mp_algebra, _mp_cmul);
2126 4860062 : GEN vm = gen_bkeval_powers(Pm, degpol(Pm), pows, NULL, &mp_algebra, _mp_cmul);
2127 4860085 : GEN xa = bkeval_single_power(D, pows);
2128 : GEN r;
2129 4859909 : if (!signe(vp)) return vm;
2130 4859909 : vp = gmul(vp, xa);
2131 4856415 : r = gadd(vp, vm);
2132 4854336 : if (gexpo(vp) - (signe(r)? gexpo(r): 0) > prec2nbits(realprec(vp)) - bitprec)
2133 204953 : return NULL;
2134 4654409 : return r;
2135 : }
2136 :
2137 : /* optimized Cauchy bound for P = X^D*Pp + Pm, D > deg(Pm) */
2138 : static GEN
2139 163094 : splitcauchy(GEN Pp, GEN Pm, long prec)
2140 : {
2141 163094 : GEN S = gel(Pp,2), A = gel(Pm,2);
2142 163094 : long i, lPm = lg(Pm), lPp = lg(Pp);
2143 500428 : for (i=3; i < lPm; i++) { GEN c = gel(Pm,i); if (abscmpii(A, c) < 0) A = c; }
2144 517639 : for (i=3; i < lPp; i++) S = addii(S, gel(Pp, i));
2145 163081 : return subsr(1, rdivii(A, S, prec)); /* 1 + |Pm|_oo / |Pp|_1 */
2146 : }
2147 :
2148 : static GEN
2149 13797 : ZX_deg1root(GEN P, long prec)
2150 : {
2151 13797 : GEN a = gel(P,3), b = gel(P,2);
2152 13797 : if (is_pm1(a))
2153 : {
2154 13797 : b = itor(b, prec); if (signe(a) > 0) togglesign(b);
2155 13797 : return b;
2156 : }
2157 0 : return rdivii(negi(b), a, prec);
2158 : }
2159 :
2160 : /* Newton for polynom P, P(0)!=0, with unique sign change => one root in ]0,oo[
2161 : * P' has also at most one zero there */
2162 : static GEN
2163 163099 : polsolve(GEN P, long bitprec)
2164 : {
2165 : pari_sp av;
2166 : GEN Pp, Pm, Pprimep, Pprimem, Pprime, Pprime2, ra, rb, rc, Pc;
2167 163099 : long dP = degpol(P), prec = nbits2prec(bitprec);
2168 : long expoold, iter, D, rt, s0, bitaddprec, cprec, PREC;
2169 :
2170 163098 : if (dP == 1) return ZX_deg1root(P, prec);
2171 163098 : Pprime = ZX_deriv(P);
2172 163083 : Pprime2 = ZX_deriv(Pprime);
2173 163081 : bitaddprec = 1 + 2*expu(dP); PREC = prec + nbits2prec(bitaddprec);
2174 163092 : D = split_pols(P, &Pp, &Pm, &Pprimep, &Pprimem); /* P = X^D*Pp + Pm */
2175 163095 : s0 = signe(gel(P, 2));
2176 163095 : rt = maxss(D, brent_kung_optpow(maxss(degpol(Pp), degpol(Pm)), 2, 1));
2177 163094 : rb = splitcauchy(Pp, Pm, DEFAULTPREC);
2178 163088 : for (cprec = DEFAULTPREC, expoold = LONG_MAX;;)
2179 0 : {
2180 163088 : GEN pows = gen_powers(rb, rt, 1, NULL, _mp_sqr, _mp_mul, _gen_one);
2181 163098 : Pc = splitpoleval(Pp, Pm, pows, D, bitaddprec);
2182 163098 : if (!Pc) { cprec += EXTRAPRECWORD; rb = rtor(rb, cprec); continue; }
2183 163098 : if (signe(Pc) != s0) break;
2184 0 : shiftr_inplace(rb,1);
2185 : }
2186 163098 : for (iter = 0, ra = NULL;;)
2187 1791555 : {
2188 : GEN wdth;
2189 1954653 : iter++;
2190 1954653 : if (ra)
2191 886563 : rc = shiftr(addrr(ra, rb), -1);
2192 : else
2193 1068090 : rc = shiftr(rb, -1);
2194 : for(;;)
2195 0 : {
2196 1955024 : GEN pows = gen_powers(rc, rt, 1, NULL, _mp_sqr, _mp_mul, _gen_one);
2197 1954452 : Pc = splitpoleval(Pp, Pm, pows, D, bitaddprec+2);
2198 1954674 : if (Pc) break;
2199 0 : cprec += EXTRAPRECWORD;
2200 0 : rc = rtor(rc, cprec);
2201 : }
2202 1954674 : if (signe(Pc) == s0)
2203 583046 : ra = rc;
2204 : else
2205 1371628 : rb = rc;
2206 1954674 : if (!ra) continue;
2207 1049660 : wdth = subrr(rb, ra);
2208 1049485 : if (!(iter % 8))
2209 : {
2210 164245 : GEN m1 = poleval(Pprime, ra), M2;
2211 164249 : if (signe(m1) == s0) continue;
2212 163150 : M2 = poleval(Pprime2, rb);
2213 163144 : if (abscmprr(gmul(M2, wdth), shiftr(m1, 1)) > 0) continue;
2214 159959 : break;
2215 : }
2216 885240 : else if (gexpo(wdth) <= -bitprec)
2217 3169 : break;
2218 : }
2219 163128 : rc = rb; av = avma;
2220 1209755 : for(;; rc = gerepileuptoleaf(av, rc))
2221 1209797 : {
2222 : long exponew;
2223 1372925 : GEN Ppc, dist, rcold = rc;
2224 1372925 : GEN pows = gen_powers(rc, rt, 1, NULL, _mp_sqr, _mp_mul, _gen_one);
2225 1372280 : Ppc = splitpoleval(Pprimep, Pprimem, pows, D-1, bitaddprec+4);
2226 1372474 : if (Ppc) Pc = splitpoleval(Pp, Pm, pows, D, bitaddprec+4);
2227 1372619 : if (!Ppc || !Pc)
2228 : {
2229 204958 : if (cprec >= PREC)
2230 24715 : cprec += EXTRAPRECWORD;
2231 : else
2232 180243 : cprec = minss(2*cprec, PREC);
2233 204959 : rc = rtor(rc, cprec); continue; /* backtrack one step */
2234 : }
2235 1167661 : dist = typ(Ppc) == t_REAL? divrr(Pc, Ppc): divri(Pc, Ppc);
2236 1167378 : rc = subrr(rc, dist);
2237 1167157 : if (cmprr(ra, rc) > 0 || cmprr(rb, rc) < 0)
2238 : {
2239 0 : if (cprec >= PREC) break;
2240 0 : cprec = minss(2*cprec, PREC);
2241 0 : rc = rtor(rcold, cprec); continue; /* backtrack one step */
2242 : }
2243 1167893 : if (expoold == LONG_MAX) { expoold = expo(dist); continue; }
2244 975007 : exponew = expo(dist);
2245 975007 : if (exponew < -bitprec - 1)
2246 : {
2247 279008 : if (cprec >= PREC) break;
2248 115909 : cprec = minss(2*cprec, PREC);
2249 115910 : rc = rtor(rc, cprec); continue;
2250 : }
2251 695999 : if (exponew > expoold - 2)
2252 : {
2253 29785 : if (cprec >= PREC) break;
2254 29785 : expoold = LONG_MAX;
2255 29785 : cprec = minss(2*cprec, PREC);
2256 29785 : rc = rtor(rc, cprec); continue;
2257 : }
2258 666214 : expoold = exponew;
2259 : }
2260 163099 : return rtor(rc, prec);
2261 : }
2262 :
2263 : /* Return primpart(P(x / 2)) */
2264 : static GEN
2265 406607 : ZX_rescale2prim(GEN P)
2266 : {
2267 406607 : long i, l = lg(P), v, n;
2268 : GEN Q;
2269 406607 : if (l==2) return pol_0(varn(P));
2270 406607 : Q = cgetg(l,t_POL); v = vali(gel(P,l-1));
2271 1362562 : for (i = l-2, n = 1; v > n && i >= 2; i--, n++)
2272 955940 : v = minss(v, vali(gel(P,i)) + n);
2273 406622 : gel(Q,l-1) = v? shifti(gel(P,l-1), -v): gel(P,l-1);
2274 2640858 : for (i = l-2, n = 1-v; i >= 2; i--, n++)
2275 2234266 : gel(Q,i) = shifti(gel(P,i), n);
2276 406592 : Q[1] = P[1]; return Q;
2277 : }
2278 :
2279 : /* assume Q0 has no rational root */
2280 : static GEN
2281 228227 : usp(GEN Q0, long flag, long bitprec)
2282 : {
2283 228227 : const pari_sp av = avma;
2284 : GEN Qremapped, Q, c, Lc, Lk, sol;
2285 228227 : GEN *pQremapped = flag == 1? &Qremapped: NULL;
2286 228227 : const long prec = nbits2prec(bitprec), deg = degpol(Q0);
2287 228231 : long listsize = 64, nbr = 0, nb_todo, ind, indf, i, k, nb;
2288 :
2289 228231 : sol = zerocol(deg);
2290 228232 : Lc = zerovec(listsize);
2291 228237 : Lk = cgetg(listsize+1, t_VECSMALL);
2292 228237 : k = Lk[1] = 0;
2293 228237 : ind = 1; indf = 2;
2294 228237 : Q = Q0;
2295 228237 : c = gen_0;
2296 228237 : nb_todo = 1;
2297 1479288 : while (nb_todo)
2298 : {
2299 1251053 : GEN nc = gel(Lc, ind);
2300 : pari_sp av2;
2301 1251053 : if (Lk[ind] == k + 1)
2302 : {
2303 406605 : Q = Q0 = ZX_rescale2prim(Q0);
2304 406601 : c = gen_0;
2305 : }
2306 1251049 : if (!equalii(nc, c)) Q = ZX_translate(Q, subii(nc, c));
2307 1251111 : av2 = avma;
2308 1251111 : k = Lk[ind];
2309 1251111 : ind++;
2310 1251111 : c = nc;
2311 1251111 : nb_todo--;
2312 1251111 : nb = X2XP1(Q, pQremapped);
2313 :
2314 1251036 : if (nb == 1)
2315 : { /* exactly one root */
2316 277164 : GEN s = gen_0;
2317 277164 : if (flag == 0)
2318 : {
2319 0 : s = mkvec2(gmul2n(c,-k), gmul2n(addiu(c,1),-k));
2320 0 : s = gerepilecopy(av2, s);
2321 : }
2322 277164 : else if (flag == 1) /* Caveat: Qremapped is the reciprocal polynomial */
2323 : {
2324 163097 : s = polsolve(*pQremapped, bitprec+1);
2325 163100 : s = addir(c, divrr(s, addsr(1, s)));
2326 163094 : shiftr_inplace(s, -k);
2327 163096 : if (realprec(s) != prec) s = rtor(s, prec);
2328 163101 : s = gerepileupto(av2, s);
2329 : }
2330 114067 : else set_avma(av2);
2331 277171 : gel(sol, ++nbr) = s;
2332 : }
2333 973872 : else if (nb)
2334 : { /* unknown, add two nodes to refine */
2335 511460 : if (indf + 2 > listsize)
2336 : {
2337 760 : if (ind>1)
2338 : {
2339 3207 : for (i = ind; i < indf; i++)
2340 : {
2341 2447 : gel(Lc, i-ind+1) = gel(Lc, i);
2342 2447 : Lk[i-ind+1] = Lk[i];
2343 : }
2344 760 : indf -= ind-1;
2345 760 : ind = 1;
2346 : }
2347 760 : if (indf + 2 > listsize)
2348 : {
2349 0 : listsize *= 2;
2350 0 : Lc = vec_lengthen(Lc, listsize);
2351 0 : Lk = vecsmall_lengthen(Lk, listsize);
2352 : }
2353 46953 : for (i = indf; i <= listsize; i++) gel(Lc, i) = gen_0;
2354 : }
2355 511460 : gel(Lc, indf) = nc = shifti(c, 1);
2356 511458 : gel(Lc, indf + 1) = addiu(nc, 1);
2357 511446 : Lk[indf] = Lk[indf + 1] = k + 1;
2358 511446 : indf += 2;
2359 511446 : nb_todo += 2;
2360 : }
2361 1251029 : if (gc_needed(av, 2))
2362 : {
2363 0 : gerepileall(av, 6, &Q0, &Q, &c, &Lc, &Lk, &sol);
2364 0 : if (DEBUGMEM > 1) pari_warn(warnmem, "ZX_Uspensky", avma);
2365 : }
2366 : }
2367 228235 : setlg(sol, nbr+1);
2368 228236 : return gerepilecopy(av, sol);
2369 : }
2370 :
2371 : static GEN
2372 14 : ZX_Uspensky_equal_yes(GEN a, long flag, long bit)
2373 : {
2374 14 : if (flag == 2) return gen_1;
2375 7 : if (flag == 1 && typ(a) != t_REAL)
2376 : {
2377 7 : if (typ(a) == t_INT && !signe(a))
2378 0 : a = real_0_bit(bit);
2379 : else
2380 7 : a = gtofp(a, nbits2prec(bit));
2381 : }
2382 7 : return mkcol(a);
2383 : }
2384 : static GEN
2385 21 : ZX_Uspensky_no(long flag)
2386 21 : { return flag <= 1 ? cgetg(1, t_COL) : gen_0; }
2387 : /* ZX_Uspensky(P, [a,a], flag) */
2388 : static GEN
2389 28 : ZX_Uspensky_equal(GEN P, GEN a, long flag, long bit)
2390 : {
2391 28 : if (typ(a) != t_INFINITY && gequal0(poleval(P, a)))
2392 14 : return ZX_Uspensky_equal_yes(a, flag, bit);
2393 : else
2394 14 : return ZX_Uspensky_no(flag);
2395 : }
2396 : static int
2397 3357 : sol_ok(GEN r, GEN a, GEN b) { return gcmp(a, r) <= 0 && gcmp(r, b) <= 0; }
2398 :
2399 : /* P a ZX without real double roots; better if primitive and squarefree but
2400 : * caller should ensure that. If flag & 4 assume that P has no rational root
2401 : * (modest speedup) */
2402 : GEN
2403 180693 : ZX_Uspensky(GEN P, GEN ab, long flag, long bitprec)
2404 : {
2405 180693 : pari_sp av = avma;
2406 : GEN a, b, res, sol;
2407 : double fb;
2408 : long l, nbz, deg;
2409 :
2410 180693 : if (ab)
2411 : {
2412 79772 : if (typ(ab) == t_VEC)
2413 : {
2414 52997 : if (lg(ab) != 3) pari_err_DIM("ZX_Uspensky");
2415 52997 : a = gel(ab, 1);
2416 52997 : b = gel(ab, 2);
2417 : }
2418 : else
2419 : {
2420 26775 : a = ab;
2421 26775 : b = mkoo();
2422 : }
2423 : }
2424 : else
2425 : {
2426 100921 : a = mkmoo();
2427 100921 : b = mkoo();
2428 : }
2429 180693 : if (flag & 4)
2430 : {
2431 126387 : if (gcmp(a, b) >= 0) { set_avma(av); return ZX_Uspensky_no(flag); }
2432 126386 : flag &= ~4;
2433 126386 : sol = cgetg(1, t_COL);
2434 : }
2435 : else
2436 : {
2437 54306 : switch (gcmp(a, b))
2438 : {
2439 7 : case 1: set_avma(av); return ZX_Uspensky_no(flag);
2440 28 : case 0: return gerepilecopy(av, ZX_Uspensky_equal(P, a, flag, bitprec));
2441 : }
2442 54271 : sol = nfrootsQ(P);
2443 : }
2444 180657 : nbz = 0; l = lg(sol);
2445 180657 : if (l > 1)
2446 : {
2447 : long i, j;
2448 2685 : P = RgX_div(P, roots_to_pol(sol, varn(P)));
2449 2685 : if (!RgV_is_ZV(sol)) P = Q_primpart(P);
2450 6042 : for (i = j = 1; i < l; i++)
2451 3357 : if (sol_ok(gel(sol,i), a, b)) gel(sol,j++) = gel(sol,i);
2452 2685 : setlg(sol, j);
2453 2685 : if (flag == 2) { nbz = j-1; sol = utoi(nbz); }
2454 2517 : else if (flag == 1) sol = RgC_gtofp(sol, nbits2prec(bitprec));
2455 : }
2456 177972 : else if (flag == 2) sol = gen_0;
2457 180657 : deg = degpol(P);
2458 180657 : if (deg == 0) return gerepilecopy(av, sol);
2459 178710 : if (typ(a) == t_INFINITY && typ(b) != t_INFINITY && gsigne(b))
2460 : {
2461 28 : fb = fujiwara_bound_real(P, -1);
2462 28 : if (fb <= -pariINFINITY) a = gen_0;
2463 21 : else if (fb < 0) a = gen_m1;
2464 21 : else a = negi(int2n((long)ceil(fb)));
2465 : }
2466 178710 : if (typ(b) == t_INFINITY && typ(a) != t_INFINITY && gsigne(a))
2467 : {
2468 21 : fb = fujiwara_bound_real(P, 1);
2469 21 : if (fb <= -pariINFINITY) b = gen_0;
2470 21 : else if (fb < 0) b = gen_1;
2471 7 : else b = int2n((long)ceil(fb));
2472 : }
2473 178710 : if (typ(a) != t_INFINITY && typ(b) != t_INFINITY)
2474 : {
2475 : GEN d, ad, bd, diff;
2476 : long i;
2477 : /* can occur if one of a,b was initially a t_INFINITY */
2478 12271 : if (gequal(a,b)) return gerepilecopy(av, sol);
2479 12257 : d = lcmii(Q_denom(a), Q_denom(b));
2480 12257 : if (is_pm1(d)) { d = NULL; ad = a; bd = b; }
2481 : else
2482 21 : { P = ZX_rescale(P, d); ad = gmul(a, d); bd = gmul(b, d); }
2483 12257 : diff = subii(bd, ad);
2484 12257 : P = ZX_affine(P, diff, ad);
2485 12257 : res = usp(P, flag, bitprec);
2486 12257 : if (flag <= 1)
2487 : {
2488 33635 : for (i = 1; i < lg(res); i++)
2489 : {
2490 21595 : GEN z = gmul(diff, gel(res, i));
2491 21595 : if (typ(z) == t_VEC)
2492 : {
2493 0 : gel(z, 1) = gadd(ad, gel(z, 1));
2494 0 : gel(z, 2) = gadd(ad, gel(z, 2));
2495 : }
2496 : else
2497 21595 : z = gadd(ad, z);
2498 21595 : if (d) z = gdiv(z, d);
2499 21595 : gel(res, i) = z;
2500 : }
2501 12040 : sol = shallowconcat(sol, res);
2502 : }
2503 : else
2504 217 : nbz += lg(res) - 1;
2505 : }
2506 178696 : if (typ(b) == t_INFINITY && (fb=fujiwara_bound_real(P, 1)) > -pariINFINITY)
2507 : {
2508 130931 : long bp = maxss((long)ceil(fb), 0);
2509 130930 : res = usp(ZX_unscale2n(P, bp), flag, bitprec);
2510 130931 : if (flag <= 1)
2511 70143 : sol = shallowconcat(sol, gmul2n(res, bp));
2512 : else
2513 60788 : nbz += lg(res)-1;
2514 : }
2515 178698 : if (typ(a) == t_INFINITY && (fb=fujiwara_bound_real(P,-1)) > -pariINFINITY)
2516 : {
2517 85050 : long i, bm = maxss((long)ceil(fb), 0);
2518 85050 : res = usp(ZX_unscale2n(ZX_z_unscale(P, -1), bm), flag, bitprec);
2519 85053 : if (flag <= 1)
2520 : {
2521 73622 : for (i = 1; i < lg(res); i++)
2522 : {
2523 46164 : GEN z = gneg(gmul2n(gel(res, i), bm));
2524 46164 : if (typ(z) == t_VEC) swap(gel(z, 1), gel(z, 2));
2525 46164 : gel(res, i) = z;
2526 : }
2527 27458 : sol = shallowconcat(res, sol);
2528 : }
2529 : else
2530 57595 : nbz += lg(res)-1;
2531 : }
2532 178699 : if (flag >= 2) return utoi(nbz);
2533 82268 : if (flag)
2534 82268 : sol = sort(sol);
2535 : else
2536 0 : sol = gen_sort(sol, (void *)_intervalcmp, cmp_nodata);
2537 82268 : return gerepileupto(av, sol);
2538 : }
2539 :
2540 : /* x a scalar */
2541 : static GEN
2542 35 : rootsdeg0(GEN x)
2543 : {
2544 35 : if (!is_real_t(typ(x))) pari_err_TYPE("realroots",x);
2545 28 : if (gequal0(x)) pari_err_ROOTS0("realroots");
2546 14 : return cgetg(1,t_COL); /* constant polynomial */
2547 : }
2548 : static void
2549 104928 : checkbound(GEN a)
2550 : {
2551 104928 : switch(typ(a))
2552 : {
2553 104928 : case t_INT: case t_FRAC: case t_INFINITY: break;
2554 0 : default: pari_err_TYPE("polrealroots", a);
2555 : }
2556 104928 : }
2557 : static GEN
2558 53255 : check_ab(GEN ab)
2559 : {
2560 : GEN a, b;
2561 53255 : if (!ab) return NULL;
2562 52464 : if (typ(ab) != t_VEC || lg(ab) != 3) pari_err_TYPE("polrootsreal",ab);
2563 52464 : a = gel(ab,1); checkbound(a);
2564 52464 : b = gel(ab,2); checkbound(b);
2565 52464 : if (typ(a) == t_INFINITY && inf_get_sign(a) < 0 &&
2566 1071 : typ(b) == t_INFINITY && inf_get_sign(b) > 0) ab = NULL;
2567 52464 : return ab;
2568 : }
2569 : /* e^(1/h) assuming the h-th root is real, beware that sqrtnr assumes e >= 0 */
2570 : static GEN
2571 21326 : _sqrtnr(GEN e, long h)
2572 : {
2573 : long s;
2574 : GEN r;
2575 21326 : if (h == 2) return sqrtr(e);
2576 14 : s = signe(e); setsigne(e, 1); /* e < 0 is possible, implies h is odd */
2577 14 : r = sqrtnr(e, h); if (s < 0) setsigne(r, -1);
2578 14 : return r;
2579 : }
2580 : GEN
2581 50281 : realroots(GEN P, GEN ab, long prec)
2582 : {
2583 50281 : pari_sp av = avma;
2584 50281 : GEN sol = NULL, fa, ex;
2585 : long i, j, v, l;
2586 :
2587 50281 : ab = check_ab(ab);
2588 50281 : if (typ(P) != t_POL) return rootsdeg0(P);
2589 50260 : switch(degpol(P))
2590 : {
2591 7 : case -1: return rootsdeg0(gen_0);
2592 7 : case 0: return rootsdeg0(gel(P,2));
2593 : }
2594 50246 : if (!RgX_is_ZX(P)) P = RgX_rescale_to_int(P);
2595 50246 : v = ZX_valrem(Q_primpart(P), &P);
2596 50246 : fa = ZX_squff(P, &ex); l = lg(fa); sol = cgetg(l + 1, t_VEC);
2597 102040 : for (i = 1; i < l; i++)
2598 : {
2599 51794 : GEN Pi = gel(fa, i), soli, soli2;
2600 : long n, h;
2601 51794 : if (ab) h = 1; else Pi = ZX_deflate_max(Pi, &h);
2602 51794 : soli = ZX_Uspensky(Pi, odd(h)? ab: gen_0, 1, prec2nbits(prec));
2603 51794 : n = lg(soli); soli2 = odd(h)? NULL: cgetg(n, t_COL);
2604 118322 : for (j = 1; j < n; j++)
2605 : {
2606 66528 : GEN r = gel(soli, j); /* != 0 */
2607 66528 : if (typ(r) != t_REAL) gel(soli, j) = r = gtofp(r, prec);
2608 66528 : if (h > 1)
2609 : {
2610 77 : gel(soli, j) = r = _sqrtnr(r, h);
2611 77 : if (soli2) gel(soli2, j) = negr(r);
2612 : }
2613 : }
2614 51794 : if (soli2) soli = shallowconcat(soli, soli2);
2615 51794 : if (ex[i] > 1) soli = shallowconcat1( const_vec(ex[i], soli) );
2616 51794 : gel(sol, i) = soli;
2617 : }
2618 50246 : if (v && (!ab || (gsigne(gel(ab,1)) <= 0 && gsigne(gel(ab,2)) >= 0)))
2619 63 : gel(sol, i++) = const_col(v, real_0(prec));
2620 50246 : setlg(sol, i); if (i == 1) { set_avma(av); return cgetg(1,t_COL); }
2621 50232 : return gerepileupto(av, sort(shallowconcat1(sol)));
2622 : }
2623 : GEN
2624 46140 : ZX_realroots_irred(GEN P, long prec)
2625 : {
2626 46140 : long dP = degpol(P), j, n, h;
2627 : GEN sol, sol2;
2628 : pari_sp av;
2629 46140 : if (dP == 1) retmkvec(ZX_deg1root(P, prec));
2630 43268 : av = avma; P = ZX_deflate_max(P, &h);
2631 43268 : if (h == dP)
2632 : {
2633 10925 : GEN r = _sqrtnr(ZX_deg1root(P, prec), h);
2634 10925 : return gerepilecopy(av, odd(h)? mkvec(r): mkvec2(negr(r), r));
2635 : }
2636 32343 : sol = ZX_Uspensky(P, odd(h)? NULL: gen_0, 1 | 4, prec2nbits(prec));
2637 32342 : n = lg(sol); sol2 = odd(h)? NULL: cgetg(n, t_COL);
2638 129616 : for (j = 1; j < n; j++)
2639 : {
2640 97273 : GEN r = gel(sol, j);
2641 97273 : if (typ(r) != t_REAL) gel(sol, j) = r = gtofp(r, prec);
2642 97273 : if (h > 1)
2643 : {
2644 10324 : gel(sol, j) = r = _sqrtnr(r, h);
2645 10325 : if (sol2) gel(sol2, j) = negr(r);
2646 : }
2647 : }
2648 32343 : if (sol2) sol = shallowconcat(sol, sol2);
2649 32343 : return gerepileupto(av, sort(sol));
2650 : }
2651 :
2652 : static long
2653 115216 : ZX_sturm_i(GEN P, long flag)
2654 : {
2655 : pari_sp av;
2656 115216 : long h, r, dP = degpol(P);
2657 115216 : if (dP == 1) return 1;
2658 112266 : av = avma; P = ZX_deflate_max(P, &h);
2659 112266 : if (h == dP)
2660 : { /* now deg P = 1 */
2661 17297 : if (odd(h))
2662 644 : r = 1;
2663 : else
2664 16653 : r = (signe(gel(P,2)) != signe(gel(P,3)))? 2: 0;
2665 17297 : return gc_long(av, r);
2666 : }
2667 94969 : if (odd(h))
2668 72905 : r = itou(ZX_Uspensky(P, NULL, flag, 0));
2669 : else
2670 22064 : r = 2*itou(ZX_Uspensky(P, gen_0, flag, 0));
2671 94968 : return gc_long(av,r);
2672 : }
2673 : /* P nonconstant, squarefree ZX */
2674 : long
2675 2974 : ZX_sturmpart(GEN P, GEN ab)
2676 : {
2677 2974 : pari_sp av = avma;
2678 2974 : if (!check_ab(ab)) return ZX_sturm(P);
2679 1588 : return gc_long(av, itou(ZX_Uspensky(P, ab, 2, 0)));
2680 : }
2681 : /* P nonconstant, squarefree ZX */
2682 : long
2683 1393 : ZX_sturm(GEN P) { return ZX_sturm_i(P, 2); }
2684 : /* P irreducible ZX */
2685 : long
2686 113823 : ZX_sturm_irred(GEN P) { return ZX_sturm_i(P, 2 + 4); }
|