From: Jake Bowhay Date: Sat, 18 Jun 2022 07:39:32 +0000 (+0100) Subject: BUG: Fix power operator behaviour for sparse arrays to be consistent with numpy arrays X-Git-Tag: archive/raspbian/1.8.1-20+rpi1^2~5 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=4f07f3a3a41c76762b5856a66fd89a512a97d007;p=scipy.git 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 --- 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)