diff --git a/pyunitwizard/__init__.py b/pyunitwizard/__init__.py index 7804da8..7b98d23 100644 --- a/pyunitwizard/__init__.py +++ b/pyunitwizard/__init__.py @@ -15,7 +15,7 @@ from .main import get_value, get_unit, get_value_and_unit, change_value from .main import convert from .main import get_standard_units, standardize, get_dimensionality -from .main import concatenate, stack +from .main import concatenate, stack, hstack, vstack from .main import are_compatible, are_equal, are_close, check from . import configure from . import kernel as _kernel diff --git a/pyunitwizard/main.py b/pyunitwizard/main.py index bc5f3f5..91f8597 100644 --- a/pyunitwizard/main.py +++ b/pyunitwizard/main.py @@ -930,6 +930,60 @@ def stack(sequence, to_unit=None, to_form=None, type_value='tuple', standardized aux_list.append(get_value(aux_quantity, to_unit=output_unit)) output_value.append(aux_list) + output_value = np.stack(output_value) + + if type_value=='list': + return quantity(output_value.tolist(), output_unit, form=to_form, standardized=standardized) + elif type_value=='tuple': + return quantity(tuple(output_value.tolist()), output_unit, form=to_form, standardized=standardized) + elif type_value=='numpy.ndarray': + return quantity(output_value, output_unit, form=to_form, standardized=standardized) + else: + raise ValueError + +def hstack(sequence, to_unit=None, to_form=None, type_value='tuple', standardized=False): + + if to_unit is None: + output_unit = get_unit(sequence[0][0]) + else: + output_unit = to_unit + + output_value = [] + + for aux_seq in sequence: + aux_list = [] + for aux_quantity in aux_seq: + aux_list.append(get_value(aux_quantity, to_unit=output_unit)) + output_value.append(aux_list) + + output_value = np.hstack(output_value) + + if type_value=='list': + return quantity(output_value.tolist(), output_unit, form=to_form, standardized=standardized) + elif type_value=='tuple': + return quantity(tuple(output_value.tolist()), output_unit, form=to_form, standardized=standardized) + elif type_value=='numpy.ndarray': + return quantity(output_value, output_unit, form=to_form, standardized=standardized) + else: + raise ValueError + +def vstack(sequence, to_unit=None, to_form=None, type_value='tuple', standardized=False): + + if to_unit is None: + output_unit = get_unit(sequence[0][0]) + else: + output_unit = to_unit + + output_value = [] + + for aux_seq in sequence: + aux_list = [] + for aux_quantity in aux_seq: + aux_list.append(get_value(aux_quantity, to_unit=output_unit)) + output_value.append(aux_list) + + output_value = np.vstack(output_value) + if type_value=='list': return quantity(output_value.tolist(), output_unit, form=to_form, standardized=standardized) elif type_value=='tuple':