What are the collect and cancel functions in Sympy

This recipe explains what are the collect and cancel functions in Sympy

Recipe Objective - What are the collect and cancel functions in Sympy?

The collect function collects additive terms from an expression with respect to a list of expressions up to powers with rational exponents.

The cancel function takes any rational function and converts it in the standard canonical form a / b, where a and b are expanded polynomials with no common factors. The principal coefficients of a and b have no denominator; they are integers.

Explore Interesting IoT Project Ideas for Practice 

For more related projects -

https://www.projectpro.io/projects/data-science-projects/deep-learning-projects
https://www.projectpro.io/projects/data-science-projects/tensorflow-projects

Collect function:

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

# Defining some expression
expression=(4*p**2)+(12*p*q)+(9*p**4)+(11*p)+(2*p**5)

# Printing expression
pprint(expression)

# collect function
pprint(collect(expression, p))

Output - 
   5      4      2                
2.p  + 9.p  + 4.p  + 12.p.q + 11.p

   5      4      2                
2.p  + 9.p  + 4.p  + p.(12.q + 11)

Cancel function:

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

# Defining some expression
expression1 = (p**2)-(4)
expression2 = (p-2)

# Printing expressions
pprint(expression1)
pprint(expression2)

# collect function
cancel(expression1/expression2)

Output - 
 2    
p  - 4
p - 2
𝑝+2

In this way, we can use collect and cancel functions in sympy.

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

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 Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.