Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Inconsistent return type when lambdifying 2D symbolic arrays/matrices #518

@YulienPohl

Description

@YulienPohl

I don't know if this is intentional, but using lambdify with symengine on a 2d symbolic ndarray or matrix creates a function that does not return a 2d ndarray. Instead, it returns a list of ndarrays.
Even stranger, when using a Matrix, the return value is a flat list of ndarrays, which is not the same as the converted symbolic numpy array.

When using lambdify from sympy on a matrix, a function with a pure 2d ndarray is returned.

import sympy as sp
import symengine as se
import numpy as np

x = se.Symbol("x")

A = np.array([
    [x, x+1],
    [x+2, x+3]
])
f = se.lambdify(x, A)
y = f(1.0)
print(type(y), y)
# <class 'list'>
# [array([1., 2.]), array([3., 4.])]

A = se.Matrix(2, 2, [
    [x, x+1],
    [x+2, x+3]
])
f = se.lambdify(x, A)
y = f(1.0)
print(type(y), y)
# <class 'list'>
# [array(1.), array(2.), array(3.), array(4.)]


x = sp.Symbol("x")

A = sp.Matrix([
    [x, x+1],
    [x+2, x+3]
])
f = sp.lambdify(x, A)
y = f(1.0)
print(type(y), y)
# <class 'numpy.ndarray'>
# [[1. 2.]
# [3. 4.]]

The current workaround is to convert the list of arrays into a 2d ndarray.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions