TypeError when multiplying matrices
Question: I'm trying to multiply some matrices together and I keep getting a type error
for the last multiplication (calculating C). All the other multiplications
proceed correctly, but I get the error:

    
    
    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'
    

I think I may have defined the B matrix incorrectly (it is a column vector)
but I can't seem to figure out the exact cause

    
    
    # 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
    


Answer: You only have lists but need matrices. Write:

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

for all your matrices.

