Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

36. Change of basis

easy
GeneralGeneral
senior

Implement change of basis for a vector, which lets you express the same geometric vector using different coordinate systems. You’ll compute the coordinates of a vector in a new basis using a basis matrix.

[v]B=B−1v[v]_{B} = B^{-1} v

Requirements

Implement the function

python

Rules:

  • Treat B as a matrix whose columns are the basis vectors.
  • Compute the new coordinates using a linear solve (preferred) or matrix inverse.
  • Return the result as a NumPy array.
  • Do not use any helper libraries beyond NumPy and Python built-ins.
  • Assume B is invertible.

Example

python

Output:

python
Input Signature
ArgumentType
Bnp.ndarray
vnp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Use np.linalg.solve; avoid explicit inverse.

  • Return NumPy array.

  • B is (n,n) invertible; v length n.

Hint 1

Treat the columns of B as basis vectors. The new coordinates x satisfy B @ x = v.

Hint 2

Avoid forming B^{-1} explicitly. Use np.linalg.solve(B, v) to compute x more stably.

Hint 3

Convert inputs to NumPy arrays with dtype=float (if not already), solve for x, then return x directly.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
linear-algebra
change-of-basis
numpy-linalg
linear-system
10 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit