Map Reduce Advanced - Matrix Multiplication

  • + 0 comments

    Another simple solution:

    def mapper(matrix1, matrix2):
        
        for i in xrange(0, len(matrix1)):
            for j in xrange(0, len(matrix2[0])):
                
                key = (i, j)
                val1 = matrix1[i]
                val2 = [row[j] for row in matrix2]
                
                mapReducer.emitIntermediate(key, val1)
                mapReducer.emitIntermediate(key, val2)
    
    def reducer(key, list_of_values):
    
        m1, m2 = list_of_values
        val1, val2 = key
        val3 = sum([m1[i] * m2[i] for i in xrange(0, len(m1))])
        
        mapReducer.emit([val1, val2, val3])