使用Python进行矢量投影
矢量是具有大小(即长度)和方向的几何对象。向量一般用一条连接起点A和终点B的具有一定方向的线段来表示,如下图所示,记为
一个向量在另一个向量上的投影
向量的投影到另一个向量上给出为
在Python中计算向量投影到另一个向量上:
# import numpy to perform operations on vector
import numpy as np
u = np.array([1, 2, 3]) # vector u
v = np.array([5, 6, 2]) # vector v:
# Task: Project vector u on vector v
# finding norm of the vector v
v_norm = np.sqrt(sum(v**2))
# Apply the formula as mentioned above
# for projecting a vector onto another vector
# find dot product using np.dot()
proj_of_u_on_v = (np.dot(u, v)/v_norm**2)*v
print("Projection of Vector u on Vector v is: ", proj_of_u_on_v)
输出:
Projection of Vector u on Vector v is: [1.76923077 2.12307692 0.70769231]
一个用于将一个向量投影到另一个向量上的线性代码:
(np.dot(u, v)/np.dot(v, v))*v
将向量投影到平面上
向量的投影在一个平面上的计算是通过减去的分量它与平面正交 .
在哪里, 是平面法向量。
在Python中计算向量投影到平面上:
# import numpy to perform operations on vector
import numpy as np
# vector u
u = np.array([2, 5, 8])
# vector n: n is orthogonal vector to Plane P
n = np.array([1, 1, 7])
# Task: Project vector u on Plane P
# finding norm of the vector n
n_norm = np.sqrt(sum(n**2))
# Apply the formula as mentioned above
# for projecting a vector onto the orthogonal vector n
# find dot product using np.dot()
proj_of_u_on_n = (np.dot(u, n)/n_norm**2)*n
# subtract proj_of_u_on_n from u:
# this is the projection of u on Plane P
print("Projection of Vector u on Plane P is: ", u - proj_of_u_on_n)
输出:
Projection of Vector u on Plane P is: [ 0.76470588 3.76470588 -0.64705882]