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.is_polynomial() Method:
We can use the sympy.is_polynomial() method to check if it is a polynomial. If a polynomial is found, it will return True as a boolean value.
Syntax:
sympy.is_polynomial()
Return Value:
If a polynomial is found, returns a boolean value True, otherwise False.
sympy.is_polynomial() Method in Python
Method #1: Using is_polynomial() 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.
- Pass the above given mathematical expression to the simplify() function and apply is_polynomial() function on it to check whether the given expression is a polynomial or Not.
- Store it in another variable.
- Print the result after checking whether the given expression is a polynomial or Not.
- 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 # Pass the above given mathematical expression to the simplify() function and # apply is_polynomial() function on it to check whether the given expression # is a polynomial or Not. # Store it in another variable. rslt = simplify(gvn_expression).is_polynomial() # Print the result after checking whether the given expression is a polynomial or Not. print("Checking whether the given expression{",gvn_expression,"} is a polynomial or Not:") print(rslt)
Output:
Checking whether the given expression{ 3*x*y + y**2 + y } is a polynomial or Not: True
Method #2: Using is_polynomial() 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 above given mathematical expression to the simplify() function and apply is_polynomial() function on it to check whether the given expression is a polynomial or Not.
- Store it in another variable.
- Print the result after checking whether the given expression is a polynomial or Not.
- 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 above given mathematical expression to the simplify() function and # apply is_polynomial() function on it to check whether the given expression # is a polynomial or Not. # Store it in another variable. rslt = simplify(gvn_expression).is_polynomial() # Print the result after checking whether the given expression is a polynomial or Not. print("Checking whether the given expression{",gvn_expression,"} is a polynomial or Not:") print(rslt)
Output:
Enter some random mathematical expression = log(x)+xy Checking whether the given expression{ log(x)+xy } is a polynomial or Not: False