Python numpy.linalg.pinv()挂起某些无限值(但不适用于其他值)

Python numpy.linalg.pinv()挂起某些无限值(但不适用于其他值),python,numpy,linear-algebra,Python,Numpy,Linear Algebra,在我的代码中,我需要计算矩阵的伪逆,矩阵的某些元素可能是无限的(np.inf)。有时pinv()函数可以很好地处理它并返回一些东西,但有时它会在CPU使用率达到100%时挂起,我需要终止进程。请参见下面的演示: Python 3.4.3+ (default, Oct 14 2015, 16:03:50) Type "copyright", "credits" or "license" for more information. IPython 4.1.2 -- An enhanced Int

在我的代码中,我需要计算矩阵的伪逆,矩阵的某些元素可能是无限的(
np.inf
)。有时
pinv()
函数可以很好地处理它并返回一些东西,但有时它会在CPU使用率达到100%时挂起,我需要终止进程。请参见下面的演示:

Python 3.4.3+ (default, Oct 14 2015, 16:03:50) 
Type "copyright", "credits" or "license" for more information.

IPython 4.1.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy as np
In [2]: import numpy.linalg as la
In [3]: x = np.array([[550.0, 1], [1, np.inf]])
In [4]: la.pinv(x)
Out[4]: 
array([[ 0.,  0.],
       [ 0.,  0.]])
In [5]: x = np.array([[np.inf, np.inf], [np.inf, np.inf]])
In [6]: la.pinv(x)
Out[6]: 
array([[ nan,  nan],
       [ nan,  nan]])
In [7]: x = np.array([[550.0, 1], [np.inf, np.inf]])
In [8]: la.pinv(x)  # here it just hung and I had to kill it from outside
Killed
为什么会发生这种情况?为什么它对
inf
s的某些排列很好,但对其他排列却挂起?这可能是一个bug(即是否应该首先检查值)?

我在numpy的GitHub存储库上打开了一个


答案是这是BLAS/LAPACK/ATLAS中的一个bug,numpy(1.10.4)函数只是对那些库中的过程的薄薄包装,这些库不会事先检查输入,因为它会损害性能。

奇怪的是,它也不会挂起相反的
x=np.array([[np.inf,np.inf],[550.0,1]])
la.pinv(x)
array([[nan,nan],[nan,nan]])此外,您提供的示例(以及我提供的示例)都不会通过以下测试:np.allclose(x,np.dot(x,np.dot(la.pinv(x,x)))