generate functionTue, 28 Feb 2023

Matrix multiplication Write two functions that each multiply two square matrices, one without using numpy's built-in functions, and the other using numpy. The input to the first task is given lists of size size by size elements in each. The second task receives objects of type np.ndarray --- square matrices of the same size. The first function should return a list of lists, and the second should return np.array.

def my_matmul(X, Y): result = [[0 for i in range(len(Y[0]))] for j in range(len(X))] for i in range(len(X)): for j in range(len(Y[0])): for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] return result my_matmul([[1,2], [3,4]], [[2,0], [1,2]]) def numpy_matmul(X, Y): return np.matmul(X, Y) numpy_matmul(np.array([[1,2], [3,4]]), np.array([[2,0], [1,2]]))

Questions about programming?Chat with your personal AI assistant