Change of basis in numpy

走远了吗. 提交于 2020-05-31 04:20:14

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/55082928/change-of-basis-in-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!