解压 sympylambdaify 函数

发布于 2025-01-09 08:32:52 字数 310 浏览 0 评论 0原文

下面的代码我有。

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

如果 print(f) 给出 '',有没有办法取回表达式(sin(x)) 来自 f?

Below code I have.

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

If print(f) its giving '<function _lambdifygenerated at 0x100d643a0>', is there any way to get back the expression(sin(x)) from f?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心荒芜 2025-01-16 08:32:52

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

help(f) displays:

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__ is the same string.

Since we specified 'math', scalars work, but not arrays:

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
守不住的情 2025-01-16 08:32:52

我进入了 Sympy 的源代码,发现了这一行,其中羔羊化函数的内容在变量 funcstr 中定义:

https://github.com/sympy/sympy/blob/88b9cba9d7e633ec769baf1fc5267acfd7f54788/sympy/utilities/lambdify.py#L863

# Create the function definition code and execute it
funcname = '_lambdifygenerated'
if _module_present('tensorflow', namespaces):
    funcprinter = _TensorflowEvaluatorPrinter(printer, dummify) # type: _EvaluatorPrinter
else:
    funcprinter = _EvaluatorPrinter(printer, dummify)

if cse == True:
    from sympy.simplify.cse_main import cse as _cse
    cses, _expr = _cse(expr, list=False)
elif callable(cse):
    cses, _expr = cse(expr)
else:
    cses, _expr = (), expr
funcstr = funcprinter.doprint(funcname, iterable_args, _expr, cses=cses)

目前 Sympy 不返回 funcstr,但我在本地安装中修改了它,这样它就可以了,而且似乎成为你所追求的。

import sympy as sy 
  
x = sy.symbols('x')
expr = sy.sin(x)
     
# Use sympy.lambdify() method
f, funcstr = sy.lambdify(x, expr, "math") 

funcstr.split(":")[-1].split("return")[-1].strip()
>>> 'sin(x)'

我获取内容的方式有点狡猾,而且不一定可靠,但我希望它能为您提供一个工作起点。

请注意,如果您使用参数 cse,则此提取将不准确,因为它将错过之前的简化。

I have gone into the source code of Sympy and found this line where the content of the lambdified function is defined in the variable funcstr:

https://github.com/sympy/sympy/blob/88b9cba9d7e633ec769baf1fc5267acfd7f54788/sympy/utilities/lambdify.py#L863

# Create the function definition code and execute it
funcname = '_lambdifygenerated'
if _module_present('tensorflow', namespaces):
    funcprinter = _TensorflowEvaluatorPrinter(printer, dummify) # type: _EvaluatorPrinter
else:
    funcprinter = _EvaluatorPrinter(printer, dummify)

if cse == True:
    from sympy.simplify.cse_main import cse as _cse
    cses, _expr = _cse(expr, list=False)
elif callable(cse):
    cses, _expr = cse(expr)
else:
    cses, _expr = (), expr
funcstr = funcprinter.doprint(funcname, iterable_args, _expr, cses=cses)

Currently Sympy does not return funcstr, but I modified it in my local installation so that it does, and it seems to be what you are after.

import sympy as sy 
  
x = sy.symbols('x')
expr = sy.sin(x)
     
# Use sympy.lambdify() method
f, funcstr = sy.lambdify(x, expr, "math") 

funcstr.split(":")[-1].split("return")[-1].strip()
>>> 'sin(x)'

The way I got the content out is a bit dodgy, and not necessarily robust, but I hope it gives you a starting point to work with.

Beware that if you used the argument cse, this extraction will not be accurate, since it will miss out on the previous simplifications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文