matrix multiplication python without numpy

Solutions on MaxInterview for matrix multiplication python without numpy by the best coders in the world

showing results for - "matrix multiplication python without numpy"
Emanuele
25 Jan 2017
1The Numpythonic approach: (using numpy.dot in order to get the dot product of two matrices)
2
3In [1]: import numpy as np
4
5In [3]: np.dot([1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]])
6Out[3]: array([1, 1])
7The Pythonic approach:
8
9The length of your second for loop is len(v) and you attempt to indexing v based on that so you got index Error . As a more pythonic way you can use zip function to get the columns of a list then use starmap and mul within a list comprehension:
10
11In [13]: first,second=[1,0,0,1,0,0], [[0,1],[1,1],[1,0],[1,0],[1,1],[0,1]]
12
13In [14]: from itertools import starmap
14
15In [15]: from operator import mul
16
17In [16]: [sum(starmap(mul, zip(first, col))) for col in zip(*second)]
18Out[16]: [1, 1]