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 14 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 #1805, PR #1832)
- Documented to_preferred and created added an autoautoconvert_to_preferred registry option.
(PR #1803)
- Fixed bug causing operations between arrays of quantity scalars and quantity holding
array resulting in incorrect units.
(PR #1677)
- Optimize matplotlib unit conversion for Quantity arrays
(PR #1819)
- Add numpy.linalg.norm implementation.
Expand Down
12 changes: 12 additions & 0 deletions pint/facets/numpy/numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ def implement_func(func_type, func_str, input_units=None, output_unit=None):

@implements(func_str, func_type)
def implementation(*args, **kwargs):
if func_str in ["multiply", "true_divide", "divide", "floor_divide"] and any(
[
not _is_quantity(arg) and _is_sequence_with_quantity_elements(arg)
for arg in args
]
):
# the sequence may contain different units, so fall back to element-wise
return np.array(
[func(args[0][i], args[1][i]) for i in range(len(args[0]))],
dtype=object,
)
andrewgsavage marked this conversation as resolved.
Show resolved Hide resolved

first_input_units = _get_first_input_units(args, kwargs)
if input_units == "all_consistent":
# Match all input args/kwargs to same units
Expand Down
20 changes: 20 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,26 @@ 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_equal(
arr_of_q * q_arr, np.array([Q_(2, "m^2"), Q_(8, "m^2")], dtype="object")
)
helpers.assert_quantity_equal(
arr_of_q / q_arr, np.array([Q_(2, ""), Q_(2, "")], dtype="object")
)

arr_of_q = np.array([Q_(2, "m"), Q_(4, "s")], dtype="object")
q_arr = Q_(np.array([1, 2]), "m")

helpers.assert_quantity_equal(
arr_of_q * q_arr, np.array([Q_(2, "m^2"), Q_(8, "m s")], dtype="object")
)

@helpers.requires_babel()
def test_issue_1400(self, sess_registry):
q1 = 3 * sess_registry.W
Expand Down
Loading