matrix Multiplication

David 71david at libero.it
Wed Oct 18 14:51:16 CEST 2006


Il 18 Oct 2006 04:17:29 -0700, Sssasss ha scritto:

> hi evrybody!
> 
> I wan't to multiply two square matrixes, and i don't understand why it
> doesn't work.
Can I suggest a little bit less cumbersome algorithm?

def multmat2(A,B):
    "A*B"
    if len(A)!=len(B): return "error"   # this check is not enough!
    n = range(len(A))
    C = []
    for i in n:
        C.append([0]*len(A))  # add a row to C
        for j in n:
            a = A[i]    # get row i from A
            b = [row[j] for row in B] # get col j from B
            C[i][j] = sum([x*y for x,y in zip(a,b)])
    return C

regards
D.



More information about the Python-list mailing list