From 718b1abfd1d6367c3ab66fc135dd3712dc8a8435 Mon Sep 17 00:00:00 2001 From: Amit Subhash Chejara <125737375+amitsubhashchejara@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:38:14 +0530 Subject: [PATCH] Update documentation in _linalg.py for np.linalg.det() to clarify floating-point precision This update modifies the documentation in _linalg.py for the np.linalg.det() function to clarify the effects of floating-point precision limitations when calculating the determinant of a matrix. It includes an explanation that small numerical errors (i.e. floating point rounding error) may occur, leading to very small non-zero values instead of exactly zero for singular matrices. Additionally, an example is provided using np.isclose() to check if the determinant is effectively zero. --- numpy/linalg/_linalg.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/numpy/linalg/_linalg.py b/numpy/linalg/_linalg.py index 70698c92fde7..932aef10f7d4 100644 --- a/numpy/linalg/_linalg.py +++ b/numpy/linalg/_linalg.py @@ -2357,6 +2357,12 @@ def det(a): The determinant is computed via LU factorization using the LAPACK routine ``z/dgetrf``. + Due to the limitations of floating-point arithmetic, small numerical + errors may occur when calculating the determinant of a matrix. As a result, + the determinant may return very small non-zero values instead of exactly zero + for singular matrices. To check if a determinant is effectively zero, use + `np.isclose()`. + Examples -------- The determinant of a 2-D array [[a, b], [c, d]] is ad - bc: @@ -2374,6 +2380,13 @@ def det(a): >>> np.linalg.det(a) array([-2., -3., -8.]) + Checking if the determinant is effectively zero for singular matrices: + + >>> a = np.array([[5, 5, 6], [7, 7, 5], [4, 4, 8]]) + >>> det = np.linalg.det(a) + >>> np.isclose(det, 0) + True + """ a = asarray(a) _assert_stacked_2d(a)