8000 ENH: handle empty matrices in qr decomposition by convexset · Pull Request #11593 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: handle empty matrices in qr decomposition #11593

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 12 commits into from
Jul 31, 2018
Merged
Prev Previous commit
Next Next commit
updated tests
  • Loading branch information
convexset committed Jul 29, 2018
commit 7f2273674ed8b0082c6da12d15ce23151a1056f2
44 changes: 19 additions & 25 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,45 +1583,39 @@ def check_qr(self, a):
assert_almost_equal(r2, r1)

def test_qr_empty(self):
for m, n in [(3, 0), (0, 3)]:
for m, n in [(0, 0), (3, 0), (0, 3)]:
k = min(m, n)
a = np.empty((m, n))
a_type = type(a)
a_dtype = a.dtype
#

q, r = np.linalg.qr(a, mode='reduced')
assert_(q.dtype == a_dtype)
assert_(r.dtype == a_dtype)
assert_equal(q.dtype, a_dtype)
assert_equal(r.dtype, a_dtype)
assert_(isinstance(q, a_type))
assert_(isinstance(r, a_type))
assert_(q.shape == (m, k))
assert_(r.shape == (k, n))
assert_almost_equal(np.dot(q, r), a)
assert_almost_equal(np.dot(q.T.conj(), q), np.eye(k))
assert_almost_equal(np.triu(r), r)
#
assert_equal(q.shape, (m, k))
assert_equal(r.shape, (k, n))

r = np.linalg.qr(a, mode='r')
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this already covered by check_qr?

assert_(r.dtype == a_dtype)
assert_equal(r.dtype, a_dtype)
assert_(isinstance(r, a_type))
assert_(r.shape == (k, n))
assert_almost_equal(np.triu(r), r)
#
assert_equal(r.shape, (k, n))
Copy link
Member

Choose a reason for hiding this comment

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

This shape test should probably be in check_qr anyway

Copy link
Contributor Author
@convexset convexset Jul 31, 2018

Choose a reason for hiding this comment

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

It's there implicitly.

        # mode == 'reduced'
        q1, r1 = linalg.qr(a, mode='reduced')
        # ...
        assert_(r1.shape == (k, n))
        # ...

        # mode == 'r'
        r2 = linalg.qr(a, mode='r')
        # ...
        assert_almost_equal(r2, r1)

So I took that bit out.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough. I think the explicit shape check you had here was a little clearer, but it's not super important.


q, r = np.linalg.qr(a, mode='complete')
assert_(q.dtype == a_dtype)
assert_(r.dtype == a_dtype)
assert_equal(q.dtype, a_dtype)
assert_equal(r.dtype, a_dtype)
assert_(isinstance(q, a_type))
assert_(isinstance(r, a_type))
assert_(q.shape == (m, m))
assert_(r.shape == (m, n))
assert_equal(q.shape, (m, m))
assert_equal(r.shape, (m, n))
assert_almost_equal(q, np.eye(m))
assert_almost_equal(np.dot(q, r), a)
assert_almost_equal(np.triu(r), r)
#

h, tau = np.linalg.qr(a, mode='raw')
Copy link
Member

Choose a reason for hiding this comment

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

This test can stay - for whatever reason, it seems check_qr decided not to test it

assert_(h.dtype == np.double)
assert_(tau.dtype == np.double)
assert_(h.shape == (n, m))
assert_(tau.shape == (k,))
assert_equal(h.dtype, np.double)
assert_equal(tau.dtype, np.double)
assert_equal(h.shape, (n, m))
assert_equal(tau.shape, (k,))

def test_mode_raw(self):
# The factorization is not unique and varies between libraries,
Expand Down
0