Loading content...
The inner product (commonly known as the dot product) is one of the most fundamental operations in linear algebra and serves as a cornerstone for countless algorithms in machine learning, physics, and computer graphics.
Given two vectors u = [u₁, u₂, ..., uₙ] and v = [v₁, v₂, ..., vₙ] of equal length n, the inner product is defined as the sum of the element-wise products of their corresponding components:
$$\langle u, v \rangle = \sum_{i=1}^{n} u_i \cdot v_i = u_1 v_1 + u_2 v_2 + \cdots + u_n v_n$$
Geometric Interpretation: The inner product has a beautiful geometric meaning. For two vectors, it equals the product of their magnitudes multiplied by the cosine of the angle between them:
$$\langle u, v \rangle = |u| \cdot |v| \cdot \cos(\theta)$$
This relationship makes the inner product invaluable for measuring:
Your Task: Write a Python function that computes the inner product of two 1D NumPy-compatible arrays. The function should take two vectors as input and return their inner product as a single numeric value.
Note: You may assume both input vectors always have the same length (equal dimensions).
vec1 = [1, 2, 3], vec2 = [4, 5, 6]32The inner product is calculated by multiplying corresponding elements and summing the results:
⟨[1, 2, 3], [4, 5, 6]⟩ = (1 × 4) + (2 × 5) + (3 × 6) = 4 + 10 + 18 = 32
The function returns 32.
vec1 = [2, 3], vec2 = [4, 5]23For two 2-dimensional vectors:
⟨[2, 3], [4, 5]⟩ = (2 × 4) + (3 × 5) = 8 + 15 = 23
The resulting inner product is 23.
vec1 = [1, 2, 3, 4, 5], vec2 = [5, 4, 3, 2, 1]35For these five-dimensional vectors with symmetric but reversed values:
⟨[1, 2, 3, 4, 5], [5, 4, 3, 2, 1]⟩ = (1 × 5) + (2 × 4) + (3 × 3) + (4 × 2) + (5 × 1) = 5 + 8 + 9 + 8 + 5 = 35
Notice how the middle term (3 × 3 = 9) contributes the maximum, while the symmetric outer pairs each contribute equally (5 + 8 on both sides).
Constraints