wirrbel
wirrbel

Reputation: 3299

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.

Upvotes: 1

Views: 2090

Answers (1)

Francesco Gramano
Francesco Gramano

Reputation: 364

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]

Upvotes: 1

Related Questions