Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add complex support for the digamma function#8163

Merged
asi1024 merged 8 commits into
cupy:mainfrom
steppi:complex-digamma
Feb 9, 2024
Merged

Add complex support for the digamma function#8163
asi1024 merged 8 commits into
cupy:mainfrom
steppi:complex-digamma

Conversation

@steppi

@steppi steppi commented Feb 5, 2024

Copy link
Copy Markdown
Contributor

This PR adds support for complex values to the digamma function. This is made possible by the recent translation of SciPy's complex valued digamma implementation to CUDA compatible C++, scipy/scipy#19834. This PR also makes an improvement to the real valued digamma implementation which was made in SciPy but had not been ported to CuPy (scipy/scipy#8129).

I've copied over the entirety of the SciPy special C++ library as it exists now, despite it not all being used for digamma, but if you'd like, I can go through and try to add only the minimum number of headers that are actually needed. I just thought it would be easier to maintain if SciPy and CuPy vendor the same version of this set of files.

polevl_definition in _digamma.py is no longer used for the digamma function but I've left it in place because it is imported in other files.

@emcastillo emcastillo added cat:enhancement Improvements to existing features prio:medium labels Feb 6, 2024
@@ -10,7 +10,7 @@ class TestDigamma(unittest.TestCase):

@testing.with_requires('scipy>=1.1.0')
@testing.for_all_dtypes(no_complex=True)

@asi1024 asi1024 Feb 6, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you remove no_complex=True option to enable tests for complex inputs?

Suggested change
@testing.for_all_dtypes(no_complex=True)
@testing.for_all_dtypes()

@steppi

steppi commented Feb 6, 2024

Copy link
Copy Markdown
Contributor Author

Thanks @asi1024, it was oversight on my part not to set no_complex=False. I've found there is a failing test due to differences in how division by complex infinity is handled in the C++ standard library on my system vs in thrust::complex.

For large z, (including infinite z) the code uses an asymptotic expansion log(z) - 0.5/z (plus additional terms if needed). If the input is inf + x*1i, for finite x, then in the C++ standard library on my system, -0.5/z evaluates to 0i, but for thrust complex numbers it evaluates to nan + nan*1i. I've found the C++ standard library does not specify the behavior of division by complex infinities; the behavior is implementation dependent. The fix will be to make sure that the results do not depend on the outcome of any divisions by complex infinity. I'll update the code to check for infinities and handle them separately.

@asi1024

asi1024 commented Feb 7, 2024

Copy link
Copy Markdown
Member

/test mini

@asi1024 asi1024 added this to the v14.0.0a1 milestone Feb 7, 2024
@asi1024

asi1024 commented Feb 9, 2024

Copy link
Copy Markdown
Member

@steppi LGTM! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:enhancement Improvements to existing features prio:medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants