Numpy np.linalg.det()与我的计算结果不同

Numpy np.linalg.det()与我的计算结果不同,numpy,Numpy,我试图理解行列式是如何计算的。 我有: 现在,我是这样理解的: >>>>>>> array([[1, 2, 3], [2, 3, 4], [5, 6, 7]]) >>>>>>> |b| = |1 2 3| = 1|3 4| - 2|2 4| + 3|2 3| |2 3 4| |6 7| |5

我试图理解行列式是如何计算的。 我有:

现在,我是这样理解的:

>>>>>>> array([[1, 2, 3],
               [2, 3, 4],
               [5, 6, 7]])
>>>>>>> |b| = |1  2  3|   = 1|3  4| - 2|2  4| + 3|2  3|
              |2  3  4|      |6  7|    |5  7|    |5  6|
              |5  6  7|
        |b|               = 1*(3*7 - 4*6) - 2*(2*7 - 4*5) + 3*(2*6 - 3*5)
        |b|               = 1*(   - 3   ) - 2*(    - 6  ) + 3*(    - 3  )    
        |b|               =   (   - 3   ) -   (    - 12 )   + (    - 9  )
        |b|               =       -3               +12             - 9     = 0      
难道我没听懂吗

为什么
det()
返回
1.77
,而我的结果是
0

是否应该改为以下内容

>>>>>>> |b| = |1  2  5|   = 1|3  6| - 2|2  6| + 5|2  3|
              |2  3  6|      |4  7|    |3  7|    |3  4|
              |3  4  7|

上述注释的意思是
numpy
输出:

b = np.array([ [1,2,3], [2,3,4], [5,6,7] ], dtype=np.float64)
np.linalg.det(b)
>>> 1.7763568394002396e-15
可以大致看作
0.+/-舍入误差
其中
舍入误差
非常接近
np.finfo(np.float64).eps=2.22e-16
,对应于
float64
数据类型的机器精度。事实上,
np.linalg.det
不以解析方式计算行列式,而是依赖于使用
b
的LU分解的数值近似(如中所述)。我没有查看源文件以了解
numpy
到底做了什么,但它确实可以归结为:

from scipy.linalg import lu_factor
b_LU = lu_factor(b)

def lu_det(A_LU):

    # The determinant of a permutation matrix is (-1)**n where n is the number of permutations.
    # Recall a permutation matrix is a matrix with a one in each row, and column, and zeros everywhere else.
    # => if the elements of the diagonal are not 1, they will be zero, and then there has been a swap
    nswaps = len(A_LU[1]) - sum(A_LU[1]==np.arange(len(A_LU[1])))
    detP = (-1)**nswaps

    # The determinant of a triangular matrix is the product of the elements on the diagonal.
    detL = 1 # The unit diagonal elements of L are not stored.
    detU = np.prod(np.diag(A_LU[0]))
    # Matrix containing U in its upper triangle, and L in its lower triangle.

    # The determinant of a product of matrices is equal to the product of the determinant of the matrices.
    return detP * detL * detU

lu_det(b_LU)
>>> 4.4408920985006196e-17
这也不是零,但确实非常接近!如果您想要一个真零,您可能需要查看符号计算的
sympy
,它简单地给出:

import sympy as sp

sp.Matrix(b).det()
>>> 0

希望您现在能更清楚地理解。

以上评论的意思是
numpy
输出:

b = np.array([ [1,2,3], [2,3,4], [5,6,7] ], dtype=np.float64)
np.linalg.det(b)
>>> 1.7763568394002396e-15
可以大致看作
0.+/-舍入误差
其中
舍入误差
非常接近
np.finfo(np.float64).eps=2.22e-16
,对应于
float64
数据类型的机器精度。事实上,
np.linalg.det
不以解析方式计算行列式,而是依赖于使用
b
的LU分解的数值近似(如中所述)。我没有查看源文件以了解
numpy
到底做了什么,但它确实可以归结为:

from scipy.linalg import lu_factor
b_LU = lu_factor(b)

def lu_det(A_LU):

    # The determinant of a permutation matrix is (-1)**n where n is the number of permutations.
    # Recall a permutation matrix is a matrix with a one in each row, and column, and zeros everywhere else.
    # => if the elements of the diagonal are not 1, they will be zero, and then there has been a swap
    nswaps = len(A_LU[1]) - sum(A_LU[1]==np.arange(len(A_LU[1])))
    detP = (-1)**nswaps

    # The determinant of a triangular matrix is the product of the elements on the diagonal.
    detL = 1 # The unit diagonal elements of L are not stored.
    detU = np.prod(np.diag(A_LU[0]))
    # Matrix containing U in its upper triangle, and L in its lower triangle.

    # The determinant of a product of matrices is equal to the product of the determinant of the matrices.
    return detP * detL * detU

lu_det(b_LU)
>>> 4.4408920985006196e-17
这也不是零,但确实非常接近!如果您想要一个真零,您可能需要查看符号计算的
sympy
,它简单地给出:

import sympy as sp

sp.Matrix(b).det()
>>> 0

希望现在对你来说更清楚。

数值方法的首要原则之一是,永远不要严格使用
=
比较两个浮点数。这里您要问的是为什么
1.7763568394002396e-15
不等于
0.0
。您的问题假设它们是“相等的”,即您在参数中隐式使用了
=
。 在没有公差值的情况下,绝对不能比较两个浮点数(即
numpy
上下文中的任何数值)

你问题中的
1.77
实际上是 在托兰斯: ,使用公差值,如
.

数值方法的首要原则之一是,永远不要严格使用
==
比较两个浮点数。这里您要问的是为什么
1.7763568394002396e-15
不等于
0.0
。您的问题假设它们是“相等的”,即您在参数中隐式使用了
=
。 在没有公差值的情况下,绝对不能比较两个浮点数(即
numpy
上下文中的任何数值)

你问题中的
1.77
实际上是 在托兰斯: ,使用公差值,如
.

1.7763568394002396e-15
似乎非常接近
float64
机器精度,不是吗?看看什么都是正确的:numpy resurns 1.7763568394002396e-15实际上是零,而不是1.77!您忽略了e-15。谢谢@Yacola。两种手动方法都是正确的,并产生相同的结果。要得到准确的结果,您可以使用
sympy.Matrix([[1,2,3],[2,3,4],[5,6,7]])。det()
1.7763568394002396e-15
看起来非常接近
float64
机器精度,不是吗?看看什么都是正确的:numpy resurns 1.7763568394002396e-15实际上是零,而不是1.77!你忽略了e-15。谢谢@Yacola。两种手动方法都是正确的,并产生相同的结果。要得到准确的结果,你可以使用
symphy.Matrix([[1,2,3],[2,3,4],[5,6,7])。det()
我既没有暗示也没有明确提到或指出
1.7763568394002396e-15
如何不等于
0
。我的问题是为什么我得到的是
0
,而不是行列式计算返回的结果。其他评论指出,我忽略了完全有意义的
e-15
的要点,您在评论中也提到了这一点,使其更加清晰。谢谢你的解释。我既没有暗示也没有明确提到或指出
1.7763568394002396e-15
如何不等于
0
。我的问题是为什么我得到的是
0
,而不是行列式计算返回的结果。其他评论指出,我忽略了完全有意义的
e-15
的要点,您在评论中也提到了这一点,使其更加清晰。谢谢你的解释。这就是我所说的完美答案!是的,我现在更清楚了。非常感谢。感谢@Stef,他在帖子评论中也指出了这一点。远非完美,但我很高兴这有帮助:)这就是我所说的完美答案!是的,我现在更清楚了。非常感谢。感谢@Stef,他在帖子评论中也指出了这一点。虽然还远远不够完美,但我很高兴这有帮助:)