【问题标题】:Matrix rank and determinant vs. linear combinations of column vectors using sympy矩阵秩和行列式与使用 sympy 的列向量的线性组合
【发布时间】:2021-05-26 05:50:34
【问题描述】:

如果一个方阵不是满秩的,它的行列式为零,它的列向量是线性相关的,我们可以得到一个列向量作为其他列向量的线性组合。下面是一个矩阵,根据 sympy,没有满秩,行列式为 0。 但是,sympy 似乎无法找到任何两个列向量的线性组合,它等于第三个列向量。这是为什么呢?

输入:

x = sympy.symbols('x')
D = Matrix([[1,1+x,(1-x)*(1+3*x)],[1, 1-x, (1-x)**2],[1,1,(1-x**2)]])
print(D)
print('rank', D.rank())
print('det', D.det())
D[:,[1,2]].LUsolve(D[:,0]) # gives same result as D[:,[0,1]].LUsolve(D[:,2]) and D[:,[0,2]].LUsolve(D[:,1])

输出:

Matrix([[1, x + 1, (1 - x)*(3*x + 1)], [1, 1 - x, (1 - x)**2], [1, 1, 1 - x**2]])
rank 2
det 0

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-330-4d4f8f3e2cc0> in <module>
      5 print('det', D.det())
      6 
----> 7 D[:,1:].LUsolve(D[:,0])

~\anaconda3\envs\symbolic\lib\site-packages\sympy\matrices\matrices.py in LUsolve(self, rhs, iszerofunc)
   2152 
   2153     def LUsolve(self, rhs, iszerofunc=_iszero):
-> 2154         return _LUsolve(self, rhs, iszerofunc=iszerofunc)
   2155 
   2156     def QRsolve(self, b):

~\anaconda3\envs\symbolic\lib\site-packages\sympy\matrices\solvers.py in _LUsolve(M, rhs, iszerofunc)
    357             for j in range(b.cols):
    358                 if not iszerofunc(b[i, j]):
--> 359                     raise ValueError("The system is inconsistent.")
    360 
    361         b = b[0:n, :]   # truncate zero rows if consistent

ValueError: The system is inconsistent.

【问题讨论】:

    标签: python sympy linear-equation


    【解决方案1】:

    这可能是 lusolve 中的一个错误。

    您可以通过linsolve得到答案:

    In [10]: linsolve((D[:,[1,2]], D[:,0]))
    Out[10]: 
    ⎧⎛ -2        -1      ⎞⎫
    ⎪⎜─────, ────────────⎟⎪
    ⎨⎜x - 1   2          ⎟⎬
    ⎪⎝       x  - 2⋅x + 1⎠⎪
    ⎩                     ⎭
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2020-06-15
      • 2021-05-18
      • 2017-08-05
      相关资源
      最近更新 更多