矩阵乘法浮点数

时间:2016-04-07 07:59:15

标签: python

我有一个由alpha1,a1 ...... theta4表示的常量列表。

我可以正确打印并读取各个矩阵,但是当我尝试矩阵乘法时,我会收到错误;

print T1 * T2 * T3 * T4
TypeError: can't multiply sequence by non-int of type 'list'

我认为这与浮动倍增有关。

from numpy import matrix
import math

def getTransformationMatrix( alpha, a, d, theta ):
    transformationMatrix = matrix([[math.cos(theta),math.cos(alpha)*math.sin(theta),math.sin(alpha)*math.sin(theta) ,a*math.cos(theta)],  [math.sin(theta),math.cos(alpha)*math.sin(alpha)  ,-math.sin(alpha)*math.cos(theta),a*math.sin(theta)],[0,math.sin(alpha),math.cos(alpha),d],[0,0,0,1]])
    return[transformationMatrix];

T1 = getTransformationMatrix( alpha1, a1, d1, theta1)
T2 = getTransformationMatrix( alpha2, a2, d2, theta2)
T3 = getTransformationMatrix( alpha3, a3, d3, theta3)
T4 = getTransformationMatrix( alpha4, a4, d4, theta4)

print T1 * T2 * T3 * T4

1 个答案:

答案 0 :(得分:1)

您的getTransformationMatrix函数会返回一个列表,而您希望它返回一个矩阵。

我怀疑你错误地添加了这些方括号。

def getTransformationMatrix( alpha, a, d, theta ):
    transformationMatrix = matrix([[math.cos(theta),math.cos(alpha)*math.sin(theta),math.sin(alpha)*math.sin(theta) ,a*math.cos(theta)],  [math.sin(theta),math.cos(alpha)*math.sin(alpha)  ,-math.sin(alpha)*math.cos(theta),a*math.sin(theta)],[0,math.sin(alpha),math.cos(alpha),d],[0,0,0,1]])
    return [transformationMatrix];

试试这个:

def getTransformationMatrix( alpha, a, d, theta ):
    transformationMatrix = matrix([[math.cos(theta),math.cos(alpha)*math.sin(theta),math.sin(alpha)*math.sin(theta) ,a*math.cos(theta)],  [math.sin(theta),math.cos(alpha)*math.sin(alpha)  ,-math.sin(alpha)*math.cos(theta),a*math.sin(theta)],[0,math.sin(alpha),math.cos(alpha),d],[0,0,0,1]])
    return transformationMatrix

看到此错误

TypeError: can't multiply sequence by non-int of type 'list'

要做的第一件事就是不仅打印T1T2等,还打印type(T1)等。

你会发现它不是你所期望的。

相关问题