Skip to content

Commit

Permalink
Merge pull request #41 from dprada/main
Browse files Browse the repository at this point in the history
Adding hstack and vstack
  • Loading branch information
dprada authored Apr 6, 2023
2 parents 3102e38 + 5671af3 commit 8067447
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyunitwizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions pyunitwizard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit 8067447

Please sign in to comment.