-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: add complex polygamma and Hurwitz zeta functions #7125
Conversation
This version of import Base.Math.@horner
function newdg(z::Union(Float64,Complex{Float64}))
# Based on eq. (12), without looking at the accompanying source
# code, of: K. S. Kölbig, "Programs for computing the logarithm of
# the gamma function, and the digamma function, for complex
# argument," Computer Phys. Commun. vol. 4, pp. 221–226 (1972).
x = real(z)
if x <= 0 # reflection formula
ψ = -π * cot(π*z)
z = 1 - z
x = real(z)
else
ψ = zero(z)
end
if x < 7
# shift using recurrence formula
n = 7 - ifloor(x)
for ν = 1:n-1
ψ -= inv(z + ν)
end
ψ -= inv(z)
z += n
end
t = inv(z)
ψ += log(z) - 0.5*t
t *= t # 1/z^2
# the coefficients here are float64(bernoulli[2:9] .// (2*(1:8)))
ψ -= t * @horner(t,0.08333333333333333,-0.008333333333333333,0.003968253968253968,-0.004166666666666667,0.007575757575757576,-0.021092796092796094,0.08333333333333333,-0.4432598039215686)
end Is there any way to fix |
This is incredibly good work – I should expect no less, @stevengj, but still, you've outdone yourself. Are you comfortable merging this before addressing the constant propagation issue, or do you want to wait? |
It can be merged before that is fixed, since it only affects performance and not correctness. Will cause a performance regression in |
I've found a workaround for the type-inference problem (the problem was that Julia was performing type inference before dead-code elimination, so it thought the function wasn't type-stable), and will update the patch shortly. |
Compared to the implementations in SciPy (for large arrays so that I am just benchmarking their underlying C/Fortran code):
Also, it looks like their |
Compared to Matlab (with However, Matlab's |
cc @andreasnoackjensen |
Looks great. |
Phenomenal. |
Please merge whenever you like. |
RFC: add complex polygamma and Hurwitz zeta functions
Eh, let's leave it as is then. It can always be deprecated in the future. |
This replaces the old
polygamma(m,z)
implementation for #7033. The new one seems to bearound the same speedaround 10-20x faster for real argumentsz
, but the same code also works for complexz
, and it also handles negativez
. I also added specializeddigamma
andtrigamma
implementations supporting real and complex arguments. The newtrigamma
is much faster,almost 10xmore than 50x faster, than the old generic one based onpolygamma
. The newdigamma
seems to be about the same speed as the olddigamma
for real arguments, but is simpler and supports both real and complexz
.However, I still seem to be hitting #7060. My
@chorner
macro is supposed to fall back to@horner
automatically for real arguments, but manually replacing@chorner
with@horner
for real arguments makes thedigamma
function 5x faster. cc: @carnavalAs a bonus, the way I implemented polygamma gives us the Hurwitz zeta function
zeta(s,z)
= ζ(s, z) for free, at least forreal(s) ≥ 1
. This is a common generalization of the Riemann zeta functionzeta(s)
= ζ(s) = ζ(s, 1), which we already export.(In total, I deleted almost as much code as I added.)