Python Implementation of Gradient

Implementations

Simple Script

The following code calculates the gradient of a function at the inputted arguments
epsilon = 0.0000000001 def gradient1(method, arg): right = method(arg+epsilon) left = method(arg-epsilon) return (right-left)/(2*epsilon) def gradient(method, args): grad = [] for index, value in enumerate(args): def method2(argument): arguments = []; for i in range(len(args)): if i == index: arguments.append(argument) else: arguments.append(args[i]) value = method(*arguments) return value calc = gradient1(method2, args[index]) grad.append(calc) return grad def test(a, b): return a**2 + 5*b temp = gradient(test, [2,2])