Home > Enterprise >  How to find the unknown variables using the given variables and equations?
How to find the unknown variables using the given variables and equations?

Time:04-14

I have an equation a-bcd/e = 0 in which any one of the variables is missing and all the other variables are given. My current way of doing this is using sympy and a lot of if conditions.

a,b,c,d,e = [sympy.Dummy()]*5
equation = a-b*c*d/e
if a is None:
  a = sympy.solve(equation.subs([(b,12),(c,10),(d,5),(e,4)]),a)
elif b is None:
  .....
elif e is None:
  e = sympy.solve(equation.subs([(b,12),(c,10),(d,5),(a,3)]),e)

This is quite cumbersome and definitely not scalable when multiple equations need to be implemented. Is there a better way to do this?

CodePudding user response:

Use a loop to iterate through all the variables and when you hit None, solve it.

Pseudocode

For variable in ListOfVariables
  if variable is None: 
    knowns = # Make a list of tuples of everything except this variable
    answer = solve(knowns, solve)
    break
  • Related