def generate_random_data_2(lth): # generates two list of values X, Y = [], [] for _ in range(lth): X.append(Value(np.random.rand(), np.random.rand()/10)) Y.append(Value(np.random.rand(), np.random.rand()/20)) return np.array(X), np.array(Y)
def test_convert_to_number(): assert complex(a) == a.x + 0j assert float(a) == a.x assert int(a) == 3 assert bool(a) assert not bool(Value(0, 0.0028))
def random_matrix(m, n): # initialize the empty array with object type # this has to be done because it will contain Value(s) # not primitive number types like floats M = np.empty((m, n), dtype=object) for i in range(m): for j in range(n): M[i, j] = Value(np.random.rand(), np.random.rand()/(j+1)) return M
def test_constructor(): v = Value(3.1415, 0.0012) assert v.x == 3.1415 == v.val assert v.ux == 0.0012 == v.unc with pytest.raises(ValueError): Value(3.14, -0.28) V = Value([3.1415] * 8, 0.0012) assert V.x.shape == (8, ) assert V.ux.shape == (8, ) assert np.mean(V.ux) == 0.0012 V = Value([3.1415] * 8, [0.0012] * 8) assert V.x.shape == (8, ) assert V.ux.shape == (8, ) assert np.mean(V.ux) == 0.0012 with pytest.raises(ValueError): Value(np.random.random((3, 2, 1)), np.random.random((4, 2, 1))) with pytest.raises(ValueError): Value(1j, 0) Value(1, 2j)
def test_precision(ux, acc): v = Value(1, ux) assert v.precision() == acc
def test_ceil(x, p, r): v = Value(x, 10.0**p) assert abs(ceil(v) - r) < ϵ
def test_floor(x, p, r): v = Value(x, 10.0**p) assert abs(floor(v) - r) < ϵ
def test_trunc(x, p, r): v = Value(x, 10.0**p) assert abs(trunc(v) - r) < ϵ
def test_round(x, p, r): v = Value(x, 10.0**p) assert abs(round(v) - r) < ϵ
import pytest import sys import os from math import trunc, ceil, floor import numpy as np sys.path.insert(0, os.getcwd()) from uncvalue import Value, val, unc, set_unc # noqa: E402 ϵ = 1e-8 a = Value(3.1415, 0.0012) b = Value(-1.618, 0.235) c = Value(3.1264e2, 1.268) A = np.array([[a, a], [b, b], [c, c]]) B = Value([a.x] * 5, a.ux) C = Value([b.x] * 5, [b.ux] * 5) @pytest.mark.parametrize( 'v, x', [(a, a.x), (A, np.array([[a.x, a.x], [b.x, b.x], [c.x, c.x]])), (B, a.x), (a.x, a.x)], ids=['Single', 'Array of values', 'Value array', 'Number']) def test_val(v, x): assert np.all(val(v) == x)
def generate_random_data_1(lth): # generates a value list return Value(np.random.rand(lth, 2), np.random.rand(lth, 2)**2)
def random_list(lth): return [Value(10*np.random.rand(), np.random.rand()) for _ in range(lth)]
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)