BUG: Fix power operator behaviour for sparse arrays to be consistent with numpy arrays
authorJake Bowhay <jb9.bowhay@gmail.com>
Sat, 18 Jun 2022 07:39:32 +0000 (08:39 +0100)
committerDrew Parsons <dparsons@debian.org>
Wed, 25 Jan 2023 14:32:10 +0000 (14:32 +0000)
Gbp-Pq: Name 0016-BUG-Fix-power-operator-behaviour-for-sparse-arrays-t.patch

scipy/sparse/_arrays.py
scipy/sparse/tests/test_array_api.py

index d272c1075bfc1298ad87840698271fb9d58e1881..5234339be542a2ba00840042741cc33e243f19bf 100644 (file)
@@ -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
index f00972a199b66837c68c8eaaea43d12461aa9a1d..a074cae7759d6fd9fad814a977cb89f2023a742b 100644 (file)
@@ -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)