Skip to content

BUG: Implemented MaskedArray.dot #5709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions numpy/ma/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4522,6 +4522,26 @@ def trace(self, offset=0, axis1=0, axis2=1, dtype=None, out=None):
return D.astype(dtype).filled(0).sum(axis=None, out=out)
trace.__doc__ = ndarray.trace.__doc__

def dot(self, other, out=None):
am = ~getmaskarray(self)
bm = ~getmaskarray(other)
if out is None:
d = np.dot(filled(self, 0), filled(other, 0))
m = ~np.dot(am, bm)
if d.ndim == 0:
d = np.asarray(d)
r = d.view(get_masked_subclass(self, other))
r.__setmask__(m)
return r
d = self.filled(0).dot(other.filled(0), out._data)
if out.mask.shape != d.shape:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to test if out is a maskedarray (other functions do this):

        if isinstance(out, MaskedArray):
           ...

out._mask = numpy.empty(d.shape, MaskType)
np.dot(am, bm, out._mask)
np.logical_not(out._mask, out._mask)
return out
dot.__doc__ = ndarray.dot.__doc__
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried the doc might get out of sync, but I think you've thought about that more than I have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not thought much about this, but what's good for trace above is good for dot. And if doc gets out of sync we will have a bigger problem because that would mean that the code got out of sync as well.

What I would like to add, though is a section explaining mask handling.



def sum(self, axis=None, dtype=None, out=None):
"""
Return the sum of the array elements over the given axis.
Expand Down
8 changes: 1 addition & 7 deletions numpy/ma/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,13 +1047,7 @@ def dot(a, b, strict=False):
if strict and (a.ndim == 2) and (b.ndim == 2):
a = mask_rows(a)
b = mask_cols(b)
#
d = np.dot(filled(a, 0), filled(b, 0))
#
am = (~getmaskarray(a))
bm = (~getmaskarray(b))
m = ~np.dot(am, bm)
return masked_array(d, mask=m)
return a.dot(b)

#####--------------------------------------------------------------------------
#---- --- arraysetops ---
Expand Down
24 changes: 24 additions & 0 deletions numpy/ma/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,30 @@ def test_trace(self):
X.trace() - sum(mXdiag.mask * X.diagonal(),
axis=0))

def test_dot(self):
# Tests dot on MaskedArrays.
(x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
fx = mx.filled(0)
r = mx.dot(mx)
assert_almost_equal(r.filled(0), fx.dot(fx))
assert_(r.mask is nomask)

fX = mX.filled(0)
r = mX.dot(mX)
assert_almost_equal(r.filled(0), fX.dot(fX))
assert_(r.mask[1,3])
r1 = empty_like(r)
mX.dot(mX, r1)
assert_almost_equal(r, r1)

mYY = mXX.swapaxes(-1, -2)
fXX, fYY = mXX.filled(0), mYY.filled(0)
r = mXX.dot(mYY)
assert_almost_equal(r.filled(0), fXX.dot(fYY))
r1 = empty_like(r)
mXX.dot(mYY, r1)
assert_almost_equal(r, r1)

def test_varstd(self):
# Tests var & std on MaskedArrays.
(x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
Expand Down