mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-03-15 14:50:25 +01:00
First version of new submodule roots
This commit is contained in:
parent
013c43fd4b
commit
77c816d6a6
2 changed files with 24 additions and 0 deletions
|
@ -4,3 +4,4 @@ from . import linalg
|
|||
from . import misc
|
||||
from . import mpm
|
||||
from . import correlators
|
||||
from . import roots
|
||||
|
|
23
pyerrors/roots.py
Normal file
23
pyerrors/roots.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import scipy.optimize
|
||||
from autograd import jacobian
|
||||
from .pyerrors import Obs, derived_observable, pseudo_Obs
|
||||
|
||||
def find_root(d, func, guess=1.0, **kwargs):
|
||||
"""Finds the root of the function func(x, d) where d is an Obs.
|
||||
|
||||
Parameters
|
||||
-----------------
|
||||
d -- Obs passed to the function.
|
||||
func -- Function to be minimized.
|
||||
guess -- Initial guess for the minimization.
|
||||
"""
|
||||
root = scipy.optimize.fsolve(func, guess, d.value)
|
||||
|
||||
dx = jacobian(func)(root[0], d.value)
|
||||
da = jacobian(lambda u, v : func(v, u))(d.value, root[0])
|
||||
deriv = - da / dx
|
||||
|
||||
return derived_observable(lambda x, **kwargs: x[0], [pseudo_Obs(root, 0.0, d.names[0], d.shape[d.names[0]]), d], man_grad=[0, deriv])
|
Loading…
Add table
Reference in a new issue