Skip to content

Instantly share code, notes, and snippets.

@jobliz
Created March 15, 2013 02:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jobliz/5167177 to your computer and use it in GitHub Desktop.
Save jobliz/5167177 to your computer and use it in GitHub Desktop.
The definition of derivative "just works" when coded in a programming language.
# from http://funcall.blogspot.sg/2009/03/not-lisp-again.html
def deriv(f, dx=.0001):
return lambda x: (f(x + dx) - f(x)) / dx
cube = lambda x: x**3
d = deriv(cube)
for n in xrange(5):
print n, d(n)
# 0 1e-08
# 1 3.00030001
# 2 12.00060001
# 3 27.0009000101
# 4 48.0012000099
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment