python 的多元方程解

multivariate equations solution with python

我对以下等式有疑问:

I have equations:
a+b=10
a+b+c+d=20

I need the answer(all results must be positive):
a=0 b=10 c=0 d=10
a=0 b=10 c=1 d=9
...

我需要所有可能的解决方案,我可以使用python来解决吗?

解有无限多,但不代表变量是独立的

# importing sympy and its friends
import sympy as sm
from sympy import symbols 
from sympy import init_printing
from sympy.printing.mathml import print_mathml
# define symbols
a,b,c,d=symbols('a,b,c,d',real=True)
# equations are in form f(x1,x2,...,xn)=0
eq1 = a+b-10
eq2 = a+b+c+d-20
# solve the system of equations for [a,b,c,d]
print(sm.solve([eq1,eq2],[a,b,c,d]))

输出

{c: 10 - d, a: 10 - b}

因此正解为d =[0;10)b=[0,10)。有关详细信息,请参阅 sympy