Term rewriting

Term rewriting is a very general class of functionalities which are used to convert expressions of one type in terms of expressions of different kind. For example expanding, combining and converting expressions apply to term rewriting, and also simplification routines can be included here. Currently !SymPy has several functions and Basic built-in methods for performing various types of rewriting.


The simplest rewrite rule is expanding expressions into a _sparse_ form. Expanding has several flavors and include expanding complex valued expressions, arithmetic expand of products and powers but also expanding functions in terms of more general functions is possible. Below are listed all currently available expand rules.

Expanding of arithmetic expressions involving products and powers:
>>> from sympy import *
>>> x, y, z = symbols('xyz')
>>> ((x + y)*(x - y)).expand(basic=True)
x**2 - y**2
>>> ((x + y + z)**2).expand(basic=True)
2*x*y + 2*x*z + 2*y*z + x**2 + y**2 + z**2

Arithmetic expand is done by default in expand() so the keyword basic can be omitted. However you can set basic=False to avoid this type of expand if you use rules described below. This give complete control on what is done with the expression.

Another type of expand rule is expanding complex valued expressions and putting them into a normal form. For this complex keyword is used. Note that it will always perform arithmetic expand to obtain the desired normal form:

>>> (x + I*y).expand(complex=True)
-im(y) + I*im(x) + I*re(y) + re(x)
>>> sin(x + I*y).expand(complex=True)
-cosh(im(x) + re(y))*sin(-re(x) + im(y)) + I*cos(-re(x) + im(y))*sinh(im(x) + re(y))

Note also that the same behavior can be obtained by using as_real_imag() method. However it will return a tuple containing the real part in the first place and the imaginary part in the other. This can be also done in a two step process by using collect function:

>>> (x + I*y).as_real_imag()
(-im(y) + re(x), im(x) + re(y))
>>> collect((x + I*y).expand(complex=True), I, evaluate=False)
{1: -im(y) + re(x), I: im(x) + re(y)}

There is also possibility for expanding expressions in terms of expressions of different kind. This is very general type of expanding and usually you would use rewrite() to do specific type of rewrite:

1/2 + (1/2)*5**(1/2)

Common Subexpression Detection and Collection

Before evaluating a large expression, it is often useful to identify common subexpressions, collect them and evaluate them at once. This is implemented in the cse method. Examples:

In [1]: cse(sqrt(sin(x)))
⎛    ⎡  ⎽⎽⎽⎽⎽⎽⎽⎽⎤⎞
⎝[], ⎣╲╱ sin(x) ⎦⎠

In [2]: cse(sqrt(sin(x)+5)*sqrt(sin(x)+4))
⎛                ⎡  ⎽⎽⎽⎽⎽⎽⎽⎽   ⎽⎽⎽⎽⎽⎽⎽⎽⎤⎞
⎝[(x₀, sin(x))], ⎣╲╱ 4 + x₀ *╲╱ 5 + x₀ ⎦⎠

In [3]: cse(sqrt(sin(x)+5+cos(y))*sqrt(sin(x)+4+cos(y)))
⎛                              ⎡  ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽   ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎤⎞
⎝[(x₀, cos(y)), (x₁, sin(x))], ⎣╲╱ 4 + x₀ + x₁ *╲╱ 5 + x₀ + x₁ ⎦⎠

In [4]: cse((x-y)*(z-y) + sqrt((x-y)*(z-y)))
⎛                            ⎡          ⎽⎽⎽⎽   ⎽⎽⎽⎽⎤⎞
⎝[(x₀, z - y), (x₁, x - y)], ⎣x₀*x₁ + ╲╱ x₀ *╲╱ x₁ ⎦⎠

More information:

sympy.simplify.cse_main.cse(exprs, symbols=None, optimizations=None)

Perform common subexpression elimination on an expression.


exprs : list of sympy expressions, or a single sympy expression
The expressions to reduce.
symbols : infinite iterator yielding unique Symbols
The symbols used to label the common subexpressions which are pulled out. The numbered_symbols generator is useful. The default is a stream of symbols of the form “x0”, “x1”, etc. This must be an infinite iterator.
optimizations : list of (callable, callable) pairs, optional
The (preprocessor, postprocessor) pairs. If not provided, sympy.simplify.cse.cse_optimizations is used.


replacements : list of (Symbol, expression) pairs
All of the common subexpressions that were replaced. Subexpressions earlier in this list might show up in subexpressions later in this list.
reduced_exprs : list of sympy expressions
The reduced expressions with all of the replacements above.

Table Of Contents

Previous topic

Assumptions module

Next topic

Series Expansions

This Page