From 102f0dbc336b653352a5468d6bf709247c869dcb Mon Sep 17 00:00:00 2001 From: Valentin Boettcher Date: Mon, 13 May 2019 16:01:45 +0200 Subject: [PATCH] fix list, add docs --- Readme.md | 28 ++++++++++++++++++++++++++++ SecondaryValue/SecondaryValue.py | 5 +++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index d09ecdc..60282df 100644 --- a/Readme.md +++ b/Readme.md @@ -54,6 +54,34 @@ print(result) # symbolic form. (Works best in Jupyter Notebooks) x.pretty_gauss_propagation('a', 'b', 'c') ``` + +### Vectorized Input +`SecondaryValue` supports vectorized input. As a rule-of-thump: Put +the iterable (list, np.array) where you would put scalars. + +You can mix scalars and vectors as long as all errors and values are +either scalar or have the same length. + +```python +from SecondaryValue import SecondaryValue +x = SecondaryValue('a**2+b') + +x(a=[[1,2,3]], b=1) +# >> array([ 2., 5., 10.]) + +x(a=([1,2,3], 1), b=1) +# >> (array([ 2., 5., 10.]), array([2., 4., 6.])) + +x(a=([1,2,3], [1,2,3]), b=1) +# >> (array([ 2., 5., 10.]), array([ 2., 8., 18.])) + +x(a=([1,2,3], [1,2,3]), b=([1,2,3], 1)) +# >> (array([ 2., 6., 12.]), array([ 2.23606798, 8.06225775, 18.02775638])) + +# THAT DOES NOT WORK: +x(a=([1,2,3], [1,2,3]), b=([1,2], 1)) +``` + ### Dependencies To make the calculation of complex values easier, one can define dependencies for a `SecondaryValue`: diff --git a/SecondaryValue/SecondaryValue.py b/SecondaryValue/SecondaryValue.py index b2aa1fe..ca6a59d 100644 --- a/SecondaryValue/SecondaryValue.py +++ b/SecondaryValue/SecondaryValue.py @@ -67,7 +67,7 @@ class SecondaryValue: continue tmp = sec_val(**kwargs) - kwargs[name] = tmp[0] if isinstance(tmp, Iterable) else tmp + kwargs[name] = tmp calc_deps[name] = tmp return kwargs, calc_deps @@ -223,7 +223,8 @@ class SecondaryValue: # calulate the central value scalar_values, vector_values = filter_out_vecotrized(values) - central_value = self._calculate_central_value(scalar_values, vector_values) + central_value = self._calculate_central_value(scalar_values, + vector_values) if not errors: return central_value