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 sympyfrom sympy import * x, y = symbols('x y') f = sin(x) + cos(2 * x)# Use sympy.xreplace() methodgfg = f.xreplace({2 * x : y}) print(gfg) |
Output :
sin(x) + cos(y)
Example #2 :
# import sympyfrom sympy import * x, y = symbols('x y') f = log(sin(x)) + tan(cos(2 * x))# Use sympy.xreplace() methodgfg = 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


