【问题标题】:How to multiply two quaternions by python or numpy [duplicate]如何通过python或numpy将两个四元数相乘[重复]
【发布时间】:2016-08-17 15:30:07
【问题描述】:

我有两个四元数:Q1= w0, x0, y0, z0 和 Q2 = w1, x1, y1, z1。我想通过使用可以返回二维数组的 NumPy 或 Python 函数来将它们相乘。我在互联网上找到了一些由 Christoph Gohlke 编写的伪代码来进行这种乘法运算。我尝试了很多但未能应用它。谁能帮我做这种乘法?伪代码在这里:`

def quaternion_multiply(quaternion1, quaternion0):
w0, x0, y0, z0 = quaternion0
w1, x1, y1, z1 = quaternion1
return array([-x1*x0 - y1*y0 - z1*z0 + w1*w0,
                     x1*w0 + y1*z0 - z1*y0 + w1*x0,
                    -x1*z0 + y1*w0 + z1*x0 + w1*y0,
                     x1*y0 - y1*x0 + z1*w0 + w1*z0], dtype=float64)` 

【问题讨论】:

  • 你昨天刚问过这个:stackoverflow.com/questions/38978441/…;如果这些答案还不够,请在此处评论或编辑您的问题。
  • @hpaulj 我认为人们不会回答老问题。我昨天问的问题也没有人正确回答我的问题。请不要将其标记为重复问题。
  • 我昨天的回答和你这里接受的基本一样。唯一的区别是这个在函数内部移动了np.array() 步骤。如果这很重要,您可以在评论中提出该问题。
  • @hpaulj 抱歉造成误会。我现在接受你的回答。

标签: python numpy 2d multiplication quaternions


【解决方案1】:

这是一个使用你的函数的小例子:

import numpy as np
import random


def quaternion_multiply(quaternion1, quaternion0):
    w0, x0, y0, z0 = quaternion0
    w1, x1, y1, z1 = quaternion1
    return np.array([-x1 * x0 - y1 * y0 - z1 * z0 + w1 * w0,
                     x1 * w0 + y1 * z0 - z1 * y0 + w1 * x0,
                     -x1 * z0 + y1 * w0 + z1 * x0 + w1 * y0,
                     x1 * y0 - y1 * x0 + z1 * w0 + w1 * z0], dtype=np.float64)

N = 4
for i in range(N):
    q1 = np.random.rand(4)
    q2 = np.random.rand(4)
    q = quaternion_multiply(q1, q2)
    print("{0} x {1} = {2}".format(q1, q2, q))

【讨论】:

  • 我已经有一个可以创建 2 个四元数数组的 python 函数。我现在正在尝试使用另一个应该返回乘法输出的函数来计算他们的乘积。
  • @Biophysics 好的,那么我的示例的哪一部分不清楚?我想我已经回答了你关于I have two quaternions Q1 and Q2. I would like to multiply them by using NumPy or Python function which can return 2-d array 的问题,看看这行q = quaternion_multiply(q1, q2)
  • 是的,它有效。谢谢:)
  • 你能帮我从上面的函数中获取数组的输出吗?
猜你喜欢
  • 1970-01-01
  • 2013-09-03
  • 2016-09-10
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
相关资源
最近更新 更多