I have a set of a few hundred simple sum equations. For example here are 3:
w1 - u1 - u2 = 0
w1 + w2 - w3 - u3 = 0
w1 - w2 - w4 = 0
I am trying to find a way to solve as many as possible given only a few of the values. For example, in the above equation set if I have u1
and u2
I can calculate w1
, but nothing else.
Given u1
, u2
and w2
I can calculate w1
and w4
. And so forth...
Currently I'm approaching this in a fairly straight forward way (psudo code):
while there are new results:
for each equation:
try to solve equation:
if solved update result set
This works but feels clunky and inefficient.
Is there a better way? (using Python if that is relevant)
EDIT: I know this could be solved as series of linear equations, IF i know enough of the values. I'm looking for a method to use when I don't know enough to solve as a system of linear equations (or possibly there a fancy way to reduce the problem somehow)
EDIT 2: erroneous, deleted
EDIT 3: For anyone interested in my solution using sympy:
from sympy import linsolve, symbols, linear_eq_to_matrix
w1, w2, w3, w4, u1, u2, u3 = symbols("w1, w2, w3, w4, u1, u2, u3", integer=True)
variables = [w1, w2, w3, w4, u1, u2, u3]
def my_solver(known_vals):
eqns = [w1 - u1 - u2,
w1 + w2 - w3 - u3,
w1 - w2 - w4]
#add the known variables to equation list
for x in known_vals.keys():
eqns.append(x - (known_vals[x]))
A, b = linear_eq_to_matrix(eqns, variables)
solution = linsolve((A, b), variables)
return solution
my_solver({w1:2, u2:-2})