Skip to main content

koopman

CI PyPI

Exact, closed-form custom-gradient ops for the linear Koopman recurrence z_{t+1} = K @ z_t. Computes the full state sequence

Z = [z_0, K z_0, K^2 z_0, ..., K^T z_0]

with a single forward scan and an exact adjoint backward — O(T n^2) time and O(T n) memory — instead of taping every per-step matmul through autodiff. Available in a PyTorch flavor (torch.autograd.Function) and a JAX flavor (jax.custom_vjp).

Installation

Install only the backend you need:

pip install koopman[torch]      # PyTorch (CPU or GPU)
pip install koopman[jax-cpu]    # JAX on CPU
pip install koopman[jax-gpu]    # JAX on GPU (CUDA 12)

For a CPU-only PyTorch wheel (e.g. on free CI runners), install torch from the CPU index first, then the package:

pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install koopman

Requires Python 3.10+.

Usage

PyTorch

import torch
from koopman.torch import linear_powers_sequence

n, T = 16, 1024
K = torch.randn(n, n, dtype=torch.float64, requires_grad=True)
z0 = torch.randn(n, dtype=torch.float64, requires_grad=True)

Z = linear_powers_sequence(K, z0, T)   # [T+1, n]
Z.sum().backward()                     # exact grad_K, grad_z0 via adjoint recurrence

There is also a torch.nn.Module wrapper, LinearPowersSequence(T), and the raw LinearPowersSequenceFn autograd function.

JAX

import jax
jax.config.update("jax_enable_x64", True)
import jax.numpy as jnp
from koopman.jax import linear_powers_sequence

n, T = 16, 1024
K = jax.random.normal(jax.random.PRNGKey(0), (n, n))
z0 = jax.random.normal(jax.random.PRNGKey(1), (n,))

Z = linear_powers_sequence(K, z0, T)   # [T+1, n]
loss = lambda K_, z0_: jnp.sum(linear_powers_sequence(K_, z0_, T))
gK, gz0 = jax.grad(loss, argnums=(0, 1))(K, z0)   # jit/vmap-friendly

Why a custom gradient?

Naive autodiff through the Python/lax.scan loop tapes all T matmuls and their activations, costing O(T n^2) memory. The backward here is the closed-form adjoint recurrence:

a_T      = grad_Z[T]
a_t      = grad_Z[t] + Kᵀ @ a_{t+1}        (t = T-1, ..., 0)
grad_K  += outer(a_{t+1}, z_t)
grad_z0  = a_0

It reuses the forward states (O(T n) memory) and avoids materializing the autodiff tape. See benchmarks/ for the comparison against naive autograd and expm-based baselines.

Development

pip install -e ".[dev]"
pytest tests/test_linear_powers_torch.py -v   # PyTorch
JAX_PLATFORMS=cpu pytest tests/test_linear_powers_jax.py -v   # JAX

License

Apache-2.0.

Metadata

Release files for koopman 0.1.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 koopman 0.1.0
File Size Uploaded
koopman-0.1.0.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for koopman 0.1.0
File Interpreter ABI Platform
koopman-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.7 kB

Release files / koopman-0.1.0.tar.gz

Download URL koopman-0.1.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c980b452d9c9821398db61507c539229e22a479362d1f7f274fdf3c90cccb7a7
BLAKE2b-256 checksum
How to use checksums
fffa535c58a4d140925a9c25f145b585b66e0bfea774ee330fcac4bfa4ada07e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.

Transparency log

Release files / koopman-0.1.0-py3-none-any.whl

Download URL koopman-0.1.0-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68dc9dc41f2a563e9bdc98041e2c6d16c079e83bbf82048cba5c167c32051884
BLAKE2b-256 checksum
How to use checksums
3eebd80512267d98eaee8a9ad4cc9f97a662ca027e2eee8b1eb1ded9a32ddc49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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