如何将向量投影到由 Python 中的正交向量定义的平面上? [英] How may I project vectors onto a plane defined by its orthogonal vector in Python?

查看:37
本文介绍了如何将向量投影到由 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) 投影到 plane 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))

在此,*. 我指的是 component-wise 产品.所以这意味着:

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屋!

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