简体   繁体   English

用不同的旋转矩阵乘以每一行

[英]Multiply each row by different rotation matrix

This function multiplies each of the n rows of pose by a different rotation matrix. 该函数将npose的每一行乘以不同的旋转矩阵。 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.

相关问题 在Numpy中将矩阵乘以另一个矩阵的每一行 - Multiply matrix by each row of another matrix in Numpy 如何将矩阵的每一行与numpy相乘 - how to multiply each row of a matrix with numpy 如何将Python中第二个矩阵中每列的矩阵中的每一行乘以? - How to multiply each row in a matrix by each column in the second matrix in Python? 将两行的值乘以两个不同的数据帧 - Multiply the values for each row in two different dataframes 将数组的每个数字与其在矩阵中对齐的行数相乘? - Multiply each number of an array with its aligned row of a matrix in numpy? 如何将矩阵与向量相乘,以便每一行都与该向量相乘 - How to multiply a matrix with vector such that each row is multiplied elementwise with this vector 如何将softmax的每一行乘以tensorflow中的特定矩阵? - How to multiply each row of the softmax by a specific matrix in tensorflow? 将矩阵的每一行与其共轭转置的numpy相乘 - Multiply each row of a matrix with it's conjugate transposed numpy 将块矩阵的每个块与NumPy中的不同系数相乘 - Multiply each block of a block matrix with different coefficient in NumPy 矩阵乘法:用Python中的另一个2D矩阵乘以矩阵的每一行 - Matrix Multiplication: Multiply each row of matrix by another 2D matrix in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM