Describe the issue:
Calling np.linalg.matrix_rank on empty matrices (0 rows or 0 columns) raises a ValueError.
This prevents correct handling of empty matrices and fails in workflows where empty matrices are valid inputs.
Reproduce the code example:
import numpy as np
# Empty matrix
A = np.zeros((0, 5))
print(f"Shape: {A.shape}, Rank: {np.linalg.matrix_rank(A)}")
Error message:
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
print(f"Shape: {A.shape}, Rank: {np.linalg.matrix_rank(A)}")
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\linalg\_linalg.py", line 2115, in matrix_rank
tol = S.max(axis=-1, keepdims=True) * rtol
File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\_core\_methods.py", line 44, 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:
NumPy version: 2.3.5
Python version: 3.12.12
Runtime Environment:
[[
{
"numpy_version": "2.3.5",
"python": "3.12.12 (conda-forge)",
"uname": {
"system": "Linux",
"machine": "x86_64"
}
},
{
"simd_extensions": {
"baseline": ["SSE", "SSE2", "SSE3"],
"found": ["SSSE3", "SSE41", "SSE42", "AVX", "AVX2"],
"not_found": ["AVX512"]
}
},
{
"ignore_floating_point_errors_in_matmul": false
},
{
"internal_api": "openblas",
"threading_layer": "pthreads"
}
]
Context for the issue:
numpy.linalg.matrix_rank currently fails to return 0 for empty matrices in some edge cases. This inconsistency can break workflows in linear algebra, data analysis, or machine learning pipelines, where empty matrices may appear dynamically. It forces developers to add extra checks or handle unexpected errors, increasing code complexity and the risk of bugs.
Ensuring matrix_rank consistently returns 0 for empty matrices will improve correctness, reliability, and robustness, making NumPy safer and more predictable for all scientific computing use cases.
Describe the issue:
Calling
np.linalg.matrix_rankon empty matrices (0 rows or 0 columns) raises a ValueError.This prevents correct handling of empty matrices and fails in workflows where empty matrices are valid inputs.
Reproduce the code example:
Error message:
Python and NumPy Versions:
NumPy version: 2.3.5
Python version: 3.12.12
Runtime Environment:
[[
{
"numpy_version": "2.3.5",
"python": "3.12.12 (conda-forge)",
"uname": {
"system": "Linux",
"machine": "x86_64"
}
},
{
"simd_extensions": {
"baseline": ["SSE", "SSE2", "SSE3"],
"found": ["SSSE3", "SSE41", "SSE42", "AVX", "AVX2"],
"not_found": ["AVX512"]
}
},
{
"ignore_floating_point_errors_in_matmul": false
},
{
"internal_api": "openblas",
"threading_layer": "pthreads"
}
]
Context for the issue:
numpy.linalg.matrix_rank currently fails to return 0 for empty matrices in some edge cases. This inconsistency can break workflows in linear algebra, data analysis, or machine learning pipelines, where empty matrices may appear dynamically. It forces developers to add extra checks or handle unexpected errors, increasing code complexity and the risk of bugs.
Ensuring matrix_rank consistently returns 0 for empty matrices will improve correctness, reliability, and robustness, making NumPy safer and more predictable for all scientific computing use cases.