Broadcasting
Broadcasting
Implement broadcasting rules for tensor operations, allowing operations between tensors of different shapes.
Background
Broadcasting allows operations between arrays of different shapes by virtually expanding the smaller array. Rules:
- Compare shapes from right to left
- Dimensions are compatible if they are equal or one of them is 1
- Missing dimensions are treated as 1
Functions to implement
1. broadcast_shape(shape_a, shape_b)
Compute the resulting shape when broadcasting two tensors.
- Return the broadcast shape or None if incompatible
2. broadcast_add(a, b)
Add two tensors with broadcasting.
3. broadcast_multiply(a, b)
Multiply two tensors element-wise with broadcasting.
Examples
broadcast_shape((3, 1), (1, 4)) # (3, 4)
broadcast_shape((2, 3), (3,)) # (2, 3)
broadcast_shape((2, 3), (4,)) # None (incompatible)
# [[1], [2], [3]] + [10, 20, 30, 40] with broadcasting:
# [[11, 21, 31, 41],
# [12, 22, 32, 42],
# [13, 23, 33, 43]]
Run tests to see results
No issues detected