Checking if two arrays are broadcastable in python
Here's a concise expression to check if two arrays are broadcastable:
In [101]: import numpy as np
In [102]: a = np.zeros((3, 1, 5))
In [103]: b = np.zeros((4, 5))
In [104]: all((m == n) or (m == 1) or (n == 1) for m, n in zip(a.shape[::-1], b.shape[::-1]))
Out[104]: True
In [105]: b = np.zeros((5, 3))
In [106]: all((m == n) or (m == 1) or (n == 1) for m, n in zip(a.shape[::-1], b.shape[::-1]))
Out[106]: False