I have a function that produces a rotation matrix according to the Euler-Rodrigues formula in python. However when I rotate an array by half pi it does not sit 90 degrees to the original vector and I am not sure why. If I rotate by pi or 2pi then I get the expected rotation. The code is as follows.
我有一个函数根据python中的欧拉-罗德里格斯公式生成一个旋转矩阵。但是当我将一个数组旋转一半的时候它不会对原始向量有90度,我也不知道为什么。如果旋转2或2,就得到期望的旋转。代码如下。
# This is my function for making the rotation matrix
def RotationMatrix(axis, theta):
"""
This uses Euler-Rodrigues formula.
"""
axis = np.asarray(axis)
axis = axis / math.sqrt(np.dot(axis, axis))
a = math.cos(theta / 2)
b, c, d = -axis * math.sin(theta / 2)
a2, b2, c2, d2 = a * a, b * b, c * c, d * d
bc, ad, ac, ab, bd, cd = b * c, a * d, a * c, a * b, b * d, c * d
return np.array([
[a2 + b2 - c2 - d2, 2 * (bc - ad), 2 * (bd + ac)],
[2 * (bc + ad), a2 + c2 - b2 - d2, 2 * (cd - ab)],
[2 * (bd - ac), 2 * (cd + ab), a2 + d2 - b2 - c2]
])
#Here I call the function to provide a rotation matrix
#that should rotate by 90 degrees
x = RotationMatrix((0, 1, 0), (math.pi * .5))
print(x)
#Here I define my test vector to rotate
vector = np.array((3, 4, 0))
print(vector)
# Here I apply the rotation matrix (this will become a function
# one day)
a1 = vector[0] * x[0,0] + vector[0] * x[0,1] + vector[0] * x[0,2]
b1 = vector[1] * x[1,0] + vector[1] * x[1,1] + vector[1] * x[1,2]
c1 = vector[2] * x[2,0] + vector[2] * x[2,1] + vector[2] * x[2,2]
appliedrotation = np.array((a1, b1, c1))
print(appliedrotation)
#below here I just get the dot product and magnitude so I
#can calculate the rotation in degrees
dp = np.dot(vector, appliedrotation)
print(dp)
maga = math.sqrt(vector[0] ** 2 + vector[1] ** 2 + vector[2] ** 2)
magb = math.sqrt(
appliedrotation[0] ** 2 + appliedrotation[1] ** 2 + appliedrotation[2]
)
magc = maga * magb
hmm = dp / magc
hmm1 = ((math.acos(hmm)) * 180) / math.pi
print(hmm1)
There is a chance I am just forgetting my A-level maths vector knowledge and when rotating in three dimensions I shouldn't be getting a change of 90 degrees at half pi but I am struggling to get my head round that. Any help would be much appreciated.
有可能我只是忘记了我的a级数学向量知识,当我在三维旋转时,我不应该在圆周率为1 / 2时得到90度的改变,但是我很难理解。如有任何帮助,我们将不胜感激。
1 个解决方案
#1
2
Your manually calculated dot product is wrong here:
你手工计算的点积在这里是错误的:
# Here I apply the rotation matrix (this will become a function one day)
a1 = vector[0] * x[0,0] + vector[0] * x[0,1] + vector[0] * x[0,2]
b1 = vector[1] * x[1,0] + vector[1] * x[1,1] + vector[1] * x[1,2]
c1 = vector[2] * x[2,0] + vector[2] * x[2,1] + vector[2] * x[2,2]
You want the first component of the result to be the first row of x
dotted with vector
, which would mean
你想要结果的第一个分量是第一行x点乘向量,这意味着
a1 = vector[0] * x[0,0] + vector[1] * x[0,1] + vector[2] * x[0,2]
What you actually have amounts to some type of weighted sum across rows of x
using the corresponding entry of vector
as the weight, which is not right.
实际上,你可以用向量的相应分量来表示某一列向量的加权和,这是不对的。
You can see this by simply computing it with np.dot
instead of the manual matrix-vector multiplication:
只要用np来计算就能看出这一点。点而不是手工矩阵向量乘法:
In [17]: x
Out[17]:
array([[ 2.22044605e-16, 0.00000000e+00, -1.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[ 1.00000000e+00, 0.00000000e+00, 2.22044605e-16]])
In [18]: vector
Out[18]: array([3, 4, 0])
In [19]: np.dot(x, vector)
Out[19]: array([ 6.66133815e-16, 4.00000000e+00, 3.00000000e+00])
which is (0, 4, 3)
, as expected.
也就是(0,4,3)
Imagine drawing the vector (3, 4)
, pointing up and to the right in the positive quadrant of a plane (since the original z coordinate is 0). If you rotate by 90 degrees around the y axis, then the vector rotates out of the page towards you until the 3-component that was previously along the x-axis is now along the z-axis (while the y-component stayed the same), meaning you're now at the vector (0, 4, 3)
.
想象画矢量(3、4),并指向正确的积极的象限的一架飞机(因为原来的z坐标是0)。如果你在y轴旋转90度,然后矢量旋转的页面转向你直到之前的三分量沿x轴正沿着z轴(虽然分量保持不变),意思是你现在在向量(0、4、3)。
#1
2
Your manually calculated dot product is wrong here:
你手工计算的点积在这里是错误的:
# Here I apply the rotation matrix (this will become a function one day)
a1 = vector[0] * x[0,0] + vector[0] * x[0,1] + vector[0] * x[0,2]
b1 = vector[1] * x[1,0] + vector[1] * x[1,1] + vector[1] * x[1,2]
c1 = vector[2] * x[2,0] + vector[2] * x[2,1] + vector[2] * x[2,2]
You want the first component of the result to be the first row of x
dotted with vector
, which would mean
你想要结果的第一个分量是第一行x点乘向量,这意味着
a1 = vector[0] * x[0,0] + vector[1] * x[0,1] + vector[2] * x[0,2]
What you actually have amounts to some type of weighted sum across rows of x
using the corresponding entry of vector
as the weight, which is not right.
实际上,你可以用向量的相应分量来表示某一列向量的加权和,这是不对的。
You can see this by simply computing it with np.dot
instead of the manual matrix-vector multiplication:
只要用np来计算就能看出这一点。点而不是手工矩阵向量乘法:
In [17]: x
Out[17]:
array([[ 2.22044605e-16, 0.00000000e+00, -1.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[ 1.00000000e+00, 0.00000000e+00, 2.22044605e-16]])
In [18]: vector
Out[18]: array([3, 4, 0])
In [19]: np.dot(x, vector)
Out[19]: array([ 6.66133815e-16, 4.00000000e+00, 3.00000000e+00])
which is (0, 4, 3)
, as expected.
也就是(0,4,3)
Imagine drawing the vector (3, 4)
, pointing up and to the right in the positive quadrant of a plane (since the original z coordinate is 0). If you rotate by 90 degrees around the y axis, then the vector rotates out of the page towards you until the 3-component that was previously along the x-axis is now along the z-axis (while the y-component stayed the same), meaning you're now at the vector (0, 4, 3)
.
想象画矢量(3、4),并指向正确的积极的象限的一架飞机(因为原来的z坐标是0)。如果你在y轴旋转90度,然后矢量旋转的页面转向你直到之前的三分量沿x轴正沿着z轴(虽然分量保持不变),意思是你现在在向量(0、4、3)。