#连带勒让德多项式 def LegendrePolynomials(N, x): """ N 为勒让德多项式的阶数 x 为自变量 sym.symbol对象 """ if N == 0: return 1 if N == 1: return x p0 = LegendrePolynomials(0, x) p1 = LegendrePolynomials(1, x) assert N >= 2 for i in range(1, N): p = (2 * i + 1) / (i + 1) * x * p1 - i / (i + 1) * p0 p0 = p1 p1 = p return sym.simplify(p1)
# Factorial function # 分数阶乘函数 def Factorial(n): if n == 0: return 1 elif n < 0: print('there is a number < 0 in the Factorial function!') return 1 else: s = 1 while n > 0: s *= n n -= 1 return s
# Associated Laguerre polynomials # 输出对应k阶参数p的关联拉盖尔多项式系数列表 def Associate_Laguerre(k, p): c_list = [] for j in range(k+1): c_u = Factorial(k+p) c_l = Factorial(k-j)*Factorial(p+j)*Factorial(j) c = math.pow(-1, j)*c_u/c_l c_list.append(c) return c_list
# Testing Laguerre polynomial for i in range(5): print(Laguerre(i)) # Testing Associated Laguerre polynomials for n in range(5): for l in range(n): p = l + 0.5 k = (n-l)//2 print(Associate_Laguerre(k, p))
# 生成勒让德多项式和关联勒让德多项式 import math import numpy as np
# Comb function # 排列组合函数 def comb(k,l): result_u = 1 temp = k for i in range(l): result_u *= temp temp -= 1 result_l = math.factorial(l) return result_u/result_l
# Legendre Polynomials # 输出对应n阶之下所有阶数勒让德多项式系数列表 def legendre(n): a = np.zeros((n, n)) for l in range(n): for k in range(l+1): if l==0 and k==0: a[l][k] = 1 elif (l+k) % 2 == 0: a_back = comb(l, int((l+k)/2)) a_u = np.prod(np.arange(1, l+1)+np.ones(l)*k) a[l][k] = np.power(-1, (l-k)/2)*a_u*a_back/(2**l*math.factorial(l)) return a
# Associated Legendre Polynomials # 输出对应参数为n,m,x的关联勒让德多项式的值 def associate_legendre(n, m, x): leg = legendre(n+1)[-1] result_front = (1-x**2)**(m/2) ass_leg = leg[m:] temp = 0 for i in range(len(ass_leg)): driv_max_num = i + m for j in range(m): ass_leg[i] *= driv_max_num driv_max_num -= 1 temp += ass_leg[i]*(x**i) return result_front*temp