# Python sympy.sympify() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.sympify() Method:

We can convert the expression of string type to general mathematical expression using the sympify() function of the sympy module.

Syntax:

 sympy.sympify()

Return Value:

The general mathematical expression is returned by the sympify() function.

## sympy.sympify() Method in Python

Example1

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be as arguments to the symbols() function and store them in corresponding variables.
• Give the mathematical expression as static input and store it in a variable.
• Pass the above-given expression as an argument to the sympify() method to get the string of expression into the real mathematical expression.
• Store it in another variable.
• Print the real mathematical expression.
• The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be as arguments to the symbols() function
# and store them in corresponding variables.
x, y = symbols('x y')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = "x**3 + x**2 - x - 1"

# Pass the above given expression as an argument to the sympify() method to get the
# string of expression into the real mathematical expression.
# Store it in another variable.
rslt = sympify(gvn_expression)

# Print the real mathematical expression.
print("The real mathematical expression = ", rslt)

Output:

The real mathematical expression = x**3 + x**2 - x - 1

Example2

Here we substitute 2 in place of x and get the simplified result using the subs() function.

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be as arguments to the symbols() function and store them in corresponding variables.
• Give the mathematical expression as static input and store it in a variable.
• Pass the above-given expression as an argument to the sympify() method to get the string of expression into the real mathematical expression.
• Store it in another variable.
• Pass the variable, number as arguments to the subs() function to substitute the given number in place of the variable given in the expression
• Here it substitutes 2 in place of x and gets the simplified result.
• Print the simplified result after substituting x=2 in the expression.
• The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be as arguments to the symbols() function
# and store them in corresponding variables.
x, y = symbols('x y')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = "x**3 + x**2 - x - 1"

# Pass the above given expression as an argument to the sympify() method to get the
# string of expression into the real mathematical expression.
# Store it in another variable.
rslt = sympify(gvn_expression)

# Pass the variable, number as arguments to the subs() function to substitute the
# given number in place of the variable given in the expression
# Here it substitutes 2 in place of x and get the simplified result.
simplified_rslt = rslt.subs(x, 2)

# Print the simplified result after substituting x=2 in the expression
print("The simplified result after substituting x=2 in the expression")
print(simplified_rslt)

Output:

The simplified result after substituting x=2 in the expression
9

Explanation:

Here we substitute 2 in place of x i.e, 2^3+2^2-2-1 = 8+4-2-1 = 9