← back

Matrix Transformation

#7 · Linear Algebra · Medium

⊣ Solve on deep-ml.com

Problem

Given a 2x2 transformation matrix and a list of 2D points, apply the matrix transformation to each point and return the transformed points.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
def transform_matrix(A: list[list[int|float]], T: list[list[int|float]], S: list[list[int|float]]) -> list[list[float]]:
    # Apply transformation: for each point, compute T * point + S (or just T * A)
    # Assuming T is the transformation matrix and A is the set of points as columns
    result = []
    n = len(T)
    m = len(A[0])
    for i in range(n):
        row = []
        for j in range(m):
            val = sum(T[i][k] * A[k][j] for k in range(len(A)))
            row.append(val)
        result.append(row)
    return result

Explanation

  1. The function performs matrix multiplication T * A where T is the transformation matrix and A contains the point vectors as columns.
  2. For each element in the result, compute the dot product of the corresponding row of T and column of A.
  3. The result is the set of transformed points.

Complexity

  • Time: O(n m k) where n, m are result dimensions and k is the shared dimension
  • Space: O(n * m) for the result matrix