Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array ufunc multiplication #1677

Merged
merged 18 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Pint Changelog
(PR #1629)
- Add new SI prefixes: ronna-, ronto-, quetta-, quecto-.
(PR #1652)
- Fixed bug causing operations between arrays of quantity scalars and quantity holding
array resulting in incorrect units.
(PR #1677)
- Implementation for numpy.positive added for Quantity.
(PR #1663)
- Changed frequency to angular frequency in the docs.
Expand Down
49 changes: 25 additions & 24 deletions pint/facets/numpy/numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,31 +171,36 @@ def get_op_output_unit(unit_op, first_input_units, all_args=None, size=None):
"""
all_args = all_args or []

if unit_op == "sum":
result_unit = (1 * first_input_units + 1 * first_input_units).units
elif unit_op == "mul":
def multiply_units(args):
product = first_input_units._REGISTRY.parse_units("")
for x in all_args:
for x in args:
if hasattr(x, "units"):
product *= x.units
result_unit = product
elif _is_sequence_with_quantity_elements(x):
product *= x[0].units
return product

def get_numerator_unit(arg):
if _is_sequence_with_quantity_elements(arg):
return getattr(arg[0], "units", first_input_units._REGISTRY.parse_units(""))
else:
return getattr(arg, "units", first_input_units._REGISTRY.parse_units(""))

if unit_op == "sum":
result_unit = (1 * first_input_units + 1 * first_input_units).units
elif unit_op == "mul":
result_unit = multiply_units(all_args)
elif unit_op == "delta":
result_unit = (1 * first_input_units - 1 * first_input_units).units
elif unit_op == "delta,div":
product = (1 * first_input_units - 1 * first_input_units).units
for x in all_args[1:]:
if hasattr(x, "units"):
product /= x.units
result_unit = product
numerator_unit = (1 * first_input_units - 1 * first_input_units).units
denominator_unit = multiply_units(all_args[1:])
result_unit = numerator_unit / denominator_unit
elif unit_op == "div":
# Start with first arg in numerator, all others in denominator
product = getattr(
all_args[0], "units", first_input_units._REGISTRY.parse_units("")
)
for x in all_args[1:]:
if hasattr(x, "units"):
product /= x.units
result_unit = product
numerator_unit = get_numerator_unit(all_args[0])
denominator_unit = multiply_units(all_args[1:])
result_unit = numerator_unit / denominator_unit
elif unit_op == "variance":
result_unit = ((1 * first_input_units + 1 * first_input_units) ** 2).units
elif unit_op == "square":
Expand All @@ -212,13 +217,9 @@ def get_op_output_unit(unit_op, first_input_units, all_args=None, size=None):
result_unit = first_input_units**size
elif unit_op == "invdiv":
# Start with first arg in numerator, all others in denominator
product = getattr(
all_args[0], "units", first_input_units._REGISTRY.parse_units("")
)
for x in all_args[1:]:
if hasattr(x, "units"):
product /= x.units
result_unit = product**-1
numerator_unit = get_numerator_unit(all_args[0])
denominator_unit = multiply_units(all_args[1:])
result_unit = (numerator_unit / denominator_unit) ** -1
else:
raise ValueError("Output unit method {} not understood".format(unit_op))

Expand Down
9 changes: 9 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,15 @@ def test_issue_1300(self):
m = module_registry.Measurement(1, 0.1, "meter")
assert m.default_format == "~P"

@helpers.requires_numpy()
def test_issue1674(self, module_registry):
Q_ = module_registry.Quantity
arr_of_q = np.array([Q_(2, "m"), Q_(4, "m")], dtype="object")
q_arr = Q_(np.array([1, 2]), "m")

helpers.assert_quantity_almost_equal(arr_of_q * q_arr, Q_([2, 8], "m^2"))
helpers.assert_quantity_almost_equal(arr_of_q / q_arr, Q_([2, 2], ""))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look quite right to me: You start from an dtype=object array of quantity scalars, where each element could potentially be of a different unit. The result is now a "normal" quantity array, where all elements share a single unit.

Please add a test case for arr_of_q = np.array([Q_(2, "m"), Q_(4, "s")], dtype="object").

Obviously, this will not work with a quantity array, and the expected result of arr_of_q * q_arr would be np.array([Q_(2, "m^2"), Q_(4, "m s")], dtype="object"). For consistency, I would have expected to receive a dtype=object array in all cases.



if np is not None:

Expand Down