Line data Source code
1 : /* Copyright (C) 2015 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 : /** L-functions **/
18 : /** **/
19 : /********************************************************************/
20 :
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_lfun
25 :
26 : /*******************************************************************/
27 : /* Accessors */
28 : /*******************************************************************/
29 :
30 : static GEN
31 12017 : mysercoeff(GEN x, long n)
32 : {
33 12017 : long N = n - valser(x);
34 12017 : return (N < 0)? gen_0: gel(x, N+2);
35 : }
36 :
37 : long
38 75308 : ldata_get_type(GEN ldata) { return mael3(ldata, 1, 1, 1); }
39 :
40 : GEN
41 73703 : ldata_get_an(GEN ldata) { return gel(ldata, 1); }
42 :
43 : GEN
44 58569 : ldata_get_dual(GEN ldata) { return gel(ldata, 2); }
45 :
46 : long
47 2799 : ldata_isreal(GEN ldata) { return isintzero(gel(ldata, 2)); }
48 :
49 : GEN
50 344848 : ldata_get_gammavec(GEN ldata) { return gel(ldata, 3); }
51 :
52 : long
53 25681 : ldata_get_degree(GEN ldata) { return lg(gel(ldata, 3))-1; }
54 :
55 : GEN
56 151928 : ldata_get_k(GEN ldata)
57 : {
58 151928 : GEN w = gel(ldata,4);
59 151928 : if (typ(w) == t_VEC) w = gel(w,1);
60 151928 : return w;
61 : }
62 :
63 : /* a_n = O(n^{k1 + epsilon}) */
64 : GEN
65 98 : ldata_get_k1(GEN ldata)
66 : {
67 98 : GEN w = gel(ldata,4);
68 98 : if (typ(w) == t_VEC) return gel(w,2);
69 : /* by default, assume that k1 = k-1 and even (k-1)/2 for entire functions */
70 98 : w = gaddgs(w,-1);
71 98 : return ldata_get_residue(ldata)? w: gmul2n(w, -1);
72 : }
73 :
74 : /* a_n = O(n^{k1 + epsilon}) */
75 : static double
76 90844 : ldata_get_k1_dbl(GEN ldata)
77 : {
78 90844 : GEN w = gel(ldata,4);
79 : double k;
80 90844 : if (typ(w) == t_VEC) return gtodouble(gel(w,2));
81 : /* by default, assume that k1 = k-1 and even (k-1)/2 for entire functions */
82 89423 : k = gtodouble(w);
83 89423 : return ldata_get_residue(ldata)? k-1: (k-1)/2.;
84 : }
85 :
86 : GEN
87 268697 : ldata_get_conductor(GEN ldata) { return gel(ldata, 5); }
88 :
89 : GEN
90 103537 : ldata_get_rootno(GEN ldata) { return gel(ldata, 6); }
91 :
92 : GEN
93 173001 : ldata_get_residue(GEN ldata) { return lg(ldata) == 7 ? NULL: gel(ldata, 7); }
94 :
95 : long
96 145908 : linit_get_type(GEN linit) { return mael(linit, 1, 1); }
97 :
98 : GEN
99 195240 : linit_get_ldata(GEN linit) { return gel(linit, 2); }
100 :
101 : GEN
102 247529 : linit_get_tech(GEN linit) { return gel(linit, 3); }
103 :
104 : long
105 296639 : is_linit(GEN data)
106 : {
107 182199 : return lg(data) == 4 && typ(data) == t_VEC
108 478838 : && typ(gel(data, 1)) == t_VECSMALL;
109 : }
110 :
111 : GEN
112 31554 : lfun_get_step(GEN tech) { return gmael(tech, 2, 1);}
113 :
114 : GEN
115 31554 : lfun_get_pol(GEN tech) { return gmael(tech, 2, 2);}
116 :
117 : GEN
118 5151 : lfun_get_Residue(GEN tech) { return gmael(tech, 2, 3);}
119 :
120 : GEN
121 49634 : lfun_get_k2(GEN tech) { return gmael(tech, 3, 1);}
122 :
123 : GEN
124 19347 : lfun_get_w2(GEN tech) { return gmael(tech, 3, 2);}
125 :
126 : GEN
127 19347 : lfun_get_expot(GEN tech) { return gmael(tech, 3, 3);}
128 :
129 : GEN
130 9814 : lfun_get_factgammavec(GEN tech) { return gmael(tech, 3, 4); }
131 :
132 : /* Handle complex Vga whose sum is real */
133 : static GEN
134 102387 : sumVga(GEN Vga) { return real_i(vecsum(Vga)); }
135 : /* sum_i max (Im v[i],0) */
136 : static double
137 26714 : sumVgaimpos(GEN v)
138 : {
139 26714 : double d = 0.;
140 26714 : long i, l = lg(v);
141 73772 : for (i = 1; i < l; i++)
142 : {
143 47058 : GEN c = imag_i(gel(v,i));
144 47058 : if (gsigne(c) > 0) d += gtodouble(c);
145 : }
146 26714 : return d;
147 : }
148 :
149 : static long
150 41646 : vgaell(GEN Vga)
151 : {
152 41646 : if (lg(Vga) == 3)
153 30124 : { GEN c = gsub(gel(Vga,1), gel(Vga,2)); return gequal1(c) || gequalm1(c); }
154 11522 : return 0;
155 : }
156 : int
157 86544 : Vgaeasytheta(GEN Vga) { return lg(Vga)-1 == 1 || vgaell(Vga); }
158 : /* return b(n) := a(n) * n^c, when Vgaeasytheta(Vga) is set */
159 : static GEN
160 18305 : antwist(GEN an, GEN Vga, long prec)
161 : {
162 : long l, i;
163 18305 : GEN b, c = vecmin(Vga);
164 18305 : if (gequal0(c)) return an;
165 4235 : l = lg(an); b = cgetg(l, t_VEC);
166 4235 : if (gequal1(c))
167 : {
168 3507 : if (typ(an) == t_VECSMALL)
169 17584 : for (i = 1; i < l; i++) gel(b,i) = mulss(an[i], i);
170 : else
171 36890 : for (i = 1; i < l; i++) gel(b,i) = gmulgu(gel(an,i), i);
172 : }
173 : else
174 : {
175 728 : GEN v = vecpowug(l-1, c, prec);
176 728 : if (typ(an) == t_VECSMALL)
177 0 : for (i = 1; i < l; i++) gel(b,i) = gmulsg(an[i], gel(v,i));
178 : else
179 20965 : for (i = 1; i < l; i++) gel(b,i) = gmul(gel(an,i), gel(v,i));
180 : }
181 4235 : return b;
182 : }
183 :
184 : static GEN
185 9387 : theta_dual(GEN theta, GEN bn)
186 : {
187 9387 : if (typ(bn)==t_INT) return NULL;
188 : else
189 : {
190 77 : GEN thetad = shallowcopy(theta), ldata = linit_get_ldata(theta);
191 77 : GEN Vga = ldata_get_gammavec(ldata);
192 77 : GEN tech = shallowcopy(linit_get_tech(theta));
193 77 : GEN an = theta_get_an(tech);
194 77 : long prec = nbits2prec(theta_get_bitprec(tech));
195 77 : GEN vb = ldata_vecan(bn, lg(an)-1, prec);
196 77 : if (!theta_get_m(tech) && Vgaeasytheta(Vga)) vb = antwist(vb, Vga, prec);
197 77 : gel(tech,1) = vb;
198 77 : gel(thetad,3) = tech; return thetad;
199 : }
200 : }
201 :
202 : static GEN
203 83530 : domain_get_dom(GEN domain) { return gel(domain,1); }
204 : static long
205 25260 : domain_get_der(GEN domain) { return mael2(domain, 2, 1); }
206 : static long
207 36278 : domain_get_bitprec(GEN domain) { return mael2(domain, 2, 2); }
208 : GEN
209 84090 : lfun_get_domain(GEN tech) { return gel(tech,1); }
210 : long
211 91 : lfun_get_bitprec(GEN tech){ return domain_get_bitprec(lfun_get_domain(tech)); }
212 : GEN
213 58662 : lfun_get_dom(GEN tech) { return domain_get_dom(lfun_get_domain(tech)); }
214 :
215 : GEN
216 2561 : lfunprod_get_fact(GEN tech) { return gel(tech, 2); }
217 :
218 : GEN
219 52153 : theta_get_an(GEN tdata) { return gel(tdata, 1);}
220 : GEN
221 7651 : theta_get_K(GEN tdata) { return gel(tdata, 2);}
222 : GEN
223 5991 : theta_get_R(GEN tdata) { return gel(tdata, 3);}
224 : long
225 65522 : theta_get_bitprec(GEN tdata) { return itos(gel(tdata, 4));}
226 : long
227 101380 : theta_get_m(GEN tdata) { return itos(gel(tdata, 5));}
228 : GEN
229 53525 : theta_get_tdom(GEN tdata) { return gel(tdata, 6);}
230 : GEN
231 62317 : theta_get_isqrtN(GEN tdata) { return gel(tdata, 7);}
232 :
233 : /*******************************************************************/
234 : /* Helper functions related to Gamma products */
235 : /*******************************************************************/
236 : /* x != 0 */
237 : static int
238 6531 : serisscalar(GEN x)
239 : {
240 : long i;
241 6531 : if (valser(x)) return 0;
242 8190 : for (i = lg(x)-1; i > 3; i--) if (!gequal0(gel(x,i))) return 0;
243 6286 : return 1;
244 : }
245 :
246 : /* return -itos(s) >= 0 if scalar s is (approximately) equal to a nonpositive
247 : * integer, and -1 otherwise */
248 : static long
249 20482 : isnegint(GEN s)
250 : {
251 20482 : GEN r = ground(real_i(s));
252 20482 : if (signe(r) <= 0 && gequal(s, r)) return -itos(r);
253 20356 : return -1;
254 : }
255 : /* if s = a + O(x^n), a <= 0 integer, replace by a + b*x^n + O(x^(n+1)) */
256 : static GEN
257 6552 : serextendifnegint(GEN s, GEN b, long *ext)
258 : {
259 6552 : if (!signe(s) || (serisscalar(s) && isnegint(gel(s,2)) >= 0))
260 : {
261 112 : long l = lg(s);
262 112 : GEN t = cgetg(l+1, t_SER);
263 301 : gel(t, l) = b; while (--l > 1) gel(t,l) = gel(s,l);
264 112 : if (gequal0(gel(t,2))) gel(t,2) = gen_0;
265 112 : t[1] = s[1]; s = normalizeser(t); *ext = 1;
266 : }
267 6552 : return s;
268 : }
269 :
270 : /* r/x + O(1), r != 0 */
271 : static GEN
272 5047 : serpole(GEN r)
273 : {
274 5047 : GEN s = cgetg(3, t_SER);
275 5047 : s[1] = evalsigne(1)|evalvalser(-1)|evalvarn(0);
276 5047 : gel(s,2) = r; return s;
277 : }
278 : /* a0 + a1 x + O(x^e), e >= 0 */
279 : static GEN
280 7483 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
281 7483 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
282 :
283 : /* pi^(-s/2) Gamma(s/2) */
284 : static GEN
285 10066 : gamma_R(GEN s, long *ext, long prec)
286 : {
287 10066 : GEN s2 = gmul2n(s, -1);
288 : long ms;
289 :
290 10066 : if (typ(s) == t_SER)
291 4788 : s2 = serextendifnegint(s2, ghalf, ext);
292 5278 : else if ((ms = isnegint(s2)) >= 0)
293 : {
294 35 : GEN r = gmul(powPis(stoi(ms),prec), gdivsg(odd(ms)? -2: 2, mpfact(ms)));
295 35 : return serpole(r);
296 : }
297 10031 : return gdiv(ggamma(s2,prec), powPis(s2,prec));
298 : }
299 : /* gamma_R(s)gamma_R(s+1) = 2 (2pi)^(-s) Gamma(s) */
300 : static GEN
301 10682 : gamma_C(GEN s, long *ext, long prec)
302 : {
303 : long ms;
304 10682 : if (typ(s) == t_SER)
305 1764 : s = serextendifnegint(s, gen_1, ext);
306 8918 : else if ((ms = isnegint(s)) >= 0)
307 : {
308 0 : GEN r = gmul(pow2Pis(stoi(ms),prec), gdivsg(odd(ms)? -2: 2, mpfact(ms)));
309 0 : return serpole(r);
310 : }
311 10682 : return gmul2n(gdiv(ggamma(s,prec), pow2Pis(s,prec)), 1);
312 : }
313 :
314 : static GEN
315 1708 : gammafrac(GEN r, long d)
316 : {
317 1708 : long i, l = labs(d) + 1, j = (d > 0)? 0: 2*d;
318 1708 : GEN T, v = cgetg(l, t_COL);
319 4466 : for (i = 1; i < l; i++, j += 2)
320 2758 : gel(v,i) = deg1pol_shallow(gen_1, gaddgs(r, j), 0);
321 1708 : T = RgV_prod(v); return d > 0? T: mkrfrac(gen_1, T);
322 : }
323 :
324 : /*
325 : GR(s)=Pi^-(s/2)*gamma(s/2);
326 : GC(s)=2*(2*Pi)^-s*gamma(s)
327 : gdirect(F,s)=prod(i=1,#F,GR(s+F[i]))
328 : gfact(F,s)=
329 : { my([R,A,B]=gammafactor(F), [a,e]=A, [b,f]=B, p=poldegree(R));
330 : subst(R,x,s) * (2*Pi)^-p * prod(i=1,#a,GR(s+a[i])^e[i])
331 : * prod(i=1,#b,GC(s+b[i])^f[i]); }
332 : */
333 : static GEN
334 21259 : gammafactor(GEN Vga)
335 : {
336 21259 : long i, r, c, l = lg(Vga);
337 21259 : GEN v, P, a, b, e, f, E, F = cgetg(l, t_VEC), R = gen_1;
338 59514 : for (i = 1; i < l; ++i)
339 : {
340 38255 : GEN a = gel(Vga,i), r = gmul2n(real_i(a), -1);
341 38255 : long q = itos(gfloor(r)); /* [Re a/2] */
342 38255 : r = gmul2n(gsubgs(r, q), 1);
343 38255 : gel(F,i) = gequal0(imag_i(a)) ? r : mkcomplex(r, imag_i(a)); /* 2{Re a/2} + I*(Im a) */
344 38255 : if (q) R = gmul(R, gammafrac(gel(F,i), q));
345 : }
346 21259 : F = vec_reduce(F, &E); l = lg(E);
347 21259 : v = cgetg(l, t_VEC);
348 54194 : for (i = 1; i < l; i++)
349 32935 : gel(v,i) = mkvec2(gsub(gel(F,i),gfloor(real_i(gel(F,i)))), stoi(E[i]));
350 21259 : gen_sort_inplace(v, (void*)cmp_universal, cmp_nodata, &P);
351 21259 : a = cgetg(l, t_VEC); e = cgetg(l, t_VECSMALL);
352 21259 : b = cgetg(l, t_VEC); f = cgetg(l, t_VECSMALL);
353 43484 : for (i = r = c = 1; i < l;)
354 22225 : if (i==l-1 || cmp_universal(gel(v,i), gel(v,i+1)))
355 11515 : { gel(a, r) = gel(F, P[i]); e[r++] = E[P[i]]; i++; }
356 : else
357 10710 : { gel(b, c) = gel(F, P[i]); f[c++] = E[P[i]]; i+=2; }
358 21259 : setlg(a, r); setlg(e, r);
359 21259 : setlg(b, c); setlg(f, c); return mkvec3(R, mkvec2(a,e), mkvec2(b,f));
360 : }
361 :
362 : static GEN
363 3640 : polgammaeval(GEN F, GEN s)
364 : {
365 3640 : GEN r = poleval(F, s);
366 3640 : if (typ(s) != t_SER && gequal0(r))
367 : { /* here typ(F) = t_POL */
368 : long e;
369 7 : for (e = 1;; e++)
370 : {
371 7 : F = RgX_deriv(F); r = poleval(F,s);
372 7 : if (!gequal0(r)) break;
373 : }
374 7 : if (e > 1) r = gdiv(r, mpfact(e));
375 7 : r = serpole(r); setvalser(r, e);
376 : }
377 3640 : return r;
378 : }
379 : static long
380 1799 : rfrac_degree(GEN R)
381 : {
382 1799 : GEN a = gel(R,1), b = gel(R,2);
383 1799 : return ((typ(a) == t_POL)? degpol(a): 0) - degpol(b);
384 : }
385 : static GEN
386 19754 : fracgammaeval(GEN F, GEN s, long prec)
387 : {
388 19754 : GEN R = gel(F,1);
389 : long d;
390 19754 : switch(typ(R))
391 : {
392 42 : case t_POL:
393 42 : d = degpol(R);
394 42 : R = polgammaeval(R, s); break;
395 1799 : case t_RFRAC:
396 1799 : d = rfrac_degree(R);
397 1799 : R = gdiv(polgammaeval(gel(R,1), s), polgammaeval(gel(R,2), s)); break;
398 17913 : default: return R;
399 : }
400 1841 : return gmul(R, powrs(Pi2n(1,prec), -d));
401 : }
402 :
403 : static GEN
404 19754 : gammafactproduct(GEN F, GEN s, long *ext, long prec)
405 : {
406 19754 : pari_sp av = avma;
407 19754 : GEN R = gel(F,2), Rw = gel(R,1), Re = gel(R,2);
408 19754 : GEN C = gel(F,3), Cw = gel(C,1), Ce = gel(C,2), z = fracgammaeval(F,s,prec);
409 19754 : long i, lR = lg(Rw), lC = lg(Cw);
410 19754 : *ext = 0;
411 29820 : for (i = 1; i < lR; i++)
412 10066 : z = gmul(z, gpowgs(gamma_R(gadd(s,gel(Rw, i)), ext, prec), Re[i]));
413 30436 : for (i = 1; i < lC; i++)
414 10682 : z = gmul(z, gpowgs(gamma_C(gadd(s,gel(Cw, i)), ext, prec), Ce[i]));
415 19754 : return gerepileupto(av, z);
416 : }
417 :
418 : static int
419 4942 : gammaordinary(GEN Vga, GEN s)
420 : {
421 4942 : long i, d = lg(Vga)-1;
422 13391 : for (i = 1; i <= d; i++)
423 : {
424 8540 : GEN z = gadd(s, gel(Vga,i));
425 : long e;
426 8540 : if (gexpo(imag_i(z)) < -10)
427 : {
428 8540 : z = real_i(z);
429 8540 : if (gsigne(z) <= 0) { (void)grndtoi(z, &e); if (e < -10) return 0; }
430 : }
431 : }
432 4851 : return 1;
433 : }
434 :
435 : /* Exponent A of t in asymptotic expansion; K(t) ~ C t^A exp(-pi d t^(2/d)).
436 : * suma = vecsum(Vga)*/
437 : static double
438 90837 : gammavec_expo(long d, double suma) { return (1 - d + suma) / d; }
439 :
440 : /*******************************************************************/
441 : /* First part: computations only involving Theta(t) */
442 : /*******************************************************************/
443 :
444 : static void
445 138410 : get_cone(GEN t, double *r, double *a)
446 : {
447 138410 : const long prec = LOWDEFAULTPREC;
448 138410 : if (typ(t) == t_COMPLEX)
449 : {
450 21518 : t = gprec_w(t, prec);
451 21518 : *r = gtodouble(gabs(t, prec));
452 21518 : *a = fabs(gtodouble(garg(t, prec)));
453 : }
454 : else
455 : {
456 116892 : *r = fabs(gtodouble(t));
457 116892 : *a = 0.;
458 : }
459 138410 : if (!*r && !*a) pari_err_DOMAIN("lfunthetainit","t","=",gen_0,t);
460 138403 : }
461 : /* slightly larger cone than necessary, to avoid round-off problems */
462 : static void
463 84885 : get_cone_fuzz(GEN t, double *r, double *a)
464 84885 : { get_cone(t, r, a); *r -= 1e-10; if (*a) *a += 1e-10; }
465 :
466 : /* Initialization m-th Theta derivative. tdom is either
467 : * - [rho,alpha]: assume |t| >= rho and |arg(t)| <= alpha
468 : * - a positive real scalar: assume t real, t >= tdom;
469 : * - a complex number t: compute at t;
470 : * N is the conductor (either the true one from ldata or a guess from
471 : * lfunconductor) */
472 : long
473 64130 : lfunthetacost(GEN ldata, GEN tdom, long m, long bitprec)
474 : {
475 64130 : pari_sp av = avma;
476 64130 : GEN Vga = ldata_get_gammavec(ldata);
477 64130 : long d = lg(Vga)-1;
478 64130 : double k1 = maxdd(ldata_get_k1_dbl(ldata), 0.);
479 64130 : double c = d/2., a, A, B, logC, al, rho, T;
480 64130 : double N = gtodouble(ldata_get_conductor(ldata));
481 :
482 64130 : if (!N) pari_err_TYPE("lfunthetaneed [missing conductor]", ldata);
483 64130 : if (typ(tdom) == t_VEC && lg(tdom) == 3)
484 : {
485 7 : rho= gtodouble(gel(tdom,1));
486 7 : al = gtodouble(gel(tdom,2));
487 : }
488 : else
489 64123 : get_cone_fuzz(tdom, &rho, &al);
490 64123 : A = gammavec_expo(d, gtodouble(sumVga(Vga))); set_avma(av);
491 64123 : a = (A+k1+1) + (m-1)/c;
492 64123 : if (fabs(a) < 1e-10) a = 0.;
493 64123 : logC = c*M_LN2 - log(c)/2;
494 : /* +1: fudge factor */
495 64123 : B = M_LN2*bitprec+logC+m*log(2*M_PI) + 1 + (k1+1)*log(N)/2 - (k1+m+1)*log(rho);
496 64123 : if (al)
497 : { /* t = rho e^(i*al), T^(1/c) = Re(t^(1/c)) > 0, T = rho cos^c(al/c) */
498 10766 : double z = cos(al/c);
499 10766 : if (z <= 0)
500 7 : pari_err_DOMAIN("lfunthetaneed", "arg t", ">", dbltor(c*M_PI/2), tdom);
501 10759 : T = (d == 2 && typ(tdom) != t_VEC)? gtodouble(real_i(tdom)): rho*pow(z,c);
502 10759 : B -= log(z) * (c * (k1+A+1) + m);
503 : }
504 : else
505 53357 : T = rho;
506 64116 : if (B <= 0) return 0;
507 64116 : A = floor(0.9 + dblcoro526(a,c,B) / T * sqrt(N));
508 64116 : if (dblexpo(A) >= BITS_IN_LONG-1) pari_err_OVERFLOW("lfunthetacost");
509 64109 : return (long)A;
510 : }
511 : long
512 21 : lfunthetacost0(GEN L, GEN tdom, long m, long bitprec)
513 : {
514 : long n;
515 21 : if (is_linit(L) && linit_get_type(L)==t_LDESC_THETA)
516 7 : {
517 7 : GEN tech = linit_get_tech(L);
518 7 : n = lg(theta_get_an(tech))-1;
519 : }
520 : else
521 : {
522 14 : pari_sp av = avma;
523 14 : GEN ldata = lfunmisc_to_ldata_shallow(L);
524 14 : n = lfunthetacost(ldata, tdom? tdom: gen_1, m, bitprec);
525 7 : set_avma(av);
526 : }
527 14 : return n;
528 : }
529 :
530 : static long
531 6321 : fracgammadegree(GEN FVga)
532 6321 : { GEN F = gel(FVga,1); return (typ(F)==t_RFRAC)? degpol(gel(F,2)): 0; }
533 :
534 : /* Poles of a L-function can be represented in the following ways:
535 : * 1) Nothing (ldata has only 6 components, ldata_get_residue = NULL).
536 : * 2) a complex number (single pole at s = k with given residue, unknown if 0).
537 : * 3) A vector (possibly empty) of 2-component vectors [a, ra], where a is the
538 : * pole, ra a t_SER: its Taylor expansion at a. A t_VEC encodes the polar
539 : * part of L, a t_COL, the polar part of Lambda */
540 :
541 : /* 'a' a complex number (pole), 'r' the polar part of L at 'a';
542 : * return 'R' the polar part of Lambda at 'a' */
543 : static GEN
544 4676 : rtoR(GEN a, GEN r, GEN FVga, GEN N, long prec)
545 : {
546 4676 : long v = lg(r)-2, d = fracgammadegree(FVga), ext;
547 4676 : GEN Na, as = deg1ser_shallow(gen_1, a, varn(r), v);
548 4676 : Na = gpow(N, gdivgu(as, 2), prec);
549 : /* make up for a possible loss of accuracy */
550 4676 : if (d) as = deg1ser_shallow(gen_1, a, varn(r), v + d);
551 4676 : return gmul(gmul(r, Na), gammafactproduct(FVga, as, &ext, prec));
552 : }
553 :
554 : /* assume r in normalized form: t_VEC of pairs [be,re] */
555 : GEN
556 4445 : lfunrtopoles(GEN r)
557 : {
558 4445 : long j, l = lg(r);
559 4445 : GEN v = cgetg(l, t_VEC);
560 9121 : for (j = 1; j < l; j++)
561 : {
562 4676 : GEN rj = gel(r,j), a = gel(rj,1);
563 4676 : gel(v,j) = a;
564 : }
565 4445 : gen_sort_inplace(v, (void*)&cmp_universal, cmp_nodata, NULL);
566 4445 : return v;
567 : }
568 :
569 : /* r / x + O(1) */
570 : static GEN
571 5152 : simple_pole(GEN r)
572 5152 : { return isintzero(r)? gen_0: serpole(r); }
573 : static GEN
574 6006 : normalize_simple_pole(GEN r, GEN k)
575 : {
576 6006 : long tx = typ(r);
577 6006 : if (is_vec_t(tx)) return r;
578 5152 : if (!is_scalar_t(tx)) pari_err_TYPE("lfunrootres [poles]", r);
579 5152 : return mkvec(mkvec2(k, simple_pole(r)));
580 : }
581 : /* normalize the description of a polar part */
582 : static GEN
583 5320 : normalizepoles(GEN r, GEN k)
584 : {
585 : long iv, j, l;
586 : GEN v;
587 5320 : if (!is_vec_t(typ(r))) return normalize_simple_pole(r, k);
588 2233 : v = cgetg_copy(r, &l);
589 5593 : for (j = iv = 1; j < l; j++)
590 : {
591 3360 : GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
592 3360 : if (!is_scalar_t(typ(a)) || typ(ra) != t_SER)
593 0 : pari_err_TYPE("lfunrootres [poles]",r);
594 3360 : if (valser(ra) >= 0) continue;
595 3346 : gel(v,iv++) = rj;
596 : }
597 2233 : setlg(v, iv); return v;
598 : }
599 : static int
600 8652 : residues_known(GEN r)
601 : {
602 8652 : long i, l = lg(r);
603 8652 : if (isintzero(r)) return 0;
604 8323 : if (!is_vec_t(typ(r))) return 1;
605 11060 : for (i = 1; i < l; i++)
606 : {
607 6734 : GEN ri = gel(r,i);
608 6734 : if (!is_vec_t(typ(ri)) || lg(ri)!=3)
609 0 : pari_err_TYPE("lfunrootres [poles]",r);
610 6734 : if (isintzero(gel(ri, 2))) return 0;
611 : }
612 4326 : return 1;
613 : }
614 :
615 : /* Compute R's from r's (r = Taylor devts of L(s), R of Lambda(s)).
616 : * 'r/eno' passed to override the one from ldata */
617 : static GEN
618 23352 : lfunrtoR_i(GEN ldata, GEN r, GEN eno, long prec)
619 : {
620 23352 : GEN Vga = ldata_get_gammavec(ldata), N = ldata_get_conductor(ldata);
621 : GEN R, vr, FVga;
622 23352 : pari_sp av = avma;
623 : long lr, j, jR;
624 23352 : GEN k = ldata_get_k(ldata);
625 :
626 23352 : if (!r || isintzero(eno) || !residues_known(r))
627 18032 : return gen_0;
628 5320 : r = normalizepoles(r, k);
629 5320 : if (typ(r) == t_COL) return gerepilecopy(av, r);
630 4445 : if (typ(ldata_get_dual(ldata)) != t_INT)
631 0 : pari_err(e_MISC,"please give the Taylor expansion of Lambda");
632 4445 : vr = lfunrtopoles(r); lr = lg(vr);
633 4445 : FVga = gammafactor(Vga);
634 4445 : R = cgetg(2*lr, t_COL);
635 9121 : for (j = jR = 1; j < lr; j++)
636 : {
637 4676 : GEN rj = gel(r,j), a = gel(rj,1), ra = gel(rj,2);
638 4676 : GEN Ra = rtoR(a, ra, FVga, N, prec);
639 4676 : GEN b = gsub(k, conj_i(a));
640 4676 : if (lg(Ra)-2 < -valser(Ra))
641 0 : pari_err(e_MISC,
642 : "please give more terms in L function's Taylor expansion at %Ps", a);
643 4676 : gel(R,jR++) = mkvec2(a, Ra);
644 4676 : if (!tablesearch(vr, b, (int (*)(GEN,GEN))&cmp_universal))
645 : {
646 4487 : GEN mX = gneg(pol_x(varn(Ra)));
647 4487 : GEN Rb = gmul(eno, gsubst(conj_i(Ra), varn(Ra), mX));
648 4487 : gel(R,jR++) = mkvec2(b, Rb);
649 : }
650 : }
651 4445 : setlg(R, jR); return gerepilecopy(av, R);
652 : }
653 : static GEN
654 22876 : lfunrtoR_eno(GEN ldata, GEN eno, long prec)
655 22876 : { return lfunrtoR_i(ldata, ldata_get_residue(ldata), eno, prec); }
656 : static GEN
657 20769 : lfunrtoR(GEN ldata, long prec)
658 20769 : { return lfunrtoR_eno(ldata, ldata_get_rootno(ldata), prec); }
659 :
660 : static long
661 20769 : prec_fix(long prec)
662 : {
663 : #ifndef LONG_IS_64BIT
664 : /* make sure that default accuracy is the same on 32/64bit */
665 2967 : if (odd(prec)) prec += EXTRAPREC64;
666 : #endif
667 20769 : return prec;
668 : }
669 :
670 : /* thetainit using {an: n <= L}; if (m = 0 && easytheta), an2 is an * n^al */
671 : static GEN
672 20769 : lfunthetainit0(GEN ldata, GEN tdom, GEN an2, long m,
673 : long bitprec, long extrabit)
674 : {
675 20769 : long prec = nbits2prec(bitprec);
676 20769 : GEN tech, N = ldata_get_conductor(ldata);
677 20769 : GEN K = gammamellininvinit(ldata, m, bitprec + extrabit);
678 20769 : GEN R = lfunrtoR(ldata, prec);
679 20769 : if (!tdom) tdom = gen_1;
680 20769 : if (typ(tdom) != t_VEC)
681 : {
682 : double r, a;
683 20762 : get_cone_fuzz(tdom, &r, &a);
684 20762 : tdom = mkvec2(dbltor(r), a? dbltor(a): gen_0);
685 : }
686 20769 : prec += maxss(EXTRAPREC64, nbits2extraprec(extrabit));
687 20769 : tech = mkvecn(7, an2,K,R, stoi(bitprec), stoi(m), tdom,
688 : gsqrt(ginv(N), prec_fix(prec)));
689 20769 : return mkvec3(mkvecsmall(t_LDESC_THETA), ldata, tech);
690 : }
691 :
692 : /* tdom: 1) positive real number r, t real, t >= r; or
693 : * 2) [r,a], describing the cone |t| >= r, |arg(t)| <= a */
694 : static GEN
695 10325 : lfunthetainit_i(GEN data, GEN tdom, long m, long bit)
696 : {
697 10325 : GEN ldata = lfunmisc_to_ldata_shallow(data);
698 10325 : long b = 32, L = lfunthetacost(ldata, tdom, m, bit), prec = nbits2prec(bit);
699 10311 : GEN ldatan = ldata_newprec(ldata, prec);
700 10311 : GEN an = ldata_vecan(ldata_get_an(ldatan), L, prec);
701 10311 : GEN Vga = ldata_get_gammavec(ldatan);
702 10311 : if (m == 0 && Vgaeasytheta(Vga)) an = antwist(an, Vga, prec);
703 10311 : if (typ(an) != t_VECSMALL) b = maxss(b, gexpo(an));
704 10311 : return lfunthetainit0(ldatan, tdom, an, m, bit, b);
705 : }
706 :
707 : GEN
708 336 : lfunthetainit(GEN ldata, GEN tdom, long m, long bitprec)
709 : {
710 336 : pari_sp av = avma;
711 336 : GEN S = lfunthetainit_i(ldata, tdom? tdom: gen_1, m, bitprec);
712 336 : return gerepilecopy(av, S);
713 : }
714 :
715 : GEN
716 2450 : lfunan(GEN ldata, long L, long prec)
717 : {
718 2450 : pari_sp av = avma;
719 : GEN an ;
720 2450 : ldata = ldata_newprec(lfunmisc_to_ldata_shallow(ldata), prec);
721 2450 : an = gerepilecopy(av, ldata_vecan(ldata_get_an(ldata), L, prec));
722 2394 : if (typ(an) != t_VEC) an = vecsmall_to_vec_inplace(an);
723 2394 : return an;
724 : }
725 :
726 : static GEN
727 15897 : mulrealvec(GEN x, GEN y)
728 : {
729 15897 : if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
730 84 : pari_APPLY_same(mulreal(gel(x,i),gel(y,i)))
731 : else
732 15869 : return mulreal(x,y);
733 : }
734 : static GEN
735 31155 : gmulvec(GEN x, GEN y)
736 : {
737 31155 : if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
738 1974 : pari_APPLY_same(gmul(gel(x,i),gel(y,i)))
739 : else
740 30630 : return gmul(x,y);
741 : }
742 : static GEN
743 9366 : gdivvec(GEN x, GEN y)
744 : {
745 9366 : if (is_vec_t(typ(x)) && is_vec_t(typ(y)))
746 1519 : pari_APPLY_same(gdiv(gel(x,i),gel(y,i)))
747 : else
748 8932 : return gdiv(x,y);
749 : }
750 :
751 : static GEN
752 3556 : gsubvec(GEN x, GEN y)
753 : {
754 3556 : if (is_vec_t(typ(x)) && !is_vec_t(typ(y)))
755 0 : pari_APPLY_same(gsub(gel(x,i),y))
756 : else
757 3556 : return gsub(x,y);
758 : }
759 :
760 : /* return [1^(2/d), 2^(2/d),...,lim^(2/d)] */
761 : static GEN
762 7651 : mkvroots(long d, long lim, long prec)
763 : {
764 7651 : if (d <= 4)
765 : {
766 7301 : GEN v = cgetg(lim+1,t_VEC);
767 : long n;
768 7301 : switch(d)
769 : {
770 2576 : case 1:
771 46916 : for (n=1; n <= lim; n++) gel(v,n) = sqru(n);
772 2576 : return v;
773 1267 : case 2:
774 227213 : for (n=1; n <= lim; n++) gel(v,n) = utoipos(n);
775 1267 : return v;
776 1990 : case 4:
777 6117579 : for (n=1; n <= lim; n++) gel(v,n) = sqrtr(utor(n, prec));
778 1990 : return v;
779 : }
780 : }
781 1818 : return vecpowug(lim, gdivgu(gen_2,d), prec);
782 : }
783 :
784 : GEN
785 61582 : lfunthetacheckinit(GEN data, GEN t, long m, long bitprec)
786 : {
787 61582 : if (is_linit(data) && linit_get_type(data)==t_LDESC_THETA)
788 : {
789 53525 : GEN tdom, thetainit = linit_get_tech(data);
790 53525 : long bitprecnew = theta_get_bitprec(thetainit);
791 53525 : long m0 = theta_get_m(thetainit);
792 : double r, al, rt, alt;
793 53525 : if (m0 != m)
794 0 : pari_err_DOMAIN("lfuntheta","derivative order","!=", stoi(m),stoi(m0));
795 53525 : if (bitprec > bitprecnew) goto INIT;
796 53525 : get_cone(t, &rt, &alt);
797 53525 : tdom = theta_get_tdom(thetainit);
798 53525 : r = gtodouble(gel(tdom,1));
799 53525 : al= gtodouble(gel(tdom,2)); if (rt >= r && alt <= al) return data;
800 : }
801 8057 : INIT:
802 9898 : return lfunthetainit_i(data, t, m, bitprec);
803 : }
804 :
805 : static GEN
806 14711353 : get_an(GEN an, long n)
807 : {
808 14711353 : if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return stoi(a); }
809 14711353 : else { GEN a = gel(an,n); if (a && !gequal0(a)) return a; }
810 12685598 : return NULL;
811 : }
812 : /* x * an[n] */
813 : static GEN
814 12779236 : mul_an(GEN an, long n, GEN x)
815 : {
816 12779236 : if (typ(an) == t_VECSMALL) { long a = an[n]; if (a) return gmulsg(a,x); }
817 7357819 : else { GEN a = gel(an,n); if (a && !gequal0(a)) return gmul(a,x); }
818 2399325 : return NULL;
819 : }
820 : /* 2*t^a * x **/
821 : static GEN
822 320752 : mulT(GEN t, GEN a, GEN x, long prec)
823 : {
824 320752 : if (gequal0(a)) return gmul2n(x,1);
825 20131 : return gmul(x, gmul2n(gequal1(a)? t: gpow(t,a,prec), 1));
826 : }
827 :
828 : static GEN
829 34062410 : vecan_cmul(void *E, GEN P, long a, GEN x)
830 : {
831 : (void)E;
832 34062410 : if (typ(P) == t_VECSMALL)
833 24529229 : return (a==0 || !P[a])? NULL: gmulsg(P[a], x);
834 : else
835 9533181 : return (a==0 || !gel(P,a))? NULL: gmul(gel(P,a), x);
836 : }
837 : /* d=2, 2 sum_{n <= N} a(n) (n t)^al q^n, q = exp(-2pi t),
838 : * an2[n] = a(n) * n^al */
839 : static GEN
840 281041 : theta2_i(GEN an2, long N, GEN t, GEN al, long prec)
841 : {
842 281041 : GEN S, q, pi2 = Pi2n(1,prec);
843 281039 : const struct bb_algebra *alg = get_Rg_algebra();
844 281038 : setsigne(pi2,-1); q = gexp(gmul(pi2, t), prec);
845 : /* Brent-Kung in case the a_n are small integers */
846 281041 : S = gen_bkeval(an2, N, q, 1, NULL, alg, vecan_cmul);
847 281041 : return mulT(t, al, S, prec);
848 : }
849 : static GEN
850 272974 : theta2(GEN an2, long N, GEN t, GEN al, long prec)
851 : {
852 272974 : pari_sp av = avma;
853 272974 : return gerepileupto(av, theta2_i(an2, N, t, al, prec));
854 : }
855 :
856 : /* d=1, 2 sum_{n <= N} a_n (n t)^al q^(n^2), q = exp(-pi t^2),
857 : * an2[n] is a_n n^al */
858 : static GEN
859 39711 : theta1(GEN an2, long N, GEN t, GEN al, long prec)
860 : {
861 39711 : GEN q = gexp(gmul(negr(mppi(prec)), gsqr(t)), prec);
862 39711 : GEN vexp = gsqrpowers(q, N), S = gen_0;
863 39711 : pari_sp av = avma;
864 : long n;
865 6777440 : for (n = 1; n <= N; n++)
866 : {
867 6737729 : GEN c = mul_an(an2, n, gel(vexp,n));
868 6737729 : if (c)
869 : {
870 5664581 : S = gadd(S, c);
871 5664581 : if (gc_needed(av, 3)) S = gerepileupto(av, S);
872 : }
873 : }
874 39711 : return mulT(t, al, S, prec);
875 : }
876 :
877 : /* If m > 0, compute m-th derivative of theta(t) = theta0(t/sqrt(N))
878 : * with absolute error 2^-bitprec; theta(t)=\sum_{n\ge1}a(n)K(nt/N^(1/2)) */
879 : GEN
880 51866 : lfuntheta(GEN data, GEN t, long m, long bitprec)
881 : {
882 51866 : pari_sp ltop = avma;
883 : long limt, d;
884 : GEN isqN, vecan, Vga, ldata, theta, thetainit, S;
885 : long n, prec;
886 :
887 51866 : theta = lfunthetacheckinit(data, t, m, bitprec);
888 51859 : ldata = linit_get_ldata(theta);
889 51859 : thetainit = linit_get_tech(theta);
890 51859 : vecan = theta_get_an(thetainit);
891 51859 : isqN = theta_get_isqrtN(thetainit);
892 51859 : prec = maxss(realprec(isqN), nbits2prec(bitprec));
893 51859 : t = gprec_w(t, prec);
894 51859 : limt = lg(vecan)-1;
895 51859 : if (theta == data)
896 47708 : limt = minss(limt, lfunthetacost(ldata, t, m, bitprec));
897 51859 : if (!limt)
898 : {
899 14 : set_avma(ltop); S = real_0_bit(-bitprec);
900 14 : if (!is_real_t(typ(t)) || !ldata_isreal(ldata))
901 7 : S = gerepilecopy(ltop, mkcomplex(S,S));
902 14 : return S;
903 : }
904 51845 : t = gmul(t, isqN);
905 51845 : Vga = ldata_get_gammavec(ldata);
906 51845 : d = lg(Vga)-1;
907 51845 : if (m == 0 && Vgaeasytheta(Vga))
908 : {
909 47778 : if (theta_get_m(thetainit) > 0) vecan = antwist(vecan, Vga, prec);
910 47778 : if (d == 1) S = theta1(vecan, limt, t, gel(Vga,1), prec);
911 8067 : else S = theta2_i(vecan, limt, t, vecmin(Vga), prec);
912 : }
913 : else
914 : {
915 4067 : GEN K = theta_get_K(thetainit);
916 4067 : GEN vroots = mkvroots(d, limt, prec);
917 : pari_sp av;
918 4067 : t = gpow(t, gdivgu(gen_2,d), prec);
919 4067 : S = gen_0; av = avma;
920 14715420 : for (n = 1; n <= limt; ++n)
921 : {
922 14711353 : GEN nt, an = get_an(vecan, n);
923 14711353 : if (!an) continue;
924 2025755 : nt = gmul(gel(vroots,n), t);
925 2025755 : if (m) an = gmul(an, powuu(n, m));
926 2025755 : S = gadd(S, gmul(an, gammamellininvrt(K, nt, bitprec)));
927 2025755 : if ((n & 0x1ff) == 0) S = gerepileupto(av, S);
928 : }
929 4067 : if (m) S = gmul(S, gpowgs(isqN, m));
930 : }
931 51845 : return gerepileupto(ltop, S);
932 : }
933 :
934 : /*******************************************************************/
935 : /* Second part: Computation of L-Functions. */
936 : /*******************************************************************/
937 :
938 : struct lfunp {
939 : long precmax, Dmax, D, M, m0, nmax, d, vgaell;
940 : double k1, dc, dw, dh, MAXs, sub;
941 : GEN L, an, bn;
942 : };
943 :
944 : static void
945 26714 : lfunp_set(GEN ldata, long der, long bitprec, struct lfunp *S)
946 : {
947 26714 : const long derprec = (der > 1)? dbllog2(mpfact(der)): 0; /* log2(der!) */
948 : GEN Vga, N, L, k;
949 : long k1, d, m, M, flag, nmax;
950 : double a, A, E, hd, Ep, d2, suma, maxs, mins, sub, B0,B1;
951 : double logN2, logC, Lestimate, Mestimate;
952 :
953 26714 : Vga = ldata_get_gammavec(ldata);
954 26714 : S->d = d = lg(Vga)-1; d2 = d/2.;
955 :
956 26714 : suma = gtodouble(sumVga(Vga));
957 26714 : k = ldata_get_k(ldata);
958 26714 : N = ldata_get_conductor(ldata);
959 26714 : logN2 = log(gtodouble(N)) / 2;
960 26714 : maxs = S->dc + S->dw;
961 26714 : mins = S->dc - S->dw;
962 26714 : S->MAXs = maxdd(maxs, gtodouble(k)-mins);
963 :
964 : /* we compute Lambda^(der)(s) / der!; need to compensate for L^(der)(s)
965 : * ln |gamma(s)| ~ -(pi/4) \sum_i |Im(s + a_i)|; max with 1: fudge factor */
966 26714 : a = (M_PI/(4*M_LN2))*(d*S->dh + sumVgaimpos(Vga));
967 26714 : S->D = (long)ceil(bitprec + derprec + maxdd(a, 1));
968 26714 : E = M_LN2*S->D; /* D:= required absolute bitprec */
969 :
970 26714 : Ep = E + maxdd(M_PI * S->dh * d2, (d*S->MAXs + suma - 1) * log(E));
971 26714 : hd = d2*M_PI*M_PI / Ep;
972 26714 : S->m0 = (long)ceil(M_LN2/hd);
973 26714 : hd = M_LN2/S->m0;
974 :
975 26714 : logC = d2*M_LN2 - log(d2)/2;
976 26714 : k1 = maxdd(ldata_get_k1_dbl(ldata), 0.);
977 26714 : S->k1 = k1; /* assume |a_n| << n^k1 with small implied constant */
978 26714 : A = gammavec_expo(d, suma);
979 :
980 26714 : sub = 0.;
981 26714 : if (mins > 1)
982 : {
983 4942 : GEN sig = dbltor(mins);
984 4942 : sub += logN2*mins;
985 4942 : if (gammaordinary(Vga, sig))
986 : {
987 : long ext;
988 4851 : GEN gas = gammafactproduct(gammafactor(Vga), sig, &ext, LOWDEFAULTPREC);
989 4851 : if (typ(gas) != t_SER)
990 : {
991 4851 : double dg = dbllog2(gas);
992 4851 : if (dg > 0) sub += dg * M_LN2;
993 : }
994 : }
995 : }
996 26714 : S->sub = sub;
997 26714 : M = 1000;
998 26714 : L = cgetg(M+2, t_VECSMALL);
999 26714 : a = S->k1 + A;
1000 :
1001 26714 : B0 = 5 + E - S->sub + logC + S->k1*logN2; /* 5 extra bits */
1002 26714 : B1 = hd * (S->MAXs - S->k1);
1003 26714 : Lestimate = dblcoro526(a + S->MAXs - 2./d, d/2.,
1004 26714 : E - S->sub + logC - log(2*M_PI*hd) + S->MAXs*logN2);
1005 26714 : Mestimate = ((Lestimate > 0? log(Lestimate): 0) + logN2) / hd;
1006 26714 : nmax = 0;
1007 26714 : flag = 0;
1008 26714 : for (m = 0;; m++)
1009 2385502 : {
1010 2412216 : double x, H = logN2 - m*hd, B = B0 + m*B1;
1011 : long n;
1012 2412216 : x = dblcoro526(a, d/2., B);
1013 2412216 : n = floor(x*exp(H));
1014 2412216 : if (n > nmax) nmax = n;
1015 2412216 : if (m > M) { M *= 2; L = vecsmall_lengthen(L,M+2); }
1016 2412216 : L[m+1] = n;
1017 2412216 : if (n == 0) { if (++flag > 2 && m > Mestimate) break; } else flag = 0;
1018 : }
1019 27554 : m -= 2; while (m > 0 && !L[m]) m--;
1020 26714 : if (m == 0) { nmax = 1; L[1] = 1; m = 1; } /* can happen for tiny bitprec */
1021 26714 : setlg(L, m+1); S->M = m-1;
1022 26714 : S->L = L;
1023 26714 : S->nmax = nmax;
1024 :
1025 26714 : S->Dmax = S->D + (long)ceil((S->M * hd * S->MAXs - S->sub) / M_LN2);
1026 26714 : if (S->Dmax < S->D) S->Dmax = S->D;
1027 26714 : S->precmax = nbits2prec(S->Dmax);
1028 26714 : if (DEBUGLEVEL > 1)
1029 0 : err_printf("Dmax=%ld, D=%ld, M = %ld, nmax = %ld, m0 = %ld\n",
1030 : S->Dmax,S->D,S->M,S->nmax, S->m0);
1031 26714 : }
1032 :
1033 : static GEN
1034 10612 : lfuninit_pol(GEN v, GEN poqk, long prec)
1035 : {
1036 10612 : long m, M = lg(v) - 2;
1037 10612 : GEN pol = cgetg(M+3, t_POL);
1038 10612 : pol[1] = evalsigne(1) | evalvarn(0);
1039 10612 : gel(pol, 2) = gprec_w(gmul2n(gel(v,1), -1), prec);
1040 10612 : if (poqk)
1041 495687 : for (m = 2; m <= M+1; m++)
1042 485131 : gel(pol, m+1) = gprec_w(gmul(gel(poqk,m), gel(v,m)), prec);
1043 : else
1044 2324 : for (m = 2; m <= M+1; m++)
1045 2268 : gel(pol, m+1) = gprec_w(gel(v,m), prec);
1046 10612 : return RgX_renormalize_lg(pol, M+3);
1047 : }
1048 :
1049 : static void
1050 75495 : worker_init(long q, GEN *an, GEN *bn, GEN *AB, GEN *A, GEN *B)
1051 : {
1052 75495 : if (typ(*bn) == t_INT) *bn = NULL;
1053 75495 : if (*bn)
1054 : {
1055 712 : *AB = cgetg(3, t_VEC);
1056 712 : gel(*AB,1) = *A = cgetg(q+1, t_VEC);
1057 712 : gel(*AB,2) = *B = cgetg(q+1, t_VEC);
1058 712 : if (typ(an) == t_VEC) *an = RgV_kill0(*an);
1059 712 : if (typ(bn) == t_VEC) *bn = RgV_kill0(*bn);
1060 : }
1061 : else
1062 : {
1063 74783 : *B = NULL;
1064 74783 : *AB = *A = cgetg(q+1, t_VEC);
1065 74780 : if (typ(*an) == t_VEC) *an = RgV_kill0(*an);
1066 : }
1067 75494 : }
1068 : GEN
1069 22264 : lfuninit_theta2_worker(long r, GEN L, GEN qk, GEN a, GEN di, GEN an, GEN bn)
1070 : {
1071 22264 : long q, m, prec = di[1], M = di[2], m0 = di[3], L0 = lg(an)-1;
1072 : GEN AB, A, B;
1073 22264 : worker_init((M - r) / m0 + 1, &an, &bn, &AB, &A, &B);
1074 288276 : for (q = 0, m = r; m <= M; m += m0, q++)
1075 : {
1076 266014 : GEN t = gel(qk, m+1);
1077 266014 : long N = minss(L[m+1],L0);
1078 266011 : gel(A, q+1) = theta2(an, N, t, a, prec); /* theta(exp(mh)) */
1079 266014 : if (bn) gel(B, q+1) = theta2(bn, N, t, a, prec);
1080 : }
1081 22262 : return AB;
1082 : }
1083 :
1084 : /* theta(exp(mh)) ~ sum_{n <= N} a(n) k[m,n] */
1085 : static GEN
1086 224955 : an_msum(GEN an, long N, GEN vKm)
1087 : {
1088 224955 : pari_sp av = avma;
1089 224955 : GEN s = gen_0;
1090 : long n;
1091 12452910 : for (n = 1; n <= N; n++)
1092 12228621 : if (gel(vKm,n))
1093 : {
1094 6041426 : GEN c = mul_an(an, n, gel(vKm,n));
1095 6040061 : if (c) s = gadd(s, c);
1096 : }
1097 224289 : return gerepileupto(av, s);
1098 : }
1099 :
1100 : GEN
1101 53245 : lfuninit_worker(long r, GEN K, GEN L, GEN peh2d, GEN vroots, GEN dr, GEN di,
1102 : GEN an, GEN bn)
1103 : {
1104 53245 : pari_sp av0 = avma;
1105 53245 : long m, n, q, L0 = lg(an)-1;
1106 53245 : double sig0 = rtodbl(gel(dr,1)), sub2 = rtodbl(gel(dr,2));
1107 53239 : double k1 = rtodbl(gel(dr,3)), MAXs = rtodbl(gel(dr,4));
1108 53238 : long D = di[1], M = di[2], m0 = di[3];
1109 53238 : double M0 = sig0? sub2 / sig0: 1./0.;
1110 53238 : GEN AB, A, B, vK = cgetg(M/m0 + 2, t_VEC);
1111 :
1112 277161 : for (q = 0, m = r; m <= M; m += m0, q++)
1113 223931 : gel(vK, q+1) = const_vec(L[m+1], NULL);
1114 53230 : worker_init(q, &an, &bn, &AB, &A, &B);
1115 276448 : for (m -= m0, q--; m >= 0; m -= m0, q--)
1116 : {
1117 223979 : double c1 = D + ((m > M0)? m * sig0 - sub2 : 0);
1118 223979 : GEN vKm = gel(vK,q+1); /* conceptually K(m,n) */
1119 12458769 : for (n = 1; n <= L[m+1]; n++)
1120 : {
1121 : GEN t2d, kmn;
1122 12235553 : long nn, mm, qq, p = 0;
1123 : double c, c2;
1124 : pari_sp av;
1125 :
1126 12235553 : if (gel(vKm, n)) continue; /* done already */
1127 9195346 : c = c1 + k1 * log2(n);
1128 : /* n *= 2; m -= m0 => c += c2, provided m >= M0. Else c += k1 */
1129 9195346 : c2 = k1 - MAXs;
1130 : /* p = largest (absolute) accuracy to which we need K(m,n) */
1131 14621614 : for (mm=m,nn=n; mm >= M0;)
1132 : {
1133 11048224 : if (nn <= L[mm+1] && (gel(an, nn) || (bn && gel(bn, nn))))
1134 2990024 : if (c > 0) p = maxuu(p, (ulong)c);
1135 11048426 : nn <<= 1;
1136 11048426 : mm -= m0; if (mm >= M0) c += c2; else { c += k1; break; }
1137 : }
1138 : /* mm < M0 || nn > L[mm+1] */
1139 16344639 : for ( ; mm >= 0; nn<<=1,mm-=m0,c+=k1)
1140 7149129 : if (nn <= L[mm+1] && (gel(an, nn) || (bn && gel(bn, nn))))
1141 1727544 : if (c > 0) p = maxuu(p, (ulong)c);
1142 9195510 : if (!p) continue; /* a_{n 2^v} = 0 for all v in range */
1143 3004408 : av = avma;
1144 3004408 : t2d = mpmul(gel(vroots,n), gel(peh2d,m+1));/*(n exp(mh)/sqrt(N))^(2/d)*/
1145 3005019 : kmn = gerepileupto(av, gammamellininvrt(K, t2d, p));
1146 9190912 : for (qq=q,mm=m,nn=n; mm >= 0; nn<<=1,mm-=m0,qq--)
1147 6187431 : if (nn <= L[mm+1]) gmael(vK, qq+1, nn) = kmn;
1148 : }
1149 : }
1150 276426 : for (q = 0, m = r; m <= M; m += m0, q++)
1151 : {
1152 223945 : long N = minss(L0, L[m+1]);
1153 223942 : gel(A, q+1) = an_msum(an, N, gel(vK,q+1));
1154 223957 : if (bn) gel(B, q+1) = an_msum(bn, N, gel(vK,q+1));
1155 : }
1156 52481 : return gerepileupto(av0, AB);
1157 : }
1158 : /* return A = [\theta(exp(mh)), m=0..M], theta(t) = sum a(n) K(n/sqrt(N) t),
1159 : * h = log(2)/m0. If bn != NULL, return the pair [A, B] */
1160 : static GEN
1161 10458 : lfuninit_ab(GEN theta, GEN h, struct lfunp *S)
1162 : {
1163 10458 : const long M = S->M, prec = S->precmax;
1164 10458 : GEN tech = linit_get_tech(theta), isqN = theta_get_isqrtN(tech);
1165 10458 : GEN an = S->an, bn = S->bn, va, vb;
1166 : struct pari_mt pt;
1167 : GEN worker;
1168 : long m0, r, pending;
1169 :
1170 10458 : if (S->vgaell)
1171 : { /* d=2 and Vga = [a,a+1] */
1172 7084 : GEN a = vecmin(ldata_get_gammavec(linit_get_ldata(theta)));
1173 7084 : GEN qk = gpowers0(mpexp(h), M, isqN);
1174 7084 : m0 = minss(M+1, mt_nbthreads());
1175 7084 : worker = snm_closure(is_entry("_lfuninit_theta2_worker"),
1176 : mkvecn(6, S->L, qk, a, mkvecsmall3(prec, M, m0),
1177 : an, bn? bn: gen_0));
1178 : }
1179 : else
1180 : {
1181 : GEN vroots, peh2d, d2;
1182 3374 : double sig0 = S->MAXs / S->m0, sub2 = S->sub / M_LN2;
1183 : /* For all 0<= m <= M, and all n <= L[m+1] such that a_n!=0, we compute
1184 : * k[m,n] = K(n exp(mh)/sqrt(N))
1185 : * with ln(absolute error) <= E + max(mh sigma - sub, 0) + k1 * log(n).
1186 : * N.B. we use the 'rt' variant and pass (n exp(mh)/sqrt(N))^(2/d).
1187 : * Speedup: if n' = 2n and m' = m - m0 >= 0; then k[m,n] = k[m',n']. */
1188 3374 : vroots = mkvroots(S->d, S->nmax, prec); /* vroots[n] = n^(2/d) */
1189 3374 : d2 = gdivgu(gen_2, S->d);
1190 3374 : peh2d = gpowers0(gexp(gmul(d2,h), prec), M, gpow(isqN, d2, prec));
1191 3374 : m0 = S->m0; /* peh2d[m+1] = (exp(mh)/sqrt(N))^(2/d) */
1192 3374 : worker = snm_closure(is_entry("_lfuninit_worker"),
1193 : mkvecn(8, theta_get_K(tech), S->L, peh2d, vroots,
1194 : mkvec4(dbltor(sig0), dbltor(sub2),
1195 : dbltor(S->k1), dbltor(S->MAXs)),
1196 : mkvecsmall3(S->D, M, m0),
1197 : an, bn? bn: gen_0));
1198 : /* For each 0 <= m <= M, we will sum for n<=L[m+1] a(n) K(m,n)
1199 : * bit accuracy for K(m,n): D + k1*log2(n) + 1_{m > M0} (m*sig0 - sub2)
1200 : * We restrict m to arithmetic progressions r mod m0 to save memory and
1201 : * allow parallelization */
1202 : }
1203 10458 : va = cgetg(M+2, t_VEC);
1204 10458 : vb = bn? cgetg(M+2, t_VEC): NULL;
1205 10458 : mt_queue_start_lim(&pt, worker, m0);
1206 10458 : pending = 0;
1207 106693 : for (r = 0; r < m0 || pending; r++)
1208 : { /* m = q m0 + r */
1209 : GEN done, A, B;
1210 : long q, m, workid;
1211 96235 : mt_queue_submit(&pt, r, r < m0 ? mkvec(utoi(r)): NULL);
1212 96235 : done = mt_queue_get(&pt, &workid, &pending);
1213 96235 : if (!done) continue;
1214 75514 : if (bn) { A = gel(done,1); B = gel(done,2); } else { A = done; B = NULL; }
1215 565545 : for (q = 0, m = workid; m <= M; m += m0, q++)
1216 : {
1217 490031 : gel(va, m+1) = gel(A, q+1);
1218 490031 : if (bn) gel(vb, m+1) = gel(B, q+1);
1219 : }
1220 : }
1221 10458 : mt_queue_end(&pt);
1222 10458 : return bn? mkvec2(va, vb): va;
1223 : }
1224 :
1225 : static void
1226 139446 : parse_dom(double k, GEN dom, struct lfunp *S)
1227 : {
1228 139446 : long l = lg(dom);
1229 139446 : if (typ(dom)!=t_VEC) pari_err_TYPE("lfuninit [domain]", dom);
1230 139446 : if (l == 1)
1231 : {
1232 98 : S->dc = 0;
1233 98 : S->dw = -1;
1234 98 : S->dh = -1; return;
1235 : }
1236 139348 : if (l == 2)
1237 : {
1238 38124 : S->dc = k/2.;
1239 38124 : S->dw = 0.;
1240 38124 : S->dh = gtodouble(gel(dom,1));
1241 : }
1242 101224 : else if (l == 3)
1243 : {
1244 301 : S->dc = k/2.;
1245 301 : S->dw = gtodouble(gel(dom,1));
1246 301 : S->dh = gtodouble(gel(dom,2));
1247 : }
1248 100923 : else if (l == 4)
1249 : {
1250 100923 : S->dc = gtodouble(gel(dom,1));
1251 100923 : S->dw = gtodouble(gel(dom,2));
1252 100923 : S->dh = gtodouble(gel(dom,3));
1253 : }
1254 : else
1255 : {
1256 0 : pari_err_TYPE("lfuninit [domain]", dom);
1257 0 : S->dc = S->dw = S->dh = 0; /*-Wall*/
1258 : }
1259 139348 : if (S->dw < 0 || S->dh < 0) pari_err_TYPE("lfuninit [domain]", dom);
1260 : }
1261 :
1262 : /* do we have dom \subset dom0 ? dom = [center, width, height] */
1263 : int
1264 24763 : sdomain_isincl(double k, GEN dom, GEN dom0)
1265 : {
1266 : struct lfunp S0, S;
1267 24763 : parse_dom(k, dom, &S); if (S.dw < 0) return 1;
1268 24763 : parse_dom(k, dom0, &S0); if (S0.dw < 0) return 0;
1269 24763 : return S0.dc - S0.dw <= S.dc - S.dw
1270 24763 : && S0.dc + S0.dw >= S.dc + S.dw && S0.dh >= S.dh;
1271 : }
1272 :
1273 : static int
1274 24840 : checklfuninit(GEN linit, GEN DOM, long der, long bitprec)
1275 : {
1276 24840 : GEN ldata = linit_get_ldata(linit);
1277 24840 : GEN domain = lfun_get_domain(linit_get_tech(linit));
1278 24840 : GEN dom = domain_get_dom(domain);
1279 24840 : if (lg(dom) == 1) return 1;
1280 24763 : return domain_get_der(domain) >= der
1281 24763 : && domain_get_bitprec(domain) >= bitprec
1282 49526 : && sdomain_isincl(gtodouble(ldata_get_k(ldata)), DOM, dom);
1283 : }
1284 :
1285 : static GEN
1286 2219 : ginvsqrtvec(GEN x, long prec)
1287 : {
1288 2219 : if (is_vec_t(typ(x)))
1289 1085 : pari_APPLY_same(ginv(gsqrt(gel(x,i), prec)))
1290 1911 : else return ginv(gsqrt(x, prec));
1291 : }
1292 :
1293 : GEN
1294 11550 : lfuninit_make(long t, GEN ldata, GEN tech, GEN domain)
1295 : {
1296 11550 : GEN Vga = ldata_get_gammavec(ldata);
1297 11550 : long d = lg(Vga)-1;
1298 11550 : GEN w2 = gen_1, k2 = gmul2n(ldata_get_k(ldata), -1);
1299 11550 : GEN expot = gdivgu(gadd(gmulsg(d, gsubgs(k2, 1)), sumVga(Vga)), 4);
1300 11550 : if (typ(ldata_get_dual(ldata))==t_INT)
1301 : {
1302 11396 : GEN eno = ldata_get_rootno(ldata);
1303 11396 : long prec = nbits2prec( domain_get_bitprec(domain) );
1304 11396 : if (!isint1(eno)) w2 = ginvsqrtvec(eno, prec);
1305 : }
1306 11550 : tech = mkvec3(domain, tech, mkvec4(k2, w2, expot, gammafactor(Vga)));
1307 11550 : return mkvec3(mkvecsmall(t), ldata, tech);
1308 : }
1309 : static GEN
1310 224 : lfunnoinit(GEN ldata, long bitprec)
1311 : {
1312 224 : GEN tech, domain = mkvec2(cgetg(1,t_VEC), mkvecsmall2(0, bitprec));
1313 224 : GEN R = gen_0, r = ldata_get_residue(ldata), v = lfunrootres(ldata, bitprec);
1314 224 : ldata = shallowcopy(ldata);
1315 224 : gel(ldata,6) = gel(v,3);
1316 224 : if (r)
1317 : {
1318 196 : if (isintzero(r)) setlg(ldata,7); else gel(ldata,7) = r;
1319 196 : R = gel(v,2);
1320 : }
1321 224 : tech = mkvec3(domain, gen_0, R);
1322 224 : return lfuninit_make(t_LDESC_INIT, ldata, tech, domain);
1323 : }
1324 :
1325 : static void
1326 3374 : lfunparams2(struct lfunp *S)
1327 : {
1328 3374 : GEN L = S->L, an = S->an, bn = S->bn;
1329 : double pmax;
1330 3374 : long m, nan, nmax, neval, M = S->M;
1331 :
1332 3374 : S->vgaell = 0;
1333 : /* try to reduce parameters now we know the a_n (some may be 0) */
1334 3374 : if (typ(an) == t_VEC) an = RgV_kill0(an);
1335 3374 : nan = S->nmax; /* lg(an)-1 may be large than this */
1336 3374 : nmax = neval = 0;
1337 3374 : if (!bn)
1338 226348 : for (m = 0; m <= M; m++)
1339 : {
1340 222995 : long n = minss(nan, L[m+1]);
1341 319889 : while (n > 0 && !gel(an,n)) n--;
1342 222995 : if (n > nmax) nmax = n;
1343 222995 : neval += n;
1344 222995 : L[m+1] = n; /* reduce S->L[m+1] */
1345 : }
1346 : else
1347 : {
1348 21 : if (typ(bn) == t_VEC) bn = RgV_kill0(bn);
1349 1036 : for (m = 0; m <= M; m++)
1350 : {
1351 1015 : long n = minss(nan, L[m+1]);
1352 1057 : while (n > 0 && !gel(an,n) && !gel(bn,n)) n--;
1353 1015 : if (n > nmax) nmax = n;
1354 1015 : neval += n;
1355 1015 : L[m+1] = n; /* reduce S->L[m+1] */
1356 : }
1357 : }
1358 3374 : if (DEBUGLEVEL >= 1) err_printf("expected evaluations: %ld\n", neval);
1359 3374 : for (; M > 0; M--)
1360 3374 : if (L[M+1]) break;
1361 3374 : setlg(L, M+2);
1362 3374 : S->M = M;
1363 3374 : S->nmax = nmax;
1364 :
1365 : /* need K(n*exp(mh)/sqrt(N)) to absolute accuracy
1366 : * D + k1*log(n) + max(m * sig0 - sub2, 0) */
1367 3374 : pmax = S->D + S->k1 * log2(L[1]);
1368 3374 : if (S->MAXs)
1369 : {
1370 3374 : double sig0 = S->MAXs/S->m0, sub2 = S->sub / M_LN2;
1371 194420 : for (m = ceil(sub2 / sig0); m <= S->M; m++)
1372 : {
1373 191046 : double c = S->D + m*sig0 - sub2;
1374 191046 : if (S->k1 > 0) c += S->k1 * log2(L[m+1]);
1375 191046 : pmax = maxdd(pmax, c);
1376 : }
1377 : }
1378 3374 : S->Dmax = pmax;
1379 3374 : S->precmax = nbits2prec(pmax);
1380 3374 : }
1381 :
1382 : static GEN
1383 10472 : lfun_init_theta(GEN ldata, GEN eno, struct lfunp *S)
1384 : {
1385 10472 : GEN an2, dual, tdom = NULL, Vga = ldata_get_gammavec(ldata);
1386 10472 : long L, prec = S->precmax;
1387 10472 : if (eno)
1388 6489 : L = S->nmax;
1389 : else
1390 : {
1391 3983 : tdom = dbltor(sqrt(0.5));
1392 3983 : L = maxss(S->nmax, lfunthetacost(ldata, tdom, 0, S->D));
1393 : }
1394 10472 : dual = ldata_get_dual(ldata);
1395 10472 : S->an = ldata_vecan(ldata_get_an(ldata), L, prec);
1396 10458 : S->bn = typ(dual)==t_INT? NULL: ldata_vecan(dual, S->nmax, prec);
1397 10458 : if (!vgaell(Vga)) lfunparams2(S);
1398 : else
1399 : {
1400 7084 : S->an = antwist(S->an, Vga, prec);
1401 7084 : if (S->bn) S->bn = antwist(S->bn, Vga, prec);
1402 7084 : S->vgaell = 1;
1403 : }
1404 10458 : an2 = lg(Vga)-1 == 1? antwist(S->an, Vga, prec): S->an;
1405 10458 : return lfunthetainit0(ldata, tdom, an2, 0, S->Dmax, 0);
1406 : }
1407 :
1408 : GEN
1409 16242 : lfuncost(GEN L, GEN dom, long der, long bit)
1410 : {
1411 16242 : pari_sp av = avma;
1412 16242 : GEN ldata = lfunmisc_to_ldata_shallow(L);
1413 16242 : GEN w, k = ldata_get_k(ldata);
1414 : struct lfunp S;
1415 :
1416 16242 : parse_dom(gtodouble(k), dom, &S); if (S.dw < 0) return mkvecsmall2(0, 0);
1417 16242 : lfunp_set(ldata, der, bit, &S);
1418 16242 : w = ldata_get_rootno(ldata);
1419 16242 : if (isintzero(w)) /* for lfunrootres */
1420 7 : S.nmax = maxss(S.nmax, lfunthetacost(ldata, dbltor(sqrt(0.5)), 0, bit+1));
1421 16242 : set_avma(av); return mkvecsmall2(S.nmax, S.Dmax);
1422 : }
1423 : GEN
1424 49 : lfuncost0(GEN L, GEN dom, long der, long bitprec)
1425 : {
1426 49 : pari_sp av = avma;
1427 : GEN C;
1428 :
1429 49 : if (is_linit(L))
1430 : {
1431 28 : GEN tech = linit_get_tech(L);
1432 28 : GEN domain = lfun_get_domain(tech);
1433 28 : dom = domain_get_dom(domain);
1434 28 : der = domain_get_der(domain);
1435 28 : bitprec = domain_get_bitprec(domain);
1436 28 : if (linit_get_type(L) == t_LDESC_PRODUCT)
1437 : {
1438 21 : GEN v = lfunprod_get_fact(linit_get_tech(L)), F = gel(v,1);
1439 21 : long i, l = lg(F);
1440 21 : C = cgetg(l, t_VEC);
1441 70 : for (i = 1; i < l; ++i)
1442 49 : gel(C, i) = zv_to_ZV( lfuncost(gel(F,i), dom, der, bitprec) );
1443 21 : return gerepileupto(av, C);
1444 : }
1445 : }
1446 28 : if (!dom) pari_err_TYPE("lfuncost [missing s domain]", L);
1447 28 : C = lfuncost(L,dom,der,bitprec);
1448 28 : return gerepileupto(av, zv_to_ZV(C));
1449 : }
1450 :
1451 : static int
1452 9450 : is_dirichlet(GEN ldata)
1453 : {
1454 9450 : switch(ldata_get_type(ldata))
1455 : {
1456 1316 : case t_LFUN_ZETA:
1457 : case t_LFUN_KRONECKER:
1458 1316 : case t_LFUN_CHIZ: return 1;
1459 770 : case t_LFUN_CHIGEN: return ldata_get_degree(ldata)==1;
1460 7364 : default: return 0;
1461 : }
1462 : }
1463 :
1464 : static ulong
1465 10568 : lfuninit_cutoff(GEN ldata)
1466 : {
1467 10568 : GEN gN = ldata_get_conductor(ldata);
1468 : ulong L, N;
1469 10568 : if (ldata_get_type(ldata) == t_LFUN_NF) /* N ~ f^(d-1), exact for d prime */
1470 728 : gN = sqrtnint(gN, ldata_get_degree(ldata) - 1);
1471 10568 : N = itou_or_0(gN);
1472 10568 : if (N > 1000) L = 7000;
1473 10554 : else if (N > 100) L = 5000;
1474 7593 : else if (N > 15) L = 3000;
1475 7404 : else L = 2500;
1476 10568 : return L;
1477 : }
1478 :
1479 : GEN
1480 36096 : lfuninit(GEN lmisc, GEN dom, long der, long bitprec)
1481 : {
1482 36096 : pari_sp av = avma;
1483 : GEN poqk, AB, R, h, theta, ldata, eno, r, domain, tech, k;
1484 : struct lfunp S;
1485 :
1486 36096 : if (is_linit(lmisc))
1487 : {
1488 24889 : long t = linit_get_type(lmisc);
1489 24889 : if (t == t_LDESC_INIT || t == t_LDESC_PRODUCT)
1490 : {
1491 24840 : if (checklfuninit(lmisc, dom, der, bitprec)) return lmisc;
1492 21 : pari_warn(warner,"lfuninit: insufficient initialization");
1493 : }
1494 : }
1495 11277 : ldata = lfunmisc_to_ldata_shallow(lmisc);
1496 :
1497 11277 : switch (ldata_get_type(ldata))
1498 : {
1499 616 : case t_LFUN_NF:
1500 : {
1501 616 : GEN T = gel(ldata_get_an(ldata), 2);
1502 616 : return gerepilecopy(av, lfunzetakinit(T, dom, der, bitprec));
1503 : }
1504 91 : case t_LFUN_ABELREL:
1505 : {
1506 91 : GEN T = gel(ldata_get_an(ldata), 2);
1507 91 : return gerepilecopy(av, lfunabelianrelinit(gel(T,1), gel(T,2), dom, der, bitprec));
1508 : }
1509 : }
1510 10570 : k = ldata_get_k(ldata);
1511 10570 : parse_dom(gtodouble(k), dom, &S);
1512 : /* Reduce domain for Dirichlet characters. NOT for Abelian t_LFUN_NF,
1513 : * handled above. */
1514 10570 : if (S.dw >= 0 && (!der && is_dirichlet(ldata)))
1515 1526 : S.dh = mindd(S.dh, lfuninit_cutoff(ldata));
1516 10570 : if (S.dw < 0)
1517 : {
1518 98 : if (der)
1519 0 : pari_err_IMPL("domain = [] for derivatives in lfuninit");
1520 98 : if (!is_dirichlet(ldata))
1521 0 : pari_err_IMPL("domain = [] for L functions of degree > 1");
1522 98 : return gerepilecopy(av, lfunnoinit(ldata, bitprec));
1523 : }
1524 :
1525 10472 : lfunp_set(ldata, der, bitprec, &S);
1526 10472 : ldata = ldata_newprec(ldata, nbits2prec(S.Dmax));
1527 10472 : r = ldata_get_residue(ldata);
1528 : /* Note: all guesses should already have been performed (thetainit more
1529 : * expensive than needed: should be either tdom = 1 or bitprec = S.D).
1530 : * BUT if the root number / polar part do not have an algebraic
1531 : * expression, there is no way to do this until we know the
1532 : * precision, i.e. now. So we can't remove guessing code from here and
1533 : * lfun_init_theta */
1534 10472 : if (r && isintzero(r)) eno = NULL;
1535 : else
1536 : {
1537 10472 : eno = ldata_get_rootno(ldata);
1538 10472 : if (isintzero(eno)) eno = NULL;
1539 : }
1540 10472 : theta = lfun_init_theta(ldata, eno, &S);
1541 10458 : if (eno && !r)
1542 4578 : R = gen_0;
1543 : else
1544 : {
1545 5880 : GEN v = lfunrootres(theta, S.D);
1546 5880 : ldata = shallowcopy(ldata);
1547 5880 : gel(ldata, 6) = gel(v,3);
1548 5880 : r = gel(v,1);
1549 5880 : R = gel(v,2);
1550 5880 : if (isintzero(r)) setlg(ldata,7); else gel(ldata, 7) = r;
1551 : }
1552 10458 : h = divru(mplog2(S.precmax), S.m0);
1553 : /* exp(kh/2 . [0..M]) */
1554 10458 : poqk = gequal0(k) ? NULL
1555 10458 : : gpowers(gprec_w(mpexp(gmul2n(gmul(k,h), -1)), S.precmax), S.M);
1556 10458 : AB = lfuninit_ab(theta, h, &S);
1557 10458 : if (S.bn)
1558 : {
1559 154 : GEN A = gel(AB,1), B = gel(AB,2);
1560 154 : A = lfuninit_pol(A, poqk, S.precmax);
1561 154 : B = lfuninit_pol(B, poqk, S.precmax);
1562 154 : AB = mkvec2(A, B);
1563 : }
1564 : else
1565 10304 : AB = lfuninit_pol(AB, poqk, S.precmax);
1566 10458 : tech = mkvec3(h, AB, R);
1567 10458 : domain = mkvec2(dom, mkvecsmall2(der, bitprec));
1568 10458 : return gerepilecopy(av, lfuninit_make(t_LDESC_INIT, ldata, tech, domain));
1569 : }
1570 :
1571 : GEN
1572 532 : lfuninit0(GEN lmisc, GEN dom, long der, long bitprec)
1573 : {
1574 532 : GEN z = lfuninit(lmisc, dom, der, bitprec);
1575 532 : return z == lmisc? gcopy(z): z;
1576 : }
1577 :
1578 : /* If s is a pole of Lambda, return polar part at s; else return NULL */
1579 : static GEN
1580 5151 : lfunpoleresidue(GEN R, GEN s)
1581 : {
1582 : long j;
1583 14676 : for (j = 1; j < lg(R); j++)
1584 : {
1585 10085 : GEN Rj = gel(R, j), be = gel(Rj, 1);
1586 10085 : if (gequal(s, be)) return gel(Rj, 2);
1587 : }
1588 4591 : return NULL;
1589 : }
1590 :
1591 : /* Compute contribution of polar part at s when not a pole. */
1592 : static GEN
1593 8377 : veccothderivn(GEN a, long n)
1594 : {
1595 : long i;
1596 8377 : pari_sp av = avma;
1597 8377 : GEN c = pol_x(0), cp = mkpoln(3, gen_m1, gen_0, gen_1);
1598 8377 : GEN v = cgetg(n+2, t_VEC);
1599 8377 : gel(v, 1) = poleval(c, a);
1600 25250 : for(i = 2; i <= n+1; i++)
1601 : {
1602 16873 : c = ZX_mul(ZX_deriv(c), cp);
1603 16873 : gel(v, i) = gdiv(poleval(c, a), mpfact(i-1));
1604 : }
1605 8377 : return gerepilecopy(av, v);
1606 : }
1607 :
1608 : static GEN
1609 8496 : polepart(long n, GEN h, GEN C)
1610 : {
1611 8496 : GEN h2n = gpowgs(gdiv(h, gen_2), n-1);
1612 8496 : GEN res = gmul(h2n, gel(C,n));
1613 8496 : return odd(n)? res : gneg(res);
1614 : }
1615 :
1616 : static GEN
1617 4157 : lfunsumcoth(GEN R, GEN s, GEN h, long prec)
1618 : {
1619 : long i,j;
1620 4157 : GEN S = gen_0;
1621 12534 : for (j = 1; j < lg(R); ++j)
1622 : {
1623 8377 : GEN r = gel(R,j), be = gel(r,1), Rj = gel(r, 2);
1624 8377 : long e = valser(Rj);
1625 8377 : GEN z1 = gexpm1(gmul(h, gsub(s,be)), prec); /* exp(h(s-beta))-1 */
1626 8377 : GEN c1 = gaddgs(gdivsg(2, z1), 1); /* coth((h/2)(s-beta)) */
1627 8377 : GEN C1 = veccothderivn(c1, 1-e);
1628 16873 : for (i = e; i < 0; i++)
1629 : {
1630 8496 : GEN Rbe = mysercoeff(Rj, i);
1631 8496 : GEN p1 = polepart(-i, h, C1);
1632 8496 : S = gadd(S, gmul(Rbe, p1));
1633 : }
1634 : }
1635 4157 : return gmul2n(S, -1);
1636 : }
1637 :
1638 : static GEN lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec);
1639 : /* L is a t_LDESC_PRODUCT or t_LDESC_INIT Linit */
1640 : static GEN
1641 2442 : _product(GEN (*fun)(GEN,GEN,long), GEN L, GEN s, long bitprec)
1642 : {
1643 2442 : GEN ldata = linit_get_ldata(L), v, r, F, E, C, cs;
1644 : long i, l;
1645 : int isreal;
1646 2442 : if (linit_get_type(L) == t_LDESC_INIT) return fun(ldata, s, bitprec);
1647 1910 : v = lfunprod_get_fact(linit_get_tech(L));
1648 1910 : F = gel(v,1); E = gel(v,2); C = gel(v,3); l = lg(F);
1649 1910 : cs = conj_i(s); isreal = gequal(imag_i(s), imag_i(cs));
1650 6136 : for (i = 1, r = gen_1; i < l; ++i)
1651 : {
1652 4226 : GEN f = fun(gel(F, i), s, bitprec);
1653 4226 : if (typ(f)==t_VEC) f = RgV_prod(f);
1654 4226 : if (E[i]) r = gmul(r, gpowgs(f, E[i]));
1655 4226 : if (C[i])
1656 : {
1657 0 : GEN fc = isreal? f: conj_i(fun(gel(F, i), cs, bitprec));
1658 0 : r = gmul(r, gpowgs(fc, C[i]));
1659 : }
1660 : }
1661 1910 : return (ldata_isreal(ldata) && gequal0(imag_i(s)))? real_i(r): r;
1662 : }
1663 :
1664 : /* s a t_SER; # terms - 1 */
1665 : static long
1666 2248 : der_level(GEN s)
1667 2248 : { return signe(s)? lg(s)-3: valser(s)-1; }
1668 :
1669 : /* s a t_SER; return coeff(s, X^0) */
1670 : static GEN
1671 1232 : ser_coeff0(GEN s) { return simplify_shallow(polcoef_i(s, 0, -1)); }
1672 :
1673 : static GEN
1674 19514 : get_domain(GEN s, GEN *dom, long *der)
1675 : {
1676 19514 : GEN sa = s;
1677 19514 : *der = 0;
1678 19514 : switch(typ(s))
1679 : {
1680 7 : case t_POL:
1681 7 : case t_RFRAC: s = toser_i(s);
1682 1232 : case t_SER:
1683 1232 : *der = der_level(s);
1684 1232 : sa = ser_coeff0(s);
1685 : }
1686 19514 : *dom = mkvec3(real_i(sa), gen_0, gabs(imag_i(sa),DEFAULTPREC));
1687 19514 : return s;
1688 : }
1689 : /* assume s went through get_domain and s/bitprec belong to domain */
1690 : static GEN
1691 33219 : lfunlambda_OK(GEN linit, GEN s, GEN sdom, long bitprec)
1692 : {
1693 33219 : GEN dom, eno, ldata, tech, h, pol, k2, cost, S, S0 = NULL;
1694 : long prec, prec0;
1695 : struct lfunp D, D0;
1696 :
1697 33219 : if (linit_get_type(linit) == t_LDESC_PRODUCT)
1698 1665 : return _product(&lfunlambda, linit, s, bitprec);
1699 31554 : ldata = linit_get_ldata(linit);
1700 31554 : eno = ldata_get_rootno(ldata);
1701 31554 : tech = linit_get_tech(linit);
1702 31554 : dom = lfun_get_dom(tech);
1703 31554 : if (lg(dom) == 1) return lfunlambda(linit, s, bitprec); /* FIXME:not OK! */
1704 31554 : h = lfun_get_step(tech); prec = realprec(h);
1705 : /* try to reduce accuracy */
1706 31554 : parse_dom(0, sdom, &D0);
1707 31554 : parse_dom(0, dom, &D);
1708 31554 : if (0.8 * D.dh > D0.dh)
1709 : {
1710 16165 : cost = lfuncost(linit, sdom, typ(s)==t_SER? der_level(s): 0, bitprec);
1711 16165 : prec0 = nbits2prec(cost[2]);
1712 16165 : if (prec0 < prec) { prec = prec0; h = gprec_w(h, prec); }
1713 : }
1714 31554 : pol = lfun_get_pol(tech);
1715 31554 : s = gprec_w(s, prec);
1716 31554 : if (ldata_get_residue(ldata))
1717 : {
1718 4556 : GEN R = lfun_get_Residue(tech);
1719 4556 : GEN Ra = lfunpoleresidue(R, s);
1720 4556 : if (Ra) return gprec_w(Ra, nbits2prec(bitprec));
1721 4157 : S0 = lfunsumcoth(R, s, h, prec);
1722 : }
1723 31155 : k2 = lfun_get_k2(tech);
1724 31155 : if (typ(pol)==t_POL && typ(s) != t_SER && gequal(real_i(s), k2))
1725 24988 : { /* on critical line: shortcut */
1726 24988 : GEN polz, b = imag_i(s);
1727 24988 : polz = gequal0(b)? poleval(pol,gen_1): poleval(pol, expIr(gmul(h,b)));
1728 24988 : S = gadd(polz, gmulvec(eno, conj_i(polz)));
1729 : }
1730 : else
1731 : {
1732 6167 : GEN z = gexp(gmul(h, gsub(s, k2)), prec);
1733 6167 : GEN zi = ginv(z), zc = conj_i(zi);
1734 6167 : if (typ(pol)==t_POL)
1735 5971 : S = gadd(poleval(pol, z), gmulvec(eno, conj_i(poleval(pol, zc))));
1736 : else
1737 196 : S = gadd(poleval(gel(pol,1), z), gmulvec(eno, poleval(gel(pol,2), zi)));
1738 : }
1739 31155 : if (S0) S = gadd(S,S0);
1740 31155 : return gprec_w(gmul(S,h), nbits2prec(bitprec));
1741 : }
1742 :
1743 : static long
1744 37972 : lfunspec_OK(GEN lmisc, GEN s, GEN *pldata)
1745 : {
1746 37972 : long t, large = 0;
1747 : GEN ldata;
1748 37972 : *pldata = ldata = lfunmisc_to_ldata_shallow(lmisc);
1749 37965 : if (!is_linit(lmisc)) lmisc = ldata;
1750 25561 : else switch(linit_get_type(lmisc))
1751 : {
1752 25519 : case t_LDESC_INIT: case t_LDESC_PRODUCT:
1753 25519 : if (lg(lfun_get_dom(linit_get_tech(lmisc))) == 1) large = 1;
1754 25519 : break;
1755 42 : default: return 0;
1756 : }
1757 37923 : switch(typ(s))
1758 : {
1759 37580 : case t_INT: case t_REAL: case t_FRAC: case t_COMPLEX: break;
1760 343 : default: return 0;
1761 : }
1762 37580 : t = ldata_get_type(ldata);
1763 37580 : switch(t)
1764 : {
1765 7902 : case t_LFUN_KRONECKER: case t_LFUN_ZETA:
1766 7902 : if (typ(s) == t_INT && !is_bigint(s)) return 1;
1767 : /* fall through */
1768 : case t_LFUN_NF: case t_LFUN_CHIZ:
1769 8042 : if (!large)
1770 7748 : large = (fabs(gtodouble(imag_i(s))) >= lfuninit_cutoff(ldata));
1771 8042 : break;
1772 1847 : case t_LFUN_CHIGEN:
1773 1847 : if (ldata_get_degree(ldata) != 1) return 0;
1774 1525 : if (!large)
1775 1273 : large = (fabs(gtodouble(imag_i(s))) >= lfuninit_cutoff(ldata));
1776 1525 : break;
1777 : }
1778 33205 : if (large)
1779 : {
1780 1904 : if (t == t_LFUN_NF)
1781 : {
1782 14 : GEN an = ldata_get_an(ldata), nf = gel(an,2), G = galoisinit(nf, NULL);
1783 14 : if (isintzero(G) || !group_isabelian(galois_group(G))) return 0;
1784 : }
1785 1904 : return 2;
1786 : }
1787 31301 : return 0;
1788 : }
1789 :
1790 : GEN
1791 4814 : lfunlambda(GEN lmisc, GEN s, long bitprec)
1792 : {
1793 4814 : pari_sp av = avma;
1794 4814 : GEN linit = NULL, dom, z;
1795 : long der;
1796 4814 : s = get_domain(s, &dom, &der);
1797 4814 : if (!der)
1798 : {
1799 : GEN ldata;
1800 3967 : long t = lfunspec_OK(lmisc, s, &ldata);
1801 3967 : if (t == 1)
1802 : { /* special value ? */
1803 539 : GEN z = lfun(ldata, s, bitprec), gv = ldata_get_gammavec(ldata);
1804 539 : long e = itou(gel(gv, 1));
1805 539 : if (!isintzero(z) && (e || gsigne(s) > 0)) /* TODO */
1806 : {
1807 462 : GEN q = ldata_get_conductor(ldata);
1808 462 : long prec = nbits2prec(bitprec);
1809 462 : GEN se, r, Q = divir(q, mppi(prec));
1810 462 : se = gmul2n(gaddgs(s, e), -1);
1811 462 : r = gmul(gpow(Q, se, prec), ggamma(se, prec));
1812 462 : if (e && !equali1(q)) r = gdiv(r, gsqrt(q, prec));
1813 490 : return gerepileupto(av, gmul(r, z));
1814 : }
1815 : }
1816 3505 : if (is_linit(lmisc)) linit = lmisc; else lmisc = ldata;
1817 3505 : if (t == 2)
1818 28 : return gerepilecopy(av, linit? _product(&lfunlambda, linit, s, bitprec)
1819 0 : : lfunlambdalarge(ldata, s, bitprec));
1820 : }
1821 4324 : linit = lfuninit(lmisc, dom, der, bitprec);
1822 4324 : z = lfunlambda_OK(linit,s, dom, bitprec);
1823 4324 : return gerepilecopy(av, z);
1824 : }
1825 :
1826 : static long
1827 18375 : is_ser(GEN x)
1828 : {
1829 18375 : long t = typ(x);
1830 18375 : if (t == t_SER) return 1;
1831 16394 : if (!is_vec_t(t) || lg(x)==1) return 0;
1832 350 : if (typ(gel(x,1))==t_SER) return 1;
1833 252 : return 0;
1834 : }
1835 :
1836 : static GEN
1837 371 : lfunser(GEN L)
1838 : {
1839 371 : long v = valser(L);
1840 371 : if (v > 0) return gen_0;
1841 329 : if (v == 0) L = gel(L, 2);
1842 : else
1843 203 : setlg(L, minss(lg(L), 2-v));
1844 329 : return L;
1845 : }
1846 :
1847 : static GEN
1848 371 : lfunservec(GEN x)
1849 : {
1850 371 : if (typ(x)==t_SER) return lfunser(x);
1851 0 : pari_APPLY_same(lfunser(gel(x,i)))
1852 : }
1853 : static GEN
1854 105 : lfununext(GEN L)
1855 : {
1856 105 : setlg(L, maxss(lg(L)-1, valser(L)? 2: 3));
1857 105 : return normalizeser(L);
1858 : }
1859 : static GEN
1860 105 : lfununextvec(GEN x)
1861 : {
1862 105 : if (typ(x)==t_SER) return lfununext(x);
1863 0 : pari_APPLY_same(lfununext(gel(x,i)));
1864 : }
1865 :
1866 : /* assume lmisc is an linit, s went through get_domain and s/bitprec belong
1867 : * to domain */
1868 : static GEN
1869 9814 : lfun_OK(GEN linit, GEN s, GEN sdom, long bitprec)
1870 : {
1871 9814 : GEN N, gas, S, FVga, res, ss = s;
1872 9814 : long prec = nbits2prec(bitprec), ext;
1873 :
1874 9814 : FVga = lfun_get_factgammavec(linit_get_tech(linit));
1875 9814 : S = lfunlambda_OK(linit, s, sdom, bitprec);
1876 9814 : if (is_ser(S))
1877 : {
1878 1645 : GEN r = typ(S)==t_SER ? S : gel(S,1);
1879 1645 : long d = lg(r) - 2 + fracgammadegree(FVga);
1880 1645 : if (typ(s) == t_SER)
1881 1316 : ss = sertoser(s, d);
1882 : else
1883 329 : ss = deg1ser_shallow(gen_1, s, varn(r), d);
1884 : }
1885 9814 : gas = gammafactproduct(FVga, ss, &ext, prec);
1886 9814 : N = ldata_get_conductor(linit_get_ldata(linit));
1887 9814 : res = gdiv(S, gmul(gpow(N, gdivgu(ss, 2), prec), gas));
1888 9814 : if (typ(s) != t_SER && is_ser(res)) res = lfunservec(res);
1889 9443 : else if (ext) res = lfununextvec(res);
1890 9814 : return gprec_w(res, prec);
1891 : }
1892 :
1893 : GEN
1894 13083 : lfun(GEN lmisc, GEN s, long bitprec)
1895 : {
1896 13083 : pari_sp av = avma;
1897 13083 : GEN linit = NULL, ldata, dom, z;
1898 : long der;
1899 13083 : s = get_domain(s, &dom, &der);
1900 13083 : if (der && typ(s) != t_SER)
1901 : {
1902 0 : if (lfunspec_OK(lmisc, s, &ldata))
1903 : {
1904 0 : linit = lfuninit(lmisc, cgetg(1,t_VEC), 0, bitprec);
1905 0 : return derivnumk((void*)linit, (GEN(*)(void*,GEN,long))&lfun,
1906 : s, stoi(der), nbits2prec(bitprec));
1907 : }
1908 : }
1909 : else
1910 : {
1911 13083 : long t = lfunspec_OK(lmisc, s, &ldata);
1912 13076 : if (t == 1)
1913 : { /* special value ? */
1914 3339 : long D = itos_or_0(gel(ldata_get_an(ldata), 2)), ss = itos(s);
1915 3339 : if (D)
1916 : {
1917 3339 : if (ss <= 0) return lfunquadneg(D, ss);
1918 : /* ss > 0 */
1919 756 : if ((!odd(ss) && D > 0) || (odd(ss) && D < 0))
1920 : {
1921 658 : long prec = nbits2prec(bitprec), q = labs(D);
1922 658 : ss = 1 - ss; /* <= 0 */
1923 658 : z = powrs(divrs(mppi(prec + EXTRAPREC64), q), 1-ss);
1924 658 : z = mulrr(shiftr(z, -ss), sqrtr_abs(utor(q, prec)));
1925 658 : z = gdiv(z, mpfactr(-ss, prec));
1926 658 : if (smodss(ss, 4) > 1) togglesign(z);
1927 658 : return gmul(z, lfunquadneg(D, ss));
1928 : }
1929 : }
1930 : }
1931 9835 : if (is_linit(lmisc)) linit = lmisc; else lmisc = ldata;
1932 9835 : if (t == 2)
1933 1211 : return gerepilecopy(av, linit? _product(&lfun, linit, s, bitprec)
1934 231 : : lfunlarge(ldata, s, bitprec));
1935 : }
1936 8855 : linit = lfuninit(lmisc, dom, der, bitprec);
1937 8841 : z = lfun_OK(linit, s, dom, bitprec);
1938 8841 : return gerepilecopy(av, z);
1939 : }
1940 :
1941 : /* given a t_SER a+x*s(x), return x*s(x), shallow */
1942 : static GEN
1943 42 : sersplit1(GEN s, GEN *head)
1944 : {
1945 42 : long i, l = lg(s);
1946 : GEN y;
1947 42 : *head = simplify_shallow(mysercoeff(s, 0));
1948 42 : if (valser(s) > 0) return s;
1949 28 : y = cgetg(l-1, t_SER); y[1] = s[1];
1950 28 : setvalser(y, 1);
1951 140 : for (i=3; i < l; i++) gel(y,i-1) = gel(s,i);
1952 28 : return normalizeser(y);
1953 : }
1954 :
1955 : /* order of pole of Lambda at s (0 if regular point) */
1956 : static long
1957 2226 : lfunlambdaord(GEN linit, GEN s)
1958 : {
1959 2226 : GEN tech = linit_get_tech(linit);
1960 2226 : if (linit_get_type(linit)==t_LDESC_PRODUCT)
1961 : {
1962 287 : GEN v = lfunprod_get_fact(linit_get_tech(linit));
1963 287 : GEN F = gel(v, 1), E = gel(v, 2), C = gel(v, 3);
1964 287 : long i, ex = 0, l = lg(F);
1965 980 : for (i = 1; i < l; i++)
1966 693 : ex += lfunlambdaord(gel(F,i), s) * (E[i]+C[i]);
1967 287 : return ex;
1968 : }
1969 1939 : if (ldata_get_residue(linit_get_ldata(linit)))
1970 : {
1971 595 : GEN r = lfunpoleresidue(lfun_get_Residue(tech), s);
1972 595 : if (r) return lg(r)-2;
1973 : }
1974 1778 : return 0;
1975 : }
1976 :
1977 : static GEN
1978 126 : derser(GEN res, long m)
1979 : {
1980 126 : long v = valser(res);
1981 126 : if (v > m) return gen_0;
1982 126 : if (v >= 0)
1983 126 : return gmul(mysercoeff(res, m), mpfact(m));
1984 : else
1985 0 : return derivn(res, m, -1);
1986 : }
1987 :
1988 : static GEN
1989 189 : derservec(GEN x, long m) { pari_APPLY_same(derser(gel(x,i),m)) }
1990 :
1991 : /* derivative of order m > 0 of L (flag = 0) or Lambda (flag = 1) */
1992 : static GEN
1993 1624 : lfunderiv(GEN lmisc, long m, GEN s, long flag, long bitprec)
1994 : {
1995 1624 : pari_sp ltop = avma;
1996 1624 : GEN res, S = NULL, linit, ldata, dom;
1997 1624 : long der, prec = nbits2prec(bitprec);
1998 1624 : if (m <= 0) pari_err_DOMAIN("lfun", "D", "<=", gen_0, stoi(m));
1999 1617 : s = get_domain(s, &dom, &der);
2000 1617 : if (typ(s) != t_SER && lfunspec_OK(lmisc, s, &ldata) == 2)
2001 : {
2002 28 : linit = lfuninit(lmisc, cgetg(1,t_VEC), 0, bitprec);
2003 28 : return derivnumk((void*)linit, (GEN(*)(void*,GEN,long))&lfun,
2004 : s, stoi(der + m), prec);
2005 : }
2006 1589 : linit = lfuninit(lmisc, dom, der + m, bitprec);
2007 1589 : if (lg(lfun_get_dom(linit_get_tech(linit))) == 1)
2008 14 : pari_err_IMPL("domain = [] for derivatives in lfuninit");
2009 1575 : if (typ(s) == t_SER)
2010 : {
2011 : GEN a;
2012 42 : if (valser(s) < 0) pari_err_DOMAIN("lfun","valuation", "<", gen_0, s);
2013 42 : S = sersplit1(s, &a);
2014 42 : s = deg1ser_shallow(gen_1, a, varn(S), m + ceildivuu(lg(s)-2, valser(S)));
2015 : }
2016 : else
2017 : {
2018 1533 : long e = lfunlambdaord(linit, s) + m + 1;
2019 : /* HACK: pretend lfuninit was done to right accuracy */
2020 1533 : if (gequal0(s)) { s = gen_0; e--; }
2021 1533 : s = deg1ser_shallow(gen_1, s, 0, e);
2022 : }
2023 1575 : res = flag ? lfunlambda_OK(linit, s, dom, bitprec):
2024 973 : lfun_OK(linit, s, dom, bitprec);
2025 1575 : if (S)
2026 42 : res = gsubst(derivn(res, m, -1), varn(S), S);
2027 1533 : else if (typ(res)==t_SER)
2028 : {
2029 1470 : long v = valser(res);
2030 1470 : if (v > m) { set_avma(ltop); return gen_0; }
2031 1456 : if (v >= 0)
2032 1330 : res = gmul(mysercoeff(res, m), mpfact(m));
2033 : else
2034 126 : res = derivn(res, m, -1);
2035 : }
2036 63 : else if (is_ser(res))
2037 63 : res = derservec(res, m);
2038 1561 : return gerepilecopy(ltop, gprec_w(res, prec));
2039 : }
2040 :
2041 : GEN
2042 1512 : lfunlambda0(GEN lmisc, GEN s, long der, long bitprec)
2043 : {
2044 623 : return der? lfunderiv(lmisc, der, s, 1, bitprec)
2045 2128 : : lfunlambda(lmisc, s, bitprec);
2046 : }
2047 :
2048 : GEN
2049 6846 : lfun0(GEN lmisc, GEN s, long der, long bitprec)
2050 : {
2051 1001 : return der? lfunderiv(lmisc, der, s, 0, bitprec)
2052 7833 : : lfun(lmisc, s, bitprec);
2053 : }
2054 :
2055 : GEN
2056 19354 : lfunhardy(GEN lmisc, GEN t, long bitprec)
2057 : {
2058 19354 : pari_sp ltop = avma;
2059 19354 : long prec = nbits2prec(bitprec), d, isbig = 0;
2060 : GEN linit, h, ldata, tech, w2, k2, E, a, argz, z;
2061 :
2062 19354 : switch(typ(t))
2063 : {
2064 19347 : case t_INT: case t_FRAC: case t_REAL: break;
2065 7 : default: pari_err_TYPE("lfunhardy",t);
2066 : }
2067 19347 : if (lfunspec_OK(lmisc, mkcomplex(gen_0, t), &ldata) == 2)
2068 : {
2069 868 : long B = bitprec + maxss(gexpo(t), 0);
2070 868 : GEN L = NULL;
2071 868 : isbig = 1;
2072 868 : k2 = ghalf;
2073 868 : z = mkcomplex(k2, t);
2074 868 : if (is_linit(lmisc))
2075 : {
2076 742 : linit = lmisc;
2077 742 : if (linit_get_type(linit) == t_LDESC_PRODUCT)
2078 14 : L = mkvec(linit);/*HACK*/
2079 : }
2080 : else
2081 : {
2082 126 : linit = lfunnoinit(ldata, B);
2083 126 : ldata = linit_get_ldata(linit); /* make sure eno is included */
2084 : }
2085 868 : h = lfunloglambdalarge(L? L: ldata, gprec_w(z, nbits2prec(B)), B);
2086 868 : tech = linit_get_tech(linit);
2087 : }
2088 : else
2089 : {
2090 18479 : GEN k = ldata_get_k(ldata);
2091 18479 : GEN dom = mkvec3(gmul2n(k, -1), gen_0, gabs(t,LOWDEFAULTPREC));
2092 18479 : if (!is_linit(lmisc)) lmisc = ldata;
2093 18479 : linit = lfuninit(lmisc, dom, 0, bitprec);
2094 18479 : tech = linit_get_tech(linit);
2095 18479 : k2 = lfun_get_k2(tech);
2096 18479 : z = mkcomplex(k2, t);
2097 18479 : h = lfunlambda_OK(linit, z, dom, bitprec);
2098 : }
2099 19347 : w2 = lfun_get_w2(tech);
2100 19347 : E = lfun_get_expot(tech); /* 4E = d(k2 - 1) + real(vecsum(Vga)) */
2101 19347 : d = ldata_get_degree(ldata);
2102 : /* more accurate than garg: k/2 in Q */
2103 19347 : argz = gequal0(k2)? Pi2n(-1, prec): gatan(gdiv(t, k2), prec);
2104 19347 : prec = precision(argz);
2105 : /* prec may have increased: don't lose accuracy if |z|^2 is exact */
2106 19347 : a = gsub(gmulsg(d, gmul(t, gmul2n(argz,-1))),
2107 : gmul(E, glog(gnorm(z),prec)));
2108 19347 : if (!isint1(w2) && typ(ldata_get_dual(ldata))==t_INT)
2109 16205 : h = isbig ? gadd(h, glog(w2, prec)) : mulrealvec(h, w2);
2110 19347 : if (typ(h) == t_COMPLEX && gexpo(imag_i(h)) < -(bitprec >> 1))
2111 2533 : h = real_i(h);
2112 19347 : if (isbig) h = greal(gexp(gadd(h, a), prec));
2113 18479 : else h = gmul(h, gexp(a, prec));
2114 19347 : return gerepileupto(ltop, h);
2115 : }
2116 :
2117 : /* L = log(t); return \sum_{i = 0}^{v-1} R[-i-1] L^i/i! */
2118 : static GEN
2119 1918 : theta_pole_contrib(GEN R, long v, GEN L)
2120 : {
2121 1918 : GEN s = mysercoeff(R,-v);
2122 : long i;
2123 2023 : for (i = v-1; i >= 1; i--)
2124 105 : s = gadd(mysercoeff(R,-i), gdivgu(gmul(s,L), i));
2125 1918 : return s;
2126 : }
2127 : /* subtract successively rather than adding everything then subtracting.
2128 : * The polar part is "large" and suffers from cancellation: a little stabler
2129 : * this way */
2130 : static GEN
2131 6706 : theta_add_polar_part(GEN S, GEN R, GEN t, long prec)
2132 : {
2133 6706 : GEN logt = NULL;
2134 6706 : long j, l = lg(R);
2135 8624 : for (j = 1; j < l; j++)
2136 : {
2137 1918 : GEN Rj = gel(R,j), b = gel(Rj,1), Rb = gel(Rj,2);
2138 1918 : long v = -valser(Rb);
2139 1918 : if (v > 1 && !logt) logt = glog(t, prec);
2140 1918 : S = gsub(S, gmul(theta_pole_contrib(Rb,v,logt), gpow(t,b,prec)));
2141 : }
2142 6706 : return S;
2143 : }
2144 :
2145 : static long
2146 3598 : lfuncheckfeq_i(GEN theta, GEN thetad, GEN t0, GEN t0i, long bitprec)
2147 : {
2148 3598 : GEN ldata = linit_get_ldata(theta);
2149 : GEN S0, S0i, w, eno;
2150 3598 : long prec = nbits2prec(bitprec);
2151 3598 : if (thetad)
2152 70 : S0 = lfuntheta(thetad, t0, 0, bitprec);
2153 : else
2154 3528 : S0 = conj_i(lfuntheta(theta, conj_i(t0), 0, bitprec));
2155 3598 : S0i = lfuntheta(theta, t0i, 0, bitprec);
2156 :
2157 3598 : eno = ldata_get_rootno(ldata);
2158 3598 : if (ldata_get_residue(ldata))
2159 : {
2160 959 : GEN R = theta_get_R(linit_get_tech(theta));
2161 959 : if (gequal0(R))
2162 : {
2163 : GEN v, r;
2164 105 : long t = ldata_get_type(ldata);
2165 105 : if (t == t_LFUN_NF || t == t_LFUN_ABELREL)
2166 : { /* inefficient since theta not needed; no need to optimize for this
2167 : (artificial) query [e.g. lfuncheckfeq(t_POL)] */
2168 42 : GEN L = lfuninit(ldata,zerovec(3),0,bitprec);
2169 42 : return lfuncheckfeq(L,t0,bitprec);
2170 : }
2171 63 : v = lfunrootres(theta, bitprec);
2172 63 : r = gel(v,1);
2173 63 : if (gequal0(eno)) eno = gel(v,3);
2174 63 : R = lfunrtoR_i(ldata, r, eno, nbits2prec(bitprec));
2175 : }
2176 917 : S0i = theta_add_polar_part(S0i, R, t0, prec);
2177 : }
2178 3556 : if (gequal0(S0i) || gequal0(S0)) pari_err_PREC("lfuncheckfeq");
2179 :
2180 3556 : w = gdivvec(S0i, gmul(S0, gpow(t0, ldata_get_k(ldata), prec)));
2181 : /* missing rootno: guess it */
2182 3556 : if (gequal0(eno)) eno = lfunrootno(theta, bitprec);
2183 3556 : w = gsubvec(w, eno);
2184 3556 : if (thetad) w = gdivvec(w, eno); /* |eno| may be large in non-dual case */
2185 3556 : return gexpo(w);
2186 : }
2187 :
2188 : /* Check whether the coefficients, conductor, weight, polar part and root
2189 : * number are compatible with the functional equation at t0 and 1/t0.
2190 : * Different from lfunrootres. */
2191 : long
2192 3731 : lfuncheckfeq(GEN lmisc, GEN t0, long bitprec)
2193 : {
2194 : GEN ldata, theta, thetad, t0i;
2195 : pari_sp av;
2196 :
2197 3731 : if (is_linit(lmisc) && linit_get_type(lmisc)==t_LDESC_PRODUCT)
2198 : {
2199 168 : GEN v = lfunprod_get_fact(linit_get_tech(lmisc)), F = gel(v,1);
2200 168 : long i, b = -bitprec, l = lg(F);
2201 560 : for (i = 1; i < l; i++) b = maxss(b, lfuncheckfeq(gel(F,i), t0, bitprec));
2202 168 : return b;
2203 : }
2204 3563 : av = avma;
2205 3563 : if (!t0)
2206 : { /* ~Pi/3 + I/7, some random complex number */
2207 3388 : t0 = mkcomplex(uutoQ(355,339), uutoQ(1,7));
2208 3388 : t0i = ginv(t0);
2209 : }
2210 175 : else if (gcmpgs(gnorm(t0), 1) < 0) { t0i = t0; t0 = ginv(t0); }
2211 119 : else t0i = ginv(t0);
2212 : /* |t0| >= 1 */
2213 3563 : theta = lfunthetacheckinit(lmisc, t0i, 0, bitprec);
2214 3556 : ldata = linit_get_ldata(theta);
2215 3556 : thetad = theta_dual(theta, ldata_get_dual(ldata));
2216 3556 : return gc_long(av, lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec));
2217 : }
2218 :
2219 : /*******************************************************************/
2220 : /* Compute root number and residues */
2221 : /*******************************************************************/
2222 : /* round root number to \pm 1 if close to integer. */
2223 : static GEN
2224 6153 : ropm1(GEN w, long prec)
2225 : {
2226 : long e;
2227 : GEN r;
2228 6153 : if (typ(w) == t_INT) return w;
2229 5747 : r = grndtoi(w, &e);
2230 5747 : return (e < -prec/2)? r: w;
2231 : }
2232 :
2233 : /* theta for t=1/sqrt(2) and t2==2t simultaneously, saving 25% of the work.
2234 : * Assume correct initialization (no thetacheck) */
2235 : static void
2236 420 : lfunthetaspec(GEN linit, long bitprec, GEN *pv, GEN *pv2)
2237 : {
2238 420 : pari_sp av = avma, av2;
2239 : GEN t, Vga, an, K, ldata, thetainit, v, v2, vroots;
2240 : long L, prec, n, d;
2241 :
2242 420 : ldata = linit_get_ldata(linit);
2243 420 : thetainit = linit_get_tech(linit);
2244 420 : prec = nbits2prec(bitprec);
2245 420 : Vga = ldata_get_gammavec(ldata); d = lg(Vga)-1;
2246 420 : if (Vgaeasytheta(Vga))
2247 : {
2248 210 : GEN v2 = sqrtr(real2n(1, nbits2prec(bitprec)));
2249 210 : GEN v = shiftr(v2,-1);
2250 210 : *pv = lfuntheta(linit, v, 0, bitprec);
2251 210 : *pv2= lfuntheta(linit, v2, 0, bitprec);
2252 210 : return;
2253 : }
2254 210 : an = RgV_kill0( theta_get_an(thetainit) );
2255 210 : L = lg(an)-1;
2256 : /* to compute theta(1/sqrt(2)) */
2257 210 : t = ginv(gsqrt(gmul2n(ldata_get_conductor(ldata), 1), prec));
2258 : /* t = 1/sqrt(2N) */
2259 :
2260 : /* From then on, the code is generic and could be used to compute
2261 : * theta(t) / theta(2t) without assuming t = 1/sqrt(2) */
2262 210 : K = theta_get_K(thetainit);
2263 210 : vroots = mkvroots(d, L, prec);
2264 210 : t = gpow(t, gdivgu(gen_2, d), prec); /* rt variant: t->t^(2/d) */
2265 : /* v = \sum_{n <= L, n odd} a_n K(nt) */
2266 1815212 : for (v = gen_0, n = 1; n <= L; n+=2)
2267 : {
2268 1815002 : GEN tn, Kn, a = gel(an, n);
2269 :
2270 1815002 : if (!a) continue;
2271 113729 : av2 = avma;
2272 113729 : tn = gmul(t, gel(vroots,n));
2273 113729 : Kn = gammamellininvrt(K, tn, bitprec);
2274 113729 : v = gerepileupto(av2, gadd(v, gmul(a,Kn)));
2275 : }
2276 : /* v += \sum_{n <= L, n even} a_n K(nt), v2 = \sum_{n <= L/2} a_n K(2n t) */
2277 1815114 : for (v2 = gen_0, n = 1; n <= L/2; n++)
2278 : {
2279 1814904 : GEN t2n, K2n, a = gel(an, n), a2 = gel(an,2*n);
2280 :
2281 1814904 : if (!a && !a2) continue;
2282 120484 : av2 = avma;
2283 120484 : t2n = gmul(t, gel(vroots,2*n));
2284 120484 : K2n = gerepileupto(av2, gammamellininvrt(K, t2n, bitprec));
2285 120484 : if (a) v2 = gadd(v2, gmul(a, K2n));
2286 120484 : if (a2) v = gadd(v, gmul(a2,K2n));
2287 : }
2288 210 : *pv = v;
2289 210 : *pv2 = v2;
2290 210 : gerepileall(av, 2, pv,pv2);
2291 : }
2292 :
2293 : static GEN
2294 413 : Rtor(GEN a, GEN R, GEN ldata, long prec)
2295 : {
2296 413 : GEN FVga = gammafactor(ldata_get_gammavec(ldata));
2297 413 : GEN Na = gpow(ldata_get_conductor(ldata), gdivgu(a,2), prec);
2298 : long ext;
2299 413 : return gdiv(R, gmul(Na, gammafactproduct(FVga, a, &ext, prec)));
2300 : }
2301 :
2302 : /* v = theta~(t), vi = theta(1/t) */
2303 : static GEN
2304 5789 : get_eno(GEN R, GEN k, GEN t, GEN v, GEN vi, long vx, long bitprec, long force)
2305 : {
2306 5789 : long prec = nbits2prec(bitprec);
2307 5789 : GEN a0, a1, S = deg1pol(gmul(gpow(t,k,prec), gneg(v)), vi, vx);
2308 :
2309 5789 : S = theta_add_polar_part(S, R, t, prec);
2310 5789 : if (typ(S) != t_POL || degpol(S) != 1) return NULL;
2311 5789 : a1 = gel(S,3); if (!force && gexpo(a1) < -bitprec/4) return NULL;
2312 5740 : a0 = gel(S,2);
2313 5740 : return gdivvec(a0, gneg(a1));
2314 :
2315 : }
2316 : /* Return w using theta(1/t) - w t^k \bar{theta}(t) = polar_part(t,w).
2317 : * The full Taylor expansion of L must be known */
2318 : GEN
2319 5740 : lfunrootno(GEN linit, long bitprec)
2320 : {
2321 : GEN ldata, t, eno, v, vi, R, thetad;
2322 5740 : long c = 0, prec = nbits2prec(bitprec), vx = fetch_var();
2323 : GEN k;
2324 : pari_sp av;
2325 :
2326 : /* initialize for t > 1/sqrt(2) */
2327 5740 : linit = lfunthetacheckinit(linit, dbltor(sqrt(0.5)), 0, bitprec);
2328 5740 : ldata = linit_get_ldata(linit);
2329 5740 : k = ldata_get_k(ldata);
2330 5754 : R = ldata_get_residue(ldata)? lfunrtoR_eno(ldata, pol_x(vx), prec)
2331 5740 : : cgetg(1, t_VEC);
2332 5740 : t = gen_1;
2333 5740 : v = lfuntheta(linit, t, 0, bitprec);
2334 5740 : thetad = theta_dual(linit, ldata_get_dual(ldata));
2335 5740 : vi = !thetad ? conj_i(v): lfuntheta(thetad, t, 0, bitprec);
2336 5740 : eno = get_eno(R,k,t,vi,v, vx, bitprec, 0);
2337 5740 : if (!eno && !thetad)
2338 : { /* t = sqrt(2), vi = theta(1/t), v = theta(t) */
2339 7 : lfunthetaspec(linit, bitprec, &vi, &v);
2340 7 : t = sqrtr(utor(2, prec));
2341 7 : eno = get_eno(R,k,t,conj_i(v),vi, vx, bitprec, 0);
2342 : }
2343 5740 : av = avma;
2344 5782 : while (!eno)
2345 : {
2346 42 : t = addsr(1, shiftr(utor(pari_rand(), prec), -2-BITS_IN_LONG));
2347 : /* t in [1,1.25[ */
2348 0 : v = thetad? lfuntheta(thetad, t, 0, bitprec)
2349 42 : : conj_i(lfuntheta(linit, t, 0, bitprec));
2350 42 : vi = lfuntheta(linit, ginv(t), 0, bitprec);
2351 42 : eno = get_eno(R,k,t,v,vi, vx, bitprec, c++ == 5);
2352 42 : set_avma(av);
2353 : }
2354 5740 : delete_var(); return ropm1(eno,prec);
2355 : }
2356 :
2357 : /* Find root number and/or residues when L-function coefficients and
2358 : conductor are known. For the moment at most a single residue allowed. */
2359 : GEN
2360 6559 : lfunrootres(GEN data, long bitprec)
2361 : {
2362 6559 : pari_sp ltop = avma;
2363 : GEN k, w, r, R, a, b, e, v, v2, be, ldata, linit;
2364 : long prec;
2365 :
2366 6559 : ldata = lfunmisc_to_ldata_shallow(data);
2367 6559 : r = ldata_get_residue(ldata);
2368 6559 : k = ldata_get_k(ldata);
2369 6559 : w = ldata_get_rootno(ldata);
2370 6559 : if (r) r = normalize_simple_pole(r, k);
2371 6559 : if (!r || residues_known(r))
2372 : {
2373 6146 : if (isintzero(w)) w = lfunrootno(data, bitprec);
2374 6146 : if (!r)
2375 4053 : r = R = gen_0;
2376 : else
2377 2093 : R = lfunrtoR_eno(ldata, w, nbits2prec(bitprec));
2378 6146 : return gerepilecopy(ltop, mkvec3(r, R, w));
2379 : }
2380 413 : linit = lfunthetacheckinit(data, dbltor(sqrt(0.5)), 0, bitprec);
2381 413 : prec = nbits2prec(bitprec);
2382 413 : if (lg(r) > 2) pari_err_IMPL("multiple poles in lfunrootres");
2383 : /* Now residue unknown, and r = [[be,0]]. */
2384 413 : be = gmael(r, 1, 1);
2385 413 : if (ldata_isreal(ldata) && gequalm1(w))
2386 0 : R = lfuntheta(linit, gen_1, 0, bitprec);
2387 : else
2388 : {
2389 413 : GEN p2k = gpow(gen_2,k,prec);
2390 413 : lfunthetaspec(linit, bitprec, &v2, &v);
2391 413 : if (gequal(gmulsg(2, be), k)) pari_err_IMPL("pole at k/2 in lfunrootres");
2392 413 : if (gequal(be, k))
2393 : {
2394 147 : a = conj_i(gsub(gmul(p2k, v), v2));
2395 147 : b = subiu(p2k, 1);
2396 147 : e = gmul(gsqrt(p2k, prec), gsub(v2, v));
2397 : }
2398 : else
2399 : {
2400 266 : GEN tk2 = gsqrt(p2k, prec);
2401 266 : GEN tbe = gpow(gen_2, be, prec);
2402 266 : GEN tkbe = gpow(gen_2, gdivgu(gsub(k, be), 2), prec);
2403 266 : a = conj_i(gsub(gmul(tbe, v), v2));
2404 266 : b = gsub(gdiv(tbe, tkbe), tkbe);
2405 266 : e = gsub(gmul(gdiv(tbe, tk2), v2), gmul(tk2, v));
2406 : }
2407 413 : if (isintzero(w))
2408 : { /* Now residue unknown, r = [[be,0]], and w unknown. */
2409 7 : GEN t0 = mkfrac(utoi(11),utoi(10));
2410 7 : GEN th1 = lfuntheta(linit, t0, 0, bitprec);
2411 7 : GEN th2 = lfuntheta(linit, ginv(t0), 0, bitprec);
2412 7 : GEN tbe = gpow(t0, gmulsg(2, be), prec);
2413 7 : GEN tkbe = gpow(t0, gsub(k, be), prec);
2414 7 : GEN tk2 = gpow(t0, k, prec);
2415 7 : GEN c = conj_i(gsub(gmul(tbe, th1), th2));
2416 7 : GEN d = gsub(gdiv(tbe, tkbe), tkbe);
2417 7 : GEN f = gsub(gmul(gdiv(tbe, tk2), th2), gmul(tk2, th1));
2418 7 : GEN D = gsub(gmul(a, d), gmul(b, c));
2419 7 : w = gdiv(gsub(gmul(d, e), gmul(b, f)), D);
2420 : }
2421 413 : w = ropm1(w, prec);
2422 413 : R = gdiv(gsub(e, gmul(a, w)), b);
2423 : }
2424 413 : r = normalize_simple_pole(Rtor(be, R, ldata, prec), be);
2425 413 : R = lfunrtoR_i(ldata, r, w, prec);
2426 413 : return gerepilecopy(ltop, mkvec3(r, R, w));
2427 : }
2428 :
2429 : /*******************************************************************/
2430 : /* Zeros */
2431 : /*******************************************************************/
2432 : struct lhardyz_t {
2433 : long bitprec, prec;
2434 : GEN linit;
2435 : };
2436 :
2437 : static GEN
2438 18570 : lfunhardyzeros(void *E, GEN t)
2439 : {
2440 18570 : struct lhardyz_t *S = (struct lhardyz_t*)E;
2441 18570 : GEN z = gprec_wensure(lfunhardy(S->linit, t, S->bitprec), S->prec);
2442 18570 : return typ(z) == t_VEC ? RgV_prod(z): z;
2443 : }
2444 :
2445 : /* initialize for computation on critical line up to height h, zero
2446 : * of order <= m */
2447 : static GEN
2448 560 : lfuncenterinit(GEN lmisc, double h, long m, long bitprec)
2449 : {
2450 560 : GEN ldata = lfunmisc_to_ldata_shallow(lmisc);
2451 560 : if (m < 0)
2452 : { /* choose a sensible default */
2453 560 : m = 4;
2454 560 : if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_INIT)
2455 : {
2456 469 : GEN domain = lfun_get_domain(linit_get_tech(lmisc));
2457 469 : m = domain_get_der(domain);
2458 : }
2459 : }
2460 560 : if (is_dirichlet(ldata)) m = 0;
2461 560 : return lfuninit(lmisc, mkvec(dbltor(h)), m, bitprec);
2462 : }
2463 :
2464 : long
2465 553 : lfunorderzero(GEN lmisc, long m, long bitprec)
2466 : {
2467 553 : pari_sp ltop = avma;
2468 : GEN eno, ldata, linit, k2;
2469 : long G, c0, c, st;
2470 :
2471 553 : if (is_linit(lmisc) && linit_get_type(lmisc) == t_LDESC_PRODUCT)
2472 : {
2473 84 : GEN M = gmael(linit_get_tech(lmisc), 2,1);
2474 84 : long i, l = lg(M);
2475 280 : for (c=0, i=1; i < l; i++) c += lfunorderzero(gel(M,i), m, bitprec);
2476 84 : return c;
2477 : }
2478 469 : linit = lfuncenterinit(lmisc, 0, m, bitprec);
2479 469 : ldata = linit_get_ldata(linit);
2480 469 : eno = ldata_get_rootno(ldata);
2481 469 : k2 = gmul2n(ldata_get_k(ldata), -1);
2482 469 : G = -bitprec/2;
2483 469 : c0 = 0; st = 1;
2484 469 : if (typ(eno) == t_VEC)
2485 : {
2486 42 : long i, l = lg(eno), cnt = l-1, s = 0;
2487 42 : GEN v = zero_zv(l-1);
2488 42 : if (ldata_isreal(ldata)) st = 2;
2489 84 : for (c = c0; cnt; c += st)
2490 : {
2491 42 : GEN L = lfun0(linit, k2, c, bitprec);
2492 154 : for (i = 1; i < l; i++)
2493 : {
2494 112 : if (v[i]==0 && gexpo(gel(L,i)) > G)
2495 : {
2496 112 : v[i] = c; cnt--; s += c;
2497 : }
2498 : }
2499 : }
2500 42 : return gc_long(ltop,s);
2501 : }
2502 : else
2503 : {
2504 427 : if (ldata_isreal(ldata)) { st = 2; if (!gequal1(eno)) c0 = 1; }
2505 455 : for (c = c0;; c += st)
2506 455 : if (gexpo(lfun0(linit, k2, c, bitprec)) > G) return gc_long(ltop, c);
2507 : }
2508 : }
2509 :
2510 : /* assume T1 * T2 > 0, T1 <= T2 */
2511 : static void
2512 98 : lfunzeros_i(struct lhardyz_t *S, GEN *pw, long *ct, GEN T1, GEN T2, long d,
2513 : GEN cN, GEN pi2, GEN pi2div, long precinit, long prec)
2514 : {
2515 98 : GEN T = T1, w = *pw;
2516 98 : long W = lg(w)-1, s = gsigne(lfunhardyzeros(S, T1));
2517 : for(;;)
2518 427 : {
2519 525 : pari_sp av = avma;
2520 : GEN D, T0, z;
2521 525 : D = gcmp(T, pi2) < 0? cN
2522 525 : : gadd(cN, gmulsg(d, glog(gdiv(T, pi2), prec)));
2523 525 : D = gdiv(pi2div, gmulsg(d, D));
2524 : for(;;)
2525 13482 : {
2526 : long s0;
2527 14007 : T0 = T; T = gadd(T, D);
2528 14007 : if (gcmp(T, T2) >= 0) T = T2;
2529 14007 : s0 = gsigne(lfunhardyzeros(S, T));
2530 14007 : if (s0 != s) { s = s0; break; }
2531 13580 : if (T == T2) { setlg(w, *ct); *pw = w; return; }
2532 : }
2533 427 : z = zbrent(S, lfunhardyzeros, T0, T, prec); /* T <= T2 */
2534 427 : gerepileall(av, 2, &T, &z);
2535 427 : if (*ct > W) { W *= 2; w = vec_lengthen(w, W); }
2536 427 : if (typ(z) == t_REAL) z = rtor(z, precinit);
2537 427 : gel(w, (*ct)++) = z;
2538 : }
2539 : setlg(w, *ct); *pw = w;
2540 : }
2541 : GEN
2542 98 : lfunzeros(GEN ldata, GEN lim, long divz, long bitprec)
2543 : {
2544 98 : pari_sp ltop = avma;
2545 : GEN linit, pi2, pi2div, cN, w, T, h1, h2;
2546 98 : long i, d, NEWD, c, ct, s1, s2, prec, prec0 = nbits2prec(bitprec);
2547 : double maxt;
2548 : struct lhardyz_t S;
2549 :
2550 98 : if (is_linit(ldata) && linit_get_type(ldata) == t_LDESC_PRODUCT)
2551 : {
2552 0 : GEN M = gmael(linit_get_tech(ldata), 2,1);
2553 0 : long l = lg(M);
2554 0 : w = cgetg(l, t_VEC);
2555 0 : for (i = 1; i < l; i++) gel(w,i) = lfunzeros(gel(M,i), lim, divz, bitprec);
2556 0 : return gerepileupto(ltop, vecsort0(shallowconcat1(w), NULL, 0));
2557 : }
2558 98 : if (typ(lim) == t_VEC)
2559 : {
2560 : double H1, H2;
2561 63 : if (lg(lim) != 3 || gcmp(gel(lim, 1), gel(lim, 2)) >= 0)
2562 7 : pari_err_TYPE("lfunzeros",lim);
2563 56 : h1 = gel(lim, 1); H1 = gtodouble(h1);
2564 56 : h2 = gel(lim, 2); H2 = gtodouble(h2);
2565 56 : maxt = maxdd(fabs(H1), fabs(H2));
2566 56 : if (H1 * H2 > 0)
2567 : {
2568 35 : GEN LDATA = lfunmisc_to_ldata_shallow(ldata);
2569 35 : double m = mindd(fabs(H1), fabs(H2));
2570 35 : if (is_dirichlet(LDATA) && m > lfuninit_cutoff(LDATA)) maxt = 0;
2571 : }
2572 : }
2573 : else
2574 : {
2575 35 : if (gcmp(lim, gen_0) <= 0) pari_err_TYPE("lfunzeros",lim);
2576 35 : h1 = gen_0;
2577 35 : h2 = lim;
2578 35 : maxt = gtodouble(h2);
2579 : }
2580 91 : S.linit = linit = lfuncenterinit(ldata, maxt, -1, bitprec);
2581 91 : S.bitprec = bitprec;
2582 91 : S.prec = prec0;
2583 91 : ldata = linit_get_ldata(linit);
2584 91 : d = ldata_get_degree(ldata);
2585 :
2586 91 : NEWD = minss((long) ceil(bitprec + (M_PI/(4*M_LN2)) * d * maxt),
2587 : lfun_get_bitprec(linit_get_tech(linit)));
2588 91 : prec = nbits2prec(NEWD);
2589 91 : cN = gdiv(ldata_get_conductor(ldata), gpowgs(Pi2n(-1, prec), d));
2590 91 : cN = gexpo(cN) >= 0? gaddsg(d, gmulsg(2, glog(cN, prec))): utoi(d);
2591 91 : pi2 = Pi2n(1, prec);
2592 91 : pi2div = gdivgu(pi2, labs(divz));
2593 91 : s1 = gsigne(h1);
2594 91 : s2 = gsigne(h2);
2595 91 : w = cgetg(100+1, t_VEC); c = 1; ct = 0; T = NULL;
2596 91 : if (s1 <= 0 && s2 >= 0)
2597 : {
2598 56 : GEN r = ldata_get_residue(ldata);
2599 56 : if (!r || gequal0(r))
2600 : {
2601 35 : ct = lfunorderzero(linit, -1, bitprec);
2602 35 : if (ct) T = real2n(-prec / (2*ct), prec);
2603 : }
2604 : }
2605 91 : if (s1 <= 0)
2606 : {
2607 63 : if (s1 < 0)
2608 21 : lfunzeros_i(&S, &w, &c, h1, T? negr(T): h2,
2609 : d, cN, pi2, pi2div, prec0, prec);
2610 63 : if (ct)
2611 : {
2612 21 : long n = lg(w)-1;
2613 21 : if (c + ct >= n) w = vec_lengthen(w, n + ct);
2614 84 : for (i = 1; i <= ct; i++) gel(w,c++) = gen_0;
2615 : }
2616 : }
2617 91 : if (s2 > 0 && (T || s1 >= 0))
2618 77 : lfunzeros_i(&S, &w, &c, T? T: h1, h2, d, cN, pi2, pi2div, prec0, prec);
2619 91 : return gerepilecopy(ltop, w);
2620 : }
2621 :
2622 : /*******************************************************************/
2623 : /* Guess conductor */
2624 : /*******************************************************************/
2625 : struct huntcond_t {
2626 : GEN k;
2627 : GEN theta, thetad;
2628 : GEN *pM, *psqrtM, *pMd, *psqrtMd;
2629 : };
2630 :
2631 : static void
2632 11962 : condset(struct huntcond_t *S, GEN M, long prec)
2633 : {
2634 11962 : *(S->pM) = M;
2635 11962 : *(S->psqrtM) = gsqrt(ginv(M), prec);
2636 11962 : if (S->thetad != S->theta)
2637 : {
2638 0 : *(S->pMd) = *(S->pM);
2639 0 : *(S->psqrtMd) = *(S->psqrtM);
2640 : }
2641 11962 : }
2642 :
2643 : /* M should eventually converge to N, the conductor. L has no pole. */
2644 : static GEN
2645 6888 : wrap1(void *E, GEN M)
2646 : {
2647 6888 : struct huntcond_t *S = (struct huntcond_t*)E;
2648 : GEN thetainit, tk, p1, p1inv;
2649 6888 : GEN t = mkfrac(stoi(11), stoi(10));
2650 : long prec, bitprec;
2651 :
2652 6888 : thetainit = linit_get_tech(S->theta);
2653 6888 : bitprec = theta_get_bitprec(thetainit);
2654 6888 : prec = nbits2prec(bitprec);
2655 6888 : condset(S, M, prec);
2656 6888 : tk = gpow(t, S->k, prec);
2657 6888 : p1 = lfuntheta(S->thetad, t, 0, bitprec);
2658 6888 : p1inv = lfuntheta(S->theta, ginv(t), 0, bitprec);
2659 6888 : return glog(gabs(gmul(tk, gdiv(p1, p1inv)), prec), prec);
2660 : }
2661 :
2662 : /* M should eventually converge to N, the conductor. L has a pole. */
2663 : static GEN
2664 5032 : wrap2(void *E, GEN M)
2665 : {
2666 5032 : struct huntcond_t *S = (struct huntcond_t*)E;
2667 : GEN t1k, t2k, p1, p1inv, p2, p2inv, thetainit, R;
2668 5032 : GEN t1 = mkfrac(stoi(11), stoi(10)), t2 = mkfrac(stoi(13), stoi(11));
2669 : GEN t1be, t2be, t1bemk, t2bemk, t1kmbe, t2kmbe;
2670 : GEN F11, F12, F21, F22, P1, P2, res;
2671 : long prec, bitprec;
2672 5032 : GEN k = S->k;
2673 :
2674 5032 : thetainit = linit_get_tech(S->theta);
2675 5032 : bitprec = theta_get_bitprec(thetainit);
2676 5032 : prec = nbits2prec(bitprec);
2677 5032 : condset(S, M, prec);
2678 :
2679 5032 : p1 = lfuntheta(S->thetad, t1, 0, bitprec);
2680 5032 : p2 = lfuntheta(S->thetad, t2, 0, bitprec);
2681 5032 : p1inv = lfuntheta(S->theta, ginv(t1), 0, bitprec);
2682 5032 : p2inv = lfuntheta(S->theta, ginv(t2), 0, bitprec);
2683 5032 : t1k = gpow(t1, k, prec);
2684 5032 : t2k = gpow(t2, k, prec);
2685 5032 : R = theta_get_R(thetainit);
2686 5032 : if (typ(R) == t_VEC)
2687 : {
2688 0 : GEN be = gmael(R, 1, 1);
2689 0 : t1be = gpow(t1, be, prec); t1bemk = gdiv(gsqr(t1be), t1k);
2690 0 : t2be = gpow(t2, be, prec); t2bemk = gdiv(gsqr(t2be), t2k);
2691 0 : t1kmbe = gdiv(t1k, t1be);
2692 0 : t2kmbe = gdiv(t2k, t2be);
2693 : }
2694 : else
2695 : { /* be = k */
2696 5032 : t1be = t1k; t1bemk = t1k; t1kmbe = gen_1;
2697 5032 : t2be = t2k; t2bemk = t2k; t2kmbe = gen_1;
2698 : }
2699 5032 : F11 = conj_i(gsub(gmul(gsqr(t1be), p1), p1inv));
2700 5032 : F12 = conj_i(gsub(gmul(gsqr(t2be), p2), p2inv));
2701 5032 : F21 = gsub(gmul(t1k, p1), gmul(t1bemk, p1inv));
2702 5032 : F22 = gsub(gmul(t2k, p2), gmul(t2bemk, p2inv));
2703 5032 : P1 = gsub(gmul(t1bemk, t1be), t1kmbe);
2704 5032 : P2 = gsub(gmul(t2bemk, t2be), t2kmbe);
2705 5032 : res = gdiv(gsub(gmul(P2,F21), gmul(P1,F22)),
2706 : gsub(gmul(P2,F11), gmul(P1,F12)));
2707 5032 : return glog(gabs(res, prec), prec);
2708 : }
2709 :
2710 : /* If flag = 0 (default) return all conductors found as integers. If
2711 : flag = 1, return the approximations, not the integers. If flag = 2,
2712 : return all, even nonintegers. */
2713 :
2714 : static GEN
2715 84 : checkconductor(GEN v, long bit, long flag)
2716 : {
2717 : GEN w;
2718 84 : long e, j, k, l = lg(v);
2719 84 : if (flag == 2) return v;
2720 84 : w = cgetg(l, t_VEC);
2721 322 : for (j = k = 1; j < l; j++)
2722 : {
2723 238 : GEN N = grndtoi(gel(v,j), &e);
2724 238 : if (e < -bit) gel(w,k++) = flag ? gel(v,j): N;
2725 : }
2726 84 : if (k == 2) return gel(w,1);
2727 7 : setlg(w,k); return w;
2728 : }
2729 :
2730 : static GEN
2731 98 : parse_maxcond(GEN maxN)
2732 : {
2733 : GEN M;
2734 98 : if (!maxN)
2735 49 : M = utoipos(10000);
2736 49 : else if (typ(maxN) == t_VEC)
2737 : {
2738 14 : if (!RgV_is_ZV(maxN)) pari_err_TYPE("lfunconductor",maxN);
2739 14 : return ZV_sort_shallow(maxN);
2740 : }
2741 : else
2742 35 : M = maxN;
2743 84 : return (typ(M) == t_INT)? addiu(M, 1): gceil(M);
2744 : }
2745 :
2746 : GEN
2747 98 : lfunconductor(GEN data, GEN maxcond, long flag, long bitprec)
2748 : {
2749 : struct huntcond_t S;
2750 98 : pari_sp av = avma;
2751 98 : GEN ldata = lfunmisc_to_ldata_shallow(data);
2752 98 : GEN ld, r, v, theta, thetad, M, tdom, t0 = NULL, t0i = NULL;
2753 : GEN (*eval)(void *, GEN);
2754 : long prec;
2755 98 : M = parse_maxcond(maxcond);
2756 98 : r = ldata_get_residue(ldata);
2757 98 : if (typ(M) == t_VEC) /* select in list */
2758 : {
2759 14 : if (lg(M) == 1) { set_avma(av); return cgetg(1,t_VEC); }
2760 7 : eval = NULL; tdom = dbltor(0.7);
2761 : }
2762 84 : else if (!r) { eval = wrap1; tdom = uutoQ(10,11); }
2763 : else
2764 : {
2765 21 : if (typ(r) == t_VEC && lg(r) > 2)
2766 0 : pari_err_IMPL("multiple poles in lfunconductor");
2767 21 : eval = wrap2; tdom = uutoQ(11,13);
2768 : }
2769 91 : if (eval) bitprec += bitprec/2;
2770 91 : prec = nbits2prec(bitprec);
2771 91 : ld = shallowcopy(ldata);
2772 91 : gel(ld, 5) = eval? M: veclast(M);
2773 91 : theta = lfunthetainit_i(ld, tdom, 0, bitprec);
2774 91 : thetad = theta_dual(theta, ldata_get_dual(ldata));
2775 91 : gel(theta,3) = shallowcopy(linit_get_tech(theta));
2776 91 : S.k = ldata_get_k(ldata);
2777 91 : S.theta = theta;
2778 91 : S.thetad = thetad? thetad: theta;
2779 91 : S.pM = &gel(linit_get_ldata(theta),5);
2780 91 : S.psqrtM = &gel(linit_get_tech(theta),7);
2781 91 : if (thetad)
2782 : {
2783 0 : S.pMd = &gel(linit_get_ldata(thetad),5);
2784 0 : S.psqrtMd = &gel(linit_get_tech(thetad),7);
2785 : }
2786 91 : if (!eval)
2787 : {
2788 7 : long i, besti = 0, beste = -10, l = lg(M);
2789 7 : t0 = uutoQ(11,10); t0i = uutoQ(10,11);
2790 49 : for (i = 1; i < l; i++)
2791 : {
2792 42 : pari_sp av2 = avma;
2793 : long e;
2794 42 : condset(&S, gel(M,i), prec);
2795 42 : e = lfuncheckfeq_i(theta, thetad, t0, t0i, bitprec);
2796 42 : set_avma(av2);
2797 42 : if (e < beste) { beste = e; besti = i; }
2798 35 : else if (e == beste) beste = besti = 0; /* tie: forget */
2799 : }
2800 7 : if (!besti) { set_avma(av); return cgetg(1,t_VEC); }
2801 7 : return gerepilecopy(av, mkvec2(gel(M,besti), stoi(beste)));
2802 : }
2803 84 : v = solvestep((void*)&S, eval, ghalf, M, gen_2, 14, prec);
2804 84 : return gerepilecopy(av, checkconductor(v, bitprec/2, flag));
2805 : }
2806 :
2807 : /* assume chi primitive */
2808 : static GEN
2809 2856 : znchargauss_i(GEN G, GEN chi, long bitprec)
2810 : {
2811 2856 : GEN z, q, F = znstar_get_N(G);
2812 : long prec;
2813 :
2814 2856 : if (equali1(F)) return gen_1;
2815 2856 : prec = nbits2prec(bitprec);
2816 2856 : q = sqrtr_abs(itor(F, prec));
2817 2856 : z = lfuntheta(mkvec2(G,chi), gen_1, 0, bitprec);
2818 2856 : if (gexpo(z) < 10 - bitprec)
2819 : {
2820 28 : if (equaliu(F,300))
2821 : {
2822 14 : GEN z = rootsof1u_cx(25, prec);
2823 14 : GEN n = znconreyexp(G, chi);
2824 14 : if (equaliu(n, 131)) return gmul(q, gpowgs(z,14));
2825 7 : if (equaliu(n, 71)) return gmul(q, gpowgs(z,11));
2826 : }
2827 14 : if (equaliu(F,600))
2828 : {
2829 14 : GEN z = rootsof1u_cx(25, prec);
2830 14 : GEN n = znconreyexp(G, chi);
2831 14 : if (equaliu(n, 491)) return gmul(q, gpowgs(z,7));
2832 7 : if (equaliu(n, 11)) return gmul(q, gpowgs(z,18));
2833 : }
2834 0 : pari_err_BUG("znchargauss [ Theta(chi,1) = 0 ]");
2835 : }
2836 2828 : z = gmul(gdiv(z, conj_i(z)), q);
2837 2828 : if (zncharisodd(G,chi)) z = mulcxI(z);
2838 2828 : return z;
2839 : }
2840 : static GEN
2841 2856 : Z_radical(GEN N, long *om)
2842 : {
2843 2856 : GEN P = gel(Z_factor(N), 1);
2844 2856 : *om = lg(P)-1; return ZV_prod(P);
2845 : }
2846 : GEN
2847 5502 : znchargauss(GEN G, GEN chi, GEN a, long bitprec)
2848 : {
2849 : GEN v, T, N, F, b0, b1, b2, bF, a1, aF, A, r, GF, tau, B, faB, u, S;
2850 5502 : long omb0, prec = nbits2prec(bitprec);
2851 5502 : pari_sp av = avma;
2852 :
2853 5502 : if (typ(chi) != t_COL) chi = znconreylog(G,chi);
2854 5502 : T = znchartoprimitive(G, chi);
2855 5502 : GF = gel(T,1);
2856 5502 : chi = gel(T,2); /* now primitive */
2857 5502 : N = znstar_get_N(G);
2858 5502 : F = znstar_get_N(GF);
2859 5502 : if (equalii(N,F)) b1 = bF = gen_1;
2860 : else
2861 : {
2862 245 : v = Z_ppio(diviiexact(N,F), F);
2863 245 : bF = gel(v,2); /* (N/F, F^oo) */
2864 245 : b1 = gel(v,3); /* cofactor */
2865 : }
2866 5502 : if (!a) a = a1 = aF = gen_1;
2867 : else
2868 : {
2869 5453 : if (typ(a) != t_INT) pari_err_TYPE("znchargauss",a);
2870 5453 : a = modii(a, N);
2871 5453 : if (!signe(a)) { set_avma(av); return is_pm1(F)? eulerphi(N): gen_0; }
2872 3024 : v = Z_ppio(a, F);
2873 3024 : aF = gel(v,2);
2874 3024 : a1 = gel(v,3);
2875 : }
2876 3073 : if (!equalii(aF, bF)) { set_avma(av); return gen_0; }
2877 2856 : b0 = Z_radical(b1, &omb0);
2878 2856 : b2 = diviiexact(b1, b0);
2879 2856 : A = dvmdii(a1, b2, &r);
2880 2856 : if (r != gen_0) { set_avma(av); return gen_0; }
2881 2856 : B = gcdii(A,b0); faB = Z_factor(B); /* squarefree */
2882 2856 : S = eulerphi(mkvec2(B,faB));
2883 2856 : if (odd(omb0 + lg(gel(faB,1))-1)) S = negi(S); /* moebius(b0/B) * phi(B) */
2884 2856 : S = mulii(S, mulii(aF,b2));
2885 2856 : tau = znchargauss_i(GF, chi, bitprec);
2886 2856 : u = Fp_div(b0, A, F);
2887 2856 : if (!equali1(u))
2888 : {
2889 7 : GEN ord = zncharorder(GF, chi), z = rootsof1_cx(ord, prec);
2890 7 : tau = gmul(tau, znchareval(GF, chi, u, mkvec2(z,ord)));
2891 : }
2892 2856 : return gerepileupto(av, gmul(tau, S));
2893 : }
|