def compute_moments(w, a, b, n, polynomial_class=lambda k, x: x**k):
'''Symbolically calculate the first n moments
int_a^b w(x) P_k(x) dx
where `P_k` is the `k`th polynomials of a specified class. The default
settings are monomials, i.e., `P_k(x)=x^k`, but you can provide any
function with the signature `p(k, x)`, e.g.,
`sympy.polys.orthopolys.legendre_poly` scaled by the inverse of its leading
coefficient `(2n)! / 2^n / (n!)^2`.
'''
x = sympy.Symbol('x')
return numpy.array([
sympy.integrate(w(x) * polynomial_class(k, x), (x, a, b))
for k in range(n)
])
评论列表
文章目录