Matrices are not just boxes of numbers; they are spatial machines. Every 2×2 matrix defines a linear transformation — a rule that stretches, rotates, reflects, or shears the entire coordinate plane. Adjust the transformation matrix below and watch how the grid, the unit square, and the basis vectors physically warp in real-time.
x' = w₁₁·x + w₁₂·y | y' = w₂₁·x + w₂₂·y
Task: Apply a 2×2 transformation matrix M = [[2, 1], [0, 3]]
to a target vector v = [2, 3] using NumPy. Print the resulting transformed vector.
Import numpy. Create the matrix as a 2D array: np.array([[...], [...]]).
Create the vector as a 1D array: np.array([...]).
Use np.dot(matrix, vector) or the @ operator to multiply them.
NumPy handles matrix multiplication for us. Here is the exact expansion:
import numpy as np
# The transformation matrix (2x2)
M = np.array([[2, 1],
[0, 3]])
# The target vector (1D array, treated as column)
v = np.array([2, 3])
# Matrix-vector multiplication
# Row 1 of M dot v: (2*2) + (1*3) = 7
# Row 2 of M dot v: (0*2) + (3*3) = 9
result = np.dot(M, v) # or M @ v
print(result) # Output: [7 9]
Task: Manually calculate the new coordinates of vector v = [2, 3]
after applying the matrix M = [[2, 1], [0, 3]], without using NumPy.
The new x-coordinate is (w₁₁ * x) + (w₁₂ * y).
The new y-coordinate is (w₂₁ * x) + (w₂₂ * y).
You are multiplying the first matrix row by the vector column to get x', and the second row to get y'.
Matrix-vector multiplication, element by element:
# Define the matrix entries
w11, w12 = 2, 1
w21, w22 = 0, 3
# Define the original vector
x, y = 2, 3
# Row 1 of M times the column vector v:
# new_x = (w11 * x) + (w12 * y) = (2*2) + (1*3) = 4 + 3 = 7
new_x = (w11 * x) + (w12 * y)
# Row 2 of M times the column vector v:
# new_y = (w21 * x) + (w22 * y) = (0*2) + (3*3) = 0 + 9 = 9
new_y = (w21 * x) + (w22 * y)
print(f"Transformed vector: [{new_x}, {new_y}]")
# Output: Transformed vector: [7, 9]