diff --git a/symengine/__init__.py b/symengine/__init__.py index 4d26ade6..5b7b8946 100644 --- a/symengine/__init__.py +++ b/symengine/__init__.py @@ -56,6 +56,15 @@ from .lib.symengine_wrapper import (Lambdify, LambdifyCSE) def lambdify(args, exprs, **kwargs): + # Lists and tuples explicitly group separate outputs. + 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: def __getattr__(name): diff --git a/symengine/tests/test_lambdify.py b/symengine/tests/test_lambdify.py index fa628a36..8f3a7ecc 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 @@ -575,6 +576,88 @@ 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_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(): + 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_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') + 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