Skip to main content

pysolve-gn

Description

Robust Gauss-Newton Least Squares Solver.

pysolve-gn is a Python package designed to solve the generalized nonlinear least squares problem using the Gauss-Newton method. The package provides efficient algorithms for solving nonlinear optimization problems, making it suitable for a wide range of applications in data fitting, machine learning, and scientific computing.

Main features:

  • Multiple terms: combine data terms and regularization terms, each with its own weight, defined either by residuals and Jacobian (rJ) or by gradient and Hessian (gH).
  • Robust loss functions: "linear", "soft_l1", "huber", "cauchy", "arctan", "tukey", "barron" or a custom loss, with a soft threshold loss_scale expressed in the unit of the residuals.
  • Levenberg-Marquardt damping: damping="lm" or "lm-diag" for difficult problems.
  • Parametrization: optimize the problem in another parameter space p_out = P(p_in).
  • Finite differences: numerical Jacobians when the analytical ones are not available.
  • Batch solver: solve thousands of independent problems at once with solve_batch, each problem converging at its own pace.

Examples

import numpy as np
import pysolvegn

np.random.seed(0)


# Define the model function
def model(params, x):
    a, b = params
    return a * np.exp(b * x)


# Generate synthetic data points
x_data = np.linspace(0, 3, 100)
true_params = [2.5, 0.5]  # True parameters for the curve: y = a * exp(b * x)
y_true = model(true_params, x_data)
y_data = y_true + 0.5 * np.random.normal(size=y_true.shape)  # Add noise to the data


# Define the residual function
def residual_func(params):
    return model(params, x_data) - y_data


# Define the Jacobian function
def jacobian_func(params):
    a, b = params
    J = np.zeros((len(x_data), len(params)))
    J[:, 0] = np.exp(b * x_data)  # Derivative with respect to a
    J[:, 1] = a * x_data * np.exp(b * x_data)  # Derivative with respect to b
    return J


data_term = pysolvegn.Term.from_rJ(
    residual_func=residual_func,
    jacobian_func=jacobian_func,
    loss="linear",
    weight=1.0,
)

initial_params = np.array([2.0, 0.4])

result = pysolvegn.solve(
    terms=data_term,
    p0=initial_params,
    max_iteration=10,
    xtol=1e-6,
    ftol=1e-6,
    verbosity=2,
)

print(result.success)     # True if a convergence criterion was satisfied
print(result.message)   # Why the optimization stopped
print(result.parameters)  # Fitted parameters [a, b]

To reduce the influence of outliers, use a robust loss function with a soft threshold loss_scale close to the expected noise level (in the unit of the residuals), and Levenberg-Marquardt damping:

robust_term = pysolvegn.Term.from_rJ(
    residual_func=residual_func,
    jacobian_func=jacobian_func,
    loss="cauchy",
    loss_scale=1.0,
)

result = pysolvegn.solve(
    terms=robust_term,
    p0=initial_params,
    max_iteration=50,
    ftol=1e-8,
    damping="lm-diag",
)

More examples are available in the online documentation.

Authors

Installation

Install with pip

pip install pysolve-gn

Or :

pip install git+https://github.com/Artezaru/pysolve-gn.git

Then import the package with pysolvegn

License

pysolve-gn - Robust Gauss-Newton Least Squares Solver. Copyright (C) 2026 Artezaru, artezaru.github@proton.me

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Metadata

Release files for pysolve-gn 2.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 pysolve-gn 2.2.0
File Size Uploaded Provenance Actions
pysolve_gn-2.2.0.tar.gz 4.2 MB Without provenance Details for pysolve_gn-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pysolve-gn 2.2.0
File Interpreter ABI Platform Actions
pysolve_gn-2.2.0-py3-none-any.whl Python 3 none any Details for pysolve_gn-2.2.0-py3-none-any.whl

Total release size: 4.4 MB

Release files / pysolve_gn-2.2.0.tar.gz

Download URL pysolve_gn-2.2.0.tar.gz
Size 4.2 MB
Tags Source
SHA-256 checksum
How to use checksums
dced94459475f7c61937939c14b88e42aaeecbab3ea325a174dd5058aca5f75f
BLAKE2b-256 checksum
How to use checksums
5a9d9117b911ec516f7ac654c52b80518679440e4f1ac421cce51ffe415e9a1f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Release files / pysolve_gn-2.2.0-py3-none-any.whl

Download URL pysolve_gn-2.2.0-py3-none-any.whl
Size 177.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b9877c05b3ca0fd44e8af3d8b187c276f39db814ce4791f227574a313508017
BLAKE2b-256 checksum
How to use checksums
885391ecda54749d86df52a832594d7067ba5938b00f1ba23dca28abbd42ca12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.18

Security for pysolve-gn 2.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.

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