解压 sympy lambdify 函数

Unpack sympy lambdify function

下面是我的代码。

from sympy import * 
  
x = symbols('x')
expr = sin(x)
     
# Use sympy.lambdify() method
f = lambdify(x, expr, "math") 

如果print(f)它给出'<function _lambdifygenerated at 0x100d643a0>',有没有办法从f取回表达式(sin(x))?

help(f) 显示:

Help on function _lambdifygenerated:

_lambdifygenerated(x)
    Created with lambdify. Signature:
    
    func(x)
    
    Expression:
    
    sin(x)
    
    Source code:
    
    def _lambdifygenerated(x):
        return sin(x)

f.__doc__是同一个字符串。

由于我们指定了 'math',标量有效,但数组无效:

In [12]: f(1.23)
Out[12]: 0.9424888019316975

In [13]: f(np.arange(3))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [13], in <module>
----> 1 f(np.arange(3))

File <lambdifygenerated-2>:2, in _lambdifygenerated(x)
      1 def _lambdifygenerated(x):
----> 2     return sin(x)

TypeError: only size-1 arrays can be converted to Python scalars