Hankel函数的二阶导数

2020-12-02 19:23:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Simpy绘制Hankel函数的二阶导数。在Mathematica中,这很简单:

D[HankelH2[1,z],z]

这可以通过使用属性进行分析

The first derivative of the Hankel function of the second kind and first order is equal to the difference between two Hankel functions of the second kind of order zero and two, respectively, all these divided by two.

但是我想学习如何用Sympy直接推导它。 到目前为止,我已经试过了:

^{pr2}$

错误信息在我看来难以辨认:

SympifyError Traceback (most recent call last) in () 1 import sympy as sp 2 x = sp.Symbol('x') ----> 3 dh2 = sp.diff(lambda x: hankel2(1,x),x)

/usr/lib/python2.7/dist-packages/sympy/core/function.pyc in diff(f, *symbols, **kwargs) 1639 """ 1640 kwargs.setdefault('evaluate', True) -> 1641 return Derivative(f, *symbols, **kwargs) 1642 1643

/usr/lib/python2.7/dist-packages/sympy/core/function.pyc in new(cls, expr, *variables, **assumptions) 985 def new(cls, expr, *variables, **assumptions): 986 --> 987 expr = sympify(expr) 988 989 # There are no variables, we differentiate wrt all of the free symbols

/usr/lib/python2.7/dist-packages/sympy/core/sympify.pyc in sympify(a, locals, convert_xor, strict, rational, evaluate) 313 expr = parse_expr(a, local_dict=locals, transformations=transformations, evaluate=evaluate) 314 except (TokenError, SyntaxError) as exc: --> 315 raise SympifyError('could not parse %r' % a, exc) 316 317 return expr

SympifyError: Sympify of expression 'could not parse u' at 0x7fdf3eca9e60>'' failed, because of exception being raised: SyntaxError: invalid syntax (, line 1)

你知道我的错误在哪里吗?在

提前谢谢。在

1条回答
网友
1楼 ·

不能在SymPy中使用SciPy函数。SciPy函数是数值函数,而SymPy只适用于符号函数(即SymPy本身内部的函数)。将其包装在lambda中不会改变这一点。在

您需要做的是从SymPy导入hankel2,并使用它。在

>>> from sympy import hankel2, symbols, diff
>>> x = symbols('x')
>>> diff(hankel2(1, x), x)
hankel2(0, x)/2 - hankel2(2, x)/2

如果您想绘制这个图,可以使用sympy.plot函数。或者,可以使用scipy with将其转换为数值函数

^{pr2}$

并将其与maptlotlib这样的绘图库一起使用(看起来hankel2是一个复杂的函数,我认为SymPy的plot处理得不好,所以您可能更喜欢这个选项)。在

相关问题