I'm trying to solve these three equations:
a1 + a2 = b1
a2 + a3 = b2
b1 + b2 = c1
If I've correctly understood what you're asking, you can use Sympy's linsolve
function to solve the equations symbolically first, then substitute in numbers afterwards. The key step is to tell linsolve
which variables you want to solve for. I suggest using set
s to separate the variables you're solving for from the ones you want to plug in values for. You might declare your variables like this:
all_vars = symbols('a1 a2 a3 b1 b2 c1')
# randomly choose which variables will have values
plug_in_for_vars = ...
solve_for_vars = tuple(set(all_vars) - set(plug_in_for_vars))
Then you can define your equations
equations = [a1 + a2 - b1, a2 + a3 - b2, b1 + b2 - c1]
and pass them and the chosen variables to linsolve
solution = linsolve(equations, solve_for_vars)
Then you can plug in the chosen values.
solution.subs({variable: value(variable) for variable in plug_in_for_vars})
Of course value(variable)
is a proxy for whatever you do to determine the numeric values you want to plug in.
There are only 19 different ways to do this, so it's not too bad to just do this symbolically to see all possibilities:
from sympy import symbols, subsets, solve, Dict
v = a1, a2, a3, b1, b2, c1 = symbols('a1 a2 a3 b1 b2 c1')
eqs = (a1 + a2 - b1, a2 + a3 - b2, b1 + b2 - c1)
# we will not assign b1,b2,c1 so we will never solve for a1,a2,a3
ignore = set(v[:3])
for i in subsets(v, 3):
if set(i) != ignore:
subs = set(v)-set(i)
sol = solve(eqs,i,dict=True)[0]
print('replace',subs,'solve for',set(sol),':\n\t', sol)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.