orthopolys.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. """Efficient functions for generating orthogonal polynomials."""
  2. from sympy.core.symbol import Dummy
  3. from sympy.polys.densearith import (dup_mul, dup_mul_ground,
  4. dup_lshift, dup_sub, dup_add)
  5. from sympy.polys.domains import ZZ, QQ
  6. from sympy.polys.polytools import named_poly
  7. from sympy.utilities import public
  8. def dup_jacobi(n, a, b, K):
  9. """Low-level implementation of Jacobi polynomials."""
  10. if n < 1:
  11. return [K.one]
  12. m2, m1 = [K.one], [(a+b)/K(2) + K.one, (a-b)/K(2)]
  13. for i in range(2, n+1):
  14. den = K(i)*(a + b + i)*(a + b + K(2)*i - K(2))
  15. f0 = (a + b + K(2)*i - K.one) * (a*a - b*b) / (K(2)*den)
  16. f1 = (a + b + K(2)*i - K.one) * (a + b + K(2)*i - K(2)) * (a + b + K(2)*i) / (K(2)*den)
  17. f2 = (a + i - K.one)*(b + i - K.one)*(a + b + K(2)*i) / den
  18. p0 = dup_mul_ground(m1, f0, K)
  19. p1 = dup_mul_ground(dup_lshift(m1, 1, K), f1, K)
  20. p2 = dup_mul_ground(m2, f2, K)
  21. m2, m1 = m1, dup_sub(dup_add(p0, p1, K), p2, K)
  22. return m1
  23. @public
  24. def jacobi_poly(n, a, b, x=None, polys=False):
  25. r"""Generates the Jacobi polynomial `P_n^{(a,b)}(x)`.
  26. Parameters
  27. ==========
  28. n : int
  29. Degree of the polynomial.
  30. a
  31. Lower limit of minimal domain for the list of coefficients.
  32. b
  33. Upper limit of minimal domain for the list of coefficients.
  34. x : optional
  35. polys : bool, optional
  36. If True, return a Poly, otherwise (default) return an expression.
  37. """
  38. return named_poly(n, dup_jacobi, None, "Jacobi polynomial", (x, a, b), polys)
  39. def dup_gegenbauer(n, a, K):
  40. """Low-level implementation of Gegenbauer polynomials."""
  41. if n < 1:
  42. return [K.one]
  43. m2, m1 = [K.one], [K(2)*a, K.zero]
  44. for i in range(2, n+1):
  45. p1 = dup_mul_ground(dup_lshift(m1, 1, K), K(2)*(a-K.one)/K(i) + K(2), K)
  46. p2 = dup_mul_ground(m2, K(2)*(a-K.one)/K(i) + K.one, K)
  47. m2, m1 = m1, dup_sub(p1, p2, K)
  48. return m1
  49. def gegenbauer_poly(n, a, x=None, polys=False):
  50. r"""Generates the Gegenbauer polynomial `C_n^{(a)}(x)`.
  51. Parameters
  52. ==========
  53. n : int
  54. Degree of the polynomial.
  55. x : optional
  56. a
  57. Decides minimal domain for the list of coefficients.
  58. polys : bool, optional
  59. If True, return a Poly, otherwise (default) return an expression.
  60. """
  61. return named_poly(n, dup_gegenbauer, None, "Gegenbauer polynomial", (x, a), polys)
  62. def dup_chebyshevt(n, K):
  63. """Low-level implementation of Chebyshev polynomials of the first kind."""
  64. if n < 1:
  65. return [K.one]
  66. m2, m1 = [K.one], [K.one, K.zero]
  67. for i in range(2, n+1):
  68. m2, m1 = m1, dup_sub(dup_mul_ground(dup_lshift(m1, 1, K), K(2), K), m2, K)
  69. return m1
  70. def dup_chebyshevu(n, K):
  71. """Low-level implementation of Chebyshev polynomials of the second kind."""
  72. if n < 1:
  73. return [K.one]
  74. m2, m1 = [K.one], [K(2), K.zero]
  75. for i in range(2, n+1):
  76. m2, m1 = m1, dup_sub(dup_mul_ground(dup_lshift(m1, 1, K), K(2), K), m2, K)
  77. return m1
  78. @public
  79. def chebyshevt_poly(n, x=None, polys=False):
  80. r"""Generates the Chebyshev polynomial of the first kind `T_n(x)`.
  81. Parameters
  82. ==========
  83. n : int
  84. Degree of the polynomial.
  85. x : optional
  86. polys : bool, optional
  87. If True, return a Poly, otherwise (default) return an expression.
  88. """
  89. return named_poly(n, dup_chebyshevt, ZZ,
  90. "Chebyshev polynomial of the first kind", (x,), polys)
  91. @public
  92. def chebyshevu_poly(n, x=None, polys=False):
  93. r"""Generates the Chebyshev polynomial of the second kind `U_n(x)`.
  94. Parameters
  95. ==========
  96. n : int
  97. Degree of the polynomial.
  98. x : optional
  99. polys : bool, optional
  100. If True, return a Poly, otherwise (default) return an expression.
  101. """
  102. return named_poly(n, dup_chebyshevu, ZZ,
  103. "Chebyshev polynomial of the second kind", (x,), polys)
  104. def dup_hermite(n, K):
  105. """Low-level implementation of Hermite polynomials."""
  106. if n < 1:
  107. return [K.one]
  108. m2, m1 = [K.one], [K(2), K.zero]
  109. for i in range(2, n+1):
  110. a = dup_lshift(m1, 1, K)
  111. b = dup_mul_ground(m2, K(i-1), K)
  112. m2, m1 = m1, dup_mul_ground(dup_sub(a, b, K), K(2), K)
  113. return m1
  114. def dup_hermite_prob(n, K):
  115. """Low-level implementation of probabilist's Hermite polynomials."""
  116. if n < 1:
  117. return [K.one]
  118. m2, m1 = [K.one], [K.one, K.zero]
  119. for i in range(2, n+1):
  120. a = dup_lshift(m1, 1, K)
  121. b = dup_mul_ground(m2, K(i-1), K)
  122. m2, m1 = m1, dup_sub(a, b, K)
  123. return m1
  124. @public
  125. def hermite_poly(n, x=None, polys=False):
  126. r"""Generates the Hermite polynomial `H_n(x)`.
  127. Parameters
  128. ==========
  129. n : int
  130. Degree of the polynomial.
  131. x : optional
  132. polys : bool, optional
  133. If True, return a Poly, otherwise (default) return an expression.
  134. """
  135. return named_poly(n, dup_hermite, ZZ, "Hermite polynomial", (x,), polys)
  136. @public
  137. def hermite_prob_poly(n, x=None, polys=False):
  138. r"""Generates the probabilist's Hermite polynomial `He_n(x)`.
  139. Parameters
  140. ==========
  141. n : int
  142. Degree of the polynomial.
  143. x : optional
  144. polys : bool, optional
  145. If True, return a Poly, otherwise (default) return an expression.
  146. """
  147. return named_poly(n, dup_hermite_prob, ZZ,
  148. "probabilist's Hermite polynomial", (x,), polys)
  149. def dup_legendre(n, K):
  150. """Low-level implementation of Legendre polynomials."""
  151. if n < 1:
  152. return [K.one]
  153. m2, m1 = [K.one], [K.one, K.zero]
  154. for i in range(2, n+1):
  155. a = dup_mul_ground(dup_lshift(m1, 1, K), K(2*i-1, i), K)
  156. b = dup_mul_ground(m2, K(i-1, i), K)
  157. m2, m1 = m1, dup_sub(a, b, K)
  158. return m1
  159. @public
  160. def legendre_poly(n, x=None, polys=False):
  161. r"""Generates the Legendre polynomial `P_n(x)`.
  162. Parameters
  163. ==========
  164. n : int
  165. Degree of the polynomial.
  166. x : optional
  167. polys : bool, optional
  168. If True, return a Poly, otherwise (default) return an expression.
  169. """
  170. return named_poly(n, dup_legendre, QQ, "Legendre polynomial", (x,), polys)
  171. def dup_laguerre(n, alpha, K):
  172. """Low-level implementation of Laguerre polynomials."""
  173. m2, m1 = [K.zero], [K.one]
  174. for i in range(1, n+1):
  175. a = dup_mul(m1, [-K.one/K(i), (alpha-K.one)/K(i) + K(2)], K)
  176. b = dup_mul_ground(m2, (alpha-K.one)/K(i) + K.one, K)
  177. m2, m1 = m1, dup_sub(a, b, K)
  178. return m1
  179. @public
  180. def laguerre_poly(n, x=None, alpha=0, polys=False):
  181. r"""Generates the Laguerre polynomial `L_n^{(\alpha)}(x)`.
  182. Parameters
  183. ==========
  184. n : int
  185. Degree of the polynomial.
  186. x : optional
  187. alpha : optional
  188. Decides minimal domain for the list of coefficients.
  189. polys : bool, optional
  190. If True, return a Poly, otherwise (default) return an expression.
  191. """
  192. return named_poly(n, dup_laguerre, None, "Laguerre polynomial", (x, alpha), polys)
  193. def dup_spherical_bessel_fn(n, K):
  194. """Low-level implementation of fn(n, x)."""
  195. if n < 1:
  196. return [K.one, K.zero]
  197. m2, m1 = [K.one], [K.one, K.zero]
  198. for i in range(2, n+1):
  199. m2, m1 = m1, dup_sub(dup_mul_ground(dup_lshift(m1, 1, K), K(2*i-1), K), m2, K)
  200. return dup_lshift(m1, 1, K)
  201. def dup_spherical_bessel_fn_minus(n, K):
  202. """Low-level implementation of fn(-n, x)."""
  203. m2, m1 = [K.one, K.zero], [K.zero]
  204. for i in range(2, n+1):
  205. m2, m1 = m1, dup_sub(dup_mul_ground(dup_lshift(m1, 1, K), K(3-2*i), K), m2, K)
  206. return m1
  207. def spherical_bessel_fn(n, x=None, polys=False):
  208. """
  209. Coefficients for the spherical Bessel functions.
  210. These are only needed in the jn() function.
  211. The coefficients are calculated from:
  212. fn(0, z) = 1/z
  213. fn(1, z) = 1/z**2
  214. fn(n-1, z) + fn(n+1, z) == (2*n+1)/z * fn(n, z)
  215. Parameters
  216. ==========
  217. n : int
  218. Degree of the polynomial.
  219. x : optional
  220. polys : bool, optional
  221. If True, return a Poly, otherwise (default) return an expression.
  222. Examples
  223. ========
  224. >>> from sympy.polys.orthopolys import spherical_bessel_fn as fn
  225. >>> from sympy import Symbol
  226. >>> z = Symbol("z")
  227. >>> fn(1, z)
  228. z**(-2)
  229. >>> fn(2, z)
  230. -1/z + 3/z**3
  231. >>> fn(3, z)
  232. -6/z**2 + 15/z**4
  233. >>> fn(4, z)
  234. 1/z - 45/z**3 + 105/z**5
  235. """
  236. if x is None:
  237. x = Dummy("x")
  238. f = dup_spherical_bessel_fn_minus if n < 0 else dup_spherical_bessel_fn
  239. return named_poly(abs(n), f, ZZ, "", (QQ(1)/x,), polys)