kauers.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. def finite_diff(expression, variable, increment=1):
  2. """
  3. Takes as input a polynomial expression and the variable used to construct
  4. it and returns the difference between function's value when the input is
  5. incremented to 1 and the original function value. If you want an increment
  6. other than one supply it as a third argument.
  7. Examples
  8. ========
  9. >>> from sympy.abc import x, y, z
  10. >>> from sympy.series.kauers import finite_diff
  11. >>> finite_diff(x**2, x)
  12. 2*x + 1
  13. >>> finite_diff(y**3 + 2*y**2 + 3*y + 4, y)
  14. 3*y**2 + 7*y + 6
  15. >>> finite_diff(x**2 + 3*x + 8, x, 2)
  16. 4*x + 10
  17. >>> finite_diff(z**3 + 8*z, z, 3)
  18. 9*z**2 + 27*z + 51
  19. """
  20. expression = expression.expand()
  21. expression2 = expression.subs(variable, variable + increment)
  22. expression2 = expression2.expand()
  23. return expression2 - expression
  24. def finite_diff_kauers(sum):
  25. """
  26. Takes as input a Sum instance and returns the difference between the sum
  27. with the upper index incremented by 1 and the original sum. For example,
  28. if S(n) is a sum, then finite_diff_kauers will return S(n + 1) - S(n).
  29. Examples
  30. ========
  31. >>> from sympy.series.kauers import finite_diff_kauers
  32. >>> from sympy import Sum
  33. >>> from sympy.abc import x, y, m, n, k
  34. >>> finite_diff_kauers(Sum(k, (k, 1, n)))
  35. n + 1
  36. >>> finite_diff_kauers(Sum(1/k, (k, 1, n)))
  37. 1/(n + 1)
  38. >>> finite_diff_kauers(Sum((x*y**2), (x, 1, n), (y, 1, m)))
  39. (m + 1)**2*(n + 1)
  40. >>> finite_diff_kauers(Sum((x*y), (x, 1, m), (y, 1, n)))
  41. (m + 1)*(n + 1)
  42. """
  43. function = sum.function
  44. for l in sum.limits:
  45. function = function.subs(l[0], l[- 1] + 1)
  46. return function