How to compare two algebraic expressions in Sympy

This recipe helps you compare two algebraic expressions in Sympy

Recipe Objective - How to compare two algebraic expressions in Sympy?

SymPy expressions are compared with equals() function.

Explore the Real-World Applications of Recommender Systems 

For more related projects -

https://www.dezyre.com/projects/data-science-projects/deep-learning-projects
https://www.dezyre.com/projects/data-science-projects/keras-deep-learning-projects

Example:

# Importing libraries
from sympy import pprint
from sympy.abc import p,q

# Defining some expressions
exp1 = (p + q)**2
exp2 = (p**2)+(2*p*q)+(q**2)

# Printing both expressions
pprint(exp1)
pprint(exp2)

# Comparing both expressions
exp1.equals(exp2)

Output - 
       2
(p + q) 
 2            2
p  + 2.p.q + q 
True

# Importing libraries
from sympy import pprint, sin, cos
from sympy.abc import p

# Defining some expression
exp1 = (sin(p)**2) + (cos(p)**2)
exp2 = 2

# Printing both expressions
pprint(exp1)
pprint(exp2)

# Comparing both expressions
exp1.equals(exp2)

Output - 
   2         2   
sin (p) + cos (p)
2
False

In this way, we can compare two expressions in sympy.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

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.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

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 an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

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

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

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.