Skip to content

Make pochhammer more robust #72

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

Merged
merged 18 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/specialfunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pochhammer(x::AbstractArray{T,1},n::Integer) where {T<:Number} = [pochhammer(x[i
pochhammer(x::AbstractArray{T,2},n::Integer) where {T<:Number} = [pochhammer(x[i,j],n) for i=1:size(x,1),j=1:size(x,2)]
pochhammer(x::AbstractArray{T},n::Integer) where {T<:Number} = reshape([ pochhammer(x[i],n) for i in eachindex(x) ], size(x))

pochhammer(x::Number,n::Number) = gamma(x+n)/gamma(x)
pochhammer(x::AbstractArray{T},n::Number) where {T<:Number} = gamma(x+n)./gamma(x)
pochhammer(x::Number,n::Number) = isinteger(n) ? pochhammer(x,Int(n)) : ogamma(x)/ogamma(x+n)
pochhammer(x::AbstractArray{T},n::Number) where {T<:Number} = isinteger(n) ? pochhammer(x,Int(n)) : ogamma.(x)./ogamma.(x.+n)

function pochhammer(x::Number,n::UnitRange{T}) where T<:Real
ret = Vector{promote_type(typeof(x),T)}(length(n))
Expand All @@ -61,6 +61,14 @@ function pochhammer(x::Number,n::UnitRange{T}) where T<:Real
ret
end

function ogamma(x::Number)
if isinteger(x) && x<0
zero(float(x))
else
inv(gamma(x))
end
end

"""
Stirling's asymptotic series for ``\\Gamma(z)``.
"""
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ include("butterflytests.jl")
include("sphericalharmonics/sphericalharmonictests.jl")

include("toeplitztests.jl")

include("specialfunctionstests.jl")
19 changes: 19 additions & 0 deletions test/specialfunctionstests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using FastTransforms
using Compat.Test
import FastTransforms.pochhammer

const sqrtpi = 1.772453850905516027298

@testset "Pochhammer" begin
@test pochhammer(2,3) == 24
@test pochhammer(0.5,3) == 0.5*1.5*2.5
@test pochhammer(0.5,0.5) == 1/sqrtpi
@test pochhammer(0,1) == 0
@test pochhammer(-1,2) == 0
@test pochhammer(-5,3) == -60
@test pochhammer(-1,-0.5) == 0
@test 1.0/pochhammer(-0.5,-0.5) == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add complex tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gamma is nonzero and holomorphic except the singularities, so complex tests don't make any difference, I suppose. I skipped array tests, though. Maybe add some array tests

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean complex types but at integers: ogamma(-1+0im)

@test pochhammer(-1+0im,-1) == -2
@test pochhammer([2,-5,-1],3) == [24,-60,0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vector-valued arguments should be deprecated in favour of pochhammer.([2,-5,-1],3), so perhaps remove this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So can pochhammer(x::AbstractArray{T},n::Integer) be removed? Do abstractarrays work with dot operations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be deprecated, though that can be done separately. Yes abstract arrays support broadcastibg

@test pochhammer([-1,-0.5],-0.5) == [0,-Inf]
end