@@ -272,7 +272,7 @@ function zeta(s::Union(Int,Float64,Complex{Float64}),
272
272
273
273
isnan (x) && return oftype (ζ, imag (z)== 0 && isa (s,Int) ? x : Complex (x,x))
274
274
275
- cutoff = 7 + real (m) # empirical cutoff for asymptotic series
275
+ cutoff = 7 + real (m) + imag (m) # TODO : this cutoff is too conservative?
276
276
if x < cutoff
277
277
# shift using recurrence formula
278
278
xf = floor (x)
@@ -407,112 +407,73 @@ lbeta(x::Number, w::Number) = lgamma(x)+lgamma(w)-lgamma(x+w)
407
407
@vectorize_2arg Number beta
408
408
@vectorize_2arg Number lbeta
409
409
410
- const eta_coeffs =
411
- [.99999999999999999997 ,
412
- - .99999999999999999821 ,
413
- .99999999999999994183 ,
414
- - .99999999999999875788 ,
415
- .99999999999998040668 ,
416
- - .99999999999975652196 ,
417
- .99999999999751767484 ,
418
- - .99999999997864739190 ,
419
- .99999999984183784058 ,
420
- - .99999999897537734890 ,
421
- .99999999412319859549 ,
422
- - .99999996986230482845 ,
423
- .99999986068828287678 ,
424
- - .99999941559419338151 ,
425
- .99999776238757525623 ,
426
- - .99999214148507363026 ,
427
- .99997457616475604912 ,
428
- - .99992394671207596228 ,
429
- .99978893483826239739 ,
430
- - .99945495809777621055 ,
431
- .99868681159465798081 ,
432
- - .99704078337369034566 ,
433
- .99374872693175507536 ,
434
- - .98759401271422391785 ,
435
- .97682326283354439220 ,
436
- - .95915923302922997013 ,
437
- .93198380256105393618 ,
438
- - .89273040299591077603 ,
439
- .83945793215750220154 ,
440
- - .77148960729470505477 ,
441
- .68992761745934847866 ,
442
- - .59784149990330073143 ,
443
- .50000000000000000000 ,
444
- - .40215850009669926857 ,
445
- .31007238254065152134 ,
446
- - .22851039270529494523 ,
447
- .16054206784249779846 ,
448
- - .10726959700408922397 ,
449
- .68016197438946063823e-1 ,
450
- - .40840766970770029873e-1 ,
451
- .23176737166455607805e-1 ,
452
- - .12405987285776082154e-1 ,
453
- .62512730682449246388e-2 ,
454
- - .29592166263096543401e-2 ,
455
- .13131884053420191908e-2 ,
456
- - .54504190222378945440e-3 ,
457
- .21106516173760261250e-3 ,
458
- - .76053287924037718971e-4 ,
459
- .25423835243950883896e-4 ,
460
- - .78585149263697370338e-5 ,
461
- .22376124247437700378e-5 ,
462
- - .58440580661848562719e-6 ,
463
- .13931171712321674741e-6 ,
464
- - .30137695171547022183e-7 ,
465
- .58768014045093054654e-8 ,
466
- - .10246226511017621219e-8 ,
467
- .15816215942184366772e-9 ,
468
- - .21352608103961806529e-10 ,
469
- .24823251635643084345e-11 ,
470
- - .24347803504257137241e-12 ,
471
- .19593322190397666205e-13 ,
472
- - .12421162189080181548e-14 ,
473
- .58167446553847312884e-16 ,
474
- - .17889335846010823161e-17 ,
475
- .27105054312137610850e-19 ]
476
-
477
- function eta (z:: Union(Float64,Complex128) )
478
- if z == 0
479
- return oftype (z, 0.5 )
480
- end
481
- re, im = reim (z)
482
- if im== 0 && re < 0 && re== round (re/ 2 )* 2
483
- return zero (z)
484
- end
485
- reflect = false
486
- if re < 0.5
487
- z = 1 - z
488
- reflect = true
489
- end
490
- s = zero (z)
491
- for n = length (eta_coeffs): - 1 : 1
492
- c = eta_coeffs[n]
493
- p = n^- z
494
- s += c * p
410
+ # Riemann zeta function; algorithm is based on specializing the Hurwitz
411
+ # zeta function above for z==1.
412
+ function zeta (s:: Union(Float64,Complex{Float64}) )
413
+ # blows up to ±Inf, but get correct sign of imaginary zero
414
+ s == 1 && return NaN + zero (s) * imag (s)
415
+
416
+ if ! isfinite (s) # annoying NaN and Inf cases
417
+ isnan (s) && return imag (s) == 0 ? s : s* s
418
+ if isfinite (imag (s))
419
+ real (s) > 0 && return 1.0 - zero (s)* imag (s)
420
+ imag (s) == 0 && return NaN + zero (s)
421
+ end
422
+ return NaN * zero (s) # NaN + NaN*im
423
+ elseif real (s) < 0.5
424
+ if abs (real (s)) + abs (imag (s)) < 1e-3 # Taylor series for small |s|
425
+ return @evalpoly (s, - 0.5 ,
426
+ - 0.918938533204672741780329736405617639861 ,
427
+ - 1.0031782279542924256050500133649802190 ,
428
+ - 1.00078519447704240796017680222772921424 ,
429
+ - 0.9998792995005711649578008136558752359121 )
430
+ end
431
+ return zeta (1 - s) * gamma (1 - s) * sinpi (s* 0.5 ) * (2 π)^ s / π
495
432
end
496
- if reflect
497
- z2 = 2.0 ^ z
498
- b = 2.0 - (2.0 * z2)
499
- f = z2 - 2
500
- piz = pi ^ z
501
-
502
- b = b/ f/ piz
503
-
504
- return s * gamma (z) * b * cospi (z/ 2 )
433
+
434
+ m = s - 1
435
+
436
+ # shift using recurrence formula:
437
+ # n is a semi-empirical cutoff for the Stirling series, based
438
+ # on the error term ~ (|m|/n)^18 / n^real(m)
439
+ n = iceil (6 + 0.7 * abs (imag (s- 1 ))^ inv (1 + real (m)* 0.05 ))
440
+ ζ = one (s)
441
+ for ν = 2 : n
442
+ ζₒ= ζ
443
+ ζ += inv (ν)^ s
444
+ ζ == ζₒ && break # prevent long loop for large m
505
445
end
506
- return s
446
+ z = 1 + n
447
+ t = inv (z)
448
+ w = t^ m
449
+ ζ += w * (inv (m) + 0.5 * t)
450
+
451
+ t *= t # 1/z^2
452
+ ζ += w* t * @pg_horner (t,m,0.08333333333333333 ,- 0.008333333333333333 ,0.003968253968253968 ,- 0.004166666666666667 ,0.007575757575757576 ,- 0.021092796092796094 ,0.08333333333333333 ,- 0.4432598039215686 ,3.0539543302701198 )
453
+
454
+ return ζ
507
455
end
508
456
457
+ zeta (x:: Integer ) = zeta (float64 (x))
458
+ zeta (x:: Real ) = oftype (float (x),zeta (float64 (x)))
459
+ zeta (z:: Complex ) = oftype (float (z),zeta (complex128 (z)))
460
+ @vectorize_1arg Number zeta
461
+
462
+ function eta (z:: Union(Float64,Complex{Float64}) )
463
+ δz = 1 - z
464
+ if abs (real (δz)) + abs (imag (δz)) < 7e-3 # Taylor expand around z==1
465
+ return 0.6931471805599453094172321214581765 *
466
+ @evalpoly (δz,
467
+ 1.0 ,
468
+ - 0.23064207462156020589789602935331414700440 ,
469
+ - 0.047156357547388879740146103148112380421254 ,
470
+ - 0.002263576552598880778433550956278702759143568 ,
471
+ 0.001081837223249910136105931217561387128141157 )
472
+ else
473
+ return - zeta (z) * expm1 (0.6931471805599453094172321214581765 * δz)
474
+ end
475
+ end
509
476
eta (x:: Integer ) = eta (float64 (x))
510
477
eta (x:: Real ) = oftype (float (x),eta (float64 (x)))
511
478
eta (z:: Complex ) = oftype (float (z),eta (complex128 (z)))
512
479
@vectorize_1arg Number eta
513
-
514
- function zeta (z:: Number )
515
- zz = 2 ^ z
516
- eta (z) * zz/ (zz- 2 )
517
- end
518
- @vectorize_1arg Number zeta
0 commit comments