简体   繁体   English

(某些功能)未使用SymPy Lambdify定义

[英](Some function) is not defined with SymPy Lambdify

So I'm writing a script that evaluates Taylor Series. 所以我正在写一个评估泰勒系列的脚本。 However, I want it to evaluate for all types of functions. 但是,我希望它能够评估所有类型的功能。 So I tried, for example, using the function acot(x) . 所以我试过,例如,使用函数acot(x)

x = sy.Symbol('x')
f = acot(x)
...
func = taylor(f,0,3)
taylor_lambda = sy.lambdify(x, func, 'numpy')

The above runs without an exception (except if I use acsch , for example, and it does not run). 以上运行没有例外(例如,如果我使用acsch ,它不会运行)。

But then when it reaches this line: 但是当它达到这条线时:

plt.plot(x1,taylor_lambda(x1),label='taylor approximation')

I get: 我明白了:

NameError: name 'acot' is not defined

I tried to replace numpy with sympy in the lambdify call but this seems to evaluate symbolically. 我尝试用sympy调用中的sympy替换numpy ,但这似乎是象征性的评估。 This is happening with some (more rare functions) but not for others. 这种情况发生在一些(更罕见的功能)但不适用于其他功能。 Thank you! 谢谢!

My imports are as follows: 我的进口如下:

import sympy as sy
import numpy as np
from sympy.functions import *
from sympy import pi, E,acot
import matplotlib.pyplot as plt
import math

The main issue here is that the lambdify function uses the modules argument to define available modules for the supplied function. 这里的主要问题是lambdify函数使用modules参数来定义所提供函数的可用模块。 It seems acot is not available within the numpy namespace. 似乎acotnumpy名称空间中不可用。

Lets reduce this down to something simple: 让我们简化为简单的事情:

import sympy as sy
import numpy as np
from sympy.functions import *

x = sy.Symbol('x')
f = acot(x)
func_lambda = sy.lambdify(x, f, modules='numpy')
print(func_lambda(1))

This raises a NameError as acot is not defined in the numpy namespace. 这会引发NameError因为未在numpy名称空间中定义acot Note the modules argument. 注意模块参数。 If we extend the available modules to sympy , we no longer get a NameError : 如果我们将可用模块扩展为sympy ,我们将不再获得NameError

func_lambda = sy.lambdify(x, f, modules=['numpy', 'sympy'])
print(func_lambda(1))
# Prints pi/4

If you're having trouble with odd functions, you can also add individual functions to the lambdify modules parameter as a dictionary of func_name : function pairs: 如果您遇到奇函数问题,还可以将单独函数作为func_namefunction对的字典添加到lambdify模块参数中:

func_lambda = sy.lambdify(x, f, modules=['numpy', {'acot':acot}])
print(func_lambda(1))
# Prints pi/4

As far as plotting using matplotlib, vectorizing the equation and then plotting works for me: 至于使用matplotlib绘图,矢量化方程,然后为我绘制作品:

import matplotlib.pyplot as plt
vfunc = np.vectorize(func_lambda)
x1 = np.linspace(-10, 10 , 1000)
plt.plot(x1, vfunc(x1),label='acot')
plt.show()

I did have similar problems before and have managed to solve them.Your line 我之前确实遇到过类似的问题,并设法解决了这些问题。你的行

plt.plot(x1,taylor_lambda(x1),label='taylor approximation')

looks OK.I am giving one my older code that works fine,you can just compare. 看起来很好。我给了一个我的旧代码,工作正常,你可以比较。

from sympy.abc import x
from sympy import sin, series
from sympy.utilities.lambdify import lambdify

import numpy as np
import matplotlib.pyplot as plt


func = sin(x)/x
taylor = series(func, n=6).removeO()

evalfunc = lambdify(x, func, modules=['numpy'])
evaltaylor = lambdify(x, taylor, modules=['numpy'])

t = np.linspace(-7.5, 7.5 , 100)
plt.plot(t, evalfunc(t), 'b', label='sin(x)/x')
plt.plot(t, evaltaylor(t), 'r', label='Taylor')
plt.legend(loc='best')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM