我正在使用sympy处理一些方程式。我想以规范的形式编写方程,这样感兴趣的变量都在LHS上。例如。如果我有,
lhs = sympify("e*x +f")`
rhs = sympify("g*y + t*x +h")`
eq = Eq(lhs,rhs)
e*x + f == g*y + h + t*x
我需要一个可以隔离给定变量列表的函数(我所谓的规范形式),例如
IsolateVariablesToLHS(eq,[x,y]) # desired function
(e-t)*x - g*y == h-f # now x and y are on LHS and remaining are on RHS
我确信我只会得到线性方程,所以这总是可能的。
>>> import sympy as sm
>>> lhs = sm.sympify('e*x + f')
>>> rhs = sm.sympify('g*y + t*x + h')
>>> eq = sm.Eq(lhs, rhs)
这是一个简单的构造
def isolateVariablesToLHS(eq, syms):
l = sm.S.Zero
eq = eq.args[0] - eq.args[1]
for e in syms:
ind = eq.as_independent(e)[1]
l += ind
eq -= ind
return sm.Eq(l, eq)
>>> isolateVariablesToLHS(eq, [x, y])
Eq(e*x - g*y - t*x, f - h)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句