def test_commutes_tolerance(): atol = 0.5 x = np.array([[0, 1], [1, 0]]) z = np.array([[1, 0], [0, -1]]) # Pays attention to specified tolerance. assert matrix_commutes(x, x + z * 0.1, atol=atol) assert not matrix_commutes(x, x + z * 0.5, atol=atol)
def _strat_commutes_from_matrix( v1: Any, v2: Any, *, atol: float) -> Union[bool, NotImplementedType, None]: """Attempts to determine commutativity of matrices.""" if not isinstance(v1, np.ndarray) or not isinstance(v2, np.ndarray): return NotImplemented if v1.shape != v2.shape: return None return linalg.matrix_commutes(v1, v2, atol=atol)
def test_commutes(): assert matrix_commutes(np.empty((0, 0)), np.empty((0, 0))) assert not matrix_commutes(np.empty((1, 0)), np.empty((0, 1))) assert not matrix_commutes(np.empty((0, 1)), np.empty((1, 0))) assert not matrix_commutes(np.empty((1, 0)), np.empty((1, 0))) assert not matrix_commutes(np.empty((0, 1)), np.empty((0, 1))) assert matrix_commutes(np.array([[1]]), np.array([[2]])) assert matrix_commutes(np.array([[1]]), np.array([[0]])) x = np.array([[0, 1], [1, 0]]) y = np.array([[0, -1j], [1j, 0]]) z = np.array([[1, 0], [0, -1]]) xx = np.kron(x, x) zz = np.kron(z, z) assert matrix_commutes(x, x) assert matrix_commutes(y, y) assert matrix_commutes(z, z) assert not matrix_commutes(x, y) assert not matrix_commutes(x, z) assert not matrix_commutes(y, z) assert matrix_commutes(xx, zz) assert matrix_commutes(xx, np.diag([1, -1, -1, 1 + 1e-9]))