Processing math: 100%
Ask Your Question
0

Bug in Legendre polynomial.

asked 11 years ago

Shashank gravatar image

updated 11 years ago

I was trying to evaluate Legendre polynomial Pn(x) for a very large n, and it seems for n greater than 50 or so sage gives wrong answer. For eg.

P.<t> = QQ[]
legendre_P(90,t)(0.9)

gives 1.56319477427853e13 whereas wolfram alpha gives -0.08749. Is there an alternate way to calculate Legendre polynomials in sage, which would give the right answer.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 11 years ago

DSM gravatar image

updated 11 years ago

The Legendre polynomial itself looks fine. To avoid the numerical imprecisions, you could work over QQ instead:

sage: legendre_P(90,t)(0.9)
1.56319477427853e13
sage: legendre_P(90,t)(QQ(0.9)).n()
-0.0874931768791928

It looks like you need 100+ bits of precision in order to evaluate the function in the original order:

sage: legendre_P(90, t)(RealIntervalField(100)("0.9"))
0.?e1
sage: legendre_P(90, t)(RealIntervalField(120)("0.9"))
-0.08749?
sage: legendre_P(90, t)(RealIntervalField(150)("0.9"))
-0.08749317687919?

The polynomial is very oscillatory in that there's a large amount of cancellation going on.

If you're really interested in the value, and only secondarily the polynomial, then you could use either scipy or mpmath, depending on how much precision you'll need:

sage: import scipy.special
sage: scipy.special.eval_legendre(90, 0.9)
-0.08749317687919235
sage: import mpmath
sage: mpmath.legendre(90, 0.9)
mpf('-0.08749317687919235')
Preview: (hide)
link

Comments

Thanks a lot. I am interested in the numerical value so scipy function works for me.

Shashank gravatar imageShashank ( 11 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 11 years ago

Seen: 448 times

Last updated: Jan 16 '14