For tensors of any rank, broadcast rules define how tensors with different dimensions can interact via element-wise operations. A justifying premise of broadcast is that a rank- tensor can be thought of as a rank- tensor () where the leading dimensions are of length .

Consider tensors and .

  1. If , no broadcast is needed.
  2. Comparing dimensions from right to left, broadcast is permitted if and only if at least one of the following is true:
    • ; and/or
    • ; and/or
    • .

If broadcast is permitted between and , then the following procedure applies.

  1. If the ranks are unequal, prepend the lower-ranked tensor with length-1 dimensions of unit value until the ranks match.
  2. For any dimension where one of the tensors has length 1 and the other has length , expand the tensor with length 1 by repeating its value times.

Examples where broadcast is permitted

Scalar and any tensor. For example, given a scalar (rank-0 tensor) and a vector (rank-1 tensor), any scalar arithmetic operation between them is defined as performing that operation between the scalar and each element of the vector.

Length vector and size matrix. Repeat the vector times, so that it is an matrix with repeated columns. and perform the elementwise operation on each corresponding pair of elements.

Examples where broadcast is not permitted

Vectors of unequal length, neither of length 1. In this case, neither can be expanded to match the other.

Length vector and size matrix. Prepending the vector with length 1 dimensions gives a matrix. Now the rightmost dimensions are of unequal size ().