SymPy – Simplification


SymPy – Simplification



”;


Sympy has powerful ability to simplify mathematical expressions. There are many functions in SymPy to perform various kinds of simplification. A general function called simplify() is there that attempts to arrive at the simplest form of an expression.

simplify

This function is defined in sympy.simplify module. simplify() tries to apply intelligent heuristics to make the input expression “simpler”. Following code shows simplifies expression $sin^2(x)+cos^2(x)$.


>>> from sympy import * 
>>> x=Symbol(''x'')
>>> expr=sin(x)**2 + cos(x)**2 
>>> simplify(expr)

The above code snippet gives the following output −

1

expand

The expand() is one of the most common simplification functions in SymPy, used in expanding polynomial expressions. For example −


>>> a,b=symbols(''a b'') 
>>> expand((a+b)**2)

The above code snippet gives an output equivalent to the below expression −

$a^2 + 2ab + b^2$


>>> expand((a+b)*(a-b))

The above code snippet gives an output equivalent to the below expression −

$a^2 – b^2$

The expand() function makes expressions bigger, not smaller. Usually this is the case, but often an expression will become smaller upon calling expand() on it.


>>> expand((x + 1)*(x - 2) - (x - 1)*x)

The above code snippet gives the following output −

-2

factor

This function takes a polynomial and factors it into irreducible factors over the rational numbers.


>>> x,y,z=symbols(''x y z'') 
>>> expr=(x**2*z + 4*x*y*z + 4*y**2*z) 
>>> factor(expr)

The above code snippet gives an output equivalent to the below expression −

$z(x + 2y)^2$


>>> factor(x**2+2*x+1)

The above code snippet gives an output equivalent to the below expression −

$(x + 1)^2$

The factor() function is the opposite of expand(). Each of the factors returned by factor() is guaranteed to be irreducible. The factor_list() function returns a more structured output.


>>> expr=(x**2*z + 4*x*y*z + 4*y**2*z) 
>>> factor_list(expr)

The above code snippet gives an output equivalent to the below expression −

(1, [(z, 1), (x + 2*y, 2)])

collect

This function collects additve terms of an expression with respect to a list of expression up to powers with rational exponents.


>>> expr=x*y + x - 3 + 2*x**2 - z*x**2 + x**3 
>>> expr

The above code snippet gives an output equivalent to the below expression −

$x^3 + x^2z + 2x^2 + xy + x – 3$

The collect() function on this expression results as follows −


>>> collect(expr,x)

The above code snippet gives an output equivalent to the below expression −

$x^3 + x^2(2 – z) + x(y + 1) – 3$


>>> expr=y**2*x + 4*x*y*z + 4*y**2*z+y**3+2*x*y 
>>> collect(expr,y)

The above code snippet gives an output equivalent to the below expression −

$Y^3+Y^2(x+4z)+y(4xz+2x)$

cancel

The cancel() function will take any rational function and put it into the standard canonical form, p/q, where p and q are expanded polynomials with no common factors. The leading coefficients of p and q do not have denominators i.e., they are integers.


>>> expr1=x**2+2*x+1 
>>> expr2=x+1 
>>> cancel(expr1/expr2)

The above code snippet gives an output equivalent to the below expression −

$x+1$


>>> expr = 1/x + (3*x/2 - 2)/(x - 4) 
>>> expr

The above code snippet gives an output equivalent to the below expression −

$frac{frac{3x}{2} – 2}{x – 4} + frac{1}{x}$


>>> cancel(expr)

The above code snippet gives an output equivalent to the below expression −

$frac{3x^2 – 2x – 8}{2x^2 – 8}$


>>> expr=1/sin(x)**2 
>>> expr1=sin(x) 
>>> cancel(expr1*expr)

The above code snippet gives an output equivalent to the below expression −

$frac{1}{sin(x)}$

trigsimp

This function is used to simplify trigonometric identities. It may be noted that naming conventions for inverse trigonometric functions is to append an a to the front of the function’s name. For example, the inverse cosine, or arc cosine, is called acos().


>>> from sympy import trigsimp, sin, cos 
>>> from sympy.abc import x, y
>>> expr = 2*sin(x)**2 + 2*cos(x)**2 
>>> trigsimp(expr)

2

The trigsimp function uses heuristics to apply the best suitable trigonometric identity.

powersimp

This function reduces given expression by combining powers with similar bases and exponents.


>>> expr=x**y*x**z*y**z 
>>> expr

The above code snippet gives an output equivalent to the below expression −

$x^y x^z y^z$


>>> powsimp(expr)

The above code snippet gives an output equivalent to the below expression −

$x^{y+z} y^z$

You can make powsimp() only combine bases or only combine exponents by changing combine=’base’ or combine=’exp’. By default, combine=’all’, which does both.If force is True then bases will be combined without checking for assumptions.


>>> powsimp(expr, combine=''base'', force=True)

The above code snippet gives an output equivalent to the below expression −

$x^y(xy)^z$

combsimp

Combinatorial expressions involving factorial an binomials can be simplified by using combsimp() function. SymPy provides a factorial() function


>>> expr=factorial(x)/factorial(x - 3) 
>>> expr

The above code snippet gives an output equivalent to the below expression −

$frac{x!}{(x – 3)!}$

To simplify above combinatorial expression we use combsimp() function as follows −


>>> combsimp(expr)

The above code snippet gives an output equivalent to the below expression −

$x(x-2)(x-1)$

The binomial(x, y) is the number of ways to choose y items from a set of x distinct items. It is also often written as xCy.


>>> binomial(x,y)

The above code snippet gives an output equivalent to the below expression −

$(frac{x}{y})$


>>> combsimp(binomial(x+1, y+1)/binomial(x, y))

The above code snippet gives an output equivalent to the below expression −

$frac{x + 1}{y + 1}$

logcombine

This function takes logarithms and combines them using the following rules −

  • log(x) + log(y) == log(x*y) if both are positive
  • a*log(x) == log(x**a) if x is positive and a is real


>>> logcombine(a*log(x) + log(y) - log(z))

The above code snippet gives an output equivalent to the below expression −

$alog(x) + log(y) – log(z)$

If force parameter of this function is set to True then the assumptions above will be assumed to hold if there is no assumption already in place on a quantity.


>>> logcombine(a*log(x) + log(y) - log(z), force=True)

The above code snippet gives an output equivalent to the below expression −

$logfrac{x^a y}{z}$

Advertisements

”;

Leave a Reply

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