diff options
author | pierregm <pierregm@localhost> | 2008-05-12 17:09:08 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2008-05-12 17:09:08 +0000 |
commit | 45c812ddb75c5efc6bbcb4b099792f2763db11fe (patch) | |
tree | 8d0585b0489838f41a4d35112135f60694ad0900 | |
parent | c2b77e022e8393f057b5b54dc595f35b01a26809 (diff) | |
download | numpy-45c812ddb75c5efc6bbcb4b099792f2763db11fe.tar.gz |
power : fixed a bug when a scalar is the first argument
MaskedArray.__pow__ : call power
MaskedArray.__ipow__: works in place. Note that the _data part gets "fixed" (NaNs/Infs set to fill_value)
-rw-r--r-- | numpy/ma/core.py | 29 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 13 |
2 files changed, 35 insertions, 7 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index b1a06fc8d..8e1e71e0a 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -1685,7 +1685,7 @@ masked_%(name)s(data = %(data)s, return multiply(self, other) # def __div__(self, other): - "Divides other into self, and return a new masked array." + "Divide other into self, and return a new masked array." return divide(self, other) # def __truediv__(self, other): @@ -1695,7 +1695,10 @@ masked_%(name)s(data = %(data)s, def __floordiv__(self, other): "Divide other into self, and return a new masked array." return floor_divide(self, other) - + # + def __pow__(self, other): + "Raise self to the power other, masking the potential NaNs/Infs" + return power(self, other) #............................................ def __iadd__(self, other): "Add other to self in-place." @@ -1740,6 +1743,19 @@ masked_%(name)s(data = %(data)s, ndarray.__idiv__(self._data, other_data) self._mask = mask_or(self._mask, new_mask) return self + #... + def __ipow__(self, other): + "Raise self to the power other, in place" + _data = self._data + other_data = getdata(other) + other_mask = getmask(other) + ndarray.__ipow__(_data, other_data) + invalid = numpy.logical_not(numpy.isfinite(_data)) + new_mask = mask_or(other_mask,invalid) + self._mask = mask_or(self._mask, new_mask) + # The following line is potentially problematic, as we change _data... + numpy.putmask(self._data,invalid,self.fill_value) + return self #............................................ def __float__(self): "Convert to float." @@ -2859,15 +2875,20 @@ def power(a, b, third=None): basetype = MaskedArray # Get the result and view it as a (subclass of) MaskedArray result = umath.power(fa,fb).view(basetype) - # Retrieve some extra attributes if needed - result._update_from(a) # Find where we're in trouble w/ NaNs and Infs invalid = numpy.logical_not(numpy.isfinite(result.view(ndarray))) + # Retrieve some extra attributes if needed + if isinstance(result,MaskedArray): + result._update_from(a) # Add the initial mask if m is not nomask: + if numpy.isscalar(result): + return masked result._mask = m # Fix the invalid parts if invalid.any(): + if not result.ndim: + return masked result[invalid] = masked result._data[invalid] = result.fill_value return result diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index b04c1c2ab..f09dcdd94 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1565,16 +1565,23 @@ class TestMiscFunctions(NumpyTestCase): def test_power(self): x = -1.1 assert_almost_equal(power(x,2.), 1.21) - assert_equal(power(x,0.5)._mask, 1) - assert_equal(power(x,masked)._mask, 1) + assert(power(x,masked) is masked) x = array([-1.1,-1.1,1.1,1.1,0.]) - b = array([0.5,2.,0.5,2.,1.], mask=[0,0,0,0,1]) + b = array([0.5,2.,0.5,2.,-1.], mask=[0,0,0,0,1]) y = power(x,b) assert_almost_equal(y, [0, 1.21, 1.04880884817, 1.21, 0.] ) assert_equal(y._mask, [1,0,0,0,1]) b.mask = nomask y = power(x,b) assert_equal(y._mask, [1,0,0,0,1]) + z = x**b + assert_equal(z._mask, y._mask) + assert_almost_equal(z,y) + assert_almost_equal(z._data,y._data) + x **= b + assert_equal(x._mask, y._mask) + assert_almost_equal(x,y) + assert_almost_equal(x._data,y._data) |