Dot Product
Problem Statement
Compute dot products and matrix multiplications.
Background
NumPy provides several multiplication operations:
- np.dot(a, b) - dot product
- a @ b - matrix multiplication (Python 3.5+)
- a * b - element-wise multiplication
For vectors: dot product = sum of element-wise products For matrices: standard matrix multiplication
Your Task
Write a function compute_products(vec1, vec2, mat) that computes various products.
Output Format
Return a dictionary with:
- "dot": Dot product of vec1 and vec2 (a plain Python int)
- "elementwise": Element-wise product of vec1 and vec2 (a plain Python list)
- "mat_vec": Matrix times vec1 (a plain Python list)
- "vec_mat": vec1 times matrix (a plain Python list)
The dictionary is printed, so values must be built-in Python types, not NumPy scalars or arrays.
Example
vec1 = [1,2,3], vec2 = [4,5,6], mat = identity
{'dot': 32, 'elementwise': [4, 10, 18], 'mat_vec': [1, 2, 3], 'vec_mat': [1, 2, 3]}- Inputs:
vec1andvec2are length-3 vectors;matis the identity matrix (ones on the diagonal, zeros elsewhere). dot: multiply matching entries and add them up: .elementwise: keep the matching products separately instead of adding them: . Notice that its entries sum to the dot product, .mat_vec: entry is , the dot product of row of the matrix withvec1. With , row picks out alone, giving .vec_mat: entry is , the dot product ofvec1with column . again gives .- Notice: the last two only agree here because is symmetric. For and , but .
Constraints
- vec1 and vec2 have same length
- mat is square with size matching vector length