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.
>>> from sympy import *
>>> x, y, z = symbols('x,y,z')
>>> ((x + y)*(x - y)).expand(basic=True)
x**2 - y**2
>>> ((x + y + z)**2).expand(basic=True)
x**2 + 2*x*y + 2*x*z + y**2 + 2*y*z + z**2
Arithmetic expand is done by default in so the keyword can be omitted. However you can set 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 keyword is used. Note that it will always perform arithmetic expand to obtain the desired normal form:
>>> (x + I*y).expand(complex=True)
I*im(x) - im(y) + re(x) + I*re(y)
>>> sin(x + I*y).expand(complex=True)
-sin(im(y) - re(x))*cosh(im(x) + re(y)) + I*cos(im(y) - re(x))*sinh(im(x) + re(y))
Note also that the same behavior can be obtained by using 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 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:
GoldenRatio.expand(func=True)
1/2 + (1/2)*5**(1/2)
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 function. Examples:
>>> from sympy import cse, sqrt, sin, pprint
>>> from sympy.abc import x
>>> pprint(cse(sqrt(sin(x))), use_unicode=True)
⎛ ⎡ ⎽⎽⎽⎽⎽⎽⎽⎽⎤⎞
⎝[], ⎣╲╱ sin(x) ⎦⎠
>>> pprint(cse(sqrt(sin(x)+5)*sqrt(sin(x)+4)), use_unicode=True)
⎛ ⎡ ⎽⎽⎽⎽⎽⎽⎽⎽ ⎽⎽⎽⎽⎽⎽⎽⎽⎤⎞
⎝[(x₀, sin(x))], ⎣╲╱ x₀ + 4 ⋅╲╱ x₀ + 5 ⎦⎠
>>> pprint(cse(sqrt(sin(x)+5+cos(y))*sqrt(sin(x)+4+cos(y))), use_unicode=True)
⎛ ⎡ ⎽⎽⎽⎽⎽⎽⎽⎽ ⎽⎽⎽⎽⎽⎽⎽⎽⎤⎞
⎝[(x₀, cos(y)), (x₁, sin(x)), (x₂, x₀ + x₁)], ⎣╲╱ x₂ + 4 ⋅╲╱ x₂ + 5 ⎦⎠
>>> pprint(cse((x-y)*(z-y) + sqrt((x-y)*(z-y))), use_unicode=True)
⎛ ⎡ ⎽⎽⎽⎽ ⎤⎞
⎝[(x₀, (x - y)⋅(-y + z))], ⎣╲╱ x₀ + x₀⎦⎠
More information:
Perform common subexpression elimination on an expression.
Parameters:
Returns: