Matrix Multiplication in Python

Pure Python:


A = np.array([[1,2,3],[4,5,6]]) # create (2 x 3) matrix

B = np.array([[7,8],[9,10],[11,12]]) # create (3 x 2) matrix

A.shape[1] == B.shape[0] # ensures two matrices are compatible

C = np.zeros((2,2)) # (2 x 2) matrix

for i in range(2):
  for k in range(2):
    for j in range(3):
      C[i,k]= C[i,k] + A[i,j]*B[j,k]

print(C)

Numpy:

import numpy as np
C = np.dot(A, B)
print(C)