How to Simplify Expressions in SymPy

How to Simplify Expressions in SymPy

SymPy is a Python library for symbolic mathematics. It provides a plethora of tools for algebraic manipulation, calculus, discrete mathematics, and other areas of mathematics.

Simplification helps make expressions more readable and manageable. Simplified expressions are easier to interpret, debug, and use in further calculations. Let’s have a look at how we can use SymPy to simplify expressions.

Before we begin, make sure you have SymPy installed. You can do this using pip:

pip install sympy as sp

After this, make sure you import SymPy into your Python script in order to be able to execute the code excerpts below.

import sympy as sp

Basic Simplification Techniques

We have already learned how to define symbols and expressions in previous articles in this series:

x = sp.symbols('x')
expr = x**4 - 4*x**2 + 4 + 2*sp.sin(x)**2 + 2*sp.cos(x)**2

The simplify function in SymPy takes an expression and returns a simplified form. This function applies various simplification techniques internally to achieve the simplest form of the expression.

Here is how to use simplify in SymPy for simplification.

simplified_expr = sp.simplify(expr)
print(simplified_expr)

Output:

x**4 - 4*x**2 + 6

Advanced Simplification Techniques

Simplifying with Expansion

SymPy provides specific functions for expanding and factoring expressions. These functions are useful in different contexts.

To expand an expression using SymPy’s expand function:

expanded_expr = sp.expand((x - 1)**2)
print(expanded_expr)

Output:

x**2 - 2*x + 1

Simplifying with Factorization

To factor an expression using SymPy’s factor function:

factored_expr = sp.factor(x**2 - 1)
print(factored_expr)

Output:

(x - 1)*(x + 1)

Simplifying Rational Expressions

SymPy can simplify rational expressions, such as fractions. The Rational function is used to create rational numbers, which can then be simplified.

rational_expr = sp.Rational(3, 6)
simplified_rational = sp.simplify(rational_expr)
print(simplified_rational)

Output:

1/2

Simplifying Trigonometric Expressions

SymPy provides functions specifically for simplifying trigonometric expressions. The trigsimp function is one such function.

x = sp.symbols('x')
trig_expr = sp.sin(x)**2 + sp.cos(x)**2
simplified_trig_expr = sp.trigsimp(trig_expr)
print(simplified_trig_expr)

Output:

1

Simplifying Logarithmic and Exponential Expressions

For logarithmic expressions, SymPy offers functions like logcombine, which can combine multiple logarithms into a single expression.

x, y = sp.symbols('x y')
log_expr = sp.log(x) + sp.log(y)
simplified_log_expr = sp.logcombine(log_expr)
print(simplified_log_expr)

Output:

log(x) + log(y)

Simplifying Piecewise Functions

SymPy can also simplify piecewise functions, which are functions defined by multiple sub-functions, each applying to a certain interval. SymPy’s Piecewise function can handle this.

x, y = sp.symbols('x y')
piecewise_expr = sp.Piecewise((x, x < 1), (x**2, x >= 1))
simplified_piecewise = sp.simplify(piecewise_expr)
print(simplified_piecewise)

Output:

Piecewise((x, x < 1), (x**2, True))

Final Thoughts

SymPy is a clearly powerful tool for performing symbolic arithmetic operations. It simplifies complex algebraic manipulations, making it an essential tool for any Python programmer looking to perform such operations.

In this article, we covered a variety of techniques for simplifying expressions using SymPy, including basic simplification, expanding, factoring, and specialized functions for rational, trigonometric, logarithmic, and piecewise expressions.

Leave a Reply

Your email address will not be published. Required fields are marked *