lambdify表达式与原生的sympy函数

时间:2022-06-19 20:20:49

I would like to lambdify sympy's exp, but I run into funny issues when trying to evaluate the function at a sympy.Symbol. This

我想讨论sympy的exp,但是当我试图在sympy.Symbol评估函数时遇到了一些有趣的问题。这个

import sympy

t = sympy.Symbol('t')
f = sympy.lambdify(t, t**2)
f(t)  # no problem

works fine, but this

工作正常,但这个

t = sympy.Symbol('t')
f = sympy.lambdify(t, sympy.exp(t))
f(t)

gives

AttributeError: 'Symbol' object has no attribute 'exp'

The same goes for all other native sympy functions I've tried (log, sin, etc.).

对于我尝试过的所有其他原生语音功能(log,sin等)也是如此。

Any idea what's going on?

知道发生了什么事吗?

2 个解决方案

#1


2  

You should specify modules you want to use with modules argument of the lambdify function:

您应该使用lambdify函数的modules参数指定要使用的模块:

f = sympy.lambdify(t, sympy.exp(t), modules=["sympy"])

#2


2  

The main use of lambdify is to allow for a fast numerical evaluation of expressions. This is achieved by replacing abstract and slow SymPy functions (like sympy.exp) with faster ones intended for numbers (like math.exp or numpy.exp). These cannot handle SymPy symbols (like your t) as an argument, which is not what lambdify is intended for anyway.

lambdify的主要用途是允许对表达式进行快速数值计算。这是通过将抽象和慢速SymPy函数(如sympy.exp)替换为更快的数字(如math.exp或numpy.exp)来实现的。这些不能处理SymPy符号(比如你的t)作为参数,这不是lambdify的目的。

If you call lambdify with dummify=False as an additional argument, you get a more meaningful error, when calling f(t), namely:

如果使用dummify = False作为附加参数调用lambdify,则在调用f(t)时会得到更有意义的错误,即:

TypeError: can't convert expression to float

The expression that cannot be converted here is your argument t.

这里无法转换的表达式是你的参数t。

If you want to use a lambdified function with symbols as an argument for some reason, you need to pass modules=["sympy"] as an additional argument to lambdify. This argument specifies which module lambdify uses to replace SymPy functions (like sympy.exp) – in this case, it’s sympy again, so nothing actually happens.

如果由于某种原因想要使用带符号作为参数的lambdified函数,则需要将modules = [“sympy”]作为lambdify的附加参数传递。这个参数指定了lambdify用来替换SymPy函数的模块(比如sympy.exp) - 在这种情况下,它再次表示同情,所以实际上什么都没发生。

#1


2  

You should specify modules you want to use with modules argument of the lambdify function:

您应该使用lambdify函数的modules参数指定要使用的模块:

f = sympy.lambdify(t, sympy.exp(t), modules=["sympy"])

#2


2  

The main use of lambdify is to allow for a fast numerical evaluation of expressions. This is achieved by replacing abstract and slow SymPy functions (like sympy.exp) with faster ones intended for numbers (like math.exp or numpy.exp). These cannot handle SymPy symbols (like your t) as an argument, which is not what lambdify is intended for anyway.

lambdify的主要用途是允许对表达式进行快速数值计算。这是通过将抽象和慢速SymPy函数(如sympy.exp)替换为更快的数字(如math.exp或numpy.exp)来实现的。这些不能处理SymPy符号(比如你的t)作为参数,这不是lambdify的目的。

If you call lambdify with dummify=False as an additional argument, you get a more meaningful error, when calling f(t), namely:

如果使用dummify = False作为附加参数调用lambdify,则在调用f(t)时会得到更有意义的错误,即:

TypeError: can't convert expression to float

The expression that cannot be converted here is your argument t.

这里无法转换的表达式是你的参数t。

If you want to use a lambdified function with symbols as an argument for some reason, you need to pass modules=["sympy"] as an additional argument to lambdify. This argument specifies which module lambdify uses to replace SymPy functions (like sympy.exp) – in this case, it’s sympy again, so nothing actually happens.

如果由于某种原因想要使用带符号作为参数的lambdified函数,则需要将modules = [“sympy”]作为lambdify的附加参数传递。这个参数指定了lambdify用来替换SymPy函数的模块(比如sympy.exp) - 在这种情况下,它再次表示同情,所以实际上什么都没发生。