Skip to content

Instantly share code, notes, and snippets.

@bpgergo
Created August 29, 2011 23:16
Show Gist options
  • Save bpgergo/1179673 to your computer and use it in GitHub Desktop.
Save bpgergo/1179673 to your computer and use it in GitHub Desktop.
Matrix multiplication
import operator
a = [[1, 2, 3], [4, 5, 6]]
b = [[1, 2], [3, 4], [5, 6]]
def sumprod(row, col):
return sum(reduce(operator.mul, data) for data in zip(row, col))
r = map(lambda row: map(lambda col: sumprod(row, col), zip(*b)), a)
def summ(a, b):
return str(a)+'+'+str(b)
def prodd(a):
return str(a[0])+'*'+str(a[1])
rr = map(lambda row: map(lambda col: reduce(summ, map(prodd, zip(row,col))), zip(*b)), a)
def prm(m):
for row in m:
print row
prm(a)
print "----"
prm(b)
print "----"
prm(r)
print "----"
prm(rr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment