Created
August 29, 2011 23:16
-
-
Save bpgergo/1179673 to your computer and use it in GitHub Desktop.
Matrix multiplication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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