Python:SymPy lamdify abs 与 NumPy 一起使用 [英] Python: SymPy lambdify abs for use with NumPy

查看:41
本文介绍了Python:SymPy lamdify abs 与 NumPy 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SymPy 对表达式进行符号操作,该表达式使用 NumPy 对大量数据进行评估.为了加快速度,我使用了 sympy.lambdify,但我无法让 abs 工作.

I am using SymPy for symbolic manipulation of expression, which is evaluated on huge amounts of data using NumPy. To speed up things, I use sympy.lambdify, but I cannot get abs to work.

import sympy
import numpy as np

x = sympy.symbols('x',real=True)

def f(x):
    return 1-sympy.Abs(x)

def g(x):
    return 1-sympy.sqrt(x)


fl = sympy.lambdify(x,f(x),'numpy')
gl = sympy.lambdify(x,g(x),'numpy')
gl(1) # success
gl(np.array([1,2,3]))
fl(2) # NameError: global name 'Abs' is not defined
fl(np.array([1,2,3])) # Same error

一个选项是为 lamdify 调用使用 'sympy' 参数,但是我不能使用数组.我曾尝试使用 sympy.abs 和 numpy.abs,但没有成功.

An option would be to use the 'sympy' argument for the lambdify call, but then I cannot use arrays. I have tried using sympy.abs and numpy.abs, but without success.

我在一个程序中使用它来解决使用逆置换和一些表格积分的繁琐积分,但是使用 abs 函数而不是显式处理不同区域的选项会非常方便.

I use it in a program for solving cumbersome integrals using inverse-substitution and some tabulated integrals, but it would be so convenient with the option using an abs function instead of explicitly handling different regions.

sympy.Abs 确实定义了

sympy.Abs is indeed defined

提前致谢

推荐答案

这看起来像是在 SymPy 的最新版本中已修复的错误:https://code.google.com/p/sympy/issues/detail?id=2654它适用于 Python 2.7.9、SymPy 0.7.3 和 Python 3.3.5、SymPy 0.7.5.

This looks like a bug that has been fixed in recent versions of SymPy: https://code.google.com/p/sympy/issues/detail?id=2654 It works on Python 2.7.9, SymPy 0.7.3 and Python 3.3.5, SymPy 0.7.5.

这篇关于Python:SymPy lamdify abs 与 NumPy 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆