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

Function divergence

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

Sum of the partial derivatives of a vector field >>> F = lambda x, y, z: (x*y+sin(x)+3*x, x-y-5*x, cos(2*x)-sin(y)**2) >>> divergence(F, (3.5, 2.1, -3.3)) 3.163543312709203 # http://www.wolframalpha.com/input/?i=div+%7Bx*y%2Bsin(x)%2B3*x,+x-y-5*x,+cos(2*x)-sin(y)%5

(F, point)

Source from the content-addressed store, hash-verified

224 return d(func(*map(Num, point, direction)))
225
226def divergence(F, point):
227 ''' Sum of the partial derivatives of a vector field
228
229 >>> F = lambda x, y, z: (x*y+sin(x)+3*x, x-y-5*x, cos(2*x)-sin(y)**2)
230 >>> divergence(F, (3.5, 2.1, -3.3))
231 3.163543312709203
232
233 # http://www.wolframalpha.com/input/?i=div+%7Bx*y%2Bsin(x)%2B3*x,+x-y-5*x,+cos(2*x)-sin(y)%5E2%7D
234 >>> x, y, z = (3.5, 2.1, -3.3)
235 >>> math.cos(x) + y + 2
236 3.1635433127092036
237
238 >>> F = lambda x, y, z: (8 * exp(-x), cosh(z), - y**2)
239 >>> divergence(F, (2, -1, 4))
240 -1.0826822658929016
241
242 # https://www.youtube.com/watch?v=S2rT2zK2bdo
243 >>> x, y, z = (2, -1, 4)
244 >>> -8 * math.exp(-x)
245 -1.0826822658929016
246
247 '''
248 return math.fsum(d(F(*[Num(x, i==index) for i, x in enumerate(point)])[index])
249 for index in range(len(point)))
250
251def curl(F, point):
252 ''' Rotation around a vector field

Callers

nothing calls this directly

Calls 5

rangeFunction · 0.85
NumClass · 0.70
dFunction · 0.50
FFunction · 0.50
enumerateFunction · 0.50

Tested by

no test coverage detected