Jump to content

User talk:TobiasHerp

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

What do think about this? (I think it is same like in java example) --Liso (talk) 11:30, 29 August 2008 (UTC)[reply]

import math

def make_derivative(f, deltaX):
    delta=[deltaX]
    function=[f]
    fnc=lambda x: (function[0](x+delta[0])-function[0](x))/delta[0]
    return fnc

cos=make_derivative(math.sin, 0.0000000001)

# cos(0)     ~> 1.0
# cos(math.pi/2)  ~> 0.0

--Liso (talk) 11:30, 29 August 2008 (UTC)[reply]

I think this is a useful use case for function literals; however, this could (and should) be stripped down to:

def make_derivative(f, deltaX):
    return lambda x: (f(x-deltaX) - f(x)) / deltaX

or even better (this kind of deltaX is usually called epsilon, isn't it?):

def numeric_derivative(f, epsilon):
    assert epsilon > 0.0
    return lambda x: (f(x-epsilon) - f(x)) / epsilon

Btw, it's a Javascript example we refer to ;-) --Tobias (talk) 18:36, 31 August 2008 (UTC)[reply]

Thanx for your clever comments! :) I was sure that I tested stripped version and that didnt worked... So I was wrong. Thanx for fix. I changed function to: ("-" -> "+" and ">" -> "!=")

def numeric_derivative(f, epsilon):
    assert epsilon != 0.0 , "epsilon cannot be zero"
    return lambda x: (f(x+epsilon) - f(x)) / epsilon

You introduce me also "assert" statement. Thanx! Python is better than I thought and I like more and more! :) --Liso (talk) 16:09, 3 September 2008 (UTC)[reply]