singularityfunctions.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from sympy.functions import SingularityFunction, DiracDelta
  2. from sympy.core import sympify
  3. from sympy.integrals import integrate
  4. def singularityintegrate(f, x):
  5. """
  6. This function handles the indefinite integrations of Singularity functions.
  7. The ``integrate`` function calls this function internally whenever an
  8. instance of SingularityFunction is passed as argument.
  9. Explanation
  10. ===========
  11. The idea for integration is the following:
  12. - If we are dealing with a SingularityFunction expression,
  13. i.e. ``SingularityFunction(x, a, n)``, we just return
  14. ``SingularityFunction(x, a, n + 1)/(n + 1)`` if ``n >= 0`` and
  15. ``SingularityFunction(x, a, n + 1)`` if ``n < 0``.
  16. - If the node is a multiplication or power node having a
  17. SingularityFunction term we rewrite the whole expression in terms of
  18. Heaviside and DiracDelta and then integrate the output. Lastly, we
  19. rewrite the output of integration back in terms of SingularityFunction.
  20. - If none of the above case arises, we return None.
  21. Examples
  22. ========
  23. >>> from sympy.integrals.singularityfunctions import singularityintegrate
  24. >>> from sympy import SingularityFunction, symbols, Function
  25. >>> x, a, n, y = symbols('x a n y')
  26. >>> f = Function('f')
  27. >>> singularityintegrate(SingularityFunction(x, a, 3), x)
  28. SingularityFunction(x, a, 4)/4
  29. >>> singularityintegrate(5*SingularityFunction(x, 5, -2), x)
  30. 5*SingularityFunction(x, 5, -1)
  31. >>> singularityintegrate(6*SingularityFunction(x, 5, -1), x)
  32. 6*SingularityFunction(x, 5, 0)
  33. >>> singularityintegrate(x*SingularityFunction(x, 0, -1), x)
  34. 0
  35. >>> singularityintegrate(SingularityFunction(x, 1, -1) * f(x), x)
  36. f(1)*SingularityFunction(x, 1, 0)
  37. """
  38. if not f.has(SingularityFunction):
  39. return None
  40. if f.func == SingularityFunction:
  41. x = sympify(f.args[0])
  42. a = sympify(f.args[1])
  43. n = sympify(f.args[2])
  44. if n.is_positive or n.is_zero:
  45. return SingularityFunction(x, a, n + 1)/(n + 1)
  46. elif n in (-1, -2):
  47. return SingularityFunction(x, a, n + 1)
  48. if f.is_Mul or f.is_Pow:
  49. expr = f.rewrite(DiracDelta)
  50. expr = integrate(expr, x)
  51. return expr.rewrite(SingularityFunction)
  52. return None