Skip to content

polyfem/polysolve-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

polysolve-python

Small Python binding for PolySolve's nonlinear solver interface.

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, result = polysolve.minimize(
    Quadratic(),
    np.zeros(3),
    {
        "solver": "Newton",
        "line_search": {"method": "Backtracking"},
        "max_iterations": 100,
    },
    {"solver": "Eigen::SimplicialLDLT"},
)

print(x)
print(result)

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.

About

Python Bindings for Polysolve

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors