Array-to-Array Operations
Problem Statement
Perform element-wise operations between two arrays.
Background
When arrays have the same shape, operations are element-wise:
arr1 + arr2 # Element-wise addition
arr1 * arr2 # Element-wise multiplication
Your Task
Write a function array_operations(arr1, arr2) that returns a dictionary with:
- "add": Element-wise addition as list
- "multiply": Element-wise multiplication as list
- "dot": Dot product (sum of element-wise multiplication)
Output Format
Return a dictionary with exactly these three keys.
Example:
arr1 = [1, 2, 3], arr2 = [4, 5, 6]
{'add': [5, 7, 9], 'multiply': [4, 10, 18], 'dot': 32}add: [1+4, 2+5, 3+6], multiply: [14, 25, 3*6], dot: 4+10+18=32
Constraints:
- Both arrays will have the same shape
- Use np.dot() for dot product
Background Knowledge for Array-to-Array Operations
1. Background Knowledge
NumPy Arrays and Element-wise Operations
NumPy arrays are the standard representation for numerical data in Python and enable efficient implementation of numerical computations in a high-level language. Unlike Python lists, NumPy arrays support vectorized operations—computations that operate on entire arrays without explicit loops.
Element-wise operations apply a function to corresponding elements in two arrays of the same shape. For arrays A and B:
- Element-wise addition: C[i]=A[i]+B[i]
- Element-wise multiplication: C[i]=A[i]×B[i]
These operations are fundamentally different from matrix operations like the dot product, which combines elements across dimensions.
Dot Product
The dot product (also called scalar product or inner product) between two 1D arrays is:
dot(A,B)=i=0∑n−1​A[i]×B[i]For the sample input [1, 2, 3] and [4, 5, 6]: 1×4+2×5+3×6=4+10+18=32
2. Algorithm Approach
Vectorization Technique
Vectorization is a technique for converting an algorithm that operates on single values to one that operates on collections of values simultaneously. NumPy performs vectorized calculations efficiently by:
- Avoiding explicit loops: Operations are compiled into optimized C code
- Minimizing memory copies: Data is processed in-place when possible
- Reducing operation counts: Batch operations are faster than iterative approaches
Implementation Strategy
For this problem, leverage NumPy's built-in operators:
-
- operator for element-wise addition
-
- operator for element-wise multiplication
- np.dot() for dot product computation
3. Step-by-Step Strategy
Step 1: Import NumPy
import numpy as np
Step 2: Perform element-wise addition
add_result = arr1 + arr2
Step 3: Perform element-wise multiplication
multiply_result = arr1 * arr2
Step 4: Calculate dot product
dot_result = np.dot(arr1, arr2)
Step 5: Convert NumPy arrays to lists and return as dictionary
return {
"add": add_result.tolist(),
"multiply": multiply_result.tolist(),
"dot": dot_result.item() # or int(dot_result) for scalar
}
Complete Solution
import numpy as np
def array_operations(arr1, arr2):
add_result = arr1 + arr2
multiply_result = arr1 * arr2
dot_result = np.dot(arr1, arr2)
return {
"add": add_result.tolist(),
"multiply": multiply_result.tolist(),
"dot": int(dot_result)
}
4. Common Pitfalls
- Forgetting .tolist() conversion: The problem requires lists in the output, not NumPy arrays. Use .tolist() to convert.
- Confusing * with matrix multiplication: In NumPy, * performs element-wise multiplication, not matrix multiplication (use @ or np.dot() for that).
- Incorrect dot product calculation: Ensure you use np.dot() as specified, not manual summation of element-wise products.
- Type mismatch for scalar dot product: The dot product returns a NumPy scalar; convert it to a Python int using .item() or direct casting.
- Assuming different shapes: The problem guarantees same-shaped arrays, but always validate in production code.
5. Time & Space Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Element-wise addition | O(n) | O(n) |
| Element-wise multiplication | O(n) | O(n) |
| Dot product | O(n) | O(1) |
| Overall | O(n) | O(n) |
Where n is the total number of elements in the arrays. NumPy's vectorized operations execute in linear time with respect to array size, making them far more efficient than Python loops for large arrays.