Skip to content

BUG: Always return a real dtype from linalg.cond (gh-18304) #29333

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -2011,6 +2011,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)
Copy link
Member

Choose a reason for hiding this comment

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

So, I think this is fine. Your other comment (deleted now) is not wrong, in that it would be fine to cast invx to the original result_t to achieve the right thing.
(Norm might in some paths end up casting back and forth then, but it would work too.)

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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy