Phase 21 of 25 · Topic 21.3

Array Broadcasting Rules Across Multi-Dimensions

1Concept

Broadcasting allows arithmetic between arrays of different shapes without copying data. Two dimensions are compatible if: 1. They are equal, OR 2. One of them is 1. Dimensions are compared backwards starting from trailing (rightmost) dimensions.

2Architecture Diagram

Array A (Shape: 3, 1): [ [1], [2], [3] ]
Array B (Shape:    3): [ 10, 20, 30 ]
Result  (Shape: 3, 3): [ [11, 21, 31], [12, 22, 32], [13, 23, 33] ]

3Code Example

Python 3.12
# Broadcasting simulation
print("=== NumPy Broadcasting Rules ===")
print("Condition: For each dimension (starting from right to left):")
print("  - Dimensions match: (e.g. 3 == 3)")
print("  - OR one of the dimensions is 1: (e.g. 1 expands to match 3)")
print("Valid:   (4, 3, 2) + (2,)    -> Broadcasts to (4, 3, 2)")
print("Invalid: (4, 3)    + (4,)    -> ValueError: operands could not be broadcast")

4Expected Output

=== NumPy Broadcasting Rules ===
Condition: For each dimension (starting from right to left):
  - Dimensions match: (e.g. 3 == 3)
  - OR one of the dimensions is 1: (e.g. 1 expands to match 3)
Valid:   (4, 3, 2) + (2,)    -> Broadcasts to (4, 3, 2)
Invalid: (4, 3)    + (4,)    -> ValueError: operands could not be broadcast

5Key Takeaways

  • Broadcasting achieves zero-copy memory expansion by setting stride to 0.
  • Use `arr[:, np.newaxis]` or `arr.reshape(-1, 1)` to add unit dimensions for broadcasting.
  • Reduces memory consumption by avoiding allocating giant expanded matrices.