简体   繁体   English

在 Python 中获取 Legendre 多项式的导数

[英]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.我有一个引力势表达式(此处的方程式 15),要计算轨道,我需要评估引力,即局部梯度,对我来说,这意味着评估勒让德多项式P2、P4 和P6单值数万次。

在此处输入图像描述

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.我可以使用这个问题中的表达式来计算它,但我想知道是否有一种方法可以向 python 询问未明确涉及我将导数评估为有限差分的导数。

I couldn't find anything in SciPy to do this automatically.我在 SciPy 中找不到任何可以自动执行此操作的东西。 In numpy.polynomial.legendre.Legendre there is a deriv() method but I have no experience operating with polynomial classes.numpy.polynomial.legendre.Legendre中有一个deriv()方法,但我没有使用多项式类的经验。

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.我知道这是一个老问题,但仍然没有关于如何使用 numpy/scipy 计算导数的答案。

This is how it works with numpy and scipy only:这就是它仅适用于 numpy 和 scipy 的方式:

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上面接受的答案也包含一个小错误(我无法评论但也许有人可以编辑答案):导数应该读作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如果您只需要P2P4P6的导数,那很容易用手计算然后写下来作为代码......例如

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

Therefore:所以:

P2' = .75 * x

And you can write that in python as:您可以在 python 中将其编写为:

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...如果你需要任意勒让德多项式,那么......事情开始变得有点棘手......

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

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