singularityfunctions.py 2.2 KB

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