0

I am not sure what the concept is called but let me explain. I apologize if this is something trivial, but I am not sure what this method is called or which library to use. My goal is to solve for one variable of a formula if I have the other two. For example: I want to define an equation as:

A + B = C

Pseudo code

def solve_equation(a, b, or c): # I know this does not make sense form a syntax perspective 
    formula = ( c = a + b)
    solve(formula, x) # x would be the the desirable variable e.g. inputs a,c, then x = b
    return x

Solve for A

A = C - B

solve_equation(b = 2, c = 3)

Output = 1

Solve for B

B = C - A

solve_equation(a = 1, c = 3)

Output = 2

Solve for C

C = A + B

solve_equation(a = 1, b = 2)

Output = 3

Hopefully, this explains a little better of what my goal is. I have used solve() before, and I have symbolically rearranged formulas to my need. What I am confused is how to do both at the same time. I work with complex formulas with many more variables, so manually coding the formula to solve every variable becomes complicated. Thank you again for the help.

  • This is not a trivial problem for general cases (since you mentioned that you _"work with complex formulas with many more variables"_). You might have a look at third party libraries such as SymPy. – Selcuk May 06 '20 at 01:18
  • I mainly use sympy, I am not sure how to implement the problem above. I have used their solve feature. – Luis Enriquez-Contreras May 06 '20 at 01:23
  • You can also do something like this. `a=1;b=2; c = eval("a+b")` as a quick dirty trick. – iamvegan May 06 '20 at 01:24
  • @iamvegan What if they want to solve for `a` instead? – Selcuk May 06 '20 at 01:27
  • @LuisEnriquez-Contreras `I am not sure how to implement the problem above. I have used their solve feature.`: In that case, it is better to post the code you wrote that did not work and ask for help about that. – Selcuk May 06 '20 at 01:28

0 Answers0