[英]Multiply each row by different rotation matrix
This function multiplies each of the n
rows of pose
by a different rotation matrix. 该函数将
n
行pose
的每一行乘以不同的旋转矩阵。 Is it possible to avoid the loop by maybe using a 3d tensor of rotation matrices? 是否可以通过使用3d张量的旋转矩阵来避免循环?
def transform(ref, pose):
n, d = pose.shape
p = ref[:, :d].copy()
c = np.cos(ref[:, 2])
s = np.sin(ref[:, 2])
for i in range(n):
p[i,:2] += pose[i,:2].dot(np.array([[c[i], s[i]], [-s[i], c[i]]]))
return p
Here's one with np.einsum
- 这是一个与
np.einsum
-
# Setup 3D rotation matrix
cs = np.empty((n,2,2))
cs[:,0,0] = c
cs[:,1,1] = c
cs[:,0,1] = s
cs[:,1,0] = -s
# Perform 3D matrix multiplications with einsum
p_out = ref[:, :d].copy()
p_out[:,:2] += np.einsum('ij,ijk->ik',pose[:,:2],cs)
Alternatively, replace the two assigning steps for c
with one involving one more einsum
- 或者,替换
c
的两个分配步骤,其中一个涉及一个einsum
-
np.einsum('ijj->ij',cs)[...] = c[:,None]
Use optimize
flag with True
value in np.einsum
to leverage BLAS
. 在
np.einsum
使用带有True
值的optimize
标志来利用BLAS
。
Alternatively, we can use np.matmul/@ operator in Python 3.x
to replace the einsum
part - 或者,我们可以
np.matmul/@ operator in Python 3.x
使用np.matmul/@ operator in Python 3.x
来替换einsum
部分 -
p_out[:,:2] += np.matmul(pose[:,None,:2],cs)[:,0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.