I want to use nypy.solve to convert a formular to a certain variable. In my example I want to convert the formular a >= ((b-c)/2)*sy.tan(d) to the variable b. But somehow the result of Sympy is not converted to b.
This is my Code:
import numpy as np
import math
import sympy as sy
a = sy.symbols('a')
b = sy.symbols('b')
c = sy.symbols('c')
d = sy.symbols('d')
eq = sy.Ge(a, ((b-c)/2)*sy.tan(d))
result = sy.solve(eq, 'b')
print(result)
The output is this:
-b*tan(d)/2 >= -a - c*tan(d)/2
what i want to get is:
b >= (2*a+c*tan(d))/tan(d)
How do I use sympy correctly to get this?
SymPy won't divide by a factor of an unknown sign when trying to solve an inequality. So use a signed symbol for
tan(d), e.g.Dummy(positive=True)orDummy(negative=True)and put your results inPiecewise((results_pos, (tan(d)>0)),(result_neg, tan(d)<0)):If you are solving linear inequalities, you can just re-arrange the equation by hand instead of using
solve:I highly recommend that you use
Piecewiseso you don't lose track of what it was that you need to assume is positive. In the example I gave, the value ofycould be-1/2and the pertinent coefficient ofxwould still be positive.{note use of
from sympy import *at the start which imports all the needed object for this example.}