简体   繁体   中英

Generating Legendre Polynomials in Sympy

I have a project where I would like to use a set of associated legendre polynomials. I had the idea to generate the code for a set of degrees and orders by using 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

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.

You're approaching it wrong. From the documentation

The coefficients of Legendre polynomials can be recovered using degree-n Taylor expansion:

>>> 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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