What is the best way to test whether an array can be broadcast to a given shape?
The \"pythonic\" approach of try
ing doesn\'t work for my case, because the
You could use np.broadcast. For example:
In [47]: x = np.ones([2,2,2])
In [48]: y = np.ones([2,3])
In [49]: try:
....: b = np.broadcast(x, y)
....: print "Result has shape", b.shape
....: except ValueError:
....: print "Not compatible for broadcasting"
....:
Not compatible for broadcasting
In [50]: y = np.ones([2,2])
In [51]: try:
....: b = np.broadcast(x, y)
....: print "Result has shape", b.shape
....: except ValueError:
....: print "Not compatible for broadcasting"
....:
Result has shape (2, 2, 2)
For your implementation of lazy evaluation, you might also find np.broadcast_arrays useful.