sympy / sympy

A computer algebra system written in pure Python

Home Page:https://sympy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`lambdify()` and `lambdastr()` can't unpack `sympy.Matrix` typed parameter symbols properly

mammalwong opened this issue · comments

In sympy version 1.12, to reproduce the issue:

for the code below:

import numpy as np
import sympy as sy
from sympy.utilities.lambdify import lambdastr
m_sy = m = sy.Matrix(2,2,sy.symbols('m_0:2_0:2'))
m_np = np.array(m_sy,dtype=object)
m_lst = m_np.tolist() # list of lists

if I run lambdastr([m_np],m) or lambdastr([m_lst],m), it returns below which unpack the inputs as a 2x2 2D array as expected:

lambda _0: (lambda m_0_0,m_0_1,m_1_0,m_1_1: (ImmutableDenseMatrix([[m_0_0, m_0_1], [m_1_0, m_1_1]])))(_0[0][0],_0[0][1],_0[1][0],_0[1][1])

But if I run lambdastr([m_sy],m) instead, it returns below which unpack the inputs as a length 4 flattened array unexpectedly:

lambda _0: (lambda m_0_0,m_0_1,m_1_0,m_1_1: (ImmutableDenseMatrix([[m_0_0, m_0_1], [m_1_0, m_1_1]])))(_0[0],_0[1],_0[2],_0[3])

lambdify() also exhibit the same unpacking issue when the matrix parameter symbol is passed as type of sympy.Matrix, is there a reason that sympy is making its own Matrix class exceptional when passing as a matrix parameter symbol to these lambda generating functions?

If there are some reasons to this, can we add a new keyword parameter to those lambda generating functions to explicitly force the 2d unpacking of sympy.Matrix typed matrix parameter symbols?