Sitelet https://github.com/numpy/numpy/issues/30915
Skip to content

BUG: numpy.linalg.matrix_rank crashes with ValueError on empty matrices [DeepTest] #30915

Description

@madanMus

Describe the issue:

numpy.linalg.matrix_rank raises a ValueError when given a matrix with a zero-sized dimension (e.g., shape (0, 3)). The rank of an empty matrix is mathematically 0, and other linalg functions (pinv, cond) already handle
this via the _is_empty_2d guard — matrix_rank is missing it.

Reproduce the code example:

import numpy as np

   a = np.empty((0, 3))
   np.linalg.matrix_rank(a)  # Expected: 0

Error message:

Traceback (most recent call last):
  File "C:\Users\madanm\git\DeepTest\repo_benchmarks\numpy-DeepTests\standalone\crash_v2\score5\linalg_matrix_rank_empty_matrix.py", line 24, in <module>
    rank = np.linalg.matrix_rank(a)
  File "C:\Users\madanm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\numpy\linalg\_linalg.py", line 2165, in matrix_rank
    tol = S.max(axis=-1, keepdims=True) * rtol
          ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\madanm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\numpy\_core\_methods.py", line 43, in _amax
    return umr_maximum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation maximum which has no identity

Python and NumPy Versions:

2.3.5
3.13.12 (tags/v3.13.12:1cbe481, Feb 3 2026, 18:22:25) [MSC v.1944 64 bit (AMD64)]

Runtime Environment:

No response

How does this issue affect you or how did you find it:

Using DeepTest

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - BugsprintableIssue fits the time-frame and setting of a sprint

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions