def test_repr(): assert (repr(Tolerance( rtol=2, atol=3, equal_nan=True)) == 'Tolerance(rtol=2, atol=3, equal_nan=True)') assert (str(Tolerance( rtol=5, atol=6, equal_nan=False)) == 'Tolerance(rtol=5, atol=6, equal_nan=False)')
def test_init(): tol = Tolerance(rtol=0.01, atol=0.002, equal_nan=False) assert tol.rtol == 0.01 assert tol.atol == 0.002 assert not tol.equal_nan tol = Tolerance(rtol=3, atol=4, equal_nan=True) assert tol.rtol == 3 assert tol.atol == 4 assert tol.equal_nan tol = Tolerance() assert tol.rtol == 1e-5 assert tol.atol == 1e-8 assert not tol.equal_nan
def test_all_zero(): tol = Tolerance(atol=5, rtol=9999999) assert tol.all_near_zero(0) assert tol.all_near_zero(4.5) assert not tol.all_near_zero(5.5) assert tol.all_near_zero([-4.5, 0, 1, 4.5, 3]) assert not tol.all_near_zero([-4.5, 0, 1, 4.5, 30])
def test_commutes_tolerance(): tol = Tolerance(atol=0.5) x = np.array([[0, 1], [1, 0]]) z = np.array([[1, 0], [0, -1]]) # Pays attention to specified tolerance. assert predicates.commutes(x, x + z * 0.1, tol) assert not predicates.commutes(x, x + z * 0.5, tol)
def test_is_diagonal_tolerance(): tol = Tolerance(atol=0.5) # Pays attention to specified tolerance. assert predicates.is_diagonal(np.array([[1, 0], [-0.5, 1]]), tol) assert not predicates.is_diagonal(np.array([[1, 0], [-0.6, 1]]), tol) # Error isn't accumulated across entries. assert predicates.is_diagonal(np.array([[1, 0.5], [-0.5, 1]]), tol) assert not predicates.is_diagonal(np.array([[1, 0.5], [-0.6, 1]]), tol)
def test_all_close(): no_tol = Tolerance() assert no_tol.all_close(1, 1) assert not no_tol.all_close(1, 0.5) assert not no_tol.all_close(1, 1.5) assert not no_tol.all_close(1, 100.5) assert no_tol.all_close(100, 100) assert not no_tol.all_close(100, 99.5) assert not no_tol.all_close(100, 100.5) assert no_tol.all_close([1, 2], [1, 2]) assert not no_tol.all_close([1, 2], [1, 3]) atol5 = Tolerance(atol=5) assert atol5.all_close(1, 1) assert atol5.all_close(1, 0.5) assert atol5.all_close(1, 1.5) assert atol5.all_close(1, 5.5) assert atol5.all_close(1, -3.5) assert not atol5.all_close(1, 6.5) assert not atol5.all_close(1, -4.5) assert not atol5.all_close(1, 100.5) assert atol5.all_close(100, 100) assert atol5.all_close(100, 100.5) assert atol5.all_close(100, 104.5) assert not atol5.all_close(100, 105.5) rtol2 = Tolerance(rtol=2) assert rtol2.all_close(100, 100) assert rtol2.all_close(299.5, 100) assert rtol2.all_close(1, 100) assert rtol2.all_close(-99, 100) assert not rtol2.all_close(100, 1) # Doesn't commute. assert not rtol2.all_close(300.5, 100) assert not rtol2.all_close(-101, 100) tol25 = Tolerance(rtol=2, atol=5) assert tol25.all_close(100, 100) assert tol25.all_close(106, 100) assert tol25.all_close(201, 100) assert tol25.all_close(304.5, 100) assert not tol25.all_close(305.5, 100)
def test_is_unitary_tolerance(): tol = Tolerance(atol=0.5) # Pays attention to specified tolerance. assert predicates.is_unitary(np.array([[1, 0], [-0.5, 1]]), tol) assert not predicates.is_unitary(np.array([[1, 0], [-0.6, 1]]), tol) # Error isn't accumulated across entries. assert predicates.is_unitary( np.array([[1.2, 0, 0], [0, 1.2, 0], [0, 0, 1.2]]), tol) assert not predicates.is_unitary( np.array([[1.2, 0, 0], [0, 1.3, 0], [0, 0, 1.2]]), tol)
def test_near_zero_mod(): tol = Tolerance(atol=5, rtol=9999999) assert tol.near_zero_mod(0, 100) assert tol.near_zero_mod(4.5, 100) assert not tol.near_zero_mod(5.5, 100) assert tol.near_zero_mod(100, 100) assert tol.near_zero_mod(95.5, 100) assert not tol.near_zero_mod(94.5, 100) assert tol.near_zero_mod(-4.5, 100) assert not tol.near_zero_mod(-5.5, 100) assert tol.near_zero_mod(104.5, 100) assert not tol.near_zero_mod(105.5, 100)
def test_is_hermitian_tolerance(): tol = Tolerance(atol=0.5) # Pays attention to specified tolerance. assert predicates.is_hermitian(np.array([[1, 0], [-0.5, 1]]), tol) assert predicates.is_hermitian(np.array([[1, 0.25], [-0.25, 1]]), tol) assert not predicates.is_hermitian(np.array([[1, 0], [-0.6, 1]]), tol) assert not predicates.is_hermitian(np.array([[1, 0.25], [-0.35, 1]]), tol) # Error isn't accumulated across entries. assert predicates.is_hermitian( np.array([[1, 0.5, 0.5], [0, 1, 0], [0, 0, 1]]), tol) assert not predicates.is_hermitian( np.array([[1, 0.5, 0.6], [0, 1, 0], [0, 0, 1]]), tol) assert not predicates.is_hermitian( np.array([[1, 0, 0.6], [0, 1, 0], [0, 0, 1]]), tol)
def test_is_special_orthogonal_tolerance(): tol = Tolerance(atol=0.5) # Pays attention to specified tolerance. assert predicates.is_special_orthogonal( np.array([[1, 0], [-0.5, 1]]), tol) assert not predicates.is_special_orthogonal( np.array([[1, 0], [-0.6, 1]]), tol) # Error isn't accumulated across entries, except for determinant factors. assert predicates.is_special_orthogonal( np.array([[1.2, 0, 0], [0, 1.2, 0], [0, 0, 1 / 1.2]]), tol) assert not predicates.is_special_orthogonal( np.array([[1.2, 0, 0], [0, 1.2, 0], [0, 0, 1.2]]), tol) assert not predicates.is_special_orthogonal( np.array([[1.2, 0, 0], [0, 1.3, 0], [0, 0, 1 / 1.2]]), tol)
def test_all_zero_mod(): tol = Tolerance(atol=5, rtol=9999999) assert tol.all_near_zero_mod(0, 100) assert tol.all_near_zero_mod(4.5, 100) assert not tol.all_near_zero_mod(5.5, 100) assert tol.all_near_zero_mod(100, 100) assert tol.all_near_zero_mod(95.5, 100) assert not tol.all_near_zero_mod(94.5, 100) assert tol.all_near_zero_mod(-4.5, 100) assert not tol.all_near_zero_mod(-5.5, 100) assert tol.all_near_zero_mod(104.5, 100) assert not tol.all_near_zero_mod(105.5, 100) assert tol.all_near_zero_mod([-4.5, 0, 1, 4.5, 3, 95.5, 104.5], 100) assert not tol.all_near_zero_mod([-4.5, 0, 1, 4.5, 30], 100)
def test_near_zero(): tol = Tolerance(atol=5, rtol=9999999) assert tol.near_zero(0) assert tol.near_zero(4.5) assert not tol.near_zero(5.5)