如何通过python或numpy将两个四元数相乘 [英] How to multiply two quaternions by python or numpy
本文介绍了如何通过python或numpy将两个四元数相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个四元数:Q1 = w0,x0,y0,z0和Q2 = w1,x1,y1,z1.我想通过使用NumPy或可以返回二维数组的Python函数将它们相乘.我在网上找到了一些由Christoph Gohlke编写的伪代码来进行这种乘法.我尝试了很多,但未能应用.有人可以帮我做这种乘法吗?伪代码在这里:`
I have two quaternions: Q1= w0, x0, y0, z0 and Q2 = w1, x1, y1, z1. I would like to multiply them by using NumPy or Python function which can return 2-d array. I found some pseudocodes on the internet which is written by Christoph Gohlke to do this kind of multiplication. I tried a lot but failed to apply it. Can anyone help me please to do this kind of multiplication? The pseudocodes are here: `
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)`
推荐答案
以下是使用您的函数的一个小示例:
Here's a little example using your function:
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))
这篇关于如何通过python或numpy将两个四元数相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文