m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1297 lines
40 KiB

6 months ago
  1. """
  2. This module mainly implements special orthogonal polynomials.
  3. See also functions.combinatorial.numbers which contains some
  4. combinatorial polynomials.
  5. """
  6. from sympy.core import Rational
  7. from sympy.core.function import Function, ArgumentIndexError
  8. from sympy.core.singleton import S
  9. from sympy.core.symbol import Dummy
  10. from sympy.functions.combinatorial.factorials import binomial, factorial, RisingFactorial
  11. from sympy.functions.elementary.complexes import re
  12. from sympy.functions.elementary.exponential import exp
  13. from sympy.functions.elementary.integers import floor
  14. from sympy.functions.elementary.miscellaneous import sqrt
  15. from sympy.functions.elementary.trigonometric import cos, sec
  16. from sympy.functions.special.gamma_functions import gamma
  17. from sympy.functions.special.hyper import hyper
  18. from sympy.polys.orthopolys import (
  19. jacobi_poly,
  20. gegenbauer_poly,
  21. chebyshevt_poly,
  22. chebyshevu_poly,
  23. laguerre_poly,
  24. hermite_poly,
  25. legendre_poly
  26. )
  27. _x = Dummy('x')
  28. class OrthogonalPolynomial(Function):
  29. """Base class for orthogonal polynomials.
  30. """
  31. @classmethod
  32. def _eval_at_order(cls, n, x):
  33. if n.is_integer and n >= 0:
  34. return cls._ortho_poly(int(n), _x).subs(_x, x)
  35. def _eval_conjugate(self):
  36. return self.func(self.args[0], self.args[1].conjugate())
  37. #----------------------------------------------------------------------------
  38. # Jacobi polynomials
  39. #
  40. class jacobi(OrthogonalPolynomial):
  41. r"""
  42. Jacobi polynomial $P_n^{\left(\alpha, \beta\right)}(x)$.
  43. Explanation
  44. ===========
  45. ``jacobi(n, alpha, beta, x)`` gives the nth Jacobi polynomial
  46. in x, $P_n^{\left(\alpha, \beta\right)}(x)$.
  47. The Jacobi polynomials are orthogonal on $[-1, 1]$ with respect
  48. to the weight $\left(1-x\right)^\alpha \left(1+x\right)^\beta$.
  49. Examples
  50. ========
  51. >>> from sympy import jacobi, S, conjugate, diff
  52. >>> from sympy.abc import a, b, n, x
  53. >>> jacobi(0, a, b, x)
  54. 1
  55. >>> jacobi(1, a, b, x)
  56. a/2 - b/2 + x*(a/2 + b/2 + 1)
  57. >>> jacobi(2, a, b, x)
  58. a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 + x**2*(a**2/8 + a*b/4 + 7*a/8 + b**2/8 + 7*b/8 + 3/2) + x*(a**2/4 + 3*a/4 - b**2/4 - 3*b/4) - 1/2
  59. >>> jacobi(n, a, b, x)
  60. jacobi(n, a, b, x)
  61. >>> jacobi(n, a, a, x)
  62. RisingFactorial(a + 1, n)*gegenbauer(n,
  63. a + 1/2, x)/RisingFactorial(2*a + 1, n)
  64. >>> jacobi(n, 0, 0, x)
  65. legendre(n, x)
  66. >>> jacobi(n, S(1)/2, S(1)/2, x)
  67. RisingFactorial(3/2, n)*chebyshevu(n, x)/factorial(n + 1)
  68. >>> jacobi(n, -S(1)/2, -S(1)/2, x)
  69. RisingFactorial(1/2, n)*chebyshevt(n, x)/factorial(n)
  70. >>> jacobi(n, a, b, -x)
  71. (-1)**n*jacobi(n, b, a, x)
  72. >>> jacobi(n, a, b, 0)
  73. gamma(a + n + 1)*hyper((-b - n, -n), (a + 1,), -1)/(2**n*factorial(n)*gamma(a + 1))
  74. >>> jacobi(n, a, b, 1)
  75. RisingFactorial(a + 1, n)/factorial(n)
  76. >>> conjugate(jacobi(n, a, b, x))
  77. jacobi(n, conjugate(a), conjugate(b), conjugate(x))
  78. >>> diff(jacobi(n,a,b,x), x)
  79. (a/2 + b/2 + n/2 + 1/2)*jacobi(n - 1, a + 1, b + 1, x)
  80. See Also
  81. ========
  82. gegenbauer,
  83. chebyshevt_root, chebyshevu, chebyshevu_root,
  84. legendre, assoc_legendre,
  85. hermite,
  86. laguerre, assoc_laguerre,
  87. sympy.polys.orthopolys.jacobi_poly,
  88. sympy.polys.orthopolys.gegenbauer_poly
  89. sympy.polys.orthopolys.chebyshevt_poly
  90. sympy.polys.orthopolys.chebyshevu_poly
  91. sympy.polys.orthopolys.hermite_poly
  92. sympy.polys.orthopolys.legendre_poly
  93. sympy.polys.orthopolys.laguerre_poly
  94. References
  95. ==========
  96. .. [1] https://en.wikipedia.org/wiki/Jacobi_polynomials
  97. .. [2] http://mathworld.wolfram.com/JacobiPolynomial.html
  98. .. [3] http://functions.wolfram.com/Polynomials/JacobiP/
  99. """
  100. @classmethod
  101. def eval(cls, n, a, b, x):
  102. # Simplify to other polynomials
  103. # P^{a, a}_n(x)
  104. if a == b:
  105. if a == Rational(-1, 2):
  106. return RisingFactorial(S.Half, n) / factorial(n) * chebyshevt(n, x)
  107. elif a.is_zero:
  108. return legendre(n, x)
  109. elif a == S.Half:
  110. return RisingFactorial(3*S.Half, n) / factorial(n + 1) * chebyshevu(n, x)
  111. else:
  112. return RisingFactorial(a + 1, n) / RisingFactorial(2*a + 1, n) * gegenbauer(n, a + S.Half, x)
  113. elif b == -a:
  114. # P^{a, -a}_n(x)
  115. return gamma(n + a + 1) / gamma(n + 1) * (1 + x)**(a/2) / (1 - x)**(a/2) * assoc_legendre(n, -a, x)
  116. if not n.is_Number:
  117. # Symbolic result P^{a,b}_n(x)
  118. # P^{a,b}_n(-x) ---> (-1)**n * P^{b,a}_n(-x)
  119. if x.could_extract_minus_sign():
  120. return S.NegativeOne**n * jacobi(n, b, a, -x)
  121. # We can evaluate for some special values of x
  122. if x.is_zero:
  123. return (2**(-n) * gamma(a + n + 1) / (gamma(a + 1) * factorial(n)) *
  124. hyper([-b - n, -n], [a + 1], -1))
  125. if x == S.One:
  126. return RisingFactorial(a + 1, n) / factorial(n)
  127. elif x is S.Infinity:
  128. if n.is_positive:
  129. # Make sure a+b+2*n \notin Z
  130. if (a + b + 2*n).is_integer:
  131. raise ValueError("Error. a + b + 2*n should not be an integer.")
  132. return RisingFactorial(a + b + n + 1, n) * S.Infinity
  133. else:
  134. # n is a given fixed integer, evaluate into polynomial
  135. return jacobi_poly(n, a, b, x)
  136. def fdiff(self, argindex=4):
  137. from sympy.concrete.summations import Sum
  138. if argindex == 1:
  139. # Diff wrt n
  140. raise ArgumentIndexError(self, argindex)
  141. elif argindex == 2:
  142. # Diff wrt a
  143. n, a, b, x = self.args
  144. k = Dummy("k")
  145. f1 = 1 / (a + b + n + k + 1)
  146. f2 = ((a + b + 2*k + 1) * RisingFactorial(b + k + 1, n - k) /
  147. ((n - k) * RisingFactorial(a + b + k + 1, n - k)))
  148. return Sum(f1 * (jacobi(n, a, b, x) + f2*jacobi(k, a, b, x)), (k, 0, n - 1))
  149. elif argindex == 3:
  150. # Diff wrt b
  151. n, a, b, x = self.args
  152. k = Dummy("k")
  153. f1 = 1 / (a + b + n + k + 1)
  154. f2 = (-1)**(n - k) * ((a + b + 2*k + 1) * RisingFactorial(a + k + 1, n - k) /
  155. ((n - k) * RisingFactorial(a + b + k + 1, n - k)))
  156. return Sum(f1 * (jacobi(n, a, b, x) + f2*jacobi(k, a, b, x)), (k, 0, n - 1))
  157. elif argindex == 4:
  158. # Diff wrt x
  159. n, a, b, x = self.args
  160. return S.Half * (a + b + n + 1) * jacobi(n - 1, a + 1, b + 1, x)
  161. else:
  162. raise ArgumentIndexError(self, argindex)
  163. def _eval_rewrite_as_polynomial(self, n, a, b, x, **kwargs):
  164. from sympy.concrete.summations import Sum
  165. # Make sure n \in N
  166. if n.is_negative or n.is_integer is False:
  167. raise ValueError("Error: n should be a non-negative integer.")
  168. k = Dummy("k")
  169. kern = (RisingFactorial(-n, k) * RisingFactorial(a + b + n + 1, k) * RisingFactorial(a + k + 1, n - k) /
  170. factorial(k) * ((1 - x)/2)**k)
  171. return 1 / factorial(n) * Sum(kern, (k, 0, n))
  172. def _eval_conjugate(self):
  173. n, a, b, x = self.args
  174. return self.func(n, a.conjugate(), b.conjugate(), x.conjugate())
  175. def jacobi_normalized(n, a, b, x):
  176. r"""
  177. Jacobi polynomial $P_n^{\left(\alpha, \beta\right)}(x)$.
  178. Explanation
  179. ===========
  180. ``jacobi_normalized(n, alpha, beta, x)`` gives the nth
  181. Jacobi polynomial in *x*, $P_n^{\left(\alpha, \beta\right)}(x)$.
  182. The Jacobi polynomials are orthogonal on $[-1, 1]$ with respect
  183. to the weight $\left(1-x\right)^\alpha \left(1+x\right)^\beta$.
  184. This functions returns the polynomials normilzed:
  185. .. math::
  186. \int_{-1}^{1}
  187. P_m^{\left(\alpha, \beta\right)}(x)
  188. P_n^{\left(\alpha, \beta\right)}(x)
  189. (1-x)^{\alpha} (1+x)^{\beta} \mathrm{d}x
  190. = \delta_{m,n}
  191. Examples
  192. ========
  193. >>> from sympy import jacobi_normalized
  194. >>> from sympy.abc import n,a,b,x
  195. >>> jacobi_normalized(n, a, b, x)
  196. jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)/((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1)))
  197. Parameters
  198. ==========
  199. n : integer degree of polynomial
  200. a : alpha value
  201. b : beta value
  202. x : symbol
  203. See Also
  204. ========
  205. gegenbauer,
  206. chebyshevt_root, chebyshevu, chebyshevu_root,
  207. legendre, assoc_legendre,
  208. hermite,
  209. laguerre, assoc_laguerre,
  210. sympy.polys.orthopolys.jacobi_poly,
  211. sympy.polys.orthopolys.gegenbauer_poly
  212. sympy.polys.orthopolys.chebyshevt_poly
  213. sympy.polys.orthopolys.chebyshevu_poly
  214. sympy.polys.orthopolys.hermite_poly
  215. sympy.polys.orthopolys.legendre_poly
  216. sympy.polys.orthopolys.laguerre_poly
  217. References
  218. ==========
  219. .. [1] https://en.wikipedia.org/wiki/Jacobi_polynomials
  220. .. [2] http://mathworld.wolfram.com/JacobiPolynomial.html
  221. .. [3] http://functions.wolfram.com/Polynomials/JacobiP/
  222. """
  223. nfactor = (S(2)**(a + b + 1) * (gamma(n + a + 1) * gamma(n + b + 1))
  224. / (2*n + a + b + 1) / (factorial(n) * gamma(n + a + b + 1)))
  225. return jacobi(n, a, b, x) / sqrt(nfactor)
  226. #----------------------------------------------------------------------------
  227. # Gegenbauer polynomials
  228. #
  229. class gegenbauer(OrthogonalPolynomial):
  230. r"""
  231. Gegenbauer polynomial $C_n^{\left(\alpha\right)}(x)$.
  232. Explanation
  233. ===========
  234. ``gegenbauer(n, alpha, x)`` gives the nth Gegenbauer polynomial
  235. in x, $C_n^{\left(\alpha\right)}(x)$.
  236. The Gegenbauer polynomials are orthogonal on $[-1, 1]$ with
  237. respect to the weight $\left(1-x^2\right)^{\alpha-\frac{1}{2}}$.
  238. Examples
  239. ========
  240. >>> from sympy import gegenbauer, conjugate, diff
  241. >>> from sympy.abc import n,a,x
  242. >>> gegenbauer(0, a, x)
  243. 1
  244. >>> gegenbauer(1, a, x)
  245. 2*a*x
  246. >>> gegenbauer(2, a, x)
  247. -a + x**2*(2*a**2 + 2*a)
  248. >>> gegenbauer(3, a, x)
  249. x**3*(4*a**3/3 + 4*a**2 + 8*a/3) + x*(-2*a**2 - 2*a)
  250. >>> gegenbauer(n, a, x)
  251. gegenbauer(n, a, x)
  252. >>> gegenbauer(n, a, -x)
  253. (-1)**n*gegenbauer(n, a, x)
  254. >>> gegenbauer(n, a, 0)
  255. 2**n*sqrt(pi)*gamma(a + n/2)/(gamma(a)*gamma(1/2 - n/2)*gamma(n + 1))
  256. >>> gegenbauer(n, a, 1)
  257. gamma(2*a + n)/(gamma(2*a)*gamma(n + 1))
  258. >>> conjugate(gegenbauer(n, a, x))
  259. gegenbauer(n, conjugate(a), conjugate(x))
  260. >>> diff(gegenbauer(n, a, x), x)
  261. 2*a*gegenbauer(n - 1, a + 1, x)
  262. See Also
  263. ========
  264. jacobi,
  265. chebyshevt_root, chebyshevu, chebyshevu_root,
  266. legendre, assoc_legendre,
  267. hermite,
  268. laguerre, assoc_laguerre,
  269. sympy.polys.orthopolys.jacobi_poly
  270. sympy.polys.orthopolys.gegenbauer_poly
  271. sympy.polys.orthopolys.chebyshevt_poly
  272. sympy.polys.orthopolys.chebyshevu_poly
  273. sympy.polys.orthopolys.hermite_poly
  274. sympy.polys.orthopolys.legendre_poly
  275. sympy.polys.orthopolys.laguerre_poly
  276. References
  277. ==========
  278. .. [1] https://en.wikipedia.org/wiki/Gegenbauer_polynomials
  279. .. [2] http://mathworld.wolfram.com/GegenbauerPolynomial.html
  280. .. [3] http://functions.wolfram.com/Polynomials/GegenbauerC3/
  281. """
  282. @classmethod
  283. def eval(cls, n, a, x):
  284. # For negative n the polynomials vanish
  285. # See http://functions.wolfram.com/Polynomials/GegenbauerC3/03/01/03/0012/
  286. if n.is_negative:
  287. return S.Zero
  288. # Some special values for fixed a
  289. if a == S.Half:
  290. return legendre(n, x)
  291. elif a == S.One:
  292. return chebyshevu(n, x)
  293. elif a == S.NegativeOne:
  294. return S.Zero
  295. if not n.is_Number:
  296. # Handle this before the general sign extraction rule
  297. if x == S.NegativeOne:
  298. if (re(a) > S.Half) == True:
  299. return S.ComplexInfinity
  300. else:
  301. return (cos(S.Pi*(a+n)) * sec(S.Pi*a) * gamma(2*a+n) /
  302. (gamma(2*a) * gamma(n+1)))
  303. # Symbolic result C^a_n(x)
  304. # C^a_n(-x) ---> (-1)**n * C^a_n(x)
  305. if x.could_extract_minus_sign():
  306. return S.NegativeOne**n * gegenbauer(n, a, -x)
  307. # We can evaluate for some special values of x
  308. if x.is_zero:
  309. return (2**n * sqrt(S.Pi) * gamma(a + S.Half*n) /
  310. (gamma((1 - n)/2) * gamma(n + 1) * gamma(a)) )
  311. if x == S.One:
  312. return gamma(2*a + n) / (gamma(2*a) * gamma(n + 1))
  313. elif x is S.Infinity:
  314. if n.is_positive:
  315. return RisingFactorial(a, n) * S.Infinity
  316. else:
  317. # n is a given fixed integer, evaluate into polynomial
  318. return gegenbauer_poly(n, a, x)
  319. def fdiff(self, argindex=3):
  320. from sympy.concrete.summations import Sum
  321. if argindex == 1:
  322. # Diff wrt n
  323. raise ArgumentIndexError(self, argindex)
  324. elif argindex == 2:
  325. # Diff wrt a
  326. n, a, x = self.args
  327. k = Dummy("k")
  328. factor1 = 2 * (1 + (-1)**(n - k)) * (k + a) / ((k +
  329. n + 2*a) * (n - k))
  330. factor2 = 2*(k + 1) / ((k + 2*a) * (2*k + 2*a + 1)) + \
  331. 2 / (k + n + 2*a)
  332. kern = factor1*gegenbauer(k, a, x) + factor2*gegenbauer(n, a, x)
  333. return Sum(kern, (k, 0, n - 1))
  334. elif argindex == 3:
  335. # Diff wrt x
  336. n, a, x = self.args
  337. return 2*a*gegenbauer(n - 1, a + 1, x)
  338. else:
  339. raise ArgumentIndexError(self, argindex)
  340. def _eval_rewrite_as_polynomial(self, n, a, x, **kwargs):
  341. from sympy.concrete.summations import Sum
  342. k = Dummy("k")
  343. kern = ((-1)**k * RisingFactorial(a, n - k) * (2*x)**(n - 2*k) /
  344. (factorial(k) * factorial(n - 2*k)))
  345. return Sum(kern, (k, 0, floor(n/2)))
  346. def _eval_conjugate(self):
  347. n, a, x = self.args
  348. return self.func(n, a.conjugate(), x.conjugate())
  349. #----------------------------------------------------------------------------
  350. # Chebyshev polynomials of first and second kind
  351. #
  352. class chebyshevt(OrthogonalPolynomial):
  353. r"""
  354. Chebyshev polynomial of the first kind, $T_n(x)$.
  355. Explanation
  356. ===========
  357. ``chebyshevt(n, x)`` gives the nth Chebyshev polynomial (of the first
  358. kind) in x, $T_n(x)$.
  359. The Chebyshev polynomials of the first kind are orthogonal on
  360. $[-1, 1]$ with respect to the weight $\frac{1}{\sqrt{1-x^2}}$.
  361. Examples
  362. ========
  363. >>> from sympy import chebyshevt, diff
  364. >>> from sympy.abc import n,x
  365. >>> chebyshevt(0, x)
  366. 1
  367. >>> chebyshevt(1, x)
  368. x
  369. >>> chebyshevt(2, x)
  370. 2*x**2 - 1
  371. >>> chebyshevt(n, x)
  372. chebyshevt(n, x)
  373. >>> chebyshevt(n, -x)
  374. (-1)**n*chebyshevt(n, x)
  375. >>> chebyshevt(-n, x)
  376. chebyshevt(n, x)
  377. >>> chebyshevt(n, 0)
  378. cos(pi*n/2)
  379. >>> chebyshevt(n, -1)
  380. (-1)**n
  381. >>> diff(chebyshevt(n, x), x)
  382. n*chebyshevu(n - 1, x)
  383. See Also
  384. ========
  385. jacobi, gegenbauer,
  386. chebyshevt_root, chebyshevu, chebyshevu_root,
  387. legendre, assoc_legendre,
  388. hermite,
  389. laguerre, assoc_laguerre,
  390. sympy.polys.orthopolys.jacobi_poly
  391. sympy.polys.orthopolys.gegenbauer_poly
  392. sympy.polys.orthopolys.chebyshevt_poly
  393. sympy.polys.orthopolys.chebyshevu_poly
  394. sympy.polys.orthopolys.hermite_poly
  395. sympy.polys.orthopolys.legendre_poly
  396. sympy.polys.orthopolys.laguerre_poly
  397. References
  398. ==========
  399. .. [1] https://en.wikipedia.org/wiki/Chebyshev_polynomial
  400. .. [2] http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html
  401. .. [3] http://mathworld.wolfram.com/ChebyshevPolynomialoftheSecondKind.html
  402. .. [4] http://functions.wolfram.com/Polynomials/ChebyshevT/
  403. .. [5] http://functions.wolfram.com/Polynomials/ChebyshevU/
  404. """
  405. _ortho_poly = staticmethod(chebyshevt_poly)
  406. @classmethod
  407. def eval(cls, n, x):
  408. if not n.is_Number:
  409. # Symbolic result T_n(x)
  410. # T_n(-x) ---> (-1)**n * T_n(x)
  411. if x.could_extract_minus_sign():
  412. return S.NegativeOne**n * chebyshevt(n, -x)
  413. # T_{-n}(x) ---> T_n(x)
  414. if n.could_extract_minus_sign():
  415. return chebyshevt(-n, x)
  416. # We can evaluate for some special values of x
  417. if x.is_zero:
  418. return cos(S.Half * S.Pi * n)
  419. if x == S.One:
  420. return S.One
  421. elif x is S.Infinity:
  422. return S.Infinity
  423. else:
  424. # n is a given fixed integer, evaluate into polynomial
  425. if n.is_negative:
  426. # T_{-n}(x) == T_n(x)
  427. return cls._eval_at_order(-n, x)
  428. else:
  429. return cls._eval_at_order(n, x)
  430. def fdiff(self, argindex=2):
  431. if argindex == 1:
  432. # Diff wrt n
  433. raise ArgumentIndexError(self, argindex)
  434. elif argindex == 2:
  435. # Diff wrt x
  436. n, x = self.args
  437. return n * chebyshevu(n - 1, x)
  438. else:
  439. raise ArgumentIndexError(self, argindex)
  440. def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
  441. from sympy.concrete.summations import Sum
  442. k = Dummy("k")
  443. kern = binomial(n, 2*k) * (x**2 - 1)**k * x**(n - 2*k)
  444. return Sum(kern, (k, 0, floor(n/2)))
  445. class chebyshevu(OrthogonalPolynomial):
  446. r"""
  447. Chebyshev polynomial of the second kind, $U_n(x)$.
  448. Explanation
  449. ===========
  450. ``chebyshevu(n, x)`` gives the nth Chebyshev polynomial of the second
  451. kind in x, $U_n(x)$.
  452. The Chebyshev polynomials of the second kind are orthogonal on
  453. $[-1, 1]$ with respect to the weight $\sqrt{1-x^2}$.
  454. Examples
  455. ========
  456. >>> from sympy import chebyshevu, diff
  457. >>> from sympy.abc import n,x
  458. >>> chebyshevu(0, x)
  459. 1
  460. >>> chebyshevu(1, x)
  461. 2*x
  462. >>> chebyshevu(2, x)
  463. 4*x**2 - 1
  464. >>> chebyshevu(n, x)
  465. chebyshevu(n, x)
  466. >>> chebyshevu(n, -x)
  467. (-1)**n*chebyshevu(n, x)
  468. >>> chebyshevu(-n, x)
  469. -chebyshevu(n - 2, x)
  470. >>> chebyshevu(n, 0)
  471. cos(pi*n/2)
  472. >>> chebyshevu(n, 1)
  473. n + 1
  474. >>> diff(chebyshevu(n, x), x)
  475. (-x*chebyshevu(n, x) + (n + 1)*chebyshevt(n + 1, x))/(x**2 - 1)
  476. See Also
  477. ========
  478. jacobi, gegenbauer,
  479. chebyshevt, chebyshevt_root, chebyshevu_root,
  480. legendre, assoc_legendre,
  481. hermite,
  482. laguerre, assoc_laguerre,
  483. sympy.polys.orthopolys.jacobi_poly
  484. sympy.polys.orthopolys.gegenbauer_poly
  485. sympy.polys.orthopolys.chebyshevt_poly
  486. sympy.polys.orthopolys.chebyshevu_poly
  487. sympy.polys.orthopolys.hermite_poly
  488. sympy.polys.orthopolys.legendre_poly
  489. sympy.polys.orthopolys.laguerre_poly
  490. References
  491. ==========
  492. .. [1] https://en.wikipedia.org/wiki/Chebyshev_polynomial
  493. .. [2] http://mathworld.wolfram.com/ChebyshevPolynomialoftheFirstKind.html
  494. .. [3] http://mathworld.wolfram.com/ChebyshevPolynomialoftheSecondKind.html
  495. .. [4] http://functions.wolfram.com/Polynomials/ChebyshevT/
  496. .. [5] http://functions.wolfram.com/Polynomials/ChebyshevU/
  497. """
  498. _ortho_poly = staticmethod(chebyshevu_poly)
  499. @classmethod
  500. def eval(cls, n, x):
  501. if not n.is_Number:
  502. # Symbolic result U_n(x)
  503. # U_n(-x) ---> (-1)**n * U_n(x)
  504. if x.could_extract_minus_sign():
  505. return S.NegativeOne**n * chebyshevu(n, -x)
  506. # U_{-n}(x) ---> -U_{n-2}(x)
  507. if n.could_extract_minus_sign():
  508. if n == S.NegativeOne:
  509. # n can not be -1 here
  510. return S.Zero
  511. elif not (-n - 2).could_extract_minus_sign():
  512. return -chebyshevu(-n - 2, x)
  513. # We can evaluate for some special values of x
  514. if x.is_zero:
  515. return cos(S.Half * S.Pi * n)
  516. if x == S.One:
  517. return S.One + n
  518. elif x is S.Infinity:
  519. return S.Infinity
  520. else:
  521. # n is a given fixed integer, evaluate into polynomial
  522. if n.is_negative:
  523. # U_{-n}(x) ---> -U_{n-2}(x)
  524. if n == S.NegativeOne:
  525. return S.Zero
  526. else:
  527. return -cls._eval_at_order(-n - 2, x)
  528. else:
  529. return cls._eval_at_order(n, x)
  530. def fdiff(self, argindex=2):
  531. if argindex == 1:
  532. # Diff wrt n
  533. raise ArgumentIndexError(self, argindex)
  534. elif argindex == 2:
  535. # Diff wrt x
  536. n, x = self.args
  537. return ((n + 1) * chebyshevt(n + 1, x) - x * chebyshevu(n, x)) / (x**2 - 1)
  538. else:
  539. raise ArgumentIndexError(self, argindex)
  540. def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
  541. from sympy.concrete.summations import Sum
  542. k = Dummy("k")
  543. kern = S.NegativeOne**k * factorial(
  544. n - k) * (2*x)**(n - 2*k) / (factorial(k) * factorial(n - 2*k))
  545. return Sum(kern, (k, 0, floor(n/2)))
  546. class chebyshevt_root(Function):
  547. r"""
  548. ``chebyshev_root(n, k)`` returns the kth root (indexed from zero) of
  549. the nth Chebyshev polynomial of the first kind; that is, if
  550. 0 <= k < n, ``chebyshevt(n, chebyshevt_root(n, k)) == 0``.
  551. Examples
  552. ========
  553. >>> from sympy import chebyshevt, chebyshevt_root
  554. >>> chebyshevt_root(3, 2)
  555. -sqrt(3)/2
  556. >>> chebyshevt(3, chebyshevt_root(3, 2))
  557. 0
  558. See Also
  559. ========
  560. jacobi, gegenbauer,
  561. chebyshevt, chebyshevu, chebyshevu_root,
  562. legendre, assoc_legendre,
  563. hermite,
  564. laguerre, assoc_laguerre,
  565. sympy.polys.orthopolys.jacobi_poly
  566. sympy.polys.orthopolys.gegenbauer_poly
  567. sympy.polys.orthopolys.chebyshevt_poly
  568. sympy.polys.orthopolys.chebyshevu_poly
  569. sympy.polys.orthopolys.hermite_poly
  570. sympy.polys.orthopolys.legendre_poly
  571. sympy.polys.orthopolys.laguerre_poly
  572. """
  573. @classmethod
  574. def eval(cls, n, k):
  575. if not ((0 <= k) and (k < n)):
  576. raise ValueError("must have 0 <= k < n, "
  577. "got k = %s and n = %s" % (k, n))
  578. return cos(S.Pi*(2*k + 1)/(2*n))
  579. class chebyshevu_root(Function):
  580. r"""
  581. ``chebyshevu_root(n, k)`` returns the kth root (indexed from zero) of the
  582. nth Chebyshev polynomial of the second kind; that is, if 0 <= k < n,
  583. ``chebyshevu(n, chebyshevu_root(n, k)) == 0``.
  584. Examples
  585. ========
  586. >>> from sympy import chebyshevu, chebyshevu_root
  587. >>> chebyshevu_root(3, 2)
  588. -sqrt(2)/2
  589. >>> chebyshevu(3, chebyshevu_root(3, 2))
  590. 0
  591. See Also
  592. ========
  593. chebyshevt, chebyshevt_root, chebyshevu,
  594. legendre, assoc_legendre,
  595. hermite,
  596. laguerre, assoc_laguerre,
  597. sympy.polys.orthopolys.jacobi_poly
  598. sympy.polys.orthopolys.gegenbauer_poly
  599. sympy.polys.orthopolys.chebyshevt_poly
  600. sympy.polys.orthopolys.chebyshevu_poly
  601. sympy.polys.orthopolys.hermite_poly
  602. sympy.polys.orthopolys.legendre_poly
  603. sympy.polys.orthopolys.laguerre_poly
  604. """
  605. @classmethod
  606. def eval(cls, n, k):
  607. if not ((0 <= k) and (k < n)):
  608. raise ValueError("must have 0 <= k < n, "
  609. "got k = %s and n = %s" % (k, n))
  610. return cos(S.Pi*(k + 1)/(n + 1))
  611. #----------------------------------------------------------------------------
  612. # Legendre polynomials and Associated Legendre polynomials
  613. #
  614. class legendre(OrthogonalPolynomial):
  615. r"""
  616. ``legendre(n, x)`` gives the nth Legendre polynomial of x, $P_n(x)$
  617. Explanation
  618. ===========
  619. The Legendre polynomials are orthogonal on [-1, 1] with respect to
  620. the constant weight 1. They satisfy $P_n(1) = 1$ for all n; further,
  621. $P_n$ is odd for odd n and even for even n.
  622. Examples
  623. ========
  624. >>> from sympy import legendre, diff
  625. >>> from sympy.abc import x, n
  626. >>> legendre(0, x)
  627. 1
  628. >>> legendre(1, x)
  629. x
  630. >>> legendre(2, x)
  631. 3*x**2/2 - 1/2
  632. >>> legendre(n, x)
  633. legendre(n, x)
  634. >>> diff(legendre(n,x), x)
  635. n*(x*legendre(n, x) - legendre(n - 1, x))/(x**2 - 1)
  636. See Also
  637. ========
  638. jacobi, gegenbauer,
  639. chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
  640. assoc_legendre,
  641. hermite,
  642. laguerre, assoc_laguerre,
  643. sympy.polys.orthopolys.jacobi_poly
  644. sympy.polys.orthopolys.gegenbauer_poly
  645. sympy.polys.orthopolys.chebyshevt_poly
  646. sympy.polys.orthopolys.chebyshevu_poly
  647. sympy.polys.orthopolys.hermite_poly
  648. sympy.polys.orthopolys.legendre_poly
  649. sympy.polys.orthopolys.laguerre_poly
  650. References
  651. ==========
  652. .. [1] https://en.wikipedia.org/wiki/Legendre_polynomial
  653. .. [2] http://mathworld.wolfram.com/LegendrePolynomial.html
  654. .. [3] http://functions.wolfram.com/Polynomials/LegendreP/
  655. .. [4] http://functions.wolfram.com/Polynomials/LegendreP2/
  656. """
  657. _ortho_poly = staticmethod(legendre_poly)
  658. @classmethod
  659. def eval(cls, n, x):
  660. if not n.is_Number:
  661. # Symbolic result L_n(x)
  662. # L_n(-x) ---> (-1)**n * L_n(x)
  663. if x.could_extract_minus_sign():
  664. return S.NegativeOne**n * legendre(n, -x)
  665. # L_{-n}(x) ---> L_{n-1}(x)
  666. if n.could_extract_minus_sign() and not(-n - 1).could_extract_minus_sign():
  667. return legendre(-n - S.One, x)
  668. # We can evaluate for some special values of x
  669. if x.is_zero:
  670. return sqrt(S.Pi)/(gamma(S.Half - n/2)*gamma(S.One + n/2))
  671. elif x == S.One:
  672. return S.One
  673. elif x is S.Infinity:
  674. return S.Infinity
  675. else:
  676. # n is a given fixed integer, evaluate into polynomial;
  677. # L_{-n}(x) ---> L_{n-1}(x)
  678. if n.is_negative:
  679. n = -n - S.One
  680. return cls._eval_at_order(n, x)
  681. def fdiff(self, argindex=2):
  682. if argindex == 1:
  683. # Diff wrt n
  684. raise ArgumentIndexError(self, argindex)
  685. elif argindex == 2:
  686. # Diff wrt x
  687. # Find better formula, this is unsuitable for x = +/-1
  688. # http://www.autodiff.org/ad16/Oral/Buecker_Legendre.pdf says
  689. # at x = 1:
  690. # n*(n + 1)/2 , m = 0
  691. # oo , m = 1
  692. # -(n-1)*n*(n+1)*(n+2)/4 , m = 2
  693. # 0 , m = 3, 4, ..., n
  694. #
  695. # at x = -1
  696. # (-1)**(n+1)*n*(n + 1)/2 , m = 0
  697. # (-1)**n*oo , m = 1
  698. # (-1)**n*(n-1)*n*(n+1)*(n+2)/4 , m = 2
  699. # 0 , m = 3, 4, ..., n
  700. n, x = self.args
  701. return n/(x**2 - 1)*(x*legendre(n, x) - legendre(n - 1, x))
  702. else:
  703. raise ArgumentIndexError(self, argindex)
  704. def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
  705. from sympy.concrete.summations import Sum
  706. k = Dummy("k")
  707. kern = S.NegativeOne**k*binomial(n, k)**2*((1 + x)/2)**(n - k)*((1 - x)/2)**k
  708. return Sum(kern, (k, 0, n))
  709. class assoc_legendre(Function):
  710. r"""
  711. ``assoc_legendre(n, m, x)`` gives $P_n^m(x)$, where n and m are
  712. the degree and order or an expression which is related to the nth
  713. order Legendre polynomial, $P_n(x)$ in the following manner:
  714. .. math::
  715. P_n^m(x) = (-1)^m (1 - x^2)^{\frac{m}{2}}
  716. \frac{\mathrm{d}^m P_n(x)}{\mathrm{d} x^m}
  717. Explanation
  718. ===========
  719. Associated Legendre polynomials are orthogonal on [-1, 1] with:
  720. - weight = 1 for the same m, and different n.
  721. - weight = 1/(1-x**2) for the same n, and different m.
  722. Examples
  723. ========
  724. >>> from sympy import assoc_legendre
  725. >>> from sympy.abc import x, m, n
  726. >>> assoc_legendre(0,0, x)
  727. 1
  728. >>> assoc_legendre(1,0, x)
  729. x
  730. >>> assoc_legendre(1,1, x)
  731. -sqrt(1 - x**2)
  732. >>> assoc_legendre(n,m,x)
  733. assoc_legendre(n, m, x)
  734. See Also
  735. ========
  736. jacobi, gegenbauer,
  737. chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
  738. legendre,
  739. hermite,
  740. laguerre, assoc_laguerre,
  741. sympy.polys.orthopolys.jacobi_poly
  742. sympy.polys.orthopolys.gegenbauer_poly
  743. sympy.polys.orthopolys.chebyshevt_poly
  744. sympy.polys.orthopolys.chebyshevu_poly
  745. sympy.polys.orthopolys.hermite_poly
  746. sympy.polys.orthopolys.legendre_poly
  747. sympy.polys.orthopolys.laguerre_poly
  748. References
  749. ==========
  750. .. [1] https://en.wikipedia.org/wiki/Associated_Legendre_polynomials
  751. .. [2] http://mathworld.wolfram.com/LegendrePolynomial.html
  752. .. [3] http://functions.wolfram.com/Polynomials/LegendreP/
  753. .. [4] http://functions.wolfram.com/Polynomials/LegendreP2/
  754. """
  755. @classmethod
  756. def _eval_at_order(cls, n, m):
  757. P = legendre_poly(n, _x, polys=True).diff((_x, m))
  758. return S.NegativeOne**m * (1 - _x**2)**Rational(m, 2) * P.as_expr()
  759. @classmethod
  760. def eval(cls, n, m, x):
  761. if m.could_extract_minus_sign():
  762. # P^{-m}_n ---> F * P^m_n
  763. return S.NegativeOne**(-m) * (factorial(m + n)/factorial(n - m)) * assoc_legendre(n, -m, x)
  764. if m == 0:
  765. # P^0_n ---> L_n
  766. return legendre(n, x)
  767. if x == 0:
  768. return 2**m*sqrt(S.Pi) / (gamma((1 - m - n)/2)*gamma(1 - (m - n)/2))
  769. if n.is_Number and m.is_Number and n.is_integer and m.is_integer:
  770. if n.is_negative:
  771. raise ValueError("%s : 1st index must be nonnegative integer (got %r)" % (cls, n))
  772. if abs(m) > n:
  773. raise ValueError("%s : abs('2nd index') must be <= '1st index' (got %r, %r)" % (cls, n, m))
  774. return cls._eval_at_order(int(n), abs(int(m))).subs(_x, x)
  775. def fdiff(self, argindex=3):
  776. if argindex == 1:
  777. # Diff wrt n
  778. raise ArgumentIndexError(self, argindex)
  779. elif argindex == 2:
  780. # Diff wrt m
  781. raise ArgumentIndexError(self, argindex)
  782. elif argindex == 3:
  783. # Diff wrt x
  784. # Find better formula, this is unsuitable for x = 1
  785. n, m, x = self.args
  786. return 1/(x**2 - 1)*(x*n*assoc_legendre(n, m, x) - (m + n)*assoc_legendre(n - 1, m, x))
  787. else:
  788. raise ArgumentIndexError(self, argindex)
  789. def _eval_rewrite_as_polynomial(self, n, m, x, **kwargs):
  790. from sympy.concrete.summations import Sum
  791. k = Dummy("k")
  792. kern = factorial(2*n - 2*k)/(2**n*factorial(n - k)*factorial(
  793. k)*factorial(n - 2*k - m))*S.NegativeOne**k*x**(n - m - 2*k)
  794. return (1 - x**2)**(m/2) * Sum(kern, (k, 0, floor((n - m)*S.Half)))
  795. def _eval_conjugate(self):
  796. n, m, x = self.args
  797. return self.func(n, m.conjugate(), x.conjugate())
  798. #----------------------------------------------------------------------------
  799. # Hermite polynomials
  800. #
  801. class hermite(OrthogonalPolynomial):
  802. r"""
  803. ``hermite(n, x)`` gives the nth Hermite polynomial in x, $H_n(x)$
  804. Explanation
  805. ===========
  806. The Hermite polynomials are orthogonal on $(-\infty, \infty)$
  807. with respect to the weight $\exp\left(-x^2\right)$.
  808. Examples
  809. ========
  810. >>> from sympy import hermite, diff
  811. >>> from sympy.abc import x, n
  812. >>> hermite(0, x)
  813. 1
  814. >>> hermite(1, x)
  815. 2*x
  816. >>> hermite(2, x)
  817. 4*x**2 - 2
  818. >>> hermite(n, x)
  819. hermite(n, x)
  820. >>> diff(hermite(n,x), x)
  821. 2*n*hermite(n - 1, x)
  822. >>> hermite(n, -x)
  823. (-1)**n*hermite(n, x)
  824. See Also
  825. ========
  826. jacobi, gegenbauer,
  827. chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
  828. legendre, assoc_legendre,
  829. laguerre, assoc_laguerre,
  830. sympy.polys.orthopolys.jacobi_poly
  831. sympy.polys.orthopolys.gegenbauer_poly
  832. sympy.polys.orthopolys.chebyshevt_poly
  833. sympy.polys.orthopolys.chebyshevu_poly
  834. sympy.polys.orthopolys.hermite_poly
  835. sympy.polys.orthopolys.legendre_poly
  836. sympy.polys.orthopolys.laguerre_poly
  837. References
  838. ==========
  839. .. [1] https://en.wikipedia.org/wiki/Hermite_polynomial
  840. .. [2] http://mathworld.wolfram.com/HermitePolynomial.html
  841. .. [3] http://functions.wolfram.com/Polynomials/HermiteH/
  842. """
  843. _ortho_poly = staticmethod(hermite_poly)
  844. @classmethod
  845. def eval(cls, n, x):
  846. if not n.is_Number:
  847. # Symbolic result H_n(x)
  848. # H_n(-x) ---> (-1)**n * H_n(x)
  849. if x.could_extract_minus_sign():
  850. return S.NegativeOne**n * hermite(n, -x)
  851. # We can evaluate for some special values of x
  852. if x.is_zero:
  853. return 2**n * sqrt(S.Pi) / gamma((S.One - n)/2)
  854. elif x is S.Infinity:
  855. return S.Infinity
  856. else:
  857. # n is a given fixed integer, evaluate into polynomial
  858. if n.is_negative:
  859. raise ValueError(
  860. "The index n must be nonnegative integer (got %r)" % n)
  861. else:
  862. return cls._eval_at_order(n, x)
  863. def fdiff(self, argindex=2):
  864. if argindex == 1:
  865. # Diff wrt n
  866. raise ArgumentIndexError(self, argindex)
  867. elif argindex == 2:
  868. # Diff wrt x
  869. n, x = self.args
  870. return 2*n*hermite(n - 1, x)
  871. else:
  872. raise ArgumentIndexError(self, argindex)
  873. def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
  874. from sympy.concrete.summations import Sum
  875. k = Dummy("k")
  876. kern = S.NegativeOne**k / (factorial(k)*factorial(n - 2*k)) * (2*x)**(n - 2*k)
  877. return factorial(n)*Sum(kern, (k, 0, floor(n/2)))
  878. #----------------------------------------------------------------------------
  879. # Laguerre polynomials
  880. #
  881. class laguerre(OrthogonalPolynomial):
  882. r"""
  883. Returns the nth Laguerre polynomial in x, $L_n(x)$.
  884. Examples
  885. ========
  886. >>> from sympy import laguerre, diff
  887. >>> from sympy.abc import x, n
  888. >>> laguerre(0, x)
  889. 1
  890. >>> laguerre(1, x)
  891. 1 - x
  892. >>> laguerre(2, x)
  893. x**2/2 - 2*x + 1
  894. >>> laguerre(3, x)
  895. -x**3/6 + 3*x**2/2 - 3*x + 1
  896. >>> laguerre(n, x)
  897. laguerre(n, x)
  898. >>> diff(laguerre(n, x), x)
  899. -assoc_laguerre(n - 1, 1, x)
  900. Parameters
  901. ==========
  902. n : int
  903. Degree of Laguerre polynomial. Must be ``n >= 0``.
  904. See Also
  905. ========
  906. jacobi, gegenbauer,
  907. chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
  908. legendre, assoc_legendre,
  909. hermite,
  910. assoc_laguerre,
  911. sympy.polys.orthopolys.jacobi_poly
  912. sympy.polys.orthopolys.gegenbauer_poly
  913. sympy.polys.orthopolys.chebyshevt_poly
  914. sympy.polys.orthopolys.chebyshevu_poly
  915. sympy.polys.orthopolys.hermite_poly
  916. sympy.polys.orthopolys.legendre_poly
  917. sympy.polys.orthopolys.laguerre_poly
  918. References
  919. ==========
  920. .. [1] https://en.wikipedia.org/wiki/Laguerre_polynomial
  921. .. [2] http://mathworld.wolfram.com/LaguerrePolynomial.html
  922. .. [3] http://functions.wolfram.com/Polynomials/LaguerreL/
  923. .. [4] http://functions.wolfram.com/Polynomials/LaguerreL3/
  924. """
  925. _ortho_poly = staticmethod(laguerre_poly)
  926. @classmethod
  927. def eval(cls, n, x):
  928. if n.is_integer is False:
  929. raise ValueError("Error: n should be an integer.")
  930. if not n.is_Number:
  931. # Symbolic result L_n(x)
  932. # L_{n}(-x) ---> exp(-x) * L_{-n-1}(x)
  933. # L_{-n}(x) ---> exp(x) * L_{n-1}(-x)
  934. if n.could_extract_minus_sign() and not(-n - 1).could_extract_minus_sign():
  935. return exp(x)*laguerre(-n - 1, -x)
  936. # We can evaluate for some special values of x
  937. if x.is_zero:
  938. return S.One
  939. elif x is S.NegativeInfinity:
  940. return S.Infinity
  941. elif x is S.Infinity:
  942. return S.NegativeOne**n * S.Infinity
  943. else:
  944. if n.is_negative:
  945. return exp(x)*laguerre(-n - 1, -x)
  946. else:
  947. return cls._eval_at_order(n, x)
  948. def fdiff(self, argindex=2):
  949. if argindex == 1:
  950. # Diff wrt n
  951. raise ArgumentIndexError(self, argindex)
  952. elif argindex == 2:
  953. # Diff wrt x
  954. n, x = self.args
  955. return -assoc_laguerre(n - 1, 1, x)
  956. else:
  957. raise ArgumentIndexError(self, argindex)
  958. def _eval_rewrite_as_polynomial(self, n, x, **kwargs):
  959. from sympy.concrete.summations import Sum
  960. # Make sure n \in N_0
  961. if n.is_negative:
  962. return exp(x) * self._eval_rewrite_as_polynomial(-n - 1, -x, **kwargs)
  963. if n.is_integer is False:
  964. raise ValueError("Error: n should be an integer.")
  965. k = Dummy("k")
  966. kern = RisingFactorial(-n, k) / factorial(k)**2 * x**k
  967. return Sum(kern, (k, 0, n))
  968. class assoc_laguerre(OrthogonalPolynomial):
  969. r"""
  970. Returns the nth generalized Laguerre polynomial in x, $L_n(x)$.
  971. Examples
  972. ========
  973. >>> from sympy import assoc_laguerre, diff
  974. >>> from sympy.abc import x, n, a
  975. >>> assoc_laguerre(0, a, x)
  976. 1
  977. >>> assoc_laguerre(1, a, x)
  978. a - x + 1
  979. >>> assoc_laguerre(2, a, x)
  980. a**2/2 + 3*a/2 + x**2/2 + x*(-a - 2) + 1
  981. >>> assoc_laguerre(3, a, x)
  982. a**3/6 + a**2 + 11*a/6 - x**3/6 + x**2*(a/2 + 3/2) +
  983. x*(-a**2/2 - 5*a/2 - 3) + 1
  984. >>> assoc_laguerre(n, a, 0)
  985. binomial(a + n, a)
  986. >>> assoc_laguerre(n, a, x)
  987. assoc_laguerre(n, a, x)
  988. >>> assoc_laguerre(n, 0, x)
  989. laguerre(n, x)
  990. >>> diff(assoc_laguerre(n, a, x), x)
  991. -assoc_laguerre(n - 1, a + 1, x)
  992. >>> diff(assoc_laguerre(n, a, x), a)
  993. Sum(assoc_laguerre(_k, a, x)/(-a + n), (_k, 0, n - 1))
  994. Parameters
  995. ==========
  996. n : int
  997. Degree of Laguerre polynomial. Must be ``n >= 0``.
  998. alpha : Expr
  999. Arbitrary expression. For ``alpha=0`` regular Laguerre
  1000. polynomials will be generated.
  1001. See Also
  1002. ========
  1003. jacobi, gegenbauer,
  1004. chebyshevt, chebyshevt_root, chebyshevu, chebyshevu_root,
  1005. legendre, assoc_legendre,
  1006. hermite,
  1007. laguerre,
  1008. sympy.polys.orthopolys.jacobi_poly
  1009. sympy.polys.orthopolys.gegenbauer_poly
  1010. sympy.polys.orthopolys.chebyshevt_poly
  1011. sympy.polys.orthopolys.chebyshevu_poly
  1012. sympy.polys.orthopolys.hermite_poly
  1013. sympy.polys.orthopolys.legendre_poly
  1014. sympy.polys.orthopolys.laguerre_poly
  1015. References
  1016. ==========
  1017. .. [1] https://en.wikipedia.org/wiki/Laguerre_polynomial#Generalized_Laguerre_polynomials
  1018. .. [2] http://mathworld.wolfram.com/AssociatedLaguerrePolynomial.html
  1019. .. [3] http://functions.wolfram.com/Polynomials/LaguerreL/
  1020. .. [4] http://functions.wolfram.com/Polynomials/LaguerreL3/
  1021. """
  1022. @classmethod
  1023. def eval(cls, n, alpha, x):
  1024. # L_{n}^{0}(x) ---> L_{n}(x)
  1025. if alpha.is_zero:
  1026. return laguerre(n, x)
  1027. if not n.is_Number:
  1028. # We can evaluate for some special values of x
  1029. if x.is_zero:
  1030. return binomial(n + alpha, alpha)
  1031. elif x is S.Infinity and n > 0:
  1032. return S.NegativeOne**n * S.Infinity
  1033. elif x is S.NegativeInfinity and n > 0:
  1034. return S.Infinity
  1035. else:
  1036. # n is a given fixed integer, evaluate into polynomial
  1037. if n.is_negative:
  1038. raise ValueError(
  1039. "The index n must be nonnegative integer (got %r)" % n)
  1040. else:
  1041. return laguerre_poly(n, x, alpha)
  1042. def fdiff(self, argindex=3):
  1043. from sympy.concrete.summations import Sum
  1044. if argindex == 1:
  1045. # Diff wrt n
  1046. raise ArgumentIndexError(self, argindex)
  1047. elif argindex == 2:
  1048. # Diff wrt alpha
  1049. n, alpha, x = self.args
  1050. k = Dummy("k")
  1051. return Sum(assoc_laguerre(k, alpha, x) / (n - alpha), (k, 0, n - 1))
  1052. elif argindex == 3:
  1053. # Diff wrt x
  1054. n, alpha, x = self.args
  1055. return -assoc_laguerre(n - 1, alpha + 1, x)
  1056. else:
  1057. raise ArgumentIndexError(self, argindex)
  1058. def _eval_rewrite_as_polynomial(self, n, alpha, x, **kwargs):
  1059. from sympy.concrete.summations import Sum
  1060. # Make sure n \in N_0
  1061. if n.is_negative or n.is_integer is False:
  1062. raise ValueError("Error: n should be a non-negative integer.")
  1063. k = Dummy("k")
  1064. kern = RisingFactorial(
  1065. -n, k) / (gamma(k + alpha + 1) * factorial(k)) * x**k
  1066. return gamma(n + alpha + 1) / factorial(n) * Sum(kern, (k, 0, n))
  1067. def _eval_conjugate(self):
  1068. n, alpha, x = self.args
  1069. return self.func(n, alpha.conjugate(), x.conjugate())