The Wayback Machine - http://web.archive.org/web/20210503214119/https://www.geeksforgeeks.org/python-sympy-xreplace-method/
Skip to content
Related Articles

Related Articles

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

With the help of sympy.xreplace() method, we can replace the variable x with any other variable of the functions in the mathematical expression without editing the whole expression by using sympy.xreplace() method.

Syntax : sympy.xreplace()
Return : Return the replaced value of x in the mathematical expression.

Example #1 :
In this example we can see that by using sympy.xreplace() method, we are able to replace the x variable of 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.xreplace() method
gfg = f.xreplace({2 * x : y})
     
print(gfg)

Output :

sin(x) + cos(y)



Example #2 :




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

Output :

log(sin(2*y)) + tan(cos(4*y))

 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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :