From ea955760488ce40c2c4a73d009a9cb5a74aa58b2 Mon Sep 17 00:00:00 2001 From: Arjav Trivedi Date: Mon, 1 Mar 2021 03:42:18 +0000 Subject: [PATCH 1/5] Add np.linalg.norm implementation --- pint/numpy_func.py | 10 +++++++++- pint/testsuite/test_issues.py | 6 ++++++ pint/testsuite/test_numpy.py | 6 ++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pint/numpy_func.py b/pint/numpy_func.py index c335f3d2f..707ea624d 100644 --- a/pint/numpy_func.py +++ b/pint/numpy_func.py @@ -879,7 +879,15 @@ def implementation(a, *args, **kwargs): implement_func("function", func_str, input_units=None, output_unit=None) # Handle functions with output unit defined by operation -for func_str in ["std", "nanstd", "sum", "nansum", "cumsum", "nancumsum"]: +for func_str in [ + "std", + "nanstd", + "sum", + "nansum", + "cumsum", + "nancumsum", + "linalg.norm", +]: implement_func("function", func_str, input_units=None, output_unit="sum") for func_str in ["cross", "trapz", "dot"]: implement_func("function", func_str, input_units=None, output_unit="mul") diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index 5058838c9..966c772bb 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -817,6 +817,12 @@ def test_issue_1185(self): np.array((0.04, 0.09)), ) + @helpers.requires_numpy + def test_issue_1250(self): + q = np.array([[3, 4], [5, 12], [8, 15]]) * self.ureg.m + expected = np.array([5, 13, 17]) * self.ureg.m + helpers.assert_quantity_equal(np.linalg.norm(q, axis=1), expected) + if np is not None: diff --git a/pint/testsuite/test_numpy.py b/pint/testsuite/test_numpy.py index 44d42714c..0f661eb3e 100644 --- a/pint/testsuite/test_numpy.py +++ b/pint/testsuite/test_numpy.py @@ -1308,6 +1308,12 @@ def test_intersect1d(self): [1, 3] * self.ureg.m, ) + @helpers.requires_array_function_protocol() + def test_linalg_norm(self): + q = np.array([[3, 5, 8], [4, 12, 15]]) * self.ureg.m + expected = [5, 13, 17] * self.ureg.m + helpers.assert_quantity_equal(np.linalg.norm(q, axis=0), expected) + @pytest.mark.skip class TestBitTwiddlingUfuncs(TestUFuncs): From c017d340518d04ca05e22c3e317bb6ea9607c95d Mon Sep 17 00:00:00 2001 From: Arjav Trivedi Date: Mon, 1 Mar 2021 03:48:48 +0000 Subject: [PATCH 2/5] Add to changelog --- CHANGES | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index e3ef610a6..e2affaa93 100644 --- a/CHANGES +++ b/CHANGES @@ -9,12 +9,13 @@ Pint Changelog - Fix comparisons between Quantities and Measurements. (Issue #1134, thanks lewisamarshall) - Implemented benchmarks based on airspeed velocity. -- Fix tolist function with scalar ndarray. +- Fix tolist function with scalar ndarray. (Issue #1195, thanks jules-ch) - UnitsContainer returns false if other is str and cannnot be parsed (Issue #1179, thanks rfrowe) - Add Github Actions CI. (Issue #1236) - Fix numpy.linalg.solve unit output. (Issue #1246) +- Add numpy.linalg.norm implementation. (Issue #1250) 0.16.1 (2020-09-22) From e86aee6416a7df40cbf76e61f60c82ea247a64d6 Mon Sep 17 00:00:00 2001 From: Arjav Trivedi Date: Thu, 14 Sep 2023 22:52:45 +0100 Subject: [PATCH 3/5] fix: add np.linalg.norm implementation after merging upstream --- CHANGES | 4 ++-- pint/facets/numpy/numpy_func.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 31c154b57..cad264249 100644 --- a/CHANGES +++ b/CHANGES @@ -10,7 +10,8 @@ Pint Changelog (PR #1803) - Optimize matplotlib unit conversion for Quantity arrays (PR #1819) - +- Add numpy.linalg.norm implementation. + (PR #1251) 0.22 (2023-05-25) ----------------- @@ -225,7 +226,6 @@ types for pint-pandas compatibility. (#1596) - UnitsContainer returns false if other is str and cannnot be parsed (Issue #1179, thanks rfrowe) - Fix numpy.linalg.solve unit output. (Issue #1246) -- Add numpy.linalg.norm implementation. (Issue #1250) - Support numpy.lib.stride_tricks.sliding_window_view. (Issue #1255) - NEP29 Support docs. diff --git a/pint/facets/numpy/numpy_func.py b/pint/facets/numpy/numpy_func.py index 0e7bfc25f..7c31de0c3 100644 --- a/pint/facets/numpy/numpy_func.py +++ b/pint/facets/numpy/numpy_func.py @@ -1003,7 +1003,15 @@ def implementation(a, *args, **kwargs): implement_func("function", func_str, input_units=None, output_unit=None) # Handle functions with output unit defined by operation -for func_str in ("std", "nanstd", "sum", "nansum", "cumsum", "nancumsum"): +for func_str in ( + "std", + "nanstd", + "sum", + "nansum", + "cumsum", + "nancumsum", + "linalg.norm", +): implement_func("function", func_str, input_units=None, output_unit="sum") for func_str in ("diff", "ediff1d"): implement_func("function", func_str, input_units=None, output_unit="delta") @@ -1032,4 +1040,3 @@ def numpy_wrap(func_type, func, args, kwargs, types): if name not in handled or any(is_upcast_type(t) for t in types): return NotImplemented return handled[name](*args, **kwargs) - From 89be94cd945bd0d8a91e17b3ce29ed90ef81cc58 Mon Sep 17 00:00:00 2001 From: Arjav Trivedi Date: Thu, 14 Sep 2023 23:26:24 +0100 Subject: [PATCH 4/5] test: rm test as per feedback --- pint/testsuite/test_issues.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pint/testsuite/test_issues.py b/pint/testsuite/test_issues.py index 488da9265..9540814c3 100644 --- a/pint/testsuite/test_issues.py +++ b/pint/testsuite/test_issues.py @@ -851,12 +851,6 @@ def test_issue_1185(self, module_registry): np.array((0.04, 0.09)), ) - @helpers.requires_numpy - def test_issue_1250(self): - q = np.array([[3, 4], [5, 12], [8, 15]]) * self.ureg.m - expected = np.array([5, 13, 17]) * self.ureg.m - helpers.assert_quantity_equal(np.linalg.norm(q, axis=1), expected) - def test_issue1277(self, module_registry): ureg = module_registry assert ureg("%") == ureg("percent") From cc56b12ca5e07ea756a71847d30635f370415344 Mon Sep 17 00:00:00 2001 From: Arjav Trivedi Date: Thu, 14 Sep 2023 23:26:49 +0100 Subject: [PATCH 5/5] docs: cleanup spurious edits from merge --- CHANGES | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CHANGES b/CHANGES index cad264249..0fd6d30f6 100644 --- a/CHANGES +++ b/CHANGES @@ -218,15 +218,10 @@ types for pint-pandas compatibility. (#1596) - Fix issue with reducable dimensionless units when using power (Quantity**ndarray) (Issue #1185) - Fix comparisons between Quantities and Measurements. - (Issue #1134, thanks lewisamarshall) -- Implemented benchmarks based on airspeed velocity. -- Fix tolist function with scalar ndarray. - (Issue #1195, thanks jules-ch) (Issue #1134, thanks lewisamarshall) - UnitsContainer returns false if other is str and cannnot be parsed (Issue #1179, thanks rfrowe) - Fix numpy.linalg.solve unit output. (Issue #1246) - - Support numpy.lib.stride_tricks.sliding_window_view. (Issue #1255) - NEP29 Support docs. - Move all tests to pytest.