question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Negative arguments for scipy.special.spherical_jn leads to nans

See original GitHub issue

scipy.special.spherical_jn seems to generate nans for negative input arguments, which does not seem to be prohibited and the documentation does not seem to warn against. For example, the following code

import numpy as np
from scipy.special import spherical_jn

n = 5
zs = np.linspace(-5, 5, 10)
print(spherical_jn(n, zs))

yields an output of [ nan nan nan nan nan 5.03098996e-06 1.11092345e-03 1.17516973e-02 4.66525179e-02 1.06811161e-01]

Based on the reflection formula from DLMF, I used the following fix for my purposes

import numpy as np
from scipy.special import spherical_jn

def spherical_bessel(n, z):
  return np.where(not isinstance(z, complex) and z<0, (-1)**n*spherical_jn(n, -z), spherical_jn(n, z))

zs = np.linspace(-5, 5, 10)
print(spherical_bessel(n, zs))

which gave the result [-1.06811161e-01 -4.66525179e-02 -1.17516973e-02 -1.11092345e-03 -5.03098996e-06 5.03098996e-06 1.11092345e-03 1.17516973e-02 4.66525179e-02 1.06811161e-01]

I suspect that a similar, but more general fix can be used for a PR if this is deemed a legitimate issue.

(I’m using Python 3.7.7, numpy-1.21.1 and scipy-1.7.1)

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
czgdp1807commented, Aug 17, 2021

Thanks. Yeah, it seems like that’s why they weren’t added originally. The PR linked is ready then.

1reaction
cw-tancommented, Aug 17, 2021

@czgdp1807 Although it doesn’t seem necessary, adding them seems nice for completeness and would be good if users need them. They also seem easy to implement by calling existing scipy.special functions, which is what a user who needs them would do anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Special functions (scipy.special) — SciPy v1.9.3 Manual
Errors are handled by returning NaNs or other appropriate values. Some of the special function routines can emit warnings or raise exceptions when...
Read more >
scipy.special.spherical_jn — SciPy v1.9.3 Manual
Argument of the Bessel function. derivativebool, optional. If True, the value of the derivative (rather than the function itself) is returned.
Read more >
scipy.special.binom — SciPy v1.9.3 Manual
binom returns nan when x is a negative integer, but is otherwise defined for negative arguments. comb returns 0 whenever one of x...
Read more >
scipy.special.jv — SciPy v1.9.3 Manual
Bessel function of the first kind of real order and complex argument. Parameters ... Argument (float or complex). ... For negative v values...
Read more >
A Design Specification for nan_policy — SciPy v1.9.3 Manual
stats have a parameter called nan_policy that determines how the function handles data that contains nan . In this section, we provide SciPy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found