How to find linearly independent rows from a matrix

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

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

\"enter

5条回答
  •  星月不相逢
    2020-12-05 08:29

    I edited the code for Cauchy-Schwartz inequality which scales better with dimension: the inputs are the matrix and its dimension, while the output is a new rectangular matrix which contains along its rows the linearly independent columns of the starting matrix. This works in the assumption that the first column in never null, but can be readily generalized in order to implement this case too. Another thing that I observed is that 1e-5 seems to be a "sloppy" threshold, since some particular pathologic vectors were found to be linearly dependent in that case: 1e-4 doesn't give me the same problems. I hope this could be of some help: it was pretty difficult for me to find a really working routine to extract li vectors, and so I'm willing to share mine. If you find some bug, please report them!!

    from numpy import dot, zeros
    from numpy.linalg import matrix_rank, norm
    
    def find_li_vectors(dim, R):
    
        r = matrix_rank(R) 
        index = zeros( r ) #this will save the positions of the li columns in the matrix
        counter = 0
        index[0] = 0 #without loss of generality we pick the first column as linearly independent
        j = 0 #therefore the second index is simply 0
    
        for i in range(R.shape[0]): #loop over the columns
            if i != j: #if the two columns are not the same
                inner_product = dot( R[:,i], R[:,j] ) #compute the scalar product
                norm_i = norm(R[:,i]) #compute norms
                norm_j = norm(R[:,j])
    
                #inner product and the product of the norms are equal only if the two vectors are parallel
                #therefore we are looking for the ones which exhibit a difference which is bigger than a threshold
                if absolute(inner_product - norm_j * norm_i) > 1e-4:
                    counter += 1 #counter is incremented
                    index[counter] = i #index is saved
                    j = i #j is refreshed
                #do not forget to refresh j: otherwise you would compute only the vectors li with the first column!!
    
        R_independent = zeros((r, dim))
    
        i = 0
        #now save everything in a new matrix
        while( i < r ):
            R_independent[i,:] = R[index[i],:] 
            i += 1
    
        return R_independent
    

提交回复
热议问题