In my code I need to compute pseudoinverse of matrices and it may happen that some of the elements of the matrix are infinite (np.inf
). Sometimes the pinv()
function handles it well and returns something, but sometimes it just hangs with 100% CPU usage and I need to kill the process. See the demonstration below:
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
Why does this happen? Why does it do fine for some arrangement of inf
s but it just hangs for other? Might this be a bug (i.e. should the values be checked first)?