Compute the gradient direction and magnitude for a given multivariable function at a specific point. The gradient points in the direction of steepest ascent.
import numpy as np
def numerical_gradient(f, x: np.ndarray, h: float = 1e-7) -> np.ndarray:
grad = np.zeros_like(x, dtype=float)
for i in range(len(x)):
x_plus = x.copy()
x_minus = x.copy()
x_plus[i] += h
x_minus[i] -= h
grad[i] = (f(x_plus) - f(x_minus)) / (2 * h)
return grad
def gradient_direction_and_magnitude(f, x: np.ndarray) -> dict:
grad = numerical_gradient(f, x)
magnitude = float(np.linalg.norm(grad))
if magnitude > 0:
direction = grad / magnitude
else:
direction = grad
return {
"gradient": grad,
"magnitude": magnitude,
"direction": direction,
}