Complete worked example
python
import numpy as np
def inspect_example(numerators, denominators):
top = np.asarray(numerators, dtype=np.float64)
bottom = np.asarray(denominators, dtype=np.float64)
if top.ndim != 1 or top.shape != bottom.shape:
raise ValueError("expected aligned vectors")
if not np.isfinite(top).all() or not np.isfinite(bottom).all():
raise ValueError("expected finite values")
result = np.zeros_like(top)
np.divide(top, bottom, out=result, where=bottom != 0)
return result- Reasoning
- The worked function validates aligned one-dimensional finite arrays. It creates an output initialized to zero and calls np.divide with out=result and where=bottom != 0. Positions with nonzero denominators are computed, while zero-denominator positions retain their initialized fallback. The lab treats unexpected RuntimeWarnings as failures, so a quiet-looking output cannot conceal invalid arithmetic performed along the way. This contract is deliberately elementwise and shape-preserving. A zero numerator with a valid denominator produces a valid zero result. A zero denominator invokes the fallback even when the numerator is also zero. Keep those cases distinct when explaining the mask.