0

いくつかの行列を掛け合わせようとしていますが、最後の掛け算 (C の計算) で型エラーが発生し続けます。他のすべての乗算は正しく続行されますが、次のエラーが発生します。

Traceback (most recent call last):
  File "C:\Python27\Lab2_MatMult_template.py", line 31, in <module>
    C = matlib.matmul(A1,B)
  File "C:\Python27\lib\site-packages\matlib.py", line 158, in matprod
    t = sum([A[i][k] * B[j] for k in range(p)])
TypeError: unsupported operand type(s) for +: 'int' and 'list'

B 行列を間違って定義した可能性があると思います (これは列ベクトルです) が、正確な原因がわかりません。

# Template for multiplying two matrices

import matlib
import math

# Use help(math) to see what functions
# the math library contains

RShoulderPitch = 0
RElbowRoll = 0

# Matrix A
A1 = [[1, 0, 0, 0], [0,1,0,0], [0,0,1,0], [0,0,0,1]]

A2 = [[-1, 0, 0, 0], [0,0,-1,0], [0,-1,0,100], [0,0,0,1]]

A3 = [[0, -1, 0, 0], [1,0,0,0], [0,0,1,98], [0,0,0,1]]

A4 = [[math.cos(-RShoulderPitch), 0, -math.sin(-RShoulderPitch), 105*math.cos(-RShoulderPitch)], [math.sin(-RShoulderPitch),0,math.cos(-RShoulderPitch),105*math.sin(-RShoulderPitch)], [0,-1,0,15], [0,0,0,1]]


# Matrix B
B = [[0],[0],[0],[2]]

T = matlib.matmul(A3,A4)

T = matlib.matmul(A2,T)

T = matlib.matmul(A1,T)

C = matlib.matmul(B,T)

print('C=')

matlib.matprint(T, format='%8.1f')


def matmul(A, B):
    """
    Computes the product of two matrices.
    2009.01.16 Revised for matrix or vector B.
    """
    m, n = matdim(A)
    p, q = matdim(B)
    if n!= p:
       return None
    try:
       if iter(B[0]):
          q = len(B[0])
    except:
       q = 1
    C = matzero(m, q)
    for i in range(m):
        for j in range(q):
            if q == 1:
               t = sum([A[i][k] * B[j] for k in range(p)])
            else:
               t = sum([A[i][k] * B[k][j] for k in range(p)])
            C[i][j] = t
    return C
4

1 に答える 1

1

リストしかありませんが、マトリックスが必要です。書く:

A1 = matlib.matrix([[1, 0, 0, 0], [0,1,0,0], [0,0,1,0], [0,0,0,1]])
A2 = matlib.matrix(...

すべてのマトリックスに。

于 2013-06-02T17:36:54.073 に答える