大矩阵的python行列式



我有一个线性方程组,比如MX=N。CCD_ 1是一个21x21矩阵,其中许多元素为零。当我试图用X = np.linalg.solve(M, N)解决这个系统时,它给了我这个错误:

numpy.linalg.linalg.LinAlgError: Singular matrix

这里的问题是np.linalg.det(M)返回的值是0.0。我尝试了两种不同的方法来生成M矩阵,这时我遇到了一个奇怪的行为:

i)M的非零元素在代码的其他地方计算。所有这些元素都是浮动的,并且表示为m_1, m_2, ... , m_21。起初,我尝试了以下代码来生成M:

M = np.zeros([21,21])
M[0,0] = m_1
M[0,1] = m_2
M[1,0] = m_3
M[1,4] = m_2
M[2,2] = m_2
M[2,3] = m_1
M[3,3] = m_3
M[3,5] = m_2
M[4,4] = m_4
M[4,5] = m_5
M[5,8] = m_6
M[5,13] = m_7
M[6,9] = m_6
M[6,14] = m_7
M[7,11] = m_6
M[7,12] = m_7
M[8,8] = m_8
M[8,9] = m_9
M[8,11] = m_10
M[9,6] = m_11
M[9,8] = m_12
M[9,20] = m_13
M[10,5] = m_11
M[10,10] = m_12
M[10,19] = m_13
M[11,19] = m_14
M[11,20] = m_15
M[12,8] = m_15
M[12,10] = m_14
M[13,16] = m_4
M[13,17] = m_17
M[14,7] = m_15
M[14,17] = m_16
M[15,16] = m_18
M[15,18] = m_7
M[16,17] = m_19
M[16,18] = m_20
M[17,4] = m_14
M[17,16] = m_16
M[18,11] = m_12
M[18,15] = m_13
M[19,12] = m_20
M[19,15] = m_21
M[20,7] = m_19
M[20,13] = m_20
M[20,20] = m_21

由CCD_ 8计算得到的该矩阵的行列式为零。

ii)然后我用相应的数值替换非零元素(m_1, ... , m_21),看看行列式是否会改变。这是代码:

 M = np.matrix([[-88.89714245, 33.72326786, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #1
                [-139.63175129, 0, 0, 0, 33.72326786, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],#2
                [0,0,33.72326786, -88.89714245, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #3
                [0, 0, 0, -139.63175129, 0, 33.72326786, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],#4
                [0, 0, 0, 0, 98.58344885, 55.0147276, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #5
                [0, 0, 0, 0, 0, 0, 0, 0, 114.92510983, 0, 0, 0, 0, 66.13785145, 0, 0, 0, 0, 0, 0, 0], #6
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 114.92510983, 0, 0, 0, 0, 66.13785145, 0, 0, 0, 0, 0, 0], #7
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 114.92510983, 66.13785145, 0, 0, 0, 0, 0, 0, 0, 0], #8
                [0, 0, 0, 0, 0, 0, 0, 0, 28.52149986, -96.35068993, 0, 67.82919006, 0, 0, 0, 0, 0, 0, 0, 0, 0], #9
                [0, 0, 0, 0, 0, 0, 83.66136319, 0, 95.15580459, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -178.81716778], #10
                [0, 0, 0, 0, 0, 83.66136319, 0, 0, 0, 0, 95.15580459, 0, 0, 0, 0, 0, 0, 0, 0, -178.81716778, 0], #11
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89.26005554, 67.6481946], #12
                [0, 0, 0, 0, 0, 0, 0, 0, 67.6481946, 0, 89.26005554, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], #13
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,98.58344885, -153.59817645, 0, 0, 0], #14
                [0, 0, 0, 0, 0, 0, 0, 67.6481946, 0, 0, 0, 0, 0, 0, 0, 0, 0, -156.90825014, 0, 0, 0], #15
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -181.06296128, 0,66.13785145, 0, 0], #16
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -153.11049424, 35.89577791, 0, 0], #17
                [0, 0, 0, 0, 89.26005554, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -156.90825014, 0, 0, 0, 0], #18
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95.15580459, 0, 0, 0, -178.81716778, 0, 0, 0, 0, 0], #19
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35.89577791, 0, 0, 117.21471633, 0, 0, 0, 0, 0], #20
                [0, 0, 0, 0, 0, 0, 0, -153.11049424, 0, 0, 0, 0, 0, 35.89577791, 0, 0, 0, 0, 0, 0, 117.21471633]]) #21

在这种情况下,M0返回-950486634.43。我很确定行列式既不是0.0,也不是-95048663423.43,因为我用MATLAB和一些在线计算器计算出了与-38.108e+019相同的行列式。

我还试着用mpmath进行LU分解和计算行列式,但这些都不起作用。为什么这两种情况会返回不同的值?如何正确计算行列式?

我在32位Win7操作系统上使用Python(x,y)2.7.6.1。

最精确和自动化的方法是:

  1. 确保/设置矩阵使用dtype='float32'
  2. 然后这样做:

    M =  # Your matrix
    (sign, logdet) = np.linalg.slogdet(M)
    determinant = np.exp(logdet)
    

相关内容

  • 没有找到相关文章