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 thresholdloss_scaleexpressed 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
-
Artezaru artezaru.github@proton.me
-
Git Plateform: https://github.com/Artezaru/pysolve-gn.git
-
Online Documentation: https://Artezaru.github.io/pysolve-gn
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.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pysolve_gn-2.1.0.tar.gz | 4.2 MB | Without provenance | Details for pysolve_gn-2.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pysolve_gn-2.1.0-py3-none-any.whl | Python 3 | none | any | Details for pysolve_gn-2.1.0-py3-none-any.whl |
Total release size: 4.4 MB
Release files / pysolve_gn-2.1.0.tar.gz
| Download URL | pysolve_gn-2.1.0.tar.gz |
|---|---|
| Size | 4.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d220600f1341428086df4542a70aa904f46be7edf4a19cefcf8ef29b12aa6b6e
|
|
BLAKE2b-256 checksum How to use checksums |
c5ddd5dba2aae43c54b004273bebff3fd73e1c13d8e24267baaa3360663b3107
|
| 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.1.0-py3-none-any.whl
| Download URL | pysolve_gn-2.1.0-py3-none-any.whl |
|---|---|
| Size | 176.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f94e6753f8114ec7d16b87bf074b3b8e36777a4d0993ae142892438dac78b352
|
|
BLAKE2b-256 checksum How to use checksums |
6e9b7c58990460fee3f344291c1524fd9c7579f0e6e61987f016dcc94d1a202c
|
| 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.1.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.