使用 Sympy 合并 2 个代数表达式
Merge 2 algebraic expression using Sympy
如何合并 2 个或更多表达式
示例:3x + 2y - 2 和 2x + 3y -1 (3x + 2y -2 = 2x + 3y -1)
给出 x-y-1
from sympy import *
# declare symbols
var("x, y")
# write the two expressions
expr1 = 3*x + 2*y - 2
expr2 = 2*x + 3*y - 1
# subtract them
result = expr1 - expr2
result
# out: x - y - 1
编辑:或者,我们也可以创建一个 Equality
对象,然后将其重写为附加项:
eqaulity = Eq(expr1, expr2)
# out: 3*x + 2*y - 2 = 2*x + 3*y - 1
eqaulity.rewrite(Add)
# out: x - y - 1
如何合并 2 个或更多表达式 示例:3x + 2y - 2 和 2x + 3y -1 (3x + 2y -2 = 2x + 3y -1) 给出 x-y-1
from sympy import *
# declare symbols
var("x, y")
# write the two expressions
expr1 = 3*x + 2*y - 2
expr2 = 2*x + 3*y - 1
# subtract them
result = expr1 - expr2
result
# out: x - y - 1
编辑:或者,我们也可以创建一个 Equality
对象,然后将其重写为附加项:
eqaulity = Eq(expr1, expr2)
# out: 3*x + 2*y - 2 = 2*x + 3*y - 1
eqaulity.rewrite(Add)
# out: x - y - 1