本文介绍了创建统一的随机四元数和两个四元数的乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个python(NumPy)函数,可创建统一的随机四元数.我想从同一个函数或另一个函数中获得两个四元数乘法作为二维返回数组.在我最近的情况下,四元数乘法的公式为Q1 * Q2和Q2 * Q1.在这里,Q1=(w0, x0, y0, z0)Q2=(w1, x1, y1, z1)是两个四元数.预期的两个四元数乘法输出(作为二维返回数组)应为

I have a python (NumPy) function which creates a uniform random quaternion. I would like to get two quaternion multiplication as 2-dimensional returned array from the same or an another function. The formula of quaternion multiplication in my recent case is Q1*Q2 and Q2*Q1. Here, Q1=(w0, x0, y0, z0) and Q2=(w1, x1, y1, z1) are two quaternions. The expected two quaternion multiplication output (as 2-d returned array) should be

return([-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])

有人可以帮我吗?我的代码在这里:

Can anyone help me please? My codes are here:

def randQ(N):
    #Generates a uniform random quaternion
    #James J. Kuffner 2004
    #A random array 3xN
    s = random.rand(3,N)
    sigma1 = sqrt(1.0 - s[0])
    sigma2 = sqrt(s[0])
    theta1 = 2*pi*s[1]
    theta2 = 2*pi*s[2]
    w = cos(theta2)*sigma2
    x = sin(theta1)*sigma1
    y = cos(theta1)*sigma1
    z = sin(theta2)*sigma2
    return array([w, x, y, z])

推荐答案

您的请求的简单呈现如下:

A simple rendition of your request would be:

In [70]: def multQ(Q1,Q2):
    ...:     w0,x0,y0,z0 = Q1   # unpack
    ...:     w1,x1,y1,z1 = Q2
    ...:     return([-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])
    ...:

In [72]: multQ(randQ(1),randQ(2))
Out[72]:
[array([-0.37695449,  0.79178506]),
 array([-0.38447116,  0.22030199]),
 array([ 0.44019022,  0.56496059]),
 array([ 0.71855397,  0.07323243])]

结果是4个数组的列表.只需将其包装在np.array()中即可获得2d数组:

The result is a list of 4 arrays. Just wrap it in np.array() to get a 2d array:

In [73]: M=np.array(_)

In [74]: M
Out[74]:
array([[-0.37695449,  0.79178506],
       [-0.38447116,  0.22030199],
       [ 0.44019022,  0.56496059],
       [ 0.71855397,  0.07323243]])

我没有试图理解或整理您的描述-只是将其呈现为工作代码.

I haven't tried to understand or clean up your description - just rendering it as working code.

这篇关于创建统一的随机四元数和两个四元数的乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 13:34