Matrix Multiplication
~12 mincode completion
Implement that returns the matrix product A @ B.
Examples
2x2 matrix multiply
- Input
- matmul([[1, 2], [3, 4]], [[5, 6], [7, 8]])
- Output
- [[19, 22], [43, 50]]
Identity matrix multiply
- Input
- matmul([[1, 0], [0, 1]], [[3, 4], [5, 6]])
- Output
- [[3, 4], [5, 6]]
Row vector times column vector (inner product)
- Input
- matmul([[1, 2, 3]], [[1], [2], [3]])
- Output
- [[14]]
Hints
Hint 1
Use a matrix product rather than nested loops, and check which operand transposes.
Hint 2
Watch for this: used element wise multiply.
Requirements
A: Array of shape (m, k)B: Array of shape (k, n)Return Array of shape (m, n).
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~12 min
••••••••••••••••
8 employers weight this skill
4 quant funds, 2 defense companies, 2 autonomy companies. Top match scores 93.
Python
import numpy as np
def matmul(A: np.ndarray, B: np.ndarray) -> np.ndarray:
"""
Compute the matrix product A @ B.
Args:
A: Array of shape (m, k)
B: Array of shape (k, n)
Returns:
Array of shape (m, n).
"""
# YOUR CODE HERE
pass