From 77c816d6a6c005047ae665fd12a1d5822d6e287f Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Mon, 27 Sep 2021 15:21:55 +0100 Subject: [PATCH] First version of new submodule roots --- pyerrors/__init__.py | 1 + pyerrors/roots.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 pyerrors/roots.py diff --git a/pyerrors/__init__.py b/pyerrors/__init__.py index f0240f2d..f1e438a6 100644 --- a/pyerrors/__init__.py +++ b/pyerrors/__init__.py @@ -4,3 +4,4 @@ from . import linalg from . import misc from . import mpm from . import correlators +from . import roots diff --git a/pyerrors/roots.py b/pyerrors/roots.py new file mode 100644 index 00000000..93c985a1 --- /dev/null +++ b/pyerrors/roots.py @@ -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])