Loading content...
In linear algebra and numerical computing, vector addition is one of the most fundamental operations. This operation combines two vectors of equal dimensions by adding their corresponding components to produce a new vector.
Given two vectors a and b, each containing the same number of elements n, the component-wise sum produces a new vector c where each element is computed as:
$$c_i = a_i + b_i \quad \text{for } i = 1, 2, ..., n$$
Dimensional Requirement: For vector addition to be mathematically valid, both input vectors must have identical lengths. If the vectors have different dimensions, the operation is undefined and cannot be performed.
Example Visualization: Consider two 3-dimensional vectors:
Their component-wise sum produces:
Your Task: Implement a Python function that performs component-wise addition of two input vectors. The function should:
a = [1, 3]
b = [4, 5][5, 8]Both vectors have 2 elements, so the operation is valid.
• Position 1: 1 + 4 = 5 • Position 2: 3 + 5 = 8
The resulting vector is [5, 8].
a = [1, 2, 3]
b = [4, 5]-1Vector 'a' has 3 elements while vector 'b' has only 2 elements. Since 3 ≠ 2, the dimensions are incompatible and the function returns -1.
a = [1, 2, 3]
b = [4, 5, 6][5, 7, 9]Both vectors have 3 elements, so the operation is valid.
• Position 1: 1 + 4 = 5 • Position 2: 2 + 5 = 7 • Position 3: 3 + 6 = 9
The resulting vector is [5, 7, 9].
Constraints