Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: Fix linalg.norm to handle empty matrices correctly. #28343

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 6 commits into from
Mar 12, 2025
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 doc/release/upcoming_changes/28343.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* The vector norm ``ord=inf`` and the matrix norms ``ord={1, 2, inf, 'nuc'}`` now always returns zero for empty arrays. Empty arrays have at least one axis of size zero. This affects `np.linalg.norm`, `np.linalg.vector_norm`, and `np.linalg.matrix_norm`. Previously, NumPy would raises errors or return zero depending on the shape of the array.
14 changes: 7 additions & 7 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ def lstsq(a, b, rcond=None):
return wrap(x), wrap(resids), rank, s


def _multi_svd_norm(x, row_axis, col_axis, op):
def _multi_svd_norm(x, row_axis, col_axis, op, initial=None):
"""Compute a function of the singular values of the 2-D matrices in `x`.

This is a private utility function used by `numpy.linalg.norm()`.
Expand All @@ -2565,7 +2565,7 @@ def _multi_svd_norm(x, row_axis, col_axis, op):

"""
y = moveaxis(x, (row_axis, col_axis), (-2, -1))
result = op(svd(y, compute_uv=False), axis=-1)
result = op(svd(y, compute_uv=False), axis=-1, initial=initial)
return result


Expand Down Expand Up @@ -2763,7 +2763,7 @@ def norm(x, ord=None, axis=None, keepdims=False):

if len(axis) == 1:
if ord == inf:
return abs(x).max(axis=axis, keepdims=keepdims)
return abs(x).max(axis=axis, keepdims=keepdims, initial=0)
elif ord == -inf:
return abs(x).min(axis=axis, keepdims=keepdims)
elif ord == 0:
Expand Down Expand Up @@ -2797,17 +2797,17 @@ def norm(x, ord=None, axis=None, keepdims=False):
if row_axis == col_axis:
raise ValueError('Duplicate axes given.')
if ord == 2:
ret = _multi_svd_norm(x, row_axis, col_axis, amax)
ret = _multi_svd_norm(x, row_axis, col_axis, amax, 0)
elif ord == -2:
ret = _multi_svd_norm(x, row_axis, col_axis, amin)
elif ord == 1:
if col_axis > row_axis:
col_axis -= 1
ret = add.reduce(abs(x), axis=row_axis).max(axis=col_axis)
ret = add.reduce(abs(x), axis=row_axis).max(axis=col_axis, initial=0)
elif ord == inf:
if row_axis > col_axis:
row_axis -= 1
ret = add.reduce(abs(x), axis=col_axis).max(axis=row_axis)
ret = add.reduce(abs(x), axis=col_axis).max(axis=row_axis, initial=0)
elif ord == -1:
if col_axis > row_axis:
col_axis -= 1
Expand All @@ -2819,7 +2819,7 @@ def norm(x, ord=None, axis=None, keepdims=False):
elif ord in [None, 'fro', 'f']:
ret = sqrt(add.reduce((x.conj() * x).real, axis=axis))
elif ord == 'nuc':
ret = _multi_svd_norm(x, row_axis, col_axis, sum)
ret = _multi_svd_norm(x, row_axis, col_axis, sum, 0)
else:
raise ValueError("Invalid norm order for matrices.")
if keepdims:
Expand Down
18 changes: 18 additions & 0 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,16 @@ def test_matrix_norm():
assert_almost_equal(actual, np.array([[14.2828]]), double_decimal=3)


def test_matrix_norm_empty():
for shape in [(0, 2), (2, 0), (0, 0)]:
for dtype in [np.float64, np.float32, np.int32]:
x = np.zeros(shape, dtype)
assert_equal(np.linalg.matrix_norm(x, ord="fro"), 0)
assert_equal(np.linalg.matrix_norm(x, ord="nuc"), 0)
assert_equal(np.linalg.matrix_norm(x, ord=1), 0)
assert_equal(np.linalg.matrix_norm(x, ord=2), 0)
assert_equal(np.linalg.matrix_norm(x, ord=np.inf), 0)

def test_vector_norm():
x = np.arange(9).reshape((3, 3))
actual = np.linalg.vector_norm(x)
Expand All @@ -2388,3 +2398,11 @@ def test_vector_norm():
expected = np.full((1, 1), 14.2828, dtype='float64')
assert_equal(actual.shape, expected.shape)
assert_almost_equal(actual, expected, double_decimal=3)


def test_vector_norm_empty():
for dtype in [np.float64, np.float32, np.int32]:
x = np.zeros(0, dtype)
assert_equal(np.linalg.vector_norm(x, ord=1), 0)
assert_equal(np.linalg.vector_norm(x, ord=2), 0)
assert_equal(np.linalg.vector_norm(x, ord=np.inf), 0)
Loading