Vector Dot Product
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] and b=[b1,b2,...,bn] can be calculated by multiplying corresponding elements and summing them up. To achieve this, we can follow these steps:
- Initialize a variable to store the sum of products.
- Iterate over the elements of both vectors in parallel.
- For each pair of elements, multiply them and add the result to the sum.
This technique is widely used in image processing.
Example:
dot_product([1, 2, 3], [4, 5, 6])
32.0000
Step-by-step calculation using the dot product formula:
a⋅b=∑i=1nai×bi
-
Identify corresponding elements:
- a=[1,2,3]
- b=[4,5,6]
-
Multiply corresponding elements:
- Position 0: 1×4=4
- Position 1: 2×5=10
- Position 2: 3×6=18
-
Sum all products: a⋅b=4+10+18=32
-
Result: 32.0000
The dot product measures how "aligned" two vectors are. When vectors point in the same direction, the result is large and positive.
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