Test if a matrix has an inverse

from numpy import array, linalg def is_invertible(matrix): # True if the solution of Ax = 0 is the zero vector. try: x = linalg.solve(matrix, [0] * matrix.shape[0]) return 'invertible' if not all(x) else 'not invertible' except linalg.linalg.LinAlgError: return 'not invertible' print(is_invertible(array([ [2, -1, 0, -1], [-1, 2, -1, 0], [0, -1, 2, -1], [-1, 0, -1, 2] ]))) # invertible print(is_invertible(array([ [1, -1, 0, 0], [-1, 2, -1, 0], [0, -1, 2, -1], [0, 0, -1, -1] ]))) # invertible print(is_invertible(array([ [3, 1, 1], [1, 3, 1], [1, 1, 3] ]))) # invertible print(is_invertible(array([ [2, 1, 1], [1, 2, 1], [1, 1, 3] ]))) # invertible print(is_invertible(array([ [1, 1, 1], [1, 1, 1], [1, 1, 3] ]))) # not invertible print(is_invertible(array([ [1, 0, 0], [3, 1, 0], [4, 5, 1] ]))) # invertible print(is_invertible(array([ [1, 0, 0], [3, 1, 0], [4, 5, 0] ]))) # not invertible