← back

Product Rule for Derivatives

#309 · Machine Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement the product rule for computing derivatives. Given two functions f and g represented as callable functions, compute the derivative of their product: d/dx [f(x) g(x)] = f'(x) g(x) + f(x) * g'(x).

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np

def numerical_derivative(f, x: float, h: float = 1e-7) -> float:
    return (f(x + h) - f(x - h)) / (2 * h)

def product_rule(f, g, x: float) -> dict:
    f_val = f(x)
    g_val = g(x)
    f_prime = numerical_derivative(f, x)
    g_prime = numerical_derivative(g, x)

    derivative = f_prime * g_val + f_val * g_prime

    return {
        "derivative": derivative,
        "f(x)": f_val,
        "g(x)": g_val,
        "f'(x)": f_prime,
        "g'(x)": g_prime,
    }

def product_rule_array(f_vals: np.ndarray, g_vals: np.ndarray,
                        f_derivs: np.ndarray, g_derivs: np.ndarray) -> np.ndarray:
    return f_derivs * g_vals + f_vals * g_derivs

Explanation

  1. The product rule states: (f g)' = f' g + f * g'.
  2. Compute f(x) and g(x) by evaluating each function at x.
  3. Compute f'(x) and g'(x) using the central difference method for numerical differentiation.
  4. Combine according to the product rule formula.
  5. The array version operates element-wise on pre-computed function values and derivatives.

Complexity

  • Time: O(1) for a single point evaluation (4 function evaluations)
  • Space: O(1)