如何将矢量投影到Python中由其正交矢量定义的平面上?

Sib*_*ing 5 python math 3d linear-algebra

我有一个平面,plane A由它的正交向量定义,比方说(a, b, c).

(即向量(a, b, c)是正交的plane A)

我希望将一个矢量(d, e, f)投影到plane A.

我怎么能在Python中做到这一点?我认为必须有一些简单的方法.

jas*_*son 8

采取(d, e, f)核减掉它到标准化垂直于平面上的投影(你的情况(a, b, c)).所以:

v = (d, e, f)
        - sum((d, e, f) *. (a, b, c)) * (a, b, c) / sum((a, b, c) *. (a, b, c))
Run Code Online (Sandbox Code Playgroud)

在这里,*.我的意思是组件方面的产品.所以这意味着:

sum([x * y for x, y in zip([d, e, f], [a, b, c])])
Run Code Online (Sandbox Code Playgroud)

要么

d * a + e * b + f * c
Run Code Online (Sandbox Code Playgroud)

如果你只是想要清楚但迂腐

并且类似地(a, b, c) *. (a, b, c).因此,在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))]
Run Code Online (Sandbox Code Playgroud)

然后你可以说:

p = project_onto_plane([3, 4, 5], [1, 2, 3])
Run Code Online (Sandbox Code Playgroud)