`NaN` in `digamma` caused by `_cotpi`

Open
#480 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
julia
Domain
backend

Research direction

Start in src/gamma.jl at the _digamma definition and _cotpi, then compare the proposed behavior with the linked mpmath implementation. Reproduce the large complex-argument example in Julia and verify that the resulting cotangent and digamma calculation no longer produce NaN while retaining the expected value for cot(pi * 500 * exp(im * 3pi/4)).

Written by the indexing model from the issue text.

Description

The definition of _digamma(z::ComplexOrReal{Float64}) contains the formula
https://github.com/JuliaMath/SpecialFunctions.jl/blob/cf35c580aebd3a698f1d3550e2b60ffbd73bdc0c/src/gamma.jl#L29
in which _cotpi is defined as
https://github.com/JuliaMath/SpecialFunctions.jl/blob/cf35c580aebd3a698f1d3550e2b60ffbd73bdc0c/src/gamma.jl#L63
For large complex arguments, _cotpi produces NaN since cospi and sinpi produce Inf. For example,

julia> cospi(500 * exp(im * 3pi/4))
Inf - Inf*im

julia> sinpi(500 * exp(im * 3pi/4))
Inf + Inf*im

julia> cospi(500 * exp(im * 3pi/4)) / sinpi(500 * exp(im * 3pi/4))
NaN + NaN*im

Nevertheless, the ordinary cot gives the correct answer

julia> cot(pi * 500 * exp(im * 3pi/4))
0.0 - 1.0im

A way to avoid NaN is to define cotpi as in mpmath
https://github.com/mpmath/mpmath/blob/b600dbcabf4b7406a61e82b9e607f754a9f12ff9/mpmath/libfp.py#L153-L161

Dominant language
Julia
Stars
384
Forks
115
Avg merge
2d 9h
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from JuliaMath/SpecialFunctions.jl

All issues in JuliaMath/SpecialFunctions.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.