ValueError:matmul:输入操作数1的核心维度0不匹配?

2024-09-27 09:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用“枢轴算法”实现随机移动。我得到以下错误信息,即:

enter image description here

我知道我正在尝试将一个3行1列的矩阵与一个3列1行的矩阵相乘。但我无法解决这个问题。有人能帮我吗

代码如下:


def pivot_move(self,angle):
    phi = np.array(2 * np.pi * np.random.rand(1)) # theta in degrees
    theta = np.array(np.arccos(1 - 2* np.random.rand(1))) # phi in degrees

    u_x = np.sin(theta) * np.cos(phi)
    u_y = np.sin(theta) * np.sin(phi)
    u_z = np.cos(theta)

    """ Initializing the rotation matrix"""
    R_xx = np.cos(angle) + np.power(u_x, 2) * (1 - np.cos(angle))
    R_xy = u_x * u_y * (1 - np.cos(angle)) - u_z * np.sin(angle)
    R_xz = u_x * u_z * (1 - np.cos(angle)) + u_y * np.sin(angle)

    R_yx = u_x * u_y * (1 - np.cos(angle) + u_z * np.sin(angle))
    R_yy = np.cos(angle) + np.power(u_y, 2) * (1 - np.cos(angle))
    R_yz = u_y * u_z * (1 - np.cos(angle)) - u_x * np.sin(angle)

    R_zx = u_x * u_z * (1 - np.cos(angle)) - u_y * np.sin(angle)
    R_zy = u_y * u_z * (1 - np.cos(angle)) + u_x * np.sin(angle)
    R_zz = np.cos(angle) + np.power(u_z, 2) * (1 - np.cos(angle))

    Rot = np.array([[R_xx, R_xy, R_xz], [R_yx, R_yy, R_yz], [R_zx, R_zy,R_zz]])
    print("Rot", Rot)
    print("Rot has shape: {}".format(Rot.shape))

    pivot = np.random.randint(N - 1) + 1

    pivot_pos = np.array(self.chain[pivot].pos)
    print("pivot pos:{}".format(pivot_pos.shape))
    for i in range(pivot+1, N):
        monomer_pos = np.array([self.chain[i].pos[0], self.chain[i].pos[1], self.chain[i].pos[2]])
        print("monmer", monomer_pos)
        print("monomer shape is:{}".format(monomer_pos.shape))
        self.chain[i].pos = np.matmul(Rot, (monomer_pos - pivot_pos)) + pivot_pos 
        print("chain", self.chain[i].pos)
    return self

结果是这样的

enter image description here


Tags: posselfchainnpsincosarraypivot

热门问题