From fce59dd462f9afb10f9d6ac8e90e47f61626693e Mon Sep 17 00:00:00 2001 From: Jake Bowhay Date: Sat, 18 Jun 2022 08:39:32 +0100 Subject: [PATCH] BUG: Fix power operator behaviour for sparse arrays to be consistent with numpy arrays Gbp-Pq: Name 0016-BUG-Fix-power-operator-behaviour-for-sparse-arrays-t.patch --- scipy/sparse/_arrays.py | 4 ++++ scipy/sparse/tests/test_array_api.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/scipy/sparse/_arrays.py b/scipy/sparse/_arrays.py index d272c107..5234339b 100644 --- a/scipy/sparse/_arrays.py +++ b/scipy/sparse/_arrays.py @@ -49,6 +49,10 @@ class _sparray: def __rmul__(self, *args, **kwargs): return self.multiply(*args, **kwargs) + # Restore elementwise power + def __pow__(self, *args, **kwargs): + return self.power(*args, **kwargs) + def _matrix_doc_to_array(docstr): # For opimized builds with stripped docstrings diff --git a/scipy/sparse/tests/test_array_api.py b/scipy/sparse/tests/test_array_api.py index f00972a1..a074cae7 100644 --- a/scipy/sparse/tests/test_array_api.py +++ b/scipy/sparse/tests/test_array_api.py @@ -331,3 +331,9 @@ def test_spilu(): ]) LU = spla.spilu(X) npt.assert_allclose(LU.solve(np.array([1, 2, 3, 4])), [1, 0, 0, 0]) + + +@parametrize_sparrays +def test_power_operator(A): + # https://github.com/scipy/scipy/issues/15948 + npt.assert_equal((A**2).todense(), (A.todense())**2) -- 2.30.2