diff options
Diffstat (limited to 'numpy/linalg/tests/test_linalg.py')
-rw-r--r-- | numpy/linalg/tests/test_linalg.py | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index c612eb6bb..8b3984883 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -712,12 +712,16 @@ class TestCondInf(object): assert_almost_equal(linalg.cond(A, inf), 3.) -class TestPinv(LinalgSquareTestCase, LinalgNonsquareTestCase): +class TestPinv(LinalgSquareTestCase, + LinalgNonsquareTestCase, + LinalgGeneralizedSquareTestCase, + LinalgGeneralizedNonsquareTestCase): def do(self, a, b, tags): a_ginv = linalg.pinv(a) # `a @ a_ginv == I` does not hold if a is singular - assert_almost_equal(dot(a, a_ginv).dot(a), a, single_decimal=5, double_decimal=11) + dot = dot_generalized + assert_almost_equal(dot(dot(a, a_ginv), a), a, single_decimal=5, double_decimal=11) assert_(imply(isinstance(a, matrix), isinstance(a_ginv, matrix))) @@ -793,7 +797,7 @@ class TestLstsq(LinalgSquareTestCase, LinalgNonsquareTestCase): arr = np.asarray(a) m, n = arr.shape u, s, vt = linalg.svd(a, 0) - x, residuals, rank, sv = linalg.lstsq(a, b) + x, residuals, rank, sv = linalg.lstsq(a, b, rcond=-1) if m <= n: assert_almost_equal(b, dot(a, x)) assert_equal(rank, m) @@ -814,6 +818,23 @@ class TestLstsq(LinalgSquareTestCase, LinalgNonsquareTestCase): assert_(imply(isinstance(b, matrix), isinstance(x, matrix))) assert_(imply(isinstance(b, matrix), isinstance(residuals, matrix))) + def test_future_rcond(self): + a = np.array([[0., 1., 0., 1., 2., 0.], + [0., 2., 0., 0., 1., 0.], + [1., 0., 1., 0., 0., 4.], + [0., 0., 0., 2., 3., 0.]]).T + + b = np.array([1, 0, 0, 0, 0, 0]) + with suppress_warnings() as sup: + w = sup.record(FutureWarning, "`rcond` parameter will change") + x, residuals, rank, s = linalg.lstsq(a, b) + assert_(rank == 4) + x, residuals, rank, s = linalg.lstsq(a, b, rcond=-1) + assert_(rank == 4) + x, residuals, rank, s = linalg.lstsq(a, b, rcond=None) + assert_(rank == 3) + # Warning should be raised exactly once (first command) + assert_(len(w) == 1) class TestMatrixPower(object): R90 = array([[0, 1], [-1, 0]]) @@ -1362,6 +1383,19 @@ class TestMatrixRank(object): # works on scalar yield assert_equal, matrix_rank(1), 1 + def test_symmetric_rank(self): + yield assert_equal, 4, matrix_rank(np.eye(4), hermitian=True) + yield assert_equal, 1, matrix_rank(np.ones((4, 4)), hermitian=True) + yield assert_equal, 0, matrix_rank(np.zeros((4, 4)), hermitian=True) + # rank deficient matrix + I = np.eye(4) + I[-1, -1] = 0. + yield assert_equal, 3, matrix_rank(I, hermitian=True) + # manually supplied tolerance + I[-1, -1] = 1e-8 + yield assert_equal, 4, matrix_rank(I, hermitian=True, tol=0.99e-8) + yield assert_equal, 3, matrix_rank(I, hermitian=True, tol=1.01e-8) + def test_reduced_rank(): # Test matrices with reduced rank @@ -1550,7 +1584,7 @@ def test_xerbla_override(): np.linalg.lapack_lite.xerbla() except ValueError: pass - except: + except Exception: os._exit(os.EX_CONFIG) try: @@ -1645,7 +1679,7 @@ class TestMultiDot(object): [0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 4, 5], [0, 0, 0, 0, 0, 5], - [0, 0, 0, 0, 0, 0]], dtype=np.int) + [0, 0, 0, 0, 0, 0]], dtype=int) s_expected -= 1 # Cormen uses 1-based index, python does not. s, m = _multi_dot_matrix_chain_order(arrays, return_costs=True) |