matrix Multiplication

Sssasss reivax85 at gmail.com
Wed Oct 18 12:51:18 EDT 2006


David wrote:
> 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.

This one is really nice, i didn't knew the zip function, thank you
ciao




More information about the Python-list mailing list