Numpy的点和交叉产品未返回预期值

发布于 2025-02-10 06:29:57 字数 1055 浏览 4 评论 0原文

我正在尝试编码将围绕轴旋转矢量的函数。 尽管我遇到了一些问题。我将显示我写的故障射击代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2025-02-17 06:29:57

旋转的第二行过程中。轴已设置为np.Array(vector,float),而不是np.Array(Axis,Float

In the second line of the rotation procedure. axis has been set to np.array(vector,float) and not np.array( axis, float.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文