derivative (pmrf.derivative)
- pmrf.derivative(eval_fn: Callable[[...], Any], *args: Unpack[Ts]) Tuple[Unpack[Ts]]
Computes the exact derivative of a function.
Dynamically routes to the most relevant autodiff method: - grad (reverse-mode) for scalar outputs. - jacfwd (forward-mode) for wide Jacobians (output size > input size). - jacrev (reverse-mode) for tall Jacobians (input size >= output size).
Safely handles models as inputs by filtering out non-differentiable static fields (like strings, booleans, integers, AND NumPy arrays).
- Parameters:
eval_fn (Callable) – The function to differentiate.
*args (*Ts) – The arguments to evaluate the derivative at. Can be raw arrays or full Models.
- Returns:
A tuple containing the derivatives. The tuple length and contents will exactly mirror the types and structure of the input args.
- Return type:
Tuple[*Ts]
Examples
Compute the sensitivity of a component’s response with respect to its parameters:
>>> import pmrf as prf >>> from pmrf.models import ShuntCapacitor >>> >>> freq = prf.Frequency(2.4, 2.4, 1, 'GHz') >>> cap = ShuntCapacitor(C=prf.Unconstrained(1.0e-12), name='c1') >>> >>> def eval_s21(model): ... return model.s_mag(freq)[0, 1, 0] ... >>> (d_cap,) = derivative(eval_s21, cap) >>> >>> # The structural layout of the model is preserved in the derivative >>> print(f"{d_cap.at('c1.C').get():.3e}") -1.060e-01