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_rational Method:
We can use the sympy.is_rational method to determine whether an element is rational or not. This method returns a boolean value, True or False.
Syntax:
sympy.is_rational
Return Value:
Returns True if the element is rational, otherwise False.
sympy.is_rational Method in Python
Method #1: Using is_rational Function (Static Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give some random number as static input and store it in a variable.
- Pass the above-given number as an argument to the simplify() function and apply is_rational
method on it to check whether the given number is a rational number or Not. - Store it in another variable.
- Print the result after checking whether the given number is a rational number 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 * # Give some random number as static input and store it in a variable. gvn_numb = 2*I # Pass the above given number as an argument to the simplify() function and apply is_rational # method on it to check whether the given number is a rational number or Not. # Store it in another variable. rslt = simplify(gvn_numb).is_rational # Print the result after checking whether the given number is a rational number or Not. print("Checking whether the given number{",gvn_numb,"} is a rational number or Not =", rslt)
Output:
Checking whether the given number{ 2*I } is a rational number or Not = False
Method #2: Using is_rational Function (User Input)
Approach:
- Import all the functions from sympy module using the import keyword
- Give some random number as user input using the input() function and store it in a variable.
- Pass the above-given number as an argument to the simplify() function and apply is_rational
method on it to check whether the given number is a rational number or Not. - Store it in another variable.
- Print the result after checking whether the given number is a rational number 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 * # Give some random number as user input using the input() function # and store it in a variable. gvn_numb = input("Enter some random number = ") # Pass the above given number as an argument to the simplify() function and apply is_rational # method on it to check whether the given number is a rational number or Not. # Store it in another variable. rslt = simplify(gvn_numb).is_rational # Print the result after checking whether the given number is a rational number or Not. print("Checking whether the given number{",gvn_numb,"} is a rational number or Not =", rslt)
Output:
Enter some random number = 5/3 Checking whether the given number{ 5/3 } is a rational number or Not = True