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 28518 : dirmuleuler_small(GEN V, GEN v, long n, ulong p, GEN s, long d)
33 : {
34 28518 : long i, j, m = n, D = minss(d+2, lg(s));
35 28518 : ulong q = 1, X = lg(V)-1;
36 :
37 94164 : for (i = 3, q = p; i < D; i++, q *= p) /* q*p does not overflow */
38 : {
39 65646 : GEN aq = gel(s,i);
40 65646 : if (gequal0(aq)) continue;
41 : /* j = 1 */
42 53424 : gel(V,q) = aq;
43 53424 : v[++n] = q;
44 3266956 : for (j = 2; j <= m; j++)
45 : {
46 3213532 : ulong nj = umuluu_le(uel(v,j), q, X);
47 3213532 : if (!nj) continue;
48 191870 : gel(V,nj) = gmul(aq, gel(V,v[j]));
49 191870 : v[++n] = nj;
50 : }
51 : }
52 28518 : return n;
53 : }
54 :
55 : /* ap != 0 for efficiency, p > sqrt(X) */
56 : static void
57 308658 : dirmuleuler_large(GEN V, ulong p, GEN ap)
58 : {
59 308658 : long j, jp, X = lg(V)-1;
60 308658 : gel(V,p) = ap;
61 1506344 : for (j = 2, jp = 2*p; jp <= X; j++, jp += p) gel(V,jp) = gmul(ap, gel(V,j));
62 308658 : }
63 :
64 : static ulong
65 10171 : direulertou(GEN a, GEN fl(GEN))
66 : {
67 10171 : 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 10150 : 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 623 : 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 623 : pari_sp av0 = avma;
104 : GEN gp, v, V;
105 : forprime_t T;
106 623 : au = direulertou(a, gceil);
107 616 : bu = direulertou(b, gfloor);
108 609 : X = c ? direulertou(c, gfloor): bu;
109 602 : if (X == 0) return cgetg(1,t_VEC);
110 595 : if (bu > X) bu = X;
111 595 : if (!u_forprime_init(&T, au, bu)) { set_avma(av0); return mkvec(gen_1); }
112 581 : v = vecsmall_ei(X, 1);
113 581 : V = vec_ei(X, 1);
114 581 : n = 1;
115 581 : if (Sbad) Sbad = direuler_Sbad(V, v, Sbad, &n);
116 546 : p = 1; gp = cgetipos(3); sqrtX = usqrt(X);
117 8085 : while (p <= sqrtX && (p = u_forprime_next(&T)))
118 7560 : if (!Sbad || umodiu(Sbad, p))
119 : {
120 7455 : long d = ulogint(X, p) + 1; /* minimal d such that p^d > X */
121 : GEN s;
122 7455 : gp[2] = p; s = eval(E, gp, d);
123 7434 : n = dirmuleuler_small(V, v, n, p, s, d);
124 : }
125 739858 : while ((p = u_forprime_next(&T))) /* sqrt(X) < p <= X */
126 739333 : if (!Sbad || umodiu(Sbad, p))
127 : {
128 : GEN s;
129 739326 : gp[2] = p; s = eval(E, gp, 2); /* s either t_POL or t_SER of val 0 */
130 739326 : if (lg(s) > 3 && !gequal0(gel(s,3)))
131 139013 : dirmuleuler_large(V, p, gel(s,3));
132 : }
133 525 : return gerepilecopy(av0,V);
134 : }
135 :
136 : /* return a t_SER or a truncated t_POL to precision n */
137 : GEN
138 751590 : direuler_factor(GEN s, long n)
139 : {
140 751590 : long t = typ(s);
141 751590 : if (is_scalar_t(t))
142 : {
143 33194 : if (!gequal1(s)) err_direuler(s);
144 33180 : return scalarpol_shallow(s,0);
145 : }
146 718396 : switch(t)
147 : {
148 5712 : case t_POL: break; /* no need to RgXn_red */
149 712369 : case t_RFRAC:
150 : {
151 712369 : GEN p = gel(s,1), q = gel(s,2);
152 712369 : q = RgXn_red_shallow(q,n);
153 712369 : s = RgXn_inv(q, n);
154 712369 : 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 712341 : if (!gequal1(p)) s = RgX_Rg_mul(s, p);
161 712369 : if (!signe(s) || !gequal1(gel(s,2))) err_direuler(s);
162 712355 : 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 718375 : return s;
170 : }
171 :
172 : struct eval_bad
173 : {
174 : void *E;
175 : GEN (*eval)(void *, GEN);
176 : };
177 : static GEN
178 687827 : eval_bad(void *E, GEN p, long n)
179 : {
180 687827 : struct eval_bad *d = (struct eval_bad*) E;
181 687827 : return direuler_factor(d->eval(d->E, p), n);
182 : }
183 : GEN
184 252 : direuler(void *E, GEN (*eval)(void *, GEN), GEN a, GEN b, GEN c)
185 : {
186 : struct eval_bad d;
187 252 : d.E= E; d.eval = eval;
188 252 : 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 34711 : while (running || pending)
240 : {
241 : GEN done;
242 30637 : GEN P = running? primelist(&T, Sbad, snX, &running): NULL;
243 30637 : mt_queue_submit(&pt, 0, P ? mkvec2(P, utoi(X)): NULL);
244 30637 : done = mt_queue_get(&pt, NULL, &pending);
245 30637 : 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 n^s require log(x) ? */
329 : static long
330 11842 : get_needlog(GEN s)
331 : {
332 11842 : switch(typ(s))
333 : {
334 294 : case t_REAL: return 2; /* yes but not powcx */
335 8421 : case t_COMPLEX: return 1; /* yes using powcx */
336 3127 : default: return 0; /* no */
337 : }
338 : }
339 : /* [1^B,...,N^B] */
340 : GEN
341 11954 : vecpowug(long N, GEN B, long prec)
342 : {
343 11954 : GEN v, logp = NULL;
344 11954 : long gp[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
345 11954 : long p, precp = 2, prec0, prec1, needlog;
346 : forprime_t T;
347 11954 : if (N == 1) return mkvec(gen_1);
348 11947 : if (typ(B) == t_INT && lgefint(B) <= 3 && signe(B) >= 0)
349 168 : return vecpowuu(N, itou(B));
350 11779 : needlog = get_needlog(B);
351 11779 : prec1 = prec0 = prec;
352 11779 : if (needlog == 1) prec1 = powcx_prec(log2((double)N), B, prec);
353 11779 : u_forprime_init(&T, 2, N);
354 11779 : v = const_vec(N, NULL);
355 11779 : gel(v,1) = gen_1;
356 1559236 : while ((p = u_forprime_next(&T)))
357 : {
358 : long m, pk, oldpk;
359 : GEN u;
360 1547457 : gp[2] = p;
361 1547457 : if (needlog)
362 : {
363 91306 : if (!logp)
364 17346 : 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 73960 : ulong a = p >> 1, b = precp >> 1; /* p = 2a + 1, precp = 2b + 1 */
369 73960 : GEN z = atanhuu(a - b, a + b + 1, prec1); /* avoid overflow */
370 73960 : shiftr_inplace(z, 1); logp = addrr(logp, z);
371 : }
372 87782 : u = needlog == 1? powcx(gp, logp, B, prec0)
373 91306 : : mpexp(gmul(B, logp));
374 91306 : if (p == 2) logp = NULL; /* reset: precp must be odd */
375 : }
376 : else
377 1456151 : u = gpow(gp, B, prec0);
378 1547457 : precp = p;
379 1547457 : gel(v,p) = u; /* p^B */
380 1547457 : if (prec0 != prec) gel(v,p) = gprec_wtrunc(gel(v,p), prec);
381 3207825 : for (pk = p, oldpk = p; pk; oldpk = pk, pk = umuluu_le(pk,p,N))
382 : {
383 1660368 : if (pk != p) gel(v,pk) = gmul(gel(v,oldpk), gel(v,p));
384 46218432 : for (m = N/pk; m > 1; m--)
385 44558064 : if (gel(v,m) && m%p) gel(v, m*pk) = gmul(gel(v,m), gel(v,pk));
386 : }
387 : }
388 11779 : 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 : static GEN
404 252 : vecmulsqlv(GEN Q, GEN V)
405 : {
406 : long lq, i;
407 : GEN W;
408 252 : if (typ(V) != t_VEC) return RgV_Rg_mul(Q, V);
409 21 : lq = lg(Q); W = cgetg(lq, t_VEC);
410 672 : for (i = 1; i < lq; i++) gel(W, i) = vecmul(gel(Q, i), V);
411 21 : return W;
412 : }
413 :
414 : /* P = prime divisors of (squarefree) n, V[i] = i^s for i <= sq.
415 : * Return NULL if n is not sq-smooth, else f(n)n^s */
416 : static GEN
417 245126 : smallfact(ulong n, GEN P, ulong sq, GEN V)
418 : {
419 : long i, l;
420 : ulong p, m, o;
421 : GEN c;
422 245126 : if (n <= sq) return gel(V,n);
423 243845 : l = lg(P); m = p = uel(P, l-1); if (p > sq) return NULL;
424 48258 : for (i = l-2; i > 1; i--, m = o) { p = uel(P,i); o = m*p; if (o > sq) break; }
425 47950 : c = gel(V,m); n /= m; /* m <= sq, o = m * p > sq */
426 47950 : if (n > sq) { c = vecmul(c, gel(V,p)); n /= p; }
427 47950 : return vecmul(c, gel(V,n));
428 : }
429 :
430 : static GEN
431 357 : Qtor(GEN x, long prec)
432 : {
433 357 : long tx = typ(x);
434 357 : if (tx == t_VEC || tx == t_COL)
435 : {
436 21 : long lx = lg(x), i;
437 21 : GEN V = cgetg(lx, tx);
438 63 : for (i = 1; i < lx; i++) gel(V, i) = Qtor(gel(x, i), prec);
439 21 : return V;
440 : }
441 336 : return tx == t_FRAC? fractor(x, prec): x;
442 : }
443 :
444 : /* Here N > 0 is small */
445 : static GEN
446 147 : naivedirpowerssum(long N, GEN s, void *E, GEN (*f)(void *, ulong, long),
447 : long prec)
448 : {
449 147 : GEN V = vecpowug(N, s, prec), S;
450 147 : if (!f) S = RgV_sum(V);
451 : else
452 : {
453 : long n;
454 35 : S = f(E, 1, prec);
455 308 : for (n = 2; n <= N; n++) S = gadd(S, gmul(gel(V, n), f(E, n, prec)));
456 : }
457 147 : return Qtor(S, prec);
458 : }
459 :
460 : static GEN
461 126 : smalldirpowerssum(long N, GEN s, void *E, GEN (*f)(void *, ulong, long),
462 : long both, long prec)
463 : {
464 126 : GEN S = naivedirpowerssum(N, s, E, f, prec), SB, sb;
465 126 : if (!both) return S;
466 42 : sb = gconj(gsubsg(-1, s));
467 42 : SB = both==2 && gequal(s,sb)? S: gconj(naivedirpowerssum(N,sb,E,f,prec));
468 42 : return mkvec2(S, SB);
469 : }
470 :
471 : static GEN
472 63 : dirpowsuminit(GEN s, void *E, GEN (*f)(void *, ulong, long), GEN data,
473 : long both, long prec)
474 : {
475 63 : GEN onef = gel(data, 1), zervec = gel(data, 2), sqlpp = gel(data, 3);
476 63 : long sq = sqlpp[1], needlog = sqlpp[2], prec0 = sqlpp[3], prec1 = sqlpp[4];
477 63 : GEN V = cgetg(sq+1, t_VEC), W = cgetg(sq+1, t_VEC), Q = cgetg(sq+1, t_VEC);
478 63 : GEN VB = NULL, WB = NULL, QB = NULL, c2, c2B = NULL;
479 63 : GEN Q2, Q3, Q6, Q2B = NULL, Q3B = NULL, Q6B = NULL;
480 63 : GEN logp, R, RB = NULL;
481 63 : long gp[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
482 : long n;
483 63 : if (both == 1 || (both == 2 && !gequal(real_i(s), gneg(ghalf))))
484 21 : { VB = cgetg(sq+1, t_VEC); WB = cgetg(sq+1, t_VEC); QB = cgetg(sq+1, t_VEC);}
485 63 : gel(V, 1) = gel(W, 1) = gel(Q, 1) = onef;
486 63 : if (VB) { gel(VB, 1) = gel(WB, 1) = gel(QB, 1) = onef; }
487 63 : c2 = gpow(gen_2, s, prec0); if (VB) c2B = ginv(gmul2n(gconj(c2), 1));
488 63 : if (f)
489 : {
490 42 : GEN tmp2 = f(E, 2, prec);
491 42 : c2 = gmul(c2, tmp2); if (VB) c2B = gmul(c2B, tmp2);
492 : }
493 63 : gel(V,2) = c2; /* f(2) 2^s */
494 63 : gel(W,2) = Qtor(gadd(c2, onef), prec0);
495 63 : gel(Q,2) = Qtor(gadd(vecsqr(c2), onef), prec0);
496 63 : if (VB)
497 : {
498 21 : gel(VB, 2) = c2B; gel(WB, 2) = Qtor(gadd(c2B, onef), prec0);
499 21 : gel(QB, 2) = Qtor(gadd(vecsqr(c2B), onef), prec0);
500 : }
501 63 : logp = NULL;
502 4074 : for (n = 3; n <= sq; n++)
503 : {
504 4011 : GEN u = NULL, uB = NULL, ks = f ? f(E, n, prec0) : gen_1;
505 4011 : long zks = !gequal0(ks);
506 4011 : if (odd(n))
507 : {
508 2023 : gp[2] = n;
509 2023 : if (needlog)
510 : {
511 476 : if (!logp)
512 42 : logp = logr_abs(utor(n, prec1));
513 : else
514 : { /* log n = log(n-2) + 2 atanh(1 / (n - 1)) */
515 434 : GEN z = atanhuu(1, n - 1, prec1);
516 434 : shiftr_inplace(z, 1); logp = addrr(logp, z);
517 : }
518 476 : if (zks)
519 476 : u = needlog == 1? powcx(gp, logp, s, prec0) : mpexp(gmul(s, logp));
520 : }
521 1547 : else if (zks) u = gpow(gp, s, prec0);
522 2023 : if (zks)
523 : {
524 2009 : if (VB) uB = gmul(ginv(gmulsg(n, gconj(u))), ks);
525 2009 : u = gmul(u, ks); /* f(n) n^s */
526 : }
527 : }
528 : else
529 : {
530 1988 : u = vecmul(c2, gel(V, n >> 1));
531 1988 : if (VB) uB = vecmul(c2B, gel(VB, n >> 1));
532 : }
533 4011 : if (zks)
534 : { /* V[n]=f(n)n^s, W[n]=sum_{i<=n} f(i)i^s, Q[n]=sum_{i<=n} f(i^2)i^2s */
535 3983 : gel(V,n) = u;
536 3983 : gel(W,n) = gadd(gel(W, n-1), gel(V,n));
537 3983 : gel(Q,n) = gadd(gel(Q, n-1), vecsqr(gel(V,n)));
538 3983 : if (VB)
539 : {
540 462 : gel(VB,n) = uB;
541 462 : gel(WB,n) = gadd(gel(WB,n-1), gel(VB,n));
542 462 : gel(QB,n) = gadd(gel(QB,n-1), vecsqr(gel(VB,n)));
543 : }
544 : }
545 : else
546 : {
547 28 : gel(V,n) = zervec; gel(W,n) = gel(W, n-1); gel(Q,n) = gel(Q, n-1);
548 28 : if (VB)
549 : {
550 0 : gel(VB,n) = zervec; gel(WB,n) = gel(WB, n-1);
551 0 : gel(QB,n) = gel(QB, n-1);
552 : }
553 : }
554 : }
555 63 : Q2 = vecmulsqlv(Q, gel(V,2));
556 63 : Q3 = vecmulsqlv(Q, gel(V,3));
557 63 : Q6 = vecmulsqlv(Q, gel(V,6));
558 63 : if (VB)
559 : {
560 21 : Q2B = vecmulsqlv(QB, gel(VB,2));
561 21 : Q3B = vecmulsqlv(QB, gel(VB,3));
562 21 : Q6B = vecmulsqlv(QB, gel(VB,6));
563 : }
564 63 : R = mkvecn(6, V, W, Q, Q2, Q3, Q6);
565 63 : if (VB) RB = mkvecn(6, VB, WB, QB, Q2B, Q3B, Q6B);
566 63 : return VB ? mkvec2(R, RB) : mkvec(R);
567 : }
568 :
569 : static GEN
570 63 : dirpowsumprimeloop(ulong N, GEN s, void *E, GEN (*f)(void *, ulong, long),
571 : GEN data, GEN W, GEN WB)
572 : {
573 : pari_sp av2;
574 63 : GEN zervec = gel(data, 2), S = zervec, SB = zervec, logp = NULL;
575 63 : GEN sqlpp = gel(data, 3);
576 : forprime_t T;
577 63 : long gp[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
578 63 : long p, precp = 0, sq = sqlpp[1], needlog = sqlpp[2];
579 63 : long prec0 = sqlpp[3], prec1 = sqlpp[4];
580 63 : u_forprime_init(&T, sq + 1, N);
581 63 : av2 = avma;
582 80969 : while ((p = u_forprime_next(&T)))
583 : {
584 80906 : GEN u = NULL, ks = f ? f(E, p, prec1) : gen_1;
585 80906 : long zks = !gequal0(ks);
586 80906 : gp[2] = p;
587 80906 : if (needlog)
588 : {
589 4690 : if (!logp)
590 42 : logp = logr_abs(utor(p, prec1));
591 : else
592 : { /* log p = log(precp) + 2 atanh((p - precp) / (p + precp)) */
593 4648 : ulong a = p >> 1, b = precp >> 1; /* p = 2a + 1, precp = 2b + 1 */
594 4648 : GEN z = atanhuu(a - b, a + b + 1, prec1); /* avoid overflow */
595 4648 : shiftr_inplace(z, 1); logp = addrr(logp, z);
596 : }
597 4690 : if (zks)
598 4690 : u = needlog == 1? powcx(gp, logp, s, prec0) : mpexp(gmul(s, logp));
599 : }
600 76216 : else { if (zks) u = gpow(gp, s, prec0); }
601 80906 : if (zks)
602 : {
603 80906 : S = gadd(S, vecmul(gel(W, N / p), gmul(ks, u)));
604 80906 : if (WB)
605 2345 : SB = gadd(SB, gdiv(vecmul(ks, gel(WB, N / p)), gmulsg(p, gconj(u))));
606 : }
607 80906 : precp = p;
608 80906 : if ((p & 0x1ff) == 1)
609 : {
610 280 : if (!logp) gerepileall(av2, SB? 2: 1, &S, &SB);
611 0 : else gerepileall(av2, SB? 3: 2, &S, &logp, &SB);
612 : }
613 : }
614 63 : return SB ? mkvec2(S, SB) : mkvec(S);
615 : }
616 :
617 : static GEN
618 413 : dirpowsumsqfloop(long N, long x1, long x2, long sq, GEN P, GEN allvecs, GEN S,
619 : GEN allvecsB, GEN SB)
620 : {
621 413 : GEN V = gel(allvecs, 1), Q = gel(allvecs, 2), Q2 = gel(allvecs, 3);
622 413 : GEN Q3 = gel(allvecs, 4), Q6 = gel(allvecs, 5), Z = gel(allvecs, 6);
623 413 : GEN VB = NULL, QB = NULL, Q2B = NULL, Q3B = NULL, Q6B = NULL, ZB = NULL;
624 413 : GEN v = vecfactorsquarefreeu_coprime(x1, x2, P);
625 413 : long lv = lg(v), j;
626 413 : if (allvecsB)
627 : {
628 21 : VB = gel(allvecsB, 1), QB = gel(allvecsB, 2), Q2B = gel(allvecsB, 3);
629 21 : Q3B = gel(allvecsB, 4), Q6B = gel(allvecsB, 5), ZB = gel(allvecsB, 6);
630 : }
631 806813 : for (j = 1; j < lv; j++)
632 806400 : if (gel(v,j))
633 : {
634 245126 : ulong d = x1 - 1 + j; /* squarefree, coprime to 6 */
635 245126 : GEN t = smallfact(d, gel(v,j), sq, V), u;
636 245126 : GEN tB = NULL, uB = NULL; /* = d^s */
637 : long a, b, c, e, q;
638 245126 : if (!t || gequal0(t)) continue;
639 48748 : if (VB) tB = vecinv(gmulsg(d, gconj(t)));
640 : /* warning: gives 1/conj(f(d)) d^(-1-conj(s)), equal to
641 : f(d) d^(-1-conj(s)) only if |f(d)|=1. */
642 : /* S += f(d) * d^s * Z[q] */
643 48748 : q = N / d;
644 48748 : if (q == 1)
645 : {
646 17339 : S = gadd(S, t); if (VB) SB = gadd(SB, tB);
647 17339 : continue;
648 : }
649 31409 : if (q <= sq) { u = gel(Z, q); if (VB) uB = gel(ZB, q); }
650 : else
651 : {
652 1274 : a = usqrt(q); b = usqrt(q / 2); c = usqrt(q / 3); e = usqrt(q / 6);
653 1274 : u = gadd(gadd(gel(Q,a), gel(Q2,b)), gadd(gel(Q3,c), gel(Q6,e)));
654 1274 : if (VB)
655 161 : uB = gadd(gadd(gel(QB,a), gel(Q2B,b)), gadd(gel(Q3B,c), gel(Q6B,e)));
656 : }
657 31409 : S = gadd(S, vecmul(t, u)); if (VB) SB = gadd(SB, vecmul(tB, uB));
658 : }
659 413 : return VB ? mkvec2(S, SB) : mkvec(S);
660 : }
661 :
662 : static GEN
663 63 : dirpowsummakez(GEN V, GEN W, GEN VB, GEN WB, GEN onef, ulong sq)
664 : {
665 63 : GEN Z = cgetg(sq+1, t_VEC), ZB = NULL;
666 : ulong a, b, c, e, q;
667 : /* a,b,c,e = sqrt(q), sqrt(q/2), sqrt(q/3), sqrt(q/6)
668 : * Z[q] = Q[a] + 2^s Q[b] + 3^s Q[c] + 6^s Q[e], with Q[0] = 0 */
669 63 : gel(Z, 1) = onef;
670 63 : gel(Z, 2) = gel(W, 2);
671 63 : gel(Z, 3) = gel(W, 3);
672 63 : gel(Z, 4) = gel(Z, 5) = gel(W, 4);
673 63 : gel(Z, 6) = gel(Z, 7) = gadd(gel(W, 4), gel(V, 6));
674 63 : if (VB)
675 : {
676 21 : ZB = cgetg(sq+1, t_VEC);
677 21 : gel(ZB, 1) = onef;
678 21 : gel(ZB, 2) = gel(WB, 2);
679 21 : gel(ZB, 3) = gel(WB, 3);
680 21 : gel(ZB, 4) = gel(ZB, 5) = gel(WB, 4);
681 21 : gel(ZB, 6) = gel(ZB, 7) = gadd(gel(WB, 4), gel(VB, 6));
682 : }
683 63 : a = 2; b = c = e = 1;
684 3759 : for (q = 8; q <= sq; q++)
685 : { /* Gray code: at most one of a,b,c,d differs (by 1) from previous value */
686 3696 : GEN z = gel(Z, q - 1), zB = NULL;
687 : ulong na, nb, nc, ne, na2, nb2, nc2, ne2;
688 3696 : if (VB) zB = gel(ZB, q - 1);
689 3696 : if ((na = usqrt(q)) != a)
690 280 : { a = na; na2 = na * na; z = gadd(z, gel(V, na2));
691 280 : if (VB) zB = gadd(zB, gel(VB, na2)); }
692 3416 : else if ((nb = usqrt(q / 2)) != b)
693 203 : { b = nb; nb2 = 2 * nb * nb; z = gadd(z, gel(V, nb2));
694 203 : if (VB) zB = gadd(zB, gel(VB, nb2)); }
695 3213 : else if ((nc = usqrt(q / 3)) != c)
696 161 : { c = nc; nc2 = 3 * nc * nc; z = gadd(z, gel(V, nc2));
697 161 : if (VB) zB = gadd(zB, gel(VB, nc2)); }
698 3052 : else if ((ne = usqrt(q / 6)) != e)
699 98 : { e = ne; ne2 = 6 * ne * ne; z = gadd(z, gel(V, ne2));
700 98 : if (VB) zB = gadd(zB, gel(VB, ne2)); }
701 3696 : gel(Z, q) = z; if (VB) gel(ZB, q) = zB;
702 : }
703 63 : return VB ? mkvec2(Z, ZB) : mkvec(Z);
704 : }
705 :
706 : /* both =
707 : * 0: sum_{n<=N}f(n)n^s
708 : * 1: sum for (f,s) and (conj(f),-1-s)
709 : * 2: sum for (f,s) and (f,-1-s), assuming |f(n)| in {0,1} */
710 : GEN
711 203 : dirpowerssumfun(ulong N, GEN s, void *E, GEN (*f)(void *, ulong, long),
712 : long both, long prec)
713 : {
714 203 : const ulong step = 2048;
715 203 : pari_sp av = avma, av2;
716 : GEN P, V, W, Q, Q2, Q3, Q6, S, Z, onef, zervec;
717 203 : GEN VB = NULL, WB = NULL, QB = NULL;
718 203 : GEN Q2B = NULL, Q3B = NULL, Q6B = NULL, SB = NULL, ZB = NULL;
719 203 : GEN R, RS, data, allvecs, allvecsB = NULL;
720 : ulong x1, sq;
721 : long prec0, prec1, needlog;
722 :
723 203 : if (!N)
724 : {
725 14 : if (!f) return gen_0;
726 0 : return gerepileupto(av, gmul(gen_0, f(E, 1, prec)));
727 : }
728 189 : onef = f ? f(E, 1, prec) : gen_1;
729 189 : zervec = gmul(gen_0, onef);
730 189 : if ((f && N < 49) || (!f && N < 1000))
731 126 : return gerepilecopy(av, smalldirpowerssum(N, s, E, f, both, prec));
732 63 : sq = usqrt(N);
733 63 : prec1 = prec0 = prec + EXTRAPREC64;
734 63 : s = gprec_w(s, prec0);
735 63 : needlog = get_needlog(s);
736 63 : if (needlog == 1) prec1 = powcx_prec(log2((double)N), s, prec);
737 63 : data = mkvec3(onef, zervec, mkvecsmall4(sq, needlog, prec0, prec1));
738 63 : RS = dirpowsuminit(s, E, f, data, both, prec);
739 63 : R = gel(RS, 1); V = gel(R, 1); W = gel(R, 2); Q = gel(R, 3);
740 63 : Q2 = gel(R, 4); Q3 = gel(R, 5); Q6 = gel(R, 6);
741 63 : if (lg(RS) > 2)
742 : {
743 21 : GEN RB = gel(RS, 2);
744 21 : VB = gel(RB, 1); WB = gel(RB, 2); QB = gel(RB, 3);
745 21 : Q2B = gel(RB, 4); Q3B = gel(RB, 5); Q6B = gel(RB, 6);
746 : }
747 63 : RS = dirpowsumprimeloop(N, s, E, f, data, W, WB);
748 63 : S = gel(RS, 1); if (VB) SB = gel(RS, 2);
749 63 : RS = dirpowsummakez(V, W, VB, WB, onef, sq);
750 63 : Z = gel(RS, 1); if (VB) ZB = gel(RS, 2);
751 63 : P = mkvecsmall2(2, 3);
752 63 : allvecs = mkvecn(6, V, Q, Q2, Q3, Q6, Z);
753 63 : if (VB) allvecsB = mkvecn(6, VB, QB, Q2B, Q3B, Q6B, ZB);
754 63 : av2 = avma;
755 63 : for(x1 = 1;; x1 += step)
756 350 : { /* beware overflow, fuse last two bins (avoid a tiny remainder) */
757 413 : ulong x2 = (N >= 2*step && N - 2*step >= x1)? x1-1 + step: N;
758 413 : RS = dirpowsumsqfloop(N, x1, x2, sq, P, allvecs, S, allvecsB, SB);
759 413 : S = gel(RS, 1); if (VB) SB = gel(RS, 2);
760 413 : if (x2 == N) break;
761 350 : gerepileall(av2, SB? 2: 1, &S, &SB);
762 : }
763 63 : if (both == 0) return gerepileupto(av, S);
764 42 : return gerepilecopy(av, mkvec2(S, gconj(VB? SB: S)));
765 : }
766 :
767 : GEN
768 133 : dirpowerssum(ulong N, GEN s, long both, long prec)
769 133 : { return dirpowerssumfun(N, s, NULL, NULL, both, prec); }
770 :
771 : static GEN
772 13846 : gp_callUp(void *E, ulong x, long prec)
773 : {
774 13846 : long court[] = {evaltyp(t_INT)|_evallg(3), evalsigne(1)|evallgefint(3),0};
775 13846 : court[2] = x; return gp_callprec(E, court, prec);
776 : }
777 :
778 : GEN
779 154 : dirpowerssum0(GEN N, GEN s, GEN f, long both, long prec)
780 : {
781 154 : if (typ(N) != t_INT) pari_err_TYPE("dirpowerssum", N);
782 147 : if (signe(N) <= 0) N = gen_0;
783 147 : if (!f) return dirpowerssum(itou(N), s, both, prec);
784 70 : if (typ(f) != t_CLOSURE) pari_err_TYPE("dirpowerssum", f);
785 70 : return dirpowerssumfun(itou(N), s, (void*)f, gp_callUp, both, prec);
786 : }
|