Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change of basis in numpy

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

like image 492
Isaac Khor Avatar asked Mar 09 '19 23:03

Isaac Khor


Video Answer


1 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.array([w1, w2, w3])).dot(vec_old)
like image 82
darksky Avatar answered Sep 28 '22 11:09

darksky