Skip to content

Instantly share code, notes, and snippets.

@pielgrzym
Created November 16, 2013 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pielgrzym/7504680 to your computer and use it in GitHub Desktop.
Save pielgrzym/7504680 to your computer and use it in GitHub Desktop.
import sys
import functools
class der(object):
def __init__(self, h):
if isinstance(h, float):
self.h = h
else:
self.h = sys.float_info.epsilon*1000
self.fun = h
def __call__(self, f):
if isinstance(f, float):
x = f
return (self.fun(x+self.h)-self.fun(x))/self.h
@functools.wraps(f)
def wrapped_f(*args):
x = float(args[0])
return (f(x+self.h)-f(x))/self.h
return wrapped_f
@der
def f(x):
return x**2
print f(0.001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment