简体   繁体   中英

Getting the derivatives of Legendre polynomials in Python

I have an expression for a gravitational potential (eq. 15 from here ), and to calculate an orbit I need to evaluate the gravitational force which is the local gradient, and for me that means evaluating the derivative of the Legendre polynomials P2, P4 and P6 at single values tens of thousands of times.

在此处输入图像描述

I can calculate it using the expression in this question , but I'm wondering if there is a way to ask python for the derivative that doesn't explicitly involve me evaluating the derivative as a finite difference.

I couldn't find anything in SciPy to do this automatically. In numpy.polynomial.legendre.Legendre there is a deriv() method but I have no experience operating with polynomial classes.

What would be the fastest way to evaluate the first derivatives of low order Legendre polynomials, one value at a time suitably for numerical integration?

I am aware that this is an old question but still there is no answer on how to calculate the derivatives using numpy/scipy.

This is how it works with numpy and scipy only:

from scipy.special import legendre
import numpy as np
n = 2               # degree of Legendre polynomial
poly = legendre(n)  # coefficients of n^th degree Legendre polynomial 
polyd= poly.deriv() # coefficients of derivative of n^th degree Legendre Polynomial
x = np.linspace(0,1,10000)  # arbitrary coordinates
evald = np.polyval(polyd,x) # evaluate derivative at desired coordinates(s)

Also the accepted answer above contains a small mistake (I am not able to comment yet maybe someone can edit the answer): The derivative should read P2' = 3*x

If you just need the derivatives of P2 , P4 and P6 , that's easy enough to compute by hand and then write down as code... eg

P2 = .5 * (3 * x^2 - 1)

Therefore:

P2' = .75 * x

And you can write that in python as:

def P2_deriv(x):
    return .75 * x

Things don't really get a whole lot faster than that;-). If you need arbitrary legendre polynomials, well... Things start to get a bit trickier at that point...

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