mirror of
				https://github.com/fjosw/pyerrors.git
				synced 2025-10-30 07:15:46 +01:00 
			
		
		
		
	rtruediv implemented for CObs, tests extended
This commit is contained in:
		
					parent
					
						
							
								c634d183a1
							
						
					
				
			
			
				commit
				
					
						325293f2b4
					
				
			
		
					 3 changed files with 27 additions and 11 deletions
				
			
		|  | @ -95,4 +95,7 @@ def Zq(inv_prop, fermion='Wilson'): | |||
|     if not res.imag.is_zero_within_error(5): | ||||
|         warnings.warn("Imaginary part of Zq is not zero within 5 sigma") | ||||
|         return res | ||||
|     if not np.abs(res.imag.value) <= 1e-6: | ||||
|         warnings.warn("Imaginary part of Zq is not smaller than 1e-6") | ||||
|         return res | ||||
|     return res.real | ||||
|  |  | |||
|  | @ -740,6 +740,13 @@ class CObs: | |||
|         else: | ||||
|             return CObs(self.real / other, self.imag / other) | ||||
| 
 | ||||
|     def __rtruediv__(self, other): | ||||
|         r = self.real ** 2 + self.imag ** 2 | ||||
|         if hasattr(other, 'real') and hasattr(other, 'imag'): | ||||
|             return CObs((self.real * other.real + self.imag * other.imag) / r, (self.real * other.imag - self.imag * other.real) / r) | ||||
|         else: | ||||
|             return CObs(self.real * other / r, -self.imag * other / r) | ||||
| 
 | ||||
|     def __abs__(self): | ||||
|         return np.sqrt(self.real**2 + self.imag**2) | ||||
| 
 | ||||
|  |  | |||
|  | @ -54,8 +54,12 @@ def test_function_overloading(): | |||
|         t1 = f([a, b]) | ||||
|         t2 = pe.derived_observable(f, [a, b]) | ||||
|         c = t2 - t1 | ||||
|         assert c.value == 0.0, str(i) | ||||
|         assert np.all(np.abs(c.deltas['e1']) < 1e-14), str(i) | ||||
|         assert c.is_zero() | ||||
| 
 | ||||
|     assert np.log(np.exp(b)) == b | ||||
|     assert np.exp(np.log(b)) == b | ||||
|     assert np.sqrt(b ** 2) == b | ||||
|     assert np.sqrt(b) ** 2 == b | ||||
| 
 | ||||
| 
 | ||||
| def test_overloading_vectorization(): | ||||
|  | @ -197,6 +201,7 @@ def test_overloaded_functions(): | |||
|         assert np.abs((ad_obs.value - item(val)) / ad_obs.value) < 1e-10, item.__name__ | ||||
|         assert np.abs(ad_obs.dvalue - dval * np.abs(deriv[i](val))) < 1e-6, item.__name__ | ||||
| 
 | ||||
| 
 | ||||
| def test_utils(): | ||||
|     my_obs = pe.pseudo_Obs(1.0, 0.5, 't') | ||||
|     my_obs.print(0) | ||||
|  | @ -211,6 +216,7 @@ def test_utils(): | |||
|     assert my_obs > (my_obs - 1) | ||||
|     assert my_obs < (my_obs + 1) | ||||
| 
 | ||||
| 
 | ||||
| def test_cobs(): | ||||
|     obs1 = pe.pseudo_Obs(1.0, 0.1, 't') | ||||
|     obs2 = pe.pseudo_Obs(-0.2, 0.03, 't') | ||||
|  | @ -227,22 +233,22 @@ def test_cobs(): | |||
| 
 | ||||
|     fs = [[lambda x: x[0] + x[1], lambda x: x[1] + x[0]], | ||||
|           [lambda x: x[0] * x[1], lambda x: x[1] * x[0]]] | ||||
|     for other in [1, 1.1, (1.1-0.2j), pe.CObs(obs1), pe.CObs(obs1, obs2)]: | ||||
|     for other in [3, 1.1, (1.1 - 0.2j), (2.3 + 0j), (0.0 + 7.7j), pe.CObs(obs1), pe.CObs(obs1, obs2)]: | ||||
|         for funcs in fs: | ||||
|             ta = funcs[0]([my_cobs, other]) | ||||
|             tb = funcs[1]([my_cobs, other]) | ||||
|             diff = ta - tb | ||||
|             assert np.isclose(0.0, float(diff.real)) | ||||
|             assert np.isclose(0.0, float(diff.imag)) | ||||
|             assert np.allclose(0.0, diff.real.deltas['t']) | ||||
|             assert np.allclose(0.0, diff.imag.deltas['t']) | ||||
|             assert diff.is_zero() | ||||
| 
 | ||||
|         ta = my_cobs - other | ||||
|         tb = other - my_cobs | ||||
|         diff = ta + tb | ||||
|         assert np.isclose(0.0, float(diff.real)) | ||||
|         assert np.isclose(0.0, float(diff.imag)) | ||||
|         assert np.allclose(0.0, diff.real.deltas['t']) | ||||
|         assert np.allclose(0.0, diff.imag.deltas['t']) | ||||
|         assert diff.is_zero() | ||||
| 
 | ||||
|         ta = my_cobs / other | ||||
|         tb = other / my_cobs | ||||
|         diff = ta * tb - 1 | ||||
|         assert diff.is_zero() | ||||
| 
 | ||||
|         assert (my_cobs / other * other - my_cobs).is_zero() | ||||
|         assert (other / my_cobs * my_cobs - other).is_zero() | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue