The Directional Derivative

~12 mincode completion

Implement directional_derivative(grad, u), normalising u yourself, and returning the slope as a float.

Examples

Along the x axis you feel only the x component of the gradient

Input
directional_derivative([3, 4], [1, 0])
Output
3

Along the gradient itself the slope is its full magnitude

Input
directional_derivative([3, 4], [3, 4])
Output
5

Perpendicular to the gradient the function does not change

Input
directional_derivative([3, 4], [-4, 3])
Output
0

Hints

Hint 1

gives the magnitude in one call; pick the axis deliberately.

Hint 2

Watch for this: forgets to normalise u.

Requirements

  • grad: the gradient at the point, shape (n,)

  • u: a direction, shape (n,), NOT necessarily unit length

  • Return float: grad . (u / ||u||)

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

6 employers weight this skill

4 quant funds, 1 big tech firm, 1 frontier lab. Top match scores 93.

Python
import numpy as np


def directional_derivative(grad, u):
    """
    Slope of f along the direction u.

    Args:
        grad: the gradient at the point, shape (n,)
        u:    a direction, shape (n,), NOT necessarily unit length

    Returns:
        float: grad . (u / ||u||)
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.