Skip to main content

Some elliptic integrals in JAX

Project description

jaxellip: JAX implementations of some elliptic integrals

test pypi-version pypi-downloads

Elliptic integrals are available in scipy.special, but are not implemented in jax.scipy.special, making them neither differentiable nor compatible with JAX’s JIT compilation. This package provides JAX-compatible implementations of several elliptic integrals from scipy.special: ellipk, ellipkm1, and ellipe. The results are tested against arbitrary-precision mpmath references and cross-checked against scipy.special (see tests).

Usage

  1. Install jaxellip.
pip install jaxellip
  1. Import jaxellip in your Python code and use it.
import jaxellip
import jax.numpy as jnp

input = jnp.linspace(-10, 10, 1000)

jaxellip.ellipk(input) # Complete elliptic integral of the first kind
jaxellip.ellipkm1(input) # Complete elliptic integral of the first kind around m = 1
jaxellip.ellipe(input) # Complete elliptic integral of the second kind

The elliptic integrals in jaxellip follow the same parameter conventions as scipy.special. In particular, the input parameter $m$ is the elliptic parameter, defined as $m = k^2$, where $k$ is the modulus. This differs from some literature or libraries (e.g., Boost or Abramowitz & Stegun), where the integrals are sometimes expressed in terms of $k$ directly.

The function ellipkm1(m) computes the complete elliptic integral of the first kind with argument $1 - m$, i.e., $K(1 - m)$. This is particularly useful for improved numerical stability and precision when $m$ is close to 1, since $K(m)$ diverges logarithmically as $m \to 1$.

Complete elliptic integral of the first kind (ellipk and ellipkm1)

$$ K(m) = \int_0^{\pi/2} \frac{1}{\sqrt{1 - m \sin^2 \theta}} d\theta $$

Complete elliptic integral of the second kind (ellipe)

$$ E(m) = \int_0^{\pi/2} \sqrt{1 - m \sin^2 \theta} d\theta $$

Numerical Method

ellipk and ellipe use the same Cephes minimax polynomials as SciPy: a degree-10 polynomial in the complementary parameter $1 - m$ plus a logarithmic term, with negative $m$ reduced to $[0, 1)$ by the Landen transformation. ellipkm1(x) evaluates $K(1 - x)$ from $x$ directly (never forming $1 - x$) and uses a logarithmic series for $x < 10^{-8}$. The forward pass does not iterate.

Derivatives use exact custom JVP rules instead of differentiating the polynomial. Each reduces to the Carlson symmetric integral $R_D$ (for example, $dK/dm = R_D(0, 1, 1-m)/6$), evaluated by a short fixed iteration whose only zero argument is handled in closed form. This makes jaxellip differentiable in every JAX mode (forward, reverse, and higher order such as jax.hessian), so it stays correct inside a larger function that is differentiated as a whole.

Edge cases match SciPy: ellipk(1) is inf, ellipe(1) is 1, ellipkm1(0) is inf, and out-of-domain inputs (m > 1, or x < 0) return NaN for both value and gradient.

Comparison against SciPy

jaxellip is tested against mpmath (a high-precision reference) and scipy.special:

  • Accuracy. Values agree with both to about 1e-14 relative. Derivatives, in forward, reverse, and second-order modes, agree with mpmath's analytic derivatives to about 1e-13 (first order) and 1e-15 (second order). This holds over the whole domain: m down to -1e308, up to the m -> 1 singularity, and ellipkm1 for x from 1e-308 to 1e308.
  • Speed. Warm CPU runtimes are comparable to scipy.special in the tested cases. Exact ratios depend on the device, runner load, JAX/XLA version, and SciPy build, so the test suite checks only that there are no large performance regressions.

Developer Guide

Install:

  1. Visual Studio Code
  2. uv
  3. just

Clone and set up:

git clone https://github.com/sinaatalay/jaxellip.git
cd jaxellip
just sync
code .

Use .venv as the Python interpreter in VS Code.

Repository layout:

  • src/jaxellip/: package code
  • tests/: tests against SciPy and JAX autodiff
  • pyproject.toml: package metadata and tool settings
  • uv.lock: locked dependency versions
  • justfile: development commands

Common commands:

just test    # run tests
just check   # run all checks
just format  # format code

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jaxellip-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jaxellip-0.1.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file jaxellip-0.1.0.tar.gz.

File metadata

  • Download URL: jaxellip-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jaxellip-0.1.0.tar.gz
Algorithm Hash digest
SHA256 22bfb21a9a632c640036175879ba5f2d6cb3ce65a2a3494f53918c2f5f0ca76c
MD5 641a8312a53d75e3d796bc273c1da3a3
BLAKE2b-256 6e7cb367c5c759bf88f613a3ed960f445a7b65cfcd02b5ec725c467d84ef1a77

See more details on using hashes here.

Provenance

The following attestation bundles were made for jaxellip-0.1.0.tar.gz:

Publisher: publish-a-release.yaml on sinaatalay/jaxellip

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jaxellip-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jaxellip-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jaxellip-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ab4c50841adf4c7532a4be1906ae6a971e631595b4bdb191e414c4101147f461
MD5 88075670f76234c3af0250f78573ceb0
BLAKE2b-256 a06f88f9778120f26589c4944e5c49aa177ca15a5a926f8c47b66f3da6d965ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for jaxellip-0.1.0-py3-none-any.whl:

Publisher: publish-a-release.yaml on sinaatalay/jaxellip

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page