diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2013-04-01 19:13:45 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2013-04-01 19:30:15 -0600 |
commit | d0b6a7a48e4662b3c2788d541b06ad5d46a2f177 (patch) | |
tree | fe931716fec71a895b7ddb21033f7cce74542408 | |
parent | e9dfb3bc760088fa50e14aed363041a7aac6aa29 (diff) | |
download | numpy-d0b6a7a48e4662b3c2788d541b06ad5d46a2f177.tar.gz |
TST: Add more tests for qr factorization.
The new tests cover the new modes 'complete' and 'raw'. The testing of
the 'reduced', aka 'full' mode is improved and tests are added for the
deprecation of the 'full' and 'economic' modes. A new file
`numpy/linalg/tests/test_deprecations.py` was added for the deprecation
tests.
-rw-r--r-- | numpy/linalg/tests/test_deprecations.py | 24 | ||||
-rw-r--r-- | numpy/linalg/tests/test_linalg.py | 104 |
2 files changed, 128 insertions, 0 deletions
diff --git a/numpy/linalg/tests/test_deprecations.py b/numpy/linalg/tests/test_deprecations.py new file mode 100644 index 000000000..13d244199 --- /dev/null +++ b/numpy/linalg/tests/test_deprecations.py @@ -0,0 +1,24 @@ +"""Test deprecation and future warnings. + +""" +import numpy as np +from numpy.testing import assert_warns, run_module_suite + + +def test_qr_mode_full_future_warning(): + """Check mode='full' FutureWarning. + + In numpy 1.8 the mode options 'full' and 'economic' in linalg.qr were + deprecated. The release date will probably be sometime in the summer + of 2013. + + """ + a = np.eye(2) + assert_warns(DeprecationWarning, np.linalg.qr, a, mode='full') + assert_warns(DeprecationWarning, np.linalg.qr, a, mode='f') + assert_warns(DeprecationWarning, np.linalg.qr, a, mode='economic') + assert_warns(DeprecationWarning, np.linalg.qr, a, mode='e') + + +if __name__ == "__main__": + run_module_suite() diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index fb8f89fe9..a6283682c 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -469,11 +469,115 @@ def test_reduced_rank(): class TestQR(TestCase): + + + def check_qr(self, a): + # This test expects the argument `a` to be an ndarray or + # a subclass of an ndarray of inexact type. + a_type = type(a) + a_dtype = a.dtype + m, n = a.shape + k = min(m, n) + + # mode == 'complete' + q, r = linalg.qr(a, mode='complete') + assert_(q.dtype == a_dtype) + assert_(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_almost_equal(dot(q, r), a) + assert_almost_equal(dot(q.T.conj(), q), np.eye(m)) + assert_almost_equal(np.triu(r), r) + + + # mode == 'reduced' + q1, r1 = linalg.qr(a, mode='reduced') + assert_(q1.dtype == a_dtype) + assert_(r1.dtype == a_dtype) + assert_(isinstance(q1, a_type)) + assert_(isinstance(r1, a_type)) + assert_(q1.shape == (m, k)) + assert_(r1.shape == (k, n)) + assert_almost_equal(dot(q1, r1), a) + assert_almost_equal(dot(q1.T.conj(), q1), np.eye(k)) + assert_almost_equal(np.triu(r1), r1) + + # mode == 'r' + r2 = linalg.qr(a, mode='r') + assert_(r2.dtype == a_dtype) + assert_(isinstance(r2, a_type)) + assert_almost_equal(r2, r1) + + + def test_qr_empty(self): a = np.zeros((0,2)) self.assertRaises(linalg.LinAlgError, linalg.qr, a) + def test_mode_raw(self): + a = array([[1, 2], [3, 4], [5, 6]], dtype=np.double) + b = a.astype(np.single) + + # m > n + h1, tau1 = ( + array([[-5.91607978, 0.43377175, 0.72295291], + [-7.43735744, 0.82807867, 0.89262383]]), + array([ 1.16903085, 1.113104 ]) + ) + # m > n + h2, tau2 = ( + array([[-2.23606798, 0.61803399], + [-4.91934955, -0.89442719], + [-7.60263112, -1.78885438]]), + array([ 1.4472136, 0. ]) + ) + + # Test double + h, tau = linalg.qr(a, mode='raw') + assert_(h.dtype == np.double) + assert_(tau.dtype == np.double) + old_assert_almost_equal(h, h1, decimal=8) + old_assert_almost_equal(tau, tau1, decimal=8) + + h, tau = linalg.qr(a.T, mode='raw') + assert_(h.dtype == np.double) + assert_(tau.dtype == np.double) + old_assert_almost_equal(h, h2, decimal=8) + old_assert_almost_equal(tau, tau2, decimal=8) + + # Test single + h, tau = linalg.qr(b, mode='raw') + assert_(h.dtype == np.double) + assert_(tau.dtype == np.double) + old_assert_almost_equal(h, h1, decimal=8) + old_assert_almost_equal(tau, tau1, decimal=8) + + + def test_mode_all_but_economic(self): + a = array([[1, 2], [3, 4]]) + b = array([[1, 2], [3, 4], [5, 6]]) + for dt in "fd": + m1 = a.astype(dt) + m2 = b.astype(dt) + self.check_qr(m1) + self.check_qr(m2) + self.check_qr(m2.T) + self.check_qr(matrix(m1)) + for dt in "fd": + m1 = 1 + 1j * a.astype(dt) + m2 = 1 + 1j * b.astype(dt) + self.check_qr(m1) + self.check_qr(m2) + self.check_qr(m2.T) + self.check_qr(matrix(m1)) + + + + + def test_byteorder_check(): # Byte order check should pass for native order if sys.byteorder == 'little': |