Can you implement matrix multiplication in python. Please write the most beautiful code…
- User
Can you implement matrix multiplication in python. Please write the most beautiful code you can.
Assistant
Here is an implementation of matrix multiplication in Python with a focus on elegance and readability:
pythonimport numpy as np def matmul(A, B): """Multiply two matrices A and B""" rows_A = len(A) cols_A = len(A[0]) rows_B = len(B) cols_B = len(B[0]) if cols_A != rows_B: raise ValueError('Number of columns in A must match number of rows in B.') C = [[0 for _ in range(cols_B)] for _ in range(rows_A)] for i in range(rows_A): for j in range(cols_B): for k in range(cols_A): C[i][j] += A[i][k] * B[k][j] return np.array(C)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.