MCPcopy Create free account
hub / github.com/ActiveState/code / gradient

Function gradient

recipes/Python/580610_Auto_differentiation/recipe-580610.py:194–205  ·  view source on GitHub ↗

Vector of the partial derivatives of a scalar field >>> func = lambda x, y: x*y + sin(x) >>> point = (2.5, 3.5) >>> gradient(func, point) (2.6988563844530664, 2.5) See: https://www.wolframalpha.com/input/?lk=3&i=grad(x*y+%2B+sin(x))

(func, point)

Source from the content-addressed store, hash-verified

192 return d(func(*[Num(x, i==index) for i, x in enumerate(point)]))
193
194def gradient(func, point):
195 ''' Vector of the partial derivatives of a scalar field
196
197 >>> func = lambda x, y: x*y + sin(x)
198 >>> point = (2.5, 3.5)
199 >>> gradient(func, point)
200 (2.6988563844530664, 2.5)
201
202 See: https://www.wolframalpha.com/input/?lk=3&i=grad(x*y+%2B+sin(x))
203
204 '''
205 return tuple(partial(func, point, index) for index in range(len(point)))
206
207def directional_derivative(func, point, direction):
208 ''' The dot product of the gradient and a direction vector.

Callers

nothing calls this directly

Calls 2

partialFunction · 0.85
rangeFunction · 0.85

Tested by

no test coverage detected