我有一个线性方程组,如M X = N. M
是一个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
由np.linalg.det(M)
计算的此矩阵的行列式为零。
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
在这种情况下,np.linalg.det(M)
返回-9504863423.43。
我很确定行列式既不是0.0也不是-9504863423.43,因为我用MATLAB和一些在线计算器计算了相同的行列式-3.8108e + 019。
我还尝试执行LU分解并用mpmath计算行列式,但这些都不起作用。 为什么这两种情况可能会返回不同的值?以及如何正确计算行列式的任何想法?
我在32位Win7操作系统上使用Python(x,y)2.7.6.1。
答案 0 :(得分:0)
最精确和自动化的方法是:
然后this:
M = # Your matrix
(sign, logdet) = np.linalg.slogdet(M)
determinant = np.exp(logdet)