From ae42bba391eae8d0f027f45b928d7e6851e62a4c Mon Sep 17 00:00:00 2001 From: Boulea7 <145515129+Boulea7@users.noreply.github.com> Date: Tue, 1 Dec 2026 03:50:06 +0800 Subject: [PATCH 1/4] Preserve matrix and array shapes in lambdify Forward multidimensional expressions to Lambdify as a single output. Unpacking a matrix into entries or an array into rows discards the original shape. A one-dimensional container can still group heterogeneous outputs. Fixes #518. --- symengine/__init__.py | 2 ++ symengine/tests/test_lambdify.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/symengine/__init__.py b/symengine/__init__.py index 4d26ade6f..690058672 100644 --- a/symengine/__init__.py +++ b/symengine/__init__.py @@ -56,6 +56,8 @@ from .lib.symengine_wrapper import (Lambdify, LambdifyCSE) def lambdify(args, exprs, **kwargs): + if len(getattr(exprs, 'shape', ())) > 1: + return Lambdify(args, exprs, **kwargs) return Lambdify(args, *exprs, **kwargs) else: def __getattr__(name): diff --git a/symengine/tests/test_lambdify.py b/symengine/tests/test_lambdify.py index fa628a363..724aefe5f 100644 --- a/symengine/tests/test_lambdify.py +++ b/symengine/tests/test_lambdify.py @@ -575,6 +575,27 @@ def test_lambdify__sympy(): _sympy_lambdify_heterogeneous_output(sp.lambdify, sp.Matrix) +@unittest.skipUnless(have_numpy, "Numpy not installed") +def test_lambdify_matrix_expression(): + x = se.Symbol('x') + rows = [[x, x+1, x+2], [x+3, x+4, x+5]] + for expr in (se.DenseMatrix(rows), np.array(rows)): + result = se.lambdify(x, expr)(1) + assert isinstance(result, np.ndarray) + np.testing.assert_array_equal(result, [[1, 2, 3], [4, 5, 6]]) + + +@unittest.skipUnless(have_numpy, "Numpy not installed") +def test_lambdify_1d_array_of_outputs(): + x = se.Symbol('x') + exprs = np.empty(2, dtype=object) + exprs[0] = x + exprs[1] = se.DenseMatrix([[x+1, x+2]]) + scalar, matrix = se.lambdify(x, exprs)(1) + np.testing.assert_array_equal(scalar, 1) + np.testing.assert_array_equal(matrix, [[2, 3]]) + + def _test_Lambdify_scalar_vector_matrix(Lambdify): if not have_numpy: return From db0838abf73540322f3350a77a8a9eef323ebac8 Mon Sep 17 00:00:00 2001 From: Boulea7 <145515129+Boulea7@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:44:08 +0800 Subject: [PATCH 2/4] Check explicit matrix and array types in lambdify --- symengine/__init__.py | 16 ++++++++++++++- symengine/tests/test_lambdify.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/symengine/__init__.py b/symengine/__init__.py index 690058672..7b2109590 100644 --- a/symengine/__init__.py +++ b/symengine/__init__.py @@ -53,10 +53,24 @@ from .lib.symengine_wrapper import ComplexMPC if have_numpy: + from sys import modules as _modules + + from numpy import ndarray as _ndarray + from .lib.symengine_wrapper import (Lambdify, LambdifyCSE) def lambdify(args, exprs, **kwargs): - if len(getattr(exprs, 'shape', ())) > 1: + # Recognize SymPy inputs without importing the optional dependency. + sympy = _modules.get('sympy') + matrix_types = ( + MatrixBase, + getattr(sympy, 'MatrixBase', ()), + getattr(sympy, 'MatrixExpr', ()), + ) + array_types = (_ndarray, getattr(sympy, 'NDimArray', ())) + if isinstance(exprs, matrix_types) or ( + isinstance(exprs, array_types) and len(exprs.shape) > 1 + ): return Lambdify(args, exprs, **kwargs) return Lambdify(args, *exprs, **kwargs) else: diff --git a/symengine/tests/test_lambdify.py b/symengine/tests/test_lambdify.py index 724aefe5f..c5d8c3ac5 100644 --- a/symengine/tests/test_lambdify.py +++ b/symengine/tests/test_lambdify.py @@ -585,6 +585,40 @@ def test_lambdify_matrix_expression(): np.testing.assert_array_equal(result, [[1, 2, 3], [4, 5, 6]]) +@unittest.skipUnless(have_numpy, "Numpy not installed") +@unittest.skipUnless(have_sympy, "SymPy not installed") +def test_lambdify_sympy_matrix_and_array_expressions(): + x = sympy.Symbol('x') + rows = [[x, x+1], [x+2, x+3]] + for expr in (sympy.Matrix(rows), sympy.SparseMatrix(rows), sympy.Array(rows)): + result = se.lambdify(x, expr)(1) + assert isinstance(result, np.ndarray) + np.testing.assert_array_equal(result, [[1, 2], [3, 4]]) + + +@unittest.skipUnless(have_numpy, "Numpy not installed") +@unittest.skipUnless(have_sympy, "SymPy not installed") +def test_lambdify_sympy_matrix_expression(): + x = sympy.Symbol('x') + expr = sympy.DiagMatrix(sympy.Matrix([x, x+1])) + result = se.lambdify(x, expr)(1) + assert isinstance(result, np.ndarray) + np.testing.assert_array_equal(result, [[1, 0], [0, 2]]) + + +@unittest.skipUnless(have_numpy, "Numpy not installed") +def test_lambdify_output_group_does_not_read_shape(): + class OutputGroup(list): + @property + def shape(self): + raise AssertionError("not an array shape") + + x = se.Symbol('x') + result = se.lambdify(x, OutputGroup([x, x+1]))(1) + assert isinstance(result, list) + np.testing.assert_array_equal(result, [1, 2]) + + @unittest.skipUnless(have_numpy, "Numpy not installed") def test_lambdify_1d_array_of_outputs(): x = se.Symbol('x') From 49609dc9f9a1929b779232ce1a355c695c3f34c9 Mon Sep 17 00:00:00 2001 From: Boulea7 <145515129+Boulea7@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:09:54 +0800 Subject: [PATCH 3/4] Use shape to preserve multidimensional lambdify outputs --- symengine/__init__.py | 17 ++--------------- symengine/tests/test_lambdify.py | 9 +++++++++ 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/symengine/__init__.py b/symengine/__init__.py index 7b2109590..c59e412e1 100644 --- a/symengine/__init__.py +++ b/symengine/__init__.py @@ -53,24 +53,11 @@ from .lib.symengine_wrapper import ComplexMPC if have_numpy: - from sys import modules as _modules - - from numpy import ndarray as _ndarray - from .lib.symengine_wrapper import (Lambdify, LambdifyCSE) def lambdify(args, exprs, **kwargs): - # Recognize SymPy inputs without importing the optional dependency. - sympy = _modules.get('sympy') - matrix_types = ( - MatrixBase, - getattr(sympy, 'MatrixBase', ()), - getattr(sympy, 'MatrixExpr', ()), - ) - array_types = (_ndarray, getattr(sympy, 'NDimArray', ())) - if isinstance(exprs, matrix_types) or ( - isinstance(exprs, array_types) and len(exprs.shape) > 1 - ): + # Lists and tuples explicitly group separate outputs. + if not isinstance(exprs, (list, tuple)) and len(getattr(exprs, 'shape', ())) > 1: return Lambdify(args, exprs, **kwargs) return Lambdify(args, *exprs, **kwargs) else: diff --git a/symengine/tests/test_lambdify.py b/symengine/tests/test_lambdify.py index c5d8c3ac5..7160e158e 100644 --- a/symengine/tests/test_lambdify.py +++ b/symengine/tests/test_lambdify.py @@ -585,6 +585,15 @@ def test_lambdify_matrix_expression(): np.testing.assert_array_equal(result, [[1, 2, 3], [4, 5, 6]]) +@unittest.skipUnless(have_numpy, "Numpy not installed") +def test_lambdify_memoryview_expression(): + x = se.Symbol('x') + expr = memoryview(np.array([[1, 2], [3, 4]], dtype=np.int64)) + result = se.lambdify(x, expr)(1) + assert isinstance(result, np.ndarray) + np.testing.assert_array_equal(result, [[1, 2], [3, 4]]) + + @unittest.skipUnless(have_numpy, "Numpy not installed") @unittest.skipUnless(have_sympy, "SymPy not installed") def test_lambdify_sympy_matrix_and_array_expressions(): From 784c1d78848dbbd19a425c34d965a69d58239b50 Mon Sep 17 00:00:00 2001 From: Boulea7 <145515129+Boulea7@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:16:26 +0800 Subject: [PATCH 4/4] Keep iterable output groups when shape is unavailable --- symengine/__init__.py | 8 +++++++- symengine/tests/test_lambdify.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/symengine/__init__.py b/symengine/__init__.py index c59e412e1..5b7b8946b 100644 --- a/symengine/__init__.py +++ b/symengine/__init__.py @@ -57,7 +57,13 @@ def lambdify(args, exprs, **kwargs): # Lists and tuples explicitly group separate outputs. - if not isinstance(exprs, (list, tuple)) and len(getattr(exprs, 'shape', ())) > 1: + ndim = 0 + if not isinstance(exprs, (list, tuple)): + try: + ndim = len(exprs.shape) + except (AttributeError, TypeError, ValueError, RuntimeError): + pass + if ndim > 1: return Lambdify(args, exprs, **kwargs) return Lambdify(args, *exprs, **kwargs) else: diff --git a/symengine/tests/test_lambdify.py b/symengine/tests/test_lambdify.py index 7160e158e..8f3a7ecc5 100644 --- a/symengine/tests/test_lambdify.py +++ b/symengine/tests/test_lambdify.py @@ -1,5 +1,6 @@ import array import cmath +from collections import UserList from functools import reduce import itertools from operator import mul @@ -628,6 +629,24 @@ def shape(self): np.testing.assert_array_equal(result, [1, 2]) +@unittest.skipUnless(have_numpy, "Numpy not installed") +def test_lambdify_iterable_with_unavailable_shape(): + class UnknownShape(UserList): + shape = None + + class UnreadableShape(UserList): + @property + def shape(self): + raise RuntimeError("shape is unavailable") + + x = se.Symbol('x') + outputs = [x, se.DenseMatrix([[x+1, x+2]])] + for group in (UnknownShape(outputs), UnreadableShape(outputs)): + scalar, matrix = se.lambdify(x, group)(1) + np.testing.assert_array_equal(scalar, 1) + np.testing.assert_array_equal(matrix, [[2, 3]]) + + @unittest.skipUnless(have_numpy, "Numpy not installed") def test_lambdify_1d_array_of_outputs(): x = se.Symbol('x')