Skip to content
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
1 change: 1 addition & 0 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,7 @@ def cond(x, p=None):
# contain nans in the entries where inversion failed.
_assert_stacked_square(x)
t, result_t = _commonType(x)
result_t = _realType(result_t) # condition number is always real
signature = 'D->D' if isComplexType(t) else 'd->d'
with errstate(all='ignore'):
invx = _umath_linalg.inv(x, signature=signature)
Expand Down
15 changes: 14 additions & 1 deletion numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,15 +793,28 @@ def do(self, a, b, tags):


class TestCond(CondCases):
def test_basic_nonsvd(self):
@pytest.mark.parametrize('is_complex', [False, True])
def test_basic_nonsvd(self, is_complex):
# Smoketest the non-svd norms
A = array([[1., 0, 1], [0, -2., 0], [0, 0, 3.]])
if is_complex:
# Since A is linearly scaled, the condition number should not change
A = A * (1 + 1j)
assert_almost_equal(linalg.cond(A, inf), 4)
assert_almost_equal(linalg.cond(A, -inf), 2 / 3)
assert_almost_equal(linalg.cond(A, 1), 4)
assert_almost_equal(linalg.cond(A, -1), 0.5)
assert_almost_equal(linalg.cond(A, 'fro'), np.sqrt(265 / 12))

@pytest.mark.parametrize('dtype', [single, double, csingle, cdouble])
@pytest.mark.parametrize('norm_ord', [1, -1, 2, -2, 'fro', np.inf, -np.inf])
def test_cond_dtypes(self, dtype, norm_ord):
# Check that the condition number is computed in the same dtype
# as the input matrix
A = array([[1., 0, 1], [0, -2., 0], [0, 0, 3.]], dtype=dtype)
out_type = get_real_dtype(dtype)
assert_equal(linalg.cond(A, p=norm_ord).dtype, out_type)

def test_singular(self):
# Singular matrices have infinite condition number for
# positive norms, and negative norms shouldn't raise
Expand Down
Loading