Metadata-Version: 2.2
Name: polyfem-polysolve
Version: 0.0.1
Summary: Python bindings for PolySolve nonlinear optimization and linear solvers
Keywords: Optimization,Newton,Linear Solvers,Python Bindings
Author-Email: Teseo Schneider <teseo@uvic.ca>
License: MIT
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Operating System :: OS Independent
Project-URL: Homepage, https://github.com/polyfem/polysolve-python
Project-URL: Repository, https://github.com/polyfem/polysolve-python
Project-URL: Issues, https://github.com/polyfem/polysolve-python/issues
Requires-Python: >=3.9
Requires-Dist: numpy
Requires-Dist: scipy
Provides-Extra: test
Requires-Dist: pytest>=8; extra == "test"
Description-Content-Type: text/markdown

# polysolve-python

[![PyPI](https://img.shields.io/pypi/v/polyfem-polysolve.svg)](https://pypi.org/project/polyfem-polysolve/)
[![Python versions](https://img.shields.io/pypi/pyversions/polyfem-polysolve.svg)](https://pypi.org/project/polyfem-polysolve/)
[![CI](https://github.com/polyfem/polysolve-python/actions/workflows/continuous.yml/badge.svg)](https://github.com/polyfem/polysolve-python/actions/workflows/continuous.yml)
[![Build & publish](https://github.com/polyfem/polysolve-python/actions/workflows/release.yml/badge.svg)](https://github.com/polyfem/polysolve-python/actions/workflows/release.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

Small Python binding for PolySolve's nonlinear and linear solver interfaces.

```python
import numpy as np
import scipy.sparse
import polysolve


class Quadratic(polysolve.Problem):
    def value(self, x):
        y = x - np.array([-2.0, 3.0, 1.0])
        return float(y @ y)

    def gradient(self, x):
        return 2.0 * (x - np.array([-2.0, 3.0, 1.0]))

    def hessian(self, x):
        return 2.0 * scipy.sparse.eye(x.size, format="csc")


x, log = polysolve.minimize(
    Quadratic(),
    np.zeros(3),
    {
        "solver": "Newton",
        "line_search": {"method": "Backtracking"},
        "max_iterations": 100,
    },
    {"solver": "Eigen::SimplicialLDLT"},
)

print(x)
print(log)
```

Python subclasses must implement `value(x)`, `gradient(x)`, and `hessian(x)`. Optional PolySolve callbacks such as `solution_changed`,  `stop`, `is_step_valid`, and `max_step_size` can also be implemented on the subclass.

## Linear solves

For a one-off linear system:

```python
A = scipy.sparse.csc_matrix([[4.0, 1.0], [1.0, 3.0]])
b = np.array([1.0, 2.0])

x = polysolve.solve(A, b, {"solver": "Eigen::SimplicialLDLT"})
```

For repeated solves with the same matrix pattern:

```python
solver = polysolve.LinearSolver({"solver": "Eigen::SimplicialLDLT"})
solver.analyze_pattern(A)
solver.factorise(A)  # factorize(A) is also available

x = solver.solve(b)
print(solver.info())
```
