test_orthopolys.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """Tests for efficient functions for generating orthogonal polynomials. """
  2. from sympy.core.numbers import Rational as Q
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import symbols
  5. from sympy.polys.polytools import Poly
  6. from sympy.testing.pytest import raises
  7. from sympy.polys.orthopolys import (
  8. jacobi_poly,
  9. gegenbauer_poly,
  10. chebyshevt_poly,
  11. chebyshevu_poly,
  12. hermite_poly,
  13. hermite_prob_poly,
  14. legendre_poly,
  15. laguerre_poly,
  16. spherical_bessel_fn,
  17. )
  18. from sympy.abc import x, a, b
  19. def test_jacobi_poly():
  20. raises(ValueError, lambda: jacobi_poly(-1, a, b, x))
  21. assert jacobi_poly(1, a, b, x, polys=True) == Poly(
  22. (a/2 + b/2 + 1)*x + a/2 - b/2, x, domain='ZZ(a,b)')
  23. assert jacobi_poly(0, a, b, x) == 1
  24. assert jacobi_poly(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1)
  25. assert jacobi_poly(2, a, b, x) == (a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 +
  26. x**2*(a**2/8 + a*b/4 + a*Q(7, 8) + b**2/8 +
  27. b*Q(7, 8) + Q(3, 2)) + x*(a**2/4 +
  28. a*Q(3, 4) - b**2/4 - b*Q(3, 4)) - S.Half)
  29. assert jacobi_poly(1, a, b, polys=True) == Poly(
  30. (a/2 + b/2 + 1)*x + a/2 - b/2, x, domain='ZZ(a,b)')
  31. def test_gegenbauer_poly():
  32. raises(ValueError, lambda: gegenbauer_poly(-1, a, x))
  33. assert gegenbauer_poly(
  34. 1, a, x, polys=True) == Poly(2*a*x, x, domain='ZZ(a)')
  35. assert gegenbauer_poly(0, a, x) == 1
  36. assert gegenbauer_poly(1, a, x) == 2*a*x
  37. assert gegenbauer_poly(2, a, x) == -a + x**2*(2*a**2 + 2*a)
  38. assert gegenbauer_poly(
  39. 3, a, x) == x**3*(4*a**3/3 + 4*a**2 + a*Q(8, 3)) + x*(-2*a**2 - 2*a)
  40. assert gegenbauer_poly(1, S.Half).dummy_eq(x)
  41. assert gegenbauer_poly(1, a, polys=True) == Poly(2*a*x, x, domain='ZZ(a)')
  42. def test_chebyshevt_poly():
  43. raises(ValueError, lambda: chebyshevt_poly(-1, x))
  44. assert chebyshevt_poly(1, x, polys=True) == Poly(x)
  45. assert chebyshevt_poly(0, x) == 1
  46. assert chebyshevt_poly(1, x) == x
  47. assert chebyshevt_poly(2, x) == 2*x**2 - 1
  48. assert chebyshevt_poly(3, x) == 4*x**3 - 3*x
  49. assert chebyshevt_poly(4, x) == 8*x**4 - 8*x**2 + 1
  50. assert chebyshevt_poly(5, x) == 16*x**5 - 20*x**3 + 5*x
  51. assert chebyshevt_poly(6, x) == 32*x**6 - 48*x**4 + 18*x**2 - 1
  52. assert chebyshevt_poly(1).dummy_eq(x)
  53. assert chebyshevt_poly(1, polys=True) == Poly(x)
  54. def test_chebyshevu_poly():
  55. raises(ValueError, lambda: chebyshevu_poly(-1, x))
  56. assert chebyshevu_poly(1, x, polys=True) == Poly(2*x)
  57. assert chebyshevu_poly(0, x) == 1
  58. assert chebyshevu_poly(1, x) == 2*x
  59. assert chebyshevu_poly(2, x) == 4*x**2 - 1
  60. assert chebyshevu_poly(3, x) == 8*x**3 - 4*x
  61. assert chebyshevu_poly(4, x) == 16*x**4 - 12*x**2 + 1
  62. assert chebyshevu_poly(5, x) == 32*x**5 - 32*x**3 + 6*x
  63. assert chebyshevu_poly(6, x) == 64*x**6 - 80*x**4 + 24*x**2 - 1
  64. assert chebyshevu_poly(1).dummy_eq(2*x)
  65. assert chebyshevu_poly(1, polys=True) == Poly(2*x)
  66. def test_hermite_poly():
  67. raises(ValueError, lambda: hermite_poly(-1, x))
  68. assert hermite_poly(1, x, polys=True) == Poly(2*x)
  69. assert hermite_poly(0, x) == 1
  70. assert hermite_poly(1, x) == 2*x
  71. assert hermite_poly(2, x) == 4*x**2 - 2
  72. assert hermite_poly(3, x) == 8*x**3 - 12*x
  73. assert hermite_poly(4, x) == 16*x**4 - 48*x**2 + 12
  74. assert hermite_poly(5, x) == 32*x**5 - 160*x**3 + 120*x
  75. assert hermite_poly(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120
  76. assert hermite_poly(1).dummy_eq(2*x)
  77. assert hermite_poly(1, polys=True) == Poly(2*x)
  78. def test_hermite_prob_poly():
  79. raises(ValueError, lambda: hermite_prob_poly(-1, x))
  80. assert hermite_prob_poly(1, x, polys=True) == Poly(x)
  81. assert hermite_prob_poly(0, x) == 1
  82. assert hermite_prob_poly(1, x) == x
  83. assert hermite_prob_poly(2, x) == x**2 - 1
  84. assert hermite_prob_poly(3, x) == x**3 - 3*x
  85. assert hermite_prob_poly(4, x) == x**4 - 6*x**2 + 3
  86. assert hermite_prob_poly(5, x) == x**5 - 10*x**3 + 15*x
  87. assert hermite_prob_poly(6, x) == x**6 - 15*x**4 + 45*x**2 - 15
  88. assert hermite_prob_poly(1).dummy_eq(x)
  89. assert hermite_prob_poly(1, polys=True) == Poly(x)
  90. def test_legendre_poly():
  91. raises(ValueError, lambda: legendre_poly(-1, x))
  92. assert legendre_poly(1, x, polys=True) == Poly(x, domain='QQ')
  93. assert legendre_poly(0, x) == 1
  94. assert legendre_poly(1, x) == x
  95. assert legendre_poly(2, x) == Q(3, 2)*x**2 - Q(1, 2)
  96. assert legendre_poly(3, x) == Q(5, 2)*x**3 - Q(3, 2)*x
  97. assert legendre_poly(4, x) == Q(35, 8)*x**4 - Q(30, 8)*x**2 + Q(3, 8)
  98. assert legendre_poly(5, x) == Q(63, 8)*x**5 - Q(70, 8)*x**3 + Q(15, 8)*x
  99. assert legendre_poly(6, x) == Q(
  100. 231, 16)*x**6 - Q(315, 16)*x**4 + Q(105, 16)*x**2 - Q(5, 16)
  101. assert legendre_poly(1).dummy_eq(x)
  102. assert legendre_poly(1, polys=True) == Poly(x)
  103. def test_laguerre_poly():
  104. raises(ValueError, lambda: laguerre_poly(-1, x))
  105. assert laguerre_poly(1, x, polys=True) == Poly(-x + 1, domain='QQ')
  106. assert laguerre_poly(0, x) == 1
  107. assert laguerre_poly(1, x) == -x + 1
  108. assert laguerre_poly(2, x) == Q(1, 2)*x**2 - Q(4, 2)*x + 1
  109. assert laguerre_poly(3, x) == -Q(1, 6)*x**3 + Q(9, 6)*x**2 - Q(18, 6)*x + 1
  110. assert laguerre_poly(4, x) == Q(
  111. 1, 24)*x**4 - Q(16, 24)*x**3 + Q(72, 24)*x**2 - Q(96, 24)*x + 1
  112. assert laguerre_poly(5, x) == -Q(1, 120)*x**5 + Q(25, 120)*x**4 - Q(
  113. 200, 120)*x**3 + Q(600, 120)*x**2 - Q(600, 120)*x + 1
  114. assert laguerre_poly(6, x) == Q(1, 720)*x**6 - Q(36, 720)*x**5 + Q(450, 720)*x**4 - Q(2400, 720)*x**3 + Q(5400, 720)*x**2 - Q(4320, 720)*x + 1
  115. assert laguerre_poly(0, x, a) == 1
  116. assert laguerre_poly(1, x, a) == -x + a + 1
  117. assert laguerre_poly(2, x, a) == x**2/2 + (-a - 2)*x + a**2/2 + a*Q(3, 2) + 1
  118. assert laguerre_poly(3, x, a) == -x**3/6 + (a/2 + Q(
  119. 3)/2)*x**2 + (-a**2/2 - a*Q(5, 2) - 3)*x + a**3/6 + a**2 + a*Q(11, 6) + 1
  120. assert laguerre_poly(1).dummy_eq(-x + 1)
  121. assert laguerre_poly(1, polys=True) == Poly(-x + 1)
  122. def test_spherical_bessel_fn():
  123. x, z = symbols("x z")
  124. assert spherical_bessel_fn(1, z) == 1/z**2
  125. assert spherical_bessel_fn(2, z) == -1/z + 3/z**3
  126. assert spherical_bessel_fn(3, z) == -6/z**2 + 15/z**4
  127. assert spherical_bessel_fn(4, z) == 1/z - 45/z**3 + 105/z**5