Skip to content

Preserve matrix and array shapes in lambdify - #540

Open
Boulea7 wants to merge 4 commits into
symengine:mainfrom
Boulea7:fix/lambdify-shaped-expressions
Open

Boulea7 wants to merge 4 commits into
symengine:mainfrom
Boulea7:fix/lambdify-shaped-expressions

Conversation

@Boulea7

@Boulea7 Boulea7 commented Nov 30, 2026

Copy link
Copy Markdown

Fixes #518.

lambdify(x, A) now returns a single array with A’s shape for a multidimensional NumPy array or a symbolic matrix. Previously it unpacked a NumPy array into rows and a matrix into individual entries. One-dimensional containers still group separate outputs, including a scalar and a matrix with different shapes.

The two regression tests cover a 2 × 3 expression in both representations and a one-dimensional object array of heterogeneous outputs. On macOS arm64, the full suite built from this commit passed 368 tests with 8 skips. The complete lambdify test module also passed 44 tests with one skip using this Python wrapper and the LLVM-enabled 0.15.0 wheel.

OpenAI Codex assisted with the implementation, tests, and PR text.

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 symengine#518.
Comment thread symengine/__init__.py Outdated
from .lib.symengine_wrapper import (Lambdify, LambdifyCSE)

def lambdify(args, exprs, **kwargs):
if len(getattr(exprs, 'shape', ())) > 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.

Check that exprs is a matrix instead of doing this

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.

Updated in db0838a. lambdify now checks concrete matrix and array types. It unpacks other iterables without reading their shape. I added regressions for SymPy MatrixExpr, multidimensional arrays, and iterable outputs.

Comment thread symengine/__init__.py Outdated
Comment on lines +63 to +73
# 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
):

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.

Let's go with something similar to your previous code. if hasattr(exprs, "shape")

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.

Updated in 784c1d7. The wrapper now uses shape to keep multidimensional inputs as one output, including a two-dimensional memoryview. One-dimensional arrays and iterable output groups are unpacked, including groups whose shape is unavailable. Both cases have regression tests.

Comment thread symengine/__init__.py
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?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent return type when lambdifying 2D symbolic arrays/matrices

2 participants