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 : /** Dirichlet series through Euler product **/
18 : /** **/
19 : /********************************************************************/
20 : #include "pari.h"
21 : #include "paripriv.h"
22 :
23 : static void
24 28 : err_direuler(GEN x)
25 28 : { pari_err_DOMAIN("direuler","constant term","!=", gen_1,x); }
26 :
27 : /* s = t_POL (tolerate t_SER of valuation 0) of constant term = 1
28 : * d = minimal such that p^d > X
29 : * V indexed by 1..X will contain the a_n
30 : * v[1..n] contains the indices nj such that V[nj] != 0 */
31 : static long
32 28700 : dirmuleuler_small(GEN V, GEN v, long n, ulong p, GEN s, long d)
33 : {
34 28700 : long i, j, m = n, D = minss(d+2, lg(s));
35 28700 : ulong q = 1, X = lg(V)-1;
36 :
37 94724 : for (i = 3, q = p; i < D; i++, q *= p) /* q*p does not overflow */
38 : {
39 66024 : GEN aq = gel(s,i);
40 66024 : if (gequal0(aq)) continue;
41 : /* j = 1 */
42 53753 : gel(V,q) = aq;
43 53753 : v[++n] = q;
44 3268013 : for (j = 2; j <= m; j++)
45 : {
46 3214260 : ulong nj = umuluu_le(uel(v,j), q, X);
47 3214260 : if (!nj) continue;
48 192017 : gel(V,nj) = gmul(aq, gel(V,v[j]));
49 192017 : v[++n] = nj;
50 : }
51 : }
52 28700 : return n;
53 : }
54 :
55 : /* ap != 0 for efficiency, p > sqrt(X) */
56 : static void
57 308798 : dirmuleuler_large(GEN V, ulong p, GEN ap)
58 : {
59 308798 : long j, jp, X = lg(V)-1;
60 308798 : gel(V,p) = ap;
61 1506547 : for (j = 2, jp = 2*p; jp <= X; j++, jp += p) gel(V,jp) = gmul(ap, gel(V,j));
62 308798 : }
63 :
64 : static ulong
65 10269 : direulertou(GEN a, GEN fl(GEN))
66 : {
67 10269 : if (typ(a) != t_INT)
68 : {
69 49 : a = fl(a);
70 28 : if (typ(a) != t_INT) pari_err_TYPE("direuler", a);
71 : }
72 10248 : return signe(a)<=0 ? 0: itou(a);
73 : }
74 :
75 : static GEN
76 3724 : direuler_Sbad(GEN V, GEN v, GEN Sbad, ulong *n)
77 : {
78 3724 : long i, l = lg(Sbad);
79 3724 : ulong X = lg(V)-1;
80 3724 : GEN pbad = gen_1;
81 9646 : for (i = 1; i < l; i++)
82 : {
83 5957 : GEN ai = gel(Sbad,i);
84 : ulong q;
85 5957 : if (typ(ai) != t_VEC || lg(ai) != 3)
86 14 : pari_err_TYPE("direuler [bad primes]",ai);
87 5943 : q = gtou(gel(ai,1));
88 5936 : if (q <= X)
89 : {
90 4809 : long d = ulogint(X, q) + 1;
91 4809 : GEN s = direuler_factor(gel(ai,2), d);
92 4795 : *n = dirmuleuler_small(V, v, *n, q, s, d);
93 4795 : pbad = muliu(pbad, q);
94 : }
95 : }
96 3689 : return pbad;
97 : }
98 :
99 : GEN
100 672 : direuler_bad(void *E, GEN (*eval)(void *,GEN,long), GEN a,GEN b,GEN c, GEN Sbad)
101 : {
102 : ulong au, bu, X, sqrtX, n, p;
103 672 : pari_sp av0 = avma;
104 : GEN gp, v, V;
105 : forprime_t T;
106 672 : au = direulertou(a, gceil);
107 665 : bu = direulertou(b, gfloor);
108 658 : X = c ? direulertou(c, gfloor): bu;
109 651 : if (X == 0) return cgetg(1,t_VEC);
110 644 : if (bu > X) bu = X;
111 644 : if (!u_forprime_init(&T, au, bu)) { set_avma(av0); return mkvec(gen_1); }
112 630 : v = vecsmall_ei(X, 1);
113 630 : V = vec_ei(X, 1);
114 630 : n = 1;
115 630 : if (Sbad) Sbad = direuler_Sbad(V, v, Sbad, &n);
116 595 : p = 1; gp = cgetipos(3); sqrtX = usqrt(X);
117 8316 : while (p <= sqrtX && (p = u_forprime_next(&T)))
118 7742 : if (!Sbad || umodiu(Sbad, p))
119 : {
120 7637 : long d = ulogint(X, p) + 1; /* minimal d such that p^d > X */
121 : GEN s;
122 7637 : gp[2] = p; s = eval(E, gp, d);
123 7616 : n = dirmuleuler_small(V, v, n, p, s, d);
124 : }
125 740201 : while ((p = u_forprime_next(&T))) /* sqrt(X) < p <= X */
126 739627 : if (!Sbad || umodiu(Sbad, p))
127 : {
128 : GEN s;
129 739620 : gp[2] = p; s = eval(E, gp, 2); /* s either t_POL or t_SER of val 0 */
130 739620 : if (lg(s) > 3 && !gequal0(gel(s,3)))
131 139153 : dirmuleuler_large(V, p, gel(s,3));
132 : }
133 574 : return gerepilecopy(av0,V);
134 : }
135 :
136 : /* return a t_SER or a truncated t_POL to precision n */
137 : GEN
138 752066 : direuler_factor(GEN s, long n)
139 : {
140 752066 : long t = typ(s);
141 752066 : if (is_scalar_t(t))
142 : {
143 33194 : if (!gequal1(s)) err_direuler(s);
144 33180 : return scalarpol_shallow(s,0);
145 : }
146 718872 : switch(t)
147 : {
148 5712 : case t_POL: break; /* no need to RgXn_red */
149 712845 : case t_RFRAC:
150 : {
151 712845 : GEN p = gel(s,1), q = gel(s,2);
152 712845 : q = RgXn_red_shallow(q,n);
153 712845 : s = RgXn_inv(q, n);
154 712845 : if (typ(p) == t_POL && varn(p) == varn(q))
155 : {
156 28 : p = RgXn_red_shallow(p, n);
157 28 : s = RgXn_mul(s, p, n);
158 : }
159 : else
160 712817 : if (!gequal1(p)) s = RgX_Rg_mul(s, p);
161 712845 : if (!signe(s) || !gequal1(gel(s,2))) err_direuler(s);
162 712831 : break;
163 : }
164 308 : case t_SER:
165 308 : if (!signe(s) || valser(s) || !gequal1(gel(s,2))) err_direuler(s);
166 308 : break;
167 7 : default: pari_err_TYPE("direuler", s);
168 : }
169 718851 : return s;
170 : }
171 :
172 : struct eval_bad
173 : {
174 : void *E;
175 : GEN (*eval)(void *, GEN);
176 : };
177 : static GEN
178 688303 : eval_bad(void *E, GEN p, long n)
179 : {
180 688303 : struct eval_bad *d = (struct eval_bad*) E;
181 688303 : return direuler_factor(d->eval(d->E, p), n);
182 : }
183 : GEN
184 301 : direuler(void *E, GEN (*eval)(void *, GEN), GEN a, GEN b, GEN c)
185 : {
186 : struct eval_bad d;
187 301 : d.E= E; d.eval = eval;
188 301 : return direuler_bad((void*)&d, eval_bad, a, b, c, NULL);
189 : }
190 :
191 : static GEN
192 31157 : primelist(forprime_t *T, GEN Sbad, long n, long *running)
193 : {
194 31157 : GEN P = cgetg(n+1, t_VECSMALL);
195 : long i, j;
196 302631 : for (i = 1, j = 1; i <= n; i++)
197 : {
198 275548 : ulong p = u_forprime_next(T);
199 275548 : if (!p) { *running = 0; break; }
200 271474 : if (Sbad && umodiu(Sbad, p)==0) continue;
201 266791 : uel(P,j++) = p;
202 : }
203 31157 : setlg(P, j);
204 31157 : return P;
205 : }
206 :
207 : GEN
208 4081 : pardireuler(GEN worker, GEN a, GEN b, GEN c, GEN Sbad)
209 : {
210 : ulong au, bu, X, sqrtX, n, snX, nX;
211 4081 : pari_sp av0 = avma;
212 : GEN v, V;
213 : forprime_t T;
214 : struct pari_mt pt;
215 4081 : long running = 1, pending = 0;
216 4081 : au = direulertou(a, gceil);
217 4081 : bu = direulertou(b, gfloor);
218 4081 : X = c ? direulertou(c, gfloor): bu;
219 4081 : if (X == 0) return cgetg(1,t_VEC);
220 4081 : if (bu > X) bu = X;
221 4081 : if (!u_forprime_init(&T, au, bu)) { set_avma(av0); return mkvec(gen_1); }
222 4074 : v = vecsmall_ei(X, 1);
223 4074 : V = vec_ei(X, 1);
224 4074 : n = 1;
225 4074 : if (Sbad) Sbad = direuler_Sbad(V, v, Sbad, &n);
226 4074 : sqrtX = usqrt(X); snX = uprimepi(sqrtX); nX = uprimepi(X);
227 4074 : if (snX)
228 : {
229 4060 : GEN P = primelist(&T, Sbad, snX, &running);
230 4060 : GEN R = gel(closure_callgenvec(worker, mkvec2(P, utoi(X))), 2);
231 4060 : long i, l = lg(P);
232 20349 : for (i = 1; i < l; i++)
233 : {
234 16289 : GEN s = gel(R,i);
235 16289 : n = dirmuleuler_small(V, v, n, uel(P,i), s, lg(s));
236 : }
237 14 : } else snX = 1;
238 4074 : mt_queue_start_lim(&pt, worker, (nX+snX-1)/snX);
239 34212 : while (running || pending)
240 : {
241 : GEN done;
242 30138 : GEN P = running? primelist(&T, Sbad, snX, &running): NULL;
243 30138 : mt_queue_submit(&pt, 0, P ? mkvec2(P, utoi(X)): NULL);
244 30138 : done = mt_queue_get(&pt, NULL, &pending);
245 30138 : if (done)
246 : {
247 27097 : GEN P = gel(done,1), R = gel(done,2);
248 27097 : long j, l = lg(P);
249 277599 : for (j=1; j<l; j++)
250 : {
251 250502 : GEN F = gel(R,j);
252 250502 : if (degpol(F) && !gequal0(gel(F,3)))
253 169645 : dirmuleuler_large(V, uel(P,j), gel(F,3));
254 : }
255 : }
256 : }
257 4074 : mt_queue_end(&pt);
258 4074 : return gerepilecopy(av0,V);
259 : }
260 :
261 : /********************************************************************/
262 : /** **/
263 : /** DIRPOWERS and DIRPOWERSSUM **/
264 : /** **/
265 : /********************************************************************/
266 :
267 : /* [1^B,...,N^B] */
268 : GEN
269 686 : vecpowuu(long N, ulong B)
270 : {
271 : GEN v;
272 : long p, i;
273 : forprime_t T;
274 :
275 686 : if (B <= 8000)
276 : {
277 686 : if (!B) return const_vec(N,gen_1);
278 679 : v = cgetg(N+1, t_VEC); if (N == 0) return v;
279 679 : gel(v,1) = gen_1;
280 679 : if (B == 1)
281 92736 : for (i = 2; i <= N; i++) gel(v,i) = utoipos(i);
282 469 : else if (B == 2)
283 : {
284 : ulong o, s;
285 273 : if (N & HIGHMASK)
286 0 : for (i = 2, o = 3; i <= N; i++, o += 2)
287 0 : gel(v,i) = addiu(gel(v,i-1), o);
288 : else
289 31073 : for (i = 2, s = 1, o = 3; i <= N; i++, s += o, o += 2)
290 30800 : gel(v,i) = utoipos(s + o);
291 : }
292 196 : else if (B == 3)
293 840 : for (i = 2; i <= N; i++) gel(v,i) = powuu(i, B);
294 : else
295 : {
296 182 : long k, Bk, e = expu(N);
297 7553 : for (i = 3; i <= N; i += 2) gel(v,i) = powuu(i, B);
298 1239 : for (k = 1; k <= e; k++)
299 : {
300 1057 : N >>= 1; Bk = B * k;
301 8498 : for (i = 1; i <= N; i += 2) gel(v, i << k) = shifti(gel(v, i), Bk);
302 : }
303 : }
304 679 : return v;
305 : }
306 0 : v = const_vec(N, NULL);
307 0 : u_forprime_init(&T, 3, N);
308 0 : while ((p = u_forprime_next(&T)))
309 : {
310 : long m, pk, oldpk;
311 0 : gel(v,p) = powuu(p, B);
312 0 : for (pk = p, oldpk = p; pk; oldpk = pk, pk = umuluu_le(pk,p,N))
313 : {
314 0 : if (pk != p) gel(v,pk) = mulii(gel(v,oldpk), gel(v,p));
315 0 : for (m = N/pk; m > 1; m--)
316 0 : if (gel(v,m) && m%p) gel(v, m*pk) = mulii(gel(v,m), gel(v,pk));
317 : }
318 : }
319 0 : gel(v,1) = gen_1;
320 0 : for (i = 2; i <= N; i+=2)
321 : {
322 0 : long vi = vals(i);
323 0 : gel(v,i) = shifti(gel(v,i >> vi), B * vi);
324 : }
325 0 : return v;
326 : }
327 :
328 : /* does x^s require log(x) ? */
329 : static long
330 12871 : get_needlog(GEN s)
331 : {
332 12871 : switch(typ(s))
333 : {
334 294 : case t_REAL: return 2; /* yes but not powcx */
335 9443 : case t_COMPLEX: return 1; /* yes using powcx */
336 3134 : default: return 0; /* no */
337 : }
338 : }
339 : /* [1^B,...,N^B] */
340 : GEN
341 12024 : vecpowug(long N, GEN B, long prec)
342 : {
343 12024 : GEN v, logp = NULL;
344 12024 : long gp[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
345 12024 : long p, precp = 2, prec0, prec1, needlog;
346 : forprime_t T;
347 12024 : if (N == 1) return mkvec(gen_1);
348 12003 : if (typ(B) == t_INT && lgefint(B) <= 3 && signe(B) >= 0)
349 168 : return vecpowuu(N, itou(B));
350 11835 : needlog = get_needlog(B);
351 11835 : prec1 = prec0 = prec;
352 11835 : if (needlog == 1) prec1 = powcx_prec(log2((double)N), B, prec);
353 11835 : u_forprime_init(&T, 2, N);
354 11835 : v = const_vec(N, NULL);
355 11835 : gel(v,1) = gen_1;
356 1565942 : while ((p = u_forprime_next(&T)))
357 : {
358 : long m, pk, oldpk;
359 : GEN u;
360 1554107 : gp[2] = p;
361 1554107 : if (needlog)
362 : {
363 96003 : if (!logp)
364 17444 : logp = logr_abs(utor(p, prec1));
365 : else
366 : { /* Assuming p and precp are odd,
367 : * log p = log(precp) + 2 atanh((p - precp) / (p + precp)) */
368 78559 : ulong a = p >> 1, b = precp >> 1; /* p = 2a + 1, precp = 2b + 1 */
369 78559 : GEN z = atanhuu(a - b, a + b + 1, prec1); /* avoid overflow */
370 78559 : shiftr_inplace(z, 1); logp = addrr(logp, z);
371 : }
372 92479 : u = needlog == 1? powcx(gp, logp, B, prec0)
373 96003 : : mpexp(gmul(B, logp));
374 96003 : if (p == 2) logp = NULL; /* reset: precp must be odd */
375 : }
376 : else
377 1458104 : u = gpow(gp, B, prec0);
378 1554107 : precp = p;
379 1554107 : gel(v,p) = u; /* p^B */
380 1554107 : if (prec0 != prec) gel(v,p) = gprec_wtrunc(gel(v,p), prec);
381 3221888 : for (pk = p, oldpk = p; pk; oldpk = pk, pk = umuluu_le(pk,p,N))
382 : {
383 1667781 : if (pk != p) gel(v,pk) = gmul(gel(v,oldpk), gel(v,p));
384 46350452 : for (m = N/pk; m > 1; m--)
385 44682671 : if (gel(v,m) && m%p) gel(v, m*pk) = gmul(gel(v,m), gel(v,pk));
386 : }
387 : }
388 11835 : return v;
389 : }
390 :
391 : GEN
392 665 : dirpowers(long n, GEN x, long prec)
393 : {
394 : pari_sp av;
395 : GEN v;
396 665 : if (n <= 0) return cgetg(1, t_VEC);
397 651 : av = avma; v = vecpowug(n, x, prec);
398 651 : if (typ(x) == t_INT && lgefint(x) <= 3 && signe(x) >= 0 && cmpiu(x, 2) <= 0)
399 133 : return v;
400 518 : return gerepilecopy(av, v);
401 : }
402 :
403 : /* f is a totally multiplicative function of modulus 0 or 1
404 : * (essentially a Dirichlet character). Compute simultaneously
405 : * sum_{0 < n <= N} f(n)n^s and sum_{0 < n <= N} f(n)n^{-1-conj(s)}
406 : * Warning: s is conjugated, but not f. Main application for Riemann-Siegel,
407 : * where we need R(chi,s) and conj(R(chi,1-conj(s))). */
408 :
409 : static GEN
410 3171 : vecmulsqlv(GEN Q, GEN V)
411 : {
412 : long l, i;
413 : GEN W;
414 3171 : if (typ(V) != t_VEC) return RgV_Rg_mul(Q, V);
415 1176 : l = lg(Q); W = cgetg(l, t_VEC);
416 61677 : for (i = 1; i < l; i++) gel(W, i) = vecmul(gel(Q, i), V);
417 1176 : return W;
418 : }
419 :
420 : /* P = prime divisors of (squarefree) n, V[i] = i^s for i <= sq.
421 : * Return NULL if n is not sq-smooth, else f(n)n^s */
422 : static GEN
423 18197803 : smallfact(ulong n, GEN P, ulong sq, GEN V)
424 : {
425 : long i, l;
426 : ulong p, m, o;
427 : GEN c;
428 18197803 : if (n <= sq) return gel(V,n);
429 18148866 : l = lg(P); m = p = uel(P, l-1); if (p > sq) return NULL;
430 3841134 : for (i = l-2; i > 1; i--, m = o) { p = uel(P,i); o = m*p; if (o > sq) break; }
431 3807823 : c = gel(V,m); n /= m; /* m <= sq, o = m * p > sq */
432 3807823 : if (n > sq) { c = vecmul(c, gel(V,p)); n /= p; }
433 3799296 : return vecmul(c, gel(V,n));
434 : }
435 :
436 : static GEN
437 910 : _Qtor(GEN x, long prec)
438 910 : { return typ(x) == t_FRAC? fractor(x, prec): x; }
439 : static GEN
440 2373 : Qtor(GEN x, long prec)
441 : {
442 2373 : long tx = typ(x);
443 3283 : if (tx == t_VEC || tx == t_COL) pari_APPLY_same(_Qtor(gel(x, i), prec));
444 1568 : return tx == t_FRAC? fractor(x, prec): x;
445 : }
446 :
447 : static GEN
448 210 : vecf(long N, void *E, GEN (*f)(void *, ulong, long), long prec)
449 : {
450 : GEN v;
451 : long n;
452 210 : if (!f) return NULL;
453 112 : v = cgetg(N + 1, t_VEC);
454 32557 : for (n = 1; n <= N; n++) gel(v,n) = f(E, n, prec);
455 112 : return v;
456 : }
457 :
458 : /* Here #V = #F > 0 is small. Analogous to dotproduct, with following
459 : * semantic differences: uses V[1] = 1; V has scalar values but F may have
460 : * vector values */
461 : static GEN
462 259 : naivedirpowerssum(GEN V, GEN F, long prec)
463 : {
464 : GEN S;
465 259 : if (!F) S = RgV_sum(V);
466 : else
467 : {
468 147 : long n, l = lg(V);
469 147 : S = gel(F,1); /* V[1] = 1 */
470 33103 : for (n = 2; n < l; n++) S = gadd(S, gmul(gel(V, n), gel(F, n)));
471 : }
472 259 : return Qtor(S, prec);
473 : }
474 :
475 : static GEN
476 252 : smalldirpowerssum(long N, GEN s, void *E, GEN (*f)(void *, ulong, long),
477 : long both, long prec)
478 : {
479 : GEN F, V, S, SB, sb;
480 252 : if (!N)
481 : {
482 42 : if (!f) return both? mkvec2(gen_0, gen_0): gen_0;
483 28 : return gmul(gen_0, f(E, 1, prec));
484 : }
485 210 : V = vecpowug(N, s, prec);
486 210 : F = vecf(N, E, f, prec);
487 210 : S = naivedirpowerssum(V, F, prec);
488 210 : if (!both) return S;
489 :
490 70 : sb = conj_i(gsubsg(-1, s));
491 70 : if ((both==2 || !f) && gequal(s,sb))
492 21 : SB = S;
493 : else
494 : {
495 49 : GEN VB = cgetg(N+1, t_VEC), FB = (both == 1 && F)? conj_i(F): F;
496 : long n;
497 49 : gel(VB, 1) = gel(V, 1); /* = 1 */
498 1428 : for (n = 2; n <= N; n++) gel(VB, n) = ginv(gmulsg(n, gel(V, n)));
499 49 : SB = naivedirpowerssum(VB, FB, prec);
500 : }
501 70 : return mkvec2(S, SB);
502 : }
503 :
504 : static void
505 68368 : v2unpack(GEN v, GEN *pV, GEN *pVB)
506 : {
507 68368 : if (typ(v) == t_COL) { *pV = gel(v,1); *pVB = gel(v,2); }
508 68326 : else { *pV = v; *pVB = NULL; }
509 68368 : }
510 : static GEN
511 69344 : v2pack(GEN V, GEN VB) { return VB? mkcol2(V,VB): V; }
512 :
513 : static GEN
514 1036 : dirpowsuminit(GEN s, GEN onef, GEN zerf, void *E, GEN (*f)(void *, ulong, long), GEN data, long both)
515 : {
516 1036 : long needlog = data[1], prec0 = data[2], prec1 = data[3];
517 1036 : ulong a, b, c, e, q, n, sq = usqrt(data[4]);
518 1036 : GEN V = cgetg(sq+1, t_VEC), W = cgetg(sq+1, t_VEC), VB = NULL, WB = NULL;
519 1036 : GEN Q = cgetg(sq+1, t_VEC), Z = cgetg(sq+1, t_VEC), QB = NULL, ZB = NULL;
520 1036 : GEN logp, c2, Q2, Q3, Q6, c2B = NULL, Q2B = NULL, Q3B = NULL, Q6B = NULL;
521 1036 : long gp[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
522 :
523 1036 : if (both == 1 || (both == 2 && !gequal(real_i(s), gneg(ghalf))))
524 21 : { VB = cgetg(sq+1, t_VEC); WB = cgetg(sq+1, t_VEC); QB = cgetg(sq+1, t_VEC);}
525 1036 : gel(V, 1) = gel(W, 1) = gel(Q, 1) = onef;
526 1036 : if (VB) { gel(VB, 1) = gel(WB, 1) = gel(QB, 1) = onef; }
527 1036 : c2 = gpow(gen_2, s, prec0); if (VB) c2B = ginv(gmul2n(conj_i(c2), 1));
528 1036 : if (f)
529 : {
530 1015 : GEN tmp2 = f(E, 2, prec0);
531 1015 : c2 = gmul(c2, tmp2); if (VB) c2B = gmul(c2B, tmp2);
532 : }
533 1036 : gel(V,2) = c2; /* f(2) 2^s */
534 1036 : gel(W,2) = Qtor(gadd(c2, onef), prec0);
535 1036 : gel(Q,2) = Qtor(gadd(vecsqr(c2), onef), prec0);
536 1036 : if (VB)
537 : {
538 21 : gel(VB, 2) = c2B; gel(WB, 2) = Qtor(gadd(c2B, onef), prec0);
539 21 : gel(QB, 2) = Qtor(gadd(vecsqr(c2B), onef), prec0);
540 : }
541 1036 : logp = NULL;
542 158697 : for (n = 3; n <= sq; n++)
543 : {
544 157661 : GEN u = NULL, uB = NULL, ks = f ? f(E, n, prec0) : gen_1;
545 157661 : if (gequal0(ks)) ks = NULL;
546 157661 : if (odd(n))
547 : {
548 79310 : gp[2] = n;
549 79310 : if (needlog)
550 : {
551 77763 : if (!logp)
552 1015 : logp = logr_abs(utor(n, prec1));
553 : else
554 : { /* log n = log(n-2) + 2 atanh(1 / (n - 1)) */
555 76748 : GEN z = atanhuu(1, n - 1, prec1);
556 76748 : shiftr_inplace(z, 1); logp = addrr(logp, z);
557 : }
558 77763 : if (ks)
559 76188 : u = needlog == 1? powcx(gp, logp, s, prec0) : mpexp(gmul(s, logp));
560 : }
561 1547 : else if (ks) u = gpow(gp, s, prec0);
562 79310 : if (ks)
563 : {
564 77721 : if (VB) uB = gmul(ginv(gmulsg(n, conj_i(u))), ks);
565 77721 : u = gmul(u, ks); /* f(n) n^s */
566 : }
567 : }
568 : else
569 : {
570 78351 : u = vecmul(c2, gel(V, n >> 1));
571 78351 : if (VB) uB = vecmul(c2B, gel(VB, n >> 1));
572 : }
573 157661 : if (ks)
574 : {
575 154651 : gel(V,n) = u; /* f(n) n^s */
576 154651 : gel(W,n) = gadd(gel(W, n-1), gel(V,n)); /*= sum_{i<=n} f(i)i^s */
577 154651 : gel(Q,n) = gadd(gel(Q, n-1), vecsqr(gel(V,n)));/*= sum_{i<=n} f(i^2)i^2s*/
578 154651 : if (VB)
579 : {
580 462 : gel(VB,n) = uB;
581 462 : gel(WB,n) = gadd(gel(WB,n-1), gel(VB,n));
582 462 : gel(QB,n) = gadd(gel(QB,n-1), vecsqr(gel(VB,n)));
583 : }
584 : }
585 : else
586 : {
587 3010 : gel(V,n) = zerf; gel(W,n) = gel(W, n-1); gel(Q,n) = gel(Q, n-1);
588 3010 : if (VB)
589 0 : { gel(VB,n) = zerf; gel(WB,n) = gel(WB, n-1); gel(QB,n) = gel(QB, n-1); }
590 : }
591 : }
592 1036 : Q2 = vecmulsqlv(Q, gel(V,2));
593 1036 : Q3 = vecmulsqlv(Q, gel(V,3));
594 1036 : Q6 = vecmulsqlv(Q, gel(V,6));
595 1036 : if (VB)
596 : {
597 21 : Q2B = vecmulsqlv(QB, gel(VB,2));
598 21 : Q3B = vecmulsqlv(QB, gel(VB,3));
599 21 : Q6B = vecmulsqlv(QB, gel(VB,6));
600 : }
601 : /* a,b,c,e = sqrt(q), sqrt(q/2), sqrt(q/3), sqrt(q/6)
602 : * Z[q] = Q[a] + 2^s Q[b] + 3^s Q[c] + 6^s Q[e], with Q[0] = 0 */
603 1036 : gel(Z, 1) = onef;
604 1036 : gel(Z, 2) = gel(W, 2);
605 1036 : gel(Z, 3) = gel(W, 3);
606 1036 : gel(Z, 4) = gel(Z, 5) = gel(W, 4);
607 1036 : gel(Z, 6) = gel(Z, 7) = gadd(gel(W, 4), gel(V, 6));
608 1036 : if (VB)
609 : {
610 21 : ZB = cgetg(sq+1, t_VEC);
611 21 : gel(ZB, 1) = onef;
612 21 : gel(ZB, 2) = gel(WB, 2);
613 21 : gel(ZB, 3) = gel(WB, 3);
614 21 : gel(ZB, 4) = gel(ZB, 5) = gel(WB, 4);
615 21 : gel(ZB, 6) = gel(ZB, 7) = gadd(gel(WB, 4), gel(VB, 6));
616 : }
617 1036 : a = 2; b = c = e = 1;
618 153517 : for (q = 8; q <= sq; q++)
619 : { /* Gray code: at most one of a,b,c,d differs (by 1) from previous value */
620 152481 : GEN z = gel(Z, q - 1), zB = NULL;
621 : ulong na, nb, nc, ne, na2, nb2, nc2, ne2;
622 152481 : if (VB) zB = gel(ZB, q - 1);
623 152481 : if ((na = usqrt(q)) != a)
624 9191 : { a = na; na2 = na * na; z = gadd(z, gel(V, na2));
625 9191 : if (VB) zB = gadd(zB, gel(VB, na2)); }
626 143290 : else if ((nb = usqrt(q / 2)) != b)
627 6202 : { b = nb; nb2 = 2 * nb * nb; z = gadd(z, gel(V, nb2));
628 6202 : if (VB) zB = gadd(zB, gel(VB, nb2)); }
629 137088 : else if ((nc = usqrt(q / 3)) != c)
630 5418 : { c = nc; nc2 = 3 * nc * nc; z = gadd(z, gel(V, nc2));
631 5418 : if (VB) zB = gadd(zB, gel(VB, nc2)); }
632 131670 : else if ((ne = usqrt(q / 6)) != e)
633 3108 : { e = ne; ne2 = 6 * ne * ne; z = gadd(z, gel(V, ne2));
634 3108 : if (VB) zB = gadd(zB, gel(VB, ne2)); }
635 152481 : gel(Z, q) = z; if (VB) gel(ZB, q) = zB;
636 : }
637 1057 : return v2pack(mkvecn(7, V, W, Q, Q2, Q3, Q6, Z),
638 21 : VB? mkvecn(7, VB, WB, QB, Q2B, Q3B, Q6B, ZB): NULL);
639 : }
640 :
641 : static GEN
642 38188 : sumprimeloop(forprime_t *pT, GEN s, long N, GEN data, GEN S, GEN W, GEN WB,
643 : void *E, GEN (*f)(void *, ulong, long))
644 : {
645 38188 : pari_sp av = avma;
646 38188 : long needlog = data[1], prec0 = data[2], prec1 = data[3];
647 38188 : long gp[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
648 38188 : ulong p, precp = 0;
649 38188 : GEN logp = NULL, SB = WB? S: NULL;
650 :
651 5281491 : while ((p = u_forprime_next(pT)))
652 : {
653 5243406 : GEN u = NULL, ks;
654 5243406 : if (!f) ks = gen_1;
655 : else
656 : {
657 5174519 : ks = f(E, p, prec1);
658 5174344 : if (gequal0(ks)) ks = NULL;
659 : }
660 5243446 : gp[2] = p;
661 5243446 : if (needlog)
662 : {
663 5167230 : if (!logp)
664 30377 : logp = logr_abs(utor(p, prec1));
665 : else
666 : { /* Assuming p and precp are odd,
667 : * log p = log(precp) + 2 atanh((p - precp) / (p + precp)) */
668 5136853 : ulong a = p >> 1, b = precp >> 1; /* p = 2a + 1, precp = 2b + 1 */
669 5136853 : GEN z = atanhuu(a - b, a + b + 1, prec1); /* avoid overflow */
670 5136360 : shiftr_inplace(z, 1); logp = addrr(logp, z);
671 : }
672 10334047 : if (ks) u = needlog == 1? powcx(gp, logp, s, prec0)
673 5167158 : : mpexp(gmul(s, logp));
674 : }
675 76216 : else if (ks) u = gpow(gp, s, prec0);
676 5245686 : if (ks)
677 : {
678 5246111 : S = gadd(S, vecmul(gel(W, N / p), ks == gen_1? u: gmul(ks, u)));
679 5243728 : if (WB)
680 : {
681 2345 : GEN w = gel(WB, N / p);
682 2345 : if (ks != gen_1) w = vecmul(ks, w);
683 2345 : SB = gadd(SB, gdiv(w, gmulsg(p, conj_i(u))));
684 : }
685 : }
686 5243303 : precp = p;
687 5243303 : if ((p & 0x1ff) == 1)
688 : {
689 19565 : if (!logp) gerepileall(av, SB? 2: 1, &S, &SB);
690 19285 : else gerepileall(av, SB? 3: 2, &S, &logp, &SB);
691 : }
692 : }
693 37984 : return v2pack(S, SB);
694 : }
695 :
696 : static GEN
697 48412 : add4(GEN a, GEN b, GEN c, GEN d) { return gadd(gadd(a,b), gadd(c,d)); }
698 :
699 : static const long step = 2048;
700 : static int
701 29547 : mksqfloop(long N, long x1, GEN R, GEN RB, GEN *pS, GEN *pSB)
702 : {
703 29547 : GEN V = gel(R,1), Q = gel(R,3), Q2 = gel(R,4);
704 29547 : GEN Q3 = gel(R,5), Q6 = gel(R,6), Z = gel(R,7);
705 29547 : GEN v, VB = NULL, QB = NULL, Q2B = NULL, Q3B = NULL, Q6B = NULL, ZB = NULL;
706 29547 : long x2, j, lv, sq = lg(V)-1;
707 :
708 29547 : if (RB)
709 21 : { VB = gel(RB,1); QB = gel(RB,3); Q2B = gel(RB,4);
710 21 : Q3B = gel(RB,5), Q6B = gel(RB,6); ZB = gel(RB,7); }
711 : /* beware overflow, fuse last two bins (avoid a tiny remainder) */
712 29547 : x2 = (N >= 2*step && N - 2*step >= x1)? x1-1 + step: N;
713 29547 : v = vecfactorsquarefreeu_coprime(x1, x2, mkvecsmall2(2, 3));
714 28143 : lv = lg(v);
715 59911030 : for (j = 1; j < lv; j++)
716 59881486 : if (gel(v,j))
717 : {
718 18199949 : ulong d = x1 - 1 + j; /* squarefree, coprime to 6 */
719 18199949 : GEN t = smallfact(d, gel(v,j), sq, V), u;
720 18175444 : GEN tB = NULL, uB = NULL; /* = f(d) d^s */
721 : long a, b, c, e, q;
722 18175444 : if (!t || gequal0(t)) continue;
723 3791089 : if (VB) tB = vecinv(gmulsg(d, conj_i(t)));
724 : /* warning: gives 1/conj(f(d)) d^(-1-conj(s)), equal to
725 : f(d) d^(-1-conj(s)) only if |f(d)|=1. */
726 : /* S += f(d) * d^s * Z[q] */
727 3862641 : q = N / d;
728 3862641 : if (q == 1)
729 : {
730 1472373 : *pS = gadd(*pS, t); if (VB) *pSB = gadd(*pSB, tB);
731 1475092 : continue;
732 : }
733 2390268 : if (q <= sq) { u = gel(Z, q); if (VB) uB = gel(ZB, q); }
734 : else
735 : {
736 48204 : a = usqrt(q); b = usqrt(q / 2); c = usqrt(q / 3); e = usqrt(q / 6);
737 48251 : u = add4(gel(Q,a), gel(Q2,b), gel(Q3,c), gel(Q6,e));
738 48251 : if (VB) uB = add4(gel(QB,a), gel(Q2B,b), gel(Q3B,c), gel(Q6B,e));
739 : }
740 2390315 : *pS = gadd(*pS, vecmul(t, u)); if (VB) *pSB = gadd(*pSB, vecmul(tB, uB));
741 : }
742 29544 : return x2 == N;
743 : }
744 :
745 : static GEN
746 1036 : mkdata(long N, GEN s, long prec)
747 : {
748 1036 : long needlog, prec0, prec1, m = mt_nbthreads(), STEP = maxss(N / (m * m), 1);
749 1036 : prec1 = prec0 = prec + EXTRAPRECWORD;
750 1036 : needlog = get_needlog(s);
751 1036 : if (needlog == 1) prec1 = powcx_prec(log2((double)N), s, prec);
752 1036 : return mkvecsmalln(5, needlog, prec0, prec1, N, STEP);
753 : }
754 :
755 : static GEN
756 13748 : gp_callUp(void *E, ulong x, long prec)
757 : {
758 13748 : long n[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
759 13748 : n[2] = x; return gp_callprec(E, n, prec);
760 : }
761 :
762 : /* set *p0 and *p1 to 0 and 1 in the algebra where f takes its values */
763 : static int
764 1036 : mk01(void *E, GEN (*f)(void*,ulong,long), long prec, GEN *p0, GEN *p1)
765 : {
766 1036 : *p0 = gen_0; *p1 = gen_1;
767 1036 : if (!f) return 1;
768 1015 : *p1 = f(E, 1, prec);
769 1015 : if (is_vec_t(typ(*p1)))
770 : {
771 392 : long l = lg(*p1);
772 392 : if (l == 1) { *p0 = *p1 = NULL; return 0; }
773 392 : *p0 = const_vec(l-1, gen_0);
774 : }
775 1015 : return 1;
776 : }
777 : static GEN
778 0 : mktrivial(long both)
779 : {
780 0 : if (!both) return cgetg(1, t_VEC);
781 0 : return mkvec2(cgetg(1,t_VEC), cgetg(1,t_VEC));
782 : }
783 :
784 : /* both is
785 : * 0: sum_{n<=N}f(n)n^s
786 : * 1: sum for (f,s) and (conj(f),-1-s)
787 : * 2: sum for (f,s) and (f,-1-s), assuming |f(n)| in {0,1} */
788 : static GEN
789 203 : dirpowerssumfun_i(ulong N, GEN s, void *E, GEN (*f)(void *, ulong, long),
790 : long both, long prec)
791 : {
792 : forprime_t T;
793 : pari_sp av;
794 : GEN onef, zerf, R, RB, W, WB, S, SB, data;
795 : ulong x1;
796 :
797 203 : if ((f && N < 49) || (!f && N < 1000))
798 140 : return smalldirpowerssum(N, s, E, f, both, prec);
799 63 : if (!mk01(E, f, prec, &zerf, &onef)) return mktrivial(both);
800 63 : data = mkdata(N, s, prec); s = gprec_w(s, prec + EXTRAPRECWORD);
801 63 : v2unpack(dirpowsuminit(s, onef, zerf, E, f, data, both), &R, &RB);
802 63 : W = gel(R,2); WB = RB? gel(RB,2): NULL;
803 63 : av = avma; u_forprime_init(&T, lg(W), N);
804 63 : v2unpack(sumprimeloop(&T, s, N, data, zerf, W, WB, E, f), &S, &SB);
805 413 : for(x1 = 1;; x1 += step)
806 : {
807 413 : if (mksqfloop(N, x1, R, RB, &S, &SB))
808 63 : return both? mkvec2(S, conj_i(SB? SB: S)): S;
809 350 : gerepileall(av, SB? 2: 1, &S, &SB);
810 : }
811 : }
812 : GEN
813 203 : dirpowerssumfun(ulong N, GEN s, void *E, GEN (*f)(void *, ulong, long),
814 : long both, long prec)
815 : {
816 203 : pari_sp av = avma;
817 203 : return gerepilecopy(av, dirpowerssumfun_i(N, s, E, f, both, prec));
818 : }
819 :
820 : GEN
821 133 : dirpowerssum(ulong N, GEN s, long both, long prec)
822 133 : { return dirpowerssumfun(N, s, NULL, NULL, both, prec); }
823 :
824 : GEN
825 154 : dirpowerssum0(GEN N, GEN s, GEN f, long both, long prec)
826 : {
827 154 : if (typ(N) != t_INT) pari_err_TYPE("dirpowerssum", N);
828 147 : if (signe(N) <= 0) N = gen_0;
829 147 : if (!f) return dirpowerssum(itou(N), s, both, prec);
830 70 : if (typ(f) != t_CLOSURE) pari_err_TYPE("dirpowerssum", f);
831 70 : return dirpowerssumfun(itou(N), s, (void*)f, gp_callUp, both, prec);
832 : }
833 :
834 : GEN
835 29134 : parsqf_worker(GEN gk, GEN vR, GEN gN)
836 : {
837 29134 : pari_sp av = avma;
838 : GEN R, RB, onef, S, SB;
839 29134 : long k = itou(gk), N = itou(gN), x1 = 1 + step * k;
840 29134 : v2unpack(vR, &R, &RB); onef = gmael(R,1,1);
841 29134 : S = SB = is_vec_t(typ(onef)) ? zerovec(lg(onef) - 1): gen_0;
842 29134 : (void)mksqfloop(N, x1, R, RB, &S, &SB);
843 29131 : return gerepilecopy(av, v2pack(S, RB? SB: NULL));
844 : }
845 :
846 : static GEN
847 5350268 : mycallvec(void *f, ulong n, long prec)
848 : {
849 5350268 : GEN F = (GEN)f;
850 5350268 : if (!f) return gen_1;
851 389757 : if (typ(F) == t_CLOSURE) return gp_callUp(f, n, prec);
852 389757 : return gel(F, (n-1) % (lg(F)-1) + 1);
853 : }
854 :
855 : GEN
856 38134 : parsumprimefun_worker(GEN gk, GEN s, GEN zerf, GEN data, GEN vW, GEN f)
857 : {
858 : forprime_t T;
859 : GEN W, WB;
860 38134 : long k = itou(gk), sq, N = data[4], STEP = data[5];
861 :
862 38133 : v2unpack(vW, &W, &WB); sq = lg(W)-1;
863 38133 : if (isintzero(f)) f = NULL;
864 38132 : u_forprime_init(&T, k * STEP + sq + 1, minss(N, (k + 1) * STEP + sq));
865 38119 : return sumprimeloop(&T, s, N, data, zerf, W, WB, (void*)f, mycallvec);
866 : }
867 :
868 : static GEN
869 973 : vR_get_vW(GEN vR)
870 : {
871 : GEN R, RB, W, WB;
872 973 : v2unpack(vR, &R, &RB); W = gel(R,2); WB = RB? gel(RB,2): NULL;
873 973 : return v2pack(W, WB);
874 : }
875 :
876 : static GEN
877 1085 : pardirpowerssumfun_i(GEN f, ulong N, GEN s, long both, long prec)
878 : {
879 : GEN worker, worker2, data, vR, onef, zerf;
880 :
881 1085 : if ((f && N < 49) || (!f && N < 10000UL))
882 112 : return smalldirpowerssum(N, s, (void*)f, mycallvec, both, prec);
883 973 : if (!mk01((void*)f, mycallvec, prec, &zerf, &onef)) return mktrivial(both);
884 973 : data = mkdata(N, s, prec); s = gprec_w(s, prec + EXTRAPRECWORD);
885 973 : vR = dirpowsuminit(s, onef, zerf, (void*)f, mycallvec, data, both);
886 973 : worker = snm_closure(is_entry("_parsumprimefun_worker"),
887 : mkvecn(5, s, zerf, data, vR_get_vW(vR), f? f: gen_0));
888 973 : worker2 = snm_closure(is_entry("_parsqf_worker"), mkvec2(vR, utoi(N)));
889 973 : return gadd(parsum(gen_0, utoipos((N-1) / data[5]), worker),
890 973 : parsum(gen_0, utoipos(maxss((N-1) / step - 1, 0)), worker2));
891 : }
892 :
893 : GEN
894 1085 : pardirpowerssumfun(GEN f, ulong N, GEN s, long both, long prec)
895 : {
896 1085 : pari_sp av = avma;
897 1085 : return gerepilecopy(av, pardirpowerssumfun_i(f, N, s, both, prec));
898 : }
899 : GEN
900 0 : pardirpowerssum0(GEN N, GEN s, GEN f, long both, long prec)
901 : {
902 0 : if (typ(N) != t_INT) pari_err_TYPE("pardirpowerssum", N);
903 0 : return pardirpowerssumfun(f, itou(N), s, both, prec);
904 : }
905 : GEN
906 0 : pardirpowerssum(ulong N, GEN s, long prec)
907 0 : { return pardirpowerssumfun(NULL, N, s, 0, prec); }
|