Home Tags About

Python 闭包示例:对函数进行求导

24 Oct 2012
python fp

Code:

#!python
def derivative(f):
    # Computes the numerical derivative of a function.
    def df(x, dx=1e-6):
        return (f(x+dx) - f(x)) / dx
    return df

def g(x):
    return x ** 2

# first derivative of g
dg = derivative(f)

# second derivative of g
d2g = derivative(dg)