Numpy的点和交叉产品未返回预期值
我正在尝试编码将围绕轴旋转矢量的函数。 尽管我遇到了一些问题。我将显示我写的故障射击代码。
import numpy as np
def unit_vector(vec):
vec = np.array(vec, float)
unit_vec = []
for i in range(len(vec)):
unit_vec.append(vec[i]/np.linalg.norm(vec))
return np.array(unit_vec, float)
def rotation(vector,axis, angle):
vector = np.array(vector, float)
axis = np.array(vector, float)
first = vector * np.cos(angle)
second = np.cross(vector, unit_vector(axis))
third = unit_vector(axis)
fourth = np.dot(vector, unit_vector(axis))
fifth = 1 - np.cos(angle)
print("first : {0} \nSecond : {1} \nThird : {2}\nFourth : {3}\nFifth : {4}\n ".format(list(first), list(second), list(third),fourth, fifth))
rotation([0,1,0],[0,0,1],np.pi/2)
这是我程序的输出:
first : [0.0, 6.123233995736766e-17, 0.0]
Second : [0.0, 0.0, 0.0]
Third : [0.0, 1.0, 0.0]
Fourth : 1.0
Fifth : 0.9999999999999999
我想知道为什么第二和第四值分别为[0,0,0]和1。他们仅在矢量和轴都朝着相同的方向(不是它们的方向)面对时,才应输出这些值。即使我在功能之外打印这些变量,它们也可以正确输出。是什么导致该程序弄错了第二和第四变量?
I'm trying to code a function that will rotate a vector around an axis by an angle.
Though I am running into some problems.I will show the trouble-shooting code I wrote.
import numpy as np
def unit_vector(vec):
vec = np.array(vec, float)
unit_vec = []
for i in range(len(vec)):
unit_vec.append(vec[i]/np.linalg.norm(vec))
return np.array(unit_vec, float)
def rotation(vector,axis, angle):
vector = np.array(vector, float)
axis = np.array(vector, float)
first = vector * np.cos(angle)
second = np.cross(vector, unit_vector(axis))
third = unit_vector(axis)
fourth = np.dot(vector, unit_vector(axis))
fifth = 1 - np.cos(angle)
print("first : {0} \nSecond : {1} \nThird : {2}\nFourth : {3}\nFifth : {4}\n ".format(list(first), list(second), list(third),fourth, fifth))
rotation([0,1,0],[0,0,1],np.pi/2)
Here is the output of my program :
first : [0.0, 6.123233995736766e-17, 0.0]
Second : [0.0, 0.0, 0.0]
Third : [0.0, 1.0, 0.0]
Fourth : 1.0
Fifth : 0.9999999999999999
I was wondering why the second and fourth values are [0,0,0] and 1 respectively. They should only output these values if the vector and the axis are both facing in the same direction (which they are not). Even when I print these variables outside of the function they output correctly. What is causing the program to get the second and fourth variables wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
旋转的第二行
过程中。轴已设置为np.Array(vector,float)
,而不是np.Array(Axis,Float
。In the second line of the
rotation
procedure. axis has been set tonp.array(vector,float)
and notnp.array( axis, float
.