简体   繁体   English

在Sympy中生成Legendre多项式

[英]Generating Legendre Polynomials in Sympy

I have a project where I would like to use a set of associated legendre polynomials. 我有一个项目,我想使用一组关联的Legendre多项式。 I had the idea to generate the code for a set of degrees and orders by using sympy. 我的想法是通过使用sympy为一组学位和命令生成代码。

I can get sympy to calculate specific values like explained in the docs: http://docs.sympy.org/dev/modules/mpmath/functions/orthogonal.html#legenp 我可以得到sympy来计算特定值,如文档中所述: http ://docs.sympy.org/dev/modules/mpmath/functions/orthogonal.html#legenp

I cannot get sympy however to obtain the polynomial's coefficients. 但是,我无法获得多项式的系数。

>>> from sympy import Symbol
x
>>> x = Symbol('x')

>>> from sympy.mpmath import *

>>> legenp(1, 0, x)

TypeError: cannot create mpf from x

I am not very experienced with either sympy or other CAS, so I guess there must be a way to do this that I am not aware of. 我对sympy或其他CAS都不是很有经验,所以我想一定有一种我不知道的方法。

You're approaching it wrong. 您正在错误地处理它。 From the documentation 文档中

The coefficients of Legendre polynomials can be recovered using degree-n Taylor expansion: 勒让德多项式的系数可以使用n次泰勒展开式恢复:

>>> for n in range(5):
...     nprint(chop(taylor(lambda x: legendre(n, x), 0, n)))
...
[1.0]
[0.0, 1.0]
[-0.5, 0.0, 1.5]
[0.0, -1.5, 0.0, 2.5]
[0.375, 0.0, -3.75, 0.0, 4.375]

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

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