Last modified: Jan 13, 2025 By Alexander Williams

Python SymPy Series: Simplify Series Calculations

Python's SymPy library is a powerful tool for symbolic mathematics. One of its key features is the series() function. This function helps you compute series expansions of mathematical expressions.

In this article, we'll explore how to use the series() function in SymPy. We'll also provide examples to help you understand its usage better.

What is SymPy's series() Function?

The series() function in SymPy is used to compute the series expansion of a mathematical expression. It can handle both Taylor and Laurent series. This function is particularly useful in calculus and mathematical analysis.

To use the series() function, you need to import SymPy first. Here's how you can do it:


from sympy import symbols, series

Basic Usage of series()

Let's start with a simple example. Suppose you want to compute the Taylor series expansion of the function sin(x) around x = 0 up to the 6th order.


x = symbols('x')
expr = series(sin(x), x, 0, 6)
print(expr)


x - x**3/6 + x**5/120 + O(x**6)

In this example, series(sin(x), x, 0, 6) computes the Taylor series of sin(x) around x = 0 up to the 6th order. The O(x**6) term indicates the remainder of the series.

Handling More Complex Expressions

The series() function can also handle more complex expressions. For example, let's compute the series expansion of exp(x)*cos(x) around x = 0 up to the 4th order.


expr = series(exp(x)*cos(x), x, 0, 4)
print(expr)


1 + x - x**3/3 + O(x**4)

This example shows how the series() function can be used to compute the series expansion of a product of functions.

Using series() with Limits

You can also use the series() function in conjunction with other SymPy functions like limit. For example, let's compute the limit of sin(x)/x as x approaches 0 using series expansion.


from sympy import limit
expr = series(sin(x)/x, x, 0, 4)
limit_expr = limit(expr, x, 0)
print(limit_expr)


1

This example demonstrates how the series() function can be used to simplify the computation of limits. For more on limits, check out our guide on Python SymPy Limit: Calculate Limits Easily.

Conclusion

The series() function in SymPy is a powerful tool for computing series expansions of mathematical expressions. It can handle both simple and complex expressions, making it a valuable tool for anyone working with symbolic mathematics.

By mastering the series() function, you can simplify many calculus problems. For more advanced topics, consider exploring other SymPy functions like integrate and diff.

Start using the series() function in your Python projects today and take your mathematical computations to the next level!