Efficient & pythonic check for singular matrix

So based on the inputs here, I’m marking my original code block with the explicit test as the solution:

if linalg.cond(x) < 1/sys.float_info.epsilon:
    i = linalg.inv(x)
else:
    #handle it

Surprisingly, the numpy.linalg.inv function doesn’t perform this test. I checked the code and found it goes through all it’s machinations, then just calls the lapack routine – seems quite inefficient. Also, I would 2nd a point made by DaveP: that the inverse of a matrix should not be computed unless it’s explicitly needed.

Leave a Comment