如何从矩阵中找到线性无关的行 [英] How to find linearly independent rows from a matrix

查看:85
本文介绍了如何从矩阵中找到线性无关的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何识别矩阵中线性无关的行?例如,

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

第 4 行是独立的.

推荐答案

首先,您的第 3 行与 1t 和第 2 行线性相关.但是,您的第 1 列和第 4 列线性相关.

First, your 3rd row is linearly dependent with 1t and 2nd row. However, your 1st and 4th column are linearly dependent.

您可以使用的两种方法:

Two methods you could use:

特征值

如果矩阵的一个特征值为零,则其对应的特征向量线性相关.文档 eig表示返回的特征值根据它们的多重性重复并且不一定是有序的.但是,假设特征值对应于您的行向量,一种方法是:

If one eigenvalue of the matrix is zero, its corresponding eigenvector is linearly dependent. The documentation eig states the returned eigenvalues are repeated according to their multiplicity and not necessarily ordered. However, assuming the eigenvalues correspond to your row vectors, one method would be:

import numpy as np

matrix = np.array(
    [
        [0, 1 ,0 ,0],
        [0, 0, 1, 0],
        [0, 1, 1, 0],
        [1, 0, 0, 1]
    ])

lambdas, V =  np.linalg.eig(matrix.T)
# The linearly dependent row vectors 
print matrix[lambdas == 0,:]

柯西-施瓦茨不等式

要测试向量的线性相关性并找出哪些向量,您可以使用 柯西-施瓦茨不等式.基本上,如果向量的内积等于向量范数的乘积,则向量线性相关.以下是列的示例:

To test linear dependence of vectors and figure out which ones, you could use the Cauchy-Schwarz inequality. Basically, if the inner product of the vectors is equal to the product of the norm of the vectors, the vectors are linearly dependent. Here is an example for the columns:

import numpy as np

matrix = np.array(
    [
        [0, 1 ,0 ,0],
        [0, 0, 1, 0],
        [0, 1, 1, 0],
        [1, 0, 0, 1]
    ])

print np.linalg.det(matrix)

for i in range(matrix.shape[0]):
    for j in range(matrix.shape[0]):
        if i != j:
            inner_product = np.inner(
                matrix[:,i],
                matrix[:,j]
            )
            norm_i = np.linalg.norm(matrix[:,i])
            norm_j = np.linalg.norm(matrix[:,j])

            print 'I: ', matrix[:,i]
            print 'J: ', matrix[:,j]
            print 'Prod: ', inner_product
            print 'Norm i: ', norm_i
            print 'Norm j: ', norm_j
            if np.abs(inner_product - norm_j * norm_i) < 1E-5:
                print 'Dependent'
            else:
                print 'Independent'

测试行是一种类似的方法.

To test the rows is a similar approach.

然后你可以扩展它来测试向量的所有组合,但我认为这个解决方案会随着大小的变化而严重扩展.

Then you could extend this to test all combinations of vectors, but I imagine this solution scale badly with size.

这篇关于如何从矩阵中找到线性无关的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆