Repository navigation
Conversation
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.
| from .lib.symengine_wrapper import (Lambdify, LambdifyCSE) | ||
|
|
||
| def lambdify(args, exprs, **kwargs): | ||
| if len(getattr(exprs, 'shape', ())) > 1: |
There was a problem hiding this comment.
Check that exprs is a matrix instead of doing this
There was a problem hiding this comment.
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.
| # 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 | ||
| ): |
There was a problem hiding this comment.
Let's go with something similar to your previous code. if hasattr(exprs, "shape")
There was a problem hiding this comment.
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.
| ndim = len(exprs.shape) | ||
| except (AttributeError, TypeError, ValueError, RuntimeError): | ||
| pass | ||
| if ndim > 1: |
There was a problem hiding this comment.
why does ndim has to be greater than 1?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This PR doesn't change the output of lambdify(x, np.array([x, x + 1]))(1) right?
Fixes #518.
lambdify(x, A)now returns a single array withA’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.