jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.scipy.special.expn(n, 0) returns 1/n instead of 1/(n-1) for n >= 2

Open the-Shallow opened this issue 2 months ago • 2 comments

Description

Description

For x == 0 and n >= 2, jax.scipy.special.expn(n, 0) returns 1/n instead of E_n(0) = 1/(n-1): expn(2, 0) is 0.5 instead of 1.0. SciPy and the Cephes routine this is a port of return 1/(n-1). The branch list in expn uses one / n1 (with n1 guarding the division by zero at n == 1).

Steps to reproduce

from jax.scipy.special import expn
import scipy.special as sc
print(expn(2, 0.0), expn(3, 0.0))            # 0.5 0.33333334
print(sc.expn(2, 0.0), sc.expn(3, 0.0))      # 1.0 0.5

Expected

1.0 0.5.

Suggested fix

Use one / (n1 - one) in that branch; n == 0 and n == 1 are selected by earlier conditions.

System info (python version, jaxlib version, accelerator, etc.)

Environment

JAX / jaxlib 0.11.2 (the latest release, CPU) and 0.10.1, Python 3.12, Ubuntu 24.04, x86_64

the-Shallow avatar Oct 01 '26 21:10 the-Shallow

Reproduced on main (35e4da0) against scipy: expn(2,0) gives 0.5 vs 1.0, expn(3,0) 0.333 vs 0.5, expn(5,0) 0.2 vs 0.25 — so jax returns 1/n where the definition is 1/(n-1) for n >= 2. Happy to send a PR with a scipy-checked test once the Google CLA is in. Drafted with an AI assistant; I own the change. Assign to me?

Adarsh-Me avatar Oct 02 '26 06:10 Adarsh-Me

Fixed by #41404: jax.scipy.special.expn(n, 0) now returns 1 / (n - 1) for n >= 2.

jakevdp avatar Oct 09 '26 19:10 jakevdp