我有一组矢量作为numpy数组。我需要从由2个向量v1和v2定义的平面中获得每个正交距离。我可以使用Gram-Schmidt过程轻松地获得单个载体。有没有办法快速完成许多向量,而不必遍历每个向量,或使用np.vectorize?
谢谢!
答案 0 :(得分:4)
你需要构建平面的单位法线:
在三个方面,这很容易做到:
nhat=np.cross( v1, v2 )
nhat=nhat/np.sqrt( np.dot( nhat,nhat) )
然后用你的每个向量点对此;我假设是一个Nx3
矩阵M
result=np.zeros( M.shape[0], dtype=M.dtype )
for idx in xrange( M.shape[0] ):
result[idx]=np.abs(np.dot( nhat, M[idx,:] ))
因此result[idx]
是idx'th
向量与平面的距离。
答案 1 :(得分:4)
一种更明确的方式来实现@ Jaime的答案,你可以明确指出投影算子的构造:
def normalized(v):
return v/np.sqrt( np.dot(v,v))
def ortho_proj_vec(v, uhat ):
'''Returns the projection of the vector v on the subspace
orthogonal to uhat (which must be a unit vector) by subtracting off
the appropriate multiple of uhat.
i.e. dot( retval, uhat )==0
'''
return v-np.dot(v,uhat)*uhat
def ortho_proj_array( Varray, uhat ):
''' Compute the orhogonal projection for an entire array of vectors.
@arg Varray: is an array of vectors, each row is one vector
(i.e. Varray.shape[1]==len(uhat)).
@arg uhat: a unit vector
@retval : an array (same shape as Varray), where each vector
has had the component parallel to uhat removed.
postcondition: np.dot( retval[i,:], uhat) ==0.0
for all i.
'''
return Varray-np.outer( np.dot( Varray, uhat), uhat )
# We need to ensure that the vectors defining the subspace are unit norm
v1hat=normalized( v1 )
# now to deal with v2, we need to project it into the subspace
# orhogonal to v1, and normalize it
v2hat=normalized( ortho_proj(v2, v1hat ) )
# TODO: if np.dot( normalized(v2), v1hat) ) is close to 1.0, we probably
# have an ill-conditioned system (the vectors are close to parallel)
# Act on each of your data vectors with the projection matrix,
# take the norm of the resulting vector.
result=np.zeros( M.shape[0], dtype=M.dtype )
for idx in xrange( M.shape[0] ):
tmp=ortho_proj_vec( ortho_proj_vec(M[idx,:], v1hat), v2hat )
result[idx]=np.sqrt(np.dot(tmp,tmp))
# The preceeding loop could be avoided via
#tmp=orhto_proj_array( ortho_proj_array( M, v1hat), v2hat )
#result=np.sum( tmp**2, axis=-1 )
# but this results in many copies of matrices that are the same
# size as M, so, initially, I prefer the loop approach just on
# a memory usage basis.
这实际上只是Gram-Schmidt正交化程序的推广。
请注意,在此过程结束时,我们有np.dot(v1hat, v1hat.T)==1
,np.dot(v2hat,v2hat.T)==1
,np.dot(v1hat, v2hat)==0
(在数值精度范围内)
答案 2 :(得分:2)
编辑我写的原始代码无法正常工作,所以我删除了它。但是按照下面解释的相同的想法,如果你花一些时间思考它,就不需要Cramer的规则,并且代码可以简化如下:
def distance(v1, v2, u) :
u = np.array(u, ndmin=2)
v = np.vstack((v1, v2))
vv = np.dot(v, v.T) # shape (2, 2)
uv = np.dot(u, v.T) # shape (n ,2)
ab = np.dot(np.linalg.inv(vv), uv.T) # shape(2, n)
w = u - np.dot(ab.T, v)
return np.sqrt(np.sum(w**2, axis=1)) # shape (n,)
为了确保它正常工作,我将Dave的代码打包到distance_3d
函数中并尝试了以下内容:
>>> d, n = 3, 1000
>>> v1, v2, u = np.random.rand(d), np.random.rand(d), np.random.rand(n, d)
>>> np.testing.assert_almost_equal(distance_3d(v1, v2, u), distance(v1, v2, u))
但当然它现在适用于任何d
:
>>> d, n = 1000, 3
>>> v1, v2, u = np.random.rand(d), np.random.rand(d), np.random.rand(n, d)
>>> distance(v1, v2, u)
array([ 10.57891286, 10.89765779, 10.75935644])
你必须分解你的向量,我们称之为u
,在两个向量的总和中,u = v + w
,v
在平面中,因此可以分解为{{ 1}},而v = a * v1 + b * v2
垂直于平面,因此w
。
如果你写np.dot(w, v1) = np.dot(w, v2) = 0
并将此表达式的点积与u = a * v1 + b * v2 + w
和v1
一起使用,你会得到两个有两个未知数的方程式:
v2
由于它只是一个2x2系统,我们可以使用Cramer's rule解决它:
np.dot(u, v1) = a * np.dot(v1, v1) + b * np.dot(v2, v1)
np.dot(u, v2) = a * np.dot(v1, v2) + b * np.dot(v2, v2)
从这里,你可以得到:
uv1 = np.dot(u, v1)
uv2 = np.dot(u, v2)
v11 = np.dot(v1, v2)
v22 = np.dot(v2, v2)
v12 = np.dot(v1, v2)
v21 = np.dot(v2, v1)
det = v11 * v22 - v21 * v12
a = (uv1 * v22 - v21 * uv2) / det
b = (v11 * uv2 - uv1 * v12) / det
与飞机的距离是w = u - v = u - a * v1 - b * v2
的模数。