Fast compiled C-level math with np.sqrt, np.abs, np.exp, np.log, np.floor, and np.ceil.
A universal function (or ufunc) is a function that operates on ndarrays element-by-element, supporting array broadcasting, type casting, and compiled execution speeds.
Applying Python's `math.sqrt` on 1 million items in a loop requires 1 million Python function calls. A NumPy ufunc executes in a single compiled C loop with CPU vector instructions.
UFuncs bypass Python bytecode execution and loop through array memory directly in C.
res = np.sqrt(arr)import math; [math.sqrt(x) for x in arr] # 🐌 Slow Python loopnp.sqrt(arr) # ⚡ Fast compiled ufuncPython's `math` module functions only accept single scalars. Always use `np.` math functions for arrays.
Solve the objective below using NumPy vectorized syntax
Create `arr = np.array([16, 25, 36])`, calculate its square root using `np.sqrt(arr)`, and print.
[4. 5. 6.]