Python| sympy.simplify() 方法
借助sympy.simplify()方法,我们可以简化任何数学表达式。
Syntax: simplify(expression)
Parameters:
expression – It is the mathematical expression which needs to be simplified.
Returns: Returns a simplified mathematical expression corresponding to the input expression.
示例 #1:
在这个例子中,我们可以看到通过使用sympy.lambdify()方法,我们可以简化任何数学表达式。
# import sympy
from sympy import *
x = symbols('x')
expr = sin(x)**2 + cos(x)**2
print("Before Simplification : {}".format(expr))
# Use sympy.simplify() method
smpl = simplify(expr)
print("After Simplification : {}".format(smpl))
输出:
Before Simplification : sin(x)**2 + cos(x)**2
After Simplification : 1
示例 #2:
# import sympy
from sympy import *
x = symbols('x')
expr = (x**3 + x**2 - x - 1)/(x**2 + 2 * x + 1)
print("Before Simplification : {}".format(expr))
# Use sympy.simplify() method
smpl = simplify(expr)
print("After Simplification : {}".format(smpl))
输出:
Before Simplification : (x**3 + x**2 - x - 1)/(x**2 + 2*x + 1)
After Simplification : After Simplification : x - 1