v9.1.1
Menu

Array-to-Array Operations

easyFREEArray MathematicsNumPy MathematicsNumpy

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) as a plain Python integer

Output Format

Return a dictionary with exactly these three keys.

Example

Input
arr1 = [1, 2, 3], arr2 = [4, 5, 6]
Output
{'add': [5, 7, 9], 'multiply': [4, 10, 18], 'dot': 32}
Reasoning
  • Inputs: arr1 =[1,2,3]= [1, 2, 3] and arr2 =[4,5,6]= [4, 5, 6] are two vectors of the same length n=3n = 3. Position ii of one is paired with position ii of the other, so a1↔b1a_1 \leftrightarrow b_1, a2↔b2a_2 \leftrightarrow b_2, a3↔b3a_3 \leftrightarrow b_3.
  • add: each entry is ai+bia_i + b_i: 1+4=51+4=5, 2+5=72+5=7, 3+6=93+6=9, giving [5, 7, 9]. The result has the same length as the inputs.
  • multiply: each entry is ai⋅bia_i \cdot b_i: 1⋅4=41 \cdot 4=4, 2⋅5=102 \cdot 5=10, 3⋅6=183 \cdot 6=18, giving [4, 10, 18]. Also length 3.
  • dot: the dot product collapses those products into one number, a⋅b=∑i=1naibi=4+10+18=32\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = 4 + 10 + 18 = 32.
  • Output: a dictionary with keys add, multiply and dot. The first two are plain lists of nn numbers; dot is a single integer, a scalar rather than a vector.
  • Notice that dot is exactly the sum of the multiply entries, so the two must always agree.

Constraints

  • Both arrays will have the same shape
  • Use np.dot() for dot product

Learn this in

More from Numpy

solution.py

Test results

Sign in to run your code against the test cases.