@@ -224,6 +224,9 @@ function inv_oftype(x::Complex, y::Real)
224
224
end
225
225
inv_oftype (x:: Real , y:: Real ) = oftype (x, inv (y))
226
226
227
+ zeta_returntype {T} (s:: Integer , z:: T ) = T
228
+ zeta_returntype (s, z) = Complex128
229
+
227
230
# Hurwitz zeta function, which is related to polygamma
228
231
# (at least for integer m > 0 and real(z) > 0) by:
229
232
# polygamma(m, z) = (-1)^(m+1) * gamma(m+1) * zeta(m+1, z).
@@ -237,21 +240,21 @@ inv_oftype(x::Real, y::Real) = oftype(x, inv(y))
237
240
# (which Julia already exports).
238
241
function zeta (s:: Union(Int,Float64,Complex{Float64}) ,
239
242
z:: Union(Float64,Complex{Float64}) )
240
- ζ = isa ( s, Int) ? zero (z) : complex ( zero ( z))
243
+ ζ = zero ( zeta_returntype ( s, z))
241
244
z == 1 && return oftype (ζ, zeta (s))
242
245
s == 2 && return oftype (ζ, trigamma (z))
243
246
244
247
x = real (z)
245
248
246
249
# annoying s = Inf or NaN cases:
247
250
if ! isfinite (s)
248
- (isnan (s) || isnan (z)) && return Complex ( NaN , NaN )
251
+ (isnan (s) || isnan (z)) && return (s * z) ^ 2 # type-stable NaN+Nan*im
249
252
if real (s) == Inf
250
253
z == 1 && return one (ζ)
251
254
if x > 1 || (x >= 0.5 ? abs (z) > 1 : abs (z - round (x)) > 1 )
252
255
return zero (ζ) # distance to poles is > 1
253
256
end
254
- x > 0 && imag (z) == 0 && imag (s) == 0 && return Complex ( Inf , 0.0 )
257
+ x > 0 && imag (z) == 0 && imag (s) == 0 && return oftype (ζ, Inf )
255
258
end
256
259
throw (DomainError ()) # nothing clever to return
257
260
end
0 commit comments