如何在Python中将向量投影到由其正交向量定义的平面上? [英] How may I project vectors onto a plane defined by its orthogonal vector in Python?
问题描述
我有一个平面,平面A
,由其正交矢量定义,例如(a,b,c)
.
I have a plane, plane A
, defined by its orthogonal vector, say (a, b, c)
.
(即向量(a,b,c)
与 A平面
正交)
我希望将向量(d,e,f)
投影到 A平面
上.
I wish to project a vector (d, e, f)
onto plane A
.
如何在Python中完成此操作?我认为必须有一些简单的方法.
How can I do it in Python? I think there must be some easy ways.
推荐答案
取(d,e,f)
并减去其在垂直于该平面的法线上的投影(在您的情况下)(a,b,c)
).所以:
Take (d, e, f)
and subtract off the projection of it onto the normalized normal to the plane (in your case (a, b, c)
). So:
v = (d, e, f)
- sum((d, e, f) *. (a, b, c)) * (a, b, c) / sum((a, b, c) *. (a, b, c))
在这里,用 *.
是指按组件划分的产品.所以这意味着:
Here, by *.
I mean the component-wise product. So this would mean:
sum([x * y for x, y in zip([d, e, f], [a, b, c])])
或
d * a + e * b + f * c
如果您只想保持清醒而又学究的话
if you just want to be clear but pedantic
,对于(a,b,c)*同样.(a,b,c)
.因此,在Python中:
and similarly for (a, b, c) *. (a, b, c)
. Thus, in Python:
from math import sqrt
def dot_product(x, y):
return sum([x[i] * y[i] for i in range(len(x))])
def norm(x):
return sqrt(dot_product(x, x))
def normalize(x):
return [x[i] / norm(x) for i in range(len(x))]
def project_onto_plane(x, n):
d = dot_product(x, n) / norm(n)
p = [d * normalize(n)[i] for i in range(len(n))]
return [x[i] - p[i] for i in range(len(x))]
那么你可以说:
p = project_onto_plane([3, 4, 5], [1, 2, 3])
这篇关于如何在Python中将向量投影到由其正交向量定义的平面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!