gpt4 book ai didi

python - 大矩阵对角化python

转载 作者:行者123 更新时间:2023-12-02 17:31:15 25 4
gpt4 key购买 nike

我在 scipy 中使用 linalg 来获取 155X156 矩阵的特征值和特征向量。然而,与矩阵相比,特征值的顺序似乎是随机的。我希望第一个特征值对应于矩阵中的第一个数字。请看下面我的例程。我首先读取一个包含所有 float 的文件(1_1o.dat)

 2533297.650278 -2373859.531153      
37695.845843 425449.129032

然后我将它们作为数组读取

  [2533297.650278, -2373859.531153,      37695.845843,     425449.129032...]

然后 reshape 为 156X156 矩阵。我希望按照读取矩阵的顺序打印所有特征值和相应的特征向量。我知道最后我的特征值(156 个数字)应该从小到大列出,而不是像当前例程那样随机排列。当然,相应特征向量的顺序相同。谁能帮我解决这个问题?

谢谢。

from scipy import linalg
from scipy.linalg import *
file2 = open('1_1f.dat', 'w')
with open('1_1o.dat', 'rU') as file:
File = file.readlines()
nums2 = np.array(File)
nums2 = [float(i.rstrip('\n')) for i in nums2[0].split()]
nums2 = np.reshape(nums2, (156, 156))
print eig(nums2)
print >> file2, eig(nums2)
file2.close()

最佳答案

根据np.linalg.eig部分的文档:Returns, w: (...,M) array:

The eigenvalues, each repeated according to its multiplicity. The eigenvalues are not necessarily ordered. The resulting array will be always be of complex type. When a is real the resulting eigenvalues will be real (0 imaginary part) or occur in conjugate pairs

显示了如何对值进行排序 here并为了完整性而复制:

import numpy as np
A = np.array([[3,1,-1], [1,3,-1], [-1,-1,5]])
w,v = np.linalg.eig(A)
print w
idx = w.argsort()[::-1] #large to small
# idx = w.argsort() #small to large
w = w[idx]
v = v[:,idx]
print w # now they are ordered and you can iterate through your results to write them to your file

关于python - 大矩阵对角化python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037616/

25 4 0
文章推荐: ruby-on-rails - rails 和 Shopify : Is there any way to see a full list of Shopify API commands?
文章推荐: laravel - 将两列显示为一列
文章推荐: html - html