Loading content...
In linear algebra, one of the most fundamental operations is the linear transformation of a vector by a matrix. This operation, also known as the matrix-vector product, transforms a vector from one space to another using the coefficients encoded in the matrix.
Given a matrix A of dimensions n × m (where n is the number of rows and m is the number of columns) and a vector v of length m, the linear transformation produces a new vector w of length n. Each element wᵢ of the resulting vector is computed as the dot product of the i-th row of the matrix with the input vector:
$$w_i = \sum_{j=1}^{m} A_{ij} \cdot v_j$$
Dimensional Compatibility: For this operation to be mathematically valid, the number of columns in the matrix must equal the length of the vector. If this condition is not met, the operation is undefined.
Your Task: Write a Python function that computes the linear transformation of a vector by a matrix. The function should:
matrix = [[1, 2], [2, 4]]
vector = [1, 2][5, 10]The matrix has 2 columns and the vector has 2 elements, so the operation is valid.
• Row 1: (1 × 1) + (2 × 2) = 1 + 4 = 5 • Row 2: (2 × 1) + (4 × 2) = 2 + 8 = 10
The resulting transformed vector is [5, 10].
matrix = [[1, 2, 3], [4, 5, 6]]
vector = [1, 2]-1The matrix has 3 columns but the vector has only 2 elements. Since 3 ≠ 2, the dimensions are incompatible and the function returns -1.
matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
vector = [5, 10, 15][5, 10, 15]This is a 3×3 identity matrix applied to a 3-element vector. The identity matrix preserves the original vector:
• Row 1: (1 × 5) + (0 × 10) + (0 × 15) = 5 • Row 2: (0 × 5) + (1 × 10) + (0 × 15) = 10 • Row 3: (0 × 5) + (0 × 10) + (1 × 15) = 15
The output equals the input vector [5, 10, 15], demonstrating the identity transformation property.
Constraints