在 Python 中获取 Legendre 多项式的导数
Getting the derivatives of Legendre polynomials in Python
我有一个引力势的表达式(来自 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 和 P6 的方程式 15 在单个值数万次。
我可以使用 this question 中的表达式来计算它,但我想知道是否有一种方法可以向 python 询问导数,而该导数没有明确涉及我将导数评估为有限差分。
我在 SciPy 中找不到任何自动执行此操作的内容。在 numpy.polynomial.legendre.Legendre
中有一个 deriv()
方法,但我没有使用多项式 类 的经验。
评估低阶勒让德多项式的一阶导数的最快方法是什么,一次一个值适合于数值积分?
如果你只需要 P2
、P4
和 P6
的导数,这很容易用手计算然后写下来作为代码...例如
P2 = .5 * (3 * x^2 - 1)
因此:
P2' = .75 * x
您可以在 python 中将其写为:
def P2_deriv(x):
return .75 * x
事情并没有比这快多少;-)。如果你需要任意勒让德多项式,那么......事情开始变得有点棘手......
我知道这是一个老问题,但仍然没有关于如何使用 numpy/scipy.
计算导数的答案
这就是它如何与 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)
此外,上面接受的答案包含一个小错误(我无法发表评论,但也许有人可以编辑答案):
导数应为 P2' = 3*x
我有一个引力势的表达式(来自 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 和 P6 的方程式 15 在单个值数万次。
我可以使用 this question 中的表达式来计算它,但我想知道是否有一种方法可以向 python 询问导数,而该导数没有明确涉及我将导数评估为有限差分。
我在 SciPy 中找不到任何自动执行此操作的内容。在 numpy.polynomial.legendre.Legendre
中有一个 deriv()
方法,但我没有使用多项式 类 的经验。
评估低阶勒让德多项式的一阶导数的最快方法是什么,一次一个值适合于数值积分?
如果你只需要 P2
、P4
和 P6
的导数,这很容易用手计算然后写下来作为代码...例如
P2 = .5 * (3 * x^2 - 1)
因此:
P2' = .75 * x
您可以在 python 中将其写为:
def P2_deriv(x):
return .75 * x
事情并没有比这快多少;-)。如果你需要任意勒让德多项式,那么......事情开始变得有点棘手......
我知道这是一个老问题,但仍然没有关于如何使用 numpy/scipy.
计算导数的答案这就是它如何与 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)
此外,上面接受的答案包含一个小错误(我无法发表评论,但也许有人可以编辑答案):
导数应为 P2' = 3*x