GPU-accelerated matrix exponential and related functions for Apple MLX
Project description
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):
- Scaling: Find
ssuch that||A / 2^s||_1 <= theta_13 = 5.37 - Pade: Evaluate the [13/13] rational approximant
R_{13}(A/2^s) - 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
- Repeatedly compute square roots (via Denman-Beavers) until
||X - I||is small - Evaluate
log(I + E)via degree-8 Taylor series - Scale back by
2^s
sqrtm — Denman-Beavers Iteration
Quadratically convergent iteration:
Y_{k+1} = (Y_k + Z_k^{-1}) / 2Z_{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
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mlx_expm-0.1.0.tar.gz.
File metadata
- Download URL: mlx_expm-0.1.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
514580433d8dea100fa07d5a5afbd60a2ed5840e667cd35cd94e2a299684685d
|
|
| MD5 |
d2b65a1fd2958ca35625e95fcae6b1f6
|
|
| BLAKE2b-256 |
7813e0c8375835ebc3e5447dfabdd7d7014f2e6b1cf7be6fb7be915a83f82c3d
|
File details
Details for the file mlx_expm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mlx_expm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a99b72bf7c5d988a2beb9c6b61d5d440d0552d30f575f7cf836ea16b2373b0ab
|
|
| MD5 |
2fa2aff0dd31bc1bc88f16b15f2f5076
|
|
| BLAKE2b-256 |
ec70fc583776087e58e0a4da0ad55436a5f002eb7e75b8244d5e8dcf58a5c6e1
|