Python| sympy.combsimp() 方法
借助sympy.combsimp()方法,我们可以简化组合表达式。
Syntax: combsimp(expression)
Parameter:
expression – It is combinatorial expression which needs to be simplified.
Returns: Returns a simplified mathematical expression corresponding to the input.
示例 #1:
# import sympy
from sympy import *
n = symbols('n', integer = True)
expr = factorial(n)/factorial(n - 3)
print("Expression = {}".format(expr))
# Use sympy.combsimp() method
simple_expr = combsimp(expr)
print("Simplified Expression : {}".format(simple_expr))
输出:
Expression = factorial(n)/factorial(n - 3)
Simplified Expression : n*(n - 2)*(n - 1)
示例 #2:
# import sympy
from sympy import *
n, k = symbols('n k', integer = True)
expr = binomial(n + 1, k + 1)/binomial(n, k)
print("Expression = {}".format(expr))
# Use sympy.combsimp() method
simple_expr = combsimp(expr)
print("Simplified Expression : {}".format(simple_expr))
输出:
Expression = binomial(n+1, k+1)/binomial(n, k)
Simplified Expression : (n + 1)/(k + 1)