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.

a = [[1,2,3],[1,2,3],[1,2,3]] b = [[3,3,3],[3,3,3],[3,3,3]] # without using numpy def matmul(a, b): r = [[0,0,0], [0,0,0], [0,0,0]] for i in range(len(a)): for j in range(len(a[0])): for k in range(len(a[0])): r[i][j] += a[i][k] * b[k][j] return r matmul(a, b) # using numpy import numpy as np def matmul_np(a, b): return np.matmul(a, b) matmul_np(np.array(a), np.array(b))

Questions about programming?Chat with your personal AI assistant