Documentation Index
Fetch the complete documentation index at: https://numpyts.dev/llms.txt
Use this file to discover all available pages before exploring further.
What is broadcasting?
Broadcasting is the mechanism that lets numpy-ts perform element-wise operations on arrays with different shapes without explicitly copying data. When you add a scalar to a matrix, or multiply a row vector by a column vector, broadcasting determines how the shapes align. numpy-ts follows NumPy’s broadcasting rules exactly.The three rules
Broadcasting compares shapes element-wise, starting from the trailing (rightmost) dimensions:- If the arrays have different numbers of dimensions, the shape of the smaller array is padded with 1s on the left until both shapes have the same length.
- Arrays with size 1 along a particular dimension act as if they had the size of the array with the largest shape along that dimension. The element is repeated virtually (no memory copy).
- If sizes disagree and neither is 1, broadcasting fails with an error.
Scalar broadcasting
The simplest case: a scalar broadcasts to match any array shape.Vector-to-matrix broadcasting
A 1-D array broadcasts along the rows of a 2-D matrix:Outer product pattern
When a column vector meets a row vector, broadcasting produces the outer product:Advanced 3D broadcasting
Broadcasting extends to any number of dimensions. Here is a 3-D example:broadcast_to and broadcast_arrays
broadcast_to(array, shape)
Expand an array to a new shape using broadcasting rules. Returns a read-only view (no data is copied):
broadcast_arrays(...arrays)
Broadcast multiple arrays against each other, returning a list of arrays with a common shape:
broadcast_shapes(...shapes)
Compute the broadcasted shape without creating any arrays:
Shape compatibility errors
When shapes are incompatible, numpy-ts throws an error. Two dimensions are incompatible when they differ and neither is 1:| Shape A | Shape B | Compatible? | Reason |
|---|---|---|---|
[3] | [4] | No | 3 vs 4, neither is 1 |
[2, 3] | [3, 2] | No | Both dims disagree |
[2, 1] | [3, 4] | No | First dim: 2 vs 3, neither is 1 |
[4, 3] | [3] | Yes | Padded to [1, 3], then [4, 3] |
[5, 1] | [1, 6] | Yes | Result [5, 6] |
[2, 3, 4] | [3, 1] | Yes | Padded to [1, 3, 1], result [2, 3, 4] |
Visual shape alignment
When comparing shapes, numpy-ts right-aligns them and pads the shorter one with 1s on the left:Broadcasting only creates virtual copies. The element at the size-1 dimension is reused via stride tricks (stride of 0), so memory usage stays proportional to the original array, not the broadcast shape.
Next steps
Slicing & Indexing
Select sub-arrays, rows, columns, and use fancy indexing.
Data Types
Understand the 13 supported dtypes and type promotion rules.