I tried to use Piecewise operation in sympy to solve a equation where a part cannot be less then zero, but it's computationaly expansive.
from sympy import symbols, solveset, Max, Piecewise, simplify
S, T1, T2, T3, T4, A, B, C, D = symbols('S T1 T2 T3 T4 A B C D')
soma = Piecewise((S - T1, S-T1 > 0), (0, S- T1 < 0)) + Piecewise((T2 - S, T2 - S > 0), (0, T2 - S < 0)) + \
- Piecewise((S - T3, S-T3 > 0), (0, S - T3 < 0)) - Piecewise((T4 - S, T4 - S > 0), (0, T4 - S < 0)) - A - B + C + D
soma = simplify(soma)
solve(soma,S)
The whole equation when I simulate using pure python can be defined as:
soma = max(S-T1,0) + max(T2-S,0) - max(S-T3,0) - max(T4-S,0) - A - B + C + D
then I find S when soma = 0
Note some proprieties:
- S, T1, T2, T3, T4 are always greater than 0 ( > 0)
- A, B, C, D are always greater than 0 too ( > 0)
I tried to write the whole equation (S - T1) - (S - T3) + (T2 - S) - (T4 - S) and define the inequalities but is obvious that S get cancellated.
Note that I'm not defining the variables because it's symbolic math
*edit - I need to solve the equation for S when soma == 0
It is not clear what you want as output. This is what I get solving case by case:
Now solve case by case: