← back

Transformation Matrix from Basis B to C

#27 · Linear Algebra · Easy

⊣ Solve on deep-ml.com

Problem

Given a vector expressed in basis B, find its representation in basis C. Compute the transformation matrix from basis B to basis C and apply it.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import numpy as np

def change_of_basis(B: list[list[float]], C: list[list[float]],
                     v: list[float]) -> list[float]:
    B_mat = np.array(B, dtype=float).T  # columns are basis vectors
    C_mat = np.array(C, dtype=float).T  # columns are basis vectors

    # Transformation matrix from B to C: C_inv * B
    C_inv = np.linalg.inv(C_mat)
    T = C_inv @ B_mat

    # Transform the vector
    v = np.array(v, dtype=float)
    result = T @ v

    return np.round(result, 4).tolist()

Explanation

  1. Arrange basis vectors of B and C as columns of matrices B_mat and C_mat.
  2. A vector v_B in basis B has standard coordinates B_mat * v_B.
  3. To express it in basis C, we need C_inv B_mat v_B.
  4. So the transformation matrix is T = C^(-1) B, and the result is T v.

Complexity

  • Time: O(n^3) for the matrix inverse, where n is the dimension
  • Space: O(n^2) for the matrices