Python | sympy.xreplace() method
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.
from sympy import *
x, y = symbols( 'x y' )
f = sin(x) + cos( 2 * x)
gfg = f.xreplace({ 2 * x : y})
print (gfg)
|
Output :
sin(x) + cos(y)
Example #2 :
from sympy import *
x, y = symbols( 'x y' )
f = log(sin(x)) + tan(cos( 2 * x))
gfg = f.xreplace({ x : 2 * y})
print (gfg)
|
Output :
log(sin(2*y)) + tan(cos(4*y))
Last Updated :
26 Jul, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...