2

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 infs but it just hangs for other? Might this be a bug (i.e. should the values be checked first)?

zegkljan
  • 8,051
  • 5
  • 34
  • 49
  • its odd that is doesn't hang for the opposite either `x = np.array([[np.inf, np.inf], [550.0,1]])` `la.pinv(x)` array([[nan, nan],[nan, nan]]) – James Tobin Mar 25 '16 at 16:17
  • also, none of the examples you provide (and the one I did) will pass the: np.allclose(x, np.dot(x, np.dot(la.pinv(x), x))) test from http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.linalg.pinv.html – James Tobin Mar 25 '16 at 16:26

1 Answers1

1

I opened an issue on numpy's GitHub repository.

The answer is that it is a bug in BLAS/LAPACK/ATLAS and numpy (1.10.4) functions are just thin wrappers around procedures from those libraries which don't check the inputs beforehand as it harms performance.

zegkljan
  • 8,051
  • 5
  • 34
  • 49