Python| sympy.replace() 方法
借助sympy.replace()
方法,我们可以替换数学表达式中的函数,而无需使用sympy.replace()
方法编辑整个表达式。
Syntax : sympy.replace()
Return : Return the replaced values in the mathematical expression.
示例 #1:
在这个例子中,我们可以看到通过使用sympy.replace()
方法,我们能够替换表达式中的数学函数并返回更新后的表达式。
# 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)
输出 :
sin(x) + sin(2*x)
示例 #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)
输出 :
tan(sin(x)) + tan(cos(2*x))