From 02d8f469eb93ec923171173a0ee4ec1a32a7d913 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Mon, 6 Dec 2021 21:59:41 +0000 Subject: [PATCH] feat: derived observable array mode works now, test added --- pyerrors/obs.py | 2 +- tests/linalg_test.py | 35 ++++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/pyerrors/obs.py b/pyerrors/obs.py index 7d4927ab..96d91ac1 100644 --- a/pyerrors/obs.py +++ b/pyerrors/obs.py @@ -1157,7 +1157,7 @@ def derived_observable(func, data, array_mode=False, **kwargs): for name in new_cov_names: g_extracted[name] = [] for i_dat, dat in enumerate(data): - g_extracted[name].append(np.array([obs.covobs[name]]).reshape(dat.shape + (1, ))) + g_extracted[name].append(np.array([o.covobs[name].grad for o in dat.reshape(np.prod(dat.shape))]).reshape(dat.shape + (1, ))) for i_val, new_val in np.ndenumerate(new_values): new_deltas = {} diff --git a/tests/linalg_test.py b/tests/linalg_test.py index d34da29f..b7fd4994 100644 --- a/tests/linalg_test.py +++ b/tests/linalg_test.py @@ -30,24 +30,25 @@ def get_complex_matrix(dimension): def test_matmul(): for dim in [4, 8]: - my_list = [] - length = 1000 + np.random.randint(200) - for i in range(dim ** 2): - my_list.append(pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2'])) - my_array = np.array(my_list).reshape((dim, dim)) - tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array - for t, e in np.ndenumerate(tt): - assert e.is_zero(), t + for const in [1, pe.cov_Obs(1.0, 0.002, 'cov')]: + my_list = [] + length = 1000 + np.random.randint(200) + for i in range(dim ** 2): + my_list.append(pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2'])) + my_array = const * np.array(my_list).reshape((dim, dim)) + tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array + for t, e in np.ndenumerate(tt): + assert e.is_zero(), t - my_list = [] - length = 1000 + np.random.randint(200) - for i in range(dim ** 2): - my_list.append(pe.CObs(pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2']), - pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2']))) - my_array = np.array(my_list).reshape((dim, dim)) - tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array - for t, e in np.ndenumerate(tt): - assert e.is_zero(), t + my_list = [] + length = 1000 + np.random.randint(200) + for i in range(dim ** 2): + my_list.append(pe.CObs(pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2']), + pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2']))) + my_array = np.array(my_list).reshape((dim, dim)) * const + tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array + for t, e in np.ndenumerate(tt): + assert e.is_zero(), t def test_jack_matmul():