scipy.sparse.linalg.eigsh返回同一矩阵的不同特征值
问题摘要
我想计算矩阵的最小特征值(代数值)。矩阵来自我使用 pynastran 库的OP4文件。以下这些指令,我正在尝试使用最小的eigevanlue计算最小scipy.sparse.linalg.eigsh
具有换挡模式的功能。为了检查计算是否正确,我将eigsh
的结果与numpy.linalg.eigvals的结果进行了比较。我观察到的很令人困惑:如果我简单地将
eigsh
应用于矩阵,则计算出的特征值是错误的,如果我将矩阵保存到CSV文件,然后将其加载到numpy中
数组特征值正确。更令人困惑的是,当我比较两个矩阵时,numpy.Array_equal
返回true
。 eigsh
如何返回同一矩阵的两个不同结果?
Code
from pyNastran.op4.op4 import read_op4
from scipy.sparse.linalg import eigsh
import numpy as np
op4 = read_op4('kllrh.op4')
matrix_name = 'KLLRH'
kllrh_matrix = op4[matrix_name][1][-1]
reference_max_eigenvalue = np.max(np.linalg.eigvals(kllrh_matrix))
reference_min_eigenvalue = np.min(np.linalg.eigvals(kllrh_matrix))
eigsh_min_eigenvalue = eigsh(kllrh_matrix, 1, sigma=0, which='LM', return_eigenvectors=False)
np.savetxt('kllrh.csv', kllrh_matrix, delimiter=',')
kllrh_matrix_reloaded = np.loadtxt('kllrh.csv', delimiter=",")
reloaded_eigsh_min_eigenvalue = eigsh(kllrh_matrix_reloaded, 1, sigma=0, which='LM', return_eigenvectors=False)
print(reference_min_eigenvalue)
print(eigsh_min_eigenvalue)
print(reloaded_eigsh_min_eigenvalue)
print(np.array_equal(kllrh_matrix, kllrh_matrix_reloaded))
print(type(kllrh_matrix))
print(type(kllrh_matrix_reloaded))
print(reference_max_eigenvalue)
This returns the following:
-0.0028387385
[0.05363945]
[-0.00283876]
True
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
6502976000.0
Please find the kllrh.op4
file
Problem summary
I want to calculate the smallest eigenvalue (algebraic value) of a matrix. The matrix comes from an op4 file that I read using the pyNastran library. Following these instructions, I am trying to calculate the smallest eigevanlue using the scipy.sparse.linalg.eigsh
function with shift-invert mode. In order to check that the computation is correct, I compare the result of eigsh
with the result of numpy.linalg.eigvals
. What I observe is quite puzzling: if I simply apply eigsh
to the matrix, the calculated eigenvalue is wrong, if I save the matrix to a csv file and then I load it back into a numpy
array the eigenvalue is correct. What is even more baffling is that numpy.array_equal
returns True
when I compare the two matrices. How is it possible that eigsh
returns two different results for the same matrix?
Code
from pyNastran.op4.op4 import read_op4
from scipy.sparse.linalg import eigsh
import numpy as np
op4 = read_op4('kllrh.op4')
matrix_name = 'KLLRH'
kllrh_matrix = op4[matrix_name][1][-1]
reference_max_eigenvalue = np.max(np.linalg.eigvals(kllrh_matrix))
reference_min_eigenvalue = np.min(np.linalg.eigvals(kllrh_matrix))
eigsh_min_eigenvalue = eigsh(kllrh_matrix, 1, sigma=0, which='LM', return_eigenvectors=False)
np.savetxt('kllrh.csv', kllrh_matrix, delimiter=',')
kllrh_matrix_reloaded = np.loadtxt('kllrh.csv', delimiter=",")
reloaded_eigsh_min_eigenvalue = eigsh(kllrh_matrix_reloaded, 1, sigma=0, which='LM', return_eigenvectors=False)
print(reference_min_eigenvalue)
print(eigsh_min_eigenvalue)
print(reloaded_eigsh_min_eigenvalue)
print(np.array_equal(kllrh_matrix, kllrh_matrix_reloaded))
print(type(kllrh_matrix))
print(type(kllrh_matrix_reloaded))
print(reference_max_eigenvalue)
This returns the following:
-0.0028387385
[0.05363945]
[-0.00283876]
True
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
6502976000.0
Please find the kllrh.op4
file here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然
np.array_equal
说kllrh_matrix
和kllrh_matrix_reloaded
是相等的,它们具有不同的dtype
s(s(
>
) float32
vsfloat64
)。如果您执行
所有操作都是正确的:
作为更好的替代方案,可以在阅读OP4文件时指定
precision
:它将给您一些其他结果:
默认精度来自OP4文件:在示例中矩阵类型是
1
(文件的第一行上的第四个数字)结果单个精度(float32
)。Although
np.array_equal
says thatkllrh_matrix
andkllrh_matrix_reloaded
are equal, they are of differentdtype
s (float32
vsfloat64
).If you do
everything is correct:
As a better alternative, you can specify the
precision
when reading the op4 file:which will give you slightly other results:
The default precision comes from the op4 file: in the example the matrix type is
1
(fourth number on first line of the file) which results in single precision (float32
).