matrix Multiplication

Sssasss reivax85 at gmail.com
Wed Oct 18 13:17:29 CEST 2006


hi evrybody!

I wan't to multiply two square matrixes, and i don't understand why it
doesn't work.
Could you explain me?

def multmat(A,B):
    "A*B"
    if len(A)!=len(B): return "error"
    D=[]
    C=[]
    for i in range(len(A)): D.append(0)
    for i in range(len(A)): C.append(D)
    for i in range(len(A)):
        for j in range(len(A)):
            for k in range(len(A)):
                C[i][j]+=A[i][k]*B[k][j]
		print C[i][j]
	print C[i]
    return C

when i use it on :
>>> A=[[2,3,4],[5,8,6],[4,5,7]]
>>> B=[[1,0,0],[0,1,0],[0,0,1]]

I get :
2
2
2
0
3
3
0
0
4
[2, 3, 4]
7
7
7
3
11
11
4
4
10
[7, 11, 10]
11
11
11
11
16
16
10
10
17
[11, 16, 17]
[[11, 16, 17], [11, 16, 17], [11, 16, 17]]

thank you




More information about the Python-list mailing list