From c9422a88ea49e5c7aedc3980dd11e672464a3191 Mon Sep 17 00:00:00 2001 From: jessegrabowski Date: Mon, 13 Oct 2025 22:28:53 -0400 Subject: [PATCH 1/3] Rename `mul` -> `multiply` --- pytensor/sparse/basic.py | 28 ++++++++++++++++++++++------ tests/sparse/test_basic.py | 14 +++++++------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/pytensor/sparse/basic.py b/pytensor/sparse/basic.py index 6ad2a8336f..724f97cb9c 100644 --- a/pytensor/sparse/basic.py +++ b/pytensor/sparse/basic.py @@ -2491,7 +2491,9 @@ def infer_shape(self, fgraph, node, ins_shapes): mul_s_v = MulSV() -def mul(x, y): +def multiply( + x: SparseTensorType | TensorType, y: SparseTensorType | TensorType +) -> SparseVariable: """ Multiply elementwise two matrices, at least one of which is sparse. @@ -2499,21 +2501,21 @@ def mul(x, y): Parameters ---------- - x + x: SparseVariable A matrix variable. - y + y: SparseVariable A matrix variable. Returns ------- - A sparse matrix - `x` * `y` + result: SparseVariable + The elementwise multiplication of `x` and `y`. Notes ----- At least one of `x` and `y` must be a sparse matrix. - The grad is regular, i.e. not structured. + The gradient is regular, i.e. not structured. """ x = as_sparse_or_tensor_variable(x) @@ -2541,6 +2543,20 @@ def mul(x, y): raise NotImplementedError() +def mul(x, y): + warn( + "pytensor.sparse.mul is deprecated and will be removed in a future version. Use " + "pytensor.sparse.multiply instead.", + category=DeprecationWarning, + stacklevel=2, + ) + + return multiply(x, y) + + +mul.__doc__ = multiply.__doc__ + + class __ComparisonOpSS(Op): """ Used as a superclass for all comparisons between two sparses matrices. diff --git a/tests/sparse/test_basic.py b/tests/sparse/test_basic.py index 27376fa770..86d703740e 100644 --- a/tests/sparse/test_basic.py +++ b/tests/sparse/test_basic.py @@ -65,8 +65,8 @@ gt, le, lt, - mul, mul_s_v, + multiply, sampling_dot, sp_ones_like, square_diagonal, @@ -724,21 +724,21 @@ def test_AddDS(self): def test_MulSS(self): self._testSS( - mul, + multiply, np.array([[1.0, 0], [3, 0], [0, 6]]), np.array([[1.0, 2], [3, 0], [0, 6]]), ) def test_MulSD(self): self._testSD( - mul, + multiply, np.array([[1.0, 0], [3, 0], [0, 6]]), np.array([[1.0, 2], [3, 0], [0, 6]]), ) def test_MulDS(self): self._testDS( - mul, + multiply, np.array([[1.0, 0], [3, 0], [0, 6]]), np.array([[1.0, 2], [3, 0], [0, 6]]), ) @@ -783,7 +783,7 @@ def _testSS( assert np.all(val.todense() == array1 + array2) if dtype1.startswith("float") and dtype2.startswith("float"): verify_grad_sparse(op, [a, b], structured=False) - elif op is mul: + elif op is multiply: assert np.all(val.todense() == array1 * array2) if dtype1.startswith("float") and dtype2.startswith("float"): verify_grad_sparse(op, [a, b], structured=False) @@ -833,7 +833,7 @@ def _testSD( continue if dtype1.startswith("float") and dtype2.startswith("float"): verify_grad_sparse(op, [a, b], structured=True) - elif op is mul: + elif op is multiply: assert _is_sparse_variable(apb) assert np.all(val.todense() == b.multiply(array1)) assert np.all( @@ -887,7 +887,7 @@ def _testDS( b = b.data if dtype1.startswith("float") and dtype2.startswith("float"): verify_grad_sparse(op, [a, b], structured=True) - elif op is mul: + elif op is multiply: assert _is_sparse_variable(apb) ans = np.array([[1, 0], [9, 0], [0, 36]]) assert np.all(val.todense() == (a.multiply(array2))) From b331f9a9166a6a22fcb6e63f0559dd0a69346d61 Mon Sep 17 00:00:00 2001 From: jessegrabowski Date: Mon, 13 Oct 2025 22:34:57 -0400 Subject: [PATCH 2/3] Rename `sub` -> `subtract` --- pytensor/sparse/basic.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/pytensor/sparse/basic.py b/pytensor/sparse/basic.py index 724f97cb9c..7e19f39e6e 100644 --- a/pytensor/sparse/basic.py +++ b/pytensor/sparse/basic.py @@ -2268,36 +2268,49 @@ def add(x, y): raise NotImplementedError() -def sub(x, y): +def subtract( + x: SparseVariable | TensorVariable, y: SparseVariable | TensorVariable +) -> SparseVariable: """ Subtract two matrices, at least one of which is sparse. - This method will provide the right op according - to the inputs. + This method will provide the right op according to the inputs. Parameters ---------- - x + x: SparseVariable or TensorVariable A matrix variable. - y + y: SparseVariable or TensorVariable A matrix variable. Returns ------- - A sparse matrix - `x` - `y` + result: SparseVariable + Result of `x - y`, as a sparse matrix. Notes ----- At least one of `x` and `y` must be a sparse matrix. - The grad will be structured only when one of the variable will be a dense - matrix. - + The grad will be structured only when one of the variable will be a dense matrix. """ return x + (-y) +def sub(x, y): + warn( + "pytensor.sparse.sub is deprecated and will be removed in a future version. Use " + "pytensor.sparse.subtract instead.", + category=DeprecationWarning, + stacklevel=2, + ) + + return subtract(x, y) + + +sub.__doc__ = subtract.__doc__ + + class MulSS(Op): # mul(sparse, sparse) # See the doc of mul() for more detail From f18803864624c732d2533b9df5da30a1e44d1a8a Mon Sep 17 00:00:00 2001 From: jessegrabowski Date: Mon, 20 Oct 2025 19:25:08 -0500 Subject: [PATCH 3/3] Space... the final frontier --- pytensor/sparse/basic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pytensor/sparse/basic.py b/pytensor/sparse/basic.py index 7e19f39e6e..6a3d8f1e70 100644 --- a/pytensor/sparse/basic.py +++ b/pytensor/sparse/basic.py @@ -2278,9 +2278,9 @@ def subtract( Parameters ---------- - x: SparseVariable or TensorVariable + x : SparseVariable or TensorVariable A matrix variable. - y: SparseVariable or TensorVariable + y : SparseVariable or TensorVariable A matrix variable. Returns @@ -2514,9 +2514,9 @@ def multiply( Parameters ---------- - x: SparseVariable + x : SparseVariable A matrix variable. - y: SparseVariable + y : SparseVariable A matrix variable. Returns