You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Value Error:无法将形状(3,)的输入数组广播至形状(3,1)求助

问题:赋值4x4矩阵列时触发ValueError

尝试替换4x4单位矩阵第4列的前3个元素,执行T[0:3, 3] = self.link_translation[joint_name]时出现ValueError,但单独测试T[0:3, 3] = [0,1,2]却能正常运行,排除语法错误可能。

相关代码

class ForwardKinematicsAgent(PostureRecognitionAgent):
    def __init__(self, simspark_ip='localhost',
                 simspark_port=3100,
                 teamname='DAInamite',
                 player_id=0,
                 sync_mode=True):
        super(ForwardKinematicsAgent, self).__init__(simspark_ip, simspark_port, teamname, player_id, sync_mode)
        self.transforms = {n: identity(4) for n in self.joint_names}

        # chains defines the name of chain and joints of the chain
        self.chains = {'Head': ['HeadYaw', 'HeadPitch'],
                       'LArm': ['LShoulderPitch']}
        
        self.link_translation = {'HeadYaw': [0., 0., 126.50], 'HeadPitch':  [0., 0., 0.], 'LShoulderPitch': [0., 98., 100.]}

    def think(self, perception):
        self.forward_kinematics(perception.joint)
        return super(ForwardKinematicsAgent, self).think(perception)

    def local_trans(self, joint_name, joint_angle):
        '''calculate local transformation of one joint

        :param str joint_name: the name of joint
        :param float joint_angle: the angle of joint in radians
        :return: transformation
        :rtype: 4x4 matrix
        '''
        T = identity(4)
        c = cos(joint_angle)
        s = sin(joint_angle)
        if(joint_name.find('Roll') != -1):                #find returns index of searched string or -1 if not found
            R = matrix([[1, 0, 0], [0,c,-s], [0, s, c]])  #if we have roll we have Rx matrix
        if(joint_name.find('Pitch') != -1):    
            R = matrix([[c, 0, s], [0, 1, 0], [-s, 0, c]]) #pitch -> Ry
        if(joint_name.find('Yaw') != -1):
            R = matrix([[c, s, 0], [-s, c, 0], [0, 0, 1]]) #Yaw -> Rz

        T[0:3, 0:3] = R  #insert appropriate rotation matrix in top left of transform matrix
        T[0:3, 3] = self.link_translation[joint_name] #insert the spatial translation of the link in 4th colum of the T-matrix
        return T

    def forward_kinematics(self, joints):
        '''forward kinematics

        :param joints: {joint_name: joint_angle}
        '''
        for chain_joints in self.chains.values():
            T = identity(4)
            for joint in chain_joints:
                angle = joints[joint]
                Tl = self.local_trans(joint, angle)
                T = T * Tl              #matrix multiplication T0_N = T0_1 * T1_2 * T1_3 etc
                self.transforms[joint] = T

原因分析

从代码里的identity(4)和matrix调用来看,你应该是用了sympy库的矩阵对象。sympy的Matrix在切片赋值时,对右侧数据的类型要求更严格:

  • 直接用[0,1,2]这种字面量列表时,sympy会自动转换匹配的格式;
  • 但从字典里取出的列表self.link_translation[joint_name],可能因为存储或传递过程中的隐式类型差异,导致sympy无法直接识别,触发维度不匹配的ValueError。

解决办法

方法1:将列表转换为sympy列矩阵

把赋值语句改成:

from sympy import Matrix
# ...
T[0:3, 3] = Matrix(self.link_translation[joint_name])

通过显式转换为sympy的Matrix对象,确保类型和维度完全匹配。

方法2:逐个元素赋值

如果不想引入额外转换,也可以直接对每个位置单独赋值:

trans = self.link_translation[joint_name]
T[0, 3] = trans[0]
T[1, 3] = trans[1]
T[2, 3] = trans[2]

这种方式完全避开切片赋值的类型限制,逻辑更直观。

方法3:确认矩阵库类型

如果实际使用的是numpy矩阵(而非sympy),检查identity(4)是否是numpy.identity,此时可以尝试将列表转为numpy数组:

import numpy as np
# ...
T[0:3, 3] = np.array(self.link_translation[joint_name])

内容的提问来源于stack exchange,提问作者JooJoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 08:05:02