0

Hello (excuse my English), I have a big doubt in python with matrix multiplication, I create a list of lists and multiplied by a scaling matrix, this is what I've done and I can not alparecer perform a multiplication operation problem with indexes, I check with paper and pencil and it works, I'm doing something bad to accommodate indexes or am I wrong accommodating matrices from the beginning?

def main():
if len(sys.argv) > 1:
    v = int(sys.argv[1])
else:
    print "error python exe:"
    print "\tpython <programa.py> <num_vertices>"
A = []
for i in range(v):
    A.append([0]*2)
for i in range(v):
    for j in range(2):
        A[i][j] = input("v: ")  


print A 
    Escala(A)

def Escala(A):
    print "Escala"
    sx = input("Sx: ")
    sy = input("Sy: ")
    S = [(sx,0),(0,sy)]
    print S
    M = mult(S,A)
    print M



def mult(m1,m2):
M = zero(len(m1),len(m2[0])) 
    for i in range(len(m2)):   
        for j in range(len(m2[0])): 
            for k in range(len(m1)): 
                M[i][j] += m1[k][j]*m2[k][j]
    print M                 
    return M

def zero(m,n):
# Create zero matrix
new_matrix = [[0 for row in range(n)] for col in range(m)]
return new_matrix
2
  • 1
    Am fairly sure DJV's answer is correct. You might want to check before doing the multiplication that the matrix dimensions allow multiplication though.
    – will
    Feb 21, 2013 at 13:54
  • 1
    why aren't you using numpy? Feb 21, 2013 at 14:24

1 Answer 1

5

This seems wrong to me:

M[i][j] += m1[k][j]*m2[k][j]

shouldn't it be:

M[i][j] += m1[i][k]*m2[k][j]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.