2016-04-07 237 views
0

我有一個由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

您的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)

打印

你會看到它不是你所期望的。

+0

完美謝謝!並感謝您的類型()提示! – Tommy

+0

很高興我幫了忙。歡迎來到SO。 –