Test for projection and reflection matrices

import sys from numpy import array from numpy.linalg import eig eps = sys.float_info.epsilon def matrix_type(matrix): eigenvalue_targets = { 'Projection': [1, 0], 'Reflection': [-1, 1] } eigvals, _ = eig(matrix) for typ, eigv_targets in eigenvalue_targets.items(): conditions_met = 0 for eigv_target in eigv_targets: for eigv in eigvals: if abs(eigv - eigv_target) < eps: conditions_met += 1 break if conditions_met == len(eigv_targets): return typ + ' matrix' return 'Matrix of unknown type' A = array([ [0, 1], [1, 0] ]) B = array([ [0.5, 0.5], [0.5, 0.5] ]) C = array([ [0.2, 0.4, 0], [0.4, 0.8, 0], [0, 0, 1], ]) D = array([ [4, 1, 2], [1, 4, 0], [2, 0, 4] ]) print(matrix_type(A)) # Reflection matrix print(matrix_type(B)) # Projection matrix print(matrix_type(C)) # Projection matrix print(matrix_type(D)) # Matrix of unknown type