Change of basis in numpy

Question:

Given two basis matrices basis_old and basis_new in numpy, is there a function somewhere in the library to get the transformation matrix to convert a vector vec in basis_old to its representation in basis_new?

For example, if I have a vector vec = [1,2,3] in the standard basis [1,0,0], [0,1,0], [0,0,1], how to I convert it to another basis, say,

e1 = [1 0 0]
e2 = [0 0 1]
e3 = [0 1 0]
basis_new = np.array([e1, e2, e3])

# I want something like this
vec_new = np.linalg.change_of_basis(vec_old, basis_old, basis_new)

# Or this:
transformation_matrix = np.linalg.basis_change(basis_old, basis_new)

Edit: changed basis_new to be linearly independent

Asked By: Isaac Khor

||

Answers:

Remember what it means for a set of vectors w1, w2, w3 to be a basis of R3.

  1. The w’s must be linearly independent. That means the only solution to x1 w1 + x2 w2 + x3 w3 = 0 should be x1 = x2 = x3 = 0. But in your case, you can verify that x1 = 1, x2 = -2, x3 = 1 is another solution. So your basis_new is not valid.

  2. The matrix W = [w1, w2, w3] must be invertible.

  3. For every vector in R3 there must be a unique way to write it as a linear combination of w’s.

Once you have nailed these requirements for a basis, then you can compute the new coordinates by a simple matrix multiplication. Suppose you want to express vector v as v = c1 w1 + c2 w2 + c3 w3. To write this in matrix form, v = W c. To get c, all you have to do is to multiply both side by the inverse of W.

c = W^{-1} v

In numpy, you would write it as,

vec_new = np.linalg.inv(np.column_stack((w1, w2, w3))).dot(vec_old)
Answered By: darksky
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.