[Numpy-discussion] bug in oldnumeric.ma
Eric Firing
efiring at hawaii.edu
Wed May 7 14:15:59 EDT 2008
Charles Doutriaux wrote:
> The following code works with numpy.ma but not numpy.oldnumeric.ma,
No, this is a bug in numpy.ma also; power is broken:
In [1]:import numpy as np
In [2]:x = np.ma.array([-1.1])
In [3]:x**2.0 ### This works
Out[3]:
masked_array(data = [1.21],
mask = [False],
fill_value=1e+20)
In [4]:np.ma.power(x, 2.0) ### This doesn't
Out[4]:
masked_array(data = [--],
mask = [ True],
fill_value=1e+20)
Here is the code in ma/core.py, which is masking the output for negative
inputs:
def power(a, b, third=None):
"""Computes a**b elementwise.
Masked values are set to 1.
"""
if third is not None:
raise MAError, "3-argument power not supported."
ma = getmask(a)
mb = getmask(b)
m = mask_or(ma, mb)
fa = getdata(a)
fb = getdata(b)
if fb.dtype.char in typecodes["Integer"]:
return masked_array(umath.power(fa, fb), m)
md = make_mask((fa < 0), shrink=True) #### wrong
m = mask_or(m, md)
if m is nomask:
return masked_array(umath.power(fa, fb))
else:
fa = fa.copy()
fa[(fa < 0)] = 1 #### wrong
return masked_array(umath.power(fa, fb), m)
Eric
More information about the NumPy-Discussion
mailing list