v9.1.1
Menu

Vector Dot Product

easyFREEVectorsLinear AlgebraCV: Mathematical Foundations

Implement a function to compute the dot product of two vectors a and b. The dot product, a fundamental concept in linear algebra, is used to measure the similarity between two vectors, which is crucial in various computer vision applications.

The dot product of two vectors a=[a1,a2,...,an]\mathbf{a} = [a_1, a_2,..., a_n] and b=[b1,b2,...,bn]\mathbf{b} = [b_1, b_2,..., b_n] can be calculated by multiplying corresponding elements and summing them up. Return the result rounded to 4 decimal places.

a⋅b=∑i=1naibi=a1b1+a2b2+...+anbn\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i b_i = a_1 b_1 + a_2 b_2 +... + a_n b_n

This technique is widely used in image processing.

Example

Input
dot_product([1, 2, 3], [4, 5, 6])
Output
32
Reasoning
  • Inputs: a=[1,2,3]\mathbf{a} = [1, 2, 3] and b=[4,5,6]\mathbf{b} = [4, 5, 6] are two vectors of the same length n=3n = 3; entry aia_i is paired with the entry bib_i in the same position.
  • Pairwise products: a1b1=1×4=4a_1 b_1 = 1 \times 4 = 4, a2b2=2×5=10a_2 b_2 = 2 \times 5 = 10, a3b3=3×6=18a_3 b_3 = 3 \times 6 = 18.
  • Sum: a⋅b=4+10+18=32\mathbf{a} \cdot \mathbf{b} = 4 + 10 + 18 = 32.
  • Output: a single scalar, 32. Rounding to 4 decimal places leaves it unchanged because every input is a whole number, so no decimals appear.
  • Notice that the result collapses two vectors into one number. Large positive values mean the vectors point in similar directions; perpendicular vectors such as [1,0][1, 0] and [0,1][0, 1] give exactly 00, and a vector dotted with itself, e.g. [3,4]⋅[3,4]=9+16=25[3, 4] \cdot [3, 4] = 9 + 16 = 25, gives its squared length.

Constraints

  • Both vectors have the same length n where 1 ≤ n ≤ 1000
  • Vector elements are floating-point numbers
  • Return the result rounded to 4 decimal places

Learn this in

More from CV: Mathematical Foundations

solution.py

Test results

Sign in to run your code against the test cases.