What are the Rational values in Sympy

This recipe explains what are the Rational values in Sympy

Recipe Objective - What are the Rational values in Sympy?

SymPy has Rational to work with rational numbers. A rational number is any number that can be expressed as the quotient or fraction a / b of two whole numbers, a numerator 'a' and denominator 'b' other than zero.

For more related projects -

https://www.projectpro.io/projects/data-science-projects/neural-network-projects
https://www.projectpro.io/projects/data-science-projects/keras-deep-learning-projects

Rational class:

from sympy import Rational
# A representation of a number (a / b) is represented as an object of class Rational, where b is a nonzero number.
Rational(1/2)

Output - 
1
-
2

from sympy import Rational
# When a floating point number is passed to the Rational () constructor, it returns the underlying value of its binary representation
Rational(0.12)

Output - 
1080863910568919
----------------
9007199254740992

from sympy import Rational
# When a string is passed to the Rational () constructor, a rational number of arbitrary precision is returned.
Rational("0.12")

Output - 
 3
--
25

from sympy import Rational
x = Rational(2/3)
y = Rational(1/2)

ans = (x + y)/2
ans

Output - 
10508399130531157
-----------------
18014398509481984

## The expression is in the symbolic form; we evaluate it with evalf() method.
ans.evalf()

Output - 
0.583333333333333

In this way, we can use Rational values in sympy.

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.