Skip to main content

mlx-hyp2f1

GPU-accelerated hypergeometric functions for Apple MLX.

JAX has hyp2f1 but PyTorch, CuPy, and MLX do not. This package fills the gap for MLX, providing vectorized implementations that run on Apple Silicon GPU.

Features

  • hyp2f1(a, b, c, z) -- Gauss hypergeometric function 2F1
  • hyp1f1(a, b, z) -- Confluent (Kummer) hypergeometric function 1F1
  • hyp0f1(b, z) -- Bessel-related hypergeometric function 0F1
  • Fully vectorized: compute over arrays of parameters simultaneously on GPU
  • Fused Metal kernel for the Taylor series: 1 GPU dispatch instead of ~200 ops, ~5x faster than the pure-MLX op path on small/medium arrays
  • Numerically stable via log-gamma Pochhammer symbols
  • Analytic continuation for |z| >= 1 using linear transformation formulas
  • Pure MLX -- no SciPy runtime dependency

Installation

pip install mlx-hyp2f1

Or from source:

git clone https://github.com/akaiHuang/mlx-hyp2f1.git
cd mlx-hyp2f1
pip install -e ".[dev]"

Usage

import mlx.core as mx
from mlx_hyp2f1 import hyp2f1, hyp1f1, hyp0f1

# Scalar
result = hyp2f1(0.5, 1.0, 1.5, 0.3)

# Vectorized over z
z = mx.linspace(0.0, 0.95, 100)
result = hyp2f1(0.5, 1.0, 1.5, z)

# Vectorized over all parameters
a = mx.array([0.5, 1.0, 2.0])
b = mx.array([1.0, 1.5, 0.5])
c = mx.array([1.5, 2.0, 3.0])
z = mx.array([0.3, 0.5, 0.8])
result = hyp2f1(a, b, c, z)

# Confluent hypergeometric (Kummer)
result = hyp1f1(1.0, 2.0, mx.linspace(-5.0, 5.0, 100))

# Bessel-related
result = hyp0f1(1.0, mx.linspace(-10.0, 10.0, 100))

Cosmological growth factor D(a)

import mlx.core as mx
from mlx_hyp2f1 import hyp2f1

def growth_factor(a, omega_m=0.3):
    """Linear growth factor D(a) for flat LCDM."""
    omega_l = 1.0 - omega_m
    x = -omega_l / omega_m * a**3
    return a * hyp2f1(1.0/3.0, 1.0, 11.0/6.0, x)

Benchmark

python -m mlx_hyp2f1.benchmark

Measured on Apple M1 Max (MLX 0.31.1, SciPy 1.16.2, float32 vs float64 reference) -- with the fused Metal kernel:

N MLX (ms) SciPy (ms) Speedup Max rel err
100 8.10 0.06 0.01x 1.92e-06
1,000 7.50 0.20 0.03x 2.17e-06
10,000 7.23 1.73 0.24x 2.57e-06
100,000 7.51 17.05 2.27x 2.67e-06
1,000,000 22.56 167.65 7.43x 2.95e-06
5,000,000 133.17 838.77 6.30x 3.10e-06
10,000,000 289.64 1,712.08 5.91x 3.10e-06

The fused Metal kernel cuts launch overhead from ~50 ms (200 op dispatches) to ~7 ms (1 dispatch). SciPy crossover is now at N≈100k, and at 1M+ points mlx-hyp2f1 is 5-7x faster than SciPy. Below ~50k points SciPy still wins because even one GPU launch costs more than SciPy's CPU loop.

See benchmark_results.md for full results, the MLX→MLX comparison table (5x speedup vs the pre-fusion op path), 1F1/0F1 numbers, and accuracy details.

Testing

pip install -e ".[dev]"
pytest

Applications

  • Cosmological growth factor D(a)
  • Beta distribution CDF / PDF
  • Angular momentum coupling coefficients
  • Legendre / Jacobi / Gegenbauer polynomials
  • Scattering amplitudes in quantum mechanics

License

MIT -- Sheng-Kai Huang

Metadata

Release files for mlx-hyp2f1 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mlx-hyp2f1 0.2.0
File Size Uploaded Provenance Actions
mlx_hyp2f1-0.2.0.tar.gz 13.6 kB Without provenance Details for mlx_hyp2f1-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mlx-hyp2f1 0.2.0
File Interpreter ABI Platform Actions
mlx_hyp2f1-0.2.0-py3-none-any.whl Python 3 none any Details for mlx_hyp2f1-0.2.0-py3-none-any.whl

Total release size: 24.8 kB

Release files / mlx_hyp2f1-0.2.0.tar.gz

Download URL mlx_hyp2f1-0.2.0.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
001db3a4a898032cc2370f86141e6332089d4e986aff8797204db8a20661ae70
BLAKE2b-256 checksum
How to use checksums
90fb6c4db62545745480c68987ce79c96ae7e31b39280f551b1c6cad89561c6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / mlx_hyp2f1-0.2.0-py3-none-any.whl

Download URL mlx_hyp2f1-0.2.0-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
904c91d21d279fbae889f606f819b4a01e1f91c889ec34589fa0e0720a946145
BLAKE2b-256 checksum
How to use checksums
aca2f65ed6d36432419bd5fa5cf36f07fd407356cf18ca2eb5db87217375afb9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for mlx-hyp2f1 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page