def test_set_unc(): v = set_unc(0.234, 0.0052) assert isinstance(v, Value) assert v.x == 0.234 assert v.ux == 0.0052 v = set_unc(a, 0.0052) assert isinstance(v, Value) assert v.x == a.x assert v.ux == 0.0052 v = set_unc([0.234] * 8, 0.0052) assert isinstance(v, np.ndarray) assert v.shape == (8, ) assert np.mean(unc(v)) == 0.0052 v = set_unc([0.234] * 8, [0.0052] * 8) assert isinstance(v, np.ndarray) assert v.shape == (8, ) assert np.mean(unc(v)) == 0.0052 with pytest.raises(ValueError): set_unc(np.random.random((3, 2, 1)), np.random.random((4, 2, 1)))
def test_sin(x): z = np.sin(x) assert np.all(val(z) == np.sin(val(x))) assert np.all(unc(z) == np.abs(unc(x) * np.cos(val(x))))
def test_expm1(x): z = np.expm1(x) assert np.all(val(z) == np.expm1(val(x))) assert np.all(unc(z) == unc(x) * np.exp(val(x)))
def test_exp2(x): z = np.exp2(x) assert np.all(val(z) == np.exp2(val(x))) assert np.all(unc(z) == unc(x) * np.exp2(val(x)) * np.log(2))
def test_add(x, y, r): z = x + y assert np.all(val(z) == val(r)) assert np.all(unc(z) == unc(r))
def test_sqrt(x): z = np.sqrt(x) assert np.all(val(z) == np.sqrt(val(x))) assert np.all(unc(z) == np.abs(unc(x) / (2 * np.sqrt(val(x)))))
def test_arccosh(x): z = np.arccosh(x) assert np.all(val(z) == np.arccosh(val(x))) assert np.all(unc(z) == np.abs(unc(x) / np.sqrt(val(x)**2 - 1)))
def test_tanh(x): z = np.tanh(x) assert np.all(val(z) == np.tanh(val(x))) assert np.all(unc(z) == np.abs(unc(x) / np.cosh(val(x))**2))
def test_unc(v, x): assert np.all(unc(v) == x)
def test_abs(x, r): z = abs(x) assert np.all(val(z) == val(r)) assert np.all(unc(z) == unc(r))
def test_invert(x, r): z = ~x assert np.all(val(z) == val(r)) assert np.all(unc(z) == unc(r))
def test_neg(x, r): z = -x assert np.all(val(z) == val(r)) assert np.all(unc(z) == unc(r))
def test_mul(x, y, r): z = x * y assert np.all(val(z) == val(r)) assert np.all(unc(z) == unc(r))
def test_sub(x, y, r): z = x - y assert np.all(val(z) == val(r)) assert np.all(unc(z) == unc(r))
def test_arccos(x): z = np.arccos(x) assert np.all(val(z) == np.arccos(val(x))) assert np.all(unc(z) == np.abs(unc(x) / np.sqrt(1 - val(x)**2)))
def test_log(x): z = np.log(x) assert np.all(val(z) == np.log(val(x))) assert np.all(unc(z) == unc(x) / val(x))
def test_cosh(x): z = np.cosh(x) assert np.all(val(z) == np.cosh(val(x))) assert np.all(unc(z) == np.abs(unc(x) * np.sinh(val(x))))
def test_log2(x): z = np.log2(x) assert np.all(val(z) == np.log2(val(x))) assert np.all(unc(z) == unc(x) / (val(x) * np.log(2)))
def test_arcsinh(x): z = np.arcsinh(x) assert np.all(val(z) == np.arcsinh(val(x))) assert np.all(unc(z) == np.abs(unc(x) / np.sqrt(1 + val(x)**2)))
def test_log10(x): z = np.log10(x) assert np.all(val(z) == np.log10(val(x))) assert np.all(unc(z) == unc(x) / (val(x) * np.log(10)))
def test_arctanh(x): z = np.arctanh(x) assert np.all(val(z) == np.arctanh(val(x))) assert np.all(unc(z) == np.abs(unc(x) / (1 - val(x)**2)))
def test_log1p(x): z = np.log1p(x) assert np.all(val(z) == np.log1p(val(x))) assert np.all(unc(z) == unc(x) / (val(x) + 1))
def test_cbrt(x): z = np.cbrt(x) assert np.all(val(z) == np.cbrt(val(x))) assert np.all(unc(z) == np.abs(unc(x) / (3 * np.cbrt(val(x))**2)))
import numpy as np import matplotlib.pyplot as plt from uncvalue import Value, val, unc ########################################### # Basic operations # ########################################### a = Value(3.12, 0.52) b = Value(2.569, 0.198) c = Value(0.00238, 0.0005498) # call val()/unc() to get the value/uncetainty of a Value print('Value = %.2f +/- %.2f' % (val(a), unc(a))) print('Value =', c) # perform any operation always using numpy library # operations made with python math library will not work print('a + b =', a + b) print('Inverse a = ', 1 / a, a**(-1)) print('a*sin(b) =', a * np.sin(b)) ########################################### # Lists # ########################################### def random_list(lth): return [Value(10*np.random.rand(), np.random.rand()) for _ in range(lth)] A = random_list(100)