Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions symengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does ndim has to be greater than 1?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this PR, lambdify(x, np.array([x, x + 1]))(1) returned [array(1.), array(2.)]. Passing the vector as one expression yields array([1., 2.]). Passing the scalar-and-matrix object array from test_lambdify_1d_array_of_outputs as one expression also raises TypeError. The ndim > 1 cutoff preserves these existing 1-D calls and covers the 2-D inputs in #518.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR doesn't change the output of lambdify(x, np.array([x, x + 1]))(1) right?

return Lambdify(args, exprs, **kwargs)
return Lambdify(args, *exprs, **kwargs)
else:
def __getattr__(name):
Expand Down
83 changes: 83 additions & 0 deletions symengine/tests/test_lambdify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import array
import cmath
from collections import UserList
from functools import reduce
import itertools
from operator import mul
Expand Down Expand Up @@ -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
Expand Down