numpy的基数变化 [英] Change of basis in numpy
问题描述
给出numpy中的两个基本矩阵 basis_old
和 basis_new
,库中某处有一个函数来获取转换矩阵以转换矢量 vec 在
basis_old
中的code>到在 basis_new
中的表示形式?
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
?
例如,如果我在标准基础上有一个向量 vec = [1,2,3]
[1,0,0],[0,1,0],[0,0,1]
,如何将其转换为另一个基础,例如
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)
将base_new更改为线性独立
changed basis_new to be linearly independent
推荐答案
记住将向量w1,w2,w3作为R3的基础的含义.
Remember what it means for a set of vectors w1, w2, w3 to be a basis of R3.
-
w必须为 线性独立 .这意味着x1 w1 + x2 w2 + x3 w3 = 0的唯一解决方案应该是x1 = x2 = x3 =0.但是在您的情况下,您可以验证x1 = 1,x2 = -2,x3 = 1是另一种解决方案.所以你的
basis_new
无效.
矩阵W = [w1,w2,w3]必须是可逆的.
The matrix W = [w1, w2, w3] must be invertible.
对于R3中的每个向量,必须有一种唯一方式将其写成w的线性组合.
For every vector in R3 there must be a unique way to write it as a linear combination of w's.
一旦您满足了这些要求,便可以通过简单的矩阵乘法来计算新坐标.假设您要将向量v表示为v = c1 w1 + c2 w2 + c3 w3.要以矩阵形式写,v = W c.要获得c,您要做的就是将两边都乘以W的倒数.
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
c = W^{-1} v
在numpy中,您将其写为
In numpy, you would write it as,
vec_new = np.linalg.inv(np.array([w1, w2, w3])).dot(vec_old)
这篇关于numpy的基数变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!