Skip to main content

mlx-expm

GPU-accelerated matrix exponential and related functions for Apple MLX.

MLX has no built-in expm. This package fills the gap.

Functions

Function Description Algorithm
expm(A) Matrix exponential Scaling & squaring + [13/13] Pade
expm_frechet(A, E) Frechet derivative of expm Block-triangular (Van Loan)
logm(A) Principal matrix logarithm Inverse scaling & squaring
sqrtm(A) Principal matrix square root Denman-Beavers iteration

Installation

pip install -e ".[dev]"

Usage

import mlx.core as mx
from mlx_expm import expm, logm, sqrtm

# Quantum time evolution: U = exp(-i H t)
H = mx.array([[0.0, 1.0], [1.0, 0.0]])  # Pauli X
t = 0.5
U = expm(-1j * H * t)

# Matrix logarithm (inverse of expm)
A = mx.array([[1.0, 2.0], [0.0, 3.0]])
L = logm(expm(A))  # recovers A

# Matrix square root
S = sqrtm(mx.array([[4.0, 0.0], [0.0, 9.0]]))  # [[2, 0], [0, 3]]

# Frechet derivative
A = mx.array([[1.0, 0.5], [0.0, 2.0]])
E = mx.array([[0.1, 0.0], [0.0, 0.1]])
expm_A, L = expm_frechet(A, E)

Applications

  • Quantum mechanics: Unitary evolution U = exp(-iHt)
  • Control theory: State transition matrix exp(At)
  • Differential equations: Matrix ODE solutions
  • Neural ODEs: Continuous-time dynamics
  • Lie groups: Exponential map on matrix Lie algebras

Algorithm Details

expm — Scaling and Squaring with [13/13] Pade

The same algorithm as scipy.linalg.expm (Higham 2005/2009):

  1. Scaling: Find s such that ||A / 2^s||_1 <= theta_13 = 5.37
  2. Pade: Evaluate the [13/13] rational approximant R_{13}(A/2^s)
  3. Squaring: Recover exp(A) = R_{13}^{2^s} via repeated squaring

This requires 13 matrix multiplications + 1 linear solve — all on MLX GPU.

logm — Inverse Scaling and Squaring

  1. Repeatedly compute square roots (via Denman-Beavers) until ||X - I|| is small
  2. Evaluate log(I + E) via degree-8 Taylor series
  3. Scale back by 2^s

sqrtm — Denman-Beavers Iteration

Quadratically convergent iteration:

  • Y_{k+1} = (Y_k + Z_k^{-1}) / 2
  • Z_{k+1} = (Z_k + Y_k^{-1}) / 2

Converges to Y -> A^{1/2}, Z -> A^{-1/2}.

Benchmark

python benchmark.py
python benchmark.py --sizes 32 64 128 256 512 --complex

Measured on Apple M1 Max (MLX 0.31.1, Python 3.11) vs scipy.linalg.expm:

n real speedup complex speedup
32 0.03x 0.01x
128 0.15x 0.26x
256 0.53x 1.96x
512 0.54x 1.33x
1024 2.08x -
2048 1.87x -

Crossover is around n=1024 real / n=256 complex; below that, SciPy's CPU LAPACK wins on dispatch overhead. Max abs error stays at the float32 noise floor (~1e-6 * n). See benchmark_results.md for full numbers and discussion.

Tests

pytest tests/ -v

References

  • Higham, Functions of Matrices, SIAM, 2008.
  • Al-Mohy & Higham, "A New Scaling and Squaring Algorithm for the Matrix Exponential", SIAM J. Matrix Anal. Appl. 31(3), 2009.
  • Al-Mohy & Higham, "Computing the Frechet Derivative of the Matrix Exponential", SIAM J. Matrix Anal. Appl. 30(4), 2009.

License

MIT — Sheng-Kai Huang

Metadata

Release files for mlx-expm 0.1.1

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-expm 0.1.1
File Size Uploaded Provenance Actions
mlx_expm-0.1.1.tar.gz 11.3 kB Without provenance Details for mlx_expm-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mlx-expm 0.1.1
File Interpreter ABI Platform Actions
mlx_expm-0.1.1-py3-none-any.whl Python 3 none any Details for mlx_expm-0.1.1-py3-none-any.whl

Total release size: 20.8 kB

Release files / mlx_expm-0.1.1.tar.gz

Download URL mlx_expm-0.1.1.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
72d7a9244c8d0f05517a5193517f99209ea13dfaeb716b5dc9b015be1768e01b
BLAKE2b-256 checksum
How to use checksums
4ec0f68631e4a2d81688e3de425646b909f9ef4d4ba8679e0f4f620ef8a36000
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / mlx_expm-0.1.1-py3-none-any.whl

Download URL mlx_expm-0.1.1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45b21675d9a0a0e28de92183bea24b2af3c12970e1997fc1b03afa14dbd46047
BLAKE2b-256 checksum
How to use checksums
07d8b5452a4f3d9b4edc529ad61187b7e7193208c457a16799f0fbc3203a6315
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Security for mlx-expm 0.1.1

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.1.1 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