spexial provides special functions for JAX, following the scipy.special API. The implementations are written in terms of JAX primitives, so they compose with jit, grad and vmap, and run on CPU, GPU and TPU.
It exists to fill gaps in jax.scipy.special, of three kinds:
- Missing. The modified Bessel
Kfunctions, the polylogarithm, the Gegenbauer polynomials, the normalized Legendre function, the unregularized incomplete beta and the Cartesian spherical harmonics have no JAX counterpart at any version. - Wrong, or narrower.
zetareturnsnanfor negative integers;spencerejects complex arguments;sph_harm_ypairs each degree with one angle instead of broadcasting, so array degrees give incorrect values. - Badly differentiated. Where a closed form exists, the JVP is written out by hand instead of differentiating through the series. Sometimes that is faster (
incomplete_beta, 143x), sometimes cheaper in memory (polylog, 268x less residual), and sometimes it is the difference between a number andnan— JAX'sspencedifferentiates tonanacross roughly 1 < x < 2, and itssph_harm_yat both poles.
Where SciPy already covers a case, spexial matches it rather than claiming to exceed it. Coverage records, per function, which gap it fills, and what upstream would have to do for the row to be deleted.
pip install spexialor
uv add spexial>>> import jax
>>> jax.config.update("jax_enable_x64", True)
>>> import jax.numpy as jnp
>>> import spexial as sp
>>> # Gegenbauer polynomial C_n^alpha(x), matching scipy.special.eval_gegenbauer
>>> sp.eval_gegenbauer(3, 0.5, 0.25)
Array(-0.3359375, dtype=float64, weak_type=True)
Everything is vectorisable and differentiable in the usual way:
>>> xs = jnp.linspace(-1.0, 1.0, 5)
>>> jax.vmap(lambda x: sp.eval_gegenbauer(3, 0.5, x))(xs)
Array([-1. , 0.4375, -0. , -0.4375, 1. ], dtype=float64)
| Function | scipy.special counterpart |
|---|---|
comb |
comb (the exact=False variant) |
gamma |
gamma — JAX's value, plus an analytic derivative |
eval_gegenbauer |
eval_gegenbauer |
eval_gegenbauers |
-- returns every order up to n |
incomplete_beta |
-- the unregularized B(a, b, z); betainc is the regularized form |
k0, k1, k2 |
k0, k1, kn |
k0e, k1e, k2e |
k0e, k1e, kve — scaled by e^z, no upper limit |
polylog |
-- the polylogarithm |
spence |
spence — complex too, which JAX rejects |
spherical_jn |
spherical_jn |
spherical_jn_all |
-- every order up to n |
sph_legendre_p |
sph_legendre_p — absent from JAX at any version |
sph_harm_y |
sph_harm_y — JAX's returns incorrect values for array degrees |
sph_harm_y_cart |
-- from a Cartesian direction; correct gradients on the z-axis |
sph_harm_y_cart_all |
sph_harm_y_all (layout only) — the whole (l, m) table in one pass |
sph_harm_y_cart_all_terms |
-- as above, returned as separate arrays so reductions stay fused |
zeta |
zeta — negative integers, which JAX gives as nan |
Read Accuracy and domains before relying on any of these. It records, per function, the domain each is tested over and the tolerance it actually meets. Some are not machine-precision — the modified Bessel functions are accurate to about 1e-7, and zeta does not implement the critical strip.
https://jaxtronomy.github.io/spexial
We welcome contributions! See CONTRIBUTING.md.
uv sync --group dev
uv run nox -s all # lint -> test -> docsIf you use spexial in work you publish, please cite it — see CITATION.cff. Several routines originate in the LINX code.
MIT. See LICENSE.