test_deltafunctions.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from sympy.core.function import Function
  2. from sympy.core.numbers import (Rational, pi)
  3. from sympy.core.singleton import S
  4. from sympy.core.symbol import symbols
  5. from sympy.functions.elementary.trigonometric import (cos, sin)
  6. from sympy.functions.special.delta_functions import (DiracDelta, Heaviside)
  7. from sympy.integrals.deltafunctions import change_mul, deltaintegrate
  8. f = Function("f")
  9. x_1, x_2, x, y, z = symbols("x_1 x_2 x y z")
  10. def test_change_mul():
  11. assert change_mul(x, x) == (None, None)
  12. assert change_mul(x*y, x) == (None, None)
  13. assert change_mul(x*y*DiracDelta(x), x) == (DiracDelta(x), x*y)
  14. assert change_mul(x*y*DiracDelta(x)*DiracDelta(y), x) == \
  15. (DiracDelta(x), x*y*DiracDelta(y))
  16. assert change_mul(DiracDelta(x)**2, x) == \
  17. (DiracDelta(x), DiracDelta(x))
  18. assert change_mul(y*DiracDelta(x)**2, x) == \
  19. (DiracDelta(x), y*DiracDelta(x))
  20. def test_deltaintegrate():
  21. assert deltaintegrate(x, x) is None
  22. assert deltaintegrate(x + DiracDelta(x), x) is None
  23. assert deltaintegrate(DiracDelta(x, 0), x) == Heaviside(x)
  24. for n in range(10):
  25. assert deltaintegrate(DiracDelta(x, n + 1), x) == DiracDelta(x, n)
  26. assert deltaintegrate(DiracDelta(x), x) == Heaviside(x)
  27. assert deltaintegrate(DiracDelta(-x), x) == Heaviside(x)
  28. assert deltaintegrate(DiracDelta(x - y), x) == Heaviside(x - y)
  29. assert deltaintegrate(DiracDelta(y - x), x) == Heaviside(x - y)
  30. assert deltaintegrate(x*DiracDelta(x), x) == 0
  31. assert deltaintegrate((x - y)*DiracDelta(x - y), x) == 0
  32. assert deltaintegrate(DiracDelta(x)**2, x) == DiracDelta(0)*Heaviside(x)
  33. assert deltaintegrate(y*DiracDelta(x)**2, x) == \
  34. y*DiracDelta(0)*Heaviside(x)
  35. assert deltaintegrate(DiracDelta(x, 1), x) == DiracDelta(x, 0)
  36. assert deltaintegrate(y*DiracDelta(x, 1), x) == y*DiracDelta(x, 0)
  37. assert deltaintegrate(DiracDelta(x, 1)**2, x) == -DiracDelta(0, 2)*Heaviside(x)
  38. assert deltaintegrate(y*DiracDelta(x, 1)**2, x) == -y*DiracDelta(0, 2)*Heaviside(x)
  39. assert deltaintegrate(DiracDelta(x) * f(x), x) == f(0) * Heaviside(x)
  40. assert deltaintegrate(DiracDelta(-x) * f(x), x) == f(0) * Heaviside(x)
  41. assert deltaintegrate(DiracDelta(x - 1) * f(x), x) == f(1) * Heaviside(x - 1)
  42. assert deltaintegrate(DiracDelta(1 - x) * f(x), x) == f(1) * Heaviside(x - 1)
  43. assert deltaintegrate(DiracDelta(x**2 + x - 2), x) == \
  44. Heaviside(x - 1)/3 + Heaviside(x + 2)/3
  45. p = cos(x)*(DiracDelta(x) + DiracDelta(x**2 - 1))*sin(x)*(x - pi)
  46. assert deltaintegrate(p, x) - (-pi*(cos(1)*Heaviside(-1 + x)*sin(1)/2 - \
  47. cos(1)*Heaviside(1 + x)*sin(1)/2) + \
  48. cos(1)*Heaviside(1 + x)*sin(1)/2 + \
  49. cos(1)*Heaviside(-1 + x)*sin(1)/2) == 0
  50. p = x_2*DiracDelta(x - x_2)*DiracDelta(x_2 - x_1)
  51. assert deltaintegrate(p, x_2) == x*DiracDelta(x - x_1)*Heaviside(x_2 - x)
  52. p = x*y**2*z*DiracDelta(y - x)*DiracDelta(y - z)*DiracDelta(x - z)
  53. assert deltaintegrate(p, y) == x**3*z*DiracDelta(x - z)**2*Heaviside(y - x)
  54. assert deltaintegrate((x + 1)*DiracDelta(2*x), x) == S.Half * Heaviside(x)
  55. assert deltaintegrate((x + 1)*DiracDelta(x*Rational(2, 3) + Rational(4, 9)), x) == \
  56. S.Half * Heaviside(x + Rational(2, 3))
  57. a, b, c = symbols('a b c', commutative=False)
  58. assert deltaintegrate(DiracDelta(x - y)*f(x - b)*f(x - a), x) == \
  59. f(y - b)*f(y - a)*Heaviside(x - y)
  60. p = f(x - a)*DiracDelta(x - y)*f(x - c)*f(x - b)
  61. assert deltaintegrate(p, x) == f(y - a)*f(y - c)*f(y - b)*Heaviside(x - y)
  62. p = DiracDelta(x - z)*f(x - b)*f(x - a)*DiracDelta(x - y)
  63. assert deltaintegrate(p, x) == DiracDelta(y - z)*f(y - b)*f(y - a) * \
  64. Heaviside(x - y)