diff --git a/pyerrors/obs.py b/pyerrors/obs.py index bf8575bd..6874bd12 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -582,7 +582,7 @@ class Obs: ensemble to the error and returns a dictionary containing the fractions.""" if not hasattr(self, 'e_dvalue'): raise Exception('Run the gamma method first.') - if self._dvalue == 0.0: + if np.isclose(0.0, self._dvalue, atol=1e-15): raise Exception('Error is 0.0') labels = self.e_names sizes = [self.e_dvalue[name] ** 2 for name in labels] / self._dvalue ** 2 @@ -729,6 +729,9 @@ class Obs: def __rsub__(self, y): return -1 * (self - y) + def __pos__(self): + return self + def __neg__(self): return -1 * self @@ -911,8 +914,11 @@ class CObs: def __abs__(self): return np.sqrt(self.real**2 + self.imag**2) - def __neg__(other): - return -1 * other + def __pos__(self): + return self + + def __neg__(self): + return -1 * self def __eq__(self, other): return self.real == other.real and self.imag == other.imag diff --git a/tests/linalg_test.py b/tests/linalg_test.py index 61c71514..09db0ac5 100644 --- a/tests/linalg_test.py +++ b/tests/linalg_test.py @@ -314,6 +314,9 @@ def test_matrix_functions(): # Check determinant assert pe.linalg.det(np.diag(np.diag(matrix))) == np.prod(np.diag(matrix)) + with pytest.raises(Exception): + pe.linalg.det(5) + pe.linalg.pinv(matrix[:,:3]) @@ -347,3 +350,10 @@ def test_complex_matrix_operations(): diff = ta * tb - 1 for (i, j), entry in np.ndenumerate(diff): assert entry.is_zero() + + +def test_complex_matrix_real_entries(): + my_mat = get_complex_matrix(4) + my_mat[0, 1] = 4 + my_mat[2, 0] = pe.Obs([np.random.normal(1.0, 0.1, 100)], ['t']) + assert np.all((my_mat @ pe.linalg.inv(my_mat) - np.identity(4)) == 0) diff --git a/tests/obs_test.py b/tests/obs_test.py index 4eae6db4..c8aef487 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -51,6 +51,12 @@ def test_Obs_exceptions(): my_obs.gamma_method() my_obs.details() + obs = pe.Obs([np.random.normal(1.0, 0.1, 100)], ['t']) + one = obs / obs + one.gamma_method() + with pytest.raises(Exception): + one.plot_piechart() + def test_dump(): value = np.random.normal(5, 10) dvalue = np.abs(np.random.normal(0, 1)) @@ -86,6 +92,8 @@ def test_comparison(): assert test_obs2 != value2 assert test_obs1 != test_obs2 assert test_obs2 != test_obs1 + assert +test_obs1 == test_obs1 + assert -test_obs1 == 0 - test_obs1 def test_function_overloading(): @@ -367,6 +375,8 @@ def test_cobs(): obs2 = pe.pseudo_Obs(-0.2, 0.03, 't') my_cobs = pe.CObs(obs1, obs2) + assert +my_cobs == my_cobs + assert -my_cobs == 0 - my_cobs my_cobs == my_cobs str(my_cobs) repr(my_cobs) @@ -758,3 +768,15 @@ def test_merge_idx(): assert(new_idx[-1] > new_idx[0]) for i in range(1, len(new_idx)): assert(new_idx[i - 1] < new_idx[i]) + +def test_cobs_array(): + cobs = pe.Obs([np.random.normal(1.0, 0.1, 100)], ['t']) * (1 + 2j) + np.identity(4) + cobs + cobs + np.identity(4) + np.identity(4) - cobs + cobs - np.identity(4) + np.identity(4) * cobs + cobs * np.identity(4) + np.identity(4) / cobs + cobs / np.ones((4, 4)) +