Python sympy.as_terms() 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.as_terms() Method:

We can get the mathematical expression as a list of terms using the sympy.as_terms() function.

Syntax:

  sympy.as_terms()

Return Value:

A list of terms in the given mathematical expression is returned by the as_terms() function.

sympy.as_terms() Method in Python

 

Method #1: Using as_terms() Function (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used 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.
  • Apply as_terms() function on the given mathematical expression to get the list of terms of a given mathematical expression
  • Store it in another variable.
  • Print the list of terms of a given 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 used 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 = y**2 + 3*y*x + y

# Apply as_terms() function on the given mathematical expression to get the 
# list of terms of a given mathematical expression
# Store it in another variable.
rslt = gvn_expression.as_terms()

# Print the list of terms of a given mathematical expression
print("The list of terms of a given mathematical expression {",gvn_expression,"} are:")
print(rslt)

Output:

The list of terms of a given mathematical expression { 3*x*y + y**2 + y } are:
([(y, ((1.0, 0.0), (0, 1), ())), (y**2, ((1.0, 0.0), (0, 2), ())), (3*x*y, ((3.0, 0.0), (1, 1), ()))], [x, y])

Method #2: Using as_terms() Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Give the mathematical expression as user input using the input() function and store it in a variable.
  • Pass the given expression as an argument to the simplify() function and apply as_terms() function on the given mathematical expression to get the list of terms of a given mathematical expression.
  • Store it in another variable.
  • Print the list of terms of a given 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 used as arguments to the symbols() function 
# and store them in corresponding variables.
x, y = symbols('x y')

# Give the mathematical expression as user input using the input() 
# function and store it in a variable.
gvn_expression = input("Enter some random  mathematical expression = ")

# Pass the given expression as an argument to the simplify() function and 
# apply as_terms() function on the given mathematical expression to get the 
# list of terms of a given mathematical expression
# Store it in another variable.
rslt = simplify(gvn_expression).as_terms()

# Print the list of terms of a given mathematical expression
print("The list of terms of a given mathematical expression {",gvn_expression,"} are:")
print(rslt)

Output:

Enter some random mathematical expression = 2*x+y
The list of terms of a given mathematical expression { 2*x+y } are:
([(y, ((1.0, 0.0), (0, 1), ())), (2*x, ((2.0, 0.0), (1, 0), ()))], [x, y])