series.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from sympy.core.sympify import sympify
  2. def series(expr, x=None, x0=0, n=6, dir="+"):
  3. """Series expansion of expr around point `x = x0`.
  4. Parameters
  5. ==========
  6. expr : Expression
  7. The expression whose series is to be expanded.
  8. x : Symbol
  9. It is the variable of the expression to be calculated.
  10. x0 : Value
  11. The value around which ``x`` is calculated. Can be any value
  12. from ``-oo`` to ``oo``.
  13. n : Value
  14. The number of terms upto which the series is to be expanded.
  15. dir : String, optional
  16. The series-expansion can be bi-directional. If ``dir="+"``,
  17. then (x->x0+). If ``dir="-", then (x->x0-). For infinite
  18. ``x0`` (``oo`` or ``-oo``), the ``dir`` argument is determined
  19. from the direction of the infinity (i.e., ``dir="-"`` for
  20. ``oo``).
  21. Examples
  22. ========
  23. >>> from sympy import series, tan, oo
  24. >>> from sympy.abc import x
  25. >>> f = tan(x)
  26. >>> series(f, x, 2, 6, "+")
  27. tan(2) + (1 + tan(2)**2)*(x - 2) + (x - 2)**2*(tan(2)**3 + tan(2)) +
  28. (x - 2)**3*(1/3 + 4*tan(2)**2/3 + tan(2)**4) + (x - 2)**4*(tan(2)**5 +
  29. 5*tan(2)**3/3 + 2*tan(2)/3) + (x - 2)**5*(2/15 + 17*tan(2)**2/15 +
  30. 2*tan(2)**4 + tan(2)**6) + O((x - 2)**6, (x, 2))
  31. >>> series(f, x, 2, 3, "-")
  32. tan(2) + (2 - x)*(-tan(2)**2 - 1) + (2 - x)**2*(tan(2)**3 + tan(2))
  33. + O((x - 2)**3, (x, 2))
  34. >>> series(f, x, 2, oo, "+")
  35. Traceback (most recent call last):
  36. ...
  37. TypeError: 'Infinity' object cannot be interpreted as an integer
  38. Returns
  39. =======
  40. Expr
  41. Series expansion of the expression about x0
  42. See Also
  43. ========
  44. sympy.core.expr.Expr.series: See the docstring of Expr.series() for complete details of this wrapper.
  45. """
  46. expr = sympify(expr)
  47. return expr.series(x, x0, n, dir)