Here is a fun one...
import numpy as np
a_2d = np.random.random((3, 5))
b_1d = np.random.random(5)
b_2d = np.vstack((b_1d, b_1d, b_1d))
a_ma_2d = np.ma.masked_array(a_2d, mask=(numpy.random.random((3, 5)) <
0.25))
b_ma_1d = np.ma.masked_array(b_1d, mask=(numpy.random.random(5) < 0.25))
b_ma_2d = np.ma.masked_array(b_2d, mask=(numpy.random.random((3, 5)) <
0.25))
# a**b without broadcasting (works)
print a_2d**b_2d
# a**b with broadcasting (works)
print a_2d**b_1d
# a_ma**b_ma without broadcasting (works)
print a_ma_2d**b_ma_2d
# a_ma**b_ma with broadcasting (works)
print a_ma_2d**b_ma_1d
# a**b_ma without broadcasting (works)
print a_2d**b_ma_2d
# a**b_ma with broadcasting (FAILS)
print a_2d**b_ma_1d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bvr/Programs/numpy/numpy/ma/core.py", line 3697, in __rpow__
return power(other, self)
File "/home/bvr/Programs/numpy/numpy/ma/core.py", line 6043, in power
m |= invalid
ValueError: invalid return array shape
# Now, test broadcasting of the other side of the operator
c = np.random.random(5)
c_ma = np.ma.masked_array(c, mask=(np.random.random(10) < 0.25))
# c**b_ma with broadcasting (works)
print c**b_ma_2d
# c_ma**b with broadcasting (FAILS)
print c_ma**b_2d
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/bvr/Programs/numpy/numpy/ma/core.py", line 3693, in __pow__
return power(self, other)
File "/home/bvr/Programs/numpy/numpy/ma/core.py", line 6043, in power
m |= invalid
ValueError: invalid return array shape
So, this fails if we broadcast the masked array portion of a power
expression and the other value is a ndarray.
Note, the same also occurs if we broadcast a masked array in order to raise
it to powers indicated by an ndarray.
Took me the longest time to nail this one down...
Ben Root