1# Program to multiply two matrices using list comprehension
2
3# 3x3 matrix
4X = [[12,7,3],
5 [4 ,5,6],
6 [7 ,8,9]]
7
8# 3x4 matrix
9Y = [[5,8,1,2],
10 [6,7,3,0],
11 [4,5,9,1]]
12
13# result is 3x4
14result = [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*Y)] for X_row in X]
15
16
1a = np.array([[-6, 1], [1, 1]])
2b = np.array([[0], [8]])
3
4c = a.dot(b) # multiply matrice a and b
1>>> a = np.ones([9, 5, 7, 4])
2>>> c = np.ones([9, 5, 4, 3])
3>>> np.dot(a, c).shape
4(9, 5, 7, 9, 5, 3)
5>>> np.matmul(a, c).shape
6(9, 5, 7, 3)
7>>> # n is 7, k is 4, m is 3
8