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 (scalar)
- "elementwise": Element-wise product of vec1 and vec2 (list)
- "mat_vec": Matrix times vec1 (list)
- "vec_mat": vec1 times matrix (list)
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]}Dot: 14+25+36=32, elementwise: [14, 25, 36], identity matrix preserves vectors
Constraints:
- vec1 and vec2 have same length
- mat is square with size matching vector length
Background Knowledge
The dot product of two vectors a=[a1,a2,…,an] and b=[b1,b2,…,bn] is a scalar computed as a⋅\vec{b}=\sum_{i=1}^n a_i b_i.Geometrically,itequals|\vec{a}∣∣\vec{b}∣cosθ, measuring projection and similarity. In NumPy, np.dot(vec1, vec2) computes this efficiently for 1D arrays as a sum of element-wise products.
Element-wise multiplication (Hadamard product) multiplies corresponding elements: a⊙\vec{b}=[a1b1,a2b2,…,anbn], using vec1 * vec2. Matrix-vector multiplication Mv produces a vector where each entry is a dot product of a matrix row with v. Vector-matrix multiplication vM uses the matrix column with v. NumPy's @ operator (or np.dot) handles these, with broadcasting aligning shapes automatically.
These operations underpin linear algebra: dot products enable projections, while matrix products model transformations like rotations.
Algorithm/Approach
Use NumPy's built-in functions for efficiency:
- Scalar dot product: np.dot(vec1, vec2) or vec1 @ vec2.
- Element-wise: vec1 * vec2, convert to list.
- Matrix-vector: mat @ vec1.
- Vector-matrix: vec1 @ mat.
Return a dictionary mapping keys to results, ensuring outputs match types (scalar for dot, lists for vectors).
Step-by-Step Strategy
- Validate inputs: Assume vec1, vec2 are 1D NumPy arrays of equal length; mat is 2D with compatible shape (e.g., n×n for identity-like sample).
- Compute dot product: Use np.dot(vec1, vec2) for scalar result.
- Compute element-wise: Multiply vec1 * vec2, convert to Python list with .tolist().
- Matrix-vector product: mat @ vec1, convert to list.
- Vector-matrix product: vec1 @ mat, convert to list.
- Package results: Create dict with keys "dot", "elementwise", "mat_vec", "vec_mat".
- Test shapes: Verify vec1.shape == mat.shape for compatibility.
Common Pitfalls
- Output types: Dot must be scalar (float/int), others lists—not NumPy arrays.
- Shape mismatches: vec1 @ mat fails if vec1 length ≠ mat rows; check dimensions.
- Operator precedence: @ for matmul vs * for element-wise—mixing causes errors.
- 1D array behavior: NumPy treats 1D as vectors; np.dot auto-handles, but explicit shapes prevent issues.
- No imports in function: Assume numpy as np is imported outside; don't re-import.
Time & Space Complexity
- Time: O(n) per operation (dot/element-wise: n multiplies + sum; mat-vec: O(n2) for n×n matrix). Total: O(n2).
- Space: O(n) for output vectors/lists; dictionary overhead negligible. NumPy operations are in-place efficient.