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

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Deep Learning Project for Beginners with Source Code Part 1
Learn to implement deep neural networks in Python .

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.