Skip to content

Instantly share code, notes, and snippets.

@brentp
Created April 25, 2011 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentp/940537 to your computer and use it in GitHub Desktop.
Save brentp/940537 to your computer and use it in GitHub Desktop.
import sys
import array
# Writen by Attractive Chaos; distributed under the MIT license
# reference: http://www.syntagmatic.net/matrix-multiplication-in-python/
def matmul(a, b): # FIXME: no error checking
ra, rb = list(range(len(a))), list(range(len(b)))
c = [array.array('d', [b[j][i] for j in rb]) for i in rb]
d = [array.array('d', [0 for j in range(len(b[0]))]) for i in ra] # transpose
for i in ra:
ai = a[i]
for j in rb:
d[i][j] = sum(ai[k] * c[j][k] for k in ra)
return d
import sys
import array
# Writen by Attractive Chaos; distributed under the MIT license
# reference: http://www.syntagmatic.net/matrix-multiplication-in-python/
def matmul(a, b): # FIXME: no error checking
ra, rb = list(range(len(a))), list(range(len(b)))
c = [array.array('d', [b[j][i] for j in rb]) for i in rb]
d = [array.array('d', [0 for j in range(len(b[0]))]) for i in ra] # transpose
for i in ra:
ai = a[i]
for j in rb:
d[i][j] = sum(ai[k] * c[j][k] for k in ra)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment