[Tutor] Functional Derivatives

Kent Johnson kent37 at tds.net
Tue Apr 21 02:37:36 CEST 2009


On Mon, Apr 20, 2009 at 7:53 PM, Matt <HellZFury+Python at gmail.com> wrote:
> I'm trying to calculate the derivative of a function in Python like so:
>
> def D5(func,h=1e-5):
>     ""' Return derivative of function func'''
>     def df(x):    return (func(x+h)-func(x))/h
>    df.__name__ = func.__name__ + '_dx'
>     return df
>
> However, I run into the problem of limited float precision.

> Is there any way that any of you can think of to avoid this for
> general-purpose functions? Thanks.

mpmath?
http://code.google.com/p/mpmath/

Kent


More information about the Tutor mailing list