Skip to main content

nlls_gram

CI Docs PyPI Python versions License: MIT Ruff

Levenberg-Marquardt and Newton nonlinear least squares for JAX pytrees, aimed at solving systems of equations. The core use case is underdetermined systems — more parameters than residuals — where a zero-residual root is not unique and something must select which interpolant is returned. Two solvers share a unified init/update/solve protocol and support both unpenalized and ridge-penalized modes:

  • LevenbergMarquardt (trust-region damping) and Newton (Armijo backtracking line search) with ridge=<positive scalar> put the selection in the objective, minimizing (|r(x)|^2 + \lambda,|x_m|_W^2) for a positive-definite metric (W) on the metric block of (x = [x_m; x_f]) (the free block stays unpenalized). Annealing (\lambda) toward zero tracks global (or local-basin) minimizers toward the minimum-seminorm — e.g. minimum-RKHS-norm — interpolant, by classical nonlinear Tikhonov regularization. Every inner problem is a well-posed NLLS.
  • LevenbergMarquardt and Newton with ridge=None (the default) put the selection in the step geometry: minimizing (|r(x)|^2) where the trust region or Gauss-Newton pseudoinverse is measured in (W), so the step direction is the minimum-(W)-norm correction.

Both take a residual over (x), (x, args), or (x, args, p), flatten any pytree x, expose per-step update(...) and an internally jitted solve(...) loop with callbacks and multi-start, and differentiate solve(...).x with respect to p through a custom implicit rule — no unrolling.

Install

uv add nlls-gram

For GPU use, install the JAX accelerator build that matches your hardware:

uv add nlls-gram "jax[cuda13]"

Minimum-RKHS-norm interpolation

import jax.numpy as jnp
from nlls_gram import AnnealRidge, LevenbergMarquardt, RepeatedFactorMetric

# W = blockdiag(K, K): the RKHS seminorm over two coefficient blocks. The
# constructor takes the FACTOR; shift a semidefinite K by epsilon*I first.
metric = RepeatedFactorMetric(jnp.linalg.cholesky(K, upper=True), repeats=2)

solver = LevenbergMarquardt(collocation_residual, metric=metric, ridge=1e-4)

# Anneal the ridge toward the interpolating limit on stationarity.
anneal = AnnealRidge(ridge_floor=1e-10)
result = solver.solve(
    x0, callback=anneal, user_state=anneal.init_state(), gtol=1e-8, atol=1e-8
)

General nonlinear least squares

import jax, jax.numpy as jnp
from nlls_gram import LevenbergMarquardt


def residual(x, args, p):
    return args["design"] @ x - p["target"]


solver = LevenbergMarquardt(residual)
result = solver.solve(
    jnp.zeros(8), {"design": design}, p={"target": y}, max_steps=200, atol=1e-8
)

# The solution is differentiable in p, at a cost independent of max_steps.
sensitivity = jax.grad(
    lambda p: jnp.sum(
        solver.solve(jnp.zeros(8), {"design": design}, p=p, max_steps=200, atol=1e-8).x
        ** 2
    )
)({"target": y})

Pass metric= to weight the damping geometry.

Linear solvers

The same typed configs serve both solvers: Cholesky() (the default, auto-selecting the smaller of the dual and normal systems), QR() (damping-row QR in the forward role — stable at tiny damping and rank-safe when damped), CG(precond) and GramCG(precond) (matrix-free in parameter and residual space), LU() for square systems, and SVD() for rank-deficient steps or implicit-AD tangents. A knob that exists for only one method is a field on that method, so it cannot be passed with another.

Row-wise residuals

For an expensive per-state residual vmapped over a simulated path, replace that jax.vmap with vmap_rows: the same values, with each row's gradient computed once per linearization and reused across the Jacobian assembly instead of one pass of the row function per row and Jacobian column (or cotangent row).

from nlls_gram import vmap_rows


@vmap_rows(in_axes=(0, None, None))
def euler_residual(state, policy, p): ...

Docs

https://highdimensionaleconlab.github.io/nlls_gram/

License

MIT.

Metadata

Release files for nlls-gram 3.0.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 nlls-gram 3.0.0
File Size Uploaded Provenance Actions
nlls_gram-3.0.0.tar.gz 233.8 kB With provenance Details for nlls_gram-3.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nlls-gram 3.0.0
File Interpreter ABI Platform Actions
nlls_gram-3.0.0-py3-none-any.whl Python 3 none any Details for nlls_gram-3.0.0-py3-none-any.whl

Total release size: 315.9 kB

Release files / nlls_gram-3.0.0.tar.gz

Download URL nlls_gram-3.0.0.tar.gz
Size 233.8 kB
Tags Source
SHA-256 checksum
How to use checksums
99f8eb5c006eb87df8ad76ada92e17ba6de6333d8016c38ad0d99fdb5f584021
BLAKE2b-256 checksum
How to use checksums
cc7518efb9ffd47641e78361bb6924162684b4facde5d32953bf8dcc701e5a67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 1, 2026.

Transparency log

Release files / nlls_gram-3.0.0-py3-none-any.whl

Download URL nlls_gram-3.0.0-py3-none-any.whl
Size 82.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9cefdebb987850962acd4eea9170285cb8ca8a22e9df39e67fe3e69048ef122
BLAKE2b-256 checksum
How to use checksums
16e9ba276024f72565826708a523de9a777e3e845330bdb368f9afd554b618e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 1, 2026.

Transparency log

Security for nlls-gram 3.0.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for nlls_gram-3.0.0.tar.gz

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 Nov 1, 2026.

Transparency log

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

3.0.0 This release

2 release files Full 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