scipy.misc.derivative for multiple argument function

后端 未结 3 1728
清酒与你
清酒与你 2020-12-28 18:39

It is straightforward to compute the partial derivatives of a function at a point with respect to the first argument using the SciPy function scipy.misc.derivative

3条回答
  •  [愿得一人]
    2020-12-28 19:45

    I would write a simple wrapper, something along the lines of

    def partial_derivative(func, var=0, point=[]):
        args = point[:]
        def wraps(x):
            args[var] = x
            return func(*args)
        return derivative(wraps, point[var], dx = 1e-6)
    

    Demo:

    >>> partial_derivative(foo, 0, [3,1])
    6.0000000008386678
    >>> partial_derivative(foo, 1, [3,1])
    2.9999999995311555
    

提交回复
热议问题