PIXELBANKv8.2.1
Menu

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]\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. To achieve this, we can follow these steps:

  1. Initialize a variable to store the sum of products.
  2. Iterate over the elements of both vectors in parallel.
  3. For each pair of elements, multiply them and add the result to the sum.
ab=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.0000
Reasoning:

Step-by-step calculation using the dot product formula:

ab=i=1nai×bi\mathbf{a} \cdot \mathbf{b} = \sum_{i=1}^{n} a_i \times b_i

  1. Identify corresponding elements:

    • a=[1,2,3]\mathbf{a} = [1, 2, 3]
    • b=[4,5,6]\mathbf{b} = [4, 5, 6]
  2. Multiply corresponding elements:

    • Position 0: 1×4=41 \times 4 = 4
    • Position 1: 2×5=102 \times 5 = 10
    • Position 2: 3×6=183 \times 6 = 18
  3. Sum all products: ab=4+10+18=32\mathbf{a} \cdot \mathbf{b} = 4 + 10 + 18 = 32

  4. Result: 32.000032.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
Editor

Test Results

0/0
Run code to see test results.