Numpy array broadcasting rules
From http://cs231n.github.io/python-numpy-tutorial/#numpy-broadcasting:
Broadcasting two arrays together follows these rules:
If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length.
The two arrays are said to be compatible in a dimension if they have the same size in the dimension, or if one of the arrays has size 1 in that dimension.
- The arrays can be broadcast together if they are compatible in all dimensions.
- After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays.
- In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension
If this explanation does not make sense, try reading the explanation from the documentation or this explanation.
Well, the meaning of trailing axes is explained on the linked documentation page.
If you have two arrays with different dimensions number, say one 1x2x3
and other 2x3
, then you compare only the trailing common dimensions, in this case 2x3
. But if both your arrays are two-dimensional, then their corresponding sizes have to be either equal or one of them has to be 1
. Dimensions along which the array has size 1
are called singular, and the array can be broadcasted along them.
In your case you have a 2x2
and 4x2
and 4 != 2
and neither 4
or 2
equals 1
, so this doesn't work.