hak*_*anc 23
首先,第3行与1t和第2行呈线性关系.但是,第1列和第4列与线性相关.
您可以使用两种方法:
特征值
如果矩阵的一个特征值为零,则其对应的特征向量是线性相关的.文档eig统计特征值不一定是有序的.不知道结果数组中的特征值是刚刚排序还是有一些随机顺序.但是,假设特征值对应于行向量,则方法为:
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,:]
Run Code Online (Sandbox Code Playgroud)
Cauchy-Schwarz不等式
为了测试向量的线性相关性并找出哪些向量,可以使用Cauchy-Schwarz不等式.基本上,如果向量的内积等于向量范数的乘积,则向量是线性相关的.以下是列的示例:
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'
Run Code Online (Sandbox Code Playgroud)
测试行是一种类似的方法.
然后你可以扩展它来测试矢量的所有组合,但我想这个解决方案与大小一样严重.
MSe*_*ert 17
通过sympy,您可以使用以下命令找到线性独立行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]
Run Code Online (Sandbox Code Playgroud)
这基本上告诉你行0,1和3是线性独立的,而行2不是(它是行0和1的线性组合).
然后你可以用切片删除这些行:
>>> mat[inds]
array([[0, 1, 0, 0],
[0, 0, 1, 0],
[1, 0, 0, 1]])
Run Code Online (Sandbox Code Playgroud)
这也适用于矩形(不仅适用于二次)矩阵.
我编辑了Cauchy-Schwartz不等式的代码,该代码随维度更好地扩展:输入是矩阵及其维度,而输出是一个新的矩形矩阵,其中包含起始矩阵的线性独立列.这工作假设第一列永远不为null,但可以很容易地推广以便实现这种情况.我观察到的另一件事是1e-5似乎是一个"草率"的阈值,因为在这种情况下发现一些特定的病理载体是线性依赖的:1e-4不会给我同样的问题.我希望这可以提供一些帮助:我很难找到一个真正有效的例程来提取li矢量,所以我愿意和我分享.如果您发现了一些错误,请报告!!
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
Run Code Online (Sandbox Code Playgroud)