1.算法流程
Inputs: array A with m dimensions; array B with n dimensions
p = max(m, n) #p为A和B的维度较大的一个
if m < p:#填充维度
left-pad A's shape with 1s until it also has p dimensions #在A的维度的左边填充1
else if n < p:
left-pad B's shape with 1s until is also has p dimensions #在B的维度的左边填充1
result_dims = new list with p elements
for i in p-1 ... 0:#从最后一个维度开始检查 ,
A_dim_i = A.shape[i]
B_dim_i = B.shape[i]
if A_dim_i != 1 and B_dim_i != 1 and A_dim_i != B_dim_i:#不符合广播条件
raise ValueError("could not broadcast")
else:
result_dims[i] = max(A_dim_i, B_dim_i)
2.两种情况
1.数组维度不同,后缘维度的轴长相符
2.数组维度相同,其中有个轴为1
https://www.cnblogs.com/jiaxin359/p/9021726.html