With the help of
Python3 1=1
Output :
Python3 1=1
sympy.collect() method, we are able to collect the mathematical expressions having same power.
Syntax : sympy.collect()
Return : Return mathematical expression having same powers.
Example #1 :
In this example, we can see that by using sympy.collect() method, all the same powers of variables are collected together as a same mathematical expression.
# import sympy
from sympy import * x, y, z = symbols('x y z')
gfg_exp = x * y + x - 3 + 2 * x**2 - z * x**2 + x**3
# Using sympy.collect() method
gfg_exp = collect(gfg_exp, x)
print(gfg_exp)
x**3 + x**2*(2 - z) + x*(y + 1) - 3Example #2 :
# import sympy
from sympy import * x, y, z = symbols('x y z')
gfg_exp = x * x + x - 3 + 2 * z**2 - y * x**2 + x**3
# Using sympy.collect() method
gfg_exp = collect(gfg_exp, x)
print(gfg_exp)
Output :
x**3 + x**2*(1 - y) + x + 2*z**2 - 3