How to find linearly independent rows from a matrix

前端 未结 5 1144
生来不讨喜
生来不讨喜 2020-12-05 08:00

How to identify the linearly independent rows from a matrix? For instance,

\"enter

5条回答
  •  粉色の甜心
    2020-12-05 08:34

    With sympy you can find the linear independant rows using: sympy.Matrix.rref:

    >>> import sympy 
    >>> import numpy as np
    >>> mat = np.array([[0,1,0,0],[0,0,1,0],[0,1,1,0],[1,0,0,1]])  # your matrix
    >>> _, inds = sympy.Matrix(mat).T.rref()   # to check the rows you need to transpose!
    >>> inds
    [0, 1, 3]
    

    Which basically tells you the rows 0, 1 and 3 are linear independant while row 2 isn't (it's a linear combination of row 0 and 1).

    Then you could remove these rows with slicing:

    >>> mat[inds]
    array([[0, 1, 0, 0],
           [0, 0, 1, 0],
           [1, 0, 0, 1]])
    

    This also works well for rectangular (not only for quadratic) matrices.

提交回复
热议问题