Skip to content
Related Articles

Related Articles

Python | sympy.replace() method
  • Last Updated : 26 Jul, 2019

With the help of sympy.replace() method, we can replace the functions in the mathematical expression without editing the whole expression by using sympy.replace() method.

Syntax : sympy.replace()
Return : Return the replaced values in the mathematical expression.

Example #1 :
In this example we can see that by using sympy.replace() method, we are able to replace the mathematical functions in the expression and return the updated expression.




# import sympy
from sympy import * 
  
x, y = symbols('x y')
  
f = sin(x) + cos(2 * x)
  
# Use sympy.replace() method
gfg = f.replace(cos, sin)
     
print(gfg)

Output :

sin(x) + sin(2*x)



Example #2 :




# import sympy
from sympy import * 
  
x, y = symbols('x y')
  
f = log(sin(x)) + tan(cos(2 * x))
# Use sympy.replace() method
gfg = f.replace(log, tan)
     
print(gfg)

Output :

tan(sin(x)) + tan(cos(2*x))

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :